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