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