blob: 24222a5d8df26f0965b003ee4856a92123a5f0ca [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"
69
70MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
71MODULE_VERSION(NTB_TRANSPORT_VER);
72MODULE_LICENSE("Dual BSD/GPL");
73MODULE_AUTHOR("Intel Corporation");
74
75static unsigned long max_mw_size;
76module_param(max_mw_size, ulong, 0644);
77MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
Jon Masonfce8a7b2012-11-16 19:27:12 -070078
Dave Jiang98914172015-06-03 11:29:38 -040079static unsigned int transport_mtu = 0x10000;
Jon Masonfce8a7b2012-11-16 19:27:12 -070080module_param(transport_mtu, uint, 0644);
81MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
82
Jon Mason948d3a62013-04-18 17:07:36 -070083static unsigned char max_num_clients;
Jon Masonfce8a7b2012-11-16 19:27:12 -070084module_param(max_num_clients, byte, 0644);
85MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
86
Jon Mason282a2fe2013-02-12 09:52:50 -070087static unsigned int copy_bytes = 1024;
88module_param(copy_bytes, uint, 0644);
89MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
90
Dave Jianga41ef052015-05-19 16:52:04 -040091static bool use_dma;
92module_param(use_dma, bool, 0644);
93MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
94
Allen Hubbee26a5842015-04-09 10:33:20 -040095static struct dentry *nt_debugfs_dir;
96
Jon Masonfce8a7b2012-11-16 19:27:12 -070097struct ntb_queue_entry {
98 /* ntb_queue list reference */
99 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400100 /* pointers to data to be transferred */
Jon Masonfce8a7b2012-11-16 19:27:12 -0700101 void *cb_data;
102 void *buf;
103 unsigned int len;
104 unsigned int flags;
Dave Jiang9cabc262016-07-20 13:14:07 -0700105 int retries;
106 int errors;
107 unsigned int tx_index;
Dave Jiang72203572016-07-20 13:14:13 -0700108 unsigned int rx_index;
Jon Mason282a2fe2013-02-12 09:52:50 -0700109
110 struct ntb_transport_qp *qp;
111 union {
112 struct ntb_payload_header __iomem *tx_hdr;
113 struct ntb_payload_header *rx_hdr;
114 };
Jon Masonfce8a7b2012-11-16 19:27:12 -0700115};
116
Jon Mason793c20e2013-01-19 02:02:26 -0700117struct ntb_rx_info {
118 unsigned int entry;
119};
120
Jon Masonfce8a7b2012-11-16 19:27:12 -0700121struct ntb_transport_qp {
Allen Hubbee26a5842015-04-09 10:33:20 -0400122 struct ntb_transport_ctx *transport;
123 struct ntb_dev *ndev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700124 void *cb_data;
Dave Jiang569410ca2015-07-13 08:07:22 -0400125 struct dma_chan *tx_dma_chan;
126 struct dma_chan *rx_dma_chan;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700127
128 bool client_ready;
Allen Hubbee26a5842015-04-09 10:33:20 -0400129 bool link_is_up;
Dave Jiange9021332016-02-23 09:11:36 -0700130 bool active;
Allen Hubbee26a5842015-04-09 10:33:20 -0400131
Jon Masonfce8a7b2012-11-16 19:27:12 -0700132 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400133 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700134
Jon Mason74465642013-01-21 15:28:52 -0700135 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -0700136 struct ntb_rx_info *remote_rx_info;
137
Jon Mason53ca4fe2013-11-26 11:21:50 -0700138 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
139 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700140 struct list_head tx_free_q;
141 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700142 void __iomem *tx_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700143 dma_addr_t tx_mw_phys;
Jon Mason793c20e2013-01-19 02:02:26 -0700144 unsigned int tx_index;
145 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700146 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700147
Jon Mason53ca4fe2013-11-26 11:21:50 -0700148 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
149 void *data, int len);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400150 struct list_head rx_post_q;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700151 struct list_head rx_pend_q;
152 struct list_head rx_free_q;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400153 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
154 spinlock_t ntb_rx_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700155 void *rx_buff;
156 unsigned int rx_index;
157 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700158 unsigned int rx_max_frame;
Dave Jianga754a8f2016-04-08 10:49:06 -0700159 unsigned int rx_alloc_entry;
Jon Mason282a2fe2013-02-12 09:52:50 -0700160 dma_cookie_t last_cookie;
Allen Hubbee26a5842015-04-09 10:33:20 -0400161 struct tasklet_struct rxc_db_work;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700162
Jon Mason53ca4fe2013-11-26 11:21:50 -0700163 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700164 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700165 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700166
167 struct dentry *debugfs_dir;
168 struct dentry *debugfs_stats;
169
170 /* Stats */
171 u64 rx_bytes;
172 u64 rx_pkts;
173 u64 rx_ring_empty;
174 u64 rx_err_no_buf;
175 u64 rx_err_oflow;
176 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700177 u64 rx_memcpy;
178 u64 rx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700179 u64 tx_bytes;
180 u64 tx_pkts;
181 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700182 u64 tx_err_no_buf;
183 u64 tx_memcpy;
184 u64 tx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700185};
186
187struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400188 phys_addr_t phys_addr;
189 resource_size_t phys_size;
190 resource_size_t xlat_align;
191 resource_size_t xlat_align_size;
192 void __iomem *vbase;
193 size_t xlat_size;
194 size_t buff_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700195 void *virt_addr;
196 dma_addr_t dma_addr;
197};
198
199struct ntb_transport_client_dev {
200 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400201 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700202 struct device dev;
203};
204
Allen Hubbee26a5842015-04-09 10:33:20 -0400205struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700206 struct list_head entry;
207 struct list_head client_devs;
208
Allen Hubbee26a5842015-04-09 10:33:20 -0400209 struct ntb_dev *ndev;
210
211 struct ntb_transport_mw *mw_vec;
212 struct ntb_transport_qp *qp_vec;
213 unsigned int mw_count;
214 unsigned int qp_count;
215 u64 qp_bitmap;
216 u64 qp_bitmap_free;
217
218 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700219 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700220 struct work_struct link_cleanup;
Dave Jiangc8650fd2015-07-13 08:07:09 -0400221
222 struct dentry *debugfs_node_dir;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700223};
224
225enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400226 DESC_DONE_FLAG = BIT(0),
227 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700228};
229
230struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700231 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700232 unsigned int len;
233 unsigned int flags;
234};
235
236enum {
237 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700238 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700239 NUM_QPS,
240 NUM_MWS,
241 MW0_SZ_HIGH,
242 MW0_SZ_LOW,
243 MW1_SZ_HIGH,
244 MW1_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700245 MAX_SPAD,
246};
247
Allen Hubbee26a5842015-04-09 10:33:20 -0400248#define dev_client_dev(__dev) \
249 container_of((__dev), struct ntb_transport_client_dev, dev)
250
251#define drv_client(__drv) \
252 container_of((__drv), struct ntb_transport_client, driver)
253
254#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700255#define NTB_QP_DEF_NUM_ENTRIES 100
256#define NTB_LINK_DOWN_TIMEOUT 10
257
Allen Hubbee26a5842015-04-09 10:33:20 -0400258static void ntb_transport_rxc_db(unsigned long data);
259static const struct ntb_ctx_ops ntb_transport_ops;
260static struct ntb_client ntb_transport_client;
Dave Jiang9cabc262016-07-20 13:14:07 -0700261static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
262 struct ntb_queue_entry *entry);
263static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
Dave Jiang72203572016-07-20 13:14:13 -0700264static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
265static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
266
Allen Hubbee26a5842015-04-09 10:33:20 -0400267
268static int ntb_transport_bus_match(struct device *dev,
269 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700270{
271 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
272}
273
Allen Hubbee26a5842015-04-09 10:33:20 -0400274static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700275{
Allen Hubbee26a5842015-04-09 10:33:20 -0400276 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700277 int rc = -EINVAL;
278
279 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400280
281 client = drv_client(dev->driver);
282 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700283 if (rc)
284 put_device(dev);
285
286 return rc;
287}
288
Allen Hubbee26a5842015-04-09 10:33:20 -0400289static int ntb_transport_bus_remove(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700290{
Allen Hubbee26a5842015-04-09 10:33:20 -0400291 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700292
Allen Hubbee26a5842015-04-09 10:33:20 -0400293 client = drv_client(dev->driver);
294 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700295
296 put_device(dev);
297
298 return 0;
299}
300
Allen Hubbee26a5842015-04-09 10:33:20 -0400301static struct bus_type ntb_transport_bus = {
302 .name = "ntb_transport",
303 .match = ntb_transport_bus_match,
304 .probe = ntb_transport_bus_probe,
305 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700306};
307
308static LIST_HEAD(ntb_transport_list);
309
Allen Hubbee26a5842015-04-09 10:33:20 -0400310static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700311{
Dave Jiang31510002015-07-13 08:07:19 -0400312 list_add_tail(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700313 return 0;
314}
315
Allen Hubbee26a5842015-04-09 10:33:20 -0400316static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700317{
318 struct ntb_transport_client_dev *client_dev, *cd;
319
320 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
321 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
322 dev_name(&client_dev->dev));
323 list_del(&client_dev->entry);
324 device_unregister(&client_dev->dev);
325 }
326
327 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700328}
329
Allen Hubbee26a5842015-04-09 10:33:20 -0400330static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700331{
332 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700333
Allen Hubbee26a5842015-04-09 10:33:20 -0400334 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700335 kfree(client_dev);
336}
337
338/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400339 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700340 * @device_name: Name of NTB client device
341 *
342 * Unregister an NTB client device with the NTB transport layer
343 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400344void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700345{
346 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400347 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700348
349 list_for_each_entry(nt, &ntb_transport_list, entry)
350 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
351 if (!strncmp(dev_name(&client->dev), device_name,
352 strlen(device_name))) {
353 list_del(&client->entry);
354 device_unregister(&client->dev);
355 }
356}
Allen Hubbee26a5842015-04-09 10:33:20 -0400357EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700358
359/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400360 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700361 * @device_name: Name of NTB client device
362 *
363 * Register an NTB client device with the NTB transport layer
364 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400365int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700366{
367 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400368 struct ntb_transport_ctx *nt;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400369 int node;
Jon Mason8b19d452013-04-26 14:51:57 -0700370 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700371
Jon Mason8222b402013-01-19 02:02:16 -0700372 if (list_empty(&ntb_transport_list))
373 return -ENODEV;
374
Jon Masonfce8a7b2012-11-16 19:27:12 -0700375 list_for_each_entry(nt, &ntb_transport_list, entry) {
376 struct device *dev;
377
Allen Hubbe1199aa62015-05-18 06:20:47 -0400378 node = dev_to_node(&nt->ndev->dev);
379
380 client_dev = kzalloc_node(sizeof(*client_dev),
381 GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700382 if (!client_dev) {
383 rc = -ENOMEM;
384 goto err;
385 }
386
387 dev = &client_dev->dev;
388
389 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700390 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400391 dev->bus = &ntb_transport_bus;
392 dev->release = ntb_transport_client_release;
393 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700394
395 rc = device_register(dev);
396 if (rc) {
397 kfree(client_dev);
398 goto err;
399 }
400
401 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700402 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700403 }
404
405 return 0;
406
407err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400408 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700409
410 return rc;
411}
Allen Hubbee26a5842015-04-09 10:33:20 -0400412EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700413
414/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400415 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700416 * @drv: NTB client driver to be registered
417 *
418 * Register an NTB client driver with the NTB transport layer
419 *
420 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
421 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400422int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700423{
Allen Hubbee26a5842015-04-09 10:33:20 -0400424 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700425
Jon Mason8222b402013-01-19 02:02:16 -0700426 if (list_empty(&ntb_transport_list))
427 return -ENODEV;
428
Jon Masonfce8a7b2012-11-16 19:27:12 -0700429 return driver_register(&drv->driver);
430}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400431EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700432
433/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400434 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700435 * @drv: NTB client driver to be unregistered
436 *
437 * Unregister an NTB client driver with the NTB transport layer
438 *
439 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
440 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400441void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700442{
443 driver_unregister(&drv->driver);
444}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400445EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700446
Jon Masonfce8a7b2012-11-16 19:27:12 -0700447static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
448 loff_t *offp)
449{
450 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700451 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700452 ssize_t ret, out_offset, out_count;
453
Dave Jiang260bee92015-07-13 08:07:11 -0400454 qp = filp->private_data;
455
456 if (!qp || !qp->link_is_up)
457 return 0;
458
Jon Mason282a2fe2013-02-12 09:52:50 -0700459 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700460
461 buf = kmalloc(out_count, GFP_KERNEL);
462 if (!buf)
463 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700464
Jon Masonfce8a7b2012-11-16 19:27:12 -0700465 out_offset = 0;
466 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400467 "\nNTB QP stats:\n\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700468 out_offset += snprintf(buf + out_offset, out_count - out_offset,
469 "rx_bytes - \t%llu\n", qp->rx_bytes);
470 out_offset += snprintf(buf + out_offset, out_count - out_offset,
471 "rx_pkts - \t%llu\n", qp->rx_pkts);
472 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700473 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
474 out_offset += snprintf(buf + out_offset, out_count - out_offset,
475 "rx_async - \t%llu\n", qp->rx_async);
476 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700477 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
478 out_offset += snprintf(buf + out_offset, out_count - out_offset,
479 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
480 out_offset += snprintf(buf + out_offset, out_count - out_offset,
481 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
482 out_offset += snprintf(buf + out_offset, out_count - out_offset,
483 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
484 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400485 "rx_buff - \t0x%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700486 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700487 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700488 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jianga754a8f2016-04-08 10:49:06 -0700489 "rx_max_entry - \t%u\n", qp->rx_max_entry);
490 out_offset += snprintf(buf + out_offset, out_count - out_offset,
491 "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700492
493 out_offset += snprintf(buf + out_offset, out_count - out_offset,
494 "tx_bytes - \t%llu\n", qp->tx_bytes);
495 out_offset += snprintf(buf + out_offset, out_count - out_offset,
496 "tx_pkts - \t%llu\n", qp->tx_pkts);
497 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700498 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
499 out_offset += snprintf(buf + out_offset, out_count - out_offset,
500 "tx_async - \t%llu\n", qp->tx_async);
501 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700502 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
503 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700504 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
505 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400506 "tx_mw - \t0x%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700507 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400508 "tx_index (H) - \t%u\n", qp->tx_index);
509 out_offset += snprintf(buf + out_offset, out_count - out_offset,
510 "RRI (T) - \t%u\n",
511 qp->remote_rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700512 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700513 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400514 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiange74bfee2015-07-13 08:07:17 -0400515 "free tx - \t%u\n",
516 ntb_transport_tx_free_entry(qp));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700517
518 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400519 "\n");
520 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiang569410ca2015-07-13 08:07:22 -0400521 "Using TX DMA - \t%s\n",
522 qp->tx_dma_chan ? "Yes" : "No");
523 out_offset += snprintf(buf + out_offset, out_count - out_offset,
524 "Using RX DMA - \t%s\n",
525 qp->rx_dma_chan ? "Yes" : "No");
Dave Jiangd98ef992015-07-13 08:07:20 -0400526 out_offset += snprintf(buf + out_offset, out_count - out_offset,
527 "QP Link - \t%s\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400528 qp->link_is_up ? "Up" : "Down");
Dave Jiangd98ef992015-07-13 08:07:20 -0400529 out_offset += snprintf(buf + out_offset, out_count - out_offset,
530 "\n");
531
Jon Masond7237e22013-01-19 02:02:25 -0700532 if (out_offset > out_count)
533 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700534
535 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700536 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700537 return ret;
538}
539
540static const struct file_operations ntb_qp_debugfs_stats = {
541 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700542 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700543 .read = debugfs_read,
544};
545
546static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
547 struct list_head *list)
548{
549 unsigned long flags;
550
551 spin_lock_irqsave(lock, flags);
552 list_add_tail(entry, list);
553 spin_unlock_irqrestore(lock, flags);
554}
555
556static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700557 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700558{
559 struct ntb_queue_entry *entry;
560 unsigned long flags;
561
562 spin_lock_irqsave(lock, flags);
563 if (list_empty(list)) {
564 entry = NULL;
565 goto out;
566 }
567 entry = list_first_entry(list, struct ntb_queue_entry, entry);
568 list_del(&entry->entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400569
Jon Masonfce8a7b2012-11-16 19:27:12 -0700570out:
571 spin_unlock_irqrestore(lock, flags);
572
573 return entry;
574}
575
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400576static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
577 struct list_head *list,
578 struct list_head *to_list)
579{
580 struct ntb_queue_entry *entry;
581 unsigned long flags;
582
583 spin_lock_irqsave(lock, flags);
584
585 if (list_empty(list)) {
586 entry = NULL;
587 } else {
588 entry = list_first_entry(list, struct ntb_queue_entry, entry);
589 list_move_tail(&entry->entry, to_list);
590 }
591
592 spin_unlock_irqrestore(lock, flags);
593
594 return entry;
595}
596
Allen Hubbee26a5842015-04-09 10:33:20 -0400597static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
598 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700599{
Allen Hubbee26a5842015-04-09 10:33:20 -0400600 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
601 struct ntb_transport_mw *mw;
Dave Jianga754a8f2016-04-08 10:49:06 -0700602 struct ntb_dev *ndev = nt->ndev;
603 struct ntb_queue_entry *entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700604 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400605 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700606 unsigned int i;
Dave Jianga754a8f2016-04-08 10:49:06 -0700607 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700608
Allen Hubbee26a5842015-04-09 10:33:20 -0400609 mw_count = nt->mw_count;
610 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700611
Allen Hubbee26a5842015-04-09 10:33:20 -0400612 mw_num = QP_TO_MW(nt, qp_num);
613 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700614
Allen Hubbee26a5842015-04-09 10:33:20 -0400615 if (!mw->virt_addr)
616 return -ENOMEM;
617
Logan Gunthorpe471954c2017-06-05 14:00:53 -0600618 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -0400619 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700620 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400621 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700622
Allen Hubbee26a5842015-04-09 10:33:20 -0400623 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -0400624 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
Jon Mason793c20e2013-01-19 02:02:26 -0700625 rx_size -= sizeof(struct ntb_rx_info);
626
Jon Mason282a2fe2013-02-12 09:52:50 -0700627 qp->remote_rx_info = qp->rx_buff + rx_size;
628
Jon Masonc9d534c2013-02-01 15:45:16 -0700629 /* Due to housekeeping, there must be atleast 2 buffs */
630 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700631 qp->rx_max_entry = rx_size / qp->rx_max_frame;
632 qp->rx_index = 0;
633
Dave Jianga754a8f2016-04-08 10:49:06 -0700634 /*
635 * Checking to see if we have more entries than the default.
636 * We should add additional entries if that is the case so we
637 * can be in sync with the transport frames.
638 */
639 node = dev_to_node(&ndev->dev);
640 for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
641 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
642 if (!entry)
643 return -ENOMEM;
644
645 entry->qp = qp;
646 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
647 &qp->rx_free_q);
648 qp->rx_alloc_entry++;
649 }
650
Jon Masonc9d534c2013-02-01 15:45:16 -0700651 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700652
Jon Masonef114ed2013-01-19 02:02:18 -0700653 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700654 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400655 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
656 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700657 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700658 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700659
660 qp->rx_pkts = 0;
661 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700662 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400663
664 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700665}
666
Allen Hubbee26a5842015-04-09 10:33:20 -0400667static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700668{
Allen Hubbee26a5842015-04-09 10:33:20 -0400669 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
670 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700671
672 if (!mw->virt_addr)
673 return;
674
Allen Hubbee26a5842015-04-09 10:33:20 -0400675 ntb_mw_clear_trans(nt->ndev, num_mw);
676 dma_free_coherent(&pdev->dev, mw->buff_size,
677 mw->virt_addr, mw->dma_addr);
678 mw->xlat_size = 0;
679 mw->buff_size = 0;
Jon Mason113fc502013-01-30 11:40:52 -0700680 mw->virt_addr = NULL;
681}
682
Allen Hubbee26a5842015-04-09 10:33:20 -0400683static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400684 resource_size_t size)
Jon Masonb77b2632013-02-01 15:25:37 -0700685{
Allen Hubbee26a5842015-04-09 10:33:20 -0400686 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
687 struct pci_dev *pdev = nt->ndev->pdev;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400688 size_t xlat_size, buff_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400689 int rc;
690
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400691 if (!size)
692 return -EINVAL;
693
Allen Hubbee26a5842015-04-09 10:33:20 -0400694 xlat_size = round_up(size, mw->xlat_align_size);
695 buff_size = round_up(size, mw->xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700696
697 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400698 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700699 return 0;
700
Allen Hubbee26a5842015-04-09 10:33:20 -0400701 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700702 ntb_free_mw(nt, num_mw);
703
Allen Hubbee26a5842015-04-09 10:33:20 -0400704 /* Alloc memory for receiving data. Must be aligned */
705 mw->xlat_size = xlat_size;
706 mw->buff_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700707
Allen Hubbee26a5842015-04-09 10:33:20 -0400708 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
709 &mw->dma_addr, GFP_KERNEL);
Jon Masonb77b2632013-02-01 15:25:37 -0700710 if (!mw->virt_addr) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400711 mw->xlat_size = 0;
712 mw->buff_size = 0;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400713 dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400714 buff_size);
Jon Masonb77b2632013-02-01 15:25:37 -0700715 return -ENOMEM;
716 }
717
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700718 /*
719 * we must ensure that the memory address allocated is BAR size
720 * aligned in order for the XLAT register to take the value. This
721 * is a requirement of the hardware. It is recommended to setup CMA
722 * for BAR sizes equal or greater than 4MB.
723 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400724 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
725 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700726 &mw->dma_addr);
727 ntb_free_mw(nt, num_mw);
728 return -ENOMEM;
729 }
730
Jon Masonb77b2632013-02-01 15:25:37 -0700731 /* Notify HW the memory location of the receive buffer */
Allen Hubbee26a5842015-04-09 10:33:20 -0400732 rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
733 if (rc) {
734 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
735 ntb_free_mw(nt, num_mw);
736 return -EIO;
737 }
Jon Masonb77b2632013-02-01 15:25:37 -0700738
739 return 0;
740}
741
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400742static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
743{
744 qp->link_is_up = false;
Dave Jiange9021332016-02-23 09:11:36 -0700745 qp->active = false;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400746
747 qp->tx_index = 0;
748 qp->rx_index = 0;
749 qp->rx_bytes = 0;
750 qp->rx_pkts = 0;
751 qp->rx_ring_empty = 0;
752 qp->rx_err_no_buf = 0;
753 qp->rx_err_oflow = 0;
754 qp->rx_err_ver = 0;
755 qp->rx_memcpy = 0;
756 qp->rx_async = 0;
757 qp->tx_bytes = 0;
758 qp->tx_pkts = 0;
759 qp->tx_ring_full = 0;
760 qp->tx_err_no_buf = 0;
761 qp->tx_memcpy = 0;
762 qp->tx_async = 0;
763}
764
Jon Masonfca4d512013-09-09 13:39:55 -0700765static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700766{
Allen Hubbee26a5842015-04-09 10:33:20 -0400767 struct ntb_transport_ctx *nt = qp->transport;
768 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700769
Allen Hubbee22e0b92015-05-12 06:55:44 -0400770 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400771
772 cancel_delayed_work_sync(&qp->link_work);
773 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400774
775 if (qp->event_handler)
776 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700777}
778
779static void ntb_qp_link_cleanup_work(struct work_struct *work)
780{
781 struct ntb_transport_qp *qp = container_of(work,
782 struct ntb_transport_qp,
783 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400784 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700785
786 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700787
Allen Hubbee26a5842015-04-09 10:33:20 -0400788 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700789 schedule_delayed_work(&qp->link_work,
790 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
791}
792
Jon Mason7b4f2d32013-01-19 02:02:19 -0700793static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700794{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700795 schedule_work(&qp->link_cleanup);
796}
797
Allen Hubbee26a5842015-04-09 10:33:20 -0400798static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700799{
Allen Hubbee26a5842015-04-09 10:33:20 -0400800 struct ntb_transport_qp *qp;
801 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700802 int i;
803
Allen Hubbee26a5842015-04-09 10:33:20 -0400804 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700805
Allen Hubbee26a5842015-04-09 10:33:20 -0400806 /* Pass along the info to any clients */
807 for (i = 0; i < nt->qp_count; i++)
808 if (qp_bitmap_alloc & BIT_ULL(i)) {
809 qp = &nt->qp_vec[i];
810 ntb_qp_link_cleanup(qp);
811 cancel_work_sync(&qp->link_cleanup);
812 cancel_delayed_work_sync(&qp->link_work);
813 }
814
815 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700816 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700817
Jon Masonfce8a7b2012-11-16 19:27:12 -0700818 /* The scratchpad registers keep the values if the remote side
819 * goes down, blast them now to give them a sane value the next
820 * time they are accessed
821 */
822 for (i = 0; i < MAX_SPAD; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400823 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700824}
825
Jon Masonfca4d512013-09-09 13:39:55 -0700826static void ntb_transport_link_cleanup_work(struct work_struct *work)
827{
Allen Hubbee26a5842015-04-09 10:33:20 -0400828 struct ntb_transport_ctx *nt =
829 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700830
831 ntb_transport_link_cleanup(nt);
832}
833
Allen Hubbee26a5842015-04-09 10:33:20 -0400834static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700835{
Allen Hubbee26a5842015-04-09 10:33:20 -0400836 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700837
Allen Hubbee26a5842015-04-09 10:33:20 -0400838 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700839 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400840 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700841 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700842}
843
844static void ntb_transport_link_work(struct work_struct *work)
845{
Allen Hubbee26a5842015-04-09 10:33:20 -0400846 struct ntb_transport_ctx *nt =
847 container_of(work, struct ntb_transport_ctx, link_work.work);
848 struct ntb_dev *ndev = nt->ndev;
849 struct pci_dev *pdev = ndev->pdev;
850 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700851 u32 val;
Dave Jiang84f76682016-02-29 09:35:26 -0700852 int rc = 0, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700853
Jon Mason113fc502013-01-30 11:40:52 -0700854 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400855 for (i = 0; i < nt->mw_count; i++) {
856 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700857
Allen Hubbee26a5842015-04-09 10:33:20 -0400858 if (max_mw_size && size > max_mw_size)
859 size = max_mw_size;
860
861 spad = MW0_SZ_HIGH + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200862 ntb_peer_spad_write(ndev, spad, upper_32_bits(size));
Allen Hubbee26a5842015-04-09 10:33:20 -0400863
864 spad = MW0_SZ_LOW + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200865 ntb_peer_spad_write(ndev, spad, lower_32_bits(size));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700866 }
867
Allen Hubbee26a5842015-04-09 10:33:20 -0400868 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700869
Allen Hubbee26a5842015-04-09 10:33:20 -0400870 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700871
Allen Hubbee26a5842015-04-09 10:33:20 -0400872 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700873
874 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400875 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400876 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700877 if (val != NTB_TRANSPORT_VERSION)
878 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700879
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400880 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700881 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400882 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700883 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700884
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400885 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700886 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400887 if (val != nt->mw_count)
888 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700889
Allen Hubbee26a5842015-04-09 10:33:20 -0400890 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700891 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700892
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400893 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400894 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700895
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400896 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700897 val64 |= val;
898
Allen Hubbee26a5842015-04-09 10:33:20 -0400899 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700900
901 rc = ntb_set_mw(nt, i, val64);
902 if (rc)
903 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700904 }
905
Allen Hubbee26a5842015-04-09 10:33:20 -0400906 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700907
Allen Hubbee26a5842015-04-09 10:33:20 -0400908 for (i = 0; i < nt->qp_count; i++) {
909 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700910
911 ntb_transport_setup_qp_mw(nt, i);
912
Allen Hubbee26a5842015-04-09 10:33:20 -0400913 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700914 schedule_delayed_work(&qp->link_work, 0);
915 }
916
917 return;
918
Jon Mason113fc502013-01-30 11:40:52 -0700919out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400920 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700921 ntb_free_mw(nt, i);
Dave Jiang84f76682016-02-29 09:35:26 -0700922
923 /* if there's an actual failure, we should just bail */
Dave Jiangc1628772017-07-28 15:10:48 -0700924 if (rc < 0)
Dave Jiang84f76682016-02-29 09:35:26 -0700925 return;
Dave Jiang84f76682016-02-29 09:35:26 -0700926
Jon Masonfce8a7b2012-11-16 19:27:12 -0700927out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400928 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700929 schedule_delayed_work(&nt->link_work,
930 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
931}
932
933static void ntb_qp_link_work(struct work_struct *work)
934{
935 struct ntb_transport_qp *qp = container_of(work,
936 struct ntb_transport_qp,
937 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400938 struct pci_dev *pdev = qp->ndev->pdev;
939 struct ntb_transport_ctx *nt = qp->transport;
940 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700941
Allen Hubbee26a5842015-04-09 10:33:20 -0400942 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700943
Allen Hubbee26a5842015-04-09 10:33:20 -0400944 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700945
Allen Hubbee26a5842015-04-09 10:33:20 -0400946 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700947
948 /* query remote spad for qp ready bits */
Allen Hubbee26a5842015-04-09 10:33:20 -0400949 ntb_peer_spad_read(nt->ndev, QP_LINKS);
Allen Hubbe28762282015-05-11 10:08:26 -0400950 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700951
952 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400953 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700954 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400955 qp->link_is_up = true;
Dave Jiange9021332016-02-23 09:11:36 -0700956 qp->active = true;
Allen Hubbee26a5842015-04-09 10:33:20 -0400957
Jon Masonfce8a7b2012-11-16 19:27:12 -0700958 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400959 qp->event_handler(qp->cb_data, qp->link_is_up);
Allen Hubbe8b5a22d2015-07-13 08:07:12 -0400960
Dave Jiange9021332016-02-23 09:11:36 -0700961 if (qp->active)
962 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400963 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700964 schedule_delayed_work(&qp->link_work,
965 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
966}
967
Allen Hubbee26a5842015-04-09 10:33:20 -0400968static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700969 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700970{
971 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400972 phys_addr_t mw_base;
973 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700974 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400975 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700976 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700977
Allen Hubbee26a5842015-04-09 10:33:20 -0400978 mw_count = nt->mw_count;
979 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700980
Allen Hubbee26a5842015-04-09 10:33:20 -0400981 mw_num = QP_TO_MW(nt, qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400982
983 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700984 qp->qp_num = qp_num;
985 qp->transport = nt;
986 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400987 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700988 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400989 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700990
Logan Gunthorpe471954c2017-06-05 14:00:53 -0600991 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -0400992 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -0700993 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400994 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -0700995
Allen Hubbee26a5842015-04-09 10:33:20 -0400996 mw_base = nt->mw_vec[mw_num].phys_addr;
997 mw_size = nt->mw_vec[mw_num].phys_size;
998
999 tx_size = (unsigned int)mw_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -04001000 qp_offset = tx_size * (qp_num / mw_count);
Allen Hubbee26a5842015-04-09 10:33:20 -04001001
1002 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001003 if (!qp->tx_mw)
1004 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -07001005
Allen Hubbee26a5842015-04-09 10:33:20 -04001006 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001007 if (!qp->tx_mw_phys)
1008 return -EINVAL;
1009
1010 tx_size -= sizeof(struct ntb_rx_info);
1011 qp->rx_info = qp->tx_mw + tx_size;
1012
Jon Masonc9d534c2013-02-01 15:45:16 -07001013 /* Due to housekeeping, there must be atleast 2 buffs */
1014 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -07001015 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -07001016
Dave Jiangc8650fd2015-07-13 08:07:09 -04001017 if (nt->debugfs_node_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001018 char debugfs_name[4];
1019
1020 snprintf(debugfs_name, 4, "qp%d", qp_num);
1021 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Dave Jiangc8650fd2015-07-13 08:07:09 -04001022 nt->debugfs_node_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001023
1024 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1025 qp->debugfs_dir, qp,
1026 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -04001027 } else {
1028 qp->debugfs_dir = NULL;
1029 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001030 }
1031
1032 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001033 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001034
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001035 spin_lock_init(&qp->ntb_rx_q_lock);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001036 spin_lock_init(&qp->ntb_tx_free_q_lock);
1037
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001038 INIT_LIST_HEAD(&qp->rx_post_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001039 INIT_LIST_HEAD(&qp->rx_pend_q);
1040 INIT_LIST_HEAD(&qp->rx_free_q);
1041 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001042
Allen Hubbee26a5842015-04-09 10:33:20 -04001043 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1044 (unsigned long)qp);
1045
Jon Mason282a2fe2013-02-12 09:52:50 -07001046 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001047}
1048
Allen Hubbee26a5842015-04-09 10:33:20 -04001049static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001050{
Allen Hubbee26a5842015-04-09 10:33:20 -04001051 struct ntb_transport_ctx *nt;
1052 struct ntb_transport_mw *mw;
1053 unsigned int mw_count, qp_count;
1054 u64 qp_bitmap;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001055 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001056 int rc, i;
1057
Logan Gunthorpe19645a02016-06-07 11:20:22 -06001058 mw_count = ntb_mw_count(ndev);
1059 if (ntb_spad_count(ndev) < (NUM_MWS + 1 + mw_count * 2)) {
1060 dev_err(&ndev->dev, "Not enough scratch pad registers for %s",
1061 NTB_TRANSPORT_NAME);
1062 return -EIO;
1063 }
1064
Allen Hubbee26a5842015-04-09 10:33:20 -04001065 if (ntb_db_is_unsafe(ndev))
1066 dev_dbg(&ndev->dev,
1067 "doorbell is unsafe, proceed anyway...\n");
1068 if (ntb_spad_is_unsafe(ndev))
1069 dev_dbg(&ndev->dev,
1070 "scratchpad is unsafe, proceed anyway...\n");
1071
Allen Hubbe1199aa62015-05-18 06:20:47 -04001072 node = dev_to_node(&ndev->dev);
1073
1074 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001075 if (!nt)
1076 return -ENOMEM;
1077
Allen Hubbee26a5842015-04-09 10:33:20 -04001078 nt->ndev = ndev;
1079
Allen Hubbee26a5842015-04-09 10:33:20 -04001080 nt->mw_count = mw_count;
1081
Allen Hubbe1199aa62015-05-18 06:20:47 -04001082 nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1083 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001084 if (!nt->mw_vec) {
1085 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001086 goto err;
1087 }
1088
Allen Hubbee26a5842015-04-09 10:33:20 -04001089 for (i = 0; i < mw_count; i++) {
1090 mw = &nt->mw_vec[i];
1091
1092 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
1093 &mw->xlat_align, &mw->xlat_align_size);
1094 if (rc)
1095 goto err1;
1096
Dave Jiang06917f72015-05-19 16:45:46 -04001097 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001098 if (!mw->vbase) {
1099 rc = -ENOMEM;
1100 goto err1;
1101 }
1102
1103 mw->buff_size = 0;
1104 mw->xlat_size = 0;
1105 mw->virt_addr = NULL;
1106 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -07001107 }
1108
Allen Hubbee26a5842015-04-09 10:33:20 -04001109 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001110
Allen Hubbee26a5842015-04-09 10:33:20 -04001111 qp_count = ilog2(qp_bitmap);
1112 if (max_num_clients && max_num_clients < qp_count)
1113 qp_count = max_num_clients;
Logan Gunthorpe4743d1b2017-06-05 14:00:52 -06001114 else if (nt->mw_count < qp_count)
1115 qp_count = nt->mw_count;
Allen Hubbee26a5842015-04-09 10:33:20 -04001116
1117 qp_bitmap &= BIT_ULL(qp_count) - 1;
1118
1119 nt->qp_count = qp_count;
1120 nt->qp_bitmap = qp_bitmap;
1121 nt->qp_bitmap_free = qp_bitmap;
1122
Allen Hubbe1199aa62015-05-18 06:20:47 -04001123 nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1124 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001125 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001126 rc = -ENOMEM;
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301127 goto err1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001128 }
1129
Dave Jiangc8650fd2015-07-13 08:07:09 -04001130 if (nt_debugfs_dir) {
1131 nt->debugfs_node_dir =
1132 debugfs_create_dir(pci_name(ndev->pdev),
1133 nt_debugfs_dir);
1134 }
1135
Allen Hubbee26a5842015-04-09 10:33:20 -04001136 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001137 rc = ntb_transport_init_queue(nt, i);
1138 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301139 goto err2;
Jon Mason282a2fe2013-02-12 09:52:50 -07001140 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001141
1142 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001143 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001144
Allen Hubbee26a5842015-04-09 10:33:20 -04001145 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001146 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301147 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001148
1149 INIT_LIST_HEAD(&nt->client_devs);
1150 rc = ntb_bus_init(nt);
1151 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301152 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001153
Allen Hubbee26a5842015-04-09 10:33:20 -04001154 nt->link_is_up = false;
1155 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1156 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001157
1158 return 0;
1159
Jon Mason948d3a62013-04-18 17:07:36 -07001160err3:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301161 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001162err2:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301163 kfree(nt->qp_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001164err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001165 while (i--) {
1166 mw = &nt->mw_vec[i];
1167 iounmap(mw->vbase);
1168 }
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301169 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001170err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001171 kfree(nt);
1172 return rc;
1173}
1174
Allen Hubbee26a5842015-04-09 10:33:20 -04001175static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001176{
Allen Hubbee26a5842015-04-09 10:33:20 -04001177 struct ntb_transport_ctx *nt = ndev->ctx;
1178 struct ntb_transport_qp *qp;
1179 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001180 int i;
1181
Jon Masonfca4d512013-09-09 13:39:55 -07001182 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001183 cancel_work_sync(&nt->link_cleanup);
1184 cancel_delayed_work_sync(&nt->link_work);
1185
1186 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001187
1188 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001189 for (i = 0; i < nt->qp_count; i++) {
1190 qp = &nt->qp_vec[i];
1191 if (qp_bitmap_alloc & BIT_ULL(i))
1192 ntb_transport_free_queue(qp);
1193 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001194 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001195
Allen Hubbee26a5842015-04-09 10:33:20 -04001196 ntb_link_disable(ndev);
1197 ntb_clear_ctx(ndev);
1198
Jon Masonfce8a7b2012-11-16 19:27:12 -07001199 ntb_bus_remove(nt);
1200
Allen Hubbee26a5842015-04-09 10:33:20 -04001201 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001202 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001203 iounmap(nt->mw_vec[i].vbase);
1204 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001205
Allen Hubbee26a5842015-04-09 10:33:20 -04001206 kfree(nt->qp_vec);
1207 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001208 kfree(nt);
1209}
1210
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001211static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1212{
1213 struct ntb_queue_entry *entry;
1214 void *cb_data;
1215 unsigned int len;
1216 unsigned long irqflags;
1217
1218 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1219
1220 while (!list_empty(&qp->rx_post_q)) {
1221 entry = list_first_entry(&qp->rx_post_q,
1222 struct ntb_queue_entry, entry);
1223 if (!(entry->flags & DESC_DONE_FLAG))
1224 break;
1225
1226 entry->rx_hdr->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07001227 iowrite32(entry->rx_index, &qp->rx_info->entry);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001228
1229 cb_data = entry->cb_data;
1230 len = entry->len;
1231
1232 list_move_tail(&entry->entry, &qp->rx_free_q);
1233
1234 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1235
1236 if (qp->rx_handler && qp->client_ready)
1237 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1238
1239 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1240 }
1241
1242 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1243}
1244
Dave Jiang72203572016-07-20 13:14:13 -07001245static void ntb_rx_copy_callback(void *data,
1246 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001247{
Jon Mason282a2fe2013-02-12 09:52:50 -07001248 struct ntb_queue_entry *entry = data;
Jon Mason448c6fb2013-01-19 02:02:27 -07001249
Dave Jiang72203572016-07-20 13:14:13 -07001250 /* we need to check DMA results if we are using DMA */
1251 if (res) {
1252 enum dmaengine_tx_result dma_err = res->result;
1253
1254 switch (dma_err) {
1255 case DMA_TRANS_READ_FAILED:
1256 case DMA_TRANS_WRITE_FAILED:
1257 entry->errors++;
1258 case DMA_TRANS_ABORTED:
1259 {
1260 struct ntb_transport_qp *qp = entry->qp;
1261 void *offset = qp->rx_buff + qp->rx_max_frame *
1262 qp->rx_index;
1263
1264 ntb_memcpy_rx(entry, offset);
1265 qp->rx_memcpy++;
1266 return;
1267 }
1268
1269 case DMA_TRANS_NOERROR:
1270 default:
1271 break;
1272 }
1273 }
1274
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001275 entry->flags |= DESC_DONE_FLAG;
Jon Mason282a2fe2013-02-12 09:52:50 -07001276
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001277 ntb_complete_rxc(entry->qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001278}
1279
Jon Mason282a2fe2013-02-12 09:52:50 -07001280static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1281{
1282 void *buf = entry->buf;
1283 size_t len = entry->len;
1284
1285 memcpy(buf, offset, len);
1286
Allen Hubbee26a5842015-04-09 10:33:20 -04001287 /* Ensure that the data is fully copied out before clearing the flag */
1288 wmb();
1289
Dave Jiang72203572016-07-20 13:14:13 -07001290 ntb_rx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001291}
1292
Dave Jiang72203572016-07-20 13:14:13 -07001293static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
Jon Mason282a2fe2013-02-12 09:52:50 -07001294{
1295 struct dma_async_tx_descriptor *txd;
1296 struct ntb_transport_qp *qp = entry->qp;
Dave Jiang569410ca2015-07-13 08:07:22 -04001297 struct dma_chan *chan = qp->rx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001298 struct dma_device *device;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001299 size_t pay_off, buff_off, len;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001300 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001301 dma_cookie_t cookie;
1302 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001303
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001304 len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001305 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001306 pay_off = (size_t)offset & ~PAGE_MASK;
1307 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001308
1309 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Allen Hubbe905921e2015-07-13 08:07:21 -04001310 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001311
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001312 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1313 if (!unmap)
Allen Hubbe905921e2015-07-13 08:07:21 -04001314 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001315
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001316 unmap->len = len;
1317 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1318 pay_off, len, DMA_TO_DEVICE);
1319 if (dma_mapping_error(device->dev, unmap->addr[0]))
1320 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001321
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001322 unmap->to_cnt = 1;
1323
1324 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1325 buff_off, len, DMA_FROM_DEVICE);
1326 if (dma_mapping_error(device->dev, unmap->addr[1]))
1327 goto err_get_unmap;
1328
1329 unmap->from_cnt = 1;
1330
Allen Hubbe7592db52017-06-09 18:06:36 -04001331 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
1332 unmap->addr[0], len,
1333 DMA_PREP_INTERRUPT);
1334 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001335 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001336
Dave Jiang72203572016-07-20 13:14:13 -07001337 txd->callback_result = ntb_rx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001338 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001339 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001340
1341 cookie = dmaengine_submit(txd);
1342 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001343 goto err_set_unmap;
1344
1345 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001346
1347 qp->last_cookie = cookie;
1348
1349 qp->rx_async++;
1350
Dave Jiang72203572016-07-20 13:14:13 -07001351 return 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001352
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001353err_set_unmap:
1354 dmaengine_unmap_put(unmap);
1355err_get_unmap:
1356 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001357err:
Dave Jiang72203572016-07-20 13:14:13 -07001358 return -ENXIO;
1359}
1360
1361static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
1362{
1363 struct ntb_transport_qp *qp = entry->qp;
1364 struct dma_chan *chan = qp->rx_dma_chan;
1365 int res;
1366
1367 if (!chan)
1368 goto err;
1369
1370 if (entry->len < copy_bytes)
1371 goto err;
1372
1373 res = ntb_async_rx_submit(entry, offset);
1374 if (res < 0)
1375 goto err;
1376
1377 if (!entry->retries)
1378 qp->rx_async++;
1379
1380 return;
1381
1382err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001383 ntb_memcpy_rx(entry, offset);
1384 qp->rx_memcpy++;
1385}
1386
Jon Masonfce8a7b2012-11-16 19:27:12 -07001387static int ntb_process_rxc(struct ntb_transport_qp *qp)
1388{
1389 struct ntb_payload_header *hdr;
1390 struct ntb_queue_entry *entry;
1391 void *offset;
1392
Jon Mason793c20e2013-01-19 02:02:26 -07001393 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1394 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1395
Allen Hubbee26a5842015-04-09 10:33:20 -04001396 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1397 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001398
Jon Masonfce8a7b2012-11-16 19:27:12 -07001399 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001400 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001401 qp->rx_ring_empty++;
1402 return -EAGAIN;
1403 }
1404
Allen Hubbee26a5842015-04-09 10:33:20 -04001405 if (hdr->flags & LINK_DOWN_FLAG) {
1406 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1407 ntb_qp_link_down(qp);
1408 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001409 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001410 }
1411
1412 if (hdr->ver != (u32)qp->rx_pkts) {
1413 dev_dbg(&qp->ndev->pdev->dev,
1414 "version mismatch, expected %llu - got %u\n",
1415 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001416 qp->rx_err_ver++;
1417 return -EIO;
1418 }
1419
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001420 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
Allen Hubbee26a5842015-04-09 10:33:20 -04001421 if (!entry) {
1422 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1423 qp->rx_err_no_buf++;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001424 return -EAGAIN;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001425 }
1426
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001427 entry->rx_hdr = hdr;
Dave Jiang72203572016-07-20 13:14:13 -07001428 entry->rx_index = qp->rx_index;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001429
Allen Hubbee26a5842015-04-09 10:33:20 -04001430 if (hdr->len > entry->len) {
1431 dev_dbg(&qp->ndev->pdev->dev,
1432 "receive buffer overflow! Wanted %d got %d\n",
1433 hdr->len, entry->len);
1434 qp->rx_err_oflow++;
1435
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001436 entry->len = -EIO;
1437 entry->flags |= DESC_DONE_FLAG;
1438
1439 ntb_complete_rxc(qp);
1440 } else {
1441 dev_dbg(&qp->ndev->pdev->dev,
1442 "RX OK index %u ver %u size %d into buf size %d\n",
1443 qp->rx_index, hdr->ver, hdr->len, entry->len);
1444
1445 qp->rx_bytes += hdr->len;
1446 qp->rx_pkts++;
1447
1448 entry->len = hdr->len;
1449
1450 ntb_async_rx(entry, offset);
Allen Hubbee26a5842015-04-09 10:33:20 -04001451 }
1452
Jon Mason282a2fe2013-02-12 09:52:50 -07001453 qp->rx_index++;
1454 qp->rx_index %= qp->rx_max_entry;
1455
1456 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001457}
1458
Allen Hubbee26a5842015-04-09 10:33:20 -04001459static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001460{
Allen Hubbee26a5842015-04-09 10:33:20 -04001461 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001462 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001463
Allen Hubbee26a5842015-04-09 10:33:20 -04001464 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1465 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001466
Jon Masonc336acd2013-01-17 15:28:45 -07001467 /* Limit the number of packets processed in a single interrupt to
1468 * provide fairness to others
1469 */
1470 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001471 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001472 if (rc)
1473 break;
1474 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001475
Dave Jiang569410ca2015-07-13 08:07:22 -04001476 if (i && qp->rx_dma_chan)
1477 dma_async_issue_pending(qp->rx_dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001478
Allen Hubbee26a5842015-04-09 10:33:20 -04001479 if (i == qp->rx_max_entry) {
1480 /* there is more work to do */
Dave Jiange9021332016-02-23 09:11:36 -07001481 if (qp->active)
1482 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001483 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1484 /* the doorbell bit is set: clear it */
1485 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1486 /* ntb_db_read ensures ntb_db_clear write is committed */
1487 ntb_db_read(qp->ndev);
1488
1489 /* an interrupt may have arrived between finishing
1490 * ntb_process_rxc and clearing the doorbell bit:
1491 * there might be some more work to do.
1492 */
Dave Jiange9021332016-02-23 09:11:36 -07001493 if (qp->active)
1494 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001495 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001496}
1497
Dave Jiang9cabc262016-07-20 13:14:07 -07001498static void ntb_tx_copy_callback(void *data,
1499 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001500{
Jon Mason282a2fe2013-02-12 09:52:50 -07001501 struct ntb_queue_entry *entry = data;
1502 struct ntb_transport_qp *qp = entry->qp;
1503 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001504
Dave Jiang9cabc262016-07-20 13:14:07 -07001505 /* we need to check DMA results if we are using DMA */
1506 if (res) {
1507 enum dmaengine_tx_result dma_err = res->result;
1508
1509 switch (dma_err) {
1510 case DMA_TRANS_READ_FAILED:
1511 case DMA_TRANS_WRITE_FAILED:
1512 entry->errors++;
1513 case DMA_TRANS_ABORTED:
1514 {
1515 void __iomem *offset =
1516 qp->tx_mw + qp->tx_max_frame *
1517 entry->tx_index;
1518
1519 /* resubmit via CPU */
1520 ntb_memcpy_tx(entry, offset);
1521 qp->tx_memcpy++;
1522 return;
1523 }
1524
1525 case DMA_TRANS_NOERROR:
1526 default:
1527 break;
1528 }
1529 }
1530
Jon Mason74465642013-01-21 15:28:52 -07001531 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001532
Allen Hubbee26a5842015-04-09 10:33:20 -04001533 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001534
1535 /* The entry length can only be zero if the packet is intended to be a
1536 * "link down" or similar. Since no payload is being sent in these
1537 * cases, there is nothing to add to the completion queue.
1538 */
1539 if (entry->len > 0) {
1540 qp->tx_bytes += entry->len;
1541
1542 if (qp->tx_handler)
1543 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1544 entry->len);
1545 }
1546
1547 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1548}
1549
Jon Mason282a2fe2013-02-12 09:52:50 -07001550static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1551{
Dave Jiang06917f72015-05-19 16:45:46 -04001552#ifdef ARCH_HAS_NOCACHE_UACCESS
1553 /*
1554 * Using non-temporal mov to improve performance on non-cached
1555 * writes, even though we aren't actually copying from user space.
1556 */
1557 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1558#else
Jon Mason282a2fe2013-02-12 09:52:50 -07001559 memcpy_toio(offset, entry->buf, entry->len);
Dave Jiang06917f72015-05-19 16:45:46 -04001560#endif
Jon Mason282a2fe2013-02-12 09:52:50 -07001561
Allen Hubbee26a5842015-04-09 10:33:20 -04001562 /* Ensure that the data is fully copied out before setting the flags */
1563 wmb();
1564
Dave Jiang9cabc262016-07-20 13:14:07 -07001565 ntb_tx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001566}
1567
Dave Jiang9cabc262016-07-20 13:14:07 -07001568static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1569 struct ntb_queue_entry *entry)
Jon Mason282a2fe2013-02-12 09:52:50 -07001570{
Jon Mason282a2fe2013-02-12 09:52:50 -07001571 struct dma_async_tx_descriptor *txd;
Dave Jiang569410ca2015-07-13 08:07:22 -04001572 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001573 struct dma_device *device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001574 size_t len = entry->len;
1575 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001576 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001577 struct dmaengine_unmap_data *unmap;
1578 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001579 dma_cookie_t cookie;
Jon Mason282a2fe2013-02-12 09:52:50 -07001580
Jon Mason282a2fe2013-02-12 09:52:50 -07001581 device = chan->device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001582 dest = qp->tx_mw_phys + qp->tx_max_frame * entry->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001583 buff_off = (size_t)buf & ~PAGE_MASK;
1584 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001585
1586 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1587 goto err;
1588
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001589 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1590 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001591 goto err;
1592
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001593 unmap->len = len;
1594 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1595 buff_off, len, DMA_TO_DEVICE);
1596 if (dma_mapping_error(device->dev, unmap->addr[0]))
1597 goto err_get_unmap;
1598
1599 unmap->to_cnt = 1;
1600
Allen Hubbe7592db52017-06-09 18:06:36 -04001601 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
1602 DMA_PREP_INTERRUPT);
1603 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001604 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001605
Dave Jiang9cabc262016-07-20 13:14:07 -07001606 txd->callback_result = ntb_tx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001607 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001608 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001609
1610 cookie = dmaengine_submit(txd);
1611 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001612 goto err_set_unmap;
1613
1614 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001615
1616 dma_async_issue_pending(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001617
Dave Jiang9cabc262016-07-20 13:14:07 -07001618 return 0;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001619err_set_unmap:
1620 dmaengine_unmap_put(unmap);
1621err_get_unmap:
1622 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001623err:
Dave Jiang9cabc262016-07-20 13:14:07 -07001624 return -ENXIO;
1625}
1626
1627static void ntb_async_tx(struct ntb_transport_qp *qp,
1628 struct ntb_queue_entry *entry)
1629{
1630 struct ntb_payload_header __iomem *hdr;
1631 struct dma_chan *chan = qp->tx_dma_chan;
1632 void __iomem *offset;
1633 int res;
1634
1635 entry->tx_index = qp->tx_index;
1636 offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
1637 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1638 entry->tx_hdr = hdr;
1639
1640 iowrite32(entry->len, &hdr->len);
1641 iowrite32((u32)qp->tx_pkts, &hdr->ver);
1642
1643 if (!chan)
1644 goto err;
1645
1646 if (entry->len < copy_bytes)
1647 goto err;
1648
1649 res = ntb_async_tx_submit(qp, entry);
1650 if (res < 0)
1651 goto err;
1652
1653 if (!entry->retries)
1654 qp->tx_async++;
1655
1656 return;
1657
1658err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001659 ntb_memcpy_tx(entry, offset);
1660 qp->tx_memcpy++;
1661}
1662
Jon Masonfce8a7b2012-11-16 19:27:12 -07001663static int ntb_process_tx(struct ntb_transport_qp *qp,
1664 struct ntb_queue_entry *entry)
1665{
Jon Mason793c20e2013-01-19 02:02:26 -07001666 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001667 qp->tx_ring_full++;
1668 return -EAGAIN;
1669 }
1670
Jon Masonef114ed2013-01-19 02:02:18 -07001671 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001672 if (qp->tx_handler)
Jon Mason179f9122015-12-18 13:22:37 -05001673 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001674
1675 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1676 &qp->tx_free_q);
1677 return 0;
1678 }
1679
Jon Mason282a2fe2013-02-12 09:52:50 -07001680 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001681
Jon Mason793c20e2013-01-19 02:02:26 -07001682 qp->tx_index++;
1683 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001684
1685 qp->tx_pkts++;
1686
1687 return 0;
1688}
1689
1690static void ntb_send_link_down(struct ntb_transport_qp *qp)
1691{
Allen Hubbee26a5842015-04-09 10:33:20 -04001692 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001693 struct ntb_queue_entry *entry;
1694 int i, rc;
1695
Allen Hubbee26a5842015-04-09 10:33:20 -04001696 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001697 return;
1698
Allen Hubbee22e0b92015-05-12 06:55:44 -04001699 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001700
1701 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001702 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001703 if (entry)
1704 break;
1705 msleep(100);
1706 }
1707
1708 if (!entry)
1709 return;
1710
1711 entry->cb_data = NULL;
1712 entry->buf = NULL;
1713 entry->len = 0;
1714 entry->flags = LINK_DOWN_FLAG;
1715
1716 rc = ntb_process_tx(qp, entry);
1717 if (rc)
1718 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1719 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001720
1721 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001722}
1723
Allen Hubbe1199aa62015-05-18 06:20:47 -04001724static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1725{
1726 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1727}
1728
Jon Masonfce8a7b2012-11-16 19:27:12 -07001729/**
1730 * ntb_transport_create_queue - Create a new NTB transport layer queue
1731 * @rx_handler: receive callback function
1732 * @tx_handler: transmit callback function
1733 * @event_handler: event callback function
1734 *
1735 * Create a new NTB transport layer queue and provide the queue with a callback
1736 * routine for both transmit and receive. The receive callback routine will be
1737 * used to pass up data when the transport has received it on the queue. The
1738 * transmit callback routine will be called when the transport has completed the
1739 * transmission of the data on the queue and the data is ready to be freed.
1740 *
1741 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1742 */
1743struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001744ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001745 const struct ntb_queue_handlers *handlers)
1746{
Allen Hubbee26a5842015-04-09 10:33:20 -04001747 struct ntb_dev *ndev;
1748 struct pci_dev *pdev;
1749 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001750 struct ntb_queue_entry *entry;
1751 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001752 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001753 unsigned int free_queue;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001754 dma_cap_mask_t dma_mask;
1755 int node;
Allen Hubbee26a5842015-04-09 10:33:20 -04001756 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001757
Allen Hubbee26a5842015-04-09 10:33:20 -04001758 ndev = dev_ntb(client_dev->parent);
1759 pdev = ndev->pdev;
1760 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001761
Allen Hubbe1199aa62015-05-18 06:20:47 -04001762 node = dev_to_node(&ndev->dev);
1763
Thomas VanSelus68214ad2017-02-13 16:46:26 -06001764 free_queue = ffs(nt->qp_bitmap_free);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001765 if (!free_queue)
1766 goto err;
1767
1768 /* decrement free_queue to make it zero based */
1769 free_queue--;
1770
Allen Hubbee26a5842015-04-09 10:33:20 -04001771 qp = &nt->qp_vec[free_queue];
1772 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001773
Allen Hubbee26a5842015-04-09 10:33:20 -04001774 nt->qp_bitmap_free &= ~qp_bit;
1775
Jon Masonfce8a7b2012-11-16 19:27:12 -07001776 qp->cb_data = data;
1777 qp->rx_handler = handlers->rx_handler;
1778 qp->tx_handler = handlers->tx_handler;
1779 qp->event_handler = handlers->event_handler;
1780
Allen Hubbe1199aa62015-05-18 06:20:47 -04001781 dma_cap_zero(dma_mask);
1782 dma_cap_set(DMA_MEMCPY, dma_mask);
1783
Dave Jianga41ef052015-05-19 16:52:04 -04001784 if (use_dma) {
Dave Jiang569410ca2015-07-13 08:07:22 -04001785 qp->tx_dma_chan =
1786 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1787 (void *)(unsigned long)node);
1788 if (!qp->tx_dma_chan)
1789 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
1790
1791 qp->rx_dma_chan =
1792 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1793 (void *)(unsigned long)node);
1794 if (!qp->rx_dma_chan)
1795 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
Dave Jianga41ef052015-05-19 16:52:04 -04001796 } else {
Dave Jiang569410ca2015-07-13 08:07:22 -04001797 qp->tx_dma_chan = NULL;
1798 qp->rx_dma_chan = NULL;
Dave Jianga41ef052015-05-19 16:52:04 -04001799 }
Dave Jiang569410ca2015-07-13 08:07:22 -04001800
1801 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
1802 qp->tx_dma_chan ? "DMA" : "CPU");
1803
1804 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
1805 qp->rx_dma_chan ? "DMA" : "CPU");
Jon Mason282a2fe2013-02-12 09:52:50 -07001806
Jon Masonfce8a7b2012-11-16 19:27:12 -07001807 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001808 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001809 if (!entry)
1810 goto err1;
1811
Jon Mason282a2fe2013-02-12 09:52:50 -07001812 entry->qp = qp;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001813 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001814 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001815 }
Dave Jianga754a8f2016-04-08 10:49:06 -07001816 qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001817
Dave Jianga754a8f2016-04-08 10:49:06 -07001818 for (i = 0; i < qp->tx_max_entry; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001819 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001820 if (!entry)
1821 goto err2;
1822
Jon Mason282a2fe2013-02-12 09:52:50 -07001823 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001824 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001825 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001826 }
1827
Allen Hubbee26a5842015-04-09 10:33:20 -04001828 ntb_db_clear(qp->ndev, qp_bit);
1829 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001830
1831 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1832
1833 return qp;
1834
Jon Masonfce8a7b2012-11-16 19:27:12 -07001835err2:
Jon Masonf7667552013-01-19 02:02:24 -07001836 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001837 kfree(entry);
1838err1:
Dave Jianga754a8f2016-04-08 10:49:06 -07001839 qp->rx_alloc_entry = 0;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001840 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001841 kfree(entry);
Dave Jiang569410ca2015-07-13 08:07:22 -04001842 if (qp->tx_dma_chan)
1843 dma_release_channel(qp->tx_dma_chan);
1844 if (qp->rx_dma_chan)
1845 dma_release_channel(qp->rx_dma_chan);
Allen Hubbee26a5842015-04-09 10:33:20 -04001846 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001847err:
1848 return NULL;
1849}
1850EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1851
1852/**
1853 * ntb_transport_free_queue - Frees NTB transport queue
1854 * @qp: NTB queue to be freed
1855 *
1856 * Frees NTB transport queue
1857 */
1858void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1859{
Jon Mason186f27f2013-01-22 11:35:40 -07001860 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001861 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001862 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001863
1864 if (!qp)
1865 return;
1866
Allen Hubbee26a5842015-04-09 10:33:20 -04001867 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001868
Dave Jiange9021332016-02-23 09:11:36 -07001869 qp->active = false;
1870
Dave Jiang569410ca2015-07-13 08:07:22 -04001871 if (qp->tx_dma_chan) {
1872 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001873 /* Putting the dma_chan to NULL will force any new traffic to be
1874 * processed by the CPU instead of the DAM engine
1875 */
Dave Jiang569410ca2015-07-13 08:07:22 -04001876 qp->tx_dma_chan = NULL;
1877
1878 /* Try to be nice and wait for any queued DMA engine
1879 * transactions to process before smashing it with a rock
1880 */
1881 dma_sync_wait(chan, qp->last_cookie);
1882 dmaengine_terminate_all(chan);
1883 dma_release_channel(chan);
1884 }
1885
1886 if (qp->rx_dma_chan) {
1887 struct dma_chan *chan = qp->rx_dma_chan;
1888 /* Putting the dma_chan to NULL will force any new traffic to be
1889 * processed by the CPU instead of the DAM engine
1890 */
1891 qp->rx_dma_chan = NULL;
Jon Mason282a2fe2013-02-12 09:52:50 -07001892
1893 /* Try to be nice and wait for any queued DMA engine
1894 * transactions to process before smashing it with a rock
1895 */
1896 dma_sync_wait(chan, qp->last_cookie);
1897 dmaengine_terminate_all(chan);
Allen Hubbe1199aa62015-05-18 06:20:47 -04001898 dma_release_channel(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001899 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001900
Allen Hubbee26a5842015-04-09 10:33:20 -04001901 qp_bit = BIT_ULL(qp->qp_num);
1902
1903 ntb_db_set_mask(qp->ndev, qp_bit);
Dave Jiange9021332016-02-23 09:11:36 -07001904 tasklet_kill(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001905
Jon Mason282a2fe2013-02-12 09:52:50 -07001906 cancel_delayed_work_sync(&qp->link_work);
1907
Allen Hubbee26a5842015-04-09 10:33:20 -04001908 qp->cb_data = NULL;
1909 qp->rx_handler = NULL;
1910 qp->tx_handler = NULL;
1911 qp->event_handler = NULL;
1912
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001913 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001914 kfree(entry);
1915
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001916 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1917 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1918 kfree(entry);
1919 }
1920
1921 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1922 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001923 kfree(entry);
1924 }
1925
Jon Masonf7667552013-01-19 02:02:24 -07001926 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001927 kfree(entry);
1928
Allen Hubbe30a4bb12015-07-13 08:07:14 -04001929 qp->transport->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001930
1931 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1932}
1933EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1934
1935/**
1936 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1937 * @qp: NTB queue to be freed
1938 * @len: pointer to variable to write enqueued buffers length
1939 *
1940 * Dequeues unused buffers from receive queue. Should only be used during
1941 * shutdown of qp.
1942 *
1943 * RETURNS: NULL error value on error, or void* for success.
1944 */
1945void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1946{
1947 struct ntb_queue_entry *entry;
1948 void *buf;
1949
Allen Hubbee26a5842015-04-09 10:33:20 -04001950 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001951 return NULL;
1952
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001953 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001954 if (!entry)
1955 return NULL;
1956
1957 buf = entry->cb_data;
1958 *len = entry->len;
1959
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001960 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001961
1962 return buf;
1963}
1964EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1965
1966/**
1967 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1968 * @qp: NTB transport layer queue the entry is to be enqueued on
1969 * @cb: per buffer pointer for callback function to use
1970 * @data: pointer to data buffer that incoming packets will be copied into
1971 * @len: length of the data buffer
1972 *
1973 * Enqueue a new receive buffer onto the transport queue into which a NTB
1974 * payload can be received into.
1975 *
1976 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1977 */
1978int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1979 unsigned int len)
1980{
1981 struct ntb_queue_entry *entry;
1982
1983 if (!qp)
1984 return -EINVAL;
1985
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001986 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001987 if (!entry)
1988 return -ENOMEM;
1989
1990 entry->cb_data = cb;
1991 entry->buf = data;
1992 entry->len = len;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001993 entry->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07001994 entry->retries = 0;
1995 entry->errors = 0;
1996 entry->rx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001997
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001998 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
1999
Dave Jiange9021332016-02-23 09:11:36 -07002000 if (qp->active)
2001 tasklet_schedule(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002002
2003 return 0;
2004}
2005EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2006
2007/**
2008 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2009 * @qp: NTB transport layer queue the entry is to be enqueued on
2010 * @cb: per buffer pointer for callback function to use
2011 * @data: pointer to data buffer that will be sent
2012 * @len: length of the data buffer
2013 *
2014 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07002015 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07002016 * serialize access to the qp.
2017 *
2018 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2019 */
2020int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2021 unsigned int len)
2022{
2023 struct ntb_queue_entry *entry;
2024 int rc;
2025
Allen Hubbee26a5842015-04-09 10:33:20 -04002026 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002027 return -EINVAL;
2028
2029 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07002030 if (!entry) {
2031 qp->tx_err_no_buf++;
Dave Jiange74bfee2015-07-13 08:07:17 -04002032 return -EBUSY;
Jon Mason282a2fe2013-02-12 09:52:50 -07002033 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07002034
2035 entry->cb_data = cb;
2036 entry->buf = data;
2037 entry->len = len;
2038 entry->flags = 0;
Dave Jiang9cabc262016-07-20 13:14:07 -07002039 entry->errors = 0;
2040 entry->retries = 0;
2041 entry->tx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002042
2043 rc = ntb_process_tx(qp, entry);
2044 if (rc)
2045 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2046 &qp->tx_free_q);
2047
2048 return rc;
2049}
2050EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2051
2052/**
2053 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2054 * @qp: NTB transport layer queue to be enabled
2055 *
2056 * Notify NTB transport layer of client readiness to use queue
2057 */
2058void ntb_transport_link_up(struct ntb_transport_qp *qp)
2059{
2060 if (!qp)
2061 return;
2062
Allen Hubbee26a5842015-04-09 10:33:20 -04002063 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002064
Allen Hubbee26a5842015-04-09 10:33:20 -04002065 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002066 schedule_delayed_work(&qp->link_work, 0);
2067}
2068EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2069
2070/**
2071 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2072 * @qp: NTB transport layer queue to be disabled
2073 *
2074 * Notify NTB transport layer of client's desire to no longer receive data on
2075 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07002076 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07002077 */
2078void ntb_transport_link_down(struct ntb_transport_qp *qp)
2079{
Allen Hubbee26a5842015-04-09 10:33:20 -04002080 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002081
2082 if (!qp)
2083 return;
2084
Allen Hubbee26a5842015-04-09 10:33:20 -04002085 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002086
Allen Hubbee26a5842015-04-09 10:33:20 -04002087 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002088
Allen Hubbee26a5842015-04-09 10:33:20 -04002089 ntb_peer_spad_write(qp->ndev, QP_LINKS,
2090 val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07002091
Allen Hubbee26a5842015-04-09 10:33:20 -04002092 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002093 ntb_send_link_down(qp);
2094 else
2095 cancel_delayed_work_sync(&qp->link_work);
2096}
2097EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2098
2099/**
2100 * ntb_transport_link_query - Query transport link state
2101 * @qp: NTB transport layer queue to be queried
2102 *
2103 * Query connectivity to the remote system of the NTB transport queue
2104 *
2105 * RETURNS: true for link up or false for link down
2106 */
2107bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2108{
Jon Mason186f27f2013-01-22 11:35:40 -07002109 if (!qp)
2110 return false;
2111
Allen Hubbee26a5842015-04-09 10:33:20 -04002112 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002113}
2114EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2115
2116/**
2117 * ntb_transport_qp_num - Query the qp number
2118 * @qp: NTB transport layer queue to be queried
2119 *
2120 * Query qp number of the NTB transport queue
2121 *
2122 * RETURNS: a zero based number specifying the qp number
2123 */
2124unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2125{
Jon Mason186f27f2013-01-22 11:35:40 -07002126 if (!qp)
2127 return 0;
2128
Jon Masonfce8a7b2012-11-16 19:27:12 -07002129 return qp->qp_num;
2130}
2131EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2132
2133/**
2134 * ntb_transport_max_size - Query the max payload size of a qp
2135 * @qp: NTB transport layer queue to be queried
2136 *
2137 * Query the maximum payload size permissible on the given qp
2138 *
2139 * RETURNS: the max payload size of a qp
2140 */
Jon Masonef114ed2013-01-19 02:02:18 -07002141unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002142{
Dave Jiang04afde42015-09-17 13:27:04 -07002143 unsigned int max_size;
Dave Jiang569410ca2015-07-13 08:07:22 -04002144 unsigned int copy_align;
Dave Jiang04afde42015-09-17 13:27:04 -07002145 struct dma_chan *rx_chan, *tx_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002146
Jon Mason186f27f2013-01-22 11:35:40 -07002147 if (!qp)
2148 return 0;
2149
Dave Jiang04afde42015-09-17 13:27:04 -07002150 rx_chan = qp->rx_dma_chan;
2151 tx_chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002152
Dave Jiang04afde42015-09-17 13:27:04 -07002153 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2154 tx_chan ? tx_chan->device->copy_align : 0);
Dave Jiang569410ca2015-07-13 08:07:22 -04002155
Jon Mason282a2fe2013-02-12 09:52:50 -07002156 /* If DMA engine usage is possible, try to find the max size for that */
Dave Jiang04afde42015-09-17 13:27:04 -07002157 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2158 max_size = round_down(max_size, 1 << copy_align);
Jon Mason282a2fe2013-02-12 09:52:50 -07002159
Dave Jiang04afde42015-09-17 13:27:04 -07002160 return max_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002161}
2162EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04002163
Dave Jiange74bfee2015-07-13 08:07:17 -04002164unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2165{
2166 unsigned int head = qp->tx_index;
2167 unsigned int tail = qp->remote_rx_info->entry;
2168
2169 return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2170}
2171EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2172
Allen Hubbee26a5842015-04-09 10:33:20 -04002173static void ntb_transport_doorbell_callback(void *data, int vector)
2174{
2175 struct ntb_transport_ctx *nt = data;
2176 struct ntb_transport_qp *qp;
2177 u64 db_bits;
2178 unsigned int qp_num;
2179
2180 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2181 ntb_db_vector_mask(nt->ndev, vector));
2182
2183 while (db_bits) {
2184 qp_num = __ffs(db_bits);
2185 qp = &nt->qp_vec[qp_num];
2186
Dave Jiange9021332016-02-23 09:11:36 -07002187 if (qp->active)
2188 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04002189
2190 db_bits &= ~BIT_ULL(qp_num);
2191 }
2192}
2193
2194static const struct ntb_ctx_ops ntb_transport_ops = {
2195 .link_event = ntb_transport_event_callback,
2196 .db_event = ntb_transport_doorbell_callback,
2197};
2198
2199static struct ntb_client ntb_transport_client = {
2200 .ops = {
2201 .probe = ntb_transport_probe,
2202 .remove = ntb_transport_free,
2203 },
2204};
2205
2206static int __init ntb_transport_init(void)
2207{
2208 int rc;
2209
Dave Jiang7eb38782015-06-15 08:21:33 -04002210 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2211
Allen Hubbee26a5842015-04-09 10:33:20 -04002212 if (debugfs_initialized())
2213 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2214
2215 rc = bus_register(&ntb_transport_bus);
2216 if (rc)
2217 goto err_bus;
2218
2219 rc = ntb_register_client(&ntb_transport_client);
2220 if (rc)
2221 goto err_client;
2222
2223 return 0;
2224
2225err_client:
2226 bus_unregister(&ntb_transport_bus);
2227err_bus:
2228 debugfs_remove_recursive(nt_debugfs_dir);
2229 return rc;
2230}
2231module_init(ntb_transport_init);
2232
2233static void __exit ntb_transport_exit(void)
2234{
Allen Hubbee26a5842015-04-09 10:33:20 -04002235 ntb_unregister_client(&ntb_transport_client);
2236 bus_unregister(&ntb_transport_bus);
Allen Hubbe43325382016-12-27 17:57:04 -05002237 debugfs_remove_recursive(nt_debugfs_dir);
Allen Hubbee26a5842015-04-09 10:33:20 -04002238}
2239module_exit(ntb_transport_exit);