blob: 0f86b60a0c0eeba501d05983e8dbb1907d79e3d2 [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
Jon Masonfca4d512013-09-09 13:39:55 -0700651static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700652{
Allen Hubbee26a5842015-04-09 10:33:20 -0400653 struct ntb_transport_ctx *nt = qp->transport;
654 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700655
Allen Hubbee26a5842015-04-09 10:33:20 -0400656 if (qp->link_is_up) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700657 cancel_delayed_work_sync(&qp->link_work);
658 return;
659 }
660
Allen Hubbee22e0b92015-05-12 06:55:44 -0400661 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400662 qp->link_is_up = false;
663
664 if (qp->event_handler)
665 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700666}
667
668static void ntb_qp_link_cleanup_work(struct work_struct *work)
669{
670 struct ntb_transport_qp *qp = container_of(work,
671 struct ntb_transport_qp,
672 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400673 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700674
675 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700676
Allen Hubbee26a5842015-04-09 10:33:20 -0400677 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700678 schedule_delayed_work(&qp->link_work,
679 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
680}
681
Jon Mason7b4f2d32013-01-19 02:02:19 -0700682static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700683{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700684 schedule_work(&qp->link_cleanup);
685}
686
Allen Hubbee26a5842015-04-09 10:33:20 -0400687static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700688{
Allen Hubbee26a5842015-04-09 10:33:20 -0400689 struct ntb_transport_qp *qp;
690 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700691 int i;
692
Allen Hubbee26a5842015-04-09 10:33:20 -0400693 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700694
Allen Hubbee26a5842015-04-09 10:33:20 -0400695 /* Pass along the info to any clients */
696 for (i = 0; i < nt->qp_count; i++)
697 if (qp_bitmap_alloc & BIT_ULL(i)) {
698 qp = &nt->qp_vec[i];
699 ntb_qp_link_cleanup(qp);
700 cancel_work_sync(&qp->link_cleanup);
701 cancel_delayed_work_sync(&qp->link_work);
702 }
703
704 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700705 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700706
Jon Masonfce8a7b2012-11-16 19:27:12 -0700707 /* The scratchpad registers keep the values if the remote side
708 * goes down, blast them now to give them a sane value the next
709 * time they are accessed
710 */
711 for (i = 0; i < MAX_SPAD; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400712 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700713}
714
Jon Masonfca4d512013-09-09 13:39:55 -0700715static void ntb_transport_link_cleanup_work(struct work_struct *work)
716{
Allen Hubbee26a5842015-04-09 10:33:20 -0400717 struct ntb_transport_ctx *nt =
718 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700719
720 ntb_transport_link_cleanup(nt);
721}
722
Allen Hubbee26a5842015-04-09 10:33:20 -0400723static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700724{
Allen Hubbee26a5842015-04-09 10:33:20 -0400725 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700726
Allen Hubbee26a5842015-04-09 10:33:20 -0400727 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700728 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400729 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700730 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700731}
732
733static void ntb_transport_link_work(struct work_struct *work)
734{
Allen Hubbee26a5842015-04-09 10:33:20 -0400735 struct ntb_transport_ctx *nt =
736 container_of(work, struct ntb_transport_ctx, link_work.work);
737 struct ntb_dev *ndev = nt->ndev;
738 struct pci_dev *pdev = ndev->pdev;
739 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700740 u32 val;
Allen Hubbee26a5842015-04-09 10:33:20 -0400741 int rc, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700742
Jon Mason113fc502013-01-30 11:40:52 -0700743 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400744 for (i = 0; i < nt->mw_count; i++) {
745 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700746
Allen Hubbee26a5842015-04-09 10:33:20 -0400747 if (max_mw_size && size > max_mw_size)
748 size = max_mw_size;
749
750 spad = MW0_SZ_HIGH + (i * 2);
751 ntb_peer_spad_write(ndev, spad, (u32)(size >> 32));
752
753 spad = MW0_SZ_LOW + (i * 2);
754 ntb_peer_spad_write(ndev, spad, (u32)size);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700755 }
756
Allen Hubbee26a5842015-04-09 10:33:20 -0400757 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700758
Allen Hubbee26a5842015-04-09 10:33:20 -0400759 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700760
Allen Hubbee26a5842015-04-09 10:33:20 -0400761 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700762
763 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400764 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400765 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700766 if (val != NTB_TRANSPORT_VERSION)
767 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700768
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400769 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700770 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400771 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700772 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700773
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400774 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700775 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400776 if (val != nt->mw_count)
777 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700778
Allen Hubbee26a5842015-04-09 10:33:20 -0400779 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700780 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700781
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400782 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400783 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700784
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400785 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700786 val64 |= val;
787
Allen Hubbee26a5842015-04-09 10:33:20 -0400788 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700789
790 rc = ntb_set_mw(nt, i, val64);
791 if (rc)
792 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700793 }
794
Allen Hubbee26a5842015-04-09 10:33:20 -0400795 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700796
Allen Hubbee26a5842015-04-09 10:33:20 -0400797 for (i = 0; i < nt->qp_count; i++) {
798 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700799
800 ntb_transport_setup_qp_mw(nt, i);
801
Allen Hubbee26a5842015-04-09 10:33:20 -0400802 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700803 schedule_delayed_work(&qp->link_work, 0);
804 }
805
806 return;
807
Jon Mason113fc502013-01-30 11:40:52 -0700808out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400809 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700810 ntb_free_mw(nt, i);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700811out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400812 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700813 schedule_delayed_work(&nt->link_work,
814 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
815}
816
817static void ntb_qp_link_work(struct work_struct *work)
818{
819 struct ntb_transport_qp *qp = container_of(work,
820 struct ntb_transport_qp,
821 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400822 struct pci_dev *pdev = qp->ndev->pdev;
823 struct ntb_transport_ctx *nt = qp->transport;
824 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700825
Allen Hubbee26a5842015-04-09 10:33:20 -0400826 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700827
Allen Hubbee26a5842015-04-09 10:33:20 -0400828 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700829
Allen Hubbee26a5842015-04-09 10:33:20 -0400830 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700831
832 /* query remote spad for qp ready bits */
Allen Hubbee26a5842015-04-09 10:33:20 -0400833 ntb_peer_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700834 dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val);
835
836 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400837 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700838 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400839 qp->link_is_up = true;
840
Jon Masonfce8a7b2012-11-16 19:27:12 -0700841 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400842 qp->event_handler(qp->cb_data, qp->link_is_up);
843 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700844 schedule_delayed_work(&qp->link_work,
845 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
846}
847
Allen Hubbee26a5842015-04-09 10:33:20 -0400848static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700849 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700850{
851 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400852 struct ntb_transport_mw *mw;
853 phys_addr_t mw_base;
854 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700855 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400856 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700857 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700858
Allen Hubbee26a5842015-04-09 10:33:20 -0400859 mw_count = nt->mw_count;
860 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700861
Allen Hubbee26a5842015-04-09 10:33:20 -0400862 mw_num = QP_TO_MW(nt, qp_num);
863 mw = &nt->mw_vec[mw_num];
864
865 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700866 qp->qp_num = qp_num;
867 qp->transport = nt;
868 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400869 qp->link_is_up = false;
870 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700871 qp->event_handler = NULL;
872
Allen Hubbee26a5842015-04-09 10:33:20 -0400873 if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
874 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -0700875 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400876 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -0700877
Allen Hubbee26a5842015-04-09 10:33:20 -0400878 mw_base = nt->mw_vec[mw_num].phys_addr;
879 mw_size = nt->mw_vec[mw_num].phys_size;
880
881 tx_size = (unsigned int)mw_size / num_qps_mw;
882 qp_offset = tx_size * qp_num / mw_count;
883
884 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -0700885 if (!qp->tx_mw)
886 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -0700887
Allen Hubbee26a5842015-04-09 10:33:20 -0400888 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -0700889 if (!qp->tx_mw_phys)
890 return -EINVAL;
891
892 tx_size -= sizeof(struct ntb_rx_info);
893 qp->rx_info = qp->tx_mw + tx_size;
894
Jon Masonc9d534c2013-02-01 15:45:16 -0700895 /* Due to housekeeping, there must be atleast 2 buffs */
896 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700897 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -0700898
Allen Hubbee26a5842015-04-09 10:33:20 -0400899 if (nt_debugfs_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700900 char debugfs_name[4];
901
902 snprintf(debugfs_name, 4, "qp%d", qp_num);
903 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Allen Hubbee26a5842015-04-09 10:33:20 -0400904 nt_debugfs_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700905
906 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
907 qp->debugfs_dir, qp,
908 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -0400909 } else {
910 qp->debugfs_dir = NULL;
911 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700912 }
913
914 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -0700915 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700916
917 spin_lock_init(&qp->ntb_rx_pend_q_lock);
918 spin_lock_init(&qp->ntb_rx_free_q_lock);
919 spin_lock_init(&qp->ntb_tx_free_q_lock);
920
921 INIT_LIST_HEAD(&qp->rx_pend_q);
922 INIT_LIST_HEAD(&qp->rx_free_q);
923 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -0700924
Allen Hubbee26a5842015-04-09 10:33:20 -0400925 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
926 (unsigned long)qp);
927
Jon Mason282a2fe2013-02-12 09:52:50 -0700928 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700929}
930
Allen Hubbee26a5842015-04-09 10:33:20 -0400931static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700932{
Allen Hubbee26a5842015-04-09 10:33:20 -0400933 struct ntb_transport_ctx *nt;
934 struct ntb_transport_mw *mw;
935 unsigned int mw_count, qp_count;
936 u64 qp_bitmap;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700937 int rc, i;
938
Allen Hubbee26a5842015-04-09 10:33:20 -0400939 if (ntb_db_is_unsafe(ndev))
940 dev_dbg(&ndev->dev,
941 "doorbell is unsafe, proceed anyway...\n");
942 if (ntb_spad_is_unsafe(ndev))
943 dev_dbg(&ndev->dev,
944 "scratchpad is unsafe, proceed anyway...\n");
945
946 nt = kzalloc(sizeof(*nt), GFP_KERNEL);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700947 if (!nt)
948 return -ENOMEM;
949
Allen Hubbee26a5842015-04-09 10:33:20 -0400950 nt->ndev = ndev;
951
952 mw_count = ntb_mw_count(ndev);
953
954 nt->mw_count = mw_count;
955
956 nt->mw_vec = kcalloc(mw_count, sizeof(*nt->mw_vec), GFP_KERNEL);
957 if (!nt->mw_vec) {
958 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700959 goto err;
960 }
961
Allen Hubbee26a5842015-04-09 10:33:20 -0400962 for (i = 0; i < mw_count; i++) {
963 mw = &nt->mw_vec[i];
964
965 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
966 &mw->xlat_align, &mw->xlat_align_size);
967 if (rc)
968 goto err1;
969
970 mw->vbase = ioremap(mw->phys_addr, mw->phys_size);
971 if (!mw->vbase) {
972 rc = -ENOMEM;
973 goto err1;
974 }
975
976 mw->buff_size = 0;
977 mw->xlat_size = 0;
978 mw->virt_addr = NULL;
979 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -0700980 }
981
Allen Hubbee26a5842015-04-09 10:33:20 -0400982 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700983
Allen Hubbee26a5842015-04-09 10:33:20 -0400984 qp_count = ilog2(qp_bitmap);
985 if (max_num_clients && max_num_clients < qp_count)
986 qp_count = max_num_clients;
987 else if (mw_count < qp_count)
988 qp_count = mw_count;
989
990 qp_bitmap &= BIT_ULL(qp_count) - 1;
991
992 nt->qp_count = qp_count;
993 nt->qp_bitmap = qp_bitmap;
994 nt->qp_bitmap_free = qp_bitmap;
995
996 nt->qp_vec = kcalloc(qp_count, sizeof(*nt->qp_vec), GFP_KERNEL);
997 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700998 rc = -ENOMEM;
Jon Mason948d3a62013-04-18 17:07:36 -0700999 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001000 }
1001
Allen Hubbee26a5842015-04-09 10:33:20 -04001002 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001003 rc = ntb_transport_init_queue(nt, i);
1004 if (rc)
1005 goto err3;
1006 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001007
1008 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001009 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001010
Allen Hubbee26a5842015-04-09 10:33:20 -04001011 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001012 if (rc)
Jon Mason948d3a62013-04-18 17:07:36 -07001013 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001014
1015 INIT_LIST_HEAD(&nt->client_devs);
1016 rc = ntb_bus_init(nt);
1017 if (rc)
Jon Mason948d3a62013-04-18 17:07:36 -07001018 goto err4;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001019
Allen Hubbee26a5842015-04-09 10:33:20 -04001020 nt->link_is_up = false;
1021 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1022 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001023
1024 return 0;
1025
Jon Mason948d3a62013-04-18 17:07:36 -07001026err4:
Allen Hubbee26a5842015-04-09 10:33:20 -04001027 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001028err3:
Allen Hubbee26a5842015-04-09 10:33:20 -04001029 kfree(nt->qp_vec);
Jon Mason948d3a62013-04-18 17:07:36 -07001030err2:
Allen Hubbee26a5842015-04-09 10:33:20 -04001031 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001032err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001033 while (i--) {
1034 mw = &nt->mw_vec[i];
1035 iounmap(mw->vbase);
1036 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001037err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001038 kfree(nt);
1039 return rc;
1040}
1041
Allen Hubbee26a5842015-04-09 10:33:20 -04001042static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001043{
Allen Hubbee26a5842015-04-09 10:33:20 -04001044 struct ntb_transport_ctx *nt = ndev->ctx;
1045 struct ntb_transport_qp *qp;
1046 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001047 int i;
1048
Jon Masonfca4d512013-09-09 13:39:55 -07001049 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001050 cancel_work_sync(&nt->link_cleanup);
1051 cancel_delayed_work_sync(&nt->link_work);
1052
1053 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001054
1055 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001056 for (i = 0; i < nt->qp_count; i++) {
1057 qp = &nt->qp_vec[i];
1058 if (qp_bitmap_alloc & BIT_ULL(i))
1059 ntb_transport_free_queue(qp);
1060 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001061 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001062
Allen Hubbee26a5842015-04-09 10:33:20 -04001063 ntb_link_disable(ndev);
1064 ntb_clear_ctx(ndev);
1065
Jon Masonfce8a7b2012-11-16 19:27:12 -07001066 ntb_bus_remove(nt);
1067
Allen Hubbee26a5842015-04-09 10:33:20 -04001068 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001069 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001070 iounmap(nt->mw_vec[i].vbase);
1071 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001072
Allen Hubbee26a5842015-04-09 10:33:20 -04001073 kfree(nt->qp_vec);
1074 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001075 kfree(nt);
1076}
1077
Jon Mason282a2fe2013-02-12 09:52:50 -07001078static void ntb_rx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001079{
Jon Mason282a2fe2013-02-12 09:52:50 -07001080 struct ntb_queue_entry *entry = data;
1081 struct ntb_transport_qp *qp = entry->qp;
Jon Mason448c6fb2013-01-19 02:02:27 -07001082 void *cb_data = entry->cb_data;
1083 unsigned int len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001084 struct ntb_payload_header *hdr = entry->rx_hdr;
Jon Mason448c6fb2013-01-19 02:02:27 -07001085
Jon Mason282a2fe2013-02-12 09:52:50 -07001086 hdr->flags = 0;
1087
1088 iowrite32(entry->index, &qp->rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001089
Jon Masonfce8a7b2012-11-16 19:27:12 -07001090 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
Jon Mason448c6fb2013-01-19 02:02:27 -07001091
Allen Hubbee26a5842015-04-09 10:33:20 -04001092 if (qp->rx_handler && qp->client_ready)
Jon Mason448c6fb2013-01-19 02:02:27 -07001093 qp->rx_handler(qp, qp->cb_data, cb_data, len);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001094}
1095
Jon Mason282a2fe2013-02-12 09:52:50 -07001096static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1097{
1098 void *buf = entry->buf;
1099 size_t len = entry->len;
1100
1101 memcpy(buf, offset, len);
1102
Allen Hubbee26a5842015-04-09 10:33:20 -04001103 /* Ensure that the data is fully copied out before clearing the flag */
1104 wmb();
1105
Jon Mason282a2fe2013-02-12 09:52:50 -07001106 ntb_rx_copy_callback(entry);
1107}
1108
1109static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset,
1110 size_t len)
1111{
1112 struct dma_async_tx_descriptor *txd;
1113 struct ntb_transport_qp *qp = entry->qp;
1114 struct dma_chan *chan = qp->dma_chan;
1115 struct dma_device *device;
1116 size_t pay_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001117 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001118 dma_cookie_t cookie;
1119 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001120
1121 entry->len = len;
1122
1123 if (!chan)
1124 goto err;
1125
Jon Mason53ca4fe2013-11-26 11:21:50 -07001126 if (len < copy_bytes)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001127 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001128
1129 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001130 pay_off = (size_t)offset & ~PAGE_MASK;
1131 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001132
1133 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001134 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001135
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001136 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1137 if (!unmap)
1138 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001139
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001140 unmap->len = len;
1141 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1142 pay_off, len, DMA_TO_DEVICE);
1143 if (dma_mapping_error(device->dev, unmap->addr[0]))
1144 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001145
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001146 unmap->to_cnt = 1;
1147
1148 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1149 buff_off, len, DMA_FROM_DEVICE);
1150 if (dma_mapping_error(device->dev, unmap->addr[1]))
1151 goto err_get_unmap;
1152
1153 unmap->from_cnt = 1;
1154
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001155 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001156 unmap->addr[0], len,
1157 DMA_PREP_INTERRUPT);
Jon Mason282a2fe2013-02-12 09:52:50 -07001158 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001159 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001160
1161 txd->callback = ntb_rx_copy_callback;
1162 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001163 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001164
1165 cookie = dmaengine_submit(txd);
1166 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001167 goto err_set_unmap;
1168
1169 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001170
1171 qp->last_cookie = cookie;
1172
1173 qp->rx_async++;
1174
1175 return;
1176
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001177err_set_unmap:
1178 dmaengine_unmap_put(unmap);
1179err_get_unmap:
1180 dmaengine_unmap_put(unmap);
1181err_wait:
Jon Mason282a2fe2013-02-12 09:52:50 -07001182 /* If the callbacks come out of order, the writing of the index to the
1183 * last completed will be out of order. This may result in the
1184 * receive stalling forever.
1185 */
1186 dma_sync_wait(chan, qp->last_cookie);
1187err:
1188 ntb_memcpy_rx(entry, offset);
1189 qp->rx_memcpy++;
1190}
1191
Jon Masonfce8a7b2012-11-16 19:27:12 -07001192static int ntb_process_rxc(struct ntb_transport_qp *qp)
1193{
1194 struct ntb_payload_header *hdr;
1195 struct ntb_queue_entry *entry;
1196 void *offset;
Allen Hubbee26a5842015-04-09 10:33:20 -04001197 int rc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001198
Jon Mason793c20e2013-01-19 02:02:26 -07001199 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1200 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1201
Allen Hubbee26a5842015-04-09 10:33:20 -04001202 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1203 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001204
Jon Masonfce8a7b2012-11-16 19:27:12 -07001205 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001206 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001207 qp->rx_ring_empty++;
1208 return -EAGAIN;
1209 }
1210
Allen Hubbee26a5842015-04-09 10:33:20 -04001211 if (hdr->flags & LINK_DOWN_FLAG) {
1212 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1213 ntb_qp_link_down(qp);
1214 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001215 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001216 }
1217
1218 if (hdr->ver != (u32)qp->rx_pkts) {
1219 dev_dbg(&qp->ndev->pdev->dev,
1220 "version mismatch, expected %llu - got %u\n",
1221 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001222 qp->rx_err_ver++;
1223 return -EIO;
1224 }
1225
Allen Hubbee26a5842015-04-09 10:33:20 -04001226 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1227 if (!entry) {
1228 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1229 qp->rx_err_no_buf++;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001230
Allen Hubbee26a5842015-04-09 10:33:20 -04001231 rc = -ENOMEM;
Jon Mason282a2fe2013-02-12 09:52:50 -07001232 goto err;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001233 }
1234
Allen Hubbee26a5842015-04-09 10:33:20 -04001235 if (hdr->len > entry->len) {
1236 dev_dbg(&qp->ndev->pdev->dev,
1237 "receive buffer overflow! Wanted %d got %d\n",
1238 hdr->len, entry->len);
1239 qp->rx_err_oflow++;
1240
1241 rc = -EIO;
1242 goto err;
1243 }
1244
1245 dev_dbg(&qp->ndev->pdev->dev,
1246 "RX OK index %u ver %u size %d into buf size %d\n",
Jon Mason793c20e2013-01-19 02:02:26 -07001247 qp->rx_index, hdr->ver, hdr->len, entry->len);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001248
Jon Mason282a2fe2013-02-12 09:52:50 -07001249 qp->rx_bytes += hdr->len;
1250 qp->rx_pkts++;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001251
Jon Mason282a2fe2013-02-12 09:52:50 -07001252 entry->index = qp->rx_index;
1253 entry->rx_hdr = hdr;
1254
1255 ntb_async_rx(entry, offset, hdr->len);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001256
Jon Mason282a2fe2013-02-12 09:52:50 -07001257 qp->rx_index++;
1258 qp->rx_index %= qp->rx_max_entry;
1259
1260 return 0;
1261
1262err:
Allen Hubbee26a5842015-04-09 10:33:20 -04001263 /* FIXME: if this syncrhonous update of the rx_index gets ahead of
1264 * asyncrhonous ntb_rx_copy_callback of previous entry, there are three
1265 * scenarios:
1266 *
1267 * 1) The peer might miss this update, but observe the update
1268 * from the memcpy completion callback. In this case, the buffer will
1269 * not be freed on the peer to be reused for a different packet. The
1270 * successful rx of a later packet would clear the condition, but the
1271 * condition could persist if several rx fail in a row.
1272 *
1273 * 2) The peer may observe this update before the asyncrhonous copy of
1274 * prior packets is completed. The peer may overwrite the buffers of
1275 * the prior packets before they are copied.
1276 *
1277 * 3) Both: the peer may observe the update, and then observe the index
1278 * decrement by the asynchronous completion callback. Who knows what
1279 * badness that will cause.
1280 */
Jon Mason793c20e2013-01-19 02:02:26 -07001281 hdr->flags = 0;
Jon Mason74465642013-01-21 15:28:52 -07001282 iowrite32(qp->rx_index, &qp->rx_info->entry);
Jon Mason793c20e2013-01-19 02:02:26 -07001283
Allen Hubbee26a5842015-04-09 10:33:20 -04001284 return rc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001285}
1286
Allen Hubbee26a5842015-04-09 10:33:20 -04001287static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001288{
Allen Hubbee26a5842015-04-09 10:33:20 -04001289 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001290 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001291
Allen Hubbee26a5842015-04-09 10:33:20 -04001292 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1293 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001294
Jon Masonc336acd2013-01-17 15:28:45 -07001295 /* Limit the number of packets processed in a single interrupt to
1296 * provide fairness to others
1297 */
1298 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001299 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001300 if (rc)
1301 break;
1302 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001303
1304 if (qp->dma_chan)
1305 dma_async_issue_pending(qp->dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001306
Allen Hubbee26a5842015-04-09 10:33:20 -04001307 if (i == qp->rx_max_entry) {
1308 /* there is more work to do */
1309 tasklet_schedule(&qp->rxc_db_work);
1310 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1311 /* the doorbell bit is set: clear it */
1312 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1313 /* ntb_db_read ensures ntb_db_clear write is committed */
1314 ntb_db_read(qp->ndev);
1315
1316 /* an interrupt may have arrived between finishing
1317 * ntb_process_rxc and clearing the doorbell bit:
1318 * there might be some more work to do.
1319 */
1320 tasklet_schedule(&qp->rxc_db_work);
1321 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001322}
1323
Jon Mason282a2fe2013-02-12 09:52:50 -07001324static void ntb_tx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001325{
Jon Mason282a2fe2013-02-12 09:52:50 -07001326 struct ntb_queue_entry *entry = data;
1327 struct ntb_transport_qp *qp = entry->qp;
1328 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001329
Jon Mason74465642013-01-21 15:28:52 -07001330 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001331
Allen Hubbee26a5842015-04-09 10:33:20 -04001332 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001333
1334 /* The entry length can only be zero if the packet is intended to be a
1335 * "link down" or similar. Since no payload is being sent in these
1336 * cases, there is nothing to add to the completion queue.
1337 */
1338 if (entry->len > 0) {
1339 qp->tx_bytes += entry->len;
1340
1341 if (qp->tx_handler)
1342 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1343 entry->len);
1344 }
1345
1346 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1347}
1348
Jon Mason282a2fe2013-02-12 09:52:50 -07001349static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1350{
1351 memcpy_toio(offset, entry->buf, entry->len);
1352
Allen Hubbee26a5842015-04-09 10:33:20 -04001353 /* Ensure that the data is fully copied out before setting the flags */
1354 wmb();
1355
Jon Mason282a2fe2013-02-12 09:52:50 -07001356 ntb_tx_copy_callback(entry);
1357}
1358
1359static void ntb_async_tx(struct ntb_transport_qp *qp,
1360 struct ntb_queue_entry *entry)
1361{
1362 struct ntb_payload_header __iomem *hdr;
1363 struct dma_async_tx_descriptor *txd;
1364 struct dma_chan *chan = qp->dma_chan;
1365 struct dma_device *device;
1366 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001367 struct dmaengine_unmap_data *unmap;
1368 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001369 dma_cookie_t cookie;
1370 void __iomem *offset;
1371 size_t len = entry->len;
1372 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001373
1374 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
1375 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1376 entry->tx_hdr = hdr;
1377
1378 iowrite32(entry->len, &hdr->len);
Allen Hubbee26a5842015-04-09 10:33:20 -04001379 iowrite32((u32)qp->tx_pkts, &hdr->ver);
Jon Mason282a2fe2013-02-12 09:52:50 -07001380
1381 if (!chan)
1382 goto err;
1383
1384 if (len < copy_bytes)
1385 goto err;
1386
1387 device = chan->device;
1388 dest = qp->tx_mw_phys + qp->tx_max_frame * qp->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001389 buff_off = (size_t)buf & ~PAGE_MASK;
1390 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001391
1392 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1393 goto err;
1394
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001395 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1396 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001397 goto err;
1398
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001399 unmap->len = len;
1400 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1401 buff_off, len, DMA_TO_DEVICE);
1402 if (dma_mapping_error(device->dev, unmap->addr[0]))
1403 goto err_get_unmap;
1404
1405 unmap->to_cnt = 1;
1406
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001407 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001408 DMA_PREP_INTERRUPT);
Jon Mason282a2fe2013-02-12 09:52:50 -07001409 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001410 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001411
1412 txd->callback = ntb_tx_copy_callback;
1413 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001414 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001415
1416 cookie = dmaengine_submit(txd);
1417 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001418 goto err_set_unmap;
1419
1420 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001421
1422 dma_async_issue_pending(chan);
1423 qp->tx_async++;
1424
1425 return;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001426err_set_unmap:
1427 dmaengine_unmap_put(unmap);
1428err_get_unmap:
1429 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001430err:
1431 ntb_memcpy_tx(entry, offset);
1432 qp->tx_memcpy++;
1433}
1434
Jon Masonfce8a7b2012-11-16 19:27:12 -07001435static int ntb_process_tx(struct ntb_transport_qp *qp,
1436 struct ntb_queue_entry *entry)
1437{
Jon Mason793c20e2013-01-19 02:02:26 -07001438 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001439 qp->tx_ring_full++;
1440 return -EAGAIN;
1441 }
1442
Jon Masonef114ed2013-01-19 02:02:18 -07001443 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001444 if (qp->tx_handler)
1445 qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1446
1447 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1448 &qp->tx_free_q);
1449 return 0;
1450 }
1451
Jon Mason282a2fe2013-02-12 09:52:50 -07001452 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001453
Jon Mason793c20e2013-01-19 02:02:26 -07001454 qp->tx_index++;
1455 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001456
1457 qp->tx_pkts++;
1458
1459 return 0;
1460}
1461
1462static void ntb_send_link_down(struct ntb_transport_qp *qp)
1463{
Allen Hubbee26a5842015-04-09 10:33:20 -04001464 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001465 struct ntb_queue_entry *entry;
1466 int i, rc;
1467
Allen Hubbee26a5842015-04-09 10:33:20 -04001468 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001469 return;
1470
Allen Hubbee26a5842015-04-09 10:33:20 -04001471 qp->link_is_up = false;
Allen Hubbee22e0b92015-05-12 06:55:44 -04001472 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001473
1474 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001475 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001476 if (entry)
1477 break;
1478 msleep(100);
1479 }
1480
1481 if (!entry)
1482 return;
1483
1484 entry->cb_data = NULL;
1485 entry->buf = NULL;
1486 entry->len = 0;
1487 entry->flags = LINK_DOWN_FLAG;
1488
1489 rc = ntb_process_tx(qp, entry);
1490 if (rc)
1491 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1492 qp->qp_num);
1493}
1494
1495/**
1496 * ntb_transport_create_queue - Create a new NTB transport layer queue
1497 * @rx_handler: receive callback function
1498 * @tx_handler: transmit callback function
1499 * @event_handler: event callback function
1500 *
1501 * Create a new NTB transport layer queue and provide the queue with a callback
1502 * routine for both transmit and receive. The receive callback routine will be
1503 * used to pass up data when the transport has received it on the queue. The
1504 * transmit callback routine will be called when the transport has completed the
1505 * transmission of the data on the queue and the data is ready to be freed.
1506 *
1507 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1508 */
1509struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001510ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001511 const struct ntb_queue_handlers *handlers)
1512{
Allen Hubbee26a5842015-04-09 10:33:20 -04001513 struct ntb_dev *ndev;
1514 struct pci_dev *pdev;
1515 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001516 struct ntb_queue_entry *entry;
1517 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001518 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001519 unsigned int free_queue;
Allen Hubbee26a5842015-04-09 10:33:20 -04001520 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001521
Allen Hubbee26a5842015-04-09 10:33:20 -04001522 ndev = dev_ntb(client_dev->parent);
1523 pdev = ndev->pdev;
1524 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001525
1526 free_queue = ffs(nt->qp_bitmap);
1527 if (!free_queue)
1528 goto err;
1529
1530 /* decrement free_queue to make it zero based */
1531 free_queue--;
1532
Allen Hubbee26a5842015-04-09 10:33:20 -04001533 qp = &nt->qp_vec[free_queue];
1534 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001535
Allen Hubbee26a5842015-04-09 10:33:20 -04001536 nt->qp_bitmap_free &= ~qp_bit;
1537
Jon Masonfce8a7b2012-11-16 19:27:12 -07001538 qp->cb_data = data;
1539 qp->rx_handler = handlers->rx_handler;
1540 qp->tx_handler = handlers->tx_handler;
1541 qp->event_handler = handlers->event_handler;
1542
Jon Mason94681192013-11-19 09:34:47 -07001543 dmaengine_get();
Jon Mason282a2fe2013-02-12 09:52:50 -07001544 qp->dma_chan = dma_find_channel(DMA_MEMCPY);
Jon Mason94681192013-11-19 09:34:47 -07001545 if (!qp->dma_chan) {
1546 dmaengine_put();
Jon Mason282a2fe2013-02-12 09:52:50 -07001547 dev_info(&pdev->dev, "Unable to allocate DMA channel, using CPU instead\n");
Jon Mason94681192013-11-19 09:34:47 -07001548 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001549
Jon Masonfce8a7b2012-11-16 19:27:12 -07001550 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001551 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001552 if (!entry)
1553 goto err1;
1554
Jon Mason282a2fe2013-02-12 09:52:50 -07001555 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001556 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001557 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001558 }
1559
1560 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001561 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001562 if (!entry)
1563 goto err2;
1564
Jon Mason282a2fe2013-02-12 09:52:50 -07001565 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001566 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001567 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001568 }
1569
Allen Hubbee26a5842015-04-09 10:33:20 -04001570 ntb_db_clear(qp->ndev, qp_bit);
1571 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001572
1573 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1574
1575 return qp;
1576
Jon Masonfce8a7b2012-11-16 19:27:12 -07001577err2:
Jon Masonf7667552013-01-19 02:02:24 -07001578 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001579 kfree(entry);
1580err1:
Jon Masonf7667552013-01-19 02:02:24 -07001581 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001582 kfree(entry);
Jon Mason94681192013-11-19 09:34:47 -07001583 if (qp->dma_chan)
1584 dmaengine_put();
Allen Hubbee26a5842015-04-09 10:33:20 -04001585 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001586err:
1587 return NULL;
1588}
1589EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1590
1591/**
1592 * ntb_transport_free_queue - Frees NTB transport queue
1593 * @qp: NTB queue to be freed
1594 *
1595 * Frees NTB transport queue
1596 */
1597void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1598{
Allen Hubbee26a5842015-04-09 10:33:20 -04001599 struct ntb_transport_ctx *nt = qp->transport;
Jon Mason186f27f2013-01-22 11:35:40 -07001600 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001601 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001602 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001603
1604 if (!qp)
1605 return;
1606
Allen Hubbee26a5842015-04-09 10:33:20 -04001607 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001608
Jon Mason282a2fe2013-02-12 09:52:50 -07001609 if (qp->dma_chan) {
1610 struct dma_chan *chan = qp->dma_chan;
1611 /* Putting the dma_chan to NULL will force any new traffic to be
1612 * processed by the CPU instead of the DAM engine
1613 */
1614 qp->dma_chan = NULL;
1615
1616 /* Try to be nice and wait for any queued DMA engine
1617 * transactions to process before smashing it with a rock
1618 */
1619 dma_sync_wait(chan, qp->last_cookie);
1620 dmaengine_terminate_all(chan);
1621 dmaengine_put();
1622 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001623
Allen Hubbee26a5842015-04-09 10:33:20 -04001624 qp_bit = BIT_ULL(qp->qp_num);
1625
1626 ntb_db_set_mask(qp->ndev, qp_bit);
1627 tasklet_disable(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001628
Jon Mason282a2fe2013-02-12 09:52:50 -07001629 cancel_delayed_work_sync(&qp->link_work);
1630
Allen Hubbee26a5842015-04-09 10:33:20 -04001631 qp->cb_data = NULL;
1632 qp->rx_handler = NULL;
1633 qp->tx_handler = NULL;
1634 qp->event_handler = NULL;
1635
Jon Masonf7667552013-01-19 02:02:24 -07001636 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001637 kfree(entry);
1638
Jon Masonf7667552013-01-19 02:02:24 -07001639 while ((entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q))) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001640 dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n");
1641 kfree(entry);
1642 }
1643
Jon Masonf7667552013-01-19 02:02:24 -07001644 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001645 kfree(entry);
1646
Allen Hubbee26a5842015-04-09 10:33:20 -04001647 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001648
1649 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1650}
1651EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1652
1653/**
1654 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1655 * @qp: NTB queue to be freed
1656 * @len: pointer to variable to write enqueued buffers length
1657 *
1658 * Dequeues unused buffers from receive queue. Should only be used during
1659 * shutdown of qp.
1660 *
1661 * RETURNS: NULL error value on error, or void* for success.
1662 */
1663void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1664{
1665 struct ntb_queue_entry *entry;
1666 void *buf;
1667
Allen Hubbee26a5842015-04-09 10:33:20 -04001668 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001669 return NULL;
1670
1671 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1672 if (!entry)
1673 return NULL;
1674
1675 buf = entry->cb_data;
1676 *len = entry->len;
1677
Jon Masonf7667552013-01-19 02:02:24 -07001678 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001679
1680 return buf;
1681}
1682EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1683
1684/**
1685 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1686 * @qp: NTB transport layer queue the entry is to be enqueued on
1687 * @cb: per buffer pointer for callback function to use
1688 * @data: pointer to data buffer that incoming packets will be copied into
1689 * @len: length of the data buffer
1690 *
1691 * Enqueue a new receive buffer onto the transport queue into which a NTB
1692 * payload can be received into.
1693 *
1694 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1695 */
1696int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1697 unsigned int len)
1698{
1699 struct ntb_queue_entry *entry;
1700
1701 if (!qp)
1702 return -EINVAL;
1703
1704 entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q);
1705 if (!entry)
1706 return -ENOMEM;
1707
1708 entry->cb_data = cb;
1709 entry->buf = data;
1710 entry->len = len;
1711
Jon Masonf7667552013-01-19 02:02:24 -07001712 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001713
1714 return 0;
1715}
1716EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1717
1718/**
1719 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1720 * @qp: NTB transport layer queue the entry is to be enqueued on
1721 * @cb: per buffer pointer for callback function to use
1722 * @data: pointer to data buffer that will be sent
1723 * @len: length of the data buffer
1724 *
1725 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07001726 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07001727 * serialize access to the qp.
1728 *
1729 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1730 */
1731int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1732 unsigned int len)
1733{
1734 struct ntb_queue_entry *entry;
1735 int rc;
1736
Allen Hubbee26a5842015-04-09 10:33:20 -04001737 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001738 return -EINVAL;
1739
1740 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001741 if (!entry) {
1742 qp->tx_err_no_buf++;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001743 return -ENOMEM;
Jon Mason282a2fe2013-02-12 09:52:50 -07001744 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001745
1746 entry->cb_data = cb;
1747 entry->buf = data;
1748 entry->len = len;
1749 entry->flags = 0;
1750
1751 rc = ntb_process_tx(qp, entry);
1752 if (rc)
1753 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1754 &qp->tx_free_q);
1755
1756 return rc;
1757}
1758EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1759
1760/**
1761 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1762 * @qp: NTB transport layer queue to be enabled
1763 *
1764 * Notify NTB transport layer of client readiness to use queue
1765 */
1766void ntb_transport_link_up(struct ntb_transport_qp *qp)
1767{
1768 if (!qp)
1769 return;
1770
Allen Hubbee26a5842015-04-09 10:33:20 -04001771 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001772
Allen Hubbee26a5842015-04-09 10:33:20 -04001773 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001774 schedule_delayed_work(&qp->link_work, 0);
1775}
1776EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1777
1778/**
1779 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1780 * @qp: NTB transport layer queue to be disabled
1781 *
1782 * Notify NTB transport layer of client's desire to no longer receive data on
1783 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07001784 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07001785 */
1786void ntb_transport_link_down(struct ntb_transport_qp *qp)
1787{
Jon Mason186f27f2013-01-22 11:35:40 -07001788 struct pci_dev *pdev;
Allen Hubbee26a5842015-04-09 10:33:20 -04001789 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001790
1791 if (!qp)
1792 return;
1793
Allen Hubbee26a5842015-04-09 10:33:20 -04001794 pdev = qp->ndev->pdev;
1795 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001796
Allen Hubbee26a5842015-04-09 10:33:20 -04001797 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001798
Allen Hubbee26a5842015-04-09 10:33:20 -04001799 ntb_peer_spad_write(qp->ndev, QP_LINKS,
1800 val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001801
Allen Hubbee26a5842015-04-09 10:33:20 -04001802 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001803 ntb_send_link_down(qp);
1804 else
1805 cancel_delayed_work_sync(&qp->link_work);
1806}
1807EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1808
1809/**
1810 * ntb_transport_link_query - Query transport link state
1811 * @qp: NTB transport layer queue to be queried
1812 *
1813 * Query connectivity to the remote system of the NTB transport queue
1814 *
1815 * RETURNS: true for link up or false for link down
1816 */
1817bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1818{
Jon Mason186f27f2013-01-22 11:35:40 -07001819 if (!qp)
1820 return false;
1821
Allen Hubbee26a5842015-04-09 10:33:20 -04001822 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001823}
1824EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1825
1826/**
1827 * ntb_transport_qp_num - Query the qp number
1828 * @qp: NTB transport layer queue to be queried
1829 *
1830 * Query qp number of the NTB transport queue
1831 *
1832 * RETURNS: a zero based number specifying the qp number
1833 */
1834unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1835{
Jon Mason186f27f2013-01-22 11:35:40 -07001836 if (!qp)
1837 return 0;
1838
Jon Masonfce8a7b2012-11-16 19:27:12 -07001839 return qp->qp_num;
1840}
1841EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1842
1843/**
1844 * ntb_transport_max_size - Query the max payload size of a qp
1845 * @qp: NTB transport layer queue to be queried
1846 *
1847 * Query the maximum payload size permissible on the given qp
1848 *
1849 * RETURNS: the max payload size of a qp
1850 */
Jon Masonef114ed2013-01-19 02:02:18 -07001851unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001852{
Jon Mason282a2fe2013-02-12 09:52:50 -07001853 unsigned int max;
1854
Jon Mason186f27f2013-01-22 11:35:40 -07001855 if (!qp)
1856 return 0;
1857
Jon Mason282a2fe2013-02-12 09:52:50 -07001858 if (!qp->dma_chan)
1859 return qp->tx_max_frame - sizeof(struct ntb_payload_header);
1860
1861 /* If DMA engine usage is possible, try to find the max size for that */
1862 max = qp->tx_max_frame - sizeof(struct ntb_payload_header);
1863 max -= max % (1 << qp->dma_chan->device->copy_align);
1864
1865 return max;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001866}
1867EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001868
1869static void ntb_transport_doorbell_callback(void *data, int vector)
1870{
1871 struct ntb_transport_ctx *nt = data;
1872 struct ntb_transport_qp *qp;
1873 u64 db_bits;
1874 unsigned int qp_num;
1875
1876 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
1877 ntb_db_vector_mask(nt->ndev, vector));
1878
1879 while (db_bits) {
1880 qp_num = __ffs(db_bits);
1881 qp = &nt->qp_vec[qp_num];
1882
1883 tasklet_schedule(&qp->rxc_db_work);
1884
1885 db_bits &= ~BIT_ULL(qp_num);
1886 }
1887}
1888
1889static const struct ntb_ctx_ops ntb_transport_ops = {
1890 .link_event = ntb_transport_event_callback,
1891 .db_event = ntb_transport_doorbell_callback,
1892};
1893
1894static struct ntb_client ntb_transport_client = {
1895 .ops = {
1896 .probe = ntb_transport_probe,
1897 .remove = ntb_transport_free,
1898 },
1899};
1900
1901static int __init ntb_transport_init(void)
1902{
1903 int rc;
1904
1905 if (debugfs_initialized())
1906 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1907
1908 rc = bus_register(&ntb_transport_bus);
1909 if (rc)
1910 goto err_bus;
1911
1912 rc = ntb_register_client(&ntb_transport_client);
1913 if (rc)
1914 goto err_client;
1915
1916 return 0;
1917
1918err_client:
1919 bus_unregister(&ntb_transport_bus);
1920err_bus:
1921 debugfs_remove_recursive(nt_debugfs_dir);
1922 return rc;
1923}
1924module_init(ntb_transport_init);
1925
1926static void __exit ntb_transport_exit(void)
1927{
1928 debugfs_remove_recursive(nt_debugfs_dir);
1929
1930 ntb_unregister_client(&ntb_transport_client);
1931 bus_unregister(&ntb_transport_bus);
1932}
1933module_exit(ntb_transport_exit);