011c822f5ff616c2afbcd917f8ac5422d739d0a3
[pandora-kernel.git] / drivers / scsi / qla4xxx / ql4_mbx.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2010 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7
8 #include "ql4_def.h"
9 #include "ql4_glbl.h"
10 #include "ql4_dbg.h"
11 #include "ql4_inline.h"
12
13
14 /**
15  * qla4xxx_mailbox_command - issues mailbox commands
16  * @ha: Pointer to host adapter structure.
17  * @inCount: number of mailbox registers to load.
18  * @outCount: number of mailbox registers to return.
19  * @mbx_cmd: data pointer for mailbox in registers.
20  * @mbx_sts: data pointer for mailbox out registers.
21  *
22  * This routine issue mailbox commands and waits for completion.
23  * If outCount is 0, this routine completes successfully WITHOUT waiting
24  * for the mailbox command to complete.
25  **/
26 int qla4xxx_mailbox_command(struct scsi_qla_host *ha, uint8_t inCount,
27                             uint8_t outCount, uint32_t *mbx_cmd,
28                             uint32_t *mbx_sts)
29 {
30         int status = QLA_ERROR;
31         uint8_t i;
32         u_long wait_count;
33         uint32_t intr_status;
34         unsigned long flags = 0;
35         uint32_t dev_state;
36
37         /* Make sure that pointers are valid */
38         if (!mbx_cmd || !mbx_sts) {
39                 DEBUG2(printk("scsi%ld: %s: Invalid mbx_cmd or mbx_sts "
40                               "pointer\n", ha->host_no, __func__));
41                 return status;
42         }
43
44         if (is_qla8022(ha)) {
45                 if (test_bit(AF_FW_RECOVERY, &ha->flags)) {
46                         DEBUG2(ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: "
47                             "prematurely completing mbx cmd as firmware "
48                             "recovery detected\n", ha->host_no, __func__));
49                         return status;
50                 }
51                 /* Do not send any mbx cmd if h/w is in failed state*/
52                 qla4_8xxx_idc_lock(ha);
53                 dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
54                 qla4_8xxx_idc_unlock(ha);
55                 if (dev_state == QLA82XX_DEV_FAILED) {
56                         ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: H/W is in "
57                             "failed state, do not send any mailbox commands\n",
58                             ha->host_no, __func__);
59                         return status;
60                 }
61         }
62
63         if ((is_aer_supported(ha)) &&
64             (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))) {
65                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Perm failure on EEH, "
66                     "timeout MBX Exiting.\n", ha->host_no, __func__));
67                 return status;
68         }
69
70         /* Mailbox code active */
71         wait_count = MBOX_TOV * 100;
72
73         while (wait_count--) {
74                 mutex_lock(&ha->mbox_sem);
75                 if (!test_bit(AF_MBOX_COMMAND, &ha->flags)) {
76                         set_bit(AF_MBOX_COMMAND, &ha->flags);
77                         mutex_unlock(&ha->mbox_sem);
78                         break;
79                 }
80                 mutex_unlock(&ha->mbox_sem);
81                 if (!wait_count) {
82                         DEBUG2(printk("scsi%ld: %s: mbox_sem failed\n",
83                                 ha->host_no, __func__));
84                         return status;
85                 }
86                 msleep(10);
87         }
88
89         spin_lock_irqsave(&ha->hardware_lock, flags);
90
91         ha->mbox_status_count = outCount;
92         for (i = 0; i < outCount; i++)
93                 ha->mbox_status[i] = 0;
94
95         if (is_qla8022(ha)) {
96                 /* Load all mailbox registers, except mailbox 0. */
97                 DEBUG5(
98                     printk("scsi%ld: %s: Cmd ", ha->host_no, __func__);
99                     for (i = 0; i < inCount; i++)
100                         printk("mb%d=%04x ", i, mbx_cmd[i]);
101                     printk("\n"));
102
103                 for (i = 1; i < inCount; i++)
104                         writel(mbx_cmd[i], &ha->qla4_8xxx_reg->mailbox_in[i]);
105                 writel(mbx_cmd[0], &ha->qla4_8xxx_reg->mailbox_in[0]);
106                 readl(&ha->qla4_8xxx_reg->mailbox_in[0]);
107                 writel(HINT_MBX_INT_PENDING, &ha->qla4_8xxx_reg->hint);
108         } else {
109                 /* Load all mailbox registers, except mailbox 0. */
110                 for (i = 1; i < inCount; i++)
111                         writel(mbx_cmd[i], &ha->reg->mailbox[i]);
112
113                 /* Wakeup firmware  */
114                 writel(mbx_cmd[0], &ha->reg->mailbox[0]);
115                 readl(&ha->reg->mailbox[0]);
116                 writel(set_rmask(CSR_INTR_RISC), &ha->reg->ctrl_status);
117                 readl(&ha->reg->ctrl_status);
118         }
119
120         spin_unlock_irqrestore(&ha->hardware_lock, flags);
121
122         /* Wait for completion */
123
124         /*
125          * If we don't want status, don't wait for the mailbox command to
126          * complete.  For example, MBOX_CMD_RESET_FW doesn't return status,
127          * you must poll the inbound Interrupt Mask for completion.
128          */
129         if (outCount == 0) {
130                 status = QLA_SUCCESS;
131                 goto mbox_exit;
132         }
133
134         /*
135          * Wait for completion: Poll or completion queue
136          */
137         if (test_bit(AF_IRQ_ATTACHED, &ha->flags) &&
138             test_bit(AF_INTERRUPTS_ON, &ha->flags) &&
139             test_bit(AF_ONLINE, &ha->flags) &&
140             !test_bit(AF_HA_REMOVAL, &ha->flags)) {
141                 /* Do not poll for completion. Use completion queue */
142                 set_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags);
143                 wait_for_completion_timeout(&ha->mbx_intr_comp, MBOX_TOV * HZ);
144                 clear_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags);
145         } else {
146                 /* Poll for command to complete */
147                 wait_count = jiffies + MBOX_TOV * HZ;
148                 while (test_bit(AF_MBOX_COMMAND_DONE, &ha->flags) == 0) {
149                         if (time_after_eq(jiffies, wait_count))
150                                 break;
151
152                         /*
153                          * Service the interrupt.
154                          * The ISR will save the mailbox status registers
155                          * to a temporary storage location in the adapter
156                          * structure.
157                          */
158
159                         spin_lock_irqsave(&ha->hardware_lock, flags);
160                         if (is_qla8022(ha)) {
161                                 intr_status =
162                                     readl(&ha->qla4_8xxx_reg->host_int);
163                                 if (intr_status & ISRX_82XX_RISC_INT) {
164                                         ha->mbox_status_count = outCount;
165                                         intr_status =
166                                          readl(&ha->qla4_8xxx_reg->host_status);
167                                         ha->isp_ops->interrupt_service_routine(
168                                             ha, intr_status);
169                                         if (test_bit(AF_INTERRUPTS_ON,
170                                             &ha->flags) &&
171                                             test_bit(AF_INTx_ENABLED,
172                                             &ha->flags))
173                                                 qla4_8xxx_wr_32(ha,
174                                                 ha->nx_legacy_intr.tgt_mask_reg,
175                                                 0xfbff);
176                                 }
177                         } else {
178                                 intr_status = readl(&ha->reg->ctrl_status);
179                                 if (intr_status & INTR_PENDING) {
180                                         /*
181                                          * Service the interrupt.
182                                          * The ISR will save the mailbox status
183                                          * registers to a temporary storage
184                                          * location in the adapter structure.
185                                          */
186                                         ha->mbox_status_count = outCount;
187                                         ha->isp_ops->interrupt_service_routine(
188                                             ha, intr_status);
189                                 }
190                         }
191                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
192                         msleep(10);
193                 }
194         }
195
196         /* Check for mailbox timeout. */
197         if (!test_bit(AF_MBOX_COMMAND_DONE, &ha->flags)) {
198                 if (is_qla8022(ha) &&
199                     test_bit(AF_FW_RECOVERY, &ha->flags)) {
200                         DEBUG2(ql4_printk(KERN_INFO, ha,
201                             "scsi%ld: %s: prematurely completing mbx cmd as "
202                             "firmware recovery detected\n",
203                             ha->host_no, __func__));
204                         goto mbox_exit;
205                 }
206                 DEBUG2(printk("scsi%ld: Mailbox Cmd 0x%08X timed out ...,"
207                               " Scheduling Adapter Reset\n", ha->host_no,
208                               mbx_cmd[0]));
209                 ha->mailbox_timeout_count++;
210                 mbx_sts[0] = (-1);
211                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
212                 goto mbox_exit;
213         }
214
215         /*
216          * Copy the mailbox out registers to the caller's mailbox in/out
217          * structure.
218          */
219         spin_lock_irqsave(&ha->hardware_lock, flags);
220         for (i = 0; i < outCount; i++)
221                 mbx_sts[i] = ha->mbox_status[i];
222
223         /* Set return status and error flags (if applicable). */
224         switch (ha->mbox_status[0]) {
225         case MBOX_STS_COMMAND_COMPLETE:
226                 status = QLA_SUCCESS;
227                 break;
228
229         case MBOX_STS_INTERMEDIATE_COMPLETION:
230                 status = QLA_SUCCESS;
231                 break;
232
233         case MBOX_STS_BUSY:
234                 DEBUG2( printk("scsi%ld: %s: Cmd = %08X, ISP BUSY\n",
235                                ha->host_no, __func__, mbx_cmd[0]));
236                 ha->mailbox_timeout_count++;
237                 break;
238
239         default:
240                 DEBUG2(printk("scsi%ld: %s: **** FAILED, cmd = %08X, "
241                               "sts = %08X ****\n", ha->host_no, __func__,
242                               mbx_cmd[0], mbx_sts[0]));
243                 break;
244         }
245         spin_unlock_irqrestore(&ha->hardware_lock, flags);
246
247 mbox_exit:
248         mutex_lock(&ha->mbox_sem);
249         clear_bit(AF_MBOX_COMMAND, &ha->flags);
250         mutex_unlock(&ha->mbox_sem);
251         clear_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
252
253         return status;
254 }
255
256 void qla4xxx_mailbox_premature_completion(struct scsi_qla_host *ha)
257 {
258         set_bit(AF_FW_RECOVERY, &ha->flags);
259         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: set FW RECOVERY!\n",
260             ha->host_no, __func__);
261
262         if (test_bit(AF_MBOX_COMMAND, &ha->flags)) {
263                 if (test_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags)) {
264                         complete(&ha->mbx_intr_comp);
265                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Due to fw "
266                             "recovery, doing premature completion of "
267                             "mbx cmd\n", ha->host_no, __func__);
268
269                 } else {
270                         set_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
271                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Due to fw "
272                             "recovery, doing premature completion of "
273                             "polling mbx cmd\n", ha->host_no, __func__);
274                 }
275         }
276 }
277
278 static uint8_t
279 qla4xxx_set_ifcb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
280                  uint32_t *mbox_sts, dma_addr_t init_fw_cb_dma)
281 {
282         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
283         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
284
285         if (is_qla8022(ha))
286                 qla4_8xxx_wr_32(ha, ha->nx_db_wr_ptr, 0);
287
288         mbox_cmd[0] = MBOX_CMD_INITIALIZE_FIRMWARE;
289         mbox_cmd[1] = 0;
290         mbox_cmd[2] = LSDW(init_fw_cb_dma);
291         mbox_cmd[3] = MSDW(init_fw_cb_dma);
292         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
293         mbox_cmd[5] = (IFCB_VER_MAX << 8) | IFCB_VER_MIN;
294
295         if (qla4xxx_mailbox_command(ha, 6, 6, mbox_cmd, mbox_sts) !=
296             QLA_SUCCESS) {
297                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: "
298                               "MBOX_CMD_INITIALIZE_FIRMWARE"
299                               " failed w/ status %04X\n",
300                               ha->host_no, __func__, mbox_sts[0]));
301                 return QLA_ERROR;
302         }
303         return QLA_SUCCESS;
304 }
305
306 uint8_t
307 qla4xxx_get_ifcb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
308                  uint32_t *mbox_sts, dma_addr_t init_fw_cb_dma)
309 {
310         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
311         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
312         mbox_cmd[0] = MBOX_CMD_GET_INIT_FW_CTRL_BLOCK;
313         mbox_cmd[2] = LSDW(init_fw_cb_dma);
314         mbox_cmd[3] = MSDW(init_fw_cb_dma);
315         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
316
317         if (qla4xxx_mailbox_command(ha, 5, 5, mbox_cmd, mbox_sts) !=
318             QLA_SUCCESS) {
319                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: "
320                               "MBOX_CMD_GET_INIT_FW_CTRL_BLOCK"
321                               " failed w/ status %04X\n",
322                               ha->host_no, __func__, mbox_sts[0]));
323                 return QLA_ERROR;
324         }
325         return QLA_SUCCESS;
326 }
327
328 static void
329 qla4xxx_update_local_ip(struct scsi_qla_host *ha,
330                         struct addr_ctrl_blk *init_fw_cb)
331 {
332         ha->ip_config.tcp_options = le16_to_cpu(init_fw_cb->ipv4_tcp_opts);
333         ha->ip_config.ipv4_options = le16_to_cpu(init_fw_cb->ipv4_ip_opts);
334         ha->ip_config.ipv4_addr_state =
335                                 le16_to_cpu(init_fw_cb->ipv4_addr_state);
336         ha->ip_config.eth_mtu_size =
337                                 le16_to_cpu(init_fw_cb->eth_mtu_size);
338
339         if (ha->acb_version == ACB_SUPPORTED) {
340                 ha->ip_config.ipv6_options = le16_to_cpu(init_fw_cb->ipv6_opts);
341                 ha->ip_config.ipv6_addl_options =
342                                 le16_to_cpu(init_fw_cb->ipv6_addtl_opts);
343         }
344
345         /* Save IPv4 Address Info */
346         memcpy(ha->ip_config.ip_address, init_fw_cb->ipv4_addr,
347                min(sizeof(ha->ip_config.ip_address),
348                    sizeof(init_fw_cb->ipv4_addr)));
349         memcpy(ha->ip_config.subnet_mask, init_fw_cb->ipv4_subnet,
350                min(sizeof(ha->ip_config.subnet_mask),
351                    sizeof(init_fw_cb->ipv4_subnet)));
352         memcpy(ha->ip_config.gateway, init_fw_cb->ipv4_gw_addr,
353                min(sizeof(ha->ip_config.gateway),
354                    sizeof(init_fw_cb->ipv4_gw_addr)));
355
356         ha->ip_config.ipv4_vlan_tag = be16_to_cpu(init_fw_cb->ipv4_vlan_tag);
357
358         if (is_ipv6_enabled(ha)) {
359                 /* Save IPv6 Address */
360                 ha->ip_config.ipv6_link_local_state =
361                         le16_to_cpu(init_fw_cb->ipv6_lnk_lcl_addr_state);
362                 ha->ip_config.ipv6_addr0_state =
363                                 le16_to_cpu(init_fw_cb->ipv6_addr0_state);
364                 ha->ip_config.ipv6_addr1_state =
365                                 le16_to_cpu(init_fw_cb->ipv6_addr1_state);
366                 ha->ip_config.ipv6_default_router_state =
367                                 le16_to_cpu(init_fw_cb->ipv6_dflt_rtr_state);
368                 ha->ip_config.ipv6_link_local_addr.in6_u.u6_addr8[0] = 0xFE;
369                 ha->ip_config.ipv6_link_local_addr.in6_u.u6_addr8[1] = 0x80;
370
371                 memcpy(&ha->ip_config.ipv6_link_local_addr.in6_u.u6_addr8[8],
372                        init_fw_cb->ipv6_if_id,
373                        min(sizeof(ha->ip_config.ipv6_link_local_addr)/2,
374                            sizeof(init_fw_cb->ipv6_if_id)));
375                 memcpy(&ha->ip_config.ipv6_addr0, init_fw_cb->ipv6_addr0,
376                        min(sizeof(ha->ip_config.ipv6_addr0),
377                            sizeof(init_fw_cb->ipv6_addr0)));
378                 memcpy(&ha->ip_config.ipv6_addr1, init_fw_cb->ipv6_addr1,
379                        min(sizeof(ha->ip_config.ipv6_addr1),
380                            sizeof(init_fw_cb->ipv6_addr1)));
381                 memcpy(&ha->ip_config.ipv6_default_router_addr,
382                        init_fw_cb->ipv6_dflt_rtr_addr,
383                        min(sizeof(ha->ip_config.ipv6_default_router_addr),
384                            sizeof(init_fw_cb->ipv6_dflt_rtr_addr)));
385                 ha->ip_config.ipv6_vlan_tag =
386                                 be16_to_cpu(init_fw_cb->ipv6_vlan_tag);
387         }
388 }
389
390 uint8_t
391 qla4xxx_update_local_ifcb(struct scsi_qla_host *ha,
392                           uint32_t *mbox_cmd,
393                           uint32_t *mbox_sts,
394                           struct addr_ctrl_blk  *init_fw_cb,
395                           dma_addr_t init_fw_cb_dma)
396 {
397         if (qla4xxx_get_ifcb(ha, mbox_cmd, mbox_sts, init_fw_cb_dma)
398             != QLA_SUCCESS) {
399                 DEBUG2(printk(KERN_WARNING
400                               "scsi%ld: %s: Failed to get init_fw_ctrl_blk\n",
401                               ha->host_no, __func__));
402                 return QLA_ERROR;
403         }
404
405         DEBUG2(qla4xxx_dump_buffer(init_fw_cb, sizeof(struct addr_ctrl_blk)));
406
407         /* Save some info in adapter structure. */
408         ha->acb_version = init_fw_cb->acb_version;
409         ha->firmware_options = le16_to_cpu(init_fw_cb->fw_options);
410         ha->heartbeat_interval = init_fw_cb->hb_interval;
411         memcpy(ha->name_string, init_fw_cb->iscsi_name,
412                 min(sizeof(ha->name_string),
413                 sizeof(init_fw_cb->iscsi_name)));
414         /*memcpy(ha->alias, init_fw_cb->Alias,
415                min(sizeof(ha->alias), sizeof(init_fw_cb->Alias)));*/
416
417         qla4xxx_update_local_ip(ha, init_fw_cb);
418
419         return QLA_SUCCESS;
420 }
421
422 /**
423  * qla4xxx_initialize_fw_cb - initializes firmware control block.
424  * @ha: Pointer to host adapter structure.
425  **/
426 int qla4xxx_initialize_fw_cb(struct scsi_qla_host * ha)
427 {
428         struct addr_ctrl_blk *init_fw_cb;
429         dma_addr_t init_fw_cb_dma;
430         uint32_t mbox_cmd[MBOX_REG_COUNT];
431         uint32_t mbox_sts[MBOX_REG_COUNT];
432         int status = QLA_ERROR;
433
434         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
435                                         sizeof(struct addr_ctrl_blk),
436                                         &init_fw_cb_dma, GFP_KERNEL);
437         if (init_fw_cb == NULL) {
438                 DEBUG2(printk("scsi%ld: %s: Unable to alloc init_cb\n",
439                               ha->host_no, __func__));
440                 goto exit_init_fw_cb_no_free;
441         }
442         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
443
444         /* Get Initialize Firmware Control Block. */
445         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
446         memset(&mbox_sts, 0, sizeof(mbox_sts));
447
448         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma) !=
449             QLA_SUCCESS) {
450                 dma_free_coherent(&ha->pdev->dev,
451                                   sizeof(struct addr_ctrl_blk),
452                                   init_fw_cb, init_fw_cb_dma);
453                 goto exit_init_fw_cb;
454         }
455
456         /* Initialize request and response queues. */
457         qla4xxx_init_rings(ha);
458
459         /* Fill in the request and response queue information. */
460         init_fw_cb->rqq_consumer_idx = cpu_to_le16(ha->request_out);
461         init_fw_cb->compq_producer_idx = cpu_to_le16(ha->response_in);
462         init_fw_cb->rqq_len = __constant_cpu_to_le16(REQUEST_QUEUE_DEPTH);
463         init_fw_cb->compq_len = __constant_cpu_to_le16(RESPONSE_QUEUE_DEPTH);
464         init_fw_cb->rqq_addr_lo = cpu_to_le32(LSDW(ha->request_dma));
465         init_fw_cb->rqq_addr_hi = cpu_to_le32(MSDW(ha->request_dma));
466         init_fw_cb->compq_addr_lo = cpu_to_le32(LSDW(ha->response_dma));
467         init_fw_cb->compq_addr_hi = cpu_to_le32(MSDW(ha->response_dma));
468         init_fw_cb->shdwreg_addr_lo = cpu_to_le32(LSDW(ha->shadow_regs_dma));
469         init_fw_cb->shdwreg_addr_hi = cpu_to_le32(MSDW(ha->shadow_regs_dma));
470
471         /* Set up required options. */
472         init_fw_cb->fw_options |=
473                 __constant_cpu_to_le16(FWOPT_SESSION_MODE |
474                                        FWOPT_INITIATOR_MODE);
475
476         if (is_qla8022(ha))
477                 init_fw_cb->fw_options |=
478                     __constant_cpu_to_le16(FWOPT_ENABLE_CRBDB);
479
480         init_fw_cb->fw_options &= __constant_cpu_to_le16(~FWOPT_TARGET_MODE);
481
482         init_fw_cb->add_fw_options = 0;
483         init_fw_cb->add_fw_options |=
484                         __constant_cpu_to_le16(ADFWOPT_SERIALIZE_TASK_MGMT);
485         init_fw_cb->add_fw_options |=
486                         __constant_cpu_to_le16(ADFWOPT_AUTOCONN_DISABLE);
487
488         if (qla4xxx_set_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)
489                 != QLA_SUCCESS) {
490                 DEBUG2(printk(KERN_WARNING
491                               "scsi%ld: %s: Failed to set init_fw_ctrl_blk\n",
492                               ha->host_no, __func__));
493                 goto exit_init_fw_cb;
494         }
495
496         if (qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0],
497                 init_fw_cb, init_fw_cb_dma) != QLA_SUCCESS) {
498                 DEBUG2(printk("scsi%ld: %s: Failed to update local ifcb\n",
499                                 ha->host_no, __func__));
500                 goto exit_init_fw_cb;
501         }
502         status = QLA_SUCCESS;
503
504 exit_init_fw_cb:
505         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
506                                 init_fw_cb, init_fw_cb_dma);
507 exit_init_fw_cb_no_free:
508         return status;
509 }
510
511 /**
512  * qla4xxx_get_dhcp_ip_address - gets HBA ip address via DHCP
513  * @ha: Pointer to host adapter structure.
514  **/
515 int qla4xxx_get_dhcp_ip_address(struct scsi_qla_host * ha)
516 {
517         struct addr_ctrl_blk *init_fw_cb;
518         dma_addr_t init_fw_cb_dma;
519         uint32_t mbox_cmd[MBOX_REG_COUNT];
520         uint32_t mbox_sts[MBOX_REG_COUNT];
521
522         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
523                                         sizeof(struct addr_ctrl_blk),
524                                         &init_fw_cb_dma, GFP_KERNEL);
525         if (init_fw_cb == NULL) {
526                 printk("scsi%ld: %s: Unable to alloc init_cb\n", ha->host_no,
527                        __func__);
528                 return QLA_ERROR;
529         }
530
531         /* Get Initialize Firmware Control Block. */
532         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
533         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma) !=
534             QLA_SUCCESS) {
535                 DEBUG2(printk("scsi%ld: %s: Failed to get init_fw_ctrl_blk\n",
536                               ha->host_no, __func__));
537                 dma_free_coherent(&ha->pdev->dev,
538                                   sizeof(struct addr_ctrl_blk),
539                                   init_fw_cb, init_fw_cb_dma);
540                 return QLA_ERROR;
541         }
542
543         /* Save IP Address. */
544         qla4xxx_update_local_ip(ha, init_fw_cb);
545         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
546                                 init_fw_cb, init_fw_cb_dma);
547
548         return QLA_SUCCESS;
549 }
550
551 /**
552  * qla4xxx_get_firmware_state - gets firmware state of HBA
553  * @ha: Pointer to host adapter structure.
554  **/
555 int qla4xxx_get_firmware_state(struct scsi_qla_host * ha)
556 {
557         uint32_t mbox_cmd[MBOX_REG_COUNT];
558         uint32_t mbox_sts[MBOX_REG_COUNT];
559
560         /* Get firmware version */
561         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
562         memset(&mbox_sts, 0, sizeof(mbox_sts));
563
564         mbox_cmd[0] = MBOX_CMD_GET_FW_STATE;
565
566         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 4, &mbox_cmd[0], &mbox_sts[0]) !=
567             QLA_SUCCESS) {
568                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_GET_FW_STATE failed w/ "
569                               "status %04X\n", ha->host_no, __func__,
570                               mbox_sts[0]));
571                 return QLA_ERROR;
572         }
573         ha->firmware_state = mbox_sts[1];
574         ha->board_id = mbox_sts[2];
575         ha->addl_fw_state = mbox_sts[3];
576         DEBUG2(printk("scsi%ld: %s firmware_state=0x%x\n",
577                       ha->host_no, __func__, ha->firmware_state);)
578
579         return QLA_SUCCESS;
580 }
581
582 /**
583  * qla4xxx_get_firmware_status - retrieves firmware status
584  * @ha: Pointer to host adapter structure.
585  **/
586 int qla4xxx_get_firmware_status(struct scsi_qla_host * ha)
587 {
588         uint32_t mbox_cmd[MBOX_REG_COUNT];
589         uint32_t mbox_sts[MBOX_REG_COUNT];
590
591         /* Get firmware version */
592         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
593         memset(&mbox_sts, 0, sizeof(mbox_sts));
594
595         mbox_cmd[0] = MBOX_CMD_GET_FW_STATUS;
596
597         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 3, &mbox_cmd[0], &mbox_sts[0]) !=
598             QLA_SUCCESS) {
599                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_GET_FW_STATUS failed w/ "
600                               "status %04X\n", ha->host_no, __func__,
601                               mbox_sts[0]));
602                 return QLA_ERROR;
603         }
604
605         ql4_printk(KERN_INFO, ha, "%ld firmare IOCBs available (%d).\n",
606             ha->host_no, mbox_sts[2]);
607
608         return QLA_SUCCESS;
609 }
610
611 /**
612  * qla4xxx_get_fwddb_entry - retrieves firmware ddb entry
613  * @ha: Pointer to host adapter structure.
614  * @fw_ddb_index: Firmware's device database index
615  * @fw_ddb_entry: Pointer to firmware's device database entry structure
616  * @num_valid_ddb_entries: Pointer to number of valid ddb entries
617  * @next_ddb_index: Pointer to next valid device database index
618  * @fw_ddb_device_state: Pointer to device state
619  **/
620 int qla4xxx_get_fwddb_entry(struct scsi_qla_host *ha,
621                             uint16_t fw_ddb_index,
622                             struct dev_db_entry *fw_ddb_entry,
623                             dma_addr_t fw_ddb_entry_dma,
624                             uint32_t *num_valid_ddb_entries,
625                             uint32_t *next_ddb_index,
626                             uint32_t *fw_ddb_device_state,
627                             uint32_t *conn_err_detail,
628                             uint16_t *tcp_source_port_num,
629                             uint16_t *connection_id)
630 {
631         int status = QLA_ERROR;
632         uint16_t options;
633         uint32_t mbox_cmd[MBOX_REG_COUNT];
634         uint32_t mbox_sts[MBOX_REG_COUNT];
635
636         /* Make sure the device index is valid */
637         if (fw_ddb_index >= MAX_DDB_ENTRIES) {
638                 DEBUG2(printk("scsi%ld: %s: ddb [%d] out of range.\n",
639                               ha->host_no, __func__, fw_ddb_index));
640                 goto exit_get_fwddb;
641         }
642         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
643         memset(&mbox_sts, 0, sizeof(mbox_sts));
644
645         mbox_cmd[0] = MBOX_CMD_GET_DATABASE_ENTRY;
646         mbox_cmd[1] = (uint32_t) fw_ddb_index;
647         mbox_cmd[2] = LSDW(fw_ddb_entry_dma);
648         mbox_cmd[3] = MSDW(fw_ddb_entry_dma);
649         mbox_cmd[4] = sizeof(struct dev_db_entry);
650
651         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 7, &mbox_cmd[0], &mbox_sts[0]) ==
652             QLA_ERROR) {
653                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_GET_DATABASE_ENTRY failed"
654                               " with status 0x%04X\n", ha->host_no, __func__,
655                               mbox_sts[0]));
656                 goto exit_get_fwddb;
657         }
658         if (fw_ddb_index != mbox_sts[1]) {
659                 DEBUG2(printk("scsi%ld: %s: ddb mismatch [%d] != [%d].\n",
660                               ha->host_no, __func__, fw_ddb_index,
661                               mbox_sts[1]));
662                 goto exit_get_fwddb;
663         }
664         if (fw_ddb_entry) {
665                 options = le16_to_cpu(fw_ddb_entry->options);
666                 if (options & DDB_OPT_IPV6_DEVICE) {
667                         ql4_printk(KERN_INFO, ha, "%s: DDB[%d] MB0 %04x Tot %d "
668                                 "Next %d State %04x ConnErr %08x %pI6 "
669                                 ":%04d \"%s\"\n", __func__, fw_ddb_index,
670                                 mbox_sts[0], mbox_sts[2], mbox_sts[3],
671                                 mbox_sts[4], mbox_sts[5],
672                                 fw_ddb_entry->ip_addr,
673                                 le16_to_cpu(fw_ddb_entry->port),
674                                 fw_ddb_entry->iscsi_name);
675                 } else {
676                         ql4_printk(KERN_INFO, ha, "%s: DDB[%d] MB0 %04x Tot %d "
677                                 "Next %d State %04x ConnErr %08x %pI4 "
678                                 ":%04d \"%s\"\n", __func__, fw_ddb_index,
679                                 mbox_sts[0], mbox_sts[2], mbox_sts[3],
680                                 mbox_sts[4], mbox_sts[5],
681                                 fw_ddb_entry->ip_addr,
682                                 le16_to_cpu(fw_ddb_entry->port),
683                                 fw_ddb_entry->iscsi_name);
684                 }
685         }
686         if (num_valid_ddb_entries)
687                 *num_valid_ddb_entries = mbox_sts[2];
688         if (next_ddb_index)
689                 *next_ddb_index = mbox_sts[3];
690         if (fw_ddb_device_state)
691                 *fw_ddb_device_state = mbox_sts[4];
692
693         /*
694          * RA: This mailbox has been changed to pass connection error and
695          * details.  Its true for ISP4010 as per Version E - Not sure when it
696          * was changed.  Get the time2wait from the fw_dd_entry field :
697          * default_time2wait which we call it as minTime2Wait DEV_DB_ENTRY
698          * struct.
699          */
700         if (conn_err_detail)
701                 *conn_err_detail = mbox_sts[5];
702         if (tcp_source_port_num)
703                 *tcp_source_port_num = (uint16_t) (mbox_sts[6] >> 16);
704         if (connection_id)
705                 *connection_id = (uint16_t) mbox_sts[6] & 0x00FF;
706         status = QLA_SUCCESS;
707
708 exit_get_fwddb:
709         return status;
710 }
711
712 int qla4xxx_conn_open(struct scsi_qla_host *ha, uint16_t fw_ddb_index)
713 {
714         uint32_t mbox_cmd[MBOX_REG_COUNT];
715         uint32_t mbox_sts[MBOX_REG_COUNT];
716         int status;
717
718         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
719         memset(&mbox_sts, 0, sizeof(mbox_sts));
720
721         mbox_cmd[0] = MBOX_CMD_CONN_OPEN;
722         mbox_cmd[1] = fw_ddb_index;
723
724         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 2, &mbox_cmd[0],
725                                          &mbox_sts[0]);
726         DEBUG2(ql4_printk(KERN_INFO, ha,
727                           "%s: status = %d mbx0 = 0x%x mbx1 = 0x%x\n",
728                           __func__, status, mbox_sts[0], mbox_sts[1]));
729         return status;
730 }
731
732 /**
733  * qla4xxx_set_fwddb_entry - sets a ddb entry.
734  * @ha: Pointer to host adapter structure.
735  * @fw_ddb_index: Firmware's device database index
736  * @fw_ddb_entry_dma: dma address of ddb entry
737  * @mbx_sts: mailbox 0 to be returned or NULL
738  *
739  * This routine initializes or updates the adapter's device database
740  * entry for the specified device.
741  **/
742 int qla4xxx_set_ddb_entry(struct scsi_qla_host * ha, uint16_t fw_ddb_index,
743                           dma_addr_t fw_ddb_entry_dma, uint32_t *mbx_sts)
744 {
745         uint32_t mbox_cmd[MBOX_REG_COUNT];
746         uint32_t mbox_sts[MBOX_REG_COUNT];
747         int status;
748
749         /* Do not wait for completion. The firmware will send us an
750          * ASTS_DATABASE_CHANGED (0x8014) to notify us of the login status.
751          */
752         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
753         memset(&mbox_sts, 0, sizeof(mbox_sts));
754
755         mbox_cmd[0] = MBOX_CMD_SET_DATABASE_ENTRY;
756         mbox_cmd[1] = (uint32_t) fw_ddb_index;
757         mbox_cmd[2] = LSDW(fw_ddb_entry_dma);
758         mbox_cmd[3] = MSDW(fw_ddb_entry_dma);
759         mbox_cmd[4] = sizeof(struct dev_db_entry);
760
761         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0],
762                                          &mbox_sts[0]);
763         if (mbx_sts)
764                 *mbx_sts = mbox_sts[0];
765         DEBUG2(printk("scsi%ld: %s: status=%d mbx0=0x%x mbx4=0x%x\n",
766             ha->host_no, __func__, status, mbox_sts[0], mbox_sts[4]);)
767
768         return status;
769 }
770
771 int qla4xxx_session_logout_ddb(struct scsi_qla_host *ha,
772                                struct ddb_entry *ddb_entry, int options)
773 {
774         int status;
775         uint32_t mbox_cmd[MBOX_REG_COUNT];
776         uint32_t mbox_sts[MBOX_REG_COUNT];
777
778         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
779         memset(&mbox_sts, 0, sizeof(mbox_sts));
780
781         mbox_cmd[0] = MBOX_CMD_CONN_CLOSE_SESS_LOGOUT;
782         mbox_cmd[1] = ddb_entry->fw_ddb_index;
783         mbox_cmd[3] = options;
784
785         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 2, &mbox_cmd[0],
786                                          &mbox_sts[0]);
787         if (status != QLA_SUCCESS) {
788                 DEBUG2(ql4_printk(KERN_INFO, ha,
789                                   "%s: MBOX_CMD_CONN_CLOSE_SESS_LOGOUT "
790                                   "failed sts %04X %04X", __func__,
791                                   mbox_sts[0], mbox_sts[1]));
792         }
793
794         return status;
795 }
796
797 /**
798  * qla4xxx_get_crash_record - retrieves crash record.
799  * @ha: Pointer to host adapter structure.
800  *
801  * This routine retrieves a crash record from the QLA4010 after an 8002h aen.
802  **/
803 void qla4xxx_get_crash_record(struct scsi_qla_host * ha)
804 {
805         uint32_t mbox_cmd[MBOX_REG_COUNT];
806         uint32_t mbox_sts[MBOX_REG_COUNT];
807         struct crash_record *crash_record = NULL;
808         dma_addr_t crash_record_dma = 0;
809         uint32_t crash_record_size = 0;
810
811         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
812         memset(&mbox_sts, 0, sizeof(mbox_cmd));
813
814         /* Get size of crash record. */
815         mbox_cmd[0] = MBOX_CMD_GET_CRASH_RECORD;
816
817         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
818             QLA_SUCCESS) {
819                 DEBUG2(printk("scsi%ld: %s: ERROR: Unable to retrieve size!\n",
820                               ha->host_no, __func__));
821                 goto exit_get_crash_record;
822         }
823         crash_record_size = mbox_sts[4];
824         if (crash_record_size == 0) {
825                 DEBUG2(printk("scsi%ld: %s: ERROR: Crash record size is 0!\n",
826                               ha->host_no, __func__));
827                 goto exit_get_crash_record;
828         }
829
830         /* Alloc Memory for Crash Record. */
831         crash_record = dma_alloc_coherent(&ha->pdev->dev, crash_record_size,
832                                           &crash_record_dma, GFP_KERNEL);
833         if (crash_record == NULL)
834                 goto exit_get_crash_record;
835
836         /* Get Crash Record. */
837         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
838         memset(&mbox_sts, 0, sizeof(mbox_cmd));
839
840         mbox_cmd[0] = MBOX_CMD_GET_CRASH_RECORD;
841         mbox_cmd[2] = LSDW(crash_record_dma);
842         mbox_cmd[3] = MSDW(crash_record_dma);
843         mbox_cmd[4] = crash_record_size;
844
845         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
846             QLA_SUCCESS)
847                 goto exit_get_crash_record;
848
849         /* Dump Crash Record. */
850
851 exit_get_crash_record:
852         if (crash_record)
853                 dma_free_coherent(&ha->pdev->dev, crash_record_size,
854                                   crash_record, crash_record_dma);
855 }
856
857 /**
858  * qla4xxx_get_conn_event_log - retrieves connection event log
859  * @ha: Pointer to host adapter structure.
860  **/
861 void qla4xxx_get_conn_event_log(struct scsi_qla_host * ha)
862 {
863         uint32_t mbox_cmd[MBOX_REG_COUNT];
864         uint32_t mbox_sts[MBOX_REG_COUNT];
865         struct conn_event_log_entry *event_log = NULL;
866         dma_addr_t event_log_dma = 0;
867         uint32_t event_log_size = 0;
868         uint32_t num_valid_entries;
869         uint32_t      oldest_entry = 0;
870         uint32_t        max_event_log_entries;
871         uint8_t         i;
872
873
874         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
875         memset(&mbox_sts, 0, sizeof(mbox_cmd));
876
877         /* Get size of crash record. */
878         mbox_cmd[0] = MBOX_CMD_GET_CONN_EVENT_LOG;
879
880         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
881             QLA_SUCCESS)
882                 goto exit_get_event_log;
883
884         event_log_size = mbox_sts[4];
885         if (event_log_size == 0)
886                 goto exit_get_event_log;
887
888         /* Alloc Memory for Crash Record. */
889         event_log = dma_alloc_coherent(&ha->pdev->dev, event_log_size,
890                                        &event_log_dma, GFP_KERNEL);
891         if (event_log == NULL)
892                 goto exit_get_event_log;
893
894         /* Get Crash Record. */
895         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
896         memset(&mbox_sts, 0, sizeof(mbox_cmd));
897
898         mbox_cmd[0] = MBOX_CMD_GET_CONN_EVENT_LOG;
899         mbox_cmd[2] = LSDW(event_log_dma);
900         mbox_cmd[3] = MSDW(event_log_dma);
901
902         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
903             QLA_SUCCESS) {
904                 DEBUG2(printk("scsi%ld: %s: ERROR: Unable to retrieve event "
905                               "log!\n", ha->host_no, __func__));
906                 goto exit_get_event_log;
907         }
908
909         /* Dump Event Log. */
910         num_valid_entries = mbox_sts[1];
911
912         max_event_log_entries = event_log_size /
913                 sizeof(struct conn_event_log_entry);
914
915         if (num_valid_entries > max_event_log_entries)
916                 oldest_entry = num_valid_entries % max_event_log_entries;
917
918         DEBUG3(printk("scsi%ld: Connection Event Log Dump (%d entries):\n",
919                       ha->host_no, num_valid_entries));
920
921         if (ql4xextended_error_logging == 3) {
922                 if (oldest_entry == 0) {
923                         /* Circular Buffer has not wrapped around */
924                         for (i=0; i < num_valid_entries; i++) {
925                                 qla4xxx_dump_buffer((uint8_t *)event_log+
926                                                     (i*sizeof(*event_log)),
927                                                     sizeof(*event_log));
928                         }
929                 }
930                 else {
931                         /* Circular Buffer has wrapped around -
932                          * display accordingly*/
933                         for (i=oldest_entry; i < max_event_log_entries; i++) {
934                                 qla4xxx_dump_buffer((uint8_t *)event_log+
935                                                     (i*sizeof(*event_log)),
936                                                     sizeof(*event_log));
937                         }
938                         for (i=0; i < oldest_entry; i++) {
939                                 qla4xxx_dump_buffer((uint8_t *)event_log+
940                                                     (i*sizeof(*event_log)),
941                                                     sizeof(*event_log));
942                         }
943                 }
944         }
945
946 exit_get_event_log:
947         if (event_log)
948                 dma_free_coherent(&ha->pdev->dev, event_log_size, event_log,
949                                   event_log_dma);
950 }
951
952 /**
953  * qla4xxx_abort_task - issues Abort Task
954  * @ha: Pointer to host adapter structure.
955  * @srb: Pointer to srb entry
956  *
957  * This routine performs a LUN RESET on the specified target/lun.
958  * The caller must ensure that the ddb_entry and lun_entry pointers
959  * are valid before calling this routine.
960  **/
961 int qla4xxx_abort_task(struct scsi_qla_host *ha, struct srb *srb)
962 {
963         uint32_t mbox_cmd[MBOX_REG_COUNT];
964         uint32_t mbox_sts[MBOX_REG_COUNT];
965         struct scsi_cmnd *cmd = srb->cmd;
966         int status = QLA_SUCCESS;
967         unsigned long flags = 0;
968         uint32_t index;
969
970         /*
971          * Send abort task command to ISP, so that the ISP will return
972          * request with ABORT status
973          */
974         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
975         memset(&mbox_sts, 0, sizeof(mbox_sts));
976
977         spin_lock_irqsave(&ha->hardware_lock, flags);
978         index = (unsigned long)(unsigned char *)cmd->host_scribble;
979         spin_unlock_irqrestore(&ha->hardware_lock, flags);
980
981         /* Firmware already posted completion on response queue */
982         if (index == MAX_SRBS)
983                 return status;
984
985         mbox_cmd[0] = MBOX_CMD_ABORT_TASK;
986         mbox_cmd[1] = srb->ddb->fw_ddb_index;
987         mbox_cmd[2] = index;
988         /* Immediate Command Enable */
989         mbox_cmd[5] = 0x01;
990
991         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0],
992             &mbox_sts[0]);
993         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE) {
994                 status = QLA_ERROR;
995
996                 DEBUG2(printk(KERN_WARNING "scsi%ld:%d:%d: abort task FAILED: "
997                     "mbx0=%04X, mb1=%04X, mb2=%04X, mb3=%04X, mb4=%04X\n",
998                     ha->host_no, cmd->device->id, cmd->device->lun, mbox_sts[0],
999                     mbox_sts[1], mbox_sts[2], mbox_sts[3], mbox_sts[4]));
1000         }
1001
1002         return status;
1003 }
1004
1005 /**
1006  * qla4xxx_reset_lun - issues LUN Reset
1007  * @ha: Pointer to host adapter structure.
1008  * @ddb_entry: Pointer to device database entry
1009  * @lun: lun number
1010  *
1011  * This routine performs a LUN RESET on the specified target/lun.
1012  * The caller must ensure that the ddb_entry and lun_entry pointers
1013  * are valid before calling this routine.
1014  **/
1015 int qla4xxx_reset_lun(struct scsi_qla_host * ha, struct ddb_entry * ddb_entry,
1016                       int lun)
1017 {
1018         uint32_t mbox_cmd[MBOX_REG_COUNT];
1019         uint32_t mbox_sts[MBOX_REG_COUNT];
1020         int status = QLA_SUCCESS;
1021
1022         DEBUG2(printk("scsi%ld:%d:%d: lun reset issued\n", ha->host_no,
1023                       ddb_entry->fw_ddb_index, lun));
1024
1025         /*
1026          * Send lun reset command to ISP, so that the ISP will return all
1027          * outstanding requests with RESET status
1028          */
1029         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1030         memset(&mbox_sts, 0, sizeof(mbox_sts));
1031
1032         mbox_cmd[0] = MBOX_CMD_LUN_RESET;
1033         mbox_cmd[1] = ddb_entry->fw_ddb_index;
1034         mbox_cmd[2] = lun << 8;
1035         mbox_cmd[5] = 0x01;     /* Immediate Command Enable */
1036
1037         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0], &mbox_sts[0]);
1038         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE &&
1039             mbox_sts[0] != MBOX_STS_COMMAND_ERROR)
1040                 status = QLA_ERROR;
1041
1042         return status;
1043 }
1044
1045 /**
1046  * qla4xxx_reset_target - issues target Reset
1047  * @ha: Pointer to host adapter structure.
1048  * @db_entry: Pointer to device database entry
1049  * @un_entry: Pointer to lun entry structure
1050  *
1051  * This routine performs a TARGET RESET on the specified target.
1052  * The caller must ensure that the ddb_entry pointers
1053  * are valid before calling this routine.
1054  **/
1055 int qla4xxx_reset_target(struct scsi_qla_host *ha,
1056                          struct ddb_entry *ddb_entry)
1057 {
1058         uint32_t mbox_cmd[MBOX_REG_COUNT];
1059         uint32_t mbox_sts[MBOX_REG_COUNT];
1060         int status = QLA_SUCCESS;
1061
1062         DEBUG2(printk("scsi%ld:%d: target reset issued\n", ha->host_no,
1063                       ddb_entry->fw_ddb_index));
1064
1065         /*
1066          * Send target reset command to ISP, so that the ISP will return all
1067          * outstanding requests with RESET status
1068          */
1069         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1070         memset(&mbox_sts, 0, sizeof(mbox_sts));
1071
1072         mbox_cmd[0] = MBOX_CMD_TARGET_WARM_RESET;
1073         mbox_cmd[1] = ddb_entry->fw_ddb_index;
1074         mbox_cmd[5] = 0x01;     /* Immediate Command Enable */
1075
1076         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
1077                                 &mbox_sts[0]);
1078         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE &&
1079             mbox_sts[0] != MBOX_STS_COMMAND_ERROR)
1080                 status = QLA_ERROR;
1081
1082         return status;
1083 }
1084
1085 int qla4xxx_get_flash(struct scsi_qla_host * ha, dma_addr_t dma_addr,
1086                       uint32_t offset, uint32_t len)
1087 {
1088         uint32_t mbox_cmd[MBOX_REG_COUNT];
1089         uint32_t mbox_sts[MBOX_REG_COUNT];
1090
1091         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1092         memset(&mbox_sts, 0, sizeof(mbox_sts));
1093
1094         mbox_cmd[0] = MBOX_CMD_READ_FLASH;
1095         mbox_cmd[1] = LSDW(dma_addr);
1096         mbox_cmd[2] = MSDW(dma_addr);
1097         mbox_cmd[3] = offset;
1098         mbox_cmd[4] = len;
1099
1100         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 2, &mbox_cmd[0], &mbox_sts[0]) !=
1101             QLA_SUCCESS) {
1102                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_READ_FLASH, failed w/ "
1103                     "status %04X %04X, offset %08x, len %08x\n", ha->host_no,
1104                     __func__, mbox_sts[0], mbox_sts[1], offset, len));
1105                 return QLA_ERROR;
1106         }
1107         return QLA_SUCCESS;
1108 }
1109
1110 /**
1111  * qla4xxx_about_firmware - gets FW, iscsi draft and boot loader version
1112  * @ha: Pointer to host adapter structure.
1113  *
1114  * Retrieves the FW version, iSCSI draft version & bootloader version of HBA.
1115  * Mailboxes 2 & 3 may hold an address for data. Make sure that we write 0 to
1116  * those mailboxes, if unused.
1117  **/
1118 int qla4xxx_about_firmware(struct scsi_qla_host *ha)
1119 {
1120         struct about_fw_info *about_fw = NULL;
1121         dma_addr_t about_fw_dma;
1122         uint32_t mbox_cmd[MBOX_REG_COUNT];
1123         uint32_t mbox_sts[MBOX_REG_COUNT];
1124         int status = QLA_ERROR;
1125
1126         about_fw = dma_alloc_coherent(&ha->pdev->dev,
1127                                       sizeof(struct about_fw_info),
1128                                       &about_fw_dma, GFP_KERNEL);
1129         if (!about_fw) {
1130                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Unable to alloc memory "
1131                                   "for about_fw\n", __func__));
1132                 return status;
1133         }
1134
1135         memset(about_fw, 0, sizeof(struct about_fw_info));
1136         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1137         memset(&mbox_sts, 0, sizeof(mbox_sts));
1138
1139         mbox_cmd[0] = MBOX_CMD_ABOUT_FW;
1140         mbox_cmd[2] = LSDW(about_fw_dma);
1141         mbox_cmd[3] = MSDW(about_fw_dma);
1142         mbox_cmd[4] = sizeof(struct about_fw_info);
1143
1144         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, MBOX_REG_COUNT,
1145                                          &mbox_cmd[0], &mbox_sts[0]);
1146         if (status != QLA_SUCCESS) {
1147                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_ABOUT_FW "
1148                                   "failed w/ status %04X\n", __func__,
1149                                   mbox_sts[0]));
1150                 goto exit_about_fw;
1151         }
1152
1153         /* Save version information. */
1154         ha->firmware_version[0] = le16_to_cpu(about_fw->fw_major);
1155         ha->firmware_version[1] = le16_to_cpu(about_fw->fw_minor);
1156         ha->patch_number = le16_to_cpu(about_fw->fw_patch);
1157         ha->build_number = le16_to_cpu(about_fw->fw_build);
1158         ha->iscsi_major = le16_to_cpu(about_fw->iscsi_major);
1159         ha->iscsi_minor = le16_to_cpu(about_fw->iscsi_minor);
1160         ha->bootload_major = le16_to_cpu(about_fw->bootload_major);
1161         ha->bootload_minor = le16_to_cpu(about_fw->bootload_minor);
1162         ha->bootload_patch = le16_to_cpu(about_fw->bootload_patch);
1163         ha->bootload_build = le16_to_cpu(about_fw->bootload_build);
1164         status = QLA_SUCCESS;
1165
1166 exit_about_fw:
1167         dma_free_coherent(&ha->pdev->dev, sizeof(struct about_fw_info),
1168                           about_fw, about_fw_dma);
1169         return status;
1170 }
1171
1172 static int qla4xxx_get_default_ddb(struct scsi_qla_host *ha, uint32_t options,
1173                                    dma_addr_t dma_addr)
1174 {
1175         uint32_t mbox_cmd[MBOX_REG_COUNT];
1176         uint32_t mbox_sts[MBOX_REG_COUNT];
1177
1178         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1179         memset(&mbox_sts, 0, sizeof(mbox_sts));
1180
1181         mbox_cmd[0] = MBOX_CMD_GET_DATABASE_ENTRY_DEFAULTS;
1182         mbox_cmd[1] = options;
1183         mbox_cmd[2] = LSDW(dma_addr);
1184         mbox_cmd[3] = MSDW(dma_addr);
1185
1186         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0], &mbox_sts[0]) !=
1187             QLA_SUCCESS) {
1188                 DEBUG2(printk("scsi%ld: %s: failed status %04X\n",
1189                      ha->host_no, __func__, mbox_sts[0]));
1190                 return QLA_ERROR;
1191         }
1192         return QLA_SUCCESS;
1193 }
1194
1195 int qla4xxx_req_ddb_entry(struct scsi_qla_host *ha, uint32_t ddb_index,
1196                           uint32_t *mbx_sts)
1197 {
1198         int status;
1199         uint32_t mbox_cmd[MBOX_REG_COUNT];
1200         uint32_t mbox_sts[MBOX_REG_COUNT];
1201
1202         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1203         memset(&mbox_sts, 0, sizeof(mbox_sts));
1204
1205         mbox_cmd[0] = MBOX_CMD_REQUEST_DATABASE_ENTRY;
1206         mbox_cmd[1] = ddb_index;
1207
1208         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
1209                                          &mbox_sts[0]);
1210         if (status != QLA_SUCCESS) {
1211                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: failed status %04X\n",
1212                                    __func__, mbox_sts[0]));
1213         }
1214
1215         *mbx_sts = mbox_sts[0];
1216         return status;
1217 }
1218
1219 int qla4xxx_clear_ddb_entry(struct scsi_qla_host *ha, uint32_t ddb_index)
1220 {
1221         int status;
1222         uint32_t mbox_cmd[MBOX_REG_COUNT];
1223         uint32_t mbox_sts[MBOX_REG_COUNT];
1224
1225         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1226         memset(&mbox_sts, 0, sizeof(mbox_sts));
1227
1228         mbox_cmd[0] = MBOX_CMD_CLEAR_DATABASE_ENTRY;
1229         mbox_cmd[1] = ddb_index;
1230
1231         status = qla4xxx_mailbox_command(ha, 2, 1, &mbox_cmd[0],
1232                                          &mbox_sts[0]);
1233         if (status != QLA_SUCCESS) {
1234                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: failed status %04X\n",
1235                                    __func__, mbox_sts[0]));
1236         }
1237
1238         return status;
1239 }
1240
1241 int qla4xxx_set_flash(struct scsi_qla_host *ha, dma_addr_t dma_addr,
1242                       uint32_t offset, uint32_t length, uint32_t options)
1243 {
1244         uint32_t mbox_cmd[MBOX_REG_COUNT];
1245         uint32_t mbox_sts[MBOX_REG_COUNT];
1246         int status = QLA_SUCCESS;
1247
1248         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1249         memset(&mbox_sts, 0, sizeof(mbox_sts));
1250
1251         mbox_cmd[0] = MBOX_CMD_WRITE_FLASH;
1252         mbox_cmd[1] = LSDW(dma_addr);
1253         mbox_cmd[2] = MSDW(dma_addr);
1254         mbox_cmd[3] = offset;
1255         mbox_cmd[4] = length;
1256         mbox_cmd[5] = options;
1257
1258         status = qla4xxx_mailbox_command(ha, 6, 2, &mbox_cmd[0], &mbox_sts[0]);
1259         if (status != QLA_SUCCESS) {
1260                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_WRITE_FLASH "
1261                                   "failed w/ status %04X, mbx1 %04X\n",
1262                                   __func__, mbox_sts[0], mbox_sts[1]));
1263         }
1264         return status;
1265 }
1266
1267 int qla4xxx_bootdb_by_index(struct scsi_qla_host *ha,
1268                             struct dev_db_entry *fw_ddb_entry,
1269                             dma_addr_t fw_ddb_entry_dma, uint16_t ddb_index)
1270 {
1271         uint32_t dev_db_start_offset = FLASH_OFFSET_DB_INFO;
1272         uint32_t dev_db_end_offset;
1273         int status = QLA_ERROR;
1274
1275         memset(fw_ddb_entry, 0, sizeof(*fw_ddb_entry));
1276
1277         dev_db_start_offset += (ddb_index * sizeof(*fw_ddb_entry));
1278         dev_db_end_offset = FLASH_OFFSET_DB_END;
1279
1280         if (dev_db_start_offset > dev_db_end_offset) {
1281                 DEBUG2(ql4_printk(KERN_ERR, ha,
1282                                   "%s:Invalid DDB index %d", __func__,
1283                                   ddb_index));
1284                 goto exit_bootdb_failed;
1285         }
1286
1287         if (qla4xxx_get_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
1288                               sizeof(*fw_ddb_entry)) != QLA_SUCCESS) {
1289                 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
1290                            "failed\n", ha->host_no, __func__);
1291                 goto exit_bootdb_failed;
1292         }
1293
1294         if (fw_ddb_entry->cookie == DDB_VALID_COOKIE)
1295                 status = QLA_SUCCESS;
1296
1297 exit_bootdb_failed:
1298         return status;
1299 }
1300
1301 int qla4xxx_get_chap(struct scsi_qla_host *ha, char *username, char *password,
1302                      uint16_t idx)
1303 {
1304         int ret = 0;
1305         int rval = QLA_ERROR;
1306         uint32_t offset = 0;
1307         struct ql4_chap_table *chap_table;
1308         dma_addr_t chap_dma;
1309
1310         chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
1311         if (chap_table == NULL) {
1312                 ret = -ENOMEM;
1313                 goto exit_get_chap;
1314         }
1315
1316         memset(chap_table, 0, sizeof(struct ql4_chap_table));
1317
1318         offset = 0x06000000 | (idx * sizeof(struct ql4_chap_table));
1319
1320         rval = qla4xxx_get_flash(ha, chap_dma, offset,
1321                                  sizeof(struct ql4_chap_table));
1322         if (rval != QLA_SUCCESS) {
1323                 ret = -EINVAL;
1324                 goto exit_get_chap;
1325         }
1326
1327         DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
1328                 __le16_to_cpu(chap_table->cookie)));
1329
1330         if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
1331                 ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
1332                 goto exit_get_chap;
1333         }
1334
1335         strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
1336         strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
1337         chap_table->cookie = __constant_cpu_to_le16(CHAP_VALID_COOKIE);
1338
1339 exit_get_chap:
1340         dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
1341         return ret;
1342 }
1343
1344 static int qla4xxx_set_chap(struct scsi_qla_host *ha, char *username,
1345                             char *password, uint16_t idx, int bidi)
1346 {
1347         int ret = 0;
1348         int rval = QLA_ERROR;
1349         uint32_t offset = 0;
1350         struct ql4_chap_table *chap_table;
1351         dma_addr_t chap_dma;
1352
1353         chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
1354         if (chap_table == NULL) {
1355                 ret =  -ENOMEM;
1356                 goto exit_set_chap;
1357         }
1358
1359         memset(chap_table, 0, sizeof(struct ql4_chap_table));
1360         if (bidi)
1361                 chap_table->flags |= BIT_6; /* peer */
1362         else
1363                 chap_table->flags |= BIT_7; /* local */
1364         chap_table->secret_len = strlen(password);
1365         strncpy(chap_table->secret, password, MAX_CHAP_SECRET_LEN);
1366         strncpy(chap_table->name, username, MAX_CHAP_NAME_LEN);
1367         chap_table->cookie = __constant_cpu_to_le16(CHAP_VALID_COOKIE);
1368         offset = 0x06000000 | (idx * sizeof(struct ql4_chap_table));
1369         rval = qla4xxx_set_flash(ha, chap_dma, offset,
1370                                 sizeof(struct ql4_chap_table),
1371                                 FLASH_OPT_RMW_COMMIT);
1372         dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
1373         if (rval != QLA_SUCCESS)
1374                 ret =  -EINVAL;
1375
1376 exit_set_chap:
1377         return ret;
1378 }
1379
1380 int qla4xxx_conn_close_sess_logout(struct scsi_qla_host *ha,
1381                                    uint16_t fw_ddb_index,
1382                                    uint16_t connection_id,
1383                                    uint16_t option)
1384 {
1385         uint32_t mbox_cmd[MBOX_REG_COUNT];
1386         uint32_t mbox_sts[MBOX_REG_COUNT];
1387         int status = QLA_SUCCESS;
1388
1389         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1390         memset(&mbox_sts, 0, sizeof(mbox_sts));
1391
1392         mbox_cmd[0] = MBOX_CMD_CONN_CLOSE_SESS_LOGOUT;
1393         mbox_cmd[1] = fw_ddb_index;
1394         mbox_cmd[2] = connection_id;
1395         mbox_cmd[3] = option;
1396
1397         status = qla4xxx_mailbox_command(ha, 4, 2, &mbox_cmd[0], &mbox_sts[0]);
1398         if (status != QLA_SUCCESS) {
1399                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_CONN_CLOSE "
1400                                   "option %04x failed w/ status %04X %04X\n",
1401                                   __func__, option, mbox_sts[0], mbox_sts[1]));
1402         }
1403         return status;
1404 }
1405
1406 int qla4xxx_disable_acb(struct scsi_qla_host *ha)
1407 {
1408         uint32_t mbox_cmd[MBOX_REG_COUNT];
1409         uint32_t mbox_sts[MBOX_REG_COUNT];
1410         int status = QLA_SUCCESS;
1411
1412         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1413         memset(&mbox_sts, 0, sizeof(mbox_sts));
1414
1415         mbox_cmd[0] = MBOX_CMD_DISABLE_ACB;
1416
1417         status = qla4xxx_mailbox_command(ha, 8, 5, &mbox_cmd[0], &mbox_sts[0]);
1418         if (status != QLA_SUCCESS) {
1419                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_DISABLE_ACB "
1420                                   "failed w/ status %04X %04X %04X", __func__,
1421                                   mbox_sts[0], mbox_sts[1], mbox_sts[2]));
1422         }
1423         return status;
1424 }
1425
1426 int qla4xxx_get_acb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
1427                     uint32_t *mbox_sts, dma_addr_t acb_dma)
1428 {
1429         int status = QLA_SUCCESS;
1430
1431         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
1432         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
1433         mbox_cmd[0] = MBOX_CMD_GET_ACB;
1434         mbox_cmd[1] = 0; /* Primary ACB */
1435         mbox_cmd[2] = LSDW(acb_dma);
1436         mbox_cmd[3] = MSDW(acb_dma);
1437         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
1438
1439         status = qla4xxx_mailbox_command(ha, 5, 5, &mbox_cmd[0], &mbox_sts[0]);
1440         if (status != QLA_SUCCESS) {
1441                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_GET_ACB "
1442                                   "failed w/ status %04X\n", __func__,
1443                                   mbox_sts[0]));
1444         }
1445         return status;
1446 }
1447
1448 int qla4xxx_set_acb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
1449                     uint32_t *mbox_sts, dma_addr_t acb_dma)
1450 {
1451         int status = QLA_SUCCESS;
1452
1453         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
1454         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
1455         mbox_cmd[0] = MBOX_CMD_SET_ACB;
1456         mbox_cmd[1] = 0; /* Primary ACB */
1457         mbox_cmd[2] = LSDW(acb_dma);
1458         mbox_cmd[3] = MSDW(acb_dma);
1459         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
1460
1461         status = qla4xxx_mailbox_command(ha, 5, 5, &mbox_cmd[0], &mbox_sts[0]);
1462         if (status != QLA_SUCCESS) {
1463                 DEBUG2(ql4_printk(KERN_WARNING, ha,  "%s: MBOX_CMD_SET_ACB "
1464                                   "failed w/ status %04X\n", __func__,
1465                                   mbox_sts[0]));
1466         }
1467         return status;
1468 }
1469
1470 int qla4xxx_set_param_ddbentry(struct scsi_qla_host *ha,
1471                                struct ddb_entry *ddb_entry,
1472                                struct iscsi_cls_conn *cls_conn,
1473                                uint32_t *mbx_sts)
1474 {
1475         struct dev_db_entry *fw_ddb_entry;
1476         struct iscsi_conn *conn;
1477         struct iscsi_session *sess;
1478         struct qla_conn *qla_conn;
1479         struct sockaddr *dst_addr;
1480         dma_addr_t fw_ddb_entry_dma;
1481         int status = QLA_SUCCESS;
1482         int rval = 0;
1483         struct sockaddr_in *addr;
1484         struct sockaddr_in6 *addr6;
1485         char *ip;
1486         uint16_t iscsi_opts = 0;
1487         uint32_t options = 0;
1488         uint16_t idx;
1489         int max_chap_entries = 0;
1490
1491         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1492                                           &fw_ddb_entry_dma, GFP_KERNEL);
1493         if (!fw_ddb_entry) {
1494                 DEBUG2(ql4_printk(KERN_ERR, ha,
1495                                   "%s: Unable to allocate dma buffer.\n",
1496                                   __func__));
1497                 rval = -ENOMEM;
1498                 goto exit_set_param_no_free;
1499         }
1500
1501         conn = cls_conn->dd_data;
1502         qla_conn = conn->dd_data;
1503         sess = conn->session;
1504         dst_addr = &qla_conn->qla_ep->dst_addr;
1505
1506         if (dst_addr->sa_family == AF_INET6)
1507                 options |= IPV6_DEFAULT_DDB_ENTRY;
1508
1509         status = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
1510         if (status == QLA_ERROR) {
1511                 rval = -EINVAL;
1512                 goto exit_set_param;
1513         }
1514
1515         iscsi_opts = le16_to_cpu(fw_ddb_entry->iscsi_options);
1516         memset(fw_ddb_entry->iscsi_alias, 0, sizeof(fw_ddb_entry->iscsi_alias));
1517
1518         memset(fw_ddb_entry->iscsi_name, 0, sizeof(fw_ddb_entry->iscsi_name));
1519
1520         if (sess->targetname != NULL) {
1521                 memcpy(fw_ddb_entry->iscsi_name, sess->targetname,
1522                        min(strlen(sess->targetname),
1523                        sizeof(fw_ddb_entry->iscsi_name)));
1524         }
1525
1526         memset(fw_ddb_entry->ip_addr, 0, sizeof(fw_ddb_entry->ip_addr));
1527         memset(fw_ddb_entry->tgt_addr, 0, sizeof(fw_ddb_entry->tgt_addr));
1528
1529         fw_ddb_entry->options =  DDB_OPT_TARGET | DDB_OPT_AUTO_SENDTGTS_DISABLE;
1530
1531         if (dst_addr->sa_family == AF_INET) {
1532                 addr = (struct sockaddr_in *)dst_addr;
1533                 ip = (char *)&addr->sin_addr;
1534                 memcpy(fw_ddb_entry->ip_addr, ip, IP_ADDR_LEN);
1535                 fw_ddb_entry->port = cpu_to_le16(ntohs(addr->sin_port));
1536                 DEBUG2(ql4_printk(KERN_INFO, ha,
1537                                   "%s: Destination Address [%pI4]: index [%d]\n",
1538                                    __func__, fw_ddb_entry->ip_addr,
1539                                   ddb_entry->fw_ddb_index));
1540         } else if (dst_addr->sa_family == AF_INET6) {
1541                 addr6 = (struct sockaddr_in6 *)dst_addr;
1542                 ip = (char *)&addr6->sin6_addr;
1543                 memcpy(fw_ddb_entry->ip_addr, ip, IPv6_ADDR_LEN);
1544                 fw_ddb_entry->port = cpu_to_le16(ntohs(addr6->sin6_port));
1545                 fw_ddb_entry->options |= DDB_OPT_IPV6_DEVICE;
1546                 DEBUG2(ql4_printk(KERN_INFO, ha,
1547                                   "%s: Destination Address [%pI6]: index [%d]\n",
1548                                    __func__, fw_ddb_entry->ip_addr,
1549                                   ddb_entry->fw_ddb_index));
1550         } else {
1551                 ql4_printk(KERN_ERR, ha,
1552                            "%s: Failed to get IP Address\n",
1553                            __func__);
1554                 rval = -EINVAL;
1555                 goto exit_set_param;
1556         }
1557
1558         if (is_qla8022(ha))
1559                 max_chap_entries = MAX_CHAP_ENTRIES_82XX;
1560         else
1561                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
1562         /* CHAP */
1563         if (sess->username != NULL && sess->password != NULL) {
1564                 if (strlen(sess->username) && strlen(sess->password)) {
1565                         iscsi_opts |= BIT_7;
1566                         idx = ddb_entry->fw_ddb_index * 2;
1567                         if (idx > max_chap_entries) {
1568                                 ql4_printk(KERN_ERR, ha,
1569                                            "%s: Invalid ddb or chap index\n",
1570                                            __func__);
1571                                 rval  = -EINVAL;
1572                                 goto exit_set_param;
1573                         }
1574
1575                         rval = qla4xxx_set_chap(ha, sess->username,
1576                                                 sess->password, idx, 0);
1577                         if (rval)
1578                                 goto exit_set_param;
1579
1580                         fw_ddb_entry->chap_tbl_idx = cpu_to_le16(idx);
1581                 }
1582         }
1583
1584         if (sess->username_in != NULL && sess->password_in != NULL) {
1585                 /* Check if BIDI CHAP */
1586                 if (strlen(sess->username_in) && strlen(sess->password_in)) {
1587                         iscsi_opts |= BIT_4;
1588                         idx = (ddb_entry->fw_ddb_index * 2) + 1;
1589                         if (idx > max_chap_entries) {
1590                                 ql4_printk(KERN_ERR, ha,
1591                                            "%s: Invalid ddb or bidi chap "
1592                                            "index\n", __func__);
1593                                 rval  = -EINVAL;
1594                                 goto exit_set_param;
1595                         }
1596                         rval = qla4xxx_set_chap(ha, sess->username_in,
1597                                                 sess->password_in, idx, 0);
1598                         if (rval)
1599                                 goto exit_set_param;
1600                 }
1601         }
1602
1603         if (sess->initial_r2t_en)
1604                 iscsi_opts |= BIT_10;
1605
1606         if (sess->imm_data_en)
1607                 iscsi_opts |= BIT_11;
1608
1609         fw_ddb_entry->iscsi_options = cpu_to_le16(iscsi_opts);
1610
1611         if (conn->max_recv_dlength)
1612                 fw_ddb_entry->iscsi_max_rcv_data_seg_len =
1613                   __constant_cpu_to_le16((conn->max_recv_dlength / BYTE_UNITS));
1614
1615         if (sess->max_r2t)
1616                 fw_ddb_entry->iscsi_max_outsnd_r2t = cpu_to_le16(sess->max_r2t);
1617
1618         if (sess->first_burst)
1619                 fw_ddb_entry->iscsi_first_burst_len =
1620                        __constant_cpu_to_le16((sess->first_burst / BYTE_UNITS));
1621
1622         if (sess->max_burst)
1623                 fw_ddb_entry->iscsi_max_burst_len =
1624                         __constant_cpu_to_le16((sess->max_burst / BYTE_UNITS));
1625
1626         if (sess->time2wait)
1627                 fw_ddb_entry->iscsi_def_time2wait =
1628                         cpu_to_le16(sess->time2wait);
1629
1630         if (sess->time2retain)
1631                 fw_ddb_entry->iscsi_def_time2retain =
1632                         cpu_to_le16(sess->time2retain);
1633
1634         status = qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index,
1635                                        fw_ddb_entry_dma, mbx_sts);
1636
1637         if (status != QLA_SUCCESS)
1638                 rval = -EINVAL;
1639 exit_set_param:
1640         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1641                           fw_ddb_entry, fw_ddb_entry_dma);
1642 exit_set_param_no_free:
1643         return rval;
1644 }
1645
1646 int qla4xxx_get_mgmt_data(struct scsi_qla_host *ha, uint16_t fw_ddb_index,
1647                           uint16_t stats_size, dma_addr_t stats_dma)
1648 {
1649         int status = QLA_SUCCESS;
1650         uint32_t mbox_cmd[MBOX_REG_COUNT];
1651         uint32_t mbox_sts[MBOX_REG_COUNT];
1652
1653         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
1654         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
1655         mbox_cmd[0] = MBOX_CMD_GET_MANAGEMENT_DATA;
1656         mbox_cmd[1] = fw_ddb_index;
1657         mbox_cmd[2] = LSDW(stats_dma);
1658         mbox_cmd[3] = MSDW(stats_dma);
1659         mbox_cmd[4] = stats_size;
1660
1661         status = qla4xxx_mailbox_command(ha, 5, 1, &mbox_cmd[0], &mbox_sts[0]);
1662         if (status != QLA_SUCCESS) {
1663                 DEBUG2(ql4_printk(KERN_WARNING, ha,
1664                                   "%s: MBOX_CMD_GET_MANAGEMENT_DATA "
1665                                   "failed w/ status %04X\n", __func__,
1666                                   mbox_sts[0]));
1667         }
1668         return status;
1669 }
1670
1671 int qla4xxx_get_ip_state(struct scsi_qla_host *ha, uint32_t acb_idx,
1672                          uint32_t ip_idx, uint32_t *sts)
1673 {
1674         uint32_t mbox_cmd[MBOX_REG_COUNT];
1675         uint32_t mbox_sts[MBOX_REG_COUNT];
1676         int status = QLA_SUCCESS;
1677
1678         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1679         memset(&mbox_sts, 0, sizeof(mbox_sts));
1680         mbox_cmd[0] = MBOX_CMD_GET_IP_ADDR_STATE;
1681         mbox_cmd[1] = acb_idx;
1682         mbox_cmd[2] = ip_idx;
1683
1684         status = qla4xxx_mailbox_command(ha, 3, 8, &mbox_cmd[0], &mbox_sts[0]);
1685         if (status != QLA_SUCCESS) {
1686                 DEBUG2(ql4_printk(KERN_WARNING, ha,  "%s: "
1687                                   "MBOX_CMD_GET_IP_ADDR_STATE failed w/ "
1688                                   "status %04X\n", __func__, mbox_sts[0]));
1689         }
1690         memcpy(sts, mbox_sts, sizeof(mbox_sts));
1691         return status;
1692 }
1693
1694 int qla4xxx_get_nvram(struct scsi_qla_host *ha, dma_addr_t nvram_dma,
1695                       uint32_t offset, uint32_t size)
1696 {
1697         int status = QLA_SUCCESS;
1698         uint32_t mbox_cmd[MBOX_REG_COUNT];
1699         uint32_t mbox_sts[MBOX_REG_COUNT];
1700
1701         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1702         memset(&mbox_sts, 0, sizeof(mbox_sts));
1703
1704         mbox_cmd[0] = MBOX_CMD_GET_NVRAM;
1705         mbox_cmd[1] = LSDW(nvram_dma);
1706         mbox_cmd[2] = MSDW(nvram_dma);
1707         mbox_cmd[3] = offset;
1708         mbox_cmd[4] = size;
1709
1710         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
1711                                          &mbox_sts[0]);
1712         if (status != QLA_SUCCESS) {
1713                 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1714                                   "status %04X\n", ha->host_no, __func__,
1715                                   mbox_sts[0]));
1716         }
1717         return status;
1718 }
1719
1720 int qla4xxx_set_nvram(struct scsi_qla_host *ha, dma_addr_t nvram_dma,
1721                       uint32_t offset, uint32_t size)
1722 {
1723         int status = QLA_SUCCESS;
1724         uint32_t mbox_cmd[MBOX_REG_COUNT];
1725         uint32_t mbox_sts[MBOX_REG_COUNT];
1726
1727         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1728         memset(&mbox_sts, 0, sizeof(mbox_sts));
1729
1730         mbox_cmd[0] = MBOX_CMD_SET_NVRAM;
1731         mbox_cmd[1] = LSDW(nvram_dma);
1732         mbox_cmd[2] = MSDW(nvram_dma);
1733         mbox_cmd[3] = offset;
1734         mbox_cmd[4] = size;
1735
1736         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
1737                                          &mbox_sts[0]);
1738         if (status != QLA_SUCCESS) {
1739                 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
1740                                   "status %04X\n", ha->host_no, __func__,
1741                                   mbox_sts[0]));
1742         }
1743         return status;
1744 }