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