blob: 25e973ff64cfba63a323027a9acaa1347e2c9527 [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;
Jon Mason282a2fe2013-02-12 09:52:50 -0700122 struct dma_chan *dma_chan;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700123
124 bool client_ready;
Allen Hubbee26a5842015-04-09 10:33:20 -0400125 bool link_is_up;
126
Jon Masonfce8a7b2012-11-16 19:27:12 -0700127 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400128 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700129
Jon Mason74465642013-01-21 15:28:52 -0700130 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -0700131 struct ntb_rx_info *remote_rx_info;
132
Jon Mason53ca4fe2013-11-26 11:21:50 -0700133 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
134 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700135 struct list_head tx_free_q;
136 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700137 void __iomem *tx_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700138 dma_addr_t tx_mw_phys;
Jon Mason793c20e2013-01-19 02:02:26 -0700139 unsigned int tx_index;
140 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700141 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700142
Jon Mason53ca4fe2013-11-26 11:21:50 -0700143 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
144 void *data, int len);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400145 struct list_head rx_post_q;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700146 struct list_head rx_pend_q;
147 struct list_head rx_free_q;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400148 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
149 spinlock_t ntb_rx_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700150 void *rx_buff;
151 unsigned int rx_index;
152 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700153 unsigned int rx_max_frame;
Jon Mason282a2fe2013-02-12 09:52:50 -0700154 dma_cookie_t last_cookie;
Allen Hubbee26a5842015-04-09 10:33:20 -0400155 struct tasklet_struct rxc_db_work;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700156
Jon Mason53ca4fe2013-11-26 11:21:50 -0700157 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700158 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700159 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700160
161 struct dentry *debugfs_dir;
162 struct dentry *debugfs_stats;
163
164 /* Stats */
165 u64 rx_bytes;
166 u64 rx_pkts;
167 u64 rx_ring_empty;
168 u64 rx_err_no_buf;
169 u64 rx_err_oflow;
170 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700171 u64 rx_memcpy;
172 u64 rx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700173 u64 tx_bytes;
174 u64 tx_pkts;
175 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700176 u64 tx_err_no_buf;
177 u64 tx_memcpy;
178 u64 tx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700179};
180
181struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400182 phys_addr_t phys_addr;
183 resource_size_t phys_size;
184 resource_size_t xlat_align;
185 resource_size_t xlat_align_size;
186 void __iomem *vbase;
187 size_t xlat_size;
188 size_t buff_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700189 void *virt_addr;
190 dma_addr_t dma_addr;
191};
192
193struct ntb_transport_client_dev {
194 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400195 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700196 struct device dev;
197};
198
Allen Hubbee26a5842015-04-09 10:33:20 -0400199struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700200 struct list_head entry;
201 struct list_head client_devs;
202
Allen Hubbee26a5842015-04-09 10:33:20 -0400203 struct ntb_dev *ndev;
204
205 struct ntb_transport_mw *mw_vec;
206 struct ntb_transport_qp *qp_vec;
207 unsigned int mw_count;
208 unsigned int qp_count;
209 u64 qp_bitmap;
210 u64 qp_bitmap_free;
211
212 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700213 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700214 struct work_struct link_cleanup;
Dave Jiangc8650fd2015-07-13 08:07:09 -0400215
216 struct dentry *debugfs_node_dir;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700217};
218
219enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400220 DESC_DONE_FLAG = BIT(0),
221 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700222};
223
224struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700225 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700226 unsigned int len;
227 unsigned int flags;
228};
229
230enum {
231 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700232 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700233 NUM_QPS,
234 NUM_MWS,
235 MW0_SZ_HIGH,
236 MW0_SZ_LOW,
237 MW1_SZ_HIGH,
238 MW1_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700239 MAX_SPAD,
240};
241
Allen Hubbee26a5842015-04-09 10:33:20 -0400242#define dev_client_dev(__dev) \
243 container_of((__dev), struct ntb_transport_client_dev, dev)
244
245#define drv_client(__drv) \
246 container_of((__drv), struct ntb_transport_client, driver)
247
248#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700249#define NTB_QP_DEF_NUM_ENTRIES 100
250#define NTB_LINK_DOWN_TIMEOUT 10
251
Allen Hubbee26a5842015-04-09 10:33:20 -0400252static void ntb_transport_rxc_db(unsigned long data);
253static const struct ntb_ctx_ops ntb_transport_ops;
254static struct ntb_client ntb_transport_client;
255
256static int ntb_transport_bus_match(struct device *dev,
257 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700258{
259 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
260}
261
Allen Hubbee26a5842015-04-09 10:33:20 -0400262static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700263{
Allen Hubbee26a5842015-04-09 10:33:20 -0400264 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700265 int rc = -EINVAL;
266
267 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400268
269 client = drv_client(dev->driver);
270 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700271 if (rc)
272 put_device(dev);
273
274 return rc;
275}
276
Allen Hubbee26a5842015-04-09 10:33:20 -0400277static int ntb_transport_bus_remove(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700278{
Allen Hubbee26a5842015-04-09 10:33:20 -0400279 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700280
Allen Hubbee26a5842015-04-09 10:33:20 -0400281 client = drv_client(dev->driver);
282 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700283
284 put_device(dev);
285
286 return 0;
287}
288
Allen Hubbee26a5842015-04-09 10:33:20 -0400289static struct bus_type ntb_transport_bus = {
290 .name = "ntb_transport",
291 .match = ntb_transport_bus_match,
292 .probe = ntb_transport_bus_probe,
293 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700294};
295
296static LIST_HEAD(ntb_transport_list);
297
Allen Hubbee26a5842015-04-09 10:33:20 -0400298static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700299{
Jon Masonfce8a7b2012-11-16 19:27:12 -0700300 list_add(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700301 return 0;
302}
303
Allen Hubbee26a5842015-04-09 10:33:20 -0400304static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700305{
306 struct ntb_transport_client_dev *client_dev, *cd;
307
308 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
309 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
310 dev_name(&client_dev->dev));
311 list_del(&client_dev->entry);
312 device_unregister(&client_dev->dev);
313 }
314
315 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700316}
317
Allen Hubbee26a5842015-04-09 10:33:20 -0400318static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700319{
320 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700321
Allen Hubbee26a5842015-04-09 10:33:20 -0400322 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700323 kfree(client_dev);
324}
325
326/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400327 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700328 * @device_name: Name of NTB client device
329 *
330 * Unregister an NTB client device with the NTB transport layer
331 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400332void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700333{
334 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400335 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700336
337 list_for_each_entry(nt, &ntb_transport_list, entry)
338 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
339 if (!strncmp(dev_name(&client->dev), device_name,
340 strlen(device_name))) {
341 list_del(&client->entry);
342 device_unregister(&client->dev);
343 }
344}
Allen Hubbee26a5842015-04-09 10:33:20 -0400345EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700346
347/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400348 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700349 * @device_name: Name of NTB client device
350 *
351 * Register an NTB client device with the NTB transport layer
352 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400353int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700354{
355 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400356 struct ntb_transport_ctx *nt;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400357 int node;
Jon Mason8b19d452013-04-26 14:51:57 -0700358 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700359
Jon Mason8222b402013-01-19 02:02:16 -0700360 if (list_empty(&ntb_transport_list))
361 return -ENODEV;
362
Jon Masonfce8a7b2012-11-16 19:27:12 -0700363 list_for_each_entry(nt, &ntb_transport_list, entry) {
364 struct device *dev;
365
Allen Hubbe1199aa62015-05-18 06:20:47 -0400366 node = dev_to_node(&nt->ndev->dev);
367
368 client_dev = kzalloc_node(sizeof(*client_dev),
369 GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700370 if (!client_dev) {
371 rc = -ENOMEM;
372 goto err;
373 }
374
375 dev = &client_dev->dev;
376
377 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700378 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400379 dev->bus = &ntb_transport_bus;
380 dev->release = ntb_transport_client_release;
381 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700382
383 rc = device_register(dev);
384 if (rc) {
385 kfree(client_dev);
386 goto err;
387 }
388
389 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700390 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700391 }
392
393 return 0;
394
395err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400396 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700397
398 return rc;
399}
Allen Hubbee26a5842015-04-09 10:33:20 -0400400EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700401
402/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400403 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700404 * @drv: NTB client driver to be registered
405 *
406 * Register an NTB client driver with the NTB transport layer
407 *
408 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
409 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400410int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700411{
Allen Hubbee26a5842015-04-09 10:33:20 -0400412 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700413
Jon Mason8222b402013-01-19 02:02:16 -0700414 if (list_empty(&ntb_transport_list))
415 return -ENODEV;
416
Jon Masonfce8a7b2012-11-16 19:27:12 -0700417 return driver_register(&drv->driver);
418}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400419EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700420
421/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400422 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700423 * @drv: NTB client driver to be unregistered
424 *
425 * Unregister an NTB client driver with the NTB transport layer
426 *
427 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
428 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400429void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700430{
431 driver_unregister(&drv->driver);
432}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400433EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700434
Jon Masonfce8a7b2012-11-16 19:27:12 -0700435static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
436 loff_t *offp)
437{
438 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700439 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700440 ssize_t ret, out_offset, out_count;
441
Jon Mason282a2fe2013-02-12 09:52:50 -0700442 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700443
444 buf = kmalloc(out_count, GFP_KERNEL);
445 if (!buf)
446 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700447
448 qp = filp->private_data;
449 out_offset = 0;
450 out_offset += snprintf(buf + out_offset, out_count - out_offset,
451 "NTB QP stats\n");
452 out_offset += snprintf(buf + out_offset, out_count - out_offset,
453 "rx_bytes - \t%llu\n", qp->rx_bytes);
454 out_offset += snprintf(buf + out_offset, out_count - out_offset,
455 "rx_pkts - \t%llu\n", qp->rx_pkts);
456 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700457 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
458 out_offset += snprintf(buf + out_offset, out_count - out_offset,
459 "rx_async - \t%llu\n", qp->rx_async);
460 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700461 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
462 out_offset += snprintf(buf + out_offset, out_count - out_offset,
463 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
464 out_offset += snprintf(buf + out_offset, out_count - out_offset,
465 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
466 out_offset += snprintf(buf + out_offset, out_count - out_offset,
467 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
468 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700469 "rx_buff - \t%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700470 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700471 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700472 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700473 "rx_max_entry - \t%u\n", qp->rx_max_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700474
475 out_offset += snprintf(buf + out_offset, out_count - out_offset,
476 "tx_bytes - \t%llu\n", qp->tx_bytes);
477 out_offset += snprintf(buf + out_offset, out_count - out_offset,
478 "tx_pkts - \t%llu\n", qp->tx_pkts);
479 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700480 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
481 out_offset += snprintf(buf + out_offset, out_count - out_offset,
482 "tx_async - \t%llu\n", qp->tx_async);
483 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700484 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
485 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700486 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
487 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700488 "tx_mw - \t%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700489 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700490 "tx_index - \t%u\n", qp->tx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700491 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700492 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700493
494 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Allen Hubbee26a5842015-04-09 10:33:20 -0400495 "\nQP Link %s\n",
496 qp->link_is_up ? "Up" : "Down");
Jon Masond7237e22013-01-19 02:02:25 -0700497 if (out_offset > out_count)
498 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700499
500 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700501 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700502 return ret;
503}
504
505static const struct file_operations ntb_qp_debugfs_stats = {
506 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700507 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700508 .read = debugfs_read,
509};
510
511static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
512 struct list_head *list)
513{
514 unsigned long flags;
515
516 spin_lock_irqsave(lock, flags);
517 list_add_tail(entry, list);
518 spin_unlock_irqrestore(lock, flags);
519}
520
521static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700522 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700523{
524 struct ntb_queue_entry *entry;
525 unsigned long flags;
526
527 spin_lock_irqsave(lock, flags);
528 if (list_empty(list)) {
529 entry = NULL;
530 goto out;
531 }
532 entry = list_first_entry(list, struct ntb_queue_entry, entry);
533 list_del(&entry->entry);
534out:
535 spin_unlock_irqrestore(lock, flags);
536
537 return entry;
538}
539
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400540static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
541 struct list_head *list,
542 struct list_head *to_list)
543{
544 struct ntb_queue_entry *entry;
545 unsigned long flags;
546
547 spin_lock_irqsave(lock, flags);
548
549 if (list_empty(list)) {
550 entry = NULL;
551 } else {
552 entry = list_first_entry(list, struct ntb_queue_entry, entry);
553 list_move_tail(&entry->entry, to_list);
554 }
555
556 spin_unlock_irqrestore(lock, flags);
557
558 return entry;
559}
560
Allen Hubbee26a5842015-04-09 10:33:20 -0400561static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
562 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700563{
Allen Hubbee26a5842015-04-09 10:33:20 -0400564 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
565 struct ntb_transport_mw *mw;
Jon Masonef114ed2013-01-19 02:02:18 -0700566 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400567 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700568 unsigned int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700569
Allen Hubbee26a5842015-04-09 10:33:20 -0400570 mw_count = nt->mw_count;
571 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700572
Allen Hubbee26a5842015-04-09 10:33:20 -0400573 mw_num = QP_TO_MW(nt, qp_num);
574 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700575
Allen Hubbee26a5842015-04-09 10:33:20 -0400576 if (!mw->virt_addr)
577 return -ENOMEM;
578
579 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
580 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700581 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400582 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700583
Allen Hubbee26a5842015-04-09 10:33:20 -0400584 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
585 qp->rx_buff = mw->virt_addr + rx_size * qp_num / mw_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700586 rx_size -= sizeof(struct ntb_rx_info);
587
Jon Mason282a2fe2013-02-12 09:52:50 -0700588 qp->remote_rx_info = qp->rx_buff + rx_size;
589
Jon Masonc9d534c2013-02-01 15:45:16 -0700590 /* Due to housekeeping, there must be atleast 2 buffs */
591 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700592 qp->rx_max_entry = rx_size / qp->rx_max_frame;
593 qp->rx_index = 0;
594
Jon Masonc9d534c2013-02-01 15:45:16 -0700595 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700596
Jon Masonef114ed2013-01-19 02:02:18 -0700597 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700598 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400599 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
600 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700601 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700602 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700603
604 qp->rx_pkts = 0;
605 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700606 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400607
608 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700609}
610
Allen Hubbee26a5842015-04-09 10:33:20 -0400611static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700612{
Allen Hubbee26a5842015-04-09 10:33:20 -0400613 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
614 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700615
616 if (!mw->virt_addr)
617 return;
618
Allen Hubbee26a5842015-04-09 10:33:20 -0400619 ntb_mw_clear_trans(nt->ndev, num_mw);
620 dma_free_coherent(&pdev->dev, mw->buff_size,
621 mw->virt_addr, mw->dma_addr);
622 mw->xlat_size = 0;
623 mw->buff_size = 0;
Jon Mason113fc502013-01-30 11:40:52 -0700624 mw->virt_addr = NULL;
625}
626
Allen Hubbee26a5842015-04-09 10:33:20 -0400627static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
628 unsigned int size)
Jon Masonb77b2632013-02-01 15:25:37 -0700629{
Allen Hubbee26a5842015-04-09 10:33:20 -0400630 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
631 struct pci_dev *pdev = nt->ndev->pdev;
632 unsigned int xlat_size, buff_size;
633 int rc;
634
635 xlat_size = round_up(size, mw->xlat_align_size);
636 buff_size = round_up(size, mw->xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700637
638 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400639 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700640 return 0;
641
Allen Hubbee26a5842015-04-09 10:33:20 -0400642 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700643 ntb_free_mw(nt, num_mw);
644
Allen Hubbee26a5842015-04-09 10:33:20 -0400645 /* Alloc memory for receiving data. Must be aligned */
646 mw->xlat_size = xlat_size;
647 mw->buff_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700648
Allen Hubbee26a5842015-04-09 10:33:20 -0400649 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
650 &mw->dma_addr, GFP_KERNEL);
Jon Masonb77b2632013-02-01 15:25:37 -0700651 if (!mw->virt_addr) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400652 mw->xlat_size = 0;
653 mw->buff_size = 0;
654 dev_err(&pdev->dev, "Unable to alloc MW buff of size %d\n",
655 buff_size);
Jon Masonb77b2632013-02-01 15:25:37 -0700656 return -ENOMEM;
657 }
658
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700659 /*
660 * we must ensure that the memory address allocated is BAR size
661 * aligned in order for the XLAT register to take the value. This
662 * is a requirement of the hardware. It is recommended to setup CMA
663 * for BAR sizes equal or greater than 4MB.
664 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400665 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
666 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700667 &mw->dma_addr);
668 ntb_free_mw(nt, num_mw);
669 return -ENOMEM;
670 }
671
Jon Masonb77b2632013-02-01 15:25:37 -0700672 /* Notify HW the memory location of the receive buffer */
Allen Hubbee26a5842015-04-09 10:33:20 -0400673 rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
674 if (rc) {
675 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
676 ntb_free_mw(nt, num_mw);
677 return -EIO;
678 }
Jon Masonb77b2632013-02-01 15:25:37 -0700679
680 return 0;
681}
682
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400683static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
684{
685 qp->link_is_up = false;
686
687 qp->tx_index = 0;
688 qp->rx_index = 0;
689 qp->rx_bytes = 0;
690 qp->rx_pkts = 0;
691 qp->rx_ring_empty = 0;
692 qp->rx_err_no_buf = 0;
693 qp->rx_err_oflow = 0;
694 qp->rx_err_ver = 0;
695 qp->rx_memcpy = 0;
696 qp->rx_async = 0;
697 qp->tx_bytes = 0;
698 qp->tx_pkts = 0;
699 qp->tx_ring_full = 0;
700 qp->tx_err_no_buf = 0;
701 qp->tx_memcpy = 0;
702 qp->tx_async = 0;
703}
704
Jon Masonfca4d512013-09-09 13:39:55 -0700705static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700706{
Allen Hubbee26a5842015-04-09 10:33:20 -0400707 struct ntb_transport_ctx *nt = qp->transport;
708 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700709
Allen Hubbee22e0b92015-05-12 06:55:44 -0400710 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400711
712 cancel_delayed_work_sync(&qp->link_work);
713 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400714
715 if (qp->event_handler)
716 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700717}
718
719static void ntb_qp_link_cleanup_work(struct work_struct *work)
720{
721 struct ntb_transport_qp *qp = container_of(work,
722 struct ntb_transport_qp,
723 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400724 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700725
726 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700727
Allen Hubbee26a5842015-04-09 10:33:20 -0400728 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700729 schedule_delayed_work(&qp->link_work,
730 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
731}
732
Jon Mason7b4f2d32013-01-19 02:02:19 -0700733static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700734{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700735 schedule_work(&qp->link_cleanup);
736}
737
Allen Hubbee26a5842015-04-09 10:33:20 -0400738static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700739{
Allen Hubbee26a5842015-04-09 10:33:20 -0400740 struct ntb_transport_qp *qp;
741 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700742 int i;
743
Allen Hubbee26a5842015-04-09 10:33:20 -0400744 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700745
Allen Hubbee26a5842015-04-09 10:33:20 -0400746 /* Pass along the info to any clients */
747 for (i = 0; i < nt->qp_count; i++)
748 if (qp_bitmap_alloc & BIT_ULL(i)) {
749 qp = &nt->qp_vec[i];
750 ntb_qp_link_cleanup(qp);
751 cancel_work_sync(&qp->link_cleanup);
752 cancel_delayed_work_sync(&qp->link_work);
753 }
754
755 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700756 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700757
Jon Masonfce8a7b2012-11-16 19:27:12 -0700758 /* The scratchpad registers keep the values if the remote side
759 * goes down, blast them now to give them a sane value the next
760 * time they are accessed
761 */
762 for (i = 0; i < MAX_SPAD; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400763 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700764}
765
Jon Masonfca4d512013-09-09 13:39:55 -0700766static void ntb_transport_link_cleanup_work(struct work_struct *work)
767{
Allen Hubbee26a5842015-04-09 10:33:20 -0400768 struct ntb_transport_ctx *nt =
769 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700770
771 ntb_transport_link_cleanup(nt);
772}
773
Allen Hubbee26a5842015-04-09 10:33:20 -0400774static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700775{
Allen Hubbee26a5842015-04-09 10:33:20 -0400776 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700777
Allen Hubbee26a5842015-04-09 10:33:20 -0400778 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700779 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400780 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700781 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700782}
783
784static void ntb_transport_link_work(struct work_struct *work)
785{
Allen Hubbee26a5842015-04-09 10:33:20 -0400786 struct ntb_transport_ctx *nt =
787 container_of(work, struct ntb_transport_ctx, link_work.work);
788 struct ntb_dev *ndev = nt->ndev;
789 struct pci_dev *pdev = ndev->pdev;
790 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700791 u32 val;
Allen Hubbee26a5842015-04-09 10:33:20 -0400792 int rc, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700793
Jon Mason113fc502013-01-30 11:40:52 -0700794 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400795 for (i = 0; i < nt->mw_count; i++) {
796 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700797
Allen Hubbee26a5842015-04-09 10:33:20 -0400798 if (max_mw_size && size > max_mw_size)
799 size = max_mw_size;
800
801 spad = MW0_SZ_HIGH + (i * 2);
802 ntb_peer_spad_write(ndev, spad, (u32)(size >> 32));
803
804 spad = MW0_SZ_LOW + (i * 2);
805 ntb_peer_spad_write(ndev, spad, (u32)size);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700806 }
807
Allen Hubbee26a5842015-04-09 10:33:20 -0400808 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700809
Allen Hubbee26a5842015-04-09 10:33:20 -0400810 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700811
Allen Hubbee26a5842015-04-09 10:33:20 -0400812 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700813
814 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400815 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400816 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700817 if (val != NTB_TRANSPORT_VERSION)
818 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700819
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400820 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700821 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400822 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700823 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700824
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400825 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700826 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400827 if (val != nt->mw_count)
828 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700829
Allen Hubbee26a5842015-04-09 10:33:20 -0400830 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700831 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700832
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400833 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400834 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700835
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400836 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700837 val64 |= val;
838
Allen Hubbee26a5842015-04-09 10:33:20 -0400839 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700840
841 rc = ntb_set_mw(nt, i, val64);
842 if (rc)
843 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700844 }
845
Allen Hubbee26a5842015-04-09 10:33:20 -0400846 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700847
Allen Hubbee26a5842015-04-09 10:33:20 -0400848 for (i = 0; i < nt->qp_count; i++) {
849 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700850
851 ntb_transport_setup_qp_mw(nt, i);
852
Allen Hubbee26a5842015-04-09 10:33:20 -0400853 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700854 schedule_delayed_work(&qp->link_work, 0);
855 }
856
857 return;
858
Jon Mason113fc502013-01-30 11:40:52 -0700859out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400860 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700861 ntb_free_mw(nt, i);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700862out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400863 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700864 schedule_delayed_work(&nt->link_work,
865 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
866}
867
868static void ntb_qp_link_work(struct work_struct *work)
869{
870 struct ntb_transport_qp *qp = container_of(work,
871 struct ntb_transport_qp,
872 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400873 struct pci_dev *pdev = qp->ndev->pdev;
874 struct ntb_transport_ctx *nt = qp->transport;
875 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700876
Allen Hubbee26a5842015-04-09 10:33:20 -0400877 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700878
Allen Hubbee26a5842015-04-09 10:33:20 -0400879 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700880
Allen Hubbee26a5842015-04-09 10:33:20 -0400881 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700882
883 /* query remote spad for qp ready bits */
Allen Hubbee26a5842015-04-09 10:33:20 -0400884 ntb_peer_spad_read(nt->ndev, QP_LINKS);
Allen Hubbe28762282015-05-11 10:08:26 -0400885 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700886
887 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400888 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700889 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400890 qp->link_is_up = true;
891
Jon Masonfce8a7b2012-11-16 19:27:12 -0700892 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400893 qp->event_handler(qp->cb_data, qp->link_is_up);
894 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700895 schedule_delayed_work(&qp->link_work,
896 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
897}
898
Allen Hubbee26a5842015-04-09 10:33:20 -0400899static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700900 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700901{
902 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400903 struct ntb_transport_mw *mw;
904 phys_addr_t mw_base;
905 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700906 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400907 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700908 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700909
Allen Hubbee26a5842015-04-09 10:33:20 -0400910 mw_count = nt->mw_count;
911 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700912
Allen Hubbee26a5842015-04-09 10:33:20 -0400913 mw_num = QP_TO_MW(nt, qp_num);
914 mw = &nt->mw_vec[mw_num];
915
916 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700917 qp->qp_num = qp_num;
918 qp->transport = nt;
919 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400920 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700921 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400922 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700923
Allen Hubbee26a5842015-04-09 10:33:20 -0400924 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
925 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -0700926 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400927 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -0700928
Allen Hubbee26a5842015-04-09 10:33:20 -0400929 mw_base = nt->mw_vec[mw_num].phys_addr;
930 mw_size = nt->mw_vec[mw_num].phys_size;
931
932 tx_size = (unsigned int)mw_size / num_qps_mw;
933 qp_offset = tx_size * qp_num / mw_count;
934
935 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -0700936 if (!qp->tx_mw)
937 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -0700938
Allen Hubbee26a5842015-04-09 10:33:20 -0400939 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -0700940 if (!qp->tx_mw_phys)
941 return -EINVAL;
942
943 tx_size -= sizeof(struct ntb_rx_info);
944 qp->rx_info = qp->tx_mw + tx_size;
945
Jon Masonc9d534c2013-02-01 15:45:16 -0700946 /* Due to housekeeping, there must be atleast 2 buffs */
947 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700948 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -0700949
Dave Jiangc8650fd2015-07-13 08:07:09 -0400950 if (nt->debugfs_node_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700951 char debugfs_name[4];
952
953 snprintf(debugfs_name, 4, "qp%d", qp_num);
954 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Dave Jiangc8650fd2015-07-13 08:07:09 -0400955 nt->debugfs_node_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700956
957 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
958 qp->debugfs_dir, qp,
959 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -0400960 } else {
961 qp->debugfs_dir = NULL;
962 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700963 }
964
965 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -0700966 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700967
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400968 spin_lock_init(&qp->ntb_rx_q_lock);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700969 spin_lock_init(&qp->ntb_tx_free_q_lock);
970
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400971 INIT_LIST_HEAD(&qp->rx_post_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700972 INIT_LIST_HEAD(&qp->rx_pend_q);
973 INIT_LIST_HEAD(&qp->rx_free_q);
974 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -0700975
Allen Hubbee26a5842015-04-09 10:33:20 -0400976 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
977 (unsigned long)qp);
978
Jon Mason282a2fe2013-02-12 09:52:50 -0700979 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700980}
981
Allen Hubbee26a5842015-04-09 10:33:20 -0400982static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700983{
Allen Hubbee26a5842015-04-09 10:33:20 -0400984 struct ntb_transport_ctx *nt;
985 struct ntb_transport_mw *mw;
986 unsigned int mw_count, qp_count;
987 u64 qp_bitmap;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400988 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700989 int rc, i;
990
Allen Hubbee26a5842015-04-09 10:33:20 -0400991 if (ntb_db_is_unsafe(ndev))
992 dev_dbg(&ndev->dev,
993 "doorbell is unsafe, proceed anyway...\n");
994 if (ntb_spad_is_unsafe(ndev))
995 dev_dbg(&ndev->dev,
996 "scratchpad is unsafe, proceed anyway...\n");
997
Allen Hubbe1199aa62015-05-18 06:20:47 -0400998 node = dev_to_node(&ndev->dev);
999
1000 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001001 if (!nt)
1002 return -ENOMEM;
1003
Allen Hubbee26a5842015-04-09 10:33:20 -04001004 nt->ndev = ndev;
1005
1006 mw_count = ntb_mw_count(ndev);
1007
1008 nt->mw_count = mw_count;
1009
Allen Hubbe1199aa62015-05-18 06:20:47 -04001010 nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1011 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001012 if (!nt->mw_vec) {
1013 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001014 goto err;
1015 }
1016
Allen Hubbee26a5842015-04-09 10:33:20 -04001017 for (i = 0; i < mw_count; i++) {
1018 mw = &nt->mw_vec[i];
1019
1020 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
1021 &mw->xlat_align, &mw->xlat_align_size);
1022 if (rc)
1023 goto err1;
1024
Dave Jiang06917f72015-05-19 16:45:46 -04001025 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001026 if (!mw->vbase) {
1027 rc = -ENOMEM;
1028 goto err1;
1029 }
1030
1031 mw->buff_size = 0;
1032 mw->xlat_size = 0;
1033 mw->virt_addr = NULL;
1034 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -07001035 }
1036
Allen Hubbee26a5842015-04-09 10:33:20 -04001037 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001038
Allen Hubbee26a5842015-04-09 10:33:20 -04001039 qp_count = ilog2(qp_bitmap);
1040 if (max_num_clients && max_num_clients < qp_count)
1041 qp_count = max_num_clients;
1042 else if (mw_count < qp_count)
1043 qp_count = mw_count;
1044
1045 qp_bitmap &= BIT_ULL(qp_count) - 1;
1046
1047 nt->qp_count = qp_count;
1048 nt->qp_bitmap = qp_bitmap;
1049 nt->qp_bitmap_free = qp_bitmap;
1050
Allen Hubbe1199aa62015-05-18 06:20:47 -04001051 nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1052 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001053 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001054 rc = -ENOMEM;
Jon Mason948d3a62013-04-18 17:07:36 -07001055 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001056 }
1057
Dave Jiangc8650fd2015-07-13 08:07:09 -04001058 if (nt_debugfs_dir) {
1059 nt->debugfs_node_dir =
1060 debugfs_create_dir(pci_name(ndev->pdev),
1061 nt_debugfs_dir);
1062 }
1063
Allen Hubbee26a5842015-04-09 10:33:20 -04001064 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001065 rc = ntb_transport_init_queue(nt, i);
1066 if (rc)
1067 goto err3;
1068 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001069
1070 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001071 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001072
Allen Hubbee26a5842015-04-09 10:33:20 -04001073 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001074 if (rc)
Jon Mason948d3a62013-04-18 17:07:36 -07001075 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001076
1077 INIT_LIST_HEAD(&nt->client_devs);
1078 rc = ntb_bus_init(nt);
1079 if (rc)
Jon Mason948d3a62013-04-18 17:07:36 -07001080 goto err4;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001081
Allen Hubbee26a5842015-04-09 10:33:20 -04001082 nt->link_is_up = false;
1083 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1084 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001085
1086 return 0;
1087
Jon Mason948d3a62013-04-18 17:07:36 -07001088err4:
Allen Hubbee26a5842015-04-09 10:33:20 -04001089 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001090err3:
Allen Hubbee26a5842015-04-09 10:33:20 -04001091 kfree(nt->qp_vec);
Jon Mason948d3a62013-04-18 17:07:36 -07001092err2:
Allen Hubbee26a5842015-04-09 10:33:20 -04001093 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001094err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001095 while (i--) {
1096 mw = &nt->mw_vec[i];
1097 iounmap(mw->vbase);
1098 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001099err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001100 kfree(nt);
1101 return rc;
1102}
1103
Allen Hubbee26a5842015-04-09 10:33:20 -04001104static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001105{
Allen Hubbee26a5842015-04-09 10:33:20 -04001106 struct ntb_transport_ctx *nt = ndev->ctx;
1107 struct ntb_transport_qp *qp;
1108 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001109 int i;
1110
Jon Masonfca4d512013-09-09 13:39:55 -07001111 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001112 cancel_work_sync(&nt->link_cleanup);
1113 cancel_delayed_work_sync(&nt->link_work);
1114
1115 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001116
1117 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001118 for (i = 0; i < nt->qp_count; i++) {
1119 qp = &nt->qp_vec[i];
1120 if (qp_bitmap_alloc & BIT_ULL(i))
1121 ntb_transport_free_queue(qp);
1122 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001123 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001124
Allen Hubbee26a5842015-04-09 10:33:20 -04001125 ntb_link_disable(ndev);
1126 ntb_clear_ctx(ndev);
1127
Jon Masonfce8a7b2012-11-16 19:27:12 -07001128 ntb_bus_remove(nt);
1129
Allen Hubbee26a5842015-04-09 10:33:20 -04001130 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001131 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001132 iounmap(nt->mw_vec[i].vbase);
1133 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001134
Allen Hubbee26a5842015-04-09 10:33:20 -04001135 kfree(nt->qp_vec);
1136 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001137 kfree(nt);
1138}
1139
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001140static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1141{
1142 struct ntb_queue_entry *entry;
1143 void *cb_data;
1144 unsigned int len;
1145 unsigned long irqflags;
1146
1147 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1148
1149 while (!list_empty(&qp->rx_post_q)) {
1150 entry = list_first_entry(&qp->rx_post_q,
1151 struct ntb_queue_entry, entry);
1152 if (!(entry->flags & DESC_DONE_FLAG))
1153 break;
1154
1155 entry->rx_hdr->flags = 0;
1156 iowrite32(entry->index, &qp->rx_info->entry);
1157
1158 cb_data = entry->cb_data;
1159 len = entry->len;
1160
1161 list_move_tail(&entry->entry, &qp->rx_free_q);
1162
1163 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1164
1165 if (qp->rx_handler && qp->client_ready)
1166 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1167
1168 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1169 }
1170
1171 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1172}
1173
Jon Mason282a2fe2013-02-12 09:52:50 -07001174static void ntb_rx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001175{
Jon Mason282a2fe2013-02-12 09:52:50 -07001176 struct ntb_queue_entry *entry = data;
Jon Mason448c6fb2013-01-19 02:02:27 -07001177
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001178 entry->flags |= DESC_DONE_FLAG;
Jon Mason282a2fe2013-02-12 09:52:50 -07001179
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001180 ntb_complete_rxc(entry->qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001181}
1182
Jon Mason282a2fe2013-02-12 09:52:50 -07001183static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1184{
1185 void *buf = entry->buf;
1186 size_t len = entry->len;
1187
1188 memcpy(buf, offset, len);
1189
Allen Hubbee26a5842015-04-09 10:33:20 -04001190 /* Ensure that the data is fully copied out before clearing the flag */
1191 wmb();
1192
Jon Mason282a2fe2013-02-12 09:52:50 -07001193 ntb_rx_copy_callback(entry);
1194}
1195
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001196static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
Jon Mason282a2fe2013-02-12 09:52:50 -07001197{
1198 struct dma_async_tx_descriptor *txd;
1199 struct ntb_transport_qp *qp = entry->qp;
1200 struct dma_chan *chan = qp->dma_chan;
1201 struct dma_device *device;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001202 size_t pay_off, buff_off, len;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001203 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001204 dma_cookie_t cookie;
1205 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001206
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001207 len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001208
1209 if (!chan)
1210 goto err;
1211
Jon Mason53ca4fe2013-11-26 11:21:50 -07001212 if (len < copy_bytes)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001213 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001214
1215 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001216 pay_off = (size_t)offset & ~PAGE_MASK;
1217 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001218
1219 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001220 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001221
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001222 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1223 if (!unmap)
1224 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001225
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001226 unmap->len = len;
1227 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1228 pay_off, len, DMA_TO_DEVICE);
1229 if (dma_mapping_error(device->dev, unmap->addr[0]))
1230 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001231
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001232 unmap->to_cnt = 1;
1233
1234 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1235 buff_off, len, DMA_FROM_DEVICE);
1236 if (dma_mapping_error(device->dev, unmap->addr[1]))
1237 goto err_get_unmap;
1238
1239 unmap->from_cnt = 1;
1240
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001241 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001242 unmap->addr[0], len,
1243 DMA_PREP_INTERRUPT);
Jon Mason282a2fe2013-02-12 09:52:50 -07001244 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001245 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001246
1247 txd->callback = ntb_rx_copy_callback;
1248 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001249 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001250
1251 cookie = dmaengine_submit(txd);
1252 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001253 goto err_set_unmap;
1254
1255 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001256
1257 qp->last_cookie = cookie;
1258
1259 qp->rx_async++;
1260
1261 return;
1262
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001263err_set_unmap:
1264 dmaengine_unmap_put(unmap);
1265err_get_unmap:
1266 dmaengine_unmap_put(unmap);
1267err_wait:
Jon Mason282a2fe2013-02-12 09:52:50 -07001268 /* If the callbacks come out of order, the writing of the index to the
1269 * last completed will be out of order. This may result in the
1270 * receive stalling forever.
1271 */
1272 dma_sync_wait(chan, qp->last_cookie);
1273err:
1274 ntb_memcpy_rx(entry, offset);
1275 qp->rx_memcpy++;
1276}
1277
Jon Masonfce8a7b2012-11-16 19:27:12 -07001278static int ntb_process_rxc(struct ntb_transport_qp *qp)
1279{
1280 struct ntb_payload_header *hdr;
1281 struct ntb_queue_entry *entry;
1282 void *offset;
1283
Jon Mason793c20e2013-01-19 02:02:26 -07001284 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1285 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1286
Allen Hubbee26a5842015-04-09 10:33:20 -04001287 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1288 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001289
Jon Masonfce8a7b2012-11-16 19:27:12 -07001290 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001291 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001292 qp->rx_ring_empty++;
1293 return -EAGAIN;
1294 }
1295
Allen Hubbee26a5842015-04-09 10:33:20 -04001296 if (hdr->flags & LINK_DOWN_FLAG) {
1297 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1298 ntb_qp_link_down(qp);
1299 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001300 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001301 }
1302
1303 if (hdr->ver != (u32)qp->rx_pkts) {
1304 dev_dbg(&qp->ndev->pdev->dev,
1305 "version mismatch, expected %llu - got %u\n",
1306 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001307 qp->rx_err_ver++;
1308 return -EIO;
1309 }
1310
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001311 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
Allen Hubbee26a5842015-04-09 10:33:20 -04001312 if (!entry) {
1313 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1314 qp->rx_err_no_buf++;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001315 return -EAGAIN;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001316 }
1317
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001318 entry->rx_hdr = hdr;
1319 entry->index = qp->rx_index;
1320
Allen Hubbee26a5842015-04-09 10:33:20 -04001321 if (hdr->len > entry->len) {
1322 dev_dbg(&qp->ndev->pdev->dev,
1323 "receive buffer overflow! Wanted %d got %d\n",
1324 hdr->len, entry->len);
1325 qp->rx_err_oflow++;
1326
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001327 entry->len = -EIO;
1328 entry->flags |= DESC_DONE_FLAG;
1329
1330 ntb_complete_rxc(qp);
1331 } else {
1332 dev_dbg(&qp->ndev->pdev->dev,
1333 "RX OK index %u ver %u size %d into buf size %d\n",
1334 qp->rx_index, hdr->ver, hdr->len, entry->len);
1335
1336 qp->rx_bytes += hdr->len;
1337 qp->rx_pkts++;
1338
1339 entry->len = hdr->len;
1340
1341 ntb_async_rx(entry, offset);
Allen Hubbee26a5842015-04-09 10:33:20 -04001342 }
1343
Jon Mason282a2fe2013-02-12 09:52:50 -07001344 qp->rx_index++;
1345 qp->rx_index %= qp->rx_max_entry;
1346
1347 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001348}
1349
Allen Hubbee26a5842015-04-09 10:33:20 -04001350static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001351{
Allen Hubbee26a5842015-04-09 10:33:20 -04001352 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001353 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001354
Allen Hubbee26a5842015-04-09 10:33:20 -04001355 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1356 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001357
Jon Masonc336acd2013-01-17 15:28:45 -07001358 /* Limit the number of packets processed in a single interrupt to
1359 * provide fairness to others
1360 */
1361 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001362 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001363 if (rc)
1364 break;
1365 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001366
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001367 if (i && qp->dma_chan)
Jon Mason282a2fe2013-02-12 09:52:50 -07001368 dma_async_issue_pending(qp->dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001369
Allen Hubbee26a5842015-04-09 10:33:20 -04001370 if (i == qp->rx_max_entry) {
1371 /* there is more work to do */
1372 tasklet_schedule(&qp->rxc_db_work);
1373 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1374 /* the doorbell bit is set: clear it */
1375 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1376 /* ntb_db_read ensures ntb_db_clear write is committed */
1377 ntb_db_read(qp->ndev);
1378
1379 /* an interrupt may have arrived between finishing
1380 * ntb_process_rxc and clearing the doorbell bit:
1381 * there might be some more work to do.
1382 */
1383 tasklet_schedule(&qp->rxc_db_work);
1384 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001385}
1386
Jon Mason282a2fe2013-02-12 09:52:50 -07001387static void ntb_tx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001388{
Jon Mason282a2fe2013-02-12 09:52:50 -07001389 struct ntb_queue_entry *entry = data;
1390 struct ntb_transport_qp *qp = entry->qp;
1391 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001392
Jon Mason74465642013-01-21 15:28:52 -07001393 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001394
Allen Hubbee26a5842015-04-09 10:33:20 -04001395 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001396
1397 /* The entry length can only be zero if the packet is intended to be a
1398 * "link down" or similar. Since no payload is being sent in these
1399 * cases, there is nothing to add to the completion queue.
1400 */
1401 if (entry->len > 0) {
1402 qp->tx_bytes += entry->len;
1403
1404 if (qp->tx_handler)
1405 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1406 entry->len);
1407 }
1408
1409 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1410}
1411
Jon Mason282a2fe2013-02-12 09:52:50 -07001412static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1413{
Dave Jiang06917f72015-05-19 16:45:46 -04001414#ifdef ARCH_HAS_NOCACHE_UACCESS
1415 /*
1416 * Using non-temporal mov to improve performance on non-cached
1417 * writes, even though we aren't actually copying from user space.
1418 */
1419 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1420#else
Jon Mason282a2fe2013-02-12 09:52:50 -07001421 memcpy_toio(offset, entry->buf, entry->len);
Dave Jiang06917f72015-05-19 16:45:46 -04001422#endif
Jon Mason282a2fe2013-02-12 09:52:50 -07001423
Allen Hubbee26a5842015-04-09 10:33:20 -04001424 /* Ensure that the data is fully copied out before setting the flags */
1425 wmb();
1426
Jon Mason282a2fe2013-02-12 09:52:50 -07001427 ntb_tx_copy_callback(entry);
1428}
1429
1430static void ntb_async_tx(struct ntb_transport_qp *qp,
1431 struct ntb_queue_entry *entry)
1432{
1433 struct ntb_payload_header __iomem *hdr;
1434 struct dma_async_tx_descriptor *txd;
1435 struct dma_chan *chan = qp->dma_chan;
1436 struct dma_device *device;
1437 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001438 struct dmaengine_unmap_data *unmap;
1439 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001440 dma_cookie_t cookie;
1441 void __iomem *offset;
1442 size_t len = entry->len;
1443 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001444
1445 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
1446 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1447 entry->tx_hdr = hdr;
1448
1449 iowrite32(entry->len, &hdr->len);
Allen Hubbee26a5842015-04-09 10:33:20 -04001450 iowrite32((u32)qp->tx_pkts, &hdr->ver);
Jon Mason282a2fe2013-02-12 09:52:50 -07001451
1452 if (!chan)
1453 goto err;
1454
1455 if (len < copy_bytes)
1456 goto err;
1457
1458 device = chan->device;
1459 dest = qp->tx_mw_phys + qp->tx_max_frame * qp->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001460 buff_off = (size_t)buf & ~PAGE_MASK;
1461 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001462
1463 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1464 goto err;
1465
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001466 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1467 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001468 goto err;
1469
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001470 unmap->len = len;
1471 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1472 buff_off, len, DMA_TO_DEVICE);
1473 if (dma_mapping_error(device->dev, unmap->addr[0]))
1474 goto err_get_unmap;
1475
1476 unmap->to_cnt = 1;
1477
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001478 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001479 DMA_PREP_INTERRUPT);
Jon Mason282a2fe2013-02-12 09:52:50 -07001480 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001481 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001482
1483 txd->callback = ntb_tx_copy_callback;
1484 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001485 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001486
1487 cookie = dmaengine_submit(txd);
1488 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001489 goto err_set_unmap;
1490
1491 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001492
1493 dma_async_issue_pending(chan);
1494 qp->tx_async++;
1495
1496 return;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001497err_set_unmap:
1498 dmaengine_unmap_put(unmap);
1499err_get_unmap:
1500 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001501err:
1502 ntb_memcpy_tx(entry, offset);
1503 qp->tx_memcpy++;
1504}
1505
Jon Masonfce8a7b2012-11-16 19:27:12 -07001506static int ntb_process_tx(struct ntb_transport_qp *qp,
1507 struct ntb_queue_entry *entry)
1508{
Jon Mason793c20e2013-01-19 02:02:26 -07001509 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001510 qp->tx_ring_full++;
1511 return -EAGAIN;
1512 }
1513
Jon Masonef114ed2013-01-19 02:02:18 -07001514 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001515 if (qp->tx_handler)
1516 qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1517
1518 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1519 &qp->tx_free_q);
1520 return 0;
1521 }
1522
Jon Mason282a2fe2013-02-12 09:52:50 -07001523 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001524
Jon Mason793c20e2013-01-19 02:02:26 -07001525 qp->tx_index++;
1526 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001527
1528 qp->tx_pkts++;
1529
1530 return 0;
1531}
1532
1533static void ntb_send_link_down(struct ntb_transport_qp *qp)
1534{
Allen Hubbee26a5842015-04-09 10:33:20 -04001535 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001536 struct ntb_queue_entry *entry;
1537 int i, rc;
1538
Allen Hubbee26a5842015-04-09 10:33:20 -04001539 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001540 return;
1541
Allen Hubbee22e0b92015-05-12 06:55:44 -04001542 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001543
1544 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001545 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001546 if (entry)
1547 break;
1548 msleep(100);
1549 }
1550
1551 if (!entry)
1552 return;
1553
1554 entry->cb_data = NULL;
1555 entry->buf = NULL;
1556 entry->len = 0;
1557 entry->flags = LINK_DOWN_FLAG;
1558
1559 rc = ntb_process_tx(qp, entry);
1560 if (rc)
1561 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1562 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001563
1564 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001565}
1566
Allen Hubbe1199aa62015-05-18 06:20:47 -04001567static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1568{
1569 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1570}
1571
Jon Masonfce8a7b2012-11-16 19:27:12 -07001572/**
1573 * ntb_transport_create_queue - Create a new NTB transport layer queue
1574 * @rx_handler: receive callback function
1575 * @tx_handler: transmit callback function
1576 * @event_handler: event callback function
1577 *
1578 * Create a new NTB transport layer queue and provide the queue with a callback
1579 * routine for both transmit and receive. The receive callback routine will be
1580 * used to pass up data when the transport has received it on the queue. The
1581 * transmit callback routine will be called when the transport has completed the
1582 * transmission of the data on the queue and the data is ready to be freed.
1583 *
1584 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1585 */
1586struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001587ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001588 const struct ntb_queue_handlers *handlers)
1589{
Allen Hubbee26a5842015-04-09 10:33:20 -04001590 struct ntb_dev *ndev;
1591 struct pci_dev *pdev;
1592 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001593 struct ntb_queue_entry *entry;
1594 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001595 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001596 unsigned int free_queue;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001597 dma_cap_mask_t dma_mask;
1598 int node;
Allen Hubbee26a5842015-04-09 10:33:20 -04001599 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001600
Allen Hubbee26a5842015-04-09 10:33:20 -04001601 ndev = dev_ntb(client_dev->parent);
1602 pdev = ndev->pdev;
1603 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001604
Allen Hubbe1199aa62015-05-18 06:20:47 -04001605 node = dev_to_node(&ndev->dev);
1606
Jon Masonfce8a7b2012-11-16 19:27:12 -07001607 free_queue = ffs(nt->qp_bitmap);
1608 if (!free_queue)
1609 goto err;
1610
1611 /* decrement free_queue to make it zero based */
1612 free_queue--;
1613
Allen Hubbee26a5842015-04-09 10:33:20 -04001614 qp = &nt->qp_vec[free_queue];
1615 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001616
Allen Hubbee26a5842015-04-09 10:33:20 -04001617 nt->qp_bitmap_free &= ~qp_bit;
1618
Jon Masonfce8a7b2012-11-16 19:27:12 -07001619 qp->cb_data = data;
1620 qp->rx_handler = handlers->rx_handler;
1621 qp->tx_handler = handlers->tx_handler;
1622 qp->event_handler = handlers->event_handler;
1623
Allen Hubbe1199aa62015-05-18 06:20:47 -04001624 dma_cap_zero(dma_mask);
1625 dma_cap_set(DMA_MEMCPY, dma_mask);
1626
Dave Jianga41ef052015-05-19 16:52:04 -04001627 if (use_dma) {
1628 qp->dma_chan = dma_request_channel(dma_mask, ntb_dma_filter_fn,
1629 (void *)(unsigned long)node);
1630 if (!qp->dma_chan)
1631 dev_info(&pdev->dev, "Unable to allocate DMA channel\n");
1632 } else {
1633 qp->dma_chan = NULL;
1634 }
1635 dev_dbg(&pdev->dev, "Using %s memcpy\n", qp->dma_chan ? "DMA" : "CPU");
Jon Mason282a2fe2013-02-12 09:52:50 -07001636
Jon Masonfce8a7b2012-11-16 19:27:12 -07001637 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001638 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001639 if (!entry)
1640 goto err1;
1641
Jon Mason282a2fe2013-02-12 09:52:50 -07001642 entry->qp = qp;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001643 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001644 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001645 }
1646
1647 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001648 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001649 if (!entry)
1650 goto err2;
1651
Jon Mason282a2fe2013-02-12 09:52:50 -07001652 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001653 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001654 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001655 }
1656
Allen Hubbee26a5842015-04-09 10:33:20 -04001657 ntb_db_clear(qp->ndev, qp_bit);
1658 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001659
1660 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1661
1662 return qp;
1663
Jon Masonfce8a7b2012-11-16 19:27:12 -07001664err2:
Jon Masonf7667552013-01-19 02:02:24 -07001665 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001666 kfree(entry);
1667err1:
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001668 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001669 kfree(entry);
Jon Mason94681192013-11-19 09:34:47 -07001670 if (qp->dma_chan)
Allen Hubbe1199aa62015-05-18 06:20:47 -04001671 dma_release_channel(qp->dma_chan);
Allen Hubbee26a5842015-04-09 10:33:20 -04001672 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001673err:
1674 return NULL;
1675}
1676EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1677
1678/**
1679 * ntb_transport_free_queue - Frees NTB transport queue
1680 * @qp: NTB queue to be freed
1681 *
1682 * Frees NTB transport queue
1683 */
1684void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1685{
Allen Hubbee26a5842015-04-09 10:33:20 -04001686 struct ntb_transport_ctx *nt = qp->transport;
Jon Mason186f27f2013-01-22 11:35:40 -07001687 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001688 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001689 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001690
1691 if (!qp)
1692 return;
1693
Allen Hubbee26a5842015-04-09 10:33:20 -04001694 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001695
Jon Mason282a2fe2013-02-12 09:52:50 -07001696 if (qp->dma_chan) {
1697 struct dma_chan *chan = qp->dma_chan;
1698 /* Putting the dma_chan to NULL will force any new traffic to be
1699 * processed by the CPU instead of the DAM engine
1700 */
1701 qp->dma_chan = NULL;
1702
1703 /* Try to be nice and wait for any queued DMA engine
1704 * transactions to process before smashing it with a rock
1705 */
1706 dma_sync_wait(chan, qp->last_cookie);
1707 dmaengine_terminate_all(chan);
Allen Hubbe1199aa62015-05-18 06:20:47 -04001708 dma_release_channel(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001709 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001710
Allen Hubbee26a5842015-04-09 10:33:20 -04001711 qp_bit = BIT_ULL(qp->qp_num);
1712
1713 ntb_db_set_mask(qp->ndev, qp_bit);
1714 tasklet_disable(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001715
Jon Mason282a2fe2013-02-12 09:52:50 -07001716 cancel_delayed_work_sync(&qp->link_work);
1717
Allen Hubbee26a5842015-04-09 10:33:20 -04001718 qp->cb_data = NULL;
1719 qp->rx_handler = NULL;
1720 qp->tx_handler = NULL;
1721 qp->event_handler = NULL;
1722
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001723 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001724 kfree(entry);
1725
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001726 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1727 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1728 kfree(entry);
1729 }
1730
1731 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1732 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001733 kfree(entry);
1734 }
1735
Jon Masonf7667552013-01-19 02:02:24 -07001736 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001737 kfree(entry);
1738
Allen Hubbee26a5842015-04-09 10:33:20 -04001739 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001740
1741 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1742}
1743EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1744
1745/**
1746 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1747 * @qp: NTB queue to be freed
1748 * @len: pointer to variable to write enqueued buffers length
1749 *
1750 * Dequeues unused buffers from receive queue. Should only be used during
1751 * shutdown of qp.
1752 *
1753 * RETURNS: NULL error value on error, or void* for success.
1754 */
1755void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1756{
1757 struct ntb_queue_entry *entry;
1758 void *buf;
1759
Allen Hubbee26a5842015-04-09 10:33:20 -04001760 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001761 return NULL;
1762
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001763 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001764 if (!entry)
1765 return NULL;
1766
1767 buf = entry->cb_data;
1768 *len = entry->len;
1769
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001770 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001771
1772 return buf;
1773}
1774EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1775
1776/**
1777 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1778 * @qp: NTB transport layer queue the entry is to be enqueued on
1779 * @cb: per buffer pointer for callback function to use
1780 * @data: pointer to data buffer that incoming packets will be copied into
1781 * @len: length of the data buffer
1782 *
1783 * Enqueue a new receive buffer onto the transport queue into which a NTB
1784 * payload can be received into.
1785 *
1786 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1787 */
1788int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1789 unsigned int len)
1790{
1791 struct ntb_queue_entry *entry;
1792
1793 if (!qp)
1794 return -EINVAL;
1795
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001796 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001797 if (!entry)
1798 return -ENOMEM;
1799
1800 entry->cb_data = cb;
1801 entry->buf = data;
1802 entry->len = len;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001803 entry->flags = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001804
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001805 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
1806
1807 tasklet_schedule(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001808
1809 return 0;
1810}
1811EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1812
1813/**
1814 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1815 * @qp: NTB transport layer queue the entry is to be enqueued on
1816 * @cb: per buffer pointer for callback function to use
1817 * @data: pointer to data buffer that will be sent
1818 * @len: length of the data buffer
1819 *
1820 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07001821 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07001822 * serialize access to the qp.
1823 *
1824 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1825 */
1826int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1827 unsigned int len)
1828{
1829 struct ntb_queue_entry *entry;
1830 int rc;
1831
Allen Hubbee26a5842015-04-09 10:33:20 -04001832 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001833 return -EINVAL;
1834
1835 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001836 if (!entry) {
1837 qp->tx_err_no_buf++;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001838 return -ENOMEM;
Jon Mason282a2fe2013-02-12 09:52:50 -07001839 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001840
1841 entry->cb_data = cb;
1842 entry->buf = data;
1843 entry->len = len;
1844 entry->flags = 0;
1845
1846 rc = ntb_process_tx(qp, entry);
1847 if (rc)
1848 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1849 &qp->tx_free_q);
1850
1851 return rc;
1852}
1853EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1854
1855/**
1856 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1857 * @qp: NTB transport layer queue to be enabled
1858 *
1859 * Notify NTB transport layer of client readiness to use queue
1860 */
1861void ntb_transport_link_up(struct ntb_transport_qp *qp)
1862{
1863 if (!qp)
1864 return;
1865
Allen Hubbee26a5842015-04-09 10:33:20 -04001866 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001867
Allen Hubbee26a5842015-04-09 10:33:20 -04001868 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001869 schedule_delayed_work(&qp->link_work, 0);
1870}
1871EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1872
1873/**
1874 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1875 * @qp: NTB transport layer queue to be disabled
1876 *
1877 * Notify NTB transport layer of client's desire to no longer receive data on
1878 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07001879 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07001880 */
1881void ntb_transport_link_down(struct ntb_transport_qp *qp)
1882{
Jon Mason186f27f2013-01-22 11:35:40 -07001883 struct pci_dev *pdev;
Allen Hubbee26a5842015-04-09 10:33:20 -04001884 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001885
1886 if (!qp)
1887 return;
1888
Allen Hubbee26a5842015-04-09 10:33:20 -04001889 pdev = qp->ndev->pdev;
1890 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001891
Allen Hubbee26a5842015-04-09 10:33:20 -04001892 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001893
Allen Hubbee26a5842015-04-09 10:33:20 -04001894 ntb_peer_spad_write(qp->ndev, QP_LINKS,
1895 val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001896
Allen Hubbee26a5842015-04-09 10:33:20 -04001897 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001898 ntb_send_link_down(qp);
1899 else
1900 cancel_delayed_work_sync(&qp->link_work);
1901}
1902EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1903
1904/**
1905 * ntb_transport_link_query - Query transport link state
1906 * @qp: NTB transport layer queue to be queried
1907 *
1908 * Query connectivity to the remote system of the NTB transport queue
1909 *
1910 * RETURNS: true for link up or false for link down
1911 */
1912bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1913{
Jon Mason186f27f2013-01-22 11:35:40 -07001914 if (!qp)
1915 return false;
1916
Allen Hubbee26a5842015-04-09 10:33:20 -04001917 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001918}
1919EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1920
1921/**
1922 * ntb_transport_qp_num - Query the qp number
1923 * @qp: NTB transport layer queue to be queried
1924 *
1925 * Query qp number of the NTB transport queue
1926 *
1927 * RETURNS: a zero based number specifying the qp number
1928 */
1929unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1930{
Jon Mason186f27f2013-01-22 11:35:40 -07001931 if (!qp)
1932 return 0;
1933
Jon Masonfce8a7b2012-11-16 19:27:12 -07001934 return qp->qp_num;
1935}
1936EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1937
1938/**
1939 * ntb_transport_max_size - Query the max payload size of a qp
1940 * @qp: NTB transport layer queue to be queried
1941 *
1942 * Query the maximum payload size permissible on the given qp
1943 *
1944 * RETURNS: the max payload size of a qp
1945 */
Jon Masonef114ed2013-01-19 02:02:18 -07001946unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001947{
Jon Mason282a2fe2013-02-12 09:52:50 -07001948 unsigned int max;
1949
Jon Mason186f27f2013-01-22 11:35:40 -07001950 if (!qp)
1951 return 0;
1952
Jon Mason282a2fe2013-02-12 09:52:50 -07001953 if (!qp->dma_chan)
1954 return qp->tx_max_frame - sizeof(struct ntb_payload_header);
1955
1956 /* If DMA engine usage is possible, try to find the max size for that */
1957 max = qp->tx_max_frame - sizeof(struct ntb_payload_header);
1958 max -= max % (1 << qp->dma_chan->device->copy_align);
1959
1960 return max;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001961}
1962EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001963
1964static void ntb_transport_doorbell_callback(void *data, int vector)
1965{
1966 struct ntb_transport_ctx *nt = data;
1967 struct ntb_transport_qp *qp;
1968 u64 db_bits;
1969 unsigned int qp_num;
1970
1971 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
1972 ntb_db_vector_mask(nt->ndev, vector));
1973
1974 while (db_bits) {
1975 qp_num = __ffs(db_bits);
1976 qp = &nt->qp_vec[qp_num];
1977
1978 tasklet_schedule(&qp->rxc_db_work);
1979
1980 db_bits &= ~BIT_ULL(qp_num);
1981 }
1982}
1983
1984static const struct ntb_ctx_ops ntb_transport_ops = {
1985 .link_event = ntb_transport_event_callback,
1986 .db_event = ntb_transport_doorbell_callback,
1987};
1988
1989static struct ntb_client ntb_transport_client = {
1990 .ops = {
1991 .probe = ntb_transport_probe,
1992 .remove = ntb_transport_free,
1993 },
1994};
1995
1996static int __init ntb_transport_init(void)
1997{
1998 int rc;
1999
Dave Jiang7eb38782015-06-15 08:21:33 -04002000 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2001
Allen Hubbee26a5842015-04-09 10:33:20 -04002002 if (debugfs_initialized())
2003 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2004
2005 rc = bus_register(&ntb_transport_bus);
2006 if (rc)
2007 goto err_bus;
2008
2009 rc = ntb_register_client(&ntb_transport_client);
2010 if (rc)
2011 goto err_client;
2012
2013 return 0;
2014
2015err_client:
2016 bus_unregister(&ntb_transport_bus);
2017err_bus:
2018 debugfs_remove_recursive(nt_debugfs_dir);
2019 return rc;
2020}
2021module_init(ntb_transport_init);
2022
2023static void __exit ntb_transport_exit(void)
2024{
2025 debugfs_remove_recursive(nt_debugfs_dir);
2026
2027 ntb_unregister_client(&ntb_transport_client);
2028 bus_unregister(&ntb_transport_bus);
2029}
2030module_exit(ntb_transport_exit);