1fce8a7bbSJon Mason /*
2fce8a7bbSJon Mason * This file is provided under a dual BSD/GPLv2 license. When using or
3fce8a7bbSJon Mason * redistributing this file, you may do so under either license.
4fce8a7bbSJon Mason *
5fce8a7bbSJon Mason * GPL LICENSE SUMMARY
6fce8a7bbSJon Mason *
7fce8a7bbSJon Mason * Copyright(c) 2012 Intel Corporation. All rights reserved.
8e26a5843SAllen Hubbe * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
9fce8a7bbSJon Mason *
10fce8a7bbSJon Mason * This program is free software; you can redistribute it and/or modify
11fce8a7bbSJon Mason * it under the terms of version 2 of the GNU General Public License as
12fce8a7bbSJon Mason * published by the Free Software Foundation.
13fce8a7bbSJon Mason *
14fce8a7bbSJon Mason * BSD LICENSE
15fce8a7bbSJon Mason *
16fce8a7bbSJon Mason * Copyright(c) 2012 Intel Corporation. All rights reserved.
17e26a5843SAllen Hubbe * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
18fce8a7bbSJon Mason *
19fce8a7bbSJon Mason * Redistribution and use in source and binary forms, with or without
20fce8a7bbSJon Mason * modification, are permitted provided that the following conditions
21fce8a7bbSJon Mason * are met:
22fce8a7bbSJon Mason *
23fce8a7bbSJon Mason * * Redistributions of source code must retain the above copyright
24fce8a7bbSJon Mason * notice, this list of conditions and the following disclaimer.
25fce8a7bbSJon Mason * * Redistributions in binary form must reproduce the above copy
26fce8a7bbSJon Mason * notice, this list of conditions and the following disclaimer in
27fce8a7bbSJon Mason * the documentation and/or other materials provided with the
28fce8a7bbSJon Mason * distribution.
29fce8a7bbSJon Mason * * Neither the name of Intel Corporation nor the names of its
30fce8a7bbSJon Mason * contributors may be used to endorse or promote products derived
31fce8a7bbSJon Mason * from this software without specific prior written permission.
32fce8a7bbSJon Mason *
33fce8a7bbSJon Mason * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34fce8a7bbSJon Mason * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35fce8a7bbSJon Mason * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36fce8a7bbSJon Mason * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37fce8a7bbSJon Mason * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38fce8a7bbSJon Mason * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39fce8a7bbSJon Mason * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40fce8a7bbSJon Mason * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41fce8a7bbSJon Mason * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42fce8a7bbSJon Mason * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43fce8a7bbSJon Mason * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44fce8a7bbSJon Mason *
45e26a5843SAllen Hubbe * PCIe NTB Transport Linux driver
46fce8a7bbSJon Mason *
47fce8a7bbSJon Mason * Contact Information:
48fce8a7bbSJon Mason * Jon Mason <jon.mason@intel.com>
49fce8a7bbSJon Mason */
50fce8a7bbSJon Mason #include <linux/debugfs.h>
51fce8a7bbSJon Mason #include <linux/delay.h>
52282a2feeSJon Mason #include <linux/dmaengine.h>
53fce8a7bbSJon Mason #include <linux/dma-mapping.h>
54fce8a7bbSJon Mason #include <linux/errno.h>
55fce8a7bbSJon Mason #include <linux/export.h>
56fce8a7bbSJon Mason #include <linux/interrupt.h>
57fce8a7bbSJon Mason #include <linux/module.h>
58fce8a7bbSJon Mason #include <linux/pci.h>
59fce8a7bbSJon Mason #include <linux/slab.h>
60fce8a7bbSJon Mason #include <linux/types.h>
6106917f75SDave Jiang #include <linux/uaccess.h>
62e26a5843SAllen Hubbe #include "linux/ntb.h"
63e26a5843SAllen Hubbe #include "linux/ntb_transport.h"
64fce8a7bbSJon Mason
65e26a5843SAllen Hubbe #define NTB_TRANSPORT_VERSION 4
66e26a5843SAllen Hubbe #define NTB_TRANSPORT_VER "4"
67e26a5843SAllen Hubbe #define NTB_TRANSPORT_NAME "ntb_transport"
68e26a5843SAllen Hubbe #define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
69b17faba0SShyam Sundar S K #define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
70e26a5843SAllen Hubbe
71e26a5843SAllen Hubbe MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
72e26a5843SAllen Hubbe MODULE_VERSION(NTB_TRANSPORT_VER);
73e26a5843SAllen Hubbe MODULE_LICENSE("Dual BSD/GPL");
74e26a5843SAllen Hubbe MODULE_AUTHOR("Intel Corporation");
75e26a5843SAllen Hubbe
76e26a5843SAllen Hubbe static unsigned long max_mw_size;
77e26a5843SAllen Hubbe module_param(max_mw_size, ulong, 0644);
78e26a5843SAllen Hubbe MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
79fce8a7bbSJon Mason
809891417dSDave Jiang static unsigned int transport_mtu = 0x10000;
81fce8a7bbSJon Mason module_param(transport_mtu, uint, 0644);
82fce8a7bbSJon Mason MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
83fce8a7bbSJon Mason
84948d3a65SJon Mason static unsigned char max_num_clients;
85fce8a7bbSJon Mason module_param(max_num_clients, byte, 0644);
86fce8a7bbSJon Mason MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
87fce8a7bbSJon Mason
88282a2feeSJon Mason static unsigned int copy_bytes = 1024;
89282a2feeSJon Mason module_param(copy_bytes, uint, 0644);
90282a2feeSJon Mason MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
91282a2feeSJon Mason
92a41ef053SDave Jiang static bool use_dma;
93a41ef053SDave Jiang module_param(use_dma, bool, 0644);
94a41ef053SDave Jiang MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
95a41ef053SDave Jiang
962b0569b3SLogan Gunthorpe static bool use_msi;
972b0569b3SLogan Gunthorpe #ifdef CONFIG_NTB_MSI
982b0569b3SLogan Gunthorpe module_param(use_msi, bool, 0644);
992b0569b3SLogan Gunthorpe MODULE_PARM_DESC(use_msi, "Use MSI interrupts instead of doorbells");
1002b0569b3SLogan Gunthorpe #endif
1012b0569b3SLogan Gunthorpe
102e26a5843SAllen Hubbe static struct dentry *nt_debugfs_dir;
103e26a5843SAllen Hubbe
1041e530119SSerge Semin /* Only two-ports NTB devices are supported */
1051e530119SSerge Semin #define PIDX NTB_DEF_PEER_IDX
1061e530119SSerge Semin
107fce8a7bbSJon Mason struct ntb_queue_entry {
108fce8a7bbSJon Mason /* ntb_queue list reference */
109fce8a7bbSJon Mason struct list_head entry;
110e26a5843SAllen Hubbe /* pointers to data to be transferred */
111fce8a7bbSJon Mason void *cb_data;
112fce8a7bbSJon Mason void *buf;
113fce8a7bbSJon Mason unsigned int len;
114fce8a7bbSJon Mason unsigned int flags;
1159cabc269SDave Jiang int retries;
1169cabc269SDave Jiang int errors;
1179cabc269SDave Jiang unsigned int tx_index;
11872203572SDave Jiang unsigned int rx_index;
119282a2feeSJon Mason
120282a2feeSJon Mason struct ntb_transport_qp *qp;
121282a2feeSJon Mason union {
122282a2feeSJon Mason struct ntb_payload_header __iomem *tx_hdr;
123282a2feeSJon Mason struct ntb_payload_header *rx_hdr;
124282a2feeSJon Mason };
125fce8a7bbSJon Mason };
126fce8a7bbSJon Mason
127793c20e9SJon Mason struct ntb_rx_info {
128793c20e9SJon Mason unsigned int entry;
129793c20e9SJon Mason };
130793c20e9SJon Mason
131fce8a7bbSJon Mason struct ntb_transport_qp {
132e26a5843SAllen Hubbe struct ntb_transport_ctx *transport;
133e26a5843SAllen Hubbe struct ntb_dev *ndev;
134fce8a7bbSJon Mason void *cb_data;
135569410caSDave Jiang struct dma_chan *tx_dma_chan;
136569410caSDave Jiang struct dma_chan *rx_dma_chan;
137fce8a7bbSJon Mason
138fce8a7bbSJon Mason bool client_ready;
139e26a5843SAllen Hubbe bool link_is_up;
140e9021331SDave Jiang bool active;
141e26a5843SAllen Hubbe
142fce8a7bbSJon Mason u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
143e26a5843SAllen Hubbe u64 qp_bit;
144fce8a7bbSJon Mason
14574465645SJon Mason struct ntb_rx_info __iomem *rx_info;
146793c20e9SJon Mason struct ntb_rx_info *remote_rx_info;
147793c20e9SJon Mason
148fce8a7bbSJon Mason void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
149fce8a7bbSJon Mason void *data, int len);
150fce8a7bbSJon Mason struct list_head tx_free_q;
151fce8a7bbSJon Mason spinlock_t ntb_tx_free_q_lock;
15274465645SJon Mason void __iomem *tx_mw;
153c59666bbSLogan Gunthorpe phys_addr_t tx_mw_phys;
154c59666bbSLogan Gunthorpe size_t tx_mw_size;
155c59666bbSLogan Gunthorpe dma_addr_t tx_mw_dma_addr;
156793c20e9SJon Mason unsigned int tx_index;
157793c20e9SJon Mason unsigned int tx_max_entry;
158ef114ed5SJon Mason unsigned int tx_max_frame;
159fce8a7bbSJon Mason
160fce8a7bbSJon Mason void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
161fce8a7bbSJon Mason void *data, int len);
162da2e5ae5SAllen Hubbe struct list_head rx_post_q;
163fce8a7bbSJon Mason struct list_head rx_pend_q;
164fce8a7bbSJon Mason struct list_head rx_free_q;
165da2e5ae5SAllen Hubbe /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
166da2e5ae5SAllen Hubbe spinlock_t ntb_rx_q_lock;
167793c20e9SJon Mason void *rx_buff;
168793c20e9SJon Mason unsigned int rx_index;
169793c20e9SJon Mason unsigned int rx_max_entry;
170ef114ed5SJon Mason unsigned int rx_max_frame;
171a754a8fcSDave Jiang unsigned int rx_alloc_entry;
172282a2feeSJon Mason dma_cookie_t last_cookie;
173e26a5843SAllen Hubbe struct tasklet_struct rxc_db_work;
174fce8a7bbSJon Mason
175fce8a7bbSJon Mason void (*event_handler)(void *data, int status);
176fce8a7bbSJon Mason struct delayed_work link_work;
1777b4f2d3cSJon Mason struct work_struct link_cleanup;
178fce8a7bbSJon Mason
179fce8a7bbSJon Mason struct dentry *debugfs_dir;
180fce8a7bbSJon Mason struct dentry *debugfs_stats;
181fce8a7bbSJon Mason
182fce8a7bbSJon Mason /* Stats */
183fce8a7bbSJon Mason u64 rx_bytes;
184fce8a7bbSJon Mason u64 rx_pkts;
185fce8a7bbSJon Mason u64 rx_ring_empty;
186fce8a7bbSJon Mason u64 rx_err_no_buf;
187fce8a7bbSJon Mason u64 rx_err_oflow;
188fce8a7bbSJon Mason u64 rx_err_ver;
189282a2feeSJon Mason u64 rx_memcpy;
190282a2feeSJon Mason u64 rx_async;
191fce8a7bbSJon Mason u64 tx_bytes;
192fce8a7bbSJon Mason u64 tx_pkts;
193fce8a7bbSJon Mason u64 tx_ring_full;
194282a2feeSJon Mason u64 tx_err_no_buf;
195282a2feeSJon Mason u64 tx_memcpy;
196282a2feeSJon Mason u64 tx_async;
1972b0569b3SLogan Gunthorpe
1982b0569b3SLogan Gunthorpe bool use_msi;
1992b0569b3SLogan Gunthorpe int msi_irq;
2002b0569b3SLogan Gunthorpe struct ntb_msi_desc msi_desc;
2012b0569b3SLogan Gunthorpe struct ntb_msi_desc peer_msi_desc;
202fce8a7bbSJon Mason };
203fce8a7bbSJon Mason
204fce8a7bbSJon Mason struct ntb_transport_mw {
205e26a5843SAllen Hubbe phys_addr_t phys_addr;
206e26a5843SAllen Hubbe resource_size_t phys_size;
207e26a5843SAllen Hubbe void __iomem *vbase;
208e26a5843SAllen Hubbe size_t xlat_size;
209e26a5843SAllen Hubbe size_t buff_size;
210fc5d1829SAaron Sierra size_t alloc_size;
211fc5d1829SAaron Sierra void *alloc_addr;
212fce8a7bbSJon Mason void *virt_addr;
213fce8a7bbSJon Mason dma_addr_t dma_addr;
214fce8a7bbSJon Mason };
215fce8a7bbSJon Mason
216fce8a7bbSJon Mason struct ntb_transport_client_dev {
217fce8a7bbSJon Mason struct list_head entry;
218e26a5843SAllen Hubbe struct ntb_transport_ctx *nt;
219fce8a7bbSJon Mason struct device dev;
220fce8a7bbSJon Mason };
221fce8a7bbSJon Mason
222e26a5843SAllen Hubbe struct ntb_transport_ctx {
223fce8a7bbSJon Mason struct list_head entry;
224fce8a7bbSJon Mason struct list_head client_devs;
225fce8a7bbSJon Mason
226e26a5843SAllen Hubbe struct ntb_dev *ndev;
227e26a5843SAllen Hubbe
228e26a5843SAllen Hubbe struct ntb_transport_mw *mw_vec;
229e26a5843SAllen Hubbe struct ntb_transport_qp *qp_vec;
230e26a5843SAllen Hubbe unsigned int mw_count;
231e26a5843SAllen Hubbe unsigned int qp_count;
232e26a5843SAllen Hubbe u64 qp_bitmap;
233e26a5843SAllen Hubbe u64 qp_bitmap_free;
234e26a5843SAllen Hubbe
2352b0569b3SLogan Gunthorpe bool use_msi;
2362b0569b3SLogan Gunthorpe unsigned int msi_spad_offset;
2372b0569b3SLogan Gunthorpe u64 msi_db_mask;
2382b0569b3SLogan Gunthorpe
239e26a5843SAllen Hubbe bool link_is_up;
240fce8a7bbSJon Mason struct delayed_work link_work;
2417b4f2d3cSJon Mason struct work_struct link_cleanup;
242c8650fd0SDave Jiang
243c8650fd0SDave Jiang struct dentry *debugfs_node_dir;
244fce8a7bbSJon Mason };
245fce8a7bbSJon Mason
246fce8a7bbSJon Mason enum {
247e26a5843SAllen Hubbe DESC_DONE_FLAG = BIT(0),
248e26a5843SAllen Hubbe LINK_DOWN_FLAG = BIT(1),
249fce8a7bbSJon Mason };
250fce8a7bbSJon Mason
251fce8a7bbSJon Mason struct ntb_payload_header {
25274465645SJon Mason unsigned int ver;
253fce8a7bbSJon Mason unsigned int len;
254fce8a7bbSJon Mason unsigned int flags;
255fce8a7bbSJon Mason };
256fce8a7bbSJon Mason
257fce8a7bbSJon Mason enum {
258fce8a7bbSJon Mason VERSION = 0,
259fce8a7bbSJon Mason QP_LINKS,
260113fc505SJon Mason NUM_QPS,
261113fc505SJon Mason NUM_MWS,
262113fc505SJon Mason MW0_SZ_HIGH,
263113fc505SJon Mason MW0_SZ_LOW,
264fce8a7bbSJon Mason };
265fce8a7bbSJon Mason
266e26a5843SAllen Hubbe #define dev_client_dev(__dev) \
267e26a5843SAllen Hubbe container_of((__dev), struct ntb_transport_client_dev, dev)
268e26a5843SAllen Hubbe
269e26a5843SAllen Hubbe #define drv_client(__drv) \
270e26a5843SAllen Hubbe container_of((__drv), struct ntb_transport_client, driver)
271e26a5843SAllen Hubbe
272e26a5843SAllen Hubbe #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
273fce8a7bbSJon Mason #define NTB_QP_DEF_NUM_ENTRIES 100
274fce8a7bbSJon Mason #define NTB_LINK_DOWN_TIMEOUT 10
275fce8a7bbSJon Mason
276e26a5843SAllen Hubbe static void ntb_transport_rxc_db(unsigned long data);
277e26a5843SAllen Hubbe static const struct ntb_ctx_ops ntb_transport_ops;
278e26a5843SAllen Hubbe static struct ntb_client ntb_transport_client;
2799cabc269SDave Jiang static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
2809cabc269SDave Jiang struct ntb_queue_entry *entry);
2819cabc269SDave Jiang static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
28272203572SDave Jiang static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
28372203572SDave Jiang static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
28472203572SDave Jiang
285e26a5843SAllen Hubbe
ntb_transport_bus_match(struct device * dev,struct device_driver * drv)286e26a5843SAllen Hubbe static int ntb_transport_bus_match(struct device *dev,
287e26a5843SAllen Hubbe struct device_driver *drv)
288fce8a7bbSJon Mason {
289fce8a7bbSJon Mason return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
290fce8a7bbSJon Mason }
291fce8a7bbSJon Mason
ntb_transport_bus_probe(struct device * dev)292e26a5843SAllen Hubbe static int ntb_transport_bus_probe(struct device *dev)
293fce8a7bbSJon Mason {
294e26a5843SAllen Hubbe const struct ntb_transport_client *client;
2955e2cbf13SColin Ian King int rc;
296fce8a7bbSJon Mason
297fce8a7bbSJon Mason get_device(dev);
298e26a5843SAllen Hubbe
299e26a5843SAllen Hubbe client = drv_client(dev->driver);
300e26a5843SAllen Hubbe rc = client->probe(dev);
301fce8a7bbSJon Mason if (rc)
302fce8a7bbSJon Mason put_device(dev);
303fce8a7bbSJon Mason
304fce8a7bbSJon Mason return rc;
305fce8a7bbSJon Mason }
306fce8a7bbSJon Mason
ntb_transport_bus_remove(struct device * dev)307fc7a6209SUwe Kleine-König static void ntb_transport_bus_remove(struct device *dev)
308fce8a7bbSJon Mason {
309e26a5843SAllen Hubbe const struct ntb_transport_client *client;
310fce8a7bbSJon Mason
311e26a5843SAllen Hubbe client = drv_client(dev->driver);
312e26a5843SAllen Hubbe client->remove(dev);
313fce8a7bbSJon Mason
314fce8a7bbSJon Mason put_device(dev);
315fce8a7bbSJon Mason }
316fce8a7bbSJon Mason
317e26a5843SAllen Hubbe static struct bus_type ntb_transport_bus = {
318e26a5843SAllen Hubbe .name = "ntb_transport",
319e26a5843SAllen Hubbe .match = ntb_transport_bus_match,
320e26a5843SAllen Hubbe .probe = ntb_transport_bus_probe,
321e26a5843SAllen Hubbe .remove = ntb_transport_bus_remove,
322fce8a7bbSJon Mason };
323fce8a7bbSJon Mason
324fce8a7bbSJon Mason static LIST_HEAD(ntb_transport_list);
325fce8a7bbSJon Mason
ntb_bus_init(struct ntb_transport_ctx * nt)326e26a5843SAllen Hubbe static int ntb_bus_init(struct ntb_transport_ctx *nt)
327fce8a7bbSJon Mason {
32831510000SDave Jiang list_add_tail(&nt->entry, &ntb_transport_list);
329fce8a7bbSJon Mason return 0;
330fce8a7bbSJon Mason }
331fce8a7bbSJon Mason
ntb_bus_remove(struct ntb_transport_ctx * nt)332e26a5843SAllen Hubbe static void ntb_bus_remove(struct ntb_transport_ctx *nt)
333fce8a7bbSJon Mason {
334fce8a7bbSJon Mason struct ntb_transport_client_dev *client_dev, *cd;
335fce8a7bbSJon Mason
336fce8a7bbSJon Mason list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
337fce8a7bbSJon Mason dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
338fce8a7bbSJon Mason dev_name(&client_dev->dev));
339fce8a7bbSJon Mason list_del(&client_dev->entry);
340fce8a7bbSJon Mason device_unregister(&client_dev->dev);
341fce8a7bbSJon Mason }
342fce8a7bbSJon Mason
343fce8a7bbSJon Mason list_del(&nt->entry);
344fce8a7bbSJon Mason }
345fce8a7bbSJon Mason
ntb_transport_client_release(struct device * dev)346e26a5843SAllen Hubbe static void ntb_transport_client_release(struct device *dev)
347fce8a7bbSJon Mason {
348fce8a7bbSJon Mason struct ntb_transport_client_dev *client_dev;
349fce8a7bbSJon Mason
350e26a5843SAllen Hubbe client_dev = dev_client_dev(dev);
351fce8a7bbSJon Mason kfree(client_dev);
352fce8a7bbSJon Mason }
353fce8a7bbSJon Mason
354fce8a7bbSJon Mason /**
355e26a5843SAllen Hubbe * ntb_transport_unregister_client_dev - Unregister NTB client device
356fce8a7bbSJon Mason * @device_name: Name of NTB client device
357fce8a7bbSJon Mason *
358fce8a7bbSJon Mason * Unregister an NTB client device with the NTB transport layer
359fce8a7bbSJon Mason */
ntb_transport_unregister_client_dev(char * device_name)360e26a5843SAllen Hubbe void ntb_transport_unregister_client_dev(char *device_name)
361fce8a7bbSJon Mason {
362fce8a7bbSJon Mason struct ntb_transport_client_dev *client, *cd;
363e26a5843SAllen Hubbe struct ntb_transport_ctx *nt;
364fce8a7bbSJon Mason
365fce8a7bbSJon Mason list_for_each_entry(nt, &ntb_transport_list, entry)
366fce8a7bbSJon Mason list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
367fce8a7bbSJon Mason if (!strncmp(dev_name(&client->dev), device_name,
368fce8a7bbSJon Mason strlen(device_name))) {
369fce8a7bbSJon Mason list_del(&client->entry);
370fce8a7bbSJon Mason device_unregister(&client->dev);
371fce8a7bbSJon Mason }
372fce8a7bbSJon Mason }
373e26a5843SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
374fce8a7bbSJon Mason
375fce8a7bbSJon Mason /**
376e26a5843SAllen Hubbe * ntb_transport_register_client_dev - Register NTB client device
377fce8a7bbSJon Mason * @device_name: Name of NTB client device
378fce8a7bbSJon Mason *
379fce8a7bbSJon Mason * Register an NTB client device with the NTB transport layer
380fce8a7bbSJon Mason */
ntb_transport_register_client_dev(char * device_name)381e26a5843SAllen Hubbe int ntb_transport_register_client_dev(char *device_name)
382fce8a7bbSJon Mason {
383fce8a7bbSJon Mason struct ntb_transport_client_dev *client_dev;
384e26a5843SAllen Hubbe struct ntb_transport_ctx *nt;
3851199aa61SAllen Hubbe int node;
3868b19d450SJon Mason int rc, i = 0;
387fce8a7bbSJon Mason
3888222b402SJon Mason if (list_empty(&ntb_transport_list))
3898222b402SJon Mason return -ENODEV;
3908222b402SJon Mason
391fce8a7bbSJon Mason list_for_each_entry(nt, &ntb_transport_list, entry) {
392fce8a7bbSJon Mason struct device *dev;
393fce8a7bbSJon Mason
3941199aa61SAllen Hubbe node = dev_to_node(&nt->ndev->dev);
3951199aa61SAllen Hubbe
3961199aa61SAllen Hubbe client_dev = kzalloc_node(sizeof(*client_dev),
3971199aa61SAllen Hubbe GFP_KERNEL, node);
398fce8a7bbSJon Mason if (!client_dev) {
399fce8a7bbSJon Mason rc = -ENOMEM;
400fce8a7bbSJon Mason goto err;
401fce8a7bbSJon Mason }
402fce8a7bbSJon Mason
403fce8a7bbSJon Mason dev = &client_dev->dev;
404fce8a7bbSJon Mason
405fce8a7bbSJon Mason /* setup and register client devices */
4068b19d450SJon Mason dev_set_name(dev, "%s%d", device_name, i);
407e26a5843SAllen Hubbe dev->bus = &ntb_transport_bus;
408e26a5843SAllen Hubbe dev->release = ntb_transport_client_release;
409e26a5843SAllen Hubbe dev->parent = &nt->ndev->dev;
410fce8a7bbSJon Mason
411fce8a7bbSJon Mason rc = device_register(dev);
412fce8a7bbSJon Mason if (rc) {
4138623ccbfSYang Yingliang put_device(dev);
414fce8a7bbSJon Mason goto err;
415fce8a7bbSJon Mason }
416fce8a7bbSJon Mason
417fce8a7bbSJon Mason list_add_tail(&client_dev->entry, &nt->client_devs);
4188b19d450SJon Mason i++;
419fce8a7bbSJon Mason }
420fce8a7bbSJon Mason
421fce8a7bbSJon Mason return 0;
422fce8a7bbSJon Mason
423fce8a7bbSJon Mason err:
424e26a5843SAllen Hubbe ntb_transport_unregister_client_dev(device_name);
425fce8a7bbSJon Mason
426fce8a7bbSJon Mason return rc;
427fce8a7bbSJon Mason }
428e26a5843SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
429fce8a7bbSJon Mason
430fce8a7bbSJon Mason /**
431ec110bc7SAllen Hubbe * ntb_transport_register_client - Register NTB client driver
432fce8a7bbSJon Mason * @drv: NTB client driver to be registered
433fce8a7bbSJon Mason *
434fce8a7bbSJon Mason * Register an NTB client driver with the NTB transport layer
435fce8a7bbSJon Mason *
436fce8a7bbSJon Mason * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
437fce8a7bbSJon Mason */
ntb_transport_register_client(struct ntb_transport_client * drv)438e26a5843SAllen Hubbe int ntb_transport_register_client(struct ntb_transport_client *drv)
439fce8a7bbSJon Mason {
440e26a5843SAllen Hubbe drv->driver.bus = &ntb_transport_bus;
441fce8a7bbSJon Mason
4428222b402SJon Mason if (list_empty(&ntb_transport_list))
4438222b402SJon Mason return -ENODEV;
4448222b402SJon Mason
445fce8a7bbSJon Mason return driver_register(&drv->driver);
446fce8a7bbSJon Mason }
447ec110bc7SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_register_client);
448fce8a7bbSJon Mason
449fce8a7bbSJon Mason /**
450ec110bc7SAllen Hubbe * ntb_transport_unregister_client - Unregister NTB client driver
451fce8a7bbSJon Mason * @drv: NTB client driver to be unregistered
452fce8a7bbSJon Mason *
453fce8a7bbSJon Mason * Unregister an NTB client driver with the NTB transport layer
454fce8a7bbSJon Mason *
455fce8a7bbSJon Mason * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
456fce8a7bbSJon Mason */
ntb_transport_unregister_client(struct ntb_transport_client * drv)457e26a5843SAllen Hubbe void ntb_transport_unregister_client(struct ntb_transport_client *drv)
458fce8a7bbSJon Mason {
459fce8a7bbSJon Mason driver_unregister(&drv->driver);
460fce8a7bbSJon Mason }
461ec110bc7SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
462fce8a7bbSJon Mason
debugfs_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)463fce8a7bbSJon Mason static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
464fce8a7bbSJon Mason loff_t *offp)
465fce8a7bbSJon Mason {
466fce8a7bbSJon Mason struct ntb_transport_qp *qp;
467d7237e22SJon Mason char *buf;
468fce8a7bbSJon Mason ssize_t ret, out_offset, out_count;
469fce8a7bbSJon Mason
470260bee94SDave Jiang qp = filp->private_data;
471260bee94SDave Jiang
472260bee94SDave Jiang if (!qp || !qp->link_is_up)
473260bee94SDave Jiang return 0;
474260bee94SDave Jiang
475282a2feeSJon Mason out_count = 1000;
476d7237e22SJon Mason
477d7237e22SJon Mason buf = kmalloc(out_count, GFP_KERNEL);
478d7237e22SJon Mason if (!buf)
479d7237e22SJon Mason return -ENOMEM;
480fce8a7bbSJon Mason
481fce8a7bbSJon Mason out_offset = 0;
4827f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
483d98ef99eSDave Jiang "\nNTB QP stats:\n\n");
4847f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
485fce8a7bbSJon Mason "rx_bytes - \t%llu\n", qp->rx_bytes);
4867f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
487fce8a7bbSJon Mason "rx_pkts - \t%llu\n", qp->rx_pkts);
4887f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
489282a2feeSJon Mason "rx_memcpy - \t%llu\n", qp->rx_memcpy);
4907f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
491282a2feeSJon Mason "rx_async - \t%llu\n", qp->rx_async);
4927f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
493fce8a7bbSJon Mason "rx_ring_empty - %llu\n", qp->rx_ring_empty);
4947f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
495fce8a7bbSJon Mason "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
4967f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
497fce8a7bbSJon Mason "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
4987f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
499fce8a7bbSJon Mason "rx_err_ver - \t%llu\n", qp->rx_err_ver);
5007f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
501d98ef99eSDave Jiang "rx_buff - \t0x%p\n", qp->rx_buff);
5027f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
503793c20e9SJon Mason "rx_index - \t%u\n", qp->rx_index);
5047f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
505a754a8fcSDave Jiang "rx_max_entry - \t%u\n", qp->rx_max_entry);
5067f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
507a754a8fcSDave Jiang "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
508fce8a7bbSJon Mason
5097f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
510fce8a7bbSJon Mason "tx_bytes - \t%llu\n", qp->tx_bytes);
5117f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
512fce8a7bbSJon Mason "tx_pkts - \t%llu\n", qp->tx_pkts);
5137f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
514282a2feeSJon Mason "tx_memcpy - \t%llu\n", qp->tx_memcpy);
5157f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
516282a2feeSJon Mason "tx_async - \t%llu\n", qp->tx_async);
5177f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
518fce8a7bbSJon Mason "tx_ring_full - \t%llu\n", qp->tx_ring_full);
5197f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
520282a2feeSJon Mason "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
5217f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
522d98ef99eSDave Jiang "tx_mw - \t0x%p\n", qp->tx_mw);
5237f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
524d98ef99eSDave Jiang "tx_index (H) - \t%u\n", qp->tx_index);
5257f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
526d98ef99eSDave Jiang "RRI (T) - \t%u\n",
527d98ef99eSDave Jiang qp->remote_rx_info->entry);
5287f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
529793c20e9SJon Mason "tx_max_entry - \t%u\n", qp->tx_max_entry);
5307f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
531e74bfeedSDave Jiang "free tx - \t%u\n",
532e74bfeedSDave Jiang ntb_transport_tx_free_entry(qp));
533fce8a7bbSJon Mason
5347f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
535d98ef99eSDave Jiang "\n");
5367f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
537569410caSDave Jiang "Using TX DMA - \t%s\n",
538569410caSDave Jiang qp->tx_dma_chan ? "Yes" : "No");
5397f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
540569410caSDave Jiang "Using RX DMA - \t%s\n",
541569410caSDave Jiang qp->rx_dma_chan ? "Yes" : "No");
5427f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
543d98ef99eSDave Jiang "QP Link - \t%s\n",
544e26a5843SAllen Hubbe qp->link_is_up ? "Up" : "Down");
5457f78c68aSTakashi Iwai out_offset += scnprintf(buf + out_offset, out_count - out_offset,
546d98ef99eSDave Jiang "\n");
547d98ef99eSDave Jiang
548d7237e22SJon Mason if (out_offset > out_count)
549d7237e22SJon Mason out_offset = out_count;
550fce8a7bbSJon Mason
551fce8a7bbSJon Mason ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
552d7237e22SJon Mason kfree(buf);
553fce8a7bbSJon Mason return ret;
554fce8a7bbSJon Mason }
555fce8a7bbSJon Mason
556fce8a7bbSJon Mason static const struct file_operations ntb_qp_debugfs_stats = {
557fce8a7bbSJon Mason .owner = THIS_MODULE,
558d66d7ac2SJon Mason .open = simple_open,
559fce8a7bbSJon Mason .read = debugfs_read,
560fce8a7bbSJon Mason };
561fce8a7bbSJon Mason
ntb_list_add(spinlock_t * lock,struct list_head * entry,struct list_head * list)562fce8a7bbSJon Mason static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
563fce8a7bbSJon Mason struct list_head *list)
564fce8a7bbSJon Mason {
565fce8a7bbSJon Mason unsigned long flags;
566fce8a7bbSJon Mason
567fce8a7bbSJon Mason spin_lock_irqsave(lock, flags);
568fce8a7bbSJon Mason list_add_tail(entry, list);
569fce8a7bbSJon Mason spin_unlock_irqrestore(lock, flags);
570fce8a7bbSJon Mason }
571fce8a7bbSJon Mason
ntb_list_rm(spinlock_t * lock,struct list_head * list)572fce8a7bbSJon Mason static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
573fce8a7bbSJon Mason struct list_head *list)
574fce8a7bbSJon Mason {
575fce8a7bbSJon Mason struct ntb_queue_entry *entry;
576fce8a7bbSJon Mason unsigned long flags;
577fce8a7bbSJon Mason
578fce8a7bbSJon Mason spin_lock_irqsave(lock, flags);
579fce8a7bbSJon Mason if (list_empty(list)) {
580fce8a7bbSJon Mason entry = NULL;
581fce8a7bbSJon Mason goto out;
582fce8a7bbSJon Mason }
583fce8a7bbSJon Mason entry = list_first_entry(list, struct ntb_queue_entry, entry);
584fce8a7bbSJon Mason list_del(&entry->entry);
585e74bfeedSDave Jiang
586fce8a7bbSJon Mason out:
587fce8a7bbSJon Mason spin_unlock_irqrestore(lock, flags);
588fce8a7bbSJon Mason
589fce8a7bbSJon Mason return entry;
590fce8a7bbSJon Mason }
591fce8a7bbSJon Mason
ntb_list_mv(spinlock_t * lock,struct list_head * list,struct list_head * to_list)592da2e5ae5SAllen Hubbe static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
593da2e5ae5SAllen Hubbe struct list_head *list,
594da2e5ae5SAllen Hubbe struct list_head *to_list)
595da2e5ae5SAllen Hubbe {
596da2e5ae5SAllen Hubbe struct ntb_queue_entry *entry;
597da2e5ae5SAllen Hubbe unsigned long flags;
598da2e5ae5SAllen Hubbe
599da2e5ae5SAllen Hubbe spin_lock_irqsave(lock, flags);
600da2e5ae5SAllen Hubbe
601da2e5ae5SAllen Hubbe if (list_empty(list)) {
602da2e5ae5SAllen Hubbe entry = NULL;
603da2e5ae5SAllen Hubbe } else {
604da2e5ae5SAllen Hubbe entry = list_first_entry(list, struct ntb_queue_entry, entry);
605da2e5ae5SAllen Hubbe list_move_tail(&entry->entry, to_list);
606da2e5ae5SAllen Hubbe }
607da2e5ae5SAllen Hubbe
608da2e5ae5SAllen Hubbe spin_unlock_irqrestore(lock, flags);
609da2e5ae5SAllen Hubbe
610da2e5ae5SAllen Hubbe return entry;
611da2e5ae5SAllen Hubbe }
612da2e5ae5SAllen Hubbe
ntb_transport_setup_qp_mw(struct ntb_transport_ctx * nt,unsigned int qp_num)613e26a5843SAllen Hubbe static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
614fce8a7bbSJon Mason unsigned int qp_num)
615fce8a7bbSJon Mason {
616e26a5843SAllen Hubbe struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
617e26a5843SAllen Hubbe struct ntb_transport_mw *mw;
618a754a8fcSDave Jiang struct ntb_dev *ndev = nt->ndev;
619a754a8fcSDave Jiang struct ntb_queue_entry *entry;
620ef114ed5SJon Mason unsigned int rx_size, num_qps_mw;
621e26a5843SAllen Hubbe unsigned int mw_num, mw_count, qp_count;
622793c20e9SJon Mason unsigned int i;
623a754a8fcSDave Jiang int node;
624fce8a7bbSJon Mason
625e26a5843SAllen Hubbe mw_count = nt->mw_count;
626e26a5843SAllen Hubbe qp_count = nt->qp_count;
627948d3a65SJon Mason
628e26a5843SAllen Hubbe mw_num = QP_TO_MW(nt, qp_num);
629e26a5843SAllen Hubbe mw = &nt->mw_vec[mw_num];
630fce8a7bbSJon Mason
631e26a5843SAllen Hubbe if (!mw->virt_addr)
632e26a5843SAllen Hubbe return -ENOMEM;
633e26a5843SAllen Hubbe
6348e8496e0SLogan Gunthorpe if (mw_num < qp_count % mw_count)
635e26a5843SAllen Hubbe num_qps_mw = qp_count / mw_count + 1;
636fce8a7bbSJon Mason else
637e26a5843SAllen Hubbe num_qps_mw = qp_count / mw_count;
638fce8a7bbSJon Mason
639e26a5843SAllen Hubbe rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
640c92ba3c5SJon Mason qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
641793c20e9SJon Mason rx_size -= sizeof(struct ntb_rx_info);
642793c20e9SJon Mason
643282a2feeSJon Mason qp->remote_rx_info = qp->rx_buff + rx_size;
644282a2feeSJon Mason
645c9d534c8SJon Mason /* Due to housekeeping, there must be atleast 2 buffs */
646c9d534c8SJon Mason qp->rx_max_frame = min(transport_mtu, rx_size / 2);
647793c20e9SJon Mason qp->rx_max_entry = rx_size / qp->rx_max_frame;
648793c20e9SJon Mason qp->rx_index = 0;
649793c20e9SJon Mason
650a754a8fcSDave Jiang /*
651a754a8fcSDave Jiang * Checking to see if we have more entries than the default.
652a754a8fcSDave Jiang * We should add additional entries if that is the case so we
653a754a8fcSDave Jiang * can be in sync with the transport frames.
654a754a8fcSDave Jiang */
655a754a8fcSDave Jiang node = dev_to_node(&ndev->dev);
656a754a8fcSDave Jiang for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
65782edcc75SJia-Ju Bai entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
658a754a8fcSDave Jiang if (!entry)
659a754a8fcSDave Jiang return -ENOMEM;
660a754a8fcSDave Jiang
661a754a8fcSDave Jiang entry->qp = qp;
662a754a8fcSDave Jiang ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
663a754a8fcSDave Jiang &qp->rx_free_q);
664a754a8fcSDave Jiang qp->rx_alloc_entry++;
665a754a8fcSDave Jiang }
666a754a8fcSDave Jiang
667c9d534c8SJon Mason qp->remote_rx_info->entry = qp->rx_max_entry - 1;
668fce8a7bbSJon Mason
669ef114ed5SJon Mason /* setup the hdr offsets with 0's */
670793c20e9SJon Mason for (i = 0; i < qp->rx_max_entry; i++) {
671e26a5843SAllen Hubbe void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
672e26a5843SAllen Hubbe sizeof(struct ntb_payload_header));
673ef114ed5SJon Mason memset(offset, 0, sizeof(struct ntb_payload_header));
674793c20e9SJon Mason }
675fce8a7bbSJon Mason
676fce8a7bbSJon Mason qp->rx_pkts = 0;
677fce8a7bbSJon Mason qp->tx_pkts = 0;
67890f9e934SJon Mason qp->tx_index = 0;
679e26a5843SAllen Hubbe
680e26a5843SAllen Hubbe return 0;
681fce8a7bbSJon Mason }
682fce8a7bbSJon Mason
ntb_transport_isr(int irq,void * dev)6832b0569b3SLogan Gunthorpe static irqreturn_t ntb_transport_isr(int irq, void *dev)
6842b0569b3SLogan Gunthorpe {
6852b0569b3SLogan Gunthorpe struct ntb_transport_qp *qp = dev;
6862b0569b3SLogan Gunthorpe
6872b0569b3SLogan Gunthorpe tasklet_schedule(&qp->rxc_db_work);
6882b0569b3SLogan Gunthorpe
6892b0569b3SLogan Gunthorpe return IRQ_HANDLED;
6902b0569b3SLogan Gunthorpe }
6912b0569b3SLogan Gunthorpe
ntb_transport_setup_qp_peer_msi(struct ntb_transport_ctx * nt,unsigned int qp_num)6922b0569b3SLogan Gunthorpe static void ntb_transport_setup_qp_peer_msi(struct ntb_transport_ctx *nt,
6932b0569b3SLogan Gunthorpe unsigned int qp_num)
6942b0569b3SLogan Gunthorpe {
6952b0569b3SLogan Gunthorpe struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
6962b0569b3SLogan Gunthorpe int spad = qp_num * 2 + nt->msi_spad_offset;
6972b0569b3SLogan Gunthorpe
6982b0569b3SLogan Gunthorpe if (!nt->use_msi)
6992b0569b3SLogan Gunthorpe return;
7002b0569b3SLogan Gunthorpe
7012b0569b3SLogan Gunthorpe if (spad >= ntb_spad_count(nt->ndev))
7022b0569b3SLogan Gunthorpe return;
7032b0569b3SLogan Gunthorpe
7042b0569b3SLogan Gunthorpe qp->peer_msi_desc.addr_offset =
7052b0569b3SLogan Gunthorpe ntb_peer_spad_read(qp->ndev, PIDX, spad);
7062b0569b3SLogan Gunthorpe qp->peer_msi_desc.data =
7072b0569b3SLogan Gunthorpe ntb_peer_spad_read(qp->ndev, PIDX, spad + 1);
7082b0569b3SLogan Gunthorpe
7092b0569b3SLogan Gunthorpe dev_dbg(&qp->ndev->pdev->dev, "QP%d Peer MSI addr=%x data=%x\n",
7102b0569b3SLogan Gunthorpe qp_num, qp->peer_msi_desc.addr_offset, qp->peer_msi_desc.data);
7112b0569b3SLogan Gunthorpe
7122b0569b3SLogan Gunthorpe if (qp->peer_msi_desc.addr_offset) {
7132b0569b3SLogan Gunthorpe qp->use_msi = true;
7142b0569b3SLogan Gunthorpe dev_info(&qp->ndev->pdev->dev,
7152b0569b3SLogan Gunthorpe "Using MSI interrupts for QP%d\n", qp_num);
7162b0569b3SLogan Gunthorpe }
7172b0569b3SLogan Gunthorpe }
7182b0569b3SLogan Gunthorpe
ntb_transport_setup_qp_msi(struct ntb_transport_ctx * nt,unsigned int qp_num)7192b0569b3SLogan Gunthorpe static void ntb_transport_setup_qp_msi(struct ntb_transport_ctx *nt,
7202b0569b3SLogan Gunthorpe unsigned int qp_num)
7212b0569b3SLogan Gunthorpe {
7222b0569b3SLogan Gunthorpe struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
7232b0569b3SLogan Gunthorpe int spad = qp_num * 2 + nt->msi_spad_offset;
7242b0569b3SLogan Gunthorpe int rc;
7252b0569b3SLogan Gunthorpe
7262b0569b3SLogan Gunthorpe if (!nt->use_msi)
7272b0569b3SLogan Gunthorpe return;
7282b0569b3SLogan Gunthorpe
7292b0569b3SLogan Gunthorpe if (spad >= ntb_spad_count(nt->ndev)) {
7302b0569b3SLogan Gunthorpe dev_warn_once(&qp->ndev->pdev->dev,
7312b0569b3SLogan Gunthorpe "Not enough SPADS to use MSI interrupts\n");
7322b0569b3SLogan Gunthorpe return;
7332b0569b3SLogan Gunthorpe }
7342b0569b3SLogan Gunthorpe
7352b0569b3SLogan Gunthorpe ntb_spad_write(qp->ndev, spad, 0);
7362b0569b3SLogan Gunthorpe ntb_spad_write(qp->ndev, spad + 1, 0);
7372b0569b3SLogan Gunthorpe
7382b0569b3SLogan Gunthorpe if (!qp->msi_irq) {
7392b0569b3SLogan Gunthorpe qp->msi_irq = ntbm_msi_request_irq(qp->ndev, ntb_transport_isr,
7402b0569b3SLogan Gunthorpe KBUILD_MODNAME, qp,
7412b0569b3SLogan Gunthorpe &qp->msi_desc);
7422b0569b3SLogan Gunthorpe if (qp->msi_irq < 0) {
7432b0569b3SLogan Gunthorpe dev_warn(&qp->ndev->pdev->dev,
7442b0569b3SLogan Gunthorpe "Unable to allocate MSI interrupt for qp%d\n",
7452b0569b3SLogan Gunthorpe qp_num);
7462b0569b3SLogan Gunthorpe return;
7472b0569b3SLogan Gunthorpe }
7482b0569b3SLogan Gunthorpe }
7492b0569b3SLogan Gunthorpe
7502b0569b3SLogan Gunthorpe rc = ntb_spad_write(qp->ndev, spad, qp->msi_desc.addr_offset);
7512b0569b3SLogan Gunthorpe if (rc)
7522b0569b3SLogan Gunthorpe goto err_free_interrupt;
7532b0569b3SLogan Gunthorpe
7542b0569b3SLogan Gunthorpe rc = ntb_spad_write(qp->ndev, spad + 1, qp->msi_desc.data);
7552b0569b3SLogan Gunthorpe if (rc)
7562b0569b3SLogan Gunthorpe goto err_free_interrupt;
7572b0569b3SLogan Gunthorpe
7582b0569b3SLogan Gunthorpe dev_dbg(&qp->ndev->pdev->dev, "QP%d MSI %d addr=%x data=%x\n",
7592b0569b3SLogan Gunthorpe qp_num, qp->msi_irq, qp->msi_desc.addr_offset,
7602b0569b3SLogan Gunthorpe qp->msi_desc.data);
7612b0569b3SLogan Gunthorpe
7622b0569b3SLogan Gunthorpe return;
7632b0569b3SLogan Gunthorpe
7642b0569b3SLogan Gunthorpe err_free_interrupt:
7652b0569b3SLogan Gunthorpe devm_free_irq(&nt->ndev->dev, qp->msi_irq, qp);
7662b0569b3SLogan Gunthorpe }
7672b0569b3SLogan Gunthorpe
ntb_transport_msi_peer_desc_changed(struct ntb_transport_ctx * nt)7682b0569b3SLogan Gunthorpe static void ntb_transport_msi_peer_desc_changed(struct ntb_transport_ctx *nt)
7692b0569b3SLogan Gunthorpe {
7702b0569b3SLogan Gunthorpe int i;
7712b0569b3SLogan Gunthorpe
7722b0569b3SLogan Gunthorpe dev_dbg(&nt->ndev->pdev->dev, "Peer MSI descriptors changed");
7732b0569b3SLogan Gunthorpe
7742b0569b3SLogan Gunthorpe for (i = 0; i < nt->qp_count; i++)
7752b0569b3SLogan Gunthorpe ntb_transport_setup_qp_peer_msi(nt, i);
7762b0569b3SLogan Gunthorpe }
7772b0569b3SLogan Gunthorpe
ntb_transport_msi_desc_changed(void * data)7782b0569b3SLogan Gunthorpe static void ntb_transport_msi_desc_changed(void *data)
7792b0569b3SLogan Gunthorpe {
7802b0569b3SLogan Gunthorpe struct ntb_transport_ctx *nt = data;
7812b0569b3SLogan Gunthorpe int i;
7822b0569b3SLogan Gunthorpe
7832b0569b3SLogan Gunthorpe dev_dbg(&nt->ndev->pdev->dev, "MSI descriptors changed");
7842b0569b3SLogan Gunthorpe
7852b0569b3SLogan Gunthorpe for (i = 0; i < nt->qp_count; i++)
7862b0569b3SLogan Gunthorpe ntb_transport_setup_qp_msi(nt, i);
7872b0569b3SLogan Gunthorpe
7882b0569b3SLogan Gunthorpe ntb_peer_db_set(nt->ndev, nt->msi_db_mask);
7892b0569b3SLogan Gunthorpe }
7902b0569b3SLogan Gunthorpe
ntb_free_mw(struct ntb_transport_ctx * nt,int num_mw)791e26a5843SAllen Hubbe static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
792113fc505SJon Mason {
793e26a5843SAllen Hubbe struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
794e26a5843SAllen Hubbe struct pci_dev *pdev = nt->ndev->pdev;
795113fc505SJon Mason
796113fc505SJon Mason if (!mw->virt_addr)
797113fc505SJon Mason return;
798113fc505SJon Mason
799443b9a14SSerge Semin ntb_mw_clear_trans(nt->ndev, PIDX, num_mw);
800fc5d1829SAaron Sierra dma_free_coherent(&pdev->dev, mw->alloc_size,
801fc5d1829SAaron Sierra mw->alloc_addr, mw->dma_addr);
802e26a5843SAllen Hubbe mw->xlat_size = 0;
803e26a5843SAllen Hubbe mw->buff_size = 0;
804fc5d1829SAaron Sierra mw->alloc_size = 0;
805fc5d1829SAaron Sierra mw->alloc_addr = NULL;
806113fc505SJon Mason mw->virt_addr = NULL;
807113fc505SJon Mason }
808113fc505SJon Mason
ntb_alloc_mw_buffer(struct ntb_transport_mw * mw,struct device * ntb_dev,size_t align)809fc5d1829SAaron Sierra static int ntb_alloc_mw_buffer(struct ntb_transport_mw *mw,
810*b743922bSDave Jiang struct device *ntb_dev, size_t align)
811fc5d1829SAaron Sierra {
812fc5d1829SAaron Sierra dma_addr_t dma_addr;
813fc5d1829SAaron Sierra void *alloc_addr, *virt_addr;
814fc5d1829SAaron Sierra int rc;
815fc5d1829SAaron Sierra
816*b743922bSDave Jiang /*
817*b743922bSDave Jiang * The buffer here is allocated against the NTB device. The reason to
818*b743922bSDave Jiang * use dma_alloc_*() call is to allocate a large IOVA contiguous buffer
819*b743922bSDave Jiang * backing the NTB BAR for the remote host to write to. During receive
820*b743922bSDave Jiang * processing, the data is being copied out of the receive buffer to
821*b743922bSDave Jiang * the kernel skbuff. When a DMA device is being used, dma_map_page()
822*b743922bSDave Jiang * is called on the kvaddr of the receive buffer (from dma_alloc_*())
823*b743922bSDave Jiang * and remapped against the DMA device. It appears to be a double
824*b743922bSDave Jiang * DMA mapping of buffers, but first is mapped to the NTB device and
825*b743922bSDave Jiang * second is to the DMA device. DMA_ATTR_FORCE_CONTIGUOUS is necessary
826*b743922bSDave Jiang * in order for the later dma_map_page() to not fail.
827*b743922bSDave Jiang */
828*b743922bSDave Jiang alloc_addr = dma_alloc_attrs(ntb_dev, mw->alloc_size,
829*b743922bSDave Jiang &dma_addr, GFP_KERNEL,
830*b743922bSDave Jiang DMA_ATTR_FORCE_CONTIGUOUS);
831fc5d1829SAaron Sierra if (!alloc_addr) {
832*b743922bSDave Jiang dev_err(ntb_dev, "Unable to alloc MW buff of size %zu\n",
833fc5d1829SAaron Sierra mw->alloc_size);
834fc5d1829SAaron Sierra return -ENOMEM;
835fc5d1829SAaron Sierra }
836fc5d1829SAaron Sierra virt_addr = alloc_addr;
837fc5d1829SAaron Sierra
838fc5d1829SAaron Sierra /*
839fc5d1829SAaron Sierra * we must ensure that the memory address allocated is BAR size
840fc5d1829SAaron Sierra * aligned in order for the XLAT register to take the value. This
841fc5d1829SAaron Sierra * is a requirement of the hardware. It is recommended to setup CMA
842fc5d1829SAaron Sierra * for BAR sizes equal or greater than 4MB.
843fc5d1829SAaron Sierra */
844fc5d1829SAaron Sierra if (!IS_ALIGNED(dma_addr, align)) {
845fc5d1829SAaron Sierra if (mw->alloc_size > mw->buff_size) {
846fc5d1829SAaron Sierra virt_addr = PTR_ALIGN(alloc_addr, align);
847fc5d1829SAaron Sierra dma_addr = ALIGN(dma_addr, align);
848fc5d1829SAaron Sierra } else {
849fc5d1829SAaron Sierra rc = -ENOMEM;
850fc5d1829SAaron Sierra goto err;
851fc5d1829SAaron Sierra }
852fc5d1829SAaron Sierra }
853fc5d1829SAaron Sierra
854fc5d1829SAaron Sierra mw->alloc_addr = alloc_addr;
855fc5d1829SAaron Sierra mw->virt_addr = virt_addr;
856fc5d1829SAaron Sierra mw->dma_addr = dma_addr;
857fc5d1829SAaron Sierra
858fc5d1829SAaron Sierra return 0;
859fc5d1829SAaron Sierra
860fc5d1829SAaron Sierra err:
861*b743922bSDave Jiang dma_free_coherent(ntb_dev, mw->alloc_size, alloc_addr, dma_addr);
862fc5d1829SAaron Sierra
863fc5d1829SAaron Sierra return rc;
864fc5d1829SAaron Sierra }
865fc5d1829SAaron Sierra
ntb_set_mw(struct ntb_transport_ctx * nt,int num_mw,resource_size_t size)866e26a5843SAllen Hubbe static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
8678c9edf63SAllen Hubbe resource_size_t size)
868b77b2637SJon Mason {
869e26a5843SAllen Hubbe struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
870e26a5843SAllen Hubbe struct pci_dev *pdev = nt->ndev->pdev;
8718c9edf63SAllen Hubbe size_t xlat_size, buff_size;
872980c41c8SLogan Gunthorpe resource_size_t xlat_align;
873980c41c8SLogan Gunthorpe resource_size_t xlat_align_size;
874e26a5843SAllen Hubbe int rc;
875e26a5843SAllen Hubbe
8768c9edf63SAllen Hubbe if (!size)
8778c9edf63SAllen Hubbe return -EINVAL;
8788c9edf63SAllen Hubbe
879980c41c8SLogan Gunthorpe rc = ntb_mw_get_align(nt->ndev, PIDX, num_mw, &xlat_align,
880980c41c8SLogan Gunthorpe &xlat_align_size, NULL);
881980c41c8SLogan Gunthorpe if (rc)
882980c41c8SLogan Gunthorpe return rc;
883980c41c8SLogan Gunthorpe
884980c41c8SLogan Gunthorpe xlat_size = round_up(size, xlat_align_size);
885980c41c8SLogan Gunthorpe buff_size = round_up(size, xlat_align);
886b77b2637SJon Mason
887b77b2637SJon Mason /* No need to re-setup */
888e26a5843SAllen Hubbe if (mw->xlat_size == xlat_size)
889b77b2637SJon Mason return 0;
890b77b2637SJon Mason
891e26a5843SAllen Hubbe if (mw->buff_size)
892b77b2637SJon Mason ntb_free_mw(nt, num_mw);
893b77b2637SJon Mason
894e26a5843SAllen Hubbe /* Alloc memory for receiving data. Must be aligned */
895e26a5843SAllen Hubbe mw->xlat_size = xlat_size;
896e26a5843SAllen Hubbe mw->buff_size = buff_size;
897fc5d1829SAaron Sierra mw->alloc_size = buff_size;
898b77b2637SJon Mason
899fc5d1829SAaron Sierra rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
900fc5d1829SAaron Sierra if (rc) {
901fc5d1829SAaron Sierra mw->alloc_size *= 2;
902fc5d1829SAaron Sierra rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
903fc5d1829SAaron Sierra if (rc) {
904fc5d1829SAaron Sierra dev_err(&pdev->dev,
905fc5d1829SAaron Sierra "Unable to alloc aligned MW buff\n");
906e26a5843SAllen Hubbe mw->xlat_size = 0;
907e26a5843SAllen Hubbe mw->buff_size = 0;
908fc5d1829SAaron Sierra mw->alloc_size = 0;
909fc5d1829SAaron Sierra return rc;
910b77b2637SJon Mason }
9113cc5ba19SDave Jiang }
9123cc5ba19SDave Jiang
913b77b2637SJon Mason /* Notify HW the memory location of the receive buffer */
914443b9a14SSerge Semin rc = ntb_mw_set_trans(nt->ndev, PIDX, num_mw, mw->dma_addr,
915443b9a14SSerge Semin mw->xlat_size);
916e26a5843SAllen Hubbe if (rc) {
917e26a5843SAllen Hubbe dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
918e26a5843SAllen Hubbe ntb_free_mw(nt, num_mw);
919e26a5843SAllen Hubbe return -EIO;
920e26a5843SAllen Hubbe }
921b77b2637SJon Mason
922b77b2637SJon Mason return 0;
923b77b2637SJon Mason }
924b77b2637SJon Mason
ntb_qp_link_context_reset(struct ntb_transport_qp * qp)925cc79bd27SDave Jiang static void ntb_qp_link_context_reset(struct ntb_transport_qp *qp)
9262849b5d7SAllen Hubbe {
9272849b5d7SAllen Hubbe qp->link_is_up = false;
928e9021331SDave Jiang qp->active = false;
9292849b5d7SAllen Hubbe
9302849b5d7SAllen Hubbe qp->tx_index = 0;
9312849b5d7SAllen Hubbe qp->rx_index = 0;
9322849b5d7SAllen Hubbe qp->rx_bytes = 0;
9332849b5d7SAllen Hubbe qp->rx_pkts = 0;
9342849b5d7SAllen Hubbe qp->rx_ring_empty = 0;
9352849b5d7SAllen Hubbe qp->rx_err_no_buf = 0;
9362849b5d7SAllen Hubbe qp->rx_err_oflow = 0;
9372849b5d7SAllen Hubbe qp->rx_err_ver = 0;
9382849b5d7SAllen Hubbe qp->rx_memcpy = 0;
9392849b5d7SAllen Hubbe qp->rx_async = 0;
9402849b5d7SAllen Hubbe qp->tx_bytes = 0;
9412849b5d7SAllen Hubbe qp->tx_pkts = 0;
9422849b5d7SAllen Hubbe qp->tx_ring_full = 0;
9432849b5d7SAllen Hubbe qp->tx_err_no_buf = 0;
9442849b5d7SAllen Hubbe qp->tx_memcpy = 0;
9452849b5d7SAllen Hubbe qp->tx_async = 0;
9462849b5d7SAllen Hubbe }
9472849b5d7SAllen Hubbe
ntb_qp_link_down_reset(struct ntb_transport_qp * qp)948cc79bd27SDave Jiang static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
949cc79bd27SDave Jiang {
950cc79bd27SDave Jiang ntb_qp_link_context_reset(qp);
951cc79bd27SDave Jiang if (qp->remote_rx_info)
952cc79bd27SDave Jiang qp->remote_rx_info->entry = qp->rx_max_entry - 1;
953cc79bd27SDave Jiang }
954cc79bd27SDave Jiang
ntb_qp_link_cleanup(struct ntb_transport_qp * qp)955fca4d518SJon Mason static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
956fce8a7bbSJon Mason {
957e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = qp->transport;
958e26a5843SAllen Hubbe struct pci_dev *pdev = nt->ndev->pdev;
959fce8a7bbSJon Mason
960e22e0b9dSAllen Hubbe dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
9612849b5d7SAllen Hubbe
9622849b5d7SAllen Hubbe cancel_delayed_work_sync(&qp->link_work);
9632849b5d7SAllen Hubbe ntb_qp_link_down_reset(qp);
964e26a5843SAllen Hubbe
965e26a5843SAllen Hubbe if (qp->event_handler)
966e26a5843SAllen Hubbe qp->event_handler(qp->cb_data, qp->link_is_up);
967fca4d518SJon Mason }
968fca4d518SJon Mason
ntb_qp_link_cleanup_work(struct work_struct * work)969fca4d518SJon Mason static void ntb_qp_link_cleanup_work(struct work_struct *work)
970fca4d518SJon Mason {
971fca4d518SJon Mason struct ntb_transport_qp *qp = container_of(work,
972fca4d518SJon Mason struct ntb_transport_qp,
973fca4d518SJon Mason link_cleanup);
974e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = qp->transport;
975fca4d518SJon Mason
976fca4d518SJon Mason ntb_qp_link_cleanup(qp);
977fce8a7bbSJon Mason
978e26a5843SAllen Hubbe if (nt->link_is_up)
979fce8a7bbSJon Mason schedule_delayed_work(&qp->link_work,
980fce8a7bbSJon Mason msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
981fce8a7bbSJon Mason }
982fce8a7bbSJon Mason
ntb_qp_link_down(struct ntb_transport_qp * qp)9837b4f2d3cSJon Mason static void ntb_qp_link_down(struct ntb_transport_qp *qp)
984fce8a7bbSJon Mason {
9857b4f2d3cSJon Mason schedule_work(&qp->link_cleanup);
9867b4f2d3cSJon Mason }
9877b4f2d3cSJon Mason
ntb_transport_link_cleanup(struct ntb_transport_ctx * nt)988e26a5843SAllen Hubbe static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
9897b4f2d3cSJon Mason {
990e26a5843SAllen Hubbe struct ntb_transport_qp *qp;
991e26a5843SAllen Hubbe u64 qp_bitmap_alloc;
992b17faba0SShyam Sundar S K unsigned int i, count;
993fce8a7bbSJon Mason
994e26a5843SAllen Hubbe qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
995fca4d518SJon Mason
996e26a5843SAllen Hubbe /* Pass along the info to any clients */
997e26a5843SAllen Hubbe for (i = 0; i < nt->qp_count; i++)
998e26a5843SAllen Hubbe if (qp_bitmap_alloc & BIT_ULL(i)) {
999e26a5843SAllen Hubbe qp = &nt->qp_vec[i];
1000e26a5843SAllen Hubbe ntb_qp_link_cleanup(qp);
1001e26a5843SAllen Hubbe cancel_work_sync(&qp->link_cleanup);
1002e26a5843SAllen Hubbe cancel_delayed_work_sync(&qp->link_work);
1003e26a5843SAllen Hubbe }
1004e26a5843SAllen Hubbe
1005e26a5843SAllen Hubbe if (!nt->link_is_up)
1006fce8a7bbSJon Mason cancel_delayed_work_sync(&nt->link_work);
1007fce8a7bbSJon Mason
10089143595aSJoey Zhang for (i = 0; i < nt->mw_count; i++)
10099143595aSJoey Zhang ntb_free_mw(nt, i);
10109143595aSJoey Zhang
1011fce8a7bbSJon Mason /* The scratchpad registers keep the values if the remote side
1012fce8a7bbSJon Mason * goes down, blast them now to give them a sane value the next
1013fce8a7bbSJon Mason * time they are accessed
1014fce8a7bbSJon Mason */
1015b17faba0SShyam Sundar S K count = ntb_spad_count(nt->ndev);
1016b17faba0SShyam Sundar S K for (i = 0; i < count; i++)
1017e26a5843SAllen Hubbe ntb_spad_write(nt->ndev, i, 0);
1018fce8a7bbSJon Mason }
1019fce8a7bbSJon Mason
ntb_transport_link_cleanup_work(struct work_struct * work)1020fca4d518SJon Mason static void ntb_transport_link_cleanup_work(struct work_struct *work)
1021fca4d518SJon Mason {
1022e26a5843SAllen Hubbe struct ntb_transport_ctx *nt =
1023e26a5843SAllen Hubbe container_of(work, struct ntb_transport_ctx, link_cleanup);
1024fca4d518SJon Mason
1025fca4d518SJon Mason ntb_transport_link_cleanup(nt);
1026fca4d518SJon Mason }
1027fca4d518SJon Mason
ntb_transport_event_callback(void * data)1028e26a5843SAllen Hubbe static void ntb_transport_event_callback(void *data)
1029fce8a7bbSJon Mason {
1030e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = data;
1031fce8a7bbSJon Mason
1032e26a5843SAllen Hubbe if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
1033fce8a7bbSJon Mason schedule_delayed_work(&nt->link_work, 0);
1034e26a5843SAllen Hubbe else
10357b4f2d3cSJon Mason schedule_work(&nt->link_cleanup);
1036fce8a7bbSJon Mason }
1037fce8a7bbSJon Mason
ntb_transport_link_work(struct work_struct * work)1038fce8a7bbSJon Mason static void ntb_transport_link_work(struct work_struct *work)
1039fce8a7bbSJon Mason {
1040e26a5843SAllen Hubbe struct ntb_transport_ctx *nt =
1041e26a5843SAllen Hubbe container_of(work, struct ntb_transport_ctx, link_work.work);
1042e26a5843SAllen Hubbe struct ntb_dev *ndev = nt->ndev;
1043e26a5843SAllen Hubbe struct pci_dev *pdev = ndev->pdev;
1044e26a5843SAllen Hubbe resource_size_t size;
1045fce8a7bbSJon Mason u32 val;
104684f76685SDave Jiang int rc = 0, i, spad;
1047fce8a7bbSJon Mason
1048113fc505SJon Mason /* send the local info, in the opposite order of the way we read it */
10492b0569b3SLogan Gunthorpe
10502b0569b3SLogan Gunthorpe if (nt->use_msi) {
10512b0569b3SLogan Gunthorpe rc = ntb_msi_setup_mws(ndev);
10522b0569b3SLogan Gunthorpe if (rc) {
10532b0569b3SLogan Gunthorpe dev_warn(&pdev->dev,
10542b0569b3SLogan Gunthorpe "Failed to register MSI memory window: %d\n",
10552b0569b3SLogan Gunthorpe rc);
10562b0569b3SLogan Gunthorpe nt->use_msi = false;
10572b0569b3SLogan Gunthorpe }
10582b0569b3SLogan Gunthorpe }
10592b0569b3SLogan Gunthorpe
10602b0569b3SLogan Gunthorpe for (i = 0; i < nt->qp_count; i++)
10612b0569b3SLogan Gunthorpe ntb_transport_setup_qp_msi(nt, i);
10622b0569b3SLogan Gunthorpe
1063e26a5843SAllen Hubbe for (i = 0; i < nt->mw_count; i++) {
1064e26a5843SAllen Hubbe size = nt->mw_vec[i].phys_size;
1065e26a5843SAllen Hubbe
1066e26a5843SAllen Hubbe if (max_mw_size && size > max_mw_size)
1067e26a5843SAllen Hubbe size = max_mw_size;
1068e26a5843SAllen Hubbe
1069e26a5843SAllen Hubbe spad = MW0_SZ_HIGH + (i * 2);
1070d67288a3SSerge Semin ntb_peer_spad_write(ndev, PIDX, spad, upper_32_bits(size));
1071e26a5843SAllen Hubbe
1072e26a5843SAllen Hubbe spad = MW0_SZ_LOW + (i * 2);
1073d67288a3SSerge Semin ntb_peer_spad_write(ndev, PIDX, spad, lower_32_bits(size));
1074fce8a7bbSJon Mason }
1075fce8a7bbSJon Mason
1076d67288a3SSerge Semin ntb_peer_spad_write(ndev, PIDX, NUM_MWS, nt->mw_count);
1077fce8a7bbSJon Mason
1078d67288a3SSerge Semin ntb_peer_spad_write(ndev, PIDX, NUM_QPS, nt->qp_count);
1079fce8a7bbSJon Mason
1080d67288a3SSerge Semin ntb_peer_spad_write(ndev, PIDX, VERSION, NTB_TRANSPORT_VERSION);
1081fce8a7bbSJon Mason
1082fce8a7bbSJon Mason /* Query the remote side for its info */
10830f69a7dfSDave Jiang val = ntb_spad_read(ndev, VERSION);
1084e26a5843SAllen Hubbe dev_dbg(&pdev->dev, "Remote version = %d\n", val);
1085fce8a7bbSJon Mason if (val != NTB_TRANSPORT_VERSION)
1086fce8a7bbSJon Mason goto out;
1087fce8a7bbSJon Mason
10880f69a7dfSDave Jiang val = ntb_spad_read(ndev, NUM_QPS);
1089fce8a7bbSJon Mason dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
1090e26a5843SAllen Hubbe if (val != nt->qp_count)
1091fce8a7bbSJon Mason goto out;
1092fce8a7bbSJon Mason
10930f69a7dfSDave Jiang val = ntb_spad_read(ndev, NUM_MWS);
1094113fc505SJon Mason dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
1095e26a5843SAllen Hubbe if (val != nt->mw_count)
1096e26a5843SAllen Hubbe goto out;
1097fce8a7bbSJon Mason
1098e26a5843SAllen Hubbe for (i = 0; i < nt->mw_count; i++) {
1099113fc505SJon Mason u64 val64;
1100fce8a7bbSJon Mason
11010f69a7dfSDave Jiang val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
1102113fc505SJon Mason val64 = (u64)val << 32;
1103fce8a7bbSJon Mason
11040f69a7dfSDave Jiang val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
1105113fc505SJon Mason val64 |= val;
1106113fc505SJon Mason
1107e26a5843SAllen Hubbe dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
1108113fc505SJon Mason
1109113fc505SJon Mason rc = ntb_set_mw(nt, i, val64);
1110fce8a7bbSJon Mason if (rc)
1111113fc505SJon Mason goto out1;
1112113fc505SJon Mason }
1113fce8a7bbSJon Mason
1114e26a5843SAllen Hubbe nt->link_is_up = true;
1115fce8a7bbSJon Mason
1116e26a5843SAllen Hubbe for (i = 0; i < nt->qp_count; i++) {
1117e26a5843SAllen Hubbe struct ntb_transport_qp *qp = &nt->qp_vec[i];
1118fce8a7bbSJon Mason
1119fce8a7bbSJon Mason ntb_transport_setup_qp_mw(nt, i);
11202b0569b3SLogan Gunthorpe ntb_transport_setup_qp_peer_msi(nt, i);
1121fce8a7bbSJon Mason
1122e26a5843SAllen Hubbe if (qp->client_ready)
1123fce8a7bbSJon Mason schedule_delayed_work(&qp->link_work, 0);
1124fce8a7bbSJon Mason }
1125fce8a7bbSJon Mason
1126fce8a7bbSJon Mason return;
1127fce8a7bbSJon Mason
1128113fc505SJon Mason out1:
1129e26a5843SAllen Hubbe for (i = 0; i < nt->mw_count; i++)
1130113fc505SJon Mason ntb_free_mw(nt, i);
113184f76685SDave Jiang
113284f76685SDave Jiang /* if there's an actual failure, we should just bail */
1133f3fd2afeSDave Jiang if (rc < 0)
113484f76685SDave Jiang return;
113584f76685SDave Jiang
1136fce8a7bbSJon Mason out:
1137e26a5843SAllen Hubbe if (ntb_link_is_up(ndev, NULL, NULL) == 1)
1138fce8a7bbSJon Mason schedule_delayed_work(&nt->link_work,
1139fce8a7bbSJon Mason msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
1140fce8a7bbSJon Mason }
1141fce8a7bbSJon Mason
ntb_qp_link_work(struct work_struct * work)1142fce8a7bbSJon Mason static void ntb_qp_link_work(struct work_struct *work)
1143fce8a7bbSJon Mason {
1144fce8a7bbSJon Mason struct ntb_transport_qp *qp = container_of(work,
1145fce8a7bbSJon Mason struct ntb_transport_qp,
1146fce8a7bbSJon Mason link_work.work);
1147e26a5843SAllen Hubbe struct pci_dev *pdev = qp->ndev->pdev;
1148e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = qp->transport;
1149e26a5843SAllen Hubbe int val;
1150fce8a7bbSJon Mason
1151e26a5843SAllen Hubbe WARN_ON(!nt->link_is_up);
1152fce8a7bbSJon Mason
1153e26a5843SAllen Hubbe val = ntb_spad_read(nt->ndev, QP_LINKS);
1154fce8a7bbSJon Mason
1155d67288a3SSerge Semin ntb_peer_spad_write(nt->ndev, PIDX, QP_LINKS, val | BIT(qp->qp_num));
1156fce8a7bbSJon Mason
1157fce8a7bbSJon Mason /* query remote spad for qp ready bits */
115828762289SAllen Hubbe dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
1159fce8a7bbSJon Mason
1160fce8a7bbSJon Mason /* See if the remote side is up */
1161e26a5843SAllen Hubbe if (val & BIT(qp->qp_num)) {
1162fce8a7bbSJon Mason dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
1163e26a5843SAllen Hubbe qp->link_is_up = true;
1164e9021331SDave Jiang qp->active = true;
1165e26a5843SAllen Hubbe
1166fce8a7bbSJon Mason if (qp->event_handler)
1167e26a5843SAllen Hubbe qp->event_handler(qp->cb_data, qp->link_is_up);
11688b5a22d8SAllen Hubbe
1169e9021331SDave Jiang if (qp->active)
11708b5a22d8SAllen Hubbe tasklet_schedule(&qp->rxc_db_work);
1171e26a5843SAllen Hubbe } else if (nt->link_is_up)
1172fce8a7bbSJon Mason schedule_delayed_work(&qp->link_work,
1173fce8a7bbSJon Mason msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
1174fce8a7bbSJon Mason }
1175fce8a7bbSJon Mason
ntb_transport_init_queue(struct ntb_transport_ctx * nt,unsigned int qp_num)1176e26a5843SAllen Hubbe static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
1177fce8a7bbSJon Mason unsigned int qp_num)
1178fce8a7bbSJon Mason {
1179fce8a7bbSJon Mason struct ntb_transport_qp *qp;
1180e26a5843SAllen Hubbe phys_addr_t mw_base;
1181e26a5843SAllen Hubbe resource_size_t mw_size;
1182ef114ed5SJon Mason unsigned int num_qps_mw, tx_size;
1183e26a5843SAllen Hubbe unsigned int mw_num, mw_count, qp_count;
1184282a2feeSJon Mason u64 qp_offset;
1185948d3a65SJon Mason
1186e26a5843SAllen Hubbe mw_count = nt->mw_count;
1187e26a5843SAllen Hubbe qp_count = nt->qp_count;
1188fce8a7bbSJon Mason
1189e26a5843SAllen Hubbe mw_num = QP_TO_MW(nt, qp_num);
1190e26a5843SAllen Hubbe
1191e26a5843SAllen Hubbe qp = &nt->qp_vec[qp_num];
1192fce8a7bbSJon Mason qp->qp_num = qp_num;
1193fce8a7bbSJon Mason qp->transport = nt;
1194fce8a7bbSJon Mason qp->ndev = nt->ndev;
1195e26a5843SAllen Hubbe qp->client_ready = false;
1196fce8a7bbSJon Mason qp->event_handler = NULL;
1197cc79bd27SDave Jiang ntb_qp_link_context_reset(qp);
1198fce8a7bbSJon Mason
11998e8496e0SLogan Gunthorpe if (mw_num < qp_count % mw_count)
1200e26a5843SAllen Hubbe num_qps_mw = qp_count / mw_count + 1;
1201ef114ed5SJon Mason else
1202e26a5843SAllen Hubbe num_qps_mw = qp_count / mw_count;
1203ef114ed5SJon Mason
1204e26a5843SAllen Hubbe mw_base = nt->mw_vec[mw_num].phys_addr;
1205e26a5843SAllen Hubbe mw_size = nt->mw_vec[mw_num].phys_size;
1206e26a5843SAllen Hubbe
1207cbd27448SLogan Gunthorpe if (max_mw_size && mw_size > max_mw_size)
1208cbd27448SLogan Gunthorpe mw_size = max_mw_size;
1209cbd27448SLogan Gunthorpe
1210e26a5843SAllen Hubbe tx_size = (unsigned int)mw_size / num_qps_mw;
1211c92ba3c5SJon Mason qp_offset = tx_size * (qp_num / mw_count);
1212e26a5843SAllen Hubbe
1213c59666bbSLogan Gunthorpe qp->tx_mw_size = tx_size;
1214e26a5843SAllen Hubbe qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
1215282a2feeSJon Mason if (!qp->tx_mw)
1216282a2feeSJon Mason return -EINVAL;
1217793c20e9SJon Mason
1218e26a5843SAllen Hubbe qp->tx_mw_phys = mw_base + qp_offset;
1219282a2feeSJon Mason if (!qp->tx_mw_phys)
1220282a2feeSJon Mason return -EINVAL;
1221282a2feeSJon Mason
1222282a2feeSJon Mason tx_size -= sizeof(struct ntb_rx_info);
1223282a2feeSJon Mason qp->rx_info = qp->tx_mw + tx_size;
1224282a2feeSJon Mason
1225c9d534c8SJon Mason /* Due to housekeeping, there must be atleast 2 buffs */
1226c9d534c8SJon Mason qp->tx_max_frame = min(transport_mtu, tx_size / 2);
1227793c20e9SJon Mason qp->tx_max_entry = tx_size / qp->tx_max_frame;
1228ef114ed5SJon Mason
1229c8650fd0SDave Jiang if (nt->debugfs_node_dir) {
1230fce8a7bbSJon Mason char debugfs_name[4];
1231fce8a7bbSJon Mason
1232fce8a7bbSJon Mason snprintf(debugfs_name, 4, "qp%d", qp_num);
1233fce8a7bbSJon Mason qp->debugfs_dir = debugfs_create_dir(debugfs_name,
1234c8650fd0SDave Jiang nt->debugfs_node_dir);
1235fce8a7bbSJon Mason
1236fce8a7bbSJon Mason qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1237fce8a7bbSJon Mason qp->debugfs_dir, qp,
1238fce8a7bbSJon Mason &ntb_qp_debugfs_stats);
1239e26a5843SAllen Hubbe } else {
1240e26a5843SAllen Hubbe qp->debugfs_dir = NULL;
1241e26a5843SAllen Hubbe qp->debugfs_stats = NULL;
1242fce8a7bbSJon Mason }
1243fce8a7bbSJon Mason
1244fce8a7bbSJon Mason INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
1245fca4d518SJon Mason INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
1246fce8a7bbSJon Mason
1247da2e5ae5SAllen Hubbe spin_lock_init(&qp->ntb_rx_q_lock);
1248fce8a7bbSJon Mason spin_lock_init(&qp->ntb_tx_free_q_lock);
1249fce8a7bbSJon Mason
1250da2e5ae5SAllen Hubbe INIT_LIST_HEAD(&qp->rx_post_q);
1251fce8a7bbSJon Mason INIT_LIST_HEAD(&qp->rx_pend_q);
1252fce8a7bbSJon Mason INIT_LIST_HEAD(&qp->rx_free_q);
1253fce8a7bbSJon Mason INIT_LIST_HEAD(&qp->tx_free_q);
1254282a2feeSJon Mason
1255e26a5843SAllen Hubbe tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1256e26a5843SAllen Hubbe (unsigned long)qp);
1257e26a5843SAllen Hubbe
1258282a2feeSJon Mason return 0;
1259fce8a7bbSJon Mason }
1260fce8a7bbSJon Mason
ntb_transport_probe(struct ntb_client * self,struct ntb_dev * ndev)1261e26a5843SAllen Hubbe static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
1262fce8a7bbSJon Mason {
1263e26a5843SAllen Hubbe struct ntb_transport_ctx *nt;
1264e26a5843SAllen Hubbe struct ntb_transport_mw *mw;
1265b17faba0SShyam Sundar S K unsigned int mw_count, qp_count, spad_count, max_mw_count_for_spads;
1266e26a5843SAllen Hubbe u64 qp_bitmap;
12671199aa61SAllen Hubbe int node;
1268fce8a7bbSJon Mason int rc, i;
1269fce8a7bbSJon Mason
1270bc240eecSLogan Gunthorpe mw_count = ntb_peer_mw_count(ndev);
1271443b9a14SSerge Semin
1272443b9a14SSerge Semin if (!ndev->ops->mw_set_trans) {
1273443b9a14SSerge Semin dev_err(&ndev->dev, "Inbound MW based NTB API is required\n");
1274443b9a14SSerge Semin return -EINVAL;
1275443b9a14SSerge Semin }
127619645a07SLogan Gunthorpe
1277e26a5843SAllen Hubbe if (ntb_db_is_unsafe(ndev))
1278e26a5843SAllen Hubbe dev_dbg(&ndev->dev,
1279e26a5843SAllen Hubbe "doorbell is unsafe, proceed anyway...\n");
1280e26a5843SAllen Hubbe if (ntb_spad_is_unsafe(ndev))
1281e26a5843SAllen Hubbe dev_dbg(&ndev->dev,
1282e26a5843SAllen Hubbe "scratchpad is unsafe, proceed anyway...\n");
1283e26a5843SAllen Hubbe
12841e530119SSerge Semin if (ntb_peer_port_count(ndev) != NTB_DEF_PEER_CNT)
12851e530119SSerge Semin dev_warn(&ndev->dev, "Multi-port NTB devices unsupported\n");
12861e530119SSerge Semin
12871199aa61SAllen Hubbe node = dev_to_node(&ndev->dev);
12881199aa61SAllen Hubbe
12891199aa61SAllen Hubbe nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
1290fce8a7bbSJon Mason if (!nt)
1291fce8a7bbSJon Mason return -ENOMEM;
1292fce8a7bbSJon Mason
1293e26a5843SAllen Hubbe nt->ndev = ndev;
12942b0569b3SLogan Gunthorpe
12952b0569b3SLogan Gunthorpe /*
12962b0569b3SLogan Gunthorpe * If we are using MSI, and have at least one extra memory window,
12972b0569b3SLogan Gunthorpe * we will reserve the last MW for the MSI window.
12982b0569b3SLogan Gunthorpe */
12992b0569b3SLogan Gunthorpe if (use_msi && mw_count > 1) {
13002b0569b3SLogan Gunthorpe rc = ntb_msi_init(ndev, ntb_transport_msi_desc_changed);
13012b0569b3SLogan Gunthorpe if (!rc) {
13022b0569b3SLogan Gunthorpe mw_count -= 1;
13032b0569b3SLogan Gunthorpe nt->use_msi = true;
13042b0569b3SLogan Gunthorpe }
13052b0569b3SLogan Gunthorpe }
13062b0569b3SLogan Gunthorpe
1307b17faba0SShyam Sundar S K spad_count = ntb_spad_count(ndev);
1308e26a5843SAllen Hubbe
1309b17faba0SShyam Sundar S K /* Limit the MW's based on the availability of scratchpads */
1310b17faba0SShyam Sundar S K
1311b17faba0SShyam Sundar S K if (spad_count < NTB_TRANSPORT_MIN_SPADS) {
1312b17faba0SShyam Sundar S K nt->mw_count = 0;
1313b17faba0SShyam Sundar S K rc = -EINVAL;
1314b17faba0SShyam Sundar S K goto err;
1315b17faba0SShyam Sundar S K }
1316b17faba0SShyam Sundar S K
1317b17faba0SShyam Sundar S K max_mw_count_for_spads = (spad_count - MW0_SZ_HIGH) / 2;
1318b17faba0SShyam Sundar S K nt->mw_count = min(mw_count, max_mw_count_for_spads);
1319e26a5843SAllen Hubbe
13202b0569b3SLogan Gunthorpe nt->msi_spad_offset = nt->mw_count * 2 + MW0_SZ_HIGH;
13212b0569b3SLogan Gunthorpe
1322590b5b7dSKees Cook nt->mw_vec = kcalloc_node(mw_count, sizeof(*nt->mw_vec),
13231199aa61SAllen Hubbe GFP_KERNEL, node);
1324e26a5843SAllen Hubbe if (!nt->mw_vec) {
1325e26a5843SAllen Hubbe rc = -ENOMEM;
1326fce8a7bbSJon Mason goto err;
1327fce8a7bbSJon Mason }
1328fce8a7bbSJon Mason
1329e26a5843SAllen Hubbe for (i = 0; i < mw_count; i++) {
1330e26a5843SAllen Hubbe mw = &nt->mw_vec[i];
1331e26a5843SAllen Hubbe
1332443b9a14SSerge Semin rc = ntb_peer_mw_get_addr(ndev, i, &mw->phys_addr,
1333443b9a14SSerge Semin &mw->phys_size);
1334e26a5843SAllen Hubbe if (rc)
1335e26a5843SAllen Hubbe goto err1;
1336e26a5843SAllen Hubbe
133706917f75SDave Jiang mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
1338e26a5843SAllen Hubbe if (!mw->vbase) {
1339948d3a65SJon Mason rc = -ENOMEM;
1340948d3a65SJon Mason goto err1;
1341948d3a65SJon Mason }
1342948d3a65SJon Mason
1343e26a5843SAllen Hubbe mw->buff_size = 0;
1344e26a5843SAllen Hubbe mw->xlat_size = 0;
1345e26a5843SAllen Hubbe mw->virt_addr = NULL;
1346e26a5843SAllen Hubbe mw->dma_addr = 0;
1347e26a5843SAllen Hubbe }
1348fce8a7bbSJon Mason
1349e26a5843SAllen Hubbe qp_bitmap = ntb_db_valid_mask(ndev);
1350e26a5843SAllen Hubbe
1351e26a5843SAllen Hubbe qp_count = ilog2(qp_bitmap);
13522b0569b3SLogan Gunthorpe if (nt->use_msi) {
13532b0569b3SLogan Gunthorpe qp_count -= 1;
13542b0569b3SLogan Gunthorpe nt->msi_db_mask = 1 << qp_count;
13552b0569b3SLogan Gunthorpe ntb_db_clear_mask(ndev, nt->msi_db_mask);
13562b0569b3SLogan Gunthorpe }
13572b0569b3SLogan Gunthorpe
1358e26a5843SAllen Hubbe if (max_num_clients && max_num_clients < qp_count)
1359e26a5843SAllen Hubbe qp_count = max_num_clients;
1360cb827ee6SLogan Gunthorpe else if (nt->mw_count < qp_count)
1361cb827ee6SLogan Gunthorpe qp_count = nt->mw_count;
1362e26a5843SAllen Hubbe
1363e26a5843SAllen Hubbe qp_bitmap &= BIT_ULL(qp_count) - 1;
1364e26a5843SAllen Hubbe
1365e26a5843SAllen Hubbe nt->qp_count = qp_count;
1366e26a5843SAllen Hubbe nt->qp_bitmap = qp_bitmap;
1367e26a5843SAllen Hubbe nt->qp_bitmap_free = qp_bitmap;
1368e26a5843SAllen Hubbe
1369590b5b7dSKees Cook nt->qp_vec = kcalloc_node(qp_count, sizeof(*nt->qp_vec),
13701199aa61SAllen Hubbe GFP_KERNEL, node);
1371e26a5843SAllen Hubbe if (!nt->qp_vec) {
1372fce8a7bbSJon Mason rc = -ENOMEM;
1373d4adee09SSudip Mukherjee goto err1;
1374fce8a7bbSJon Mason }
1375fce8a7bbSJon Mason
1376c8650fd0SDave Jiang if (nt_debugfs_dir) {
1377c8650fd0SDave Jiang nt->debugfs_node_dir =
1378c8650fd0SDave Jiang debugfs_create_dir(pci_name(ndev->pdev),
1379c8650fd0SDave Jiang nt_debugfs_dir);
1380c8650fd0SDave Jiang }
1381c8650fd0SDave Jiang
1382e26a5843SAllen Hubbe for (i = 0; i < qp_count; i++) {
1383282a2feeSJon Mason rc = ntb_transport_init_queue(nt, i);
1384282a2feeSJon Mason if (rc)
1385d4adee09SSudip Mukherjee goto err2;
1386282a2feeSJon Mason }
1387fce8a7bbSJon Mason
1388fce8a7bbSJon Mason INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
1389fca4d518SJon Mason INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
1390fce8a7bbSJon Mason
1391e26a5843SAllen Hubbe rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
1392fce8a7bbSJon Mason if (rc)
1393d4adee09SSudip Mukherjee goto err2;
1394fce8a7bbSJon Mason
1395fce8a7bbSJon Mason INIT_LIST_HEAD(&nt->client_devs);
1396fce8a7bbSJon Mason rc = ntb_bus_init(nt);
1397fce8a7bbSJon Mason if (rc)
1398d4adee09SSudip Mukherjee goto err3;
1399fce8a7bbSJon Mason
1400e26a5843SAllen Hubbe nt->link_is_up = false;
1401e26a5843SAllen Hubbe ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1402e26a5843SAllen Hubbe ntb_link_event(ndev);
1403fce8a7bbSJon Mason
1404fce8a7bbSJon Mason return 0;
1405fce8a7bbSJon Mason
1406948d3a65SJon Mason err3:
1407d4adee09SSudip Mukherjee ntb_clear_ctx(ndev);
1408948d3a65SJon Mason err2:
1409d4adee09SSudip Mukherjee kfree(nt->qp_vec);
1410fce8a7bbSJon Mason err1:
1411e26a5843SAllen Hubbe while (i--) {
1412e26a5843SAllen Hubbe mw = &nt->mw_vec[i];
1413e26a5843SAllen Hubbe iounmap(mw->vbase);
1414e26a5843SAllen Hubbe }
1415d4adee09SSudip Mukherjee kfree(nt->mw_vec);
1416fce8a7bbSJon Mason err:
1417fce8a7bbSJon Mason kfree(nt);
1418fce8a7bbSJon Mason return rc;
1419fce8a7bbSJon Mason }
1420fce8a7bbSJon Mason
ntb_transport_free(struct ntb_client * self,struct ntb_dev * ndev)1421e26a5843SAllen Hubbe static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
1422fce8a7bbSJon Mason {
1423e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = ndev->ctx;
1424e26a5843SAllen Hubbe struct ntb_transport_qp *qp;
1425e26a5843SAllen Hubbe u64 qp_bitmap_alloc;
1426fce8a7bbSJon Mason int i;
1427fce8a7bbSJon Mason
1428fca4d518SJon Mason ntb_transport_link_cleanup(nt);
1429e26a5843SAllen Hubbe cancel_work_sync(&nt->link_cleanup);
1430e26a5843SAllen Hubbe cancel_delayed_work_sync(&nt->link_work);
1431e26a5843SAllen Hubbe
1432e26a5843SAllen Hubbe qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
1433fce8a7bbSJon Mason
1434fce8a7bbSJon Mason /* verify that all the qp's are freed */
1435e26a5843SAllen Hubbe for (i = 0; i < nt->qp_count; i++) {
1436e26a5843SAllen Hubbe qp = &nt->qp_vec[i];
1437e26a5843SAllen Hubbe if (qp_bitmap_alloc & BIT_ULL(i))
1438e26a5843SAllen Hubbe ntb_transport_free_queue(qp);
1439e26a5843SAllen Hubbe debugfs_remove_recursive(qp->debugfs_dir);
14401517a3f2SJon Mason }
1441fce8a7bbSJon Mason
1442e26a5843SAllen Hubbe ntb_link_disable(ndev);
1443e26a5843SAllen Hubbe ntb_clear_ctx(ndev);
1444e26a5843SAllen Hubbe
1445fce8a7bbSJon Mason ntb_bus_remove(nt);
1446fce8a7bbSJon Mason
1447e26a5843SAllen Hubbe for (i = nt->mw_count; i--; ) {
1448113fc505SJon Mason ntb_free_mw(nt, i);
1449e26a5843SAllen Hubbe iounmap(nt->mw_vec[i].vbase);
1450e26a5843SAllen Hubbe }
1451fce8a7bbSJon Mason
1452e26a5843SAllen Hubbe kfree(nt->qp_vec);
1453e26a5843SAllen Hubbe kfree(nt->mw_vec);
1454fce8a7bbSJon Mason kfree(nt);
1455fce8a7bbSJon Mason }
1456fce8a7bbSJon Mason
ntb_complete_rxc(struct ntb_transport_qp * qp)1457da2e5ae5SAllen Hubbe static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1458fce8a7bbSJon Mason {
1459da2e5ae5SAllen Hubbe struct ntb_queue_entry *entry;
1460da2e5ae5SAllen Hubbe void *cb_data;
1461da2e5ae5SAllen Hubbe unsigned int len;
1462da2e5ae5SAllen Hubbe unsigned long irqflags;
1463448c6fb3SJon Mason
1464da2e5ae5SAllen Hubbe spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1465282a2feeSJon Mason
1466da2e5ae5SAllen Hubbe while (!list_empty(&qp->rx_post_q)) {
1467da2e5ae5SAllen Hubbe entry = list_first_entry(&qp->rx_post_q,
1468da2e5ae5SAllen Hubbe struct ntb_queue_entry, entry);
1469da2e5ae5SAllen Hubbe if (!(entry->flags & DESC_DONE_FLAG))
1470da2e5ae5SAllen Hubbe break;
1471da2e5ae5SAllen Hubbe
1472da2e5ae5SAllen Hubbe entry->rx_hdr->flags = 0;
147372203572SDave Jiang iowrite32(entry->rx_index, &qp->rx_info->entry);
1474fce8a7bbSJon Mason
1475da2e5ae5SAllen Hubbe cb_data = entry->cb_data;
1476da2e5ae5SAllen Hubbe len = entry->len;
1477da2e5ae5SAllen Hubbe
1478da2e5ae5SAllen Hubbe list_move_tail(&entry->entry, &qp->rx_free_q);
1479da2e5ae5SAllen Hubbe
1480da2e5ae5SAllen Hubbe spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1481448c6fb3SJon Mason
1482e26a5843SAllen Hubbe if (qp->rx_handler && qp->client_ready)
1483448c6fb3SJon Mason qp->rx_handler(qp, qp->cb_data, cb_data, len);
1484da2e5ae5SAllen Hubbe
1485da2e5ae5SAllen Hubbe spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1486da2e5ae5SAllen Hubbe }
1487da2e5ae5SAllen Hubbe
1488da2e5ae5SAllen Hubbe spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1489da2e5ae5SAllen Hubbe }
1490da2e5ae5SAllen Hubbe
ntb_rx_copy_callback(void * data,const struct dmaengine_result * res)149172203572SDave Jiang static void ntb_rx_copy_callback(void *data,
149272203572SDave Jiang const struct dmaengine_result *res)
1493da2e5ae5SAllen Hubbe {
1494da2e5ae5SAllen Hubbe struct ntb_queue_entry *entry = data;
1495da2e5ae5SAllen Hubbe
149672203572SDave Jiang /* we need to check DMA results if we are using DMA */
149772203572SDave Jiang if (res) {
149872203572SDave Jiang enum dmaengine_tx_result dma_err = res->result;
149972203572SDave Jiang
150072203572SDave Jiang switch (dma_err) {
150172203572SDave Jiang case DMA_TRANS_READ_FAILED:
150272203572SDave Jiang case DMA_TRANS_WRITE_FAILED:
150372203572SDave Jiang entry->errors++;
1504df561f66SGustavo A. R. Silva fallthrough;
150572203572SDave Jiang case DMA_TRANS_ABORTED:
150672203572SDave Jiang {
150772203572SDave Jiang struct ntb_transport_qp *qp = entry->qp;
150872203572SDave Jiang void *offset = qp->rx_buff + qp->rx_max_frame *
150972203572SDave Jiang qp->rx_index;
151072203572SDave Jiang
151172203572SDave Jiang ntb_memcpy_rx(entry, offset);
151272203572SDave Jiang qp->rx_memcpy++;
151372203572SDave Jiang return;
151472203572SDave Jiang }
151572203572SDave Jiang
151672203572SDave Jiang case DMA_TRANS_NOERROR:
151772203572SDave Jiang default:
151872203572SDave Jiang break;
151972203572SDave Jiang }
152072203572SDave Jiang }
152172203572SDave Jiang
1522da2e5ae5SAllen Hubbe entry->flags |= DESC_DONE_FLAG;
1523da2e5ae5SAllen Hubbe
1524da2e5ae5SAllen Hubbe ntb_complete_rxc(entry->qp);
1525fce8a7bbSJon Mason }
1526fce8a7bbSJon Mason
ntb_memcpy_rx(struct ntb_queue_entry * entry,void * offset)1527282a2feeSJon Mason static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1528282a2feeSJon Mason {
1529282a2feeSJon Mason void *buf = entry->buf;
1530282a2feeSJon Mason size_t len = entry->len;
1531282a2feeSJon Mason
1532282a2feeSJon Mason memcpy(buf, offset, len);
1533282a2feeSJon Mason
1534e26a5843SAllen Hubbe /* Ensure that the data is fully copied out before clearing the flag */
1535e26a5843SAllen Hubbe wmb();
1536e26a5843SAllen Hubbe
153772203572SDave Jiang ntb_rx_copy_callback(entry, NULL);
1538282a2feeSJon Mason }
1539282a2feeSJon Mason
ntb_async_rx_submit(struct ntb_queue_entry * entry,void * offset)154072203572SDave Jiang static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
1541282a2feeSJon Mason {
1542282a2feeSJon Mason struct dma_async_tx_descriptor *txd;
1543282a2feeSJon Mason struct ntb_transport_qp *qp = entry->qp;
1544569410caSDave Jiang struct dma_chan *chan = qp->rx_dma_chan;
1545282a2feeSJon Mason struct dma_device *device;
1546da2e5ae5SAllen Hubbe size_t pay_off, buff_off, len;
15476f57fd05SBartlomiej Zolnierkiewicz struct dmaengine_unmap_data *unmap;
1548282a2feeSJon Mason dma_cookie_t cookie;
1549282a2feeSJon Mason void *buf = entry->buf;
1550282a2feeSJon Mason
1551da2e5ae5SAllen Hubbe len = entry->len;
1552282a2feeSJon Mason device = chan->device;
1553282a2feeSJon Mason pay_off = (size_t)offset & ~PAGE_MASK;
1554282a2feeSJon Mason buff_off = (size_t)buf & ~PAGE_MASK;
1555282a2feeSJon Mason
1556282a2feeSJon Mason if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
1557905921e7SAllen Hubbe goto err;
1558282a2feeSJon Mason
15596f57fd05SBartlomiej Zolnierkiewicz unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
15606f57fd05SBartlomiej Zolnierkiewicz if (!unmap)
1561905921e7SAllen Hubbe goto err;
1562282a2feeSJon Mason
15636f57fd05SBartlomiej Zolnierkiewicz unmap->len = len;
15646f57fd05SBartlomiej Zolnierkiewicz unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
15656f57fd05SBartlomiej Zolnierkiewicz pay_off, len, DMA_TO_DEVICE);
15666f57fd05SBartlomiej Zolnierkiewicz if (dma_mapping_error(device->dev, unmap->addr[0]))
15676f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap;
1568282a2feeSJon Mason
15696f57fd05SBartlomiej Zolnierkiewicz unmap->to_cnt = 1;
15706f57fd05SBartlomiej Zolnierkiewicz
15716f57fd05SBartlomiej Zolnierkiewicz unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
15726f57fd05SBartlomiej Zolnierkiewicz buff_off, len, DMA_FROM_DEVICE);
15736f57fd05SBartlomiej Zolnierkiewicz if (dma_mapping_error(device->dev, unmap->addr[1]))
15746f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap;
15756f57fd05SBartlomiej Zolnierkiewicz
15766f57fd05SBartlomiej Zolnierkiewicz unmap->from_cnt = 1;
15776f57fd05SBartlomiej Zolnierkiewicz
157888931ec3SAllen Hubbe txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
15790776ae7bSBartlomiej Zolnierkiewicz unmap->addr[0], len,
15800776ae7bSBartlomiej Zolnierkiewicz DMA_PREP_INTERRUPT);
158188931ec3SAllen Hubbe if (!txd)
15826f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap;
1583282a2feeSJon Mason
158472203572SDave Jiang txd->callback_result = ntb_rx_copy_callback;
1585282a2feeSJon Mason txd->callback_param = entry;
15866f57fd05SBartlomiej Zolnierkiewicz dma_set_unmap(txd, unmap);
1587282a2feeSJon Mason
1588282a2feeSJon Mason cookie = dmaengine_submit(txd);
1589282a2feeSJon Mason if (dma_submit_error(cookie))
15906f57fd05SBartlomiej Zolnierkiewicz goto err_set_unmap;
15916f57fd05SBartlomiej Zolnierkiewicz
15926f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap);
1593282a2feeSJon Mason
1594282a2feeSJon Mason qp->last_cookie = cookie;
1595282a2feeSJon Mason
1596282a2feeSJon Mason qp->rx_async++;
1597282a2feeSJon Mason
159872203572SDave Jiang return 0;
1599282a2feeSJon Mason
16006f57fd05SBartlomiej Zolnierkiewicz err_set_unmap:
16016f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap);
16026f57fd05SBartlomiej Zolnierkiewicz err_get_unmap:
16036f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap);
1604282a2feeSJon Mason err:
160572203572SDave Jiang return -ENXIO;
160672203572SDave Jiang }
160772203572SDave Jiang
ntb_async_rx(struct ntb_queue_entry * entry,void * offset)160872203572SDave Jiang static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
160972203572SDave Jiang {
161072203572SDave Jiang struct ntb_transport_qp *qp = entry->qp;
161172203572SDave Jiang struct dma_chan *chan = qp->rx_dma_chan;
161272203572SDave Jiang int res;
161372203572SDave Jiang
161472203572SDave Jiang if (!chan)
161572203572SDave Jiang goto err;
161672203572SDave Jiang
161772203572SDave Jiang if (entry->len < copy_bytes)
161872203572SDave Jiang goto err;
161972203572SDave Jiang
162072203572SDave Jiang res = ntb_async_rx_submit(entry, offset);
162172203572SDave Jiang if (res < 0)
162272203572SDave Jiang goto err;
162372203572SDave Jiang
162472203572SDave Jiang if (!entry->retries)
162572203572SDave Jiang qp->rx_async++;
162672203572SDave Jiang
162772203572SDave Jiang return;
162872203572SDave Jiang
162972203572SDave Jiang err:
1630282a2feeSJon Mason ntb_memcpy_rx(entry, offset);
1631282a2feeSJon Mason qp->rx_memcpy++;
1632282a2feeSJon Mason }
1633282a2feeSJon Mason
ntb_process_rxc(struct ntb_transport_qp * qp)1634fce8a7bbSJon Mason static int ntb_process_rxc(struct ntb_transport_qp *qp)
1635fce8a7bbSJon Mason {
1636fce8a7bbSJon Mason struct ntb_payload_header *hdr;
1637fce8a7bbSJon Mason struct ntb_queue_entry *entry;
1638fce8a7bbSJon Mason void *offset;
1639fce8a7bbSJon Mason
1640793c20e9SJon Mason offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1641793c20e9SJon Mason hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1642793c20e9SJon Mason
1643e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1644e26a5843SAllen Hubbe qp->qp_num, hdr->ver, hdr->len, hdr->flags);
1645fce8a7bbSJon Mason
1646fce8a7bbSJon Mason if (!(hdr->flags & DESC_DONE_FLAG)) {
1647e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
1648fce8a7bbSJon Mason qp->rx_ring_empty++;
1649fce8a7bbSJon Mason return -EAGAIN;
1650fce8a7bbSJon Mason }
1651fce8a7bbSJon Mason
1652e26a5843SAllen Hubbe if (hdr->flags & LINK_DOWN_FLAG) {
1653e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1654e26a5843SAllen Hubbe ntb_qp_link_down(qp);
1655e26a5843SAllen Hubbe hdr->flags = 0;
1656c0900b33SAllen Hubbe return -EAGAIN;
1657e26a5843SAllen Hubbe }
1658e26a5843SAllen Hubbe
165974465645SJon Mason if (hdr->ver != (u32)qp->rx_pkts) {
1660e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev,
1661e26a5843SAllen Hubbe "version mismatch, expected %llu - got %u\n",
1662e26a5843SAllen Hubbe qp->rx_pkts, hdr->ver);
1663fce8a7bbSJon Mason qp->rx_err_ver++;
1664fce8a7bbSJon Mason return -EIO;
1665fce8a7bbSJon Mason }
1666fce8a7bbSJon Mason
1667da2e5ae5SAllen Hubbe entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
1668e26a5843SAllen Hubbe if (!entry) {
1669e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1670e26a5843SAllen Hubbe qp->rx_err_no_buf++;
1671da2e5ae5SAllen Hubbe return -EAGAIN;
1672fce8a7bbSJon Mason }
1673fce8a7bbSJon Mason
1674da2e5ae5SAllen Hubbe entry->rx_hdr = hdr;
167572203572SDave Jiang entry->rx_index = qp->rx_index;
1676da2e5ae5SAllen Hubbe
1677e26a5843SAllen Hubbe if (hdr->len > entry->len) {
1678e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev,
1679e26a5843SAllen Hubbe "receive buffer overflow! Wanted %d got %d\n",
1680e26a5843SAllen Hubbe hdr->len, entry->len);
1681e26a5843SAllen Hubbe qp->rx_err_oflow++;
1682e26a5843SAllen Hubbe
1683da2e5ae5SAllen Hubbe entry->len = -EIO;
1684da2e5ae5SAllen Hubbe entry->flags |= DESC_DONE_FLAG;
1685e26a5843SAllen Hubbe
1686da2e5ae5SAllen Hubbe ntb_complete_rxc(qp);
1687da2e5ae5SAllen Hubbe } else {
1688e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev,
1689e26a5843SAllen Hubbe "RX OK index %u ver %u size %d into buf size %d\n",
1690793c20e9SJon Mason qp->rx_index, hdr->ver, hdr->len, entry->len);
1691fce8a7bbSJon Mason
1692282a2feeSJon Mason qp->rx_bytes += hdr->len;
1693282a2feeSJon Mason qp->rx_pkts++;
1694fce8a7bbSJon Mason
1695da2e5ae5SAllen Hubbe entry->len = hdr->len;
1696282a2feeSJon Mason
1697da2e5ae5SAllen Hubbe ntb_async_rx(entry, offset);
1698da2e5ae5SAllen Hubbe }
1699fce8a7bbSJon Mason
1700282a2feeSJon Mason qp->rx_index++;
1701282a2feeSJon Mason qp->rx_index %= qp->rx_max_entry;
1702282a2feeSJon Mason
1703282a2feeSJon Mason return 0;
1704fce8a7bbSJon Mason }
1705fce8a7bbSJon Mason
ntb_transport_rxc_db(unsigned long data)1706e26a5843SAllen Hubbe static void ntb_transport_rxc_db(unsigned long data)
1707fce8a7bbSJon Mason {
1708e26a5843SAllen Hubbe struct ntb_transport_qp *qp = (void *)data;
1709c336acd3SJon Mason int rc, i;
1710fce8a7bbSJon Mason
1711e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1712e26a5843SAllen Hubbe __func__, qp->qp_num);
1713e8aeb60cSJon Mason
1714c336acd3SJon Mason /* Limit the number of packets processed in a single interrupt to
1715c336acd3SJon Mason * provide fairness to others
1716c336acd3SJon Mason */
1717c336acd3SJon Mason for (i = 0; i < qp->rx_max_entry; i++) {
1718fce8a7bbSJon Mason rc = ntb_process_rxc(qp);
1719c336acd3SJon Mason if (rc)
1720c336acd3SJon Mason break;
1721c336acd3SJon Mason }
1722282a2feeSJon Mason
1723569410caSDave Jiang if (i && qp->rx_dma_chan)
1724569410caSDave Jiang dma_async_issue_pending(qp->rx_dma_chan);
1725fce8a7bbSJon Mason
1726e26a5843SAllen Hubbe if (i == qp->rx_max_entry) {
1727e26a5843SAllen Hubbe /* there is more work to do */
1728e9021331SDave Jiang if (qp->active)
1729e26a5843SAllen Hubbe tasklet_schedule(&qp->rxc_db_work);
1730e26a5843SAllen Hubbe } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1731e26a5843SAllen Hubbe /* the doorbell bit is set: clear it */
1732e26a5843SAllen Hubbe ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1733e26a5843SAllen Hubbe /* ntb_db_read ensures ntb_db_clear write is committed */
1734e26a5843SAllen Hubbe ntb_db_read(qp->ndev);
1735e26a5843SAllen Hubbe
1736e26a5843SAllen Hubbe /* an interrupt may have arrived between finishing
1737e26a5843SAllen Hubbe * ntb_process_rxc and clearing the doorbell bit:
1738e26a5843SAllen Hubbe * there might be some more work to do.
1739e26a5843SAllen Hubbe */
1740e9021331SDave Jiang if (qp->active)
1741e26a5843SAllen Hubbe tasklet_schedule(&qp->rxc_db_work);
1742e26a5843SAllen Hubbe }
1743fce8a7bbSJon Mason }
1744fce8a7bbSJon Mason
ntb_tx_copy_callback(void * data,const struct dmaengine_result * res)17459cabc269SDave Jiang static void ntb_tx_copy_callback(void *data,
17469cabc269SDave Jiang const struct dmaengine_result *res)
1747fce8a7bbSJon Mason {
1748282a2feeSJon Mason struct ntb_queue_entry *entry = data;
1749282a2feeSJon Mason struct ntb_transport_qp *qp = entry->qp;
1750282a2feeSJon Mason struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
1751fce8a7bbSJon Mason
17529cabc269SDave Jiang /* we need to check DMA results if we are using DMA */
17539cabc269SDave Jiang if (res) {
17549cabc269SDave Jiang enum dmaengine_tx_result dma_err = res->result;
17559cabc269SDave Jiang
17569cabc269SDave Jiang switch (dma_err) {
17579cabc269SDave Jiang case DMA_TRANS_READ_FAILED:
17589cabc269SDave Jiang case DMA_TRANS_WRITE_FAILED:
17599cabc269SDave Jiang entry->errors++;
1760df561f66SGustavo A. R. Silva fallthrough;
17619cabc269SDave Jiang case DMA_TRANS_ABORTED:
17629cabc269SDave Jiang {
17639cabc269SDave Jiang void __iomem *offset =
17649cabc269SDave Jiang qp->tx_mw + qp->tx_max_frame *
17659cabc269SDave Jiang entry->tx_index;
17669cabc269SDave Jiang
17679cabc269SDave Jiang /* resubmit via CPU */
17689cabc269SDave Jiang ntb_memcpy_tx(entry, offset);
17699cabc269SDave Jiang qp->tx_memcpy++;
17709cabc269SDave Jiang return;
17719cabc269SDave Jiang }
17729cabc269SDave Jiang
17739cabc269SDave Jiang case DMA_TRANS_NOERROR:
17749cabc269SDave Jiang default:
17759cabc269SDave Jiang break;
17769cabc269SDave Jiang }
17779cabc269SDave Jiang }
17789cabc269SDave Jiang
177974465645SJon Mason iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
1780fce8a7bbSJon Mason
17812b0569b3SLogan Gunthorpe if (qp->use_msi)
17822b0569b3SLogan Gunthorpe ntb_msi_peer_trigger(qp->ndev, PIDX, &qp->peer_msi_desc);
17832b0569b3SLogan Gunthorpe else
1784e26a5843SAllen Hubbe ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
1785fce8a7bbSJon Mason
1786fce8a7bbSJon Mason /* The entry length can only be zero if the packet is intended to be a
1787fce8a7bbSJon Mason * "link down" or similar. Since no payload is being sent in these
1788fce8a7bbSJon Mason * cases, there is nothing to add to the completion queue.
1789fce8a7bbSJon Mason */
1790fce8a7bbSJon Mason if (entry->len > 0) {
1791fce8a7bbSJon Mason qp->tx_bytes += entry->len;
1792fce8a7bbSJon Mason
1793fce8a7bbSJon Mason if (qp->tx_handler)
1794fce8a7bbSJon Mason qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1795fce8a7bbSJon Mason entry->len);
1796fce8a7bbSJon Mason }
1797fce8a7bbSJon Mason
1798fce8a7bbSJon Mason ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1799fce8a7bbSJon Mason }
1800fce8a7bbSJon Mason
ntb_memcpy_tx(struct ntb_queue_entry * entry,void __iomem * offset)1801282a2feeSJon Mason static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1802282a2feeSJon Mason {
180306917f75SDave Jiang #ifdef ARCH_HAS_NOCACHE_UACCESS
180406917f75SDave Jiang /*
180506917f75SDave Jiang * Using non-temporal mov to improve performance on non-cached
180606917f75SDave Jiang * writes, even though we aren't actually copying from user space.
180706917f75SDave Jiang */
180806917f75SDave Jiang __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
180906917f75SDave Jiang #else
1810282a2feeSJon Mason memcpy_toio(offset, entry->buf, entry->len);
181106917f75SDave Jiang #endif
1812282a2feeSJon Mason
1813e26a5843SAllen Hubbe /* Ensure that the data is fully copied out before setting the flags */
1814e26a5843SAllen Hubbe wmb();
1815e26a5843SAllen Hubbe
18169cabc269SDave Jiang ntb_tx_copy_callback(entry, NULL);
1817282a2feeSJon Mason }
1818282a2feeSJon Mason
ntb_async_tx_submit(struct ntb_transport_qp * qp,struct ntb_queue_entry * entry)18199cabc269SDave Jiang static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1820282a2feeSJon Mason struct ntb_queue_entry *entry)
1821282a2feeSJon Mason {
1822282a2feeSJon Mason struct dma_async_tx_descriptor *txd;
1823569410caSDave Jiang struct dma_chan *chan = qp->tx_dma_chan;
1824282a2feeSJon Mason struct dma_device *device;
18259cabc269SDave Jiang size_t len = entry->len;
18269cabc269SDave Jiang void *buf = entry->buf;
1827282a2feeSJon Mason size_t dest_off, buff_off;
18286f57fd05SBartlomiej Zolnierkiewicz struct dmaengine_unmap_data *unmap;
18296f57fd05SBartlomiej Zolnierkiewicz dma_addr_t dest;
1830282a2feeSJon Mason dma_cookie_t cookie;
1831282a2feeSJon Mason
1832282a2feeSJon Mason device = chan->device;
1833c59666bbSLogan Gunthorpe dest = qp->tx_mw_dma_addr + qp->tx_max_frame * entry->tx_index;
1834282a2feeSJon Mason buff_off = (size_t)buf & ~PAGE_MASK;
1835282a2feeSJon Mason dest_off = (size_t)dest & ~PAGE_MASK;
1836282a2feeSJon Mason
1837282a2feeSJon Mason if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1838282a2feeSJon Mason goto err;
1839282a2feeSJon Mason
18406f57fd05SBartlomiej Zolnierkiewicz unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
18416f57fd05SBartlomiej Zolnierkiewicz if (!unmap)
1842282a2feeSJon Mason goto err;
1843282a2feeSJon Mason
18446f57fd05SBartlomiej Zolnierkiewicz unmap->len = len;
18456f57fd05SBartlomiej Zolnierkiewicz unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
18466f57fd05SBartlomiej Zolnierkiewicz buff_off, len, DMA_TO_DEVICE);
18476f57fd05SBartlomiej Zolnierkiewicz if (dma_mapping_error(device->dev, unmap->addr[0]))
18486f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap;
18496f57fd05SBartlomiej Zolnierkiewicz
18506f57fd05SBartlomiej Zolnierkiewicz unmap->to_cnt = 1;
18516f57fd05SBartlomiej Zolnierkiewicz
185288931ec3SAllen Hubbe txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
18539cabc269SDave Jiang DMA_PREP_INTERRUPT);
185488931ec3SAllen Hubbe if (!txd)
18556f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap;
1856282a2feeSJon Mason
18579cabc269SDave Jiang txd->callback_result = ntb_tx_copy_callback;
1858282a2feeSJon Mason txd->callback_param = entry;
18596f57fd05SBartlomiej Zolnierkiewicz dma_set_unmap(txd, unmap);
1860282a2feeSJon Mason
1861282a2feeSJon Mason cookie = dmaengine_submit(txd);
1862282a2feeSJon Mason if (dma_submit_error(cookie))
18636f57fd05SBartlomiej Zolnierkiewicz goto err_set_unmap;
18646f57fd05SBartlomiej Zolnierkiewicz
18656f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap);
1866282a2feeSJon Mason
1867282a2feeSJon Mason dma_async_issue_pending(chan);
1868282a2feeSJon Mason
18699cabc269SDave Jiang return 0;
18706f57fd05SBartlomiej Zolnierkiewicz err_set_unmap:
18716f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap);
18726f57fd05SBartlomiej Zolnierkiewicz err_get_unmap:
18736f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap);
1874282a2feeSJon Mason err:
18759cabc269SDave Jiang return -ENXIO;
18769cabc269SDave Jiang }
18779cabc269SDave Jiang
ntb_async_tx(struct ntb_transport_qp * qp,struct ntb_queue_entry * entry)18789cabc269SDave Jiang static void ntb_async_tx(struct ntb_transport_qp *qp,
18799cabc269SDave Jiang struct ntb_queue_entry *entry)
18809cabc269SDave Jiang {
18819cabc269SDave Jiang struct ntb_payload_header __iomem *hdr;
18829cabc269SDave Jiang struct dma_chan *chan = qp->tx_dma_chan;
18839cabc269SDave Jiang void __iomem *offset;
18849cabc269SDave Jiang int res;
18859cabc269SDave Jiang
18869cabc269SDave Jiang entry->tx_index = qp->tx_index;
18879cabc269SDave Jiang offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
18889cabc269SDave Jiang hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
18899cabc269SDave Jiang entry->tx_hdr = hdr;
18909cabc269SDave Jiang
18919cabc269SDave Jiang iowrite32(entry->len, &hdr->len);
18929cabc269SDave Jiang iowrite32((u32)qp->tx_pkts, &hdr->ver);
18939cabc269SDave Jiang
18949cabc269SDave Jiang if (!chan)
18959cabc269SDave Jiang goto err;
18969cabc269SDave Jiang
18979cabc269SDave Jiang if (entry->len < copy_bytes)
18989cabc269SDave Jiang goto err;
18999cabc269SDave Jiang
19009cabc269SDave Jiang res = ntb_async_tx_submit(qp, entry);
19019cabc269SDave Jiang if (res < 0)
19029cabc269SDave Jiang goto err;
19039cabc269SDave Jiang
19049cabc269SDave Jiang if (!entry->retries)
19059cabc269SDave Jiang qp->tx_async++;
19069cabc269SDave Jiang
19079cabc269SDave Jiang return;
19089cabc269SDave Jiang
19099cabc269SDave Jiang err:
1910282a2feeSJon Mason ntb_memcpy_tx(entry, offset);
1911282a2feeSJon Mason qp->tx_memcpy++;
1912282a2feeSJon Mason }
1913282a2feeSJon Mason
ntb_process_tx(struct ntb_transport_qp * qp,struct ntb_queue_entry * entry)1914fce8a7bbSJon Mason static int ntb_process_tx(struct ntb_transport_qp *qp,
1915fce8a7bbSJon Mason struct ntb_queue_entry *entry)
1916fce8a7bbSJon Mason {
191764398223SDave Jiang if (!ntb_transport_tx_free_entry(qp)) {
1918fce8a7bbSJon Mason qp->tx_ring_full++;
1919fce8a7bbSJon Mason return -EAGAIN;
1920fce8a7bbSJon Mason }
1921fce8a7bbSJon Mason
1922ef114ed5SJon Mason if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
1923fce8a7bbSJon Mason if (qp->tx_handler)
1924179f912aSJon Mason qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
1925fce8a7bbSJon Mason
1926fce8a7bbSJon Mason ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1927fce8a7bbSJon Mason &qp->tx_free_q);
1928fce8a7bbSJon Mason return 0;
1929fce8a7bbSJon Mason }
1930fce8a7bbSJon Mason
1931282a2feeSJon Mason ntb_async_tx(qp, entry);
1932fce8a7bbSJon Mason
1933793c20e9SJon Mason qp->tx_index++;
1934793c20e9SJon Mason qp->tx_index %= qp->tx_max_entry;
1935fce8a7bbSJon Mason
1936fce8a7bbSJon Mason qp->tx_pkts++;
1937fce8a7bbSJon Mason
1938fce8a7bbSJon Mason return 0;
1939fce8a7bbSJon Mason }
1940fce8a7bbSJon Mason
ntb_send_link_down(struct ntb_transport_qp * qp)1941fce8a7bbSJon Mason static void ntb_send_link_down(struct ntb_transport_qp *qp)
1942fce8a7bbSJon Mason {
1943e26a5843SAllen Hubbe struct pci_dev *pdev = qp->ndev->pdev;
1944fce8a7bbSJon Mason struct ntb_queue_entry *entry;
1945fce8a7bbSJon Mason int i, rc;
1946fce8a7bbSJon Mason
1947e26a5843SAllen Hubbe if (!qp->link_is_up)
1948fce8a7bbSJon Mason return;
1949fce8a7bbSJon Mason
1950e22e0b9dSAllen Hubbe dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
1951fce8a7bbSJon Mason
1952fce8a7bbSJon Mason for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1953f766755cSJon Mason entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1954fce8a7bbSJon Mason if (entry)
1955fce8a7bbSJon Mason break;
1956fce8a7bbSJon Mason msleep(100);
1957fce8a7bbSJon Mason }
1958fce8a7bbSJon Mason
1959fce8a7bbSJon Mason if (!entry)
1960fce8a7bbSJon Mason return;
1961fce8a7bbSJon Mason
1962fce8a7bbSJon Mason entry->cb_data = NULL;
1963fce8a7bbSJon Mason entry->buf = NULL;
1964fce8a7bbSJon Mason entry->len = 0;
1965fce8a7bbSJon Mason entry->flags = LINK_DOWN_FLAG;
1966fce8a7bbSJon Mason
1967fce8a7bbSJon Mason rc = ntb_process_tx(qp, entry);
1968fce8a7bbSJon Mason if (rc)
1969fce8a7bbSJon Mason dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1970fce8a7bbSJon Mason qp->qp_num);
19712849b5d7SAllen Hubbe
19722849b5d7SAllen Hubbe ntb_qp_link_down_reset(qp);
1973fce8a7bbSJon Mason }
1974fce8a7bbSJon Mason
ntb_dma_filter_fn(struct dma_chan * chan,void * node)19751199aa61SAllen Hubbe static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
19761199aa61SAllen Hubbe {
19771199aa61SAllen Hubbe return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
19781199aa61SAllen Hubbe }
19791199aa61SAllen Hubbe
1980fce8a7bbSJon Mason /**
1981fce8a7bbSJon Mason * ntb_transport_create_queue - Create a new NTB transport layer queue
1982fce8a7bbSJon Mason * @rx_handler: receive callback function
1983fce8a7bbSJon Mason * @tx_handler: transmit callback function
1984fce8a7bbSJon Mason * @event_handler: event callback function
1985fce8a7bbSJon Mason *
1986fce8a7bbSJon Mason * Create a new NTB transport layer queue and provide the queue with a callback
1987fce8a7bbSJon Mason * routine for both transmit and receive. The receive callback routine will be
1988fce8a7bbSJon Mason * used to pass up data when the transport has received it on the queue. The
1989fce8a7bbSJon Mason * transmit callback routine will be called when the transport has completed the
1990fce8a7bbSJon Mason * transmission of the data on the queue and the data is ready to be freed.
1991fce8a7bbSJon Mason *
1992fce8a7bbSJon Mason * RETURNS: pointer to newly created ntb_queue, NULL on error.
1993fce8a7bbSJon Mason */
1994fce8a7bbSJon Mason struct ntb_transport_qp *
ntb_transport_create_queue(void * data,struct device * client_dev,const struct ntb_queue_handlers * handlers)1995e26a5843SAllen Hubbe ntb_transport_create_queue(void *data, struct device *client_dev,
1996fce8a7bbSJon Mason const struct ntb_queue_handlers *handlers)
1997fce8a7bbSJon Mason {
1998e26a5843SAllen Hubbe struct ntb_dev *ndev;
1999e26a5843SAllen Hubbe struct pci_dev *pdev;
2000e26a5843SAllen Hubbe struct ntb_transport_ctx *nt;
2001fce8a7bbSJon Mason struct ntb_queue_entry *entry;
2002fce8a7bbSJon Mason struct ntb_transport_qp *qp;
2003e26a5843SAllen Hubbe u64 qp_bit;
2004fce8a7bbSJon Mason unsigned int free_queue;
20051199aa61SAllen Hubbe dma_cap_mask_t dma_mask;
20061199aa61SAllen Hubbe int node;
2007e26a5843SAllen Hubbe int i;
2008fce8a7bbSJon Mason
2009e26a5843SAllen Hubbe ndev = dev_ntb(client_dev->parent);
2010e26a5843SAllen Hubbe pdev = ndev->pdev;
2011e26a5843SAllen Hubbe nt = ndev->ctx;
2012fce8a7bbSJon Mason
20131199aa61SAllen Hubbe node = dev_to_node(&ndev->dev);
20141199aa61SAllen Hubbe
20158fcd0950SThomas VanSelus free_queue = ffs(nt->qp_bitmap_free);
2016fce8a7bbSJon Mason if (!free_queue)
2017fce8a7bbSJon Mason goto err;
2018fce8a7bbSJon Mason
2019fce8a7bbSJon Mason /* decrement free_queue to make it zero based */
2020fce8a7bbSJon Mason free_queue--;
2021fce8a7bbSJon Mason
2022e26a5843SAllen Hubbe qp = &nt->qp_vec[free_queue];
2023e26a5843SAllen Hubbe qp_bit = BIT_ULL(qp->qp_num);
2024fce8a7bbSJon Mason
2025e26a5843SAllen Hubbe nt->qp_bitmap_free &= ~qp_bit;
2026e26a5843SAllen Hubbe
2027fce8a7bbSJon Mason qp->cb_data = data;
2028fce8a7bbSJon Mason qp->rx_handler = handlers->rx_handler;
2029fce8a7bbSJon Mason qp->tx_handler = handlers->tx_handler;
2030fce8a7bbSJon Mason qp->event_handler = handlers->event_handler;
2031fce8a7bbSJon Mason
20321199aa61SAllen Hubbe dma_cap_zero(dma_mask);
20331199aa61SAllen Hubbe dma_cap_set(DMA_MEMCPY, dma_mask);
20341199aa61SAllen Hubbe
2035a41ef053SDave Jiang if (use_dma) {
2036569410caSDave Jiang qp->tx_dma_chan =
2037569410caSDave Jiang dma_request_channel(dma_mask, ntb_dma_filter_fn,
20381199aa61SAllen Hubbe (void *)(unsigned long)node);
2039569410caSDave Jiang if (!qp->tx_dma_chan)
2040569410caSDave Jiang dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
2041569410caSDave Jiang
2042569410caSDave Jiang qp->rx_dma_chan =
2043569410caSDave Jiang dma_request_channel(dma_mask, ntb_dma_filter_fn,
2044569410caSDave Jiang (void *)(unsigned long)node);
2045569410caSDave Jiang if (!qp->rx_dma_chan)
2046569410caSDave Jiang dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
2047a41ef053SDave Jiang } else {
2048569410caSDave Jiang qp->tx_dma_chan = NULL;
2049569410caSDave Jiang qp->rx_dma_chan = NULL;
2050a41ef053SDave Jiang }
2051569410caSDave Jiang
205251cb8dbfSLogan Gunthorpe qp->tx_mw_dma_addr = 0;
2053c59666bbSLogan Gunthorpe if (qp->tx_dma_chan) {
2054c59666bbSLogan Gunthorpe qp->tx_mw_dma_addr =
2055c59666bbSLogan Gunthorpe dma_map_resource(qp->tx_dma_chan->device->dev,
2056c59666bbSLogan Gunthorpe qp->tx_mw_phys, qp->tx_mw_size,
2057c59666bbSLogan Gunthorpe DMA_FROM_DEVICE, 0);
2058c59666bbSLogan Gunthorpe if (dma_mapping_error(qp->tx_dma_chan->device->dev,
2059c59666bbSLogan Gunthorpe qp->tx_mw_dma_addr)) {
2060c59666bbSLogan Gunthorpe qp->tx_mw_dma_addr = 0;
2061c59666bbSLogan Gunthorpe goto err1;
2062c59666bbSLogan Gunthorpe }
2063c59666bbSLogan Gunthorpe }
2064c59666bbSLogan Gunthorpe
2065569410caSDave Jiang dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
2066569410caSDave Jiang qp->tx_dma_chan ? "DMA" : "CPU");
2067569410caSDave Jiang
2068569410caSDave Jiang dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
2069569410caSDave Jiang qp->rx_dma_chan ? "DMA" : "CPU");
2070282a2feeSJon Mason
2071fce8a7bbSJon Mason for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
2072c9160b69SJia-Ju Bai entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
2073fce8a7bbSJon Mason if (!entry)
2074fce8a7bbSJon Mason goto err1;
2075fce8a7bbSJon Mason
2076282a2feeSJon Mason entry->qp = qp;
2077da2e5ae5SAllen Hubbe ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
2078fce8a7bbSJon Mason &qp->rx_free_q);
2079fce8a7bbSJon Mason }
2080a754a8fcSDave Jiang qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
2081fce8a7bbSJon Mason
2082a754a8fcSDave Jiang for (i = 0; i < qp->tx_max_entry; i++) {
2083c9160b69SJia-Ju Bai entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
2084fce8a7bbSJon Mason if (!entry)
2085fce8a7bbSJon Mason goto err2;
2086fce8a7bbSJon Mason
2087282a2feeSJon Mason entry->qp = qp;
2088fce8a7bbSJon Mason ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2089fce8a7bbSJon Mason &qp->tx_free_q);
2090fce8a7bbSJon Mason }
2091fce8a7bbSJon Mason
2092e26a5843SAllen Hubbe ntb_db_clear(qp->ndev, qp_bit);
2093e26a5843SAllen Hubbe ntb_db_clear_mask(qp->ndev, qp_bit);
2094fce8a7bbSJon Mason
2095fce8a7bbSJon Mason dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
2096fce8a7bbSJon Mason
2097fce8a7bbSJon Mason return qp;
2098fce8a7bbSJon Mason
2099fce8a7bbSJon Mason err2:
2100f766755cSJon Mason while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
2101fce8a7bbSJon Mason kfree(entry);
2102fce8a7bbSJon Mason err1:
2103a754a8fcSDave Jiang qp->rx_alloc_entry = 0;
2104da2e5ae5SAllen Hubbe while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
2105fce8a7bbSJon Mason kfree(entry);
2106c59666bbSLogan Gunthorpe if (qp->tx_mw_dma_addr)
2107c59666bbSLogan Gunthorpe dma_unmap_resource(qp->tx_dma_chan->device->dev,
2108c59666bbSLogan Gunthorpe qp->tx_mw_dma_addr, qp->tx_mw_size,
2109c59666bbSLogan Gunthorpe DMA_FROM_DEVICE, 0);
2110569410caSDave Jiang if (qp->tx_dma_chan)
2111569410caSDave Jiang dma_release_channel(qp->tx_dma_chan);
2112569410caSDave Jiang if (qp->rx_dma_chan)
2113569410caSDave Jiang dma_release_channel(qp->rx_dma_chan);
2114e26a5843SAllen Hubbe nt->qp_bitmap_free |= qp_bit;
2115fce8a7bbSJon Mason err:
2116fce8a7bbSJon Mason return NULL;
2117fce8a7bbSJon Mason }
2118fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
2119fce8a7bbSJon Mason
2120fce8a7bbSJon Mason /**
2121fce8a7bbSJon Mason * ntb_transport_free_queue - Frees NTB transport queue
2122fce8a7bbSJon Mason * @qp: NTB queue to be freed
2123fce8a7bbSJon Mason *
2124fce8a7bbSJon Mason * Frees NTB transport queue
2125fce8a7bbSJon Mason */
ntb_transport_free_queue(struct ntb_transport_qp * qp)2126fce8a7bbSJon Mason void ntb_transport_free_queue(struct ntb_transport_qp *qp)
2127fce8a7bbSJon Mason {
2128186f27ffSJon Mason struct pci_dev *pdev;
2129fce8a7bbSJon Mason struct ntb_queue_entry *entry;
2130e26a5843SAllen Hubbe u64 qp_bit;
2131fce8a7bbSJon Mason
2132fce8a7bbSJon Mason if (!qp)
2133fce8a7bbSJon Mason return;
2134fce8a7bbSJon Mason
2135e26a5843SAllen Hubbe pdev = qp->ndev->pdev;
2136186f27ffSJon Mason
2137e9021331SDave Jiang qp->active = false;
2138e9021331SDave Jiang
2139569410caSDave Jiang if (qp->tx_dma_chan) {
2140569410caSDave Jiang struct dma_chan *chan = qp->tx_dma_chan;
2141282a2feeSJon Mason /* Putting the dma_chan to NULL will force any new traffic to be
2142282a2feeSJon Mason * processed by the CPU instead of the DAM engine
2143282a2feeSJon Mason */
2144569410caSDave Jiang qp->tx_dma_chan = NULL;
2145569410caSDave Jiang
2146569410caSDave Jiang /* Try to be nice and wait for any queued DMA engine
2147569410caSDave Jiang * transactions to process before smashing it with a rock
2148569410caSDave Jiang */
2149569410caSDave Jiang dma_sync_wait(chan, qp->last_cookie);
2150569410caSDave Jiang dmaengine_terminate_all(chan);
2151c59666bbSLogan Gunthorpe
2152c59666bbSLogan Gunthorpe dma_unmap_resource(chan->device->dev,
2153c59666bbSLogan Gunthorpe qp->tx_mw_dma_addr, qp->tx_mw_size,
2154c59666bbSLogan Gunthorpe DMA_FROM_DEVICE, 0);
2155c59666bbSLogan Gunthorpe
2156569410caSDave Jiang dma_release_channel(chan);
2157569410caSDave Jiang }
2158569410caSDave Jiang
2159569410caSDave Jiang if (qp->rx_dma_chan) {
2160569410caSDave Jiang struct dma_chan *chan = qp->rx_dma_chan;
2161569410caSDave Jiang /* Putting the dma_chan to NULL will force any new traffic to be
2162569410caSDave Jiang * processed by the CPU instead of the DAM engine
2163569410caSDave Jiang */
2164569410caSDave Jiang qp->rx_dma_chan = NULL;
2165282a2feeSJon Mason
2166282a2feeSJon Mason /* Try to be nice and wait for any queued DMA engine
2167282a2feeSJon Mason * transactions to process before smashing it with a rock
2168282a2feeSJon Mason */
2169282a2feeSJon Mason dma_sync_wait(chan, qp->last_cookie);
2170282a2feeSJon Mason dmaengine_terminate_all(chan);
21711199aa61SAllen Hubbe dma_release_channel(chan);
2172282a2feeSJon Mason }
2173fce8a7bbSJon Mason
2174e26a5843SAllen Hubbe qp_bit = BIT_ULL(qp->qp_num);
2175e26a5843SAllen Hubbe
2176e26a5843SAllen Hubbe ntb_db_set_mask(qp->ndev, qp_bit);
2177e9021331SDave Jiang tasklet_kill(&qp->rxc_db_work);
2178fce8a7bbSJon Mason
2179282a2feeSJon Mason cancel_delayed_work_sync(&qp->link_work);
2180282a2feeSJon Mason
2181e26a5843SAllen Hubbe qp->cb_data = NULL;
2182e26a5843SAllen Hubbe qp->rx_handler = NULL;
2183e26a5843SAllen Hubbe qp->tx_handler = NULL;
2184e26a5843SAllen Hubbe qp->event_handler = NULL;
2185e26a5843SAllen Hubbe
2186da2e5ae5SAllen Hubbe while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
2187fce8a7bbSJon Mason kfree(entry);
2188fce8a7bbSJon Mason
2189da2e5ae5SAllen Hubbe while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
2190da2e5ae5SAllen Hubbe dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
2191da2e5ae5SAllen Hubbe kfree(entry);
2192da2e5ae5SAllen Hubbe }
2193da2e5ae5SAllen Hubbe
2194da2e5ae5SAllen Hubbe while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
2195da2e5ae5SAllen Hubbe dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
2196fce8a7bbSJon Mason kfree(entry);
2197fce8a7bbSJon Mason }
2198fce8a7bbSJon Mason
2199f766755cSJon Mason while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
2200fce8a7bbSJon Mason kfree(entry);
2201fce8a7bbSJon Mason
220230a4bb1eSAllen Hubbe qp->transport->qp_bitmap_free |= qp_bit;
2203fce8a7bbSJon Mason
2204fce8a7bbSJon Mason dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
2205fce8a7bbSJon Mason }
2206fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
2207fce8a7bbSJon Mason
2208fce8a7bbSJon Mason /**
2209fce8a7bbSJon Mason * ntb_transport_rx_remove - Dequeues enqueued rx packet
2210fce8a7bbSJon Mason * @qp: NTB queue to be freed
2211fce8a7bbSJon Mason * @len: pointer to variable to write enqueued buffers length
2212fce8a7bbSJon Mason *
2213fce8a7bbSJon Mason * Dequeues unused buffers from receive queue. Should only be used during
2214fce8a7bbSJon Mason * shutdown of qp.
2215fce8a7bbSJon Mason *
2216fce8a7bbSJon Mason * RETURNS: NULL error value on error, or void* for success.
2217fce8a7bbSJon Mason */
ntb_transport_rx_remove(struct ntb_transport_qp * qp,unsigned int * len)2218fce8a7bbSJon Mason void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
2219fce8a7bbSJon Mason {
2220fce8a7bbSJon Mason struct ntb_queue_entry *entry;
2221fce8a7bbSJon Mason void *buf;
2222fce8a7bbSJon Mason
2223e26a5843SAllen Hubbe if (!qp || qp->client_ready)
2224fce8a7bbSJon Mason return NULL;
2225fce8a7bbSJon Mason
2226da2e5ae5SAllen Hubbe entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
2227fce8a7bbSJon Mason if (!entry)
2228fce8a7bbSJon Mason return NULL;
2229fce8a7bbSJon Mason
2230fce8a7bbSJon Mason buf = entry->cb_data;
2231fce8a7bbSJon Mason *len = entry->len;
2232fce8a7bbSJon Mason
2233da2e5ae5SAllen Hubbe ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
2234fce8a7bbSJon Mason
2235fce8a7bbSJon Mason return buf;
2236fce8a7bbSJon Mason }
2237fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
2238fce8a7bbSJon Mason
2239fce8a7bbSJon Mason /**
2240fce8a7bbSJon Mason * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
2241fce8a7bbSJon Mason * @qp: NTB transport layer queue the entry is to be enqueued on
2242fce8a7bbSJon Mason * @cb: per buffer pointer for callback function to use
2243fce8a7bbSJon Mason * @data: pointer to data buffer that incoming packets will be copied into
2244fce8a7bbSJon Mason * @len: length of the data buffer
2245fce8a7bbSJon Mason *
2246fce8a7bbSJon Mason * Enqueue a new receive buffer onto the transport queue into which a NTB
2247fce8a7bbSJon Mason * payload can be received into.
2248fce8a7bbSJon Mason *
2249fce8a7bbSJon Mason * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2250fce8a7bbSJon Mason */
ntb_transport_rx_enqueue(struct ntb_transport_qp * qp,void * cb,void * data,unsigned int len)2251fce8a7bbSJon Mason int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2252fce8a7bbSJon Mason unsigned int len)
2253fce8a7bbSJon Mason {
2254fce8a7bbSJon Mason struct ntb_queue_entry *entry;
2255fce8a7bbSJon Mason
2256fce8a7bbSJon Mason if (!qp)
2257fce8a7bbSJon Mason return -EINVAL;
2258fce8a7bbSJon Mason
2259da2e5ae5SAllen Hubbe entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
2260fce8a7bbSJon Mason if (!entry)
2261fce8a7bbSJon Mason return -ENOMEM;
2262fce8a7bbSJon Mason
2263fce8a7bbSJon Mason entry->cb_data = cb;
2264fce8a7bbSJon Mason entry->buf = data;
2265fce8a7bbSJon Mason entry->len = len;
2266da2e5ae5SAllen Hubbe entry->flags = 0;
226772203572SDave Jiang entry->retries = 0;
226872203572SDave Jiang entry->errors = 0;
226972203572SDave Jiang entry->rx_index = 0;
2270fce8a7bbSJon Mason
2271da2e5ae5SAllen Hubbe ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
2272da2e5ae5SAllen Hubbe
2273e9021331SDave Jiang if (qp->active)
2274da2e5ae5SAllen Hubbe tasklet_schedule(&qp->rxc_db_work);
2275fce8a7bbSJon Mason
2276fce8a7bbSJon Mason return 0;
2277fce8a7bbSJon Mason }
2278fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2279fce8a7bbSJon Mason
2280fce8a7bbSJon Mason /**
2281fce8a7bbSJon Mason * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2282fce8a7bbSJon Mason * @qp: NTB transport layer queue the entry is to be enqueued on
2283fce8a7bbSJon Mason * @cb: per buffer pointer for callback function to use
2284fce8a7bbSJon Mason * @data: pointer to data buffer that will be sent
2285fce8a7bbSJon Mason * @len: length of the data buffer
2286fce8a7bbSJon Mason *
2287fce8a7bbSJon Mason * Enqueue a new transmit buffer onto the transport queue from which a NTB
2288f9a2cf89SJon Mason * payload will be transmitted. This assumes that a lock is being held to
2289fce8a7bbSJon Mason * serialize access to the qp.
2290fce8a7bbSJon Mason *
2291fce8a7bbSJon Mason * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2292fce8a7bbSJon Mason */
ntb_transport_tx_enqueue(struct ntb_transport_qp * qp,void * cb,void * data,unsigned int len)2293fce8a7bbSJon Mason int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2294fce8a7bbSJon Mason unsigned int len)
2295fce8a7bbSJon Mason {
2296fce8a7bbSJon Mason struct ntb_queue_entry *entry;
2297fce8a7bbSJon Mason int rc;
2298fce8a7bbSJon Mason
2299f195a1a6SDave Jiang if (!qp || !len)
2300fce8a7bbSJon Mason return -EINVAL;
2301fce8a7bbSJon Mason
2302f195a1a6SDave Jiang /* If the qp link is down already, just ignore. */
2303f195a1a6SDave Jiang if (!qp->link_is_up)
2304f195a1a6SDave Jiang return 0;
2305f195a1a6SDave Jiang
2306fce8a7bbSJon Mason entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
2307282a2feeSJon Mason if (!entry) {
2308282a2feeSJon Mason qp->tx_err_no_buf++;
2309e74bfeedSDave Jiang return -EBUSY;
2310282a2feeSJon Mason }
2311fce8a7bbSJon Mason
2312fce8a7bbSJon Mason entry->cb_data = cb;
2313fce8a7bbSJon Mason entry->buf = data;
2314fce8a7bbSJon Mason entry->len = len;
2315fce8a7bbSJon Mason entry->flags = 0;
23169cabc269SDave Jiang entry->errors = 0;
23179cabc269SDave Jiang entry->retries = 0;
23189cabc269SDave Jiang entry->tx_index = 0;
2319fce8a7bbSJon Mason
2320fce8a7bbSJon Mason rc = ntb_process_tx(qp, entry);
2321fce8a7bbSJon Mason if (rc)
2322fce8a7bbSJon Mason ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2323fce8a7bbSJon Mason &qp->tx_free_q);
2324fce8a7bbSJon Mason
2325fce8a7bbSJon Mason return rc;
2326fce8a7bbSJon Mason }
2327fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2328fce8a7bbSJon Mason
2329fce8a7bbSJon Mason /**
2330fce8a7bbSJon Mason * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2331fce8a7bbSJon Mason * @qp: NTB transport layer queue to be enabled
2332fce8a7bbSJon Mason *
2333fce8a7bbSJon Mason * Notify NTB transport layer of client readiness to use queue
2334fce8a7bbSJon Mason */
ntb_transport_link_up(struct ntb_transport_qp * qp)2335fce8a7bbSJon Mason void ntb_transport_link_up(struct ntb_transport_qp *qp)
2336fce8a7bbSJon Mason {
2337fce8a7bbSJon Mason if (!qp)
2338fce8a7bbSJon Mason return;
2339fce8a7bbSJon Mason
2340e26a5843SAllen Hubbe qp->client_ready = true;
2341fce8a7bbSJon Mason
2342e26a5843SAllen Hubbe if (qp->transport->link_is_up)
2343fce8a7bbSJon Mason schedule_delayed_work(&qp->link_work, 0);
2344fce8a7bbSJon Mason }
2345fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2346fce8a7bbSJon Mason
2347fce8a7bbSJon Mason /**
2348fce8a7bbSJon Mason * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2349fce8a7bbSJon Mason * @qp: NTB transport layer queue to be disabled
2350fce8a7bbSJon Mason *
2351fce8a7bbSJon Mason * Notify NTB transport layer of client's desire to no longer receive data on
2352fce8a7bbSJon Mason * transport queue specified. It is the client's responsibility to ensure all
2353f9a2cf89SJon Mason * entries on queue are purged or otherwise handled appropriately.
2354fce8a7bbSJon Mason */
ntb_transport_link_down(struct ntb_transport_qp * qp)2355fce8a7bbSJon Mason void ntb_transport_link_down(struct ntb_transport_qp *qp)
2356fce8a7bbSJon Mason {
2357e26a5843SAllen Hubbe int val;
2358fce8a7bbSJon Mason
2359fce8a7bbSJon Mason if (!qp)
2360fce8a7bbSJon Mason return;
2361fce8a7bbSJon Mason
2362e26a5843SAllen Hubbe qp->client_ready = false;
2363fce8a7bbSJon Mason
2364e26a5843SAllen Hubbe val = ntb_spad_read(qp->ndev, QP_LINKS);
2365fce8a7bbSJon Mason
2366d67288a3SSerge Semin ntb_peer_spad_write(qp->ndev, PIDX, QP_LINKS, val & ~BIT(qp->qp_num));
2367fce8a7bbSJon Mason
2368e26a5843SAllen Hubbe if (qp->link_is_up)
2369fce8a7bbSJon Mason ntb_send_link_down(qp);
2370fce8a7bbSJon Mason else
2371fce8a7bbSJon Mason cancel_delayed_work_sync(&qp->link_work);
2372fce8a7bbSJon Mason }
2373fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2374fce8a7bbSJon Mason
2375fce8a7bbSJon Mason /**
2376fce8a7bbSJon Mason * ntb_transport_link_query - Query transport link state
2377fce8a7bbSJon Mason * @qp: NTB transport layer queue to be queried
2378fce8a7bbSJon Mason *
2379fce8a7bbSJon Mason * Query connectivity to the remote system of the NTB transport queue
2380fce8a7bbSJon Mason *
2381fce8a7bbSJon Mason * RETURNS: true for link up or false for link down
2382fce8a7bbSJon Mason */
ntb_transport_link_query(struct ntb_transport_qp * qp)2383fce8a7bbSJon Mason bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2384fce8a7bbSJon Mason {
2385186f27ffSJon Mason if (!qp)
2386186f27ffSJon Mason return false;
2387186f27ffSJon Mason
2388e26a5843SAllen Hubbe return qp->link_is_up;
2389fce8a7bbSJon Mason }
2390fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2391fce8a7bbSJon Mason
2392fce8a7bbSJon Mason /**
2393fce8a7bbSJon Mason * ntb_transport_qp_num - Query the qp number
2394fce8a7bbSJon Mason * @qp: NTB transport layer queue to be queried
2395fce8a7bbSJon Mason *
2396fce8a7bbSJon Mason * Query qp number of the NTB transport queue
2397fce8a7bbSJon Mason *
2398fce8a7bbSJon Mason * RETURNS: a zero based number specifying the qp number
2399fce8a7bbSJon Mason */
ntb_transport_qp_num(struct ntb_transport_qp * qp)2400fce8a7bbSJon Mason unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2401fce8a7bbSJon Mason {
2402186f27ffSJon Mason if (!qp)
2403186f27ffSJon Mason return 0;
2404186f27ffSJon Mason
2405fce8a7bbSJon Mason return qp->qp_num;
2406fce8a7bbSJon Mason }
2407fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2408fce8a7bbSJon Mason
2409fce8a7bbSJon Mason /**
2410fce8a7bbSJon Mason * ntb_transport_max_size - Query the max payload size of a qp
2411fce8a7bbSJon Mason * @qp: NTB transport layer queue to be queried
2412fce8a7bbSJon Mason *
2413fce8a7bbSJon Mason * Query the maximum payload size permissible on the given qp
2414fce8a7bbSJon Mason *
2415fce8a7bbSJon Mason * RETURNS: the max payload size of a qp
2416fce8a7bbSJon Mason */
ntb_transport_max_size(struct ntb_transport_qp * qp)2417ef114ed5SJon Mason unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
2418fce8a7bbSJon Mason {
241904afde45SDave Jiang unsigned int max_size;
2420569410caSDave Jiang unsigned int copy_align;
242104afde45SDave Jiang struct dma_chan *rx_chan, *tx_chan;
2422282a2feeSJon Mason
2423186f27ffSJon Mason if (!qp)
2424186f27ffSJon Mason return 0;
2425186f27ffSJon Mason
242604afde45SDave Jiang rx_chan = qp->rx_dma_chan;
242704afde45SDave Jiang tx_chan = qp->tx_dma_chan;
2428282a2feeSJon Mason
242904afde45SDave Jiang copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
243004afde45SDave Jiang tx_chan ? tx_chan->device->copy_align : 0);
2431569410caSDave Jiang
2432282a2feeSJon Mason /* If DMA engine usage is possible, try to find the max size for that */
243304afde45SDave Jiang max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
243404afde45SDave Jiang max_size = round_down(max_size, 1 << copy_align);
2435282a2feeSJon Mason
243604afde45SDave Jiang return max_size;
2437fce8a7bbSJon Mason }
2438fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_max_size);
2439e26a5843SAllen Hubbe
ntb_transport_tx_free_entry(struct ntb_transport_qp * qp)2440e74bfeedSDave Jiang unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2441e74bfeedSDave Jiang {
2442e74bfeedSDave Jiang unsigned int head = qp->tx_index;
2443e74bfeedSDave Jiang unsigned int tail = qp->remote_rx_info->entry;
2444e74bfeedSDave Jiang
24455a7693e6SDave Jiang return tail >= head ? tail - head : qp->tx_max_entry + tail - head;
2446e74bfeedSDave Jiang }
2447e74bfeedSDave Jiang EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2448e74bfeedSDave Jiang
ntb_transport_doorbell_callback(void * data,int vector)2449e26a5843SAllen Hubbe static void ntb_transport_doorbell_callback(void *data, int vector)
2450e26a5843SAllen Hubbe {
2451e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = data;
2452e26a5843SAllen Hubbe struct ntb_transport_qp *qp;
2453e26a5843SAllen Hubbe u64 db_bits;
2454e26a5843SAllen Hubbe unsigned int qp_num;
2455e26a5843SAllen Hubbe
24562b0569b3SLogan Gunthorpe if (ntb_db_read(nt->ndev) & nt->msi_db_mask) {
24572b0569b3SLogan Gunthorpe ntb_transport_msi_peer_desc_changed(nt);
24582b0569b3SLogan Gunthorpe ntb_db_clear(nt->ndev, nt->msi_db_mask);
24592b0569b3SLogan Gunthorpe }
24602b0569b3SLogan Gunthorpe
2461e26a5843SAllen Hubbe db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2462e26a5843SAllen Hubbe ntb_db_vector_mask(nt->ndev, vector));
2463e26a5843SAllen Hubbe
2464e26a5843SAllen Hubbe while (db_bits) {
2465e26a5843SAllen Hubbe qp_num = __ffs(db_bits);
2466e26a5843SAllen Hubbe qp = &nt->qp_vec[qp_num];
2467e26a5843SAllen Hubbe
2468e9021331SDave Jiang if (qp->active)
2469e26a5843SAllen Hubbe tasklet_schedule(&qp->rxc_db_work);
2470e26a5843SAllen Hubbe
2471e26a5843SAllen Hubbe db_bits &= ~BIT_ULL(qp_num);
2472e26a5843SAllen Hubbe }
2473e26a5843SAllen Hubbe }
2474e26a5843SAllen Hubbe
2475e26a5843SAllen Hubbe static const struct ntb_ctx_ops ntb_transport_ops = {
2476e26a5843SAllen Hubbe .link_event = ntb_transport_event_callback,
2477e26a5843SAllen Hubbe .db_event = ntb_transport_doorbell_callback,
2478e26a5843SAllen Hubbe };
2479e26a5843SAllen Hubbe
2480e26a5843SAllen Hubbe static struct ntb_client ntb_transport_client = {
2481e26a5843SAllen Hubbe .ops = {
2482e26a5843SAllen Hubbe .probe = ntb_transport_probe,
2483e26a5843SAllen Hubbe .remove = ntb_transport_free,
2484e26a5843SAllen Hubbe },
2485e26a5843SAllen Hubbe };
2486e26a5843SAllen Hubbe
ntb_transport_init(void)2487e26a5843SAllen Hubbe static int __init ntb_transport_init(void)
2488e26a5843SAllen Hubbe {
2489e26a5843SAllen Hubbe int rc;
2490e26a5843SAllen Hubbe
24917eb38781SDave Jiang pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
24927eb38781SDave Jiang
2493e26a5843SAllen Hubbe if (debugfs_initialized())
2494e26a5843SAllen Hubbe nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2495e26a5843SAllen Hubbe
2496e26a5843SAllen Hubbe rc = bus_register(&ntb_transport_bus);
2497e26a5843SAllen Hubbe if (rc)
2498e26a5843SAllen Hubbe goto err_bus;
2499e26a5843SAllen Hubbe
2500e26a5843SAllen Hubbe rc = ntb_register_client(&ntb_transport_client);
2501e26a5843SAllen Hubbe if (rc)
2502e26a5843SAllen Hubbe goto err_client;
2503e26a5843SAllen Hubbe
2504e26a5843SAllen Hubbe return 0;
2505e26a5843SAllen Hubbe
2506e26a5843SAllen Hubbe err_client:
2507e26a5843SAllen Hubbe bus_unregister(&ntb_transport_bus);
2508e26a5843SAllen Hubbe err_bus:
2509e26a5843SAllen Hubbe debugfs_remove_recursive(nt_debugfs_dir);
2510e26a5843SAllen Hubbe return rc;
2511e26a5843SAllen Hubbe }
2512e26a5843SAllen Hubbe module_init(ntb_transport_init);
2513e26a5843SAllen Hubbe
ntb_transport_exit(void)2514e26a5843SAllen Hubbe static void __exit ntb_transport_exit(void)
2515e26a5843SAllen Hubbe {
2516e26a5843SAllen Hubbe ntb_unregister_client(&ntb_transport_client);
2517e26a5843SAllen Hubbe bus_unregister(&ntb_transport_bus);
2518dd62245eSAllen Hubbe debugfs_remove_recursive(nt_debugfs_dir);
2519e26a5843SAllen Hubbe }
2520e26a5843SAllen Hubbe module_exit(ntb_transport_exit);
2521