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