linux/drivers/scsi/qedi/qedi_main.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * QLogic iSCSI Offload Driver
   4 * Copyright (c) 2016 Cavium Inc.
   5 */
   6
   7#include <linux/module.h>
   8#include <linux/pci.h>
   9#include <linux/kernel.h>
  10#include <linux/if_arp.h>
  11#include <scsi/iscsi_if.h>
  12#include <linux/inet.h>
  13#include <net/arp.h>
  14#include <linux/list.h>
  15#include <linux/kthread.h>
  16#include <linux/mm.h>
  17#include <linux/if_vlan.h>
  18#include <linux/cpu.h>
  19#include <linux/iscsi_boot_sysfs.h>
  20
  21#include <scsi/scsi_cmnd.h>
  22#include <scsi/scsi_device.h>
  23#include <scsi/scsi_eh.h>
  24#include <scsi/scsi_host.h>
  25#include <scsi/scsi.h>
  26
  27#include "qedi.h"
  28#include "qedi_gbl.h"
  29#include "qedi_iscsi.h"
  30
  31static uint qedi_qed_debug;
  32module_param(qedi_qed_debug, uint, 0644);
  33MODULE_PARM_DESC(qedi_qed_debug, " QED debug level 0 (default)");
  34
  35static uint qedi_fw_debug;
  36module_param(qedi_fw_debug, uint, 0644);
  37MODULE_PARM_DESC(qedi_fw_debug, " Firmware debug level 0(default) to 3");
  38
  39uint qedi_dbg_log = QEDI_LOG_WARN | QEDI_LOG_SCSI_TM;
  40module_param(qedi_dbg_log, uint, 0644);
  41MODULE_PARM_DESC(qedi_dbg_log, " Default debug level");
  42
  43uint qedi_io_tracing;
  44module_param(qedi_io_tracing, uint, 0644);
  45MODULE_PARM_DESC(qedi_io_tracing,
  46                 " Enable logging of SCSI requests/completions into trace buffer. (default off).");
  47
  48static uint qedi_ll2_buf_size = 0x400;
  49module_param(qedi_ll2_buf_size, uint, 0644);
  50MODULE_PARM_DESC(qedi_ll2_buf_size,
  51                 "parameter to set ping packet size, default - 0x400, Jumbo packets - 0x2400.");
  52
  53const struct qed_iscsi_ops *qedi_ops;
  54static struct scsi_transport_template *qedi_scsi_transport;
  55static struct pci_driver qedi_pci_driver;
  56static DEFINE_PER_CPU(struct qedi_percpu_s, qedi_percpu);
  57static LIST_HEAD(qedi_udev_list);
  58/* Static function declaration */
  59static int qedi_alloc_global_queues(struct qedi_ctx *qedi);
  60static void qedi_free_global_queues(struct qedi_ctx *qedi);
  61static struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid);
  62static void qedi_reset_uio_rings(struct qedi_uio_dev *udev);
  63static void qedi_ll2_free_skbs(struct qedi_ctx *qedi);
  64static struct nvm_iscsi_block *qedi_get_nvram_block(struct qedi_ctx *qedi);
  65static void qedi_recovery_handler(struct work_struct *work);
  66
  67static int qedi_iscsi_event_cb(void *context, u8 fw_event_code, void *fw_handle)
  68{
  69        struct qedi_ctx *qedi;
  70        struct qedi_endpoint *qedi_ep;
  71        struct iscsi_eqe_data *data;
  72        int rval = 0;
  73
  74        if (!context || !fw_handle) {
  75                QEDI_ERR(NULL, "Recv event with ctx NULL\n");
  76                return -EINVAL;
  77        }
  78
  79        qedi = (struct qedi_ctx *)context;
  80        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  81                  "Recv Event %d fw_handle %p\n", fw_event_code, fw_handle);
  82
  83        data = (struct iscsi_eqe_data *)fw_handle;
  84        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
  85                  "icid=0x%x conn_id=0x%x err-code=0x%x error-pdu-opcode-reserved=0x%x\n",
  86                   data->icid, data->conn_id, data->error_code,
  87                   data->error_pdu_opcode_reserved);
  88
  89        qedi_ep = qedi->ep_tbl[data->icid];
  90
  91        if (!qedi_ep) {
  92                QEDI_WARN(&qedi->dbg_ctx,
  93                          "Cannot process event, ep already disconnected, cid=0x%x\n",
  94                           data->icid);
  95                WARN_ON(1);
  96                return -ENODEV;
  97        }
  98
  99        switch (fw_event_code) {
 100        case ISCSI_EVENT_TYPE_ASYN_CONNECT_COMPLETE:
 101                if (qedi_ep->state == EP_STATE_OFLDCONN_START)
 102                        qedi_ep->state = EP_STATE_OFLDCONN_COMPL;
 103
 104                wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
 105                break;
 106        case ISCSI_EVENT_TYPE_ASYN_TERMINATE_DONE:
 107                qedi_ep->state = EP_STATE_DISCONN_COMPL;
 108                wake_up_interruptible(&qedi_ep->tcp_ofld_wait);
 109                break;
 110        case ISCSI_EVENT_TYPE_ISCSI_CONN_ERROR:
 111                qedi_process_iscsi_error(qedi_ep, data);
 112                break;
 113        case ISCSI_EVENT_TYPE_ASYN_ABORT_RCVD:
 114        case ISCSI_EVENT_TYPE_ASYN_SYN_RCVD:
 115        case ISCSI_EVENT_TYPE_ASYN_MAX_RT_TIME:
 116        case ISCSI_EVENT_TYPE_ASYN_MAX_RT_CNT:
 117        case ISCSI_EVENT_TYPE_ASYN_MAX_KA_PROBES_CNT:
 118        case ISCSI_EVENT_TYPE_ASYN_FIN_WAIT2:
 119        case ISCSI_EVENT_TYPE_TCP_CONN_ERROR:
 120                qedi_process_tcp_error(qedi_ep, data);
 121                break;
 122        default:
 123                QEDI_ERR(&qedi->dbg_ctx, "Recv Unknown Event %u\n",
 124                         fw_event_code);
 125        }
 126
 127        return rval;
 128}
 129
 130static int qedi_uio_open(struct uio_info *uinfo, struct inode *inode)
 131{
 132        struct qedi_uio_dev *udev = uinfo->priv;
 133        struct qedi_ctx *qedi = udev->qedi;
 134
 135        if (!capable(CAP_NET_ADMIN))
 136                return -EPERM;
 137
 138        if (udev->uio_dev != -1)
 139                return -EBUSY;
 140
 141        rtnl_lock();
 142        udev->uio_dev = iminor(inode);
 143        qedi_reset_uio_rings(udev);
 144        set_bit(UIO_DEV_OPENED, &qedi->flags);
 145        rtnl_unlock();
 146
 147        return 0;
 148}
 149
 150static int qedi_uio_close(struct uio_info *uinfo, struct inode *inode)
 151{
 152        struct qedi_uio_dev *udev = uinfo->priv;
 153        struct qedi_ctx *qedi = udev->qedi;
 154
 155        udev->uio_dev = -1;
 156        clear_bit(UIO_DEV_OPENED, &qedi->flags);
 157        qedi_ll2_free_skbs(qedi);
 158        return 0;
 159}
 160
 161static void __qedi_free_uio_rings(struct qedi_uio_dev *udev)
 162{
 163        if (udev->uctrl) {
 164                free_page((unsigned long)udev->uctrl);
 165                udev->uctrl = NULL;
 166        }
 167
 168        if (udev->ll2_ring) {
 169                free_page((unsigned long)udev->ll2_ring);
 170                udev->ll2_ring = NULL;
 171        }
 172
 173        if (udev->ll2_buf) {
 174                free_pages((unsigned long)udev->ll2_buf, 2);
 175                udev->ll2_buf = NULL;
 176        }
 177}
 178
 179static void __qedi_free_uio(struct qedi_uio_dev *udev)
 180{
 181        uio_unregister_device(&udev->qedi_uinfo);
 182
 183        __qedi_free_uio_rings(udev);
 184
 185        pci_dev_put(udev->pdev);
 186        kfree(udev);
 187}
 188
 189static void qedi_free_uio(struct qedi_uio_dev *udev)
 190{
 191        if (!udev)
 192                return;
 193
 194        list_del_init(&udev->list);
 195        __qedi_free_uio(udev);
 196}
 197
 198static void qedi_reset_uio_rings(struct qedi_uio_dev *udev)
 199{
 200        struct qedi_ctx *qedi = NULL;
 201        struct qedi_uio_ctrl *uctrl = NULL;
 202
 203        qedi = udev->qedi;
 204        uctrl = udev->uctrl;
 205
 206        spin_lock_bh(&qedi->ll2_lock);
 207        uctrl->host_rx_cons = 0;
 208        uctrl->hw_rx_prod = 0;
 209        uctrl->hw_rx_bd_prod = 0;
 210        uctrl->host_rx_bd_cons = 0;
 211
 212        memset(udev->ll2_ring, 0, udev->ll2_ring_size);
 213        memset(udev->ll2_buf, 0, udev->ll2_buf_size);
 214        spin_unlock_bh(&qedi->ll2_lock);
 215}
 216
 217static int __qedi_alloc_uio_rings(struct qedi_uio_dev *udev)
 218{
 219        int rc = 0;
 220
 221        if (udev->ll2_ring || udev->ll2_buf)
 222                return rc;
 223
 224        /* Memory for control area.  */
 225        udev->uctrl = (void *)get_zeroed_page(GFP_KERNEL);
 226        if (!udev->uctrl)
 227                return -ENOMEM;
 228
 229        /* Allocating memory for LL2 ring  */
 230        udev->ll2_ring_size = QEDI_PAGE_SIZE;
 231        udev->ll2_ring = (void *)get_zeroed_page(GFP_KERNEL | __GFP_COMP);
 232        if (!udev->ll2_ring) {
 233                rc = -ENOMEM;
 234                goto exit_alloc_ring;
 235        }
 236
 237        /* Allocating memory for Tx/Rx pkt buffer */
 238        udev->ll2_buf_size = TX_RX_RING * qedi_ll2_buf_size;
 239        udev->ll2_buf_size = QEDI_PAGE_ALIGN(udev->ll2_buf_size);
 240        udev->ll2_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_COMP |
 241                                                 __GFP_ZERO, 2);
 242        if (!udev->ll2_buf) {
 243                rc = -ENOMEM;
 244                goto exit_alloc_buf;
 245        }
 246        return rc;
 247
 248exit_alloc_buf:
 249        free_page((unsigned long)udev->ll2_ring);
 250        udev->ll2_ring = NULL;
 251exit_alloc_ring:
 252        return rc;
 253}
 254
 255static int qedi_alloc_uio_rings(struct qedi_ctx *qedi)
 256{
 257        struct qedi_uio_dev *udev = NULL;
 258        int rc = 0;
 259
 260        list_for_each_entry(udev, &qedi_udev_list, list) {
 261                if (udev->pdev == qedi->pdev) {
 262                        udev->qedi = qedi;
 263                        if (__qedi_alloc_uio_rings(udev)) {
 264                                udev->qedi = NULL;
 265                                return -ENOMEM;
 266                        }
 267                        qedi->udev = udev;
 268                        return 0;
 269                }
 270        }
 271
 272        udev = kzalloc(sizeof(*udev), GFP_KERNEL);
 273        if (!udev) {
 274                rc = -ENOMEM;
 275                goto err_udev;
 276        }
 277
 278        udev->uio_dev = -1;
 279
 280        udev->qedi = qedi;
 281        udev->pdev = qedi->pdev;
 282
 283        rc = __qedi_alloc_uio_rings(udev);
 284        if (rc)
 285                goto err_uctrl;
 286
 287        list_add(&udev->list, &qedi_udev_list);
 288
 289        pci_dev_get(udev->pdev);
 290        qedi->udev = udev;
 291
 292        udev->tx_pkt = udev->ll2_buf;
 293        udev->rx_pkt = udev->ll2_buf + qedi_ll2_buf_size;
 294        return 0;
 295
 296 err_uctrl:
 297        kfree(udev);
 298 err_udev:
 299        return -ENOMEM;
 300}
 301
 302static int qedi_init_uio(struct qedi_ctx *qedi)
 303{
 304        struct qedi_uio_dev *udev = qedi->udev;
 305        struct uio_info *uinfo;
 306        int ret = 0;
 307
 308        if (!udev)
 309                return -ENOMEM;
 310
 311        uinfo = &udev->qedi_uinfo;
 312
 313        uinfo->mem[0].addr = (unsigned long)udev->uctrl;
 314        uinfo->mem[0].size = sizeof(struct qedi_uio_ctrl);
 315        uinfo->mem[0].memtype = UIO_MEM_LOGICAL;
 316
 317        uinfo->mem[1].addr = (unsigned long)udev->ll2_ring;
 318        uinfo->mem[1].size = udev->ll2_ring_size;
 319        uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
 320
 321        uinfo->mem[2].addr = (unsigned long)udev->ll2_buf;
 322        uinfo->mem[2].size = udev->ll2_buf_size;
 323        uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
 324
 325        uinfo->name = "qedi_uio";
 326        uinfo->version = QEDI_MODULE_VERSION;
 327        uinfo->irq = UIO_IRQ_CUSTOM;
 328
 329        uinfo->open = qedi_uio_open;
 330        uinfo->release = qedi_uio_close;
 331
 332        if (udev->uio_dev == -1) {
 333                if (!uinfo->priv) {
 334                        uinfo->priv = udev;
 335
 336                        ret = uio_register_device(&udev->pdev->dev, uinfo);
 337                        if (ret) {
 338                                QEDI_ERR(&qedi->dbg_ctx,
 339                                         "UIO registration failed\n");
 340                        }
 341                }
 342        }
 343
 344        return ret;
 345}
 346
 347static int qedi_alloc_and_init_sb(struct qedi_ctx *qedi,
 348                                  struct qed_sb_info *sb_info, u16 sb_id)
 349{
 350        struct status_block_e4 *sb_virt;
 351        dma_addr_t sb_phys;
 352        int ret;
 353
 354        sb_virt = dma_alloc_coherent(&qedi->pdev->dev,
 355                                     sizeof(struct status_block_e4), &sb_phys,
 356                                     GFP_KERNEL);
 357        if (!sb_virt) {
 358                QEDI_ERR(&qedi->dbg_ctx,
 359                         "Status block allocation failed for id = %d.\n",
 360                          sb_id);
 361                return -ENOMEM;
 362        }
 363
 364        ret = qedi_ops->common->sb_init(qedi->cdev, sb_info, sb_virt, sb_phys,
 365                                       sb_id, QED_SB_TYPE_STORAGE);
 366        if (ret) {
 367                QEDI_ERR(&qedi->dbg_ctx,
 368                         "Status block initialization failed for id = %d.\n",
 369                          sb_id);
 370                return ret;
 371        }
 372
 373        return 0;
 374}
 375
 376static void qedi_free_sb(struct qedi_ctx *qedi)
 377{
 378        struct qed_sb_info *sb_info;
 379        int id;
 380
 381        for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
 382                sb_info = &qedi->sb_array[id];
 383                if (sb_info->sb_virt)
 384                        dma_free_coherent(&qedi->pdev->dev,
 385                                          sizeof(*sb_info->sb_virt),
 386                                          (void *)sb_info->sb_virt,
 387                                          sb_info->sb_phys);
 388        }
 389}
 390
 391static void qedi_free_fp(struct qedi_ctx *qedi)
 392{
 393        kfree(qedi->fp_array);
 394        kfree(qedi->sb_array);
 395}
 396
 397static void qedi_destroy_fp(struct qedi_ctx *qedi)
 398{
 399        qedi_free_sb(qedi);
 400        qedi_free_fp(qedi);
 401}
 402
 403static int qedi_alloc_fp(struct qedi_ctx *qedi)
 404{
 405        int ret = 0;
 406
 407        qedi->fp_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
 408                                 sizeof(struct qedi_fastpath), GFP_KERNEL);
 409        if (!qedi->fp_array) {
 410                QEDI_ERR(&qedi->dbg_ctx,
 411                         "fastpath fp array allocation failed.\n");
 412                return -ENOMEM;
 413        }
 414
 415        qedi->sb_array = kcalloc(MIN_NUM_CPUS_MSIX(qedi),
 416                                 sizeof(struct qed_sb_info), GFP_KERNEL);
 417        if (!qedi->sb_array) {
 418                QEDI_ERR(&qedi->dbg_ctx,
 419                         "fastpath sb array allocation failed.\n");
 420                ret = -ENOMEM;
 421                goto free_fp;
 422        }
 423
 424        return ret;
 425
 426free_fp:
 427        qedi_free_fp(qedi);
 428        return ret;
 429}
 430
 431static void qedi_int_fp(struct qedi_ctx *qedi)
 432{
 433        struct qedi_fastpath *fp;
 434        int id;
 435
 436        memset(qedi->fp_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
 437               sizeof(*qedi->fp_array));
 438        memset(qedi->sb_array, 0, MIN_NUM_CPUS_MSIX(qedi) *
 439               sizeof(*qedi->sb_array));
 440
 441        for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
 442                fp = &qedi->fp_array[id];
 443                fp->sb_info = &qedi->sb_array[id];
 444                fp->sb_id = id;
 445                fp->qedi = qedi;
 446                snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
 447                         "qedi", id);
 448
 449                /* fp_array[i] ---- irq cookie
 450                 * So init data which is needed in int ctx
 451                 */
 452        }
 453}
 454
 455static int qedi_prepare_fp(struct qedi_ctx *qedi)
 456{
 457        struct qedi_fastpath *fp;
 458        int id, ret = 0;
 459
 460        ret = qedi_alloc_fp(qedi);
 461        if (ret)
 462                goto err;
 463
 464        qedi_int_fp(qedi);
 465
 466        for (id = 0; id < MIN_NUM_CPUS_MSIX(qedi); id++) {
 467                fp = &qedi->fp_array[id];
 468                ret = qedi_alloc_and_init_sb(qedi, fp->sb_info, fp->sb_id);
 469                if (ret) {
 470                        QEDI_ERR(&qedi->dbg_ctx,
 471                                 "SB allocation and initialization failed.\n");
 472                        ret = -EIO;
 473                        goto err_init;
 474                }
 475        }
 476
 477        return 0;
 478
 479err_init:
 480        qedi_free_sb(qedi);
 481        qedi_free_fp(qedi);
 482err:
 483        return ret;
 484}
 485
 486static int qedi_setup_cid_que(struct qedi_ctx *qedi)
 487{
 488        int i;
 489
 490        qedi->cid_que.cid_que_base = kmalloc_array(qedi->max_active_conns,
 491                                                   sizeof(u32), GFP_KERNEL);
 492        if (!qedi->cid_que.cid_que_base)
 493                return -ENOMEM;
 494
 495        qedi->cid_que.conn_cid_tbl = kmalloc_array(qedi->max_active_conns,
 496                                                   sizeof(struct qedi_conn *),
 497                                                   GFP_KERNEL);
 498        if (!qedi->cid_que.conn_cid_tbl) {
 499                kfree(qedi->cid_que.cid_que_base);
 500                qedi->cid_que.cid_que_base = NULL;
 501                return -ENOMEM;
 502        }
 503
 504        qedi->cid_que.cid_que = (u32 *)qedi->cid_que.cid_que_base;
 505        qedi->cid_que.cid_q_prod_idx = 0;
 506        qedi->cid_que.cid_q_cons_idx = 0;
 507        qedi->cid_que.cid_q_max_idx = qedi->max_active_conns;
 508        qedi->cid_que.cid_free_cnt = qedi->max_active_conns;
 509
 510        for (i = 0; i < qedi->max_active_conns; i++) {
 511                qedi->cid_que.cid_que[i] = i;
 512                qedi->cid_que.conn_cid_tbl[i] = NULL;
 513        }
 514
 515        return 0;
 516}
 517
 518static void qedi_release_cid_que(struct qedi_ctx *qedi)
 519{
 520        kfree(qedi->cid_que.cid_que_base);
 521        qedi->cid_que.cid_que_base = NULL;
 522
 523        kfree(qedi->cid_que.conn_cid_tbl);
 524        qedi->cid_que.conn_cid_tbl = NULL;
 525}
 526
 527static int qedi_init_id_tbl(struct qedi_portid_tbl *id_tbl, u16 size,
 528                            u16 start_id, u16 next)
 529{
 530        id_tbl->start = start_id;
 531        id_tbl->max = size;
 532        id_tbl->next = next;
 533        spin_lock_init(&id_tbl->lock);
 534        id_tbl->table = kcalloc(BITS_TO_LONGS(size), sizeof(long), GFP_KERNEL);
 535        if (!id_tbl->table)
 536                return -ENOMEM;
 537
 538        return 0;
 539}
 540
 541static void qedi_free_id_tbl(struct qedi_portid_tbl *id_tbl)
 542{
 543        kfree(id_tbl->table);
 544        id_tbl->table = NULL;
 545}
 546
 547int qedi_alloc_id(struct qedi_portid_tbl *id_tbl, u16 id)
 548{
 549        int ret = -1;
 550
 551        id -= id_tbl->start;
 552        if (id >= id_tbl->max)
 553                return ret;
 554
 555        spin_lock(&id_tbl->lock);
 556        if (!test_bit(id, id_tbl->table)) {
 557                set_bit(id, id_tbl->table);
 558                ret = 0;
 559        }
 560        spin_unlock(&id_tbl->lock);
 561        return ret;
 562}
 563
 564u16 qedi_alloc_new_id(struct qedi_portid_tbl *id_tbl)
 565{
 566        u16 id;
 567
 568        spin_lock(&id_tbl->lock);
 569        id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
 570        if (id >= id_tbl->max) {
 571                id = QEDI_LOCAL_PORT_INVALID;
 572                if (id_tbl->next != 0) {
 573                        id = find_first_zero_bit(id_tbl->table, id_tbl->next);
 574                        if (id >= id_tbl->next)
 575                                id = QEDI_LOCAL_PORT_INVALID;
 576                }
 577        }
 578
 579        if (id < id_tbl->max) {
 580                set_bit(id, id_tbl->table);
 581                id_tbl->next = (id + 1) & (id_tbl->max - 1);
 582                id += id_tbl->start;
 583        }
 584
 585        spin_unlock(&id_tbl->lock);
 586
 587        return id;
 588}
 589
 590void qedi_free_id(struct qedi_portid_tbl *id_tbl, u16 id)
 591{
 592        if (id == QEDI_LOCAL_PORT_INVALID)
 593                return;
 594
 595        id -= id_tbl->start;
 596        if (id >= id_tbl->max)
 597                return;
 598
 599        clear_bit(id, id_tbl->table);
 600}
 601
 602static void qedi_cm_free_mem(struct qedi_ctx *qedi)
 603{
 604        kfree(qedi->ep_tbl);
 605        qedi->ep_tbl = NULL;
 606        qedi_free_id_tbl(&qedi->lcl_port_tbl);
 607}
 608
 609static int qedi_cm_alloc_mem(struct qedi_ctx *qedi)
 610{
 611        u16 port_id;
 612
 613        qedi->ep_tbl = kzalloc((qedi->max_active_conns *
 614                                sizeof(struct qedi_endpoint *)), GFP_KERNEL);
 615        if (!qedi->ep_tbl)
 616                return -ENOMEM;
 617        port_id = prandom_u32() % QEDI_LOCAL_PORT_RANGE;
 618        if (qedi_init_id_tbl(&qedi->lcl_port_tbl, QEDI_LOCAL_PORT_RANGE,
 619                             QEDI_LOCAL_PORT_MIN, port_id)) {
 620                qedi_cm_free_mem(qedi);
 621                return -ENOMEM;
 622        }
 623
 624        return 0;
 625}
 626
 627static struct qedi_ctx *qedi_host_alloc(struct pci_dev *pdev)
 628{
 629        struct Scsi_Host *shost;
 630        struct qedi_ctx *qedi = NULL;
 631
 632        shost = iscsi_host_alloc(&qedi_host_template,
 633                                 sizeof(struct qedi_ctx), 0);
 634        if (!shost) {
 635                QEDI_ERR(NULL, "Could not allocate shost\n");
 636                goto exit_setup_shost;
 637        }
 638
 639        shost->max_id = QEDI_MAX_ISCSI_CONNS_PER_HBA;
 640        shost->max_channel = 0;
 641        shost->max_lun = ~0;
 642        shost->max_cmd_len = 16;
 643        shost->transportt = qedi_scsi_transport;
 644
 645        qedi = iscsi_host_priv(shost);
 646        memset(qedi, 0, sizeof(*qedi));
 647        qedi->shost = shost;
 648        qedi->dbg_ctx.host_no = shost->host_no;
 649        qedi->pdev = pdev;
 650        qedi->dbg_ctx.pdev = pdev;
 651        qedi->max_active_conns = ISCSI_MAX_SESS_PER_HBA;
 652        qedi->max_sqes = QEDI_SQ_SIZE;
 653
 654        shost->nr_hw_queues = MIN_NUM_CPUS_MSIX(qedi);
 655
 656        pci_set_drvdata(pdev, qedi);
 657
 658exit_setup_shost:
 659        return qedi;
 660}
 661
 662static int qedi_ll2_rx(void *cookie, struct sk_buff *skb, u32 arg1, u32 arg2)
 663{
 664        struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
 665        struct skb_work_list *work;
 666        struct ethhdr *eh;
 667
 668        if (!qedi) {
 669                QEDI_ERR(NULL, "qedi is NULL\n");
 670                return -1;
 671        }
 672
 673        if (!test_bit(UIO_DEV_OPENED, &qedi->flags)) {
 674                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_UIO,
 675                          "UIO DEV is not opened\n");
 676                kfree_skb(skb);
 677                return 0;
 678        }
 679
 680        eh = (struct ethhdr *)skb->data;
 681        /* Undo VLAN encapsulation */
 682        if (eh->h_proto == htons(ETH_P_8021Q)) {
 683                memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
 684                eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
 685                skb_reset_mac_header(skb);
 686        }
 687
 688        /* Filter out non FIP/FCoE frames here to free them faster */
 689        if (eh->h_proto != htons(ETH_P_ARP) &&
 690            eh->h_proto != htons(ETH_P_IP) &&
 691            eh->h_proto != htons(ETH_P_IPV6)) {
 692                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
 693                          "Dropping frame ethertype [0x%x] len [0x%x].\n",
 694                          eh->h_proto, skb->len);
 695                kfree_skb(skb);
 696                return 0;
 697        }
 698
 699        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
 700                  "Allowed frame ethertype [0x%x] len [0x%x].\n",
 701                  eh->h_proto, skb->len);
 702
 703        work = kzalloc(sizeof(*work), GFP_ATOMIC);
 704        if (!work) {
 705                QEDI_WARN(&qedi->dbg_ctx,
 706                          "Could not allocate work so dropping frame.\n");
 707                kfree_skb(skb);
 708                return 0;
 709        }
 710
 711        INIT_LIST_HEAD(&work->list);
 712        work->skb = skb;
 713
 714        if (skb_vlan_tag_present(skb))
 715                work->vlan_id = skb_vlan_tag_get(skb);
 716
 717        if (work->vlan_id)
 718                __vlan_insert_tag(work->skb, htons(ETH_P_8021Q), work->vlan_id);
 719
 720        spin_lock_bh(&qedi->ll2_lock);
 721        list_add_tail(&work->list, &qedi->ll2_skb_list);
 722        spin_unlock_bh(&qedi->ll2_lock);
 723
 724        wake_up_process(qedi->ll2_recv_thread);
 725
 726        return 0;
 727}
 728
 729/* map this skb to iscsiuio mmaped region */
 730static int qedi_ll2_process_skb(struct qedi_ctx *qedi, struct sk_buff *skb,
 731                                u16 vlan_id)
 732{
 733        struct qedi_uio_dev *udev = NULL;
 734        struct qedi_uio_ctrl *uctrl = NULL;
 735        struct qedi_rx_bd rxbd;
 736        struct qedi_rx_bd *p_rxbd;
 737        u32 rx_bd_prod;
 738        void *pkt;
 739        int len = 0;
 740        u32 prod;
 741
 742        if (!qedi) {
 743                QEDI_ERR(NULL, "qedi is NULL\n");
 744                return -1;
 745        }
 746
 747        udev = qedi->udev;
 748        uctrl = udev->uctrl;
 749
 750        ++uctrl->hw_rx_prod_cnt;
 751        prod = (uctrl->hw_rx_prod + 1) % RX_RING;
 752
 753        pkt = udev->rx_pkt + (prod * qedi_ll2_buf_size);
 754        len = min_t(u32, skb->len, (u32)qedi_ll2_buf_size);
 755        memcpy(pkt, skb->data, len);
 756
 757        memset(&rxbd, 0, sizeof(rxbd));
 758        rxbd.rx_pkt_index = prod;
 759        rxbd.rx_pkt_len = len;
 760        rxbd.vlan_id = vlan_id;
 761
 762        uctrl->hw_rx_bd_prod = (uctrl->hw_rx_bd_prod + 1) % QEDI_NUM_RX_BD;
 763        rx_bd_prod = uctrl->hw_rx_bd_prod;
 764        p_rxbd = (struct qedi_rx_bd *)udev->ll2_ring;
 765        p_rxbd += rx_bd_prod;
 766
 767        memcpy(p_rxbd, &rxbd, sizeof(rxbd));
 768
 769        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
 770                  "hw_rx_prod [%d] prod [%d] hw_rx_bd_prod [%d] rx_pkt_idx [%d] rx_len [%d].\n",
 771                  uctrl->hw_rx_prod, prod, uctrl->hw_rx_bd_prod,
 772                  rxbd.rx_pkt_index, rxbd.rx_pkt_len);
 773        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_LL2,
 774                  "host_rx_cons [%d] hw_rx_bd_cons [%d].\n",
 775                  uctrl->host_rx_cons, uctrl->host_rx_bd_cons);
 776
 777        uctrl->hw_rx_prod = prod;
 778
 779        /* notify the iscsiuio about new packet */
 780        uio_event_notify(&udev->qedi_uinfo);
 781
 782        return 0;
 783}
 784
 785static void qedi_ll2_free_skbs(struct qedi_ctx *qedi)
 786{
 787        struct skb_work_list *work, *work_tmp;
 788
 789        spin_lock_bh(&qedi->ll2_lock);
 790        list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list, list) {
 791                list_del(&work->list);
 792                if (work->skb)
 793                        kfree_skb(work->skb);
 794                kfree(work);
 795        }
 796        spin_unlock_bh(&qedi->ll2_lock);
 797}
 798
 799static int qedi_ll2_recv_thread(void *arg)
 800{
 801        struct qedi_ctx *qedi = (struct qedi_ctx *)arg;
 802        struct skb_work_list *work, *work_tmp;
 803
 804        set_user_nice(current, -20);
 805
 806        while (!kthread_should_stop()) {
 807                spin_lock_bh(&qedi->ll2_lock);
 808                list_for_each_entry_safe(work, work_tmp, &qedi->ll2_skb_list,
 809                                         list) {
 810                        list_del(&work->list);
 811                        qedi_ll2_process_skb(qedi, work->skb, work->vlan_id);
 812                        kfree_skb(work->skb);
 813                        kfree(work);
 814                }
 815                set_current_state(TASK_INTERRUPTIBLE);
 816                spin_unlock_bh(&qedi->ll2_lock);
 817                schedule();
 818        }
 819
 820        __set_current_state(TASK_RUNNING);
 821        return 0;
 822}
 823
 824static int qedi_set_iscsi_pf_param(struct qedi_ctx *qedi)
 825{
 826        u8 num_sq_pages;
 827        u32 log_page_size;
 828        int rval = 0;
 829
 830
 831        num_sq_pages = (MAX_OUTSTANDING_TASKS_PER_CON * 8) / QEDI_PAGE_SIZE;
 832
 833        qedi->num_queues = MIN_NUM_CPUS_MSIX(qedi);
 834
 835        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
 836                  "Number of CQ count is %d\n", qedi->num_queues);
 837
 838        memset(&qedi->pf_params.iscsi_pf_params, 0,
 839               sizeof(qedi->pf_params.iscsi_pf_params));
 840
 841        qedi->p_cpuq = dma_alloc_coherent(&qedi->pdev->dev,
 842                        qedi->num_queues * sizeof(struct qedi_glbl_q_params),
 843                        &qedi->hw_p_cpuq, GFP_KERNEL);
 844        if (!qedi->p_cpuq) {
 845                QEDI_ERR(&qedi->dbg_ctx, "dma_alloc_coherent fail\n");
 846                rval = -1;
 847                goto err_alloc_mem;
 848        }
 849
 850        rval = qedi_alloc_global_queues(qedi);
 851        if (rval) {
 852                QEDI_ERR(&qedi->dbg_ctx, "Global queue allocation failed.\n");
 853                rval = -1;
 854                goto err_alloc_mem;
 855        }
 856
 857        qedi->pf_params.iscsi_pf_params.num_cons = QEDI_MAX_ISCSI_CONNS_PER_HBA;
 858        qedi->pf_params.iscsi_pf_params.num_tasks = QEDI_MAX_ISCSI_TASK;
 859        qedi->pf_params.iscsi_pf_params.half_way_close_timeout = 10;
 860        qedi->pf_params.iscsi_pf_params.num_sq_pages_in_ring = num_sq_pages;
 861        qedi->pf_params.iscsi_pf_params.num_r2tq_pages_in_ring = num_sq_pages;
 862        qedi->pf_params.iscsi_pf_params.num_uhq_pages_in_ring = num_sq_pages;
 863        qedi->pf_params.iscsi_pf_params.num_queues = qedi->num_queues;
 864        qedi->pf_params.iscsi_pf_params.debug_mode = qedi_fw_debug;
 865        qedi->pf_params.iscsi_pf_params.two_msl_timer = 4000;
 866        qedi->pf_params.iscsi_pf_params.max_fin_rt = 2;
 867
 868        for (log_page_size = 0 ; log_page_size < 32 ; log_page_size++) {
 869                if ((1 << log_page_size) == QEDI_PAGE_SIZE)
 870                        break;
 871        }
 872        qedi->pf_params.iscsi_pf_params.log_page_size = log_page_size;
 873
 874        qedi->pf_params.iscsi_pf_params.glbl_q_params_addr =
 875                                                           (u64)qedi->hw_p_cpuq;
 876
 877        /* RQ BDQ initializations.
 878         * rq_num_entries: suggested value for Initiator is 16 (4KB RQ)
 879         * rqe_log_size: 8 for 256B RQE
 880         */
 881        qedi->pf_params.iscsi_pf_params.rqe_log_size = 8;
 882        /* BDQ address and size */
 883        qedi->pf_params.iscsi_pf_params.bdq_pbl_base_addr[BDQ_ID_RQ] =
 884                                                        qedi->bdq_pbl_list_dma;
 885        qedi->pf_params.iscsi_pf_params.bdq_pbl_num_entries[BDQ_ID_RQ] =
 886                                                qedi->bdq_pbl_list_num_entries;
 887        qedi->pf_params.iscsi_pf_params.rq_buffer_size = QEDI_BDQ_BUF_SIZE;
 888
 889        /* cq_num_entries: num_tasks + rq_num_entries */
 890        qedi->pf_params.iscsi_pf_params.cq_num_entries = 2048;
 891
 892        qedi->pf_params.iscsi_pf_params.gl_rq_pi = QEDI_PROTO_CQ_PROD_IDX;
 893        qedi->pf_params.iscsi_pf_params.gl_cmd_pi = 1;
 894
 895err_alloc_mem:
 896        return rval;
 897}
 898
 899/* Free DMA coherent memory for array of queue pointers we pass to qed */
 900static void qedi_free_iscsi_pf_param(struct qedi_ctx *qedi)
 901{
 902        size_t size = 0;
 903
 904        if (qedi->p_cpuq) {
 905                size = qedi->num_queues * sizeof(struct qedi_glbl_q_params);
 906                dma_free_coherent(&qedi->pdev->dev, size, qedi->p_cpuq,
 907                                    qedi->hw_p_cpuq);
 908        }
 909
 910        qedi_free_global_queues(qedi);
 911
 912        kfree(qedi->global_queues);
 913}
 914
 915static void qedi_get_boot_tgt_info(struct nvm_iscsi_block *block,
 916                                   struct qedi_boot_target *tgt, u8 index)
 917{
 918        u32 ipv6_en;
 919
 920        ipv6_en = !!(block->generic.ctrl_flags &
 921                     NVM_ISCSI_CFG_GEN_IPV6_ENABLED);
 922
 923        snprintf(tgt->iscsi_name, sizeof(tgt->iscsi_name), "%s",
 924                 block->target[index].target_name.byte);
 925
 926        tgt->ipv6_en = ipv6_en;
 927
 928        if (ipv6_en)
 929                snprintf(tgt->ip_addr, IPV6_LEN, "%pI6\n",
 930                         block->target[index].ipv6_addr.byte);
 931        else
 932                snprintf(tgt->ip_addr, IPV4_LEN, "%pI4\n",
 933                         block->target[index].ipv4_addr.byte);
 934}
 935
 936static int qedi_find_boot_info(struct qedi_ctx *qedi,
 937                               struct qed_mfw_tlv_iscsi *iscsi,
 938                               struct nvm_iscsi_block *block)
 939{
 940        struct qedi_boot_target *pri_tgt = NULL, *sec_tgt = NULL;
 941        u32 pri_ctrl_flags = 0, sec_ctrl_flags = 0, found = 0;
 942        struct iscsi_cls_session *cls_sess;
 943        struct iscsi_cls_conn *cls_conn;
 944        struct qedi_conn *qedi_conn;
 945        struct iscsi_session *sess;
 946        struct iscsi_conn *conn;
 947        char ep_ip_addr[64];
 948        int i, ret = 0;
 949
 950        pri_ctrl_flags = !!(block->target[0].ctrl_flags &
 951                                        NVM_ISCSI_CFG_TARGET_ENABLED);
 952        if (pri_ctrl_flags) {
 953                pri_tgt = kzalloc(sizeof(*pri_tgt), GFP_KERNEL);
 954                if (!pri_tgt)
 955                        return -1;
 956                qedi_get_boot_tgt_info(block, pri_tgt, 0);
 957        }
 958
 959        sec_ctrl_flags = !!(block->target[1].ctrl_flags &
 960                                        NVM_ISCSI_CFG_TARGET_ENABLED);
 961        if (sec_ctrl_flags) {
 962                sec_tgt = kzalloc(sizeof(*sec_tgt), GFP_KERNEL);
 963                if (!sec_tgt) {
 964                        ret = -1;
 965                        goto free_tgt;
 966                }
 967                qedi_get_boot_tgt_info(block, sec_tgt, 1);
 968        }
 969
 970        for (i = 0; i < qedi->max_active_conns; i++) {
 971                qedi_conn = qedi_get_conn_from_id(qedi, i);
 972                if (!qedi_conn)
 973                        continue;
 974
 975                if (qedi_conn->ep->ip_type == TCP_IPV4)
 976                        snprintf(ep_ip_addr, IPV4_LEN, "%pI4\n",
 977                                 qedi_conn->ep->dst_addr);
 978                else
 979                        snprintf(ep_ip_addr, IPV6_LEN, "%pI6\n",
 980                                 qedi_conn->ep->dst_addr);
 981
 982                cls_conn = qedi_conn->cls_conn;
 983                conn = cls_conn->dd_data;
 984                cls_sess = iscsi_conn_to_session(cls_conn);
 985                sess = cls_sess->dd_data;
 986
 987                if (!iscsi_is_session_online(cls_sess))
 988                        continue;
 989
 990                if (!sess->targetname)
 991                        continue;
 992
 993                if (pri_ctrl_flags) {
 994                        if (!strcmp(pri_tgt->iscsi_name, sess->targetname) &&
 995                            !strcmp(pri_tgt->ip_addr, ep_ip_addr)) {
 996                                found = 1;
 997                                break;
 998                        }
 999                }
1000
1001                if (sec_ctrl_flags) {
1002                        if (!strcmp(sec_tgt->iscsi_name, sess->targetname) &&
1003                            !strcmp(sec_tgt->ip_addr, ep_ip_addr)) {
1004                                found = 1;
1005                                break;
1006                        }
1007                }
1008        }
1009
1010        if (found) {
1011                if (conn->hdrdgst_en) {
1012                        iscsi->header_digest_set = true;
1013                        iscsi->header_digest = 1;
1014                }
1015
1016                if (conn->datadgst_en) {
1017                        iscsi->data_digest_set = true;
1018                        iscsi->data_digest = 1;
1019                }
1020                iscsi->boot_taget_portal_set = true;
1021                iscsi->boot_taget_portal = sess->tpgt;
1022
1023        } else {
1024                ret = -1;
1025        }
1026
1027        if (sec_ctrl_flags)
1028                kfree(sec_tgt);
1029free_tgt:
1030        if (pri_ctrl_flags)
1031                kfree(pri_tgt);
1032
1033        return ret;
1034}
1035
1036static void qedi_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
1037{
1038        struct qedi_ctx *qedi;
1039
1040        if (!dev) {
1041                QEDI_INFO(NULL, QEDI_LOG_EVT,
1042                          "dev is NULL so ignoring get_generic_tlv_data request.\n");
1043                return;
1044        }
1045        qedi = (struct qedi_ctx *)dev;
1046
1047        memset(data, 0, sizeof(struct qed_generic_tlvs));
1048        ether_addr_copy(data->mac[0], qedi->mac);
1049}
1050
1051/*
1052 * Protocol TLV handler
1053 */
1054static void qedi_get_protocol_tlv_data(void *dev, void *data)
1055{
1056        struct qed_mfw_tlv_iscsi *iscsi = data;
1057        struct qed_iscsi_stats *fw_iscsi_stats;
1058        struct nvm_iscsi_block *block = NULL;
1059        u32 chap_en = 0, mchap_en = 0;
1060        struct qedi_ctx *qedi = dev;
1061        int rval = 0;
1062
1063        fw_iscsi_stats = kmalloc(sizeof(*fw_iscsi_stats), GFP_KERNEL);
1064        if (!fw_iscsi_stats) {
1065                QEDI_ERR(&qedi->dbg_ctx,
1066                         "Could not allocate memory for fw_iscsi_stats.\n");
1067                goto exit_get_data;
1068        }
1069
1070        mutex_lock(&qedi->stats_lock);
1071        /* Query firmware for offload stats */
1072        qedi_ops->get_stats(qedi->cdev, fw_iscsi_stats);
1073        mutex_unlock(&qedi->stats_lock);
1074
1075        iscsi->rx_frames_set = true;
1076        iscsi->rx_frames = fw_iscsi_stats->iscsi_rx_packet_cnt;
1077        iscsi->rx_bytes_set = true;
1078        iscsi->rx_bytes = fw_iscsi_stats->iscsi_rx_bytes_cnt;
1079        iscsi->tx_frames_set = true;
1080        iscsi->tx_frames = fw_iscsi_stats->iscsi_tx_packet_cnt;
1081        iscsi->tx_bytes_set = true;
1082        iscsi->tx_bytes = fw_iscsi_stats->iscsi_tx_bytes_cnt;
1083        iscsi->frame_size_set = true;
1084        iscsi->frame_size = qedi->ll2_mtu;
1085        block = qedi_get_nvram_block(qedi);
1086        if (block) {
1087                chap_en = !!(block->generic.ctrl_flags &
1088                             NVM_ISCSI_CFG_GEN_CHAP_ENABLED);
1089                mchap_en = !!(block->generic.ctrl_flags &
1090                              NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED);
1091
1092                iscsi->auth_method_set = (chap_en || mchap_en) ? true : false;
1093                iscsi->auth_method = 1;
1094                if (chap_en)
1095                        iscsi->auth_method = 2;
1096                if (mchap_en)
1097                        iscsi->auth_method = 3;
1098
1099                iscsi->tx_desc_size_set = true;
1100                iscsi->tx_desc_size = QEDI_SQ_SIZE;
1101                iscsi->rx_desc_size_set = true;
1102                iscsi->rx_desc_size = QEDI_CQ_SIZE;
1103
1104                /* tpgt, hdr digest, data digest */
1105                rval = qedi_find_boot_info(qedi, iscsi, block);
1106                if (rval)
1107                        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1108                                  "Boot target not set");
1109        }
1110
1111        kfree(fw_iscsi_stats);
1112exit_get_data:
1113        return;
1114}
1115
1116static void qedi_schedule_recovery_handler(void *dev)
1117{
1118        struct qedi_ctx *qedi = dev;
1119
1120        QEDI_ERR(&qedi->dbg_ctx, "Recovery handler scheduled.\n");
1121
1122        if (test_and_set_bit(QEDI_IN_RECOVERY, &qedi->flags))
1123                return;
1124
1125        atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
1126
1127        schedule_delayed_work(&qedi->recovery_work, 0);
1128}
1129
1130static void qedi_link_update(void *dev, struct qed_link_output *link)
1131{
1132        struct qedi_ctx *qedi = (struct qedi_ctx *)dev;
1133
1134        if (link->link_up) {
1135                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "Link Up event.\n");
1136                atomic_set(&qedi->link_state, QEDI_LINK_UP);
1137        } else {
1138                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1139                          "Link Down event.\n");
1140                atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
1141        }
1142}
1143
1144static struct qed_iscsi_cb_ops qedi_cb_ops = {
1145        {
1146                .link_update =          qedi_link_update,
1147                .schedule_recovery_handler = qedi_schedule_recovery_handler,
1148                .get_protocol_tlv_data = qedi_get_protocol_tlv_data,
1149                .get_generic_tlv_data = qedi_get_generic_tlv_data,
1150        }
1151};
1152
1153static int qedi_queue_cqe(struct qedi_ctx *qedi, union iscsi_cqe *cqe,
1154                          u16 que_idx, struct qedi_percpu_s *p)
1155{
1156        struct qedi_work *qedi_work;
1157        struct qedi_conn *q_conn;
1158        struct qedi_cmd *qedi_cmd;
1159        u32 iscsi_cid;
1160        int rc = 0;
1161
1162        iscsi_cid  = cqe->cqe_common.conn_id;
1163        q_conn = qedi->cid_que.conn_cid_tbl[iscsi_cid];
1164        if (!q_conn) {
1165                QEDI_WARN(&qedi->dbg_ctx,
1166                          "Session no longer exists for cid=0x%x!!\n",
1167                          iscsi_cid);
1168                return -1;
1169        }
1170
1171        switch (cqe->cqe_common.cqe_type) {
1172        case ISCSI_CQE_TYPE_SOLICITED:
1173        case ISCSI_CQE_TYPE_SOLICITED_WITH_SENSE:
1174                qedi_cmd = qedi_get_cmd_from_tid(qedi, cqe->cqe_solicited.itid);
1175                if (!qedi_cmd) {
1176                        rc = -1;
1177                        break;
1178                }
1179                INIT_LIST_HEAD(&qedi_cmd->cqe_work.list);
1180                qedi_cmd->cqe_work.qedi = qedi;
1181                memcpy(&qedi_cmd->cqe_work.cqe, cqe, sizeof(union iscsi_cqe));
1182                qedi_cmd->cqe_work.que_idx = que_idx;
1183                qedi_cmd->cqe_work.is_solicited = true;
1184                list_add_tail(&qedi_cmd->cqe_work.list, &p->work_list);
1185                break;
1186        case ISCSI_CQE_TYPE_UNSOLICITED:
1187        case ISCSI_CQE_TYPE_DUMMY:
1188        case ISCSI_CQE_TYPE_TASK_CLEANUP:
1189                qedi_work = kzalloc(sizeof(*qedi_work), GFP_ATOMIC);
1190                if (!qedi_work) {
1191                        rc = -1;
1192                        break;
1193                }
1194                INIT_LIST_HEAD(&qedi_work->list);
1195                qedi_work->qedi = qedi;
1196                memcpy(&qedi_work->cqe, cqe, sizeof(union iscsi_cqe));
1197                qedi_work->que_idx = que_idx;
1198                qedi_work->is_solicited = false;
1199                list_add_tail(&qedi_work->list, &p->work_list);
1200                break;
1201        default:
1202                rc = -1;
1203                QEDI_ERR(&qedi->dbg_ctx, "FW Error cqe.\n");
1204        }
1205        return rc;
1206}
1207
1208static bool qedi_process_completions(struct qedi_fastpath *fp)
1209{
1210        struct qedi_ctx *qedi = fp->qedi;
1211        struct qed_sb_info *sb_info = fp->sb_info;
1212        struct status_block_e4 *sb = sb_info->sb_virt;
1213        struct qedi_percpu_s *p = NULL;
1214        struct global_queue *que;
1215        u16 prod_idx;
1216        unsigned long flags;
1217        union iscsi_cqe *cqe;
1218        int cpu;
1219        int ret;
1220
1221        /* Get the current firmware producer index */
1222        prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
1223
1224        if (prod_idx >= QEDI_CQ_SIZE)
1225                prod_idx = prod_idx % QEDI_CQ_SIZE;
1226
1227        que = qedi->global_queues[fp->sb_id];
1228        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1229                  "Before: global queue=%p prod_idx=%d cons_idx=%d, sb_id=%d\n",
1230                  que, prod_idx, que->cq_cons_idx, fp->sb_id);
1231
1232        qedi->intr_cpu = fp->sb_id;
1233        cpu = smp_processor_id();
1234        p = &per_cpu(qedi_percpu, cpu);
1235
1236        if (unlikely(!p->iothread))
1237                WARN_ON(1);
1238
1239        spin_lock_irqsave(&p->p_work_lock, flags);
1240        while (que->cq_cons_idx != prod_idx) {
1241                cqe = &que->cq[que->cq_cons_idx];
1242
1243                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_IO,
1244                          "cqe=%p prod_idx=%d cons_idx=%d.\n",
1245                          cqe, prod_idx, que->cq_cons_idx);
1246
1247                ret = qedi_queue_cqe(qedi, cqe, fp->sb_id, p);
1248                if (ret)
1249                        QEDI_WARN(&qedi->dbg_ctx,
1250                                  "Dropping CQE 0x%x for cid=0x%x.\n",
1251                                  que->cq_cons_idx, cqe->cqe_common.conn_id);
1252
1253                que->cq_cons_idx++;
1254                if (que->cq_cons_idx == QEDI_CQ_SIZE)
1255                        que->cq_cons_idx = 0;
1256        }
1257        wake_up_process(p->iothread);
1258        spin_unlock_irqrestore(&p->p_work_lock, flags);
1259
1260        return true;
1261}
1262
1263static bool qedi_fp_has_work(struct qedi_fastpath *fp)
1264{
1265        struct qedi_ctx *qedi = fp->qedi;
1266        struct global_queue *que;
1267        struct qed_sb_info *sb_info = fp->sb_info;
1268        struct status_block_e4 *sb = sb_info->sb_virt;
1269        u16 prod_idx;
1270
1271        barrier();
1272
1273        /* Get the current firmware producer index */
1274        prod_idx = sb->pi_array[QEDI_PROTO_CQ_PROD_IDX];
1275
1276        /* Get the pointer to the global CQ this completion is on */
1277        que = qedi->global_queues[fp->sb_id];
1278
1279        /* prod idx wrap around uint16 */
1280        if (prod_idx >= QEDI_CQ_SIZE)
1281                prod_idx = prod_idx % QEDI_CQ_SIZE;
1282
1283        return (que->cq_cons_idx != prod_idx);
1284}
1285
1286/* MSI-X fastpath handler code */
1287static irqreturn_t qedi_msix_handler(int irq, void *dev_id)
1288{
1289        struct qedi_fastpath *fp = dev_id;
1290        struct qedi_ctx *qedi = fp->qedi;
1291        bool wake_io_thread = true;
1292
1293        qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0);
1294
1295process_again:
1296        wake_io_thread = qedi_process_completions(fp);
1297        if (wake_io_thread) {
1298                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1299                          "process already running\n");
1300        }
1301
1302        if (!qedi_fp_has_work(fp))
1303                qed_sb_update_sb_idx(fp->sb_info);
1304
1305        /* Check for more work */
1306        rmb();
1307
1308        if (!qedi_fp_has_work(fp))
1309                qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
1310        else
1311                goto process_again;
1312
1313        return IRQ_HANDLED;
1314}
1315
1316/* simd handler for MSI/INTa */
1317static void qedi_simd_int_handler(void *cookie)
1318{
1319        /* Cookie is qedi_ctx struct */
1320        struct qedi_ctx *qedi = (struct qedi_ctx *)cookie;
1321
1322        QEDI_WARN(&qedi->dbg_ctx, "qedi=%p.\n", qedi);
1323}
1324
1325#define QEDI_SIMD_HANDLER_NUM           0
1326static void qedi_sync_free_irqs(struct qedi_ctx *qedi)
1327{
1328        int i;
1329        u16 idx;
1330
1331        if (qedi->int_info.msix_cnt) {
1332                for (i = 0; i < qedi->int_info.used_cnt; i++) {
1333                        idx = i * qedi->dev_info.common.num_hwfns +
1334                        qedi_ops->common->get_affin_hwfn_idx(qedi->cdev);
1335
1336                        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1337                                  "Freeing IRQ #%d vector_idx=%d.\n", i, idx);
1338
1339                        synchronize_irq(qedi->int_info.msix[idx].vector);
1340                        irq_set_affinity_hint(qedi->int_info.msix[idx].vector,
1341                                              NULL);
1342                        free_irq(qedi->int_info.msix[idx].vector,
1343                                 &qedi->fp_array[i]);
1344                }
1345        } else {
1346                qedi_ops->common->simd_handler_clean(qedi->cdev,
1347                                                     QEDI_SIMD_HANDLER_NUM);
1348        }
1349
1350        qedi->int_info.used_cnt = 0;
1351        qedi_ops->common->set_fp_int(qedi->cdev, 0);
1352}
1353
1354static int qedi_request_msix_irq(struct qedi_ctx *qedi)
1355{
1356        int i, rc, cpu;
1357        u16 idx;
1358
1359        cpu = cpumask_first(cpu_online_mask);
1360        for (i = 0; i < qedi->int_info.msix_cnt; i++) {
1361                idx = i * qedi->dev_info.common.num_hwfns +
1362                          qedi_ops->common->get_affin_hwfn_idx(qedi->cdev);
1363
1364                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1365                          "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
1366                          qedi->dev_info.common.num_hwfns,
1367                          qedi_ops->common->get_affin_hwfn_idx(qedi->cdev));
1368
1369                rc = request_irq(qedi->int_info.msix[idx].vector,
1370                                 qedi_msix_handler, 0, "qedi",
1371                                 &qedi->fp_array[i]);
1372                if (rc) {
1373                        QEDI_WARN(&qedi->dbg_ctx, "request_irq failed.\n");
1374                        qedi_sync_free_irqs(qedi);
1375                        return rc;
1376                }
1377                qedi->int_info.used_cnt++;
1378                rc = irq_set_affinity_hint(qedi->int_info.msix[idx].vector,
1379                                           get_cpu_mask(cpu));
1380                cpu = cpumask_next(cpu, cpu_online_mask);
1381        }
1382
1383        return 0;
1384}
1385
1386static int qedi_setup_int(struct qedi_ctx *qedi)
1387{
1388        int rc = 0;
1389
1390        rc = qedi_ops->common->set_fp_int(qedi->cdev, num_online_cpus());
1391        rc = qedi_ops->common->get_fp_int(qedi->cdev, &qedi->int_info);
1392        if (rc)
1393                goto exit_setup_int;
1394
1395        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1396                  "Number of msix_cnt = 0x%x num of cpus = 0x%x\n",
1397                   qedi->int_info.msix_cnt, num_online_cpus());
1398
1399        if (qedi->int_info.msix_cnt) {
1400                rc = qedi_request_msix_irq(qedi);
1401                goto exit_setup_int;
1402        } else {
1403                qedi_ops->common->simd_handler_config(qedi->cdev, &qedi,
1404                                                      QEDI_SIMD_HANDLER_NUM,
1405                                                      qedi_simd_int_handler);
1406                qedi->int_info.used_cnt = 1;
1407        }
1408
1409exit_setup_int:
1410        return rc;
1411}
1412
1413static void qedi_free_nvm_iscsi_cfg(struct qedi_ctx *qedi)
1414{
1415        if (qedi->iscsi_image)
1416                dma_free_coherent(&qedi->pdev->dev,
1417                                  sizeof(struct qedi_nvm_iscsi_image),
1418                                  qedi->iscsi_image, qedi->nvm_buf_dma);
1419}
1420
1421static int qedi_alloc_nvm_iscsi_cfg(struct qedi_ctx *qedi)
1422{
1423        qedi->iscsi_image = dma_alloc_coherent(&qedi->pdev->dev,
1424                                               sizeof(struct qedi_nvm_iscsi_image),
1425                                               &qedi->nvm_buf_dma, GFP_KERNEL);
1426        if (!qedi->iscsi_image) {
1427                QEDI_ERR(&qedi->dbg_ctx, "Could not allocate NVM BUF.\n");
1428                return -ENOMEM;
1429        }
1430        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1431                  "NVM BUF addr=0x%p dma=0x%llx.\n", qedi->iscsi_image,
1432                  qedi->nvm_buf_dma);
1433
1434        return 0;
1435}
1436
1437static void qedi_free_bdq(struct qedi_ctx *qedi)
1438{
1439        int i;
1440
1441        if (qedi->bdq_pbl_list)
1442                dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
1443                                  qedi->bdq_pbl_list, qedi->bdq_pbl_list_dma);
1444
1445        if (qedi->bdq_pbl)
1446                dma_free_coherent(&qedi->pdev->dev, qedi->bdq_pbl_mem_size,
1447                                  qedi->bdq_pbl, qedi->bdq_pbl_dma);
1448
1449        for (i = 0; i < QEDI_BDQ_NUM; i++) {
1450                if (qedi->bdq[i].buf_addr) {
1451                        dma_free_coherent(&qedi->pdev->dev, QEDI_BDQ_BUF_SIZE,
1452                                          qedi->bdq[i].buf_addr,
1453                                          qedi->bdq[i].buf_dma);
1454                }
1455        }
1456}
1457
1458static void qedi_free_global_queues(struct qedi_ctx *qedi)
1459{
1460        int i;
1461        struct global_queue **gl = qedi->global_queues;
1462
1463        for (i = 0; i < qedi->num_queues; i++) {
1464                if (!gl[i])
1465                        continue;
1466
1467                if (gl[i]->cq)
1468                        dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_mem_size,
1469                                          gl[i]->cq, gl[i]->cq_dma);
1470                if (gl[i]->cq_pbl)
1471                        dma_free_coherent(&qedi->pdev->dev, gl[i]->cq_pbl_size,
1472                                          gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
1473
1474                kfree(gl[i]);
1475        }
1476        qedi_free_bdq(qedi);
1477        qedi_free_nvm_iscsi_cfg(qedi);
1478}
1479
1480static int qedi_alloc_bdq(struct qedi_ctx *qedi)
1481{
1482        int i;
1483        struct scsi_bd *pbl;
1484        u64 *list;
1485        dma_addr_t page;
1486
1487        /* Alloc dma memory for BDQ buffers */
1488        for (i = 0; i < QEDI_BDQ_NUM; i++) {
1489                qedi->bdq[i].buf_addr =
1490                                dma_alloc_coherent(&qedi->pdev->dev,
1491                                                   QEDI_BDQ_BUF_SIZE,
1492                                                   &qedi->bdq[i].buf_dma,
1493                                                   GFP_KERNEL);
1494                if (!qedi->bdq[i].buf_addr) {
1495                        QEDI_ERR(&qedi->dbg_ctx,
1496                                 "Could not allocate BDQ buffer %d.\n", i);
1497                        return -ENOMEM;
1498                }
1499        }
1500
1501        /* Alloc dma memory for BDQ page buffer list */
1502        qedi->bdq_pbl_mem_size = QEDI_BDQ_NUM * sizeof(struct scsi_bd);
1503        qedi->bdq_pbl_mem_size = ALIGN(qedi->bdq_pbl_mem_size, QEDI_PAGE_SIZE);
1504        qedi->rq_num_entries = qedi->bdq_pbl_mem_size / sizeof(struct scsi_bd);
1505
1506        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN, "rq_num_entries = %d.\n",
1507                  qedi->rq_num_entries);
1508
1509        qedi->bdq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
1510                                           qedi->bdq_pbl_mem_size,
1511                                           &qedi->bdq_pbl_dma, GFP_KERNEL);
1512        if (!qedi->bdq_pbl) {
1513                QEDI_ERR(&qedi->dbg_ctx, "Could not allocate BDQ PBL.\n");
1514                return -ENOMEM;
1515        }
1516
1517        /*
1518         * Populate BDQ PBL with physical and virtual address of individual
1519         * BDQ buffers
1520         */
1521        pbl = (struct scsi_bd  *)qedi->bdq_pbl;
1522        for (i = 0; i < QEDI_BDQ_NUM; i++) {
1523                pbl->address.hi =
1524                                cpu_to_le32(QEDI_U64_HI(qedi->bdq[i].buf_dma));
1525                pbl->address.lo =
1526                                cpu_to_le32(QEDI_U64_LO(qedi->bdq[i].buf_dma));
1527                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1528                          "pbl [0x%p] pbl->address hi [0x%llx] lo [0x%llx], idx [%d]\n",
1529                          pbl, pbl->address.hi, pbl->address.lo, i);
1530                pbl->opaque.iscsi_opaque.reserved_zero[0] = 0;
1531                pbl->opaque.iscsi_opaque.reserved_zero[1] = 0;
1532                pbl->opaque.iscsi_opaque.reserved_zero[2] = 0;
1533                pbl->opaque.iscsi_opaque.opaque = cpu_to_le16(i);
1534                pbl++;
1535        }
1536
1537        /* Allocate list of PBL pages */
1538        qedi->bdq_pbl_list = dma_alloc_coherent(&qedi->pdev->dev,
1539                                                QEDI_PAGE_SIZE,
1540                                                &qedi->bdq_pbl_list_dma,
1541                                                GFP_KERNEL);
1542        if (!qedi->bdq_pbl_list) {
1543                QEDI_ERR(&qedi->dbg_ctx,
1544                         "Could not allocate list of PBL pages.\n");
1545                return -ENOMEM;
1546        }
1547
1548        /*
1549         * Now populate PBL list with pages that contain pointers to the
1550         * individual buffers.
1551         */
1552        qedi->bdq_pbl_list_num_entries = qedi->bdq_pbl_mem_size /
1553                                         QEDI_PAGE_SIZE;
1554        list = (u64 *)qedi->bdq_pbl_list;
1555        page = qedi->bdq_pbl_list_dma;
1556        for (i = 0; i < qedi->bdq_pbl_list_num_entries; i++) {
1557                *list = qedi->bdq_pbl_dma;
1558                list++;
1559                page += QEDI_PAGE_SIZE;
1560        }
1561
1562        return 0;
1563}
1564
1565static int qedi_alloc_global_queues(struct qedi_ctx *qedi)
1566{
1567        u32 *list;
1568        int i;
1569        int status = 0, rc;
1570        u32 *pbl;
1571        dma_addr_t page;
1572        int num_pages;
1573
1574        /*
1575         * Number of global queues (CQ / RQ). This should
1576         * be <= number of available MSIX vectors for the PF
1577         */
1578        if (!qedi->num_queues) {
1579                QEDI_ERR(&qedi->dbg_ctx, "No MSI-X vectors available!\n");
1580                return 1;
1581        }
1582
1583        /* Make sure we allocated the PBL that will contain the physical
1584         * addresses of our queues
1585         */
1586        if (!qedi->p_cpuq) {
1587                status = 1;
1588                goto mem_alloc_failure;
1589        }
1590
1591        qedi->global_queues = kzalloc((sizeof(struct global_queue *) *
1592                                       qedi->num_queues), GFP_KERNEL);
1593        if (!qedi->global_queues) {
1594                QEDI_ERR(&qedi->dbg_ctx,
1595                         "Unable to allocate global queues array ptr memory\n");
1596                return -ENOMEM;
1597        }
1598        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
1599                  "qedi->global_queues=%p.\n", qedi->global_queues);
1600
1601        /* Allocate DMA coherent buffers for BDQ */
1602        rc = qedi_alloc_bdq(qedi);
1603        if (rc)
1604                goto mem_alloc_failure;
1605
1606        /* Allocate DMA coherent buffers for NVM_ISCSI_CFG */
1607        rc = qedi_alloc_nvm_iscsi_cfg(qedi);
1608        if (rc)
1609                goto mem_alloc_failure;
1610
1611        /* Allocate a CQ and an associated PBL for each MSI-X
1612         * vector.
1613         */
1614        for (i = 0; i < qedi->num_queues; i++) {
1615                qedi->global_queues[i] =
1616                                        kzalloc(sizeof(*qedi->global_queues[0]),
1617                                                GFP_KERNEL);
1618                if (!qedi->global_queues[i]) {
1619                        QEDI_ERR(&qedi->dbg_ctx,
1620                                 "Unable to allocation global queue %d.\n", i);
1621                        goto mem_alloc_failure;
1622                }
1623
1624                qedi->global_queues[i]->cq_mem_size =
1625                    (QEDI_CQ_SIZE + 8) * sizeof(union iscsi_cqe);
1626                qedi->global_queues[i]->cq_mem_size =
1627                    (qedi->global_queues[i]->cq_mem_size +
1628                    (QEDI_PAGE_SIZE - 1));
1629
1630                qedi->global_queues[i]->cq_pbl_size =
1631                    (qedi->global_queues[i]->cq_mem_size /
1632                    QEDI_PAGE_SIZE) * sizeof(void *);
1633                qedi->global_queues[i]->cq_pbl_size =
1634                    (qedi->global_queues[i]->cq_pbl_size +
1635                    (QEDI_PAGE_SIZE - 1));
1636
1637                qedi->global_queues[i]->cq = dma_alloc_coherent(&qedi->pdev->dev,
1638                                                                qedi->global_queues[i]->cq_mem_size,
1639                                                                &qedi->global_queues[i]->cq_dma,
1640                                                                GFP_KERNEL);
1641
1642                if (!qedi->global_queues[i]->cq) {
1643                        QEDI_WARN(&qedi->dbg_ctx,
1644                                  "Could not allocate cq.\n");
1645                        status = -ENOMEM;
1646                        goto mem_alloc_failure;
1647                }
1648                qedi->global_queues[i]->cq_pbl = dma_alloc_coherent(&qedi->pdev->dev,
1649                                                                    qedi->global_queues[i]->cq_pbl_size,
1650                                                                    &qedi->global_queues[i]->cq_pbl_dma,
1651                                                                    GFP_KERNEL);
1652
1653                if (!qedi->global_queues[i]->cq_pbl) {
1654                        QEDI_WARN(&qedi->dbg_ctx,
1655                                  "Could not allocate cq PBL.\n");
1656                        status = -ENOMEM;
1657                        goto mem_alloc_failure;
1658                }
1659
1660                /* Create PBL */
1661                num_pages = qedi->global_queues[i]->cq_mem_size /
1662                    QEDI_PAGE_SIZE;
1663                page = qedi->global_queues[i]->cq_dma;
1664                pbl = (u32 *)qedi->global_queues[i]->cq_pbl;
1665
1666                while (num_pages--) {
1667                        *pbl = (u32)page;
1668                        pbl++;
1669                        *pbl = (u32)((u64)page >> 32);
1670                        pbl++;
1671                        page += QEDI_PAGE_SIZE;
1672                }
1673        }
1674
1675        list = (u32 *)qedi->p_cpuq;
1676
1677        /*
1678         * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
1679         * CQ#1 PBL pointer, RQ#1 PBL pointer, etc.  Each PBL pointer points
1680         * to the physical address which contains an array of pointers to the
1681         * physical addresses of the specific queue pages.
1682         */
1683        for (i = 0; i < qedi->num_queues; i++) {
1684                *list = (u32)qedi->global_queues[i]->cq_pbl_dma;
1685                list++;
1686                *list = (u32)((u64)qedi->global_queues[i]->cq_pbl_dma >> 32);
1687                list++;
1688
1689                *list = (u32)0;
1690                list++;
1691                *list = (u32)((u64)0 >> 32);
1692                list++;
1693        }
1694
1695        return 0;
1696
1697mem_alloc_failure:
1698        qedi_free_global_queues(qedi);
1699        return status;
1700}
1701
1702int qedi_alloc_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1703{
1704        int rval = 0;
1705        u32 *pbl;
1706        dma_addr_t page;
1707        int num_pages;
1708
1709        if (!ep)
1710                return -EIO;
1711
1712        /* Calculate appropriate queue and PBL sizes */
1713        ep->sq_mem_size = QEDI_SQ_SIZE * sizeof(struct iscsi_wqe);
1714        ep->sq_mem_size += QEDI_PAGE_SIZE - 1;
1715
1716        ep->sq_pbl_size = (ep->sq_mem_size / QEDI_PAGE_SIZE) * sizeof(void *);
1717        ep->sq_pbl_size = ep->sq_pbl_size + QEDI_PAGE_SIZE;
1718
1719        ep->sq = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_mem_size,
1720                                    &ep->sq_dma, GFP_KERNEL);
1721        if (!ep->sq) {
1722                QEDI_WARN(&qedi->dbg_ctx,
1723                          "Could not allocate send queue.\n");
1724                rval = -ENOMEM;
1725                goto out;
1726        }
1727        ep->sq_pbl = dma_alloc_coherent(&qedi->pdev->dev, ep->sq_pbl_size,
1728                                        &ep->sq_pbl_dma, GFP_KERNEL);
1729        if (!ep->sq_pbl) {
1730                QEDI_WARN(&qedi->dbg_ctx,
1731                          "Could not allocate send queue PBL.\n");
1732                rval = -ENOMEM;
1733                goto out_free_sq;
1734        }
1735
1736        /* Create PBL */
1737        num_pages = ep->sq_mem_size / QEDI_PAGE_SIZE;
1738        page = ep->sq_dma;
1739        pbl = (u32 *)ep->sq_pbl;
1740
1741        while (num_pages--) {
1742                *pbl = (u32)page;
1743                pbl++;
1744                *pbl = (u32)((u64)page >> 32);
1745                pbl++;
1746                page += QEDI_PAGE_SIZE;
1747        }
1748
1749        return rval;
1750
1751out_free_sq:
1752        dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1753                          ep->sq_dma);
1754out:
1755        return rval;
1756}
1757
1758void qedi_free_sq(struct qedi_ctx *qedi, struct qedi_endpoint *ep)
1759{
1760        if (ep->sq_pbl)
1761                dma_free_coherent(&qedi->pdev->dev, ep->sq_pbl_size, ep->sq_pbl,
1762                                  ep->sq_pbl_dma);
1763        if (ep->sq)
1764                dma_free_coherent(&qedi->pdev->dev, ep->sq_mem_size, ep->sq,
1765                                  ep->sq_dma);
1766}
1767
1768int qedi_get_task_idx(struct qedi_ctx *qedi)
1769{
1770        s16 tmp_idx;
1771
1772again:
1773        tmp_idx = find_first_zero_bit(qedi->task_idx_map,
1774                                      MAX_ISCSI_TASK_ENTRIES);
1775
1776        if (tmp_idx >= MAX_ISCSI_TASK_ENTRIES) {
1777                QEDI_ERR(&qedi->dbg_ctx, "FW task context pool is full.\n");
1778                tmp_idx = -1;
1779                goto err_idx;
1780        }
1781
1782        if (test_and_set_bit(tmp_idx, qedi->task_idx_map))
1783                goto again;
1784
1785err_idx:
1786        return tmp_idx;
1787}
1788
1789void qedi_clear_task_idx(struct qedi_ctx *qedi, int idx)
1790{
1791        if (!test_and_clear_bit(idx, qedi->task_idx_map))
1792                QEDI_ERR(&qedi->dbg_ctx,
1793                         "FW task context, already cleared, tid=0x%x\n", idx);
1794}
1795
1796void qedi_update_itt_map(struct qedi_ctx *qedi, u32 tid, u32 proto_itt,
1797                         struct qedi_cmd *cmd)
1798{
1799        qedi->itt_map[tid].itt = proto_itt;
1800        qedi->itt_map[tid].p_cmd = cmd;
1801
1802        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1803                  "update itt map tid=0x%x, with proto itt=0x%x\n", tid,
1804                  qedi->itt_map[tid].itt);
1805}
1806
1807void qedi_get_task_tid(struct qedi_ctx *qedi, u32 itt, s16 *tid)
1808{
1809        u16 i;
1810
1811        for (i = 0; i < MAX_ISCSI_TASK_ENTRIES; i++) {
1812                if (qedi->itt_map[i].itt == itt) {
1813                        *tid = i;
1814                        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1815                                  "Ref itt=0x%x, found at tid=0x%x\n",
1816                                  itt, *tid);
1817                        return;
1818                }
1819        }
1820
1821        WARN_ON(1);
1822}
1823
1824void qedi_get_proto_itt(struct qedi_ctx *qedi, u32 tid, u32 *proto_itt)
1825{
1826        *proto_itt = qedi->itt_map[tid].itt;
1827        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1828                  "Get itt map tid [0x%x with proto itt[0x%x]",
1829                  tid, *proto_itt);
1830}
1831
1832struct qedi_cmd *qedi_get_cmd_from_tid(struct qedi_ctx *qedi, u32 tid)
1833{
1834        struct qedi_cmd *cmd = NULL;
1835
1836        if (tid >= MAX_ISCSI_TASK_ENTRIES)
1837                return NULL;
1838
1839        cmd = qedi->itt_map[tid].p_cmd;
1840        if (cmd->task_id != tid)
1841                return NULL;
1842
1843        qedi->itt_map[tid].p_cmd = NULL;
1844
1845        return cmd;
1846}
1847
1848static int qedi_alloc_itt(struct qedi_ctx *qedi)
1849{
1850        qedi->itt_map = kcalloc(MAX_ISCSI_TASK_ENTRIES,
1851                                sizeof(struct qedi_itt_map), GFP_KERNEL);
1852        if (!qedi->itt_map) {
1853                QEDI_ERR(&qedi->dbg_ctx,
1854                         "Unable to allocate itt map array memory\n");
1855                return -ENOMEM;
1856        }
1857        return 0;
1858}
1859
1860static void qedi_free_itt(struct qedi_ctx *qedi)
1861{
1862        kfree(qedi->itt_map);
1863}
1864
1865static struct qed_ll2_cb_ops qedi_ll2_cb_ops = {
1866        .rx_cb = qedi_ll2_rx,
1867        .tx_cb = NULL,
1868};
1869
1870static int qedi_percpu_io_thread(void *arg)
1871{
1872        struct qedi_percpu_s *p = arg;
1873        struct qedi_work *work, *tmp;
1874        unsigned long flags;
1875        LIST_HEAD(work_list);
1876
1877        set_user_nice(current, -20);
1878
1879        while (!kthread_should_stop()) {
1880                spin_lock_irqsave(&p->p_work_lock, flags);
1881                while (!list_empty(&p->work_list)) {
1882                        list_splice_init(&p->work_list, &work_list);
1883                        spin_unlock_irqrestore(&p->p_work_lock, flags);
1884
1885                        list_for_each_entry_safe(work, tmp, &work_list, list) {
1886                                list_del_init(&work->list);
1887                                qedi_fp_process_cqes(work);
1888                                if (!work->is_solicited)
1889                                        kfree(work);
1890                        }
1891                        cond_resched();
1892                        spin_lock_irqsave(&p->p_work_lock, flags);
1893                }
1894                set_current_state(TASK_INTERRUPTIBLE);
1895                spin_unlock_irqrestore(&p->p_work_lock, flags);
1896                schedule();
1897        }
1898        __set_current_state(TASK_RUNNING);
1899
1900        return 0;
1901}
1902
1903static int qedi_cpu_online(unsigned int cpu)
1904{
1905        struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1906        struct task_struct *thread;
1907
1908        thread = kthread_create_on_node(qedi_percpu_io_thread, (void *)p,
1909                                        cpu_to_node(cpu),
1910                                        "qedi_thread/%d", cpu);
1911        if (IS_ERR(thread))
1912                return PTR_ERR(thread);
1913
1914        kthread_bind(thread, cpu);
1915        p->iothread = thread;
1916        wake_up_process(thread);
1917        return 0;
1918}
1919
1920static int qedi_cpu_offline(unsigned int cpu)
1921{
1922        struct qedi_percpu_s *p = this_cpu_ptr(&qedi_percpu);
1923        struct qedi_work *work, *tmp;
1924        struct task_struct *thread;
1925
1926        spin_lock_bh(&p->p_work_lock);
1927        thread = p->iothread;
1928        p->iothread = NULL;
1929
1930        list_for_each_entry_safe(work, tmp, &p->work_list, list) {
1931                list_del_init(&work->list);
1932                qedi_fp_process_cqes(work);
1933                if (!work->is_solicited)
1934                        kfree(work);
1935        }
1936
1937        spin_unlock_bh(&p->p_work_lock);
1938        if (thread)
1939                kthread_stop(thread);
1940        return 0;
1941}
1942
1943void qedi_reset_host_mtu(struct qedi_ctx *qedi, u16 mtu)
1944{
1945        struct qed_ll2_params params;
1946
1947        qedi_recover_all_conns(qedi);
1948
1949        qedi_ops->ll2->stop(qedi->cdev);
1950        qedi_ll2_free_skbs(qedi);
1951
1952        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO, "old MTU %u, new MTU %u\n",
1953                  qedi->ll2_mtu, mtu);
1954        memset(&params, 0, sizeof(params));
1955        qedi->ll2_mtu = mtu;
1956        params.mtu = qedi->ll2_mtu + IPV6_HDR_LEN + TCP_HDR_LEN;
1957        params.drop_ttl0_packets = 0;
1958        params.rx_vlan_stripping = 1;
1959        ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
1960        qedi_ops->ll2->start(qedi->cdev, &params);
1961}
1962
1963/*
1964 * qedi_get_nvram_block: - Scan through the iSCSI NVRAM block (while accounting
1965 * for gaps) for the matching absolute-pf-id of the QEDI device.
1966 */
1967static struct nvm_iscsi_block *
1968qedi_get_nvram_block(struct qedi_ctx *qedi)
1969{
1970        int i;
1971        u8 pf;
1972        u32 flags;
1973        struct nvm_iscsi_block *block;
1974
1975        pf = qedi->dev_info.common.abs_pf_id;
1976        block = &qedi->iscsi_image->iscsi_cfg.block[0];
1977        for (i = 0; i < NUM_OF_ISCSI_PF_SUPPORTED; i++, block++) {
1978                flags = ((block->id) & NVM_ISCSI_CFG_BLK_CTRL_FLAG_MASK) >>
1979                        NVM_ISCSI_CFG_BLK_CTRL_FLAG_OFFSET;
1980                if (flags & (NVM_ISCSI_CFG_BLK_CTRL_FLAG_IS_NOT_EMPTY |
1981                                NVM_ISCSI_CFG_BLK_CTRL_FLAG_PF_MAPPED) &&
1982                        (pf == (block->id & NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_MASK)
1983                                >> NVM_ISCSI_CFG_BLK_MAPPED_PF_ID_OFFSET))
1984                        return block;
1985        }
1986        return NULL;
1987}
1988
1989static ssize_t qedi_show_boot_eth_info(void *data, int type, char *buf)
1990{
1991        struct qedi_ctx *qedi = data;
1992        struct nvm_iscsi_initiator *initiator;
1993        int rc = 1;
1994        u32 ipv6_en, dhcp_en, ip_len;
1995        struct nvm_iscsi_block *block;
1996        char *fmt, *ip, *sub, *gw;
1997
1998        block = qedi_get_nvram_block(qedi);
1999        if (!block)
2000                return 0;
2001
2002        initiator = &block->initiator;
2003        ipv6_en = block->generic.ctrl_flags &
2004                  NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
2005        dhcp_en = block->generic.ctrl_flags &
2006                  NVM_ISCSI_CFG_GEN_DHCP_TCPIP_CONFIG_ENABLED;
2007        /* Static IP assignments. */
2008        fmt = ipv6_en ? "%pI6\n" : "%pI4\n";
2009        ip = ipv6_en ? initiator->ipv6.addr.byte : initiator->ipv4.addr.byte;
2010        ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
2011        sub = ipv6_en ? initiator->ipv6.subnet_mask.byte :
2012              initiator->ipv4.subnet_mask.byte;
2013        gw = ipv6_en ? initiator->ipv6.gateway.byte :
2014             initiator->ipv4.gateway.byte;
2015        /* DHCP IP adjustments. */
2016        fmt = dhcp_en ? "%s\n" : fmt;
2017        if (dhcp_en) {
2018                ip = ipv6_en ? "0::0" : "0.0.0.0";
2019                sub = ip;
2020                gw = ip;
2021                ip_len = ipv6_en ? 5 : 8;
2022        }
2023
2024        switch (type) {
2025        case ISCSI_BOOT_ETH_IP_ADDR:
2026                rc = snprintf(buf, ip_len, fmt, ip);
2027                break;
2028        case ISCSI_BOOT_ETH_SUBNET_MASK:
2029                rc = snprintf(buf, ip_len, fmt, sub);
2030                break;
2031        case ISCSI_BOOT_ETH_GATEWAY:
2032                rc = snprintf(buf, ip_len, fmt, gw);
2033                break;
2034        case ISCSI_BOOT_ETH_FLAGS:
2035                rc = snprintf(buf, 3, "%hhd\n",
2036                              SYSFS_FLAG_FW_SEL_BOOT);
2037                break;
2038        case ISCSI_BOOT_ETH_INDEX:
2039                rc = snprintf(buf, 3, "0\n");
2040                break;
2041        case ISCSI_BOOT_ETH_MAC:
2042                rc = sysfs_format_mac(buf, qedi->mac, ETH_ALEN);
2043                break;
2044        case ISCSI_BOOT_ETH_VLAN:
2045                rc = snprintf(buf, 12, "%d\n",
2046                              GET_FIELD2(initiator->generic_cont0,
2047                                         NVM_ISCSI_CFG_INITIATOR_VLAN));
2048                break;
2049        case ISCSI_BOOT_ETH_ORIGIN:
2050                if (dhcp_en)
2051                        rc = snprintf(buf, 3, "3\n");
2052                break;
2053        default:
2054                rc = 0;
2055                break;
2056        }
2057
2058        return rc;
2059}
2060
2061static umode_t qedi_eth_get_attr_visibility(void *data, int type)
2062{
2063        int rc = 1;
2064
2065        switch (type) {
2066        case ISCSI_BOOT_ETH_FLAGS:
2067        case ISCSI_BOOT_ETH_MAC:
2068        case ISCSI_BOOT_ETH_INDEX:
2069        case ISCSI_BOOT_ETH_IP_ADDR:
2070        case ISCSI_BOOT_ETH_SUBNET_MASK:
2071        case ISCSI_BOOT_ETH_GATEWAY:
2072        case ISCSI_BOOT_ETH_ORIGIN:
2073        case ISCSI_BOOT_ETH_VLAN:
2074                rc = 0444;
2075                break;
2076        default:
2077                rc = 0;
2078                break;
2079        }
2080        return rc;
2081}
2082
2083static ssize_t qedi_show_boot_ini_info(void *data, int type, char *buf)
2084{
2085        struct qedi_ctx *qedi = data;
2086        struct nvm_iscsi_initiator *initiator;
2087        int rc;
2088        struct nvm_iscsi_block *block;
2089
2090        block = qedi_get_nvram_block(qedi);
2091        if (!block)
2092                return 0;
2093
2094        initiator = &block->initiator;
2095
2096        switch (type) {
2097        case ISCSI_BOOT_INI_INITIATOR_NAME:
2098                rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
2099                             initiator->initiator_name.byte);
2100                break;
2101        default:
2102                rc = 0;
2103                break;
2104        }
2105        return rc;
2106}
2107
2108static umode_t qedi_ini_get_attr_visibility(void *data, int type)
2109{
2110        int rc;
2111
2112        switch (type) {
2113        case ISCSI_BOOT_INI_INITIATOR_NAME:
2114                rc = 0444;
2115                break;
2116        default:
2117                rc = 0;
2118                break;
2119        }
2120        return rc;
2121}
2122
2123static ssize_t
2124qedi_show_boot_tgt_info(struct qedi_ctx *qedi, int type,
2125                        char *buf, enum qedi_nvm_tgts idx)
2126{
2127        int rc = 1;
2128        u32 ctrl_flags, ipv6_en, chap_en, mchap_en, ip_len;
2129        struct nvm_iscsi_block *block;
2130        char *chap_name, *chap_secret;
2131        char *mchap_name, *mchap_secret;
2132
2133        block = qedi_get_nvram_block(qedi);
2134        if (!block)
2135                goto exit_show_tgt_info;
2136
2137        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
2138                  "Port:%d, tgt_idx:%d\n",
2139                  GET_FIELD2(block->id, NVM_ISCSI_CFG_BLK_MAPPED_PF_ID), idx);
2140
2141        ctrl_flags = block->target[idx].ctrl_flags &
2142                     NVM_ISCSI_CFG_TARGET_ENABLED;
2143
2144        if (!ctrl_flags) {
2145                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_EVT,
2146                          "Target disabled\n");
2147                goto exit_show_tgt_info;
2148        }
2149
2150        ipv6_en = block->generic.ctrl_flags &
2151                  NVM_ISCSI_CFG_GEN_IPV6_ENABLED;
2152        ip_len = ipv6_en ? IPV6_LEN : IPV4_LEN;
2153        chap_en = block->generic.ctrl_flags &
2154                  NVM_ISCSI_CFG_GEN_CHAP_ENABLED;
2155        chap_name = chap_en ? block->initiator.chap_name.byte : NULL;
2156        chap_secret = chap_en ? block->initiator.chap_password.byte : NULL;
2157
2158        mchap_en = block->generic.ctrl_flags &
2159                  NVM_ISCSI_CFG_GEN_CHAP_MUTUAL_ENABLED;
2160        mchap_name = mchap_en ? block->target[idx].chap_name.byte : NULL;
2161        mchap_secret = mchap_en ? block->target[idx].chap_password.byte : NULL;
2162
2163        switch (type) {
2164        case ISCSI_BOOT_TGT_NAME:
2165                rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_ISCSI_NAME_MAX_LEN,
2166                             block->target[idx].target_name.byte);
2167                break;
2168        case ISCSI_BOOT_TGT_IP_ADDR:
2169                if (ipv6_en)
2170                        rc = snprintf(buf, ip_len, "%pI6\n",
2171                                      block->target[idx].ipv6_addr.byte);
2172                else
2173                        rc = snprintf(buf, ip_len, "%pI4\n",
2174                                      block->target[idx].ipv4_addr.byte);
2175                break;
2176        case ISCSI_BOOT_TGT_PORT:
2177                rc = snprintf(buf, 12, "%d\n",
2178                              GET_FIELD2(block->target[idx].generic_cont0,
2179                                         NVM_ISCSI_CFG_TARGET_TCP_PORT));
2180                break;
2181        case ISCSI_BOOT_TGT_LUN:
2182                rc = snprintf(buf, 22, "%.*d\n",
2183                              block->target[idx].lun.value[1],
2184                              block->target[idx].lun.value[0]);
2185                break;
2186        case ISCSI_BOOT_TGT_CHAP_NAME:
2187                rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2188                             chap_name);
2189                break;
2190        case ISCSI_BOOT_TGT_CHAP_SECRET:
2191                rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2192                             chap_secret);
2193                break;
2194        case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2195                rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2196                             mchap_name);
2197                break;
2198        case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2199                rc = sprintf(buf, "%.*s\n", NVM_ISCSI_CFG_CHAP_NAME_MAX_LEN,
2200                             mchap_secret);
2201                break;
2202        case ISCSI_BOOT_TGT_FLAGS:
2203                rc = snprintf(buf, 3, "%hhd\n", SYSFS_FLAG_FW_SEL_BOOT);
2204                break;
2205        case ISCSI_BOOT_TGT_NIC_ASSOC:
2206                rc = snprintf(buf, 3, "0\n");
2207                break;
2208        default:
2209                rc = 0;
2210                break;
2211        }
2212
2213exit_show_tgt_info:
2214        return rc;
2215}
2216
2217static ssize_t qedi_show_boot_tgt_pri_info(void *data, int type, char *buf)
2218{
2219        struct qedi_ctx *qedi = data;
2220
2221        return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_PRI);
2222}
2223
2224static ssize_t qedi_show_boot_tgt_sec_info(void *data, int type, char *buf)
2225{
2226        struct qedi_ctx *qedi = data;
2227
2228        return qedi_show_boot_tgt_info(qedi, type, buf, QEDI_NVM_TGT_SEC);
2229}
2230
2231static umode_t qedi_tgt_get_attr_visibility(void *data, int type)
2232{
2233        int rc;
2234
2235        switch (type) {
2236        case ISCSI_BOOT_TGT_NAME:
2237        case ISCSI_BOOT_TGT_IP_ADDR:
2238        case ISCSI_BOOT_TGT_PORT:
2239        case ISCSI_BOOT_TGT_LUN:
2240        case ISCSI_BOOT_TGT_CHAP_NAME:
2241        case ISCSI_BOOT_TGT_CHAP_SECRET:
2242        case ISCSI_BOOT_TGT_REV_CHAP_NAME:
2243        case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
2244        case ISCSI_BOOT_TGT_NIC_ASSOC:
2245        case ISCSI_BOOT_TGT_FLAGS:
2246                rc = 0444;
2247                break;
2248        default:
2249                rc = 0;
2250                break;
2251        }
2252        return rc;
2253}
2254
2255static void qedi_boot_release(void *data)
2256{
2257        struct qedi_ctx *qedi = data;
2258
2259        scsi_host_put(qedi->shost);
2260}
2261
2262static int qedi_get_boot_info(struct qedi_ctx *qedi)
2263{
2264        int ret = 1;
2265
2266        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2267                  "Get NVM iSCSI CFG image\n");
2268        ret = qedi_ops->common->nvm_get_image(qedi->cdev,
2269                                              QED_NVM_IMAGE_ISCSI_CFG,
2270                                              (char *)qedi->iscsi_image,
2271                                              sizeof(struct qedi_nvm_iscsi_image));
2272        if (ret)
2273                QEDI_ERR(&qedi->dbg_ctx,
2274                         "Could not get NVM image. ret = %d\n", ret);
2275
2276        return ret;
2277}
2278
2279static int qedi_setup_boot_info(struct qedi_ctx *qedi)
2280{
2281        struct iscsi_boot_kobj *boot_kobj;
2282
2283        if (qedi_get_boot_info(qedi))
2284                return -EPERM;
2285
2286        qedi->boot_kset = iscsi_boot_create_host_kset(qedi->shost->host_no);
2287        if (!qedi->boot_kset)
2288                goto kset_free;
2289
2290        if (!scsi_host_get(qedi->shost))
2291                goto kset_free;
2292
2293        boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 0, qedi,
2294                                             qedi_show_boot_tgt_pri_info,
2295                                             qedi_tgt_get_attr_visibility,
2296                                             qedi_boot_release);
2297        if (!boot_kobj)
2298                goto put_host;
2299
2300        if (!scsi_host_get(qedi->shost))
2301                goto kset_free;
2302
2303        boot_kobj = iscsi_boot_create_target(qedi->boot_kset, 1, qedi,
2304                                             qedi_show_boot_tgt_sec_info,
2305                                             qedi_tgt_get_attr_visibility,
2306                                             qedi_boot_release);
2307        if (!boot_kobj)
2308                goto put_host;
2309
2310        if (!scsi_host_get(qedi->shost))
2311                goto kset_free;
2312
2313        boot_kobj = iscsi_boot_create_initiator(qedi->boot_kset, 0, qedi,
2314                                                qedi_show_boot_ini_info,
2315                                                qedi_ini_get_attr_visibility,
2316                                                qedi_boot_release);
2317        if (!boot_kobj)
2318                goto put_host;
2319
2320        if (!scsi_host_get(qedi->shost))
2321                goto kset_free;
2322
2323        boot_kobj = iscsi_boot_create_ethernet(qedi->boot_kset, 0, qedi,
2324                                               qedi_show_boot_eth_info,
2325                                               qedi_eth_get_attr_visibility,
2326                                               qedi_boot_release);
2327        if (!boot_kobj)
2328                goto put_host;
2329
2330        return 0;
2331
2332put_host:
2333        scsi_host_put(qedi->shost);
2334kset_free:
2335        iscsi_boot_destroy_kset(qedi->boot_kset);
2336        return -ENOMEM;
2337}
2338
2339static void __qedi_remove(struct pci_dev *pdev, int mode)
2340{
2341        struct qedi_ctx *qedi = pci_get_drvdata(pdev);
2342        int rval;
2343
2344        if (mode == QEDI_MODE_SHUTDOWN)
2345                iscsi_host_for_each_session(qedi->shost,
2346                                            qedi_clear_session_ctx);
2347
2348        if (mode == QEDI_MODE_NORMAL || mode == QEDI_MODE_SHUTDOWN) {
2349                if (qedi->tmf_thread) {
2350                        flush_workqueue(qedi->tmf_thread);
2351                        destroy_workqueue(qedi->tmf_thread);
2352                        qedi->tmf_thread = NULL;
2353                }
2354
2355                if (qedi->offload_thread) {
2356                        flush_workqueue(qedi->offload_thread);
2357                        destroy_workqueue(qedi->offload_thread);
2358                        qedi->offload_thread = NULL;
2359                }
2360        }
2361
2362#ifdef CONFIG_DEBUG_FS
2363        qedi_dbg_host_exit(&qedi->dbg_ctx);
2364#endif
2365        if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags))
2366                qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
2367
2368        qedi_sync_free_irqs(qedi);
2369
2370        if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
2371                qedi_ops->stop(qedi->cdev);
2372                qedi_ops->ll2->stop(qedi->cdev);
2373        }
2374
2375        qedi_free_iscsi_pf_param(qedi);
2376
2377        rval = qedi_ops->common->update_drv_state(qedi->cdev, false);
2378        if (rval)
2379                QEDI_ERR(&qedi->dbg_ctx, "Failed to send drv state to MFW\n");
2380
2381        if (!test_bit(QEDI_IN_OFFLINE, &qedi->flags)) {
2382                qedi_ops->common->slowpath_stop(qedi->cdev);
2383                qedi_ops->common->remove(qedi->cdev);
2384        }
2385
2386        qedi_destroy_fp(qedi);
2387
2388        if (mode == QEDI_MODE_NORMAL || mode == QEDI_MODE_SHUTDOWN) {
2389                qedi_release_cid_que(qedi);
2390                qedi_cm_free_mem(qedi);
2391                qedi_free_uio(qedi->udev);
2392                qedi_free_itt(qedi);
2393
2394                if (qedi->ll2_recv_thread) {
2395                        kthread_stop(qedi->ll2_recv_thread);
2396                        qedi->ll2_recv_thread = NULL;
2397                }
2398                qedi_ll2_free_skbs(qedi);
2399
2400                if (qedi->boot_kset)
2401                        iscsi_boot_destroy_kset(qedi->boot_kset);
2402
2403                iscsi_host_remove(qedi->shost);
2404                iscsi_host_free(qedi->shost);
2405        }
2406}
2407
2408static void qedi_shutdown(struct pci_dev *pdev)
2409{
2410        struct qedi_ctx *qedi = pci_get_drvdata(pdev);
2411
2412        QEDI_ERR(&qedi->dbg_ctx, "%s: Shutdown qedi\n", __func__);
2413        if (test_and_set_bit(QEDI_IN_SHUTDOWN, &qedi->flags))
2414                return;
2415        __qedi_remove(pdev, QEDI_MODE_SHUTDOWN);
2416}
2417
2418static int __qedi_probe(struct pci_dev *pdev, int mode)
2419{
2420        struct qedi_ctx *qedi;
2421        struct qed_ll2_params params;
2422        u8 dp_level = 0;
2423        bool is_vf = false;
2424        char host_buf[16];
2425        struct qed_link_params link_params;
2426        struct qed_slowpath_params sp_params;
2427        struct qed_probe_params qed_params;
2428        void *task_start, *task_end;
2429        int rc;
2430
2431        if (mode != QEDI_MODE_RECOVERY) {
2432                qedi = qedi_host_alloc(pdev);
2433                if (!qedi) {
2434                        rc = -ENOMEM;
2435                        goto exit_probe;
2436                }
2437        } else {
2438                qedi = pci_get_drvdata(pdev);
2439        }
2440
2441        memset(&qed_params, 0, sizeof(qed_params));
2442        qed_params.protocol = QED_PROTOCOL_ISCSI;
2443        qed_params.dp_module = qedi_qed_debug;
2444        qed_params.dp_level = dp_level;
2445        qed_params.is_vf = is_vf;
2446        qedi->cdev = qedi_ops->common->probe(pdev, &qed_params);
2447        if (!qedi->cdev) {
2448                rc = -ENODEV;
2449                QEDI_ERR(&qedi->dbg_ctx, "Cannot initialize hardware\n");
2450                goto free_host;
2451        }
2452
2453        atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
2454
2455        rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
2456        if (rc)
2457                goto free_host;
2458
2459        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2460                  "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
2461                  qedi->dev_info.common.num_hwfns,
2462                  qedi_ops->common->get_affin_hwfn_idx(qedi->cdev));
2463
2464        rc = qedi_set_iscsi_pf_param(qedi);
2465        if (rc) {
2466                rc = -ENOMEM;
2467                QEDI_ERR(&qedi->dbg_ctx,
2468                         "Set iSCSI pf param fail\n");
2469                goto free_host;
2470        }
2471
2472        qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
2473
2474        rc = qedi_prepare_fp(qedi);
2475        if (rc) {
2476                QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath.\n");
2477                goto free_pf_params;
2478        }
2479
2480        /* Start the Slowpath-process */
2481        memset(&sp_params, 0, sizeof(struct qed_slowpath_params));
2482        sp_params.int_mode = QED_INT_MODE_MSIX;
2483        sp_params.drv_major = QEDI_DRIVER_MAJOR_VER;
2484        sp_params.drv_minor = QEDI_DRIVER_MINOR_VER;
2485        sp_params.drv_rev = QEDI_DRIVER_REV_VER;
2486        sp_params.drv_eng = QEDI_DRIVER_ENG_VER;
2487        strlcpy(sp_params.name, "qedi iSCSI", QED_DRV_VER_STR_SIZE);
2488        rc = qedi_ops->common->slowpath_start(qedi->cdev, &sp_params);
2489        if (rc) {
2490                QEDI_ERR(&qedi->dbg_ctx, "Cannot start slowpath\n");
2491                goto stop_hw;
2492        }
2493
2494        /* update_pf_params needs to be called before and after slowpath
2495         * start
2496         */
2497        qedi_ops->common->update_pf_params(qedi->cdev, &qedi->pf_params);
2498
2499        rc = qedi_setup_int(qedi);
2500        if (rc)
2501                goto stop_iscsi_func;
2502
2503        qedi_ops->common->set_power_state(qedi->cdev, PCI_D0);
2504
2505        /* Learn information crucial for qedi to progress */
2506        rc = qedi_ops->fill_dev_info(qedi->cdev, &qedi->dev_info);
2507        if (rc)
2508                goto stop_iscsi_func;
2509
2510        /* Record BDQ producer doorbell addresses */
2511        qedi->bdq_primary_prod = qedi->dev_info.primary_dbq_rq_addr;
2512        qedi->bdq_secondary_prod = qedi->dev_info.secondary_bdq_rq_addr;
2513        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2514                  "BDQ primary_prod=%p secondary_prod=%p.\n",
2515                  qedi->bdq_primary_prod,
2516                  qedi->bdq_secondary_prod);
2517
2518        /*
2519         * We need to write the number of BDs in the BDQ we've preallocated so
2520         * the f/w will do a prefetch and we'll get an unsolicited CQE when a
2521         * packet arrives.
2522         */
2523        qedi->bdq_prod_idx = QEDI_BDQ_NUM;
2524        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2525                  "Writing %d to primary and secondary BDQ doorbell registers.\n",
2526                  qedi->bdq_prod_idx);
2527        writew(qedi->bdq_prod_idx, qedi->bdq_primary_prod);
2528        readw(qedi->bdq_primary_prod);
2529        writew(qedi->bdq_prod_idx, qedi->bdq_secondary_prod);
2530        readw(qedi->bdq_secondary_prod);
2531
2532        ether_addr_copy(qedi->mac, qedi->dev_info.common.hw_mac);
2533        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC, "MAC address is %pM.\n",
2534                  qedi->mac);
2535
2536        sprintf(host_buf, "host_%d", qedi->shost->host_no);
2537        qedi_ops->common->set_name(qedi->cdev, host_buf);
2538
2539        qedi_ops->register_ops(qedi->cdev, &qedi_cb_ops, qedi);
2540
2541        memset(&params, 0, sizeof(params));
2542        params.mtu = DEF_PATH_MTU + IPV6_HDR_LEN + TCP_HDR_LEN;
2543        qedi->ll2_mtu = DEF_PATH_MTU;
2544        params.drop_ttl0_packets = 0;
2545        params.rx_vlan_stripping = 1;
2546        ether_addr_copy(params.ll2_mac_address, qedi->dev_info.common.hw_mac);
2547
2548        if (mode != QEDI_MODE_RECOVERY) {
2549                /* set up rx path */
2550                INIT_LIST_HEAD(&qedi->ll2_skb_list);
2551                spin_lock_init(&qedi->ll2_lock);
2552                /* start qedi context */
2553                spin_lock_init(&qedi->hba_lock);
2554                spin_lock_init(&qedi->task_idx_lock);
2555                mutex_init(&qedi->stats_lock);
2556        }
2557        qedi_ops->ll2->register_cb_ops(qedi->cdev, &qedi_ll2_cb_ops, qedi);
2558        qedi_ops->ll2->start(qedi->cdev, &params);
2559
2560        if (mode != QEDI_MODE_RECOVERY) {
2561                qedi->ll2_recv_thread = kthread_run(qedi_ll2_recv_thread,
2562                                                    (void *)qedi,
2563                                                    "qedi_ll2_thread");
2564        }
2565
2566        rc = qedi_ops->start(qedi->cdev, &qedi->tasks,
2567                             qedi, qedi_iscsi_event_cb);
2568        if (rc) {
2569                rc = -ENODEV;
2570                QEDI_ERR(&qedi->dbg_ctx, "Cannot start iSCSI function\n");
2571                goto stop_slowpath;
2572        }
2573
2574        task_start = qedi_get_task_mem(&qedi->tasks, 0);
2575        task_end = qedi_get_task_mem(&qedi->tasks, MAX_TID_BLOCKS_ISCSI - 1);
2576        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_DISC,
2577                  "Task context start=%p, end=%p block_size=%u.\n",
2578                   task_start, task_end, qedi->tasks.size);
2579
2580        memset(&link_params, 0, sizeof(link_params));
2581        link_params.link_up = true;
2582        rc = qedi_ops->common->set_link(qedi->cdev, &link_params);
2583        if (rc) {
2584                QEDI_WARN(&qedi->dbg_ctx, "Link set up failed.\n");
2585                atomic_set(&qedi->link_state, QEDI_LINK_DOWN);
2586        }
2587
2588#ifdef CONFIG_DEBUG_FS
2589        qedi_dbg_host_init(&qedi->dbg_ctx, qedi_debugfs_ops,
2590                           qedi_dbg_fops);
2591#endif
2592        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
2593                  "QLogic FastLinQ iSCSI Module qedi %s, FW %d.%d.%d.%d\n",
2594                  QEDI_MODULE_VERSION, FW_MAJOR_VERSION, FW_MINOR_VERSION,
2595                  FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
2596
2597        if (mode == QEDI_MODE_NORMAL) {
2598                if (iscsi_host_add(qedi->shost, &pdev->dev)) {
2599                        QEDI_ERR(&qedi->dbg_ctx,
2600                                 "Could not add iscsi host\n");
2601                        rc = -ENOMEM;
2602                        goto remove_host;
2603                }
2604
2605                /* Allocate uio buffers */
2606                rc = qedi_alloc_uio_rings(qedi);
2607                if (rc) {
2608                        QEDI_ERR(&qedi->dbg_ctx,
2609                                 "UIO alloc ring failed err=%d\n", rc);
2610                        goto remove_host;
2611                }
2612
2613                rc = qedi_init_uio(qedi);
2614                if (rc) {
2615                        QEDI_ERR(&qedi->dbg_ctx,
2616                                 "UIO init failed, err=%d\n", rc);
2617                        goto free_uio;
2618                }
2619
2620                /* host the array on iscsi_conn */
2621                rc = qedi_setup_cid_que(qedi);
2622                if (rc) {
2623                        QEDI_ERR(&qedi->dbg_ctx,
2624                                 "Could not setup cid que\n");
2625                        goto free_uio;
2626                }
2627
2628                rc = qedi_cm_alloc_mem(qedi);
2629                if (rc) {
2630                        QEDI_ERR(&qedi->dbg_ctx,
2631                                 "Could not alloc cm memory\n");
2632                        goto free_cid_que;
2633                }
2634
2635                rc = qedi_alloc_itt(qedi);
2636                if (rc) {
2637                        QEDI_ERR(&qedi->dbg_ctx,
2638                                 "Could not alloc itt memory\n");
2639                        goto free_cid_que;
2640                }
2641
2642                sprintf(host_buf, "host_%d", qedi->shost->host_no);
2643                qedi->tmf_thread = create_singlethread_workqueue(host_buf);
2644                if (!qedi->tmf_thread) {
2645                        QEDI_ERR(&qedi->dbg_ctx,
2646                                 "Unable to start tmf thread!\n");
2647                        rc = -ENODEV;
2648                        goto free_cid_que;
2649                }
2650
2651                sprintf(host_buf, "qedi_ofld%d", qedi->shost->host_no);
2652                qedi->offload_thread = create_workqueue(host_buf);
2653                if (!qedi->offload_thread) {
2654                        QEDI_ERR(&qedi->dbg_ctx,
2655                                 "Unable to start offload thread!\n");
2656                        rc = -ENODEV;
2657                        goto free_cid_que;
2658                }
2659
2660                INIT_DELAYED_WORK(&qedi->recovery_work, qedi_recovery_handler);
2661
2662                /* F/w needs 1st task context memory entry for performance */
2663                set_bit(QEDI_RESERVE_TASK_ID, qedi->task_idx_map);
2664                atomic_set(&qedi->num_offloads, 0);
2665
2666                if (qedi_setup_boot_info(qedi))
2667                        QEDI_ERR(&qedi->dbg_ctx,
2668                                 "No iSCSI boot target configured\n");
2669
2670                rc = qedi_ops->common->update_drv_state(qedi->cdev, true);
2671                if (rc)
2672                        QEDI_ERR(&qedi->dbg_ctx,
2673                                 "Failed to send drv state to MFW\n");
2674
2675        }
2676
2677        return 0;
2678
2679free_cid_que:
2680        qedi_release_cid_que(qedi);
2681free_uio:
2682        qedi_free_uio(qedi->udev);
2683remove_host:
2684#ifdef CONFIG_DEBUG_FS
2685        qedi_dbg_host_exit(&qedi->dbg_ctx);
2686#endif
2687        iscsi_host_remove(qedi->shost);
2688stop_iscsi_func:
2689        qedi_ops->stop(qedi->cdev);
2690stop_slowpath:
2691        qedi_ops->common->slowpath_stop(qedi->cdev);
2692stop_hw:
2693        qedi_ops->common->remove(qedi->cdev);
2694free_pf_params:
2695        qedi_free_iscsi_pf_param(qedi);
2696free_host:
2697        iscsi_host_free(qedi->shost);
2698exit_probe:
2699        return rc;
2700}
2701
2702static void qedi_mark_conn_recovery(struct iscsi_cls_session *cls_session)
2703{
2704        struct iscsi_session *session = cls_session->dd_data;
2705        struct iscsi_conn *conn = session->leadconn;
2706        struct qedi_conn *qedi_conn = conn->dd_data;
2707
2708        iscsi_conn_failure(qedi_conn->cls_conn->dd_data, ISCSI_ERR_CONN_FAILED);
2709}
2710
2711static void qedi_recovery_handler(struct work_struct *work)
2712{
2713        struct qedi_ctx *qedi =
2714                        container_of(work, struct qedi_ctx, recovery_work.work);
2715
2716        iscsi_host_for_each_session(qedi->shost, qedi_mark_conn_recovery);
2717
2718        /* Call common_ops->recovery_prolog to allow the MFW to quiesce
2719         * any PCI transactions.
2720         */
2721        qedi_ops->common->recovery_prolog(qedi->cdev);
2722
2723        __qedi_remove(qedi->pdev, QEDI_MODE_RECOVERY);
2724        __qedi_probe(qedi->pdev, QEDI_MODE_RECOVERY);
2725        clear_bit(QEDI_IN_RECOVERY, &qedi->flags);
2726}
2727
2728static int qedi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2729{
2730        return __qedi_probe(pdev, QEDI_MODE_NORMAL);
2731}
2732
2733static void qedi_remove(struct pci_dev *pdev)
2734{
2735        __qedi_remove(pdev, QEDI_MODE_NORMAL);
2736}
2737
2738static struct pci_device_id qedi_pci_tbl[] = {
2739        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165E) },
2740        { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8084) },
2741        { 0 },
2742};
2743MODULE_DEVICE_TABLE(pci, qedi_pci_tbl);
2744
2745static enum cpuhp_state qedi_cpuhp_state;
2746
2747static struct pci_driver qedi_pci_driver = {
2748        .name = QEDI_MODULE_NAME,
2749        .id_table = qedi_pci_tbl,
2750        .probe = qedi_probe,
2751        .remove = qedi_remove,
2752        .shutdown = qedi_shutdown,
2753};
2754
2755static int __init qedi_init(void)
2756{
2757        struct qedi_percpu_s *p;
2758        int cpu, rc = 0;
2759
2760        qedi_ops = qed_get_iscsi_ops();
2761        if (!qedi_ops) {
2762                QEDI_ERR(NULL, "Failed to get qed iSCSI operations\n");
2763                return -EINVAL;
2764        }
2765
2766#ifdef CONFIG_DEBUG_FS
2767        qedi_dbg_init("qedi");
2768#endif
2769
2770        qedi_scsi_transport = iscsi_register_transport(&qedi_iscsi_transport);
2771        if (!qedi_scsi_transport) {
2772                QEDI_ERR(NULL, "Could not register qedi transport");
2773                rc = -ENOMEM;
2774                goto exit_qedi_init_1;
2775        }
2776
2777        for_each_possible_cpu(cpu) {
2778                p = &per_cpu(qedi_percpu, cpu);
2779                INIT_LIST_HEAD(&p->work_list);
2780                spin_lock_init(&p->p_work_lock);
2781                p->iothread = NULL;
2782        }
2783
2784        rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/qedi:online",
2785                               qedi_cpu_online, qedi_cpu_offline);
2786        if (rc < 0)
2787                goto exit_qedi_init_2;
2788        qedi_cpuhp_state = rc;
2789
2790        rc = pci_register_driver(&qedi_pci_driver);
2791        if (rc) {
2792                QEDI_ERR(NULL, "Failed to register driver\n");
2793                goto exit_qedi_hp;
2794        }
2795
2796        return 0;
2797
2798exit_qedi_hp:
2799        cpuhp_remove_state(qedi_cpuhp_state);
2800exit_qedi_init_2:
2801        iscsi_unregister_transport(&qedi_iscsi_transport);
2802exit_qedi_init_1:
2803#ifdef CONFIG_DEBUG_FS
2804        qedi_dbg_exit();
2805#endif
2806        qed_put_iscsi_ops();
2807        return rc;
2808}
2809
2810static void __exit qedi_cleanup(void)
2811{
2812        pci_unregister_driver(&qedi_pci_driver);
2813        cpuhp_remove_state(qedi_cpuhp_state);
2814        iscsi_unregister_transport(&qedi_iscsi_transport);
2815
2816#ifdef CONFIG_DEBUG_FS
2817        qedi_dbg_exit();
2818#endif
2819        qed_put_iscsi_ops();
2820}
2821
2822MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx iSCSI Module");
2823MODULE_LICENSE("GPL");
2824MODULE_AUTHOR("QLogic Corporation");
2825MODULE_VERSION(QEDI_MODULE_VERSION);
2826module_init(qedi_init);
2827module_exit(qedi_cleanup);
2828