blob: 10e5bf4601398c8723d82b5f3340f9b246d341e5 [file] [log] [blame]
Jon Masonfce8a7b2012-11-16 19:27:12 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -04008 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * BSD LICENSE
15 *
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -040017 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -070018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
28 * distribution.
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
Allen Hubbee26a5842015-04-09 10:33:20 -040045 * PCIe NTB Transport Linux driver
Jon Masonfce8a7b2012-11-16 19:27:12 -070046 *
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
49 */
50#include <linux/debugfs.h>
51#include <linux/delay.h>
Jon Mason282a2fe2013-02-12 09:52:50 -070052#include <linux/dmaengine.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070053#include <linux/dma-mapping.h>
54#include <linux/errno.h>
55#include <linux/export.h>
56#include <linux/interrupt.h>
57#include <linux/module.h>
58#include <linux/pci.h>
59#include <linux/slab.h>
60#include <linux/types.h>
Dave Jiang06917f72015-05-19 16:45:46 -040061#include <linux/uaccess.h>
Allen Hubbee26a5842015-04-09 10:33:20 -040062#include "linux/ntb.h"
63#include "linux/ntb_transport.h"
Jon Masonfce8a7b2012-11-16 19:27:12 -070064
Allen Hubbee26a5842015-04-09 10:33:20 -040065#define NTB_TRANSPORT_VERSION 4
66#define NTB_TRANSPORT_VER "4"
67#define NTB_TRANSPORT_NAME "ntb_transport"
68#define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +053069#define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
Allen Hubbee26a5842015-04-09 10:33:20 -040070
71MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
72MODULE_VERSION(NTB_TRANSPORT_VER);
73MODULE_LICENSE("Dual BSD/GPL");
74MODULE_AUTHOR("Intel Corporation");
75
76static unsigned long max_mw_size;
77module_param(max_mw_size, ulong, 0644);
78MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
Jon Masonfce8a7b2012-11-16 19:27:12 -070079
Dave Jiang98914172015-06-03 11:29:38 -040080static unsigned int transport_mtu = 0x10000;
Jon Masonfce8a7b2012-11-16 19:27:12 -070081module_param(transport_mtu, uint, 0644);
82MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
83
Jon Mason948d3a62013-04-18 17:07:36 -070084static unsigned char max_num_clients;
Jon Masonfce8a7b2012-11-16 19:27:12 -070085module_param(max_num_clients, byte, 0644);
86MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
87
Jon Mason282a2fe2013-02-12 09:52:50 -070088static unsigned int copy_bytes = 1024;
89module_param(copy_bytes, uint, 0644);
90MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
91
Dave Jianga41ef052015-05-19 16:52:04 -040092static bool use_dma;
93module_param(use_dma, bool, 0644);
94MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
95
Allen Hubbee26a5842015-04-09 10:33:20 -040096static struct dentry *nt_debugfs_dir;
97
Jon Masonfce8a7b2012-11-16 19:27:12 -070098struct ntb_queue_entry {
99 /* ntb_queue list reference */
100 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400101 /* pointers to data to be transferred */
Jon Masonfce8a7b2012-11-16 19:27:12 -0700102 void *cb_data;
103 void *buf;
104 unsigned int len;
105 unsigned int flags;
Dave Jiang9cabc262016-07-20 13:14:07 -0700106 int retries;
107 int errors;
108 unsigned int tx_index;
Dave Jiang72203572016-07-20 13:14:13 -0700109 unsigned int rx_index;
Jon Mason282a2fe2013-02-12 09:52:50 -0700110
111 struct ntb_transport_qp *qp;
112 union {
113 struct ntb_payload_header __iomem *tx_hdr;
114 struct ntb_payload_header *rx_hdr;
115 };
Jon Masonfce8a7b2012-11-16 19:27:12 -0700116};
117
Jon Mason793c20e2013-01-19 02:02:26 -0700118struct ntb_rx_info {
119 unsigned int entry;
120};
121
Jon Masonfce8a7b2012-11-16 19:27:12 -0700122struct ntb_transport_qp {
Allen Hubbee26a5842015-04-09 10:33:20 -0400123 struct ntb_transport_ctx *transport;
124 struct ntb_dev *ndev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700125 void *cb_data;
Dave Jiang569410ca2015-07-13 08:07:22 -0400126 struct dma_chan *tx_dma_chan;
127 struct dma_chan *rx_dma_chan;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700128
129 bool client_ready;
Allen Hubbee26a5842015-04-09 10:33:20 -0400130 bool link_is_up;
Dave Jiange9021332016-02-23 09:11:36 -0700131 bool active;
Allen Hubbee26a5842015-04-09 10:33:20 -0400132
Jon Masonfce8a7b2012-11-16 19:27:12 -0700133 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400134 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700135
Jon Mason74465642013-01-21 15:28:52 -0700136 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -0700137 struct ntb_rx_info *remote_rx_info;
138
Jon Mason53ca4fe2013-11-26 11:21:50 -0700139 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
140 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700141 struct list_head tx_free_q;
142 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700143 void __iomem *tx_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700144 dma_addr_t tx_mw_phys;
Jon Mason793c20e2013-01-19 02:02:26 -0700145 unsigned int tx_index;
146 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700147 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700148
Jon Mason53ca4fe2013-11-26 11:21:50 -0700149 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
150 void *data, int len);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400151 struct list_head rx_post_q;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700152 struct list_head rx_pend_q;
153 struct list_head rx_free_q;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400154 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
155 spinlock_t ntb_rx_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700156 void *rx_buff;
157 unsigned int rx_index;
158 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700159 unsigned int rx_max_frame;
Dave Jianga754a8f2016-04-08 10:49:06 -0700160 unsigned int rx_alloc_entry;
Jon Mason282a2fe2013-02-12 09:52:50 -0700161 dma_cookie_t last_cookie;
Allen Hubbee26a5842015-04-09 10:33:20 -0400162 struct tasklet_struct rxc_db_work;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700163
Jon Mason53ca4fe2013-11-26 11:21:50 -0700164 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700165 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700166 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700167
168 struct dentry *debugfs_dir;
169 struct dentry *debugfs_stats;
170
171 /* Stats */
172 u64 rx_bytes;
173 u64 rx_pkts;
174 u64 rx_ring_empty;
175 u64 rx_err_no_buf;
176 u64 rx_err_oflow;
177 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700178 u64 rx_memcpy;
179 u64 rx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700180 u64 tx_bytes;
181 u64 tx_pkts;
182 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700183 u64 tx_err_no_buf;
184 u64 tx_memcpy;
185 u64 tx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700186};
187
188struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400189 phys_addr_t phys_addr;
190 resource_size_t phys_size;
191 resource_size_t xlat_align;
192 resource_size_t xlat_align_size;
193 void __iomem *vbase;
194 size_t xlat_size;
195 size_t buff_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700196 void *virt_addr;
197 dma_addr_t dma_addr;
198};
199
200struct ntb_transport_client_dev {
201 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400202 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700203 struct device dev;
204};
205
Allen Hubbee26a5842015-04-09 10:33:20 -0400206struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700207 struct list_head entry;
208 struct list_head client_devs;
209
Allen Hubbee26a5842015-04-09 10:33:20 -0400210 struct ntb_dev *ndev;
211
212 struct ntb_transport_mw *mw_vec;
213 struct ntb_transport_qp *qp_vec;
214 unsigned int mw_count;
215 unsigned int qp_count;
216 u64 qp_bitmap;
217 u64 qp_bitmap_free;
218
219 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700220 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700221 struct work_struct link_cleanup;
Dave Jiangc8650fd2015-07-13 08:07:09 -0400222
223 struct dentry *debugfs_node_dir;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700224};
225
226enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400227 DESC_DONE_FLAG = BIT(0),
228 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700229};
230
231struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700232 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700233 unsigned int len;
234 unsigned int flags;
235};
236
237enum {
238 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700239 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700240 NUM_QPS,
241 NUM_MWS,
242 MW0_SZ_HIGH,
243 MW0_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700244};
245
Allen Hubbee26a5842015-04-09 10:33:20 -0400246#define dev_client_dev(__dev) \
247 container_of((__dev), struct ntb_transport_client_dev, dev)
248
249#define drv_client(__drv) \
250 container_of((__drv), struct ntb_transport_client, driver)
251
252#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700253#define NTB_QP_DEF_NUM_ENTRIES 100
254#define NTB_LINK_DOWN_TIMEOUT 10
255
Allen Hubbee26a5842015-04-09 10:33:20 -0400256static void ntb_transport_rxc_db(unsigned long data);
257static const struct ntb_ctx_ops ntb_transport_ops;
258static struct ntb_client ntb_transport_client;
Dave Jiang9cabc262016-07-20 13:14:07 -0700259static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
260 struct ntb_queue_entry *entry);
261static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
Dave Jiang72203572016-07-20 13:14:13 -0700262static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
263static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
264
Allen Hubbee26a5842015-04-09 10:33:20 -0400265
266static int ntb_transport_bus_match(struct device *dev,
267 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700268{
269 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
270}
271
Allen Hubbee26a5842015-04-09 10:33:20 -0400272static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700273{
Allen Hubbee26a5842015-04-09 10:33:20 -0400274 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700275 int rc = -EINVAL;
276
277 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400278
279 client = drv_client(dev->driver);
280 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700281 if (rc)
282 put_device(dev);
283
284 return rc;
285}
286
Allen Hubbee26a5842015-04-09 10:33:20 -0400287static int ntb_transport_bus_remove(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700288{
Allen Hubbee26a5842015-04-09 10:33:20 -0400289 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700290
Allen Hubbee26a5842015-04-09 10:33:20 -0400291 client = drv_client(dev->driver);
292 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700293
294 put_device(dev);
295
296 return 0;
297}
298
Allen Hubbee26a5842015-04-09 10:33:20 -0400299static struct bus_type ntb_transport_bus = {
300 .name = "ntb_transport",
301 .match = ntb_transport_bus_match,
302 .probe = ntb_transport_bus_probe,
303 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700304};
305
306static LIST_HEAD(ntb_transport_list);
307
Allen Hubbee26a5842015-04-09 10:33:20 -0400308static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700309{
Dave Jiang31510002015-07-13 08:07:19 -0400310 list_add_tail(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700311 return 0;
312}
313
Allen Hubbee26a5842015-04-09 10:33:20 -0400314static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700315{
316 struct ntb_transport_client_dev *client_dev, *cd;
317
318 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
319 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
320 dev_name(&client_dev->dev));
321 list_del(&client_dev->entry);
322 device_unregister(&client_dev->dev);
323 }
324
325 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700326}
327
Allen Hubbee26a5842015-04-09 10:33:20 -0400328static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700329{
330 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700331
Allen Hubbee26a5842015-04-09 10:33:20 -0400332 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700333 kfree(client_dev);
334}
335
336/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400337 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700338 * @device_name: Name of NTB client device
339 *
340 * Unregister an NTB client device with the NTB transport layer
341 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400342void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700343{
344 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400345 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700346
347 list_for_each_entry(nt, &ntb_transport_list, entry)
348 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
349 if (!strncmp(dev_name(&client->dev), device_name,
350 strlen(device_name))) {
351 list_del(&client->entry);
352 device_unregister(&client->dev);
353 }
354}
Allen Hubbee26a5842015-04-09 10:33:20 -0400355EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700356
357/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400358 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700359 * @device_name: Name of NTB client device
360 *
361 * Register an NTB client device with the NTB transport layer
362 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400363int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700364{
365 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400366 struct ntb_transport_ctx *nt;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400367 int node;
Jon Mason8b19d452013-04-26 14:51:57 -0700368 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700369
Jon Mason8222b402013-01-19 02:02:16 -0700370 if (list_empty(&ntb_transport_list))
371 return -ENODEV;
372
Jon Masonfce8a7b2012-11-16 19:27:12 -0700373 list_for_each_entry(nt, &ntb_transport_list, entry) {
374 struct device *dev;
375
Allen Hubbe1199aa62015-05-18 06:20:47 -0400376 node = dev_to_node(&nt->ndev->dev);
377
378 client_dev = kzalloc_node(sizeof(*client_dev),
379 GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700380 if (!client_dev) {
381 rc = -ENOMEM;
382 goto err;
383 }
384
385 dev = &client_dev->dev;
386
387 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700388 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400389 dev->bus = &ntb_transport_bus;
390 dev->release = ntb_transport_client_release;
391 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700392
393 rc = device_register(dev);
394 if (rc) {
395 kfree(client_dev);
396 goto err;
397 }
398
399 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700400 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700401 }
402
403 return 0;
404
405err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400406 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700407
408 return rc;
409}
Allen Hubbee26a5842015-04-09 10:33:20 -0400410EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700411
412/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400413 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700414 * @drv: NTB client driver to be registered
415 *
416 * Register an NTB client driver with the NTB transport layer
417 *
418 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
419 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400420int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700421{
Allen Hubbee26a5842015-04-09 10:33:20 -0400422 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700423
Jon Mason8222b402013-01-19 02:02:16 -0700424 if (list_empty(&ntb_transport_list))
425 return -ENODEV;
426
Jon Masonfce8a7b2012-11-16 19:27:12 -0700427 return driver_register(&drv->driver);
428}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400429EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700430
431/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400432 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700433 * @drv: NTB client driver to be unregistered
434 *
435 * Unregister an NTB client driver with the NTB transport layer
436 *
437 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
438 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400439void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700440{
441 driver_unregister(&drv->driver);
442}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400443EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700444
Jon Masonfce8a7b2012-11-16 19:27:12 -0700445static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
446 loff_t *offp)
447{
448 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700449 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700450 ssize_t ret, out_offset, out_count;
451
Dave Jiang260bee92015-07-13 08:07:11 -0400452 qp = filp->private_data;
453
454 if (!qp || !qp->link_is_up)
455 return 0;
456
Jon Mason282a2fe2013-02-12 09:52:50 -0700457 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700458
459 buf = kmalloc(out_count, GFP_KERNEL);
460 if (!buf)
461 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700462
Jon Masonfce8a7b2012-11-16 19:27:12 -0700463 out_offset = 0;
464 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400465 "\nNTB QP stats:\n\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700466 out_offset += snprintf(buf + out_offset, out_count - out_offset,
467 "rx_bytes - \t%llu\n", qp->rx_bytes);
468 out_offset += snprintf(buf + out_offset, out_count - out_offset,
469 "rx_pkts - \t%llu\n", qp->rx_pkts);
470 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700471 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
472 out_offset += snprintf(buf + out_offset, out_count - out_offset,
473 "rx_async - \t%llu\n", qp->rx_async);
474 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700475 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
476 out_offset += snprintf(buf + out_offset, out_count - out_offset,
477 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
478 out_offset += snprintf(buf + out_offset, out_count - out_offset,
479 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
480 out_offset += snprintf(buf + out_offset, out_count - out_offset,
481 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
482 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400483 "rx_buff - \t0x%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700484 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700485 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700486 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jianga754a8f2016-04-08 10:49:06 -0700487 "rx_max_entry - \t%u\n", qp->rx_max_entry);
488 out_offset += snprintf(buf + out_offset, out_count - out_offset,
489 "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700490
491 out_offset += snprintf(buf + out_offset, out_count - out_offset,
492 "tx_bytes - \t%llu\n", qp->tx_bytes);
493 out_offset += snprintf(buf + out_offset, out_count - out_offset,
494 "tx_pkts - \t%llu\n", qp->tx_pkts);
495 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700496 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
497 out_offset += snprintf(buf + out_offset, out_count - out_offset,
498 "tx_async - \t%llu\n", qp->tx_async);
499 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700500 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
501 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700502 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
503 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400504 "tx_mw - \t0x%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700505 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400506 "tx_index (H) - \t%u\n", qp->tx_index);
507 out_offset += snprintf(buf + out_offset, out_count - out_offset,
508 "RRI (T) - \t%u\n",
509 qp->remote_rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700510 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700511 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400512 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiange74bfee2015-07-13 08:07:17 -0400513 "free tx - \t%u\n",
514 ntb_transport_tx_free_entry(qp));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700515
516 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400517 "\n");
518 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiang569410ca2015-07-13 08:07:22 -0400519 "Using TX DMA - \t%s\n",
520 qp->tx_dma_chan ? "Yes" : "No");
521 out_offset += snprintf(buf + out_offset, out_count - out_offset,
522 "Using RX DMA - \t%s\n",
523 qp->rx_dma_chan ? "Yes" : "No");
Dave Jiangd98ef992015-07-13 08:07:20 -0400524 out_offset += snprintf(buf + out_offset, out_count - out_offset,
525 "QP Link - \t%s\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400526 qp->link_is_up ? "Up" : "Down");
Dave Jiangd98ef992015-07-13 08:07:20 -0400527 out_offset += snprintf(buf + out_offset, out_count - out_offset,
528 "\n");
529
Jon Masond7237e22013-01-19 02:02:25 -0700530 if (out_offset > out_count)
531 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700532
533 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700534 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700535 return ret;
536}
537
538static const struct file_operations ntb_qp_debugfs_stats = {
539 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700540 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700541 .read = debugfs_read,
542};
543
544static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
545 struct list_head *list)
546{
547 unsigned long flags;
548
549 spin_lock_irqsave(lock, flags);
550 list_add_tail(entry, list);
551 spin_unlock_irqrestore(lock, flags);
552}
553
554static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700555 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700556{
557 struct ntb_queue_entry *entry;
558 unsigned long flags;
559
560 spin_lock_irqsave(lock, flags);
561 if (list_empty(list)) {
562 entry = NULL;
563 goto out;
564 }
565 entry = list_first_entry(list, struct ntb_queue_entry, entry);
566 list_del(&entry->entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400567
Jon Masonfce8a7b2012-11-16 19:27:12 -0700568out:
569 spin_unlock_irqrestore(lock, flags);
570
571 return entry;
572}
573
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400574static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
575 struct list_head *list,
576 struct list_head *to_list)
577{
578 struct ntb_queue_entry *entry;
579 unsigned long flags;
580
581 spin_lock_irqsave(lock, flags);
582
583 if (list_empty(list)) {
584 entry = NULL;
585 } else {
586 entry = list_first_entry(list, struct ntb_queue_entry, entry);
587 list_move_tail(&entry->entry, to_list);
588 }
589
590 spin_unlock_irqrestore(lock, flags);
591
592 return entry;
593}
594
Allen Hubbee26a5842015-04-09 10:33:20 -0400595static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
596 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700597{
Allen Hubbee26a5842015-04-09 10:33:20 -0400598 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
599 struct ntb_transport_mw *mw;
Dave Jianga754a8f2016-04-08 10:49:06 -0700600 struct ntb_dev *ndev = nt->ndev;
601 struct ntb_queue_entry *entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700602 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400603 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700604 unsigned int i;
Dave Jianga754a8f2016-04-08 10:49:06 -0700605 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700606
Allen Hubbee26a5842015-04-09 10:33:20 -0400607 mw_count = nt->mw_count;
608 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700609
Allen Hubbee26a5842015-04-09 10:33:20 -0400610 mw_num = QP_TO_MW(nt, qp_num);
611 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700612
Allen Hubbee26a5842015-04-09 10:33:20 -0400613 if (!mw->virt_addr)
614 return -ENOMEM;
615
Logan Gunthorpe8e8496e2017-06-05 14:00:53 -0600616 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -0400617 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700618 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400619 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700620
Allen Hubbee26a5842015-04-09 10:33:20 -0400621 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -0400622 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
Jon Mason793c20e2013-01-19 02:02:26 -0700623 rx_size -= sizeof(struct ntb_rx_info);
624
Jon Mason282a2fe2013-02-12 09:52:50 -0700625 qp->remote_rx_info = qp->rx_buff + rx_size;
626
Jon Masonc9d534c2013-02-01 15:45:16 -0700627 /* Due to housekeeping, there must be atleast 2 buffs */
628 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700629 qp->rx_max_entry = rx_size / qp->rx_max_frame;
630 qp->rx_index = 0;
631
Dave Jianga754a8f2016-04-08 10:49:06 -0700632 /*
633 * Checking to see if we have more entries than the default.
634 * We should add additional entries if that is the case so we
635 * can be in sync with the transport frames.
636 */
637 node = dev_to_node(&ndev->dev);
638 for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
639 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
640 if (!entry)
641 return -ENOMEM;
642
643 entry->qp = qp;
644 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
645 &qp->rx_free_q);
646 qp->rx_alloc_entry++;
647 }
648
Jon Masonc9d534c2013-02-01 15:45:16 -0700649 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700650
Jon Masonef114ed2013-01-19 02:02:18 -0700651 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700652 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400653 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
654 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700655 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700656 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700657
658 qp->rx_pkts = 0;
659 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700660 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400661
662 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700663}
664
Allen Hubbee26a5842015-04-09 10:33:20 -0400665static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700666{
Allen Hubbee26a5842015-04-09 10:33:20 -0400667 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
668 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700669
670 if (!mw->virt_addr)
671 return;
672
Allen Hubbee26a5842015-04-09 10:33:20 -0400673 ntb_mw_clear_trans(nt->ndev, num_mw);
674 dma_free_coherent(&pdev->dev, mw->buff_size,
675 mw->virt_addr, mw->dma_addr);
676 mw->xlat_size = 0;
677 mw->buff_size = 0;
Jon Mason113fc502013-01-30 11:40:52 -0700678 mw->virt_addr = NULL;
679}
680
Allen Hubbee26a5842015-04-09 10:33:20 -0400681static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400682 resource_size_t size)
Jon Masonb77b2632013-02-01 15:25:37 -0700683{
Allen Hubbee26a5842015-04-09 10:33:20 -0400684 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
685 struct pci_dev *pdev = nt->ndev->pdev;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400686 size_t xlat_size, buff_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400687 int rc;
688
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400689 if (!size)
690 return -EINVAL;
691
Allen Hubbee26a5842015-04-09 10:33:20 -0400692 xlat_size = round_up(size, mw->xlat_align_size);
693 buff_size = round_up(size, mw->xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700694
695 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400696 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700697 return 0;
698
Allen Hubbee26a5842015-04-09 10:33:20 -0400699 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700700 ntb_free_mw(nt, num_mw);
701
Allen Hubbee26a5842015-04-09 10:33:20 -0400702 /* Alloc memory for receiving data. Must be aligned */
703 mw->xlat_size = xlat_size;
704 mw->buff_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700705
Allen Hubbee26a5842015-04-09 10:33:20 -0400706 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
707 &mw->dma_addr, GFP_KERNEL);
Jon Masonb77b2632013-02-01 15:25:37 -0700708 if (!mw->virt_addr) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400709 mw->xlat_size = 0;
710 mw->buff_size = 0;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400711 dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400712 buff_size);
Jon Masonb77b2632013-02-01 15:25:37 -0700713 return -ENOMEM;
714 }
715
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700716 /*
717 * we must ensure that the memory address allocated is BAR size
718 * aligned in order for the XLAT register to take the value. This
719 * is a requirement of the hardware. It is recommended to setup CMA
720 * for BAR sizes equal or greater than 4MB.
721 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400722 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
723 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700724 &mw->dma_addr);
725 ntb_free_mw(nt, num_mw);
726 return -ENOMEM;
727 }
728
Jon Masonb77b2632013-02-01 15:25:37 -0700729 /* Notify HW the memory location of the receive buffer */
Allen Hubbee26a5842015-04-09 10:33:20 -0400730 rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
731 if (rc) {
732 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
733 ntb_free_mw(nt, num_mw);
734 return -EIO;
735 }
Jon Masonb77b2632013-02-01 15:25:37 -0700736
737 return 0;
738}
739
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400740static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
741{
742 qp->link_is_up = false;
Dave Jiange9021332016-02-23 09:11:36 -0700743 qp->active = false;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400744
745 qp->tx_index = 0;
746 qp->rx_index = 0;
747 qp->rx_bytes = 0;
748 qp->rx_pkts = 0;
749 qp->rx_ring_empty = 0;
750 qp->rx_err_no_buf = 0;
751 qp->rx_err_oflow = 0;
752 qp->rx_err_ver = 0;
753 qp->rx_memcpy = 0;
754 qp->rx_async = 0;
755 qp->tx_bytes = 0;
756 qp->tx_pkts = 0;
757 qp->tx_ring_full = 0;
758 qp->tx_err_no_buf = 0;
759 qp->tx_memcpy = 0;
760 qp->tx_async = 0;
761}
762
Jon Masonfca4d512013-09-09 13:39:55 -0700763static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700764{
Allen Hubbee26a5842015-04-09 10:33:20 -0400765 struct ntb_transport_ctx *nt = qp->transport;
766 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700767
Allen Hubbee22e0b92015-05-12 06:55:44 -0400768 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400769
770 cancel_delayed_work_sync(&qp->link_work);
771 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400772
773 if (qp->event_handler)
774 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700775}
776
777static void ntb_qp_link_cleanup_work(struct work_struct *work)
778{
779 struct ntb_transport_qp *qp = container_of(work,
780 struct ntb_transport_qp,
781 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400782 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700783
784 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700785
Allen Hubbee26a5842015-04-09 10:33:20 -0400786 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700787 schedule_delayed_work(&qp->link_work,
788 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
789}
790
Jon Mason7b4f2d32013-01-19 02:02:19 -0700791static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700792{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700793 schedule_work(&qp->link_cleanup);
794}
795
Allen Hubbee26a5842015-04-09 10:33:20 -0400796static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700797{
Allen Hubbee26a5842015-04-09 10:33:20 -0400798 struct ntb_transport_qp *qp;
799 u64 qp_bitmap_alloc;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +0530800 unsigned int i, count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700801
Allen Hubbee26a5842015-04-09 10:33:20 -0400802 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700803
Allen Hubbee26a5842015-04-09 10:33:20 -0400804 /* Pass along the info to any clients */
805 for (i = 0; i < nt->qp_count; i++)
806 if (qp_bitmap_alloc & BIT_ULL(i)) {
807 qp = &nt->qp_vec[i];
808 ntb_qp_link_cleanup(qp);
809 cancel_work_sync(&qp->link_cleanup);
810 cancel_delayed_work_sync(&qp->link_work);
811 }
812
813 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700814 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700815
Jon Masonfce8a7b2012-11-16 19:27:12 -0700816 /* The scratchpad registers keep the values if the remote side
817 * goes down, blast them now to give them a sane value the next
818 * time they are accessed
819 */
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +0530820 count = ntb_spad_count(nt->ndev);
821 for (i = 0; i < count; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400822 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700823}
824
Jon Masonfca4d512013-09-09 13:39:55 -0700825static void ntb_transport_link_cleanup_work(struct work_struct *work)
826{
Allen Hubbee26a5842015-04-09 10:33:20 -0400827 struct ntb_transport_ctx *nt =
828 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700829
830 ntb_transport_link_cleanup(nt);
831}
832
Allen Hubbee26a5842015-04-09 10:33:20 -0400833static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700834{
Allen Hubbee26a5842015-04-09 10:33:20 -0400835 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700836
Allen Hubbee26a5842015-04-09 10:33:20 -0400837 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700838 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400839 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700840 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700841}
842
843static void ntb_transport_link_work(struct work_struct *work)
844{
Allen Hubbee26a5842015-04-09 10:33:20 -0400845 struct ntb_transport_ctx *nt =
846 container_of(work, struct ntb_transport_ctx, link_work.work);
847 struct ntb_dev *ndev = nt->ndev;
848 struct pci_dev *pdev = ndev->pdev;
849 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700850 u32 val;
Dave Jiang84f76682016-02-29 09:35:26 -0700851 int rc = 0, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700852
Jon Mason113fc502013-01-30 11:40:52 -0700853 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400854 for (i = 0; i < nt->mw_count; i++) {
855 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700856
Allen Hubbee26a5842015-04-09 10:33:20 -0400857 if (max_mw_size && size > max_mw_size)
858 size = max_mw_size;
859
860 spad = MW0_SZ_HIGH + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200861 ntb_peer_spad_write(ndev, spad, upper_32_bits(size));
Allen Hubbee26a5842015-04-09 10:33:20 -0400862
863 spad = MW0_SZ_LOW + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200864 ntb_peer_spad_write(ndev, spad, lower_32_bits(size));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700865 }
866
Allen Hubbee26a5842015-04-09 10:33:20 -0400867 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700868
Allen Hubbee26a5842015-04-09 10:33:20 -0400869 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700870
Allen Hubbee26a5842015-04-09 10:33:20 -0400871 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700872
873 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400874 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400875 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700876 if (val != NTB_TRANSPORT_VERSION)
877 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700878
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400879 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700880 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400881 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700882 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700883
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400884 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700885 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400886 if (val != nt->mw_count)
887 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700888
Allen Hubbee26a5842015-04-09 10:33:20 -0400889 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700890 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700891
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400892 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400893 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700894
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400895 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700896 val64 |= val;
897
Allen Hubbee26a5842015-04-09 10:33:20 -0400898 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700899
900 rc = ntb_set_mw(nt, i, val64);
901 if (rc)
902 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700903 }
904
Allen Hubbee26a5842015-04-09 10:33:20 -0400905 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700906
Allen Hubbee26a5842015-04-09 10:33:20 -0400907 for (i = 0; i < nt->qp_count; i++) {
908 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700909
910 ntb_transport_setup_qp_mw(nt, i);
911
Allen Hubbee26a5842015-04-09 10:33:20 -0400912 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700913 schedule_delayed_work(&qp->link_work, 0);
914 }
915
916 return;
917
Jon Mason113fc502013-01-30 11:40:52 -0700918out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400919 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700920 ntb_free_mw(nt, i);
Dave Jiang84f76682016-02-29 09:35:26 -0700921
922 /* if there's an actual failure, we should just bail */
923 if (rc < 0) {
924 ntb_link_disable(ndev);
925 return;
926 }
927
Jon Masonfce8a7b2012-11-16 19:27:12 -0700928out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400929 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700930 schedule_delayed_work(&nt->link_work,
931 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
932}
933
934static void ntb_qp_link_work(struct work_struct *work)
935{
936 struct ntb_transport_qp *qp = container_of(work,
937 struct ntb_transport_qp,
938 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400939 struct pci_dev *pdev = qp->ndev->pdev;
940 struct ntb_transport_ctx *nt = qp->transport;
941 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700942
Allen Hubbee26a5842015-04-09 10:33:20 -0400943 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700944
Allen Hubbee26a5842015-04-09 10:33:20 -0400945 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700946
Allen Hubbee26a5842015-04-09 10:33:20 -0400947 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700948
949 /* query remote spad for qp ready bits */
Allen Hubbe28762282015-05-11 10:08:26 -0400950 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700951
952 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400953 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700954 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400955 qp->link_is_up = true;
Dave Jiange9021332016-02-23 09:11:36 -0700956 qp->active = true;
Allen Hubbee26a5842015-04-09 10:33:20 -0400957
Jon Masonfce8a7b2012-11-16 19:27:12 -0700958 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400959 qp->event_handler(qp->cb_data, qp->link_is_up);
Allen Hubbe8b5a22d2015-07-13 08:07:12 -0400960
Dave Jiange9021332016-02-23 09:11:36 -0700961 if (qp->active)
962 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400963 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700964 schedule_delayed_work(&qp->link_work,
965 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
966}
967
Allen Hubbee26a5842015-04-09 10:33:20 -0400968static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700969 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700970{
971 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400972 phys_addr_t mw_base;
973 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700974 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400975 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700976 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700977
Allen Hubbee26a5842015-04-09 10:33:20 -0400978 mw_count = nt->mw_count;
979 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700980
Allen Hubbee26a5842015-04-09 10:33:20 -0400981 mw_num = QP_TO_MW(nt, qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400982
983 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700984 qp->qp_num = qp_num;
985 qp->transport = nt;
986 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400987 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700988 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400989 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700990
Logan Gunthorpe8e8496e2017-06-05 14:00:53 -0600991 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -0400992 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -0700993 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400994 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -0700995
Allen Hubbee26a5842015-04-09 10:33:20 -0400996 mw_base = nt->mw_vec[mw_num].phys_addr;
997 mw_size = nt->mw_vec[mw_num].phys_size;
998
999 tx_size = (unsigned int)mw_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -04001000 qp_offset = tx_size * (qp_num / mw_count);
Allen Hubbee26a5842015-04-09 10:33:20 -04001001
1002 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001003 if (!qp->tx_mw)
1004 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -07001005
Allen Hubbee26a5842015-04-09 10:33:20 -04001006 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001007 if (!qp->tx_mw_phys)
1008 return -EINVAL;
1009
1010 tx_size -= sizeof(struct ntb_rx_info);
1011 qp->rx_info = qp->tx_mw + tx_size;
1012
Jon Masonc9d534c2013-02-01 15:45:16 -07001013 /* Due to housekeeping, there must be atleast 2 buffs */
1014 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -07001015 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -07001016
Dave Jiangc8650fd2015-07-13 08:07:09 -04001017 if (nt->debugfs_node_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001018 char debugfs_name[4];
1019
1020 snprintf(debugfs_name, 4, "qp%d", qp_num);
1021 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Dave Jiangc8650fd2015-07-13 08:07:09 -04001022 nt->debugfs_node_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001023
1024 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1025 qp->debugfs_dir, qp,
1026 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -04001027 } else {
1028 qp->debugfs_dir = NULL;
1029 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001030 }
1031
1032 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001033 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001034
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001035 spin_lock_init(&qp->ntb_rx_q_lock);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001036 spin_lock_init(&qp->ntb_tx_free_q_lock);
1037
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001038 INIT_LIST_HEAD(&qp->rx_post_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001039 INIT_LIST_HEAD(&qp->rx_pend_q);
1040 INIT_LIST_HEAD(&qp->rx_free_q);
1041 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001042
Allen Hubbee26a5842015-04-09 10:33:20 -04001043 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1044 (unsigned long)qp);
1045
Jon Mason282a2fe2013-02-12 09:52:50 -07001046 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001047}
1048
Allen Hubbee26a5842015-04-09 10:33:20 -04001049static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001050{
Allen Hubbee26a5842015-04-09 10:33:20 -04001051 struct ntb_transport_ctx *nt;
1052 struct ntb_transport_mw *mw;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301053 unsigned int mw_count, qp_count, spad_count, max_mw_count_for_spads;
Allen Hubbee26a5842015-04-09 10:33:20 -04001054 u64 qp_bitmap;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001055 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001056 int rc, i;
1057
Logan Gunthorpe19645a02016-06-07 11:20:22 -06001058 mw_count = ntb_mw_count(ndev);
Logan Gunthorpe19645a02016-06-07 11:20:22 -06001059
Allen Hubbee26a5842015-04-09 10:33:20 -04001060 if (ntb_db_is_unsafe(ndev))
1061 dev_dbg(&ndev->dev,
1062 "doorbell is unsafe, proceed anyway...\n");
1063 if (ntb_spad_is_unsafe(ndev))
1064 dev_dbg(&ndev->dev,
1065 "scratchpad is unsafe, proceed anyway...\n");
1066
Allen Hubbe1199aa62015-05-18 06:20:47 -04001067 node = dev_to_node(&ndev->dev);
1068
1069 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001070 if (!nt)
1071 return -ENOMEM;
1072
Allen Hubbee26a5842015-04-09 10:33:20 -04001073 nt->ndev = ndev;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301074 spad_count = ntb_spad_count(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04001075
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301076 /* Limit the MW's based on the availability of scratchpads */
1077
1078 if (spad_count < NTB_TRANSPORT_MIN_SPADS) {
1079 nt->mw_count = 0;
1080 rc = -EINVAL;
1081 goto err;
1082 }
1083
1084 max_mw_count_for_spads = (spad_count - MW0_SZ_HIGH) / 2;
1085 nt->mw_count = min(mw_count, max_mw_count_for_spads);
Allen Hubbee26a5842015-04-09 10:33:20 -04001086
Allen Hubbe1199aa62015-05-18 06:20:47 -04001087 nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1088 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001089 if (!nt->mw_vec) {
1090 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001091 goto err;
1092 }
1093
Allen Hubbee26a5842015-04-09 10:33:20 -04001094 for (i = 0; i < mw_count; i++) {
1095 mw = &nt->mw_vec[i];
1096
1097 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
1098 &mw->xlat_align, &mw->xlat_align_size);
1099 if (rc)
1100 goto err1;
1101
Dave Jiang06917f72015-05-19 16:45:46 -04001102 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001103 if (!mw->vbase) {
1104 rc = -ENOMEM;
1105 goto err1;
1106 }
1107
1108 mw->buff_size = 0;
1109 mw->xlat_size = 0;
1110 mw->virt_addr = NULL;
1111 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -07001112 }
1113
Allen Hubbee26a5842015-04-09 10:33:20 -04001114 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001115
Allen Hubbee26a5842015-04-09 10:33:20 -04001116 qp_count = ilog2(qp_bitmap);
1117 if (max_num_clients && max_num_clients < qp_count)
1118 qp_count = max_num_clients;
Logan Gunthorpecb827ee2017-06-05 14:00:52 -06001119 else if (nt->mw_count < qp_count)
1120 qp_count = nt->mw_count;
Allen Hubbee26a5842015-04-09 10:33:20 -04001121
1122 qp_bitmap &= BIT_ULL(qp_count) - 1;
1123
1124 nt->qp_count = qp_count;
1125 nt->qp_bitmap = qp_bitmap;
1126 nt->qp_bitmap_free = qp_bitmap;
1127
Allen Hubbe1199aa62015-05-18 06:20:47 -04001128 nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1129 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001130 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001131 rc = -ENOMEM;
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301132 goto err1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001133 }
1134
Dave Jiangc8650fd2015-07-13 08:07:09 -04001135 if (nt_debugfs_dir) {
1136 nt->debugfs_node_dir =
1137 debugfs_create_dir(pci_name(ndev->pdev),
1138 nt_debugfs_dir);
1139 }
1140
Allen Hubbee26a5842015-04-09 10:33:20 -04001141 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001142 rc = ntb_transport_init_queue(nt, i);
1143 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301144 goto err2;
Jon Mason282a2fe2013-02-12 09:52:50 -07001145 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001146
1147 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001148 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001149
Allen Hubbee26a5842015-04-09 10:33:20 -04001150 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001151 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301152 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001153
1154 INIT_LIST_HEAD(&nt->client_devs);
1155 rc = ntb_bus_init(nt);
1156 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301157 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001158
Allen Hubbee26a5842015-04-09 10:33:20 -04001159 nt->link_is_up = false;
1160 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1161 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001162
1163 return 0;
1164
Jon Mason948d3a62013-04-18 17:07:36 -07001165err3:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301166 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001167err2:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301168 kfree(nt->qp_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001169err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001170 while (i--) {
1171 mw = &nt->mw_vec[i];
1172 iounmap(mw->vbase);
1173 }
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301174 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001175err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001176 kfree(nt);
1177 return rc;
1178}
1179
Allen Hubbee26a5842015-04-09 10:33:20 -04001180static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001181{
Allen Hubbee26a5842015-04-09 10:33:20 -04001182 struct ntb_transport_ctx *nt = ndev->ctx;
1183 struct ntb_transport_qp *qp;
1184 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001185 int i;
1186
Jon Masonfca4d512013-09-09 13:39:55 -07001187 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001188 cancel_work_sync(&nt->link_cleanup);
1189 cancel_delayed_work_sync(&nt->link_work);
1190
1191 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001192
1193 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001194 for (i = 0; i < nt->qp_count; i++) {
1195 qp = &nt->qp_vec[i];
1196 if (qp_bitmap_alloc & BIT_ULL(i))
1197 ntb_transport_free_queue(qp);
1198 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001199 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001200
Allen Hubbee26a5842015-04-09 10:33:20 -04001201 ntb_link_disable(ndev);
1202 ntb_clear_ctx(ndev);
1203
Jon Masonfce8a7b2012-11-16 19:27:12 -07001204 ntb_bus_remove(nt);
1205
Allen Hubbee26a5842015-04-09 10:33:20 -04001206 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001207 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001208 iounmap(nt->mw_vec[i].vbase);
1209 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001210
Allen Hubbee26a5842015-04-09 10:33:20 -04001211 kfree(nt->qp_vec);
1212 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001213 kfree(nt);
1214}
1215
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001216static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1217{
1218 struct ntb_queue_entry *entry;
1219 void *cb_data;
1220 unsigned int len;
1221 unsigned long irqflags;
1222
1223 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1224
1225 while (!list_empty(&qp->rx_post_q)) {
1226 entry = list_first_entry(&qp->rx_post_q,
1227 struct ntb_queue_entry, entry);
1228 if (!(entry->flags & DESC_DONE_FLAG))
1229 break;
1230
1231 entry->rx_hdr->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07001232 iowrite32(entry->rx_index, &qp->rx_info->entry);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001233
1234 cb_data = entry->cb_data;
1235 len = entry->len;
1236
1237 list_move_tail(&entry->entry, &qp->rx_free_q);
1238
1239 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1240
1241 if (qp->rx_handler && qp->client_ready)
1242 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1243
1244 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1245 }
1246
1247 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1248}
1249
Dave Jiang72203572016-07-20 13:14:13 -07001250static void ntb_rx_copy_callback(void *data,
1251 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001252{
Jon Mason282a2fe2013-02-12 09:52:50 -07001253 struct ntb_queue_entry *entry = data;
Jon Mason448c6fb2013-01-19 02:02:27 -07001254
Dave Jiang72203572016-07-20 13:14:13 -07001255 /* we need to check DMA results if we are using DMA */
1256 if (res) {
1257 enum dmaengine_tx_result dma_err = res->result;
1258
1259 switch (dma_err) {
1260 case DMA_TRANS_READ_FAILED:
1261 case DMA_TRANS_WRITE_FAILED:
1262 entry->errors++;
1263 case DMA_TRANS_ABORTED:
1264 {
1265 struct ntb_transport_qp *qp = entry->qp;
1266 void *offset = qp->rx_buff + qp->rx_max_frame *
1267 qp->rx_index;
1268
1269 ntb_memcpy_rx(entry, offset);
1270 qp->rx_memcpy++;
1271 return;
1272 }
1273
1274 case DMA_TRANS_NOERROR:
1275 default:
1276 break;
1277 }
1278 }
1279
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001280 entry->flags |= DESC_DONE_FLAG;
Jon Mason282a2fe2013-02-12 09:52:50 -07001281
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001282 ntb_complete_rxc(entry->qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001283}
1284
Jon Mason282a2fe2013-02-12 09:52:50 -07001285static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1286{
1287 void *buf = entry->buf;
1288 size_t len = entry->len;
1289
1290 memcpy(buf, offset, len);
1291
Allen Hubbee26a5842015-04-09 10:33:20 -04001292 /* Ensure that the data is fully copied out before clearing the flag */
1293 wmb();
1294
Dave Jiang72203572016-07-20 13:14:13 -07001295 ntb_rx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001296}
1297
Dave Jiang72203572016-07-20 13:14:13 -07001298static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
Jon Mason282a2fe2013-02-12 09:52:50 -07001299{
1300 struct dma_async_tx_descriptor *txd;
1301 struct ntb_transport_qp *qp = entry->qp;
Dave Jiang569410ca2015-07-13 08:07:22 -04001302 struct dma_chan *chan = qp->rx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001303 struct dma_device *device;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001304 size_t pay_off, buff_off, len;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001305 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001306 dma_cookie_t cookie;
1307 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001308
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001309 len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001310 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001311 pay_off = (size_t)offset & ~PAGE_MASK;
1312 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001313
1314 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Allen Hubbe905921e2015-07-13 08:07:21 -04001315 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001316
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001317 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1318 if (!unmap)
Allen Hubbe905921e2015-07-13 08:07:21 -04001319 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001320
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001321 unmap->len = len;
1322 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1323 pay_off, len, DMA_TO_DEVICE);
1324 if (dma_mapping_error(device->dev, unmap->addr[0]))
1325 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001326
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001327 unmap->to_cnt = 1;
1328
1329 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1330 buff_off, len, DMA_FROM_DEVICE);
1331 if (dma_mapping_error(device->dev, unmap->addr[1]))
1332 goto err_get_unmap;
1333
1334 unmap->from_cnt = 1;
1335
Allen Hubbe88931ec2017-06-09 18:06:36 -04001336 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
1337 unmap->addr[0], len,
1338 DMA_PREP_INTERRUPT);
1339 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001340 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001341
Dave Jiang72203572016-07-20 13:14:13 -07001342 txd->callback_result = ntb_rx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001343 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001344 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001345
1346 cookie = dmaengine_submit(txd);
1347 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001348 goto err_set_unmap;
1349
1350 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001351
1352 qp->last_cookie = cookie;
1353
1354 qp->rx_async++;
1355
Dave Jiang72203572016-07-20 13:14:13 -07001356 return 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001357
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001358err_set_unmap:
1359 dmaengine_unmap_put(unmap);
1360err_get_unmap:
1361 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001362err:
Dave Jiang72203572016-07-20 13:14:13 -07001363 return -ENXIO;
1364}
1365
1366static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
1367{
1368 struct ntb_transport_qp *qp = entry->qp;
1369 struct dma_chan *chan = qp->rx_dma_chan;
1370 int res;
1371
1372 if (!chan)
1373 goto err;
1374
1375 if (entry->len < copy_bytes)
1376 goto err;
1377
1378 res = ntb_async_rx_submit(entry, offset);
1379 if (res < 0)
1380 goto err;
1381
1382 if (!entry->retries)
1383 qp->rx_async++;
1384
1385 return;
1386
1387err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001388 ntb_memcpy_rx(entry, offset);
1389 qp->rx_memcpy++;
1390}
1391
Jon Masonfce8a7b2012-11-16 19:27:12 -07001392static int ntb_process_rxc(struct ntb_transport_qp *qp)
1393{
1394 struct ntb_payload_header *hdr;
1395 struct ntb_queue_entry *entry;
1396 void *offset;
1397
Jon Mason793c20e2013-01-19 02:02:26 -07001398 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1399 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1400
Allen Hubbee26a5842015-04-09 10:33:20 -04001401 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1402 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001403
Jon Masonfce8a7b2012-11-16 19:27:12 -07001404 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001405 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001406 qp->rx_ring_empty++;
1407 return -EAGAIN;
1408 }
1409
Allen Hubbee26a5842015-04-09 10:33:20 -04001410 if (hdr->flags & LINK_DOWN_FLAG) {
1411 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1412 ntb_qp_link_down(qp);
1413 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001414 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001415 }
1416
1417 if (hdr->ver != (u32)qp->rx_pkts) {
1418 dev_dbg(&qp->ndev->pdev->dev,
1419 "version mismatch, expected %llu - got %u\n",
1420 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001421 qp->rx_err_ver++;
1422 return -EIO;
1423 }
1424
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001425 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
Allen Hubbee26a5842015-04-09 10:33:20 -04001426 if (!entry) {
1427 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1428 qp->rx_err_no_buf++;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001429 return -EAGAIN;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001430 }
1431
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001432 entry->rx_hdr = hdr;
Dave Jiang72203572016-07-20 13:14:13 -07001433 entry->rx_index = qp->rx_index;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001434
Allen Hubbee26a5842015-04-09 10:33:20 -04001435 if (hdr->len > entry->len) {
1436 dev_dbg(&qp->ndev->pdev->dev,
1437 "receive buffer overflow! Wanted %d got %d\n",
1438 hdr->len, entry->len);
1439 qp->rx_err_oflow++;
1440
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001441 entry->len = -EIO;
1442 entry->flags |= DESC_DONE_FLAG;
1443
1444 ntb_complete_rxc(qp);
1445 } else {
1446 dev_dbg(&qp->ndev->pdev->dev,
1447 "RX OK index %u ver %u size %d into buf size %d\n",
1448 qp->rx_index, hdr->ver, hdr->len, entry->len);
1449
1450 qp->rx_bytes += hdr->len;
1451 qp->rx_pkts++;
1452
1453 entry->len = hdr->len;
1454
1455 ntb_async_rx(entry, offset);
Allen Hubbee26a5842015-04-09 10:33:20 -04001456 }
1457
Jon Mason282a2fe2013-02-12 09:52:50 -07001458 qp->rx_index++;
1459 qp->rx_index %= qp->rx_max_entry;
1460
1461 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001462}
1463
Allen Hubbee26a5842015-04-09 10:33:20 -04001464static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001465{
Allen Hubbee26a5842015-04-09 10:33:20 -04001466 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001467 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001468
Allen Hubbee26a5842015-04-09 10:33:20 -04001469 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1470 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001471
Jon Masonc336acd2013-01-17 15:28:45 -07001472 /* Limit the number of packets processed in a single interrupt to
1473 * provide fairness to others
1474 */
1475 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001476 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001477 if (rc)
1478 break;
1479 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001480
Dave Jiang569410ca2015-07-13 08:07:22 -04001481 if (i && qp->rx_dma_chan)
1482 dma_async_issue_pending(qp->rx_dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001483
Allen Hubbee26a5842015-04-09 10:33:20 -04001484 if (i == qp->rx_max_entry) {
1485 /* there is more work to do */
Dave Jiange9021332016-02-23 09:11:36 -07001486 if (qp->active)
1487 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001488 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1489 /* the doorbell bit is set: clear it */
1490 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1491 /* ntb_db_read ensures ntb_db_clear write is committed */
1492 ntb_db_read(qp->ndev);
1493
1494 /* an interrupt may have arrived between finishing
1495 * ntb_process_rxc and clearing the doorbell bit:
1496 * there might be some more work to do.
1497 */
Dave Jiange9021332016-02-23 09:11:36 -07001498 if (qp->active)
1499 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001500 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001501}
1502
Dave Jiang9cabc262016-07-20 13:14:07 -07001503static void ntb_tx_copy_callback(void *data,
1504 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001505{
Jon Mason282a2fe2013-02-12 09:52:50 -07001506 struct ntb_queue_entry *entry = data;
1507 struct ntb_transport_qp *qp = entry->qp;
1508 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001509
Dave Jiang9cabc262016-07-20 13:14:07 -07001510 /* we need to check DMA results if we are using DMA */
1511 if (res) {
1512 enum dmaengine_tx_result dma_err = res->result;
1513
1514 switch (dma_err) {
1515 case DMA_TRANS_READ_FAILED:
1516 case DMA_TRANS_WRITE_FAILED:
1517 entry->errors++;
1518 case DMA_TRANS_ABORTED:
1519 {
1520 void __iomem *offset =
1521 qp->tx_mw + qp->tx_max_frame *
1522 entry->tx_index;
1523
1524 /* resubmit via CPU */
1525 ntb_memcpy_tx(entry, offset);
1526 qp->tx_memcpy++;
1527 return;
1528 }
1529
1530 case DMA_TRANS_NOERROR:
1531 default:
1532 break;
1533 }
1534 }
1535
Jon Mason74465642013-01-21 15:28:52 -07001536 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001537
Allen Hubbee26a5842015-04-09 10:33:20 -04001538 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001539
1540 /* The entry length can only be zero if the packet is intended to be a
1541 * "link down" or similar. Since no payload is being sent in these
1542 * cases, there is nothing to add to the completion queue.
1543 */
1544 if (entry->len > 0) {
1545 qp->tx_bytes += entry->len;
1546
1547 if (qp->tx_handler)
1548 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1549 entry->len);
1550 }
1551
1552 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1553}
1554
Jon Mason282a2fe2013-02-12 09:52:50 -07001555static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1556{
Dave Jiang06917f72015-05-19 16:45:46 -04001557#ifdef ARCH_HAS_NOCACHE_UACCESS
1558 /*
1559 * Using non-temporal mov to improve performance on non-cached
1560 * writes, even though we aren't actually copying from user space.
1561 */
1562 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1563#else
Jon Mason282a2fe2013-02-12 09:52:50 -07001564 memcpy_toio(offset, entry->buf, entry->len);
Dave Jiang06917f72015-05-19 16:45:46 -04001565#endif
Jon Mason282a2fe2013-02-12 09:52:50 -07001566
Allen Hubbee26a5842015-04-09 10:33:20 -04001567 /* Ensure that the data is fully copied out before setting the flags */
1568 wmb();
1569
Dave Jiang9cabc262016-07-20 13:14:07 -07001570 ntb_tx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001571}
1572
Dave Jiang9cabc262016-07-20 13:14:07 -07001573static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1574 struct ntb_queue_entry *entry)
Jon Mason282a2fe2013-02-12 09:52:50 -07001575{
Jon Mason282a2fe2013-02-12 09:52:50 -07001576 struct dma_async_tx_descriptor *txd;
Dave Jiang569410ca2015-07-13 08:07:22 -04001577 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001578 struct dma_device *device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001579 size_t len = entry->len;
1580 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001581 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001582 struct dmaengine_unmap_data *unmap;
1583 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001584 dma_cookie_t cookie;
Jon Mason282a2fe2013-02-12 09:52:50 -07001585
Jon Mason282a2fe2013-02-12 09:52:50 -07001586 device = chan->device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001587 dest = qp->tx_mw_phys + qp->tx_max_frame * entry->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001588 buff_off = (size_t)buf & ~PAGE_MASK;
1589 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001590
1591 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1592 goto err;
1593
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001594 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1595 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001596 goto err;
1597
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001598 unmap->len = len;
1599 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1600 buff_off, len, DMA_TO_DEVICE);
1601 if (dma_mapping_error(device->dev, unmap->addr[0]))
1602 goto err_get_unmap;
1603
1604 unmap->to_cnt = 1;
1605
Allen Hubbe88931ec2017-06-09 18:06:36 -04001606 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
1607 DMA_PREP_INTERRUPT);
1608 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001609 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001610
Dave Jiang9cabc262016-07-20 13:14:07 -07001611 txd->callback_result = ntb_tx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001612 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001613 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001614
1615 cookie = dmaengine_submit(txd);
1616 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001617 goto err_set_unmap;
1618
1619 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001620
1621 dma_async_issue_pending(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001622
Dave Jiang9cabc262016-07-20 13:14:07 -07001623 return 0;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001624err_set_unmap:
1625 dmaengine_unmap_put(unmap);
1626err_get_unmap:
1627 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001628err:
Dave Jiang9cabc262016-07-20 13:14:07 -07001629 return -ENXIO;
1630}
1631
1632static void ntb_async_tx(struct ntb_transport_qp *qp,
1633 struct ntb_queue_entry *entry)
1634{
1635 struct ntb_payload_header __iomem *hdr;
1636 struct dma_chan *chan = qp->tx_dma_chan;
1637 void __iomem *offset;
1638 int res;
1639
1640 entry->tx_index = qp->tx_index;
1641 offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
1642 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1643 entry->tx_hdr = hdr;
1644
1645 iowrite32(entry->len, &hdr->len);
1646 iowrite32((u32)qp->tx_pkts, &hdr->ver);
1647
1648 if (!chan)
1649 goto err;
1650
1651 if (entry->len < copy_bytes)
1652 goto err;
1653
1654 res = ntb_async_tx_submit(qp, entry);
1655 if (res < 0)
1656 goto err;
1657
1658 if (!entry->retries)
1659 qp->tx_async++;
1660
1661 return;
1662
1663err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001664 ntb_memcpy_tx(entry, offset);
1665 qp->tx_memcpy++;
1666}
1667
Jon Masonfce8a7b2012-11-16 19:27:12 -07001668static int ntb_process_tx(struct ntb_transport_qp *qp,
1669 struct ntb_queue_entry *entry)
1670{
Jon Mason793c20e2013-01-19 02:02:26 -07001671 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001672 qp->tx_ring_full++;
1673 return -EAGAIN;
1674 }
1675
Jon Masonef114ed2013-01-19 02:02:18 -07001676 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001677 if (qp->tx_handler)
Jon Mason179f9122015-12-18 13:22:37 -05001678 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001679
1680 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1681 &qp->tx_free_q);
1682 return 0;
1683 }
1684
Jon Mason282a2fe2013-02-12 09:52:50 -07001685 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001686
Jon Mason793c20e2013-01-19 02:02:26 -07001687 qp->tx_index++;
1688 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001689
1690 qp->tx_pkts++;
1691
1692 return 0;
1693}
1694
1695static void ntb_send_link_down(struct ntb_transport_qp *qp)
1696{
Allen Hubbee26a5842015-04-09 10:33:20 -04001697 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001698 struct ntb_queue_entry *entry;
1699 int i, rc;
1700
Allen Hubbee26a5842015-04-09 10:33:20 -04001701 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001702 return;
1703
Allen Hubbee22e0b92015-05-12 06:55:44 -04001704 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001705
1706 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001707 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001708 if (entry)
1709 break;
1710 msleep(100);
1711 }
1712
1713 if (!entry)
1714 return;
1715
1716 entry->cb_data = NULL;
1717 entry->buf = NULL;
1718 entry->len = 0;
1719 entry->flags = LINK_DOWN_FLAG;
1720
1721 rc = ntb_process_tx(qp, entry);
1722 if (rc)
1723 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1724 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001725
1726 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001727}
1728
Allen Hubbe1199aa62015-05-18 06:20:47 -04001729static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1730{
1731 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1732}
1733
Jon Masonfce8a7b2012-11-16 19:27:12 -07001734/**
1735 * ntb_transport_create_queue - Create a new NTB transport layer queue
1736 * @rx_handler: receive callback function
1737 * @tx_handler: transmit callback function
1738 * @event_handler: event callback function
1739 *
1740 * Create a new NTB transport layer queue and provide the queue with a callback
1741 * routine for both transmit and receive. The receive callback routine will be
1742 * used to pass up data when the transport has received it on the queue. The
1743 * transmit callback routine will be called when the transport has completed the
1744 * transmission of the data on the queue and the data is ready to be freed.
1745 *
1746 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1747 */
1748struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001749ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001750 const struct ntb_queue_handlers *handlers)
1751{
Allen Hubbee26a5842015-04-09 10:33:20 -04001752 struct ntb_dev *ndev;
1753 struct pci_dev *pdev;
1754 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001755 struct ntb_queue_entry *entry;
1756 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001757 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001758 unsigned int free_queue;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001759 dma_cap_mask_t dma_mask;
1760 int node;
Allen Hubbee26a5842015-04-09 10:33:20 -04001761 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001762
Allen Hubbee26a5842015-04-09 10:33:20 -04001763 ndev = dev_ntb(client_dev->parent);
1764 pdev = ndev->pdev;
1765 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001766
Allen Hubbe1199aa62015-05-18 06:20:47 -04001767 node = dev_to_node(&ndev->dev);
1768
Thomas VanSelus8fcd0952017-02-13 16:46:26 -06001769 free_queue = ffs(nt->qp_bitmap_free);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001770 if (!free_queue)
1771 goto err;
1772
1773 /* decrement free_queue to make it zero based */
1774 free_queue--;
1775
Allen Hubbee26a5842015-04-09 10:33:20 -04001776 qp = &nt->qp_vec[free_queue];
1777 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001778
Allen Hubbee26a5842015-04-09 10:33:20 -04001779 nt->qp_bitmap_free &= ~qp_bit;
1780
Jon Masonfce8a7b2012-11-16 19:27:12 -07001781 qp->cb_data = data;
1782 qp->rx_handler = handlers->rx_handler;
1783 qp->tx_handler = handlers->tx_handler;
1784 qp->event_handler = handlers->event_handler;
1785
Allen Hubbe1199aa62015-05-18 06:20:47 -04001786 dma_cap_zero(dma_mask);
1787 dma_cap_set(DMA_MEMCPY, dma_mask);
1788
Dave Jianga41ef052015-05-19 16:52:04 -04001789 if (use_dma) {
Dave Jiang569410ca2015-07-13 08:07:22 -04001790 qp->tx_dma_chan =
1791 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1792 (void *)(unsigned long)node);
1793 if (!qp->tx_dma_chan)
1794 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
1795
1796 qp->rx_dma_chan =
1797 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1798 (void *)(unsigned long)node);
1799 if (!qp->rx_dma_chan)
1800 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
Dave Jianga41ef052015-05-19 16:52:04 -04001801 } else {
Dave Jiang569410ca2015-07-13 08:07:22 -04001802 qp->tx_dma_chan = NULL;
1803 qp->rx_dma_chan = NULL;
Dave Jianga41ef052015-05-19 16:52:04 -04001804 }
Dave Jiang569410ca2015-07-13 08:07:22 -04001805
1806 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
1807 qp->tx_dma_chan ? "DMA" : "CPU");
1808
1809 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
1810 qp->rx_dma_chan ? "DMA" : "CPU");
Jon Mason282a2fe2013-02-12 09:52:50 -07001811
Jon Masonfce8a7b2012-11-16 19:27:12 -07001812 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001813 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001814 if (!entry)
1815 goto err1;
1816
Jon Mason282a2fe2013-02-12 09:52:50 -07001817 entry->qp = qp;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001818 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001819 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001820 }
Dave Jianga754a8f2016-04-08 10:49:06 -07001821 qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001822
Dave Jianga754a8f2016-04-08 10:49:06 -07001823 for (i = 0; i < qp->tx_max_entry; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001824 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001825 if (!entry)
1826 goto err2;
1827
Jon Mason282a2fe2013-02-12 09:52:50 -07001828 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001829 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001830 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001831 }
1832
Allen Hubbee26a5842015-04-09 10:33:20 -04001833 ntb_db_clear(qp->ndev, qp_bit);
1834 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001835
1836 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1837
1838 return qp;
1839
Jon Masonfce8a7b2012-11-16 19:27:12 -07001840err2:
Jon Masonf7667552013-01-19 02:02:24 -07001841 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001842 kfree(entry);
1843err1:
Dave Jianga754a8f2016-04-08 10:49:06 -07001844 qp->rx_alloc_entry = 0;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001845 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001846 kfree(entry);
Dave Jiang569410ca2015-07-13 08:07:22 -04001847 if (qp->tx_dma_chan)
1848 dma_release_channel(qp->tx_dma_chan);
1849 if (qp->rx_dma_chan)
1850 dma_release_channel(qp->rx_dma_chan);
Allen Hubbee26a5842015-04-09 10:33:20 -04001851 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001852err:
1853 return NULL;
1854}
1855EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1856
1857/**
1858 * ntb_transport_free_queue - Frees NTB transport queue
1859 * @qp: NTB queue to be freed
1860 *
1861 * Frees NTB transport queue
1862 */
1863void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1864{
Jon Mason186f27f2013-01-22 11:35:40 -07001865 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001866 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001867 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001868
1869 if (!qp)
1870 return;
1871
Allen Hubbee26a5842015-04-09 10:33:20 -04001872 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001873
Dave Jiange9021332016-02-23 09:11:36 -07001874 qp->active = false;
1875
Dave Jiang569410ca2015-07-13 08:07:22 -04001876 if (qp->tx_dma_chan) {
1877 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001878 /* Putting the dma_chan to NULL will force any new traffic to be
1879 * processed by the CPU instead of the DAM engine
1880 */
Dave Jiang569410ca2015-07-13 08:07:22 -04001881 qp->tx_dma_chan = NULL;
1882
1883 /* Try to be nice and wait for any queued DMA engine
1884 * transactions to process before smashing it with a rock
1885 */
1886 dma_sync_wait(chan, qp->last_cookie);
1887 dmaengine_terminate_all(chan);
1888 dma_release_channel(chan);
1889 }
1890
1891 if (qp->rx_dma_chan) {
1892 struct dma_chan *chan = qp->rx_dma_chan;
1893 /* Putting the dma_chan to NULL will force any new traffic to be
1894 * processed by the CPU instead of the DAM engine
1895 */
1896 qp->rx_dma_chan = NULL;
Jon Mason282a2fe2013-02-12 09:52:50 -07001897
1898 /* Try to be nice and wait for any queued DMA engine
1899 * transactions to process before smashing it with a rock
1900 */
1901 dma_sync_wait(chan, qp->last_cookie);
1902 dmaengine_terminate_all(chan);
Allen Hubbe1199aa62015-05-18 06:20:47 -04001903 dma_release_channel(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001904 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001905
Allen Hubbee26a5842015-04-09 10:33:20 -04001906 qp_bit = BIT_ULL(qp->qp_num);
1907
1908 ntb_db_set_mask(qp->ndev, qp_bit);
Dave Jiange9021332016-02-23 09:11:36 -07001909 tasklet_kill(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001910
Jon Mason282a2fe2013-02-12 09:52:50 -07001911 cancel_delayed_work_sync(&qp->link_work);
1912
Allen Hubbee26a5842015-04-09 10:33:20 -04001913 qp->cb_data = NULL;
1914 qp->rx_handler = NULL;
1915 qp->tx_handler = NULL;
1916 qp->event_handler = NULL;
1917
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001918 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001919 kfree(entry);
1920
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001921 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1922 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1923 kfree(entry);
1924 }
1925
1926 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1927 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001928 kfree(entry);
1929 }
1930
Jon Masonf7667552013-01-19 02:02:24 -07001931 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001932 kfree(entry);
1933
Allen Hubbe30a4bb12015-07-13 08:07:14 -04001934 qp->transport->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001935
1936 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1937}
1938EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1939
1940/**
1941 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1942 * @qp: NTB queue to be freed
1943 * @len: pointer to variable to write enqueued buffers length
1944 *
1945 * Dequeues unused buffers from receive queue. Should only be used during
1946 * shutdown of qp.
1947 *
1948 * RETURNS: NULL error value on error, or void* for success.
1949 */
1950void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1951{
1952 struct ntb_queue_entry *entry;
1953 void *buf;
1954
Allen Hubbee26a5842015-04-09 10:33:20 -04001955 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001956 return NULL;
1957
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001958 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001959 if (!entry)
1960 return NULL;
1961
1962 buf = entry->cb_data;
1963 *len = entry->len;
1964
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001965 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001966
1967 return buf;
1968}
1969EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1970
1971/**
1972 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1973 * @qp: NTB transport layer queue the entry is to be enqueued on
1974 * @cb: per buffer pointer for callback function to use
1975 * @data: pointer to data buffer that incoming packets will be copied into
1976 * @len: length of the data buffer
1977 *
1978 * Enqueue a new receive buffer onto the transport queue into which a NTB
1979 * payload can be received into.
1980 *
1981 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1982 */
1983int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1984 unsigned int len)
1985{
1986 struct ntb_queue_entry *entry;
1987
1988 if (!qp)
1989 return -EINVAL;
1990
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001991 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001992 if (!entry)
1993 return -ENOMEM;
1994
1995 entry->cb_data = cb;
1996 entry->buf = data;
1997 entry->len = len;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001998 entry->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07001999 entry->retries = 0;
2000 entry->errors = 0;
2001 entry->rx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002002
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002003 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
2004
Dave Jiange9021332016-02-23 09:11:36 -07002005 if (qp->active)
2006 tasklet_schedule(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002007
2008 return 0;
2009}
2010EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2011
2012/**
2013 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2014 * @qp: NTB transport layer queue the entry is to be enqueued on
2015 * @cb: per buffer pointer for callback function to use
2016 * @data: pointer to data buffer that will be sent
2017 * @len: length of the data buffer
2018 *
2019 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07002020 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07002021 * serialize access to the qp.
2022 *
2023 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2024 */
2025int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2026 unsigned int len)
2027{
2028 struct ntb_queue_entry *entry;
2029 int rc;
2030
Allen Hubbee26a5842015-04-09 10:33:20 -04002031 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002032 return -EINVAL;
2033
2034 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07002035 if (!entry) {
2036 qp->tx_err_no_buf++;
Dave Jiange74bfee2015-07-13 08:07:17 -04002037 return -EBUSY;
Jon Mason282a2fe2013-02-12 09:52:50 -07002038 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07002039
2040 entry->cb_data = cb;
2041 entry->buf = data;
2042 entry->len = len;
2043 entry->flags = 0;
Dave Jiang9cabc262016-07-20 13:14:07 -07002044 entry->errors = 0;
2045 entry->retries = 0;
2046 entry->tx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002047
2048 rc = ntb_process_tx(qp, entry);
2049 if (rc)
2050 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2051 &qp->tx_free_q);
2052
2053 return rc;
2054}
2055EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2056
2057/**
2058 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2059 * @qp: NTB transport layer queue to be enabled
2060 *
2061 * Notify NTB transport layer of client readiness to use queue
2062 */
2063void ntb_transport_link_up(struct ntb_transport_qp *qp)
2064{
2065 if (!qp)
2066 return;
2067
Allen Hubbee26a5842015-04-09 10:33:20 -04002068 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002069
Allen Hubbee26a5842015-04-09 10:33:20 -04002070 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002071 schedule_delayed_work(&qp->link_work, 0);
2072}
2073EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2074
2075/**
2076 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2077 * @qp: NTB transport layer queue to be disabled
2078 *
2079 * Notify NTB transport layer of client's desire to no longer receive data on
2080 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07002081 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07002082 */
2083void ntb_transport_link_down(struct ntb_transport_qp *qp)
2084{
Allen Hubbee26a5842015-04-09 10:33:20 -04002085 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002086
2087 if (!qp)
2088 return;
2089
Allen Hubbee26a5842015-04-09 10:33:20 -04002090 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002091
Allen Hubbee26a5842015-04-09 10:33:20 -04002092 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002093
Allen Hubbee26a5842015-04-09 10:33:20 -04002094 ntb_peer_spad_write(qp->ndev, QP_LINKS,
2095 val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07002096
Allen Hubbee26a5842015-04-09 10:33:20 -04002097 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002098 ntb_send_link_down(qp);
2099 else
2100 cancel_delayed_work_sync(&qp->link_work);
2101}
2102EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2103
2104/**
2105 * ntb_transport_link_query - Query transport link state
2106 * @qp: NTB transport layer queue to be queried
2107 *
2108 * Query connectivity to the remote system of the NTB transport queue
2109 *
2110 * RETURNS: true for link up or false for link down
2111 */
2112bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2113{
Jon Mason186f27f2013-01-22 11:35:40 -07002114 if (!qp)
2115 return false;
2116
Allen Hubbee26a5842015-04-09 10:33:20 -04002117 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002118}
2119EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2120
2121/**
2122 * ntb_transport_qp_num - Query the qp number
2123 * @qp: NTB transport layer queue to be queried
2124 *
2125 * Query qp number of the NTB transport queue
2126 *
2127 * RETURNS: a zero based number specifying the qp number
2128 */
2129unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2130{
Jon Mason186f27f2013-01-22 11:35:40 -07002131 if (!qp)
2132 return 0;
2133
Jon Masonfce8a7b2012-11-16 19:27:12 -07002134 return qp->qp_num;
2135}
2136EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2137
2138/**
2139 * ntb_transport_max_size - Query the max payload size of a qp
2140 * @qp: NTB transport layer queue to be queried
2141 *
2142 * Query the maximum payload size permissible on the given qp
2143 *
2144 * RETURNS: the max payload size of a qp
2145 */
Jon Masonef114ed2013-01-19 02:02:18 -07002146unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002147{
Dave Jiang04afde42015-09-17 13:27:04 -07002148 unsigned int max_size;
Dave Jiang569410ca2015-07-13 08:07:22 -04002149 unsigned int copy_align;
Dave Jiang04afde42015-09-17 13:27:04 -07002150 struct dma_chan *rx_chan, *tx_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002151
Jon Mason186f27f2013-01-22 11:35:40 -07002152 if (!qp)
2153 return 0;
2154
Dave Jiang04afde42015-09-17 13:27:04 -07002155 rx_chan = qp->rx_dma_chan;
2156 tx_chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002157
Dave Jiang04afde42015-09-17 13:27:04 -07002158 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2159 tx_chan ? tx_chan->device->copy_align : 0);
Dave Jiang569410ca2015-07-13 08:07:22 -04002160
Jon Mason282a2fe2013-02-12 09:52:50 -07002161 /* If DMA engine usage is possible, try to find the max size for that */
Dave Jiang04afde42015-09-17 13:27:04 -07002162 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2163 max_size = round_down(max_size, 1 << copy_align);
Jon Mason282a2fe2013-02-12 09:52:50 -07002164
Dave Jiang04afde42015-09-17 13:27:04 -07002165 return max_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002166}
2167EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04002168
Dave Jiange74bfee2015-07-13 08:07:17 -04002169unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2170{
2171 unsigned int head = qp->tx_index;
2172 unsigned int tail = qp->remote_rx_info->entry;
2173
2174 return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2175}
2176EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2177
Allen Hubbee26a5842015-04-09 10:33:20 -04002178static void ntb_transport_doorbell_callback(void *data, int vector)
2179{
2180 struct ntb_transport_ctx *nt = data;
2181 struct ntb_transport_qp *qp;
2182 u64 db_bits;
2183 unsigned int qp_num;
2184
2185 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2186 ntb_db_vector_mask(nt->ndev, vector));
2187
2188 while (db_bits) {
2189 qp_num = __ffs(db_bits);
2190 qp = &nt->qp_vec[qp_num];
2191
Dave Jiange9021332016-02-23 09:11:36 -07002192 if (qp->active)
2193 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04002194
2195 db_bits &= ~BIT_ULL(qp_num);
2196 }
2197}
2198
2199static const struct ntb_ctx_ops ntb_transport_ops = {
2200 .link_event = ntb_transport_event_callback,
2201 .db_event = ntb_transport_doorbell_callback,
2202};
2203
2204static struct ntb_client ntb_transport_client = {
2205 .ops = {
2206 .probe = ntb_transport_probe,
2207 .remove = ntb_transport_free,
2208 },
2209};
2210
2211static int __init ntb_transport_init(void)
2212{
2213 int rc;
2214
Dave Jiang7eb38782015-06-15 08:21:33 -04002215 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2216
Allen Hubbee26a5842015-04-09 10:33:20 -04002217 if (debugfs_initialized())
2218 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2219
2220 rc = bus_register(&ntb_transport_bus);
2221 if (rc)
2222 goto err_bus;
2223
2224 rc = ntb_register_client(&ntb_transport_client);
2225 if (rc)
2226 goto err_client;
2227
2228 return 0;
2229
2230err_client:
2231 bus_unregister(&ntb_transport_bus);
2232err_bus:
2233 debugfs_remove_recursive(nt_debugfs_dir);
2234 return rc;
2235}
2236module_init(ntb_transport_init);
2237
2238static void __exit ntb_transport_exit(void)
2239{
Allen Hubbee26a5842015-04-09 10:33:20 -04002240 ntb_unregister_client(&ntb_transport_client);
2241 bus_unregister(&ntb_transport_bus);
Allen Hubbedd622452016-12-27 17:57:04 -05002242 debugfs_remove_recursive(nt_debugfs_dir);
Allen Hubbee26a5842015-04-09 10:33:20 -04002243}
2244module_exit(ntb_transport_exit);