blob: 5b6b00ea6ed9ef5fa2049ce49cdcada4a4930dba [file] [log] [blame]
Jon Masonfce8a7b2012-11-16 19:27:12 -07001/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -04008 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * BSD LICENSE
15 *
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -040017 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -070018 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
28 * distribution.
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
Allen Hubbee26a5842015-04-09 10:33:20 -040045 * PCIe NTB Transport Linux driver
Jon Masonfce8a7b2012-11-16 19:27:12 -070046 *
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
49 */
50#include <linux/debugfs.h>
51#include <linux/delay.h>
Jon Mason282a2fe2013-02-12 09:52:50 -070052#include <linux/dmaengine.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070053#include <linux/dma-mapping.h>
54#include <linux/errno.h>
55#include <linux/export.h>
56#include <linux/interrupt.h>
57#include <linux/module.h>
58#include <linux/pci.h>
59#include <linux/slab.h>
60#include <linux/types.h>
Dave Jiang06917f72015-05-19 16:45:46 -040061#include <linux/uaccess.h>
Allen Hubbee26a5842015-04-09 10:33:20 -040062#include "linux/ntb.h"
63#include "linux/ntb_transport.h"
Jon Masonfce8a7b2012-11-16 19:27:12 -070064
Allen Hubbee26a5842015-04-09 10:33:20 -040065#define NTB_TRANSPORT_VERSION 4
66#define NTB_TRANSPORT_VER "4"
67#define NTB_TRANSPORT_NAME "ntb_transport"
68#define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +053069#define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
Allen Hubbee26a5842015-04-09 10:33:20 -040070
71MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
72MODULE_VERSION(NTB_TRANSPORT_VER);
73MODULE_LICENSE("Dual BSD/GPL");
74MODULE_AUTHOR("Intel Corporation");
75
76static unsigned long max_mw_size;
77module_param(max_mw_size, ulong, 0644);
78MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
Jon Masonfce8a7b2012-11-16 19:27:12 -070079
Dave Jiang98914172015-06-03 11:29:38 -040080static unsigned int transport_mtu = 0x10000;
Jon Masonfce8a7b2012-11-16 19:27:12 -070081module_param(transport_mtu, uint, 0644);
82MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
83
Jon Mason948d3a62013-04-18 17:07:36 -070084static unsigned char max_num_clients;
Jon Masonfce8a7b2012-11-16 19:27:12 -070085module_param(max_num_clients, byte, 0644);
86MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
87
Jon Mason282a2fe2013-02-12 09:52:50 -070088static unsigned int copy_bytes = 1024;
89module_param(copy_bytes, uint, 0644);
90MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
91
Dave Jianga41ef052015-05-19 16:52:04 -040092static bool use_dma;
93module_param(use_dma, bool, 0644);
94MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
95
Allen Hubbee26a5842015-04-09 10:33:20 -040096static struct dentry *nt_debugfs_dir;
97
Jon Masonfce8a7b2012-11-16 19:27:12 -070098struct ntb_queue_entry {
99 /* ntb_queue list reference */
100 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400101 /* pointers to data to be transferred */
Jon Masonfce8a7b2012-11-16 19:27:12 -0700102 void *cb_data;
103 void *buf;
104 unsigned int len;
105 unsigned int flags;
Dave Jiang9cabc262016-07-20 13:14:07 -0700106 int retries;
107 int errors;
108 unsigned int tx_index;
Dave Jiang72203572016-07-20 13:14:13 -0700109 unsigned int rx_index;
Jon Mason282a2fe2013-02-12 09:52:50 -0700110
111 struct ntb_transport_qp *qp;
112 union {
113 struct ntb_payload_header __iomem *tx_hdr;
114 struct ntb_payload_header *rx_hdr;
115 };
Jon Masonfce8a7b2012-11-16 19:27:12 -0700116};
117
Jon Mason793c20e2013-01-19 02:02:26 -0700118struct ntb_rx_info {
119 unsigned int entry;
120};
121
Jon Masonfce8a7b2012-11-16 19:27:12 -0700122struct ntb_transport_qp {
Allen Hubbee26a5842015-04-09 10:33:20 -0400123 struct ntb_transport_ctx *transport;
124 struct ntb_dev *ndev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700125 void *cb_data;
Dave Jiang569410ca2015-07-13 08:07:22 -0400126 struct dma_chan *tx_dma_chan;
127 struct dma_chan *rx_dma_chan;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700128
129 bool client_ready;
Allen Hubbee26a5842015-04-09 10:33:20 -0400130 bool link_is_up;
Dave Jiange9021332016-02-23 09:11:36 -0700131 bool active;
Allen Hubbee26a5842015-04-09 10:33:20 -0400132
Jon Masonfce8a7b2012-11-16 19:27:12 -0700133 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400134 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700135
Jon Mason74465642013-01-21 15:28:52 -0700136 struct ntb_rx_info __iomem *rx_info;
Jon Mason793c20e2013-01-19 02:02:26 -0700137 struct ntb_rx_info *remote_rx_info;
138
Jon Mason53ca4fe2013-11-26 11:21:50 -0700139 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
140 void *data, int len);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700141 struct list_head tx_free_q;
142 spinlock_t ntb_tx_free_q_lock;
Jon Mason74465642013-01-21 15:28:52 -0700143 void __iomem *tx_mw;
Jon Mason282a2fe2013-02-12 09:52:50 -0700144 dma_addr_t tx_mw_phys;
Jon Mason793c20e2013-01-19 02:02:26 -0700145 unsigned int tx_index;
146 unsigned int tx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700147 unsigned int tx_max_frame;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700148
Jon Mason53ca4fe2013-11-26 11:21:50 -0700149 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
150 void *data, int len);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400151 struct list_head rx_post_q;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700152 struct list_head rx_pend_q;
153 struct list_head rx_free_q;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400154 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
155 spinlock_t ntb_rx_q_lock;
Jon Mason793c20e2013-01-19 02:02:26 -0700156 void *rx_buff;
157 unsigned int rx_index;
158 unsigned int rx_max_entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700159 unsigned int rx_max_frame;
Dave Jianga754a8f2016-04-08 10:49:06 -0700160 unsigned int rx_alloc_entry;
Jon Mason282a2fe2013-02-12 09:52:50 -0700161 dma_cookie_t last_cookie;
Allen Hubbee26a5842015-04-09 10:33:20 -0400162 struct tasklet_struct rxc_db_work;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700163
Jon Mason53ca4fe2013-11-26 11:21:50 -0700164 void (*event_handler)(void *data, int status);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700165 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700166 struct work_struct link_cleanup;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700167
168 struct dentry *debugfs_dir;
169 struct dentry *debugfs_stats;
170
171 /* Stats */
172 u64 rx_bytes;
173 u64 rx_pkts;
174 u64 rx_ring_empty;
175 u64 rx_err_no_buf;
176 u64 rx_err_oflow;
177 u64 rx_err_ver;
Jon Mason282a2fe2013-02-12 09:52:50 -0700178 u64 rx_memcpy;
179 u64 rx_async;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700180 u64 dma_rx_prep_err;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700181 u64 tx_bytes;
182 u64 tx_pkts;
183 u64 tx_ring_full;
Jon Mason282a2fe2013-02-12 09:52:50 -0700184 u64 tx_err_no_buf;
185 u64 tx_memcpy;
186 u64 tx_async;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700187 u64 dma_tx_prep_err;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700188};
189
190struct ntb_transport_mw {
Allen Hubbee26a5842015-04-09 10:33:20 -0400191 phys_addr_t phys_addr;
192 resource_size_t phys_size;
193 resource_size_t xlat_align;
194 resource_size_t xlat_align_size;
195 void __iomem *vbase;
196 size_t xlat_size;
197 size_t buff_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700198 void *virt_addr;
199 dma_addr_t dma_addr;
200};
201
202struct ntb_transport_client_dev {
203 struct list_head entry;
Allen Hubbee26a5842015-04-09 10:33:20 -0400204 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700205 struct device dev;
206};
207
Allen Hubbee26a5842015-04-09 10:33:20 -0400208struct ntb_transport_ctx {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700209 struct list_head entry;
210 struct list_head client_devs;
211
Allen Hubbee26a5842015-04-09 10:33:20 -0400212 struct ntb_dev *ndev;
213
214 struct ntb_transport_mw *mw_vec;
215 struct ntb_transport_qp *qp_vec;
216 unsigned int mw_count;
217 unsigned int qp_count;
218 u64 qp_bitmap;
219 u64 qp_bitmap_free;
220
221 bool link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700222 struct delayed_work link_work;
Jon Mason7b4f2d32013-01-19 02:02:19 -0700223 struct work_struct link_cleanup;
Dave Jiangc8650fd2015-07-13 08:07:09 -0400224
225 struct dentry *debugfs_node_dir;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700226};
227
228enum {
Allen Hubbee26a5842015-04-09 10:33:20 -0400229 DESC_DONE_FLAG = BIT(0),
230 LINK_DOWN_FLAG = BIT(1),
Jon Masonfce8a7b2012-11-16 19:27:12 -0700231};
232
233struct ntb_payload_header {
Jon Mason74465642013-01-21 15:28:52 -0700234 unsigned int ver;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700235 unsigned int len;
236 unsigned int flags;
237};
238
239enum {
240 VERSION = 0,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700241 QP_LINKS,
Jon Mason113fc502013-01-30 11:40:52 -0700242 NUM_QPS,
243 NUM_MWS,
244 MW0_SZ_HIGH,
245 MW0_SZ_LOW,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700246};
247
Allen Hubbee26a5842015-04-09 10:33:20 -0400248#define dev_client_dev(__dev) \
249 container_of((__dev), struct ntb_transport_client_dev, dev)
250
251#define drv_client(__drv) \
252 container_of((__drv), struct ntb_transport_client, driver)
253
254#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700255#define NTB_QP_DEF_NUM_ENTRIES 100
256#define NTB_LINK_DOWN_TIMEOUT 10
Dave Jiang8c874cc2016-01-08 09:51:05 -0700257#define DMA_RETRIES 20
Nicholas Mc Guirec0a88032016-08-22 18:51:35 +0200258#define DMA_OUT_RESOURCE_TO msecs_to_jiffies(50)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700259
Allen Hubbee26a5842015-04-09 10:33:20 -0400260static void ntb_transport_rxc_db(unsigned long data);
261static const struct ntb_ctx_ops ntb_transport_ops;
262static struct ntb_client ntb_transport_client;
Dave Jiang9cabc262016-07-20 13:14:07 -0700263static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
264 struct ntb_queue_entry *entry);
265static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
Dave Jiang72203572016-07-20 13:14:13 -0700266static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
267static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
268
Allen Hubbee26a5842015-04-09 10:33:20 -0400269
270static int ntb_transport_bus_match(struct device *dev,
271 struct device_driver *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700272{
273 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
274}
275
Allen Hubbee26a5842015-04-09 10:33:20 -0400276static int ntb_transport_bus_probe(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700277{
Allen Hubbee26a5842015-04-09 10:33:20 -0400278 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700279 int rc = -EINVAL;
280
281 get_device(dev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400282
283 client = drv_client(dev->driver);
284 rc = client->probe(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700285 if (rc)
286 put_device(dev);
287
288 return rc;
289}
290
Allen Hubbee26a5842015-04-09 10:33:20 -0400291static int ntb_transport_bus_remove(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700292{
Allen Hubbee26a5842015-04-09 10:33:20 -0400293 const struct ntb_transport_client *client;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700294
Allen Hubbee26a5842015-04-09 10:33:20 -0400295 client = drv_client(dev->driver);
296 client->remove(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700297
298 put_device(dev);
299
300 return 0;
301}
302
Allen Hubbee26a5842015-04-09 10:33:20 -0400303static struct bus_type ntb_transport_bus = {
304 .name = "ntb_transport",
305 .match = ntb_transport_bus_match,
306 .probe = ntb_transport_bus_probe,
307 .remove = ntb_transport_bus_remove,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700308};
309
310static LIST_HEAD(ntb_transport_list);
311
Allen Hubbee26a5842015-04-09 10:33:20 -0400312static int ntb_bus_init(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700313{
Dave Jiang31510002015-07-13 08:07:19 -0400314 list_add_tail(&nt->entry, &ntb_transport_list);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700315 return 0;
316}
317
Allen Hubbee26a5842015-04-09 10:33:20 -0400318static void ntb_bus_remove(struct ntb_transport_ctx *nt)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700319{
320 struct ntb_transport_client_dev *client_dev, *cd;
321
322 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
323 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
324 dev_name(&client_dev->dev));
325 list_del(&client_dev->entry);
326 device_unregister(&client_dev->dev);
327 }
328
329 list_del(&nt->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700330}
331
Allen Hubbee26a5842015-04-09 10:33:20 -0400332static void ntb_transport_client_release(struct device *dev)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700333{
334 struct ntb_transport_client_dev *client_dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700335
Allen Hubbee26a5842015-04-09 10:33:20 -0400336 client_dev = dev_client_dev(dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700337 kfree(client_dev);
338}
339
340/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400341 * ntb_transport_unregister_client_dev - Unregister NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700342 * @device_name: Name of NTB client device
343 *
344 * Unregister an NTB client device with the NTB transport layer
345 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400346void ntb_transport_unregister_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700347{
348 struct ntb_transport_client_dev *client, *cd;
Allen Hubbee26a5842015-04-09 10:33:20 -0400349 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700350
351 list_for_each_entry(nt, &ntb_transport_list, entry)
352 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
353 if (!strncmp(dev_name(&client->dev), device_name,
354 strlen(device_name))) {
355 list_del(&client->entry);
356 device_unregister(&client->dev);
357 }
358}
Allen Hubbee26a5842015-04-09 10:33:20 -0400359EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700360
361/**
Allen Hubbee26a5842015-04-09 10:33:20 -0400362 * ntb_transport_register_client_dev - Register NTB client device
Jon Masonfce8a7b2012-11-16 19:27:12 -0700363 * @device_name: Name of NTB client device
364 *
365 * Register an NTB client device with the NTB transport layer
366 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400367int ntb_transport_register_client_dev(char *device_name)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700368{
369 struct ntb_transport_client_dev *client_dev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400370 struct ntb_transport_ctx *nt;
Allen Hubbe1199aa62015-05-18 06:20:47 -0400371 int node;
Jon Mason8b19d452013-04-26 14:51:57 -0700372 int rc, i = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700373
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 list_for_each_entry(nt, &ntb_transport_list, entry) {
378 struct device *dev;
379
Allen Hubbe1199aa62015-05-18 06:20:47 -0400380 node = dev_to_node(&nt->ndev->dev);
381
382 client_dev = kzalloc_node(sizeof(*client_dev),
383 GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700384 if (!client_dev) {
385 rc = -ENOMEM;
386 goto err;
387 }
388
389 dev = &client_dev->dev;
390
391 /* setup and register client devices */
Jon Mason8b19d452013-04-26 14:51:57 -0700392 dev_set_name(dev, "%s%d", device_name, i);
Allen Hubbee26a5842015-04-09 10:33:20 -0400393 dev->bus = &ntb_transport_bus;
394 dev->release = ntb_transport_client_release;
395 dev->parent = &nt->ndev->dev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700396
397 rc = device_register(dev);
398 if (rc) {
399 kfree(client_dev);
400 goto err;
401 }
402
403 list_add_tail(&client_dev->entry, &nt->client_devs);
Jon Mason8b19d452013-04-26 14:51:57 -0700404 i++;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700405 }
406
407 return 0;
408
409err:
Allen Hubbee26a5842015-04-09 10:33:20 -0400410 ntb_transport_unregister_client_dev(device_name);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700411
412 return rc;
413}
Allen Hubbee26a5842015-04-09 10:33:20 -0400414EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700415
416/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400417 * ntb_transport_register_client - Register NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700418 * @drv: NTB client driver to be registered
419 *
420 * Register an NTB client driver with the NTB transport layer
421 *
422 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
423 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400424int ntb_transport_register_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700425{
Allen Hubbee26a5842015-04-09 10:33:20 -0400426 drv->driver.bus = &ntb_transport_bus;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700427
Jon Mason8222b402013-01-19 02:02:16 -0700428 if (list_empty(&ntb_transport_list))
429 return -ENODEV;
430
Jon Masonfce8a7b2012-11-16 19:27:12 -0700431 return driver_register(&drv->driver);
432}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400433EXPORT_SYMBOL_GPL(ntb_transport_register_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700434
435/**
Allen Hubbeec110bc2015-05-07 06:45:21 -0400436 * ntb_transport_unregister_client - Unregister NTB client driver
Jon Masonfce8a7b2012-11-16 19:27:12 -0700437 * @drv: NTB client driver to be unregistered
438 *
439 * Unregister an NTB client driver with the NTB transport layer
440 *
441 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
442 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400443void ntb_transport_unregister_client(struct ntb_transport_client *drv)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700444{
445 driver_unregister(&drv->driver);
446}
Allen Hubbeec110bc2015-05-07 06:45:21 -0400447EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700448
Jon Masonfce8a7b2012-11-16 19:27:12 -0700449static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
450 loff_t *offp)
451{
452 struct ntb_transport_qp *qp;
Jon Masond7237e22013-01-19 02:02:25 -0700453 char *buf;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700454 ssize_t ret, out_offset, out_count;
455
Dave Jiang260bee92015-07-13 08:07:11 -0400456 qp = filp->private_data;
457
458 if (!qp || !qp->link_is_up)
459 return 0;
460
Jon Mason282a2fe2013-02-12 09:52:50 -0700461 out_count = 1000;
Jon Masond7237e22013-01-19 02:02:25 -0700462
463 buf = kmalloc(out_count, GFP_KERNEL);
464 if (!buf)
465 return -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700466
Jon Masonfce8a7b2012-11-16 19:27:12 -0700467 out_offset = 0;
468 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400469 "\nNTB QP stats:\n\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700470 out_offset += snprintf(buf + out_offset, out_count - out_offset,
471 "rx_bytes - \t%llu\n", qp->rx_bytes);
472 out_offset += snprintf(buf + out_offset, out_count - out_offset,
473 "rx_pkts - \t%llu\n", qp->rx_pkts);
474 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700475 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
476 out_offset += snprintf(buf + out_offset, out_count - out_offset,
477 "rx_async - \t%llu\n", qp->rx_async);
478 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700479 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
480 out_offset += snprintf(buf + out_offset, out_count - out_offset,
481 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
482 out_offset += snprintf(buf + out_offset, out_count - out_offset,
483 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
484 out_offset += snprintf(buf + out_offset, out_count - out_offset,
485 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
486 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400487 "rx_buff - \t0x%p\n", qp->rx_buff);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700488 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700489 "rx_index - \t%u\n", qp->rx_index);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700490 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jianga754a8f2016-04-08 10:49:06 -0700491 "rx_max_entry - \t%u\n", qp->rx_max_entry);
492 out_offset += snprintf(buf + out_offset, out_count - out_offset,
493 "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700494
495 out_offset += snprintf(buf + out_offset, out_count - out_offset,
496 "tx_bytes - \t%llu\n", qp->tx_bytes);
497 out_offset += snprintf(buf + out_offset, out_count - out_offset,
498 "tx_pkts - \t%llu\n", qp->tx_pkts);
499 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700500 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
501 out_offset += snprintf(buf + out_offset, out_count - out_offset,
502 "tx_async - \t%llu\n", qp->tx_async);
503 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700504 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
505 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason282a2fe2013-02-12 09:52:50 -0700506 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
507 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400508 "tx_mw - \t0x%p\n", qp->tx_mw);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700509 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400510 "tx_index (H) - \t%u\n", qp->tx_index);
511 out_offset += snprintf(buf + out_offset, out_count - out_offset,
512 "RRI (T) - \t%u\n",
513 qp->remote_rx_info->entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700514 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Jon Mason793c20e2013-01-19 02:02:26 -0700515 "tx_max_entry - \t%u\n", qp->tx_max_entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400516 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiange74bfee2015-07-13 08:07:17 -0400517 "free tx - \t%u\n",
518 ntb_transport_tx_free_entry(qp));
Dave Jiang8c874cc2016-01-08 09:51:05 -0700519 out_offset += snprintf(buf + out_offset, out_count - out_offset,
520 "DMA tx prep err - \t%llu\n",
521 qp->dma_tx_prep_err);
522 out_offset += snprintf(buf + out_offset, out_count - out_offset,
523 "DMA rx prep err - \t%llu\n",
524 qp->dma_rx_prep_err);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700525
526 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiangd98ef992015-07-13 08:07:20 -0400527 "\n");
528 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Dave Jiang569410ca2015-07-13 08:07:22 -0400529 "Using TX DMA - \t%s\n",
530 qp->tx_dma_chan ? "Yes" : "No");
531 out_offset += snprintf(buf + out_offset, out_count - out_offset,
532 "Using RX DMA - \t%s\n",
533 qp->rx_dma_chan ? "Yes" : "No");
Dave Jiangd98ef992015-07-13 08:07:20 -0400534 out_offset += snprintf(buf + out_offset, out_count - out_offset,
535 "QP Link - \t%s\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400536 qp->link_is_up ? "Up" : "Down");
Dave Jiangd98ef992015-07-13 08:07:20 -0400537 out_offset += snprintf(buf + out_offset, out_count - out_offset,
538 "\n");
539
Jon Masond7237e22013-01-19 02:02:25 -0700540 if (out_offset > out_count)
541 out_offset = out_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700542
543 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
Jon Masond7237e22013-01-19 02:02:25 -0700544 kfree(buf);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700545 return ret;
546}
547
548static const struct file_operations ntb_qp_debugfs_stats = {
549 .owner = THIS_MODULE,
Jon Masond66d7ac2013-01-19 02:02:20 -0700550 .open = simple_open,
Jon Masonfce8a7b2012-11-16 19:27:12 -0700551 .read = debugfs_read,
552};
553
554static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
555 struct list_head *list)
556{
557 unsigned long flags;
558
559 spin_lock_irqsave(lock, flags);
560 list_add_tail(entry, list);
561 spin_unlock_irqrestore(lock, flags);
562}
563
564static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700565 struct list_head *list)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700566{
567 struct ntb_queue_entry *entry;
568 unsigned long flags;
569
570 spin_lock_irqsave(lock, flags);
571 if (list_empty(list)) {
572 entry = NULL;
573 goto out;
574 }
575 entry = list_first_entry(list, struct ntb_queue_entry, entry);
576 list_del(&entry->entry);
Dave Jiange74bfee2015-07-13 08:07:17 -0400577
Jon Masonfce8a7b2012-11-16 19:27:12 -0700578out:
579 spin_unlock_irqrestore(lock, flags);
580
581 return entry;
582}
583
Allen Hubbeda2e5ae2015-07-13 08:07:08 -0400584static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
585 struct list_head *list,
586 struct list_head *to_list)
587{
588 struct ntb_queue_entry *entry;
589 unsigned long flags;
590
591 spin_lock_irqsave(lock, flags);
592
593 if (list_empty(list)) {
594 entry = NULL;
595 } else {
596 entry = list_first_entry(list, struct ntb_queue_entry, entry);
597 list_move_tail(&entry->entry, to_list);
598 }
599
600 spin_unlock_irqrestore(lock, flags);
601
602 return entry;
603}
604
Allen Hubbee26a5842015-04-09 10:33:20 -0400605static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
606 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700607{
Allen Hubbee26a5842015-04-09 10:33:20 -0400608 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
609 struct ntb_transport_mw *mw;
Dave Jianga754a8f2016-04-08 10:49:06 -0700610 struct ntb_dev *ndev = nt->ndev;
611 struct ntb_queue_entry *entry;
Jon Masonef114ed2013-01-19 02:02:18 -0700612 unsigned int rx_size, num_qps_mw;
Allen Hubbee26a5842015-04-09 10:33:20 -0400613 unsigned int mw_num, mw_count, qp_count;
Jon Mason793c20e2013-01-19 02:02:26 -0700614 unsigned int i;
Dave Jianga754a8f2016-04-08 10:49:06 -0700615 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700616
Allen Hubbee26a5842015-04-09 10:33:20 -0400617 mw_count = nt->mw_count;
618 qp_count = nt->qp_count;
Jon Mason948d3a62013-04-18 17:07:36 -0700619
Allen Hubbee26a5842015-04-09 10:33:20 -0400620 mw_num = QP_TO_MW(nt, qp_num);
621 mw = &nt->mw_vec[mw_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700622
Allen Hubbee26a5842015-04-09 10:33:20 -0400623 if (!mw->virt_addr)
624 return -ENOMEM;
625
Logan Gunthorpe8e8496e2017-06-05 14:00:53 -0600626 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -0400627 num_qps_mw = qp_count / mw_count + 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700628 else
Allen Hubbee26a5842015-04-09 10:33:20 -0400629 num_qps_mw = qp_count / mw_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700630
Allen Hubbee26a5842015-04-09 10:33:20 -0400631 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -0400632 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
Jon Mason793c20e2013-01-19 02:02:26 -0700633 rx_size -= sizeof(struct ntb_rx_info);
634
Jon Mason282a2fe2013-02-12 09:52:50 -0700635 qp->remote_rx_info = qp->rx_buff + rx_size;
636
Jon Masonc9d534c2013-02-01 15:45:16 -0700637 /* Due to housekeeping, there must be atleast 2 buffs */
638 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -0700639 qp->rx_max_entry = rx_size / qp->rx_max_frame;
640 qp->rx_index = 0;
641
Dave Jianga754a8f2016-04-08 10:49:06 -0700642 /*
643 * Checking to see if we have more entries than the default.
644 * We should add additional entries if that is the case so we
645 * can be in sync with the transport frames.
646 */
647 node = dev_to_node(&ndev->dev);
648 for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
649 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
650 if (!entry)
651 return -ENOMEM;
652
653 entry->qp = qp;
654 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
655 &qp->rx_free_q);
656 qp->rx_alloc_entry++;
657 }
658
Jon Masonc9d534c2013-02-01 15:45:16 -0700659 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700660
Jon Masonef114ed2013-01-19 02:02:18 -0700661 /* setup the hdr offsets with 0's */
Jon Mason793c20e2013-01-19 02:02:26 -0700662 for (i = 0; i < qp->rx_max_entry; i++) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400663 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
664 sizeof(struct ntb_payload_header));
Jon Masonef114ed2013-01-19 02:02:18 -0700665 memset(offset, 0, sizeof(struct ntb_payload_header));
Jon Mason793c20e2013-01-19 02:02:26 -0700666 }
Jon Masonfce8a7b2012-11-16 19:27:12 -0700667
668 qp->rx_pkts = 0;
669 qp->tx_pkts = 0;
Jon Mason90f9e932013-02-01 15:34:35 -0700670 qp->tx_index = 0;
Allen Hubbee26a5842015-04-09 10:33:20 -0400671
672 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700673}
674
Allen Hubbee26a5842015-04-09 10:33:20 -0400675static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
Jon Mason113fc502013-01-30 11:40:52 -0700676{
Allen Hubbee26a5842015-04-09 10:33:20 -0400677 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
678 struct pci_dev *pdev = nt->ndev->pdev;
Jon Mason113fc502013-01-30 11:40:52 -0700679
680 if (!mw->virt_addr)
681 return;
682
Allen Hubbee26a5842015-04-09 10:33:20 -0400683 ntb_mw_clear_trans(nt->ndev, num_mw);
684 dma_free_coherent(&pdev->dev, mw->buff_size,
685 mw->virt_addr, mw->dma_addr);
686 mw->xlat_size = 0;
687 mw->buff_size = 0;
Jon Mason113fc502013-01-30 11:40:52 -0700688 mw->virt_addr = NULL;
689}
690
Allen Hubbee26a5842015-04-09 10:33:20 -0400691static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400692 resource_size_t size)
Jon Masonb77b2632013-02-01 15:25:37 -0700693{
Allen Hubbee26a5842015-04-09 10:33:20 -0400694 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
695 struct pci_dev *pdev = nt->ndev->pdev;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400696 size_t xlat_size, buff_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400697 int rc;
698
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400699 if (!size)
700 return -EINVAL;
701
Allen Hubbee26a5842015-04-09 10:33:20 -0400702 xlat_size = round_up(size, mw->xlat_align_size);
703 buff_size = round_up(size, mw->xlat_align);
Jon Masonb77b2632013-02-01 15:25:37 -0700704
705 /* No need to re-setup */
Allen Hubbee26a5842015-04-09 10:33:20 -0400706 if (mw->xlat_size == xlat_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700707 return 0;
708
Allen Hubbee26a5842015-04-09 10:33:20 -0400709 if (mw->buff_size)
Jon Masonb77b2632013-02-01 15:25:37 -0700710 ntb_free_mw(nt, num_mw);
711
Allen Hubbee26a5842015-04-09 10:33:20 -0400712 /* Alloc memory for receiving data. Must be aligned */
713 mw->xlat_size = xlat_size;
714 mw->buff_size = buff_size;
Jon Masonb77b2632013-02-01 15:25:37 -0700715
Allen Hubbee26a5842015-04-09 10:33:20 -0400716 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
717 &mw->dma_addr, GFP_KERNEL);
Jon Masonb77b2632013-02-01 15:25:37 -0700718 if (!mw->virt_addr) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400719 mw->xlat_size = 0;
720 mw->buff_size = 0;
Allen Hubbe8c9edf62015-07-13 08:07:13 -0400721 dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
Allen Hubbee26a5842015-04-09 10:33:20 -0400722 buff_size);
Jon Masonb77b2632013-02-01 15:25:37 -0700723 return -ENOMEM;
724 }
725
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700726 /*
727 * we must ensure that the memory address allocated is BAR size
728 * aligned in order for the XLAT register to take the value. This
729 * is a requirement of the hardware. It is recommended to setup CMA
730 * for BAR sizes equal or greater than 4MB.
731 */
Allen Hubbee26a5842015-04-09 10:33:20 -0400732 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
733 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
Dave Jiang3cc5ba12014-08-28 13:53:02 -0700734 &mw->dma_addr);
735 ntb_free_mw(nt, num_mw);
736 return -ENOMEM;
737 }
738
Jon Masonb77b2632013-02-01 15:25:37 -0700739 /* Notify HW the memory location of the receive buffer */
Allen Hubbee26a5842015-04-09 10:33:20 -0400740 rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
741 if (rc) {
742 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
743 ntb_free_mw(nt, num_mw);
744 return -EIO;
745 }
Jon Masonb77b2632013-02-01 15:25:37 -0700746
747 return 0;
748}
749
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400750static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
751{
752 qp->link_is_up = false;
Dave Jiange9021332016-02-23 09:11:36 -0700753 qp->active = false;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400754
755 qp->tx_index = 0;
756 qp->rx_index = 0;
757 qp->rx_bytes = 0;
758 qp->rx_pkts = 0;
759 qp->rx_ring_empty = 0;
760 qp->rx_err_no_buf = 0;
761 qp->rx_err_oflow = 0;
762 qp->rx_err_ver = 0;
763 qp->rx_memcpy = 0;
764 qp->rx_async = 0;
765 qp->tx_bytes = 0;
766 qp->tx_pkts = 0;
767 qp->tx_ring_full = 0;
768 qp->tx_err_no_buf = 0;
769 qp->tx_memcpy = 0;
770 qp->tx_async = 0;
Dave Jiang8c874cc2016-01-08 09:51:05 -0700771 qp->dma_tx_prep_err = 0;
772 qp->dma_rx_prep_err = 0;
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400773}
774
Jon Masonfca4d512013-09-09 13:39:55 -0700775static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700776{
Allen Hubbee26a5842015-04-09 10:33:20 -0400777 struct ntb_transport_ctx *nt = qp->transport;
778 struct pci_dev *pdev = nt->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700779
Allen Hubbee22e0b92015-05-12 06:55:44 -0400780 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -0400781
782 cancel_delayed_work_sync(&qp->link_work);
783 ntb_qp_link_down_reset(qp);
Allen Hubbee26a5842015-04-09 10:33:20 -0400784
785 if (qp->event_handler)
786 qp->event_handler(qp->cb_data, qp->link_is_up);
Jon Masonfca4d512013-09-09 13:39:55 -0700787}
788
789static void ntb_qp_link_cleanup_work(struct work_struct *work)
790{
791 struct ntb_transport_qp *qp = container_of(work,
792 struct ntb_transport_qp,
793 link_cleanup);
Allen Hubbee26a5842015-04-09 10:33:20 -0400794 struct ntb_transport_ctx *nt = qp->transport;
Jon Masonfca4d512013-09-09 13:39:55 -0700795
796 ntb_qp_link_cleanup(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700797
Allen Hubbee26a5842015-04-09 10:33:20 -0400798 if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700799 schedule_delayed_work(&qp->link_work,
800 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
801}
802
Jon Mason7b4f2d32013-01-19 02:02:19 -0700803static void ntb_qp_link_down(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700804{
Jon Mason7b4f2d32013-01-19 02:02:19 -0700805 schedule_work(&qp->link_cleanup);
806}
807
Allen Hubbee26a5842015-04-09 10:33:20 -0400808static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
Jon Mason7b4f2d32013-01-19 02:02:19 -0700809{
Allen Hubbee26a5842015-04-09 10:33:20 -0400810 struct ntb_transport_qp *qp;
811 u64 qp_bitmap_alloc;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +0530812 unsigned int i, count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700813
Allen Hubbee26a5842015-04-09 10:33:20 -0400814 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfca4d512013-09-09 13:39:55 -0700815
Allen Hubbee26a5842015-04-09 10:33:20 -0400816 /* Pass along the info to any clients */
817 for (i = 0; i < nt->qp_count; i++)
818 if (qp_bitmap_alloc & BIT_ULL(i)) {
819 qp = &nt->qp_vec[i];
820 ntb_qp_link_cleanup(qp);
821 cancel_work_sync(&qp->link_cleanup);
822 cancel_delayed_work_sync(&qp->link_work);
823 }
824
825 if (!nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700826 cancel_delayed_work_sync(&nt->link_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700827
Jon Masonfce8a7b2012-11-16 19:27:12 -0700828 /* The scratchpad registers keep the values if the remote side
829 * goes down, blast them now to give them a sane value the next
830 * time they are accessed
831 */
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +0530832 count = ntb_spad_count(nt->ndev);
833 for (i = 0; i < count; i++)
Allen Hubbee26a5842015-04-09 10:33:20 -0400834 ntb_spad_write(nt->ndev, i, 0);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700835}
836
Jon Masonfca4d512013-09-09 13:39:55 -0700837static void ntb_transport_link_cleanup_work(struct work_struct *work)
838{
Allen Hubbee26a5842015-04-09 10:33:20 -0400839 struct ntb_transport_ctx *nt =
840 container_of(work, struct ntb_transport_ctx, link_cleanup);
Jon Masonfca4d512013-09-09 13:39:55 -0700841
842 ntb_transport_link_cleanup(nt);
843}
844
Allen Hubbee26a5842015-04-09 10:33:20 -0400845static void ntb_transport_event_callback(void *data)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700846{
Allen Hubbee26a5842015-04-09 10:33:20 -0400847 struct ntb_transport_ctx *nt = data;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700848
Allen Hubbee26a5842015-04-09 10:33:20 -0400849 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700850 schedule_delayed_work(&nt->link_work, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -0400851 else
Jon Mason7b4f2d32013-01-19 02:02:19 -0700852 schedule_work(&nt->link_cleanup);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700853}
854
855static void ntb_transport_link_work(struct work_struct *work)
856{
Allen Hubbee26a5842015-04-09 10:33:20 -0400857 struct ntb_transport_ctx *nt =
858 container_of(work, struct ntb_transport_ctx, link_work.work);
859 struct ntb_dev *ndev = nt->ndev;
860 struct pci_dev *pdev = ndev->pdev;
861 resource_size_t size;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700862 u32 val;
Dave Jiang84f76682016-02-29 09:35:26 -0700863 int rc = 0, i, spad;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700864
Jon Mason113fc502013-01-30 11:40:52 -0700865 /* send the local info, in the opposite order of the way we read it */
Allen Hubbee26a5842015-04-09 10:33:20 -0400866 for (i = 0; i < nt->mw_count; i++) {
867 size = nt->mw_vec[i].phys_size;
Jon Mason113fc502013-01-30 11:40:52 -0700868
Allen Hubbee26a5842015-04-09 10:33:20 -0400869 if (max_mw_size && size > max_mw_size)
870 size = max_mw_size;
871
872 spad = MW0_SZ_HIGH + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200873 ntb_peer_spad_write(ndev, spad, upper_32_bits(size));
Allen Hubbee26a5842015-04-09 10:33:20 -0400874
875 spad = MW0_SZ_LOW + (i * 2);
Arnd Bergmannfdcb4b22015-10-07 09:59:34 +0200876 ntb_peer_spad_write(ndev, spad, lower_32_bits(size));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700877 }
878
Allen Hubbee26a5842015-04-09 10:33:20 -0400879 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700880
Allen Hubbee26a5842015-04-09 10:33:20 -0400881 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700882
Allen Hubbee26a5842015-04-09 10:33:20 -0400883 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700884
885 /* Query the remote side for its info */
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400886 val = ntb_spad_read(ndev, VERSION);
Allen Hubbee26a5842015-04-09 10:33:20 -0400887 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700888 if (val != NTB_TRANSPORT_VERSION)
889 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700890
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400891 val = ntb_spad_read(ndev, NUM_QPS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700892 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400893 if (val != nt->qp_count)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700894 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700895
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400896 val = ntb_spad_read(ndev, NUM_MWS);
Jon Mason113fc502013-01-30 11:40:52 -0700897 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
Allen Hubbee26a5842015-04-09 10:33:20 -0400898 if (val != nt->mw_count)
899 goto out;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700900
Allen Hubbee26a5842015-04-09 10:33:20 -0400901 for (i = 0; i < nt->mw_count; i++) {
Jon Mason113fc502013-01-30 11:40:52 -0700902 u64 val64;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700903
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400904 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
Allen Hubbee26a5842015-04-09 10:33:20 -0400905 val64 = (u64)val << 32;
Jon Mason113fc502013-01-30 11:40:52 -0700906
Dave Jiang0f69a7d2015-06-02 03:45:07 -0400907 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
Jon Mason113fc502013-01-30 11:40:52 -0700908 val64 |= val;
909
Allen Hubbee26a5842015-04-09 10:33:20 -0400910 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
Jon Mason113fc502013-01-30 11:40:52 -0700911
912 rc = ntb_set_mw(nt, i, val64);
913 if (rc)
914 goto out1;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700915 }
916
Allen Hubbee26a5842015-04-09 10:33:20 -0400917 nt->link_is_up = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700918
Allen Hubbee26a5842015-04-09 10:33:20 -0400919 for (i = 0; i < nt->qp_count; i++) {
920 struct ntb_transport_qp *qp = &nt->qp_vec[i];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700921
922 ntb_transport_setup_qp_mw(nt, i);
923
Allen Hubbee26a5842015-04-09 10:33:20 -0400924 if (qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700925 schedule_delayed_work(&qp->link_work, 0);
926 }
927
928 return;
929
Jon Mason113fc502013-01-30 11:40:52 -0700930out1:
Allen Hubbee26a5842015-04-09 10:33:20 -0400931 for (i = 0; i < nt->mw_count; i++)
Jon Mason113fc502013-01-30 11:40:52 -0700932 ntb_free_mw(nt, i);
Dave Jiang84f76682016-02-29 09:35:26 -0700933
934 /* if there's an actual failure, we should just bail */
935 if (rc < 0) {
936 ntb_link_disable(ndev);
937 return;
938 }
939
Jon Masonfce8a7b2012-11-16 19:27:12 -0700940out:
Allen Hubbee26a5842015-04-09 10:33:20 -0400941 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700942 schedule_delayed_work(&nt->link_work,
943 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
944}
945
946static void ntb_qp_link_work(struct work_struct *work)
947{
948 struct ntb_transport_qp *qp = container_of(work,
949 struct ntb_transport_qp,
950 link_work.work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400951 struct pci_dev *pdev = qp->ndev->pdev;
952 struct ntb_transport_ctx *nt = qp->transport;
953 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700954
Allen Hubbee26a5842015-04-09 10:33:20 -0400955 WARN_ON(!nt->link_is_up);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700956
Allen Hubbee26a5842015-04-09 10:33:20 -0400957 val = ntb_spad_read(nt->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700958
Allen Hubbee26a5842015-04-09 10:33:20 -0400959 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -0700960
961 /* query remote spad for qp ready bits */
Allen Hubbe28762282015-05-11 10:08:26 -0400962 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
Jon Masonfce8a7b2012-11-16 19:27:12 -0700963
964 /* See if the remote side is up */
Allen Hubbee26a5842015-04-09 10:33:20 -0400965 if (val & BIT(qp->qp_num)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -0700966 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400967 qp->link_is_up = true;
Dave Jiange9021332016-02-23 09:11:36 -0700968 qp->active = true;
Allen Hubbee26a5842015-04-09 10:33:20 -0400969
Jon Masonfce8a7b2012-11-16 19:27:12 -0700970 if (qp->event_handler)
Allen Hubbee26a5842015-04-09 10:33:20 -0400971 qp->event_handler(qp->cb_data, qp->link_is_up);
Allen Hubbe8b5a22d2015-07-13 08:07:12 -0400972
Dave Jiange9021332016-02-23 09:11:36 -0700973 if (qp->active)
974 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -0400975 } else if (nt->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700976 schedule_delayed_work(&qp->link_work,
977 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
978}
979
Allen Hubbee26a5842015-04-09 10:33:20 -0400980static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
Jon Mason53ca4fe2013-11-26 11:21:50 -0700981 unsigned int qp_num)
Jon Masonfce8a7b2012-11-16 19:27:12 -0700982{
983 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -0400984 phys_addr_t mw_base;
985 resource_size_t mw_size;
Jon Masonef114ed2013-01-19 02:02:18 -0700986 unsigned int num_qps_mw, tx_size;
Allen Hubbee26a5842015-04-09 10:33:20 -0400987 unsigned int mw_num, mw_count, qp_count;
Jon Mason282a2fe2013-02-12 09:52:50 -0700988 u64 qp_offset;
Jon Mason948d3a62013-04-18 17:07:36 -0700989
Allen Hubbee26a5842015-04-09 10:33:20 -0400990 mw_count = nt->mw_count;
991 qp_count = nt->qp_count;
Jon Masonfce8a7b2012-11-16 19:27:12 -0700992
Allen Hubbee26a5842015-04-09 10:33:20 -0400993 mw_num = QP_TO_MW(nt, qp_num);
Allen Hubbee26a5842015-04-09 10:33:20 -0400994
995 qp = &nt->qp_vec[qp_num];
Jon Masonfce8a7b2012-11-16 19:27:12 -0700996 qp->qp_num = qp_num;
997 qp->transport = nt;
998 qp->ndev = nt->ndev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400999 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001000 qp->event_handler = NULL;
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001001 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001002
Logan Gunthorpe8e8496e2017-06-05 14:00:53 -06001003 if (mw_num < qp_count % mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -04001004 num_qps_mw = qp_count / mw_count + 1;
Jon Masonef114ed2013-01-19 02:02:18 -07001005 else
Allen Hubbee26a5842015-04-09 10:33:20 -04001006 num_qps_mw = qp_count / mw_count;
Jon Masonef114ed2013-01-19 02:02:18 -07001007
Allen Hubbee26a5842015-04-09 10:33:20 -04001008 mw_base = nt->mw_vec[mw_num].phys_addr;
1009 mw_size = nt->mw_vec[mw_num].phys_size;
1010
1011 tx_size = (unsigned int)mw_size / num_qps_mw;
Jon Masonc92ba3c2015-10-04 22:54:22 -04001012 qp_offset = tx_size * (qp_num / mw_count);
Allen Hubbee26a5842015-04-09 10:33:20 -04001013
1014 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001015 if (!qp->tx_mw)
1016 return -EINVAL;
Jon Mason793c20e2013-01-19 02:02:26 -07001017
Allen Hubbee26a5842015-04-09 10:33:20 -04001018 qp->tx_mw_phys = mw_base + qp_offset;
Jon Mason282a2fe2013-02-12 09:52:50 -07001019 if (!qp->tx_mw_phys)
1020 return -EINVAL;
1021
1022 tx_size -= sizeof(struct ntb_rx_info);
1023 qp->rx_info = qp->tx_mw + tx_size;
1024
Jon Masonc9d534c2013-02-01 15:45:16 -07001025 /* Due to housekeeping, there must be atleast 2 buffs */
1026 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
Jon Mason793c20e2013-01-19 02:02:26 -07001027 qp->tx_max_entry = tx_size / qp->tx_max_frame;
Jon Masonef114ed2013-01-19 02:02:18 -07001028
Dave Jiangc8650fd2015-07-13 08:07:09 -04001029 if (nt->debugfs_node_dir) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001030 char debugfs_name[4];
1031
1032 snprintf(debugfs_name, 4, "qp%d", qp_num);
1033 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
Dave Jiangc8650fd2015-07-13 08:07:09 -04001034 nt->debugfs_node_dir);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001035
1036 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1037 qp->debugfs_dir, qp,
1038 &ntb_qp_debugfs_stats);
Allen Hubbee26a5842015-04-09 10:33:20 -04001039 } else {
1040 qp->debugfs_dir = NULL;
1041 qp->debugfs_stats = NULL;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001042 }
1043
1044 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001045 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001046
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001047 spin_lock_init(&qp->ntb_rx_q_lock);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001048 spin_lock_init(&qp->ntb_tx_free_q_lock);
1049
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001050 INIT_LIST_HEAD(&qp->rx_post_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001051 INIT_LIST_HEAD(&qp->rx_pend_q);
1052 INIT_LIST_HEAD(&qp->rx_free_q);
1053 INIT_LIST_HEAD(&qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07001054
Allen Hubbee26a5842015-04-09 10:33:20 -04001055 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1056 (unsigned long)qp);
1057
Jon Mason282a2fe2013-02-12 09:52:50 -07001058 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001059}
1060
Allen Hubbee26a5842015-04-09 10:33:20 -04001061static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001062{
Allen Hubbee26a5842015-04-09 10:33:20 -04001063 struct ntb_transport_ctx *nt;
1064 struct ntb_transport_mw *mw;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301065 unsigned int mw_count, qp_count, spad_count, max_mw_count_for_spads;
Allen Hubbee26a5842015-04-09 10:33:20 -04001066 u64 qp_bitmap;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001067 int node;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001068 int rc, i;
1069
Logan Gunthorpe19645a02016-06-07 11:20:22 -06001070 mw_count = ntb_mw_count(ndev);
Logan Gunthorpe19645a02016-06-07 11:20:22 -06001071
Allen Hubbee26a5842015-04-09 10:33:20 -04001072 if (ntb_db_is_unsafe(ndev))
1073 dev_dbg(&ndev->dev,
1074 "doorbell is unsafe, proceed anyway...\n");
1075 if (ntb_spad_is_unsafe(ndev))
1076 dev_dbg(&ndev->dev,
1077 "scratchpad is unsafe, proceed anyway...\n");
1078
Allen Hubbe1199aa62015-05-18 06:20:47 -04001079 node = dev_to_node(&ndev->dev);
1080
1081 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001082 if (!nt)
1083 return -ENOMEM;
1084
Allen Hubbee26a5842015-04-09 10:33:20 -04001085 nt->ndev = ndev;
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301086 spad_count = ntb_spad_count(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04001087
Shyam Sundar S Kb17faba2016-12-07 22:37:05 +05301088 /* Limit the MW's based on the availability of scratchpads */
1089
1090 if (spad_count < NTB_TRANSPORT_MIN_SPADS) {
1091 nt->mw_count = 0;
1092 rc = -EINVAL;
1093 goto err;
1094 }
1095
1096 max_mw_count_for_spads = (spad_count - MW0_SZ_HIGH) / 2;
1097 nt->mw_count = min(mw_count, max_mw_count_for_spads);
Allen Hubbee26a5842015-04-09 10:33:20 -04001098
Allen Hubbe1199aa62015-05-18 06:20:47 -04001099 nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1100 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001101 if (!nt->mw_vec) {
1102 rc = -ENOMEM;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001103 goto err;
1104 }
1105
Allen Hubbee26a5842015-04-09 10:33:20 -04001106 for (i = 0; i < mw_count; i++) {
1107 mw = &nt->mw_vec[i];
1108
1109 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
1110 &mw->xlat_align, &mw->xlat_align_size);
1111 if (rc)
1112 goto err1;
1113
Dave Jiang06917f72015-05-19 16:45:46 -04001114 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04001115 if (!mw->vbase) {
1116 rc = -ENOMEM;
1117 goto err1;
1118 }
1119
1120 mw->buff_size = 0;
1121 mw->xlat_size = 0;
1122 mw->virt_addr = NULL;
1123 mw->dma_addr = 0;
Jon Mason948d3a62013-04-18 17:07:36 -07001124 }
1125
Allen Hubbee26a5842015-04-09 10:33:20 -04001126 qp_bitmap = ntb_db_valid_mask(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001127
Allen Hubbee26a5842015-04-09 10:33:20 -04001128 qp_count = ilog2(qp_bitmap);
1129 if (max_num_clients && max_num_clients < qp_count)
1130 qp_count = max_num_clients;
Logan Gunthorpecb827ee2017-06-05 14:00:52 -06001131 else if (nt->mw_count < qp_count)
1132 qp_count = nt->mw_count;
Allen Hubbee26a5842015-04-09 10:33:20 -04001133
1134 qp_bitmap &= BIT_ULL(qp_count) - 1;
1135
1136 nt->qp_count = qp_count;
1137 nt->qp_bitmap = qp_bitmap;
1138 nt->qp_bitmap_free = qp_bitmap;
1139
Allen Hubbe1199aa62015-05-18 06:20:47 -04001140 nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1141 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04001142 if (!nt->qp_vec) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001143 rc = -ENOMEM;
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301144 goto err1;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001145 }
1146
Dave Jiangc8650fd2015-07-13 08:07:09 -04001147 if (nt_debugfs_dir) {
1148 nt->debugfs_node_dir =
1149 debugfs_create_dir(pci_name(ndev->pdev),
1150 nt_debugfs_dir);
1151 }
1152
Allen Hubbee26a5842015-04-09 10:33:20 -04001153 for (i = 0; i < qp_count; i++) {
Jon Mason282a2fe2013-02-12 09:52:50 -07001154 rc = ntb_transport_init_queue(nt, i);
1155 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301156 goto err2;
Jon Mason282a2fe2013-02-12 09:52:50 -07001157 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001158
1159 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
Jon Masonfca4d512013-09-09 13:39:55 -07001160 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001161
Allen Hubbee26a5842015-04-09 10:33:20 -04001162 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001163 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301164 goto err2;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001165
1166 INIT_LIST_HEAD(&nt->client_devs);
1167 rc = ntb_bus_init(nt);
1168 if (rc)
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301169 goto err3;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001170
Allen Hubbee26a5842015-04-09 10:33:20 -04001171 nt->link_is_up = false;
1172 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1173 ntb_link_event(ndev);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001174
1175 return 0;
1176
Jon Mason948d3a62013-04-18 17:07:36 -07001177err3:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301178 ntb_clear_ctx(ndev);
Jon Mason948d3a62013-04-18 17:07:36 -07001179err2:
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301180 kfree(nt->qp_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001181err1:
Allen Hubbee26a5842015-04-09 10:33:20 -04001182 while (i--) {
1183 mw = &nt->mw_vec[i];
1184 iounmap(mw->vbase);
1185 }
Sudip Mukherjeed4adee02015-10-03 13:39:41 +05301186 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001187err:
Jon Masonfce8a7b2012-11-16 19:27:12 -07001188 kfree(nt);
1189 return rc;
1190}
1191
Allen Hubbee26a5842015-04-09 10:33:20 -04001192static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001193{
Allen Hubbee26a5842015-04-09 10:33:20 -04001194 struct ntb_transport_ctx *nt = ndev->ctx;
1195 struct ntb_transport_qp *qp;
1196 u64 qp_bitmap_alloc;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001197 int i;
1198
Jon Masonfca4d512013-09-09 13:39:55 -07001199 ntb_transport_link_cleanup(nt);
Allen Hubbee26a5842015-04-09 10:33:20 -04001200 cancel_work_sync(&nt->link_cleanup);
1201 cancel_delayed_work_sync(&nt->link_work);
1202
1203 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001204
1205 /* verify that all the qp's are freed */
Allen Hubbee26a5842015-04-09 10:33:20 -04001206 for (i = 0; i < nt->qp_count; i++) {
1207 qp = &nt->qp_vec[i];
1208 if (qp_bitmap_alloc & BIT_ULL(i))
1209 ntb_transport_free_queue(qp);
1210 debugfs_remove_recursive(qp->debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07001211 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001212
Allen Hubbee26a5842015-04-09 10:33:20 -04001213 ntb_link_disable(ndev);
1214 ntb_clear_ctx(ndev);
1215
Jon Masonfce8a7b2012-11-16 19:27:12 -07001216 ntb_bus_remove(nt);
1217
Allen Hubbee26a5842015-04-09 10:33:20 -04001218 for (i = nt->mw_count; i--; ) {
Jon Mason113fc502013-01-30 11:40:52 -07001219 ntb_free_mw(nt, i);
Allen Hubbee26a5842015-04-09 10:33:20 -04001220 iounmap(nt->mw_vec[i].vbase);
1221 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001222
Allen Hubbee26a5842015-04-09 10:33:20 -04001223 kfree(nt->qp_vec);
1224 kfree(nt->mw_vec);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001225 kfree(nt);
1226}
1227
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001228static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1229{
1230 struct ntb_queue_entry *entry;
1231 void *cb_data;
1232 unsigned int len;
1233 unsigned long irqflags;
1234
1235 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1236
1237 while (!list_empty(&qp->rx_post_q)) {
1238 entry = list_first_entry(&qp->rx_post_q,
1239 struct ntb_queue_entry, entry);
1240 if (!(entry->flags & DESC_DONE_FLAG))
1241 break;
1242
1243 entry->rx_hdr->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07001244 iowrite32(entry->rx_index, &qp->rx_info->entry);
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001245
1246 cb_data = entry->cb_data;
1247 len = entry->len;
1248
1249 list_move_tail(&entry->entry, &qp->rx_free_q);
1250
1251 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1252
1253 if (qp->rx_handler && qp->client_ready)
1254 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1255
1256 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1257 }
1258
1259 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1260}
1261
Dave Jiang72203572016-07-20 13:14:13 -07001262static void ntb_rx_copy_callback(void *data,
1263 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001264{
Jon Mason282a2fe2013-02-12 09:52:50 -07001265 struct ntb_queue_entry *entry = data;
Jon Mason448c6fb2013-01-19 02:02:27 -07001266
Dave Jiang72203572016-07-20 13:14:13 -07001267 /* we need to check DMA results if we are using DMA */
1268 if (res) {
1269 enum dmaengine_tx_result dma_err = res->result;
1270
1271 switch (dma_err) {
1272 case DMA_TRANS_READ_FAILED:
1273 case DMA_TRANS_WRITE_FAILED:
1274 entry->errors++;
1275 case DMA_TRANS_ABORTED:
1276 {
1277 struct ntb_transport_qp *qp = entry->qp;
1278 void *offset = qp->rx_buff + qp->rx_max_frame *
1279 qp->rx_index;
1280
1281 ntb_memcpy_rx(entry, offset);
1282 qp->rx_memcpy++;
1283 return;
1284 }
1285
1286 case DMA_TRANS_NOERROR:
1287 default:
1288 break;
1289 }
1290 }
1291
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001292 entry->flags |= DESC_DONE_FLAG;
Jon Mason282a2fe2013-02-12 09:52:50 -07001293
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001294 ntb_complete_rxc(entry->qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001295}
1296
Jon Mason282a2fe2013-02-12 09:52:50 -07001297static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1298{
1299 void *buf = entry->buf;
1300 size_t len = entry->len;
1301
1302 memcpy(buf, offset, len);
1303
Allen Hubbee26a5842015-04-09 10:33:20 -04001304 /* Ensure that the data is fully copied out before clearing the flag */
1305 wmb();
1306
Dave Jiang72203572016-07-20 13:14:13 -07001307 ntb_rx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001308}
1309
Dave Jiang72203572016-07-20 13:14:13 -07001310static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
Jon Mason282a2fe2013-02-12 09:52:50 -07001311{
1312 struct dma_async_tx_descriptor *txd;
1313 struct ntb_transport_qp *qp = entry->qp;
Dave Jiang569410ca2015-07-13 08:07:22 -04001314 struct dma_chan *chan = qp->rx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001315 struct dma_device *device;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001316 size_t pay_off, buff_off, len;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001317 struct dmaengine_unmap_data *unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001318 dma_cookie_t cookie;
1319 void *buf = entry->buf;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001320 int retries = 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001321
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001322 len = entry->len;
Jon Mason282a2fe2013-02-12 09:52:50 -07001323 device = chan->device;
Allen Hubbee26a5842015-04-09 10:33:20 -04001324 pay_off = (size_t)offset & ~PAGE_MASK;
1325 buff_off = (size_t)buf & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001326
1327 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
Allen Hubbe905921e2015-07-13 08:07:21 -04001328 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001329
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001330 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1331 if (!unmap)
Allen Hubbe905921e2015-07-13 08:07:21 -04001332 goto err;
Jon Mason282a2fe2013-02-12 09:52:50 -07001333
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001334 unmap->len = len;
1335 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1336 pay_off, len, DMA_TO_DEVICE);
1337 if (dma_mapping_error(device->dev, unmap->addr[0]))
1338 goto err_get_unmap;
Jon Mason282a2fe2013-02-12 09:52:50 -07001339
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001340 unmap->to_cnt = 1;
1341
1342 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1343 buff_off, len, DMA_FROM_DEVICE);
1344 if (dma_mapping_error(device->dev, unmap->addr[1]))
1345 goto err_get_unmap;
1346
1347 unmap->from_cnt = 1;
1348
Dave Jiang8c874cc2016-01-08 09:51:05 -07001349 for (retries = 0; retries < DMA_RETRIES; retries++) {
Dave Jiang72203572016-07-20 13:14:13 -07001350 txd = device->device_prep_dma_memcpy(chan,
1351 unmap->addr[1],
Dave Jiang8c874cc2016-01-08 09:51:05 -07001352 unmap->addr[0], len,
1353 DMA_PREP_INTERRUPT);
1354 if (txd)
1355 break;
1356
1357 set_current_state(TASK_INTERRUPTIBLE);
1358 schedule_timeout(DMA_OUT_RESOURCE_TO);
1359 }
1360
1361 if (!txd) {
1362 qp->dma_rx_prep_err++;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001363 goto err_get_unmap;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001364 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001365
Dave Jiang72203572016-07-20 13:14:13 -07001366 txd->callback_result = ntb_rx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001367 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001368 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001369
1370 cookie = dmaengine_submit(txd);
1371 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001372 goto err_set_unmap;
1373
1374 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001375
1376 qp->last_cookie = cookie;
1377
1378 qp->rx_async++;
1379
Dave Jiang72203572016-07-20 13:14:13 -07001380 return 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001381
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001382err_set_unmap:
1383 dmaengine_unmap_put(unmap);
1384err_get_unmap:
1385 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001386err:
Dave Jiang72203572016-07-20 13:14:13 -07001387 return -ENXIO;
1388}
1389
1390static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
1391{
1392 struct ntb_transport_qp *qp = entry->qp;
1393 struct dma_chan *chan = qp->rx_dma_chan;
1394 int res;
1395
1396 if (!chan)
1397 goto err;
1398
1399 if (entry->len < copy_bytes)
1400 goto err;
1401
1402 res = ntb_async_rx_submit(entry, offset);
1403 if (res < 0)
1404 goto err;
1405
1406 if (!entry->retries)
1407 qp->rx_async++;
1408
1409 return;
1410
1411err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001412 ntb_memcpy_rx(entry, offset);
1413 qp->rx_memcpy++;
1414}
1415
Jon Masonfce8a7b2012-11-16 19:27:12 -07001416static int ntb_process_rxc(struct ntb_transport_qp *qp)
1417{
1418 struct ntb_payload_header *hdr;
1419 struct ntb_queue_entry *entry;
1420 void *offset;
1421
Jon Mason793c20e2013-01-19 02:02:26 -07001422 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1423 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1424
Allen Hubbee26a5842015-04-09 10:33:20 -04001425 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1426 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001427
Jon Masonfce8a7b2012-11-16 19:27:12 -07001428 if (!(hdr->flags & DESC_DONE_FLAG)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001429 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001430 qp->rx_ring_empty++;
1431 return -EAGAIN;
1432 }
1433
Allen Hubbee26a5842015-04-09 10:33:20 -04001434 if (hdr->flags & LINK_DOWN_FLAG) {
1435 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1436 ntb_qp_link_down(qp);
1437 hdr->flags = 0;
Allen Hubbec0900b32015-05-12 06:24:27 -04001438 return -EAGAIN;
Allen Hubbee26a5842015-04-09 10:33:20 -04001439 }
1440
1441 if (hdr->ver != (u32)qp->rx_pkts) {
1442 dev_dbg(&qp->ndev->pdev->dev,
1443 "version mismatch, expected %llu - got %u\n",
1444 qp->rx_pkts, hdr->ver);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001445 qp->rx_err_ver++;
1446 return -EIO;
1447 }
1448
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001449 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
Allen Hubbee26a5842015-04-09 10:33:20 -04001450 if (!entry) {
1451 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1452 qp->rx_err_no_buf++;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001453 return -EAGAIN;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001454 }
1455
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001456 entry->rx_hdr = hdr;
Dave Jiang72203572016-07-20 13:14:13 -07001457 entry->rx_index = qp->rx_index;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001458
Allen Hubbee26a5842015-04-09 10:33:20 -04001459 if (hdr->len > entry->len) {
1460 dev_dbg(&qp->ndev->pdev->dev,
1461 "receive buffer overflow! Wanted %d got %d\n",
1462 hdr->len, entry->len);
1463 qp->rx_err_oflow++;
1464
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001465 entry->len = -EIO;
1466 entry->flags |= DESC_DONE_FLAG;
1467
1468 ntb_complete_rxc(qp);
1469 } else {
1470 dev_dbg(&qp->ndev->pdev->dev,
1471 "RX OK index %u ver %u size %d into buf size %d\n",
1472 qp->rx_index, hdr->ver, hdr->len, entry->len);
1473
1474 qp->rx_bytes += hdr->len;
1475 qp->rx_pkts++;
1476
1477 entry->len = hdr->len;
1478
1479 ntb_async_rx(entry, offset);
Allen Hubbee26a5842015-04-09 10:33:20 -04001480 }
1481
Jon Mason282a2fe2013-02-12 09:52:50 -07001482 qp->rx_index++;
1483 qp->rx_index %= qp->rx_max_entry;
1484
1485 return 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001486}
1487
Allen Hubbee26a5842015-04-09 10:33:20 -04001488static void ntb_transport_rxc_db(unsigned long data)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001489{
Allen Hubbee26a5842015-04-09 10:33:20 -04001490 struct ntb_transport_qp *qp = (void *)data;
Jon Masonc336acd2013-01-17 15:28:45 -07001491 int rc, i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001492
Allen Hubbee26a5842015-04-09 10:33:20 -04001493 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1494 __func__, qp->qp_num);
Jon Masone8aeb602013-04-18 17:59:44 -07001495
Jon Masonc336acd2013-01-17 15:28:45 -07001496 /* Limit the number of packets processed in a single interrupt to
1497 * provide fairness to others
1498 */
1499 for (i = 0; i < qp->rx_max_entry; i++) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001500 rc = ntb_process_rxc(qp);
Jon Masonc336acd2013-01-17 15:28:45 -07001501 if (rc)
1502 break;
1503 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001504
Dave Jiang569410ca2015-07-13 08:07:22 -04001505 if (i && qp->rx_dma_chan)
1506 dma_async_issue_pending(qp->rx_dma_chan);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001507
Allen Hubbee26a5842015-04-09 10:33:20 -04001508 if (i == qp->rx_max_entry) {
1509 /* there is more work to do */
Dave Jiange9021332016-02-23 09:11:36 -07001510 if (qp->active)
1511 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001512 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1513 /* the doorbell bit is set: clear it */
1514 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1515 /* ntb_db_read ensures ntb_db_clear write is committed */
1516 ntb_db_read(qp->ndev);
1517
1518 /* an interrupt may have arrived between finishing
1519 * ntb_process_rxc and clearing the doorbell bit:
1520 * there might be some more work to do.
1521 */
Dave Jiange9021332016-02-23 09:11:36 -07001522 if (qp->active)
1523 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04001524 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001525}
1526
Dave Jiang9cabc262016-07-20 13:14:07 -07001527static void ntb_tx_copy_callback(void *data,
1528 const struct dmaengine_result *res)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001529{
Jon Mason282a2fe2013-02-12 09:52:50 -07001530 struct ntb_queue_entry *entry = data;
1531 struct ntb_transport_qp *qp = entry->qp;
1532 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001533
Dave Jiang9cabc262016-07-20 13:14:07 -07001534 /* we need to check DMA results if we are using DMA */
1535 if (res) {
1536 enum dmaengine_tx_result dma_err = res->result;
1537
1538 switch (dma_err) {
1539 case DMA_TRANS_READ_FAILED:
1540 case DMA_TRANS_WRITE_FAILED:
1541 entry->errors++;
1542 case DMA_TRANS_ABORTED:
1543 {
1544 void __iomem *offset =
1545 qp->tx_mw + qp->tx_max_frame *
1546 entry->tx_index;
1547
1548 /* resubmit via CPU */
1549 ntb_memcpy_tx(entry, offset);
1550 qp->tx_memcpy++;
1551 return;
1552 }
1553
1554 case DMA_TRANS_NOERROR:
1555 default:
1556 break;
1557 }
1558 }
1559
Jon Mason74465642013-01-21 15:28:52 -07001560 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001561
Allen Hubbee26a5842015-04-09 10:33:20 -04001562 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07001563
1564 /* The entry length can only be zero if the packet is intended to be a
1565 * "link down" or similar. Since no payload is being sent in these
1566 * cases, there is nothing to add to the completion queue.
1567 */
1568 if (entry->len > 0) {
1569 qp->tx_bytes += entry->len;
1570
1571 if (qp->tx_handler)
1572 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1573 entry->len);
1574 }
1575
1576 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1577}
1578
Jon Mason282a2fe2013-02-12 09:52:50 -07001579static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1580{
Dave Jiang06917f72015-05-19 16:45:46 -04001581#ifdef ARCH_HAS_NOCACHE_UACCESS
1582 /*
1583 * Using non-temporal mov to improve performance on non-cached
1584 * writes, even though we aren't actually copying from user space.
1585 */
1586 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1587#else
Jon Mason282a2fe2013-02-12 09:52:50 -07001588 memcpy_toio(offset, entry->buf, entry->len);
Dave Jiang06917f72015-05-19 16:45:46 -04001589#endif
Jon Mason282a2fe2013-02-12 09:52:50 -07001590
Allen Hubbee26a5842015-04-09 10:33:20 -04001591 /* Ensure that the data is fully copied out before setting the flags */
1592 wmb();
1593
Dave Jiang9cabc262016-07-20 13:14:07 -07001594 ntb_tx_copy_callback(entry, NULL);
Jon Mason282a2fe2013-02-12 09:52:50 -07001595}
1596
Dave Jiang9cabc262016-07-20 13:14:07 -07001597static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1598 struct ntb_queue_entry *entry)
Jon Mason282a2fe2013-02-12 09:52:50 -07001599{
Jon Mason282a2fe2013-02-12 09:52:50 -07001600 struct dma_async_tx_descriptor *txd;
Dave Jiang569410ca2015-07-13 08:07:22 -04001601 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001602 struct dma_device *device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001603 size_t len = entry->len;
1604 void *buf = entry->buf;
Jon Mason282a2fe2013-02-12 09:52:50 -07001605 size_t dest_off, buff_off;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001606 struct dmaengine_unmap_data *unmap;
1607 dma_addr_t dest;
Jon Mason282a2fe2013-02-12 09:52:50 -07001608 dma_cookie_t cookie;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001609 int retries = 0;
Jon Mason282a2fe2013-02-12 09:52:50 -07001610
Jon Mason282a2fe2013-02-12 09:52:50 -07001611 device = chan->device;
Dave Jiang9cabc262016-07-20 13:14:07 -07001612 dest = qp->tx_mw_phys + qp->tx_max_frame * entry->tx_index;
Allen Hubbee26a5842015-04-09 10:33:20 -04001613 buff_off = (size_t)buf & ~PAGE_MASK;
1614 dest_off = (size_t)dest & ~PAGE_MASK;
Jon Mason282a2fe2013-02-12 09:52:50 -07001615
1616 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1617 goto err;
1618
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001619 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1620 if (!unmap)
Jon Mason282a2fe2013-02-12 09:52:50 -07001621 goto err;
1622
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001623 unmap->len = len;
1624 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1625 buff_off, len, DMA_TO_DEVICE);
1626 if (dma_mapping_error(device->dev, unmap->addr[0]))
1627 goto err_get_unmap;
1628
1629 unmap->to_cnt = 1;
1630
Dave Jiang8c874cc2016-01-08 09:51:05 -07001631 for (retries = 0; retries < DMA_RETRIES; retries++) {
Dave Jiang9cabc262016-07-20 13:14:07 -07001632 txd = device->device_prep_dma_memcpy(chan, dest,
1633 unmap->addr[0], len,
1634 DMA_PREP_INTERRUPT);
Dave Jiang8c874cc2016-01-08 09:51:05 -07001635 if (txd)
1636 break;
1637
1638 set_current_state(TASK_INTERRUPTIBLE);
1639 schedule_timeout(DMA_OUT_RESOURCE_TO);
1640 }
1641
1642 if (!txd) {
1643 qp->dma_tx_prep_err++;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001644 goto err_get_unmap;
Dave Jiang8c874cc2016-01-08 09:51:05 -07001645 }
Jon Mason282a2fe2013-02-12 09:52:50 -07001646
Dave Jiang9cabc262016-07-20 13:14:07 -07001647 txd->callback_result = ntb_tx_copy_callback;
Jon Mason282a2fe2013-02-12 09:52:50 -07001648 txd->callback_param = entry;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001649 dma_set_unmap(txd, unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001650
1651 cookie = dmaengine_submit(txd);
1652 if (dma_submit_error(cookie))
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001653 goto err_set_unmap;
1654
1655 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001656
1657 dma_async_issue_pending(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001658
Dave Jiang9cabc262016-07-20 13:14:07 -07001659 return 0;
Bartlomiej Zolnierkiewicz6f57fd02013-10-18 19:35:31 +02001660err_set_unmap:
1661 dmaengine_unmap_put(unmap);
1662err_get_unmap:
1663 dmaengine_unmap_put(unmap);
Jon Mason282a2fe2013-02-12 09:52:50 -07001664err:
Dave Jiang9cabc262016-07-20 13:14:07 -07001665 return -ENXIO;
1666}
1667
1668static void ntb_async_tx(struct ntb_transport_qp *qp,
1669 struct ntb_queue_entry *entry)
1670{
1671 struct ntb_payload_header __iomem *hdr;
1672 struct dma_chan *chan = qp->tx_dma_chan;
1673 void __iomem *offset;
1674 int res;
1675
1676 entry->tx_index = qp->tx_index;
1677 offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
1678 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1679 entry->tx_hdr = hdr;
1680
1681 iowrite32(entry->len, &hdr->len);
1682 iowrite32((u32)qp->tx_pkts, &hdr->ver);
1683
1684 if (!chan)
1685 goto err;
1686
1687 if (entry->len < copy_bytes)
1688 goto err;
1689
1690 res = ntb_async_tx_submit(qp, entry);
1691 if (res < 0)
1692 goto err;
1693
1694 if (!entry->retries)
1695 qp->tx_async++;
1696
1697 return;
1698
1699err:
Jon Mason282a2fe2013-02-12 09:52:50 -07001700 ntb_memcpy_tx(entry, offset);
1701 qp->tx_memcpy++;
1702}
1703
Jon Masonfce8a7b2012-11-16 19:27:12 -07001704static int ntb_process_tx(struct ntb_transport_qp *qp,
1705 struct ntb_queue_entry *entry)
1706{
Jon Mason793c20e2013-01-19 02:02:26 -07001707 if (qp->tx_index == qp->remote_rx_info->entry) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001708 qp->tx_ring_full++;
1709 return -EAGAIN;
1710 }
1711
Jon Masonef114ed2013-01-19 02:02:18 -07001712 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
Jon Masonfce8a7b2012-11-16 19:27:12 -07001713 if (qp->tx_handler)
Jon Mason179f9122015-12-18 13:22:37 -05001714 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001715
1716 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1717 &qp->tx_free_q);
1718 return 0;
1719 }
1720
Jon Mason282a2fe2013-02-12 09:52:50 -07001721 ntb_async_tx(qp, entry);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001722
Jon Mason793c20e2013-01-19 02:02:26 -07001723 qp->tx_index++;
1724 qp->tx_index %= qp->tx_max_entry;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001725
1726 qp->tx_pkts++;
1727
1728 return 0;
1729}
1730
1731static void ntb_send_link_down(struct ntb_transport_qp *qp)
1732{
Allen Hubbee26a5842015-04-09 10:33:20 -04001733 struct pci_dev *pdev = qp->ndev->pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001734 struct ntb_queue_entry *entry;
1735 int i, rc;
1736
Allen Hubbee26a5842015-04-09 10:33:20 -04001737 if (!qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001738 return;
1739
Allen Hubbee22e0b92015-05-12 06:55:44 -04001740 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001741
1742 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
Jon Masonf7667552013-01-19 02:02:24 -07001743 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001744 if (entry)
1745 break;
1746 msleep(100);
1747 }
1748
1749 if (!entry)
1750 return;
1751
1752 entry->cb_data = NULL;
1753 entry->buf = NULL;
1754 entry->len = 0;
1755 entry->flags = LINK_DOWN_FLAG;
1756
1757 rc = ntb_process_tx(qp, entry);
1758 if (rc)
1759 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1760 qp->qp_num);
Allen Hubbe2849b5d72015-05-12 08:09:15 -04001761
1762 ntb_qp_link_down_reset(qp);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001763}
1764
Allen Hubbe1199aa62015-05-18 06:20:47 -04001765static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1766{
1767 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1768}
1769
Jon Masonfce8a7b2012-11-16 19:27:12 -07001770/**
1771 * ntb_transport_create_queue - Create a new NTB transport layer queue
1772 * @rx_handler: receive callback function
1773 * @tx_handler: transmit callback function
1774 * @event_handler: event callback function
1775 *
1776 * Create a new NTB transport layer queue and provide the queue with a callback
1777 * routine for both transmit and receive. The receive callback routine will be
1778 * used to pass up data when the transport has received it on the queue. The
1779 * transmit callback routine will be called when the transport has completed the
1780 * transmission of the data on the queue and the data is ready to be freed.
1781 *
1782 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1783 */
1784struct ntb_transport_qp *
Allen Hubbee26a5842015-04-09 10:33:20 -04001785ntb_transport_create_queue(void *data, struct device *client_dev,
Jon Masonfce8a7b2012-11-16 19:27:12 -07001786 const struct ntb_queue_handlers *handlers)
1787{
Allen Hubbee26a5842015-04-09 10:33:20 -04001788 struct ntb_dev *ndev;
1789 struct pci_dev *pdev;
1790 struct ntb_transport_ctx *nt;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001791 struct ntb_queue_entry *entry;
1792 struct ntb_transport_qp *qp;
Allen Hubbee26a5842015-04-09 10:33:20 -04001793 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001794 unsigned int free_queue;
Allen Hubbe1199aa62015-05-18 06:20:47 -04001795 dma_cap_mask_t dma_mask;
1796 int node;
Allen Hubbee26a5842015-04-09 10:33:20 -04001797 int i;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001798
Allen Hubbee26a5842015-04-09 10:33:20 -04001799 ndev = dev_ntb(client_dev->parent);
1800 pdev = ndev->pdev;
1801 nt = ndev->ctx;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001802
Allen Hubbe1199aa62015-05-18 06:20:47 -04001803 node = dev_to_node(&ndev->dev);
1804
Thomas VanSelus8fcd0952017-02-13 16:46:26 -06001805 free_queue = ffs(nt->qp_bitmap_free);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001806 if (!free_queue)
1807 goto err;
1808
1809 /* decrement free_queue to make it zero based */
1810 free_queue--;
1811
Allen Hubbee26a5842015-04-09 10:33:20 -04001812 qp = &nt->qp_vec[free_queue];
1813 qp_bit = BIT_ULL(qp->qp_num);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001814
Allen Hubbee26a5842015-04-09 10:33:20 -04001815 nt->qp_bitmap_free &= ~qp_bit;
1816
Jon Masonfce8a7b2012-11-16 19:27:12 -07001817 qp->cb_data = data;
1818 qp->rx_handler = handlers->rx_handler;
1819 qp->tx_handler = handlers->tx_handler;
1820 qp->event_handler = handlers->event_handler;
1821
Allen Hubbe1199aa62015-05-18 06:20:47 -04001822 dma_cap_zero(dma_mask);
1823 dma_cap_set(DMA_MEMCPY, dma_mask);
1824
Dave Jianga41ef052015-05-19 16:52:04 -04001825 if (use_dma) {
Dave Jiang569410ca2015-07-13 08:07:22 -04001826 qp->tx_dma_chan =
1827 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1828 (void *)(unsigned long)node);
1829 if (!qp->tx_dma_chan)
1830 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
1831
1832 qp->rx_dma_chan =
1833 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1834 (void *)(unsigned long)node);
1835 if (!qp->rx_dma_chan)
1836 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
Dave Jianga41ef052015-05-19 16:52:04 -04001837 } else {
Dave Jiang569410ca2015-07-13 08:07:22 -04001838 qp->tx_dma_chan = NULL;
1839 qp->rx_dma_chan = NULL;
Dave Jianga41ef052015-05-19 16:52:04 -04001840 }
Dave Jiang569410ca2015-07-13 08:07:22 -04001841
1842 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
1843 qp->tx_dma_chan ? "DMA" : "CPU");
1844
1845 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
1846 qp->rx_dma_chan ? "DMA" : "CPU");
Jon Mason282a2fe2013-02-12 09:52:50 -07001847
Jon Masonfce8a7b2012-11-16 19:27:12 -07001848 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001849 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001850 if (!entry)
1851 goto err1;
1852
Jon Mason282a2fe2013-02-12 09:52:50 -07001853 entry->qp = qp;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001854 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001855 &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001856 }
Dave Jianga754a8f2016-04-08 10:49:06 -07001857 qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001858
Dave Jianga754a8f2016-04-08 10:49:06 -07001859 for (i = 0; i < qp->tx_max_entry; i++) {
Allen Hubbe1199aa62015-05-18 06:20:47 -04001860 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001861 if (!entry)
1862 goto err2;
1863
Jon Mason282a2fe2013-02-12 09:52:50 -07001864 entry->qp = qp;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001865 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
Jon Masonf7667552013-01-19 02:02:24 -07001866 &qp->tx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001867 }
1868
Allen Hubbee26a5842015-04-09 10:33:20 -04001869 ntb_db_clear(qp->ndev, qp_bit);
1870 ntb_db_clear_mask(qp->ndev, qp_bit);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001871
1872 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1873
1874 return qp;
1875
Jon Masonfce8a7b2012-11-16 19:27:12 -07001876err2:
Jon Masonf7667552013-01-19 02:02:24 -07001877 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001878 kfree(entry);
1879err1:
Dave Jianga754a8f2016-04-08 10:49:06 -07001880 qp->rx_alloc_entry = 0;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001881 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001882 kfree(entry);
Dave Jiang569410ca2015-07-13 08:07:22 -04001883 if (qp->tx_dma_chan)
1884 dma_release_channel(qp->tx_dma_chan);
1885 if (qp->rx_dma_chan)
1886 dma_release_channel(qp->rx_dma_chan);
Allen Hubbee26a5842015-04-09 10:33:20 -04001887 nt->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001888err:
1889 return NULL;
1890}
1891EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1892
1893/**
1894 * ntb_transport_free_queue - Frees NTB transport queue
1895 * @qp: NTB queue to be freed
1896 *
1897 * Frees NTB transport queue
1898 */
1899void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1900{
Jon Mason186f27f2013-01-22 11:35:40 -07001901 struct pci_dev *pdev;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001902 struct ntb_queue_entry *entry;
Allen Hubbee26a5842015-04-09 10:33:20 -04001903 u64 qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001904
1905 if (!qp)
1906 return;
1907
Allen Hubbee26a5842015-04-09 10:33:20 -04001908 pdev = qp->ndev->pdev;
Jon Mason186f27f2013-01-22 11:35:40 -07001909
Dave Jiange9021332016-02-23 09:11:36 -07001910 qp->active = false;
1911
Dave Jiang569410ca2015-07-13 08:07:22 -04001912 if (qp->tx_dma_chan) {
1913 struct dma_chan *chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07001914 /* Putting the dma_chan to NULL will force any new traffic to be
1915 * processed by the CPU instead of the DAM engine
1916 */
Dave Jiang569410ca2015-07-13 08:07:22 -04001917 qp->tx_dma_chan = NULL;
1918
1919 /* Try to be nice and wait for any queued DMA engine
1920 * transactions to process before smashing it with a rock
1921 */
1922 dma_sync_wait(chan, qp->last_cookie);
1923 dmaengine_terminate_all(chan);
1924 dma_release_channel(chan);
1925 }
1926
1927 if (qp->rx_dma_chan) {
1928 struct dma_chan *chan = qp->rx_dma_chan;
1929 /* Putting the dma_chan to NULL will force any new traffic to be
1930 * processed by the CPU instead of the DAM engine
1931 */
1932 qp->rx_dma_chan = NULL;
Jon Mason282a2fe2013-02-12 09:52:50 -07001933
1934 /* Try to be nice and wait for any queued DMA engine
1935 * transactions to process before smashing it with a rock
1936 */
1937 dma_sync_wait(chan, qp->last_cookie);
1938 dmaengine_terminate_all(chan);
Allen Hubbe1199aa62015-05-18 06:20:47 -04001939 dma_release_channel(chan);
Jon Mason282a2fe2013-02-12 09:52:50 -07001940 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07001941
Allen Hubbee26a5842015-04-09 10:33:20 -04001942 qp_bit = BIT_ULL(qp->qp_num);
1943
1944 ntb_db_set_mask(qp->ndev, qp_bit);
Dave Jiange9021332016-02-23 09:11:36 -07001945 tasklet_kill(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001946
Jon Mason282a2fe2013-02-12 09:52:50 -07001947 cancel_delayed_work_sync(&qp->link_work);
1948
Allen Hubbee26a5842015-04-09 10:33:20 -04001949 qp->cb_data = NULL;
1950 qp->rx_handler = NULL;
1951 qp->tx_handler = NULL;
1952 qp->event_handler = NULL;
1953
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001954 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001955 kfree(entry);
1956
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001957 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1958 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1959 kfree(entry);
1960 }
1961
1962 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1963 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
Jon Masonfce8a7b2012-11-16 19:27:12 -07001964 kfree(entry);
1965 }
1966
Jon Masonf7667552013-01-19 02:02:24 -07001967 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
Jon Masonfce8a7b2012-11-16 19:27:12 -07001968 kfree(entry);
1969
Allen Hubbe30a4bb12015-07-13 08:07:14 -04001970 qp->transport->qp_bitmap_free |= qp_bit;
Jon Masonfce8a7b2012-11-16 19:27:12 -07001971
1972 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1973}
1974EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1975
1976/**
1977 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1978 * @qp: NTB queue to be freed
1979 * @len: pointer to variable to write enqueued buffers length
1980 *
1981 * Dequeues unused buffers from receive queue. Should only be used during
1982 * shutdown of qp.
1983 *
1984 * RETURNS: NULL error value on error, or void* for success.
1985 */
1986void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1987{
1988 struct ntb_queue_entry *entry;
1989 void *buf;
1990
Allen Hubbee26a5842015-04-09 10:33:20 -04001991 if (!qp || qp->client_ready)
Jon Masonfce8a7b2012-11-16 19:27:12 -07001992 return NULL;
1993
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04001994 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07001995 if (!entry)
1996 return NULL;
1997
1998 buf = entry->cb_data;
1999 *len = entry->len;
2000
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002001 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002002
2003 return buf;
2004}
2005EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
2006
2007/**
2008 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
2009 * @qp: NTB transport layer queue the entry is to be enqueued on
2010 * @cb: per buffer pointer for callback function to use
2011 * @data: pointer to data buffer that incoming packets will be copied into
2012 * @len: length of the data buffer
2013 *
2014 * Enqueue a new receive buffer onto the transport queue into which a NTB
2015 * payload can be received into.
2016 *
2017 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2018 */
2019int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2020 unsigned int len)
2021{
2022 struct ntb_queue_entry *entry;
2023
2024 if (!qp)
2025 return -EINVAL;
2026
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002027 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002028 if (!entry)
2029 return -ENOMEM;
2030
2031 entry->cb_data = cb;
2032 entry->buf = data;
2033 entry->len = len;
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002034 entry->flags = 0;
Dave Jiang72203572016-07-20 13:14:13 -07002035 entry->retries = 0;
2036 entry->errors = 0;
2037 entry->rx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002038
Allen Hubbeda2e5ae2015-07-13 08:07:08 -04002039 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
2040
Dave Jiange9021332016-02-23 09:11:36 -07002041 if (qp->active)
2042 tasklet_schedule(&qp->rxc_db_work);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002043
2044 return 0;
2045}
2046EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2047
2048/**
2049 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2050 * @qp: NTB transport layer queue the entry is to be enqueued on
2051 * @cb: per buffer pointer for callback function to use
2052 * @data: pointer to data buffer that will be sent
2053 * @len: length of the data buffer
2054 *
2055 * Enqueue a new transmit buffer onto the transport queue from which a NTB
Jon Masonf9a2cf82013-07-29 16:46:43 -07002056 * payload will be transmitted. This assumes that a lock is being held to
Jon Masonfce8a7b2012-11-16 19:27:12 -07002057 * serialize access to the qp.
2058 *
2059 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2060 */
2061int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2062 unsigned int len)
2063{
2064 struct ntb_queue_entry *entry;
2065 int rc;
2066
Allen Hubbee26a5842015-04-09 10:33:20 -04002067 if (!qp || !qp->link_is_up || !len)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002068 return -EINVAL;
2069
2070 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
Jon Mason282a2fe2013-02-12 09:52:50 -07002071 if (!entry) {
2072 qp->tx_err_no_buf++;
Dave Jiange74bfee2015-07-13 08:07:17 -04002073 return -EBUSY;
Jon Mason282a2fe2013-02-12 09:52:50 -07002074 }
Jon Masonfce8a7b2012-11-16 19:27:12 -07002075
2076 entry->cb_data = cb;
2077 entry->buf = data;
2078 entry->len = len;
2079 entry->flags = 0;
Dave Jiang9cabc262016-07-20 13:14:07 -07002080 entry->errors = 0;
2081 entry->retries = 0;
2082 entry->tx_index = 0;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002083
2084 rc = ntb_process_tx(qp, entry);
2085 if (rc)
2086 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2087 &qp->tx_free_q);
2088
2089 return rc;
2090}
2091EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2092
2093/**
2094 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2095 * @qp: NTB transport layer queue to be enabled
2096 *
2097 * Notify NTB transport layer of client readiness to use queue
2098 */
2099void ntb_transport_link_up(struct ntb_transport_qp *qp)
2100{
2101 if (!qp)
2102 return;
2103
Allen Hubbee26a5842015-04-09 10:33:20 -04002104 qp->client_ready = true;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002105
Allen Hubbee26a5842015-04-09 10:33:20 -04002106 if (qp->transport->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002107 schedule_delayed_work(&qp->link_work, 0);
2108}
2109EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2110
2111/**
2112 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2113 * @qp: NTB transport layer queue to be disabled
2114 *
2115 * Notify NTB transport layer of client's desire to no longer receive data on
2116 * transport queue specified. It is the client's responsibility to ensure all
Jon Masonf9a2cf82013-07-29 16:46:43 -07002117 * entries on queue are purged or otherwise handled appropriately.
Jon Masonfce8a7b2012-11-16 19:27:12 -07002118 */
2119void ntb_transport_link_down(struct ntb_transport_qp *qp)
2120{
Allen Hubbee26a5842015-04-09 10:33:20 -04002121 int val;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002122
2123 if (!qp)
2124 return;
2125
Allen Hubbee26a5842015-04-09 10:33:20 -04002126 qp->client_ready = false;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002127
Allen Hubbee26a5842015-04-09 10:33:20 -04002128 val = ntb_spad_read(qp->ndev, QP_LINKS);
Jon Masonfce8a7b2012-11-16 19:27:12 -07002129
Allen Hubbee26a5842015-04-09 10:33:20 -04002130 ntb_peer_spad_write(qp->ndev, QP_LINKS,
2131 val & ~BIT(qp->qp_num));
Jon Masonfce8a7b2012-11-16 19:27:12 -07002132
Allen Hubbee26a5842015-04-09 10:33:20 -04002133 if (qp->link_is_up)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002134 ntb_send_link_down(qp);
2135 else
2136 cancel_delayed_work_sync(&qp->link_work);
2137}
2138EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2139
2140/**
2141 * ntb_transport_link_query - Query transport link state
2142 * @qp: NTB transport layer queue to be queried
2143 *
2144 * Query connectivity to the remote system of the NTB transport queue
2145 *
2146 * RETURNS: true for link up or false for link down
2147 */
2148bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2149{
Jon Mason186f27f2013-01-22 11:35:40 -07002150 if (!qp)
2151 return false;
2152
Allen Hubbee26a5842015-04-09 10:33:20 -04002153 return qp->link_is_up;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002154}
2155EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2156
2157/**
2158 * ntb_transport_qp_num - Query the qp number
2159 * @qp: NTB transport layer queue to be queried
2160 *
2161 * Query qp number of the NTB transport queue
2162 *
2163 * RETURNS: a zero based number specifying the qp number
2164 */
2165unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2166{
Jon Mason186f27f2013-01-22 11:35:40 -07002167 if (!qp)
2168 return 0;
2169
Jon Masonfce8a7b2012-11-16 19:27:12 -07002170 return qp->qp_num;
2171}
2172EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2173
2174/**
2175 * ntb_transport_max_size - Query the max payload size of a qp
2176 * @qp: NTB transport layer queue to be queried
2177 *
2178 * Query the maximum payload size permissible on the given qp
2179 *
2180 * RETURNS: the max payload size of a qp
2181 */
Jon Masonef114ed2013-01-19 02:02:18 -07002182unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
Jon Masonfce8a7b2012-11-16 19:27:12 -07002183{
Dave Jiang04afde42015-09-17 13:27:04 -07002184 unsigned int max_size;
Dave Jiang569410ca2015-07-13 08:07:22 -04002185 unsigned int copy_align;
Dave Jiang04afde42015-09-17 13:27:04 -07002186 struct dma_chan *rx_chan, *tx_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002187
Jon Mason186f27f2013-01-22 11:35:40 -07002188 if (!qp)
2189 return 0;
2190
Dave Jiang04afde42015-09-17 13:27:04 -07002191 rx_chan = qp->rx_dma_chan;
2192 tx_chan = qp->tx_dma_chan;
Jon Mason282a2fe2013-02-12 09:52:50 -07002193
Dave Jiang04afde42015-09-17 13:27:04 -07002194 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2195 tx_chan ? tx_chan->device->copy_align : 0);
Dave Jiang569410ca2015-07-13 08:07:22 -04002196
Jon Mason282a2fe2013-02-12 09:52:50 -07002197 /* If DMA engine usage is possible, try to find the max size for that */
Dave Jiang04afde42015-09-17 13:27:04 -07002198 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2199 max_size = round_down(max_size, 1 << copy_align);
Jon Mason282a2fe2013-02-12 09:52:50 -07002200
Dave Jiang04afde42015-09-17 13:27:04 -07002201 return max_size;
Jon Masonfce8a7b2012-11-16 19:27:12 -07002202}
2203EXPORT_SYMBOL_GPL(ntb_transport_max_size);
Allen Hubbee26a5842015-04-09 10:33:20 -04002204
Dave Jiange74bfee2015-07-13 08:07:17 -04002205unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2206{
2207 unsigned int head = qp->tx_index;
2208 unsigned int tail = qp->remote_rx_info->entry;
2209
2210 return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2211}
2212EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2213
Allen Hubbee26a5842015-04-09 10:33:20 -04002214static void ntb_transport_doorbell_callback(void *data, int vector)
2215{
2216 struct ntb_transport_ctx *nt = data;
2217 struct ntb_transport_qp *qp;
2218 u64 db_bits;
2219 unsigned int qp_num;
2220
2221 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2222 ntb_db_vector_mask(nt->ndev, vector));
2223
2224 while (db_bits) {
2225 qp_num = __ffs(db_bits);
2226 qp = &nt->qp_vec[qp_num];
2227
Dave Jiange9021332016-02-23 09:11:36 -07002228 if (qp->active)
2229 tasklet_schedule(&qp->rxc_db_work);
Allen Hubbee26a5842015-04-09 10:33:20 -04002230
2231 db_bits &= ~BIT_ULL(qp_num);
2232 }
2233}
2234
2235static const struct ntb_ctx_ops ntb_transport_ops = {
2236 .link_event = ntb_transport_event_callback,
2237 .db_event = ntb_transport_doorbell_callback,
2238};
2239
2240static struct ntb_client ntb_transport_client = {
2241 .ops = {
2242 .probe = ntb_transport_probe,
2243 .remove = ntb_transport_free,
2244 },
2245};
2246
2247static int __init ntb_transport_init(void)
2248{
2249 int rc;
2250
Dave Jiang7eb38782015-06-15 08:21:33 -04002251 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2252
Allen Hubbee26a5842015-04-09 10:33:20 -04002253 if (debugfs_initialized())
2254 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2255
2256 rc = bus_register(&ntb_transport_bus);
2257 if (rc)
2258 goto err_bus;
2259
2260 rc = ntb_register_client(&ntb_transport_client);
2261 if (rc)
2262 goto err_client;
2263
2264 return 0;
2265
2266err_client:
2267 bus_unregister(&ntb_transport_bus);
2268err_bus:
2269 debugfs_remove_recursive(nt_debugfs_dir);
2270 return rc;
2271}
2272module_init(ntb_transport_init);
2273
2274static void __exit ntb_transport_exit(void)
2275{
Allen Hubbee26a5842015-04-09 10:33:20 -04002276 ntb_unregister_client(&ntb_transport_client);
2277 bus_unregister(&ntb_transport_bus);
Allen Hubbedd622452016-12-27 17:57:04 -05002278 debugfs_remove_recursive(nt_debugfs_dir);
Allen Hubbee26a5842015-04-09 10:33:20 -04002279}
2280module_exit(ntb_transport_exit);