[SCSI] qla4xxx: Boot from SAN support for open-iscsi
[linux-2.6.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
337         if (ha->acb_version == ACB_SUPPORTED) {
338                 ha->ip_config.ipv6_options = le16_to_cpu(init_fw_cb->ipv6_opts);
339                 ha->ip_config.ipv6_addl_options =
340                                 le16_to_cpu(init_fw_cb->ipv6_addtl_opts);
341         }
342
343         /* Save IPv4 Address Info */
344         memcpy(ha->ip_config.ip_address, init_fw_cb->ipv4_addr,
345                min(sizeof(ha->ip_config.ip_address),
346                    sizeof(init_fw_cb->ipv4_addr)));
347         memcpy(ha->ip_config.subnet_mask, init_fw_cb->ipv4_subnet,
348                min(sizeof(ha->ip_config.subnet_mask),
349                    sizeof(init_fw_cb->ipv4_subnet)));
350         memcpy(ha->ip_config.gateway, init_fw_cb->ipv4_gw_addr,
351                min(sizeof(ha->ip_config.gateway),
352                    sizeof(init_fw_cb->ipv4_gw_addr)));
353
354         ha->ip_config.ipv4_vlan_tag = be16_to_cpu(init_fw_cb->ipv4_vlan_tag);
355
356         if (is_ipv6_enabled(ha)) {
357                 /* Save IPv6 Address */
358                 ha->ip_config.ipv6_link_local_state =
359                         le16_to_cpu(init_fw_cb->ipv6_lnk_lcl_addr_state);
360                 ha->ip_config.ipv6_addr0_state =
361                                 le16_to_cpu(init_fw_cb->ipv6_addr0_state);
362                 ha->ip_config.ipv6_addr1_state =
363                                 le16_to_cpu(init_fw_cb->ipv6_addr1_state);
364                 ha->ip_config.ipv6_default_router_state =
365                                 le16_to_cpu(init_fw_cb->ipv6_dflt_rtr_state);
366                 ha->ip_config.ipv6_link_local_addr.in6_u.u6_addr8[0] = 0xFE;
367                 ha->ip_config.ipv6_link_local_addr.in6_u.u6_addr8[1] = 0x80;
368
369                 memcpy(&ha->ip_config.ipv6_link_local_addr.in6_u.u6_addr8[8],
370                        init_fw_cb->ipv6_if_id,
371                        min(sizeof(ha->ip_config.ipv6_link_local_addr)/2,
372                            sizeof(init_fw_cb->ipv6_if_id)));
373                 memcpy(&ha->ip_config.ipv6_addr0, init_fw_cb->ipv6_addr0,
374                        min(sizeof(ha->ip_config.ipv6_addr0),
375                            sizeof(init_fw_cb->ipv6_addr0)));
376                 memcpy(&ha->ip_config.ipv6_addr1, init_fw_cb->ipv6_addr1,
377                        min(sizeof(ha->ip_config.ipv6_addr1),
378                            sizeof(init_fw_cb->ipv6_addr1)));
379                 memcpy(&ha->ip_config.ipv6_default_router_addr,
380                        init_fw_cb->ipv6_dflt_rtr_addr,
381                        min(sizeof(ha->ip_config.ipv6_default_router_addr),
382                            sizeof(init_fw_cb->ipv6_dflt_rtr_addr)));
383                 ha->ip_config.ipv6_vlan_tag =
384                                 be16_to_cpu(init_fw_cb->ipv6_vlan_tag);
385         }
386 }
387
388 uint8_t
389 qla4xxx_update_local_ifcb(struct scsi_qla_host *ha,
390                           uint32_t *mbox_cmd,
391                           uint32_t *mbox_sts,
392                           struct addr_ctrl_blk  *init_fw_cb,
393                           dma_addr_t init_fw_cb_dma)
394 {
395         if (qla4xxx_get_ifcb(ha, mbox_cmd, mbox_sts, init_fw_cb_dma)
396             != QLA_SUCCESS) {
397                 DEBUG2(printk(KERN_WARNING
398                               "scsi%ld: %s: Failed to get init_fw_ctrl_blk\n",
399                               ha->host_no, __func__));
400                 return QLA_ERROR;
401         }
402
403         DEBUG2(qla4xxx_dump_buffer(init_fw_cb, sizeof(struct addr_ctrl_blk)));
404
405         /* Save some info in adapter structure. */
406         ha->acb_version = init_fw_cb->acb_version;
407         ha->firmware_options = le16_to_cpu(init_fw_cb->fw_options);
408         ha->heartbeat_interval = init_fw_cb->hb_interval;
409         memcpy(ha->name_string, init_fw_cb->iscsi_name,
410                 min(sizeof(ha->name_string),
411                 sizeof(init_fw_cb->iscsi_name)));
412         /*memcpy(ha->alias, init_fw_cb->Alias,
413                min(sizeof(ha->alias), sizeof(init_fw_cb->Alias)));*/
414
415         qla4xxx_update_local_ip(ha, init_fw_cb);
416
417         return QLA_SUCCESS;
418 }
419
420 /**
421  * qla4xxx_initialize_fw_cb - initializes firmware control block.
422  * @ha: Pointer to host adapter structure.
423  **/
424 int qla4xxx_initialize_fw_cb(struct scsi_qla_host * ha)
425 {
426         struct addr_ctrl_blk *init_fw_cb;
427         dma_addr_t init_fw_cb_dma;
428         uint32_t mbox_cmd[MBOX_REG_COUNT];
429         uint32_t mbox_sts[MBOX_REG_COUNT];
430         int status = QLA_ERROR;
431
432         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
433                                         sizeof(struct addr_ctrl_blk),
434                                         &init_fw_cb_dma, GFP_KERNEL);
435         if (init_fw_cb == NULL) {
436                 DEBUG2(printk("scsi%ld: %s: Unable to alloc init_cb\n",
437                               ha->host_no, __func__));
438                 goto exit_init_fw_cb_no_free;
439         }
440         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
441
442         /* Get Initialize Firmware Control Block. */
443         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
444         memset(&mbox_sts, 0, sizeof(mbox_sts));
445
446         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma) !=
447             QLA_SUCCESS) {
448                 dma_free_coherent(&ha->pdev->dev,
449                                   sizeof(struct addr_ctrl_blk),
450                                   init_fw_cb, init_fw_cb_dma);
451                 goto exit_init_fw_cb;
452         }
453
454         /* Initialize request and response queues. */
455         qla4xxx_init_rings(ha);
456
457         /* Fill in the request and response queue information. */
458         init_fw_cb->rqq_consumer_idx = cpu_to_le16(ha->request_out);
459         init_fw_cb->compq_producer_idx = cpu_to_le16(ha->response_in);
460         init_fw_cb->rqq_len = __constant_cpu_to_le16(REQUEST_QUEUE_DEPTH);
461         init_fw_cb->compq_len = __constant_cpu_to_le16(RESPONSE_QUEUE_DEPTH);
462         init_fw_cb->rqq_addr_lo = cpu_to_le32(LSDW(ha->request_dma));
463         init_fw_cb->rqq_addr_hi = cpu_to_le32(MSDW(ha->request_dma));
464         init_fw_cb->compq_addr_lo = cpu_to_le32(LSDW(ha->response_dma));
465         init_fw_cb->compq_addr_hi = cpu_to_le32(MSDW(ha->response_dma));
466         init_fw_cb->shdwreg_addr_lo = cpu_to_le32(LSDW(ha->shadow_regs_dma));
467         init_fw_cb->shdwreg_addr_hi = cpu_to_le32(MSDW(ha->shadow_regs_dma));
468
469         /* Set up required options. */
470         init_fw_cb->fw_options |=
471                 __constant_cpu_to_le16(FWOPT_SESSION_MODE |
472                                        FWOPT_INITIATOR_MODE);
473
474         if (is_qla8022(ha))
475                 init_fw_cb->fw_options |=
476                     __constant_cpu_to_le16(FWOPT_ENABLE_CRBDB);
477
478         init_fw_cb->fw_options &= __constant_cpu_to_le16(~FWOPT_TARGET_MODE);
479
480         init_fw_cb->add_fw_options = 0;
481         init_fw_cb->add_fw_options |=
482                         __constant_cpu_to_le16(ADFWOPT_SERIALIZE_TASK_MGMT);
483         init_fw_cb->add_fw_options |=
484                         __constant_cpu_to_le16(ADFWOPT_AUTOCONN_DISABLE);
485
486         if (qla4xxx_set_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)
487                 != QLA_SUCCESS) {
488                 DEBUG2(printk(KERN_WARNING
489                               "scsi%ld: %s: Failed to set init_fw_ctrl_blk\n",
490                               ha->host_no, __func__));
491                 goto exit_init_fw_cb;
492         }
493
494         if (qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0],
495                 init_fw_cb, init_fw_cb_dma) != QLA_SUCCESS) {
496                 DEBUG2(printk("scsi%ld: %s: Failed to update local ifcb\n",
497                                 ha->host_no, __func__));
498                 goto exit_init_fw_cb;
499         }
500         status = QLA_SUCCESS;
501
502 exit_init_fw_cb:
503         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
504                                 init_fw_cb, init_fw_cb_dma);
505 exit_init_fw_cb_no_free:
506         return status;
507 }
508
509 /**
510  * qla4xxx_get_dhcp_ip_address - gets HBA ip address via DHCP
511  * @ha: Pointer to host adapter structure.
512  **/
513 int qla4xxx_get_dhcp_ip_address(struct scsi_qla_host * ha)
514 {
515         struct addr_ctrl_blk *init_fw_cb;
516         dma_addr_t init_fw_cb_dma;
517         uint32_t mbox_cmd[MBOX_REG_COUNT];
518         uint32_t mbox_sts[MBOX_REG_COUNT];
519
520         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
521                                         sizeof(struct addr_ctrl_blk),
522                                         &init_fw_cb_dma, GFP_KERNEL);
523         if (init_fw_cb == NULL) {
524                 printk("scsi%ld: %s: Unable to alloc init_cb\n", ha->host_no,
525                        __func__);
526                 return QLA_ERROR;
527         }
528
529         /* Get Initialize Firmware Control Block. */
530         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
531         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma) !=
532             QLA_SUCCESS) {
533                 DEBUG2(printk("scsi%ld: %s: Failed to get init_fw_ctrl_blk\n",
534                               ha->host_no, __func__));
535                 dma_free_coherent(&ha->pdev->dev,
536                                   sizeof(struct addr_ctrl_blk),
537                                   init_fw_cb, init_fw_cb_dma);
538                 return QLA_ERROR;
539         }
540
541         /* Save IP Address. */
542         qla4xxx_update_local_ip(ha, init_fw_cb);
543         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
544                                 init_fw_cb, init_fw_cb_dma);
545
546         return QLA_SUCCESS;
547 }
548
549 /**
550  * qla4xxx_get_firmware_state - gets firmware state of HBA
551  * @ha: Pointer to host adapter structure.
552  **/
553 int qla4xxx_get_firmware_state(struct scsi_qla_host * ha)
554 {
555         uint32_t mbox_cmd[MBOX_REG_COUNT];
556         uint32_t mbox_sts[MBOX_REG_COUNT];
557
558         /* Get firmware version */
559         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
560         memset(&mbox_sts, 0, sizeof(mbox_sts));
561
562         mbox_cmd[0] = MBOX_CMD_GET_FW_STATE;
563
564         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 4, &mbox_cmd[0], &mbox_sts[0]) !=
565             QLA_SUCCESS) {
566                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_GET_FW_STATE failed w/ "
567                               "status %04X\n", ha->host_no, __func__,
568                               mbox_sts[0]));
569                 return QLA_ERROR;
570         }
571         ha->firmware_state = mbox_sts[1];
572         ha->board_id = mbox_sts[2];
573         ha->addl_fw_state = mbox_sts[3];
574         DEBUG2(printk("scsi%ld: %s firmware_state=0x%x\n",
575                       ha->host_no, __func__, ha->firmware_state);)
576
577         return QLA_SUCCESS;
578 }
579
580 /**
581  * qla4xxx_get_firmware_status - retrieves firmware status
582  * @ha: Pointer to host adapter structure.
583  **/
584 int qla4xxx_get_firmware_status(struct scsi_qla_host * ha)
585 {
586         uint32_t mbox_cmd[MBOX_REG_COUNT];
587         uint32_t mbox_sts[MBOX_REG_COUNT];
588
589         /* Get firmware version */
590         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
591         memset(&mbox_sts, 0, sizeof(mbox_sts));
592
593         mbox_cmd[0] = MBOX_CMD_GET_FW_STATUS;
594
595         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 3, &mbox_cmd[0], &mbox_sts[0]) !=
596             QLA_SUCCESS) {
597                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_GET_FW_STATUS failed w/ "
598                               "status %04X\n", ha->host_no, __func__,
599                               mbox_sts[0]));
600                 return QLA_ERROR;
601         }
602
603         ql4_printk(KERN_INFO, ha, "%ld firmare IOCBs available (%d).\n",
604             ha->host_no, mbox_sts[2]);
605
606         return QLA_SUCCESS;
607 }
608
609 /**
610  * qla4xxx_get_fwddb_entry - retrieves firmware ddb entry
611  * @ha: Pointer to host adapter structure.
612  * @fw_ddb_index: Firmware's device database index
613  * @fw_ddb_entry: Pointer to firmware's device database entry structure
614  * @num_valid_ddb_entries: Pointer to number of valid ddb entries
615  * @next_ddb_index: Pointer to next valid device database index
616  * @fw_ddb_device_state: Pointer to device state
617  **/
618 int qla4xxx_get_fwddb_entry(struct scsi_qla_host *ha,
619                             uint16_t fw_ddb_index,
620                             struct dev_db_entry *fw_ddb_entry,
621                             dma_addr_t fw_ddb_entry_dma,
622                             uint32_t *num_valid_ddb_entries,
623                             uint32_t *next_ddb_index,
624                             uint32_t *fw_ddb_device_state,
625                             uint32_t *conn_err_detail,
626                             uint16_t *tcp_source_port_num,
627                             uint16_t *connection_id)
628 {
629         int status = QLA_ERROR;
630         uint16_t options;
631         uint32_t mbox_cmd[MBOX_REG_COUNT];
632         uint32_t mbox_sts[MBOX_REG_COUNT];
633
634         /* Make sure the device index is valid */
635         if (fw_ddb_index >= MAX_DDB_ENTRIES) {
636                 DEBUG2(printk("scsi%ld: %s: ddb [%d] out of range.\n",
637                               ha->host_no, __func__, fw_ddb_index));
638                 goto exit_get_fwddb;
639         }
640         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
641         memset(&mbox_sts, 0, sizeof(mbox_sts));
642
643         mbox_cmd[0] = MBOX_CMD_GET_DATABASE_ENTRY;
644         mbox_cmd[1] = (uint32_t) fw_ddb_index;
645         mbox_cmd[2] = LSDW(fw_ddb_entry_dma);
646         mbox_cmd[3] = MSDW(fw_ddb_entry_dma);
647         mbox_cmd[4] = sizeof(struct dev_db_entry);
648
649         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 7, &mbox_cmd[0], &mbox_sts[0]) ==
650             QLA_ERROR) {
651                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_GET_DATABASE_ENTRY failed"
652                               " with status 0x%04X\n", ha->host_no, __func__,
653                               mbox_sts[0]));
654                 goto exit_get_fwddb;
655         }
656         if (fw_ddb_index != mbox_sts[1]) {
657                 DEBUG2(printk("scsi%ld: %s: ddb mismatch [%d] != [%d].\n",
658                               ha->host_no, __func__, fw_ddb_index,
659                               mbox_sts[1]));
660                 goto exit_get_fwddb;
661         }
662         if (fw_ddb_entry) {
663                 options = le16_to_cpu(fw_ddb_entry->options);
664                 if (options & DDB_OPT_IPV6_DEVICE) {
665                         ql4_printk(KERN_INFO, ha, "%s: DDB[%d] MB0 %04x Tot %d "
666                                 "Next %d State %04x ConnErr %08x %pI6 "
667                                 ":%04d \"%s\"\n", __func__, fw_ddb_index,
668                                 mbox_sts[0], mbox_sts[2], mbox_sts[3],
669                                 mbox_sts[4], mbox_sts[5],
670                                 fw_ddb_entry->ip_addr,
671                                 le16_to_cpu(fw_ddb_entry->port),
672                                 fw_ddb_entry->iscsi_name);
673                 } else {
674                         ql4_printk(KERN_INFO, ha, "%s: DDB[%d] MB0 %04x Tot %d "
675                                 "Next %d State %04x ConnErr %08x %pI4 "
676                                 ":%04d \"%s\"\n", __func__, fw_ddb_index,
677                                 mbox_sts[0], mbox_sts[2], mbox_sts[3],
678                                 mbox_sts[4], mbox_sts[5],
679                                 fw_ddb_entry->ip_addr,
680                                 le16_to_cpu(fw_ddb_entry->port),
681                                 fw_ddb_entry->iscsi_name);
682                 }
683         }
684         if (num_valid_ddb_entries)
685                 *num_valid_ddb_entries = mbox_sts[2];
686         if (next_ddb_index)
687                 *next_ddb_index = mbox_sts[3];
688         if (fw_ddb_device_state)
689                 *fw_ddb_device_state = mbox_sts[4];
690
691         /*
692          * RA: This mailbox has been changed to pass connection error and
693          * details.  Its true for ISP4010 as per Version E - Not sure when it
694          * was changed.  Get the time2wait from the fw_dd_entry field :
695          * default_time2wait which we call it as minTime2Wait DEV_DB_ENTRY
696          * struct.
697          */
698         if (conn_err_detail)
699                 *conn_err_detail = mbox_sts[5];
700         if (tcp_source_port_num)
701                 *tcp_source_port_num = (uint16_t) (mbox_sts[6] >> 16);
702         if (connection_id)
703                 *connection_id = (uint16_t) mbox_sts[6] & 0x00FF;
704         status = QLA_SUCCESS;
705
706 exit_get_fwddb:
707         return status;
708 }
709
710 int qla4xxx_conn_open(struct scsi_qla_host *ha, uint16_t fw_ddb_index)
711 {
712         uint32_t mbox_cmd[MBOX_REG_COUNT];
713         uint32_t mbox_sts[MBOX_REG_COUNT];
714         int status;
715
716         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
717         memset(&mbox_sts, 0, sizeof(mbox_sts));
718
719         mbox_cmd[0] = MBOX_CMD_CONN_OPEN;
720         mbox_cmd[1] = fw_ddb_index;
721
722         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 2, &mbox_cmd[0],
723                                          &mbox_sts[0]);
724         DEBUG2(ql4_printk(KERN_INFO, ha,
725                           "%s: status = %d mbx0 = 0x%x mbx1 = 0x%x\n",
726                           __func__, status, mbox_sts[0], mbox_sts[1]));
727         return status;
728 }
729
730 /**
731  * qla4xxx_set_fwddb_entry - sets a ddb entry.
732  * @ha: Pointer to host adapter structure.
733  * @fw_ddb_index: Firmware's device database index
734  * @fw_ddb_entry_dma: dma address of ddb entry
735  * @mbx_sts: mailbox 0 to be returned or NULL
736  *
737  * This routine initializes or updates the adapter's device database
738  * entry for the specified device.
739  **/
740 int qla4xxx_set_ddb_entry(struct scsi_qla_host * ha, uint16_t fw_ddb_index,
741                           dma_addr_t fw_ddb_entry_dma, uint32_t *mbx_sts)
742 {
743         uint32_t mbox_cmd[MBOX_REG_COUNT];
744         uint32_t mbox_sts[MBOX_REG_COUNT];
745         int status;
746
747         /* Do not wait for completion. The firmware will send us an
748          * ASTS_DATABASE_CHANGED (0x8014) to notify us of the login status.
749          */
750         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
751         memset(&mbox_sts, 0, sizeof(mbox_sts));
752
753         mbox_cmd[0] = MBOX_CMD_SET_DATABASE_ENTRY;
754         mbox_cmd[1] = (uint32_t) fw_ddb_index;
755         mbox_cmd[2] = LSDW(fw_ddb_entry_dma);
756         mbox_cmd[3] = MSDW(fw_ddb_entry_dma);
757         mbox_cmd[4] = sizeof(struct dev_db_entry);
758
759         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0],
760                                          &mbox_sts[0]);
761         if (mbx_sts)
762                 *mbx_sts = mbox_sts[0];
763         DEBUG2(printk("scsi%ld: %s: status=%d mbx0=0x%x mbx4=0x%x\n",
764             ha->host_no, __func__, status, mbox_sts[0], mbox_sts[4]);)
765
766         return status;
767 }
768
769 int qla4xxx_session_logout_ddb(struct scsi_qla_host *ha,
770                                struct ddb_entry *ddb_entry, int options)
771 {
772         int status;
773         uint32_t mbox_cmd[MBOX_REG_COUNT];
774         uint32_t mbox_sts[MBOX_REG_COUNT];
775
776         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
777         memset(&mbox_sts, 0, sizeof(mbox_sts));
778
779         mbox_cmd[0] = MBOX_CMD_CONN_CLOSE_SESS_LOGOUT;
780         mbox_cmd[1] = ddb_entry->fw_ddb_index;
781         mbox_cmd[3] = options;
782
783         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 2, &mbox_cmd[0],
784                                          &mbox_sts[0]);
785         if (status != QLA_SUCCESS) {
786                 DEBUG2(ql4_printk(KERN_INFO, ha,
787                                   "%s: MBOX_CMD_CONN_CLOSE_SESS_LOGOUT "
788                                   "failed sts %04X %04X", __func__,
789                                   mbox_sts[0], mbox_sts[1]));
790         }
791
792         return status;
793 }
794
795 /**
796  * qla4xxx_get_crash_record - retrieves crash record.
797  * @ha: Pointer to host adapter structure.
798  *
799  * This routine retrieves a crash record from the QLA4010 after an 8002h aen.
800  **/
801 void qla4xxx_get_crash_record(struct scsi_qla_host * ha)
802 {
803         uint32_t mbox_cmd[MBOX_REG_COUNT];
804         uint32_t mbox_sts[MBOX_REG_COUNT];
805         struct crash_record *crash_record = NULL;
806         dma_addr_t crash_record_dma = 0;
807         uint32_t crash_record_size = 0;
808
809         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
810         memset(&mbox_sts, 0, sizeof(mbox_cmd));
811
812         /* Get size of crash record. */
813         mbox_cmd[0] = MBOX_CMD_GET_CRASH_RECORD;
814
815         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
816             QLA_SUCCESS) {
817                 DEBUG2(printk("scsi%ld: %s: ERROR: Unable to retrieve size!\n",
818                               ha->host_no, __func__));
819                 goto exit_get_crash_record;
820         }
821         crash_record_size = mbox_sts[4];
822         if (crash_record_size == 0) {
823                 DEBUG2(printk("scsi%ld: %s: ERROR: Crash record size is 0!\n",
824                               ha->host_no, __func__));
825                 goto exit_get_crash_record;
826         }
827
828         /* Alloc Memory for Crash Record. */
829         crash_record = dma_alloc_coherent(&ha->pdev->dev, crash_record_size,
830                                           &crash_record_dma, GFP_KERNEL);
831         if (crash_record == NULL)
832                 goto exit_get_crash_record;
833
834         /* Get Crash Record. */
835         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
836         memset(&mbox_sts, 0, sizeof(mbox_cmd));
837
838         mbox_cmd[0] = MBOX_CMD_GET_CRASH_RECORD;
839         mbox_cmd[2] = LSDW(crash_record_dma);
840         mbox_cmd[3] = MSDW(crash_record_dma);
841         mbox_cmd[4] = crash_record_size;
842
843         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
844             QLA_SUCCESS)
845                 goto exit_get_crash_record;
846
847         /* Dump Crash Record. */
848
849 exit_get_crash_record:
850         if (crash_record)
851                 dma_free_coherent(&ha->pdev->dev, crash_record_size,
852                                   crash_record, crash_record_dma);
853 }
854
855 /**
856  * qla4xxx_get_conn_event_log - retrieves connection event log
857  * @ha: Pointer to host adapter structure.
858  **/
859 void qla4xxx_get_conn_event_log(struct scsi_qla_host * ha)
860 {
861         uint32_t mbox_cmd[MBOX_REG_COUNT];
862         uint32_t mbox_sts[MBOX_REG_COUNT];
863         struct conn_event_log_entry *event_log = NULL;
864         dma_addr_t event_log_dma = 0;
865         uint32_t event_log_size = 0;
866         uint32_t num_valid_entries;
867         uint32_t      oldest_entry = 0;
868         uint32_t        max_event_log_entries;
869         uint8_t         i;
870
871
872         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
873         memset(&mbox_sts, 0, sizeof(mbox_cmd));
874
875         /* Get size of crash record. */
876         mbox_cmd[0] = MBOX_CMD_GET_CONN_EVENT_LOG;
877
878         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
879             QLA_SUCCESS)
880                 goto exit_get_event_log;
881
882         event_log_size = mbox_sts[4];
883         if (event_log_size == 0)
884                 goto exit_get_event_log;
885
886         /* Alloc Memory for Crash Record. */
887         event_log = dma_alloc_coherent(&ha->pdev->dev, event_log_size,
888                                        &event_log_dma, GFP_KERNEL);
889         if (event_log == NULL)
890                 goto exit_get_event_log;
891
892         /* Get Crash Record. */
893         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
894         memset(&mbox_sts, 0, sizeof(mbox_cmd));
895
896         mbox_cmd[0] = MBOX_CMD_GET_CONN_EVENT_LOG;
897         mbox_cmd[2] = LSDW(event_log_dma);
898         mbox_cmd[3] = MSDW(event_log_dma);
899
900         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
901             QLA_SUCCESS) {
902                 DEBUG2(printk("scsi%ld: %s: ERROR: Unable to retrieve event "
903                               "log!\n", ha->host_no, __func__));
904                 goto exit_get_event_log;
905         }
906
907         /* Dump Event Log. */
908         num_valid_entries = mbox_sts[1];
909
910         max_event_log_entries = event_log_size /
911                 sizeof(struct conn_event_log_entry);
912
913         if (num_valid_entries > max_event_log_entries)
914                 oldest_entry = num_valid_entries % max_event_log_entries;
915
916         DEBUG3(printk("scsi%ld: Connection Event Log Dump (%d entries):\n",
917                       ha->host_no, num_valid_entries));
918
919         if (ql4xextended_error_logging == 3) {
920                 if (oldest_entry == 0) {
921                         /* Circular Buffer has not wrapped around */
922                         for (i=0; i < num_valid_entries; i++) {
923                                 qla4xxx_dump_buffer((uint8_t *)event_log+
924                                                     (i*sizeof(*event_log)),
925                                                     sizeof(*event_log));
926                         }
927                 }
928                 else {
929                         /* Circular Buffer has wrapped around -
930                          * display accordingly*/
931                         for (i=oldest_entry; i < max_event_log_entries; i++) {
932                                 qla4xxx_dump_buffer((uint8_t *)event_log+
933                                                     (i*sizeof(*event_log)),
934                                                     sizeof(*event_log));
935                         }
936                         for (i=0; i < oldest_entry; i++) {
937                                 qla4xxx_dump_buffer((uint8_t *)event_log+
938                                                     (i*sizeof(*event_log)),
939                                                     sizeof(*event_log));
940                         }
941                 }
942         }
943
944 exit_get_event_log:
945         if (event_log)
946                 dma_free_coherent(&ha->pdev->dev, event_log_size, event_log,
947                                   event_log_dma);
948 }
949
950 /**
951  * qla4xxx_abort_task - issues Abort Task
952  * @ha: Pointer to host adapter structure.
953  * @srb: Pointer to srb entry
954  *
955  * This routine performs a LUN RESET on the specified target/lun.
956  * The caller must ensure that the ddb_entry and lun_entry pointers
957  * are valid before calling this routine.
958  **/
959 int qla4xxx_abort_task(struct scsi_qla_host *ha, struct srb *srb)
960 {
961         uint32_t mbox_cmd[MBOX_REG_COUNT];
962         uint32_t mbox_sts[MBOX_REG_COUNT];
963         struct scsi_cmnd *cmd = srb->cmd;
964         int status = QLA_SUCCESS;
965         unsigned long flags = 0;
966         uint32_t index;
967
968         /*
969          * Send abort task command to ISP, so that the ISP will return
970          * request with ABORT status
971          */
972         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
973         memset(&mbox_sts, 0, sizeof(mbox_sts));
974
975         spin_lock_irqsave(&ha->hardware_lock, flags);
976         index = (unsigned long)(unsigned char *)cmd->host_scribble;
977         spin_unlock_irqrestore(&ha->hardware_lock, flags);
978
979         /* Firmware already posted completion on response queue */
980         if (index == MAX_SRBS)
981                 return status;
982
983         mbox_cmd[0] = MBOX_CMD_ABORT_TASK;
984         mbox_cmd[1] = srb->ddb->fw_ddb_index;
985         mbox_cmd[2] = index;
986         /* Immediate Command Enable */
987         mbox_cmd[5] = 0x01;
988
989         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0],
990             &mbox_sts[0]);
991         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE) {
992                 status = QLA_ERROR;
993
994                 DEBUG2(printk(KERN_WARNING "scsi%ld:%d:%d: abort task FAILED: "
995                     "mbx0=%04X, mb1=%04X, mb2=%04X, mb3=%04X, mb4=%04X\n",
996                     ha->host_no, cmd->device->id, cmd->device->lun, mbox_sts[0],
997                     mbox_sts[1], mbox_sts[2], mbox_sts[3], mbox_sts[4]));
998         }
999
1000         return status;
1001 }
1002
1003 /**
1004  * qla4xxx_reset_lun - issues LUN Reset
1005  * @ha: Pointer to host adapter structure.
1006  * @ddb_entry: Pointer to device database entry
1007  * @lun: lun number
1008  *
1009  * This routine performs a LUN RESET on the specified target/lun.
1010  * The caller must ensure that the ddb_entry and lun_entry pointers
1011  * are valid before calling this routine.
1012  **/
1013 int qla4xxx_reset_lun(struct scsi_qla_host * ha, struct ddb_entry * ddb_entry,
1014                       int lun)
1015 {
1016         uint32_t mbox_cmd[MBOX_REG_COUNT];
1017         uint32_t mbox_sts[MBOX_REG_COUNT];
1018         int status = QLA_SUCCESS;
1019
1020         DEBUG2(printk("scsi%ld:%d:%d: lun reset issued\n", ha->host_no,
1021                       ddb_entry->fw_ddb_index, lun));
1022
1023         /*
1024          * Send lun reset command to ISP, so that the ISP will return all
1025          * outstanding requests with RESET status
1026          */
1027         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1028         memset(&mbox_sts, 0, sizeof(mbox_sts));
1029
1030         mbox_cmd[0] = MBOX_CMD_LUN_RESET;
1031         mbox_cmd[1] = ddb_entry->fw_ddb_index;
1032         mbox_cmd[2] = lun << 8;
1033         mbox_cmd[5] = 0x01;     /* Immediate Command Enable */
1034
1035         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0], &mbox_sts[0]);
1036         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE &&
1037             mbox_sts[0] != MBOX_STS_COMMAND_ERROR)
1038                 status = QLA_ERROR;
1039
1040         return status;
1041 }
1042
1043 /**
1044  * qla4xxx_reset_target - issues target Reset
1045  * @ha: Pointer to host adapter structure.
1046  * @db_entry: Pointer to device database entry
1047  * @un_entry: Pointer to lun entry structure
1048  *
1049  * This routine performs a TARGET RESET on the specified target.
1050  * The caller must ensure that the ddb_entry pointers
1051  * are valid before calling this routine.
1052  **/
1053 int qla4xxx_reset_target(struct scsi_qla_host *ha,
1054                          struct ddb_entry *ddb_entry)
1055 {
1056         uint32_t mbox_cmd[MBOX_REG_COUNT];
1057         uint32_t mbox_sts[MBOX_REG_COUNT];
1058         int status = QLA_SUCCESS;
1059
1060         DEBUG2(printk("scsi%ld:%d: target reset issued\n", ha->host_no,
1061                       ddb_entry->fw_ddb_index));
1062
1063         /*
1064          * Send target reset command to ISP, so that the ISP will return all
1065          * outstanding requests with RESET status
1066          */
1067         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1068         memset(&mbox_sts, 0, sizeof(mbox_sts));
1069
1070         mbox_cmd[0] = MBOX_CMD_TARGET_WARM_RESET;
1071         mbox_cmd[1] = ddb_entry->fw_ddb_index;
1072         mbox_cmd[5] = 0x01;     /* Immediate Command Enable */
1073
1074         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
1075                                 &mbox_sts[0]);
1076         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE &&
1077             mbox_sts[0] != MBOX_STS_COMMAND_ERROR)
1078                 status = QLA_ERROR;
1079
1080         return status;
1081 }
1082
1083 int qla4xxx_get_flash(struct scsi_qla_host * ha, dma_addr_t dma_addr,
1084                       uint32_t offset, uint32_t len)
1085 {
1086         uint32_t mbox_cmd[MBOX_REG_COUNT];
1087         uint32_t mbox_sts[MBOX_REG_COUNT];
1088
1089         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1090         memset(&mbox_sts, 0, sizeof(mbox_sts));
1091
1092         mbox_cmd[0] = MBOX_CMD_READ_FLASH;
1093         mbox_cmd[1] = LSDW(dma_addr);
1094         mbox_cmd[2] = MSDW(dma_addr);
1095         mbox_cmd[3] = offset;
1096         mbox_cmd[4] = len;
1097
1098         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 2, &mbox_cmd[0], &mbox_sts[0]) !=
1099             QLA_SUCCESS) {
1100                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_READ_FLASH, failed w/ "
1101                     "status %04X %04X, offset %08x, len %08x\n", ha->host_no,
1102                     __func__, mbox_sts[0], mbox_sts[1], offset, len));
1103                 return QLA_ERROR;
1104         }
1105         return QLA_SUCCESS;
1106 }
1107
1108 /**
1109  * qla4xxx_about_firmware - gets FW, iscsi draft and boot loader version
1110  * @ha: Pointer to host adapter structure.
1111  *
1112  * Retrieves the FW version, iSCSI draft version & bootloader version of HBA.
1113  * Mailboxes 2 & 3 may hold an address for data. Make sure that we write 0 to
1114  * those mailboxes, if unused.
1115  **/
1116 int qla4xxx_about_firmware(struct scsi_qla_host *ha)
1117 {
1118         struct about_fw_info *about_fw = NULL;
1119         dma_addr_t about_fw_dma;
1120         uint32_t mbox_cmd[MBOX_REG_COUNT];
1121         uint32_t mbox_sts[MBOX_REG_COUNT];
1122         int status = QLA_ERROR;
1123
1124         about_fw = dma_alloc_coherent(&ha->pdev->dev,
1125                                       sizeof(struct about_fw_info),
1126                                       &about_fw_dma, GFP_KERNEL);
1127         if (!about_fw) {
1128                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Unable to alloc memory "
1129                                   "for about_fw\n", __func__));
1130                 return status;
1131         }
1132
1133         memset(about_fw, 0, sizeof(struct about_fw_info));
1134         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1135         memset(&mbox_sts, 0, sizeof(mbox_sts));
1136
1137         mbox_cmd[0] = MBOX_CMD_ABOUT_FW;
1138         mbox_cmd[2] = LSDW(about_fw_dma);
1139         mbox_cmd[3] = MSDW(about_fw_dma);
1140         mbox_cmd[4] = sizeof(struct about_fw_info);
1141
1142         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, MBOX_REG_COUNT,
1143                                          &mbox_cmd[0], &mbox_sts[0]);
1144         if (status != QLA_SUCCESS) {
1145                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_ABOUT_FW "
1146                                   "failed w/ status %04X\n", __func__,
1147                                   mbox_sts[0]));
1148                 goto exit_about_fw;
1149         }
1150
1151         /* Save version information. */
1152         ha->firmware_version[0] = le16_to_cpu(about_fw->fw_major);
1153         ha->firmware_version[1] = le16_to_cpu(about_fw->fw_minor);
1154         ha->patch_number = le16_to_cpu(about_fw->fw_patch);
1155         ha->build_number = le16_to_cpu(about_fw->fw_build);
1156         ha->iscsi_major = le16_to_cpu(about_fw->iscsi_major);
1157         ha->iscsi_minor = le16_to_cpu(about_fw->iscsi_minor);
1158         ha->bootload_major = le16_to_cpu(about_fw->bootload_major);
1159         ha->bootload_minor = le16_to_cpu(about_fw->bootload_minor);
1160         ha->bootload_patch = le16_to_cpu(about_fw->bootload_patch);
1161         ha->bootload_build = le16_to_cpu(about_fw->bootload_build);
1162         status = QLA_SUCCESS;
1163
1164 exit_about_fw:
1165         dma_free_coherent(&ha->pdev->dev, sizeof(struct about_fw_info),
1166                           about_fw, about_fw_dma);
1167         return status;
1168 }
1169
1170 static int qla4xxx_get_default_ddb(struct scsi_qla_host *ha, uint32_t options,
1171                                    dma_addr_t dma_addr)
1172 {
1173         uint32_t mbox_cmd[MBOX_REG_COUNT];
1174         uint32_t mbox_sts[MBOX_REG_COUNT];
1175
1176         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1177         memset(&mbox_sts, 0, sizeof(mbox_sts));
1178
1179         mbox_cmd[0] = MBOX_CMD_GET_DATABASE_ENTRY_DEFAULTS;
1180         mbox_cmd[1] = options;
1181         mbox_cmd[2] = LSDW(dma_addr);
1182         mbox_cmd[3] = MSDW(dma_addr);
1183
1184         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0], &mbox_sts[0]) !=
1185             QLA_SUCCESS) {
1186                 DEBUG2(printk("scsi%ld: %s: failed status %04X\n",
1187                      ha->host_no, __func__, mbox_sts[0]));
1188                 return QLA_ERROR;
1189         }
1190         return QLA_SUCCESS;
1191 }
1192
1193 int qla4xxx_req_ddb_entry(struct scsi_qla_host *ha, uint32_t ddb_index,
1194                           uint32_t *mbx_sts)
1195 {
1196         int status;
1197         uint32_t mbox_cmd[MBOX_REG_COUNT];
1198         uint32_t mbox_sts[MBOX_REG_COUNT];
1199
1200         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1201         memset(&mbox_sts, 0, sizeof(mbox_sts));
1202
1203         mbox_cmd[0] = MBOX_CMD_REQUEST_DATABASE_ENTRY;
1204         mbox_cmd[1] = ddb_index;
1205
1206         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
1207                                          &mbox_sts[0]);
1208         if (status != QLA_SUCCESS) {
1209                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: failed status %04X\n",
1210                                    __func__, mbox_sts[0]));
1211         }
1212
1213         *mbx_sts = mbox_sts[0];
1214         return status;
1215 }
1216
1217 int qla4xxx_clear_ddb_entry(struct scsi_qla_host *ha, uint32_t ddb_index)
1218 {
1219         int status;
1220         uint32_t mbox_cmd[MBOX_REG_COUNT];
1221         uint32_t mbox_sts[MBOX_REG_COUNT];
1222
1223         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1224         memset(&mbox_sts, 0, sizeof(mbox_sts));
1225
1226         mbox_cmd[0] = MBOX_CMD_CLEAR_DATABASE_ENTRY;
1227         mbox_cmd[1] = ddb_index;
1228
1229         status = qla4xxx_mailbox_command(ha, 2, 1, &mbox_cmd[0],
1230                                          &mbox_sts[0]);
1231         if (status != QLA_SUCCESS) {
1232                 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: failed status %04X\n",
1233                                    __func__, mbox_sts[0]));
1234         }
1235
1236         return status;
1237 }
1238
1239 int qla4xxx_set_flash(struct scsi_qla_host *ha, dma_addr_t dma_addr,
1240                       uint32_t offset, uint32_t length, uint32_t options)
1241 {
1242         uint32_t mbox_cmd[MBOX_REG_COUNT];
1243         uint32_t mbox_sts[MBOX_REG_COUNT];
1244         int status = QLA_SUCCESS;
1245
1246         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1247         memset(&mbox_sts, 0, sizeof(mbox_sts));
1248
1249         mbox_cmd[0] = MBOX_CMD_WRITE_FLASH;
1250         mbox_cmd[1] = LSDW(dma_addr);
1251         mbox_cmd[2] = MSDW(dma_addr);
1252         mbox_cmd[3] = offset;
1253         mbox_cmd[4] = length;
1254         mbox_cmd[5] = options;
1255
1256         status = qla4xxx_mailbox_command(ha, 6, 2, &mbox_cmd[0], &mbox_sts[0]);
1257         if (status != QLA_SUCCESS) {
1258                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_WRITE_FLASH "
1259                                   "failed w/ status %04X, mbx1 %04X\n",
1260                                   __func__, mbox_sts[0], mbox_sts[1]));
1261         }
1262         return status;
1263 }
1264
1265 int qla4xxx_bootdb_by_index(struct scsi_qla_host *ha,
1266                             struct dev_db_entry *fw_ddb_entry,
1267                             dma_addr_t fw_ddb_entry_dma, uint16_t ddb_index)
1268 {
1269         uint32_t dev_db_start_offset = FLASH_OFFSET_DB_INFO;
1270         uint32_t dev_db_end_offset;
1271         int status = QLA_ERROR;
1272
1273         memset(fw_ddb_entry, 0, sizeof(*fw_ddb_entry));
1274
1275         dev_db_start_offset += (ddb_index * sizeof(*fw_ddb_entry));
1276         dev_db_end_offset = FLASH_OFFSET_DB_END;
1277
1278         if (dev_db_start_offset > dev_db_end_offset) {
1279                 DEBUG2(ql4_printk(KERN_ERR, ha,
1280                                   "%s:Invalid DDB index %d", __func__,
1281                                   ddb_index));
1282                 goto exit_bootdb_failed;
1283         }
1284
1285         if (qla4xxx_get_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
1286                               sizeof(*fw_ddb_entry)) != QLA_SUCCESS) {
1287                 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
1288                            "failed\n", ha->host_no, __func__);
1289                 goto exit_bootdb_failed;
1290         }
1291
1292         if (fw_ddb_entry->cookie == DDB_VALID_COOKIE)
1293                 status = QLA_SUCCESS;
1294
1295 exit_bootdb_failed:
1296         return status;
1297 }
1298
1299 int qla4xxx_get_chap(struct scsi_qla_host *ha, char *username, char *password,
1300                      uint16_t idx)
1301 {
1302         int ret = 0;
1303         int rval = QLA_ERROR;
1304         uint32_t offset = 0;
1305         struct ql4_chap_table *chap_table;
1306         dma_addr_t chap_dma;
1307
1308         chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
1309         if (chap_table == NULL) {
1310                 ret = -ENOMEM;
1311                 goto exit_get_chap;
1312         }
1313
1314         memset(chap_table, 0, sizeof(struct ql4_chap_table));
1315
1316         offset = 0x06000000 | (idx * sizeof(struct ql4_chap_table));
1317
1318         rval = qla4xxx_get_flash(ha, chap_dma, offset,
1319                                  sizeof(struct ql4_chap_table));
1320         if (rval != QLA_SUCCESS) {
1321                 ret = -EINVAL;
1322                 goto exit_get_chap;
1323         }
1324
1325         DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
1326                 __le16_to_cpu(chap_table->cookie)));
1327
1328         if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
1329                 ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
1330                 goto exit_get_chap;
1331         }
1332
1333         strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
1334         strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
1335         chap_table->cookie = __constant_cpu_to_le16(CHAP_VALID_COOKIE);
1336
1337 exit_get_chap:
1338         dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
1339         return ret;
1340 }
1341
1342 static int qla4xxx_set_chap(struct scsi_qla_host *ha, char *username,
1343                             char *password, uint16_t idx, int bidi)
1344 {
1345         int ret = 0;
1346         int rval = QLA_ERROR;
1347         uint32_t offset = 0;
1348         struct ql4_chap_table *chap_table;
1349         dma_addr_t chap_dma;
1350
1351         chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
1352         if (chap_table == NULL) {
1353                 ret =  -ENOMEM;
1354                 goto exit_set_chap;
1355         }
1356
1357         memset(chap_table, 0, sizeof(struct ql4_chap_table));
1358         if (bidi)
1359                 chap_table->flags |= BIT_6; /* peer */
1360         else
1361                 chap_table->flags |= BIT_7; /* local */
1362         chap_table->secret_len = strlen(password);
1363         strncpy(chap_table->secret, password, MAX_CHAP_SECRET_LEN);
1364         strncpy(chap_table->name, username, MAX_CHAP_NAME_LEN);
1365         chap_table->cookie = __constant_cpu_to_le16(CHAP_VALID_COOKIE);
1366         offset = 0x06000000 | (idx * sizeof(struct ql4_chap_table));
1367         rval = qla4xxx_set_flash(ha, chap_dma, offset,
1368                                 sizeof(struct ql4_chap_table),
1369                                 FLASH_OPT_RMW_COMMIT);
1370         dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
1371         if (rval != QLA_SUCCESS)
1372                 ret =  -EINVAL;
1373
1374 exit_set_chap:
1375         return ret;
1376 }
1377
1378 int qla4xxx_conn_close_sess_logout(struct scsi_qla_host *ha,
1379                                    uint16_t fw_ddb_index,
1380                                    uint16_t connection_id,
1381                                    uint16_t option)
1382 {
1383         uint32_t mbox_cmd[MBOX_REG_COUNT];
1384         uint32_t mbox_sts[MBOX_REG_COUNT];
1385         int status = QLA_SUCCESS;
1386
1387         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1388         memset(&mbox_sts, 0, sizeof(mbox_sts));
1389
1390         mbox_cmd[0] = MBOX_CMD_CONN_CLOSE_SESS_LOGOUT;
1391         mbox_cmd[1] = fw_ddb_index;
1392         mbox_cmd[2] = connection_id;
1393         mbox_cmd[3] = option;
1394
1395         status = qla4xxx_mailbox_command(ha, 4, 2, &mbox_cmd[0], &mbox_sts[0]);
1396         if (status != QLA_SUCCESS) {
1397                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_CONN_CLOSE "
1398                                   "option %04x failed w/ status %04X %04X\n",
1399                                   __func__, option, mbox_sts[0], mbox_sts[1]));
1400         }
1401         return status;
1402 }
1403
1404 int qla4xxx_disable_acb(struct scsi_qla_host *ha)
1405 {
1406         uint32_t mbox_cmd[MBOX_REG_COUNT];
1407         uint32_t mbox_sts[MBOX_REG_COUNT];
1408         int status = QLA_SUCCESS;
1409
1410         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1411         memset(&mbox_sts, 0, sizeof(mbox_sts));
1412
1413         mbox_cmd[0] = MBOX_CMD_DISABLE_ACB;
1414
1415         status = qla4xxx_mailbox_command(ha, 8, 5, &mbox_cmd[0], &mbox_sts[0]);
1416         if (status != QLA_SUCCESS) {
1417                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_DISABLE_ACB "
1418                                   "failed w/ status %04X %04X %04X", __func__,
1419                                   mbox_sts[0], mbox_sts[1], mbox_sts[2]));
1420         }
1421         return status;
1422 }
1423
1424 int qla4xxx_get_acb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
1425                     uint32_t *mbox_sts, dma_addr_t acb_dma)
1426 {
1427         int status = QLA_SUCCESS;
1428
1429         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
1430         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
1431         mbox_cmd[0] = MBOX_CMD_GET_ACB;
1432         mbox_cmd[1] = 0; /* Primary ACB */
1433         mbox_cmd[2] = LSDW(acb_dma);
1434         mbox_cmd[3] = MSDW(acb_dma);
1435         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
1436
1437         status = qla4xxx_mailbox_command(ha, 5, 5, &mbox_cmd[0], &mbox_sts[0]);
1438         if (status != QLA_SUCCESS) {
1439                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: MBOX_CMD_GET_ACB "
1440                                   "failed w/ status %04X\n", __func__,
1441                                   mbox_sts[0]));
1442         }
1443         return status;
1444 }
1445
1446 int qla4xxx_set_acb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
1447                     uint32_t *mbox_sts, dma_addr_t acb_dma)
1448 {
1449         int status = QLA_SUCCESS;
1450
1451         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
1452         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
1453         mbox_cmd[0] = MBOX_CMD_SET_ACB;
1454         mbox_cmd[1] = 0; /* Primary ACB */
1455         mbox_cmd[2] = LSDW(acb_dma);
1456         mbox_cmd[3] = MSDW(acb_dma);
1457         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
1458
1459         status = qla4xxx_mailbox_command(ha, 5, 5, &mbox_cmd[0], &mbox_sts[0]);
1460         if (status != QLA_SUCCESS) {
1461                 DEBUG2(ql4_printk(KERN_WARNING, ha,  "%s: MBOX_CMD_SET_ACB "
1462                                   "failed w/ status %04X\n", __func__,
1463                                   mbox_sts[0]));
1464         }
1465         return status;
1466 }
1467
1468 int qla4xxx_set_param_ddbentry(struct scsi_qla_host *ha,
1469                                struct ddb_entry *ddb_entry,
1470                                struct iscsi_cls_conn *cls_conn,
1471                                uint32_t *mbx_sts)
1472 {
1473         struct dev_db_entry *fw_ddb_entry;
1474         struct iscsi_conn *conn;
1475         struct iscsi_session *sess;
1476         struct qla_conn *qla_conn;
1477         struct sockaddr *dst_addr;
1478         dma_addr_t fw_ddb_entry_dma;
1479         int status = QLA_SUCCESS;
1480         int rval = 0;
1481         struct sockaddr_in *addr;
1482         struct sockaddr_in6 *addr6;
1483         char *ip;
1484         uint16_t iscsi_opts = 0;
1485         uint32_t options = 0;
1486         uint16_t idx;
1487         int max_chap_entries = 0;
1488
1489         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1490                                           &fw_ddb_entry_dma, GFP_KERNEL);
1491         if (!fw_ddb_entry) {
1492                 DEBUG2(ql4_printk(KERN_ERR, ha,
1493                                   "%s: Unable to allocate dma buffer.\n",
1494                                   __func__));
1495                 rval = -ENOMEM;
1496                 goto exit_set_param_no_free;
1497         }
1498
1499         conn = cls_conn->dd_data;
1500         qla_conn = conn->dd_data;
1501         sess = conn->session;
1502         dst_addr = &qla_conn->qla_ep->dst_addr;
1503
1504         if (dst_addr->sa_family == AF_INET6)
1505                 options |= IPV6_DEFAULT_DDB_ENTRY;
1506
1507         status = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
1508         if (status == QLA_ERROR) {
1509                 rval = -EINVAL;
1510                 goto exit_set_param;
1511         }
1512
1513         iscsi_opts = le16_to_cpu(fw_ddb_entry->iscsi_options);
1514         memset(fw_ddb_entry->iscsi_alias, 0, sizeof(fw_ddb_entry->iscsi_alias));
1515
1516         memset(fw_ddb_entry->iscsi_name, 0, sizeof(fw_ddb_entry->iscsi_name));
1517
1518         if (sess->targetname != NULL) {
1519                 memcpy(fw_ddb_entry->iscsi_name, sess->targetname,
1520                        min(strlen(sess->targetname),
1521                        sizeof(fw_ddb_entry->iscsi_name)));
1522         }
1523
1524         memset(fw_ddb_entry->ip_addr, 0, sizeof(fw_ddb_entry->ip_addr));
1525         memset(fw_ddb_entry->tgt_addr, 0, sizeof(fw_ddb_entry->tgt_addr));
1526
1527         fw_ddb_entry->options =  DDB_OPT_TARGET | DDB_OPT_AUTO_SENDTGTS_DISABLE;
1528
1529         if (dst_addr->sa_family == AF_INET) {
1530                 addr = (struct sockaddr_in *)dst_addr;
1531                 ip = (char *)&addr->sin_addr;
1532                 memcpy(fw_ddb_entry->ip_addr, ip, IP_ADDR_LEN);
1533                 fw_ddb_entry->port = cpu_to_le16(ntohs(addr->sin_port));
1534                 DEBUG2(ql4_printk(KERN_INFO, ha,
1535                                   "%s: Destination Address [%pI4]: index [%d]\n",
1536                                    __func__, fw_ddb_entry->ip_addr,
1537                                   ddb_entry->fw_ddb_index));
1538         } else if (dst_addr->sa_family == AF_INET6) {
1539                 addr6 = (struct sockaddr_in6 *)dst_addr;
1540                 ip = (char *)&addr6->sin6_addr;
1541                 memcpy(fw_ddb_entry->ip_addr, ip, IPv6_ADDR_LEN);
1542                 fw_ddb_entry->port = cpu_to_le16(ntohs(addr6->sin6_port));
1543                 fw_ddb_entry->options |= DDB_OPT_IPV6_DEVICE;
1544                 DEBUG2(ql4_printk(KERN_INFO, ha,
1545                                   "%s: Destination Address [%pI6]: index [%d]\n",
1546                                    __func__, fw_ddb_entry->ip_addr,
1547                                   ddb_entry->fw_ddb_index));
1548         } else {
1549                 ql4_printk(KERN_ERR, ha,
1550                            "%s: Failed to get IP Address\n",
1551                            __func__);
1552                 rval = -EINVAL;
1553                 goto exit_set_param;
1554         }
1555
1556         if (is_qla8022(ha))
1557                 max_chap_entries = MAX_CHAP_ENTRIES_82XX;
1558         else
1559                 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
1560         /* CHAP */
1561         if (sess->username != NULL && sess->password != NULL) {
1562                 if (strlen(sess->username) && strlen(sess->password)) {
1563                         iscsi_opts |= BIT_7;
1564                         idx = ddb_entry->fw_ddb_index * 2;
1565                         if (idx > max_chap_entries) {
1566                                 ql4_printk(KERN_ERR, ha,
1567                                            "%s: Invalid ddb or chap index\n",
1568                                            __func__);
1569                                 rval  = -EINVAL;
1570                                 goto exit_set_param;
1571                         }
1572
1573                         rval = qla4xxx_set_chap(ha, sess->username,
1574                                                 sess->password, idx, 0);
1575                         if (rval)
1576                                 goto exit_set_param;
1577
1578                         fw_ddb_entry->chap_tbl_idx = cpu_to_le16(idx);
1579                 }
1580         }
1581
1582         if (sess->username_in != NULL && sess->password_in != NULL) {
1583                 /* Check if BIDI CHAP */
1584                 if (strlen(sess->username_in) && strlen(sess->password_in)) {
1585                         iscsi_opts |= BIT_4;
1586                         idx = (ddb_entry->fw_ddb_index * 2) + 1;
1587                         if (idx > max_chap_entries) {
1588                                 ql4_printk(KERN_ERR, ha,
1589                                            "%s: Invalid ddb or bidi chap "
1590                                            "index\n", __func__);
1591                                 rval  = -EINVAL;
1592                                 goto exit_set_param;
1593                         }
1594                         rval = qla4xxx_set_chap(ha, sess->username_in,
1595                                                 sess->password_in, idx, 0);
1596                         if (rval)
1597                                 goto exit_set_param;
1598                 }
1599         }
1600
1601         if (sess->initial_r2t_en)
1602                 iscsi_opts |= BIT_10;
1603
1604         if (sess->imm_data_en)
1605                 iscsi_opts |= BIT_11;
1606
1607         fw_ddb_entry->iscsi_options = cpu_to_le16(iscsi_opts);
1608
1609         if (conn->max_recv_dlength)
1610                 fw_ddb_entry->iscsi_max_rcv_data_seg_len =
1611                   __constant_cpu_to_le16((conn->max_recv_dlength / BYTE_UNITS));
1612
1613         if (sess->max_r2t)
1614                 fw_ddb_entry->iscsi_max_outsnd_r2t = cpu_to_le16(sess->max_r2t);
1615
1616         if (sess->first_burst)
1617                 fw_ddb_entry->iscsi_first_burst_len =
1618                        __constant_cpu_to_le16((sess->first_burst / BYTE_UNITS));
1619
1620         if (sess->max_burst)
1621                 fw_ddb_entry->iscsi_max_burst_len =
1622                         __constant_cpu_to_le16((sess->max_burst / BYTE_UNITS));
1623
1624         if (sess->time2wait)
1625                 fw_ddb_entry->iscsi_def_time2wait =
1626                         cpu_to_le16(sess->time2wait);
1627
1628         if (sess->time2retain)
1629                 fw_ddb_entry->iscsi_def_time2retain =
1630                         cpu_to_le16(sess->time2retain);
1631
1632         status = qla4xxx_set_ddb_entry(ha, ddb_entry->fw_ddb_index,
1633                                        fw_ddb_entry_dma, mbx_sts);
1634
1635         if (status != QLA_SUCCESS)
1636                 rval = -EINVAL;
1637 exit_set_param:
1638         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1639                           fw_ddb_entry, fw_ddb_entry_dma);
1640 exit_set_param_no_free:
1641         return rval;
1642 }
1643
1644 int qla4xxx_get_mgmt_data(struct scsi_qla_host *ha, uint16_t fw_ddb_index,
1645                           uint16_t stats_size, dma_addr_t stats_dma)
1646 {
1647         int status = QLA_SUCCESS;
1648         uint32_t mbox_cmd[MBOX_REG_COUNT];
1649         uint32_t mbox_sts[MBOX_REG_COUNT];
1650
1651         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
1652         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
1653         mbox_cmd[0] = MBOX_CMD_GET_MANAGEMENT_DATA;
1654         mbox_cmd[1] = fw_ddb_index;
1655         mbox_cmd[2] = LSDW(stats_dma);
1656         mbox_cmd[3] = MSDW(stats_dma);
1657         mbox_cmd[4] = stats_size;
1658
1659         status = qla4xxx_mailbox_command(ha, 5, 1, &mbox_cmd[0], &mbox_sts[0]);
1660         if (status != QLA_SUCCESS) {
1661                 DEBUG2(ql4_printk(KERN_WARNING, ha,
1662                                   "%s: MBOX_CMD_GET_MANAGEMENT_DATA "
1663                                   "failed w/ status %04X\n", __func__,
1664                                   mbox_sts[0]));
1665         }
1666         return status;
1667 }