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. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of version 2 of the GNU General Public License as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * BSD LICENSE |
| 14 | * |
| 15 | * Copyright(c) 2012 Intel Corporation. All rights reserved. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * |
| 21 | * * Redistributions of source code must retain the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer. |
| 23 | * * Redistributions in binary form must reproduce the above copy |
| 24 | * notice, this list of conditions and the following disclaimer in |
| 25 | * the documentation and/or other materials provided with the |
| 26 | * distribution. |
| 27 | * * Neither the name of Intel Corporation nor the names of its |
| 28 | * contributors may be used to endorse or promote products derived |
| 29 | * from this software without specific prior written permission. |
| 30 | * |
| 31 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 32 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 33 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 34 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 35 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 36 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 37 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 38 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 39 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 40 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 41 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 42 | * |
| 43 | * Intel PCIe NTB Linux driver |
| 44 | * |
| 45 | * Contact Information: |
| 46 | * Jon Mason <jon.mason@intel.com> |
| 47 | */ |
| 48 | #include <linux/debugfs.h> |
| 49 | #include <linux/delay.h> |
| 50 | #include <linux/dma-mapping.h> |
| 51 | #include <linux/errno.h> |
| 52 | #include <linux/export.h> |
| 53 | #include <linux/interrupt.h> |
| 54 | #include <linux/module.h> |
| 55 | #include <linux/pci.h> |
| 56 | #include <linux/slab.h> |
| 57 | #include <linux/types.h> |
| 58 | #include <linux/ntb.h> |
| 59 | #include "ntb_hw.h" |
| 60 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 61 | #define NTB_TRANSPORT_VERSION 3 |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 62 | |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 63 | static unsigned int transport_mtu = 0x401E; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 64 | module_param(transport_mtu, uint, 0644); |
| 65 | MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets"); |
| 66 | |
| 67 | static unsigned char max_num_clients = 2; |
| 68 | module_param(max_num_clients, byte, 0644); |
| 69 | MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients"); |
| 70 | |
| 71 | struct ntb_queue_entry { |
| 72 | /* ntb_queue list reference */ |
| 73 | struct list_head entry; |
| 74 | /* pointers to data to be transfered */ |
| 75 | void *cb_data; |
| 76 | void *buf; |
| 77 | unsigned int len; |
| 78 | unsigned int flags; |
| 79 | }; |
| 80 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 81 | struct ntb_rx_info { |
| 82 | unsigned int entry; |
| 83 | }; |
| 84 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 85 | struct ntb_transport_qp { |
| 86 | struct ntb_transport *transport; |
| 87 | struct ntb_device *ndev; |
| 88 | void *cb_data; |
| 89 | |
| 90 | bool client_ready; |
| 91 | bool qp_link; |
| 92 | u8 qp_num; /* Only 64 QP's are allowed. 0-63 */ |
| 93 | |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 94 | struct ntb_rx_info __iomem *rx_info; |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 95 | struct ntb_rx_info *remote_rx_info; |
| 96 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 97 | void (*tx_handler) (struct ntb_transport_qp *qp, void *qp_data, |
| 98 | void *data, int len); |
| 99 | struct list_head tx_free_q; |
| 100 | spinlock_t ntb_tx_free_q_lock; |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 101 | void __iomem *tx_mw; |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 102 | unsigned int tx_index; |
| 103 | unsigned int tx_max_entry; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 104 | unsigned int tx_max_frame; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 105 | |
| 106 | void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data, |
| 107 | void *data, int len); |
| 108 | struct tasklet_struct rx_work; |
| 109 | struct list_head rx_pend_q; |
| 110 | struct list_head rx_free_q; |
| 111 | spinlock_t ntb_rx_pend_q_lock; |
| 112 | spinlock_t ntb_rx_free_q_lock; |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 113 | void *rx_buff; |
| 114 | unsigned int rx_index; |
| 115 | unsigned int rx_max_entry; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 116 | unsigned int rx_max_frame; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 117 | |
| 118 | void (*event_handler) (void *data, int status); |
| 119 | struct delayed_work link_work; |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 120 | struct work_struct link_cleanup; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 121 | |
| 122 | struct dentry *debugfs_dir; |
| 123 | struct dentry *debugfs_stats; |
| 124 | |
| 125 | /* Stats */ |
| 126 | u64 rx_bytes; |
| 127 | u64 rx_pkts; |
| 128 | u64 rx_ring_empty; |
| 129 | u64 rx_err_no_buf; |
| 130 | u64 rx_err_oflow; |
| 131 | u64 rx_err_ver; |
| 132 | u64 tx_bytes; |
| 133 | u64 tx_pkts; |
| 134 | u64 tx_ring_full; |
| 135 | }; |
| 136 | |
| 137 | struct ntb_transport_mw { |
| 138 | size_t size; |
| 139 | void *virt_addr; |
| 140 | dma_addr_t dma_addr; |
| 141 | }; |
| 142 | |
| 143 | struct ntb_transport_client_dev { |
| 144 | struct list_head entry; |
| 145 | struct device dev; |
| 146 | }; |
| 147 | |
| 148 | struct ntb_transport { |
| 149 | struct list_head entry; |
| 150 | struct list_head client_devs; |
| 151 | |
| 152 | struct ntb_device *ndev; |
| 153 | struct ntb_transport_mw mw[NTB_NUM_MW]; |
| 154 | struct ntb_transport_qp *qps; |
| 155 | unsigned int max_qps; |
| 156 | unsigned long qp_bitmap; |
| 157 | bool transport_link; |
| 158 | struct delayed_work link_work; |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 159 | struct work_struct link_cleanup; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 160 | struct dentry *debugfs_dir; |
| 161 | }; |
| 162 | |
| 163 | enum { |
| 164 | DESC_DONE_FLAG = 1 << 0, |
| 165 | LINK_DOWN_FLAG = 1 << 1, |
| 166 | }; |
| 167 | |
| 168 | struct ntb_payload_header { |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 169 | unsigned int ver; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 170 | unsigned int len; |
| 171 | unsigned int flags; |
| 172 | }; |
| 173 | |
| 174 | enum { |
| 175 | VERSION = 0, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 176 | QP_LINKS, |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 177 | NUM_QPS, |
| 178 | NUM_MWS, |
| 179 | MW0_SZ_HIGH, |
| 180 | MW0_SZ_LOW, |
| 181 | MW1_SZ_HIGH, |
| 182 | MW1_SZ_LOW, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 183 | MAX_SPAD, |
| 184 | }; |
| 185 | |
| 186 | #define QP_TO_MW(qp) ((qp) % NTB_NUM_MW) |
| 187 | #define NTB_QP_DEF_NUM_ENTRIES 100 |
| 188 | #define NTB_LINK_DOWN_TIMEOUT 10 |
| 189 | |
| 190 | static int ntb_match_bus(struct device *dev, struct device_driver *drv) |
| 191 | { |
| 192 | return !strncmp(dev_name(dev), drv->name, strlen(drv->name)); |
| 193 | } |
| 194 | |
| 195 | static int ntb_client_probe(struct device *dev) |
| 196 | { |
| 197 | const struct ntb_client *drv = container_of(dev->driver, |
| 198 | struct ntb_client, driver); |
| 199 | struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev); |
| 200 | int rc = -EINVAL; |
| 201 | |
| 202 | get_device(dev); |
| 203 | if (drv && drv->probe) |
| 204 | rc = drv->probe(pdev); |
| 205 | if (rc) |
| 206 | put_device(dev); |
| 207 | |
| 208 | return rc; |
| 209 | } |
| 210 | |
| 211 | static int ntb_client_remove(struct device *dev) |
| 212 | { |
| 213 | const struct ntb_client *drv = container_of(dev->driver, |
| 214 | struct ntb_client, driver); |
| 215 | struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev); |
| 216 | |
| 217 | if (drv && drv->remove) |
| 218 | drv->remove(pdev); |
| 219 | |
| 220 | put_device(dev); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Jon Mason | 170d35a | 2013-01-19 02:02:23 -0700 | [diff] [blame] | 225 | static struct bus_type ntb_bus_type = { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 226 | .name = "ntb_bus", |
| 227 | .match = ntb_match_bus, |
| 228 | .probe = ntb_client_probe, |
| 229 | .remove = ntb_client_remove, |
| 230 | }; |
| 231 | |
| 232 | static LIST_HEAD(ntb_transport_list); |
| 233 | |
Greg Kroah-Hartman | 78a61ab | 2013-01-17 19:17:42 -0800 | [diff] [blame] | 234 | static int ntb_bus_init(struct ntb_transport *nt) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 235 | { |
| 236 | if (list_empty(&ntb_transport_list)) { |
| 237 | int rc = bus_register(&ntb_bus_type); |
| 238 | if (rc) |
| 239 | return rc; |
| 240 | } |
| 241 | |
| 242 | list_add(&nt->entry, &ntb_transport_list); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Greg Kroah-Hartman | 78a61ab | 2013-01-17 19:17:42 -0800 | [diff] [blame] | 247 | static void ntb_bus_remove(struct ntb_transport *nt) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 248 | { |
| 249 | struct ntb_transport_client_dev *client_dev, *cd; |
| 250 | |
| 251 | list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) { |
| 252 | dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n", |
| 253 | dev_name(&client_dev->dev)); |
| 254 | list_del(&client_dev->entry); |
| 255 | device_unregister(&client_dev->dev); |
| 256 | } |
| 257 | |
| 258 | list_del(&nt->entry); |
| 259 | |
| 260 | if (list_empty(&ntb_transport_list)) |
| 261 | bus_unregister(&ntb_bus_type); |
| 262 | } |
| 263 | |
| 264 | static void ntb_client_release(struct device *dev) |
| 265 | { |
| 266 | struct ntb_transport_client_dev *client_dev; |
| 267 | client_dev = container_of(dev, struct ntb_transport_client_dev, dev); |
| 268 | |
| 269 | kfree(client_dev); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * ntb_unregister_client_dev - Unregister NTB client device |
| 274 | * @device_name: Name of NTB client device |
| 275 | * |
| 276 | * Unregister an NTB client device with the NTB transport layer |
| 277 | */ |
| 278 | void ntb_unregister_client_dev(char *device_name) |
| 279 | { |
| 280 | struct ntb_transport_client_dev *client, *cd; |
| 281 | struct ntb_transport *nt; |
| 282 | |
| 283 | list_for_each_entry(nt, &ntb_transport_list, entry) |
| 284 | list_for_each_entry_safe(client, cd, &nt->client_devs, entry) |
| 285 | if (!strncmp(dev_name(&client->dev), device_name, |
| 286 | strlen(device_name))) { |
| 287 | list_del(&client->entry); |
| 288 | device_unregister(&client->dev); |
| 289 | } |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(ntb_unregister_client_dev); |
| 292 | |
| 293 | /** |
| 294 | * ntb_register_client_dev - Register NTB client device |
| 295 | * @device_name: Name of NTB client device |
| 296 | * |
| 297 | * Register an NTB client device with the NTB transport layer |
| 298 | */ |
| 299 | int ntb_register_client_dev(char *device_name) |
| 300 | { |
| 301 | struct ntb_transport_client_dev *client_dev; |
| 302 | struct ntb_transport *nt; |
| 303 | int rc; |
| 304 | |
Jon Mason | 8222b40 | 2013-01-19 02:02:16 -0700 | [diff] [blame] | 305 | if (list_empty(&ntb_transport_list)) |
| 306 | return -ENODEV; |
| 307 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 308 | list_for_each_entry(nt, &ntb_transport_list, entry) { |
| 309 | struct device *dev; |
| 310 | |
| 311 | client_dev = kzalloc(sizeof(struct ntb_transport_client_dev), |
| 312 | GFP_KERNEL); |
| 313 | if (!client_dev) { |
| 314 | rc = -ENOMEM; |
| 315 | goto err; |
| 316 | } |
| 317 | |
| 318 | dev = &client_dev->dev; |
| 319 | |
| 320 | /* setup and register client devices */ |
| 321 | dev_set_name(dev, "%s", device_name); |
| 322 | dev->bus = &ntb_bus_type; |
| 323 | dev->release = ntb_client_release; |
| 324 | dev->parent = &ntb_query_pdev(nt->ndev)->dev; |
| 325 | |
| 326 | rc = device_register(dev); |
| 327 | if (rc) { |
| 328 | kfree(client_dev); |
| 329 | goto err; |
| 330 | } |
| 331 | |
| 332 | list_add_tail(&client_dev->entry, &nt->client_devs); |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | |
| 337 | err: |
| 338 | ntb_unregister_client_dev(device_name); |
| 339 | |
| 340 | return rc; |
| 341 | } |
| 342 | EXPORT_SYMBOL_GPL(ntb_register_client_dev); |
| 343 | |
| 344 | /** |
| 345 | * ntb_register_client - Register NTB client driver |
| 346 | * @drv: NTB client driver to be registered |
| 347 | * |
| 348 | * Register an NTB client driver with the NTB transport layer |
| 349 | * |
| 350 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 351 | */ |
| 352 | int ntb_register_client(struct ntb_client *drv) |
| 353 | { |
| 354 | drv->driver.bus = &ntb_bus_type; |
| 355 | |
Jon Mason | 8222b40 | 2013-01-19 02:02:16 -0700 | [diff] [blame] | 356 | if (list_empty(&ntb_transport_list)) |
| 357 | return -ENODEV; |
| 358 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 359 | return driver_register(&drv->driver); |
| 360 | } |
| 361 | EXPORT_SYMBOL_GPL(ntb_register_client); |
| 362 | |
| 363 | /** |
| 364 | * ntb_unregister_client - Unregister NTB client driver |
| 365 | * @drv: NTB client driver to be unregistered |
| 366 | * |
| 367 | * Unregister an NTB client driver with the NTB transport layer |
| 368 | * |
| 369 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 370 | */ |
| 371 | void ntb_unregister_client(struct ntb_client *drv) |
| 372 | { |
| 373 | driver_unregister(&drv->driver); |
| 374 | } |
| 375 | EXPORT_SYMBOL_GPL(ntb_unregister_client); |
| 376 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 377 | static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count, |
| 378 | loff_t *offp) |
| 379 | { |
| 380 | struct ntb_transport_qp *qp; |
Jon Mason | d7237e2 | 2013-01-19 02:02:25 -0700 | [diff] [blame] | 381 | char *buf; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 382 | ssize_t ret, out_offset, out_count; |
| 383 | |
Jon Mason | d7237e2 | 2013-01-19 02:02:25 -0700 | [diff] [blame] | 384 | out_count = 600; |
| 385 | |
| 386 | buf = kmalloc(out_count, GFP_KERNEL); |
| 387 | if (!buf) |
| 388 | return -ENOMEM; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 389 | |
| 390 | qp = filp->private_data; |
| 391 | out_offset = 0; |
| 392 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 393 | "NTB QP stats\n"); |
| 394 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 395 | "rx_bytes - \t%llu\n", qp->rx_bytes); |
| 396 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 397 | "rx_pkts - \t%llu\n", qp->rx_pkts); |
| 398 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 399 | "rx_ring_empty - %llu\n", qp->rx_ring_empty); |
| 400 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 401 | "rx_err_no_buf - %llu\n", qp->rx_err_no_buf); |
| 402 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 403 | "rx_err_oflow - \t%llu\n", qp->rx_err_oflow); |
| 404 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 405 | "rx_err_ver - \t%llu\n", qp->rx_err_ver); |
| 406 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 407 | "rx_buff - \t%p\n", qp->rx_buff); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 408 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 409 | "rx_index - \t%u\n", qp->rx_index); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 410 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 411 | "rx_max_entry - \t%u\n", qp->rx_max_entry); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 412 | |
| 413 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 414 | "tx_bytes - \t%llu\n", qp->tx_bytes); |
| 415 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 416 | "tx_pkts - \t%llu\n", qp->tx_pkts); |
| 417 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
| 418 | "tx_ring_full - \t%llu\n", qp->tx_ring_full); |
| 419 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 420 | "tx_mw - \t%p\n", qp->tx_mw); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 421 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 422 | "tx_index - \t%u\n", qp->tx_index); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 423 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 424 | "tx_max_entry - \t%u\n", qp->tx_max_entry); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 425 | |
| 426 | out_offset += snprintf(buf + out_offset, out_count - out_offset, |
Jon Mason | d7237e2 | 2013-01-19 02:02:25 -0700 | [diff] [blame] | 427 | "\nQP Link %s\n", (qp->qp_link == NTB_LINK_UP) ? |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 428 | "Up" : "Down"); |
Jon Mason | d7237e2 | 2013-01-19 02:02:25 -0700 | [diff] [blame] | 429 | if (out_offset > out_count) |
| 430 | out_offset = out_count; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 431 | |
| 432 | ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset); |
Jon Mason | d7237e2 | 2013-01-19 02:02:25 -0700 | [diff] [blame] | 433 | kfree(buf); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static const struct file_operations ntb_qp_debugfs_stats = { |
| 438 | .owner = THIS_MODULE, |
Jon Mason | d66d7ac | 2013-01-19 02:02:20 -0700 | [diff] [blame] | 439 | .open = simple_open, |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 440 | .read = debugfs_read, |
| 441 | }; |
| 442 | |
| 443 | static void ntb_list_add(spinlock_t *lock, struct list_head *entry, |
| 444 | struct list_head *list) |
| 445 | { |
| 446 | unsigned long flags; |
| 447 | |
| 448 | spin_lock_irqsave(lock, flags); |
| 449 | list_add_tail(entry, list); |
| 450 | spin_unlock_irqrestore(lock, flags); |
| 451 | } |
| 452 | |
| 453 | static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock, |
| 454 | struct list_head *list) |
| 455 | { |
| 456 | struct ntb_queue_entry *entry; |
| 457 | unsigned long flags; |
| 458 | |
| 459 | spin_lock_irqsave(lock, flags); |
| 460 | if (list_empty(list)) { |
| 461 | entry = NULL; |
| 462 | goto out; |
| 463 | } |
| 464 | entry = list_first_entry(list, struct ntb_queue_entry, entry); |
| 465 | list_del(&entry->entry); |
| 466 | out: |
| 467 | spin_unlock_irqrestore(lock, flags); |
| 468 | |
| 469 | return entry; |
| 470 | } |
| 471 | |
| 472 | static void ntb_transport_setup_qp_mw(struct ntb_transport *nt, |
| 473 | unsigned int qp_num) |
| 474 | { |
| 475 | struct ntb_transport_qp *qp = &nt->qps[qp_num]; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 476 | unsigned int rx_size, num_qps_mw; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 477 | u8 mw_num = QP_TO_MW(qp_num); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 478 | unsigned int i; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 479 | |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 480 | WARN_ON(nt->mw[mw_num].virt_addr == NULL); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 481 | |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 482 | if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW) |
| 483 | num_qps_mw = nt->max_qps / NTB_NUM_MW + 1; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 484 | else |
| 485 | num_qps_mw = nt->max_qps / NTB_NUM_MW; |
| 486 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 487 | rx_size = (unsigned int) nt->mw[mw_num].size / num_qps_mw; |
| 488 | qp->remote_rx_info = nt->mw[mw_num].virt_addr + |
| 489 | (qp_num / NTB_NUM_MW * rx_size); |
| 490 | rx_size -= sizeof(struct ntb_rx_info); |
| 491 | |
Dan Carpenter | cc0f868 | 2013-01-23 22:26:05 +0300 | [diff] [blame] | 492 | qp->rx_buff = qp->remote_rx_info + 1; |
Jon Mason | c9d534c | 2013-02-01 15:45:16 -0700 | [diff] [blame] | 493 | /* Due to housekeeping, there must be atleast 2 buffs */ |
| 494 | qp->rx_max_frame = min(transport_mtu, rx_size / 2); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 495 | qp->rx_max_entry = rx_size / qp->rx_max_frame; |
| 496 | qp->rx_index = 0; |
| 497 | |
Jon Mason | c9d534c | 2013-02-01 15:45:16 -0700 | [diff] [blame] | 498 | qp->remote_rx_info->entry = qp->rx_max_entry - 1; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 499 | |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 500 | /* setup the hdr offsets with 0's */ |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 501 | for (i = 0; i < qp->rx_max_entry; i++) { |
| 502 | void *offset = qp->rx_buff + qp->rx_max_frame * (i + 1) - |
| 503 | sizeof(struct ntb_payload_header); |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 504 | memset(offset, 0, sizeof(struct ntb_payload_header)); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 505 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 506 | |
| 507 | qp->rx_pkts = 0; |
| 508 | qp->tx_pkts = 0; |
Jon Mason | 90f9e93 | 2013-02-01 15:34:35 -0700 | [diff] [blame] | 509 | qp->tx_index = 0; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 512 | static void ntb_free_mw(struct ntb_transport *nt, int num_mw) |
| 513 | { |
| 514 | struct ntb_transport_mw *mw = &nt->mw[num_mw]; |
| 515 | struct pci_dev *pdev = ntb_query_pdev(nt->ndev); |
| 516 | |
| 517 | if (!mw->virt_addr) |
| 518 | return; |
| 519 | |
| 520 | dma_free_coherent(&pdev->dev, mw->size, mw->virt_addr, mw->dma_addr); |
| 521 | mw->virt_addr = NULL; |
| 522 | } |
| 523 | |
Jon Mason | b77b263 | 2013-02-01 15:25:37 -0700 | [diff] [blame] | 524 | static int ntb_set_mw(struct ntb_transport *nt, int num_mw, unsigned int size) |
| 525 | { |
| 526 | struct ntb_transport_mw *mw = &nt->mw[num_mw]; |
| 527 | struct pci_dev *pdev = ntb_query_pdev(nt->ndev); |
| 528 | |
| 529 | /* No need to re-setup */ |
| 530 | if (mw->size == ALIGN(size, 4096)) |
| 531 | return 0; |
| 532 | |
| 533 | if (mw->size != 0) |
| 534 | ntb_free_mw(nt, num_mw); |
| 535 | |
| 536 | /* Alloc memory for receiving data. Must be 4k aligned */ |
| 537 | mw->size = ALIGN(size, 4096); |
| 538 | |
| 539 | mw->virt_addr = dma_alloc_coherent(&pdev->dev, mw->size, &mw->dma_addr, |
| 540 | GFP_KERNEL); |
| 541 | if (!mw->virt_addr) { |
| 542 | mw->size = 0; |
| 543 | dev_err(&pdev->dev, "Unable to allocate MW buffer of size %d\n", |
| 544 | (int) mw->size); |
| 545 | return -ENOMEM; |
| 546 | } |
| 547 | |
| 548 | /* Notify HW the memory location of the receive buffer */ |
| 549 | ntb_set_mw_addr(nt->ndev, num_mw, mw->dma_addr); |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 554 | static void ntb_qp_link_cleanup(struct work_struct *work) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 555 | { |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 556 | struct ntb_transport_qp *qp = container_of(work, |
| 557 | struct ntb_transport_qp, |
| 558 | link_cleanup); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 559 | struct ntb_transport *nt = qp->transport; |
| 560 | struct pci_dev *pdev = ntb_query_pdev(nt->ndev); |
| 561 | |
| 562 | if (qp->qp_link == NTB_LINK_DOWN) { |
| 563 | cancel_delayed_work_sync(&qp->link_work); |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | if (qp->event_handler) |
| 568 | qp->event_handler(qp->cb_data, NTB_LINK_DOWN); |
| 569 | |
| 570 | dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num); |
| 571 | qp->qp_link = NTB_LINK_DOWN; |
| 572 | |
| 573 | if (nt->transport_link == NTB_LINK_UP) |
| 574 | schedule_delayed_work(&qp->link_work, |
| 575 | msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT)); |
| 576 | } |
| 577 | |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 578 | static void ntb_qp_link_down(struct ntb_transport_qp *qp) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 579 | { |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 580 | schedule_work(&qp->link_cleanup); |
| 581 | } |
| 582 | |
| 583 | static void ntb_transport_link_cleanup(struct work_struct *work) |
| 584 | { |
| 585 | struct ntb_transport *nt = container_of(work, struct ntb_transport, |
| 586 | link_cleanup); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 587 | int i; |
| 588 | |
| 589 | if (nt->transport_link == NTB_LINK_DOWN) |
| 590 | cancel_delayed_work_sync(&nt->link_work); |
| 591 | else |
| 592 | nt->transport_link = NTB_LINK_DOWN; |
| 593 | |
| 594 | /* Pass along the info to any clients */ |
| 595 | for (i = 0; i < nt->max_qps; i++) |
| 596 | if (!test_bit(i, &nt->qp_bitmap)) |
| 597 | ntb_qp_link_down(&nt->qps[i]); |
| 598 | |
| 599 | /* The scratchpad registers keep the values if the remote side |
| 600 | * goes down, blast them now to give them a sane value the next |
| 601 | * time they are accessed |
| 602 | */ |
| 603 | for (i = 0; i < MAX_SPAD; i++) |
| 604 | ntb_write_local_spad(nt->ndev, i, 0); |
| 605 | } |
| 606 | |
| 607 | static void ntb_transport_event_callback(void *data, enum ntb_hw_event event) |
| 608 | { |
| 609 | struct ntb_transport *nt = data; |
| 610 | |
| 611 | switch (event) { |
| 612 | case NTB_EVENT_HW_LINK_UP: |
| 613 | schedule_delayed_work(&nt->link_work, 0); |
| 614 | break; |
| 615 | case NTB_EVENT_HW_LINK_DOWN: |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 616 | schedule_work(&nt->link_cleanup); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 617 | break; |
| 618 | default: |
| 619 | BUG(); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | static void ntb_transport_link_work(struct work_struct *work) |
| 624 | { |
| 625 | struct ntb_transport *nt = container_of(work, struct ntb_transport, |
| 626 | link_work.work); |
| 627 | struct ntb_device *ndev = nt->ndev; |
| 628 | struct pci_dev *pdev = ntb_query_pdev(ndev); |
| 629 | u32 val; |
| 630 | int rc, i; |
| 631 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 632 | /* send the local info, in the opposite order of the way we read it */ |
| 633 | for (i = 0; i < NTB_NUM_MW; i++) { |
| 634 | rc = ntb_write_remote_spad(ndev, MW0_SZ_HIGH + (i * 2), |
| 635 | ntb_get_mw_size(ndev, i) >> 32); |
| 636 | if (rc) { |
| 637 | dev_err(&pdev->dev, "Error writing %u to remote spad %d\n", |
| 638 | (u32)(ntb_get_mw_size(ndev, i) >> 32), |
| 639 | MW0_SZ_HIGH + (i * 2)); |
| 640 | goto out; |
| 641 | } |
| 642 | |
| 643 | rc = ntb_write_remote_spad(ndev, MW0_SZ_LOW + (i * 2), |
| 644 | (u32) ntb_get_mw_size(ndev, i)); |
| 645 | if (rc) { |
| 646 | dev_err(&pdev->dev, "Error writing %u to remote spad %d\n", |
| 647 | (u32) ntb_get_mw_size(ndev, i), |
| 648 | MW0_SZ_LOW + (i * 2)); |
| 649 | goto out; |
| 650 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 653 | rc = ntb_write_remote_spad(ndev, NUM_MWS, NTB_NUM_MW); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 654 | if (rc) { |
| 655 | dev_err(&pdev->dev, "Error writing %x to remote spad %d\n", |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 656 | NTB_NUM_MW, NUM_MWS); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 657 | goto out; |
| 658 | } |
| 659 | |
| 660 | rc = ntb_write_remote_spad(ndev, NUM_QPS, nt->max_qps); |
| 661 | if (rc) { |
| 662 | dev_err(&pdev->dev, "Error writing %x to remote spad %d\n", |
| 663 | nt->max_qps, NUM_QPS); |
| 664 | goto out; |
| 665 | } |
| 666 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 667 | rc = ntb_write_remote_spad(ndev, VERSION, NTB_TRANSPORT_VERSION); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 668 | if (rc) { |
| 669 | dev_err(&pdev->dev, "Error writing %x to remote spad %d\n", |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 670 | NTB_TRANSPORT_VERSION, VERSION); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 671 | goto out; |
| 672 | } |
| 673 | |
| 674 | /* Query the remote side for its info */ |
| 675 | rc = ntb_read_remote_spad(ndev, VERSION, &val); |
| 676 | if (rc) { |
| 677 | dev_err(&pdev->dev, "Error reading remote spad %d\n", VERSION); |
| 678 | goto out; |
| 679 | } |
| 680 | |
| 681 | if (val != NTB_TRANSPORT_VERSION) |
| 682 | goto out; |
| 683 | dev_dbg(&pdev->dev, "Remote version = %d\n", val); |
| 684 | |
| 685 | rc = ntb_read_remote_spad(ndev, NUM_QPS, &val); |
| 686 | if (rc) { |
| 687 | dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_QPS); |
| 688 | goto out; |
| 689 | } |
| 690 | |
| 691 | if (val != nt->max_qps) |
| 692 | goto out; |
| 693 | dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val); |
| 694 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 695 | rc = ntb_read_remote_spad(ndev, NUM_MWS, &val); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 696 | if (rc) { |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 697 | dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_MWS); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 698 | goto out; |
| 699 | } |
| 700 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 701 | if (val != NTB_NUM_MW) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 702 | goto out; |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 703 | dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 704 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 705 | for (i = 0; i < NTB_NUM_MW; i++) { |
| 706 | u64 val64; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 707 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 708 | rc = ntb_read_remote_spad(ndev, MW0_SZ_HIGH + (i * 2), &val); |
| 709 | if (rc) { |
| 710 | dev_err(&pdev->dev, "Error reading remote spad %d\n", |
| 711 | MW0_SZ_HIGH + (i * 2)); |
| 712 | goto out1; |
| 713 | } |
| 714 | |
| 715 | val64 = (u64) val << 32; |
| 716 | |
| 717 | rc = ntb_read_remote_spad(ndev, MW0_SZ_LOW + (i * 2), &val); |
| 718 | if (rc) { |
| 719 | dev_err(&pdev->dev, "Error reading remote spad %d\n", |
| 720 | MW0_SZ_LOW + (i * 2)); |
| 721 | goto out1; |
| 722 | } |
| 723 | |
| 724 | val64 |= val; |
| 725 | |
| 726 | dev_dbg(&pdev->dev, "Remote MW%d size = %llu\n", i, val64); |
| 727 | |
| 728 | rc = ntb_set_mw(nt, i, val64); |
| 729 | if (rc) |
| 730 | goto out1; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 733 | nt->transport_link = NTB_LINK_UP; |
| 734 | |
| 735 | for (i = 0; i < nt->max_qps; i++) { |
| 736 | struct ntb_transport_qp *qp = &nt->qps[i]; |
| 737 | |
| 738 | ntb_transport_setup_qp_mw(nt, i); |
| 739 | |
| 740 | if (qp->client_ready == NTB_LINK_UP) |
| 741 | schedule_delayed_work(&qp->link_work, 0); |
| 742 | } |
| 743 | |
| 744 | return; |
| 745 | |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 746 | out1: |
| 747 | for (i = 0; i < NTB_NUM_MW; i++) |
| 748 | ntb_free_mw(nt, i); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 749 | out: |
| 750 | if (ntb_hw_link_status(ndev)) |
| 751 | schedule_delayed_work(&nt->link_work, |
| 752 | msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT)); |
| 753 | } |
| 754 | |
| 755 | static void ntb_qp_link_work(struct work_struct *work) |
| 756 | { |
| 757 | struct ntb_transport_qp *qp = container_of(work, |
| 758 | struct ntb_transport_qp, |
| 759 | link_work.work); |
| 760 | struct pci_dev *pdev = ntb_query_pdev(qp->ndev); |
| 761 | struct ntb_transport *nt = qp->transport; |
| 762 | int rc, val; |
| 763 | |
| 764 | WARN_ON(nt->transport_link != NTB_LINK_UP); |
| 765 | |
| 766 | rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val); |
| 767 | if (rc) { |
| 768 | dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS); |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | rc = ntb_write_remote_spad(nt->ndev, QP_LINKS, val | 1 << qp->qp_num); |
| 773 | if (rc) |
| 774 | dev_err(&pdev->dev, "Error writing %x to remote spad %d\n", |
| 775 | val | 1 << qp->qp_num, QP_LINKS); |
| 776 | |
| 777 | /* query remote spad for qp ready bits */ |
| 778 | rc = ntb_read_remote_spad(nt->ndev, QP_LINKS, &val); |
| 779 | if (rc) |
| 780 | dev_err(&pdev->dev, "Error reading remote spad %d\n", QP_LINKS); |
| 781 | |
| 782 | dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val); |
| 783 | |
| 784 | /* See if the remote side is up */ |
| 785 | if (1 << qp->qp_num & val) { |
| 786 | qp->qp_link = NTB_LINK_UP; |
| 787 | |
| 788 | dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num); |
| 789 | if (qp->event_handler) |
| 790 | qp->event_handler(qp->cb_data, NTB_LINK_UP); |
| 791 | } else if (nt->transport_link == NTB_LINK_UP) |
| 792 | schedule_delayed_work(&qp->link_work, |
| 793 | msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT)); |
| 794 | } |
| 795 | |
| 796 | static void ntb_transport_init_queue(struct ntb_transport *nt, |
| 797 | unsigned int qp_num) |
| 798 | { |
| 799 | struct ntb_transport_qp *qp; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 800 | unsigned int num_qps_mw, tx_size; |
| 801 | u8 mw_num = QP_TO_MW(qp_num); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 802 | |
| 803 | qp = &nt->qps[qp_num]; |
| 804 | qp->qp_num = qp_num; |
| 805 | qp->transport = nt; |
| 806 | qp->ndev = nt->ndev; |
| 807 | qp->qp_link = NTB_LINK_DOWN; |
| 808 | qp->client_ready = NTB_LINK_DOWN; |
| 809 | qp->event_handler = NULL; |
| 810 | |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 811 | if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW) |
| 812 | num_qps_mw = nt->max_qps / NTB_NUM_MW + 1; |
| 813 | else |
| 814 | num_qps_mw = nt->max_qps / NTB_NUM_MW; |
| 815 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 816 | tx_size = (unsigned int) ntb_get_mw_size(qp->ndev, mw_num) / num_qps_mw; |
| 817 | qp->rx_info = ntb_get_mw_vbase(nt->ndev, mw_num) + |
| 818 | (qp_num / NTB_NUM_MW * tx_size); |
| 819 | tx_size -= sizeof(struct ntb_rx_info); |
| 820 | |
Dan Carpenter | cc0f868 | 2013-01-23 22:26:05 +0300 | [diff] [blame] | 821 | qp->tx_mw = qp->rx_info + 1; |
Jon Mason | c9d534c | 2013-02-01 15:45:16 -0700 | [diff] [blame] | 822 | /* Due to housekeeping, there must be atleast 2 buffs */ |
| 823 | qp->tx_max_frame = min(transport_mtu, tx_size / 2); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 824 | qp->tx_max_entry = tx_size / qp->tx_max_frame; |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 825 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 826 | if (nt->debugfs_dir) { |
| 827 | char debugfs_name[4]; |
| 828 | |
| 829 | snprintf(debugfs_name, 4, "qp%d", qp_num); |
| 830 | qp->debugfs_dir = debugfs_create_dir(debugfs_name, |
| 831 | nt->debugfs_dir); |
| 832 | |
| 833 | qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR, |
| 834 | qp->debugfs_dir, qp, |
| 835 | &ntb_qp_debugfs_stats); |
| 836 | } |
| 837 | |
| 838 | INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work); |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 839 | INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 840 | |
| 841 | spin_lock_init(&qp->ntb_rx_pend_q_lock); |
| 842 | spin_lock_init(&qp->ntb_rx_free_q_lock); |
| 843 | spin_lock_init(&qp->ntb_tx_free_q_lock); |
| 844 | |
| 845 | INIT_LIST_HEAD(&qp->rx_pend_q); |
| 846 | INIT_LIST_HEAD(&qp->rx_free_q); |
| 847 | INIT_LIST_HEAD(&qp->tx_free_q); |
| 848 | } |
| 849 | |
| 850 | int ntb_transport_init(struct pci_dev *pdev) |
| 851 | { |
| 852 | struct ntb_transport *nt; |
| 853 | int rc, i; |
| 854 | |
| 855 | nt = kzalloc(sizeof(struct ntb_transport), GFP_KERNEL); |
| 856 | if (!nt) |
| 857 | return -ENOMEM; |
| 858 | |
| 859 | if (debugfs_initialized()) |
| 860 | nt->debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL); |
| 861 | else |
| 862 | nt->debugfs_dir = NULL; |
| 863 | |
| 864 | nt->ndev = ntb_register_transport(pdev, nt); |
| 865 | if (!nt->ndev) { |
| 866 | rc = -EIO; |
| 867 | goto err; |
| 868 | } |
| 869 | |
| 870 | nt->max_qps = min(nt->ndev->max_cbs, max_num_clients); |
| 871 | |
| 872 | nt->qps = kcalloc(nt->max_qps, sizeof(struct ntb_transport_qp), |
| 873 | GFP_KERNEL); |
| 874 | if (!nt->qps) { |
| 875 | rc = -ENOMEM; |
| 876 | goto err1; |
| 877 | } |
| 878 | |
| 879 | nt->qp_bitmap = ((u64) 1 << nt->max_qps) - 1; |
| 880 | |
| 881 | for (i = 0; i < nt->max_qps; i++) |
| 882 | ntb_transport_init_queue(nt, i); |
| 883 | |
| 884 | INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work); |
Jon Mason | 7b4f2d3 | 2013-01-19 02:02:19 -0700 | [diff] [blame] | 885 | INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 886 | |
| 887 | rc = ntb_register_event_callback(nt->ndev, |
| 888 | ntb_transport_event_callback); |
| 889 | if (rc) |
| 890 | goto err2; |
| 891 | |
| 892 | INIT_LIST_HEAD(&nt->client_devs); |
| 893 | rc = ntb_bus_init(nt); |
| 894 | if (rc) |
| 895 | goto err3; |
| 896 | |
| 897 | if (ntb_hw_link_status(nt->ndev)) |
| 898 | schedule_delayed_work(&nt->link_work, 0); |
| 899 | |
| 900 | return 0; |
| 901 | |
| 902 | err3: |
| 903 | ntb_unregister_event_callback(nt->ndev); |
| 904 | err2: |
| 905 | kfree(nt->qps); |
| 906 | err1: |
| 907 | ntb_unregister_transport(nt->ndev); |
| 908 | err: |
| 909 | debugfs_remove_recursive(nt->debugfs_dir); |
| 910 | kfree(nt); |
| 911 | return rc; |
| 912 | } |
| 913 | |
| 914 | void ntb_transport_free(void *transport) |
| 915 | { |
| 916 | struct ntb_transport *nt = transport; |
| 917 | struct pci_dev *pdev; |
| 918 | int i; |
| 919 | |
| 920 | nt->transport_link = NTB_LINK_DOWN; |
| 921 | |
| 922 | /* verify that all the qp's are freed */ |
| 923 | for (i = 0; i < nt->max_qps; i++) |
| 924 | if (!test_bit(i, &nt->qp_bitmap)) |
| 925 | ntb_transport_free_queue(&nt->qps[i]); |
| 926 | |
| 927 | ntb_bus_remove(nt); |
| 928 | |
| 929 | cancel_delayed_work_sync(&nt->link_work); |
| 930 | |
| 931 | debugfs_remove_recursive(nt->debugfs_dir); |
| 932 | |
| 933 | ntb_unregister_event_callback(nt->ndev); |
| 934 | |
| 935 | pdev = ntb_query_pdev(nt->ndev); |
| 936 | |
| 937 | for (i = 0; i < NTB_NUM_MW; i++) |
Jon Mason | 113fc50 | 2013-01-30 11:40:52 -0700 | [diff] [blame] | 938 | ntb_free_mw(nt, i); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 939 | |
| 940 | kfree(nt->qps); |
| 941 | ntb_unregister_transport(nt->ndev); |
| 942 | kfree(nt); |
| 943 | } |
| 944 | |
| 945 | static void ntb_rx_copy_task(struct ntb_transport_qp *qp, |
| 946 | struct ntb_queue_entry *entry, void *offset) |
| 947 | { |
Jon Mason | 448c6fb | 2013-01-19 02:02:27 -0700 | [diff] [blame] | 948 | void *cb_data = entry->cb_data; |
| 949 | unsigned int len = entry->len; |
| 950 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 951 | memcpy(entry->buf, offset, entry->len); |
| 952 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 953 | 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] | 954 | |
| 955 | if (qp->rx_handler && qp->client_ready == NTB_LINK_UP) |
| 956 | qp->rx_handler(qp, qp->cb_data, cb_data, len); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | static int ntb_process_rxc(struct ntb_transport_qp *qp) |
| 960 | { |
| 961 | struct ntb_payload_header *hdr; |
| 962 | struct ntb_queue_entry *entry; |
| 963 | void *offset; |
| 964 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 965 | offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index; |
| 966 | hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header); |
| 967 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 968 | entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q); |
| 969 | if (!entry) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 970 | dev_dbg(&ntb_query_pdev(qp->ndev)->dev, |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 971 | "no buffer - HDR ver %u, len %d, flags %x\n", |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 972 | hdr->ver, hdr->len, hdr->flags); |
| 973 | qp->rx_err_no_buf++; |
| 974 | return -ENOMEM; |
| 975 | } |
| 976 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 977 | if (!(hdr->flags & DESC_DONE_FLAG)) { |
| 978 | ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 979 | &qp->rx_pend_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 980 | qp->rx_ring_empty++; |
| 981 | return -EAGAIN; |
| 982 | } |
| 983 | |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 984 | if (hdr->ver != (u32) qp->rx_pkts) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 985 | dev_dbg(&ntb_query_pdev(qp->ndev)->dev, |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 986 | "qp %d: version mismatch, expected %llu - got %u\n", |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 987 | qp->qp_num, qp->rx_pkts, hdr->ver); |
| 988 | ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 989 | &qp->rx_pend_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 990 | qp->rx_err_ver++; |
| 991 | return -EIO; |
| 992 | } |
| 993 | |
| 994 | if (hdr->flags & LINK_DOWN_FLAG) { |
| 995 | ntb_qp_link_down(qp); |
| 996 | |
| 997 | ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 998 | &qp->rx_pend_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 999 | goto out; |
| 1000 | } |
| 1001 | |
| 1002 | dev_dbg(&ntb_query_pdev(qp->ndev)->dev, |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 1003 | "rx offset %u, ver %u - %d payload received, buf size %d\n", |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1004 | qp->rx_index, hdr->ver, hdr->len, entry->len); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1005 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1006 | if (hdr->len <= entry->len) { |
| 1007 | entry->len = hdr->len; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1008 | ntb_rx_copy_task(qp, entry, offset); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1009 | } else { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1010 | ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1011 | &qp->rx_pend_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1012 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1013 | qp->rx_err_oflow++; |
| 1014 | dev_dbg(&ntb_query_pdev(qp->ndev)->dev, |
| 1015 | "RX overflow! Wanted %d got %d\n", |
| 1016 | hdr->len, entry->len); |
| 1017 | } |
| 1018 | |
| 1019 | qp->rx_bytes += hdr->len; |
| 1020 | qp->rx_pkts++; |
| 1021 | |
| 1022 | out: |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1023 | /* Ensure that the data is fully copied out before clearing the flag */ |
| 1024 | wmb(); |
| 1025 | hdr->flags = 0; |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 1026 | iowrite32(qp->rx_index, &qp->rx_info->entry); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1027 | |
| 1028 | qp->rx_index++; |
| 1029 | qp->rx_index %= qp->rx_max_entry; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1030 | |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | static void ntb_transport_rx(unsigned long data) |
| 1035 | { |
| 1036 | struct ntb_transport_qp *qp = (struct ntb_transport_qp *)data; |
Jon Mason | c336acd | 2013-01-17 15:28:45 -0700 | [diff] [blame^] | 1037 | int rc, i; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1038 | |
Jon Mason | c336acd | 2013-01-17 15:28:45 -0700 | [diff] [blame^] | 1039 | /* Limit the number of packets processed in a single interrupt to |
| 1040 | * provide fairness to others |
| 1041 | */ |
| 1042 | for (i = 0; i < qp->rx_max_entry; i++) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1043 | rc = ntb_process_rxc(qp); |
Jon Mason | c336acd | 2013-01-17 15:28:45 -0700 | [diff] [blame^] | 1044 | if (rc) |
| 1045 | break; |
| 1046 | } |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | static void ntb_transport_rxc_db(void *data, int db_num) |
| 1050 | { |
| 1051 | struct ntb_transport_qp *qp = data; |
| 1052 | |
| 1053 | dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%s: doorbell %d received\n", |
| 1054 | __func__, db_num); |
| 1055 | |
| 1056 | tasklet_schedule(&qp->rx_work); |
| 1057 | } |
| 1058 | |
| 1059 | static void ntb_tx_copy_task(struct ntb_transport_qp *qp, |
| 1060 | struct ntb_queue_entry *entry, |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 1061 | void __iomem *offset) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1062 | { |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 1063 | struct ntb_payload_header __iomem *hdr; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1064 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1065 | memcpy_toio(offset, entry->buf, entry->len); |
| 1066 | |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 1067 | hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header); |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 1068 | iowrite32(entry->len, &hdr->len); |
| 1069 | iowrite32((u32) qp->tx_pkts, &hdr->ver); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1070 | |
| 1071 | /* Ensure that the data is fully copied out before setting the flag */ |
Jon Mason | 842c1dd | 2013-01-19 02:02:17 -0700 | [diff] [blame] | 1072 | wmb(); |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 1073 | iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1074 | |
| 1075 | ntb_ring_sdb(qp->ndev, qp->qp_num); |
| 1076 | |
| 1077 | /* The entry length can only be zero if the packet is intended to be a |
| 1078 | * "link down" or similar. Since no payload is being sent in these |
| 1079 | * cases, there is nothing to add to the completion queue. |
| 1080 | */ |
| 1081 | if (entry->len > 0) { |
| 1082 | qp->tx_bytes += entry->len; |
| 1083 | |
| 1084 | if (qp->tx_handler) |
| 1085 | qp->tx_handler(qp, qp->cb_data, entry->cb_data, |
| 1086 | entry->len); |
| 1087 | } |
| 1088 | |
| 1089 | ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q); |
| 1090 | } |
| 1091 | |
| 1092 | static int ntb_process_tx(struct ntb_transport_qp *qp, |
| 1093 | struct ntb_queue_entry *entry) |
| 1094 | { |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 1095 | void __iomem *offset; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1096 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1097 | offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1098 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1099 | dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%lld - offset %p, tx %u, entry len %d flags %x buff %p\n", |
| 1100 | qp->tx_pkts, offset, qp->tx_index, entry->len, entry->flags, |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1101 | entry->buf); |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1102 | if (qp->tx_index == qp->remote_rx_info->entry) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1103 | qp->tx_ring_full++; |
| 1104 | return -EAGAIN; |
| 1105 | } |
| 1106 | |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 1107 | if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) { |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1108 | if (qp->tx_handler) |
| 1109 | qp->tx_handler(qp->cb_data, qp, NULL, -EIO); |
| 1110 | |
| 1111 | ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, |
| 1112 | &qp->tx_free_q); |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | ntb_tx_copy_task(qp, entry, offset); |
| 1117 | |
Jon Mason | 793c20e | 2013-01-19 02:02:26 -0700 | [diff] [blame] | 1118 | qp->tx_index++; |
| 1119 | qp->tx_index %= qp->tx_max_entry; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1120 | |
| 1121 | qp->tx_pkts++; |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | static void ntb_send_link_down(struct ntb_transport_qp *qp) |
| 1127 | { |
| 1128 | struct pci_dev *pdev = ntb_query_pdev(qp->ndev); |
| 1129 | struct ntb_queue_entry *entry; |
| 1130 | int i, rc; |
| 1131 | |
| 1132 | if (qp->qp_link == NTB_LINK_DOWN) |
| 1133 | return; |
| 1134 | |
| 1135 | qp->qp_link = NTB_LINK_DOWN; |
| 1136 | dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num); |
| 1137 | |
| 1138 | for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) { |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1139 | 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] | 1140 | if (entry) |
| 1141 | break; |
| 1142 | msleep(100); |
| 1143 | } |
| 1144 | |
| 1145 | if (!entry) |
| 1146 | return; |
| 1147 | |
| 1148 | entry->cb_data = NULL; |
| 1149 | entry->buf = NULL; |
| 1150 | entry->len = 0; |
| 1151 | entry->flags = LINK_DOWN_FLAG; |
| 1152 | |
| 1153 | rc = ntb_process_tx(qp, entry); |
| 1154 | if (rc) |
| 1155 | dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n", |
| 1156 | qp->qp_num); |
| 1157 | } |
| 1158 | |
| 1159 | /** |
| 1160 | * ntb_transport_create_queue - Create a new NTB transport layer queue |
| 1161 | * @rx_handler: receive callback function |
| 1162 | * @tx_handler: transmit callback function |
| 1163 | * @event_handler: event callback function |
| 1164 | * |
| 1165 | * Create a new NTB transport layer queue and provide the queue with a callback |
| 1166 | * routine for both transmit and receive. The receive callback routine will be |
| 1167 | * used to pass up data when the transport has received it on the queue. The |
| 1168 | * transmit callback routine will be called when the transport has completed the |
| 1169 | * transmission of the data on the queue and the data is ready to be freed. |
| 1170 | * |
| 1171 | * RETURNS: pointer to newly created ntb_queue, NULL on error. |
| 1172 | */ |
| 1173 | struct ntb_transport_qp * |
| 1174 | ntb_transport_create_queue(void *data, struct pci_dev *pdev, |
| 1175 | const struct ntb_queue_handlers *handlers) |
| 1176 | { |
| 1177 | struct ntb_queue_entry *entry; |
| 1178 | struct ntb_transport_qp *qp; |
| 1179 | struct ntb_transport *nt; |
| 1180 | unsigned int free_queue; |
| 1181 | int rc, i; |
| 1182 | |
| 1183 | nt = ntb_find_transport(pdev); |
| 1184 | if (!nt) |
| 1185 | goto err; |
| 1186 | |
| 1187 | free_queue = ffs(nt->qp_bitmap); |
| 1188 | if (!free_queue) |
| 1189 | goto err; |
| 1190 | |
| 1191 | /* decrement free_queue to make it zero based */ |
| 1192 | free_queue--; |
| 1193 | |
| 1194 | clear_bit(free_queue, &nt->qp_bitmap); |
| 1195 | |
| 1196 | qp = &nt->qps[free_queue]; |
| 1197 | qp->cb_data = data; |
| 1198 | qp->rx_handler = handlers->rx_handler; |
| 1199 | qp->tx_handler = handlers->tx_handler; |
| 1200 | qp->event_handler = handlers->event_handler; |
| 1201 | |
| 1202 | for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) { |
| 1203 | entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC); |
| 1204 | if (!entry) |
| 1205 | goto err1; |
| 1206 | |
| 1207 | ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1208 | &qp->rx_free_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) { |
| 1212 | entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC); |
| 1213 | if (!entry) |
| 1214 | goto err2; |
| 1215 | |
| 1216 | ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1217 | &qp->tx_free_q); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | tasklet_init(&qp->rx_work, ntb_transport_rx, (unsigned long) qp); |
| 1221 | |
| 1222 | rc = ntb_register_db_callback(qp->ndev, free_queue, qp, |
| 1223 | ntb_transport_rxc_db); |
| 1224 | if (rc) |
| 1225 | goto err3; |
| 1226 | |
| 1227 | dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num); |
| 1228 | |
| 1229 | return qp; |
| 1230 | |
| 1231 | err3: |
| 1232 | tasklet_disable(&qp->rx_work); |
| 1233 | err2: |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1234 | 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] | 1235 | kfree(entry); |
| 1236 | err1: |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1237 | 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] | 1238 | kfree(entry); |
| 1239 | set_bit(free_queue, &nt->qp_bitmap); |
| 1240 | err: |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | EXPORT_SYMBOL_GPL(ntb_transport_create_queue); |
| 1244 | |
| 1245 | /** |
| 1246 | * ntb_transport_free_queue - Frees NTB transport queue |
| 1247 | * @qp: NTB queue to be freed |
| 1248 | * |
| 1249 | * Frees NTB transport queue |
| 1250 | */ |
| 1251 | void ntb_transport_free_queue(struct ntb_transport_qp *qp) |
| 1252 | { |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 1253 | struct pci_dev *pdev; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1254 | struct ntb_queue_entry *entry; |
| 1255 | |
| 1256 | if (!qp) |
| 1257 | return; |
| 1258 | |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 1259 | pdev = ntb_query_pdev(qp->ndev); |
| 1260 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1261 | cancel_delayed_work_sync(&qp->link_work); |
| 1262 | |
| 1263 | ntb_unregister_db_callback(qp->ndev, qp->qp_num); |
| 1264 | tasklet_disable(&qp->rx_work); |
| 1265 | |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1266 | 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] | 1267 | kfree(entry); |
| 1268 | |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1269 | 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] | 1270 | dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n"); |
| 1271 | kfree(entry); |
| 1272 | } |
| 1273 | |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1274 | 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] | 1275 | kfree(entry); |
| 1276 | |
| 1277 | set_bit(qp->qp_num, &qp->transport->qp_bitmap); |
| 1278 | |
| 1279 | dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num); |
| 1280 | } |
| 1281 | EXPORT_SYMBOL_GPL(ntb_transport_free_queue); |
| 1282 | |
| 1283 | /** |
| 1284 | * ntb_transport_rx_remove - Dequeues enqueued rx packet |
| 1285 | * @qp: NTB queue to be freed |
| 1286 | * @len: pointer to variable to write enqueued buffers length |
| 1287 | * |
| 1288 | * Dequeues unused buffers from receive queue. Should only be used during |
| 1289 | * shutdown of qp. |
| 1290 | * |
| 1291 | * RETURNS: NULL error value on error, or void* for success. |
| 1292 | */ |
| 1293 | void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len) |
| 1294 | { |
| 1295 | struct ntb_queue_entry *entry; |
| 1296 | void *buf; |
| 1297 | |
| 1298 | if (!qp || qp->client_ready == NTB_LINK_UP) |
| 1299 | return NULL; |
| 1300 | |
| 1301 | entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q); |
| 1302 | if (!entry) |
| 1303 | return NULL; |
| 1304 | |
| 1305 | buf = entry->cb_data; |
| 1306 | *len = entry->len; |
| 1307 | |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1308 | 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] | 1309 | |
| 1310 | return buf; |
| 1311 | } |
| 1312 | EXPORT_SYMBOL_GPL(ntb_transport_rx_remove); |
| 1313 | |
| 1314 | /** |
| 1315 | * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry |
| 1316 | * @qp: NTB transport layer queue the entry is to be enqueued on |
| 1317 | * @cb: per buffer pointer for callback function to use |
| 1318 | * @data: pointer to data buffer that incoming packets will be copied into |
| 1319 | * @len: length of the data buffer |
| 1320 | * |
| 1321 | * Enqueue a new receive buffer onto the transport queue into which a NTB |
| 1322 | * payload can be received into. |
| 1323 | * |
| 1324 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 1325 | */ |
| 1326 | int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data, |
| 1327 | unsigned int len) |
| 1328 | { |
| 1329 | struct ntb_queue_entry *entry; |
| 1330 | |
| 1331 | if (!qp) |
| 1332 | return -EINVAL; |
| 1333 | |
| 1334 | entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q); |
| 1335 | if (!entry) |
| 1336 | return -ENOMEM; |
| 1337 | |
| 1338 | entry->cb_data = cb; |
| 1339 | entry->buf = data; |
| 1340 | entry->len = len; |
| 1341 | |
Jon Mason | f766755 | 2013-01-19 02:02:24 -0700 | [diff] [blame] | 1342 | 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] | 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue); |
| 1347 | |
| 1348 | /** |
| 1349 | * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry |
| 1350 | * @qp: NTB transport layer queue the entry is to be enqueued on |
| 1351 | * @cb: per buffer pointer for callback function to use |
| 1352 | * @data: pointer to data buffer that will be sent |
| 1353 | * @len: length of the data buffer |
| 1354 | * |
| 1355 | * Enqueue a new transmit buffer onto the transport queue from which a NTB |
| 1356 | * payload will be transmitted. This assumes that a lock is behing held to |
| 1357 | * serialize access to the qp. |
| 1358 | * |
| 1359 | * RETURNS: An appropriate -ERRNO error value on error, or zero for success. |
| 1360 | */ |
| 1361 | int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data, |
| 1362 | unsigned int len) |
| 1363 | { |
| 1364 | struct ntb_queue_entry *entry; |
| 1365 | int rc; |
| 1366 | |
| 1367 | if (!qp || qp->qp_link != NTB_LINK_UP || !len) |
| 1368 | return -EINVAL; |
| 1369 | |
| 1370 | entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q); |
| 1371 | if (!entry) |
| 1372 | return -ENOMEM; |
| 1373 | |
| 1374 | entry->cb_data = cb; |
| 1375 | entry->buf = data; |
| 1376 | entry->len = len; |
| 1377 | entry->flags = 0; |
| 1378 | |
| 1379 | rc = ntb_process_tx(qp, entry); |
| 1380 | if (rc) |
| 1381 | ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, |
| 1382 | &qp->tx_free_q); |
| 1383 | |
| 1384 | return rc; |
| 1385 | } |
| 1386 | EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue); |
| 1387 | |
| 1388 | /** |
| 1389 | * ntb_transport_link_up - Notify NTB transport of client readiness to use queue |
| 1390 | * @qp: NTB transport layer queue to be enabled |
| 1391 | * |
| 1392 | * Notify NTB transport layer of client readiness to use queue |
| 1393 | */ |
| 1394 | void ntb_transport_link_up(struct ntb_transport_qp *qp) |
| 1395 | { |
| 1396 | if (!qp) |
| 1397 | return; |
| 1398 | |
| 1399 | qp->client_ready = NTB_LINK_UP; |
| 1400 | |
| 1401 | if (qp->transport->transport_link == NTB_LINK_UP) |
| 1402 | schedule_delayed_work(&qp->link_work, 0); |
| 1403 | } |
| 1404 | EXPORT_SYMBOL_GPL(ntb_transport_link_up); |
| 1405 | |
| 1406 | /** |
| 1407 | * ntb_transport_link_down - Notify NTB transport to no longer enqueue data |
| 1408 | * @qp: NTB transport layer queue to be disabled |
| 1409 | * |
| 1410 | * Notify NTB transport layer of client's desire to no longer receive data on |
| 1411 | * transport queue specified. It is the client's responsibility to ensure all |
| 1412 | * entries on queue are purged or otherwise handled appropraitely. |
| 1413 | */ |
| 1414 | void ntb_transport_link_down(struct ntb_transport_qp *qp) |
| 1415 | { |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 1416 | struct pci_dev *pdev; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1417 | int rc, val; |
| 1418 | |
| 1419 | if (!qp) |
| 1420 | return; |
| 1421 | |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 1422 | pdev = ntb_query_pdev(qp->ndev); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1423 | qp->client_ready = NTB_LINK_DOWN; |
| 1424 | |
| 1425 | rc = ntb_read_local_spad(qp->ndev, QP_LINKS, &val); |
| 1426 | if (rc) { |
| 1427 | dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS); |
| 1428 | return; |
| 1429 | } |
| 1430 | |
| 1431 | rc = ntb_write_remote_spad(qp->ndev, QP_LINKS, |
| 1432 | val & ~(1 << qp->qp_num)); |
| 1433 | if (rc) |
| 1434 | dev_err(&pdev->dev, "Error writing %x to remote spad %d\n", |
| 1435 | val & ~(1 << qp->qp_num), QP_LINKS); |
| 1436 | |
| 1437 | if (qp->qp_link == NTB_LINK_UP) |
| 1438 | ntb_send_link_down(qp); |
| 1439 | else |
| 1440 | cancel_delayed_work_sync(&qp->link_work); |
| 1441 | } |
| 1442 | EXPORT_SYMBOL_GPL(ntb_transport_link_down); |
| 1443 | |
| 1444 | /** |
| 1445 | * ntb_transport_link_query - Query transport link state |
| 1446 | * @qp: NTB transport layer queue to be queried |
| 1447 | * |
| 1448 | * Query connectivity to the remote system of the NTB transport queue |
| 1449 | * |
| 1450 | * RETURNS: true for link up or false for link down |
| 1451 | */ |
| 1452 | bool ntb_transport_link_query(struct ntb_transport_qp *qp) |
| 1453 | { |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 1454 | if (!qp) |
| 1455 | return false; |
| 1456 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1457 | return qp->qp_link == NTB_LINK_UP; |
| 1458 | } |
| 1459 | EXPORT_SYMBOL_GPL(ntb_transport_link_query); |
| 1460 | |
| 1461 | /** |
| 1462 | * ntb_transport_qp_num - Query the qp number |
| 1463 | * @qp: NTB transport layer queue to be queried |
| 1464 | * |
| 1465 | * Query qp number of the NTB transport queue |
| 1466 | * |
| 1467 | * RETURNS: a zero based number specifying the qp number |
| 1468 | */ |
| 1469 | unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp) |
| 1470 | { |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 1471 | if (!qp) |
| 1472 | return 0; |
| 1473 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1474 | return qp->qp_num; |
| 1475 | } |
| 1476 | EXPORT_SYMBOL_GPL(ntb_transport_qp_num); |
| 1477 | |
| 1478 | /** |
| 1479 | * ntb_transport_max_size - Query the max payload size of a qp |
| 1480 | * @qp: NTB transport layer queue to be queried |
| 1481 | * |
| 1482 | * Query the maximum payload size permissible on the given qp |
| 1483 | * |
| 1484 | * RETURNS: the max payload size of a qp |
| 1485 | */ |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 1486 | unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp) |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1487 | { |
Jon Mason | 186f27f | 2013-01-22 11:35:40 -0700 | [diff] [blame] | 1488 | if (!qp) |
| 1489 | return 0; |
| 1490 | |
Jon Mason | ef114ed | 2013-01-19 02:02:18 -0700 | [diff] [blame] | 1491 | return qp->tx_max_frame - sizeof(struct ntb_payload_header); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 1492 | } |
| 1493 | EXPORT_SYMBOL_GPL(ntb_transport_max_size); |