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