Back to home page

OSCL-LXR

 
 

    


0001 /* bnx2i_hwi.c: QLogic NetXtreme II iSCSI driver.
0002  *
0003  * Copyright (c) 2006 - 2013 Broadcom Corporation
0004  * Copyright (c) 2007, 2008 Red Hat, Inc.  All rights reserved.
0005  * Copyright (c) 2007, 2008 Mike Christie
0006  * Copyright (c) 2014, QLogic Corporation
0007  *
0008  * This program is free software; you can redistribute it and/or modify
0009  * it under the terms of the GNU General Public License as published by
0010  * the Free Software Foundation.
0011  *
0012  * Written by: Anil Veerabhadrappa (anilgv@broadcom.com)
0013  * Previously Maintained by: Eddie Wai (eddie.wai@broadcom.com)
0014  * Maintained by: QLogic-Storage-Upstream@qlogic.com
0015  */
0016 
0017 #include <linux/gfp.h>
0018 #include <scsi/scsi_tcq.h>
0019 #include <scsi/libiscsi.h>
0020 #include "bnx2i.h"
0021 
0022 DECLARE_PER_CPU(struct bnx2i_percpu_s, bnx2i_percpu);
0023 
0024 /**
0025  * bnx2i_get_cid_num - get cid from ep
0026  * @ep:     endpoint pointer
0027  *
0028  * Only applicable to 57710 family of devices
0029  */
0030 static u32 bnx2i_get_cid_num(struct bnx2i_endpoint *ep)
0031 {
0032     u32 cid;
0033 
0034     if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
0035         cid = ep->ep_cid;
0036     else
0037         cid = GET_CID_NUM(ep->ep_cid);
0038     return cid;
0039 }
0040 
0041 
0042 /**
0043  * bnx2i_adjust_qp_size - Adjust SQ/RQ/CQ size for 57710 device type
0044  * @hba:        Adapter for which adjustments is to be made
0045  *
0046  * Only applicable to 57710 family of devices
0047  */
0048 static void bnx2i_adjust_qp_size(struct bnx2i_hba *hba)
0049 {
0050     u32 num_elements_per_pg;
0051 
0052     if (test_bit(BNX2I_NX2_DEV_5706, &hba->cnic_dev_type) ||
0053         test_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type) ||
0054         test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type)) {
0055         if (!is_power_of_2(hba->max_sqes))
0056             hba->max_sqes = rounddown_pow_of_two(hba->max_sqes);
0057 
0058         if (!is_power_of_2(hba->max_rqes))
0059             hba->max_rqes = rounddown_pow_of_two(hba->max_rqes);
0060     }
0061 
0062     /* Adjust each queue size if the user selection does not
0063      * yield integral num of page buffers
0064      */
0065     /* adjust SQ */
0066     num_elements_per_pg = CNIC_PAGE_SIZE / BNX2I_SQ_WQE_SIZE;
0067     if (hba->max_sqes < num_elements_per_pg)
0068         hba->max_sqes = num_elements_per_pg;
0069     else if (hba->max_sqes % num_elements_per_pg)
0070         hba->max_sqes = (hba->max_sqes + num_elements_per_pg - 1) &
0071                  ~(num_elements_per_pg - 1);
0072 
0073     /* adjust CQ */
0074     num_elements_per_pg = CNIC_PAGE_SIZE / BNX2I_CQE_SIZE;
0075     if (hba->max_cqes < num_elements_per_pg)
0076         hba->max_cqes = num_elements_per_pg;
0077     else if (hba->max_cqes % num_elements_per_pg)
0078         hba->max_cqes = (hba->max_cqes + num_elements_per_pg - 1) &
0079                  ~(num_elements_per_pg - 1);
0080 
0081     /* adjust RQ */
0082     num_elements_per_pg = CNIC_PAGE_SIZE / BNX2I_RQ_WQE_SIZE;
0083     if (hba->max_rqes < num_elements_per_pg)
0084         hba->max_rqes = num_elements_per_pg;
0085     else if (hba->max_rqes % num_elements_per_pg)
0086         hba->max_rqes = (hba->max_rqes + num_elements_per_pg - 1) &
0087                  ~(num_elements_per_pg - 1);
0088 }
0089 
0090 
0091 /**
0092  * bnx2i_get_link_state - get network interface link state
0093  * @hba:    adapter instance pointer
0094  *
0095  * updates adapter structure flag based on netdev state
0096  */
0097 static void bnx2i_get_link_state(struct bnx2i_hba *hba)
0098 {
0099     if (test_bit(__LINK_STATE_NOCARRIER, &hba->netdev->state))
0100         set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
0101     else
0102         clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
0103 }
0104 
0105 
0106 /**
0107  * bnx2i_iscsi_license_error - displays iscsi license related error message
0108  * @hba:        adapter instance pointer
0109  * @error_code:     error classification
0110  *
0111  * Puts out an error log when driver is unable to offload iscsi connection
0112  *  due to license restrictions
0113  */
0114 static void bnx2i_iscsi_license_error(struct bnx2i_hba *hba, u32 error_code)
0115 {
0116     if (error_code == ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED)
0117         /* iSCSI offload not supported on this device */
0118         printk(KERN_ERR "bnx2i: iSCSI not supported, dev=%s\n",
0119                 hba->netdev->name);
0120     if (error_code == ISCSI_KCQE_COMPLETION_STATUS_LOM_ISCSI_NOT_ENABLED)
0121         /* iSCSI offload not supported on this LOM device */
0122         printk(KERN_ERR "bnx2i: LOM is not enable to "
0123                 "offload iSCSI connections, dev=%s\n",
0124                 hba->netdev->name);
0125     set_bit(ADAPTER_STATE_INIT_FAILED, &hba->adapter_state);
0126 }
0127 
0128 
0129 /**
0130  * bnx2i_arm_cq_event_coalescing - arms CQ to enable EQ notification
0131  * @ep:     endpoint (transport identifier) structure
0132  * @action: action, ARM or DISARM. For now only ARM_CQE is used
0133  *
0134  * Arm'ing CQ will enable chip to generate global EQ events inorder to interrupt
0135  *  the driver. EQ event is generated CQ index is hit or at least 1 CQ is
0136  *  outstanding and on chip timer expires
0137  */
0138 int bnx2i_arm_cq_event_coalescing(struct bnx2i_endpoint *ep, u8 action)
0139 {
0140     struct bnx2i_5771x_cq_db *cq_db;
0141     u16 cq_index;
0142     u16 next_index = 0;
0143     u32 num_active_cmds;
0144 
0145     /* Coalesce CQ entries only on 10G devices */
0146     if (!test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
0147         return 0;
0148 
0149     /* Do not update CQ DB multiple times before firmware writes
0150      * '0xFFFF' to CQDB->SQN field. Deviation may cause spurious
0151      * interrupts and other unwanted results
0152      */
0153     cq_db = (struct bnx2i_5771x_cq_db *) ep->qp.cq_pgtbl_virt;
0154 
0155     if (action != CNIC_ARM_CQE_FP)
0156         if (cq_db->sqn[0] && cq_db->sqn[0] != 0xFFFF)
0157             return 0;
0158 
0159     if (action == CNIC_ARM_CQE || action == CNIC_ARM_CQE_FP) {
0160         num_active_cmds = atomic_read(&ep->num_active_cmds);
0161         if (num_active_cmds <= event_coal_min)
0162             next_index = 1;
0163         else {
0164             next_index = num_active_cmds >> ep->ec_shift;
0165             if (next_index > num_active_cmds - event_coal_min)
0166                 next_index = num_active_cmds - event_coal_min;
0167         }
0168         if (!next_index)
0169             next_index = 1;
0170         cq_index = ep->qp.cqe_exp_seq_sn + next_index - 1;
0171         if (cq_index > ep->qp.cqe_size * 2)
0172             cq_index -= ep->qp.cqe_size * 2;
0173         if (!cq_index)
0174             cq_index = 1;
0175 
0176         cq_db->sqn[0] = cq_index;
0177     }
0178     return next_index;
0179 }
0180 
0181 
0182 /**
0183  * bnx2i_get_rq_buf - copy RQ buffer contents to driver buffer
0184  * @bnx2i_conn:     iscsi connection on which RQ event occurred
0185  * @ptr:        driver buffer to which RQ buffer contents is to
0186  *          be copied
0187  * @len:        length of valid data inside RQ buf
0188  *
0189  * Copies RQ buffer contents from shared (DMA'able) memory region to
0190  *  driver buffer. RQ is used to DMA unsolicitated iscsi pdu's and
0191  *  scsi sense info
0192  */
0193 void bnx2i_get_rq_buf(struct bnx2i_conn *bnx2i_conn, char *ptr, int len)
0194 {
0195     if (!bnx2i_conn->ep->qp.rqe_left)
0196         return;
0197 
0198     bnx2i_conn->ep->qp.rqe_left--;
0199     memcpy(ptr, (u8 *) bnx2i_conn->ep->qp.rq_cons_qe, len);
0200     if (bnx2i_conn->ep->qp.rq_cons_qe == bnx2i_conn->ep->qp.rq_last_qe) {
0201         bnx2i_conn->ep->qp.rq_cons_qe = bnx2i_conn->ep->qp.rq_first_qe;
0202         bnx2i_conn->ep->qp.rq_cons_idx = 0;
0203     } else {
0204         bnx2i_conn->ep->qp.rq_cons_qe++;
0205         bnx2i_conn->ep->qp.rq_cons_idx++;
0206     }
0207 }
0208 
0209 
0210 static void bnx2i_ring_577xx_doorbell(struct bnx2i_conn *conn)
0211 {
0212     struct bnx2i_5771x_dbell dbell;
0213     u32 msg;
0214 
0215     memset(&dbell, 0, sizeof(dbell));
0216     dbell.dbell.header = (B577XX_ISCSI_CONNECTION_TYPE <<
0217                   B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT);
0218     msg = *((u32 *)&dbell);
0219     /* TODO : get doorbell register mapping */
0220     writel(cpu_to_le32(msg), conn->ep->qp.ctx_base);
0221 }
0222 
0223 
0224 /**
0225  * bnx2i_put_rq_buf - Replenish RQ buffer, if required ring on chip doorbell
0226  * @bnx2i_conn: iscsi connection on which event to post
0227  * @count:  number of RQ buffer being posted to chip
0228  *
0229  * No need to ring hardware doorbell for 57710 family of devices
0230  */
0231 void bnx2i_put_rq_buf(struct bnx2i_conn *bnx2i_conn, int count)
0232 {
0233     struct bnx2i_5771x_sq_rq_db *rq_db;
0234     u16 hi_bit = (bnx2i_conn->ep->qp.rq_prod_idx & 0x8000);
0235     struct bnx2i_endpoint *ep = bnx2i_conn->ep;
0236 
0237     ep->qp.rqe_left += count;
0238     ep->qp.rq_prod_idx &= 0x7FFF;
0239     ep->qp.rq_prod_idx += count;
0240 
0241     if (ep->qp.rq_prod_idx > bnx2i_conn->hba->max_rqes) {
0242         ep->qp.rq_prod_idx %= bnx2i_conn->hba->max_rqes;
0243         if (!hi_bit)
0244             ep->qp.rq_prod_idx |= 0x8000;
0245     } else
0246         ep->qp.rq_prod_idx |= hi_bit;
0247 
0248     if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type)) {
0249         rq_db = (struct bnx2i_5771x_sq_rq_db *) ep->qp.rq_pgtbl_virt;
0250         rq_db->prod_idx = ep->qp.rq_prod_idx;
0251         /* no need to ring hardware doorbell for 57710 */
0252     } else {
0253         writew(ep->qp.rq_prod_idx,
0254                ep->qp.ctx_base + CNIC_RECV_DOORBELL);
0255     }
0256 }
0257 
0258 
0259 /**
0260  * bnx2i_ring_sq_dbell - Ring SQ doorbell to wake-up the processing engine
0261  * @bnx2i_conn:     iscsi connection to which new SQ entries belong
0262  * @count:      number of SQ WQEs to post
0263  *
0264  * SQ DB is updated in host memory and TX Doorbell is rung for 57710 family
0265  *  of devices. For 5706/5708/5709 new SQ WQE count is written into the
0266  *  doorbell register
0267  */
0268 static void bnx2i_ring_sq_dbell(struct bnx2i_conn *bnx2i_conn, int count)
0269 {
0270     struct bnx2i_5771x_sq_rq_db *sq_db;
0271     struct bnx2i_endpoint *ep = bnx2i_conn->ep;
0272 
0273     atomic_inc(&ep->num_active_cmds);
0274     wmb();  /* flush SQ WQE memory before the doorbell is rung */
0275     if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type)) {
0276         sq_db = (struct bnx2i_5771x_sq_rq_db *) ep->qp.sq_pgtbl_virt;
0277         sq_db->prod_idx = ep->qp.sq_prod_idx;
0278         bnx2i_ring_577xx_doorbell(bnx2i_conn);
0279     } else
0280         writew(count, ep->qp.ctx_base + CNIC_SEND_DOORBELL);
0281 }
0282 
0283 
0284 /**
0285  * bnx2i_ring_dbell_update_sq_params - update SQ driver parameters
0286  * @bnx2i_conn: iscsi connection to which new SQ entries belong
0287  * @count:  number of SQ WQEs to post
0288  *
0289  * this routine will update SQ driver parameters and ring the doorbell
0290  */
0291 static void bnx2i_ring_dbell_update_sq_params(struct bnx2i_conn *bnx2i_conn,
0292                           int count)
0293 {
0294     int tmp_cnt;
0295 
0296     if (count == 1) {
0297         if (bnx2i_conn->ep->qp.sq_prod_qe ==
0298             bnx2i_conn->ep->qp.sq_last_qe)
0299             bnx2i_conn->ep->qp.sq_prod_qe =
0300                         bnx2i_conn->ep->qp.sq_first_qe;
0301         else
0302             bnx2i_conn->ep->qp.sq_prod_qe++;
0303     } else {
0304         if ((bnx2i_conn->ep->qp.sq_prod_qe + count) <=
0305             bnx2i_conn->ep->qp.sq_last_qe)
0306             bnx2i_conn->ep->qp.sq_prod_qe += count;
0307         else {
0308             tmp_cnt = bnx2i_conn->ep->qp.sq_last_qe -
0309                 bnx2i_conn->ep->qp.sq_prod_qe;
0310             bnx2i_conn->ep->qp.sq_prod_qe =
0311                 &bnx2i_conn->ep->qp.sq_first_qe[count -
0312                                 (tmp_cnt + 1)];
0313         }
0314     }
0315     bnx2i_conn->ep->qp.sq_prod_idx += count;
0316     /* Ring the doorbell */
0317     bnx2i_ring_sq_dbell(bnx2i_conn, bnx2i_conn->ep->qp.sq_prod_idx);
0318 }
0319 
0320 
0321 /**
0322  * bnx2i_send_iscsi_login - post iSCSI login request MP WQE to hardware
0323  * @bnx2i_conn: iscsi connection
0324  * @task: transport layer's command structure pointer which is requesting
0325  *    a WQE to sent to chip for further processing
0326  *
0327  * prepare and post an iSCSI Login request WQE to CNIC firmware
0328  */
0329 int bnx2i_send_iscsi_login(struct bnx2i_conn *bnx2i_conn,
0330                struct iscsi_task *task)
0331 {
0332     struct bnx2i_login_request *login_wqe;
0333     struct iscsi_login_req *login_hdr;
0334     u32 dword;
0335 
0336     login_hdr = (struct iscsi_login_req *)task->hdr;
0337     login_wqe = (struct bnx2i_login_request *)
0338                         bnx2i_conn->ep->qp.sq_prod_qe;
0339 
0340     login_wqe->op_code = login_hdr->opcode;
0341     login_wqe->op_attr = login_hdr->flags;
0342     login_wqe->version_max = login_hdr->max_version;
0343     login_wqe->version_min = login_hdr->min_version;
0344     login_wqe->data_length = ntoh24(login_hdr->dlength);
0345     login_wqe->isid_lo = *((u32 *) login_hdr->isid);
0346     login_wqe->isid_hi = *((u16 *) login_hdr->isid + 2);
0347     login_wqe->tsih = login_hdr->tsih;
0348     login_wqe->itt = task->itt |
0349         (ISCSI_TASK_TYPE_MPATH << ISCSI_LOGIN_REQUEST_TYPE_SHIFT);
0350     login_wqe->cid = login_hdr->cid;
0351 
0352     login_wqe->cmd_sn = be32_to_cpu(login_hdr->cmdsn);
0353     login_wqe->exp_stat_sn = be32_to_cpu(login_hdr->exp_statsn);
0354     login_wqe->flags = ISCSI_LOGIN_REQUEST_UPDATE_EXP_STAT_SN;
0355 
0356     login_wqe->resp_bd_list_addr_lo = (u32) bnx2i_conn->gen_pdu.resp_bd_dma;
0357     login_wqe->resp_bd_list_addr_hi =
0358         (u32) ((u64) bnx2i_conn->gen_pdu.resp_bd_dma >> 32);
0359 
0360     dword = ((1 << ISCSI_LOGIN_REQUEST_NUM_RESP_BDS_SHIFT) |
0361          (bnx2i_conn->gen_pdu.resp_buf_size <<
0362           ISCSI_LOGIN_REQUEST_RESP_BUFFER_LENGTH_SHIFT));
0363     login_wqe->resp_buffer = dword;
0364     login_wqe->bd_list_addr_lo = (u32) bnx2i_conn->gen_pdu.req_bd_dma;
0365     login_wqe->bd_list_addr_hi =
0366         (u32) ((u64) bnx2i_conn->gen_pdu.req_bd_dma >> 32);
0367     login_wqe->num_bds = 1;
0368     login_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
0369 
0370     bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
0371     return 0;
0372 }
0373 
0374 /**
0375  * bnx2i_send_iscsi_tmf - post iSCSI task management request MP WQE to hardware
0376  * @bnx2i_conn: iscsi connection
0377  * @mtask:  driver command structure which is requesting
0378  *      a WQE to sent to chip for further processing
0379  *
0380  * prepare and post an iSCSI Login request WQE to CNIC firmware
0381  */
0382 int bnx2i_send_iscsi_tmf(struct bnx2i_conn *bnx2i_conn,
0383              struct iscsi_task *mtask)
0384 {
0385     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
0386     struct iscsi_tm *tmfabort_hdr;
0387     struct scsi_cmnd *ref_sc;
0388     struct iscsi_task *ctask;
0389     struct bnx2i_tmf_request *tmfabort_wqe;
0390     u32 dword;
0391     u32 scsi_lun[2];
0392 
0393     tmfabort_hdr = (struct iscsi_tm *)mtask->hdr;
0394     tmfabort_wqe = (struct bnx2i_tmf_request *)
0395                         bnx2i_conn->ep->qp.sq_prod_qe;
0396 
0397     tmfabort_wqe->op_code = tmfabort_hdr->opcode;
0398     tmfabort_wqe->op_attr = tmfabort_hdr->flags;
0399 
0400     tmfabort_wqe->itt = (mtask->itt | (ISCSI_TASK_TYPE_MPATH << 14));
0401     tmfabort_wqe->reserved2 = 0;
0402     tmfabort_wqe->cmd_sn = be32_to_cpu(tmfabort_hdr->cmdsn);
0403 
0404     switch (tmfabort_hdr->flags & ISCSI_FLAG_TM_FUNC_MASK) {
0405     case ISCSI_TM_FUNC_ABORT_TASK:
0406     case ISCSI_TM_FUNC_TASK_REASSIGN:
0407         ctask = iscsi_itt_to_task(conn, tmfabort_hdr->rtt);
0408         if (!ctask || !ctask->sc)
0409             /*
0410              * the iscsi layer must have completed the cmd while
0411              * was starting up.
0412              *
0413              * Note: In the case of a SCSI cmd timeout, the task's
0414              *       sc is still active; hence ctask->sc != 0
0415              *       In this case, the task must be aborted
0416              */
0417             return 0;
0418 
0419         ref_sc = ctask->sc;
0420         if (ref_sc->sc_data_direction == DMA_TO_DEVICE)
0421             dword = (ISCSI_TASK_TYPE_WRITE <<
0422                  ISCSI_CMD_REQUEST_TYPE_SHIFT);
0423         else
0424             dword = (ISCSI_TASK_TYPE_READ <<
0425                  ISCSI_CMD_REQUEST_TYPE_SHIFT);
0426         tmfabort_wqe->ref_itt = (dword |
0427                     (tmfabort_hdr->rtt & ISCSI_ITT_MASK));
0428         break;
0429     default:
0430         tmfabort_wqe->ref_itt = RESERVED_ITT;
0431     }
0432     memcpy(scsi_lun, &tmfabort_hdr->lun, sizeof(struct scsi_lun));
0433     tmfabort_wqe->lun[0] = be32_to_cpu(scsi_lun[0]);
0434     tmfabort_wqe->lun[1] = be32_to_cpu(scsi_lun[1]);
0435 
0436     tmfabort_wqe->ref_cmd_sn = be32_to_cpu(tmfabort_hdr->refcmdsn);
0437 
0438     tmfabort_wqe->bd_list_addr_lo = (u32) bnx2i_conn->hba->mp_bd_dma;
0439     tmfabort_wqe->bd_list_addr_hi = (u32)
0440                 ((u64) bnx2i_conn->hba->mp_bd_dma >> 32);
0441     tmfabort_wqe->num_bds = 1;
0442     tmfabort_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
0443 
0444     bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
0445     return 0;
0446 }
0447 
0448 /**
0449  * bnx2i_send_iscsi_text - post iSCSI text WQE to hardware
0450  * @bnx2i_conn: iscsi connection
0451  * @mtask:  driver command structure which is requesting
0452  *      a WQE to sent to chip for further processing
0453  *
0454  * prepare and post an iSCSI Text request WQE to CNIC firmware
0455  */
0456 int bnx2i_send_iscsi_text(struct bnx2i_conn *bnx2i_conn,
0457               struct iscsi_task *mtask)
0458 {
0459     struct bnx2i_text_request *text_wqe;
0460     struct iscsi_text *text_hdr;
0461     u32 dword;
0462 
0463     text_hdr = (struct iscsi_text *)mtask->hdr;
0464     text_wqe = (struct bnx2i_text_request *) bnx2i_conn->ep->qp.sq_prod_qe;
0465 
0466     memset(text_wqe, 0, sizeof(struct bnx2i_text_request));
0467 
0468     text_wqe->op_code = text_hdr->opcode;
0469     text_wqe->op_attr = text_hdr->flags;
0470     text_wqe->data_length = ntoh24(text_hdr->dlength);
0471     text_wqe->itt = mtask->itt |
0472         (ISCSI_TASK_TYPE_MPATH << ISCSI_TEXT_REQUEST_TYPE_SHIFT);
0473     text_wqe->ttt = be32_to_cpu(text_hdr->ttt);
0474 
0475     text_wqe->cmd_sn = be32_to_cpu(text_hdr->cmdsn);
0476 
0477     text_wqe->resp_bd_list_addr_lo = (u32) bnx2i_conn->gen_pdu.resp_bd_dma;
0478     text_wqe->resp_bd_list_addr_hi =
0479             (u32) ((u64) bnx2i_conn->gen_pdu.resp_bd_dma >> 32);
0480 
0481     dword = ((1 << ISCSI_TEXT_REQUEST_NUM_RESP_BDS_SHIFT) |
0482          (bnx2i_conn->gen_pdu.resp_buf_size <<
0483           ISCSI_TEXT_REQUEST_RESP_BUFFER_LENGTH_SHIFT));
0484     text_wqe->resp_buffer = dword;
0485     text_wqe->bd_list_addr_lo = (u32) bnx2i_conn->gen_pdu.req_bd_dma;
0486     text_wqe->bd_list_addr_hi =
0487             (u32) ((u64) bnx2i_conn->gen_pdu.req_bd_dma >> 32);
0488     text_wqe->num_bds = 1;
0489     text_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
0490 
0491     bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
0492     return 0;
0493 }
0494 
0495 
0496 /**
0497  * bnx2i_send_iscsi_scsicmd - post iSCSI scsicmd request WQE to hardware
0498  * @bnx2i_conn: iscsi connection
0499  * @cmd:    driver command structure which is requesting
0500  *      a WQE to sent to chip for further processing
0501  *
0502  * prepare and post an iSCSI SCSI-CMD request WQE to CNIC firmware
0503  */
0504 int bnx2i_send_iscsi_scsicmd(struct bnx2i_conn *bnx2i_conn,
0505                  struct bnx2i_cmd *cmd)
0506 {
0507     struct bnx2i_cmd_request *scsi_cmd_wqe;
0508 
0509     scsi_cmd_wqe = (struct bnx2i_cmd_request *)
0510                         bnx2i_conn->ep->qp.sq_prod_qe;
0511     memcpy(scsi_cmd_wqe, &cmd->req, sizeof(struct bnx2i_cmd_request));
0512     scsi_cmd_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
0513 
0514     bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
0515     return 0;
0516 }
0517 
0518 /**
0519  * bnx2i_send_iscsi_nopout - post iSCSI NOPOUT request WQE to hardware
0520  * @bnx2i_conn:     iscsi connection
0521  * @task:       transport layer's command structure pointer which is
0522  *                      requesting a WQE to sent to chip for further processing
0523  * @datap:      payload buffer pointer
0524  * @data_len:       payload data length
0525  * @unsol:      indicated whether nopout pdu is unsolicited pdu or
0526  *          in response to target's NOPIN w/ TTT != FFFFFFFF
0527  *
0528  * prepare and post a nopout request WQE to CNIC firmware
0529  */
0530 int bnx2i_send_iscsi_nopout(struct bnx2i_conn *bnx2i_conn,
0531                 struct iscsi_task *task,
0532                 char *datap, int data_len, int unsol)
0533 {
0534     struct bnx2i_endpoint *ep = bnx2i_conn->ep;
0535     struct bnx2i_nop_out_request *nopout_wqe;
0536     struct iscsi_nopout *nopout_hdr;
0537 
0538     nopout_hdr = (struct iscsi_nopout *)task->hdr;
0539     nopout_wqe = (struct bnx2i_nop_out_request *)ep->qp.sq_prod_qe;
0540 
0541     memset(nopout_wqe, 0x00, sizeof(struct bnx2i_nop_out_request));
0542 
0543     nopout_wqe->op_code = nopout_hdr->opcode;
0544     nopout_wqe->op_attr = ISCSI_FLAG_CMD_FINAL;
0545     memcpy(nopout_wqe->lun, &nopout_hdr->lun, 8);
0546 
0547     /* 57710 requires LUN field to be swapped */
0548     if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
0549         swap(nopout_wqe->lun[0], nopout_wqe->lun[1]);
0550 
0551     nopout_wqe->itt = ((u16)task->itt |
0552                (ISCSI_TASK_TYPE_MPATH <<
0553                 ISCSI_TMF_REQUEST_TYPE_SHIFT));
0554     nopout_wqe->ttt = be32_to_cpu(nopout_hdr->ttt);
0555     nopout_wqe->flags = 0;
0556     if (!unsol)
0557         nopout_wqe->flags = ISCSI_NOP_OUT_REQUEST_LOCAL_COMPLETION;
0558     else if (nopout_hdr->itt == RESERVED_ITT)
0559         nopout_wqe->flags = ISCSI_NOP_OUT_REQUEST_LOCAL_COMPLETION;
0560 
0561     nopout_wqe->cmd_sn = be32_to_cpu(nopout_hdr->cmdsn);
0562     nopout_wqe->data_length = data_len;
0563     if (data_len) {
0564         /* handle payload data, not required in first release */
0565         printk(KERN_ALERT "NOPOUT: WARNING!! payload len != 0\n");
0566     } else {
0567         nopout_wqe->bd_list_addr_lo = (u32)
0568                     bnx2i_conn->hba->mp_bd_dma;
0569         nopout_wqe->bd_list_addr_hi =
0570             (u32) ((u64) bnx2i_conn->hba->mp_bd_dma >> 32);
0571         nopout_wqe->num_bds = 1;
0572     }
0573     nopout_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
0574 
0575     bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
0576     return 0;
0577 }
0578 
0579 
0580 /**
0581  * bnx2i_send_iscsi_logout - post iSCSI logout request WQE to hardware
0582  * @bnx2i_conn: iscsi connection
0583  * @task:   transport layer's command structure pointer which is
0584  *      requesting a WQE to sent to chip for further processing
0585  *
0586  * prepare and post logout request WQE to CNIC firmware
0587  */
0588 int bnx2i_send_iscsi_logout(struct bnx2i_conn *bnx2i_conn,
0589                 struct iscsi_task *task)
0590 {
0591     struct bnx2i_logout_request *logout_wqe;
0592     struct iscsi_logout *logout_hdr;
0593 
0594     logout_hdr = (struct iscsi_logout *)task->hdr;
0595 
0596     logout_wqe = (struct bnx2i_logout_request *)
0597                         bnx2i_conn->ep->qp.sq_prod_qe;
0598     memset(logout_wqe, 0x00, sizeof(struct bnx2i_logout_request));
0599 
0600     logout_wqe->op_code = logout_hdr->opcode;
0601     logout_wqe->cmd_sn = be32_to_cpu(logout_hdr->cmdsn);
0602     logout_wqe->op_attr =
0603             logout_hdr->flags | ISCSI_LOGOUT_REQUEST_ALWAYS_ONE;
0604     logout_wqe->itt = ((u16)task->itt |
0605                (ISCSI_TASK_TYPE_MPATH <<
0606                 ISCSI_LOGOUT_REQUEST_TYPE_SHIFT));
0607     logout_wqe->data_length = 0;
0608     logout_wqe->cid = 0;
0609 
0610     logout_wqe->bd_list_addr_lo = (u32) bnx2i_conn->hba->mp_bd_dma;
0611     logout_wqe->bd_list_addr_hi = (u32)
0612                 ((u64) bnx2i_conn->hba->mp_bd_dma >> 32);
0613     logout_wqe->num_bds = 1;
0614     logout_wqe->cq_index = 0; /* CQ# used for completion, 5771x only */
0615 
0616     bnx2i_conn->ep->state = EP_STATE_LOGOUT_SENT;
0617 
0618     bnx2i_ring_dbell_update_sq_params(bnx2i_conn, 1);
0619     return 0;
0620 }
0621 
0622 
0623 /**
0624  * bnx2i_update_iscsi_conn - post iSCSI logout request WQE to hardware
0625  * @conn:   iscsi connection which requires iscsi parameter update
0626  *
0627  * sends down iSCSI Conn Update request to move iSCSI conn to FFP
0628  */
0629 void bnx2i_update_iscsi_conn(struct iscsi_conn *conn)
0630 {
0631     struct bnx2i_conn *bnx2i_conn = conn->dd_data;
0632     struct bnx2i_hba *hba = bnx2i_conn->hba;
0633     struct kwqe *kwqe_arr[2];
0634     struct iscsi_kwqe_conn_update *update_wqe;
0635     struct iscsi_kwqe_conn_update conn_update_kwqe;
0636 
0637     update_wqe = &conn_update_kwqe;
0638 
0639     update_wqe->hdr.op_code = ISCSI_KWQE_OPCODE_UPDATE_CONN;
0640     update_wqe->hdr.flags =
0641         (ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
0642 
0643     /* 5771x requires conn context id to be passed as is */
0644     if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_conn->ep->hba->cnic_dev_type))
0645         update_wqe->context_id = bnx2i_conn->ep->ep_cid;
0646     else
0647         update_wqe->context_id = (bnx2i_conn->ep->ep_cid >> 7);
0648     update_wqe->conn_flags = 0;
0649     if (conn->hdrdgst_en)
0650         update_wqe->conn_flags |= ISCSI_KWQE_CONN_UPDATE_HEADER_DIGEST;
0651     if (conn->datadgst_en)
0652         update_wqe->conn_flags |= ISCSI_KWQE_CONN_UPDATE_DATA_DIGEST;
0653     if (conn->session->initial_r2t_en)
0654         update_wqe->conn_flags |= ISCSI_KWQE_CONN_UPDATE_INITIAL_R2T;
0655     if (conn->session->imm_data_en)
0656         update_wqe->conn_flags |= ISCSI_KWQE_CONN_UPDATE_IMMEDIATE_DATA;
0657 
0658     update_wqe->max_send_pdu_length = conn->max_xmit_dlength;
0659     update_wqe->max_recv_pdu_length = conn->max_recv_dlength;
0660     update_wqe->first_burst_length = conn->session->first_burst;
0661     update_wqe->max_burst_length = conn->session->max_burst;
0662     update_wqe->exp_stat_sn = conn->exp_statsn;
0663     update_wqe->max_outstanding_r2ts = conn->session->max_r2t;
0664     update_wqe->session_error_recovery_level = conn->session->erl;
0665     iscsi_conn_printk(KERN_ALERT, conn,
0666               "bnx2i: conn update - MBL 0x%x FBL 0x%x"
0667               "MRDSL_I 0x%x MRDSL_T 0x%x \n",
0668               update_wqe->max_burst_length,
0669               update_wqe->first_burst_length,
0670               update_wqe->max_recv_pdu_length,
0671               update_wqe->max_send_pdu_length);
0672 
0673     kwqe_arr[0] = (struct kwqe *) update_wqe;
0674     if (hba->cnic && hba->cnic->submit_kwqes)
0675         hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, 1);
0676 }
0677 
0678 
0679 /**
0680  * bnx2i_ep_ofld_timer - post iSCSI logout request WQE to hardware
0681  * @t:  timer context used to fetch the endpoint (transport
0682  *  handle) structure pointer
0683  *
0684  * routine to handle connection offload/destroy request timeout
0685  */
0686 void bnx2i_ep_ofld_timer(struct timer_list *t)
0687 {
0688     struct bnx2i_endpoint *ep = from_timer(ep, t, ofld_timer);
0689 
0690     if (ep->state == EP_STATE_OFLD_START) {
0691         printk(KERN_ALERT "ofld_timer: CONN_OFLD timeout\n");
0692         ep->state = EP_STATE_OFLD_FAILED;
0693     } else if (ep->state == EP_STATE_DISCONN_START) {
0694         printk(KERN_ALERT "ofld_timer: CONN_DISCON timeout\n");
0695         ep->state = EP_STATE_DISCONN_TIMEDOUT;
0696     } else if (ep->state == EP_STATE_CLEANUP_START) {
0697         printk(KERN_ALERT "ofld_timer: CONN_CLEANUP timeout\n");
0698         ep->state = EP_STATE_CLEANUP_FAILED;
0699     }
0700 
0701     wake_up_interruptible(&ep->ofld_wait);
0702 }
0703 
0704 
0705 static int bnx2i_power_of2(u32 val)
0706 {
0707     u32 power = 0;
0708     if (val & (val - 1))
0709         return power;
0710     val--;
0711     while (val) {
0712         val = val >> 1;
0713         power++;
0714     }
0715     return power;
0716 }
0717 
0718 
0719 /**
0720  * bnx2i_send_cmd_cleanup_req - send iscsi cmd context clean-up request
0721  * @hba:    adapter structure pointer
0722  * @cmd:    driver command structure which is requesting
0723  *      a WQE to sent to chip for further processing
0724  *
0725  * prepares and posts CONN_OFLD_REQ1/2 KWQE
0726  */
0727 void bnx2i_send_cmd_cleanup_req(struct bnx2i_hba *hba, struct bnx2i_cmd *cmd)
0728 {
0729     struct bnx2i_cleanup_request *cmd_cleanup;
0730 
0731     cmd_cleanup =
0732         (struct bnx2i_cleanup_request *)cmd->conn->ep->qp.sq_prod_qe;
0733     memset(cmd_cleanup, 0x00, sizeof(struct bnx2i_cleanup_request));
0734 
0735     cmd_cleanup->op_code = ISCSI_OPCODE_CLEANUP_REQUEST;
0736     cmd_cleanup->itt = cmd->req.itt;
0737     cmd_cleanup->cq_index = 0; /* CQ# used for completion, 5771x only */
0738 
0739     bnx2i_ring_dbell_update_sq_params(cmd->conn, 1);
0740 }
0741 
0742 
0743 /**
0744  * bnx2i_send_conn_destroy - initiates iscsi connection teardown process
0745  * @hba:    adapter structure pointer
0746  * @ep:     endpoint (transport identifier) structure
0747  *
0748  * this routine prepares and posts CONN_OFLD_REQ1/2 KWQE to initiate
0749  *  iscsi connection context clean-up process
0750  */
0751 int bnx2i_send_conn_destroy(struct bnx2i_hba *hba, struct bnx2i_endpoint *ep)
0752 {
0753     struct kwqe *kwqe_arr[2];
0754     struct iscsi_kwqe_conn_destroy conn_cleanup;
0755     int rc = -EINVAL;
0756 
0757     memset(&conn_cleanup, 0x00, sizeof(struct iscsi_kwqe_conn_destroy));
0758 
0759     conn_cleanup.hdr.op_code = ISCSI_KWQE_OPCODE_DESTROY_CONN;
0760     conn_cleanup.hdr.flags =
0761         (ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
0762     /* 5771x requires conn context id to be passed as is */
0763     if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
0764         conn_cleanup.context_id = ep->ep_cid;
0765     else
0766         conn_cleanup.context_id = (ep->ep_cid >> 7);
0767 
0768     conn_cleanup.reserved0 = (u16)ep->ep_iscsi_cid;
0769 
0770     kwqe_arr[0] = (struct kwqe *) &conn_cleanup;
0771     if (hba->cnic && hba->cnic->submit_kwqes)
0772         rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, 1);
0773 
0774     return rc;
0775 }
0776 
0777 
0778 /**
0779  * bnx2i_570x_send_conn_ofld_req - initiates iscsi conn context setup process
0780  * @hba:        adapter structure pointer
0781  * @ep:         endpoint (transport identifier) structure
0782  *
0783  * 5706/5708/5709 specific - prepares and posts CONN_OFLD_REQ1/2 KWQE
0784  */
0785 static int bnx2i_570x_send_conn_ofld_req(struct bnx2i_hba *hba,
0786                      struct bnx2i_endpoint *ep)
0787 {
0788     struct kwqe *kwqe_arr[2];
0789     struct iscsi_kwqe_conn_offload1 ofld_req1;
0790     struct iscsi_kwqe_conn_offload2 ofld_req2;
0791     dma_addr_t dma_addr;
0792     int num_kwqes = 2;
0793     u32 *ptbl;
0794     int rc = -EINVAL;
0795 
0796     ofld_req1.hdr.op_code = ISCSI_KWQE_OPCODE_OFFLOAD_CONN1;
0797     ofld_req1.hdr.flags =
0798         (ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
0799 
0800     ofld_req1.iscsi_conn_id = (u16) ep->ep_iscsi_cid;
0801 
0802     dma_addr = ep->qp.sq_pgtbl_phys;
0803     ofld_req1.sq_page_table_addr_lo = (u32) dma_addr;
0804     ofld_req1.sq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
0805 
0806     dma_addr = ep->qp.cq_pgtbl_phys;
0807     ofld_req1.cq_page_table_addr_lo = (u32) dma_addr;
0808     ofld_req1.cq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
0809 
0810     ofld_req2.hdr.op_code = ISCSI_KWQE_OPCODE_OFFLOAD_CONN2;
0811     ofld_req2.hdr.flags =
0812         (ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
0813 
0814     dma_addr = ep->qp.rq_pgtbl_phys;
0815     ofld_req2.rq_page_table_addr_lo = (u32) dma_addr;
0816     ofld_req2.rq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
0817 
0818     ptbl = (u32 *) ep->qp.sq_pgtbl_virt;
0819 
0820     ofld_req2.sq_first_pte.hi = *ptbl++;
0821     ofld_req2.sq_first_pte.lo = *ptbl;
0822 
0823     ptbl = (u32 *) ep->qp.cq_pgtbl_virt;
0824     ofld_req2.cq_first_pte.hi = *ptbl++;
0825     ofld_req2.cq_first_pte.lo = *ptbl;
0826 
0827     kwqe_arr[0] = (struct kwqe *) &ofld_req1;
0828     kwqe_arr[1] = (struct kwqe *) &ofld_req2;
0829     ofld_req2.num_additional_wqes = 0;
0830 
0831     if (hba->cnic && hba->cnic->submit_kwqes)
0832         rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
0833 
0834     return rc;
0835 }
0836 
0837 
0838 /**
0839  * bnx2i_5771x_send_conn_ofld_req - initiates iscsi connection context creation
0840  * @hba:        adapter structure pointer
0841  * @ep:         endpoint (transport identifier) structure
0842  *
0843  * 57710 specific - prepares and posts CONN_OFLD_REQ1/2 KWQE
0844  */
0845 static int bnx2i_5771x_send_conn_ofld_req(struct bnx2i_hba *hba,
0846                       struct bnx2i_endpoint *ep)
0847 {
0848     struct kwqe *kwqe_arr[5];
0849     struct iscsi_kwqe_conn_offload1 ofld_req1;
0850     struct iscsi_kwqe_conn_offload2 ofld_req2;
0851     struct iscsi_kwqe_conn_offload3 ofld_req3[1];
0852     dma_addr_t dma_addr;
0853     int num_kwqes = 2;
0854     u32 *ptbl;
0855     int rc = -EINVAL;
0856 
0857     ofld_req1.hdr.op_code = ISCSI_KWQE_OPCODE_OFFLOAD_CONN1;
0858     ofld_req1.hdr.flags =
0859         (ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
0860 
0861     ofld_req1.iscsi_conn_id = (u16) ep->ep_iscsi_cid;
0862 
0863     dma_addr = ep->qp.sq_pgtbl_phys + ISCSI_SQ_DB_SIZE;
0864     ofld_req1.sq_page_table_addr_lo = (u32) dma_addr;
0865     ofld_req1.sq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
0866 
0867     dma_addr = ep->qp.cq_pgtbl_phys + ISCSI_CQ_DB_SIZE;
0868     ofld_req1.cq_page_table_addr_lo = (u32) dma_addr;
0869     ofld_req1.cq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
0870 
0871     ofld_req2.hdr.op_code = ISCSI_KWQE_OPCODE_OFFLOAD_CONN2;
0872     ofld_req2.hdr.flags =
0873         (ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
0874 
0875     dma_addr = ep->qp.rq_pgtbl_phys + ISCSI_RQ_DB_SIZE;
0876     ofld_req2.rq_page_table_addr_lo = (u32) dma_addr;
0877     ofld_req2.rq_page_table_addr_hi = (u32) ((u64) dma_addr >> 32);
0878 
0879     ptbl = (u32 *)((u8 *)ep->qp.sq_pgtbl_virt + ISCSI_SQ_DB_SIZE);
0880     ofld_req2.sq_first_pte.hi = *ptbl++;
0881     ofld_req2.sq_first_pte.lo = *ptbl;
0882 
0883     ptbl = (u32 *)((u8 *)ep->qp.cq_pgtbl_virt + ISCSI_CQ_DB_SIZE);
0884     ofld_req2.cq_first_pte.hi = *ptbl++;
0885     ofld_req2.cq_first_pte.lo = *ptbl;
0886 
0887     kwqe_arr[0] = (struct kwqe *) &ofld_req1;
0888     kwqe_arr[1] = (struct kwqe *) &ofld_req2;
0889 
0890     ofld_req2.num_additional_wqes = 1;
0891     memset(ofld_req3, 0x00, sizeof(ofld_req3[0]));
0892     ptbl = (u32 *)((u8 *)ep->qp.rq_pgtbl_virt + ISCSI_RQ_DB_SIZE);
0893     ofld_req3[0].qp_first_pte[0].hi = *ptbl++;
0894     ofld_req3[0].qp_first_pte[0].lo = *ptbl;
0895 
0896     kwqe_arr[2] = (struct kwqe *) ofld_req3;
0897     /* need if we decide to go with multiple KCQE's per conn */
0898     num_kwqes += 1;
0899 
0900     if (hba->cnic && hba->cnic->submit_kwqes)
0901         rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
0902 
0903     return rc;
0904 }
0905 
0906 /**
0907  * bnx2i_send_conn_ofld_req - initiates iscsi connection context setup process
0908  *
0909  * @hba:        adapter structure pointer
0910  * @ep:         endpoint (transport identifier) structure
0911  *
0912  * this routine prepares and posts CONN_OFLD_REQ1/2 KWQE
0913  */
0914 int bnx2i_send_conn_ofld_req(struct bnx2i_hba *hba, struct bnx2i_endpoint *ep)
0915 {
0916     int rc;
0917 
0918     if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type))
0919         rc = bnx2i_5771x_send_conn_ofld_req(hba, ep);
0920     else
0921         rc = bnx2i_570x_send_conn_ofld_req(hba, ep);
0922 
0923     return rc;
0924 }
0925 
0926 
0927 /**
0928  * setup_qp_page_tables - iscsi QP page table setup function
0929  * @ep:     endpoint (transport identifier) structure
0930  *
0931  * Sets up page tables for SQ/RQ/CQ, 1G/sec (5706/5708/5709) devices requires
0932  *  64-bit address in big endian format. Whereas 10G/sec (57710) requires
0933  *  PT in little endian format
0934  */
0935 static void setup_qp_page_tables(struct bnx2i_endpoint *ep)
0936 {
0937     int num_pages;
0938     u32 *ptbl;
0939     dma_addr_t page;
0940     int cnic_dev_10g;
0941 
0942     if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type))
0943         cnic_dev_10g = 1;
0944     else
0945         cnic_dev_10g = 0;
0946 
0947     /* SQ page table */
0948     memset(ep->qp.sq_pgtbl_virt, 0, ep->qp.sq_pgtbl_size);
0949     num_pages = ep->qp.sq_mem_size / CNIC_PAGE_SIZE;
0950     page = ep->qp.sq_phys;
0951 
0952     if (cnic_dev_10g)
0953         ptbl = (u32 *)((u8 *)ep->qp.sq_pgtbl_virt + ISCSI_SQ_DB_SIZE);
0954     else
0955         ptbl = (u32 *) ep->qp.sq_pgtbl_virt;
0956     while (num_pages--) {
0957         if (cnic_dev_10g) {
0958             /* PTE is written in little endian format for 57710 */
0959             *ptbl = (u32) page;
0960             ptbl++;
0961             *ptbl = (u32) ((u64) page >> 32);
0962             ptbl++;
0963             page += CNIC_PAGE_SIZE;
0964         } else {
0965             /* PTE is written in big endian format for
0966              * 5706/5708/5709 devices */
0967             *ptbl = (u32) ((u64) page >> 32);
0968             ptbl++;
0969             *ptbl = (u32) page;
0970             ptbl++;
0971             page += CNIC_PAGE_SIZE;
0972         }
0973     }
0974 
0975     /* RQ page table */
0976     memset(ep->qp.rq_pgtbl_virt, 0, ep->qp.rq_pgtbl_size);
0977     num_pages = ep->qp.rq_mem_size / CNIC_PAGE_SIZE;
0978     page = ep->qp.rq_phys;
0979 
0980     if (cnic_dev_10g)
0981         ptbl = (u32 *)((u8 *)ep->qp.rq_pgtbl_virt + ISCSI_RQ_DB_SIZE);
0982     else
0983         ptbl = (u32 *) ep->qp.rq_pgtbl_virt;
0984     while (num_pages--) {
0985         if (cnic_dev_10g) {
0986             /* PTE is written in little endian format for 57710 */
0987             *ptbl = (u32) page;
0988             ptbl++;
0989             *ptbl = (u32) ((u64) page >> 32);
0990             ptbl++;
0991             page += CNIC_PAGE_SIZE;
0992         } else {
0993             /* PTE is written in big endian format for
0994              * 5706/5708/5709 devices */
0995             *ptbl = (u32) ((u64) page >> 32);
0996             ptbl++;
0997             *ptbl = (u32) page;
0998             ptbl++;
0999             page += CNIC_PAGE_SIZE;
1000         }
1001     }
1002 
1003     /* CQ page table */
1004     memset(ep->qp.cq_pgtbl_virt, 0, ep->qp.cq_pgtbl_size);
1005     num_pages = ep->qp.cq_mem_size / CNIC_PAGE_SIZE;
1006     page = ep->qp.cq_phys;
1007 
1008     if (cnic_dev_10g)
1009         ptbl = (u32 *)((u8 *)ep->qp.cq_pgtbl_virt + ISCSI_CQ_DB_SIZE);
1010     else
1011         ptbl = (u32 *) ep->qp.cq_pgtbl_virt;
1012     while (num_pages--) {
1013         if (cnic_dev_10g) {
1014             /* PTE is written in little endian format for 57710 */
1015             *ptbl = (u32) page;
1016             ptbl++;
1017             *ptbl = (u32) ((u64) page >> 32);
1018             ptbl++;
1019             page += CNIC_PAGE_SIZE;
1020         } else {
1021             /* PTE is written in big endian format for
1022              * 5706/5708/5709 devices */
1023             *ptbl = (u32) ((u64) page >> 32);
1024             ptbl++;
1025             *ptbl = (u32) page;
1026             ptbl++;
1027             page += CNIC_PAGE_SIZE;
1028         }
1029     }
1030 }
1031 
1032 
1033 /**
1034  * bnx2i_alloc_qp_resc - allocates required resources for QP.
1035  * @hba:    adapter structure pointer
1036  * @ep:     endpoint (transport identifier) structure
1037  *
1038  * Allocate QP (transport layer for iSCSI connection) resources, DMA'able
1039  *  memory for SQ/RQ/CQ and page tables. EP structure elements such
1040  *  as producer/consumer indexes/pointers, queue sizes and page table
1041  *  contents are setup
1042  */
1043 int bnx2i_alloc_qp_resc(struct bnx2i_hba *hba, struct bnx2i_endpoint *ep)
1044 {
1045     struct bnx2i_5771x_cq_db *cq_db;
1046 
1047     ep->hba = hba;
1048     ep->conn = NULL;
1049     ep->ep_cid = ep->ep_iscsi_cid = ep->ep_pg_cid = 0;
1050 
1051     /* Allocate page table memory for SQ which is page aligned */
1052     ep->qp.sq_mem_size = hba->max_sqes * BNX2I_SQ_WQE_SIZE;
1053     ep->qp.sq_mem_size =
1054         (ep->qp.sq_mem_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1055     ep->qp.sq_pgtbl_size =
1056         (ep->qp.sq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
1057     ep->qp.sq_pgtbl_size =
1058         (ep->qp.sq_pgtbl_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1059 
1060     ep->qp.sq_pgtbl_virt =
1061         dma_alloc_coherent(&hba->pcidev->dev, ep->qp.sq_pgtbl_size,
1062                    &ep->qp.sq_pgtbl_phys, GFP_KERNEL);
1063     if (!ep->qp.sq_pgtbl_virt) {
1064         printk(KERN_ALERT "bnx2i: unable to alloc SQ PT mem (%d)\n",
1065                   ep->qp.sq_pgtbl_size);
1066         goto mem_alloc_err;
1067     }
1068 
1069     /* Allocate memory area for actual SQ element */
1070     ep->qp.sq_virt =
1071         dma_alloc_coherent(&hba->pcidev->dev, ep->qp.sq_mem_size,
1072                    &ep->qp.sq_phys, GFP_KERNEL);
1073     if (!ep->qp.sq_virt) {
1074         printk(KERN_ALERT "bnx2i: unable to alloc SQ BD memory %d\n",
1075                   ep->qp.sq_mem_size);
1076         goto mem_alloc_err;
1077     }
1078 
1079     ep->qp.sq_first_qe = ep->qp.sq_virt;
1080     ep->qp.sq_prod_qe = ep->qp.sq_first_qe;
1081     ep->qp.sq_cons_qe = ep->qp.sq_first_qe;
1082     ep->qp.sq_last_qe = &ep->qp.sq_first_qe[hba->max_sqes - 1];
1083     ep->qp.sq_prod_idx = 0;
1084     ep->qp.sq_cons_idx = 0;
1085     ep->qp.sqe_left = hba->max_sqes;
1086 
1087     /* Allocate page table memory for CQ which is page aligned */
1088     ep->qp.cq_mem_size = hba->max_cqes * BNX2I_CQE_SIZE;
1089     ep->qp.cq_mem_size =
1090         (ep->qp.cq_mem_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1091     ep->qp.cq_pgtbl_size =
1092         (ep->qp.cq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
1093     ep->qp.cq_pgtbl_size =
1094         (ep->qp.cq_pgtbl_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1095 
1096     ep->qp.cq_pgtbl_virt =
1097         dma_alloc_coherent(&hba->pcidev->dev, ep->qp.cq_pgtbl_size,
1098                    &ep->qp.cq_pgtbl_phys, GFP_KERNEL);
1099     if (!ep->qp.cq_pgtbl_virt) {
1100         printk(KERN_ALERT "bnx2i: unable to alloc CQ PT memory %d\n",
1101                   ep->qp.cq_pgtbl_size);
1102         goto mem_alloc_err;
1103     }
1104 
1105     /* Allocate memory area for actual CQ element */
1106     ep->qp.cq_virt =
1107         dma_alloc_coherent(&hba->pcidev->dev, ep->qp.cq_mem_size,
1108                    &ep->qp.cq_phys, GFP_KERNEL);
1109     if (!ep->qp.cq_virt) {
1110         printk(KERN_ALERT "bnx2i: unable to alloc CQ BD memory %d\n",
1111                   ep->qp.cq_mem_size);
1112         goto mem_alloc_err;
1113     }
1114 
1115     ep->qp.cq_first_qe = ep->qp.cq_virt;
1116     ep->qp.cq_prod_qe = ep->qp.cq_first_qe;
1117     ep->qp.cq_cons_qe = ep->qp.cq_first_qe;
1118     ep->qp.cq_last_qe = &ep->qp.cq_first_qe[hba->max_cqes - 1];
1119     ep->qp.cq_prod_idx = 0;
1120     ep->qp.cq_cons_idx = 0;
1121     ep->qp.cqe_left = hba->max_cqes;
1122     ep->qp.cqe_exp_seq_sn = ISCSI_INITIAL_SN;
1123     ep->qp.cqe_size = hba->max_cqes;
1124 
1125     /* Invalidate all EQ CQE index, req only for 57710 */
1126     cq_db = (struct bnx2i_5771x_cq_db *) ep->qp.cq_pgtbl_virt;
1127     memset(cq_db->sqn, 0xFF, sizeof(cq_db->sqn[0]) * BNX2X_MAX_CQS);
1128 
1129     /* Allocate page table memory for RQ which is page aligned */
1130     ep->qp.rq_mem_size = hba->max_rqes * BNX2I_RQ_WQE_SIZE;
1131     ep->qp.rq_mem_size =
1132         (ep->qp.rq_mem_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1133     ep->qp.rq_pgtbl_size =
1134         (ep->qp.rq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
1135     ep->qp.rq_pgtbl_size =
1136         (ep->qp.rq_pgtbl_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
1137 
1138     ep->qp.rq_pgtbl_virt =
1139         dma_alloc_coherent(&hba->pcidev->dev, ep->qp.rq_pgtbl_size,
1140                    &ep->qp.rq_pgtbl_phys, GFP_KERNEL);
1141     if (!ep->qp.rq_pgtbl_virt) {
1142         printk(KERN_ALERT "bnx2i: unable to alloc RQ PT mem %d\n",
1143                   ep->qp.rq_pgtbl_size);
1144         goto mem_alloc_err;
1145     }
1146 
1147     /* Allocate memory area for actual RQ element */
1148     ep->qp.rq_virt =
1149         dma_alloc_coherent(&hba->pcidev->dev, ep->qp.rq_mem_size,
1150                    &ep->qp.rq_phys, GFP_KERNEL);
1151     if (!ep->qp.rq_virt) {
1152         printk(KERN_ALERT "bnx2i: unable to alloc RQ BD memory %d\n",
1153                   ep->qp.rq_mem_size);
1154         goto mem_alloc_err;
1155     }
1156 
1157     ep->qp.rq_first_qe = ep->qp.rq_virt;
1158     ep->qp.rq_prod_qe = ep->qp.rq_first_qe;
1159     ep->qp.rq_cons_qe = ep->qp.rq_first_qe;
1160     ep->qp.rq_last_qe = &ep->qp.rq_first_qe[hba->max_rqes - 1];
1161     ep->qp.rq_prod_idx = 0x8000;
1162     ep->qp.rq_cons_idx = 0;
1163     ep->qp.rqe_left = hba->max_rqes;
1164 
1165     setup_qp_page_tables(ep);
1166 
1167     return 0;
1168 
1169 mem_alloc_err:
1170     bnx2i_free_qp_resc(hba, ep);
1171     return -ENOMEM;
1172 }
1173 
1174 
1175 
1176 /**
1177  * bnx2i_free_qp_resc - free memory resources held by QP
1178  * @hba:    adapter structure pointer
1179  * @ep: endpoint (transport identifier) structure
1180  *
1181  * Free QP resources - SQ/RQ/CQ memory and page tables.
1182  */
1183 void bnx2i_free_qp_resc(struct bnx2i_hba *hba, struct bnx2i_endpoint *ep)
1184 {
1185     if (ep->qp.ctx_base) {
1186         iounmap(ep->qp.ctx_base);
1187         ep->qp.ctx_base = NULL;
1188     }
1189     /* Free SQ mem */
1190     if (ep->qp.sq_pgtbl_virt) {
1191         dma_free_coherent(&hba->pcidev->dev, ep->qp.sq_pgtbl_size,
1192                   ep->qp.sq_pgtbl_virt, ep->qp.sq_pgtbl_phys);
1193         ep->qp.sq_pgtbl_virt = NULL;
1194         ep->qp.sq_pgtbl_phys = 0;
1195     }
1196     if (ep->qp.sq_virt) {
1197         dma_free_coherent(&hba->pcidev->dev, ep->qp.sq_mem_size,
1198                   ep->qp.sq_virt, ep->qp.sq_phys);
1199         ep->qp.sq_virt = NULL;
1200         ep->qp.sq_phys = 0;
1201     }
1202 
1203     /* Free RQ mem */
1204     if (ep->qp.rq_pgtbl_virt) {
1205         dma_free_coherent(&hba->pcidev->dev, ep->qp.rq_pgtbl_size,
1206                   ep->qp.rq_pgtbl_virt, ep->qp.rq_pgtbl_phys);
1207         ep->qp.rq_pgtbl_virt = NULL;
1208         ep->qp.rq_pgtbl_phys = 0;
1209     }
1210     if (ep->qp.rq_virt) {
1211         dma_free_coherent(&hba->pcidev->dev, ep->qp.rq_mem_size,
1212                   ep->qp.rq_virt, ep->qp.rq_phys);
1213         ep->qp.rq_virt = NULL;
1214         ep->qp.rq_phys = 0;
1215     }
1216 
1217     /* Free CQ mem */
1218     if (ep->qp.cq_pgtbl_virt) {
1219         dma_free_coherent(&hba->pcidev->dev, ep->qp.cq_pgtbl_size,
1220                   ep->qp.cq_pgtbl_virt, ep->qp.cq_pgtbl_phys);
1221         ep->qp.cq_pgtbl_virt = NULL;
1222         ep->qp.cq_pgtbl_phys = 0;
1223     }
1224     if (ep->qp.cq_virt) {
1225         dma_free_coherent(&hba->pcidev->dev, ep->qp.cq_mem_size,
1226                   ep->qp.cq_virt, ep->qp.cq_phys);
1227         ep->qp.cq_virt = NULL;
1228         ep->qp.cq_phys = 0;
1229     }
1230 }
1231 
1232 
1233 /**
1234  * bnx2i_send_fw_iscsi_init_msg - initiates initial handshake with iscsi f/w
1235  * @hba:    adapter structure pointer
1236  *
1237  * Send down iscsi_init KWQEs which initiates the initial handshake with the f/w
1238  *  This results in iSCSi support validation and on-chip context manager
1239  *  initialization.  Firmware completes this handshake with a CQE carrying
1240  *  the result of iscsi support validation. Parameter carried by
1241  *  iscsi init request determines the number of offloaded connection and
1242  *  tolerance level for iscsi protocol violation this hba/chip can support
1243  */
1244 int bnx2i_send_fw_iscsi_init_msg(struct bnx2i_hba *hba)
1245 {
1246     struct kwqe *kwqe_arr[3];
1247     struct iscsi_kwqe_init1 iscsi_init;
1248     struct iscsi_kwqe_init2 iscsi_init2;
1249     int rc = 0;
1250     u64 mask64;
1251 
1252     memset(&iscsi_init, 0x00, sizeof(struct iscsi_kwqe_init1));
1253     memset(&iscsi_init2, 0x00, sizeof(struct iscsi_kwqe_init2));
1254 
1255     bnx2i_adjust_qp_size(hba);
1256 
1257     iscsi_init.flags =
1258         (CNIC_PAGE_BITS - 8) << ISCSI_KWQE_INIT1_PAGE_SIZE_SHIFT;
1259     if (en_tcp_dack)
1260         iscsi_init.flags |= ISCSI_KWQE_INIT1_DELAYED_ACK_ENABLE;
1261     iscsi_init.reserved0 = 0;
1262     iscsi_init.num_cqs = 1;
1263     iscsi_init.hdr.op_code = ISCSI_KWQE_OPCODE_INIT1;
1264     iscsi_init.hdr.flags =
1265         (ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
1266 
1267     iscsi_init.dummy_buffer_addr_lo = (u32) hba->dummy_buf_dma;
1268     iscsi_init.dummy_buffer_addr_hi =
1269         (u32) ((u64) hba->dummy_buf_dma >> 32);
1270 
1271     hba->num_ccell = hba->max_sqes >> 1;
1272     hba->ctx_ccell_tasks =
1273             ((hba->num_ccell & 0xFFFF) | (hba->max_sqes << 16));
1274     iscsi_init.num_ccells_per_conn = hba->num_ccell;
1275     iscsi_init.num_tasks_per_conn = hba->max_sqes;
1276     iscsi_init.sq_wqes_per_page = CNIC_PAGE_SIZE / BNX2I_SQ_WQE_SIZE;
1277     iscsi_init.sq_num_wqes = hba->max_sqes;
1278     iscsi_init.cq_log_wqes_per_page =
1279         (u8) bnx2i_power_of2(CNIC_PAGE_SIZE / BNX2I_CQE_SIZE);
1280     iscsi_init.cq_num_wqes = hba->max_cqes;
1281     iscsi_init.cq_num_pages = (hba->max_cqes * BNX2I_CQE_SIZE +
1282                    (CNIC_PAGE_SIZE - 1)) / CNIC_PAGE_SIZE;
1283     iscsi_init.sq_num_pages = (hba->max_sqes * BNX2I_SQ_WQE_SIZE +
1284                    (CNIC_PAGE_SIZE - 1)) / CNIC_PAGE_SIZE;
1285     iscsi_init.rq_buffer_size = BNX2I_RQ_WQE_SIZE;
1286     iscsi_init.rq_num_wqes = hba->max_rqes;
1287 
1288 
1289     iscsi_init2.hdr.op_code = ISCSI_KWQE_OPCODE_INIT2;
1290     iscsi_init2.hdr.flags =
1291         (ISCSI_KWQE_LAYER_CODE << ISCSI_KWQE_HEADER_LAYER_CODE_SHIFT);
1292     iscsi_init2.max_cq_sqn = hba->max_cqes * 2 + 1;
1293     mask64 = 0x0ULL;
1294     mask64 |= (
1295         /* CISCO MDS */
1296         (1UL <<
1297           ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_TTT_NOT_RSRV) |
1298         /* HP MSA1510i */
1299         (1UL <<
1300           ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_EXP_DATASN) |
1301         /* EMC */
1302         (1ULL << ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_LUN));
1303     if (error_mask1) {
1304         iscsi_init2.error_bit_map[0] = error_mask1;
1305         mask64 ^= (u32)(mask64);
1306         mask64 |= error_mask1;
1307     } else
1308         iscsi_init2.error_bit_map[0] = (u32) mask64;
1309 
1310     if (error_mask2) {
1311         iscsi_init2.error_bit_map[1] = error_mask2;
1312         mask64 &= 0xffffffff;
1313         mask64 |= ((u64)error_mask2 << 32);
1314     } else
1315         iscsi_init2.error_bit_map[1] = (u32) (mask64 >> 32);
1316 
1317     iscsi_error_mask = mask64;
1318 
1319     kwqe_arr[0] = (struct kwqe *) &iscsi_init;
1320     kwqe_arr[1] = (struct kwqe *) &iscsi_init2;
1321 
1322     if (hba->cnic && hba->cnic->submit_kwqes)
1323         rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, 2);
1324     return rc;
1325 }
1326 
1327 
1328 /**
1329  * bnx2i_process_scsi_cmd_resp - this function handles scsi cmd completion.
1330  * @session:    iscsi session
1331  * @bnx2i_conn: bnx2i connection
1332  * @cqe:    pointer to newly DMA'ed CQE entry for processing
1333  *
1334  * process SCSI CMD Response CQE & complete the request to SCSI-ML
1335  */
1336 int bnx2i_process_scsi_cmd_resp(struct iscsi_session *session,
1337                 struct bnx2i_conn *bnx2i_conn,
1338                 struct cqe *cqe)
1339 {
1340     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1341     struct bnx2i_hba *hba = bnx2i_conn->hba;
1342     struct bnx2i_cmd_response *resp_cqe;
1343     struct bnx2i_cmd *bnx2i_cmd;
1344     struct iscsi_task *task;
1345     struct iscsi_scsi_rsp *hdr;
1346     u32 datalen = 0;
1347 
1348     resp_cqe = (struct bnx2i_cmd_response *)cqe;
1349     spin_lock_bh(&session->back_lock);
1350     task = iscsi_itt_to_task(conn,
1351                  resp_cqe->itt & ISCSI_CMD_RESPONSE_INDEX);
1352     if (!task)
1353         goto fail;
1354 
1355     bnx2i_cmd = task->dd_data;
1356 
1357     if (bnx2i_cmd->req.op_attr & ISCSI_CMD_REQUEST_READ) {
1358         conn->datain_pdus_cnt +=
1359             resp_cqe->task_stat.read_stat.num_data_ins;
1360         conn->rxdata_octets +=
1361             bnx2i_cmd->req.total_data_transfer_length;
1362         ADD_STATS_64(hba, rx_pdus,
1363                  resp_cqe->task_stat.read_stat.num_data_ins);
1364         ADD_STATS_64(hba, rx_bytes,
1365                  bnx2i_cmd->req.total_data_transfer_length);
1366     } else {
1367         conn->dataout_pdus_cnt +=
1368             resp_cqe->task_stat.write_stat.num_data_outs;
1369         conn->r2t_pdus_cnt +=
1370             resp_cqe->task_stat.write_stat.num_r2ts;
1371         conn->txdata_octets +=
1372             bnx2i_cmd->req.total_data_transfer_length;
1373         ADD_STATS_64(hba, tx_pdus,
1374                  resp_cqe->task_stat.write_stat.num_data_outs);
1375         ADD_STATS_64(hba, tx_bytes,
1376                  bnx2i_cmd->req.total_data_transfer_length);
1377         ADD_STATS_64(hba, rx_pdus,
1378                  resp_cqe->task_stat.write_stat.num_r2ts);
1379     }
1380     bnx2i_iscsi_unmap_sg_list(bnx2i_cmd);
1381 
1382     hdr = (struct iscsi_scsi_rsp *)task->hdr;
1383     resp_cqe = (struct bnx2i_cmd_response *)cqe;
1384     hdr->opcode = resp_cqe->op_code;
1385     hdr->max_cmdsn = cpu_to_be32(resp_cqe->max_cmd_sn);
1386     hdr->exp_cmdsn = cpu_to_be32(resp_cqe->exp_cmd_sn);
1387     hdr->response = resp_cqe->response;
1388     hdr->cmd_status = resp_cqe->status;
1389     hdr->flags = resp_cqe->response_flags;
1390     hdr->residual_count = cpu_to_be32(resp_cqe->residual_count);
1391 
1392     if (resp_cqe->op_code == ISCSI_OP_SCSI_DATA_IN)
1393         goto done;
1394 
1395     if (resp_cqe->status == SAM_STAT_CHECK_CONDITION) {
1396         datalen = resp_cqe->data_length;
1397         if (datalen < 2)
1398             goto done;
1399 
1400         if (datalen > BNX2I_RQ_WQE_SIZE) {
1401             iscsi_conn_printk(KERN_ERR, conn,
1402                       "sense data len %d > RQ sz\n",
1403                       datalen);
1404             datalen = BNX2I_RQ_WQE_SIZE;
1405         } else if (datalen > ISCSI_DEF_MAX_RECV_SEG_LEN) {
1406             iscsi_conn_printk(KERN_ERR, conn,
1407                       "sense data len %d > conn data\n",
1408                       datalen);
1409             datalen = ISCSI_DEF_MAX_RECV_SEG_LEN;
1410         }
1411 
1412         bnx2i_get_rq_buf(bnx2i_cmd->conn, conn->data, datalen);
1413         bnx2i_put_rq_buf(bnx2i_cmd->conn, 1);
1414     }
1415 
1416 done:
1417     __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr,
1418                  conn->data, datalen);
1419 fail:
1420     spin_unlock_bh(&session->back_lock);
1421     return 0;
1422 }
1423 
1424 
1425 /**
1426  * bnx2i_process_login_resp - this function handles iscsi login response
1427  * @session:        iscsi session pointer
1428  * @bnx2i_conn:     iscsi connection pointer
1429  * @cqe:        pointer to newly DMA'ed CQE entry for processing
1430  *
1431  * process Login Response CQE & complete it to open-iscsi user daemon
1432  */
1433 static int bnx2i_process_login_resp(struct iscsi_session *session,
1434                     struct bnx2i_conn *bnx2i_conn,
1435                     struct cqe *cqe)
1436 {
1437     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1438     struct iscsi_task *task;
1439     struct bnx2i_login_response *login;
1440     struct iscsi_login_rsp *resp_hdr;
1441     int pld_len;
1442     int pad_len;
1443 
1444     login = (struct bnx2i_login_response *) cqe;
1445     spin_lock(&session->back_lock);
1446     task = iscsi_itt_to_task(conn,
1447                  login->itt & ISCSI_LOGIN_RESPONSE_INDEX);
1448     if (!task)
1449         goto done;
1450 
1451     resp_hdr = (struct iscsi_login_rsp *) &bnx2i_conn->gen_pdu.resp_hdr;
1452     memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1453     resp_hdr->opcode = login->op_code;
1454     resp_hdr->flags = login->response_flags;
1455     resp_hdr->max_version = login->version_max;
1456     resp_hdr->active_version = login->version_active;
1457     resp_hdr->hlength = 0;
1458 
1459     hton24(resp_hdr->dlength, login->data_length);
1460     memcpy(resp_hdr->isid, &login->isid_lo, 6);
1461     resp_hdr->tsih = cpu_to_be16(login->tsih);
1462     resp_hdr->itt = task->hdr->itt;
1463     resp_hdr->statsn = cpu_to_be32(login->stat_sn);
1464     resp_hdr->exp_cmdsn = cpu_to_be32(login->exp_cmd_sn);
1465     resp_hdr->max_cmdsn = cpu_to_be32(login->max_cmd_sn);
1466     resp_hdr->status_class = login->status_class;
1467     resp_hdr->status_detail = login->status_detail;
1468     pld_len = login->data_length;
1469     bnx2i_conn->gen_pdu.resp_wr_ptr =
1470                     bnx2i_conn->gen_pdu.resp_buf + pld_len;
1471 
1472     pad_len = 0;
1473     if (pld_len & 0x3)
1474         pad_len = 4 - (pld_len % 4);
1475 
1476     if (pad_len) {
1477         int i = 0;
1478         for (i = 0; i < pad_len; i++) {
1479             bnx2i_conn->gen_pdu.resp_wr_ptr[0] = 0;
1480             bnx2i_conn->gen_pdu.resp_wr_ptr++;
1481         }
1482     }
1483 
1484     __iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr,
1485         bnx2i_conn->gen_pdu.resp_buf,
1486         bnx2i_conn->gen_pdu.resp_wr_ptr - bnx2i_conn->gen_pdu.resp_buf);
1487 done:
1488     spin_unlock(&session->back_lock);
1489     return 0;
1490 }
1491 
1492 
1493 /**
1494  * bnx2i_process_text_resp - this function handles iscsi text response
1495  * @session:    iscsi session pointer
1496  * @bnx2i_conn: iscsi connection pointer
1497  * @cqe:    pointer to newly DMA'ed CQE entry for processing
1498  *
1499  * process iSCSI Text Response CQE&  complete it to open-iscsi user daemon
1500  */
1501 static int bnx2i_process_text_resp(struct iscsi_session *session,
1502                    struct bnx2i_conn *bnx2i_conn,
1503                    struct cqe *cqe)
1504 {
1505     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1506     struct iscsi_task *task;
1507     struct bnx2i_text_response *text;
1508     struct iscsi_text_rsp *resp_hdr;
1509     int pld_len;
1510     int pad_len;
1511 
1512     text = (struct bnx2i_text_response *) cqe;
1513     spin_lock(&session->back_lock);
1514     task = iscsi_itt_to_task(conn, text->itt & ISCSI_LOGIN_RESPONSE_INDEX);
1515     if (!task)
1516         goto done;
1517 
1518     resp_hdr = (struct iscsi_text_rsp *)&bnx2i_conn->gen_pdu.resp_hdr;
1519     memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1520     resp_hdr->opcode = text->op_code;
1521     resp_hdr->flags = text->response_flags;
1522     resp_hdr->hlength = 0;
1523 
1524     hton24(resp_hdr->dlength, text->data_length);
1525     resp_hdr->itt = task->hdr->itt;
1526     resp_hdr->ttt = cpu_to_be32(text->ttt);
1527     resp_hdr->statsn = task->hdr->exp_statsn;
1528     resp_hdr->exp_cmdsn = cpu_to_be32(text->exp_cmd_sn);
1529     resp_hdr->max_cmdsn = cpu_to_be32(text->max_cmd_sn);
1530     pld_len = text->data_length;
1531     bnx2i_conn->gen_pdu.resp_wr_ptr = bnx2i_conn->gen_pdu.resp_buf +
1532                       pld_len;
1533     pad_len = 0;
1534     if (pld_len & 0x3)
1535         pad_len = 4 - (pld_len % 4);
1536 
1537     if (pad_len) {
1538         int i = 0;
1539         for (i = 0; i < pad_len; i++) {
1540             bnx2i_conn->gen_pdu.resp_wr_ptr[0] = 0;
1541             bnx2i_conn->gen_pdu.resp_wr_ptr++;
1542         }
1543     }
1544     __iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr,
1545                  bnx2i_conn->gen_pdu.resp_buf,
1546                  bnx2i_conn->gen_pdu.resp_wr_ptr -
1547                  bnx2i_conn->gen_pdu.resp_buf);
1548 done:
1549     spin_unlock(&session->back_lock);
1550     return 0;
1551 }
1552 
1553 
1554 /**
1555  * bnx2i_process_tmf_resp - this function handles iscsi TMF response
1556  * @session:        iscsi session pointer
1557  * @bnx2i_conn:     iscsi connection pointer
1558  * @cqe:        pointer to newly DMA'ed CQE entry for processing
1559  *
1560  * process iSCSI TMF Response CQE and wake up the driver eh thread.
1561  */
1562 static int bnx2i_process_tmf_resp(struct iscsi_session *session,
1563                   struct bnx2i_conn *bnx2i_conn,
1564                   struct cqe *cqe)
1565 {
1566     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1567     struct iscsi_task *task;
1568     struct bnx2i_tmf_response *tmf_cqe;
1569     struct iscsi_tm_rsp *resp_hdr;
1570 
1571     tmf_cqe = (struct bnx2i_tmf_response *)cqe;
1572     spin_lock(&session->back_lock);
1573     task = iscsi_itt_to_task(conn,
1574                  tmf_cqe->itt & ISCSI_TMF_RESPONSE_INDEX);
1575     if (!task)
1576         goto done;
1577 
1578     resp_hdr = (struct iscsi_tm_rsp *) &bnx2i_conn->gen_pdu.resp_hdr;
1579     memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1580     resp_hdr->opcode = tmf_cqe->op_code;
1581     resp_hdr->max_cmdsn = cpu_to_be32(tmf_cqe->max_cmd_sn);
1582     resp_hdr->exp_cmdsn = cpu_to_be32(tmf_cqe->exp_cmd_sn);
1583     resp_hdr->itt = task->hdr->itt;
1584     resp_hdr->response = tmf_cqe->response;
1585 
1586     __iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr, NULL, 0);
1587 done:
1588     spin_unlock(&session->back_lock);
1589     return 0;
1590 }
1591 
1592 /**
1593  * bnx2i_process_logout_resp - this function handles iscsi logout response
1594  * @session:        iscsi session pointer
1595  * @bnx2i_conn:     iscsi connection pointer
1596  * @cqe:        pointer to newly DMA'ed CQE entry for processing
1597  *
1598  * process iSCSI Logout Response CQE & make function call to
1599  * notify the user daemon.
1600  */
1601 static int bnx2i_process_logout_resp(struct iscsi_session *session,
1602                      struct bnx2i_conn *bnx2i_conn,
1603                      struct cqe *cqe)
1604 {
1605     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1606     struct iscsi_task *task;
1607     struct bnx2i_logout_response *logout;
1608     struct iscsi_logout_rsp *resp_hdr;
1609 
1610     logout = (struct bnx2i_logout_response *) cqe;
1611     spin_lock(&session->back_lock);
1612     task = iscsi_itt_to_task(conn,
1613                  logout->itt & ISCSI_LOGOUT_RESPONSE_INDEX);
1614     if (!task)
1615         goto done;
1616 
1617     resp_hdr = (struct iscsi_logout_rsp *) &bnx2i_conn->gen_pdu.resp_hdr;
1618     memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1619     resp_hdr->opcode = logout->op_code;
1620     resp_hdr->flags = logout->response;
1621     resp_hdr->hlength = 0;
1622 
1623     resp_hdr->itt = task->hdr->itt;
1624     resp_hdr->statsn = task->hdr->exp_statsn;
1625     resp_hdr->exp_cmdsn = cpu_to_be32(logout->exp_cmd_sn);
1626     resp_hdr->max_cmdsn = cpu_to_be32(logout->max_cmd_sn);
1627 
1628     resp_hdr->t2wait = cpu_to_be32(logout->time_to_wait);
1629     resp_hdr->t2retain = cpu_to_be32(logout->time_to_retain);
1630 
1631     __iscsi_complete_pdu(conn, (struct iscsi_hdr *)resp_hdr, NULL, 0);
1632 
1633     bnx2i_conn->ep->state = EP_STATE_LOGOUT_RESP_RCVD;
1634 done:
1635     spin_unlock(&session->back_lock);
1636     return 0;
1637 }
1638 
1639 /**
1640  * bnx2i_process_nopin_local_cmpl - this function handles iscsi nopin CQE
1641  * @session:        iscsi session pointer
1642  * @bnx2i_conn:     iscsi connection pointer
1643  * @cqe:        pointer to newly DMA'ed CQE entry for processing
1644  *
1645  * process iSCSI NOPIN local completion CQE, frees IIT and command structures
1646  */
1647 static void bnx2i_process_nopin_local_cmpl(struct iscsi_session *session,
1648                        struct bnx2i_conn *bnx2i_conn,
1649                        struct cqe *cqe)
1650 {
1651     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1652     struct bnx2i_nop_in_msg *nop_in;
1653     struct iscsi_task *task;
1654 
1655     nop_in = (struct bnx2i_nop_in_msg *)cqe;
1656     spin_lock(&session->back_lock);
1657     task = iscsi_itt_to_task(conn,
1658                  nop_in->itt & ISCSI_NOP_IN_MSG_INDEX);
1659     if (task)
1660         __iscsi_put_task(task);
1661     spin_unlock(&session->back_lock);
1662 }
1663 
1664 /**
1665  * bnx2i_unsol_pdu_adjust_rq - makes adjustments to RQ after unsol pdu is recvd
1666  * @bnx2i_conn: iscsi connection
1667  *
1668  * Firmware advances RQ producer index for every unsolicited PDU even if
1669  *  payload data length is '0'. This function makes corresponding
1670  *  adjustments on the driver side to match this f/w behavior
1671  */
1672 static void bnx2i_unsol_pdu_adjust_rq(struct bnx2i_conn *bnx2i_conn)
1673 {
1674     char dummy_rq_data[2];
1675     bnx2i_get_rq_buf(bnx2i_conn, dummy_rq_data, 1);
1676     bnx2i_put_rq_buf(bnx2i_conn, 1);
1677 }
1678 
1679 
1680 /**
1681  * bnx2i_process_nopin_mesg - this function handles iscsi nopin CQE
1682  * @session:        iscsi session pointer
1683  * @bnx2i_conn:     iscsi connection pointer
1684  * @cqe:        pointer to newly DMA'ed CQE entry for processing
1685  *
1686  * process iSCSI target's proactive iSCSI NOPIN request
1687  */
1688 static int bnx2i_process_nopin_mesg(struct iscsi_session *session,
1689                      struct bnx2i_conn *bnx2i_conn,
1690                      struct cqe *cqe)
1691 {
1692     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1693     struct iscsi_task *task;
1694     struct bnx2i_nop_in_msg *nop_in;
1695     struct iscsi_nopin *hdr;
1696     int tgt_async_nop = 0;
1697 
1698     nop_in = (struct bnx2i_nop_in_msg *)cqe;
1699 
1700     spin_lock(&session->back_lock);
1701     hdr = (struct iscsi_nopin *)&bnx2i_conn->gen_pdu.resp_hdr;
1702     memset(hdr, 0, sizeof(struct iscsi_hdr));
1703     hdr->opcode = nop_in->op_code;
1704     hdr->max_cmdsn = cpu_to_be32(nop_in->max_cmd_sn);
1705     hdr->exp_cmdsn = cpu_to_be32(nop_in->exp_cmd_sn);
1706     hdr->ttt = cpu_to_be32(nop_in->ttt);
1707 
1708     if (nop_in->itt == (u16) RESERVED_ITT) {
1709         bnx2i_unsol_pdu_adjust_rq(bnx2i_conn);
1710         hdr->itt = RESERVED_ITT;
1711         tgt_async_nop = 1;
1712         goto done;
1713     }
1714 
1715     /* this is a response to one of our nop-outs */
1716     task = iscsi_itt_to_task(conn,
1717              (itt_t) (nop_in->itt & ISCSI_NOP_IN_MSG_INDEX));
1718     if (task) {
1719         hdr->flags = ISCSI_FLAG_CMD_FINAL;
1720         hdr->itt = task->hdr->itt;
1721         hdr->ttt = cpu_to_be32(nop_in->ttt);
1722         memcpy(&hdr->lun, nop_in->lun, 8);
1723     }
1724 done:
1725     __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1726     spin_unlock(&session->back_lock);
1727 
1728     return tgt_async_nop;
1729 }
1730 
1731 
1732 /**
1733  * bnx2i_process_async_mesg - this function handles iscsi async message
1734  * @session:        iscsi session pointer
1735  * @bnx2i_conn:     iscsi connection pointer
1736  * @cqe:        pointer to newly DMA'ed CQE entry for processing
1737  *
1738  * process iSCSI ASYNC Message
1739  */
1740 static void bnx2i_process_async_mesg(struct iscsi_session *session,
1741                      struct bnx2i_conn *bnx2i_conn,
1742                      struct cqe *cqe)
1743 {
1744     struct bnx2i_async_msg *async_cqe;
1745     struct iscsi_async *resp_hdr;
1746     u8 async_event;
1747 
1748     bnx2i_unsol_pdu_adjust_rq(bnx2i_conn);
1749 
1750     async_cqe = (struct bnx2i_async_msg *)cqe;
1751     async_event = async_cqe->async_event;
1752 
1753     if (async_event == ISCSI_ASYNC_MSG_SCSI_EVENT) {
1754         iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
1755                   "async: scsi events not supported\n");
1756         return;
1757     }
1758 
1759     spin_lock(&session->back_lock);
1760     resp_hdr = (struct iscsi_async *) &bnx2i_conn->gen_pdu.resp_hdr;
1761     memset(resp_hdr, 0, sizeof(struct iscsi_hdr));
1762     resp_hdr->opcode = async_cqe->op_code;
1763     resp_hdr->flags = 0x80;
1764 
1765     memcpy(&resp_hdr->lun, async_cqe->lun, 8);
1766     resp_hdr->exp_cmdsn = cpu_to_be32(async_cqe->exp_cmd_sn);
1767     resp_hdr->max_cmdsn = cpu_to_be32(async_cqe->max_cmd_sn);
1768 
1769     resp_hdr->async_event = async_cqe->async_event;
1770     resp_hdr->async_vcode = async_cqe->async_vcode;
1771 
1772     resp_hdr->param1 = cpu_to_be16(async_cqe->param1);
1773     resp_hdr->param2 = cpu_to_be16(async_cqe->param2);
1774     resp_hdr->param3 = cpu_to_be16(async_cqe->param3);
1775 
1776     __iscsi_complete_pdu(bnx2i_conn->cls_conn->dd_data,
1777                  (struct iscsi_hdr *)resp_hdr, NULL, 0);
1778     spin_unlock(&session->back_lock);
1779 }
1780 
1781 
1782 /**
1783  * bnx2i_process_reject_mesg - process iscsi reject pdu
1784  * @session:        iscsi session pointer
1785  * @bnx2i_conn:     iscsi connection pointer
1786  * @cqe:        pointer to newly DMA'ed CQE entry for processing
1787  *
1788  * process iSCSI REJECT message
1789  */
1790 static void bnx2i_process_reject_mesg(struct iscsi_session *session,
1791                       struct bnx2i_conn *bnx2i_conn,
1792                       struct cqe *cqe)
1793 {
1794     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1795     struct bnx2i_reject_msg *reject;
1796     struct iscsi_reject *hdr;
1797 
1798     reject = (struct bnx2i_reject_msg *) cqe;
1799     if (reject->data_length) {
1800         bnx2i_get_rq_buf(bnx2i_conn, conn->data, reject->data_length);
1801         bnx2i_put_rq_buf(bnx2i_conn, 1);
1802     } else
1803         bnx2i_unsol_pdu_adjust_rq(bnx2i_conn);
1804 
1805     spin_lock(&session->back_lock);
1806     hdr = (struct iscsi_reject *) &bnx2i_conn->gen_pdu.resp_hdr;
1807     memset(hdr, 0, sizeof(struct iscsi_hdr));
1808     hdr->opcode = reject->op_code;
1809     hdr->reason = reject->reason;
1810     hton24(hdr->dlength, reject->data_length);
1811     hdr->max_cmdsn = cpu_to_be32(reject->max_cmd_sn);
1812     hdr->exp_cmdsn = cpu_to_be32(reject->exp_cmd_sn);
1813     hdr->ffffffff = cpu_to_be32(RESERVED_ITT);
1814     __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, conn->data,
1815                  reject->data_length);
1816     spin_unlock(&session->back_lock);
1817 }
1818 
1819 /**
1820  * bnx2i_process_cmd_cleanup_resp - process scsi command clean-up completion
1821  * @session:        iscsi session pointer
1822  * @bnx2i_conn:     iscsi connection pointer
1823  * @cqe:        pointer to newly DMA'ed CQE entry for processing
1824  *
1825  * process command cleanup response CQE during conn shutdown or error recovery
1826  */
1827 static void bnx2i_process_cmd_cleanup_resp(struct iscsi_session *session,
1828                        struct bnx2i_conn *bnx2i_conn,
1829                        struct cqe *cqe)
1830 {
1831     struct bnx2i_cleanup_response *cmd_clean_rsp;
1832     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1833     struct iscsi_task *task;
1834 
1835     cmd_clean_rsp = (struct bnx2i_cleanup_response *)cqe;
1836     spin_lock(&session->back_lock);
1837     task = iscsi_itt_to_task(conn,
1838             cmd_clean_rsp->itt & ISCSI_CLEANUP_RESPONSE_INDEX);
1839     if (!task)
1840         printk(KERN_ALERT "bnx2i: cmd clean ITT %x not active\n",
1841             cmd_clean_rsp->itt & ISCSI_CLEANUP_RESPONSE_INDEX);
1842     spin_unlock(&session->back_lock);
1843     complete(&bnx2i_conn->cmd_cleanup_cmpl);
1844 }
1845 
1846 
1847 /**
1848  * bnx2i_percpu_io_thread - thread per cpu for ios
1849  *
1850  * @arg:    ptr to bnx2i_percpu_info structure
1851  */
1852 int bnx2i_percpu_io_thread(void *arg)
1853 {
1854     struct bnx2i_percpu_s *p = arg;
1855     struct bnx2i_work *work, *tmp;
1856     LIST_HEAD(work_list);
1857 
1858     set_user_nice(current, MIN_NICE);
1859 
1860     while (!kthread_should_stop()) {
1861         spin_lock_bh(&p->p_work_lock);
1862         while (!list_empty(&p->work_list)) {
1863             list_splice_init(&p->work_list, &work_list);
1864             spin_unlock_bh(&p->p_work_lock);
1865 
1866             list_for_each_entry_safe(work, tmp, &work_list, list) {
1867                 list_del_init(&work->list);
1868                 /* work allocated in the bh, freed here */
1869                 bnx2i_process_scsi_cmd_resp(work->session,
1870                                 work->bnx2i_conn,
1871                                 &work->cqe);
1872                 atomic_dec(&work->bnx2i_conn->work_cnt);
1873                 kfree(work);
1874             }
1875             spin_lock_bh(&p->p_work_lock);
1876         }
1877         set_current_state(TASK_INTERRUPTIBLE);
1878         spin_unlock_bh(&p->p_work_lock);
1879         schedule();
1880     }
1881     __set_current_state(TASK_RUNNING);
1882 
1883     return 0;
1884 }
1885 
1886 
1887 /**
1888  * bnx2i_queue_scsi_cmd_resp - queue cmd completion to the percpu thread
1889  * @session:        iscsi session
1890  * @bnx2i_conn:     bnx2i connection
1891  * @cqe:        pointer to newly DMA'ed CQE entry for processing
1892  *
1893  * this function is called by generic KCQ handler to queue all pending cmd
1894  * completion CQEs
1895  *
1896  * The implementation is to queue the cmd response based on the
1897  * last recorded command for the given connection.  The
1898  * cpu_id gets recorded upon task_xmit.  No out-of-order completion!
1899  */
1900 static int bnx2i_queue_scsi_cmd_resp(struct iscsi_session *session,
1901                      struct bnx2i_conn *bnx2i_conn,
1902                      struct bnx2i_nop_in_msg *cqe)
1903 {
1904     struct bnx2i_work *bnx2i_work = NULL;
1905     struct bnx2i_percpu_s *p = NULL;
1906     struct iscsi_task *task;
1907     struct scsi_cmnd *sc;
1908     int rc = 0;
1909 
1910     spin_lock(&session->back_lock);
1911     task = iscsi_itt_to_task(bnx2i_conn->cls_conn->dd_data,
1912                  cqe->itt & ISCSI_CMD_RESPONSE_INDEX);
1913     if (!task || !task->sc) {
1914         spin_unlock(&session->back_lock);
1915         return -EINVAL;
1916     }
1917     sc = task->sc;
1918 
1919     spin_unlock(&session->back_lock);
1920 
1921     p = &per_cpu(bnx2i_percpu, blk_mq_rq_cpu(scsi_cmd_to_rq(sc)));
1922     spin_lock(&p->p_work_lock);
1923     if (unlikely(!p->iothread)) {
1924         rc = -EINVAL;
1925         goto err;
1926     }
1927     /* Alloc and copy to the cqe */
1928     bnx2i_work = kzalloc(sizeof(struct bnx2i_work), GFP_ATOMIC);
1929     if (bnx2i_work) {
1930         INIT_LIST_HEAD(&bnx2i_work->list);
1931         bnx2i_work->session = session;
1932         bnx2i_work->bnx2i_conn = bnx2i_conn;
1933         memcpy(&bnx2i_work->cqe, cqe, sizeof(struct cqe));
1934         list_add_tail(&bnx2i_work->list, &p->work_list);
1935         atomic_inc(&bnx2i_conn->work_cnt);
1936         wake_up_process(p->iothread);
1937         spin_unlock(&p->p_work_lock);
1938         goto done;
1939     } else
1940         rc = -ENOMEM;
1941 err:
1942     spin_unlock(&p->p_work_lock);
1943     bnx2i_process_scsi_cmd_resp(session, bnx2i_conn, (struct cqe *)cqe);
1944 done:
1945     return rc;
1946 }
1947 
1948 
1949 /**
1950  * bnx2i_process_new_cqes - process newly DMA'ed CQE's
1951  * @bnx2i_conn:     bnx2i connection
1952  *
1953  * this function is called by generic KCQ handler to process all pending CQE's
1954  */
1955 static int bnx2i_process_new_cqes(struct bnx2i_conn *bnx2i_conn)
1956 {
1957     struct iscsi_conn *conn = bnx2i_conn->cls_conn->dd_data;
1958     struct iscsi_session *session = conn->session;
1959     struct bnx2i_hba *hba = bnx2i_conn->hba;
1960     struct qp_info *qp;
1961     struct bnx2i_nop_in_msg *nopin;
1962     int tgt_async_msg;
1963     int cqe_cnt = 0;
1964 
1965     if (bnx2i_conn->ep == NULL)
1966         return 0;
1967 
1968     qp = &bnx2i_conn->ep->qp;
1969 
1970     if (!qp->cq_virt) {
1971         printk(KERN_ALERT "bnx2i (%s): cq resr freed in bh execution!",
1972                hba->netdev->name);
1973         goto out;
1974     }
1975     while (1) {
1976         nopin = (struct bnx2i_nop_in_msg *) qp->cq_cons_qe;
1977         if (nopin->cq_req_sn != qp->cqe_exp_seq_sn)
1978             break;
1979 
1980         if (unlikely(test_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags))) {
1981             if (nopin->op_code == ISCSI_OP_NOOP_IN &&
1982                 nopin->itt == (u16) RESERVED_ITT) {
1983                 printk(KERN_ALERT "bnx2i: Unsolicited "
1984                        "NOP-In detected for suspended "
1985                        "connection dev=%s!\n",
1986                        hba->netdev->name);
1987                 bnx2i_unsol_pdu_adjust_rq(bnx2i_conn);
1988                 goto cqe_out;
1989             }
1990             break;
1991         }
1992         tgt_async_msg = 0;
1993 
1994         switch (nopin->op_code) {
1995         case ISCSI_OP_SCSI_CMD_RSP:
1996         case ISCSI_OP_SCSI_DATA_IN:
1997             /* Run the kthread engine only for data cmds
1998                All other cmds will be completed in this bh! */
1999             bnx2i_queue_scsi_cmd_resp(session, bnx2i_conn, nopin);
2000             goto done;
2001         case ISCSI_OP_LOGIN_RSP:
2002             bnx2i_process_login_resp(session, bnx2i_conn,
2003                          qp->cq_cons_qe);
2004             break;
2005         case ISCSI_OP_SCSI_TMFUNC_RSP:
2006             bnx2i_process_tmf_resp(session, bnx2i_conn,
2007                            qp->cq_cons_qe);
2008             break;
2009         case ISCSI_OP_TEXT_RSP:
2010             bnx2i_process_text_resp(session, bnx2i_conn,
2011                         qp->cq_cons_qe);
2012             break;
2013         case ISCSI_OP_LOGOUT_RSP:
2014             bnx2i_process_logout_resp(session, bnx2i_conn,
2015                           qp->cq_cons_qe);
2016             break;
2017         case ISCSI_OP_NOOP_IN:
2018             if (bnx2i_process_nopin_mesg(session, bnx2i_conn,
2019                              qp->cq_cons_qe))
2020                 tgt_async_msg = 1;
2021             break;
2022         case ISCSI_OPCODE_NOPOUT_LOCAL_COMPLETION:
2023             bnx2i_process_nopin_local_cmpl(session, bnx2i_conn,
2024                                qp->cq_cons_qe);
2025             break;
2026         case ISCSI_OP_ASYNC_EVENT:
2027             bnx2i_process_async_mesg(session, bnx2i_conn,
2028                          qp->cq_cons_qe);
2029             tgt_async_msg = 1;
2030             break;
2031         case ISCSI_OP_REJECT:
2032             bnx2i_process_reject_mesg(session, bnx2i_conn,
2033                           qp->cq_cons_qe);
2034             break;
2035         case ISCSI_OPCODE_CLEANUP_RESPONSE:
2036             bnx2i_process_cmd_cleanup_resp(session, bnx2i_conn,
2037                                qp->cq_cons_qe);
2038             break;
2039         default:
2040             printk(KERN_ALERT "bnx2i: unknown opcode 0x%x\n",
2041                       nopin->op_code);
2042         }
2043 
2044         ADD_STATS_64(hba, rx_pdus, 1);
2045         ADD_STATS_64(hba, rx_bytes, nopin->data_length);
2046 done:
2047         if (!tgt_async_msg) {
2048             if (!atomic_read(&bnx2i_conn->ep->num_active_cmds))
2049                 printk(KERN_ALERT "bnx2i (%s): no active cmd! "
2050                        "op 0x%x\n",
2051                        hba->netdev->name,
2052                        nopin->op_code);
2053             else
2054                 atomic_dec(&bnx2i_conn->ep->num_active_cmds);
2055         }
2056 cqe_out:
2057         /* clear out in production version only, till beta keep opcode
2058          * field intact, will be helpful in debugging (context dump)
2059          * nopin->op_code = 0;
2060          */
2061         cqe_cnt++;
2062         qp->cqe_exp_seq_sn++;
2063         if (qp->cqe_exp_seq_sn == (qp->cqe_size * 2 + 1))
2064             qp->cqe_exp_seq_sn = ISCSI_INITIAL_SN;
2065 
2066         if (qp->cq_cons_qe == qp->cq_last_qe) {
2067             qp->cq_cons_qe = qp->cq_first_qe;
2068             qp->cq_cons_idx = 0;
2069         } else {
2070             qp->cq_cons_qe++;
2071             qp->cq_cons_idx++;
2072         }
2073     }
2074 out:
2075     return cqe_cnt;
2076 }
2077 
2078 /**
2079  * bnx2i_fastpath_notification - process global event queue (KCQ)
2080  * @hba:        adapter structure pointer
2081  * @new_cqe_kcqe:   pointer to newly DMA'ed KCQE entry
2082  *
2083  * Fast path event notification handler, KCQ entry carries context id
2084  *  of the connection that has 1 or more pending CQ entries
2085  */
2086 static void bnx2i_fastpath_notification(struct bnx2i_hba *hba,
2087                     struct iscsi_kcqe *new_cqe_kcqe)
2088 {
2089     struct bnx2i_conn *bnx2i_conn;
2090     u32 iscsi_cid;
2091     int nxt_idx;
2092 
2093     iscsi_cid = new_cqe_kcqe->iscsi_conn_id;
2094     bnx2i_conn = bnx2i_get_conn_from_id(hba, iscsi_cid);
2095 
2096     if (!bnx2i_conn) {
2097         printk(KERN_ALERT "cid #%x not valid\n", iscsi_cid);
2098         return;
2099     }
2100     if (!bnx2i_conn->ep) {
2101         printk(KERN_ALERT "cid #%x - ep not bound\n", iscsi_cid);
2102         return;
2103     }
2104 
2105     bnx2i_process_new_cqes(bnx2i_conn);
2106     nxt_idx = bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep,
2107                         CNIC_ARM_CQE_FP);
2108     if (nxt_idx && nxt_idx == bnx2i_process_new_cqes(bnx2i_conn))
2109         bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep, CNIC_ARM_CQE_FP);
2110 }
2111 
2112 
2113 /**
2114  * bnx2i_process_update_conn_cmpl - process iscsi conn update completion KCQE
2115  * @hba:        adapter structure pointer
2116  * @update_kcqe:    kcqe pointer
2117  *
2118  * CONN_UPDATE completion handler, this completes iSCSI connection FFP migration
2119  */
2120 static void bnx2i_process_update_conn_cmpl(struct bnx2i_hba *hba,
2121                        struct iscsi_kcqe *update_kcqe)
2122 {
2123     struct bnx2i_conn *conn;
2124     u32 iscsi_cid;
2125 
2126     iscsi_cid = update_kcqe->iscsi_conn_id;
2127     conn = bnx2i_get_conn_from_id(hba, iscsi_cid);
2128 
2129     if (!conn) {
2130         printk(KERN_ALERT "conn_update: cid %x not valid\n", iscsi_cid);
2131         return;
2132     }
2133     if (!conn->ep) {
2134         printk(KERN_ALERT "cid %x does not have ep bound\n", iscsi_cid);
2135         return;
2136     }
2137 
2138     if (update_kcqe->completion_status) {
2139         printk(KERN_ALERT "request failed cid %x\n", iscsi_cid);
2140         conn->ep->state = EP_STATE_ULP_UPDATE_FAILED;
2141     } else
2142         conn->ep->state = EP_STATE_ULP_UPDATE_COMPL;
2143 
2144     wake_up_interruptible(&conn->ep->ofld_wait);
2145 }
2146 
2147 
2148 /**
2149  * bnx2i_recovery_que_add_conn - add connection to recovery queue
2150  * @hba:        adapter structure pointer
2151  * @bnx2i_conn:     iscsi connection
2152  *
2153  * Add connection to recovery queue and schedule adapter eh worker
2154  */
2155 static void bnx2i_recovery_que_add_conn(struct bnx2i_hba *hba,
2156                     struct bnx2i_conn *bnx2i_conn)
2157 {
2158     iscsi_conn_failure(bnx2i_conn->cls_conn->dd_data,
2159                ISCSI_ERR_CONN_FAILED);
2160 }
2161 
2162 
2163 /**
2164  * bnx2i_process_tcp_error - process error notification on a given connection
2165  *
2166  * @hba:        adapter structure pointer
2167  * @tcp_err:        tcp error kcqe pointer
2168  *
2169  * handles tcp level error notifications from FW.
2170  */
2171 static void bnx2i_process_tcp_error(struct bnx2i_hba *hba,
2172                     struct iscsi_kcqe *tcp_err)
2173 {
2174     struct bnx2i_conn *bnx2i_conn;
2175     u32 iscsi_cid;
2176 
2177     iscsi_cid = tcp_err->iscsi_conn_id;
2178     bnx2i_conn = bnx2i_get_conn_from_id(hba, iscsi_cid);
2179 
2180     if (!bnx2i_conn) {
2181         printk(KERN_ALERT "bnx2i - cid 0x%x not valid\n", iscsi_cid);
2182         return;
2183     }
2184 
2185     printk(KERN_ALERT "bnx2i - cid 0x%x had TCP errors, error code 0x%x\n",
2186               iscsi_cid, tcp_err->completion_status);
2187     bnx2i_recovery_que_add_conn(bnx2i_conn->hba, bnx2i_conn);
2188 }
2189 
2190 
2191 /**
2192  * bnx2i_process_iscsi_error - process error notification on a given connection
2193  * @hba:        adapter structure pointer
2194  * @iscsi_err:      iscsi error kcqe pointer
2195  *
2196  * handles iscsi error notifications from the FW. Firmware based in initial
2197  *  handshake classifies iscsi protocol / TCP rfc violation into either
2198  *  warning or error indications. If indication is of "Error" type, driver
2199  *  will initiate session recovery for that connection/session. For
2200  *  "Warning" type indication, driver will put out a system log message
2201  *  (there will be only one message for each type for the life of the
2202  *  session, this is to avoid un-necessarily overloading the system)
2203  */
2204 static void bnx2i_process_iscsi_error(struct bnx2i_hba *hba,
2205                       struct iscsi_kcqe *iscsi_err)
2206 {
2207     struct bnx2i_conn *bnx2i_conn;
2208     u32 iscsi_cid;
2209     const char *additional_notice = "";
2210     const char *message;
2211     int need_recovery;
2212     u64 err_mask64;
2213 
2214     iscsi_cid = iscsi_err->iscsi_conn_id;
2215     bnx2i_conn = bnx2i_get_conn_from_id(hba, iscsi_cid);
2216     if (!bnx2i_conn) {
2217         printk(KERN_ALERT "bnx2i - cid 0x%x not valid\n", iscsi_cid);
2218         return;
2219     }
2220 
2221     err_mask64 = (0x1ULL << iscsi_err->completion_status);
2222 
2223     if (err_mask64 & iscsi_error_mask) {
2224         need_recovery = 0;
2225         message = "iscsi_warning";
2226     } else {
2227         need_recovery = 1;
2228         message = "iscsi_error";
2229     }
2230 
2231     switch (iscsi_err->completion_status) {
2232     case ISCSI_KCQE_COMPLETION_STATUS_HDR_DIG_ERR:
2233         additional_notice = "hdr digest err";
2234         break;
2235     case ISCSI_KCQE_COMPLETION_STATUS_DATA_DIG_ERR:
2236         additional_notice = "data digest err";
2237         break;
2238     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_OPCODE:
2239         additional_notice = "wrong opcode rcvd";
2240         break;
2241     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_AHS_LEN:
2242         additional_notice = "AHS len > 0 rcvd";
2243         break;
2244     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_ITT:
2245         additional_notice = "invalid ITT rcvd";
2246         break;
2247     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_STATSN:
2248         additional_notice = "wrong StatSN rcvd";
2249         break;
2250     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_EXP_DATASN:
2251         additional_notice = "wrong DataSN rcvd";
2252         break;
2253     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_PEND_R2T:
2254         additional_notice = "pend R2T violation";
2255         break;
2256     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_0:
2257         additional_notice = "ERL0, UO";
2258         break;
2259     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_1:
2260         additional_notice = "ERL0, U1";
2261         break;
2262     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_2:
2263         additional_notice = "ERL0, U2";
2264         break;
2265     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_3:
2266         additional_notice = "ERL0, U3";
2267         break;
2268     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_4:
2269         additional_notice = "ERL0, U4";
2270         break;
2271     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_5:
2272         additional_notice = "ERL0, U5";
2273         break;
2274     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_O_U_6:
2275         additional_notice = "ERL0, U6";
2276         break;
2277     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_REMAIN_RCV_LEN:
2278         additional_notice = "invalid resi len";
2279         break;
2280     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_MAX_RCV_PDU_LEN:
2281         additional_notice = "MRDSL violation";
2282         break;
2283     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_F_BIT_ZERO:
2284         additional_notice = "F-bit not set";
2285         break;
2286     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_TTT_NOT_RSRV:
2287         additional_notice = "invalid TTT";
2288         break;
2289     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_DATASN:
2290         additional_notice = "invalid DataSN";
2291         break;
2292     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_REMAIN_BURST_LEN:
2293         additional_notice = "burst len violation";
2294         break;
2295     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_BUFFER_OFF:
2296         additional_notice = "buf offset violation";
2297         break;
2298     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_LUN:
2299         additional_notice = "invalid LUN field";
2300         break;
2301     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_R2TSN:
2302         additional_notice = "invalid R2TSN field";
2303         break;
2304 #define BNX2I_ERR_DESIRED_DATA_TRNS_LEN_0   \
2305     ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_0
2306     case BNX2I_ERR_DESIRED_DATA_TRNS_LEN_0:
2307         additional_notice = "invalid cmd len1";
2308         break;
2309 #define BNX2I_ERR_DESIRED_DATA_TRNS_LEN_1   \
2310     ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_DESIRED_DATA_TRNS_LEN_1
2311     case BNX2I_ERR_DESIRED_DATA_TRNS_LEN_1:
2312         additional_notice = "invalid cmd len2";
2313         break;
2314     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_PEND_R2T_EXCEED:
2315         additional_notice = "pend r2t exceeds MaxOutstandingR2T value";
2316         break;
2317     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_TTT_IS_RSRV:
2318         additional_notice = "TTT is rsvd";
2319         break;
2320     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_MAX_BURST_LEN:
2321         additional_notice = "MBL violation";
2322         break;
2323 #define BNX2I_ERR_DATA_SEG_LEN_NOT_ZERO     \
2324     ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_DATA_SEG_LEN_NOT_ZERO
2325     case BNX2I_ERR_DATA_SEG_LEN_NOT_ZERO:
2326         additional_notice = "data seg len != 0";
2327         break;
2328     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_REJECT_PDU_LEN:
2329         additional_notice = "reject pdu len error";
2330         break;
2331     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_ASYNC_PDU_LEN:
2332         additional_notice = "async pdu len error";
2333         break;
2334     case ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_NOPIN_PDU_LEN:
2335         additional_notice = "nopin pdu len error";
2336         break;
2337 #define BNX2_ERR_PEND_R2T_IN_CLEANUP            \
2338     ISCSI_KCQE_COMPLETION_STATUS_PROTOCOL_ERR_PEND_R2T_IN_CLEANUP
2339     case BNX2_ERR_PEND_R2T_IN_CLEANUP:
2340         additional_notice = "pend r2t in cleanup";
2341         break;
2342 
2343     case ISCI_KCQE_COMPLETION_STATUS_TCP_ERROR_IP_FRAGMENT:
2344         additional_notice = "IP fragments rcvd";
2345         break;
2346     case ISCI_KCQE_COMPLETION_STATUS_TCP_ERROR_IP_OPTIONS:
2347         additional_notice = "IP options error";
2348         break;
2349     case ISCI_KCQE_COMPLETION_STATUS_TCP_ERROR_URGENT_FLAG:
2350         additional_notice = "urgent flag error";
2351         break;
2352     default:
2353         printk(KERN_ALERT "iscsi_err - unknown err %x\n",
2354                   iscsi_err->completion_status);
2355     }
2356 
2357     if (need_recovery) {
2358         iscsi_conn_printk(KERN_ALERT,
2359                   bnx2i_conn->cls_conn->dd_data,
2360                   "bnx2i: %s - %s\n",
2361                   message, additional_notice);
2362 
2363         iscsi_conn_printk(KERN_ALERT,
2364                   bnx2i_conn->cls_conn->dd_data,
2365                   "conn_err - hostno %d conn %p, "
2366                   "iscsi_cid %x cid %x\n",
2367                   bnx2i_conn->hba->shost->host_no,
2368                   bnx2i_conn, bnx2i_conn->ep->ep_iscsi_cid,
2369                   bnx2i_conn->ep->ep_cid);
2370         bnx2i_recovery_que_add_conn(bnx2i_conn->hba, bnx2i_conn);
2371     } else
2372         if (!test_and_set_bit(iscsi_err->completion_status,
2373                       (void *) &bnx2i_conn->violation_notified))
2374             iscsi_conn_printk(KERN_ALERT,
2375                       bnx2i_conn->cls_conn->dd_data,
2376                       "bnx2i: %s - %s\n",
2377                       message, additional_notice);
2378 }
2379 
2380 
2381 /**
2382  * bnx2i_process_conn_destroy_cmpl - process iscsi conn destroy completion
2383  * @hba:        adapter structure pointer
2384  * @conn_destroy:   conn destroy kcqe pointer
2385  *
2386  * handles connection destroy completion request.
2387  */
2388 static void bnx2i_process_conn_destroy_cmpl(struct bnx2i_hba *hba,
2389                         struct iscsi_kcqe *conn_destroy)
2390 {
2391     struct bnx2i_endpoint *ep;
2392 
2393     ep = bnx2i_find_ep_in_destroy_list(hba, conn_destroy->iscsi_conn_id);
2394     if (!ep) {
2395         printk(KERN_ALERT "bnx2i_conn_destroy_cmpl: no pending "
2396                   "offload request, unexpected completion\n");
2397         return;
2398     }
2399 
2400     if (hba != ep->hba) {
2401         printk(KERN_ALERT "conn destroy- error hba mismatch\n");
2402         return;
2403     }
2404 
2405     if (conn_destroy->completion_status) {
2406         printk(KERN_ALERT "conn_destroy_cmpl: op failed\n");
2407         ep->state = EP_STATE_CLEANUP_FAILED;
2408     } else
2409         ep->state = EP_STATE_CLEANUP_CMPL;
2410     wake_up_interruptible(&ep->ofld_wait);
2411 }
2412 
2413 
2414 /**
2415  * bnx2i_process_ofld_cmpl - process initial iscsi conn offload completion
2416  * @hba:        adapter structure pointer
2417  * @ofld_kcqe:      conn offload kcqe pointer
2418  *
2419  * handles initial connection offload completion, ep_connect() thread is
2420  *  woken-up to continue with LLP connect process
2421  */
2422 static void bnx2i_process_ofld_cmpl(struct bnx2i_hba *hba,
2423                     struct iscsi_kcqe *ofld_kcqe)
2424 {
2425     u32 cid_addr;
2426     struct bnx2i_endpoint *ep;
2427 
2428     ep = bnx2i_find_ep_in_ofld_list(hba, ofld_kcqe->iscsi_conn_id);
2429     if (!ep) {
2430         printk(KERN_ALERT "ofld_cmpl: no pend offload request\n");
2431         return;
2432     }
2433 
2434     if (hba != ep->hba) {
2435         printk(KERN_ALERT "ofld_cmpl: error hba mismatch\n");
2436         return;
2437     }
2438 
2439     if (ofld_kcqe->completion_status) {
2440         ep->state = EP_STATE_OFLD_FAILED;
2441         if (ofld_kcqe->completion_status ==
2442             ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE)
2443             printk(KERN_ALERT "bnx2i (%s): ofld1 cmpl - unable "
2444                 "to allocate iSCSI context resources\n",
2445                 hba->netdev->name);
2446         else if (ofld_kcqe->completion_status ==
2447              ISCSI_KCQE_COMPLETION_STATUS_INVALID_OPCODE)
2448             printk(KERN_ALERT "bnx2i (%s): ofld1 cmpl - invalid "
2449                 "opcode\n", hba->netdev->name);
2450         else if (ofld_kcqe->completion_status ==
2451              ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY)
2452             /* error status code valid only for 5771x chipset */
2453             ep->state = EP_STATE_OFLD_FAILED_CID_BUSY;
2454         else
2455             printk(KERN_ALERT "bnx2i (%s): ofld1 cmpl - invalid "
2456                 "error code %d\n", hba->netdev->name,
2457                 ofld_kcqe->completion_status);
2458     } else {
2459         ep->state = EP_STATE_OFLD_COMPL;
2460         cid_addr = ofld_kcqe->iscsi_conn_context_id;
2461         ep->ep_cid = cid_addr;
2462         ep->qp.ctx_base = NULL;
2463     }
2464     wake_up_interruptible(&ep->ofld_wait);
2465 }
2466 
2467 /**
2468  * bnx2i_indicate_kcqe - process iscsi conn update completion KCQE
2469  * @context:        adapter structure pointer
2470  * @kcqe:       kcqe pointer
2471  * @num_cqe:        number of kcqes to process
2472  *
2473  * Generic KCQ event handler/dispatcher
2474  */
2475 static void bnx2i_indicate_kcqe(void *context, struct kcqe *kcqe[],
2476                 u32 num_cqe)
2477 {
2478     struct bnx2i_hba *hba = context;
2479     int i = 0;
2480     struct iscsi_kcqe *ikcqe = NULL;
2481 
2482     while (i < num_cqe) {
2483         ikcqe = (struct iscsi_kcqe *) kcqe[i++];
2484 
2485         if (ikcqe->op_code ==
2486             ISCSI_KCQE_OPCODE_CQ_EVENT_NOTIFICATION)
2487             bnx2i_fastpath_notification(hba, ikcqe);
2488         else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_OFFLOAD_CONN)
2489             bnx2i_process_ofld_cmpl(hba, ikcqe);
2490         else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_UPDATE_CONN)
2491             bnx2i_process_update_conn_cmpl(hba, ikcqe);
2492         else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_INIT) {
2493             if (ikcqe->completion_status !=
2494                 ISCSI_KCQE_COMPLETION_STATUS_SUCCESS)
2495                 bnx2i_iscsi_license_error(hba, ikcqe->\
2496                               completion_status);
2497             else {
2498                 set_bit(ADAPTER_STATE_UP, &hba->adapter_state);
2499                 bnx2i_get_link_state(hba);
2500                 printk(KERN_INFO "bnx2i [%.2x:%.2x.%.2x]: "
2501                          "ISCSI_INIT passed\n",
2502                          (u8)hba->pcidev->bus->number,
2503                          hba->pci_devno,
2504                          (u8)hba->pci_func);
2505 
2506 
2507             }
2508         } else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_DESTROY_CONN)
2509             bnx2i_process_conn_destroy_cmpl(hba, ikcqe);
2510         else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_ISCSI_ERROR)
2511             bnx2i_process_iscsi_error(hba, ikcqe);
2512         else if (ikcqe->op_code == ISCSI_KCQE_OPCODE_TCP_ERROR)
2513             bnx2i_process_tcp_error(hba, ikcqe);
2514         else
2515             printk(KERN_ALERT "bnx2i: unknown opcode 0x%x\n",
2516                       ikcqe->op_code);
2517     }
2518 }
2519 
2520 
2521 /**
2522  * bnx2i_indicate_netevent - Generic netdev event handler
2523  * @context:    adapter structure pointer
2524  * @event:  event type
2525  * @vlan_id:    vlans id - associated vlan id with this event
2526  *
2527  * Handles four netdev events, NETDEV_UP, NETDEV_DOWN,
2528  *  NETDEV_GOING_DOWN and NETDEV_CHANGE
2529  */
2530 static void bnx2i_indicate_netevent(void *context, unsigned long event,
2531                     u16 vlan_id)
2532 {
2533     struct bnx2i_hba *hba = context;
2534 
2535     /* Ignore all netevent coming from vlans */
2536     if (vlan_id != 0)
2537         return;
2538 
2539     switch (event) {
2540     case NETDEV_UP:
2541         if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
2542             bnx2i_send_fw_iscsi_init_msg(hba);
2543         break;
2544     case NETDEV_DOWN:
2545         clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
2546         clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
2547         break;
2548     case NETDEV_GOING_DOWN:
2549         set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
2550         iscsi_host_for_each_session(hba->shost,
2551                         bnx2i_drop_session);
2552         break;
2553     case NETDEV_CHANGE:
2554         bnx2i_get_link_state(hba);
2555         break;
2556     default:
2557         ;
2558     }
2559 }
2560 
2561 
2562 /**
2563  * bnx2i_cm_connect_cmpl - process iscsi conn establishment completion
2564  * @cm_sk:      cnic sock structure pointer
2565  *
2566  * function callback exported via bnx2i - cnic driver interface to
2567  *  indicate completion of option-2 TCP connect request.
2568  */
2569 static void bnx2i_cm_connect_cmpl(struct cnic_sock *cm_sk)
2570 {
2571     struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2572 
2573     if (test_bit(ADAPTER_STATE_GOING_DOWN, &ep->hba->adapter_state))
2574         ep->state = EP_STATE_CONNECT_FAILED;
2575     else if (test_bit(SK_F_OFFLD_COMPLETE, &cm_sk->flags))
2576         ep->state = EP_STATE_CONNECT_COMPL;
2577     else
2578         ep->state = EP_STATE_CONNECT_FAILED;
2579 
2580     wake_up_interruptible(&ep->ofld_wait);
2581 }
2582 
2583 
2584 /**
2585  * bnx2i_cm_close_cmpl - process tcp conn close completion
2586  * @cm_sk:  cnic sock structure pointer
2587  *
2588  * function callback exported via bnx2i - cnic driver interface to
2589  *  indicate completion of option-2 graceful TCP connect shutdown
2590  */
2591 static void bnx2i_cm_close_cmpl(struct cnic_sock *cm_sk)
2592 {
2593     struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2594 
2595     ep->state = EP_STATE_DISCONN_COMPL;
2596     wake_up_interruptible(&ep->ofld_wait);
2597 }
2598 
2599 
2600 /**
2601  * bnx2i_cm_abort_cmpl - process abortive tcp conn teardown completion
2602  * @cm_sk:  cnic sock structure pointer
2603  *
2604  * function callback exported via bnx2i - cnic driver interface to
2605  *  indicate completion of option-2 abortive TCP connect termination
2606  */
2607 static void bnx2i_cm_abort_cmpl(struct cnic_sock *cm_sk)
2608 {
2609     struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2610 
2611     ep->state = EP_STATE_DISCONN_COMPL;
2612     wake_up_interruptible(&ep->ofld_wait);
2613 }
2614 
2615 
2616 /**
2617  * bnx2i_cm_remote_close - process received TCP FIN
2618  * @cm_sk:  cnic sock structure pointer
2619  *
2620  * function callback exported via bnx2i - cnic driver interface to indicate
2621  *  async TCP events such as FIN
2622  */
2623 static void bnx2i_cm_remote_close(struct cnic_sock *cm_sk)
2624 {
2625     struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2626 
2627     ep->state = EP_STATE_TCP_FIN_RCVD;
2628     if (ep->conn)
2629         bnx2i_recovery_que_add_conn(ep->hba, ep->conn);
2630 }
2631 
2632 /**
2633  * bnx2i_cm_remote_abort - process TCP RST and start conn cleanup
2634  * @cm_sk:  cnic sock structure pointer
2635  *
2636  * function callback exported via bnx2i - cnic driver interface to
2637  *  indicate async TCP events (RST) sent by the peer.
2638  */
2639 static void bnx2i_cm_remote_abort(struct cnic_sock *cm_sk)
2640 {
2641     struct bnx2i_endpoint *ep = (struct bnx2i_endpoint *) cm_sk->context;
2642     u32 old_state = ep->state;
2643 
2644     ep->state = EP_STATE_TCP_RST_RCVD;
2645     if (old_state == EP_STATE_DISCONN_START)
2646         wake_up_interruptible(&ep->ofld_wait);
2647     else
2648         if (ep->conn)
2649             bnx2i_recovery_que_add_conn(ep->hba, ep->conn);
2650 }
2651 
2652 
2653 static int bnx2i_send_nl_mesg(void *context, u32 msg_type,
2654                   char *buf, u16 buflen)
2655 {
2656     struct bnx2i_hba *hba = context;
2657     int rc;
2658 
2659     if (!hba)
2660         return -ENODEV;
2661 
2662     rc = iscsi_offload_mesg(hba->shost, &bnx2i_iscsi_transport,
2663                 msg_type, buf, buflen);
2664     if (rc)
2665         printk(KERN_ALERT "bnx2i: private nl message send error\n");
2666 
2667     return rc;
2668 }
2669 
2670 
2671 /*
2672  * bnx2i_cnic_cb - global template of bnx2i - cnic driver interface structure
2673  *          carrying callback function pointers
2674  */
2675 struct cnic_ulp_ops bnx2i_cnic_cb = {
2676     .cnic_init = bnx2i_ulp_init,
2677     .cnic_exit = bnx2i_ulp_exit,
2678     .cnic_start = bnx2i_start,
2679     .cnic_stop = bnx2i_stop,
2680     .indicate_kcqes = bnx2i_indicate_kcqe,
2681     .indicate_netevent = bnx2i_indicate_netevent,
2682     .cm_connect_complete = bnx2i_cm_connect_cmpl,
2683     .cm_close_complete = bnx2i_cm_close_cmpl,
2684     .cm_abort_complete = bnx2i_cm_abort_cmpl,
2685     .cm_remote_close = bnx2i_cm_remote_close,
2686     .cm_remote_abort = bnx2i_cm_remote_abort,
2687     .iscsi_nl_send_msg = bnx2i_send_nl_mesg,
2688     .cnic_get_stats = bnx2i_get_stats,
2689     .owner = THIS_MODULE
2690 };
2691 
2692 
2693 /**
2694  * bnx2i_map_ep_dbell_regs - map connection doorbell registers
2695  * @ep: bnx2i endpoint
2696  *
2697  * maps connection's SQ and RQ doorbell registers, 5706/5708/5709 hosts these
2698  *  register in BAR #0. Whereas in 57710 these register are accessed by
2699  *  mapping BAR #1
2700  */
2701 int bnx2i_map_ep_dbell_regs(struct bnx2i_endpoint *ep)
2702 {
2703     u32 cid_num;
2704     u32 reg_off;
2705     u32 first_l4l5;
2706     u32 ctx_sz;
2707     u32 config2;
2708     resource_size_t reg_base;
2709 
2710     cid_num = bnx2i_get_cid_num(ep);
2711 
2712     if (test_bit(BNX2I_NX2_DEV_57710, &ep->hba->cnic_dev_type)) {
2713         reg_base = pci_resource_start(ep->hba->pcidev,
2714                           BNX2X_DOORBELL_PCI_BAR);
2715         reg_off = (1 << BNX2X_DB_SHIFT) * (cid_num & 0x1FFFF);
2716         ep->qp.ctx_base = ioremap(reg_base + reg_off, 4);
2717         if (!ep->qp.ctx_base)
2718             return -ENOMEM;
2719         goto arm_cq;
2720     }
2721 
2722     if ((test_bit(BNX2I_NX2_DEV_5709, &ep->hba->cnic_dev_type)) &&
2723         (ep->hba->mail_queue_access == BNX2I_MQ_BIN_MODE)) {
2724         config2 = REG_RD(ep->hba, BNX2_MQ_CONFIG2);
2725         first_l4l5 = config2 & BNX2_MQ_CONFIG2_FIRST_L4L5;
2726         ctx_sz = (config2 & BNX2_MQ_CONFIG2_CONT_SZ) >> 3;
2727         if (ctx_sz)
2728             reg_off = CTX_OFFSET + MAX_CID_CNT * MB_KERNEL_CTX_SIZE
2729                   + BNX2I_570X_PAGE_SIZE_DEFAULT *
2730                   (((cid_num - first_l4l5) / ctx_sz) + 256);
2731         else
2732             reg_off = CTX_OFFSET + (MB_KERNEL_CTX_SIZE * cid_num);
2733     } else
2734         /* 5709 device in normal node and 5706/5708 devices */
2735         reg_off = CTX_OFFSET + (MB_KERNEL_CTX_SIZE * cid_num);
2736 
2737     ep->qp.ctx_base = ioremap(ep->hba->reg_base + reg_off,
2738                       MB_KERNEL_CTX_SIZE);
2739     if (!ep->qp.ctx_base)
2740         return -ENOMEM;
2741 
2742 arm_cq:
2743     bnx2i_arm_cq_event_coalescing(ep, CNIC_ARM_CQE);
2744     return 0;
2745 }