qla_mbx.c revision 6ad11eaa8a689a27e0c99905bcf800a37cd432a0
1/*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c)  2003-2011 QLogic Corporation
4 *
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7#include "qla_def.h"
8
9#include <linux/delay.h>
10#include <linux/gfp.h>
11
12
13/*
14 * qla2x00_mailbox_command
15 *	Issue mailbox command and waits for completion.
16 *
17 * Input:
18 *	ha = adapter block pointer.
19 *	mcp = driver internal mbx struct pointer.
20 *
21 * Output:
22 *	mb[MAX_MAILBOX_REGISTER_COUNT] = returned mailbox data.
23 *
24 * Returns:
25 *	0 : QLA_SUCCESS = cmd performed success
26 *	1 : QLA_FUNCTION_FAILED   (error encountered)
27 *	6 : QLA_FUNCTION_TIMEOUT (timeout condition encountered)
28 *
29 * Context:
30 *	Kernel context.
31 */
32static int
33qla2x00_mailbox_command(scsi_qla_host_t *vha, mbx_cmd_t *mcp)
34{
35	int		rval;
36	unsigned long    flags = 0;
37	device_reg_t __iomem *reg;
38	uint8_t		abort_active;
39	uint8_t		io_lock_on;
40	uint16_t	command = 0;
41	uint16_t	*iptr;
42	uint16_t __iomem *optr;
43	uint32_t	cnt;
44	uint32_t	mboxes;
45	unsigned long	wait_time;
46	struct qla_hw_data *ha = vha->hw;
47	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
48
49	if (ha->pdev->error_state > pci_channel_io_frozen)
50		return QLA_FUNCTION_TIMEOUT;
51
52	if (vha->device_flags & DFLG_DEV_FAILED) {
53		DEBUG2_3_11(qla_printk(KERN_WARNING, ha,
54			"%s(%ld): Device in failed state, "
55			"timeout MBX Exiting.\n",
56			__func__, base_vha->host_no));
57		return QLA_FUNCTION_TIMEOUT;
58	}
59
60	reg = ha->iobase;
61	io_lock_on = base_vha->flags.init_done;
62
63	rval = QLA_SUCCESS;
64	abort_active = test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
65
66	DEBUG11(printk("%s(%ld): entered.\n", __func__, base_vha->host_no));
67
68	if (ha->flags.pci_channel_io_perm_failure) {
69		DEBUG(printk("%s(%ld): Perm failure on EEH, timeout MBX "
70			     "Exiting.\n", __func__, vha->host_no));
71		return QLA_FUNCTION_TIMEOUT;
72	}
73
74	if (ha->flags.isp82xx_fw_hung) {
75		/* Setting Link-Down error */
76		mcp->mb[0] = MBS_LINK_DOWN_ERROR;
77		rval = QLA_FUNCTION_FAILED;
78		goto premature_exit;
79	}
80
81	/*
82	 * Wait for active mailbox commands to finish by waiting at most tov
83	 * seconds. This is to serialize actual issuing of mailbox cmds during
84	 * non ISP abort time.
85	 */
86	if (!wait_for_completion_timeout(&ha->mbx_cmd_comp, mcp->tov * HZ)) {
87		/* Timeout occurred. Return error. */
88		DEBUG2_3_11(printk("%s(%ld): cmd access timeout. "
89		    "Exiting.\n", __func__, base_vha->host_no));
90		return QLA_FUNCTION_TIMEOUT;
91	}
92
93	ha->flags.mbox_busy = 1;
94	/* Save mailbox command for debug */
95	ha->mcp = mcp;
96
97	DEBUG11(printk("scsi(%ld): prepare to issue mbox cmd=0x%x.\n",
98	    base_vha->host_no, mcp->mb[0]));
99
100	spin_lock_irqsave(&ha->hardware_lock, flags);
101
102	/* Load mailbox registers. */
103	if (IS_QLA82XX(ha))
104		optr = (uint16_t __iomem *)&reg->isp82.mailbox_in[0];
105	else if (IS_FWI2_CAPABLE(ha) && !IS_QLA82XX(ha))
106		optr = (uint16_t __iomem *)&reg->isp24.mailbox0;
107	else
108		optr = (uint16_t __iomem *)MAILBOX_REG(ha, &reg->isp, 0);
109
110	iptr = mcp->mb;
111	command = mcp->mb[0];
112	mboxes = mcp->out_mb;
113
114	for (cnt = 0; cnt < ha->mbx_count; cnt++) {
115		if (IS_QLA2200(ha) && cnt == 8)
116			optr =
117			    (uint16_t __iomem *)MAILBOX_REG(ha, &reg->isp, 8);
118		if (mboxes & BIT_0)
119			WRT_REG_WORD(optr, *iptr);
120
121		mboxes >>= 1;
122		optr++;
123		iptr++;
124	}
125
126#if defined(QL_DEBUG_LEVEL_1)
127	printk("%s(%ld): Loaded MBX registers (displayed in bytes) = \n",
128	    __func__, base_vha->host_no);
129	qla2x00_dump_buffer((uint8_t *)mcp->mb, 16);
130	printk("\n");
131	qla2x00_dump_buffer(((uint8_t *)mcp->mb + 0x10), 16);
132	printk("\n");
133	qla2x00_dump_buffer(((uint8_t *)mcp->mb + 0x20), 8);
134	printk("\n");
135	printk("%s(%ld): I/O address = %p.\n", __func__, base_vha->host_no,
136		optr);
137	qla2x00_dump_regs(base_vha);
138#endif
139
140	/* Issue set host interrupt command to send cmd out. */
141	ha->flags.mbox_int = 0;
142	clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
143
144	/* Unlock mbx registers and wait for interrupt */
145	DEBUG11(printk("%s(%ld): going to unlock irq & waiting for interrupt. "
146	    "jiffies=%lx.\n", __func__, base_vha->host_no, jiffies));
147
148	/* Wait for mbx cmd completion until timeout */
149
150	if ((!abort_active && io_lock_on) || IS_NOPOLLING_TYPE(ha)) {
151		set_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
152
153		if (IS_QLA82XX(ha)) {
154			if (RD_REG_DWORD(&reg->isp82.hint) &
155				HINT_MBX_INT_PENDING) {
156				spin_unlock_irqrestore(&ha->hardware_lock,
157					flags);
158				DEBUG2_3_11(printk(KERN_INFO
159				    "%s(%ld): Pending Mailbox timeout. "
160				    "Exiting.\n", __func__, base_vha->host_no));
161				rval = QLA_FUNCTION_TIMEOUT;
162				goto premature_exit;
163			}
164			WRT_REG_DWORD(&reg->isp82.hint, HINT_MBX_INT_PENDING);
165		} else if (IS_FWI2_CAPABLE(ha))
166			WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
167		else
168			WRT_REG_WORD(&reg->isp.hccr, HCCR_SET_HOST_INT);
169		spin_unlock_irqrestore(&ha->hardware_lock, flags);
170
171		wait_for_completion_timeout(&ha->mbx_intr_comp, mcp->tov * HZ);
172
173		clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
174
175	} else {
176		DEBUG3_11(printk("%s(%ld): cmd=%x POLLING MODE.\n", __func__,
177		    base_vha->host_no, command));
178
179		if (IS_QLA82XX(ha)) {
180			if (RD_REG_DWORD(&reg->isp82.hint) &
181				HINT_MBX_INT_PENDING) {
182				spin_unlock_irqrestore(&ha->hardware_lock,
183					flags);
184				DEBUG2_3_11(printk(KERN_INFO
185				    "%s(%ld): Pending Mailbox timeout. "
186				    "Exiting.\n", __func__, base_vha->host_no));
187				rval = QLA_FUNCTION_TIMEOUT;
188				goto premature_exit;
189			}
190			WRT_REG_DWORD(&reg->isp82.hint, HINT_MBX_INT_PENDING);
191		} else if (IS_FWI2_CAPABLE(ha))
192			WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_SET_HOST_INT);
193		else
194			WRT_REG_WORD(&reg->isp.hccr, HCCR_SET_HOST_INT);
195		spin_unlock_irqrestore(&ha->hardware_lock, flags);
196
197		wait_time = jiffies + mcp->tov * HZ; /* wait at most tov secs */
198		while (!ha->flags.mbox_int) {
199			if (time_after(jiffies, wait_time))
200				break;
201
202			/* Check for pending interrupts. */
203			qla2x00_poll(ha->rsp_q_map[0]);
204
205			if (!ha->flags.mbox_int &&
206			    !(IS_QLA2200(ha) &&
207			    command == MBC_LOAD_RISC_RAM_EXTENDED))
208				msleep(10);
209		} /* while */
210		DEBUG17(qla_printk(KERN_WARNING, ha,
211			"Waited %d sec\n",
212			(uint)((jiffies - (wait_time - (mcp->tov * HZ)))/HZ)));
213	}
214
215	/* Check whether we timed out */
216	if (ha->flags.mbox_int) {
217		uint16_t *iptr2;
218
219		DEBUG3_11(printk("%s(%ld): cmd %x completed.\n", __func__,
220		    base_vha->host_no, command));
221
222		/* Got interrupt. Clear the flag. */
223		ha->flags.mbox_int = 0;
224		clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
225
226		if (ha->flags.isp82xx_fw_hung) {
227			ha->flags.mbox_busy = 0;
228			/* Setting Link-Down error */
229			mcp->mb[0] = MBS_LINK_DOWN_ERROR;
230			ha->mcp = NULL;
231			rval = QLA_FUNCTION_FAILED;
232			goto premature_exit;
233		}
234
235		if (ha->mailbox_out[0] != MBS_COMMAND_COMPLETE)
236			rval = QLA_FUNCTION_FAILED;
237
238		/* Load return mailbox registers. */
239		iptr2 = mcp->mb;
240		iptr = (uint16_t *)&ha->mailbox_out[0];
241		mboxes = mcp->in_mb;
242		for (cnt = 0; cnt < ha->mbx_count; cnt++) {
243			if (mboxes & BIT_0)
244				*iptr2 = *iptr;
245
246			mboxes >>= 1;
247			iptr2++;
248			iptr++;
249		}
250	} else {
251
252#if defined(QL_DEBUG_LEVEL_2) || defined(QL_DEBUG_LEVEL_3) || \
253		defined(QL_DEBUG_LEVEL_11)
254		uint16_t mb0;
255		uint32_t ictrl;
256
257		if (IS_FWI2_CAPABLE(ha)) {
258			mb0 = RD_REG_WORD(&reg->isp24.mailbox0);
259			ictrl = RD_REG_DWORD(&reg->isp24.ictrl);
260		} else {
261			mb0 = RD_MAILBOX_REG(ha, &reg->isp, 0);
262			ictrl = RD_REG_WORD(&reg->isp.ictrl);
263		}
264		printk("%s(%ld): **** MB Command Timeout for cmd %x ****\n",
265		    __func__, base_vha->host_no, command);
266		printk("%s(%ld): icontrol=%x jiffies=%lx\n", __func__,
267		    base_vha->host_no, ictrl, jiffies);
268		printk("%s(%ld): *** mailbox[0] = 0x%x ***\n", __func__,
269		    base_vha->host_no, mb0);
270		qla2x00_dump_regs(base_vha);
271#endif
272
273		rval = QLA_FUNCTION_TIMEOUT;
274	}
275
276	ha->flags.mbox_busy = 0;
277
278	/* Clean up */
279	ha->mcp = NULL;
280
281	if ((abort_active || !io_lock_on) && !IS_NOPOLLING_TYPE(ha)) {
282		DEBUG11(printk("%s(%ld): checking for additional resp "
283		    "interrupt.\n", __func__, base_vha->host_no));
284
285		/* polling mode for non isp_abort commands. */
286		qla2x00_poll(ha->rsp_q_map[0]);
287	}
288
289	if (rval == QLA_FUNCTION_TIMEOUT &&
290	    mcp->mb[0] != MBC_GEN_SYSTEM_ERROR) {
291		if (!io_lock_on || (mcp->flags & IOCTL_CMD) ||
292		    ha->flags.eeh_busy) {
293			/* not in dpc. schedule it for dpc to take over. */
294			DEBUG(printk("%s(%ld): timeout schedule "
295			"isp_abort_needed.\n", __func__,
296			base_vha->host_no));
297			DEBUG2_3_11(printk("%s(%ld): timeout schedule "
298			"isp_abort_needed.\n", __func__,
299			base_vha->host_no));
300
301			if (!test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) &&
302			    !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) &&
303			    !test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
304
305				qla_printk(KERN_WARNING, ha,
306				    "Mailbox command timeout occurred. "
307				    "Scheduling ISP " "abort. eeh_busy: 0x%x\n",
308				    ha->flags.eeh_busy);
309				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
310				qla2xxx_wake_dpc(vha);
311			}
312		} else if (!abort_active) {
313			/* call abort directly since we are in the DPC thread */
314			DEBUG(printk("%s(%ld): timeout calling abort_isp\n",
315			    __func__, base_vha->host_no));
316			DEBUG2_3_11(printk("%s(%ld): timeout calling "
317			    "abort_isp\n", __func__, base_vha->host_no));
318
319			if (!test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) &&
320			    !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) &&
321			    !test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
322
323				qla_printk(KERN_WARNING, ha,
324				    "Mailbox command timeout occurred. "
325				    "Issuing ISP abort.\n");
326
327				set_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
328				clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
329				if (ha->isp_ops->abort_isp(vha)) {
330					/* Failed. retry later. */
331					set_bit(ISP_ABORT_NEEDED,
332					    &vha->dpc_flags);
333				}
334				clear_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags);
335				DEBUG(printk("%s(%ld): finished abort_isp\n",
336				    __func__, vha->host_no));
337				DEBUG2_3_11(printk(
338				    "%s(%ld): finished abort_isp\n",
339				    __func__, vha->host_no));
340			}
341		}
342	}
343
344premature_exit:
345	/* Allow next mbx cmd to come in. */
346	complete(&ha->mbx_cmd_comp);
347
348	if (rval) {
349		DEBUG2_3_11(printk("%s(%ld): **** FAILED. mbx0=%x, mbx1=%x, "
350		    "mbx2=%x, cmd=%x ****\n", __func__, base_vha->host_no,
351		    mcp->mb[0], mcp->mb[1], mcp->mb[2], command));
352	} else {
353		DEBUG11(printk("%s(%ld): done.\n", __func__,
354		base_vha->host_no));
355	}
356
357	return rval;
358}
359
360int
361qla2x00_load_ram(scsi_qla_host_t *vha, dma_addr_t req_dma, uint32_t risc_addr,
362    uint32_t risc_code_size)
363{
364	int rval;
365	struct qla_hw_data *ha = vha->hw;
366	mbx_cmd_t mc;
367	mbx_cmd_t *mcp = &mc;
368
369	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
370
371	if (MSW(risc_addr) || IS_FWI2_CAPABLE(ha)) {
372		mcp->mb[0] = MBC_LOAD_RISC_RAM_EXTENDED;
373		mcp->mb[8] = MSW(risc_addr);
374		mcp->out_mb = MBX_8|MBX_0;
375	} else {
376		mcp->mb[0] = MBC_LOAD_RISC_RAM;
377		mcp->out_mb = MBX_0;
378	}
379	mcp->mb[1] = LSW(risc_addr);
380	mcp->mb[2] = MSW(req_dma);
381	mcp->mb[3] = LSW(req_dma);
382	mcp->mb[6] = MSW(MSD(req_dma));
383	mcp->mb[7] = LSW(MSD(req_dma));
384	mcp->out_mb |= MBX_7|MBX_6|MBX_3|MBX_2|MBX_1;
385	if (IS_FWI2_CAPABLE(ha)) {
386		mcp->mb[4] = MSW(risc_code_size);
387		mcp->mb[5] = LSW(risc_code_size);
388		mcp->out_mb |= MBX_5|MBX_4;
389	} else {
390		mcp->mb[4] = LSW(risc_code_size);
391		mcp->out_mb |= MBX_4;
392	}
393
394	mcp->in_mb = MBX_0;
395	mcp->tov = MBX_TOV_SECONDS;
396	mcp->flags = 0;
397	rval = qla2x00_mailbox_command(vha, mcp);
398
399	if (rval != QLA_SUCCESS) {
400		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
401		    vha->host_no, rval, mcp->mb[0]));
402	} else {
403		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
404	}
405
406	return rval;
407}
408
409#define	EXTENDED_BB_CREDITS	BIT_0
410/*
411 * qla2x00_execute_fw
412 *     Start adapter firmware.
413 *
414 * Input:
415 *     ha = adapter block pointer.
416 *     TARGET_QUEUE_LOCK must be released.
417 *     ADAPTER_STATE_LOCK must be released.
418 *
419 * Returns:
420 *     qla2x00 local function return status code.
421 *
422 * Context:
423 *     Kernel context.
424 */
425int
426qla2x00_execute_fw(scsi_qla_host_t *vha, uint32_t risc_addr)
427{
428	int rval;
429	struct qla_hw_data *ha = vha->hw;
430	mbx_cmd_t mc;
431	mbx_cmd_t *mcp = &mc;
432
433	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
434
435	mcp->mb[0] = MBC_EXECUTE_FIRMWARE;
436	mcp->out_mb = MBX_0;
437	mcp->in_mb = MBX_0;
438	if (IS_FWI2_CAPABLE(ha)) {
439		mcp->mb[1] = MSW(risc_addr);
440		mcp->mb[2] = LSW(risc_addr);
441		mcp->mb[3] = 0;
442		if (IS_QLA81XX(ha)) {
443			struct nvram_81xx *nv = ha->nvram;
444			mcp->mb[4] = (nv->enhanced_features &
445			    EXTENDED_BB_CREDITS);
446		} else
447			mcp->mb[4] = 0;
448		mcp->out_mb |= MBX_4|MBX_3|MBX_2|MBX_1;
449		mcp->in_mb |= MBX_1;
450	} else {
451		mcp->mb[1] = LSW(risc_addr);
452		mcp->out_mb |= MBX_1;
453		if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
454			mcp->mb[2] = 0;
455			mcp->out_mb |= MBX_2;
456		}
457	}
458
459	mcp->tov = MBX_TOV_SECONDS;
460	mcp->flags = 0;
461	rval = qla2x00_mailbox_command(vha, mcp);
462
463	if (rval != QLA_SUCCESS) {
464		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
465		    vha->host_no, rval, mcp->mb[0]));
466	} else {
467		if (IS_FWI2_CAPABLE(ha)) {
468			DEBUG11(printk("%s(%ld): done exchanges=%x.\n",
469			    __func__, vha->host_no, mcp->mb[1]));
470		} else {
471			DEBUG11(printk("%s(%ld): done.\n", __func__,
472			    vha->host_no));
473		}
474	}
475
476	return rval;
477}
478
479/*
480 * qla2x00_get_fw_version
481 *	Get firmware version.
482 *
483 * Input:
484 *	ha:		adapter state pointer.
485 *	major:		pointer for major number.
486 *	minor:		pointer for minor number.
487 *	subminor:	pointer for subminor number.
488 *
489 * Returns:
490 *	qla2x00 local function return status code.
491 *
492 * Context:
493 *	Kernel context.
494 */
495int
496qla2x00_get_fw_version(scsi_qla_host_t *vha, uint16_t *major, uint16_t *minor,
497    uint16_t *subminor, uint16_t *attributes, uint32_t *memory, uint8_t *mpi,
498    uint32_t *mpi_caps, uint8_t *phy)
499{
500	int		rval;
501	mbx_cmd_t	mc;
502	mbx_cmd_t	*mcp = &mc;
503
504	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
505
506	mcp->mb[0] = MBC_GET_FIRMWARE_VERSION;
507	mcp->out_mb = MBX_0;
508	mcp->in_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
509	if (IS_QLA81XX(vha->hw))
510		mcp->in_mb |= MBX_13|MBX_12|MBX_11|MBX_10|MBX_9|MBX_8;
511	mcp->flags = 0;
512	mcp->tov = MBX_TOV_SECONDS;
513	rval = qla2x00_mailbox_command(vha, mcp);
514	if (rval != QLA_SUCCESS)
515		goto failed;
516
517	/* Return mailbox data. */
518	*major = mcp->mb[1];
519	*minor = mcp->mb[2];
520	*subminor = mcp->mb[3];
521	*attributes = mcp->mb[6];
522	if (IS_QLA2100(vha->hw) || IS_QLA2200(vha->hw))
523		*memory = 0x1FFFF;			/* Defaults to 128KB. */
524	else
525		*memory = (mcp->mb[5] << 16) | mcp->mb[4];
526	if (IS_QLA81XX(vha->hw)) {
527		mpi[0] = mcp->mb[10] & 0xff;
528		mpi[1] = mcp->mb[11] >> 8;
529		mpi[2] = mcp->mb[11] & 0xff;
530		*mpi_caps = (mcp->mb[12] << 16) | mcp->mb[13];
531		phy[0] = mcp->mb[8] & 0xff;
532		phy[1] = mcp->mb[9] >> 8;
533		phy[2] = mcp->mb[9] & 0xff;
534	}
535failed:
536	if (rval != QLA_SUCCESS) {
537		/*EMPTY*/
538		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
539		    vha->host_no, rval));
540	} else {
541		/*EMPTY*/
542		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
543	}
544	return rval;
545}
546
547/*
548 * qla2x00_get_fw_options
549 *	Set firmware options.
550 *
551 * Input:
552 *	ha = adapter block pointer.
553 *	fwopt = pointer for firmware options.
554 *
555 * Returns:
556 *	qla2x00 local function return status code.
557 *
558 * Context:
559 *	Kernel context.
560 */
561int
562qla2x00_get_fw_options(scsi_qla_host_t *vha, uint16_t *fwopts)
563{
564	int rval;
565	mbx_cmd_t mc;
566	mbx_cmd_t *mcp = &mc;
567
568	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
569
570	mcp->mb[0] = MBC_GET_FIRMWARE_OPTION;
571	mcp->out_mb = MBX_0;
572	mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
573	mcp->tov = MBX_TOV_SECONDS;
574	mcp->flags = 0;
575	rval = qla2x00_mailbox_command(vha, mcp);
576
577	if (rval != QLA_SUCCESS) {
578		/*EMPTY*/
579		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
580		    vha->host_no, rval));
581	} else {
582		fwopts[0] = mcp->mb[0];
583		fwopts[1] = mcp->mb[1];
584		fwopts[2] = mcp->mb[2];
585		fwopts[3] = mcp->mb[3];
586
587		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
588	}
589
590	return rval;
591}
592
593
594/*
595 * qla2x00_set_fw_options
596 *	Set firmware options.
597 *
598 * Input:
599 *	ha = adapter block pointer.
600 *	fwopt = pointer for firmware options.
601 *
602 * Returns:
603 *	qla2x00 local function return status code.
604 *
605 * Context:
606 *	Kernel context.
607 */
608int
609qla2x00_set_fw_options(scsi_qla_host_t *vha, uint16_t *fwopts)
610{
611	int rval;
612	mbx_cmd_t mc;
613	mbx_cmd_t *mcp = &mc;
614
615	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
616
617	mcp->mb[0] = MBC_SET_FIRMWARE_OPTION;
618	mcp->mb[1] = fwopts[1];
619	mcp->mb[2] = fwopts[2];
620	mcp->mb[3] = fwopts[3];
621	mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
622	mcp->in_mb = MBX_0;
623	if (IS_FWI2_CAPABLE(vha->hw)) {
624		mcp->in_mb |= MBX_1;
625	} else {
626		mcp->mb[10] = fwopts[10];
627		mcp->mb[11] = fwopts[11];
628		mcp->mb[12] = 0;	/* Undocumented, but used */
629		mcp->out_mb |= MBX_12|MBX_11|MBX_10;
630	}
631	mcp->tov = MBX_TOV_SECONDS;
632	mcp->flags = 0;
633	rval = qla2x00_mailbox_command(vha, mcp);
634
635	fwopts[0] = mcp->mb[0];
636
637	if (rval != QLA_SUCCESS) {
638		/*EMPTY*/
639		DEBUG2_3_11(printk("%s(%ld): failed=%x (%x/%x).\n", __func__,
640		    vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
641	} else {
642		/*EMPTY*/
643		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
644	}
645
646	return rval;
647}
648
649/*
650 * qla2x00_mbx_reg_test
651 *	Mailbox register wrap test.
652 *
653 * Input:
654 *	ha = adapter block pointer.
655 *	TARGET_QUEUE_LOCK must be released.
656 *	ADAPTER_STATE_LOCK must be released.
657 *
658 * Returns:
659 *	qla2x00 local function return status code.
660 *
661 * Context:
662 *	Kernel context.
663 */
664int
665qla2x00_mbx_reg_test(scsi_qla_host_t *vha)
666{
667	int rval;
668	mbx_cmd_t mc;
669	mbx_cmd_t *mcp = &mc;
670
671	DEBUG11(printk("qla2x00_mbx_reg_test(%ld): entered.\n", vha->host_no));
672
673	mcp->mb[0] = MBC_MAILBOX_REGISTER_TEST;
674	mcp->mb[1] = 0xAAAA;
675	mcp->mb[2] = 0x5555;
676	mcp->mb[3] = 0xAA55;
677	mcp->mb[4] = 0x55AA;
678	mcp->mb[5] = 0xA5A5;
679	mcp->mb[6] = 0x5A5A;
680	mcp->mb[7] = 0x2525;
681	mcp->out_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
682	mcp->in_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
683	mcp->tov = MBX_TOV_SECONDS;
684	mcp->flags = 0;
685	rval = qla2x00_mailbox_command(vha, mcp);
686
687	if (rval == QLA_SUCCESS) {
688		if (mcp->mb[1] != 0xAAAA || mcp->mb[2] != 0x5555 ||
689		    mcp->mb[3] != 0xAA55 || mcp->mb[4] != 0x55AA)
690			rval = QLA_FUNCTION_FAILED;
691		if (mcp->mb[5] != 0xA5A5 || mcp->mb[6] != 0x5A5A ||
692		    mcp->mb[7] != 0x2525)
693			rval = QLA_FUNCTION_FAILED;
694	}
695
696	if (rval != QLA_SUCCESS) {
697		/*EMPTY*/
698		DEBUG2_3_11(printk("qla2x00_mbx_reg_test(%ld): failed=%x.\n",
699		    vha->host_no, rval));
700	} else {
701		/*EMPTY*/
702		DEBUG11(printk("qla2x00_mbx_reg_test(%ld): done.\n",
703		    vha->host_no));
704	}
705
706	return rval;
707}
708
709/*
710 * qla2x00_verify_checksum
711 *	Verify firmware checksum.
712 *
713 * Input:
714 *	ha = adapter block pointer.
715 *	TARGET_QUEUE_LOCK must be released.
716 *	ADAPTER_STATE_LOCK must be released.
717 *
718 * Returns:
719 *	qla2x00 local function return status code.
720 *
721 * Context:
722 *	Kernel context.
723 */
724int
725qla2x00_verify_checksum(scsi_qla_host_t *vha, uint32_t risc_addr)
726{
727	int rval;
728	mbx_cmd_t mc;
729	mbx_cmd_t *mcp = &mc;
730
731	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
732
733	mcp->mb[0] = MBC_VERIFY_CHECKSUM;
734	mcp->out_mb = MBX_0;
735	mcp->in_mb = MBX_0;
736	if (IS_FWI2_CAPABLE(vha->hw)) {
737		mcp->mb[1] = MSW(risc_addr);
738		mcp->mb[2] = LSW(risc_addr);
739		mcp->out_mb |= MBX_2|MBX_1;
740		mcp->in_mb |= MBX_2|MBX_1;
741	} else {
742		mcp->mb[1] = LSW(risc_addr);
743		mcp->out_mb |= MBX_1;
744		mcp->in_mb |= MBX_1;
745	}
746
747	mcp->tov = MBX_TOV_SECONDS;
748	mcp->flags = 0;
749	rval = qla2x00_mailbox_command(vha, mcp);
750
751	if (rval != QLA_SUCCESS) {
752		DEBUG2_3_11(printk("%s(%ld): failed=%x chk sum=%x.\n", __func__,
753		    vha->host_no, rval, IS_FWI2_CAPABLE(vha->hw) ?
754		    (mcp->mb[2] << 16) | mcp->mb[1]: mcp->mb[1]));
755	} else {
756		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
757	}
758
759	return rval;
760}
761
762/*
763 * qla2x00_issue_iocb
764 *	Issue IOCB using mailbox command
765 *
766 * Input:
767 *	ha = adapter state pointer.
768 *	buffer = buffer pointer.
769 *	phys_addr = physical address of buffer.
770 *	size = size of buffer.
771 *	TARGET_QUEUE_LOCK must be released.
772 *	ADAPTER_STATE_LOCK must be released.
773 *
774 * Returns:
775 *	qla2x00 local function return status code.
776 *
777 * Context:
778 *	Kernel context.
779 */
780int
781qla2x00_issue_iocb_timeout(scsi_qla_host_t *vha, void *buffer,
782    dma_addr_t phys_addr, size_t size, uint32_t tov)
783{
784	int		rval;
785	mbx_cmd_t	mc;
786	mbx_cmd_t	*mcp = &mc;
787
788	mcp->mb[0] = MBC_IOCB_COMMAND_A64;
789	mcp->mb[1] = 0;
790	mcp->mb[2] = MSW(phys_addr);
791	mcp->mb[3] = LSW(phys_addr);
792	mcp->mb[6] = MSW(MSD(phys_addr));
793	mcp->mb[7] = LSW(MSD(phys_addr));
794	mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
795	mcp->in_mb = MBX_2|MBX_0;
796	mcp->tov = tov;
797	mcp->flags = 0;
798	rval = qla2x00_mailbox_command(vha, mcp);
799
800	if (rval != QLA_SUCCESS) {
801		/*EMPTY*/
802		DEBUG(printk("qla2x00_issue_iocb(%ld): failed rval 0x%x\n",
803		    vha->host_no, rval));
804	} else {
805		sts_entry_t *sts_entry = (sts_entry_t *) buffer;
806
807		/* Mask reserved bits. */
808		sts_entry->entry_status &=
809		    IS_FWI2_CAPABLE(vha->hw) ? RF_MASK_24XX : RF_MASK;
810	}
811
812	return rval;
813}
814
815int
816qla2x00_issue_iocb(scsi_qla_host_t *vha, void *buffer, dma_addr_t phys_addr,
817    size_t size)
818{
819	return qla2x00_issue_iocb_timeout(vha, buffer, phys_addr, size,
820	    MBX_TOV_SECONDS);
821}
822
823/*
824 * qla2x00_abort_command
825 *	Abort command aborts a specified IOCB.
826 *
827 * Input:
828 *	ha = adapter block pointer.
829 *	sp = SB structure pointer.
830 *
831 * Returns:
832 *	qla2x00 local function return status code.
833 *
834 * Context:
835 *	Kernel context.
836 */
837int
838qla2x00_abort_command(srb_t *sp)
839{
840	unsigned long   flags = 0;
841	int		rval;
842	uint32_t	handle = 0;
843	mbx_cmd_t	mc;
844	mbx_cmd_t	*mcp = &mc;
845	fc_port_t	*fcport = sp->fcport;
846	scsi_qla_host_t *vha = fcport->vha;
847	struct qla_hw_data *ha = vha->hw;
848	struct req_que *req = vha->req;
849
850	DEBUG11(printk("qla2x00_abort_command(%ld): entered.\n", vha->host_no));
851
852	spin_lock_irqsave(&ha->hardware_lock, flags);
853	for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
854		if (req->outstanding_cmds[handle] == sp)
855			break;
856	}
857	spin_unlock_irqrestore(&ha->hardware_lock, flags);
858
859	if (handle == MAX_OUTSTANDING_COMMANDS) {
860		/* command not found */
861		return QLA_FUNCTION_FAILED;
862	}
863
864	mcp->mb[0] = MBC_ABORT_COMMAND;
865	if (HAS_EXTENDED_IDS(ha))
866		mcp->mb[1] = fcport->loop_id;
867	else
868		mcp->mb[1] = fcport->loop_id << 8;
869	mcp->mb[2] = (uint16_t)handle;
870	mcp->mb[3] = (uint16_t)(handle >> 16);
871	mcp->mb[6] = (uint16_t)sp->cmd->device->lun;
872	mcp->out_mb = MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
873	mcp->in_mb = MBX_0;
874	mcp->tov = MBX_TOV_SECONDS;
875	mcp->flags = 0;
876	rval = qla2x00_mailbox_command(vha, mcp);
877
878	if (rval != QLA_SUCCESS) {
879		DEBUG2_3_11(printk("qla2x00_abort_command(%ld): failed=%x.\n",
880		    vha->host_no, rval));
881	} else {
882		DEBUG11(printk("qla2x00_abort_command(%ld): done.\n",
883		    vha->host_no));
884	}
885
886	return rval;
887}
888
889int
890qla2x00_abort_target(struct fc_port *fcport, unsigned int l, int tag)
891{
892	int rval, rval2;
893	mbx_cmd_t  mc;
894	mbx_cmd_t  *mcp = &mc;
895	scsi_qla_host_t *vha;
896	struct req_que *req;
897	struct rsp_que *rsp;
898
899	DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->vha->host_no));
900
901	l = l;
902	vha = fcport->vha;
903	req = vha->hw->req_q_map[0];
904	rsp = req->rsp;
905	mcp->mb[0] = MBC_ABORT_TARGET;
906	mcp->out_mb = MBX_9|MBX_2|MBX_1|MBX_0;
907	if (HAS_EXTENDED_IDS(vha->hw)) {
908		mcp->mb[1] = fcport->loop_id;
909		mcp->mb[10] = 0;
910		mcp->out_mb |= MBX_10;
911	} else {
912		mcp->mb[1] = fcport->loop_id << 8;
913	}
914	mcp->mb[2] = vha->hw->loop_reset_delay;
915	mcp->mb[9] = vha->vp_idx;
916
917	mcp->in_mb = MBX_0;
918	mcp->tov = MBX_TOV_SECONDS;
919	mcp->flags = 0;
920	rval = qla2x00_mailbox_command(vha, mcp);
921	if (rval != QLA_SUCCESS) {
922		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
923		    vha->host_no, rval));
924	}
925
926	/* Issue marker IOCB. */
927	rval2 = qla2x00_marker(vha, req, rsp, fcport->loop_id, 0,
928							MK_SYNC_ID);
929	if (rval2 != QLA_SUCCESS) {
930		DEBUG2_3_11(printk("%s(%ld): failed to issue Marker IOCB "
931		    "(%x).\n", __func__, vha->host_no, rval2));
932	} else {
933		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
934	}
935
936	return rval;
937}
938
939int
940qla2x00_lun_reset(struct fc_port *fcport, unsigned int l, int tag)
941{
942	int rval, rval2;
943	mbx_cmd_t  mc;
944	mbx_cmd_t  *mcp = &mc;
945	scsi_qla_host_t *vha;
946	struct req_que *req;
947	struct rsp_que *rsp;
948
949	DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->vha->host_no));
950
951	vha = fcport->vha;
952	req = vha->hw->req_q_map[0];
953	rsp = req->rsp;
954	mcp->mb[0] = MBC_LUN_RESET;
955	mcp->out_mb = MBX_9|MBX_3|MBX_2|MBX_1|MBX_0;
956	if (HAS_EXTENDED_IDS(vha->hw))
957		mcp->mb[1] = fcport->loop_id;
958	else
959		mcp->mb[1] = fcport->loop_id << 8;
960	mcp->mb[2] = l;
961	mcp->mb[3] = 0;
962	mcp->mb[9] = vha->vp_idx;
963
964	mcp->in_mb = MBX_0;
965	mcp->tov = MBX_TOV_SECONDS;
966	mcp->flags = 0;
967	rval = qla2x00_mailbox_command(vha, mcp);
968	if (rval != QLA_SUCCESS) {
969		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
970		    vha->host_no, rval));
971	}
972
973	/* Issue marker IOCB. */
974	rval2 = qla2x00_marker(vha, req, rsp, fcport->loop_id, l,
975								MK_SYNC_ID_LUN);
976	if (rval2 != QLA_SUCCESS) {
977		DEBUG2_3_11(printk("%s(%ld): failed to issue Marker IOCB "
978		    "(%x).\n", __func__, vha->host_no, rval2));
979	} else {
980		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
981	}
982
983	return rval;
984}
985
986/*
987 * qla2x00_get_adapter_id
988 *	Get adapter ID and topology.
989 *
990 * Input:
991 *	ha = adapter block pointer.
992 *	id = pointer for loop ID.
993 *	al_pa = pointer for AL_PA.
994 *	area = pointer for area.
995 *	domain = pointer for domain.
996 *	top = pointer for topology.
997 *	TARGET_QUEUE_LOCK must be released.
998 *	ADAPTER_STATE_LOCK must be released.
999 *
1000 * Returns:
1001 *	qla2x00 local function return status code.
1002 *
1003 * Context:
1004 *	Kernel context.
1005 */
1006int
1007qla2x00_get_adapter_id(scsi_qla_host_t *vha, uint16_t *id, uint8_t *al_pa,
1008    uint8_t *area, uint8_t *domain, uint16_t *top, uint16_t *sw_cap)
1009{
1010	int rval;
1011	mbx_cmd_t mc;
1012	mbx_cmd_t *mcp = &mc;
1013
1014	DEBUG11(printk("qla2x00_get_adapter_id(%ld): entered.\n",
1015	    vha->host_no));
1016
1017	mcp->mb[0] = MBC_GET_ADAPTER_LOOP_ID;
1018	mcp->mb[9] = vha->vp_idx;
1019	mcp->out_mb = MBX_9|MBX_0;
1020	mcp->in_mb = MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1021	if (IS_QLA8XXX_TYPE(vha->hw))
1022		mcp->in_mb |= MBX_13|MBX_12|MBX_11|MBX_10;
1023	mcp->tov = MBX_TOV_SECONDS;
1024	mcp->flags = 0;
1025	rval = qla2x00_mailbox_command(vha, mcp);
1026	if (mcp->mb[0] == MBS_COMMAND_ERROR)
1027		rval = QLA_COMMAND_ERROR;
1028	else if (mcp->mb[0] == MBS_INVALID_COMMAND)
1029		rval = QLA_INVALID_COMMAND;
1030
1031	/* Return data. */
1032	*id = mcp->mb[1];
1033	*al_pa = LSB(mcp->mb[2]);
1034	*area = MSB(mcp->mb[2]);
1035	*domain	= LSB(mcp->mb[3]);
1036	*top = mcp->mb[6];
1037	*sw_cap = mcp->mb[7];
1038
1039	if (rval != QLA_SUCCESS) {
1040		/*EMPTY*/
1041		DEBUG2_3_11(printk("qla2x00_get_adapter_id(%ld): failed=%x.\n",
1042		    vha->host_no, rval));
1043	} else {
1044		DEBUG11(printk("qla2x00_get_adapter_id(%ld): done.\n",
1045		    vha->host_no));
1046
1047		if (IS_QLA8XXX_TYPE(vha->hw)) {
1048			vha->fcoe_vlan_id = mcp->mb[9] & 0xfff;
1049			vha->fcoe_fcf_idx = mcp->mb[10];
1050			vha->fcoe_vn_port_mac[5] = mcp->mb[11] >> 8;
1051			vha->fcoe_vn_port_mac[4] = mcp->mb[11] & 0xff;
1052			vha->fcoe_vn_port_mac[3] = mcp->mb[12] >> 8;
1053			vha->fcoe_vn_port_mac[2] = mcp->mb[12] & 0xff;
1054			vha->fcoe_vn_port_mac[1] = mcp->mb[13] >> 8;
1055			vha->fcoe_vn_port_mac[0] = mcp->mb[13] & 0xff;
1056		}
1057	}
1058
1059	return rval;
1060}
1061
1062/*
1063 * qla2x00_get_retry_cnt
1064 *	Get current firmware login retry count and delay.
1065 *
1066 * Input:
1067 *	ha = adapter block pointer.
1068 *	retry_cnt = pointer to login retry count.
1069 *	tov = pointer to login timeout value.
1070 *
1071 * Returns:
1072 *	qla2x00 local function return status code.
1073 *
1074 * Context:
1075 *	Kernel context.
1076 */
1077int
1078qla2x00_get_retry_cnt(scsi_qla_host_t *vha, uint8_t *retry_cnt, uint8_t *tov,
1079    uint16_t *r_a_tov)
1080{
1081	int rval;
1082	uint16_t ratov;
1083	mbx_cmd_t mc;
1084	mbx_cmd_t *mcp = &mc;
1085
1086	DEBUG11(printk("qla2x00_get_retry_cnt(%ld): entered.\n",
1087			vha->host_no));
1088
1089	mcp->mb[0] = MBC_GET_RETRY_COUNT;
1090	mcp->out_mb = MBX_0;
1091	mcp->in_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1092	mcp->tov = MBX_TOV_SECONDS;
1093	mcp->flags = 0;
1094	rval = qla2x00_mailbox_command(vha, mcp);
1095
1096	if (rval != QLA_SUCCESS) {
1097		/*EMPTY*/
1098		DEBUG2_3_11(printk("qla2x00_get_retry_cnt(%ld): failed = %x.\n",
1099		    vha->host_no, mcp->mb[0]));
1100	} else {
1101		/* Convert returned data and check our values. */
1102		*r_a_tov = mcp->mb[3] / 2;
1103		ratov = (mcp->mb[3]/2) / 10;  /* mb[3] value is in 100ms */
1104		if (mcp->mb[1] * ratov > (*retry_cnt) * (*tov)) {
1105			/* Update to the larger values */
1106			*retry_cnt = (uint8_t)mcp->mb[1];
1107			*tov = ratov;
1108		}
1109
1110		DEBUG11(printk("qla2x00_get_retry_cnt(%ld): done. mb3=%d "
1111		    "ratov=%d.\n", vha->host_no, mcp->mb[3], ratov));
1112	}
1113
1114	return rval;
1115}
1116
1117/*
1118 * qla2x00_init_firmware
1119 *	Initialize adapter firmware.
1120 *
1121 * Input:
1122 *	ha = adapter block pointer.
1123 *	dptr = Initialization control block pointer.
1124 *	size = size of initialization control block.
1125 *	TARGET_QUEUE_LOCK must be released.
1126 *	ADAPTER_STATE_LOCK must be released.
1127 *
1128 * Returns:
1129 *	qla2x00 local function return status code.
1130 *
1131 * Context:
1132 *	Kernel context.
1133 */
1134int
1135qla2x00_init_firmware(scsi_qla_host_t *vha, uint16_t size)
1136{
1137	int rval;
1138	mbx_cmd_t mc;
1139	mbx_cmd_t *mcp = &mc;
1140	struct qla_hw_data *ha = vha->hw;
1141
1142	DEBUG11(printk("qla2x00_init_firmware(%ld): entered.\n",
1143	    vha->host_no));
1144
1145	if (IS_QLA82XX(ha) && ql2xdbwr)
1146		qla82xx_wr_32(ha, ha->nxdb_wr_ptr,
1147			(0x04 | (ha->portnum << 5) | (0 << 8) | (0 << 16)));
1148
1149	if (ha->flags.npiv_supported)
1150		mcp->mb[0] = MBC_MID_INITIALIZE_FIRMWARE;
1151	else
1152		mcp->mb[0] = MBC_INITIALIZE_FIRMWARE;
1153
1154	mcp->mb[1] = 0;
1155	mcp->mb[2] = MSW(ha->init_cb_dma);
1156	mcp->mb[3] = LSW(ha->init_cb_dma);
1157	mcp->mb[6] = MSW(MSD(ha->init_cb_dma));
1158	mcp->mb[7] = LSW(MSD(ha->init_cb_dma));
1159	mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1160	if (IS_QLA81XX(ha) && ha->ex_init_cb->ex_version) {
1161		mcp->mb[1] = BIT_0;
1162		mcp->mb[10] = MSW(ha->ex_init_cb_dma);
1163		mcp->mb[11] = LSW(ha->ex_init_cb_dma);
1164		mcp->mb[12] = MSW(MSD(ha->ex_init_cb_dma));
1165		mcp->mb[13] = LSW(MSD(ha->ex_init_cb_dma));
1166		mcp->mb[14] = sizeof(*ha->ex_init_cb);
1167		mcp->out_mb |= MBX_14|MBX_13|MBX_12|MBX_11|MBX_10;
1168	}
1169	mcp->in_mb = MBX_0;
1170	mcp->buf_size = size;
1171	mcp->flags = MBX_DMA_OUT;
1172	mcp->tov = MBX_TOV_SECONDS;
1173	rval = qla2x00_mailbox_command(vha, mcp);
1174
1175	if (rval != QLA_SUCCESS) {
1176		/*EMPTY*/
1177		DEBUG2_3_11(printk("qla2x00_init_firmware(%ld): failed=%x "
1178		    "mb0=%x.\n",
1179		    vha->host_no, rval, mcp->mb[0]));
1180	} else {
1181		/*EMPTY*/
1182		DEBUG11(printk("qla2x00_init_firmware(%ld): done.\n",
1183		    vha->host_no));
1184	}
1185
1186	return rval;
1187}
1188
1189/*
1190 * qla2x00_get_port_database
1191 *	Issue normal/enhanced get port database mailbox command
1192 *	and copy device name as necessary.
1193 *
1194 * Input:
1195 *	ha = adapter state pointer.
1196 *	dev = structure pointer.
1197 *	opt = enhanced cmd option byte.
1198 *
1199 * Returns:
1200 *	qla2x00 local function return status code.
1201 *
1202 * Context:
1203 *	Kernel context.
1204 */
1205int
1206qla2x00_get_port_database(scsi_qla_host_t *vha, fc_port_t *fcport, uint8_t opt)
1207{
1208	int rval;
1209	mbx_cmd_t mc;
1210	mbx_cmd_t *mcp = &mc;
1211	port_database_t *pd;
1212	struct port_database_24xx *pd24;
1213	dma_addr_t pd_dma;
1214	struct qla_hw_data *ha = vha->hw;
1215
1216	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1217
1218	pd24 = NULL;
1219	pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1220	if (pd  == NULL) {
1221		DEBUG2_3(printk("%s(%ld): failed to allocate Port Database "
1222		    "structure.\n", __func__, vha->host_no));
1223		return QLA_MEMORY_ALLOC_FAILED;
1224	}
1225	memset(pd, 0, max(PORT_DATABASE_SIZE, PORT_DATABASE_24XX_SIZE));
1226
1227	mcp->mb[0] = MBC_GET_PORT_DATABASE;
1228	if (opt != 0 && !IS_FWI2_CAPABLE(ha))
1229		mcp->mb[0] = MBC_ENHANCED_GET_PORT_DATABASE;
1230	mcp->mb[2] = MSW(pd_dma);
1231	mcp->mb[3] = LSW(pd_dma);
1232	mcp->mb[6] = MSW(MSD(pd_dma));
1233	mcp->mb[7] = LSW(MSD(pd_dma));
1234	mcp->mb[9] = vha->vp_idx;
1235	mcp->out_mb = MBX_9|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
1236	mcp->in_mb = MBX_0;
1237	if (IS_FWI2_CAPABLE(ha)) {
1238		mcp->mb[1] = fcport->loop_id;
1239		mcp->mb[10] = opt;
1240		mcp->out_mb |= MBX_10|MBX_1;
1241		mcp->in_mb |= MBX_1;
1242	} else if (HAS_EXTENDED_IDS(ha)) {
1243		mcp->mb[1] = fcport->loop_id;
1244		mcp->mb[10] = opt;
1245		mcp->out_mb |= MBX_10|MBX_1;
1246	} else {
1247		mcp->mb[1] = fcport->loop_id << 8 | opt;
1248		mcp->out_mb |= MBX_1;
1249	}
1250	mcp->buf_size = IS_FWI2_CAPABLE(ha) ?
1251	    PORT_DATABASE_24XX_SIZE : PORT_DATABASE_SIZE;
1252	mcp->flags = MBX_DMA_IN;
1253	mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1254	rval = qla2x00_mailbox_command(vha, mcp);
1255	if (rval != QLA_SUCCESS)
1256		goto gpd_error_out;
1257
1258	if (IS_FWI2_CAPABLE(ha)) {
1259		pd24 = (struct port_database_24xx *) pd;
1260
1261		/* Check for logged in state. */
1262		if (pd24->current_login_state != PDS_PRLI_COMPLETE &&
1263		    pd24->last_login_state != PDS_PRLI_COMPLETE) {
1264			DEBUG2(qla_printk(KERN_WARNING, ha,
1265			   "scsi(%ld): Unable to verify login-state (%x/%x) "
1266			   " - portid=%02x%02x%02x.\n", vha->host_no,
1267			   pd24->current_login_state, pd24->last_login_state,
1268			   fcport->d_id.b.domain, fcport->d_id.b.area,
1269			   fcport->d_id.b.al_pa));
1270			rval = QLA_FUNCTION_FAILED;
1271			goto gpd_error_out;
1272		}
1273
1274		/* Names are little-endian. */
1275		memcpy(fcport->node_name, pd24->node_name, WWN_SIZE);
1276		memcpy(fcport->port_name, pd24->port_name, WWN_SIZE);
1277
1278		/* Get port_id of device. */
1279		fcport->d_id.b.domain = pd24->port_id[0];
1280		fcport->d_id.b.area = pd24->port_id[1];
1281		fcport->d_id.b.al_pa = pd24->port_id[2];
1282		fcport->d_id.b.rsvd_1 = 0;
1283
1284		/* If not target must be initiator or unknown type. */
1285		if ((pd24->prli_svc_param_word_3[0] & BIT_4) == 0)
1286			fcport->port_type = FCT_INITIATOR;
1287		else
1288			fcport->port_type = FCT_TARGET;
1289	} else {
1290		/* Check for logged in state. */
1291		if (pd->master_state != PD_STATE_PORT_LOGGED_IN &&
1292		    pd->slave_state != PD_STATE_PORT_LOGGED_IN) {
1293			DEBUG2(qla_printk(KERN_WARNING, ha,
1294			   "scsi(%ld): Unable to verify login-state (%x/%x) "
1295			   " - portid=%02x%02x%02x.\n", vha->host_no,
1296			   pd->master_state, pd->slave_state,
1297			   fcport->d_id.b.domain, fcport->d_id.b.area,
1298			   fcport->d_id.b.al_pa));
1299			rval = QLA_FUNCTION_FAILED;
1300			goto gpd_error_out;
1301		}
1302
1303		/* Names are little-endian. */
1304		memcpy(fcport->node_name, pd->node_name, WWN_SIZE);
1305		memcpy(fcport->port_name, pd->port_name, WWN_SIZE);
1306
1307		/* Get port_id of device. */
1308		fcport->d_id.b.domain = pd->port_id[0];
1309		fcport->d_id.b.area = pd->port_id[3];
1310		fcport->d_id.b.al_pa = pd->port_id[2];
1311		fcport->d_id.b.rsvd_1 = 0;
1312
1313		/* If not target must be initiator or unknown type. */
1314		if ((pd->prli_svc_param_word_3[0] & BIT_4) == 0)
1315			fcport->port_type = FCT_INITIATOR;
1316		else
1317			fcport->port_type = FCT_TARGET;
1318
1319		/* Passback COS information. */
1320		fcport->supported_classes = (pd->options & BIT_4) ?
1321		    FC_COS_CLASS2: FC_COS_CLASS3;
1322	}
1323
1324gpd_error_out:
1325	dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1326
1327	if (rval != QLA_SUCCESS) {
1328		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
1329		    __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1330	} else {
1331		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
1332	}
1333
1334	return rval;
1335}
1336
1337/*
1338 * qla2x00_get_firmware_state
1339 *	Get adapter firmware state.
1340 *
1341 * Input:
1342 *	ha = adapter block pointer.
1343 *	dptr = pointer for firmware state.
1344 *	TARGET_QUEUE_LOCK must be released.
1345 *	ADAPTER_STATE_LOCK must be released.
1346 *
1347 * Returns:
1348 *	qla2x00 local function return status code.
1349 *
1350 * Context:
1351 *	Kernel context.
1352 */
1353int
1354qla2x00_get_firmware_state(scsi_qla_host_t *vha, uint16_t *states)
1355{
1356	int rval;
1357	mbx_cmd_t mc;
1358	mbx_cmd_t *mcp = &mc;
1359
1360	DEBUG11(printk("qla2x00_get_firmware_state(%ld): entered.\n",
1361	    vha->host_no));
1362
1363	mcp->mb[0] = MBC_GET_FIRMWARE_STATE;
1364	mcp->out_mb = MBX_0;
1365	if (IS_FWI2_CAPABLE(vha->hw))
1366		mcp->in_mb = MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
1367	else
1368		mcp->in_mb = MBX_1|MBX_0;
1369	mcp->tov = MBX_TOV_SECONDS;
1370	mcp->flags = 0;
1371	rval = qla2x00_mailbox_command(vha, mcp);
1372
1373	/* Return firmware states. */
1374	states[0] = mcp->mb[1];
1375	if (IS_FWI2_CAPABLE(vha->hw)) {
1376		states[1] = mcp->mb[2];
1377		states[2] = mcp->mb[3];
1378		states[3] = mcp->mb[4];
1379		states[4] = mcp->mb[5];
1380	}
1381
1382	if (rval != QLA_SUCCESS) {
1383		/*EMPTY*/
1384		DEBUG2_3_11(printk("qla2x00_get_firmware_state(%ld): "
1385		    "failed=%x.\n", vha->host_no, rval));
1386	} else {
1387		/*EMPTY*/
1388		DEBUG11(printk("qla2x00_get_firmware_state(%ld): done.\n",
1389		    vha->host_no));
1390	}
1391
1392	return rval;
1393}
1394
1395/*
1396 * qla2x00_get_port_name
1397 *	Issue get port name mailbox command.
1398 *	Returned name is in big endian format.
1399 *
1400 * Input:
1401 *	ha = adapter block pointer.
1402 *	loop_id = loop ID of device.
1403 *	name = pointer for name.
1404 *	TARGET_QUEUE_LOCK must be released.
1405 *	ADAPTER_STATE_LOCK must be released.
1406 *
1407 * Returns:
1408 *	qla2x00 local function return status code.
1409 *
1410 * Context:
1411 *	Kernel context.
1412 */
1413int
1414qla2x00_get_port_name(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t *name,
1415    uint8_t opt)
1416{
1417	int rval;
1418	mbx_cmd_t mc;
1419	mbx_cmd_t *mcp = &mc;
1420
1421	DEBUG11(printk("qla2x00_get_port_name(%ld): entered.\n",
1422	    vha->host_no));
1423
1424	mcp->mb[0] = MBC_GET_PORT_NAME;
1425	mcp->mb[9] = vha->vp_idx;
1426	mcp->out_mb = MBX_9|MBX_1|MBX_0;
1427	if (HAS_EXTENDED_IDS(vha->hw)) {
1428		mcp->mb[1] = loop_id;
1429		mcp->mb[10] = opt;
1430		mcp->out_mb |= MBX_10;
1431	} else {
1432		mcp->mb[1] = loop_id << 8 | opt;
1433	}
1434
1435	mcp->in_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1436	mcp->tov = MBX_TOV_SECONDS;
1437	mcp->flags = 0;
1438	rval = qla2x00_mailbox_command(vha, mcp);
1439
1440	if (rval != QLA_SUCCESS) {
1441		/*EMPTY*/
1442		DEBUG2_3_11(printk("qla2x00_get_port_name(%ld): failed=%x.\n",
1443		    vha->host_no, rval));
1444	} else {
1445		if (name != NULL) {
1446			/* This function returns name in big endian. */
1447			name[0] = MSB(mcp->mb[2]);
1448			name[1] = LSB(mcp->mb[2]);
1449			name[2] = MSB(mcp->mb[3]);
1450			name[3] = LSB(mcp->mb[3]);
1451			name[4] = MSB(mcp->mb[6]);
1452			name[5] = LSB(mcp->mb[6]);
1453			name[6] = MSB(mcp->mb[7]);
1454			name[7] = LSB(mcp->mb[7]);
1455		}
1456
1457		DEBUG11(printk("qla2x00_get_port_name(%ld): done.\n",
1458		    vha->host_no));
1459	}
1460
1461	return rval;
1462}
1463
1464/*
1465 * qla2x00_lip_reset
1466 *	Issue LIP reset mailbox command.
1467 *
1468 * Input:
1469 *	ha = adapter block pointer.
1470 *	TARGET_QUEUE_LOCK must be released.
1471 *	ADAPTER_STATE_LOCK must be released.
1472 *
1473 * Returns:
1474 *	qla2x00 local function return status code.
1475 *
1476 * Context:
1477 *	Kernel context.
1478 */
1479int
1480qla2x00_lip_reset(scsi_qla_host_t *vha)
1481{
1482	int rval;
1483	mbx_cmd_t mc;
1484	mbx_cmd_t *mcp = &mc;
1485
1486	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1487
1488	if (IS_QLA8XXX_TYPE(vha->hw)) {
1489		/* Logout across all FCFs. */
1490		mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1491		mcp->mb[1] = BIT_1;
1492		mcp->mb[2] = 0;
1493		mcp->out_mb = MBX_2|MBX_1|MBX_0;
1494	} else if (IS_FWI2_CAPABLE(vha->hw)) {
1495		mcp->mb[0] = MBC_LIP_FULL_LOGIN;
1496		mcp->mb[1] = BIT_6;
1497		mcp->mb[2] = 0;
1498		mcp->mb[3] = vha->hw->loop_reset_delay;
1499		mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1500	} else {
1501		mcp->mb[0] = MBC_LIP_RESET;
1502		mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1503		if (HAS_EXTENDED_IDS(vha->hw)) {
1504			mcp->mb[1] = 0x00ff;
1505			mcp->mb[10] = 0;
1506			mcp->out_mb |= MBX_10;
1507		} else {
1508			mcp->mb[1] = 0xff00;
1509		}
1510		mcp->mb[2] = vha->hw->loop_reset_delay;
1511		mcp->mb[3] = 0;
1512	}
1513	mcp->in_mb = MBX_0;
1514	mcp->tov = MBX_TOV_SECONDS;
1515	mcp->flags = 0;
1516	rval = qla2x00_mailbox_command(vha, mcp);
1517
1518	if (rval != QLA_SUCCESS) {
1519		/*EMPTY*/
1520		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n",
1521		    __func__, vha->host_no, rval));
1522	} else {
1523		/*EMPTY*/
1524		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
1525	}
1526
1527	return rval;
1528}
1529
1530/*
1531 * qla2x00_send_sns
1532 *	Send SNS command.
1533 *
1534 * Input:
1535 *	ha = adapter block pointer.
1536 *	sns = pointer for command.
1537 *	cmd_size = command size.
1538 *	buf_size = response/command size.
1539 *	TARGET_QUEUE_LOCK must be released.
1540 *	ADAPTER_STATE_LOCK must be released.
1541 *
1542 * Returns:
1543 *	qla2x00 local function return status code.
1544 *
1545 * Context:
1546 *	Kernel context.
1547 */
1548int
1549qla2x00_send_sns(scsi_qla_host_t *vha, dma_addr_t sns_phys_address,
1550    uint16_t cmd_size, size_t buf_size)
1551{
1552	int rval;
1553	mbx_cmd_t mc;
1554	mbx_cmd_t *mcp = &mc;
1555
1556	DEBUG11(printk("qla2x00_send_sns(%ld): entered.\n",
1557	    vha->host_no));
1558
1559	DEBUG11(printk("qla2x00_send_sns: retry cnt=%d ratov=%d total "
1560		"tov=%d.\n", vha->hw->retry_count, vha->hw->login_timeout,
1561		mcp->tov));
1562
1563	mcp->mb[0] = MBC_SEND_SNS_COMMAND;
1564	mcp->mb[1] = cmd_size;
1565	mcp->mb[2] = MSW(sns_phys_address);
1566	mcp->mb[3] = LSW(sns_phys_address);
1567	mcp->mb[6] = MSW(MSD(sns_phys_address));
1568	mcp->mb[7] = LSW(MSD(sns_phys_address));
1569	mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
1570	mcp->in_mb = MBX_0|MBX_1;
1571	mcp->buf_size = buf_size;
1572	mcp->flags = MBX_DMA_OUT|MBX_DMA_IN;
1573	mcp->tov = (vha->hw->login_timeout * 2) + (vha->hw->login_timeout / 2);
1574	rval = qla2x00_mailbox_command(vha, mcp);
1575
1576	if (rval != QLA_SUCCESS) {
1577		/*EMPTY*/
1578		DEBUG(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1579		    "mb[1]=%x.\n", vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1580		DEBUG2_3_11(printk("qla2x00_send_sns(%ld): failed=%x mb[0]=%x "
1581		    "mb[1]=%x.\n", vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
1582	} else {
1583		/*EMPTY*/
1584		DEBUG11(printk("qla2x00_send_sns(%ld): done.\n", vha->host_no));
1585	}
1586
1587	return rval;
1588}
1589
1590int
1591qla24xx_login_fabric(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
1592    uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1593{
1594	int		rval;
1595
1596	struct logio_entry_24xx *lg;
1597	dma_addr_t	lg_dma;
1598	uint32_t	iop[2];
1599	struct qla_hw_data *ha = vha->hw;
1600	struct req_que *req;
1601	struct rsp_que *rsp;
1602
1603	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1604
1605	if (ha->flags.cpu_affinity_enabled)
1606		req = ha->req_q_map[0];
1607	else
1608		req = vha->req;
1609	rsp = req->rsp;
1610
1611	lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1612	if (lg == NULL) {
1613		DEBUG2_3(printk("%s(%ld): failed to allocate Login IOCB.\n",
1614		    __func__, vha->host_no));
1615		return QLA_MEMORY_ALLOC_FAILED;
1616	}
1617	memset(lg, 0, sizeof(struct logio_entry_24xx));
1618
1619	lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1620	lg->entry_count = 1;
1621	lg->handle = MAKE_HANDLE(req->id, lg->handle);
1622	lg->nport_handle = cpu_to_le16(loop_id);
1623	lg->control_flags = __constant_cpu_to_le16(LCF_COMMAND_PLOGI);
1624	if (opt & BIT_0)
1625		lg->control_flags |= __constant_cpu_to_le16(LCF_COND_PLOGI);
1626	if (opt & BIT_1)
1627		lg->control_flags |= __constant_cpu_to_le16(LCF_SKIP_PRLI);
1628	lg->port_id[0] = al_pa;
1629	lg->port_id[1] = area;
1630	lg->port_id[2] = domain;
1631	lg->vp_index = vha->vp_idx;
1632	rval = qla2x00_issue_iocb(vha, lg, lg_dma, 0);
1633	if (rval != QLA_SUCCESS) {
1634		DEBUG2_3_11(printk("%s(%ld): failed to issue Login IOCB "
1635		    "(%x).\n", __func__, vha->host_no, rval));
1636	} else if (lg->entry_status != 0) {
1637		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1638		    "-- error status (%x).\n", __func__, vha->host_no,
1639		    lg->entry_status));
1640		rval = QLA_FUNCTION_FAILED;
1641	} else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1642		iop[0] = le32_to_cpu(lg->io_parameter[0]);
1643		iop[1] = le32_to_cpu(lg->io_parameter[1]);
1644
1645		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1646		    "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1647		    vha->host_no, le16_to_cpu(lg->comp_status), iop[0],
1648		    iop[1]));
1649
1650		switch (iop[0]) {
1651		case LSC_SCODE_PORTID_USED:
1652			mb[0] = MBS_PORT_ID_USED;
1653			mb[1] = LSW(iop[1]);
1654			break;
1655		case LSC_SCODE_NPORT_USED:
1656			mb[0] = MBS_LOOP_ID_USED;
1657			break;
1658		case LSC_SCODE_NOLINK:
1659		case LSC_SCODE_NOIOCB:
1660		case LSC_SCODE_NOXCB:
1661		case LSC_SCODE_CMD_FAILED:
1662		case LSC_SCODE_NOFABRIC:
1663		case LSC_SCODE_FW_NOT_READY:
1664		case LSC_SCODE_NOT_LOGGED_IN:
1665		case LSC_SCODE_NOPCB:
1666		case LSC_SCODE_ELS_REJECT:
1667		case LSC_SCODE_CMD_PARAM_ERR:
1668		case LSC_SCODE_NONPORT:
1669		case LSC_SCODE_LOGGED_IN:
1670		case LSC_SCODE_NOFLOGI_ACC:
1671		default:
1672			mb[0] = MBS_COMMAND_ERROR;
1673			break;
1674		}
1675	} else {
1676		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
1677
1678		iop[0] = le32_to_cpu(lg->io_parameter[0]);
1679
1680		mb[0] = MBS_COMMAND_COMPLETE;
1681		mb[1] = 0;
1682		if (iop[0] & BIT_4) {
1683			if (iop[0] & BIT_8)
1684				mb[1] |= BIT_1;
1685		} else
1686			mb[1] = BIT_0;
1687
1688		/* Passback COS information. */
1689		mb[10] = 0;
1690		if (lg->io_parameter[7] || lg->io_parameter[8])
1691			mb[10] |= BIT_0;	/* Class 2. */
1692		if (lg->io_parameter[9] || lg->io_parameter[10])
1693			mb[10] |= BIT_1;	/* Class 3. */
1694	}
1695
1696	dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1697
1698	return rval;
1699}
1700
1701/*
1702 * qla2x00_login_fabric
1703 *	Issue login fabric port mailbox command.
1704 *
1705 * Input:
1706 *	ha = adapter block pointer.
1707 *	loop_id = device loop ID.
1708 *	domain = device domain.
1709 *	area = device area.
1710 *	al_pa = device AL_PA.
1711 *	status = pointer for return status.
1712 *	opt = command options.
1713 *	TARGET_QUEUE_LOCK must be released.
1714 *	ADAPTER_STATE_LOCK must be released.
1715 *
1716 * Returns:
1717 *	qla2x00 local function return status code.
1718 *
1719 * Context:
1720 *	Kernel context.
1721 */
1722int
1723qla2x00_login_fabric(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
1724    uint8_t area, uint8_t al_pa, uint16_t *mb, uint8_t opt)
1725{
1726	int rval;
1727	mbx_cmd_t mc;
1728	mbx_cmd_t *mcp = &mc;
1729	struct qla_hw_data *ha = vha->hw;
1730
1731	DEBUG11(printk("qla2x00_login_fabric(%ld): entered.\n", vha->host_no));
1732
1733	mcp->mb[0] = MBC_LOGIN_FABRIC_PORT;
1734	mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
1735	if (HAS_EXTENDED_IDS(ha)) {
1736		mcp->mb[1] = loop_id;
1737		mcp->mb[10] = opt;
1738		mcp->out_mb |= MBX_10;
1739	} else {
1740		mcp->mb[1] = (loop_id << 8) | opt;
1741	}
1742	mcp->mb[2] = domain;
1743	mcp->mb[3] = area << 8 | al_pa;
1744
1745	mcp->in_mb = MBX_7|MBX_6|MBX_2|MBX_1|MBX_0;
1746	mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1747	mcp->flags = 0;
1748	rval = qla2x00_mailbox_command(vha, mcp);
1749
1750	/* Return mailbox statuses. */
1751	if (mb != NULL) {
1752		mb[0] = mcp->mb[0];
1753		mb[1] = mcp->mb[1];
1754		mb[2] = mcp->mb[2];
1755		mb[6] = mcp->mb[6];
1756		mb[7] = mcp->mb[7];
1757		/* COS retrieved from Get-Port-Database mailbox command. */
1758		mb[10] = 0;
1759	}
1760
1761	if (rval != QLA_SUCCESS) {
1762		/* RLU tmp code: need to change main mailbox_command function to
1763		 * return ok even when the mailbox completion value is not
1764		 * SUCCESS. The caller needs to be responsible to interpret
1765		 * the return values of this mailbox command if we're not
1766		 * to change too much of the existing code.
1767		 */
1768		if (mcp->mb[0] == 0x4001 || mcp->mb[0] == 0x4002 ||
1769		    mcp->mb[0] == 0x4003 || mcp->mb[0] == 0x4005 ||
1770		    mcp->mb[0] == 0x4006)
1771			rval = QLA_SUCCESS;
1772
1773		/*EMPTY*/
1774		DEBUG2_3_11(printk("qla2x00_login_fabric(%ld): failed=%x "
1775		    "mb[0]=%x mb[1]=%x mb[2]=%x.\n", vha->host_no, rval,
1776		    mcp->mb[0], mcp->mb[1], mcp->mb[2]));
1777	} else {
1778		/*EMPTY*/
1779		DEBUG11(printk("qla2x00_login_fabric(%ld): done.\n",
1780		    vha->host_no));
1781	}
1782
1783	return rval;
1784}
1785
1786/*
1787 * qla2x00_login_local_device
1788 *           Issue login loop port mailbox command.
1789 *
1790 * Input:
1791 *           ha = adapter block pointer.
1792 *           loop_id = device loop ID.
1793 *           opt = command options.
1794 *
1795 * Returns:
1796 *            Return status code.
1797 *
1798 * Context:
1799 *            Kernel context.
1800 *
1801 */
1802int
1803qla2x00_login_local_device(scsi_qla_host_t *vha, fc_port_t *fcport,
1804    uint16_t *mb_ret, uint8_t opt)
1805{
1806	int rval;
1807	mbx_cmd_t mc;
1808	mbx_cmd_t *mcp = &mc;
1809	struct qla_hw_data *ha = vha->hw;
1810
1811	if (IS_FWI2_CAPABLE(ha))
1812		return qla24xx_login_fabric(vha, fcport->loop_id,
1813		    fcport->d_id.b.domain, fcport->d_id.b.area,
1814		    fcport->d_id.b.al_pa, mb_ret, opt);
1815
1816	DEBUG3(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1817
1818	mcp->mb[0] = MBC_LOGIN_LOOP_PORT;
1819	if (HAS_EXTENDED_IDS(ha))
1820		mcp->mb[1] = fcport->loop_id;
1821	else
1822		mcp->mb[1] = fcport->loop_id << 8;
1823	mcp->mb[2] = opt;
1824	mcp->out_mb = MBX_2|MBX_1|MBX_0;
1825 	mcp->in_mb = MBX_7|MBX_6|MBX_1|MBX_0;
1826	mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
1827	mcp->flags = 0;
1828	rval = qla2x00_mailbox_command(vha, mcp);
1829
1830 	/* Return mailbox statuses. */
1831 	if (mb_ret != NULL) {
1832 		mb_ret[0] = mcp->mb[0];
1833 		mb_ret[1] = mcp->mb[1];
1834 		mb_ret[6] = mcp->mb[6];
1835 		mb_ret[7] = mcp->mb[7];
1836 	}
1837
1838	if (rval != QLA_SUCCESS) {
1839 		/* AV tmp code: need to change main mailbox_command function to
1840 		 * return ok even when the mailbox completion value is not
1841 		 * SUCCESS. The caller needs to be responsible to interpret
1842 		 * the return values of this mailbox command if we're not
1843 		 * to change too much of the existing code.
1844 		 */
1845 		if (mcp->mb[0] == 0x4005 || mcp->mb[0] == 0x4006)
1846 			rval = QLA_SUCCESS;
1847
1848		DEBUG(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1849		    "mb[6]=%x mb[7]=%x.\n", __func__, vha->host_no, rval,
1850		    mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]));
1851		DEBUG2_3(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
1852		    "mb[6]=%x mb[7]=%x.\n", __func__, vha->host_no, rval,
1853		    mcp->mb[0], mcp->mb[1], mcp->mb[6], mcp->mb[7]));
1854	} else {
1855		/*EMPTY*/
1856		DEBUG3(printk("%s(%ld): done.\n", __func__, vha->host_no));
1857	}
1858
1859	return (rval);
1860}
1861
1862int
1863qla24xx_fabric_logout(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
1864    uint8_t area, uint8_t al_pa)
1865{
1866	int		rval;
1867	struct logio_entry_24xx *lg;
1868	dma_addr_t	lg_dma;
1869	struct qla_hw_data *ha = vha->hw;
1870	struct req_que *req;
1871	struct rsp_que *rsp;
1872
1873	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
1874
1875	lg = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &lg_dma);
1876	if (lg == NULL) {
1877		DEBUG2_3(printk("%s(%ld): failed to allocate Logout IOCB.\n",
1878		    __func__, vha->host_no));
1879		return QLA_MEMORY_ALLOC_FAILED;
1880	}
1881	memset(lg, 0, sizeof(struct logio_entry_24xx));
1882
1883	if (ql2xmaxqueues > 1)
1884		req = ha->req_q_map[0];
1885	else
1886		req = vha->req;
1887	rsp = req->rsp;
1888	lg->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1889	lg->entry_count = 1;
1890	lg->handle = MAKE_HANDLE(req->id, lg->handle);
1891	lg->nport_handle = cpu_to_le16(loop_id);
1892	lg->control_flags =
1893	    __constant_cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO|
1894		LCF_FREE_NPORT);
1895	lg->port_id[0] = al_pa;
1896	lg->port_id[1] = area;
1897	lg->port_id[2] = domain;
1898	lg->vp_index = vha->vp_idx;
1899
1900	rval = qla2x00_issue_iocb(vha, lg, lg_dma, 0);
1901	if (rval != QLA_SUCCESS) {
1902		DEBUG2_3_11(printk("%s(%ld): failed to issue Logout IOCB "
1903		    "(%x).\n", __func__, vha->host_no, rval));
1904	} else if (lg->entry_status != 0) {
1905		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
1906		    "-- error status (%x).\n", __func__, vha->host_no,
1907		    lg->entry_status));
1908		rval = QLA_FUNCTION_FAILED;
1909	} else if (lg->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
1910		DEBUG2_3_11(printk("%s(%ld %d): failed to complete IOCB "
1911		    "-- completion status (%x)  ioparam=%x/%x.\n", __func__,
1912		    vha->host_no, vha->vp_idx, le16_to_cpu(lg->comp_status),
1913		    le32_to_cpu(lg->io_parameter[0]),
1914		    le32_to_cpu(lg->io_parameter[1])));
1915	} else {
1916		/*EMPTY*/
1917		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
1918	}
1919
1920	dma_pool_free(ha->s_dma_pool, lg, lg_dma);
1921
1922	return rval;
1923}
1924
1925/*
1926 * qla2x00_fabric_logout
1927 *	Issue logout fabric port mailbox command.
1928 *
1929 * Input:
1930 *	ha = adapter block pointer.
1931 *	loop_id = device loop ID.
1932 *	TARGET_QUEUE_LOCK must be released.
1933 *	ADAPTER_STATE_LOCK must be released.
1934 *
1935 * Returns:
1936 *	qla2x00 local function return status code.
1937 *
1938 * Context:
1939 *	Kernel context.
1940 */
1941int
1942qla2x00_fabric_logout(scsi_qla_host_t *vha, uint16_t loop_id, uint8_t domain,
1943    uint8_t area, uint8_t al_pa)
1944{
1945	int rval;
1946	mbx_cmd_t mc;
1947	mbx_cmd_t *mcp = &mc;
1948
1949	DEBUG11(printk("qla2x00_fabric_logout(%ld): entered.\n",
1950	    vha->host_no));
1951
1952	mcp->mb[0] = MBC_LOGOUT_FABRIC_PORT;
1953	mcp->out_mb = MBX_1|MBX_0;
1954	if (HAS_EXTENDED_IDS(vha->hw)) {
1955		mcp->mb[1] = loop_id;
1956		mcp->mb[10] = 0;
1957		mcp->out_mb |= MBX_10;
1958	} else {
1959		mcp->mb[1] = loop_id << 8;
1960	}
1961
1962	mcp->in_mb = MBX_1|MBX_0;
1963	mcp->tov = MBX_TOV_SECONDS;
1964	mcp->flags = 0;
1965	rval = qla2x00_mailbox_command(vha, mcp);
1966
1967	if (rval != QLA_SUCCESS) {
1968		/*EMPTY*/
1969		DEBUG2_3_11(printk("qla2x00_fabric_logout(%ld): failed=%x "
1970		    "mbx1=%x.\n", vha->host_no, rval, mcp->mb[1]));
1971	} else {
1972		/*EMPTY*/
1973		DEBUG11(printk("qla2x00_fabric_logout(%ld): done.\n",
1974		    vha->host_no));
1975	}
1976
1977	return rval;
1978}
1979
1980/*
1981 * qla2x00_full_login_lip
1982 *	Issue full login LIP mailbox command.
1983 *
1984 * Input:
1985 *	ha = adapter block pointer.
1986 *	TARGET_QUEUE_LOCK must be released.
1987 *	ADAPTER_STATE_LOCK must be released.
1988 *
1989 * Returns:
1990 *	qla2x00 local function return status code.
1991 *
1992 * Context:
1993 *	Kernel context.
1994 */
1995int
1996qla2x00_full_login_lip(scsi_qla_host_t *vha)
1997{
1998	int rval;
1999	mbx_cmd_t mc;
2000	mbx_cmd_t *mcp = &mc;
2001
2002	DEBUG11(printk("qla2x00_full_login_lip(%ld): entered.\n",
2003	    vha->host_no));
2004
2005	mcp->mb[0] = MBC_LIP_FULL_LOGIN;
2006	mcp->mb[1] = IS_FWI2_CAPABLE(vha->hw) ? BIT_3 : 0;
2007	mcp->mb[2] = 0;
2008	mcp->mb[3] = 0;
2009	mcp->out_mb = MBX_3|MBX_2|MBX_1|MBX_0;
2010	mcp->in_mb = MBX_0;
2011	mcp->tov = MBX_TOV_SECONDS;
2012	mcp->flags = 0;
2013	rval = qla2x00_mailbox_command(vha, mcp);
2014
2015	if (rval != QLA_SUCCESS) {
2016		/*EMPTY*/
2017		DEBUG2_3_11(printk("qla2x00_full_login_lip(%ld): failed=%x.\n",
2018		    vha->host_no, rval));
2019	} else {
2020		/*EMPTY*/
2021		DEBUG11(printk("qla2x00_full_login_lip(%ld): done.\n",
2022		    vha->host_no));
2023	}
2024
2025	return rval;
2026}
2027
2028/*
2029 * qla2x00_get_id_list
2030 *
2031 * Input:
2032 *	ha = adapter block pointer.
2033 *
2034 * Returns:
2035 *	qla2x00 local function return status code.
2036 *
2037 * Context:
2038 *	Kernel context.
2039 */
2040int
2041qla2x00_get_id_list(scsi_qla_host_t *vha, void *id_list, dma_addr_t id_list_dma,
2042    uint16_t *entries)
2043{
2044	int rval;
2045	mbx_cmd_t mc;
2046	mbx_cmd_t *mcp = &mc;
2047
2048	DEBUG11(printk("qla2x00_get_id_list(%ld): entered.\n",
2049	    vha->host_no));
2050
2051	if (id_list == NULL)
2052		return QLA_FUNCTION_FAILED;
2053
2054	mcp->mb[0] = MBC_GET_ID_LIST;
2055	mcp->out_mb = MBX_0;
2056	if (IS_FWI2_CAPABLE(vha->hw)) {
2057		mcp->mb[2] = MSW(id_list_dma);
2058		mcp->mb[3] = LSW(id_list_dma);
2059		mcp->mb[6] = MSW(MSD(id_list_dma));
2060		mcp->mb[7] = LSW(MSD(id_list_dma));
2061		mcp->mb[8] = 0;
2062		mcp->mb[9] = vha->vp_idx;
2063		mcp->out_mb |= MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2;
2064	} else {
2065		mcp->mb[1] = MSW(id_list_dma);
2066		mcp->mb[2] = LSW(id_list_dma);
2067		mcp->mb[3] = MSW(MSD(id_list_dma));
2068		mcp->mb[6] = LSW(MSD(id_list_dma));
2069		mcp->out_mb |= MBX_6|MBX_3|MBX_2|MBX_1;
2070	}
2071	mcp->in_mb = MBX_1|MBX_0;
2072	mcp->tov = MBX_TOV_SECONDS;
2073	mcp->flags = 0;
2074	rval = qla2x00_mailbox_command(vha, mcp);
2075
2076	if (rval != QLA_SUCCESS) {
2077		/*EMPTY*/
2078		DEBUG2_3_11(printk("qla2x00_get_id_list(%ld): failed=%x.\n",
2079		    vha->host_no, rval));
2080	} else {
2081		*entries = mcp->mb[1];
2082		DEBUG11(printk("qla2x00_get_id_list(%ld): done.\n",
2083		    vha->host_no));
2084	}
2085
2086	return rval;
2087}
2088
2089/*
2090 * qla2x00_get_resource_cnts
2091 *	Get current firmware resource counts.
2092 *
2093 * Input:
2094 *	ha = adapter block pointer.
2095 *
2096 * Returns:
2097 *	qla2x00 local function return status code.
2098 *
2099 * Context:
2100 *	Kernel context.
2101 */
2102int
2103qla2x00_get_resource_cnts(scsi_qla_host_t *vha, uint16_t *cur_xchg_cnt,
2104    uint16_t *orig_xchg_cnt, uint16_t *cur_iocb_cnt,
2105    uint16_t *orig_iocb_cnt, uint16_t *max_npiv_vports, uint16_t *max_fcfs)
2106{
2107	int rval;
2108	mbx_cmd_t mc;
2109	mbx_cmd_t *mcp = &mc;
2110
2111	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2112
2113	mcp->mb[0] = MBC_GET_RESOURCE_COUNTS;
2114	mcp->out_mb = MBX_0;
2115	mcp->in_mb = MBX_11|MBX_10|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
2116	if (IS_QLA81XX(vha->hw))
2117		mcp->in_mb |= MBX_12;
2118	mcp->tov = MBX_TOV_SECONDS;
2119	mcp->flags = 0;
2120	rval = qla2x00_mailbox_command(vha, mcp);
2121
2122	if (rval != QLA_SUCCESS) {
2123		/*EMPTY*/
2124		DEBUG2_3_11(printk("%s(%ld): failed = %x.\n", __func__,
2125		    vha->host_no, mcp->mb[0]));
2126	} else {
2127		DEBUG11(printk("%s(%ld): done. mb1=%x mb2=%x mb3=%x mb6=%x "
2128		    "mb7=%x mb10=%x mb11=%x mb12=%x.\n", __func__,
2129		    vha->host_no, mcp->mb[1], mcp->mb[2], mcp->mb[3],
2130		    mcp->mb[6], mcp->mb[7], mcp->mb[10], mcp->mb[11],
2131		    mcp->mb[12]));
2132
2133		if (cur_xchg_cnt)
2134			*cur_xchg_cnt = mcp->mb[3];
2135		if (orig_xchg_cnt)
2136			*orig_xchg_cnt = mcp->mb[6];
2137		if (cur_iocb_cnt)
2138			*cur_iocb_cnt = mcp->mb[7];
2139		if (orig_iocb_cnt)
2140			*orig_iocb_cnt = mcp->mb[10];
2141		if (vha->hw->flags.npiv_supported && max_npiv_vports)
2142			*max_npiv_vports = mcp->mb[11];
2143		if (IS_QLA81XX(vha->hw) && max_fcfs)
2144			*max_fcfs = mcp->mb[12];
2145	}
2146
2147	return (rval);
2148}
2149
2150#if defined(QL_DEBUG_LEVEL_3)
2151/*
2152 * qla2x00_get_fcal_position_map
2153 *	Get FCAL (LILP) position map using mailbox command
2154 *
2155 * Input:
2156 *	ha = adapter state pointer.
2157 *	pos_map = buffer pointer (can be NULL).
2158 *
2159 * Returns:
2160 *	qla2x00 local function return status code.
2161 *
2162 * Context:
2163 *	Kernel context.
2164 */
2165int
2166qla2x00_get_fcal_position_map(scsi_qla_host_t *vha, char *pos_map)
2167{
2168	int rval;
2169	mbx_cmd_t mc;
2170	mbx_cmd_t *mcp = &mc;
2171	char *pmap;
2172	dma_addr_t pmap_dma;
2173	struct qla_hw_data *ha = vha->hw;
2174
2175	pmap = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &pmap_dma);
2176	if (pmap  == NULL) {
2177		DEBUG2_3_11(printk("%s(%ld): **** Mem Alloc Failed ****",
2178		    __func__, vha->host_no));
2179		return QLA_MEMORY_ALLOC_FAILED;
2180	}
2181	memset(pmap, 0, FCAL_MAP_SIZE);
2182
2183	mcp->mb[0] = MBC_GET_FC_AL_POSITION_MAP;
2184	mcp->mb[2] = MSW(pmap_dma);
2185	mcp->mb[3] = LSW(pmap_dma);
2186	mcp->mb[6] = MSW(MSD(pmap_dma));
2187	mcp->mb[7] = LSW(MSD(pmap_dma));
2188	mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2189	mcp->in_mb = MBX_1|MBX_0;
2190	mcp->buf_size = FCAL_MAP_SIZE;
2191	mcp->flags = MBX_DMA_IN;
2192	mcp->tov = (ha->login_timeout * 2) + (ha->login_timeout / 2);
2193	rval = qla2x00_mailbox_command(vha, mcp);
2194
2195	if (rval == QLA_SUCCESS) {
2196		DEBUG11(printk("%s(%ld): (mb0=%x/mb1=%x) FC/AL Position Map "
2197		    "size (%x)\n", __func__, vha->host_no, mcp->mb[0],
2198		    mcp->mb[1], (unsigned)pmap[0]));
2199		DEBUG11(qla2x00_dump_buffer(pmap, pmap[0] + 1));
2200
2201		if (pos_map)
2202			memcpy(pos_map, pmap, FCAL_MAP_SIZE);
2203	}
2204	dma_pool_free(ha->s_dma_pool, pmap, pmap_dma);
2205
2206	if (rval != QLA_SUCCESS) {
2207		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2208		    vha->host_no, rval));
2209	} else {
2210		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2211	}
2212
2213	return rval;
2214}
2215#endif
2216
2217/*
2218 * qla2x00_get_link_status
2219 *
2220 * Input:
2221 *	ha = adapter block pointer.
2222 *	loop_id = device loop ID.
2223 *	ret_buf = pointer to link status return buffer.
2224 *
2225 * Returns:
2226 *	0 = success.
2227 *	BIT_0 = mem alloc error.
2228 *	BIT_1 = mailbox error.
2229 */
2230int
2231qla2x00_get_link_status(scsi_qla_host_t *vha, uint16_t loop_id,
2232    struct link_statistics *stats, dma_addr_t stats_dma)
2233{
2234	int rval;
2235	mbx_cmd_t mc;
2236	mbx_cmd_t *mcp = &mc;
2237	uint32_t *siter, *diter, dwords;
2238	struct qla_hw_data *ha = vha->hw;
2239
2240	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2241
2242	mcp->mb[0] = MBC_GET_LINK_STATUS;
2243	mcp->mb[2] = MSW(stats_dma);
2244	mcp->mb[3] = LSW(stats_dma);
2245	mcp->mb[6] = MSW(MSD(stats_dma));
2246	mcp->mb[7] = LSW(MSD(stats_dma));
2247	mcp->out_mb = MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2248	mcp->in_mb = MBX_0;
2249	if (IS_FWI2_CAPABLE(ha)) {
2250		mcp->mb[1] = loop_id;
2251		mcp->mb[4] = 0;
2252		mcp->mb[10] = 0;
2253		mcp->out_mb |= MBX_10|MBX_4|MBX_1;
2254		mcp->in_mb |= MBX_1;
2255	} else if (HAS_EXTENDED_IDS(ha)) {
2256		mcp->mb[1] = loop_id;
2257		mcp->mb[10] = 0;
2258		mcp->out_mb |= MBX_10|MBX_1;
2259	} else {
2260		mcp->mb[1] = loop_id << 8;
2261		mcp->out_mb |= MBX_1;
2262	}
2263	mcp->tov = MBX_TOV_SECONDS;
2264	mcp->flags = IOCTL_CMD;
2265	rval = qla2x00_mailbox_command(vha, mcp);
2266
2267	if (rval == QLA_SUCCESS) {
2268		if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2269			DEBUG2_3_11(printk("%s(%ld): cmd failed. mbx0=%x.\n",
2270			    __func__, vha->host_no, mcp->mb[0]));
2271			rval = QLA_FUNCTION_FAILED;
2272		} else {
2273			/* Copy over data -- firmware data is LE. */
2274			dwords = offsetof(struct link_statistics, unused1) / 4;
2275			siter = diter = &stats->link_fail_cnt;
2276			while (dwords--)
2277				*diter++ = le32_to_cpu(*siter++);
2278		}
2279	} else {
2280		/* Failed. */
2281		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2282		    vha->host_no, rval));
2283	}
2284
2285	return rval;
2286}
2287
2288int
2289qla24xx_get_isp_stats(scsi_qla_host_t *vha, struct link_statistics *stats,
2290    dma_addr_t stats_dma)
2291{
2292	int rval;
2293	mbx_cmd_t mc;
2294	mbx_cmd_t *mcp = &mc;
2295	uint32_t *siter, *diter, dwords;
2296
2297	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2298
2299	mcp->mb[0] = MBC_GET_LINK_PRIV_STATS;
2300	mcp->mb[2] = MSW(stats_dma);
2301	mcp->mb[3] = LSW(stats_dma);
2302	mcp->mb[6] = MSW(MSD(stats_dma));
2303	mcp->mb[7] = LSW(MSD(stats_dma));
2304	mcp->mb[8] = sizeof(struct link_statistics) / 4;
2305	mcp->mb[9] = vha->vp_idx;
2306	mcp->mb[10] = 0;
2307	mcp->out_mb = MBX_10|MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
2308	mcp->in_mb = MBX_2|MBX_1|MBX_0;
2309	mcp->tov = MBX_TOV_SECONDS;
2310	mcp->flags = IOCTL_CMD;
2311	rval = qla2x00_mailbox_command(vha, mcp);
2312
2313	if (rval == QLA_SUCCESS) {
2314		if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
2315			DEBUG2_3_11(printk("%s(%ld): cmd failed. mbx0=%x.\n",
2316			    __func__, vha->host_no, mcp->mb[0]));
2317			rval = QLA_FUNCTION_FAILED;
2318		} else {
2319			/* Copy over data -- firmware data is LE. */
2320			dwords = sizeof(struct link_statistics) / 4;
2321			siter = diter = &stats->link_fail_cnt;
2322			while (dwords--)
2323				*diter++ = le32_to_cpu(*siter++);
2324		}
2325	} else {
2326		/* Failed. */
2327		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2328		    vha->host_no, rval));
2329	}
2330
2331	return rval;
2332}
2333
2334int
2335qla24xx_abort_command(srb_t *sp)
2336{
2337	int		rval;
2338	unsigned long   flags = 0;
2339
2340	struct abort_entry_24xx *abt;
2341	dma_addr_t	abt_dma;
2342	uint32_t	handle;
2343	fc_port_t	*fcport = sp->fcport;
2344	struct scsi_qla_host *vha = fcport->vha;
2345	struct qla_hw_data *ha = vha->hw;
2346	struct req_que *req = vha->req;
2347
2348	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2349
2350	spin_lock_irqsave(&ha->hardware_lock, flags);
2351	for (handle = 1; handle < MAX_OUTSTANDING_COMMANDS; handle++) {
2352		if (req->outstanding_cmds[handle] == sp)
2353			break;
2354	}
2355	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2356	if (handle == MAX_OUTSTANDING_COMMANDS) {
2357		/* Command not found. */
2358		return QLA_FUNCTION_FAILED;
2359	}
2360
2361	abt = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &abt_dma);
2362	if (abt == NULL) {
2363		DEBUG2_3(printk("%s(%ld): failed to allocate Abort IOCB.\n",
2364		    __func__, vha->host_no));
2365		return QLA_MEMORY_ALLOC_FAILED;
2366	}
2367	memset(abt, 0, sizeof(struct abort_entry_24xx));
2368
2369	abt->entry_type = ABORT_IOCB_TYPE;
2370	abt->entry_count = 1;
2371	abt->handle = MAKE_HANDLE(req->id, abt->handle);
2372	abt->nport_handle = cpu_to_le16(fcport->loop_id);
2373	abt->handle_to_abort = MAKE_HANDLE(req->id, handle);
2374	abt->port_id[0] = fcport->d_id.b.al_pa;
2375	abt->port_id[1] = fcport->d_id.b.area;
2376	abt->port_id[2] = fcport->d_id.b.domain;
2377	abt->vp_index = fcport->vp_idx;
2378
2379	abt->req_que_no = cpu_to_le16(req->id);
2380
2381	rval = qla2x00_issue_iocb(vha, abt, abt_dma, 0);
2382	if (rval != QLA_SUCCESS) {
2383		DEBUG2_3_11(printk("%s(%ld): failed to issue IOCB (%x).\n",
2384		    __func__, vha->host_no, rval));
2385	} else if (abt->entry_status != 0) {
2386		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2387		    "-- error status (%x).\n", __func__, vha->host_no,
2388		    abt->entry_status));
2389		rval = QLA_FUNCTION_FAILED;
2390	} else if (abt->nport_handle != __constant_cpu_to_le16(0)) {
2391		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2392		    "-- completion status (%x).\n", __func__, vha->host_no,
2393		    le16_to_cpu(abt->nport_handle)));
2394		rval = QLA_FUNCTION_FAILED;
2395	} else {
2396		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2397	}
2398
2399	dma_pool_free(ha->s_dma_pool, abt, abt_dma);
2400
2401	return rval;
2402}
2403
2404struct tsk_mgmt_cmd {
2405	union {
2406		struct tsk_mgmt_entry tsk;
2407		struct sts_entry_24xx sts;
2408	} p;
2409};
2410
2411static int
2412__qla24xx_issue_tmf(char *name, uint32_t type, struct fc_port *fcport,
2413    unsigned int l, int tag)
2414{
2415	int		rval, rval2;
2416	struct tsk_mgmt_cmd *tsk;
2417	struct sts_entry_24xx *sts;
2418	dma_addr_t	tsk_dma;
2419	scsi_qla_host_t *vha;
2420	struct qla_hw_data *ha;
2421	struct req_que *req;
2422	struct rsp_que *rsp;
2423
2424	DEBUG11(printk("%s(%ld): entered.\n", __func__, fcport->vha->host_no));
2425
2426	vha = fcport->vha;
2427	ha = vha->hw;
2428	req = vha->req;
2429	if (ha->flags.cpu_affinity_enabled)
2430		rsp = ha->rsp_q_map[tag + 1];
2431	else
2432		rsp = req->rsp;
2433	tsk = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &tsk_dma);
2434	if (tsk == NULL) {
2435		DEBUG2_3(printk("%s(%ld): failed to allocate Task Management "
2436		    "IOCB.\n", __func__, vha->host_no));
2437		return QLA_MEMORY_ALLOC_FAILED;
2438	}
2439	memset(tsk, 0, sizeof(struct tsk_mgmt_cmd));
2440
2441	tsk->p.tsk.entry_type = TSK_MGMT_IOCB_TYPE;
2442	tsk->p.tsk.entry_count = 1;
2443	tsk->p.tsk.handle = MAKE_HANDLE(req->id, tsk->p.tsk.handle);
2444	tsk->p.tsk.nport_handle = cpu_to_le16(fcport->loop_id);
2445	tsk->p.tsk.timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2446	tsk->p.tsk.control_flags = cpu_to_le32(type);
2447	tsk->p.tsk.port_id[0] = fcport->d_id.b.al_pa;
2448	tsk->p.tsk.port_id[1] = fcport->d_id.b.area;
2449	tsk->p.tsk.port_id[2] = fcport->d_id.b.domain;
2450	tsk->p.tsk.vp_index = fcport->vp_idx;
2451	if (type == TCF_LUN_RESET) {
2452		int_to_scsilun(l, &tsk->p.tsk.lun);
2453		host_to_fcp_swap((uint8_t *)&tsk->p.tsk.lun,
2454		    sizeof(tsk->p.tsk.lun));
2455	}
2456
2457	sts = &tsk->p.sts;
2458	rval = qla2x00_issue_iocb(vha, tsk, tsk_dma, 0);
2459	if (rval != QLA_SUCCESS) {
2460		DEBUG2_3_11(printk("%s(%ld): failed to issue %s Reset IOCB "
2461		    "(%x).\n", __func__, vha->host_no, name, rval));
2462	} else if (sts->entry_status != 0) {
2463		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2464		    "-- error status (%x).\n", __func__, vha->host_no,
2465		    sts->entry_status));
2466		rval = QLA_FUNCTION_FAILED;
2467	} else if (sts->comp_status !=
2468	    __constant_cpu_to_le16(CS_COMPLETE)) {
2469		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2470		    "-- completion status (%x).\n", __func__,
2471		    vha->host_no, le16_to_cpu(sts->comp_status)));
2472		rval = QLA_FUNCTION_FAILED;
2473	} else if (le16_to_cpu(sts->scsi_status) &
2474	    SS_RESPONSE_INFO_LEN_VALID) {
2475		if (le32_to_cpu(sts->rsp_data_len) < 4) {
2476			DEBUG2_3_11(printk("%s(%ld): ignoring inconsistent "
2477			    "data length -- not enough response info (%d).\n",
2478			    __func__, vha->host_no,
2479			    le32_to_cpu(sts->rsp_data_len)));
2480		} else if (sts->data[3]) {
2481			DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2482			    "-- response (%x).\n", __func__,
2483			    vha->host_no, sts->data[3]));
2484			rval = QLA_FUNCTION_FAILED;
2485		}
2486	}
2487
2488	/* Issue marker IOCB. */
2489	rval2 = qla2x00_marker(vha, req, rsp, fcport->loop_id, l,
2490	    type == TCF_LUN_RESET ? MK_SYNC_ID_LUN: MK_SYNC_ID);
2491	if (rval2 != QLA_SUCCESS) {
2492		DEBUG2_3_11(printk("%s(%ld): failed to issue Marker IOCB "
2493		    "(%x).\n", __func__, vha->host_no, rval2));
2494	} else {
2495		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2496	}
2497
2498	dma_pool_free(ha->s_dma_pool, tsk, tsk_dma);
2499
2500	return rval;
2501}
2502
2503int
2504qla24xx_abort_target(struct fc_port *fcport, unsigned int l, int tag)
2505{
2506	struct qla_hw_data *ha = fcport->vha->hw;
2507
2508	if ((ql2xasynctmfenable) && IS_FWI2_CAPABLE(ha))
2509		return qla2x00_async_tm_cmd(fcport, TCF_TARGET_RESET, l, tag);
2510
2511	return __qla24xx_issue_tmf("Target", TCF_TARGET_RESET, fcport, l, tag);
2512}
2513
2514int
2515qla24xx_lun_reset(struct fc_port *fcport, unsigned int l, int tag)
2516{
2517	struct qla_hw_data *ha = fcport->vha->hw;
2518
2519	if ((ql2xasynctmfenable) && IS_FWI2_CAPABLE(ha))
2520		return qla2x00_async_tm_cmd(fcport, TCF_LUN_RESET, l, tag);
2521
2522	return __qla24xx_issue_tmf("Lun", TCF_LUN_RESET, fcport, l, tag);
2523}
2524
2525int
2526qla2x00_system_error(scsi_qla_host_t *vha)
2527{
2528	int rval;
2529	mbx_cmd_t mc;
2530	mbx_cmd_t *mcp = &mc;
2531	struct qla_hw_data *ha = vha->hw;
2532
2533	if (!IS_QLA23XX(ha) && !IS_FWI2_CAPABLE(ha))
2534		return QLA_FUNCTION_FAILED;
2535
2536	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2537
2538	mcp->mb[0] = MBC_GEN_SYSTEM_ERROR;
2539	mcp->out_mb = MBX_0;
2540	mcp->in_mb = MBX_0;
2541	mcp->tov = 5;
2542	mcp->flags = 0;
2543	rval = qla2x00_mailbox_command(vha, mcp);
2544
2545	if (rval != QLA_SUCCESS) {
2546		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2547		    vha->host_no, rval));
2548	} else {
2549		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2550	}
2551
2552	return rval;
2553}
2554
2555/**
2556 * qla2x00_set_serdes_params() -
2557 * @ha: HA context
2558 *
2559 * Returns
2560 */
2561int
2562qla2x00_set_serdes_params(scsi_qla_host_t *vha, uint16_t sw_em_1g,
2563    uint16_t sw_em_2g, uint16_t sw_em_4g)
2564{
2565	int rval;
2566	mbx_cmd_t mc;
2567	mbx_cmd_t *mcp = &mc;
2568
2569	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2570
2571	mcp->mb[0] = MBC_SERDES_PARAMS;
2572	mcp->mb[1] = BIT_0;
2573	mcp->mb[2] = sw_em_1g | BIT_15;
2574	mcp->mb[3] = sw_em_2g | BIT_15;
2575	mcp->mb[4] = sw_em_4g | BIT_15;
2576	mcp->out_mb = MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2577	mcp->in_mb = MBX_0;
2578	mcp->tov = MBX_TOV_SECONDS;
2579	mcp->flags = 0;
2580	rval = qla2x00_mailbox_command(vha, mcp);
2581
2582	if (rval != QLA_SUCCESS) {
2583		/*EMPTY*/
2584		DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
2585		    vha->host_no, rval, mcp->mb[0]));
2586	} else {
2587		/*EMPTY*/
2588		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2589	}
2590
2591	return rval;
2592}
2593
2594int
2595qla2x00_stop_firmware(scsi_qla_host_t *vha)
2596{
2597	int rval;
2598	mbx_cmd_t mc;
2599	mbx_cmd_t *mcp = &mc;
2600
2601	if (!IS_FWI2_CAPABLE(vha->hw))
2602		return QLA_FUNCTION_FAILED;
2603
2604	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2605
2606	mcp->mb[0] = MBC_STOP_FIRMWARE;
2607	mcp->out_mb = MBX_0;
2608	mcp->in_mb = MBX_0;
2609	mcp->tov = 5;
2610	mcp->flags = 0;
2611	rval = qla2x00_mailbox_command(vha, mcp);
2612
2613	if (rval != QLA_SUCCESS) {
2614		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2615		    vha->host_no, rval));
2616		if (mcp->mb[0] == MBS_INVALID_COMMAND)
2617			rval = QLA_INVALID_COMMAND;
2618	} else {
2619		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2620	}
2621
2622	return rval;
2623}
2624
2625int
2626qla2x00_enable_eft_trace(scsi_qla_host_t *vha, dma_addr_t eft_dma,
2627    uint16_t buffers)
2628{
2629	int rval;
2630	mbx_cmd_t mc;
2631	mbx_cmd_t *mcp = &mc;
2632
2633	if (!IS_FWI2_CAPABLE(vha->hw))
2634		return QLA_FUNCTION_FAILED;
2635
2636	if (unlikely(pci_channel_offline(vha->hw->pdev)))
2637		return QLA_FUNCTION_FAILED;
2638
2639	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2640
2641	mcp->mb[0] = MBC_TRACE_CONTROL;
2642	mcp->mb[1] = TC_EFT_ENABLE;
2643	mcp->mb[2] = LSW(eft_dma);
2644	mcp->mb[3] = MSW(eft_dma);
2645	mcp->mb[4] = LSW(MSD(eft_dma));
2646	mcp->mb[5] = MSW(MSD(eft_dma));
2647	mcp->mb[6] = buffers;
2648	mcp->mb[7] = TC_AEN_DISABLE;
2649	mcp->out_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2650	mcp->in_mb = MBX_1|MBX_0;
2651	mcp->tov = MBX_TOV_SECONDS;
2652	mcp->flags = 0;
2653	rval = qla2x00_mailbox_command(vha, mcp);
2654	if (rval != QLA_SUCCESS) {
2655		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
2656		    __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
2657	} else {
2658		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2659	}
2660
2661	return rval;
2662}
2663
2664int
2665qla2x00_disable_eft_trace(scsi_qla_host_t *vha)
2666{
2667	int rval;
2668	mbx_cmd_t mc;
2669	mbx_cmd_t *mcp = &mc;
2670
2671	if (!IS_FWI2_CAPABLE(vha->hw))
2672		return QLA_FUNCTION_FAILED;
2673
2674	if (unlikely(pci_channel_offline(vha->hw->pdev)))
2675		return QLA_FUNCTION_FAILED;
2676
2677	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2678
2679	mcp->mb[0] = MBC_TRACE_CONTROL;
2680	mcp->mb[1] = TC_EFT_DISABLE;
2681	mcp->out_mb = MBX_1|MBX_0;
2682	mcp->in_mb = MBX_1|MBX_0;
2683	mcp->tov = MBX_TOV_SECONDS;
2684	mcp->flags = 0;
2685	rval = qla2x00_mailbox_command(vha, mcp);
2686	if (rval != QLA_SUCCESS) {
2687		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
2688		    __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
2689	} else {
2690		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2691	}
2692
2693	return rval;
2694}
2695
2696int
2697qla2x00_enable_fce_trace(scsi_qla_host_t *vha, dma_addr_t fce_dma,
2698    uint16_t buffers, uint16_t *mb, uint32_t *dwords)
2699{
2700	int rval;
2701	mbx_cmd_t mc;
2702	mbx_cmd_t *mcp = &mc;
2703
2704	if (!IS_QLA25XX(vha->hw) && !IS_QLA81XX(vha->hw))
2705		return QLA_FUNCTION_FAILED;
2706
2707	if (unlikely(pci_channel_offline(vha->hw->pdev)))
2708		return QLA_FUNCTION_FAILED;
2709
2710	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2711
2712	mcp->mb[0] = MBC_TRACE_CONTROL;
2713	mcp->mb[1] = TC_FCE_ENABLE;
2714	mcp->mb[2] = LSW(fce_dma);
2715	mcp->mb[3] = MSW(fce_dma);
2716	mcp->mb[4] = LSW(MSD(fce_dma));
2717	mcp->mb[5] = MSW(MSD(fce_dma));
2718	mcp->mb[6] = buffers;
2719	mcp->mb[7] = TC_AEN_DISABLE;
2720	mcp->mb[8] = 0;
2721	mcp->mb[9] = TC_FCE_DEFAULT_RX_SIZE;
2722	mcp->mb[10] = TC_FCE_DEFAULT_TX_SIZE;
2723	mcp->out_mb = MBX_10|MBX_9|MBX_8|MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|
2724	    MBX_1|MBX_0;
2725	mcp->in_mb = MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
2726	mcp->tov = MBX_TOV_SECONDS;
2727	mcp->flags = 0;
2728	rval = qla2x00_mailbox_command(vha, mcp);
2729	if (rval != QLA_SUCCESS) {
2730		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
2731		    __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
2732	} else {
2733		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2734
2735		if (mb)
2736			memcpy(mb, mcp->mb, 8 * sizeof(*mb));
2737		if (dwords)
2738			*dwords = buffers;
2739	}
2740
2741	return rval;
2742}
2743
2744int
2745qla2x00_disable_fce_trace(scsi_qla_host_t *vha, uint64_t *wr, uint64_t *rd)
2746{
2747	int rval;
2748	mbx_cmd_t mc;
2749	mbx_cmd_t *mcp = &mc;
2750
2751	if (!IS_FWI2_CAPABLE(vha->hw))
2752		return QLA_FUNCTION_FAILED;
2753
2754	if (unlikely(pci_channel_offline(vha->hw->pdev)))
2755		return QLA_FUNCTION_FAILED;
2756
2757	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2758
2759	mcp->mb[0] = MBC_TRACE_CONTROL;
2760	mcp->mb[1] = TC_FCE_DISABLE;
2761	mcp->mb[2] = TC_FCE_DISABLE_TRACE;
2762	mcp->out_mb = MBX_2|MBX_1|MBX_0;
2763	mcp->in_mb = MBX_9|MBX_8|MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|
2764	    MBX_1|MBX_0;
2765	mcp->tov = MBX_TOV_SECONDS;
2766	mcp->flags = 0;
2767	rval = qla2x00_mailbox_command(vha, mcp);
2768	if (rval != QLA_SUCCESS) {
2769		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
2770		    __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
2771	} else {
2772		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2773
2774		if (wr)
2775			*wr = (uint64_t) mcp->mb[5] << 48 |
2776			    (uint64_t) mcp->mb[4] << 32 |
2777			    (uint64_t) mcp->mb[3] << 16 |
2778			    (uint64_t) mcp->mb[2];
2779		if (rd)
2780			*rd = (uint64_t) mcp->mb[9] << 48 |
2781			    (uint64_t) mcp->mb[8] << 32 |
2782			    (uint64_t) mcp->mb[7] << 16 |
2783			    (uint64_t) mcp->mb[6];
2784	}
2785
2786	return rval;
2787}
2788
2789int
2790qla2x00_get_idma_speed(scsi_qla_host_t *vha, uint16_t loop_id,
2791	uint16_t *port_speed, uint16_t *mb)
2792{
2793	int rval;
2794	mbx_cmd_t mc;
2795	mbx_cmd_t *mcp = &mc;
2796
2797	if (!IS_IIDMA_CAPABLE(vha->hw))
2798		return QLA_FUNCTION_FAILED;
2799
2800	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2801
2802	mcp->mb[0] = MBC_PORT_PARAMS;
2803	mcp->mb[1] = loop_id;
2804	mcp->mb[2] = mcp->mb[3] = 0;
2805	mcp->mb[9] = vha->vp_idx;
2806	mcp->out_mb = MBX_9|MBX_3|MBX_2|MBX_1|MBX_0;
2807	mcp->in_mb = MBX_3|MBX_1|MBX_0;
2808	mcp->tov = MBX_TOV_SECONDS;
2809	mcp->flags = 0;
2810	rval = qla2x00_mailbox_command(vha, mcp);
2811
2812	/* Return mailbox statuses. */
2813	if (mb != NULL) {
2814		mb[0] = mcp->mb[0];
2815		mb[1] = mcp->mb[1];
2816		mb[3] = mcp->mb[3];
2817	}
2818
2819	if (rval != QLA_SUCCESS) {
2820		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2821		    vha->host_no, rval));
2822	} else {
2823		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2824		if (port_speed)
2825			*port_speed = mcp->mb[3];
2826	}
2827
2828	return rval;
2829}
2830
2831int
2832qla2x00_set_idma_speed(scsi_qla_host_t *vha, uint16_t loop_id,
2833    uint16_t port_speed, uint16_t *mb)
2834{
2835	int rval;
2836	mbx_cmd_t mc;
2837	mbx_cmd_t *mcp = &mc;
2838
2839	if (!IS_IIDMA_CAPABLE(vha->hw))
2840		return QLA_FUNCTION_FAILED;
2841
2842	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
2843
2844	mcp->mb[0] = MBC_PORT_PARAMS;
2845	mcp->mb[1] = loop_id;
2846	mcp->mb[2] = BIT_0;
2847	if (IS_QLA8XXX_TYPE(vha->hw))
2848		mcp->mb[3] = port_speed & (BIT_5|BIT_4|BIT_3|BIT_2|BIT_1|BIT_0);
2849	else
2850		mcp->mb[3] = port_speed & (BIT_2|BIT_1|BIT_0);
2851	mcp->mb[9] = vha->vp_idx;
2852	mcp->out_mb = MBX_9|MBX_3|MBX_2|MBX_1|MBX_0;
2853	mcp->in_mb = MBX_3|MBX_1|MBX_0;
2854	mcp->tov = MBX_TOV_SECONDS;
2855	mcp->flags = 0;
2856	rval = qla2x00_mailbox_command(vha, mcp);
2857
2858	/* Return mailbox statuses. */
2859	if (mb != NULL) {
2860		mb[0] = mcp->mb[0];
2861		mb[1] = mcp->mb[1];
2862		mb[3] = mcp->mb[3];
2863	}
2864
2865	if (rval != QLA_SUCCESS) {
2866		DEBUG2_3_11(printk("%s(%ld): failed=%x.\n", __func__,
2867		    vha->host_no, rval));
2868	} else {
2869		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
2870	}
2871
2872	return rval;
2873}
2874
2875void
2876qla24xx_report_id_acquisition(scsi_qla_host_t *vha,
2877	struct vp_rpt_id_entry_24xx *rptid_entry)
2878{
2879	uint8_t vp_idx;
2880	uint16_t stat = le16_to_cpu(rptid_entry->vp_idx);
2881	struct qla_hw_data *ha = vha->hw;
2882	scsi_qla_host_t *vp;
2883	unsigned long   flags;
2884
2885	if (rptid_entry->entry_status != 0)
2886		return;
2887
2888	if (rptid_entry->format == 0) {
2889		DEBUG15(printk("%s:format 0 : scsi(%ld) number of VPs setup %d,"
2890			" number of VPs acquired %d\n", __func__, vha->host_no,
2891			MSB(le16_to_cpu(rptid_entry->vp_count)),
2892			LSB(le16_to_cpu(rptid_entry->vp_count))));
2893		DEBUG15(printk("%s primary port id %02x%02x%02x\n", __func__,
2894			rptid_entry->port_id[2], rptid_entry->port_id[1],
2895			rptid_entry->port_id[0]));
2896	} else if (rptid_entry->format == 1) {
2897		vp_idx = LSB(stat);
2898		DEBUG15(printk("%s:format 1: scsi(%ld): VP[%d] enabled "
2899		    "- status %d - "
2900		    "with port id %02x%02x%02x\n", __func__, vha->host_no,
2901		    vp_idx, MSB(stat),
2902		    rptid_entry->port_id[2], rptid_entry->port_id[1],
2903		    rptid_entry->port_id[0]));
2904
2905		vp = vha;
2906		if (vp_idx == 0 && (MSB(stat) != 1))
2907			goto reg_needed;
2908
2909		if (MSB(stat) == 1) {
2910			DEBUG2(printk("scsi(%ld): Could not acquire ID for "
2911			    "VP[%d].\n", vha->host_no, vp_idx));
2912			return;
2913		}
2914
2915		spin_lock_irqsave(&ha->vport_slock, flags);
2916		list_for_each_entry(vp, &ha->vp_list, list)
2917			if (vp_idx == vp->vp_idx)
2918				break;
2919		spin_unlock_irqrestore(&ha->vport_slock, flags);
2920
2921		if (!vp)
2922			return;
2923
2924		vp->d_id.b.domain = rptid_entry->port_id[2];
2925		vp->d_id.b.area =  rptid_entry->port_id[1];
2926		vp->d_id.b.al_pa = rptid_entry->port_id[0];
2927
2928		/*
2929		 * Cannot configure here as we are still sitting on the
2930		 * response queue. Handle it in dpc context.
2931		 */
2932		set_bit(VP_IDX_ACQUIRED, &vp->vp_flags);
2933
2934reg_needed:
2935		set_bit(REGISTER_FC4_NEEDED, &vp->dpc_flags);
2936		set_bit(REGISTER_FDMI_NEEDED, &vp->dpc_flags);
2937		set_bit(VP_DPC_NEEDED, &vha->dpc_flags);
2938		qla2xxx_wake_dpc(vha);
2939	}
2940}
2941
2942/*
2943 * qla24xx_modify_vp_config
2944 *	Change VP configuration for vha
2945 *
2946 * Input:
2947 *	vha = adapter block pointer.
2948 *
2949 * Returns:
2950 *	qla2xxx local function return status code.
2951 *
2952 * Context:
2953 *	Kernel context.
2954 */
2955int
2956qla24xx_modify_vp_config(scsi_qla_host_t *vha)
2957{
2958	int		rval;
2959	struct vp_config_entry_24xx *vpmod;
2960	dma_addr_t	vpmod_dma;
2961	struct qla_hw_data *ha = vha->hw;
2962	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2963
2964	/* This can be called by the parent */
2965
2966	vpmod = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &vpmod_dma);
2967	if (!vpmod) {
2968		DEBUG2_3(printk("%s(%ld): failed to allocate Modify VP "
2969		    "IOCB.\n", __func__, vha->host_no));
2970		return QLA_MEMORY_ALLOC_FAILED;
2971	}
2972
2973	memset(vpmod, 0, sizeof(struct vp_config_entry_24xx));
2974	vpmod->entry_type = VP_CONFIG_IOCB_TYPE;
2975	vpmod->entry_count = 1;
2976	vpmod->command = VCT_COMMAND_MOD_ENABLE_VPS;
2977	vpmod->vp_count = 1;
2978	vpmod->vp_index1 = vha->vp_idx;
2979	vpmod->options_idx1 = BIT_3|BIT_4|BIT_5;
2980	memcpy(vpmod->node_name_idx1, vha->node_name, WWN_SIZE);
2981	memcpy(vpmod->port_name_idx1, vha->port_name, WWN_SIZE);
2982	vpmod->entry_count = 1;
2983
2984	rval = qla2x00_issue_iocb(base_vha, vpmod, vpmod_dma, 0);
2985	if (rval != QLA_SUCCESS) {
2986		DEBUG2_3_11(printk("%s(%ld): failed to issue VP config IOCB"
2987			"(%x).\n", __func__, base_vha->host_no, rval));
2988	} else if (vpmod->comp_status != 0) {
2989		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2990			"-- error status (%x).\n", __func__, base_vha->host_no,
2991			vpmod->comp_status));
2992		rval = QLA_FUNCTION_FAILED;
2993	} else if (vpmod->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
2994		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
2995		    "-- completion status (%x).\n", __func__, base_vha->host_no,
2996		    le16_to_cpu(vpmod->comp_status)));
2997		rval = QLA_FUNCTION_FAILED;
2998	} else {
2999		/* EMPTY */
3000		DEBUG11(printk("%s(%ld): done.\n", __func__,
3001							base_vha->host_no));
3002		fc_vport_set_state(vha->fc_vport, FC_VPORT_INITIALIZING);
3003	}
3004	dma_pool_free(ha->s_dma_pool, vpmod, vpmod_dma);
3005
3006	return rval;
3007}
3008
3009/*
3010 * qla24xx_control_vp
3011 *	Enable a virtual port for given host
3012 *
3013 * Input:
3014 *	ha = adapter block pointer.
3015 *	vhba = virtual adapter (unused)
3016 *	index = index number for enabled VP
3017 *
3018 * Returns:
3019 *	qla2xxx local function return status code.
3020 *
3021 * Context:
3022 *	Kernel context.
3023 */
3024int
3025qla24xx_control_vp(scsi_qla_host_t *vha, int cmd)
3026{
3027	int		rval;
3028	int		map, pos;
3029	struct vp_ctrl_entry_24xx   *vce;
3030	dma_addr_t	vce_dma;
3031	struct qla_hw_data *ha = vha->hw;
3032	int	vp_index = vha->vp_idx;
3033	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
3034
3035	DEBUG11(printk("%s(%ld): entered. Enabling index %d\n", __func__,
3036	    vha->host_no, vp_index));
3037
3038	if (vp_index == 0 || vp_index >= ha->max_npiv_vports)
3039		return QLA_PARAMETER_ERROR;
3040
3041	vce = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &vce_dma);
3042	if (!vce) {
3043		DEBUG2_3(printk("%s(%ld): "
3044		    "failed to allocate VP Control IOCB.\n", __func__,
3045		    base_vha->host_no));
3046		return QLA_MEMORY_ALLOC_FAILED;
3047	}
3048	memset(vce, 0, sizeof(struct vp_ctrl_entry_24xx));
3049
3050	vce->entry_type = VP_CTRL_IOCB_TYPE;
3051	vce->entry_count = 1;
3052	vce->command = cpu_to_le16(cmd);
3053	vce->vp_count = __constant_cpu_to_le16(1);
3054
3055	/* index map in firmware starts with 1; decrement index
3056	 * this is ok as we never use index 0
3057	 */
3058	map = (vp_index - 1) / 8;
3059	pos = (vp_index - 1) & 7;
3060	mutex_lock(&ha->vport_lock);
3061	vce->vp_idx_map[map] |= 1 << pos;
3062	mutex_unlock(&ha->vport_lock);
3063
3064	rval = qla2x00_issue_iocb(base_vha, vce, vce_dma, 0);
3065	if (rval != QLA_SUCCESS) {
3066		DEBUG2_3_11(printk("%s(%ld): failed to issue VP control IOCB"
3067		    "(%x).\n", __func__, base_vha->host_no, rval));
3068		printk("%s(%ld): failed to issue VP control IOCB"
3069		    "(%x).\n", __func__, base_vha->host_no, rval);
3070	} else if (vce->entry_status != 0) {
3071		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
3072		    "-- error status (%x).\n", __func__, base_vha->host_no,
3073		    vce->entry_status));
3074		printk("%s(%ld): failed to complete IOCB "
3075		    "-- error status (%x).\n", __func__, base_vha->host_no,
3076		    vce->entry_status);
3077		rval = QLA_FUNCTION_FAILED;
3078	} else if (vce->comp_status != __constant_cpu_to_le16(CS_COMPLETE)) {
3079		DEBUG2_3_11(printk("%s(%ld): failed to complete IOCB "
3080		    "-- completion status (%x).\n", __func__, base_vha->host_no,
3081		    le16_to_cpu(vce->comp_status)));
3082		printk("%s(%ld): failed to complete IOCB "
3083		    "-- completion status (%x).\n", __func__, base_vha->host_no,
3084		    le16_to_cpu(vce->comp_status));
3085		rval = QLA_FUNCTION_FAILED;
3086	} else {
3087		DEBUG2(printk("%s(%ld): done.\n", __func__, base_vha->host_no));
3088	}
3089
3090	dma_pool_free(ha->s_dma_pool, vce, vce_dma);
3091
3092	return rval;
3093}
3094
3095/*
3096 * qla2x00_send_change_request
3097 *	Receive or disable RSCN request from fabric controller
3098 *
3099 * Input:
3100 *	ha = adapter block pointer
3101 *	format = registration format:
3102 *		0 - Reserved
3103 *		1 - Fabric detected registration
3104 *		2 - N_port detected registration
3105 *		3 - Full registration
3106 *		FF - clear registration
3107 *	vp_idx = Virtual port index
3108 *
3109 * Returns:
3110 *	qla2x00 local function return status code.
3111 *
3112 * Context:
3113 *	Kernel Context
3114 */
3115
3116int
3117qla2x00_send_change_request(scsi_qla_host_t *vha, uint16_t format,
3118			    uint16_t vp_idx)
3119{
3120	int rval;
3121	mbx_cmd_t mc;
3122	mbx_cmd_t *mcp = &mc;
3123
3124	/*
3125	 * This command is implicitly executed by firmware during login for the
3126	 * physical hosts
3127	 */
3128	if (vp_idx == 0)
3129		return QLA_FUNCTION_FAILED;
3130
3131	mcp->mb[0] = MBC_SEND_CHANGE_REQUEST;
3132	mcp->mb[1] = format;
3133	mcp->mb[9] = vp_idx;
3134	mcp->out_mb = MBX_9|MBX_1|MBX_0;
3135	mcp->in_mb = MBX_0|MBX_1;
3136	mcp->tov = MBX_TOV_SECONDS;
3137	mcp->flags = 0;
3138	rval = qla2x00_mailbox_command(vha, mcp);
3139
3140	if (rval == QLA_SUCCESS) {
3141		if (mcp->mb[0] != MBS_COMMAND_COMPLETE) {
3142			rval = BIT_1;
3143		}
3144	} else
3145		rval = BIT_1;
3146
3147	return rval;
3148}
3149
3150int
3151qla2x00_dump_ram(scsi_qla_host_t *vha, dma_addr_t req_dma, uint32_t addr,
3152    uint32_t size)
3153{
3154	int rval;
3155	mbx_cmd_t mc;
3156	mbx_cmd_t *mcp = &mc;
3157
3158	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3159
3160	if (MSW(addr) || IS_FWI2_CAPABLE(vha->hw)) {
3161		mcp->mb[0] = MBC_DUMP_RISC_RAM_EXTENDED;
3162		mcp->mb[8] = MSW(addr);
3163		mcp->out_mb = MBX_8|MBX_0;
3164	} else {
3165		mcp->mb[0] = MBC_DUMP_RISC_RAM;
3166		mcp->out_mb = MBX_0;
3167	}
3168	mcp->mb[1] = LSW(addr);
3169	mcp->mb[2] = MSW(req_dma);
3170	mcp->mb[3] = LSW(req_dma);
3171	mcp->mb[6] = MSW(MSD(req_dma));
3172	mcp->mb[7] = LSW(MSD(req_dma));
3173	mcp->out_mb |= MBX_7|MBX_6|MBX_3|MBX_2|MBX_1;
3174	if (IS_FWI2_CAPABLE(vha->hw)) {
3175		mcp->mb[4] = MSW(size);
3176		mcp->mb[5] = LSW(size);
3177		mcp->out_mb |= MBX_5|MBX_4;
3178	} else {
3179		mcp->mb[4] = LSW(size);
3180		mcp->out_mb |= MBX_4;
3181	}
3182
3183	mcp->in_mb = MBX_0;
3184	mcp->tov = MBX_TOV_SECONDS;
3185	mcp->flags = 0;
3186	rval = qla2x00_mailbox_command(vha, mcp);
3187
3188	if (rval != QLA_SUCCESS) {
3189		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
3190		    vha->host_no, rval, mcp->mb[0]));
3191	} else {
3192		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3193	}
3194
3195	return rval;
3196}
3197
3198/* 84XX Support **************************************************************/
3199
3200struct cs84xx_mgmt_cmd {
3201	union {
3202		struct verify_chip_entry_84xx req;
3203		struct verify_chip_rsp_84xx rsp;
3204	} p;
3205};
3206
3207int
3208qla84xx_verify_chip(struct scsi_qla_host *vha, uint16_t *status)
3209{
3210	int rval, retry;
3211	struct cs84xx_mgmt_cmd *mn;
3212	dma_addr_t mn_dma;
3213	uint16_t options;
3214	unsigned long flags;
3215	struct qla_hw_data *ha = vha->hw;
3216
3217	DEBUG16(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3218
3219	mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
3220	if (mn == NULL) {
3221		DEBUG2_3(printk("%s(%ld): failed to allocate Verify ISP84XX "
3222		    "IOCB.\n", __func__, vha->host_no));
3223		return QLA_MEMORY_ALLOC_FAILED;
3224	}
3225
3226	/* Force Update? */
3227	options = ha->cs84xx->fw_update ? VCO_FORCE_UPDATE : 0;
3228	/* Diagnostic firmware? */
3229	/* options |= MENLO_DIAG_FW; */
3230	/* We update the firmware with only one data sequence. */
3231	options |= VCO_END_OF_DATA;
3232
3233	do {
3234		retry = 0;
3235		memset(mn, 0, sizeof(*mn));
3236		mn->p.req.entry_type = VERIFY_CHIP_IOCB_TYPE;
3237		mn->p.req.entry_count = 1;
3238		mn->p.req.options = cpu_to_le16(options);
3239
3240		DEBUG16(printk("%s(%ld): Dump of Verify Request.\n", __func__,
3241		    vha->host_no));
3242		DEBUG16(qla2x00_dump_buffer((uint8_t *)mn,
3243		    sizeof(*mn)));
3244
3245		rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
3246		if (rval != QLA_SUCCESS) {
3247			DEBUG2_16(printk("%s(%ld): failed to issue Verify "
3248			    "IOCB (%x).\n", __func__, vha->host_no, rval));
3249			goto verify_done;
3250		}
3251
3252		DEBUG16(printk("%s(%ld): Dump of Verify Response.\n", __func__,
3253		    vha->host_no));
3254		DEBUG16(qla2x00_dump_buffer((uint8_t *)mn,
3255		    sizeof(*mn)));
3256
3257		status[0] = le16_to_cpu(mn->p.rsp.comp_status);
3258		status[1] = status[0] == CS_VCS_CHIP_FAILURE ?
3259		    le16_to_cpu(mn->p.rsp.failure_code) : 0;
3260		DEBUG2_16(printk("%s(%ld): cs=%x fc=%x\n", __func__,
3261		    vha->host_no, status[0], status[1]));
3262
3263		if (status[0] != CS_COMPLETE) {
3264			rval = QLA_FUNCTION_FAILED;
3265			if (!(options & VCO_DONT_UPDATE_FW)) {
3266				DEBUG2_16(printk("%s(%ld): Firmware update "
3267				    "failed. Retrying without update "
3268				    "firmware.\n", __func__, vha->host_no));
3269				options |= VCO_DONT_UPDATE_FW;
3270				options &= ~VCO_FORCE_UPDATE;
3271				retry = 1;
3272			}
3273		} else {
3274			DEBUG2_16(printk("%s(%ld): firmware updated to %x.\n",
3275			    __func__, vha->host_no,
3276			    le32_to_cpu(mn->p.rsp.fw_ver)));
3277
3278			/* NOTE: we only update OP firmware. */
3279			spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
3280			ha->cs84xx->op_fw_version =
3281			    le32_to_cpu(mn->p.rsp.fw_ver);
3282			spin_unlock_irqrestore(&ha->cs84xx->access_lock,
3283			    flags);
3284		}
3285	} while (retry);
3286
3287verify_done:
3288	dma_pool_free(ha->s_dma_pool, mn, mn_dma);
3289
3290	if (rval != QLA_SUCCESS) {
3291		DEBUG2_16(printk("%s(%ld): failed=%x.\n", __func__,
3292		    vha->host_no, rval));
3293	} else {
3294		DEBUG16(printk("%s(%ld): done.\n", __func__, vha->host_no));
3295	}
3296
3297	return rval;
3298}
3299
3300int
3301qla25xx_init_req_que(struct scsi_qla_host *vha, struct req_que *req)
3302{
3303	int rval;
3304	unsigned long flags;
3305	mbx_cmd_t mc;
3306	mbx_cmd_t *mcp = &mc;
3307	struct device_reg_25xxmq __iomem *reg;
3308	struct qla_hw_data *ha = vha->hw;
3309
3310	mcp->mb[0] = MBC_INITIALIZE_MULTIQ;
3311	mcp->mb[1] = req->options;
3312	mcp->mb[2] = MSW(LSD(req->dma));
3313	mcp->mb[3] = LSW(LSD(req->dma));
3314	mcp->mb[6] = MSW(MSD(req->dma));
3315	mcp->mb[7] = LSW(MSD(req->dma));
3316	mcp->mb[5] = req->length;
3317	if (req->rsp)
3318		mcp->mb[10] = req->rsp->id;
3319	mcp->mb[12] = req->qos;
3320	mcp->mb[11] = req->vp_idx;
3321	mcp->mb[13] = req->rid;
3322
3323	reg = (struct device_reg_25xxmq *)((void *)(ha->mqiobase) +
3324		QLA_QUE_PAGE * req->id);
3325
3326	mcp->mb[4] = req->id;
3327	/* que in ptr index */
3328	mcp->mb[8] = 0;
3329	/* que out ptr index */
3330	mcp->mb[9] = 0;
3331	mcp->out_mb = MBX_14|MBX_13|MBX_12|MBX_11|MBX_10|MBX_9|MBX_8|MBX_7|
3332			MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
3333	mcp->in_mb = MBX_0;
3334	mcp->flags = MBX_DMA_OUT;
3335	mcp->tov = 60;
3336
3337	spin_lock_irqsave(&ha->hardware_lock, flags);
3338	if (!(req->options & BIT_0)) {
3339		WRT_REG_DWORD(&reg->req_q_in, 0);
3340		WRT_REG_DWORD(&reg->req_q_out, 0);
3341	}
3342	req->req_q_in = &reg->req_q_in;
3343	req->req_q_out = &reg->req_q_out;
3344	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3345
3346	rval = qla2x00_mailbox_command(vha, mcp);
3347	if (rval != QLA_SUCCESS)
3348		DEBUG2_3_11(printk(KERN_WARNING "%s(%ld): failed=%x mb0=%x.\n",
3349			__func__, vha->host_no, rval, mcp->mb[0]));
3350	return rval;
3351}
3352
3353int
3354qla25xx_init_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
3355{
3356	int rval;
3357	unsigned long flags;
3358	mbx_cmd_t mc;
3359	mbx_cmd_t *mcp = &mc;
3360	struct device_reg_25xxmq __iomem *reg;
3361	struct qla_hw_data *ha = vha->hw;
3362
3363	mcp->mb[0] = MBC_INITIALIZE_MULTIQ;
3364	mcp->mb[1] = rsp->options;
3365	mcp->mb[2] = MSW(LSD(rsp->dma));
3366	mcp->mb[3] = LSW(LSD(rsp->dma));
3367	mcp->mb[6] = MSW(MSD(rsp->dma));
3368	mcp->mb[7] = LSW(MSD(rsp->dma));
3369	mcp->mb[5] = rsp->length;
3370	mcp->mb[14] = rsp->msix->entry;
3371	mcp->mb[13] = rsp->rid;
3372
3373	reg = (struct device_reg_25xxmq *)((void *)(ha->mqiobase) +
3374		QLA_QUE_PAGE * rsp->id);
3375
3376	mcp->mb[4] = rsp->id;
3377	/* que in ptr index */
3378	mcp->mb[8] = 0;
3379	/* que out ptr index */
3380	mcp->mb[9] = 0;
3381	mcp->out_mb = MBX_14|MBX_13|MBX_9|MBX_8|MBX_7
3382			|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
3383	mcp->in_mb = MBX_0;
3384	mcp->flags = MBX_DMA_OUT;
3385	mcp->tov = 60;
3386
3387	spin_lock_irqsave(&ha->hardware_lock, flags);
3388	if (!(rsp->options & BIT_0)) {
3389		WRT_REG_DWORD(&reg->rsp_q_out, 0);
3390		WRT_REG_DWORD(&reg->rsp_q_in, 0);
3391	}
3392
3393	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3394
3395	rval = qla2x00_mailbox_command(vha, mcp);
3396	if (rval != QLA_SUCCESS)
3397		DEBUG2_3_11(printk(KERN_WARNING "%s(%ld): failed=%x "
3398			"mb0=%x.\n", __func__,
3399			vha->host_no, rval, mcp->mb[0]));
3400	return rval;
3401}
3402
3403int
3404qla81xx_idc_ack(scsi_qla_host_t *vha, uint16_t *mb)
3405{
3406	int rval;
3407	mbx_cmd_t mc;
3408	mbx_cmd_t *mcp = &mc;
3409
3410	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3411
3412	mcp->mb[0] = MBC_IDC_ACK;
3413	memcpy(&mcp->mb[1], mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
3414	mcp->out_mb = MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
3415	mcp->in_mb = MBX_0;
3416	mcp->tov = MBX_TOV_SECONDS;
3417	mcp->flags = 0;
3418	rval = qla2x00_mailbox_command(vha, mcp);
3419
3420	if (rval != QLA_SUCCESS) {
3421		DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
3422		    vha->host_no, rval, mcp->mb[0]));
3423	} else {
3424		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3425	}
3426
3427	return rval;
3428}
3429
3430int
3431qla81xx_fac_get_sector_size(scsi_qla_host_t *vha, uint32_t *sector_size)
3432{
3433	int rval;
3434	mbx_cmd_t mc;
3435	mbx_cmd_t *mcp = &mc;
3436
3437	if (!IS_QLA81XX(vha->hw))
3438		return QLA_FUNCTION_FAILED;
3439
3440	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3441
3442	mcp->mb[0] = MBC_FLASH_ACCESS_CTRL;
3443	mcp->mb[1] = FAC_OPT_CMD_GET_SECTOR_SIZE;
3444	mcp->out_mb = MBX_1|MBX_0;
3445	mcp->in_mb = MBX_1|MBX_0;
3446	mcp->tov = MBX_TOV_SECONDS;
3447	mcp->flags = 0;
3448	rval = qla2x00_mailbox_command(vha, mcp);
3449
3450	if (rval != QLA_SUCCESS) {
3451		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
3452		    __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
3453	} else {
3454		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3455		*sector_size = mcp->mb[1];
3456	}
3457
3458	return rval;
3459}
3460
3461int
3462qla81xx_fac_do_write_enable(scsi_qla_host_t *vha, int enable)
3463{
3464	int rval;
3465	mbx_cmd_t mc;
3466	mbx_cmd_t *mcp = &mc;
3467
3468	if (!IS_QLA81XX(vha->hw))
3469		return QLA_FUNCTION_FAILED;
3470
3471	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3472
3473	mcp->mb[0] = MBC_FLASH_ACCESS_CTRL;
3474	mcp->mb[1] = enable ? FAC_OPT_CMD_WRITE_ENABLE :
3475	    FAC_OPT_CMD_WRITE_PROTECT;
3476	mcp->out_mb = MBX_1|MBX_0;
3477	mcp->in_mb = MBX_1|MBX_0;
3478	mcp->tov = MBX_TOV_SECONDS;
3479	mcp->flags = 0;
3480	rval = qla2x00_mailbox_command(vha, mcp);
3481
3482	if (rval != QLA_SUCCESS) {
3483		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x.\n",
3484		    __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
3485	} else {
3486		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3487	}
3488
3489	return rval;
3490}
3491
3492int
3493qla81xx_fac_erase_sector(scsi_qla_host_t *vha, uint32_t start, uint32_t finish)
3494{
3495	int rval;
3496	mbx_cmd_t mc;
3497	mbx_cmd_t *mcp = &mc;
3498
3499	if (!IS_QLA81XX(vha->hw))
3500		return QLA_FUNCTION_FAILED;
3501
3502	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3503
3504	mcp->mb[0] = MBC_FLASH_ACCESS_CTRL;
3505	mcp->mb[1] = FAC_OPT_CMD_ERASE_SECTOR;
3506	mcp->mb[2] = LSW(start);
3507	mcp->mb[3] = MSW(start);
3508	mcp->mb[4] = LSW(finish);
3509	mcp->mb[5] = MSW(finish);
3510	mcp->out_mb = MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
3511	mcp->in_mb = MBX_2|MBX_1|MBX_0;
3512	mcp->tov = MBX_TOV_SECONDS;
3513	mcp->flags = 0;
3514	rval = qla2x00_mailbox_command(vha, mcp);
3515
3516	if (rval != QLA_SUCCESS) {
3517		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x mb[1]=%x "
3518		    "mb[2]=%x.\n", __func__, vha->host_no, rval, mcp->mb[0],
3519		    mcp->mb[1], mcp->mb[2]));
3520	} else {
3521		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3522	}
3523
3524	return rval;
3525}
3526
3527int
3528qla81xx_restart_mpi_firmware(scsi_qla_host_t *vha)
3529{
3530	int rval = 0;
3531	mbx_cmd_t mc;
3532	mbx_cmd_t *mcp = &mc;
3533
3534	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3535
3536	mcp->mb[0] = MBC_RESTART_MPI_FW;
3537	mcp->out_mb = MBX_0;
3538	mcp->in_mb = MBX_0|MBX_1;
3539	mcp->tov = MBX_TOV_SECONDS;
3540	mcp->flags = 0;
3541	rval = qla2x00_mailbox_command(vha, mcp);
3542
3543	if (rval != QLA_SUCCESS) {
3544		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=0x%x mb[1]=0x%x.\n",
3545		    __func__, vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
3546	} else {
3547		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3548	}
3549
3550	return rval;
3551}
3552
3553int
3554qla2x00_read_sfp(scsi_qla_host_t *vha, dma_addr_t sfp_dma, uint8_t *sfp,
3555	uint16_t dev, uint16_t off, uint16_t len, uint16_t opt)
3556{
3557	int rval;
3558	mbx_cmd_t mc;
3559	mbx_cmd_t *mcp = &mc;
3560	struct qla_hw_data *ha = vha->hw;
3561
3562	if (!IS_FWI2_CAPABLE(ha))
3563		return QLA_FUNCTION_FAILED;
3564
3565	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3566
3567	if (len == 1)
3568		opt |= BIT_0;
3569
3570	mcp->mb[0] = MBC_READ_SFP;
3571	mcp->mb[1] = dev;
3572	mcp->mb[2] = MSW(sfp_dma);
3573	mcp->mb[3] = LSW(sfp_dma);
3574	mcp->mb[6] = MSW(MSD(sfp_dma));
3575	mcp->mb[7] = LSW(MSD(sfp_dma));
3576	mcp->mb[8] = len;
3577	mcp->mb[9] = off;
3578	mcp->mb[10] = opt;
3579	mcp->out_mb = MBX_10|MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
3580	mcp->in_mb = MBX_1|MBX_0;
3581	mcp->tov = MBX_TOV_SECONDS;
3582	mcp->flags = 0;
3583	rval = qla2x00_mailbox_command(vha, mcp);
3584
3585	if (opt & BIT_0)
3586		*sfp = mcp->mb[1];
3587
3588	if (rval != QLA_SUCCESS) {
3589		DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
3590		    vha->host_no, rval, mcp->mb[0]));
3591	} else {
3592		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3593	}
3594
3595	return rval;
3596}
3597
3598int
3599qla2x00_write_sfp(scsi_qla_host_t *vha, dma_addr_t sfp_dma, uint8_t *sfp,
3600	uint16_t dev, uint16_t off, uint16_t len, uint16_t opt)
3601{
3602	int rval;
3603	mbx_cmd_t mc;
3604	mbx_cmd_t *mcp = &mc;
3605	struct qla_hw_data *ha = vha->hw;
3606
3607	if (!IS_FWI2_CAPABLE(ha))
3608		return QLA_FUNCTION_FAILED;
3609
3610	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3611
3612	if (len == 1)
3613		opt |= BIT_0;
3614
3615	if (opt & BIT_0)
3616		len = *sfp;
3617
3618	mcp->mb[0] = MBC_WRITE_SFP;
3619	mcp->mb[1] = dev;
3620	mcp->mb[2] = MSW(sfp_dma);
3621	mcp->mb[3] = LSW(sfp_dma);
3622	mcp->mb[6] = MSW(MSD(sfp_dma));
3623	mcp->mb[7] = LSW(MSD(sfp_dma));
3624	mcp->mb[8] = len;
3625	mcp->mb[9] = off;
3626	mcp->mb[10] = opt;
3627	mcp->out_mb = MBX_10|MBX_9|MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
3628	mcp->in_mb = MBX_1|MBX_0;
3629	mcp->tov = MBX_TOV_SECONDS;
3630	mcp->flags = 0;
3631	rval = qla2x00_mailbox_command(vha, mcp);
3632
3633	if (rval != QLA_SUCCESS) {
3634		DEBUG2_3_11(printk("%s(%ld): failed=%x (%x).\n", __func__,
3635		    vha->host_no, rval, mcp->mb[0]));
3636	} else {
3637		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3638	}
3639
3640	return rval;
3641}
3642
3643int
3644qla2x00_get_xgmac_stats(scsi_qla_host_t *vha, dma_addr_t stats_dma,
3645    uint16_t size_in_bytes, uint16_t *actual_size)
3646{
3647	int rval;
3648	mbx_cmd_t mc;
3649	mbx_cmd_t *mcp = &mc;
3650
3651	if (!IS_QLA8XXX_TYPE(vha->hw))
3652		return QLA_FUNCTION_FAILED;
3653
3654	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3655
3656	mcp->mb[0] = MBC_GET_XGMAC_STATS;
3657	mcp->mb[2] = MSW(stats_dma);
3658	mcp->mb[3] = LSW(stats_dma);
3659	mcp->mb[6] = MSW(MSD(stats_dma));
3660	mcp->mb[7] = LSW(MSD(stats_dma));
3661	mcp->mb[8] = size_in_bytes >> 2;
3662	mcp->out_mb = MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_0;
3663	mcp->in_mb = MBX_2|MBX_1|MBX_0;
3664	mcp->tov = MBX_TOV_SECONDS;
3665	mcp->flags = 0;
3666	rval = qla2x00_mailbox_command(vha, mcp);
3667
3668	if (rval != QLA_SUCCESS) {
3669		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=0x%x "
3670		    "mb[1]=0x%x mb[2]=0x%x.\n", __func__, vha->host_no, rval,
3671		    mcp->mb[0], mcp->mb[1], mcp->mb[2]));
3672	} else {
3673		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3674
3675		*actual_size = mcp->mb[2] << 2;
3676	}
3677
3678	return rval;
3679}
3680
3681int
3682qla2x00_get_dcbx_params(scsi_qla_host_t *vha, dma_addr_t tlv_dma,
3683    uint16_t size)
3684{
3685	int rval;
3686	mbx_cmd_t mc;
3687	mbx_cmd_t *mcp = &mc;
3688
3689	if (!IS_QLA8XXX_TYPE(vha->hw))
3690		return QLA_FUNCTION_FAILED;
3691
3692	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3693
3694	mcp->mb[0] = MBC_GET_DCBX_PARAMS;
3695	mcp->mb[1] = 0;
3696	mcp->mb[2] = MSW(tlv_dma);
3697	mcp->mb[3] = LSW(tlv_dma);
3698	mcp->mb[6] = MSW(MSD(tlv_dma));
3699	mcp->mb[7] = LSW(MSD(tlv_dma));
3700	mcp->mb[8] = size;
3701	mcp->out_mb = MBX_8|MBX_7|MBX_6|MBX_3|MBX_2|MBX_1|MBX_0;
3702	mcp->in_mb = MBX_2|MBX_1|MBX_0;
3703	mcp->tov = MBX_TOV_SECONDS;
3704	mcp->flags = 0;
3705	rval = qla2x00_mailbox_command(vha, mcp);
3706
3707	if (rval != QLA_SUCCESS) {
3708		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=0x%x "
3709		    "mb[1]=0x%x mb[2]=0x%x.\n", __func__, vha->host_no, rval,
3710		    mcp->mb[0], mcp->mb[1], mcp->mb[2]));
3711	} else {
3712		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3713	}
3714
3715	return rval;
3716}
3717
3718int
3719qla2x00_read_ram_word(scsi_qla_host_t *vha, uint32_t risc_addr, uint32_t *data)
3720{
3721	int rval;
3722	mbx_cmd_t mc;
3723	mbx_cmd_t *mcp = &mc;
3724
3725	if (!IS_FWI2_CAPABLE(vha->hw))
3726		return QLA_FUNCTION_FAILED;
3727
3728	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3729
3730	mcp->mb[0] = MBC_READ_RAM_EXTENDED;
3731	mcp->mb[1] = LSW(risc_addr);
3732	mcp->mb[8] = MSW(risc_addr);
3733	mcp->out_mb = MBX_8|MBX_1|MBX_0;
3734	mcp->in_mb = MBX_3|MBX_2|MBX_0;
3735	mcp->tov = 30;
3736	mcp->flags = 0;
3737	rval = qla2x00_mailbox_command(vha, mcp);
3738	if (rval != QLA_SUCCESS) {
3739		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
3740		    vha->host_no, rval, mcp->mb[0]));
3741	} else {
3742		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3743		*data = mcp->mb[3] << 16 | mcp->mb[2];
3744	}
3745
3746	return rval;
3747}
3748
3749int
3750qla2x00_loopback_test(scsi_qla_host_t *vha, struct msg_echo_lb *mreq,
3751	uint16_t *mresp)
3752{
3753	int rval;
3754	mbx_cmd_t mc;
3755	mbx_cmd_t *mcp = &mc;
3756	uint32_t iter_cnt = 0x1;
3757
3758	DEBUG11(printk("scsi(%ld): entered.\n", vha->host_no));
3759
3760	memset(mcp->mb, 0 , sizeof(mcp->mb));
3761	mcp->mb[0] = MBC_DIAGNOSTIC_LOOP_BACK;
3762	mcp->mb[1] = mreq->options | BIT_6;	// BIT_6 specifies 64 bit addressing
3763
3764	/* transfer count */
3765	mcp->mb[10] = LSW(mreq->transfer_size);
3766	mcp->mb[11] = MSW(mreq->transfer_size);
3767
3768	/* send data address */
3769	mcp->mb[14] = LSW(mreq->send_dma);
3770	mcp->mb[15] = MSW(mreq->send_dma);
3771	mcp->mb[20] = LSW(MSD(mreq->send_dma));
3772	mcp->mb[21] = MSW(MSD(mreq->send_dma));
3773
3774	/* receive data address */
3775	mcp->mb[16] = LSW(mreq->rcv_dma);
3776	mcp->mb[17] = MSW(mreq->rcv_dma);
3777	mcp->mb[6] = LSW(MSD(mreq->rcv_dma));
3778	mcp->mb[7] = MSW(MSD(mreq->rcv_dma));
3779
3780	/* Iteration count */
3781	mcp->mb[18] = LSW(iter_cnt);
3782	mcp->mb[19] = MSW(iter_cnt);
3783
3784	mcp->out_mb = MBX_21|MBX_20|MBX_19|MBX_18|MBX_17|MBX_16|MBX_15|
3785	    MBX_14|MBX_13|MBX_12|MBX_11|MBX_10|MBX_7|MBX_6|MBX_1|MBX_0;
3786	if (IS_QLA8XXX_TYPE(vha->hw))
3787		mcp->out_mb |= MBX_2;
3788	mcp->in_mb = MBX_19|MBX_18|MBX_3|MBX_2|MBX_1|MBX_0;
3789
3790	mcp->buf_size = mreq->transfer_size;
3791	mcp->tov = MBX_TOV_SECONDS;
3792	mcp->flags = MBX_DMA_OUT|MBX_DMA_IN|IOCTL_CMD;
3793
3794	rval = qla2x00_mailbox_command(vha, mcp);
3795
3796	if (rval != QLA_SUCCESS) {
3797		DEBUG2(printk(KERN_WARNING
3798			"(%ld): failed=%x mb[0]=0x%x "
3799			"mb[1]=0x%x mb[2]=0x%x mb[3]=0x%x mb[18]=0x%x "
3800			"mb[19]=0x%x.\n",
3801			vha->host_no, rval, mcp->mb[0], mcp->mb[1], mcp->mb[2],
3802			mcp->mb[3], mcp->mb[18], mcp->mb[19]));
3803	} else {
3804		DEBUG2(printk(KERN_WARNING
3805		    "scsi(%ld): done.\n", vha->host_no));
3806	}
3807
3808	/* Copy mailbox information */
3809	memcpy( mresp, mcp->mb, 64);
3810	return rval;
3811}
3812
3813int
3814qla2x00_echo_test(scsi_qla_host_t *vha, struct msg_echo_lb *mreq,
3815	uint16_t *mresp)
3816{
3817	int rval;
3818	mbx_cmd_t mc;
3819	mbx_cmd_t *mcp = &mc;
3820	struct qla_hw_data *ha = vha->hw;
3821
3822	DEBUG11(printk("scsi(%ld): entered.\n", vha->host_no));
3823
3824	memset(mcp->mb, 0 , sizeof(mcp->mb));
3825	mcp->mb[0] = MBC_DIAGNOSTIC_ECHO;
3826	mcp->mb[1] = mreq->options | BIT_6;	/* BIT_6 specifies 64bit address */
3827	if (IS_QLA8XXX_TYPE(ha)) {
3828		mcp->mb[1] |= BIT_15;
3829		mcp->mb[2] = vha->fcoe_fcf_idx;
3830	}
3831	mcp->mb[16] = LSW(mreq->rcv_dma);
3832	mcp->mb[17] = MSW(mreq->rcv_dma);
3833	mcp->mb[6] = LSW(MSD(mreq->rcv_dma));
3834	mcp->mb[7] = MSW(MSD(mreq->rcv_dma));
3835
3836	mcp->mb[10] = LSW(mreq->transfer_size);
3837
3838	mcp->mb[14] = LSW(mreq->send_dma);
3839	mcp->mb[15] = MSW(mreq->send_dma);
3840	mcp->mb[20] = LSW(MSD(mreq->send_dma));
3841	mcp->mb[21] = MSW(MSD(mreq->send_dma));
3842
3843	mcp->out_mb = MBX_21|MBX_20|MBX_17|MBX_16|MBX_15|
3844	    MBX_14|MBX_10|MBX_7|MBX_6|MBX_1|MBX_0;
3845	if (IS_QLA8XXX_TYPE(ha))
3846		mcp->out_mb |= MBX_2;
3847
3848	mcp->in_mb = MBX_0;
3849	if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_QLA8XXX_TYPE(ha))
3850		mcp->in_mb |= MBX_1;
3851	if (IS_QLA8XXX_TYPE(ha))
3852		mcp->in_mb |= MBX_3;
3853
3854	mcp->tov = MBX_TOV_SECONDS;
3855	mcp->flags = MBX_DMA_OUT|MBX_DMA_IN|IOCTL_CMD;
3856	mcp->buf_size = mreq->transfer_size;
3857
3858	rval = qla2x00_mailbox_command(vha, mcp);
3859
3860	if (rval != QLA_SUCCESS) {
3861		DEBUG2(printk(KERN_WARNING
3862		    "(%ld): failed=%x mb[0]=0x%x mb[1]=0x%x.\n",
3863		    vha->host_no, rval, mcp->mb[0], mcp->mb[1]));
3864	} else {
3865		DEBUG2(printk(KERN_WARNING
3866		    "scsi(%ld): done.\n", vha->host_no));
3867	}
3868
3869	/* Copy mailbox information */
3870	memcpy(mresp, mcp->mb, 64);
3871	return rval;
3872}
3873
3874int
3875qla84xx_reset_chip(scsi_qla_host_t *ha, uint16_t enable_diagnostic)
3876{
3877	int rval;
3878	mbx_cmd_t mc;
3879	mbx_cmd_t *mcp = &mc;
3880
3881	DEBUG16(printk("%s(%ld): enable_diag=%d entered.\n", __func__,
3882		ha->host_no, enable_diagnostic));
3883
3884	mcp->mb[0] = MBC_ISP84XX_RESET;
3885	mcp->mb[1] = enable_diagnostic;
3886	mcp->out_mb = MBX_1|MBX_0;
3887	mcp->in_mb = MBX_1|MBX_0;
3888	mcp->tov = MBX_TOV_SECONDS;
3889	mcp->flags = MBX_DMA_OUT|MBX_DMA_IN|IOCTL_CMD;
3890	rval = qla2x00_mailbox_command(ha, mcp);
3891
3892	if (rval != QLA_SUCCESS)
3893		DEBUG16(printk("%s(%ld): failed=%x.\n", __func__, ha->host_no,
3894			rval));
3895	else
3896		DEBUG16(printk("%s(%ld): done.\n", __func__, ha->host_no));
3897
3898	return rval;
3899}
3900
3901int
3902qla2x00_write_ram_word(scsi_qla_host_t *vha, uint32_t risc_addr, uint32_t data)
3903{
3904	int rval;
3905	mbx_cmd_t mc;
3906	mbx_cmd_t *mcp = &mc;
3907
3908	if (!IS_FWI2_CAPABLE(vha->hw))
3909		return QLA_FUNCTION_FAILED;
3910
3911	DEBUG11(printk("%s(%ld): entered.\n", __func__, vha->host_no));
3912
3913	mcp->mb[0] = MBC_WRITE_RAM_WORD_EXTENDED;
3914	mcp->mb[1] = LSW(risc_addr);
3915	mcp->mb[2] = LSW(data);
3916	mcp->mb[3] = MSW(data);
3917	mcp->mb[8] = MSW(risc_addr);
3918	mcp->out_mb = MBX_8|MBX_3|MBX_2|MBX_1|MBX_0;
3919	mcp->in_mb = MBX_0;
3920	mcp->tov = 30;
3921	mcp->flags = 0;
3922	rval = qla2x00_mailbox_command(vha, mcp);
3923	if (rval != QLA_SUCCESS) {
3924		DEBUG2_3_11(printk("%s(%ld): failed=%x mb[0]=%x.\n", __func__,
3925		    vha->host_no, rval, mcp->mb[0]));
3926	} else {
3927		DEBUG11(printk("%s(%ld): done.\n", __func__, vha->host_no));
3928	}
3929
3930	return rval;
3931}
3932
3933int
3934qla81xx_write_mpi_register(scsi_qla_host_t *vha, uint16_t *mb)
3935{
3936	int rval;
3937	uint32_t stat, timer;
3938	uint16_t mb0 = 0;
3939	struct qla_hw_data *ha = vha->hw;
3940	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3941
3942	rval = QLA_SUCCESS;
3943
3944	DEBUG11(qla_printk(KERN_INFO, ha,
3945	    "%s(%ld): entered.\n", __func__, vha->host_no));
3946
3947	clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
3948
3949	/* Write the MBC data to the registers */
3950	WRT_REG_WORD(&reg->mailbox0, MBC_WRITE_MPI_REGISTER);
3951	WRT_REG_WORD(&reg->mailbox1, mb[0]);
3952	WRT_REG_WORD(&reg->mailbox2, mb[1]);
3953	WRT_REG_WORD(&reg->mailbox3, mb[2]);
3954	WRT_REG_WORD(&reg->mailbox4, mb[3]);
3955
3956	WRT_REG_DWORD(&reg->hccr, HCCRX_SET_HOST_INT);
3957
3958	/* Poll for MBC interrupt */
3959	for (timer = 6000000; timer; timer--) {
3960		/* Check for pending interrupts. */
3961		stat = RD_REG_DWORD(&reg->host_status);
3962		if (stat & HSRX_RISC_INT) {
3963			stat &= 0xff;
3964
3965			if (stat == 0x1 || stat == 0x2 ||
3966			    stat == 0x10 || stat == 0x11) {
3967				set_bit(MBX_INTERRUPT,
3968				    &ha->mbx_cmd_flags);
3969				mb0 = RD_REG_WORD(&reg->mailbox0);
3970				WRT_REG_DWORD(&reg->hccr,
3971				    HCCRX_CLR_RISC_INT);
3972				RD_REG_DWORD(&reg->hccr);
3973				break;
3974			}
3975		}
3976		udelay(5);
3977	}
3978
3979	if (test_and_clear_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags))
3980		rval = mb0 & MBS_MASK;
3981	else
3982		rval = QLA_FUNCTION_FAILED;
3983
3984	if (rval != QLA_SUCCESS) {
3985		DEBUG2_3_11(printk(KERN_INFO "%s(%ld): failed=%x mb[0]=%x.\n",
3986		    __func__, vha->host_no, rval, mb[0]));
3987	} else {
3988		DEBUG11(printk(KERN_INFO
3989		    "%s(%ld): done.\n", __func__, vha->host_no));
3990	}
3991
3992	return rval;
3993}
3994int
3995qla2x00_get_data_rate(scsi_qla_host_t *vha)
3996{
3997	int rval;
3998	mbx_cmd_t mc;
3999	mbx_cmd_t *mcp = &mc;
4000	struct qla_hw_data *ha = vha->hw;
4001
4002	if (!IS_FWI2_CAPABLE(ha))
4003		return QLA_FUNCTION_FAILED;
4004
4005	DEBUG11(qla_printk(KERN_INFO, ha,
4006		"%s(%ld): entered.\n", __func__, vha->host_no));
4007
4008	mcp->mb[0] = MBC_DATA_RATE;
4009	mcp->mb[1] = 0;
4010	mcp->out_mb = MBX_1|MBX_0;
4011	mcp->in_mb = MBX_2|MBX_1|MBX_0;
4012	mcp->tov = MBX_TOV_SECONDS;
4013	mcp->flags = 0;
4014	rval = qla2x00_mailbox_command(vha, mcp);
4015	if (rval != QLA_SUCCESS) {
4016		DEBUG2_3_11(printk(KERN_INFO "%s(%ld): failed=%x mb[0]=%x.\n",
4017		    __func__, vha->host_no, rval, mcp->mb[0]));
4018	} else {
4019		DEBUG11(printk(KERN_INFO
4020		    "%s(%ld): done.\n", __func__, vha->host_no));
4021		if (mcp->mb[1] != 0x7)
4022			ha->link_data_rate = mcp->mb[1];
4023	}
4024
4025	return rval;
4026}
4027
4028int
4029qla81xx_get_port_config(scsi_qla_host_t *vha, uint16_t *mb)
4030{
4031	int rval;
4032	mbx_cmd_t mc;
4033	mbx_cmd_t *mcp = &mc;
4034	struct qla_hw_data *ha = vha->hw;
4035
4036	DEBUG11(printk(KERN_INFO
4037	    "%s(%ld): entered.\n", __func__, vha->host_no));
4038
4039	if (!IS_QLA81XX(ha))
4040		return QLA_FUNCTION_FAILED;
4041	mcp->mb[0] = MBC_GET_PORT_CONFIG;
4042	mcp->out_mb = MBX_0;
4043	mcp->in_mb = MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
4044	mcp->tov = MBX_TOV_SECONDS;
4045	mcp->flags = 0;
4046
4047	rval = qla2x00_mailbox_command(vha, mcp);
4048
4049	if (rval != QLA_SUCCESS) {
4050		DEBUG2_3_11(printk(KERN_WARNING
4051		    "%s(%ld): failed=%x (%x).\n", __func__,
4052		    vha->host_no, rval, mcp->mb[0]));
4053	} else {
4054		/* Copy all bits to preserve original value */
4055		memcpy(mb, &mcp->mb[1], sizeof(uint16_t) * 4);
4056
4057		DEBUG11(printk(KERN_INFO
4058		    "%s(%ld): done.\n", __func__, vha->host_no));
4059	}
4060	return rval;
4061}
4062
4063int
4064qla81xx_set_port_config(scsi_qla_host_t *vha, uint16_t *mb)
4065{
4066	int rval;
4067	mbx_cmd_t mc;
4068	mbx_cmd_t *mcp = &mc;
4069
4070	DEBUG11(printk(KERN_INFO
4071	    "%s(%ld): entered.\n", __func__, vha->host_no));
4072
4073	mcp->mb[0] = MBC_SET_PORT_CONFIG;
4074	/* Copy all bits to preserve original setting */
4075	memcpy(&mcp->mb[1], mb, sizeof(uint16_t) * 4);
4076	mcp->out_mb = MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
4077	mcp->in_mb = MBX_0;
4078	mcp->tov = MBX_TOV_SECONDS;
4079	mcp->flags = 0;
4080	rval = qla2x00_mailbox_command(vha, mcp);
4081
4082	if (rval != QLA_SUCCESS) {
4083		DEBUG2_3_11(printk(KERN_WARNING
4084		    "%s(%ld): failed=%x (%x).\n", __func__,
4085		    vha->host_no, rval, mcp->mb[0]));
4086	} else
4087		DEBUG11(printk(KERN_INFO
4088		    "%s(%ld): done.\n", __func__, vha->host_no));
4089
4090	return rval;
4091}
4092
4093
4094int
4095qla24xx_set_fcp_prio(scsi_qla_host_t *vha, uint16_t loop_id, uint16_t priority,
4096		uint16_t *mb)
4097{
4098	int rval;
4099	mbx_cmd_t mc;
4100	mbx_cmd_t *mcp = &mc;
4101	struct qla_hw_data *ha = vha->hw;
4102
4103	if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha))
4104		return QLA_FUNCTION_FAILED;
4105
4106	DEBUG11(printk(KERN_INFO
4107	    "%s(%ld): entered.\n", __func__, vha->host_no));
4108
4109	mcp->mb[0] = MBC_PORT_PARAMS;
4110	mcp->mb[1] = loop_id;
4111	if (ha->flags.fcp_prio_enabled)
4112		mcp->mb[2] = BIT_1;
4113	else
4114		mcp->mb[2] = BIT_2;
4115	mcp->mb[4] = priority & 0xf;
4116	mcp->mb[9] = vha->vp_idx;
4117	mcp->out_mb = MBX_9|MBX_4|MBX_3|MBX_2|MBX_1|MBX_0;
4118	mcp->in_mb = MBX_4|MBX_3|MBX_1|MBX_0;
4119	mcp->tov = 30;
4120	mcp->flags = 0;
4121	rval = qla2x00_mailbox_command(vha, mcp);
4122	if (mb != NULL) {
4123		mb[0] = mcp->mb[0];
4124		mb[1] = mcp->mb[1];
4125		mb[3] = mcp->mb[3];
4126		mb[4] = mcp->mb[4];
4127	}
4128
4129	if (rval != QLA_SUCCESS) {
4130		DEBUG2_3_11(printk(KERN_WARNING
4131		    "%s(%ld): failed=%x.\n", __func__,
4132		    vha->host_no, rval));
4133	} else {
4134		DEBUG11(printk(KERN_INFO
4135		    "%s(%ld): done.\n", __func__, vha->host_no));
4136	}
4137
4138	return rval;
4139}
4140
4141int
4142qla2x00_get_thermal_temp(scsi_qla_host_t *vha, uint16_t *temp, uint16_t *frac)
4143{
4144	int rval;
4145	uint8_t byte;
4146	struct qla_hw_data *ha = vha->hw;
4147
4148	DEBUG11(printk(KERN_INFO "%s(%ld): entered.\n", __func__, vha->host_no));
4149
4150	/* Integer part */
4151	rval = qla2x00_read_sfp(vha, 0, &byte, 0x98, 0x01, 1, BIT_13|BIT_0);
4152	if (rval != QLA_SUCCESS) {
4153		DEBUG2_3_11(printk(KERN_WARNING
4154		    "%s(%ld): failed=%x.\n", __func__, vha->host_no, rval));
4155		ha->flags.thermal_supported = 0;
4156		goto fail;
4157	}
4158	*temp = byte;
4159
4160	/* Fraction part */
4161	rval = qla2x00_read_sfp(vha, 0, &byte, 0x98, 0x10, 1, BIT_13|BIT_0);
4162	if (rval != QLA_SUCCESS) {
4163		DEBUG2_3_11(printk(KERN_WARNING
4164		    "%s(%ld): failed=%x.\n", __func__, vha->host_no, rval));
4165		ha->flags.thermal_supported = 0;
4166		goto fail;
4167	}
4168	*frac = (byte >> 6) * 25;
4169
4170	DEBUG11(printk(KERN_INFO "%s(%ld): done.\n", __func__, vha->host_no));
4171fail:
4172	return rval;
4173}
4174
4175int
4176qla82xx_mbx_intr_enable(scsi_qla_host_t *vha)
4177{
4178	int rval;
4179	struct qla_hw_data *ha = vha->hw;
4180	mbx_cmd_t mc;
4181	mbx_cmd_t *mcp = &mc;
4182
4183	if (!IS_FWI2_CAPABLE(ha))
4184		return QLA_FUNCTION_FAILED;
4185
4186	DEBUG11(qla_printk(KERN_INFO, ha,
4187		"%s(%ld): entered.\n", __func__, vha->host_no));
4188
4189	memset(mcp, 0, sizeof(mbx_cmd_t));
4190	mcp->mb[0] = MBC_TOGGLE_INTERRUPT;
4191	mcp->mb[1] = 1;
4192
4193	mcp->out_mb = MBX_1|MBX_0;
4194	mcp->in_mb = MBX_0;
4195	mcp->tov = 30;
4196	mcp->flags = 0;
4197
4198	rval = qla2x00_mailbox_command(vha, mcp);
4199	if (rval != QLA_SUCCESS) {
4200		DEBUG2_3_11(qla_printk(KERN_WARNING, ha,
4201			"%s(%ld): failed=%x mb[0]=%x.\n", __func__,
4202			vha->host_no, rval, mcp->mb[0]));
4203	} else {
4204		DEBUG11(qla_printk(KERN_INFO, ha,
4205			"%s(%ld): done.\n", __func__, vha->host_no));
4206	}
4207
4208	return rval;
4209}
4210
4211int
4212qla82xx_mbx_intr_disable(scsi_qla_host_t *vha)
4213{
4214	int rval;
4215	struct qla_hw_data *ha = vha->hw;
4216	mbx_cmd_t mc;
4217	mbx_cmd_t *mcp = &mc;
4218
4219	if (!IS_QLA82XX(ha))
4220		return QLA_FUNCTION_FAILED;
4221
4222	DEBUG11(qla_printk(KERN_INFO, ha,
4223		"%s(%ld): entered.\n", __func__, vha->host_no));
4224
4225	memset(mcp, 0, sizeof(mbx_cmd_t));
4226	mcp->mb[0] = MBC_TOGGLE_INTERRUPT;
4227	mcp->mb[1] = 0;
4228
4229	mcp->out_mb = MBX_1|MBX_0;
4230	mcp->in_mb = MBX_0;
4231	mcp->tov = 30;
4232	mcp->flags = 0;
4233
4234	rval = qla2x00_mailbox_command(vha, mcp);
4235	if (rval != QLA_SUCCESS) {
4236		DEBUG2_3_11(qla_printk(KERN_WARNING, ha,
4237			"%s(%ld): failed=%x mb[0]=%x.\n", __func__,
4238			vha->host_no, rval, mcp->mb[0]));
4239	} else {
4240		DEBUG11(qla_printk(KERN_INFO, ha,
4241			"%s(%ld): done.\n", __func__, vha->host_no));
4242	}
4243
4244	return rval;
4245}
4246