scsi: be2iscsi: Add IOCTL to check UER supported
[cascardo/linux.git] / drivers / scsi / be2iscsi / be_mgmt.c
1 /**
2  * Copyright (C) 2005 - 2015 Emulex
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.  The full GNU General
8  * Public License is included in this distribution in the file called COPYING.
9  *
10  * Written by: Jayamohan Kallickal (jayamohan.kallickal@avagotech.com)
11  *
12  * Contact Information:
13  * linux-drivers@avagotech.com
14  *
15  * Emulex
16  * 3333 Susan Street
17  * Costa Mesa, CA 92626
18  */
19
20 #include <linux/bsg-lib.h>
21 #include <scsi/scsi_transport_iscsi.h>
22 #include <scsi/scsi_bsg_iscsi.h>
23 #include "be_mgmt.h"
24 #include "be_iscsi.h"
25 #include "be_main.h"
26
27 /* UE Status Low CSR */
28 static const char * const desc_ue_status_low[] = {
29         "CEV",
30         "CTX",
31         "DBUF",
32         "ERX",
33         "Host",
34         "MPU",
35         "NDMA",
36         "PTC ",
37         "RDMA ",
38         "RXF ",
39         "RXIPS ",
40         "RXULP0 ",
41         "RXULP1 ",
42         "RXULP2 ",
43         "TIM ",
44         "TPOST ",
45         "TPRE ",
46         "TXIPS ",
47         "TXULP0 ",
48         "TXULP1 ",
49         "UC ",
50         "WDMA ",
51         "TXULP2 ",
52         "HOST1 ",
53         "P0_OB_LINK ",
54         "P1_OB_LINK ",
55         "HOST_GPIO ",
56         "MBOX ",
57         "AXGMAC0",
58         "AXGMAC1",
59         "JTAG",
60         "MPU_INTPEND"
61 };
62
63 /* UE Status High CSR */
64 static const char * const desc_ue_status_hi[] = {
65         "LPCMEMHOST",
66         "MGMT_MAC",
67         "PCS0ONLINE",
68         "MPU_IRAM",
69         "PCS1ONLINE",
70         "PCTL0",
71         "PCTL1",
72         "PMEM",
73         "RR",
74         "TXPB",
75         "RXPP",
76         "XAUI",
77         "TXP",
78         "ARM",
79         "IPC",
80         "HOST2",
81         "HOST3",
82         "HOST4",
83         "HOST5",
84         "HOST6",
85         "HOST7",
86         "HOST8",
87         "HOST9",
88         "NETC",
89         "Unknown",
90         "Unknown",
91         "Unknown",
92         "Unknown",
93         "Unknown",
94         "Unknown",
95         "Unknown",
96         "Unknown"
97 };
98
99 /*
100  * beiscsi_ue_detect()- Detect Unrecoverable Error on adapter
101  * @phba: Driver priv structure
102  *
103  * Read registers linked to UE and check for the UE status
104  **/
105 void beiscsi_ue_detect(struct beiscsi_hba *phba)
106 {
107         uint32_t ue_hi = 0, ue_lo = 0;
108         uint32_t ue_mask_hi = 0, ue_mask_lo = 0;
109         uint8_t i = 0;
110
111         pci_read_config_dword(phba->pcidev,
112                               PCICFG_UE_STATUS_LOW, &ue_lo);
113         pci_read_config_dword(phba->pcidev,
114                               PCICFG_UE_STATUS_MASK_LOW,
115                               &ue_mask_lo);
116         pci_read_config_dword(phba->pcidev,
117                               PCICFG_UE_STATUS_HIGH,
118                               &ue_hi);
119         pci_read_config_dword(phba->pcidev,
120                               PCICFG_UE_STATUS_MASK_HI,
121                               &ue_mask_hi);
122
123         ue_lo = (ue_lo & ~ue_mask_lo);
124         ue_hi = (ue_hi & ~ue_mask_hi);
125
126
127         if (ue_lo || ue_hi) {
128                 set_bit(BEISCSI_HBA_IN_UE, &phba->state);
129                 beiscsi_log(phba, KERN_ERR,
130                             BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
131                             "BG_%d : HBA error detected\n");
132         }
133
134         if (ue_lo) {
135                 for (i = 0; ue_lo; ue_lo >>= 1, i++) {
136                         if (ue_lo & 1)
137                                 beiscsi_log(phba, KERN_ERR,
138                                             BEISCSI_LOG_CONFIG,
139                                             "BG_%d : UE_LOW %s bit set\n",
140                                             desc_ue_status_low[i]);
141                 }
142         }
143
144         if (ue_hi) {
145                 for (i = 0; ue_hi; ue_hi >>= 1, i++) {
146                         if (ue_hi & 1)
147                                 beiscsi_log(phba, KERN_ERR,
148                                             BEISCSI_LOG_CONFIG,
149                                             "BG_%d : UE_HIGH %s bit set\n",
150                                             desc_ue_status_hi[i]);
151                 }
152         }
153 }
154
155 int beiscsi_modify_eq_delay(struct beiscsi_hba *phba,
156                             struct be_set_eqd *set_eqd,
157                             int num)
158 {
159         struct be_ctrl_info *ctrl = &phba->ctrl;
160         struct be_mcc_wrb *wrb;
161         struct be_cmd_req_modify_eq_delay *req;
162         unsigned int tag;
163         int i;
164
165         mutex_lock(&ctrl->mbox_lock);
166         wrb = alloc_mcc_wrb(phba, &tag);
167         if (!wrb) {
168                 mutex_unlock(&ctrl->mbox_lock);
169                 return 0;
170         }
171
172         req = embedded_payload(wrb);
173         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
174         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_COMMON,
175                            OPCODE_COMMON_MODIFY_EQ_DELAY, sizeof(*req));
176
177         req->num_eq = cpu_to_le32(num);
178         for (i = 0; i < num; i++) {
179                 req->delay[i].eq_id = cpu_to_le32(set_eqd[i].eq_id);
180                 req->delay[i].phase = 0;
181                 req->delay[i].delay_multiplier =
182                                 cpu_to_le32(set_eqd[i].delay_multiplier);
183         }
184
185         /* ignore the completion of this mbox command */
186         set_bit(MCC_TAG_STATE_IGNORE, &ctrl->ptag_state[tag].tag_state);
187         be_mcc_notify(phba, tag);
188         mutex_unlock(&ctrl->mbox_lock);
189         return tag;
190 }
191
192 /**
193  * mgmt_get_port_name()- Get port name for the function
194  * @ctrl: ptr to Ctrl Info
195  * @phba: ptr to the dev priv structure
196  *
197  * Get the alphanumeric character for port
198  *
199  **/
200 int mgmt_get_port_name(struct be_ctrl_info *ctrl,
201                        struct beiscsi_hba *phba)
202 {
203         int ret = 0;
204         struct be_mcc_wrb *wrb;
205         struct be_cmd_get_port_name *ioctl;
206
207         mutex_lock(&ctrl->mbox_lock);
208         wrb = wrb_from_mbox(&ctrl->mbox_mem);
209         memset(wrb, 0, sizeof(*wrb));
210         ioctl = embedded_payload(wrb);
211
212         be_wrb_hdr_prepare(wrb, sizeof(*ioctl), true, 0);
213         be_cmd_hdr_prepare(&ioctl->h.req_hdr, CMD_SUBSYSTEM_COMMON,
214                            OPCODE_COMMON_GET_PORT_NAME,
215                            EMBED_MBX_MAX_PAYLOAD_SIZE);
216         ret = be_mbox_notify(ctrl);
217         phba->port_name = 0;
218         if (!ret) {
219                 phba->port_name = ioctl->p.resp.port_names >>
220                                   (phba->fw_config.phys_port * 8) & 0xff;
221         } else {
222                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
223                             "BG_%d : GET_PORT_NAME ret 0x%x status 0x%x\n",
224                             ret, ioctl->h.resp_hdr.status);
225         }
226
227         if (phba->port_name == 0)
228                 phba->port_name = '?';
229
230         mutex_unlock(&ctrl->mbox_lock);
231         return ret;
232 }
233
234 /**
235  * mgmt_get_fw_config()- Get the FW config for the function
236  * @ctrl: ptr to Ctrl Info
237  * @phba: ptr to the dev priv structure
238  *
239  * Get the FW config and resources available for the function.
240  * The resources are created based on the count received here.
241  *
242  * return
243  *      Success: 0
244  *      Failure: Non-Zero Value
245  **/
246 int mgmt_get_fw_config(struct be_ctrl_info *ctrl,
247                                 struct beiscsi_hba *phba)
248 {
249         struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
250         struct be_fw_cfg *pfw_cfg = embedded_payload(wrb);
251         uint32_t cid_count, icd_count;
252         int status = -EINVAL;
253         uint8_t ulp_num = 0;
254
255         mutex_lock(&ctrl->mbox_lock);
256         memset(wrb, 0, sizeof(*wrb));
257         be_wrb_hdr_prepare(wrb, sizeof(*pfw_cfg), true, 0);
258
259         be_cmd_hdr_prepare(&pfw_cfg->hdr, CMD_SUBSYSTEM_COMMON,
260                            OPCODE_COMMON_QUERY_FIRMWARE_CONFIG,
261                            EMBED_MBX_MAX_PAYLOAD_SIZE);
262
263         if (be_mbox_notify(ctrl)) {
264                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
265                             "BG_%d : Failed in mgmt_get_fw_config\n");
266                 goto fail_init;
267         }
268
269         /* FW response formats depend on port id */
270         phba->fw_config.phys_port = pfw_cfg->phys_port;
271         if (phba->fw_config.phys_port >= BEISCSI_PHYS_PORT_MAX) {
272                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
273                             "BG_%d : invalid physical port id %d\n",
274                             phba->fw_config.phys_port);
275                 goto fail_init;
276         }
277
278         /* populate and check FW config against min and max values */
279         if (!is_chip_be2_be3r(phba)) {
280                 phba->fw_config.eqid_count = pfw_cfg->eqid_count;
281                 phba->fw_config.cqid_count = pfw_cfg->cqid_count;
282                 if (phba->fw_config.eqid_count == 0 ||
283                     phba->fw_config.eqid_count > 2048) {
284                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
285                                     "BG_%d : invalid EQ count %d\n",
286                                     phba->fw_config.eqid_count);
287                         goto fail_init;
288                 }
289                 if (phba->fw_config.cqid_count == 0 ||
290                     phba->fw_config.cqid_count > 4096) {
291                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
292                                     "BG_%d : invalid CQ count %d\n",
293                                     phba->fw_config.cqid_count);
294                         goto fail_init;
295                 }
296                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
297                             "BG_%d : EQ_Count : %d CQ_Count : %d\n",
298                             phba->fw_config.eqid_count,
299                             phba->fw_config.cqid_count);
300         }
301
302         /**
303          * Check on which all ULP iSCSI Protocol is loaded.
304          * Set the Bit for those ULP. This set flag is used
305          * at all places in the code to check on which ULP
306          * iSCSi Protocol is loaded
307          **/
308         for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
309                 if (pfw_cfg->ulp[ulp_num].ulp_mode &
310                     BEISCSI_ULP_ISCSI_INI_MODE) {
311                         set_bit(ulp_num, &phba->fw_config.ulp_supported);
312
313                         /* Get the CID, ICD and Chain count for each ULP */
314                         phba->fw_config.iscsi_cid_start[ulp_num] =
315                                 pfw_cfg->ulp[ulp_num].sq_base;
316                         phba->fw_config.iscsi_cid_count[ulp_num] =
317                                 pfw_cfg->ulp[ulp_num].sq_count;
318
319                         phba->fw_config.iscsi_icd_start[ulp_num] =
320                                 pfw_cfg->ulp[ulp_num].icd_base;
321                         phba->fw_config.iscsi_icd_count[ulp_num] =
322                                 pfw_cfg->ulp[ulp_num].icd_count;
323
324                         phba->fw_config.iscsi_chain_start[ulp_num] =
325                                 pfw_cfg->chain_icd[ulp_num].chain_base;
326                         phba->fw_config.iscsi_chain_count[ulp_num] =
327                                 pfw_cfg->chain_icd[ulp_num].chain_count;
328
329                         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
330                                     "BG_%d : Function loaded on ULP : %d\n"
331                                     "\tiscsi_cid_count : %d\n"
332                                     "\tiscsi_cid_start : %d\n"
333                                     "\t iscsi_icd_count : %d\n"
334                                     "\t iscsi_icd_start : %d\n",
335                                     ulp_num,
336                                     phba->fw_config.
337                                     iscsi_cid_count[ulp_num],
338                                     phba->fw_config.
339                                     iscsi_cid_start[ulp_num],
340                                     phba->fw_config.
341                                     iscsi_icd_count[ulp_num],
342                                     phba->fw_config.
343                                     iscsi_icd_start[ulp_num]);
344                 }
345         }
346
347         if (phba->fw_config.ulp_supported == 0) {
348                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
349                             "BG_%d : iSCSI initiator mode not set: ULP0 %x ULP1 %x\n",
350                             pfw_cfg->ulp[BEISCSI_ULP0].ulp_mode,
351                             pfw_cfg->ulp[BEISCSI_ULP1].ulp_mode);
352                 goto fail_init;
353         }
354
355         /**
356          * ICD is shared among ULPs. Use icd_count of any one loaded ULP
357          **/
358         for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
359                 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
360                         break;
361         icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
362         if (icd_count == 0 || icd_count > 65536) {
363                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
364                             "BG_%d: invalid ICD count %d\n", icd_count);
365                 goto fail_init;
366         }
367
368         cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
369                     BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
370         if (cid_count == 0 || cid_count > 4096) {
371                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
372                             "BG_%d: invalid CID count %d\n", cid_count);
373                 goto fail_init;
374         }
375
376         /**
377          * Check FW is dual ULP aware i.e. can handle either
378          * of the protocols.
379          */
380         phba->fw_config.dual_ulp_aware = (pfw_cfg->function_mode &
381                                           BEISCSI_FUNC_DUA_MODE);
382
383         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
384                     "BG_%d : DUA Mode : 0x%x\n",
385                     phba->fw_config.dual_ulp_aware);
386
387         /* all set, continue using this FW config */
388         status = 0;
389 fail_init:
390         mutex_unlock(&ctrl->mbox_lock);
391         return status;
392 }
393
394 int mgmt_check_supported_fw(struct be_ctrl_info *ctrl,
395                                       struct beiscsi_hba *phba)
396 {
397         struct be_dma_mem nonemb_cmd;
398         struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
399         struct be_mgmt_controller_attributes *req;
400         struct be_sge *sge = nonembedded_sgl(wrb);
401         int status = 0;
402
403         nonemb_cmd.va = pci_alloc_consistent(ctrl->pdev,
404                                 sizeof(struct be_mgmt_controller_attributes),
405                                 &nonemb_cmd.dma);
406         if (nonemb_cmd.va == NULL) {
407                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
408                             "BG_%d : Failed to allocate memory for "
409                             "mgmt_check_supported_fw\n");
410                 return -ENOMEM;
411         }
412         nonemb_cmd.size = sizeof(struct be_mgmt_controller_attributes);
413         req = nonemb_cmd.va;
414         memset(req, 0, sizeof(*req));
415         mutex_lock(&ctrl->mbox_lock);
416         memset(wrb, 0, sizeof(*wrb));
417         be_wrb_hdr_prepare(wrb, sizeof(*req), false, 1);
418         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_COMMON,
419                            OPCODE_COMMON_GET_CNTL_ATTRIBUTES, sizeof(*req));
420         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd.dma));
421         sge->pa_lo = cpu_to_le32(nonemb_cmd.dma & 0xFFFFFFFF);
422         sge->len = cpu_to_le32(nonemb_cmd.size);
423         status = be_mbox_notify(ctrl);
424         if (!status) {
425                 struct be_mgmt_controller_attributes_resp *resp = nonemb_cmd.va;
426                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
427                             "BG_%d : Firmware Version of CMD : %s\n"
428                             "Firmware Version is : %s\n"
429                             "Developer Build, not performing version check...\n",
430                             resp->params.hba_attribs
431                             .flashrom_version_string,
432                             resp->params.hba_attribs.
433                             firmware_version_string);
434
435                 phba->fw_config.iscsi_features =
436                                 resp->params.hba_attribs.iscsi_features;
437                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
438                             "BM_%d : phba->fw_config.iscsi_features = %d\n",
439                             phba->fw_config.iscsi_features);
440                 memcpy(phba->fw_ver_str, resp->params.hba_attribs.
441                        firmware_version_string, BEISCSI_VER_STRLEN);
442         } else
443                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
444                             "BG_%d :  Failed in mgmt_check_supported_fw\n");
445         mutex_unlock(&ctrl->mbox_lock);
446         if (nonemb_cmd.va)
447                 pci_free_consistent(ctrl->pdev, nonemb_cmd.size,
448                                     nonemb_cmd.va, nonemb_cmd.dma);
449
450         return status;
451 }
452
453 unsigned int mgmt_vendor_specific_fw_cmd(struct be_ctrl_info *ctrl,
454                                          struct beiscsi_hba *phba,
455                                          struct bsg_job *job,
456                                          struct be_dma_mem *nonemb_cmd)
457 {
458         struct be_cmd_resp_hdr *resp;
459         struct be_mcc_wrb *wrb;
460         struct be_sge *mcc_sge;
461         unsigned int tag = 0;
462         struct iscsi_bsg_request *bsg_req = job->request;
463         struct be_bsg_vendor_cmd *req = nonemb_cmd->va;
464         unsigned short region, sector_size, sector, offset;
465
466         nonemb_cmd->size = job->request_payload.payload_len;
467         memset(nonemb_cmd->va, 0, nonemb_cmd->size);
468         resp = nonemb_cmd->va;
469         region =  bsg_req->rqst_data.h_vendor.vendor_cmd[1];
470         sector_size =  bsg_req->rqst_data.h_vendor.vendor_cmd[2];
471         sector =  bsg_req->rqst_data.h_vendor.vendor_cmd[3];
472         offset =  bsg_req->rqst_data.h_vendor.vendor_cmd[4];
473         req->region = region;
474         req->sector = sector;
475         req->offset = offset;
476
477         if (mutex_lock_interruptible(&ctrl->mbox_lock))
478                 return 0;
479         switch (bsg_req->rqst_data.h_vendor.vendor_cmd[0]) {
480         case BEISCSI_WRITE_FLASH:
481                 offset = sector * sector_size + offset;
482                 be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
483                                    OPCODE_COMMON_WRITE_FLASH, sizeof(*req));
484                 sg_copy_to_buffer(job->request_payload.sg_list,
485                                   job->request_payload.sg_cnt,
486                                   nonemb_cmd->va + offset, job->request_len);
487                 break;
488         case BEISCSI_READ_FLASH:
489                 be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
490                            OPCODE_COMMON_READ_FLASH, sizeof(*req));
491                 break;
492         default:
493                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
494                             "BG_%d : Unsupported cmd = 0x%x\n\n",
495                             bsg_req->rqst_data.h_vendor.vendor_cmd[0]);
496
497                 mutex_unlock(&ctrl->mbox_lock);
498                 return -EPERM;
499         }
500
501         wrb = alloc_mcc_wrb(phba, &tag);
502         if (!wrb) {
503                 mutex_unlock(&ctrl->mbox_lock);
504                 return 0;
505         }
506
507         mcc_sge = nonembedded_sgl(wrb);
508         be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false,
509                            job->request_payload.sg_cnt);
510         mcc_sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
511         mcc_sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
512         mcc_sge->len = cpu_to_le32(nonemb_cmd->size);
513
514         be_mcc_notify(phba, tag);
515
516         mutex_unlock(&ctrl->mbox_lock);
517         return tag;
518 }
519
520 /**
521  * mgmt_epfw_cleanup()- Inform FW to cleanup data structures.
522  * @phba: pointer to dev priv structure
523  * @ulp_num: ULP number.
524  *
525  * return
526  *      Success: 0
527  *      Failure: Non-Zero Value
528  **/
529 int mgmt_epfw_cleanup(struct beiscsi_hba *phba, unsigned short ulp_num)
530 {
531         struct be_ctrl_info *ctrl = &phba->ctrl;
532         struct be_mcc_wrb *wrb;
533         struct iscsi_cleanup_req *req;
534         unsigned int tag;
535         int status;
536
537         mutex_lock(&ctrl->mbox_lock);
538         wrb = alloc_mcc_wrb(phba, &tag);
539         if (!wrb) {
540                 mutex_unlock(&ctrl->mbox_lock);
541                 return -EBUSY;
542         }
543
544         req = embedded_payload(wrb);
545         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
546         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
547                            OPCODE_COMMON_ISCSI_CLEANUP, sizeof(*req));
548
549         req->chute = (1 << ulp_num);
550         req->hdr_ring_id = cpu_to_le16(HWI_GET_DEF_HDRQ_ID(phba, ulp_num));
551         req->data_ring_id = cpu_to_le16(HWI_GET_DEF_BUFQ_ID(phba, ulp_num));
552
553         be_mcc_notify(phba, tag);
554         status = be_mcc_compl_poll(phba, tag);
555         if (status)
556                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
557                             "BG_%d : mgmt_epfw_cleanup , FAILED\n");
558         mutex_unlock(&ctrl->mbox_lock);
559         return status;
560 }
561
562 unsigned int  mgmt_invalidate_icds(struct beiscsi_hba *phba,
563                                 struct invalidate_command_table *inv_tbl,
564                                 unsigned int num_invalidate, unsigned int cid,
565                                 struct be_dma_mem *nonemb_cmd)
566
567 {
568         struct be_ctrl_info *ctrl = &phba->ctrl;
569         struct be_mcc_wrb *wrb;
570         struct be_sge *sge;
571         struct invalidate_commands_params_in *req;
572         unsigned int i, tag;
573
574         mutex_lock(&ctrl->mbox_lock);
575         wrb = alloc_mcc_wrb(phba, &tag);
576         if (!wrb) {
577                 mutex_unlock(&ctrl->mbox_lock);
578                 return 0;
579         }
580
581         req = nonemb_cmd->va;
582         memset(req, 0, sizeof(*req));
583         sge = nonembedded_sgl(wrb);
584
585         be_wrb_hdr_prepare(wrb, sizeof(*req), false, 1);
586         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
587                         OPCODE_COMMON_ISCSI_ERROR_RECOVERY_INVALIDATE_COMMANDS,
588                         sizeof(*req));
589         req->ref_handle = 0;
590         req->cleanup_type = CMD_ISCSI_COMMAND_INVALIDATE;
591         for (i = 0; i < num_invalidate; i++) {
592                 req->table[i].icd = inv_tbl->icd;
593                 req->table[i].cid = inv_tbl->cid;
594                 req->icd_count++;
595                 inv_tbl++;
596         }
597         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
598         sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
599         sge->len = cpu_to_le32(nonemb_cmd->size);
600
601         be_mcc_notify(phba, tag);
602         mutex_unlock(&ctrl->mbox_lock);
603         return tag;
604 }
605
606 unsigned int mgmt_invalidate_connection(struct beiscsi_hba *phba,
607                                          struct beiscsi_endpoint *beiscsi_ep,
608                                          unsigned short cid,
609                                          unsigned short issue_reset,
610                                          unsigned short savecfg_flag)
611 {
612         struct be_ctrl_info *ctrl = &phba->ctrl;
613         struct be_mcc_wrb *wrb;
614         struct iscsi_invalidate_connection_params_in *req;
615         unsigned int tag = 0;
616
617         mutex_lock(&ctrl->mbox_lock);
618         wrb = alloc_mcc_wrb(phba, &tag);
619         if (!wrb) {
620                 mutex_unlock(&ctrl->mbox_lock);
621                 return 0;
622         }
623
624         req = embedded_payload(wrb);
625         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
626         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
627                            OPCODE_ISCSI_INI_DRIVER_INVALIDATE_CONNECTION,
628                            sizeof(*req));
629         req->session_handle = beiscsi_ep->fw_handle;
630         req->cid = cid;
631         if (issue_reset)
632                 req->cleanup_type = CMD_ISCSI_CONNECTION_ISSUE_TCP_RST;
633         else
634                 req->cleanup_type = CMD_ISCSI_CONNECTION_INVALIDATE;
635         req->save_cfg = savecfg_flag;
636         be_mcc_notify(phba, tag);
637         mutex_unlock(&ctrl->mbox_lock);
638         return tag;
639 }
640
641 unsigned int mgmt_upload_connection(struct beiscsi_hba *phba,
642                                 unsigned short cid, unsigned int upload_flag)
643 {
644         struct be_ctrl_info *ctrl = &phba->ctrl;
645         struct be_mcc_wrb *wrb;
646         struct tcp_upload_params_in *req;
647         unsigned int tag;
648
649         mutex_lock(&ctrl->mbox_lock);
650         wrb = alloc_mcc_wrb(phba, &tag);
651         if (!wrb) {
652                 mutex_unlock(&ctrl->mbox_lock);
653                 return 0;
654         }
655
656         req = embedded_payload(wrb);
657         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
658         be_cmd_hdr_prepare(&req->hdr, CMD_COMMON_TCP_UPLOAD,
659                            OPCODE_COMMON_TCP_UPLOAD, sizeof(*req));
660         req->id = (unsigned short)cid;
661         req->upload_type = (unsigned char)upload_flag;
662         be_mcc_notify(phba, tag);
663         mutex_unlock(&ctrl->mbox_lock);
664         return tag;
665 }
666
667 /**
668  * mgmt_open_connection()- Establish a TCP CXN
669  * @dst_addr: Destination Address
670  * @beiscsi_ep: ptr to device endpoint struct
671  * @nonemb_cmd: ptr to memory allocated for command
672  *
673  * return
674  *      Success: Tag number of the MBX Command issued
675  *      Failure: Error code
676  **/
677 int mgmt_open_connection(struct beiscsi_hba *phba,
678                          struct sockaddr *dst_addr,
679                          struct beiscsi_endpoint *beiscsi_ep,
680                          struct be_dma_mem *nonemb_cmd)
681 {
682         struct hwi_controller *phwi_ctrlr;
683         struct hwi_context_memory *phwi_context;
684         struct sockaddr_in *daddr_in = (struct sockaddr_in *)dst_addr;
685         struct sockaddr_in6 *daddr_in6 = (struct sockaddr_in6 *)dst_addr;
686         struct be_ctrl_info *ctrl = &phba->ctrl;
687         struct be_mcc_wrb *wrb;
688         struct tcp_connect_and_offload_in_v1 *req;
689         unsigned short def_hdr_id;
690         unsigned short def_data_id;
691         struct phys_addr template_address = { 0, 0 };
692         struct phys_addr *ptemplate_address;
693         unsigned int tag = 0;
694         unsigned int i, ulp_num;
695         unsigned short cid = beiscsi_ep->ep_cid;
696         struct be_sge *sge;
697
698         if (dst_addr->sa_family != PF_INET && dst_addr->sa_family != PF_INET6) {
699                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
700                             "BG_%d : unknown addr family %d\n",
701                             dst_addr->sa_family);
702                 return -EINVAL;
703         }
704
705         phwi_ctrlr = phba->phwi_ctrlr;
706         phwi_context = phwi_ctrlr->phwi_ctxt;
707
708         ulp_num = phwi_ctrlr->wrb_context[BE_GET_CRI_FROM_CID(cid)].ulp_num;
709
710         def_hdr_id = (unsigned short)HWI_GET_DEF_HDRQ_ID(phba, ulp_num);
711         def_data_id = (unsigned short)HWI_GET_DEF_BUFQ_ID(phba, ulp_num);
712
713         ptemplate_address = &template_address;
714         ISCSI_GET_PDU_TEMPLATE_ADDRESS(phba, ptemplate_address);
715         if (mutex_lock_interruptible(&ctrl->mbox_lock))
716                 return 0;
717         wrb = alloc_mcc_wrb(phba, &tag);
718         if (!wrb) {
719                 mutex_unlock(&ctrl->mbox_lock);
720                 return 0;
721         }
722
723         sge = nonembedded_sgl(wrb);
724         req = nonemb_cmd->va;
725         memset(req, 0, sizeof(*req));
726
727         be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false, 1);
728         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
729                            OPCODE_COMMON_ISCSI_TCP_CONNECT_AND_OFFLOAD,
730                            nonemb_cmd->size);
731         if (dst_addr->sa_family == PF_INET) {
732                 __be32 s_addr = daddr_in->sin_addr.s_addr;
733                 req->ip_address.ip_type = BEISCSI_IP_TYPE_V4;
734                 req->ip_address.addr[0] = s_addr & 0x000000ff;
735                 req->ip_address.addr[1] = (s_addr & 0x0000ff00) >> 8;
736                 req->ip_address.addr[2] = (s_addr & 0x00ff0000) >> 16;
737                 req->ip_address.addr[3] = (s_addr & 0xff000000) >> 24;
738                 req->tcp_port = ntohs(daddr_in->sin_port);
739                 beiscsi_ep->dst_addr = daddr_in->sin_addr.s_addr;
740                 beiscsi_ep->dst_tcpport = ntohs(daddr_in->sin_port);
741                 beiscsi_ep->ip_type = BEISCSI_IP_TYPE_V4;
742         } else {
743                 /* else its PF_INET6 family */
744                 req->ip_address.ip_type = BEISCSI_IP_TYPE_V6;
745                 memcpy(&req->ip_address.addr,
746                        &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
747                 req->tcp_port = ntohs(daddr_in6->sin6_port);
748                 beiscsi_ep->dst_tcpport = ntohs(daddr_in6->sin6_port);
749                 memcpy(&beiscsi_ep->dst6_addr,
750                        &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
751                 beiscsi_ep->ip_type = BEISCSI_IP_TYPE_V6;
752         }
753         req->cid = cid;
754         i = phba->nxt_cqid++;
755         if (phba->nxt_cqid == phba->num_cpus)
756                 phba->nxt_cqid = 0;
757         req->cq_id = phwi_context->be_cq[i].id;
758         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
759                     "BG_%d : i=%d cq_id=%d\n", i, req->cq_id);
760         req->defq_id = def_hdr_id;
761         req->hdr_ring_id = def_hdr_id;
762         req->data_ring_id = def_data_id;
763         req->do_offload = 1;
764         req->dataout_template_pa.lo = ptemplate_address->lo;
765         req->dataout_template_pa.hi = ptemplate_address->hi;
766         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
767         sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
768         sge->len = cpu_to_le32(nonemb_cmd->size);
769
770         if (!is_chip_be2_be3r(phba)) {
771                 req->hdr.version = MBX_CMD_VER1;
772                 req->tcp_window_size = 0;
773                 req->tcp_window_scale_count = 2;
774         }
775
776         be_mcc_notify(phba, tag);
777         mutex_unlock(&ctrl->mbox_lock);
778         return tag;
779 }
780
781 /*
782  * mgmt_exec_nonemb_cmd()- Execute Non Embedded MBX Cmd
783  * @phba: Driver priv structure
784  * @nonemb_cmd: Address of the MBX command issued
785  * @resp_buf: Buffer to copy the MBX cmd response
786  * @resp_buf_len: respone lenght to be copied
787  *
788  **/
789 static int mgmt_exec_nonemb_cmd(struct beiscsi_hba *phba,
790                                 struct be_dma_mem *nonemb_cmd, void *resp_buf,
791                                 int resp_buf_len)
792 {
793         struct be_ctrl_info *ctrl = &phba->ctrl;
794         struct be_mcc_wrb *wrb;
795         struct be_sge *sge;
796         unsigned int tag;
797         int rc = 0;
798
799         mutex_lock(&ctrl->mbox_lock);
800         wrb = alloc_mcc_wrb(phba, &tag);
801         if (!wrb) {
802                 mutex_unlock(&ctrl->mbox_lock);
803                 rc = -ENOMEM;
804                 goto free_cmd;
805         }
806
807         sge = nonembedded_sgl(wrb);
808         be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false, 1);
809         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
810         sge->pa_lo = cpu_to_le32(lower_32_bits(nonemb_cmd->dma));
811         sge->len = cpu_to_le32(nonemb_cmd->size);
812
813         be_mcc_notify(phba, tag);
814         mutex_unlock(&ctrl->mbox_lock);
815
816         rc = beiscsi_mccq_compl_wait(phba, tag, NULL, nonemb_cmd);
817
818         if (resp_buf)
819                 memcpy(resp_buf, nonemb_cmd->va, resp_buf_len);
820
821         if (rc) {
822                 /* Check if the MBX Cmd needs to be re-issued */
823                 if (rc == -EAGAIN)
824                         return rc;
825
826                 beiscsi_log(phba, KERN_WARNING,
827                             BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
828                             "BG_%d : mgmt_exec_nonemb_cmd Failed status\n");
829
830                 if (rc != -EBUSY)
831                         goto free_cmd;
832                 else
833                         return rc;
834         }
835 free_cmd:
836         pci_free_consistent(ctrl->pdev, nonemb_cmd->size,
837                             nonemb_cmd->va, nonemb_cmd->dma);
838         return rc;
839 }
840
841 static int mgmt_alloc_cmd_data(struct beiscsi_hba *phba, struct be_dma_mem *cmd,
842                                int iscsi_cmd, int size)
843 {
844         cmd->va = pci_zalloc_consistent(phba->ctrl.pdev, size, &cmd->dma);
845         if (!cmd->va) {
846                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
847                             "BG_%d : Failed to allocate memory for if info\n");
848                 return -ENOMEM;
849         }
850         cmd->size = size;
851         be_cmd_hdr_prepare(cmd->va, CMD_SUBSYSTEM_ISCSI, iscsi_cmd, size);
852         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
853                     "BG_%d : subsystem iSCSI cmd %d size %d\n",
854                     iscsi_cmd, size);
855         return 0;
856 }
857
858 unsigned int beiscsi_if_get_handle(struct beiscsi_hba *phba)
859 {
860         struct be_ctrl_info *ctrl = &phba->ctrl;
861         struct be_mcc_wrb *wrb;
862         struct be_cmd_get_all_if_id_req *req;
863         struct be_cmd_get_all_if_id_req *pbe_allid;
864         unsigned int tag;
865         int status = 0;
866
867         if (mutex_lock_interruptible(&ctrl->mbox_lock))
868                 return -EINTR;
869         wrb = alloc_mcc_wrb(phba, &tag);
870         if (!wrb) {
871                 mutex_unlock(&ctrl->mbox_lock);
872                 return -ENOMEM;
873         }
874
875         req = embedded_payload(wrb);
876         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
877         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
878                            OPCODE_COMMON_ISCSI_NTWK_GET_ALL_IF_ID,
879                            sizeof(*req));
880         be_mcc_notify(phba, tag);
881         mutex_unlock(&ctrl->mbox_lock);
882
883         status = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
884         if (status) {
885                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
886                             "BG_%d : %s failed: %d\n", __func__, status);
887                 return -EBUSY;
888         }
889
890         pbe_allid = embedded_payload(wrb);
891         /* we now support only one interface per function */
892         phba->interface_handle = pbe_allid->if_hndl_list[0];
893
894         return status;
895 }
896
897 static inline bool beiscsi_if_zero_ip(u8 *ip, u32 ip_type)
898 {
899         u32 len;
900
901         len = (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN;
902         while (len && !ip[len - 1])
903                 len--;
904         return (len == 0);
905 }
906
907 static int beiscsi_if_mod_gw(struct beiscsi_hba *phba,
908                              u32 action, u32 ip_type, u8 *gw)
909 {
910         struct be_cmd_set_def_gateway_req *req;
911         struct be_dma_mem nonemb_cmd;
912         int rt_val;
913
914         rt_val = mgmt_alloc_cmd_data(phba, &nonemb_cmd,
915                                 OPCODE_COMMON_ISCSI_NTWK_MODIFY_DEFAULT_GATEWAY,
916                                 sizeof(*req));
917         if (rt_val)
918                 return rt_val;
919
920         req = nonemb_cmd.va;
921         req->action = action;
922         req->ip_addr.ip_type = ip_type;
923         memcpy(req->ip_addr.addr, gw,
924                (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN);
925         return mgmt_exec_nonemb_cmd(phba, &nonemb_cmd, NULL, 0);
926 }
927
928 int beiscsi_if_set_gw(struct beiscsi_hba *phba, u32 ip_type, u8 *gw)
929 {
930         struct be_cmd_get_def_gateway_resp gw_resp;
931         int rt_val;
932
933         memset(&gw_resp, 0, sizeof(gw_resp));
934         rt_val = beiscsi_if_get_gw(phba, ip_type, &gw_resp);
935         if (rt_val) {
936                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
937                             "BG_%d : Failed to Get Gateway Addr\n");
938                 return rt_val;
939         }
940
941         if (!beiscsi_if_zero_ip(gw_resp.ip_addr.addr, ip_type)) {
942                 rt_val = beiscsi_if_mod_gw(phba, IP_ACTION_DEL, ip_type,
943                                            gw_resp.ip_addr.addr);
944                 if (rt_val) {
945                         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
946                                     "BG_%d : Failed to clear Gateway Addr Set\n");
947                         return rt_val;
948                 }
949         }
950
951         rt_val = beiscsi_if_mod_gw(phba, IP_ACTION_ADD, ip_type, gw);
952         if (rt_val)
953                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
954                             "BG_%d : Failed to Set Gateway Addr\n");
955
956         return rt_val;
957 }
958
959 int beiscsi_if_get_gw(struct beiscsi_hba *phba, u32 ip_type,
960                       struct be_cmd_get_def_gateway_resp *resp)
961 {
962         struct be_cmd_get_def_gateway_req *req;
963         struct be_dma_mem nonemb_cmd;
964         int rc;
965
966         rc = mgmt_alloc_cmd_data(phba, &nonemb_cmd,
967                                  OPCODE_COMMON_ISCSI_NTWK_GET_DEFAULT_GATEWAY,
968                                  sizeof(*resp));
969         if (rc)
970                 return rc;
971
972         req = nonemb_cmd.va;
973         req->ip_type = ip_type;
974
975         return mgmt_exec_nonemb_cmd(phba, &nonemb_cmd, resp,
976                                     sizeof(*resp));
977 }
978
979 static int
980 beiscsi_if_clr_ip(struct beiscsi_hba *phba,
981                   struct be_cmd_get_if_info_resp *if_info)
982 {
983         struct be_cmd_set_ip_addr_req *req;
984         struct be_dma_mem nonemb_cmd;
985         int rc;
986
987         rc = mgmt_alloc_cmd_data(phba, &nonemb_cmd,
988                                  OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR,
989                                  sizeof(*req));
990         if (rc)
991                 return rc;
992
993         req = nonemb_cmd.va;
994         req->ip_params.record_entry_count = 1;
995         req->ip_params.ip_record.action = IP_ACTION_DEL;
996         req->ip_params.ip_record.interface_hndl =
997                 phba->interface_handle;
998         req->ip_params.ip_record.ip_addr.size_of_structure =
999                 sizeof(struct be_ip_addr_subnet_format);
1000         req->ip_params.ip_record.ip_addr.ip_type = if_info->ip_addr.ip_type;
1001         memcpy(req->ip_params.ip_record.ip_addr.addr,
1002                if_info->ip_addr.addr,
1003                sizeof(if_info->ip_addr.addr));
1004         memcpy(req->ip_params.ip_record.ip_addr.subnet_mask,
1005                if_info->ip_addr.subnet_mask,
1006                sizeof(if_info->ip_addr.subnet_mask));
1007         rc = mgmt_exec_nonemb_cmd(phba, &nonemb_cmd, NULL, 0);
1008         if (rc < 0 || req->ip_params.ip_record.status) {
1009                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1010                             "BG_%d : failed to clear IP: rc %d status %d\n",
1011                             rc, req->ip_params.ip_record.status);
1012         }
1013         return rc;
1014 }
1015
1016 static int
1017 beiscsi_if_set_ip(struct beiscsi_hba *phba, u8 *ip,
1018                   u8 *subnet, u32 ip_type)
1019 {
1020         struct be_cmd_set_ip_addr_req *req;
1021         struct be_dma_mem nonemb_cmd;
1022         uint32_t ip_len;
1023         int rc;
1024
1025         rc = mgmt_alloc_cmd_data(phba, &nonemb_cmd,
1026                                  OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR,
1027                                  sizeof(*req));
1028         if (rc)
1029                 return rc;
1030
1031         req = nonemb_cmd.va;
1032         req->ip_params.record_entry_count = 1;
1033         req->ip_params.ip_record.action = IP_ACTION_ADD;
1034         req->ip_params.ip_record.interface_hndl =
1035                 phba->interface_handle;
1036         req->ip_params.ip_record.ip_addr.size_of_structure =
1037                 sizeof(struct be_ip_addr_subnet_format);
1038         req->ip_params.ip_record.ip_addr.ip_type = ip_type;
1039         ip_len = (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN;
1040         memcpy(req->ip_params.ip_record.ip_addr.addr, ip, ip_len);
1041         if (subnet)
1042                 memcpy(req->ip_params.ip_record.ip_addr.subnet_mask,
1043                        subnet, ip_len);
1044
1045         rc = mgmt_exec_nonemb_cmd(phba, &nonemb_cmd, NULL, 0);
1046         /**
1047          * In some cases, host needs to look into individual record status
1048          * even though FW reported success for that IOCTL.
1049          */
1050         if (rc < 0 || req->ip_params.ip_record.status) {
1051                 __beiscsi_log(phba, KERN_ERR,
1052                             "BG_%d : failed to set IP: rc %d status %d\n",
1053                             rc, req->ip_params.ip_record.status);
1054                 if (req->ip_params.ip_record.status)
1055                         rc = -EINVAL;
1056         }
1057         return rc;
1058 }
1059
1060 int beiscsi_if_en_static(struct beiscsi_hba *phba, u32 ip_type,
1061                          u8 *ip, u8 *subnet)
1062 {
1063         struct be_cmd_get_if_info_resp *if_info;
1064         struct be_cmd_rel_dhcp_req *reldhcp;
1065         struct be_dma_mem nonemb_cmd;
1066         int rc;
1067
1068         rc = beiscsi_if_get_info(phba, ip_type, &if_info);
1069         if (rc)
1070                 return rc;
1071
1072         if (if_info->dhcp_state) {
1073                 rc = mgmt_alloc_cmd_data(phba, &nonemb_cmd,
1074                                 OPCODE_COMMON_ISCSI_NTWK_REL_STATELESS_IP_ADDR,
1075                                 sizeof(*reldhcp));
1076                 if (rc)
1077                         goto exit;
1078
1079                 reldhcp = nonemb_cmd.va;
1080                 reldhcp->interface_hndl = phba->interface_handle;
1081                 reldhcp->ip_type = ip_type;
1082                 rc = mgmt_exec_nonemb_cmd(phba, &nonemb_cmd, NULL, 0);
1083                 if (rc < 0) {
1084                         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1085                                     "BG_%d : failed to release existing DHCP: %d\n",
1086                                     rc);
1087                         goto exit;
1088                 }
1089         }
1090
1091         /* first delete any IP set */
1092         if (!beiscsi_if_zero_ip(if_info->ip_addr.addr, ip_type)) {
1093                 rc = beiscsi_if_clr_ip(phba, if_info);
1094                 if (rc)
1095                         goto exit;
1096         }
1097
1098         /* if ip == NULL then this is called just to release DHCP IP */
1099         if (ip)
1100                 rc = beiscsi_if_set_ip(phba, ip, subnet, ip_type);
1101 exit:
1102         kfree(if_info);
1103         return rc;
1104 }
1105
1106 int beiscsi_if_en_dhcp(struct beiscsi_hba *phba, u32 ip_type)
1107 {
1108         struct be_cmd_get_def_gateway_resp gw_resp;
1109         struct be_cmd_get_if_info_resp *if_info;
1110         struct be_cmd_set_dhcp_req *dhcpreq;
1111         struct be_dma_mem nonemb_cmd;
1112         u8 *gw;
1113         int rc;
1114
1115         rc = beiscsi_if_get_info(phba, ip_type, &if_info);
1116         if (rc)
1117                 return rc;
1118
1119         if (if_info->dhcp_state) {
1120                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1121                                 "BG_%d : DHCP Already Enabled\n");
1122                 goto exit;
1123         }
1124
1125         /* first delete any IP set */
1126         if (!beiscsi_if_zero_ip(if_info->ip_addr.addr, ip_type)) {
1127                 rc = beiscsi_if_clr_ip(phba, if_info);
1128                 if (rc)
1129                         goto exit;
1130         }
1131
1132         /* delete gateway settings if mode change is to DHCP */
1133         memset(&gw_resp, 0, sizeof(gw_resp));
1134         /* use ip_type provided in if_info */
1135         rc = beiscsi_if_get_gw(phba, if_info->ip_addr.ip_type, &gw_resp);
1136         if (rc) {
1137                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1138                             "BG_%d : Failed to Get Gateway Addr\n");
1139                 goto exit;
1140         }
1141         gw = (u8 *)&gw_resp.ip_addr.addr;
1142         if (!beiscsi_if_zero_ip(gw, if_info->ip_addr.ip_type)) {
1143                 rc = beiscsi_if_mod_gw(phba, IP_ACTION_DEL,
1144                                        if_info->ip_addr.ip_type, gw);
1145                 if (rc) {
1146                         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1147                                     "BG_%d : Failed to clear Gateway Addr Set\n");
1148                         goto exit;
1149                 }
1150         }
1151
1152         rc = mgmt_alloc_cmd_data(phba, &nonemb_cmd,
1153                         OPCODE_COMMON_ISCSI_NTWK_CONFIG_STATELESS_IP_ADDR,
1154                         sizeof(*dhcpreq));
1155         if (rc)
1156                 goto exit;
1157
1158         dhcpreq = nonemb_cmd.va;
1159         dhcpreq->flags = 1; /* 1 - blocking; 0 - non-blocking */
1160         dhcpreq->retry_count = 1;
1161         dhcpreq->interface_hndl = phba->interface_handle;
1162         dhcpreq->ip_type = ip_type;
1163         rc = mgmt_exec_nonemb_cmd(phba, &nonemb_cmd, NULL, 0);
1164
1165 exit:
1166         kfree(if_info);
1167         return rc;
1168 }
1169
1170 /**
1171  * beiscsi_if_set_vlan()- Issue and wait for CMD completion
1172  * @phba: device private structure instance
1173  * @vlan_tag: VLAN tag
1174  *
1175  * Issue the MBX Cmd and wait for the completion of the
1176  * command.
1177  *
1178  * returns
1179  *      Success: 0
1180  *      Failure: Non-Xero Value
1181  **/
1182 int beiscsi_if_set_vlan(struct beiscsi_hba *phba, uint16_t vlan_tag)
1183 {
1184         int rc;
1185         unsigned int tag;
1186
1187         tag = be_cmd_set_vlan(phba, vlan_tag);
1188         if (!tag) {
1189                 beiscsi_log(phba, KERN_ERR,
1190                             (BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX),
1191                             "BG_%d : VLAN Setting Failed\n");
1192                 return -EBUSY;
1193         }
1194
1195         rc = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1196         if (rc) {
1197                 beiscsi_log(phba, KERN_ERR,
1198                             (BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX),
1199                             "BS_%d : VLAN MBX Cmd Failed\n");
1200                 return rc;
1201         }
1202         return rc;
1203 }
1204
1205
1206 int beiscsi_if_get_info(struct beiscsi_hba *phba, int ip_type,
1207                         struct be_cmd_get_if_info_resp **if_info)
1208 {
1209         struct be_cmd_get_if_info_req *req;
1210         struct be_dma_mem nonemb_cmd;
1211         uint32_t ioctl_size = sizeof(struct be_cmd_get_if_info_resp);
1212         int rc;
1213
1214         rc = beiscsi_if_get_handle(phba);
1215         if (rc)
1216                 return rc;
1217
1218         do {
1219                 rc = mgmt_alloc_cmd_data(phba, &nonemb_cmd,
1220                                          OPCODE_COMMON_ISCSI_NTWK_GET_IF_INFO,
1221                                          ioctl_size);
1222                 if (rc)
1223                         return rc;
1224
1225                 req = nonemb_cmd.va;
1226                 req->interface_hndl = phba->interface_handle;
1227                 req->ip_type = ip_type;
1228
1229                 /* Allocate memory for if_info */
1230                 *if_info = kzalloc(ioctl_size, GFP_KERNEL);
1231                 if (!*if_info) {
1232                         beiscsi_log(phba, KERN_ERR,
1233                                     BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
1234                                     "BG_%d : Memory Allocation Failure\n");
1235
1236                                 /* Free the DMA memory for the IOCTL issuing */
1237                                 pci_free_consistent(phba->ctrl.pdev,
1238                                                     nonemb_cmd.size,
1239                                                     nonemb_cmd.va,
1240                                                     nonemb_cmd.dma);
1241                                 return -ENOMEM;
1242                 }
1243
1244                 rc =  mgmt_exec_nonemb_cmd(phba, &nonemb_cmd, *if_info,
1245                                            ioctl_size);
1246
1247                 /* Check if the error is because of Insufficent_Buffer */
1248                 if (rc == -EAGAIN) {
1249
1250                         /* Get the new memory size */
1251                         ioctl_size = ((struct be_cmd_resp_hdr *)
1252                                       nonemb_cmd.va)->actual_resp_len;
1253                         ioctl_size += sizeof(struct be_cmd_req_hdr);
1254
1255                         /* Free the previous allocated DMA memory */
1256                         pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1257                                             nonemb_cmd.va,
1258                                             nonemb_cmd.dma);
1259
1260                         /* Free the virtual memory */
1261                         kfree(*if_info);
1262                 } else
1263                         break;
1264         } while (true);
1265         return rc;
1266 }
1267
1268 int mgmt_get_nic_conf(struct beiscsi_hba *phba,
1269                       struct be_cmd_get_nic_conf_resp *nic)
1270 {
1271         struct be_dma_mem nonemb_cmd;
1272         int rc;
1273
1274         rc = mgmt_alloc_cmd_data(phba, &nonemb_cmd,
1275                                  OPCODE_COMMON_ISCSI_NTWK_GET_NIC_CONFIG,
1276                                  sizeof(*nic));
1277         if (rc)
1278                 return rc;
1279
1280         return mgmt_exec_nonemb_cmd(phba, &nonemb_cmd, nic, sizeof(*nic));
1281 }
1282
1283
1284
1285 unsigned int be_cmd_get_initname(struct beiscsi_hba *phba)
1286 {
1287         unsigned int tag;
1288         struct be_mcc_wrb *wrb;
1289         struct be_cmd_hba_name *req;
1290         struct be_ctrl_info *ctrl = &phba->ctrl;
1291
1292         if (mutex_lock_interruptible(&ctrl->mbox_lock))
1293                 return 0;
1294         wrb = alloc_mcc_wrb(phba, &tag);
1295         if (!wrb) {
1296                 mutex_unlock(&ctrl->mbox_lock);
1297                 return 0;
1298         }
1299
1300         req = embedded_payload(wrb);
1301         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
1302         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1303                         OPCODE_ISCSI_INI_CFG_GET_HBA_NAME,
1304                         sizeof(*req));
1305
1306         be_mcc_notify(phba, tag);
1307         mutex_unlock(&ctrl->mbox_lock);
1308         return tag;
1309 }
1310
1311 static void beiscsi_boot_process_compl(struct beiscsi_hba *phba,
1312                                        unsigned int tag)
1313 {
1314         struct be_cmd_get_boot_target_resp *boot_resp;
1315         struct be_cmd_resp_logout_fw_sess *logo_resp;
1316         struct be_cmd_get_session_resp *sess_resp;
1317         struct be_mcc_wrb *wrb;
1318         struct boot_struct *bs;
1319         int boot_work, status;
1320
1321         if (!test_bit(BEISCSI_HBA_BOOT_WORK, &phba->state)) {
1322                 __beiscsi_log(phba, KERN_ERR,
1323                               "BG_%d : %s no boot work %lx\n",
1324                               __func__, phba->state);
1325                 return;
1326         }
1327
1328         if (phba->boot_struct.tag != tag) {
1329                 __beiscsi_log(phba, KERN_ERR,
1330                               "BG_%d : %s tag mismatch %d:%d\n",
1331                               __func__, tag, phba->boot_struct.tag);
1332                 return;
1333         }
1334         bs = &phba->boot_struct;
1335         boot_work = 1;
1336         status = 0;
1337         switch (bs->action) {
1338         case BEISCSI_BOOT_REOPEN_SESS:
1339                 status = __beiscsi_mcc_compl_status(phba, tag, NULL, NULL);
1340                 if (!status)
1341                         bs->action = BEISCSI_BOOT_GET_SHANDLE;
1342                 else
1343                         bs->retry--;
1344                 break;
1345         case BEISCSI_BOOT_GET_SHANDLE:
1346                 status = __beiscsi_mcc_compl_status(phba, tag, &wrb, NULL);
1347                 if (!status) {
1348                         boot_resp = embedded_payload(wrb);
1349                         bs->s_handle = boot_resp->boot_session_handle;
1350                 }
1351                 if (bs->s_handle == BE_BOOT_INVALID_SHANDLE) {
1352                         bs->action = BEISCSI_BOOT_REOPEN_SESS;
1353                         bs->retry--;
1354                 } else {
1355                         bs->action = BEISCSI_BOOT_GET_SINFO;
1356                 }
1357                 break;
1358         case BEISCSI_BOOT_GET_SINFO:
1359                 status = __beiscsi_mcc_compl_status(phba, tag, NULL,
1360                                                     &bs->nonemb_cmd);
1361                 if (!status) {
1362                         sess_resp = bs->nonemb_cmd.va;
1363                         memcpy(&bs->boot_sess, &sess_resp->session_info,
1364                                sizeof(struct mgmt_session_info));
1365                         bs->action = BEISCSI_BOOT_LOGOUT_SESS;
1366                 } else {
1367                         __beiscsi_log(phba, KERN_ERR,
1368                                       "BG_%d : get boot session info error : 0x%x\n",
1369                                       status);
1370                         boot_work = 0;
1371                 }
1372                 pci_free_consistent(phba->ctrl.pdev, bs->nonemb_cmd.size,
1373                                     bs->nonemb_cmd.va, bs->nonemb_cmd.dma);
1374                 bs->nonemb_cmd.va = NULL;
1375                 break;
1376         case BEISCSI_BOOT_LOGOUT_SESS:
1377                 status = __beiscsi_mcc_compl_status(phba, tag, &wrb, NULL);
1378                 if (!status) {
1379                         logo_resp = embedded_payload(wrb);
1380                         if (logo_resp->session_status != BE_SESS_STATUS_CLOSE) {
1381                                 __beiscsi_log(phba, KERN_ERR,
1382                                               "BG_%d : FW boot session logout error : 0x%x\n",
1383                                               logo_resp->session_status);
1384                         }
1385                 }
1386                 /* continue to create boot_kset even if logout failed? */
1387                 bs->action = BEISCSI_BOOT_CREATE_KSET;
1388                 break;
1389         default:
1390                 break;
1391         }
1392
1393         /* clear the tag so no other completion matches this tag */
1394         bs->tag = 0;
1395         if (!bs->retry) {
1396                 boot_work = 0;
1397                 __beiscsi_log(phba, KERN_ERR,
1398                               "BG_%d : failed to setup boot target: status %d action %d\n",
1399                               status, bs->action);
1400         }
1401         if (!boot_work) {
1402                 /* wait for next event to start boot_work */
1403                 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
1404                 return;
1405         }
1406         schedule_work(&phba->boot_work);
1407 }
1408
1409 /**
1410  * beiscsi_boot_logout_sess()- Logout from boot FW session
1411  * @phba: Device priv structure instance
1412  *
1413  * return
1414  *      the TAG used for MBOX Command
1415  *
1416  */
1417 unsigned int beiscsi_boot_logout_sess(struct beiscsi_hba *phba)
1418 {
1419         struct be_ctrl_info *ctrl = &phba->ctrl;
1420         struct be_mcc_wrb *wrb;
1421         struct be_cmd_req_logout_fw_sess *req;
1422         unsigned int tag;
1423
1424         mutex_lock(&ctrl->mbox_lock);
1425         wrb = alloc_mcc_wrb(phba, &tag);
1426         if (!wrb) {
1427                 mutex_unlock(&ctrl->mbox_lock);
1428                 return 0;
1429         }
1430
1431         req = embedded_payload(wrb);
1432         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
1433         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1434                            OPCODE_ISCSI_INI_SESSION_LOGOUT_TARGET,
1435                            sizeof(struct be_cmd_req_logout_fw_sess));
1436         /* Use the session handle copied into boot_sess */
1437         req->session_handle = phba->boot_struct.boot_sess.session_handle;
1438
1439         phba->boot_struct.tag = tag;
1440         set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1441         ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1442
1443         be_mcc_notify(phba, tag);
1444         mutex_unlock(&ctrl->mbox_lock);
1445
1446         return tag;
1447 }
1448 /**
1449  * beiscsi_boot_reopen_sess()- Reopen boot session
1450  * @phba: Device priv structure instance
1451  *
1452  * return
1453  *      the TAG used for MBOX Command
1454  *
1455  **/
1456 unsigned int beiscsi_boot_reopen_sess(struct beiscsi_hba *phba)
1457 {
1458         struct be_ctrl_info *ctrl = &phba->ctrl;
1459         struct be_mcc_wrb *wrb;
1460         struct be_cmd_reopen_session_req *req;
1461         unsigned int tag;
1462
1463         mutex_lock(&ctrl->mbox_lock);
1464         wrb = alloc_mcc_wrb(phba, &tag);
1465         if (!wrb) {
1466                 mutex_unlock(&ctrl->mbox_lock);
1467                 return 0;
1468         }
1469
1470         req = embedded_payload(wrb);
1471         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
1472         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1473                            OPCODE_ISCSI_INI_DRIVER_REOPEN_ALL_SESSIONS,
1474                            sizeof(struct be_cmd_reopen_session_resp));
1475         req->reopen_type = BE_REOPEN_BOOT_SESSIONS;
1476         req->session_handle = BE_BOOT_INVALID_SHANDLE;
1477
1478         phba->boot_struct.tag = tag;
1479         set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1480         ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1481
1482         be_mcc_notify(phba, tag);
1483         mutex_unlock(&ctrl->mbox_lock);
1484         return tag;
1485 }
1486
1487
1488 /**
1489  * beiscsi_boot_get_sinfo()- Get boot session info
1490  * @phba: device priv structure instance
1491  *
1492  * Fetches the boot_struct.s_handle info from FW.
1493  * return
1494  *      the TAG used for MBOX Command
1495  *
1496  **/
1497 unsigned int beiscsi_boot_get_sinfo(struct beiscsi_hba *phba)
1498 {
1499         struct be_ctrl_info *ctrl = &phba->ctrl;
1500         struct be_cmd_get_session_resp *resp;
1501         struct be_cmd_get_session_req *req;
1502         struct be_dma_mem *nonemb_cmd;
1503         struct be_mcc_wrb *wrb;
1504         struct be_sge *sge;
1505         unsigned int tag;
1506
1507         mutex_lock(&ctrl->mbox_lock);
1508         wrb = alloc_mcc_wrb(phba, &tag);
1509         if (!wrb) {
1510                 mutex_unlock(&ctrl->mbox_lock);
1511                 return 0;
1512         }
1513
1514         nonemb_cmd = &phba->boot_struct.nonemb_cmd;
1515         nonemb_cmd->size = sizeof(*resp);
1516         nonemb_cmd->va = pci_alloc_consistent(phba->ctrl.pdev,
1517                                               sizeof(nonemb_cmd->size),
1518                                               &nonemb_cmd->dma);
1519         if (!nonemb_cmd->va)
1520                 return 0;
1521
1522         req = nonemb_cmd->va;
1523         memset(req, 0, sizeof(*req));
1524         sge = nonembedded_sgl(wrb);
1525         be_wrb_hdr_prepare(wrb, sizeof(*req), false, 1);
1526         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1527                            OPCODE_ISCSI_INI_SESSION_GET_A_SESSION,
1528                            sizeof(*resp));
1529         req->session_handle = phba->boot_struct.s_handle;
1530         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
1531         sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
1532         sge->len = cpu_to_le32(nonemb_cmd->size);
1533
1534         phba->boot_struct.tag = tag;
1535         set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1536         ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1537
1538         be_mcc_notify(phba, tag);
1539         mutex_unlock(&ctrl->mbox_lock);
1540         return tag;
1541 }
1542
1543 unsigned int __beiscsi_boot_get_shandle(struct beiscsi_hba *phba, int async)
1544 {
1545         struct be_ctrl_info *ctrl = &phba->ctrl;
1546         struct be_mcc_wrb *wrb;
1547         struct be_cmd_get_boot_target_req *req;
1548         unsigned int tag;
1549
1550         mutex_lock(&ctrl->mbox_lock);
1551         wrb = alloc_mcc_wrb(phba, &tag);
1552         if (!wrb) {
1553                 mutex_unlock(&ctrl->mbox_lock);
1554                 return 0;
1555         }
1556
1557         req = embedded_payload(wrb);
1558         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
1559         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1560                            OPCODE_ISCSI_INI_BOOT_GET_BOOT_TARGET,
1561                            sizeof(struct be_cmd_get_boot_target_resp));
1562
1563         if (async) {
1564                 phba->boot_struct.tag = tag;
1565                 set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1566                 ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1567         }
1568
1569         be_mcc_notify(phba, tag);
1570         mutex_unlock(&ctrl->mbox_lock);
1571         return tag;
1572 }
1573
1574 /**
1575  * beiscsi_boot_get_shandle()- Get boot session handle
1576  * @phba: device priv structure instance
1577  * @s_handle: session handle returned for boot session.
1578  *
1579  * return
1580  *      Success: 1
1581  *      Failure: negative
1582  *
1583  **/
1584 int beiscsi_boot_get_shandle(struct beiscsi_hba *phba, unsigned int *s_handle)
1585 {
1586         struct be_cmd_get_boot_target_resp *boot_resp;
1587         struct be_mcc_wrb *wrb;
1588         unsigned int tag;
1589         int rc;
1590
1591         *s_handle = BE_BOOT_INVALID_SHANDLE;
1592         /* get configured boot session count and handle */
1593         tag = __beiscsi_boot_get_shandle(phba, 0);
1594         if (!tag) {
1595                 beiscsi_log(phba, KERN_ERR,
1596                             BEISCSI_LOG_CONFIG | BEISCSI_LOG_INIT,
1597                             "BG_%d : Getting Boot Target Info Failed\n");
1598                 return -EAGAIN;
1599         }
1600
1601         rc = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
1602         if (rc) {
1603                 beiscsi_log(phba, KERN_ERR,
1604                             BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
1605                             "BG_%d : MBX CMD get_boot_target Failed\n");
1606                 return -EBUSY;
1607         }
1608
1609         boot_resp = embedded_payload(wrb);
1610         /* check if there are any boot targets configured */
1611         if (!boot_resp->boot_session_count) {
1612                 __beiscsi_log(phba, KERN_INFO,
1613                               "BG_%d : No boot targets configured\n");
1614                 return -ENXIO;
1615         }
1616
1617         /* only if FW has logged in to the boot target, s_handle is valid */
1618         *s_handle = boot_resp->boot_session_handle;
1619         return 1;
1620 }
1621
1622 /**
1623  * beiscsi_drvr_ver_disp()- Display the driver Name and Version
1624  * @dev: ptr to device not used.
1625  * @attr: device attribute, not used.
1626  * @buf: contains formatted text driver name and version
1627  *
1628  * return
1629  * size of the formatted string
1630  **/
1631 ssize_t
1632 beiscsi_drvr_ver_disp(struct device *dev, struct device_attribute *attr,
1633                        char *buf)
1634 {
1635         return snprintf(buf, PAGE_SIZE, BE_NAME "\n");
1636 }
1637
1638 /**
1639  * beiscsi_fw_ver_disp()- Display Firmware Version
1640  * @dev: ptr to device not used.
1641  * @attr: device attribute, not used.
1642  * @buf: contains formatted text Firmware version
1643  *
1644  * return
1645  * size of the formatted string
1646  **/
1647 ssize_t
1648 beiscsi_fw_ver_disp(struct device *dev, struct device_attribute *attr,
1649                      char *buf)
1650 {
1651         struct Scsi_Host *shost = class_to_shost(dev);
1652         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1653
1654         return snprintf(buf, PAGE_SIZE, "%s\n", phba->fw_ver_str);
1655 }
1656
1657 /**
1658  * beiscsi_active_session_disp()- Display Sessions Active
1659  * @dev: ptr to device not used.
1660  * @attr: device attribute, not used.
1661  * @buf: contains formatted text Session Count
1662  *
1663  * return
1664  * size of the formatted string
1665  **/
1666 ssize_t
1667 beiscsi_active_session_disp(struct device *dev, struct device_attribute *attr,
1668                          char *buf)
1669 {
1670         struct Scsi_Host *shost = class_to_shost(dev);
1671         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1672         uint16_t avlbl_cids = 0, ulp_num, len = 0, total_cids = 0;
1673
1674         for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
1675                 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
1676                         avlbl_cids = BEISCSI_ULP_AVLBL_CID(phba, ulp_num);
1677                         total_cids = BEISCSI_GET_CID_COUNT(phba, ulp_num);
1678                         len += snprintf(buf+len, PAGE_SIZE - len,
1679                                         "ULP%d : %d\n", ulp_num,
1680                                         (total_cids - avlbl_cids));
1681                 } else
1682                         len += snprintf(buf+len, PAGE_SIZE - len,
1683                                         "ULP%d : %d\n", ulp_num, 0);
1684         }
1685
1686         return len;
1687 }
1688
1689 /**
1690  * beiscsi_free_session_disp()- Display Avaliable Session
1691  * @dev: ptr to device not used.
1692  * @attr: device attribute, not used.
1693  * @buf: contains formatted text Session Count
1694  *
1695  * return
1696  * size of the formatted string
1697  **/
1698 ssize_t
1699 beiscsi_free_session_disp(struct device *dev, struct device_attribute *attr,
1700                        char *buf)
1701 {
1702         struct Scsi_Host *shost = class_to_shost(dev);
1703         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1704         uint16_t ulp_num, len = 0;
1705
1706         for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
1707                 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported))
1708                         len += snprintf(buf+len, PAGE_SIZE - len,
1709                                         "ULP%d : %d\n", ulp_num,
1710                                         BEISCSI_ULP_AVLBL_CID(phba, ulp_num));
1711                 else
1712                         len += snprintf(buf+len, PAGE_SIZE - len,
1713                                         "ULP%d : %d\n", ulp_num, 0);
1714         }
1715
1716         return len;
1717 }
1718
1719 /**
1720  * beiscsi_adap_family_disp()- Display adapter family.
1721  * @dev: ptr to device to get priv structure
1722  * @attr: device attribute, not used.
1723  * @buf: contains formatted text driver name and version
1724  *
1725  * return
1726  * size of the formatted string
1727  **/
1728 ssize_t
1729 beiscsi_adap_family_disp(struct device *dev, struct device_attribute *attr,
1730                           char *buf)
1731 {
1732         uint16_t dev_id = 0;
1733         struct Scsi_Host *shost = class_to_shost(dev);
1734         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1735
1736         dev_id = phba->pcidev->device;
1737         switch (dev_id) {
1738         case BE_DEVICE_ID1:
1739         case OC_DEVICE_ID1:
1740         case OC_DEVICE_ID2:
1741                 return snprintf(buf, PAGE_SIZE, "BE2 Adapter Family\n");
1742                 break;
1743         case BE_DEVICE_ID2:
1744         case OC_DEVICE_ID3:
1745                 return snprintf(buf, PAGE_SIZE, "BE3-R Adapter Family\n");
1746                 break;
1747         case OC_SKH_ID1:
1748                 return snprintf(buf, PAGE_SIZE, "Skyhawk-R Adapter Family\n");
1749                 break;
1750         default:
1751                 return snprintf(buf, PAGE_SIZE,
1752                                 "Unknown Adapter Family: 0x%x\n", dev_id);
1753                 break;
1754         }
1755 }
1756
1757 /**
1758  * beiscsi_phys_port()- Display Physical Port Identifier
1759  * @dev: ptr to device not used.
1760  * @attr: device attribute, not used.
1761  * @buf: contains formatted text port identifier
1762  *
1763  * return
1764  * size of the formatted string
1765  **/
1766 ssize_t
1767 beiscsi_phys_port_disp(struct device *dev, struct device_attribute *attr,
1768                          char *buf)
1769 {
1770         struct Scsi_Host *shost = class_to_shost(dev);
1771         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1772
1773         return snprintf(buf, PAGE_SIZE, "Port Identifier : %d\n",
1774                         phba->fw_config.phys_port);
1775 }
1776
1777 void beiscsi_offload_cxn_v0(struct beiscsi_offload_params *params,
1778                              struct wrb_handle *pwrb_handle,
1779                              struct be_mem_descriptor *mem_descr,
1780                              struct hwi_wrb_context *pwrb_context)
1781 {
1782         struct iscsi_wrb *pwrb = pwrb_handle->pwrb;
1783
1784         memset(pwrb, 0, sizeof(*pwrb));
1785         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1786                       max_send_data_segment_length, pwrb,
1787                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1788                       max_send_data_segment_length) / 32]);
1789         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
1790                       BE_TGT_CTX_UPDT_CMD);
1791         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1792                       first_burst_length,
1793                       pwrb,
1794                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1795                       first_burst_length) / 32]);
1796         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
1797                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1798                       erl) / 32] & OFFLD_PARAMS_ERL));
1799         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
1800                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1801                        dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
1802         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
1803                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1804                       hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
1805         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
1806                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1807                       ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
1808         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
1809                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1810                       imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
1811         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
1812                       pwrb,
1813                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1814                       exp_statsn) / 32] + 1));
1815         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
1816                       pwrb, pwrb_handle->wrb_index);
1817
1818         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1819                       max_burst_length, pwrb, params->dw[offsetof
1820                       (struct amap_beiscsi_offload_params,
1821                       max_burst_length) / 32]);
1822
1823         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
1824                       pwrb, pwrb_handle->wrb_index);
1825         if (pwrb_context->plast_wrb)
1826                 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1827                               ptr2nextwrb,
1828                               pwrb_context->plast_wrb,
1829                               pwrb_handle->wrb_index);
1830         pwrb_context->plast_wrb = pwrb;
1831
1832         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1833                       session_state, pwrb, 0);
1834         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
1835                       pwrb, 1);
1836         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
1837                       pwrb, 0);
1838         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
1839                       0);
1840
1841         mem_descr += ISCSI_MEM_GLOBAL_HEADER;
1842         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1843                       pad_buffer_addr_hi, pwrb,
1844                       mem_descr->mem_array[0].bus_address.u.a32.address_hi);
1845         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1846                       pad_buffer_addr_lo, pwrb,
1847                       mem_descr->mem_array[0].bus_address.u.a32.address_lo);
1848 }
1849
1850 void beiscsi_offload_cxn_v2(struct beiscsi_offload_params *params,
1851                              struct wrb_handle *pwrb_handle,
1852                              struct hwi_wrb_context *pwrb_context)
1853 {
1854         struct iscsi_wrb *pwrb = pwrb_handle->pwrb;
1855
1856         memset(pwrb, 0, sizeof(*pwrb));
1857
1858         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1859                       max_burst_length, pwrb, params->dw[offsetof
1860                       (struct amap_beiscsi_offload_params,
1861                       max_burst_length) / 32]);
1862         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1863                       type, pwrb,
1864                       BE_TGT_CTX_UPDT_CMD);
1865         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1866                       ptr2nextwrb,
1867                       pwrb, pwrb_handle->wrb_index);
1868         if (pwrb_context->plast_wrb)
1869                 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1870                               ptr2nextwrb,
1871                               pwrb_context->plast_wrb,
1872                               pwrb_handle->wrb_index);
1873         pwrb_context->plast_wrb = pwrb;
1874
1875         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, wrb_idx,
1876                       pwrb, pwrb_handle->wrb_index);
1877         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1878                       max_send_data_segment_length, pwrb,
1879                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1880                       max_send_data_segment_length) / 32]);
1881         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1882                       first_burst_length, pwrb,
1883                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1884                       first_burst_length) / 32]);
1885         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1886                       max_recv_dataseg_len, pwrb,
1887                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1888                       max_recv_data_segment_length) / 32]);
1889         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1890                       max_cxns, pwrb, BEISCSI_MAX_CXNS);
1891         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, erl, pwrb,
1892                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1893                       erl) / 32] & OFFLD_PARAMS_ERL));
1894         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, dde, pwrb,
1895                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1896                       dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
1897         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, hde, pwrb,
1898                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1899                       hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
1900         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1901                       ir2t, pwrb,
1902                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1903                       ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
1904         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, imd, pwrb,
1905                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1906                       imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
1907         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1908                       data_seq_inorder,
1909                       pwrb,
1910                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1911                       data_seq_inorder) / 32] &
1912                       OFFLD_PARAMS_DATA_SEQ_INORDER) >> 6);
1913         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1914                       pdu_seq_inorder,
1915                       pwrb,
1916                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1917                       pdu_seq_inorder) / 32] &
1918                       OFFLD_PARAMS_PDU_SEQ_INORDER) >> 7);
1919         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, max_r2t,
1920                       pwrb,
1921                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1922                       max_r2t) / 32] &
1923                       OFFLD_PARAMS_MAX_R2T) >> 8);
1924         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, stat_sn,
1925                       pwrb,
1926                      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1927                       exp_statsn) / 32] + 1));
1928 }