blob: c234ee43b6ef4b5b85d1f738df695dc3194d59c0 [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;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700179 u64 dma_rx_prep_err;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700180 u64 tx_bytes;
181 u64 tx_pkts;
182 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700183 u64 tx_err_no_buf;
184 u64 tx_memcpy;
185 u64 tx_async;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700186 u64 dma_tx_prep_err;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700187};
188
189struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400190 phys_addr_t phys_addr;
191 resource_size_t phys_size;
192 resource_size_t xlat_align;
193 resource_size_t xlat_align_size;
194 void __iomem *vbase;
195 size_t xlat_size;
196 size_t buff_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700197 void *virt_addr;
198 dma_addr_t dma_addr;
199};
200
201struct ntb_transport_client_dev {
202 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400203 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700204 struct device dev;
205};
206
Allen Hubbee26a5842015-04-09 10:33:20 -0400207struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700208 struct list_head entry;
209 struct list_head client_devs;
210
Allen Hubbee26a5842015-04-09 10:33:20 -0400211 struct ntb_dev *ndev;
212
213 struct ntb_transport_mw *mw_vec;
214 struct ntb_transport_qp *qp_vec;
215 unsigned int mw_count;
216 unsigned int qp_count;
217 u64 qp_bitmap;
218 u64 qp_bitmap_free;
219
220 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700221 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700222 struct work_struct link_cleanup;
Dave Jiangc8650fd2015-07-13 08:07:09 -0400223
224 struct dentry *debugfs_node_dir;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700225};
226
227enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400228 DESC_DONE_FLAG = BIT(0),
229 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700230};
231
232struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700233 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700234 unsigned int len;
235 unsigned int flags;
236};
237
238enum {
239 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700240 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700241 NUM_QPS,
242 NUM_MWS,
243 MW0_SZ_HIGH,
244 MW0_SZ_LOW,
245 MW1_SZ_HIGH,
246 MW1_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700247 MAX_SPAD,
248};
249
Allen Hubbee26a5842015-04-09 10:33:20 -0400250#define dev_client_dev(__dev) \
251 container_of((__dev), struct ntb_transport_client_dev, dev)
252
253#define drv_client(__drv) \
254 container_of((__drv), struct ntb_transport_client, driver)
255
256#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700257#define NTB_QP_DEF_NUM_ENTRIES 100
258#define NTB_LINK_DOWN_TIMEOUT 10
Dave Jiang8c874cc2016-01-08 09:51:05 -0700259#define DMA_RETRIES 20
Nicholas Mc Guirec0a88032016-08-22 18:51:35 +0200260#define DMA_OUT_RESOURCE_TO msecs_to_jiffies(50)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700261
Allen Hubbee26a5842015-04-09 10:33:20 -0400262static void ntb_transport_rxc_db(unsigned long data);
263static const struct ntb_ctx_ops ntb_transport_ops;
264static struct ntb_client ntb_transport_client;
Dave Jiang9cabc262016-07-20 13:14:07 -0700265static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
266 struct ntb_queue_entry *entry);
267static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
Dave Jiang72203572016-07-20 13:14:13 -0700268static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
269static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
270
Allen Hubbee26a5842015-04-09 10:33:20 -0400271
272static int ntb_transport_bus_match(struct device *dev,
273 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700274{
275 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
276}
277
Allen Hubbee26a5842015-04-09 10:33:20 -0400278static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700279{
Allen Hubbee26a5842015-04-09 10:33:20 -0400280 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700281 int rc = -EINVAL;
282
283 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400284
285 client = drv_client(dev->driver);
286 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700287 if (rc)
288 put_device(dev);
289
290 return rc;
291}
292
Allen Hubbee26a5842015-04-09 10:33:20 -0400293static int ntb_transport_bus_remove(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700294{
Allen Hubbee26a5842015-04-09 10:33:20 -0400295 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700296
Allen Hubbee26a5842015-04-09 10:33:20 -0400297 client = drv_client(dev->driver);
298 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700299
300 put_device(dev);
301
302 return 0;
303}
304
Allen Hubbee26a5842015-04-09 10:33:20 -0400305static struct bus_type ntb_transport_bus = {
306 .name = "ntb_transport",
307 .match = ntb_transport_bus_match,
308 .probe = ntb_transport_bus_probe,
309 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700310};
311
312static LIST_HEAD(ntb_transport_list);
313
Allen Hubbee26a5842015-04-09 10:33:20 -0400314static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700315{
Dave Jiang31510002015-07-13 08:07:19 -0400316 list_add_tail(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700317 return 0;
318}
319
Allen Hubbee26a5842015-04-09 10:33:20 -0400320static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700321{
322 struct ntb_transport_client_dev *client_dev, *cd;
323
324 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
325 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
326 dev_name(&client_dev->dev));
327 list_del(&client_dev->entry);
328 device_unregister(&client_dev->dev);
329 }
330
331 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700332}
333
Allen Hubbee26a5842015-04-09 10:33:20 -0400334static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700335{
336 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700337
Allen Hubbee26a5842015-04-09 10:33:20 -0400338 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700339 kfree(client_dev);
340}
341
342/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400343 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700344 * @device_name: Name of NTB client device
345 *
346 * Unregister an NTB client device with the NTB transport layer
347 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400348void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700349{
350 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400351 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700352
353 list_for_each_entry(nt, &ntb_transport_list, entry)
354 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
355 if (!strncmp(dev_name(&client->dev), device_name,
356 strlen(device_name))) {
357 list_del(&client->entry);
358 device_unregister(&client->dev);
359 }
360}
Allen Hubbee26a5842015-04-09 10:33:20 -0400361EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700362
363/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400364 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700365 * @device_name: Name of NTB client device
366 *
367 * Register an NTB client device with the NTB transport layer
368 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400369int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700370{
371 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400372 struct ntb_transport_ctx *nt;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400373 int node;
Jon Mason8b19d452013-04-26 14:51:57 -0700374 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700375
Jon Mason8222b402013-01-19 02:02:16 -0700376 if (list_empty(&ntb_transport_list))
377 return -ENODEV;
378
Jon Masonfce8a7b2012-11-16 19:27:12 -0700379 list_for_each_entry(nt, &ntb_transport_list, entry) {
380 struct device *dev;
381
Allen Hubbe1199aa62015-05-18 06:20:47 -0400382 node = dev_to_node(&nt->ndev->dev);
383
384 client_dev = kzalloc_node(sizeof(*client_dev),
385 GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700386 if (!client_dev) {
387 rc = -ENOMEM;
388 goto err;
389 }
390
391 dev = &client_dev->dev;
392
393 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700394 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400395 dev->bus = &ntb_transport_bus;
396 dev->release = ntb_transport_client_release;
397 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700398
399 rc = device_register(dev);
400 if (rc) {
401 kfree(client_dev);
402 goto err;
403 }
404
405 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700406 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700407 }
408
409 return 0;
410
411err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400412 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700413
414 return rc;
415}
Allen Hubbee26a5842015-04-09 10:33:20 -0400416EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700417
418/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400419 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700420 * @drv: NTB client driver to be registered
421 *
422 * Register an NTB client driver with the NTB transport layer
423 *
424 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
425 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400426int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700427{
Allen Hubbee26a5842015-04-09 10:33:20 -0400428 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700429
Jon Mason8222b402013-01-19 02:02:16 -0700430 if (list_empty(&ntb_transport_list))
431 return -ENODEV;
432
Jon Masonfce8a7b2012-11-16 19:27:12 -0700433 return driver_register(&drv->driver);
434}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400435EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700436
437/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400438 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700439 * @drv: NTB client driver to be unregistered
440 *
441 * Unregister an NTB client driver with the NTB transport layer
442 *
443 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
444 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400445void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700446{
447 driver_unregister(&drv->driver);
448}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400449EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700450
Jon Masonfce8a7b2012-11-16 19:27:12 -0700451static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
452 loff_t *offp)
453{
454 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700455 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700456 ssize_t ret, out_offset, out_count;
457
Dave Jiang260bee92015-07-13 08:07:11 -0400458 qp = filp->private_data;
459
460 if (!qp || !qp->link_is_up)
461 return 0;
462
Jon Mason282a2fe2013-02-12 09:52:50 -0700463 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700464
465 buf = kmalloc(out_count, GFP_KERNEL);
466 if (!buf)
467 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700468
Jon Masonfce8a7b2012-11-16 19:27:12 -0700469 out_offset = 0;
470 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400471 "\nNTB QP stats:\n\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700472 out_offset += snprintf(buf + out_offset, out_count - out_offset,
473 "rx_bytes - \t%llu\n", qp->rx_bytes);
474 out_offset += snprintf(buf + out_offset, out_count - out_offset,
475 "rx_pkts - \t%llu\n", qp->rx_pkts);
476 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700477 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
478 out_offset += snprintf(buf + out_offset, out_count - out_offset,
479 "rx_async - \t%llu\n", qp->rx_async);
480 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700481 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
482 out_offset += snprintf(buf + out_offset, out_count - out_offset,
483 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
484 out_offset += snprintf(buf + out_offset, out_count - out_offset,
485 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
486 out_offset += snprintf(buf + out_offset, out_count - out_offset,
487 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
488 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400489 "rx_buff - \t0x%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700490 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700491 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700492 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jianga754a8f2016-04-08 10:49:06 -0700493 "rx_max_entry - \t%u\n", qp->rx_max_entry);
494 out_offset += snprintf(buf + out_offset, out_count - out_offset,
495 "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700496
497 out_offset += snprintf(buf + out_offset, out_count - out_offset,
498 "tx_bytes - \t%llu\n", qp->tx_bytes);
499 out_offset += snprintf(buf + out_offset, out_count - out_offset,
500 "tx_pkts - \t%llu\n", qp->tx_pkts);
501 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700502 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
503 out_offset += snprintf(buf + out_offset, out_count - out_offset,
504 "tx_async - \t%llu\n", qp->tx_async);
505 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700506 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
507 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700508 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
509 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400510 "tx_mw - \t0x%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700511 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400512 "tx_index (H) - \t%u\n", qp->tx_index);
513 out_offset += snprintf(buf + out_offset, out_count - out_offset,
514 "RRI (T) - \t%u\n",
515 qp->remote_rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700516 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700517 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400518 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiange74bfee2015-07-13 08:07:17 -0400519 "free tx - \t%u\n",
520 ntb_transport_tx_free_entry(qp));
Dave Jiang8c874cc2016-01-08 09:51:05 -0700521 out_offset += snprintf(buf + out_offset, out_count - out_offset,
522 "DMA tx prep err - \t%llu\n",
523 qp->dma_tx_prep_err);
524 out_offset += snprintf(buf + out_offset, out_count - out_offset,
525 "DMA rx prep err - \t%llu\n",
526 qp->dma_rx_prep_err);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700527
528 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400529 "\n");
530 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiang569410ca2015-07-13 08:07:22 -0400531 "Using TX DMA - \t%s\n",
532 qp->tx_dma_chan ? "Yes" : "No");
533 out_offset += snprintf(buf + out_offset, out_count - out_offset,
534 "Using RX DMA - \t%s\n",
535 qp->rx_dma_chan ? "Yes" : "No");
Dave Jiangd98ef992015-07-13 08:07:20 -0400536 out_offset += snprintf(buf + out_offset, out_count - out_offset,
537 "QP Link - \t%s\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400538 qp->link_is_up ? "Up" : "Down");
Dave Jiangd98ef992015-07-13 08:07:20 -0400539 out_offset += snprintf(buf + out_offset, out_count - out_offset,
540 "\n");
541
Jon Masond7237e22013-01-19 02:02:25 -0700542 if (out_offset > out_count)
543 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700544
545 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700546 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700547 return ret;
548}
549
550static const struct file_operations ntb_qp_debugfs_stats = {
551 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700552 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700553 .read = debugfs_read,
554};
555
556static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
557 struct list_head *list)
558{
559 unsigned long flags;
560
561 spin_lock_irqsave(lock, flags);
562 list_add_tail(entry, list);
563 spin_unlock_irqrestore(lock, flags);
564}
565
566static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700567 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700568{
569 struct ntb_queue_entry *entry;
570 unsigned long flags;
571
572 spin_lock_irqsave(lock, flags);
573 if (list_empty(list)) {
574 entry = NULL;
575 goto out;
576 }
577 entry = list_first_entry(list, struct ntb_queue_entry, entry);
578 list_del(&entry->entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400579
Jon Masonfce8a7b2012-11-16 19:27:12 -0700580out:
581 spin_unlock_irqrestore(lock, flags);
582
583 return entry;
584}
585
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400586static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
587 struct list_head *list,
588 struct list_head *to_list)
589{
590 struct ntb_queue_entry *entry;
591 unsigned long flags;
592
593 spin_lock_irqsave(lock, flags);
594
595 if (list_empty(list)) {
596 entry = NULL;
597 } else {
598 entry = list_first_entry(list, struct ntb_queue_entry, entry);
599 list_move_tail(&entry->entry, to_list);
600 }
601
602 spin_unlock_irqrestore(lock, flags);
603
604 return entry;
605}
606
Allen Hubbee26a5842015-04-09 10:33:20 -0400607static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
608 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700609{
Allen Hubbee26a5842015-04-09 10:33:20 -0400610 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
611 struct ntb_transport_mw *mw;
Dave Jianga754a8f2016-04-08 10:49:06 -0700612 struct ntb_dev *ndev = nt->ndev;
613 struct ntb_queue_entry *entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700614 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400615 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700616 unsigned int i;
Dave Jianga754a8f2016-04-08 10:49:06 -0700617 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700618
Allen Hubbee26a5842015-04-09 10:33:20 -0400619 mw_count = nt->mw_count;
620 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700621
Allen Hubbee26a5842015-04-09 10:33:20 -0400622 mw_num = QP_TO_MW(nt, qp_num);
623 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700624
Allen Hubbee26a5842015-04-09 10:33:20 -0400625 if (!mw->virt_addr)
626 return -ENOMEM;
627
628 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
629 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700630 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400631 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700632
Allen Hubbee26a5842015-04-09 10:33:20 -0400633 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -0400634 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
Jon Mason793c20e2013-01-19 02:02:26 -0700635 rx_size -= sizeof(struct ntb_rx_info);
636
Jon Mason282a2fe2013-02-12 09:52:50 -0700637 qp->remote_rx_info = qp->rx_buff + rx_size;
638
Jon Masonc9d534c2013-02-01 15:45:16 -0700639 /* Due to housekeeping, there must be atleast 2 buffs */
640 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700641 qp->rx_max_entry = rx_size / qp->rx_max_frame;
642 qp->rx_index = 0;
643
Dave Jianga754a8f2016-04-08 10:49:06 -0700644 /*
645 * Checking to see if we have more entries than the default.
646 * We should add additional entries if that is the case so we
647 * can be in sync with the transport frames.
648 */
649 node = dev_to_node(&ndev->dev);
650 for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
651 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
652 if (!entry)
653 return -ENOMEM;
654
655 entry->qp = qp;
656 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
657 &qp->rx_free_q);
658 qp->rx_alloc_entry++;
659 }
660
Jon Masonc9d534c2013-02-01 15:45:16 -0700661 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700662
Jon Masonef114ed2013-01-19 02:02:18 -0700663 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700664 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400665 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
666 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700667 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700668 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700669
670 qp->rx_pkts = 0;
671 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700672 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400673
674 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700675}
676
Allen Hubbee26a5842015-04-09 10:33:20 -0400677static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700678{
Allen Hubbee26a5842015-04-09 10:33:20 -0400679 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
680 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700681
682 if (!mw->virt_addr)
683 return;
684
Allen Hubbee26a5842015-04-09 10:33:20 -0400685 ntb_mw_clear_trans(nt->ndev, num_mw);
686 dma_free_coherent(&pdev->dev, mw->buff_size,
687 mw->virt_addr, mw->dma_addr);
688 mw->xlat_size = 0;
689 mw->buff_size = 0;
Jon Mason113fc502013-01-30 11:40:52 -0700690 mw->virt_addr = NULL;
691}
692
Allen Hubbee26a5842015-04-09 10:33:20 -0400693static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400694 resource_size_t size)
Jon Masonb77b2632013-02-01 15:25:37 -0700695{
Allen Hubbee26a5842015-04-09 10:33:20 -0400696 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
697 struct pci_dev *pdev = nt->ndev->pdev;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400698 size_t xlat_size, buff_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400699 int rc;
700
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400701 if (!size)
702 return -EINVAL;
703
Allen Hubbee26a5842015-04-09 10:33:20 -0400704 xlat_size = round_up(size, mw->xlat_align_size);
705 buff_size = round_up(size, mw->xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700706
707 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400708 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700709 return 0;
710
Allen Hubbee26a5842015-04-09 10:33:20 -0400711 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700712 ntb_free_mw(nt, num_mw);
713
Allen Hubbee26a5842015-04-09 10:33:20 -0400714 /* Alloc memory for receiving data. Must be aligned */
715 mw->xlat_size = xlat_size;
716 mw->buff_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700717
Allen Hubbee26a5842015-04-09 10:33:20 -0400718 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
719 &mw->dma_addr, GFP_KERNEL);
Jon Masonb77b2632013-02-01 15:25:37 -0700720 if (!mw->virt_addr) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400721 mw->xlat_size = 0;
722 mw->buff_size = 0;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400723 dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400724 buff_size);
Jon Masonb77b2632013-02-01 15:25:37 -0700725 return -ENOMEM;
726 }
727
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700728 /*
729 * we must ensure that the memory address allocated is BAR size
730 * aligned in order for the XLAT register to take the value. This
731 * is a requirement of the hardware. It is recommended to setup CMA
732 * for BAR sizes equal or greater than 4MB.
733 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400734 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
735 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700736 &mw->dma_addr);
737 ntb_free_mw(nt, num_mw);
738 return -ENOMEM;
739 }
740
Jon Masonb77b2632013-02-01 15:25:37 -0700741 /* Notify HW the memory location of the receive buffer */
Allen Hubbee26a5842015-04-09 10:33:20 -0400742 rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
743 if (rc) {
744 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
745 ntb_free_mw(nt, num_mw);
746 return -EIO;
747 }
Jon Masonb77b2632013-02-01 15:25:37 -0700748
749 return 0;
750}
751
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400752static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
753{
754 qp->link_is_up = false;
Dave Jiange9021332016-02-23 09:11:36 -0700755 qp->active = false;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400756
757 qp->tx_index = 0;
758 qp->rx_index = 0;
759 qp->rx_bytes = 0;
760 qp->rx_pkts = 0;
761 qp->rx_ring_empty = 0;
762 qp->rx_err_no_buf = 0;
763 qp->rx_err_oflow = 0;
764 qp->rx_err_ver = 0;
765 qp->rx_memcpy = 0;
766 qp->rx_async = 0;
767 qp->tx_bytes = 0;
768 qp->tx_pkts = 0;
769 qp->tx_ring_full = 0;
770 qp->tx_err_no_buf = 0;
771 qp->tx_memcpy = 0;
772 qp->tx_async = 0;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700773 qp->dma_tx_prep_err = 0;
774 qp->dma_rx_prep_err = 0;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400775}
776
Jon Masonfca4d512013-09-09 13:39:55 -0700777static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700778{
Allen Hubbee26a5842015-04-09 10:33:20 -0400779 struct ntb_transport_ctx *nt = qp->transport;
780 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700781
Allen Hubbee22e0b92015-05-12 06:55:44 -0400782 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400783
784 cancel_delayed_work_sync(&qp->link_work);
785 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400786
787 if (qp->event_handler)
788 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700789}
790
791static void ntb_qp_link_cleanup_work(struct work_struct *work)
792{
793 struct ntb_transport_qp *qp = container_of(work,
794 struct ntb_transport_qp,
795 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400796 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700797
798 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700799
Allen Hubbee26a5842015-04-09 10:33:20 -0400800 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700801 schedule_delayed_work(&qp->link_work,
802 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
803}
804
Jon Mason7b4f2d32013-01-19 02:02:19 -0700805static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700806{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700807 schedule_work(&qp->link_cleanup);
808}
809
Allen Hubbee26a5842015-04-09 10:33:20 -0400810static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700811{
Allen Hubbee26a5842015-04-09 10:33:20 -0400812 struct ntb_transport_qp *qp;
813 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700814 int i;
815
Allen Hubbee26a5842015-04-09 10:33:20 -0400816 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700817
Allen Hubbee26a5842015-04-09 10:33:20 -0400818 /* Pass along the info to any clients */
819 for (i = 0; i < nt->qp_count; i++)
820 if (qp_bitmap_alloc & BIT_ULL(i)) {
821 qp = &nt->qp_vec[i];
822 ntb_qp_link_cleanup(qp);
823 cancel_work_sync(&qp->link_cleanup);
824 cancel_delayed_work_sync(&qp->link_work);
825 }
826
827 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700828 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700829
Jon Masonfce8a7b2012-11-16 19:27:12 -0700830 /* The scratchpad registers keep the values if the remote side
831 * goes down, blast them now to give them a sane value the next
832 * time they are accessed
833 */
834 for (i = 0; i < MAX_SPAD; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400835 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700836}
837
Jon Masonfca4d512013-09-09 13:39:55 -0700838static void ntb_transport_link_cleanup_work(struct work_struct *work)
839{
Allen Hubbee26a5842015-04-09 10:33:20 -0400840 struct ntb_transport_ctx *nt =
841 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700842
843 ntb_transport_link_cleanup(nt);
844}
845
Allen Hubbee26a5842015-04-09 10:33:20 -0400846static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700847{
Allen Hubbee26a5842015-04-09 10:33:20 -0400848 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700849
Allen Hubbee26a5842015-04-09 10:33:20 -0400850 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700851 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400852 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700853 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700854}
855
856static void ntb_transport_link_work(struct work_struct *work)
857{
Allen Hubbee26a5842015-04-09 10:33:20 -0400858 struct ntb_transport_ctx *nt =
859 container_of(work, struct ntb_transport_ctx, link_work.work);
860 struct ntb_dev *ndev = nt->ndev;
861 struct pci_dev *pdev = ndev->pdev;
862 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700863 u32 val;
Dave Jiang84f76682016-02-29 09:35:26 -0700864 int rc = 0, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700865
Jon Mason113fc502013-01-30 11:40:52 -0700866 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400867 for (i = 0; i < nt->mw_count; i++) {
868 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700869
Allen Hubbee26a5842015-04-09 10:33:20 -0400870 if (max_mw_size && size > max_mw_size)
871 size = max_mw_size;
872
873 spad = MW0_SZ_HIGH + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200874 ntb_peer_spad_write(ndev, spad, upper_32_bits(size));
Allen Hubbee26a5842015-04-09 10:33:20 -0400875
876 spad = MW0_SZ_LOW + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200877 ntb_peer_spad_write(ndev, spad, lower_32_bits(size));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700878 }
879
Allen Hubbee26a5842015-04-09 10:33:20 -0400880 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700881
Allen Hubbee26a5842015-04-09 10:33:20 -0400882 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700883
Allen Hubbee26a5842015-04-09 10:33:20 -0400884 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700885
886 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400887 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400888 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700889 if (val != NTB_TRANSPORT_VERSION)
890 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700891
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400892 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700893 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400894 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700895 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700896
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400897 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700898 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400899 if (val != nt->mw_count)
900 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700901
Allen Hubbee26a5842015-04-09 10:33:20 -0400902 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700903 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700904
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400905 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400906 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700907
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400908 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700909 val64 |= val;
910
Allen Hubbee26a5842015-04-09 10:33:20 -0400911 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700912
913 rc = ntb_set_mw(nt, i, val64);
914 if (rc)
915 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700916 }
917
Allen Hubbee26a5842015-04-09 10:33:20 -0400918 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700919
Allen Hubbee26a5842015-04-09 10:33:20 -0400920 for (i = 0; i < nt->qp_count; i++) {
921 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700922
923 ntb_transport_setup_qp_mw(nt, i);
924
Allen Hubbee26a5842015-04-09 10:33:20 -0400925 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700926 schedule_delayed_work(&qp->link_work, 0);
927 }
928
929 return;
930
Jon Mason113fc502013-01-30 11:40:52 -0700931out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400932 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700933 ntb_free_mw(nt, i);
Dave Jiang84f76682016-02-29 09:35:26 -0700934
935 /* if there's an actual failure, we should just bail */
936 if (rc < 0) {
937 ntb_link_disable(ndev);
938 return;
939 }
940
Jon Masonfce8a7b2012-11-16 19:27:12 -0700941out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400942 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700943 schedule_delayed_work(&nt->link_work,
944 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
945}
946
947static void ntb_qp_link_work(struct work_struct *work)
948{
949 struct ntb_transport_qp *qp = container_of(work,
950 struct ntb_transport_qp,
951 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400952 struct pci_dev *pdev = qp->ndev->pdev;
953 struct ntb_transport_ctx *nt = qp->transport;
954 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700955
Allen Hubbee26a5842015-04-09 10:33:20 -0400956 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700957
Allen Hubbee26a5842015-04-09 10:33:20 -0400958 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700959
Allen Hubbee26a5842015-04-09 10:33:20 -0400960 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700961
962 /* query remote spad for qp ready bits */
Allen Hubbee26a5842015-04-09 10:33:20 -0400963 ntb_peer_spad_read(nt->ndev, QP_LINKS);
Allen Hubbe28762282015-05-11 10:08:26 -0400964 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700965
966 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400967 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700968 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400969 qp->link_is_up = true;
Dave Jiange9021332016-02-23 09:11:36 -0700970 qp->active = true;
Allen Hubbee26a5842015-04-09 10:33:20 -0400971
Jon Masonfce8a7b2012-11-16 19:27:12 -0700972 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400973 qp->event_handler(qp->cb_data, qp->link_is_up);
Allen Hubbe8b5a22d2015-07-13 08:07:12 -0400974
Dave Jiange9021332016-02-23 09:11:36 -0700975 if (qp->active)
976 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400977 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700978 schedule_delayed_work(&qp->link_work,
979 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
980}
981
Allen Hubbee26a5842015-04-09 10:33:20 -0400982static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700983 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700984{
985 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400986 phys_addr_t mw_base;
987 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700988 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400989 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700990 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700991
Allen Hubbee26a5842015-04-09 10:33:20 -0400992 mw_count = nt->mw_count;
993 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700994
Allen Hubbee26a5842015-04-09 10:33:20 -0400995 mw_num = QP_TO_MW(nt, qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400996
997 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700998 qp->qp_num = qp_num;
999 qp->transport = nt;
1000 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -04001001 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001002 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001003 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001004
Allen Hubbee26a5842015-04-09 10:33:20 -04001005 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
1006 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -07001007 else
Allen Hubbee26a5842015-04-09 10:33:20 -04001008 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -07001009
Allen Hubbee26a5842015-04-09 10:33:20 -04001010 mw_base = nt->mw_vec[mw_num].phys_addr;
1011 mw_size = nt->mw_vec[mw_num].phys_size;
1012
1013 tx_size = (unsigned int)mw_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -04001014 qp_offset = tx_size * (qp_num / mw_count);
Allen Hubbee26a5842015-04-09 10:33:20 -04001015
1016 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001017 if (!qp->tx_mw)
1018 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -07001019
Allen Hubbee26a5842015-04-09 10:33:20 -04001020 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001021 if (!qp->tx_mw_phys)
1022 return -EINVAL;
1023
1024 tx_size -= sizeof(struct ntb_rx_info);
1025 qp->rx_info = qp->tx_mw + tx_size;
1026
Jon Masonc9d534c2013-02-01 15:45:16 -07001027 /* Due to housekeeping, there must be atleast 2 buffs */
1028 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -07001029 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -07001030
Dave Jiangc8650fd2015-07-13 08:07:09 -04001031 if (nt->debugfs_node_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001032 char debugfs_name[4];
1033
1034 snprintf(debugfs_name, 4, "qp%d", qp_num);
1035 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Dave Jiangc8650fd2015-07-13 08:07:09 -04001036 nt->debugfs_node_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001037
1038 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1039 qp->debugfs_dir, qp,
1040 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -04001041 } else {
1042 qp->debugfs_dir = NULL;
1043 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001044 }
1045
1046 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001047 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001048
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001049 spin_lock_init(&qp->ntb_rx_q_lock);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001050 spin_lock_init(&qp->ntb_tx_free_q_lock);
1051
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001052 INIT_LIST_HEAD(&qp->rx_post_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001053 INIT_LIST_HEAD(&qp->rx_pend_q);
1054 INIT_LIST_HEAD(&qp->rx_free_q);
1055 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001056
Allen Hubbee26a5842015-04-09 10:33:20 -04001057 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1058 (unsigned long)qp);
1059
Jon Mason282a2fe2013-02-12 09:52:50 -07001060 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001061}
1062
Allen Hubbee26a5842015-04-09 10:33:20 -04001063static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001064{
Allen Hubbee26a5842015-04-09 10:33:20 -04001065 struct ntb_transport_ctx *nt;
1066 struct ntb_transport_mw *mw;
1067 unsigned int mw_count, qp_count;
1068 u64 qp_bitmap;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001069 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001070 int rc, i;
1071
Logan Gunthorpe19645a02016-06-07 11:20:22 -06001072 mw_count = ntb_mw_count(ndev);
1073 if (ntb_spad_count(ndev) < (NUM_MWS + 1 + mw_count * 2)) {
1074 dev_err(&ndev->dev, "Not enough scratch pad registers for %s",
1075 NTB_TRANSPORT_NAME);
1076 return -EIO;
1077 }
1078
Allen Hubbee26a5842015-04-09 10:33:20 -04001079 if (ntb_db_is_unsafe(ndev))
1080 dev_dbg(&ndev->dev,
1081 "doorbell is unsafe, proceed anyway...\n");
1082 if (ntb_spad_is_unsafe(ndev))
1083 dev_dbg(&ndev->dev,
1084 "scratchpad is unsafe, proceed anyway...\n");
1085
Allen Hubbe1199aa62015-05-18 06:20:47 -04001086 node = dev_to_node(&ndev->dev);
1087
1088 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001089 if (!nt)
1090 return -ENOMEM;
1091
Allen Hubbee26a5842015-04-09 10:33:20 -04001092 nt->ndev = ndev;
1093
Allen Hubbee26a5842015-04-09 10:33:20 -04001094 nt->mw_count = mw_count;
1095
Allen Hubbe1199aa62015-05-18 06:20:47 -04001096 nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1097 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001098 if (!nt->mw_vec) {
1099 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001100 goto err;
1101 }
1102
Allen Hubbee26a5842015-04-09 10:33:20 -04001103 for (i = 0; i < mw_count; i++) {
1104 mw = &nt->mw_vec[i];
1105
1106 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
1107 &mw->xlat_align, &mw->xlat_align_size);
1108 if (rc)
1109 goto err1;
1110
Dave Jiang06917f72015-05-19 16:45:46 -04001111 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001112 if (!mw->vbase) {
1113 rc = -ENOMEM;
1114 goto err1;
1115 }
1116
1117 mw->buff_size = 0;
1118 mw->xlat_size = 0;
1119 mw->virt_addr = NULL;
1120 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -07001121 }
1122
Allen Hubbee26a5842015-04-09 10:33:20 -04001123 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001124
Allen Hubbee26a5842015-04-09 10:33:20 -04001125 qp_count = ilog2(qp_bitmap);
1126 if (max_num_clients && max_num_clients < qp_count)
1127 qp_count = max_num_clients;
1128 else if (mw_count < qp_count)
1129 qp_count = mw_count;
1130
1131 qp_bitmap &= BIT_ULL(qp_count) - 1;
1132
1133 nt->qp_count = qp_count;
1134 nt->qp_bitmap = qp_bitmap;
1135 nt->qp_bitmap_free = qp_bitmap;
1136
Allen Hubbe1199aa62015-05-18 06:20:47 -04001137 nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1138 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001139 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001140 rc = -ENOMEM;
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301141 goto err1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001142 }
1143
Dave Jiangc8650fd2015-07-13 08:07:09 -04001144 if (nt_debugfs_dir) {
1145 nt->debugfs_node_dir =
1146 debugfs_create_dir(pci_name(ndev->pdev),
1147 nt_debugfs_dir);
1148 }
1149
Allen Hubbee26a5842015-04-09 10:33:20 -04001150 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001151 rc = ntb_transport_init_queue(nt, i);
1152 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301153 goto err2;
Jon Mason282a2fe2013-02-12 09:52:50 -07001154 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001155
1156 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001157 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001158
Allen Hubbee26a5842015-04-09 10:33:20 -04001159 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001160 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301161 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001162
1163 INIT_LIST_HEAD(&nt->client_devs);
1164 rc = ntb_bus_init(nt);
1165 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301166 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001167
Allen Hubbee26a5842015-04-09 10:33:20 -04001168 nt->link_is_up = false;
1169 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1170 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001171
1172 return 0;
1173
Jon Mason948d3a62013-04-18 17:07:36 -07001174err3:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301175 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001176err2:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301177 kfree(nt->qp_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001178err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001179 while (i--) {
1180 mw = &nt->mw_vec[i];
1181 iounmap(mw->vbase);
1182 }
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301183 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001184err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001185 kfree(nt);
1186 return rc;
1187}
1188
Allen Hubbee26a5842015-04-09 10:33:20 -04001189static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001190{
Allen Hubbee26a5842015-04-09 10:33:20 -04001191 struct ntb_transport_ctx *nt = ndev->ctx;
1192 struct ntb_transport_qp *qp;
1193 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001194 int i;
1195
Jon Masonfca4d512013-09-09 13:39:55 -07001196 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001197 cancel_work_sync(&nt->link_cleanup);
1198 cancel_delayed_work_sync(&nt->link_work);
1199
1200 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001201
1202 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001203 for (i = 0; i < nt->qp_count; i++) {
1204 qp = &nt->qp_vec[i];
1205 if (qp_bitmap_alloc & BIT_ULL(i))
1206 ntb_transport_free_queue(qp);
1207 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001208 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001209
Allen Hubbee26a5842015-04-09 10:33:20 -04001210 ntb_link_disable(ndev);
1211 ntb_clear_ctx(ndev);
1212
Jon Masonfce8a7b2012-11-16 19:27:12 -07001213 ntb_bus_remove(nt);
1214
Allen Hubbee26a5842015-04-09 10:33:20 -04001215 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001216 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001217 iounmap(nt->mw_vec[i].vbase);
1218 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001219
Allen Hubbee26a5842015-04-09 10:33:20 -04001220 kfree(nt->qp_vec);
1221 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001222 kfree(nt);
1223}
1224
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001225static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1226{
1227 struct ntb_queue_entry *entry;
1228 void *cb_data;
1229 unsigned int len;
1230 unsigned long irqflags;
1231
1232 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1233
1234 while (!list_empty(&qp->rx_post_q)) {
1235 entry = list_first_entry(&qp->rx_post_q,
1236 struct ntb_queue_entry, entry);
1237 if (!(entry->flags & DESC_DONE_FLAG))
1238 break;
1239
1240 entry->rx_hdr->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07001241 iowrite32(entry->rx_index, &qp->rx_info->entry);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001242
1243 cb_data = entry->cb_data;
1244 len = entry->len;
1245
1246 list_move_tail(&entry->entry, &qp->rx_free_q);
1247
1248 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1249
1250 if (qp->rx_handler && qp->client_ready)
1251 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1252
1253 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1254 }
1255
1256 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1257}
1258
Dave Jiang72203572016-07-20 13:14:13 -07001259static void ntb_rx_copy_callback(void *data,
1260 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001261{
Jon Mason282a2fe2013-02-12 09:52:50 -07001262 struct ntb_queue_entry *entry = data;
Jon Mason448c6fb2013-01-19 02:02:27 -07001263
Dave Jiang72203572016-07-20 13:14:13 -07001264 /* we need to check DMA results if we are using DMA */
1265 if (res) {
1266 enum dmaengine_tx_result dma_err = res->result;
1267
1268 switch (dma_err) {
1269 case DMA_TRANS_READ_FAILED:
1270 case DMA_TRANS_WRITE_FAILED:
1271 entry->errors++;
1272 case DMA_TRANS_ABORTED:
1273 {
1274 struct ntb_transport_qp *qp = entry->qp;
1275 void *offset = qp->rx_buff + qp->rx_max_frame *
1276 qp->rx_index;
1277
1278 ntb_memcpy_rx(entry, offset);
1279 qp->rx_memcpy++;
1280 return;
1281 }
1282
1283 case DMA_TRANS_NOERROR:
1284 default:
1285 break;
1286 }
1287 }
1288
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001289 entry->flags |= DESC_DONE_FLAG;
Jon Mason282a2fe2013-02-12 09:52:50 -07001290
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001291 ntb_complete_rxc(entry->qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001292}
1293
Jon Mason282a2fe2013-02-12 09:52:50 -07001294static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1295{
1296 void *buf = entry->buf;
1297 size_t len = entry->len;
1298
1299 memcpy(buf, offset, len);
1300
Allen Hubbee26a5842015-04-09 10:33:20 -04001301 /* Ensure that the data is fully copied out before clearing the flag */
1302 wmb();
1303
Dave Jiang72203572016-07-20 13:14:13 -07001304 ntb_rx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001305}
1306
Dave Jiang72203572016-07-20 13:14:13 -07001307static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
Jon Mason282a2fe2013-02-12 09:52:50 -07001308{
1309 struct dma_async_tx_descriptor *txd;
1310 struct ntb_transport_qp *qp = entry->qp;
Dave Jiang569410ca2015-07-13 08:07:22 -04001311 struct dma_chan *chan = qp->rx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001312 struct dma_device *device;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001313 size_t pay_off, buff_off, len;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001314 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001315 dma_cookie_t cookie;
1316 void *buf = entry->buf;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001317 int retries = 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001318
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001319 len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001320 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001321 pay_off = (size_t)offset & ~PAGE_MASK;
1322 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001323
1324 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Allen Hubbe905921e2015-07-13 08:07:21 -04001325 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001326
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001327 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1328 if (!unmap)
Allen Hubbe905921e2015-07-13 08:07:21 -04001329 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001330
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001331 unmap->len = len;
1332 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1333 pay_off, len, DMA_TO_DEVICE);
1334 if (dma_mapping_error(device->dev, unmap->addr[0]))
1335 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001336
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001337 unmap->to_cnt = 1;
1338
1339 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1340 buff_off, len, DMA_FROM_DEVICE);
1341 if (dma_mapping_error(device->dev, unmap->addr[1]))
1342 goto err_get_unmap;
1343
1344 unmap->from_cnt = 1;
1345
Dave Jiang8c874cc2016-01-08 09:51:05 -07001346 for (retries = 0; retries < DMA_RETRIES; retries++) {
Dave Jiang72203572016-07-20 13:14:13 -07001347 txd = device->device_prep_dma_memcpy(chan,
1348 unmap->addr[1],
Dave Jiang8c874cc2016-01-08 09:51:05 -07001349 unmap->addr[0], len,
1350 DMA_PREP_INTERRUPT);
1351 if (txd)
1352 break;
1353
1354 set_current_state(TASK_INTERRUPTIBLE);
1355 schedule_timeout(DMA_OUT_RESOURCE_TO);
1356 }
1357
1358 if (!txd) {
1359 qp->dma_rx_prep_err++;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001360 goto err_get_unmap;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001361 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001362
Dave Jiang72203572016-07-20 13:14:13 -07001363 txd->callback_result = ntb_rx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001364 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001365 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001366
1367 cookie = dmaengine_submit(txd);
1368 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001369 goto err_set_unmap;
1370
1371 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001372
1373 qp->last_cookie = cookie;
1374
1375 qp->rx_async++;
1376
Dave Jiang72203572016-07-20 13:14:13 -07001377 return 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001378
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001379err_set_unmap:
1380 dmaengine_unmap_put(unmap);
1381err_get_unmap:
1382 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001383err:
Dave Jiang72203572016-07-20 13:14:13 -07001384 return -ENXIO;
1385}
1386
1387static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
1388{
1389 struct ntb_transport_qp *qp = entry->qp;
1390 struct dma_chan *chan = qp->rx_dma_chan;
1391 int res;
1392
1393 if (!chan)
1394 goto err;
1395
1396 if (entry->len < copy_bytes)
1397 goto err;
1398
1399 res = ntb_async_rx_submit(entry, offset);
1400 if (res < 0)
1401 goto err;
1402
1403 if (!entry->retries)
1404 qp->rx_async++;
1405
1406 return;
1407
1408err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001409 ntb_memcpy_rx(entry, offset);
1410 qp->rx_memcpy++;
1411}
1412
Jon Masonfce8a7b2012-11-16 19:27:12 -07001413static int ntb_process_rxc(struct ntb_transport_qp *qp)
1414{
1415 struct ntb_payload_header *hdr;
1416 struct ntb_queue_entry *entry;
1417 void *offset;
1418
Jon Mason793c20e2013-01-19 02:02:26 -07001419 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1420 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1421
Allen Hubbee26a5842015-04-09 10:33:20 -04001422 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1423 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001424
Jon Masonfce8a7b2012-11-16 19:27:12 -07001425 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001426 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001427 qp->rx_ring_empty++;
1428 return -EAGAIN;
1429 }
1430
Allen Hubbee26a5842015-04-09 10:33:20 -04001431 if (hdr->flags & LINK_DOWN_FLAG) {
1432 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1433 ntb_qp_link_down(qp);
1434 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001435 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001436 }
1437
1438 if (hdr->ver != (u32)qp->rx_pkts) {
1439 dev_dbg(&qp->ndev->pdev->dev,
1440 "version mismatch, expected %llu - got %u\n",
1441 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001442 qp->rx_err_ver++;
1443 return -EIO;
1444 }
1445
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001446 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
Allen Hubbee26a5842015-04-09 10:33:20 -04001447 if (!entry) {
1448 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1449 qp->rx_err_no_buf++;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001450 return -EAGAIN;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001451 }
1452
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001453 entry->rx_hdr = hdr;
Dave Jiang72203572016-07-20 13:14:13 -07001454 entry->rx_index = qp->rx_index;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001455
Allen Hubbee26a5842015-04-09 10:33:20 -04001456 if (hdr->len > entry->len) {
1457 dev_dbg(&qp->ndev->pdev->dev,
1458 "receive buffer overflow! Wanted %d got %d\n",
1459 hdr->len, entry->len);
1460 qp->rx_err_oflow++;
1461
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001462 entry->len = -EIO;
1463 entry->flags |= DESC_DONE_FLAG;
1464
1465 ntb_complete_rxc(qp);
1466 } else {
1467 dev_dbg(&qp->ndev->pdev->dev,
1468 "RX OK index %u ver %u size %d into buf size %d\n",
1469 qp->rx_index, hdr->ver, hdr->len, entry->len);
1470
1471 qp->rx_bytes += hdr->len;
1472 qp->rx_pkts++;
1473
1474 entry->len = hdr->len;
1475
1476 ntb_async_rx(entry, offset);
Allen Hubbee26a5842015-04-09 10:33:20 -04001477 }
1478
Jon Mason282a2fe2013-02-12 09:52:50 -07001479 qp->rx_index++;
1480 qp->rx_index %= qp->rx_max_entry;
1481
1482 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001483}
1484
Allen Hubbee26a5842015-04-09 10:33:20 -04001485static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001486{
Allen Hubbee26a5842015-04-09 10:33:20 -04001487 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001488 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001489
Allen Hubbee26a5842015-04-09 10:33:20 -04001490 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1491 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001492
Jon Masonc336acd2013-01-17 15:28:45 -07001493 /* Limit the number of packets processed in a single interrupt to
1494 * provide fairness to others
1495 */
1496 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001497 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001498 if (rc)
1499 break;
1500 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001501
Dave Jiang569410ca2015-07-13 08:07:22 -04001502 if (i && qp->rx_dma_chan)
1503 dma_async_issue_pending(qp->rx_dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001504
Allen Hubbee26a5842015-04-09 10:33:20 -04001505 if (i == qp->rx_max_entry) {
1506 /* there is more work to do */
Dave Jiange9021332016-02-23 09:11:36 -07001507 if (qp->active)
1508 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001509 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1510 /* the doorbell bit is set: clear it */
1511 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1512 /* ntb_db_read ensures ntb_db_clear write is committed */
1513 ntb_db_read(qp->ndev);
1514
1515 /* an interrupt may have arrived between finishing
1516 * ntb_process_rxc and clearing the doorbell bit:
1517 * there might be some more work to do.
1518 */
Dave Jiange9021332016-02-23 09:11:36 -07001519 if (qp->active)
1520 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001521 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001522}
1523
Dave Jiang9cabc262016-07-20 13:14:07 -07001524static void ntb_tx_copy_callback(void *data,
1525 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001526{
Jon Mason282a2fe2013-02-12 09:52:50 -07001527 struct ntb_queue_entry *entry = data;
1528 struct ntb_transport_qp *qp = entry->qp;
1529 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001530
Dave Jiang9cabc262016-07-20 13:14:07 -07001531 /* we need to check DMA results if we are using DMA */
1532 if (res) {
1533 enum dmaengine_tx_result dma_err = res->result;
1534
1535 switch (dma_err) {
1536 case DMA_TRANS_READ_FAILED:
1537 case DMA_TRANS_WRITE_FAILED:
1538 entry->errors++;
1539 case DMA_TRANS_ABORTED:
1540 {
1541 void __iomem *offset =
1542 qp->tx_mw + qp->tx_max_frame *
1543 entry->tx_index;
1544
1545 /* resubmit via CPU */
1546 ntb_memcpy_tx(entry, offset);
1547 qp->tx_memcpy++;
1548 return;
1549 }
1550
1551 case DMA_TRANS_NOERROR:
1552 default:
1553 break;
1554 }
1555 }
1556
Jon Mason74465642013-01-21 15:28:52 -07001557 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001558
Allen Hubbee26a5842015-04-09 10:33:20 -04001559 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001560
1561 /* The entry length can only be zero if the packet is intended to be a
1562 * "link down" or similar. Since no payload is being sent in these
1563 * cases, there is nothing to add to the completion queue.
1564 */
1565 if (entry->len > 0) {
1566 qp->tx_bytes += entry->len;
1567
1568 if (qp->tx_handler)
1569 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1570 entry->len);
1571 }
1572
1573 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1574}
1575
Jon Mason282a2fe2013-02-12 09:52:50 -07001576static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1577{
Dave Jiang06917f72015-05-19 16:45:46 -04001578#ifdef ARCH_HAS_NOCACHE_UACCESS
1579 /*
1580 * Using non-temporal mov to improve performance on non-cached
1581 * writes, even though we aren't actually copying from user space.
1582 */
1583 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1584#else
Jon Mason282a2fe2013-02-12 09:52:50 -07001585 memcpy_toio(offset, entry->buf, entry->len);
Dave Jiang06917f72015-05-19 16:45:46 -04001586#endif
Jon Mason282a2fe2013-02-12 09:52:50 -07001587
Allen Hubbee26a5842015-04-09 10:33:20 -04001588 /* Ensure that the data is fully copied out before setting the flags */
1589 wmb();
1590
Dave Jiang9cabc262016-07-20 13:14:07 -07001591 ntb_tx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001592}
1593
Dave Jiang9cabc262016-07-20 13:14:07 -07001594static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1595 struct ntb_queue_entry *entry)
Jon Mason282a2fe2013-02-12 09:52:50 -07001596{
Jon Mason282a2fe2013-02-12 09:52:50 -07001597 struct dma_async_tx_descriptor *txd;
Dave Jiang569410ca2015-07-13 08:07:22 -04001598 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001599 struct dma_device *device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001600 size_t len = entry->len;
1601 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001602 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001603 struct dmaengine_unmap_data *unmap;
1604 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001605 dma_cookie_t cookie;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001606 int retries = 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001607
Jon Mason282a2fe2013-02-12 09:52:50 -07001608 device = chan->device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001609 dest = qp->tx_mw_phys + qp->tx_max_frame * entry->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001610 buff_off = (size_t)buf & ~PAGE_MASK;
1611 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001612
1613 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1614 goto err;
1615
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001616 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1617 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001618 goto err;
1619
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001620 unmap->len = len;
1621 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1622 buff_off, len, DMA_TO_DEVICE);
1623 if (dma_mapping_error(device->dev, unmap->addr[0]))
1624 goto err_get_unmap;
1625
1626 unmap->to_cnt = 1;
1627
Dave Jiang8c874cc2016-01-08 09:51:05 -07001628 for (retries = 0; retries < DMA_RETRIES; retries++) {
Dave Jiang9cabc262016-07-20 13:14:07 -07001629 txd = device->device_prep_dma_memcpy(chan, dest,
1630 unmap->addr[0], len,
1631 DMA_PREP_INTERRUPT);
Dave Jiang8c874cc2016-01-08 09:51:05 -07001632 if (txd)
1633 break;
1634
1635 set_current_state(TASK_INTERRUPTIBLE);
1636 schedule_timeout(DMA_OUT_RESOURCE_TO);
1637 }
1638
1639 if (!txd) {
1640 qp->dma_tx_prep_err++;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001641 goto err_get_unmap;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001642 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001643
Dave Jiang9cabc262016-07-20 13:14:07 -07001644 txd->callback_result = ntb_tx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001645 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001646 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001647
1648 cookie = dmaengine_submit(txd);
1649 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001650 goto err_set_unmap;
1651
1652 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001653
1654 dma_async_issue_pending(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001655
Dave Jiang9cabc262016-07-20 13:14:07 -07001656 return 0;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001657err_set_unmap:
1658 dmaengine_unmap_put(unmap);
1659err_get_unmap:
1660 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001661err:
Dave Jiang9cabc262016-07-20 13:14:07 -07001662 return -ENXIO;
1663}
1664
1665static void ntb_async_tx(struct ntb_transport_qp *qp,
1666 struct ntb_queue_entry *entry)
1667{
1668 struct ntb_payload_header __iomem *hdr;
1669 struct dma_chan *chan = qp->tx_dma_chan;
1670 void __iomem *offset;
1671 int res;
1672
1673 entry->tx_index = qp->tx_index;
1674 offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
1675 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1676 entry->tx_hdr = hdr;
1677
1678 iowrite32(entry->len, &hdr->len);
1679 iowrite32((u32)qp->tx_pkts, &hdr->ver);
1680
1681 if (!chan)
1682 goto err;
1683
1684 if (entry->len < copy_bytes)
1685 goto err;
1686
1687 res = ntb_async_tx_submit(qp, entry);
1688 if (res < 0)
1689 goto err;
1690
1691 if (!entry->retries)
1692 qp->tx_async++;
1693
1694 return;
1695
1696err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001697 ntb_memcpy_tx(entry, offset);
1698 qp->tx_memcpy++;
1699}
1700
Jon Masonfce8a7b2012-11-16 19:27:12 -07001701static int ntb_process_tx(struct ntb_transport_qp *qp,
1702 struct ntb_queue_entry *entry)
1703{
Jon Mason793c20e2013-01-19 02:02:26 -07001704 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001705 qp->tx_ring_full++;
1706 return -EAGAIN;
1707 }
1708
Jon Masonef114ed2013-01-19 02:02:18 -07001709 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001710 if (qp->tx_handler)
Jon Mason179f9122015-12-18 13:22:37 -05001711 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001712
1713 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1714 &qp->tx_free_q);
1715 return 0;
1716 }
1717
Jon Mason282a2fe2013-02-12 09:52:50 -07001718 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001719
Jon Mason793c20e2013-01-19 02:02:26 -07001720 qp->tx_index++;
1721 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001722
1723 qp->tx_pkts++;
1724
1725 return 0;
1726}
1727
1728static void ntb_send_link_down(struct ntb_transport_qp *qp)
1729{
Allen Hubbee26a5842015-04-09 10:33:20 -04001730 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001731 struct ntb_queue_entry *entry;
1732 int i, rc;
1733
Allen Hubbee26a5842015-04-09 10:33:20 -04001734 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001735 return;
1736
Allen Hubbee22e0b92015-05-12 06:55:44 -04001737 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001738
1739 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001740 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001741 if (entry)
1742 break;
1743 msleep(100);
1744 }
1745
1746 if (!entry)
1747 return;
1748
1749 entry->cb_data = NULL;
1750 entry->buf = NULL;
1751 entry->len = 0;
1752 entry->flags = LINK_DOWN_FLAG;
1753
1754 rc = ntb_process_tx(qp, entry);
1755 if (rc)
1756 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1757 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001758
1759 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001760}
1761
Allen Hubbe1199aa62015-05-18 06:20:47 -04001762static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1763{
1764 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1765}
1766
Jon Masonfce8a7b2012-11-16 19:27:12 -07001767/**
1768 * ntb_transport_create_queue - Create a new NTB transport layer queue
1769 * @rx_handler: receive callback function
1770 * @tx_handler: transmit callback function
1771 * @event_handler: event callback function
1772 *
1773 * Create a new NTB transport layer queue and provide the queue with a callback
1774 * routine for both transmit and receive. The receive callback routine will be
1775 * used to pass up data when the transport has received it on the queue. The
1776 * transmit callback routine will be called when the transport has completed the
1777 * transmission of the data on the queue and the data is ready to be freed.
1778 *
1779 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1780 */
1781struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001782ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001783 const struct ntb_queue_handlers *handlers)
1784{
Allen Hubbee26a5842015-04-09 10:33:20 -04001785 struct ntb_dev *ndev;
1786 struct pci_dev *pdev;
1787 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001788 struct ntb_queue_entry *entry;
1789 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001790 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001791 unsigned int free_queue;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001792 dma_cap_mask_t dma_mask;
1793 int node;
Allen Hubbee26a5842015-04-09 10:33:20 -04001794 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001795
Allen Hubbee26a5842015-04-09 10:33:20 -04001796 ndev = dev_ntb(client_dev->parent);
1797 pdev = ndev->pdev;
1798 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001799
Allen Hubbe1199aa62015-05-18 06:20:47 -04001800 node = dev_to_node(&ndev->dev);
1801
Thomas VanSelus68214ad2017-02-13 16:46:26 -06001802 free_queue = ffs(nt->qp_bitmap_free);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001803 if (!free_queue)
1804 goto err;
1805
1806 /* decrement free_queue to make it zero based */
1807 free_queue--;
1808
Allen Hubbee26a5842015-04-09 10:33:20 -04001809 qp = &nt->qp_vec[free_queue];
1810 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001811
Allen Hubbee26a5842015-04-09 10:33:20 -04001812 nt->qp_bitmap_free &= ~qp_bit;
1813
Jon Masonfce8a7b2012-11-16 19:27:12 -07001814 qp->cb_data = data;
1815 qp->rx_handler = handlers->rx_handler;
1816 qp->tx_handler = handlers->tx_handler;
1817 qp->event_handler = handlers->event_handler;
1818
Allen Hubbe1199aa62015-05-18 06:20:47 -04001819 dma_cap_zero(dma_mask);
1820 dma_cap_set(DMA_MEMCPY, dma_mask);
1821
Dave Jianga41ef052015-05-19 16:52:04 -04001822 if (use_dma) {
Dave Jiang569410ca2015-07-13 08:07:22 -04001823 qp->tx_dma_chan =
1824 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1825 (void *)(unsigned long)node);
1826 if (!qp->tx_dma_chan)
1827 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
1828
1829 qp->rx_dma_chan =
1830 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1831 (void *)(unsigned long)node);
1832 if (!qp->rx_dma_chan)
1833 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
Dave Jianga41ef052015-05-19 16:52:04 -04001834 } else {
Dave Jiang569410ca2015-07-13 08:07:22 -04001835 qp->tx_dma_chan = NULL;
1836 qp->rx_dma_chan = NULL;
Dave Jianga41ef052015-05-19 16:52:04 -04001837 }
Dave Jiang569410ca2015-07-13 08:07:22 -04001838
1839 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
1840 qp->tx_dma_chan ? "DMA" : "CPU");
1841
1842 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
1843 qp->rx_dma_chan ? "DMA" : "CPU");
Jon Mason282a2fe2013-02-12 09:52:50 -07001844
Jon Masonfce8a7b2012-11-16 19:27:12 -07001845 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001846 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001847 if (!entry)
1848 goto err1;
1849
Jon Mason282a2fe2013-02-12 09:52:50 -07001850 entry->qp = qp;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001851 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001852 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001853 }
Dave Jianga754a8f2016-04-08 10:49:06 -07001854 qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001855
Dave Jianga754a8f2016-04-08 10:49:06 -07001856 for (i = 0; i < qp->tx_max_entry; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001857 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001858 if (!entry)
1859 goto err2;
1860
Jon Mason282a2fe2013-02-12 09:52:50 -07001861 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001862 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001863 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001864 }
1865
Allen Hubbee26a5842015-04-09 10:33:20 -04001866 ntb_db_clear(qp->ndev, qp_bit);
1867 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001868
1869 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1870
1871 return qp;
1872
Jon Masonfce8a7b2012-11-16 19:27:12 -07001873err2:
Jon Masonf7667552013-01-19 02:02:24 -07001874 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001875 kfree(entry);
1876err1:
Dave Jianga754a8f2016-04-08 10:49:06 -07001877 qp->rx_alloc_entry = 0;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001878 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001879 kfree(entry);
Dave Jiang569410ca2015-07-13 08:07:22 -04001880 if (qp->tx_dma_chan)
1881 dma_release_channel(qp->tx_dma_chan);
1882 if (qp->rx_dma_chan)
1883 dma_release_channel(qp->rx_dma_chan);
Allen Hubbee26a5842015-04-09 10:33:20 -04001884 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001885err:
1886 return NULL;
1887}
1888EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1889
1890/**
1891 * ntb_transport_free_queue - Frees NTB transport queue
1892 * @qp: NTB queue to be freed
1893 *
1894 * Frees NTB transport queue
1895 */
1896void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1897{
Jon Mason186f27f2013-01-22 11:35:40 -07001898 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001899 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001900 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001901
1902 if (!qp)
1903 return;
1904
Allen Hubbee26a5842015-04-09 10:33:20 -04001905 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001906
Dave Jiange9021332016-02-23 09:11:36 -07001907 qp->active = false;
1908
Dave Jiang569410ca2015-07-13 08:07:22 -04001909 if (qp->tx_dma_chan) {
1910 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001911 /* Putting the dma_chan to NULL will force any new traffic to be
1912 * processed by the CPU instead of the DAM engine
1913 */
Dave Jiang569410ca2015-07-13 08:07:22 -04001914 qp->tx_dma_chan = NULL;
1915
1916 /* Try to be nice and wait for any queued DMA engine
1917 * transactions to process before smashing it with a rock
1918 */
1919 dma_sync_wait(chan, qp->last_cookie);
1920 dmaengine_terminate_all(chan);
1921 dma_release_channel(chan);
1922 }
1923
1924 if (qp->rx_dma_chan) {
1925 struct dma_chan *chan = qp->rx_dma_chan;
1926 /* Putting the dma_chan to NULL will force any new traffic to be
1927 * processed by the CPU instead of the DAM engine
1928 */
1929 qp->rx_dma_chan = NULL;
Jon Mason282a2fe2013-02-12 09:52:50 -07001930
1931 /* Try to be nice and wait for any queued DMA engine
1932 * transactions to process before smashing it with a rock
1933 */
1934 dma_sync_wait(chan, qp->last_cookie);
1935 dmaengine_terminate_all(chan);
Allen Hubbe1199aa62015-05-18 06:20:47 -04001936 dma_release_channel(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001937 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001938
Allen Hubbee26a5842015-04-09 10:33:20 -04001939 qp_bit = BIT_ULL(qp->qp_num);
1940
1941 ntb_db_set_mask(qp->ndev, qp_bit);
Dave Jiange9021332016-02-23 09:11:36 -07001942 tasklet_kill(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001943
Jon Mason282a2fe2013-02-12 09:52:50 -07001944 cancel_delayed_work_sync(&qp->link_work);
1945
Allen Hubbee26a5842015-04-09 10:33:20 -04001946 qp->cb_data = NULL;
1947 qp->rx_handler = NULL;
1948 qp->tx_handler = NULL;
1949 qp->event_handler = NULL;
1950
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001951 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001952 kfree(entry);
1953
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001954 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1955 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1956 kfree(entry);
1957 }
1958
1959 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1960 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001961 kfree(entry);
1962 }
1963
Jon Masonf7667552013-01-19 02:02:24 -07001964 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001965 kfree(entry);
1966
Allen Hubbe30a4bb12015-07-13 08:07:14 -04001967 qp->transport->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001968
1969 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1970}
1971EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1972
1973/**
1974 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1975 * @qp: NTB queue to be freed
1976 * @len: pointer to variable to write enqueued buffers length
1977 *
1978 * Dequeues unused buffers from receive queue. Should only be used during
1979 * shutdown of qp.
1980 *
1981 * RETURNS: NULL error value on error, or void* for success.
1982 */
1983void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1984{
1985 struct ntb_queue_entry *entry;
1986 void *buf;
1987
Allen Hubbee26a5842015-04-09 10:33:20 -04001988 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001989 return NULL;
1990
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001991 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001992 if (!entry)
1993 return NULL;
1994
1995 buf = entry->cb_data;
1996 *len = entry->len;
1997
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001998 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001999
2000 return buf;
2001}
2002EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
2003
2004/**
2005 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
2006 * @qp: NTB transport layer queue the entry is to be enqueued on
2007 * @cb: per buffer pointer for callback function to use
2008 * @data: pointer to data buffer that incoming packets will be copied into
2009 * @len: length of the data buffer
2010 *
2011 * Enqueue a new receive buffer onto the transport queue into which a NTB
2012 * payload can be received into.
2013 *
2014 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2015 */
2016int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2017 unsigned int len)
2018{
2019 struct ntb_queue_entry *entry;
2020
2021 if (!qp)
2022 return -EINVAL;
2023
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002024 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002025 if (!entry)
2026 return -ENOMEM;
2027
2028 entry->cb_data = cb;
2029 entry->buf = data;
2030 entry->len = len;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002031 entry->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07002032 entry->retries = 0;
2033 entry->errors = 0;
2034 entry->rx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002035
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002036 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
2037
Dave Jiange9021332016-02-23 09:11:36 -07002038 if (qp->active)
2039 tasklet_schedule(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002040
2041 return 0;
2042}
2043EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2044
2045/**
2046 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2047 * @qp: NTB transport layer queue the entry is to be enqueued on
2048 * @cb: per buffer pointer for callback function to use
2049 * @data: pointer to data buffer that will be sent
2050 * @len: length of the data buffer
2051 *
2052 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07002053 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07002054 * serialize access to the qp.
2055 *
2056 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2057 */
2058int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2059 unsigned int len)
2060{
2061 struct ntb_queue_entry *entry;
2062 int rc;
2063
Allen Hubbee26a5842015-04-09 10:33:20 -04002064 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002065 return -EINVAL;
2066
2067 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07002068 if (!entry) {
2069 qp->tx_err_no_buf++;
Dave Jiange74bfee2015-07-13 08:07:17 -04002070 return -EBUSY;
Jon Mason282a2fe2013-02-12 09:52:50 -07002071 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07002072
2073 entry->cb_data = cb;
2074 entry->buf = data;
2075 entry->len = len;
2076 entry->flags = 0;
Dave Jiang9cabc262016-07-20 13:14:07 -07002077 entry->errors = 0;
2078 entry->retries = 0;
2079 entry->tx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002080
2081 rc = ntb_process_tx(qp, entry);
2082 if (rc)
2083 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2084 &qp->tx_free_q);
2085
2086 return rc;
2087}
2088EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2089
2090/**
2091 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2092 * @qp: NTB transport layer queue to be enabled
2093 *
2094 * Notify NTB transport layer of client readiness to use queue
2095 */
2096void ntb_transport_link_up(struct ntb_transport_qp *qp)
2097{
2098 if (!qp)
2099 return;
2100
Allen Hubbee26a5842015-04-09 10:33:20 -04002101 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002102
Allen Hubbee26a5842015-04-09 10:33:20 -04002103 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002104 schedule_delayed_work(&qp->link_work, 0);
2105}
2106EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2107
2108/**
2109 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2110 * @qp: NTB transport layer queue to be disabled
2111 *
2112 * Notify NTB transport layer of client's desire to no longer receive data on
2113 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07002114 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07002115 */
2116void ntb_transport_link_down(struct ntb_transport_qp *qp)
2117{
Allen Hubbee26a5842015-04-09 10:33:20 -04002118 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002119
2120 if (!qp)
2121 return;
2122
Allen Hubbee26a5842015-04-09 10:33:20 -04002123 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002124
Allen Hubbee26a5842015-04-09 10:33:20 -04002125 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002126
Allen Hubbee26a5842015-04-09 10:33:20 -04002127 ntb_peer_spad_write(qp->ndev, QP_LINKS,
2128 val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07002129
Allen Hubbee26a5842015-04-09 10:33:20 -04002130 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002131 ntb_send_link_down(qp);
2132 else
2133 cancel_delayed_work_sync(&qp->link_work);
2134}
2135EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2136
2137/**
2138 * ntb_transport_link_query - Query transport link state
2139 * @qp: NTB transport layer queue to be queried
2140 *
2141 * Query connectivity to the remote system of the NTB transport queue
2142 *
2143 * RETURNS: true for link up or false for link down
2144 */
2145bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2146{
Jon Mason186f27f2013-01-22 11:35:40 -07002147 if (!qp)
2148 return false;
2149
Allen Hubbee26a5842015-04-09 10:33:20 -04002150 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002151}
2152EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2153
2154/**
2155 * ntb_transport_qp_num - Query the qp number
2156 * @qp: NTB transport layer queue to be queried
2157 *
2158 * Query qp number of the NTB transport queue
2159 *
2160 * RETURNS: a zero based number specifying the qp number
2161 */
2162unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2163{
Jon Mason186f27f2013-01-22 11:35:40 -07002164 if (!qp)
2165 return 0;
2166
Jon Masonfce8a7b2012-11-16 19:27:12 -07002167 return qp->qp_num;
2168}
2169EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2170
2171/**
2172 * ntb_transport_max_size - Query the max payload size of a qp
2173 * @qp: NTB transport layer queue to be queried
2174 *
2175 * Query the maximum payload size permissible on the given qp
2176 *
2177 * RETURNS: the max payload size of a qp
2178 */
Jon Masonef114ed2013-01-19 02:02:18 -07002179unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002180{
Dave Jiang04afde42015-09-17 13:27:04 -07002181 unsigned int max_size;
Dave Jiang569410ca2015-07-13 08:07:22 -04002182 unsigned int copy_align;
Dave Jiang04afde42015-09-17 13:27:04 -07002183 struct dma_chan *rx_chan, *tx_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002184
Jon Mason186f27f2013-01-22 11:35:40 -07002185 if (!qp)
2186 return 0;
2187
Dave Jiang04afde42015-09-17 13:27:04 -07002188 rx_chan = qp->rx_dma_chan;
2189 tx_chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002190
Dave Jiang04afde42015-09-17 13:27:04 -07002191 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2192 tx_chan ? tx_chan->device->copy_align : 0);
Dave Jiang569410ca2015-07-13 08:07:22 -04002193
Jon Mason282a2fe2013-02-12 09:52:50 -07002194 /* If DMA engine usage is possible, try to find the max size for that */
Dave Jiang04afde42015-09-17 13:27:04 -07002195 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2196 max_size = round_down(max_size, 1 << copy_align);
Jon Mason282a2fe2013-02-12 09:52:50 -07002197
Dave Jiang04afde42015-09-17 13:27:04 -07002198 return max_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002199}
2200EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04002201
Dave Jiange74bfee2015-07-13 08:07:17 -04002202unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2203{
2204 unsigned int head = qp->tx_index;
2205 unsigned int tail = qp->remote_rx_info->entry;
2206
2207 return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2208}
2209EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2210
Allen Hubbee26a5842015-04-09 10:33:20 -04002211static void ntb_transport_doorbell_callback(void *data, int vector)
2212{
2213 struct ntb_transport_ctx *nt = data;
2214 struct ntb_transport_qp *qp;
2215 u64 db_bits;
2216 unsigned int qp_num;
2217
2218 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2219 ntb_db_vector_mask(nt->ndev, vector));
2220
2221 while (db_bits) {
2222 qp_num = __ffs(db_bits);
2223 qp = &nt->qp_vec[qp_num];
2224
Dave Jiange9021332016-02-23 09:11:36 -07002225 if (qp->active)
2226 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04002227
2228 db_bits &= ~BIT_ULL(qp_num);
2229 }
2230}
2231
2232static const struct ntb_ctx_ops ntb_transport_ops = {
2233 .link_event = ntb_transport_event_callback,
2234 .db_event = ntb_transport_doorbell_callback,
2235};
2236
2237static struct ntb_client ntb_transport_client = {
2238 .ops = {
2239 .probe = ntb_transport_probe,
2240 .remove = ntb_transport_free,
2241 },
2242};
2243
2244static int __init ntb_transport_init(void)
2245{
2246 int rc;
2247
Dave Jiang7eb38782015-06-15 08:21:33 -04002248 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2249
Allen Hubbee26a5842015-04-09 10:33:20 -04002250 if (debugfs_initialized())
2251 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2252
2253 rc = bus_register(&ntb_transport_bus);
2254 if (rc)
2255 goto err_bus;
2256
2257 rc = ntb_register_client(&ntb_transport_client);
2258 if (rc)
2259 goto err_client;
2260
2261 return 0;
2262
2263err_client:
2264 bus_unregister(&ntb_transport_bus);
2265err_bus:
2266 debugfs_remove_recursive(nt_debugfs_dir);
2267 return rc;
2268}
2269module_init(ntb_transport_init);
2270
2271static void __exit ntb_transport_exit(void)
2272{
Allen Hubbee26a5842015-04-09 10:33:20 -04002273 ntb_unregister_client(&ntb_transport_client);
2274 bus_unregister(&ntb_transport_bus);
Allen Hubbe43325382016-12-27 17:57:04 -05002275 debugfs_remove_recursive(nt_debugfs_dir);
Allen Hubbee26a5842015-04-09 10:33:20 -04002276}
2277module_exit(ntb_transport_exit);