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