Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[pandora-kernel.git] / drivers / scsi / qla4xxx / ql4_mbx.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2006 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
36         /* Make sure that pointers are valid */
37         if (!mbx_cmd || !mbx_sts) {
38                 DEBUG2(printk("scsi%ld: %s: Invalid mbx_cmd or mbx_sts "
39                               "pointer\n", ha->host_no, __func__));
40                 return status;
41         }
42
43         if (is_qla8022(ha) &&
44             test_bit(AF_FW_RECOVERY, &ha->flags)) {
45                 DEBUG2(ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: prematurely "
46                     "completing mbx cmd as firmware recovery detected\n",
47                     ha->host_no, __func__));
48                 return status;
49         }
50
51         if ((is_aer_supported(ha)) &&
52             (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))) {
53                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Perm failure on EEH, "
54                     "timeout MBX Exiting.\n", ha->host_no, __func__));
55                 return status;
56         }
57
58         /* Mailbox code active */
59         wait_count = MBOX_TOV * 100;
60
61         while (wait_count--) {
62                 mutex_lock(&ha->mbox_sem);
63                 if (!test_bit(AF_MBOX_COMMAND, &ha->flags)) {
64                         set_bit(AF_MBOX_COMMAND, &ha->flags);
65                         mutex_unlock(&ha->mbox_sem);
66                         break;
67                 }
68                 mutex_unlock(&ha->mbox_sem);
69                 if (!wait_count) {
70                         DEBUG2(printk("scsi%ld: %s: mbox_sem failed\n",
71                                 ha->host_no, __func__));
72                         return status;
73                 }
74                 msleep(10);
75         }
76
77         /* To prevent overwriting mailbox registers for a command that has
78          * not yet been serviced, check to see if an active command
79          * (AEN, IOCB, etc.) is interrupting, then service it.
80          * -----------------------------------------------------------------
81          */
82         spin_lock_irqsave(&ha->hardware_lock, flags);
83
84         if (is_qla8022(ha)) {
85                 intr_status = readl(&ha->qla4_8xxx_reg->host_int);
86                 if (intr_status & ISRX_82XX_RISC_INT) {
87                         /* Service existing interrupt */
88                         DEBUG2(printk("scsi%ld: %s: "
89                             "servicing existing interrupt\n",
90                             ha->host_no, __func__));
91                         intr_status = readl(&ha->qla4_8xxx_reg->host_status);
92                         ha->isp_ops->interrupt_service_routine(ha, intr_status);
93                         clear_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
94                         if (test_bit(AF_INTERRUPTS_ON, &ha->flags) &&
95                             test_bit(AF_INTx_ENABLED, &ha->flags))
96                                 qla4_8xxx_wr_32(ha,
97                                     ha->nx_legacy_intr.tgt_mask_reg,
98                                     0xfbff);
99                 }
100         } else {
101                 intr_status = readl(&ha->reg->ctrl_status);
102                 if (intr_status & CSR_SCSI_PROCESSOR_INTR) {
103                         /* Service existing interrupt */
104                         ha->isp_ops->interrupt_service_routine(ha, intr_status);
105                         clear_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
106                 }
107         }
108
109         ha->mbox_status_count = outCount;
110         for (i = 0; i < outCount; i++)
111                 ha->mbox_status[i] = 0;
112
113         if (is_qla8022(ha)) {
114                 /* Load all mailbox registers, except mailbox 0. */
115                 DEBUG5(
116                     printk("scsi%ld: %s: Cmd ", ha->host_no, __func__);
117                     for (i = 0; i < inCount; i++)
118                         printk("mb%d=%04x ", i, mbx_cmd[i]);
119                     printk("\n"));
120
121                 for (i = 1; i < inCount; i++)
122                         writel(mbx_cmd[i], &ha->qla4_8xxx_reg->mailbox_in[i]);
123                 writel(mbx_cmd[0], &ha->qla4_8xxx_reg->mailbox_in[0]);
124                 readl(&ha->qla4_8xxx_reg->mailbox_in[0]);
125                 writel(HINT_MBX_INT_PENDING, &ha->qla4_8xxx_reg->hint);
126         } else {
127                 /* Load all mailbox registers, except mailbox 0. */
128                 for (i = 1; i < inCount; i++)
129                         writel(mbx_cmd[i], &ha->reg->mailbox[i]);
130
131                 /* Wakeup firmware  */
132                 writel(mbx_cmd[0], &ha->reg->mailbox[0]);
133                 readl(&ha->reg->mailbox[0]);
134                 writel(set_rmask(CSR_INTR_RISC), &ha->reg->ctrl_status);
135                 readl(&ha->reg->ctrl_status);
136         }
137
138         spin_unlock_irqrestore(&ha->hardware_lock, flags);
139
140         /* Wait for completion */
141
142         /*
143          * If we don't want status, don't wait for the mailbox command to
144          * complete.  For example, MBOX_CMD_RESET_FW doesn't return status,
145          * you must poll the inbound Interrupt Mask for completion.
146          */
147         if (outCount == 0) {
148                 status = QLA_SUCCESS;
149                 goto mbox_exit;
150         }
151
152         /*
153          * Wait for completion: Poll or completion queue
154          */
155         if (test_bit(AF_IRQ_ATTACHED, &ha->flags) &&
156             test_bit(AF_INTERRUPTS_ON, &ha->flags) &&
157             test_bit(AF_ONLINE, &ha->flags) &&
158             !test_bit(AF_HBA_GOING_AWAY, &ha->flags)) {
159                 /* Do not poll for completion. Use completion queue */
160                 set_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags);
161                 wait_for_completion_timeout(&ha->mbx_intr_comp, MBOX_TOV * HZ);
162                 clear_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags);
163         } else {
164                 /* Poll for command to complete */
165                 wait_count = jiffies + MBOX_TOV * HZ;
166                 while (test_bit(AF_MBOX_COMMAND_DONE, &ha->flags) == 0) {
167                         if (time_after_eq(jiffies, wait_count))
168                                 break;
169
170                         /*
171                          * Service the interrupt.
172                          * The ISR will save the mailbox status registers
173                          * to a temporary storage location in the adapter
174                          * structure.
175                          */
176
177                         spin_lock_irqsave(&ha->hardware_lock, flags);
178                         if (is_qla8022(ha)) {
179                                 intr_status =
180                                     readl(&ha->qla4_8xxx_reg->host_int);
181                                 if (intr_status & ISRX_82XX_RISC_INT) {
182                                         ha->mbox_status_count = outCount;
183                                         intr_status =
184                                          readl(&ha->qla4_8xxx_reg->host_status);
185                                         ha->isp_ops->interrupt_service_routine(
186                                             ha, intr_status);
187                                         if (test_bit(AF_INTERRUPTS_ON,
188                                             &ha->flags) &&
189                                             test_bit(AF_INTx_ENABLED,
190                                             &ha->flags))
191                                                 qla4_8xxx_wr_32(ha,
192                                                 ha->nx_legacy_intr.tgt_mask_reg,
193                                                 0xfbff);
194                                 }
195                         } else {
196                                 intr_status = readl(&ha->reg->ctrl_status);
197                                 if (intr_status & INTR_PENDING) {
198                                         /*
199                                          * Service the interrupt.
200                                          * The ISR will save the mailbox status
201                                          * registers to a temporary storage
202                                          * location in the adapter structure.
203                                          */
204                                         ha->mbox_status_count = outCount;
205                                         ha->isp_ops->interrupt_service_routine(
206                                             ha, intr_status);
207                                 }
208                         }
209                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
210                         msleep(10);
211                 }
212         }
213
214         /* Check for mailbox timeout. */
215         if (!test_bit(AF_MBOX_COMMAND_DONE, &ha->flags)) {
216                 if (is_qla8022(ha) &&
217                     test_bit(AF_FW_RECOVERY, &ha->flags)) {
218                         DEBUG2(ql4_printk(KERN_INFO, ha,
219                             "scsi%ld: %s: prematurely completing mbx cmd as "
220                             "firmware recovery detected\n",
221                             ha->host_no, __func__));
222                         goto mbox_exit;
223                 }
224                 DEBUG2(printk("scsi%ld: Mailbox Cmd 0x%08X timed out ...,"
225                               " Scheduling Adapter Reset\n", ha->host_no,
226                               mbx_cmd[0]));
227                 ha->mailbox_timeout_count++;
228                 mbx_sts[0] = (-1);
229                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
230                 goto mbox_exit;
231         }
232
233         /*
234          * Copy the mailbox out registers to the caller's mailbox in/out
235          * structure.
236          */
237         spin_lock_irqsave(&ha->hardware_lock, flags);
238         for (i = 0; i < outCount; i++)
239                 mbx_sts[i] = ha->mbox_status[i];
240
241         /* Set return status and error flags (if applicable). */
242         switch (ha->mbox_status[0]) {
243         case MBOX_STS_COMMAND_COMPLETE:
244                 status = QLA_SUCCESS;
245                 break;
246
247         case MBOX_STS_INTERMEDIATE_COMPLETION:
248                 status = QLA_SUCCESS;
249                 break;
250
251         case MBOX_STS_BUSY:
252                 DEBUG2( printk("scsi%ld: %s: Cmd = %08X, ISP BUSY\n",
253                                ha->host_no, __func__, mbx_cmd[0]));
254                 ha->mailbox_timeout_count++;
255                 break;
256
257         default:
258                 DEBUG2(printk("scsi%ld: %s: **** FAILED, cmd = %08X, "
259                               "sts = %08X ****\n", ha->host_no, __func__,
260                               mbx_cmd[0], mbx_sts[0]));
261                 break;
262         }
263         spin_unlock_irqrestore(&ha->hardware_lock, flags);
264
265 mbox_exit:
266         mutex_lock(&ha->mbox_sem);
267         clear_bit(AF_MBOX_COMMAND, &ha->flags);
268         mutex_unlock(&ha->mbox_sem);
269         clear_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
270
271         return status;
272 }
273
274 void qla4xxx_mailbox_premature_completion(struct scsi_qla_host *ha)
275 {
276         set_bit(AF_FW_RECOVERY, &ha->flags);
277         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: set FW RECOVERY!\n",
278             ha->host_no, __func__);
279
280         if (test_bit(AF_MBOX_COMMAND, &ha->flags)) {
281                 if (test_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags)) {
282                         complete(&ha->mbx_intr_comp);
283                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Due to fw "
284                             "recovery, doing premature completion of "
285                             "mbx cmd\n", ha->host_no, __func__);
286
287                 } else {
288                         set_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
289                         ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Due to fw "
290                             "recovery, doing premature completion of "
291                             "polling mbx cmd\n", ha->host_no, __func__);
292                 }
293         }
294 }
295
296 static uint8_t
297 qla4xxx_set_ifcb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
298                  uint32_t *mbox_sts, dma_addr_t init_fw_cb_dma)
299 {
300         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
301         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
302
303         if (is_qla8022(ha))
304                 qla4_8xxx_wr_32(ha, ha->nx_db_wr_ptr, 0);
305
306         mbox_cmd[0] = MBOX_CMD_INITIALIZE_FIRMWARE;
307         mbox_cmd[1] = 0;
308         mbox_cmd[2] = LSDW(init_fw_cb_dma);
309         mbox_cmd[3] = MSDW(init_fw_cb_dma);
310         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
311         mbox_cmd[5] = (IFCB_VER_MAX << 8) | IFCB_VER_MIN;
312
313         if (qla4xxx_mailbox_command(ha, 6, 6, mbox_cmd, mbox_sts) !=
314             QLA_SUCCESS) {
315                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: "
316                               "MBOX_CMD_INITIALIZE_FIRMWARE"
317                               " failed w/ status %04X\n",
318                               ha->host_no, __func__, mbox_sts[0]));
319                 return QLA_ERROR;
320         }
321         return QLA_SUCCESS;
322 }
323
324 static uint8_t
325 qla4xxx_get_ifcb(struct scsi_qla_host *ha, uint32_t *mbox_cmd,
326                  uint32_t *mbox_sts, dma_addr_t init_fw_cb_dma)
327 {
328         memset(mbox_cmd, 0, sizeof(mbox_cmd[0]) * MBOX_REG_COUNT);
329         memset(mbox_sts, 0, sizeof(mbox_sts[0]) * MBOX_REG_COUNT);
330         mbox_cmd[0] = MBOX_CMD_GET_INIT_FW_CTRL_BLOCK;
331         mbox_cmd[2] = LSDW(init_fw_cb_dma);
332         mbox_cmd[3] = MSDW(init_fw_cb_dma);
333         mbox_cmd[4] = sizeof(struct addr_ctrl_blk);
334
335         if (qla4xxx_mailbox_command(ha, 5, 5, mbox_cmd, mbox_sts) !=
336             QLA_SUCCESS) {
337                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: "
338                               "MBOX_CMD_GET_INIT_FW_CTRL_BLOCK"
339                               " failed w/ status %04X\n",
340                               ha->host_no, __func__, mbox_sts[0]));
341                 return QLA_ERROR;
342         }
343         return QLA_SUCCESS;
344 }
345
346 static void
347 qla4xxx_update_local_ip(struct scsi_qla_host *ha,
348                          struct addr_ctrl_blk  *init_fw_cb)
349 {
350         /* Save IPv4 Address Info */
351         memcpy(ha->ip_address, init_fw_cb->ipv4_addr,
352                 min(sizeof(ha->ip_address), sizeof(init_fw_cb->ipv4_addr)));
353         memcpy(ha->subnet_mask, init_fw_cb->ipv4_subnet,
354                 min(sizeof(ha->subnet_mask), sizeof(init_fw_cb->ipv4_subnet)));
355         memcpy(ha->gateway, init_fw_cb->ipv4_gw_addr,
356                 min(sizeof(ha->gateway), sizeof(init_fw_cb->ipv4_gw_addr)));
357
358         if (is_ipv6_enabled(ha)) {
359                 /* Save IPv6 Address */
360                 ha->ipv6_link_local_state = init_fw_cb->ipv6_lnk_lcl_addr_state;
361                 ha->ipv6_addr0_state = init_fw_cb->ipv6_addr0_state;
362                 ha->ipv6_addr1_state = init_fw_cb->ipv6_addr1_state;
363                 ha->ipv6_default_router_state = init_fw_cb->ipv6_dflt_rtr_state;
364                 ha->ipv6_link_local_addr.in6_u.u6_addr8[0] = 0xFE;
365                 ha->ipv6_link_local_addr.in6_u.u6_addr8[1] = 0x80;
366
367                 memcpy(&ha->ipv6_link_local_addr.in6_u.u6_addr8[8],
368                         init_fw_cb->ipv6_if_id,
369                         min(sizeof(ha->ipv6_link_local_addr)/2,
370                         sizeof(init_fw_cb->ipv6_if_id)));
371                 memcpy(&ha->ipv6_addr0, init_fw_cb->ipv6_addr0,
372                         min(sizeof(ha->ipv6_addr0),
373                         sizeof(init_fw_cb->ipv6_addr0)));
374                 memcpy(&ha->ipv6_addr1, init_fw_cb->ipv6_addr1,
375                         min(sizeof(ha->ipv6_addr1),
376                         sizeof(init_fw_cb->ipv6_addr1)));
377                 memcpy(&ha->ipv6_default_router_addr,
378                         init_fw_cb->ipv6_dflt_rtr_addr,
379                         min(sizeof(ha->ipv6_default_router_addr),
380                         sizeof(init_fw_cb->ipv6_dflt_rtr_addr)));
381         }
382 }
383
384 static uint8_t
385 qla4xxx_update_local_ifcb(struct scsi_qla_host *ha,
386                           uint32_t *mbox_cmd,
387                           uint32_t *mbox_sts,
388                           struct addr_ctrl_blk  *init_fw_cb,
389                           dma_addr_t init_fw_cb_dma)
390 {
391         if (qla4xxx_get_ifcb(ha, mbox_cmd, mbox_sts, init_fw_cb_dma)
392             != QLA_SUCCESS) {
393                 DEBUG2(printk(KERN_WARNING
394                               "scsi%ld: %s: Failed to get init_fw_ctrl_blk\n",
395                               ha->host_no, __func__));
396                 return QLA_ERROR;
397         }
398
399         DEBUG2(qla4xxx_dump_buffer(init_fw_cb, sizeof(struct addr_ctrl_blk)));
400
401         /* Save some info in adapter structure. */
402         ha->acb_version = init_fw_cb->acb_version;
403         ha->firmware_options = le16_to_cpu(init_fw_cb->fw_options);
404         ha->tcp_options = le16_to_cpu(init_fw_cb->ipv4_tcp_opts);
405         ha->ipv4_options = le16_to_cpu(init_fw_cb->ipv4_ip_opts);
406         ha->ipv4_addr_state = le16_to_cpu(init_fw_cb->ipv4_addr_state);
407         ha->heartbeat_interval = init_fw_cb->hb_interval;
408         memcpy(ha->name_string, init_fw_cb->iscsi_name,
409                 min(sizeof(ha->name_string),
410                 sizeof(init_fw_cb->iscsi_name)));
411         /*memcpy(ha->alias, init_fw_cb->Alias,
412                min(sizeof(ha->alias), sizeof(init_fw_cb->Alias)));*/
413
414         /* Save Command Line Paramater info */
415         ha->discovery_wait = ql4xdiscoverywait;
416
417         if (ha->acb_version == ACB_SUPPORTED) {
418                 ha->ipv6_options = init_fw_cb->ipv6_opts;
419                 ha->ipv6_addl_options = init_fw_cb->ipv6_addtl_opts;
420         }
421         qla4xxx_update_local_ip(ha, init_fw_cb);
422
423         return QLA_SUCCESS;
424 }
425
426 /**
427  * qla4xxx_initialize_fw_cb - initializes firmware control block.
428  * @ha: Pointer to host adapter structure.
429  **/
430 int qla4xxx_initialize_fw_cb(struct scsi_qla_host * ha)
431 {
432         struct addr_ctrl_blk *init_fw_cb;
433         dma_addr_t init_fw_cb_dma;
434         uint32_t mbox_cmd[MBOX_REG_COUNT];
435         uint32_t mbox_sts[MBOX_REG_COUNT];
436         int status = QLA_ERROR;
437
438         init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
439                                         sizeof(struct addr_ctrl_blk),
440                                         &init_fw_cb_dma, GFP_KERNEL);
441         if (init_fw_cb == NULL) {
442                 DEBUG2(printk("scsi%ld: %s: Unable to alloc init_cb\n",
443                               ha->host_no, __func__));
444                 goto exit_init_fw_cb_no_free;
445         }
446         memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
447
448         /* Get Initialize Firmware Control Block. */
449         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
450         memset(&mbox_sts, 0, sizeof(mbox_sts));
451
452         if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma) !=
453             QLA_SUCCESS) {
454                 dma_free_coherent(&ha->pdev->dev,
455                                   sizeof(struct addr_ctrl_blk),
456                                   init_fw_cb, init_fw_cb_dma);
457                 goto exit_init_fw_cb;
458         }
459
460         /* Initialize request and response queues. */
461         qla4xxx_init_rings(ha);
462
463         /* Fill in the request and response queue information. */
464         init_fw_cb->rqq_consumer_idx = cpu_to_le16(ha->request_out);
465         init_fw_cb->compq_producer_idx = cpu_to_le16(ha->response_in);
466         init_fw_cb->rqq_len = __constant_cpu_to_le16(REQUEST_QUEUE_DEPTH);
467         init_fw_cb->compq_len = __constant_cpu_to_le16(RESPONSE_QUEUE_DEPTH);
468         init_fw_cb->rqq_addr_lo = cpu_to_le32(LSDW(ha->request_dma));
469         init_fw_cb->rqq_addr_hi = cpu_to_le32(MSDW(ha->request_dma));
470         init_fw_cb->compq_addr_lo = cpu_to_le32(LSDW(ha->response_dma));
471         init_fw_cb->compq_addr_hi = cpu_to_le32(MSDW(ha->response_dma));
472         init_fw_cb->shdwreg_addr_lo = cpu_to_le32(LSDW(ha->shadow_regs_dma));
473         init_fw_cb->shdwreg_addr_hi = cpu_to_le32(MSDW(ha->shadow_regs_dma));
474
475         /* Set up required options. */
476         init_fw_cb->fw_options |=
477                 __constant_cpu_to_le16(FWOPT_SESSION_MODE |
478                                        FWOPT_INITIATOR_MODE);
479
480         if (is_qla8022(ha))
481                 init_fw_cb->fw_options |=
482                     __constant_cpu_to_le16(FWOPT_ENABLE_CRBDB);
483
484         init_fw_cb->fw_options &= __constant_cpu_to_le16(~FWOPT_TARGET_MODE);
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 /**
711  * qla4xxx_set_fwddb_entry - sets a ddb entry.
712  * @ha: Pointer to host adapter structure.
713  * @fw_ddb_index: Firmware's device database index
714  * @fw_ddb_entry: Pointer to firmware's ddb entry structure, or NULL.
715  *
716  * This routine initializes or updates the adapter's device database
717  * entry for the specified device. It also triggers a login for the
718  * specified device. Therefore, it may also be used as a secondary
719  * login routine when a NULL pointer is specified for the fw_ddb_entry.
720  **/
721 int qla4xxx_set_ddb_entry(struct scsi_qla_host * ha, uint16_t fw_ddb_index,
722                           dma_addr_t fw_ddb_entry_dma)
723 {
724         uint32_t mbox_cmd[MBOX_REG_COUNT];
725         uint32_t mbox_sts[MBOX_REG_COUNT];
726         int status;
727
728         /* Do not wait for completion. The firmware will send us an
729          * ASTS_DATABASE_CHANGED (0x8014) to notify us of the login status.
730          */
731         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
732         memset(&mbox_sts, 0, sizeof(mbox_sts));
733
734         mbox_cmd[0] = MBOX_CMD_SET_DATABASE_ENTRY;
735         mbox_cmd[1] = (uint32_t) fw_ddb_index;
736         mbox_cmd[2] = LSDW(fw_ddb_entry_dma);
737         mbox_cmd[3] = MSDW(fw_ddb_entry_dma);
738         mbox_cmd[4] = sizeof(struct dev_db_entry);
739
740         status = qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0],
741             &mbox_sts[0]);
742         DEBUG2(printk("scsi%ld: %s: status=%d mbx0=0x%x mbx4=0x%x\n",
743             ha->host_no, __func__, status, mbox_sts[0], mbox_sts[4]);)
744
745         return status;
746 }
747
748 /**
749  * qla4xxx_get_crash_record - retrieves crash record.
750  * @ha: Pointer to host adapter structure.
751  *
752  * This routine retrieves a crash record from the QLA4010 after an 8002h aen.
753  **/
754 void qla4xxx_get_crash_record(struct scsi_qla_host * ha)
755 {
756         uint32_t mbox_cmd[MBOX_REG_COUNT];
757         uint32_t mbox_sts[MBOX_REG_COUNT];
758         struct crash_record *crash_record = NULL;
759         dma_addr_t crash_record_dma = 0;
760         uint32_t crash_record_size = 0;
761
762         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
763         memset(&mbox_sts, 0, sizeof(mbox_cmd));
764
765         /* Get size of crash record. */
766         mbox_cmd[0] = MBOX_CMD_GET_CRASH_RECORD;
767
768         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
769             QLA_SUCCESS) {
770                 DEBUG2(printk("scsi%ld: %s: ERROR: Unable to retrieve size!\n",
771                               ha->host_no, __func__));
772                 goto exit_get_crash_record;
773         }
774         crash_record_size = mbox_sts[4];
775         if (crash_record_size == 0) {
776                 DEBUG2(printk("scsi%ld: %s: ERROR: Crash record size is 0!\n",
777                               ha->host_no, __func__));
778                 goto exit_get_crash_record;
779         }
780
781         /* Alloc Memory for Crash Record. */
782         crash_record = dma_alloc_coherent(&ha->pdev->dev, crash_record_size,
783                                           &crash_record_dma, GFP_KERNEL);
784         if (crash_record == NULL)
785                 goto exit_get_crash_record;
786
787         /* Get Crash Record. */
788         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
789         memset(&mbox_sts, 0, sizeof(mbox_cmd));
790
791         mbox_cmd[0] = MBOX_CMD_GET_CRASH_RECORD;
792         mbox_cmd[2] = LSDW(crash_record_dma);
793         mbox_cmd[3] = MSDW(crash_record_dma);
794         mbox_cmd[4] = crash_record_size;
795
796         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
797             QLA_SUCCESS)
798                 goto exit_get_crash_record;
799
800         /* Dump Crash Record. */
801
802 exit_get_crash_record:
803         if (crash_record)
804                 dma_free_coherent(&ha->pdev->dev, crash_record_size,
805                                   crash_record, crash_record_dma);
806 }
807
808 /**
809  * qla4xxx_get_conn_event_log - retrieves connection event log
810  * @ha: Pointer to host adapter structure.
811  **/
812 void qla4xxx_get_conn_event_log(struct scsi_qla_host * ha)
813 {
814         uint32_t mbox_cmd[MBOX_REG_COUNT];
815         uint32_t mbox_sts[MBOX_REG_COUNT];
816         struct conn_event_log_entry *event_log = NULL;
817         dma_addr_t event_log_dma = 0;
818         uint32_t event_log_size = 0;
819         uint32_t num_valid_entries;
820         uint32_t      oldest_entry = 0;
821         uint32_t        max_event_log_entries;
822         uint8_t         i;
823
824
825         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
826         memset(&mbox_sts, 0, sizeof(mbox_cmd));
827
828         /* Get size of crash record. */
829         mbox_cmd[0] = MBOX_CMD_GET_CONN_EVENT_LOG;
830
831         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
832             QLA_SUCCESS)
833                 goto exit_get_event_log;
834
835         event_log_size = mbox_sts[4];
836         if (event_log_size == 0)
837                 goto exit_get_event_log;
838
839         /* Alloc Memory for Crash Record. */
840         event_log = dma_alloc_coherent(&ha->pdev->dev, event_log_size,
841                                        &event_log_dma, GFP_KERNEL);
842         if (event_log == NULL)
843                 goto exit_get_event_log;
844
845         /* Get Crash Record. */
846         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
847         memset(&mbox_sts, 0, sizeof(mbox_cmd));
848
849         mbox_cmd[0] = MBOX_CMD_GET_CONN_EVENT_LOG;
850         mbox_cmd[2] = LSDW(event_log_dma);
851         mbox_cmd[3] = MSDW(event_log_dma);
852
853         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
854             QLA_SUCCESS) {
855                 DEBUG2(printk("scsi%ld: %s: ERROR: Unable to retrieve event "
856                               "log!\n", ha->host_no, __func__));
857                 goto exit_get_event_log;
858         }
859
860         /* Dump Event Log. */
861         num_valid_entries = mbox_sts[1];
862
863         max_event_log_entries = event_log_size /
864                 sizeof(struct conn_event_log_entry);
865
866         if (num_valid_entries > max_event_log_entries)
867                 oldest_entry = num_valid_entries % max_event_log_entries;
868
869         DEBUG3(printk("scsi%ld: Connection Event Log Dump (%d entries):\n",
870                       ha->host_no, num_valid_entries));
871
872         if (ql4xextended_error_logging == 3) {
873                 if (oldest_entry == 0) {
874                         /* Circular Buffer has not wrapped around */
875                         for (i=0; i < num_valid_entries; i++) {
876                                 qla4xxx_dump_buffer((uint8_t *)event_log+
877                                                     (i*sizeof(*event_log)),
878                                                     sizeof(*event_log));
879                         }
880                 }
881                 else {
882                         /* Circular Buffer has wrapped around -
883                          * display accordingly*/
884                         for (i=oldest_entry; i < max_event_log_entries; i++) {
885                                 qla4xxx_dump_buffer((uint8_t *)event_log+
886                                                     (i*sizeof(*event_log)),
887                                                     sizeof(*event_log));
888                         }
889                         for (i=0; i < oldest_entry; i++) {
890                                 qla4xxx_dump_buffer((uint8_t *)event_log+
891                                                     (i*sizeof(*event_log)),
892                                                     sizeof(*event_log));
893                         }
894                 }
895         }
896
897 exit_get_event_log:
898         if (event_log)
899                 dma_free_coherent(&ha->pdev->dev, event_log_size, event_log,
900                                   event_log_dma);
901 }
902
903 /**
904  * qla4xxx_abort_task - issues Abort Task
905  * @ha: Pointer to host adapter structure.
906  * @srb: Pointer to srb entry
907  *
908  * This routine performs a LUN RESET on the specified target/lun.
909  * The caller must ensure that the ddb_entry and lun_entry pointers
910  * are valid before calling this routine.
911  **/
912 int qla4xxx_abort_task(struct scsi_qla_host *ha, struct srb *srb)
913 {
914         uint32_t mbox_cmd[MBOX_REG_COUNT];
915         uint32_t mbox_sts[MBOX_REG_COUNT];
916         struct scsi_cmnd *cmd = srb->cmd;
917         int status = QLA_SUCCESS;
918         unsigned long flags = 0;
919         uint32_t index;
920
921         /*
922          * Send abort task command to ISP, so that the ISP will return
923          * request with ABORT status
924          */
925         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
926         memset(&mbox_sts, 0, sizeof(mbox_sts));
927
928         spin_lock_irqsave(&ha->hardware_lock, flags);
929         index = (unsigned long)(unsigned char *)cmd->host_scribble;
930         spin_unlock_irqrestore(&ha->hardware_lock, flags);
931
932         /* Firmware already posted completion on response queue */
933         if (index == MAX_SRBS)
934                 return status;
935
936         mbox_cmd[0] = MBOX_CMD_ABORT_TASK;
937         mbox_cmd[1] = srb->fw_ddb_index;
938         mbox_cmd[2] = index;
939         /* Immediate Command Enable */
940         mbox_cmd[5] = 0x01;
941
942         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0],
943             &mbox_sts[0]);
944         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE) {
945                 status = QLA_ERROR;
946
947                 DEBUG2(printk(KERN_WARNING "scsi%ld:%d:%d: abort task FAILED: "
948                     "mbx0=%04X, mb1=%04X, mb2=%04X, mb3=%04X, mb4=%04X\n",
949                     ha->host_no, cmd->device->id, cmd->device->lun, mbox_sts[0],
950                     mbox_sts[1], mbox_sts[2], mbox_sts[3], mbox_sts[4]));
951         }
952
953         return status;
954 }
955
956 /**
957  * qla4xxx_reset_lun - issues LUN Reset
958  * @ha: Pointer to host adapter structure.
959  * @ddb_entry: Pointer to device database entry
960  * @lun: lun number
961  *
962  * This routine performs a LUN RESET on the specified target/lun.
963  * The caller must ensure that the ddb_entry and lun_entry pointers
964  * are valid before calling this routine.
965  **/
966 int qla4xxx_reset_lun(struct scsi_qla_host * ha, struct ddb_entry * ddb_entry,
967                       int lun)
968 {
969         uint32_t mbox_cmd[MBOX_REG_COUNT];
970         uint32_t mbox_sts[MBOX_REG_COUNT];
971         int status = QLA_SUCCESS;
972
973         DEBUG2(printk("scsi%ld:%d:%d: lun reset issued\n", ha->host_no,
974                       ddb_entry->fw_ddb_index, lun));
975
976         /*
977          * Send lun reset command to ISP, so that the ISP will return all
978          * outstanding requests with RESET status
979          */
980         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
981         memset(&mbox_sts, 0, sizeof(mbox_sts));
982
983         mbox_cmd[0] = MBOX_CMD_LUN_RESET;
984         mbox_cmd[1] = ddb_entry->fw_ddb_index;
985         mbox_cmd[2] = lun << 8;
986         mbox_cmd[5] = 0x01;     /* Immediate Command Enable */
987
988         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0], &mbox_sts[0]);
989         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE &&
990             mbox_sts[0] != MBOX_STS_COMMAND_ERROR)
991                 status = QLA_ERROR;
992
993         return status;
994 }
995
996 /**
997  * qla4xxx_reset_target - issues target Reset
998  * @ha: Pointer to host adapter structure.
999  * @db_entry: Pointer to device database entry
1000  * @un_entry: Pointer to lun entry structure
1001  *
1002  * This routine performs a TARGET RESET on the specified target.
1003  * The caller must ensure that the ddb_entry pointers
1004  * are valid before calling this routine.
1005  **/
1006 int qla4xxx_reset_target(struct scsi_qla_host *ha,
1007                          struct ddb_entry *ddb_entry)
1008 {
1009         uint32_t mbox_cmd[MBOX_REG_COUNT];
1010         uint32_t mbox_sts[MBOX_REG_COUNT];
1011         int status = QLA_SUCCESS;
1012
1013         DEBUG2(printk("scsi%ld:%d: target reset issued\n", ha->host_no,
1014                       ddb_entry->fw_ddb_index));
1015
1016         /*
1017          * Send target reset command to ISP, so that the ISP will return all
1018          * outstanding requests with RESET status
1019          */
1020         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1021         memset(&mbox_sts, 0, sizeof(mbox_sts));
1022
1023         mbox_cmd[0] = MBOX_CMD_TARGET_WARM_RESET;
1024         mbox_cmd[1] = ddb_entry->fw_ddb_index;
1025         mbox_cmd[5] = 0x01;     /* Immediate Command Enable */
1026
1027         qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0],
1028                                 &mbox_sts[0]);
1029         if (mbox_sts[0] != MBOX_STS_COMMAND_COMPLETE &&
1030             mbox_sts[0] != MBOX_STS_COMMAND_ERROR)
1031                 status = QLA_ERROR;
1032
1033         return status;
1034 }
1035
1036 int qla4xxx_get_flash(struct scsi_qla_host * ha, dma_addr_t dma_addr,
1037                       uint32_t offset, uint32_t len)
1038 {
1039         uint32_t mbox_cmd[MBOX_REG_COUNT];
1040         uint32_t mbox_sts[MBOX_REG_COUNT];
1041
1042         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1043         memset(&mbox_sts, 0, sizeof(mbox_sts));
1044
1045         mbox_cmd[0] = MBOX_CMD_READ_FLASH;
1046         mbox_cmd[1] = LSDW(dma_addr);
1047         mbox_cmd[2] = MSDW(dma_addr);
1048         mbox_cmd[3] = offset;
1049         mbox_cmd[4] = len;
1050
1051         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 2, &mbox_cmd[0], &mbox_sts[0]) !=
1052             QLA_SUCCESS) {
1053                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_READ_FLASH, failed w/ "
1054                     "status %04X %04X, offset %08x, len %08x\n", ha->host_no,
1055                     __func__, mbox_sts[0], mbox_sts[1], offset, len));
1056                 return QLA_ERROR;
1057         }
1058         return QLA_SUCCESS;
1059 }
1060
1061 /**
1062  * qla4xxx_get_fw_version - gets firmware version
1063  * @ha: Pointer to host adapter structure.
1064  *
1065  * Retrieves the firmware version on HBA. In QLA4010, mailboxes 2 & 3 may
1066  * hold an address for data.  Make sure that we write 0 to those mailboxes,
1067  * if unused.
1068  **/
1069 int qla4xxx_get_fw_version(struct scsi_qla_host * ha)
1070 {
1071         uint32_t mbox_cmd[MBOX_REG_COUNT];
1072         uint32_t mbox_sts[MBOX_REG_COUNT];
1073
1074         /* Get firmware version. */
1075         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1076         memset(&mbox_sts, 0, sizeof(mbox_sts));
1077
1078         mbox_cmd[0] = MBOX_CMD_ABOUT_FW;
1079
1080         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 5, &mbox_cmd[0], &mbox_sts[0]) !=
1081             QLA_SUCCESS) {
1082                 DEBUG2(printk("scsi%ld: %s: MBOX_CMD_ABOUT_FW failed w/ "
1083                     "status %04X\n", ha->host_no, __func__, mbox_sts[0]));
1084                 return QLA_ERROR;
1085         }
1086
1087         /* Save firmware version information. */
1088         ha->firmware_version[0] = mbox_sts[1];
1089         ha->firmware_version[1] = mbox_sts[2];
1090         ha->patch_number = mbox_sts[3];
1091         ha->build_number = mbox_sts[4];
1092
1093         return QLA_SUCCESS;
1094 }
1095
1096 static int qla4xxx_get_default_ddb(struct scsi_qla_host *ha,
1097                                    dma_addr_t dma_addr)
1098 {
1099         uint32_t mbox_cmd[MBOX_REG_COUNT];
1100         uint32_t mbox_sts[MBOX_REG_COUNT];
1101
1102         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1103         memset(&mbox_sts, 0, sizeof(mbox_sts));
1104
1105         mbox_cmd[0] = MBOX_CMD_GET_DATABASE_ENTRY_DEFAULTS;
1106         mbox_cmd[2] = LSDW(dma_addr);
1107         mbox_cmd[3] = MSDW(dma_addr);
1108
1109         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 1, &mbox_cmd[0], &mbox_sts[0]) !=
1110             QLA_SUCCESS) {
1111                 DEBUG2(printk("scsi%ld: %s: failed status %04X\n",
1112                      ha->host_no, __func__, mbox_sts[0]));
1113                 return QLA_ERROR;
1114         }
1115         return QLA_SUCCESS;
1116 }
1117
1118 static int qla4xxx_req_ddb_entry(struct scsi_qla_host *ha, uint32_t *ddb_index)
1119 {
1120         uint32_t mbox_cmd[MBOX_REG_COUNT];
1121         uint32_t mbox_sts[MBOX_REG_COUNT];
1122
1123         memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1124         memset(&mbox_sts, 0, sizeof(mbox_sts));
1125
1126         mbox_cmd[0] = MBOX_CMD_REQUEST_DATABASE_ENTRY;
1127         mbox_cmd[1] = MAX_PRST_DEV_DB_ENTRIES;
1128
1129         if (qla4xxx_mailbox_command(ha, MBOX_REG_COUNT, 3, &mbox_cmd[0], &mbox_sts[0]) !=
1130             QLA_SUCCESS) {
1131                 if (mbox_sts[0] == MBOX_STS_COMMAND_ERROR) {
1132                         *ddb_index = mbox_sts[2];
1133                 } else {
1134                         DEBUG2(printk("scsi%ld: %s: failed status %04X\n",
1135                              ha->host_no, __func__, mbox_sts[0]));
1136                         return QLA_ERROR;
1137                 }
1138         } else {
1139                 *ddb_index = MAX_PRST_DEV_DB_ENTRIES;
1140         }
1141
1142         return QLA_SUCCESS;
1143 }
1144
1145
1146 int qla4xxx_send_tgts(struct scsi_qla_host *ha, char *ip, uint16_t port)
1147 {
1148         struct dev_db_entry *fw_ddb_entry;
1149         dma_addr_t fw_ddb_entry_dma;
1150         uint32_t ddb_index;
1151         int ret_val = QLA_SUCCESS;
1152
1153
1154         fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
1155                                           sizeof(*fw_ddb_entry),
1156                                           &fw_ddb_entry_dma, GFP_KERNEL);
1157         if (!fw_ddb_entry) {
1158                 DEBUG2(printk("scsi%ld: %s: Unable to allocate dma buffer.\n",
1159                               ha->host_no, __func__));
1160                 ret_val = QLA_ERROR;
1161                 goto exit_send_tgts_no_free;
1162         }
1163
1164         ret_val = qla4xxx_get_default_ddb(ha, fw_ddb_entry_dma);
1165         if (ret_val != QLA_SUCCESS)
1166                 goto exit_send_tgts;
1167
1168         ret_val = qla4xxx_req_ddb_entry(ha, &ddb_index);
1169         if (ret_val != QLA_SUCCESS)
1170                 goto exit_send_tgts;
1171
1172         memset(fw_ddb_entry->iscsi_alias, 0,
1173                sizeof(fw_ddb_entry->iscsi_alias));
1174
1175         memset(fw_ddb_entry->iscsi_name, 0,
1176                sizeof(fw_ddb_entry->iscsi_name));
1177
1178         memset(fw_ddb_entry->ip_addr, 0, sizeof(fw_ddb_entry->ip_addr));
1179         memset(fw_ddb_entry->tgt_addr, 0,
1180                sizeof(fw_ddb_entry->tgt_addr));
1181
1182         fw_ddb_entry->options = (DDB_OPT_DISC_SESSION | DDB_OPT_TARGET);
1183         fw_ddb_entry->port = cpu_to_le16(ntohs(port));
1184
1185         fw_ddb_entry->ip_addr[0] = *ip;
1186         fw_ddb_entry->ip_addr[1] = *(ip + 1);
1187         fw_ddb_entry->ip_addr[2] = *(ip + 2);
1188         fw_ddb_entry->ip_addr[3] = *(ip + 3);
1189
1190         ret_val = qla4xxx_set_ddb_entry(ha, ddb_index, fw_ddb_entry_dma);
1191
1192 exit_send_tgts:
1193         dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1194                           fw_ddb_entry, fw_ddb_entry_dma);
1195 exit_send_tgts_no_free:
1196         return ret_val;
1197 }
1198