[SCSI] qla4xxx: cleanup DDB relogin logic during initialization
[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 isssue 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         /* To prevent overwriting mailbox registers for a command that has
90          * not yet been serviced, check to see if an active command
91          * (AEN, IOCB, etc.) is interrupting, then service it.
92          * -----------------------------------------------------------------
93          */
94         spin_lock_irqsave(&ha->hardware_lock, flags);
95
96         if (!is_qla8022(ha)) {
97                 intr_status = readl(&ha->reg->ctrl_status);
98                 if (intr_status & CSR_SCSI_PROCESSOR_INTR) {
99                         /* Service existing interrupt */
100                         ha->isp_ops->interrupt_service_routine(ha, intr_status);
101                         clear_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
102                 }
103         }
104
105         ha->mbox_status_count = outCount;
106         for (i = 0; i < outCount; i++)
107                 ha->mbox_status[i] = 0;
108
109         if (is_qla8022(ha)) {
110                 /* Load all mailbox registers, except mailbox 0. */
111                 DEBUG5(
112                     printk("scsi%ld: %s: Cmd ", ha->host_no, __func__);
113                     for (i = 0; i < inCount; i++)
114                         printk("mb%d=%04x ", i, mbx_cmd[i]);
115                     printk("\n"));
116
117                 for (i = 1; i < inCount; i++)
118                         writel(mbx_cmd[i], &ha->qla4_8xxx_reg->mailbox_in[i]);
119                 writel(mbx_cmd[0], &ha->qla4_8xxx_reg->mailbox_in[0]);
120                 readl(&ha->qla4_8xxx_reg->mailbox_in[0]);
121                 writel(HINT_MBX_INT_PENDING, &ha->qla4_8xxx_reg->hint);
122         } else {
123                 /* Load all mailbox registers, except mailbox 0. */
124                 for (i = 1; i < inCount; i++)
125                         writel(mbx_cmd[i], &ha->reg->mailbox[i]);
126
127                 /* Wakeup firmware  */
128                 writel(mbx_cmd[0], &ha->reg->mailbox[0]);
129                 readl(&ha->reg->mailbox[0]);
130                 writel(set_rmask(CSR_INTR_RISC), &ha->reg->ctrl_status);
131                 readl(&ha->reg->ctrl_status);
132         }
133
134         spin_unlock_irqrestore(&ha->hardware_lock, flags);
135
136         /* Wait for completion */
137
138         /*
139          * If we don't want status, don't wait for the mailbox command to
140          * complete.  For example, MBOX_CMD_RESET_FW doesn't return status,
141          * you must poll the inbound Interrupt Mask for completion.
142          */
143         if (outCount == 0) {
144                 status = QLA_SUCCESS;
145                 goto mbox_exit;
146         }
147
148         /*
149          * Wait for completion: Poll or completion queue
150          */
151         if (test_bit(AF_IRQ_ATTACHED, &ha->flags) &&
152             test_bit(AF_INTERRUPTS_ON, &ha->flags) &&
153             test_bit(AF_ONLINE, &ha->flags) &&
154             !test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
155                 /* Do not poll for completion. Use completion queue */
156                 set_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags);
157                 wait_for_completion_timeout(&ha->mbx_intr_comp, MBOX_TOV * HZ);
158                 clear_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags);
159         } else {
160                 /* Poll for command to complete */
161                 wait_count = jiffies + MBOX_TOV * HZ;
162                 while (test_bit(AF_MBOX_COMMAND_DONE, &ha->flags) == 0) {
163                         if (time_after_eq(jiffies, wait_count))
164                                 break;
165
166                         /*
167                          * Service the interrupt.
168                          * The ISR will save the mailbox status registers
169                          * to a temporary storage location in the adapter
170                          * structure.
171                          */
172
173                         spin_lock_irqsave(&ha->hardware_lock, flags);
174                         if (is_qla8022(ha)) {
175                                 intr_status =
176                                     readl(&ha->qla4_8xxx_reg->host_int);
177                                 if (intr_status & ISRX_82XX_RISC_INT) {
178                                         ha->mbox_status_count = outCount;
179                                         intr_status =
180                                          readl(&ha->qla4_8xxx_reg->host_status);
181                                         ha->isp_ops->interrupt_service_routine(
182                                             ha, intr_status);
183                                         if (test_bit(AF_INTERRUPTS_ON,
184                                             &ha->flags) &&
185                                             test_bit(AF_INTx_ENABLED,
186                                             &ha->flags))
187                                                 qla4_8xxx_wr_32(ha,
188                                                 ha->nx_legacy_intr.tgt_mask_reg,
189                                                 0xfbff);
190                                 }
191                         } else {
192                                 intr_status = readl(&ha->reg->ctrl_status);
193                                 if (intr_status & INTR_PENDING) {
194                                         /*
195                                          * Service the interrupt.
196                                          * The ISR will save the mailbox status
197                                          * registers to a temporary storage
198                                          * location in the adapter structure.
199                                          */
200                                         ha->mbox_status_count = outCount;
201                                         ha->isp_ops->interrupt_service_routine(
202                                             ha, intr_status);
203                                 }
204                         }
205                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
206                         msleep(10);
207                 }
208         }
209
210         /* Check for mailbox timeout. */
211         if (!test_bit(AF_MBOX_COMMAND_DONE, &ha->flags)) {
212                 if (is_qla8022(ha) &&
213                     test_bit(AF_FW_RECOVERY, &ha->flags)) {
214                         DEBUG2(ql4_printk(KERN_INFO, ha,
215                             "scsi%ld: %s: prematurely completing mbx cmd as "
216                             "firmware recovery detected\n",
217                             ha->host_no, __func__));
218                         goto mbox_exit;
219                 }
220                 DEBUG2(printk("scsi%ld: Mailbox Cmd 0x%08X timed out ...,"
221                               " Scheduling Adapter Reset\n", ha->host_no,
222                               mbx_cmd[0]));
223                 ha->mailbox_timeout_count++;
224                 mbx_sts[0] = (-1);
225                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
226                 goto mbox_exit;
227         }
228
229         /*
230          * Copy the mailbox out registers to the caller's mailbox in/out
231          * structure.
232          */
233         spin_lock_irqsave(&ha->hardware_lock, flags);
234         for (i = 0; i < outCount; i++)
235                 mbx_sts[i] = ha->mbox_status[i];
236
237         /* Set return status and error flags (if applicable). */
238         switch (ha->mbox_status[0]) {
239         case MBOX_STS_COMMAND_COMPLETE:
240                 status = QLA_SUCCESS;
241                 break;
242
243         case MBOX_STS_INTERMEDIATE_COMPLETION:
244                 status = QLA_SUCCESS;
245                 break;
246
247         case MBOX_STS_BUSY:
248                 DEBUG2( printk("scsi%ld: %s: Cmd = %08X, ISP BUSY\n",
249                                ha->host_no, __func__, mbx_cmd[0]));
250                 ha->mailbox_timeout_count++;
251                 break;
252
253         default:
254                 DEBUG2(printk("scsi%ld: %s: **** FAILED, cmd = %08X, "
255                               "sts = %08X ****\n", ha->host_no, __func__,
256                               mbx_cmd[0], mbx_sts[0]));
257                 break;
258         }
259         spin_unlock_irqrestore(&ha->hardware_lock, flags);
260
261 mbox_exit:
262         mutex_lock(&ha->mbox_sem);
263         clear_bit(AF_MBOX_COMMAND, &ha->flags);
264         mutex_unlock(&ha->mbox_sem);
265         clear_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
266
267         return status;
268 }
269
270 void qla4xxx_mailbox_premature_completion(struct scsi_qla_host *ha)
271 {
272         set_bit(AF_FW_RECOVERY, &ha->flags);
273         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: set FW RECOVERY!\n",
274             ha->host_no, __func__);
275
276         if (test_bit(AF_MBOX_COMMAND, &ha->flags)) {
277                 if (test_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags)) {
278                         complete(&ha->mbx_intr_comp);
279                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Due to fw "
280                             "recovery, doing premature completion of "
281                             "mbx cmd\n", ha->host_no, __func__);
282
283                 } else {
284                         set_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
285                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Due to fw "
286                             "recovery, doing premature completion of "
287                             "polling mbx cmd\n", ha->host_no, __func__);
288                 }
289         }
290 }
291
292 static uint8_t
293 qla4xxx_set_ifcb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
294                  uint32_t *mbox_sts, dma_addr_t init_fw_cb_dma)
295 {
296         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
297         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
298
299         if (is_qla8022(ha))
300                 qla4_8xxx_wr_32(ha, ha->nx_db_wr_ptr, 0);
301
302         mbox_cmd[0] = MBOX_CMD_INITIALIZE_FIRMWARE;
303         mbox_cmd[1] = 0;
304         mbox_cmd[2] = LSDW(init_fw_cb_dma);
305         mbox_cmd[3] = MSDW(init_fw_cb_dma);
306         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
307         mbox_cmd[5] = (IFCB_VER_MAX << 8) | IFCB_VER_MIN;
308
309         if (qla4xxx_mailbox_command(ha, 6, 6, mbox_cmd, mbox_sts) !=
310             QLA_SUCCESS) {
311                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: "
312                               "MBOX_CMD_INITIALIZE_FIRMWARE"
313                               " failed w/ status %04X\n",
314                               ha->host_no, __func__, mbox_sts[0]));
315                 return QLA_ERROR;
316         }
317         return QLA_SUCCESS;
318 }
319
320 static uint8_t
321 qla4xxx_get_ifcb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
322                  uint32_t *mbox_sts, dma_addr_t init_fw_cb_dma)
323 {
324         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
325         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
326         mbox_cmd[0] = MBOX_CMD_GET_INIT_FW_CTRL_BLOCK;
327         mbox_cmd[2] = LSDW(init_fw_cb_dma);
328         mbox_cmd[3] = MSDW(init_fw_cb_dma);
329         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
330
331         if (qla4xxx_mailbox_command(ha, 5, 5, mbox_cmd, mbox_sts) !=
332             QLA_SUCCESS) {
333                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: "
334                               "MBOX_CMD_GET_INIT_FW_CTRL_BLOCK"
335                               " failed w/ status %04X\n",
336                               ha->host_no, __func__, mbox_sts[0]));
337                 return QLA_ERROR;
338         }
339         return QLA_SUCCESS;
340 }
341
342 static void
343 qla4xxx_update_local_ip(struct scsi_qla_host *ha,
344                          struct addr_ctrl_blk  *init_fw_cb)
345 {
346         /* Save IPv4 Address Info */
347         memcpy(ha->ip_address, init_fw_cb->ipv4_addr,
348                 min(sizeof(ha->ip_address), sizeof(init_fw_cb->ipv4_addr)));
349         memcpy(ha->subnet_mask, init_fw_cb->ipv4_subnet,
350                 min(sizeof(ha->subnet_mask), sizeof(init_fw_cb->ipv4_subnet)));
351         memcpy(ha->gateway, init_fw_cb->ipv4_gw_addr,
352                 min(sizeof(ha->gateway), sizeof(init_fw_cb->ipv4_gw_addr)));
353
354         if (is_ipv6_enabled(ha)) {
355                 /* Save IPv6 Address */
356                 ha->ipv6_link_local_state = init_fw_cb->ipv6_lnk_lcl_addr_state;
357                 ha->ipv6_addr0_state = init_fw_cb->ipv6_addr0_state;
358                 ha->ipv6_addr1_state = init_fw_cb->ipv6_addr1_state;
359                 ha->ipv6_default_router_state = init_fw_cb->ipv6_dflt_rtr_state;
360                 ha->ipv6_link_local_addr.in6_u.u6_addr8[0] = 0xFE;
361                 ha->ipv6_link_local_addr.in6_u.u6_addr8[1] = 0x80;
362
363                 memcpy(&ha->ipv6_link_local_addr.in6_u.u6_addr8[8],
364                         init_fw_cb->ipv6_if_id,
365                         min(sizeof(ha->ipv6_link_local_addr)/2,
366                         sizeof(init_fw_cb->ipv6_if_id)));
367                 memcpy(&ha->ipv6_addr0, init_fw_cb->ipv6_addr0,
368                         min(sizeof(ha->ipv6_addr0),
369                         sizeof(init_fw_cb->ipv6_addr0)));
370                 memcpy(&ha->ipv6_addr1, init_fw_cb->ipv6_addr1,
371                         min(sizeof(ha->ipv6_addr1),
372                         sizeof(init_fw_cb->ipv6_addr1)));
373                 memcpy(&ha->ipv6_default_router_addr,
374                         init_fw_cb->ipv6_dflt_rtr_addr,
375                         min(sizeof(ha->ipv6_default_router_addr),
376                         sizeof(init_fw_cb->ipv6_dflt_rtr_addr)));
377         }
378 }
379
380 static uint8_t
381 qla4xxx_update_local_ifcb(struct scsi_qla_host *ha,
382                           uint32_t *mbox_cmd,
383                           uint32_t *mbox_sts,
384                           struct addr_ctrl_blk  *init_fw_cb,
385                           dma_addr_t init_fw_cb_dma)
386 {
387         if (qla4xxx_get_ifcb(ha, mbox_cmd, mbox_sts, init_fw_cb_dma)
388             != QLA_SUCCESS) {
389                 DEBUG2(printk(KERN_WARNING
390                               "scsi%ld: %s: Failed to get init_fw_ctrl_blk\n",
391                               ha->host_no, __func__));
392                 return QLA_ERROR;
393         }
394
395         DEBUG2(qla4xxx_dump_buffer(init_fw_cb, sizeof(struct addr_ctrl_blk)));
396
397         /* Save some info in adapter structure. */
398         ha->acb_version = init_fw_cb->acb_version;
399         ha->firmware_options = le16_to_cpu(init_fw_cb->fw_options);
400         ha->tcp_options = le16_to_cpu(init_fw_cb->ipv4_tcp_opts);
401         ha->ipv4_options = le16_to_cpu(init_fw_cb->ipv4_ip_opts);
402         ha->ipv4_addr_state = le16_to_cpu(init_fw_cb->ipv4_addr_state);
403         ha->heartbeat_interval = init_fw_cb->hb_interval;
404         memcpy(ha->name_string, init_fw_cb->iscsi_name,
405                 min(sizeof(ha->name_string),
406                 sizeof(init_fw_cb->iscsi_name)));
407         /*memcpy(ha->alias, init_fw_cb->Alias,
408                min(sizeof(ha->alias), sizeof(init_fw_cb->Alias)));*/
409
410         if (ha->acb_version == ACB_SUPPORTED) {
411                 ha->ipv6_options = init_fw_cb->ipv6_opts;
412                 ha->ipv6_addl_options = init_fw_cb->ipv6_addtl_opts;
413         }
414         qla4xxx_update_local_ip(ha, init_fw_cb);
415
416         return QLA_SUCCESS;
417 }
418
419 /**
420  * qla4xxx_initialize_fw_cb - initializes firmware control block.
421  * @ha: Pointer to host adapter structure.
422  **/
423 int qla4xxx_initialize_fw_cb(struct scsi_qla_host * ha)
424 {
425         struct addr_ctrl_blk *init_fw_cb;
426         dma_addr_t init_fw_cb_dma;
427         uint32_t mbox_cmd[MBOX_REG_COUNT];
428         uint32_t mbox_sts[MBOX_REG_COUNT];
429         int status = QLA_ERROR;
430
431         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
432                                         sizeof(struct addr_ctrl_blk),
433                                         &init_fw_cb_dma, GFP_KERNEL);
434         if (init_fw_cb == NULL) {
435                 DEBUG2(printk("scsi%ld: %s: Unable to alloc init_cb\n",
436                               ha->host_no, __func__));
437                 goto exit_init_fw_cb_no_free;
438         }
439         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
440
441         /* Get Initialize Firmware Control Block. */
442         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
443         memset(&mbox_sts, 0, sizeof(mbox_sts));
444
445         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma) !=
446             QLA_SUCCESS) {
447                 dma_free_coherent(&ha->pdev->dev,
448                                   sizeof(struct addr_ctrl_blk),
449                                   init_fw_cb, init_fw_cb_dma);
450                 goto exit_init_fw_cb;
451         }
452
453         /* Initialize request and response queues. */
454         qla4xxx_init_rings(ha);
455
456         /* Fill in the request and response queue information. */
457         init_fw_cb->rqq_consumer_idx = cpu_to_le16(ha->request_out);
458         init_fw_cb->compq_producer_idx = cpu_to_le16(ha->response_in);
459         init_fw_cb->rqq_len = __constant_cpu_to_le16(REQUEST_QUEUE_DEPTH);
460         init_fw_cb->compq_len = __constant_cpu_to_le16(RESPONSE_QUEUE_DEPTH);
461         init_fw_cb->rqq_addr_lo = cpu_to_le32(LSDW(ha->request_dma));
462         init_fw_cb->rqq_addr_hi = cpu_to_le32(MSDW(ha->request_dma));
463         init_fw_cb->compq_addr_lo = cpu_to_le32(LSDW(ha->response_dma));
464         init_fw_cb->compq_addr_hi = cpu_to_le32(MSDW(ha->response_dma));
465         init_fw_cb->shdwreg_addr_lo = cpu_to_le32(LSDW(ha->shadow_regs_dma));
466         init_fw_cb->shdwreg_addr_hi = cpu_to_le32(MSDW(ha->shadow_regs_dma));
467
468         /* Set up required options. */
469         init_fw_cb->fw_options |=
470                 __constant_cpu_to_le16(FWOPT_SESSION_MODE |
471                                        FWOPT_INITIATOR_MODE);
472
473         if (is_qla8022(ha))
474                 init_fw_cb->fw_options |=
475                     __constant_cpu_to_le16(FWOPT_ENABLE_CRBDB);
476
477         init_fw_cb->fw_options &= __constant_cpu_to_le16(~FWOPT_TARGET_MODE);
478
479         if (qla4xxx_set_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)
480                 != QLA_SUCCESS) {
481                 DEBUG2(printk(KERN_WARNING
482                               "scsi%ld: %s: Failed to set init_fw_ctrl_blk\n",
483                               ha->host_no, __func__));
484                 goto exit_init_fw_cb;
485         }
486
487         if (qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0],
488                 init_fw_cb, init_fw_cb_dma) != QLA_SUCCESS) {
489                 DEBUG2(printk("scsi%ld: %s: Failed to update local ifcb\n",
490                                 ha->host_no, __func__));
491                 goto exit_init_fw_cb;
492         }
493         status = QLA_SUCCESS;
494
495 exit_init_fw_cb:
496         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
497                                 init_fw_cb, init_fw_cb_dma);
498 exit_init_fw_cb_no_free:
499         return status;
500 }
501
502 /**
503  * qla4xxx_get_dhcp_ip_address - gets HBA ip address via DHCP
504  * @ha: Pointer to host adapter structure.
505  **/
506 int qla4xxx_get_dhcp_ip_address(struct scsi_qla_host * ha)
507 {
508         struct addr_ctrl_blk *init_fw_cb;
509         dma_addr_t init_fw_cb_dma;
510         uint32_t mbox_cmd[MBOX_REG_COUNT];
511         uint32_t mbox_sts[MBOX_REG_COUNT];
512
513         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
514                                         sizeof(struct addr_ctrl_blk),
515                                         &init_fw_cb_dma, GFP_KERNEL);
516         if (init_fw_cb == NULL) {
517                 printk("scsi%ld: %s: Unable to alloc init_cb\n", ha->host_no,
518                        __func__);
519                 return QLA_ERROR;
520         }
521
522         /* Get Initialize Firmware Control Block. */
523         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
524         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma) !=
525             QLA_SUCCESS) {
526                 DEBUG2(printk("scsi%ld: %s: Failed to get init_fw_ctrl_blk\n",
527                               ha->host_no, __func__));
528                 dma_free_coherent(&ha->pdev->dev,
529                                   sizeof(struct addr_ctrl_blk),
530                                   init_fw_cb, init_fw_cb_dma);
531                 return QLA_ERROR;
532         }
533
534         /* Save IP Address. */
535         qla4xxx_update_local_ip(ha, init_fw_cb);
536         dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
537                                 init_fw_cb, init_fw_cb_dma);
538
539         return QLA_SUCCESS;
540 }
541
542 /**
543  * qla4xxx_get_firmware_state - gets firmware state of HBA
544  * @ha: Pointer to host adapter structure.
545  **/
546 int qla4xxx_get_firmware_state(struct scsi_qla_host * ha)
547 {
548         uint32_t mbox_cmd[MBOX_REG_COUNT];
549         uint32_t mbox_sts[MBOX_REG_COUNT];
550
551         /* Get firmware version */
552         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
553         memset(&mbox_sts, 0, sizeof(mbox_sts));
554
555         mbox_cmd[0] = MBOX_CMD_GET_FW_STATE;
556
557         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 4, &mbox_cmd[0], &mbox_sts[0]) !=
558             QLA_SUCCESS) {
559                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_GET_FW_STATE failed w/ "
560                               "status %04X\n", ha->host_no, __func__,
561                               mbox_sts[0]));
562                 return QLA_ERROR;
563         }
564         ha->firmware_state = mbox_sts[1];
565         ha->board_id = mbox_sts[2];
566         ha->addl_fw_state = mbox_sts[3];
567         DEBUG2(printk("scsi%ld: %s firmware_state=0x%x\n",
568                       ha->host_no, __func__, ha->firmware_state);)
569
570         return QLA_SUCCESS;
571 }
572
573 /**
574  * qla4xxx_get_firmware_status - retrieves firmware status
575  * @ha: Pointer to host adapter structure.
576  **/
577 int qla4xxx_get_firmware_status(struct scsi_qla_host * ha)
578 {
579         uint32_t mbox_cmd[MBOX_REG_COUNT];
580         uint32_t mbox_sts[MBOX_REG_COUNT];
581
582         /* Get firmware version */
583         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
584         memset(&mbox_sts, 0, sizeof(mbox_sts));
585
586         mbox_cmd[0] = MBOX_CMD_GET_FW_STATUS;
587
588         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 3, &mbox_cmd[0], &mbox_sts[0]) !=
589             QLA_SUCCESS) {
590                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_GET_FW_STATUS failed w/ "
591                               "status %04X\n", ha->host_no, __func__,
592                               mbox_sts[0]));
593                 return QLA_ERROR;
594         }
595
596         ql4_printk(KERN_INFO, ha, "%ld firmare IOCBs available (%d).\n",
597             ha->host_no, mbox_sts[2]);
598
599         return QLA_SUCCESS;
600 }
601
602 /**
603  * qla4xxx_get_fwddb_entry - retrieves firmware ddb entry
604  * @ha: Pointer to host adapter structure.
605  * @fw_ddb_index: Firmware's device database index
606  * @fw_ddb_entry: Pointer to firmware's device database entry structure
607  * @num_valid_ddb_entries: Pointer to number of valid ddb entries
608  * @next_ddb_index: Pointer to next valid device database index
609  * @fw_ddb_device_state: Pointer to device state
610  **/
611 int qla4xxx_get_fwddb_entry(struct scsi_qla_host *ha,
612                             uint16_t fw_ddb_index,
613                             struct dev_db_entry *fw_ddb_entry,
614                             dma_addr_t fw_ddb_entry_dma,
615                             uint32_t *num_valid_ddb_entries,
616                             uint32_t *next_ddb_index,
617                             uint32_t *fw_ddb_device_state,
618                             uint32_t *conn_err_detail,
619                             uint16_t *tcp_source_port_num,
620                             uint16_t *connection_id)
621 {
622         int status = QLA_ERROR;
623         uint16_t options;
624         uint32_t mbox_cmd[MBOX_REG_COUNT];
625         uint32_t mbox_sts[MBOX_REG_COUNT];
626
627         /* Make sure the device index is valid */
628         if (fw_ddb_index >= MAX_DDB_ENTRIES) {
629                 DEBUG2(printk("scsi%ld: %s: ddb [%d] out of range.\n",
630                               ha->host_no, __func__, fw_ddb_index));
631                 goto exit_get_fwddb;
632         }
633         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
634         memset(&mbox_sts, 0, sizeof(mbox_sts));
635
636         mbox_cmd[0] = MBOX_CMD_GET_DATABASE_ENTRY;
637         mbox_cmd[1] = (uint32_t) fw_ddb_index;
638         mbox_cmd[2] = LSDW(fw_ddb_entry_dma);
639         mbox_cmd[3] = MSDW(fw_ddb_entry_dma);
640         mbox_cmd[4] = sizeof(struct dev_db_entry);
641
642         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 7, &mbox_cmd[0], &mbox_sts[0]) ==
643             QLA_ERROR) {
644                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_GET_DATABASE_ENTRY failed"
645                               " with status 0x%04X\n", ha->host_no, __func__,
646                               mbox_sts[0]));
647                 goto exit_get_fwddb;
648         }
649         if (fw_ddb_index != mbox_sts[1]) {
650                 DEBUG2(printk("scsi%ld: %s: ddb mismatch [%d] != [%d].\n",
651                               ha->host_no, __func__, fw_ddb_index,
652                               mbox_sts[1]));
653                 goto exit_get_fwddb;
654         }
655         if (fw_ddb_entry) {
656                 options = le16_to_cpu(fw_ddb_entry->options);
657                 if (options & DDB_OPT_IPV6_DEVICE) {
658                         ql4_printk(KERN_INFO, ha, "%s: DDB[%d] MB0 %04x Tot %d "
659                                 "Next %d State %04x ConnErr %08x %pI6 "
660                                 ":%04d \"%s\"\n", __func__, fw_ddb_index,
661                                 mbox_sts[0], mbox_sts[2], mbox_sts[3],
662                                 mbox_sts[4], mbox_sts[5],
663                                 fw_ddb_entry->ip_addr,
664                                 le16_to_cpu(fw_ddb_entry->port),
665                                 fw_ddb_entry->iscsi_name);
666                 } else {
667                         ql4_printk(KERN_INFO, ha, "%s: DDB[%d] MB0 %04x Tot %d "
668                                 "Next %d State %04x ConnErr %08x %pI4 "
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                 }
676         }
677         if (num_valid_ddb_entries)
678                 *num_valid_ddb_entries = mbox_sts[2];
679         if (next_ddb_index)
680                 *next_ddb_index = mbox_sts[3];
681         if (fw_ddb_device_state)
682                 *fw_ddb_device_state = mbox_sts[4];
683
684         /*
685          * RA: This mailbox has been changed to pass connection error and
686          * details.  Its true for ISP4010 as per Version E - Not sure when it
687          * was changed.  Get the time2wait from the fw_dd_entry field :
688          * default_time2wait which we call it as minTime2Wait DEV_DB_ENTRY
689          * struct.
690          */
691         if (conn_err_detail)
692                 *conn_err_detail = mbox_sts[5];
693         if (tcp_source_port_num)
694                 *tcp_source_port_num = (uint16_t) (mbox_sts[6] >> 16);
695         if (connection_id)
696                 *connection_id = (uint16_t) mbox_sts[6] & 0x00FF;
697         status = QLA_SUCCESS;
698
699 exit_get_fwddb:
700         return status;
701 }
702
703 /**
704  * qla4xxx_set_fwddb_entry - sets a ddb entry.
705  * @ha: Pointer to host adapter structure.
706  * @fw_ddb_index: Firmware's device database index
707  * @fw_ddb_entry: Pointer to firmware's ddb entry structure, or NULL.
708  *
709  * This routine initializes or updates the adapter's device database
710  * entry for the specified device. It also triggers a login for the
711  * specified device. Therefore, it may also be used as a secondary
712  * login routine when a NULL pointer is specified for the fw_ddb_entry.
713  **/
714 int qla4xxx_set_ddb_entry(struct scsi_qla_host * ha, uint16_t fw_ddb_index,
715                           dma_addr_t fw_ddb_entry_dma)
716 {
717         uint32_t mbox_cmd[MBOX_REG_COUNT];
718         uint32_t mbox_sts[MBOX_REG_COUNT];
719         int status;
720
721         /* Do not wait for completion. The firmware will send us an
722          * ASTS_DATABASE_CHANGED (0x8014) to notify us of the login status.
723          */
724         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
725         memset(&mbox_sts, 0, sizeof(mbox_sts));
726
727         mbox_cmd[0] = MBOX_CMD_SET_DATABASE_ENTRY;
728         mbox_cmd[1] = (uint32_t) fw_ddb_index;
729         mbox_cmd[2] = LSDW(fw_ddb_entry_dma);
730         mbox_cmd[3] = MSDW(fw_ddb_entry_dma);
731         mbox_cmd[4] = sizeof(struct dev_db_entry);
732
733         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0],
734             &mbox_sts[0]);
735         DEBUG2(printk("scsi%ld: %s: status=%d mbx0=0x%x mbx4=0x%x\n",
736             ha->host_no, __func__, status, mbox_sts[0], mbox_sts[4]);)
737
738         return status;
739 }
740
741 /**
742  * qla4xxx_get_crash_record - retrieves crash record.
743  * @ha: Pointer to host adapter structure.
744  *
745  * This routine retrieves a crash record from the QLA4010 after an 8002h aen.
746  **/
747 void qla4xxx_get_crash_record(struct scsi_qla_host * ha)
748 {
749         uint32_t mbox_cmd[MBOX_REG_COUNT];
750         uint32_t mbox_sts[MBOX_REG_COUNT];
751         struct crash_record *crash_record = NULL;
752         dma_addr_t crash_record_dma = 0;
753         uint32_t crash_record_size = 0;
754
755         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
756         memset(&mbox_sts, 0, sizeof(mbox_cmd));
757
758         /* Get size of crash record. */
759         mbox_cmd[0] = MBOX_CMD_GET_CRASH_RECORD;
760
761         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
762             QLA_SUCCESS) {
763                 DEBUG2(printk("scsi%ld: %s: ERROR: Unable to retrieve size!\n",
764                               ha->host_no, __func__));
765                 goto exit_get_crash_record;
766         }
767         crash_record_size = mbox_sts[4];
768         if (crash_record_size == 0) {
769                 DEBUG2(printk("scsi%ld: %s: ERROR: Crash record size is 0!\n",
770                               ha->host_no, __func__));
771                 goto exit_get_crash_record;
772         }
773
774         /* Alloc Memory for Crash Record. */
775         crash_record = dma_alloc_coherent(&ha->pdev->dev, crash_record_size,
776                                           &crash_record_dma, GFP_KERNEL);
777         if (crash_record == NULL)
778                 goto exit_get_crash_record;
779
780         /* Get Crash Record. */
781         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
782         memset(&mbox_sts, 0, sizeof(mbox_cmd));
783
784         mbox_cmd[0] = MBOX_CMD_GET_CRASH_RECORD;
785         mbox_cmd[2] = LSDW(crash_record_dma);
786         mbox_cmd[3] = MSDW(crash_record_dma);
787         mbox_cmd[4] = crash_record_size;
788
789         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
790             QLA_SUCCESS)
791                 goto exit_get_crash_record;
792
793         /* Dump Crash Record. */
794
795 exit_get_crash_record:
796         if (crash_record)
797                 dma_free_coherent(&ha->pdev->dev, crash_record_size,
798                                   crash_record, crash_record_dma);
799 }
800
801 /**
802  * qla4xxx_get_conn_event_log - retrieves connection event log
803  * @ha: Pointer to host adapter structure.
804  **/
805 void qla4xxx_get_conn_event_log(struct scsi_qla_host * ha)
806 {
807         uint32_t mbox_cmd[MBOX_REG_COUNT];
808         uint32_t mbox_sts[MBOX_REG_COUNT];
809         struct conn_event_log_entry *event_log = NULL;
810         dma_addr_t event_log_dma = 0;
811         uint32_t event_log_size = 0;
812         uint32_t num_valid_entries;
813         uint32_t      oldest_entry = 0;
814         uint32_t        max_event_log_entries;
815         uint8_t         i;
816
817
818         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
819         memset(&mbox_sts, 0, sizeof(mbox_cmd));
820
821         /* Get size of crash record. */
822         mbox_cmd[0] = MBOX_CMD_GET_CONN_EVENT_LOG;
823
824         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
825             QLA_SUCCESS)
826                 goto exit_get_event_log;
827
828         event_log_size = mbox_sts[4];
829         if (event_log_size == 0)
830                 goto exit_get_event_log;
831
832         /* Alloc Memory for Crash Record. */
833         event_log = dma_alloc_coherent(&ha->pdev->dev, event_log_size,
834                                        &event_log_dma, GFP_KERNEL);
835         if (event_log == NULL)
836                 goto exit_get_event_log;
837
838         /* Get Crash Record. */
839         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
840         memset(&mbox_sts, 0, sizeof(mbox_cmd));
841
842         mbox_cmd[0] = MBOX_CMD_GET_CONN_EVENT_LOG;
843         mbox_cmd[2] = LSDW(event_log_dma);
844         mbox_cmd[3] = MSDW(event_log_dma);
845
846         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
847             QLA_SUCCESS) {
848                 DEBUG2(printk("scsi%ld: %s: ERROR: Unable to retrieve event "
849                               "log!\n", ha->host_no, __func__));
850                 goto exit_get_event_log;
851         }
852
853         /* Dump Event Log. */
854         num_valid_entries = mbox_sts[1];
855
856         max_event_log_entries = event_log_size /
857                 sizeof(struct conn_event_log_entry);
858
859         if (num_valid_entries > max_event_log_entries)
860                 oldest_entry = num_valid_entries % max_event_log_entries;
861
862         DEBUG3(printk("scsi%ld: Connection Event Log Dump (%d entries):\n",
863                       ha->host_no, num_valid_entries));
864
865         if (ql4xextended_error_logging == 3) {
866                 if (oldest_entry == 0) {
867                         /* Circular Buffer has not wrapped around */
868                         for (i=0; i < num_valid_entries; i++) {
869                                 qla4xxx_dump_buffer((uint8_t *)event_log+
870                                                     (i*sizeof(*event_log)),
871                                                     sizeof(*event_log));
872                         }
873                 }
874                 else {
875                         /* Circular Buffer has wrapped around -
876                          * display accordingly*/
877                         for (i=oldest_entry; i < max_event_log_entries; i++) {
878                                 qla4xxx_dump_buffer((uint8_t *)event_log+
879                                                     (i*sizeof(*event_log)),
880                                                     sizeof(*event_log));
881                         }
882                         for (i=0; i < oldest_entry; i++) {
883                                 qla4xxx_dump_buffer((uint8_t *)event_log+
884                                                     (i*sizeof(*event_log)),
885                                                     sizeof(*event_log));
886                         }
887                 }
888         }
889
890 exit_get_event_log:
891         if (event_log)
892                 dma_free_coherent(&ha->pdev->dev, event_log_size, event_log,
893                                   event_log_dma);
894 }
895
896 /**
897  * qla4xxx_abort_task - issues Abort Task
898  * @ha: Pointer to host adapter structure.
899  * @srb: Pointer to srb entry
900  *
901  * This routine performs a LUN RESET on the specified target/lun.
902  * The caller must ensure that the ddb_entry and lun_entry pointers
903  * are valid before calling this routine.
904  **/
905 int qla4xxx_abort_task(struct scsi_qla_host *ha, struct srb *srb)
906 {
907         uint32_t mbox_cmd[MBOX_REG_COUNT];
908         uint32_t mbox_sts[MBOX_REG_COUNT];
909         struct scsi_cmnd *cmd = srb->cmd;
910         int status = QLA_SUCCESS;
911         unsigned long flags = 0;
912         uint32_t index;
913
914         /*
915          * Send abort task command to ISP, so that the ISP will return
916          * request with ABORT status
917          */
918         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
919         memset(&mbox_sts, 0, sizeof(mbox_sts));
920
921         spin_lock_irqsave(&ha->hardware_lock, flags);
922         index = (unsigned long)(unsigned char *)cmd->host_scribble;
923         spin_unlock_irqrestore(&ha->hardware_lock, flags);
924
925         /* Firmware already posted completion on response queue */
926         if (index == MAX_SRBS)
927                 return status;
928
929         mbox_cmd[0] = MBOX_CMD_ABORT_TASK;
930         mbox_cmd[1] = srb->ddb->fw_ddb_index;
931         mbox_cmd[2] = index;
932         /* Immediate Command Enable */
933         mbox_cmd[5] = 0x01;
934
935         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0],
936             &mbox_sts[0]);
937         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE) {
938                 status = QLA_ERROR;
939
940                 DEBUG2(printk(KERN_WARNING "scsi%ld:%d:%d: abort task FAILED: "
941                     "mbx0=%04X, mb1=%04X, mb2=%04X, mb3=%04X, mb4=%04X\n",
942                     ha->host_no, cmd->device->id, cmd->device->lun, mbox_sts[0],
943                     mbox_sts[1], mbox_sts[2], mbox_sts[3], mbox_sts[4]));
944         }
945
946         return status;
947 }
948
949 /**
950  * qla4xxx_reset_lun - issues LUN Reset
951  * @ha: Pointer to host adapter structure.
952  * @ddb_entry: Pointer to device database entry
953  * @lun: lun number
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_reset_lun(struct scsi_qla_host * ha, struct ddb_entry * ddb_entry,
960                       int lun)
961 {
962         uint32_t mbox_cmd[MBOX_REG_COUNT];
963         uint32_t mbox_sts[MBOX_REG_COUNT];
964         int status = QLA_SUCCESS;
965
966         DEBUG2(printk("scsi%ld:%d:%d: lun reset issued\n", ha->host_no,
967                       ddb_entry->fw_ddb_index, lun));
968
969         /*
970          * Send lun reset command to ISP, so that the ISP will return all
971          * outstanding requests with RESET status
972          */
973         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
974         memset(&mbox_sts, 0, sizeof(mbox_sts));
975
976         mbox_cmd[0] = MBOX_CMD_LUN_RESET;
977         mbox_cmd[1] = ddb_entry->fw_ddb_index;
978         mbox_cmd[2] = lun << 8;
979         mbox_cmd[5] = 0x01;     /* Immediate Command Enable */
980
981         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0], &mbox_sts[0]);
982         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE &&
983             mbox_sts[0] != MBOX_STS_COMMAND_ERROR)
984                 status = QLA_ERROR;
985
986         return status;
987 }
988
989 /**
990  * qla4xxx_reset_target - issues target Reset
991  * @ha: Pointer to host adapter structure.
992  * @db_entry: Pointer to device database entry
993  * @un_entry: Pointer to lun entry structure
994  *
995  * This routine performs a TARGET RESET on the specified target.
996  * The caller must ensure that the ddb_entry pointers
997  * are valid before calling this routine.
998  **/
999 int qla4xxx_reset_target(struct scsi_qla_host *ha,
1000                          struct ddb_entry *ddb_entry)
1001 {
1002         uint32_t mbox_cmd[MBOX_REG_COUNT];
1003         uint32_t mbox_sts[MBOX_REG_COUNT];
1004         int status = QLA_SUCCESS;
1005
1006         DEBUG2(printk("scsi%ld:%d: target reset issued\n", ha->host_no,
1007                       ddb_entry->fw_ddb_index));
1008
1009         /*
1010          * Send target reset command to ISP, so that the ISP will return all
1011          * outstanding requests with RESET status
1012          */
1013         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1014         memset(&mbox_sts, 0, sizeof(mbox_sts));
1015
1016         mbox_cmd[0] = MBOX_CMD_TARGET_WARM_RESET;
1017         mbox_cmd[1] = ddb_entry->fw_ddb_index;
1018         mbox_cmd[5] = 0x01;     /* Immediate Command Enable */
1019
1020         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
1021                                 &mbox_sts[0]);
1022         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE &&
1023             mbox_sts[0] != MBOX_STS_COMMAND_ERROR)
1024                 status = QLA_ERROR;
1025
1026         return status;
1027 }
1028
1029 int qla4xxx_get_flash(struct scsi_qla_host * ha, dma_addr_t dma_addr,
1030                       uint32_t offset, uint32_t len)
1031 {
1032         uint32_t mbox_cmd[MBOX_REG_COUNT];
1033         uint32_t mbox_sts[MBOX_REG_COUNT];
1034
1035         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1036         memset(&mbox_sts, 0, sizeof(mbox_sts));
1037
1038         mbox_cmd[0] = MBOX_CMD_READ_FLASH;
1039         mbox_cmd[1] = LSDW(dma_addr);
1040         mbox_cmd[2] = MSDW(dma_addr);
1041         mbox_cmd[3] = offset;
1042         mbox_cmd[4] = len;
1043
1044         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 2, &mbox_cmd[0], &mbox_sts[0]) !=
1045             QLA_SUCCESS) {
1046                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_READ_FLASH, failed w/ "
1047                     "status %04X %04X, offset %08x, len %08x\n", ha->host_no,
1048                     __func__, mbox_sts[0], mbox_sts[1], offset, len));
1049                 return QLA_ERROR;
1050         }
1051         return QLA_SUCCESS;
1052 }
1053
1054 /**
1055  * qla4xxx_get_fw_version - gets firmware version
1056  * @ha: Pointer to host adapter structure.
1057  *
1058  * Retrieves the firmware version on HBA. In QLA4010, mailboxes 2 & 3 may
1059  * hold an address for data.  Make sure that we write 0 to those mailboxes,
1060  * if unused.
1061  **/
1062 int qla4xxx_get_fw_version(struct scsi_qla_host * ha)
1063 {
1064         uint32_t mbox_cmd[MBOX_REG_COUNT];
1065         uint32_t mbox_sts[MBOX_REG_COUNT];
1066
1067         /* Get firmware version. */
1068         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1069         memset(&mbox_sts, 0, sizeof(mbox_sts));
1070
1071         mbox_cmd[0] = MBOX_CMD_ABOUT_FW;
1072
1073         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
1074             QLA_SUCCESS) {
1075                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_ABOUT_FW failed w/ "
1076                     "status %04X\n", ha->host_no, __func__, mbox_sts[0]));
1077                 return QLA_ERROR;
1078         }
1079
1080         /* Save firmware version information. */
1081         ha->firmware_version[0] = mbox_sts[1];
1082         ha->firmware_version[1] = mbox_sts[2];
1083         ha->patch_number = mbox_sts[3];
1084         ha->build_number = mbox_sts[4];
1085
1086         return QLA_SUCCESS;
1087 }
1088
1089 static int qla4xxx_get_default_ddb(struct scsi_qla_host *ha,
1090                                    dma_addr_t dma_addr)
1091 {
1092         uint32_t mbox_cmd[MBOX_REG_COUNT];
1093         uint32_t mbox_sts[MBOX_REG_COUNT];
1094
1095         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1096         memset(&mbox_sts, 0, sizeof(mbox_sts));
1097
1098         mbox_cmd[0] = MBOX_CMD_GET_DATABASE_ENTRY_DEFAULTS;
1099         mbox_cmd[2] = LSDW(dma_addr);
1100         mbox_cmd[3] = MSDW(dma_addr);
1101
1102         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0], &mbox_sts[0]) !=
1103             QLA_SUCCESS) {
1104                 DEBUG2(printk("scsi%ld: %s: failed status %04X\n",
1105                      ha->host_no, __func__, mbox_sts[0]));
1106                 return QLA_ERROR;
1107         }
1108         return QLA_SUCCESS;
1109 }
1110
1111 static int qla4xxx_req_ddb_entry(struct scsi_qla_host *ha, uint32_t *ddb_index)
1112 {
1113         uint32_t mbox_cmd[MBOX_REG_COUNT];
1114         uint32_t mbox_sts[MBOX_REG_COUNT];
1115
1116         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1117         memset(&mbox_sts, 0, sizeof(mbox_sts));
1118
1119         mbox_cmd[0] = MBOX_CMD_REQUEST_DATABASE_ENTRY;
1120         mbox_cmd[1] = MAX_PRST_DEV_DB_ENTRIES;
1121
1122         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 3, &mbox_cmd[0], &mbox_sts[0]) !=
1123             QLA_SUCCESS) {
1124                 if (mbox_sts[0] == MBOX_STS_COMMAND_ERROR) {
1125                         *ddb_index = mbox_sts[2];
1126                 } else {
1127                         DEBUG2(printk("scsi%ld: %s: failed status %04X\n",
1128                              ha->host_no, __func__, mbox_sts[0]));
1129                         return QLA_ERROR;
1130                 }
1131         } else {
1132                 *ddb_index = MAX_PRST_DEV_DB_ENTRIES;
1133         }
1134
1135         return QLA_SUCCESS;
1136 }
1137
1138
1139 int qla4xxx_send_tgts(struct scsi_qla_host *ha, char *ip, uint16_t port)
1140 {
1141         struct dev_db_entry *fw_ddb_entry;
1142         dma_addr_t fw_ddb_entry_dma;
1143         uint32_t ddb_index;
1144         int ret_val = QLA_SUCCESS;
1145
1146
1147         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
1148                                           sizeof(*fw_ddb_entry),
1149                                           &fw_ddb_entry_dma, GFP_KERNEL);
1150         if (!fw_ddb_entry) {
1151                 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
1152                               ha->host_no, __func__));
1153                 ret_val = QLA_ERROR;
1154                 goto exit_send_tgts_no_free;
1155         }
1156
1157         ret_val = qla4xxx_get_default_ddb(ha, fw_ddb_entry_dma);
1158         if (ret_val != QLA_SUCCESS)
1159                 goto exit_send_tgts;
1160
1161         ret_val = qla4xxx_req_ddb_entry(ha, &ddb_index);
1162         if (ret_val != QLA_SUCCESS)
1163                 goto exit_send_tgts;
1164
1165         memset(fw_ddb_entry->iscsi_alias, 0,
1166                sizeof(fw_ddb_entry->iscsi_alias));
1167
1168         memset(fw_ddb_entry->iscsi_name, 0,
1169                sizeof(fw_ddb_entry->iscsi_name));
1170
1171         memset(fw_ddb_entry->ip_addr, 0, sizeof(fw_ddb_entry->ip_addr));
1172         memset(fw_ddb_entry->tgt_addr, 0,
1173                sizeof(fw_ddb_entry->tgt_addr));
1174
1175         fw_ddb_entry->options = (DDB_OPT_DISC_SESSION | DDB_OPT_TARGET);
1176         fw_ddb_entry->port = cpu_to_le16(ntohs(port));
1177
1178         fw_ddb_entry->ip_addr[0] = *ip;
1179         fw_ddb_entry->ip_addr[1] = *(ip + 1);
1180         fw_ddb_entry->ip_addr[2] = *(ip + 2);
1181         fw_ddb_entry->ip_addr[3] = *(ip + 3);
1182
1183         ret_val = qla4xxx_set_ddb_entry(ha, ddb_index, fw_ddb_entry_dma);
1184
1185 exit_send_tgts:
1186         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1187                           fw_ddb_entry, fw_ddb_entry_dma);
1188 exit_send_tgts_no_free:
1189         return ret_val;
1190 }
1191