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