Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 8 | * Copyright (C) 2015 EMC Corporation. All Rights Reserved. |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 9 | * |
| 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 Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 17 | * Copyright (C) 2015 EMC Corporation. All Rights Reserved. |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 18 | * |
| 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 Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 45 | * PCIe NTB Transport Linux driver |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 46 | * |
| 47 | * Contact Information: |
| 48 | * Jon Mason <jon.mason@intel.com> |
| 49 | */ |
| 50 | #include <linux/debugfs.h> |
| 51 | #include <linux/delay.h> |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 52 | #include <linux/dmaengine.h> |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 53 | #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 Jiang | 06917f7 | 2015-05-19 16:45:46 -0400 | [diff] [blame] | 61 | #include <linux/uaccess.h> |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 62 | #include "linux/ntb.h" |
| 63 | #include "linux/ntb_transport.h" |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 64 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 65 | #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" |
| 69 | |
| 70 | MODULE_DESCRIPTION(NTB_TRANSPORT_DESC); |
| 71 | MODULE_VERSION(NTB_TRANSPORT_VER); |
| 72 | MODULE_LICENSE("Dual BSD/GPL"); |
| 73 | MODULE_AUTHOR("Intel Corporation"); |
| 74 | |
| 75 | static unsigned long max_mw_size; |
| 76 | module_param(max_mw_size, ulong, 0644); |
| 77 | MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows"); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 78 | |
Dave Jiang | 9891417 | 2015-06-03 11:29:38 -0400 | [diff] [blame] | 79 | static unsigned int transport_mtu = 0x10000; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 80 | module_param(transport_mtu, uint, 0644); |
| 81 | MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets"); |
| 82 | |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 83 | static unsigned char max_num_clients; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 84 | module_param(max_num_clients, byte, 0644); |
| 85 | MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients"); |
| 86 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 87 | static unsigned int copy_bytes = 1024; |
| 88 | module_param(copy_bytes, uint, 0644); |
| 89 | MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA"); |
| 90 | |
Dave Jiang | a41ef05 | 2015-05-19 16:52:04 -0400 | [diff] [blame] | 91 | static bool use_dma; |
| 92 | module_param(use_dma, bool, 0644); |
| 93 | MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy"); |
| 94 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 95 | static struct dentry *nt_debugfs_dir; |
| 96 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 97 | struct ntb_queue_entry { |
| 98 | /* ntb_queue list reference */ |
| 99 | struct list_head entry; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 100 | /* pointers to data to be transferred */ |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 101 | void *cb_data; |
| 102 | void *buf; |
| 103 | unsigned int len; |
| 104 | unsigned int flags; |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 105 | int retries; |
| 106 | int errors; |
| 107 | unsigned int tx_index; |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 108 | unsigned int rx_index; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 109 | |
| 110 | struct ntb_transport_qp *qp; |
| 111 | union { |
| 112 | struct ntb_payload_header __iomem *tx_hdr; |
| 113 | struct ntb_payload_header *rx_hdr; |
| 114 | }; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 117 | struct ntb_rx_info { |
| 118 | unsigned int entry; |
| 119 | }; |
| 120 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 121 | struct ntb_transport_qp { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 122 | struct ntb_transport_ctx *transport; |
| 123 | struct ntb_dev *ndev; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 124 | void *cb_data; |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 125 | struct dma_chan *tx_dma_chan; |
| 126 | struct dma_chan *rx_dma_chan; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 127 | |
| 128 | bool client_ready; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 129 | bool link_is_up; |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 130 | bool active; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 131 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 132 | u8 qp_num; /* Only 64 QP's are allowed. 0-63 */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 133 | u64 qp_bit; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 134 | |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 135 | struct ntb_rx_info __iomem *rx_info; |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 136 | struct ntb_rx_info *remote_rx_info; |
| 137 | |
Jon Mason | 53ca4fe | 2013-11-26 11:21:50 -0700 | [diff] [blame] | 138 | void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data, |
| 139 | void *data, int len); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 140 | struct list_head tx_free_q; |
| 141 | spinlock_t ntb_tx_free_q_lock; |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 142 | void __iomem *tx_mw; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 143 | dma_addr_t tx_mw_phys; |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 144 | unsigned int tx_index; |
| 145 | unsigned int tx_max_entry; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 146 | unsigned int tx_max_frame; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 147 | |
Jon Mason | 53ca4fe | 2013-11-26 11:21:50 -0700 | [diff] [blame] | 148 | void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data, |
| 149 | void *data, int len); |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 150 | struct list_head rx_post_q; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 151 | struct list_head rx_pend_q; |
| 152 | struct list_head rx_free_q; |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 153 | /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */ |
| 154 | spinlock_t ntb_rx_q_lock; |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 155 | void *rx_buff; |
| 156 | unsigned int rx_index; |
| 157 | unsigned int rx_max_entry; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 158 | unsigned int rx_max_frame; |
Dave Jiang | a754a8f | 2016-04-08 10:49:06 -0700 | [diff] [blame] | 159 | unsigned int rx_alloc_entry; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 160 | dma_cookie_t last_cookie; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 161 | struct tasklet_struct rxc_db_work; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 162 | |
Jon Mason | 53ca4fe | 2013-11-26 11:21:50 -0700 | [diff] [blame] | 163 | void (*event_handler)(void *data, int status); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 164 | struct delayed_work link_work; |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 165 | struct work_struct link_cleanup; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 166 | |
| 167 | struct dentry *debugfs_dir; |
| 168 | struct dentry *debugfs_stats; |
| 169 | |
| 170 | /* Stats */ |
| 171 | u64 rx_bytes; |
| 172 | u64 rx_pkts; |
| 173 | u64 rx_ring_empty; |
| 174 | u64 rx_err_no_buf; |
| 175 | u64 rx_err_oflow; |
| 176 | u64 rx_err_ver; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 177 | u64 rx_memcpy; |
| 178 | u64 rx_async; |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 179 | u64 dma_rx_prep_err; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 180 | u64 tx_bytes; |
| 181 | u64 tx_pkts; |
| 182 | u64 tx_ring_full; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 183 | u64 tx_err_no_buf; |
| 184 | u64 tx_memcpy; |
| 185 | u64 tx_async; |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 186 | u64 dma_tx_prep_err; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | struct ntb_transport_mw { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 190 | phys_addr_t phys_addr; |
| 191 | resource_size_t phys_size; |
| 192 | resource_size_t xlat_align; |
| 193 | resource_size_t xlat_align_size; |
| 194 | void __iomem *vbase; |
| 195 | size_t xlat_size; |
| 196 | size_t buff_size; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 197 | void *virt_addr; |
| 198 | dma_addr_t dma_addr; |
| 199 | }; |
| 200 | |
| 201 | struct ntb_transport_client_dev { |
| 202 | struct list_head entry; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 203 | struct ntb_transport_ctx *nt; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 204 | struct device dev; |
| 205 | }; |
| 206 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 207 | struct ntb_transport_ctx { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 208 | struct list_head entry; |
| 209 | struct list_head client_devs; |
| 210 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 211 | struct ntb_dev *ndev; |
| 212 | |
| 213 | struct ntb_transport_mw *mw_vec; |
| 214 | struct ntb_transport_qp *qp_vec; |
| 215 | unsigned int mw_count; |
| 216 | unsigned int qp_count; |
| 217 | u64 qp_bitmap; |
| 218 | u64 qp_bitmap_free; |
| 219 | |
| 220 | bool link_is_up; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 221 | struct delayed_work link_work; |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 222 | struct work_struct link_cleanup; |
Dave Jiang | c8650fd | 2015-07-13 08:07:09 -0400 | [diff] [blame] | 223 | |
| 224 | struct dentry *debugfs_node_dir; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | enum { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 228 | DESC_DONE_FLAG = BIT(0), |
| 229 | LINK_DOWN_FLAG = BIT(1), |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | struct ntb_payload_header { |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 233 | unsigned int ver; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 234 | unsigned int len; |
| 235 | unsigned int flags; |
| 236 | }; |
| 237 | |
| 238 | enum { |
| 239 | VERSION = 0, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 240 | QP_LINKS, |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 241 | NUM_QPS, |
| 242 | NUM_MWS, |
| 243 | MW0_SZ_HIGH, |
| 244 | MW0_SZ_LOW, |
| 245 | MW1_SZ_HIGH, |
| 246 | MW1_SZ_LOW, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 247 | MAX_SPAD, |
| 248 | }; |
| 249 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 250 | #define dev_client_dev(__dev) \ |
| 251 | container_of((__dev), struct ntb_transport_client_dev, dev) |
| 252 | |
| 253 | #define drv_client(__drv) \ |
| 254 | container_of((__drv), struct ntb_transport_client, driver) |
| 255 | |
| 256 | #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 257 | #define NTB_QP_DEF_NUM_ENTRIES 100 |
| 258 | #define NTB_LINK_DOWN_TIMEOUT 10 |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 259 | #define DMA_RETRIES 20 |
Nicholas Mc Guire | c0a8803 | 2016-08-22 18:51:35 +0200 | [diff] [blame] | 260 | #define DMA_OUT_RESOURCE_TO msecs_to_jiffies(50) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 261 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 262 | static void ntb_transport_rxc_db(unsigned long data); |
| 263 | static const struct ntb_ctx_ops ntb_transport_ops; |
| 264 | static struct ntb_client ntb_transport_client; |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 265 | static int ntb_async_tx_submit(struct ntb_transport_qp *qp, |
| 266 | struct ntb_queue_entry *entry); |
| 267 | static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset); |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 268 | static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset); |
| 269 | static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset); |
| 270 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 271 | |
| 272 | static int ntb_transport_bus_match(struct device *dev, |
| 273 | struct device_driver *drv) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 274 | { |
| 275 | return !strncmp(dev_name(dev), drv->name, strlen(drv->name)); |
| 276 | } |
| 277 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 278 | static int ntb_transport_bus_probe(struct device *dev) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 279 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 280 | const struct ntb_transport_client *client; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 281 | int rc = -EINVAL; |
| 282 | |
| 283 | get_device(dev); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 284 | |
| 285 | client = drv_client(dev->driver); |
| 286 | rc = client->probe(dev); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 287 | if (rc) |
| 288 | put_device(dev); |
| 289 | |
| 290 | return rc; |
| 291 | } |
| 292 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 293 | static int ntb_transport_bus_remove(struct device *dev) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 294 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 295 | const struct ntb_transport_client *client; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 296 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 297 | client = drv_client(dev->driver); |
| 298 | client->remove(dev); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 299 | |
| 300 | put_device(dev); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 305 | static struct bus_type ntb_transport_bus = { |
| 306 | .name = "ntb_transport", |
| 307 | .match = ntb_transport_bus_match, |
| 308 | .probe = ntb_transport_bus_probe, |
| 309 | .remove = ntb_transport_bus_remove, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | static LIST_HEAD(ntb_transport_list); |
| 313 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 314 | static int ntb_bus_init(struct ntb_transport_ctx *nt) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 315 | { |
Dave Jiang | 3151000 | 2015-07-13 08:07:19 -0400 | [diff] [blame] | 316 | list_add_tail(&nt->entry, &ntb_transport_list); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 320 | static void ntb_bus_remove(struct ntb_transport_ctx *nt) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 321 | { |
| 322 | struct ntb_transport_client_dev *client_dev, *cd; |
| 323 | |
| 324 | list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) { |
| 325 | dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n", |
| 326 | dev_name(&client_dev->dev)); |
| 327 | list_del(&client_dev->entry); |
| 328 | device_unregister(&client_dev->dev); |
| 329 | } |
| 330 | |
| 331 | list_del(&nt->entry); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 334 | static void ntb_transport_client_release(struct device *dev) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 335 | { |
| 336 | struct ntb_transport_client_dev *client_dev; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 337 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 338 | client_dev = dev_client_dev(dev); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 339 | kfree(client_dev); |
| 340 | } |
| 341 | |
| 342 | /** |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 343 | * ntb_transport_unregister_client_dev - Unregister NTB client device |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 344 | * @device_name: Name of NTB client device |
| 345 | * |
| 346 | * Unregister an NTB client device with the NTB transport layer |
| 347 | */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 348 | void ntb_transport_unregister_client_dev(char *device_name) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 349 | { |
| 350 | struct ntb_transport_client_dev *client, *cd; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 351 | struct ntb_transport_ctx *nt; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 352 | |
| 353 | list_for_each_entry(nt, &ntb_transport_list, entry) |
| 354 | list_for_each_entry_safe(client, cd, &nt->client_devs, entry) |
| 355 | if (!strncmp(dev_name(&client->dev), device_name, |
| 356 | strlen(device_name))) { |
| 357 | list_del(&client->entry); |
| 358 | device_unregister(&client->dev); |
| 359 | } |
| 360 | } |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 361 | EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 362 | |
| 363 | /** |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 364 | * ntb_transport_register_client_dev - Register NTB client device |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 365 | * @device_name: Name of NTB client device |
| 366 | * |
| 367 | * Register an NTB client device with the NTB transport layer |
| 368 | */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 369 | int ntb_transport_register_client_dev(char *device_name) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 370 | { |
| 371 | struct ntb_transport_client_dev *client_dev; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 372 | struct ntb_transport_ctx *nt; |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 373 | int node; |
Jon Mason | 8b19d45 | 2013-04-26 14:51:57 -0700 | [diff] [blame] | 374 | int rc, i = 0; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 375 | |
Jon Mason | 8222b40 | 2013-01-19 02:02:16 -0700 | [diff] [blame] | 376 | if (list_empty(&ntb_transport_list)) |
| 377 | return -ENODEV; |
| 378 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 379 | list_for_each_entry(nt, &ntb_transport_list, entry) { |
| 380 | struct device *dev; |
| 381 | |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 382 | node = dev_to_node(&nt->ndev->dev); |
| 383 | |
| 384 | client_dev = kzalloc_node(sizeof(*client_dev), |
| 385 | GFP_KERNEL, node); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 386 | if (!client_dev) { |
| 387 | rc = -ENOMEM; |
| 388 | goto err; |
| 389 | } |
| 390 | |
| 391 | dev = &client_dev->dev; |
| 392 | |
| 393 | /* setup and register client devices */ |
Jon Mason | 8b19d45 | 2013-04-26 14:51:57 -0700 | [diff] [blame] | 394 | dev_set_name(dev, "%s%d", device_name, i); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 395 | dev->bus = &ntb_transport_bus; |
| 396 | dev->release = ntb_transport_client_release; |
| 397 | dev->parent = &nt->ndev->dev; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 398 | |
| 399 | rc = device_register(dev); |
| 400 | if (rc) { |
| 401 | kfree(client_dev); |
| 402 | goto err; |
| 403 | } |
| 404 | |
| 405 | list_add_tail(&client_dev->entry, &nt->client_devs); |
Jon Mason | 8b19d45 | 2013-04-26 14:51:57 -0700 | [diff] [blame] | 406 | i++; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return 0; |
| 410 | |
| 411 | err: |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 412 | ntb_transport_unregister_client_dev(device_name); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 413 | |
| 414 | return rc; |
| 415 | } |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 416 | EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 417 | |
| 418 | /** |
Allen Hubbe | ec110bc | 2015-05-07 06:45:21 -0400 | [diff] [blame] | 419 | * ntb_transport_register_client - Register NTB client driver |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 420 | * @drv: NTB client driver to be registered |
| 421 | * |
| 422 | * Register an NTB client driver with the NTB transport layer |
| 423 | * |
| 424 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 425 | */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 426 | int ntb_transport_register_client(struct ntb_transport_client *drv) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 427 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 428 | drv->driver.bus = &ntb_transport_bus; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 429 | |
Jon Mason | 8222b40 | 2013-01-19 02:02:16 -0700 | [diff] [blame] | 430 | if (list_empty(&ntb_transport_list)) |
| 431 | return -ENODEV; |
| 432 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 433 | return driver_register(&drv->driver); |
| 434 | } |
Allen Hubbe | ec110bc | 2015-05-07 06:45:21 -0400 | [diff] [blame] | 435 | EXPORT_SYMBOL_GPL(ntb_transport_register_client); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 436 | |
| 437 | /** |
Allen Hubbe | ec110bc | 2015-05-07 06:45:21 -0400 | [diff] [blame] | 438 | * ntb_transport_unregister_client - Unregister NTB client driver |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 439 | * @drv: NTB client driver to be unregistered |
| 440 | * |
| 441 | * Unregister an NTB client driver with the NTB transport layer |
| 442 | * |
| 443 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 444 | */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 445 | void ntb_transport_unregister_client(struct ntb_transport_client *drv) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 446 | { |
| 447 | driver_unregister(&drv->driver); |
| 448 | } |
Allen Hubbe | ec110bc | 2015-05-07 06:45:21 -0400 | [diff] [blame] | 449 | EXPORT_SYMBOL_GPL(ntb_transport_unregister_client); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 450 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 451 | static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count, |
| 452 | loff_t *offp) |
| 453 | { |
| 454 | struct ntb_transport_qp *qp; |
Jon Mason | d7237e2 | 2013-01-19 02:02:25 -0700 | [diff] [blame] | 455 | char *buf; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 456 | ssize_t ret, out_offset, out_count; |
| 457 | |
Dave Jiang | 260bee9 | 2015-07-13 08:07:11 -0400 | [diff] [blame] | 458 | qp = filp->private_data; |
| 459 | |
| 460 | if (!qp || !qp->link_is_up) |
| 461 | return 0; |
| 462 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 463 | out_count = 1000; |
Jon Mason | d7237e2 | 2013-01-19 02:02:25 -0700 | [diff] [blame] | 464 | |
| 465 | buf = kmalloc(out_count, GFP_KERNEL); |
| 466 | if (!buf) |
| 467 | return -ENOMEM; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 468 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 469 | out_offset = 0; |
| 470 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Dave Jiang | d98ef99 | 2015-07-13 08:07:20 -0400 | [diff] [blame] | 471 | "\nNTB QP stats:\n\n"); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 472 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 473 | "rx_bytes - \t%llu\n", qp->rx_bytes); |
| 474 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 475 | "rx_pkts - \t%llu\n", qp->rx_pkts); |
| 476 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 477 | "rx_memcpy - \t%llu\n", qp->rx_memcpy); |
| 478 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 479 | "rx_async - \t%llu\n", qp->rx_async); |
| 480 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 481 | "rx_ring_empty - %llu\n", qp->rx_ring_empty); |
| 482 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 483 | "rx_err_no_buf - %llu\n", qp->rx_err_no_buf); |
| 484 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 485 | "rx_err_oflow - \t%llu\n", qp->rx_err_oflow); |
| 486 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 487 | "rx_err_ver - \t%llu\n", qp->rx_err_ver); |
| 488 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Dave Jiang | d98ef99 | 2015-07-13 08:07:20 -0400 | [diff] [blame] | 489 | "rx_buff - \t0x%p\n", qp->rx_buff); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 490 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 491 | "rx_index - \t%u\n", qp->rx_index); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 492 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Dave Jiang | a754a8f | 2016-04-08 10:49:06 -0700 | [diff] [blame] | 493 | "rx_max_entry - \t%u\n", qp->rx_max_entry); |
| 494 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 495 | "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 496 | |
| 497 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 498 | "tx_bytes - \t%llu\n", qp->tx_bytes); |
| 499 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 500 | "tx_pkts - \t%llu\n", qp->tx_pkts); |
| 501 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 502 | "tx_memcpy - \t%llu\n", qp->tx_memcpy); |
| 503 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 504 | "tx_async - \t%llu\n", qp->tx_async); |
| 505 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 506 | "tx_ring_full - \t%llu\n", qp->tx_ring_full); |
| 507 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 508 | "tx_err_no_buf - %llu\n", qp->tx_err_no_buf); |
| 509 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Dave Jiang | d98ef99 | 2015-07-13 08:07:20 -0400 | [diff] [blame] | 510 | "tx_mw - \t0x%p\n", qp->tx_mw); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 511 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Dave Jiang | d98ef99 | 2015-07-13 08:07:20 -0400 | [diff] [blame] | 512 | "tx_index (H) - \t%u\n", qp->tx_index); |
| 513 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 514 | "RRI (T) - \t%u\n", |
| 515 | qp->remote_rx_info->entry); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 516 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 517 | "tx_max_entry - \t%u\n", qp->tx_max_entry); |
Dave Jiang | e74bfee | 2015-07-13 08:07:17 -0400 | [diff] [blame] | 518 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Dave Jiang | e74bfee | 2015-07-13 08:07:17 -0400 | [diff] [blame] | 519 | "free tx - \t%u\n", |
| 520 | ntb_transport_tx_free_entry(qp)); |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 521 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 522 | "DMA tx prep err - \t%llu\n", |
| 523 | qp->dma_tx_prep_err); |
| 524 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 525 | "DMA rx prep err - \t%llu\n", |
| 526 | qp->dma_rx_prep_err); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 527 | |
| 528 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Dave Jiang | d98ef99 | 2015-07-13 08:07:20 -0400 | [diff] [blame] | 529 | "\n"); |
| 530 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 531 | "Using TX DMA - \t%s\n", |
| 532 | qp->tx_dma_chan ? "Yes" : "No"); |
| 533 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 534 | "Using RX DMA - \t%s\n", |
| 535 | qp->rx_dma_chan ? "Yes" : "No"); |
Dave Jiang | d98ef99 | 2015-07-13 08:07:20 -0400 | [diff] [blame] | 536 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 537 | "QP Link - \t%s\n", |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 538 | qp->link_is_up ? "Up" : "Down"); |
Dave Jiang | d98ef99 | 2015-07-13 08:07:20 -0400 | [diff] [blame] | 539 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 540 | "\n"); |
| 541 | |
Jon Mason | d7237e2 | 2013-01-19 02:02:25 -0700 | [diff] [blame] | 542 | if (out_offset > out_count) |
| 543 | out_offset = out_count; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 544 | |
| 545 | ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset); |
Jon Mason | d7237e2 | 2013-01-19 02:02:25 -0700 | [diff] [blame] | 546 | kfree(buf); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 547 | return ret; |
| 548 | } |
| 549 | |
| 550 | static const struct file_operations ntb_qp_debugfs_stats = { |
| 551 | .owner = THIS_MODULE, |
Jon Mason | d66d7ac | 2013-01-19 02:02:20 -0700 | [diff] [blame] | 552 | .open = simple_open, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 553 | .read = debugfs_read, |
| 554 | }; |
| 555 | |
| 556 | static void ntb_list_add(spinlock_t *lock, struct list_head *entry, |
| 557 | struct list_head *list) |
| 558 | { |
| 559 | unsigned long flags; |
| 560 | |
| 561 | spin_lock_irqsave(lock, flags); |
| 562 | list_add_tail(entry, list); |
| 563 | spin_unlock_irqrestore(lock, flags); |
| 564 | } |
| 565 | |
| 566 | static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock, |
Jon Mason | 53ca4fe | 2013-11-26 11:21:50 -0700 | [diff] [blame] | 567 | struct list_head *list) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 568 | { |
| 569 | struct ntb_queue_entry *entry; |
| 570 | unsigned long flags; |
| 571 | |
| 572 | spin_lock_irqsave(lock, flags); |
| 573 | if (list_empty(list)) { |
| 574 | entry = NULL; |
| 575 | goto out; |
| 576 | } |
| 577 | entry = list_first_entry(list, struct ntb_queue_entry, entry); |
| 578 | list_del(&entry->entry); |
Dave Jiang | e74bfee | 2015-07-13 08:07:17 -0400 | [diff] [blame] | 579 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 580 | out: |
| 581 | spin_unlock_irqrestore(lock, flags); |
| 582 | |
| 583 | return entry; |
| 584 | } |
| 585 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 586 | static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock, |
| 587 | struct list_head *list, |
| 588 | struct list_head *to_list) |
| 589 | { |
| 590 | struct ntb_queue_entry *entry; |
| 591 | unsigned long flags; |
| 592 | |
| 593 | spin_lock_irqsave(lock, flags); |
| 594 | |
| 595 | if (list_empty(list)) { |
| 596 | entry = NULL; |
| 597 | } else { |
| 598 | entry = list_first_entry(list, struct ntb_queue_entry, entry); |
| 599 | list_move_tail(&entry->entry, to_list); |
| 600 | } |
| 601 | |
| 602 | spin_unlock_irqrestore(lock, flags); |
| 603 | |
| 604 | return entry; |
| 605 | } |
| 606 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 607 | static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, |
| 608 | unsigned int qp_num) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 609 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 610 | struct ntb_transport_qp *qp = &nt->qp_vec[qp_num]; |
| 611 | struct ntb_transport_mw *mw; |
Dave Jiang | a754a8f | 2016-04-08 10:49:06 -0700 | [diff] [blame] | 612 | struct ntb_dev *ndev = nt->ndev; |
| 613 | struct ntb_queue_entry *entry; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 614 | unsigned int rx_size, num_qps_mw; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 615 | unsigned int mw_num, mw_count, qp_count; |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 616 | unsigned int i; |
Dave Jiang | a754a8f | 2016-04-08 10:49:06 -0700 | [diff] [blame] | 617 | int node; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 618 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 619 | mw_count = nt->mw_count; |
| 620 | qp_count = nt->qp_count; |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 621 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 622 | mw_num = QP_TO_MW(nt, qp_num); |
| 623 | mw = &nt->mw_vec[mw_num]; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 624 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 625 | if (!mw->virt_addr) |
| 626 | return -ENOMEM; |
| 627 | |
| 628 | if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count) |
| 629 | num_qps_mw = qp_count / mw_count + 1; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 630 | else |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 631 | num_qps_mw = qp_count / mw_count; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 632 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 633 | rx_size = (unsigned int)mw->xlat_size / num_qps_mw; |
Jon Mason | c92ba3c | 2015-10-04 22:54:22 -0400 | [diff] [blame] | 634 | qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 635 | rx_size -= sizeof(struct ntb_rx_info); |
| 636 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 637 | qp->remote_rx_info = qp->rx_buff + rx_size; |
| 638 | |
Jon Mason | c9d534c | 2013-02-01 15:45:16 -0700 | [diff] [blame] | 639 | /* Due to housekeeping, there must be atleast 2 buffs */ |
| 640 | qp->rx_max_frame = min(transport_mtu, rx_size / 2); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 641 | qp->rx_max_entry = rx_size / qp->rx_max_frame; |
| 642 | qp->rx_index = 0; |
| 643 | |
Dave Jiang | a754a8f | 2016-04-08 10:49:06 -0700 | [diff] [blame] | 644 | /* |
| 645 | * Checking to see if we have more entries than the default. |
| 646 | * We should add additional entries if that is the case so we |
| 647 | * can be in sync with the transport frames. |
| 648 | */ |
| 649 | node = dev_to_node(&ndev->dev); |
| 650 | for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) { |
| 651 | entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node); |
| 652 | if (!entry) |
| 653 | return -ENOMEM; |
| 654 | |
| 655 | entry->qp = qp; |
| 656 | ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, |
| 657 | &qp->rx_free_q); |
| 658 | qp->rx_alloc_entry++; |
| 659 | } |
| 660 | |
Jon Mason | c9d534c | 2013-02-01 15:45:16 -0700 | [diff] [blame] | 661 | qp->remote_rx_info->entry = qp->rx_max_entry - 1; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 662 | |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 663 | /* setup the hdr offsets with 0's */ |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 664 | for (i = 0; i < qp->rx_max_entry; i++) { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 665 | void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) - |
| 666 | sizeof(struct ntb_payload_header)); |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 667 | memset(offset, 0, sizeof(struct ntb_payload_header)); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 668 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 669 | |
| 670 | qp->rx_pkts = 0; |
| 671 | qp->tx_pkts = 0; |
Jon Mason | 90f9e93 | 2013-02-01 15:34:35 -0700 | [diff] [blame] | 672 | qp->tx_index = 0; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 673 | |
| 674 | return 0; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 677 | static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw) |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 678 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 679 | struct ntb_transport_mw *mw = &nt->mw_vec[num_mw]; |
| 680 | struct pci_dev *pdev = nt->ndev->pdev; |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 681 | |
| 682 | if (!mw->virt_addr) |
| 683 | return; |
| 684 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 685 | ntb_mw_clear_trans(nt->ndev, num_mw); |
| 686 | dma_free_coherent(&pdev->dev, mw->buff_size, |
| 687 | mw->virt_addr, mw->dma_addr); |
| 688 | mw->xlat_size = 0; |
| 689 | mw->buff_size = 0; |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 690 | mw->virt_addr = NULL; |
| 691 | } |
| 692 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 693 | static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw, |
Allen Hubbe | 8c9edf6 | 2015-07-13 08:07:13 -0400 | [diff] [blame] | 694 | resource_size_t size) |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 695 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 696 | struct ntb_transport_mw *mw = &nt->mw_vec[num_mw]; |
| 697 | struct pci_dev *pdev = nt->ndev->pdev; |
Allen Hubbe | 8c9edf6 | 2015-07-13 08:07:13 -0400 | [diff] [blame] | 698 | size_t xlat_size, buff_size; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 699 | int rc; |
| 700 | |
Allen Hubbe | 8c9edf6 | 2015-07-13 08:07:13 -0400 | [diff] [blame] | 701 | if (!size) |
| 702 | return -EINVAL; |
| 703 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 704 | xlat_size = round_up(size, mw->xlat_align_size); |
| 705 | buff_size = round_up(size, mw->xlat_align); |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 706 | |
| 707 | /* No need to re-setup */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 708 | if (mw->xlat_size == xlat_size) |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 709 | return 0; |
| 710 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 711 | if (mw->buff_size) |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 712 | ntb_free_mw(nt, num_mw); |
| 713 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 714 | /* Alloc memory for receiving data. Must be aligned */ |
| 715 | mw->xlat_size = xlat_size; |
| 716 | mw->buff_size = buff_size; |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 717 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 718 | mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size, |
| 719 | &mw->dma_addr, GFP_KERNEL); |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 720 | if (!mw->virt_addr) { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 721 | mw->xlat_size = 0; |
| 722 | mw->buff_size = 0; |
Allen Hubbe | 8c9edf6 | 2015-07-13 08:07:13 -0400 | [diff] [blame] | 723 | dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n", |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 724 | buff_size); |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 725 | return -ENOMEM; |
| 726 | } |
| 727 | |
Dave Jiang | 3cc5ba1 | 2014-08-28 13:53:02 -0700 | [diff] [blame] | 728 | /* |
| 729 | * we must ensure that the memory address allocated is BAR size |
| 730 | * aligned in order for the XLAT register to take the value. This |
| 731 | * is a requirement of the hardware. It is recommended to setup CMA |
| 732 | * for BAR sizes equal or greater than 4MB. |
| 733 | */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 734 | if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) { |
| 735 | dev_err(&pdev->dev, "DMA memory %pad is not aligned\n", |
Dave Jiang | 3cc5ba1 | 2014-08-28 13:53:02 -0700 | [diff] [blame] | 736 | &mw->dma_addr); |
| 737 | ntb_free_mw(nt, num_mw); |
| 738 | return -ENOMEM; |
| 739 | } |
| 740 | |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 741 | /* Notify HW the memory location of the receive buffer */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 742 | rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size); |
| 743 | if (rc) { |
| 744 | dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw); |
| 745 | ntb_free_mw(nt, num_mw); |
| 746 | return -EIO; |
| 747 | } |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
Allen Hubbe | 2849b5d7 | 2015-05-12 08:09:15 -0400 | [diff] [blame] | 752 | static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp) |
| 753 | { |
| 754 | qp->link_is_up = false; |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 755 | qp->active = false; |
Allen Hubbe | 2849b5d7 | 2015-05-12 08:09:15 -0400 | [diff] [blame] | 756 | |
| 757 | qp->tx_index = 0; |
| 758 | qp->rx_index = 0; |
| 759 | qp->rx_bytes = 0; |
| 760 | qp->rx_pkts = 0; |
| 761 | qp->rx_ring_empty = 0; |
| 762 | qp->rx_err_no_buf = 0; |
| 763 | qp->rx_err_oflow = 0; |
| 764 | qp->rx_err_ver = 0; |
| 765 | qp->rx_memcpy = 0; |
| 766 | qp->rx_async = 0; |
| 767 | qp->tx_bytes = 0; |
| 768 | qp->tx_pkts = 0; |
| 769 | qp->tx_ring_full = 0; |
| 770 | qp->tx_err_no_buf = 0; |
| 771 | qp->tx_memcpy = 0; |
| 772 | qp->tx_async = 0; |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 773 | qp->dma_tx_prep_err = 0; |
| 774 | qp->dma_rx_prep_err = 0; |
Allen Hubbe | 2849b5d7 | 2015-05-12 08:09:15 -0400 | [diff] [blame] | 775 | } |
| 776 | |
Jon Mason | fca4d51 | 2013-09-09 13:39:55 -0700 | [diff] [blame] | 777 | static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 778 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 779 | struct ntb_transport_ctx *nt = qp->transport; |
| 780 | struct pci_dev *pdev = nt->ndev->pdev; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 781 | |
Allen Hubbe | e22e0b9 | 2015-05-12 06:55:44 -0400 | [diff] [blame] | 782 | dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num); |
Allen Hubbe | 2849b5d7 | 2015-05-12 08:09:15 -0400 | [diff] [blame] | 783 | |
| 784 | cancel_delayed_work_sync(&qp->link_work); |
| 785 | ntb_qp_link_down_reset(qp); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 786 | |
| 787 | if (qp->event_handler) |
| 788 | qp->event_handler(qp->cb_data, qp->link_is_up); |
Jon Mason | fca4d51 | 2013-09-09 13:39:55 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static void ntb_qp_link_cleanup_work(struct work_struct *work) |
| 792 | { |
| 793 | struct ntb_transport_qp *qp = container_of(work, |
| 794 | struct ntb_transport_qp, |
| 795 | link_cleanup); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 796 | struct ntb_transport_ctx *nt = qp->transport; |
Jon Mason | fca4d51 | 2013-09-09 13:39:55 -0700 | [diff] [blame] | 797 | |
| 798 | ntb_qp_link_cleanup(qp); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 799 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 800 | if (nt->link_is_up) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 801 | schedule_delayed_work(&qp->link_work, |
| 802 | msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT)); |
| 803 | } |
| 804 | |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 805 | static void ntb_qp_link_down(struct ntb_transport_qp *qp) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 806 | { |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 807 | schedule_work(&qp->link_cleanup); |
| 808 | } |
| 809 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 810 | static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt) |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 811 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 812 | struct ntb_transport_qp *qp; |
| 813 | u64 qp_bitmap_alloc; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 814 | int i; |
| 815 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 816 | qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free; |
Jon Mason | fca4d51 | 2013-09-09 13:39:55 -0700 | [diff] [blame] | 817 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 818 | /* Pass along the info to any clients */ |
| 819 | for (i = 0; i < nt->qp_count; i++) |
| 820 | if (qp_bitmap_alloc & BIT_ULL(i)) { |
| 821 | qp = &nt->qp_vec[i]; |
| 822 | ntb_qp_link_cleanup(qp); |
| 823 | cancel_work_sync(&qp->link_cleanup); |
| 824 | cancel_delayed_work_sync(&qp->link_work); |
| 825 | } |
| 826 | |
| 827 | if (!nt->link_is_up) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 828 | cancel_delayed_work_sync(&nt->link_work); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 829 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 830 | /* The scratchpad registers keep the values if the remote side |
| 831 | * goes down, blast them now to give them a sane value the next |
| 832 | * time they are accessed |
| 833 | */ |
| 834 | for (i = 0; i < MAX_SPAD; i++) |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 835 | ntb_spad_write(nt->ndev, i, 0); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Jon Mason | fca4d51 | 2013-09-09 13:39:55 -0700 | [diff] [blame] | 838 | static void ntb_transport_link_cleanup_work(struct work_struct *work) |
| 839 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 840 | struct ntb_transport_ctx *nt = |
| 841 | container_of(work, struct ntb_transport_ctx, link_cleanup); |
Jon Mason | fca4d51 | 2013-09-09 13:39:55 -0700 | [diff] [blame] | 842 | |
| 843 | ntb_transport_link_cleanup(nt); |
| 844 | } |
| 845 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 846 | static void ntb_transport_event_callback(void *data) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 847 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 848 | struct ntb_transport_ctx *nt = data; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 849 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 850 | if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 851 | schedule_delayed_work(&nt->link_work, 0); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 852 | else |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 853 | schedule_work(&nt->link_cleanup); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static void ntb_transport_link_work(struct work_struct *work) |
| 857 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 858 | struct ntb_transport_ctx *nt = |
| 859 | container_of(work, struct ntb_transport_ctx, link_work.work); |
| 860 | struct ntb_dev *ndev = nt->ndev; |
| 861 | struct pci_dev *pdev = ndev->pdev; |
| 862 | resource_size_t size; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 863 | u32 val; |
Dave Jiang | 84f7668 | 2016-02-29 09:35:26 -0700 | [diff] [blame] | 864 | int rc = 0, i, spad; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 865 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 866 | /* send the local info, in the opposite order of the way we read it */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 867 | for (i = 0; i < nt->mw_count; i++) { |
| 868 | size = nt->mw_vec[i].phys_size; |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 869 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 870 | if (max_mw_size && size > max_mw_size) |
| 871 | size = max_mw_size; |
| 872 | |
| 873 | spad = MW0_SZ_HIGH + (i * 2); |
Arnd Bergmann | fdcb4b2 | 2015-10-07 09:59:34 +0200 | [diff] [blame] | 874 | ntb_peer_spad_write(ndev, spad, upper_32_bits(size)); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 875 | |
| 876 | spad = MW0_SZ_LOW + (i * 2); |
Arnd Bergmann | fdcb4b2 | 2015-10-07 09:59:34 +0200 | [diff] [blame] | 877 | ntb_peer_spad_write(ndev, spad, lower_32_bits(size)); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 880 | ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 881 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 882 | ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 883 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 884 | ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 885 | |
| 886 | /* Query the remote side for its info */ |
Dave Jiang | 0f69a7d | 2015-06-02 03:45:07 -0400 | [diff] [blame] | 887 | val = ntb_spad_read(ndev, VERSION); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 888 | dev_dbg(&pdev->dev, "Remote version = %d\n", val); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 889 | if (val != NTB_TRANSPORT_VERSION) |
| 890 | goto out; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 891 | |
Dave Jiang | 0f69a7d | 2015-06-02 03:45:07 -0400 | [diff] [blame] | 892 | val = ntb_spad_read(ndev, NUM_QPS); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 893 | dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 894 | if (val != nt->qp_count) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 895 | goto out; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 896 | |
Dave Jiang | 0f69a7d | 2015-06-02 03:45:07 -0400 | [diff] [blame] | 897 | val = ntb_spad_read(ndev, NUM_MWS); |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 898 | dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 899 | if (val != nt->mw_count) |
| 900 | goto out; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 901 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 902 | for (i = 0; i < nt->mw_count; i++) { |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 903 | u64 val64; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 904 | |
Dave Jiang | 0f69a7d | 2015-06-02 03:45:07 -0400 | [diff] [blame] | 905 | val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2)); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 906 | val64 = (u64)val << 32; |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 907 | |
Dave Jiang | 0f69a7d | 2015-06-02 03:45:07 -0400 | [diff] [blame] | 908 | val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2)); |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 909 | val64 |= val; |
| 910 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 911 | dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64); |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 912 | |
| 913 | rc = ntb_set_mw(nt, i, val64); |
| 914 | if (rc) |
| 915 | goto out1; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 918 | nt->link_is_up = true; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 919 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 920 | for (i = 0; i < nt->qp_count; i++) { |
| 921 | struct ntb_transport_qp *qp = &nt->qp_vec[i]; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 922 | |
| 923 | ntb_transport_setup_qp_mw(nt, i); |
| 924 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 925 | if (qp->client_ready) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 926 | schedule_delayed_work(&qp->link_work, 0); |
| 927 | } |
| 928 | |
| 929 | return; |
| 930 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 931 | out1: |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 932 | for (i = 0; i < nt->mw_count; i++) |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 933 | ntb_free_mw(nt, i); |
Dave Jiang | 84f7668 | 2016-02-29 09:35:26 -0700 | [diff] [blame] | 934 | |
| 935 | /* if there's an actual failure, we should just bail */ |
| 936 | if (rc < 0) { |
| 937 | ntb_link_disable(ndev); |
| 938 | return; |
| 939 | } |
| 940 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 941 | out: |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 942 | if (ntb_link_is_up(ndev, NULL, NULL) == 1) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 943 | schedule_delayed_work(&nt->link_work, |
| 944 | msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT)); |
| 945 | } |
| 946 | |
| 947 | static void ntb_qp_link_work(struct work_struct *work) |
| 948 | { |
| 949 | struct ntb_transport_qp *qp = container_of(work, |
| 950 | struct ntb_transport_qp, |
| 951 | link_work.work); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 952 | struct pci_dev *pdev = qp->ndev->pdev; |
| 953 | struct ntb_transport_ctx *nt = qp->transport; |
| 954 | int val; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 955 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 956 | WARN_ON(!nt->link_is_up); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 957 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 958 | val = ntb_spad_read(nt->ndev, QP_LINKS); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 959 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 960 | ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num)); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 961 | |
| 962 | /* query remote spad for qp ready bits */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 963 | ntb_peer_spad_read(nt->ndev, QP_LINKS); |
Allen Hubbe | 2876228 | 2015-05-11 10:08:26 -0400 | [diff] [blame] | 964 | dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 965 | |
| 966 | /* See if the remote side is up */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 967 | if (val & BIT(qp->qp_num)) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 968 | dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 969 | qp->link_is_up = true; |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 970 | qp->active = true; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 971 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 972 | if (qp->event_handler) |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 973 | qp->event_handler(qp->cb_data, qp->link_is_up); |
Allen Hubbe | 8b5a22d | 2015-07-13 08:07:12 -0400 | [diff] [blame] | 974 | |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 975 | if (qp->active) |
| 976 | tasklet_schedule(&qp->rxc_db_work); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 977 | } else if (nt->link_is_up) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 978 | schedule_delayed_work(&qp->link_work, |
| 979 | msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT)); |
| 980 | } |
| 981 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 982 | static int ntb_transport_init_queue(struct ntb_transport_ctx *nt, |
Jon Mason | 53ca4fe | 2013-11-26 11:21:50 -0700 | [diff] [blame] | 983 | unsigned int qp_num) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 984 | { |
| 985 | struct ntb_transport_qp *qp; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 986 | phys_addr_t mw_base; |
| 987 | resource_size_t mw_size; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 988 | unsigned int num_qps_mw, tx_size; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 989 | unsigned int mw_num, mw_count, qp_count; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 990 | u64 qp_offset; |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 991 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 992 | mw_count = nt->mw_count; |
| 993 | qp_count = nt->qp_count; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 994 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 995 | mw_num = QP_TO_MW(nt, qp_num); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 996 | |
| 997 | qp = &nt->qp_vec[qp_num]; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 998 | qp->qp_num = qp_num; |
| 999 | qp->transport = nt; |
| 1000 | qp->ndev = nt->ndev; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1001 | qp->client_ready = false; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1002 | qp->event_handler = NULL; |
Allen Hubbe | 2849b5d7 | 2015-05-12 08:09:15 -0400 | [diff] [blame] | 1003 | ntb_qp_link_down_reset(qp); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1004 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1005 | if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count) |
| 1006 | num_qps_mw = qp_count / mw_count + 1; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 1007 | else |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1008 | num_qps_mw = qp_count / mw_count; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 1009 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1010 | mw_base = nt->mw_vec[mw_num].phys_addr; |
| 1011 | mw_size = nt->mw_vec[mw_num].phys_size; |
| 1012 | |
| 1013 | tx_size = (unsigned int)mw_size / num_qps_mw; |
Jon Mason | c92ba3c | 2015-10-04 22:54:22 -0400 | [diff] [blame] | 1014 | qp_offset = tx_size * (qp_num / mw_count); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1015 | |
| 1016 | qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1017 | if (!qp->tx_mw) |
| 1018 | return -EINVAL; |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1019 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1020 | qp->tx_mw_phys = mw_base + qp_offset; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1021 | if (!qp->tx_mw_phys) |
| 1022 | return -EINVAL; |
| 1023 | |
| 1024 | tx_size -= sizeof(struct ntb_rx_info); |
| 1025 | qp->rx_info = qp->tx_mw + tx_size; |
| 1026 | |
Jon Mason | c9d534c | 2013-02-01 15:45:16 -0700 | [diff] [blame] | 1027 | /* Due to housekeeping, there must be atleast 2 buffs */ |
| 1028 | qp->tx_max_frame = min(transport_mtu, tx_size / 2); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1029 | qp->tx_max_entry = tx_size / qp->tx_max_frame; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 1030 | |
Dave Jiang | c8650fd | 2015-07-13 08:07:09 -0400 | [diff] [blame] | 1031 | if (nt->debugfs_node_dir) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1032 | char debugfs_name[4]; |
| 1033 | |
| 1034 | snprintf(debugfs_name, 4, "qp%d", qp_num); |
| 1035 | qp->debugfs_dir = debugfs_create_dir(debugfs_name, |
Dave Jiang | c8650fd | 2015-07-13 08:07:09 -0400 | [diff] [blame] | 1036 | nt->debugfs_node_dir); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1037 | |
| 1038 | qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR, |
| 1039 | qp->debugfs_dir, qp, |
| 1040 | &ntb_qp_debugfs_stats); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1041 | } else { |
| 1042 | qp->debugfs_dir = NULL; |
| 1043 | qp->debugfs_stats = NULL; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work); |
Jon Mason | fca4d51 | 2013-09-09 13:39:55 -0700 | [diff] [blame] | 1047 | INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1048 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1049 | spin_lock_init(&qp->ntb_rx_q_lock); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1050 | spin_lock_init(&qp->ntb_tx_free_q_lock); |
| 1051 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1052 | INIT_LIST_HEAD(&qp->rx_post_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1053 | INIT_LIST_HEAD(&qp->rx_pend_q); |
| 1054 | INIT_LIST_HEAD(&qp->rx_free_q); |
| 1055 | INIT_LIST_HEAD(&qp->tx_free_q); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1056 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1057 | tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db, |
| 1058 | (unsigned long)qp); |
| 1059 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1060 | return 0; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1063 | static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1064 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1065 | struct ntb_transport_ctx *nt; |
| 1066 | struct ntb_transport_mw *mw; |
| 1067 | unsigned int mw_count, qp_count; |
| 1068 | u64 qp_bitmap; |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1069 | int node; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1070 | int rc, i; |
| 1071 | |
Logan Gunthorpe | 19645a0 | 2016-06-07 11:20:22 -0600 | [diff] [blame] | 1072 | mw_count = ntb_mw_count(ndev); |
| 1073 | if (ntb_spad_count(ndev) < (NUM_MWS + 1 + mw_count * 2)) { |
| 1074 | dev_err(&ndev->dev, "Not enough scratch pad registers for %s", |
| 1075 | NTB_TRANSPORT_NAME); |
| 1076 | return -EIO; |
| 1077 | } |
| 1078 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1079 | if (ntb_db_is_unsafe(ndev)) |
| 1080 | dev_dbg(&ndev->dev, |
| 1081 | "doorbell is unsafe, proceed anyway...\n"); |
| 1082 | if (ntb_spad_is_unsafe(ndev)) |
| 1083 | dev_dbg(&ndev->dev, |
| 1084 | "scratchpad is unsafe, proceed anyway...\n"); |
| 1085 | |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1086 | node = dev_to_node(&ndev->dev); |
| 1087 | |
| 1088 | nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1089 | if (!nt) |
| 1090 | return -ENOMEM; |
| 1091 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1092 | nt->ndev = ndev; |
| 1093 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1094 | nt->mw_count = mw_count; |
| 1095 | |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1096 | nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec), |
| 1097 | GFP_KERNEL, node); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1098 | if (!nt->mw_vec) { |
| 1099 | rc = -ENOMEM; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1100 | goto err; |
| 1101 | } |
| 1102 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1103 | for (i = 0; i < mw_count; i++) { |
| 1104 | mw = &nt->mw_vec[i]; |
| 1105 | |
| 1106 | rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size, |
| 1107 | &mw->xlat_align, &mw->xlat_align_size); |
| 1108 | if (rc) |
| 1109 | goto err1; |
| 1110 | |
Dave Jiang | 06917f7 | 2015-05-19 16:45:46 -0400 | [diff] [blame] | 1111 | mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1112 | if (!mw->vbase) { |
| 1113 | rc = -ENOMEM; |
| 1114 | goto err1; |
| 1115 | } |
| 1116 | |
| 1117 | mw->buff_size = 0; |
| 1118 | mw->xlat_size = 0; |
| 1119 | mw->virt_addr = NULL; |
| 1120 | mw->dma_addr = 0; |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1123 | qp_bitmap = ntb_db_valid_mask(ndev); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1124 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1125 | qp_count = ilog2(qp_bitmap); |
| 1126 | if (max_num_clients && max_num_clients < qp_count) |
| 1127 | qp_count = max_num_clients; |
| 1128 | else if (mw_count < qp_count) |
| 1129 | qp_count = mw_count; |
| 1130 | |
| 1131 | qp_bitmap &= BIT_ULL(qp_count) - 1; |
| 1132 | |
| 1133 | nt->qp_count = qp_count; |
| 1134 | nt->qp_bitmap = qp_bitmap; |
| 1135 | nt->qp_bitmap_free = qp_bitmap; |
| 1136 | |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1137 | nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec), |
| 1138 | GFP_KERNEL, node); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1139 | if (!nt->qp_vec) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1140 | rc = -ENOMEM; |
Sudip Mukherjee | d4adee0 | 2015-10-03 13:39:41 +0530 | [diff] [blame] | 1141 | goto err1; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Dave Jiang | c8650fd | 2015-07-13 08:07:09 -0400 | [diff] [blame] | 1144 | if (nt_debugfs_dir) { |
| 1145 | nt->debugfs_node_dir = |
| 1146 | debugfs_create_dir(pci_name(ndev->pdev), |
| 1147 | nt_debugfs_dir); |
| 1148 | } |
| 1149 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1150 | for (i = 0; i < qp_count; i++) { |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1151 | rc = ntb_transport_init_queue(nt, i); |
| 1152 | if (rc) |
Sudip Mukherjee | d4adee0 | 2015-10-03 13:39:41 +0530 | [diff] [blame] | 1153 | goto err2; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1154 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1155 | |
| 1156 | INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work); |
Jon Mason | fca4d51 | 2013-09-09 13:39:55 -0700 | [diff] [blame] | 1157 | INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1158 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1159 | rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1160 | if (rc) |
Sudip Mukherjee | d4adee0 | 2015-10-03 13:39:41 +0530 | [diff] [blame] | 1161 | goto err2; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1162 | |
| 1163 | INIT_LIST_HEAD(&nt->client_devs); |
| 1164 | rc = ntb_bus_init(nt); |
| 1165 | if (rc) |
Sudip Mukherjee | d4adee0 | 2015-10-03 13:39:41 +0530 | [diff] [blame] | 1166 | goto err3; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1167 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1168 | nt->link_is_up = false; |
| 1169 | ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO); |
| 1170 | ntb_link_event(ndev); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1171 | |
| 1172 | return 0; |
| 1173 | |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 1174 | err3: |
Sudip Mukherjee | d4adee0 | 2015-10-03 13:39:41 +0530 | [diff] [blame] | 1175 | ntb_clear_ctx(ndev); |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 1176 | err2: |
Sudip Mukherjee | d4adee0 | 2015-10-03 13:39:41 +0530 | [diff] [blame] | 1177 | kfree(nt->qp_vec); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1178 | err1: |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1179 | while (i--) { |
| 1180 | mw = &nt->mw_vec[i]; |
| 1181 | iounmap(mw->vbase); |
| 1182 | } |
Sudip Mukherjee | d4adee0 | 2015-10-03 13:39:41 +0530 | [diff] [blame] | 1183 | kfree(nt->mw_vec); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1184 | err: |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1185 | kfree(nt); |
| 1186 | return rc; |
| 1187 | } |
| 1188 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1189 | static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1190 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1191 | struct ntb_transport_ctx *nt = ndev->ctx; |
| 1192 | struct ntb_transport_qp *qp; |
| 1193 | u64 qp_bitmap_alloc; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1194 | int i; |
| 1195 | |
Jon Mason | fca4d51 | 2013-09-09 13:39:55 -0700 | [diff] [blame] | 1196 | ntb_transport_link_cleanup(nt); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1197 | cancel_work_sync(&nt->link_cleanup); |
| 1198 | cancel_delayed_work_sync(&nt->link_work); |
| 1199 | |
| 1200 | qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1201 | |
| 1202 | /* verify that all the qp's are freed */ |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1203 | for (i = 0; i < nt->qp_count; i++) { |
| 1204 | qp = &nt->qp_vec[i]; |
| 1205 | if (qp_bitmap_alloc & BIT_ULL(i)) |
| 1206 | ntb_transport_free_queue(qp); |
| 1207 | debugfs_remove_recursive(qp->debugfs_dir); |
Jon Mason | 1517a3f | 2013-07-30 15:58:49 -0700 | [diff] [blame] | 1208 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1209 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1210 | ntb_link_disable(ndev); |
| 1211 | ntb_clear_ctx(ndev); |
| 1212 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1213 | ntb_bus_remove(nt); |
| 1214 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1215 | for (i = nt->mw_count; i--; ) { |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 1216 | ntb_free_mw(nt, i); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1217 | iounmap(nt->mw_vec[i].vbase); |
| 1218 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1219 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1220 | kfree(nt->qp_vec); |
| 1221 | kfree(nt->mw_vec); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1222 | kfree(nt); |
| 1223 | } |
| 1224 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1225 | static void ntb_complete_rxc(struct ntb_transport_qp *qp) |
| 1226 | { |
| 1227 | struct ntb_queue_entry *entry; |
| 1228 | void *cb_data; |
| 1229 | unsigned int len; |
| 1230 | unsigned long irqflags; |
| 1231 | |
| 1232 | spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags); |
| 1233 | |
| 1234 | while (!list_empty(&qp->rx_post_q)) { |
| 1235 | entry = list_first_entry(&qp->rx_post_q, |
| 1236 | struct ntb_queue_entry, entry); |
| 1237 | if (!(entry->flags & DESC_DONE_FLAG)) |
| 1238 | break; |
| 1239 | |
| 1240 | entry->rx_hdr->flags = 0; |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1241 | iowrite32(entry->rx_index, &qp->rx_info->entry); |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1242 | |
| 1243 | cb_data = entry->cb_data; |
| 1244 | len = entry->len; |
| 1245 | |
| 1246 | list_move_tail(&entry->entry, &qp->rx_free_q); |
| 1247 | |
| 1248 | spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags); |
| 1249 | |
| 1250 | if (qp->rx_handler && qp->client_ready) |
| 1251 | qp->rx_handler(qp, qp->cb_data, cb_data, len); |
| 1252 | |
| 1253 | spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags); |
| 1254 | } |
| 1255 | |
| 1256 | spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags); |
| 1257 | } |
| 1258 | |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1259 | static void ntb_rx_copy_callback(void *data, |
| 1260 | const struct dmaengine_result *res) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1261 | { |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1262 | struct ntb_queue_entry *entry = data; |
Jon Mason | 448c6fb | 2013-01-19 02:02:27 -0700 | [diff] [blame] | 1263 | |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1264 | /* we need to check DMA results if we are using DMA */ |
| 1265 | if (res) { |
| 1266 | enum dmaengine_tx_result dma_err = res->result; |
| 1267 | |
| 1268 | switch (dma_err) { |
| 1269 | case DMA_TRANS_READ_FAILED: |
| 1270 | case DMA_TRANS_WRITE_FAILED: |
| 1271 | entry->errors++; |
| 1272 | case DMA_TRANS_ABORTED: |
| 1273 | { |
| 1274 | struct ntb_transport_qp *qp = entry->qp; |
| 1275 | void *offset = qp->rx_buff + qp->rx_max_frame * |
| 1276 | qp->rx_index; |
| 1277 | |
| 1278 | ntb_memcpy_rx(entry, offset); |
| 1279 | qp->rx_memcpy++; |
| 1280 | return; |
| 1281 | } |
| 1282 | |
| 1283 | case DMA_TRANS_NOERROR: |
| 1284 | default: |
| 1285 | break; |
| 1286 | } |
| 1287 | } |
| 1288 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1289 | entry->flags |= DESC_DONE_FLAG; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1290 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1291 | ntb_complete_rxc(entry->qp); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1294 | static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset) |
| 1295 | { |
| 1296 | void *buf = entry->buf; |
| 1297 | size_t len = entry->len; |
| 1298 | |
| 1299 | memcpy(buf, offset, len); |
| 1300 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1301 | /* Ensure that the data is fully copied out before clearing the flag */ |
| 1302 | wmb(); |
| 1303 | |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1304 | ntb_rx_copy_callback(entry, NULL); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1307 | static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset) |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1308 | { |
| 1309 | struct dma_async_tx_descriptor *txd; |
| 1310 | struct ntb_transport_qp *qp = entry->qp; |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 1311 | struct dma_chan *chan = qp->rx_dma_chan; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1312 | struct dma_device *device; |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1313 | size_t pay_off, buff_off, len; |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1314 | struct dmaengine_unmap_data *unmap; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1315 | dma_cookie_t cookie; |
| 1316 | void *buf = entry->buf; |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 1317 | int retries = 0; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1318 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1319 | len = entry->len; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1320 | device = chan->device; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1321 | pay_off = (size_t)offset & ~PAGE_MASK; |
| 1322 | buff_off = (size_t)buf & ~PAGE_MASK; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1323 | |
| 1324 | if (!is_dma_copy_aligned(device, pay_off, buff_off, len)) |
Allen Hubbe | 905921e | 2015-07-13 08:07:21 -0400 | [diff] [blame] | 1325 | goto err; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1326 | |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1327 | unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT); |
| 1328 | if (!unmap) |
Allen Hubbe | 905921e | 2015-07-13 08:07:21 -0400 | [diff] [blame] | 1329 | goto err; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1330 | |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1331 | unmap->len = len; |
| 1332 | unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset), |
| 1333 | pay_off, len, DMA_TO_DEVICE); |
| 1334 | if (dma_mapping_error(device->dev, unmap->addr[0])) |
| 1335 | goto err_get_unmap; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1336 | |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1337 | unmap->to_cnt = 1; |
| 1338 | |
| 1339 | unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf), |
| 1340 | buff_off, len, DMA_FROM_DEVICE); |
| 1341 | if (dma_mapping_error(device->dev, unmap->addr[1])) |
| 1342 | goto err_get_unmap; |
| 1343 | |
| 1344 | unmap->from_cnt = 1; |
| 1345 | |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 1346 | for (retries = 0; retries < DMA_RETRIES; retries++) { |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1347 | txd = device->device_prep_dma_memcpy(chan, |
| 1348 | unmap->addr[1], |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 1349 | unmap->addr[0], len, |
| 1350 | DMA_PREP_INTERRUPT); |
| 1351 | if (txd) |
| 1352 | break; |
| 1353 | |
| 1354 | set_current_state(TASK_INTERRUPTIBLE); |
| 1355 | schedule_timeout(DMA_OUT_RESOURCE_TO); |
| 1356 | } |
| 1357 | |
| 1358 | if (!txd) { |
| 1359 | qp->dma_rx_prep_err++; |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1360 | goto err_get_unmap; |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 1361 | } |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1362 | |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1363 | txd->callback_result = ntb_rx_copy_callback; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1364 | txd->callback_param = entry; |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1365 | dma_set_unmap(txd, unmap); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1366 | |
| 1367 | cookie = dmaengine_submit(txd); |
| 1368 | if (dma_submit_error(cookie)) |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1369 | goto err_set_unmap; |
| 1370 | |
| 1371 | dmaengine_unmap_put(unmap); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1372 | |
| 1373 | qp->last_cookie = cookie; |
| 1374 | |
| 1375 | qp->rx_async++; |
| 1376 | |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1377 | return 0; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1378 | |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1379 | err_set_unmap: |
| 1380 | dmaengine_unmap_put(unmap); |
| 1381 | err_get_unmap: |
| 1382 | dmaengine_unmap_put(unmap); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1383 | err: |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1384 | return -ENXIO; |
| 1385 | } |
| 1386 | |
| 1387 | static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset) |
| 1388 | { |
| 1389 | struct ntb_transport_qp *qp = entry->qp; |
| 1390 | struct dma_chan *chan = qp->rx_dma_chan; |
| 1391 | int res; |
| 1392 | |
| 1393 | if (!chan) |
| 1394 | goto err; |
| 1395 | |
| 1396 | if (entry->len < copy_bytes) |
| 1397 | goto err; |
| 1398 | |
| 1399 | res = ntb_async_rx_submit(entry, offset); |
| 1400 | if (res < 0) |
| 1401 | goto err; |
| 1402 | |
| 1403 | if (!entry->retries) |
| 1404 | qp->rx_async++; |
| 1405 | |
| 1406 | return; |
| 1407 | |
| 1408 | err: |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1409 | ntb_memcpy_rx(entry, offset); |
| 1410 | qp->rx_memcpy++; |
| 1411 | } |
| 1412 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1413 | static int ntb_process_rxc(struct ntb_transport_qp *qp) |
| 1414 | { |
| 1415 | struct ntb_payload_header *hdr; |
| 1416 | struct ntb_queue_entry *entry; |
| 1417 | void *offset; |
| 1418 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1419 | offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index; |
| 1420 | hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header); |
| 1421 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1422 | dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n", |
| 1423 | qp->qp_num, hdr->ver, hdr->len, hdr->flags); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1424 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1425 | if (!(hdr->flags & DESC_DONE_FLAG)) { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1426 | dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n"); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1427 | qp->rx_ring_empty++; |
| 1428 | return -EAGAIN; |
| 1429 | } |
| 1430 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1431 | if (hdr->flags & LINK_DOWN_FLAG) { |
| 1432 | dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n"); |
| 1433 | ntb_qp_link_down(qp); |
| 1434 | hdr->flags = 0; |
Allen Hubbe | c0900b3 | 2015-05-12 06:24:27 -0400 | [diff] [blame] | 1435 | return -EAGAIN; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | if (hdr->ver != (u32)qp->rx_pkts) { |
| 1439 | dev_dbg(&qp->ndev->pdev->dev, |
| 1440 | "version mismatch, expected %llu - got %u\n", |
| 1441 | qp->rx_pkts, hdr->ver); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1442 | qp->rx_err_ver++; |
| 1443 | return -EIO; |
| 1444 | } |
| 1445 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1446 | entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1447 | if (!entry) { |
| 1448 | dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n"); |
| 1449 | qp->rx_err_no_buf++; |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1450 | return -EAGAIN; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1453 | entry->rx_hdr = hdr; |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 1454 | entry->rx_index = qp->rx_index; |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1455 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1456 | if (hdr->len > entry->len) { |
| 1457 | dev_dbg(&qp->ndev->pdev->dev, |
| 1458 | "receive buffer overflow! Wanted %d got %d\n", |
| 1459 | hdr->len, entry->len); |
| 1460 | qp->rx_err_oflow++; |
| 1461 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1462 | entry->len = -EIO; |
| 1463 | entry->flags |= DESC_DONE_FLAG; |
| 1464 | |
| 1465 | ntb_complete_rxc(qp); |
| 1466 | } else { |
| 1467 | dev_dbg(&qp->ndev->pdev->dev, |
| 1468 | "RX OK index %u ver %u size %d into buf size %d\n", |
| 1469 | qp->rx_index, hdr->ver, hdr->len, entry->len); |
| 1470 | |
| 1471 | qp->rx_bytes += hdr->len; |
| 1472 | qp->rx_pkts++; |
| 1473 | |
| 1474 | entry->len = hdr->len; |
| 1475 | |
| 1476 | ntb_async_rx(entry, offset); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1477 | } |
| 1478 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1479 | qp->rx_index++; |
| 1480 | qp->rx_index %= qp->rx_max_entry; |
| 1481 | |
| 1482 | return 0; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1485 | static void ntb_transport_rxc_db(unsigned long data) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1486 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1487 | struct ntb_transport_qp *qp = (void *)data; |
Jon Mason | c336acd | 2013-01-17 15:28:45 -0700 | [diff] [blame] | 1488 | int rc, i; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1489 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1490 | dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n", |
| 1491 | __func__, qp->qp_num); |
Jon Mason | e8aeb60 | 2013-04-18 17:59:44 -0700 | [diff] [blame] | 1492 | |
Jon Mason | c336acd | 2013-01-17 15:28:45 -0700 | [diff] [blame] | 1493 | /* Limit the number of packets processed in a single interrupt to |
| 1494 | * provide fairness to others |
| 1495 | */ |
| 1496 | for (i = 0; i < qp->rx_max_entry; i++) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1497 | rc = ntb_process_rxc(qp); |
Jon Mason | c336acd | 2013-01-17 15:28:45 -0700 | [diff] [blame] | 1498 | if (rc) |
| 1499 | break; |
| 1500 | } |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1501 | |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 1502 | if (i && qp->rx_dma_chan) |
| 1503 | dma_async_issue_pending(qp->rx_dma_chan); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1504 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1505 | if (i == qp->rx_max_entry) { |
| 1506 | /* there is more work to do */ |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 1507 | if (qp->active) |
| 1508 | tasklet_schedule(&qp->rxc_db_work); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1509 | } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) { |
| 1510 | /* the doorbell bit is set: clear it */ |
| 1511 | ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num)); |
| 1512 | /* ntb_db_read ensures ntb_db_clear write is committed */ |
| 1513 | ntb_db_read(qp->ndev); |
| 1514 | |
| 1515 | /* an interrupt may have arrived between finishing |
| 1516 | * ntb_process_rxc and clearing the doorbell bit: |
| 1517 | * there might be some more work to do. |
| 1518 | */ |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 1519 | if (qp->active) |
| 1520 | tasklet_schedule(&qp->rxc_db_work); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1521 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1524 | static void ntb_tx_copy_callback(void *data, |
| 1525 | const struct dmaengine_result *res) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1526 | { |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1527 | struct ntb_queue_entry *entry = data; |
| 1528 | struct ntb_transport_qp *qp = entry->qp; |
| 1529 | struct ntb_payload_header __iomem *hdr = entry->tx_hdr; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1530 | |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1531 | /* we need to check DMA results if we are using DMA */ |
| 1532 | if (res) { |
| 1533 | enum dmaengine_tx_result dma_err = res->result; |
| 1534 | |
| 1535 | switch (dma_err) { |
| 1536 | case DMA_TRANS_READ_FAILED: |
| 1537 | case DMA_TRANS_WRITE_FAILED: |
| 1538 | entry->errors++; |
| 1539 | case DMA_TRANS_ABORTED: |
| 1540 | { |
| 1541 | void __iomem *offset = |
| 1542 | qp->tx_mw + qp->tx_max_frame * |
| 1543 | entry->tx_index; |
| 1544 | |
| 1545 | /* resubmit via CPU */ |
| 1546 | ntb_memcpy_tx(entry, offset); |
| 1547 | qp->tx_memcpy++; |
| 1548 | return; |
| 1549 | } |
| 1550 | |
| 1551 | case DMA_TRANS_NOERROR: |
| 1552 | default: |
| 1553 | break; |
| 1554 | } |
| 1555 | } |
| 1556 | |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 1557 | iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1558 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1559 | ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num)); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1560 | |
| 1561 | /* The entry length can only be zero if the packet is intended to be a |
| 1562 | * "link down" or similar. Since no payload is being sent in these |
| 1563 | * cases, there is nothing to add to the completion queue. |
| 1564 | */ |
| 1565 | if (entry->len > 0) { |
| 1566 | qp->tx_bytes += entry->len; |
| 1567 | |
| 1568 | if (qp->tx_handler) |
| 1569 | qp->tx_handler(qp, qp->cb_data, entry->cb_data, |
| 1570 | entry->len); |
| 1571 | } |
| 1572 | |
| 1573 | ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q); |
| 1574 | } |
| 1575 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1576 | static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset) |
| 1577 | { |
Dave Jiang | 06917f7 | 2015-05-19 16:45:46 -0400 | [diff] [blame] | 1578 | #ifdef ARCH_HAS_NOCACHE_UACCESS |
| 1579 | /* |
| 1580 | * Using non-temporal mov to improve performance on non-cached |
| 1581 | * writes, even though we aren't actually copying from user space. |
| 1582 | */ |
| 1583 | __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len); |
| 1584 | #else |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1585 | memcpy_toio(offset, entry->buf, entry->len); |
Dave Jiang | 06917f7 | 2015-05-19 16:45:46 -0400 | [diff] [blame] | 1586 | #endif |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1587 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1588 | /* Ensure that the data is fully copied out before setting the flags */ |
| 1589 | wmb(); |
| 1590 | |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1591 | ntb_tx_copy_callback(entry, NULL); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1594 | static int ntb_async_tx_submit(struct ntb_transport_qp *qp, |
| 1595 | struct ntb_queue_entry *entry) |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1596 | { |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1597 | struct dma_async_tx_descriptor *txd; |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 1598 | struct dma_chan *chan = qp->tx_dma_chan; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1599 | struct dma_device *device; |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1600 | size_t len = entry->len; |
| 1601 | void *buf = entry->buf; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1602 | size_t dest_off, buff_off; |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1603 | struct dmaengine_unmap_data *unmap; |
| 1604 | dma_addr_t dest; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1605 | dma_cookie_t cookie; |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 1606 | int retries = 0; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1607 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1608 | device = chan->device; |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1609 | dest = qp->tx_mw_phys + qp->tx_max_frame * entry->tx_index; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1610 | buff_off = (size_t)buf & ~PAGE_MASK; |
| 1611 | dest_off = (size_t)dest & ~PAGE_MASK; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1612 | |
| 1613 | if (!is_dma_copy_aligned(device, buff_off, dest_off, len)) |
| 1614 | goto err; |
| 1615 | |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1616 | unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT); |
| 1617 | if (!unmap) |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1618 | goto err; |
| 1619 | |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1620 | unmap->len = len; |
| 1621 | unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf), |
| 1622 | buff_off, len, DMA_TO_DEVICE); |
| 1623 | if (dma_mapping_error(device->dev, unmap->addr[0])) |
| 1624 | goto err_get_unmap; |
| 1625 | |
| 1626 | unmap->to_cnt = 1; |
| 1627 | |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 1628 | for (retries = 0; retries < DMA_RETRIES; retries++) { |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1629 | txd = device->device_prep_dma_memcpy(chan, dest, |
| 1630 | unmap->addr[0], len, |
| 1631 | DMA_PREP_INTERRUPT); |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 1632 | if (txd) |
| 1633 | break; |
| 1634 | |
| 1635 | set_current_state(TASK_INTERRUPTIBLE); |
| 1636 | schedule_timeout(DMA_OUT_RESOURCE_TO); |
| 1637 | } |
| 1638 | |
| 1639 | if (!txd) { |
| 1640 | qp->dma_tx_prep_err++; |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1641 | goto err_get_unmap; |
Dave Jiang | 8c874cc | 2016-01-08 09:51:05 -0700 | [diff] [blame] | 1642 | } |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1643 | |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1644 | txd->callback_result = ntb_tx_copy_callback; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1645 | txd->callback_param = entry; |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1646 | dma_set_unmap(txd, unmap); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1647 | |
| 1648 | cookie = dmaengine_submit(txd); |
| 1649 | if (dma_submit_error(cookie)) |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1650 | goto err_set_unmap; |
| 1651 | |
| 1652 | dmaengine_unmap_put(unmap); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1653 | |
| 1654 | dma_async_issue_pending(chan); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1655 | |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1656 | return 0; |
Bartlomiej Zolnierkiewicz | 6f57fd0 | 2013-10-18 19:35:31 +0200 | [diff] [blame] | 1657 | err_set_unmap: |
| 1658 | dmaengine_unmap_put(unmap); |
| 1659 | err_get_unmap: |
| 1660 | dmaengine_unmap_put(unmap); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1661 | err: |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 1662 | return -ENXIO; |
| 1663 | } |
| 1664 | |
| 1665 | static void ntb_async_tx(struct ntb_transport_qp *qp, |
| 1666 | struct ntb_queue_entry *entry) |
| 1667 | { |
| 1668 | struct ntb_payload_header __iomem *hdr; |
| 1669 | struct dma_chan *chan = qp->tx_dma_chan; |
| 1670 | void __iomem *offset; |
| 1671 | int res; |
| 1672 | |
| 1673 | entry->tx_index = qp->tx_index; |
| 1674 | offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index; |
| 1675 | hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header); |
| 1676 | entry->tx_hdr = hdr; |
| 1677 | |
| 1678 | iowrite32(entry->len, &hdr->len); |
| 1679 | iowrite32((u32)qp->tx_pkts, &hdr->ver); |
| 1680 | |
| 1681 | if (!chan) |
| 1682 | goto err; |
| 1683 | |
| 1684 | if (entry->len < copy_bytes) |
| 1685 | goto err; |
| 1686 | |
| 1687 | res = ntb_async_tx_submit(qp, entry); |
| 1688 | if (res < 0) |
| 1689 | goto err; |
| 1690 | |
| 1691 | if (!entry->retries) |
| 1692 | qp->tx_async++; |
| 1693 | |
| 1694 | return; |
| 1695 | |
| 1696 | err: |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1697 | ntb_memcpy_tx(entry, offset); |
| 1698 | qp->tx_memcpy++; |
| 1699 | } |
| 1700 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1701 | static int ntb_process_tx(struct ntb_transport_qp *qp, |
| 1702 | struct ntb_queue_entry *entry) |
| 1703 | { |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1704 | if (qp->tx_index == qp->remote_rx_info->entry) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1705 | qp->tx_ring_full++; |
| 1706 | return -EAGAIN; |
| 1707 | } |
| 1708 | |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 1709 | if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1710 | if (qp->tx_handler) |
Jon Mason | 179f912 | 2015-12-18 13:22:37 -0500 | [diff] [blame] | 1711 | qp->tx_handler(qp, qp->cb_data, NULL, -EIO); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1712 | |
| 1713 | ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, |
| 1714 | &qp->tx_free_q); |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1718 | ntb_async_tx(qp, entry); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1719 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1720 | qp->tx_index++; |
| 1721 | qp->tx_index %= qp->tx_max_entry; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1722 | |
| 1723 | qp->tx_pkts++; |
| 1724 | |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | static void ntb_send_link_down(struct ntb_transport_qp *qp) |
| 1729 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1730 | struct pci_dev *pdev = qp->ndev->pdev; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1731 | struct ntb_queue_entry *entry; |
| 1732 | int i, rc; |
| 1733 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1734 | if (!qp->link_is_up) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1735 | return; |
| 1736 | |
Allen Hubbe | e22e0b9 | 2015-05-12 06:55:44 -0400 | [diff] [blame] | 1737 | dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1738 | |
| 1739 | for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) { |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1740 | entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1741 | if (entry) |
| 1742 | break; |
| 1743 | msleep(100); |
| 1744 | } |
| 1745 | |
| 1746 | if (!entry) |
| 1747 | return; |
| 1748 | |
| 1749 | entry->cb_data = NULL; |
| 1750 | entry->buf = NULL; |
| 1751 | entry->len = 0; |
| 1752 | entry->flags = LINK_DOWN_FLAG; |
| 1753 | |
| 1754 | rc = ntb_process_tx(qp, entry); |
| 1755 | if (rc) |
| 1756 | dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n", |
| 1757 | qp->qp_num); |
Allen Hubbe | 2849b5d7 | 2015-05-12 08:09:15 -0400 | [diff] [blame] | 1758 | |
| 1759 | ntb_qp_link_down_reset(qp); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1762 | static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node) |
| 1763 | { |
| 1764 | return dev_to_node(&chan->dev->device) == (int)(unsigned long)node; |
| 1765 | } |
| 1766 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1767 | /** |
| 1768 | * ntb_transport_create_queue - Create a new NTB transport layer queue |
| 1769 | * @rx_handler: receive callback function |
| 1770 | * @tx_handler: transmit callback function |
| 1771 | * @event_handler: event callback function |
| 1772 | * |
| 1773 | * Create a new NTB transport layer queue and provide the queue with a callback |
| 1774 | * routine for both transmit and receive. The receive callback routine will be |
| 1775 | * used to pass up data when the transport has received it on the queue. The |
| 1776 | * transmit callback routine will be called when the transport has completed the |
| 1777 | * transmission of the data on the queue and the data is ready to be freed. |
| 1778 | * |
| 1779 | * RETURNS: pointer to newly created ntb_queue, NULL on error. |
| 1780 | */ |
| 1781 | struct ntb_transport_qp * |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1782 | ntb_transport_create_queue(void *data, struct device *client_dev, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1783 | const struct ntb_queue_handlers *handlers) |
| 1784 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1785 | struct ntb_dev *ndev; |
| 1786 | struct pci_dev *pdev; |
| 1787 | struct ntb_transport_ctx *nt; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1788 | struct ntb_queue_entry *entry; |
| 1789 | struct ntb_transport_qp *qp; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1790 | u64 qp_bit; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1791 | unsigned int free_queue; |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1792 | dma_cap_mask_t dma_mask; |
| 1793 | int node; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1794 | int i; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1795 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1796 | ndev = dev_ntb(client_dev->parent); |
| 1797 | pdev = ndev->pdev; |
| 1798 | nt = ndev->ctx; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1799 | |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1800 | node = dev_to_node(&ndev->dev); |
| 1801 | |
Thomas VanSelus | 68214ad | 2017-02-13 16:46:26 -0600 | [diff] [blame^] | 1802 | free_queue = ffs(nt->qp_bitmap_free); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1803 | if (!free_queue) |
| 1804 | goto err; |
| 1805 | |
| 1806 | /* decrement free_queue to make it zero based */ |
| 1807 | free_queue--; |
| 1808 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1809 | qp = &nt->qp_vec[free_queue]; |
| 1810 | qp_bit = BIT_ULL(qp->qp_num); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1811 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1812 | nt->qp_bitmap_free &= ~qp_bit; |
| 1813 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1814 | qp->cb_data = data; |
| 1815 | qp->rx_handler = handlers->rx_handler; |
| 1816 | qp->tx_handler = handlers->tx_handler; |
| 1817 | qp->event_handler = handlers->event_handler; |
| 1818 | |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1819 | dma_cap_zero(dma_mask); |
| 1820 | dma_cap_set(DMA_MEMCPY, dma_mask); |
| 1821 | |
Dave Jiang | a41ef05 | 2015-05-19 16:52:04 -0400 | [diff] [blame] | 1822 | if (use_dma) { |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 1823 | qp->tx_dma_chan = |
| 1824 | dma_request_channel(dma_mask, ntb_dma_filter_fn, |
| 1825 | (void *)(unsigned long)node); |
| 1826 | if (!qp->tx_dma_chan) |
| 1827 | dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n"); |
| 1828 | |
| 1829 | qp->rx_dma_chan = |
| 1830 | dma_request_channel(dma_mask, ntb_dma_filter_fn, |
| 1831 | (void *)(unsigned long)node); |
| 1832 | if (!qp->rx_dma_chan) |
| 1833 | dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n"); |
Dave Jiang | a41ef05 | 2015-05-19 16:52:04 -0400 | [diff] [blame] | 1834 | } else { |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 1835 | qp->tx_dma_chan = NULL; |
| 1836 | qp->rx_dma_chan = NULL; |
Dave Jiang | a41ef05 | 2015-05-19 16:52:04 -0400 | [diff] [blame] | 1837 | } |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 1838 | |
| 1839 | dev_dbg(&pdev->dev, "Using %s memcpy for TX\n", |
| 1840 | qp->tx_dma_chan ? "DMA" : "CPU"); |
| 1841 | |
| 1842 | dev_dbg(&pdev->dev, "Using %s memcpy for RX\n", |
| 1843 | qp->rx_dma_chan ? "DMA" : "CPU"); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1844 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1845 | for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) { |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1846 | entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1847 | if (!entry) |
| 1848 | goto err1; |
| 1849 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1850 | entry->qp = qp; |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1851 | ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1852 | &qp->rx_free_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1853 | } |
Dave Jiang | a754a8f | 2016-04-08 10:49:06 -0700 | [diff] [blame] | 1854 | qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1855 | |
Dave Jiang | a754a8f | 2016-04-08 10:49:06 -0700 | [diff] [blame] | 1856 | for (i = 0; i < qp->tx_max_entry; i++) { |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1857 | entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1858 | if (!entry) |
| 1859 | goto err2; |
| 1860 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1861 | entry->qp = qp; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1862 | ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1863 | &qp->tx_free_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1866 | ntb_db_clear(qp->ndev, qp_bit); |
| 1867 | ntb_db_clear_mask(qp->ndev, qp_bit); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1868 | |
| 1869 | dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num); |
| 1870 | |
| 1871 | return qp; |
| 1872 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1873 | err2: |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1874 | while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q))) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1875 | kfree(entry); |
| 1876 | err1: |
Dave Jiang | a754a8f | 2016-04-08 10:49:06 -0700 | [diff] [blame] | 1877 | qp->rx_alloc_entry = 0; |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1878 | while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q))) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1879 | kfree(entry); |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 1880 | if (qp->tx_dma_chan) |
| 1881 | dma_release_channel(qp->tx_dma_chan); |
| 1882 | if (qp->rx_dma_chan) |
| 1883 | dma_release_channel(qp->rx_dma_chan); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1884 | nt->qp_bitmap_free |= qp_bit; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1885 | err: |
| 1886 | return NULL; |
| 1887 | } |
| 1888 | EXPORT_SYMBOL_GPL(ntb_transport_create_queue); |
| 1889 | |
| 1890 | /** |
| 1891 | * ntb_transport_free_queue - Frees NTB transport queue |
| 1892 | * @qp: NTB queue to be freed |
| 1893 | * |
| 1894 | * Frees NTB transport queue |
| 1895 | */ |
| 1896 | void ntb_transport_free_queue(struct ntb_transport_qp *qp) |
| 1897 | { |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 1898 | struct pci_dev *pdev; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1899 | struct ntb_queue_entry *entry; |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1900 | u64 qp_bit; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1901 | |
| 1902 | if (!qp) |
| 1903 | return; |
| 1904 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1905 | pdev = qp->ndev->pdev; |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 1906 | |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 1907 | qp->active = false; |
| 1908 | |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 1909 | if (qp->tx_dma_chan) { |
| 1910 | struct dma_chan *chan = qp->tx_dma_chan; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1911 | /* Putting the dma_chan to NULL will force any new traffic to be |
| 1912 | * processed by the CPU instead of the DAM engine |
| 1913 | */ |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 1914 | qp->tx_dma_chan = NULL; |
| 1915 | |
| 1916 | /* Try to be nice and wait for any queued DMA engine |
| 1917 | * transactions to process before smashing it with a rock |
| 1918 | */ |
| 1919 | dma_sync_wait(chan, qp->last_cookie); |
| 1920 | dmaengine_terminate_all(chan); |
| 1921 | dma_release_channel(chan); |
| 1922 | } |
| 1923 | |
| 1924 | if (qp->rx_dma_chan) { |
| 1925 | struct dma_chan *chan = qp->rx_dma_chan; |
| 1926 | /* Putting the dma_chan to NULL will force any new traffic to be |
| 1927 | * processed by the CPU instead of the DAM engine |
| 1928 | */ |
| 1929 | qp->rx_dma_chan = NULL; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1930 | |
| 1931 | /* Try to be nice and wait for any queued DMA engine |
| 1932 | * transactions to process before smashing it with a rock |
| 1933 | */ |
| 1934 | dma_sync_wait(chan, qp->last_cookie); |
| 1935 | dmaengine_terminate_all(chan); |
Allen Hubbe | 1199aa6 | 2015-05-18 06:20:47 -0400 | [diff] [blame] | 1936 | dma_release_channel(chan); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1937 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1938 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1939 | qp_bit = BIT_ULL(qp->qp_num); |
| 1940 | |
| 1941 | ntb_db_set_mask(qp->ndev, qp_bit); |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 1942 | tasklet_kill(&qp->rxc_db_work); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1943 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 1944 | cancel_delayed_work_sync(&qp->link_work); |
| 1945 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1946 | qp->cb_data = NULL; |
| 1947 | qp->rx_handler = NULL; |
| 1948 | qp->tx_handler = NULL; |
| 1949 | qp->event_handler = NULL; |
| 1950 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1951 | while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q))) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1952 | kfree(entry); |
| 1953 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1954 | while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) { |
| 1955 | dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n"); |
| 1956 | kfree(entry); |
| 1957 | } |
| 1958 | |
| 1959 | while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) { |
| 1960 | dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n"); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1961 | kfree(entry); |
| 1962 | } |
| 1963 | |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1964 | while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q))) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1965 | kfree(entry); |
| 1966 | |
Allen Hubbe | 30a4bb1 | 2015-07-13 08:07:14 -0400 | [diff] [blame] | 1967 | qp->transport->qp_bitmap_free |= qp_bit; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1968 | |
| 1969 | dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num); |
| 1970 | } |
| 1971 | EXPORT_SYMBOL_GPL(ntb_transport_free_queue); |
| 1972 | |
| 1973 | /** |
| 1974 | * ntb_transport_rx_remove - Dequeues enqueued rx packet |
| 1975 | * @qp: NTB queue to be freed |
| 1976 | * @len: pointer to variable to write enqueued buffers length |
| 1977 | * |
| 1978 | * Dequeues unused buffers from receive queue. Should only be used during |
| 1979 | * shutdown of qp. |
| 1980 | * |
| 1981 | * RETURNS: NULL error value on error, or void* for success. |
| 1982 | */ |
| 1983 | void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len) |
| 1984 | { |
| 1985 | struct ntb_queue_entry *entry; |
| 1986 | void *buf; |
| 1987 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 1988 | if (!qp || qp->client_ready) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1989 | return NULL; |
| 1990 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1991 | entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1992 | if (!entry) |
| 1993 | return NULL; |
| 1994 | |
| 1995 | buf = entry->cb_data; |
| 1996 | *len = entry->len; |
| 1997 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 1998 | ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1999 | |
| 2000 | return buf; |
| 2001 | } |
| 2002 | EXPORT_SYMBOL_GPL(ntb_transport_rx_remove); |
| 2003 | |
| 2004 | /** |
| 2005 | * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry |
| 2006 | * @qp: NTB transport layer queue the entry is to be enqueued on |
| 2007 | * @cb: per buffer pointer for callback function to use |
| 2008 | * @data: pointer to data buffer that incoming packets will be copied into |
| 2009 | * @len: length of the data buffer |
| 2010 | * |
| 2011 | * Enqueue a new receive buffer onto the transport queue into which a NTB |
| 2012 | * payload can be received into. |
| 2013 | * |
| 2014 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 2015 | */ |
| 2016 | int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data, |
| 2017 | unsigned int len) |
| 2018 | { |
| 2019 | struct ntb_queue_entry *entry; |
| 2020 | |
| 2021 | if (!qp) |
| 2022 | return -EINVAL; |
| 2023 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 2024 | entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2025 | if (!entry) |
| 2026 | return -ENOMEM; |
| 2027 | |
| 2028 | entry->cb_data = cb; |
| 2029 | entry->buf = data; |
| 2030 | entry->len = len; |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 2031 | entry->flags = 0; |
Dave Jiang | 7220357 | 2016-07-20 13:14:13 -0700 | [diff] [blame] | 2032 | entry->retries = 0; |
| 2033 | entry->errors = 0; |
| 2034 | entry->rx_index = 0; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2035 | |
Allen Hubbe | da2e5ae | 2015-07-13 08:07:08 -0400 | [diff] [blame] | 2036 | ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q); |
| 2037 | |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 2038 | if (qp->active) |
| 2039 | tasklet_schedule(&qp->rxc_db_work); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2040 | |
| 2041 | return 0; |
| 2042 | } |
| 2043 | EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue); |
| 2044 | |
| 2045 | /** |
| 2046 | * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry |
| 2047 | * @qp: NTB transport layer queue the entry is to be enqueued on |
| 2048 | * @cb: per buffer pointer for callback function to use |
| 2049 | * @data: pointer to data buffer that will be sent |
| 2050 | * @len: length of the data buffer |
| 2051 | * |
| 2052 | * Enqueue a new transmit buffer onto the transport queue from which a NTB |
Jon Mason | f9a2cf8 | 2013-07-29 16:46:43 -0700 | [diff] [blame] | 2053 | * payload will be transmitted. This assumes that a lock is being held to |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2054 | * serialize access to the qp. |
| 2055 | * |
| 2056 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 2057 | */ |
| 2058 | int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data, |
| 2059 | unsigned int len) |
| 2060 | { |
| 2061 | struct ntb_queue_entry *entry; |
| 2062 | int rc; |
| 2063 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2064 | if (!qp || !qp->link_is_up || !len) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2065 | return -EINVAL; |
| 2066 | |
| 2067 | entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 2068 | if (!entry) { |
| 2069 | qp->tx_err_no_buf++; |
Dave Jiang | e74bfee | 2015-07-13 08:07:17 -0400 | [diff] [blame] | 2070 | return -EBUSY; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 2071 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2072 | |
| 2073 | entry->cb_data = cb; |
| 2074 | entry->buf = data; |
| 2075 | entry->len = len; |
| 2076 | entry->flags = 0; |
Dave Jiang | 9cabc26 | 2016-07-20 13:14:07 -0700 | [diff] [blame] | 2077 | entry->errors = 0; |
| 2078 | entry->retries = 0; |
| 2079 | entry->tx_index = 0; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2080 | |
| 2081 | rc = ntb_process_tx(qp, entry); |
| 2082 | if (rc) |
| 2083 | ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, |
| 2084 | &qp->tx_free_q); |
| 2085 | |
| 2086 | return rc; |
| 2087 | } |
| 2088 | EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue); |
| 2089 | |
| 2090 | /** |
| 2091 | * ntb_transport_link_up - Notify NTB transport of client readiness to use queue |
| 2092 | * @qp: NTB transport layer queue to be enabled |
| 2093 | * |
| 2094 | * Notify NTB transport layer of client readiness to use queue |
| 2095 | */ |
| 2096 | void ntb_transport_link_up(struct ntb_transport_qp *qp) |
| 2097 | { |
| 2098 | if (!qp) |
| 2099 | return; |
| 2100 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2101 | qp->client_ready = true; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2102 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2103 | if (qp->transport->link_is_up) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2104 | schedule_delayed_work(&qp->link_work, 0); |
| 2105 | } |
| 2106 | EXPORT_SYMBOL_GPL(ntb_transport_link_up); |
| 2107 | |
| 2108 | /** |
| 2109 | * ntb_transport_link_down - Notify NTB transport to no longer enqueue data |
| 2110 | * @qp: NTB transport layer queue to be disabled |
| 2111 | * |
| 2112 | * Notify NTB transport layer of client's desire to no longer receive data on |
| 2113 | * transport queue specified. It is the client's responsibility to ensure all |
Jon Mason | f9a2cf8 | 2013-07-29 16:46:43 -0700 | [diff] [blame] | 2114 | * entries on queue are purged or otherwise handled appropriately. |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2115 | */ |
| 2116 | void ntb_transport_link_down(struct ntb_transport_qp *qp) |
| 2117 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2118 | int val; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2119 | |
| 2120 | if (!qp) |
| 2121 | return; |
| 2122 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2123 | qp->client_ready = false; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2124 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2125 | val = ntb_spad_read(qp->ndev, QP_LINKS); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2126 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2127 | ntb_peer_spad_write(qp->ndev, QP_LINKS, |
| 2128 | val & ~BIT(qp->qp_num)); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2129 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2130 | if (qp->link_is_up) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2131 | ntb_send_link_down(qp); |
| 2132 | else |
| 2133 | cancel_delayed_work_sync(&qp->link_work); |
| 2134 | } |
| 2135 | EXPORT_SYMBOL_GPL(ntb_transport_link_down); |
| 2136 | |
| 2137 | /** |
| 2138 | * ntb_transport_link_query - Query transport link state |
| 2139 | * @qp: NTB transport layer queue to be queried |
| 2140 | * |
| 2141 | * Query connectivity to the remote system of the NTB transport queue |
| 2142 | * |
| 2143 | * RETURNS: true for link up or false for link down |
| 2144 | */ |
| 2145 | bool ntb_transport_link_query(struct ntb_transport_qp *qp) |
| 2146 | { |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 2147 | if (!qp) |
| 2148 | return false; |
| 2149 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2150 | return qp->link_is_up; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2151 | } |
| 2152 | EXPORT_SYMBOL_GPL(ntb_transport_link_query); |
| 2153 | |
| 2154 | /** |
| 2155 | * ntb_transport_qp_num - Query the qp number |
| 2156 | * @qp: NTB transport layer queue to be queried |
| 2157 | * |
| 2158 | * Query qp number of the NTB transport queue |
| 2159 | * |
| 2160 | * RETURNS: a zero based number specifying the qp number |
| 2161 | */ |
| 2162 | unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp) |
| 2163 | { |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 2164 | if (!qp) |
| 2165 | return 0; |
| 2166 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2167 | return qp->qp_num; |
| 2168 | } |
| 2169 | EXPORT_SYMBOL_GPL(ntb_transport_qp_num); |
| 2170 | |
| 2171 | /** |
| 2172 | * ntb_transport_max_size - Query the max payload size of a qp |
| 2173 | * @qp: NTB transport layer queue to be queried |
| 2174 | * |
| 2175 | * Query the maximum payload size permissible on the given qp |
| 2176 | * |
| 2177 | * RETURNS: the max payload size of a qp |
| 2178 | */ |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 2179 | unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2180 | { |
Dave Jiang | 04afde4 | 2015-09-17 13:27:04 -0700 | [diff] [blame] | 2181 | unsigned int max_size; |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 2182 | unsigned int copy_align; |
Dave Jiang | 04afde4 | 2015-09-17 13:27:04 -0700 | [diff] [blame] | 2183 | struct dma_chan *rx_chan, *tx_chan; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 2184 | |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 2185 | if (!qp) |
| 2186 | return 0; |
| 2187 | |
Dave Jiang | 04afde4 | 2015-09-17 13:27:04 -0700 | [diff] [blame] | 2188 | rx_chan = qp->rx_dma_chan; |
| 2189 | tx_chan = qp->tx_dma_chan; |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 2190 | |
Dave Jiang | 04afde4 | 2015-09-17 13:27:04 -0700 | [diff] [blame] | 2191 | copy_align = max(rx_chan ? rx_chan->device->copy_align : 0, |
| 2192 | tx_chan ? tx_chan->device->copy_align : 0); |
Dave Jiang | 569410ca | 2015-07-13 08:07:22 -0400 | [diff] [blame] | 2193 | |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 2194 | /* If DMA engine usage is possible, try to find the max size for that */ |
Dave Jiang | 04afde4 | 2015-09-17 13:27:04 -0700 | [diff] [blame] | 2195 | max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header); |
| 2196 | max_size = round_down(max_size, 1 << copy_align); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 2197 | |
Dave Jiang | 04afde4 | 2015-09-17 13:27:04 -0700 | [diff] [blame] | 2198 | return max_size; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 2199 | } |
| 2200 | EXPORT_SYMBOL_GPL(ntb_transport_max_size); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2201 | |
Dave Jiang | e74bfee | 2015-07-13 08:07:17 -0400 | [diff] [blame] | 2202 | unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp) |
| 2203 | { |
| 2204 | unsigned int head = qp->tx_index; |
| 2205 | unsigned int tail = qp->remote_rx_info->entry; |
| 2206 | |
| 2207 | return tail > head ? tail - head : qp->tx_max_entry + tail - head; |
| 2208 | } |
| 2209 | EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry); |
| 2210 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2211 | static void ntb_transport_doorbell_callback(void *data, int vector) |
| 2212 | { |
| 2213 | struct ntb_transport_ctx *nt = data; |
| 2214 | struct ntb_transport_qp *qp; |
| 2215 | u64 db_bits; |
| 2216 | unsigned int qp_num; |
| 2217 | |
| 2218 | db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free & |
| 2219 | ntb_db_vector_mask(nt->ndev, vector)); |
| 2220 | |
| 2221 | while (db_bits) { |
| 2222 | qp_num = __ffs(db_bits); |
| 2223 | qp = &nt->qp_vec[qp_num]; |
| 2224 | |
Dave Jiang | e902133 | 2016-02-23 09:11:36 -0700 | [diff] [blame] | 2225 | if (qp->active) |
| 2226 | tasklet_schedule(&qp->rxc_db_work); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2227 | |
| 2228 | db_bits &= ~BIT_ULL(qp_num); |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | static const struct ntb_ctx_ops ntb_transport_ops = { |
| 2233 | .link_event = ntb_transport_event_callback, |
| 2234 | .db_event = ntb_transport_doorbell_callback, |
| 2235 | }; |
| 2236 | |
| 2237 | static struct ntb_client ntb_transport_client = { |
| 2238 | .ops = { |
| 2239 | .probe = ntb_transport_probe, |
| 2240 | .remove = ntb_transport_free, |
| 2241 | }, |
| 2242 | }; |
| 2243 | |
| 2244 | static int __init ntb_transport_init(void) |
| 2245 | { |
| 2246 | int rc; |
| 2247 | |
Dave Jiang | 7eb3878 | 2015-06-15 08:21:33 -0400 | [diff] [blame] | 2248 | pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER); |
| 2249 | |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2250 | if (debugfs_initialized()) |
| 2251 | nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL); |
| 2252 | |
| 2253 | rc = bus_register(&ntb_transport_bus); |
| 2254 | if (rc) |
| 2255 | goto err_bus; |
| 2256 | |
| 2257 | rc = ntb_register_client(&ntb_transport_client); |
| 2258 | if (rc) |
| 2259 | goto err_client; |
| 2260 | |
| 2261 | return 0; |
| 2262 | |
| 2263 | err_client: |
| 2264 | bus_unregister(&ntb_transport_bus); |
| 2265 | err_bus: |
| 2266 | debugfs_remove_recursive(nt_debugfs_dir); |
| 2267 | return rc; |
| 2268 | } |
| 2269 | module_init(ntb_transport_init); |
| 2270 | |
| 2271 | static void __exit ntb_transport_exit(void) |
| 2272 | { |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2273 | ntb_unregister_client(&ntb_transport_client); |
| 2274 | bus_unregister(&ntb_transport_bus); |
Allen Hubbe | 4332538 | 2016-12-27 17:57:04 -0500 | [diff] [blame] | 2275 | debugfs_remove_recursive(nt_debugfs_dir); |
Allen Hubbe | e26a584 | 2015-04-09 10:33:20 -0400 | [diff] [blame] | 2276 | } |
| 2277 | module_exit(ntb_transport_exit); |