Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * This file is part of the Emulex Linux Device Driver for Enterprise iSCSI
0003  * Host Bus Adapters. Refer to the README file included with this package
0004  * for driver version and adapter compatibility.
0005  *
0006  * Copyright (c) 2018 Broadcom. All Rights Reserved.
0007  * The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
0008  *
0009  * This program is free software; you can redistribute it and/or modify it
0010  * under the terms of version 2 of the GNU General Public License as published
0011  * by the Free Software Foundation.
0012  *
0013  * This program is distributed in the hope that it will be useful. ALL EXPRESS
0014  * OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
0015  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
0016  * OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH
0017  * DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
0018  * See the GNU General Public License for more details, a copy of which
0019  * can be found in the file COPYING included with this package.
0020  *
0021  * Contact Information:
0022  * linux-drivers@broadcom.com
0023  *
0024  */
0025 
0026 #include <linux/bsg-lib.h>
0027 #include <scsi/scsi_transport_iscsi.h>
0028 #include <scsi/scsi_bsg_iscsi.h>
0029 #include "be_mgmt.h"
0030 #include "be_iscsi.h"
0031 #include "be_main.h"
0032 
0033 unsigned int mgmt_vendor_specific_fw_cmd(struct be_ctrl_info *ctrl,
0034                      struct beiscsi_hba *phba,
0035                      struct bsg_job *job,
0036                      struct be_dma_mem *nonemb_cmd)
0037 {
0038     struct be_mcc_wrb *wrb;
0039     struct be_sge *mcc_sge;
0040     unsigned int tag = 0;
0041     struct iscsi_bsg_request *bsg_req = job->request;
0042     struct be_bsg_vendor_cmd *req = nonemb_cmd->va;
0043     unsigned short region, sector_size, sector, offset;
0044 
0045     nonemb_cmd->size = job->request_payload.payload_len;
0046     memset(nonemb_cmd->va, 0, nonemb_cmd->size);
0047     region =  bsg_req->rqst_data.h_vendor.vendor_cmd[1];
0048     sector_size =  bsg_req->rqst_data.h_vendor.vendor_cmd[2];
0049     sector =  bsg_req->rqst_data.h_vendor.vendor_cmd[3];
0050     offset =  bsg_req->rqst_data.h_vendor.vendor_cmd[4];
0051     req->region = region;
0052     req->sector = sector;
0053     req->offset = offset;
0054 
0055     if (mutex_lock_interruptible(&ctrl->mbox_lock))
0056         return 0;
0057     switch (bsg_req->rqst_data.h_vendor.vendor_cmd[0]) {
0058     case BEISCSI_WRITE_FLASH:
0059         offset = sector * sector_size + offset;
0060         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
0061                    OPCODE_COMMON_WRITE_FLASH, sizeof(*req));
0062         sg_copy_to_buffer(job->request_payload.sg_list,
0063                   job->request_payload.sg_cnt,
0064                   nonemb_cmd->va + offset, job->request_len);
0065         break;
0066     case BEISCSI_READ_FLASH:
0067         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
0068                OPCODE_COMMON_READ_FLASH, sizeof(*req));
0069         break;
0070     default:
0071         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
0072                 "BG_%d : Unsupported cmd = 0x%x\n\n",
0073                 bsg_req->rqst_data.h_vendor.vendor_cmd[0]);
0074 
0075         mutex_unlock(&ctrl->mbox_lock);
0076         return -EPERM;
0077     }
0078 
0079     wrb = alloc_mcc_wrb(phba, &tag);
0080     if (!wrb) {
0081         mutex_unlock(&ctrl->mbox_lock);
0082         return 0;
0083     }
0084 
0085     mcc_sge = nonembedded_sgl(wrb);
0086     be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false,
0087                job->request_payload.sg_cnt);
0088     mcc_sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
0089     mcc_sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
0090     mcc_sge->len = cpu_to_le32(nonemb_cmd->size);
0091 
0092     be_mcc_notify(phba, tag);
0093 
0094     mutex_unlock(&ctrl->mbox_lock);
0095     return tag;
0096 }
0097 
0098 /**
0099  * mgmt_open_connection()- Establish a TCP CXN
0100  * @phba: driver priv structure
0101  * @dst_addr: Destination Address
0102  * @beiscsi_ep: ptr to device endpoint struct
0103  * @nonemb_cmd: ptr to memory allocated for command
0104  *
0105  * return
0106  *  Success: Tag number of the MBX Command issued
0107  *  Failure: Error code
0108  **/
0109 int mgmt_open_connection(struct beiscsi_hba *phba,
0110              struct sockaddr *dst_addr,
0111              struct beiscsi_endpoint *beiscsi_ep,
0112              struct be_dma_mem *nonemb_cmd)
0113 {
0114     struct hwi_controller *phwi_ctrlr;
0115     struct hwi_context_memory *phwi_context;
0116     struct sockaddr_in *daddr_in = (struct sockaddr_in *)dst_addr;
0117     struct sockaddr_in6 *daddr_in6 = (struct sockaddr_in6 *)dst_addr;
0118     struct be_ctrl_info *ctrl = &phba->ctrl;
0119     struct be_mcc_wrb *wrb;
0120     struct tcp_connect_and_offload_in_v1 *req;
0121     unsigned short def_hdr_id;
0122     unsigned short def_data_id;
0123     struct phys_addr template_address = { 0, 0 };
0124     struct phys_addr *ptemplate_address;
0125     unsigned int tag = 0;
0126     unsigned int i, ulp_num;
0127     unsigned short cid = beiscsi_ep->ep_cid;
0128     struct be_sge *sge;
0129 
0130     if (dst_addr->sa_family != PF_INET && dst_addr->sa_family != PF_INET6) {
0131         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0132                 "BG_%d : unknown addr family %d\n",
0133                 dst_addr->sa_family);
0134         return 0;
0135     }
0136 
0137     phwi_ctrlr = phba->phwi_ctrlr;
0138     phwi_context = phwi_ctrlr->phwi_ctxt;
0139 
0140     ulp_num = phwi_ctrlr->wrb_context[BE_GET_CRI_FROM_CID(cid)].ulp_num;
0141 
0142     def_hdr_id = (unsigned short)HWI_GET_DEF_HDRQ_ID(phba, ulp_num);
0143     def_data_id = (unsigned short)HWI_GET_DEF_BUFQ_ID(phba, ulp_num);
0144 
0145     ptemplate_address = &template_address;
0146     ISCSI_GET_PDU_TEMPLATE_ADDRESS(phba, ptemplate_address);
0147     if (mutex_lock_interruptible(&ctrl->mbox_lock))
0148         return 0;
0149     wrb = alloc_mcc_wrb(phba, &tag);
0150     if (!wrb) {
0151         mutex_unlock(&ctrl->mbox_lock);
0152         return 0;
0153     }
0154 
0155     sge = nonembedded_sgl(wrb);
0156     req = nonemb_cmd->va;
0157     memset(req, 0, sizeof(*req));
0158 
0159     be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false, 1);
0160     be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
0161                OPCODE_COMMON_ISCSI_TCP_CONNECT_AND_OFFLOAD,
0162                nonemb_cmd->size);
0163     if (dst_addr->sa_family == PF_INET) {
0164         __be32 s_addr = daddr_in->sin_addr.s_addr;
0165         req->ip_address.ip_type = BEISCSI_IP_TYPE_V4;
0166         req->ip_address.addr[0] = s_addr & 0x000000ff;
0167         req->ip_address.addr[1] = (s_addr & 0x0000ff00) >> 8;
0168         req->ip_address.addr[2] = (s_addr & 0x00ff0000) >> 16;
0169         req->ip_address.addr[3] = (s_addr & 0xff000000) >> 24;
0170         req->tcp_port = ntohs(daddr_in->sin_port);
0171         beiscsi_ep->dst_addr = daddr_in->sin_addr.s_addr;
0172         beiscsi_ep->dst_tcpport = ntohs(daddr_in->sin_port);
0173         beiscsi_ep->ip_type = BEISCSI_IP_TYPE_V4;
0174     } else {
0175         /* else its PF_INET6 family */
0176         req->ip_address.ip_type = BEISCSI_IP_TYPE_V6;
0177         memcpy(&req->ip_address.addr,
0178                &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
0179         req->tcp_port = ntohs(daddr_in6->sin6_port);
0180         beiscsi_ep->dst_tcpport = ntohs(daddr_in6->sin6_port);
0181         memcpy(&beiscsi_ep->dst6_addr,
0182                &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
0183         beiscsi_ep->ip_type = BEISCSI_IP_TYPE_V6;
0184     }
0185     req->cid = cid;
0186     i = phba->nxt_cqid++;
0187     if (phba->nxt_cqid == phba->num_cpus)
0188         phba->nxt_cqid = 0;
0189     req->cq_id = phwi_context->be_cq[i].id;
0190     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0191             "BG_%d : i=%d cq_id=%d\n", i, req->cq_id);
0192     req->defq_id = def_hdr_id;
0193     req->hdr_ring_id = def_hdr_id;
0194     req->data_ring_id = def_data_id;
0195     req->do_offload = 1;
0196     req->dataout_template_pa.lo = ptemplate_address->lo;
0197     req->dataout_template_pa.hi = ptemplate_address->hi;
0198     sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
0199     sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
0200     sge->len = cpu_to_le32(nonemb_cmd->size);
0201 
0202     if (!is_chip_be2_be3r(phba)) {
0203         req->hdr.version = MBX_CMD_VER1;
0204         req->tcp_window_size = 0x8000;
0205         req->tcp_window_scale_count = 2;
0206     }
0207 
0208     be_mcc_notify(phba, tag);
0209     mutex_unlock(&ctrl->mbox_lock);
0210     return tag;
0211 }
0212 
0213 /**
0214  * beiscsi_exec_nemb_cmd()- execute non-embedded MBX cmd
0215  * @phba: driver priv structure
0216  * @nonemb_cmd: DMA address of the MBX command to be issued
0217  * @cbfn: callback func on MCC completion
0218  * @resp_buf: buffer to copy the MBX cmd response
0219  * @resp_buf_len: response length to be copied
0220  *
0221  **/
0222 static int beiscsi_exec_nemb_cmd(struct beiscsi_hba *phba,
0223                  struct be_dma_mem *nonemb_cmd,
0224                  void (*cbfn)(struct beiscsi_hba *,
0225                           unsigned int),
0226                  void *resp_buf, u32 resp_buf_len)
0227 {
0228     struct be_ctrl_info *ctrl = &phba->ctrl;
0229     struct be_mcc_wrb *wrb;
0230     struct be_sge *sge;
0231     unsigned int tag;
0232     int rc = 0;
0233 
0234     mutex_lock(&ctrl->mbox_lock);
0235     wrb = alloc_mcc_wrb(phba, &tag);
0236     if (!wrb) {
0237         mutex_unlock(&ctrl->mbox_lock);
0238         return -ENOMEM;
0239     }
0240 
0241     sge = nonembedded_sgl(wrb);
0242     be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false, 1);
0243     sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
0244     sge->pa_lo = cpu_to_le32(lower_32_bits(nonemb_cmd->dma));
0245     sge->len = cpu_to_le32(nonemb_cmd->size);
0246 
0247     if (cbfn) {
0248         struct be_dma_mem *tag_mem;
0249 
0250         set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
0251         ctrl->ptag_state[tag].cbfn = cbfn;
0252         tag_mem = &phba->ctrl.ptag_state[tag].tag_mem_state;
0253 
0254         /* store DMA mem to be freed in callback */
0255         tag_mem->size = nonemb_cmd->size;
0256         tag_mem->va = nonemb_cmd->va;
0257         tag_mem->dma = nonemb_cmd->dma;
0258     }
0259     be_mcc_notify(phba, tag);
0260     mutex_unlock(&ctrl->mbox_lock);
0261 
0262     /* with cbfn set, its async cmd, don't wait */
0263     if (cbfn)
0264         return 0;
0265 
0266     rc = beiscsi_mccq_compl_wait(phba, tag, NULL, nonemb_cmd);
0267 
0268     /* copy the response, if any */
0269     if (resp_buf)
0270         memcpy(resp_buf, nonemb_cmd->va, resp_buf_len);
0271     return rc;
0272 }
0273 
0274 static int beiscsi_prep_nemb_cmd(struct beiscsi_hba *phba,
0275                  struct be_dma_mem *cmd,
0276                  u8 subsystem, u8 opcode, u32 size)
0277 {
0278     cmd->va = dma_alloc_coherent(&phba->ctrl.pdev->dev, size, &cmd->dma,
0279                      GFP_KERNEL);
0280     if (!cmd->va) {
0281         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
0282                 "BG_%d : Failed to allocate memory for if info\n");
0283         return -ENOMEM;
0284     }
0285     cmd->size = size;
0286     be_cmd_hdr_prepare(cmd->va, subsystem, opcode, size);
0287     beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0288             "BG_%d : subsystem %u cmd %u size %u\n",
0289             subsystem, opcode, size);
0290     return 0;
0291 }
0292 
0293 static void beiscsi_free_nemb_cmd(struct beiscsi_hba *phba,
0294                   struct be_dma_mem *cmd, int rc)
0295 {
0296     /*
0297      * If FW is busy the DMA buffer is saved with the tag. When the cmd
0298      * completes this buffer is freed.
0299      */
0300     if (rc == -EBUSY)
0301         return;
0302 
0303     dma_free_coherent(&phba->ctrl.pdev->dev, cmd->size, cmd->va, cmd->dma);
0304 }
0305 
0306 static void __beiscsi_eq_delay_compl(struct beiscsi_hba *phba, unsigned int tag)
0307 {
0308     struct be_dma_mem *tag_mem;
0309 
0310     /* status is ignored */
0311     __beiscsi_mcc_compl_status(phba, tag, NULL, NULL);
0312     tag_mem = &phba->ctrl.ptag_state[tag].tag_mem_state;
0313     if (tag_mem->size) {
0314         dma_free_coherent(&phba->pcidev->dev, tag_mem->size,
0315                     tag_mem->va, tag_mem->dma);
0316         tag_mem->size = 0;
0317     }
0318 }
0319 
0320 int beiscsi_modify_eq_delay(struct beiscsi_hba *phba,
0321                 struct be_set_eqd *set_eqd, int num)
0322 {
0323     struct be_cmd_req_modify_eq_delay *req;
0324     struct be_dma_mem nonemb_cmd;
0325     int i, rc;
0326 
0327     rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_COMMON,
0328             OPCODE_COMMON_MODIFY_EQ_DELAY, sizeof(*req));
0329     if (rc)
0330         return rc;
0331 
0332     req = nonemb_cmd.va;
0333     req->num_eq = cpu_to_le32(num);
0334     for (i = 0; i < num; i++) {
0335         req->delay[i].eq_id = cpu_to_le32(set_eqd[i].eq_id);
0336         req->delay[i].phase = 0;
0337         req->delay[i].delay_multiplier =
0338                 cpu_to_le32(set_eqd[i].delay_multiplier);
0339     }
0340 
0341     rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, __beiscsi_eq_delay_compl,
0342                    NULL, 0);
0343     if (rc) {
0344         /*
0345          * Only free on failure. Async cmds are handled like -EBUSY
0346          * where it's handled for us.
0347          */
0348         beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0349     }
0350     return rc;
0351 }
0352 
0353 /**
0354  * beiscsi_get_initiator_name - read initiator name from flash
0355  * @phba: device priv structure
0356  * @name: buffer pointer
0357  * @cfg: fetch user configured
0358  *
0359  */
0360 int beiscsi_get_initiator_name(struct beiscsi_hba *phba, char *name, bool cfg)
0361 {
0362     struct be_dma_mem nonemb_cmd;
0363     struct be_cmd_hba_name resp;
0364     struct be_cmd_hba_name *req;
0365     int rc;
0366 
0367     rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI_INI,
0368             OPCODE_ISCSI_INI_CFG_GET_HBA_NAME, sizeof(resp));
0369     if (rc)
0370         return rc;
0371 
0372     req = nonemb_cmd.va;
0373     if (cfg)
0374         req->hdr.version = 1;
0375     rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL,
0376                    &resp, sizeof(resp));
0377     beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0378     if (rc) {
0379         beiscsi_log(phba, KERN_ERR,
0380                 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
0381                 "BS_%d : Initiator Name MBX Failed\n");
0382         return rc;
0383     }
0384     rc = sprintf(name, "%s\n", resp.initiator_name);
0385     return rc;
0386 }
0387 
0388 unsigned int beiscsi_if_get_handle(struct beiscsi_hba *phba)
0389 {
0390     struct be_ctrl_info *ctrl = &phba->ctrl;
0391     struct be_mcc_wrb *wrb;
0392     struct be_cmd_get_all_if_id_req *req;
0393     struct be_cmd_get_all_if_id_req *pbe_allid;
0394     unsigned int tag;
0395     int status = 0;
0396 
0397     if (mutex_lock_interruptible(&ctrl->mbox_lock))
0398         return -EINTR;
0399     wrb = alloc_mcc_wrb(phba, &tag);
0400     if (!wrb) {
0401         mutex_unlock(&ctrl->mbox_lock);
0402         return -ENOMEM;
0403     }
0404 
0405     req = embedded_payload(wrb);
0406     be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
0407     be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
0408                OPCODE_COMMON_ISCSI_NTWK_GET_ALL_IF_ID,
0409                sizeof(*req));
0410     be_mcc_notify(phba, tag);
0411     mutex_unlock(&ctrl->mbox_lock);
0412 
0413     status = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
0414     if (status) {
0415         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
0416                 "BG_%d : %s failed: %d\n", __func__, status);
0417         return -EBUSY;
0418     }
0419 
0420     pbe_allid = embedded_payload(wrb);
0421     /* we now support only one interface per function */
0422     phba->interface_handle = pbe_allid->if_hndl_list[0];
0423 
0424     return status;
0425 }
0426 
0427 static inline bool beiscsi_if_zero_ip(u8 *ip, u32 ip_type)
0428 {
0429     u32 len;
0430 
0431     len = (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN;
0432     while (len && !ip[len - 1])
0433         len--;
0434     return (len == 0);
0435 }
0436 
0437 static int beiscsi_if_mod_gw(struct beiscsi_hba *phba,
0438                  u32 action, u32 ip_type, u8 *gw)
0439 {
0440     struct be_cmd_set_def_gateway_req *req;
0441     struct be_dma_mem nonemb_cmd;
0442     int rt_val;
0443 
0444     rt_val = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
0445             OPCODE_COMMON_ISCSI_NTWK_MODIFY_DEFAULT_GATEWAY,
0446             sizeof(*req));
0447     if (rt_val)
0448         return rt_val;
0449 
0450     req = nonemb_cmd.va;
0451     req->action = action;
0452     req->ip_addr.ip_type = ip_type;
0453     memcpy(req->ip_addr.addr, gw,
0454            (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN);
0455     rt_val = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
0456     beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rt_val);
0457     return rt_val;
0458 }
0459 
0460 int beiscsi_if_set_gw(struct beiscsi_hba *phba, u32 ip_type, u8 *gw)
0461 {
0462     struct be_cmd_get_def_gateway_resp gw_resp;
0463     int rt_val;
0464 
0465     memset(&gw_resp, 0, sizeof(gw_resp));
0466     rt_val = beiscsi_if_get_gw(phba, ip_type, &gw_resp);
0467     if (rt_val) {
0468         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
0469                 "BG_%d : Failed to Get Gateway Addr\n");
0470         return rt_val;
0471     }
0472 
0473     if (!beiscsi_if_zero_ip(gw_resp.ip_addr.addr, ip_type)) {
0474         rt_val = beiscsi_if_mod_gw(phba, IP_ACTION_DEL, ip_type,
0475                        gw_resp.ip_addr.addr);
0476         if (rt_val) {
0477             beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
0478                     "BG_%d : Failed to clear Gateway Addr Set\n");
0479             return rt_val;
0480         }
0481     }
0482 
0483     rt_val = beiscsi_if_mod_gw(phba, IP_ACTION_ADD, ip_type, gw);
0484     if (rt_val)
0485         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
0486                 "BG_%d : Failed to Set Gateway Addr\n");
0487 
0488     return rt_val;
0489 }
0490 
0491 int beiscsi_if_get_gw(struct beiscsi_hba *phba, u32 ip_type,
0492               struct be_cmd_get_def_gateway_resp *resp)
0493 {
0494     struct be_cmd_get_def_gateway_req *req;
0495     struct be_dma_mem nonemb_cmd;
0496     int rc;
0497 
0498     rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
0499             OPCODE_COMMON_ISCSI_NTWK_GET_DEFAULT_GATEWAY,
0500             sizeof(*resp));
0501     if (rc)
0502         return rc;
0503 
0504     req = nonemb_cmd.va;
0505     req->ip_type = ip_type;
0506 
0507     rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, resp,
0508                    sizeof(*resp));
0509     beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0510     return rc;
0511 }
0512 
0513 static int
0514 beiscsi_if_clr_ip(struct beiscsi_hba *phba,
0515           struct be_cmd_get_if_info_resp *if_info)
0516 {
0517     struct be_cmd_set_ip_addr_req *req;
0518     struct be_dma_mem nonemb_cmd;
0519     int rc;
0520 
0521     rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
0522             OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR,
0523             sizeof(*req));
0524     if (rc)
0525         return rc;
0526 
0527     req = nonemb_cmd.va;
0528     req->ip_params.record_entry_count = 1;
0529     req->ip_params.ip_record.action = IP_ACTION_DEL;
0530     req->ip_params.ip_record.interface_hndl =
0531         phba->interface_handle;
0532     req->ip_params.ip_record.ip_addr.size_of_structure =
0533         sizeof(struct be_ip_addr_subnet_format);
0534     req->ip_params.ip_record.ip_addr.ip_type = if_info->ip_addr.ip_type;
0535     memcpy(req->ip_params.ip_record.ip_addr.addr,
0536            if_info->ip_addr.addr,
0537            sizeof(if_info->ip_addr.addr));
0538     memcpy(req->ip_params.ip_record.ip_addr.subnet_mask,
0539            if_info->ip_addr.subnet_mask,
0540            sizeof(if_info->ip_addr.subnet_mask));
0541     rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
0542     if (rc < 0 || req->ip_params.ip_record.status) {
0543         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
0544                 "BG_%d : failed to clear IP: rc %d status %d\n",
0545                 rc, req->ip_params.ip_record.status);
0546     }
0547     beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0548     return rc;
0549 }
0550 
0551 static int
0552 beiscsi_if_set_ip(struct beiscsi_hba *phba, u8 *ip,
0553           u8 *subnet, u32 ip_type)
0554 {
0555     struct be_cmd_set_ip_addr_req *req;
0556     struct be_dma_mem nonemb_cmd;
0557     uint32_t ip_len;
0558     int rc;
0559 
0560     rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
0561             OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR,
0562             sizeof(*req));
0563     if (rc)
0564         return rc;
0565 
0566     req = nonemb_cmd.va;
0567     req->ip_params.record_entry_count = 1;
0568     req->ip_params.ip_record.action = IP_ACTION_ADD;
0569     req->ip_params.ip_record.interface_hndl =
0570         phba->interface_handle;
0571     req->ip_params.ip_record.ip_addr.size_of_structure =
0572         sizeof(struct be_ip_addr_subnet_format);
0573     req->ip_params.ip_record.ip_addr.ip_type = ip_type;
0574     ip_len = (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN;
0575     memcpy(req->ip_params.ip_record.ip_addr.addr, ip, ip_len);
0576     if (subnet)
0577         memcpy(req->ip_params.ip_record.ip_addr.subnet_mask,
0578                subnet, ip_len);
0579 
0580     rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
0581     /**
0582      * In some cases, host needs to look into individual record status
0583      * even though FW reported success for that IOCTL.
0584      */
0585     if (rc < 0 || req->ip_params.ip_record.status) {
0586         __beiscsi_log(phba, KERN_ERR,
0587                 "BG_%d : failed to set IP: rc %d status %d\n",
0588                 rc, req->ip_params.ip_record.status);
0589         if (req->ip_params.ip_record.status)
0590             rc = -EINVAL;
0591     }
0592     beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0593     return rc;
0594 }
0595 
0596 int beiscsi_if_en_static(struct beiscsi_hba *phba, u32 ip_type,
0597              u8 *ip, u8 *subnet)
0598 {
0599     struct be_cmd_get_if_info_resp *if_info;
0600     struct be_cmd_rel_dhcp_req *reldhcp;
0601     struct be_dma_mem nonemb_cmd;
0602     int rc;
0603 
0604     rc = beiscsi_if_get_info(phba, ip_type, &if_info);
0605     if (rc)
0606         return rc;
0607 
0608     if (if_info->dhcp_state) {
0609         rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd,
0610                 CMD_SUBSYSTEM_ISCSI,
0611                 OPCODE_COMMON_ISCSI_NTWK_REL_STATELESS_IP_ADDR,
0612                 sizeof(*reldhcp));
0613         if (rc)
0614             goto exit;
0615 
0616         reldhcp = nonemb_cmd.va;
0617         reldhcp->interface_hndl = phba->interface_handle;
0618         reldhcp->ip_type = ip_type;
0619         rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
0620         beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0621         if (rc < 0) {
0622             beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
0623                     "BG_%d : failed to release existing DHCP: %d\n",
0624                     rc);
0625             goto exit;
0626         }
0627     }
0628 
0629     /* first delete any IP set */
0630     if (!beiscsi_if_zero_ip(if_info->ip_addr.addr, ip_type)) {
0631         rc = beiscsi_if_clr_ip(phba, if_info);
0632         if (rc)
0633             goto exit;
0634     }
0635 
0636     /* if ip == NULL then this is called just to release DHCP IP */
0637     if (ip)
0638         rc = beiscsi_if_set_ip(phba, ip, subnet, ip_type);
0639 exit:
0640     kfree(if_info);
0641     return rc;
0642 }
0643 
0644 int beiscsi_if_en_dhcp(struct beiscsi_hba *phba, u32 ip_type)
0645 {
0646     struct be_cmd_get_def_gateway_resp gw_resp;
0647     struct be_cmd_get_if_info_resp *if_info;
0648     struct be_cmd_set_dhcp_req *dhcpreq;
0649     struct be_dma_mem nonemb_cmd;
0650     u8 *gw;
0651     int rc;
0652 
0653     rc = beiscsi_if_get_info(phba, ip_type, &if_info);
0654     if (rc)
0655         return rc;
0656 
0657     if (if_info->dhcp_state) {
0658         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
0659                 "BG_%d : DHCP Already Enabled\n");
0660         goto exit;
0661     }
0662 
0663     /* first delete any IP set */
0664     if (!beiscsi_if_zero_ip(if_info->ip_addr.addr, ip_type)) {
0665         rc = beiscsi_if_clr_ip(phba, if_info);
0666         if (rc)
0667             goto exit;
0668     }
0669 
0670     /* delete gateway settings if mode change is to DHCP */
0671     memset(&gw_resp, 0, sizeof(gw_resp));
0672     /* use ip_type provided in if_info */
0673     rc = beiscsi_if_get_gw(phba, if_info->ip_addr.ip_type, &gw_resp);
0674     if (rc) {
0675         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
0676                 "BG_%d : Failed to Get Gateway Addr\n");
0677         goto exit;
0678     }
0679     gw = (u8 *)&gw_resp.ip_addr.addr;
0680     if (!beiscsi_if_zero_ip(gw, if_info->ip_addr.ip_type)) {
0681         rc = beiscsi_if_mod_gw(phba, IP_ACTION_DEL,
0682                        if_info->ip_addr.ip_type, gw);
0683         if (rc) {
0684             beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
0685                     "BG_%d : Failed to clear Gateway Addr Set\n");
0686             goto exit;
0687         }
0688     }
0689 
0690     rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
0691             OPCODE_COMMON_ISCSI_NTWK_CONFIG_STATELESS_IP_ADDR,
0692             sizeof(*dhcpreq));
0693     if (rc)
0694         goto exit;
0695 
0696     dhcpreq = nonemb_cmd.va;
0697     dhcpreq->flags = 1; /* 1 - blocking; 0 - non-blocking */
0698     dhcpreq->retry_count = 1;
0699     dhcpreq->interface_hndl = phba->interface_handle;
0700     dhcpreq->ip_type = ip_type;
0701     rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
0702     beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0703 exit:
0704     kfree(if_info);
0705     return rc;
0706 }
0707 
0708 /**
0709  * beiscsi_if_set_vlan()- Issue and wait for CMD completion
0710  * @phba: device private structure instance
0711  * @vlan_tag: VLAN tag
0712  *
0713  * Issue the MBX Cmd and wait for the completion of the
0714  * command.
0715  *
0716  * returns
0717  *  Success: 0
0718  *  Failure: Non-Xero Value
0719  **/
0720 int beiscsi_if_set_vlan(struct beiscsi_hba *phba, uint16_t vlan_tag)
0721 {
0722     int rc;
0723     unsigned int tag;
0724 
0725     tag = be_cmd_set_vlan(phba, vlan_tag);
0726     if (!tag) {
0727         beiscsi_log(phba, KERN_ERR,
0728                 (BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX),
0729                 "BG_%d : VLAN Setting Failed\n");
0730         return -EBUSY;
0731     }
0732 
0733     rc = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
0734     if (rc) {
0735         beiscsi_log(phba, KERN_ERR,
0736                 (BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX),
0737                 "BS_%d : VLAN MBX Cmd Failed\n");
0738         return rc;
0739     }
0740     return rc;
0741 }
0742 
0743 
0744 int beiscsi_if_get_info(struct beiscsi_hba *phba, int ip_type,
0745             struct be_cmd_get_if_info_resp **if_info)
0746 {
0747     struct be_cmd_get_if_info_req *req;
0748     struct be_dma_mem nonemb_cmd;
0749     uint32_t ioctl_size = sizeof(struct be_cmd_get_if_info_resp);
0750     int rc;
0751 
0752     rc = beiscsi_if_get_handle(phba);
0753     if (rc)
0754         return rc;
0755 
0756     do {
0757         rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd,
0758                 CMD_SUBSYSTEM_ISCSI,
0759                 OPCODE_COMMON_ISCSI_NTWK_GET_IF_INFO,
0760                 ioctl_size);
0761         if (rc)
0762             return rc;
0763 
0764         req = nonemb_cmd.va;
0765         req->interface_hndl = phba->interface_handle;
0766         req->ip_type = ip_type;
0767 
0768         /* Allocate memory for if_info */
0769         *if_info = kzalloc(ioctl_size, GFP_KERNEL);
0770         if (!*if_info) {
0771             beiscsi_log(phba, KERN_ERR,
0772                     BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
0773                     "BG_%d : Memory Allocation Failure\n");
0774 
0775                 beiscsi_free_nemb_cmd(phba, &nonemb_cmd,
0776                               -ENOMEM);
0777                 return -ENOMEM;
0778         }
0779 
0780         rc =  beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, *if_info,
0781                         ioctl_size);
0782 
0783         /* Check if the error is because of Insufficent_Buffer */
0784         if (rc == -EAGAIN) {
0785 
0786             /* Get the new memory size */
0787             ioctl_size = ((struct be_cmd_resp_hdr *)
0788                       nonemb_cmd.va)->actual_resp_len;
0789             ioctl_size += sizeof(struct be_cmd_req_hdr);
0790 
0791             beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0792             /* Free the virtual memory */
0793             kfree(*if_info);
0794         } else {
0795             beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0796             break;
0797         }
0798     } while (true);
0799     return rc;
0800 }
0801 
0802 int mgmt_get_nic_conf(struct beiscsi_hba *phba,
0803               struct be_cmd_get_nic_conf_resp *nic)
0804 {
0805     struct be_dma_mem nonemb_cmd;
0806     int rc;
0807 
0808     rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
0809             OPCODE_COMMON_ISCSI_NTWK_GET_NIC_CONFIG,
0810             sizeof(*nic));
0811     if (rc)
0812         return rc;
0813 
0814     rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, nic, sizeof(*nic));
0815     beiscsi_free_nemb_cmd(phba, &nonemb_cmd, rc);
0816     return rc;
0817 }
0818 
0819 static void beiscsi_boot_process_compl(struct beiscsi_hba *phba,
0820                        unsigned int tag)
0821 {
0822     struct be_cmd_get_boot_target_resp *boot_resp;
0823     struct be_cmd_resp_logout_fw_sess *logo_resp;
0824     struct be_cmd_get_session_resp *sess_resp;
0825     struct be_mcc_wrb *wrb;
0826     struct boot_struct *bs;
0827     int boot_work, status;
0828 
0829     if (!test_bit(BEISCSI_HBA_BOOT_WORK, &phba->state)) {
0830         __beiscsi_log(phba, KERN_ERR,
0831                   "BG_%d : %s no boot work %lx\n",
0832                   __func__, phba->state);
0833         return;
0834     }
0835 
0836     if (phba->boot_struct.tag != tag) {
0837         __beiscsi_log(phba, KERN_ERR,
0838                   "BG_%d : %s tag mismatch %d:%d\n",
0839                   __func__, tag, phba->boot_struct.tag);
0840         return;
0841     }
0842     bs = &phba->boot_struct;
0843     boot_work = 1;
0844     status = 0;
0845     switch (bs->action) {
0846     case BEISCSI_BOOT_REOPEN_SESS:
0847         status = __beiscsi_mcc_compl_status(phba, tag, NULL, NULL);
0848         if (!status)
0849             bs->action = BEISCSI_BOOT_GET_SHANDLE;
0850         else
0851             bs->retry--;
0852         break;
0853     case BEISCSI_BOOT_GET_SHANDLE:
0854         status = __beiscsi_mcc_compl_status(phba, tag, &wrb, NULL);
0855         if (!status) {
0856             boot_resp = embedded_payload(wrb);
0857             bs->s_handle = boot_resp->boot_session_handle;
0858         }
0859         if (bs->s_handle == BE_BOOT_INVALID_SHANDLE) {
0860             bs->action = BEISCSI_BOOT_REOPEN_SESS;
0861             bs->retry--;
0862         } else {
0863             bs->action = BEISCSI_BOOT_GET_SINFO;
0864         }
0865         break;
0866     case BEISCSI_BOOT_GET_SINFO:
0867         status = __beiscsi_mcc_compl_status(phba, tag, NULL,
0868                             &bs->nonemb_cmd);
0869         if (!status) {
0870             sess_resp = bs->nonemb_cmd.va;
0871             memcpy(&bs->boot_sess, &sess_resp->session_info,
0872                    sizeof(struct mgmt_session_info));
0873             bs->action = BEISCSI_BOOT_LOGOUT_SESS;
0874         } else {
0875             __beiscsi_log(phba, KERN_ERR,
0876                       "BG_%d : get boot session info error : 0x%x\n",
0877                       status);
0878             boot_work = 0;
0879         }
0880         dma_free_coherent(&phba->ctrl.pdev->dev, bs->nonemb_cmd.size,
0881                     bs->nonemb_cmd.va, bs->nonemb_cmd.dma);
0882         bs->nonemb_cmd.va = NULL;
0883         break;
0884     case BEISCSI_BOOT_LOGOUT_SESS:
0885         status = __beiscsi_mcc_compl_status(phba, tag, &wrb, NULL);
0886         if (!status) {
0887             logo_resp = embedded_payload(wrb);
0888             if (logo_resp->session_status != BE_SESS_STATUS_CLOSE) {
0889                 __beiscsi_log(phba, KERN_ERR,
0890                           "BG_%d : FW boot session logout error : 0x%x\n",
0891                           logo_resp->session_status);
0892             }
0893         }
0894         /* continue to create boot_kset even if logout failed? */
0895         bs->action = BEISCSI_BOOT_CREATE_KSET;
0896         break;
0897     default:
0898         break;
0899     }
0900 
0901     /* clear the tag so no other completion matches this tag */
0902     bs->tag = 0;
0903     if (!bs->retry) {
0904         boot_work = 0;
0905         __beiscsi_log(phba, KERN_ERR,
0906                   "BG_%d : failed to setup boot target: status %d action %d\n",
0907                   status, bs->action);
0908     }
0909     if (!boot_work) {
0910         /* wait for next event to start boot_work */
0911         clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
0912         return;
0913     }
0914     schedule_work(&phba->boot_work);
0915 }
0916 
0917 /**
0918  * beiscsi_boot_logout_sess()- Logout from boot FW session
0919  * @phba: Device priv structure instance
0920  *
0921  * return
0922  *  the TAG used for MBOX Command
0923  *
0924  */
0925 unsigned int beiscsi_boot_logout_sess(struct beiscsi_hba *phba)
0926 {
0927     struct be_ctrl_info *ctrl = &phba->ctrl;
0928     struct be_mcc_wrb *wrb;
0929     struct be_cmd_req_logout_fw_sess *req;
0930     unsigned int tag;
0931 
0932     mutex_lock(&ctrl->mbox_lock);
0933     wrb = alloc_mcc_wrb(phba, &tag);
0934     if (!wrb) {
0935         mutex_unlock(&ctrl->mbox_lock);
0936         return 0;
0937     }
0938 
0939     req = embedded_payload(wrb);
0940     be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
0941     be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
0942                OPCODE_ISCSI_INI_SESSION_LOGOUT_TARGET,
0943                sizeof(struct be_cmd_req_logout_fw_sess));
0944     /* Use the session handle copied into boot_sess */
0945     req->session_handle = phba->boot_struct.boot_sess.session_handle;
0946 
0947     phba->boot_struct.tag = tag;
0948     set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
0949     ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
0950 
0951     be_mcc_notify(phba, tag);
0952     mutex_unlock(&ctrl->mbox_lock);
0953 
0954     return tag;
0955 }
0956 /**
0957  * beiscsi_boot_reopen_sess()- Reopen boot session
0958  * @phba: Device priv structure instance
0959  *
0960  * return
0961  *  the TAG used for MBOX Command
0962  *
0963  **/
0964 unsigned int beiscsi_boot_reopen_sess(struct beiscsi_hba *phba)
0965 {
0966     struct be_ctrl_info *ctrl = &phba->ctrl;
0967     struct be_mcc_wrb *wrb;
0968     struct be_cmd_reopen_session_req *req;
0969     unsigned int tag;
0970 
0971     mutex_lock(&ctrl->mbox_lock);
0972     wrb = alloc_mcc_wrb(phba, &tag);
0973     if (!wrb) {
0974         mutex_unlock(&ctrl->mbox_lock);
0975         return 0;
0976     }
0977 
0978     req = embedded_payload(wrb);
0979     be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
0980     be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
0981                OPCODE_ISCSI_INI_DRIVER_REOPEN_ALL_SESSIONS,
0982                sizeof(struct be_cmd_reopen_session_resp));
0983     req->reopen_type = BE_REOPEN_BOOT_SESSIONS;
0984     req->session_handle = BE_BOOT_INVALID_SHANDLE;
0985 
0986     phba->boot_struct.tag = tag;
0987     set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
0988     ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
0989 
0990     be_mcc_notify(phba, tag);
0991     mutex_unlock(&ctrl->mbox_lock);
0992     return tag;
0993 }
0994 
0995 
0996 /**
0997  * beiscsi_boot_get_sinfo()- Get boot session info
0998  * @phba: device priv structure instance
0999  *
1000  * Fetches the boot_struct.s_handle info from FW.
1001  * return
1002  *  the TAG used for MBOX Command
1003  *
1004  **/
1005 unsigned int beiscsi_boot_get_sinfo(struct beiscsi_hba *phba)
1006 {
1007     struct be_ctrl_info *ctrl = &phba->ctrl;
1008     struct be_cmd_get_session_req *req;
1009     struct be_dma_mem *nonemb_cmd;
1010     struct be_mcc_wrb *wrb;
1011     struct be_sge *sge;
1012     unsigned int tag;
1013 
1014     mutex_lock(&ctrl->mbox_lock);
1015     wrb = alloc_mcc_wrb(phba, &tag);
1016     if (!wrb) {
1017         mutex_unlock(&ctrl->mbox_lock);
1018         return 0;
1019     }
1020 
1021     nonemb_cmd = &phba->boot_struct.nonemb_cmd;
1022     nonemb_cmd->size = sizeof(struct be_cmd_get_session_resp);
1023     nonemb_cmd->va = dma_alloc_coherent(&phba->ctrl.pdev->dev,
1024                           nonemb_cmd->size,
1025                           &nonemb_cmd->dma,
1026                           GFP_KERNEL);
1027     if (!nonemb_cmd->va) {
1028         mutex_unlock(&ctrl->mbox_lock);
1029         return 0;
1030     }
1031 
1032     req = nonemb_cmd->va;
1033     memset(req, 0, sizeof(*req));
1034     sge = nonembedded_sgl(wrb);
1035     be_wrb_hdr_prepare(wrb, sizeof(*req), false, 1);
1036     be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1037                OPCODE_ISCSI_INI_SESSION_GET_A_SESSION,
1038                sizeof(struct be_cmd_get_session_resp));
1039     req->session_handle = phba->boot_struct.s_handle;
1040     sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
1041     sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
1042     sge->len = cpu_to_le32(nonemb_cmd->size);
1043 
1044     phba->boot_struct.tag = tag;
1045     set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1046     ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1047 
1048     be_mcc_notify(phba, tag);
1049     mutex_unlock(&ctrl->mbox_lock);
1050     return tag;
1051 }
1052 
1053 unsigned int __beiscsi_boot_get_shandle(struct beiscsi_hba *phba, int async)
1054 {
1055     struct be_ctrl_info *ctrl = &phba->ctrl;
1056     struct be_mcc_wrb *wrb;
1057     struct be_cmd_get_boot_target_req *req;
1058     unsigned int tag;
1059 
1060     mutex_lock(&ctrl->mbox_lock);
1061     wrb = alloc_mcc_wrb(phba, &tag);
1062     if (!wrb) {
1063         mutex_unlock(&ctrl->mbox_lock);
1064         return 0;
1065     }
1066 
1067     req = embedded_payload(wrb);
1068     be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
1069     be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1070                OPCODE_ISCSI_INI_BOOT_GET_BOOT_TARGET,
1071                sizeof(struct be_cmd_get_boot_target_resp));
1072 
1073     if (async) {
1074         phba->boot_struct.tag = tag;
1075         set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1076         ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1077     }
1078 
1079     be_mcc_notify(phba, tag);
1080     mutex_unlock(&ctrl->mbox_lock);
1081     return tag;
1082 }
1083 
1084 /**
1085  * beiscsi_boot_get_shandle()- Get boot session handle
1086  * @phba: device priv structure instance
1087  * @s_handle: session handle returned for boot session.
1088  *
1089  * return
1090  *  Success: 1
1091  *  Failure: negative
1092  *
1093  **/
1094 int beiscsi_boot_get_shandle(struct beiscsi_hba *phba, unsigned int *s_handle)
1095 {
1096     struct be_cmd_get_boot_target_resp *boot_resp;
1097     struct be_mcc_wrb *wrb;
1098     unsigned int tag;
1099     int rc;
1100 
1101     *s_handle = BE_BOOT_INVALID_SHANDLE;
1102     /* get configured boot session count and handle */
1103     tag = __beiscsi_boot_get_shandle(phba, 0);
1104     if (!tag) {
1105         beiscsi_log(phba, KERN_ERR,
1106                 BEISCSI_LOG_CONFIG | BEISCSI_LOG_INIT,
1107                 "BG_%d : Getting Boot Target Info Failed\n");
1108         return -EAGAIN;
1109     }
1110 
1111     rc = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
1112     if (rc) {
1113         beiscsi_log(phba, KERN_ERR,
1114                 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
1115                 "BG_%d : MBX CMD get_boot_target Failed\n");
1116         return -EBUSY;
1117     }
1118 
1119     boot_resp = embedded_payload(wrb);
1120     /* check if there are any boot targets configured */
1121     if (!boot_resp->boot_session_count) {
1122         __beiscsi_log(phba, KERN_INFO,
1123                   "BG_%d : No boot targets configured\n");
1124         return -ENXIO;
1125     }
1126 
1127     /* only if FW has logged in to the boot target, s_handle is valid */
1128     *s_handle = boot_resp->boot_session_handle;
1129     return 1;
1130 }
1131 
1132 /**
1133  * beiscsi_drvr_ver_disp()- Display the driver Name and Version
1134  * @dev: ptr to device not used.
1135  * @attr: device attribute, not used.
1136  * @buf: contains formatted text driver name and version
1137  *
1138  * return
1139  * size of the formatted string
1140  **/
1141 ssize_t
1142 beiscsi_drvr_ver_disp(struct device *dev, struct device_attribute *attr,
1143                char *buf)
1144 {
1145     return snprintf(buf, PAGE_SIZE, BE_NAME "\n");
1146 }
1147 
1148 /**
1149  * beiscsi_fw_ver_disp()- Display Firmware Version
1150  * @dev: ptr to device not used.
1151  * @attr: device attribute, not used.
1152  * @buf: contains formatted text Firmware version
1153  *
1154  * return
1155  * size of the formatted string
1156  **/
1157 ssize_t
1158 beiscsi_fw_ver_disp(struct device *dev, struct device_attribute *attr,
1159              char *buf)
1160 {
1161     struct Scsi_Host *shost = class_to_shost(dev);
1162     struct beiscsi_hba *phba = iscsi_host_priv(shost);
1163 
1164     return snprintf(buf, PAGE_SIZE, "%s\n", phba->fw_ver_str);
1165 }
1166 
1167 /**
1168  * beiscsi_active_session_disp()- Display Sessions Active
1169  * @dev: ptr to device not used.
1170  * @attr: device attribute, not used.
1171  * @buf: contains formatted text Session Count
1172  *
1173  * return
1174  * size of the formatted string
1175  **/
1176 ssize_t
1177 beiscsi_active_session_disp(struct device *dev, struct device_attribute *attr,
1178              char *buf)
1179 {
1180     struct Scsi_Host *shost = class_to_shost(dev);
1181     struct beiscsi_hba *phba = iscsi_host_priv(shost);
1182     uint16_t avlbl_cids = 0, ulp_num, len = 0, total_cids = 0;
1183 
1184     for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
1185         if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
1186             avlbl_cids = BEISCSI_ULP_AVLBL_CID(phba, ulp_num);
1187             total_cids = BEISCSI_GET_CID_COUNT(phba, ulp_num);
1188             len += scnprintf(buf+len, PAGE_SIZE - len,
1189                      "ULP%d : %d\n", ulp_num,
1190                      (total_cids - avlbl_cids));
1191         } else
1192             len += scnprintf(buf+len, PAGE_SIZE - len,
1193                      "ULP%d : %d\n", ulp_num, 0);
1194     }
1195 
1196     return len;
1197 }
1198 
1199 /**
1200  * beiscsi_free_session_disp()- Display Avaliable Session
1201  * @dev: ptr to device not used.
1202  * @attr: device attribute, not used.
1203  * @buf: contains formatted text Session Count
1204  *
1205  * return
1206  * size of the formatted string
1207  **/
1208 ssize_t
1209 beiscsi_free_session_disp(struct device *dev, struct device_attribute *attr,
1210                char *buf)
1211 {
1212     struct Scsi_Host *shost = class_to_shost(dev);
1213     struct beiscsi_hba *phba = iscsi_host_priv(shost);
1214     uint16_t ulp_num, len = 0;
1215 
1216     for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
1217         if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported))
1218             len += scnprintf(buf+len, PAGE_SIZE - len,
1219                      "ULP%d : %d\n", ulp_num,
1220                      BEISCSI_ULP_AVLBL_CID(phba, ulp_num));
1221         else
1222             len += scnprintf(buf+len, PAGE_SIZE - len,
1223                      "ULP%d : %d\n", ulp_num, 0);
1224     }
1225 
1226     return len;
1227 }
1228 
1229 /**
1230  * beiscsi_adap_family_disp()- Display adapter family.
1231  * @dev: ptr to device to get priv structure
1232  * @attr: device attribute, not used.
1233  * @buf: contains formatted text driver name and version
1234  *
1235  * return
1236  * size of the formatted string
1237  **/
1238 ssize_t
1239 beiscsi_adap_family_disp(struct device *dev, struct device_attribute *attr,
1240               char *buf)
1241 {
1242     uint16_t dev_id = 0;
1243     struct Scsi_Host *shost = class_to_shost(dev);
1244     struct beiscsi_hba *phba = iscsi_host_priv(shost);
1245 
1246     dev_id = phba->pcidev->device;
1247     switch (dev_id) {
1248     case BE_DEVICE_ID1:
1249     case OC_DEVICE_ID1:
1250     case OC_DEVICE_ID2:
1251         return snprintf(buf, PAGE_SIZE,
1252                 "Obsolete/Unsupported BE2 Adapter Family\n");
1253     case BE_DEVICE_ID2:
1254     case OC_DEVICE_ID3:
1255         return snprintf(buf, PAGE_SIZE, "BE3-R Adapter Family\n");
1256     case OC_SKH_ID1:
1257         return snprintf(buf, PAGE_SIZE, "Skyhawk-R Adapter Family\n");
1258     default:
1259         return snprintf(buf, PAGE_SIZE,
1260                 "Unknown Adapter Family: 0x%x\n", dev_id);
1261     }
1262 }
1263 
1264 /**
1265  * beiscsi_phys_port_disp()- Display Physical Port Identifier
1266  * @dev: ptr to device not used.
1267  * @attr: device attribute, not used.
1268  * @buf: contains formatted text port identifier
1269  *
1270  * return
1271  * size of the formatted string
1272  **/
1273 ssize_t
1274 beiscsi_phys_port_disp(struct device *dev, struct device_attribute *attr,
1275              char *buf)
1276 {
1277     struct Scsi_Host *shost = class_to_shost(dev);
1278     struct beiscsi_hba *phba = iscsi_host_priv(shost);
1279 
1280     return snprintf(buf, PAGE_SIZE, "Port Identifier : %u\n",
1281             phba->fw_config.phys_port);
1282 }
1283 
1284 void beiscsi_offload_cxn_v0(struct beiscsi_offload_params *params,
1285                  struct wrb_handle *pwrb_handle,
1286                  struct be_mem_descriptor *mem_descr,
1287                  struct hwi_wrb_context *pwrb_context)
1288 {
1289     struct iscsi_wrb *pwrb = pwrb_handle->pwrb;
1290 
1291     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1292               max_send_data_segment_length, pwrb,
1293               params->dw[offsetof(struct amap_beiscsi_offload_params,
1294               max_send_data_segment_length) / 32]);
1295     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
1296               BE_TGT_CTX_UPDT_CMD);
1297     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1298               first_burst_length,
1299               pwrb,
1300               params->dw[offsetof(struct amap_beiscsi_offload_params,
1301               first_burst_length) / 32]);
1302     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
1303               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1304               erl) / 32] & OFFLD_PARAMS_ERL));
1305     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
1306               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1307                dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
1308     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
1309               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1310               hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
1311     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
1312               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1313               ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
1314     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
1315               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1316               imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
1317     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
1318               pwrb,
1319               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1320               exp_statsn) / 32] + 1));
1321     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
1322               pwrb, pwrb_handle->wrb_index);
1323 
1324     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1325               max_burst_length, pwrb, params->dw[offsetof
1326               (struct amap_beiscsi_offload_params,
1327               max_burst_length) / 32]);
1328 
1329     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
1330               pwrb, pwrb_handle->wrb_index);
1331     if (pwrb_context->plast_wrb)
1332         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1333                   ptr2nextwrb,
1334                   pwrb_context->plast_wrb,
1335                   pwrb_handle->wrb_index);
1336     pwrb_context->plast_wrb = pwrb;
1337 
1338     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1339               session_state, pwrb, 0);
1340     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
1341               pwrb, 1);
1342     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
1343               pwrb, 0);
1344     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
1345               0);
1346 
1347     mem_descr += ISCSI_MEM_GLOBAL_HEADER;
1348     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1349               pad_buffer_addr_hi, pwrb,
1350               mem_descr->mem_array[0].bus_address.u.a32.address_hi);
1351     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1352               pad_buffer_addr_lo, pwrb,
1353               mem_descr->mem_array[0].bus_address.u.a32.address_lo);
1354 }
1355 
1356 void beiscsi_offload_cxn_v2(struct beiscsi_offload_params *params,
1357                  struct wrb_handle *pwrb_handle,
1358                  struct hwi_wrb_context *pwrb_context)
1359 {
1360     struct iscsi_wrb *pwrb = pwrb_handle->pwrb;
1361 
1362     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1363               max_burst_length, pwrb, params->dw[offsetof
1364               (struct amap_beiscsi_offload_params,
1365               max_burst_length) / 32]);
1366     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1367               type, pwrb,
1368               BE_TGT_CTX_UPDT_CMD);
1369     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1370               ptr2nextwrb,
1371               pwrb, pwrb_handle->wrb_index);
1372     if (pwrb_context->plast_wrb)
1373         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1374                   ptr2nextwrb,
1375                   pwrb_context->plast_wrb,
1376                   pwrb_handle->wrb_index);
1377     pwrb_context->plast_wrb = pwrb;
1378 
1379     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, wrb_idx,
1380               pwrb, pwrb_handle->wrb_index);
1381     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1382               max_send_data_segment_length, pwrb,
1383               params->dw[offsetof(struct amap_beiscsi_offload_params,
1384               max_send_data_segment_length) / 32]);
1385     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1386               first_burst_length, pwrb,
1387               params->dw[offsetof(struct amap_beiscsi_offload_params,
1388               first_burst_length) / 32]);
1389     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1390               max_recv_dataseg_len, pwrb,
1391               params->dw[offsetof(struct amap_beiscsi_offload_params,
1392               max_recv_data_segment_length) / 32]);
1393     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1394               max_cxns, pwrb, BEISCSI_MAX_CXNS);
1395     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, erl, pwrb,
1396               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1397               erl) / 32] & OFFLD_PARAMS_ERL));
1398     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, dde, pwrb,
1399               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1400               dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
1401     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, hde, pwrb,
1402               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1403               hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
1404     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1405               ir2t, pwrb,
1406               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1407               ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
1408     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, imd, pwrb,
1409               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1410               imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
1411     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1412               data_seq_inorder,
1413               pwrb,
1414               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1415               data_seq_inorder) / 32] &
1416               OFFLD_PARAMS_DATA_SEQ_INORDER) >> 6);
1417     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1418               pdu_seq_inorder,
1419               pwrb,
1420               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1421               pdu_seq_inorder) / 32] &
1422               OFFLD_PARAMS_PDU_SEQ_INORDER) >> 7);
1423     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, max_r2t,
1424               pwrb,
1425               (params->dw[offsetof(struct amap_beiscsi_offload_params,
1426               max_r2t) / 32] &
1427               OFFLD_PARAMS_MAX_R2T) >> 8);
1428     AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, stat_sn,
1429               pwrb,
1430              (params->dw[offsetof(struct amap_beiscsi_offload_params,
1431               exp_statsn) / 32] + 1));
1432 }
1433 
1434 unsigned int beiscsi_invalidate_cxn(struct beiscsi_hba *phba,
1435                     struct beiscsi_endpoint *beiscsi_ep)
1436 {
1437     struct be_invalidate_connection_params_in *req;
1438     struct be_ctrl_info *ctrl = &phba->ctrl;
1439     struct be_mcc_wrb *wrb;
1440     unsigned int tag = 0;
1441 
1442     mutex_lock(&ctrl->mbox_lock);
1443     wrb = alloc_mcc_wrb(phba, &tag);
1444     if (!wrb) {
1445         mutex_unlock(&ctrl->mbox_lock);
1446         return 0;
1447     }
1448 
1449     req = embedded_payload(wrb);
1450     be_wrb_hdr_prepare(wrb, sizeof(union be_invalidate_connection_params),
1451                true, 0);
1452     be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1453                OPCODE_ISCSI_INI_DRIVER_INVALIDATE_CONNECTION,
1454                sizeof(*req));
1455     req->session_handle = beiscsi_ep->fw_handle;
1456     req->cid = beiscsi_ep->ep_cid;
1457     if (beiscsi_ep->conn)
1458         req->cleanup_type = BE_CLEANUP_TYPE_INVALIDATE;
1459     else
1460         req->cleanup_type = BE_CLEANUP_TYPE_ISSUE_TCP_RST;
1461     /**
1462      * 0 - non-persistent targets
1463      * 1 - save session info on flash
1464      */
1465     req->save_cfg = 0;
1466     be_mcc_notify(phba, tag);
1467     mutex_unlock(&ctrl->mbox_lock);
1468     return tag;
1469 }
1470 
1471 unsigned int beiscsi_upload_cxn(struct beiscsi_hba *phba,
1472                 struct beiscsi_endpoint *beiscsi_ep)
1473 {
1474     struct be_ctrl_info *ctrl = &phba->ctrl;
1475     struct be_mcc_wrb *wrb;
1476     struct be_tcp_upload_params_in *req;
1477     unsigned int tag;
1478 
1479     mutex_lock(&ctrl->mbox_lock);
1480     wrb = alloc_mcc_wrb(phba, &tag);
1481     if (!wrb) {
1482         mutex_unlock(&ctrl->mbox_lock);
1483         return 0;
1484     }
1485 
1486     req = embedded_payload(wrb);
1487     be_wrb_hdr_prepare(wrb, sizeof(union be_tcp_upload_params), true, 0);
1488     be_cmd_hdr_prepare(&req->hdr, CMD_COMMON_TCP_UPLOAD,
1489                OPCODE_COMMON_TCP_UPLOAD, sizeof(*req));
1490     req->id = beiscsi_ep->ep_cid;
1491     if (beiscsi_ep->conn)
1492         req->upload_type = BE_UPLOAD_TYPE_GRACEFUL;
1493     else
1494         req->upload_type = BE_UPLOAD_TYPE_ABORT;
1495     be_mcc_notify(phba, tag);
1496     mutex_unlock(&ctrl->mbox_lock);
1497     return tag;
1498 }
1499 
1500 int beiscsi_mgmt_invalidate_icds(struct beiscsi_hba *phba,
1501                  struct invldt_cmd_tbl *inv_tbl,
1502                  unsigned int nents)
1503 {
1504     struct be_ctrl_info *ctrl = &phba->ctrl;
1505     struct invldt_cmds_params_in *req;
1506     struct be_dma_mem nonemb_cmd;
1507     struct be_mcc_wrb *wrb;
1508     unsigned int i, tag;
1509     struct be_sge *sge;
1510     int rc;
1511 
1512     if (!nents || nents > BE_INVLDT_CMD_TBL_SZ)
1513         return -EINVAL;
1514 
1515     nonemb_cmd.size = sizeof(union be_invldt_cmds_params);
1516     nonemb_cmd.va = dma_alloc_coherent(&phba->ctrl.pdev->dev,
1517                        nonemb_cmd.size, &nonemb_cmd.dma,
1518                        GFP_KERNEL);
1519     if (!nonemb_cmd.va) {
1520         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
1521                 "BM_%d : invldt_cmds_params alloc failed\n");
1522         return -ENOMEM;
1523     }
1524 
1525     mutex_lock(&ctrl->mbox_lock);
1526     wrb = alloc_mcc_wrb(phba, &tag);
1527     if (!wrb) {
1528         mutex_unlock(&ctrl->mbox_lock);
1529         dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
1530                     nonemb_cmd.va, nonemb_cmd.dma);
1531         return -ENOMEM;
1532     }
1533 
1534     req = nonemb_cmd.va;
1535     be_wrb_hdr_prepare(wrb, nonemb_cmd.size, false, 1);
1536     be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
1537             OPCODE_COMMON_ISCSI_ERROR_RECOVERY_INVALIDATE_COMMANDS,
1538             sizeof(*req));
1539     req->ref_handle = 0;
1540     req->cleanup_type = CMD_ISCSI_COMMAND_INVALIDATE;
1541     for (i = 0; i < nents; i++) {
1542         req->table[i].icd = inv_tbl[i].icd;
1543         req->table[i].cid = inv_tbl[i].cid;
1544         req->icd_count++;
1545     }
1546     sge = nonembedded_sgl(wrb);
1547     sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd.dma));
1548     sge->pa_lo = cpu_to_le32(lower_32_bits(nonemb_cmd.dma));
1549     sge->len = cpu_to_le32(nonemb_cmd.size);
1550 
1551     be_mcc_notify(phba, tag);
1552     mutex_unlock(&ctrl->mbox_lock);
1553 
1554     rc = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
1555     if (rc != -EBUSY)
1556         dma_free_coherent(&phba->ctrl.pdev->dev, nonemb_cmd.size,
1557                     nonemb_cmd.va, nonemb_cmd.dma);
1558     return rc;
1559 }