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