blob: d5c5894f252e04b07c110da0d3c3be96d6a515ce [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;
Dave Jianga754a8f2016-04-08 10:49:06 -0700156 unsigned int rx_alloc_entry;
Jon Mason282a2fe2013-02-12 09:52:50 -0700157 dma_cookie_t last_cookie;
Allen Hubbee26a5842015-04-09 10:33:20 -0400158 struct tasklet_struct rxc_db_work;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700159
Jon Mason53ca4fe2013-11-26 11:21:50 -0700160 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700161 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700162 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700163
164 struct dentry *debugfs_dir;
165 struct dentry *debugfs_stats;
166
167 /* Stats */
168 u64 rx_bytes;
169 u64 rx_pkts;
170 u64 rx_ring_empty;
171 u64 rx_err_no_buf;
172 u64 rx_err_oflow;
173 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700174 u64 rx_memcpy;
175 u64 rx_async;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700176 u64 dma_rx_prep_err;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700177 u64 tx_bytes;
178 u64 tx_pkts;
179 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700180 u64 tx_err_no_buf;
181 u64 tx_memcpy;
182 u64 tx_async;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700183 u64 dma_tx_prep_err;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700184};
185
186struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400187 phys_addr_t phys_addr;
188 resource_size_t phys_size;
189 resource_size_t xlat_align;
190 resource_size_t xlat_align_size;
191 void __iomem *vbase;
192 size_t xlat_size;
193 size_t buff_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700194 void *virt_addr;
195 dma_addr_t dma_addr;
196};
197
198struct ntb_transport_client_dev {
199 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400200 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700201 struct device dev;
202};
203
Allen Hubbee26a5842015-04-09 10:33:20 -0400204struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700205 struct list_head entry;
206 struct list_head client_devs;
207
Allen Hubbee26a5842015-04-09 10:33:20 -0400208 struct ntb_dev *ndev;
209
210 struct ntb_transport_mw *mw_vec;
211 struct ntb_transport_qp *qp_vec;
212 unsigned int mw_count;
213 unsigned int qp_count;
214 u64 qp_bitmap;
215 u64 qp_bitmap_free;
216
217 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700218 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700219 struct work_struct link_cleanup;
Dave Jiangc8650fd2015-07-13 08:07:09 -0400220
221 struct dentry *debugfs_node_dir;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700222};
223
224enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400225 DESC_DONE_FLAG = BIT(0),
226 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700227};
228
229struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700230 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700231 unsigned int len;
232 unsigned int flags;
233};
234
235enum {
236 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700237 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700238 NUM_QPS,
239 NUM_MWS,
240 MW0_SZ_HIGH,
241 MW0_SZ_LOW,
242 MW1_SZ_HIGH,
243 MW1_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700244 MAX_SPAD,
245};
246
Allen Hubbee26a5842015-04-09 10:33:20 -0400247#define dev_client_dev(__dev) \
248 container_of((__dev), struct ntb_transport_client_dev, dev)
249
250#define drv_client(__drv) \
251 container_of((__drv), struct ntb_transport_client, driver)
252
253#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700254#define NTB_QP_DEF_NUM_ENTRIES 100
255#define NTB_LINK_DOWN_TIMEOUT 10
Dave Jiang8c874cc2016-01-08 09:51:05 -0700256#define DMA_RETRIES 20
257#define DMA_OUT_RESOURCE_TO 50
Jon Masonfce8a7b2012-11-16 19:27:12 -0700258
Allen Hubbee26a5842015-04-09 10:33:20 -0400259static void ntb_transport_rxc_db(unsigned long data);
260static const struct ntb_ctx_ops ntb_transport_ops;
261static struct ntb_client ntb_transport_client;
262
263static int ntb_transport_bus_match(struct device *dev,
264 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700265{
266 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
267}
268
Allen Hubbee26a5842015-04-09 10:33:20 -0400269static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700270{
Allen Hubbee26a5842015-04-09 10:33:20 -0400271 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700272 int rc = -EINVAL;
273
274 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400275
276 client = drv_client(dev->driver);
277 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700278 if (rc)
279 put_device(dev);
280
281 return rc;
282}
283
Allen Hubbee26a5842015-04-09 10:33:20 -0400284static int ntb_transport_bus_remove(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700285{
Allen Hubbee26a5842015-04-09 10:33:20 -0400286 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700287
Allen Hubbee26a5842015-04-09 10:33:20 -0400288 client = drv_client(dev->driver);
289 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700290
291 put_device(dev);
292
293 return 0;
294}
295
Allen Hubbee26a5842015-04-09 10:33:20 -0400296static struct bus_type ntb_transport_bus = {
297 .name = "ntb_transport",
298 .match = ntb_transport_bus_match,
299 .probe = ntb_transport_bus_probe,
300 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700301};
302
303static LIST_HEAD(ntb_transport_list);
304
Allen Hubbee26a5842015-04-09 10:33:20 -0400305static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700306{
Dave Jiang31510002015-07-13 08:07:19 -0400307 list_add_tail(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700308 return 0;
309}
310
Allen Hubbee26a5842015-04-09 10:33:20 -0400311static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700312{
313 struct ntb_transport_client_dev *client_dev, *cd;
314
315 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
316 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
317 dev_name(&client_dev->dev));
318 list_del(&client_dev->entry);
319 device_unregister(&client_dev->dev);
320 }
321
322 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700323}
324
Allen Hubbee26a5842015-04-09 10:33:20 -0400325static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700326{
327 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700328
Allen Hubbee26a5842015-04-09 10:33:20 -0400329 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700330 kfree(client_dev);
331}
332
333/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400334 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700335 * @device_name: Name of NTB client device
336 *
337 * Unregister an NTB client device with the NTB transport layer
338 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400339void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700340{
341 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400342 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700343
344 list_for_each_entry(nt, &ntb_transport_list, entry)
345 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
346 if (!strncmp(dev_name(&client->dev), device_name,
347 strlen(device_name))) {
348 list_del(&client->entry);
349 device_unregister(&client->dev);
350 }
351}
Allen Hubbee26a5842015-04-09 10:33:20 -0400352EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700353
354/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400355 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700356 * @device_name: Name of NTB client device
357 *
358 * Register an NTB client device with the NTB transport layer
359 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400360int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700361{
362 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400363 struct ntb_transport_ctx *nt;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400364 int node;
Jon Mason8b19d452013-04-26 14:51:57 -0700365 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700366
Jon Mason8222b402013-01-19 02:02:16 -0700367 if (list_empty(&ntb_transport_list))
368 return -ENODEV;
369
Jon Masonfce8a7b2012-11-16 19:27:12 -0700370 list_for_each_entry(nt, &ntb_transport_list, entry) {
371 struct device *dev;
372
Allen Hubbe1199aa62015-05-18 06:20:47 -0400373 node = dev_to_node(&nt->ndev->dev);
374
375 client_dev = kzalloc_node(sizeof(*client_dev),
376 GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700377 if (!client_dev) {
378 rc = -ENOMEM;
379 goto err;
380 }
381
382 dev = &client_dev->dev;
383
384 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700385 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400386 dev->bus = &ntb_transport_bus;
387 dev->release = ntb_transport_client_release;
388 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700389
390 rc = device_register(dev);
391 if (rc) {
392 kfree(client_dev);
393 goto err;
394 }
395
396 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700397 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700398 }
399
400 return 0;
401
402err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400403 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700404
405 return rc;
406}
Allen Hubbee26a5842015-04-09 10:33:20 -0400407EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700408
409/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400410 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700411 * @drv: NTB client driver to be registered
412 *
413 * Register an NTB client driver with the NTB transport layer
414 *
415 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
416 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400417int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700418{
Allen Hubbee26a5842015-04-09 10:33:20 -0400419 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700420
Jon Mason8222b402013-01-19 02:02:16 -0700421 if (list_empty(&ntb_transport_list))
422 return -ENODEV;
423
Jon Masonfce8a7b2012-11-16 19:27:12 -0700424 return driver_register(&drv->driver);
425}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400426EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700427
428/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400429 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700430 * @drv: NTB client driver to be unregistered
431 *
432 * Unregister an NTB client driver with the NTB transport layer
433 *
434 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
435 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400436void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700437{
438 driver_unregister(&drv->driver);
439}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400440EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700441
Jon Masonfce8a7b2012-11-16 19:27:12 -0700442static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
443 loff_t *offp)
444{
445 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700446 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700447 ssize_t ret, out_offset, out_count;
448
Dave Jiang260bee92015-07-13 08:07:11 -0400449 qp = filp->private_data;
450
451 if (!qp || !qp->link_is_up)
452 return 0;
453
Jon Mason282a2fe2013-02-12 09:52:50 -0700454 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700455
456 buf = kmalloc(out_count, GFP_KERNEL);
457 if (!buf)
458 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700459
Jon Masonfce8a7b2012-11-16 19:27:12 -0700460 out_offset = 0;
461 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400462 "\nNTB QP stats:\n\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700463 out_offset += snprintf(buf + out_offset, out_count - out_offset,
464 "rx_bytes - \t%llu\n", qp->rx_bytes);
465 out_offset += snprintf(buf + out_offset, out_count - out_offset,
466 "rx_pkts - \t%llu\n", qp->rx_pkts);
467 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700468 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
469 out_offset += snprintf(buf + out_offset, out_count - out_offset,
470 "rx_async - \t%llu\n", qp->rx_async);
471 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700472 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
473 out_offset += snprintf(buf + out_offset, out_count - out_offset,
474 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
475 out_offset += snprintf(buf + out_offset, out_count - out_offset,
476 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
477 out_offset += snprintf(buf + out_offset, out_count - out_offset,
478 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
479 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400480 "rx_buff - \t0x%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700481 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700482 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700483 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jianga754a8f2016-04-08 10:49:06 -0700484 "rx_max_entry - \t%u\n", qp->rx_max_entry);
485 out_offset += snprintf(buf + out_offset, out_count - out_offset,
486 "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700487
488 out_offset += snprintf(buf + out_offset, out_count - out_offset,
489 "tx_bytes - \t%llu\n", qp->tx_bytes);
490 out_offset += snprintf(buf + out_offset, out_count - out_offset,
491 "tx_pkts - \t%llu\n", qp->tx_pkts);
492 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700493 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
494 out_offset += snprintf(buf + out_offset, out_count - out_offset,
495 "tx_async - \t%llu\n", qp->tx_async);
496 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700497 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
498 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700499 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
500 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400501 "tx_mw - \t0x%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700502 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400503 "tx_index (H) - \t%u\n", qp->tx_index);
504 out_offset += snprintf(buf + out_offset, out_count - out_offset,
505 "RRI (T) - \t%u\n",
506 qp->remote_rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700507 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700508 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400509 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiange74bfee2015-07-13 08:07:17 -0400510 "free tx - \t%u\n",
511 ntb_transport_tx_free_entry(qp));
Dave Jiang8c874cc2016-01-08 09:51:05 -0700512 out_offset += snprintf(buf + out_offset, out_count - out_offset,
513 "DMA tx prep err - \t%llu\n",
514 qp->dma_tx_prep_err);
515 out_offset += snprintf(buf + out_offset, out_count - out_offset,
516 "DMA rx prep err - \t%llu\n",
517 qp->dma_rx_prep_err);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700518
519 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400520 "\n");
521 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiang569410ca2015-07-13 08:07:22 -0400522 "Using TX DMA - \t%s\n",
523 qp->tx_dma_chan ? "Yes" : "No");
524 out_offset += snprintf(buf + out_offset, out_count - out_offset,
525 "Using RX DMA - \t%s\n",
526 qp->rx_dma_chan ? "Yes" : "No");
Dave Jiangd98ef992015-07-13 08:07:20 -0400527 out_offset += snprintf(buf + out_offset, out_count - out_offset,
528 "QP Link - \t%s\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400529 qp->link_is_up ? "Up" : "Down");
Dave Jiangd98ef992015-07-13 08:07:20 -0400530 out_offset += snprintf(buf + out_offset, out_count - out_offset,
531 "\n");
532
Jon Masond7237e22013-01-19 02:02:25 -0700533 if (out_offset > out_count)
534 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700535
536 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700537 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700538 return ret;
539}
540
541static const struct file_operations ntb_qp_debugfs_stats = {
542 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700543 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700544 .read = debugfs_read,
545};
546
547static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
548 struct list_head *list)
549{
550 unsigned long flags;
551
552 spin_lock_irqsave(lock, flags);
553 list_add_tail(entry, list);
554 spin_unlock_irqrestore(lock, flags);
555}
556
557static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700558 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700559{
560 struct ntb_queue_entry *entry;
561 unsigned long flags;
562
563 spin_lock_irqsave(lock, flags);
564 if (list_empty(list)) {
565 entry = NULL;
566 goto out;
567 }
568 entry = list_first_entry(list, struct ntb_queue_entry, entry);
569 list_del(&entry->entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400570
Jon Masonfce8a7b2012-11-16 19:27:12 -0700571out:
572 spin_unlock_irqrestore(lock, flags);
573
574 return entry;
575}
576
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400577static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
578 struct list_head *list,
579 struct list_head *to_list)
580{
581 struct ntb_queue_entry *entry;
582 unsigned long flags;
583
584 spin_lock_irqsave(lock, flags);
585
586 if (list_empty(list)) {
587 entry = NULL;
588 } else {
589 entry = list_first_entry(list, struct ntb_queue_entry, entry);
590 list_move_tail(&entry->entry, to_list);
591 }
592
593 spin_unlock_irqrestore(lock, flags);
594
595 return entry;
596}
597
Allen Hubbee26a5842015-04-09 10:33:20 -0400598static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
599 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700600{
Allen Hubbee26a5842015-04-09 10:33:20 -0400601 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
602 struct ntb_transport_mw *mw;
Dave Jianga754a8f2016-04-08 10:49:06 -0700603 struct ntb_dev *ndev = nt->ndev;
604 struct ntb_queue_entry *entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700605 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400606 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700607 unsigned int i;
Dave Jianga754a8f2016-04-08 10:49:06 -0700608 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700609
Allen Hubbee26a5842015-04-09 10:33:20 -0400610 mw_count = nt->mw_count;
611 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700612
Allen Hubbee26a5842015-04-09 10:33:20 -0400613 mw_num = QP_TO_MW(nt, qp_num);
614 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700615
Allen Hubbee26a5842015-04-09 10:33:20 -0400616 if (!mw->virt_addr)
617 return -ENOMEM;
618
619 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
620 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700621 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400622 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700623
Allen Hubbee26a5842015-04-09 10:33:20 -0400624 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -0400625 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
Jon Mason793c20e2013-01-19 02:02:26 -0700626 rx_size -= sizeof(struct ntb_rx_info);
627
Jon Mason282a2fe2013-02-12 09:52:50 -0700628 qp->remote_rx_info = qp->rx_buff + rx_size;
629
Jon Masonc9d534c2013-02-01 15:45:16 -0700630 /* Due to housekeeping, there must be atleast 2 buffs */
631 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700632 qp->rx_max_entry = rx_size / qp->rx_max_frame;
633 qp->rx_index = 0;
634
Dave Jianga754a8f2016-04-08 10:49:06 -0700635 /*
636 * Checking to see if we have more entries than the default.
637 * We should add additional entries if that is the case so we
638 * can be in sync with the transport frames.
639 */
640 node = dev_to_node(&ndev->dev);
641 for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
642 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
643 if (!entry)
644 return -ENOMEM;
645
646 entry->qp = qp;
647 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
648 &qp->rx_free_q);
649 qp->rx_alloc_entry++;
650 }
651
Jon Masonc9d534c2013-02-01 15:45:16 -0700652 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700653
Jon Masonef114ed2013-01-19 02:02:18 -0700654 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700655 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400656 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
657 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700658 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700659 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700660
661 qp->rx_pkts = 0;
662 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700663 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400664
665 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700666}
667
Allen Hubbee26a5842015-04-09 10:33:20 -0400668static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700669{
Allen Hubbee26a5842015-04-09 10:33:20 -0400670 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
671 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700672
673 if (!mw->virt_addr)
674 return;
675
Allen Hubbee26a5842015-04-09 10:33:20 -0400676 ntb_mw_clear_trans(nt->ndev, num_mw);
677 dma_free_coherent(&pdev->dev, mw->buff_size,
678 mw->virt_addr, mw->dma_addr);
679 mw->xlat_size = 0;
680 mw->buff_size = 0;
Jon Mason113fc502013-01-30 11:40:52 -0700681 mw->virt_addr = NULL;
682}
683
Allen Hubbee26a5842015-04-09 10:33:20 -0400684static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400685 resource_size_t size)
Jon Masonb77b2632013-02-01 15:25:37 -0700686{
Allen Hubbee26a5842015-04-09 10:33:20 -0400687 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
688 struct pci_dev *pdev = nt->ndev->pdev;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400689 size_t xlat_size, buff_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400690 int rc;
691
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400692 if (!size)
693 return -EINVAL;
694
Allen Hubbee26a5842015-04-09 10:33:20 -0400695 xlat_size = round_up(size, mw->xlat_align_size);
696 buff_size = round_up(size, mw->xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700697
698 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400699 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700700 return 0;
701
Allen Hubbee26a5842015-04-09 10:33:20 -0400702 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700703 ntb_free_mw(nt, num_mw);
704
Allen Hubbee26a5842015-04-09 10:33:20 -0400705 /* Alloc memory for receiving data. Must be aligned */
706 mw->xlat_size = xlat_size;
707 mw->buff_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700708
Allen Hubbee26a5842015-04-09 10:33:20 -0400709 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
710 &mw->dma_addr, GFP_KERNEL);
Jon Masonb77b2632013-02-01 15:25:37 -0700711 if (!mw->virt_addr) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400712 mw->xlat_size = 0;
713 mw->buff_size = 0;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400714 dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400715 buff_size);
Jon Masonb77b2632013-02-01 15:25:37 -0700716 return -ENOMEM;
717 }
718
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700719 /*
720 * we must ensure that the memory address allocated is BAR size
721 * aligned in order for the XLAT register to take the value. This
722 * is a requirement of the hardware. It is recommended to setup CMA
723 * for BAR sizes equal or greater than 4MB.
724 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400725 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
726 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700727 &mw->dma_addr);
728 ntb_free_mw(nt, num_mw);
729 return -ENOMEM;
730 }
731
Jon Masonb77b2632013-02-01 15:25:37 -0700732 /* Notify HW the memory location of the receive buffer */
Allen Hubbee26a5842015-04-09 10:33:20 -0400733 rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
734 if (rc) {
735 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
736 ntb_free_mw(nt, num_mw);
737 return -EIO;
738 }
Jon Masonb77b2632013-02-01 15:25:37 -0700739
740 return 0;
741}
742
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400743static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
744{
745 qp->link_is_up = false;
Dave Jiange9021332016-02-23 09:11:36 -0700746 qp->active = false;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400747
748 qp->tx_index = 0;
749 qp->rx_index = 0;
750 qp->rx_bytes = 0;
751 qp->rx_pkts = 0;
752 qp->rx_ring_empty = 0;
753 qp->rx_err_no_buf = 0;
754 qp->rx_err_oflow = 0;
755 qp->rx_err_ver = 0;
756 qp->rx_memcpy = 0;
757 qp->rx_async = 0;
758 qp->tx_bytes = 0;
759 qp->tx_pkts = 0;
760 qp->tx_ring_full = 0;
761 qp->tx_err_no_buf = 0;
762 qp->tx_memcpy = 0;
763 qp->tx_async = 0;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700764 qp->dma_tx_prep_err = 0;
765 qp->dma_rx_prep_err = 0;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400766}
767
Jon Masonfca4d512013-09-09 13:39:55 -0700768static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700769{
Allen Hubbee26a5842015-04-09 10:33:20 -0400770 struct ntb_transport_ctx *nt = qp->transport;
771 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700772
Allen Hubbee22e0b92015-05-12 06:55:44 -0400773 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400774
775 cancel_delayed_work_sync(&qp->link_work);
776 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400777
778 if (qp->event_handler)
779 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700780}
781
782static void ntb_qp_link_cleanup_work(struct work_struct *work)
783{
784 struct ntb_transport_qp *qp = container_of(work,
785 struct ntb_transport_qp,
786 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400787 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700788
789 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700790
Allen Hubbee26a5842015-04-09 10:33:20 -0400791 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700792 schedule_delayed_work(&qp->link_work,
793 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
794}
795
Jon Mason7b4f2d32013-01-19 02:02:19 -0700796static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700797{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700798 schedule_work(&qp->link_cleanup);
799}
800
Allen Hubbee26a5842015-04-09 10:33:20 -0400801static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700802{
Allen Hubbee26a5842015-04-09 10:33:20 -0400803 struct ntb_transport_qp *qp;
804 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700805 int i;
806
Allen Hubbee26a5842015-04-09 10:33:20 -0400807 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700808
Allen Hubbee26a5842015-04-09 10:33:20 -0400809 /* Pass along the info to any clients */
810 for (i = 0; i < nt->qp_count; i++)
811 if (qp_bitmap_alloc & BIT_ULL(i)) {
812 qp = &nt->qp_vec[i];
813 ntb_qp_link_cleanup(qp);
814 cancel_work_sync(&qp->link_cleanup);
815 cancel_delayed_work_sync(&qp->link_work);
816 }
817
818 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700819 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700820
Jon Masonfce8a7b2012-11-16 19:27:12 -0700821 /* The scratchpad registers keep the values if the remote side
822 * goes down, blast them now to give them a sane value the next
823 * time they are accessed
824 */
825 for (i = 0; i < MAX_SPAD; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400826 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700827}
828
Jon Masonfca4d512013-09-09 13:39:55 -0700829static void ntb_transport_link_cleanup_work(struct work_struct *work)
830{
Allen Hubbee26a5842015-04-09 10:33:20 -0400831 struct ntb_transport_ctx *nt =
832 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700833
834 ntb_transport_link_cleanup(nt);
835}
836
Allen Hubbee26a5842015-04-09 10:33:20 -0400837static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700838{
Allen Hubbee26a5842015-04-09 10:33:20 -0400839 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700840
Allen Hubbee26a5842015-04-09 10:33:20 -0400841 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700842 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400843 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700844 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700845}
846
847static void ntb_transport_link_work(struct work_struct *work)
848{
Allen Hubbee26a5842015-04-09 10:33:20 -0400849 struct ntb_transport_ctx *nt =
850 container_of(work, struct ntb_transport_ctx, link_work.work);
851 struct ntb_dev *ndev = nt->ndev;
852 struct pci_dev *pdev = ndev->pdev;
853 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700854 u32 val;
Dave Jiang84f76682016-02-29 09:35:26 -0700855 int rc = 0, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700856
Jon Mason113fc502013-01-30 11:40:52 -0700857 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400858 for (i = 0; i < nt->mw_count; i++) {
859 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700860
Allen Hubbee26a5842015-04-09 10:33:20 -0400861 if (max_mw_size && size > max_mw_size)
862 size = max_mw_size;
863
864 spad = MW0_SZ_HIGH + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200865 ntb_peer_spad_write(ndev, spad, upper_32_bits(size));
Allen Hubbee26a5842015-04-09 10:33:20 -0400866
867 spad = MW0_SZ_LOW + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200868 ntb_peer_spad_write(ndev, spad, lower_32_bits(size));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700869 }
870
Allen Hubbee26a5842015-04-09 10:33:20 -0400871 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700872
Allen Hubbee26a5842015-04-09 10:33:20 -0400873 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700874
Allen Hubbee26a5842015-04-09 10:33:20 -0400875 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700876
877 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400878 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400879 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700880 if (val != NTB_TRANSPORT_VERSION)
881 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700882
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400883 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700884 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400885 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700886 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700887
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400888 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700889 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400890 if (val != nt->mw_count)
891 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700892
Allen Hubbee26a5842015-04-09 10:33:20 -0400893 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700894 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700895
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400896 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400897 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700898
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400899 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700900 val64 |= val;
901
Allen Hubbee26a5842015-04-09 10:33:20 -0400902 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700903
904 rc = ntb_set_mw(nt, i, val64);
905 if (rc)
906 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700907 }
908
Allen Hubbee26a5842015-04-09 10:33:20 -0400909 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700910
Allen Hubbee26a5842015-04-09 10:33:20 -0400911 for (i = 0; i < nt->qp_count; i++) {
912 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700913
914 ntb_transport_setup_qp_mw(nt, i);
915
Allen Hubbee26a5842015-04-09 10:33:20 -0400916 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700917 schedule_delayed_work(&qp->link_work, 0);
918 }
919
920 return;
921
Jon Mason113fc502013-01-30 11:40:52 -0700922out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400923 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700924 ntb_free_mw(nt, i);
Dave Jiang84f76682016-02-29 09:35:26 -0700925
926 /* if there's an actual failure, we should just bail */
927 if (rc < 0) {
928 ntb_link_disable(ndev);
929 return;
930 }
931
Jon Masonfce8a7b2012-11-16 19:27:12 -0700932out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400933 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700934 schedule_delayed_work(&nt->link_work,
935 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
936}
937
938static void ntb_qp_link_work(struct work_struct *work)
939{
940 struct ntb_transport_qp *qp = container_of(work,
941 struct ntb_transport_qp,
942 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400943 struct pci_dev *pdev = qp->ndev->pdev;
944 struct ntb_transport_ctx *nt = qp->transport;
945 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700946
Allen Hubbee26a5842015-04-09 10:33:20 -0400947 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700948
Allen Hubbee26a5842015-04-09 10:33:20 -0400949 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700950
Allen Hubbee26a5842015-04-09 10:33:20 -0400951 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700952
953 /* query remote spad for qp ready bits */
Allen Hubbee26a5842015-04-09 10:33:20 -0400954 ntb_peer_spad_read(nt->ndev, QP_LINKS);
Allen Hubbe28762282015-05-11 10:08:26 -0400955 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700956
957 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400958 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700959 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400960 qp->link_is_up = true;
Dave Jiange9021332016-02-23 09:11:36 -0700961 qp->active = true;
Allen Hubbee26a5842015-04-09 10:33:20 -0400962
Jon Masonfce8a7b2012-11-16 19:27:12 -0700963 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400964 qp->event_handler(qp->cb_data, qp->link_is_up);
Allen Hubbe8b5a22d2015-07-13 08:07:12 -0400965
Dave Jiange9021332016-02-23 09:11:36 -0700966 if (qp->active)
967 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400968 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700969 schedule_delayed_work(&qp->link_work,
970 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
971}
972
Allen Hubbee26a5842015-04-09 10:33:20 -0400973static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700974 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700975{
976 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400977 phys_addr_t mw_base;
978 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700979 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400980 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700981 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700982
Allen Hubbee26a5842015-04-09 10:33:20 -0400983 mw_count = nt->mw_count;
984 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700985
Allen Hubbee26a5842015-04-09 10:33:20 -0400986 mw_num = QP_TO_MW(nt, qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400987
988 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700989 qp->qp_num = qp_num;
990 qp->transport = nt;
991 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400992 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700993 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400994 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700995
Allen Hubbee26a5842015-04-09 10:33:20 -0400996 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
997 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -0700998 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400999 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -07001000
Allen Hubbee26a5842015-04-09 10:33:20 -04001001 mw_base = nt->mw_vec[mw_num].phys_addr;
1002 mw_size = nt->mw_vec[mw_num].phys_size;
1003
1004 tx_size = (unsigned int)mw_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -04001005 qp_offset = tx_size * (qp_num / mw_count);
Allen Hubbee26a5842015-04-09 10:33:20 -04001006
1007 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001008 if (!qp->tx_mw)
1009 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -07001010
Allen Hubbee26a5842015-04-09 10:33:20 -04001011 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001012 if (!qp->tx_mw_phys)
1013 return -EINVAL;
1014
1015 tx_size -= sizeof(struct ntb_rx_info);
1016 qp->rx_info = qp->tx_mw + tx_size;
1017
Jon Masonc9d534c2013-02-01 15:45:16 -07001018 /* Due to housekeeping, there must be atleast 2 buffs */
1019 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -07001020 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -07001021
Dave Jiangc8650fd2015-07-13 08:07:09 -04001022 if (nt->debugfs_node_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001023 char debugfs_name[4];
1024
1025 snprintf(debugfs_name, 4, "qp%d", qp_num);
1026 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Dave Jiangc8650fd2015-07-13 08:07:09 -04001027 nt->debugfs_node_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001028
1029 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1030 qp->debugfs_dir, qp,
1031 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -04001032 } else {
1033 qp->debugfs_dir = NULL;
1034 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001035 }
1036
1037 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001038 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001039
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001040 spin_lock_init(&qp->ntb_rx_q_lock);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001041 spin_lock_init(&qp->ntb_tx_free_q_lock);
1042
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001043 INIT_LIST_HEAD(&qp->rx_post_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001044 INIT_LIST_HEAD(&qp->rx_pend_q);
1045 INIT_LIST_HEAD(&qp->rx_free_q);
1046 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001047
Allen Hubbee26a5842015-04-09 10:33:20 -04001048 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1049 (unsigned long)qp);
1050
Jon Mason282a2fe2013-02-12 09:52:50 -07001051 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001052}
1053
Allen Hubbee26a5842015-04-09 10:33:20 -04001054static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001055{
Allen Hubbee26a5842015-04-09 10:33:20 -04001056 struct ntb_transport_ctx *nt;
1057 struct ntb_transport_mw *mw;
1058 unsigned int mw_count, qp_count;
1059 u64 qp_bitmap;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001060 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001061 int rc, i;
1062
Logan Gunthorpe19645a02016-06-07 11:20:22 -06001063 mw_count = ntb_mw_count(ndev);
1064 if (ntb_spad_count(ndev) < (NUM_MWS + 1 + mw_count * 2)) {
1065 dev_err(&ndev->dev, "Not enough scratch pad registers for %s",
1066 NTB_TRANSPORT_NAME);
1067 return -EIO;
1068 }
1069
Allen Hubbee26a5842015-04-09 10:33:20 -04001070 if (ntb_db_is_unsafe(ndev))
1071 dev_dbg(&ndev->dev,
1072 "doorbell is unsafe, proceed anyway...\n");
1073 if (ntb_spad_is_unsafe(ndev))
1074 dev_dbg(&ndev->dev,
1075 "scratchpad is unsafe, proceed anyway...\n");
1076
Allen Hubbe1199aa62015-05-18 06:20:47 -04001077 node = dev_to_node(&ndev->dev);
1078
1079 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001080 if (!nt)
1081 return -ENOMEM;
1082
Allen Hubbee26a5842015-04-09 10:33:20 -04001083 nt->ndev = ndev;
1084
Allen Hubbee26a5842015-04-09 10:33:20 -04001085 nt->mw_count = mw_count;
1086
Allen Hubbe1199aa62015-05-18 06:20:47 -04001087 nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1088 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001089 if (!nt->mw_vec) {
1090 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001091 goto err;
1092 }
1093
Allen Hubbee26a5842015-04-09 10:33:20 -04001094 for (i = 0; i < mw_count; i++) {
1095 mw = &nt->mw_vec[i];
1096
1097 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
1098 &mw->xlat_align, &mw->xlat_align_size);
1099 if (rc)
1100 goto err1;
1101
Dave Jiang06917f72015-05-19 16:45:46 -04001102 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001103 if (!mw->vbase) {
1104 rc = -ENOMEM;
1105 goto err1;
1106 }
1107
1108 mw->buff_size = 0;
1109 mw->xlat_size = 0;
1110 mw->virt_addr = NULL;
1111 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -07001112 }
1113
Allen Hubbee26a5842015-04-09 10:33:20 -04001114 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001115
Allen Hubbee26a5842015-04-09 10:33:20 -04001116 qp_count = ilog2(qp_bitmap);
1117 if (max_num_clients && max_num_clients < qp_count)
1118 qp_count = max_num_clients;
1119 else if (mw_count < qp_count)
1120 qp_count = mw_count;
1121
1122 qp_bitmap &= BIT_ULL(qp_count) - 1;
1123
1124 nt->qp_count = qp_count;
1125 nt->qp_bitmap = qp_bitmap;
1126 nt->qp_bitmap_free = qp_bitmap;
1127
Allen Hubbe1199aa62015-05-18 06:20:47 -04001128 nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1129 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001130 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001131 rc = -ENOMEM;
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301132 goto err1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001133 }
1134
Dave Jiangc8650fd2015-07-13 08:07:09 -04001135 if (nt_debugfs_dir) {
1136 nt->debugfs_node_dir =
1137 debugfs_create_dir(pci_name(ndev->pdev),
1138 nt_debugfs_dir);
1139 }
1140
Allen Hubbee26a5842015-04-09 10:33:20 -04001141 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001142 rc = ntb_transport_init_queue(nt, i);
1143 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301144 goto err2;
Jon Mason282a2fe2013-02-12 09:52:50 -07001145 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001146
1147 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001148 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001149
Allen Hubbee26a5842015-04-09 10:33:20 -04001150 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001151 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301152 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001153
1154 INIT_LIST_HEAD(&nt->client_devs);
1155 rc = ntb_bus_init(nt);
1156 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301157 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001158
Allen Hubbee26a5842015-04-09 10:33:20 -04001159 nt->link_is_up = false;
1160 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1161 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001162
1163 return 0;
1164
Jon Mason948d3a62013-04-18 17:07:36 -07001165err3:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301166 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001167err2:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301168 kfree(nt->qp_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001169err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001170 while (i--) {
1171 mw = &nt->mw_vec[i];
1172 iounmap(mw->vbase);
1173 }
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301174 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001175err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001176 kfree(nt);
1177 return rc;
1178}
1179
Allen Hubbee26a5842015-04-09 10:33:20 -04001180static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001181{
Allen Hubbee26a5842015-04-09 10:33:20 -04001182 struct ntb_transport_ctx *nt = ndev->ctx;
1183 struct ntb_transport_qp *qp;
1184 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001185 int i;
1186
Jon Masonfca4d512013-09-09 13:39:55 -07001187 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001188 cancel_work_sync(&nt->link_cleanup);
1189 cancel_delayed_work_sync(&nt->link_work);
1190
1191 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001192
1193 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001194 for (i = 0; i < nt->qp_count; i++) {
1195 qp = &nt->qp_vec[i];
1196 if (qp_bitmap_alloc & BIT_ULL(i))
1197 ntb_transport_free_queue(qp);
1198 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001199 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001200
Allen Hubbee26a5842015-04-09 10:33:20 -04001201 ntb_link_disable(ndev);
1202 ntb_clear_ctx(ndev);
1203
Jon Masonfce8a7b2012-11-16 19:27:12 -07001204 ntb_bus_remove(nt);
1205
Allen Hubbee26a5842015-04-09 10:33:20 -04001206 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001207 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001208 iounmap(nt->mw_vec[i].vbase);
1209 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001210
Allen Hubbee26a5842015-04-09 10:33:20 -04001211 kfree(nt->qp_vec);
1212 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001213 kfree(nt);
1214}
1215
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001216static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1217{
1218 struct ntb_queue_entry *entry;
1219 void *cb_data;
1220 unsigned int len;
1221 unsigned long irqflags;
1222
1223 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1224
1225 while (!list_empty(&qp->rx_post_q)) {
1226 entry = list_first_entry(&qp->rx_post_q,
1227 struct ntb_queue_entry, entry);
1228 if (!(entry->flags & DESC_DONE_FLAG))
1229 break;
1230
1231 entry->rx_hdr->flags = 0;
1232 iowrite32(entry->index, &qp->rx_info->entry);
1233
1234 cb_data = entry->cb_data;
1235 len = entry->len;
1236
1237 list_move_tail(&entry->entry, &qp->rx_free_q);
1238
1239 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1240
1241 if (qp->rx_handler && qp->client_ready)
1242 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1243
1244 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1245 }
1246
1247 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1248}
1249
Jon Mason282a2fe2013-02-12 09:52:50 -07001250static void ntb_rx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001251{
Jon Mason282a2fe2013-02-12 09:52:50 -07001252 struct ntb_queue_entry *entry = data;
Jon Mason448c6fb2013-01-19 02:02:27 -07001253
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001254 entry->flags |= DESC_DONE_FLAG;
Jon Mason282a2fe2013-02-12 09:52:50 -07001255
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001256 ntb_complete_rxc(entry->qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001257}
1258
Jon Mason282a2fe2013-02-12 09:52:50 -07001259static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1260{
1261 void *buf = entry->buf;
1262 size_t len = entry->len;
1263
1264 memcpy(buf, offset, len);
1265
Allen Hubbee26a5842015-04-09 10:33:20 -04001266 /* Ensure that the data is fully copied out before clearing the flag */
1267 wmb();
1268
Jon Mason282a2fe2013-02-12 09:52:50 -07001269 ntb_rx_copy_callback(entry);
1270}
1271
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001272static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
Jon Mason282a2fe2013-02-12 09:52:50 -07001273{
1274 struct dma_async_tx_descriptor *txd;
1275 struct ntb_transport_qp *qp = entry->qp;
Dave Jiang569410ca2015-07-13 08:07:22 -04001276 struct dma_chan *chan = qp->rx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001277 struct dma_device *device;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001278 size_t pay_off, buff_off, len;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001279 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001280 dma_cookie_t cookie;
1281 void *buf = entry->buf;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001282 int retries = 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001283
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001284 len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001285
1286 if (!chan)
1287 goto err;
1288
Jon Mason53ca4fe2013-11-26 11:21:50 -07001289 if (len < copy_bytes)
Allen Hubbe905921e2015-07-13 08:07:21 -04001290 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001291
1292 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001293 pay_off = (size_t)offset & ~PAGE_MASK;
1294 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001295
1296 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Allen Hubbe905921e2015-07-13 08:07:21 -04001297 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001298
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001299 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1300 if (!unmap)
Allen Hubbe905921e2015-07-13 08:07:21 -04001301 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001302
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001303 unmap->len = len;
1304 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1305 pay_off, len, DMA_TO_DEVICE);
1306 if (dma_mapping_error(device->dev, unmap->addr[0]))
1307 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001308
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001309 unmap->to_cnt = 1;
1310
1311 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1312 buff_off, len, DMA_FROM_DEVICE);
1313 if (dma_mapping_error(device->dev, unmap->addr[1]))
1314 goto err_get_unmap;
1315
1316 unmap->from_cnt = 1;
1317
Dave Jiang8c874cc2016-01-08 09:51:05 -07001318 for (retries = 0; retries < DMA_RETRIES; retries++) {
1319 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
1320 unmap->addr[0], len,
1321 DMA_PREP_INTERRUPT);
1322 if (txd)
1323 break;
1324
1325 set_current_state(TASK_INTERRUPTIBLE);
1326 schedule_timeout(DMA_OUT_RESOURCE_TO);
1327 }
1328
1329 if (!txd) {
1330 qp->dma_rx_prep_err++;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001331 goto err_get_unmap;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001332 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001333
1334 txd->callback = ntb_rx_copy_callback;
1335 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001336 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001337
1338 cookie = dmaengine_submit(txd);
1339 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001340 goto err_set_unmap;
1341
1342 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001343
1344 qp->last_cookie = cookie;
1345
1346 qp->rx_async++;
1347
1348 return;
1349
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001350err_set_unmap:
1351 dmaengine_unmap_put(unmap);
1352err_get_unmap:
1353 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001354err:
1355 ntb_memcpy_rx(entry, offset);
1356 qp->rx_memcpy++;
1357}
1358
Jon Masonfce8a7b2012-11-16 19:27:12 -07001359static int ntb_process_rxc(struct ntb_transport_qp *qp)
1360{
1361 struct ntb_payload_header *hdr;
1362 struct ntb_queue_entry *entry;
1363 void *offset;
1364
Jon Mason793c20e2013-01-19 02:02:26 -07001365 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1366 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1367
Allen Hubbee26a5842015-04-09 10:33:20 -04001368 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1369 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001370
Jon Masonfce8a7b2012-11-16 19:27:12 -07001371 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001372 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001373 qp->rx_ring_empty++;
1374 return -EAGAIN;
1375 }
1376
Allen Hubbee26a5842015-04-09 10:33:20 -04001377 if (hdr->flags & LINK_DOWN_FLAG) {
1378 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1379 ntb_qp_link_down(qp);
1380 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001381 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001382 }
1383
1384 if (hdr->ver != (u32)qp->rx_pkts) {
1385 dev_dbg(&qp->ndev->pdev->dev,
1386 "version mismatch, expected %llu - got %u\n",
1387 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001388 qp->rx_err_ver++;
1389 return -EIO;
1390 }
1391
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001392 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
Allen Hubbee26a5842015-04-09 10:33:20 -04001393 if (!entry) {
1394 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1395 qp->rx_err_no_buf++;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001396 return -EAGAIN;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001397 }
1398
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001399 entry->rx_hdr = hdr;
1400 entry->index = qp->rx_index;
1401
Allen Hubbee26a5842015-04-09 10:33:20 -04001402 if (hdr->len > entry->len) {
1403 dev_dbg(&qp->ndev->pdev->dev,
1404 "receive buffer overflow! Wanted %d got %d\n",
1405 hdr->len, entry->len);
1406 qp->rx_err_oflow++;
1407
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001408 entry->len = -EIO;
1409 entry->flags |= DESC_DONE_FLAG;
1410
1411 ntb_complete_rxc(qp);
1412 } else {
1413 dev_dbg(&qp->ndev->pdev->dev,
1414 "RX OK index %u ver %u size %d into buf size %d\n",
1415 qp->rx_index, hdr->ver, hdr->len, entry->len);
1416
1417 qp->rx_bytes += hdr->len;
1418 qp->rx_pkts++;
1419
1420 entry->len = hdr->len;
1421
1422 ntb_async_rx(entry, offset);
Allen Hubbee26a5842015-04-09 10:33:20 -04001423 }
1424
Jon Mason282a2fe2013-02-12 09:52:50 -07001425 qp->rx_index++;
1426 qp->rx_index %= qp->rx_max_entry;
1427
1428 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001429}
1430
Allen Hubbee26a5842015-04-09 10:33:20 -04001431static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001432{
Allen Hubbee26a5842015-04-09 10:33:20 -04001433 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001434 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001435
Allen Hubbee26a5842015-04-09 10:33:20 -04001436 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1437 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001438
Jon Masonc336acd2013-01-17 15:28:45 -07001439 /* Limit the number of packets processed in a single interrupt to
1440 * provide fairness to others
1441 */
1442 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001443 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001444 if (rc)
1445 break;
1446 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001447
Dave Jiang569410ca2015-07-13 08:07:22 -04001448 if (i && qp->rx_dma_chan)
1449 dma_async_issue_pending(qp->rx_dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001450
Allen Hubbee26a5842015-04-09 10:33:20 -04001451 if (i == qp->rx_max_entry) {
1452 /* there is more work to do */
Dave Jiange9021332016-02-23 09:11:36 -07001453 if (qp->active)
1454 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001455 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1456 /* the doorbell bit is set: clear it */
1457 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1458 /* ntb_db_read ensures ntb_db_clear write is committed */
1459 ntb_db_read(qp->ndev);
1460
1461 /* an interrupt may have arrived between finishing
1462 * ntb_process_rxc and clearing the doorbell bit:
1463 * there might be some more work to do.
1464 */
Dave Jiange9021332016-02-23 09:11:36 -07001465 if (qp->active)
1466 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001467 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001468}
1469
Jon Mason282a2fe2013-02-12 09:52:50 -07001470static void ntb_tx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001471{
Jon Mason282a2fe2013-02-12 09:52:50 -07001472 struct ntb_queue_entry *entry = data;
1473 struct ntb_transport_qp *qp = entry->qp;
1474 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001475
Jon Mason74465642013-01-21 15:28:52 -07001476 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001477
Allen Hubbee26a5842015-04-09 10:33:20 -04001478 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001479
1480 /* The entry length can only be zero if the packet is intended to be a
1481 * "link down" or similar. Since no payload is being sent in these
1482 * cases, there is nothing to add to the completion queue.
1483 */
1484 if (entry->len > 0) {
1485 qp->tx_bytes += entry->len;
1486
1487 if (qp->tx_handler)
1488 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1489 entry->len);
1490 }
1491
1492 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1493}
1494
Jon Mason282a2fe2013-02-12 09:52:50 -07001495static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1496{
Dave Jiang06917f72015-05-19 16:45:46 -04001497#ifdef ARCH_HAS_NOCACHE_UACCESS
1498 /*
1499 * Using non-temporal mov to improve performance on non-cached
1500 * writes, even though we aren't actually copying from user space.
1501 */
1502 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1503#else
Jon Mason282a2fe2013-02-12 09:52:50 -07001504 memcpy_toio(offset, entry->buf, entry->len);
Dave Jiang06917f72015-05-19 16:45:46 -04001505#endif
Jon Mason282a2fe2013-02-12 09:52:50 -07001506
Allen Hubbee26a5842015-04-09 10:33:20 -04001507 /* Ensure that the data is fully copied out before setting the flags */
1508 wmb();
1509
Jon Mason282a2fe2013-02-12 09:52:50 -07001510 ntb_tx_copy_callback(entry);
1511}
1512
1513static void ntb_async_tx(struct ntb_transport_qp *qp,
1514 struct ntb_queue_entry *entry)
1515{
1516 struct ntb_payload_header __iomem *hdr;
1517 struct dma_async_tx_descriptor *txd;
Dave Jiang569410ca2015-07-13 08:07:22 -04001518 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001519 struct dma_device *device;
1520 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001521 struct dmaengine_unmap_data *unmap;
1522 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001523 dma_cookie_t cookie;
1524 void __iomem *offset;
1525 size_t len = entry->len;
1526 void *buf = entry->buf;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001527 int retries = 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001528
1529 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
1530 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1531 entry->tx_hdr = hdr;
1532
1533 iowrite32(entry->len, &hdr->len);
Allen Hubbee26a5842015-04-09 10:33:20 -04001534 iowrite32((u32)qp->tx_pkts, &hdr->ver);
Jon Mason282a2fe2013-02-12 09:52:50 -07001535
1536 if (!chan)
1537 goto err;
1538
1539 if (len < copy_bytes)
1540 goto err;
1541
1542 device = chan->device;
1543 dest = qp->tx_mw_phys + qp->tx_max_frame * qp->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001544 buff_off = (size_t)buf & ~PAGE_MASK;
1545 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001546
1547 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1548 goto err;
1549
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001550 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1551 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001552 goto err;
1553
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001554 unmap->len = len;
1555 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1556 buff_off, len, DMA_TO_DEVICE);
1557 if (dma_mapping_error(device->dev, unmap->addr[0]))
1558 goto err_get_unmap;
1559
1560 unmap->to_cnt = 1;
1561
Dave Jiang8c874cc2016-01-08 09:51:05 -07001562 for (retries = 0; retries < DMA_RETRIES; retries++) {
1563 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0],
1564 len, DMA_PREP_INTERRUPT);
1565 if (txd)
1566 break;
1567
1568 set_current_state(TASK_INTERRUPTIBLE);
1569 schedule_timeout(DMA_OUT_RESOURCE_TO);
1570 }
1571
1572 if (!txd) {
1573 qp->dma_tx_prep_err++;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001574 goto err_get_unmap;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001575 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001576
1577 txd->callback = ntb_tx_copy_callback;
1578 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001579 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001580
1581 cookie = dmaengine_submit(txd);
1582 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001583 goto err_set_unmap;
1584
1585 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001586
1587 dma_async_issue_pending(chan);
1588 qp->tx_async++;
1589
1590 return;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001591err_set_unmap:
1592 dmaengine_unmap_put(unmap);
1593err_get_unmap:
1594 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001595err:
1596 ntb_memcpy_tx(entry, offset);
1597 qp->tx_memcpy++;
1598}
1599
Jon Masonfce8a7b2012-11-16 19:27:12 -07001600static int ntb_process_tx(struct ntb_transport_qp *qp,
1601 struct ntb_queue_entry *entry)
1602{
Jon Mason793c20e2013-01-19 02:02:26 -07001603 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001604 qp->tx_ring_full++;
1605 return -EAGAIN;
1606 }
1607
Jon Masonef114ed2013-01-19 02:02:18 -07001608 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001609 if (qp->tx_handler)
Jon Mason179f9122015-12-18 13:22:37 -05001610 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001611
1612 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1613 &qp->tx_free_q);
1614 return 0;
1615 }
1616
Jon Mason282a2fe2013-02-12 09:52:50 -07001617 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001618
Jon Mason793c20e2013-01-19 02:02:26 -07001619 qp->tx_index++;
1620 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001621
1622 qp->tx_pkts++;
1623
1624 return 0;
1625}
1626
1627static void ntb_send_link_down(struct ntb_transport_qp *qp)
1628{
Allen Hubbee26a5842015-04-09 10:33:20 -04001629 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001630 struct ntb_queue_entry *entry;
1631 int i, rc;
1632
Allen Hubbee26a5842015-04-09 10:33:20 -04001633 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001634 return;
1635
Allen Hubbee22e0b92015-05-12 06:55:44 -04001636 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001637
1638 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001639 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001640 if (entry)
1641 break;
1642 msleep(100);
1643 }
1644
1645 if (!entry)
1646 return;
1647
1648 entry->cb_data = NULL;
1649 entry->buf = NULL;
1650 entry->len = 0;
1651 entry->flags = LINK_DOWN_FLAG;
1652
1653 rc = ntb_process_tx(qp, entry);
1654 if (rc)
1655 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1656 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001657
1658 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001659}
1660
Allen Hubbe1199aa62015-05-18 06:20:47 -04001661static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1662{
1663 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1664}
1665
Jon Masonfce8a7b2012-11-16 19:27:12 -07001666/**
1667 * ntb_transport_create_queue - Create a new NTB transport layer queue
1668 * @rx_handler: receive callback function
1669 * @tx_handler: transmit callback function
1670 * @event_handler: event callback function
1671 *
1672 * Create a new NTB transport layer queue and provide the queue with a callback
1673 * routine for both transmit and receive. The receive callback routine will be
1674 * used to pass up data when the transport has received it on the queue. The
1675 * transmit callback routine will be called when the transport has completed the
1676 * transmission of the data on the queue and the data is ready to be freed.
1677 *
1678 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1679 */
1680struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001681ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001682 const struct ntb_queue_handlers *handlers)
1683{
Allen Hubbee26a5842015-04-09 10:33:20 -04001684 struct ntb_dev *ndev;
1685 struct pci_dev *pdev;
1686 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001687 struct ntb_queue_entry *entry;
1688 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001689 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001690 unsigned int free_queue;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001691 dma_cap_mask_t dma_mask;
1692 int node;
Allen Hubbee26a5842015-04-09 10:33:20 -04001693 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001694
Allen Hubbee26a5842015-04-09 10:33:20 -04001695 ndev = dev_ntb(client_dev->parent);
1696 pdev = ndev->pdev;
1697 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001698
Allen Hubbe1199aa62015-05-18 06:20:47 -04001699 node = dev_to_node(&ndev->dev);
1700
Jon Masonfce8a7b2012-11-16 19:27:12 -07001701 free_queue = ffs(nt->qp_bitmap);
1702 if (!free_queue)
1703 goto err;
1704
1705 /* decrement free_queue to make it zero based */
1706 free_queue--;
1707
Allen Hubbee26a5842015-04-09 10:33:20 -04001708 qp = &nt->qp_vec[free_queue];
1709 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001710
Allen Hubbee26a5842015-04-09 10:33:20 -04001711 nt->qp_bitmap_free &= ~qp_bit;
1712
Jon Masonfce8a7b2012-11-16 19:27:12 -07001713 qp->cb_data = data;
1714 qp->rx_handler = handlers->rx_handler;
1715 qp->tx_handler = handlers->tx_handler;
1716 qp->event_handler = handlers->event_handler;
1717
Allen Hubbe1199aa62015-05-18 06:20:47 -04001718 dma_cap_zero(dma_mask);
1719 dma_cap_set(DMA_MEMCPY, dma_mask);
1720
Dave Jianga41ef052015-05-19 16:52:04 -04001721 if (use_dma) {
Dave Jiang569410ca2015-07-13 08:07:22 -04001722 qp->tx_dma_chan =
1723 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1724 (void *)(unsigned long)node);
1725 if (!qp->tx_dma_chan)
1726 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
1727
1728 qp->rx_dma_chan =
1729 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1730 (void *)(unsigned long)node);
1731 if (!qp->rx_dma_chan)
1732 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
Dave Jianga41ef052015-05-19 16:52:04 -04001733 } else {
Dave Jiang569410ca2015-07-13 08:07:22 -04001734 qp->tx_dma_chan = NULL;
1735 qp->rx_dma_chan = NULL;
Dave Jianga41ef052015-05-19 16:52:04 -04001736 }
Dave Jiang569410ca2015-07-13 08:07:22 -04001737
1738 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
1739 qp->tx_dma_chan ? "DMA" : "CPU");
1740
1741 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
1742 qp->rx_dma_chan ? "DMA" : "CPU");
Jon Mason282a2fe2013-02-12 09:52:50 -07001743
Jon Masonfce8a7b2012-11-16 19:27:12 -07001744 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001745 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001746 if (!entry)
1747 goto err1;
1748
Jon Mason282a2fe2013-02-12 09:52:50 -07001749 entry->qp = qp;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001750 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001751 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001752 }
Dave Jianga754a8f2016-04-08 10:49:06 -07001753 qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001754
Dave Jianga754a8f2016-04-08 10:49:06 -07001755 for (i = 0; i < qp->tx_max_entry; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001756 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001757 if (!entry)
1758 goto err2;
1759
Jon Mason282a2fe2013-02-12 09:52:50 -07001760 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001761 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001762 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001763 }
1764
Allen Hubbee26a5842015-04-09 10:33:20 -04001765 ntb_db_clear(qp->ndev, qp_bit);
1766 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001767
1768 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1769
1770 return qp;
1771
Jon Masonfce8a7b2012-11-16 19:27:12 -07001772err2:
Jon Masonf7667552013-01-19 02:02:24 -07001773 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001774 kfree(entry);
1775err1:
Dave Jianga754a8f2016-04-08 10:49:06 -07001776 qp->rx_alloc_entry = 0;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001777 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001778 kfree(entry);
Dave Jiang569410ca2015-07-13 08:07:22 -04001779 if (qp->tx_dma_chan)
1780 dma_release_channel(qp->tx_dma_chan);
1781 if (qp->rx_dma_chan)
1782 dma_release_channel(qp->rx_dma_chan);
Allen Hubbee26a5842015-04-09 10:33:20 -04001783 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001784err:
1785 return NULL;
1786}
1787EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1788
1789/**
1790 * ntb_transport_free_queue - Frees NTB transport queue
1791 * @qp: NTB queue to be freed
1792 *
1793 * Frees NTB transport queue
1794 */
1795void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1796{
Jon Mason186f27f2013-01-22 11:35:40 -07001797 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001798 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001799 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001800
1801 if (!qp)
1802 return;
1803
Allen Hubbee26a5842015-04-09 10:33:20 -04001804 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001805
Dave Jiange9021332016-02-23 09:11:36 -07001806 qp->active = false;
1807
Dave Jiang569410ca2015-07-13 08:07:22 -04001808 if (qp->tx_dma_chan) {
1809 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001810 /* Putting the dma_chan to NULL will force any new traffic to be
1811 * processed by the CPU instead of the DAM engine
1812 */
Dave Jiang569410ca2015-07-13 08:07:22 -04001813 qp->tx_dma_chan = NULL;
1814
1815 /* Try to be nice and wait for any queued DMA engine
1816 * transactions to process before smashing it with a rock
1817 */
1818 dma_sync_wait(chan, qp->last_cookie);
1819 dmaengine_terminate_all(chan);
1820 dma_release_channel(chan);
1821 }
1822
1823 if (qp->rx_dma_chan) {
1824 struct dma_chan *chan = qp->rx_dma_chan;
1825 /* Putting the dma_chan to NULL will force any new traffic to be
1826 * processed by the CPU instead of the DAM engine
1827 */
1828 qp->rx_dma_chan = NULL;
Jon Mason282a2fe2013-02-12 09:52:50 -07001829
1830 /* Try to be nice and wait for any queued DMA engine
1831 * transactions to process before smashing it with a rock
1832 */
1833 dma_sync_wait(chan, qp->last_cookie);
1834 dmaengine_terminate_all(chan);
Allen Hubbe1199aa62015-05-18 06:20:47 -04001835 dma_release_channel(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001836 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001837
Allen Hubbee26a5842015-04-09 10:33:20 -04001838 qp_bit = BIT_ULL(qp->qp_num);
1839
1840 ntb_db_set_mask(qp->ndev, qp_bit);
Dave Jiange9021332016-02-23 09:11:36 -07001841 tasklet_kill(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001842
Jon Mason282a2fe2013-02-12 09:52:50 -07001843 cancel_delayed_work_sync(&qp->link_work);
1844
Allen Hubbee26a5842015-04-09 10:33:20 -04001845 qp->cb_data = NULL;
1846 qp->rx_handler = NULL;
1847 qp->tx_handler = NULL;
1848 qp->event_handler = NULL;
1849
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001850 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001851 kfree(entry);
1852
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001853 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1854 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1855 kfree(entry);
1856 }
1857
1858 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1859 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001860 kfree(entry);
1861 }
1862
Jon Masonf7667552013-01-19 02:02:24 -07001863 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001864 kfree(entry);
1865
Allen Hubbe30a4bb12015-07-13 08:07:14 -04001866 qp->transport->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001867
1868 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1869}
1870EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1871
1872/**
1873 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1874 * @qp: NTB queue to be freed
1875 * @len: pointer to variable to write enqueued buffers length
1876 *
1877 * Dequeues unused buffers from receive queue. Should only be used during
1878 * shutdown of qp.
1879 *
1880 * RETURNS: NULL error value on error, or void* for success.
1881 */
1882void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1883{
1884 struct ntb_queue_entry *entry;
1885 void *buf;
1886
Allen Hubbee26a5842015-04-09 10:33:20 -04001887 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001888 return NULL;
1889
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001890 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001891 if (!entry)
1892 return NULL;
1893
1894 buf = entry->cb_data;
1895 *len = entry->len;
1896
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001897 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001898
1899 return buf;
1900}
1901EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1902
1903/**
1904 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1905 * @qp: NTB transport layer queue the entry is to be enqueued on
1906 * @cb: per buffer pointer for callback function to use
1907 * @data: pointer to data buffer that incoming packets will be copied into
1908 * @len: length of the data buffer
1909 *
1910 * Enqueue a new receive buffer onto the transport queue into which a NTB
1911 * payload can be received into.
1912 *
1913 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1914 */
1915int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1916 unsigned int len)
1917{
1918 struct ntb_queue_entry *entry;
1919
1920 if (!qp)
1921 return -EINVAL;
1922
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001923 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001924 if (!entry)
1925 return -ENOMEM;
1926
1927 entry->cb_data = cb;
1928 entry->buf = data;
1929 entry->len = len;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001930 entry->flags = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001931
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001932 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
1933
Dave Jiange9021332016-02-23 09:11:36 -07001934 if (qp->active)
1935 tasklet_schedule(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001936
1937 return 0;
1938}
1939EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1940
1941/**
1942 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1943 * @qp: NTB transport layer queue the entry is to be enqueued on
1944 * @cb: per buffer pointer for callback function to use
1945 * @data: pointer to data buffer that will be sent
1946 * @len: length of the data buffer
1947 *
1948 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07001949 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07001950 * serialize access to the qp.
1951 *
1952 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1953 */
1954int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1955 unsigned int len)
1956{
1957 struct ntb_queue_entry *entry;
1958 int rc;
1959
Allen Hubbee26a5842015-04-09 10:33:20 -04001960 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001961 return -EINVAL;
1962
1963 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001964 if (!entry) {
1965 qp->tx_err_no_buf++;
Dave Jiange74bfee2015-07-13 08:07:17 -04001966 return -EBUSY;
Jon Mason282a2fe2013-02-12 09:52:50 -07001967 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001968
1969 entry->cb_data = cb;
1970 entry->buf = data;
1971 entry->len = len;
1972 entry->flags = 0;
1973
1974 rc = ntb_process_tx(qp, entry);
1975 if (rc)
1976 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1977 &qp->tx_free_q);
1978
1979 return rc;
1980}
1981EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1982
1983/**
1984 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1985 * @qp: NTB transport layer queue to be enabled
1986 *
1987 * Notify NTB transport layer of client readiness to use queue
1988 */
1989void ntb_transport_link_up(struct ntb_transport_qp *qp)
1990{
1991 if (!qp)
1992 return;
1993
Allen Hubbee26a5842015-04-09 10:33:20 -04001994 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001995
Allen Hubbee26a5842015-04-09 10:33:20 -04001996 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001997 schedule_delayed_work(&qp->link_work, 0);
1998}
1999EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2000
2001/**
2002 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2003 * @qp: NTB transport layer queue to be disabled
2004 *
2005 * Notify NTB transport layer of client's desire to no longer receive data on
2006 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07002007 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07002008 */
2009void ntb_transport_link_down(struct ntb_transport_qp *qp)
2010{
Allen Hubbee26a5842015-04-09 10:33:20 -04002011 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002012
2013 if (!qp)
2014 return;
2015
Allen Hubbee26a5842015-04-09 10:33:20 -04002016 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002017
Allen Hubbee26a5842015-04-09 10:33:20 -04002018 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002019
Allen Hubbee26a5842015-04-09 10:33:20 -04002020 ntb_peer_spad_write(qp->ndev, QP_LINKS,
2021 val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07002022
Allen Hubbee26a5842015-04-09 10:33:20 -04002023 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002024 ntb_send_link_down(qp);
2025 else
2026 cancel_delayed_work_sync(&qp->link_work);
2027}
2028EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2029
2030/**
2031 * ntb_transport_link_query - Query transport link state
2032 * @qp: NTB transport layer queue to be queried
2033 *
2034 * Query connectivity to the remote system of the NTB transport queue
2035 *
2036 * RETURNS: true for link up or false for link down
2037 */
2038bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2039{
Jon Mason186f27f2013-01-22 11:35:40 -07002040 if (!qp)
2041 return false;
2042
Allen Hubbee26a5842015-04-09 10:33:20 -04002043 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002044}
2045EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2046
2047/**
2048 * ntb_transport_qp_num - Query the qp number
2049 * @qp: NTB transport layer queue to be queried
2050 *
2051 * Query qp number of the NTB transport queue
2052 *
2053 * RETURNS: a zero based number specifying the qp number
2054 */
2055unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2056{
Jon Mason186f27f2013-01-22 11:35:40 -07002057 if (!qp)
2058 return 0;
2059
Jon Masonfce8a7b2012-11-16 19:27:12 -07002060 return qp->qp_num;
2061}
2062EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2063
2064/**
2065 * ntb_transport_max_size - Query the max payload size of a qp
2066 * @qp: NTB transport layer queue to be queried
2067 *
2068 * Query the maximum payload size permissible on the given qp
2069 *
2070 * RETURNS: the max payload size of a qp
2071 */
Jon Masonef114ed2013-01-19 02:02:18 -07002072unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002073{
Dave Jiang04afde42015-09-17 13:27:04 -07002074 unsigned int max_size;
Dave Jiang569410ca2015-07-13 08:07:22 -04002075 unsigned int copy_align;
Dave Jiang04afde42015-09-17 13:27:04 -07002076 struct dma_chan *rx_chan, *tx_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002077
Jon Mason186f27f2013-01-22 11:35:40 -07002078 if (!qp)
2079 return 0;
2080
Dave Jiang04afde42015-09-17 13:27:04 -07002081 rx_chan = qp->rx_dma_chan;
2082 tx_chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002083
Dave Jiang04afde42015-09-17 13:27:04 -07002084 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2085 tx_chan ? tx_chan->device->copy_align : 0);
Dave Jiang569410ca2015-07-13 08:07:22 -04002086
Jon Mason282a2fe2013-02-12 09:52:50 -07002087 /* If DMA engine usage is possible, try to find the max size for that */
Dave Jiang04afde42015-09-17 13:27:04 -07002088 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2089 max_size = round_down(max_size, 1 << copy_align);
Jon Mason282a2fe2013-02-12 09:52:50 -07002090
Dave Jiang04afde42015-09-17 13:27:04 -07002091 return max_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002092}
2093EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04002094
Dave Jiange74bfee2015-07-13 08:07:17 -04002095unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2096{
2097 unsigned int head = qp->tx_index;
2098 unsigned int tail = qp->remote_rx_info->entry;
2099
2100 return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2101}
2102EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2103
Allen Hubbee26a5842015-04-09 10:33:20 -04002104static void ntb_transport_doorbell_callback(void *data, int vector)
2105{
2106 struct ntb_transport_ctx *nt = data;
2107 struct ntb_transport_qp *qp;
2108 u64 db_bits;
2109 unsigned int qp_num;
2110
2111 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2112 ntb_db_vector_mask(nt->ndev, vector));
2113
2114 while (db_bits) {
2115 qp_num = __ffs(db_bits);
2116 qp = &nt->qp_vec[qp_num];
2117
Dave Jiange9021332016-02-23 09:11:36 -07002118 if (qp->active)
2119 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04002120
2121 db_bits &= ~BIT_ULL(qp_num);
2122 }
2123}
2124
2125static const struct ntb_ctx_ops ntb_transport_ops = {
2126 .link_event = ntb_transport_event_callback,
2127 .db_event = ntb_transport_doorbell_callback,
2128};
2129
2130static struct ntb_client ntb_transport_client = {
2131 .ops = {
2132 .probe = ntb_transport_probe,
2133 .remove = ntb_transport_free,
2134 },
2135};
2136
2137static int __init ntb_transport_init(void)
2138{
2139 int rc;
2140
Dave Jiang7eb38782015-06-15 08:21:33 -04002141 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2142
Allen Hubbee26a5842015-04-09 10:33:20 -04002143 if (debugfs_initialized())
2144 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2145
2146 rc = bus_register(&ntb_transport_bus);
2147 if (rc)
2148 goto err_bus;
2149
2150 rc = ntb_register_client(&ntb_transport_client);
2151 if (rc)
2152 goto err_client;
2153
2154 return 0;
2155
2156err_client:
2157 bus_unregister(&ntb_transport_bus);
2158err_bus:
2159 debugfs_remove_recursive(nt_debugfs_dir);
2160 return rc;
2161}
2162module_init(ntb_transport_init);
2163
2164static void __exit ntb_transport_exit(void)
2165{
2166 debugfs_remove_recursive(nt_debugfs_dir);
2167
2168 ntb_unregister_client(&ntb_transport_client);
2169 bus_unregister(&ntb_transport_bus);
2170}
2171module_exit(ntb_transport_exit);