blob: 2ef9d91308646010ec5d017dc93d8aa43b6ff584 [file] [log] [blame]
Jon Masonfce8a7b2012-11-16 19:27:12 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -04008 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * BSD LICENSE
15 *
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -040017 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -070018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
28 * distribution.
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
Allen Hubbee26a5842015-04-09 10:33:20 -040045 * PCIe NTB Transport Linux driver
Jon Masonfce8a7b2012-11-16 19:27:12 -070046 *
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
49 */
50#include <linux/debugfs.h>
51#include <linux/delay.h>
Jon Mason282a2fe2013-02-12 09:52:50 -070052#include <linux/dmaengine.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070053#include <linux/dma-mapping.h>
54#include <linux/errno.h>
55#include <linux/export.h>
56#include <linux/interrupt.h>
57#include <linux/module.h>
58#include <linux/pci.h>
59#include <linux/slab.h>
60#include <linux/types.h>
Dave Jiang06917f72015-05-19 16:45:46 -040061#include <linux/uaccess.h>
Allen Hubbee26a5842015-04-09 10:33:20 -040062#include "linux/ntb.h"
63#include "linux/ntb_transport.h"
Jon Masonfce8a7b2012-11-16 19:27:12 -070064
Allen Hubbee26a5842015-04-09 10:33:20 -040065#define NTB_TRANSPORT_VERSION 4
66#define NTB_TRANSPORT_VER "4"
67#define NTB_TRANSPORT_NAME "ntb_transport"
68#define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
69
70MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
71MODULE_VERSION(NTB_TRANSPORT_VER);
72MODULE_LICENSE("Dual BSD/GPL");
73MODULE_AUTHOR("Intel Corporation");
74
75static unsigned long max_mw_size;
76module_param(max_mw_size, ulong, 0644);
77MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
Jon Masonfce8a7b2012-11-16 19:27:12 -070078
Dave Jiang98914172015-06-03 11:29:38 -040079static unsigned int transport_mtu = 0x10000;
Jon Masonfce8a7b2012-11-16 19:27:12 -070080module_param(transport_mtu, uint, 0644);
81MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
82
Jon Mason948d3a62013-04-18 17:07:36 -070083static unsigned char max_num_clients;
Jon Masonfce8a7b2012-11-16 19:27:12 -070084module_param(max_num_clients, byte, 0644);
85MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
86
Jon Mason282a2fe2013-02-12 09:52:50 -070087static unsigned int copy_bytes = 1024;
88module_param(copy_bytes, uint, 0644);
89MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
90
Dave Jianga41ef052015-05-19 16:52:04 -040091static bool use_dma;
92module_param(use_dma, bool, 0644);
93MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
94
Allen Hubbee26a5842015-04-09 10:33:20 -040095static struct dentry *nt_debugfs_dir;
96
Jon Masonfce8a7b2012-11-16 19:27:12 -070097struct ntb_queue_entry {
98 /* ntb_queue list reference */
99 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400100 /* pointers to data to be transferred */
Jon Masonfce8a7b2012-11-16 19:27:12 -0700101 void *cb_data;
102 void *buf;
103 unsigned int len;
104 unsigned int flags;
Jon Mason282a2fe2013-02-12 09:52:50 -0700105
106 struct ntb_transport_qp *qp;
107 union {
108 struct ntb_payload_header __iomem *tx_hdr;
109 struct ntb_payload_header *rx_hdr;
110 };
111 unsigned int index;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700112};
113
Jon Mason793c20e2013-01-19 02:02:26 -0700114struct ntb_rx_info {
115 unsigned int entry;
116};
117
Jon Masonfce8a7b2012-11-16 19:27:12 -0700118struct ntb_transport_qp {
Allen Hubbee26a5842015-04-09 10:33:20 -0400119 struct ntb_transport_ctx *transport;
120 struct ntb_dev *ndev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700121 void *cb_data;
Dave Jiang569410ca2015-07-13 08:07:22 -0400122 struct dma_chan *tx_dma_chan;
123 struct dma_chan *rx_dma_chan;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700124
125 bool client_ready;
Allen Hubbee26a5842015-04-09 10:33:20 -0400126 bool link_is_up;
Dave Jiange9021332016-02-23 09:11:36 -0700127 bool active;
Allen Hubbee26a5842015-04-09 10:33:20 -0400128
Jon Masonfce8a7b2012-11-16 19:27:12 -0700129 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400130 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700131
Jon Mason74465642013-01-21 15:28:52 -0700132 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -0700133 struct ntb_rx_info *remote_rx_info;
134
Jon Mason53ca4fe2013-11-26 11:21:50 -0700135 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
136 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700137 struct list_head tx_free_q;
138 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700139 void __iomem *tx_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700140 dma_addr_t tx_mw_phys;
Jon Mason793c20e2013-01-19 02:02:26 -0700141 unsigned int tx_index;
142 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700143 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700144
Jon Mason53ca4fe2013-11-26 11:21:50 -0700145 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
146 void *data, int len);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400147 struct list_head rx_post_q;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700148 struct list_head rx_pend_q;
149 struct list_head rx_free_q;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400150 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
151 spinlock_t ntb_rx_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700152 void *rx_buff;
153 unsigned int rx_index;
154 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700155 unsigned int rx_max_frame;
Jon Mason282a2fe2013-02-12 09:52:50 -0700156 dma_cookie_t last_cookie;
Allen Hubbee26a5842015-04-09 10:33:20 -0400157 struct tasklet_struct rxc_db_work;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700158
Jon Mason53ca4fe2013-11-26 11:21:50 -0700159 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700160 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700161 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700162
163 struct dentry *debugfs_dir;
164 struct dentry *debugfs_stats;
165
166 /* Stats */
167 u64 rx_bytes;
168 u64 rx_pkts;
169 u64 rx_ring_empty;
170 u64 rx_err_no_buf;
171 u64 rx_err_oflow;
172 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700173 u64 rx_memcpy;
174 u64 rx_async;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700175 u64 dma_rx_prep_err;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700176 u64 tx_bytes;
177 u64 tx_pkts;
178 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700179 u64 tx_err_no_buf;
180 u64 tx_memcpy;
181 u64 tx_async;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700182 u64 dma_tx_prep_err;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700183};
184
185struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400186 phys_addr_t phys_addr;
187 resource_size_t phys_size;
188 resource_size_t xlat_align;
189 resource_size_t xlat_align_size;
190 void __iomem *vbase;
191 size_t xlat_size;
192 size_t buff_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700193 void *virt_addr;
194 dma_addr_t dma_addr;
195};
196
197struct ntb_transport_client_dev {
198 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400199 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700200 struct device dev;
201};
202
Allen Hubbee26a5842015-04-09 10:33:20 -0400203struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700204 struct list_head entry;
205 struct list_head client_devs;
206
Allen Hubbee26a5842015-04-09 10:33:20 -0400207 struct ntb_dev *ndev;
208
209 struct ntb_transport_mw *mw_vec;
210 struct ntb_transport_qp *qp_vec;
211 unsigned int mw_count;
212 unsigned int qp_count;
213 u64 qp_bitmap;
214 u64 qp_bitmap_free;
215
216 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700217 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700218 struct work_struct link_cleanup;
Dave Jiangc8650fd2015-07-13 08:07:09 -0400219
220 struct dentry *debugfs_node_dir;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700221};
222
223enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400224 DESC_DONE_FLAG = BIT(0),
225 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700226};
227
228struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700229 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700230 unsigned int len;
231 unsigned int flags;
232};
233
234enum {
235 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700236 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700237 NUM_QPS,
238 NUM_MWS,
239 MW0_SZ_HIGH,
240 MW0_SZ_LOW,
241 MW1_SZ_HIGH,
242 MW1_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700243 MAX_SPAD,
244};
245
Allen Hubbee26a5842015-04-09 10:33:20 -0400246#define dev_client_dev(__dev) \
247 container_of((__dev), struct ntb_transport_client_dev, dev)
248
249#define drv_client(__drv) \
250 container_of((__drv), struct ntb_transport_client, driver)
251
252#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700253#define NTB_QP_DEF_NUM_ENTRIES 100
254#define NTB_LINK_DOWN_TIMEOUT 10
Dave Jiang8c874cc2016-01-08 09:51:05 -0700255#define DMA_RETRIES 20
256#define DMA_OUT_RESOURCE_TO 50
Jon Masonfce8a7b2012-11-16 19:27:12 -0700257
Allen Hubbee26a5842015-04-09 10:33:20 -0400258static void ntb_transport_rxc_db(unsigned long data);
259static const struct ntb_ctx_ops ntb_transport_ops;
260static struct ntb_client ntb_transport_client;
261
262static int ntb_transport_bus_match(struct device *dev,
263 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700264{
265 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
266}
267
Allen Hubbee26a5842015-04-09 10:33:20 -0400268static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700269{
Allen Hubbee26a5842015-04-09 10:33:20 -0400270 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700271 int rc = -EINVAL;
272
273 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400274
275 client = drv_client(dev->driver);
276 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700277 if (rc)
278 put_device(dev);
279
280 return rc;
281}
282
Allen Hubbee26a5842015-04-09 10:33:20 -0400283static int ntb_transport_bus_remove(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700284{
Allen Hubbee26a5842015-04-09 10:33:20 -0400285 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700286
Allen Hubbee26a5842015-04-09 10:33:20 -0400287 client = drv_client(dev->driver);
288 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700289
290 put_device(dev);
291
292 return 0;
293}
294
Allen Hubbee26a5842015-04-09 10:33:20 -0400295static struct bus_type ntb_transport_bus = {
296 .name = "ntb_transport",
297 .match = ntb_transport_bus_match,
298 .probe = ntb_transport_bus_probe,
299 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700300};
301
302static LIST_HEAD(ntb_transport_list);
303
Allen Hubbee26a5842015-04-09 10:33:20 -0400304static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700305{
Dave Jiang31510002015-07-13 08:07:19 -0400306 list_add_tail(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700307 return 0;
308}
309
Allen Hubbee26a5842015-04-09 10:33:20 -0400310static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700311{
312 struct ntb_transport_client_dev *client_dev, *cd;
313
314 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
315 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
316 dev_name(&client_dev->dev));
317 list_del(&client_dev->entry);
318 device_unregister(&client_dev->dev);
319 }
320
321 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700322}
323
Allen Hubbee26a5842015-04-09 10:33:20 -0400324static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700325{
326 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700327
Allen Hubbee26a5842015-04-09 10:33:20 -0400328 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700329 kfree(client_dev);
330}
331
332/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400333 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700334 * @device_name: Name of NTB client device
335 *
336 * Unregister an NTB client device with the NTB transport layer
337 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400338void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700339{
340 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400341 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700342
343 list_for_each_entry(nt, &ntb_transport_list, entry)
344 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
345 if (!strncmp(dev_name(&client->dev), device_name,
346 strlen(device_name))) {
347 list_del(&client->entry);
348 device_unregister(&client->dev);
349 }
350}
Allen Hubbee26a5842015-04-09 10:33:20 -0400351EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700352
353/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400354 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700355 * @device_name: Name of NTB client device
356 *
357 * Register an NTB client device with the NTB transport layer
358 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400359int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700360{
361 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400362 struct ntb_transport_ctx *nt;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400363 int node;
Jon Mason8b19d452013-04-26 14:51:57 -0700364 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700365
Jon Mason8222b402013-01-19 02:02:16 -0700366 if (list_empty(&ntb_transport_list))
367 return -ENODEV;
368
Jon Masonfce8a7b2012-11-16 19:27:12 -0700369 list_for_each_entry(nt, &ntb_transport_list, entry) {
370 struct device *dev;
371
Allen Hubbe1199aa62015-05-18 06:20:47 -0400372 node = dev_to_node(&nt->ndev->dev);
373
374 client_dev = kzalloc_node(sizeof(*client_dev),
375 GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700376 if (!client_dev) {
377 rc = -ENOMEM;
378 goto err;
379 }
380
381 dev = &client_dev->dev;
382
383 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700384 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400385 dev->bus = &ntb_transport_bus;
386 dev->release = ntb_transport_client_release;
387 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700388
389 rc = device_register(dev);
390 if (rc) {
391 kfree(client_dev);
392 goto err;
393 }
394
395 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700396 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700397 }
398
399 return 0;
400
401err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400402 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700403
404 return rc;
405}
Allen Hubbee26a5842015-04-09 10:33:20 -0400406EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700407
408/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400409 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700410 * @drv: NTB client driver to be registered
411 *
412 * Register an NTB client driver with the NTB transport layer
413 *
414 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
415 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400416int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700417{
Allen Hubbee26a5842015-04-09 10:33:20 -0400418 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700419
Jon Mason8222b402013-01-19 02:02:16 -0700420 if (list_empty(&ntb_transport_list))
421 return -ENODEV;
422
Jon Masonfce8a7b2012-11-16 19:27:12 -0700423 return driver_register(&drv->driver);
424}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400425EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700426
427/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400428 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700429 * @drv: NTB client driver to be unregistered
430 *
431 * Unregister an NTB client driver with the NTB transport layer
432 *
433 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
434 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400435void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700436{
437 driver_unregister(&drv->driver);
438}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400439EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700440
Jon Masonfce8a7b2012-11-16 19:27:12 -0700441static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
442 loff_t *offp)
443{
444 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700445 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700446 ssize_t ret, out_offset, out_count;
447
Dave Jiang260bee92015-07-13 08:07:11 -0400448 qp = filp->private_data;
449
450 if (!qp || !qp->link_is_up)
451 return 0;
452
Jon Mason282a2fe2013-02-12 09:52:50 -0700453 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700454
455 buf = kmalloc(out_count, GFP_KERNEL);
456 if (!buf)
457 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700458
Jon Masonfce8a7b2012-11-16 19:27:12 -0700459 out_offset = 0;
460 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400461 "\nNTB QP stats:\n\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700462 out_offset += snprintf(buf + out_offset, out_count - out_offset,
463 "rx_bytes - \t%llu\n", qp->rx_bytes);
464 out_offset += snprintf(buf + out_offset, out_count - out_offset,
465 "rx_pkts - \t%llu\n", qp->rx_pkts);
466 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700467 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
468 out_offset += snprintf(buf + out_offset, out_count - out_offset,
469 "rx_async - \t%llu\n", qp->rx_async);
470 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700471 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
472 out_offset += snprintf(buf + out_offset, out_count - out_offset,
473 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
474 out_offset += snprintf(buf + out_offset, out_count - out_offset,
475 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
476 out_offset += snprintf(buf + out_offset, out_count - out_offset,
477 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
478 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400479 "rx_buff - \t0x%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700480 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700481 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700482 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400483 "rx_max_entry - \t%u\n\n", qp->rx_max_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700484
485 out_offset += snprintf(buf + out_offset, out_count - out_offset,
486 "tx_bytes - \t%llu\n", qp->tx_bytes);
487 out_offset += snprintf(buf + out_offset, out_count - out_offset,
488 "tx_pkts - \t%llu\n", qp->tx_pkts);
489 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700490 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
491 out_offset += snprintf(buf + out_offset, out_count - out_offset,
492 "tx_async - \t%llu\n", qp->tx_async);
493 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700494 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
495 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700496 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
497 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400498 "tx_mw - \t0x%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700499 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400500 "tx_index (H) - \t%u\n", qp->tx_index);
501 out_offset += snprintf(buf + out_offset, out_count - out_offset,
502 "RRI (T) - \t%u\n",
503 qp->remote_rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700504 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700505 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400506 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiange74bfee2015-07-13 08:07:17 -0400507 "free tx - \t%u\n",
508 ntb_transport_tx_free_entry(qp));
Dave Jiang8c874cc2016-01-08 09:51:05 -0700509 out_offset += snprintf(buf + out_offset, out_count - out_offset,
510 "DMA tx prep err - \t%llu\n",
511 qp->dma_tx_prep_err);
512 out_offset += snprintf(buf + out_offset, out_count - out_offset,
513 "DMA rx prep err - \t%llu\n",
514 qp->dma_rx_prep_err);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700515
516 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400517 "\n");
518 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiang569410ca2015-07-13 08:07:22 -0400519 "Using TX DMA - \t%s\n",
520 qp->tx_dma_chan ? "Yes" : "No");
521 out_offset += snprintf(buf + out_offset, out_count - out_offset,
522 "Using RX DMA - \t%s\n",
523 qp->rx_dma_chan ? "Yes" : "No");
Dave Jiangd98ef992015-07-13 08:07:20 -0400524 out_offset += snprintf(buf + out_offset, out_count - out_offset,
525 "QP Link - \t%s\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400526 qp->link_is_up ? "Up" : "Down");
Dave Jiangd98ef992015-07-13 08:07:20 -0400527 out_offset += snprintf(buf + out_offset, out_count - out_offset,
528 "\n");
529
Jon Masond7237e22013-01-19 02:02:25 -0700530 if (out_offset > out_count)
531 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700532
533 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700534 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700535 return ret;
536}
537
538static const struct file_operations ntb_qp_debugfs_stats = {
539 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700540 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700541 .read = debugfs_read,
542};
543
544static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
545 struct list_head *list)
546{
547 unsigned long flags;
548
549 spin_lock_irqsave(lock, flags);
550 list_add_tail(entry, list);
551 spin_unlock_irqrestore(lock, flags);
552}
553
554static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700555 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700556{
557 struct ntb_queue_entry *entry;
558 unsigned long flags;
559
560 spin_lock_irqsave(lock, flags);
561 if (list_empty(list)) {
562 entry = NULL;
563 goto out;
564 }
565 entry = list_first_entry(list, struct ntb_queue_entry, entry);
566 list_del(&entry->entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400567
Jon Masonfce8a7b2012-11-16 19:27:12 -0700568out:
569 spin_unlock_irqrestore(lock, flags);
570
571 return entry;
572}
573
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400574static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
575 struct list_head *list,
576 struct list_head *to_list)
577{
578 struct ntb_queue_entry *entry;
579 unsigned long flags;
580
581 spin_lock_irqsave(lock, flags);
582
583 if (list_empty(list)) {
584 entry = NULL;
585 } else {
586 entry = list_first_entry(list, struct ntb_queue_entry, entry);
587 list_move_tail(&entry->entry, to_list);
588 }
589
590 spin_unlock_irqrestore(lock, flags);
591
592 return entry;
593}
594
Allen Hubbee26a5842015-04-09 10:33:20 -0400595static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
596 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700597{
Allen Hubbee26a5842015-04-09 10:33:20 -0400598 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
599 struct ntb_transport_mw *mw;
Jon Masonef114ed2013-01-19 02:02:18 -0700600 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400601 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700602 unsigned int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700603
Allen Hubbee26a5842015-04-09 10:33:20 -0400604 mw_count = nt->mw_count;
605 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700606
Allen Hubbee26a5842015-04-09 10:33:20 -0400607 mw_num = QP_TO_MW(nt, qp_num);
608 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700609
Allen Hubbee26a5842015-04-09 10:33:20 -0400610 if (!mw->virt_addr)
611 return -ENOMEM;
612
613 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
614 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700615 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400616 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700617
Allen Hubbee26a5842015-04-09 10:33:20 -0400618 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -0400619 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
Jon Mason793c20e2013-01-19 02:02:26 -0700620 rx_size -= sizeof(struct ntb_rx_info);
621
Jon Mason282a2fe2013-02-12 09:52:50 -0700622 qp->remote_rx_info = qp->rx_buff + rx_size;
623
Jon Masonc9d534c2013-02-01 15:45:16 -0700624 /* Due to housekeeping, there must be atleast 2 buffs */
625 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700626 qp->rx_max_entry = rx_size / qp->rx_max_frame;
627 qp->rx_index = 0;
628
Jon Masonc9d534c2013-02-01 15:45:16 -0700629 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700630
Jon Masonef114ed2013-01-19 02:02:18 -0700631 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700632 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400633 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
634 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700635 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700636 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700637
638 qp->rx_pkts = 0;
639 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700640 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400641
642 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700643}
644
Allen Hubbee26a5842015-04-09 10:33:20 -0400645static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700646{
Allen Hubbee26a5842015-04-09 10:33:20 -0400647 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
648 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700649
650 if (!mw->virt_addr)
651 return;
652
Allen Hubbee26a5842015-04-09 10:33:20 -0400653 ntb_mw_clear_trans(nt->ndev, num_mw);
654 dma_free_coherent(&pdev->dev, mw->buff_size,
655 mw->virt_addr, mw->dma_addr);
656 mw->xlat_size = 0;
657 mw->buff_size = 0;
Jon Mason113fc502013-01-30 11:40:52 -0700658 mw->virt_addr = NULL;
659}
660
Allen Hubbee26a5842015-04-09 10:33:20 -0400661static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400662 resource_size_t size)
Jon Masonb77b2632013-02-01 15:25:37 -0700663{
Allen Hubbee26a5842015-04-09 10:33:20 -0400664 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
665 struct pci_dev *pdev = nt->ndev->pdev;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400666 size_t xlat_size, buff_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400667 int rc;
668
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400669 if (!size)
670 return -EINVAL;
671
Allen Hubbee26a5842015-04-09 10:33:20 -0400672 xlat_size = round_up(size, mw->xlat_align_size);
673 buff_size = round_up(size, mw->xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700674
675 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400676 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700677 return 0;
678
Allen Hubbee26a5842015-04-09 10:33:20 -0400679 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700680 ntb_free_mw(nt, num_mw);
681
Allen Hubbee26a5842015-04-09 10:33:20 -0400682 /* Alloc memory for receiving data. Must be aligned */
683 mw->xlat_size = xlat_size;
684 mw->buff_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700685
Allen Hubbee26a5842015-04-09 10:33:20 -0400686 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
687 &mw->dma_addr, GFP_KERNEL);
Jon Masonb77b2632013-02-01 15:25:37 -0700688 if (!mw->virt_addr) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400689 mw->xlat_size = 0;
690 mw->buff_size = 0;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400691 dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400692 buff_size);
Jon Masonb77b2632013-02-01 15:25:37 -0700693 return -ENOMEM;
694 }
695
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700696 /*
697 * we must ensure that the memory address allocated is BAR size
698 * aligned in order for the XLAT register to take the value. This
699 * is a requirement of the hardware. It is recommended to setup CMA
700 * for BAR sizes equal or greater than 4MB.
701 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400702 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
703 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700704 &mw->dma_addr);
705 ntb_free_mw(nt, num_mw);
706 return -ENOMEM;
707 }
708
Jon Masonb77b2632013-02-01 15:25:37 -0700709 /* Notify HW the memory location of the receive buffer */
Allen Hubbee26a5842015-04-09 10:33:20 -0400710 rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
711 if (rc) {
712 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
713 ntb_free_mw(nt, num_mw);
714 return -EIO;
715 }
Jon Masonb77b2632013-02-01 15:25:37 -0700716
717 return 0;
718}
719
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400720static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
721{
722 qp->link_is_up = false;
Dave Jiange9021332016-02-23 09:11:36 -0700723 qp->active = false;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400724
725 qp->tx_index = 0;
726 qp->rx_index = 0;
727 qp->rx_bytes = 0;
728 qp->rx_pkts = 0;
729 qp->rx_ring_empty = 0;
730 qp->rx_err_no_buf = 0;
731 qp->rx_err_oflow = 0;
732 qp->rx_err_ver = 0;
733 qp->rx_memcpy = 0;
734 qp->rx_async = 0;
735 qp->tx_bytes = 0;
736 qp->tx_pkts = 0;
737 qp->tx_ring_full = 0;
738 qp->tx_err_no_buf = 0;
739 qp->tx_memcpy = 0;
740 qp->tx_async = 0;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700741 qp->dma_tx_prep_err = 0;
742 qp->dma_rx_prep_err = 0;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400743}
744
Jon Masonfca4d512013-09-09 13:39:55 -0700745static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700746{
Allen Hubbee26a5842015-04-09 10:33:20 -0400747 struct ntb_transport_ctx *nt = qp->transport;
748 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700749
Allen Hubbee22e0b92015-05-12 06:55:44 -0400750 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400751
752 cancel_delayed_work_sync(&qp->link_work);
753 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400754
755 if (qp->event_handler)
756 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700757}
758
759static void ntb_qp_link_cleanup_work(struct work_struct *work)
760{
761 struct ntb_transport_qp *qp = container_of(work,
762 struct ntb_transport_qp,
763 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400764 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700765
766 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700767
Allen Hubbee26a5842015-04-09 10:33:20 -0400768 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700769 schedule_delayed_work(&qp->link_work,
770 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
771}
772
Jon Mason7b4f2d32013-01-19 02:02:19 -0700773static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700774{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700775 schedule_work(&qp->link_cleanup);
776}
777
Allen Hubbee26a5842015-04-09 10:33:20 -0400778static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700779{
Allen Hubbee26a5842015-04-09 10:33:20 -0400780 struct ntb_transport_qp *qp;
781 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700782 int i;
783
Allen Hubbee26a5842015-04-09 10:33:20 -0400784 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700785
Allen Hubbee26a5842015-04-09 10:33:20 -0400786 /* Pass along the info to any clients */
787 for (i = 0; i < nt->qp_count; i++)
788 if (qp_bitmap_alloc & BIT_ULL(i)) {
789 qp = &nt->qp_vec[i];
790 ntb_qp_link_cleanup(qp);
791 cancel_work_sync(&qp->link_cleanup);
792 cancel_delayed_work_sync(&qp->link_work);
793 }
794
795 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700796 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700797
Jon Masonfce8a7b2012-11-16 19:27:12 -0700798 /* The scratchpad registers keep the values if the remote side
799 * goes down, blast them now to give them a sane value the next
800 * time they are accessed
801 */
802 for (i = 0; i < MAX_SPAD; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400803 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700804}
805
Jon Masonfca4d512013-09-09 13:39:55 -0700806static void ntb_transport_link_cleanup_work(struct work_struct *work)
807{
Allen Hubbee26a5842015-04-09 10:33:20 -0400808 struct ntb_transport_ctx *nt =
809 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700810
811 ntb_transport_link_cleanup(nt);
812}
813
Allen Hubbee26a5842015-04-09 10:33:20 -0400814static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700815{
Allen Hubbee26a5842015-04-09 10:33:20 -0400816 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700817
Allen Hubbee26a5842015-04-09 10:33:20 -0400818 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700819 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400820 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700821 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700822}
823
824static void ntb_transport_link_work(struct work_struct *work)
825{
Allen Hubbee26a5842015-04-09 10:33:20 -0400826 struct ntb_transport_ctx *nt =
827 container_of(work, struct ntb_transport_ctx, link_work.work);
828 struct ntb_dev *ndev = nt->ndev;
829 struct pci_dev *pdev = ndev->pdev;
830 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700831 u32 val;
Dave Jiang84f76682016-02-29 09:35:26 -0700832 int rc = 0, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700833
Jon Mason113fc502013-01-30 11:40:52 -0700834 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400835 for (i = 0; i < nt->mw_count; i++) {
836 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700837
Allen Hubbee26a5842015-04-09 10:33:20 -0400838 if (max_mw_size && size > max_mw_size)
839 size = max_mw_size;
840
841 spad = MW0_SZ_HIGH + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200842 ntb_peer_spad_write(ndev, spad, upper_32_bits(size));
Allen Hubbee26a5842015-04-09 10:33:20 -0400843
844 spad = MW0_SZ_LOW + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200845 ntb_peer_spad_write(ndev, spad, lower_32_bits(size));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700846 }
847
Allen Hubbee26a5842015-04-09 10:33:20 -0400848 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700849
Allen Hubbee26a5842015-04-09 10:33:20 -0400850 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700851
Allen Hubbee26a5842015-04-09 10:33:20 -0400852 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700853
854 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400855 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400856 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700857 if (val != NTB_TRANSPORT_VERSION)
858 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700859
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400860 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700861 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400862 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700863 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700864
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400865 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700866 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400867 if (val != nt->mw_count)
868 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700869
Allen Hubbee26a5842015-04-09 10:33:20 -0400870 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700871 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700872
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400873 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400874 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700875
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400876 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700877 val64 |= val;
878
Allen Hubbee26a5842015-04-09 10:33:20 -0400879 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700880
881 rc = ntb_set_mw(nt, i, val64);
882 if (rc)
883 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700884 }
885
Allen Hubbee26a5842015-04-09 10:33:20 -0400886 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700887
Allen Hubbee26a5842015-04-09 10:33:20 -0400888 for (i = 0; i < nt->qp_count; i++) {
889 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700890
891 ntb_transport_setup_qp_mw(nt, i);
892
Allen Hubbee26a5842015-04-09 10:33:20 -0400893 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700894 schedule_delayed_work(&qp->link_work, 0);
895 }
896
897 return;
898
Jon Mason113fc502013-01-30 11:40:52 -0700899out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400900 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700901 ntb_free_mw(nt, i);
Dave Jiang84f76682016-02-29 09:35:26 -0700902
903 /* if there's an actual failure, we should just bail */
904 if (rc < 0) {
905 ntb_link_disable(ndev);
906 return;
907 }
908
Jon Masonfce8a7b2012-11-16 19:27:12 -0700909out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400910 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700911 schedule_delayed_work(&nt->link_work,
912 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
913}
914
915static void ntb_qp_link_work(struct work_struct *work)
916{
917 struct ntb_transport_qp *qp = container_of(work,
918 struct ntb_transport_qp,
919 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400920 struct pci_dev *pdev = qp->ndev->pdev;
921 struct ntb_transport_ctx *nt = qp->transport;
922 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700923
Allen Hubbee26a5842015-04-09 10:33:20 -0400924 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700925
Allen Hubbee26a5842015-04-09 10:33:20 -0400926 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700927
Allen Hubbee26a5842015-04-09 10:33:20 -0400928 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700929
930 /* query remote spad for qp ready bits */
Allen Hubbee26a5842015-04-09 10:33:20 -0400931 ntb_peer_spad_read(nt->ndev, QP_LINKS);
Allen Hubbe28762282015-05-11 10:08:26 -0400932 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700933
934 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400935 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700936 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400937 qp->link_is_up = true;
Dave Jiange9021332016-02-23 09:11:36 -0700938 qp->active = true;
Allen Hubbee26a5842015-04-09 10:33:20 -0400939
Jon Masonfce8a7b2012-11-16 19:27:12 -0700940 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400941 qp->event_handler(qp->cb_data, qp->link_is_up);
Allen Hubbe8b5a22d2015-07-13 08:07:12 -0400942
Dave Jiange9021332016-02-23 09:11:36 -0700943 if (qp->active)
944 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400945 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700946 schedule_delayed_work(&qp->link_work,
947 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
948}
949
Allen Hubbee26a5842015-04-09 10:33:20 -0400950static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700951 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700952{
953 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400954 phys_addr_t mw_base;
955 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700956 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400957 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700958 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700959
Allen Hubbee26a5842015-04-09 10:33:20 -0400960 mw_count = nt->mw_count;
961 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700962
Allen Hubbee26a5842015-04-09 10:33:20 -0400963 mw_num = QP_TO_MW(nt, qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400964
965 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700966 qp->qp_num = qp_num;
967 qp->transport = nt;
968 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400969 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700970 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400971 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700972
Allen Hubbee26a5842015-04-09 10:33:20 -0400973 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
974 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -0700975 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400976 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -0700977
Allen Hubbee26a5842015-04-09 10:33:20 -0400978 mw_base = nt->mw_vec[mw_num].phys_addr;
979 mw_size = nt->mw_vec[mw_num].phys_size;
980
981 tx_size = (unsigned int)mw_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -0400982 qp_offset = tx_size * (qp_num / mw_count);
Allen Hubbee26a5842015-04-09 10:33:20 -0400983
984 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -0700985 if (!qp->tx_mw)
986 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -0700987
Allen Hubbee26a5842015-04-09 10:33:20 -0400988 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -0700989 if (!qp->tx_mw_phys)
990 return -EINVAL;
991
992 tx_size -= sizeof(struct ntb_rx_info);
993 qp->rx_info = qp->tx_mw + tx_size;
994
Jon Masonc9d534c2013-02-01 15:45:16 -0700995 /* Due to housekeeping, there must be atleast 2 buffs */
996 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700997 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -0700998
Dave Jiangc8650fd2015-07-13 08:07:09 -0400999 if (nt->debugfs_node_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001000 char debugfs_name[4];
1001
1002 snprintf(debugfs_name, 4, "qp%d", qp_num);
1003 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Dave Jiangc8650fd2015-07-13 08:07:09 -04001004 nt->debugfs_node_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001005
1006 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1007 qp->debugfs_dir, qp,
1008 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -04001009 } else {
1010 qp->debugfs_dir = NULL;
1011 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001012 }
1013
1014 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001015 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001016
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001017 spin_lock_init(&qp->ntb_rx_q_lock);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001018 spin_lock_init(&qp->ntb_tx_free_q_lock);
1019
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001020 INIT_LIST_HEAD(&qp->rx_post_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001021 INIT_LIST_HEAD(&qp->rx_pend_q);
1022 INIT_LIST_HEAD(&qp->rx_free_q);
1023 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001024
Allen Hubbee26a5842015-04-09 10:33:20 -04001025 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1026 (unsigned long)qp);
1027
Jon Mason282a2fe2013-02-12 09:52:50 -07001028 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001029}
1030
Allen Hubbee26a5842015-04-09 10:33:20 -04001031static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001032{
Allen Hubbee26a5842015-04-09 10:33:20 -04001033 struct ntb_transport_ctx *nt;
1034 struct ntb_transport_mw *mw;
1035 unsigned int mw_count, qp_count;
1036 u64 qp_bitmap;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001037 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001038 int rc, i;
1039
Allen Hubbee26a5842015-04-09 10:33:20 -04001040 if (ntb_db_is_unsafe(ndev))
1041 dev_dbg(&ndev->dev,
1042 "doorbell is unsafe, proceed anyway...\n");
1043 if (ntb_spad_is_unsafe(ndev))
1044 dev_dbg(&ndev->dev,
1045 "scratchpad is unsafe, proceed anyway...\n");
1046
Allen Hubbe1199aa62015-05-18 06:20:47 -04001047 node = dev_to_node(&ndev->dev);
1048
1049 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001050 if (!nt)
1051 return -ENOMEM;
1052
Allen Hubbee26a5842015-04-09 10:33:20 -04001053 nt->ndev = ndev;
1054
1055 mw_count = ntb_mw_count(ndev);
1056
1057 nt->mw_count = mw_count;
1058
Allen Hubbe1199aa62015-05-18 06:20:47 -04001059 nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1060 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001061 if (!nt->mw_vec) {
1062 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001063 goto err;
1064 }
1065
Allen Hubbee26a5842015-04-09 10:33:20 -04001066 for (i = 0; i < mw_count; i++) {
1067 mw = &nt->mw_vec[i];
1068
1069 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
1070 &mw->xlat_align, &mw->xlat_align_size);
1071 if (rc)
1072 goto err1;
1073
Dave Jiang06917f72015-05-19 16:45:46 -04001074 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001075 if (!mw->vbase) {
1076 rc = -ENOMEM;
1077 goto err1;
1078 }
1079
1080 mw->buff_size = 0;
1081 mw->xlat_size = 0;
1082 mw->virt_addr = NULL;
1083 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -07001084 }
1085
Allen Hubbee26a5842015-04-09 10:33:20 -04001086 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001087
Allen Hubbee26a5842015-04-09 10:33:20 -04001088 qp_count = ilog2(qp_bitmap);
1089 if (max_num_clients && max_num_clients < qp_count)
1090 qp_count = max_num_clients;
1091 else if (mw_count < qp_count)
1092 qp_count = mw_count;
1093
1094 qp_bitmap &= BIT_ULL(qp_count) - 1;
1095
1096 nt->qp_count = qp_count;
1097 nt->qp_bitmap = qp_bitmap;
1098 nt->qp_bitmap_free = qp_bitmap;
1099
Allen Hubbe1199aa62015-05-18 06:20:47 -04001100 nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1101 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001102 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001103 rc = -ENOMEM;
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301104 goto err1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001105 }
1106
Dave Jiangc8650fd2015-07-13 08:07:09 -04001107 if (nt_debugfs_dir) {
1108 nt->debugfs_node_dir =
1109 debugfs_create_dir(pci_name(ndev->pdev),
1110 nt_debugfs_dir);
1111 }
1112
Allen Hubbee26a5842015-04-09 10:33:20 -04001113 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001114 rc = ntb_transport_init_queue(nt, i);
1115 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301116 goto err2;
Jon Mason282a2fe2013-02-12 09:52:50 -07001117 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001118
1119 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001120 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001121
Allen Hubbee26a5842015-04-09 10:33:20 -04001122 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001123 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301124 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001125
1126 INIT_LIST_HEAD(&nt->client_devs);
1127 rc = ntb_bus_init(nt);
1128 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301129 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001130
Allen Hubbee26a5842015-04-09 10:33:20 -04001131 nt->link_is_up = false;
1132 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1133 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001134
1135 return 0;
1136
Jon Mason948d3a62013-04-18 17:07:36 -07001137err3:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301138 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001139err2:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301140 kfree(nt->qp_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001141err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001142 while (i--) {
1143 mw = &nt->mw_vec[i];
1144 iounmap(mw->vbase);
1145 }
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301146 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001147err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001148 kfree(nt);
1149 return rc;
1150}
1151
Allen Hubbee26a5842015-04-09 10:33:20 -04001152static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001153{
Allen Hubbee26a5842015-04-09 10:33:20 -04001154 struct ntb_transport_ctx *nt = ndev->ctx;
1155 struct ntb_transport_qp *qp;
1156 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001157 int i;
1158
Jon Masonfca4d512013-09-09 13:39:55 -07001159 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001160 cancel_work_sync(&nt->link_cleanup);
1161 cancel_delayed_work_sync(&nt->link_work);
1162
1163 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001164
1165 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001166 for (i = 0; i < nt->qp_count; i++) {
1167 qp = &nt->qp_vec[i];
1168 if (qp_bitmap_alloc & BIT_ULL(i))
1169 ntb_transport_free_queue(qp);
1170 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001171 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001172
Allen Hubbee26a5842015-04-09 10:33:20 -04001173 ntb_link_disable(ndev);
1174 ntb_clear_ctx(ndev);
1175
Jon Masonfce8a7b2012-11-16 19:27:12 -07001176 ntb_bus_remove(nt);
1177
Allen Hubbee26a5842015-04-09 10:33:20 -04001178 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001179 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001180 iounmap(nt->mw_vec[i].vbase);
1181 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001182
Allen Hubbee26a5842015-04-09 10:33:20 -04001183 kfree(nt->qp_vec);
1184 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001185 kfree(nt);
1186}
1187
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001188static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1189{
1190 struct ntb_queue_entry *entry;
1191 void *cb_data;
1192 unsigned int len;
1193 unsigned long irqflags;
1194
1195 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1196
1197 while (!list_empty(&qp->rx_post_q)) {
1198 entry = list_first_entry(&qp->rx_post_q,
1199 struct ntb_queue_entry, entry);
1200 if (!(entry->flags & DESC_DONE_FLAG))
1201 break;
1202
1203 entry->rx_hdr->flags = 0;
1204 iowrite32(entry->index, &qp->rx_info->entry);
1205
1206 cb_data = entry->cb_data;
1207 len = entry->len;
1208
1209 list_move_tail(&entry->entry, &qp->rx_free_q);
1210
1211 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1212
1213 if (qp->rx_handler && qp->client_ready)
1214 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1215
1216 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1217 }
1218
1219 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1220}
1221
Jon Mason282a2fe2013-02-12 09:52:50 -07001222static void ntb_rx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001223{
Jon Mason282a2fe2013-02-12 09:52:50 -07001224 struct ntb_queue_entry *entry = data;
Jon Mason448c6fb2013-01-19 02:02:27 -07001225
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001226 entry->flags |= DESC_DONE_FLAG;
Jon Mason282a2fe2013-02-12 09:52:50 -07001227
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001228 ntb_complete_rxc(entry->qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001229}
1230
Jon Mason282a2fe2013-02-12 09:52:50 -07001231static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1232{
1233 void *buf = entry->buf;
1234 size_t len = entry->len;
1235
1236 memcpy(buf, offset, len);
1237
Allen Hubbee26a5842015-04-09 10:33:20 -04001238 /* Ensure that the data is fully copied out before clearing the flag */
1239 wmb();
1240
Jon Mason282a2fe2013-02-12 09:52:50 -07001241 ntb_rx_copy_callback(entry);
1242}
1243
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001244static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
Jon Mason282a2fe2013-02-12 09:52:50 -07001245{
1246 struct dma_async_tx_descriptor *txd;
1247 struct ntb_transport_qp *qp = entry->qp;
Dave Jiang569410ca2015-07-13 08:07:22 -04001248 struct dma_chan *chan = qp->rx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001249 struct dma_device *device;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001250 size_t pay_off, buff_off, len;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001251 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001252 dma_cookie_t cookie;
1253 void *buf = entry->buf;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001254 int retries = 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001255
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001256 len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001257
1258 if (!chan)
1259 goto err;
1260
Jon Mason53ca4fe2013-11-26 11:21:50 -07001261 if (len < copy_bytes)
Allen Hubbe905921e2015-07-13 08:07:21 -04001262 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001263
1264 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001265 pay_off = (size_t)offset & ~PAGE_MASK;
1266 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001267
1268 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Allen Hubbe905921e2015-07-13 08:07:21 -04001269 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001270
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001271 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1272 if (!unmap)
Allen Hubbe905921e2015-07-13 08:07:21 -04001273 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001274
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001275 unmap->len = len;
1276 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1277 pay_off, len, DMA_TO_DEVICE);
1278 if (dma_mapping_error(device->dev, unmap->addr[0]))
1279 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001280
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001281 unmap->to_cnt = 1;
1282
1283 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1284 buff_off, len, DMA_FROM_DEVICE);
1285 if (dma_mapping_error(device->dev, unmap->addr[1]))
1286 goto err_get_unmap;
1287
1288 unmap->from_cnt = 1;
1289
Dave Jiang8c874cc2016-01-08 09:51:05 -07001290 for (retries = 0; retries < DMA_RETRIES; retries++) {
1291 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
1292 unmap->addr[0], len,
1293 DMA_PREP_INTERRUPT);
1294 if (txd)
1295 break;
1296
1297 set_current_state(TASK_INTERRUPTIBLE);
1298 schedule_timeout(DMA_OUT_RESOURCE_TO);
1299 }
1300
1301 if (!txd) {
1302 qp->dma_rx_prep_err++;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001303 goto err_get_unmap;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001304 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001305
1306 txd->callback = ntb_rx_copy_callback;
1307 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001308 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001309
1310 cookie = dmaengine_submit(txd);
1311 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001312 goto err_set_unmap;
1313
1314 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001315
1316 qp->last_cookie = cookie;
1317
1318 qp->rx_async++;
1319
1320 return;
1321
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001322err_set_unmap:
1323 dmaengine_unmap_put(unmap);
1324err_get_unmap:
1325 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001326err:
1327 ntb_memcpy_rx(entry, offset);
1328 qp->rx_memcpy++;
1329}
1330
Jon Masonfce8a7b2012-11-16 19:27:12 -07001331static int ntb_process_rxc(struct ntb_transport_qp *qp)
1332{
1333 struct ntb_payload_header *hdr;
1334 struct ntb_queue_entry *entry;
1335 void *offset;
1336
Jon Mason793c20e2013-01-19 02:02:26 -07001337 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1338 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1339
Allen Hubbee26a5842015-04-09 10:33:20 -04001340 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1341 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001342
Jon Masonfce8a7b2012-11-16 19:27:12 -07001343 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001344 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001345 qp->rx_ring_empty++;
1346 return -EAGAIN;
1347 }
1348
Allen Hubbee26a5842015-04-09 10:33:20 -04001349 if (hdr->flags & LINK_DOWN_FLAG) {
1350 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1351 ntb_qp_link_down(qp);
1352 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001353 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001354 }
1355
1356 if (hdr->ver != (u32)qp->rx_pkts) {
1357 dev_dbg(&qp->ndev->pdev->dev,
1358 "version mismatch, expected %llu - got %u\n",
1359 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001360 qp->rx_err_ver++;
1361 return -EIO;
1362 }
1363
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001364 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
Allen Hubbee26a5842015-04-09 10:33:20 -04001365 if (!entry) {
1366 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1367 qp->rx_err_no_buf++;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001368 return -EAGAIN;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001369 }
1370
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001371 entry->rx_hdr = hdr;
1372 entry->index = qp->rx_index;
1373
Allen Hubbee26a5842015-04-09 10:33:20 -04001374 if (hdr->len > entry->len) {
1375 dev_dbg(&qp->ndev->pdev->dev,
1376 "receive buffer overflow! Wanted %d got %d\n",
1377 hdr->len, entry->len);
1378 qp->rx_err_oflow++;
1379
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001380 entry->len = -EIO;
1381 entry->flags |= DESC_DONE_FLAG;
1382
1383 ntb_complete_rxc(qp);
1384 } else {
1385 dev_dbg(&qp->ndev->pdev->dev,
1386 "RX OK index %u ver %u size %d into buf size %d\n",
1387 qp->rx_index, hdr->ver, hdr->len, entry->len);
1388
1389 qp->rx_bytes += hdr->len;
1390 qp->rx_pkts++;
1391
1392 entry->len = hdr->len;
1393
1394 ntb_async_rx(entry, offset);
Allen Hubbee26a5842015-04-09 10:33:20 -04001395 }
1396
Jon Mason282a2fe2013-02-12 09:52:50 -07001397 qp->rx_index++;
1398 qp->rx_index %= qp->rx_max_entry;
1399
1400 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001401}
1402
Allen Hubbee26a5842015-04-09 10:33:20 -04001403static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001404{
Allen Hubbee26a5842015-04-09 10:33:20 -04001405 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001406 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001407
Allen Hubbee26a5842015-04-09 10:33:20 -04001408 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1409 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001410
Jon Masonc336acd2013-01-17 15:28:45 -07001411 /* Limit the number of packets processed in a single interrupt to
1412 * provide fairness to others
1413 */
1414 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001415 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001416 if (rc)
1417 break;
1418 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001419
Dave Jiang569410ca2015-07-13 08:07:22 -04001420 if (i && qp->rx_dma_chan)
1421 dma_async_issue_pending(qp->rx_dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001422
Allen Hubbee26a5842015-04-09 10:33:20 -04001423 if (i == qp->rx_max_entry) {
1424 /* there is more work to do */
Dave Jiange9021332016-02-23 09:11:36 -07001425 if (qp->active)
1426 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001427 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1428 /* the doorbell bit is set: clear it */
1429 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1430 /* ntb_db_read ensures ntb_db_clear write is committed */
1431 ntb_db_read(qp->ndev);
1432
1433 /* an interrupt may have arrived between finishing
1434 * ntb_process_rxc and clearing the doorbell bit:
1435 * there might be some more work to do.
1436 */
Dave Jiange9021332016-02-23 09:11:36 -07001437 if (qp->active)
1438 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001439 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001440}
1441
Jon Mason282a2fe2013-02-12 09:52:50 -07001442static void ntb_tx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001443{
Jon Mason282a2fe2013-02-12 09:52:50 -07001444 struct ntb_queue_entry *entry = data;
1445 struct ntb_transport_qp *qp = entry->qp;
1446 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001447
Jon Mason74465642013-01-21 15:28:52 -07001448 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001449
Allen Hubbee26a5842015-04-09 10:33:20 -04001450 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001451
1452 /* The entry length can only be zero if the packet is intended to be a
1453 * "link down" or similar. Since no payload is being sent in these
1454 * cases, there is nothing to add to the completion queue.
1455 */
1456 if (entry->len > 0) {
1457 qp->tx_bytes += entry->len;
1458
1459 if (qp->tx_handler)
1460 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1461 entry->len);
1462 }
1463
1464 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1465}
1466
Jon Mason282a2fe2013-02-12 09:52:50 -07001467static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1468{
Dave Jiang06917f72015-05-19 16:45:46 -04001469#ifdef ARCH_HAS_NOCACHE_UACCESS
1470 /*
1471 * Using non-temporal mov to improve performance on non-cached
1472 * writes, even though we aren't actually copying from user space.
1473 */
1474 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1475#else
Jon Mason282a2fe2013-02-12 09:52:50 -07001476 memcpy_toio(offset, entry->buf, entry->len);
Dave Jiang06917f72015-05-19 16:45:46 -04001477#endif
Jon Mason282a2fe2013-02-12 09:52:50 -07001478
Allen Hubbee26a5842015-04-09 10:33:20 -04001479 /* Ensure that the data is fully copied out before setting the flags */
1480 wmb();
1481
Jon Mason282a2fe2013-02-12 09:52:50 -07001482 ntb_tx_copy_callback(entry);
1483}
1484
1485static void ntb_async_tx(struct ntb_transport_qp *qp,
1486 struct ntb_queue_entry *entry)
1487{
1488 struct ntb_payload_header __iomem *hdr;
1489 struct dma_async_tx_descriptor *txd;
Dave Jiang569410ca2015-07-13 08:07:22 -04001490 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001491 struct dma_device *device;
1492 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001493 struct dmaengine_unmap_data *unmap;
1494 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001495 dma_cookie_t cookie;
1496 void __iomem *offset;
1497 size_t len = entry->len;
1498 void *buf = entry->buf;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001499 int retries = 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001500
1501 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
1502 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1503 entry->tx_hdr = hdr;
1504
1505 iowrite32(entry->len, &hdr->len);
Allen Hubbee26a5842015-04-09 10:33:20 -04001506 iowrite32((u32)qp->tx_pkts, &hdr->ver);
Jon Mason282a2fe2013-02-12 09:52:50 -07001507
1508 if (!chan)
1509 goto err;
1510
1511 if (len < copy_bytes)
1512 goto err;
1513
1514 device = chan->device;
1515 dest = qp->tx_mw_phys + qp->tx_max_frame * qp->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001516 buff_off = (size_t)buf & ~PAGE_MASK;
1517 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001518
1519 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1520 goto err;
1521
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001522 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1523 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001524 goto err;
1525
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001526 unmap->len = len;
1527 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1528 buff_off, len, DMA_TO_DEVICE);
1529 if (dma_mapping_error(device->dev, unmap->addr[0]))
1530 goto err_get_unmap;
1531
1532 unmap->to_cnt = 1;
1533
Dave Jiang8c874cc2016-01-08 09:51:05 -07001534 for (retries = 0; retries < DMA_RETRIES; retries++) {
1535 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0],
1536 len, DMA_PREP_INTERRUPT);
1537 if (txd)
1538 break;
1539
1540 set_current_state(TASK_INTERRUPTIBLE);
1541 schedule_timeout(DMA_OUT_RESOURCE_TO);
1542 }
1543
1544 if (!txd) {
1545 qp->dma_tx_prep_err++;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001546 goto err_get_unmap;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001547 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001548
1549 txd->callback = ntb_tx_copy_callback;
1550 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001551 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001552
1553 cookie = dmaengine_submit(txd);
1554 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001555 goto err_set_unmap;
1556
1557 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001558
1559 dma_async_issue_pending(chan);
1560 qp->tx_async++;
1561
1562 return;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001563err_set_unmap:
1564 dmaengine_unmap_put(unmap);
1565err_get_unmap:
1566 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001567err:
1568 ntb_memcpy_tx(entry, offset);
1569 qp->tx_memcpy++;
1570}
1571
Jon Masonfce8a7b2012-11-16 19:27:12 -07001572static int ntb_process_tx(struct ntb_transport_qp *qp,
1573 struct ntb_queue_entry *entry)
1574{
Jon Mason793c20e2013-01-19 02:02:26 -07001575 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001576 qp->tx_ring_full++;
1577 return -EAGAIN;
1578 }
1579
Jon Masonef114ed2013-01-19 02:02:18 -07001580 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001581 if (qp->tx_handler)
Jon Mason179f9122015-12-18 13:22:37 -05001582 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001583
1584 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1585 &qp->tx_free_q);
1586 return 0;
1587 }
1588
Jon Mason282a2fe2013-02-12 09:52:50 -07001589 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001590
Jon Mason793c20e2013-01-19 02:02:26 -07001591 qp->tx_index++;
1592 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001593
1594 qp->tx_pkts++;
1595
1596 return 0;
1597}
1598
1599static void ntb_send_link_down(struct ntb_transport_qp *qp)
1600{
Allen Hubbee26a5842015-04-09 10:33:20 -04001601 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001602 struct ntb_queue_entry *entry;
1603 int i, rc;
1604
Allen Hubbee26a5842015-04-09 10:33:20 -04001605 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001606 return;
1607
Allen Hubbee22e0b92015-05-12 06:55:44 -04001608 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001609
1610 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001611 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001612 if (entry)
1613 break;
1614 msleep(100);
1615 }
1616
1617 if (!entry)
1618 return;
1619
1620 entry->cb_data = NULL;
1621 entry->buf = NULL;
1622 entry->len = 0;
1623 entry->flags = LINK_DOWN_FLAG;
1624
1625 rc = ntb_process_tx(qp, entry);
1626 if (rc)
1627 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1628 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001629
1630 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001631}
1632
Allen Hubbe1199aa62015-05-18 06:20:47 -04001633static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1634{
1635 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1636}
1637
Jon Masonfce8a7b2012-11-16 19:27:12 -07001638/**
1639 * ntb_transport_create_queue - Create a new NTB transport layer queue
1640 * @rx_handler: receive callback function
1641 * @tx_handler: transmit callback function
1642 * @event_handler: event callback function
1643 *
1644 * Create a new NTB transport layer queue and provide the queue with a callback
1645 * routine for both transmit and receive. The receive callback routine will be
1646 * used to pass up data when the transport has received it on the queue. The
1647 * transmit callback routine will be called when the transport has completed the
1648 * transmission of the data on the queue and the data is ready to be freed.
1649 *
1650 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1651 */
1652struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001653ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001654 const struct ntb_queue_handlers *handlers)
1655{
Allen Hubbee26a5842015-04-09 10:33:20 -04001656 struct ntb_dev *ndev;
1657 struct pci_dev *pdev;
1658 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001659 struct ntb_queue_entry *entry;
1660 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001661 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001662 unsigned int free_queue;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001663 dma_cap_mask_t dma_mask;
1664 int node;
Allen Hubbee26a5842015-04-09 10:33:20 -04001665 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001666
Allen Hubbee26a5842015-04-09 10:33:20 -04001667 ndev = dev_ntb(client_dev->parent);
1668 pdev = ndev->pdev;
1669 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001670
Allen Hubbe1199aa62015-05-18 06:20:47 -04001671 node = dev_to_node(&ndev->dev);
1672
Jon Masonfce8a7b2012-11-16 19:27:12 -07001673 free_queue = ffs(nt->qp_bitmap);
1674 if (!free_queue)
1675 goto err;
1676
1677 /* decrement free_queue to make it zero based */
1678 free_queue--;
1679
Allen Hubbee26a5842015-04-09 10:33:20 -04001680 qp = &nt->qp_vec[free_queue];
1681 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001682
Allen Hubbee26a5842015-04-09 10:33:20 -04001683 nt->qp_bitmap_free &= ~qp_bit;
1684
Jon Masonfce8a7b2012-11-16 19:27:12 -07001685 qp->cb_data = data;
1686 qp->rx_handler = handlers->rx_handler;
1687 qp->tx_handler = handlers->tx_handler;
1688 qp->event_handler = handlers->event_handler;
1689
Allen Hubbe1199aa62015-05-18 06:20:47 -04001690 dma_cap_zero(dma_mask);
1691 dma_cap_set(DMA_MEMCPY, dma_mask);
1692
Dave Jianga41ef052015-05-19 16:52:04 -04001693 if (use_dma) {
Dave Jiang569410ca2015-07-13 08:07:22 -04001694 qp->tx_dma_chan =
1695 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1696 (void *)(unsigned long)node);
1697 if (!qp->tx_dma_chan)
1698 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
1699
1700 qp->rx_dma_chan =
1701 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1702 (void *)(unsigned long)node);
1703 if (!qp->rx_dma_chan)
1704 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
Dave Jianga41ef052015-05-19 16:52:04 -04001705 } else {
Dave Jiang569410ca2015-07-13 08:07:22 -04001706 qp->tx_dma_chan = NULL;
1707 qp->rx_dma_chan = NULL;
Dave Jianga41ef052015-05-19 16:52:04 -04001708 }
Dave Jiang569410ca2015-07-13 08:07:22 -04001709
1710 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
1711 qp->tx_dma_chan ? "DMA" : "CPU");
1712
1713 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
1714 qp->rx_dma_chan ? "DMA" : "CPU");
Jon Mason282a2fe2013-02-12 09:52:50 -07001715
Jon Masonfce8a7b2012-11-16 19:27:12 -07001716 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001717 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001718 if (!entry)
1719 goto err1;
1720
Jon Mason282a2fe2013-02-12 09:52:50 -07001721 entry->qp = qp;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001722 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001723 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001724 }
1725
1726 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001727 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001728 if (!entry)
1729 goto err2;
1730
Jon Mason282a2fe2013-02-12 09:52:50 -07001731 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001732 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001733 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001734 }
1735
Allen Hubbee26a5842015-04-09 10:33:20 -04001736 ntb_db_clear(qp->ndev, qp_bit);
1737 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001738
1739 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1740
1741 return qp;
1742
Jon Masonfce8a7b2012-11-16 19:27:12 -07001743err2:
Jon Masonf7667552013-01-19 02:02:24 -07001744 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001745 kfree(entry);
1746err1:
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001747 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001748 kfree(entry);
Dave Jiang569410ca2015-07-13 08:07:22 -04001749 if (qp->tx_dma_chan)
1750 dma_release_channel(qp->tx_dma_chan);
1751 if (qp->rx_dma_chan)
1752 dma_release_channel(qp->rx_dma_chan);
Allen Hubbee26a5842015-04-09 10:33:20 -04001753 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001754err:
1755 return NULL;
1756}
1757EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1758
1759/**
1760 * ntb_transport_free_queue - Frees NTB transport queue
1761 * @qp: NTB queue to be freed
1762 *
1763 * Frees NTB transport queue
1764 */
1765void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1766{
Jon Mason186f27f2013-01-22 11:35:40 -07001767 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001768 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001769 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001770
1771 if (!qp)
1772 return;
1773
Allen Hubbee26a5842015-04-09 10:33:20 -04001774 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001775
Dave Jiange9021332016-02-23 09:11:36 -07001776 qp->active = false;
1777
Dave Jiang569410ca2015-07-13 08:07:22 -04001778 if (qp->tx_dma_chan) {
1779 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001780 /* Putting the dma_chan to NULL will force any new traffic to be
1781 * processed by the CPU instead of the DAM engine
1782 */
Dave Jiang569410ca2015-07-13 08:07:22 -04001783 qp->tx_dma_chan = NULL;
1784
1785 /* Try to be nice and wait for any queued DMA engine
1786 * transactions to process before smashing it with a rock
1787 */
1788 dma_sync_wait(chan, qp->last_cookie);
1789 dmaengine_terminate_all(chan);
1790 dma_release_channel(chan);
1791 }
1792
1793 if (qp->rx_dma_chan) {
1794 struct dma_chan *chan = qp->rx_dma_chan;
1795 /* Putting the dma_chan to NULL will force any new traffic to be
1796 * processed by the CPU instead of the DAM engine
1797 */
1798 qp->rx_dma_chan = NULL;
Jon Mason282a2fe2013-02-12 09:52:50 -07001799
1800 /* Try to be nice and wait for any queued DMA engine
1801 * transactions to process before smashing it with a rock
1802 */
1803 dma_sync_wait(chan, qp->last_cookie);
1804 dmaengine_terminate_all(chan);
Allen Hubbe1199aa62015-05-18 06:20:47 -04001805 dma_release_channel(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001806 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001807
Allen Hubbee26a5842015-04-09 10:33:20 -04001808 qp_bit = BIT_ULL(qp->qp_num);
1809
1810 ntb_db_set_mask(qp->ndev, qp_bit);
Dave Jiange9021332016-02-23 09:11:36 -07001811 tasklet_kill(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001812
Jon Mason282a2fe2013-02-12 09:52:50 -07001813 cancel_delayed_work_sync(&qp->link_work);
1814
Allen Hubbee26a5842015-04-09 10:33:20 -04001815 qp->cb_data = NULL;
1816 qp->rx_handler = NULL;
1817 qp->tx_handler = NULL;
1818 qp->event_handler = NULL;
1819
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001820 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001821 kfree(entry);
1822
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001823 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1824 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1825 kfree(entry);
1826 }
1827
1828 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1829 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001830 kfree(entry);
1831 }
1832
Jon Masonf7667552013-01-19 02:02:24 -07001833 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001834 kfree(entry);
1835
Allen Hubbe30a4bb12015-07-13 08:07:14 -04001836 qp->transport->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001837
1838 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1839}
1840EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1841
1842/**
1843 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1844 * @qp: NTB queue to be freed
1845 * @len: pointer to variable to write enqueued buffers length
1846 *
1847 * Dequeues unused buffers from receive queue. Should only be used during
1848 * shutdown of qp.
1849 *
1850 * RETURNS: NULL error value on error, or void* for success.
1851 */
1852void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1853{
1854 struct ntb_queue_entry *entry;
1855 void *buf;
1856
Allen Hubbee26a5842015-04-09 10:33:20 -04001857 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001858 return NULL;
1859
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001860 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001861 if (!entry)
1862 return NULL;
1863
1864 buf = entry->cb_data;
1865 *len = entry->len;
1866
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001867 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001868
1869 return buf;
1870}
1871EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1872
1873/**
1874 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1875 * @qp: NTB transport layer queue the entry is to be enqueued on
1876 * @cb: per buffer pointer for callback function to use
1877 * @data: pointer to data buffer that incoming packets will be copied into
1878 * @len: length of the data buffer
1879 *
1880 * Enqueue a new receive buffer onto the transport queue into which a NTB
1881 * payload can be received into.
1882 *
1883 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1884 */
1885int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1886 unsigned int len)
1887{
1888 struct ntb_queue_entry *entry;
1889
1890 if (!qp)
1891 return -EINVAL;
1892
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001893 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001894 if (!entry)
1895 return -ENOMEM;
1896
1897 entry->cb_data = cb;
1898 entry->buf = data;
1899 entry->len = len;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001900 entry->flags = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001901
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001902 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
1903
Dave Jiange9021332016-02-23 09:11:36 -07001904 if (qp->active)
1905 tasklet_schedule(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001906
1907 return 0;
1908}
1909EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1910
1911/**
1912 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1913 * @qp: NTB transport layer queue the entry is to be enqueued on
1914 * @cb: per buffer pointer for callback function to use
1915 * @data: pointer to data buffer that will be sent
1916 * @len: length of the data buffer
1917 *
1918 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07001919 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07001920 * serialize access to the qp.
1921 *
1922 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1923 */
1924int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1925 unsigned int len)
1926{
1927 struct ntb_queue_entry *entry;
1928 int rc;
1929
Allen Hubbee26a5842015-04-09 10:33:20 -04001930 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001931 return -EINVAL;
1932
1933 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001934 if (!entry) {
1935 qp->tx_err_no_buf++;
Dave Jiange74bfee2015-07-13 08:07:17 -04001936 return -EBUSY;
Jon Mason282a2fe2013-02-12 09:52:50 -07001937 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001938
1939 entry->cb_data = cb;
1940 entry->buf = data;
1941 entry->len = len;
1942 entry->flags = 0;
1943
1944 rc = ntb_process_tx(qp, entry);
1945 if (rc)
1946 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1947 &qp->tx_free_q);
1948
1949 return rc;
1950}
1951EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1952
1953/**
1954 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1955 * @qp: NTB transport layer queue to be enabled
1956 *
1957 * Notify NTB transport layer of client readiness to use queue
1958 */
1959void ntb_transport_link_up(struct ntb_transport_qp *qp)
1960{
1961 if (!qp)
1962 return;
1963
Allen Hubbee26a5842015-04-09 10:33:20 -04001964 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001965
Allen Hubbee26a5842015-04-09 10:33:20 -04001966 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001967 schedule_delayed_work(&qp->link_work, 0);
1968}
1969EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1970
1971/**
1972 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1973 * @qp: NTB transport layer queue to be disabled
1974 *
1975 * Notify NTB transport layer of client's desire to no longer receive data on
1976 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07001977 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07001978 */
1979void ntb_transport_link_down(struct ntb_transport_qp *qp)
1980{
Allen Hubbee26a5842015-04-09 10:33:20 -04001981 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001982
1983 if (!qp)
1984 return;
1985
Allen Hubbee26a5842015-04-09 10:33:20 -04001986 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001987
Allen Hubbee26a5842015-04-09 10:33:20 -04001988 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001989
Allen Hubbee26a5842015-04-09 10:33:20 -04001990 ntb_peer_spad_write(qp->ndev, QP_LINKS,
1991 val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001992
Allen Hubbee26a5842015-04-09 10:33:20 -04001993 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001994 ntb_send_link_down(qp);
1995 else
1996 cancel_delayed_work_sync(&qp->link_work);
1997}
1998EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1999
2000/**
2001 * ntb_transport_link_query - Query transport link state
2002 * @qp: NTB transport layer queue to be queried
2003 *
2004 * Query connectivity to the remote system of the NTB transport queue
2005 *
2006 * RETURNS: true for link up or false for link down
2007 */
2008bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2009{
Jon Mason186f27f2013-01-22 11:35:40 -07002010 if (!qp)
2011 return false;
2012
Allen Hubbee26a5842015-04-09 10:33:20 -04002013 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002014}
2015EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2016
2017/**
2018 * ntb_transport_qp_num - Query the qp number
2019 * @qp: NTB transport layer queue to be queried
2020 *
2021 * Query qp number of the NTB transport queue
2022 *
2023 * RETURNS: a zero based number specifying the qp number
2024 */
2025unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2026{
Jon Mason186f27f2013-01-22 11:35:40 -07002027 if (!qp)
2028 return 0;
2029
Jon Masonfce8a7b2012-11-16 19:27:12 -07002030 return qp->qp_num;
2031}
2032EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2033
2034/**
2035 * ntb_transport_max_size - Query the max payload size of a qp
2036 * @qp: NTB transport layer queue to be queried
2037 *
2038 * Query the maximum payload size permissible on the given qp
2039 *
2040 * RETURNS: the max payload size of a qp
2041 */
Jon Masonef114ed2013-01-19 02:02:18 -07002042unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002043{
Dave Jiang04afde42015-09-17 13:27:04 -07002044 unsigned int max_size;
Dave Jiang569410ca2015-07-13 08:07:22 -04002045 unsigned int copy_align;
Dave Jiang04afde42015-09-17 13:27:04 -07002046 struct dma_chan *rx_chan, *tx_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002047
Jon Mason186f27f2013-01-22 11:35:40 -07002048 if (!qp)
2049 return 0;
2050
Dave Jiang04afde42015-09-17 13:27:04 -07002051 rx_chan = qp->rx_dma_chan;
2052 tx_chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002053
Dave Jiang04afde42015-09-17 13:27:04 -07002054 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2055 tx_chan ? tx_chan->device->copy_align : 0);
Dave Jiang569410ca2015-07-13 08:07:22 -04002056
Jon Mason282a2fe2013-02-12 09:52:50 -07002057 /* If DMA engine usage is possible, try to find the max size for that */
Dave Jiang04afde42015-09-17 13:27:04 -07002058 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2059 max_size = round_down(max_size, 1 << copy_align);
Jon Mason282a2fe2013-02-12 09:52:50 -07002060
Dave Jiang04afde42015-09-17 13:27:04 -07002061 return max_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002062}
2063EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04002064
Dave Jiange74bfee2015-07-13 08:07:17 -04002065unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2066{
2067 unsigned int head = qp->tx_index;
2068 unsigned int tail = qp->remote_rx_info->entry;
2069
2070 return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2071}
2072EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2073
Allen Hubbee26a5842015-04-09 10:33:20 -04002074static void ntb_transport_doorbell_callback(void *data, int vector)
2075{
2076 struct ntb_transport_ctx *nt = data;
2077 struct ntb_transport_qp *qp;
2078 u64 db_bits;
2079 unsigned int qp_num;
2080
2081 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2082 ntb_db_vector_mask(nt->ndev, vector));
2083
2084 while (db_bits) {
2085 qp_num = __ffs(db_bits);
2086 qp = &nt->qp_vec[qp_num];
2087
Dave Jiange9021332016-02-23 09:11:36 -07002088 if (qp->active)
2089 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04002090
2091 db_bits &= ~BIT_ULL(qp_num);
2092 }
2093}
2094
2095static const struct ntb_ctx_ops ntb_transport_ops = {
2096 .link_event = ntb_transport_event_callback,
2097 .db_event = ntb_transport_doorbell_callback,
2098};
2099
2100static struct ntb_client ntb_transport_client = {
2101 .ops = {
2102 .probe = ntb_transport_probe,
2103 .remove = ntb_transport_free,
2104 },
2105};
2106
2107static int __init ntb_transport_init(void)
2108{
2109 int rc;
2110
Dave Jiang7eb38782015-06-15 08:21:33 -04002111 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2112
Allen Hubbee26a5842015-04-09 10:33:20 -04002113 if (debugfs_initialized())
2114 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2115
2116 rc = bus_register(&ntb_transport_bus);
2117 if (rc)
2118 goto err_bus;
2119
2120 rc = ntb_register_client(&ntb_transport_client);
2121 if (rc)
2122 goto err_client;
2123
2124 return 0;
2125
2126err_client:
2127 bus_unregister(&ntb_transport_bus);
2128err_bus:
2129 debugfs_remove_recursive(nt_debugfs_dir);
2130 return rc;
2131}
2132module_init(ntb_transport_init);
2133
2134static void __exit ntb_transport_exit(void)
2135{
2136 debugfs_remove_recursive(nt_debugfs_dir);
2137
2138 ntb_unregister_client(&ntb_transport_client);
2139 bus_unregister(&ntb_transport_bus);
2140}
2141module_exit(ntb_transport_exit);