blob: f58d8e3053236ad4608e5552052214b0694a3a95 [file] [log] [blame]
Jon Masonfce8a7b2012-11-16 19:27:12 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -04008 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * BSD LICENSE
15 *
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -040017 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -070018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
28 * distribution.
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
Allen Hubbee26a5842015-04-09 10:33:20 -040045 * PCIe NTB Transport Linux driver
Jon Masonfce8a7b2012-11-16 19:27:12 -070046 *
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
49 */
50#include <linux/debugfs.h>
51#include <linux/delay.h>
Jon Mason282a2fe2013-02-12 09:52:50 -070052#include <linux/dmaengine.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070053#include <linux/dma-mapping.h>
54#include <linux/errno.h>
55#include <linux/export.h>
56#include <linux/interrupt.h>
57#include <linux/module.h>
58#include <linux/pci.h>
59#include <linux/slab.h>
60#include <linux/types.h>
Dave Jiang06917f72015-05-19 16:45:46 -040061#include <linux/uaccess.h>
Allen Hubbee26a5842015-04-09 10:33:20 -040062#include "linux/ntb.h"
63#include "linux/ntb_transport.h"
Jon Masonfce8a7b2012-11-16 19:27:12 -070064
Allen Hubbee26a5842015-04-09 10:33:20 -040065#define NTB_TRANSPORT_VERSION 4
66#define NTB_TRANSPORT_VER "4"
67#define NTB_TRANSPORT_NAME "ntb_transport"
68#define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +053069#define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
Allen Hubbee26a5842015-04-09 10:33:20 -040070
71MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
72MODULE_VERSION(NTB_TRANSPORT_VER);
73MODULE_LICENSE("Dual BSD/GPL");
74MODULE_AUTHOR("Intel Corporation");
75
76static unsigned long max_mw_size;
77module_param(max_mw_size, ulong, 0644);
78MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
Jon Masonfce8a7b2012-11-16 19:27:12 -070079
Dave Jiang98914172015-06-03 11:29:38 -040080static unsigned int transport_mtu = 0x10000;
Jon Masonfce8a7b2012-11-16 19:27:12 -070081module_param(transport_mtu, uint, 0644);
82MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
83
Jon Mason948d3a62013-04-18 17:07:36 -070084static unsigned char max_num_clients;
Jon Masonfce8a7b2012-11-16 19:27:12 -070085module_param(max_num_clients, byte, 0644);
86MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
87
Jon Mason282a2fe2013-02-12 09:52:50 -070088static unsigned int copy_bytes = 1024;
89module_param(copy_bytes, uint, 0644);
90MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
91
Dave Jianga41ef052015-05-19 16:52:04 -040092static bool use_dma;
93module_param(use_dma, bool, 0644);
94MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
95
Allen Hubbee26a5842015-04-09 10:33:20 -040096static struct dentry *nt_debugfs_dir;
97
Serge Semin1e530112016-12-14 02:49:14 +030098/* Only two-ports NTB devices are supported */
99#define PIDX NTB_DEF_PEER_IDX
100
Jon Masonfce8a7b2012-11-16 19:27:12 -0700101struct ntb_queue_entry {
102 /* ntb_queue list reference */
103 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400104 /* pointers to data to be transferred */
Jon Masonfce8a7b2012-11-16 19:27:12 -0700105 void *cb_data;
106 void *buf;
107 unsigned int len;
108 unsigned int flags;
Dave Jiang9cabc262016-07-20 13:14:07 -0700109 int retries;
110 int errors;
111 unsigned int tx_index;
Dave Jiang72203572016-07-20 13:14:13 -0700112 unsigned int rx_index;
Jon Mason282a2fe2013-02-12 09:52:50 -0700113
114 struct ntb_transport_qp *qp;
115 union {
116 struct ntb_payload_header __iomem *tx_hdr;
117 struct ntb_payload_header *rx_hdr;
118 };
Jon Masonfce8a7b2012-11-16 19:27:12 -0700119};
120
Jon Mason793c20e2013-01-19 02:02:26 -0700121struct ntb_rx_info {
122 unsigned int entry;
123};
124
Jon Masonfce8a7b2012-11-16 19:27:12 -0700125struct ntb_transport_qp {
Allen Hubbee26a5842015-04-09 10:33:20 -0400126 struct ntb_transport_ctx *transport;
127 struct ntb_dev *ndev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700128 void *cb_data;
Dave Jiang569410ca2015-07-13 08:07:22 -0400129 struct dma_chan *tx_dma_chan;
130 struct dma_chan *rx_dma_chan;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700131
132 bool client_ready;
Allen Hubbee26a5842015-04-09 10:33:20 -0400133 bool link_is_up;
Dave Jiange9021332016-02-23 09:11:36 -0700134 bool active;
Allen Hubbee26a5842015-04-09 10:33:20 -0400135
Jon Masonfce8a7b2012-11-16 19:27:12 -0700136 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400137 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700138
Jon Mason74465642013-01-21 15:28:52 -0700139 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -0700140 struct ntb_rx_info *remote_rx_info;
141
Jon Mason53ca4fe2013-11-26 11:21:50 -0700142 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
143 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700144 struct list_head tx_free_q;
145 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700146 void __iomem *tx_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700147 dma_addr_t tx_mw_phys;
Jon Mason793c20e2013-01-19 02:02:26 -0700148 unsigned int tx_index;
149 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700150 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700151
Jon Mason53ca4fe2013-11-26 11:21:50 -0700152 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
153 void *data, int len);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400154 struct list_head rx_post_q;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700155 struct list_head rx_pend_q;
156 struct list_head rx_free_q;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400157 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
158 spinlock_t ntb_rx_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700159 void *rx_buff;
160 unsigned int rx_index;
161 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700162 unsigned int rx_max_frame;
Dave Jianga754a8f2016-04-08 10:49:06 -0700163 unsigned int rx_alloc_entry;
Jon Mason282a2fe2013-02-12 09:52:50 -0700164 dma_cookie_t last_cookie;
Allen Hubbee26a5842015-04-09 10:33:20 -0400165 struct tasklet_struct rxc_db_work;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700166
Jon Mason53ca4fe2013-11-26 11:21:50 -0700167 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700168 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700169 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700170
171 struct dentry *debugfs_dir;
172 struct dentry *debugfs_stats;
173
174 /* Stats */
175 u64 rx_bytes;
176 u64 rx_pkts;
177 u64 rx_ring_empty;
178 u64 rx_err_no_buf;
179 u64 rx_err_oflow;
180 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700181 u64 rx_memcpy;
182 u64 rx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700183 u64 tx_bytes;
184 u64 tx_pkts;
185 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700186 u64 tx_err_no_buf;
187 u64 tx_memcpy;
188 u64 tx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700189};
190
191struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400192 phys_addr_t phys_addr;
193 resource_size_t phys_size;
194 resource_size_t xlat_align;
195 resource_size_t xlat_align_size;
196 void __iomem *vbase;
197 size_t xlat_size;
198 size_t buff_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700199 void *virt_addr;
200 dma_addr_t dma_addr;
201};
202
203struct ntb_transport_client_dev {
204 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400205 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700206 struct device dev;
207};
208
Allen Hubbee26a5842015-04-09 10:33:20 -0400209struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700210 struct list_head entry;
211 struct list_head client_devs;
212
Allen Hubbee26a5842015-04-09 10:33:20 -0400213 struct ntb_dev *ndev;
214
215 struct ntb_transport_mw *mw_vec;
216 struct ntb_transport_qp *qp_vec;
217 unsigned int mw_count;
218 unsigned int qp_count;
219 u64 qp_bitmap;
220 u64 qp_bitmap_free;
221
222 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700223 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700224 struct work_struct link_cleanup;
Dave Jiangc8650fd2015-07-13 08:07:09 -0400225
226 struct dentry *debugfs_node_dir;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700227};
228
229enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400230 DESC_DONE_FLAG = BIT(0),
231 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700232};
233
234struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700235 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700236 unsigned int len;
237 unsigned int flags;
238};
239
240enum {
241 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700242 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700243 NUM_QPS,
244 NUM_MWS,
245 MW0_SZ_HIGH,
246 MW0_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700247};
248
Allen Hubbee26a5842015-04-09 10:33:20 -0400249#define dev_client_dev(__dev) \
250 container_of((__dev), struct ntb_transport_client_dev, dev)
251
252#define drv_client(__drv) \
253 container_of((__drv), struct ntb_transport_client, driver)
254
255#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700256#define NTB_QP_DEF_NUM_ENTRIES 100
257#define NTB_LINK_DOWN_TIMEOUT 10
258
Allen Hubbee26a5842015-04-09 10:33:20 -0400259static void ntb_transport_rxc_db(unsigned long data);
260static const struct ntb_ctx_ops ntb_transport_ops;
261static struct ntb_client ntb_transport_client;
Dave Jiang9cabc262016-07-20 13:14:07 -0700262static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
263 struct ntb_queue_entry *entry);
264static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
Dave Jiang72203572016-07-20 13:14:13 -0700265static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
266static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
267
Allen Hubbee26a5842015-04-09 10:33:20 -0400268
269static int ntb_transport_bus_match(struct device *dev,
270 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700271{
272 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
273}
274
Allen Hubbee26a5842015-04-09 10:33:20 -0400275static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700276{
Allen Hubbee26a5842015-04-09 10:33:20 -0400277 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700278 int rc = -EINVAL;
279
280 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400281
282 client = drv_client(dev->driver);
283 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700284 if (rc)
285 put_device(dev);
286
287 return rc;
288}
289
Allen Hubbee26a5842015-04-09 10:33:20 -0400290static int ntb_transport_bus_remove(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700291{
Allen Hubbee26a5842015-04-09 10:33:20 -0400292 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700293
Allen Hubbee26a5842015-04-09 10:33:20 -0400294 client = drv_client(dev->driver);
295 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700296
297 put_device(dev);
298
299 return 0;
300}
301
Allen Hubbee26a5842015-04-09 10:33:20 -0400302static struct bus_type ntb_transport_bus = {
303 .name = "ntb_transport",
304 .match = ntb_transport_bus_match,
305 .probe = ntb_transport_bus_probe,
306 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700307};
308
309static LIST_HEAD(ntb_transport_list);
310
Allen Hubbee26a5842015-04-09 10:33:20 -0400311static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700312{
Dave Jiang31510002015-07-13 08:07:19 -0400313 list_add_tail(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700314 return 0;
315}
316
Allen Hubbee26a5842015-04-09 10:33:20 -0400317static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700318{
319 struct ntb_transport_client_dev *client_dev, *cd;
320
321 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
322 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
323 dev_name(&client_dev->dev));
324 list_del(&client_dev->entry);
325 device_unregister(&client_dev->dev);
326 }
327
328 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700329}
330
Allen Hubbee26a5842015-04-09 10:33:20 -0400331static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700332{
333 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700334
Allen Hubbee26a5842015-04-09 10:33:20 -0400335 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700336 kfree(client_dev);
337}
338
339/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400340 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700341 * @device_name: Name of NTB client device
342 *
343 * Unregister an NTB client device with the NTB transport layer
344 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400345void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700346{
347 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400348 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700349
350 list_for_each_entry(nt, &ntb_transport_list, entry)
351 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
352 if (!strncmp(dev_name(&client->dev), device_name,
353 strlen(device_name))) {
354 list_del(&client->entry);
355 device_unregister(&client->dev);
356 }
357}
Allen Hubbee26a5842015-04-09 10:33:20 -0400358EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700359
360/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400361 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700362 * @device_name: Name of NTB client device
363 *
364 * Register an NTB client device with the NTB transport layer
365 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400366int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700367{
368 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400369 struct ntb_transport_ctx *nt;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400370 int node;
Jon Mason8b19d452013-04-26 14:51:57 -0700371 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700372
Jon Mason8222b402013-01-19 02:02:16 -0700373 if (list_empty(&ntb_transport_list))
374 return -ENODEV;
375
Jon Masonfce8a7b2012-11-16 19:27:12 -0700376 list_for_each_entry(nt, &ntb_transport_list, entry) {
377 struct device *dev;
378
Allen Hubbe1199aa62015-05-18 06:20:47 -0400379 node = dev_to_node(&nt->ndev->dev);
380
381 client_dev = kzalloc_node(sizeof(*client_dev),
382 GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700383 if (!client_dev) {
384 rc = -ENOMEM;
385 goto err;
386 }
387
388 dev = &client_dev->dev;
389
390 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700391 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400392 dev->bus = &ntb_transport_bus;
393 dev->release = ntb_transport_client_release;
394 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700395
396 rc = device_register(dev);
397 if (rc) {
398 kfree(client_dev);
399 goto err;
400 }
401
402 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700403 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700404 }
405
406 return 0;
407
408err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400409 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700410
411 return rc;
412}
Allen Hubbee26a5842015-04-09 10:33:20 -0400413EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700414
415/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400416 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700417 * @drv: NTB client driver to be registered
418 *
419 * Register an NTB client driver with the NTB transport layer
420 *
421 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
422 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400423int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700424{
Allen Hubbee26a5842015-04-09 10:33:20 -0400425 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700426
Jon Mason8222b402013-01-19 02:02:16 -0700427 if (list_empty(&ntb_transport_list))
428 return -ENODEV;
429
Jon Masonfce8a7b2012-11-16 19:27:12 -0700430 return driver_register(&drv->driver);
431}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400432EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700433
434/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400435 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700436 * @drv: NTB client driver to be unregistered
437 *
438 * Unregister an NTB client driver with the NTB transport layer
439 *
440 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
441 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400442void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700443{
444 driver_unregister(&drv->driver);
445}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400446EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700447
Jon Masonfce8a7b2012-11-16 19:27:12 -0700448static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
449 loff_t *offp)
450{
451 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700452 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700453 ssize_t ret, out_offset, out_count;
454
Dave Jiang260bee92015-07-13 08:07:11 -0400455 qp = filp->private_data;
456
457 if (!qp || !qp->link_is_up)
458 return 0;
459
Jon Mason282a2fe2013-02-12 09:52:50 -0700460 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700461
462 buf = kmalloc(out_count, GFP_KERNEL);
463 if (!buf)
464 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700465
Jon Masonfce8a7b2012-11-16 19:27:12 -0700466 out_offset = 0;
467 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400468 "\nNTB QP stats:\n\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700469 out_offset += snprintf(buf + out_offset, out_count - out_offset,
470 "rx_bytes - \t%llu\n", qp->rx_bytes);
471 out_offset += snprintf(buf + out_offset, out_count - out_offset,
472 "rx_pkts - \t%llu\n", qp->rx_pkts);
473 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700474 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
475 out_offset += snprintf(buf + out_offset, out_count - out_offset,
476 "rx_async - \t%llu\n", qp->rx_async);
477 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700478 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
479 out_offset += snprintf(buf + out_offset, out_count - out_offset,
480 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
481 out_offset += snprintf(buf + out_offset, out_count - out_offset,
482 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
483 out_offset += snprintf(buf + out_offset, out_count - out_offset,
484 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
485 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400486 "rx_buff - \t0x%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700487 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700488 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700489 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jianga754a8f2016-04-08 10:49:06 -0700490 "rx_max_entry - \t%u\n", qp->rx_max_entry);
491 out_offset += snprintf(buf + out_offset, out_count - out_offset,
492 "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700493
494 out_offset += snprintf(buf + out_offset, out_count - out_offset,
495 "tx_bytes - \t%llu\n", qp->tx_bytes);
496 out_offset += snprintf(buf + out_offset, out_count - out_offset,
497 "tx_pkts - \t%llu\n", qp->tx_pkts);
498 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700499 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
500 out_offset += snprintf(buf + out_offset, out_count - out_offset,
501 "tx_async - \t%llu\n", qp->tx_async);
502 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700503 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
504 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700505 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
506 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400507 "tx_mw - \t0x%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700508 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400509 "tx_index (H) - \t%u\n", qp->tx_index);
510 out_offset += snprintf(buf + out_offset, out_count - out_offset,
511 "RRI (T) - \t%u\n",
512 qp->remote_rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700513 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700514 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400515 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiange74bfee2015-07-13 08:07:17 -0400516 "free tx - \t%u\n",
517 ntb_transport_tx_free_entry(qp));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700518
519 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400520 "\n");
521 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiang569410ca2015-07-13 08:07:22 -0400522 "Using TX DMA - \t%s\n",
523 qp->tx_dma_chan ? "Yes" : "No");
524 out_offset += snprintf(buf + out_offset, out_count - out_offset,
525 "Using RX DMA - \t%s\n",
526 qp->rx_dma_chan ? "Yes" : "No");
Dave Jiangd98ef992015-07-13 08:07:20 -0400527 out_offset += snprintf(buf + out_offset, out_count - out_offset,
528 "QP Link - \t%s\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400529 qp->link_is_up ? "Up" : "Down");
Dave Jiangd98ef992015-07-13 08:07:20 -0400530 out_offset += snprintf(buf + out_offset, out_count - out_offset,
531 "\n");
532
Jon Masond7237e22013-01-19 02:02:25 -0700533 if (out_offset > out_count)
534 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700535
536 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700537 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700538 return ret;
539}
540
541static const struct file_operations ntb_qp_debugfs_stats = {
542 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700543 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700544 .read = debugfs_read,
545};
546
547static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
548 struct list_head *list)
549{
550 unsigned long flags;
551
552 spin_lock_irqsave(lock, flags);
553 list_add_tail(entry, list);
554 spin_unlock_irqrestore(lock, flags);
555}
556
557static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700558 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700559{
560 struct ntb_queue_entry *entry;
561 unsigned long flags;
562
563 spin_lock_irqsave(lock, flags);
564 if (list_empty(list)) {
565 entry = NULL;
566 goto out;
567 }
568 entry = list_first_entry(list, struct ntb_queue_entry, entry);
569 list_del(&entry->entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400570
Jon Masonfce8a7b2012-11-16 19:27:12 -0700571out:
572 spin_unlock_irqrestore(lock, flags);
573
574 return entry;
575}
576
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400577static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
578 struct list_head *list,
579 struct list_head *to_list)
580{
581 struct ntb_queue_entry *entry;
582 unsigned long flags;
583
584 spin_lock_irqsave(lock, flags);
585
586 if (list_empty(list)) {
587 entry = NULL;
588 } else {
589 entry = list_first_entry(list, struct ntb_queue_entry, entry);
590 list_move_tail(&entry->entry, to_list);
591 }
592
593 spin_unlock_irqrestore(lock, flags);
594
595 return entry;
596}
597
Allen Hubbee26a5842015-04-09 10:33:20 -0400598static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
599 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700600{
Allen Hubbee26a5842015-04-09 10:33:20 -0400601 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
602 struct ntb_transport_mw *mw;
Dave Jianga754a8f2016-04-08 10:49:06 -0700603 struct ntb_dev *ndev = nt->ndev;
604 struct ntb_queue_entry *entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700605 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400606 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700607 unsigned int i;
Dave Jianga754a8f2016-04-08 10:49:06 -0700608 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700609
Allen Hubbee26a5842015-04-09 10:33:20 -0400610 mw_count = nt->mw_count;
611 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700612
Allen Hubbee26a5842015-04-09 10:33:20 -0400613 mw_num = QP_TO_MW(nt, qp_num);
614 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700615
Allen Hubbee26a5842015-04-09 10:33:20 -0400616 if (!mw->virt_addr)
617 return -ENOMEM;
618
Logan Gunthorpe8e8496e2017-06-05 14:00:53 -0600619 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -0400620 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700621 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400622 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700623
Allen Hubbee26a5842015-04-09 10:33:20 -0400624 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -0400625 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
Jon Mason793c20e2013-01-19 02:02:26 -0700626 rx_size -= sizeof(struct ntb_rx_info);
627
Jon Mason282a2fe2013-02-12 09:52:50 -0700628 qp->remote_rx_info = qp->rx_buff + rx_size;
629
Jon Masonc9d534c2013-02-01 15:45:16 -0700630 /* Due to housekeeping, there must be atleast 2 buffs */
631 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700632 qp->rx_max_entry = rx_size / qp->rx_max_frame;
633 qp->rx_index = 0;
634
Dave Jianga754a8f2016-04-08 10:49:06 -0700635 /*
636 * Checking to see if we have more entries than the default.
637 * We should add additional entries if that is the case so we
638 * can be in sync with the transport frames.
639 */
640 node = dev_to_node(&ndev->dev);
641 for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
642 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
643 if (!entry)
644 return -ENOMEM;
645
646 entry->qp = qp;
647 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
648 &qp->rx_free_q);
649 qp->rx_alloc_entry++;
650 }
651
Jon Masonc9d534c2013-02-01 15:45:16 -0700652 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700653
Jon Masonef114ed2013-01-19 02:02:18 -0700654 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700655 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400656 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
657 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700658 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700659 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700660
661 qp->rx_pkts = 0;
662 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700663 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400664
665 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700666}
667
Allen Hubbee26a5842015-04-09 10:33:20 -0400668static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700669{
Allen Hubbee26a5842015-04-09 10:33:20 -0400670 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
671 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700672
673 if (!mw->virt_addr)
674 return;
675
Serge Semin443b9a12017-01-11 03:11:33 +0300676 ntb_mw_clear_trans(nt->ndev, PIDX, num_mw);
Allen Hubbee26a5842015-04-09 10:33:20 -0400677 dma_free_coherent(&pdev->dev, mw->buff_size,
678 mw->virt_addr, mw->dma_addr);
679 mw->xlat_size = 0;
680 mw->buff_size = 0;
Jon Mason113fc502013-01-30 11:40:52 -0700681 mw->virt_addr = NULL;
682}
683
Allen Hubbee26a5842015-04-09 10:33:20 -0400684static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400685 resource_size_t size)
Jon Masonb77b2632013-02-01 15:25:37 -0700686{
Allen Hubbee26a5842015-04-09 10:33:20 -0400687 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
688 struct pci_dev *pdev = nt->ndev->pdev;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400689 size_t xlat_size, buff_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400690 int rc;
691
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400692 if (!size)
693 return -EINVAL;
694
Allen Hubbee26a5842015-04-09 10:33:20 -0400695 xlat_size = round_up(size, mw->xlat_align_size);
696 buff_size = round_up(size, mw->xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700697
698 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400699 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700700 return 0;
701
Allen Hubbee26a5842015-04-09 10:33:20 -0400702 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700703 ntb_free_mw(nt, num_mw);
704
Allen Hubbee26a5842015-04-09 10:33:20 -0400705 /* Alloc memory for receiving data. Must be aligned */
706 mw->xlat_size = xlat_size;
707 mw->buff_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700708
Allen Hubbee26a5842015-04-09 10:33:20 -0400709 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
710 &mw->dma_addr, GFP_KERNEL);
Jon Masonb77b2632013-02-01 15:25:37 -0700711 if (!mw->virt_addr) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400712 mw->xlat_size = 0;
713 mw->buff_size = 0;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400714 dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400715 buff_size);
Jon Masonb77b2632013-02-01 15:25:37 -0700716 return -ENOMEM;
717 }
718
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700719 /*
720 * we must ensure that the memory address allocated is BAR size
721 * aligned in order for the XLAT register to take the value. This
722 * is a requirement of the hardware. It is recommended to setup CMA
723 * for BAR sizes equal or greater than 4MB.
724 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400725 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
726 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700727 &mw->dma_addr);
728 ntb_free_mw(nt, num_mw);
729 return -ENOMEM;
730 }
731
Jon Masonb77b2632013-02-01 15:25:37 -0700732 /* Notify HW the memory location of the receive buffer */
Serge Semin443b9a12017-01-11 03:11:33 +0300733 rc = ntb_mw_set_trans(nt->ndev, PIDX, num_mw, mw->dma_addr,
734 mw->xlat_size);
Allen Hubbee26a5842015-04-09 10:33:20 -0400735 if (rc) {
736 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
737 ntb_free_mw(nt, num_mw);
738 return -EIO;
739 }
Jon Masonb77b2632013-02-01 15:25:37 -0700740
741 return 0;
742}
743
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400744static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
745{
746 qp->link_is_up = false;
Dave Jiange9021332016-02-23 09:11:36 -0700747 qp->active = false;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400748
749 qp->tx_index = 0;
750 qp->rx_index = 0;
751 qp->rx_bytes = 0;
752 qp->rx_pkts = 0;
753 qp->rx_ring_empty = 0;
754 qp->rx_err_no_buf = 0;
755 qp->rx_err_oflow = 0;
756 qp->rx_err_ver = 0;
757 qp->rx_memcpy = 0;
758 qp->rx_async = 0;
759 qp->tx_bytes = 0;
760 qp->tx_pkts = 0;
761 qp->tx_ring_full = 0;
762 qp->tx_err_no_buf = 0;
763 qp->tx_memcpy = 0;
764 qp->tx_async = 0;
765}
766
Jon Masonfca4d512013-09-09 13:39:55 -0700767static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700768{
Allen Hubbee26a5842015-04-09 10:33:20 -0400769 struct ntb_transport_ctx *nt = qp->transport;
770 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700771
Allen Hubbee22e0b92015-05-12 06:55:44 -0400772 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400773
774 cancel_delayed_work_sync(&qp->link_work);
775 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400776
777 if (qp->event_handler)
778 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700779}
780
781static void ntb_qp_link_cleanup_work(struct work_struct *work)
782{
783 struct ntb_transport_qp *qp = container_of(work,
784 struct ntb_transport_qp,
785 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400786 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700787
788 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700789
Allen Hubbee26a5842015-04-09 10:33:20 -0400790 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700791 schedule_delayed_work(&qp->link_work,
792 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
793}
794
Jon Mason7b4f2d32013-01-19 02:02:19 -0700795static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700796{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700797 schedule_work(&qp->link_cleanup);
798}
799
Allen Hubbee26a5842015-04-09 10:33:20 -0400800static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700801{
Allen Hubbee26a5842015-04-09 10:33:20 -0400802 struct ntb_transport_qp *qp;
803 u64 qp_bitmap_alloc;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +0530804 unsigned int i, count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700805
Allen Hubbee26a5842015-04-09 10:33:20 -0400806 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700807
Allen Hubbee26a5842015-04-09 10:33:20 -0400808 /* Pass along the info to any clients */
809 for (i = 0; i < nt->qp_count; i++)
810 if (qp_bitmap_alloc & BIT_ULL(i)) {
811 qp = &nt->qp_vec[i];
812 ntb_qp_link_cleanup(qp);
813 cancel_work_sync(&qp->link_cleanup);
814 cancel_delayed_work_sync(&qp->link_work);
815 }
816
817 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700818 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700819
Jon Masonfce8a7b2012-11-16 19:27:12 -0700820 /* The scratchpad registers keep the values if the remote side
821 * goes down, blast them now to give them a sane value the next
822 * time they are accessed
823 */
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +0530824 count = ntb_spad_count(nt->ndev);
825 for (i = 0; i < count; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400826 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700827}
828
Jon Masonfca4d512013-09-09 13:39:55 -0700829static void ntb_transport_link_cleanup_work(struct work_struct *work)
830{
Allen Hubbee26a5842015-04-09 10:33:20 -0400831 struct ntb_transport_ctx *nt =
832 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700833
834 ntb_transport_link_cleanup(nt);
835}
836
Allen Hubbee26a5842015-04-09 10:33:20 -0400837static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700838{
Allen Hubbee26a5842015-04-09 10:33:20 -0400839 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700840
Allen Hubbee26a5842015-04-09 10:33:20 -0400841 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700842 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400843 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700844 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700845}
846
847static void ntb_transport_link_work(struct work_struct *work)
848{
Allen Hubbee26a5842015-04-09 10:33:20 -0400849 struct ntb_transport_ctx *nt =
850 container_of(work, struct ntb_transport_ctx, link_work.work);
851 struct ntb_dev *ndev = nt->ndev;
852 struct pci_dev *pdev = ndev->pdev;
853 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700854 u32 val;
Dave Jiang84f76682016-02-29 09:35:26 -0700855 int rc = 0, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700856
Jon Mason113fc502013-01-30 11:40:52 -0700857 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400858 for (i = 0; i < nt->mw_count; i++) {
859 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700860
Allen Hubbee26a5842015-04-09 10:33:20 -0400861 if (max_mw_size && size > max_mw_size)
862 size = max_mw_size;
863
864 spad = MW0_SZ_HIGH + (i * 2);
Serge Semind67288a2017-01-11 03:13:20 +0300865 ntb_peer_spad_write(ndev, PIDX, spad, upper_32_bits(size));
Allen Hubbee26a5842015-04-09 10:33:20 -0400866
867 spad = MW0_SZ_LOW + (i * 2);
Serge Semind67288a2017-01-11 03:13:20 +0300868 ntb_peer_spad_write(ndev, PIDX, spad, lower_32_bits(size));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700869 }
870
Serge Semind67288a2017-01-11 03:13:20 +0300871 ntb_peer_spad_write(ndev, PIDX, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700872
Serge Semind67288a2017-01-11 03:13:20 +0300873 ntb_peer_spad_write(ndev, PIDX, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700874
Serge Semind67288a2017-01-11 03:13:20 +0300875 ntb_peer_spad_write(ndev, PIDX, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700876
877 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400878 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400879 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700880 if (val != NTB_TRANSPORT_VERSION)
881 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700882
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400883 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700884 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400885 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700886 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700887
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400888 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700889 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400890 if (val != nt->mw_count)
891 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700892
Allen Hubbee26a5842015-04-09 10:33:20 -0400893 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700894 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700895
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400896 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400897 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700898
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400899 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700900 val64 |= val;
901
Allen Hubbee26a5842015-04-09 10:33:20 -0400902 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700903
904 rc = ntb_set_mw(nt, i, val64);
905 if (rc)
906 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700907 }
908
Allen Hubbee26a5842015-04-09 10:33:20 -0400909 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700910
Allen Hubbee26a5842015-04-09 10:33:20 -0400911 for (i = 0; i < nt->qp_count; i++) {
912 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700913
914 ntb_transport_setup_qp_mw(nt, i);
915
Allen Hubbee26a5842015-04-09 10:33:20 -0400916 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700917 schedule_delayed_work(&qp->link_work, 0);
918 }
919
920 return;
921
Jon Mason113fc502013-01-30 11:40:52 -0700922out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400923 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700924 ntb_free_mw(nt, i);
Dave Jiang84f76682016-02-29 09:35:26 -0700925
926 /* if there's an actual failure, we should just bail */
Dave Jiangf3fd2af2017-07-28 15:10:48 -0700927 if (rc < 0)
Dave Jiang84f76682016-02-29 09:35:26 -0700928 return;
Dave Jiang84f76682016-02-29 09:35:26 -0700929
Jon Masonfce8a7b2012-11-16 19:27:12 -0700930out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400931 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700932 schedule_delayed_work(&nt->link_work,
933 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
934}
935
936static void ntb_qp_link_work(struct work_struct *work)
937{
938 struct ntb_transport_qp *qp = container_of(work,
939 struct ntb_transport_qp,
940 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400941 struct pci_dev *pdev = qp->ndev->pdev;
942 struct ntb_transport_ctx *nt = qp->transport;
943 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700944
Allen Hubbee26a5842015-04-09 10:33:20 -0400945 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700946
Allen Hubbee26a5842015-04-09 10:33:20 -0400947 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700948
Serge Semind67288a2017-01-11 03:13:20 +0300949 ntb_peer_spad_write(nt->ndev, PIDX, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700950
951 /* query remote spad for qp ready bits */
Allen Hubbe28762282015-05-11 10:08:26 -0400952 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700953
954 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400955 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700956 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400957 qp->link_is_up = true;
Dave Jiange9021332016-02-23 09:11:36 -0700958 qp->active = true;
Allen Hubbee26a5842015-04-09 10:33:20 -0400959
Jon Masonfce8a7b2012-11-16 19:27:12 -0700960 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400961 qp->event_handler(qp->cb_data, qp->link_is_up);
Allen Hubbe8b5a22d2015-07-13 08:07:12 -0400962
Dave Jiange9021332016-02-23 09:11:36 -0700963 if (qp->active)
964 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400965 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700966 schedule_delayed_work(&qp->link_work,
967 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
968}
969
Allen Hubbee26a5842015-04-09 10:33:20 -0400970static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700971 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700972{
973 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400974 phys_addr_t mw_base;
975 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700976 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400977 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700978 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700979
Allen Hubbee26a5842015-04-09 10:33:20 -0400980 mw_count = nt->mw_count;
981 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700982
Allen Hubbee26a5842015-04-09 10:33:20 -0400983 mw_num = QP_TO_MW(nt, qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400984
985 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700986 qp->qp_num = qp_num;
987 qp->transport = nt;
988 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400989 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700990 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400991 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700992
Logan Gunthorpe8e8496e2017-06-05 14:00:53 -0600993 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -0400994 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -0700995 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400996 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -0700997
Allen Hubbee26a5842015-04-09 10:33:20 -0400998 mw_base = nt->mw_vec[mw_num].phys_addr;
999 mw_size = nt->mw_vec[mw_num].phys_size;
1000
1001 tx_size = (unsigned int)mw_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -04001002 qp_offset = tx_size * (qp_num / mw_count);
Allen Hubbee26a5842015-04-09 10:33:20 -04001003
1004 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001005 if (!qp->tx_mw)
1006 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -07001007
Allen Hubbee26a5842015-04-09 10:33:20 -04001008 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001009 if (!qp->tx_mw_phys)
1010 return -EINVAL;
1011
1012 tx_size -= sizeof(struct ntb_rx_info);
1013 qp->rx_info = qp->tx_mw + tx_size;
1014
Jon Masonc9d534c2013-02-01 15:45:16 -07001015 /* Due to housekeeping, there must be atleast 2 buffs */
1016 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -07001017 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -07001018
Dave Jiangc8650fd2015-07-13 08:07:09 -04001019 if (nt->debugfs_node_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001020 char debugfs_name[4];
1021
1022 snprintf(debugfs_name, 4, "qp%d", qp_num);
1023 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Dave Jiangc8650fd2015-07-13 08:07:09 -04001024 nt->debugfs_node_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001025
1026 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1027 qp->debugfs_dir, qp,
1028 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -04001029 } else {
1030 qp->debugfs_dir = NULL;
1031 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001032 }
1033
1034 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001035 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001036
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001037 spin_lock_init(&qp->ntb_rx_q_lock);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001038 spin_lock_init(&qp->ntb_tx_free_q_lock);
1039
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001040 INIT_LIST_HEAD(&qp->rx_post_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001041 INIT_LIST_HEAD(&qp->rx_pend_q);
1042 INIT_LIST_HEAD(&qp->rx_free_q);
1043 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001044
Allen Hubbee26a5842015-04-09 10:33:20 -04001045 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1046 (unsigned long)qp);
1047
Jon Mason282a2fe2013-02-12 09:52:50 -07001048 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001049}
1050
Allen Hubbee26a5842015-04-09 10:33:20 -04001051static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001052{
Allen Hubbee26a5842015-04-09 10:33:20 -04001053 struct ntb_transport_ctx *nt;
1054 struct ntb_transport_mw *mw;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301055 unsigned int mw_count, qp_count, spad_count, max_mw_count_for_spads;
Allen Hubbee26a5842015-04-09 10:33:20 -04001056 u64 qp_bitmap;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001057 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001058 int rc, i;
1059
Logan Gunthorpebc240ee2017-06-26 13:50:41 -06001060 mw_count = ntb_peer_mw_count(ndev);
Serge Semin443b9a12017-01-11 03:11:33 +03001061
1062 if (!ndev->ops->mw_set_trans) {
1063 dev_err(&ndev->dev, "Inbound MW based NTB API is required\n");
1064 return -EINVAL;
1065 }
Logan Gunthorpe19645a02016-06-07 11:20:22 -06001066
Allen Hubbee26a5842015-04-09 10:33:20 -04001067 if (ntb_db_is_unsafe(ndev))
1068 dev_dbg(&ndev->dev,
1069 "doorbell is unsafe, proceed anyway...\n");
1070 if (ntb_spad_is_unsafe(ndev))
1071 dev_dbg(&ndev->dev,
1072 "scratchpad is unsafe, proceed anyway...\n");
1073
Serge Semin1e530112016-12-14 02:49:14 +03001074 if (ntb_peer_port_count(ndev) != NTB_DEF_PEER_CNT)
1075 dev_warn(&ndev->dev, "Multi-port NTB devices unsupported\n");
1076
Allen Hubbe1199aa62015-05-18 06:20:47 -04001077 node = dev_to_node(&ndev->dev);
1078
1079 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001080 if (!nt)
1081 return -ENOMEM;
1082
Allen Hubbee26a5842015-04-09 10:33:20 -04001083 nt->ndev = ndev;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301084 spad_count = ntb_spad_count(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04001085
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301086 /* Limit the MW's based on the availability of scratchpads */
1087
1088 if (spad_count < NTB_TRANSPORT_MIN_SPADS) {
1089 nt->mw_count = 0;
1090 rc = -EINVAL;
1091 goto err;
1092 }
1093
1094 max_mw_count_for_spads = (spad_count - MW0_SZ_HIGH) / 2;
1095 nt->mw_count = min(mw_count, max_mw_count_for_spads);
Allen Hubbee26a5842015-04-09 10:33:20 -04001096
Allen Hubbe1199aa62015-05-18 06:20:47 -04001097 nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1098 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001099 if (!nt->mw_vec) {
1100 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001101 goto err;
1102 }
1103
Allen Hubbee26a5842015-04-09 10:33:20 -04001104 for (i = 0; i < mw_count; i++) {
1105 mw = &nt->mw_vec[i];
1106
Serge Semin443b9a12017-01-11 03:11:33 +03001107 rc = ntb_mw_get_align(ndev, PIDX, i, &mw->xlat_align,
1108 &mw->xlat_align_size, NULL);
1109 if (rc)
1110 goto err1;
1111
1112 rc = ntb_peer_mw_get_addr(ndev, i, &mw->phys_addr,
1113 &mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001114 if (rc)
1115 goto err1;
1116
Dave Jiang06917f72015-05-19 16:45:46 -04001117 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001118 if (!mw->vbase) {
1119 rc = -ENOMEM;
1120 goto err1;
1121 }
1122
1123 mw->buff_size = 0;
1124 mw->xlat_size = 0;
1125 mw->virt_addr = NULL;
1126 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -07001127 }
1128
Allen Hubbee26a5842015-04-09 10:33:20 -04001129 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001130
Allen Hubbee26a5842015-04-09 10:33:20 -04001131 qp_count = ilog2(qp_bitmap);
1132 if (max_num_clients && max_num_clients < qp_count)
1133 qp_count = max_num_clients;
Logan Gunthorpecb827ee2017-06-05 14:00:52 -06001134 else if (nt->mw_count < qp_count)
1135 qp_count = nt->mw_count;
Allen Hubbee26a5842015-04-09 10:33:20 -04001136
1137 qp_bitmap &= BIT_ULL(qp_count) - 1;
1138
1139 nt->qp_count = qp_count;
1140 nt->qp_bitmap = qp_bitmap;
1141 nt->qp_bitmap_free = qp_bitmap;
1142
Allen Hubbe1199aa62015-05-18 06:20:47 -04001143 nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1144 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001145 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001146 rc = -ENOMEM;
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301147 goto err1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001148 }
1149
Dave Jiangc8650fd2015-07-13 08:07:09 -04001150 if (nt_debugfs_dir) {
1151 nt->debugfs_node_dir =
1152 debugfs_create_dir(pci_name(ndev->pdev),
1153 nt_debugfs_dir);
1154 }
1155
Allen Hubbee26a5842015-04-09 10:33:20 -04001156 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001157 rc = ntb_transport_init_queue(nt, i);
1158 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301159 goto err2;
Jon Mason282a2fe2013-02-12 09:52:50 -07001160 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001161
1162 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001163 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001164
Allen Hubbee26a5842015-04-09 10:33:20 -04001165 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001166 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301167 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001168
1169 INIT_LIST_HEAD(&nt->client_devs);
1170 rc = ntb_bus_init(nt);
1171 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301172 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001173
Allen Hubbee26a5842015-04-09 10:33:20 -04001174 nt->link_is_up = false;
1175 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1176 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001177
1178 return 0;
1179
Jon Mason948d3a62013-04-18 17:07:36 -07001180err3:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301181 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001182err2:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301183 kfree(nt->qp_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001184err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001185 while (i--) {
1186 mw = &nt->mw_vec[i];
1187 iounmap(mw->vbase);
1188 }
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301189 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001190err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001191 kfree(nt);
1192 return rc;
1193}
1194
Allen Hubbee26a5842015-04-09 10:33:20 -04001195static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001196{
Allen Hubbee26a5842015-04-09 10:33:20 -04001197 struct ntb_transport_ctx *nt = ndev->ctx;
1198 struct ntb_transport_qp *qp;
1199 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001200 int i;
1201
Jon Masonfca4d512013-09-09 13:39:55 -07001202 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001203 cancel_work_sync(&nt->link_cleanup);
1204 cancel_delayed_work_sync(&nt->link_work);
1205
1206 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001207
1208 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001209 for (i = 0; i < nt->qp_count; i++) {
1210 qp = &nt->qp_vec[i];
1211 if (qp_bitmap_alloc & BIT_ULL(i))
1212 ntb_transport_free_queue(qp);
1213 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001214 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001215
Allen Hubbee26a5842015-04-09 10:33:20 -04001216 ntb_link_disable(ndev);
1217 ntb_clear_ctx(ndev);
1218
Jon Masonfce8a7b2012-11-16 19:27:12 -07001219 ntb_bus_remove(nt);
1220
Allen Hubbee26a5842015-04-09 10:33:20 -04001221 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001222 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001223 iounmap(nt->mw_vec[i].vbase);
1224 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001225
Allen Hubbee26a5842015-04-09 10:33:20 -04001226 kfree(nt->qp_vec);
1227 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001228 kfree(nt);
1229}
1230
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001231static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1232{
1233 struct ntb_queue_entry *entry;
1234 void *cb_data;
1235 unsigned int len;
1236 unsigned long irqflags;
1237
1238 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1239
1240 while (!list_empty(&qp->rx_post_q)) {
1241 entry = list_first_entry(&qp->rx_post_q,
1242 struct ntb_queue_entry, entry);
1243 if (!(entry->flags & DESC_DONE_FLAG))
1244 break;
1245
1246 entry->rx_hdr->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07001247 iowrite32(entry->rx_index, &qp->rx_info->entry);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001248
1249 cb_data = entry->cb_data;
1250 len = entry->len;
1251
1252 list_move_tail(&entry->entry, &qp->rx_free_q);
1253
1254 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1255
1256 if (qp->rx_handler && qp->client_ready)
1257 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1258
1259 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1260 }
1261
1262 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1263}
1264
Dave Jiang72203572016-07-20 13:14:13 -07001265static void ntb_rx_copy_callback(void *data,
1266 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001267{
Jon Mason282a2fe2013-02-12 09:52:50 -07001268 struct ntb_queue_entry *entry = data;
Jon Mason448c6fb2013-01-19 02:02:27 -07001269
Dave Jiang72203572016-07-20 13:14:13 -07001270 /* we need to check DMA results if we are using DMA */
1271 if (res) {
1272 enum dmaengine_tx_result dma_err = res->result;
1273
1274 switch (dma_err) {
1275 case DMA_TRANS_READ_FAILED:
1276 case DMA_TRANS_WRITE_FAILED:
1277 entry->errors++;
1278 case DMA_TRANS_ABORTED:
1279 {
1280 struct ntb_transport_qp *qp = entry->qp;
1281 void *offset = qp->rx_buff + qp->rx_max_frame *
1282 qp->rx_index;
1283
1284 ntb_memcpy_rx(entry, offset);
1285 qp->rx_memcpy++;
1286 return;
1287 }
1288
1289 case DMA_TRANS_NOERROR:
1290 default:
1291 break;
1292 }
1293 }
1294
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001295 entry->flags |= DESC_DONE_FLAG;
Jon Mason282a2fe2013-02-12 09:52:50 -07001296
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001297 ntb_complete_rxc(entry->qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001298}
1299
Jon Mason282a2fe2013-02-12 09:52:50 -07001300static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1301{
1302 void *buf = entry->buf;
1303 size_t len = entry->len;
1304
1305 memcpy(buf, offset, len);
1306
Allen Hubbee26a5842015-04-09 10:33:20 -04001307 /* Ensure that the data is fully copied out before clearing the flag */
1308 wmb();
1309
Dave Jiang72203572016-07-20 13:14:13 -07001310 ntb_rx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001311}
1312
Dave Jiang72203572016-07-20 13:14:13 -07001313static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
Jon Mason282a2fe2013-02-12 09:52:50 -07001314{
1315 struct dma_async_tx_descriptor *txd;
1316 struct ntb_transport_qp *qp = entry->qp;
Dave Jiang569410ca2015-07-13 08:07:22 -04001317 struct dma_chan *chan = qp->rx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001318 struct dma_device *device;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001319 size_t pay_off, buff_off, len;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001320 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001321 dma_cookie_t cookie;
1322 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001323
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001324 len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001325 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001326 pay_off = (size_t)offset & ~PAGE_MASK;
1327 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001328
1329 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Allen Hubbe905921e2015-07-13 08:07:21 -04001330 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001331
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001332 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1333 if (!unmap)
Allen Hubbe905921e2015-07-13 08:07:21 -04001334 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001335
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001336 unmap->len = len;
1337 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1338 pay_off, len, DMA_TO_DEVICE);
1339 if (dma_mapping_error(device->dev, unmap->addr[0]))
1340 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001341
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001342 unmap->to_cnt = 1;
1343
1344 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1345 buff_off, len, DMA_FROM_DEVICE);
1346 if (dma_mapping_error(device->dev, unmap->addr[1]))
1347 goto err_get_unmap;
1348
1349 unmap->from_cnt = 1;
1350
Allen Hubbe88931ec2017-06-09 18:06:36 -04001351 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
1352 unmap->addr[0], len,
1353 DMA_PREP_INTERRUPT);
1354 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001355 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001356
Dave Jiang72203572016-07-20 13:14:13 -07001357 txd->callback_result = ntb_rx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001358 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001359 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001360
1361 cookie = dmaengine_submit(txd);
1362 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001363 goto err_set_unmap;
1364
1365 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001366
1367 qp->last_cookie = cookie;
1368
1369 qp->rx_async++;
1370
Dave Jiang72203572016-07-20 13:14:13 -07001371 return 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001372
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001373err_set_unmap:
1374 dmaengine_unmap_put(unmap);
1375err_get_unmap:
1376 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001377err:
Dave Jiang72203572016-07-20 13:14:13 -07001378 return -ENXIO;
1379}
1380
1381static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
1382{
1383 struct ntb_transport_qp *qp = entry->qp;
1384 struct dma_chan *chan = qp->rx_dma_chan;
1385 int res;
1386
1387 if (!chan)
1388 goto err;
1389
1390 if (entry->len < copy_bytes)
1391 goto err;
1392
1393 res = ntb_async_rx_submit(entry, offset);
1394 if (res < 0)
1395 goto err;
1396
1397 if (!entry->retries)
1398 qp->rx_async++;
1399
1400 return;
1401
1402err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001403 ntb_memcpy_rx(entry, offset);
1404 qp->rx_memcpy++;
1405}
1406
Jon Masonfce8a7b2012-11-16 19:27:12 -07001407static int ntb_process_rxc(struct ntb_transport_qp *qp)
1408{
1409 struct ntb_payload_header *hdr;
1410 struct ntb_queue_entry *entry;
1411 void *offset;
1412
Jon Mason793c20e2013-01-19 02:02:26 -07001413 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1414 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1415
Allen Hubbee26a5842015-04-09 10:33:20 -04001416 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1417 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001418
Jon Masonfce8a7b2012-11-16 19:27:12 -07001419 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001420 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001421 qp->rx_ring_empty++;
1422 return -EAGAIN;
1423 }
1424
Allen Hubbee26a5842015-04-09 10:33:20 -04001425 if (hdr->flags & LINK_DOWN_FLAG) {
1426 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1427 ntb_qp_link_down(qp);
1428 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001429 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001430 }
1431
1432 if (hdr->ver != (u32)qp->rx_pkts) {
1433 dev_dbg(&qp->ndev->pdev->dev,
1434 "version mismatch, expected %llu - got %u\n",
1435 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001436 qp->rx_err_ver++;
1437 return -EIO;
1438 }
1439
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001440 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
Allen Hubbee26a5842015-04-09 10:33:20 -04001441 if (!entry) {
1442 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1443 qp->rx_err_no_buf++;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001444 return -EAGAIN;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001445 }
1446
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001447 entry->rx_hdr = hdr;
Dave Jiang72203572016-07-20 13:14:13 -07001448 entry->rx_index = qp->rx_index;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001449
Allen Hubbee26a5842015-04-09 10:33:20 -04001450 if (hdr->len > entry->len) {
1451 dev_dbg(&qp->ndev->pdev->dev,
1452 "receive buffer overflow! Wanted %d got %d\n",
1453 hdr->len, entry->len);
1454 qp->rx_err_oflow++;
1455
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001456 entry->len = -EIO;
1457 entry->flags |= DESC_DONE_FLAG;
1458
1459 ntb_complete_rxc(qp);
1460 } else {
1461 dev_dbg(&qp->ndev->pdev->dev,
1462 "RX OK index %u ver %u size %d into buf size %d\n",
1463 qp->rx_index, hdr->ver, hdr->len, entry->len);
1464
1465 qp->rx_bytes += hdr->len;
1466 qp->rx_pkts++;
1467
1468 entry->len = hdr->len;
1469
1470 ntb_async_rx(entry, offset);
Allen Hubbee26a5842015-04-09 10:33:20 -04001471 }
1472
Jon Mason282a2fe2013-02-12 09:52:50 -07001473 qp->rx_index++;
1474 qp->rx_index %= qp->rx_max_entry;
1475
1476 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001477}
1478
Allen Hubbee26a5842015-04-09 10:33:20 -04001479static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001480{
Allen Hubbee26a5842015-04-09 10:33:20 -04001481 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001482 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001483
Allen Hubbee26a5842015-04-09 10:33:20 -04001484 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1485 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001486
Jon Masonc336acd2013-01-17 15:28:45 -07001487 /* Limit the number of packets processed in a single interrupt to
1488 * provide fairness to others
1489 */
1490 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001491 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001492 if (rc)
1493 break;
1494 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001495
Dave Jiang569410ca2015-07-13 08:07:22 -04001496 if (i && qp->rx_dma_chan)
1497 dma_async_issue_pending(qp->rx_dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001498
Allen Hubbee26a5842015-04-09 10:33:20 -04001499 if (i == qp->rx_max_entry) {
1500 /* there is more work to do */
Dave Jiange9021332016-02-23 09:11:36 -07001501 if (qp->active)
1502 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001503 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1504 /* the doorbell bit is set: clear it */
1505 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1506 /* ntb_db_read ensures ntb_db_clear write is committed */
1507 ntb_db_read(qp->ndev);
1508
1509 /* an interrupt may have arrived between finishing
1510 * ntb_process_rxc and clearing the doorbell bit:
1511 * there might be some more work to do.
1512 */
Dave Jiange9021332016-02-23 09:11:36 -07001513 if (qp->active)
1514 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001515 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001516}
1517
Dave Jiang9cabc262016-07-20 13:14:07 -07001518static void ntb_tx_copy_callback(void *data,
1519 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001520{
Jon Mason282a2fe2013-02-12 09:52:50 -07001521 struct ntb_queue_entry *entry = data;
1522 struct ntb_transport_qp *qp = entry->qp;
1523 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001524
Dave Jiang9cabc262016-07-20 13:14:07 -07001525 /* we need to check DMA results if we are using DMA */
1526 if (res) {
1527 enum dmaengine_tx_result dma_err = res->result;
1528
1529 switch (dma_err) {
1530 case DMA_TRANS_READ_FAILED:
1531 case DMA_TRANS_WRITE_FAILED:
1532 entry->errors++;
1533 case DMA_TRANS_ABORTED:
1534 {
1535 void __iomem *offset =
1536 qp->tx_mw + qp->tx_max_frame *
1537 entry->tx_index;
1538
1539 /* resubmit via CPU */
1540 ntb_memcpy_tx(entry, offset);
1541 qp->tx_memcpy++;
1542 return;
1543 }
1544
1545 case DMA_TRANS_NOERROR:
1546 default:
1547 break;
1548 }
1549 }
1550
Jon Mason74465642013-01-21 15:28:52 -07001551 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001552
Allen Hubbee26a5842015-04-09 10:33:20 -04001553 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001554
1555 /* The entry length can only be zero if the packet is intended to be a
1556 * "link down" or similar. Since no payload is being sent in these
1557 * cases, there is nothing to add to the completion queue.
1558 */
1559 if (entry->len > 0) {
1560 qp->tx_bytes += entry->len;
1561
1562 if (qp->tx_handler)
1563 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1564 entry->len);
1565 }
1566
1567 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1568}
1569
Jon Mason282a2fe2013-02-12 09:52:50 -07001570static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1571{
Dave Jiang06917f72015-05-19 16:45:46 -04001572#ifdef ARCH_HAS_NOCACHE_UACCESS
1573 /*
1574 * Using non-temporal mov to improve performance on non-cached
1575 * writes, even though we aren't actually copying from user space.
1576 */
1577 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1578#else
Jon Mason282a2fe2013-02-12 09:52:50 -07001579 memcpy_toio(offset, entry->buf, entry->len);
Dave Jiang06917f72015-05-19 16:45:46 -04001580#endif
Jon Mason282a2fe2013-02-12 09:52:50 -07001581
Allen Hubbee26a5842015-04-09 10:33:20 -04001582 /* Ensure that the data is fully copied out before setting the flags */
1583 wmb();
1584
Dave Jiang9cabc262016-07-20 13:14:07 -07001585 ntb_tx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001586}
1587
Dave Jiang9cabc262016-07-20 13:14:07 -07001588static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1589 struct ntb_queue_entry *entry)
Jon Mason282a2fe2013-02-12 09:52:50 -07001590{
Jon Mason282a2fe2013-02-12 09:52:50 -07001591 struct dma_async_tx_descriptor *txd;
Dave Jiang569410ca2015-07-13 08:07:22 -04001592 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001593 struct dma_device *device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001594 size_t len = entry->len;
1595 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001596 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001597 struct dmaengine_unmap_data *unmap;
1598 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001599 dma_cookie_t cookie;
Jon Mason282a2fe2013-02-12 09:52:50 -07001600
Jon Mason282a2fe2013-02-12 09:52:50 -07001601 device = chan->device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001602 dest = qp->tx_mw_phys + qp->tx_max_frame * entry->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001603 buff_off = (size_t)buf & ~PAGE_MASK;
1604 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001605
1606 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1607 goto err;
1608
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001609 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1610 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001611 goto err;
1612
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001613 unmap->len = len;
1614 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1615 buff_off, len, DMA_TO_DEVICE);
1616 if (dma_mapping_error(device->dev, unmap->addr[0]))
1617 goto err_get_unmap;
1618
1619 unmap->to_cnt = 1;
1620
Allen Hubbe88931ec2017-06-09 18:06:36 -04001621 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
1622 DMA_PREP_INTERRUPT);
1623 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001624 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001625
Dave Jiang9cabc262016-07-20 13:14:07 -07001626 txd->callback_result = ntb_tx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001627 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001628 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001629
1630 cookie = dmaengine_submit(txd);
1631 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001632 goto err_set_unmap;
1633
1634 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001635
1636 dma_async_issue_pending(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001637
Dave Jiang9cabc262016-07-20 13:14:07 -07001638 return 0;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001639err_set_unmap:
1640 dmaengine_unmap_put(unmap);
1641err_get_unmap:
1642 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001643err:
Dave Jiang9cabc262016-07-20 13:14:07 -07001644 return -ENXIO;
1645}
1646
1647static void ntb_async_tx(struct ntb_transport_qp *qp,
1648 struct ntb_queue_entry *entry)
1649{
1650 struct ntb_payload_header __iomem *hdr;
1651 struct dma_chan *chan = qp->tx_dma_chan;
1652 void __iomem *offset;
1653 int res;
1654
1655 entry->tx_index = qp->tx_index;
1656 offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
1657 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1658 entry->tx_hdr = hdr;
1659
1660 iowrite32(entry->len, &hdr->len);
1661 iowrite32((u32)qp->tx_pkts, &hdr->ver);
1662
1663 if (!chan)
1664 goto err;
1665
1666 if (entry->len < copy_bytes)
1667 goto err;
1668
1669 res = ntb_async_tx_submit(qp, entry);
1670 if (res < 0)
1671 goto err;
1672
1673 if (!entry->retries)
1674 qp->tx_async++;
1675
1676 return;
1677
1678err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001679 ntb_memcpy_tx(entry, offset);
1680 qp->tx_memcpy++;
1681}
1682
Jon Masonfce8a7b2012-11-16 19:27:12 -07001683static int ntb_process_tx(struct ntb_transport_qp *qp,
1684 struct ntb_queue_entry *entry)
1685{
Jon Mason793c20e2013-01-19 02:02:26 -07001686 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001687 qp->tx_ring_full++;
1688 return -EAGAIN;
1689 }
1690
Jon Masonef114ed2013-01-19 02:02:18 -07001691 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001692 if (qp->tx_handler)
Jon Mason179f9122015-12-18 13:22:37 -05001693 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001694
1695 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1696 &qp->tx_free_q);
1697 return 0;
1698 }
1699
Jon Mason282a2fe2013-02-12 09:52:50 -07001700 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001701
Jon Mason793c20e2013-01-19 02:02:26 -07001702 qp->tx_index++;
1703 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001704
1705 qp->tx_pkts++;
1706
1707 return 0;
1708}
1709
1710static void ntb_send_link_down(struct ntb_transport_qp *qp)
1711{
Allen Hubbee26a5842015-04-09 10:33:20 -04001712 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001713 struct ntb_queue_entry *entry;
1714 int i, rc;
1715
Allen Hubbee26a5842015-04-09 10:33:20 -04001716 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001717 return;
1718
Allen Hubbee22e0b92015-05-12 06:55:44 -04001719 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001720
1721 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001722 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001723 if (entry)
1724 break;
1725 msleep(100);
1726 }
1727
1728 if (!entry)
1729 return;
1730
1731 entry->cb_data = NULL;
1732 entry->buf = NULL;
1733 entry->len = 0;
1734 entry->flags = LINK_DOWN_FLAG;
1735
1736 rc = ntb_process_tx(qp, entry);
1737 if (rc)
1738 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1739 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001740
1741 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001742}
1743
Allen Hubbe1199aa62015-05-18 06:20:47 -04001744static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1745{
1746 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1747}
1748
Jon Masonfce8a7b2012-11-16 19:27:12 -07001749/**
1750 * ntb_transport_create_queue - Create a new NTB transport layer queue
1751 * @rx_handler: receive callback function
1752 * @tx_handler: transmit callback function
1753 * @event_handler: event callback function
1754 *
1755 * Create a new NTB transport layer queue and provide the queue with a callback
1756 * routine for both transmit and receive. The receive callback routine will be
1757 * used to pass up data when the transport has received it on the queue. The
1758 * transmit callback routine will be called when the transport has completed the
1759 * transmission of the data on the queue and the data is ready to be freed.
1760 *
1761 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1762 */
1763struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001764ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001765 const struct ntb_queue_handlers *handlers)
1766{
Allen Hubbee26a5842015-04-09 10:33:20 -04001767 struct ntb_dev *ndev;
1768 struct pci_dev *pdev;
1769 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001770 struct ntb_queue_entry *entry;
1771 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001772 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001773 unsigned int free_queue;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001774 dma_cap_mask_t dma_mask;
1775 int node;
Allen Hubbee26a5842015-04-09 10:33:20 -04001776 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001777
Allen Hubbee26a5842015-04-09 10:33:20 -04001778 ndev = dev_ntb(client_dev->parent);
1779 pdev = ndev->pdev;
1780 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001781
Allen Hubbe1199aa62015-05-18 06:20:47 -04001782 node = dev_to_node(&ndev->dev);
1783
Thomas VanSelus8fcd0952017-02-13 16:46:26 -06001784 free_queue = ffs(nt->qp_bitmap_free);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001785 if (!free_queue)
1786 goto err;
1787
1788 /* decrement free_queue to make it zero based */
1789 free_queue--;
1790
Allen Hubbee26a5842015-04-09 10:33:20 -04001791 qp = &nt->qp_vec[free_queue];
1792 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001793
Allen Hubbee26a5842015-04-09 10:33:20 -04001794 nt->qp_bitmap_free &= ~qp_bit;
1795
Jon Masonfce8a7b2012-11-16 19:27:12 -07001796 qp->cb_data = data;
1797 qp->rx_handler = handlers->rx_handler;
1798 qp->tx_handler = handlers->tx_handler;
1799 qp->event_handler = handlers->event_handler;
1800
Allen Hubbe1199aa62015-05-18 06:20:47 -04001801 dma_cap_zero(dma_mask);
1802 dma_cap_set(DMA_MEMCPY, dma_mask);
1803
Dave Jianga41ef052015-05-19 16:52:04 -04001804 if (use_dma) {
Dave Jiang569410ca2015-07-13 08:07:22 -04001805 qp->tx_dma_chan =
1806 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1807 (void *)(unsigned long)node);
1808 if (!qp->tx_dma_chan)
1809 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
1810
1811 qp->rx_dma_chan =
1812 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1813 (void *)(unsigned long)node);
1814 if (!qp->rx_dma_chan)
1815 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
Dave Jianga41ef052015-05-19 16:52:04 -04001816 } else {
Dave Jiang569410ca2015-07-13 08:07:22 -04001817 qp->tx_dma_chan = NULL;
1818 qp->rx_dma_chan = NULL;
Dave Jianga41ef052015-05-19 16:52:04 -04001819 }
Dave Jiang569410ca2015-07-13 08:07:22 -04001820
1821 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
1822 qp->tx_dma_chan ? "DMA" : "CPU");
1823
1824 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
1825 qp->rx_dma_chan ? "DMA" : "CPU");
Jon Mason282a2fe2013-02-12 09:52:50 -07001826
Jon Masonfce8a7b2012-11-16 19:27:12 -07001827 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001828 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001829 if (!entry)
1830 goto err1;
1831
Jon Mason282a2fe2013-02-12 09:52:50 -07001832 entry->qp = qp;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001833 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001834 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001835 }
Dave Jianga754a8f2016-04-08 10:49:06 -07001836 qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001837
Dave Jianga754a8f2016-04-08 10:49:06 -07001838 for (i = 0; i < qp->tx_max_entry; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001839 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001840 if (!entry)
1841 goto err2;
1842
Jon Mason282a2fe2013-02-12 09:52:50 -07001843 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001844 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001845 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001846 }
1847
Allen Hubbee26a5842015-04-09 10:33:20 -04001848 ntb_db_clear(qp->ndev, qp_bit);
1849 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001850
1851 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1852
1853 return qp;
1854
Jon Masonfce8a7b2012-11-16 19:27:12 -07001855err2:
Jon Masonf7667552013-01-19 02:02:24 -07001856 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001857 kfree(entry);
1858err1:
Dave Jianga754a8f2016-04-08 10:49:06 -07001859 qp->rx_alloc_entry = 0;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001860 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001861 kfree(entry);
Dave Jiang569410ca2015-07-13 08:07:22 -04001862 if (qp->tx_dma_chan)
1863 dma_release_channel(qp->tx_dma_chan);
1864 if (qp->rx_dma_chan)
1865 dma_release_channel(qp->rx_dma_chan);
Allen Hubbee26a5842015-04-09 10:33:20 -04001866 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001867err:
1868 return NULL;
1869}
1870EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1871
1872/**
1873 * ntb_transport_free_queue - Frees NTB transport queue
1874 * @qp: NTB queue to be freed
1875 *
1876 * Frees NTB transport queue
1877 */
1878void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1879{
Jon Mason186f27f2013-01-22 11:35:40 -07001880 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001881 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001882 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001883
1884 if (!qp)
1885 return;
1886
Allen Hubbee26a5842015-04-09 10:33:20 -04001887 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001888
Dave Jiange9021332016-02-23 09:11:36 -07001889 qp->active = false;
1890
Dave Jiang569410ca2015-07-13 08:07:22 -04001891 if (qp->tx_dma_chan) {
1892 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001893 /* Putting the dma_chan to NULL will force any new traffic to be
1894 * processed by the CPU instead of the DAM engine
1895 */
Dave Jiang569410ca2015-07-13 08:07:22 -04001896 qp->tx_dma_chan = NULL;
1897
1898 /* Try to be nice and wait for any queued DMA engine
1899 * transactions to process before smashing it with a rock
1900 */
1901 dma_sync_wait(chan, qp->last_cookie);
1902 dmaengine_terminate_all(chan);
1903 dma_release_channel(chan);
1904 }
1905
1906 if (qp->rx_dma_chan) {
1907 struct dma_chan *chan = qp->rx_dma_chan;
1908 /* Putting the dma_chan to NULL will force any new traffic to be
1909 * processed by the CPU instead of the DAM engine
1910 */
1911 qp->rx_dma_chan = NULL;
Jon Mason282a2fe2013-02-12 09:52:50 -07001912
1913 /* Try to be nice and wait for any queued DMA engine
1914 * transactions to process before smashing it with a rock
1915 */
1916 dma_sync_wait(chan, qp->last_cookie);
1917 dmaengine_terminate_all(chan);
Allen Hubbe1199aa62015-05-18 06:20:47 -04001918 dma_release_channel(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001919 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001920
Allen Hubbee26a5842015-04-09 10:33:20 -04001921 qp_bit = BIT_ULL(qp->qp_num);
1922
1923 ntb_db_set_mask(qp->ndev, qp_bit);
Dave Jiange9021332016-02-23 09:11:36 -07001924 tasklet_kill(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001925
Jon Mason282a2fe2013-02-12 09:52:50 -07001926 cancel_delayed_work_sync(&qp->link_work);
1927
Allen Hubbee26a5842015-04-09 10:33:20 -04001928 qp->cb_data = NULL;
1929 qp->rx_handler = NULL;
1930 qp->tx_handler = NULL;
1931 qp->event_handler = NULL;
1932
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001933 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001934 kfree(entry);
1935
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001936 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1937 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1938 kfree(entry);
1939 }
1940
1941 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1942 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001943 kfree(entry);
1944 }
1945
Jon Masonf7667552013-01-19 02:02:24 -07001946 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001947 kfree(entry);
1948
Allen Hubbe30a4bb12015-07-13 08:07:14 -04001949 qp->transport->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001950
1951 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1952}
1953EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1954
1955/**
1956 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1957 * @qp: NTB queue to be freed
1958 * @len: pointer to variable to write enqueued buffers length
1959 *
1960 * Dequeues unused buffers from receive queue. Should only be used during
1961 * shutdown of qp.
1962 *
1963 * RETURNS: NULL error value on error, or void* for success.
1964 */
1965void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1966{
1967 struct ntb_queue_entry *entry;
1968 void *buf;
1969
Allen Hubbee26a5842015-04-09 10:33:20 -04001970 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001971 return NULL;
1972
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001973 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001974 if (!entry)
1975 return NULL;
1976
1977 buf = entry->cb_data;
1978 *len = entry->len;
1979
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001980 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001981
1982 return buf;
1983}
1984EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1985
1986/**
1987 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1988 * @qp: NTB transport layer queue the entry is to be enqueued on
1989 * @cb: per buffer pointer for callback function to use
1990 * @data: pointer to data buffer that incoming packets will be copied into
1991 * @len: length of the data buffer
1992 *
1993 * Enqueue a new receive buffer onto the transport queue into which a NTB
1994 * payload can be received into.
1995 *
1996 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1997 */
1998int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1999 unsigned int len)
2000{
2001 struct ntb_queue_entry *entry;
2002
2003 if (!qp)
2004 return -EINVAL;
2005
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002006 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002007 if (!entry)
2008 return -ENOMEM;
2009
2010 entry->cb_data = cb;
2011 entry->buf = data;
2012 entry->len = len;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002013 entry->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07002014 entry->retries = 0;
2015 entry->errors = 0;
2016 entry->rx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002017
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002018 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
2019
Dave Jiange9021332016-02-23 09:11:36 -07002020 if (qp->active)
2021 tasklet_schedule(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002022
2023 return 0;
2024}
2025EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2026
2027/**
2028 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2029 * @qp: NTB transport layer queue the entry is to be enqueued on
2030 * @cb: per buffer pointer for callback function to use
2031 * @data: pointer to data buffer that will be sent
2032 * @len: length of the data buffer
2033 *
2034 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07002035 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07002036 * serialize access to the qp.
2037 *
2038 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2039 */
2040int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2041 unsigned int len)
2042{
2043 struct ntb_queue_entry *entry;
2044 int rc;
2045
Allen Hubbee26a5842015-04-09 10:33:20 -04002046 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002047 return -EINVAL;
2048
2049 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07002050 if (!entry) {
2051 qp->tx_err_no_buf++;
Dave Jiange74bfee2015-07-13 08:07:17 -04002052 return -EBUSY;
Jon Mason282a2fe2013-02-12 09:52:50 -07002053 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07002054
2055 entry->cb_data = cb;
2056 entry->buf = data;
2057 entry->len = len;
2058 entry->flags = 0;
Dave Jiang9cabc262016-07-20 13:14:07 -07002059 entry->errors = 0;
2060 entry->retries = 0;
2061 entry->tx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002062
2063 rc = ntb_process_tx(qp, entry);
2064 if (rc)
2065 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2066 &qp->tx_free_q);
2067
2068 return rc;
2069}
2070EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2071
2072/**
2073 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2074 * @qp: NTB transport layer queue to be enabled
2075 *
2076 * Notify NTB transport layer of client readiness to use queue
2077 */
2078void ntb_transport_link_up(struct ntb_transport_qp *qp)
2079{
2080 if (!qp)
2081 return;
2082
Allen Hubbee26a5842015-04-09 10:33:20 -04002083 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002084
Allen Hubbee26a5842015-04-09 10:33:20 -04002085 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002086 schedule_delayed_work(&qp->link_work, 0);
2087}
2088EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2089
2090/**
2091 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2092 * @qp: NTB transport layer queue to be disabled
2093 *
2094 * Notify NTB transport layer of client's desire to no longer receive data on
2095 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07002096 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07002097 */
2098void ntb_transport_link_down(struct ntb_transport_qp *qp)
2099{
Allen Hubbee26a5842015-04-09 10:33:20 -04002100 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002101
2102 if (!qp)
2103 return;
2104
Allen Hubbee26a5842015-04-09 10:33:20 -04002105 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002106
Allen Hubbee26a5842015-04-09 10:33:20 -04002107 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002108
Serge Semind67288a2017-01-11 03:13:20 +03002109 ntb_peer_spad_write(qp->ndev, PIDX, QP_LINKS, val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07002110
Allen Hubbee26a5842015-04-09 10:33:20 -04002111 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002112 ntb_send_link_down(qp);
2113 else
2114 cancel_delayed_work_sync(&qp->link_work);
2115}
2116EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2117
2118/**
2119 * ntb_transport_link_query - Query transport link state
2120 * @qp: NTB transport layer queue to be queried
2121 *
2122 * Query connectivity to the remote system of the NTB transport queue
2123 *
2124 * RETURNS: true for link up or false for link down
2125 */
2126bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2127{
Jon Mason186f27f2013-01-22 11:35:40 -07002128 if (!qp)
2129 return false;
2130
Allen Hubbee26a5842015-04-09 10:33:20 -04002131 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002132}
2133EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2134
2135/**
2136 * ntb_transport_qp_num - Query the qp number
2137 * @qp: NTB transport layer queue to be queried
2138 *
2139 * Query qp number of the NTB transport queue
2140 *
2141 * RETURNS: a zero based number specifying the qp number
2142 */
2143unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2144{
Jon Mason186f27f2013-01-22 11:35:40 -07002145 if (!qp)
2146 return 0;
2147
Jon Masonfce8a7b2012-11-16 19:27:12 -07002148 return qp->qp_num;
2149}
2150EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2151
2152/**
2153 * ntb_transport_max_size - Query the max payload size of a qp
2154 * @qp: NTB transport layer queue to be queried
2155 *
2156 * Query the maximum payload size permissible on the given qp
2157 *
2158 * RETURNS: the max payload size of a qp
2159 */
Jon Masonef114ed2013-01-19 02:02:18 -07002160unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002161{
Dave Jiang04afde42015-09-17 13:27:04 -07002162 unsigned int max_size;
Dave Jiang569410ca2015-07-13 08:07:22 -04002163 unsigned int copy_align;
Dave Jiang04afde42015-09-17 13:27:04 -07002164 struct dma_chan *rx_chan, *tx_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002165
Jon Mason186f27f2013-01-22 11:35:40 -07002166 if (!qp)
2167 return 0;
2168
Dave Jiang04afde42015-09-17 13:27:04 -07002169 rx_chan = qp->rx_dma_chan;
2170 tx_chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002171
Dave Jiang04afde42015-09-17 13:27:04 -07002172 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2173 tx_chan ? tx_chan->device->copy_align : 0);
Dave Jiang569410ca2015-07-13 08:07:22 -04002174
Jon Mason282a2fe2013-02-12 09:52:50 -07002175 /* If DMA engine usage is possible, try to find the max size for that */
Dave Jiang04afde42015-09-17 13:27:04 -07002176 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2177 max_size = round_down(max_size, 1 << copy_align);
Jon Mason282a2fe2013-02-12 09:52:50 -07002178
Dave Jiang04afde42015-09-17 13:27:04 -07002179 return max_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002180}
2181EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04002182
Dave Jiange74bfee2015-07-13 08:07:17 -04002183unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2184{
2185 unsigned int head = qp->tx_index;
2186 unsigned int tail = qp->remote_rx_info->entry;
2187
2188 return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2189}
2190EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2191
Allen Hubbee26a5842015-04-09 10:33:20 -04002192static void ntb_transport_doorbell_callback(void *data, int vector)
2193{
2194 struct ntb_transport_ctx *nt = data;
2195 struct ntb_transport_qp *qp;
2196 u64 db_bits;
2197 unsigned int qp_num;
2198
2199 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2200 ntb_db_vector_mask(nt->ndev, vector));
2201
2202 while (db_bits) {
2203 qp_num = __ffs(db_bits);
2204 qp = &nt->qp_vec[qp_num];
2205
Dave Jiange9021332016-02-23 09:11:36 -07002206 if (qp->active)
2207 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04002208
2209 db_bits &= ~BIT_ULL(qp_num);
2210 }
2211}
2212
2213static const struct ntb_ctx_ops ntb_transport_ops = {
2214 .link_event = ntb_transport_event_callback,
2215 .db_event = ntb_transport_doorbell_callback,
2216};
2217
2218static struct ntb_client ntb_transport_client = {
2219 .ops = {
2220 .probe = ntb_transport_probe,
2221 .remove = ntb_transport_free,
2222 },
2223};
2224
2225static int __init ntb_transport_init(void)
2226{
2227 int rc;
2228
Dave Jiang7eb38782015-06-15 08:21:33 -04002229 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2230
Allen Hubbee26a5842015-04-09 10:33:20 -04002231 if (debugfs_initialized())
2232 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2233
2234 rc = bus_register(&ntb_transport_bus);
2235 if (rc)
2236 goto err_bus;
2237
2238 rc = ntb_register_client(&ntb_transport_client);
2239 if (rc)
2240 goto err_client;
2241
2242 return 0;
2243
2244err_client:
2245 bus_unregister(&ntb_transport_bus);
2246err_bus:
2247 debugfs_remove_recursive(nt_debugfs_dir);
2248 return rc;
2249}
2250module_init(ntb_transport_init);
2251
2252static void __exit ntb_transport_exit(void)
2253{
Allen Hubbee26a5842015-04-09 10:33:20 -04002254 ntb_unregister_client(&ntb_transport_client);
2255 bus_unregister(&ntb_transport_bus);
Allen Hubbedd622452016-12-27 17:57:04 -05002256 debugfs_remove_recursive(nt_debugfs_dir);
Allen Hubbee26a5842015-04-09 10:33:20 -04002257}
2258module_exit(ntb_transport_exit);