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