xref: /linux/drivers/scsi/qla2xxx/qla_isr.c (revision 3e85fd614c7b6bb7f33bb04a0dcb5a3bfca4c0fe)
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 
9 #include <linux/delay.h>
10 #include <scsi/scsi_tcq.h>
11 
12 static void qla2x00_mbx_completion(scsi_qla_host_t *, uint16_t);
13 static void qla2x00_process_completed_request(struct scsi_qla_host *,
14 	struct req_que *, uint32_t);
15 static void qla2x00_status_entry(scsi_qla_host_t *, struct rsp_que *, void *);
16 static void qla2x00_status_cont_entry(struct rsp_que *, sts_cont_entry_t *);
17 static void qla2x00_error_entry(scsi_qla_host_t *, struct rsp_que *,
18 	sts_entry_t *);
19 
20 /**
21  * qla2100_intr_handler() - Process interrupts for the ISP2100 and ISP2200.
22  * @irq:
23  * @dev_id: SCSI driver HA context
24  *
25  * Called by system whenever the host adapter generates an interrupt.
26  *
27  * Returns handled flag.
28  */
29 irqreturn_t
30 qla2100_intr_handler(int irq, void *dev_id)
31 {
32 	scsi_qla_host_t	*vha;
33 	struct qla_hw_data *ha;
34 	struct device_reg_2xxx __iomem *reg;
35 	int		status;
36 	unsigned long	iter;
37 	uint16_t	hccr;
38 	uint16_t	mb[4];
39 	struct rsp_que *rsp;
40 	unsigned long	flags;
41 
42 	rsp = (struct rsp_que *) dev_id;
43 	if (!rsp) {
44 		printk(KERN_INFO
45 		    "%s(): NULL response queue pointer\n", __func__);
46 		return (IRQ_NONE);
47 	}
48 
49 	ha = rsp->hw;
50 	reg = &ha->iobase->isp;
51 	status = 0;
52 
53 	spin_lock_irqsave(&ha->hardware_lock, flags);
54 	vha = pci_get_drvdata(ha->pdev);
55 	for (iter = 50; iter--; ) {
56 		hccr = RD_REG_WORD(&reg->hccr);
57 		if (hccr & HCCR_RISC_PAUSE) {
58 			if (pci_channel_offline(ha->pdev))
59 				break;
60 
61 			/*
62 			 * Issue a "HARD" reset in order for the RISC interrupt
63 			 * bit to be cleared.  Schedule a big hammmer to get
64 			 * out of the RISC PAUSED state.
65 			 */
66 			WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
67 			RD_REG_WORD(&reg->hccr);
68 
69 			ha->isp_ops->fw_dump(vha, 1);
70 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
71 			break;
72 		} else if ((RD_REG_WORD(&reg->istatus) & ISR_RISC_INT) == 0)
73 			break;
74 
75 		if (RD_REG_WORD(&reg->semaphore) & BIT_0) {
76 			WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
77 			RD_REG_WORD(&reg->hccr);
78 
79 			/* Get mailbox data. */
80 			mb[0] = RD_MAILBOX_REG(ha, reg, 0);
81 			if (mb[0] > 0x3fff && mb[0] < 0x8000) {
82 				qla2x00_mbx_completion(vha, mb[0]);
83 				status |= MBX_INTERRUPT;
84 			} else if (mb[0] > 0x7fff && mb[0] < 0xc000) {
85 				mb[1] = RD_MAILBOX_REG(ha, reg, 1);
86 				mb[2] = RD_MAILBOX_REG(ha, reg, 2);
87 				mb[3] = RD_MAILBOX_REG(ha, reg, 3);
88 				qla2x00_async_event(vha, rsp, mb);
89 			} else {
90 				/*EMPTY*/
91 				DEBUG2(printk("scsi(%ld): Unrecognized "
92 				    "interrupt type (%d).\n",
93 				    vha->host_no, mb[0]));
94 			}
95 			/* Release mailbox registers. */
96 			WRT_REG_WORD(&reg->semaphore, 0);
97 			RD_REG_WORD(&reg->semaphore);
98 		} else {
99 			qla2x00_process_response_queue(rsp);
100 
101 			WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
102 			RD_REG_WORD(&reg->hccr);
103 		}
104 	}
105 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
106 
107 	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
108 	    (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
109 		set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
110 		complete(&ha->mbx_intr_comp);
111 	}
112 
113 	return (IRQ_HANDLED);
114 }
115 
116 /**
117  * qla2300_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
118  * @irq:
119  * @dev_id: SCSI driver HA context
120  *
121  * Called by system whenever the host adapter generates an interrupt.
122  *
123  * Returns handled flag.
124  */
125 irqreturn_t
126 qla2300_intr_handler(int irq, void *dev_id)
127 {
128 	scsi_qla_host_t	*vha;
129 	struct device_reg_2xxx __iomem *reg;
130 	int		status;
131 	unsigned long	iter;
132 	uint32_t	stat;
133 	uint16_t	hccr;
134 	uint16_t	mb[4];
135 	struct rsp_que *rsp;
136 	struct qla_hw_data *ha;
137 	unsigned long	flags;
138 
139 	rsp = (struct rsp_que *) dev_id;
140 	if (!rsp) {
141 		printk(KERN_INFO
142 		    "%s(): NULL response queue pointer\n", __func__);
143 		return (IRQ_NONE);
144 	}
145 
146 	ha = rsp->hw;
147 	reg = &ha->iobase->isp;
148 	status = 0;
149 
150 	spin_lock_irqsave(&ha->hardware_lock, flags);
151 	vha = pci_get_drvdata(ha->pdev);
152 	for (iter = 50; iter--; ) {
153 		stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
154 		if (stat & HSR_RISC_PAUSED) {
155 			if (pci_channel_offline(ha->pdev))
156 				break;
157 
158 			hccr = RD_REG_WORD(&reg->hccr);
159 			if (hccr & (BIT_15 | BIT_13 | BIT_11 | BIT_8))
160 				qla_printk(KERN_INFO, ha, "Parity error -- "
161 				    "HCCR=%x, Dumping firmware!\n", hccr);
162 			else
163 				qla_printk(KERN_INFO, ha, "RISC paused -- "
164 				    "HCCR=%x, Dumping firmware!\n", hccr);
165 
166 			/*
167 			 * Issue a "HARD" reset in order for the RISC
168 			 * interrupt bit to be cleared.  Schedule a big
169 			 * hammmer to get out of the RISC PAUSED state.
170 			 */
171 			WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
172 			RD_REG_WORD(&reg->hccr);
173 
174 			ha->isp_ops->fw_dump(vha, 1);
175 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
176 			break;
177 		} else if ((stat & HSR_RISC_INT) == 0)
178 			break;
179 
180 		switch (stat & 0xff) {
181 		case 0x1:
182 		case 0x2:
183 		case 0x10:
184 		case 0x11:
185 			qla2x00_mbx_completion(vha, MSW(stat));
186 			status |= MBX_INTERRUPT;
187 
188 			/* Release mailbox registers. */
189 			WRT_REG_WORD(&reg->semaphore, 0);
190 			break;
191 		case 0x12:
192 			mb[0] = MSW(stat);
193 			mb[1] = RD_MAILBOX_REG(ha, reg, 1);
194 			mb[2] = RD_MAILBOX_REG(ha, reg, 2);
195 			mb[3] = RD_MAILBOX_REG(ha, reg, 3);
196 			qla2x00_async_event(vha, rsp, mb);
197 			break;
198 		case 0x13:
199 			qla2x00_process_response_queue(rsp);
200 			break;
201 		case 0x15:
202 			mb[0] = MBA_CMPLT_1_16BIT;
203 			mb[1] = MSW(stat);
204 			qla2x00_async_event(vha, rsp, mb);
205 			break;
206 		case 0x16:
207 			mb[0] = MBA_SCSI_COMPLETION;
208 			mb[1] = MSW(stat);
209 			mb[2] = RD_MAILBOX_REG(ha, reg, 2);
210 			qla2x00_async_event(vha, rsp, mb);
211 			break;
212 		default:
213 			DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
214 			    "(%d).\n",
215 			    vha->host_no, stat & 0xff));
216 			break;
217 		}
218 		WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
219 		RD_REG_WORD_RELAXED(&reg->hccr);
220 	}
221 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
222 
223 	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
224 	    (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
225 		set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
226 		complete(&ha->mbx_intr_comp);
227 	}
228 
229 	return (IRQ_HANDLED);
230 }
231 
232 /**
233  * qla2x00_mbx_completion() - Process mailbox command completions.
234  * @ha: SCSI driver HA context
235  * @mb0: Mailbox0 register
236  */
237 static void
238 qla2x00_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
239 {
240 	uint16_t	cnt;
241 	uint16_t __iomem *wptr;
242 	struct qla_hw_data *ha = vha->hw;
243 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
244 
245 	/* Load return mailbox registers. */
246 	ha->flags.mbox_int = 1;
247 	ha->mailbox_out[0] = mb0;
248 	wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 1);
249 
250 	for (cnt = 1; cnt < ha->mbx_count; cnt++) {
251 		if (IS_QLA2200(ha) && cnt == 8)
252 			wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 8);
253 		if (cnt == 4 || cnt == 5)
254 			ha->mailbox_out[cnt] = qla2x00_debounce_register(wptr);
255 		else
256 			ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
257 
258 		wptr++;
259 	}
260 
261 	if (ha->mcp) {
262 		DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
263 		    __func__, vha->host_no, ha->mcp->mb[0]));
264 	} else {
265 		DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
266 		    __func__, vha->host_no));
267 	}
268 }
269 
270 static void
271 qla81xx_idc_event(scsi_qla_host_t *vha, uint16_t aen, uint16_t descr)
272 {
273 	static char *event[] =
274 		{ "Complete", "Request Notification", "Time Extension" };
275 	int rval;
276 	struct device_reg_24xx __iomem *reg24 = &vha->hw->iobase->isp24;
277 	uint16_t __iomem *wptr;
278 	uint16_t cnt, timeout, mb[QLA_IDC_ACK_REGS];
279 
280 	/* Seed data -- mailbox1 -> mailbox7. */
281 	wptr = (uint16_t __iomem *)&reg24->mailbox1;
282 	for (cnt = 0; cnt < QLA_IDC_ACK_REGS; cnt++, wptr++)
283 		mb[cnt] = RD_REG_WORD(wptr);
284 
285 	DEBUG2(printk("scsi(%ld): Inter-Driver Commucation %s -- "
286 	    "%04x %04x %04x %04x %04x %04x %04x.\n", vha->host_no,
287 	    event[aen & 0xff],
288 	    mb[0], mb[1], mb[2], mb[3], mb[4], mb[5], mb[6]));
289 
290 	/* Acknowledgement needed? [Notify && non-zero timeout]. */
291 	timeout = (descr >> 8) & 0xf;
292 	if (aen != MBA_IDC_NOTIFY || !timeout)
293 		return;
294 
295 	DEBUG2(printk("scsi(%ld): Inter-Driver Commucation %s -- "
296 	    "ACK timeout=%d.\n", vha->host_no, event[aen & 0xff], timeout));
297 
298 	rval = qla2x00_post_idc_ack_work(vha, mb);
299 	if (rval != QLA_SUCCESS)
300 		qla_printk(KERN_WARNING, vha->hw,
301 		    "IDC failed to post ACK.\n");
302 }
303 
304 /**
305  * qla2x00_async_event() - Process aynchronous events.
306  * @ha: SCSI driver HA context
307  * @mb: Mailbox registers (0 - 3)
308  */
309 void
310 qla2x00_async_event(scsi_qla_host_t *vha, struct rsp_que *rsp, uint16_t *mb)
311 {
312 #define LS_UNKNOWN	2
313 	static char	*link_speeds[] = { "1", "2", "?", "4", "8", "10" };
314 	char		*link_speed;
315 	uint16_t	handle_cnt;
316 	uint16_t	cnt, mbx;
317 	uint32_t	handles[5];
318 	struct qla_hw_data *ha = vha->hw;
319 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
320 	struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
321 	uint32_t	rscn_entry, host_pid;
322 	uint8_t		rscn_queue_index;
323 	unsigned long	flags;
324 
325 	/* Setup to process RIO completion. */
326 	handle_cnt = 0;
327 	if (IS_QLA81XX(ha))
328 		goto skip_rio;
329 	switch (mb[0]) {
330 	case MBA_SCSI_COMPLETION:
331 		handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
332 		handle_cnt = 1;
333 		break;
334 	case MBA_CMPLT_1_16BIT:
335 		handles[0] = mb[1];
336 		handle_cnt = 1;
337 		mb[0] = MBA_SCSI_COMPLETION;
338 		break;
339 	case MBA_CMPLT_2_16BIT:
340 		handles[0] = mb[1];
341 		handles[1] = mb[2];
342 		handle_cnt = 2;
343 		mb[0] = MBA_SCSI_COMPLETION;
344 		break;
345 	case MBA_CMPLT_3_16BIT:
346 		handles[0] = mb[1];
347 		handles[1] = mb[2];
348 		handles[2] = mb[3];
349 		handle_cnt = 3;
350 		mb[0] = MBA_SCSI_COMPLETION;
351 		break;
352 	case MBA_CMPLT_4_16BIT:
353 		handles[0] = mb[1];
354 		handles[1] = mb[2];
355 		handles[2] = mb[3];
356 		handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
357 		handle_cnt = 4;
358 		mb[0] = MBA_SCSI_COMPLETION;
359 		break;
360 	case MBA_CMPLT_5_16BIT:
361 		handles[0] = mb[1];
362 		handles[1] = mb[2];
363 		handles[2] = mb[3];
364 		handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
365 		handles[4] = (uint32_t)RD_MAILBOX_REG(ha, reg, 7);
366 		handle_cnt = 5;
367 		mb[0] = MBA_SCSI_COMPLETION;
368 		break;
369 	case MBA_CMPLT_2_32BIT:
370 		handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
371 		handles[1] = le32_to_cpu(
372 		    ((uint32_t)(RD_MAILBOX_REG(ha, reg, 7) << 16)) |
373 		    RD_MAILBOX_REG(ha, reg, 6));
374 		handle_cnt = 2;
375 		mb[0] = MBA_SCSI_COMPLETION;
376 		break;
377 	default:
378 		break;
379 	}
380 skip_rio:
381 	switch (mb[0]) {
382 	case MBA_SCSI_COMPLETION:	/* Fast Post */
383 		if (!vha->flags.online)
384 			break;
385 
386 		for (cnt = 0; cnt < handle_cnt; cnt++)
387 			qla2x00_process_completed_request(vha, rsp->req,
388 				handles[cnt]);
389 		break;
390 
391 	case MBA_RESET:			/* Reset */
392 		DEBUG2(printk("scsi(%ld): Asynchronous RESET.\n",
393 			vha->host_no));
394 
395 		set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
396 		break;
397 
398 	case MBA_SYSTEM_ERR:		/* System Error */
399 		mbx = IS_QLA81XX(ha) ? RD_REG_WORD(&reg24->mailbox7) : 0;
400 		qla_printk(KERN_INFO, ha,
401 		    "ISP System Error - mbx1=%xh mbx2=%xh mbx3=%xh "
402 		    "mbx7=%xh.\n", mb[1], mb[2], mb[3], mbx);
403 
404 		ha->isp_ops->fw_dump(vha, 1);
405 
406 		if (IS_FWI2_CAPABLE(ha)) {
407 			if (mb[1] == 0 && mb[2] == 0) {
408 				qla_printk(KERN_ERR, ha,
409 				    "Unrecoverable Hardware Error: adapter "
410 				    "marked OFFLINE!\n");
411 				vha->flags.online = 0;
412 			} else
413 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
414 		} else if (mb[1] == 0) {
415 			qla_printk(KERN_INFO, ha,
416 			    "Unrecoverable Hardware Error: adapter marked "
417 			    "OFFLINE!\n");
418 			vha->flags.online = 0;
419 		} else
420 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
421 		break;
422 
423 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
424 		DEBUG2(printk("scsi(%ld): ISP Request Transfer Error (%x).\n",
425 		    vha->host_no, mb[1]));
426 		qla_printk(KERN_WARNING, ha,
427 		    "ISP Request Transfer Error (%x).\n", mb[1]);
428 
429 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
430 		break;
431 
432 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
433 		DEBUG2(printk("scsi(%ld): ISP Response Transfer Error.\n",
434 		    vha->host_no));
435 		qla_printk(KERN_WARNING, ha, "ISP Response Transfer Error.\n");
436 
437 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
438 		break;
439 
440 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up */
441 		DEBUG2(printk("scsi(%ld): Asynchronous WAKEUP_THRES.\n",
442 		    vha->host_no));
443 		break;
444 
445 	case MBA_LIP_OCCURRED:		/* Loop Initialization Procedure */
446 		DEBUG2(printk("scsi(%ld): LIP occurred (%x).\n", vha->host_no,
447 		    mb[1]));
448 		qla_printk(KERN_INFO, ha, "LIP occurred (%x).\n", mb[1]);
449 
450 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
451 			atomic_set(&vha->loop_state, LOOP_DOWN);
452 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
453 			qla2x00_mark_all_devices_lost(vha, 1);
454 		}
455 
456 		if (vha->vp_idx) {
457 			atomic_set(&vha->vp_state, VP_FAILED);
458 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
459 		}
460 
461 		set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
462 		set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
463 
464 		vha->flags.management_server_logged_in = 0;
465 		qla2x00_post_aen_work(vha, FCH_EVT_LIP, mb[1]);
466 		break;
467 
468 	case MBA_LOOP_UP:		/* Loop Up Event */
469 		if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
470 			link_speed = link_speeds[0];
471 			ha->link_data_rate = PORT_SPEED_1GB;
472 		} else {
473 			link_speed = link_speeds[LS_UNKNOWN];
474 			if (mb[1] < 5)
475 				link_speed = link_speeds[mb[1]];
476 			else if (mb[1] == 0x13)
477 				link_speed = link_speeds[5];
478 			ha->link_data_rate = mb[1];
479 		}
480 
481 		DEBUG2(printk("scsi(%ld): Asynchronous LOOP UP (%s Gbps).\n",
482 		    vha->host_no, link_speed));
483 		qla_printk(KERN_INFO, ha, "LOOP UP detected (%s Gbps).\n",
484 		    link_speed);
485 
486 		vha->flags.management_server_logged_in = 0;
487 		qla2x00_post_aen_work(vha, FCH_EVT_LINKUP, ha->link_data_rate);
488 		break;
489 
490 	case MBA_LOOP_DOWN:		/* Loop Down Event */
491 		mbx = IS_QLA81XX(ha) ? RD_REG_WORD(&reg24->mailbox4) : 0;
492 		DEBUG2(printk("scsi(%ld): Asynchronous LOOP DOWN "
493 		    "(%x %x %x %x).\n", vha->host_no, mb[1], mb[2], mb[3],
494 		    mbx));
495 		qla_printk(KERN_INFO, ha,
496 		    "LOOP DOWN detected (%x %x %x %x).\n", mb[1], mb[2], mb[3],
497 		    mbx);
498 
499 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
500 			atomic_set(&vha->loop_state, LOOP_DOWN);
501 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
502 			vha->device_flags |= DFLG_NO_CABLE;
503 			qla2x00_mark_all_devices_lost(vha, 1);
504 		}
505 
506 		if (vha->vp_idx) {
507 			atomic_set(&vha->vp_state, VP_FAILED);
508 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
509 		}
510 
511 		vha->flags.management_server_logged_in = 0;
512 		ha->link_data_rate = PORT_SPEED_UNKNOWN;
513 		qla2x00_post_aen_work(vha, FCH_EVT_LINKDOWN, 0);
514 		break;
515 
516 	case MBA_LIP_RESET:		/* LIP reset occurred */
517 		DEBUG2(printk("scsi(%ld): Asynchronous LIP RESET (%x).\n",
518 		    vha->host_no, mb[1]));
519 		qla_printk(KERN_INFO, ha,
520 		    "LIP reset occurred (%x).\n", mb[1]);
521 
522 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
523 			atomic_set(&vha->loop_state, LOOP_DOWN);
524 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
525 			qla2x00_mark_all_devices_lost(vha, 1);
526 		}
527 
528 		if (vha->vp_idx) {
529 			atomic_set(&vha->vp_state, VP_FAILED);
530 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
531 		}
532 
533 		set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
534 
535 		ha->operating_mode = LOOP;
536 		vha->flags.management_server_logged_in = 0;
537 		qla2x00_post_aen_work(vha, FCH_EVT_LIPRESET, mb[1]);
538 		break;
539 
540 	/* case MBA_DCBX_COMPLETE: */
541 	case MBA_POINT_TO_POINT:	/* Point-to-Point */
542 		if (IS_QLA2100(ha))
543 			break;
544 
545 		if (IS_QLA81XX(ha))
546 			DEBUG2(printk("scsi(%ld): DCBX Completed -- %04x %04x "
547 			    "%04x\n", vha->host_no, mb[1], mb[2], mb[3]));
548 		else
549 			DEBUG2(printk("scsi(%ld): Asynchronous P2P MODE "
550 			    "received.\n", vha->host_no));
551 
552 		/*
553 		 * Until there's a transition from loop down to loop up, treat
554 		 * this as loop down only.
555 		 */
556 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
557 			atomic_set(&vha->loop_state, LOOP_DOWN);
558 			if (!atomic_read(&vha->loop_down_timer))
559 				atomic_set(&vha->loop_down_timer,
560 				    LOOP_DOWN_TIME);
561 			qla2x00_mark_all_devices_lost(vha, 1);
562 		}
563 
564 		if (vha->vp_idx) {
565 			atomic_set(&vha->vp_state, VP_FAILED);
566 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
567 		}
568 
569 		if (!(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)))
570 			set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
571 
572 		set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
573 		set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
574 
575 		ha->flags.gpsc_supported = 1;
576 		vha->flags.management_server_logged_in = 0;
577 		break;
578 
579 	case MBA_CHG_IN_CONNECTION:	/* Change in connection mode */
580 		if (IS_QLA2100(ha))
581 			break;
582 
583 		DEBUG2(printk("scsi(%ld): Asynchronous Change In Connection "
584 		    "received.\n",
585 		    vha->host_no));
586 		qla_printk(KERN_INFO, ha,
587 		    "Configuration change detected: value=%x.\n", mb[1]);
588 
589 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
590 			atomic_set(&vha->loop_state, LOOP_DOWN);
591 			if (!atomic_read(&vha->loop_down_timer))
592 				atomic_set(&vha->loop_down_timer,
593 				    LOOP_DOWN_TIME);
594 			qla2x00_mark_all_devices_lost(vha, 1);
595 		}
596 
597 		if (vha->vp_idx) {
598 			atomic_set(&vha->vp_state, VP_FAILED);
599 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
600 		}
601 
602 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
603 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
604 		break;
605 
606 	case MBA_PORT_UPDATE:		/* Port database update */
607 		/*
608 		 * Handle only global and vn-port update events
609 		 *
610 		 * Relevant inputs:
611 		 * mb[1] = N_Port handle of changed port
612 		 * OR 0xffff for global event
613 		 * mb[2] = New login state
614 		 * 7 = Port logged out
615 		 * mb[3] = LSB is vp_idx, 0xff = all vps
616 		 *
617 		 * Skip processing if:
618 		 *       Event is global, vp_idx is NOT all vps,
619 		 *           vp_idx does not match
620 		 *       Event is not global, vp_idx does not match
621 		 */
622 		if ((mb[1] == 0xffff && (mb[3] & 0xff) != 0xff)
623 			|| (mb[1] != 0xffff)) {
624 			if (vha->vp_idx != (mb[3] & 0xff))
625 				break;
626 		}
627 
628 		/* Global event -- port logout or port unavailable. */
629 		if (mb[1] == 0xffff && mb[2] == 0x7) {
630 			DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE.\n",
631 			    vha->host_no));
632 			DEBUG(printk(KERN_INFO
633 			    "scsi(%ld): Port unavailable %04x %04x %04x.\n",
634 			    vha->host_no, mb[1], mb[2], mb[3]));
635 
636 			if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
637 				atomic_set(&vha->loop_state, LOOP_DOWN);
638 				atomic_set(&vha->loop_down_timer,
639 				    LOOP_DOWN_TIME);
640 				vha->device_flags |= DFLG_NO_CABLE;
641 				qla2x00_mark_all_devices_lost(vha, 1);
642 			}
643 
644 			if (vha->vp_idx) {
645 				atomic_set(&vha->vp_state, VP_FAILED);
646 				fc_vport_set_state(vha->fc_vport,
647 				    FC_VPORT_FAILED);
648 				qla2x00_mark_all_devices_lost(vha, 1);
649 			}
650 
651 			vha->flags.management_server_logged_in = 0;
652 			ha->link_data_rate = PORT_SPEED_UNKNOWN;
653 			break;
654 		}
655 
656 		/*
657 		 * If PORT UPDATE is global (received LIP_OCCURRED/LIP_RESET
658 		 * event etc. earlier indicating loop is down) then process
659 		 * it.  Otherwise ignore it and Wait for RSCN to come in.
660 		 */
661 		atomic_set(&vha->loop_down_timer, 0);
662 		if (atomic_read(&vha->loop_state) != LOOP_DOWN &&
663 		    atomic_read(&vha->loop_state) != LOOP_DEAD) {
664 			DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE "
665 			    "ignored %04x/%04x/%04x.\n", vha->host_no, mb[1],
666 			    mb[2], mb[3]));
667 			break;
668 		}
669 
670 		DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE.\n",
671 		    vha->host_no));
672 		DEBUG(printk(KERN_INFO
673 		    "scsi(%ld): Port database changed %04x %04x %04x.\n",
674 		    vha->host_no, mb[1], mb[2], mb[3]));
675 
676 		/*
677 		 * Mark all devices as missing so we will login again.
678 		 */
679 		atomic_set(&vha->loop_state, LOOP_UP);
680 
681 		qla2x00_mark_all_devices_lost(vha, 1);
682 
683 		vha->flags.rscn_queue_overflow = 1;
684 
685 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
686 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
687 		break;
688 
689 	case MBA_RSCN_UPDATE:		/* State Change Registration */
690 		/* Check if the Vport has issued a SCR */
691 		if (vha->vp_idx && test_bit(VP_SCR_NEEDED, &vha->vp_flags))
692 			break;
693 		/* Only handle SCNs for our Vport index. */
694 		if (ha->flags.npiv_supported && vha->vp_idx != (mb[3] & 0xff))
695 			break;
696 
697 		DEBUG2(printk("scsi(%ld): Asynchronous RSCR UPDATE.\n",
698 		    vha->host_no));
699 		DEBUG(printk(KERN_INFO
700 		    "scsi(%ld): RSCN database changed -- %04x %04x %04x.\n",
701 		    vha->host_no, mb[1], mb[2], mb[3]));
702 
703 		rscn_entry = ((mb[1] & 0xff) << 16) | mb[2];
704 		host_pid = (vha->d_id.b.domain << 16) | (vha->d_id.b.area << 8)
705 				| vha->d_id.b.al_pa;
706 		if (rscn_entry == host_pid) {
707 			DEBUG(printk(KERN_INFO
708 			    "scsi(%ld): Ignoring RSCN update to local host "
709 			    "port ID (%06x)\n",
710 			    vha->host_no, host_pid));
711 			break;
712 		}
713 
714 		/* Ignore reserved bits from RSCN-payload. */
715 		rscn_entry = ((mb[1] & 0x3ff) << 16) | mb[2];
716 		rscn_queue_index = vha->rscn_in_ptr + 1;
717 		if (rscn_queue_index == MAX_RSCN_COUNT)
718 			rscn_queue_index = 0;
719 		if (rscn_queue_index != vha->rscn_out_ptr) {
720 			vha->rscn_queue[vha->rscn_in_ptr] = rscn_entry;
721 			vha->rscn_in_ptr = rscn_queue_index;
722 		} else {
723 			vha->flags.rscn_queue_overflow = 1;
724 		}
725 
726 		atomic_set(&vha->loop_state, LOOP_UPDATE);
727 		atomic_set(&vha->loop_down_timer, 0);
728 		vha->flags.management_server_logged_in = 0;
729 
730 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
731 		set_bit(RSCN_UPDATE, &vha->dpc_flags);
732 		qla2x00_post_aen_work(vha, FCH_EVT_RSCN, rscn_entry);
733 		break;
734 
735 	/* case MBA_RIO_RESPONSE: */
736 	case MBA_ZIO_RESPONSE:
737 		DEBUG3(printk("scsi(%ld): [R|Z]IO update completion.\n",
738 		    vha->host_no));
739 
740 		if (IS_FWI2_CAPABLE(ha))
741 			qla24xx_process_response_queue(vha, rsp);
742 		else
743 			qla2x00_process_response_queue(rsp);
744 		break;
745 
746 	case MBA_DISCARD_RND_FRAME:
747 		DEBUG2(printk("scsi(%ld): Discard RND Frame -- %04x %04x "
748 		    "%04x.\n", vha->host_no, mb[1], mb[2], mb[3]));
749 		break;
750 
751 	case MBA_TRACE_NOTIFICATION:
752 		DEBUG2(printk("scsi(%ld): Trace Notification -- %04x %04x.\n",
753 		vha->host_no, mb[1], mb[2]));
754 		break;
755 
756 	case MBA_ISP84XX_ALERT:
757 		DEBUG2(printk("scsi(%ld): ISP84XX Alert Notification -- "
758 		    "%04x %04x %04x\n", vha->host_no, mb[1], mb[2], mb[3]));
759 
760 		spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
761 		switch (mb[1]) {
762 		case A84_PANIC_RECOVERY:
763 			qla_printk(KERN_INFO, ha, "Alert 84XX: panic recovery "
764 			    "%04x %04x\n", mb[2], mb[3]);
765 			break;
766 		case A84_OP_LOGIN_COMPLETE:
767 			ha->cs84xx->op_fw_version = mb[3] << 16 | mb[2];
768 			DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
769 			    "firmware version %x\n", ha->cs84xx->op_fw_version));
770 			break;
771 		case A84_DIAG_LOGIN_COMPLETE:
772 			ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
773 			DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
774 			    "diagnostic firmware version %x\n",
775 			    ha->cs84xx->diag_fw_version));
776 			break;
777 		case A84_GOLD_LOGIN_COMPLETE:
778 			ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
779 			ha->cs84xx->fw_update = 1;
780 			DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX: gold "
781 			    "firmware version %x\n",
782 			    ha->cs84xx->gold_fw_version));
783 			break;
784 		default:
785 			qla_printk(KERN_ERR, ha,
786 			    "Alert 84xx: Invalid Alert %04x %04x %04x\n",
787 			    mb[1], mb[2], mb[3]);
788 		}
789 		spin_unlock_irqrestore(&ha->cs84xx->access_lock, flags);
790 		break;
791 	case MBA_DCBX_START:
792 		DEBUG2(printk("scsi(%ld): DCBX Started -- %04x %04x %04x\n",
793 		    vha->host_no, mb[1], mb[2], mb[3]));
794 		break;
795 	case MBA_DCBX_PARAM_UPDATE:
796 		DEBUG2(printk("scsi(%ld): DCBX Parameters Updated -- "
797 		    "%04x %04x %04x\n", vha->host_no, mb[1], mb[2], mb[3]));
798 		break;
799 	case MBA_FCF_CONF_ERR:
800 		DEBUG2(printk("scsi(%ld): FCF Configuration Error -- "
801 		    "%04x %04x %04x\n", vha->host_no, mb[1], mb[2], mb[3]));
802 		break;
803 	case MBA_IDC_COMPLETE:
804 	case MBA_IDC_NOTIFY:
805 	case MBA_IDC_TIME_EXT:
806 		qla81xx_idc_event(vha, mb[0], mb[1]);
807 		break;
808 	}
809 
810 	if (!vha->vp_idx && ha->num_vhosts)
811 		qla2x00_alert_all_vps(rsp, mb);
812 }
813 
814 static void
815 qla2x00_adjust_sdev_qdepth_up(struct scsi_device *sdev, void *data)
816 {
817 	fc_port_t *fcport = data;
818 	struct scsi_qla_host *vha = fcport->vha;
819 	struct qla_hw_data *ha = vha->hw;
820 	struct req_que *req = NULL;
821 
822 	if (!ql2xqfulltracking)
823 		return;
824 
825 	req = vha->req;
826 	if (!req)
827 		return;
828 	if (req->max_q_depth <= sdev->queue_depth)
829 		return;
830 
831 	if (sdev->ordered_tags)
832 		scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG,
833 		    sdev->queue_depth + 1);
834 	else
835 		scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG,
836 		    sdev->queue_depth + 1);
837 
838 	fcport->last_ramp_up = jiffies;
839 
840 	DEBUG2(qla_printk(KERN_INFO, ha,
841 	    "scsi(%ld:%d:%d:%d): Queue depth adjusted-up to %d.\n",
842 	    fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
843 	    sdev->queue_depth));
844 }
845 
846 static void
847 qla2x00_adjust_sdev_qdepth_down(struct scsi_device *sdev, void *data)
848 {
849 	fc_port_t *fcport = data;
850 
851 	if (!scsi_track_queue_full(sdev, sdev->queue_depth - 1))
852 		return;
853 
854 	DEBUG2(qla_printk(KERN_INFO, fcport->vha->hw,
855 	    "scsi(%ld:%d:%d:%d): Queue depth adjusted-down to %d.\n",
856 	    fcport->vha->host_no, sdev->channel, sdev->id, sdev->lun,
857 	    sdev->queue_depth));
858 }
859 
860 static inline void
861 qla2x00_ramp_up_queue_depth(scsi_qla_host_t *vha, struct req_que *req,
862 								srb_t *sp)
863 {
864 	fc_port_t *fcport;
865 	struct scsi_device *sdev;
866 
867 	if (!ql2xqfulltracking)
868 		return;
869 
870 	sdev = sp->cmd->device;
871 	if (sdev->queue_depth >= req->max_q_depth)
872 		return;
873 
874 	fcport = sp->fcport;
875 	if (time_before(jiffies,
876 	    fcport->last_ramp_up + ql2xqfullrampup * HZ))
877 		return;
878 	if (time_before(jiffies,
879 	    fcport->last_queue_full + ql2xqfullrampup * HZ))
880 		return;
881 
882 	starget_for_each_device(sdev->sdev_target, fcport,
883 	    qla2x00_adjust_sdev_qdepth_up);
884 }
885 
886 /**
887  * qla2x00_process_completed_request() - Process a Fast Post response.
888  * @ha: SCSI driver HA context
889  * @index: SRB index
890  */
891 static void
892 qla2x00_process_completed_request(struct scsi_qla_host *vha,
893 				struct req_que *req, uint32_t index)
894 {
895 	srb_t *sp;
896 	struct qla_hw_data *ha = vha->hw;
897 
898 	/* Validate handle. */
899 	if (index >= MAX_OUTSTANDING_COMMANDS) {
900 		DEBUG2(printk("scsi(%ld): Invalid SCSI completion handle %d.\n",
901 		    vha->host_no, index));
902 		qla_printk(KERN_WARNING, ha,
903 		    "Invalid SCSI completion handle %d.\n", index);
904 
905 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
906 		return;
907 	}
908 
909 	sp = req->outstanding_cmds[index];
910 	if (sp) {
911 		/* Free outstanding command slot. */
912 		req->outstanding_cmds[index] = NULL;
913 
914 		/* Save ISP completion status */
915 		sp->cmd->result = DID_OK << 16;
916 
917 		qla2x00_ramp_up_queue_depth(vha, req, sp);
918 		qla2x00_sp_compl(ha, sp);
919 	} else {
920 		DEBUG2(printk("scsi(%ld) Req:%d: Invalid ISP SCSI completion"
921 			" handle(%d)\n", vha->host_no, req->id, index));
922 		qla_printk(KERN_WARNING, ha,
923 		    "Invalid ISP SCSI completion handle\n");
924 
925 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
926 	}
927 }
928 
929 static srb_t *
930 qla2x00_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
931     struct req_que *req, void *iocb)
932 {
933 	struct qla_hw_data *ha = vha->hw;
934 	sts_entry_t *pkt = iocb;
935 	srb_t *sp = NULL;
936 	uint16_t index;
937 
938 	index = LSW(pkt->handle);
939 	if (index >= MAX_OUTSTANDING_COMMANDS) {
940 		qla_printk(KERN_WARNING, ha,
941 		    "%s: Invalid completion handle (%x).\n", func, index);
942 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
943 		goto done;
944 	}
945 	sp = req->outstanding_cmds[index];
946 	if (!sp) {
947 		qla_printk(KERN_WARNING, ha,
948 		    "%s: Invalid completion handle (%x) -- timed-out.\n", func,
949 		    index);
950 		return sp;
951 	}
952 	if (sp->handle != index) {
953 		qla_printk(KERN_WARNING, ha,
954 		    "%s: SRB handle (%x) mismatch %x.\n", func, sp->handle,
955 		    index);
956 		return NULL;
957 	}
958 	req->outstanding_cmds[index] = NULL;
959 done:
960 	return sp;
961 }
962 
963 static void
964 qla2x00_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
965     struct mbx_entry *mbx)
966 {
967 	const char func[] = "MBX-IOCB";
968 	const char *type;
969 	struct qla_hw_data *ha = vha->hw;
970 	fc_port_t *fcport;
971 	srb_t *sp;
972 	struct srb_logio *lio;
973 	uint16_t data[2];
974 
975 	sp = qla2x00_get_sp_from_handle(vha, func, req, mbx);
976 	if (!sp)
977 		return;
978 
979 	type = NULL;
980 	lio = sp->ctx;
981 	switch (lio->ctx.type) {
982 	case SRB_LOGIN_CMD:
983 		type = "login";
984 		break;
985 	case SRB_LOGOUT_CMD:
986 		type = "logout";
987 		break;
988 	default:
989 		qla_printk(KERN_WARNING, ha,
990 		    "%s: Unrecognized SRB: (%p) type=%d.\n", func, sp,
991 		    lio->ctx.type);
992 		return;
993 	}
994 
995 	del_timer(&lio->ctx.timer);
996 	fcport = sp->fcport;
997 
998 	data[0] = data[1] = 0;
999 	if (mbx->entry_status) {
1000 		DEBUG2(printk(KERN_WARNING
1001 		    "scsi(%ld:%x): Async-%s error entry - entry-status=%x "
1002 		    "status=%x state-flag=%x status-flags=%x.\n",
1003 		    fcport->vha->host_no, sp->handle, type,
1004 		    mbx->entry_status, le16_to_cpu(mbx->status),
1005 		    le16_to_cpu(mbx->state_flags),
1006 		    le16_to_cpu(mbx->status_flags)));
1007 		DEBUG2(qla2x00_dump_buffer((uint8_t *)mbx, sizeof(*mbx)));
1008 
1009 		data[0] = MBS_COMMAND_ERROR;
1010 		data[1] = lio->flags & SRB_LOGIN_RETRIED ?
1011 		    QLA_LOGIO_LOGIN_RETRIED: 0;
1012 		goto done_post_logio_done_work;
1013 	}
1014 
1015 	if (!mbx->status && le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE) {
1016 		DEBUG2(printk(KERN_DEBUG
1017 		    "scsi(%ld:%x): Async-%s complete - mbx1=%x.\n",
1018 		    fcport->vha->host_no, sp->handle, type,
1019 		    le16_to_cpu(mbx->mb1)));
1020 
1021 		data[0] = MBS_COMMAND_COMPLETE;
1022 		if (lio->ctx.type == SRB_LOGIN_CMD && le16_to_cpu(mbx->mb1) & BIT_1)
1023 			fcport->flags |= FCF_FCP2_DEVICE;
1024 
1025 		goto done_post_logio_done_work;
1026 	}
1027 
1028 	data[0] = le16_to_cpu(mbx->mb0);
1029 	switch (data[0]) {
1030 	case MBS_PORT_ID_USED:
1031 		data[1] = le16_to_cpu(mbx->mb1);
1032 		break;
1033 	case MBS_LOOP_ID_USED:
1034 		break;
1035 	default:
1036 		data[0] = MBS_COMMAND_ERROR;
1037 		data[1] = lio->flags & SRB_LOGIN_RETRIED ?
1038 		    QLA_LOGIO_LOGIN_RETRIED: 0;
1039 		break;
1040 	}
1041 
1042 	DEBUG2(printk(KERN_WARNING
1043 	    "scsi(%ld:%x): Async-%s failed - status=%x mb0=%x mb1=%x mb2=%x "
1044 	    "mb6=%x mb7=%x.\n",
1045 	    fcport->vha->host_no, sp->handle, type, le16_to_cpu(mbx->status),
1046 	    le16_to_cpu(mbx->mb0), le16_to_cpu(mbx->mb1),
1047 	    le16_to_cpu(mbx->mb2), le16_to_cpu(mbx->mb6),
1048 	    le16_to_cpu(mbx->mb7)));
1049 
1050 done_post_logio_done_work:
1051 	lio->ctx.type == SRB_LOGIN_CMD ?
1052 	    qla2x00_post_async_login_done_work(fcport->vha, fcport, data):
1053 	    qla2x00_post_async_logout_done_work(fcport->vha, fcport, data);
1054 
1055 	lio->ctx.free(sp);
1056 }
1057 
1058 static void
1059 qla24xx_logio_entry(scsi_qla_host_t *vha, struct req_que *req,
1060     struct logio_entry_24xx *logio)
1061 {
1062 	const char func[] = "LOGIO-IOCB";
1063 	const char *type;
1064 	struct qla_hw_data *ha = vha->hw;
1065 	fc_port_t *fcport;
1066 	srb_t *sp;
1067 	struct srb_logio *lio;
1068 	uint16_t data[2];
1069 	uint32_t iop[2];
1070 
1071 	sp = qla2x00_get_sp_from_handle(vha, func, req, logio);
1072 	if (!sp)
1073 		return;
1074 
1075 	type = NULL;
1076 	lio = sp->ctx;
1077 	switch (lio->ctx.type) {
1078 	case SRB_LOGIN_CMD:
1079 		type = "login";
1080 		break;
1081 	case SRB_LOGOUT_CMD:
1082 		type = "logout";
1083 		break;
1084 	default:
1085 		qla_printk(KERN_WARNING, ha,
1086 		    "%s: Unrecognized SRB: (%p) type=%d.\n", func, sp,
1087 		    lio->ctx.type);
1088 		return;
1089 	}
1090 
1091 	del_timer(&lio->ctx.timer);
1092 	fcport = sp->fcport;
1093 
1094 	data[0] = data[1] = 0;
1095 	if (logio->entry_status) {
1096 		DEBUG2(printk(KERN_WARNING
1097 		    "scsi(%ld:%x): Async-%s error entry - entry-status=%x.\n",
1098 		    fcport->vha->host_no, sp->handle, type,
1099 		    logio->entry_status));
1100 		DEBUG2(qla2x00_dump_buffer((uint8_t *)logio, sizeof(*logio)));
1101 
1102 		data[0] = MBS_COMMAND_ERROR;
1103 		data[1] = lio->flags & SRB_LOGIN_RETRIED ?
1104 		    QLA_LOGIO_LOGIN_RETRIED: 0;
1105 		goto done_post_logio_done_work;
1106 	}
1107 
1108 	if (le16_to_cpu(logio->comp_status) == CS_COMPLETE) {
1109 		DEBUG2(printk(KERN_DEBUG
1110 		    "scsi(%ld:%x): Async-%s complete - iop0=%x.\n",
1111 		    fcport->vha->host_no, sp->handle, type,
1112 		    le32_to_cpu(logio->io_parameter[0])));
1113 
1114 		data[0] = MBS_COMMAND_COMPLETE;
1115 		if (lio->ctx.type == SRB_LOGOUT_CMD)
1116 			goto done_post_logio_done_work;
1117 
1118 		iop[0] = le32_to_cpu(logio->io_parameter[0]);
1119 		if (iop[0] & BIT_4) {
1120 			fcport->port_type = FCT_TARGET;
1121 			if (iop[0] & BIT_8)
1122 				fcport->flags |= FCF_FCP2_DEVICE;
1123 		}
1124 		if (iop[0] & BIT_5)
1125 			fcport->port_type = FCT_INITIATOR;
1126 		if (logio->io_parameter[7] || logio->io_parameter[8])
1127 			fcport->supported_classes |= FC_COS_CLASS2;
1128 		if (logio->io_parameter[9] || logio->io_parameter[10])
1129 			fcport->supported_classes |= FC_COS_CLASS3;
1130 
1131 		goto done_post_logio_done_work;
1132 	}
1133 
1134 	iop[0] = le32_to_cpu(logio->io_parameter[0]);
1135 	iop[1] = le32_to_cpu(logio->io_parameter[1]);
1136 	switch (iop[0]) {
1137 	case LSC_SCODE_PORTID_USED:
1138 		data[0] = MBS_PORT_ID_USED;
1139 		data[1] = LSW(iop[1]);
1140 		break;
1141 	case LSC_SCODE_NPORT_USED:
1142 		data[0] = MBS_LOOP_ID_USED;
1143 		break;
1144 	case LSC_SCODE_CMD_FAILED:
1145 		if ((iop[1] & 0xff) == 0x05) {
1146 			data[0] = MBS_NOT_LOGGED_IN;
1147 			break;
1148 		}
1149 		/* Fall through. */
1150 	default:
1151 		data[0] = MBS_COMMAND_ERROR;
1152 		data[1] = lio->flags & SRB_LOGIN_RETRIED ?
1153 		    QLA_LOGIO_LOGIN_RETRIED: 0;
1154 		break;
1155 	}
1156 
1157 	DEBUG2(printk(KERN_WARNING
1158 	    "scsi(%ld:%x): Async-%s failed - comp=%x iop0=%x iop1=%x.\n",
1159 	    fcport->vha->host_no, sp->handle, type,
1160 	    le16_to_cpu(logio->comp_status),
1161 	    le32_to_cpu(logio->io_parameter[0]),
1162 	    le32_to_cpu(logio->io_parameter[1])));
1163 
1164 done_post_logio_done_work:
1165 	lio->ctx.type == SRB_LOGIN_CMD ?
1166 	    qla2x00_post_async_login_done_work(fcport->vha, fcport, data):
1167 	    qla2x00_post_async_logout_done_work(fcport->vha, fcport, data);
1168 
1169 	lio->ctx.free(sp);
1170 }
1171 
1172 /**
1173  * qla2x00_process_response_queue() - Process response queue entries.
1174  * @ha: SCSI driver HA context
1175  */
1176 void
1177 qla2x00_process_response_queue(struct rsp_que *rsp)
1178 {
1179 	struct scsi_qla_host *vha;
1180 	struct qla_hw_data *ha = rsp->hw;
1181 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1182 	sts_entry_t	*pkt;
1183 	uint16_t        handle_cnt;
1184 	uint16_t        cnt;
1185 
1186 	vha = pci_get_drvdata(ha->pdev);
1187 
1188 	if (!vha->flags.online)
1189 		return;
1190 
1191 	while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
1192 		pkt = (sts_entry_t *)rsp->ring_ptr;
1193 
1194 		rsp->ring_index++;
1195 		if (rsp->ring_index == rsp->length) {
1196 			rsp->ring_index = 0;
1197 			rsp->ring_ptr = rsp->ring;
1198 		} else {
1199 			rsp->ring_ptr++;
1200 		}
1201 
1202 		if (pkt->entry_status != 0) {
1203 			DEBUG3(printk(KERN_INFO
1204 			    "scsi(%ld): Process error entry.\n", vha->host_no));
1205 
1206 			qla2x00_error_entry(vha, rsp, pkt);
1207 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1208 			wmb();
1209 			continue;
1210 		}
1211 
1212 		switch (pkt->entry_type) {
1213 		case STATUS_TYPE:
1214 			qla2x00_status_entry(vha, rsp, pkt);
1215 			break;
1216 		case STATUS_TYPE_21:
1217 			handle_cnt = ((sts21_entry_t *)pkt)->handle_count;
1218 			for (cnt = 0; cnt < handle_cnt; cnt++) {
1219 				qla2x00_process_completed_request(vha, rsp->req,
1220 				    ((sts21_entry_t *)pkt)->handle[cnt]);
1221 			}
1222 			break;
1223 		case STATUS_TYPE_22:
1224 			handle_cnt = ((sts22_entry_t *)pkt)->handle_count;
1225 			for (cnt = 0; cnt < handle_cnt; cnt++) {
1226 				qla2x00_process_completed_request(vha, rsp->req,
1227 				    ((sts22_entry_t *)pkt)->handle[cnt]);
1228 			}
1229 			break;
1230 		case STATUS_CONT_TYPE:
1231 			qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
1232 			break;
1233 		case MBX_IOCB_TYPE:
1234 			qla2x00_mbx_iocb_entry(vha, rsp->req,
1235 			    (struct mbx_entry *)pkt);
1236 		default:
1237 			/* Type Not Supported. */
1238 			DEBUG4(printk(KERN_WARNING
1239 			    "scsi(%ld): Received unknown response pkt type %x "
1240 			    "entry status=%x.\n",
1241 			    vha->host_no, pkt->entry_type, pkt->entry_status));
1242 			break;
1243 		}
1244 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1245 		wmb();
1246 	}
1247 
1248 	/* Adjust ring index */
1249 	WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), rsp->ring_index);
1250 }
1251 
1252 static inline void
1253 qla2x00_handle_sense(srb_t *sp, uint8_t *sense_data, uint32_t sense_len,
1254 	struct rsp_que *rsp)
1255 {
1256 	struct scsi_cmnd *cp = sp->cmd;
1257 
1258 	if (sense_len >= SCSI_SENSE_BUFFERSIZE)
1259 		sense_len = SCSI_SENSE_BUFFERSIZE;
1260 
1261 	sp->request_sense_length = sense_len;
1262 	sp->request_sense_ptr = cp->sense_buffer;
1263 	if (sp->request_sense_length > 32)
1264 		sense_len = 32;
1265 
1266 	memcpy(cp->sense_buffer, sense_data, sense_len);
1267 
1268 	sp->request_sense_ptr += sense_len;
1269 	sp->request_sense_length -= sense_len;
1270 	if (sp->request_sense_length != 0)
1271 		rsp->status_srb = sp;
1272 
1273 	DEBUG5(printk("%s(): Check condition Sense data, scsi(%ld:%d:%d:%d) "
1274 	    "cmd=%p pid=%ld\n", __func__, sp->fcport->vha->host_no,
1275 	    cp->device->channel, cp->device->id, cp->device->lun, cp,
1276 	    cp->serial_number));
1277 	if (sense_len)
1278 		DEBUG5(qla2x00_dump_buffer(cp->sense_buffer, sense_len));
1279 }
1280 
1281 /**
1282  * qla2x00_status_entry() - Process a Status IOCB entry.
1283  * @ha: SCSI driver HA context
1284  * @pkt: Entry pointer
1285  */
1286 static void
1287 qla2x00_status_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, void *pkt)
1288 {
1289 	srb_t		*sp;
1290 	fc_port_t	*fcport;
1291 	struct scsi_cmnd *cp;
1292 	sts_entry_t *sts;
1293 	struct sts_entry_24xx *sts24;
1294 	uint16_t	comp_status;
1295 	uint16_t	scsi_status;
1296 	uint8_t		lscsi_status;
1297 	int32_t		resid;
1298 	uint32_t	sense_len, rsp_info_len, resid_len, fw_resid_len;
1299 	uint8_t		*rsp_info, *sense_data;
1300 	struct qla_hw_data *ha = vha->hw;
1301 	uint32_t handle;
1302 	uint16_t que;
1303 	struct req_que *req;
1304 
1305 	sts = (sts_entry_t *) pkt;
1306 	sts24 = (struct sts_entry_24xx *) pkt;
1307 	if (IS_FWI2_CAPABLE(ha)) {
1308 		comp_status = le16_to_cpu(sts24->comp_status);
1309 		scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
1310 	} else {
1311 		comp_status = le16_to_cpu(sts->comp_status);
1312 		scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
1313 	}
1314 	handle = (uint32_t) LSW(sts->handle);
1315 	que = MSW(sts->handle);
1316 	req = ha->req_q_map[que];
1317 	/* Fast path completion. */
1318 	if (comp_status == CS_COMPLETE && scsi_status == 0) {
1319 		qla2x00_process_completed_request(vha, req, handle);
1320 
1321 		return;
1322 	}
1323 
1324 	/* Validate handle. */
1325 	if (handle < MAX_OUTSTANDING_COMMANDS) {
1326 		sp = req->outstanding_cmds[handle];
1327 		req->outstanding_cmds[handle] = NULL;
1328 	} else
1329 		sp = NULL;
1330 
1331 	if (sp == NULL) {
1332 		DEBUG2(printk("scsi(%ld): Status Entry invalid handle.\n",
1333 		    vha->host_no));
1334 		qla_printk(KERN_WARNING, ha, "Status Entry invalid handle.\n");
1335 
1336 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1337 		qla2xxx_wake_dpc(vha);
1338 		return;
1339 	}
1340 	cp = sp->cmd;
1341 	if (cp == NULL) {
1342 		DEBUG2(printk("scsi(%ld): Command already returned back to OS "
1343 		    "pkt->handle=%d sp=%p.\n", vha->host_no, handle, sp));
1344 		qla_printk(KERN_WARNING, ha,
1345 		    "Command is NULL: already returned to OS (sp=%p)\n", sp);
1346 
1347 		return;
1348 	}
1349 
1350   	lscsi_status = scsi_status & STATUS_MASK;
1351 
1352 	fcport = sp->fcport;
1353 
1354 	sense_len = rsp_info_len = resid_len = fw_resid_len = 0;
1355 	if (IS_FWI2_CAPABLE(ha)) {
1356 		if (scsi_status & SS_SENSE_LEN_VALID)
1357 			sense_len = le32_to_cpu(sts24->sense_len);
1358 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
1359 			rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
1360 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER))
1361 			resid_len = le32_to_cpu(sts24->rsp_residual_count);
1362 		if (comp_status == CS_DATA_UNDERRUN)
1363 			fw_resid_len = le32_to_cpu(sts24->residual_len);
1364 		rsp_info = sts24->data;
1365 		sense_data = sts24->data;
1366 		host_to_fcp_swap(sts24->data, sizeof(sts24->data));
1367 	} else {
1368 		if (scsi_status & SS_SENSE_LEN_VALID)
1369 			sense_len = le16_to_cpu(sts->req_sense_length);
1370 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
1371 			rsp_info_len = le16_to_cpu(sts->rsp_info_len);
1372 		resid_len = le32_to_cpu(sts->residual_length);
1373 		rsp_info = sts->rsp_info;
1374 		sense_data = sts->req_sense_data;
1375 	}
1376 
1377 	/* Check for any FCP transport errors. */
1378 	if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
1379 		/* Sense data lies beyond any FCP RESPONSE data. */
1380 		if (IS_FWI2_CAPABLE(ha))
1381 			sense_data += rsp_info_len;
1382 		if (rsp_info_len > 3 && rsp_info[3]) {
1383 			DEBUG2(printk("scsi(%ld:%d:%d:%d) FCP I/O protocol "
1384 			    "failure (%x/%02x%02x%02x%02x%02x%02x%02x%02x)..."
1385 			    "retrying command\n", vha->host_no,
1386 			    cp->device->channel, cp->device->id,
1387 			    cp->device->lun, rsp_info_len, rsp_info[0],
1388 			    rsp_info[1], rsp_info[2], rsp_info[3], rsp_info[4],
1389 			    rsp_info[5], rsp_info[6], rsp_info[7]));
1390 
1391 			cp->result = DID_BUS_BUSY << 16;
1392 			qla2x00_sp_compl(ha, sp);
1393 			return;
1394 		}
1395 	}
1396 
1397 	/* Check for overrun. */
1398 	if (IS_FWI2_CAPABLE(ha) && comp_status == CS_COMPLETE &&
1399 	    scsi_status & SS_RESIDUAL_OVER)
1400 		comp_status = CS_DATA_OVERRUN;
1401 
1402 	/*
1403 	 * Based on Host and scsi status generate status code for Linux
1404 	 */
1405 	switch (comp_status) {
1406 	case CS_COMPLETE:
1407 	case CS_QUEUE_FULL:
1408 		if (scsi_status == 0) {
1409 			cp->result = DID_OK << 16;
1410 			break;
1411 		}
1412 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
1413 			resid = resid_len;
1414 			scsi_set_resid(cp, resid);
1415 
1416 			if (!lscsi_status &&
1417 			    ((unsigned)(scsi_bufflen(cp) - resid) <
1418 			     cp->underflow)) {
1419 				qla_printk(KERN_INFO, ha,
1420 					   "scsi(%ld:%d:%d:%d): Mid-layer underflow "
1421 					   "detected (%x of %x bytes)...returning "
1422 					   "error status.\n", vha->host_no,
1423 					   cp->device->channel, cp->device->id,
1424 					   cp->device->lun, resid,
1425 					   scsi_bufflen(cp));
1426 
1427 				cp->result = DID_ERROR << 16;
1428 				break;
1429 			}
1430 		}
1431 		cp->result = DID_OK << 16 | lscsi_status;
1432 
1433 		if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1434 			DEBUG2(printk(KERN_INFO
1435 			    "scsi(%ld): QUEUE FULL status detected "
1436 			    "0x%x-0x%x.\n", vha->host_no, comp_status,
1437 			    scsi_status));
1438 
1439 			/* Adjust queue depth for all luns on the port. */
1440 			if (!ql2xqfulltracking)
1441 				break;
1442 			fcport->last_queue_full = jiffies;
1443 			starget_for_each_device(cp->device->sdev_target,
1444 			    fcport, qla2x00_adjust_sdev_qdepth_down);
1445 			break;
1446 		}
1447 		if (lscsi_status != SS_CHECK_CONDITION)
1448 			break;
1449 
1450 		memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1451 		if (!(scsi_status & SS_SENSE_LEN_VALID))
1452 			break;
1453 
1454 		qla2x00_handle_sense(sp, sense_data, sense_len, rsp);
1455 		break;
1456 
1457 	case CS_DATA_UNDERRUN:
1458 		DEBUG2(printk(KERN_INFO
1459 		    "scsi(%ld:%d:%d) UNDERRUN status detected 0x%x-0x%x. "
1460 		    "resid=0x%x fw_resid=0x%x cdb=0x%x os_underflow=0x%x\n",
1461 		    vha->host_no, cp->device->id, cp->device->lun, comp_status,
1462 		    scsi_status, resid_len, fw_resid_len, cp->cmnd[0],
1463 		    cp->underflow));
1464 
1465 		/* Use F/W calculated residual length. */
1466 		resid = IS_FWI2_CAPABLE(ha) ? fw_resid_len : resid_len;
1467 		scsi_set_resid(cp, resid);
1468 		if (scsi_status & SS_RESIDUAL_UNDER) {
1469 			if (IS_FWI2_CAPABLE(ha) && fw_resid_len != resid_len) {
1470 				DEBUG2(printk(
1471 				    "scsi(%ld:%d:%d:%d) Dropped frame(s) "
1472 				    "detected (%x of %x bytes)...residual "
1473 				    "length mismatch...retrying command.\n",
1474 				    vha->host_no, cp->device->channel,
1475 				    cp->device->id, cp->device->lun, resid,
1476 				    scsi_bufflen(cp)));
1477 
1478 				cp->result = DID_ERROR << 16 | lscsi_status;
1479 				break;
1480 			}
1481 
1482 			if (!lscsi_status &&
1483 			    ((unsigned)(scsi_bufflen(cp) - resid) <
1484 			    cp->underflow)) {
1485 				qla_printk(KERN_INFO, ha,
1486 				    "scsi(%ld:%d:%d:%d): Mid-layer underflow "
1487 				    "detected (%x of %x bytes)...returning "
1488 				    "error status.\n", vha->host_no,
1489 				    cp->device->channel, cp->device->id,
1490 				    cp->device->lun, resid, scsi_bufflen(cp));
1491 
1492 				cp->result = DID_ERROR << 16;
1493 				break;
1494 			}
1495 		} else if (!lscsi_status) {
1496 			DEBUG2(printk(
1497 			    "scsi(%ld:%d:%d:%d) Dropped frame(s) detected "
1498 			    "(%x of %x bytes)...firmware reported underrun..."
1499 			    "retrying command.\n", vha->host_no,
1500 			    cp->device->channel, cp->device->id,
1501 			    cp->device->lun, resid, scsi_bufflen(cp)));
1502 
1503 			cp->result = DID_ERROR << 16;
1504 			break;
1505 		}
1506 
1507 		cp->result = DID_OK << 16 | lscsi_status;
1508 
1509 		/*
1510 		 * Check to see if SCSI Status is non zero. If so report SCSI
1511 		 * Status.
1512 		 */
1513 		if (lscsi_status != 0) {
1514 			if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1515 				DEBUG2(printk(KERN_INFO
1516 				    "scsi(%ld): QUEUE FULL status detected "
1517 				    "0x%x-0x%x.\n", vha->host_no, comp_status,
1518 				    scsi_status));
1519 
1520 				/*
1521 				 * Adjust queue depth for all luns on the
1522 				 * port.
1523 				 */
1524 				if (!ql2xqfulltracking)
1525 					break;
1526 				fcport->last_queue_full = jiffies;
1527 				starget_for_each_device(
1528 				    cp->device->sdev_target, fcport,
1529 				    qla2x00_adjust_sdev_qdepth_down);
1530 				break;
1531 			}
1532 			if (lscsi_status != SS_CHECK_CONDITION)
1533 				break;
1534 
1535 			memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1536 			if (!(scsi_status & SS_SENSE_LEN_VALID))
1537 				break;
1538 
1539 			qla2x00_handle_sense(sp, sense_data, sense_len, rsp);
1540 		}
1541 		break;
1542 
1543 	case CS_DATA_OVERRUN:
1544 		DEBUG2(printk(KERN_INFO
1545 		    "scsi(%ld:%d:%d): OVERRUN status detected 0x%x-0x%x\n",
1546 		    vha->host_no, cp->device->id, cp->device->lun, comp_status,
1547 		    scsi_status));
1548 		DEBUG2(printk(KERN_INFO
1549 		    "CDB: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1550 		    cp->cmnd[0], cp->cmnd[1], cp->cmnd[2], cp->cmnd[3],
1551 		    cp->cmnd[4], cp->cmnd[5]));
1552 		DEBUG2(printk(KERN_INFO
1553 		    "PID=0x%lx req=0x%x xtra=0x%x -- returning DID_ERROR "
1554 		    "status!\n",
1555 		    cp->serial_number, scsi_bufflen(cp), resid_len));
1556 
1557 		cp->result = DID_ERROR << 16;
1558 		break;
1559 
1560 	case CS_PORT_LOGGED_OUT:
1561 	case CS_PORT_CONFIG_CHG:
1562 	case CS_PORT_BUSY:
1563 	case CS_INCOMPLETE:
1564 	case CS_PORT_UNAVAILABLE:
1565 		/*
1566 		 * If the port is in Target Down state, return all IOs for this
1567 		 * Target with DID_NO_CONNECT ELSE Queue the IOs in the
1568 		 * retry_queue.
1569 		 */
1570 		DEBUG2(printk("scsi(%ld:%d:%d): status_entry: Port Down "
1571 		    "pid=%ld, compl status=0x%x, port state=0x%x\n",
1572 		    vha->host_no, cp->device->id, cp->device->lun,
1573 		    cp->serial_number, comp_status,
1574 		    atomic_read(&fcport->state)));
1575 
1576 		/*
1577 		 * We are going to have the fc class block the rport
1578 		 * while we try to recover so instruct the mid layer
1579 		 * to requeue until the class decides how to handle this.
1580 		 */
1581 		cp->result = DID_TRANSPORT_DISRUPTED << 16;
1582 		if (atomic_read(&fcport->state) == FCS_ONLINE)
1583 			qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
1584 		break;
1585 
1586 	case CS_RESET:
1587 		DEBUG2(printk(KERN_INFO
1588 		    "scsi(%ld): RESET status detected 0x%x-0x%x.\n",
1589 		    vha->host_no, comp_status, scsi_status));
1590 
1591 		cp->result = DID_RESET << 16;
1592 		break;
1593 
1594 	case CS_ABORTED:
1595 		/*
1596 		 * hv2.19.12 - DID_ABORT does not retry the request if we
1597 		 * aborted this request then abort otherwise it must be a
1598 		 * reset.
1599 		 */
1600 		DEBUG2(printk(KERN_INFO
1601 		    "scsi(%ld): ABORT status detected 0x%x-0x%x.\n",
1602 		    vha->host_no, comp_status, scsi_status));
1603 
1604 		cp->result = DID_RESET << 16;
1605 		break;
1606 
1607 	case CS_TIMEOUT:
1608 		/*
1609 		 * We are going to have the fc class block the rport
1610 		 * while we try to recover so instruct the mid layer
1611 		 * to requeue until the class decides how to handle this.
1612 		 */
1613 		cp->result = DID_TRANSPORT_DISRUPTED << 16;
1614 
1615 		if (IS_FWI2_CAPABLE(ha)) {
1616 			DEBUG2(printk(KERN_INFO
1617 			    "scsi(%ld:%d:%d:%d): TIMEOUT status detected "
1618 			    "0x%x-0x%x\n", vha->host_no, cp->device->channel,
1619 			    cp->device->id, cp->device->lun, comp_status,
1620 			    scsi_status));
1621 			break;
1622 		}
1623 		DEBUG2(printk(KERN_INFO
1624 		    "scsi(%ld:%d:%d:%d): TIMEOUT status detected 0x%x-0x%x "
1625 		    "sflags=%x.\n", vha->host_no, cp->device->channel,
1626 		    cp->device->id, cp->device->lun, comp_status, scsi_status,
1627 		    le16_to_cpu(sts->status_flags)));
1628 
1629 		/* Check to see if logout occurred. */
1630 		if ((le16_to_cpu(sts->status_flags) & SF_LOGOUT_SENT))
1631 			qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
1632 		break;
1633 
1634 	default:
1635 		DEBUG3(printk("scsi(%ld): Error detected (unknown status) "
1636 		    "0x%x-0x%x.\n", vha->host_no, comp_status, scsi_status));
1637 		qla_printk(KERN_INFO, ha,
1638 		    "Unknown status detected 0x%x-0x%x.\n",
1639 		    comp_status, scsi_status);
1640 
1641 		cp->result = DID_ERROR << 16;
1642 		break;
1643 	}
1644 
1645 	/* Place command on done queue. */
1646 	if (rsp->status_srb == NULL)
1647 		qla2x00_sp_compl(ha, sp);
1648 }
1649 
1650 /**
1651  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
1652  * @ha: SCSI driver HA context
1653  * @pkt: Entry pointer
1654  *
1655  * Extended sense data.
1656  */
1657 static void
1658 qla2x00_status_cont_entry(struct rsp_que *rsp, sts_cont_entry_t *pkt)
1659 {
1660 	uint8_t		sense_sz = 0;
1661 	struct qla_hw_data *ha = rsp->hw;
1662 	srb_t		*sp = rsp->status_srb;
1663 	struct scsi_cmnd *cp;
1664 
1665 	if (sp != NULL && sp->request_sense_length != 0) {
1666 		cp = sp->cmd;
1667 		if (cp == NULL) {
1668 			DEBUG2(printk("%s(): Cmd already returned back to OS "
1669 			    "sp=%p.\n", __func__, sp));
1670 			qla_printk(KERN_INFO, ha,
1671 			    "cmd is NULL: already returned to OS (sp=%p)\n",
1672 			    sp);
1673 
1674 			rsp->status_srb = NULL;
1675 			return;
1676 		}
1677 
1678 		if (sp->request_sense_length > sizeof(pkt->data)) {
1679 			sense_sz = sizeof(pkt->data);
1680 		} else {
1681 			sense_sz = sp->request_sense_length;
1682 		}
1683 
1684 		/* Move sense data. */
1685 		if (IS_FWI2_CAPABLE(ha))
1686 			host_to_fcp_swap(pkt->data, sizeof(pkt->data));
1687 		memcpy(sp->request_sense_ptr, pkt->data, sense_sz);
1688 		DEBUG5(qla2x00_dump_buffer(sp->request_sense_ptr, sense_sz));
1689 
1690 		sp->request_sense_ptr += sense_sz;
1691 		sp->request_sense_length -= sense_sz;
1692 
1693 		/* Place command on done queue. */
1694 		if (sp->request_sense_length == 0) {
1695 			rsp->status_srb = NULL;
1696 			qla2x00_sp_compl(ha, sp);
1697 		}
1698 	}
1699 }
1700 
1701 /**
1702  * qla2x00_error_entry() - Process an error entry.
1703  * @ha: SCSI driver HA context
1704  * @pkt: Entry pointer
1705  */
1706 static void
1707 qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
1708 {
1709 	srb_t *sp;
1710 	struct qla_hw_data *ha = vha->hw;
1711 	uint32_t handle = LSW(pkt->handle);
1712 	uint16_t que = MSW(pkt->handle);
1713 	struct req_que *req = ha->req_q_map[que];
1714 #if defined(QL_DEBUG_LEVEL_2)
1715 	if (pkt->entry_status & RF_INV_E_ORDER)
1716 		qla_printk(KERN_ERR, ha, "%s: Invalid Entry Order\n", __func__);
1717 	else if (pkt->entry_status & RF_INV_E_COUNT)
1718 		qla_printk(KERN_ERR, ha, "%s: Invalid Entry Count\n", __func__);
1719 	else if (pkt->entry_status & RF_INV_E_PARAM)
1720 		qla_printk(KERN_ERR, ha,
1721 		    "%s: Invalid Entry Parameter\n", __func__);
1722 	else if (pkt->entry_status & RF_INV_E_TYPE)
1723 		qla_printk(KERN_ERR, ha, "%s: Invalid Entry Type\n", __func__);
1724 	else if (pkt->entry_status & RF_BUSY)
1725 		qla_printk(KERN_ERR, ha, "%s: Busy\n", __func__);
1726 	else
1727 		qla_printk(KERN_ERR, ha, "%s: UNKNOWN flag error\n", __func__);
1728 #endif
1729 
1730 	/* Validate handle. */
1731 	if (handle < MAX_OUTSTANDING_COMMANDS)
1732 		sp = req->outstanding_cmds[handle];
1733 	else
1734 		sp = NULL;
1735 
1736 	if (sp) {
1737 		/* Free outstanding command slot. */
1738 		req->outstanding_cmds[handle] = NULL;
1739 
1740 		/* Bad payload or header */
1741 		if (pkt->entry_status &
1742 		    (RF_INV_E_ORDER | RF_INV_E_COUNT |
1743 		     RF_INV_E_PARAM | RF_INV_E_TYPE)) {
1744 			sp->cmd->result = DID_ERROR << 16;
1745 		} else if (pkt->entry_status & RF_BUSY) {
1746 			sp->cmd->result = DID_BUS_BUSY << 16;
1747 		} else {
1748 			sp->cmd->result = DID_ERROR << 16;
1749 		}
1750 		qla2x00_sp_compl(ha, sp);
1751 
1752 	} else if (pkt->entry_type == COMMAND_A64_TYPE || pkt->entry_type ==
1753 	    COMMAND_TYPE || pkt->entry_type == COMMAND_TYPE_7) {
1754 		DEBUG2(printk("scsi(%ld): Error entry - invalid handle\n",
1755 		    vha->host_no));
1756 		qla_printk(KERN_WARNING, ha,
1757 		    "Error entry - invalid handle\n");
1758 
1759 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1760 		qla2xxx_wake_dpc(vha);
1761 	}
1762 }
1763 
1764 /**
1765  * qla24xx_mbx_completion() - Process mailbox command completions.
1766  * @ha: SCSI driver HA context
1767  * @mb0: Mailbox0 register
1768  */
1769 static void
1770 qla24xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
1771 {
1772 	uint16_t	cnt;
1773 	uint16_t __iomem *wptr;
1774 	struct qla_hw_data *ha = vha->hw;
1775 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1776 
1777 	/* Load return mailbox registers. */
1778 	ha->flags.mbox_int = 1;
1779 	ha->mailbox_out[0] = mb0;
1780 	wptr = (uint16_t __iomem *)&reg->mailbox1;
1781 
1782 	for (cnt = 1; cnt < ha->mbx_count; cnt++) {
1783 		ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
1784 		wptr++;
1785 	}
1786 
1787 	if (ha->mcp) {
1788 		DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
1789 		    __func__, vha->host_no, ha->mcp->mb[0]));
1790 	} else {
1791 		DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
1792 		    __func__, vha->host_no));
1793 	}
1794 }
1795 
1796 /**
1797  * qla24xx_process_response_queue() - Process response queue entries.
1798  * @ha: SCSI driver HA context
1799  */
1800 void qla24xx_process_response_queue(struct scsi_qla_host *vha,
1801 	struct rsp_que *rsp)
1802 {
1803 	struct sts_entry_24xx *pkt;
1804 
1805 	if (!vha->flags.online)
1806 		return;
1807 
1808 	while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
1809 		pkt = (struct sts_entry_24xx *)rsp->ring_ptr;
1810 
1811 		rsp->ring_index++;
1812 		if (rsp->ring_index == rsp->length) {
1813 			rsp->ring_index = 0;
1814 			rsp->ring_ptr = rsp->ring;
1815 		} else {
1816 			rsp->ring_ptr++;
1817 		}
1818 
1819 		if (pkt->entry_status != 0) {
1820 			DEBUG3(printk(KERN_INFO
1821 			    "scsi(%ld): Process error entry.\n", vha->host_no));
1822 
1823 			qla2x00_error_entry(vha, rsp, (sts_entry_t *) pkt);
1824 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1825 			wmb();
1826 			continue;
1827 		}
1828 
1829 		switch (pkt->entry_type) {
1830 		case STATUS_TYPE:
1831 			qla2x00_status_entry(vha, rsp, pkt);
1832 			break;
1833 		case STATUS_CONT_TYPE:
1834 			qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
1835 			break;
1836 		case VP_RPT_ID_IOCB_TYPE:
1837 			qla24xx_report_id_acquisition(vha,
1838 			    (struct vp_rpt_id_entry_24xx *)pkt);
1839 			break;
1840 		case LOGINOUT_PORT_IOCB_TYPE:
1841 			qla24xx_logio_entry(vha, rsp->req,
1842 			    (struct logio_entry_24xx *)pkt);
1843 			break;
1844 		default:
1845 			/* Type Not Supported. */
1846 			DEBUG4(printk(KERN_WARNING
1847 			    "scsi(%ld): Received unknown response pkt type %x "
1848 			    "entry status=%x.\n",
1849 			    vha->host_no, pkt->entry_type, pkt->entry_status));
1850 			break;
1851 		}
1852 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1853 		wmb();
1854 	}
1855 
1856 	/* Adjust ring index */
1857 	WRT_REG_DWORD(rsp->rsp_q_out, rsp->ring_index);
1858 }
1859 
1860 static void
1861 qla2xxx_check_risc_status(scsi_qla_host_t *vha)
1862 {
1863 	int rval;
1864 	uint32_t cnt;
1865 	struct qla_hw_data *ha = vha->hw;
1866 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1867 
1868 	if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha))
1869 		return;
1870 
1871 	rval = QLA_SUCCESS;
1872 	WRT_REG_DWORD(&reg->iobase_addr, 0x7C00);
1873 	RD_REG_DWORD(&reg->iobase_addr);
1874 	WRT_REG_DWORD(&reg->iobase_window, 0x0001);
1875 	for (cnt = 10000; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
1876 	    rval == QLA_SUCCESS; cnt--) {
1877 		if (cnt) {
1878 			WRT_REG_DWORD(&reg->iobase_window, 0x0001);
1879 			udelay(10);
1880 		} else
1881 			rval = QLA_FUNCTION_TIMEOUT;
1882 	}
1883 	if (rval == QLA_SUCCESS)
1884 		goto next_test;
1885 
1886 	WRT_REG_DWORD(&reg->iobase_window, 0x0003);
1887 	for (cnt = 100; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
1888 	    rval == QLA_SUCCESS; cnt--) {
1889 		if (cnt) {
1890 			WRT_REG_DWORD(&reg->iobase_window, 0x0003);
1891 			udelay(10);
1892 		} else
1893 			rval = QLA_FUNCTION_TIMEOUT;
1894 	}
1895 	if (rval != QLA_SUCCESS)
1896 		goto done;
1897 
1898 next_test:
1899 	if (RD_REG_DWORD(&reg->iobase_c8) & BIT_3)
1900 		qla_printk(KERN_INFO, ha, "Additional code -- 0x55AA.\n");
1901 
1902 done:
1903 	WRT_REG_DWORD(&reg->iobase_window, 0x0000);
1904 	RD_REG_DWORD(&reg->iobase_window);
1905 }
1906 
1907 /**
1908  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
1909  * @irq:
1910  * @dev_id: SCSI driver HA context
1911  *
1912  * Called by system whenever the host adapter generates an interrupt.
1913  *
1914  * Returns handled flag.
1915  */
1916 irqreturn_t
1917 qla24xx_intr_handler(int irq, void *dev_id)
1918 {
1919 	scsi_qla_host_t	*vha;
1920 	struct qla_hw_data *ha;
1921 	struct device_reg_24xx __iomem *reg;
1922 	int		status;
1923 	unsigned long	iter;
1924 	uint32_t	stat;
1925 	uint32_t	hccr;
1926 	uint16_t	mb[4];
1927 	struct rsp_que *rsp;
1928 	unsigned long	flags;
1929 
1930 	rsp = (struct rsp_que *) dev_id;
1931 	if (!rsp) {
1932 		printk(KERN_INFO
1933 		    "%s(): NULL response queue pointer\n", __func__);
1934 		return IRQ_NONE;
1935 	}
1936 
1937 	ha = rsp->hw;
1938 	reg = &ha->iobase->isp24;
1939 	status = 0;
1940 
1941 	spin_lock_irqsave(&ha->hardware_lock, flags);
1942 	vha = pci_get_drvdata(ha->pdev);
1943 	for (iter = 50; iter--; ) {
1944 		stat = RD_REG_DWORD(&reg->host_status);
1945 		if (stat & HSRX_RISC_PAUSED) {
1946 			if (pci_channel_offline(ha->pdev))
1947 				break;
1948 
1949 			hccr = RD_REG_DWORD(&reg->hccr);
1950 
1951 			qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
1952 			    "Dumping firmware!\n", hccr);
1953 
1954 			qla2xxx_check_risc_status(vha);
1955 
1956 			ha->isp_ops->fw_dump(vha, 1);
1957 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1958 			break;
1959 		} else if ((stat & HSRX_RISC_INT) == 0)
1960 			break;
1961 
1962 		switch (stat & 0xff) {
1963 		case 0x1:
1964 		case 0x2:
1965 		case 0x10:
1966 		case 0x11:
1967 			qla24xx_mbx_completion(vha, MSW(stat));
1968 			status |= MBX_INTERRUPT;
1969 
1970 			break;
1971 		case 0x12:
1972 			mb[0] = MSW(stat);
1973 			mb[1] = RD_REG_WORD(&reg->mailbox1);
1974 			mb[2] = RD_REG_WORD(&reg->mailbox2);
1975 			mb[3] = RD_REG_WORD(&reg->mailbox3);
1976 			qla2x00_async_event(vha, rsp, mb);
1977 			break;
1978 		case 0x13:
1979 		case 0x14:
1980 			qla24xx_process_response_queue(vha, rsp);
1981 			break;
1982 		default:
1983 			DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
1984 			    "(%d).\n",
1985 			    vha->host_no, stat & 0xff));
1986 			break;
1987 		}
1988 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1989 		RD_REG_DWORD_RELAXED(&reg->hccr);
1990 	}
1991 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1992 
1993 	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
1994 	    (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
1995 		set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
1996 		complete(&ha->mbx_intr_comp);
1997 	}
1998 
1999 	return IRQ_HANDLED;
2000 }
2001 
2002 static irqreturn_t
2003 qla24xx_msix_rsp_q(int irq, void *dev_id)
2004 {
2005 	struct qla_hw_data *ha;
2006 	struct rsp_que *rsp;
2007 	struct device_reg_24xx __iomem *reg;
2008 	struct scsi_qla_host *vha;
2009 
2010 	rsp = (struct rsp_que *) dev_id;
2011 	if (!rsp) {
2012 		printk(KERN_INFO
2013 		"%s(): NULL response queue pointer\n", __func__);
2014 		return IRQ_NONE;
2015 	}
2016 	ha = rsp->hw;
2017 	reg = &ha->iobase->isp24;
2018 
2019 	spin_lock_irq(&ha->hardware_lock);
2020 
2021 	vha = qla25xx_get_host(rsp);
2022 	qla24xx_process_response_queue(vha, rsp);
2023 	if (!ha->mqenable) {
2024 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
2025 		RD_REG_DWORD_RELAXED(&reg->hccr);
2026 	}
2027 	spin_unlock_irq(&ha->hardware_lock);
2028 
2029 	return IRQ_HANDLED;
2030 }
2031 
2032 static irqreturn_t
2033 qla25xx_msix_rsp_q(int irq, void *dev_id)
2034 {
2035 	struct qla_hw_data *ha;
2036 	struct rsp_que *rsp;
2037 
2038 	rsp = (struct rsp_que *) dev_id;
2039 	if (!rsp) {
2040 		printk(KERN_INFO
2041 			"%s(): NULL response queue pointer\n", __func__);
2042 		return IRQ_NONE;
2043 	}
2044 	ha = rsp->hw;
2045 
2046 	queue_work_on((int) (rsp->id - 1), ha->wq, &rsp->q_work);
2047 
2048 	return IRQ_HANDLED;
2049 }
2050 
2051 static irqreturn_t
2052 qla24xx_msix_default(int irq, void *dev_id)
2053 {
2054 	scsi_qla_host_t	*vha;
2055 	struct qla_hw_data *ha;
2056 	struct rsp_que *rsp;
2057 	struct device_reg_24xx __iomem *reg;
2058 	int		status;
2059 	uint32_t	stat;
2060 	uint32_t	hccr;
2061 	uint16_t	mb[4];
2062 
2063 	rsp = (struct rsp_que *) dev_id;
2064 	if (!rsp) {
2065 		DEBUG(printk(
2066 		"%s(): NULL response queue pointer\n", __func__));
2067 		return IRQ_NONE;
2068 	}
2069 	ha = rsp->hw;
2070 	reg = &ha->iobase->isp24;
2071 	status = 0;
2072 
2073 	spin_lock_irq(&ha->hardware_lock);
2074 	vha = pci_get_drvdata(ha->pdev);
2075 	do {
2076 		stat = RD_REG_DWORD(&reg->host_status);
2077 		if (stat & HSRX_RISC_PAUSED) {
2078 			if (pci_channel_offline(ha->pdev))
2079 				break;
2080 
2081 			hccr = RD_REG_DWORD(&reg->hccr);
2082 
2083 			qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
2084 			    "Dumping firmware!\n", hccr);
2085 
2086 			qla2xxx_check_risc_status(vha);
2087 
2088 			ha->isp_ops->fw_dump(vha, 1);
2089 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2090 			break;
2091 		} else if ((stat & HSRX_RISC_INT) == 0)
2092 			break;
2093 
2094 		switch (stat & 0xff) {
2095 		case 0x1:
2096 		case 0x2:
2097 		case 0x10:
2098 		case 0x11:
2099 			qla24xx_mbx_completion(vha, MSW(stat));
2100 			status |= MBX_INTERRUPT;
2101 
2102 			break;
2103 		case 0x12:
2104 			mb[0] = MSW(stat);
2105 			mb[1] = RD_REG_WORD(&reg->mailbox1);
2106 			mb[2] = RD_REG_WORD(&reg->mailbox2);
2107 			mb[3] = RD_REG_WORD(&reg->mailbox3);
2108 			qla2x00_async_event(vha, rsp, mb);
2109 			break;
2110 		case 0x13:
2111 		case 0x14:
2112 			qla24xx_process_response_queue(vha, rsp);
2113 			break;
2114 		default:
2115 			DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
2116 			    "(%d).\n",
2117 			    vha->host_no, stat & 0xff));
2118 			break;
2119 		}
2120 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
2121 	} while (0);
2122 	spin_unlock_irq(&ha->hardware_lock);
2123 
2124 	if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
2125 	    (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
2126 		set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
2127 		complete(&ha->mbx_intr_comp);
2128 	}
2129 
2130 	return IRQ_HANDLED;
2131 }
2132 
2133 /* Interrupt handling helpers. */
2134 
2135 struct qla_init_msix_entry {
2136 	const char *name;
2137 	irq_handler_t handler;
2138 };
2139 
2140 static struct qla_init_msix_entry msix_entries[3] = {
2141 	{ "qla2xxx (default)", qla24xx_msix_default },
2142 	{ "qla2xxx (rsp_q)", qla24xx_msix_rsp_q },
2143 	{ "qla2xxx (multiq)", qla25xx_msix_rsp_q },
2144 };
2145 
2146 static void
2147 qla24xx_disable_msix(struct qla_hw_data *ha)
2148 {
2149 	int i;
2150 	struct qla_msix_entry *qentry;
2151 
2152 	for (i = 0; i < ha->msix_count; i++) {
2153 		qentry = &ha->msix_entries[i];
2154 		if (qentry->have_irq)
2155 			free_irq(qentry->vector, qentry->rsp);
2156 	}
2157 	pci_disable_msix(ha->pdev);
2158 	kfree(ha->msix_entries);
2159 	ha->msix_entries = NULL;
2160 	ha->flags.msix_enabled = 0;
2161 }
2162 
2163 static int
2164 qla24xx_enable_msix(struct qla_hw_data *ha, struct rsp_que *rsp)
2165 {
2166 #define MIN_MSIX_COUNT	2
2167 	int i, ret;
2168 	struct msix_entry *entries;
2169 	struct qla_msix_entry *qentry;
2170 
2171 	entries = kzalloc(sizeof(struct msix_entry) * ha->msix_count,
2172 					GFP_KERNEL);
2173 	if (!entries)
2174 		return -ENOMEM;
2175 
2176 	for (i = 0; i < ha->msix_count; i++)
2177 		entries[i].entry = i;
2178 
2179 	ret = pci_enable_msix(ha->pdev, entries, ha->msix_count);
2180 	if (ret) {
2181 		if (ret < MIN_MSIX_COUNT)
2182 			goto msix_failed;
2183 
2184 		qla_printk(KERN_WARNING, ha,
2185 			"MSI-X: Failed to enable support -- %d/%d\n"
2186 			" Retry with %d vectors\n", ha->msix_count, ret, ret);
2187 		ha->msix_count = ret;
2188 		ret = pci_enable_msix(ha->pdev, entries, ha->msix_count);
2189 		if (ret) {
2190 msix_failed:
2191 			qla_printk(KERN_WARNING, ha, "MSI-X: Failed to enable"
2192 				" support, giving up -- %d/%d\n",
2193 				ha->msix_count, ret);
2194 			goto msix_out;
2195 		}
2196 		ha->max_rsp_queues = ha->msix_count - 1;
2197 	}
2198 	ha->msix_entries = kzalloc(sizeof(struct qla_msix_entry) *
2199 				ha->msix_count, GFP_KERNEL);
2200 	if (!ha->msix_entries) {
2201 		ret = -ENOMEM;
2202 		goto msix_out;
2203 	}
2204 	ha->flags.msix_enabled = 1;
2205 
2206 	for (i = 0; i < ha->msix_count; i++) {
2207 		qentry = &ha->msix_entries[i];
2208 		qentry->vector = entries[i].vector;
2209 		qentry->entry = entries[i].entry;
2210 		qentry->have_irq = 0;
2211 		qentry->rsp = NULL;
2212 	}
2213 
2214 	/* Enable MSI-X vectors for the base queue */
2215 	for (i = 0; i < 2; i++) {
2216 		qentry = &ha->msix_entries[i];
2217 		ret = request_irq(qentry->vector, msix_entries[i].handler,
2218 					0, msix_entries[i].name, rsp);
2219 		if (ret) {
2220 			qla_printk(KERN_WARNING, ha,
2221 			"MSI-X: Unable to register handler -- %x/%d.\n",
2222 			qentry->vector, ret);
2223 			qla24xx_disable_msix(ha);
2224 			ha->mqenable = 0;
2225 			goto msix_out;
2226 		}
2227 		qentry->have_irq = 1;
2228 		qentry->rsp = rsp;
2229 		rsp->msix = qentry;
2230 	}
2231 
2232 	/* Enable MSI-X vector for response queue update for queue 0 */
2233 	if (ha->mqiobase &&  (ha->max_rsp_queues > 1 || ha->max_req_queues > 1))
2234 		ha->mqenable = 1;
2235 
2236 msix_out:
2237 	kfree(entries);
2238 	return ret;
2239 }
2240 
2241 int
2242 qla2x00_request_irqs(struct qla_hw_data *ha, struct rsp_que *rsp)
2243 {
2244 	int ret;
2245 	device_reg_t __iomem *reg = ha->iobase;
2246 
2247 	/* If possible, enable MSI-X. */
2248 	if (!IS_QLA2432(ha) && !IS_QLA2532(ha) &&
2249 	    !IS_QLA8432(ha) && !IS_QLA8001(ha))
2250 		goto skip_msix;
2251 
2252 	if (IS_QLA2432(ha) && (ha->pdev->revision < QLA_MSIX_CHIP_REV_24XX ||
2253 		!QLA_MSIX_FW_MODE_1(ha->fw_attributes))) {
2254 		DEBUG2(qla_printk(KERN_WARNING, ha,
2255 		"MSI-X: Unsupported ISP2432 (0x%X, 0x%X).\n",
2256 			ha->pdev->revision, ha->fw_attributes));
2257 
2258 		goto skip_msix;
2259 	}
2260 
2261 	if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_HP &&
2262 	    (ha->pdev->subsystem_device == 0x7040 ||
2263 		ha->pdev->subsystem_device == 0x7041 ||
2264 		ha->pdev->subsystem_device == 0x1705)) {
2265 		DEBUG2(qla_printk(KERN_WARNING, ha,
2266 		    "MSI-X: Unsupported ISP2432 SSVID/SSDID (0x%X, 0x%X).\n",
2267 		    ha->pdev->subsystem_vendor,
2268 		    ha->pdev->subsystem_device));
2269 
2270 		goto skip_msi;
2271 	}
2272 
2273 	ret = qla24xx_enable_msix(ha, rsp);
2274 	if (!ret) {
2275 		DEBUG2(qla_printk(KERN_INFO, ha,
2276 		    "MSI-X: Enabled (0x%X, 0x%X).\n", ha->chip_revision,
2277 		    ha->fw_attributes));
2278 		goto clear_risc_ints;
2279 	}
2280 	qla_printk(KERN_WARNING, ha,
2281 	    "MSI-X: Falling back-to INTa mode -- %d.\n", ret);
2282 skip_msix:
2283 
2284 	if (!IS_QLA24XX(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha) &&
2285 	    !IS_QLA8001(ha))
2286 		goto skip_msi;
2287 
2288 	ret = pci_enable_msi(ha->pdev);
2289 	if (!ret) {
2290 		DEBUG2(qla_printk(KERN_INFO, ha, "MSI: Enabled.\n"));
2291 		ha->flags.msi_enabled = 1;
2292 	}
2293 skip_msi:
2294 
2295 	ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
2296 	    IRQF_SHARED, QLA2XXX_DRIVER_NAME, rsp);
2297 	if (ret) {
2298 		qla_printk(KERN_WARNING, ha,
2299 		    "Failed to reserve interrupt %d already in use.\n",
2300 		    ha->pdev->irq);
2301 		goto fail;
2302 	}
2303 	ha->flags.inta_enabled = 1;
2304 clear_risc_ints:
2305 
2306 	/*
2307 	 * FIXME: Noted that 8014s were being dropped during NK testing.
2308 	 * Timing deltas during MSI-X/INTa transitions?
2309 	 */
2310 	if (IS_QLA81XX(ha))
2311 		goto fail;
2312 	spin_lock_irq(&ha->hardware_lock);
2313 	if (IS_FWI2_CAPABLE(ha)) {
2314 		WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
2315 		WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
2316 	} else {
2317 		WRT_REG_WORD(&reg->isp.semaphore, 0);
2318 		WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
2319 		WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
2320 	}
2321 	spin_unlock_irq(&ha->hardware_lock);
2322 
2323 fail:
2324 	return ret;
2325 }
2326 
2327 void
2328 qla2x00_free_irqs(scsi_qla_host_t *vha)
2329 {
2330 	struct qla_hw_data *ha = vha->hw;
2331 	struct rsp_que *rsp = ha->rsp_q_map[0];
2332 
2333 	if (ha->flags.msix_enabled)
2334 		qla24xx_disable_msix(ha);
2335 	else if (ha->flags.inta_enabled) {
2336 		free_irq(ha->pdev->irq, rsp);
2337 		pci_disable_msi(ha->pdev);
2338 	}
2339 }
2340 
2341 
2342 int qla25xx_request_irq(struct rsp_que *rsp)
2343 {
2344 	struct qla_hw_data *ha = rsp->hw;
2345 	struct qla_init_msix_entry *intr = &msix_entries[2];
2346 	struct qla_msix_entry *msix = rsp->msix;
2347 	int ret;
2348 
2349 	ret = request_irq(msix->vector, intr->handler, 0, intr->name, rsp);
2350 	if (ret) {
2351 		qla_printk(KERN_WARNING, ha,
2352 			"MSI-X: Unable to register handler -- %x/%d.\n",
2353 			msix->vector, ret);
2354 		return ret;
2355 	}
2356 	msix->have_irq = 1;
2357 	msix->rsp = rsp;
2358 	return ret;
2359 }
2360 
2361 struct scsi_qla_host *
2362 qla25xx_get_host(struct rsp_que *rsp)
2363 {
2364 	srb_t *sp;
2365 	struct qla_hw_data *ha = rsp->hw;
2366 	struct scsi_qla_host *vha = NULL;
2367 	struct sts_entry_24xx *pkt;
2368 	struct req_que *req;
2369 	uint16_t que;
2370 	uint32_t handle;
2371 
2372 	pkt = (struct sts_entry_24xx *) rsp->ring_ptr;
2373 	que = MSW(pkt->handle);
2374 	handle = (uint32_t) LSW(pkt->handle);
2375 	req = ha->req_q_map[que];
2376 	if (handle < MAX_OUTSTANDING_COMMANDS) {
2377 		sp = req->outstanding_cmds[handle];
2378 		if (sp)
2379 			return  sp->fcport->vha;
2380 		else
2381 			goto base_que;
2382 	}
2383 base_que:
2384 	vha = pci_get_drvdata(ha->pdev);
2385 	return vha;
2386 }
2387