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