blob: 4a02561cfb965726c2fe2d73c9934f10adcbfb20 [file] [log] [blame]
Jon Masonfce8a7b2012-11-16 19:27:12 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -04008 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * BSD LICENSE
15 *
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -040017 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -070018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
28 * distribution.
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
Allen Hubbee26a5842015-04-09 10:33:20 -040045 * PCIe NTB Transport Linux driver
Jon Masonfce8a7b2012-11-16 19:27:12 -070046 *
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
49 */
50#include <linux/debugfs.h>
51#include <linux/delay.h>
Jon Mason282a2fe2013-02-12 09:52:50 -070052#include <linux/dmaengine.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070053#include <linux/dma-mapping.h>
54#include <linux/errno.h>
55#include <linux/export.h>
56#include <linux/interrupt.h>
57#include <linux/module.h>
58#include <linux/pci.h>
59#include <linux/slab.h>
60#include <linux/types.h>
Dave Jiang06917f72015-05-19 16:45:46 -040061#include <linux/uaccess.h>
Allen Hubbee26a5842015-04-09 10:33:20 -040062#include "linux/ntb.h"
63#include "linux/ntb_transport.h"
Jon Masonfce8a7b2012-11-16 19:27:12 -070064
Allen Hubbee26a5842015-04-09 10:33:20 -040065#define NTB_TRANSPORT_VERSION 4
66#define NTB_TRANSPORT_VER "4"
67#define NTB_TRANSPORT_NAME "ntb_transport"
68#define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +053069#define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
Allen Hubbee26a5842015-04-09 10:33:20 -040070
71MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
72MODULE_VERSION(NTB_TRANSPORT_VER);
73MODULE_LICENSE("Dual BSD/GPL");
74MODULE_AUTHOR("Intel Corporation");
75
76static unsigned long max_mw_size;
77module_param(max_mw_size, ulong, 0644);
78MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
Jon Masonfce8a7b2012-11-16 19:27:12 -070079
Dave Jiang98914172015-06-03 11:29:38 -040080static unsigned int transport_mtu = 0x10000;
Jon Masonfce8a7b2012-11-16 19:27:12 -070081module_param(transport_mtu, uint, 0644);
82MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
83
Jon Mason948d3a62013-04-18 17:07:36 -070084static unsigned char max_num_clients;
Jon Masonfce8a7b2012-11-16 19:27:12 -070085module_param(max_num_clients, byte, 0644);
86MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
87
Jon Mason282a2fe2013-02-12 09:52:50 -070088static unsigned int copy_bytes = 1024;
89module_param(copy_bytes, uint, 0644);
90MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
91
Dave Jianga41ef052015-05-19 16:52:04 -040092static bool use_dma;
93module_param(use_dma, bool, 0644);
94MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
95
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -060096static bool use_msi;
97#ifdef CONFIG_NTB_MSI
98module_param(use_msi, bool, 0644);
99MODULE_PARM_DESC(use_msi, "Use MSI interrupts instead of doorbells");
100#endif
101
Allen Hubbee26a5842015-04-09 10:33:20 -0400102static struct dentry *nt_debugfs_dir;
103
Serge Semin1e530112016-12-14 02:49:14 +0300104/* Only two-ports NTB devices are supported */
105#define PIDX NTB_DEF_PEER_IDX
106
Jon Masonfce8a7b2012-11-16 19:27:12 -0700107struct ntb_queue_entry {
108 /* ntb_queue list reference */
109 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400110 /* pointers to data to be transferred */
Jon Masonfce8a7b2012-11-16 19:27:12 -0700111 void *cb_data;
112 void *buf;
113 unsigned int len;
114 unsigned int flags;
Dave Jiang9cabc262016-07-20 13:14:07 -0700115 int retries;
116 int errors;
117 unsigned int tx_index;
Dave Jiang72203572016-07-20 13:14:13 -0700118 unsigned int rx_index;
Jon Mason282a2fe2013-02-12 09:52:50 -0700119
120 struct ntb_transport_qp *qp;
121 union {
122 struct ntb_payload_header __iomem *tx_hdr;
123 struct ntb_payload_header *rx_hdr;
124 };
Jon Masonfce8a7b2012-11-16 19:27:12 -0700125};
126
Jon Mason793c20e2013-01-19 02:02:26 -0700127struct ntb_rx_info {
128 unsigned int entry;
129};
130
Jon Masonfce8a7b2012-11-16 19:27:12 -0700131struct ntb_transport_qp {
Allen Hubbee26a5842015-04-09 10:33:20 -0400132 struct ntb_transport_ctx *transport;
133 struct ntb_dev *ndev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700134 void *cb_data;
Dave Jiang569410ca2015-07-13 08:07:22 -0400135 struct dma_chan *tx_dma_chan;
136 struct dma_chan *rx_dma_chan;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700137
138 bool client_ready;
Allen Hubbee26a5842015-04-09 10:33:20 -0400139 bool link_is_up;
Dave Jiange9021332016-02-23 09:11:36 -0700140 bool active;
Allen Hubbee26a5842015-04-09 10:33:20 -0400141
Jon Masonfce8a7b2012-11-16 19:27:12 -0700142 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400143 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700144
Jon Mason74465642013-01-21 15:28:52 -0700145 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -0700146 struct ntb_rx_info *remote_rx_info;
147
Jon Mason53ca4fe2013-11-26 11:21:50 -0700148 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
149 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700150 struct list_head tx_free_q;
151 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700152 void __iomem *tx_mw;
Logan Gunthorpec59666b2019-01-18 17:10:01 -0700153 phys_addr_t tx_mw_phys;
154 size_t tx_mw_size;
155 dma_addr_t tx_mw_dma_addr;
Jon Mason793c20e2013-01-19 02:02:26 -0700156 unsigned int tx_index;
157 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700158 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700159
Jon Mason53ca4fe2013-11-26 11:21:50 -0700160 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
161 void *data, int len);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400162 struct list_head rx_post_q;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700163 struct list_head rx_pend_q;
164 struct list_head rx_free_q;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400165 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
166 spinlock_t ntb_rx_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700167 void *rx_buff;
168 unsigned int rx_index;
169 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700170 unsigned int rx_max_frame;
Dave Jianga754a8f2016-04-08 10:49:06 -0700171 unsigned int rx_alloc_entry;
Jon Mason282a2fe2013-02-12 09:52:50 -0700172 dma_cookie_t last_cookie;
Allen Hubbee26a5842015-04-09 10:33:20 -0400173 struct tasklet_struct rxc_db_work;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700174
Jon Mason53ca4fe2013-11-26 11:21:50 -0700175 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700176 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700177 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700178
179 struct dentry *debugfs_dir;
180 struct dentry *debugfs_stats;
181
182 /* Stats */
183 u64 rx_bytes;
184 u64 rx_pkts;
185 u64 rx_ring_empty;
186 u64 rx_err_no_buf;
187 u64 rx_err_oflow;
188 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700189 u64 rx_memcpy;
190 u64 rx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700191 u64 tx_bytes;
192 u64 tx_pkts;
193 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700194 u64 tx_err_no_buf;
195 u64 tx_memcpy;
196 u64 tx_async;
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -0600197
198 bool use_msi;
199 int msi_irq;
200 struct ntb_msi_desc msi_desc;
201 struct ntb_msi_desc peer_msi_desc;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700202};
203
204struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400205 phys_addr_t phys_addr;
206 resource_size_t phys_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400207 void __iomem *vbase;
208 size_t xlat_size;
209 size_t buff_size;
Aaron Sierrafc5d1822018-10-12 15:35:03 -0500210 size_t alloc_size;
211 void *alloc_addr;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700212 void *virt_addr;
213 dma_addr_t dma_addr;
214};
215
216struct ntb_transport_client_dev {
217 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400218 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700219 struct device dev;
220};
221
Allen Hubbee26a5842015-04-09 10:33:20 -0400222struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700223 struct list_head entry;
224 struct list_head client_devs;
225
Allen Hubbee26a5842015-04-09 10:33:20 -0400226 struct ntb_dev *ndev;
227
228 struct ntb_transport_mw *mw_vec;
229 struct ntb_transport_qp *qp_vec;
230 unsigned int mw_count;
231 unsigned int qp_count;
232 u64 qp_bitmap;
233 u64 qp_bitmap_free;
234
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -0600235 bool use_msi;
236 unsigned int msi_spad_offset;
237 u64 msi_db_mask;
238
Allen Hubbee26a5842015-04-09 10:33:20 -0400239 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700240 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700241 struct work_struct link_cleanup;
Dave Jiangc8650fd2015-07-13 08:07:09 -0400242
243 struct dentry *debugfs_node_dir;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700244};
245
246enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400247 DESC_DONE_FLAG = BIT(0),
248 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700249};
250
251struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700252 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700253 unsigned int len;
254 unsigned int flags;
255};
256
257enum {
258 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700259 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700260 NUM_QPS,
261 NUM_MWS,
262 MW0_SZ_HIGH,
263 MW0_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700264};
265
Allen Hubbee26a5842015-04-09 10:33:20 -0400266#define dev_client_dev(__dev) \
267 container_of((__dev), struct ntb_transport_client_dev, dev)
268
269#define drv_client(__drv) \
270 container_of((__drv), struct ntb_transport_client, driver)
271
272#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700273#define NTB_QP_DEF_NUM_ENTRIES 100
274#define NTB_LINK_DOWN_TIMEOUT 10
275
Allen Hubbee26a5842015-04-09 10:33:20 -0400276static void ntb_transport_rxc_db(unsigned long data);
277static const struct ntb_ctx_ops ntb_transport_ops;
278static struct ntb_client ntb_transport_client;
Dave Jiang9cabc262016-07-20 13:14:07 -0700279static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
280 struct ntb_queue_entry *entry);
281static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
Dave Jiang72203572016-07-20 13:14:13 -0700282static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
283static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
284
Allen Hubbee26a5842015-04-09 10:33:20 -0400285
286static int ntb_transport_bus_match(struct device *dev,
287 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700288{
289 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
290}
291
Allen Hubbee26a5842015-04-09 10:33:20 -0400292static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700293{
Allen Hubbee26a5842015-04-09 10:33:20 -0400294 const struct ntb_transport_client *client;
Colin Ian King5e2cbf12019-08-18 19:53:49 +0100295 int rc;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700296
297 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400298
299 client = drv_client(dev->driver);
300 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700301 if (rc)
302 put_device(dev);
303
304 return rc;
305}
306
Allen Hubbee26a5842015-04-09 10:33:20 -0400307static int ntb_transport_bus_remove(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700308{
Allen Hubbee26a5842015-04-09 10:33:20 -0400309 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700310
Allen Hubbee26a5842015-04-09 10:33:20 -0400311 client = drv_client(dev->driver);
312 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700313
314 put_device(dev);
315
316 return 0;
317}
318
Allen Hubbee26a5842015-04-09 10:33:20 -0400319static struct bus_type ntb_transport_bus = {
320 .name = "ntb_transport",
321 .match = ntb_transport_bus_match,
322 .probe = ntb_transport_bus_probe,
323 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700324};
325
326static LIST_HEAD(ntb_transport_list);
327
Allen Hubbee26a5842015-04-09 10:33:20 -0400328static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700329{
Dave Jiang31510002015-07-13 08:07:19 -0400330 list_add_tail(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700331 return 0;
332}
333
Allen Hubbee26a5842015-04-09 10:33:20 -0400334static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700335{
336 struct ntb_transport_client_dev *client_dev, *cd;
337
338 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
339 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
340 dev_name(&client_dev->dev));
341 list_del(&client_dev->entry);
342 device_unregister(&client_dev->dev);
343 }
344
345 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700346}
347
Allen Hubbee26a5842015-04-09 10:33:20 -0400348static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700349{
350 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700351
Allen Hubbee26a5842015-04-09 10:33:20 -0400352 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700353 kfree(client_dev);
354}
355
356/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400357 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700358 * @device_name: Name of NTB client device
359 *
360 * Unregister an NTB client device with the NTB transport layer
361 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400362void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700363{
364 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400365 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700366
367 list_for_each_entry(nt, &ntb_transport_list, entry)
368 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
369 if (!strncmp(dev_name(&client->dev), device_name,
370 strlen(device_name))) {
371 list_del(&client->entry);
372 device_unregister(&client->dev);
373 }
374}
Allen Hubbee26a5842015-04-09 10:33:20 -0400375EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700376
377/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400378 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700379 * @device_name: Name of NTB client device
380 *
381 * Register an NTB client device with the NTB transport layer
382 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400383int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700384{
385 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400386 struct ntb_transport_ctx *nt;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400387 int node;
Jon Mason8b19d452013-04-26 14:51:57 -0700388 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700389
Jon Mason8222b402013-01-19 02:02:16 -0700390 if (list_empty(&ntb_transport_list))
391 return -ENODEV;
392
Jon Masonfce8a7b2012-11-16 19:27:12 -0700393 list_for_each_entry(nt, &ntb_transport_list, entry) {
394 struct device *dev;
395
Allen Hubbe1199aa62015-05-18 06:20:47 -0400396 node = dev_to_node(&nt->ndev->dev);
397
398 client_dev = kzalloc_node(sizeof(*client_dev),
399 GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700400 if (!client_dev) {
401 rc = -ENOMEM;
402 goto err;
403 }
404
405 dev = &client_dev->dev;
406
407 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700408 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400409 dev->bus = &ntb_transport_bus;
410 dev->release = ntb_transport_client_release;
411 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700412
413 rc = device_register(dev);
414 if (rc) {
415 kfree(client_dev);
416 goto err;
417 }
418
419 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700420 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700421 }
422
423 return 0;
424
425err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400426 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700427
428 return rc;
429}
Allen Hubbee26a5842015-04-09 10:33:20 -0400430EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700431
432/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400433 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700434 * @drv: NTB client driver to be registered
435 *
436 * Register an NTB client driver with the NTB transport layer
437 *
438 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
439 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400440int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700441{
Allen Hubbee26a5842015-04-09 10:33:20 -0400442 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700443
Jon Mason8222b402013-01-19 02:02:16 -0700444 if (list_empty(&ntb_transport_list))
445 return -ENODEV;
446
Jon Masonfce8a7b2012-11-16 19:27:12 -0700447 return driver_register(&drv->driver);
448}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400449EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700450
451/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400452 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700453 * @drv: NTB client driver to be unregistered
454 *
455 * Unregister an NTB client driver with the NTB transport layer
456 *
457 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
458 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400459void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700460{
461 driver_unregister(&drv->driver);
462}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400463EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700464
Jon Masonfce8a7b2012-11-16 19:27:12 -0700465static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
466 loff_t *offp)
467{
468 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700469 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700470 ssize_t ret, out_offset, out_count;
471
Dave Jiang260bee92015-07-13 08:07:11 -0400472 qp = filp->private_data;
473
474 if (!qp || !qp->link_is_up)
475 return 0;
476
Jon Mason282a2fe2013-02-12 09:52:50 -0700477 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700478
479 buf = kmalloc(out_count, GFP_KERNEL);
480 if (!buf)
481 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700482
Jon Masonfce8a7b2012-11-16 19:27:12 -0700483 out_offset = 0;
Takashi Iwai7f78c682020-03-11 09:49:17 +0100484 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400485 "\nNTB QP stats:\n\n");
Takashi Iwai7f78c682020-03-11 09:49:17 +0100486 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700487 "rx_bytes - \t%llu\n", qp->rx_bytes);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100488 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700489 "rx_pkts - \t%llu\n", qp->rx_pkts);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100490 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700491 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100492 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700493 "rx_async - \t%llu\n", qp->rx_async);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100494 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700495 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100496 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700497 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100498 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700499 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100500 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700501 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100502 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400503 "rx_buff - \t0x%p\n", qp->rx_buff);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100504 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700505 "rx_index - \t%u\n", qp->rx_index);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100506 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jianga754a8f2016-04-08 10:49:06 -0700507 "rx_max_entry - \t%u\n", qp->rx_max_entry);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100508 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jianga754a8f2016-04-08 10:49:06 -0700509 "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700510
Takashi Iwai7f78c682020-03-11 09:49:17 +0100511 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700512 "tx_bytes - \t%llu\n", qp->tx_bytes);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100513 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700514 "tx_pkts - \t%llu\n", qp->tx_pkts);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100515 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700516 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100517 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700518 "tx_async - \t%llu\n", qp->tx_async);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100519 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700520 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100521 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700522 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100523 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400524 "tx_mw - \t0x%p\n", qp->tx_mw);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100525 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400526 "tx_index (H) - \t%u\n", qp->tx_index);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100527 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400528 "RRI (T) - \t%u\n",
529 qp->remote_rx_info->entry);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100530 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700531 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Takashi Iwai7f78c682020-03-11 09:49:17 +0100532 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiange74bfee2015-07-13 08:07:17 -0400533 "free tx - \t%u\n",
534 ntb_transport_tx_free_entry(qp));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700535
Takashi Iwai7f78c682020-03-11 09:49:17 +0100536 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400537 "\n");
Takashi Iwai7f78c682020-03-11 09:49:17 +0100538 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiang569410ca2015-07-13 08:07:22 -0400539 "Using TX DMA - \t%s\n",
540 qp->tx_dma_chan ? "Yes" : "No");
Takashi Iwai7f78c682020-03-11 09:49:17 +0100541 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiang569410ca2015-07-13 08:07:22 -0400542 "Using RX DMA - \t%s\n",
543 qp->rx_dma_chan ? "Yes" : "No");
Takashi Iwai7f78c682020-03-11 09:49:17 +0100544 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400545 "QP Link - \t%s\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400546 qp->link_is_up ? "Up" : "Down");
Takashi Iwai7f78c682020-03-11 09:49:17 +0100547 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400548 "\n");
549
Jon Masond7237e22013-01-19 02:02:25 -0700550 if (out_offset > out_count)
551 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700552
553 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700554 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700555 return ret;
556}
557
558static const struct file_operations ntb_qp_debugfs_stats = {
559 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700560 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700561 .read = debugfs_read,
562};
563
564static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
565 struct list_head *list)
566{
567 unsigned long flags;
568
569 spin_lock_irqsave(lock, flags);
570 list_add_tail(entry, list);
571 spin_unlock_irqrestore(lock, flags);
572}
573
574static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700575 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700576{
577 struct ntb_queue_entry *entry;
578 unsigned long flags;
579
580 spin_lock_irqsave(lock, flags);
581 if (list_empty(list)) {
582 entry = NULL;
583 goto out;
584 }
585 entry = list_first_entry(list, struct ntb_queue_entry, entry);
586 list_del(&entry->entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400587
Jon Masonfce8a7b2012-11-16 19:27:12 -0700588out:
589 spin_unlock_irqrestore(lock, flags);
590
591 return entry;
592}
593
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400594static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
595 struct list_head *list,
596 struct list_head *to_list)
597{
598 struct ntb_queue_entry *entry;
599 unsigned long flags;
600
601 spin_lock_irqsave(lock, flags);
602
603 if (list_empty(list)) {
604 entry = NULL;
605 } else {
606 entry = list_first_entry(list, struct ntb_queue_entry, entry);
607 list_move_tail(&entry->entry, to_list);
608 }
609
610 spin_unlock_irqrestore(lock, flags);
611
612 return entry;
613}
614
Allen Hubbee26a5842015-04-09 10:33:20 -0400615static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
616 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700617{
Allen Hubbee26a5842015-04-09 10:33:20 -0400618 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
619 struct ntb_transport_mw *mw;
Dave Jianga754a8f2016-04-08 10:49:06 -0700620 struct ntb_dev *ndev = nt->ndev;
621 struct ntb_queue_entry *entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700622 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400623 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700624 unsigned int i;
Dave Jianga754a8f2016-04-08 10:49:06 -0700625 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700626
Allen Hubbee26a5842015-04-09 10:33:20 -0400627 mw_count = nt->mw_count;
628 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700629
Allen Hubbee26a5842015-04-09 10:33:20 -0400630 mw_num = QP_TO_MW(nt, qp_num);
631 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700632
Allen Hubbee26a5842015-04-09 10:33:20 -0400633 if (!mw->virt_addr)
634 return -ENOMEM;
635
Logan Gunthorpe8e8496e2017-06-05 14:00:53 -0600636 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -0400637 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700638 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400639 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700640
Allen Hubbee26a5842015-04-09 10:33:20 -0400641 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -0400642 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
Jon Mason793c20e2013-01-19 02:02:26 -0700643 rx_size -= sizeof(struct ntb_rx_info);
644
Jon Mason282a2fe2013-02-12 09:52:50 -0700645 qp->remote_rx_info = qp->rx_buff + rx_size;
646
Jon Masonc9d534c2013-02-01 15:45:16 -0700647 /* Due to housekeeping, there must be atleast 2 buffs */
648 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700649 qp->rx_max_entry = rx_size / qp->rx_max_frame;
650 qp->rx_index = 0;
651
Dave Jianga754a8f2016-04-08 10:49:06 -0700652 /*
653 * Checking to see if we have more entries than the default.
654 * We should add additional entries if that is the case so we
655 * can be in sync with the transport frames.
656 */
657 node = dev_to_node(&ndev->dev);
658 for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
Jia-Ju Bai82edcc72018-04-10 21:17:29 +0800659 entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
Dave Jianga754a8f2016-04-08 10:49:06 -0700660 if (!entry)
661 return -ENOMEM;
662
663 entry->qp = qp;
664 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
665 &qp->rx_free_q);
666 qp->rx_alloc_entry++;
667 }
668
Jon Masonc9d534c2013-02-01 15:45:16 -0700669 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700670
Jon Masonef114ed2013-01-19 02:02:18 -0700671 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700672 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400673 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
674 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700675 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700676 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700677
678 qp->rx_pkts = 0;
679 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700680 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400681
682 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700683}
684
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -0600685static irqreturn_t ntb_transport_isr(int irq, void *dev)
686{
687 struct ntb_transport_qp *qp = dev;
688
689 tasklet_schedule(&qp->rxc_db_work);
690
691 return IRQ_HANDLED;
692}
693
694static void ntb_transport_setup_qp_peer_msi(struct ntb_transport_ctx *nt,
695 unsigned int qp_num)
696{
697 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
698 int spad = qp_num * 2 + nt->msi_spad_offset;
699
700 if (!nt->use_msi)
701 return;
702
703 if (spad >= ntb_spad_count(nt->ndev))
704 return;
705
706 qp->peer_msi_desc.addr_offset =
707 ntb_peer_spad_read(qp->ndev, PIDX, spad);
708 qp->peer_msi_desc.data =
709 ntb_peer_spad_read(qp->ndev, PIDX, spad + 1);
710
711 dev_dbg(&qp->ndev->pdev->dev, "QP%d Peer MSI addr=%x data=%x\n",
712 qp_num, qp->peer_msi_desc.addr_offset, qp->peer_msi_desc.data);
713
714 if (qp->peer_msi_desc.addr_offset) {
715 qp->use_msi = true;
716 dev_info(&qp->ndev->pdev->dev,
717 "Using MSI interrupts for QP%d\n", qp_num);
718 }
719}
720
721static void ntb_transport_setup_qp_msi(struct ntb_transport_ctx *nt,
722 unsigned int qp_num)
723{
724 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
725 int spad = qp_num * 2 + nt->msi_spad_offset;
726 int rc;
727
728 if (!nt->use_msi)
729 return;
730
731 if (spad >= ntb_spad_count(nt->ndev)) {
732 dev_warn_once(&qp->ndev->pdev->dev,
733 "Not enough SPADS to use MSI interrupts\n");
734 return;
735 }
736
737 ntb_spad_write(qp->ndev, spad, 0);
738 ntb_spad_write(qp->ndev, spad + 1, 0);
739
740 if (!qp->msi_irq) {
741 qp->msi_irq = ntbm_msi_request_irq(qp->ndev, ntb_transport_isr,
742 KBUILD_MODNAME, qp,
743 &qp->msi_desc);
744 if (qp->msi_irq < 0) {
745 dev_warn(&qp->ndev->pdev->dev,
746 "Unable to allocate MSI interrupt for qp%d\n",
747 qp_num);
748 return;
749 }
750 }
751
752 rc = ntb_spad_write(qp->ndev, spad, qp->msi_desc.addr_offset);
753 if (rc)
754 goto err_free_interrupt;
755
756 rc = ntb_spad_write(qp->ndev, spad + 1, qp->msi_desc.data);
757 if (rc)
758 goto err_free_interrupt;
759
760 dev_dbg(&qp->ndev->pdev->dev, "QP%d MSI %d addr=%x data=%x\n",
761 qp_num, qp->msi_irq, qp->msi_desc.addr_offset,
762 qp->msi_desc.data);
763
764 return;
765
766err_free_interrupt:
767 devm_free_irq(&nt->ndev->dev, qp->msi_irq, qp);
768}
769
770static void ntb_transport_msi_peer_desc_changed(struct ntb_transport_ctx *nt)
771{
772 int i;
773
774 dev_dbg(&nt->ndev->pdev->dev, "Peer MSI descriptors changed");
775
776 for (i = 0; i < nt->qp_count; i++)
777 ntb_transport_setup_qp_peer_msi(nt, i);
778}
779
780static void ntb_transport_msi_desc_changed(void *data)
781{
782 struct ntb_transport_ctx *nt = data;
783 int i;
784
785 dev_dbg(&nt->ndev->pdev->dev, "MSI descriptors changed");
786
787 for (i = 0; i < nt->qp_count; i++)
788 ntb_transport_setup_qp_msi(nt, i);
789
790 ntb_peer_db_set(nt->ndev, nt->msi_db_mask);
791}
792
Allen Hubbee26a5842015-04-09 10:33:20 -0400793static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700794{
Allen Hubbee26a5842015-04-09 10:33:20 -0400795 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
796 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700797
798 if (!mw->virt_addr)
799 return;
800
Serge Semin443b9a12017-01-11 03:11:33 +0300801 ntb_mw_clear_trans(nt->ndev, PIDX, num_mw);
Aaron Sierrafc5d1822018-10-12 15:35:03 -0500802 dma_free_coherent(&pdev->dev, mw->alloc_size,
803 mw->alloc_addr, mw->dma_addr);
Allen Hubbee26a5842015-04-09 10:33:20 -0400804 mw->xlat_size = 0;
805 mw->buff_size = 0;
Aaron Sierrafc5d1822018-10-12 15:35:03 -0500806 mw->alloc_size = 0;
807 mw->alloc_addr = NULL;
Jon Mason113fc502013-01-30 11:40:52 -0700808 mw->virt_addr = NULL;
809}
810
Aaron Sierrafc5d1822018-10-12 15:35:03 -0500811static int ntb_alloc_mw_buffer(struct ntb_transport_mw *mw,
812 struct device *dma_dev, size_t align)
813{
814 dma_addr_t dma_addr;
815 void *alloc_addr, *virt_addr;
816 int rc;
817
818 alloc_addr = dma_alloc_coherent(dma_dev, mw->alloc_size,
819 &dma_addr, GFP_KERNEL);
820 if (!alloc_addr) {
821 dev_err(dma_dev, "Unable to alloc MW buff of size %zu\n",
822 mw->alloc_size);
823 return -ENOMEM;
824 }
825 virt_addr = alloc_addr;
826
827 /*
828 * we must ensure that the memory address allocated is BAR size
829 * aligned in order for the XLAT register to take the value. This
830 * is a requirement of the hardware. It is recommended to setup CMA
831 * for BAR sizes equal or greater than 4MB.
832 */
833 if (!IS_ALIGNED(dma_addr, align)) {
834 if (mw->alloc_size > mw->buff_size) {
835 virt_addr = PTR_ALIGN(alloc_addr, align);
836 dma_addr = ALIGN(dma_addr, align);
837 } else {
838 rc = -ENOMEM;
839 goto err;
840 }
841 }
842
843 mw->alloc_addr = alloc_addr;
844 mw->virt_addr = virt_addr;
845 mw->dma_addr = dma_addr;
846
847 return 0;
848
849err:
850 dma_free_coherent(dma_dev, mw->alloc_size, alloc_addr, dma_addr);
851
852 return rc;
853}
854
Allen Hubbee26a5842015-04-09 10:33:20 -0400855static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400856 resource_size_t size)
Jon Masonb77b2632013-02-01 15:25:37 -0700857{
Allen Hubbee26a5842015-04-09 10:33:20 -0400858 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
859 struct pci_dev *pdev = nt->ndev->pdev;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400860 size_t xlat_size, buff_size;
Logan Gunthorpe980c41c2017-08-03 12:19:44 -0600861 resource_size_t xlat_align;
862 resource_size_t xlat_align_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400863 int rc;
864
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400865 if (!size)
866 return -EINVAL;
867
Logan Gunthorpe980c41c2017-08-03 12:19:44 -0600868 rc = ntb_mw_get_align(nt->ndev, PIDX, num_mw, &xlat_align,
869 &xlat_align_size, NULL);
870 if (rc)
871 return rc;
872
873 xlat_size = round_up(size, xlat_align_size);
874 buff_size = round_up(size, xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700875
876 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400877 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700878 return 0;
879
Allen Hubbee26a5842015-04-09 10:33:20 -0400880 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700881 ntb_free_mw(nt, num_mw);
882
Allen Hubbee26a5842015-04-09 10:33:20 -0400883 /* Alloc memory for receiving data. Must be aligned */
884 mw->xlat_size = xlat_size;
885 mw->buff_size = buff_size;
Aaron Sierrafc5d1822018-10-12 15:35:03 -0500886 mw->alloc_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700887
Aaron Sierrafc5d1822018-10-12 15:35:03 -0500888 rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
889 if (rc) {
890 mw->alloc_size *= 2;
891 rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
892 if (rc) {
893 dev_err(&pdev->dev,
894 "Unable to alloc aligned MW buff\n");
895 mw->xlat_size = 0;
896 mw->buff_size = 0;
897 mw->alloc_size = 0;
898 return rc;
899 }
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700900 }
901
Jon Masonb77b2632013-02-01 15:25:37 -0700902 /* Notify HW the memory location of the receive buffer */
Serge Semin443b9a12017-01-11 03:11:33 +0300903 rc = ntb_mw_set_trans(nt->ndev, PIDX, num_mw, mw->dma_addr,
904 mw->xlat_size);
Allen Hubbee26a5842015-04-09 10:33:20 -0400905 if (rc) {
906 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
907 ntb_free_mw(nt, num_mw);
908 return -EIO;
909 }
Jon Masonb77b2632013-02-01 15:25:37 -0700910
911 return 0;
912}
913
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400914static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
915{
916 qp->link_is_up = false;
Dave Jiange9021332016-02-23 09:11:36 -0700917 qp->active = false;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400918
919 qp->tx_index = 0;
920 qp->rx_index = 0;
921 qp->rx_bytes = 0;
922 qp->rx_pkts = 0;
923 qp->rx_ring_empty = 0;
924 qp->rx_err_no_buf = 0;
925 qp->rx_err_oflow = 0;
926 qp->rx_err_ver = 0;
927 qp->rx_memcpy = 0;
928 qp->rx_async = 0;
929 qp->tx_bytes = 0;
930 qp->tx_pkts = 0;
931 qp->tx_ring_full = 0;
932 qp->tx_err_no_buf = 0;
933 qp->tx_memcpy = 0;
934 qp->tx_async = 0;
935}
936
Jon Masonfca4d512013-09-09 13:39:55 -0700937static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700938{
Allen Hubbee26a5842015-04-09 10:33:20 -0400939 struct ntb_transport_ctx *nt = qp->transport;
940 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700941
Allen Hubbee22e0b92015-05-12 06:55:44 -0400942 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400943
944 cancel_delayed_work_sync(&qp->link_work);
945 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400946
947 if (qp->event_handler)
948 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700949}
950
951static void ntb_qp_link_cleanup_work(struct work_struct *work)
952{
953 struct ntb_transport_qp *qp = container_of(work,
954 struct ntb_transport_qp,
955 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400956 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700957
958 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700959
Allen Hubbee26a5842015-04-09 10:33:20 -0400960 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700961 schedule_delayed_work(&qp->link_work,
962 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
963}
964
Jon Mason7b4f2d32013-01-19 02:02:19 -0700965static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700966{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700967 schedule_work(&qp->link_cleanup);
968}
969
Allen Hubbee26a5842015-04-09 10:33:20 -0400970static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700971{
Allen Hubbee26a5842015-04-09 10:33:20 -0400972 struct ntb_transport_qp *qp;
973 u64 qp_bitmap_alloc;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +0530974 unsigned int i, count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700975
Allen Hubbee26a5842015-04-09 10:33:20 -0400976 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700977
Allen Hubbee26a5842015-04-09 10:33:20 -0400978 /* Pass along the info to any clients */
979 for (i = 0; i < nt->qp_count; i++)
980 if (qp_bitmap_alloc & BIT_ULL(i)) {
981 qp = &nt->qp_vec[i];
982 ntb_qp_link_cleanup(qp);
983 cancel_work_sync(&qp->link_cleanup);
984 cancel_delayed_work_sync(&qp->link_work);
985 }
986
987 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700988 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700989
Joey Zhang91435952019-01-07 11:12:56 +0800990 for (i = 0; i < nt->mw_count; i++)
991 ntb_free_mw(nt, i);
992
Jon Masonfce8a7b2012-11-16 19:27:12 -0700993 /* The scratchpad registers keep the values if the remote side
994 * goes down, blast them now to give them a sane value the next
995 * time they are accessed
996 */
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +0530997 count = ntb_spad_count(nt->ndev);
998 for (i = 0; i < count; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400999 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001000}
1001
Jon Masonfca4d512013-09-09 13:39:55 -07001002static void ntb_transport_link_cleanup_work(struct work_struct *work)
1003{
Allen Hubbee26a5842015-04-09 10:33:20 -04001004 struct ntb_transport_ctx *nt =
1005 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -07001006
1007 ntb_transport_link_cleanup(nt);
1008}
1009
Allen Hubbee26a5842015-04-09 10:33:20 -04001010static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001011{
Allen Hubbee26a5842015-04-09 10:33:20 -04001012 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001013
Allen Hubbee26a5842015-04-09 10:33:20 -04001014 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001015 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -04001016 else
Jon Mason7b4f2d32013-01-19 02:02:19 -07001017 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001018}
1019
1020static void ntb_transport_link_work(struct work_struct *work)
1021{
Allen Hubbee26a5842015-04-09 10:33:20 -04001022 struct ntb_transport_ctx *nt =
1023 container_of(work, struct ntb_transport_ctx, link_work.work);
1024 struct ntb_dev *ndev = nt->ndev;
1025 struct pci_dev *pdev = ndev->pdev;
1026 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001027 u32 val;
Dave Jiang84f76682016-02-29 09:35:26 -07001028 int rc = 0, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001029
Jon Mason113fc502013-01-30 11:40:52 -07001030 /* send the local info, in the opposite order of the way we read it */
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -06001031
1032 if (nt->use_msi) {
1033 rc = ntb_msi_setup_mws(ndev);
1034 if (rc) {
1035 dev_warn(&pdev->dev,
1036 "Failed to register MSI memory window: %d\n",
1037 rc);
1038 nt->use_msi = false;
1039 }
1040 }
1041
1042 for (i = 0; i < nt->qp_count; i++)
1043 ntb_transport_setup_qp_msi(nt, i);
1044
Allen Hubbee26a5842015-04-09 10:33:20 -04001045 for (i = 0; i < nt->mw_count; i++) {
1046 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -07001047
Allen Hubbee26a5842015-04-09 10:33:20 -04001048 if (max_mw_size && size > max_mw_size)
1049 size = max_mw_size;
1050
1051 spad = MW0_SZ_HIGH + (i * 2);
Serge Semind67288a2017-01-11 03:13:20 +03001052 ntb_peer_spad_write(ndev, PIDX, spad, upper_32_bits(size));
Allen Hubbee26a5842015-04-09 10:33:20 -04001053
1054 spad = MW0_SZ_LOW + (i * 2);
Serge Semind67288a2017-01-11 03:13:20 +03001055 ntb_peer_spad_write(ndev, PIDX, spad, lower_32_bits(size));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001056 }
1057
Serge Semind67288a2017-01-11 03:13:20 +03001058 ntb_peer_spad_write(ndev, PIDX, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001059
Serge Semind67288a2017-01-11 03:13:20 +03001060 ntb_peer_spad_write(ndev, PIDX, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001061
Serge Semind67288a2017-01-11 03:13:20 +03001062 ntb_peer_spad_write(ndev, PIDX, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001063
1064 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -04001065 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -04001066 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001067 if (val != NTB_TRANSPORT_VERSION)
1068 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001069
Dave Jiang0f69a7d2015-06-02 03:45:07 -04001070 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001071 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -04001072 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001073 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001074
Dave Jiang0f69a7d2015-06-02 03:45:07 -04001075 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -07001076 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -04001077 if (val != nt->mw_count)
1078 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001079
Allen Hubbee26a5842015-04-09 10:33:20 -04001080 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -07001081 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001082
Dave Jiang0f69a7d2015-06-02 03:45:07 -04001083 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -04001084 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -07001085
Dave Jiang0f69a7d2015-06-02 03:45:07 -04001086 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -07001087 val64 |= val;
1088
Allen Hubbee26a5842015-04-09 10:33:20 -04001089 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -07001090
1091 rc = ntb_set_mw(nt, i, val64);
1092 if (rc)
1093 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001094 }
1095
Allen Hubbee26a5842015-04-09 10:33:20 -04001096 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001097
Allen Hubbee26a5842015-04-09 10:33:20 -04001098 for (i = 0; i < nt->qp_count; i++) {
1099 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -07001100
1101 ntb_transport_setup_qp_mw(nt, i);
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -06001102 ntb_transport_setup_qp_peer_msi(nt, i);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001103
Allen Hubbee26a5842015-04-09 10:33:20 -04001104 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001105 schedule_delayed_work(&qp->link_work, 0);
1106 }
1107
1108 return;
1109
Jon Mason113fc502013-01-30 11:40:52 -07001110out1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001111 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -07001112 ntb_free_mw(nt, i);
Dave Jiang84f76682016-02-29 09:35:26 -07001113
1114 /* if there's an actual failure, we should just bail */
Dave Jiangf3fd2af2017-07-28 15:10:48 -07001115 if (rc < 0)
Dave Jiang84f76682016-02-29 09:35:26 -07001116 return;
Dave Jiang84f76682016-02-29 09:35:26 -07001117
Jon Masonfce8a7b2012-11-16 19:27:12 -07001118out:
Allen Hubbee26a5842015-04-09 10:33:20 -04001119 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001120 schedule_delayed_work(&nt->link_work,
1121 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
1122}
1123
1124static void ntb_qp_link_work(struct work_struct *work)
1125{
1126 struct ntb_transport_qp *qp = container_of(work,
1127 struct ntb_transport_qp,
1128 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001129 struct pci_dev *pdev = qp->ndev->pdev;
1130 struct ntb_transport_ctx *nt = qp->transport;
1131 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001132
Allen Hubbee26a5842015-04-09 10:33:20 -04001133 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001134
Allen Hubbee26a5842015-04-09 10:33:20 -04001135 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001136
Serge Semind67288a2017-01-11 03:13:20 +03001137 ntb_peer_spad_write(nt->ndev, PIDX, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001138
1139 /* query remote spad for qp ready bits */
Allen Hubbe28762282015-05-11 10:08:26 -04001140 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001141
1142 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -04001143 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001144 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -04001145 qp->link_is_up = true;
Dave Jiange9021332016-02-23 09:11:36 -07001146 qp->active = true;
Allen Hubbee26a5842015-04-09 10:33:20 -04001147
Jon Masonfce8a7b2012-11-16 19:27:12 -07001148 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -04001149 qp->event_handler(qp->cb_data, qp->link_is_up);
Allen Hubbe8b5a22d2015-07-13 08:07:12 -04001150
Dave Jiange9021332016-02-23 09:11:36 -07001151 if (qp->active)
1152 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001153 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001154 schedule_delayed_work(&qp->link_work,
1155 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
1156}
1157
Allen Hubbee26a5842015-04-09 10:33:20 -04001158static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -07001159 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001160{
1161 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001162 phys_addr_t mw_base;
1163 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -07001164 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -04001165 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -07001166 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -07001167
Allen Hubbee26a5842015-04-09 10:33:20 -04001168 mw_count = nt->mw_count;
1169 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001170
Allen Hubbee26a5842015-04-09 10:33:20 -04001171 mw_num = QP_TO_MW(nt, qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -04001172
1173 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -07001174 qp->qp_num = qp_num;
1175 qp->transport = nt;
1176 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -04001177 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001178 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001179 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001180
Logan Gunthorpe8e8496e2017-06-05 14:00:53 -06001181 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -04001182 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -07001183 else
Allen Hubbee26a5842015-04-09 10:33:20 -04001184 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -07001185
Allen Hubbee26a5842015-04-09 10:33:20 -04001186 mw_base = nt->mw_vec[mw_num].phys_addr;
1187 mw_size = nt->mw_vec[mw_num].phys_size;
1188
Logan Gunthorpecbd27442017-12-18 11:25:05 -07001189 if (max_mw_size && mw_size > max_mw_size)
1190 mw_size = max_mw_size;
1191
Allen Hubbee26a5842015-04-09 10:33:20 -04001192 tx_size = (unsigned int)mw_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -04001193 qp_offset = tx_size * (qp_num / mw_count);
Allen Hubbee26a5842015-04-09 10:33:20 -04001194
Logan Gunthorpec59666b2019-01-18 17:10:01 -07001195 qp->tx_mw_size = tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -04001196 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001197 if (!qp->tx_mw)
1198 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -07001199
Allen Hubbee26a5842015-04-09 10:33:20 -04001200 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001201 if (!qp->tx_mw_phys)
1202 return -EINVAL;
1203
1204 tx_size -= sizeof(struct ntb_rx_info);
1205 qp->rx_info = qp->tx_mw + tx_size;
1206
Jon Masonc9d534c2013-02-01 15:45:16 -07001207 /* Due to housekeeping, there must be atleast 2 buffs */
1208 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -07001209 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -07001210
Dave Jiangc8650fd2015-07-13 08:07:09 -04001211 if (nt->debugfs_node_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001212 char debugfs_name[4];
1213
1214 snprintf(debugfs_name, 4, "qp%d", qp_num);
1215 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Dave Jiangc8650fd2015-07-13 08:07:09 -04001216 nt->debugfs_node_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001217
1218 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1219 qp->debugfs_dir, qp,
1220 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -04001221 } else {
1222 qp->debugfs_dir = NULL;
1223 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001224 }
1225
1226 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001227 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001228
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001229 spin_lock_init(&qp->ntb_rx_q_lock);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001230 spin_lock_init(&qp->ntb_tx_free_q_lock);
1231
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001232 INIT_LIST_HEAD(&qp->rx_post_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001233 INIT_LIST_HEAD(&qp->rx_pend_q);
1234 INIT_LIST_HEAD(&qp->rx_free_q);
1235 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001236
Allen Hubbee26a5842015-04-09 10:33:20 -04001237 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1238 (unsigned long)qp);
1239
Jon Mason282a2fe2013-02-12 09:52:50 -07001240 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001241}
1242
Allen Hubbee26a5842015-04-09 10:33:20 -04001243static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001244{
Allen Hubbee26a5842015-04-09 10:33:20 -04001245 struct ntb_transport_ctx *nt;
1246 struct ntb_transport_mw *mw;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301247 unsigned int mw_count, qp_count, spad_count, max_mw_count_for_spads;
Allen Hubbee26a5842015-04-09 10:33:20 -04001248 u64 qp_bitmap;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001249 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001250 int rc, i;
1251
Logan Gunthorpebc240ee2017-06-26 13:50:41 -06001252 mw_count = ntb_peer_mw_count(ndev);
Serge Semin443b9a12017-01-11 03:11:33 +03001253
1254 if (!ndev->ops->mw_set_trans) {
1255 dev_err(&ndev->dev, "Inbound MW based NTB API is required\n");
1256 return -EINVAL;
1257 }
Logan Gunthorpe19645a02016-06-07 11:20:22 -06001258
Allen Hubbee26a5842015-04-09 10:33:20 -04001259 if (ntb_db_is_unsafe(ndev))
1260 dev_dbg(&ndev->dev,
1261 "doorbell is unsafe, proceed anyway...\n");
1262 if (ntb_spad_is_unsafe(ndev))
1263 dev_dbg(&ndev->dev,
1264 "scratchpad is unsafe, proceed anyway...\n");
1265
Serge Semin1e530112016-12-14 02:49:14 +03001266 if (ntb_peer_port_count(ndev) != NTB_DEF_PEER_CNT)
1267 dev_warn(&ndev->dev, "Multi-port NTB devices unsupported\n");
1268
Allen Hubbe1199aa62015-05-18 06:20:47 -04001269 node = dev_to_node(&ndev->dev);
1270
1271 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001272 if (!nt)
1273 return -ENOMEM;
1274
Allen Hubbee26a5842015-04-09 10:33:20 -04001275 nt->ndev = ndev;
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -06001276
1277 /*
1278 * If we are using MSI, and have at least one extra memory window,
1279 * we will reserve the last MW for the MSI window.
1280 */
1281 if (use_msi && mw_count > 1) {
1282 rc = ntb_msi_init(ndev, ntb_transport_msi_desc_changed);
1283 if (!rc) {
1284 mw_count -= 1;
1285 nt->use_msi = true;
1286 }
1287 }
1288
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301289 spad_count = ntb_spad_count(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04001290
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301291 /* Limit the MW's based on the availability of scratchpads */
1292
1293 if (spad_count < NTB_TRANSPORT_MIN_SPADS) {
1294 nt->mw_count = 0;
1295 rc = -EINVAL;
1296 goto err;
1297 }
1298
1299 max_mw_count_for_spads = (spad_count - MW0_SZ_HIGH) / 2;
1300 nt->mw_count = min(mw_count, max_mw_count_for_spads);
Allen Hubbee26a5842015-04-09 10:33:20 -04001301
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -06001302 nt->msi_spad_offset = nt->mw_count * 2 + MW0_SZ_HIGH;
1303
Kees Cook590b5b72018-06-12 14:04:20 -07001304 nt->mw_vec = kcalloc_node(mw_count, sizeof(*nt->mw_vec),
Allen Hubbe1199aa62015-05-18 06:20:47 -04001305 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001306 if (!nt->mw_vec) {
1307 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001308 goto err;
1309 }
1310
Allen Hubbee26a5842015-04-09 10:33:20 -04001311 for (i = 0; i < mw_count; i++) {
1312 mw = &nt->mw_vec[i];
1313
Serge Semin443b9a12017-01-11 03:11:33 +03001314 rc = ntb_peer_mw_get_addr(ndev, i, &mw->phys_addr,
1315 &mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001316 if (rc)
1317 goto err1;
1318
Dave Jiang06917f72015-05-19 16:45:46 -04001319 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001320 if (!mw->vbase) {
1321 rc = -ENOMEM;
1322 goto err1;
1323 }
1324
1325 mw->buff_size = 0;
1326 mw->xlat_size = 0;
1327 mw->virt_addr = NULL;
1328 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -07001329 }
1330
Allen Hubbee26a5842015-04-09 10:33:20 -04001331 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001332
Allen Hubbee26a5842015-04-09 10:33:20 -04001333 qp_count = ilog2(qp_bitmap);
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -06001334 if (nt->use_msi) {
1335 qp_count -= 1;
1336 nt->msi_db_mask = 1 << qp_count;
1337 ntb_db_clear_mask(ndev, nt->msi_db_mask);
1338 }
1339
Allen Hubbee26a5842015-04-09 10:33:20 -04001340 if (max_num_clients && max_num_clients < qp_count)
1341 qp_count = max_num_clients;
Logan Gunthorpecb827ee2017-06-05 14:00:52 -06001342 else if (nt->mw_count < qp_count)
1343 qp_count = nt->mw_count;
Allen Hubbee26a5842015-04-09 10:33:20 -04001344
1345 qp_bitmap &= BIT_ULL(qp_count) - 1;
1346
1347 nt->qp_count = qp_count;
1348 nt->qp_bitmap = qp_bitmap;
1349 nt->qp_bitmap_free = qp_bitmap;
1350
Kees Cook590b5b72018-06-12 14:04:20 -07001351 nt->qp_vec = kcalloc_node(qp_count, sizeof(*nt->qp_vec),
Allen Hubbe1199aa62015-05-18 06:20:47 -04001352 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001353 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001354 rc = -ENOMEM;
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301355 goto err1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001356 }
1357
Dave Jiangc8650fd2015-07-13 08:07:09 -04001358 if (nt_debugfs_dir) {
1359 nt->debugfs_node_dir =
1360 debugfs_create_dir(pci_name(ndev->pdev),
1361 nt_debugfs_dir);
1362 }
1363
Allen Hubbee26a5842015-04-09 10:33:20 -04001364 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001365 rc = ntb_transport_init_queue(nt, i);
1366 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301367 goto err2;
Jon Mason282a2fe2013-02-12 09:52:50 -07001368 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001369
1370 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001371 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001372
Allen Hubbee26a5842015-04-09 10:33:20 -04001373 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001374 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301375 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001376
1377 INIT_LIST_HEAD(&nt->client_devs);
1378 rc = ntb_bus_init(nt);
1379 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301380 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001381
Allen Hubbee26a5842015-04-09 10:33:20 -04001382 nt->link_is_up = false;
1383 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1384 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001385
1386 return 0;
1387
Jon Mason948d3a62013-04-18 17:07:36 -07001388err3:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301389 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001390err2:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301391 kfree(nt->qp_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001392err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001393 while (i--) {
1394 mw = &nt->mw_vec[i];
1395 iounmap(mw->vbase);
1396 }
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301397 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001398err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001399 kfree(nt);
1400 return rc;
1401}
1402
Allen Hubbee26a5842015-04-09 10:33:20 -04001403static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001404{
Allen Hubbee26a5842015-04-09 10:33:20 -04001405 struct ntb_transport_ctx *nt = ndev->ctx;
1406 struct ntb_transport_qp *qp;
1407 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001408 int i;
1409
Jon Masonfca4d512013-09-09 13:39:55 -07001410 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001411 cancel_work_sync(&nt->link_cleanup);
1412 cancel_delayed_work_sync(&nt->link_work);
1413
1414 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001415
1416 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001417 for (i = 0; i < nt->qp_count; i++) {
1418 qp = &nt->qp_vec[i];
1419 if (qp_bitmap_alloc & BIT_ULL(i))
1420 ntb_transport_free_queue(qp);
1421 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001422 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001423
Allen Hubbee26a5842015-04-09 10:33:20 -04001424 ntb_link_disable(ndev);
1425 ntb_clear_ctx(ndev);
1426
Jon Masonfce8a7b2012-11-16 19:27:12 -07001427 ntb_bus_remove(nt);
1428
Allen Hubbee26a5842015-04-09 10:33:20 -04001429 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001430 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001431 iounmap(nt->mw_vec[i].vbase);
1432 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001433
Allen Hubbee26a5842015-04-09 10:33:20 -04001434 kfree(nt->qp_vec);
1435 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001436 kfree(nt);
1437}
1438
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001439static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1440{
1441 struct ntb_queue_entry *entry;
1442 void *cb_data;
1443 unsigned int len;
1444 unsigned long irqflags;
1445
1446 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1447
1448 while (!list_empty(&qp->rx_post_q)) {
1449 entry = list_first_entry(&qp->rx_post_q,
1450 struct ntb_queue_entry, entry);
1451 if (!(entry->flags & DESC_DONE_FLAG))
1452 break;
1453
1454 entry->rx_hdr->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07001455 iowrite32(entry->rx_index, &qp->rx_info->entry);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001456
1457 cb_data = entry->cb_data;
1458 len = entry->len;
1459
1460 list_move_tail(&entry->entry, &qp->rx_free_q);
1461
1462 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1463
1464 if (qp->rx_handler && qp->client_ready)
1465 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1466
1467 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1468 }
1469
1470 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1471}
1472
Dave Jiang72203572016-07-20 13:14:13 -07001473static void ntb_rx_copy_callback(void *data,
1474 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001475{
Jon Mason282a2fe2013-02-12 09:52:50 -07001476 struct ntb_queue_entry *entry = data;
Jon Mason448c6fb2013-01-19 02:02:27 -07001477
Dave Jiang72203572016-07-20 13:14:13 -07001478 /* we need to check DMA results if we are using DMA */
1479 if (res) {
1480 enum dmaengine_tx_result dma_err = res->result;
1481
1482 switch (dma_err) {
1483 case DMA_TRANS_READ_FAILED:
1484 case DMA_TRANS_WRITE_FAILED:
1485 entry->errors++;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001486 fallthrough;
Dave Jiang72203572016-07-20 13:14:13 -07001487 case DMA_TRANS_ABORTED:
1488 {
1489 struct ntb_transport_qp *qp = entry->qp;
1490 void *offset = qp->rx_buff + qp->rx_max_frame *
1491 qp->rx_index;
1492
1493 ntb_memcpy_rx(entry, offset);
1494 qp->rx_memcpy++;
1495 return;
1496 }
1497
1498 case DMA_TRANS_NOERROR:
1499 default:
1500 break;
1501 }
1502 }
1503
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001504 entry->flags |= DESC_DONE_FLAG;
Jon Mason282a2fe2013-02-12 09:52:50 -07001505
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001506 ntb_complete_rxc(entry->qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001507}
1508
Jon Mason282a2fe2013-02-12 09:52:50 -07001509static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1510{
1511 void *buf = entry->buf;
1512 size_t len = entry->len;
1513
1514 memcpy(buf, offset, len);
1515
Allen Hubbee26a5842015-04-09 10:33:20 -04001516 /* Ensure that the data is fully copied out before clearing the flag */
1517 wmb();
1518
Dave Jiang72203572016-07-20 13:14:13 -07001519 ntb_rx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001520}
1521
Dave Jiang72203572016-07-20 13:14:13 -07001522static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
Jon Mason282a2fe2013-02-12 09:52:50 -07001523{
1524 struct dma_async_tx_descriptor *txd;
1525 struct ntb_transport_qp *qp = entry->qp;
Dave Jiang569410ca2015-07-13 08:07:22 -04001526 struct dma_chan *chan = qp->rx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001527 struct dma_device *device;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001528 size_t pay_off, buff_off, len;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001529 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001530 dma_cookie_t cookie;
1531 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001532
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001533 len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001534 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001535 pay_off = (size_t)offset & ~PAGE_MASK;
1536 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001537
1538 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Allen Hubbe905921e2015-07-13 08:07:21 -04001539 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001540
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001541 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1542 if (!unmap)
Allen Hubbe905921e2015-07-13 08:07:21 -04001543 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001544
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001545 unmap->len = len;
1546 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1547 pay_off, len, DMA_TO_DEVICE);
1548 if (dma_mapping_error(device->dev, unmap->addr[0]))
1549 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001550
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001551 unmap->to_cnt = 1;
1552
1553 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1554 buff_off, len, DMA_FROM_DEVICE);
1555 if (dma_mapping_error(device->dev, unmap->addr[1]))
1556 goto err_get_unmap;
1557
1558 unmap->from_cnt = 1;
1559
Allen Hubbe88931ec2017-06-09 18:06:36 -04001560 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
1561 unmap->addr[0], len,
1562 DMA_PREP_INTERRUPT);
1563 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001564 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001565
Dave Jiang72203572016-07-20 13:14:13 -07001566 txd->callback_result = ntb_rx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001567 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001568 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001569
1570 cookie = dmaengine_submit(txd);
1571 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001572 goto err_set_unmap;
1573
1574 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001575
1576 qp->last_cookie = cookie;
1577
1578 qp->rx_async++;
1579
Dave Jiang72203572016-07-20 13:14:13 -07001580 return 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001581
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001582err_set_unmap:
1583 dmaengine_unmap_put(unmap);
1584err_get_unmap:
1585 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001586err:
Dave Jiang72203572016-07-20 13:14:13 -07001587 return -ENXIO;
1588}
1589
1590static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
1591{
1592 struct ntb_transport_qp *qp = entry->qp;
1593 struct dma_chan *chan = qp->rx_dma_chan;
1594 int res;
1595
1596 if (!chan)
1597 goto err;
1598
1599 if (entry->len < copy_bytes)
1600 goto err;
1601
1602 res = ntb_async_rx_submit(entry, offset);
1603 if (res < 0)
1604 goto err;
1605
1606 if (!entry->retries)
1607 qp->rx_async++;
1608
1609 return;
1610
1611err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001612 ntb_memcpy_rx(entry, offset);
1613 qp->rx_memcpy++;
1614}
1615
Jon Masonfce8a7b2012-11-16 19:27:12 -07001616static int ntb_process_rxc(struct ntb_transport_qp *qp)
1617{
1618 struct ntb_payload_header *hdr;
1619 struct ntb_queue_entry *entry;
1620 void *offset;
1621
Jon Mason793c20e2013-01-19 02:02:26 -07001622 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1623 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1624
Allen Hubbee26a5842015-04-09 10:33:20 -04001625 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1626 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001627
Jon Masonfce8a7b2012-11-16 19:27:12 -07001628 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001629 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001630 qp->rx_ring_empty++;
1631 return -EAGAIN;
1632 }
1633
Allen Hubbee26a5842015-04-09 10:33:20 -04001634 if (hdr->flags & LINK_DOWN_FLAG) {
1635 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1636 ntb_qp_link_down(qp);
1637 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001638 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001639 }
1640
1641 if (hdr->ver != (u32)qp->rx_pkts) {
1642 dev_dbg(&qp->ndev->pdev->dev,
1643 "version mismatch, expected %llu - got %u\n",
1644 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001645 qp->rx_err_ver++;
1646 return -EIO;
1647 }
1648
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001649 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
Allen Hubbee26a5842015-04-09 10:33:20 -04001650 if (!entry) {
1651 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1652 qp->rx_err_no_buf++;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001653 return -EAGAIN;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001654 }
1655
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001656 entry->rx_hdr = hdr;
Dave Jiang72203572016-07-20 13:14:13 -07001657 entry->rx_index = qp->rx_index;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001658
Allen Hubbee26a5842015-04-09 10:33:20 -04001659 if (hdr->len > entry->len) {
1660 dev_dbg(&qp->ndev->pdev->dev,
1661 "receive buffer overflow! Wanted %d got %d\n",
1662 hdr->len, entry->len);
1663 qp->rx_err_oflow++;
1664
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001665 entry->len = -EIO;
1666 entry->flags |= DESC_DONE_FLAG;
1667
1668 ntb_complete_rxc(qp);
1669 } else {
1670 dev_dbg(&qp->ndev->pdev->dev,
1671 "RX OK index %u ver %u size %d into buf size %d\n",
1672 qp->rx_index, hdr->ver, hdr->len, entry->len);
1673
1674 qp->rx_bytes += hdr->len;
1675 qp->rx_pkts++;
1676
1677 entry->len = hdr->len;
1678
1679 ntb_async_rx(entry, offset);
Allen Hubbee26a5842015-04-09 10:33:20 -04001680 }
1681
Jon Mason282a2fe2013-02-12 09:52:50 -07001682 qp->rx_index++;
1683 qp->rx_index %= qp->rx_max_entry;
1684
1685 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001686}
1687
Allen Hubbee26a5842015-04-09 10:33:20 -04001688static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001689{
Allen Hubbee26a5842015-04-09 10:33:20 -04001690 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001691 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001692
Allen Hubbee26a5842015-04-09 10:33:20 -04001693 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1694 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001695
Jon Masonc336acd2013-01-17 15:28:45 -07001696 /* Limit the number of packets processed in a single interrupt to
1697 * provide fairness to others
1698 */
1699 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001700 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001701 if (rc)
1702 break;
1703 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001704
Dave Jiang569410ca2015-07-13 08:07:22 -04001705 if (i && qp->rx_dma_chan)
1706 dma_async_issue_pending(qp->rx_dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001707
Allen Hubbee26a5842015-04-09 10:33:20 -04001708 if (i == qp->rx_max_entry) {
1709 /* there is more work to do */
Dave Jiange9021332016-02-23 09:11:36 -07001710 if (qp->active)
1711 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001712 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1713 /* the doorbell bit is set: clear it */
1714 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1715 /* ntb_db_read ensures ntb_db_clear write is committed */
1716 ntb_db_read(qp->ndev);
1717
1718 /* an interrupt may have arrived between finishing
1719 * ntb_process_rxc and clearing the doorbell bit:
1720 * there might be some more work to do.
1721 */
Dave Jiange9021332016-02-23 09:11:36 -07001722 if (qp->active)
1723 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001724 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001725}
1726
Dave Jiang9cabc262016-07-20 13:14:07 -07001727static void ntb_tx_copy_callback(void *data,
1728 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001729{
Jon Mason282a2fe2013-02-12 09:52:50 -07001730 struct ntb_queue_entry *entry = data;
1731 struct ntb_transport_qp *qp = entry->qp;
1732 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001733
Dave Jiang9cabc262016-07-20 13:14:07 -07001734 /* we need to check DMA results if we are using DMA */
1735 if (res) {
1736 enum dmaengine_tx_result dma_err = res->result;
1737
1738 switch (dma_err) {
1739 case DMA_TRANS_READ_FAILED:
1740 case DMA_TRANS_WRITE_FAILED:
1741 entry->errors++;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001742 fallthrough;
Dave Jiang9cabc262016-07-20 13:14:07 -07001743 case DMA_TRANS_ABORTED:
1744 {
1745 void __iomem *offset =
1746 qp->tx_mw + qp->tx_max_frame *
1747 entry->tx_index;
1748
1749 /* resubmit via CPU */
1750 ntb_memcpy_tx(entry, offset);
1751 qp->tx_memcpy++;
1752 return;
1753 }
1754
1755 case DMA_TRANS_NOERROR:
1756 default:
1757 break;
1758 }
1759 }
1760
Jon Mason74465642013-01-21 15:28:52 -07001761 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001762
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -06001763 if (qp->use_msi)
1764 ntb_msi_peer_trigger(qp->ndev, PIDX, &qp->peer_msi_desc);
1765 else
1766 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001767
1768 /* The entry length can only be zero if the packet is intended to be a
1769 * "link down" or similar. Since no payload is being sent in these
1770 * cases, there is nothing to add to the completion queue.
1771 */
1772 if (entry->len > 0) {
1773 qp->tx_bytes += entry->len;
1774
1775 if (qp->tx_handler)
1776 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1777 entry->len);
1778 }
1779
1780 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1781}
1782
Jon Mason282a2fe2013-02-12 09:52:50 -07001783static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1784{
Dave Jiang06917f72015-05-19 16:45:46 -04001785#ifdef ARCH_HAS_NOCACHE_UACCESS
1786 /*
1787 * Using non-temporal mov to improve performance on non-cached
1788 * writes, even though we aren't actually copying from user space.
1789 */
1790 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1791#else
Jon Mason282a2fe2013-02-12 09:52:50 -07001792 memcpy_toio(offset, entry->buf, entry->len);
Dave Jiang06917f72015-05-19 16:45:46 -04001793#endif
Jon Mason282a2fe2013-02-12 09:52:50 -07001794
Allen Hubbee26a5842015-04-09 10:33:20 -04001795 /* Ensure that the data is fully copied out before setting the flags */
1796 wmb();
1797
Dave Jiang9cabc262016-07-20 13:14:07 -07001798 ntb_tx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001799}
1800
Dave Jiang9cabc262016-07-20 13:14:07 -07001801static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1802 struct ntb_queue_entry *entry)
Jon Mason282a2fe2013-02-12 09:52:50 -07001803{
Jon Mason282a2fe2013-02-12 09:52:50 -07001804 struct dma_async_tx_descriptor *txd;
Dave Jiang569410ca2015-07-13 08:07:22 -04001805 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001806 struct dma_device *device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001807 size_t len = entry->len;
1808 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001809 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001810 struct dmaengine_unmap_data *unmap;
1811 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001812 dma_cookie_t cookie;
Jon Mason282a2fe2013-02-12 09:52:50 -07001813
Jon Mason282a2fe2013-02-12 09:52:50 -07001814 device = chan->device;
Logan Gunthorpec59666b2019-01-18 17:10:01 -07001815 dest = qp->tx_mw_dma_addr + qp->tx_max_frame * entry->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001816 buff_off = (size_t)buf & ~PAGE_MASK;
1817 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001818
1819 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1820 goto err;
1821
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001822 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1823 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001824 goto err;
1825
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001826 unmap->len = len;
1827 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1828 buff_off, len, DMA_TO_DEVICE);
1829 if (dma_mapping_error(device->dev, unmap->addr[0]))
1830 goto err_get_unmap;
1831
1832 unmap->to_cnt = 1;
1833
Allen Hubbe88931ec2017-06-09 18:06:36 -04001834 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
1835 DMA_PREP_INTERRUPT);
1836 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001837 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001838
Dave Jiang9cabc262016-07-20 13:14:07 -07001839 txd->callback_result = ntb_tx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001840 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001841 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001842
1843 cookie = dmaengine_submit(txd);
1844 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001845 goto err_set_unmap;
1846
1847 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001848
1849 dma_async_issue_pending(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001850
Dave Jiang9cabc262016-07-20 13:14:07 -07001851 return 0;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001852err_set_unmap:
1853 dmaengine_unmap_put(unmap);
1854err_get_unmap:
1855 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001856err:
Dave Jiang9cabc262016-07-20 13:14:07 -07001857 return -ENXIO;
1858}
1859
1860static void ntb_async_tx(struct ntb_transport_qp *qp,
1861 struct ntb_queue_entry *entry)
1862{
1863 struct ntb_payload_header __iomem *hdr;
1864 struct dma_chan *chan = qp->tx_dma_chan;
1865 void __iomem *offset;
1866 int res;
1867
1868 entry->tx_index = qp->tx_index;
1869 offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
1870 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1871 entry->tx_hdr = hdr;
1872
1873 iowrite32(entry->len, &hdr->len);
1874 iowrite32((u32)qp->tx_pkts, &hdr->ver);
1875
1876 if (!chan)
1877 goto err;
1878
1879 if (entry->len < copy_bytes)
1880 goto err;
1881
1882 res = ntb_async_tx_submit(qp, entry);
1883 if (res < 0)
1884 goto err;
1885
1886 if (!entry->retries)
1887 qp->tx_async++;
1888
1889 return;
1890
1891err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001892 ntb_memcpy_tx(entry, offset);
1893 qp->tx_memcpy++;
1894}
1895
Jon Masonfce8a7b2012-11-16 19:27:12 -07001896static int ntb_process_tx(struct ntb_transport_qp *qp,
1897 struct ntb_queue_entry *entry)
1898{
Jon Mason793c20e2013-01-19 02:02:26 -07001899 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001900 qp->tx_ring_full++;
1901 return -EAGAIN;
1902 }
1903
Jon Masonef114ed2013-01-19 02:02:18 -07001904 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001905 if (qp->tx_handler)
Jon Mason179f9122015-12-18 13:22:37 -05001906 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001907
1908 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1909 &qp->tx_free_q);
1910 return 0;
1911 }
1912
Jon Mason282a2fe2013-02-12 09:52:50 -07001913 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001914
Jon Mason793c20e2013-01-19 02:02:26 -07001915 qp->tx_index++;
1916 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001917
1918 qp->tx_pkts++;
1919
1920 return 0;
1921}
1922
1923static void ntb_send_link_down(struct ntb_transport_qp *qp)
1924{
Allen Hubbee26a5842015-04-09 10:33:20 -04001925 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001926 struct ntb_queue_entry *entry;
1927 int i, rc;
1928
Allen Hubbee26a5842015-04-09 10:33:20 -04001929 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001930 return;
1931
Allen Hubbee22e0b92015-05-12 06:55:44 -04001932 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001933
1934 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001935 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001936 if (entry)
1937 break;
1938 msleep(100);
1939 }
1940
1941 if (!entry)
1942 return;
1943
1944 entry->cb_data = NULL;
1945 entry->buf = NULL;
1946 entry->len = 0;
1947 entry->flags = LINK_DOWN_FLAG;
1948
1949 rc = ntb_process_tx(qp, entry);
1950 if (rc)
1951 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1952 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001953
1954 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001955}
1956
Allen Hubbe1199aa62015-05-18 06:20:47 -04001957static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1958{
1959 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1960}
1961
Jon Masonfce8a7b2012-11-16 19:27:12 -07001962/**
1963 * ntb_transport_create_queue - Create a new NTB transport layer queue
1964 * @rx_handler: receive callback function
1965 * @tx_handler: transmit callback function
1966 * @event_handler: event callback function
1967 *
1968 * Create a new NTB transport layer queue and provide the queue with a callback
1969 * routine for both transmit and receive. The receive callback routine will be
1970 * used to pass up data when the transport has received it on the queue. The
1971 * transmit callback routine will be called when the transport has completed the
1972 * transmission of the data on the queue and the data is ready to be freed.
1973 *
1974 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1975 */
1976struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001977ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001978 const struct ntb_queue_handlers *handlers)
1979{
Allen Hubbee26a5842015-04-09 10:33:20 -04001980 struct ntb_dev *ndev;
1981 struct pci_dev *pdev;
1982 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001983 struct ntb_queue_entry *entry;
1984 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001985 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001986 unsigned int free_queue;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001987 dma_cap_mask_t dma_mask;
1988 int node;
Allen Hubbee26a5842015-04-09 10:33:20 -04001989 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001990
Allen Hubbee26a5842015-04-09 10:33:20 -04001991 ndev = dev_ntb(client_dev->parent);
1992 pdev = ndev->pdev;
1993 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001994
Allen Hubbe1199aa62015-05-18 06:20:47 -04001995 node = dev_to_node(&ndev->dev);
1996
Thomas VanSelus8fcd0952017-02-13 16:46:26 -06001997 free_queue = ffs(nt->qp_bitmap_free);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001998 if (!free_queue)
1999 goto err;
2000
2001 /* decrement free_queue to make it zero based */
2002 free_queue--;
2003
Allen Hubbee26a5842015-04-09 10:33:20 -04002004 qp = &nt->qp_vec[free_queue];
2005 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002006
Allen Hubbee26a5842015-04-09 10:33:20 -04002007 nt->qp_bitmap_free &= ~qp_bit;
2008
Jon Masonfce8a7b2012-11-16 19:27:12 -07002009 qp->cb_data = data;
2010 qp->rx_handler = handlers->rx_handler;
2011 qp->tx_handler = handlers->tx_handler;
2012 qp->event_handler = handlers->event_handler;
2013
Allen Hubbe1199aa62015-05-18 06:20:47 -04002014 dma_cap_zero(dma_mask);
2015 dma_cap_set(DMA_MEMCPY, dma_mask);
2016
Dave Jianga41ef052015-05-19 16:52:04 -04002017 if (use_dma) {
Dave Jiang569410ca2015-07-13 08:07:22 -04002018 qp->tx_dma_chan =
2019 dma_request_channel(dma_mask, ntb_dma_filter_fn,
2020 (void *)(unsigned long)node);
2021 if (!qp->tx_dma_chan)
2022 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
2023
2024 qp->rx_dma_chan =
2025 dma_request_channel(dma_mask, ntb_dma_filter_fn,
2026 (void *)(unsigned long)node);
2027 if (!qp->rx_dma_chan)
2028 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
Dave Jianga41ef052015-05-19 16:52:04 -04002029 } else {
Dave Jiang569410ca2015-07-13 08:07:22 -04002030 qp->tx_dma_chan = NULL;
2031 qp->rx_dma_chan = NULL;
Dave Jianga41ef052015-05-19 16:52:04 -04002032 }
Dave Jiang569410ca2015-07-13 08:07:22 -04002033
Logan Gunthorpe51cb8db2019-02-19 12:56:36 -07002034 qp->tx_mw_dma_addr = 0;
Logan Gunthorpec59666b2019-01-18 17:10:01 -07002035 if (qp->tx_dma_chan) {
2036 qp->tx_mw_dma_addr =
2037 dma_map_resource(qp->tx_dma_chan->device->dev,
2038 qp->tx_mw_phys, qp->tx_mw_size,
2039 DMA_FROM_DEVICE, 0);
2040 if (dma_mapping_error(qp->tx_dma_chan->device->dev,
2041 qp->tx_mw_dma_addr)) {
2042 qp->tx_mw_dma_addr = 0;
2043 goto err1;
2044 }
2045 }
2046
Dave Jiang569410ca2015-07-13 08:07:22 -04002047 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
2048 qp->tx_dma_chan ? "DMA" : "CPU");
2049
2050 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
2051 qp->rx_dma_chan ? "DMA" : "CPU");
Jon Mason282a2fe2013-02-12 09:52:50 -07002052
Jon Masonfce8a7b2012-11-16 19:27:12 -07002053 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Jia-Ju Baic9160b62018-04-10 21:17:54 +08002054 entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002055 if (!entry)
2056 goto err1;
2057
Jon Mason282a2fe2013-02-12 09:52:50 -07002058 entry->qp = qp;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002059 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07002060 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002061 }
Dave Jianga754a8f2016-04-08 10:49:06 -07002062 qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002063
Dave Jianga754a8f2016-04-08 10:49:06 -07002064 for (i = 0; i < qp->tx_max_entry; i++) {
Jia-Ju Baic9160b62018-04-10 21:17:54 +08002065 entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002066 if (!entry)
2067 goto err2;
2068
Jon Mason282a2fe2013-02-12 09:52:50 -07002069 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002070 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07002071 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002072 }
2073
Allen Hubbee26a5842015-04-09 10:33:20 -04002074 ntb_db_clear(qp->ndev, qp_bit);
2075 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002076
2077 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
2078
2079 return qp;
2080
Jon Masonfce8a7b2012-11-16 19:27:12 -07002081err2:
Jon Masonf7667552013-01-19 02:02:24 -07002082 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07002083 kfree(entry);
2084err1:
Dave Jianga754a8f2016-04-08 10:49:06 -07002085 qp->rx_alloc_entry = 0;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002086 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07002087 kfree(entry);
Logan Gunthorpec59666b2019-01-18 17:10:01 -07002088 if (qp->tx_mw_dma_addr)
2089 dma_unmap_resource(qp->tx_dma_chan->device->dev,
2090 qp->tx_mw_dma_addr, qp->tx_mw_size,
2091 DMA_FROM_DEVICE, 0);
Dave Jiang569410ca2015-07-13 08:07:22 -04002092 if (qp->tx_dma_chan)
2093 dma_release_channel(qp->tx_dma_chan);
2094 if (qp->rx_dma_chan)
2095 dma_release_channel(qp->rx_dma_chan);
Allen Hubbee26a5842015-04-09 10:33:20 -04002096 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002097err:
2098 return NULL;
2099}
2100EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
2101
2102/**
2103 * ntb_transport_free_queue - Frees NTB transport queue
2104 * @qp: NTB queue to be freed
2105 *
2106 * Frees NTB transport queue
2107 */
2108void ntb_transport_free_queue(struct ntb_transport_qp *qp)
2109{
Jon Mason186f27f2013-01-22 11:35:40 -07002110 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002111 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04002112 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002113
2114 if (!qp)
2115 return;
2116
Allen Hubbee26a5842015-04-09 10:33:20 -04002117 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07002118
Dave Jiange9021332016-02-23 09:11:36 -07002119 qp->active = false;
2120
Dave Jiang569410ca2015-07-13 08:07:22 -04002121 if (qp->tx_dma_chan) {
2122 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002123 /* Putting the dma_chan to NULL will force any new traffic to be
2124 * processed by the CPU instead of the DAM engine
2125 */
Dave Jiang569410ca2015-07-13 08:07:22 -04002126 qp->tx_dma_chan = NULL;
2127
2128 /* Try to be nice and wait for any queued DMA engine
2129 * transactions to process before smashing it with a rock
2130 */
2131 dma_sync_wait(chan, qp->last_cookie);
2132 dmaengine_terminate_all(chan);
Logan Gunthorpec59666b2019-01-18 17:10:01 -07002133
2134 dma_unmap_resource(chan->device->dev,
2135 qp->tx_mw_dma_addr, qp->tx_mw_size,
2136 DMA_FROM_DEVICE, 0);
2137
Dave Jiang569410ca2015-07-13 08:07:22 -04002138 dma_release_channel(chan);
2139 }
2140
2141 if (qp->rx_dma_chan) {
2142 struct dma_chan *chan = qp->rx_dma_chan;
2143 /* Putting the dma_chan to NULL will force any new traffic to be
2144 * processed by the CPU instead of the DAM engine
2145 */
2146 qp->rx_dma_chan = NULL;
Jon Mason282a2fe2013-02-12 09:52:50 -07002147
2148 /* Try to be nice and wait for any queued DMA engine
2149 * transactions to process before smashing it with a rock
2150 */
2151 dma_sync_wait(chan, qp->last_cookie);
2152 dmaengine_terminate_all(chan);
Allen Hubbe1199aa62015-05-18 06:20:47 -04002153 dma_release_channel(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07002154 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07002155
Allen Hubbee26a5842015-04-09 10:33:20 -04002156 qp_bit = BIT_ULL(qp->qp_num);
2157
2158 ntb_db_set_mask(qp->ndev, qp_bit);
Dave Jiange9021332016-02-23 09:11:36 -07002159 tasklet_kill(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002160
Jon Mason282a2fe2013-02-12 09:52:50 -07002161 cancel_delayed_work_sync(&qp->link_work);
2162
Allen Hubbee26a5842015-04-09 10:33:20 -04002163 qp->cb_data = NULL;
2164 qp->rx_handler = NULL;
2165 qp->tx_handler = NULL;
2166 qp->event_handler = NULL;
2167
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002168 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07002169 kfree(entry);
2170
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002171 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
2172 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
2173 kfree(entry);
2174 }
2175
2176 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
2177 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07002178 kfree(entry);
2179 }
2180
Jon Masonf7667552013-01-19 02:02:24 -07002181 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07002182 kfree(entry);
2183
Allen Hubbe30a4bb12015-07-13 08:07:14 -04002184 qp->transport->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002185
2186 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
2187}
2188EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
2189
2190/**
2191 * ntb_transport_rx_remove - Dequeues enqueued rx packet
2192 * @qp: NTB queue to be freed
2193 * @len: pointer to variable to write enqueued buffers length
2194 *
2195 * Dequeues unused buffers from receive queue. Should only be used during
2196 * shutdown of qp.
2197 *
2198 * RETURNS: NULL error value on error, or void* for success.
2199 */
2200void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
2201{
2202 struct ntb_queue_entry *entry;
2203 void *buf;
2204
Allen Hubbee26a5842015-04-09 10:33:20 -04002205 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002206 return NULL;
2207
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002208 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002209 if (!entry)
2210 return NULL;
2211
2212 buf = entry->cb_data;
2213 *len = entry->len;
2214
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002215 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002216
2217 return buf;
2218}
2219EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
2220
2221/**
2222 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
2223 * @qp: NTB transport layer queue the entry is to be enqueued on
2224 * @cb: per buffer pointer for callback function to use
2225 * @data: pointer to data buffer that incoming packets will be copied into
2226 * @len: length of the data buffer
2227 *
2228 * Enqueue a new receive buffer onto the transport queue into which a NTB
2229 * payload can be received into.
2230 *
2231 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2232 */
2233int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2234 unsigned int len)
2235{
2236 struct ntb_queue_entry *entry;
2237
2238 if (!qp)
2239 return -EINVAL;
2240
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002241 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002242 if (!entry)
2243 return -ENOMEM;
2244
2245 entry->cb_data = cb;
2246 entry->buf = data;
2247 entry->len = len;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002248 entry->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07002249 entry->retries = 0;
2250 entry->errors = 0;
2251 entry->rx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002252
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002253 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
2254
Dave Jiange9021332016-02-23 09:11:36 -07002255 if (qp->active)
2256 tasklet_schedule(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002257
2258 return 0;
2259}
2260EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2261
2262/**
2263 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2264 * @qp: NTB transport layer queue the entry is to be enqueued on
2265 * @cb: per buffer pointer for callback function to use
2266 * @data: pointer to data buffer that will be sent
2267 * @len: length of the data buffer
2268 *
2269 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07002270 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07002271 * serialize access to the qp.
2272 *
2273 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2274 */
2275int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2276 unsigned int len)
2277{
2278 struct ntb_queue_entry *entry;
2279 int rc;
2280
Allen Hubbee26a5842015-04-09 10:33:20 -04002281 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002282 return -EINVAL;
2283
2284 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07002285 if (!entry) {
2286 qp->tx_err_no_buf++;
Dave Jiange74bfee2015-07-13 08:07:17 -04002287 return -EBUSY;
Jon Mason282a2fe2013-02-12 09:52:50 -07002288 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07002289
2290 entry->cb_data = cb;
2291 entry->buf = data;
2292 entry->len = len;
2293 entry->flags = 0;
Dave Jiang9cabc262016-07-20 13:14:07 -07002294 entry->errors = 0;
2295 entry->retries = 0;
2296 entry->tx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002297
2298 rc = ntb_process_tx(qp, entry);
2299 if (rc)
2300 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2301 &qp->tx_free_q);
2302
2303 return rc;
2304}
2305EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2306
2307/**
2308 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2309 * @qp: NTB transport layer queue to be enabled
2310 *
2311 * Notify NTB transport layer of client readiness to use queue
2312 */
2313void ntb_transport_link_up(struct ntb_transport_qp *qp)
2314{
2315 if (!qp)
2316 return;
2317
Allen Hubbee26a5842015-04-09 10:33:20 -04002318 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002319
Allen Hubbee26a5842015-04-09 10:33:20 -04002320 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002321 schedule_delayed_work(&qp->link_work, 0);
2322}
2323EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2324
2325/**
2326 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2327 * @qp: NTB transport layer queue to be disabled
2328 *
2329 * Notify NTB transport layer of client's desire to no longer receive data on
2330 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07002331 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07002332 */
2333void ntb_transport_link_down(struct ntb_transport_qp *qp)
2334{
Allen Hubbee26a5842015-04-09 10:33:20 -04002335 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002336
2337 if (!qp)
2338 return;
2339
Allen Hubbee26a5842015-04-09 10:33:20 -04002340 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002341
Allen Hubbee26a5842015-04-09 10:33:20 -04002342 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002343
Serge Semind67288a2017-01-11 03:13:20 +03002344 ntb_peer_spad_write(qp->ndev, PIDX, QP_LINKS, val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07002345
Allen Hubbee26a5842015-04-09 10:33:20 -04002346 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002347 ntb_send_link_down(qp);
2348 else
2349 cancel_delayed_work_sync(&qp->link_work);
2350}
2351EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2352
2353/**
2354 * ntb_transport_link_query - Query transport link state
2355 * @qp: NTB transport layer queue to be queried
2356 *
2357 * Query connectivity to the remote system of the NTB transport queue
2358 *
2359 * RETURNS: true for link up or false for link down
2360 */
2361bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2362{
Jon Mason186f27f2013-01-22 11:35:40 -07002363 if (!qp)
2364 return false;
2365
Allen Hubbee26a5842015-04-09 10:33:20 -04002366 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002367}
2368EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2369
2370/**
2371 * ntb_transport_qp_num - Query the qp number
2372 * @qp: NTB transport layer queue to be queried
2373 *
2374 * Query qp number of the NTB transport queue
2375 *
2376 * RETURNS: a zero based number specifying the qp number
2377 */
2378unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2379{
Jon Mason186f27f2013-01-22 11:35:40 -07002380 if (!qp)
2381 return 0;
2382
Jon Masonfce8a7b2012-11-16 19:27:12 -07002383 return qp->qp_num;
2384}
2385EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2386
2387/**
2388 * ntb_transport_max_size - Query the max payload size of a qp
2389 * @qp: NTB transport layer queue to be queried
2390 *
2391 * Query the maximum payload size permissible on the given qp
2392 *
2393 * RETURNS: the max payload size of a qp
2394 */
Jon Masonef114ed2013-01-19 02:02:18 -07002395unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002396{
Dave Jiang04afde42015-09-17 13:27:04 -07002397 unsigned int max_size;
Dave Jiang569410ca2015-07-13 08:07:22 -04002398 unsigned int copy_align;
Dave Jiang04afde42015-09-17 13:27:04 -07002399 struct dma_chan *rx_chan, *tx_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002400
Jon Mason186f27f2013-01-22 11:35:40 -07002401 if (!qp)
2402 return 0;
2403
Dave Jiang04afde42015-09-17 13:27:04 -07002404 rx_chan = qp->rx_dma_chan;
2405 tx_chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002406
Dave Jiang04afde42015-09-17 13:27:04 -07002407 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2408 tx_chan ? tx_chan->device->copy_align : 0);
Dave Jiang569410ca2015-07-13 08:07:22 -04002409
Jon Mason282a2fe2013-02-12 09:52:50 -07002410 /* If DMA engine usage is possible, try to find the max size for that */
Dave Jiang04afde42015-09-17 13:27:04 -07002411 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2412 max_size = round_down(max_size, 1 << copy_align);
Jon Mason282a2fe2013-02-12 09:52:50 -07002413
Dave Jiang04afde42015-09-17 13:27:04 -07002414 return max_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002415}
2416EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04002417
Dave Jiange74bfee2015-07-13 08:07:17 -04002418unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2419{
2420 unsigned int head = qp->tx_index;
2421 unsigned int tail = qp->remote_rx_info->entry;
2422
2423 return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2424}
2425EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2426
Allen Hubbee26a5842015-04-09 10:33:20 -04002427static void ntb_transport_doorbell_callback(void *data, int vector)
2428{
2429 struct ntb_transport_ctx *nt = data;
2430 struct ntb_transport_qp *qp;
2431 u64 db_bits;
2432 unsigned int qp_num;
2433
Logan Gunthorpe2b0569b2019-05-23 16:30:59 -06002434 if (ntb_db_read(nt->ndev) & nt->msi_db_mask) {
2435 ntb_transport_msi_peer_desc_changed(nt);
2436 ntb_db_clear(nt->ndev, nt->msi_db_mask);
2437 }
2438
Allen Hubbee26a5842015-04-09 10:33:20 -04002439 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2440 ntb_db_vector_mask(nt->ndev, vector));
2441
2442 while (db_bits) {
2443 qp_num = __ffs(db_bits);
2444 qp = &nt->qp_vec[qp_num];
2445
Dave Jiange9021332016-02-23 09:11:36 -07002446 if (qp->active)
2447 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04002448
2449 db_bits &= ~BIT_ULL(qp_num);
2450 }
2451}
2452
2453static const struct ntb_ctx_ops ntb_transport_ops = {
2454 .link_event = ntb_transport_event_callback,
2455 .db_event = ntb_transport_doorbell_callback,
2456};
2457
2458static struct ntb_client ntb_transport_client = {
2459 .ops = {
2460 .probe = ntb_transport_probe,
2461 .remove = ntb_transport_free,
2462 },
2463};
2464
2465static int __init ntb_transport_init(void)
2466{
2467 int rc;
2468
Dave Jiang7eb38782015-06-15 08:21:33 -04002469 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2470
Allen Hubbee26a5842015-04-09 10:33:20 -04002471 if (debugfs_initialized())
2472 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2473
2474 rc = bus_register(&ntb_transport_bus);
2475 if (rc)
2476 goto err_bus;
2477
2478 rc = ntb_register_client(&ntb_transport_client);
2479 if (rc)
2480 goto err_client;
2481
2482 return 0;
2483
2484err_client:
2485 bus_unregister(&ntb_transport_bus);
2486err_bus:
2487 debugfs_remove_recursive(nt_debugfs_dir);
2488 return rc;
2489}
2490module_init(ntb_transport_init);
2491
2492static void __exit ntb_transport_exit(void)
2493{
Allen Hubbee26a5842015-04-09 10:33:20 -04002494 ntb_unregister_client(&ntb_transport_client);
2495 bus_unregister(&ntb_transport_bus);
Allen Hubbedd622452016-12-27 17:57:04 -05002496 debugfs_remove_recursive(nt_debugfs_dir);
Allen Hubbee26a5842015-04-09 10:33:20 -04002497}
2498module_exit(ntb_transport_exit);