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