]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/qla2xxx/qla_mbx.c
Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[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
872         spin_lock_irqsave(&ha->hardware_lock, flags);
873         for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
874                 if (ha->outstanding_cmds[handle] == sp)
875                         break;
876         }
877         spin_unlock_irqrestore(&ha->hardware_lock, flags);
878
879         if (handle == MAX_OUTSTANDING_COMMANDS) {
880                 /* command not found */
881                 return QLA_FUNCTION_FAILED;
882         }
883
884         mcp->mb[0] = MBC_ABORT_COMMAND;
885         if (HAS_EXTENDED_IDS(ha))
886                 mcp->mb[1] = fcport->loop_id;
887         else
888                 mcp->mb[1] = fcport->loop_id << 8;
889         mcp->mb[2] = (uint16_t)handle;
890         mcp->mb[3] = (uint16_t)(handle >> 16);
891         mcp->mb[6] = (uint16_t)sp->cmd->device->lun;
892         mcp->out_mb = MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
893         mcp->in_mb = MBX_0;
894         mcp->tov = 30;
895         mcp->flags = 0;
896         rval = qla2x00_mailbox_command(ha, mcp);
897
898         if (rval != QLA_SUCCESS) {
899                 DEBUG2_3_11(printk("qla2x00_abort_command(%ld): failed=%x.\n",
900                     ha->host_no, rval);)
901         } else {
902                 sp->flags |= SRB_ABORT_PENDING;
903                 DEBUG11(printk("qla2x00_abort_command(%ld): done.\n",
904                     ha->host_no);)
905         }
906
907         return rval;
908 }
909
910 #if USE_ABORT_TGT
911 /*
912  * qla2x00_abort_target
913  *      Issue abort target mailbox command.
914  *
915  * Input:
916  *      ha = adapter block pointer.
917  *
918  * Returns:
919  *      qla2x00 local function return status code.
920  *
921  * Context:
922  *      Kernel context.
923  */
924 int
925 qla2x00_abort_target(fc_port_t *fcport)
926 {
927         int        rval;
928         mbx_cmd_t  mc;
929         mbx_cmd_t  *mcp = &mc;
930         scsi_qla_host_t *ha;
931
932         if (fcport == NULL)
933                 return 0;
934
935         DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->ha->host_no);)
936
937         ha = fcport->ha;
938         mcp->mb[0] = MBC_ABORT_TARGET;
939         mcp->out_mb = MBX_2|MBX_1|MBX_0;
940         if (HAS_EXTENDED_IDS(ha)) {
941                 mcp->mb[1] = fcport->loop_id;
942                 mcp->mb[10] = 0;
943                 mcp->out_mb |= MBX_10;
944         } else {
945                 mcp->mb[1] = fcport->loop_id << 8;
946         }
947         mcp->mb[2] = ha->loop_reset_delay;
948
949         mcp->in_mb = MBX_0;
950         mcp->tov = 30;
951         mcp->flags = 0;
952         rval = qla2x00_mailbox_command(ha, mcp);
953
954         /* Issue marker command. */
955         ha->marker_needed = 1;
956
957         if (rval != QLA_SUCCESS) {
958                 DEBUG2_3_11(printk("qla2x00_abort_target(%ld): failed=%x.\n",
959                     ha->host_no, rval);)
960         } else {
961                 /*EMPTY*/
962                 DEBUG11(printk("qla2x00_abort_target(%ld): done.\n",
963                     ha->host_no);)
964         }
965
966         return rval;
967 }
968 #endif
969
970 /*
971  * qla2x00_get_adapter_id
972  *      Get adapter ID and topology.
973  *
974  * Input:
975  *      ha = adapter block pointer.
976  *      id = pointer for loop ID.
977  *      al_pa = pointer for AL_PA.
978  *      area = pointer for area.
979  *      domain = pointer for domain.
980  *      top = pointer for topology.
981  *      TARGET_QUEUE_LOCK must be released.
982  *      ADAPTER_STATE_LOCK must be released.
983  *
984  * Returns:
985  *      qla2x00 local function return status code.
986  *
987  * Context:
988  *      Kernel context.
989  */
990 int
991 qla2x00_get_adapter_id(scsi_qla_host_t *ha, uint16_t *id, uint8_t *al_pa,
992     uint8_t *area, uint8_t *domain, uint16_t *top)
993 {
994         int rval;
995         mbx_cmd_t mc;
996         mbx_cmd_t *mcp = &mc;
997
998         DEBUG11(printk("qla2x00_get_adapter_id(%ld): entered.\n",
999             ha->host_no);)
1000
1001         mcp->mb[0] = MBC_GET_ADAPTER_LOOP_ID;
1002         mcp->out_mb = MBX_0;
1003         mcp->in_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1004         mcp->tov = 30;
1005         mcp->flags = 0;
1006         rval = qla2x00_mailbox_command(ha, mcp);
1007         if (mcp->mb[0] == MBS_COMMAND_ERROR)
1008                 rval = QLA_COMMAND_ERROR;
1009
1010         /* Return data. */
1011         *id = mcp->mb[1];
1012         *al_pa = LSB(mcp->mb[2]);
1013         *area = MSB(mcp->mb[2]);
1014         *domain = LSB(mcp->mb[3]);
1015         *top = mcp->mb[6];
1016
1017         if (rval != QLA_SUCCESS) {
1018                 /*EMPTY*/
1019                 DEBUG2_3_11(printk("qla2x00_get_adapter_id(%ld): failed=%x.\n",
1020                     ha->host_no, rval);)
1021         } else {
1022                 /*EMPTY*/
1023                 DEBUG11(printk("qla2x00_get_adapter_id(%ld): done.\n",
1024                     ha->host_no);)
1025         }
1026
1027         return rval;
1028 }
1029
1030 /*
1031  * qla2x00_get_retry_cnt
1032  *      Get current firmware login retry count and delay.
1033  *
1034  * Input:
1035  *      ha = adapter block pointer.
1036  *      retry_cnt = pointer to login retry count.
1037  *      tov = pointer to login timeout value.
1038  *
1039  * Returns:
1040  *      qla2x00 local function return status code.
1041  *
1042  * Context:
1043  *      Kernel context.
1044  */
1045 int
1046 qla2x00_get_retry_cnt(scsi_qla_host_t *ha, uint8_t *retry_cnt, uint8_t *tov,
1047     uint16_t *r_a_tov)
1048 {
1049         int rval;
1050         uint16_t ratov;
1051         mbx_cmd_t mc;
1052         mbx_cmd_t *mcp = &mc;
1053
1054         DEBUG11(printk("qla2x00_get_retry_cnt(%ld): entered.\n",
1055                         ha->host_no);)
1056
1057         mcp->mb[0] = MBC_GET_RETRY_COUNT;
1058         mcp->out_mb = MBX_0;
1059         mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1060         mcp->tov = 30;
1061         mcp->flags = 0;
1062         rval = qla2x00_mailbox_command(ha, mcp);
1063
1064         if (rval != QLA_SUCCESS) {
1065                 /*EMPTY*/
1066                 DEBUG2_3_11(printk("qla2x00_get_retry_cnt(%ld): failed = %x.\n",
1067                     ha->host_no, mcp->mb[0]);)
1068         } else {
1069                 /* Convert returned data and check our values. */
1070                 *r_a_tov = mcp->mb[3] / 2;
1071                 ratov = (mcp->mb[3]/2) / 10;  /* mb[3] value is in 100ms */
1072                 if (mcp->mb[1] * ratov > (*retry_cnt) * (*tov)) {
1073                         /* Update to the larger values */
1074                         *retry_cnt = (uint8_t)mcp->mb[1];
1075                         *tov = ratov;
1076                 }
1077
1078                 DEBUG11(printk("qla2x00_get_retry_cnt(%ld): done. mb3=%d "
1079                     "ratov=%d.\n", ha->host_no, mcp->mb[3], ratov);)
1080         }
1081
1082         return rval;
1083 }
1084
1085 /*
1086  * qla2x00_init_firmware
1087  *      Initialize adapter firmware.
1088  *
1089  * Input:
1090  *      ha = adapter block pointer.
1091  *      dptr = Initialization control block pointer.
1092  *      size = size of initialization control block.
1093  *      TARGET_QUEUE_LOCK must be released.
1094  *      ADAPTER_STATE_LOCK must be released.
1095  *
1096  * Returns:
1097  *      qla2x00 local function return status code.
1098  *
1099  * Context:
1100  *      Kernel context.
1101  */
1102 int
1103 qla2x00_init_firmware(scsi_qla_host_t *ha, uint16_t size)
1104 {
1105         int rval;
1106         mbx_cmd_t mc;
1107         mbx_cmd_t *mcp = &mc;
1108
1109         DEBUG11(printk("qla2x00_init_firmware(%ld): entered.\n",
1110             ha->host_no);)
1111
1112         mcp->mb[0] = MBC_INITIALIZE_FIRMWARE;
1113         mcp->mb[2] = MSW(ha->init_cb_dma);
1114         mcp->mb[3] = LSW(ha->init_cb_dma);
1115         mcp->mb[4] = 0;
1116         mcp->mb[5] = 0;
1117         mcp->mb[6] = MSW(MSD(ha->init_cb_dma));
1118         mcp->mb[7] = LSW(MSD(ha->init_cb_dma));
1119         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1120         mcp->in_mb = MBX_5|MBX_4|MBX_0;
1121         mcp->buf_size = size;
1122         mcp->flags = MBX_DMA_OUT;
1123         mcp->tov = 30;
1124         rval = qla2x00_mailbox_command(ha, mcp);
1125
1126         if (rval != QLA_SUCCESS) {
1127                 /*EMPTY*/
1128                 DEBUG2_3_11(printk("qla2x00_init_firmware(%ld): failed=%x "
1129                     "mb0=%x.\n",
1130                     ha->host_no, rval, mcp->mb[0]);)
1131         } else {
1132                 /*EMPTY*/
1133                 DEBUG11(printk("qla2x00_init_firmware(%ld): done.\n",
1134                     ha->host_no);)
1135         }
1136
1137         return rval;
1138 }
1139
1140 /*
1141  * qla2x00_get_port_database
1142  *      Issue normal/enhanced get port database mailbox command
1143  *      and copy device name as necessary.
1144  *
1145  * Input:
1146  *      ha = adapter state pointer.
1147  *      dev = structure pointer.
1148  *      opt = enhanced cmd option byte.
1149  *
1150  * Returns:
1151  *      qla2x00 local function return status code.
1152  *
1153  * Context:
1154  *      Kernel context.
1155  */
1156 int
1157 qla2x00_get_port_database(scsi_qla_host_t *ha, fc_port_t *fcport, uint8_t opt)
1158 {
1159         int rval;
1160         mbx_cmd_t mc;
1161         mbx_cmd_t *mcp = &mc;
1162         port_database_t *pd;
1163         struct port_database_24xx *pd24;
1164         dma_addr_t pd_dma;
1165
1166         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1167
1168         pd24 = NULL;
1169         pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1170         if (pd  == NULL) {
1171                 DEBUG2_3(printk("%s(%ld): failed to allocate Port Database "
1172                     "structure.\n", __func__, ha->host_no));
1173                 return QLA_MEMORY_ALLOC_FAILED;
1174         }
1175         memset(pd, 0, max(PORT_DATABASE_SIZE, PORT_DATABASE_24XX_SIZE));
1176
1177         mcp->mb[0] = MBC_GET_PORT_DATABASE;
1178         if (opt != 0 && !IS_QLA24XX(ha) && !IS_QLA25XX(ha))
1179                 mcp->mb[0] = MBC_ENHANCED_GET_PORT_DATABASE;
1180         mcp->mb[2] = MSW(pd_dma);
1181         mcp->mb[3] = LSW(pd_dma);
1182         mcp->mb[6] = MSW(MSD(pd_dma));
1183         mcp->mb[7] = LSW(MSD(pd_dma));
1184         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1185         mcp->in_mb = MBX_0;
1186         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1187                 mcp->mb[1] = fcport->loop_id;
1188                 mcp->mb[10] = opt;
1189                 mcp->out_mb |= MBX_10|MBX_1;
1190                 mcp->in_mb |= MBX_1;
1191         } else if (HAS_EXTENDED_IDS(ha)) {
1192                 mcp->mb[1] = fcport->loop_id;
1193                 mcp->mb[10] = opt;
1194                 mcp->out_mb |= MBX_10|MBX_1;
1195         } else {
1196                 mcp->mb[1] = fcport->loop_id << 8 | opt;
1197                 mcp->out_mb |= MBX_1;
1198         }
1199         mcp->buf_size = (IS_QLA24XX(ha) || IS_QLA25XX(ha) ?
1200             PORT_DATABASE_24XX_SIZE : PORT_DATABASE_SIZE);
1201         mcp->flags = MBX_DMA_IN;
1202         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1203         rval = qla2x00_mailbox_command(ha, mcp);
1204         if (rval != QLA_SUCCESS)
1205                 goto gpd_error_out;
1206
1207         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1208                 pd24 = (struct port_database_24xx *) pd;
1209
1210                 /* Check for logged in state. */
1211                 if (pd24->current_login_state != PDS_PRLI_COMPLETE &&
1212                     pd24->last_login_state != PDS_PRLI_COMPLETE) {
1213                         DEBUG2(printk("%s(%ld): Unable to verify "
1214                             "login-state (%x/%x) for loop_id %x\n",
1215                             __func__, ha->host_no,
1216                             pd24->current_login_state,
1217                             pd24->last_login_state, fcport->loop_id));
1218                         rval = QLA_FUNCTION_FAILED;
1219                         goto gpd_error_out;
1220                 }
1221
1222                 /* Names are little-endian. */
1223                 memcpy(fcport->node_name, pd24->node_name, WWN_SIZE);
1224                 memcpy(fcport->port_name, pd24->port_name, WWN_SIZE);
1225
1226                 /* Get port_id of device. */
1227                 fcport->d_id.b.domain = pd24->port_id[0];
1228                 fcport->d_id.b.area = pd24->port_id[1];
1229                 fcport->d_id.b.al_pa = pd24->port_id[2];
1230                 fcport->d_id.b.rsvd_1 = 0;
1231
1232                 /* If not target must be initiator or unknown type. */
1233                 if ((pd24->prli_svc_param_word_3[0] & BIT_4) == 0)
1234                         fcport->port_type = FCT_INITIATOR;
1235                 else
1236                         fcport->port_type = FCT_TARGET;
1237         } else {
1238                 /* Check for logged in state. */
1239                 if (pd->master_state != PD_STATE_PORT_LOGGED_IN &&
1240                     pd->slave_state != PD_STATE_PORT_LOGGED_IN) {
1241                         rval = QLA_FUNCTION_FAILED;
1242                         goto gpd_error_out;
1243                 }
1244
1245                 /* Names are little-endian. */
1246                 memcpy(fcport->node_name, pd->node_name, WWN_SIZE);
1247                 memcpy(fcport->port_name, pd->port_name, WWN_SIZE);
1248
1249                 /* Get port_id of device. */
1250                 fcport->d_id.b.domain = pd->port_id[0];
1251                 fcport->d_id.b.area = pd->port_id[3];
1252                 fcport->d_id.b.al_pa = pd->port_id[2];
1253                 fcport->d_id.b.rsvd_1 = 0;
1254
1255                 /* Check for device require authentication. */
1256                 pd->common_features & BIT_5 ? (fcport->flags |= FCF_AUTH_REQ) :
1257                     (fcport->flags &= ~FCF_AUTH_REQ);
1258
1259                 /* If not target must be initiator or unknown type. */
1260                 if ((pd->prli_svc_param_word_3[0] & BIT_4) == 0)
1261                         fcport->port_type = FCT_INITIATOR;
1262                 else
1263                         fcport->port_type = FCT_TARGET;
1264
1265                 /* Passback COS information. */
1266                 fcport->supported_classes = (pd->options & BIT_4) ?
1267                     FC_COS_CLASS2: FC_COS_CLASS3;
1268         }
1269
1270 gpd_error_out:
1271         dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1272
1273         if (rval != QLA_SUCCESS) {
1274                 DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
1275                     __func__, ha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1276         } else {
1277                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
1278         }
1279
1280         return rval;
1281 }
1282
1283 /*
1284  * qla2x00_get_firmware_state
1285  *      Get adapter firmware state.
1286  *
1287  * Input:
1288  *      ha = adapter block pointer.
1289  *      dptr = pointer for firmware state.
1290  *      TARGET_QUEUE_LOCK must be released.
1291  *      ADAPTER_STATE_LOCK must be released.
1292  *
1293  * Returns:
1294  *      qla2x00 local function return status code.
1295  *
1296  * Context:
1297  *      Kernel context.
1298  */
1299 int
1300 qla2x00_get_firmware_state(scsi_qla_host_t *ha, uint16_t *dptr)
1301 {
1302         int rval;
1303         mbx_cmd_t mc;
1304         mbx_cmd_t *mcp = &mc;
1305
1306         DEBUG11(printk("qla2x00_get_firmware_state(%ld): entered.\n",
1307             ha->host_no);)
1308
1309         mcp->mb[0] = MBC_GET_FIRMWARE_STATE;
1310         mcp->out_mb = MBX_0;
1311         mcp->in_mb = MBX_2|MBX_1|MBX_0;
1312         mcp->tov = 30;
1313         mcp->flags = 0;
1314         rval = qla2x00_mailbox_command(ha, mcp);
1315
1316         /* Return firmware state. */
1317         *dptr = mcp->mb[1];
1318
1319         if (rval != QLA_SUCCESS) {
1320                 /*EMPTY*/
1321                 DEBUG2_3_11(printk("qla2x00_get_firmware_state(%ld): "
1322                     "failed=%x.\n", ha->host_no, rval);)
1323         } else {
1324                 /*EMPTY*/
1325                 DEBUG11(printk("qla2x00_get_firmware_state(%ld): done.\n",
1326                     ha->host_no);)
1327         }
1328
1329         return rval;
1330 }
1331
1332 /*
1333  * qla2x00_get_port_name
1334  *      Issue get port name mailbox command.
1335  *      Returned name is in big endian format.
1336  *
1337  * Input:
1338  *      ha = adapter block pointer.
1339  *      loop_id = loop ID of device.
1340  *      name = pointer for name.
1341  *      TARGET_QUEUE_LOCK must be released.
1342  *      ADAPTER_STATE_LOCK must be released.
1343  *
1344  * Returns:
1345  *      qla2x00 local function return status code.
1346  *
1347  * Context:
1348  *      Kernel context.
1349  */
1350 int
1351 qla2x00_get_port_name(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t *name,
1352     uint8_t opt)
1353 {
1354         int rval;
1355         mbx_cmd_t mc;
1356         mbx_cmd_t *mcp = &mc;
1357
1358         DEBUG11(printk("qla2x00_get_port_name(%ld): entered.\n",
1359             ha->host_no);)
1360
1361         mcp->mb[0] = MBC_GET_PORT_NAME;
1362         mcp->out_mb = MBX_1|MBX_0;
1363         if (HAS_EXTENDED_IDS(ha)) {
1364                 mcp->mb[1] = loop_id;
1365                 mcp->mb[10] = opt;
1366                 mcp->out_mb |= MBX_10;
1367         } else {
1368                 mcp->mb[1] = loop_id << 8 | opt;
1369         }
1370
1371         mcp->in_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1372         mcp->tov = 30;
1373         mcp->flags = 0;
1374         rval = qla2x00_mailbox_command(ha, mcp);
1375
1376         if (rval != QLA_SUCCESS) {
1377                 /*EMPTY*/
1378                 DEBUG2_3_11(printk("qla2x00_get_port_name(%ld): failed=%x.\n",
1379                     ha->host_no, rval);)
1380         } else {
1381                 if (name != NULL) {
1382                         /* This function returns name in big endian. */
1383                         name[0] = LSB(mcp->mb[2]);
1384                         name[1] = MSB(mcp->mb[2]);
1385                         name[2] = LSB(mcp->mb[3]);
1386                         name[3] = MSB(mcp->mb[3]);
1387                         name[4] = LSB(mcp->mb[6]);
1388                         name[5] = MSB(mcp->mb[6]);
1389                         name[6] = LSB(mcp->mb[7]);
1390                         name[7] = MSB(mcp->mb[7]);
1391                 }
1392
1393                 DEBUG11(printk("qla2x00_get_port_name(%ld): done.\n",
1394                     ha->host_no);)
1395         }
1396
1397         return rval;
1398 }
1399
1400 /*
1401  * qla2x00_lip_reset
1402  *      Issue LIP reset mailbox command.
1403  *
1404  * Input:
1405  *      ha = adapter block pointer.
1406  *      TARGET_QUEUE_LOCK must be released.
1407  *      ADAPTER_STATE_LOCK must be released.
1408  *
1409  * Returns:
1410  *      qla2x00 local function return status code.
1411  *
1412  * Context:
1413  *      Kernel context.
1414  */
1415 int
1416 qla2x00_lip_reset(scsi_qla_host_t *ha)
1417 {
1418         int rval;
1419         mbx_cmd_t mc;
1420         mbx_cmd_t *mcp = &mc;
1421
1422         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1423
1424         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1425                 mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1426                 mcp->mb[1] = BIT_0;
1427                 mcp->mb[2] = 0xff;
1428                 mcp->mb[3] = 0;
1429                 mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1430         } else {
1431                 mcp->mb[0] = MBC_LIP_RESET;
1432                 mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1433                 if (HAS_EXTENDED_IDS(ha)) {
1434                         mcp->mb[1] = 0x00ff;
1435                         mcp->mb[10] = 0;
1436                         mcp->out_mb |= MBX_10;
1437                 } else {
1438                         mcp->mb[1] = 0xff00;
1439                 }
1440                 mcp->mb[2] = ha->loop_reset_delay;
1441                 mcp->mb[3] = 0;
1442         }
1443         mcp->in_mb = MBX_0;
1444         mcp->tov = 30;
1445         mcp->flags = 0;
1446         rval = qla2x00_mailbox_command(ha, mcp);
1447
1448         if (rval != QLA_SUCCESS) {
1449                 /*EMPTY*/
1450                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n",
1451                     __func__, ha->host_no, rval);)
1452         } else {
1453                 /*EMPTY*/
1454                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
1455         }
1456
1457         return rval;
1458 }
1459
1460 /*
1461  * qla2x00_send_sns
1462  *      Send SNS command.
1463  *
1464  * Input:
1465  *      ha = adapter block pointer.
1466  *      sns = pointer for command.
1467  *      cmd_size = command size.
1468  *      buf_size = response/command size.
1469  *      TARGET_QUEUE_LOCK must be released.
1470  *      ADAPTER_STATE_LOCK must be released.
1471  *
1472  * Returns:
1473  *      qla2x00 local function return status code.
1474  *
1475  * Context:
1476  *      Kernel context.
1477  */
1478 int
1479 qla2x00_send_sns(scsi_qla_host_t *ha, dma_addr_t sns_phys_address,
1480     uint16_t cmd_size, size_t buf_size)
1481 {
1482         int rval;
1483         mbx_cmd_t mc;
1484         mbx_cmd_t *mcp = &mc;
1485
1486         DEBUG11(printk("qla2x00_send_sns(%ld): entered.\n",
1487             ha->host_no);)
1488
1489         DEBUG11(printk("qla2x00_send_sns: retry cnt=%d ratov=%d total "
1490             "tov=%d.\n", ha->retry_count, ha->login_timeout, mcp->tov);)
1491
1492         mcp->mb[0] = MBC_SEND_SNS_COMMAND;
1493         mcp->mb[1] = cmd_size;
1494         mcp->mb[2] = MSW(sns_phys_address);
1495         mcp->mb[3] = LSW(sns_phys_address);
1496         mcp->mb[6] = MSW(MSD(sns_phys_address));
1497         mcp->mb[7] = LSW(MSD(sns_phys_address));
1498         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1499         mcp->in_mb = MBX_0|MBX_1;
1500         mcp->buf_size = buf_size;
1501         mcp->flags = MBX_DMA_OUT|MBX_DMA_IN;
1502         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1503         rval = qla2x00_mailbox_command(ha, mcp);
1504
1505         if (rval != QLA_SUCCESS) {
1506                 /*EMPTY*/
1507                 DEBUG(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1508                     "mb[1]=%x.\n", ha->host_no, rval, mcp->mb[0], mcp->mb[1]);)
1509                 DEBUG2_3_11(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1510                     "mb[1]=%x.\n", ha->host_no, rval, mcp->mb[0], mcp->mb[1]);)
1511         } else {
1512                 /*EMPTY*/
1513                 DEBUG11(printk("qla2x00_send_sns(%ld): done.\n", ha->host_no);)
1514         }
1515
1516         return rval;
1517 }
1518
1519 int
1520 qla24xx_login_fabric(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1521     uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1522 {
1523         int             rval;
1524
1525         struct logio_entry_24xx *lg;
1526         dma_addr_t      lg_dma;
1527         uint32_t        iop[2];
1528
1529         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1530
1531         lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1532         if (lg == NULL) {
1533                 DEBUG2_3(printk("%s(%ld): failed to allocate Login IOCB.\n",
1534                     __func__, ha->host_no));
1535                 return QLA_MEMORY_ALLOC_FAILED;
1536         }
1537         memset(lg, 0, sizeof(struct logio_entry_24xx));
1538
1539         lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1540         lg->entry_count = 1;
1541         lg->nport_handle = cpu_to_le16(loop_id);
1542         lg->control_flags = __constant_cpu_to_le16(LCF_COMMAND_PLOGI);
1543         if (opt & BIT_0)
1544                 lg->control_flags |= __constant_cpu_to_le16(LCF_COND_PLOGI);
1545         lg->port_id[0] = al_pa;
1546         lg->port_id[1] = area;
1547         lg->port_id[2] = domain;
1548         rval = qla2x00_issue_iocb(ha, lg, lg_dma, 0);
1549         if (rval != QLA_SUCCESS) {
1550                 DEBUG2_3_11(printk("%s(%ld): failed to issue Login IOCB "
1551                     "(%x).\n", __func__, ha->host_no, rval);)
1552         } else if (lg->entry_status != 0) {
1553                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1554                     "-- error status (%x).\n", __func__, ha->host_no,
1555                     lg->entry_status));
1556                 rval = QLA_FUNCTION_FAILED;
1557         } else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1558                 iop[0] = le32_to_cpu(lg->io_parameter[0]);
1559                 iop[1] = le32_to_cpu(lg->io_parameter[1]);
1560
1561                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1562                     "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1563                     ha->host_no, le16_to_cpu(lg->comp_status), iop[0],
1564                     iop[1]));
1565
1566                 switch (iop[0]) {
1567                 case LSC_SCODE_PORTID_USED:
1568                         mb[0] = MBS_PORT_ID_USED;
1569                         mb[1] = LSW(iop[1]);
1570                         break;
1571                 case LSC_SCODE_NPORT_USED:
1572                         mb[0] = MBS_LOOP_ID_USED;
1573                         break;
1574                 case LSC_SCODE_NOLINK:
1575                 case LSC_SCODE_NOIOCB:
1576                 case LSC_SCODE_NOXCB:
1577                 case LSC_SCODE_CMD_FAILED:
1578                 case LSC_SCODE_NOFABRIC:
1579                 case LSC_SCODE_FW_NOT_READY:
1580                 case LSC_SCODE_NOT_LOGGED_IN:
1581                 case LSC_SCODE_NOPCB:
1582                 case LSC_SCODE_ELS_REJECT:
1583                 case LSC_SCODE_CMD_PARAM_ERR:
1584                 case LSC_SCODE_NONPORT:
1585                 case LSC_SCODE_LOGGED_IN:
1586                 case LSC_SCODE_NOFLOGI_ACC:
1587                 default:
1588                         mb[0] = MBS_COMMAND_ERROR;
1589                         break;
1590                 }
1591         } else {
1592                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
1593
1594                 iop[0] = le32_to_cpu(lg->io_parameter[0]);
1595
1596                 mb[0] = MBS_COMMAND_COMPLETE;
1597                 mb[1] = 0;
1598                 if (iop[0] & BIT_4) {
1599                         if (iop[0] & BIT_8)
1600                                 mb[1] |= BIT_1;
1601                 } else
1602                         mb[1] = BIT_0;
1603
1604                 /* Passback COS information. */
1605                 mb[10] = 0;
1606                 if (lg->io_parameter[7] || lg->io_parameter[8])
1607                         mb[10] |= BIT_0;        /* Class 2. */
1608                 if (lg->io_parameter[9] || lg->io_parameter[10])
1609                         mb[10] |= BIT_1;        /* Class 3. */
1610         }
1611
1612         dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1613
1614         return rval;
1615 }
1616
1617 /*
1618  * qla2x00_login_fabric
1619  *      Issue login fabric port mailbox command.
1620  *
1621  * Input:
1622  *      ha = adapter block pointer.
1623  *      loop_id = device loop ID.
1624  *      domain = device domain.
1625  *      area = device area.
1626  *      al_pa = device AL_PA.
1627  *      status = pointer for return status.
1628  *      opt = command options.
1629  *      TARGET_QUEUE_LOCK must be released.
1630  *      ADAPTER_STATE_LOCK must be released.
1631  *
1632  * Returns:
1633  *      qla2x00 local function return status code.
1634  *
1635  * Context:
1636  *      Kernel context.
1637  */
1638 int
1639 qla2x00_login_fabric(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1640     uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1641 {
1642         int rval;
1643         mbx_cmd_t mc;
1644         mbx_cmd_t *mcp = &mc;
1645
1646         DEBUG11(printk("qla2x00_login_fabric(%ld): entered.\n", ha->host_no);)
1647
1648         mcp->mb[0] = MBC_LOGIN_FABRIC_PORT;
1649         mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1650         if (HAS_EXTENDED_IDS(ha)) {
1651                 mcp->mb[1] = loop_id;
1652                 mcp->mb[10] = opt;
1653                 mcp->out_mb |= MBX_10;
1654         } else {
1655                 mcp->mb[1] = (loop_id << 8) | opt;
1656         }
1657         mcp->mb[2] = domain;
1658         mcp->mb[3] = area << 8 | al_pa;
1659
1660         mcp->in_mb = MBX_7|MBX_6|MBX_2|MBX_1|MBX_0;
1661         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1662         mcp->flags = 0;
1663         rval = qla2x00_mailbox_command(ha, mcp);
1664
1665         /* Return mailbox statuses. */
1666         if (mb != NULL) {
1667                 mb[0] = mcp->mb[0];
1668                 mb[1] = mcp->mb[1];
1669                 mb[2] = mcp->mb[2];
1670                 mb[6] = mcp->mb[6];
1671                 mb[7] = mcp->mb[7];
1672                 /* COS retrieved from Get-Port-Database mailbox command. */
1673                 mb[10] = 0;
1674         }
1675
1676         if (rval != QLA_SUCCESS) {
1677                 /* RLU tmp code: need to change main mailbox_command function to
1678                  * return ok even when the mailbox completion value is not
1679                  * SUCCESS. The caller needs to be responsible to interpret
1680                  * the return values of this mailbox command if we're not
1681                  * to change too much of the existing code.
1682                  */
1683                 if (mcp->mb[0] == 0x4001 || mcp->mb[0] == 0x4002 ||
1684                     mcp->mb[0] == 0x4003 || mcp->mb[0] == 0x4005 ||
1685                     mcp->mb[0] == 0x4006)
1686                         rval = QLA_SUCCESS;
1687
1688                 /*EMPTY*/
1689                 DEBUG2_3_11(printk("qla2x00_login_fabric(%ld): failed=%x "
1690                     "mb[0]=%x mb[1]=%x mb[2]=%x.\n", ha->host_no, rval,
1691                     mcp->mb[0], mcp->mb[1], mcp->mb[2]);)
1692         } else {
1693                 /*EMPTY*/
1694                 DEBUG11(printk("qla2x00_login_fabric(%ld): done.\n",
1695                     ha->host_no);)
1696         }
1697
1698         return rval;
1699 }
1700
1701 /*
1702  * qla2x00_login_local_device
1703  *           Issue login loop port mailbox command.
1704  *
1705  * Input:
1706  *           ha = adapter block pointer.
1707  *           loop_id = device loop ID.
1708  *           opt = command options.
1709  *
1710  * Returns:
1711  *            Return status code.
1712  *
1713  * Context:
1714  *            Kernel context.
1715  *
1716  */
1717 int
1718 qla2x00_login_local_device(scsi_qla_host_t *ha, uint16_t loop_id,
1719     uint16_t *mb_ret, uint8_t opt)
1720 {
1721         int rval;
1722         mbx_cmd_t mc;
1723         mbx_cmd_t *mcp = &mc;
1724
1725         DEBUG3(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1726
1727         mcp->mb[0] = MBC_LOGIN_LOOP_PORT;
1728         if (HAS_EXTENDED_IDS(ha))
1729                 mcp->mb[1] = loop_id;
1730         else
1731                 mcp->mb[1] = loop_id << 8;
1732         mcp->mb[2] = opt;
1733         mcp->out_mb = MBX_2|MBX_1|MBX_0;
1734         mcp->in_mb = MBX_7|MBX_6|MBX_1|MBX_0;
1735         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1736         mcp->flags = 0;
1737         rval = qla2x00_mailbox_command(ha, mcp);
1738
1739         /* Return mailbox statuses. */
1740         if (mb_ret != NULL) {
1741                 mb_ret[0] = mcp->mb[0];
1742                 mb_ret[1] = mcp->mb[1];
1743                 mb_ret[6] = mcp->mb[6];
1744                 mb_ret[7] = mcp->mb[7];
1745         }
1746
1747         if (rval != QLA_SUCCESS) {
1748                 /* AV tmp code: need to change main mailbox_command function to
1749                  * return ok even when the mailbox completion value is not
1750                  * SUCCESS. The caller needs to be responsible to interpret
1751                  * the return values of this mailbox command if we're not
1752                  * to change too much of the existing code.
1753                  */
1754                 if (mcp->mb[0] == 0x4005 || mcp->mb[0] == 0x4006)
1755                         rval = QLA_SUCCESS;
1756
1757                 DEBUG(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1758                     "mb[6]=%x mb[7]=%x.\n", __func__, ha->host_no, rval,
1759                     mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]);)
1760                 DEBUG2_3(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1761                     "mb[6]=%x mb[7]=%x.\n", __func__, ha->host_no, rval,
1762                     mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]);)
1763         } else {
1764                 /*EMPTY*/
1765                 DEBUG3(printk("%s(%ld): done.\n", __func__, ha->host_no);)
1766         }
1767
1768         return (rval);
1769 }
1770
1771 int
1772 qla24xx_fabric_logout(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1773     uint8_t area, uint8_t al_pa)
1774 {
1775         int             rval;
1776         struct logio_entry_24xx *lg;
1777         dma_addr_t      lg_dma;
1778
1779         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
1780
1781         lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1782         if (lg == NULL) {
1783                 DEBUG2_3(printk("%s(%ld): failed to allocate Logout IOCB.\n",
1784                     __func__, ha->host_no));
1785                 return QLA_MEMORY_ALLOC_FAILED;
1786         }
1787         memset(lg, 0, sizeof(struct logio_entry_24xx));
1788
1789         lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1790         lg->entry_count = 1;
1791         lg->nport_handle = cpu_to_le16(loop_id);
1792         lg->control_flags =
1793             __constant_cpu_to_le16(LCF_COMMAND_LOGO|LCF_EXPL_LOGO);
1794         lg->port_id[0] = al_pa;
1795         lg->port_id[1] = area;
1796         lg->port_id[2] = domain;
1797         rval = qla2x00_issue_iocb(ha, lg, lg_dma, 0);
1798         if (rval != QLA_SUCCESS) {
1799                 DEBUG2_3_11(printk("%s(%ld): failed to issue Logout IOCB "
1800                     "(%x).\n", __func__, ha->host_no, rval);)
1801         } else if (lg->entry_status != 0) {
1802                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1803                     "-- error status (%x).\n", __func__, ha->host_no,
1804                     lg->entry_status));
1805                 rval = QLA_FUNCTION_FAILED;
1806         } else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1807                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1808                     "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1809                     ha->host_no, le16_to_cpu(lg->comp_status),
1810                     le32_to_cpu(lg->io_parameter[0]),
1811                     le32_to_cpu(lg->io_parameter[1]));)
1812         } else {
1813                 /*EMPTY*/
1814                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
1815         }
1816
1817         dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1818
1819         return rval;
1820 }
1821
1822 /*
1823  * qla2x00_fabric_logout
1824  *      Issue logout fabric port mailbox command.
1825  *
1826  * Input:
1827  *      ha = adapter block pointer.
1828  *      loop_id = device loop ID.
1829  *      TARGET_QUEUE_LOCK must be released.
1830  *      ADAPTER_STATE_LOCK must be released.
1831  *
1832  * Returns:
1833  *      qla2x00 local function return status code.
1834  *
1835  * Context:
1836  *      Kernel context.
1837  */
1838 int
1839 qla2x00_fabric_logout(scsi_qla_host_t *ha, uint16_t loop_id, uint8_t domain,
1840     uint8_t area, uint8_t al_pa)
1841 {
1842         int rval;
1843         mbx_cmd_t mc;
1844         mbx_cmd_t *mcp = &mc;
1845
1846         DEBUG11(printk("qla2x00_fabric_logout(%ld): entered.\n",
1847             ha->host_no);)
1848
1849         mcp->mb[0] = MBC_LOGOUT_FABRIC_PORT;
1850         mcp->out_mb = MBX_1|MBX_0;
1851         if (HAS_EXTENDED_IDS(ha)) {
1852                 mcp->mb[1] = loop_id;
1853                 mcp->mb[10] = 0;
1854                 mcp->out_mb |= MBX_10;
1855         } else {
1856                 mcp->mb[1] = loop_id << 8;
1857         }
1858
1859         mcp->in_mb = MBX_1|MBX_0;
1860         mcp->tov = 30;
1861         mcp->flags = 0;
1862         rval = qla2x00_mailbox_command(ha, mcp);
1863
1864         if (rval != QLA_SUCCESS) {
1865                 /*EMPTY*/
1866                 DEBUG2_3_11(printk("qla2x00_fabric_logout(%ld): failed=%x "
1867                     "mbx1=%x.\n", ha->host_no, rval, mcp->mb[1]);)
1868         } else {
1869                 /*EMPTY*/
1870                 DEBUG11(printk("qla2x00_fabric_logout(%ld): done.\n",
1871                     ha->host_no);)
1872         }
1873
1874         return rval;
1875 }
1876
1877 /*
1878  * qla2x00_full_login_lip
1879  *      Issue full login LIP mailbox command.
1880  *
1881  * Input:
1882  *      ha = adapter block pointer.
1883  *      TARGET_QUEUE_LOCK must be released.
1884  *      ADAPTER_STATE_LOCK must be released.
1885  *
1886  * Returns:
1887  *      qla2x00 local function return status code.
1888  *
1889  * Context:
1890  *      Kernel context.
1891  */
1892 int
1893 qla2x00_full_login_lip(scsi_qla_host_t *ha)
1894 {
1895         int rval;
1896         mbx_cmd_t mc;
1897         mbx_cmd_t *mcp = &mc;
1898
1899         DEBUG11(printk("qla2x00_full_login_lip(%ld): entered.\n",
1900             ha->host_no);)
1901
1902         mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1903         mcp->mb[1] = 0;
1904         mcp->mb[2] = 0xff;
1905         mcp->mb[3] = 0;
1906         mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1907         mcp->in_mb = MBX_0;
1908         mcp->tov = 30;
1909         mcp->flags = 0;
1910         rval = qla2x00_mailbox_command(ha, mcp);
1911
1912         if (rval != QLA_SUCCESS) {
1913                 /*EMPTY*/
1914                 DEBUG2_3_11(printk("qla2x00_full_login_lip(%ld): failed=%x.\n",
1915                     ha->host_no, rval);)
1916         } else {
1917                 /*EMPTY*/
1918                 DEBUG11(printk("qla2x00_full_login_lip(%ld): done.\n",
1919                     ha->host_no);)
1920         }
1921
1922         return rval;
1923 }
1924
1925 /*
1926  * qla2x00_get_id_list
1927  *
1928  * Input:
1929  *      ha = adapter block pointer.
1930  *
1931  * Returns:
1932  *      qla2x00 local function return status code.
1933  *
1934  * Context:
1935  *      Kernel context.
1936  */
1937 int
1938 qla2x00_get_id_list(scsi_qla_host_t *ha, void *id_list, dma_addr_t id_list_dma,
1939     uint16_t *entries)
1940 {
1941         int rval;
1942         mbx_cmd_t mc;
1943         mbx_cmd_t *mcp = &mc;
1944
1945         DEBUG11(printk("qla2x00_get_id_list(%ld): entered.\n",
1946             ha->host_no);)
1947
1948         if (id_list == NULL)
1949                 return QLA_FUNCTION_FAILED;
1950
1951         mcp->mb[0] = MBC_GET_ID_LIST;
1952         mcp->out_mb = MBX_0;
1953         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1954                 mcp->mb[2] = MSW(id_list_dma);
1955                 mcp->mb[3] = LSW(id_list_dma);
1956                 mcp->mb[6] = MSW(MSD(id_list_dma));
1957                 mcp->mb[7] = LSW(MSD(id_list_dma));
1958                 mcp->out_mb |= MBX_7|MBX_6|MBX_3|MBX_2;
1959         } else {
1960                 mcp->mb[1] = MSW(id_list_dma);
1961                 mcp->mb[2] = LSW(id_list_dma);
1962                 mcp->mb[3] = MSW(MSD(id_list_dma));
1963                 mcp->mb[6] = LSW(MSD(id_list_dma));
1964                 mcp->out_mb |= MBX_6|MBX_3|MBX_2|MBX_1;
1965         }
1966         mcp->in_mb = MBX_1|MBX_0;
1967         mcp->tov = 30;
1968         mcp->flags = 0;
1969         rval = qla2x00_mailbox_command(ha, mcp);
1970
1971         if (rval != QLA_SUCCESS) {
1972                 /*EMPTY*/
1973                 DEBUG2_3_11(printk("qla2x00_get_id_list(%ld): failed=%x.\n",
1974                     ha->host_no, rval);)
1975         } else {
1976                 *entries = mcp->mb[1];
1977                 DEBUG11(printk("qla2x00_get_id_list(%ld): done.\n",
1978                     ha->host_no);)
1979         }
1980
1981         return rval;
1982 }
1983
1984 /*
1985  * qla2x00_get_resource_cnts
1986  *      Get current firmware resource counts.
1987  *
1988  * Input:
1989  *      ha = adapter block pointer.
1990  *
1991  * Returns:
1992  *      qla2x00 local function return status code.
1993  *
1994  * Context:
1995  *      Kernel context.
1996  */
1997 int
1998 qla2x00_get_resource_cnts(scsi_qla_host_t *ha, uint16_t *cur_xchg_cnt,
1999     uint16_t *orig_xchg_cnt, uint16_t *cur_iocb_cnt, uint16_t *orig_iocb_cnt)
2000 {
2001         int rval;
2002         mbx_cmd_t mc;
2003         mbx_cmd_t *mcp = &mc;
2004
2005         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2006
2007         mcp->mb[0] = MBC_GET_RESOURCE_COUNTS;
2008         mcp->out_mb = MBX_0;
2009         mcp->in_mb = MBX_10|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
2010         mcp->tov = 30;
2011         mcp->flags = 0;
2012         rval = qla2x00_mailbox_command(ha, mcp);
2013
2014         if (rval != QLA_SUCCESS) {
2015                 /*EMPTY*/
2016                 DEBUG2_3_11(printk("%s(%ld): failed = %x.\n", __func__,
2017                     ha->host_no, mcp->mb[0]);)
2018         } else {
2019                 DEBUG11(printk("%s(%ld): done. mb1=%x mb2=%x mb3=%x mb6=%x "
2020                     "mb7=%x mb10=%x.\n", __func__, ha->host_no,
2021                     mcp->mb[1], mcp->mb[2], mcp->mb[3], mcp->mb[6], mcp->mb[7],
2022                     mcp->mb[10]));
2023
2024                 if (cur_xchg_cnt)
2025                         *cur_xchg_cnt = mcp->mb[3];
2026                 if (orig_xchg_cnt)
2027                         *orig_xchg_cnt = mcp->mb[6];
2028                 if (cur_iocb_cnt)
2029                         *cur_iocb_cnt = mcp->mb[7];
2030                 if (orig_iocb_cnt)
2031                         *orig_iocb_cnt = mcp->mb[10];
2032         }
2033
2034         return (rval);
2035 }
2036
2037 #if defined(QL_DEBUG_LEVEL_3)
2038 /*
2039  * qla2x00_get_fcal_position_map
2040  *      Get FCAL (LILP) position map using mailbox command
2041  *
2042  * Input:
2043  *      ha = adapter state pointer.
2044  *      pos_map = buffer pointer (can be NULL).
2045  *
2046  * Returns:
2047  *      qla2x00 local function return status code.
2048  *
2049  * Context:
2050  *      Kernel context.
2051  */
2052 int
2053 qla2x00_get_fcal_position_map(scsi_qla_host_t *ha, char *pos_map)
2054 {
2055         int rval;
2056         mbx_cmd_t mc;
2057         mbx_cmd_t *mcp = &mc;
2058         char *pmap;
2059         dma_addr_t pmap_dma;
2060
2061         pmap = dma_pool_alloc(ha->s_dma_pool, GFP_ATOMIC, &pmap_dma);
2062         if (pmap  == NULL) {
2063                 DEBUG2_3_11(printk("%s(%ld): **** Mem Alloc Failed ****",
2064                     __func__, ha->host_no));
2065                 return QLA_MEMORY_ALLOC_FAILED;
2066         }
2067         memset(pmap, 0, FCAL_MAP_SIZE);
2068
2069         mcp->mb[0] = MBC_GET_FC_AL_POSITION_MAP;
2070         mcp->mb[2] = MSW(pmap_dma);
2071         mcp->mb[3] = LSW(pmap_dma);
2072         mcp->mb[6] = MSW(MSD(pmap_dma));
2073         mcp->mb[7] = LSW(MSD(pmap_dma));
2074         mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2075         mcp->in_mb = MBX_1|MBX_0;
2076         mcp->buf_size = FCAL_MAP_SIZE;
2077         mcp->flags = MBX_DMA_IN;
2078         mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
2079         rval = qla2x00_mailbox_command(ha, mcp);
2080
2081         if (rval == QLA_SUCCESS) {
2082                 DEBUG11(printk("%s(%ld): (mb0=%x/mb1=%x) FC/AL Position Map "
2083                     "size (%x)\n", __func__, ha->host_no, mcp->mb[0],
2084                     mcp->mb[1], (unsigned)pmap[0]));
2085                 DEBUG11(qla2x00_dump_buffer(pmap, pmap[0] + 1));
2086
2087                 if (pos_map)
2088                         memcpy(pos_map, pmap, FCAL_MAP_SIZE);
2089         }
2090         dma_pool_free(ha->s_dma_pool, pmap, pmap_dma);
2091
2092         if (rval != QLA_SUCCESS) {
2093                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2094                     ha->host_no, rval));
2095         } else {
2096                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2097         }
2098
2099         return rval;
2100 }
2101
2102 uint8_t
2103 qla24xx_get_isp_stats(scsi_qla_host_t *ha, uint32_t *dwbuf, uint32_t dwords,
2104     uint16_t *status)
2105 {
2106         int rval;
2107         mbx_cmd_t mc;
2108         mbx_cmd_t *mcp = &mc;
2109         uint32_t *sbuf, *siter;
2110         dma_addr_t sbuf_dma;
2111
2112         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
2113
2114         if (dwords > (DMA_POOL_SIZE / 4)) {
2115                 DEBUG2_3_11(printk("%s(%ld): Unabled to retrieve %d DWORDs "
2116                     "(max %d).\n", __func__, ha->host_no, dwords,
2117                     DMA_POOL_SIZE / 4));
2118                 return BIT_0;
2119         }
2120         sbuf = dma_pool_alloc(ha->s_dma_pool, GFP_ATOMIC, &sbuf_dma);
2121         if (sbuf == NULL) {
2122                 DEBUG2_3_11(printk("%s(%ld): Failed to allocate memory.\n",
2123                     __func__, ha->host_no));
2124                 return BIT_0;
2125         }
2126         memset(sbuf, 0, DMA_POOL_SIZE);
2127
2128         mcp->mb[0] = MBC_GET_LINK_PRIV_STATS;
2129         mcp->mb[2] = MSW(sbuf_dma);
2130         mcp->mb[3] = LSW(sbuf_dma);
2131         mcp->mb[6] = MSW(MSD(sbuf_dma));
2132         mcp->mb[7] = LSW(MSD(sbuf_dma));
2133         mcp->mb[8] = dwords;
2134         mcp->mb[10] = 0;
2135         mcp->out_mb = MBX_10|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2136         mcp->in_mb = MBX_2|MBX_1|MBX_0;
2137         mcp->tov = 30;
2138         mcp->flags = IOCTL_CMD;
2139         rval = qla2x00_mailbox_command(ha, mcp);
2140
2141         if (rval == QLA_SUCCESS) {
2142                 if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2143                         DEBUG2_3_11(printk("%s(%ld): cmd failed. mbx0=%x.\n",
2144                             __func__, ha->host_no, mcp->mb[0]));
2145                         status[0] = mcp->mb[0];
2146                         rval = BIT_1;
2147                 } else {
2148                         /* Copy over data -- firmware data is LE. */
2149                         siter = sbuf;
2150                         while (dwords--)
2151                                 *dwbuf++ = le32_to_cpu(*siter++);
2152                 }
2153         } else {
2154                 /* Failed. */
2155                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2156                     ha->host_no, rval));
2157                 rval = BIT_1;
2158         }
2159
2160         dma_pool_free(ha->s_dma_pool, sbuf, sbuf_dma);
2161
2162         return rval;
2163 }
2164 #endif
2165
2166 int
2167 qla24xx_abort_command(scsi_qla_host_t *ha, srb_t *sp)
2168 {
2169         int             rval;
2170         fc_port_t       *fcport;
2171         unsigned long   flags = 0;
2172
2173         struct abort_entry_24xx *abt;
2174         dma_addr_t      abt_dma;
2175         uint32_t        handle;
2176
2177         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no);)
2178
2179         fcport = sp->fcport;
2180
2181         spin_lock_irqsave(&ha->hardware_lock, flags);
2182         for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
2183                 if (ha->outstanding_cmds[handle] == sp)
2184                         break;
2185         }
2186         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2187         if (handle == MAX_OUTSTANDING_COMMANDS) {
2188                 /* Command not found. */
2189                 return QLA_FUNCTION_FAILED;
2190         }
2191
2192         abt = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &abt_dma);
2193         if (abt == NULL) {
2194                 DEBUG2_3(printk("%s(%ld): failed to allocate Abort IOCB.\n",
2195                     __func__, ha->host_no));
2196                 return QLA_MEMORY_ALLOC_FAILED;
2197         }
2198         memset(abt, 0, sizeof(struct abort_entry_24xx));
2199
2200         abt->entry_type = ABORT_IOCB_TYPE;
2201         abt->entry_count = 1;
2202         abt->nport_handle = cpu_to_le16(fcport->loop_id);
2203         abt->handle_to_abort = handle;
2204         abt->port_id[0] = fcport->d_id.b.al_pa;
2205         abt->port_id[1] = fcport->d_id.b.area;
2206         abt->port_id[2] = fcport->d_id.b.domain;
2207         rval = qla2x00_issue_iocb(ha, abt, abt_dma, 0);
2208         if (rval != QLA_SUCCESS) {
2209                 DEBUG2_3_11(printk("%s(%ld): failed to issue IOCB (%x).\n",
2210                     __func__, ha->host_no, rval);)
2211         } else if (abt->entry_status != 0) {
2212                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2213                     "-- error status (%x).\n", __func__, ha->host_no,
2214                     abt->entry_status));
2215                 rval = QLA_FUNCTION_FAILED;
2216         } else if (abt->nport_handle != __constant_cpu_to_le16(0)) {
2217                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2218                     "-- completion status (%x).\n", __func__, ha->host_no,
2219                     le16_to_cpu(abt->nport_handle));)
2220                 rval = QLA_FUNCTION_FAILED;
2221         } else {
2222                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
2223                 sp->flags |= SRB_ABORT_PENDING;
2224         }
2225
2226         dma_pool_free(ha->s_dma_pool, abt, abt_dma);
2227
2228         return rval;
2229 }
2230
2231 struct tsk_mgmt_cmd {
2232         union {
2233                 struct tsk_mgmt_entry tsk;
2234                 struct sts_entry_24xx sts;
2235         } p;
2236 };
2237
2238 int
2239 qla24xx_abort_target(fc_port_t *fcport)
2240 {
2241         int             rval;
2242         struct tsk_mgmt_cmd *tsk;
2243         dma_addr_t      tsk_dma;
2244         scsi_qla_host_t *ha;
2245
2246         if (fcport == NULL)
2247                 return 0;
2248
2249         DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->ha->host_no);)
2250
2251         ha = fcport->ha;
2252         tsk = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &tsk_dma);
2253         if (tsk == NULL) {
2254                 DEBUG2_3(printk("%s(%ld): failed to allocate Task Management "
2255                     "IOCB.\n", __func__, ha->host_no));
2256                 return QLA_MEMORY_ALLOC_FAILED;
2257         }
2258         memset(tsk, 0, sizeof(struct tsk_mgmt_cmd));
2259
2260         tsk->p.tsk.entry_type = TSK_MGMT_IOCB_TYPE;
2261         tsk->p.tsk.entry_count = 1;
2262         tsk->p.tsk.nport_handle = cpu_to_le16(fcport->loop_id);
2263         tsk->p.tsk.timeout = __constant_cpu_to_le16(25);
2264         tsk->p.tsk.control_flags = __constant_cpu_to_le32(TCF_TARGET_RESET);
2265         tsk->p.tsk.port_id[0] = fcport->d_id.b.al_pa;
2266         tsk->p.tsk.port_id[1] = fcport->d_id.b.area;
2267         tsk->p.tsk.port_id[2] = fcport->d_id.b.domain;
2268         rval = qla2x00_issue_iocb(ha, tsk, tsk_dma, 0);
2269         if (rval != QLA_SUCCESS) {
2270                 DEBUG2_3_11(printk("%s(%ld): failed to issue Target Reset IOCB "
2271                     "(%x).\n", __func__, ha->host_no, rval);)
2272                 goto atarget_done;
2273         } else if (tsk->p.sts.entry_status != 0) {
2274                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2275                     "-- error status (%x).\n", __func__, ha->host_no,
2276                     tsk->p.sts.entry_status));
2277                 rval = QLA_FUNCTION_FAILED;
2278                 goto atarget_done;
2279         } else if (tsk->p.sts.comp_status !=
2280             __constant_cpu_to_le16(CS_COMPLETE)) {
2281                 DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2282                     "-- completion status (%x).\n", __func__,
2283                     ha->host_no, le16_to_cpu(tsk->p.sts.comp_status));)
2284                 rval = QLA_FUNCTION_FAILED;
2285                 goto atarget_done;
2286         }
2287
2288         /* Issue marker IOCB. */
2289         rval = qla2x00_marker(ha, fcport->loop_id, 0, MK_SYNC_ID);
2290         if (rval != QLA_SUCCESS) {
2291                 DEBUG2_3_11(printk("%s(%ld): failed to issue Marker IOCB "
2292                     "(%x).\n", __func__, ha->host_no, rval);)
2293         } else {
2294                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no);)
2295         }
2296
2297 atarget_done:
2298         dma_pool_free(ha->s_dma_pool, tsk, tsk_dma);
2299
2300         return rval;
2301 }
2302
2303 int
2304 qla2x00_system_error(scsi_qla_host_t *ha)
2305 {
2306         int rval;
2307         mbx_cmd_t mc;
2308         mbx_cmd_t *mcp = &mc;
2309
2310         if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha))
2311                 return QLA_FUNCTION_FAILED;
2312
2313         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2314
2315         mcp->mb[0] = MBC_GEN_SYSTEM_ERROR;
2316         mcp->out_mb = MBX_0;
2317         mcp->in_mb = MBX_0;
2318         mcp->tov = 5;
2319         mcp->flags = 0;
2320         rval = qla2x00_mailbox_command(ha, mcp);
2321
2322         if (rval != QLA_SUCCESS) {
2323                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2324                     ha->host_no, rval));
2325         } else {
2326                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2327         }
2328
2329         return rval;
2330 }
2331
2332 /**
2333  * qla2x00_get_serdes_params() -
2334  * @ha: HA context
2335  *
2336  * Returns
2337  */
2338 int
2339 qla2x00_get_serdes_params(scsi_qla_host_t *ha, uint16_t *sw_em_1g,
2340     uint16_t *sw_em_2g, uint16_t *sw_em_4g)
2341 {
2342         int rval;
2343         mbx_cmd_t mc;
2344         mbx_cmd_t *mcp = &mc;
2345
2346         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2347
2348         mcp->mb[0] = MBC_SERDES_PARAMS;
2349         mcp->mb[1] = 0;
2350         mcp->out_mb = MBX_1|MBX_0;
2351         mcp->in_mb = MBX_4|MBX_3|MBX_2|MBX_0;
2352         mcp->tov = 30;
2353         mcp->flags = 0;
2354         rval = qla2x00_mailbox_command(ha, mcp);
2355
2356         if (rval != QLA_SUCCESS) {
2357                 /*EMPTY*/
2358                 DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2359                     ha->host_no, rval, mcp->mb[0]));
2360         } else {
2361                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2362
2363                 if (sw_em_1g)
2364                         *sw_em_1g = mcp->mb[2];
2365                 if (sw_em_2g)
2366                         *sw_em_2g = mcp->mb[3];
2367                 if (sw_em_4g)
2368                         *sw_em_4g = mcp->mb[4];
2369         }
2370
2371         return rval;
2372 }
2373
2374 /**
2375  * qla2x00_set_serdes_params() -
2376  * @ha: HA context
2377  *
2378  * Returns
2379  */
2380 int
2381 qla2x00_set_serdes_params(scsi_qla_host_t *ha, uint16_t sw_em_1g,
2382     uint16_t sw_em_2g, uint16_t sw_em_4g)
2383 {
2384         int rval;
2385         mbx_cmd_t mc;
2386         mbx_cmd_t *mcp = &mc;
2387
2388         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2389
2390         mcp->mb[0] = MBC_SERDES_PARAMS;
2391         mcp->mb[1] = BIT_0;
2392         mcp->mb[2] = sw_em_1g;
2393         mcp->mb[3] = sw_em_2g;
2394         mcp->mb[4] = sw_em_4g;
2395         mcp->out_mb = MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2396         mcp->in_mb = MBX_0;
2397         mcp->tov = 30;
2398         mcp->flags = 0;
2399         rval = qla2x00_mailbox_command(ha, mcp);
2400
2401         if (rval != QLA_SUCCESS) {
2402                 /*EMPTY*/
2403                 DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2404                     ha->host_no, rval, mcp->mb[0]));
2405         } else {
2406                 /*EMPTY*/
2407                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2408         }
2409
2410         return rval;
2411 }
2412
2413 int
2414 qla2x00_stop_firmware(scsi_qla_host_t *ha)
2415 {
2416         int rval;
2417         mbx_cmd_t mc;
2418         mbx_cmd_t *mcp = &mc;
2419
2420         if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha))
2421                 return QLA_FUNCTION_FAILED;
2422
2423         DEBUG11(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2424
2425         mcp->mb[0] = MBC_STOP_FIRMWARE;
2426         mcp->out_mb = MBX_0;
2427         mcp->in_mb = MBX_0;
2428         mcp->tov = 5;
2429         mcp->flags = 0;
2430         rval = qla2x00_mailbox_command(ha, mcp);
2431
2432         if (rval != QLA_SUCCESS) {
2433                 DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2434                     ha->host_no, rval));
2435         } else {
2436                 DEBUG11(printk("%s(%ld): done.\n", __func__, ha->host_no));
2437         }
2438
2439         return rval;
2440 }