blob: 2efd1eae9a7c7cf9ee9abce9c188b74cf7867cfd [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>
Allen Hubbee26a5842015-04-09 10:33:20 -040061#include "linux/ntb.h"
62#include "linux/ntb_transport.h"
Jon Masonfce8a7b2012-11-16 19:27:12 -070063
Allen Hubbee26a5842015-04-09 10:33:20 -040064#define NTB_TRANSPORT_VERSION 4
65#define NTB_TRANSPORT_VER "4"
66#define NTB_TRANSPORT_NAME "ntb_transport"
67#define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
68
69MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
70MODULE_VERSION(NTB_TRANSPORT_VER);
71MODULE_LICENSE("Dual BSD/GPL");
72MODULE_AUTHOR("Intel Corporation");
73
74static unsigned long max_mw_size;
75module_param(max_mw_size, ulong, 0644);
76MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
Jon Masonfce8a7b2012-11-16 19:27:12 -070077
Jon Masonef114ed2013-01-19 02:02:18 -070078static unsigned int transport_mtu = 0x401E;
Jon Masonfce8a7b2012-11-16 19:27:12 -070079module_param(transport_mtu, uint, 0644);
80MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
81
Jon Mason948d3a62013-04-18 17:07:36 -070082static unsigned char max_num_clients;
Jon Masonfce8a7b2012-11-16 19:27:12 -070083module_param(max_num_clients, byte, 0644);
84MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
85
Jon Mason282a2fe2013-02-12 09:52:50 -070086static unsigned int copy_bytes = 1024;
87module_param(copy_bytes, uint, 0644);
88MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
89
Allen Hubbee26a5842015-04-09 10:33:20 -040090static struct dentry *nt_debugfs_dir;
91
Jon Masonfce8a7b2012-11-16 19:27:12 -070092struct ntb_queue_entry {
93 /* ntb_queue list reference */
94 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -040095 /* pointers to data to be transferred */
Jon Masonfce8a7b2012-11-16 19:27:12 -070096 void *cb_data;
97 void *buf;
98 unsigned int len;
99 unsigned int flags;
Jon Mason282a2fe2013-02-12 09:52:50 -0700100
101 struct ntb_transport_qp *qp;
102 union {
103 struct ntb_payload_header __iomem *tx_hdr;
104 struct ntb_payload_header *rx_hdr;
105 };
106 unsigned int index;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700107};
108
Jon Mason793c20e2013-01-19 02:02:26 -0700109struct ntb_rx_info {
110 unsigned int entry;
111};
112
Jon Masonfce8a7b2012-11-16 19:27:12 -0700113struct ntb_transport_qp {
Allen Hubbee26a5842015-04-09 10:33:20 -0400114 struct ntb_transport_ctx *transport;
115 struct ntb_dev *ndev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700116 void *cb_data;
Jon Mason282a2fe2013-02-12 09:52:50 -0700117 struct dma_chan *dma_chan;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700118
119 bool client_ready;
Allen Hubbee26a5842015-04-09 10:33:20 -0400120 bool link_is_up;
121
Jon Masonfce8a7b2012-11-16 19:27:12 -0700122 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400123 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700124
Jon Mason74465642013-01-21 15:28:52 -0700125 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -0700126 struct ntb_rx_info *remote_rx_info;
127
Jon Mason53ca4fe2013-11-26 11:21:50 -0700128 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
129 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700130 struct list_head tx_free_q;
131 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700132 void __iomem *tx_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700133 dma_addr_t tx_mw_phys;
Jon Mason793c20e2013-01-19 02:02:26 -0700134 unsigned int tx_index;
135 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700136 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700137
Jon Mason53ca4fe2013-11-26 11:21:50 -0700138 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
139 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700140 struct list_head rx_pend_q;
141 struct list_head rx_free_q;
142 spinlock_t ntb_rx_pend_q_lock;
143 spinlock_t ntb_rx_free_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700144 void *rx_buff;
145 unsigned int rx_index;
146 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700147 unsigned int rx_max_frame;
Jon Mason282a2fe2013-02-12 09:52:50 -0700148 dma_cookie_t last_cookie;
Allen Hubbee26a5842015-04-09 10:33:20 -0400149 struct tasklet_struct rxc_db_work;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700150
Jon Mason53ca4fe2013-11-26 11:21:50 -0700151 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700152 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700153 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700154
155 struct dentry *debugfs_dir;
156 struct dentry *debugfs_stats;
157
158 /* Stats */
159 u64 rx_bytes;
160 u64 rx_pkts;
161 u64 rx_ring_empty;
162 u64 rx_err_no_buf;
163 u64 rx_err_oflow;
164 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700165 u64 rx_memcpy;
166 u64 rx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700167 u64 tx_bytes;
168 u64 tx_pkts;
169 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700170 u64 tx_err_no_buf;
171 u64 tx_memcpy;
172 u64 tx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700173};
174
175struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400176 phys_addr_t phys_addr;
177 resource_size_t phys_size;
178 resource_size_t xlat_align;
179 resource_size_t xlat_align_size;
180 void __iomem *vbase;
181 size_t xlat_size;
182 size_t buff_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700183 void *virt_addr;
184 dma_addr_t dma_addr;
185};
186
187struct ntb_transport_client_dev {
188 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400189 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700190 struct device dev;
191};
192
Allen Hubbee26a5842015-04-09 10:33:20 -0400193struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700194 struct list_head entry;
195 struct list_head client_devs;
196
Allen Hubbee26a5842015-04-09 10:33:20 -0400197 struct ntb_dev *ndev;
198
199 struct ntb_transport_mw *mw_vec;
200 struct ntb_transport_qp *qp_vec;
201 unsigned int mw_count;
202 unsigned int qp_count;
203 u64 qp_bitmap;
204 u64 qp_bitmap_free;
205
206 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700207 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700208 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700209};
210
211enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400212 DESC_DONE_FLAG = BIT(0),
213 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700214};
215
216struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700217 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700218 unsigned int len;
219 unsigned int flags;
220};
221
222enum {
223 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700224 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700225 NUM_QPS,
226 NUM_MWS,
227 MW0_SZ_HIGH,
228 MW0_SZ_LOW,
229 MW1_SZ_HIGH,
230 MW1_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700231 MAX_SPAD,
232};
233
Allen Hubbee26a5842015-04-09 10:33:20 -0400234#define dev_client_dev(__dev) \
235 container_of((__dev), struct ntb_transport_client_dev, dev)
236
237#define drv_client(__drv) \
238 container_of((__drv), struct ntb_transport_client, driver)
239
240#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700241#define NTB_QP_DEF_NUM_ENTRIES 100
242#define NTB_LINK_DOWN_TIMEOUT 10
243
Allen Hubbee26a5842015-04-09 10:33:20 -0400244static void ntb_transport_rxc_db(unsigned long data);
245static const struct ntb_ctx_ops ntb_transport_ops;
246static struct ntb_client ntb_transport_client;
247
248static int ntb_transport_bus_match(struct device *dev,
249 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700250{
251 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
252}
253
Allen Hubbee26a5842015-04-09 10:33:20 -0400254static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700255{
Allen Hubbee26a5842015-04-09 10:33:20 -0400256 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700257 int rc = -EINVAL;
258
259 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400260
261 client = drv_client(dev->driver);
262 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700263 if (rc)
264 put_device(dev);
265
266 return rc;
267}
268
Allen Hubbee26a5842015-04-09 10:33:20 -0400269static int ntb_transport_bus_remove(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
Allen Hubbee26a5842015-04-09 10:33:20 -0400273 client = drv_client(dev->driver);
274 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700275
276 put_device(dev);
277
278 return 0;
279}
280
Allen Hubbee26a5842015-04-09 10:33:20 -0400281static struct bus_type ntb_transport_bus = {
282 .name = "ntb_transport",
283 .match = ntb_transport_bus_match,
284 .probe = ntb_transport_bus_probe,
285 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700286};
287
288static LIST_HEAD(ntb_transport_list);
289
Allen Hubbee26a5842015-04-09 10:33:20 -0400290static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700291{
Jon Masonfce8a7b2012-11-16 19:27:12 -0700292 list_add(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700293 return 0;
294}
295
Allen Hubbee26a5842015-04-09 10:33:20 -0400296static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700297{
298 struct ntb_transport_client_dev *client_dev, *cd;
299
300 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
301 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
302 dev_name(&client_dev->dev));
303 list_del(&client_dev->entry);
304 device_unregister(&client_dev->dev);
305 }
306
307 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700308}
309
Allen Hubbee26a5842015-04-09 10:33:20 -0400310static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700311{
312 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700313
Allen Hubbee26a5842015-04-09 10:33:20 -0400314 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700315 kfree(client_dev);
316}
317
318/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400319 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700320 * @device_name: Name of NTB client device
321 *
322 * Unregister an NTB client device with the NTB transport layer
323 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400324void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700325{
326 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400327 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700328
329 list_for_each_entry(nt, &ntb_transport_list, entry)
330 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
331 if (!strncmp(dev_name(&client->dev), device_name,
332 strlen(device_name))) {
333 list_del(&client->entry);
334 device_unregister(&client->dev);
335 }
336}
Allen Hubbee26a5842015-04-09 10:33:20 -0400337EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700338
339/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400340 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700341 * @device_name: Name of NTB client device
342 *
343 * Register an NTB client device with the NTB transport layer
344 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400345int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700346{
347 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400348 struct ntb_transport_ctx *nt;
Jon Mason8b19d452013-04-26 14:51:57 -0700349 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700350
Jon Mason8222b402013-01-19 02:02:16 -0700351 if (list_empty(&ntb_transport_list))
352 return -ENODEV;
353
Jon Masonfce8a7b2012-11-16 19:27:12 -0700354 list_for_each_entry(nt, &ntb_transport_list, entry) {
355 struct device *dev;
356
Allen Hubbee26a5842015-04-09 10:33:20 -0400357 client_dev = kzalloc(sizeof(*client_dev),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700358 GFP_KERNEL);
359 if (!client_dev) {
360 rc = -ENOMEM;
361 goto err;
362 }
363
364 dev = &client_dev->dev;
365
366 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700367 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400368 dev->bus = &ntb_transport_bus;
369 dev->release = ntb_transport_client_release;
370 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700371
372 rc = device_register(dev);
373 if (rc) {
374 kfree(client_dev);
375 goto err;
376 }
377
378 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700379 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700380 }
381
382 return 0;
383
384err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400385 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700386
387 return rc;
388}
Allen Hubbee26a5842015-04-09 10:33:20 -0400389EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700390
391/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400392 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700393 * @drv: NTB client driver to be registered
394 *
395 * Register an NTB client driver with the NTB transport layer
396 *
397 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
398 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400399int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700400{
Allen Hubbee26a5842015-04-09 10:33:20 -0400401 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700402
Jon Mason8222b402013-01-19 02:02:16 -0700403 if (list_empty(&ntb_transport_list))
404 return -ENODEV;
405
Jon Masonfce8a7b2012-11-16 19:27:12 -0700406 return driver_register(&drv->driver);
407}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400408EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700409
410/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400411 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700412 * @drv: NTB client driver to be unregistered
413 *
414 * Unregister an NTB client driver with the NTB transport layer
415 *
416 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
417 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400418void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700419{
420 driver_unregister(&drv->driver);
421}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400422EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700423
Jon Masonfce8a7b2012-11-16 19:27:12 -0700424static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
425 loff_t *offp)
426{
427 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700428 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700429 ssize_t ret, out_offset, out_count;
430
Jon Mason282a2fe2013-02-12 09:52:50 -0700431 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700432
433 buf = kmalloc(out_count, GFP_KERNEL);
434 if (!buf)
435 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700436
437 qp = filp->private_data;
438 out_offset = 0;
439 out_offset += snprintf(buf + out_offset, out_count - out_offset,
440 "NTB QP stats\n");
441 out_offset += snprintf(buf + out_offset, out_count - out_offset,
442 "rx_bytes - \t%llu\n", qp->rx_bytes);
443 out_offset += snprintf(buf + out_offset, out_count - out_offset,
444 "rx_pkts - \t%llu\n", qp->rx_pkts);
445 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700446 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
447 out_offset += snprintf(buf + out_offset, out_count - out_offset,
448 "rx_async - \t%llu\n", qp->rx_async);
449 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700450 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
451 out_offset += snprintf(buf + out_offset, out_count - out_offset,
452 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
453 out_offset += snprintf(buf + out_offset, out_count - out_offset,
454 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
455 out_offset += snprintf(buf + out_offset, out_count - out_offset,
456 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
457 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700458 "rx_buff - \t%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700459 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700460 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700461 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700462 "rx_max_entry - \t%u\n", qp->rx_max_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700463
464 out_offset += snprintf(buf + out_offset, out_count - out_offset,
465 "tx_bytes - \t%llu\n", qp->tx_bytes);
466 out_offset += snprintf(buf + out_offset, out_count - out_offset,
467 "tx_pkts - \t%llu\n", qp->tx_pkts);
468 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700469 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
470 out_offset += snprintf(buf + out_offset, out_count - out_offset,
471 "tx_async - \t%llu\n", qp->tx_async);
472 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700473 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
474 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700475 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
476 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700477 "tx_mw - \t%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700478 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700479 "tx_index - \t%u\n", qp->tx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700480 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700481 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700482
483 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Allen Hubbee26a5842015-04-09 10:33:20 -0400484 "\nQP Link %s\n",
485 qp->link_is_up ? "Up" : "Down");
Jon Masond7237e22013-01-19 02:02:25 -0700486 if (out_offset > out_count)
487 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700488
489 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700490 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700491 return ret;
492}
493
494static const struct file_operations ntb_qp_debugfs_stats = {
495 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700496 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700497 .read = debugfs_read,
498};
499
500static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
501 struct list_head *list)
502{
503 unsigned long flags;
504
505 spin_lock_irqsave(lock, flags);
506 list_add_tail(entry, list);
507 spin_unlock_irqrestore(lock, flags);
508}
509
510static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700511 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700512{
513 struct ntb_queue_entry *entry;
514 unsigned long flags;
515
516 spin_lock_irqsave(lock, flags);
517 if (list_empty(list)) {
518 entry = NULL;
519 goto out;
520 }
521 entry = list_first_entry(list, struct ntb_queue_entry, entry);
522 list_del(&entry->entry);
523out:
524 spin_unlock_irqrestore(lock, flags);
525
526 return entry;
527}
528
Allen Hubbee26a5842015-04-09 10:33:20 -0400529static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
530 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700531{
Allen Hubbee26a5842015-04-09 10:33:20 -0400532 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
533 struct ntb_transport_mw *mw;
Jon Masonef114ed2013-01-19 02:02:18 -0700534 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400535 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700536 unsigned int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700537
Allen Hubbee26a5842015-04-09 10:33:20 -0400538 mw_count = nt->mw_count;
539 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700540
Allen Hubbee26a5842015-04-09 10:33:20 -0400541 mw_num = QP_TO_MW(nt, qp_num);
542 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700543
Allen Hubbee26a5842015-04-09 10:33:20 -0400544 if (!mw->virt_addr)
545 return -ENOMEM;
546
547 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
548 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700549 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400550 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700551
Allen Hubbee26a5842015-04-09 10:33:20 -0400552 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
553 qp->rx_buff = mw->virt_addr + rx_size * qp_num / mw_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700554 rx_size -= sizeof(struct ntb_rx_info);
555
Jon Mason282a2fe2013-02-12 09:52:50 -0700556 qp->remote_rx_info = qp->rx_buff + rx_size;
557
Jon Masonc9d534c2013-02-01 15:45:16 -0700558 /* Due to housekeeping, there must be atleast 2 buffs */
559 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700560 qp->rx_max_entry = rx_size / qp->rx_max_frame;
561 qp->rx_index = 0;
562
Jon Masonc9d534c2013-02-01 15:45:16 -0700563 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700564
Jon Masonef114ed2013-01-19 02:02:18 -0700565 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700566 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400567 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
568 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700569 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700570 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700571
572 qp->rx_pkts = 0;
573 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700574 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400575
576 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700577}
578
Allen Hubbee26a5842015-04-09 10:33:20 -0400579static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700580{
Allen Hubbee26a5842015-04-09 10:33:20 -0400581 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
582 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700583
584 if (!mw->virt_addr)
585 return;
586
Allen Hubbee26a5842015-04-09 10:33:20 -0400587 ntb_mw_clear_trans(nt->ndev, num_mw);
588 dma_free_coherent(&pdev->dev, mw->buff_size,
589 mw->virt_addr, mw->dma_addr);
590 mw->xlat_size = 0;
591 mw->buff_size = 0;
Jon Mason113fc502013-01-30 11:40:52 -0700592 mw->virt_addr = NULL;
593}
594
Allen Hubbee26a5842015-04-09 10:33:20 -0400595static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
596 unsigned int size)
Jon Masonb77b2632013-02-01 15:25:37 -0700597{
Allen Hubbee26a5842015-04-09 10:33:20 -0400598 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
599 struct pci_dev *pdev = nt->ndev->pdev;
600 unsigned int xlat_size, buff_size;
601 int rc;
602
603 xlat_size = round_up(size, mw->xlat_align_size);
604 buff_size = round_up(size, mw->xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700605
606 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400607 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700608 return 0;
609
Allen Hubbee26a5842015-04-09 10:33:20 -0400610 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700611 ntb_free_mw(nt, num_mw);
612
Allen Hubbee26a5842015-04-09 10:33:20 -0400613 /* Alloc memory for receiving data. Must be aligned */
614 mw->xlat_size = xlat_size;
615 mw->buff_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700616
Allen Hubbee26a5842015-04-09 10:33:20 -0400617 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
618 &mw->dma_addr, GFP_KERNEL);
Jon Masonb77b2632013-02-01 15:25:37 -0700619 if (!mw->virt_addr) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400620 mw->xlat_size = 0;
621 mw->buff_size = 0;
622 dev_err(&pdev->dev, "Unable to alloc MW buff of size %d\n",
623 buff_size);
Jon Masonb77b2632013-02-01 15:25:37 -0700624 return -ENOMEM;
625 }
626
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700627 /*
628 * we must ensure that the memory address allocated is BAR size
629 * aligned in order for the XLAT register to take the value. This
630 * is a requirement of the hardware. It is recommended to setup CMA
631 * for BAR sizes equal or greater than 4MB.
632 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400633 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
634 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700635 &mw->dma_addr);
636 ntb_free_mw(nt, num_mw);
637 return -ENOMEM;
638 }
639
Jon Masonb77b2632013-02-01 15:25:37 -0700640 /* Notify HW the memory location of the receive buffer */
Allen Hubbee26a5842015-04-09 10:33:20 -0400641 rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
642 if (rc) {
643 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
644 ntb_free_mw(nt, num_mw);
645 return -EIO;
646 }
Jon Masonb77b2632013-02-01 15:25:37 -0700647
648 return 0;
649}
650
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400651static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
652{
653 qp->link_is_up = false;
654
655 qp->tx_index = 0;
656 qp->rx_index = 0;
657 qp->rx_bytes = 0;
658 qp->rx_pkts = 0;
659 qp->rx_ring_empty = 0;
660 qp->rx_err_no_buf = 0;
661 qp->rx_err_oflow = 0;
662 qp->rx_err_ver = 0;
663 qp->rx_memcpy = 0;
664 qp->rx_async = 0;
665 qp->tx_bytes = 0;
666 qp->tx_pkts = 0;
667 qp->tx_ring_full = 0;
668 qp->tx_err_no_buf = 0;
669 qp->tx_memcpy = 0;
670 qp->tx_async = 0;
671}
672
Jon Masonfca4d512013-09-09 13:39:55 -0700673static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700674{
Allen Hubbee26a5842015-04-09 10:33:20 -0400675 struct ntb_transport_ctx *nt = qp->transport;
676 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700677
Allen Hubbee22e0b92015-05-12 06:55:44 -0400678 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400679
680 cancel_delayed_work_sync(&qp->link_work);
681 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400682
683 if (qp->event_handler)
684 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700685}
686
687static void ntb_qp_link_cleanup_work(struct work_struct *work)
688{
689 struct ntb_transport_qp *qp = container_of(work,
690 struct ntb_transport_qp,
691 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400692 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700693
694 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700695
Allen Hubbee26a5842015-04-09 10:33:20 -0400696 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700697 schedule_delayed_work(&qp->link_work,
698 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
699}
700
Jon Mason7b4f2d32013-01-19 02:02:19 -0700701static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700702{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700703 schedule_work(&qp->link_cleanup);
704}
705
Allen Hubbee26a5842015-04-09 10:33:20 -0400706static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700707{
Allen Hubbee26a5842015-04-09 10:33:20 -0400708 struct ntb_transport_qp *qp;
709 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700710 int i;
711
Allen Hubbee26a5842015-04-09 10:33:20 -0400712 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700713
Allen Hubbee26a5842015-04-09 10:33:20 -0400714 /* Pass along the info to any clients */
715 for (i = 0; i < nt->qp_count; i++)
716 if (qp_bitmap_alloc & BIT_ULL(i)) {
717 qp = &nt->qp_vec[i];
718 ntb_qp_link_cleanup(qp);
719 cancel_work_sync(&qp->link_cleanup);
720 cancel_delayed_work_sync(&qp->link_work);
721 }
722
723 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700724 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700725
Jon Masonfce8a7b2012-11-16 19:27:12 -0700726 /* The scratchpad registers keep the values if the remote side
727 * goes down, blast them now to give them a sane value the next
728 * time they are accessed
729 */
730 for (i = 0; i < MAX_SPAD; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400731 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700732}
733
Jon Masonfca4d512013-09-09 13:39:55 -0700734static void ntb_transport_link_cleanup_work(struct work_struct *work)
735{
Allen Hubbee26a5842015-04-09 10:33:20 -0400736 struct ntb_transport_ctx *nt =
737 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700738
739 ntb_transport_link_cleanup(nt);
740}
741
Allen Hubbee26a5842015-04-09 10:33:20 -0400742static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700743{
Allen Hubbee26a5842015-04-09 10:33:20 -0400744 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700745
Allen Hubbee26a5842015-04-09 10:33:20 -0400746 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700747 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400748 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700749 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700750}
751
752static void ntb_transport_link_work(struct work_struct *work)
753{
Allen Hubbee26a5842015-04-09 10:33:20 -0400754 struct ntb_transport_ctx *nt =
755 container_of(work, struct ntb_transport_ctx, link_work.work);
756 struct ntb_dev *ndev = nt->ndev;
757 struct pci_dev *pdev = ndev->pdev;
758 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700759 u32 val;
Allen Hubbee26a5842015-04-09 10:33:20 -0400760 int rc, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700761
Jon Mason113fc502013-01-30 11:40:52 -0700762 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400763 for (i = 0; i < nt->mw_count; i++) {
764 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700765
Allen Hubbee26a5842015-04-09 10:33:20 -0400766 if (max_mw_size && size > max_mw_size)
767 size = max_mw_size;
768
769 spad = MW0_SZ_HIGH + (i * 2);
770 ntb_peer_spad_write(ndev, spad, (u32)(size >> 32));
771
772 spad = MW0_SZ_LOW + (i * 2);
773 ntb_peer_spad_write(ndev, spad, (u32)size);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700774 }
775
Allen Hubbee26a5842015-04-09 10:33:20 -0400776 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700777
Allen Hubbee26a5842015-04-09 10:33:20 -0400778 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700779
Allen Hubbee26a5842015-04-09 10:33:20 -0400780 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700781
782 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400783 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400784 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700785 if (val != NTB_TRANSPORT_VERSION)
786 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700787
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400788 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700789 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400790 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700791 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700792
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400793 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700794 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400795 if (val != nt->mw_count)
796 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700797
Allen Hubbee26a5842015-04-09 10:33:20 -0400798 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700799 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700800
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400801 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400802 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700803
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400804 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700805 val64 |= val;
806
Allen Hubbee26a5842015-04-09 10:33:20 -0400807 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700808
809 rc = ntb_set_mw(nt, i, val64);
810 if (rc)
811 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700812 }
813
Allen Hubbee26a5842015-04-09 10:33:20 -0400814 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700815
Allen Hubbee26a5842015-04-09 10:33:20 -0400816 for (i = 0; i < nt->qp_count; i++) {
817 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700818
819 ntb_transport_setup_qp_mw(nt, i);
820
Allen Hubbee26a5842015-04-09 10:33:20 -0400821 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700822 schedule_delayed_work(&qp->link_work, 0);
823 }
824
825 return;
826
Jon Mason113fc502013-01-30 11:40:52 -0700827out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400828 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700829 ntb_free_mw(nt, i);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700830out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400831 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700832 schedule_delayed_work(&nt->link_work,
833 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
834}
835
836static void ntb_qp_link_work(struct work_struct *work)
837{
838 struct ntb_transport_qp *qp = container_of(work,
839 struct ntb_transport_qp,
840 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400841 struct pci_dev *pdev = qp->ndev->pdev;
842 struct ntb_transport_ctx *nt = qp->transport;
843 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700844
Allen Hubbee26a5842015-04-09 10:33:20 -0400845 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700846
Allen Hubbee26a5842015-04-09 10:33:20 -0400847 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700848
Allen Hubbee26a5842015-04-09 10:33:20 -0400849 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700850
851 /* query remote spad for qp ready bits */
Allen Hubbee26a5842015-04-09 10:33:20 -0400852 ntb_peer_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700853 dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val);
854
855 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400856 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700857 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400858 qp->link_is_up = true;
859
Jon Masonfce8a7b2012-11-16 19:27:12 -0700860 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400861 qp->event_handler(qp->cb_data, qp->link_is_up);
862 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700863 schedule_delayed_work(&qp->link_work,
864 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
865}
866
Allen Hubbee26a5842015-04-09 10:33:20 -0400867static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700868 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700869{
870 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400871 struct ntb_transport_mw *mw;
872 phys_addr_t mw_base;
873 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700874 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400875 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700876 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700877
Allen Hubbee26a5842015-04-09 10:33:20 -0400878 mw_count = nt->mw_count;
879 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700880
Allen Hubbee26a5842015-04-09 10:33:20 -0400881 mw_num = QP_TO_MW(nt, qp_num);
882 mw = &nt->mw_vec[mw_num];
883
884 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700885 qp->qp_num = qp_num;
886 qp->transport = nt;
887 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400888 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700889 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400890 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700891
Allen Hubbee26a5842015-04-09 10:33:20 -0400892 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
893 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -0700894 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400895 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -0700896
Allen Hubbee26a5842015-04-09 10:33:20 -0400897 mw_base = nt->mw_vec[mw_num].phys_addr;
898 mw_size = nt->mw_vec[mw_num].phys_size;
899
900 tx_size = (unsigned int)mw_size / num_qps_mw;
901 qp_offset = tx_size * qp_num / mw_count;
902
903 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -0700904 if (!qp->tx_mw)
905 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -0700906
Allen Hubbee26a5842015-04-09 10:33:20 -0400907 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -0700908 if (!qp->tx_mw_phys)
909 return -EINVAL;
910
911 tx_size -= sizeof(struct ntb_rx_info);
912 qp->rx_info = qp->tx_mw + tx_size;
913
Jon Masonc9d534c2013-02-01 15:45:16 -0700914 /* Due to housekeeping, there must be atleast 2 buffs */
915 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700916 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -0700917
Allen Hubbee26a5842015-04-09 10:33:20 -0400918 if (nt_debugfs_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700919 char debugfs_name[4];
920
921 snprintf(debugfs_name, 4, "qp%d", qp_num);
922 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Allen Hubbee26a5842015-04-09 10:33:20 -0400923 nt_debugfs_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700924
925 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
926 qp->debugfs_dir, qp,
927 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -0400928 } else {
929 qp->debugfs_dir = NULL;
930 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700931 }
932
933 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -0700934 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700935
936 spin_lock_init(&qp->ntb_rx_pend_q_lock);
937 spin_lock_init(&qp->ntb_rx_free_q_lock);
938 spin_lock_init(&qp->ntb_tx_free_q_lock);
939
940 INIT_LIST_HEAD(&qp->rx_pend_q);
941 INIT_LIST_HEAD(&qp->rx_free_q);
942 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -0700943
Allen Hubbee26a5842015-04-09 10:33:20 -0400944 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
945 (unsigned long)qp);
946
Jon Mason282a2fe2013-02-12 09:52:50 -0700947 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700948}
949
Allen Hubbee26a5842015-04-09 10:33:20 -0400950static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700951{
Allen Hubbee26a5842015-04-09 10:33:20 -0400952 struct ntb_transport_ctx *nt;
953 struct ntb_transport_mw *mw;
954 unsigned int mw_count, qp_count;
955 u64 qp_bitmap;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700956 int rc, i;
957
Allen Hubbee26a5842015-04-09 10:33:20 -0400958 if (ntb_db_is_unsafe(ndev))
959 dev_dbg(&ndev->dev,
960 "doorbell is unsafe, proceed anyway...\n");
961 if (ntb_spad_is_unsafe(ndev))
962 dev_dbg(&ndev->dev,
963 "scratchpad is unsafe, proceed anyway...\n");
964
965 nt = kzalloc(sizeof(*nt), GFP_KERNEL);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700966 if (!nt)
967 return -ENOMEM;
968
Allen Hubbee26a5842015-04-09 10:33:20 -0400969 nt->ndev = ndev;
970
971 mw_count = ntb_mw_count(ndev);
972
973 nt->mw_count = mw_count;
974
975 nt->mw_vec = kcalloc(mw_count, sizeof(*nt->mw_vec), GFP_KERNEL);
976 if (!nt->mw_vec) {
977 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700978 goto err;
979 }
980
Allen Hubbee26a5842015-04-09 10:33:20 -0400981 for (i = 0; i < mw_count; i++) {
982 mw = &nt->mw_vec[i];
983
984 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
985 &mw->xlat_align, &mw->xlat_align_size);
986 if (rc)
987 goto err1;
988
989 mw->vbase = ioremap(mw->phys_addr, mw->phys_size);
990 if (!mw->vbase) {
991 rc = -ENOMEM;
992 goto err1;
993 }
994
995 mw->buff_size = 0;
996 mw->xlat_size = 0;
997 mw->virt_addr = NULL;
998 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -0700999 }
1000
Allen Hubbee26a5842015-04-09 10:33:20 -04001001 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001002
Allen Hubbee26a5842015-04-09 10:33:20 -04001003 qp_count = ilog2(qp_bitmap);
1004 if (max_num_clients && max_num_clients < qp_count)
1005 qp_count = max_num_clients;
1006 else if (mw_count < qp_count)
1007 qp_count = mw_count;
1008
1009 qp_bitmap &= BIT_ULL(qp_count) - 1;
1010
1011 nt->qp_count = qp_count;
1012 nt->qp_bitmap = qp_bitmap;
1013 nt->qp_bitmap_free = qp_bitmap;
1014
1015 nt->qp_vec = kcalloc(qp_count, sizeof(*nt->qp_vec), GFP_KERNEL);
1016 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001017 rc = -ENOMEM;
Jon Mason948d3a62013-04-18 17:07:36 -07001018 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001019 }
1020
Allen Hubbee26a5842015-04-09 10:33:20 -04001021 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001022 rc = ntb_transport_init_queue(nt, i);
1023 if (rc)
1024 goto err3;
1025 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001026
1027 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001028 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001029
Allen Hubbee26a5842015-04-09 10:33:20 -04001030 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001031 if (rc)
Jon Mason948d3a62013-04-18 17:07:36 -07001032 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001033
1034 INIT_LIST_HEAD(&nt->client_devs);
1035 rc = ntb_bus_init(nt);
1036 if (rc)
Jon Mason948d3a62013-04-18 17:07:36 -07001037 goto err4;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001038
Allen Hubbee26a5842015-04-09 10:33:20 -04001039 nt->link_is_up = false;
1040 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1041 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001042
1043 return 0;
1044
Jon Mason948d3a62013-04-18 17:07:36 -07001045err4:
Allen Hubbee26a5842015-04-09 10:33:20 -04001046 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001047err3:
Allen Hubbee26a5842015-04-09 10:33:20 -04001048 kfree(nt->qp_vec);
Jon Mason948d3a62013-04-18 17:07:36 -07001049err2:
Allen Hubbee26a5842015-04-09 10:33:20 -04001050 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001051err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001052 while (i--) {
1053 mw = &nt->mw_vec[i];
1054 iounmap(mw->vbase);
1055 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001056err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001057 kfree(nt);
1058 return rc;
1059}
1060
Allen Hubbee26a5842015-04-09 10:33:20 -04001061static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001062{
Allen Hubbee26a5842015-04-09 10:33:20 -04001063 struct ntb_transport_ctx *nt = ndev->ctx;
1064 struct ntb_transport_qp *qp;
1065 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001066 int i;
1067
Jon Masonfca4d512013-09-09 13:39:55 -07001068 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001069 cancel_work_sync(&nt->link_cleanup);
1070 cancel_delayed_work_sync(&nt->link_work);
1071
1072 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001073
1074 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001075 for (i = 0; i < nt->qp_count; i++) {
1076 qp = &nt->qp_vec[i];
1077 if (qp_bitmap_alloc & BIT_ULL(i))
1078 ntb_transport_free_queue(qp);
1079 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001080 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001081
Allen Hubbee26a5842015-04-09 10:33:20 -04001082 ntb_link_disable(ndev);
1083 ntb_clear_ctx(ndev);
1084
Jon Masonfce8a7b2012-11-16 19:27:12 -07001085 ntb_bus_remove(nt);
1086
Allen Hubbee26a5842015-04-09 10:33:20 -04001087 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001088 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001089 iounmap(nt->mw_vec[i].vbase);
1090 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001091
Allen Hubbee26a5842015-04-09 10:33:20 -04001092 kfree(nt->qp_vec);
1093 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001094 kfree(nt);
1095}
1096
Jon Mason282a2fe2013-02-12 09:52:50 -07001097static void ntb_rx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001098{
Jon Mason282a2fe2013-02-12 09:52:50 -07001099 struct ntb_queue_entry *entry = data;
1100 struct ntb_transport_qp *qp = entry->qp;
Jon Mason448c6fb2013-01-19 02:02:27 -07001101 void *cb_data = entry->cb_data;
1102 unsigned int len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001103 struct ntb_payload_header *hdr = entry->rx_hdr;
Jon Mason448c6fb2013-01-19 02:02:27 -07001104
Jon Mason282a2fe2013-02-12 09:52:50 -07001105 hdr->flags = 0;
1106
1107 iowrite32(entry->index, &qp->rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001108
Jon Masonfce8a7b2012-11-16 19:27:12 -07001109 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
Jon Mason448c6fb2013-01-19 02:02:27 -07001110
Allen Hubbee26a5842015-04-09 10:33:20 -04001111 if (qp->rx_handler && qp->client_ready)
Jon Mason448c6fb2013-01-19 02:02:27 -07001112 qp->rx_handler(qp, qp->cb_data, cb_data, len);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001113}
1114
Jon Mason282a2fe2013-02-12 09:52:50 -07001115static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1116{
1117 void *buf = entry->buf;
1118 size_t len = entry->len;
1119
1120 memcpy(buf, offset, len);
1121
Allen Hubbee26a5842015-04-09 10:33:20 -04001122 /* Ensure that the data is fully copied out before clearing the flag */
1123 wmb();
1124
Jon Mason282a2fe2013-02-12 09:52:50 -07001125 ntb_rx_copy_callback(entry);
1126}
1127
1128static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset,
1129 size_t len)
1130{
1131 struct dma_async_tx_descriptor *txd;
1132 struct ntb_transport_qp *qp = entry->qp;
1133 struct dma_chan *chan = qp->dma_chan;
1134 struct dma_device *device;
1135 size_t pay_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001136 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001137 dma_cookie_t cookie;
1138 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001139
1140 entry->len = len;
1141
1142 if (!chan)
1143 goto err;
1144
Jon Mason53ca4fe2013-11-26 11:21:50 -07001145 if (len < copy_bytes)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001146 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001147
1148 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001149 pay_off = (size_t)offset & ~PAGE_MASK;
1150 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001151
1152 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001153 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001154
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001155 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1156 if (!unmap)
1157 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001158
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001159 unmap->len = len;
1160 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1161 pay_off, len, DMA_TO_DEVICE);
1162 if (dma_mapping_error(device->dev, unmap->addr[0]))
1163 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001164
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001165 unmap->to_cnt = 1;
1166
1167 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1168 buff_off, len, DMA_FROM_DEVICE);
1169 if (dma_mapping_error(device->dev, unmap->addr[1]))
1170 goto err_get_unmap;
1171
1172 unmap->from_cnt = 1;
1173
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001174 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001175 unmap->addr[0], len,
1176 DMA_PREP_INTERRUPT);
Jon Mason282a2fe2013-02-12 09:52:50 -07001177 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001178 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001179
1180 txd->callback = ntb_rx_copy_callback;
1181 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001182 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001183
1184 cookie = dmaengine_submit(txd);
1185 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001186 goto err_set_unmap;
1187
1188 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001189
1190 qp->last_cookie = cookie;
1191
1192 qp->rx_async++;
1193
1194 return;
1195
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001196err_set_unmap:
1197 dmaengine_unmap_put(unmap);
1198err_get_unmap:
1199 dmaengine_unmap_put(unmap);
1200err_wait:
Jon Mason282a2fe2013-02-12 09:52:50 -07001201 /* If the callbacks come out of order, the writing of the index to the
1202 * last completed will be out of order. This may result in the
1203 * receive stalling forever.
1204 */
1205 dma_sync_wait(chan, qp->last_cookie);
1206err:
1207 ntb_memcpy_rx(entry, offset);
1208 qp->rx_memcpy++;
1209}
1210
Jon Masonfce8a7b2012-11-16 19:27:12 -07001211static int ntb_process_rxc(struct ntb_transport_qp *qp)
1212{
1213 struct ntb_payload_header *hdr;
1214 struct ntb_queue_entry *entry;
1215 void *offset;
Allen Hubbee26a5842015-04-09 10:33:20 -04001216 int rc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001217
Jon Mason793c20e2013-01-19 02:02:26 -07001218 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1219 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1220
Allen Hubbee26a5842015-04-09 10:33:20 -04001221 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1222 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001223
Jon Masonfce8a7b2012-11-16 19:27:12 -07001224 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001225 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001226 qp->rx_ring_empty++;
1227 return -EAGAIN;
1228 }
1229
Allen Hubbee26a5842015-04-09 10:33:20 -04001230 if (hdr->flags & LINK_DOWN_FLAG) {
1231 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1232 ntb_qp_link_down(qp);
1233 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001234 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001235 }
1236
1237 if (hdr->ver != (u32)qp->rx_pkts) {
1238 dev_dbg(&qp->ndev->pdev->dev,
1239 "version mismatch, expected %llu - got %u\n",
1240 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001241 qp->rx_err_ver++;
1242 return -EIO;
1243 }
1244
Allen Hubbee26a5842015-04-09 10:33:20 -04001245 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1246 if (!entry) {
1247 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1248 qp->rx_err_no_buf++;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001249
Allen Hubbee26a5842015-04-09 10:33:20 -04001250 rc = -ENOMEM;
Jon Mason282a2fe2013-02-12 09:52:50 -07001251 goto err;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001252 }
1253
Allen Hubbee26a5842015-04-09 10:33:20 -04001254 if (hdr->len > entry->len) {
1255 dev_dbg(&qp->ndev->pdev->dev,
1256 "receive buffer overflow! Wanted %d got %d\n",
1257 hdr->len, entry->len);
1258 qp->rx_err_oflow++;
1259
1260 rc = -EIO;
1261 goto err;
1262 }
1263
1264 dev_dbg(&qp->ndev->pdev->dev,
1265 "RX OK index %u ver %u size %d into buf size %d\n",
Jon Mason793c20e2013-01-19 02:02:26 -07001266 qp->rx_index, hdr->ver, hdr->len, entry->len);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001267
Jon Mason282a2fe2013-02-12 09:52:50 -07001268 qp->rx_bytes += hdr->len;
1269 qp->rx_pkts++;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001270
Jon Mason282a2fe2013-02-12 09:52:50 -07001271 entry->index = qp->rx_index;
1272 entry->rx_hdr = hdr;
1273
1274 ntb_async_rx(entry, offset, hdr->len);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001275
Jon Mason282a2fe2013-02-12 09:52:50 -07001276 qp->rx_index++;
1277 qp->rx_index %= qp->rx_max_entry;
1278
1279 return 0;
1280
1281err:
Allen Hubbee26a5842015-04-09 10:33:20 -04001282 /* FIXME: if this syncrhonous update of the rx_index gets ahead of
1283 * asyncrhonous ntb_rx_copy_callback of previous entry, there are three
1284 * scenarios:
1285 *
1286 * 1) The peer might miss this update, but observe the update
1287 * from the memcpy completion callback. In this case, the buffer will
1288 * not be freed on the peer to be reused for a different packet. The
1289 * successful rx of a later packet would clear the condition, but the
1290 * condition could persist if several rx fail in a row.
1291 *
1292 * 2) The peer may observe this update before the asyncrhonous copy of
1293 * prior packets is completed. The peer may overwrite the buffers of
1294 * the prior packets before they are copied.
1295 *
1296 * 3) Both: the peer may observe the update, and then observe the index
1297 * decrement by the asynchronous completion callback. Who knows what
1298 * badness that will cause.
1299 */
Jon Mason793c20e2013-01-19 02:02:26 -07001300 hdr->flags = 0;
Jon Mason74465642013-01-21 15:28:52 -07001301 iowrite32(qp->rx_index, &qp->rx_info->entry);
Jon Mason793c20e2013-01-19 02:02:26 -07001302
Allen Hubbee26a5842015-04-09 10:33:20 -04001303 return rc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001304}
1305
Allen Hubbee26a5842015-04-09 10:33:20 -04001306static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001307{
Allen Hubbee26a5842015-04-09 10:33:20 -04001308 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001309 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001310
Allen Hubbee26a5842015-04-09 10:33:20 -04001311 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1312 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001313
Jon Masonc336acd2013-01-17 15:28:45 -07001314 /* Limit the number of packets processed in a single interrupt to
1315 * provide fairness to others
1316 */
1317 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001318 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001319 if (rc)
1320 break;
1321 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001322
1323 if (qp->dma_chan)
1324 dma_async_issue_pending(qp->dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001325
Allen Hubbee26a5842015-04-09 10:33:20 -04001326 if (i == qp->rx_max_entry) {
1327 /* there is more work to do */
1328 tasklet_schedule(&qp->rxc_db_work);
1329 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1330 /* the doorbell bit is set: clear it */
1331 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1332 /* ntb_db_read ensures ntb_db_clear write is committed */
1333 ntb_db_read(qp->ndev);
1334
1335 /* an interrupt may have arrived between finishing
1336 * ntb_process_rxc and clearing the doorbell bit:
1337 * there might be some more work to do.
1338 */
1339 tasklet_schedule(&qp->rxc_db_work);
1340 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001341}
1342
Jon Mason282a2fe2013-02-12 09:52:50 -07001343static void ntb_tx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001344{
Jon Mason282a2fe2013-02-12 09:52:50 -07001345 struct ntb_queue_entry *entry = data;
1346 struct ntb_transport_qp *qp = entry->qp;
1347 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001348
Jon Mason74465642013-01-21 15:28:52 -07001349 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001350
Allen Hubbee26a5842015-04-09 10:33:20 -04001351 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001352
1353 /* The entry length can only be zero if the packet is intended to be a
1354 * "link down" or similar. Since no payload is being sent in these
1355 * cases, there is nothing to add to the completion queue.
1356 */
1357 if (entry->len > 0) {
1358 qp->tx_bytes += entry->len;
1359
1360 if (qp->tx_handler)
1361 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1362 entry->len);
1363 }
1364
1365 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1366}
1367
Jon Mason282a2fe2013-02-12 09:52:50 -07001368static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1369{
1370 memcpy_toio(offset, entry->buf, entry->len);
1371
Allen Hubbee26a5842015-04-09 10:33:20 -04001372 /* Ensure that the data is fully copied out before setting the flags */
1373 wmb();
1374
Jon Mason282a2fe2013-02-12 09:52:50 -07001375 ntb_tx_copy_callback(entry);
1376}
1377
1378static void ntb_async_tx(struct ntb_transport_qp *qp,
1379 struct ntb_queue_entry *entry)
1380{
1381 struct ntb_payload_header __iomem *hdr;
1382 struct dma_async_tx_descriptor *txd;
1383 struct dma_chan *chan = qp->dma_chan;
1384 struct dma_device *device;
1385 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001386 struct dmaengine_unmap_data *unmap;
1387 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001388 dma_cookie_t cookie;
1389 void __iomem *offset;
1390 size_t len = entry->len;
1391 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001392
1393 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
1394 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1395 entry->tx_hdr = hdr;
1396
1397 iowrite32(entry->len, &hdr->len);
Allen Hubbee26a5842015-04-09 10:33:20 -04001398 iowrite32((u32)qp->tx_pkts, &hdr->ver);
Jon Mason282a2fe2013-02-12 09:52:50 -07001399
1400 if (!chan)
1401 goto err;
1402
1403 if (len < copy_bytes)
1404 goto err;
1405
1406 device = chan->device;
1407 dest = qp->tx_mw_phys + qp->tx_max_frame * qp->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001408 buff_off = (size_t)buf & ~PAGE_MASK;
1409 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001410
1411 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1412 goto err;
1413
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001414 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1415 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001416 goto err;
1417
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001418 unmap->len = len;
1419 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1420 buff_off, len, DMA_TO_DEVICE);
1421 if (dma_mapping_error(device->dev, unmap->addr[0]))
1422 goto err_get_unmap;
1423
1424 unmap->to_cnt = 1;
1425
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001426 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001427 DMA_PREP_INTERRUPT);
Jon Mason282a2fe2013-02-12 09:52:50 -07001428 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001429 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001430
1431 txd->callback = ntb_tx_copy_callback;
1432 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001433 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001434
1435 cookie = dmaengine_submit(txd);
1436 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001437 goto err_set_unmap;
1438
1439 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001440
1441 dma_async_issue_pending(chan);
1442 qp->tx_async++;
1443
1444 return;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001445err_set_unmap:
1446 dmaengine_unmap_put(unmap);
1447err_get_unmap:
1448 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001449err:
1450 ntb_memcpy_tx(entry, offset);
1451 qp->tx_memcpy++;
1452}
1453
Jon Masonfce8a7b2012-11-16 19:27:12 -07001454static int ntb_process_tx(struct ntb_transport_qp *qp,
1455 struct ntb_queue_entry *entry)
1456{
Jon Mason793c20e2013-01-19 02:02:26 -07001457 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001458 qp->tx_ring_full++;
1459 return -EAGAIN;
1460 }
1461
Jon Masonef114ed2013-01-19 02:02:18 -07001462 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001463 if (qp->tx_handler)
1464 qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1465
1466 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1467 &qp->tx_free_q);
1468 return 0;
1469 }
1470
Jon Mason282a2fe2013-02-12 09:52:50 -07001471 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001472
Jon Mason793c20e2013-01-19 02:02:26 -07001473 qp->tx_index++;
1474 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001475
1476 qp->tx_pkts++;
1477
1478 return 0;
1479}
1480
1481static void ntb_send_link_down(struct ntb_transport_qp *qp)
1482{
Allen Hubbee26a5842015-04-09 10:33:20 -04001483 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001484 struct ntb_queue_entry *entry;
1485 int i, rc;
1486
Allen Hubbee26a5842015-04-09 10:33:20 -04001487 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001488 return;
1489
Allen Hubbee22e0b92015-05-12 06:55:44 -04001490 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001491
1492 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001493 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001494 if (entry)
1495 break;
1496 msleep(100);
1497 }
1498
1499 if (!entry)
1500 return;
1501
1502 entry->cb_data = NULL;
1503 entry->buf = NULL;
1504 entry->len = 0;
1505 entry->flags = LINK_DOWN_FLAG;
1506
1507 rc = ntb_process_tx(qp, entry);
1508 if (rc)
1509 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1510 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001511
1512 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001513}
1514
1515/**
1516 * ntb_transport_create_queue - Create a new NTB transport layer queue
1517 * @rx_handler: receive callback function
1518 * @tx_handler: transmit callback function
1519 * @event_handler: event callback function
1520 *
1521 * Create a new NTB transport layer queue and provide the queue with a callback
1522 * routine for both transmit and receive. The receive callback routine will be
1523 * used to pass up data when the transport has received it on the queue. The
1524 * transmit callback routine will be called when the transport has completed the
1525 * transmission of the data on the queue and the data is ready to be freed.
1526 *
1527 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1528 */
1529struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001530ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001531 const struct ntb_queue_handlers *handlers)
1532{
Allen Hubbee26a5842015-04-09 10:33:20 -04001533 struct ntb_dev *ndev;
1534 struct pci_dev *pdev;
1535 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001536 struct ntb_queue_entry *entry;
1537 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001538 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001539 unsigned int free_queue;
Allen Hubbee26a5842015-04-09 10:33:20 -04001540 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001541
Allen Hubbee26a5842015-04-09 10:33:20 -04001542 ndev = dev_ntb(client_dev->parent);
1543 pdev = ndev->pdev;
1544 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001545
1546 free_queue = ffs(nt->qp_bitmap);
1547 if (!free_queue)
1548 goto err;
1549
1550 /* decrement free_queue to make it zero based */
1551 free_queue--;
1552
Allen Hubbee26a5842015-04-09 10:33:20 -04001553 qp = &nt->qp_vec[free_queue];
1554 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001555
Allen Hubbee26a5842015-04-09 10:33:20 -04001556 nt->qp_bitmap_free &= ~qp_bit;
1557
Jon Masonfce8a7b2012-11-16 19:27:12 -07001558 qp->cb_data = data;
1559 qp->rx_handler = handlers->rx_handler;
1560 qp->tx_handler = handlers->tx_handler;
1561 qp->event_handler = handlers->event_handler;
1562
Jon Mason94681192013-11-19 09:34:47 -07001563 dmaengine_get();
Jon Mason282a2fe2013-02-12 09:52:50 -07001564 qp->dma_chan = dma_find_channel(DMA_MEMCPY);
Jon Mason94681192013-11-19 09:34:47 -07001565 if (!qp->dma_chan) {
1566 dmaengine_put();
Jon Mason282a2fe2013-02-12 09:52:50 -07001567 dev_info(&pdev->dev, "Unable to allocate DMA channel, using CPU instead\n");
Jon Mason94681192013-11-19 09:34:47 -07001568 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001569
Jon Masonfce8a7b2012-11-16 19:27:12 -07001570 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001571 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001572 if (!entry)
1573 goto err1;
1574
Jon Mason282a2fe2013-02-12 09:52:50 -07001575 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001576 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001577 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001578 }
1579
1580 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001581 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001582 if (!entry)
1583 goto err2;
1584
Jon Mason282a2fe2013-02-12 09:52:50 -07001585 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001586 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001587 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001588 }
1589
Allen Hubbee26a5842015-04-09 10:33:20 -04001590 ntb_db_clear(qp->ndev, qp_bit);
1591 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001592
1593 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1594
1595 return qp;
1596
Jon Masonfce8a7b2012-11-16 19:27:12 -07001597err2:
Jon Masonf7667552013-01-19 02:02:24 -07001598 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001599 kfree(entry);
1600err1:
Jon Masonf7667552013-01-19 02:02:24 -07001601 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001602 kfree(entry);
Jon Mason94681192013-11-19 09:34:47 -07001603 if (qp->dma_chan)
1604 dmaengine_put();
Allen Hubbee26a5842015-04-09 10:33:20 -04001605 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001606err:
1607 return NULL;
1608}
1609EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1610
1611/**
1612 * ntb_transport_free_queue - Frees NTB transport queue
1613 * @qp: NTB queue to be freed
1614 *
1615 * Frees NTB transport queue
1616 */
1617void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1618{
Allen Hubbee26a5842015-04-09 10:33:20 -04001619 struct ntb_transport_ctx *nt = qp->transport;
Jon Mason186f27f2013-01-22 11:35:40 -07001620 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001621 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001622 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001623
1624 if (!qp)
1625 return;
1626
Allen Hubbee26a5842015-04-09 10:33:20 -04001627 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001628
Jon Mason282a2fe2013-02-12 09:52:50 -07001629 if (qp->dma_chan) {
1630 struct dma_chan *chan = qp->dma_chan;
1631 /* Putting the dma_chan to NULL will force any new traffic to be
1632 * processed by the CPU instead of the DAM engine
1633 */
1634 qp->dma_chan = NULL;
1635
1636 /* Try to be nice and wait for any queued DMA engine
1637 * transactions to process before smashing it with a rock
1638 */
1639 dma_sync_wait(chan, qp->last_cookie);
1640 dmaengine_terminate_all(chan);
1641 dmaengine_put();
1642 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001643
Allen Hubbee26a5842015-04-09 10:33:20 -04001644 qp_bit = BIT_ULL(qp->qp_num);
1645
1646 ntb_db_set_mask(qp->ndev, qp_bit);
1647 tasklet_disable(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001648
Jon Mason282a2fe2013-02-12 09:52:50 -07001649 cancel_delayed_work_sync(&qp->link_work);
1650
Allen Hubbee26a5842015-04-09 10:33:20 -04001651 qp->cb_data = NULL;
1652 qp->rx_handler = NULL;
1653 qp->tx_handler = NULL;
1654 qp->event_handler = NULL;
1655
Jon Masonf7667552013-01-19 02:02:24 -07001656 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001657 kfree(entry);
1658
Jon Masonf7667552013-01-19 02:02:24 -07001659 while ((entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q))) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001660 dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n");
1661 kfree(entry);
1662 }
1663
Jon Masonf7667552013-01-19 02:02:24 -07001664 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001665 kfree(entry);
1666
Allen Hubbee26a5842015-04-09 10:33:20 -04001667 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001668
1669 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1670}
1671EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1672
1673/**
1674 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1675 * @qp: NTB queue to be freed
1676 * @len: pointer to variable to write enqueued buffers length
1677 *
1678 * Dequeues unused buffers from receive queue. Should only be used during
1679 * shutdown of qp.
1680 *
1681 * RETURNS: NULL error value on error, or void* for success.
1682 */
1683void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1684{
1685 struct ntb_queue_entry *entry;
1686 void *buf;
1687
Allen Hubbee26a5842015-04-09 10:33:20 -04001688 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001689 return NULL;
1690
1691 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1692 if (!entry)
1693 return NULL;
1694
1695 buf = entry->cb_data;
1696 *len = entry->len;
1697
Jon Masonf7667552013-01-19 02:02:24 -07001698 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001699
1700 return buf;
1701}
1702EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1703
1704/**
1705 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1706 * @qp: NTB transport layer queue the entry is to be enqueued on
1707 * @cb: per buffer pointer for callback function to use
1708 * @data: pointer to data buffer that incoming packets will be copied into
1709 * @len: length of the data buffer
1710 *
1711 * Enqueue a new receive buffer onto the transport queue into which a NTB
1712 * payload can be received into.
1713 *
1714 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1715 */
1716int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1717 unsigned int len)
1718{
1719 struct ntb_queue_entry *entry;
1720
1721 if (!qp)
1722 return -EINVAL;
1723
1724 entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q);
1725 if (!entry)
1726 return -ENOMEM;
1727
1728 entry->cb_data = cb;
1729 entry->buf = data;
1730 entry->len = len;
1731
Jon Masonf7667552013-01-19 02:02:24 -07001732 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001733
1734 return 0;
1735}
1736EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1737
1738/**
1739 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1740 * @qp: NTB transport layer queue the entry is to be enqueued on
1741 * @cb: per buffer pointer for callback function to use
1742 * @data: pointer to data buffer that will be sent
1743 * @len: length of the data buffer
1744 *
1745 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07001746 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07001747 * serialize access to the qp.
1748 *
1749 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1750 */
1751int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1752 unsigned int len)
1753{
1754 struct ntb_queue_entry *entry;
1755 int rc;
1756
Allen Hubbee26a5842015-04-09 10:33:20 -04001757 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001758 return -EINVAL;
1759
1760 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001761 if (!entry) {
1762 qp->tx_err_no_buf++;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001763 return -ENOMEM;
Jon Mason282a2fe2013-02-12 09:52:50 -07001764 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001765
1766 entry->cb_data = cb;
1767 entry->buf = data;
1768 entry->len = len;
1769 entry->flags = 0;
1770
1771 rc = ntb_process_tx(qp, entry);
1772 if (rc)
1773 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1774 &qp->tx_free_q);
1775
1776 return rc;
1777}
1778EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1779
1780/**
1781 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1782 * @qp: NTB transport layer queue to be enabled
1783 *
1784 * Notify NTB transport layer of client readiness to use queue
1785 */
1786void ntb_transport_link_up(struct ntb_transport_qp *qp)
1787{
1788 if (!qp)
1789 return;
1790
Allen Hubbee26a5842015-04-09 10:33:20 -04001791 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001792
Allen Hubbee26a5842015-04-09 10:33:20 -04001793 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001794 schedule_delayed_work(&qp->link_work, 0);
1795}
1796EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1797
1798/**
1799 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1800 * @qp: NTB transport layer queue to be disabled
1801 *
1802 * Notify NTB transport layer of client's desire to no longer receive data on
1803 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07001804 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07001805 */
1806void ntb_transport_link_down(struct ntb_transport_qp *qp)
1807{
Jon Mason186f27f2013-01-22 11:35:40 -07001808 struct pci_dev *pdev;
Allen Hubbee26a5842015-04-09 10:33:20 -04001809 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001810
1811 if (!qp)
1812 return;
1813
Allen Hubbee26a5842015-04-09 10:33:20 -04001814 pdev = qp->ndev->pdev;
1815 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001816
Allen Hubbee26a5842015-04-09 10:33:20 -04001817 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001818
Allen Hubbee26a5842015-04-09 10:33:20 -04001819 ntb_peer_spad_write(qp->ndev, QP_LINKS,
1820 val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001821
Allen Hubbee26a5842015-04-09 10:33:20 -04001822 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001823 ntb_send_link_down(qp);
1824 else
1825 cancel_delayed_work_sync(&qp->link_work);
1826}
1827EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1828
1829/**
1830 * ntb_transport_link_query - Query transport link state
1831 * @qp: NTB transport layer queue to be queried
1832 *
1833 * Query connectivity to the remote system of the NTB transport queue
1834 *
1835 * RETURNS: true for link up or false for link down
1836 */
1837bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1838{
Jon Mason186f27f2013-01-22 11:35:40 -07001839 if (!qp)
1840 return false;
1841
Allen Hubbee26a5842015-04-09 10:33:20 -04001842 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001843}
1844EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1845
1846/**
1847 * ntb_transport_qp_num - Query the qp number
1848 * @qp: NTB transport layer queue to be queried
1849 *
1850 * Query qp number of the NTB transport queue
1851 *
1852 * RETURNS: a zero based number specifying the qp number
1853 */
1854unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1855{
Jon Mason186f27f2013-01-22 11:35:40 -07001856 if (!qp)
1857 return 0;
1858
Jon Masonfce8a7b2012-11-16 19:27:12 -07001859 return qp->qp_num;
1860}
1861EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1862
1863/**
1864 * ntb_transport_max_size - Query the max payload size of a qp
1865 * @qp: NTB transport layer queue to be queried
1866 *
1867 * Query the maximum payload size permissible on the given qp
1868 *
1869 * RETURNS: the max payload size of a qp
1870 */
Jon Masonef114ed2013-01-19 02:02:18 -07001871unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001872{
Jon Mason282a2fe2013-02-12 09:52:50 -07001873 unsigned int max;
1874
Jon Mason186f27f2013-01-22 11:35:40 -07001875 if (!qp)
1876 return 0;
1877
Jon Mason282a2fe2013-02-12 09:52:50 -07001878 if (!qp->dma_chan)
1879 return qp->tx_max_frame - sizeof(struct ntb_payload_header);
1880
1881 /* If DMA engine usage is possible, try to find the max size for that */
1882 max = qp->tx_max_frame - sizeof(struct ntb_payload_header);
1883 max -= max % (1 << qp->dma_chan->device->copy_align);
1884
1885 return max;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001886}
1887EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001888
1889static void ntb_transport_doorbell_callback(void *data, int vector)
1890{
1891 struct ntb_transport_ctx *nt = data;
1892 struct ntb_transport_qp *qp;
1893 u64 db_bits;
1894 unsigned int qp_num;
1895
1896 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
1897 ntb_db_vector_mask(nt->ndev, vector));
1898
1899 while (db_bits) {
1900 qp_num = __ffs(db_bits);
1901 qp = &nt->qp_vec[qp_num];
1902
1903 tasklet_schedule(&qp->rxc_db_work);
1904
1905 db_bits &= ~BIT_ULL(qp_num);
1906 }
1907}
1908
1909static const struct ntb_ctx_ops ntb_transport_ops = {
1910 .link_event = ntb_transport_event_callback,
1911 .db_event = ntb_transport_doorbell_callback,
1912};
1913
1914static struct ntb_client ntb_transport_client = {
1915 .ops = {
1916 .probe = ntb_transport_probe,
1917 .remove = ntb_transport_free,
1918 },
1919};
1920
1921static int __init ntb_transport_init(void)
1922{
1923 int rc;
1924
1925 if (debugfs_initialized())
1926 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1927
1928 rc = bus_register(&ntb_transport_bus);
1929 if (rc)
1930 goto err_bus;
1931
1932 rc = ntb_register_client(&ntb_transport_client);
1933 if (rc)
1934 goto err_client;
1935
1936 return 0;
1937
1938err_client:
1939 bus_unregister(&ntb_transport_bus);
1940err_bus:
1941 debugfs_remove_recursive(nt_debugfs_dir);
1942 return rc;
1943}
1944module_init(ntb_transport_init);
1945
1946static void __exit ntb_transport_exit(void)
1947{
1948 debugfs_remove_recursive(nt_debugfs_dir);
1949
1950 ntb_unregister_client(&ntb_transport_client);
1951 bus_unregister(&ntb_transport_bus);
1952}
1953module_exit(ntb_transport_exit);