linux/drivers/scsi/qedi/qedi_iscsi.c
<<
>>
Prefs
   1/*
   2 * QLogic iSCSI Offload Driver
   3 * Copyright (c) 2016 Cavium Inc.
   4 *
   5 * This software is available under the terms of the GNU General Public License
   6 * (GPL) Version 2, available from the file COPYING in the main directory of
   7 * this source tree.
   8 */
   9
  10#include <linux/blkdev.h>
  11#include <linux/etherdevice.h>
  12#include <linux/if_ether.h>
  13#include <linux/if_vlan.h>
  14#include <scsi/scsi_tcq.h>
  15
  16#include "qedi.h"
  17#include "qedi_iscsi.h"
  18#include "qedi_gbl.h"
  19
  20int qedi_recover_all_conns(struct qedi_ctx *qedi)
  21{
  22        struct qedi_conn *qedi_conn;
  23        int i;
  24
  25        for (i = 0; i < qedi->max_active_conns; i++) {
  26                qedi_conn = qedi_get_conn_from_id(qedi, i);
  27                if (!qedi_conn)
  28                        continue;
  29
  30                qedi_start_conn_recovery(qedi, qedi_conn);
  31        }
  32
  33        return SUCCESS;
  34}
  35
  36static int qedi_eh_host_reset(struct scsi_cmnd *cmd)
  37{
  38        struct Scsi_Host *shost = cmd->device->host;
  39        struct qedi_ctx *qedi;
  40
  41        qedi = iscsi_host_priv(shost);
  42
  43        return qedi_recover_all_conns(qedi);
  44}
  45
  46struct scsi_host_template qedi_host_template = {
  47        .module = THIS_MODULE,
  48        .name = "QLogic QEDI 25/40/100Gb iSCSI Initiator Driver",
  49        .proc_name = QEDI_MODULE_NAME,
  50        .queuecommand = iscsi_queuecommand,
  51        .eh_timed_out = iscsi_eh_cmd_timed_out,
  52        .eh_abort_handler = iscsi_eh_abort,
  53        .eh_device_reset_handler = iscsi_eh_device_reset,
  54        .eh_target_reset_handler = iscsi_eh_recover_target,
  55        .eh_host_reset_handler = qedi_eh_host_reset,
  56        .target_alloc = iscsi_target_alloc,
  57        .change_queue_depth = scsi_change_queue_depth,
  58        .can_queue = QEDI_MAX_ISCSI_TASK,
  59        .this_id = -1,
  60        .sg_tablesize = QEDI_ISCSI_MAX_BDS_PER_CMD,
  61        .max_sectors = 0xffff,
  62        .dma_boundary = QEDI_HW_DMA_BOUNDARY,
  63        .cmd_per_lun = 128,
  64        .use_clustering = ENABLE_CLUSTERING,
  65        .shost_attrs = qedi_shost_attrs,
  66};
  67
  68static void qedi_conn_free_login_resources(struct qedi_ctx *qedi,
  69                                           struct qedi_conn *qedi_conn)
  70{
  71        if (qedi_conn->gen_pdu.resp_bd_tbl) {
  72                dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
  73                                  qedi_conn->gen_pdu.resp_bd_tbl,
  74                                  qedi_conn->gen_pdu.resp_bd_dma);
  75                qedi_conn->gen_pdu.resp_bd_tbl = NULL;
  76        }
  77
  78        if (qedi_conn->gen_pdu.req_bd_tbl) {
  79                dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
  80                                  qedi_conn->gen_pdu.req_bd_tbl,
  81                                  qedi_conn->gen_pdu.req_bd_dma);
  82                qedi_conn->gen_pdu.req_bd_tbl = NULL;
  83        }
  84
  85        if (qedi_conn->gen_pdu.resp_buf) {
  86                dma_free_coherent(&qedi->pdev->dev,
  87                                  ISCSI_DEF_MAX_RECV_SEG_LEN,
  88                                  qedi_conn->gen_pdu.resp_buf,
  89                                  qedi_conn->gen_pdu.resp_dma_addr);
  90                qedi_conn->gen_pdu.resp_buf = NULL;
  91        }
  92
  93        if (qedi_conn->gen_pdu.req_buf) {
  94                dma_free_coherent(&qedi->pdev->dev,
  95                                  ISCSI_DEF_MAX_RECV_SEG_LEN,
  96                                  qedi_conn->gen_pdu.req_buf,
  97                                  qedi_conn->gen_pdu.req_dma_addr);
  98                qedi_conn->gen_pdu.req_buf = NULL;
  99        }
 100}
 101
 102static int qedi_conn_alloc_login_resources(struct qedi_ctx *qedi,
 103                                           struct qedi_conn *qedi_conn)
 104{
 105        qedi_conn->gen_pdu.req_buf =
 106                dma_alloc_coherent(&qedi->pdev->dev,
 107                                   ISCSI_DEF_MAX_RECV_SEG_LEN,
 108                                   &qedi_conn->gen_pdu.req_dma_addr,
 109                                   GFP_KERNEL);
 110        if (!qedi_conn->gen_pdu.req_buf)
 111                goto login_req_buf_failure;
 112
 113        qedi_conn->gen_pdu.req_buf_size = 0;
 114        qedi_conn->gen_pdu.req_wr_ptr = qedi_conn->gen_pdu.req_buf;
 115
 116        qedi_conn->gen_pdu.resp_buf =
 117                dma_alloc_coherent(&qedi->pdev->dev,
 118                                   ISCSI_DEF_MAX_RECV_SEG_LEN,
 119                                   &qedi_conn->gen_pdu.resp_dma_addr,
 120                                   GFP_KERNEL);
 121        if (!qedi_conn->gen_pdu.resp_buf)
 122                goto login_resp_buf_failure;
 123
 124        qedi_conn->gen_pdu.resp_buf_size = ISCSI_DEF_MAX_RECV_SEG_LEN;
 125        qedi_conn->gen_pdu.resp_wr_ptr = qedi_conn->gen_pdu.resp_buf;
 126
 127        qedi_conn->gen_pdu.req_bd_tbl =
 128                dma_alloc_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
 129                                   &qedi_conn->gen_pdu.req_bd_dma, GFP_KERNEL);
 130        if (!qedi_conn->gen_pdu.req_bd_tbl)
 131                goto login_req_bd_tbl_failure;
 132
 133        qedi_conn->gen_pdu.resp_bd_tbl =
 134                dma_alloc_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
 135                                   &qedi_conn->gen_pdu.resp_bd_dma,
 136                                   GFP_KERNEL);
 137        if (!qedi_conn->gen_pdu.resp_bd_tbl)
 138                goto login_resp_bd_tbl_failure;
 139
 140        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_SESS,
 141                  "Allocation successful, cid=0x%x\n",
 142                  qedi_conn->iscsi_conn_id);
 143        return 0;
 144
 145login_resp_bd_tbl_failure:
 146        dma_free_coherent(&qedi->pdev->dev, QEDI_PAGE_SIZE,
 147                          qedi_conn->gen_pdu.req_bd_tbl,
 148                          qedi_conn->gen_pdu.req_bd_dma);
 149        qedi_conn->gen_pdu.req_bd_tbl = NULL;
 150
 151login_req_bd_tbl_failure:
 152        dma_free_coherent(&qedi->pdev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
 153                          qedi_conn->gen_pdu.resp_buf,
 154                          qedi_conn->gen_pdu.resp_dma_addr);
 155        qedi_conn->gen_pdu.resp_buf = NULL;
 156login_resp_buf_failure:
 157        dma_free_coherent(&qedi->pdev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
 158                          qedi_conn->gen_pdu.req_buf,
 159                          qedi_conn->gen_pdu.req_dma_addr);
 160        qedi_conn->gen_pdu.req_buf = NULL;
 161login_req_buf_failure:
 162        iscsi_conn_printk(KERN_ERR, qedi_conn->cls_conn->dd_data,
 163                          "login resource alloc failed!!\n");
 164        return -ENOMEM;
 165}
 166
 167static void qedi_destroy_cmd_pool(struct qedi_ctx *qedi,
 168                                  struct iscsi_session *session)
 169{
 170        int i;
 171
 172        for (i = 0; i < session->cmds_max; i++) {
 173                struct iscsi_task *task = session->cmds[i];
 174                struct qedi_cmd *cmd = task->dd_data;
 175
 176                if (cmd->io_tbl.sge_tbl)
 177                        dma_free_coherent(&qedi->pdev->dev,
 178                                          QEDI_ISCSI_MAX_BDS_PER_CMD *
 179                                          sizeof(struct scsi_sge),
 180                                          cmd->io_tbl.sge_tbl,
 181                                          cmd->io_tbl.sge_tbl_dma);
 182
 183                if (cmd->sense_buffer)
 184                        dma_free_coherent(&qedi->pdev->dev,
 185                                          SCSI_SENSE_BUFFERSIZE,
 186                                          cmd->sense_buffer,
 187                                          cmd->sense_buffer_dma);
 188        }
 189}
 190
 191static int qedi_alloc_sget(struct qedi_ctx *qedi, struct iscsi_session *session,
 192                           struct qedi_cmd *cmd)
 193{
 194        struct qedi_io_bdt *io = &cmd->io_tbl;
 195        struct scsi_sge *sge;
 196
 197        io->sge_tbl = dma_alloc_coherent(&qedi->pdev->dev,
 198                                         QEDI_ISCSI_MAX_BDS_PER_CMD *
 199                                         sizeof(*sge),
 200                                         &io->sge_tbl_dma, GFP_KERNEL);
 201        if (!io->sge_tbl) {
 202                iscsi_session_printk(KERN_ERR, session,
 203                                     "Could not allocate BD table.\n");
 204                return -ENOMEM;
 205        }
 206
 207        io->sge_valid = 0;
 208        return 0;
 209}
 210
 211static int qedi_setup_cmd_pool(struct qedi_ctx *qedi,
 212                               struct iscsi_session *session)
 213{
 214        int i;
 215
 216        for (i = 0; i < session->cmds_max; i++) {
 217                struct iscsi_task *task = session->cmds[i];
 218                struct qedi_cmd *cmd = task->dd_data;
 219
 220                task->hdr = &cmd->hdr;
 221                task->hdr_max = sizeof(struct iscsi_hdr);
 222
 223                if (qedi_alloc_sget(qedi, session, cmd))
 224                        goto free_sgets;
 225
 226                cmd->sense_buffer = dma_alloc_coherent(&qedi->pdev->dev,
 227                                                       SCSI_SENSE_BUFFERSIZE,
 228                                                       &cmd->sense_buffer_dma,
 229                                                       GFP_KERNEL);
 230                if (!cmd->sense_buffer)
 231                        goto free_sgets;
 232        }
 233
 234        return 0;
 235
 236free_sgets:
 237        qedi_destroy_cmd_pool(qedi, session);
 238        return -ENOMEM;
 239}
 240
 241static struct iscsi_cls_session *
 242qedi_session_create(struct iscsi_endpoint *ep, u16 cmds_max,
 243                    u16 qdepth, uint32_t initial_cmdsn)
 244{
 245        struct Scsi_Host *shost;
 246        struct iscsi_cls_session *cls_session;
 247        struct qedi_ctx *qedi;
 248        struct qedi_endpoint *qedi_ep;
 249
 250        if (!ep)
 251                return NULL;
 252
 253        qedi_ep = ep->dd_data;
 254        shost = qedi_ep->qedi->shost;
 255        qedi = iscsi_host_priv(shost);
 256
 257        if (cmds_max > qedi->max_sqes)
 258                cmds_max = qedi->max_sqes;
 259        else if (cmds_max < QEDI_SQ_WQES_MIN)
 260                cmds_max = QEDI_SQ_WQES_MIN;
 261
 262        cls_session = iscsi_session_setup(&qedi_iscsi_transport, shost,
 263                                          cmds_max, 0, sizeof(struct qedi_cmd),
 264                                          initial_cmdsn, ISCSI_MAX_TARGET);
 265        if (!cls_session) {
 266                QEDI_ERR(&qedi->dbg_ctx,
 267                         "Failed to setup session for ep=%p\n", qedi_ep);
 268                return NULL;
 269        }
 270
 271        if (qedi_setup_cmd_pool(qedi, cls_session->dd_data)) {
 272                QEDI_ERR(&qedi->dbg_ctx,
 273                         "Failed to setup cmd pool for ep=%p\n", qedi_ep);
 274                goto session_teardown;
 275        }
 276
 277        return cls_session;
 278
 279session_teardown:
 280        iscsi_session_teardown(cls_session);
 281        return NULL;
 282}
 283
 284static void qedi_session_destroy(struct iscsi_cls_session *cls_session)
 285{
 286        struct iscsi_session *session = cls_session->dd_data;
 287        struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
 288        struct qedi_ctx *qedi = iscsi_host_priv(shost);
 289
 290        qedi_destroy_cmd_pool(qedi, session);
 291        iscsi_session_teardown(cls_session);
 292}
 293
 294static struct iscsi_cls_conn *
 295qedi_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
 296{
 297        struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
 298        struct qedi_ctx *qedi = iscsi_host_priv(shost);
 299        struct iscsi_cls_conn *cls_conn;
 300        struct qedi_conn *qedi_conn;
 301        struct iscsi_conn *conn;
 302
 303        cls_conn = iscsi_conn_setup(cls_session, sizeof(*qedi_conn),
 304                                    cid);
 305        if (!cls_conn) {
 306                QEDI_ERR(&qedi->dbg_ctx,
 307                         "conn_new: iscsi conn setup failed, cid=0x%x, cls_sess=%p!\n",
 308                         cid, cls_session);
 309                return NULL;
 310        }
 311
 312        conn = cls_conn->dd_data;
 313        qedi_conn = conn->dd_data;
 314        qedi_conn->cls_conn = cls_conn;
 315        qedi_conn->qedi = qedi;
 316        qedi_conn->ep = NULL;
 317        qedi_conn->active_cmd_count = 0;
 318        INIT_LIST_HEAD(&qedi_conn->active_cmd_list);
 319        spin_lock_init(&qedi_conn->list_lock);
 320
 321        if (qedi_conn_alloc_login_resources(qedi, qedi_conn)) {
 322                iscsi_conn_printk(KERN_ALERT, conn,
 323                                  "conn_new: login resc alloc failed, cid=0x%x, cls_sess=%p!!\n",
 324                                   cid, cls_session);
 325                goto free_conn;
 326        }
 327
 328        return cls_conn;
 329
 330free_conn:
 331        iscsi_conn_teardown(cls_conn);
 332        return NULL;
 333}
 334
 335void qedi_mark_device_missing(struct iscsi_cls_session *cls_session)
 336{
 337        iscsi_block_session(cls_session);
 338}
 339
 340void qedi_mark_device_available(struct iscsi_cls_session *cls_session)
 341{
 342        iscsi_unblock_session(cls_session);
 343}
 344
 345static int qedi_bind_conn_to_iscsi_cid(struct qedi_ctx *qedi,
 346                                       struct qedi_conn *qedi_conn)
 347{
 348        u32 iscsi_cid = qedi_conn->iscsi_conn_id;
 349
 350        if (qedi->cid_que.conn_cid_tbl[iscsi_cid]) {
 351                iscsi_conn_printk(KERN_ALERT, qedi_conn->cls_conn->dd_data,
 352                                  "conn bind - entry #%d not free\n",
 353                                  iscsi_cid);
 354                return -EBUSY;
 355        }
 356
 357        qedi->cid_que.conn_cid_tbl[iscsi_cid] = qedi_conn;
 358        return 0;
 359}
 360
 361struct qedi_conn *qedi_get_conn_from_id(struct qedi_ctx *qedi, u32 iscsi_cid)
 362{
 363        if (!qedi->cid_que.conn_cid_tbl) {
 364                QEDI_ERR(&qedi->dbg_ctx, "missing conn<->cid table\n");
 365                return NULL;
 366
 367        } else if (iscsi_cid >= qedi->max_active_conns) {
 368                QEDI_ERR(&qedi->dbg_ctx, "wrong cid #%d\n", iscsi_cid);
 369                return NULL;
 370        }
 371        return qedi->cid_que.conn_cid_tbl[iscsi_cid];
 372}
 373
 374static int qedi_conn_bind(struct iscsi_cls_session *cls_session,
 375                          struct iscsi_cls_conn *cls_conn,
 376                          u64 transport_fd, int is_leading)
 377{
 378        struct iscsi_conn *conn = cls_conn->dd_data;
 379        struct qedi_conn *qedi_conn = conn->dd_data;
 380        struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
 381        struct qedi_ctx *qedi = iscsi_host_priv(shost);
 382        struct qedi_endpoint *qedi_ep;
 383        struct iscsi_endpoint *ep;
 384
 385        ep = iscsi_lookup_endpoint(transport_fd);
 386        if (!ep)
 387                return -EINVAL;
 388
 389        qedi_ep = ep->dd_data;
 390        if ((qedi_ep->state == EP_STATE_TCP_FIN_RCVD) ||
 391            (qedi_ep->state == EP_STATE_TCP_RST_RCVD))
 392                return -EINVAL;
 393
 394        if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
 395                return -EINVAL;
 396
 397        qedi_ep->conn = qedi_conn;
 398        qedi_conn->ep = qedi_ep;
 399        qedi_conn->iscsi_ep = ep;
 400        qedi_conn->iscsi_conn_id = qedi_ep->iscsi_cid;
 401        qedi_conn->fw_cid = qedi_ep->fw_cid;
 402        qedi_conn->cmd_cleanup_req = 0;
 403        qedi_conn->cmd_cleanup_cmpl = 0;
 404
 405        if (qedi_bind_conn_to_iscsi_cid(qedi, qedi_conn))
 406                return -EINVAL;
 407
 408        spin_lock_init(&qedi_conn->tmf_work_lock);
 409        INIT_LIST_HEAD(&qedi_conn->tmf_work_list);
 410        init_waitqueue_head(&qedi_conn->wait_queue);
 411        return 0;
 412}
 413
 414static int qedi_iscsi_update_conn(struct qedi_ctx *qedi,
 415                                  struct qedi_conn *qedi_conn)
 416{
 417        struct qed_iscsi_params_update *conn_info;
 418        struct iscsi_cls_conn *cls_conn = qedi_conn->cls_conn;
 419        struct iscsi_conn *conn = cls_conn->dd_data;
 420        struct qedi_endpoint *qedi_ep;
 421        int rval;
 422
 423        qedi_ep = qedi_conn->ep;
 424
 425        conn_info = kzalloc(sizeof(*conn_info), GFP_KERNEL);
 426        if (!conn_info) {
 427                QEDI_ERR(&qedi->dbg_ctx, "memory alloc failed\n");
 428                return -ENOMEM;
 429        }
 430
 431        conn_info->update_flag = 0;
 432
 433        if (conn->hdrdgst_en)
 434                SET_FIELD(conn_info->update_flag,
 435                          ISCSI_CONN_UPDATE_RAMROD_PARAMS_HD_EN, true);
 436        if (conn->datadgst_en)
 437                SET_FIELD(conn_info->update_flag,
 438                          ISCSI_CONN_UPDATE_RAMROD_PARAMS_DD_EN, true);
 439        if (conn->session->initial_r2t_en)
 440                SET_FIELD(conn_info->update_flag,
 441                          ISCSI_CONN_UPDATE_RAMROD_PARAMS_INITIAL_R2T,
 442                          true);
 443        if (conn->session->imm_data_en)
 444                SET_FIELD(conn_info->update_flag,
 445                          ISCSI_CONN_UPDATE_RAMROD_PARAMS_IMMEDIATE_DATA,
 446                          true);
 447
 448        conn_info->max_seq_size = conn->session->max_burst;
 449        conn_info->max_recv_pdu_length = conn->max_recv_dlength;
 450        conn_info->max_send_pdu_length = conn->max_xmit_dlength;
 451        conn_info->first_seq_length = conn->session->first_burst;
 452        conn_info->exp_stat_sn = conn->exp_statsn;
 453
 454        rval = qedi_ops->update_conn(qedi->cdev, qedi_ep->handle,
 455                                     conn_info);
 456        if (rval) {
 457                rval = -ENXIO;
 458                QEDI_ERR(&qedi->dbg_ctx, "Could not update connection\n");
 459        }
 460
 461        kfree(conn_info);
 462        return rval;
 463}
 464
 465static u16 qedi_calc_mss(u16 pmtu, u8 is_ipv6, u8 tcp_ts_en, u8 vlan_en)
 466{
 467        u16 mss = 0;
 468        u16 hdrs = TCP_HDR_LEN;
 469
 470        if (is_ipv6)
 471                hdrs += IPV6_HDR_LEN;
 472        else
 473                hdrs += IPV4_HDR_LEN;
 474
 475        mss = pmtu - hdrs;
 476
 477        if (!mss)
 478                mss = DEF_MSS;
 479
 480        return mss;
 481}
 482
 483static int qedi_iscsi_offload_conn(struct qedi_endpoint *qedi_ep)
 484{
 485        struct qedi_ctx *qedi = qedi_ep->qedi;
 486        struct qed_iscsi_params_offload *conn_info;
 487        int rval;
 488        int i;
 489
 490        conn_info = kzalloc(sizeof(*conn_info), GFP_KERNEL);
 491        if (!conn_info) {
 492                QEDI_ERR(&qedi->dbg_ctx,
 493                         "Failed to allocate memory ep=%p\n", qedi_ep);
 494                return -ENOMEM;
 495        }
 496
 497        ether_addr_copy(conn_info->src.mac, qedi_ep->src_mac);
 498        ether_addr_copy(conn_info->dst.mac, qedi_ep->dst_mac);
 499
 500        conn_info->src.ip[0] = ntohl(qedi_ep->src_addr[0]);
 501        conn_info->dst.ip[0] = ntohl(qedi_ep->dst_addr[0]);
 502
 503        if (qedi_ep->ip_type == TCP_IPV4) {
 504                conn_info->ip_version = 0;
 505                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
 506                          "After ntohl: src_addr=%pI4, dst_addr=%pI4\n",
 507                          qedi_ep->src_addr, qedi_ep->dst_addr);
 508        } else {
 509                for (i = 1; i < 4; i++) {
 510                        conn_info->src.ip[i] = ntohl(qedi_ep->src_addr[i]);
 511                        conn_info->dst.ip[i] = ntohl(qedi_ep->dst_addr[i]);
 512                }
 513
 514                conn_info->ip_version = 1;
 515                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
 516                          "After ntohl: src_addr=%pI6, dst_addr=%pI6\n",
 517                          qedi_ep->src_addr, qedi_ep->dst_addr);
 518        }
 519
 520        conn_info->src.port = qedi_ep->src_port;
 521        conn_info->dst.port = qedi_ep->dst_port;
 522
 523        conn_info->layer_code = ISCSI_SLOW_PATH_LAYER_CODE;
 524        conn_info->sq_pbl_addr = qedi_ep->sq_pbl_dma;
 525        conn_info->vlan_id = qedi_ep->vlan_id;
 526
 527        SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_TS_EN, 1);
 528        SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_DA_EN, 1);
 529        SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_DA_CNT_EN, 1);
 530        SET_FIELD(conn_info->tcp_flags, TCP_OFFLOAD_PARAMS_KA_EN, 1);
 531
 532        conn_info->default_cq = (qedi_ep->fw_cid % qedi->num_queues);
 533
 534        conn_info->ka_max_probe_cnt = DEF_KA_MAX_PROBE_COUNT;
 535        conn_info->dup_ack_theshold = 3;
 536        conn_info->rcv_wnd = 65535;
 537
 538        conn_info->ss_thresh = 65535;
 539        conn_info->srtt = 300;
 540        conn_info->rtt_var = 150;
 541        conn_info->flow_label = 0;
 542        conn_info->ka_timeout = DEF_KA_TIMEOUT;
 543        conn_info->ka_interval = DEF_KA_INTERVAL;
 544        conn_info->max_rt_time = DEF_MAX_RT_TIME;
 545        conn_info->ttl = DEF_TTL;
 546        conn_info->tos_or_tc = DEF_TOS;
 547        conn_info->remote_port = qedi_ep->dst_port;
 548        conn_info->local_port = qedi_ep->src_port;
 549
 550        conn_info->mss = qedi_calc_mss(qedi_ep->pmtu,
 551                                       (qedi_ep->ip_type == TCP_IPV6),
 552                                       1, (qedi_ep->vlan_id != 0));
 553
 554        conn_info->cwnd = DEF_MAX_CWND * conn_info->mss;
 555        conn_info->rcv_wnd_scale = 4;
 556        conn_info->da_timeout_value = 200;
 557        conn_info->ack_frequency = 2;
 558
 559        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
 560                  "Default cq index [%d], mss [%d]\n",
 561                  conn_info->default_cq, conn_info->mss);
 562
 563        rval = qedi_ops->offload_conn(qedi->cdev, qedi_ep->handle, conn_info);
 564        if (rval)
 565                QEDI_ERR(&qedi->dbg_ctx, "offload_conn returned %d, ep=%p\n",
 566                         rval, qedi_ep);
 567
 568        kfree(conn_info);
 569        return rval;
 570}
 571
 572static int qedi_conn_start(struct iscsi_cls_conn *cls_conn)
 573{
 574        struct iscsi_conn *conn = cls_conn->dd_data;
 575        struct qedi_conn *qedi_conn = conn->dd_data;
 576        struct qedi_ctx *qedi;
 577        int rval;
 578
 579        qedi = qedi_conn->qedi;
 580
 581        rval = qedi_iscsi_update_conn(qedi, qedi_conn);
 582        if (rval) {
 583                iscsi_conn_printk(KERN_ALERT, conn,
 584                                  "conn_start: FW offload conn failed.\n");
 585                rval = -EINVAL;
 586                goto start_err;
 587        }
 588
 589        clear_bit(QEDI_CONN_FW_CLEANUP, &qedi_conn->flags);
 590        qedi_conn->abrt_conn = 0;
 591
 592        rval = iscsi_conn_start(cls_conn);
 593        if (rval) {
 594                iscsi_conn_printk(KERN_ALERT, conn,
 595                                  "iscsi_conn_start: FW offload conn failed!!\n");
 596        }
 597
 598start_err:
 599        return rval;
 600}
 601
 602static void qedi_conn_destroy(struct iscsi_cls_conn *cls_conn)
 603{
 604        struct iscsi_conn *conn = cls_conn->dd_data;
 605        struct qedi_conn *qedi_conn = conn->dd_data;
 606        struct Scsi_Host *shost;
 607        struct qedi_ctx *qedi;
 608
 609        shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
 610        qedi = iscsi_host_priv(shost);
 611
 612        qedi_conn_free_login_resources(qedi, qedi_conn);
 613        iscsi_conn_teardown(cls_conn);
 614}
 615
 616static int qedi_ep_get_param(struct iscsi_endpoint *ep,
 617                             enum iscsi_param param, char *buf)
 618{
 619        struct qedi_endpoint *qedi_ep = ep->dd_data;
 620        int len;
 621
 622        if (!qedi_ep)
 623                return -ENOTCONN;
 624
 625        switch (param) {
 626        case ISCSI_PARAM_CONN_PORT:
 627                len = sprintf(buf, "%hu\n", qedi_ep->dst_port);
 628                break;
 629        case ISCSI_PARAM_CONN_ADDRESS:
 630                if (qedi_ep->ip_type == TCP_IPV4)
 631                        len = sprintf(buf, "%pI4\n", qedi_ep->dst_addr);
 632                else
 633                        len = sprintf(buf, "%pI6\n", qedi_ep->dst_addr);
 634                break;
 635        default:
 636                return -ENOTCONN;
 637        }
 638
 639        return len;
 640}
 641
 642static int qedi_host_get_param(struct Scsi_Host *shost,
 643                               enum iscsi_host_param param, char *buf)
 644{
 645        struct qedi_ctx *qedi;
 646        int len;
 647
 648        qedi = iscsi_host_priv(shost);
 649
 650        switch (param) {
 651        case ISCSI_HOST_PARAM_HWADDRESS:
 652                len = sysfs_format_mac(buf, qedi->mac, 6);
 653                break;
 654        case ISCSI_HOST_PARAM_NETDEV_NAME:
 655                len = sprintf(buf, "host%d\n", shost->host_no);
 656                break;
 657        case ISCSI_HOST_PARAM_IPADDRESS:
 658                if (qedi->ip_type == TCP_IPV4)
 659                        len = sprintf(buf, "%pI4\n", qedi->src_ip);
 660                else
 661                        len = sprintf(buf, "%pI6\n", qedi->src_ip);
 662                break;
 663        default:
 664                return iscsi_host_get_param(shost, param, buf);
 665        }
 666
 667        return len;
 668}
 669
 670static void qedi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
 671                                struct iscsi_stats *stats)
 672{
 673        struct iscsi_conn *conn = cls_conn->dd_data;
 674        struct qed_iscsi_stats iscsi_stats;
 675        struct Scsi_Host *shost;
 676        struct qedi_ctx *qedi;
 677
 678        shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
 679        qedi = iscsi_host_priv(shost);
 680        qedi_ops->get_stats(qedi->cdev, &iscsi_stats);
 681
 682        conn->txdata_octets = iscsi_stats.iscsi_tx_bytes_cnt;
 683        conn->rxdata_octets = iscsi_stats.iscsi_rx_bytes_cnt;
 684        conn->dataout_pdus_cnt = (uint32_t)iscsi_stats.iscsi_tx_data_pdu_cnt;
 685        conn->datain_pdus_cnt = (uint32_t)iscsi_stats.iscsi_rx_data_pdu_cnt;
 686        conn->r2t_pdus_cnt = (uint32_t)iscsi_stats.iscsi_rx_r2t_pdu_cnt;
 687
 688        stats->txdata_octets = conn->txdata_octets;
 689        stats->rxdata_octets = conn->rxdata_octets;
 690        stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
 691        stats->dataout_pdus = conn->dataout_pdus_cnt;
 692        stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
 693        stats->datain_pdus = conn->datain_pdus_cnt;
 694        stats->r2t_pdus = conn->r2t_pdus_cnt;
 695        stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
 696        stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
 697        stats->digest_err = 0;
 698        stats->timeout_err = 0;
 699        strcpy(stats->custom[0].desc, "eh_abort_cnt");
 700        stats->custom[0].value = conn->eh_abort_cnt;
 701        stats->custom_length = 1;
 702}
 703
 704static void qedi_iscsi_prep_generic_pdu_bd(struct qedi_conn *qedi_conn)
 705{
 706        struct scsi_sge *bd_tbl;
 707
 708        bd_tbl = (struct scsi_sge *)qedi_conn->gen_pdu.req_bd_tbl;
 709
 710        bd_tbl->sge_addr.hi =
 711                (u32)((u64)qedi_conn->gen_pdu.req_dma_addr >> 32);
 712        bd_tbl->sge_addr.lo = (u32)qedi_conn->gen_pdu.req_dma_addr;
 713        bd_tbl->sge_len = qedi_conn->gen_pdu.req_wr_ptr -
 714                                qedi_conn->gen_pdu.req_buf;
 715        bd_tbl = (struct scsi_sge  *)qedi_conn->gen_pdu.resp_bd_tbl;
 716        bd_tbl->sge_addr.hi =
 717                        (u32)((u64)qedi_conn->gen_pdu.resp_dma_addr >> 32);
 718        bd_tbl->sge_addr.lo = (u32)qedi_conn->gen_pdu.resp_dma_addr;
 719        bd_tbl->sge_len = ISCSI_DEF_MAX_RECV_SEG_LEN;
 720}
 721
 722static int qedi_iscsi_send_generic_request(struct iscsi_task *task)
 723{
 724        struct qedi_cmd *cmd = task->dd_data;
 725        struct qedi_conn *qedi_conn = cmd->conn;
 726        char *buf;
 727        int data_len;
 728        int rc = 0;
 729
 730        qedi_iscsi_prep_generic_pdu_bd(qedi_conn);
 731        switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
 732        case ISCSI_OP_LOGIN:
 733                qedi_send_iscsi_login(qedi_conn, task);
 734                break;
 735        case ISCSI_OP_NOOP_OUT:
 736                data_len = qedi_conn->gen_pdu.req_buf_size;
 737                buf = qedi_conn->gen_pdu.req_buf;
 738                if (data_len)
 739                        rc = qedi_send_iscsi_nopout(qedi_conn, task,
 740                                                    buf, data_len, 1);
 741                else
 742                        rc = qedi_send_iscsi_nopout(qedi_conn, task,
 743                                                    NULL, 0, 1);
 744                break;
 745        case ISCSI_OP_LOGOUT:
 746                rc = qedi_send_iscsi_logout(qedi_conn, task);
 747                break;
 748        case ISCSI_OP_SCSI_TMFUNC:
 749                rc = qedi_iscsi_abort_work(qedi_conn, task);
 750                break;
 751        case ISCSI_OP_TEXT:
 752                rc = qedi_send_iscsi_text(qedi_conn, task);
 753                break;
 754        default:
 755                iscsi_conn_printk(KERN_ALERT, qedi_conn->cls_conn->dd_data,
 756                                  "unsupported op 0x%x\n", task->hdr->opcode);
 757        }
 758
 759        return rc;
 760}
 761
 762static int qedi_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
 763{
 764        struct qedi_conn *qedi_conn = conn->dd_data;
 765        struct qedi_cmd *cmd = task->dd_data;
 766
 767        memset(qedi_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
 768
 769        qedi_conn->gen_pdu.req_buf_size = task->data_count;
 770
 771        if (task->data_count) {
 772                memcpy(qedi_conn->gen_pdu.req_buf, task->data,
 773                       task->data_count);
 774                qedi_conn->gen_pdu.req_wr_ptr =
 775                        qedi_conn->gen_pdu.req_buf + task->data_count;
 776        }
 777
 778        cmd->conn = conn->dd_data;
 779        cmd->scsi_cmd = NULL;
 780        return qedi_iscsi_send_generic_request(task);
 781}
 782
 783static int qedi_task_xmit(struct iscsi_task *task)
 784{
 785        struct iscsi_conn *conn = task->conn;
 786        struct qedi_conn *qedi_conn = conn->dd_data;
 787        struct qedi_cmd *cmd = task->dd_data;
 788        struct scsi_cmnd *sc = task->sc;
 789
 790        if (test_bit(QEDI_IN_SHUTDOWN, &qedi_conn->qedi->flags))
 791                return -ENODEV;
 792
 793        cmd->state = 0;
 794        cmd->task = NULL;
 795        cmd->use_slowpath = false;
 796        cmd->conn = qedi_conn;
 797        cmd->task = task;
 798        cmd->io_cmd_in_list = false;
 799        INIT_LIST_HEAD(&cmd->io_cmd);
 800
 801        if (!sc)
 802                return qedi_mtask_xmit(conn, task);
 803
 804        cmd->scsi_cmd = sc;
 805        return qedi_iscsi_send_ioreq(task);
 806}
 807
 808static struct iscsi_endpoint *
 809qedi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
 810                int non_blocking)
 811{
 812        struct qedi_ctx *qedi;
 813        struct iscsi_endpoint *ep;
 814        struct qedi_endpoint *qedi_ep;
 815        struct sockaddr_in *addr;
 816        struct sockaddr_in6 *addr6;
 817        struct iscsi_path path_req;
 818        u32 msg_type = ISCSI_KEVENT_IF_DOWN;
 819        u32 iscsi_cid = QEDI_CID_RESERVED;
 820        u16 len = 0;
 821        char *buf = NULL;
 822        int ret, tmp;
 823
 824        if (!shost) {
 825                ret = -ENXIO;
 826                QEDI_ERR(NULL, "shost is NULL\n");
 827                return ERR_PTR(ret);
 828        }
 829
 830        if (qedi_do_not_recover) {
 831                ret = -ENOMEM;
 832                return ERR_PTR(ret);
 833        }
 834
 835        qedi = iscsi_host_priv(shost);
 836
 837        if (test_bit(QEDI_IN_OFFLINE, &qedi->flags) ||
 838            test_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
 839                ret = -ENOMEM;
 840                return ERR_PTR(ret);
 841        }
 842
 843        if (atomic_read(&qedi->link_state) != QEDI_LINK_UP) {
 844                QEDI_WARN(&qedi->dbg_ctx, "qedi link down\n");
 845                return ERR_PTR(-ENXIO);
 846        }
 847
 848        ep = iscsi_create_endpoint(sizeof(struct qedi_endpoint));
 849        if (!ep) {
 850                QEDI_ERR(&qedi->dbg_ctx, "endpoint create fail\n");
 851                ret = -ENOMEM;
 852                return ERR_PTR(ret);
 853        }
 854        qedi_ep = ep->dd_data;
 855        memset(qedi_ep, 0, sizeof(struct qedi_endpoint));
 856        qedi_ep->state = EP_STATE_IDLE;
 857        qedi_ep->iscsi_cid = (u32)-1;
 858        qedi_ep->qedi = qedi;
 859
 860        if (dst_addr->sa_family == AF_INET) {
 861                addr = (struct sockaddr_in *)dst_addr;
 862                memcpy(qedi_ep->dst_addr, &addr->sin_addr.s_addr,
 863                       sizeof(struct in_addr));
 864                qedi_ep->dst_port = ntohs(addr->sin_port);
 865                qedi_ep->ip_type = TCP_IPV4;
 866                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
 867                          "dst_addr=%pI4, dst_port=%u\n",
 868                          qedi_ep->dst_addr, qedi_ep->dst_port);
 869        } else if (dst_addr->sa_family == AF_INET6) {
 870                addr6 = (struct sockaddr_in6 *)dst_addr;
 871                memcpy(qedi_ep->dst_addr, &addr6->sin6_addr,
 872                       sizeof(struct in6_addr));
 873                qedi_ep->dst_port = ntohs(addr6->sin6_port);
 874                qedi_ep->ip_type = TCP_IPV6;
 875                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
 876                          "dst_addr=%pI6, dst_port=%u\n",
 877                          qedi_ep->dst_addr, qedi_ep->dst_port);
 878        } else {
 879                QEDI_ERR(&qedi->dbg_ctx, "Invalid endpoint\n");
 880        }
 881
 882        ret = qedi_alloc_sq(qedi, qedi_ep);
 883        if (ret)
 884                goto ep_conn_exit;
 885
 886        ret = qedi_ops->acquire_conn(qedi->cdev, &qedi_ep->handle,
 887                                     &qedi_ep->fw_cid, &qedi_ep->p_doorbell);
 888
 889        if (ret) {
 890                QEDI_ERR(&qedi->dbg_ctx, "Could not acquire connection\n");
 891                ret = -ENXIO;
 892                goto ep_free_sq;
 893        }
 894
 895        iscsi_cid = qedi_ep->handle;
 896        qedi_ep->iscsi_cid = iscsi_cid;
 897
 898        init_waitqueue_head(&qedi_ep->ofld_wait);
 899        init_waitqueue_head(&qedi_ep->tcp_ofld_wait);
 900        qedi_ep->state = EP_STATE_OFLDCONN_START;
 901        qedi->ep_tbl[iscsi_cid] = qedi_ep;
 902
 903        buf = (char *)&path_req;
 904        len = sizeof(path_req);
 905        memset(&path_req, 0, len);
 906
 907        msg_type = ISCSI_KEVENT_PATH_REQ;
 908        path_req.handle = (u64)qedi_ep->iscsi_cid;
 909        path_req.pmtu = qedi->ll2_mtu;
 910        qedi_ep->pmtu = qedi->ll2_mtu;
 911        if (qedi_ep->ip_type == TCP_IPV4) {
 912                memcpy(&path_req.dst.v4_addr, &qedi_ep->dst_addr,
 913                       sizeof(struct in_addr));
 914                path_req.ip_addr_len = 4;
 915        } else {
 916                memcpy(&path_req.dst.v6_addr, &qedi_ep->dst_addr,
 917                       sizeof(struct in6_addr));
 918                path_req.ip_addr_len = 16;
 919        }
 920
 921        ret = iscsi_offload_mesg(shost, &qedi_iscsi_transport, msg_type, buf,
 922                                 len);
 923        if (ret) {
 924                QEDI_ERR(&qedi->dbg_ctx,
 925                         "iscsi_offload_mesg() failed for cid=0x%x ret=%d\n",
 926                         iscsi_cid, ret);
 927                goto ep_rel_conn;
 928        }
 929
 930        atomic_inc(&qedi->num_offloads);
 931        return ep;
 932
 933ep_rel_conn:
 934        qedi->ep_tbl[iscsi_cid] = NULL;
 935        tmp = qedi_ops->release_conn(qedi->cdev, qedi_ep->handle);
 936        if (tmp)
 937                QEDI_WARN(&qedi->dbg_ctx, "release_conn returned %d\n",
 938                          tmp);
 939ep_free_sq:
 940        qedi_free_sq(qedi, qedi_ep);
 941ep_conn_exit:
 942        iscsi_destroy_endpoint(ep);
 943        return ERR_PTR(ret);
 944}
 945
 946static int qedi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
 947{
 948        struct qedi_endpoint *qedi_ep;
 949        int ret = 0;
 950
 951        if (qedi_do_not_recover)
 952                return 1;
 953
 954        qedi_ep = ep->dd_data;
 955        if (qedi_ep->state == EP_STATE_IDLE ||
 956            qedi_ep->state == EP_STATE_OFLDCONN_NONE ||
 957            qedi_ep->state == EP_STATE_OFLDCONN_FAILED)
 958                return -1;
 959
 960        if (qedi_ep->state == EP_STATE_OFLDCONN_COMPL)
 961                ret = 1;
 962
 963        ret = wait_event_interruptible_timeout(qedi_ep->ofld_wait,
 964                                               QEDI_OFLD_WAIT_STATE(qedi_ep),
 965                                               msecs_to_jiffies(timeout_ms));
 966
 967        if (qedi_ep->state == EP_STATE_OFLDCONN_FAILED)
 968                ret = -1;
 969
 970        if (ret > 0)
 971                return 1;
 972        else if (!ret)
 973                return 0;
 974        else
 975                return ret;
 976}
 977
 978static void qedi_cleanup_active_cmd_list(struct qedi_conn *qedi_conn)
 979{
 980        struct qedi_cmd *cmd, *cmd_tmp;
 981
 982        list_for_each_entry_safe(cmd, cmd_tmp, &qedi_conn->active_cmd_list,
 983                                 io_cmd) {
 984                list_del_init(&cmd->io_cmd);
 985                qedi_conn->active_cmd_count--;
 986        }
 987}
 988
 989static void qedi_ep_disconnect(struct iscsi_endpoint *ep)
 990{
 991        struct qedi_endpoint *qedi_ep;
 992        struct qedi_conn *qedi_conn = NULL;
 993        struct iscsi_conn *conn = NULL;
 994        struct qedi_ctx *qedi;
 995        int ret = 0;
 996        int wait_delay;
 997        int abrt_conn = 0;
 998        int count = 10;
 999
1000        wait_delay = 60 * HZ + DEF_MAX_RT_TIME;
1001        qedi_ep = ep->dd_data;
1002        qedi = qedi_ep->qedi;
1003
1004        if (qedi_ep->state == EP_STATE_OFLDCONN_START)
1005                goto ep_exit_recover;
1006
1007        if (qedi_ep->state != EP_STATE_OFLDCONN_NONE)
1008                flush_work(&qedi_ep->offload_work);
1009
1010        if (qedi_ep->conn) {
1011                qedi_conn = qedi_ep->conn;
1012                conn = qedi_conn->cls_conn->dd_data;
1013                iscsi_suspend_queue(conn);
1014                abrt_conn = qedi_conn->abrt_conn;
1015
1016                while (count--) {
1017                        if (!test_bit(QEDI_CONN_FW_CLEANUP,
1018                                      &qedi_conn->flags)) {
1019                                break;
1020                        }
1021                        msleep(1000);
1022                }
1023
1024                if (test_bit(QEDI_IN_RECOVERY, &qedi->flags)) {
1025                        if (qedi_do_not_recover) {
1026                                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1027                                          "Do not recover cid=0x%x\n",
1028                                          qedi_ep->iscsi_cid);
1029                                goto ep_exit_recover;
1030                        }
1031                        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1032                                  "Reset recovery cid=0x%x, qedi_ep=%p, state=0x%x\n",
1033                                  qedi_ep->iscsi_cid, qedi_ep, qedi_ep->state);
1034                        qedi_cleanup_active_cmd_list(qedi_conn);
1035                        goto ep_release_conn;
1036                }
1037        }
1038
1039        if (qedi_do_not_recover)
1040                goto ep_exit_recover;
1041
1042        switch (qedi_ep->state) {
1043        case EP_STATE_OFLDCONN_START:
1044        case EP_STATE_OFLDCONN_NONE:
1045                goto ep_release_conn;
1046        case EP_STATE_OFLDCONN_FAILED:
1047                        break;
1048        case EP_STATE_OFLDCONN_COMPL:
1049                if (unlikely(!qedi_conn))
1050                        break;
1051
1052                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1053                          "Active cmd count=%d, abrt_conn=%d, ep state=0x%x, cid=0x%x, qedi_conn=%p\n",
1054                          qedi_conn->active_cmd_count, abrt_conn,
1055                          qedi_ep->state,
1056                          qedi_ep->iscsi_cid,
1057                          qedi_ep->conn
1058                          );
1059
1060                if (!qedi_conn->active_cmd_count)
1061                        abrt_conn = 0;
1062                else
1063                        abrt_conn = 1;
1064
1065                if (abrt_conn)
1066                        qedi_clearsq(qedi, qedi_conn, NULL);
1067                break;
1068        default:
1069                break;
1070        }
1071
1072        if (!abrt_conn)
1073                wait_delay += qedi->pf_params.iscsi_pf_params.two_msl_timer;
1074
1075        qedi_ep->state = EP_STATE_DISCONN_START;
1076        ret = qedi_ops->destroy_conn(qedi->cdev, qedi_ep->handle, abrt_conn);
1077        if (ret) {
1078                QEDI_WARN(&qedi->dbg_ctx,
1079                          "destroy_conn failed returned %d\n", ret);
1080        } else {
1081                ret = wait_event_interruptible_timeout(
1082                                        qedi_ep->tcp_ofld_wait,
1083                                        (qedi_ep->state !=
1084                                         EP_STATE_DISCONN_START),
1085                                        wait_delay);
1086                if ((ret <= 0) || (qedi_ep->state == EP_STATE_DISCONN_START)) {
1087                        QEDI_WARN(&qedi->dbg_ctx,
1088                                  "Destroy conn timedout or interrupted, ret=%d, delay=%d, cid=0x%x\n",
1089                                  ret, wait_delay, qedi_ep->iscsi_cid);
1090                }
1091        }
1092
1093ep_release_conn:
1094        ret = qedi_ops->release_conn(qedi->cdev, qedi_ep->handle);
1095        if (ret)
1096                QEDI_WARN(&qedi->dbg_ctx,
1097                          "release_conn returned %d, cid=0x%x\n",
1098                          ret, qedi_ep->iscsi_cid);
1099ep_exit_recover:
1100        qedi_ep->state = EP_STATE_IDLE;
1101        qedi->ep_tbl[qedi_ep->iscsi_cid] = NULL;
1102        qedi->cid_que.conn_cid_tbl[qedi_ep->iscsi_cid] = NULL;
1103        qedi_free_id(&qedi->lcl_port_tbl, qedi_ep->src_port);
1104        qedi_free_sq(qedi, qedi_ep);
1105
1106        if (qedi_conn)
1107                qedi_conn->ep = NULL;
1108
1109        qedi_ep->conn = NULL;
1110        qedi_ep->qedi = NULL;
1111        atomic_dec(&qedi->num_offloads);
1112
1113        iscsi_destroy_endpoint(ep);
1114}
1115
1116static int qedi_data_avail(struct qedi_ctx *qedi, u16 vlanid)
1117{
1118        struct qed_dev *cdev = qedi->cdev;
1119        struct qedi_uio_dev *udev;
1120        struct qedi_uio_ctrl *uctrl;
1121        struct sk_buff *skb;
1122        u32 len;
1123        int rc = 0;
1124
1125        udev = qedi->udev;
1126        if (!udev) {
1127                QEDI_ERR(&qedi->dbg_ctx, "udev is NULL.\n");
1128                return -EINVAL;
1129        }
1130
1131        uctrl = (struct qedi_uio_ctrl *)udev->uctrl;
1132        if (!uctrl) {
1133                QEDI_ERR(&qedi->dbg_ctx, "uctlr is NULL.\n");
1134                return -EINVAL;
1135        }
1136
1137        len = uctrl->host_tx_pkt_len;
1138        if (!len) {
1139                QEDI_ERR(&qedi->dbg_ctx, "Invalid len %u\n", len);
1140                return -EINVAL;
1141        }
1142
1143        skb = alloc_skb(len, GFP_ATOMIC);
1144        if (!skb) {
1145                QEDI_ERR(&qedi->dbg_ctx, "alloc_skb failed\n");
1146                return -EINVAL;
1147        }
1148
1149        skb_put(skb, len);
1150        memcpy(skb->data, udev->tx_pkt, len);
1151        skb->ip_summed = CHECKSUM_NONE;
1152
1153        if (vlanid)
1154                __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
1155
1156        rc = qedi_ops->ll2->start_xmit(cdev, skb, 0);
1157        if (rc) {
1158                QEDI_ERR(&qedi->dbg_ctx, "ll2 start_xmit returned %d\n",
1159                         rc);
1160                kfree_skb(skb);
1161        }
1162
1163        uctrl->host_tx_pkt_len = 0;
1164        uctrl->hw_tx_cons++;
1165
1166        return rc;
1167}
1168
1169static void qedi_offload_work(struct work_struct *work)
1170{
1171        struct qedi_endpoint *qedi_ep =
1172                container_of(work, struct qedi_endpoint, offload_work);
1173        struct qedi_ctx *qedi;
1174        int wait_delay = 5 * HZ;
1175        int ret;
1176
1177        qedi = qedi_ep->qedi;
1178
1179        ret = qedi_iscsi_offload_conn(qedi_ep);
1180        if (ret) {
1181                QEDI_ERR(&qedi->dbg_ctx,
1182                         "offload error: iscsi_cid=%u, qedi_ep=%p, ret=%d\n",
1183                         qedi_ep->iscsi_cid, qedi_ep, ret);
1184                qedi_ep->state = EP_STATE_OFLDCONN_FAILED;
1185                return;
1186        }
1187
1188        ret = wait_event_interruptible_timeout(qedi_ep->tcp_ofld_wait,
1189                                               (qedi_ep->state ==
1190                                               EP_STATE_OFLDCONN_COMPL),
1191                                               wait_delay);
1192        if ((ret <= 0) || (qedi_ep->state != EP_STATE_OFLDCONN_COMPL)) {
1193                qedi_ep->state = EP_STATE_OFLDCONN_FAILED;
1194                QEDI_ERR(&qedi->dbg_ctx,
1195                         "Offload conn TIMEOUT iscsi_cid=%u, qedi_ep=%p\n",
1196                         qedi_ep->iscsi_cid, qedi_ep);
1197        }
1198}
1199
1200static int qedi_set_path(struct Scsi_Host *shost, struct iscsi_path *path_data)
1201{
1202        struct qedi_ctx *qedi;
1203        struct qedi_endpoint *qedi_ep;
1204        int ret = 0;
1205        u32 iscsi_cid;
1206        u16 port_id = 0;
1207
1208        if (!shost) {
1209                ret = -ENXIO;
1210                QEDI_ERR(NULL, "shost is NULL\n");
1211                return ret;
1212        }
1213
1214        if (strcmp(shost->hostt->proc_name, "qedi")) {
1215                ret = -ENXIO;
1216                QEDI_ERR(NULL, "shost %s is invalid\n",
1217                         shost->hostt->proc_name);
1218                return ret;
1219        }
1220
1221        qedi = iscsi_host_priv(shost);
1222        if (path_data->handle == QEDI_PATH_HANDLE) {
1223                ret = qedi_data_avail(qedi, path_data->vlan_id);
1224                goto set_path_exit;
1225        }
1226
1227        iscsi_cid = (u32)path_data->handle;
1228        qedi_ep = qedi->ep_tbl[iscsi_cid];
1229        QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1230                  "iscsi_cid=0x%x, qedi_ep=%p\n", iscsi_cid, qedi_ep);
1231        if (!qedi_ep) {
1232                ret = -EINVAL;
1233                goto set_path_exit;
1234        }
1235
1236        if (!is_valid_ether_addr(&path_data->mac_addr[0])) {
1237                QEDI_NOTICE(&qedi->dbg_ctx, "dst mac NOT VALID\n");
1238                qedi_ep->state = EP_STATE_OFLDCONN_NONE;
1239                ret = -EIO;
1240                goto set_path_exit;
1241        }
1242
1243        ether_addr_copy(&qedi_ep->src_mac[0], &qedi->mac[0]);
1244        ether_addr_copy(&qedi_ep->dst_mac[0], &path_data->mac_addr[0]);
1245
1246        qedi_ep->vlan_id = path_data->vlan_id;
1247        if (path_data->pmtu < DEF_PATH_MTU) {
1248                qedi_ep->pmtu = qedi->ll2_mtu;
1249                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_INFO,
1250                          "MTU cannot be %u, using default MTU %u\n",
1251                           path_data->pmtu, qedi_ep->pmtu);
1252        }
1253
1254        if (path_data->pmtu != qedi->ll2_mtu) {
1255                if (path_data->pmtu > JUMBO_MTU) {
1256                        ret = -EINVAL;
1257                        QEDI_ERR(NULL, "Invalid MTU %u\n", path_data->pmtu);
1258                        goto set_path_exit;
1259                }
1260
1261                qedi_reset_host_mtu(qedi, path_data->pmtu);
1262                qedi_ep->pmtu = qedi->ll2_mtu;
1263        }
1264
1265        port_id = qedi_ep->src_port;
1266        if (port_id >= QEDI_LOCAL_PORT_MIN &&
1267            port_id < QEDI_LOCAL_PORT_MAX) {
1268                if (qedi_alloc_id(&qedi->lcl_port_tbl, port_id))
1269                        port_id = 0;
1270        } else {
1271                port_id = 0;
1272        }
1273
1274        if (!port_id) {
1275                port_id = qedi_alloc_new_id(&qedi->lcl_port_tbl);
1276                if (port_id == QEDI_LOCAL_PORT_INVALID) {
1277                        QEDI_ERR(&qedi->dbg_ctx,
1278                                 "Failed to allocate port id for iscsi_cid=0x%x\n",
1279                                 iscsi_cid);
1280                        ret = -ENOMEM;
1281                        goto set_path_exit;
1282                }
1283        }
1284
1285        qedi_ep->src_port = port_id;
1286
1287        if (qedi_ep->ip_type == TCP_IPV4) {
1288                memcpy(&qedi_ep->src_addr[0], &path_data->src.v4_addr,
1289                       sizeof(struct in_addr));
1290                memcpy(&qedi->src_ip[0], &path_data->src.v4_addr,
1291                       sizeof(struct in_addr));
1292                qedi->ip_type = TCP_IPV4;
1293
1294                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1295                          "src addr:port=%pI4:%u, dst addr:port=%pI4:%u\n",
1296                          qedi_ep->src_addr, qedi_ep->src_port,
1297                          qedi_ep->dst_addr, qedi_ep->dst_port);
1298        } else {
1299                memcpy(&qedi_ep->src_addr[0], &path_data->src.v6_addr,
1300                       sizeof(struct in6_addr));
1301                memcpy(&qedi->src_ip[0], &path_data->src.v6_addr,
1302                       sizeof(struct in6_addr));
1303                qedi->ip_type = TCP_IPV6;
1304
1305                QEDI_INFO(&qedi->dbg_ctx, QEDI_LOG_CONN,
1306                          "src addr:port=%pI6:%u, dst addr:port=%pI6:%u\n",
1307                          qedi_ep->src_addr, qedi_ep->src_port,
1308                          qedi_ep->dst_addr, qedi_ep->dst_port);
1309        }
1310
1311        INIT_WORK(&qedi_ep->offload_work, qedi_offload_work);
1312        queue_work(qedi->offload_thread, &qedi_ep->offload_work);
1313
1314        ret = 0;
1315
1316set_path_exit:
1317        return ret;
1318}
1319
1320static umode_t qedi_attr_is_visible(int param_type, int param)
1321{
1322        switch (param_type) {
1323        case ISCSI_HOST_PARAM:
1324                switch (param) {
1325                case ISCSI_HOST_PARAM_NETDEV_NAME:
1326                case ISCSI_HOST_PARAM_HWADDRESS:
1327                case ISCSI_HOST_PARAM_IPADDRESS:
1328                        return 0444;
1329                default:
1330                        return 0;
1331                }
1332        case ISCSI_PARAM:
1333                switch (param) {
1334                case ISCSI_PARAM_MAX_RECV_DLENGTH:
1335                case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1336                case ISCSI_PARAM_HDRDGST_EN:
1337                case ISCSI_PARAM_DATADGST_EN:
1338                case ISCSI_PARAM_CONN_ADDRESS:
1339                case ISCSI_PARAM_CONN_PORT:
1340                case ISCSI_PARAM_EXP_STATSN:
1341                case ISCSI_PARAM_PERSISTENT_ADDRESS:
1342                case ISCSI_PARAM_PERSISTENT_PORT:
1343                case ISCSI_PARAM_PING_TMO:
1344                case ISCSI_PARAM_RECV_TMO:
1345                case ISCSI_PARAM_INITIAL_R2T_EN:
1346                case ISCSI_PARAM_MAX_R2T:
1347                case ISCSI_PARAM_IMM_DATA_EN:
1348                case ISCSI_PARAM_FIRST_BURST:
1349                case ISCSI_PARAM_MAX_BURST:
1350                case ISCSI_PARAM_PDU_INORDER_EN:
1351                case ISCSI_PARAM_DATASEQ_INORDER_EN:
1352                case ISCSI_PARAM_ERL:
1353                case ISCSI_PARAM_TARGET_NAME:
1354                case ISCSI_PARAM_TPGT:
1355                case ISCSI_PARAM_USERNAME:
1356                case ISCSI_PARAM_PASSWORD:
1357                case ISCSI_PARAM_USERNAME_IN:
1358                case ISCSI_PARAM_PASSWORD_IN:
1359                case ISCSI_PARAM_FAST_ABORT:
1360                case ISCSI_PARAM_ABORT_TMO:
1361                case ISCSI_PARAM_LU_RESET_TMO:
1362                case ISCSI_PARAM_TGT_RESET_TMO:
1363                case ISCSI_PARAM_IFACE_NAME:
1364                case ISCSI_PARAM_INITIATOR_NAME:
1365                case ISCSI_PARAM_BOOT_ROOT:
1366                case ISCSI_PARAM_BOOT_NIC:
1367                case ISCSI_PARAM_BOOT_TARGET:
1368                        return 0444;
1369                default:
1370                        return 0;
1371                }
1372        }
1373
1374        return 0;
1375}
1376
1377static void qedi_cleanup_task(struct iscsi_task *task)
1378{
1379        if (!task->sc || task->state == ISCSI_TASK_PENDING) {
1380                QEDI_INFO(NULL, QEDI_LOG_IO, "Returning ref_cnt=%d\n",
1381                          refcount_read(&task->refcount));
1382                return;
1383        }
1384
1385        qedi_iscsi_unmap_sg_list(task->dd_data);
1386}
1387
1388struct iscsi_transport qedi_iscsi_transport = {
1389        .owner = THIS_MODULE,
1390        .name = QEDI_MODULE_NAME,
1391        .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_MULTI_R2T | CAP_DATADGST |
1392                CAP_DATA_PATH_OFFLOAD | CAP_TEXT_NEGO,
1393        .create_session = qedi_session_create,
1394        .destroy_session = qedi_session_destroy,
1395        .create_conn = qedi_conn_create,
1396        .bind_conn = qedi_conn_bind,
1397        .start_conn = qedi_conn_start,
1398        .stop_conn = iscsi_conn_stop,
1399        .destroy_conn = qedi_conn_destroy,
1400        .set_param = iscsi_set_param,
1401        .get_ep_param = qedi_ep_get_param,
1402        .get_conn_param = iscsi_conn_get_param,
1403        .get_session_param = iscsi_session_get_param,
1404        .get_host_param = qedi_host_get_param,
1405        .send_pdu = iscsi_conn_send_pdu,
1406        .get_stats = qedi_conn_get_stats,
1407        .xmit_task = qedi_task_xmit,
1408        .cleanup_task = qedi_cleanup_task,
1409        .session_recovery_timedout = iscsi_session_recovery_timedout,
1410        .ep_connect = qedi_ep_connect,
1411        .ep_poll = qedi_ep_poll,
1412        .ep_disconnect = qedi_ep_disconnect,
1413        .set_path = qedi_set_path,
1414        .attr_is_visible = qedi_attr_is_visible,
1415};
1416
1417void qedi_start_conn_recovery(struct qedi_ctx *qedi,
1418                              struct qedi_conn *qedi_conn)
1419{
1420        struct iscsi_cls_session *cls_sess;
1421        struct iscsi_cls_conn *cls_conn;
1422        struct iscsi_conn *conn;
1423
1424        cls_conn = qedi_conn->cls_conn;
1425        conn = cls_conn->dd_data;
1426        cls_sess = iscsi_conn_to_session(cls_conn);
1427
1428        if (iscsi_is_session_online(cls_sess)) {
1429                qedi_conn->abrt_conn = 1;
1430                QEDI_ERR(&qedi->dbg_ctx,
1431                         "Failing connection, state=0x%x, cid=0x%x\n",
1432                         conn->session->state, qedi_conn->iscsi_conn_id);
1433                iscsi_conn_failure(qedi_conn->cls_conn->dd_data,
1434                                   ISCSI_ERR_CONN_FAILED);
1435        }
1436}
1437
1438static const struct {
1439        enum iscsi_error_types error_code;
1440        char *err_string;
1441} qedi_iscsi_error[] = {
1442        { ISCSI_STATUS_NONE,
1443          "tcp_error none"
1444        },
1445        { ISCSI_CONN_ERROR_TASK_CID_MISMATCH,
1446          "task cid mismatch"
1447        },
1448        { ISCSI_CONN_ERROR_TASK_NOT_VALID,
1449          "invalid task"
1450        },
1451        { ISCSI_CONN_ERROR_RQ_RING_IS_FULL,
1452          "rq ring full"
1453        },
1454        { ISCSI_CONN_ERROR_CMDQ_RING_IS_FULL,
1455          "cmdq ring full"
1456        },
1457        { ISCSI_CONN_ERROR_HQE_CACHING_FAILED,
1458          "sge caching failed"
1459        },
1460        { ISCSI_CONN_ERROR_HEADER_DIGEST_ERROR,
1461          "hdr digest error"
1462        },
1463        { ISCSI_CONN_ERROR_LOCAL_COMPLETION_ERROR,
1464          "local cmpl error"
1465        },
1466        { ISCSI_CONN_ERROR_DATA_OVERRUN,
1467          "invalid task"
1468        },
1469        { ISCSI_CONN_ERROR_OUT_OF_SGES_ERROR,
1470          "out of sge error"
1471        },
1472        { ISCSI_CONN_ERROR_TCP_IP_FRAGMENT_ERROR,
1473          "tcp ip fragment error"
1474        },
1475        { ISCSI_CONN_ERROR_PROTOCOL_ERR_AHS_LEN,
1476          "AHS len protocol error"
1477        },
1478        { ISCSI_CONN_ERROR_PROTOCOL_ERR_ITT_OUT_OF_RANGE,
1479          "itt out of range error"
1480        },
1481        { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SEG_LEN_EXCEEDS_PDU_SIZE,
1482          "data seg more than pdu size"
1483        },
1484        { ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_OPCODE,
1485          "invalid opcode"
1486        },
1487        { ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_OPCODE_BEFORE_UPDATE,
1488          "invalid opcode before update"
1489        },
1490        { ISCSI_CONN_ERROR_UNVALID_NOPIN_DSL,
1491          "unexpected opcode"
1492        },
1493        { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_CARRIES_NO_DATA,
1494          "r2t carries no data"
1495        },
1496        { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SN,
1497          "data sn error"
1498        },
1499        { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_IN_TTT,
1500          "data TTT error"
1501        },
1502        { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_TTT,
1503          "r2t TTT error"
1504        },
1505        { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_BUFFER_OFFSET,
1506          "buffer offset error"
1507        },
1508        { ISCSI_CONN_ERROR_PROTOCOL_ERR_BUFFER_OFFSET_OOO,
1509          "buffer offset ooo"
1510        },
1511        { ISCSI_CONN_ERROR_PROTOCOL_ERR_R2T_SN,
1512          "data seg len 0"
1513        },
1514        { ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_0,
1515          "data xer len error"
1516        },
1517        { ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_1,
1518          "data xer len1 error"
1519        },
1520        { ISCSI_CONN_ERROR_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_2,
1521          "data xer len2 error"
1522        },
1523        { ISCSI_CONN_ERROR_PROTOCOL_ERR_LUN,
1524          "protocol lun error"
1525        },
1526        { ISCSI_CONN_ERROR_PROTOCOL_ERR_F_BIT_ZERO,
1527          "f bit zero error"
1528        },
1529        { ISCSI_CONN_ERROR_PROTOCOL_ERR_EXP_STAT_SN,
1530          "exp stat sn error"
1531        },
1532        { ISCSI_CONN_ERROR_PROTOCOL_ERR_DSL_NOT_ZERO,
1533          "dsl not zero error"
1534        },
1535        { ISCSI_CONN_ERROR_PROTOCOL_ERR_INVALID_DSL,
1536          "invalid dsl"
1537        },
1538        { ISCSI_CONN_ERROR_PROTOCOL_ERR_DATA_SEG_LEN_TOO_BIG,
1539          "data seg len too big"
1540        },
1541        { ISCSI_CONN_ERROR_PROTOCOL_ERR_OUTSTANDING_R2T_COUNT,
1542          "outstanding r2t count error"
1543        },
1544        { ISCSI_CONN_ERROR_SENSE_DATA_LENGTH,
1545          "sense datalen error"
1546        },
1547};
1548
1549char *qedi_get_iscsi_error(enum iscsi_error_types err_code)
1550{
1551        int i;
1552        char *msg = NULL;
1553
1554        for (i = 0; i < ARRAY_SIZE(qedi_iscsi_error); i++) {
1555                if (qedi_iscsi_error[i].error_code == err_code) {
1556                        msg = qedi_iscsi_error[i].err_string;
1557                        break;
1558                }
1559        }
1560        return msg;
1561}
1562
1563void qedi_process_iscsi_error(struct qedi_endpoint *ep,
1564                              struct iscsi_eqe_data *data)
1565{
1566        struct qedi_conn *qedi_conn;
1567        struct qedi_ctx *qedi;
1568        char warn_notice[] = "iscsi_warning";
1569        char error_notice[] = "iscsi_error";
1570        char unknown_msg[] = "Unknown error";
1571        char *message;
1572        int need_recovery = 0;
1573        u32 err_mask = 0;
1574        char *msg;
1575
1576        if (!ep)
1577                return;
1578
1579        qedi_conn = ep->conn;
1580        if (!qedi_conn)
1581                return;
1582
1583        qedi = ep->qedi;
1584
1585        QEDI_ERR(&qedi->dbg_ctx, "async event iscsi error:0x%x\n",
1586                 data->error_code);
1587
1588        if (err_mask) {
1589                need_recovery = 0;
1590                message = warn_notice;
1591        } else {
1592                need_recovery = 1;
1593                message = error_notice;
1594        }
1595
1596        msg = qedi_get_iscsi_error(data->error_code);
1597        if (!msg) {
1598                need_recovery = 0;
1599                msg = unknown_msg;
1600        }
1601
1602        iscsi_conn_printk(KERN_ALERT,
1603                          qedi_conn->cls_conn->dd_data,
1604                          "qedi: %s - %s\n", message, msg);
1605
1606        if (need_recovery)
1607                qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
1608}
1609
1610void qedi_clear_session_ctx(struct iscsi_cls_session *cls_sess)
1611{
1612        struct iscsi_session *session = cls_sess->dd_data;
1613        struct iscsi_conn *conn = session->leadconn;
1614        struct qedi_conn *qedi_conn = conn->dd_data;
1615
1616        if (iscsi_is_session_online(cls_sess))
1617                qedi_ep_disconnect(qedi_conn->iscsi_ep);
1618
1619        qedi_conn_destroy(qedi_conn->cls_conn);
1620
1621        qedi_session_destroy(cls_sess);
1622}
1623
1624void qedi_process_tcp_error(struct qedi_endpoint *ep,
1625                            struct iscsi_eqe_data *data)
1626{
1627        struct qedi_conn *qedi_conn;
1628
1629        if (!ep)
1630                return;
1631
1632        qedi_conn = ep->conn;
1633        if (!qedi_conn)
1634                return;
1635
1636        QEDI_ERR(&ep->qedi->dbg_ctx, "async event TCP error:0x%x\n",
1637                 data->error_code);
1638
1639        qedi_start_conn_recovery(qedi_conn->qedi, qedi_conn);
1640}
1641