[SCSI] qla2xxx: Don't fallback to interrupt-polling during re-initialization with...
[linux-2.6] / drivers / scsi / qla2xxx / qla_mbx.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/delay.h>
10
11
12 /*
13  * qla2x00_mailbox_command
14  *      Issue mailbox command and waits for completion.
15  *
16  * Input:
17  *      ha = adapter block pointer.
18  *      mcp = driver internal mbx struct pointer.
19  *
20  * Output:
21  *      mb[MAX_MAILBOX_REGISTER_COUNT] = returned mailbox data.
22  *
23  * Returns:
24  *      0 : QLA_SUCCESS = cmd performed success
25  *      1 : QLA_FUNCTION_FAILED   (error encountered)
26  *      6 : QLA_FUNCTION_TIMEOUT (timeout condition encountered)
27  *
28  * Context:
29  *      Kernel context.
30  */
31 static int
32 qla2x00_mailbox_command(scsi_qla_host_t *vha, mbx_cmd_t *mcp)
33 {
34         int             rval;
35         unsigned long    flags = 0;
36         device_reg_t __iomem *reg;
37         uint8_t         abort_active;
38         uint8_t         io_lock_on;
39         uint16_t        command;
40         uint16_t        *iptr;
41         uint16_t __iomem *optr;
42         uint32_t        cnt;
43         uint32_t        mboxes;
44         unsigned long   wait_time;
45         struct qla_hw_data *ha = vha->hw;
46         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
47
48         reg = ha->iobase;
49         io_lock_on = base_vha->flags.init_done;
50
51         rval = QLA_SUCCESS;
52         abort_active = test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
53
54         DEBUG11(printk("%s(%ld): entered.\n", __func__, base_vha->host_no));
55
56         /*
57          * Wait for active mailbox commands to finish by waiting at most tov
58          * seconds. This is to serialize actual issuing of mailbox cmds during
59          * non ISP abort time.
60          */
61         if (!abort_active) {
62                 if (!wait_for_completion_timeout(&ha->mbx_cmd_comp,
63                     mcp->tov * HZ)) {
64                         /* Timeout occurred. Return error. */
65                         DEBUG2_3_11(printk("%s(%ld): cmd access timeout. "
66                             "Exiting.\n", __func__, base_vha->host_no));
67                         return QLA_FUNCTION_TIMEOUT;
68                 }
69         }
70
71         ha->flags.mbox_busy = 1;
72         /* Save mailbox command for debug */
73         ha->mcp = mcp;
74
75         DEBUG11(printk("scsi(%ld): prepare to issue mbox cmd=0x%x.\n",
76             base_vha->host_no, mcp->mb[0]));
77
78         spin_lock_irqsave(&ha->hardware_lock, flags);
79
80         /* Load mailbox registers. */
81         if (IS_FWI2_CAPABLE(ha))
82                 optr = (uint16_t __iomem *)&reg->isp24.mailbox0;
83         else
84                 optr = (uint16_t __iomem *)MAILBOX_REG(ha, &reg->isp, 0);
85
86         iptr = mcp->mb;
87         command = mcp->mb[0];
88         mboxes = mcp->out_mb;
89
90         for (cnt = 0; cnt < ha->mbx_count; cnt++) {
91                 if (IS_QLA2200(ha) && cnt == 8)
92                         optr =
93                             (uint16_t __iomem *)MAILBOX_REG(ha, &reg->isp, 8);
94                 if (mboxes & BIT_0)
95                         WRT_REG_WORD(optr, *iptr);
96
97                 mboxes >>= 1;
98                 optr++;
99                 iptr++;
100         }
101
102 #if defined(QL_DEBUG_LEVEL_1)
103         printk("%s(%ld): Loaded MBX registers (displayed in bytes) = \n",
104             __func__, base_vha->host_no);
105         qla2x00_dump_buffer((uint8_t *)mcp->mb, 16);
106         printk("\n");
107         qla2x00_dump_buffer(((uint8_t *)mcp->mb + 0x10), 16);
108         printk("\n");
109         qla2x00_dump_buffer(((uint8_t *)mcp->mb + 0x20), 8);
110         printk("\n");
111         printk("%s(%ld): I/O address = %p.\n", __func__, base_vha->host_no,
112                 optr);
113         qla2x00_dump_regs(base_vha);
114 #endif
115
116         /* Issue set host interrupt command to send cmd out. */
117         ha->flags.mbox_int = 0;
118         clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
119
120         /* Unlock mbx registers and wait for interrupt */
121         DEBUG11(printk("%s(%ld): going to unlock irq & waiting for interrupt. "
122             "jiffies=%lx.\n", __func__, base_vha->host_no, jiffies));
123
124         /* Wait for mbx cmd completion until timeout */
125
126         if ((!abort_active && io_lock_on) || IS_NOPOLLING_TYPE(ha)) {
127                 set_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
128
129                 if (IS_FWI2_CAPABLE(ha))
130                         WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
131                 else
132                         WRT_REG_WORD(&reg->isp.hccr, HCCR_SET_HOST_INT);
133                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
134
135                 wait_for_completion_timeout(&ha->mbx_intr_comp, mcp->tov * HZ);
136
137                 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
138
139         } else {
140                 DEBUG3_11(printk("%s(%ld): cmd=%x POLLING MODE.\n", __func__,
141                     base_vha->host_no, command));
142
143                 if (IS_FWI2_CAPABLE(ha))
144                         WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
145                 else
146                         WRT_REG_WORD(&reg->isp.hccr, HCCR_SET_HOST_INT);
147                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
148
149                 wait_time = jiffies + mcp->tov * HZ; /* wait at most tov secs */
150                 while (!ha->flags.mbox_int) {
151                         if (time_after(jiffies, wait_time))
152                                 break;
153
154                         /* Check for pending interrupts. */
155                         qla2x00_poll(ha->rsp_q_map[0]);
156
157                         if (command != MBC_LOAD_RISC_RAM_EXTENDED &&
158                             !ha->flags.mbox_int)
159                                 msleep(10);
160                 } /* while */
161         }
162
163         /* Check whether we timed out */
164         if (ha->flags.mbox_int) {
165                 uint16_t *iptr2;
166
167                 DEBUG3_11(printk("%s(%ld): cmd %x completed.\n", __func__,
168                     base_vha->host_no, command));
169
170                 /* Got interrupt. Clear the flag. */
171                 ha->flags.mbox_int = 0;
172                 clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
173
174                 if (ha->mailbox_out[0] != MBS_COMMAND_COMPLETE)
175                         rval = QLA_FUNCTION_FAILED;
176
177                 /* Load return mailbox registers. */
178                 iptr2 = mcp->mb;
179                 iptr = (uint16_t *)&ha->mailbox_out[0];
180                 mboxes = mcp->in_mb;
181                 for (cnt = 0; cnt < ha->mbx_count; cnt++) {
182                         if (mboxes & BIT_0)
183                                 *iptr2 = *iptr;
184
185                         mboxes >>= 1;
186                         iptr2++;
187                         iptr++;
188                 }
189         } else {
190
191 #if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3) || \
192                 defined(QL_DEBUG_LEVEL_11)
193                 uint16_t mb0;
194                 uint32_t ictrl;
195
196                 if (IS_FWI2_CAPABLE(ha)) {
197                         mb0 = RD_REG_WORD(&reg->isp24.mailbox0);
198                         ictrl = RD_REG_DWORD(&reg->isp24.ictrl);
199                 } else {
200                         mb0 = RD_MAILBOX_REG(ha, &reg->isp, 0);
201                         ictrl = RD_REG_WORD(&reg->isp.ictrl);
202                 }
203                 printk("%s(%ld): **** MB Command Timeout for cmd %x ****\n",
204                     __func__, base_vha->host_no, command);
205                 printk("%s(%ld): icontrol=%x jiffies=%lx\n", __func__,
206                     base_vha->host_no, ictrl, jiffies);
207                 printk("%s(%ld): *** mailbox[0] = 0x%x ***\n", __func__,
208                     base_vha->host_no, mb0);
209                 qla2x00_dump_regs(base_vha);
210 #endif
211
212                 rval = QLA_FUNCTION_TIMEOUT;
213         }
214
215         ha->flags.mbox_busy = 0;
216
217         /* Clean up */
218         ha->mcp = NULL;
219
220         if ((abort_active || !io_lock_on) && !IS_NOPOLLING_TYPE(ha)) {
221                 DEBUG11(printk("%s(%ld): checking for additional resp "
222                     "interrupt.\n", __func__, base_vha->host_no));
223
224                 /* polling mode for non isp_abort commands. */
225                 qla2x00_poll(ha->rsp_q_map[0]);
226         }
227
228         if (rval == QLA_FUNCTION_TIMEOUT &&
229             mcp->mb[0] != MBC_GEN_SYSTEM_ERROR) {
230                 if (!io_lock_on || (mcp->flags & IOCTL_CMD)) {
231                         /* not in dpc. schedule it for dpc to take over. */
232                         DEBUG(printk("%s(%ld): timeout schedule "
233                         "isp_abort_needed.\n", __func__,
234                         base_vha->host_no));
235                         DEBUG2_3_11(printk("%s(%ld): timeout schedule "
236                         "isp_abort_needed.\n", __func__,
237                         base_vha->host_no));
238                         qla_printk(KERN_WARNING, ha,
239                             "Mailbox command timeout occurred. Scheduling ISP "
240                             "abort.\n");
241                         set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
242                         qla2xxx_wake_dpc(vha);
243                 } else if (!abort_active) {
244                         /* call abort directly since we are in the DPC thread */
245                         DEBUG(printk("%s(%ld): timeout calling abort_isp\n",
246                             __func__, base_vha->host_no));
247                         DEBUG2_3_11(printk("%s(%ld): timeout calling "
248                             "abort_isp\n", __func__, base_vha->host_no));
249                         qla_printk(KERN_WARNING, ha,
250                             "Mailbox command timeout occurred. Issuing ISP "
251                             "abort.\n");
252
253                         set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
254                         clear_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
255                         if (qla2x00_abort_isp(base_vha)) {
256                                 /* Failed. retry later. */
257                                 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
258                         }
259                         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
260                         DEBUG(printk("%s(%ld): finished abort_isp\n", __func__,
261                             base_vha->host_no));
262                         DEBUG2_3_11(printk("%s(%ld): finished abort_isp\n",
263                             __func__, base_vha->host_no));
264                 }
265         }
266
267         /* Allow next mbx cmd to come in. */
268         if (!abort_active)
269                 complete(&ha->mbx_cmd_comp);
270
271         if (rval) {
272                 DEBUG2_3_11(printk("%s(%ld): **** FAILED. mbx0=%x, mbx1=%x, "
273                     "mbx2=%x, cmd=%x ****\n", __func__, base_vha->host_no,
274                     mcp->mb[0], mcp->mb[1], mcp->mb[2], command));
275         } else {
276                 DEBUG11(printk("%s(%ld): done.\n", __func__,
277                 base_vha->host_no));
278         }
279
280         return rval;
281 }
282
283 int
284 qla2x00_load_ram(scsi_qla_host_t *vha, dma_addr_t req_dma, uint32_t risc_addr,
285     uint32_t risc_code_size)
286 {
287         int rval;
288         struct qla_hw_data *ha = vha->hw;
289         mbx_cmd_t mc;
290         mbx_cmd_t *mcp = &mc;
291
292         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
293
294         if (MSW(risc_addr) || IS_FWI2_CAPABLE(ha)) {
295                 mcp->mb[0] = MBC_LOAD_RISC_RAM_EXTENDED;
296                 mcp->mb[8] = MSW(risc_addr);
297                 mcp->out_mb = MBX_8|MBX_0;
298         } else {
299                 mcp->mb[0] = MBC_LOAD_RISC_RAM;
300                 mcp->out_mb = MBX_0;
301         }
302         mcp->mb[1] = LSW(risc_addr);
303         mcp->mb[2] = MSW(req_dma);
304         mcp->mb[3] = LSW(req_dma);
305         mcp->mb[6] = MSW(MSD(req_dma));
306         mcp->mb[7] = LSW(MSD(req_dma));
307         mcp->out_mb |= MBX_7|MBX_6|MBX_3|MBX_2|MBX_1;
308         if (IS_FWI2_CAPABLE(ha)) {
309                 mcp->mb[4] = MSW(risc_code_size);
310                 mcp->mb[5] = LSW(risc_code_size);
311                 mcp->out_mb |= MBX_5|MBX_4;
312         } else {
313                 mcp->mb[4] = LSW(risc_code_size);
314                 mcp->out_mb |= MBX_4;
315         }
316
317         mcp->in_mb = MBX_0;
318         mcp->tov = MBX_TOV_SECONDS;
319         mcp->flags = 0;
320         rval = qla2x00_mailbox_command(vha, mcp);
321
322         if (rval != QLA_SUCCESS) {
323                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
324                     vha->host_no, rval, mcp->mb[0]));
325         } else {
326                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
327         }
328
329         return rval;
330 }
331
332 /*
333  * qla2x00_execute_fw
334  *     Start adapter firmware.
335  *
336  * Input:
337  *     ha = adapter block pointer.
338  *     TARGET_QUEUE_LOCK must be released.
339  *     ADAPTER_STATE_LOCK must be released.
340  *
341  * Returns:
342  *     qla2x00 local function return status code.
343  *
344  * Context:
345  *     Kernel context.
346  */
347 int
348 qla2x00_execute_fw(scsi_qla_host_t *vha, uint32_t risc_addr)
349 {
350         int rval;
351         struct qla_hw_data *ha = vha->hw;
352         mbx_cmd_t mc;
353         mbx_cmd_t *mcp = &mc;
354
355         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
356
357         mcp->mb[0] = MBC_EXECUTE_FIRMWARE;
358         mcp->out_mb = MBX_0;
359         mcp->in_mb = MBX_0;
360         if (IS_FWI2_CAPABLE(ha)) {
361                 mcp->mb[1] = MSW(risc_addr);
362                 mcp->mb[2] = LSW(risc_addr);
363                 mcp->mb[3] = 0;
364                 mcp->mb[4] = 0;
365                 mcp->out_mb |= MBX_4|MBX_3|MBX_2|MBX_1;
366                 mcp->in_mb |= MBX_1;
367         } else {
368                 mcp->mb[1] = LSW(risc_addr);
369                 mcp->out_mb |= MBX_1;
370                 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
371                         mcp->mb[2] = 0;
372                         mcp->out_mb |= MBX_2;
373                 }
374         }
375
376         mcp->tov = MBX_TOV_SECONDS;
377         mcp->flags = 0;
378         rval = qla2x00_mailbox_command(vha, mcp);
379
380         if (rval != QLA_SUCCESS) {
381                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
382                     vha->host_no, rval, mcp->mb[0]));
383         } else {
384                 if (IS_FWI2_CAPABLE(ha)) {
385                         DEBUG11(printk("%s(%ld): done exchanges=%x.\n",
386                             __func__, vha->host_no, mcp->mb[1]));
387                 } else {
388                         DEBUG11(printk("%s(%ld): done.\n", __func__,
389                             vha->host_no));
390                 }
391         }
392
393         return rval;
394 }
395
396 /*
397  * qla2x00_get_fw_version
398  *      Get firmware version.
399  *
400  * Input:
401  *      ha:             adapter state pointer.
402  *      major:          pointer for major number.
403  *      minor:          pointer for minor number.
404  *      subminor:       pointer for subminor number.
405  *
406  * Returns:
407  *      qla2x00 local function return status code.
408  *
409  * Context:
410  *      Kernel context.
411  */
412 void
413 qla2x00_get_fw_version(scsi_qla_host_t *vha, uint16_t *major, uint16_t *minor,
414     uint16_t *subminor, uint16_t *attributes, uint32_t *memory)
415 {
416         int             rval;
417         mbx_cmd_t       mc;
418         mbx_cmd_t       *mcp = &mc;
419
420         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
421
422         mcp->mb[0] = MBC_GET_FIRMWARE_VERSION;
423         mcp->out_mb = MBX_0;
424         mcp->in_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
425         mcp->flags = 0;
426         mcp->tov = MBX_TOV_SECONDS;
427         rval = qla2x00_mailbox_command(vha, mcp);
428
429         /* Return mailbox data. */
430         *major = mcp->mb[1];
431         *minor = mcp->mb[2];
432         *subminor = mcp->mb[3];
433         *attributes = mcp->mb[6];
434         if (IS_QLA2100(vha->hw) || IS_QLA2200(vha->hw))
435                 *memory = 0x1FFFF;                      /* Defaults to 128KB. */
436         else
437                 *memory = (mcp->mb[5] << 16) | mcp->mb[4];
438
439         if (rval != QLA_SUCCESS) {
440                 /*EMPTY*/
441                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
442                     vha->host_no, rval));
443         } else {
444                 /*EMPTY*/
445                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
446         }
447 }
448
449 /*
450  * qla2x00_get_fw_options
451  *      Set firmware options.
452  *
453  * Input:
454  *      ha = adapter block pointer.
455  *      fwopt = pointer for firmware options.
456  *
457  * Returns:
458  *      qla2x00 local function return status code.
459  *
460  * Context:
461  *      Kernel context.
462  */
463 int
464 qla2x00_get_fw_options(scsi_qla_host_t *vha, uint16_t *fwopts)
465 {
466         int rval;
467         mbx_cmd_t mc;
468         mbx_cmd_t *mcp = &mc;
469
470         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
471
472         mcp->mb[0] = MBC_GET_FIRMWARE_OPTION;
473         mcp->out_mb = MBX_0;
474         mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
475         mcp->tov = MBX_TOV_SECONDS;
476         mcp->flags = 0;
477         rval = qla2x00_mailbox_command(vha, mcp);
478
479         if (rval != QLA_SUCCESS) {
480                 /*EMPTY*/
481                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
482                     vha->host_no, rval));
483         } else {
484                 fwopts[0] = mcp->mb[0];
485                 fwopts[1] = mcp->mb[1];
486                 fwopts[2] = mcp->mb[2];
487                 fwopts[3] = mcp->mb[3];
488
489                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
490         }
491
492         return rval;
493 }
494
495
496 /*
497  * qla2x00_set_fw_options
498  *      Set firmware options.
499  *
500  * Input:
501  *      ha = adapter block pointer.
502  *      fwopt = pointer for firmware options.
503  *
504  * Returns:
505  *      qla2x00 local function return status code.
506  *
507  * Context:
508  *      Kernel context.
509  */
510 int
511 qla2x00_set_fw_options(scsi_qla_host_t *vha, uint16_t *fwopts)
512 {
513         int rval;
514         mbx_cmd_t mc;
515         mbx_cmd_t *mcp = &mc;
516
517         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
518
519         mcp->mb[0] = MBC_SET_FIRMWARE_OPTION;
520         mcp->mb[1] = fwopts[1];
521         mcp->mb[2] = fwopts[2];
522         mcp->mb[3] = fwopts[3];
523         mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
524         mcp->in_mb = MBX_0;
525         if (IS_FWI2_CAPABLE(vha->hw)) {
526                 mcp->in_mb |= MBX_1;
527         } else {
528                 mcp->mb[10] = fwopts[10];
529                 mcp->mb[11] = fwopts[11];
530                 mcp->mb[12] = 0;        /* Undocumented, but used */
531                 mcp->out_mb |= MBX_12|MBX_11|MBX_10;
532         }
533         mcp->tov = MBX_TOV_SECONDS;
534         mcp->flags = 0;
535         rval = qla2x00_mailbox_command(vha, mcp);
536
537         fwopts[0] = mcp->mb[0];
538
539         if (rval != QLA_SUCCESS) {
540                 /*EMPTY*/
541                 DEBUG2_3_11(printk("%s(%ld): failed=%x (%x/%x).\n", __func__,
542                     vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
543         } else {
544                 /*EMPTY*/
545                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
546         }
547
548         return rval;
549 }
550
551 /*
552  * qla2x00_mbx_reg_test
553  *      Mailbox register wrap test.
554  *
555  * Input:
556  *      ha = adapter block pointer.
557  *      TARGET_QUEUE_LOCK must be released.
558  *      ADAPTER_STATE_LOCK must be released.
559  *
560  * Returns:
561  *      qla2x00 local function return status code.
562  *
563  * Context:
564  *      Kernel context.
565  */
566 int
567 qla2x00_mbx_reg_test(scsi_qla_host_t *vha)
568 {
569         int rval;
570         mbx_cmd_t mc;
571         mbx_cmd_t *mcp = &mc;
572
573         DEBUG11(printk("qla2x00_mbx_reg_test(%ld): entered.\n", vha->host_no));
574
575         mcp->mb[0] = MBC_MAILBOX_REGISTER_TEST;
576         mcp->mb[1] = 0xAAAA;
577         mcp->mb[2] = 0x5555;
578         mcp->mb[3] = 0xAA55;
579         mcp->mb[4] = 0x55AA;
580         mcp->mb[5] = 0xA5A5;
581         mcp->mb[6] = 0x5A5A;
582         mcp->mb[7] = 0x2525;
583         mcp->out_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
584         mcp->in_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
585         mcp->tov = MBX_TOV_SECONDS;
586         mcp->flags = 0;
587         rval = qla2x00_mailbox_command(vha, mcp);
588
589         if (rval == QLA_SUCCESS) {
590                 if (mcp->mb[1] != 0xAAAA || mcp->mb[2] != 0x5555 ||
591                     mcp->mb[3] != 0xAA55 || mcp->mb[4] != 0x55AA)
592                         rval = QLA_FUNCTION_FAILED;
593                 if (mcp->mb[5] != 0xA5A5 || mcp->mb[6] != 0x5A5A ||
594                     mcp->mb[7] != 0x2525)
595                         rval = QLA_FUNCTION_FAILED;
596         }
597
598         if (rval != QLA_SUCCESS) {
599                 /*EMPTY*/
600                 DEBUG2_3_11(printk("qla2x00_mbx_reg_test(%ld): failed=%x.\n",
601                     vha->host_no, rval));
602         } else {
603                 /*EMPTY*/
604                 DEBUG11(printk("qla2x00_mbx_reg_test(%ld): done.\n",
605                     vha->host_no));
606         }
607
608         return rval;
609 }
610
611 /*
612  * qla2x00_verify_checksum
613  *      Verify firmware checksum.
614  *
615  * Input:
616  *      ha = adapter block pointer.
617  *      TARGET_QUEUE_LOCK must be released.
618  *      ADAPTER_STATE_LOCK must be released.
619  *
620  * Returns:
621  *      qla2x00 local function return status code.
622  *
623  * Context:
624  *      Kernel context.
625  */
626 int
627 qla2x00_verify_checksum(scsi_qla_host_t *vha, uint32_t risc_addr)
628 {
629         int rval;
630         mbx_cmd_t mc;
631         mbx_cmd_t *mcp = &mc;
632
633         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
634
635         mcp->mb[0] = MBC_VERIFY_CHECKSUM;
636         mcp->out_mb = MBX_0;
637         mcp->in_mb = MBX_0;
638         if (IS_FWI2_CAPABLE(vha->hw)) {
639                 mcp->mb[1] = MSW(risc_addr);
640                 mcp->mb[2] = LSW(risc_addr);
641                 mcp->out_mb |= MBX_2|MBX_1;
642                 mcp->in_mb |= MBX_2|MBX_1;
643         } else {
644                 mcp->mb[1] = LSW(risc_addr);
645                 mcp->out_mb |= MBX_1;
646                 mcp->in_mb |= MBX_1;
647         }
648
649         mcp->tov = MBX_TOV_SECONDS;
650         mcp->flags = 0;
651         rval = qla2x00_mailbox_command(vha, mcp);
652
653         if (rval != QLA_SUCCESS) {
654                 DEBUG2_3_11(printk("%s(%ld): failed=%x chk sum=%x.\n", __func__,
655                     vha->host_no, rval, IS_FWI2_CAPABLE(vha->hw) ?
656                     (mcp->mb[2] << 16) | mcp->mb[1]: mcp->mb[1]));
657         } else {
658                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
659         }
660
661         return rval;
662 }
663
664 /*
665  * qla2x00_issue_iocb
666  *      Issue IOCB using mailbox command
667  *
668  * Input:
669  *      ha = adapter state pointer.
670  *      buffer = buffer pointer.
671  *      phys_addr = physical address of buffer.
672  *      size = size of buffer.
673  *      TARGET_QUEUE_LOCK must be released.
674  *      ADAPTER_STATE_LOCK must be released.
675  *
676  * Returns:
677  *      qla2x00 local function return status code.
678  *
679  * Context:
680  *      Kernel context.
681  */
682 static int
683 qla2x00_issue_iocb_timeout(scsi_qla_host_t *vha, void *buffer,
684     dma_addr_t phys_addr, size_t size, uint32_t tov)
685 {
686         int             rval;
687         mbx_cmd_t       mc;
688         mbx_cmd_t       *mcp = &mc;
689
690         mcp->mb[0] = MBC_IOCB_COMMAND_A64;
691         mcp->mb[1] = 0;
692         mcp->mb[2] = MSW(phys_addr);
693         mcp->mb[3] = LSW(phys_addr);
694         mcp->mb[6] = MSW(MSD(phys_addr));
695         mcp->mb[7] = LSW(MSD(phys_addr));
696         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
697         mcp->in_mb = MBX_2|MBX_0;
698         mcp->tov = tov;
699         mcp->flags = 0;
700         rval = qla2x00_mailbox_command(vha, mcp);
701
702         if (rval != QLA_SUCCESS) {
703                 /*EMPTY*/
704                 DEBUG(printk("qla2x00_issue_iocb(%ld): failed rval 0x%x\n",
705                     vha->host_no, rval));
706         } else {
707                 sts_entry_t *sts_entry = (sts_entry_t *) buffer;
708
709                 /* Mask reserved bits. */
710                 sts_entry->entry_status &=
711                     IS_FWI2_CAPABLE(vha->hw) ? RF_MASK_24XX : RF_MASK;
712         }
713
714         return rval;
715 }
716
717 int
718 qla2x00_issue_iocb(scsi_qla_host_t *vha, void *buffer, dma_addr_t phys_addr,
719     size_t size)
720 {
721         return qla2x00_issue_iocb_timeout(vha, buffer, phys_addr, size,
722             MBX_TOV_SECONDS);
723 }
724
725 /*
726  * qla2x00_abort_command
727  *      Abort command aborts a specified IOCB.
728  *
729  * Input:
730  *      ha = adapter block pointer.
731  *      sp = SB structure pointer.
732  *
733  * Returns:
734  *      qla2x00 local function return status code.
735  *
736  * Context:
737  *      Kernel context.
738  */
739 int
740 qla2x00_abort_command(scsi_qla_host_t *vha, srb_t *sp, struct req_que *req)
741 {
742         unsigned long   flags = 0;
743         fc_port_t       *fcport;
744         int             rval;
745         uint32_t        handle = 0;
746         mbx_cmd_t       mc;
747         mbx_cmd_t       *mcp = &mc;
748         struct qla_hw_data *ha = vha->hw;
749
750         DEBUG11(printk("qla2x00_abort_command(%ld): entered.\n", vha->host_no));
751
752         fcport = sp->fcport;
753
754         spin_lock_irqsave(&ha->hardware_lock, flags);
755         for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
756                 if (req->outstanding_cmds[handle] == sp)
757                         break;
758         }
759         spin_unlock_irqrestore(&ha->hardware_lock, flags);
760
761         if (handle == MAX_OUTSTANDING_COMMANDS) {
762                 /* command not found */
763                 return QLA_FUNCTION_FAILED;
764         }
765
766         mcp->mb[0] = MBC_ABORT_COMMAND;
767         if (HAS_EXTENDED_IDS(ha))
768                 mcp->mb[1] = fcport->loop_id;
769         else
770                 mcp->mb[1] = fcport->loop_id << 8;
771         mcp->mb[2] = (uint16_t)handle;
772         mcp->mb[3] = (uint16_t)(handle >> 16);
773         mcp->mb[6] = (uint16_t)sp->cmd->device->lun;
774         mcp->out_mb = MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
775         mcp->in_mb = MBX_0;
776         mcp->tov = MBX_TOV_SECONDS;
777         mcp->flags = 0;
778         rval = qla2x00_mailbox_command(vha, mcp);
779
780         if (rval != QLA_SUCCESS) {
781                 DEBUG2_3_11(printk("qla2x00_abort_command(%ld): failed=%x.\n",
782                     vha->host_no, rval));
783         } else {
784                 DEBUG11(printk("qla2x00_abort_command(%ld): done.\n",
785                     vha->host_no));
786         }
787
788         return rval;
789 }
790
791 int
792 qla2x00_abort_target(struct fc_port *fcport, unsigned int l)
793 {
794         int rval, rval2;
795         mbx_cmd_t  mc;
796         mbx_cmd_t  *mcp = &mc;
797         scsi_qla_host_t *vha;
798         struct req_que *req;
799         struct rsp_que *rsp;
800
801         DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->vha->host_no));
802
803         l = l;
804         vha = fcport->vha;
805         req = vha->hw->req_q_map[0];
806         rsp = vha->hw->rsp_q_map[0];
807         mcp->mb[0] = MBC_ABORT_TARGET;
808         mcp->out_mb = MBX_9|MBX_2|MBX_1|MBX_0;
809         if (HAS_EXTENDED_IDS(vha->hw)) {
810                 mcp->mb[1] = fcport->loop_id;
811                 mcp->mb[10] = 0;
812                 mcp->out_mb |= MBX_10;
813         } else {
814                 mcp->mb[1] = fcport->loop_id << 8;
815         }
816         mcp->mb[2] = vha->hw->loop_reset_delay;
817         mcp->mb[9] = vha->vp_idx;
818
819         mcp->in_mb = MBX_0;
820         mcp->tov = MBX_TOV_SECONDS;
821         mcp->flags = 0;
822         rval = qla2x00_mailbox_command(vha, mcp);
823         if (rval != QLA_SUCCESS) {
824                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
825                     vha->host_no, rval));
826         }
827
828         /* Issue marker IOCB. */
829         rval2 = qla2x00_marker(vha, req, rsp, fcport->loop_id, 0,
830                                                         MK_SYNC_ID);
831         if (rval2 != QLA_SUCCESS) {
832                 DEBUG2_3_11(printk("%s(%ld): failed to issue Marker IOCB "
833                     "(%x).\n", __func__, vha->host_no, rval2));
834         } else {
835                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
836         }
837
838         return rval;
839 }
840
841 int
842 qla2x00_lun_reset(struct fc_port *fcport, unsigned int l)
843 {
844         int rval, rval2;
845         mbx_cmd_t  mc;
846         mbx_cmd_t  *mcp = &mc;
847         scsi_qla_host_t *vha;
848         struct req_que *req;
849         struct rsp_que *rsp;
850
851         DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->vha->host_no));
852
853         vha = fcport->vha;
854         req = vha->hw->req_q_map[0];
855         rsp = vha->hw->rsp_q_map[0];
856         mcp->mb[0] = MBC_LUN_RESET;
857         mcp->out_mb = MBX_9|MBX_3|MBX_2|MBX_1|MBX_0;
858         if (HAS_EXTENDED_IDS(vha->hw))
859                 mcp->mb[1] = fcport->loop_id;
860         else
861                 mcp->mb[1] = fcport->loop_id << 8;
862         mcp->mb[2] = l;
863         mcp->mb[3] = 0;
864         mcp->mb[9] = vha->vp_idx;
865
866         mcp->in_mb = MBX_0;
867         mcp->tov = MBX_TOV_SECONDS;
868         mcp->flags = 0;
869         rval = qla2x00_mailbox_command(vha, mcp);
870         if (rval != QLA_SUCCESS) {
871                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
872                     vha->host_no, rval));
873         }
874
875         /* Issue marker IOCB. */
876         rval2 = qla2x00_marker(vha, req, rsp, fcport->loop_id, l,
877                                                                 MK_SYNC_ID_LUN);
878         if (rval2 != QLA_SUCCESS) {
879                 DEBUG2_3_11(printk("%s(%ld): failed to issue Marker IOCB "
880                     "(%x).\n", __func__, vha->host_no, rval2));
881         } else {
882                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
883         }
884
885         return rval;
886 }
887
888 /*
889  * qla2x00_get_adapter_id
890  *      Get adapter ID and topology.
891  *
892  * Input:
893  *      ha = adapter block pointer.
894  *      id = pointer for loop ID.
895  *      al_pa = pointer for AL_PA.
896  *      area = pointer for area.
897  *      domain = pointer for domain.
898  *      top = pointer for topology.
899  *      TARGET_QUEUE_LOCK must be released.
900  *      ADAPTER_STATE_LOCK must be released.
901  *
902  * Returns:
903  *      qla2x00 local function return status code.
904  *
905  * Context:
906  *      Kernel context.
907  */
908 int
909 qla2x00_get_adapter_id(scsi_qla_host_t *vha, uint16_t *id, uint8_t *al_pa,
910     uint8_t *area, uint8_t *domain, uint16_t *top, uint16_t *sw_cap)
911 {
912         int rval;
913         mbx_cmd_t mc;
914         mbx_cmd_t *mcp = &mc;
915
916         DEBUG11(printk("qla2x00_get_adapter_id(%ld): entered.\n",
917             vha->host_no));
918
919         mcp->mb[0] = MBC_GET_ADAPTER_LOOP_ID;
920         mcp->mb[9] = vha->vp_idx;
921         mcp->out_mb = MBX_9|MBX_0;
922         mcp->in_mb = MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
923         mcp->tov = MBX_TOV_SECONDS;
924         mcp->flags = 0;
925         rval = qla2x00_mailbox_command(vha, mcp);
926         if (mcp->mb[0] == MBS_COMMAND_ERROR)
927                 rval = QLA_COMMAND_ERROR;
928         else if (mcp->mb[0] == MBS_INVALID_COMMAND)
929                 rval = QLA_INVALID_COMMAND;
930
931         /* Return data. */
932         *id = mcp->mb[1];
933         *al_pa = LSB(mcp->mb[2]);
934         *area = MSB(mcp->mb[2]);
935         *domain = LSB(mcp->mb[3]);
936         *top = mcp->mb[6];
937         *sw_cap = mcp->mb[7];
938
939         if (rval != QLA_SUCCESS) {
940                 /*EMPTY*/
941                 DEBUG2_3_11(printk("qla2x00_get_adapter_id(%ld): failed=%x.\n",
942                     vha->host_no, rval));
943         } else {
944                 /*EMPTY*/
945                 DEBUG11(printk("qla2x00_get_adapter_id(%ld): done.\n",
946                     vha->host_no));
947         }
948
949         return rval;
950 }
951
952 /*
953  * qla2x00_get_retry_cnt
954  *      Get current firmware login retry count and delay.
955  *
956  * Input:
957  *      ha = adapter block pointer.
958  *      retry_cnt = pointer to login retry count.
959  *      tov = pointer to login timeout value.
960  *
961  * Returns:
962  *      qla2x00 local function return status code.
963  *
964  * Context:
965  *      Kernel context.
966  */
967 int
968 qla2x00_get_retry_cnt(scsi_qla_host_t *vha, uint8_t *retry_cnt, uint8_t *tov,
969     uint16_t *r_a_tov)
970 {
971         int rval;
972         uint16_t ratov;
973         mbx_cmd_t mc;
974         mbx_cmd_t *mcp = &mc;
975
976         DEBUG11(printk("qla2x00_get_retry_cnt(%ld): entered.\n",
977                         vha->host_no));
978
979         mcp->mb[0] = MBC_GET_RETRY_COUNT;
980         mcp->out_mb = MBX_0;
981         mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
982         mcp->tov = MBX_TOV_SECONDS;
983         mcp->flags = 0;
984         rval = qla2x00_mailbox_command(vha, mcp);
985
986         if (rval != QLA_SUCCESS) {
987                 /*EMPTY*/
988                 DEBUG2_3_11(printk("qla2x00_get_retry_cnt(%ld): failed = %x.\n",
989                     vha->host_no, mcp->mb[0]));
990         } else {
991                 /* Convert returned data and check our values. */
992                 *r_a_tov = mcp->mb[3] / 2;
993                 ratov = (mcp->mb[3]/2) / 10;  /* mb[3] value is in 100ms */
994                 if (mcp->mb[1] * ratov > (*retry_cnt) * (*tov)) {
995                         /* Update to the larger values */
996                         *retry_cnt = (uint8_t)mcp->mb[1];
997                         *tov = ratov;
998                 }
999
1000                 DEBUG11(printk("qla2x00_get_retry_cnt(%ld): done. mb3=%d "
1001                     "ratov=%d.\n", vha->host_no, mcp->mb[3], ratov));
1002         }
1003
1004         return rval;
1005 }
1006
1007 /*
1008  * qla2x00_init_firmware
1009  *      Initialize adapter firmware.
1010  *
1011  * Input:
1012  *      ha = adapter block pointer.
1013  *      dptr = Initialization control block pointer.
1014  *      size = size of initialization control block.
1015  *      TARGET_QUEUE_LOCK must be released.
1016  *      ADAPTER_STATE_LOCK must be released.
1017  *
1018  * Returns:
1019  *      qla2x00 local function return status code.
1020  *
1021  * Context:
1022  *      Kernel context.
1023  */
1024 int
1025 qla2x00_init_firmware(scsi_qla_host_t *vha, uint16_t size)
1026 {
1027         int rval;
1028         mbx_cmd_t mc;
1029         mbx_cmd_t *mcp = &mc;
1030         struct qla_hw_data *ha = vha->hw;
1031
1032         DEBUG11(printk("qla2x00_init_firmware(%ld): entered.\n",
1033             vha->host_no));
1034
1035         if (ha->flags.npiv_supported)
1036                 mcp->mb[0] = MBC_MID_INITIALIZE_FIRMWARE;
1037         else
1038                 mcp->mb[0] = MBC_INITIALIZE_FIRMWARE;
1039
1040         mcp->mb[2] = MSW(ha->init_cb_dma);
1041         mcp->mb[3] = LSW(ha->init_cb_dma);
1042         mcp->mb[4] = 0;
1043         mcp->mb[5] = 0;
1044         mcp->mb[6] = MSW(MSD(ha->init_cb_dma));
1045         mcp->mb[7] = LSW(MSD(ha->init_cb_dma));
1046         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1047         mcp->in_mb = MBX_5|MBX_4|MBX_0;
1048         mcp->buf_size = size;
1049         mcp->flags = MBX_DMA_OUT;
1050         mcp->tov = MBX_TOV_SECONDS;
1051         rval = qla2x00_mailbox_command(vha, mcp);
1052
1053         if (rval != QLA_SUCCESS) {
1054                 /*EMPTY*/
1055                 DEBUG2_3_11(printk("qla2x00_init_firmware(%ld): failed=%x "
1056                     "mb0=%x.\n",
1057                     vha->host_no, rval, mcp->mb[0]));
1058         } else {
1059                 /*EMPTY*/
1060                 DEBUG11(printk("qla2x00_init_firmware(%ld): done.\n",
1061                     vha->host_no));
1062         }
1063
1064         return rval;
1065 }
1066
1067 /*
1068  * qla2x00_get_port_database
1069  *      Issue normal/enhanced get port database mailbox command
1070  *      and copy device name as necessary.
1071  *
1072  * Input:
1073  *      ha = adapter state pointer.
1074  *      dev = structure pointer.
1075  *      opt = enhanced cmd option byte.
1076  *
1077  * Returns:
1078  *      qla2x00 local function return status code.
1079  *
1080  * Context:
1081  *      Kernel context.
1082  */
1083 int
1084 qla2x00_get_port_database(scsi_qla_host_t *vha, fc_port_t *fcport, uint8_t opt)
1085 {
1086         int rval;
1087         mbx_cmd_t mc;
1088         mbx_cmd_t *mcp = &mc;
1089         port_database_t *pd;
1090         struct port_database_24xx *pd24;
1091         dma_addr_t pd_dma;
1092         struct qla_hw_data *ha = vha->hw;
1093
1094         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1095
1096         pd24 = NULL;
1097         pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1098         if (pd  == NULL) {
1099                 DEBUG2_3(printk("%s(%ld): failed to allocate Port Database "
1100                     "structure.\n", __func__, vha->host_no));
1101                 return QLA_MEMORY_ALLOC_FAILED;
1102         }
1103         memset(pd, 0, max(PORT_DATABASE_SIZE, PORT_DATABASE_24XX_SIZE));
1104
1105         mcp->mb[0] = MBC_GET_PORT_DATABASE;
1106         if (opt != 0 && !IS_FWI2_CAPABLE(ha))
1107                 mcp->mb[0] = MBC_ENHANCED_GET_PORT_DATABASE;
1108         mcp->mb[2] = MSW(pd_dma);
1109         mcp->mb[3] = LSW(pd_dma);
1110         mcp->mb[6] = MSW(MSD(pd_dma));
1111         mcp->mb[7] = LSW(MSD(pd_dma));
1112         mcp->mb[9] = vha->vp_idx;
1113         mcp->out_mb = MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1114         mcp->in_mb = MBX_0;
1115         if (IS_FWI2_CAPABLE(ha)) {
1116                 mcp->mb[1] = fcport->loop_id;
1117                 mcp->mb[10] = opt;
1118                 mcp->out_mb |= MBX_10|MBX_1;
1119                 mcp->in_mb |= MBX_1;
1120         } else if (HAS_EXTENDED_IDS(ha)) {
1121                 mcp->mb[1] = fcport->loop_id;
1122                 mcp->mb[10] = opt;
1123                 mcp->out_mb |= MBX_10|MBX_1;
1124         } else {
1125                 mcp->mb[1] = fcport->loop_id << 8 | opt;
1126                 mcp->out_mb |= MBX_1;
1127         }
1128         mcp->buf_size = IS_FWI2_CAPABLE(ha) ?
1129             PORT_DATABASE_24XX_SIZE : PORT_DATABASE_SIZE;
1130         mcp->flags = MBX_DMA_IN;
1131         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1132         rval = qla2x00_mailbox_command(vha, mcp);
1133         if (rval != QLA_SUCCESS)
1134                 goto gpd_error_out;
1135
1136         if (IS_FWI2_CAPABLE(ha)) {
1137                 pd24 = (struct port_database_24xx *) pd;
1138
1139                 /* Check for logged in state. */
1140                 if (pd24->current_login_state != PDS_PRLI_COMPLETE &&
1141                     pd24->last_login_state != PDS_PRLI_COMPLETE) {
1142                         DEBUG2(printk("%s(%ld): Unable to verify "
1143                             "login-state (%x/%x) for loop_id %x\n",
1144                             __func__, vha->host_no,
1145                             pd24->current_login_state,
1146                             pd24->last_login_state, fcport->loop_id));
1147                         rval = QLA_FUNCTION_FAILED;
1148                         goto gpd_error_out;
1149                 }
1150
1151                 /* Names are little-endian. */
1152                 memcpy(fcport->node_name, pd24->node_name, WWN_SIZE);
1153                 memcpy(fcport->port_name, pd24->port_name, WWN_SIZE);
1154
1155                 /* Get port_id of device. */
1156                 fcport->d_id.b.domain = pd24->port_id[0];
1157                 fcport->d_id.b.area = pd24->port_id[1];
1158                 fcport->d_id.b.al_pa = pd24->port_id[2];
1159                 fcport->d_id.b.rsvd_1 = 0;
1160
1161                 /* If not target must be initiator or unknown type. */
1162                 if ((pd24->prli_svc_param_word_3[0] & BIT_4) == 0)
1163                         fcport->port_type = FCT_INITIATOR;
1164                 else
1165                         fcport->port_type = FCT_TARGET;
1166         } else {
1167                 /* Check for logged in state. */
1168                 if (pd->master_state != PD_STATE_PORT_LOGGED_IN &&
1169                     pd->slave_state != PD_STATE_PORT_LOGGED_IN) {
1170                         rval = QLA_FUNCTION_FAILED;
1171                         goto gpd_error_out;
1172                 }
1173
1174                 /* Names are little-endian. */
1175                 memcpy(fcport->node_name, pd->node_name, WWN_SIZE);
1176                 memcpy(fcport->port_name, pd->port_name, WWN_SIZE);
1177
1178                 /* Get port_id of device. */
1179                 fcport->d_id.b.domain = pd->port_id[0];
1180                 fcport->d_id.b.area = pd->port_id[3];
1181                 fcport->d_id.b.al_pa = pd->port_id[2];
1182                 fcport->d_id.b.rsvd_1 = 0;
1183
1184                 /* Check for device require authentication. */
1185                 pd->common_features & BIT_5 ? (fcport->flags |= FCF_AUTH_REQ) :
1186                     (fcport->flags &= ~FCF_AUTH_REQ);
1187
1188                 /* If not target must be initiator or unknown type. */
1189                 if ((pd->prli_svc_param_word_3[0] & BIT_4) == 0)
1190                         fcport->port_type = FCT_INITIATOR;
1191                 else
1192                         fcport->port_type = FCT_TARGET;
1193
1194                 /* Passback COS information. */
1195                 fcport->supported_classes = (pd->options & BIT_4) ?
1196                     FC_COS_CLASS2: FC_COS_CLASS3;
1197         }
1198
1199 gpd_error_out:
1200         dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1201
1202         if (rval != QLA_SUCCESS) {
1203                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
1204                     __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1205         } else {
1206                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
1207         }
1208
1209         return rval;
1210 }
1211
1212 /*
1213  * qla2x00_get_firmware_state
1214  *      Get adapter firmware state.
1215  *
1216  * Input:
1217  *      ha = adapter block pointer.
1218  *      dptr = pointer for firmware state.
1219  *      TARGET_QUEUE_LOCK must be released.
1220  *      ADAPTER_STATE_LOCK must be released.
1221  *
1222  * Returns:
1223  *      qla2x00 local function return status code.
1224  *
1225  * Context:
1226  *      Kernel context.
1227  */
1228 int
1229 qla2x00_get_firmware_state(scsi_qla_host_t *vha, uint16_t *states)
1230 {
1231         int rval;
1232         mbx_cmd_t mc;
1233         mbx_cmd_t *mcp = &mc;
1234
1235         DEBUG11(printk("qla2x00_get_firmware_state(%ld): entered.\n",
1236             vha->host_no));
1237
1238         mcp->mb[0] = MBC_GET_FIRMWARE_STATE;
1239         mcp->out_mb = MBX_0;
1240         mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1241         mcp->tov = MBX_TOV_SECONDS;
1242         mcp->flags = 0;
1243         rval = qla2x00_mailbox_command(vha, mcp);
1244
1245         /* Return firmware states. */
1246         states[0] = mcp->mb[1];
1247         states[1] = mcp->mb[2];
1248         states[2] = mcp->mb[3];
1249
1250         if (rval != QLA_SUCCESS) {
1251                 /*EMPTY*/
1252                 DEBUG2_3_11(printk("qla2x00_get_firmware_state(%ld): "
1253                     "failed=%x.\n", vha->host_no, rval));
1254         } else {
1255                 /*EMPTY*/
1256                 DEBUG11(printk("qla2x00_get_firmware_state(%ld): done.\n",
1257                     vha->host_no));
1258         }
1259
1260         return rval;
1261 }
1262
1263 /*
1264  * qla2x00_get_port_name
1265  *      Issue get port name mailbox command.
1266  *      Returned name is in big endian format.
1267  *
1268  * Input:
1269  *      ha = adapter block pointer.
1270  *      loop_id = loop ID of device.
1271  *      name = pointer for name.
1272  *      TARGET_QUEUE_LOCK must be released.
1273  *      ADAPTER_STATE_LOCK must be released.
1274  *
1275  * Returns:
1276  *      qla2x00 local function return status code.
1277  *
1278  * Context:
1279  *      Kernel context.
1280  */
1281 int
1282 qla2x00_get_port_name(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t *name,
1283     uint8_t opt)
1284 {
1285         int rval;
1286         mbx_cmd_t mc;
1287         mbx_cmd_t *mcp = &mc;
1288
1289         DEBUG11(printk("qla2x00_get_port_name(%ld): entered.\n",
1290             vha->host_no));
1291
1292         mcp->mb[0] = MBC_GET_PORT_NAME;
1293         mcp->mb[9] = vha->vp_idx;
1294         mcp->out_mb = MBX_9|MBX_1|MBX_0;
1295         if (HAS_EXTENDED_IDS(vha->hw)) {
1296                 mcp->mb[1] = loop_id;
1297                 mcp->mb[10] = opt;
1298                 mcp->out_mb |= MBX_10;
1299         } else {
1300                 mcp->mb[1] = loop_id << 8 | opt;
1301         }
1302
1303         mcp->in_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1304         mcp->tov = MBX_TOV_SECONDS;
1305         mcp->flags = 0;
1306         rval = qla2x00_mailbox_command(vha, mcp);
1307
1308         if (rval != QLA_SUCCESS) {
1309                 /*EMPTY*/
1310                 DEBUG2_3_11(printk("qla2x00_get_port_name(%ld): failed=%x.\n",
1311                     vha->host_no, rval));
1312         } else {
1313                 if (name != NULL) {
1314                         /* This function returns name in big endian. */
1315                         name[0] = MSB(mcp->mb[2]);
1316                         name[1] = LSB(mcp->mb[2]);
1317                         name[2] = MSB(mcp->mb[3]);
1318                         name[3] = LSB(mcp->mb[3]);
1319                         name[4] = MSB(mcp->mb[6]);
1320                         name[5] = LSB(mcp->mb[6]);
1321                         name[6] = MSB(mcp->mb[7]);
1322                         name[7] = LSB(mcp->mb[7]);
1323                 }
1324
1325                 DEBUG11(printk("qla2x00_get_port_name(%ld): done.\n",
1326                     vha->host_no));
1327         }
1328
1329         return rval;
1330 }
1331
1332 /*
1333  * qla2x00_lip_reset
1334  *      Issue LIP reset mailbox command.
1335  *
1336  * Input:
1337  *      ha = adapter block pointer.
1338  *      TARGET_QUEUE_LOCK must be released.
1339  *      ADAPTER_STATE_LOCK must be released.
1340  *
1341  * Returns:
1342  *      qla2x00 local function return status code.
1343  *
1344  * Context:
1345  *      Kernel context.
1346  */
1347 int
1348 qla2x00_lip_reset(scsi_qla_host_t *vha)
1349 {
1350         int rval;
1351         mbx_cmd_t mc;
1352         mbx_cmd_t *mcp = &mc;
1353
1354         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1355
1356         if (IS_FWI2_CAPABLE(vha->hw)) {
1357                 mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1358                 mcp->mb[1] = BIT_6;
1359                 mcp->mb[2] = 0;
1360                 mcp->mb[3] = vha->hw->loop_reset_delay;
1361                 mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1362         } else {
1363                 mcp->mb[0] = MBC_LIP_RESET;
1364                 mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1365                 if (HAS_EXTENDED_IDS(vha->hw)) {
1366                         mcp->mb[1] = 0x00ff;
1367                         mcp->mb[10] = 0;
1368                         mcp->out_mb |= MBX_10;
1369                 } else {
1370                         mcp->mb[1] = 0xff00;
1371                 }
1372                 mcp->mb[2] = vha->hw->loop_reset_delay;
1373                 mcp->mb[3] = 0;
1374         }
1375         mcp->in_mb = MBX_0;
1376         mcp->tov = MBX_TOV_SECONDS;
1377         mcp->flags = 0;
1378         rval = qla2x00_mailbox_command(vha, mcp);
1379
1380         if (rval != QLA_SUCCESS) {
1381                 /*EMPTY*/
1382                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n",
1383                     __func__, vha->host_no, rval));
1384         } else {
1385                 /*EMPTY*/
1386                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
1387         }
1388
1389         return rval;
1390 }
1391
1392 /*
1393  * qla2x00_send_sns
1394  *      Send SNS command.
1395  *
1396  * Input:
1397  *      ha = adapter block pointer.
1398  *      sns = pointer for command.
1399  *      cmd_size = command size.
1400  *      buf_size = response/command size.
1401  *      TARGET_QUEUE_LOCK must be released.
1402  *      ADAPTER_STATE_LOCK must be released.
1403  *
1404  * Returns:
1405  *      qla2x00 local function return status code.
1406  *
1407  * Context:
1408  *      Kernel context.
1409  */
1410 int
1411 qla2x00_send_sns(scsi_qla_host_t *vha, dma_addr_t sns_phys_address,
1412     uint16_t cmd_size, size_t buf_size)
1413 {
1414         int rval;
1415         mbx_cmd_t mc;
1416         mbx_cmd_t *mcp = &mc;
1417
1418         DEBUG11(printk("qla2x00_send_sns(%ld): entered.\n",
1419             vha->host_no));
1420
1421         DEBUG11(printk("qla2x00_send_sns: retry cnt=%d ratov=%d total "
1422                 "tov=%d.\n", vha->hw->retry_count, vha->hw->login_timeout,
1423                 mcp->tov));
1424
1425         mcp->mb[0] = MBC_SEND_SNS_COMMAND;
1426         mcp->mb[1] = cmd_size;
1427         mcp->mb[2] = MSW(sns_phys_address);
1428         mcp->mb[3] = LSW(sns_phys_address);
1429         mcp->mb[6] = MSW(MSD(sns_phys_address));
1430         mcp->mb[7] = LSW(MSD(sns_phys_address));
1431         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1432         mcp->in_mb = MBX_0|MBX_1;
1433         mcp->buf_size = buf_size;
1434         mcp->flags = MBX_DMA_OUT|MBX_DMA_IN;
1435         mcp->tov = (vha->hw->login_timeout * 2) + (vha->hw->login_timeout / 2);
1436         rval = qla2x00_mailbox_command(vha, mcp);
1437
1438         if (rval != QLA_SUCCESS) {
1439                 /*EMPTY*/
1440                 DEBUG(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1441                     "mb[1]=%x.\n", vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1442                 DEBUG2_3_11(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1443                     "mb[1]=%x.\n", vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1444         } else {
1445                 /*EMPTY*/
1446                 DEBUG11(printk("qla2x00_send_sns(%ld): done.\n", vha->host_no));
1447         }
1448
1449         return rval;
1450 }
1451
1452 int
1453 qla24xx_login_fabric(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
1454     uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1455 {
1456         int             rval;
1457
1458         struct logio_entry_24xx *lg;
1459         dma_addr_t      lg_dma;
1460         uint32_t        iop[2];
1461         struct qla_hw_data *ha = vha->hw;
1462
1463         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1464
1465         lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1466         if (lg == NULL) {
1467                 DEBUG2_3(printk("%s(%ld): failed to allocate Login IOCB.\n",
1468                     __func__, vha->host_no));
1469                 return QLA_MEMORY_ALLOC_FAILED;
1470         }
1471         memset(lg, 0, sizeof(struct logio_entry_24xx));
1472
1473         lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1474         lg->entry_count = 1;
1475         lg->nport_handle = cpu_to_le16(loop_id);
1476         lg->control_flags = __constant_cpu_to_le16(LCF_COMMAND_PLOGI);
1477         if (opt & BIT_0)
1478                 lg->control_flags |= __constant_cpu_to_le16(LCF_COND_PLOGI);
1479         if (opt & BIT_1)
1480                 lg->control_flags |= __constant_cpu_to_le16(LCF_SKIP_PRLI);
1481         lg->port_id[0] = al_pa;
1482         lg->port_id[1] = area;
1483         lg->port_id[2] = domain;
1484         lg->vp_index = vha->vp_idx;
1485         rval = qla2x00_issue_iocb(vha, lg, lg_dma, 0);
1486         if (rval != QLA_SUCCESS) {
1487                 DEBUG2_3_11(printk("%s(%ld): failed to issue Login IOCB "
1488                     "(%x).\n", __func__, vha->host_no, rval));
1489         } else if (lg->entry_status != 0) {
1490                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1491                     "-- error status (%x).\n", __func__, vha->host_no,
1492                     lg->entry_status));
1493                 rval = QLA_FUNCTION_FAILED;
1494         } else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1495                 iop[0] = le32_to_cpu(lg->io_parameter[0]);
1496                 iop[1] = le32_to_cpu(lg->io_parameter[1]);
1497
1498                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1499                     "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1500                     vha->host_no, le16_to_cpu(lg->comp_status), iop[0],
1501                     iop[1]));
1502
1503                 switch (iop[0]) {
1504                 case LSC_SCODE_PORTID_USED:
1505                         mb[0] = MBS_PORT_ID_USED;
1506                         mb[1] = LSW(iop[1]);
1507                         break;
1508                 case LSC_SCODE_NPORT_USED:
1509                         mb[0] = MBS_LOOP_ID_USED;
1510                         break;
1511                 case LSC_SCODE_NOLINK:
1512                 case LSC_SCODE_NOIOCB:
1513                 case LSC_SCODE_NOXCB:
1514                 case LSC_SCODE_CMD_FAILED:
1515                 case LSC_SCODE_NOFABRIC:
1516                 case LSC_SCODE_FW_NOT_READY:
1517                 case LSC_SCODE_NOT_LOGGED_IN:
1518                 case LSC_SCODE_NOPCB:
1519                 case LSC_SCODE_ELS_REJECT:
1520                 case LSC_SCODE_CMD_PARAM_ERR:
1521                 case LSC_SCODE_NONPORT:
1522                 case LSC_SCODE_LOGGED_IN:
1523                 case LSC_SCODE_NOFLOGI_ACC:
1524                 default:
1525                         mb[0] = MBS_COMMAND_ERROR;
1526                         break;
1527                 }
1528         } else {
1529                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
1530
1531                 iop[0] = le32_to_cpu(lg->io_parameter[0]);
1532
1533                 mb[0] = MBS_COMMAND_COMPLETE;
1534                 mb[1] = 0;
1535                 if (iop[0] & BIT_4) {
1536                         if (iop[0] & BIT_8)
1537                                 mb[1] |= BIT_1;
1538                 } else
1539                         mb[1] = BIT_0;
1540
1541                 /* Passback COS information. */
1542                 mb[10] = 0;
1543                 if (lg->io_parameter[7] || lg->io_parameter[8])
1544                         mb[10] |= BIT_0;        /* Class 2. */
1545                 if (lg->io_parameter[9] || lg->io_parameter[10])
1546                         mb[10] |= BIT_1;        /* Class 3. */
1547         }
1548
1549         dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1550
1551         return rval;
1552 }
1553
1554 /*
1555  * qla2x00_login_fabric
1556  *      Issue login fabric port mailbox command.
1557  *
1558  * Input:
1559  *      ha = adapter block pointer.
1560  *      loop_id = device loop ID.
1561  *      domain = device domain.
1562  *      area = device area.
1563  *      al_pa = device AL_PA.
1564  *      status = pointer for return status.
1565  *      opt = command options.
1566  *      TARGET_QUEUE_LOCK must be released.
1567  *      ADAPTER_STATE_LOCK must be released.
1568  *
1569  * Returns:
1570  *      qla2x00 local function return status code.
1571  *
1572  * Context:
1573  *      Kernel context.
1574  */
1575 int
1576 qla2x00_login_fabric(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
1577     uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1578 {
1579         int rval;
1580         mbx_cmd_t mc;
1581         mbx_cmd_t *mcp = &mc;
1582         struct qla_hw_data *ha = vha->hw;
1583
1584         DEBUG11(printk("qla2x00_login_fabric(%ld): entered.\n", vha->host_no));
1585
1586         mcp->mb[0] = MBC_LOGIN_FABRIC_PORT;
1587         mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1588         if (HAS_EXTENDED_IDS(ha)) {
1589                 mcp->mb[1] = loop_id;
1590                 mcp->mb[10] = opt;
1591                 mcp->out_mb |= MBX_10;
1592         } else {
1593                 mcp->mb[1] = (loop_id << 8) | opt;
1594         }
1595         mcp->mb[2] = domain;
1596         mcp->mb[3] = area << 8 | al_pa;
1597
1598         mcp->in_mb = MBX_7|MBX_6|MBX_2|MBX_1|MBX_0;
1599         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1600         mcp->flags = 0;
1601         rval = qla2x00_mailbox_command(vha, mcp);
1602
1603         /* Return mailbox statuses. */
1604         if (mb != NULL) {
1605                 mb[0] = mcp->mb[0];
1606                 mb[1] = mcp->mb[1];
1607                 mb[2] = mcp->mb[2];
1608                 mb[6] = mcp->mb[6];
1609                 mb[7] = mcp->mb[7];
1610                 /* COS retrieved from Get-Port-Database mailbox command. */
1611                 mb[10] = 0;
1612         }
1613
1614         if (rval != QLA_SUCCESS) {
1615                 /* RLU tmp code: need to change main mailbox_command function to
1616                  * return ok even when the mailbox completion value is not
1617                  * SUCCESS. The caller needs to be responsible to interpret
1618                  * the return values of this mailbox command if we're not
1619                  * to change too much of the existing code.
1620                  */
1621                 if (mcp->mb[0] == 0x4001 || mcp->mb[0] == 0x4002 ||
1622                     mcp->mb[0] == 0x4003 || mcp->mb[0] == 0x4005 ||
1623                     mcp->mb[0] == 0x4006)
1624                         rval = QLA_SUCCESS;
1625
1626                 /*EMPTY*/
1627                 DEBUG2_3_11(printk("qla2x00_login_fabric(%ld): failed=%x "
1628                     "mb[0]=%x mb[1]=%x mb[2]=%x.\n", vha->host_no, rval,
1629                     mcp->mb[0], mcp->mb[1], mcp->mb[2]));
1630         } else {
1631                 /*EMPTY*/
1632                 DEBUG11(printk("qla2x00_login_fabric(%ld): done.\n",
1633                     vha->host_no));
1634         }
1635
1636         return rval;
1637 }
1638
1639 /*
1640  * qla2x00_login_local_device
1641  *           Issue login loop port mailbox command.
1642  *
1643  * Input:
1644  *           ha = adapter block pointer.
1645  *           loop_id = device loop ID.
1646  *           opt = command options.
1647  *
1648  * Returns:
1649  *            Return status code.
1650  *
1651  * Context:
1652  *            Kernel context.
1653  *
1654  */
1655 int
1656 qla2x00_login_local_device(scsi_qla_host_t *vha, fc_port_t *fcport,
1657     uint16_t *mb_ret, uint8_t opt)
1658 {
1659         int rval;
1660         mbx_cmd_t mc;
1661         mbx_cmd_t *mcp = &mc;
1662         struct qla_hw_data *ha = vha->hw;
1663
1664         if (IS_FWI2_CAPABLE(ha))
1665                 return qla24xx_login_fabric(vha, fcport->loop_id,
1666                     fcport->d_id.b.domain, fcport->d_id.b.area,
1667                     fcport->d_id.b.al_pa, mb_ret, opt);
1668
1669         DEBUG3(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1670
1671         mcp->mb[0] = MBC_LOGIN_LOOP_PORT;
1672         if (HAS_EXTENDED_IDS(ha))
1673                 mcp->mb[1] = fcport->loop_id;
1674         else
1675                 mcp->mb[1] = fcport->loop_id << 8;
1676         mcp->mb[2] = opt;
1677         mcp->out_mb = MBX_2|MBX_1|MBX_0;
1678         mcp->in_mb = MBX_7|MBX_6|MBX_1|MBX_0;
1679         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1680         mcp->flags = 0;
1681         rval = qla2x00_mailbox_command(vha, mcp);
1682
1683         /* Return mailbox statuses. */
1684         if (mb_ret != NULL) {
1685                 mb_ret[0] = mcp->mb[0];
1686                 mb_ret[1] = mcp->mb[1];
1687                 mb_ret[6] = mcp->mb[6];
1688                 mb_ret[7] = mcp->mb[7];
1689         }
1690
1691         if (rval != QLA_SUCCESS) {
1692                 /* AV tmp code: need to change main mailbox_command function to
1693                  * return ok even when the mailbox completion value is not
1694                  * SUCCESS. The caller needs to be responsible to interpret
1695                  * the return values of this mailbox command if we're not
1696                  * to change too much of the existing code.
1697                  */
1698                 if (mcp->mb[0] == 0x4005 || mcp->mb[0] == 0x4006)
1699                         rval = QLA_SUCCESS;
1700
1701                 DEBUG(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1702                     "mb[6]=%x mb[7]=%x.\n", __func__, vha->host_no, rval,
1703                     mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]));
1704                 DEBUG2_3(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1705                     "mb[6]=%x mb[7]=%x.\n", __func__, vha->host_no, rval,
1706                     mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]));
1707         } else {
1708                 /*EMPTY*/
1709                 DEBUG3(printk("%s(%ld): done.\n", __func__, vha->host_no));
1710         }
1711
1712         return (rval);
1713 }
1714
1715 int
1716 qla24xx_fabric_logout(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
1717     uint8_t area, uint8_t al_pa)
1718 {
1719         int             rval;
1720         struct logio_entry_24xx *lg;
1721         dma_addr_t      lg_dma;
1722         struct qla_hw_data *ha = vha->hw;
1723
1724         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1725
1726         lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1727         if (lg == NULL) {
1728                 DEBUG2_3(printk("%s(%ld): failed to allocate Logout IOCB.\n",
1729                     __func__, vha->host_no));
1730                 return QLA_MEMORY_ALLOC_FAILED;
1731         }
1732         memset(lg, 0, sizeof(struct logio_entry_24xx));
1733
1734         lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1735         lg->entry_count = 1;
1736         lg->nport_handle = cpu_to_le16(loop_id);
1737         lg->control_flags =
1738             __constant_cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1739         lg->port_id[0] = al_pa;
1740         lg->port_id[1] = area;
1741         lg->port_id[2] = domain;
1742         lg->vp_index = vha->vp_idx;
1743
1744         rval = qla2x00_issue_iocb(vha, lg, lg_dma, 0);
1745         if (rval != QLA_SUCCESS) {
1746                 DEBUG2_3_11(printk("%s(%ld): failed to issue Logout IOCB "
1747                     "(%x).\n", __func__, vha->host_no, rval));
1748         } else if (lg->entry_status != 0) {
1749                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1750                     "-- error status (%x).\n", __func__, vha->host_no,
1751                     lg->entry_status));
1752                 rval = QLA_FUNCTION_FAILED;
1753         } else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1754                 DEBUG2_3_11(printk("%s(%ld %d): failed to complete IOCB "
1755                     "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1756                     vha->host_no, vha->vp_idx, le16_to_cpu(lg->comp_status),
1757                     le32_to_cpu(lg->io_parameter[0]),
1758                     le32_to_cpu(lg->io_parameter[1])));
1759         } else {
1760                 /*EMPTY*/
1761                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
1762         }
1763
1764         dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1765
1766         return rval;
1767 }
1768
1769 /*
1770  * qla2x00_fabric_logout
1771  *      Issue logout fabric port mailbox command.
1772  *
1773  * Input:
1774  *      ha = adapter block pointer.
1775  *      loop_id = device loop ID.
1776  *      TARGET_QUEUE_LOCK must be released.
1777  *      ADAPTER_STATE_LOCK must be released.
1778  *
1779  * Returns:
1780  *      qla2x00 local function return status code.
1781  *
1782  * Context:
1783  *      Kernel context.
1784  */
1785 int
1786 qla2x00_fabric_logout(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
1787     uint8_t area, uint8_t al_pa)
1788 {
1789         int rval;
1790         mbx_cmd_t mc;
1791         mbx_cmd_t *mcp = &mc;
1792
1793         DEBUG11(printk("qla2x00_fabric_logout(%ld): entered.\n",
1794             vha->host_no));
1795
1796         mcp->mb[0] = MBC_LOGOUT_FABRIC_PORT;
1797         mcp->out_mb = MBX_1|MBX_0;
1798         if (HAS_EXTENDED_IDS(vha->hw)) {
1799                 mcp->mb[1] = loop_id;
1800                 mcp->mb[10] = 0;
1801                 mcp->out_mb |= MBX_10;
1802         } else {
1803                 mcp->mb[1] = loop_id << 8;
1804         }
1805
1806         mcp->in_mb = MBX_1|MBX_0;
1807         mcp->tov = MBX_TOV_SECONDS;
1808         mcp->flags = 0;
1809         rval = qla2x00_mailbox_command(vha, mcp);
1810
1811         if (rval != QLA_SUCCESS) {
1812                 /*EMPTY*/
1813                 DEBUG2_3_11(printk("qla2x00_fabric_logout(%ld): failed=%x "
1814                     "mbx1=%x.\n", vha->host_no, rval, mcp->mb[1]));
1815         } else {
1816                 /*EMPTY*/
1817                 DEBUG11(printk("qla2x00_fabric_logout(%ld): done.\n",
1818                     vha->host_no));
1819         }
1820
1821         return rval;
1822 }
1823
1824 /*
1825  * qla2x00_full_login_lip
1826  *      Issue full login LIP mailbox command.
1827  *
1828  * Input:
1829  *      ha = adapter block pointer.
1830  *      TARGET_QUEUE_LOCK must be released.
1831  *      ADAPTER_STATE_LOCK must be released.
1832  *
1833  * Returns:
1834  *      qla2x00 local function return status code.
1835  *
1836  * Context:
1837  *      Kernel context.
1838  */
1839 int
1840 qla2x00_full_login_lip(scsi_qla_host_t *vha)
1841 {
1842         int rval;
1843         mbx_cmd_t mc;
1844         mbx_cmd_t *mcp = &mc;
1845
1846         DEBUG11(printk("qla2x00_full_login_lip(%ld): entered.\n",
1847             vha->host_no));
1848
1849         mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1850         mcp->mb[1] = IS_FWI2_CAPABLE(vha->hw) ? BIT_3 : 0;
1851         mcp->mb[2] = 0;
1852         mcp->mb[3] = 0;
1853         mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1854         mcp->in_mb = MBX_0;
1855         mcp->tov = MBX_TOV_SECONDS;
1856         mcp->flags = 0;
1857         rval = qla2x00_mailbox_command(vha, mcp);
1858
1859         if (rval != QLA_SUCCESS) {
1860                 /*EMPTY*/
1861                 DEBUG2_3_11(printk("qla2x00_full_login_lip(%ld): failed=%x.\n",
1862                     vha->host_no, rval));
1863         } else {
1864                 /*EMPTY*/
1865                 DEBUG11(printk("qla2x00_full_login_lip(%ld): done.\n",
1866                     vha->host_no));
1867         }
1868
1869         return rval;
1870 }
1871
1872 /*
1873  * qla2x00_get_id_list
1874  *
1875  * Input:
1876  *      ha = adapter block pointer.
1877  *
1878  * Returns:
1879  *      qla2x00 local function return status code.
1880  *
1881  * Context:
1882  *      Kernel context.
1883  */
1884 int
1885 qla2x00_get_id_list(scsi_qla_host_t *vha, void *id_list, dma_addr_t id_list_dma,
1886     uint16_t *entries)
1887 {
1888         int rval;
1889         mbx_cmd_t mc;
1890         mbx_cmd_t *mcp = &mc;
1891
1892         DEBUG11(printk("qla2x00_get_id_list(%ld): entered.\n",
1893             vha->host_no));
1894
1895         if (id_list == NULL)
1896                 return QLA_FUNCTION_FAILED;
1897
1898         mcp->mb[0] = MBC_GET_ID_LIST;
1899         mcp->out_mb = MBX_0;
1900         if (IS_FWI2_CAPABLE(vha->hw)) {
1901                 mcp->mb[2] = MSW(id_list_dma);
1902                 mcp->mb[3] = LSW(id_list_dma);
1903                 mcp->mb[6] = MSW(MSD(id_list_dma));
1904                 mcp->mb[7] = LSW(MSD(id_list_dma));
1905                 mcp->mb[8] = 0;
1906                 mcp->mb[9] = vha->vp_idx;
1907                 mcp->out_mb |= MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2;
1908         } else {
1909                 mcp->mb[1] = MSW(id_list_dma);
1910                 mcp->mb[2] = LSW(id_list_dma);
1911                 mcp->mb[3] = MSW(MSD(id_list_dma));
1912                 mcp->mb[6] = LSW(MSD(id_list_dma));
1913                 mcp->out_mb |= MBX_6|MBX_3|MBX_2|MBX_1;
1914         }
1915         mcp->in_mb = MBX_1|MBX_0;
1916         mcp->tov = MBX_TOV_SECONDS;
1917         mcp->flags = 0;
1918         rval = qla2x00_mailbox_command(vha, mcp);
1919
1920         if (rval != QLA_SUCCESS) {
1921                 /*EMPTY*/
1922                 DEBUG2_3_11(printk("qla2x00_get_id_list(%ld): failed=%x.\n",
1923                     vha->host_no, rval));
1924         } else {
1925                 *entries = mcp->mb[1];
1926                 DEBUG11(printk("qla2x00_get_id_list(%ld): done.\n",
1927                     vha->host_no));
1928         }
1929
1930         return rval;
1931 }
1932
1933 /*
1934  * qla2x00_get_resource_cnts
1935  *      Get current firmware resource counts.
1936  *
1937  * Input:
1938  *      ha = adapter block pointer.
1939  *
1940  * Returns:
1941  *      qla2x00 local function return status code.
1942  *
1943  * Context:
1944  *      Kernel context.
1945  */
1946 int
1947 qla2x00_get_resource_cnts(scsi_qla_host_t *vha, uint16_t *cur_xchg_cnt,
1948     uint16_t *orig_xchg_cnt, uint16_t *cur_iocb_cnt,
1949     uint16_t *orig_iocb_cnt, uint16_t *max_npiv_vports)
1950 {
1951         int rval;
1952         mbx_cmd_t mc;
1953         mbx_cmd_t *mcp = &mc;
1954
1955         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1956
1957         mcp->mb[0] = MBC_GET_RESOURCE_COUNTS;
1958         mcp->out_mb = MBX_0;
1959         mcp->in_mb = MBX_11|MBX_10|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1960         mcp->tov = MBX_TOV_SECONDS;
1961         mcp->flags = 0;
1962         rval = qla2x00_mailbox_command(vha, mcp);
1963
1964         if (rval != QLA_SUCCESS) {
1965                 /*EMPTY*/
1966                 DEBUG2_3_11(printk("%s(%ld): failed = %x.\n", __func__,
1967                     vha->host_no, mcp->mb[0]));
1968         } else {
1969                 DEBUG11(printk("%s(%ld): done. mb1=%x mb2=%x mb3=%x mb6=%x "
1970                     "mb7=%x mb10=%x mb11=%x.\n", __func__, vha->host_no,
1971                     mcp->mb[1], mcp->mb[2], mcp->mb[3], mcp->mb[6], mcp->mb[7],
1972                     mcp->mb[10], mcp->mb[11]));
1973
1974                 if (cur_xchg_cnt)
1975                         *cur_xchg_cnt = mcp->mb[3];
1976                 if (orig_xchg_cnt)
1977                         *orig_xchg_cnt = mcp->mb[6];
1978                 if (cur_iocb_cnt)
1979                         *cur_iocb_cnt = mcp->mb[7];
1980                 if (orig_iocb_cnt)
1981                         *orig_iocb_cnt = mcp->mb[10];
1982                 if (vha->hw->flags.npiv_supported && max_npiv_vports)
1983                         *max_npiv_vports = mcp->mb[11];
1984         }
1985
1986         return (rval);
1987 }
1988
1989 #if defined(QL_DEBUG_LEVEL_3)
1990 /*
1991  * qla2x00_get_fcal_position_map
1992  *      Get FCAL (LILP) position map using mailbox command
1993  *
1994  * Input:
1995  *      ha = adapter state pointer.
1996  *      pos_map = buffer pointer (can be NULL).
1997  *
1998  * Returns:
1999  *      qla2x00 local function return status code.
2000  *
2001  * Context:
2002  *      Kernel context.
2003  */
2004 int
2005 qla2x00_get_fcal_position_map(scsi_qla_host_t *vha, char *pos_map)
2006 {
2007         int rval;
2008         mbx_cmd_t mc;
2009         mbx_cmd_t *mcp = &mc;
2010         char *pmap;
2011         dma_addr_t pmap_dma;
2012         struct qla_hw_data *ha = vha->hw;
2013
2014         pmap = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &pmap_dma);
2015         if (pmap  == NULL) {
2016                 DEBUG2_3_11(printk("%s(%ld): **** Mem Alloc Failed ****",
2017                     __func__, vha->host_no));
2018                 return QLA_MEMORY_ALLOC_FAILED;
2019         }
2020         memset(pmap, 0, FCAL_MAP_SIZE);
2021
2022         mcp->mb[0] = MBC_GET_FC_AL_POSITION_MAP;
2023         mcp->mb[2] = MSW(pmap_dma);
2024         mcp->mb[3] = LSW(pmap_dma);
2025         mcp->mb[6] = MSW(MSD(pmap_dma));
2026         mcp->mb[7] = LSW(MSD(pmap_dma));
2027         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2028         mcp->in_mb = MBX_1|MBX_0;
2029         mcp->buf_size = FCAL_MAP_SIZE;
2030         mcp->flags = MBX_DMA_IN;
2031         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
2032         rval = qla2x00_mailbox_command(vha, mcp);
2033
2034         if (rval == QLA_SUCCESS) {
2035                 DEBUG11(printk("%s(%ld): (mb0=%x/mb1=%x) FC/AL Position Map "
2036                     "size (%x)\n", __func__, vha->host_no, mcp->mb[0],
2037                     mcp->mb[1], (unsigned)pmap[0]));
2038                 DEBUG11(qla2x00_dump_buffer(pmap, pmap[0] + 1));
2039
2040                 if (pos_map)
2041                         memcpy(pos_map, pmap, FCAL_MAP_SIZE);
2042         }
2043         dma_pool_free(ha->s_dma_pool, pmap, pmap_dma);
2044
2045         if (rval != QLA_SUCCESS) {
2046                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2047                     vha->host_no, rval));
2048         } else {
2049                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2050         }
2051
2052         return rval;
2053 }
2054 #endif
2055
2056 /*
2057  * qla2x00_get_link_status
2058  *
2059  * Input:
2060  *      ha = adapter block pointer.
2061  *      loop_id = device loop ID.
2062  *      ret_buf = pointer to link status return buffer.
2063  *
2064  * Returns:
2065  *      0 = success.
2066  *      BIT_0 = mem alloc error.
2067  *      BIT_1 = mailbox error.
2068  */
2069 int
2070 qla2x00_get_link_status(scsi_qla_host_t *vha, uint16_t loop_id,
2071     struct link_statistics *stats, dma_addr_t stats_dma)
2072 {
2073         int rval;
2074         mbx_cmd_t mc;
2075         mbx_cmd_t *mcp = &mc;
2076         uint32_t *siter, *diter, dwords;
2077         struct qla_hw_data *ha = vha->hw;
2078
2079         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2080
2081         mcp->mb[0] = MBC_GET_LINK_STATUS;
2082         mcp->mb[2] = MSW(stats_dma);
2083         mcp->mb[3] = LSW(stats_dma);
2084         mcp->mb[6] = MSW(MSD(stats_dma));
2085         mcp->mb[7] = LSW(MSD(stats_dma));
2086         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2087         mcp->in_mb = MBX_0;
2088         if (IS_FWI2_CAPABLE(ha)) {
2089                 mcp->mb[1] = loop_id;
2090                 mcp->mb[4] = 0;
2091                 mcp->mb[10] = 0;
2092                 mcp->out_mb |= MBX_10|MBX_4|MBX_1;
2093                 mcp->in_mb |= MBX_1;
2094         } else if (HAS_EXTENDED_IDS(ha)) {
2095                 mcp->mb[1] = loop_id;
2096                 mcp->mb[10] = 0;
2097                 mcp->out_mb |= MBX_10|MBX_1;
2098         } else {
2099                 mcp->mb[1] = loop_id << 8;
2100                 mcp->out_mb |= MBX_1;
2101         }
2102         mcp->tov = MBX_TOV_SECONDS;
2103         mcp->flags = IOCTL_CMD;
2104         rval = qla2x00_mailbox_command(vha, mcp);
2105
2106         if (rval == QLA_SUCCESS) {
2107                 if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2108                         DEBUG2_3_11(printk("%s(%ld): cmd failed. mbx0=%x.\n",
2109                             __func__, vha->host_no, mcp->mb[0]));
2110                         rval = QLA_FUNCTION_FAILED;
2111                 } else {
2112                         /* Copy over data -- firmware data is LE. */
2113                         dwords = offsetof(struct link_statistics, unused1) / 4;
2114                         siter = diter = &stats->link_fail_cnt;
2115                         while (dwords--)
2116                                 *diter++ = le32_to_cpu(*siter++);
2117                 }
2118         } else {
2119                 /* Failed. */
2120                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2121                     vha->host_no, rval));
2122         }
2123
2124         return rval;
2125 }
2126
2127 int
2128 qla24xx_get_isp_stats(scsi_qla_host_t *vha, struct link_statistics *stats,
2129     dma_addr_t stats_dma)
2130 {
2131         int rval;
2132         mbx_cmd_t mc;
2133         mbx_cmd_t *mcp = &mc;
2134         uint32_t *siter, *diter, dwords;
2135
2136         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2137
2138         mcp->mb[0] = MBC_GET_LINK_PRIV_STATS;
2139         mcp->mb[2] = MSW(stats_dma);
2140         mcp->mb[3] = LSW(stats_dma);
2141         mcp->mb[6] = MSW(MSD(stats_dma));
2142         mcp->mb[7] = LSW(MSD(stats_dma));
2143         mcp->mb[8] = sizeof(struct link_statistics) / 4;
2144         mcp->mb[9] = vha->vp_idx;
2145         mcp->mb[10] = 0;
2146         mcp->out_mb = MBX_10|MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2147         mcp->in_mb = MBX_2|MBX_1|MBX_0;
2148         mcp->tov = MBX_TOV_SECONDS;
2149         mcp->flags = IOCTL_CMD;
2150         rval = qla2x00_mailbox_command(vha, mcp);
2151
2152         if (rval == QLA_SUCCESS) {
2153                 if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2154                         DEBUG2_3_11(printk("%s(%ld): cmd failed. mbx0=%x.\n",
2155                             __func__, vha->host_no, mcp->mb[0]));
2156                         rval = QLA_FUNCTION_FAILED;
2157                 } else {
2158                         /* Copy over data -- firmware data is LE. */
2159                         dwords = sizeof(struct link_statistics) / 4;
2160                         siter = diter = &stats->link_fail_cnt;
2161                         while (dwords--)
2162                                 *diter++ = le32_to_cpu(*siter++);
2163                 }
2164         } else {
2165                 /* Failed. */
2166                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2167                     vha->host_no, rval));
2168         }
2169
2170         return rval;
2171 }
2172
2173 int
2174 qla24xx_abort_command(scsi_qla_host_t *vha, srb_t *sp, struct req_que *req)
2175 {
2176         int             rval;
2177         fc_port_t       *fcport;
2178         unsigned long   flags = 0;
2179
2180         struct abort_entry_24xx *abt;
2181         dma_addr_t      abt_dma;
2182         uint32_t        handle;
2183         struct qla_hw_data *ha = vha->hw;
2184
2185         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2186
2187         fcport = sp->fcport;
2188
2189         spin_lock_irqsave(&ha->hardware_lock, flags);
2190         for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
2191                 if (req->outstanding_cmds[handle] == sp)
2192                         break;
2193         }
2194         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2195         if (handle == MAX_OUTSTANDING_COMMANDS) {
2196                 /* Command not found. */
2197                 return QLA_FUNCTION_FAILED;
2198         }
2199
2200         abt = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &abt_dma);
2201         if (abt == NULL) {
2202                 DEBUG2_3(printk("%s(%ld): failed to allocate Abort IOCB.\n",
2203                     __func__, vha->host_no));
2204                 return QLA_MEMORY_ALLOC_FAILED;
2205         }
2206         memset(abt, 0, sizeof(struct abort_entry_24xx));
2207
2208         abt->entry_type = ABORT_IOCB_TYPE;
2209         abt->entry_count = 1;
2210         abt->nport_handle = cpu_to_le16(fcport->loop_id);
2211         abt->handle_to_abort = handle;
2212         abt->port_id[0] = fcport->d_id.b.al_pa;
2213         abt->port_id[1] = fcport->d_id.b.area;
2214         abt->port_id[2] = fcport->d_id.b.domain;
2215         abt->vp_index = fcport->vp_idx;
2216
2217         abt->req_que_no = cpu_to_le16(req->id);
2218
2219         rval = qla2x00_issue_iocb(vha, abt, abt_dma, 0);
2220         if (rval != QLA_SUCCESS) {
2221                 DEBUG2_3_11(printk("%s(%ld): failed to issue IOCB (%x).\n",
2222                     __func__, vha->host_no, rval));
2223         } else if (abt->entry_status != 0) {
2224                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2225                     "-- error status (%x).\n", __func__, vha->host_no,
2226                     abt->entry_status));
2227                 rval = QLA_FUNCTION_FAILED;
2228         } else if (abt->nport_handle != __constant_cpu_to_le16(0)) {
2229                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2230                     "-- completion status (%x).\n", __func__, vha->host_no,
2231                     le16_to_cpu(abt->nport_handle)));
2232                 rval = QLA_FUNCTION_FAILED;
2233         } else {
2234                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2235         }
2236
2237         dma_pool_free(ha->s_dma_pool, abt, abt_dma);
2238
2239         return rval;
2240 }
2241
2242 struct tsk_mgmt_cmd {
2243         union {
2244                 struct tsk_mgmt_entry tsk;
2245                 struct sts_entry_24xx sts;
2246         } p;
2247 };
2248
2249 static int
2250 __qla24xx_issue_tmf(char *name, uint32_t type, struct fc_port *fcport,
2251     unsigned int l)
2252 {
2253         int             rval, rval2;
2254         struct tsk_mgmt_cmd *tsk;
2255         dma_addr_t      tsk_dma;
2256         scsi_qla_host_t *vha;
2257         struct qla_hw_data *ha;
2258         struct req_que *req;
2259         struct rsp_que *rsp;
2260
2261         DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->vha->host_no));
2262
2263         vha = fcport->vha;
2264         ha = vha->hw;
2265         req = ha->req_q_map[0];
2266         rsp = ha->rsp_q_map[0];
2267         tsk = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &tsk_dma);
2268         if (tsk == NULL) {
2269                 DEBUG2_3(printk("%s(%ld): failed to allocate Task Management "
2270                     "IOCB.\n", __func__, vha->host_no));
2271                 return QLA_MEMORY_ALLOC_FAILED;
2272         }
2273         memset(tsk, 0, sizeof(struct tsk_mgmt_cmd));
2274
2275         tsk->p.tsk.entry_type = TSK_MGMT_IOCB_TYPE;
2276         tsk->p.tsk.entry_count = 1;
2277         tsk->p.tsk.nport_handle = cpu_to_le16(fcport->loop_id);
2278         tsk->p.tsk.timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2279         tsk->p.tsk.control_flags = cpu_to_le32(type);
2280         tsk->p.tsk.port_id[0] = fcport->d_id.b.al_pa;
2281         tsk->p.tsk.port_id[1] = fcport->d_id.b.area;
2282         tsk->p.tsk.port_id[2] = fcport->d_id.b.domain;
2283         tsk->p.tsk.vp_index = fcport->vp_idx;
2284         if (type == TCF_LUN_RESET) {
2285                 int_to_scsilun(l, &tsk->p.tsk.lun);
2286                 host_to_fcp_swap((uint8_t *)&tsk->p.tsk.lun,
2287                     sizeof(tsk->p.tsk.lun));
2288         }
2289
2290         rval = qla2x00_issue_iocb(vha, tsk, tsk_dma, 0);
2291         if (rval != QLA_SUCCESS) {
2292                 DEBUG2_3_11(printk("%s(%ld): failed to issue %s Reset IOCB "
2293                     "(%x).\n", __func__, vha->host_no, name, rval));
2294         } else if (tsk->p.sts.entry_status != 0) {
2295                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2296                     "-- error status (%x).\n", __func__, vha->host_no,
2297                     tsk->p.sts.entry_status));
2298                 rval = QLA_FUNCTION_FAILED;
2299         } else if (tsk->p.sts.comp_status !=
2300             __constant_cpu_to_le16(CS_COMPLETE)) {
2301                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2302                     "-- completion status (%x).\n", __func__,
2303                     vha->host_no, le16_to_cpu(tsk->p.sts.comp_status)));
2304                 rval = QLA_FUNCTION_FAILED;
2305         }
2306
2307         /* Issue marker IOCB. */
2308         rval2 = qla2x00_marker(vha, req, rsp, fcport->loop_id, l,
2309             type == TCF_LUN_RESET ? MK_SYNC_ID_LUN: MK_SYNC_ID);
2310         if (rval2 != QLA_SUCCESS) {
2311                 DEBUG2_3_11(printk("%s(%ld): failed to issue Marker IOCB "
2312                     "(%x).\n", __func__, vha->host_no, rval2));
2313         } else {
2314                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2315         }
2316
2317         dma_pool_free(ha->s_dma_pool, tsk, tsk_dma);
2318
2319         return rval;
2320 }
2321
2322 int
2323 qla24xx_abort_target(struct fc_port *fcport, unsigned int l)
2324 {
2325         return __qla24xx_issue_tmf("Target", TCF_TARGET_RESET, fcport, l);
2326 }
2327
2328 int
2329 qla24xx_lun_reset(struct fc_port *fcport, unsigned int l)
2330 {
2331         return __qla24xx_issue_tmf("Lun", TCF_LUN_RESET, fcport, l);
2332 }
2333
2334 int
2335 qla2x00_system_error(scsi_qla_host_t *vha)
2336 {
2337         int rval;
2338         mbx_cmd_t mc;
2339         mbx_cmd_t *mcp = &mc;
2340         struct qla_hw_data *ha = vha->hw;
2341
2342         if (!IS_QLA23XX(ha) && !IS_FWI2_CAPABLE(ha))
2343                 return QLA_FUNCTION_FAILED;
2344
2345         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2346
2347         mcp->mb[0] = MBC_GEN_SYSTEM_ERROR;
2348         mcp->out_mb = MBX_0;
2349         mcp->in_mb = MBX_0;
2350         mcp->tov = 5;
2351         mcp->flags = 0;
2352         rval = qla2x00_mailbox_command(vha, mcp);
2353
2354         if (rval != QLA_SUCCESS) {
2355                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2356                     vha->host_no, rval));
2357         } else {
2358                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2359         }
2360
2361         return rval;
2362 }
2363
2364 /**
2365  * qla2x00_set_serdes_params() -
2366  * @ha: HA context
2367  *
2368  * Returns
2369  */
2370 int
2371 qla2x00_set_serdes_params(scsi_qla_host_t *vha, uint16_t sw_em_1g,
2372     uint16_t sw_em_2g, uint16_t sw_em_4g)
2373 {
2374         int rval;
2375         mbx_cmd_t mc;
2376         mbx_cmd_t *mcp = &mc;
2377
2378         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2379
2380         mcp->mb[0] = MBC_SERDES_PARAMS;
2381         mcp->mb[1] = BIT_0;
2382         mcp->mb[2] = sw_em_1g | BIT_15;
2383         mcp->mb[3] = sw_em_2g | BIT_15;
2384         mcp->mb[4] = sw_em_4g | BIT_15;
2385         mcp->out_mb = MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2386         mcp->in_mb = MBX_0;
2387         mcp->tov = MBX_TOV_SECONDS;
2388         mcp->flags = 0;
2389         rval = qla2x00_mailbox_command(vha, mcp);
2390
2391         if (rval != QLA_SUCCESS) {
2392                 /*EMPTY*/
2393                 DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2394                     vha->host_no, rval, mcp->mb[0]));
2395         } else {
2396                 /*EMPTY*/
2397                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2398         }
2399
2400         return rval;
2401 }
2402
2403 int
2404 qla2x00_stop_firmware(scsi_qla_host_t *vha)
2405 {
2406         int rval;
2407         mbx_cmd_t mc;
2408         mbx_cmd_t *mcp = &mc;
2409
2410         if (!IS_FWI2_CAPABLE(vha->hw))
2411                 return QLA_FUNCTION_FAILED;
2412
2413         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2414
2415         mcp->mb[0] = MBC_STOP_FIRMWARE;
2416         mcp->out_mb = MBX_0;
2417         mcp->in_mb = MBX_0;
2418         mcp->tov = 5;
2419         mcp->flags = 0;
2420         rval = qla2x00_mailbox_command(vha, mcp);
2421
2422         if (rval != QLA_SUCCESS) {
2423                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2424                     vha->host_no, rval));
2425         } else {
2426                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2427         }
2428
2429         return rval;
2430 }
2431
2432 int
2433 qla2x00_enable_eft_trace(scsi_qla_host_t *vha, dma_addr_t eft_dma,
2434     uint16_t buffers)
2435 {
2436         int rval;
2437         mbx_cmd_t mc;
2438         mbx_cmd_t *mcp = &mc;
2439
2440         if (!IS_FWI2_CAPABLE(vha->hw))
2441                 return QLA_FUNCTION_FAILED;
2442
2443         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2444
2445         mcp->mb[0] = MBC_TRACE_CONTROL;
2446         mcp->mb[1] = TC_EFT_ENABLE;
2447         mcp->mb[2] = LSW(eft_dma);
2448         mcp->mb[3] = MSW(eft_dma);
2449         mcp->mb[4] = LSW(MSD(eft_dma));
2450         mcp->mb[5] = MSW(MSD(eft_dma));
2451         mcp->mb[6] = buffers;
2452         mcp->mb[7] = TC_AEN_DISABLE;
2453         mcp->out_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2454         mcp->in_mb = MBX_1|MBX_0;
2455         mcp->tov = MBX_TOV_SECONDS;
2456         mcp->flags = 0;
2457         rval = qla2x00_mailbox_command(vha, mcp);
2458         if (rval != QLA_SUCCESS) {
2459                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
2460                     __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
2461         } else {
2462                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2463         }
2464
2465         return rval;
2466 }
2467
2468 int
2469 qla2x00_disable_eft_trace(scsi_qla_host_t *vha)
2470 {
2471         int rval;
2472         mbx_cmd_t mc;
2473         mbx_cmd_t *mcp = &mc;
2474
2475         if (!IS_FWI2_CAPABLE(vha->hw))
2476                 return QLA_FUNCTION_FAILED;
2477
2478         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2479
2480         mcp->mb[0] = MBC_TRACE_CONTROL;
2481         mcp->mb[1] = TC_EFT_DISABLE;
2482         mcp->out_mb = MBX_1|MBX_0;
2483         mcp->in_mb = MBX_1|MBX_0;
2484         mcp->tov = MBX_TOV_SECONDS;
2485         mcp->flags = 0;
2486         rval = qla2x00_mailbox_command(vha, mcp);
2487         if (rval != QLA_SUCCESS) {
2488                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
2489                     __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
2490         } else {
2491                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2492         }
2493
2494         return rval;
2495 }
2496
2497 int
2498 qla2x00_enable_fce_trace(scsi_qla_host_t *vha, dma_addr_t fce_dma,
2499     uint16_t buffers, uint16_t *mb, uint32_t *dwords)
2500 {
2501         int rval;
2502         mbx_cmd_t mc;
2503         mbx_cmd_t *mcp = &mc;
2504
2505         if (!IS_QLA25XX(vha->hw))
2506                 return QLA_FUNCTION_FAILED;
2507
2508         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2509
2510         mcp->mb[0] = MBC_TRACE_CONTROL;
2511         mcp->mb[1] = TC_FCE_ENABLE;
2512         mcp->mb[2] = LSW(fce_dma);
2513         mcp->mb[3] = MSW(fce_dma);
2514         mcp->mb[4] = LSW(MSD(fce_dma));
2515         mcp->mb[5] = MSW(MSD(fce_dma));
2516         mcp->mb[6] = buffers;
2517         mcp->mb[7] = TC_AEN_DISABLE;
2518         mcp->mb[8] = 0;
2519         mcp->mb[9] = TC_FCE_DEFAULT_RX_SIZE;
2520         mcp->mb[10] = TC_FCE_DEFAULT_TX_SIZE;
2521         mcp->out_mb = MBX_10|MBX_9|MBX_8|MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|
2522             MBX_1|MBX_0;
2523         mcp->in_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2524         mcp->tov = MBX_TOV_SECONDS;
2525         mcp->flags = 0;
2526         rval = qla2x00_mailbox_command(vha, mcp);
2527         if (rval != QLA_SUCCESS) {
2528                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
2529                     __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
2530         } else {
2531                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2532
2533                 if (mb)
2534                         memcpy(mb, mcp->mb, 8 * sizeof(*mb));
2535                 if (dwords)
2536                         *dwords = buffers;
2537         }
2538
2539         return rval;
2540 }
2541
2542 int
2543 qla2x00_disable_fce_trace(scsi_qla_host_t *vha, uint64_t *wr, uint64_t *rd)
2544 {
2545         int rval;
2546         mbx_cmd_t mc;
2547         mbx_cmd_t *mcp = &mc;
2548
2549         if (!IS_FWI2_CAPABLE(vha->hw))
2550                 return QLA_FUNCTION_FAILED;
2551
2552         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2553
2554         mcp->mb[0] = MBC_TRACE_CONTROL;
2555         mcp->mb[1] = TC_FCE_DISABLE;
2556         mcp->mb[2] = TC_FCE_DISABLE_TRACE;
2557         mcp->out_mb = MBX_2|MBX_1|MBX_0;
2558         mcp->in_mb = MBX_9|MBX_8|MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|
2559             MBX_1|MBX_0;
2560         mcp->tov = MBX_TOV_SECONDS;
2561         mcp->flags = 0;
2562         rval = qla2x00_mailbox_command(vha, mcp);
2563         if (rval != QLA_SUCCESS) {
2564                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
2565                     __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
2566         } else {
2567                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2568
2569                 if (wr)
2570                         *wr = (uint64_t) mcp->mb[5] << 48 |
2571                             (uint64_t) mcp->mb[4] << 32 |
2572                             (uint64_t) mcp->mb[3] << 16 |
2573                             (uint64_t) mcp->mb[2];
2574                 if (rd)
2575                         *rd = (uint64_t) mcp->mb[9] << 48 |
2576                             (uint64_t) mcp->mb[8] << 32 |
2577                             (uint64_t) mcp->mb[7] << 16 |
2578                             (uint64_t) mcp->mb[6];
2579         }
2580
2581         return rval;
2582 }
2583
2584 int
2585 qla2x00_read_sfp(scsi_qla_host_t *vha, dma_addr_t sfp_dma, uint16_t addr,
2586     uint16_t off, uint16_t count)
2587 {
2588         int rval;
2589         mbx_cmd_t mc;
2590         mbx_cmd_t *mcp = &mc;
2591
2592         if (!IS_FWI2_CAPABLE(vha->hw))
2593                 return QLA_FUNCTION_FAILED;
2594
2595         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2596
2597         mcp->mb[0] = MBC_READ_SFP;
2598         mcp->mb[1] = addr;
2599         mcp->mb[2] = MSW(sfp_dma);
2600         mcp->mb[3] = LSW(sfp_dma);
2601         mcp->mb[6] = MSW(MSD(sfp_dma));
2602         mcp->mb[7] = LSW(MSD(sfp_dma));
2603         mcp->mb[8] = count;
2604         mcp->mb[9] = off;
2605         mcp->mb[10] = 0;
2606         mcp->out_mb = MBX_10|MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
2607         mcp->in_mb = MBX_0;
2608         mcp->tov = MBX_TOV_SECONDS;
2609         mcp->flags = 0;
2610         rval = qla2x00_mailbox_command(vha, mcp);
2611
2612         if (rval != QLA_SUCCESS) {
2613                 DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2614                     vha->host_no, rval, mcp->mb[0]));
2615         } else {
2616                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2617         }
2618
2619         return rval;
2620 }
2621
2622 int
2623 qla2x00_set_idma_speed(scsi_qla_host_t *vha, uint16_t loop_id,
2624     uint16_t port_speed, uint16_t *mb)
2625 {
2626         int rval;
2627         mbx_cmd_t mc;
2628         mbx_cmd_t *mcp = &mc;
2629
2630         if (!IS_IIDMA_CAPABLE(vha->hw))
2631                 return QLA_FUNCTION_FAILED;
2632
2633         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2634
2635         mcp->mb[0] = MBC_PORT_PARAMS;
2636         mcp->mb[1] = loop_id;
2637         mcp->mb[2] = BIT_0;
2638         mcp->mb[3] = port_speed & (BIT_2|BIT_1|BIT_0);
2639         mcp->mb[4] = mcp->mb[5] = 0;
2640         mcp->out_mb = MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2641         mcp->in_mb = MBX_5|MBX_4|MBX_3|MBX_1|MBX_0;
2642         mcp->tov = MBX_TOV_SECONDS;
2643         mcp->flags = 0;
2644         rval = qla2x00_mailbox_command(vha, mcp);
2645
2646         /* Return mailbox statuses. */
2647         if (mb != NULL) {
2648                 mb[0] = mcp->mb[0];
2649                 mb[1] = mcp->mb[1];
2650                 mb[3] = mcp->mb[3];
2651                 mb[4] = mcp->mb[4];
2652                 mb[5] = mcp->mb[5];
2653         }
2654
2655         if (rval != QLA_SUCCESS) {
2656                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2657                     vha->host_no, rval));
2658         } else {
2659                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2660         }
2661
2662         return rval;
2663 }
2664
2665 void
2666 qla24xx_report_id_acquisition(scsi_qla_host_t *vha,
2667         struct vp_rpt_id_entry_24xx *rptid_entry)
2668 {
2669         uint8_t vp_idx;
2670         uint16_t stat = le16_to_cpu(rptid_entry->vp_idx);
2671         struct qla_hw_data *ha = vha->hw;
2672         scsi_qla_host_t *vp;
2673
2674         if (rptid_entry->entry_status != 0)
2675                 return;
2676
2677         if (rptid_entry->format == 0) {
2678                 DEBUG15(printk("%s:format 0 : scsi(%ld) number of VPs setup %d,"
2679                         " number of VPs acquired %d\n", __func__, vha->host_no,
2680                         MSB(rptid_entry->vp_count), LSB(rptid_entry->vp_count)));
2681                 DEBUG15(printk("%s primary port id %02x%02x%02x\n", __func__,
2682                         rptid_entry->port_id[2], rptid_entry->port_id[1],
2683                         rptid_entry->port_id[0]));
2684         } else if (rptid_entry->format == 1) {
2685                 vp_idx = LSB(stat);
2686                 DEBUG15(printk("%s:format 1: scsi(%ld): VP[%d] enabled "
2687                     "- status %d - "
2688                     "with port id %02x%02x%02x\n", __func__, vha->host_no,
2689                     vp_idx, MSB(stat),
2690                     rptid_entry->port_id[2], rptid_entry->port_id[1],
2691                     rptid_entry->port_id[0]));
2692                 if (vp_idx == 0)
2693                         return;
2694
2695                 if (MSB(stat) == 1)
2696                         return;
2697
2698                 list_for_each_entry(vp, &ha->vp_list, list)
2699                         if (vp_idx == vp->vp_idx)
2700                                 break;
2701                 if (!vp)
2702                         return;
2703
2704                 vp->d_id.b.domain = rptid_entry->port_id[2];
2705                 vp->d_id.b.area =  rptid_entry->port_id[1];
2706                 vp->d_id.b.al_pa = rptid_entry->port_id[0];
2707
2708                 /*
2709                  * Cannot configure here as we are still sitting on the
2710                  * response queue. Handle it in dpc context.
2711                  */
2712                 set_bit(VP_IDX_ACQUIRED, &vp->vp_flags);
2713                 set_bit(VP_DPC_NEEDED, &vha->dpc_flags);
2714
2715                 qla2xxx_wake_dpc(vha);
2716         }
2717 }
2718
2719 /*
2720  * qla24xx_modify_vp_config
2721  *      Change VP configuration for vha
2722  *
2723  * Input:
2724  *      vha = adapter block pointer.
2725  *
2726  * Returns:
2727  *      qla2xxx local function return status code.
2728  *
2729  * Context:
2730  *      Kernel context.
2731  */
2732 int
2733 qla24xx_modify_vp_config(scsi_qla_host_t *vha)
2734 {
2735         int             rval;
2736         struct vp_config_entry_24xx *vpmod;
2737         dma_addr_t      vpmod_dma;
2738         struct qla_hw_data *ha = vha->hw;
2739         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2740
2741         /* This can be called by the parent */
2742
2743         vpmod = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &vpmod_dma);
2744         if (!vpmod) {
2745                 DEBUG2_3(printk("%s(%ld): failed to allocate Modify VP "
2746                     "IOCB.\n", __func__, vha->host_no));
2747                 return QLA_MEMORY_ALLOC_FAILED;
2748         }
2749
2750         memset(vpmod, 0, sizeof(struct vp_config_entry_24xx));
2751         vpmod->entry_type = VP_CONFIG_IOCB_TYPE;
2752         vpmod->entry_count = 1;
2753         vpmod->command = VCT_COMMAND_MOD_ENABLE_VPS;
2754         vpmod->vp_count = 1;
2755         vpmod->vp_index1 = vha->vp_idx;
2756         vpmod->options_idx1 = BIT_3|BIT_4|BIT_5;
2757         memcpy(vpmod->node_name_idx1, vha->node_name, WWN_SIZE);
2758         memcpy(vpmod->port_name_idx1, vha->port_name, WWN_SIZE);
2759         vpmod->entry_count = 1;
2760
2761         rval = qla2x00_issue_iocb(base_vha, vpmod, vpmod_dma, 0);
2762         if (rval != QLA_SUCCESS) {
2763                 DEBUG2_3_11(printk("%s(%ld): failed to issue VP config IOCB"
2764                         "(%x).\n", __func__, base_vha->host_no, rval));
2765         } else if (vpmod->comp_status != 0) {
2766                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2767                         "-- error status (%x).\n", __func__, base_vha->host_no,
2768                         vpmod->comp_status));
2769                 rval = QLA_FUNCTION_FAILED;
2770         } else if (vpmod->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
2771                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2772                     "-- completion status (%x).\n", __func__, base_vha->host_no,
2773                     le16_to_cpu(vpmod->comp_status)));
2774                 rval = QLA_FUNCTION_FAILED;
2775         } else {
2776                 /* EMPTY */
2777                 DEBUG11(printk("%s(%ld): done.\n", __func__,
2778                                                         base_vha->host_no));
2779                 fc_vport_set_state(vha->fc_vport, FC_VPORT_INITIALIZING);
2780         }
2781         dma_pool_free(ha->s_dma_pool, vpmod, vpmod_dma);
2782
2783         return rval;
2784 }
2785
2786 /*
2787  * qla24xx_control_vp
2788  *      Enable a virtual port for given host
2789  *
2790  * Input:
2791  *      ha = adapter block pointer.
2792  *      vhba = virtual adapter (unused)
2793  *      index = index number for enabled VP
2794  *
2795  * Returns:
2796  *      qla2xxx local function return status code.
2797  *
2798  * Context:
2799  *      Kernel context.
2800  */
2801 int
2802 qla24xx_control_vp(scsi_qla_host_t *vha, int cmd)
2803 {
2804         int             rval;
2805         int             map, pos;
2806         struct vp_ctrl_entry_24xx   *vce;
2807         dma_addr_t      vce_dma;
2808         struct qla_hw_data *ha = vha->hw;
2809         int     vp_index = vha->vp_idx;
2810         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2811
2812         DEBUG11(printk("%s(%ld): entered. Enabling index %d\n", __func__,
2813             vha->host_no, vp_index));
2814
2815         if (vp_index == 0 || vp_index >= ha->max_npiv_vports)
2816                 return QLA_PARAMETER_ERROR;
2817
2818         vce = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &vce_dma);
2819         if (!vce) {
2820                 DEBUG2_3(printk("%s(%ld): "
2821                     "failed to allocate VP Control IOCB.\n", __func__,
2822                     base_vha->host_no));
2823                 return QLA_MEMORY_ALLOC_FAILED;
2824         }
2825         memset(vce, 0, sizeof(struct vp_ctrl_entry_24xx));
2826
2827         vce->entry_type = VP_CTRL_IOCB_TYPE;
2828         vce->entry_count = 1;
2829         vce->command = cpu_to_le16(cmd);
2830         vce->vp_count = __constant_cpu_to_le16(1);
2831
2832         /* index map in firmware starts with 1; decrement index
2833          * this is ok as we never use index 0
2834          */
2835         map = (vp_index - 1) / 8;
2836         pos = (vp_index - 1) & 7;
2837         mutex_lock(&ha->vport_lock);
2838         vce->vp_idx_map[map] |= 1 << pos;
2839         mutex_unlock(&ha->vport_lock);
2840
2841         rval = qla2x00_issue_iocb(base_vha, vce, vce_dma, 0);
2842         if (rval != QLA_SUCCESS) {
2843                 DEBUG2_3_11(printk("%s(%ld): failed to issue VP control IOCB"
2844                     "(%x).\n", __func__, base_vha->host_no, rval));
2845                 printk("%s(%ld): failed to issue VP control IOCB"
2846                     "(%x).\n", __func__, base_vha->host_no, rval);
2847         } else if (vce->entry_status != 0) {
2848                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2849                     "-- error status (%x).\n", __func__, base_vha->host_no,
2850                     vce->entry_status));
2851                 printk("%s(%ld): failed to complete IOCB "
2852                     "-- error status (%x).\n", __func__, base_vha->host_no,
2853                     vce->entry_status);
2854                 rval = QLA_FUNCTION_FAILED;
2855         } else if (vce->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
2856                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2857                     "-- completion status (%x).\n", __func__, base_vha->host_no,
2858                     le16_to_cpu(vce->comp_status)));
2859                 printk("%s(%ld): failed to complete IOCB "
2860                     "-- completion status (%x).\n", __func__, base_vha->host_no,
2861                     le16_to_cpu(vce->comp_status));
2862                 rval = QLA_FUNCTION_FAILED;
2863         } else {
2864                 DEBUG2(printk("%s(%ld): done.\n", __func__, base_vha->host_no));
2865         }
2866
2867         dma_pool_free(ha->s_dma_pool, vce, vce_dma);
2868
2869         return rval;
2870 }
2871
2872 /*
2873  * qla2x00_send_change_request
2874  *      Receive or disable RSCN request from fabric controller
2875  *
2876  * Input:
2877  *      ha = adapter block pointer
2878  *      format = registration format:
2879  *              0 - Reserved
2880  *              1 - Fabric detected registration
2881  *              2 - N_port detected registration
2882  *              3 - Full registration
2883  *              FF - clear registration
2884  *      vp_idx = Virtual port index
2885  *
2886  * Returns:
2887  *      qla2x00 local function return status code.
2888  *
2889  * Context:
2890  *      Kernel Context
2891  */
2892
2893 int
2894 qla2x00_send_change_request(scsi_qla_host_t *vha, uint16_t format,
2895                             uint16_t vp_idx)
2896 {
2897         int rval;
2898         mbx_cmd_t mc;
2899         mbx_cmd_t *mcp = &mc;
2900
2901         /*
2902          * This command is implicitly executed by firmware during login for the
2903          * physical hosts
2904          */
2905         if (vp_idx == 0)
2906                 return QLA_FUNCTION_FAILED;
2907
2908         mcp->mb[0] = MBC_SEND_CHANGE_REQUEST;
2909         mcp->mb[1] = format;
2910         mcp->mb[9] = vp_idx;
2911         mcp->out_mb = MBX_9|MBX_1|MBX_0;
2912         mcp->in_mb = MBX_0|MBX_1;
2913         mcp->tov = MBX_TOV_SECONDS;
2914         mcp->flags = 0;
2915         rval = qla2x00_mailbox_command(vha, mcp);
2916
2917         if (rval == QLA_SUCCESS) {
2918                 if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2919                         rval = BIT_1;
2920                 }
2921         } else
2922                 rval = BIT_1;
2923
2924         return rval;
2925 }
2926
2927 int
2928 qla2x00_dump_ram(scsi_qla_host_t *vha, dma_addr_t req_dma, uint32_t addr,
2929     uint32_t size)
2930 {
2931         int rval;
2932         mbx_cmd_t mc;
2933         mbx_cmd_t *mcp = &mc;
2934
2935         DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2936
2937         if (MSW(addr) || IS_FWI2_CAPABLE(vha->hw)) {
2938                 mcp->mb[0] = MBC_DUMP_RISC_RAM_EXTENDED;
2939                 mcp->mb[8] = MSW(addr);
2940                 mcp->out_mb = MBX_8|MBX_0;
2941         } else {
2942                 mcp->mb[0] = MBC_DUMP_RISC_RAM;
2943                 mcp->out_mb = MBX_0;
2944         }
2945         mcp->mb[1] = LSW(addr);
2946         mcp->mb[2] = MSW(req_dma);
2947         mcp->mb[3] = LSW(req_dma);
2948         mcp->mb[6] = MSW(MSD(req_dma));
2949         mcp->mb[7] = LSW(MSD(req_dma));
2950         mcp->out_mb |= MBX_7|MBX_6|MBX_3|MBX_2|MBX_1;
2951         if (IS_FWI2_CAPABLE(vha->hw)) {
2952                 mcp->mb[4] = MSW(size);
2953                 mcp->mb[5] = LSW(size);
2954                 mcp->out_mb |= MBX_5|MBX_4;
2955         } else {
2956                 mcp->mb[4] = LSW(size);
2957                 mcp->out_mb |= MBX_4;
2958         }
2959
2960         mcp->in_mb = MBX_0;
2961         mcp->tov = MBX_TOV_SECONDS;
2962         mcp->flags = 0;
2963         rval = qla2x00_mailbox_command(vha, mcp);
2964
2965         if (rval != QLA_SUCCESS) {
2966                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
2967                     vha->host_no, rval, mcp->mb[0]));
2968         } else {
2969                 DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2970         }
2971
2972         return rval;
2973 }
2974
2975 /* 84XX Support **************************************************************/
2976
2977 struct cs84xx_mgmt_cmd {
2978         union {
2979                 struct verify_chip_entry_84xx req;
2980                 struct verify_chip_rsp_84xx rsp;
2981         } p;
2982 };
2983
2984 int
2985 qla84xx_verify_chip(struct scsi_qla_host *vha, uint16_t *status)
2986 {
2987         int rval, retry;
2988         struct cs84xx_mgmt_cmd *mn;
2989         dma_addr_t mn_dma;
2990         uint16_t options;
2991         unsigned long flags;
2992         struct qla_hw_data *ha = vha->hw;
2993
2994         DEBUG16(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2995
2996         mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
2997         if (mn == NULL) {
2998                 DEBUG2_3(printk("%s(%ld): failed to allocate Verify ISP84XX "
2999                     "IOCB.\n", __func__, vha->host_no));
3000                 return QLA_MEMORY_ALLOC_FAILED;
3001         }
3002
3003         /* Force Update? */
3004         options = ha->cs84xx->fw_update ? VCO_FORCE_UPDATE : 0;
3005         /* Diagnostic firmware? */
3006         /* options |= MENLO_DIAG_FW; */
3007         /* We update the firmware with only one data sequence. */
3008         options |= VCO_END_OF_DATA;
3009
3010         do {
3011                 retry = 0;
3012                 memset(mn, 0, sizeof(*mn));
3013                 mn->p.req.entry_type = VERIFY_CHIP_IOCB_TYPE;
3014                 mn->p.req.entry_count = 1;
3015                 mn->p.req.options = cpu_to_le16(options);
3016
3017                 DEBUG16(printk("%s(%ld): Dump of Verify Request.\n", __func__,
3018                     vha->host_no));
3019                 DEBUG16(qla2x00_dump_buffer((uint8_t *)mn,
3020                     sizeof(*mn)));
3021
3022                 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
3023                 if (rval != QLA_SUCCESS) {
3024                         DEBUG2_16(printk("%s(%ld): failed to issue Verify "
3025                             "IOCB (%x).\n", __func__, vha->host_no, rval));
3026                         goto verify_done;
3027                 }
3028
3029                 DEBUG16(printk("%s(%ld): Dump of Verify Response.\n", __func__,
3030                     vha->host_no));
3031                 DEBUG16(qla2x00_dump_buffer((uint8_t *)mn,
3032                     sizeof(*mn)));
3033
3034                 status[0] = le16_to_cpu(mn->p.rsp.comp_status);
3035                 status[1] = status[0] == CS_VCS_CHIP_FAILURE ?
3036                     le16_to_cpu(mn->p.rsp.failure_code) : 0;
3037                 DEBUG2_16(printk("%s(%ld): cs=%x fc=%x\n", __func__,
3038                     vha->host_no, status[0], status[1]));
3039
3040                 if (status[0] != CS_COMPLETE) {
3041                         rval = QLA_FUNCTION_FAILED;
3042                         if (!(options & VCO_DONT_UPDATE_FW)) {
3043                                 DEBUG2_16(printk("%s(%ld): Firmware update "
3044                                     "failed. Retrying without update "
3045                                     "firmware.\n", __func__, vha->host_no));
3046                                 options |= VCO_DONT_UPDATE_FW;
3047                                 options &= ~VCO_FORCE_UPDATE;
3048                                 retry = 1;
3049                         }
3050                 } else {
3051                         DEBUG2_16(printk("%s(%ld): firmware updated to %x.\n",
3052                             __func__, vha->host_no,
3053                             le32_to_cpu(mn->p.rsp.fw_ver)));
3054
3055                         /* NOTE: we only update OP firmware. */
3056                         spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
3057                         ha->cs84xx->op_fw_version =
3058                             le32_to_cpu(mn->p.rsp.fw_ver);
3059                         spin_unlock_irqrestore(&ha->cs84xx->access_lock,
3060                             flags);
3061                 }
3062         } while (retry);
3063
3064 verify_done:
3065         dma_pool_free(ha->s_dma_pool, mn, mn_dma);
3066
3067         if (rval != QLA_SUCCESS) {
3068                 DEBUG2_16(printk("%s(%ld): failed=%x.\n", __func__,
3069                     vha->host_no, rval));
3070         } else {
3071                 DEBUG16(printk("%s(%ld): done.\n", __func__, vha->host_no));
3072         }
3073
3074         return rval;
3075 }
3076
3077 int
3078 qla25xx_init_req_que(struct scsi_qla_host *vha, struct req_que *req,
3079         uint8_t options)
3080 {
3081         int rval;
3082         unsigned long flags;
3083         mbx_cmd_t mc;
3084         mbx_cmd_t *mcp = &mc;
3085         struct device_reg_25xxmq __iomem *reg;
3086         struct qla_hw_data *ha = vha->hw;
3087
3088         mcp->mb[0] = MBC_INITIALIZE_MULTIQ;
3089         mcp->mb[1] = options;
3090         mcp->mb[2] = MSW(LSD(req->dma));
3091         mcp->mb[3] = LSW(LSD(req->dma));
3092         mcp->mb[6] = MSW(MSD(req->dma));
3093         mcp->mb[7] = LSW(MSD(req->dma));
3094         mcp->mb[5] = req->length;
3095         if (req->rsp)
3096                 mcp->mb[10] = req->rsp->id;
3097         mcp->mb[12] = req->qos;
3098         mcp->mb[11] = req->vp_idx;
3099         mcp->mb[13] = req->rid;
3100
3101         reg = (struct device_reg_25xxmq *)((void *)(ha->mqiobase) +
3102                 QLA_QUE_PAGE * req->id);
3103
3104         mcp->mb[4] = req->id;
3105         /* que in ptr index */
3106         mcp->mb[8] = 0;
3107         /* que out ptr index */
3108         mcp->mb[9] = 0;
3109         mcp->out_mb = MBX_14|MBX_13|MBX_12|MBX_11|MBX_10|MBX_9|MBX_8|MBX_7|
3110                         MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
3111         mcp->in_mb = MBX_0;
3112         mcp->flags = MBX_DMA_OUT;
3113         mcp->tov = 60;
3114
3115         spin_lock_irqsave(&ha->hardware_lock, flags);
3116         if (!(options & BIT_0)) {
3117                 WRT_REG_DWORD(&reg->req_q_in, 0);
3118                 WRT_REG_DWORD(&reg->req_q_out, 0);
3119         }
3120         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3121
3122         rval = qla2x00_mailbox_command(vha, mcp);
3123         if (rval != QLA_SUCCESS)
3124                 DEBUG2_3_11(printk(KERN_WARNING "%s(%ld): failed=%x mb0=%x.\n",
3125                         __func__, vha->host_no, rval, mcp->mb[0]));
3126         return rval;
3127 }
3128
3129 int
3130 qla25xx_init_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp,
3131         uint8_t options)
3132 {
3133         int rval;
3134         unsigned long flags;
3135         mbx_cmd_t mc;
3136         mbx_cmd_t *mcp = &mc;
3137         struct device_reg_25xxmq __iomem *reg;
3138         struct qla_hw_data *ha = vha->hw;
3139
3140         mcp->mb[0] = MBC_INITIALIZE_MULTIQ;
3141         mcp->mb[1] = options;
3142         mcp->mb[2] = MSW(LSD(rsp->dma));
3143         mcp->mb[3] = LSW(LSD(rsp->dma));
3144         mcp->mb[6] = MSW(MSD(rsp->dma));
3145         mcp->mb[7] = LSW(MSD(rsp->dma));
3146         mcp->mb[5] = rsp->length;
3147         mcp->mb[11] = rsp->vp_idx;
3148         mcp->mb[14] = rsp->msix->vector;
3149         mcp->mb[13] = rsp->rid;
3150
3151         reg = (struct device_reg_25xxmq *)((void *)(ha->mqiobase) +
3152                 QLA_QUE_PAGE * rsp->id);
3153
3154         mcp->mb[4] = rsp->id;
3155         /* que in ptr index */
3156         mcp->mb[8] = 0;
3157         /* que out ptr index */
3158         mcp->mb[9] = 0;
3159         mcp->out_mb = MBX_14|MBX_13|MBX_12|MBX_11|MBX_10|MBX_9|MBX_8|MBX_7
3160                         |MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
3161         mcp->in_mb = MBX_0;
3162         mcp->flags = MBX_DMA_OUT;
3163         mcp->tov = 60;
3164
3165         spin_lock_irqsave(&ha->hardware_lock, flags);
3166         if (!(options & BIT_0)) {
3167                 WRT_REG_DWORD(&reg->rsp_q_out, 0);
3168                 WRT_REG_DWORD(&reg->rsp_q_in, 0);
3169         }
3170
3171         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3172
3173         rval = qla2x00_mailbox_command(vha, mcp);
3174         if (rval != QLA_SUCCESS)
3175                 DEBUG2_3_11(printk(KERN_WARNING "%s(%ld): failed=%x "
3176                         "mb0=%x.\n", __func__,
3177                         vha->host_no, rval, mcp->mb[0]));
3178         return rval;
3179 }
3180