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