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