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