blob: e9bf2f47b61ada12a6730990c433d57a71ce90ce [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.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * BSD LICENSE
14 *
15 * Copyright(c) 2012 Intel Corporation. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 *
21 * * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * * Redistributions in binary form must reproduce the above copy
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * * Neither the name of Intel Corporation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 * Intel PCIe NTB Linux driver
44 *
45 * Contact Information:
46 * Jon Mason <jon.mason@intel.com>
47 */
48#include <linux/debugfs.h>
49#include <linux/delay.h>
Jon Mason282a2fe2013-02-12 09:52:50 -070050#include <linux/dmaengine.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070051#include <linux/dma-mapping.h>
52#include <linux/errno.h>
53#include <linux/export.h>
54#include <linux/interrupt.h>
55#include <linux/module.h>
56#include <linux/pci.h>
57#include <linux/slab.h>
58#include <linux/types.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070059#include "ntb_hw.h"
60
Jon Mason113fc502013-01-30 11:40:52 -070061#define NTB_TRANSPORT_VERSION 3
Jon Masonfce8a7b2012-11-16 19:27:12 -070062
Jon Masonef114ed2013-01-19 02:02:18 -070063static unsigned int transport_mtu = 0x401E;
Jon Masonfce8a7b2012-11-16 19:27:12 -070064module_param(transport_mtu, uint, 0644);
65MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
66
Jon Mason948d3a62013-04-18 17:07:36 -070067static unsigned char max_num_clients;
Jon Masonfce8a7b2012-11-16 19:27:12 -070068module_param(max_num_clients, byte, 0644);
69MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
70
Jon Mason282a2fe2013-02-12 09:52:50 -070071static unsigned int copy_bytes = 1024;
72module_param(copy_bytes, uint, 0644);
73MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
74
Jon Masonfce8a7b2012-11-16 19:27:12 -070075struct ntb_queue_entry {
76 /* ntb_queue list reference */
77 struct list_head entry;
78 /* pointers to data to be transfered */
79 void *cb_data;
80 void *buf;
81 unsigned int len;
82 unsigned int flags;
Jon Mason282a2fe2013-02-12 09:52:50 -070083
84 struct ntb_transport_qp *qp;
85 union {
86 struct ntb_payload_header __iomem *tx_hdr;
87 struct ntb_payload_header *rx_hdr;
88 };
89 unsigned int index;
Jon Masonfce8a7b2012-11-16 19:27:12 -070090};
91
Jon Mason793c20e2013-01-19 02:02:26 -070092struct ntb_rx_info {
93 unsigned int entry;
94};
95
Jon Masonfce8a7b2012-11-16 19:27:12 -070096struct ntb_transport_qp {
97 struct ntb_transport *transport;
98 struct ntb_device *ndev;
99 void *cb_data;
Jon Mason282a2fe2013-02-12 09:52:50 -0700100 struct dma_chan *dma_chan;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700101
102 bool client_ready;
103 bool qp_link;
104 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
105
Jon Mason74465642013-01-21 15:28:52 -0700106 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -0700107 struct ntb_rx_info *remote_rx_info;
108
Jon Mason53ca4fe2013-11-26 11:21:50 -0700109 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
110 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700111 struct list_head tx_free_q;
112 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700113 void __iomem *tx_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700114 dma_addr_t tx_mw_phys;
Jon Mason793c20e2013-01-19 02:02:26 -0700115 unsigned int tx_index;
116 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700117 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700118
Jon Mason53ca4fe2013-11-26 11:21:50 -0700119 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
120 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700121 struct list_head rx_pend_q;
122 struct list_head rx_free_q;
123 spinlock_t ntb_rx_pend_q_lock;
124 spinlock_t ntb_rx_free_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700125 void *rx_buff;
126 unsigned int rx_index;
127 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700128 unsigned int rx_max_frame;
Jon Mason282a2fe2013-02-12 09:52:50 -0700129 dma_cookie_t last_cookie;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700130
Jon Mason53ca4fe2013-11-26 11:21:50 -0700131 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700132 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700133 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700134
135 struct dentry *debugfs_dir;
136 struct dentry *debugfs_stats;
137
138 /* Stats */
139 u64 rx_bytes;
140 u64 rx_pkts;
141 u64 rx_ring_empty;
142 u64 rx_err_no_buf;
143 u64 rx_err_oflow;
144 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700145 u64 rx_memcpy;
146 u64 rx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700147 u64 tx_bytes;
148 u64 tx_pkts;
149 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700150 u64 tx_err_no_buf;
151 u64 tx_memcpy;
152 u64 tx_async;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700153};
154
155struct ntb_transport_mw {
156 size_t size;
157 void *virt_addr;
158 dma_addr_t dma_addr;
159};
160
161struct ntb_transport_client_dev {
162 struct list_head entry;
163 struct device dev;
164};
165
166struct ntb_transport {
167 struct list_head entry;
168 struct list_head client_devs;
169
170 struct ntb_device *ndev;
Jon Mason948d3a62013-04-18 17:07:36 -0700171 struct ntb_transport_mw *mw;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700172 struct ntb_transport_qp *qps;
173 unsigned int max_qps;
174 unsigned long qp_bitmap;
175 bool transport_link;
176 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700177 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700178};
179
180enum {
181 DESC_DONE_FLAG = 1 << 0,
182 LINK_DOWN_FLAG = 1 << 1,
183};
184
185struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700186 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700187 unsigned int len;
188 unsigned int flags;
189};
190
191enum {
192 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700193 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700194 NUM_QPS,
195 NUM_MWS,
196 MW0_SZ_HIGH,
197 MW0_SZ_LOW,
198 MW1_SZ_HIGH,
199 MW1_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700200 MAX_SPAD,
201};
202
Jon Mason948d3a62013-04-18 17:07:36 -0700203#define QP_TO_MW(ndev, qp) ((qp) % ntb_max_mw(ndev))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700204#define NTB_QP_DEF_NUM_ENTRIES 100
205#define NTB_LINK_DOWN_TIMEOUT 10
206
207static int ntb_match_bus(struct device *dev, struct device_driver *drv)
208{
209 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
210}
211
212static int ntb_client_probe(struct device *dev)
213{
214 const struct ntb_client *drv = container_of(dev->driver,
215 struct ntb_client, driver);
216 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
217 int rc = -EINVAL;
218
219 get_device(dev);
220 if (drv && drv->probe)
221 rc = drv->probe(pdev);
222 if (rc)
223 put_device(dev);
224
225 return rc;
226}
227
228static int ntb_client_remove(struct device *dev)
229{
230 const struct ntb_client *drv = container_of(dev->driver,
231 struct ntb_client, driver);
232 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
233
234 if (drv && drv->remove)
235 drv->remove(pdev);
236
237 put_device(dev);
238
239 return 0;
240}
241
Jon Mason170d35a2013-01-19 02:02:23 -0700242static struct bus_type ntb_bus_type = {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700243 .name = "ntb_bus",
244 .match = ntb_match_bus,
245 .probe = ntb_client_probe,
246 .remove = ntb_client_remove,
247};
248
249static LIST_HEAD(ntb_transport_list);
250
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800251static int ntb_bus_init(struct ntb_transport *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700252{
253 if (list_empty(&ntb_transport_list)) {
254 int rc = bus_register(&ntb_bus_type);
255 if (rc)
256 return rc;
257 }
258
259 list_add(&nt->entry, &ntb_transport_list);
260
261 return 0;
262}
263
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800264static void ntb_bus_remove(struct ntb_transport *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700265{
266 struct ntb_transport_client_dev *client_dev, *cd;
267
268 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
269 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
270 dev_name(&client_dev->dev));
271 list_del(&client_dev->entry);
272 device_unregister(&client_dev->dev);
273 }
274
275 list_del(&nt->entry);
276
277 if (list_empty(&ntb_transport_list))
278 bus_unregister(&ntb_bus_type);
279}
280
281static void ntb_client_release(struct device *dev)
282{
283 struct ntb_transport_client_dev *client_dev;
284 client_dev = container_of(dev, struct ntb_transport_client_dev, dev);
285
286 kfree(client_dev);
287}
288
289/**
290 * ntb_unregister_client_dev - Unregister NTB client device
291 * @device_name: Name of NTB client device
292 *
293 * Unregister an NTB client device with the NTB transport layer
294 */
295void ntb_unregister_client_dev(char *device_name)
296{
297 struct ntb_transport_client_dev *client, *cd;
298 struct ntb_transport *nt;
299
300 list_for_each_entry(nt, &ntb_transport_list, entry)
301 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
302 if (!strncmp(dev_name(&client->dev), device_name,
303 strlen(device_name))) {
304 list_del(&client->entry);
305 device_unregister(&client->dev);
306 }
307}
308EXPORT_SYMBOL_GPL(ntb_unregister_client_dev);
309
310/**
311 * ntb_register_client_dev - Register NTB client device
312 * @device_name: Name of NTB client device
313 *
314 * Register an NTB client device with the NTB transport layer
315 */
316int ntb_register_client_dev(char *device_name)
317{
318 struct ntb_transport_client_dev *client_dev;
319 struct ntb_transport *nt;
Jon Mason8b19d452013-04-26 14:51:57 -0700320 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700321
Jon Mason8222b402013-01-19 02:02:16 -0700322 if (list_empty(&ntb_transport_list))
323 return -ENODEV;
324
Jon Masonfce8a7b2012-11-16 19:27:12 -0700325 list_for_each_entry(nt, &ntb_transport_list, entry) {
326 struct device *dev;
327
328 client_dev = kzalloc(sizeof(struct ntb_transport_client_dev),
329 GFP_KERNEL);
330 if (!client_dev) {
331 rc = -ENOMEM;
332 goto err;
333 }
334
335 dev = &client_dev->dev;
336
337 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700338 dev_set_name(dev, "%s%d", device_name, i);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700339 dev->bus = &ntb_bus_type;
340 dev->release = ntb_client_release;
341 dev->parent = &ntb_query_pdev(nt->ndev)->dev;
342
343 rc = device_register(dev);
344 if (rc) {
345 kfree(client_dev);
346 goto err;
347 }
348
349 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700350 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700351 }
352
353 return 0;
354
355err:
356 ntb_unregister_client_dev(device_name);
357
358 return rc;
359}
360EXPORT_SYMBOL_GPL(ntb_register_client_dev);
361
362/**
363 * ntb_register_client - Register NTB client driver
364 * @drv: NTB client driver to be registered
365 *
366 * Register an NTB client driver with the NTB transport layer
367 *
368 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
369 */
370int ntb_register_client(struct ntb_client *drv)
371{
372 drv->driver.bus = &ntb_bus_type;
373
Jon Mason8222b402013-01-19 02:02:16 -0700374 if (list_empty(&ntb_transport_list))
375 return -ENODEV;
376
Jon Masonfce8a7b2012-11-16 19:27:12 -0700377 return driver_register(&drv->driver);
378}
379EXPORT_SYMBOL_GPL(ntb_register_client);
380
381/**
382 * ntb_unregister_client - Unregister NTB client driver
383 * @drv: NTB client driver to be unregistered
384 *
385 * Unregister an NTB client driver with the NTB transport layer
386 *
387 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
388 */
389void ntb_unregister_client(struct ntb_client *drv)
390{
391 driver_unregister(&drv->driver);
392}
393EXPORT_SYMBOL_GPL(ntb_unregister_client);
394
Jon Masonfce8a7b2012-11-16 19:27:12 -0700395static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
396 loff_t *offp)
397{
398 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700399 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700400 ssize_t ret, out_offset, out_count;
401
Jon Mason282a2fe2013-02-12 09:52:50 -0700402 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700403
404 buf = kmalloc(out_count, GFP_KERNEL);
405 if (!buf)
406 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700407
408 qp = filp->private_data;
409 out_offset = 0;
410 out_offset += snprintf(buf + out_offset, out_count - out_offset,
411 "NTB QP stats\n");
412 out_offset += snprintf(buf + out_offset, out_count - out_offset,
413 "rx_bytes - \t%llu\n", qp->rx_bytes);
414 out_offset += snprintf(buf + out_offset, out_count - out_offset,
415 "rx_pkts - \t%llu\n", qp->rx_pkts);
416 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700417 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
418 out_offset += snprintf(buf + out_offset, out_count - out_offset,
419 "rx_async - \t%llu\n", qp->rx_async);
420 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700421 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
422 out_offset += snprintf(buf + out_offset, out_count - out_offset,
423 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
424 out_offset += snprintf(buf + out_offset, out_count - out_offset,
425 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
426 out_offset += snprintf(buf + out_offset, out_count - out_offset,
427 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
428 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700429 "rx_buff - \t%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700430 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700431 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700432 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700433 "rx_max_entry - \t%u\n", qp->rx_max_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700434
435 out_offset += snprintf(buf + out_offset, out_count - out_offset,
436 "tx_bytes - \t%llu\n", qp->tx_bytes);
437 out_offset += snprintf(buf + out_offset, out_count - out_offset,
438 "tx_pkts - \t%llu\n", qp->tx_pkts);
439 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700440 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
441 out_offset += snprintf(buf + out_offset, out_count - out_offset,
442 "tx_async - \t%llu\n", qp->tx_async);
443 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700444 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
445 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700446 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
447 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700448 "tx_mw - \t%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700449 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700450 "tx_index - \t%u\n", qp->tx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700451 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700452 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700453
454 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masond7237e22013-01-19 02:02:25 -0700455 "\nQP Link %s\n", (qp->qp_link == NTB_LINK_UP) ?
Jon Masonfce8a7b2012-11-16 19:27:12 -0700456 "Up" : "Down");
Jon Masond7237e22013-01-19 02:02:25 -0700457 if (out_offset > out_count)
458 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700459
460 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700461 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700462 return ret;
463}
464
465static const struct file_operations ntb_qp_debugfs_stats = {
466 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700467 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700468 .read = debugfs_read,
469};
470
471static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
472 struct list_head *list)
473{
474 unsigned long flags;
475
476 spin_lock_irqsave(lock, flags);
477 list_add_tail(entry, list);
478 spin_unlock_irqrestore(lock, flags);
479}
480
481static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700482 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700483{
484 struct ntb_queue_entry *entry;
485 unsigned long flags;
486
487 spin_lock_irqsave(lock, flags);
488 if (list_empty(list)) {
489 entry = NULL;
490 goto out;
491 }
492 entry = list_first_entry(list, struct ntb_queue_entry, entry);
493 list_del(&entry->entry);
494out:
495 spin_unlock_irqrestore(lock, flags);
496
497 return entry;
498}
499
500static void ntb_transport_setup_qp_mw(struct ntb_transport *nt,
501 unsigned int qp_num)
502{
503 struct ntb_transport_qp *qp = &nt->qps[qp_num];
Jon Masonef114ed2013-01-19 02:02:18 -0700504 unsigned int rx_size, num_qps_mw;
Jon Mason948d3a62013-04-18 17:07:36 -0700505 u8 mw_num, mw_max;
Jon Mason793c20e2013-01-19 02:02:26 -0700506 unsigned int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700507
Jon Mason948d3a62013-04-18 17:07:36 -0700508 mw_max = ntb_max_mw(nt->ndev);
509 mw_num = QP_TO_MW(nt->ndev, qp_num);
510
Jon Mason74465642013-01-21 15:28:52 -0700511 WARN_ON(nt->mw[mw_num].virt_addr == NULL);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700512
Jon Masona1413cf2014-06-19 10:11:13 -0700513 if (nt->max_qps % mw_max && mw_num + 1 < nt->max_qps / mw_max)
Jon Mason948d3a62013-04-18 17:07:36 -0700514 num_qps_mw = nt->max_qps / mw_max + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700515 else
Jon Mason948d3a62013-04-18 17:07:36 -0700516 num_qps_mw = nt->max_qps / mw_max;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700517
Jon Mason793c20e2013-01-19 02:02:26 -0700518 rx_size = (unsigned int) nt->mw[mw_num].size / num_qps_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700519 qp->rx_buff = nt->mw[mw_num].virt_addr + qp_num / mw_max * rx_size;
Jon Mason793c20e2013-01-19 02:02:26 -0700520 rx_size -= sizeof(struct ntb_rx_info);
521
Jon Mason282a2fe2013-02-12 09:52:50 -0700522 qp->remote_rx_info = qp->rx_buff + rx_size;
523
Jon Masonc9d534c2013-02-01 15:45:16 -0700524 /* Due to housekeeping, there must be atleast 2 buffs */
525 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700526 qp->rx_max_entry = rx_size / qp->rx_max_frame;
527 qp->rx_index = 0;
528
Jon Masonc9d534c2013-02-01 15:45:16 -0700529 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700530
Jon Masonef114ed2013-01-19 02:02:18 -0700531 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700532 for (i = 0; i < qp->rx_max_entry; i++) {
533 void *offset = qp->rx_buff + qp->rx_max_frame * (i + 1) -
534 sizeof(struct ntb_payload_header);
Jon Masonef114ed2013-01-19 02:02:18 -0700535 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700536 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700537
538 qp->rx_pkts = 0;
539 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700540 qp->tx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700541}
542
Jon Mason113fc502013-01-30 11:40:52 -0700543static void ntb_free_mw(struct ntb_transport *nt, int num_mw)
544{
545 struct ntb_transport_mw *mw = &nt->mw[num_mw];
546 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
547
548 if (!mw->virt_addr)
549 return;
550
551 dma_free_coherent(&pdev->dev, mw->size, mw->virt_addr, mw->dma_addr);
552 mw->virt_addr = NULL;
553}
554
Jon Masonb77b2632013-02-01 15:25:37 -0700555static int ntb_set_mw(struct ntb_transport *nt, int num_mw, unsigned int size)
556{
557 struct ntb_transport_mw *mw = &nt->mw[num_mw];
558 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
559
560 /* No need to re-setup */
561 if (mw->size == ALIGN(size, 4096))
562 return 0;
563
564 if (mw->size != 0)
565 ntb_free_mw(nt, num_mw);
566
567 /* Alloc memory for receiving data. Must be 4k aligned */
568 mw->size = ALIGN(size, 4096);
569
570 mw->virt_addr = dma_alloc_coherent(&pdev->dev, mw->size, &mw->dma_addr,
571 GFP_KERNEL);
572 if (!mw->virt_addr) {
573 mw->size = 0;
574 dev_err(&pdev->dev, "Unable to allocate MW buffer of size %d\n",
575 (int) mw->size);
576 return -ENOMEM;
577 }
578
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700579 /*
580 * we must ensure that the memory address allocated is BAR size
581 * aligned in order for the XLAT register to take the value. This
582 * is a requirement of the hardware. It is recommended to setup CMA
583 * for BAR sizes equal or greater than 4MB.
584 */
585 if (!IS_ALIGNED(mw->dma_addr, mw->size)) {
586 dev_err(&pdev->dev, "DMA memory %pad not aligned to BAR size\n",
587 &mw->dma_addr);
588 ntb_free_mw(nt, num_mw);
589 return -ENOMEM;
590 }
591
Jon Masonb77b2632013-02-01 15:25:37 -0700592 /* Notify HW the memory location of the receive buffer */
593 ntb_set_mw_addr(nt->ndev, num_mw, mw->dma_addr);
594
595 return 0;
596}
597
Jon Masonfca4d512013-09-09 13:39:55 -0700598static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700599{
600 struct ntb_transport *nt = qp->transport;
601 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
602
603 if (qp->qp_link == NTB_LINK_DOWN) {
604 cancel_delayed_work_sync(&qp->link_work);
605 return;
606 }
607
608 if (qp->event_handler)
609 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
610
611 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
612 qp->qp_link = NTB_LINK_DOWN;
Jon Masonfca4d512013-09-09 13:39:55 -0700613}
614
615static void ntb_qp_link_cleanup_work(struct work_struct *work)
616{
617 struct ntb_transport_qp *qp = container_of(work,
618 struct ntb_transport_qp,
619 link_cleanup);
620 struct ntb_transport *nt = qp->transport;
621
622 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700623
624 if (nt->transport_link == NTB_LINK_UP)
625 schedule_delayed_work(&qp->link_work,
626 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
627}
628
Jon Mason7b4f2d32013-01-19 02:02:19 -0700629static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700630{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700631 schedule_work(&qp->link_cleanup);
632}
633
Jon Masonfca4d512013-09-09 13:39:55 -0700634static void ntb_transport_link_cleanup(struct ntb_transport *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700635{
Jon Masonfce8a7b2012-11-16 19:27:12 -0700636 int i;
637
Jon Masonfca4d512013-09-09 13:39:55 -0700638 /* Pass along the info to any clients */
639 for (i = 0; i < nt->max_qps; i++)
640 if (!test_bit(i, &nt->qp_bitmap))
641 ntb_qp_link_cleanup(&nt->qps[i]);
642
Jon Masonfce8a7b2012-11-16 19:27:12 -0700643 if (nt->transport_link == NTB_LINK_DOWN)
644 cancel_delayed_work_sync(&nt->link_work);
645 else
646 nt->transport_link = NTB_LINK_DOWN;
647
Jon Masonfce8a7b2012-11-16 19:27:12 -0700648 /* The scratchpad registers keep the values if the remote side
649 * goes down, blast them now to give them a sane value the next
650 * time they are accessed
651 */
652 for (i = 0; i < MAX_SPAD; i++)
653 ntb_write_local_spad(nt->ndev, i, 0);
654}
655
Jon Masonfca4d512013-09-09 13:39:55 -0700656static void ntb_transport_link_cleanup_work(struct work_struct *work)
657{
658 struct ntb_transport *nt = container_of(work, struct ntb_transport,
659 link_cleanup);
660
661 ntb_transport_link_cleanup(nt);
662}
663
Jon Masonfce8a7b2012-11-16 19:27:12 -0700664static void ntb_transport_event_callback(void *data, enum ntb_hw_event event)
665{
666 struct ntb_transport *nt = data;
667
668 switch (event) {
669 case NTB_EVENT_HW_LINK_UP:
670 schedule_delayed_work(&nt->link_work, 0);
671 break;
672 case NTB_EVENT_HW_LINK_DOWN:
Jon Mason7b4f2d32013-01-19 02:02:19 -0700673 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700674 break;
675 default:
676 BUG();
677 }
678}
679
680static void ntb_transport_link_work(struct work_struct *work)
681{
682 struct ntb_transport *nt = container_of(work, struct ntb_transport,
683 link_work.work);
684 struct ntb_device *ndev = nt->ndev;
685 struct pci_dev *pdev = ntb_query_pdev(ndev);
686 u32 val;
687 int rc, i;
688
Jon Mason113fc502013-01-30 11:40:52 -0700689 /* send the local info, in the opposite order of the way we read it */
Jon Mason948d3a62013-04-18 17:07:36 -0700690 for (i = 0; i < ntb_max_mw(ndev); i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700691 rc = ntb_write_remote_spad(ndev, MW0_SZ_HIGH + (i * 2),
692 ntb_get_mw_size(ndev, i) >> 32);
693 if (rc) {
694 dev_err(&pdev->dev, "Error writing %u to remote spad %d\n",
695 (u32)(ntb_get_mw_size(ndev, i) >> 32),
696 MW0_SZ_HIGH + (i * 2));
697 goto out;
698 }
699
700 rc = ntb_write_remote_spad(ndev, MW0_SZ_LOW + (i * 2),
701 (u32) ntb_get_mw_size(ndev, i));
702 if (rc) {
703 dev_err(&pdev->dev, "Error writing %u to remote spad %d\n",
704 (u32) ntb_get_mw_size(ndev, i),
705 MW0_SZ_LOW + (i * 2));
706 goto out;
707 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700708 }
709
Jon Mason948d3a62013-04-18 17:07:36 -0700710 rc = ntb_write_remote_spad(ndev, NUM_MWS, ntb_max_mw(ndev));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700711 if (rc) {
712 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
Jon Mason948d3a62013-04-18 17:07:36 -0700713 ntb_max_mw(ndev), NUM_MWS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700714 goto out;
715 }
716
717 rc = ntb_write_remote_spad(ndev, NUM_QPS, nt->max_qps);
718 if (rc) {
719 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
720 nt->max_qps, NUM_QPS);
721 goto out;
722 }
723
Jon Mason113fc502013-01-30 11:40:52 -0700724 rc = ntb_write_remote_spad(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700725 if (rc) {
726 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
Jon Mason113fc502013-01-30 11:40:52 -0700727 NTB_TRANSPORT_VERSION, VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700728 goto out;
729 }
730
731 /* Query the remote side for its info */
732 rc = ntb_read_remote_spad(ndev, VERSION, &val);
733 if (rc) {
734 dev_err(&pdev->dev, "Error reading remote spad %d\n", VERSION);
735 goto out;
736 }
737
738 if (val != NTB_TRANSPORT_VERSION)
739 goto out;
740 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
741
742 rc = ntb_read_remote_spad(ndev, NUM_QPS, &val);
743 if (rc) {
744 dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_QPS);
745 goto out;
746 }
747
748 if (val != nt->max_qps)
749 goto out;
750 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
751
Jon Mason113fc502013-01-30 11:40:52 -0700752 rc = ntb_read_remote_spad(ndev, NUM_MWS, &val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700753 if (rc) {
Jon Mason113fc502013-01-30 11:40:52 -0700754 dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_MWS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700755 goto out;
756 }
757
Jon Mason948d3a62013-04-18 17:07:36 -0700758 if (val != ntb_max_mw(ndev))
Jon Masonfce8a7b2012-11-16 19:27:12 -0700759 goto out;
Jon Mason113fc502013-01-30 11:40:52 -0700760 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700761
Jon Mason948d3a62013-04-18 17:07:36 -0700762 for (i = 0; i < ntb_max_mw(ndev); i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700763 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700764
Jon Mason113fc502013-01-30 11:40:52 -0700765 rc = ntb_read_remote_spad(ndev, MW0_SZ_HIGH + (i * 2), &val);
766 if (rc) {
767 dev_err(&pdev->dev, "Error reading remote spad %d\n",
768 MW0_SZ_HIGH + (i * 2));
769 goto out1;
770 }
771
772 val64 = (u64) val << 32;
773
774 rc = ntb_read_remote_spad(ndev, MW0_SZ_LOW + (i * 2), &val);
775 if (rc) {
776 dev_err(&pdev->dev, "Error reading remote spad %d\n",
777 MW0_SZ_LOW + (i * 2));
778 goto out1;
779 }
780
781 val64 |= val;
782
783 dev_dbg(&pdev->dev, "Remote MW%d size = %llu\n", i, val64);
784
785 rc = ntb_set_mw(nt, i, val64);
786 if (rc)
787 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700788 }
789
Jon Masonfce8a7b2012-11-16 19:27:12 -0700790 nt->transport_link = NTB_LINK_UP;
791
792 for (i = 0; i < nt->max_qps; i++) {
793 struct ntb_transport_qp *qp = &nt->qps[i];
794
795 ntb_transport_setup_qp_mw(nt, i);
796
797 if (qp->client_ready == NTB_LINK_UP)
798 schedule_delayed_work(&qp->link_work, 0);
799 }
800
801 return;
802
Jon Mason113fc502013-01-30 11:40:52 -0700803out1:
Jon Mason948d3a62013-04-18 17:07:36 -0700804 for (i = 0; i < ntb_max_mw(ndev); i++)
Jon Mason113fc502013-01-30 11:40:52 -0700805 ntb_free_mw(nt, i);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700806out:
807 if (ntb_hw_link_status(ndev))
808 schedule_delayed_work(&nt->link_work,
809 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
810}
811
812static void ntb_qp_link_work(struct work_struct *work)
813{
814 struct ntb_transport_qp *qp = container_of(work,
815 struct ntb_transport_qp,
816 link_work.work);
817 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
818 struct ntb_transport *nt = qp->transport;
819 int rc, val;
820
821 WARN_ON(nt->transport_link != NTB_LINK_UP);
822
823 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
824 if (rc) {
825 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
826 return;
827 }
828
829 rc = ntb_write_remote_spad(nt->ndev, QP_LINKS, val | 1 << qp->qp_num);
830 if (rc)
831 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
832 val | 1 << qp->qp_num, QP_LINKS);
833
834 /* query remote spad for qp ready bits */
835 rc = ntb_read_remote_spad(nt->ndev, QP_LINKS, &val);
836 if (rc)
837 dev_err(&pdev->dev, "Error reading remote spad %d\n", QP_LINKS);
838
839 dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val);
840
841 /* See if the remote side is up */
842 if (1 << qp->qp_num & val) {
843 qp->qp_link = NTB_LINK_UP;
844
845 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
846 if (qp->event_handler)
847 qp->event_handler(qp->cb_data, NTB_LINK_UP);
848 } else if (nt->transport_link == NTB_LINK_UP)
849 schedule_delayed_work(&qp->link_work,
850 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
851}
852
Jon Mason282a2fe2013-02-12 09:52:50 -0700853static int ntb_transport_init_queue(struct ntb_transport *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700854 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700855{
856 struct ntb_transport_qp *qp;
Jon Masonef114ed2013-01-19 02:02:18 -0700857 unsigned int num_qps_mw, tx_size;
Jon Mason948d3a62013-04-18 17:07:36 -0700858 u8 mw_num, mw_max;
Jon Mason282a2fe2013-02-12 09:52:50 -0700859 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700860
861 mw_max = ntb_max_mw(nt->ndev);
862 mw_num = QP_TO_MW(nt->ndev, qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700863
864 qp = &nt->qps[qp_num];
865 qp->qp_num = qp_num;
866 qp->transport = nt;
867 qp->ndev = nt->ndev;
868 qp->qp_link = NTB_LINK_DOWN;
869 qp->client_ready = NTB_LINK_DOWN;
870 qp->event_handler = NULL;
871
Jon Masona1413cf2014-06-19 10:11:13 -0700872 if (nt->max_qps % mw_max && mw_num + 1 < nt->max_qps / mw_max)
Jon Mason948d3a62013-04-18 17:07:36 -0700873 num_qps_mw = nt->max_qps / mw_max + 1;
Jon Masonef114ed2013-01-19 02:02:18 -0700874 else
Jon Mason948d3a62013-04-18 17:07:36 -0700875 num_qps_mw = nt->max_qps / mw_max;
Jon Masonef114ed2013-01-19 02:02:18 -0700876
Jon Mason793c20e2013-01-19 02:02:26 -0700877 tx_size = (unsigned int) ntb_get_mw_size(qp->ndev, mw_num) / num_qps_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700878 qp_offset = qp_num / mw_max * tx_size;
879 qp->tx_mw = ntb_get_mw_vbase(nt->ndev, mw_num) + qp_offset;
880 if (!qp->tx_mw)
881 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -0700882
Jon Mason282a2fe2013-02-12 09:52:50 -0700883 qp->tx_mw_phys = ntb_get_mw_base(qp->ndev, mw_num) + qp_offset;
884 if (!qp->tx_mw_phys)
885 return -EINVAL;
886
887 tx_size -= sizeof(struct ntb_rx_info);
888 qp->rx_info = qp->tx_mw + tx_size;
889
Jon Masonc9d534c2013-02-01 15:45:16 -0700890 /* Due to housekeeping, there must be atleast 2 buffs */
891 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700892 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -0700893
Jon Mason1517a3f2013-07-30 15:58:49 -0700894 if (ntb_query_debugfs(nt->ndev)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700895 char debugfs_name[4];
896
897 snprintf(debugfs_name, 4, "qp%d", qp_num);
898 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Jon Mason1517a3f2013-07-30 15:58:49 -0700899 ntb_query_debugfs(nt->ndev));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700900
901 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
902 qp->debugfs_dir, qp,
903 &ntb_qp_debugfs_stats);
904 }
905
906 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -0700907 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700908
909 spin_lock_init(&qp->ntb_rx_pend_q_lock);
910 spin_lock_init(&qp->ntb_rx_free_q_lock);
911 spin_lock_init(&qp->ntb_tx_free_q_lock);
912
913 INIT_LIST_HEAD(&qp->rx_pend_q);
914 INIT_LIST_HEAD(&qp->rx_free_q);
915 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -0700916
917 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700918}
919
920int ntb_transport_init(struct pci_dev *pdev)
921{
922 struct ntb_transport *nt;
923 int rc, i;
924
925 nt = kzalloc(sizeof(struct ntb_transport), GFP_KERNEL);
926 if (!nt)
927 return -ENOMEM;
928
Jon Masonfce8a7b2012-11-16 19:27:12 -0700929 nt->ndev = ntb_register_transport(pdev, nt);
930 if (!nt->ndev) {
931 rc = -EIO;
932 goto err;
933 }
934
Jon Mason948d3a62013-04-18 17:07:36 -0700935 nt->mw = kcalloc(ntb_max_mw(nt->ndev), sizeof(struct ntb_transport_mw),
936 GFP_KERNEL);
937 if (!nt->mw) {
938 rc = -ENOMEM;
939 goto err1;
940 }
941
942 if (max_num_clients)
943 nt->max_qps = min(ntb_max_cbs(nt->ndev), max_num_clients);
944 else
945 nt->max_qps = min(ntb_max_cbs(nt->ndev), ntb_max_mw(nt->ndev));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700946
947 nt->qps = kcalloc(nt->max_qps, sizeof(struct ntb_transport_qp),
948 GFP_KERNEL);
949 if (!nt->qps) {
950 rc = -ENOMEM;
Jon Mason948d3a62013-04-18 17:07:36 -0700951 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700952 }
953
954 nt->qp_bitmap = ((u64) 1 << nt->max_qps) - 1;
955
Jon Mason282a2fe2013-02-12 09:52:50 -0700956 for (i = 0; i < nt->max_qps; i++) {
957 rc = ntb_transport_init_queue(nt, i);
958 if (rc)
959 goto err3;
960 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700961
962 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -0700963 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700964
965 rc = ntb_register_event_callback(nt->ndev,
966 ntb_transport_event_callback);
967 if (rc)
Jon Mason948d3a62013-04-18 17:07:36 -0700968 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700969
970 INIT_LIST_HEAD(&nt->client_devs);
971 rc = ntb_bus_init(nt);
972 if (rc)
Jon Mason948d3a62013-04-18 17:07:36 -0700973 goto err4;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700974
975 if (ntb_hw_link_status(nt->ndev))
976 schedule_delayed_work(&nt->link_work, 0);
977
978 return 0;
979
Jon Mason948d3a62013-04-18 17:07:36 -0700980err4:
Jon Masonfce8a7b2012-11-16 19:27:12 -0700981 ntb_unregister_event_callback(nt->ndev);
Jon Mason948d3a62013-04-18 17:07:36 -0700982err3:
Jon Masonfce8a7b2012-11-16 19:27:12 -0700983 kfree(nt->qps);
Jon Mason948d3a62013-04-18 17:07:36 -0700984err2:
985 kfree(nt->mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700986err1:
987 ntb_unregister_transport(nt->ndev);
988err:
Jon Masonfce8a7b2012-11-16 19:27:12 -0700989 kfree(nt);
990 return rc;
991}
992
993void ntb_transport_free(void *transport)
994{
995 struct ntb_transport *nt = transport;
Jon Mason948d3a62013-04-18 17:07:36 -0700996 struct ntb_device *ndev = nt->ndev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700997 int i;
998
Jon Masonfca4d512013-09-09 13:39:55 -0700999 ntb_transport_link_cleanup(nt);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001000
1001 /* verify that all the qp's are freed */
Jon Mason1517a3f2013-07-30 15:58:49 -07001002 for (i = 0; i < nt->max_qps; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001003 if (!test_bit(i, &nt->qp_bitmap))
1004 ntb_transport_free_queue(&nt->qps[i]);
Jon Mason1517a3f2013-07-30 15:58:49 -07001005 debugfs_remove_recursive(nt->qps[i].debugfs_dir);
1006 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001007
1008 ntb_bus_remove(nt);
1009
1010 cancel_delayed_work_sync(&nt->link_work);
1011
Jon Mason948d3a62013-04-18 17:07:36 -07001012 ntb_unregister_event_callback(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001013
Jon Mason948d3a62013-04-18 17:07:36 -07001014 for (i = 0; i < ntb_max_mw(ndev); i++)
Jon Mason113fc502013-01-30 11:40:52 -07001015 ntb_free_mw(nt, i);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001016
1017 kfree(nt->qps);
Jon Mason948d3a62013-04-18 17:07:36 -07001018 kfree(nt->mw);
1019 ntb_unregister_transport(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001020 kfree(nt);
1021}
1022
Jon Mason282a2fe2013-02-12 09:52:50 -07001023static void ntb_rx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001024{
Jon Mason282a2fe2013-02-12 09:52:50 -07001025 struct ntb_queue_entry *entry = data;
1026 struct ntb_transport_qp *qp = entry->qp;
Jon Mason448c6fb2013-01-19 02:02:27 -07001027 void *cb_data = entry->cb_data;
1028 unsigned int len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001029 struct ntb_payload_header *hdr = entry->rx_hdr;
Jon Mason448c6fb2013-01-19 02:02:27 -07001030
Jon Mason282a2fe2013-02-12 09:52:50 -07001031 /* Ensure that the data is fully copied out before clearing the flag */
1032 wmb();
1033 hdr->flags = 0;
1034
1035 iowrite32(entry->index, &qp->rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001036
Jon Masonfce8a7b2012-11-16 19:27:12 -07001037 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
Jon Mason448c6fb2013-01-19 02:02:27 -07001038
1039 if (qp->rx_handler && qp->client_ready == NTB_LINK_UP)
1040 qp->rx_handler(qp, qp->cb_data, cb_data, len);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001041}
1042
Jon Mason282a2fe2013-02-12 09:52:50 -07001043static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1044{
1045 void *buf = entry->buf;
1046 size_t len = entry->len;
1047
1048 memcpy(buf, offset, len);
1049
1050 ntb_rx_copy_callback(entry);
1051}
1052
1053static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset,
1054 size_t len)
1055{
1056 struct dma_async_tx_descriptor *txd;
1057 struct ntb_transport_qp *qp = entry->qp;
1058 struct dma_chan *chan = qp->dma_chan;
1059 struct dma_device *device;
1060 size_t pay_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001061 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001062 dma_cookie_t cookie;
1063 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001064
1065 entry->len = len;
1066
1067 if (!chan)
1068 goto err;
1069
Jon Mason53ca4fe2013-11-26 11:21:50 -07001070 if (len < copy_bytes)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001071 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001072
1073 device = chan->device;
1074 pay_off = (size_t) offset & ~PAGE_MASK;
1075 buff_off = (size_t) buf & ~PAGE_MASK;
1076
1077 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001078 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001079
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001080 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1081 if (!unmap)
1082 goto err_wait;
Jon Mason282a2fe2013-02-12 09:52:50 -07001083
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001084 unmap->len = len;
1085 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1086 pay_off, len, DMA_TO_DEVICE);
1087 if (dma_mapping_error(device->dev, unmap->addr[0]))
1088 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001089
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001090 unmap->to_cnt = 1;
1091
1092 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1093 buff_off, len, DMA_FROM_DEVICE);
1094 if (dma_mapping_error(device->dev, unmap->addr[1]))
1095 goto err_get_unmap;
1096
1097 unmap->from_cnt = 1;
1098
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001099 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001100 unmap->addr[0], len,
1101 DMA_PREP_INTERRUPT);
Jon Mason282a2fe2013-02-12 09:52:50 -07001102 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001103 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001104
1105 txd->callback = ntb_rx_copy_callback;
1106 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001107 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001108
1109 cookie = dmaengine_submit(txd);
1110 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001111 goto err_set_unmap;
1112
1113 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001114
1115 qp->last_cookie = cookie;
1116
1117 qp->rx_async++;
1118
1119 return;
1120
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001121err_set_unmap:
1122 dmaengine_unmap_put(unmap);
1123err_get_unmap:
1124 dmaengine_unmap_put(unmap);
1125err_wait:
Jon Mason282a2fe2013-02-12 09:52:50 -07001126 /* If the callbacks come out of order, the writing of the index to the
1127 * last completed will be out of order. This may result in the
1128 * receive stalling forever.
1129 */
1130 dma_sync_wait(chan, qp->last_cookie);
1131err:
1132 ntb_memcpy_rx(entry, offset);
1133 qp->rx_memcpy++;
1134}
1135
Jon Masonfce8a7b2012-11-16 19:27:12 -07001136static int ntb_process_rxc(struct ntb_transport_qp *qp)
1137{
1138 struct ntb_payload_header *hdr;
1139 struct ntb_queue_entry *entry;
1140 void *offset;
1141
Jon Mason793c20e2013-01-19 02:02:26 -07001142 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1143 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1144
Jon Masonfce8a7b2012-11-16 19:27:12 -07001145 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1146 if (!entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001147 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
Jon Mason74465642013-01-21 15:28:52 -07001148 "no buffer - HDR ver %u, len %d, flags %x\n",
Jon Masonfce8a7b2012-11-16 19:27:12 -07001149 hdr->ver, hdr->len, hdr->flags);
1150 qp->rx_err_no_buf++;
1151 return -ENOMEM;
1152 }
1153
Jon Masonfce8a7b2012-11-16 19:27:12 -07001154 if (!(hdr->flags & DESC_DONE_FLAG)) {
1155 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001156 &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001157 qp->rx_ring_empty++;
1158 return -EAGAIN;
1159 }
1160
Jon Mason74465642013-01-21 15:28:52 -07001161 if (hdr->ver != (u32) qp->rx_pkts) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001162 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
Jon Mason74465642013-01-21 15:28:52 -07001163 "qp %d: version mismatch, expected %llu - got %u\n",
Jon Masonfce8a7b2012-11-16 19:27:12 -07001164 qp->qp_num, qp->rx_pkts, hdr->ver);
1165 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001166 &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001167 qp->rx_err_ver++;
1168 return -EIO;
1169 }
1170
1171 if (hdr->flags & LINK_DOWN_FLAG) {
1172 ntb_qp_link_down(qp);
1173
Jon Mason282a2fe2013-02-12 09:52:50 -07001174 goto err;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001175 }
1176
1177 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
Jon Mason74465642013-01-21 15:28:52 -07001178 "rx offset %u, ver %u - %d payload received, buf size %d\n",
Jon Mason793c20e2013-01-19 02:02:26 -07001179 qp->rx_index, hdr->ver, hdr->len, entry->len);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001180
Jon Mason282a2fe2013-02-12 09:52:50 -07001181 qp->rx_bytes += hdr->len;
1182 qp->rx_pkts++;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001183
Jon Mason282a2fe2013-02-12 09:52:50 -07001184 if (hdr->len > entry->len) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001185 qp->rx_err_oflow++;
1186 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
1187 "RX overflow! Wanted %d got %d\n",
1188 hdr->len, entry->len);
Jon Mason282a2fe2013-02-12 09:52:50 -07001189
1190 goto err;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001191 }
1192
Jon Mason282a2fe2013-02-12 09:52:50 -07001193 entry->index = qp->rx_index;
1194 entry->rx_hdr = hdr;
1195
1196 ntb_async_rx(entry, offset, hdr->len);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001197
1198out:
Jon Mason282a2fe2013-02-12 09:52:50 -07001199 qp->rx_index++;
1200 qp->rx_index %= qp->rx_max_entry;
1201
1202 return 0;
1203
1204err:
Jon Mason53ca4fe2013-11-26 11:21:50 -07001205 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, &qp->rx_pend_q);
Jon Mason793c20e2013-01-19 02:02:26 -07001206 /* Ensure that the data is fully copied out before clearing the flag */
1207 wmb();
1208 hdr->flags = 0;
Jon Mason74465642013-01-21 15:28:52 -07001209 iowrite32(qp->rx_index, &qp->rx_info->entry);
Jon Mason793c20e2013-01-19 02:02:26 -07001210
Jon Mason282a2fe2013-02-12 09:52:50 -07001211 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001212}
1213
Jon Masone8aeb602013-04-18 17:59:44 -07001214static int ntb_transport_rxc_db(void *data, int db_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001215{
Jon Masone8aeb602013-04-18 17:59:44 -07001216 struct ntb_transport_qp *qp = data;
Jon Masonc336acd2013-01-17 15:28:45 -07001217 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001218
Jon Masone8aeb602013-04-18 17:59:44 -07001219 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%s: doorbell %d received\n",
1220 __func__, db_num);
1221
Jon Masonc336acd2013-01-17 15:28:45 -07001222 /* Limit the number of packets processed in a single interrupt to
1223 * provide fairness to others
1224 */
1225 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001226 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001227 if (rc)
1228 break;
1229 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001230
1231 if (qp->dma_chan)
1232 dma_async_issue_pending(qp->dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001233
Jon Masone8aeb602013-04-18 17:59:44 -07001234 return i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001235}
1236
Jon Mason282a2fe2013-02-12 09:52:50 -07001237static void ntb_tx_copy_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001238{
Jon Mason282a2fe2013-02-12 09:52:50 -07001239 struct ntb_queue_entry *entry = data;
1240 struct ntb_transport_qp *qp = entry->qp;
1241 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001242
Jon Mason282a2fe2013-02-12 09:52:50 -07001243 /* Ensure that the data is fully copied out before setting the flags */
Jon Mason842c1dd2013-01-19 02:02:17 -07001244 wmb();
Jon Mason74465642013-01-21 15:28:52 -07001245 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001246
Jon Mason49793882013-07-15 15:53:54 -07001247 ntb_ring_doorbell(qp->ndev, qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001248
1249 /* The entry length can only be zero if the packet is intended to be a
1250 * "link down" or similar. Since no payload is being sent in these
1251 * cases, there is nothing to add to the completion queue.
1252 */
1253 if (entry->len > 0) {
1254 qp->tx_bytes += entry->len;
1255
1256 if (qp->tx_handler)
1257 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1258 entry->len);
1259 }
1260
1261 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1262}
1263
Jon Mason282a2fe2013-02-12 09:52:50 -07001264static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1265{
1266 memcpy_toio(offset, entry->buf, entry->len);
1267
1268 ntb_tx_copy_callback(entry);
1269}
1270
1271static void ntb_async_tx(struct ntb_transport_qp *qp,
1272 struct ntb_queue_entry *entry)
1273{
1274 struct ntb_payload_header __iomem *hdr;
1275 struct dma_async_tx_descriptor *txd;
1276 struct dma_chan *chan = qp->dma_chan;
1277 struct dma_device *device;
1278 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001279 struct dmaengine_unmap_data *unmap;
1280 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001281 dma_cookie_t cookie;
1282 void __iomem *offset;
1283 size_t len = entry->len;
1284 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001285
1286 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
1287 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1288 entry->tx_hdr = hdr;
1289
1290 iowrite32(entry->len, &hdr->len);
1291 iowrite32((u32) qp->tx_pkts, &hdr->ver);
1292
1293 if (!chan)
1294 goto err;
1295
1296 if (len < copy_bytes)
1297 goto err;
1298
1299 device = chan->device;
1300 dest = qp->tx_mw_phys + qp->tx_max_frame * qp->tx_index;
1301 buff_off = (size_t) buf & ~PAGE_MASK;
1302 dest_off = (size_t) dest & ~PAGE_MASK;
1303
1304 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1305 goto err;
1306
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001307 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1308 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001309 goto err;
1310
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001311 unmap->len = len;
1312 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1313 buff_off, len, DMA_TO_DEVICE);
1314 if (dma_mapping_error(device->dev, unmap->addr[0]))
1315 goto err_get_unmap;
1316
1317 unmap->to_cnt = 1;
1318
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001319 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
Bartlomiej Zolnierkiewicz0776ae72013-10-18 19:35:33 +02001320 DMA_PREP_INTERRUPT);
Jon Mason282a2fe2013-02-12 09:52:50 -07001321 if (!txd)
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001322 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001323
1324 txd->callback = ntb_tx_copy_callback;
1325 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001326 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001327
1328 cookie = dmaengine_submit(txd);
1329 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001330 goto err_set_unmap;
1331
1332 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001333
1334 dma_async_issue_pending(chan);
1335 qp->tx_async++;
1336
1337 return;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001338err_set_unmap:
1339 dmaengine_unmap_put(unmap);
1340err_get_unmap:
1341 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001342err:
1343 ntb_memcpy_tx(entry, offset);
1344 qp->tx_memcpy++;
1345}
1346
Jon Masonfce8a7b2012-11-16 19:27:12 -07001347static int ntb_process_tx(struct ntb_transport_qp *qp,
1348 struct ntb_queue_entry *entry)
1349{
Jon Mason282a2fe2013-02-12 09:52:50 -07001350 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%lld - tx %u, entry len %d flags %x buff %p\n",
1351 qp->tx_pkts, qp->tx_index, entry->len, entry->flags,
Jon Masonf7667552013-01-19 02:02:24 -07001352 entry->buf);
Jon Mason793c20e2013-01-19 02:02:26 -07001353 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001354 qp->tx_ring_full++;
1355 return -EAGAIN;
1356 }
1357
Jon Masonef114ed2013-01-19 02:02:18 -07001358 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001359 if (qp->tx_handler)
1360 qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1361
1362 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1363 &qp->tx_free_q);
1364 return 0;
1365 }
1366
Jon Mason282a2fe2013-02-12 09:52:50 -07001367 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001368
Jon Mason793c20e2013-01-19 02:02:26 -07001369 qp->tx_index++;
1370 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001371
1372 qp->tx_pkts++;
1373
1374 return 0;
1375}
1376
1377static void ntb_send_link_down(struct ntb_transport_qp *qp)
1378{
1379 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1380 struct ntb_queue_entry *entry;
1381 int i, rc;
1382
1383 if (qp->qp_link == NTB_LINK_DOWN)
1384 return;
1385
1386 qp->qp_link = NTB_LINK_DOWN;
1387 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
1388
1389 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001390 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001391 if (entry)
1392 break;
1393 msleep(100);
1394 }
1395
1396 if (!entry)
1397 return;
1398
1399 entry->cb_data = NULL;
1400 entry->buf = NULL;
1401 entry->len = 0;
1402 entry->flags = LINK_DOWN_FLAG;
1403
1404 rc = ntb_process_tx(qp, entry);
1405 if (rc)
1406 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1407 qp->qp_num);
1408}
1409
1410/**
1411 * ntb_transport_create_queue - Create a new NTB transport layer queue
1412 * @rx_handler: receive callback function
1413 * @tx_handler: transmit callback function
1414 * @event_handler: event callback function
1415 *
1416 * Create a new NTB transport layer queue and provide the queue with a callback
1417 * routine for both transmit and receive. The receive callback routine will be
1418 * used to pass up data when the transport has received it on the queue. The
1419 * transmit callback routine will be called when the transport has completed the
1420 * transmission of the data on the queue and the data is ready to be freed.
1421 *
1422 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1423 */
1424struct ntb_transport_qp *
1425ntb_transport_create_queue(void *data, struct pci_dev *pdev,
1426 const struct ntb_queue_handlers *handlers)
1427{
1428 struct ntb_queue_entry *entry;
1429 struct ntb_transport_qp *qp;
1430 struct ntb_transport *nt;
1431 unsigned int free_queue;
1432 int rc, i;
1433
1434 nt = ntb_find_transport(pdev);
1435 if (!nt)
1436 goto err;
1437
1438 free_queue = ffs(nt->qp_bitmap);
1439 if (!free_queue)
1440 goto err;
1441
1442 /* decrement free_queue to make it zero based */
1443 free_queue--;
1444
1445 clear_bit(free_queue, &nt->qp_bitmap);
1446
1447 qp = &nt->qps[free_queue];
1448 qp->cb_data = data;
1449 qp->rx_handler = handlers->rx_handler;
1450 qp->tx_handler = handlers->tx_handler;
1451 qp->event_handler = handlers->event_handler;
1452
Jon Mason94681192013-11-19 09:34:47 -07001453 dmaengine_get();
Jon Mason282a2fe2013-02-12 09:52:50 -07001454 qp->dma_chan = dma_find_channel(DMA_MEMCPY);
Jon Mason94681192013-11-19 09:34:47 -07001455 if (!qp->dma_chan) {
1456 dmaengine_put();
Jon Mason282a2fe2013-02-12 09:52:50 -07001457 dev_info(&pdev->dev, "Unable to allocate DMA channel, using CPU instead\n");
Jon Mason94681192013-11-19 09:34:47 -07001458 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001459
Jon Masonfce8a7b2012-11-16 19:27:12 -07001460 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1461 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1462 if (!entry)
1463 goto err1;
1464
Jon Mason282a2fe2013-02-12 09:52:50 -07001465 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001466 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001467 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001468 }
1469
1470 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1471 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1472 if (!entry)
1473 goto err2;
1474
Jon Mason282a2fe2013-02-12 09:52:50 -07001475 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001476 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001477 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001478 }
1479
Jon Masonfce8a7b2012-11-16 19:27:12 -07001480 rc = ntb_register_db_callback(qp->ndev, free_queue, qp,
1481 ntb_transport_rxc_db);
1482 if (rc)
Jon Masone8aeb602013-04-18 17:59:44 -07001483 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001484
1485 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1486
1487 return qp;
1488
Jon Masonfce8a7b2012-11-16 19:27:12 -07001489err2:
Jon Masonf7667552013-01-19 02:02:24 -07001490 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001491 kfree(entry);
1492err1:
Jon Masonf7667552013-01-19 02:02:24 -07001493 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001494 kfree(entry);
Jon Mason94681192013-11-19 09:34:47 -07001495 if (qp->dma_chan)
1496 dmaengine_put();
Jon Masonfce8a7b2012-11-16 19:27:12 -07001497 set_bit(free_queue, &nt->qp_bitmap);
1498err:
1499 return NULL;
1500}
1501EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1502
1503/**
1504 * ntb_transport_free_queue - Frees NTB transport queue
1505 * @qp: NTB queue to be freed
1506 *
1507 * Frees NTB transport queue
1508 */
1509void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1510{
Jon Mason186f27f2013-01-22 11:35:40 -07001511 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001512 struct ntb_queue_entry *entry;
1513
1514 if (!qp)
1515 return;
1516
Jon Mason186f27f2013-01-22 11:35:40 -07001517 pdev = ntb_query_pdev(qp->ndev);
1518
Jon Mason282a2fe2013-02-12 09:52:50 -07001519 if (qp->dma_chan) {
1520 struct dma_chan *chan = qp->dma_chan;
1521 /* Putting the dma_chan to NULL will force any new traffic to be
1522 * processed by the CPU instead of the DAM engine
1523 */
1524 qp->dma_chan = NULL;
1525
1526 /* Try to be nice and wait for any queued DMA engine
1527 * transactions to process before smashing it with a rock
1528 */
1529 dma_sync_wait(chan, qp->last_cookie);
1530 dmaengine_terminate_all(chan);
1531 dmaengine_put();
1532 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001533
1534 ntb_unregister_db_callback(qp->ndev, qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001535
Jon Mason282a2fe2013-02-12 09:52:50 -07001536 cancel_delayed_work_sync(&qp->link_work);
1537
Jon Masonf7667552013-01-19 02:02:24 -07001538 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001539 kfree(entry);
1540
Jon Masonf7667552013-01-19 02:02:24 -07001541 while ((entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q))) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001542 dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n");
1543 kfree(entry);
1544 }
1545
Jon Masonf7667552013-01-19 02:02:24 -07001546 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001547 kfree(entry);
1548
1549 set_bit(qp->qp_num, &qp->transport->qp_bitmap);
1550
1551 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1552}
1553EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1554
1555/**
1556 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1557 * @qp: NTB queue to be freed
1558 * @len: pointer to variable to write enqueued buffers length
1559 *
1560 * Dequeues unused buffers from receive queue. Should only be used during
1561 * shutdown of qp.
1562 *
1563 * RETURNS: NULL error value on error, or void* for success.
1564 */
1565void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1566{
1567 struct ntb_queue_entry *entry;
1568 void *buf;
1569
1570 if (!qp || qp->client_ready == NTB_LINK_UP)
1571 return NULL;
1572
1573 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1574 if (!entry)
1575 return NULL;
1576
1577 buf = entry->cb_data;
1578 *len = entry->len;
1579
Jon Masonf7667552013-01-19 02:02:24 -07001580 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001581
1582 return buf;
1583}
1584EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1585
1586/**
1587 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1588 * @qp: NTB transport layer queue the entry is to be enqueued on
1589 * @cb: per buffer pointer for callback function to use
1590 * @data: pointer to data buffer that incoming packets will be copied into
1591 * @len: length of the data buffer
1592 *
1593 * Enqueue a new receive buffer onto the transport queue into which a NTB
1594 * payload can be received into.
1595 *
1596 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1597 */
1598int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1599 unsigned int len)
1600{
1601 struct ntb_queue_entry *entry;
1602
1603 if (!qp)
1604 return -EINVAL;
1605
1606 entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q);
1607 if (!entry)
1608 return -ENOMEM;
1609
1610 entry->cb_data = cb;
1611 entry->buf = data;
1612 entry->len = len;
1613
Jon Masonf7667552013-01-19 02:02:24 -07001614 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001615
1616 return 0;
1617}
1618EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1619
1620/**
1621 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1622 * @qp: NTB transport layer queue the entry is to be enqueued on
1623 * @cb: per buffer pointer for callback function to use
1624 * @data: pointer to data buffer that will be sent
1625 * @len: length of the data buffer
1626 *
1627 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07001628 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07001629 * serialize access to the qp.
1630 *
1631 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1632 */
1633int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1634 unsigned int len)
1635{
1636 struct ntb_queue_entry *entry;
1637 int rc;
1638
1639 if (!qp || qp->qp_link != NTB_LINK_UP || !len)
1640 return -EINVAL;
1641
1642 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001643 if (!entry) {
1644 qp->tx_err_no_buf++;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001645 return -ENOMEM;
Jon Mason282a2fe2013-02-12 09:52:50 -07001646 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001647
1648 entry->cb_data = cb;
1649 entry->buf = data;
1650 entry->len = len;
1651 entry->flags = 0;
1652
1653 rc = ntb_process_tx(qp, entry);
1654 if (rc)
1655 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1656 &qp->tx_free_q);
1657
1658 return rc;
1659}
1660EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1661
1662/**
1663 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1664 * @qp: NTB transport layer queue to be enabled
1665 *
1666 * Notify NTB transport layer of client readiness to use queue
1667 */
1668void ntb_transport_link_up(struct ntb_transport_qp *qp)
1669{
1670 if (!qp)
1671 return;
1672
1673 qp->client_ready = NTB_LINK_UP;
1674
1675 if (qp->transport->transport_link == NTB_LINK_UP)
1676 schedule_delayed_work(&qp->link_work, 0);
1677}
1678EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1679
1680/**
1681 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1682 * @qp: NTB transport layer queue to be disabled
1683 *
1684 * Notify NTB transport layer of client's desire to no longer receive data on
1685 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07001686 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07001687 */
1688void ntb_transport_link_down(struct ntb_transport_qp *qp)
1689{
Jon Mason186f27f2013-01-22 11:35:40 -07001690 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001691 int rc, val;
1692
1693 if (!qp)
1694 return;
1695
Jon Mason186f27f2013-01-22 11:35:40 -07001696 pdev = ntb_query_pdev(qp->ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001697 qp->client_ready = NTB_LINK_DOWN;
1698
1699 rc = ntb_read_local_spad(qp->ndev, QP_LINKS, &val);
1700 if (rc) {
1701 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
1702 return;
1703 }
1704
1705 rc = ntb_write_remote_spad(qp->ndev, QP_LINKS,
1706 val & ~(1 << qp->qp_num));
1707 if (rc)
1708 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
1709 val & ~(1 << qp->qp_num), QP_LINKS);
1710
1711 if (qp->qp_link == NTB_LINK_UP)
1712 ntb_send_link_down(qp);
1713 else
1714 cancel_delayed_work_sync(&qp->link_work);
1715}
1716EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1717
1718/**
1719 * ntb_transport_link_query - Query transport link state
1720 * @qp: NTB transport layer queue to be queried
1721 *
1722 * Query connectivity to the remote system of the NTB transport queue
1723 *
1724 * RETURNS: true for link up or false for link down
1725 */
1726bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1727{
Jon Mason186f27f2013-01-22 11:35:40 -07001728 if (!qp)
1729 return false;
1730
Jon Masonfce8a7b2012-11-16 19:27:12 -07001731 return qp->qp_link == NTB_LINK_UP;
1732}
1733EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1734
1735/**
1736 * ntb_transport_qp_num - Query the qp number
1737 * @qp: NTB transport layer queue to be queried
1738 *
1739 * Query qp number of the NTB transport queue
1740 *
1741 * RETURNS: a zero based number specifying the qp number
1742 */
1743unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1744{
Jon Mason186f27f2013-01-22 11:35:40 -07001745 if (!qp)
1746 return 0;
1747
Jon Masonfce8a7b2012-11-16 19:27:12 -07001748 return qp->qp_num;
1749}
1750EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1751
1752/**
1753 * ntb_transport_max_size - Query the max payload size of a qp
1754 * @qp: NTB transport layer queue to be queried
1755 *
1756 * Query the maximum payload size permissible on the given qp
1757 *
1758 * RETURNS: the max payload size of a qp
1759 */
Jon Masonef114ed2013-01-19 02:02:18 -07001760unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001761{
Jon Mason282a2fe2013-02-12 09:52:50 -07001762 unsigned int max;
1763
Jon Mason186f27f2013-01-22 11:35:40 -07001764 if (!qp)
1765 return 0;
1766
Jon Mason282a2fe2013-02-12 09:52:50 -07001767 if (!qp->dma_chan)
1768 return qp->tx_max_frame - sizeof(struct ntb_payload_header);
1769
1770 /* If DMA engine usage is possible, try to find the max size for that */
1771 max = qp->tx_max_frame - sizeof(struct ntb_payload_header);
1772 max -= max % (1 << qp->dma_chan->device->copy_align);
1773
1774 return max;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001775}
1776EXPORT_SYMBOL_GPL(ntb_transport_max_size);