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