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