blob: e0bdfd7f9930aab4b42984b468ff87fac2868f44 [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>
50#include <linux/dma-mapping.h>
51#include <linux/errno.h>
52#include <linux/export.h>
53#include <linux/interrupt.h>
54#include <linux/module.h>
55#include <linux/pci.h>
56#include <linux/slab.h>
57#include <linux/types.h>
58#include <linux/ntb.h>
59#include "ntb_hw.h"
60
Jon Mason74465642013-01-21 15:28:52 -070061#define NTB_TRANSPORT_VERSION 2
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
67static unsigned char max_num_clients = 2;
68module_param(max_num_clients, byte, 0644);
69MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
70
71struct ntb_queue_entry {
72 /* ntb_queue list reference */
73 struct list_head entry;
74 /* pointers to data to be transfered */
75 void *cb_data;
76 void *buf;
77 unsigned int len;
78 unsigned int flags;
79};
80
Jon Mason793c20e2013-01-19 02:02:26 -070081struct ntb_rx_info {
82 unsigned int entry;
83};
84
Jon Masonfce8a7b2012-11-16 19:27:12 -070085struct ntb_transport_qp {
86 struct ntb_transport *transport;
87 struct ntb_device *ndev;
88 void *cb_data;
89
90 bool client_ready;
91 bool qp_link;
92 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
93
Jon Mason74465642013-01-21 15:28:52 -070094 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -070095 struct ntb_rx_info *remote_rx_info;
96
Jon Masonfce8a7b2012-11-16 19:27:12 -070097 void (*tx_handler) (struct ntb_transport_qp *qp, void *qp_data,
98 void *data, int len);
99 struct list_head tx_free_q;
100 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700101 void __iomem *tx_mw;
Jon Mason793c20e2013-01-19 02:02:26 -0700102 unsigned int tx_index;
103 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700104 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700105
106 void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data,
107 void *data, int len);
108 struct tasklet_struct rx_work;
109 struct list_head rx_pend_q;
110 struct list_head rx_free_q;
111 spinlock_t ntb_rx_pend_q_lock;
112 spinlock_t ntb_rx_free_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700113 void *rx_buff;
114 unsigned int rx_index;
115 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700116 unsigned int rx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700117
118 void (*event_handler) (void *data, int status);
119 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700120 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700121
122 struct dentry *debugfs_dir;
123 struct dentry *debugfs_stats;
124
125 /* Stats */
126 u64 rx_bytes;
127 u64 rx_pkts;
128 u64 rx_ring_empty;
129 u64 rx_err_no_buf;
130 u64 rx_err_oflow;
131 u64 rx_err_ver;
132 u64 tx_bytes;
133 u64 tx_pkts;
134 u64 tx_ring_full;
135};
136
137struct ntb_transport_mw {
138 size_t size;
139 void *virt_addr;
140 dma_addr_t dma_addr;
141};
142
143struct ntb_transport_client_dev {
144 struct list_head entry;
145 struct device dev;
146};
147
148struct ntb_transport {
149 struct list_head entry;
150 struct list_head client_devs;
151
152 struct ntb_device *ndev;
153 struct ntb_transport_mw mw[NTB_NUM_MW];
154 struct ntb_transport_qp *qps;
155 unsigned int max_qps;
156 unsigned long qp_bitmap;
157 bool transport_link;
158 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700159 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700160 struct dentry *debugfs_dir;
161};
162
163enum {
164 DESC_DONE_FLAG = 1 << 0,
165 LINK_DOWN_FLAG = 1 << 1,
166};
167
168struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700169 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700170 unsigned int len;
171 unsigned int flags;
172};
173
174enum {
175 VERSION = 0,
176 MW0_SZ,
177 MW1_SZ,
178 NUM_QPS,
179 QP_LINKS,
180 MAX_SPAD,
181};
182
183#define QP_TO_MW(qp) ((qp) % NTB_NUM_MW)
184#define NTB_QP_DEF_NUM_ENTRIES 100
185#define NTB_LINK_DOWN_TIMEOUT 10
186
187static int ntb_match_bus(struct device *dev, struct device_driver *drv)
188{
189 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
190}
191
192static int ntb_client_probe(struct device *dev)
193{
194 const struct ntb_client *drv = container_of(dev->driver,
195 struct ntb_client, driver);
196 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
197 int rc = -EINVAL;
198
199 get_device(dev);
200 if (drv && drv->probe)
201 rc = drv->probe(pdev);
202 if (rc)
203 put_device(dev);
204
205 return rc;
206}
207
208static int ntb_client_remove(struct device *dev)
209{
210 const struct ntb_client *drv = container_of(dev->driver,
211 struct ntb_client, driver);
212 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
213
214 if (drv && drv->remove)
215 drv->remove(pdev);
216
217 put_device(dev);
218
219 return 0;
220}
221
Jon Mason170d35a2013-01-19 02:02:23 -0700222static struct bus_type ntb_bus_type = {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700223 .name = "ntb_bus",
224 .match = ntb_match_bus,
225 .probe = ntb_client_probe,
226 .remove = ntb_client_remove,
227};
228
229static LIST_HEAD(ntb_transport_list);
230
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800231static int ntb_bus_init(struct ntb_transport *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700232{
233 if (list_empty(&ntb_transport_list)) {
234 int rc = bus_register(&ntb_bus_type);
235 if (rc)
236 return rc;
237 }
238
239 list_add(&nt->entry, &ntb_transport_list);
240
241 return 0;
242}
243
Greg Kroah-Hartman78a61ab2013-01-17 19:17:42 -0800244static void ntb_bus_remove(struct ntb_transport *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700245{
246 struct ntb_transport_client_dev *client_dev, *cd;
247
248 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
249 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
250 dev_name(&client_dev->dev));
251 list_del(&client_dev->entry);
252 device_unregister(&client_dev->dev);
253 }
254
255 list_del(&nt->entry);
256
257 if (list_empty(&ntb_transport_list))
258 bus_unregister(&ntb_bus_type);
259}
260
261static void ntb_client_release(struct device *dev)
262{
263 struct ntb_transport_client_dev *client_dev;
264 client_dev = container_of(dev, struct ntb_transport_client_dev, dev);
265
266 kfree(client_dev);
267}
268
269/**
270 * ntb_unregister_client_dev - Unregister NTB client device
271 * @device_name: Name of NTB client device
272 *
273 * Unregister an NTB client device with the NTB transport layer
274 */
275void ntb_unregister_client_dev(char *device_name)
276{
277 struct ntb_transport_client_dev *client, *cd;
278 struct ntb_transport *nt;
279
280 list_for_each_entry(nt, &ntb_transport_list, entry)
281 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
282 if (!strncmp(dev_name(&client->dev), device_name,
283 strlen(device_name))) {
284 list_del(&client->entry);
285 device_unregister(&client->dev);
286 }
287}
288EXPORT_SYMBOL_GPL(ntb_unregister_client_dev);
289
290/**
291 * ntb_register_client_dev - Register NTB client device
292 * @device_name: Name of NTB client device
293 *
294 * Register an NTB client device with the NTB transport layer
295 */
296int ntb_register_client_dev(char *device_name)
297{
298 struct ntb_transport_client_dev *client_dev;
299 struct ntb_transport *nt;
300 int rc;
301
Jon Mason8222b402013-01-19 02:02:16 -0700302 if (list_empty(&ntb_transport_list))
303 return -ENODEV;
304
Jon Masonfce8a7b2012-11-16 19:27:12 -0700305 list_for_each_entry(nt, &ntb_transport_list, entry) {
306 struct device *dev;
307
308 client_dev = kzalloc(sizeof(struct ntb_transport_client_dev),
309 GFP_KERNEL);
310 if (!client_dev) {
311 rc = -ENOMEM;
312 goto err;
313 }
314
315 dev = &client_dev->dev;
316
317 /* setup and register client devices */
318 dev_set_name(dev, "%s", device_name);
319 dev->bus = &ntb_bus_type;
320 dev->release = ntb_client_release;
321 dev->parent = &ntb_query_pdev(nt->ndev)->dev;
322
323 rc = device_register(dev);
324 if (rc) {
325 kfree(client_dev);
326 goto err;
327 }
328
329 list_add_tail(&client_dev->entry, &nt->client_devs);
330 }
331
332 return 0;
333
334err:
335 ntb_unregister_client_dev(device_name);
336
337 return rc;
338}
339EXPORT_SYMBOL_GPL(ntb_register_client_dev);
340
341/**
342 * ntb_register_client - Register NTB client driver
343 * @drv: NTB client driver to be registered
344 *
345 * Register an NTB client driver with the NTB transport layer
346 *
347 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
348 */
349int ntb_register_client(struct ntb_client *drv)
350{
351 drv->driver.bus = &ntb_bus_type;
352
Jon Mason8222b402013-01-19 02:02:16 -0700353 if (list_empty(&ntb_transport_list))
354 return -ENODEV;
355
Jon Masonfce8a7b2012-11-16 19:27:12 -0700356 return driver_register(&drv->driver);
357}
358EXPORT_SYMBOL_GPL(ntb_register_client);
359
360/**
361 * ntb_unregister_client - Unregister NTB client driver
362 * @drv: NTB client driver to be unregistered
363 *
364 * Unregister an NTB client driver with the NTB transport layer
365 *
366 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
367 */
368void ntb_unregister_client(struct ntb_client *drv)
369{
370 driver_unregister(&drv->driver);
371}
372EXPORT_SYMBOL_GPL(ntb_unregister_client);
373
Jon Masonfce8a7b2012-11-16 19:27:12 -0700374static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
375 loff_t *offp)
376{
377 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700378 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700379 ssize_t ret, out_offset, out_count;
380
Jon Masond7237e22013-01-19 02:02:25 -0700381 out_count = 600;
382
383 buf = kmalloc(out_count, GFP_KERNEL);
384 if (!buf)
385 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700386
387 qp = filp->private_data;
388 out_offset = 0;
389 out_offset += snprintf(buf + out_offset, out_count - out_offset,
390 "NTB QP stats\n");
391 out_offset += snprintf(buf + out_offset, out_count - out_offset,
392 "rx_bytes - \t%llu\n", qp->rx_bytes);
393 out_offset += snprintf(buf + out_offset, out_count - out_offset,
394 "rx_pkts - \t%llu\n", qp->rx_pkts);
395 out_offset += snprintf(buf + out_offset, out_count - out_offset,
396 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
397 out_offset += snprintf(buf + out_offset, out_count - out_offset,
398 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
399 out_offset += snprintf(buf + out_offset, out_count - out_offset,
400 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
401 out_offset += snprintf(buf + out_offset, out_count - out_offset,
402 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
403 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700404 "rx_buff - \t%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700405 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700406 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700407 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700408 "rx_max_entry - \t%u\n", qp->rx_max_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700409
410 out_offset += snprintf(buf + out_offset, out_count - out_offset,
411 "tx_bytes - \t%llu\n", qp->tx_bytes);
412 out_offset += snprintf(buf + out_offset, out_count - out_offset,
413 "tx_pkts - \t%llu\n", qp->tx_pkts);
414 out_offset += snprintf(buf + out_offset, out_count - out_offset,
415 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
416 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700417 "tx_mw - \t%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700418 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700419 "tx_index - \t%u\n", qp->tx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700420 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700421 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700422
423 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masond7237e22013-01-19 02:02:25 -0700424 "\nQP Link %s\n", (qp->qp_link == NTB_LINK_UP) ?
Jon Masonfce8a7b2012-11-16 19:27:12 -0700425 "Up" : "Down");
Jon Masond7237e22013-01-19 02:02:25 -0700426 if (out_offset > out_count)
427 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700428
429 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700430 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700431 return ret;
432}
433
434static const struct file_operations ntb_qp_debugfs_stats = {
435 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700436 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700437 .read = debugfs_read,
438};
439
440static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
441 struct list_head *list)
442{
443 unsigned long flags;
444
445 spin_lock_irqsave(lock, flags);
446 list_add_tail(entry, list);
447 spin_unlock_irqrestore(lock, flags);
448}
449
450static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
451 struct list_head *list)
452{
453 struct ntb_queue_entry *entry;
454 unsigned long flags;
455
456 spin_lock_irqsave(lock, flags);
457 if (list_empty(list)) {
458 entry = NULL;
459 goto out;
460 }
461 entry = list_first_entry(list, struct ntb_queue_entry, entry);
462 list_del(&entry->entry);
463out:
464 spin_unlock_irqrestore(lock, flags);
465
466 return entry;
467}
468
469static void ntb_transport_setup_qp_mw(struct ntb_transport *nt,
470 unsigned int qp_num)
471{
472 struct ntb_transport_qp *qp = &nt->qps[qp_num];
Jon Masonef114ed2013-01-19 02:02:18 -0700473 unsigned int rx_size, num_qps_mw;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700474 u8 mw_num = QP_TO_MW(qp_num);
Jon Mason793c20e2013-01-19 02:02:26 -0700475 unsigned int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700476
Jon Mason74465642013-01-21 15:28:52 -0700477 WARN_ON(nt->mw[mw_num].virt_addr == NULL);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700478
Jon Masonef114ed2013-01-19 02:02:18 -0700479 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
480 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700481 else
482 num_qps_mw = nt->max_qps / NTB_NUM_MW;
483
Jon Mason793c20e2013-01-19 02:02:26 -0700484 rx_size = (unsigned int) nt->mw[mw_num].size / num_qps_mw;
485 qp->remote_rx_info = nt->mw[mw_num].virt_addr +
486 (qp_num / NTB_NUM_MW * rx_size);
487 rx_size -= sizeof(struct ntb_rx_info);
488
489 qp->rx_buff = qp->remote_rx_info + sizeof(struct ntb_rx_info);
Jon Masonef114ed2013-01-19 02:02:18 -0700490 qp->rx_max_frame = min(transport_mtu, rx_size);
Jon Mason793c20e2013-01-19 02:02:26 -0700491 qp->rx_max_entry = rx_size / qp->rx_max_frame;
492 qp->rx_index = 0;
493
494 qp->remote_rx_info->entry = qp->rx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700495
Jon Masonef114ed2013-01-19 02:02:18 -0700496 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700497 for (i = 0; i < qp->rx_max_entry; i++) {
498 void *offset = qp->rx_buff + qp->rx_max_frame * (i + 1) -
499 sizeof(struct ntb_payload_header);
Jon Masonef114ed2013-01-19 02:02:18 -0700500 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700501 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700502
503 qp->rx_pkts = 0;
504 qp->tx_pkts = 0;
505}
506
507static int ntb_set_mw(struct ntb_transport *nt, int num_mw, unsigned int size)
508{
509 struct ntb_transport_mw *mw = &nt->mw[num_mw];
510 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700511
512 /* Alloc memory for receiving data. Must be 4k aligned */
513 mw->size = ALIGN(size, 4096);
514
515 mw->virt_addr = dma_alloc_coherent(&pdev->dev, mw->size, &mw->dma_addr,
516 GFP_KERNEL);
517 if (!mw->virt_addr) {
518 dev_err(&pdev->dev, "Unable to allocate MW buffer of size %d\n",
519 (int) mw->size);
520 return -ENOMEM;
521 }
522
Jon Masonfce8a7b2012-11-16 19:27:12 -0700523 /* Notify HW the memory location of the receive buffer */
524 ntb_set_mw_addr(nt->ndev, num_mw, mw->dma_addr);
525
526 return 0;
527}
528
Jon Mason7b4f2d32013-01-19 02:02:19 -0700529static void ntb_qp_link_cleanup(struct work_struct *work)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700530{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700531 struct ntb_transport_qp *qp = container_of(work,
532 struct ntb_transport_qp,
533 link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700534 struct ntb_transport *nt = qp->transport;
535 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
536
537 if (qp->qp_link == NTB_LINK_DOWN) {
538 cancel_delayed_work_sync(&qp->link_work);
539 return;
540 }
541
542 if (qp->event_handler)
543 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
544
545 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
546 qp->qp_link = NTB_LINK_DOWN;
547
548 if (nt->transport_link == NTB_LINK_UP)
549 schedule_delayed_work(&qp->link_work,
550 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
551}
552
Jon Mason7b4f2d32013-01-19 02:02:19 -0700553static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700554{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700555 schedule_work(&qp->link_cleanup);
556}
557
558static void ntb_transport_link_cleanup(struct work_struct *work)
559{
560 struct ntb_transport *nt = container_of(work, struct ntb_transport,
561 link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700562 int i;
563
564 if (nt->transport_link == NTB_LINK_DOWN)
565 cancel_delayed_work_sync(&nt->link_work);
566 else
567 nt->transport_link = NTB_LINK_DOWN;
568
569 /* Pass along the info to any clients */
570 for (i = 0; i < nt->max_qps; i++)
571 if (!test_bit(i, &nt->qp_bitmap))
572 ntb_qp_link_down(&nt->qps[i]);
573
574 /* The scratchpad registers keep the values if the remote side
575 * goes down, blast them now to give them a sane value the next
576 * time they are accessed
577 */
578 for (i = 0; i < MAX_SPAD; i++)
579 ntb_write_local_spad(nt->ndev, i, 0);
580}
581
582static void ntb_transport_event_callback(void *data, enum ntb_hw_event event)
583{
584 struct ntb_transport *nt = data;
585
586 switch (event) {
587 case NTB_EVENT_HW_LINK_UP:
588 schedule_delayed_work(&nt->link_work, 0);
589 break;
590 case NTB_EVENT_HW_LINK_DOWN:
Jon Mason7b4f2d32013-01-19 02:02:19 -0700591 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700592 break;
593 default:
594 BUG();
595 }
596}
597
598static void ntb_transport_link_work(struct work_struct *work)
599{
600 struct ntb_transport *nt = container_of(work, struct ntb_transport,
601 link_work.work);
602 struct ntb_device *ndev = nt->ndev;
603 struct pci_dev *pdev = ntb_query_pdev(ndev);
604 u32 val;
605 int rc, i;
606
607 /* send the local info */
608 rc = ntb_write_remote_spad(ndev, VERSION, NTB_TRANSPORT_VERSION);
609 if (rc) {
610 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
611 0, VERSION);
612 goto out;
613 }
614
615 rc = ntb_write_remote_spad(ndev, MW0_SZ, ntb_get_mw_size(ndev, 0));
616 if (rc) {
617 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
618 (u32) ntb_get_mw_size(ndev, 0), MW0_SZ);
619 goto out;
620 }
621
622 rc = ntb_write_remote_spad(ndev, MW1_SZ, ntb_get_mw_size(ndev, 1));
623 if (rc) {
624 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
625 (u32) ntb_get_mw_size(ndev, 1), MW1_SZ);
626 goto out;
627 }
628
629 rc = ntb_write_remote_spad(ndev, NUM_QPS, nt->max_qps);
630 if (rc) {
631 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
632 nt->max_qps, NUM_QPS);
633 goto out;
634 }
635
636 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
637 if (rc) {
638 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
639 goto out;
640 }
641
642 rc = ntb_write_remote_spad(ndev, QP_LINKS, val);
643 if (rc) {
644 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
645 val, QP_LINKS);
646 goto out;
647 }
648
649 /* Query the remote side for its info */
650 rc = ntb_read_remote_spad(ndev, VERSION, &val);
651 if (rc) {
652 dev_err(&pdev->dev, "Error reading remote spad %d\n", VERSION);
653 goto out;
654 }
655
656 if (val != NTB_TRANSPORT_VERSION)
657 goto out;
658 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
659
660 rc = ntb_read_remote_spad(ndev, NUM_QPS, &val);
661 if (rc) {
662 dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_QPS);
663 goto out;
664 }
665
666 if (val != nt->max_qps)
667 goto out;
668 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
669
670 rc = ntb_read_remote_spad(ndev, MW0_SZ, &val);
671 if (rc) {
672 dev_err(&pdev->dev, "Error reading remote spad %d\n", MW0_SZ);
673 goto out;
674 }
675
676 if (!val)
677 goto out;
678 dev_dbg(&pdev->dev, "Remote MW0 size = %d\n", val);
679
680 rc = ntb_set_mw(nt, 0, val);
681 if (rc)
682 goto out;
683
684 rc = ntb_read_remote_spad(ndev, MW1_SZ, &val);
685 if (rc) {
686 dev_err(&pdev->dev, "Error reading remote spad %d\n", MW1_SZ);
687 goto out;
688 }
689
690 if (!val)
691 goto out;
692 dev_dbg(&pdev->dev, "Remote MW1 size = %d\n", val);
693
694 rc = ntb_set_mw(nt, 1, val);
695 if (rc)
696 goto out;
697
698 nt->transport_link = NTB_LINK_UP;
699
700 for (i = 0; i < nt->max_qps; i++) {
701 struct ntb_transport_qp *qp = &nt->qps[i];
702
703 ntb_transport_setup_qp_mw(nt, i);
704
705 if (qp->client_ready == NTB_LINK_UP)
706 schedule_delayed_work(&qp->link_work, 0);
707 }
708
709 return;
710
711out:
712 if (ntb_hw_link_status(ndev))
713 schedule_delayed_work(&nt->link_work,
714 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
715}
716
717static void ntb_qp_link_work(struct work_struct *work)
718{
719 struct ntb_transport_qp *qp = container_of(work,
720 struct ntb_transport_qp,
721 link_work.work);
722 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
723 struct ntb_transport *nt = qp->transport;
724 int rc, val;
725
726 WARN_ON(nt->transport_link != NTB_LINK_UP);
727
728 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
729 if (rc) {
730 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
731 return;
732 }
733
734 rc = ntb_write_remote_spad(nt->ndev, QP_LINKS, val | 1 << qp->qp_num);
735 if (rc)
736 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
737 val | 1 << qp->qp_num, QP_LINKS);
738
739 /* query remote spad for qp ready bits */
740 rc = ntb_read_remote_spad(nt->ndev, QP_LINKS, &val);
741 if (rc)
742 dev_err(&pdev->dev, "Error reading remote spad %d\n", QP_LINKS);
743
744 dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val);
745
746 /* See if the remote side is up */
747 if (1 << qp->qp_num & val) {
748 qp->qp_link = NTB_LINK_UP;
749
750 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
751 if (qp->event_handler)
752 qp->event_handler(qp->cb_data, NTB_LINK_UP);
753 } else if (nt->transport_link == NTB_LINK_UP)
754 schedule_delayed_work(&qp->link_work,
755 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
756}
757
758static void ntb_transport_init_queue(struct ntb_transport *nt,
759 unsigned int qp_num)
760{
761 struct ntb_transport_qp *qp;
Jon Masonef114ed2013-01-19 02:02:18 -0700762 unsigned int num_qps_mw, tx_size;
763 u8 mw_num = QP_TO_MW(qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700764
765 qp = &nt->qps[qp_num];
766 qp->qp_num = qp_num;
767 qp->transport = nt;
768 qp->ndev = nt->ndev;
769 qp->qp_link = NTB_LINK_DOWN;
770 qp->client_ready = NTB_LINK_DOWN;
771 qp->event_handler = NULL;
772
Jon Masonef114ed2013-01-19 02:02:18 -0700773 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
774 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
775 else
776 num_qps_mw = nt->max_qps / NTB_NUM_MW;
777
Jon Mason793c20e2013-01-19 02:02:26 -0700778 tx_size = (unsigned int) ntb_get_mw_size(qp->ndev, mw_num) / num_qps_mw;
779 qp->rx_info = ntb_get_mw_vbase(nt->ndev, mw_num) +
780 (qp_num / NTB_NUM_MW * tx_size);
781 tx_size -= sizeof(struct ntb_rx_info);
782
783 qp->tx_mw = qp->rx_info + sizeof(struct ntb_rx_info);
Jon Masonef114ed2013-01-19 02:02:18 -0700784 qp->tx_max_frame = min(transport_mtu, tx_size);
Jon Mason793c20e2013-01-19 02:02:26 -0700785 qp->tx_max_entry = tx_size / qp->tx_max_frame;
786 qp->tx_index = 0;
Jon Masonef114ed2013-01-19 02:02:18 -0700787
Jon Masonfce8a7b2012-11-16 19:27:12 -0700788 if (nt->debugfs_dir) {
789 char debugfs_name[4];
790
791 snprintf(debugfs_name, 4, "qp%d", qp_num);
792 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
793 nt->debugfs_dir);
794
795 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
796 qp->debugfs_dir, qp,
797 &ntb_qp_debugfs_stats);
798 }
799
800 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Mason7b4f2d32013-01-19 02:02:19 -0700801 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700802
803 spin_lock_init(&qp->ntb_rx_pend_q_lock);
804 spin_lock_init(&qp->ntb_rx_free_q_lock);
805 spin_lock_init(&qp->ntb_tx_free_q_lock);
806
807 INIT_LIST_HEAD(&qp->rx_pend_q);
808 INIT_LIST_HEAD(&qp->rx_free_q);
809 INIT_LIST_HEAD(&qp->tx_free_q);
810}
811
812int ntb_transport_init(struct pci_dev *pdev)
813{
814 struct ntb_transport *nt;
815 int rc, i;
816
817 nt = kzalloc(sizeof(struct ntb_transport), GFP_KERNEL);
818 if (!nt)
819 return -ENOMEM;
820
821 if (debugfs_initialized())
822 nt->debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
823 else
824 nt->debugfs_dir = NULL;
825
826 nt->ndev = ntb_register_transport(pdev, nt);
827 if (!nt->ndev) {
828 rc = -EIO;
829 goto err;
830 }
831
832 nt->max_qps = min(nt->ndev->max_cbs, max_num_clients);
833
834 nt->qps = kcalloc(nt->max_qps, sizeof(struct ntb_transport_qp),
835 GFP_KERNEL);
836 if (!nt->qps) {
837 rc = -ENOMEM;
838 goto err1;
839 }
840
841 nt->qp_bitmap = ((u64) 1 << nt->max_qps) - 1;
842
843 for (i = 0; i < nt->max_qps; i++)
844 ntb_transport_init_queue(nt, i);
845
846 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Mason7b4f2d32013-01-19 02:02:19 -0700847 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700848
849 rc = ntb_register_event_callback(nt->ndev,
850 ntb_transport_event_callback);
851 if (rc)
852 goto err2;
853
854 INIT_LIST_HEAD(&nt->client_devs);
855 rc = ntb_bus_init(nt);
856 if (rc)
857 goto err3;
858
859 if (ntb_hw_link_status(nt->ndev))
860 schedule_delayed_work(&nt->link_work, 0);
861
862 return 0;
863
864err3:
865 ntb_unregister_event_callback(nt->ndev);
866err2:
867 kfree(nt->qps);
868err1:
869 ntb_unregister_transport(nt->ndev);
870err:
871 debugfs_remove_recursive(nt->debugfs_dir);
872 kfree(nt);
873 return rc;
874}
875
876void ntb_transport_free(void *transport)
877{
878 struct ntb_transport *nt = transport;
879 struct pci_dev *pdev;
880 int i;
881
882 nt->transport_link = NTB_LINK_DOWN;
883
884 /* verify that all the qp's are freed */
885 for (i = 0; i < nt->max_qps; i++)
886 if (!test_bit(i, &nt->qp_bitmap))
887 ntb_transport_free_queue(&nt->qps[i]);
888
889 ntb_bus_remove(nt);
890
891 cancel_delayed_work_sync(&nt->link_work);
892
893 debugfs_remove_recursive(nt->debugfs_dir);
894
895 ntb_unregister_event_callback(nt->ndev);
896
897 pdev = ntb_query_pdev(nt->ndev);
898
899 for (i = 0; i < NTB_NUM_MW; i++)
900 if (nt->mw[i].virt_addr)
901 dma_free_coherent(&pdev->dev, nt->mw[i].size,
902 nt->mw[i].virt_addr,
903 nt->mw[i].dma_addr);
904
905 kfree(nt->qps);
906 ntb_unregister_transport(nt->ndev);
907 kfree(nt);
908}
909
910static void ntb_rx_copy_task(struct ntb_transport_qp *qp,
911 struct ntb_queue_entry *entry, void *offset)
912{
Jon Mason448c6fb2013-01-19 02:02:27 -0700913 void *cb_data = entry->cb_data;
914 unsigned int len = entry->len;
915
Jon Masonfce8a7b2012-11-16 19:27:12 -0700916 memcpy(entry->buf, offset, entry->len);
917
Jon Masonfce8a7b2012-11-16 19:27:12 -0700918 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
Jon Mason448c6fb2013-01-19 02:02:27 -0700919
920 if (qp->rx_handler && qp->client_ready == NTB_LINK_UP)
921 qp->rx_handler(qp, qp->cb_data, cb_data, len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700922}
923
924static int ntb_process_rxc(struct ntb_transport_qp *qp)
925{
926 struct ntb_payload_header *hdr;
927 struct ntb_queue_entry *entry;
928 void *offset;
929
Jon Mason793c20e2013-01-19 02:02:26 -0700930 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
931 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
932
Jon Masonfce8a7b2012-11-16 19:27:12 -0700933 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
934 if (!entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700935 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
Jon Mason74465642013-01-21 15:28:52 -0700936 "no buffer - HDR ver %u, len %d, flags %x\n",
Jon Masonfce8a7b2012-11-16 19:27:12 -0700937 hdr->ver, hdr->len, hdr->flags);
938 qp->rx_err_no_buf++;
939 return -ENOMEM;
940 }
941
Jon Masonfce8a7b2012-11-16 19:27:12 -0700942 if (!(hdr->flags & DESC_DONE_FLAG)) {
943 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -0700944 &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700945 qp->rx_ring_empty++;
946 return -EAGAIN;
947 }
948
Jon Mason74465642013-01-21 15:28:52 -0700949 if (hdr->ver != (u32) qp->rx_pkts) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700950 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
Jon Mason74465642013-01-21 15:28:52 -0700951 "qp %d: version mismatch, expected %llu - got %u\n",
Jon Masonfce8a7b2012-11-16 19:27:12 -0700952 qp->qp_num, qp->rx_pkts, hdr->ver);
953 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -0700954 &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700955 qp->rx_err_ver++;
956 return -EIO;
957 }
958
959 if (hdr->flags & LINK_DOWN_FLAG) {
960 ntb_qp_link_down(qp);
961
962 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -0700963 &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700964 goto out;
965 }
966
967 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
Jon Mason74465642013-01-21 15:28:52 -0700968 "rx offset %u, ver %u - %d payload received, buf size %d\n",
Jon Mason793c20e2013-01-19 02:02:26 -0700969 qp->rx_index, hdr->ver, hdr->len, entry->len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700970
Jon Mason793c20e2013-01-19 02:02:26 -0700971 if (hdr->len <= entry->len) {
972 entry->len = hdr->len;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700973 ntb_rx_copy_task(qp, entry, offset);
Jon Mason793c20e2013-01-19 02:02:26 -0700974 } else {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700975 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -0700976 &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700977
Jon Masonfce8a7b2012-11-16 19:27:12 -0700978 qp->rx_err_oflow++;
979 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
980 "RX overflow! Wanted %d got %d\n",
981 hdr->len, entry->len);
982 }
983
984 qp->rx_bytes += hdr->len;
985 qp->rx_pkts++;
986
987out:
Jon Mason793c20e2013-01-19 02:02:26 -0700988 /* Ensure that the data is fully copied out before clearing the flag */
989 wmb();
990 hdr->flags = 0;
Jon Mason74465642013-01-21 15:28:52 -0700991 iowrite32(qp->rx_index, &qp->rx_info->entry);
Jon Mason793c20e2013-01-19 02:02:26 -0700992
993 qp->rx_index++;
994 qp->rx_index %= qp->rx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700995
996 return 0;
997}
998
999static void ntb_transport_rx(unsigned long data)
1000{
1001 struct ntb_transport_qp *qp = (struct ntb_transport_qp *)data;
1002 int rc;
1003
1004 do {
1005 rc = ntb_process_rxc(qp);
1006 } while (!rc);
1007}
1008
1009static void ntb_transport_rxc_db(void *data, int db_num)
1010{
1011 struct ntb_transport_qp *qp = data;
1012
1013 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%s: doorbell %d received\n",
1014 __func__, db_num);
1015
1016 tasklet_schedule(&qp->rx_work);
1017}
1018
1019static void ntb_tx_copy_task(struct ntb_transport_qp *qp,
1020 struct ntb_queue_entry *entry,
Jon Mason74465642013-01-21 15:28:52 -07001021 void __iomem *offset)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001022{
Jon Mason74465642013-01-21 15:28:52 -07001023 struct ntb_payload_header __iomem *hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001024
Jon Masonfce8a7b2012-11-16 19:27:12 -07001025 memcpy_toio(offset, entry->buf, entry->len);
1026
Jon Masonef114ed2013-01-19 02:02:18 -07001027 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
Jon Mason74465642013-01-21 15:28:52 -07001028 iowrite32(entry->len, &hdr->len);
1029 iowrite32((u32) qp->tx_pkts, &hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001030
1031 /* Ensure that the data is fully copied out before setting the flag */
Jon Mason842c1dd2013-01-19 02:02:17 -07001032 wmb();
Jon Mason74465642013-01-21 15:28:52 -07001033 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001034
1035 ntb_ring_sdb(qp->ndev, qp->qp_num);
1036
1037 /* The entry length can only be zero if the packet is intended to be a
1038 * "link down" or similar. Since no payload is being sent in these
1039 * cases, there is nothing to add to the completion queue.
1040 */
1041 if (entry->len > 0) {
1042 qp->tx_bytes += entry->len;
1043
1044 if (qp->tx_handler)
1045 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1046 entry->len);
1047 }
1048
1049 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1050}
1051
1052static int ntb_process_tx(struct ntb_transport_qp *qp,
1053 struct ntb_queue_entry *entry)
1054{
Jon Mason74465642013-01-21 15:28:52 -07001055 void __iomem *offset;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001056
Jon Mason793c20e2013-01-19 02:02:26 -07001057 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001058
Jon Mason793c20e2013-01-19 02:02:26 -07001059 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%lld - offset %p, tx %u, entry len %d flags %x buff %p\n",
1060 qp->tx_pkts, offset, qp->tx_index, entry->len, entry->flags,
Jon Masonf7667552013-01-19 02:02:24 -07001061 entry->buf);
Jon Mason793c20e2013-01-19 02:02:26 -07001062 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001063 qp->tx_ring_full++;
1064 return -EAGAIN;
1065 }
1066
Jon Masonef114ed2013-01-19 02:02:18 -07001067 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001068 if (qp->tx_handler)
1069 qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1070
1071 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1072 &qp->tx_free_q);
1073 return 0;
1074 }
1075
1076 ntb_tx_copy_task(qp, entry, offset);
1077
Jon Mason793c20e2013-01-19 02:02:26 -07001078 qp->tx_index++;
1079 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001080
1081 qp->tx_pkts++;
1082
1083 return 0;
1084}
1085
1086static void ntb_send_link_down(struct ntb_transport_qp *qp)
1087{
1088 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1089 struct ntb_queue_entry *entry;
1090 int i, rc;
1091
1092 if (qp->qp_link == NTB_LINK_DOWN)
1093 return;
1094
1095 qp->qp_link = NTB_LINK_DOWN;
1096 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
1097
1098 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001099 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001100 if (entry)
1101 break;
1102 msleep(100);
1103 }
1104
1105 if (!entry)
1106 return;
1107
1108 entry->cb_data = NULL;
1109 entry->buf = NULL;
1110 entry->len = 0;
1111 entry->flags = LINK_DOWN_FLAG;
1112
1113 rc = ntb_process_tx(qp, entry);
1114 if (rc)
1115 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1116 qp->qp_num);
1117}
1118
1119/**
1120 * ntb_transport_create_queue - Create a new NTB transport layer queue
1121 * @rx_handler: receive callback function
1122 * @tx_handler: transmit callback function
1123 * @event_handler: event callback function
1124 *
1125 * Create a new NTB transport layer queue and provide the queue with a callback
1126 * routine for both transmit and receive. The receive callback routine will be
1127 * used to pass up data when the transport has received it on the queue. The
1128 * transmit callback routine will be called when the transport has completed the
1129 * transmission of the data on the queue and the data is ready to be freed.
1130 *
1131 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1132 */
1133struct ntb_transport_qp *
1134ntb_transport_create_queue(void *data, struct pci_dev *pdev,
1135 const struct ntb_queue_handlers *handlers)
1136{
1137 struct ntb_queue_entry *entry;
1138 struct ntb_transport_qp *qp;
1139 struct ntb_transport *nt;
1140 unsigned int free_queue;
1141 int rc, i;
1142
1143 nt = ntb_find_transport(pdev);
1144 if (!nt)
1145 goto err;
1146
1147 free_queue = ffs(nt->qp_bitmap);
1148 if (!free_queue)
1149 goto err;
1150
1151 /* decrement free_queue to make it zero based */
1152 free_queue--;
1153
1154 clear_bit(free_queue, &nt->qp_bitmap);
1155
1156 qp = &nt->qps[free_queue];
1157 qp->cb_data = data;
1158 qp->rx_handler = handlers->rx_handler;
1159 qp->tx_handler = handlers->tx_handler;
1160 qp->event_handler = handlers->event_handler;
1161
1162 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1163 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1164 if (!entry)
1165 goto err1;
1166
1167 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001168 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001169 }
1170
1171 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1172 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1173 if (!entry)
1174 goto err2;
1175
1176 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001177 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001178 }
1179
1180 tasklet_init(&qp->rx_work, ntb_transport_rx, (unsigned long) qp);
1181
1182 rc = ntb_register_db_callback(qp->ndev, free_queue, qp,
1183 ntb_transport_rxc_db);
1184 if (rc)
1185 goto err3;
1186
1187 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1188
1189 return qp;
1190
1191err3:
1192 tasklet_disable(&qp->rx_work);
1193err2:
Jon Masonf7667552013-01-19 02:02:24 -07001194 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001195 kfree(entry);
1196err1:
Jon Masonf7667552013-01-19 02:02:24 -07001197 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001198 kfree(entry);
1199 set_bit(free_queue, &nt->qp_bitmap);
1200err:
1201 return NULL;
1202}
1203EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1204
1205/**
1206 * ntb_transport_free_queue - Frees NTB transport queue
1207 * @qp: NTB queue to be freed
1208 *
1209 * Frees NTB transport queue
1210 */
1211void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1212{
1213 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1214 struct ntb_queue_entry *entry;
1215
1216 if (!qp)
1217 return;
1218
1219 cancel_delayed_work_sync(&qp->link_work);
1220
1221 ntb_unregister_db_callback(qp->ndev, qp->qp_num);
1222 tasklet_disable(&qp->rx_work);
1223
Jon Masonf7667552013-01-19 02:02:24 -07001224 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001225 kfree(entry);
1226
Jon Masonf7667552013-01-19 02:02:24 -07001227 while ((entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q))) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001228 dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n");
1229 kfree(entry);
1230 }
1231
Jon Masonf7667552013-01-19 02:02:24 -07001232 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001233 kfree(entry);
1234
1235 set_bit(qp->qp_num, &qp->transport->qp_bitmap);
1236
1237 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1238}
1239EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1240
1241/**
1242 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1243 * @qp: NTB queue to be freed
1244 * @len: pointer to variable to write enqueued buffers length
1245 *
1246 * Dequeues unused buffers from receive queue. Should only be used during
1247 * shutdown of qp.
1248 *
1249 * RETURNS: NULL error value on error, or void* for success.
1250 */
1251void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1252{
1253 struct ntb_queue_entry *entry;
1254 void *buf;
1255
1256 if (!qp || qp->client_ready == NTB_LINK_UP)
1257 return NULL;
1258
1259 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1260 if (!entry)
1261 return NULL;
1262
1263 buf = entry->cb_data;
1264 *len = entry->len;
1265
Jon Masonf7667552013-01-19 02:02:24 -07001266 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001267
1268 return buf;
1269}
1270EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1271
1272/**
1273 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1274 * @qp: NTB transport layer queue the entry is to be enqueued on
1275 * @cb: per buffer pointer for callback function to use
1276 * @data: pointer to data buffer that incoming packets will be copied into
1277 * @len: length of the data buffer
1278 *
1279 * Enqueue a new receive buffer onto the transport queue into which a NTB
1280 * payload can be received into.
1281 *
1282 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1283 */
1284int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1285 unsigned int len)
1286{
1287 struct ntb_queue_entry *entry;
1288
1289 if (!qp)
1290 return -EINVAL;
1291
1292 entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q);
1293 if (!entry)
1294 return -ENOMEM;
1295
1296 entry->cb_data = cb;
1297 entry->buf = data;
1298 entry->len = len;
1299
Jon Masonf7667552013-01-19 02:02:24 -07001300 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001301
1302 return 0;
1303}
1304EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1305
1306/**
1307 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1308 * @qp: NTB transport layer queue the entry is to be enqueued on
1309 * @cb: per buffer pointer for callback function to use
1310 * @data: pointer to data buffer that will be sent
1311 * @len: length of the data buffer
1312 *
1313 * Enqueue a new transmit buffer onto the transport queue from which a NTB
1314 * payload will be transmitted. This assumes that a lock is behing held to
1315 * serialize access to the qp.
1316 *
1317 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1318 */
1319int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1320 unsigned int len)
1321{
1322 struct ntb_queue_entry *entry;
1323 int rc;
1324
1325 if (!qp || qp->qp_link != NTB_LINK_UP || !len)
1326 return -EINVAL;
1327
1328 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1329 if (!entry)
1330 return -ENOMEM;
1331
1332 entry->cb_data = cb;
1333 entry->buf = data;
1334 entry->len = len;
1335 entry->flags = 0;
1336
1337 rc = ntb_process_tx(qp, entry);
1338 if (rc)
1339 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1340 &qp->tx_free_q);
1341
1342 return rc;
1343}
1344EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1345
1346/**
1347 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1348 * @qp: NTB transport layer queue to be enabled
1349 *
1350 * Notify NTB transport layer of client readiness to use queue
1351 */
1352void ntb_transport_link_up(struct ntb_transport_qp *qp)
1353{
1354 if (!qp)
1355 return;
1356
1357 qp->client_ready = NTB_LINK_UP;
1358
1359 if (qp->transport->transport_link == NTB_LINK_UP)
1360 schedule_delayed_work(&qp->link_work, 0);
1361}
1362EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1363
1364/**
1365 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1366 * @qp: NTB transport layer queue to be disabled
1367 *
1368 * Notify NTB transport layer of client's desire to no longer receive data on
1369 * transport queue specified. It is the client's responsibility to ensure all
1370 * entries on queue are purged or otherwise handled appropraitely.
1371 */
1372void ntb_transport_link_down(struct ntb_transport_qp *qp)
1373{
1374 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1375 int rc, val;
1376
1377 if (!qp)
1378 return;
1379
1380 qp->client_ready = NTB_LINK_DOWN;
1381
1382 rc = ntb_read_local_spad(qp->ndev, QP_LINKS, &val);
1383 if (rc) {
1384 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
1385 return;
1386 }
1387
1388 rc = ntb_write_remote_spad(qp->ndev, QP_LINKS,
1389 val & ~(1 << qp->qp_num));
1390 if (rc)
1391 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
1392 val & ~(1 << qp->qp_num), QP_LINKS);
1393
1394 if (qp->qp_link == NTB_LINK_UP)
1395 ntb_send_link_down(qp);
1396 else
1397 cancel_delayed_work_sync(&qp->link_work);
1398}
1399EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1400
1401/**
1402 * ntb_transport_link_query - Query transport link state
1403 * @qp: NTB transport layer queue to be queried
1404 *
1405 * Query connectivity to the remote system of the NTB transport queue
1406 *
1407 * RETURNS: true for link up or false for link down
1408 */
1409bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1410{
1411 return qp->qp_link == NTB_LINK_UP;
1412}
1413EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1414
1415/**
1416 * ntb_transport_qp_num - Query the qp number
1417 * @qp: NTB transport layer queue to be queried
1418 *
1419 * Query qp number of the NTB transport queue
1420 *
1421 * RETURNS: a zero based number specifying the qp number
1422 */
1423unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1424{
1425 return qp->qp_num;
1426}
1427EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1428
1429/**
1430 * ntb_transport_max_size - Query the max payload size of a qp
1431 * @qp: NTB transport layer queue to be queried
1432 *
1433 * Query the maximum payload size permissible on the given qp
1434 *
1435 * RETURNS: the max payload size of a qp
1436 */
Jon Masonef114ed2013-01-19 02:02:18 -07001437unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001438{
Jon Masonef114ed2013-01-19 02:02:18 -07001439 return qp->tx_max_frame - sizeof(struct ntb_payload_header);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001440}
1441EXPORT_SYMBOL_GPL(ntb_transport_max_size);