Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * QLogic iSCSI Offload Driver |
| 3 | * Copyright (c) 2016 Cavium Inc. |
| 4 | * |
| 5 | * This software is available under the terms of the GNU General Public License |
| 6 | * (GPL) Version 2, available from the file COPYING in the main directory of |
| 7 | * this source tree. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/pci.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/if_arp.h> |
| 14 | #include <scsi/iscsi_if.h> |
| 15 | #include <linux/inet.h> |
| 16 | #include <net/arp.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/kthread.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/if_vlan.h> |
| 21 | #include <linux/cpu.h> |
| 22 | |
| 23 | #include <scsi/scsi_cmnd.h> |
| 24 | #include <scsi/scsi_device.h> |
| 25 | #include <scsi/scsi_eh.h> |
| 26 | #include <scsi/scsi_host.h> |
| 27 | #include <scsi/scsi.h> |
| 28 | |
| 29 | #include "qedi.h" |
| 30 | #include "qedi_gbl.h" |
| 31 | #include "qedi_iscsi.h" |
| 32 | |
| 33 | static uint qedi_fw_debug; |
| 34 | module_param(qedi_fw_debug, uint, 0644); |
| 35 | MODULE_PARM_DESC(qedi_fw_debug, " Firmware debug level 0(default) to 3"); |
| 36 | |
| 37 | uint qedi_dbg_log = QEDI_LOG_WARN | QEDI_LOG_SCSI_TM; |
| 38 | module_param(qedi_dbg_log, uint, 0644); |
| 39 | MODULE_PARM_DESC(qedi_dbg_log, " Default debug level"); |
| 40 | |
| 41 | uint qedi_io_tracing; |
| 42 | module_param(qedi_io_tracing, uint, 0644); |
| 43 | MODULE_PARM_DESC(qedi_io_tracing, |
| 44 | " Enable logging of SCSI requests/completions into trace buffer. (default off)."); |
| 45 | |
| 46 | const struct qed_iscsi_ops *qedi_ops; |
| 47 | static struct scsi_transport_template *qedi_scsi_transport; |
| 48 | static struct pci_driver qedi_pci_driver; |
| 49 | static DEFINE_PER_CPU(struct qedi_percpu_s, qedi_percpu); |
| 50 | static LIST_HEAD(qedi_udev_list); |
| 51 | /* Static function declaration */ |
| 52 | static int qedi_alloc_global_queues(struct qedi_ctx *qedi); |
| 53 | static void qedi_free_global_queues(struct qedi_ctx *qedi); |
| 54 | static struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid); |
| 55 | static void qedi_reset_uio_rings(struct qedi_uio_dev *udev); |
| 56 | static void qedi_ll2_free_skbs(struct qedi_ctx *qedi); |
| 57 | |
| 58 | static int qedi_iscsi_event_cb(void *context, u8 fw_event_code, void *fw_handle) |
| 59 | { |
| 60 | struct qedi_ctx *qedi; |
| 61 | struct qedi_endpoint *qedi_ep; |
| 62 | struct async_data *data; |
| 63 | int rval = 0; |
| 64 | |
| 65 | if (!context || !fw_handle) { |
| 66 | QEDI_ERR(NULL, "Recv event with ctx NULL\n"); |
| 67 | return -EINVAL; |
| 68 | } |
| 69 | |
| 70 | qedi = (struct qedi_ctx *)context; |
| 71 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, |
| 72 | "Recv Event %d fw_handle %p\n", fw_event_code, fw_handle); |
| 73 | |
| 74 | data = (struct async_data *)fw_handle; |
| 75 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, |
| 76 | "cid=0x%x tid=0x%x err-code=0x%x fw-dbg-param=0x%x\n", |
| 77 | data->cid, data->itid, data->error_code, |
| 78 | data->fw_debug_param); |
| 79 | |
| 80 | qedi_ep = qedi->ep_tbl[data->cid]; |
| 81 | |
| 82 | if (!qedi_ep) { |
| 83 | QEDI_WARN(&qedi->dbg_ctx, |
| 84 | "Cannot process event, ep already disconnected, cid=0x%x\n", |
| 85 | data->cid); |
| 86 | WARN_ON(1); |
| 87 | return -ENODEV; |
| 88 | } |
| 89 | |
| 90 | switch (fw_event_code) { |
| 91 | case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE: |
| 92 | if (qedi_ep->state == EP_STATE_OFLDCONN_START) |
| 93 | qedi_ep->state = EP_STATE_OFLDCONN_COMPL; |
| 94 | |
| 95 | wake_up_interruptible(&qedi_ep->tcp_ofld_wait); |
| 96 | break; |
| 97 | case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE: |
| 98 | qedi_ep->state = EP_STATE_DISCONN_COMPL; |
| 99 | wake_up_interruptible(&qedi_ep->tcp_ofld_wait); |
| 100 | break; |
| 101 | case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR: |
| 102 | qedi_process_iscsi_error(qedi_ep, data); |
| 103 | break; |
| 104 | case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD: |
| 105 | case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD: |
| 106 | case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME: |
| 107 | case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT: |
| 108 | case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT: |
| 109 | case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2: |
| 110 | case ISCSI_EVENT_TYPE_TCP_CONN_ERROR: |
| 111 | qedi_process_tcp_error(qedi_ep, data); |
| 112 | break; |
| 113 | default: |
| 114 | QEDI_ERR(&qedi->dbg_ctx, "Recv Unknown Event %u\n", |
| 115 | fw_event_code); |
| 116 | } |
| 117 | |
| 118 | return rval; |
| 119 | } |
| 120 | |
| 121 | static int qedi_uio_open(struct uio_info *uinfo, struct inode *inode) |
| 122 | { |
| 123 | struct qedi_uio_dev *udev = uinfo->priv; |
| 124 | struct qedi_ctx *qedi = udev->qedi; |
| 125 | |
| 126 | if (!capable(CAP_NET_ADMIN)) |
| 127 | return -EPERM; |
| 128 | |
| 129 | if (udev->uio_dev != -1) |
| 130 | return -EBUSY; |
| 131 | |
| 132 | rtnl_lock(); |
| 133 | udev->uio_dev = iminor(inode); |
| 134 | qedi_reset_uio_rings(udev); |
| 135 | set_bit(UIO_DEV_OPENED, &qedi->flags); |
| 136 | rtnl_unlock(); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int qedi_uio_close(struct uio_info *uinfo, struct inode *inode) |
| 142 | { |
| 143 | struct qedi_uio_dev *udev = uinfo->priv; |
| 144 | struct qedi_ctx *qedi = udev->qedi; |
| 145 | |
| 146 | udev->uio_dev = -1; |
| 147 | clear_bit(UIO_DEV_OPENED, &qedi->flags); |
| 148 | qedi_ll2_free_skbs(qedi); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static void __qedi_free_uio_rings(struct qedi_uio_dev *udev) |
| 153 | { |
Arun Easi | 5e901d0 | 2017-05-19 01:33:15 -0700 | [diff] [blame^] | 154 | if (udev->uctrl) { |
| 155 | free_page((unsigned long)udev->uctrl); |
| 156 | udev->uctrl = NULL; |
| 157 | } |
| 158 | |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 159 | if (udev->ll2_ring) { |
| 160 | free_page((unsigned long)udev->ll2_ring); |
| 161 | udev->ll2_ring = NULL; |
| 162 | } |
| 163 | |
| 164 | if (udev->ll2_buf) { |
| 165 | free_pages((unsigned long)udev->ll2_buf, 2); |
| 166 | udev->ll2_buf = NULL; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static void __qedi_free_uio(struct qedi_uio_dev *udev) |
| 171 | { |
| 172 | uio_unregister_device(&udev->qedi_uinfo); |
| 173 | |
| 174 | __qedi_free_uio_rings(udev); |
| 175 | |
| 176 | pci_dev_put(udev->pdev); |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 177 | kfree(udev); |
| 178 | } |
| 179 | |
| 180 | static void qedi_free_uio(struct qedi_uio_dev *udev) |
| 181 | { |
| 182 | if (!udev) |
| 183 | return; |
| 184 | |
| 185 | list_del_init(&udev->list); |
| 186 | __qedi_free_uio(udev); |
| 187 | } |
| 188 | |
| 189 | static void qedi_reset_uio_rings(struct qedi_uio_dev *udev) |
| 190 | { |
| 191 | struct qedi_ctx *qedi = NULL; |
| 192 | struct qedi_uio_ctrl *uctrl = NULL; |
| 193 | |
| 194 | qedi = udev->qedi; |
| 195 | uctrl = udev->uctrl; |
| 196 | |
| 197 | spin_lock_bh(&qedi->ll2_lock); |
| 198 | uctrl->host_rx_cons = 0; |
| 199 | uctrl->hw_rx_prod = 0; |
| 200 | uctrl->hw_rx_bd_prod = 0; |
| 201 | uctrl->host_rx_bd_cons = 0; |
| 202 | |
| 203 | memset(udev->ll2_ring, 0, udev->ll2_ring_size); |
| 204 | memset(udev->ll2_buf, 0, udev->ll2_buf_size); |
| 205 | spin_unlock_bh(&qedi->ll2_lock); |
| 206 | } |
| 207 | |
| 208 | static int __qedi_alloc_uio_rings(struct qedi_uio_dev *udev) |
| 209 | { |
| 210 | int rc = 0; |
| 211 | |
| 212 | if (udev->ll2_ring || udev->ll2_buf) |
| 213 | return rc; |
| 214 | |
Arun Easi | 5e901d0 | 2017-05-19 01:33:15 -0700 | [diff] [blame^] | 215 | /* Memory for control area. */ |
| 216 | udev->uctrl = (void *)get_zeroed_page(GFP_KERNEL); |
| 217 | if (!udev->uctrl) |
| 218 | return -ENOMEM; |
| 219 | |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 220 | /* Allocating memory for LL2 ring */ |
| 221 | udev->ll2_ring_size = QEDI_PAGE_SIZE; |
| 222 | udev->ll2_ring = (void *)get_zeroed_page(GFP_KERNEL | __GFP_COMP); |
| 223 | if (!udev->ll2_ring) { |
| 224 | rc = -ENOMEM; |
| 225 | goto exit_alloc_ring; |
| 226 | } |
| 227 | |
| 228 | /* Allocating memory for Tx/Rx pkt buffer */ |
| 229 | udev->ll2_buf_size = TX_RX_RING * LL2_SINGLE_BUF_SIZE; |
| 230 | udev->ll2_buf_size = QEDI_PAGE_ALIGN(udev->ll2_buf_size); |
| 231 | udev->ll2_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_COMP | |
| 232 | __GFP_ZERO, 2); |
| 233 | if (!udev->ll2_buf) { |
| 234 | rc = -ENOMEM; |
| 235 | goto exit_alloc_buf; |
| 236 | } |
| 237 | return rc; |
| 238 | |
| 239 | exit_alloc_buf: |
| 240 | free_page((unsigned long)udev->ll2_ring); |
| 241 | udev->ll2_ring = NULL; |
| 242 | exit_alloc_ring: |
| 243 | return rc; |
| 244 | } |
| 245 | |
| 246 | static int qedi_alloc_uio_rings(struct qedi_ctx *qedi) |
| 247 | { |
| 248 | struct qedi_uio_dev *udev = NULL; |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 249 | int rc = 0; |
| 250 | |
| 251 | list_for_each_entry(udev, &qedi_udev_list, list) { |
| 252 | if (udev->pdev == qedi->pdev) { |
| 253 | udev->qedi = qedi; |
| 254 | if (__qedi_alloc_uio_rings(udev)) { |
| 255 | udev->qedi = NULL; |
| 256 | return -ENOMEM; |
| 257 | } |
| 258 | qedi->udev = udev; |
| 259 | return 0; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | udev = kzalloc(sizeof(*udev), GFP_KERNEL); |
| 264 | if (!udev) { |
| 265 | rc = -ENOMEM; |
| 266 | goto err_udev; |
| 267 | } |
| 268 | |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 269 | udev->uio_dev = -1; |
| 270 | |
| 271 | udev->qedi = qedi; |
| 272 | udev->pdev = qedi->pdev; |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 273 | |
| 274 | rc = __qedi_alloc_uio_rings(udev); |
| 275 | if (rc) |
Arun Easi | 5e901d0 | 2017-05-19 01:33:15 -0700 | [diff] [blame^] | 276 | goto err_uctrl; |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 277 | |
| 278 | list_add(&udev->list, &qedi_udev_list); |
| 279 | |
| 280 | pci_dev_get(udev->pdev); |
| 281 | qedi->udev = udev; |
| 282 | |
| 283 | udev->tx_pkt = udev->ll2_buf; |
| 284 | udev->rx_pkt = udev->ll2_buf + LL2_SINGLE_BUF_SIZE; |
| 285 | return 0; |
| 286 | |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 287 | err_uctrl: |
| 288 | kfree(udev); |
| 289 | err_udev: |
| 290 | return -ENOMEM; |
| 291 | } |
| 292 | |
| 293 | static int qedi_init_uio(struct qedi_ctx *qedi) |
| 294 | { |
| 295 | struct qedi_uio_dev *udev = qedi->udev; |
| 296 | struct uio_info *uinfo; |
| 297 | int ret = 0; |
| 298 | |
| 299 | if (!udev) |
| 300 | return -ENOMEM; |
| 301 | |
| 302 | uinfo = &udev->qedi_uinfo; |
| 303 | |
| 304 | uinfo->mem[0].addr = (unsigned long)udev->uctrl; |
| 305 | uinfo->mem[0].size = sizeof(struct qedi_uio_ctrl); |
| 306 | uinfo->mem[0].memtype = UIO_MEM_LOGICAL; |
| 307 | |
| 308 | uinfo->mem[1].addr = (unsigned long)udev->ll2_ring; |
| 309 | uinfo->mem[1].size = udev->ll2_ring_size; |
| 310 | uinfo->mem[1].memtype = UIO_MEM_LOGICAL; |
| 311 | |
| 312 | uinfo->mem[2].addr = (unsigned long)udev->ll2_buf; |
| 313 | uinfo->mem[2].size = udev->ll2_buf_size; |
| 314 | uinfo->mem[2].memtype = UIO_MEM_LOGICAL; |
| 315 | |
| 316 | uinfo->name = "qedi_uio"; |
| 317 | uinfo->version = QEDI_MODULE_VERSION; |
| 318 | uinfo->irq = UIO_IRQ_CUSTOM; |
| 319 | |
| 320 | uinfo->open = qedi_uio_open; |
| 321 | uinfo->release = qedi_uio_close; |
| 322 | |
| 323 | if (udev->uio_dev == -1) { |
| 324 | if (!uinfo->priv) { |
| 325 | uinfo->priv = udev; |
| 326 | |
| 327 | ret = uio_register_device(&udev->pdev->dev, uinfo); |
| 328 | if (ret) { |
| 329 | QEDI_ERR(&qedi->dbg_ctx, |
| 330 | "UIO registration failed\n"); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | static int qedi_alloc_and_init_sb(struct qedi_ctx *qedi, |
| 339 | struct qed_sb_info *sb_info, u16 sb_id) |
| 340 | { |
| 341 | struct status_block *sb_virt; |
| 342 | dma_addr_t sb_phys; |
| 343 | int ret; |
| 344 | |
| 345 | sb_virt = dma_alloc_coherent(&qedi->pdev->dev, |
| 346 | sizeof(struct status_block), &sb_phys, |
| 347 | GFP_KERNEL); |
| 348 | if (!sb_virt) { |
| 349 | QEDI_ERR(&qedi->dbg_ctx, |
| 350 | "Status block allocation failed for id = %d.\n", |
| 351 | sb_id); |
| 352 | return -ENOMEM; |
| 353 | } |
| 354 | |
| 355 | ret = qedi_ops->common->sb_init(qedi->cdev, sb_info, sb_virt, sb_phys, |
| 356 | sb_id, QED_SB_TYPE_STORAGE); |
| 357 | if (ret) { |
| 358 | QEDI_ERR(&qedi->dbg_ctx, |
| 359 | "Status block initialization failed for id = %d.\n", |
| 360 | sb_id); |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static void qedi_free_sb(struct qedi_ctx *qedi) |
| 368 | { |
| 369 | struct qed_sb_info *sb_info; |
| 370 | int id; |
| 371 | |
| 372 | for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) { |
| 373 | sb_info = &qedi->sb_array[id]; |
| 374 | if (sb_info->sb_virt) |
| 375 | dma_free_coherent(&qedi->pdev->dev, |
| 376 | sizeof(*sb_info->sb_virt), |
| 377 | (void *)sb_info->sb_virt, |
| 378 | sb_info->sb_phys); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static void qedi_free_fp(struct qedi_ctx *qedi) |
| 383 | { |
| 384 | kfree(qedi->fp_array); |
| 385 | kfree(qedi->sb_array); |
| 386 | } |
| 387 | |
| 388 | static void qedi_destroy_fp(struct qedi_ctx *qedi) |
| 389 | { |
| 390 | qedi_free_sb(qedi); |
| 391 | qedi_free_fp(qedi); |
| 392 | } |
| 393 | |
| 394 | static int qedi_alloc_fp(struct qedi_ctx *qedi) |
| 395 | { |
| 396 | int ret = 0; |
| 397 | |
| 398 | qedi->fp_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi), |
| 399 | sizeof(struct qedi_fastpath), GFP_KERNEL); |
| 400 | if (!qedi->fp_array) { |
| 401 | QEDI_ERR(&qedi->dbg_ctx, |
| 402 | "fastpath fp array allocation failed.\n"); |
| 403 | return -ENOMEM; |
| 404 | } |
| 405 | |
| 406 | qedi->sb_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi), |
| 407 | sizeof(struct qed_sb_info), GFP_KERNEL); |
| 408 | if (!qedi->sb_array) { |
| 409 | QEDI_ERR(&qedi->dbg_ctx, |
| 410 | "fastpath sb array allocation failed.\n"); |
| 411 | ret = -ENOMEM; |
| 412 | goto free_fp; |
| 413 | } |
| 414 | |
| 415 | return ret; |
| 416 | |
| 417 | free_fp: |
| 418 | qedi_free_fp(qedi); |
| 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | static void qedi_int_fp(struct qedi_ctx *qedi) |
| 423 | { |
| 424 | struct qedi_fastpath *fp; |
| 425 | int id; |
| 426 | |
| 427 | memset(qedi->fp_array, 0, MIN_NUM_CPUS_MSIX(qedi) * |
| 428 | sizeof(*qedi->fp_array)); |
| 429 | memset(qedi->sb_array, 0, MIN_NUM_CPUS_MSIX(qedi) * |
| 430 | sizeof(*qedi->sb_array)); |
| 431 | |
| 432 | for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) { |
| 433 | fp = &qedi->fp_array[id]; |
| 434 | fp->sb_info = &qedi->sb_array[id]; |
| 435 | fp->sb_id = id; |
| 436 | fp->qedi = qedi; |
| 437 | snprintf(fp->name, sizeof(fp->name), "%s-fp-%d", |
| 438 | "qedi", id); |
| 439 | |
| 440 | /* fp_array[i] ---- irq cookie |
| 441 | * So init data which is needed in int ctx |
| 442 | */ |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | static int qedi_prepare_fp(struct qedi_ctx *qedi) |
| 447 | { |
| 448 | struct qedi_fastpath *fp; |
| 449 | int id, ret = 0; |
| 450 | |
| 451 | ret = qedi_alloc_fp(qedi); |
| 452 | if (ret) |
| 453 | goto err; |
| 454 | |
| 455 | qedi_int_fp(qedi); |
| 456 | |
| 457 | for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) { |
| 458 | fp = &qedi->fp_array[id]; |
| 459 | ret = qedi_alloc_and_init_sb(qedi, fp->sb_info, fp->sb_id); |
| 460 | if (ret) { |
| 461 | QEDI_ERR(&qedi->dbg_ctx, |
| 462 | "SB allocation and initialization failed.\n"); |
| 463 | ret = -EIO; |
| 464 | goto err_init; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | |
| 470 | err_init: |
| 471 | qedi_free_sb(qedi); |
| 472 | qedi_free_fp(qedi); |
| 473 | err: |
| 474 | return ret; |
| 475 | } |
| 476 | |
| 477 | static int qedi_setup_cid_que(struct qedi_ctx *qedi) |
| 478 | { |
| 479 | int i; |
| 480 | |
| 481 | qedi->cid_que.cid_que_base = kmalloc_array(qedi->max_active_conns, |
| 482 | sizeof(u32), GFP_KERNEL); |
| 483 | if (!qedi->cid_que.cid_que_base) |
| 484 | return -ENOMEM; |
| 485 | |
| 486 | qedi->cid_que.conn_cid_tbl = kmalloc_array(qedi->max_active_conns, |
| 487 | sizeof(struct qedi_conn *), |
| 488 | GFP_KERNEL); |
| 489 | if (!qedi->cid_que.conn_cid_tbl) { |
| 490 | kfree(qedi->cid_que.cid_que_base); |
| 491 | qedi->cid_que.cid_que_base = NULL; |
| 492 | return -ENOMEM; |
| 493 | } |
| 494 | |
| 495 | qedi->cid_que.cid_que = (u32 *)qedi->cid_que.cid_que_base; |
| 496 | qedi->cid_que.cid_q_prod_idx = 0; |
| 497 | qedi->cid_que.cid_q_cons_idx = 0; |
| 498 | qedi->cid_que.cid_q_max_idx = qedi->max_active_conns; |
| 499 | qedi->cid_que.cid_free_cnt = qedi->max_active_conns; |
| 500 | |
| 501 | for (i = 0; i < qedi->max_active_conns; i++) { |
| 502 | qedi->cid_que.cid_que[i] = i; |
| 503 | qedi->cid_que.conn_cid_tbl[i] = NULL; |
| 504 | } |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static void qedi_release_cid_que(struct qedi_ctx *qedi) |
| 510 | { |
| 511 | kfree(qedi->cid_que.cid_que_base); |
| 512 | qedi->cid_que.cid_que_base = NULL; |
| 513 | |
| 514 | kfree(qedi->cid_que.conn_cid_tbl); |
| 515 | qedi->cid_que.conn_cid_tbl = NULL; |
| 516 | } |
| 517 | |
| 518 | static int qedi_init_id_tbl(struct qedi_portid_tbl *id_tbl, u16 size, |
| 519 | u16 start_id, u16 next) |
| 520 | { |
| 521 | id_tbl->start = start_id; |
| 522 | id_tbl->max = size; |
| 523 | id_tbl->next = next; |
| 524 | spin_lock_init(&id_tbl->lock); |
| 525 | id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL); |
| 526 | if (!id_tbl->table) |
| 527 | return -ENOMEM; |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static void qedi_free_id_tbl(struct qedi_portid_tbl *id_tbl) |
| 533 | { |
| 534 | kfree(id_tbl->table); |
| 535 | id_tbl->table = NULL; |
| 536 | } |
| 537 | |
| 538 | int qedi_alloc_id(struct qedi_portid_tbl *id_tbl, u16 id) |
| 539 | { |
| 540 | int ret = -1; |
| 541 | |
| 542 | id -= id_tbl->start; |
| 543 | if (id >= id_tbl->max) |
| 544 | return ret; |
| 545 | |
| 546 | spin_lock(&id_tbl->lock); |
| 547 | if (!test_bit(id, id_tbl->table)) { |
| 548 | set_bit(id, id_tbl->table); |
| 549 | ret = 0; |
| 550 | } |
| 551 | spin_unlock(&id_tbl->lock); |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | u16 qedi_alloc_new_id(struct qedi_portid_tbl *id_tbl) |
| 556 | { |
| 557 | u16 id; |
| 558 | |
| 559 | spin_lock(&id_tbl->lock); |
| 560 | id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next); |
| 561 | if (id >= id_tbl->max) { |
| 562 | id = QEDI_LOCAL_PORT_INVALID; |
| 563 | if (id_tbl->next != 0) { |
| 564 | id = find_first_zero_bit(id_tbl->table, id_tbl->next); |
| 565 | if (id >= id_tbl->next) |
| 566 | id = QEDI_LOCAL_PORT_INVALID; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if (id < id_tbl->max) { |
| 571 | set_bit(id, id_tbl->table); |
| 572 | id_tbl->next = (id + 1) & (id_tbl->max - 1); |
| 573 | id += id_tbl->start; |
| 574 | } |
| 575 | |
| 576 | spin_unlock(&id_tbl->lock); |
| 577 | |
| 578 | return id; |
| 579 | } |
| 580 | |
| 581 | void qedi_free_id(struct qedi_portid_tbl *id_tbl, u16 id) |
| 582 | { |
| 583 | if (id == QEDI_LOCAL_PORT_INVALID) |
| 584 | return; |
| 585 | |
| 586 | id -= id_tbl->start; |
| 587 | if (id >= id_tbl->max) |
| 588 | return; |
| 589 | |
| 590 | clear_bit(id, id_tbl->table); |
| 591 | } |
| 592 | |
| 593 | static void qedi_cm_free_mem(struct qedi_ctx *qedi) |
| 594 | { |
| 595 | kfree(qedi->ep_tbl); |
| 596 | qedi->ep_tbl = NULL; |
| 597 | qedi_free_id_tbl(&qedi->lcl_port_tbl); |
| 598 | } |
| 599 | |
| 600 | static int qedi_cm_alloc_mem(struct qedi_ctx *qedi) |
| 601 | { |
| 602 | u16 port_id; |
| 603 | |
| 604 | qedi->ep_tbl = kzalloc((qedi->max_active_conns * |
| 605 | sizeof(struct qedi_endpoint *)), GFP_KERNEL); |
| 606 | if (!qedi->ep_tbl) |
| 607 | return -ENOMEM; |
| 608 | port_id = prandom_u32() % QEDI_LOCAL_PORT_RANGE; |
| 609 | if (qedi_init_id_tbl(&qedi->lcl_port_tbl, QEDI_LOCAL_PORT_RANGE, |
| 610 | QEDI_LOCAL_PORT_MIN, port_id)) { |
| 611 | qedi_cm_free_mem(qedi); |
| 612 | return -ENOMEM; |
| 613 | } |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static struct qedi_ctx *qedi_host_alloc(struct pci_dev *pdev) |
| 619 | { |
| 620 | struct Scsi_Host *shost; |
| 621 | struct qedi_ctx *qedi = NULL; |
| 622 | |
| 623 | shost = iscsi_host_alloc(&qedi_host_template, |
| 624 | sizeof(struct qedi_ctx), 0); |
| 625 | if (!shost) { |
| 626 | QEDI_ERR(NULL, "Could not allocate shost\n"); |
| 627 | goto exit_setup_shost; |
| 628 | } |
| 629 | |
| 630 | shost->max_id = QEDI_MAX_ISCSI_CONNS_PER_HBA; |
| 631 | shost->max_channel = 0; |
| 632 | shost->max_lun = ~0; |
| 633 | shost->max_cmd_len = 16; |
| 634 | shost->transportt = qedi_scsi_transport; |
| 635 | |
| 636 | qedi = iscsi_host_priv(shost); |
| 637 | memset(qedi, 0, sizeof(*qedi)); |
| 638 | qedi->shost = shost; |
| 639 | qedi->dbg_ctx.host_no = shost->host_no; |
| 640 | qedi->pdev = pdev; |
| 641 | qedi->dbg_ctx.pdev = pdev; |
| 642 | qedi->max_active_conns = ISCSI_MAX_SESS_PER_HBA; |
| 643 | qedi->max_sqes = QEDI_SQ_SIZE; |
| 644 | |
| 645 | if (shost_use_blk_mq(shost)) |
| 646 | shost->nr_hw_queues = MIN_NUM_CPUS_MSIX(qedi); |
| 647 | |
| 648 | pci_set_drvdata(pdev, qedi); |
| 649 | |
| 650 | exit_setup_shost: |
| 651 | return qedi; |
| 652 | } |
| 653 | |
| 654 | static int qedi_ll2_rx(void *cookie, struct sk_buff *skb, u32 arg1, u32 arg2) |
| 655 | { |
| 656 | struct qedi_ctx *qedi = (struct qedi_ctx *)cookie; |
| 657 | struct qedi_uio_dev *udev; |
| 658 | struct qedi_uio_ctrl *uctrl; |
| 659 | struct skb_work_list *work; |
| 660 | u32 prod; |
| 661 | |
| 662 | if (!qedi) { |
| 663 | QEDI_ERR(NULL, "qedi is NULL\n"); |
| 664 | return -1; |
| 665 | } |
| 666 | |
| 667 | if (!test_bit(UIO_DEV_OPENED, &qedi->flags)) { |
| 668 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UIO, |
| 669 | "UIO DEV is not opened\n"); |
| 670 | kfree_skb(skb); |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | udev = qedi->udev; |
| 675 | uctrl = udev->uctrl; |
| 676 | |
| 677 | work = kzalloc(sizeof(*work), GFP_ATOMIC); |
| 678 | if (!work) { |
| 679 | QEDI_WARN(&qedi->dbg_ctx, |
| 680 | "Could not allocate work so dropping frame.\n"); |
| 681 | kfree_skb(skb); |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | INIT_LIST_HEAD(&work->list); |
| 686 | work->skb = skb; |
| 687 | |
| 688 | if (skb_vlan_tag_present(skb)) |
| 689 | work->vlan_id = skb_vlan_tag_get(skb); |
| 690 | |
| 691 | if (work->vlan_id) |
| 692 | __vlan_insert_tag(work->skb, htons(ETH_P_8021Q), work->vlan_id); |
| 693 | |
| 694 | spin_lock_bh(&qedi->ll2_lock); |
| 695 | list_add_tail(&work->list, &qedi->ll2_skb_list); |
| 696 | |
| 697 | ++uctrl->hw_rx_prod_cnt; |
| 698 | prod = (uctrl->hw_rx_prod + 1) % RX_RING; |
| 699 | if (prod != uctrl->host_rx_cons) { |
| 700 | uctrl->hw_rx_prod = prod; |
| 701 | spin_unlock_bh(&qedi->ll2_lock); |
| 702 | wake_up_process(qedi->ll2_recv_thread); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | spin_unlock_bh(&qedi->ll2_lock); |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | /* map this skb to iscsiuio mmaped region */ |
| 711 | static int qedi_ll2_process_skb(struct qedi_ctx *qedi, struct sk_buff *skb, |
| 712 | u16 vlan_id) |
| 713 | { |
| 714 | struct qedi_uio_dev *udev = NULL; |
| 715 | struct qedi_uio_ctrl *uctrl = NULL; |
| 716 | struct qedi_rx_bd rxbd; |
| 717 | struct qedi_rx_bd *p_rxbd; |
| 718 | u32 rx_bd_prod; |
| 719 | void *pkt; |
| 720 | int len = 0; |
| 721 | |
| 722 | if (!qedi) { |
| 723 | QEDI_ERR(NULL, "qedi is NULL\n"); |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | udev = qedi->udev; |
| 728 | uctrl = udev->uctrl; |
| 729 | pkt = udev->rx_pkt + (uctrl->hw_rx_prod * LL2_SINGLE_BUF_SIZE); |
| 730 | len = min_t(u32, skb->len, (u32)LL2_SINGLE_BUF_SIZE); |
| 731 | memcpy(pkt, skb->data, len); |
| 732 | |
| 733 | memset(&rxbd, 0, sizeof(rxbd)); |
| 734 | rxbd.rx_pkt_index = uctrl->hw_rx_prod; |
| 735 | rxbd.rx_pkt_len = len; |
| 736 | rxbd.vlan_id = vlan_id; |
| 737 | |
| 738 | uctrl->hw_rx_bd_prod = (uctrl->hw_rx_bd_prod + 1) % QEDI_NUM_RX_BD; |
| 739 | rx_bd_prod = uctrl->hw_rx_bd_prod; |
| 740 | p_rxbd = (struct qedi_rx_bd *)udev->ll2_ring; |
| 741 | p_rxbd += rx_bd_prod; |
| 742 | |
| 743 | memcpy(p_rxbd, &rxbd, sizeof(rxbd)); |
| 744 | |
| 745 | /* notify the iscsiuio about new packet */ |
| 746 | uio_event_notify(&udev->qedi_uinfo); |
| 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static void qedi_ll2_free_skbs(struct qedi_ctx *qedi) |
| 752 | { |
| 753 | struct skb_work_list *work, *work_tmp; |
| 754 | |
| 755 | spin_lock_bh(&qedi->ll2_lock); |
| 756 | list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list, list) { |
| 757 | list_del(&work->list); |
| 758 | if (work->skb) |
| 759 | kfree_skb(work->skb); |
| 760 | kfree(work); |
| 761 | } |
| 762 | spin_unlock_bh(&qedi->ll2_lock); |
| 763 | } |
| 764 | |
| 765 | static int qedi_ll2_recv_thread(void *arg) |
| 766 | { |
| 767 | struct qedi_ctx *qedi = (struct qedi_ctx *)arg; |
| 768 | struct skb_work_list *work, *work_tmp; |
| 769 | |
| 770 | set_user_nice(current, -20); |
| 771 | |
| 772 | while (!kthread_should_stop()) { |
| 773 | spin_lock_bh(&qedi->ll2_lock); |
| 774 | list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list, |
| 775 | list) { |
| 776 | list_del(&work->list); |
| 777 | qedi_ll2_process_skb(qedi, work->skb, work->vlan_id); |
| 778 | kfree_skb(work->skb); |
| 779 | kfree(work); |
| 780 | } |
| 781 | set_current_state(TASK_INTERRUPTIBLE); |
| 782 | spin_unlock_bh(&qedi->ll2_lock); |
| 783 | schedule(); |
| 784 | } |
| 785 | |
| 786 | __set_current_state(TASK_RUNNING); |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static int qedi_set_iscsi_pf_param(struct qedi_ctx *qedi) |
| 791 | { |
| 792 | u8 num_sq_pages; |
| 793 | u32 log_page_size; |
| 794 | int rval = 0; |
| 795 | |
| 796 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "Min number of MSIX %d\n", |
| 797 | MIN_NUM_CPUS_MSIX(qedi)); |
| 798 | |
| 799 | num_sq_pages = (MAX_OUSTANDING_TASKS_PER_CON * 8) / PAGE_SIZE; |
| 800 | |
| 801 | qedi->num_queues = MIN_NUM_CPUS_MSIX(qedi); |
| 802 | |
| 803 | memset(&qedi->pf_params.iscsi_pf_params, 0, |
| 804 | sizeof(qedi->pf_params.iscsi_pf_params)); |
| 805 | |
| 806 | qedi->p_cpuq = pci_alloc_consistent(qedi->pdev, |
| 807 | qedi->num_queues * sizeof(struct qedi_glbl_q_params), |
| 808 | &qedi->hw_p_cpuq); |
| 809 | if (!qedi->p_cpuq) { |
| 810 | QEDI_ERR(&qedi->dbg_ctx, "pci_alloc_consistent fail\n"); |
| 811 | rval = -1; |
| 812 | goto err_alloc_mem; |
| 813 | } |
| 814 | |
| 815 | rval = qedi_alloc_global_queues(qedi); |
| 816 | if (rval) { |
| 817 | QEDI_ERR(&qedi->dbg_ctx, "Global queue allocation failed.\n"); |
| 818 | rval = -1; |
| 819 | goto err_alloc_mem; |
| 820 | } |
| 821 | |
| 822 | qedi->pf_params.iscsi_pf_params.num_cons = QEDI_MAX_ISCSI_CONNS_PER_HBA; |
| 823 | qedi->pf_params.iscsi_pf_params.num_tasks = QEDI_MAX_ISCSI_TASK; |
| 824 | qedi->pf_params.iscsi_pf_params.half_way_close_timeout = 10; |
| 825 | qedi->pf_params.iscsi_pf_params.num_sq_pages_in_ring = num_sq_pages; |
| 826 | qedi->pf_params.iscsi_pf_params.num_r2tq_pages_in_ring = num_sq_pages; |
| 827 | qedi->pf_params.iscsi_pf_params.num_uhq_pages_in_ring = num_sq_pages; |
| 828 | qedi->pf_params.iscsi_pf_params.num_queues = qedi->num_queues; |
| 829 | qedi->pf_params.iscsi_pf_params.debug_mode = qedi_fw_debug; |
| 830 | |
| 831 | for (log_page_size = 0 ; log_page_size < 32 ; log_page_size++) { |
| 832 | if ((1 << log_page_size) == PAGE_SIZE) |
| 833 | break; |
| 834 | } |
| 835 | qedi->pf_params.iscsi_pf_params.log_page_size = log_page_size; |
| 836 | |
| 837 | qedi->pf_params.iscsi_pf_params.glbl_q_params_addr = |
| 838 | (u64)qedi->hw_p_cpuq; |
| 839 | |
| 840 | /* RQ BDQ initializations. |
| 841 | * rq_num_entries: suggested value for Initiator is 16 (4KB RQ) |
| 842 | * rqe_log_size: 8 for 256B RQE |
| 843 | */ |
| 844 | qedi->pf_params.iscsi_pf_params.rqe_log_size = 8; |
| 845 | /* BDQ address and size */ |
| 846 | qedi->pf_params.iscsi_pf_params.bdq_pbl_base_addr[BDQ_ID_RQ] = |
| 847 | qedi->bdq_pbl_list_dma; |
| 848 | qedi->pf_params.iscsi_pf_params.bdq_pbl_num_entries[BDQ_ID_RQ] = |
| 849 | qedi->bdq_pbl_list_num_entries; |
| 850 | qedi->pf_params.iscsi_pf_params.rq_buffer_size = QEDI_BDQ_BUF_SIZE; |
| 851 | |
| 852 | /* cq_num_entries: num_tasks + rq_num_entries */ |
| 853 | qedi->pf_params.iscsi_pf_params.cq_num_entries = 2048; |
| 854 | |
| 855 | qedi->pf_params.iscsi_pf_params.gl_rq_pi = QEDI_PROTO_CQ_PROD_IDX; |
| 856 | qedi->pf_params.iscsi_pf_params.gl_cmd_pi = 1; |
| 857 | qedi->pf_params.iscsi_pf_params.ooo_enable = 1; |
| 858 | |
| 859 | err_alloc_mem: |
| 860 | return rval; |
| 861 | } |
| 862 | |
| 863 | /* Free DMA coherent memory for array of queue pointers we pass to qed */ |
| 864 | static void qedi_free_iscsi_pf_param(struct qedi_ctx *qedi) |
| 865 | { |
| 866 | size_t size = 0; |
| 867 | |
| 868 | if (qedi->p_cpuq) { |
| 869 | size = qedi->num_queues * sizeof(struct qedi_glbl_q_params); |
| 870 | pci_free_consistent(qedi->pdev, size, qedi->p_cpuq, |
| 871 | qedi->hw_p_cpuq); |
| 872 | } |
| 873 | |
| 874 | qedi_free_global_queues(qedi); |
| 875 | |
| 876 | kfree(qedi->global_queues); |
| 877 | } |
| 878 | |
| 879 | static void qedi_link_update(void *dev, struct qed_link_output *link) |
| 880 | { |
| 881 | struct qedi_ctx *qedi = (struct qedi_ctx *)dev; |
| 882 | |
| 883 | if (link->link_up) { |
| 884 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "Link Up event.\n"); |
| 885 | atomic_set(&qedi->link_state, QEDI_LINK_UP); |
| 886 | } else { |
| 887 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, |
| 888 | "Link Down event.\n"); |
| 889 | atomic_set(&qedi->link_state, QEDI_LINK_DOWN); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | static struct qed_iscsi_cb_ops qedi_cb_ops = { |
| 894 | { |
| 895 | .link_update = qedi_link_update, |
| 896 | } |
| 897 | }; |
| 898 | |
| 899 | static int qedi_queue_cqe(struct qedi_ctx *qedi, union iscsi_cqe *cqe, |
| 900 | u16 que_idx, struct qedi_percpu_s *p) |
| 901 | { |
| 902 | struct qedi_work *qedi_work; |
| 903 | struct qedi_conn *q_conn; |
| 904 | struct iscsi_conn *conn; |
| 905 | struct qedi_cmd *qedi_cmd; |
| 906 | u32 iscsi_cid; |
| 907 | int rc = 0; |
| 908 | |
| 909 | iscsi_cid = cqe->cqe_common.conn_id; |
| 910 | q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid]; |
| 911 | if (!q_conn) { |
| 912 | QEDI_WARN(&qedi->dbg_ctx, |
| 913 | "Session no longer exists for cid=0x%x!!\n", |
| 914 | iscsi_cid); |
| 915 | return -1; |
| 916 | } |
| 917 | conn = q_conn->cls_conn->dd_data; |
| 918 | |
| 919 | switch (cqe->cqe_common.cqe_type) { |
| 920 | case ISCSI_CQE_TYPE_SOLICITED: |
| 921 | case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE: |
| 922 | qedi_cmd = qedi_get_cmd_from_tid(qedi, cqe->cqe_solicited.itid); |
| 923 | if (!qedi_cmd) { |
| 924 | rc = -1; |
| 925 | break; |
| 926 | } |
| 927 | INIT_LIST_HEAD(&qedi_cmd->cqe_work.list); |
| 928 | qedi_cmd->cqe_work.qedi = qedi; |
| 929 | memcpy(&qedi_cmd->cqe_work.cqe, cqe, sizeof(union iscsi_cqe)); |
| 930 | qedi_cmd->cqe_work.que_idx = que_idx; |
| 931 | qedi_cmd->cqe_work.is_solicited = true; |
| 932 | list_add_tail(&qedi_cmd->cqe_work.list, &p->work_list); |
| 933 | break; |
| 934 | case ISCSI_CQE_TYPE_UNSOLICITED: |
| 935 | case ISCSI_CQE_TYPE_DUMMY: |
| 936 | case ISCSI_CQE_TYPE_TASK_CLEANUP: |
| 937 | qedi_work = kzalloc(sizeof(*qedi_work), GFP_ATOMIC); |
| 938 | if (!qedi_work) { |
| 939 | rc = -1; |
| 940 | break; |
| 941 | } |
| 942 | INIT_LIST_HEAD(&qedi_work->list); |
| 943 | qedi_work->qedi = qedi; |
| 944 | memcpy(&qedi_work->cqe, cqe, sizeof(union iscsi_cqe)); |
| 945 | qedi_work->que_idx = que_idx; |
| 946 | qedi_work->is_solicited = false; |
| 947 | list_add_tail(&qedi_work->list, &p->work_list); |
| 948 | break; |
| 949 | default: |
| 950 | rc = -1; |
| 951 | QEDI_ERR(&qedi->dbg_ctx, "FW Error cqe.\n"); |
| 952 | } |
| 953 | return rc; |
| 954 | } |
| 955 | |
| 956 | static bool qedi_process_completions(struct qedi_fastpath *fp) |
| 957 | { |
| 958 | struct qedi_ctx *qedi = fp->qedi; |
| 959 | struct qed_sb_info *sb_info = fp->sb_info; |
| 960 | struct status_block *sb = sb_info->sb_virt; |
| 961 | struct qedi_percpu_s *p = NULL; |
| 962 | struct global_queue *que; |
| 963 | u16 prod_idx; |
| 964 | unsigned long flags; |
| 965 | union iscsi_cqe *cqe; |
| 966 | int cpu; |
| 967 | int ret; |
| 968 | |
| 969 | /* Get the current firmware producer index */ |
| 970 | prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX]; |
| 971 | |
| 972 | if (prod_idx >= QEDI_CQ_SIZE) |
| 973 | prod_idx = prod_idx % QEDI_CQ_SIZE; |
| 974 | |
| 975 | que = qedi->global_queues[fp->sb_id]; |
| 976 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO, |
| 977 | "Before: global queue=%p prod_idx=%d cons_idx=%d, sb_id=%d\n", |
| 978 | que, prod_idx, que->cq_cons_idx, fp->sb_id); |
| 979 | |
| 980 | qedi->intr_cpu = fp->sb_id; |
| 981 | cpu = smp_processor_id(); |
| 982 | p = &per_cpu(qedi_percpu, cpu); |
| 983 | |
| 984 | if (unlikely(!p->iothread)) |
| 985 | WARN_ON(1); |
| 986 | |
| 987 | spin_lock_irqsave(&p->p_work_lock, flags); |
| 988 | while (que->cq_cons_idx != prod_idx) { |
| 989 | cqe = &que->cq[que->cq_cons_idx]; |
| 990 | |
| 991 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO, |
| 992 | "cqe=%p prod_idx=%d cons_idx=%d.\n", |
| 993 | cqe, prod_idx, que->cq_cons_idx); |
| 994 | |
| 995 | ret = qedi_queue_cqe(qedi, cqe, fp->sb_id, p); |
| 996 | if (ret) |
| 997 | continue; |
| 998 | |
| 999 | que->cq_cons_idx++; |
| 1000 | if (que->cq_cons_idx == QEDI_CQ_SIZE) |
| 1001 | que->cq_cons_idx = 0; |
| 1002 | } |
| 1003 | wake_up_process(p->iothread); |
| 1004 | spin_unlock_irqrestore(&p->p_work_lock, flags); |
| 1005 | |
| 1006 | return true; |
| 1007 | } |
| 1008 | |
| 1009 | static bool qedi_fp_has_work(struct qedi_fastpath *fp) |
| 1010 | { |
| 1011 | struct qedi_ctx *qedi = fp->qedi; |
| 1012 | struct global_queue *que; |
| 1013 | struct qed_sb_info *sb_info = fp->sb_info; |
| 1014 | struct status_block *sb = sb_info->sb_virt; |
| 1015 | u16 prod_idx; |
| 1016 | |
| 1017 | barrier(); |
| 1018 | |
| 1019 | /* Get the current firmware producer index */ |
| 1020 | prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX]; |
| 1021 | |
| 1022 | /* Get the pointer to the global CQ this completion is on */ |
| 1023 | que = qedi->global_queues[fp->sb_id]; |
| 1024 | |
| 1025 | /* prod idx wrap around uint16 */ |
| 1026 | if (prod_idx >= QEDI_CQ_SIZE) |
| 1027 | prod_idx = prod_idx % QEDI_CQ_SIZE; |
| 1028 | |
| 1029 | return (que->cq_cons_idx != prod_idx); |
| 1030 | } |
| 1031 | |
| 1032 | /* MSI-X fastpath handler code */ |
| 1033 | static irqreturn_t qedi_msix_handler(int irq, void *dev_id) |
| 1034 | { |
| 1035 | struct qedi_fastpath *fp = dev_id; |
| 1036 | struct qedi_ctx *qedi = fp->qedi; |
| 1037 | bool wake_io_thread = true; |
| 1038 | |
| 1039 | qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0); |
| 1040 | |
| 1041 | process_again: |
| 1042 | wake_io_thread = qedi_process_completions(fp); |
| 1043 | if (wake_io_thread) { |
| 1044 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, |
| 1045 | "process already running\n"); |
| 1046 | } |
| 1047 | |
| 1048 | if (qedi_fp_has_work(fp) == 0) |
| 1049 | qed_sb_update_sb_idx(fp->sb_info); |
| 1050 | |
| 1051 | /* Check for more work */ |
| 1052 | rmb(); |
| 1053 | |
| 1054 | if (qedi_fp_has_work(fp) == 0) |
| 1055 | qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1); |
| 1056 | else |
| 1057 | goto process_again; |
| 1058 | |
| 1059 | return IRQ_HANDLED; |
| 1060 | } |
| 1061 | |
| 1062 | /* simd handler for MSI/INTa */ |
| 1063 | static void qedi_simd_int_handler(void *cookie) |
| 1064 | { |
| 1065 | /* Cookie is qedi_ctx struct */ |
| 1066 | struct qedi_ctx *qedi = (struct qedi_ctx *)cookie; |
| 1067 | |
| 1068 | QEDI_WARN(&qedi->dbg_ctx, "qedi=%p.\n", qedi); |
| 1069 | } |
| 1070 | |
| 1071 | #define QEDI_SIMD_HANDLER_NUM 0 |
| 1072 | static void qedi_sync_free_irqs(struct qedi_ctx *qedi) |
| 1073 | { |
| 1074 | int i; |
| 1075 | |
| 1076 | if (qedi->int_info.msix_cnt) { |
| 1077 | for (i = 0; i < qedi->int_info.used_cnt; i++) { |
| 1078 | synchronize_irq(qedi->int_info.msix[i].vector); |
| 1079 | irq_set_affinity_hint(qedi->int_info.msix[i].vector, |
| 1080 | NULL); |
| 1081 | free_irq(qedi->int_info.msix[i].vector, |
| 1082 | &qedi->fp_array[i]); |
| 1083 | } |
| 1084 | } else { |
| 1085 | qedi_ops->common->simd_handler_clean(qedi->cdev, |
| 1086 | QEDI_SIMD_HANDLER_NUM); |
| 1087 | } |
| 1088 | |
| 1089 | qedi->int_info.used_cnt = 0; |
| 1090 | qedi_ops->common->set_fp_int(qedi->cdev, 0); |
| 1091 | } |
| 1092 | |
| 1093 | static int qedi_request_msix_irq(struct qedi_ctx *qedi) |
| 1094 | { |
| 1095 | int i, rc, cpu; |
| 1096 | |
| 1097 | cpu = cpumask_first(cpu_online_mask); |
| 1098 | for (i = 0; i < MIN_NUM_CPUS_MSIX(qedi); i++) { |
| 1099 | rc = request_irq(qedi->int_info.msix[i].vector, |
| 1100 | qedi_msix_handler, 0, "qedi", |
| 1101 | &qedi->fp_array[i]); |
| 1102 | |
| 1103 | if (rc) { |
| 1104 | QEDI_WARN(&qedi->dbg_ctx, "request_irq failed.\n"); |
| 1105 | qedi_sync_free_irqs(qedi); |
| 1106 | return rc; |
| 1107 | } |
| 1108 | qedi->int_info.used_cnt++; |
| 1109 | rc = irq_set_affinity_hint(qedi->int_info.msix[i].vector, |
| 1110 | get_cpu_mask(cpu)); |
| 1111 | cpu = cpumask_next(cpu, cpu_online_mask); |
| 1112 | } |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | static int qedi_setup_int(struct qedi_ctx *qedi) |
| 1118 | { |
| 1119 | int rc = 0; |
| 1120 | |
| 1121 | rc = qedi_ops->common->set_fp_int(qedi->cdev, num_online_cpus()); |
| 1122 | rc = qedi_ops->common->get_fp_int(qedi->cdev, &qedi->int_info); |
| 1123 | if (rc) |
| 1124 | goto exit_setup_int; |
| 1125 | |
| 1126 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, |
| 1127 | "Number of msix_cnt = 0x%x num of cpus = 0x%x\n", |
| 1128 | qedi->int_info.msix_cnt, num_online_cpus()); |
| 1129 | |
| 1130 | if (qedi->int_info.msix_cnt) { |
| 1131 | rc = qedi_request_msix_irq(qedi); |
| 1132 | goto exit_setup_int; |
| 1133 | } else { |
| 1134 | qedi_ops->common->simd_handler_config(qedi->cdev, &qedi, |
| 1135 | QEDI_SIMD_HANDLER_NUM, |
| 1136 | qedi_simd_int_handler); |
| 1137 | qedi->int_info.used_cnt = 1; |
| 1138 | } |
| 1139 | |
| 1140 | exit_setup_int: |
| 1141 | return rc; |
| 1142 | } |
| 1143 | |
| 1144 | static void qedi_free_bdq(struct qedi_ctx *qedi) |
| 1145 | { |
| 1146 | int i; |
| 1147 | |
| 1148 | if (qedi->bdq_pbl_list) |
| 1149 | dma_free_coherent(&qedi->pdev->dev, PAGE_SIZE, |
| 1150 | qedi->bdq_pbl_list, qedi->bdq_pbl_list_dma); |
| 1151 | |
| 1152 | if (qedi->bdq_pbl) |
| 1153 | dma_free_coherent(&qedi->pdev->dev, qedi->bdq_pbl_mem_size, |
| 1154 | qedi->bdq_pbl, qedi->bdq_pbl_dma); |
| 1155 | |
| 1156 | for (i = 0; i < QEDI_BDQ_NUM; i++) { |
| 1157 | if (qedi->bdq[i].buf_addr) { |
| 1158 | dma_free_coherent(&qedi->pdev->dev, QEDI_BDQ_BUF_SIZE, |
| 1159 | qedi->bdq[i].buf_addr, |
| 1160 | qedi->bdq[i].buf_dma); |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | static void qedi_free_global_queues(struct qedi_ctx *qedi) |
| 1166 | { |
| 1167 | int i; |
| 1168 | struct global_queue **gl = qedi->global_queues; |
| 1169 | |
| 1170 | for (i = 0; i < qedi->num_queues; i++) { |
| 1171 | if (!gl[i]) |
| 1172 | continue; |
| 1173 | |
| 1174 | if (gl[i]->cq) |
| 1175 | dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_mem_size, |
| 1176 | gl[i]->cq, gl[i]->cq_dma); |
| 1177 | if (gl[i]->cq_pbl) |
| 1178 | dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_pbl_size, |
| 1179 | gl[i]->cq_pbl, gl[i]->cq_pbl_dma); |
| 1180 | |
| 1181 | kfree(gl[i]); |
| 1182 | } |
| 1183 | qedi_free_bdq(qedi); |
| 1184 | } |
| 1185 | |
| 1186 | static int qedi_alloc_bdq(struct qedi_ctx *qedi) |
| 1187 | { |
| 1188 | int i; |
| 1189 | struct scsi_bd *pbl; |
| 1190 | u64 *list; |
| 1191 | dma_addr_t page; |
| 1192 | |
| 1193 | /* Alloc dma memory for BDQ buffers */ |
| 1194 | for (i = 0; i < QEDI_BDQ_NUM; i++) { |
| 1195 | qedi->bdq[i].buf_addr = |
| 1196 | dma_alloc_coherent(&qedi->pdev->dev, |
| 1197 | QEDI_BDQ_BUF_SIZE, |
| 1198 | &qedi->bdq[i].buf_dma, |
| 1199 | GFP_KERNEL); |
| 1200 | if (!qedi->bdq[i].buf_addr) { |
| 1201 | QEDI_ERR(&qedi->dbg_ctx, |
| 1202 | "Could not allocate BDQ buffer %d.\n", i); |
| 1203 | return -ENOMEM; |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | /* Alloc dma memory for BDQ page buffer list */ |
| 1208 | qedi->bdq_pbl_mem_size = QEDI_BDQ_NUM * sizeof(struct scsi_bd); |
| 1209 | qedi->bdq_pbl_mem_size = ALIGN(qedi->bdq_pbl_mem_size, PAGE_SIZE); |
| 1210 | qedi->rq_num_entries = qedi->bdq_pbl_mem_size / sizeof(struct scsi_bd); |
| 1211 | |
| 1212 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, "rq_num_entries = %d.\n", |
| 1213 | qedi->rq_num_entries); |
| 1214 | |
| 1215 | qedi->bdq_pbl = dma_alloc_coherent(&qedi->pdev->dev, |
| 1216 | qedi->bdq_pbl_mem_size, |
| 1217 | &qedi->bdq_pbl_dma, GFP_KERNEL); |
| 1218 | if (!qedi->bdq_pbl) { |
| 1219 | QEDI_ERR(&qedi->dbg_ctx, "Could not allocate BDQ PBL.\n"); |
| 1220 | return -ENOMEM; |
| 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | * Populate BDQ PBL with physical and virtual address of individual |
| 1225 | * BDQ buffers |
| 1226 | */ |
| 1227 | pbl = (struct scsi_bd *)qedi->bdq_pbl; |
| 1228 | for (i = 0; i < QEDI_BDQ_NUM; i++) { |
| 1229 | pbl->address.hi = |
| 1230 | cpu_to_le32(QEDI_U64_HI(qedi->bdq[i].buf_dma)); |
| 1231 | pbl->address.lo = |
| 1232 | cpu_to_le32(QEDI_U64_LO(qedi->bdq[i].buf_dma)); |
| 1233 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, |
| 1234 | "pbl [0x%p] pbl->address hi [0x%llx] lo [0x%llx], idx [%d]\n", |
| 1235 | pbl, pbl->address.hi, pbl->address.lo, i); |
| 1236 | pbl->opaque.hi = 0; |
| 1237 | pbl->opaque.lo = cpu_to_le32(QEDI_U64_LO(i)); |
| 1238 | pbl++; |
| 1239 | } |
| 1240 | |
| 1241 | /* Allocate list of PBL pages */ |
| 1242 | qedi->bdq_pbl_list = dma_alloc_coherent(&qedi->pdev->dev, |
| 1243 | PAGE_SIZE, |
| 1244 | &qedi->bdq_pbl_list_dma, |
| 1245 | GFP_KERNEL); |
| 1246 | if (!qedi->bdq_pbl_list) { |
| 1247 | QEDI_ERR(&qedi->dbg_ctx, |
| 1248 | "Could not allocate list of PBL pages.\n"); |
| 1249 | return -ENOMEM; |
| 1250 | } |
| 1251 | memset(qedi->bdq_pbl_list, 0, PAGE_SIZE); |
| 1252 | |
| 1253 | /* |
| 1254 | * Now populate PBL list with pages that contain pointers to the |
| 1255 | * individual buffers. |
| 1256 | */ |
| 1257 | qedi->bdq_pbl_list_num_entries = qedi->bdq_pbl_mem_size / PAGE_SIZE; |
| 1258 | list = (u64 *)qedi->bdq_pbl_list; |
| 1259 | page = qedi->bdq_pbl_list_dma; |
| 1260 | for (i = 0; i < qedi->bdq_pbl_list_num_entries; i++) { |
| 1261 | *list = qedi->bdq_pbl_dma; |
| 1262 | list++; |
| 1263 | page += PAGE_SIZE; |
| 1264 | } |
| 1265 | |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | static int qedi_alloc_global_queues(struct qedi_ctx *qedi) |
| 1270 | { |
| 1271 | u32 *list; |
| 1272 | int i; |
| 1273 | int status = 0, rc; |
| 1274 | u32 *pbl; |
| 1275 | dma_addr_t page; |
| 1276 | int num_pages; |
| 1277 | |
| 1278 | /* |
| 1279 | * Number of global queues (CQ / RQ). This should |
| 1280 | * be <= number of available MSIX vectors for the PF |
| 1281 | */ |
| 1282 | if (!qedi->num_queues) { |
| 1283 | QEDI_ERR(&qedi->dbg_ctx, "No MSI-X vectors available!\n"); |
| 1284 | return 1; |
| 1285 | } |
| 1286 | |
| 1287 | /* Make sure we allocated the PBL that will contain the physical |
| 1288 | * addresses of our queues |
| 1289 | */ |
| 1290 | if (!qedi->p_cpuq) { |
| 1291 | status = 1; |
| 1292 | goto mem_alloc_failure; |
| 1293 | } |
| 1294 | |
| 1295 | qedi->global_queues = kzalloc((sizeof(struct global_queue *) * |
| 1296 | qedi->num_queues), GFP_KERNEL); |
| 1297 | if (!qedi->global_queues) { |
| 1298 | QEDI_ERR(&qedi->dbg_ctx, |
| 1299 | "Unable to allocate global queues array ptr memory\n"); |
| 1300 | return -ENOMEM; |
| 1301 | } |
| 1302 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, |
| 1303 | "qedi->global_queues=%p.\n", qedi->global_queues); |
| 1304 | |
| 1305 | /* Allocate DMA coherent buffers for BDQ */ |
| 1306 | rc = qedi_alloc_bdq(qedi); |
| 1307 | if (rc) |
| 1308 | goto mem_alloc_failure; |
| 1309 | |
| 1310 | /* Allocate a CQ and an associated PBL for each MSI-X |
| 1311 | * vector. |
| 1312 | */ |
| 1313 | for (i = 0; i < qedi->num_queues; i++) { |
| 1314 | qedi->global_queues[i] = |
| 1315 | kzalloc(sizeof(*qedi->global_queues[0]), |
| 1316 | GFP_KERNEL); |
| 1317 | if (!qedi->global_queues[i]) { |
| 1318 | QEDI_ERR(&qedi->dbg_ctx, |
| 1319 | "Unable to allocation global queue %d.\n", i); |
| 1320 | goto mem_alloc_failure; |
| 1321 | } |
| 1322 | |
| 1323 | qedi->global_queues[i]->cq_mem_size = |
| 1324 | (QEDI_CQ_SIZE + 8) * sizeof(union iscsi_cqe); |
| 1325 | qedi->global_queues[i]->cq_mem_size = |
| 1326 | (qedi->global_queues[i]->cq_mem_size + |
| 1327 | (QEDI_PAGE_SIZE - 1)); |
| 1328 | |
| 1329 | qedi->global_queues[i]->cq_pbl_size = |
| 1330 | (qedi->global_queues[i]->cq_mem_size / |
| 1331 | QEDI_PAGE_SIZE) * sizeof(void *); |
| 1332 | qedi->global_queues[i]->cq_pbl_size = |
| 1333 | (qedi->global_queues[i]->cq_pbl_size + |
| 1334 | (QEDI_PAGE_SIZE - 1)); |
| 1335 | |
| 1336 | qedi->global_queues[i]->cq = |
| 1337 | dma_alloc_coherent(&qedi->pdev->dev, |
| 1338 | qedi->global_queues[i]->cq_mem_size, |
| 1339 | &qedi->global_queues[i]->cq_dma, |
| 1340 | GFP_KERNEL); |
| 1341 | |
| 1342 | if (!qedi->global_queues[i]->cq) { |
| 1343 | QEDI_WARN(&qedi->dbg_ctx, |
| 1344 | "Could not allocate cq.\n"); |
| 1345 | status = -ENOMEM; |
| 1346 | goto mem_alloc_failure; |
| 1347 | } |
| 1348 | memset(qedi->global_queues[i]->cq, 0, |
| 1349 | qedi->global_queues[i]->cq_mem_size); |
| 1350 | |
| 1351 | qedi->global_queues[i]->cq_pbl = |
| 1352 | dma_alloc_coherent(&qedi->pdev->dev, |
| 1353 | qedi->global_queues[i]->cq_pbl_size, |
| 1354 | &qedi->global_queues[i]->cq_pbl_dma, |
| 1355 | GFP_KERNEL); |
| 1356 | |
| 1357 | if (!qedi->global_queues[i]->cq_pbl) { |
| 1358 | QEDI_WARN(&qedi->dbg_ctx, |
| 1359 | "Could not allocate cq PBL.\n"); |
| 1360 | status = -ENOMEM; |
| 1361 | goto mem_alloc_failure; |
| 1362 | } |
| 1363 | memset(qedi->global_queues[i]->cq_pbl, 0, |
| 1364 | qedi->global_queues[i]->cq_pbl_size); |
| 1365 | |
| 1366 | /* Create PBL */ |
| 1367 | num_pages = qedi->global_queues[i]->cq_mem_size / |
| 1368 | QEDI_PAGE_SIZE; |
| 1369 | page = qedi->global_queues[i]->cq_dma; |
| 1370 | pbl = (u32 *)qedi->global_queues[i]->cq_pbl; |
| 1371 | |
| 1372 | while (num_pages--) { |
| 1373 | *pbl = (u32)page; |
| 1374 | pbl++; |
| 1375 | *pbl = (u32)((u64)page >> 32); |
| 1376 | pbl++; |
| 1377 | page += QEDI_PAGE_SIZE; |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | list = (u32 *)qedi->p_cpuq; |
| 1382 | |
| 1383 | /* |
| 1384 | * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer, |
| 1385 | * CQ#1 PBL pointer, RQ#1 PBL pointer, etc. Each PBL pointer points |
| 1386 | * to the physical address which contains an array of pointers to the |
| 1387 | * physical addresses of the specific queue pages. |
| 1388 | */ |
| 1389 | for (i = 0; i < qedi->num_queues; i++) { |
| 1390 | *list = (u32)qedi->global_queues[i]->cq_pbl_dma; |
| 1391 | list++; |
| 1392 | *list = (u32)((u64)qedi->global_queues[i]->cq_pbl_dma >> 32); |
| 1393 | list++; |
| 1394 | |
| 1395 | *list = (u32)0; |
| 1396 | list++; |
| 1397 | *list = (u32)((u64)0 >> 32); |
| 1398 | list++; |
| 1399 | } |
| 1400 | |
| 1401 | return 0; |
| 1402 | |
| 1403 | mem_alloc_failure: |
| 1404 | qedi_free_global_queues(qedi); |
| 1405 | return status; |
| 1406 | } |
| 1407 | |
| 1408 | int qedi_alloc_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep) |
| 1409 | { |
| 1410 | int rval = 0; |
| 1411 | u32 *pbl; |
| 1412 | dma_addr_t page; |
| 1413 | int num_pages; |
| 1414 | |
| 1415 | if (!ep) |
| 1416 | return -EIO; |
| 1417 | |
| 1418 | /* Calculate appropriate queue and PBL sizes */ |
| 1419 | ep->sq_mem_size = QEDI_SQ_SIZE * sizeof(struct iscsi_wqe); |
| 1420 | ep->sq_mem_size += QEDI_PAGE_SIZE - 1; |
| 1421 | |
| 1422 | ep->sq_pbl_size = (ep->sq_mem_size / QEDI_PAGE_SIZE) * sizeof(void *); |
| 1423 | ep->sq_pbl_size = ep->sq_pbl_size + QEDI_PAGE_SIZE; |
| 1424 | |
| 1425 | ep->sq = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_mem_size, |
| 1426 | &ep->sq_dma, GFP_KERNEL); |
| 1427 | if (!ep->sq) { |
| 1428 | QEDI_WARN(&qedi->dbg_ctx, |
| 1429 | "Could not allocate send queue.\n"); |
| 1430 | rval = -ENOMEM; |
| 1431 | goto out; |
| 1432 | } |
| 1433 | memset(ep->sq, 0, ep->sq_mem_size); |
| 1434 | |
| 1435 | ep->sq_pbl = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_pbl_size, |
| 1436 | &ep->sq_pbl_dma, GFP_KERNEL); |
| 1437 | if (!ep->sq_pbl) { |
| 1438 | QEDI_WARN(&qedi->dbg_ctx, |
| 1439 | "Could not allocate send queue PBL.\n"); |
| 1440 | rval = -ENOMEM; |
| 1441 | goto out_free_sq; |
| 1442 | } |
| 1443 | memset(ep->sq_pbl, 0, ep->sq_pbl_size); |
| 1444 | |
| 1445 | /* Create PBL */ |
| 1446 | num_pages = ep->sq_mem_size / QEDI_PAGE_SIZE; |
| 1447 | page = ep->sq_dma; |
| 1448 | pbl = (u32 *)ep->sq_pbl; |
| 1449 | |
| 1450 | while (num_pages--) { |
| 1451 | *pbl = (u32)page; |
| 1452 | pbl++; |
| 1453 | *pbl = (u32)((u64)page >> 32); |
| 1454 | pbl++; |
| 1455 | page += QEDI_PAGE_SIZE; |
| 1456 | } |
| 1457 | |
| 1458 | return rval; |
| 1459 | |
| 1460 | out_free_sq: |
| 1461 | dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq, |
| 1462 | ep->sq_dma); |
| 1463 | out: |
| 1464 | return rval; |
| 1465 | } |
| 1466 | |
| 1467 | void qedi_free_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep) |
| 1468 | { |
| 1469 | if (ep->sq_pbl) |
| 1470 | dma_free_coherent(&qedi->pdev->dev, ep->sq_pbl_size, ep->sq_pbl, |
| 1471 | ep->sq_pbl_dma); |
| 1472 | if (ep->sq) |
| 1473 | dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq, |
| 1474 | ep->sq_dma); |
| 1475 | } |
| 1476 | |
| 1477 | int qedi_get_task_idx(struct qedi_ctx *qedi) |
| 1478 | { |
| 1479 | s16 tmp_idx; |
| 1480 | |
| 1481 | again: |
| 1482 | tmp_idx = find_first_zero_bit(qedi->task_idx_map, |
| 1483 | MAX_ISCSI_TASK_ENTRIES); |
| 1484 | |
| 1485 | if (tmp_idx >= MAX_ISCSI_TASK_ENTRIES) { |
| 1486 | QEDI_ERR(&qedi->dbg_ctx, "FW task context pool is full.\n"); |
| 1487 | tmp_idx = -1; |
| 1488 | goto err_idx; |
| 1489 | } |
| 1490 | |
| 1491 | if (test_and_set_bit(tmp_idx, qedi->task_idx_map)) |
| 1492 | goto again; |
| 1493 | |
| 1494 | err_idx: |
| 1495 | return tmp_idx; |
| 1496 | } |
| 1497 | |
| 1498 | void qedi_clear_task_idx(struct qedi_ctx *qedi, int idx) |
| 1499 | { |
| 1500 | if (!test_and_clear_bit(idx, qedi->task_idx_map)) { |
| 1501 | QEDI_ERR(&qedi->dbg_ctx, |
| 1502 | "FW task context, already cleared, tid=0x%x\n", idx); |
| 1503 | WARN_ON(1); |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | void qedi_update_itt_map(struct qedi_ctx *qedi, u32 tid, u32 proto_itt, |
| 1508 | struct qedi_cmd *cmd) |
| 1509 | { |
| 1510 | qedi->itt_map[tid].itt = proto_itt; |
| 1511 | qedi->itt_map[tid].p_cmd = cmd; |
| 1512 | |
| 1513 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, |
| 1514 | "update itt map tid=0x%x, with proto itt=0x%x\n", tid, |
| 1515 | qedi->itt_map[tid].itt); |
| 1516 | } |
| 1517 | |
| 1518 | void qedi_get_task_tid(struct qedi_ctx *qedi, u32 itt, s16 *tid) |
| 1519 | { |
| 1520 | u16 i; |
| 1521 | |
| 1522 | for (i = 0; i < MAX_ISCSI_TASK_ENTRIES; i++) { |
| 1523 | if (qedi->itt_map[i].itt == itt) { |
| 1524 | *tid = i; |
| 1525 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, |
| 1526 | "Ref itt=0x%x, found at tid=0x%x\n", |
| 1527 | itt, *tid); |
| 1528 | return; |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | WARN_ON(1); |
| 1533 | } |
| 1534 | |
| 1535 | void qedi_get_proto_itt(struct qedi_ctx *qedi, u32 tid, u32 *proto_itt) |
| 1536 | { |
| 1537 | *proto_itt = qedi->itt_map[tid].itt; |
| 1538 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, |
| 1539 | "Get itt map tid [0x%x with proto itt[0x%x]", |
| 1540 | tid, *proto_itt); |
| 1541 | } |
| 1542 | |
| 1543 | struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid) |
| 1544 | { |
| 1545 | struct qedi_cmd *cmd = NULL; |
| 1546 | |
| 1547 | if (tid > MAX_ISCSI_TASK_ENTRIES) |
| 1548 | return NULL; |
| 1549 | |
| 1550 | cmd = qedi->itt_map[tid].p_cmd; |
| 1551 | if (cmd->task_id != tid) |
| 1552 | return NULL; |
| 1553 | |
| 1554 | qedi->itt_map[tid].p_cmd = NULL; |
| 1555 | |
| 1556 | return cmd; |
| 1557 | } |
| 1558 | |
| 1559 | static int qedi_alloc_itt(struct qedi_ctx *qedi) |
| 1560 | { |
| 1561 | qedi->itt_map = kcalloc(MAX_ISCSI_TASK_ENTRIES, |
| 1562 | sizeof(struct qedi_itt_map), GFP_KERNEL); |
| 1563 | if (!qedi->itt_map) { |
| 1564 | QEDI_ERR(&qedi->dbg_ctx, |
| 1565 | "Unable to allocate itt map array memory\n"); |
| 1566 | return -ENOMEM; |
| 1567 | } |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
| 1571 | static void qedi_free_itt(struct qedi_ctx *qedi) |
| 1572 | { |
| 1573 | kfree(qedi->itt_map); |
| 1574 | } |
| 1575 | |
| 1576 | static struct qed_ll2_cb_ops qedi_ll2_cb_ops = { |
| 1577 | .rx_cb = qedi_ll2_rx, |
| 1578 | .tx_cb = NULL, |
| 1579 | }; |
| 1580 | |
| 1581 | static int qedi_percpu_io_thread(void *arg) |
| 1582 | { |
| 1583 | struct qedi_percpu_s *p = arg; |
| 1584 | struct qedi_work *work, *tmp; |
| 1585 | unsigned long flags; |
| 1586 | LIST_HEAD(work_list); |
| 1587 | |
| 1588 | set_user_nice(current, -20); |
| 1589 | |
| 1590 | while (!kthread_should_stop()) { |
| 1591 | spin_lock_irqsave(&p->p_work_lock, flags); |
| 1592 | while (!list_empty(&p->work_list)) { |
| 1593 | list_splice_init(&p->work_list, &work_list); |
| 1594 | spin_unlock_irqrestore(&p->p_work_lock, flags); |
| 1595 | |
| 1596 | list_for_each_entry_safe(work, tmp, &work_list, list) { |
| 1597 | list_del_init(&work->list); |
| 1598 | qedi_fp_process_cqes(work); |
| 1599 | if (!work->is_solicited) |
| 1600 | kfree(work); |
| 1601 | } |
| 1602 | cond_resched(); |
| 1603 | spin_lock_irqsave(&p->p_work_lock, flags); |
| 1604 | } |
| 1605 | set_current_state(TASK_INTERRUPTIBLE); |
| 1606 | spin_unlock_irqrestore(&p->p_work_lock, flags); |
| 1607 | schedule(); |
| 1608 | } |
| 1609 | __set_current_state(TASK_RUNNING); |
| 1610 | |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 1614 | static int qedi_cpu_online(unsigned int cpu) |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1615 | { |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 1616 | struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu); |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1617 | struct task_struct *thread; |
| 1618 | |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1619 | thread = kthread_create_on_node(qedi_percpu_io_thread, (void *)p, |
| 1620 | cpu_to_node(cpu), |
| 1621 | "qedi_thread/%d", cpu); |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 1622 | if (IS_ERR(thread)) |
| 1623 | return PTR_ERR(thread); |
| 1624 | |
| 1625 | kthread_bind(thread, cpu); |
| 1626 | p->iothread = thread; |
| 1627 | wake_up_process(thread); |
| 1628 | return 0; |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 1631 | static int qedi_cpu_offline(unsigned int cpu) |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1632 | { |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 1633 | struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu); |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1634 | struct qedi_work *work, *tmp; |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 1635 | struct task_struct *thread; |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1636 | |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1637 | spin_lock_bh(&p->p_work_lock); |
| 1638 | thread = p->iothread; |
| 1639 | p->iothread = NULL; |
| 1640 | |
| 1641 | list_for_each_entry_safe(work, tmp, &p->work_list, list) { |
| 1642 | list_del_init(&work->list); |
| 1643 | qedi_fp_process_cqes(work); |
| 1644 | if (!work->is_solicited) |
| 1645 | kfree(work); |
| 1646 | } |
| 1647 | |
| 1648 | spin_unlock_bh(&p->p_work_lock); |
| 1649 | if (thread) |
| 1650 | kthread_stop(thread); |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 1651 | return 0; |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1652 | } |
| 1653 | |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1654 | void qedi_reset_host_mtu(struct qedi_ctx *qedi, u16 mtu) |
| 1655 | { |
| 1656 | struct qed_ll2_params params; |
| 1657 | |
| 1658 | qedi_recover_all_conns(qedi); |
| 1659 | |
| 1660 | qedi_ops->ll2->stop(qedi->cdev); |
| 1661 | qedi_ll2_free_skbs(qedi); |
| 1662 | |
| 1663 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "old MTU %u, new MTU %u\n", |
| 1664 | qedi->ll2_mtu, mtu); |
| 1665 | memset(¶ms, 0, sizeof(params)); |
| 1666 | qedi->ll2_mtu = mtu; |
| 1667 | params.mtu = qedi->ll2_mtu + IPV6_HDR_LEN + TCP_HDR_LEN; |
| 1668 | params.drop_ttl0_packets = 0; |
| 1669 | params.rx_vlan_stripping = 1; |
| 1670 | ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac); |
| 1671 | qedi_ops->ll2->start(qedi->cdev, ¶ms); |
| 1672 | } |
| 1673 | |
| 1674 | static void __qedi_remove(struct pci_dev *pdev, int mode) |
| 1675 | { |
| 1676 | struct qedi_ctx *qedi = pci_get_drvdata(pdev); |
| 1677 | |
| 1678 | if (qedi->tmf_thread) { |
| 1679 | flush_workqueue(qedi->tmf_thread); |
| 1680 | destroy_workqueue(qedi->tmf_thread); |
| 1681 | qedi->tmf_thread = NULL; |
| 1682 | } |
| 1683 | |
| 1684 | if (qedi->offload_thread) { |
| 1685 | flush_workqueue(qedi->offload_thread); |
| 1686 | destroy_workqueue(qedi->offload_thread); |
| 1687 | qedi->offload_thread = NULL; |
| 1688 | } |
| 1689 | |
| 1690 | #ifdef CONFIG_DEBUG_FS |
| 1691 | qedi_dbg_host_exit(&qedi->dbg_ctx); |
| 1692 | #endif |
| 1693 | if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) |
| 1694 | qedi_ops->common->set_power_state(qedi->cdev, PCI_D0); |
| 1695 | |
| 1696 | qedi_sync_free_irqs(qedi); |
| 1697 | |
| 1698 | if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) { |
| 1699 | qedi_ops->stop(qedi->cdev); |
| 1700 | qedi_ops->ll2->stop(qedi->cdev); |
| 1701 | } |
| 1702 | |
| 1703 | if (mode == QEDI_MODE_NORMAL) |
| 1704 | qedi_free_iscsi_pf_param(qedi); |
| 1705 | |
| 1706 | if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) { |
| 1707 | qedi_ops->common->slowpath_stop(qedi->cdev); |
| 1708 | qedi_ops->common->remove(qedi->cdev); |
| 1709 | } |
| 1710 | |
| 1711 | qedi_destroy_fp(qedi); |
| 1712 | |
| 1713 | if (mode == QEDI_MODE_NORMAL) { |
| 1714 | qedi_release_cid_que(qedi); |
| 1715 | qedi_cm_free_mem(qedi); |
| 1716 | qedi_free_uio(qedi->udev); |
| 1717 | qedi_free_itt(qedi); |
| 1718 | |
| 1719 | iscsi_host_remove(qedi->shost); |
| 1720 | iscsi_host_free(qedi->shost); |
| 1721 | |
| 1722 | if (qedi->ll2_recv_thread) { |
| 1723 | kthread_stop(qedi->ll2_recv_thread); |
| 1724 | qedi->ll2_recv_thread = NULL; |
| 1725 | } |
| 1726 | qedi_ll2_free_skbs(qedi); |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | static int __qedi_probe(struct pci_dev *pdev, int mode) |
| 1731 | { |
| 1732 | struct qedi_ctx *qedi; |
| 1733 | struct qed_ll2_params params; |
| 1734 | u32 dp_module = 0; |
| 1735 | u8 dp_level = 0; |
| 1736 | bool is_vf = false; |
| 1737 | char host_buf[16]; |
| 1738 | struct qed_link_params link_params; |
| 1739 | struct qed_slowpath_params sp_params; |
| 1740 | struct qed_probe_params qed_params; |
| 1741 | void *task_start, *task_end; |
| 1742 | int rc; |
| 1743 | u16 tmp; |
| 1744 | |
| 1745 | if (mode != QEDI_MODE_RECOVERY) { |
| 1746 | qedi = qedi_host_alloc(pdev); |
| 1747 | if (!qedi) { |
| 1748 | rc = -ENOMEM; |
| 1749 | goto exit_probe; |
| 1750 | } |
| 1751 | } else { |
| 1752 | qedi = pci_get_drvdata(pdev); |
| 1753 | } |
| 1754 | |
| 1755 | memset(&qed_params, 0, sizeof(qed_params)); |
| 1756 | qed_params.protocol = QED_PROTOCOL_ISCSI; |
| 1757 | qed_params.dp_module = dp_module; |
| 1758 | qed_params.dp_level = dp_level; |
| 1759 | qed_params.is_vf = is_vf; |
| 1760 | qedi->cdev = qedi_ops->common->probe(pdev, &qed_params); |
| 1761 | if (!qedi->cdev) { |
| 1762 | rc = -ENODEV; |
| 1763 | QEDI_ERR(&qedi->dbg_ctx, "Cannot initialize hardware\n"); |
| 1764 | goto free_host; |
| 1765 | } |
| 1766 | |
| 1767 | qedi->msix_count = MAX_NUM_MSIX_PF; |
| 1768 | atomic_set(&qedi->link_state, QEDI_LINK_DOWN); |
| 1769 | |
| 1770 | if (mode != QEDI_MODE_RECOVERY) { |
| 1771 | rc = qedi_set_iscsi_pf_param(qedi); |
| 1772 | if (rc) { |
| 1773 | rc = -ENOMEM; |
| 1774 | QEDI_ERR(&qedi->dbg_ctx, |
| 1775 | "Set iSCSI pf param fail\n"); |
| 1776 | goto free_host; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params); |
| 1781 | |
| 1782 | rc = qedi_prepare_fp(qedi); |
| 1783 | if (rc) { |
| 1784 | QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath.\n"); |
| 1785 | goto free_pf_params; |
| 1786 | } |
| 1787 | |
| 1788 | /* Start the Slowpath-process */ |
| 1789 | memset(&sp_params, 0, sizeof(struct qed_slowpath_params)); |
| 1790 | sp_params.int_mode = QED_INT_MODE_MSIX; |
| 1791 | sp_params.drv_major = QEDI_DRIVER_MAJOR_VER; |
| 1792 | sp_params.drv_minor = QEDI_DRIVER_MINOR_VER; |
| 1793 | sp_params.drv_rev = QEDI_DRIVER_REV_VER; |
| 1794 | sp_params.drv_eng = QEDI_DRIVER_ENG_VER; |
| 1795 | strlcpy(sp_params.name, "qedi iSCSI", QED_DRV_VER_STR_SIZE); |
| 1796 | rc = qedi_ops->common->slowpath_start(qedi->cdev, &sp_params); |
| 1797 | if (rc) { |
| 1798 | QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath\n"); |
| 1799 | goto stop_hw; |
| 1800 | } |
| 1801 | |
| 1802 | /* update_pf_params needs to be called before and after slowpath |
| 1803 | * start |
| 1804 | */ |
| 1805 | qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params); |
| 1806 | |
Colin Ian King | d1a9ccc | 2017-02-28 11:02:48 +0000 | [diff] [blame] | 1807 | rc = qedi_setup_int(qedi); |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 1808 | if (rc) |
| 1809 | goto stop_iscsi_func; |
| 1810 | |
| 1811 | qedi_ops->common->set_power_state(qedi->cdev, PCI_D0); |
| 1812 | |
| 1813 | /* Learn information crucial for qedi to progress */ |
| 1814 | rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info); |
| 1815 | if (rc) |
| 1816 | goto stop_iscsi_func; |
| 1817 | |
| 1818 | /* Record BDQ producer doorbell addresses */ |
| 1819 | qedi->bdq_primary_prod = qedi->dev_info.primary_dbq_rq_addr; |
| 1820 | qedi->bdq_secondary_prod = qedi->dev_info.secondary_bdq_rq_addr; |
| 1821 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, |
| 1822 | "BDQ primary_prod=%p secondary_prod=%p.\n", |
| 1823 | qedi->bdq_primary_prod, |
| 1824 | qedi->bdq_secondary_prod); |
| 1825 | |
| 1826 | /* |
| 1827 | * We need to write the number of BDs in the BDQ we've preallocated so |
| 1828 | * the f/w will do a prefetch and we'll get an unsolicited CQE when a |
| 1829 | * packet arrives. |
| 1830 | */ |
| 1831 | qedi->bdq_prod_idx = QEDI_BDQ_NUM; |
| 1832 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, |
| 1833 | "Writing %d to primary and secondary BDQ doorbell registers.\n", |
| 1834 | qedi->bdq_prod_idx); |
| 1835 | writew(qedi->bdq_prod_idx, qedi->bdq_primary_prod); |
| 1836 | tmp = readw(qedi->bdq_primary_prod); |
| 1837 | writew(qedi->bdq_prod_idx, qedi->bdq_secondary_prod); |
| 1838 | tmp = readw(qedi->bdq_secondary_prod); |
| 1839 | |
| 1840 | ether_addr_copy(qedi->mac, qedi->dev_info.common.hw_mac); |
| 1841 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "MAC address is %pM.\n", |
| 1842 | qedi->mac); |
| 1843 | |
| 1844 | sprintf(host_buf, "host_%d", qedi->shost->host_no); |
| 1845 | qedi_ops->common->set_id(qedi->cdev, host_buf, QEDI_MODULE_VERSION); |
| 1846 | |
| 1847 | qedi_ops->register_ops(qedi->cdev, &qedi_cb_ops, qedi); |
| 1848 | |
| 1849 | memset(¶ms, 0, sizeof(params)); |
| 1850 | params.mtu = DEF_PATH_MTU + IPV6_HDR_LEN + TCP_HDR_LEN; |
| 1851 | qedi->ll2_mtu = DEF_PATH_MTU; |
| 1852 | params.drop_ttl0_packets = 0; |
| 1853 | params.rx_vlan_stripping = 1; |
| 1854 | ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac); |
| 1855 | |
| 1856 | if (mode != QEDI_MODE_RECOVERY) { |
| 1857 | /* set up rx path */ |
| 1858 | INIT_LIST_HEAD(&qedi->ll2_skb_list); |
| 1859 | spin_lock_init(&qedi->ll2_lock); |
| 1860 | /* start qedi context */ |
| 1861 | spin_lock_init(&qedi->hba_lock); |
| 1862 | spin_lock_init(&qedi->task_idx_lock); |
| 1863 | } |
| 1864 | qedi_ops->ll2->register_cb_ops(qedi->cdev, &qedi_ll2_cb_ops, qedi); |
| 1865 | qedi_ops->ll2->start(qedi->cdev, ¶ms); |
| 1866 | |
| 1867 | if (mode != QEDI_MODE_RECOVERY) { |
| 1868 | qedi->ll2_recv_thread = kthread_run(qedi_ll2_recv_thread, |
| 1869 | (void *)qedi, |
| 1870 | "qedi_ll2_thread"); |
| 1871 | } |
| 1872 | |
| 1873 | rc = qedi_ops->start(qedi->cdev, &qedi->tasks, |
| 1874 | qedi, qedi_iscsi_event_cb); |
| 1875 | if (rc) { |
| 1876 | rc = -ENODEV; |
| 1877 | QEDI_ERR(&qedi->dbg_ctx, "Cannot start iSCSI function\n"); |
| 1878 | goto stop_slowpath; |
| 1879 | } |
| 1880 | |
| 1881 | task_start = qedi_get_task_mem(&qedi->tasks, 0); |
| 1882 | task_end = qedi_get_task_mem(&qedi->tasks, MAX_TID_BLOCKS_ISCSI - 1); |
| 1883 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, |
| 1884 | "Task context start=%p, end=%p block_size=%u.\n", |
| 1885 | task_start, task_end, qedi->tasks.size); |
| 1886 | |
| 1887 | memset(&link_params, 0, sizeof(link_params)); |
| 1888 | link_params.link_up = true; |
| 1889 | rc = qedi_ops->common->set_link(qedi->cdev, &link_params); |
| 1890 | if (rc) { |
| 1891 | QEDI_WARN(&qedi->dbg_ctx, "Link set up failed.\n"); |
| 1892 | atomic_set(&qedi->link_state, QEDI_LINK_DOWN); |
| 1893 | } |
| 1894 | |
| 1895 | #ifdef CONFIG_DEBUG_FS |
| 1896 | qedi_dbg_host_init(&qedi->dbg_ctx, &qedi_debugfs_ops, |
| 1897 | &qedi_dbg_fops); |
| 1898 | #endif |
| 1899 | QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, |
| 1900 | "QLogic FastLinQ iSCSI Module qedi %s, FW %d.%d.%d.%d\n", |
| 1901 | QEDI_MODULE_VERSION, FW_MAJOR_VERSION, FW_MINOR_VERSION, |
| 1902 | FW_REVISION_VERSION, FW_ENGINEERING_VERSION); |
| 1903 | |
| 1904 | if (mode == QEDI_MODE_NORMAL) { |
| 1905 | if (iscsi_host_add(qedi->shost, &pdev->dev)) { |
| 1906 | QEDI_ERR(&qedi->dbg_ctx, |
| 1907 | "Could not add iscsi host\n"); |
| 1908 | rc = -ENOMEM; |
| 1909 | goto remove_host; |
| 1910 | } |
| 1911 | |
| 1912 | /* Allocate uio buffers */ |
| 1913 | rc = qedi_alloc_uio_rings(qedi); |
| 1914 | if (rc) { |
| 1915 | QEDI_ERR(&qedi->dbg_ctx, |
| 1916 | "UIO alloc ring failed err=%d\n", rc); |
| 1917 | goto remove_host; |
| 1918 | } |
| 1919 | |
| 1920 | rc = qedi_init_uio(qedi); |
| 1921 | if (rc) { |
| 1922 | QEDI_ERR(&qedi->dbg_ctx, |
| 1923 | "UIO init failed, err=%d\n", rc); |
| 1924 | goto free_uio; |
| 1925 | } |
| 1926 | |
| 1927 | /* host the array on iscsi_conn */ |
| 1928 | rc = qedi_setup_cid_que(qedi); |
| 1929 | if (rc) { |
| 1930 | QEDI_ERR(&qedi->dbg_ctx, |
| 1931 | "Could not setup cid que\n"); |
| 1932 | goto free_uio; |
| 1933 | } |
| 1934 | |
| 1935 | rc = qedi_cm_alloc_mem(qedi); |
| 1936 | if (rc) { |
| 1937 | QEDI_ERR(&qedi->dbg_ctx, |
| 1938 | "Could not alloc cm memory\n"); |
| 1939 | goto free_cid_que; |
| 1940 | } |
| 1941 | |
| 1942 | rc = qedi_alloc_itt(qedi); |
| 1943 | if (rc) { |
| 1944 | QEDI_ERR(&qedi->dbg_ctx, |
| 1945 | "Could not alloc itt memory\n"); |
| 1946 | goto free_cid_que; |
| 1947 | } |
| 1948 | |
| 1949 | sprintf(host_buf, "host_%d", qedi->shost->host_no); |
| 1950 | qedi->tmf_thread = create_singlethread_workqueue(host_buf); |
| 1951 | if (!qedi->tmf_thread) { |
| 1952 | QEDI_ERR(&qedi->dbg_ctx, |
| 1953 | "Unable to start tmf thread!\n"); |
| 1954 | rc = -ENODEV; |
| 1955 | goto free_cid_que; |
| 1956 | } |
| 1957 | |
| 1958 | sprintf(host_buf, "qedi_ofld%d", qedi->shost->host_no); |
| 1959 | qedi->offload_thread = create_workqueue(host_buf); |
| 1960 | if (!qedi->offload_thread) { |
| 1961 | QEDI_ERR(&qedi->dbg_ctx, |
| 1962 | "Unable to start offload thread!\n"); |
| 1963 | rc = -ENODEV; |
| 1964 | goto free_cid_que; |
| 1965 | } |
| 1966 | |
| 1967 | /* F/w needs 1st task context memory entry for performance */ |
| 1968 | set_bit(QEDI_RESERVE_TASK_ID, qedi->task_idx_map); |
| 1969 | atomic_set(&qedi->num_offloads, 0); |
| 1970 | } |
| 1971 | |
| 1972 | return 0; |
| 1973 | |
| 1974 | free_cid_que: |
| 1975 | qedi_release_cid_que(qedi); |
| 1976 | free_uio: |
| 1977 | qedi_free_uio(qedi->udev); |
| 1978 | remove_host: |
| 1979 | #ifdef CONFIG_DEBUG_FS |
| 1980 | qedi_dbg_host_exit(&qedi->dbg_ctx); |
| 1981 | #endif |
| 1982 | iscsi_host_remove(qedi->shost); |
| 1983 | stop_iscsi_func: |
| 1984 | qedi_ops->stop(qedi->cdev); |
| 1985 | stop_slowpath: |
| 1986 | qedi_ops->common->slowpath_stop(qedi->cdev); |
| 1987 | stop_hw: |
| 1988 | qedi_ops->common->remove(qedi->cdev); |
| 1989 | free_pf_params: |
| 1990 | qedi_free_iscsi_pf_param(qedi); |
| 1991 | free_host: |
| 1992 | iscsi_host_free(qedi->shost); |
| 1993 | exit_probe: |
| 1994 | return rc; |
| 1995 | } |
| 1996 | |
| 1997 | static int qedi_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 1998 | { |
| 1999 | return __qedi_probe(pdev, QEDI_MODE_NORMAL); |
| 2000 | } |
| 2001 | |
| 2002 | static void qedi_remove(struct pci_dev *pdev) |
| 2003 | { |
| 2004 | __qedi_remove(pdev, QEDI_MODE_NORMAL); |
| 2005 | } |
| 2006 | |
| 2007 | static struct pci_device_id qedi_pci_tbl[] = { |
| 2008 | { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165E) }, |
Manish Rangankar | 0468852 | 2017-03-15 01:32:53 -0700 | [diff] [blame] | 2009 | { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8084) }, |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2010 | { 0 }, |
| 2011 | }; |
| 2012 | MODULE_DEVICE_TABLE(pci, qedi_pci_tbl); |
| 2013 | |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 2014 | static enum cpuhp_state qedi_cpuhp_state; |
| 2015 | |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2016 | static struct pci_driver qedi_pci_driver = { |
| 2017 | .name = QEDI_MODULE_NAME, |
| 2018 | .id_table = qedi_pci_tbl, |
| 2019 | .probe = qedi_probe, |
| 2020 | .remove = qedi_remove, |
| 2021 | }; |
| 2022 | |
| 2023 | static int __init qedi_init(void) |
| 2024 | { |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2025 | struct qedi_percpu_s *p; |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 2026 | int cpu, rc = 0; |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2027 | |
| 2028 | qedi_ops = qed_get_iscsi_ops(); |
| 2029 | if (!qedi_ops) { |
| 2030 | QEDI_ERR(NULL, "Failed to get qed iSCSI operations\n"); |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 2031 | return -EINVAL; |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | #ifdef CONFIG_DEBUG_FS |
| 2035 | qedi_dbg_init("qedi"); |
| 2036 | #endif |
| 2037 | |
| 2038 | qedi_scsi_transport = iscsi_register_transport(&qedi_iscsi_transport); |
| 2039 | if (!qedi_scsi_transport) { |
| 2040 | QEDI_ERR(NULL, "Could not register qedi transport"); |
| 2041 | rc = -ENOMEM; |
| 2042 | goto exit_qedi_init_1; |
| 2043 | } |
| 2044 | |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2045 | for_each_possible_cpu(cpu) { |
| 2046 | p = &per_cpu(qedi_percpu, cpu); |
| 2047 | INIT_LIST_HEAD(&p->work_list); |
| 2048 | spin_lock_init(&p->p_work_lock); |
| 2049 | p->iothread = NULL; |
| 2050 | } |
| 2051 | |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 2052 | rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/qedi:online", |
| 2053 | qedi_cpu_online, qedi_cpu_offline); |
| 2054 | if (rc < 0) |
| 2055 | goto exit_qedi_init_2; |
| 2056 | qedi_cpuhp_state = rc; |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2057 | |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 2058 | rc = pci_register_driver(&qedi_pci_driver); |
| 2059 | if (rc) { |
| 2060 | QEDI_ERR(NULL, "Failed to register driver\n"); |
| 2061 | goto exit_qedi_hp; |
| 2062 | } |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2063 | |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 2064 | return 0; |
| 2065 | |
| 2066 | exit_qedi_hp: |
| 2067 | cpuhp_remove_state(qedi_cpuhp_state); |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2068 | exit_qedi_init_2: |
| 2069 | iscsi_unregister_transport(&qedi_iscsi_transport); |
| 2070 | exit_qedi_init_1: |
| 2071 | #ifdef CONFIG_DEBUG_FS |
| 2072 | qedi_dbg_exit(); |
| 2073 | #endif |
| 2074 | qed_put_iscsi_ops(); |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2075 | return rc; |
| 2076 | } |
| 2077 | |
| 2078 | static void __exit qedi_cleanup(void) |
| 2079 | { |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2080 | pci_unregister_driver(&qedi_pci_driver); |
Thomas Gleixner | a98d1a0 | 2016-12-24 12:34:02 +0100 | [diff] [blame] | 2081 | cpuhp_remove_state(qedi_cpuhp_state); |
Manish Rangankar | ace7f46 | 2016-12-01 00:21:08 -0800 | [diff] [blame] | 2082 | iscsi_unregister_transport(&qedi_iscsi_transport); |
| 2083 | |
| 2084 | #ifdef CONFIG_DEBUG_FS |
| 2085 | qedi_dbg_exit(); |
| 2086 | #endif |
| 2087 | qed_put_iscsi_ops(); |
| 2088 | } |
| 2089 | |
| 2090 | MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx iSCSI Module"); |
| 2091 | MODULE_LICENSE("GPL"); |
| 2092 | MODULE_AUTHOR("QLogic Corporation"); |
| 2093 | MODULE_VERSION(QEDI_MODULE_VERSION); |
| 2094 | module_init(qedi_init); |
| 2095 | module_exit(qedi_cleanup); |