]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/qla2xxx/qla_mbx.c
[SCSI] qla2xxx: Correct loop-in-transition issues
[linux-2.6-omap-h63xx.git] / drivers / scsi / qla2xxx / qla_mbx.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2005 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/delay.h>
10 #include <scsi/scsi_transport_fc.h>
11
12 static void
13 qla2x00_mbx_sem_timeout(unsigned long data)
14 {
15         struct semaphore        *sem_ptr = (struct semaphore *)data;
16
17         DEBUG11(printk("qla2x00_sem_timeout: entered.\n");)
18
19         if (sem_ptr != NULL) {
20                 up(sem_ptr);
21         }
22
23         DEBUG11(printk("qla2x00_mbx_sem_timeout: exiting.\n");)
24 }
25
26 /*
27  * qla2x00_mailbox_command
28  *      Issue mailbox command and waits for completion.
29  *
30  * Input:
31  *      ha = adapter block pointer.
32  *      mcp = driver internal mbx struct pointer.
33  *
34  * Output:
35  *      mb[MAX_MAILBOX_REGISTER_COUNT] = returned mailbox data.
36  *
37  * Returns:
38  *      0 : QLA_SUCCESS = cmd performed success
39  *      1 : QLA_FUNCTION_FAILED   (error encountered)
40  *      6 : QLA_FUNCTION_TIMEOUT (timeout condition encountered)
41  *
42  * Context:
43  *      Kernel context.
44  */
45 static int
46 qla2x00_mailbox_command(scsi_qla_host_t *ha, mbx_cmd_t *mcp)
47 {
48         int             rval;
49         unsigned long    flags = 0;
50         device_reg_t __iomem *reg = ha->iobase;
51         struct timer_list       tmp_intr_timer;
52         uint8_t         abort_active;
53         uint8_t         io_lock_on = ha->flags.init_done;
54         uint16_t        command;
55         uint16_t        *iptr;
56         uint16_t __iomem *optr;
57         uint32_t        cnt;
58         uint32_t        mboxes;
59         unsigned long   mbx_flags = 0;
60         unsigned long   wait_time;
61
62         rval = QLA_SUCCESS;
63         abort_active = test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
64
65         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
66
67         /*
68          * Wait for active mailbox commands to finish by waiting at most tov
69          * seconds. This is to serialize actual issuing of mailbox cmds during
70          * non ISP abort time.
71          */
72         if (!abort_active) {
73                 if (qla2x00_down_timeout(&ha->mbx_cmd_sem, mcp->tov * HZ)) {
74                         /* Timeout occurred. Return error. */
75                         DEBUG2_3_11(printk("%s(%ld): cmd access timeout. "
76                             "Exiting.\n", __func__, ha->host_no);)
77                         return QLA_FUNCTION_TIMEOUT;
78                 }
79         }
80
81         ha->flags.mbox_busy = 1;
82         /* Save mailbox command for debug */
83         ha->mcp = mcp;
84
85         /* Try to get mailbox register access */
86         if (!abort_active)
87                 spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
88
89         DEBUG11(printk("scsi(%ld): prepare to issue mbox cmd=0x%x.\n",
90             ha->host_no, mcp->mb[0]);)
91
92         spin_lock_irqsave(&ha->hardware_lock, flags);
93
94         /* Load mailbox registers. */
95         if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
96                 optr = (uint16_t __iomem *)&reg->isp24.mailbox0;
97         else
98                 optr = (uint16_t __iomem *)MAILBOX_REG(ha, &reg->isp, 0);
99
100         iptr = mcp->mb;
101         command = mcp->mb[0];
102         mboxes = mcp->out_mb;
103
104         for (cnt = 0; cnt < ha->mbx_count; cnt++) {
105                 if (IS_QLA2200(ha) && cnt == 8)
106                         optr =
107                             (uint16_t __iomem *)MAILBOX_REG(ha, &reg->isp, 8);
108                 if (mboxes & BIT_0)
109                         WRT_REG_WORD(optr, *iptr);
110
111                 mboxes >>= 1;
112                 optr++;
113                 iptr++;
114         }
115
116 #if defined(QL_DEBUG_LEVEL_1)
117         printk("%s(%ld): Loaded MBX registers (displayed in bytes) = \n",
118             __func__, ha->host_no);
119         qla2x00_dump_buffer((uint8_t *)mcp->mb, 16);
120         printk("\n");
121         qla2x00_dump_buffer(((uint8_t *)mcp->mb + 0x10), 16);
122         printk("\n");
123         qla2x00_dump_buffer(((uint8_t *)mcp->mb + 0x20), 8);
124         printk("\n");
125         printk("%s(%ld): I/O address = %p.\n", __func__, ha->host_no, optr);
126         qla2x00_dump_regs(ha);
127 #endif
128
129         /* Issue set host interrupt command to send cmd out. */
130         ha->flags.mbox_int = 0;
131         clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
132
133         /* Unlock mbx registers and wait for interrupt */
134         DEBUG11(printk("%s(%ld): going to unlock irq & waiting for interrupt. "
135             "jiffies=%lx.\n", __func__, ha->host_no, jiffies);)
136
137         /* Wait for mbx cmd completion until timeout */
138
139         if (!abort_active && io_lock_on) {
140                 /* sleep on completion semaphore */
141                 DEBUG11(printk("%s(%ld): INTERRUPT MODE. Initializing timer.\n",
142                     __func__, ha->host_no);)
143
144                 init_timer(&tmp_intr_timer);
145                 tmp_intr_timer.data = (unsigned long)&ha->mbx_intr_sem;
146                 tmp_intr_timer.expires = jiffies + mcp->tov * HZ;
147                 tmp_intr_timer.function =
148                     (void (*)(unsigned long))qla2x00_mbx_sem_timeout;
149
150                 DEBUG11(printk("%s(%ld): Adding timer.\n", __func__,
151                     ha->host_no);)
152                 add_timer(&tmp_intr_timer);
153
154                 DEBUG11(printk("%s(%ld): going to unlock & sleep. "
155                     "time=0x%lx.\n", __func__, ha->host_no, jiffies);)
156
157                 set_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
158
159                 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
160                         WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
161                 else
162                         WRT_REG_WORD(&reg->isp.hccr, HCCR_SET_HOST_INT);
163                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
164
165                 if (!abort_active)
166                         spin_unlock_irqrestore(&ha->mbx_reg_lock, mbx_flags);
167
168                 /* Wait for either the timer to expire
169                  * or the mbox completion interrupt
170                  */
171                 down(&ha->mbx_intr_sem);
172
173                 DEBUG11(printk("%s(%ld): waking up. time=0x%lx\n", __func__,
174                     ha->host_no, jiffies);)
175                 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
176
177                 /* delete the timer */
178                 del_timer(&tmp_intr_timer);
179         } else {
180                 DEBUG3_11(printk("%s(%ld): cmd=%x POLLING MODE.\n", __func__,
181                     ha->host_no, command);)
182
183                 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
184                         WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
185                 else
186                         WRT_REG_WORD(&reg->isp.hccr, HCCR_SET_HOST_INT);
187                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
188                 if (!abort_active)
189                         spin_unlock_irqrestore(&ha->mbx_reg_lock, mbx_flags);
190
191                 wait_time = jiffies + mcp->tov * HZ; /* wait at most tov secs */
192                 while (!ha->flags.mbox_int) {
193                         if (time_after(jiffies, wait_time))
194                                 break;
195
196                         /* Check for pending interrupts. */
197                         qla2x00_poll(ha);
198
199                         udelay(10); /* v4.27 */
200                 } /* while */
201         }
202
203         if (!abort_active)
204                 spin_lock_irqsave(&ha->mbx_reg_lock, mbx_flags);
205
206         /* Check whether we timed out */
207         if (ha->flags.mbox_int) {
208                 uint16_t *iptr2;
209
210                 DEBUG3_11(printk("%s(%ld): cmd %x completed.\n", __func__,
211                     ha->host_no, command);)
212
213                 /* Got interrupt. Clear the flag. */
214                 ha->flags.mbox_int = 0;
215                 clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
216
217                 if (ha->mailbox_out[0] != MBS_COMMAND_COMPLETE)
218                         rval = QLA_FUNCTION_FAILED;
219
220                 /* Load return mailbox registers. */
221                 iptr2 = mcp->mb;
222                 iptr = (uint16_t *)&ha->mailbox_out[0];
223                 mboxes = mcp->in_mb;
224                 for (cnt = 0; cnt < ha->mbx_count; cnt++) {
225                         if (mboxes & BIT_0)
226                                 *iptr2 = *iptr;
227
228                         mboxes >>= 1;
229                         iptr2++;
230                         iptr++;
231                 }
232         } else {
233
234 #if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3) || \
235                 defined(QL_DEBUG_LEVEL_11)
236                 uint16_t mb0;
237                 uint32_t ictrl;
238
239                 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
240                         mb0 = RD_REG_WORD(&reg->isp24.mailbox0);
241                         ictrl = RD_REG_DWORD(&reg->isp24.ictrl);
242                 } else {
243                         mb0 = RD_MAILBOX_REG(ha, &reg->isp, 0);
244                         ictrl = RD_REG_WORD(&reg->isp.ictrl);
245                 }
246                 printk("%s(%ld): **** MB Command Timeout for cmd %x ****\n",
247                     __func__, ha->host_no, command);
248                 printk("%s(%ld): icontrol=%x jiffies=%lx\n", __func__,
249                     ha->host_no, ictrl, jiffies);
250                 printk("%s(%ld): *** mailbox[0] = 0x%x ***\n", __func__,
251                     ha->host_no, mb0);
252                 qla2x00_dump_regs(ha);
253 #endif
254
255                 rval = QLA_FUNCTION_TIMEOUT;
256         }
257
258         if (!abort_active)
259                 spin_unlock_irqrestore(&ha->mbx_reg_lock, mbx_flags);
260
261         ha->flags.mbox_busy = 0;
262
263         /* Clean up */
264         ha->mcp = NULL;
265
266         if (!abort_active) {
267                 DEBUG11(printk("%s(%ld): checking for additional resp "
268                     "interrupt.\n", __func__, ha->host_no);)
269
270                 /* polling mode for non isp_abort commands. */
271                 qla2x00_poll(ha);
272         }
273
274         if (rval == QLA_FUNCTION_TIMEOUT &&
275             mcp->mb[0] != MBC_GEN_SYSTEM_ERROR) {
276                 if (!io_lock_on || (mcp->flags & IOCTL_CMD)) {
277                         /* not in dpc. schedule it for dpc to take over. */
278                         DEBUG(printk("%s(%ld): timeout schedule "
279                             "isp_abort_needed.\n", __func__, ha->host_no);)
280                         DEBUG2_3_11(printk("%s(%ld): timeout schedule "
281                             "isp_abort_needed.\n", __func__, ha->host_no);)
282                         qla_printk(KERN_WARNING, ha,
283                             "Mailbox command timeout occured. Scheduling ISP "
284                             "abort.\n");
285                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
286                         if (ha->dpc_wait && !ha->dpc_active)
287                                 up(ha->dpc_wait);
288
289                 } else if (!abort_active) {
290                         /* call abort directly since we are in the DPC thread */
291                         DEBUG(printk("%s(%ld): timeout calling abort_isp\n",
292                             __func__, ha->host_no);)
293                         DEBUG2_3_11(printk("%s(%ld): timeout calling "
294                             "abort_isp\n", __func__, ha->host_no);)
295                         qla_printk(KERN_WARNING, ha,
296                             "Mailbox command timeout occured. Issuing ISP "
297                             "abort.\n");
298
299                         set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
300                         clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
301                         if (qla2x00_abort_isp(ha)) {
302                                 /* Failed. retry later. */
303                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
304                         }
305                         clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
306                         DEBUG(printk("%s(%ld): finished abort_isp\n", __func__,
307                             ha->host_no);)
308                         DEBUG2_3_11(printk("%s(%ld): finished abort_isp\n",
309                             __func__, ha->host_no);)
310                 }
311         }
312
313         /* Allow next mbx cmd to come in. */
314         if (!abort_active)
315                 up(&ha->mbx_cmd_sem);
316
317         if (rval) {
318                 DEBUG2_3_11(printk("%s(%ld): **** FAILED. mbx0=%x, mbx1=%x, "
319                     "mbx2=%x, cmd=%x ****\n", __func__, ha->host_no,
320                     mcp->mb[0], mcp->mb[1], mcp->mb[2], command);)
321         } else {
322                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
323         }
324
325         return rval;
326 }
327
328 /*
329  * qla2x00_load_ram
330  *      Load adapter RAM using DMA.
331  *
332  * Input:
333  *      ha = adapter block pointer.
334  *
335  * Returns:
336  *      qla2x00 local function return status code.
337  *
338  * Context:
339  *      Kernel context.
340  */
341 int
342 qla2x00_load_ram(scsi_qla_host_t *ha, dma_addr_t req_dma, uint16_t risc_addr,
343     uint16_t risc_code_size)
344 {
345         int rval;
346         mbx_cmd_t mc;
347         mbx_cmd_t *mcp = &mc;
348         uint32_t        req_len;
349         dma_addr_t      nml_dma;
350         uint32_t        nml_len;
351         uint32_t        normalized;
352
353         DEBUG11(printk("qla2x00_load_ram(%ld): entered.\n",
354             ha->host_no);)
355
356         req_len = risc_code_size;
357         nml_dma = 0;
358         nml_len = 0;
359
360         normalized = qla2x00_normalize_dma_addr(&req_dma, &req_len, &nml_dma,
361             &nml_len);
362
363         /* Load first segment */
364         mcp->mb[0] = MBC_LOAD_RISC_RAM;
365         mcp->mb[1] = risc_addr;
366         mcp->mb[2] = MSW(req_dma);
367         mcp->mb[3] = LSW(req_dma);
368         mcp->mb[4] = (uint16_t)req_len;
369         mcp->mb[6] = MSW(MSD(req_dma));
370         mcp->mb[7] = LSW(MSD(req_dma));
371         mcp->out_mb = MBX_7|MBX_6|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
372         mcp->in_mb = MBX_0;
373         mcp->tov = 30;
374         mcp->flags = 0;
375         rval = qla2x00_mailbox_command(ha, mcp);
376
377         /* Load second segment - if necessary */
378         if (normalized && (rval == QLA_SUCCESS)) {
379                 mcp->mb[0] = MBC_LOAD_RISC_RAM;
380                 mcp->mb[1] = risc_addr + (uint16_t)req_len;
381                 mcp->mb[2] = MSW(nml_dma);
382                 mcp->mb[3] = LSW(nml_dma);
383                 mcp->mb[4] = (uint16_t)nml_len;
384                 mcp->mb[6] = MSW(MSD(nml_dma));
385                 mcp->mb[7] = LSW(MSD(nml_dma));
386                 mcp->out_mb = MBX_7|MBX_6|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
387                 mcp->in_mb = MBX_0;
388                 mcp->tov = 30;
389                 mcp->flags = 0;
390                 rval = qla2x00_mailbox_command(ha, mcp);
391         }
392
393         if (rval == QLA_SUCCESS) {
394                 /* Empty */
395                 DEBUG11(printk("qla2x00_load_ram(%ld): done.\n", ha->host_no);)
396         } else {
397                 /* Empty */
398                 DEBUG2_3_11(printk("qla2x00_load_ram(%ld): failed. rval=%x "
399                     "mb[0]=%x.\n", ha->host_no, rval, mcp->mb[0]);)
400         }
401         return rval;
402 }
403
404 /*
405  * qla2x00_load_ram_ext
406  *      Load adapter extended RAM using DMA.
407  *
408  * Input:
409  *      ha = adapter block pointer.
410  *
411  * Returns:
412  *      qla2x00 local function return status code.
413  *
414  * Context:
415  *      Kernel context.
416  */
417 int
418 qla2x00_load_ram_ext(scsi_qla_host_t *ha, dma_addr_t req_dma,
419     uint32_t risc_addr, uint32_t risc_code_size)
420 {
421         int rval;
422         mbx_cmd_t mc;
423         mbx_cmd_t *mcp = &mc;
424
425         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
426
427         mcp->mb[0] = MBC_LOAD_RISC_RAM_EXTENDED;
428         mcp->mb[1] = LSW(risc_addr);
429         mcp->mb[2] = MSW(req_dma);
430         mcp->mb[3] = LSW(req_dma);
431         mcp->mb[6] = MSW(MSD(req_dma));
432         mcp->mb[7] = LSW(MSD(req_dma));
433         mcp->mb[8] = MSW(risc_addr);
434         mcp->out_mb = MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
435         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
436                 mcp->mb[4] = MSW(risc_code_size);
437                 mcp->mb[5] = LSW(risc_code_size);
438                 mcp->out_mb |= MBX_5|MBX_4;
439         } else {
440                 mcp->mb[4] = LSW(risc_code_size);
441                 mcp->out_mb |= MBX_4;
442         }
443
444         mcp->in_mb = MBX_0;
445         mcp->tov = 30;
446         mcp->flags = 0;
447         rval = qla2x00_mailbox_command(ha, mcp);
448
449         if (rval != QLA_SUCCESS) {
450                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
451                     ha->host_no, rval, mcp->mb[0]));
452         } else {
453                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
454         }
455
456         return rval;
457 }
458
459 /*
460  * qla2x00_execute_fw
461  *     Start adapter firmware.
462  *
463  * Input:
464  *     ha = adapter block pointer.
465  *     TARGET_QUEUE_LOCK must be released.
466  *     ADAPTER_STATE_LOCK must be released.
467  *
468  * Returns:
469  *     qla2x00 local function return status code.
470  *
471  * Context:
472  *     Kernel context.
473  */
474 int
475 qla2x00_execute_fw(scsi_qla_host_t *ha, uint32_t risc_addr)
476 {
477         int rval;
478         mbx_cmd_t mc;
479         mbx_cmd_t *mcp = &mc;
480
481         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
482
483         mcp->mb[0] = MBC_EXECUTE_FIRMWARE;
484         mcp->out_mb = MBX_0;
485         mcp->in_mb = MBX_0;
486         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
487                 mcp->mb[1] = MSW(risc_addr);
488                 mcp->mb[2] = LSW(risc_addr);
489                 mcp->mb[3] = 0;
490                 mcp->out_mb |= MBX_3|MBX_2|MBX_1;
491                 mcp->in_mb |= MBX_1;
492         } else {
493                 mcp->mb[1] = LSW(risc_addr);
494                 mcp->out_mb |= MBX_1;
495                 if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
496                         mcp->mb[2] = 0;
497                         mcp->out_mb |= MBX_2;
498                 }
499         }
500
501         mcp->tov = 30;
502         mcp->flags = 0;
503         rval = qla2x00_mailbox_command(ha, mcp);
504
505         if (rval != QLA_SUCCESS) {
506                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
507                     ha->host_no, rval, mcp->mb[0]));
508         } else {
509                 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
510                         DEBUG11(printk("%s(%ld): done exchanges=%x.\n",
511                             __func__, ha->host_no, mcp->mb[1]);)
512                 } else {
513                         DEBUG11(printk("%s(%ld): done.\n", __func__,
514                             ha->host_no);)
515                 }
516         }
517
518         return rval;
519 }
520
521 /*
522  * qla2x00_get_fw_version
523  *      Get firmware version.
524  *
525  * Input:
526  *      ha:             adapter state pointer.
527  *      major:          pointer for major number.
528  *      minor:          pointer for minor number.
529  *      subminor:       pointer for subminor number.
530  *
531  * Returns:
532  *      qla2x00 local function return status code.
533  *
534  * Context:
535  *      Kernel context.
536  */
537 void
538 qla2x00_get_fw_version(scsi_qla_host_t *ha, uint16_t *major, uint16_t *minor,
539     uint16_t *subminor, uint16_t *attributes, uint32_t *memory)
540 {
541         int             rval;
542         mbx_cmd_t       mc;
543         mbx_cmd_t       *mcp = &mc;
544
545         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
546
547         mcp->mb[0] = MBC_GET_FIRMWARE_VERSION;
548         mcp->out_mb = MBX_0;
549         mcp->in_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
550         mcp->flags = 0;
551         mcp->tov = 30;
552         rval = qla2x00_mailbox_command(ha, mcp);
553
554         /* Return mailbox data. */
555         *major = mcp->mb[1];
556         *minor = mcp->mb[2];
557         *subminor = mcp->mb[3];
558         *attributes = mcp->mb[6];
559         if (IS_QLA2100(ha) || IS_QLA2200(ha))
560                 *memory = 0x1FFFF;                      /* Defaults to 128KB. */
561         else
562                 *memory = (mcp->mb[5] << 16) | mcp->mb[4];
563
564         if (rval != QLA_SUCCESS) {
565                 /*EMPTY*/
566                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
567                     ha->host_no, rval));
568         } else {
569                 /*EMPTY*/
570                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
571         }
572 }
573
574 /*
575  * qla2x00_get_fw_options
576  *      Set firmware options.
577  *
578  * Input:
579  *      ha = adapter block pointer.
580  *      fwopt = pointer for firmware options.
581  *
582  * Returns:
583  *      qla2x00 local function return status code.
584  *
585  * Context:
586  *      Kernel context.
587  */
588 int
589 qla2x00_get_fw_options(scsi_qla_host_t *ha, uint16_t *fwopts)
590 {
591         int rval;
592         mbx_cmd_t mc;
593         mbx_cmd_t *mcp = &mc;
594
595         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
596
597         mcp->mb[0] = MBC_GET_FIRMWARE_OPTION;
598         mcp->out_mb = MBX_0;
599         mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
600         mcp->tov = 30;
601         mcp->flags = 0;
602         rval = qla2x00_mailbox_command(ha, mcp);
603
604         if (rval != QLA_SUCCESS) {
605                 /*EMPTY*/
606                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
607                     ha->host_no, rval));
608         } else {
609                 fwopts[0] = mcp->mb[0];
610                 fwopts[1] = mcp->mb[1];
611                 fwopts[2] = mcp->mb[2];
612                 fwopts[3] = mcp->mb[3];
613
614                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
615         }
616
617         return rval;
618 }
619
620
621 /*
622  * qla2x00_set_fw_options
623  *      Set firmware options.
624  *
625  * Input:
626  *      ha = adapter block pointer.
627  *      fwopt = pointer for firmware options.
628  *
629  * Returns:
630  *      qla2x00 local function return status code.
631  *
632  * Context:
633  *      Kernel context.
634  */
635 int
636 qla2x00_set_fw_options(scsi_qla_host_t *ha, uint16_t *fwopts)
637 {
638         int rval;
639         mbx_cmd_t mc;
640         mbx_cmd_t *mcp = &mc;
641
642         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
643
644         mcp->mb[0] = MBC_SET_FIRMWARE_OPTION;
645         mcp->mb[1] = fwopts[1];
646         mcp->mb[2] = fwopts[2];
647         mcp->mb[3] = fwopts[3];
648         mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
649         mcp->in_mb = MBX_0;
650         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
651                 mcp->in_mb |= MBX_1;
652         } else {
653                 mcp->mb[10] = fwopts[10];
654                 mcp->mb[11] = fwopts[11];
655                 mcp->mb[12] = 0;        /* Undocumented, but used */
656                 mcp->out_mb |= MBX_12|MBX_11|MBX_10;
657         }
658         mcp->tov = 30;
659         mcp->flags = 0;
660         rval = qla2x00_mailbox_command(ha, mcp);
661
662         fwopts[0] = mcp->mb[0];
663
664         if (rval != QLA_SUCCESS) {
665                 /*EMPTY*/
666                 DEBUG2_3_11(printk("%s(%ld): failed=%x (%x/%x).\n", __func__,
667                     ha->host_no, rval, mcp->mb[0], mcp->mb[1]));
668         } else {
669                 /*EMPTY*/
670                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
671         }
672
673         return rval;
674 }
675
676 /*
677  * qla2x00_mbx_reg_test
678  *      Mailbox register wrap test.
679  *
680  * Input:
681  *      ha = adapter block pointer.
682  *      TARGET_QUEUE_LOCK must be released.
683  *      ADAPTER_STATE_LOCK must be released.
684  *
685  * Returns:
686  *      qla2x00 local function return status code.
687  *
688  * Context:
689  *      Kernel context.
690  */
691 int
692 qla2x00_mbx_reg_test(scsi_qla_host_t *ha)
693 {
694         int rval;
695         mbx_cmd_t mc;
696         mbx_cmd_t *mcp = &mc;
697
698         DEBUG11(printk("qla2x00_mbx_reg_test(%ld): entered.\n", ha->host_no);)
699
700         mcp->mb[0] = MBC_MAILBOX_REGISTER_TEST;
701         mcp->mb[1] = 0xAAAA;
702         mcp->mb[2] = 0x5555;
703         mcp->mb[3] = 0xAA55;
704         mcp->mb[4] = 0x55AA;
705         mcp->mb[5] = 0xA5A5;
706         mcp->mb[6] = 0x5A5A;
707         mcp->mb[7] = 0x2525;
708         mcp->out_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
709         mcp->in_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
710         mcp->tov = 30;
711         mcp->flags = 0;
712         rval = qla2x00_mailbox_command(ha, mcp);
713
714         if (rval == QLA_SUCCESS) {
715                 if (mcp->mb[1] != 0xAAAA || mcp->mb[2] != 0x5555 ||
716                     mcp->mb[3] != 0xAA55 || mcp->mb[4] != 0x55AA)
717                         rval = QLA_FUNCTION_FAILED;
718                 if (mcp->mb[5] != 0xA5A5 || mcp->mb[6] != 0x5A5A ||
719                     mcp->mb[7] != 0x2525)
720                         rval = QLA_FUNCTION_FAILED;
721         }
722
723         if (rval != QLA_SUCCESS) {
724                 /*EMPTY*/
725                 DEBUG2_3_11(printk("qla2x00_mbx_reg_test(%ld): failed=%x.\n",
726                     ha->host_no, rval);)
727         } else {
728                 /*EMPTY*/
729                 DEBUG11(printk("qla2x00_mbx_reg_test(%ld): done.\n",
730                     ha->host_no);)
731         }
732
733         return rval;
734 }
735
736 /*
737  * qla2x00_verify_checksum
738  *      Verify firmware checksum.
739  *
740  * Input:
741  *      ha = adapter block pointer.
742  *      TARGET_QUEUE_LOCK must be released.
743  *      ADAPTER_STATE_LOCK must be released.
744  *
745  * Returns:
746  *      qla2x00 local function return status code.
747  *
748  * Context:
749  *      Kernel context.
750  */
751 int
752 qla2x00_verify_checksum(scsi_qla_host_t *ha, uint32_t risc_addr)
753 {
754         int rval;
755         mbx_cmd_t mc;
756         mbx_cmd_t *mcp = &mc;
757
758         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
759
760         mcp->mb[0] = MBC_VERIFY_CHECKSUM;
761         mcp->out_mb = MBX_0;
762         mcp->in_mb = MBX_0;
763         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
764                 mcp->mb[1] = MSW(risc_addr);
765                 mcp->mb[2] = LSW(risc_addr);
766                 mcp->out_mb |= MBX_2|MBX_1;
767                 mcp->in_mb |= MBX_2|MBX_1;
768         } else {
769                 mcp->mb[1] = LSW(risc_addr);
770                 mcp->out_mb |= MBX_1;
771                 mcp->in_mb |= MBX_1;
772         }
773
774         mcp->tov = 30;
775         mcp->flags = 0;
776         rval = qla2x00_mailbox_command(ha, mcp);
777
778         if (rval != QLA_SUCCESS) {
779                 DEBUG2_3_11(printk("%s(%ld): failed=%x chk sum=%x.\n", __func__,
780                     ha->host_no, rval, (IS_QLA24XX(ha) || IS_QLA25XX(ha) ?
781                     (mcp->mb[2] << 16) | mcp->mb[1]: mcp->mb[1]));)
782         } else {
783                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
784         }
785
786         return rval;
787 }
788
789 /*
790  * qla2x00_issue_iocb
791  *      Issue IOCB using mailbox command
792  *
793  * Input:
794  *      ha = adapter state pointer.
795  *      buffer = buffer pointer.
796  *      phys_addr = physical address of buffer.
797  *      size = size of buffer.
798  *      TARGET_QUEUE_LOCK must be released.
799  *      ADAPTER_STATE_LOCK must be released.
800  *
801  * Returns:
802  *      qla2x00 local function return status code.
803  *
804  * Context:
805  *      Kernel context.
806  */
807 int
808 qla2x00_issue_iocb(scsi_qla_host_t *ha, void*  buffer, dma_addr_t phys_addr,
809     size_t size)
810 {
811         int             rval;
812         mbx_cmd_t       mc;
813         mbx_cmd_t       *mcp = &mc;
814
815         mcp->mb[0] = MBC_IOCB_COMMAND_A64;
816         mcp->mb[1] = 0;
817         mcp->mb[2] = MSW(phys_addr);
818         mcp->mb[3] = LSW(phys_addr);
819         mcp->mb[6] = MSW(MSD(phys_addr));
820         mcp->mb[7] = LSW(MSD(phys_addr));
821         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
822         mcp->in_mb = MBX_2|MBX_0;
823         mcp->tov = 30;
824         mcp->flags = 0;
825         rval = qla2x00_mailbox_command(ha, mcp);
826
827         if (rval != QLA_SUCCESS) {
828                 /*EMPTY*/
829                 DEBUG(printk("qla2x00_issue_iocb(%ld): failed rval 0x%x\n",
830                     ha->host_no, rval);)
831                 DEBUG2(printk("qla2x00_issue_iocb(%ld): failed rval 0x%x\n",
832                     ha->host_no, rval);)
833         } else {
834                 sts_entry_t *sts_entry = (sts_entry_t *) buffer;
835
836                 /* Mask reserved bits. */
837                 sts_entry->entry_status &=
838                     IS_QLA24XX(ha) || IS_QLA25XX(ha) ? RF_MASK_24XX :RF_MASK;
839         }
840
841         return rval;
842 }
843
844 /*
845  * qla2x00_abort_command
846  *      Abort command aborts a specified IOCB.
847  *
848  * Input:
849  *      ha = adapter block pointer.
850  *      sp = SB structure pointer.
851  *
852  * Returns:
853  *      qla2x00 local function return status code.
854  *
855  * Context:
856  *      Kernel context.
857  */
858 int
859 qla2x00_abort_command(scsi_qla_host_t *ha, srb_t *sp)
860 {
861         unsigned long   flags = 0;
862         fc_port_t       *fcport;
863         int             rval;
864         uint32_t        handle;
865         mbx_cmd_t       mc;
866         mbx_cmd_t       *mcp = &mc;
867
868         DEBUG11(printk("qla2x00_abort_command(%ld): entered.\n", ha->host_no);)
869
870         fcport = sp->fcport;
871         if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
872             atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
873                 return 1;
874         }
875
876         spin_lock_irqsave(&ha->hardware_lock, flags);
877         for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
878                 if (ha->outstanding_cmds[handle] == sp)
879                         break;
880         }
881         spin_unlock_irqrestore(&ha->hardware_lock, flags);
882
883         if (handle == MAX_OUTSTANDING_COMMANDS) {
884                 /* command not found */
885                 return QLA_FUNCTION_FAILED;
886         }
887
888         mcp->mb[0] = MBC_ABORT_COMMAND;
889         if (HAS_EXTENDED_IDS(ha))
890                 mcp->mb[1] = fcport->loop_id;
891         else
892                 mcp->mb[1] = fcport->loop_id << 8;
893         mcp->mb[2] = (uint16_t)handle;
894         mcp->mb[3] = (uint16_t)(handle >> 16);
895         mcp->mb[6] = (uint16_t)sp->cmd->device->lun;
896         mcp->out_mb = MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
897         mcp->in_mb = MBX_0;
898         mcp->tov = 30;
899         mcp->flags = 0;
900         rval = qla2x00_mailbox_command(ha, mcp);
901
902         if (rval != QLA_SUCCESS) {
903                 DEBUG2_3_11(printk("qla2x00_abort_command(%ld): failed=%x.\n",
904                     ha->host_no, rval);)
905         } else {
906                 sp->flags |= SRB_ABORT_PENDING;
907                 DEBUG11(printk("qla2x00_abort_command(%ld): done.\n",
908                     ha->host_no);)
909         }
910
911         return rval;
912 }
913
914 #if USE_ABORT_TGT
915 /*
916  * qla2x00_abort_target
917  *      Issue abort target mailbox command.
918  *
919  * Input:
920  *      ha = adapter block pointer.
921  *
922  * Returns:
923  *      qla2x00 local function return status code.
924  *
925  * Context:
926  *      Kernel context.
927  */
928 int
929 qla2x00_abort_target(fc_port_t *fcport)
930 {
931         int        rval;
932         mbx_cmd_t  mc;
933         mbx_cmd_t  *mcp = &mc;
934         scsi_qla_host_t *ha;
935
936         if (fcport == NULL)
937                 return 0;
938
939         DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->ha->host_no);)
940
941         ha = fcport->ha;
942         mcp->mb[0] = MBC_ABORT_TARGET;
943         mcp->out_mb = MBX_2|MBX_1|MBX_0;
944         if (HAS_EXTENDED_IDS(ha)) {
945                 mcp->mb[1] = fcport->loop_id;
946                 mcp->mb[10] = 0;
947                 mcp->out_mb |= MBX_10;
948         } else {
949                 mcp->mb[1] = fcport->loop_id << 8;
950         }
951         mcp->mb[2] = ha->loop_reset_delay;
952
953         mcp->in_mb = MBX_0;
954         mcp->tov = 30;
955         mcp->flags = 0;
956         rval = qla2x00_mailbox_command(ha, mcp);
957
958         /* Issue marker command. */
959         ha->marker_needed = 1;
960
961         if (rval != QLA_SUCCESS) {
962                 DEBUG2_3_11(printk("qla2x00_abort_target(%ld): failed=%x.\n",
963                     ha->host_no, rval);)
964         } else {
965                 /*EMPTY*/
966                 DEBUG11(printk("qla2x00_abort_target(%ld): done.\n",
967                     ha->host_no);)
968         }
969
970         return rval;
971 }
972 #endif
973
974 /*
975  * qla2x00_get_adapter_id
976  *      Get adapter ID and topology.
977  *
978  * Input:
979  *      ha = adapter block pointer.
980  *      id = pointer for loop ID.
981  *      al_pa = pointer for AL_PA.
982  *      area = pointer for area.
983  *      domain = pointer for domain.
984  *      top = pointer for topology.
985  *      TARGET_QUEUE_LOCK must be released.
986  *      ADAPTER_STATE_LOCK must be released.
987  *
988  * Returns:
989  *      qla2x00 local function return status code.
990  *
991  * Context:
992  *      Kernel context.
993  */
994 int
995 qla2x00_get_adapter_id(scsi_qla_host_t *ha, uint16_t *id, uint8_t *al_pa,
996     uint8_t *area, uint8_t *domain, uint16_t *top)
997 {
998         int rval;
999         mbx_cmd_t mc;
1000         mbx_cmd_t *mcp = &mc;
1001
1002         DEBUG11(printk("qla2x00_get_adapter_id(%ld): entered.\n",
1003             ha->host_no);)
1004
1005         mcp->mb[0] = MBC_GET_ADAPTER_LOOP_ID;
1006         mcp->out_mb = MBX_0;
1007         mcp->in_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1008         mcp->tov = 30;
1009         mcp->flags = 0;
1010         rval = qla2x00_mailbox_command(ha, mcp);
1011         if (mcp->mb[0] == MBS_COMMAND_ERROR)
1012                 rval = QLA_COMMAND_ERROR;
1013
1014         /* Return data. */
1015         *id = mcp->mb[1];
1016         *al_pa = LSB(mcp->mb[2]);
1017         *area = MSB(mcp->mb[2]);
1018         *domain = LSB(mcp->mb[3]);
1019         *top = mcp->mb[6];
1020
1021         if (rval != QLA_SUCCESS) {
1022                 /*EMPTY*/
1023                 DEBUG2_3_11(printk("qla2x00_get_adapter_id(%ld): failed=%x.\n",
1024                     ha->host_no, rval);)
1025         } else {
1026                 /*EMPTY*/
1027                 DEBUG11(printk("qla2x00_get_adapter_id(%ld): done.\n",
1028                     ha->host_no);)
1029         }
1030
1031         return rval;
1032 }
1033
1034 /*
1035  * qla2x00_get_retry_cnt
1036  *      Get current firmware login retry count and delay.
1037  *
1038  * Input:
1039  *      ha = adapter block pointer.
1040  *      retry_cnt = pointer to login retry count.
1041  *      tov = pointer to login timeout value.
1042  *
1043  * Returns:
1044  *      qla2x00 local function return status code.
1045  *
1046  * Context:
1047  *      Kernel context.
1048  */
1049 int
1050 qla2x00_get_retry_cnt(scsi_qla_host_t *ha, uint8_t *retry_cnt, uint8_t *tov,
1051     uint16_t *r_a_tov)
1052 {
1053         int rval;
1054         uint16_t ratov;
1055         mbx_cmd_t mc;
1056         mbx_cmd_t *mcp = &mc;
1057
1058         DEBUG11(printk("qla2x00_get_retry_cnt(%ld): entered.\n",
1059                         ha->host_no);)
1060
1061         mcp->mb[0] = MBC_GET_RETRY_COUNT;
1062         mcp->out_mb = MBX_0;
1063         mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1064         mcp->tov = 30;
1065         mcp->flags = 0;
1066         rval = qla2x00_mailbox_command(ha, mcp);
1067
1068         if (rval != QLA_SUCCESS) {
1069                 /*EMPTY*/
1070                 DEBUG2_3_11(printk("qla2x00_get_retry_cnt(%ld): failed = %x.\n",
1071                     ha->host_no, mcp->mb[0]);)
1072         } else {
1073                 /* Convert returned data and check our values. */
1074                 *r_a_tov = mcp->mb[3] / 2;
1075                 ratov = (mcp->mb[3]/2) / 10;  /* mb[3] value is in 100ms */
1076                 if (mcp->mb[1] * ratov > (*retry_cnt) * (*tov)) {
1077                         /* Update to the larger values */
1078                         *retry_cnt = (uint8_t)mcp->mb[1];
1079                         *tov = ratov;
1080                 }
1081
1082                 DEBUG11(printk("qla2x00_get_retry_cnt(%ld): done. mb3=%d "
1083                     "ratov=%d.\n", ha->host_no, mcp->mb[3], ratov);)
1084         }
1085
1086         return rval;
1087 }
1088
1089 /*
1090  * qla2x00_init_firmware
1091  *      Initialize adapter firmware.
1092  *
1093  * Input:
1094  *      ha = adapter block pointer.
1095  *      dptr = Initialization control block pointer.
1096  *      size = size of initialization control block.
1097  *      TARGET_QUEUE_LOCK must be released.
1098  *      ADAPTER_STATE_LOCK must be released.
1099  *
1100  * Returns:
1101  *      qla2x00 local function return status code.
1102  *
1103  * Context:
1104  *      Kernel context.
1105  */
1106 int
1107 qla2x00_init_firmware(scsi_qla_host_t *ha, uint16_t size)
1108 {
1109         int rval;
1110         mbx_cmd_t mc;
1111         mbx_cmd_t *mcp = &mc;
1112
1113         DEBUG11(printk("qla2x00_init_firmware(%ld): entered.\n",
1114             ha->host_no);)
1115
1116         mcp->mb[0] = MBC_INITIALIZE_FIRMWARE;
1117         mcp->mb[2] = MSW(ha->init_cb_dma);
1118         mcp->mb[3] = LSW(ha->init_cb_dma);
1119         mcp->mb[4] = 0;
1120         mcp->mb[5] = 0;
1121         mcp->mb[6] = MSW(MSD(ha->init_cb_dma));
1122         mcp->mb[7] = LSW(MSD(ha->init_cb_dma));
1123         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1124         mcp->in_mb = MBX_5|MBX_4|MBX_0;
1125         mcp->buf_size = size;
1126         mcp->flags = MBX_DMA_OUT;
1127         mcp->tov = 30;
1128         rval = qla2x00_mailbox_command(ha, mcp);
1129
1130         if (rval != QLA_SUCCESS) {
1131                 /*EMPTY*/
1132                 DEBUG2_3_11(printk("qla2x00_init_firmware(%ld): failed=%x "
1133                     "mb0=%x.\n",
1134                     ha->host_no, rval, mcp->mb[0]);)
1135         } else {
1136                 /*EMPTY*/
1137                 DEBUG11(printk("qla2x00_init_firmware(%ld): done.\n",
1138                     ha->host_no);)
1139         }
1140
1141         return rval;
1142 }
1143
1144 /*
1145  * qla2x00_get_port_database
1146  *      Issue normal/enhanced get port database mailbox command
1147  *      and copy device name as necessary.
1148  *
1149  * Input:
1150  *      ha = adapter state pointer.
1151  *      dev = structure pointer.
1152  *      opt = enhanced cmd option byte.
1153  *
1154  * Returns:
1155  *      qla2x00 local function return status code.
1156  *
1157  * Context:
1158  *      Kernel context.
1159  */
1160 int
1161 qla2x00_get_port_database(scsi_qla_host_t *ha, fc_port_t *fcport, uint8_t opt)
1162 {
1163         int rval;
1164         mbx_cmd_t mc;
1165         mbx_cmd_t *mcp = &mc;
1166         port_database_t *pd;
1167         struct port_database_24xx *pd24;
1168         dma_addr_t pd_dma;
1169
1170         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1171
1172         pd24 = NULL;
1173         pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1174         if (pd  == NULL) {
1175                 DEBUG2_3(printk("%s(%ld): failed to allocate Port Database "
1176                     "structure.\n", __func__, ha->host_no));
1177                 return QLA_MEMORY_ALLOC_FAILED;
1178         }
1179         memset(pd, 0, max(PORT_DATABASE_SIZE, PORT_DATABASE_24XX_SIZE));
1180
1181         mcp->mb[0] = MBC_GET_PORT_DATABASE;
1182         if (opt != 0 && !IS_QLA24XX(ha) && !IS_QLA25XX(ha))
1183                 mcp->mb[0] = MBC_ENHANCED_GET_PORT_DATABASE;
1184         mcp->mb[2] = MSW(pd_dma);
1185         mcp->mb[3] = LSW(pd_dma);
1186         mcp->mb[6] = MSW(MSD(pd_dma));
1187         mcp->mb[7] = LSW(MSD(pd_dma));
1188         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1189         mcp->in_mb = MBX_0;
1190         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1191                 mcp->mb[1] = fcport->loop_id;
1192                 mcp->mb[10] = opt;
1193                 mcp->out_mb |= MBX_10|MBX_1;
1194                 mcp->in_mb |= MBX_1;
1195         } else if (HAS_EXTENDED_IDS(ha)) {
1196                 mcp->mb[1] = fcport->loop_id;
1197                 mcp->mb[10] = opt;
1198                 mcp->out_mb |= MBX_10|MBX_1;
1199         } else {
1200                 mcp->mb[1] = fcport->loop_id << 8 | opt;
1201                 mcp->out_mb |= MBX_1;
1202         }
1203         mcp->buf_size = (IS_QLA24XX(ha) || IS_QLA25XX(ha) ?
1204             PORT_DATABASE_24XX_SIZE : PORT_DATABASE_SIZE);
1205         mcp->flags = MBX_DMA_IN;
1206         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1207         rval = qla2x00_mailbox_command(ha, mcp);
1208         if (rval != QLA_SUCCESS)
1209                 goto gpd_error_out;
1210
1211         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1212                 pd24 = (struct port_database_24xx *) pd;
1213
1214                 /* Check for logged in state. */
1215                 if (pd24->current_login_state != PDS_PRLI_COMPLETE &&
1216                     pd24->last_login_state != PDS_PRLI_COMPLETE) {
1217                         DEBUG2(printk("%s(%ld): Unable to verify "
1218                             "login-state (%x/%x) for loop_id %x\n",
1219                             __func__, ha->host_no,
1220                             pd24->current_login_state,
1221                             pd24->last_login_state, fcport->loop_id));
1222                         rval = QLA_FUNCTION_FAILED;
1223                         goto gpd_error_out;
1224                 }
1225
1226                 /* Names are little-endian. */
1227                 memcpy(fcport->node_name, pd24->node_name, WWN_SIZE);
1228                 memcpy(fcport->port_name, pd24->port_name, WWN_SIZE);
1229
1230                 /* Get port_id of device. */
1231                 fcport->d_id.b.domain = pd24->port_id[0];
1232                 fcport->d_id.b.area = pd24->port_id[1];
1233                 fcport->d_id.b.al_pa = pd24->port_id[2];
1234                 fcport->d_id.b.rsvd_1 = 0;
1235
1236                 /* If not target must be initiator or unknown type. */
1237                 if ((pd24->prli_svc_param_word_3[0] & BIT_4) == 0)
1238                         fcport->port_type = FCT_INITIATOR;
1239                 else
1240                         fcport->port_type = FCT_TARGET;
1241         } else {
1242                 /* Check for logged in state. */
1243                 if (pd->master_state != PD_STATE_PORT_LOGGED_IN &&
1244                     pd->slave_state != PD_STATE_PORT_LOGGED_IN) {
1245                         rval = QLA_FUNCTION_FAILED;
1246                         goto gpd_error_out;
1247                 }
1248
1249                 /* Names are little-endian. */
1250                 memcpy(fcport->node_name, pd->node_name, WWN_SIZE);
1251                 memcpy(fcport->port_name, pd->port_name, WWN_SIZE);
1252
1253                 /* Get port_id of device. */
1254                 fcport->d_id.b.domain = pd->port_id[0];
1255                 fcport->d_id.b.area = pd->port_id[3];
1256                 fcport->d_id.b.al_pa = pd->port_id[2];
1257                 fcport->d_id.b.rsvd_1 = 0;
1258
1259                 /* Check for device require authentication. */
1260                 pd->common_features & BIT_5 ? (fcport->flags |= FCF_AUTH_REQ) :
1261                     (fcport->flags &= ~FCF_AUTH_REQ);
1262
1263                 /* If not target must be initiator or unknown type. */
1264                 if ((pd->prli_svc_param_word_3[0] & BIT_4) == 0)
1265                         fcport->port_type = FCT_INITIATOR;
1266                 else
1267                         fcport->port_type = FCT_TARGET;
1268
1269                 /* Passback COS information. */
1270                 fcport->supported_classes = (pd->options & BIT_4) ?
1271                     FC_COS_CLASS2: FC_COS_CLASS3;
1272         }
1273
1274 gpd_error_out:
1275         dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1276
1277         if (rval != QLA_SUCCESS) {
1278                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
1279                     __func__, ha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1280         } else {
1281                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
1282         }
1283
1284         return rval;
1285 }
1286
1287 /*
1288  * qla2x00_get_firmware_state
1289  *      Get adapter firmware state.
1290  *
1291  * Input:
1292  *      ha = adapter block pointer.
1293  *      dptr = pointer for firmware state.
1294  *      TARGET_QUEUE_LOCK must be released.
1295  *      ADAPTER_STATE_LOCK must be released.
1296  *
1297  * Returns:
1298  *      qla2x00 local function return status code.
1299  *
1300  * Context:
1301  *      Kernel context.
1302  */
1303 int
1304 qla2x00_get_firmware_state(scsi_qla_host_t *ha, uint16_t *dptr)
1305 {
1306         int rval;
1307         mbx_cmd_t mc;
1308         mbx_cmd_t *mcp = &mc;
1309
1310         DEBUG11(printk("qla2x00_get_firmware_state(%ld): entered.\n",
1311             ha->host_no);)
1312
1313         mcp->mb[0] = MBC_GET_FIRMWARE_STATE;
1314         mcp->out_mb = MBX_0;
1315         mcp->in_mb = MBX_2|MBX_1|MBX_0;
1316         mcp->tov = 30;
1317         mcp->flags = 0;
1318         rval = qla2x00_mailbox_command(ha, mcp);
1319
1320         /* Return firmware state. */
1321         *dptr = mcp->mb[1];
1322
1323         if (rval != QLA_SUCCESS) {
1324                 /*EMPTY*/
1325                 DEBUG2_3_11(printk("qla2x00_get_firmware_state(%ld): "
1326                     "failed=%x.\n", ha->host_no, rval);)
1327         } else {
1328                 /*EMPTY*/
1329                 DEBUG11(printk("qla2x00_get_firmware_state(%ld): done.\n",
1330                     ha->host_no);)
1331         }
1332
1333         return rval;
1334 }
1335
1336 /*
1337  * qla2x00_get_port_name
1338  *      Issue get port name mailbox command.
1339  *      Returned name is in big endian format.
1340  *
1341  * Input:
1342  *      ha = adapter block pointer.
1343  *      loop_id = loop ID of device.
1344  *      name = pointer for name.
1345  *      TARGET_QUEUE_LOCK must be released.
1346  *      ADAPTER_STATE_LOCK must be released.
1347  *
1348  * Returns:
1349  *      qla2x00 local function return status code.
1350  *
1351  * Context:
1352  *      Kernel context.
1353  */
1354 int
1355 qla2x00_get_port_name(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t *name,
1356     uint8_t opt)
1357 {
1358         int rval;
1359         mbx_cmd_t mc;
1360         mbx_cmd_t *mcp = &mc;
1361
1362         DEBUG11(printk("qla2x00_get_port_name(%ld): entered.\n",
1363             ha->host_no);)
1364
1365         mcp->mb[0] = MBC_GET_PORT_NAME;
1366         mcp->out_mb = MBX_1|MBX_0;
1367         if (HAS_EXTENDED_IDS(ha)) {
1368                 mcp->mb[1] = loop_id;
1369                 mcp->mb[10] = opt;
1370                 mcp->out_mb |= MBX_10;
1371         } else {
1372                 mcp->mb[1] = loop_id << 8 | opt;
1373         }
1374
1375         mcp->in_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1376         mcp->tov = 30;
1377         mcp->flags = 0;
1378         rval = qla2x00_mailbox_command(ha, mcp);
1379
1380         if (rval != QLA_SUCCESS) {
1381                 /*EMPTY*/
1382                 DEBUG2_3_11(printk("qla2x00_get_port_name(%ld): failed=%x.\n",
1383                     ha->host_no, rval);)
1384         } else {
1385                 if (name != NULL) {
1386                         /* This function returns name in big endian. */
1387                         name[0] = LSB(mcp->mb[2]);
1388                         name[1] = MSB(mcp->mb[2]);
1389                         name[2] = LSB(mcp->mb[3]);
1390                         name[3] = MSB(mcp->mb[3]);
1391                         name[4] = LSB(mcp->mb[6]);
1392                         name[5] = MSB(mcp->mb[6]);
1393                         name[6] = LSB(mcp->mb[7]);
1394                         name[7] = MSB(mcp->mb[7]);
1395                 }
1396
1397                 DEBUG11(printk("qla2x00_get_port_name(%ld): done.\n",
1398                     ha->host_no);)
1399         }
1400
1401         return rval;
1402 }
1403
1404 /*
1405  * qla2x00_lip_reset
1406  *      Issue LIP reset mailbox command.
1407  *
1408  * Input:
1409  *      ha = adapter block pointer.
1410  *      TARGET_QUEUE_LOCK must be released.
1411  *      ADAPTER_STATE_LOCK must be released.
1412  *
1413  * Returns:
1414  *      qla2x00 local function return status code.
1415  *
1416  * Context:
1417  *      Kernel context.
1418  */
1419 int
1420 qla2x00_lip_reset(scsi_qla_host_t *ha)
1421 {
1422         int rval;
1423         mbx_cmd_t mc;
1424         mbx_cmd_t *mcp = &mc;
1425
1426         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1427
1428         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1429                 mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1430                 mcp->mb[1] = BIT_0;
1431                 mcp->mb[2] = 0xff;
1432                 mcp->mb[3] = 0;
1433                 mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1434         } else {
1435                 mcp->mb[0] = MBC_LIP_RESET;
1436                 mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1437                 if (HAS_EXTENDED_IDS(ha)) {
1438                         mcp->mb[1] = 0x00ff;
1439                         mcp->mb[10] = 0;
1440                         mcp->out_mb |= MBX_10;
1441                 } else {
1442                         mcp->mb[1] = 0xff00;
1443                 }
1444                 mcp->mb[2] = ha->loop_reset_delay;
1445                 mcp->mb[3] = 0;
1446         }
1447         mcp->in_mb = MBX_0;
1448         mcp->tov = 30;
1449         mcp->flags = 0;
1450         rval = qla2x00_mailbox_command(ha, mcp);
1451
1452         if (rval != QLA_SUCCESS) {
1453                 /*EMPTY*/
1454                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n",
1455                     __func__, ha->host_no, rval);)
1456         } else {
1457                 /*EMPTY*/
1458                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
1459         }
1460
1461         return rval;
1462 }
1463
1464 /*
1465  * qla2x00_send_sns
1466  *      Send SNS command.
1467  *
1468  * Input:
1469  *      ha = adapter block pointer.
1470  *      sns = pointer for command.
1471  *      cmd_size = command size.
1472  *      buf_size = response/command size.
1473  *      TARGET_QUEUE_LOCK must be released.
1474  *      ADAPTER_STATE_LOCK must be released.
1475  *
1476  * Returns:
1477  *      qla2x00 local function return status code.
1478  *
1479  * Context:
1480  *      Kernel context.
1481  */
1482 int
1483 qla2x00_send_sns(scsi_qla_host_t *ha, dma_addr_t sns_phys_address,
1484     uint16_t cmd_size, size_t buf_size)
1485 {
1486         int rval;
1487         mbx_cmd_t mc;
1488         mbx_cmd_t *mcp = &mc;
1489
1490         DEBUG11(printk("qla2x00_send_sns(%ld): entered.\n",
1491             ha->host_no);)
1492
1493         DEBUG11(printk("qla2x00_send_sns: retry cnt=%d ratov=%d total "
1494             "tov=%d.\n", ha->retry_count, ha->login_timeout, mcp->tov);)
1495
1496         mcp->mb[0] = MBC_SEND_SNS_COMMAND;
1497         mcp->mb[1] = cmd_size;
1498         mcp->mb[2] = MSW(sns_phys_address);
1499         mcp->mb[3] = LSW(sns_phys_address);
1500         mcp->mb[6] = MSW(MSD(sns_phys_address));
1501         mcp->mb[7] = LSW(MSD(sns_phys_address));
1502         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1503         mcp->in_mb = MBX_0|MBX_1;
1504         mcp->buf_size = buf_size;
1505         mcp->flags = MBX_DMA_OUT|MBX_DMA_IN;
1506         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1507         rval = qla2x00_mailbox_command(ha, mcp);
1508
1509         if (rval != QLA_SUCCESS) {
1510                 /*EMPTY*/
1511                 DEBUG(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1512                     "mb[1]=%x.\n", ha->host_no, rval, mcp->mb[0], mcp->mb[1]);)
1513                 DEBUG2_3_11(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1514                     "mb[1]=%x.\n", ha->host_no, rval, mcp->mb[0], mcp->mb[1]);)
1515         } else {
1516                 /*EMPTY*/
1517                 DEBUG11(printk("qla2x00_send_sns(%ld): done.\n", ha->host_no);)
1518         }
1519
1520         return rval;
1521 }
1522
1523 int
1524 qla24xx_login_fabric(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1525     uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1526 {
1527         int             rval;
1528
1529         struct logio_entry_24xx *lg;
1530         dma_addr_t      lg_dma;
1531         uint32_t        iop[2];
1532
1533         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1534
1535         lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1536         if (lg == NULL) {
1537                 DEBUG2_3(printk("%s(%ld): failed to allocate Login IOCB.\n",
1538                     __func__, ha->host_no));
1539                 return QLA_MEMORY_ALLOC_FAILED;
1540         }
1541         memset(lg, 0, sizeof(struct logio_entry_24xx));
1542
1543         lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1544         lg->entry_count = 1;
1545         lg->nport_handle = cpu_to_le16(loop_id);
1546         lg->control_flags = __constant_cpu_to_le16(LCF_COMMAND_PLOGI);
1547         if (opt & BIT_0)
1548                 lg->control_flags |= __constant_cpu_to_le16(LCF_COND_PLOGI);
1549         lg->port_id[0] = al_pa;
1550         lg->port_id[1] = area;
1551         lg->port_id[2] = domain;
1552         rval = qla2x00_issue_iocb(ha, lg, lg_dma, 0);
1553         if (rval != QLA_SUCCESS) {
1554                 DEBUG2_3_11(printk("%s(%ld): failed to issue Login IOCB "
1555                     "(%x).\n", __func__, ha->host_no, rval);)
1556         } else if (lg->entry_status != 0) {
1557                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1558                     "-- error status (%x).\n", __func__, ha->host_no,
1559                     lg->entry_status));
1560                 rval = QLA_FUNCTION_FAILED;
1561         } else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1562                 iop[0] = le32_to_cpu(lg->io_parameter[0]);
1563                 iop[1] = le32_to_cpu(lg->io_parameter[1]);
1564
1565                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1566                     "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1567                     ha->host_no, le16_to_cpu(lg->comp_status), iop[0],
1568                     iop[1]));
1569
1570                 switch (iop[0]) {
1571                 case LSC_SCODE_PORTID_USED:
1572                         mb[0] = MBS_PORT_ID_USED;
1573                         mb[1] = LSW(iop[1]);
1574                         break;
1575                 case LSC_SCODE_NPORT_USED:
1576                         mb[0] = MBS_LOOP_ID_USED;
1577                         break;
1578                 case LSC_SCODE_NOLINK:
1579                 case LSC_SCODE_NOIOCB:
1580                 case LSC_SCODE_NOXCB:
1581                 case LSC_SCODE_CMD_FAILED:
1582                 case LSC_SCODE_NOFABRIC:
1583                 case LSC_SCODE_FW_NOT_READY:
1584                 case LSC_SCODE_NOT_LOGGED_IN:
1585                 case LSC_SCODE_NOPCB:
1586                 case LSC_SCODE_ELS_REJECT:
1587                 case LSC_SCODE_CMD_PARAM_ERR:
1588                 case LSC_SCODE_NONPORT:
1589                 case LSC_SCODE_LOGGED_IN:
1590                 case LSC_SCODE_NOFLOGI_ACC:
1591                 default:
1592                         mb[0] = MBS_COMMAND_ERROR;
1593                         break;
1594                 }
1595         } else {
1596                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
1597
1598                 iop[0] = le32_to_cpu(lg->io_parameter[0]);
1599
1600                 mb[0] = MBS_COMMAND_COMPLETE;
1601                 mb[1] = 0;
1602                 if (iop[0] & BIT_4) {
1603                         if (iop[0] & BIT_8)
1604                                 mb[1] |= BIT_1;
1605                 } else
1606                         mb[1] = BIT_0;
1607
1608                 /* Passback COS information. */
1609                 mb[10] = 0;
1610                 if (lg->io_parameter[7] || lg->io_parameter[8])
1611                         mb[10] |= BIT_0;        /* Class 2. */
1612                 if (lg->io_parameter[9] || lg->io_parameter[10])
1613                         mb[10] |= BIT_1;        /* Class 3. */
1614         }
1615
1616         dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1617
1618         return rval;
1619 }
1620
1621 /*
1622  * qla2x00_login_fabric
1623  *      Issue login fabric port mailbox command.
1624  *
1625  * Input:
1626  *      ha = adapter block pointer.
1627  *      loop_id = device loop ID.
1628  *      domain = device domain.
1629  *      area = device area.
1630  *      al_pa = device AL_PA.
1631  *      status = pointer for return status.
1632  *      opt = command options.
1633  *      TARGET_QUEUE_LOCK must be released.
1634  *      ADAPTER_STATE_LOCK must be released.
1635  *
1636  * Returns:
1637  *      qla2x00 local function return status code.
1638  *
1639  * Context:
1640  *      Kernel context.
1641  */
1642 int
1643 qla2x00_login_fabric(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1644     uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1645 {
1646         int rval;
1647         mbx_cmd_t mc;
1648         mbx_cmd_t *mcp = &mc;
1649
1650         DEBUG11(printk("qla2x00_login_fabric(%ld): entered.\n", ha->host_no);)
1651
1652         mcp->mb[0] = MBC_LOGIN_FABRIC_PORT;
1653         mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1654         if (HAS_EXTENDED_IDS(ha)) {
1655                 mcp->mb[1] = loop_id;
1656                 mcp->mb[10] = opt;
1657                 mcp->out_mb |= MBX_10;
1658         } else {
1659                 mcp->mb[1] = (loop_id << 8) | opt;
1660         }
1661         mcp->mb[2] = domain;
1662         mcp->mb[3] = area << 8 | al_pa;
1663
1664         mcp->in_mb = MBX_7|MBX_6|MBX_2|MBX_1|MBX_0;
1665         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1666         mcp->flags = 0;
1667         rval = qla2x00_mailbox_command(ha, mcp);
1668
1669         /* Return mailbox statuses. */
1670         if (mb != NULL) {
1671                 mb[0] = mcp->mb[0];
1672                 mb[1] = mcp->mb[1];
1673                 mb[2] = mcp->mb[2];
1674                 mb[6] = mcp->mb[6];
1675                 mb[7] = mcp->mb[7];
1676                 /* COS retrieved from Get-Port-Database mailbox command. */
1677                 mb[10] = 0;
1678         }
1679
1680         if (rval != QLA_SUCCESS) {
1681                 /* RLU tmp code: need to change main mailbox_command function to
1682                  * return ok even when the mailbox completion value is not
1683                  * SUCCESS. The caller needs to be responsible to interpret
1684                  * the return values of this mailbox command if we're not
1685                  * to change too much of the existing code.
1686                  */
1687                 if (mcp->mb[0] == 0x4001 || mcp->mb[0] == 0x4002 ||
1688                     mcp->mb[0] == 0x4003 || mcp->mb[0] == 0x4005 ||
1689                     mcp->mb[0] == 0x4006)
1690                         rval = QLA_SUCCESS;
1691
1692                 /*EMPTY*/
1693                 DEBUG2_3_11(printk("qla2x00_login_fabric(%ld): failed=%x "
1694                     "mb[0]=%x mb[1]=%x mb[2]=%x.\n", ha->host_no, rval,
1695                     mcp->mb[0], mcp->mb[1], mcp->mb[2]);)
1696         } else {
1697                 /*EMPTY*/
1698                 DEBUG11(printk("qla2x00_login_fabric(%ld): done.\n",
1699                     ha->host_no);)
1700         }
1701
1702         return rval;
1703 }
1704
1705 /*
1706  * qla2x00_login_local_device
1707  *           Issue login loop port mailbox command.
1708  *
1709  * Input:
1710  *           ha = adapter block pointer.
1711  *           loop_id = device loop ID.
1712  *           opt = command options.
1713  *
1714  * Returns:
1715  *            Return status code.
1716  *
1717  * Context:
1718  *            Kernel context.
1719  *
1720  */
1721 int
1722 qla2x00_login_local_device(scsi_qla_host_t *ha, uint16_t loop_id,
1723     uint16_t *mb_ret, uint8_t opt)
1724 {
1725         int rval;
1726         mbx_cmd_t mc;
1727         mbx_cmd_t *mcp = &mc;
1728
1729         DEBUG3(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1730
1731         mcp->mb[0] = MBC_LOGIN_LOOP_PORT;
1732         if (HAS_EXTENDED_IDS(ha))
1733                 mcp->mb[1] = loop_id;
1734         else
1735                 mcp->mb[1] = loop_id << 8;
1736         mcp->mb[2] = opt;
1737         mcp->out_mb = MBX_2|MBX_1|MBX_0;
1738         mcp->in_mb = MBX_7|MBX_6|MBX_1|MBX_0;
1739         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1740         mcp->flags = 0;
1741         rval = qla2x00_mailbox_command(ha, mcp);
1742
1743         /* Return mailbox statuses. */
1744         if (mb_ret != NULL) {
1745                 mb_ret[0] = mcp->mb[0];
1746                 mb_ret[1] = mcp->mb[1];
1747                 mb_ret[6] = mcp->mb[6];
1748                 mb_ret[7] = mcp->mb[7];
1749         }
1750
1751         if (rval != QLA_SUCCESS) {
1752                 /* AV tmp code: need to change main mailbox_command function to
1753                  * return ok even when the mailbox completion value is not
1754                  * SUCCESS. The caller needs to be responsible to interpret
1755                  * the return values of this mailbox command if we're not
1756                  * to change too much of the existing code.
1757                  */
1758                 if (mcp->mb[0] == 0x4005 || mcp->mb[0] == 0x4006)
1759                         rval = QLA_SUCCESS;
1760
1761                 DEBUG(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1762                     "mb[6]=%x mb[7]=%x.\n", __func__, ha->host_no, rval,
1763                     mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]);)
1764                 DEBUG2_3(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1765                     "mb[6]=%x mb[7]=%x.\n", __func__, ha->host_no, rval,
1766                     mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]);)
1767         } else {
1768                 /*EMPTY*/
1769                 DEBUG3(printk("%s(%ld): done.\n", __func__, ha->host_no);)
1770         }
1771
1772         return (rval);
1773 }
1774
1775 int
1776 qla24xx_fabric_logout(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1777     uint8_t area, uint8_t al_pa)
1778 {
1779         int             rval;
1780         struct logio_entry_24xx *lg;
1781         dma_addr_t      lg_dma;
1782
1783         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1784
1785         lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1786         if (lg == NULL) {
1787                 DEBUG2_3(printk("%s(%ld): failed to allocate Logout IOCB.\n",
1788                     __func__, ha->host_no));
1789                 return QLA_MEMORY_ALLOC_FAILED;
1790         }
1791         memset(lg, 0, sizeof(struct logio_entry_24xx));
1792
1793         lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1794         lg->entry_count = 1;
1795         lg->nport_handle = cpu_to_le16(loop_id);
1796         lg->control_flags =
1797             __constant_cpu_to_le16(LCF_COMMAND_LOGO|LCF_EXPL_LOGO);
1798         lg->port_id[0] = al_pa;
1799         lg->port_id[1] = area;
1800         lg->port_id[2] = domain;
1801         rval = qla2x00_issue_iocb(ha, lg, lg_dma, 0);
1802         if (rval != QLA_SUCCESS) {
1803                 DEBUG2_3_11(printk("%s(%ld): failed to issue Logout IOCB "
1804                     "(%x).\n", __func__, ha->host_no, rval);)
1805         } else if (lg->entry_status != 0) {
1806                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1807                     "-- error status (%x).\n", __func__, ha->host_no,
1808                     lg->entry_status));
1809                 rval = QLA_FUNCTION_FAILED;
1810         } else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1811                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1812                     "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1813                     ha->host_no, le16_to_cpu(lg->comp_status),
1814                     le32_to_cpu(lg->io_parameter[0]),
1815                     le32_to_cpu(lg->io_parameter[1]));)
1816         } else {
1817                 /*EMPTY*/
1818                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
1819         }
1820
1821         dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1822
1823         return rval;
1824 }
1825
1826 /*
1827  * qla2x00_fabric_logout
1828  *      Issue logout fabric port mailbox command.
1829  *
1830  * Input:
1831  *      ha = adapter block pointer.
1832  *      loop_id = device loop ID.
1833  *      TARGET_QUEUE_LOCK must be released.
1834  *      ADAPTER_STATE_LOCK must be released.
1835  *
1836  * Returns:
1837  *      qla2x00 local function return status code.
1838  *
1839  * Context:
1840  *      Kernel context.
1841  */
1842 int
1843 qla2x00_fabric_logout(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1844     uint8_t area, uint8_t al_pa)
1845 {
1846         int rval;
1847         mbx_cmd_t mc;
1848         mbx_cmd_t *mcp = &mc;
1849
1850         DEBUG11(printk("qla2x00_fabric_logout(%ld): entered.\n",
1851             ha->host_no);)
1852
1853         mcp->mb[0] = MBC_LOGOUT_FABRIC_PORT;
1854         mcp->out_mb = MBX_1|MBX_0;
1855         if (HAS_EXTENDED_IDS(ha)) {
1856                 mcp->mb[1] = loop_id;
1857                 mcp->mb[10] = 0;
1858                 mcp->out_mb |= MBX_10;
1859         } else {
1860                 mcp->mb[1] = loop_id << 8;
1861         }
1862
1863         mcp->in_mb = MBX_1|MBX_0;
1864         mcp->tov = 30;
1865         mcp->flags = 0;
1866         rval = qla2x00_mailbox_command(ha, mcp);
1867
1868         if (rval != QLA_SUCCESS) {
1869                 /*EMPTY*/
1870                 DEBUG2_3_11(printk("qla2x00_fabric_logout(%ld): failed=%x "
1871                     "mbx1=%x.\n", ha->host_no, rval, mcp->mb[1]);)
1872         } else {
1873                 /*EMPTY*/
1874                 DEBUG11(printk("qla2x00_fabric_logout(%ld): done.\n",
1875                     ha->host_no);)
1876         }
1877
1878         return rval;
1879 }
1880
1881 /*
1882  * qla2x00_full_login_lip
1883  *      Issue full login LIP mailbox command.
1884  *
1885  * Input:
1886  *      ha = adapter block pointer.
1887  *      TARGET_QUEUE_LOCK must be released.
1888  *      ADAPTER_STATE_LOCK must be released.
1889  *
1890  * Returns:
1891  *      qla2x00 local function return status code.
1892  *
1893  * Context:
1894  *      Kernel context.
1895  */
1896 int
1897 qla2x00_full_login_lip(scsi_qla_host_t *ha)
1898 {
1899         int rval;
1900         mbx_cmd_t mc;
1901         mbx_cmd_t *mcp = &mc;
1902
1903         DEBUG11(printk("qla2x00_full_login_lip(%ld): entered.\n",
1904             ha->host_no);)
1905
1906         mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1907         mcp->mb[1] = 0;
1908         mcp->mb[2] = 0xff;
1909         mcp->mb[3] = 0;
1910         mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1911         mcp->in_mb = MBX_0;
1912         mcp->tov = 30;
1913         mcp->flags = 0;
1914         rval = qla2x00_mailbox_command(ha, mcp);
1915
1916         if (rval != QLA_SUCCESS) {
1917                 /*EMPTY*/
1918                 DEBUG2_3_11(printk("qla2x00_full_login_lip(%ld): failed=%x.\n",
1919                     ha->host_no, rval);)
1920         } else {
1921                 /*EMPTY*/
1922                 DEBUG11(printk("qla2x00_full_login_lip(%ld): done.\n",
1923                     ha->host_no);)
1924         }
1925
1926         return rval;
1927 }
1928
1929 /*
1930  * qla2x00_get_id_list
1931  *
1932  * Input:
1933  *      ha = adapter block pointer.
1934  *
1935  * Returns:
1936  *      qla2x00 local function return status code.
1937  *
1938  * Context:
1939  *      Kernel context.
1940  */
1941 int
1942 qla2x00_get_id_list(scsi_qla_host_t *ha, void *id_list, dma_addr_t id_list_dma,
1943     uint16_t *entries)
1944 {
1945         int rval;
1946         mbx_cmd_t mc;
1947         mbx_cmd_t *mcp = &mc;
1948
1949         DEBUG11(printk("qla2x00_get_id_list(%ld): entered.\n",
1950             ha->host_no);)
1951
1952         if (id_list == NULL)
1953                 return QLA_FUNCTION_FAILED;
1954
1955         mcp->mb[0] = MBC_GET_ID_LIST;
1956         mcp->out_mb = MBX_0;
1957         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1958                 mcp->mb[2] = MSW(id_list_dma);
1959                 mcp->mb[3] = LSW(id_list_dma);
1960                 mcp->mb[6] = MSW(MSD(id_list_dma));
1961                 mcp->mb[7] = LSW(MSD(id_list_dma));
1962                 mcp->out_mb |= MBX_7|MBX_6|MBX_3|MBX_2;
1963         } else {
1964                 mcp->mb[1] = MSW(id_list_dma);
1965                 mcp->mb[2] = LSW(id_list_dma);
1966                 mcp->mb[3] = MSW(MSD(id_list_dma));
1967                 mcp->mb[6] = LSW(MSD(id_list_dma));
1968                 mcp->out_mb |= MBX_6|MBX_3|MBX_2|MBX_1;
1969         }
1970         mcp->in_mb = MBX_1|MBX_0;
1971         mcp->tov = 30;
1972         mcp->flags = 0;
1973         rval = qla2x00_mailbox_command(ha, mcp);
1974
1975         if (rval != QLA_SUCCESS) {
1976                 /*EMPTY*/
1977                 DEBUG2_3_11(printk("qla2x00_get_id_list(%ld): failed=%x.\n",
1978                     ha->host_no, rval);)
1979         } else {
1980                 *entries = mcp->mb[1];
1981                 DEBUG11(printk("qla2x00_get_id_list(%ld): done.\n",
1982                     ha->host_no);)
1983         }
1984
1985         return rval;
1986 }
1987
1988 /*
1989  * qla2x00_get_resource_cnts
1990  *      Get current firmware resource counts.
1991  *
1992  * Input:
1993  *      ha = adapter block pointer.
1994  *
1995  * Returns:
1996  *      qla2x00 local function return status code.
1997  *
1998  * Context:
1999  *      Kernel context.
2000  */
2001 int
2002 qla2x00_get_resource_cnts(scsi_qla_host_t *ha, uint16_t *cur_xchg_cnt,
2003     uint16_t *orig_xchg_cnt, uint16_t *cur_iocb_cnt, uint16_t *orig_iocb_cnt)
2004 {
2005         int rval;
2006         mbx_cmd_t mc;
2007         mbx_cmd_t *mcp = &mc;
2008
2009         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2010
2011         mcp->mb[0] = MBC_GET_RESOURCE_COUNTS;
2012         mcp->out_mb = MBX_0;
2013         mcp->in_mb = MBX_10|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
2014         mcp->tov = 30;
2015         mcp->flags = 0;
2016         rval = qla2x00_mailbox_command(ha, mcp);
2017
2018         if (rval != QLA_SUCCESS) {
2019                 /*EMPTY*/
2020                 DEBUG2_3_11(printk("%s(%ld): failed = %x.\n", __func__,
2021                     ha->host_no, mcp->mb[0]);)
2022         } else {
2023                 DEBUG11(printk("%s(%ld): done. mb1=%x mb2=%x mb3=%x mb6=%x "
2024                     "mb7=%x mb10=%x.\n", __func__, ha->host_no,
2025                     mcp->mb[1], mcp->mb[2], mcp->mb[3], mcp->mb[6], mcp->mb[7],
2026                     mcp->mb[10]));
2027
2028                 if (cur_xchg_cnt)
2029                         *cur_xchg_cnt = mcp->mb[3];
2030                 if (orig_xchg_cnt)
2031                         *orig_xchg_cnt = mcp->mb[6];
2032                 if (cur_iocb_cnt)
2033                         *cur_iocb_cnt = mcp->mb[7];
2034                 if (orig_iocb_cnt)
2035                         *orig_iocb_cnt = mcp->mb[10];
2036         }
2037
2038         return (rval);
2039 }
2040
2041 #if defined(QL_DEBUG_LEVEL_3)
2042 /*
2043  * qla2x00_get_fcal_position_map
2044  *      Get FCAL (LILP) position map using mailbox command
2045  *
2046  * Input:
2047  *      ha = adapter state pointer.
2048  *      pos_map = buffer pointer (can be NULL).
2049  *
2050  * Returns:
2051  *      qla2x00 local function return status code.
2052  *
2053  * Context:
2054  *      Kernel context.
2055  */
2056 int
2057 qla2x00_get_fcal_position_map(scsi_qla_host_t *ha, char *pos_map)
2058 {
2059         int rval;
2060         mbx_cmd_t mc;
2061         mbx_cmd_t *mcp = &mc;
2062         char *pmap;
2063         dma_addr_t pmap_dma;
2064
2065         pmap = dma_pool_alloc(ha->s_dma_pool, GFP_ATOMIC, &pmap_dma);
2066         if (pmap  == NULL) {
2067                 DEBUG2_3_11(printk("%s(%ld): **** Mem Alloc Failed ****",
2068                     __func__, ha->host_no));
2069                 return QLA_MEMORY_ALLOC_FAILED;
2070         }
2071         memset(pmap, 0, FCAL_MAP_SIZE);
2072
2073         mcp->mb[0] = MBC_GET_FC_AL_POSITION_MAP;
2074         mcp->mb[2] = MSW(pmap_dma);
2075         mcp->mb[3] = LSW(pmap_dma);
2076         mcp->mb[6] = MSW(MSD(pmap_dma));
2077         mcp->mb[7] = LSW(MSD(pmap_dma));
2078         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2079         mcp->in_mb = MBX_1|MBX_0;
2080         mcp->buf_size = FCAL_MAP_SIZE;
2081         mcp->flags = MBX_DMA_IN;
2082         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
2083         rval = qla2x00_mailbox_command(ha, mcp);
2084
2085         if (rval == QLA_SUCCESS) {
2086                 DEBUG11(printk("%s(%ld): (mb0=%x/mb1=%x) FC/AL Position Map "
2087                     "size (%x)\n", __func__, ha->host_no, mcp->mb[0],
2088                     mcp->mb[1], (unsigned)pmap[0]));
2089                 DEBUG11(qla2x00_dump_buffer(pmap, pmap[0] + 1));
2090
2091                 if (pos_map)
2092                         memcpy(pos_map, pmap, FCAL_MAP_SIZE);
2093         }
2094         dma_pool_free(ha->s_dma_pool, pmap, pmap_dma);
2095
2096         if (rval != QLA_SUCCESS) {
2097                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2098                     ha->host_no, rval));
2099         } else {
2100                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2101         }
2102
2103         return rval;
2104 }
2105
2106 uint8_t
2107 qla24xx_get_isp_stats(scsi_qla_host_t *ha, uint32_t *dwbuf, uint32_t dwords,
2108     uint16_t *status)
2109 {
2110         int rval;
2111         mbx_cmd_t mc;
2112         mbx_cmd_t *mcp = &mc;
2113         uint32_t *sbuf, *siter;
2114         dma_addr_t sbuf_dma;
2115
2116         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
2117
2118         if (dwords > (DMA_POOL_SIZE / 4)) {
2119                 DEBUG2_3_11(printk("%s(%ld): Unabled to retrieve %d DWORDs "
2120                     "(max %d).\n", __func__, ha->host_no, dwords,
2121                     DMA_POOL_SIZE / 4));
2122                 return BIT_0;
2123         }
2124         sbuf = dma_pool_alloc(ha->s_dma_pool, GFP_ATOMIC, &sbuf_dma);
2125         if (sbuf == NULL) {
2126                 DEBUG2_3_11(printk("%s(%ld): Failed to allocate memory.\n",
2127                     __func__, ha->host_no));
2128                 return BIT_0;
2129         }
2130         memset(sbuf, 0, DMA_POOL_SIZE);
2131
2132         mcp->mb[0] = MBC_GET_LINK_PRIV_STATS;
2133         mcp->mb[2] = MSW(sbuf_dma);
2134         mcp->mb[3] = LSW(sbuf_dma);
2135         mcp->mb[6] = MSW(MSD(sbuf_dma));
2136         mcp->mb[7] = LSW(MSD(sbuf_dma));
2137         mcp->mb[8] = dwords;
2138         mcp->mb[10] = 0;
2139         mcp->out_mb = MBX_10|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2140         mcp->in_mb = MBX_2|MBX_1|MBX_0;
2141         mcp->tov = 30;
2142         mcp->flags = IOCTL_CMD;
2143         rval = qla2x00_mailbox_command(ha, mcp);
2144
2145         if (rval == QLA_SUCCESS) {
2146                 if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2147                         DEBUG2_3_11(printk("%s(%ld): cmd failed. mbx0=%x.\n",
2148                             __func__, ha->host_no, mcp->mb[0]));
2149                         status[0] = mcp->mb[0];
2150                         rval = BIT_1;
2151                 } else {
2152                         /* Copy over data -- firmware data is LE. */
2153                         siter = sbuf;
2154                         while (dwords--)
2155                                 *dwbuf++ = le32_to_cpu(*siter++);
2156                 }
2157         } else {
2158                 /* Failed. */
2159                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2160                     ha->host_no, rval));
2161                 rval = BIT_1;
2162         }
2163
2164         dma_pool_free(ha->s_dma_pool, sbuf, sbuf_dma);
2165
2166         return rval;
2167 }
2168 #endif
2169
2170 int
2171 qla24xx_abort_command(scsi_qla_host_t *ha, srb_t *sp)
2172 {
2173         int             rval;
2174         fc_port_t       *fcport;
2175         unsigned long   flags = 0;
2176
2177         struct abort_entry_24xx *abt;
2178         dma_addr_t      abt_dma;
2179         uint32_t        handle;
2180
2181         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
2182
2183         fcport = sp->fcport;
2184         if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
2185             atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2186                 return QLA_FUNCTION_FAILED;
2187         }
2188
2189         spin_lock_irqsave(&ha->hardware_lock, flags);
2190         for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
2191                 if (ha->outstanding_cmds[handle] == sp)
2192                         break;
2193         }
2194         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2195         if (handle == MAX_OUTSTANDING_COMMANDS) {
2196                 /* Command not found. */
2197                 return QLA_FUNCTION_FAILED;
2198         }
2199
2200         abt = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &abt_dma);
2201         if (abt == NULL) {
2202                 DEBUG2_3(printk("%s(%ld): failed to allocate Abort IOCB.\n",
2203                     __func__, ha->host_no));
2204                 return QLA_MEMORY_ALLOC_FAILED;
2205         }
2206         memset(abt, 0, sizeof(struct abort_entry_24xx));
2207
2208         abt->entry_type = ABORT_IOCB_TYPE;
2209         abt->entry_count = 1;
2210         abt->nport_handle = cpu_to_le16(fcport->loop_id);
2211         abt->handle_to_abort = handle;
2212         abt->port_id[0] = fcport->d_id.b.al_pa;
2213         abt->port_id[1] = fcport->d_id.b.area;
2214         abt->port_id[2] = fcport->d_id.b.domain;
2215         rval = qla2x00_issue_iocb(ha, abt, abt_dma, 0);
2216         if (rval != QLA_SUCCESS) {
2217                 DEBUG2_3_11(printk("%s(%ld): failed to issue IOCB (%x).\n",
2218                     __func__, ha->host_no, rval);)
2219         } else if (abt->entry_status != 0) {
2220                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2221                     "-- error status (%x).\n", __func__, ha->host_no,
2222                     abt->entry_status));
2223                 rval = QLA_FUNCTION_FAILED;
2224         } else if (abt->nport_handle != __constant_cpu_to_le16(0)) {
2225                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2226                     "-- completion status (%x).\n", __func__, ha->host_no,
2227                     le16_to_cpu(abt->nport_handle));)
2228                 rval = QLA_FUNCTION_FAILED;
2229         } else {
2230                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
2231                 sp->flags |= SRB_ABORT_PENDING;
2232         }
2233
2234         dma_pool_free(ha->s_dma_pool, abt, abt_dma);
2235
2236         return rval;
2237 }
2238
2239 struct tsk_mgmt_cmd {
2240         union {
2241                 struct tsk_mgmt_entry tsk;
2242                 struct sts_entry_24xx sts;
2243         } p;
2244 };
2245
2246 int
2247 qla24xx_abort_target(fc_port_t *fcport)
2248 {
2249         int             rval;
2250         struct tsk_mgmt_cmd *tsk;
2251         dma_addr_t      tsk_dma;
2252         scsi_qla_host_t *ha;
2253
2254         if (fcport == NULL)
2255                 return 0;
2256
2257         DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->ha->host_no);)
2258
2259         ha = fcport->ha;
2260         tsk = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &tsk_dma);
2261         if (tsk == NULL) {
2262                 DEBUG2_3(printk("%s(%ld): failed to allocate Task Management "
2263                     "IOCB.\n", __func__, ha->host_no));
2264                 return QLA_MEMORY_ALLOC_FAILED;
2265         }
2266         memset(tsk, 0, sizeof(struct tsk_mgmt_cmd));
2267
2268         tsk->p.tsk.entry_type = TSK_MGMT_IOCB_TYPE;
2269         tsk->p.tsk.entry_count = 1;
2270         tsk->p.tsk.nport_handle = cpu_to_le16(fcport->loop_id);
2271         tsk->p.tsk.timeout = __constant_cpu_to_le16(25);
2272         tsk->p.tsk.control_flags = __constant_cpu_to_le32(TCF_TARGET_RESET);
2273         tsk->p.tsk.port_id[0] = fcport->d_id.b.al_pa;
2274         tsk->p.tsk.port_id[1] = fcport->d_id.b.area;
2275         tsk->p.tsk.port_id[2] = fcport->d_id.b.domain;
2276         rval = qla2x00_issue_iocb(ha, tsk, tsk_dma, 0);
2277         if (rval != QLA_SUCCESS) {
2278                 DEBUG2_3_11(printk("%s(%ld): failed to issue Target Reset IOCB "
2279                     "(%x).\n", __func__, ha->host_no, rval);)
2280                 goto atarget_done;
2281         } else if (tsk->p.sts.entry_status != 0) {
2282                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2283                     "-- error status (%x).\n", __func__, ha->host_no,
2284                     tsk->p.sts.entry_status));
2285                 rval = QLA_FUNCTION_FAILED;
2286                 goto atarget_done;
2287         } else if (tsk->p.sts.comp_status !=
2288             __constant_cpu_to_le16(CS_COMPLETE)) {
2289                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2290                     "-- completion status (%x).\n", __func__,
2291                     ha->host_no, le16_to_cpu(tsk->p.sts.comp_status));)
2292                 rval = QLA_FUNCTION_FAILED;
2293                 goto atarget_done;
2294         }
2295
2296         /* Issue marker IOCB. */
2297         rval = qla2x00_marker(ha, fcport->loop_id, 0, MK_SYNC_ID);
2298         if (rval != QLA_SUCCESS) {
2299                 DEBUG2_3_11(printk("%s(%ld): failed to issue Marker IOCB "
2300                     "(%x).\n", __func__, ha->host_no, rval);)
2301         } else {
2302                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
2303         }
2304
2305 atarget_done:
2306         dma_pool_free(ha->s_dma_pool, tsk, tsk_dma);
2307
2308         return rval;
2309 }
2310
2311 int
2312 qla2x00_system_error(scsi_qla_host_t *ha)
2313 {
2314         int rval;
2315         mbx_cmd_t mc;
2316         mbx_cmd_t *mcp = &mc;
2317
2318         if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha))
2319                 return QLA_FUNCTION_FAILED;
2320
2321         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2322
2323         mcp->mb[0] = MBC_GEN_SYSTEM_ERROR;
2324         mcp->out_mb = MBX_0;
2325         mcp->in_mb = MBX_0;
2326         mcp->tov = 5;
2327         mcp->flags = 0;
2328         rval = qla2x00_mailbox_command(ha, mcp);
2329
2330         if (rval != QLA_SUCCESS) {
2331                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2332                     ha->host_no, rval));
2333         } else {
2334                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2335         }
2336
2337         return rval;
2338 }
2339
2340 /**
2341  * qla2x00_get_serdes_params() -
2342  * @ha: HA context
2343  *
2344  * Returns
2345  */
2346 int
2347 qla2x00_get_serdes_params(scsi_qla_host_t *ha, uint16_t *sw_em_1g,
2348     uint16_t *sw_em_2g, uint16_t *sw_em_4g)
2349 {
2350         int rval;
2351         mbx_cmd_t mc;
2352         mbx_cmd_t *mcp = &mc;
2353
2354         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2355
2356         mcp->mb[0] = MBC_SERDES_PARAMS;
2357         mcp->mb[1] = 0;
2358         mcp->out_mb = MBX_1|MBX_0;
2359         mcp->in_mb = MBX_4|MBX_3|MBX_2|MBX_0;
2360         mcp->tov = 30;
2361         mcp->flags = 0;
2362         rval = qla2x00_mailbox_command(ha, mcp);
2363
2364         if (rval != QLA_SUCCESS) {
2365                 /*EMPTY*/
2366                 DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2367                     ha->host_no, rval, mcp->mb[0]));
2368         } else {
2369                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2370
2371                 if (sw_em_1g)
2372                         *sw_em_1g = mcp->mb[2];
2373                 if (sw_em_2g)
2374                         *sw_em_2g = mcp->mb[3];
2375                 if (sw_em_4g)
2376                         *sw_em_4g = mcp->mb[4];
2377         }
2378
2379         return rval;
2380 }
2381
2382 /**
2383  * qla2x00_set_serdes_params() -
2384  * @ha: HA context
2385  *
2386  * Returns
2387  */
2388 int
2389 qla2x00_set_serdes_params(scsi_qla_host_t *ha, uint16_t sw_em_1g,
2390     uint16_t sw_em_2g, uint16_t sw_em_4g)
2391 {
2392         int rval;
2393         mbx_cmd_t mc;
2394         mbx_cmd_t *mcp = &mc;
2395
2396         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2397
2398         mcp->mb[0] = MBC_SERDES_PARAMS;
2399         mcp->mb[1] = BIT_0;
2400         mcp->mb[2] = sw_em_1g;
2401         mcp->mb[3] = sw_em_2g;
2402         mcp->mb[4] = sw_em_4g;
2403         mcp->out_mb = MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2404         mcp->in_mb = MBX_0;
2405         mcp->tov = 30;
2406         mcp->flags = 0;
2407         rval = qla2x00_mailbox_command(ha, mcp);
2408
2409         if (rval != QLA_SUCCESS) {
2410                 /*EMPTY*/
2411                 DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2412                     ha->host_no, rval, mcp->mb[0]));
2413         } else {
2414                 /*EMPTY*/
2415                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2416         }
2417
2418         return rval;
2419 }
2420
2421 int
2422 qla2x00_stop_firmware(scsi_qla_host_t *ha)
2423 {
2424         int rval;
2425         mbx_cmd_t mc;
2426         mbx_cmd_t *mcp = &mc;
2427
2428         if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha))
2429                 return QLA_FUNCTION_FAILED;
2430
2431         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2432
2433         mcp->mb[0] = MBC_STOP_FIRMWARE;
2434         mcp->out_mb = MBX_0;
2435         mcp->in_mb = MBX_0;
2436         mcp->tov = 5;
2437         mcp->flags = 0;
2438         rval = qla2x00_mailbox_command(ha, mcp);
2439
2440         if (rval != QLA_SUCCESS) {
2441                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2442                     ha->host_no, rval));
2443         } else {
2444                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2445         }
2446
2447         return rval;
2448 }