xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_isr.c (revision 8795a739)
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 #include "qla_target.h"
9 
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/cpu.h>
13 #include <linux/t10-pi.h>
14 #include <scsi/scsi_tcq.h>
15 #include <scsi/scsi_bsg_fc.h>
16 #include <scsi/scsi_eh.h>
17 #include <scsi/fc/fc_fs.h>
18 #include <linux/nvme-fc-driver.h>
19 
20 static void qla2x00_mbx_completion(scsi_qla_host_t *, uint16_t);
21 static void qla2x00_status_entry(scsi_qla_host_t *, struct rsp_que *, void *);
22 static void qla2x00_status_cont_entry(struct rsp_que *, sts_cont_entry_t *);
23 static int qla2x00_error_entry(scsi_qla_host_t *, struct rsp_que *,
24 	sts_entry_t *);
25 
26 const char *const port_state_str[] = {
27 	"Unknown",
28 	"UNCONFIGURED",
29 	"DEAD",
30 	"LOST",
31 	"ONLINE"
32 };
33 
34 /**
35  * qla2100_intr_handler() - Process interrupts for the ISP2100 and ISP2200.
36  * @irq: interrupt number
37  * @dev_id: SCSI driver HA context
38  *
39  * Called by system whenever the host adapter generates an interrupt.
40  *
41  * Returns handled flag.
42  */
43 irqreturn_t
44 qla2100_intr_handler(int irq, void *dev_id)
45 {
46 	scsi_qla_host_t	*vha;
47 	struct qla_hw_data *ha;
48 	struct device_reg_2xxx __iomem *reg;
49 	int		status;
50 	unsigned long	iter;
51 	uint16_t	hccr;
52 	uint16_t	mb[8];
53 	struct rsp_que *rsp;
54 	unsigned long	flags;
55 
56 	rsp = (struct rsp_que *) dev_id;
57 	if (!rsp) {
58 		ql_log(ql_log_info, NULL, 0x505d,
59 		    "%s: NULL response queue pointer.\n", __func__);
60 		return (IRQ_NONE);
61 	}
62 
63 	ha = rsp->hw;
64 	reg = &ha->iobase->isp;
65 	status = 0;
66 
67 	spin_lock_irqsave(&ha->hardware_lock, flags);
68 	vha = pci_get_drvdata(ha->pdev);
69 	for (iter = 50; iter--; ) {
70 		hccr = RD_REG_WORD(&reg->hccr);
71 		if (qla2x00_check_reg16_for_disconnect(vha, hccr))
72 			break;
73 		if (hccr & HCCR_RISC_PAUSE) {
74 			if (pci_channel_offline(ha->pdev))
75 				break;
76 
77 			/*
78 			 * Issue a "HARD" reset in order for the RISC interrupt
79 			 * bit to be cleared.  Schedule a big hammer to get
80 			 * out of the RISC PAUSED state.
81 			 */
82 			WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
83 			RD_REG_WORD(&reg->hccr);
84 
85 			ha->isp_ops->fw_dump(vha, 1);
86 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
87 			break;
88 		} else if ((RD_REG_WORD(&reg->istatus) & ISR_RISC_INT) == 0)
89 			break;
90 
91 		if (RD_REG_WORD(&reg->semaphore) & BIT_0) {
92 			WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
93 			RD_REG_WORD(&reg->hccr);
94 
95 			/* Get mailbox data. */
96 			mb[0] = RD_MAILBOX_REG(ha, reg, 0);
97 			if (mb[0] > 0x3fff && mb[0] < 0x8000) {
98 				qla2x00_mbx_completion(vha, mb[0]);
99 				status |= MBX_INTERRUPT;
100 			} else if (mb[0] > 0x7fff && mb[0] < 0xc000) {
101 				mb[1] = RD_MAILBOX_REG(ha, reg, 1);
102 				mb[2] = RD_MAILBOX_REG(ha, reg, 2);
103 				mb[3] = RD_MAILBOX_REG(ha, reg, 3);
104 				qla2x00_async_event(vha, rsp, mb);
105 			} else {
106 				/*EMPTY*/
107 				ql_dbg(ql_dbg_async, vha, 0x5025,
108 				    "Unrecognized interrupt type (%d).\n",
109 				    mb[0]);
110 			}
111 			/* Release mailbox registers. */
112 			WRT_REG_WORD(&reg->semaphore, 0);
113 			RD_REG_WORD(&reg->semaphore);
114 		} else {
115 			qla2x00_process_response_queue(rsp);
116 
117 			WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
118 			RD_REG_WORD(&reg->hccr);
119 		}
120 	}
121 	qla2x00_handle_mbx_completion(ha, status);
122 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
123 
124 	return (IRQ_HANDLED);
125 }
126 
127 bool
128 qla2x00_check_reg32_for_disconnect(scsi_qla_host_t *vha, uint32_t reg)
129 {
130 	/* Check for PCI disconnection */
131 	if (reg == 0xffffffff && !pci_channel_offline(vha->hw->pdev)) {
132 		if (!test_and_set_bit(PFLG_DISCONNECTED, &vha->pci_flags) &&
133 		    !test_bit(PFLG_DRIVER_REMOVING, &vha->pci_flags) &&
134 		    !test_bit(PFLG_DRIVER_PROBING, &vha->pci_flags)) {
135 			/*
136 			 * Schedule this (only once) on the default system
137 			 * workqueue so that all the adapter workqueues and the
138 			 * DPC thread can be shutdown cleanly.
139 			 */
140 			schedule_work(&vha->hw->board_disable);
141 		}
142 		return true;
143 	} else
144 		return false;
145 }
146 
147 bool
148 qla2x00_check_reg16_for_disconnect(scsi_qla_host_t *vha, uint16_t reg)
149 {
150 	return qla2x00_check_reg32_for_disconnect(vha, 0xffff0000 | reg);
151 }
152 
153 /**
154  * qla2300_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
155  * @irq: interrupt number
156  * @dev_id: SCSI driver HA context
157  *
158  * Called by system whenever the host adapter generates an interrupt.
159  *
160  * Returns handled flag.
161  */
162 irqreturn_t
163 qla2300_intr_handler(int irq, void *dev_id)
164 {
165 	scsi_qla_host_t	*vha;
166 	struct device_reg_2xxx __iomem *reg;
167 	int		status;
168 	unsigned long	iter;
169 	uint32_t	stat;
170 	uint16_t	hccr;
171 	uint16_t	mb[8];
172 	struct rsp_que *rsp;
173 	struct qla_hw_data *ha;
174 	unsigned long	flags;
175 
176 	rsp = (struct rsp_que *) dev_id;
177 	if (!rsp) {
178 		ql_log(ql_log_info, NULL, 0x5058,
179 		    "%s: NULL response queue pointer.\n", __func__);
180 		return (IRQ_NONE);
181 	}
182 
183 	ha = rsp->hw;
184 	reg = &ha->iobase->isp;
185 	status = 0;
186 
187 	spin_lock_irqsave(&ha->hardware_lock, flags);
188 	vha = pci_get_drvdata(ha->pdev);
189 	for (iter = 50; iter--; ) {
190 		stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
191 		if (qla2x00_check_reg32_for_disconnect(vha, stat))
192 			break;
193 		if (stat & HSR_RISC_PAUSED) {
194 			if (unlikely(pci_channel_offline(ha->pdev)))
195 				break;
196 
197 			hccr = RD_REG_WORD(&reg->hccr);
198 
199 			if (hccr & (BIT_15 | BIT_13 | BIT_11 | BIT_8))
200 				ql_log(ql_log_warn, vha, 0x5026,
201 				    "Parity error -- HCCR=%x, Dumping "
202 				    "firmware.\n", hccr);
203 			else
204 				ql_log(ql_log_warn, vha, 0x5027,
205 				    "RISC paused -- HCCR=%x, Dumping "
206 				    "firmware.\n", hccr);
207 
208 			/*
209 			 * Issue a "HARD" reset in order for the RISC
210 			 * interrupt bit to be cleared.  Schedule a big
211 			 * hammer to get out of the RISC PAUSED state.
212 			 */
213 			WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
214 			RD_REG_WORD(&reg->hccr);
215 
216 			ha->isp_ops->fw_dump(vha, 1);
217 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
218 			break;
219 		} else if ((stat & HSR_RISC_INT) == 0)
220 			break;
221 
222 		switch (stat & 0xff) {
223 		case 0x1:
224 		case 0x2:
225 		case 0x10:
226 		case 0x11:
227 			qla2x00_mbx_completion(vha, MSW(stat));
228 			status |= MBX_INTERRUPT;
229 
230 			/* Release mailbox registers. */
231 			WRT_REG_WORD(&reg->semaphore, 0);
232 			break;
233 		case 0x12:
234 			mb[0] = MSW(stat);
235 			mb[1] = RD_MAILBOX_REG(ha, reg, 1);
236 			mb[2] = RD_MAILBOX_REG(ha, reg, 2);
237 			mb[3] = RD_MAILBOX_REG(ha, reg, 3);
238 			qla2x00_async_event(vha, rsp, mb);
239 			break;
240 		case 0x13:
241 			qla2x00_process_response_queue(rsp);
242 			break;
243 		case 0x15:
244 			mb[0] = MBA_CMPLT_1_16BIT;
245 			mb[1] = MSW(stat);
246 			qla2x00_async_event(vha, rsp, mb);
247 			break;
248 		case 0x16:
249 			mb[0] = MBA_SCSI_COMPLETION;
250 			mb[1] = MSW(stat);
251 			mb[2] = RD_MAILBOX_REG(ha, reg, 2);
252 			qla2x00_async_event(vha, rsp, mb);
253 			break;
254 		default:
255 			ql_dbg(ql_dbg_async, vha, 0x5028,
256 			    "Unrecognized interrupt type (%d).\n", stat & 0xff);
257 			break;
258 		}
259 		WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
260 		RD_REG_WORD_RELAXED(&reg->hccr);
261 	}
262 	qla2x00_handle_mbx_completion(ha, status);
263 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
264 
265 	return (IRQ_HANDLED);
266 }
267 
268 /**
269  * qla2x00_mbx_completion() - Process mailbox command completions.
270  * @vha: SCSI driver HA context
271  * @mb0: Mailbox0 register
272  */
273 static void
274 qla2x00_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
275 {
276 	uint16_t	cnt;
277 	uint32_t	mboxes;
278 	uint16_t __iomem *wptr;
279 	struct qla_hw_data *ha = vha->hw;
280 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
281 
282 	/* Read all mbox registers? */
283 	WARN_ON_ONCE(ha->mbx_count > 32);
284 	mboxes = (1ULL << ha->mbx_count) - 1;
285 	if (!ha->mcp)
286 		ql_dbg(ql_dbg_async, vha, 0x5001, "MBX pointer ERROR.\n");
287 	else
288 		mboxes = ha->mcp->in_mb;
289 
290 	/* Load return mailbox registers. */
291 	ha->flags.mbox_int = 1;
292 	ha->mailbox_out[0] = mb0;
293 	mboxes >>= 1;
294 	wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 1);
295 
296 	for (cnt = 1; cnt < ha->mbx_count; cnt++) {
297 		if (IS_QLA2200(ha) && cnt == 8)
298 			wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 8);
299 		if ((cnt == 4 || cnt == 5) && (mboxes & BIT_0))
300 			ha->mailbox_out[cnt] = qla2x00_debounce_register(wptr);
301 		else if (mboxes & BIT_0)
302 			ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
303 
304 		wptr++;
305 		mboxes >>= 1;
306 	}
307 }
308 
309 static void
310 qla81xx_idc_event(scsi_qla_host_t *vha, uint16_t aen, uint16_t descr)
311 {
312 	static char *event[] =
313 		{ "Complete", "Request Notification", "Time Extension" };
314 	int rval;
315 	struct device_reg_24xx __iomem *reg24 = &vha->hw->iobase->isp24;
316 	struct device_reg_82xx __iomem *reg82 = &vha->hw->iobase->isp82;
317 	uint16_t __iomem *wptr;
318 	uint16_t cnt, timeout, mb[QLA_IDC_ACK_REGS];
319 
320 	/* Seed data -- mailbox1 -> mailbox7. */
321 	if (IS_QLA81XX(vha->hw) || IS_QLA83XX(vha->hw))
322 		wptr = (uint16_t __iomem *)&reg24->mailbox1;
323 	else if (IS_QLA8044(vha->hw))
324 		wptr = (uint16_t __iomem *)&reg82->mailbox_out[1];
325 	else
326 		return;
327 
328 	for (cnt = 0; cnt < QLA_IDC_ACK_REGS; cnt++, wptr++)
329 		mb[cnt] = RD_REG_WORD(wptr);
330 
331 	ql_dbg(ql_dbg_async, vha, 0x5021,
332 	    "Inter-Driver Communication %s -- "
333 	    "%04x %04x %04x %04x %04x %04x %04x.\n",
334 	    event[aen & 0xff], mb[0], mb[1], mb[2], mb[3],
335 	    mb[4], mb[5], mb[6]);
336 	switch (aen) {
337 	/* Handle IDC Error completion case. */
338 	case MBA_IDC_COMPLETE:
339 		if (mb[1] >> 15) {
340 			vha->hw->flags.idc_compl_status = 1;
341 			if (vha->hw->notify_dcbx_comp && !vha->vp_idx)
342 				complete(&vha->hw->dcbx_comp);
343 		}
344 		break;
345 
346 	case MBA_IDC_NOTIFY:
347 		/* Acknowledgement needed? [Notify && non-zero timeout]. */
348 		timeout = (descr >> 8) & 0xf;
349 		ql_dbg(ql_dbg_async, vha, 0x5022,
350 		    "%lu Inter-Driver Communication %s -- ACK timeout=%d.\n",
351 		    vha->host_no, event[aen & 0xff], timeout);
352 
353 		if (!timeout)
354 			return;
355 		rval = qla2x00_post_idc_ack_work(vha, mb);
356 		if (rval != QLA_SUCCESS)
357 			ql_log(ql_log_warn, vha, 0x5023,
358 			    "IDC failed to post ACK.\n");
359 		break;
360 	case MBA_IDC_TIME_EXT:
361 		vha->hw->idc_extend_tmo = descr;
362 		ql_dbg(ql_dbg_async, vha, 0x5087,
363 		    "%lu Inter-Driver Communication %s -- "
364 		    "Extend timeout by=%d.\n",
365 		    vha->host_no, event[aen & 0xff], vha->hw->idc_extend_tmo);
366 		break;
367 	}
368 }
369 
370 #define LS_UNKNOWN	2
371 const char *
372 qla2x00_get_link_speed_str(struct qla_hw_data *ha, uint16_t speed)
373 {
374 	static const char *const link_speeds[] = {
375 		"1", "2", "?", "4", "8", "16", "32", "10"
376 	};
377 #define	QLA_LAST_SPEED (ARRAY_SIZE(link_speeds) - 1)
378 
379 	if (IS_QLA2100(ha) || IS_QLA2200(ha))
380 		return link_speeds[0];
381 	else if (speed == 0x13)
382 		return link_speeds[QLA_LAST_SPEED];
383 	else if (speed < QLA_LAST_SPEED)
384 		return link_speeds[speed];
385 	else
386 		return link_speeds[LS_UNKNOWN];
387 }
388 
389 static void
390 qla83xx_handle_8200_aen(scsi_qla_host_t *vha, uint16_t *mb)
391 {
392 	struct qla_hw_data *ha = vha->hw;
393 
394 	/*
395 	 * 8200 AEN Interpretation:
396 	 * mb[0] = AEN code
397 	 * mb[1] = AEN Reason code
398 	 * mb[2] = LSW of Peg-Halt Status-1 Register
399 	 * mb[6] = MSW of Peg-Halt Status-1 Register
400 	 * mb[3] = LSW of Peg-Halt Status-2 register
401 	 * mb[7] = MSW of Peg-Halt Status-2 register
402 	 * mb[4] = IDC Device-State Register value
403 	 * mb[5] = IDC Driver-Presence Register value
404 	 */
405 	ql_dbg(ql_dbg_async, vha, 0x506b, "AEN Code: mb[0] = 0x%x AEN reason: "
406 	    "mb[1] = 0x%x PH-status1: mb[2] = 0x%x PH-status1: mb[6] = 0x%x.\n",
407 	    mb[0], mb[1], mb[2], mb[6]);
408 	ql_dbg(ql_dbg_async, vha, 0x506c, "PH-status2: mb[3] = 0x%x "
409 	    "PH-status2: mb[7] = 0x%x Device-State: mb[4] = 0x%x "
410 	    "Drv-Presence: mb[5] = 0x%x.\n", mb[3], mb[7], mb[4], mb[5]);
411 
412 	if (mb[1] & (IDC_PEG_HALT_STATUS_CHANGE | IDC_NIC_FW_REPORTED_FAILURE |
413 				IDC_HEARTBEAT_FAILURE)) {
414 		ha->flags.nic_core_hung = 1;
415 		ql_log(ql_log_warn, vha, 0x5060,
416 		    "83XX: F/W Error Reported: Check if reset required.\n");
417 
418 		if (mb[1] & IDC_PEG_HALT_STATUS_CHANGE) {
419 			uint32_t protocol_engine_id, fw_err_code, err_level;
420 
421 			/*
422 			 * IDC_PEG_HALT_STATUS_CHANGE interpretation:
423 			 *  - PEG-Halt Status-1 Register:
424 			 *	(LSW = mb[2], MSW = mb[6])
425 			 *	Bits 0-7   = protocol-engine ID
426 			 *	Bits 8-28  = f/w error code
427 			 *	Bits 29-31 = Error-level
428 			 *	    Error-level 0x1 = Non-Fatal error
429 			 *	    Error-level 0x2 = Recoverable Fatal error
430 			 *	    Error-level 0x4 = UnRecoverable Fatal error
431 			 *  - PEG-Halt Status-2 Register:
432 			 *	(LSW = mb[3], MSW = mb[7])
433 			 */
434 			protocol_engine_id = (mb[2] & 0xff);
435 			fw_err_code = (((mb[2] & 0xff00) >> 8) |
436 			    ((mb[6] & 0x1fff) << 8));
437 			err_level = ((mb[6] & 0xe000) >> 13);
438 			ql_log(ql_log_warn, vha, 0x5061, "PegHalt Status-1 "
439 			    "Register: protocol_engine_id=0x%x "
440 			    "fw_err_code=0x%x err_level=0x%x.\n",
441 			    protocol_engine_id, fw_err_code, err_level);
442 			ql_log(ql_log_warn, vha, 0x5062, "PegHalt Status-2 "
443 			    "Register: 0x%x%x.\n", mb[7], mb[3]);
444 			if (err_level == ERR_LEVEL_NON_FATAL) {
445 				ql_log(ql_log_warn, vha, 0x5063,
446 				    "Not a fatal error, f/w has recovered itself.\n");
447 			} else if (err_level == ERR_LEVEL_RECOVERABLE_FATAL) {
448 				ql_log(ql_log_fatal, vha, 0x5064,
449 				    "Recoverable Fatal error: Chip reset "
450 				    "required.\n");
451 				qla83xx_schedule_work(vha,
452 				    QLA83XX_NIC_CORE_RESET);
453 			} else if (err_level == ERR_LEVEL_UNRECOVERABLE_FATAL) {
454 				ql_log(ql_log_fatal, vha, 0x5065,
455 				    "Unrecoverable Fatal error: Set FAILED "
456 				    "state, reboot required.\n");
457 				qla83xx_schedule_work(vha,
458 				    QLA83XX_NIC_CORE_UNRECOVERABLE);
459 			}
460 		}
461 
462 		if (mb[1] & IDC_NIC_FW_REPORTED_FAILURE) {
463 			uint16_t peg_fw_state, nw_interface_link_up;
464 			uint16_t nw_interface_signal_detect, sfp_status;
465 			uint16_t htbt_counter, htbt_monitor_enable;
466 			uint16_t sfp_additional_info, sfp_multirate;
467 			uint16_t sfp_tx_fault, link_speed, dcbx_status;
468 
469 			/*
470 			 * IDC_NIC_FW_REPORTED_FAILURE interpretation:
471 			 *  - PEG-to-FC Status Register:
472 			 *	(LSW = mb[2], MSW = mb[6])
473 			 *	Bits 0-7   = Peg-Firmware state
474 			 *	Bit 8      = N/W Interface Link-up
475 			 *	Bit 9      = N/W Interface signal detected
476 			 *	Bits 10-11 = SFP Status
477 			 *	  SFP Status 0x0 = SFP+ transceiver not expected
478 			 *	  SFP Status 0x1 = SFP+ transceiver not present
479 			 *	  SFP Status 0x2 = SFP+ transceiver invalid
480 			 *	  SFP Status 0x3 = SFP+ transceiver present and
481 			 *	  valid
482 			 *	Bits 12-14 = Heartbeat Counter
483 			 *	Bit 15     = Heartbeat Monitor Enable
484 			 *	Bits 16-17 = SFP Additional Info
485 			 *	  SFP info 0x0 = Unregocnized transceiver for
486 			 *	  Ethernet
487 			 *	  SFP info 0x1 = SFP+ brand validation failed
488 			 *	  SFP info 0x2 = SFP+ speed validation failed
489 			 *	  SFP info 0x3 = SFP+ access error
490 			 *	Bit 18     = SFP Multirate
491 			 *	Bit 19     = SFP Tx Fault
492 			 *	Bits 20-22 = Link Speed
493 			 *	Bits 23-27 = Reserved
494 			 *	Bits 28-30 = DCBX Status
495 			 *	  DCBX Status 0x0 = DCBX Disabled
496 			 *	  DCBX Status 0x1 = DCBX Enabled
497 			 *	  DCBX Status 0x2 = DCBX Exchange error
498 			 *	Bit 31     = Reserved
499 			 */
500 			peg_fw_state = (mb[2] & 0x00ff);
501 			nw_interface_link_up = ((mb[2] & 0x0100) >> 8);
502 			nw_interface_signal_detect = ((mb[2] & 0x0200) >> 9);
503 			sfp_status = ((mb[2] & 0x0c00) >> 10);
504 			htbt_counter = ((mb[2] & 0x7000) >> 12);
505 			htbt_monitor_enable = ((mb[2] & 0x8000) >> 15);
506 			sfp_additional_info = (mb[6] & 0x0003);
507 			sfp_multirate = ((mb[6] & 0x0004) >> 2);
508 			sfp_tx_fault = ((mb[6] & 0x0008) >> 3);
509 			link_speed = ((mb[6] & 0x0070) >> 4);
510 			dcbx_status = ((mb[6] & 0x7000) >> 12);
511 
512 			ql_log(ql_log_warn, vha, 0x5066,
513 			    "Peg-to-Fc Status Register:\n"
514 			    "peg_fw_state=0x%x, nw_interface_link_up=0x%x, "
515 			    "nw_interface_signal_detect=0x%x"
516 			    "\nsfp_statis=0x%x.\n ", peg_fw_state,
517 			    nw_interface_link_up, nw_interface_signal_detect,
518 			    sfp_status);
519 			ql_log(ql_log_warn, vha, 0x5067,
520 			    "htbt_counter=0x%x, htbt_monitor_enable=0x%x, "
521 			    "sfp_additional_info=0x%x, sfp_multirate=0x%x.\n ",
522 			    htbt_counter, htbt_monitor_enable,
523 			    sfp_additional_info, sfp_multirate);
524 			ql_log(ql_log_warn, vha, 0x5068,
525 			    "sfp_tx_fault=0x%x, link_state=0x%x, "
526 			    "dcbx_status=0x%x.\n", sfp_tx_fault, link_speed,
527 			    dcbx_status);
528 
529 			qla83xx_schedule_work(vha, QLA83XX_NIC_CORE_RESET);
530 		}
531 
532 		if (mb[1] & IDC_HEARTBEAT_FAILURE) {
533 			ql_log(ql_log_warn, vha, 0x5069,
534 			    "Heartbeat Failure encountered, chip reset "
535 			    "required.\n");
536 
537 			qla83xx_schedule_work(vha, QLA83XX_NIC_CORE_RESET);
538 		}
539 	}
540 
541 	if (mb[1] & IDC_DEVICE_STATE_CHANGE) {
542 		ql_log(ql_log_info, vha, 0x506a,
543 		    "IDC Device-State changed = 0x%x.\n", mb[4]);
544 		if (ha->flags.nic_core_reset_owner)
545 			return;
546 		qla83xx_schedule_work(vha, MBA_IDC_AEN);
547 	}
548 }
549 
550 int
551 qla2x00_is_a_vp_did(scsi_qla_host_t *vha, uint32_t rscn_entry)
552 {
553 	struct qla_hw_data *ha = vha->hw;
554 	scsi_qla_host_t *vp;
555 	uint32_t vp_did;
556 	unsigned long flags;
557 	int ret = 0;
558 
559 	if (!ha->num_vhosts)
560 		return ret;
561 
562 	spin_lock_irqsave(&ha->vport_slock, flags);
563 	list_for_each_entry(vp, &ha->vp_list, list) {
564 		vp_did = vp->d_id.b24;
565 		if (vp_did == rscn_entry) {
566 			ret = 1;
567 			break;
568 		}
569 	}
570 	spin_unlock_irqrestore(&ha->vport_slock, flags);
571 
572 	return ret;
573 }
574 
575 fc_port_t *
576 qla2x00_find_fcport_by_loopid(scsi_qla_host_t *vha, uint16_t loop_id)
577 {
578 	fc_port_t *f, *tf;
579 
580 	f = tf = NULL;
581 	list_for_each_entry_safe(f, tf, &vha->vp_fcports, list)
582 		if (f->loop_id == loop_id)
583 			return f;
584 	return NULL;
585 }
586 
587 fc_port_t *
588 qla2x00_find_fcport_by_wwpn(scsi_qla_host_t *vha, u8 *wwpn, u8 incl_deleted)
589 {
590 	fc_port_t *f, *tf;
591 
592 	f = tf = NULL;
593 	list_for_each_entry_safe(f, tf, &vha->vp_fcports, list) {
594 		if (memcmp(f->port_name, wwpn, WWN_SIZE) == 0) {
595 			if (incl_deleted)
596 				return f;
597 			else if (f->deleted == 0)
598 				return f;
599 		}
600 	}
601 	return NULL;
602 }
603 
604 fc_port_t *
605 qla2x00_find_fcport_by_nportid(scsi_qla_host_t *vha, port_id_t *id,
606 	u8 incl_deleted)
607 {
608 	fc_port_t *f, *tf;
609 
610 	f = tf = NULL;
611 	list_for_each_entry_safe(f, tf, &vha->vp_fcports, list) {
612 		if (f->d_id.b24 == id->b24) {
613 			if (incl_deleted)
614 				return f;
615 			else if (f->deleted == 0)
616 				return f;
617 		}
618 	}
619 	return NULL;
620 }
621 
622 /**
623  * qla2x00_async_event() - Process aynchronous events.
624  * @vha: SCSI driver HA context
625  * @rsp: response queue
626  * @mb: Mailbox registers (0 - 3)
627  */
628 void
629 qla2x00_async_event(scsi_qla_host_t *vha, struct rsp_que *rsp, uint16_t *mb)
630 {
631 	uint16_t	handle_cnt;
632 	uint16_t	cnt, mbx;
633 	uint32_t	handles[5];
634 	struct qla_hw_data *ha = vha->hw;
635 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
636 	struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
637 	struct device_reg_82xx __iomem *reg82 = &ha->iobase->isp82;
638 	uint32_t	rscn_entry, host_pid;
639 	unsigned long	flags;
640 	fc_port_t	*fcport = NULL;
641 
642 	if (!vha->hw->flags.fw_started)
643 		return;
644 
645 	/* Setup to process RIO completion. */
646 	handle_cnt = 0;
647 	if (IS_CNA_CAPABLE(ha))
648 		goto skip_rio;
649 	switch (mb[0]) {
650 	case MBA_SCSI_COMPLETION:
651 		handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
652 		handle_cnt = 1;
653 		break;
654 	case MBA_CMPLT_1_16BIT:
655 		handles[0] = mb[1];
656 		handle_cnt = 1;
657 		mb[0] = MBA_SCSI_COMPLETION;
658 		break;
659 	case MBA_CMPLT_2_16BIT:
660 		handles[0] = mb[1];
661 		handles[1] = mb[2];
662 		handle_cnt = 2;
663 		mb[0] = MBA_SCSI_COMPLETION;
664 		break;
665 	case MBA_CMPLT_3_16BIT:
666 		handles[0] = mb[1];
667 		handles[1] = mb[2];
668 		handles[2] = mb[3];
669 		handle_cnt = 3;
670 		mb[0] = MBA_SCSI_COMPLETION;
671 		break;
672 	case MBA_CMPLT_4_16BIT:
673 		handles[0] = mb[1];
674 		handles[1] = mb[2];
675 		handles[2] = mb[3];
676 		handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
677 		handle_cnt = 4;
678 		mb[0] = MBA_SCSI_COMPLETION;
679 		break;
680 	case MBA_CMPLT_5_16BIT:
681 		handles[0] = mb[1];
682 		handles[1] = mb[2];
683 		handles[2] = mb[3];
684 		handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
685 		handles[4] = (uint32_t)RD_MAILBOX_REG(ha, reg, 7);
686 		handle_cnt = 5;
687 		mb[0] = MBA_SCSI_COMPLETION;
688 		break;
689 	case MBA_CMPLT_2_32BIT:
690 		handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
691 		handles[1] = le32_to_cpu(
692 		    ((uint32_t)(RD_MAILBOX_REG(ha, reg, 7) << 16)) |
693 		    RD_MAILBOX_REG(ha, reg, 6));
694 		handle_cnt = 2;
695 		mb[0] = MBA_SCSI_COMPLETION;
696 		break;
697 	default:
698 		break;
699 	}
700 skip_rio:
701 	switch (mb[0]) {
702 	case MBA_SCSI_COMPLETION:	/* Fast Post */
703 		if (!vha->flags.online)
704 			break;
705 
706 		for (cnt = 0; cnt < handle_cnt; cnt++)
707 			qla2x00_process_completed_request(vha, rsp->req,
708 				handles[cnt]);
709 		break;
710 
711 	case MBA_RESET:			/* Reset */
712 		ql_dbg(ql_dbg_async, vha, 0x5002,
713 		    "Asynchronous RESET.\n");
714 
715 		set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
716 		break;
717 
718 	case MBA_SYSTEM_ERR:		/* System Error */
719 		mbx = (IS_QLA81XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
720 		    IS_QLA28XX(ha)) ?
721 			RD_REG_WORD(&reg24->mailbox7) : 0;
722 		ql_log(ql_log_warn, vha, 0x5003,
723 		    "ISP System Error - mbx1=%xh mbx2=%xh mbx3=%xh "
724 		    "mbx7=%xh.\n", mb[1], mb[2], mb[3], mbx);
725 		ha->fw_dump_mpi =
726 		    (IS_QLA27XX(ha) || IS_QLA28XX(ha)) &&
727 		    RD_REG_WORD(&reg24->mailbox7) & BIT_8;
728 		ha->isp_ops->fw_dump(vha, 1);
729 		ha->flags.fw_init_done = 0;
730 		QLA_FW_STOPPED(ha);
731 
732 		if (IS_FWI2_CAPABLE(ha)) {
733 			if (mb[1] == 0 && mb[2] == 0) {
734 				ql_log(ql_log_fatal, vha, 0x5004,
735 				    "Unrecoverable Hardware Error: adapter "
736 				    "marked OFFLINE!\n");
737 				vha->flags.online = 0;
738 				vha->device_flags |= DFLG_DEV_FAILED;
739 			} else {
740 				/* Check to see if MPI timeout occurred */
741 				if ((mbx & MBX_3) && (ha->port_no == 0))
742 					set_bit(MPI_RESET_NEEDED,
743 					    &vha->dpc_flags);
744 
745 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
746 			}
747 		} else if (mb[1] == 0) {
748 			ql_log(ql_log_fatal, vha, 0x5005,
749 			    "Unrecoverable Hardware Error: adapter marked "
750 			    "OFFLINE!\n");
751 			vha->flags.online = 0;
752 			vha->device_flags |= DFLG_DEV_FAILED;
753 		} else
754 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
755 		break;
756 
757 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
758 		ql_log(ql_log_warn, vha, 0x5006,
759 		    "ISP Request Transfer Error (%x).\n",  mb[1]);
760 
761 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
762 		break;
763 
764 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
765 		ql_log(ql_log_warn, vha, 0x5007,
766 		    "ISP Response Transfer Error (%x).\n", mb[1]);
767 
768 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
769 		break;
770 
771 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up */
772 		ql_dbg(ql_dbg_async, vha, 0x5008,
773 		    "Asynchronous WAKEUP_THRES (%x).\n", mb[1]);
774 		break;
775 
776 	case MBA_LOOP_INIT_ERR:
777 		ql_log(ql_log_warn, vha, 0x5090,
778 		    "LOOP INIT ERROR (%x).\n", mb[1]);
779 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
780 		break;
781 
782 	case MBA_LIP_OCCURRED:		/* Loop Initialization Procedure */
783 		ha->flags.lip_ae = 1;
784 
785 		ql_dbg(ql_dbg_async, vha, 0x5009,
786 		    "LIP occurred (%x).\n", mb[1]);
787 
788 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
789 			atomic_set(&vha->loop_state, LOOP_DOWN);
790 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
791 			qla2x00_mark_all_devices_lost(vha, 1);
792 		}
793 
794 		if (vha->vp_idx) {
795 			atomic_set(&vha->vp_state, VP_FAILED);
796 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
797 		}
798 
799 		set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
800 		set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
801 
802 		vha->flags.management_server_logged_in = 0;
803 		qla2x00_post_aen_work(vha, FCH_EVT_LIP, mb[1]);
804 		break;
805 
806 	case MBA_LOOP_UP:		/* Loop Up Event */
807 		if (IS_QLA2100(ha) || IS_QLA2200(ha))
808 			ha->link_data_rate = PORT_SPEED_1GB;
809 		else
810 			ha->link_data_rate = mb[1];
811 
812 		ql_log(ql_log_info, vha, 0x500a,
813 		    "LOOP UP detected (%s Gbps).\n",
814 		    qla2x00_get_link_speed_str(ha, ha->link_data_rate));
815 
816 		vha->flags.management_server_logged_in = 0;
817 		qla2x00_post_aen_work(vha, FCH_EVT_LINKUP, ha->link_data_rate);
818 
819 		if (AUTO_DETECT_SFP_SUPPORT(vha)) {
820 			set_bit(DETECT_SFP_CHANGE, &vha->dpc_flags);
821 			qla2xxx_wake_dpc(vha);
822 		}
823 		break;
824 
825 	case MBA_LOOP_DOWN:		/* Loop Down Event */
826 		SAVE_TOPO(ha);
827 		ha->flags.lip_ae = 0;
828 		ha->current_topology = 0;
829 
830 		mbx = (IS_QLA81XX(ha) || IS_QLA8031(ha))
831 			? RD_REG_WORD(&reg24->mailbox4) : 0;
832 		mbx = (IS_P3P_TYPE(ha)) ? RD_REG_WORD(&reg82->mailbox_out[4])
833 			: mbx;
834 		ql_log(ql_log_info, vha, 0x500b,
835 		    "LOOP DOWN detected (%x %x %x %x).\n",
836 		    mb[1], mb[2], mb[3], mbx);
837 
838 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
839 			atomic_set(&vha->loop_state, LOOP_DOWN);
840 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
841 			/*
842 			 * In case of loop down, restore WWPN from
843 			 * NVRAM in case of FA-WWPN capable ISP
844 			 * Restore for Physical Port only
845 			 */
846 			if (!vha->vp_idx) {
847 				if (ha->flags.fawwpn_enabled &&
848 				    (ha->current_topology == ISP_CFG_F)) {
849 					void *wwpn = ha->init_cb->port_name;
850 
851 					memcpy(vha->port_name, wwpn, WWN_SIZE);
852 					fc_host_port_name(vha->host) =
853 					    wwn_to_u64(vha->port_name);
854 					ql_dbg(ql_dbg_init + ql_dbg_verbose,
855 					    vha, 0x00d8, "LOOP DOWN detected,"
856 					    "restore WWPN %016llx\n",
857 					    wwn_to_u64(vha->port_name));
858 				}
859 
860 				clear_bit(VP_CONFIG_OK, &vha->vp_flags);
861 			}
862 
863 			vha->device_flags |= DFLG_NO_CABLE;
864 			qla2x00_mark_all_devices_lost(vha, 1);
865 		}
866 
867 		if (vha->vp_idx) {
868 			atomic_set(&vha->vp_state, VP_FAILED);
869 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
870 		}
871 
872 		vha->flags.management_server_logged_in = 0;
873 		ha->link_data_rate = PORT_SPEED_UNKNOWN;
874 		qla2x00_post_aen_work(vha, FCH_EVT_LINKDOWN, 0);
875 		break;
876 
877 	case MBA_LIP_RESET:		/* LIP reset occurred */
878 		ql_dbg(ql_dbg_async, vha, 0x500c,
879 		    "LIP reset occurred (%x).\n", mb[1]);
880 
881 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
882 			atomic_set(&vha->loop_state, LOOP_DOWN);
883 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
884 			qla2x00_mark_all_devices_lost(vha, 1);
885 		}
886 
887 		if (vha->vp_idx) {
888 			atomic_set(&vha->vp_state, VP_FAILED);
889 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
890 		}
891 
892 		set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
893 
894 		ha->operating_mode = LOOP;
895 		vha->flags.management_server_logged_in = 0;
896 		qla2x00_post_aen_work(vha, FCH_EVT_LIPRESET, mb[1]);
897 		break;
898 
899 	/* case MBA_DCBX_COMPLETE: */
900 	case MBA_POINT_TO_POINT:	/* Point-to-Point */
901 		ha->flags.lip_ae = 0;
902 
903 		if (IS_QLA2100(ha))
904 			break;
905 
906 		if (IS_CNA_CAPABLE(ha)) {
907 			ql_dbg(ql_dbg_async, vha, 0x500d,
908 			    "DCBX Completed -- %04x %04x %04x.\n",
909 			    mb[1], mb[2], mb[3]);
910 			if (ha->notify_dcbx_comp && !vha->vp_idx)
911 				complete(&ha->dcbx_comp);
912 
913 		} else
914 			ql_dbg(ql_dbg_async, vha, 0x500e,
915 			    "Asynchronous P2P MODE received.\n");
916 
917 		/*
918 		 * Until there's a transition from loop down to loop up, treat
919 		 * this as loop down only.
920 		 */
921 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
922 			atomic_set(&vha->loop_state, LOOP_DOWN);
923 			if (!atomic_read(&vha->loop_down_timer))
924 				atomic_set(&vha->loop_down_timer,
925 				    LOOP_DOWN_TIME);
926 			if (!N2N_TOPO(ha))
927 				qla2x00_mark_all_devices_lost(vha, 1);
928 		}
929 
930 		if (vha->vp_idx) {
931 			atomic_set(&vha->vp_state, VP_FAILED);
932 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
933 		}
934 
935 		if (!(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)))
936 			set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
937 
938 		set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
939 		set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
940 
941 		vha->flags.management_server_logged_in = 0;
942 		break;
943 
944 	case MBA_CHG_IN_CONNECTION:	/* Change in connection mode */
945 		if (IS_QLA2100(ha))
946 			break;
947 
948 		ql_dbg(ql_dbg_async, vha, 0x500f,
949 		    "Configuration change detected: value=%x.\n", mb[1]);
950 
951 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
952 			atomic_set(&vha->loop_state, LOOP_DOWN);
953 			if (!atomic_read(&vha->loop_down_timer))
954 				atomic_set(&vha->loop_down_timer,
955 				    LOOP_DOWN_TIME);
956 			qla2x00_mark_all_devices_lost(vha, 1);
957 		}
958 
959 		if (vha->vp_idx) {
960 			atomic_set(&vha->vp_state, VP_FAILED);
961 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
962 		}
963 
964 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
965 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
966 		break;
967 
968 	case MBA_PORT_UPDATE:		/* Port database update */
969 		/*
970 		 * Handle only global and vn-port update events
971 		 *
972 		 * Relevant inputs:
973 		 * mb[1] = N_Port handle of changed port
974 		 * OR 0xffff for global event
975 		 * mb[2] = New login state
976 		 * 7 = Port logged out
977 		 * mb[3] = LSB is vp_idx, 0xff = all vps
978 		 *
979 		 * Skip processing if:
980 		 *       Event is global, vp_idx is NOT all vps,
981 		 *           vp_idx does not match
982 		 *       Event is not global, vp_idx does not match
983 		 */
984 		if (IS_QLA2XXX_MIDTYPE(ha) &&
985 		    ((mb[1] == 0xffff && (mb[3] & 0xff) != 0xff) ||
986 			(mb[1] != 0xffff)) && vha->vp_idx != (mb[3] & 0xff))
987 			break;
988 
989 		if (mb[2] == 0x7) {
990 			ql_dbg(ql_dbg_async, vha, 0x5010,
991 			    "Port %s %04x %04x %04x.\n",
992 			    mb[1] == 0xffff ? "unavailable" : "logout",
993 			    mb[1], mb[2], mb[3]);
994 
995 			if (mb[1] == 0xffff)
996 				goto global_port_update;
997 
998 			if (mb[1] == NPH_SNS_LID(ha)) {
999 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1000 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1001 				break;
1002 			}
1003 
1004 			/* use handle_cnt for loop id/nport handle */
1005 			if (IS_FWI2_CAPABLE(ha))
1006 				handle_cnt = NPH_SNS;
1007 			else
1008 				handle_cnt = SIMPLE_NAME_SERVER;
1009 			if (mb[1] == handle_cnt) {
1010 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1011 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1012 				break;
1013 			}
1014 
1015 			/* Port logout */
1016 			fcport = qla2x00_find_fcport_by_loopid(vha, mb[1]);
1017 			if (!fcport)
1018 				break;
1019 			if (atomic_read(&fcport->state) != FCS_ONLINE)
1020 				break;
1021 			ql_dbg(ql_dbg_async, vha, 0x508a,
1022 			    "Marking port lost loopid=%04x portid=%06x.\n",
1023 			    fcport->loop_id, fcport->d_id.b24);
1024 			if (qla_ini_mode_enabled(vha)) {
1025 				qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
1026 				fcport->logout_on_delete = 0;
1027 				qlt_schedule_sess_for_deletion(fcport);
1028 			}
1029 			break;
1030 
1031 global_port_update:
1032 			if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
1033 				atomic_set(&vha->loop_state, LOOP_DOWN);
1034 				atomic_set(&vha->loop_down_timer,
1035 				    LOOP_DOWN_TIME);
1036 				vha->device_flags |= DFLG_NO_CABLE;
1037 				qla2x00_mark_all_devices_lost(vha, 1);
1038 			}
1039 
1040 			if (vha->vp_idx) {
1041 				atomic_set(&vha->vp_state, VP_FAILED);
1042 				fc_vport_set_state(vha->fc_vport,
1043 				    FC_VPORT_FAILED);
1044 				qla2x00_mark_all_devices_lost(vha, 1);
1045 			}
1046 
1047 			vha->flags.management_server_logged_in = 0;
1048 			ha->link_data_rate = PORT_SPEED_UNKNOWN;
1049 			break;
1050 		}
1051 
1052 		/*
1053 		 * If PORT UPDATE is global (received LIP_OCCURRED/LIP_RESET
1054 		 * event etc. earlier indicating loop is down) then process
1055 		 * it.  Otherwise ignore it and Wait for RSCN to come in.
1056 		 */
1057 		atomic_set(&vha->loop_down_timer, 0);
1058 		if (atomic_read(&vha->loop_state) != LOOP_DOWN &&
1059 			!ha->flags.n2n_ae  &&
1060 		    atomic_read(&vha->loop_state) != LOOP_DEAD) {
1061 			ql_dbg(ql_dbg_async, vha, 0x5011,
1062 			    "Asynchronous PORT UPDATE ignored %04x/%04x/%04x.\n",
1063 			    mb[1], mb[2], mb[3]);
1064 
1065 			qlt_async_event(mb[0], vha, mb);
1066 			break;
1067 		}
1068 
1069 		ql_dbg(ql_dbg_async, vha, 0x5012,
1070 		    "Port database changed %04x %04x %04x.\n",
1071 		    mb[1], mb[2], mb[3]);
1072 
1073 		/*
1074 		 * Mark all devices as missing so we will login again.
1075 		 */
1076 		atomic_set(&vha->loop_state, LOOP_UP);
1077 		vha->scan.scan_retry = 0;
1078 
1079 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1080 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1081 		set_bit(VP_CONFIG_OK, &vha->vp_flags);
1082 
1083 		qlt_async_event(mb[0], vha, mb);
1084 		break;
1085 
1086 	case MBA_RSCN_UPDATE:		/* State Change Registration */
1087 		/* Check if the Vport has issued a SCR */
1088 		if (vha->vp_idx && test_bit(VP_SCR_NEEDED, &vha->vp_flags))
1089 			break;
1090 		/* Only handle SCNs for our Vport index. */
1091 		if (ha->flags.npiv_supported && vha->vp_idx != (mb[3] & 0xff))
1092 			break;
1093 
1094 		ql_dbg(ql_dbg_async, vha, 0x5013,
1095 		    "RSCN database changed -- %04x %04x %04x.\n",
1096 		    mb[1], mb[2], mb[3]);
1097 
1098 		rscn_entry = ((mb[1] & 0xff) << 16) | mb[2];
1099 		host_pid = (vha->d_id.b.domain << 16) | (vha->d_id.b.area << 8)
1100 				| vha->d_id.b.al_pa;
1101 		if (rscn_entry == host_pid) {
1102 			ql_dbg(ql_dbg_async, vha, 0x5014,
1103 			    "Ignoring RSCN update to local host "
1104 			    "port ID (%06x).\n", host_pid);
1105 			break;
1106 		}
1107 
1108 		/* Ignore reserved bits from RSCN-payload. */
1109 		rscn_entry = ((mb[1] & 0x3ff) << 16) | mb[2];
1110 
1111 		/* Skip RSCNs for virtual ports on the same physical port */
1112 		if (qla2x00_is_a_vp_did(vha, rscn_entry))
1113 			break;
1114 
1115 		atomic_set(&vha->loop_down_timer, 0);
1116 		vha->flags.management_server_logged_in = 0;
1117 		{
1118 			struct event_arg ea;
1119 
1120 			memset(&ea, 0, sizeof(ea));
1121 			ea.id.b24 = rscn_entry;
1122 			ea.id.b.rsvd_1 = rscn_entry >> 24;
1123 			qla2x00_handle_rscn(vha, &ea);
1124 			qla2x00_post_aen_work(vha, FCH_EVT_RSCN, rscn_entry);
1125 		}
1126 		break;
1127 	/* case MBA_RIO_RESPONSE: */
1128 	case MBA_ZIO_RESPONSE:
1129 		ql_dbg(ql_dbg_async, vha, 0x5015,
1130 		    "[R|Z]IO update completion.\n");
1131 
1132 		if (IS_FWI2_CAPABLE(ha))
1133 			qla24xx_process_response_queue(vha, rsp);
1134 		else
1135 			qla2x00_process_response_queue(rsp);
1136 		break;
1137 
1138 	case MBA_DISCARD_RND_FRAME:
1139 		ql_dbg(ql_dbg_async, vha, 0x5016,
1140 		    "Discard RND Frame -- %04x %04x %04x.\n",
1141 		    mb[1], mb[2], mb[3]);
1142 		break;
1143 
1144 	case MBA_TRACE_NOTIFICATION:
1145 		ql_dbg(ql_dbg_async, vha, 0x5017,
1146 		    "Trace Notification -- %04x %04x.\n", mb[1], mb[2]);
1147 		break;
1148 
1149 	case MBA_ISP84XX_ALERT:
1150 		ql_dbg(ql_dbg_async, vha, 0x5018,
1151 		    "ISP84XX Alert Notification -- %04x %04x %04x.\n",
1152 		    mb[1], mb[2], mb[3]);
1153 
1154 		spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
1155 		switch (mb[1]) {
1156 		case A84_PANIC_RECOVERY:
1157 			ql_log(ql_log_info, vha, 0x5019,
1158 			    "Alert 84XX: panic recovery %04x %04x.\n",
1159 			    mb[2], mb[3]);
1160 			break;
1161 		case A84_OP_LOGIN_COMPLETE:
1162 			ha->cs84xx->op_fw_version = mb[3] << 16 | mb[2];
1163 			ql_log(ql_log_info, vha, 0x501a,
1164 			    "Alert 84XX: firmware version %x.\n",
1165 			    ha->cs84xx->op_fw_version);
1166 			break;
1167 		case A84_DIAG_LOGIN_COMPLETE:
1168 			ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
1169 			ql_log(ql_log_info, vha, 0x501b,
1170 			    "Alert 84XX: diagnostic firmware version %x.\n",
1171 			    ha->cs84xx->diag_fw_version);
1172 			break;
1173 		case A84_GOLD_LOGIN_COMPLETE:
1174 			ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
1175 			ha->cs84xx->fw_update = 1;
1176 			ql_log(ql_log_info, vha, 0x501c,
1177 			    "Alert 84XX: gold firmware version %x.\n",
1178 			    ha->cs84xx->gold_fw_version);
1179 			break;
1180 		default:
1181 			ql_log(ql_log_warn, vha, 0x501d,
1182 			    "Alert 84xx: Invalid Alert %04x %04x %04x.\n",
1183 			    mb[1], mb[2], mb[3]);
1184 		}
1185 		spin_unlock_irqrestore(&ha->cs84xx->access_lock, flags);
1186 		break;
1187 	case MBA_DCBX_START:
1188 		ql_dbg(ql_dbg_async, vha, 0x501e,
1189 		    "DCBX Started -- %04x %04x %04x.\n",
1190 		    mb[1], mb[2], mb[3]);
1191 		break;
1192 	case MBA_DCBX_PARAM_UPDATE:
1193 		ql_dbg(ql_dbg_async, vha, 0x501f,
1194 		    "DCBX Parameters Updated -- %04x %04x %04x.\n",
1195 		    mb[1], mb[2], mb[3]);
1196 		break;
1197 	case MBA_FCF_CONF_ERR:
1198 		ql_dbg(ql_dbg_async, vha, 0x5020,
1199 		    "FCF Configuration Error -- %04x %04x %04x.\n",
1200 		    mb[1], mb[2], mb[3]);
1201 		break;
1202 	case MBA_IDC_NOTIFY:
1203 		if (IS_QLA8031(vha->hw) || IS_QLA8044(ha)) {
1204 			mb[4] = RD_REG_WORD(&reg24->mailbox4);
1205 			if (((mb[2] & 0x7fff) == MBC_PORT_RESET ||
1206 			    (mb[2] & 0x7fff) == MBC_SET_PORT_CONFIG) &&
1207 			    (mb[4] & INTERNAL_LOOPBACK_MASK) != 0) {
1208 				set_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags);
1209 				/*
1210 				 * Extend loop down timer since port is active.
1211 				 */
1212 				if (atomic_read(&vha->loop_state) == LOOP_DOWN)
1213 					atomic_set(&vha->loop_down_timer,
1214 					    LOOP_DOWN_TIME);
1215 				qla2xxx_wake_dpc(vha);
1216 			}
1217 		}
1218 		/* fall through */
1219 	case MBA_IDC_COMPLETE:
1220 		if (ha->notify_lb_portup_comp && !vha->vp_idx)
1221 			complete(&ha->lb_portup_comp);
1222 		/* Fallthru */
1223 	case MBA_IDC_TIME_EXT:
1224 		if (IS_QLA81XX(vha->hw) || IS_QLA8031(vha->hw) ||
1225 		    IS_QLA8044(ha))
1226 			qla81xx_idc_event(vha, mb[0], mb[1]);
1227 		break;
1228 
1229 	case MBA_IDC_AEN:
1230 		mb[4] = RD_REG_WORD(&reg24->mailbox4);
1231 		mb[5] = RD_REG_WORD(&reg24->mailbox5);
1232 		mb[6] = RD_REG_WORD(&reg24->mailbox6);
1233 		mb[7] = RD_REG_WORD(&reg24->mailbox7);
1234 		qla83xx_handle_8200_aen(vha, mb);
1235 		break;
1236 
1237 	case MBA_DPORT_DIAGNOSTICS:
1238 		ql_dbg(ql_dbg_async, vha, 0x5052,
1239 		    "D-Port Diagnostics: %04x result=%s\n",
1240 		    mb[0],
1241 		    mb[1] == 0 ? "start" :
1242 		    mb[1] == 1 ? "done (pass)" :
1243 		    mb[1] == 2 ? "done (error)" : "other");
1244 		break;
1245 
1246 	case MBA_TEMPERATURE_ALERT:
1247 		ql_dbg(ql_dbg_async, vha, 0x505e,
1248 		    "TEMPERATURE ALERT: %04x %04x %04x\n", mb[1], mb[2], mb[3]);
1249 		if (mb[1] == 0x12)
1250 			schedule_work(&ha->board_disable);
1251 		break;
1252 
1253 	case MBA_TRANS_INSERT:
1254 		ql_dbg(ql_dbg_async, vha, 0x5091,
1255 		    "Transceiver Insertion: %04x\n", mb[1]);
1256 		break;
1257 
1258 	default:
1259 		ql_dbg(ql_dbg_async, vha, 0x5057,
1260 		    "Unknown AEN:%04x %04x %04x %04x\n",
1261 		    mb[0], mb[1], mb[2], mb[3]);
1262 	}
1263 
1264 	qlt_async_event(mb[0], vha, mb);
1265 
1266 	if (!vha->vp_idx && ha->num_vhosts)
1267 		qla2x00_alert_all_vps(rsp, mb);
1268 }
1269 
1270 /**
1271  * qla2x00_process_completed_request() - Process a Fast Post response.
1272  * @vha: SCSI driver HA context
1273  * @req: request queue
1274  * @index: SRB index
1275  */
1276 void
1277 qla2x00_process_completed_request(struct scsi_qla_host *vha,
1278 				  struct req_que *req, uint32_t index)
1279 {
1280 	srb_t *sp;
1281 	struct qla_hw_data *ha = vha->hw;
1282 
1283 	/* Validate handle. */
1284 	if (index >= req->num_outstanding_cmds) {
1285 		ql_log(ql_log_warn, vha, 0x3014,
1286 		    "Invalid SCSI command index (%x).\n", index);
1287 
1288 		if (IS_P3P_TYPE(ha))
1289 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1290 		else
1291 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1292 		return;
1293 	}
1294 
1295 	sp = req->outstanding_cmds[index];
1296 	if (sp) {
1297 		/* Free outstanding command slot. */
1298 		req->outstanding_cmds[index] = NULL;
1299 
1300 		/* Save ISP completion status */
1301 		sp->done(sp, DID_OK << 16);
1302 	} else {
1303 		ql_log(ql_log_warn, vha, 0x3016, "Invalid SCSI SRB.\n");
1304 
1305 		if (IS_P3P_TYPE(ha))
1306 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1307 		else
1308 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1309 	}
1310 }
1311 
1312 srb_t *
1313 qla2x00_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
1314     struct req_que *req, void *iocb)
1315 {
1316 	struct qla_hw_data *ha = vha->hw;
1317 	sts_entry_t *pkt = iocb;
1318 	srb_t *sp = NULL;
1319 	uint16_t index;
1320 
1321 	index = LSW(pkt->handle);
1322 	if (index >= req->num_outstanding_cmds) {
1323 		ql_log(ql_log_warn, vha, 0x5031,
1324 			   "Invalid command index (%x) type %8ph.\n",
1325 			   index, iocb);
1326 		if (IS_P3P_TYPE(ha))
1327 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1328 		else
1329 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1330 		goto done;
1331 	}
1332 	sp = req->outstanding_cmds[index];
1333 	if (!sp) {
1334 		ql_log(ql_log_warn, vha, 0x5032,
1335 		    "Invalid completion handle (%x) -- timed-out.\n", index);
1336 		return sp;
1337 	}
1338 	if (sp->handle != index) {
1339 		ql_log(ql_log_warn, vha, 0x5033,
1340 		    "SRB handle (%x) mismatch %x.\n", sp->handle, index);
1341 		return NULL;
1342 	}
1343 
1344 	req->outstanding_cmds[index] = NULL;
1345 
1346 done:
1347 	return sp;
1348 }
1349 
1350 static void
1351 qla2x00_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1352     struct mbx_entry *mbx)
1353 {
1354 	const char func[] = "MBX-IOCB";
1355 	const char *type;
1356 	fc_port_t *fcport;
1357 	srb_t *sp;
1358 	struct srb_iocb *lio;
1359 	uint16_t *data;
1360 	uint16_t status;
1361 
1362 	sp = qla2x00_get_sp_from_handle(vha, func, req, mbx);
1363 	if (!sp)
1364 		return;
1365 
1366 	lio = &sp->u.iocb_cmd;
1367 	type = sp->name;
1368 	fcport = sp->fcport;
1369 	data = lio->u.logio.data;
1370 
1371 	data[0] = MBS_COMMAND_ERROR;
1372 	data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
1373 	    QLA_LOGIO_LOGIN_RETRIED : 0;
1374 	if (mbx->entry_status) {
1375 		ql_dbg(ql_dbg_async, vha, 0x5043,
1376 		    "Async-%s error entry - hdl=%x portid=%02x%02x%02x "
1377 		    "entry-status=%x status=%x state-flag=%x "
1378 		    "status-flags=%x.\n", type, sp->handle,
1379 		    fcport->d_id.b.domain, fcport->d_id.b.area,
1380 		    fcport->d_id.b.al_pa, mbx->entry_status,
1381 		    le16_to_cpu(mbx->status), le16_to_cpu(mbx->state_flags),
1382 		    le16_to_cpu(mbx->status_flags));
1383 
1384 		ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x5029,
1385 		    mbx, sizeof(*mbx));
1386 
1387 		goto logio_done;
1388 	}
1389 
1390 	status = le16_to_cpu(mbx->status);
1391 	if (status == 0x30 && sp->type == SRB_LOGIN_CMD &&
1392 	    le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE)
1393 		status = 0;
1394 	if (!status && le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE) {
1395 		ql_dbg(ql_dbg_async, vha, 0x5045,
1396 		    "Async-%s complete - hdl=%x portid=%02x%02x%02x mbx1=%x.\n",
1397 		    type, sp->handle, fcport->d_id.b.domain,
1398 		    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1399 		    le16_to_cpu(mbx->mb1));
1400 
1401 		data[0] = MBS_COMMAND_COMPLETE;
1402 		if (sp->type == SRB_LOGIN_CMD) {
1403 			fcport->port_type = FCT_TARGET;
1404 			if (le16_to_cpu(mbx->mb1) & BIT_0)
1405 				fcport->port_type = FCT_INITIATOR;
1406 			else if (le16_to_cpu(mbx->mb1) & BIT_1)
1407 				fcport->flags |= FCF_FCP2_DEVICE;
1408 		}
1409 		goto logio_done;
1410 	}
1411 
1412 	data[0] = le16_to_cpu(mbx->mb0);
1413 	switch (data[0]) {
1414 	case MBS_PORT_ID_USED:
1415 		data[1] = le16_to_cpu(mbx->mb1);
1416 		break;
1417 	case MBS_LOOP_ID_USED:
1418 		break;
1419 	default:
1420 		data[0] = MBS_COMMAND_ERROR;
1421 		break;
1422 	}
1423 
1424 	ql_log(ql_log_warn, vha, 0x5046,
1425 	    "Async-%s failed - hdl=%x portid=%02x%02x%02x status=%x "
1426 	    "mb0=%x mb1=%x mb2=%x mb6=%x mb7=%x.\n", type, sp->handle,
1427 	    fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
1428 	    status, le16_to_cpu(mbx->mb0), le16_to_cpu(mbx->mb1),
1429 	    le16_to_cpu(mbx->mb2), le16_to_cpu(mbx->mb6),
1430 	    le16_to_cpu(mbx->mb7));
1431 
1432 logio_done:
1433 	sp->done(sp, 0);
1434 }
1435 
1436 static void
1437 qla24xx_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1438     struct mbx_24xx_entry *pkt)
1439 {
1440 	const char func[] = "MBX-IOCB2";
1441 	srb_t *sp;
1442 	struct srb_iocb *si;
1443 	u16 sz, i;
1444 	int res;
1445 
1446 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1447 	if (!sp)
1448 		return;
1449 
1450 	si = &sp->u.iocb_cmd;
1451 	sz = min(ARRAY_SIZE(pkt->mb), ARRAY_SIZE(sp->u.iocb_cmd.u.mbx.in_mb));
1452 
1453 	for (i = 0; i < sz; i++)
1454 		si->u.mbx.in_mb[i] = le16_to_cpu(pkt->mb[i]);
1455 
1456 	res = (si->u.mbx.in_mb[0] & MBS_MASK);
1457 
1458 	sp->done(sp, res);
1459 }
1460 
1461 static void
1462 qla24xxx_nack_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1463     struct nack_to_isp *pkt)
1464 {
1465 	const char func[] = "nack";
1466 	srb_t *sp;
1467 	int res = 0;
1468 
1469 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1470 	if (!sp)
1471 		return;
1472 
1473 	if (pkt->u.isp2x.status != cpu_to_le16(NOTIFY_ACK_SUCCESS))
1474 		res = QLA_FUNCTION_FAILED;
1475 
1476 	sp->done(sp, res);
1477 }
1478 
1479 static void
1480 qla2x00_ct_entry(scsi_qla_host_t *vha, struct req_que *req,
1481     sts_entry_t *pkt, int iocb_type)
1482 {
1483 	const char func[] = "CT_IOCB";
1484 	const char *type;
1485 	srb_t *sp;
1486 	struct bsg_job *bsg_job;
1487 	struct fc_bsg_reply *bsg_reply;
1488 	uint16_t comp_status;
1489 	int res = 0;
1490 
1491 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1492 	if (!sp)
1493 		return;
1494 
1495 	switch (sp->type) {
1496 	case SRB_CT_CMD:
1497 	    bsg_job = sp->u.bsg_job;
1498 	    bsg_reply = bsg_job->reply;
1499 
1500 	    type = "ct pass-through";
1501 
1502 	    comp_status = le16_to_cpu(pkt->comp_status);
1503 
1504 	    /*
1505 	     * return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
1506 	     * fc payload  to the caller
1507 	     */
1508 	    bsg_reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
1509 	    bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1510 
1511 	    if (comp_status != CS_COMPLETE) {
1512 		    if (comp_status == CS_DATA_UNDERRUN) {
1513 			    res = DID_OK << 16;
1514 			    bsg_reply->reply_payload_rcv_len =
1515 				le16_to_cpu(pkt->rsp_info_len);
1516 
1517 			    ql_log(ql_log_warn, vha, 0x5048,
1518 				"CT pass-through-%s error comp_status=0x%x total_byte=0x%x.\n",
1519 				type, comp_status,
1520 				bsg_reply->reply_payload_rcv_len);
1521 		    } else {
1522 			    ql_log(ql_log_warn, vha, 0x5049,
1523 				"CT pass-through-%s error comp_status=0x%x.\n",
1524 				type, comp_status);
1525 			    res = DID_ERROR << 16;
1526 			    bsg_reply->reply_payload_rcv_len = 0;
1527 		    }
1528 		    ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x5035,
1529 			pkt, sizeof(*pkt));
1530 	    } else {
1531 		    res = DID_OK << 16;
1532 		    bsg_reply->reply_payload_rcv_len =
1533 			bsg_job->reply_payload.payload_len;
1534 		    bsg_job->reply_len = 0;
1535 	    }
1536 	    break;
1537 	case SRB_CT_PTHRU_CMD:
1538 	    /*
1539 	     * borrowing sts_entry_24xx.comp_status.
1540 	     * same location as ct_entry_24xx.comp_status
1541 	     */
1542 	     res = qla2x00_chk_ms_status(vha, (ms_iocb_entry_t *)pkt,
1543 		 (struct ct_sns_rsp *)sp->u.iocb_cmd.u.ctarg.rsp,
1544 		 sp->name);
1545 	     break;
1546 	}
1547 
1548 	sp->done(sp, res);
1549 }
1550 
1551 static void
1552 qla24xx_els_ct_entry(scsi_qla_host_t *vha, struct req_que *req,
1553     struct sts_entry_24xx *pkt, int iocb_type)
1554 {
1555 	const char func[] = "ELS_CT_IOCB";
1556 	const char *type;
1557 	srb_t *sp;
1558 	struct bsg_job *bsg_job;
1559 	struct fc_bsg_reply *bsg_reply;
1560 	uint16_t comp_status;
1561 	uint32_t fw_status[3];
1562 	int res;
1563 	struct srb_iocb *els;
1564 
1565 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1566 	if (!sp)
1567 		return;
1568 
1569 	type = NULL;
1570 	switch (sp->type) {
1571 	case SRB_ELS_CMD_RPT:
1572 	case SRB_ELS_CMD_HST:
1573 		type = "els";
1574 		break;
1575 	case SRB_CT_CMD:
1576 		type = "ct pass-through";
1577 		break;
1578 	case SRB_ELS_DCMD:
1579 		type = "Driver ELS logo";
1580 		if (iocb_type != ELS_IOCB_TYPE) {
1581 			ql_dbg(ql_dbg_user, vha, 0x5047,
1582 			    "Completing %s: (%p) type=%d.\n",
1583 			    type, sp, sp->type);
1584 			sp->done(sp, 0);
1585 			return;
1586 		}
1587 		break;
1588 	case SRB_CT_PTHRU_CMD:
1589 		/* borrowing sts_entry_24xx.comp_status.
1590 		   same location as ct_entry_24xx.comp_status
1591 		 */
1592 		res = qla2x00_chk_ms_status(sp->vha, (ms_iocb_entry_t *)pkt,
1593 			(struct ct_sns_rsp *)sp->u.iocb_cmd.u.ctarg.rsp,
1594 			sp->name);
1595 		sp->done(sp, res);
1596 		return;
1597 	default:
1598 		ql_dbg(ql_dbg_user, vha, 0x503e,
1599 		    "Unrecognized SRB: (%p) type=%d.\n", sp, sp->type);
1600 		return;
1601 	}
1602 
1603 	comp_status = fw_status[0] = le16_to_cpu(pkt->comp_status);
1604 	fw_status[1] = le16_to_cpu(((struct els_sts_entry_24xx *)pkt)->error_subcode_1);
1605 	fw_status[2] = le16_to_cpu(((struct els_sts_entry_24xx *)pkt)->error_subcode_2);
1606 
1607 	if (iocb_type == ELS_IOCB_TYPE) {
1608 		els = &sp->u.iocb_cmd;
1609 		els->u.els_plogi.fw_status[0] = fw_status[0];
1610 		els->u.els_plogi.fw_status[1] = fw_status[1];
1611 		els->u.els_plogi.fw_status[2] = fw_status[2];
1612 		els->u.els_plogi.comp_status = fw_status[0];
1613 		if (comp_status == CS_COMPLETE) {
1614 			res =  DID_OK << 16;
1615 		} else {
1616 			if (comp_status == CS_DATA_UNDERRUN) {
1617 				res =  DID_OK << 16;
1618 				els->u.els_plogi.len =
1619 				le16_to_cpu(((struct els_sts_entry_24xx *)
1620 					pkt)->total_byte_count);
1621 			} else {
1622 				els->u.els_plogi.len = 0;
1623 				res = DID_ERROR << 16;
1624 			}
1625 		}
1626 		ql_dbg(ql_dbg_user, vha, 0x503f,
1627 		    "ELS IOCB Done -%s error hdl=%x comp_status=0x%x error subcode 1=0x%x error subcode 2=0x%x total_byte=0x%x\n",
1628 		    type, sp->handle, comp_status, fw_status[1], fw_status[2],
1629 		    le16_to_cpu(((struct els_sts_entry_24xx *)
1630 			pkt)->total_byte_count));
1631 		goto els_ct_done;
1632 	}
1633 
1634 	/* return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
1635 	 * fc payload  to the caller
1636 	 */
1637 	bsg_job = sp->u.bsg_job;
1638 	bsg_reply = bsg_job->reply;
1639 	bsg_reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
1640 	bsg_job->reply_len = sizeof(struct fc_bsg_reply) + sizeof(fw_status);
1641 
1642 	if (comp_status != CS_COMPLETE) {
1643 		if (comp_status == CS_DATA_UNDERRUN) {
1644 			res = DID_OK << 16;
1645 			bsg_reply->reply_payload_rcv_len =
1646 			    le16_to_cpu(((struct els_sts_entry_24xx *)pkt)->total_byte_count);
1647 
1648 			ql_dbg(ql_dbg_user, vha, 0x503f,
1649 			    "ELS-CT pass-through-%s error hdl=%x comp_status-status=0x%x "
1650 			    "error subcode 1=0x%x error subcode 2=0x%x total_byte = 0x%x.\n",
1651 			    type, sp->handle, comp_status, fw_status[1], fw_status[2],
1652 			    le16_to_cpu(((struct els_sts_entry_24xx *)
1653 				pkt)->total_byte_count));
1654 		} else {
1655 			ql_dbg(ql_dbg_user, vha, 0x5040,
1656 			    "ELS-CT pass-through-%s error hdl=%x comp_status-status=0x%x "
1657 			    "error subcode 1=0x%x error subcode 2=0x%x.\n",
1658 			    type, sp->handle, comp_status,
1659 			    le16_to_cpu(((struct els_sts_entry_24xx *)
1660 				pkt)->error_subcode_1),
1661 			    le16_to_cpu(((struct els_sts_entry_24xx *)
1662 				    pkt)->error_subcode_2));
1663 			res = DID_ERROR << 16;
1664 			bsg_reply->reply_payload_rcv_len = 0;
1665 		}
1666 		memcpy(bsg_job->reply + sizeof(struct fc_bsg_reply),
1667 		       fw_status, sizeof(fw_status));
1668 		ql_dump_buffer(ql_dbg_user + ql_dbg_buffer, vha, 0x5056,
1669 		    pkt, sizeof(*pkt));
1670 	}
1671 	else {
1672 		res =  DID_OK << 16;
1673 		bsg_reply->reply_payload_rcv_len = bsg_job->reply_payload.payload_len;
1674 		bsg_job->reply_len = 0;
1675 	}
1676 els_ct_done:
1677 
1678 	sp->done(sp, res);
1679 }
1680 
1681 static void
1682 qla24xx_logio_entry(scsi_qla_host_t *vha, struct req_que *req,
1683     struct logio_entry_24xx *logio)
1684 {
1685 	const char func[] = "LOGIO-IOCB";
1686 	const char *type;
1687 	fc_port_t *fcport;
1688 	srb_t *sp;
1689 	struct srb_iocb *lio;
1690 	uint16_t *data;
1691 	uint32_t iop[2];
1692 
1693 	sp = qla2x00_get_sp_from_handle(vha, func, req, logio);
1694 	if (!sp)
1695 		return;
1696 
1697 	lio = &sp->u.iocb_cmd;
1698 	type = sp->name;
1699 	fcport = sp->fcport;
1700 	data = lio->u.logio.data;
1701 
1702 	data[0] = MBS_COMMAND_ERROR;
1703 	data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
1704 		QLA_LOGIO_LOGIN_RETRIED : 0;
1705 	if (logio->entry_status) {
1706 		ql_log(ql_log_warn, fcport->vha, 0x5034,
1707 		    "Async-%s error entry - %8phC hdl=%x"
1708 		    "portid=%02x%02x%02x entry-status=%x.\n",
1709 		    type, fcport->port_name, sp->handle, fcport->d_id.b.domain,
1710 		    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1711 		    logio->entry_status);
1712 		ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x504d,
1713 		    logio, sizeof(*logio));
1714 
1715 		goto logio_done;
1716 	}
1717 
1718 	if (le16_to_cpu(logio->comp_status) == CS_COMPLETE) {
1719 		ql_dbg(ql_dbg_async, fcport->vha, 0x5036,
1720 		    "Async-%s complete - %8phC hdl=%x portid=%02x%02x%02x "
1721 		    "iop0=%x.\n", type, fcport->port_name, sp->handle,
1722 		    fcport->d_id.b.domain,
1723 		    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1724 		    le32_to_cpu(logio->io_parameter[0]));
1725 
1726 		vha->hw->exch_starvation = 0;
1727 		data[0] = MBS_COMMAND_COMPLETE;
1728 
1729 		if (sp->type == SRB_PRLI_CMD) {
1730 			lio->u.logio.iop[0] =
1731 			    le32_to_cpu(logio->io_parameter[0]);
1732 			lio->u.logio.iop[1] =
1733 			    le32_to_cpu(logio->io_parameter[1]);
1734 			goto logio_done;
1735 		}
1736 
1737 		if (sp->type != SRB_LOGIN_CMD)
1738 			goto logio_done;
1739 
1740 		iop[0] = le32_to_cpu(logio->io_parameter[0]);
1741 		if (iop[0] & BIT_4) {
1742 			fcport->port_type = FCT_TARGET;
1743 			if (iop[0] & BIT_8)
1744 				fcport->flags |= FCF_FCP2_DEVICE;
1745 		} else if (iop[0] & BIT_5)
1746 			fcport->port_type = FCT_INITIATOR;
1747 
1748 		if (iop[0] & BIT_7)
1749 			fcport->flags |= FCF_CONF_COMP_SUPPORTED;
1750 
1751 		if (logio->io_parameter[7] || logio->io_parameter[8])
1752 			fcport->supported_classes |= FC_COS_CLASS2;
1753 		if (logio->io_parameter[9] || logio->io_parameter[10])
1754 			fcport->supported_classes |= FC_COS_CLASS3;
1755 
1756 		goto logio_done;
1757 	}
1758 
1759 	iop[0] = le32_to_cpu(logio->io_parameter[0]);
1760 	iop[1] = le32_to_cpu(logio->io_parameter[1]);
1761 	lio->u.logio.iop[0] = iop[0];
1762 	lio->u.logio.iop[1] = iop[1];
1763 	switch (iop[0]) {
1764 	case LSC_SCODE_PORTID_USED:
1765 		data[0] = MBS_PORT_ID_USED;
1766 		data[1] = LSW(iop[1]);
1767 		break;
1768 	case LSC_SCODE_NPORT_USED:
1769 		data[0] = MBS_LOOP_ID_USED;
1770 		break;
1771 	case LSC_SCODE_CMD_FAILED:
1772 		if (iop[1] == 0x0606) {
1773 			/*
1774 			 * PLOGI/PRLI Completed. We must have Recv PLOGI/PRLI,
1775 			 * Target side acked.
1776 			 */
1777 			data[0] = MBS_COMMAND_COMPLETE;
1778 			goto logio_done;
1779 		}
1780 		data[0] = MBS_COMMAND_ERROR;
1781 		break;
1782 	case LSC_SCODE_NOXCB:
1783 		vha->hw->exch_starvation++;
1784 		if (vha->hw->exch_starvation > 5) {
1785 			ql_log(ql_log_warn, vha, 0xd046,
1786 			    "Exchange starvation. Resetting RISC\n");
1787 
1788 			vha->hw->exch_starvation = 0;
1789 
1790 			if (IS_P3P_TYPE(vha->hw))
1791 				set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1792 			else
1793 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1794 			qla2xxx_wake_dpc(vha);
1795 		}
1796 		/* fall through */
1797 	default:
1798 		data[0] = MBS_COMMAND_ERROR;
1799 		break;
1800 	}
1801 
1802 	ql_dbg(ql_dbg_async, fcport->vha, 0x5037,
1803 	    "Async-%s failed - %8phC hdl=%x portid=%02x%02x%02x comp=%x "
1804 	    "iop0=%x iop1=%x.\n", type, fcport->port_name,
1805 		sp->handle, fcport->d_id.b.domain,
1806 	    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1807 	    le16_to_cpu(logio->comp_status),
1808 	    le32_to_cpu(logio->io_parameter[0]),
1809 	    le32_to_cpu(logio->io_parameter[1]));
1810 
1811 logio_done:
1812 	sp->done(sp, 0);
1813 }
1814 
1815 static void
1816 qla24xx_tm_iocb_entry(scsi_qla_host_t *vha, struct req_que *req, void *tsk)
1817 {
1818 	const char func[] = "TMF-IOCB";
1819 	const char *type;
1820 	fc_port_t *fcport;
1821 	srb_t *sp;
1822 	struct srb_iocb *iocb;
1823 	struct sts_entry_24xx *sts = (struct sts_entry_24xx *)tsk;
1824 
1825 	sp = qla2x00_get_sp_from_handle(vha, func, req, tsk);
1826 	if (!sp)
1827 		return;
1828 
1829 	iocb = &sp->u.iocb_cmd;
1830 	type = sp->name;
1831 	fcport = sp->fcport;
1832 	iocb->u.tmf.data = QLA_SUCCESS;
1833 
1834 	if (sts->entry_status) {
1835 		ql_log(ql_log_warn, fcport->vha, 0x5038,
1836 		    "Async-%s error - hdl=%x entry-status(%x).\n",
1837 		    type, sp->handle, sts->entry_status);
1838 		iocb->u.tmf.data = QLA_FUNCTION_FAILED;
1839 	} else if (sts->comp_status != cpu_to_le16(CS_COMPLETE)) {
1840 		ql_log(ql_log_warn, fcport->vha, 0x5039,
1841 		    "Async-%s error - hdl=%x completion status(%x).\n",
1842 		    type, sp->handle, sts->comp_status);
1843 		iocb->u.tmf.data = QLA_FUNCTION_FAILED;
1844 	} else if ((le16_to_cpu(sts->scsi_status) &
1845 	    SS_RESPONSE_INFO_LEN_VALID)) {
1846 		if (le32_to_cpu(sts->rsp_data_len) < 4) {
1847 			ql_log(ql_log_warn, fcport->vha, 0x503b,
1848 			    "Async-%s error - hdl=%x not enough response(%d).\n",
1849 			    type, sp->handle, sts->rsp_data_len);
1850 		} else if (sts->data[3]) {
1851 			ql_log(ql_log_warn, fcport->vha, 0x503c,
1852 			    "Async-%s error - hdl=%x response(%x).\n",
1853 			    type, sp->handle, sts->data[3]);
1854 			iocb->u.tmf.data = QLA_FUNCTION_FAILED;
1855 		}
1856 	}
1857 
1858 	if (iocb->u.tmf.data != QLA_SUCCESS)
1859 		ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, sp->vha, 0x5055,
1860 		    sts, sizeof(*sts));
1861 
1862 	sp->done(sp, 0);
1863 }
1864 
1865 static void qla24xx_nvme_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1866     void *tsk, srb_t *sp)
1867 {
1868 	fc_port_t *fcport;
1869 	struct srb_iocb *iocb;
1870 	struct sts_entry_24xx *sts = (struct sts_entry_24xx *)tsk;
1871 	uint16_t        state_flags;
1872 	struct nvmefc_fcp_req *fd;
1873 	uint16_t        ret = QLA_SUCCESS;
1874 	uint16_t	comp_status = le16_to_cpu(sts->comp_status);
1875 
1876 	iocb = &sp->u.iocb_cmd;
1877 	fcport = sp->fcport;
1878 	iocb->u.nvme.comp_status = comp_status;
1879 	state_flags  = le16_to_cpu(sts->state_flags);
1880 	fd = iocb->u.nvme.desc;
1881 
1882 	if (unlikely(iocb->u.nvme.aen_op))
1883 		atomic_dec(&sp->vha->hw->nvme_active_aen_cnt);
1884 
1885 	/*
1886 	 * State flags: Bit 6 and 0.
1887 	 * If 0 is set, we don't care about 6.
1888 	 * both cases resp was dma'd to host buffer
1889 	 * if both are 0, that is good path case.
1890 	 * if six is set and 0 is clear, we need to
1891 	 * copy resp data from status iocb to resp buffer.
1892 	 */
1893 	if (!(state_flags & (SF_FCP_RSP_DMA | SF_NVME_ERSP))) {
1894 		iocb->u.nvme.rsp_pyld_len = 0;
1895 	} else if ((state_flags & SF_FCP_RSP_DMA)) {
1896 		iocb->u.nvme.rsp_pyld_len = le16_to_cpu(sts->nvme_rsp_pyld_len);
1897 	} else if (state_flags & SF_NVME_ERSP) {
1898 		uint32_t *inbuf, *outbuf;
1899 		uint16_t iter;
1900 
1901 		inbuf = (uint32_t *)&sts->nvme_ersp_data;
1902 		outbuf = (uint32_t *)fd->rspaddr;
1903 		iocb->u.nvme.rsp_pyld_len = le16_to_cpu(sts->nvme_rsp_pyld_len);
1904 		iter = iocb->u.nvme.rsp_pyld_len >> 2;
1905 		for (; iter; iter--)
1906 			*outbuf++ = swab32(*inbuf++);
1907 	} else { /* unhandled case */
1908 	    ql_log(ql_log_warn, fcport->vha, 0x503a,
1909 		"NVME-%s error. Unhandled state_flags of %x\n",
1910 		sp->name, state_flags);
1911 	}
1912 
1913 	fd->transferred_length = fd->payload_length -
1914 	    le32_to_cpu(sts->residual_len);
1915 
1916 	if (unlikely(comp_status != CS_COMPLETE))
1917 		ql_log(ql_log_warn, fcport->vha, 0x5060,
1918 		   "NVME-%s ERR Handling - hdl=%x status(%x) tr_len:%x resid=%x  ox_id=%x\n",
1919 		   sp->name, sp->handle, comp_status,
1920 		   fd->transferred_length, le32_to_cpu(sts->residual_len),
1921 		   sts->ox_id);
1922 
1923 	/*
1924 	 * If transport error then Failure (HBA rejects request)
1925 	 * otherwise transport will handle.
1926 	 */
1927 	switch (comp_status) {
1928 	case CS_COMPLETE:
1929 		break;
1930 
1931 	case CS_RESET:
1932 	case CS_PORT_UNAVAILABLE:
1933 	case CS_PORT_LOGGED_OUT:
1934 		fcport->nvme_flag |= NVME_FLAG_RESETTING;
1935 		/* fall through */
1936 	case CS_ABORTED:
1937 	case CS_PORT_BUSY:
1938 		fd->transferred_length = 0;
1939 		iocb->u.nvme.rsp_pyld_len = 0;
1940 		ret = QLA_ABORTED;
1941 		break;
1942 	case CS_DATA_UNDERRUN:
1943 		break;
1944 	default:
1945 		ret = QLA_FUNCTION_FAILED;
1946 		break;
1947 	}
1948 	sp->done(sp, ret);
1949 }
1950 
1951 static void qla_ctrlvp_completed(scsi_qla_host_t *vha, struct req_que *req,
1952     struct vp_ctrl_entry_24xx *vce)
1953 {
1954 	const char func[] = "CTRLVP-IOCB";
1955 	srb_t *sp;
1956 	int rval = QLA_SUCCESS;
1957 
1958 	sp = qla2x00_get_sp_from_handle(vha, func, req, vce);
1959 	if (!sp)
1960 		return;
1961 
1962 	if (vce->entry_status != 0) {
1963 		ql_dbg(ql_dbg_vport, vha, 0x10c4,
1964 		    "%s: Failed to complete IOCB -- error status (%x)\n",
1965 		    sp->name, vce->entry_status);
1966 		rval = QLA_FUNCTION_FAILED;
1967 	} else if (vce->comp_status != cpu_to_le16(CS_COMPLETE)) {
1968 		ql_dbg(ql_dbg_vport, vha, 0x10c5,
1969 		    "%s: Failed to complete IOCB -- completion status (%x) vpidx %x\n",
1970 		    sp->name, le16_to_cpu(vce->comp_status),
1971 		    le16_to_cpu(vce->vp_idx_failed));
1972 		rval = QLA_FUNCTION_FAILED;
1973 	} else {
1974 		ql_dbg(ql_dbg_vport, vha, 0x10c6,
1975 		    "Done %s.\n", __func__);
1976 	}
1977 
1978 	sp->rc = rval;
1979 	sp->done(sp, rval);
1980 }
1981 
1982 /* Process a single response queue entry. */
1983 static void qla2x00_process_response_entry(struct scsi_qla_host *vha,
1984 					   struct rsp_que *rsp,
1985 					   sts_entry_t *pkt)
1986 {
1987 	sts21_entry_t *sts21_entry;
1988 	sts22_entry_t *sts22_entry;
1989 	uint16_t handle_cnt;
1990 	uint16_t cnt;
1991 
1992 	switch (pkt->entry_type) {
1993 	case STATUS_TYPE:
1994 		qla2x00_status_entry(vha, rsp, pkt);
1995 		break;
1996 	case STATUS_TYPE_21:
1997 		sts21_entry = (sts21_entry_t *)pkt;
1998 		handle_cnt = sts21_entry->handle_count;
1999 		for (cnt = 0; cnt < handle_cnt; cnt++)
2000 			qla2x00_process_completed_request(vha, rsp->req,
2001 						sts21_entry->handle[cnt]);
2002 		break;
2003 	case STATUS_TYPE_22:
2004 		sts22_entry = (sts22_entry_t *)pkt;
2005 		handle_cnt = sts22_entry->handle_count;
2006 		for (cnt = 0; cnt < handle_cnt; cnt++)
2007 			qla2x00_process_completed_request(vha, rsp->req,
2008 						sts22_entry->handle[cnt]);
2009 		break;
2010 	case STATUS_CONT_TYPE:
2011 		qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
2012 		break;
2013 	case MBX_IOCB_TYPE:
2014 		qla2x00_mbx_iocb_entry(vha, rsp->req, (struct mbx_entry *)pkt);
2015 		break;
2016 	case CT_IOCB_TYPE:
2017 		qla2x00_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
2018 		break;
2019 	default:
2020 		/* Type Not Supported. */
2021 		ql_log(ql_log_warn, vha, 0x504a,
2022 		       "Received unknown response pkt type %x entry status=%x.\n",
2023 		       pkt->entry_type, pkt->entry_status);
2024 		break;
2025 	}
2026 }
2027 
2028 /**
2029  * qla2x00_process_response_queue() - Process response queue entries.
2030  * @rsp: response queue
2031  */
2032 void
2033 qla2x00_process_response_queue(struct rsp_que *rsp)
2034 {
2035 	struct scsi_qla_host *vha;
2036 	struct qla_hw_data *ha = rsp->hw;
2037 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2038 	sts_entry_t	*pkt;
2039 
2040 	vha = pci_get_drvdata(ha->pdev);
2041 
2042 	if (!vha->flags.online)
2043 		return;
2044 
2045 	while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
2046 		pkt = (sts_entry_t *)rsp->ring_ptr;
2047 
2048 		rsp->ring_index++;
2049 		if (rsp->ring_index == rsp->length) {
2050 			rsp->ring_index = 0;
2051 			rsp->ring_ptr = rsp->ring;
2052 		} else {
2053 			rsp->ring_ptr++;
2054 		}
2055 
2056 		if (pkt->entry_status != 0) {
2057 			qla2x00_error_entry(vha, rsp, pkt);
2058 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
2059 			wmb();
2060 			continue;
2061 		}
2062 
2063 		qla2x00_process_response_entry(vha, rsp, pkt);
2064 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
2065 		wmb();
2066 	}
2067 
2068 	/* Adjust ring index */
2069 	WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), rsp->ring_index);
2070 }
2071 
2072 static inline void
2073 qla2x00_handle_sense(srb_t *sp, uint8_t *sense_data, uint32_t par_sense_len,
2074 		     uint32_t sense_len, struct rsp_que *rsp, int res)
2075 {
2076 	struct scsi_qla_host *vha = sp->vha;
2077 	struct scsi_cmnd *cp = GET_CMD_SP(sp);
2078 	uint32_t track_sense_len;
2079 
2080 	if (sense_len >= SCSI_SENSE_BUFFERSIZE)
2081 		sense_len = SCSI_SENSE_BUFFERSIZE;
2082 
2083 	SET_CMD_SENSE_LEN(sp, sense_len);
2084 	SET_CMD_SENSE_PTR(sp, cp->sense_buffer);
2085 	track_sense_len = sense_len;
2086 
2087 	if (sense_len > par_sense_len)
2088 		sense_len = par_sense_len;
2089 
2090 	memcpy(cp->sense_buffer, sense_data, sense_len);
2091 
2092 	SET_CMD_SENSE_PTR(sp, cp->sense_buffer + sense_len);
2093 	track_sense_len -= sense_len;
2094 	SET_CMD_SENSE_LEN(sp, track_sense_len);
2095 
2096 	if (track_sense_len != 0) {
2097 		rsp->status_srb = sp;
2098 		cp->result = res;
2099 	}
2100 
2101 	if (sense_len) {
2102 		ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x301c,
2103 		    "Check condition Sense data, nexus%ld:%d:%llu cmd=%p.\n",
2104 		    sp->vha->host_no, cp->device->id, cp->device->lun,
2105 		    cp);
2106 		ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302b,
2107 		    cp->sense_buffer, sense_len);
2108 	}
2109 }
2110 
2111 struct scsi_dif_tuple {
2112 	__be16 guard;       /* Checksum */
2113 	__be16 app_tag;         /* APPL identifier */
2114 	__be32 ref_tag;         /* Target LBA or indirect LBA */
2115 };
2116 
2117 /*
2118  * Checks the guard or meta-data for the type of error
2119  * detected by the HBA. In case of errors, we set the
2120  * ASC/ASCQ fields in the sense buffer with ILLEGAL_REQUEST
2121  * to indicate to the kernel that the HBA detected error.
2122  */
2123 static inline int
2124 qla2x00_handle_dif_error(srb_t *sp, struct sts_entry_24xx *sts24)
2125 {
2126 	struct scsi_qla_host *vha = sp->vha;
2127 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
2128 	uint8_t		*ap = &sts24->data[12];
2129 	uint8_t		*ep = &sts24->data[20];
2130 	uint32_t	e_ref_tag, a_ref_tag;
2131 	uint16_t	e_app_tag, a_app_tag;
2132 	uint16_t	e_guard, a_guard;
2133 
2134 	/*
2135 	 * swab32 of the "data" field in the beginning of qla2x00_status_entry()
2136 	 * would make guard field appear at offset 2
2137 	 */
2138 	a_guard   = le16_to_cpu(*(uint16_t *)(ap + 2));
2139 	a_app_tag = le16_to_cpu(*(uint16_t *)(ap + 0));
2140 	a_ref_tag = le32_to_cpu(*(uint32_t *)(ap + 4));
2141 	e_guard   = le16_to_cpu(*(uint16_t *)(ep + 2));
2142 	e_app_tag = le16_to_cpu(*(uint16_t *)(ep + 0));
2143 	e_ref_tag = le32_to_cpu(*(uint32_t *)(ep + 4));
2144 
2145 	ql_dbg(ql_dbg_io, vha, 0x3023,
2146 	    "iocb(s) %p Returned STATUS.\n", sts24);
2147 
2148 	ql_dbg(ql_dbg_io, vha, 0x3024,
2149 	    "DIF ERROR in cmd 0x%x lba 0x%llx act ref"
2150 	    " tag=0x%x, exp ref_tag=0x%x, act app tag=0x%x, exp app"
2151 	    " tag=0x%x, act guard=0x%x, exp guard=0x%x.\n",
2152 	    cmd->cmnd[0], (u64)scsi_get_lba(cmd), a_ref_tag, e_ref_tag,
2153 	    a_app_tag, e_app_tag, a_guard, e_guard);
2154 
2155 	/*
2156 	 * Ignore sector if:
2157 	 * For type     3: ref & app tag is all 'f's
2158 	 * For type 0,1,2: app tag is all 'f's
2159 	 */
2160 	if ((a_app_tag == T10_PI_APP_ESCAPE) &&
2161 	    ((scsi_get_prot_type(cmd) != SCSI_PROT_DIF_TYPE3) ||
2162 	     (a_ref_tag == T10_PI_REF_ESCAPE))) {
2163 		uint32_t blocks_done, resid;
2164 		sector_t lba_s = scsi_get_lba(cmd);
2165 
2166 		/* 2TB boundary case covered automatically with this */
2167 		blocks_done = e_ref_tag - (uint32_t)lba_s + 1;
2168 
2169 		resid = scsi_bufflen(cmd) - (blocks_done *
2170 		    cmd->device->sector_size);
2171 
2172 		scsi_set_resid(cmd, resid);
2173 		cmd->result = DID_OK << 16;
2174 
2175 		/* Update protection tag */
2176 		if (scsi_prot_sg_count(cmd)) {
2177 			uint32_t i, j = 0, k = 0, num_ent;
2178 			struct scatterlist *sg;
2179 			struct t10_pi_tuple *spt;
2180 
2181 			/* Patch the corresponding protection tags */
2182 			scsi_for_each_prot_sg(cmd, sg,
2183 			    scsi_prot_sg_count(cmd), i) {
2184 				num_ent = sg_dma_len(sg) / 8;
2185 				if (k + num_ent < blocks_done) {
2186 					k += num_ent;
2187 					continue;
2188 				}
2189 				j = blocks_done - k - 1;
2190 				k = blocks_done;
2191 				break;
2192 			}
2193 
2194 			if (k != blocks_done) {
2195 				ql_log(ql_log_warn, vha, 0x302f,
2196 				    "unexpected tag values tag:lba=%x:%llx)\n",
2197 				    e_ref_tag, (unsigned long long)lba_s);
2198 				return 1;
2199 			}
2200 
2201 			spt = page_address(sg_page(sg)) + sg->offset;
2202 			spt += j;
2203 
2204 			spt->app_tag = T10_PI_APP_ESCAPE;
2205 			if (scsi_get_prot_type(cmd) == SCSI_PROT_DIF_TYPE3)
2206 				spt->ref_tag = T10_PI_REF_ESCAPE;
2207 		}
2208 
2209 		return 0;
2210 	}
2211 
2212 	/* check guard */
2213 	if (e_guard != a_guard) {
2214 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2215 		    0x10, 0x1);
2216 		set_driver_byte(cmd, DRIVER_SENSE);
2217 		set_host_byte(cmd, DID_ABORT);
2218 		cmd->result |= SAM_STAT_CHECK_CONDITION;
2219 		return 1;
2220 	}
2221 
2222 	/* check ref tag */
2223 	if (e_ref_tag != a_ref_tag) {
2224 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2225 		    0x10, 0x3);
2226 		set_driver_byte(cmd, DRIVER_SENSE);
2227 		set_host_byte(cmd, DID_ABORT);
2228 		cmd->result |= SAM_STAT_CHECK_CONDITION;
2229 		return 1;
2230 	}
2231 
2232 	/* check appl tag */
2233 	if (e_app_tag != a_app_tag) {
2234 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2235 		    0x10, 0x2);
2236 		set_driver_byte(cmd, DRIVER_SENSE);
2237 		set_host_byte(cmd, DID_ABORT);
2238 		cmd->result |= SAM_STAT_CHECK_CONDITION;
2239 		return 1;
2240 	}
2241 
2242 	return 1;
2243 }
2244 
2245 static void
2246 qla25xx_process_bidir_status_iocb(scsi_qla_host_t *vha, void *pkt,
2247 				  struct req_que *req, uint32_t index)
2248 {
2249 	struct qla_hw_data *ha = vha->hw;
2250 	srb_t *sp;
2251 	uint16_t	comp_status;
2252 	uint16_t	scsi_status;
2253 	uint16_t thread_id;
2254 	uint32_t rval = EXT_STATUS_OK;
2255 	struct bsg_job *bsg_job = NULL;
2256 	struct fc_bsg_request *bsg_request;
2257 	struct fc_bsg_reply *bsg_reply;
2258 	sts_entry_t *sts = pkt;
2259 	struct sts_entry_24xx *sts24 = pkt;
2260 
2261 	/* Validate handle. */
2262 	if (index >= req->num_outstanding_cmds) {
2263 		ql_log(ql_log_warn, vha, 0x70af,
2264 		    "Invalid SCSI completion handle 0x%x.\n", index);
2265 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2266 		return;
2267 	}
2268 
2269 	sp = req->outstanding_cmds[index];
2270 	if (!sp) {
2271 		ql_log(ql_log_warn, vha, 0x70b0,
2272 		    "Req:%d: Invalid ISP SCSI completion handle(0x%x)\n",
2273 		    req->id, index);
2274 
2275 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2276 		return;
2277 	}
2278 
2279 	/* Free outstanding command slot. */
2280 	req->outstanding_cmds[index] = NULL;
2281 	bsg_job = sp->u.bsg_job;
2282 	bsg_request = bsg_job->request;
2283 	bsg_reply = bsg_job->reply;
2284 
2285 	if (IS_FWI2_CAPABLE(ha)) {
2286 		comp_status = le16_to_cpu(sts24->comp_status);
2287 		scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
2288 	} else {
2289 		comp_status = le16_to_cpu(sts->comp_status);
2290 		scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
2291 	}
2292 
2293 	thread_id = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
2294 	switch (comp_status) {
2295 	case CS_COMPLETE:
2296 		if (scsi_status == 0) {
2297 			bsg_reply->reply_payload_rcv_len =
2298 					bsg_job->reply_payload.payload_len;
2299 			vha->qla_stats.input_bytes +=
2300 				bsg_reply->reply_payload_rcv_len;
2301 			vha->qla_stats.input_requests++;
2302 			rval = EXT_STATUS_OK;
2303 		}
2304 		goto done;
2305 
2306 	case CS_DATA_OVERRUN:
2307 		ql_dbg(ql_dbg_user, vha, 0x70b1,
2308 		    "Command completed with data overrun thread_id=%d\n",
2309 		    thread_id);
2310 		rval = EXT_STATUS_DATA_OVERRUN;
2311 		break;
2312 
2313 	case CS_DATA_UNDERRUN:
2314 		ql_dbg(ql_dbg_user, vha, 0x70b2,
2315 		    "Command completed with data underrun thread_id=%d\n",
2316 		    thread_id);
2317 		rval = EXT_STATUS_DATA_UNDERRUN;
2318 		break;
2319 	case CS_BIDIR_RD_OVERRUN:
2320 		ql_dbg(ql_dbg_user, vha, 0x70b3,
2321 		    "Command completed with read data overrun thread_id=%d\n",
2322 		    thread_id);
2323 		rval = EXT_STATUS_DATA_OVERRUN;
2324 		break;
2325 
2326 	case CS_BIDIR_RD_WR_OVERRUN:
2327 		ql_dbg(ql_dbg_user, vha, 0x70b4,
2328 		    "Command completed with read and write data overrun "
2329 		    "thread_id=%d\n", thread_id);
2330 		rval = EXT_STATUS_DATA_OVERRUN;
2331 		break;
2332 
2333 	case CS_BIDIR_RD_OVERRUN_WR_UNDERRUN:
2334 		ql_dbg(ql_dbg_user, vha, 0x70b5,
2335 		    "Command completed with read data over and write data "
2336 		    "underrun thread_id=%d\n", thread_id);
2337 		rval = EXT_STATUS_DATA_OVERRUN;
2338 		break;
2339 
2340 	case CS_BIDIR_RD_UNDERRUN:
2341 		ql_dbg(ql_dbg_user, vha, 0x70b6,
2342 		    "Command completed with read data underrun "
2343 		    "thread_id=%d\n", thread_id);
2344 		rval = EXT_STATUS_DATA_UNDERRUN;
2345 		break;
2346 
2347 	case CS_BIDIR_RD_UNDERRUN_WR_OVERRUN:
2348 		ql_dbg(ql_dbg_user, vha, 0x70b7,
2349 		    "Command completed with read data under and write data "
2350 		    "overrun thread_id=%d\n", thread_id);
2351 		rval = EXT_STATUS_DATA_UNDERRUN;
2352 		break;
2353 
2354 	case CS_BIDIR_RD_WR_UNDERRUN:
2355 		ql_dbg(ql_dbg_user, vha, 0x70b8,
2356 		    "Command completed with read and write data underrun "
2357 		    "thread_id=%d\n", thread_id);
2358 		rval = EXT_STATUS_DATA_UNDERRUN;
2359 		break;
2360 
2361 	case CS_BIDIR_DMA:
2362 		ql_dbg(ql_dbg_user, vha, 0x70b9,
2363 		    "Command completed with data DMA error thread_id=%d\n",
2364 		    thread_id);
2365 		rval = EXT_STATUS_DMA_ERR;
2366 		break;
2367 
2368 	case CS_TIMEOUT:
2369 		ql_dbg(ql_dbg_user, vha, 0x70ba,
2370 		    "Command completed with timeout thread_id=%d\n",
2371 		    thread_id);
2372 		rval = EXT_STATUS_TIMEOUT;
2373 		break;
2374 	default:
2375 		ql_dbg(ql_dbg_user, vha, 0x70bb,
2376 		    "Command completed with completion status=0x%x "
2377 		    "thread_id=%d\n", comp_status, thread_id);
2378 		rval = EXT_STATUS_ERR;
2379 		break;
2380 	}
2381 	bsg_reply->reply_payload_rcv_len = 0;
2382 
2383 done:
2384 	/* Return the vendor specific reply to API */
2385 	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
2386 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2387 	/* Always return DID_OK, bsg will send the vendor specific response
2388 	 * in this case only */
2389 	sp->done(sp, DID_OK << 16);
2390 
2391 }
2392 
2393 /**
2394  * qla2x00_status_entry() - Process a Status IOCB entry.
2395  * @vha: SCSI driver HA context
2396  * @rsp: response queue
2397  * @pkt: Entry pointer
2398  */
2399 static void
2400 qla2x00_status_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, void *pkt)
2401 {
2402 	srb_t		*sp;
2403 	fc_port_t	*fcport;
2404 	struct scsi_cmnd *cp;
2405 	sts_entry_t *sts = pkt;
2406 	struct sts_entry_24xx *sts24 = pkt;
2407 	uint16_t	comp_status;
2408 	uint16_t	scsi_status;
2409 	uint16_t	ox_id;
2410 	uint8_t		lscsi_status;
2411 	int32_t		resid;
2412 	uint32_t sense_len, par_sense_len, rsp_info_len, resid_len,
2413 	    fw_resid_len;
2414 	uint8_t		*rsp_info, *sense_data;
2415 	struct qla_hw_data *ha = vha->hw;
2416 	uint32_t handle;
2417 	uint16_t que;
2418 	struct req_que *req;
2419 	int logit = 1;
2420 	int res = 0;
2421 	uint16_t state_flags = 0;
2422 	uint16_t retry_delay = 0;
2423 
2424 	if (IS_FWI2_CAPABLE(ha)) {
2425 		comp_status = le16_to_cpu(sts24->comp_status);
2426 		scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
2427 		state_flags = le16_to_cpu(sts24->state_flags);
2428 	} else {
2429 		comp_status = le16_to_cpu(sts->comp_status);
2430 		scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
2431 	}
2432 	handle = (uint32_t) LSW(sts->handle);
2433 	que = MSW(sts->handle);
2434 	req = ha->req_q_map[que];
2435 
2436 	/* Check for invalid queue pointer */
2437 	if (req == NULL ||
2438 	    que >= find_first_zero_bit(ha->req_qid_map, ha->max_req_queues)) {
2439 		ql_dbg(ql_dbg_io, vha, 0x3059,
2440 		    "Invalid status handle (0x%x): Bad req pointer. req=%p, "
2441 		    "que=%u.\n", sts->handle, req, que);
2442 		return;
2443 	}
2444 
2445 	/* Validate handle. */
2446 	if (handle < req->num_outstanding_cmds) {
2447 		sp = req->outstanding_cmds[handle];
2448 		if (!sp) {
2449 			ql_dbg(ql_dbg_io, vha, 0x3075,
2450 			    "%s(%ld): Already returned command for status handle (0x%x).\n",
2451 			    __func__, vha->host_no, sts->handle);
2452 			return;
2453 		}
2454 	} else {
2455 		ql_dbg(ql_dbg_io, vha, 0x3017,
2456 		    "Invalid status handle, out of range (0x%x).\n",
2457 		    sts->handle);
2458 
2459 		if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
2460 			if (IS_P3P_TYPE(ha))
2461 				set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
2462 			else
2463 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2464 			qla2xxx_wake_dpc(vha);
2465 		}
2466 		return;
2467 	}
2468 
2469 	if (sp->cmd_type != TYPE_SRB) {
2470 		req->outstanding_cmds[handle] = NULL;
2471 		ql_dbg(ql_dbg_io, vha, 0x3015,
2472 		    "Unknown sp->cmd_type %x %p).\n",
2473 		    sp->cmd_type, sp);
2474 		return;
2475 	}
2476 
2477 	/* NVME completion. */
2478 	if (sp->type == SRB_NVME_CMD) {
2479 		req->outstanding_cmds[handle] = NULL;
2480 		qla24xx_nvme_iocb_entry(vha, req, pkt, sp);
2481 		return;
2482 	}
2483 
2484 	if (unlikely((state_flags & BIT_1) && (sp->type == SRB_BIDI_CMD))) {
2485 		qla25xx_process_bidir_status_iocb(vha, pkt, req, handle);
2486 		return;
2487 	}
2488 
2489 	/* Task Management completion. */
2490 	if (sp->type == SRB_TM_CMD) {
2491 		qla24xx_tm_iocb_entry(vha, req, pkt);
2492 		return;
2493 	}
2494 
2495 	/* Fast path completion. */
2496 	if (comp_status == CS_COMPLETE && scsi_status == 0) {
2497 		qla2x00_process_completed_request(vha, req, handle);
2498 
2499 		return;
2500 	}
2501 
2502 	req->outstanding_cmds[handle] = NULL;
2503 	cp = GET_CMD_SP(sp);
2504 	if (cp == NULL) {
2505 		ql_dbg(ql_dbg_io, vha, 0x3018,
2506 		    "Command already returned (0x%x/%p).\n",
2507 		    sts->handle, sp);
2508 
2509 		return;
2510 	}
2511 
2512 	lscsi_status = scsi_status & STATUS_MASK;
2513 
2514 	fcport = sp->fcport;
2515 
2516 	ox_id = 0;
2517 	sense_len = par_sense_len = rsp_info_len = resid_len =
2518 	    fw_resid_len = 0;
2519 	if (IS_FWI2_CAPABLE(ha)) {
2520 		if (scsi_status & SS_SENSE_LEN_VALID)
2521 			sense_len = le32_to_cpu(sts24->sense_len);
2522 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
2523 			rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
2524 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER))
2525 			resid_len = le32_to_cpu(sts24->rsp_residual_count);
2526 		if (comp_status == CS_DATA_UNDERRUN)
2527 			fw_resid_len = le32_to_cpu(sts24->residual_len);
2528 		rsp_info = sts24->data;
2529 		sense_data = sts24->data;
2530 		host_to_fcp_swap(sts24->data, sizeof(sts24->data));
2531 		ox_id = le16_to_cpu(sts24->ox_id);
2532 		par_sense_len = sizeof(sts24->data);
2533 		/* Valid values of the retry delay timer are 0x1-0xffef */
2534 		if (sts24->retry_delay > 0 && sts24->retry_delay < 0xfff1) {
2535 			retry_delay = sts24->retry_delay & 0x3fff;
2536 			ql_dbg(ql_dbg_io, sp->vha, 0x3033,
2537 			    "%s: scope=%#x retry_delay=%#x\n", __func__,
2538 			    sts24->retry_delay >> 14, retry_delay);
2539 		}
2540 	} else {
2541 		if (scsi_status & SS_SENSE_LEN_VALID)
2542 			sense_len = le16_to_cpu(sts->req_sense_length);
2543 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
2544 			rsp_info_len = le16_to_cpu(sts->rsp_info_len);
2545 		resid_len = le32_to_cpu(sts->residual_length);
2546 		rsp_info = sts->rsp_info;
2547 		sense_data = sts->req_sense_data;
2548 		par_sense_len = sizeof(sts->req_sense_data);
2549 	}
2550 
2551 	/* Check for any FCP transport errors. */
2552 	if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
2553 		/* Sense data lies beyond any FCP RESPONSE data. */
2554 		if (IS_FWI2_CAPABLE(ha)) {
2555 			sense_data += rsp_info_len;
2556 			par_sense_len -= rsp_info_len;
2557 		}
2558 		if (rsp_info_len > 3 && rsp_info[3]) {
2559 			ql_dbg(ql_dbg_io, fcport->vha, 0x3019,
2560 			    "FCP I/O protocol failure (0x%x/0x%x).\n",
2561 			    rsp_info_len, rsp_info[3]);
2562 
2563 			res = DID_BUS_BUSY << 16;
2564 			goto out;
2565 		}
2566 	}
2567 
2568 	/* Check for overrun. */
2569 	if (IS_FWI2_CAPABLE(ha) && comp_status == CS_COMPLETE &&
2570 	    scsi_status & SS_RESIDUAL_OVER)
2571 		comp_status = CS_DATA_OVERRUN;
2572 
2573 	/*
2574 	 * Check retry_delay_timer value if we receive a busy or
2575 	 * queue full.
2576 	 */
2577 	if (lscsi_status == SAM_STAT_TASK_SET_FULL ||
2578 	    lscsi_status == SAM_STAT_BUSY)
2579 		qla2x00_set_retry_delay_timestamp(fcport, retry_delay);
2580 
2581 	/*
2582 	 * Based on Host and scsi status generate status code for Linux
2583 	 */
2584 	switch (comp_status) {
2585 	case CS_COMPLETE:
2586 	case CS_QUEUE_FULL:
2587 		if (scsi_status == 0) {
2588 			res = DID_OK << 16;
2589 			break;
2590 		}
2591 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
2592 			resid = resid_len;
2593 			scsi_set_resid(cp, resid);
2594 
2595 			if (!lscsi_status &&
2596 			    ((unsigned)(scsi_bufflen(cp) - resid) <
2597 			     cp->underflow)) {
2598 				ql_dbg(ql_dbg_io, fcport->vha, 0x301a,
2599 				    "Mid-layer underflow detected (0x%x of 0x%x bytes).\n",
2600 				    resid, scsi_bufflen(cp));
2601 
2602 				res = DID_ERROR << 16;
2603 				break;
2604 			}
2605 		}
2606 		res = DID_OK << 16 | lscsi_status;
2607 
2608 		if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
2609 			ql_dbg(ql_dbg_io, fcport->vha, 0x301b,
2610 			    "QUEUE FULL detected.\n");
2611 			break;
2612 		}
2613 		logit = 0;
2614 		if (lscsi_status != SS_CHECK_CONDITION)
2615 			break;
2616 
2617 		memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2618 		if (!(scsi_status & SS_SENSE_LEN_VALID))
2619 			break;
2620 
2621 		qla2x00_handle_sense(sp, sense_data, par_sense_len, sense_len,
2622 		    rsp, res);
2623 		break;
2624 
2625 	case CS_DATA_UNDERRUN:
2626 		/* Use F/W calculated residual length. */
2627 		resid = IS_FWI2_CAPABLE(ha) ? fw_resid_len : resid_len;
2628 		scsi_set_resid(cp, resid);
2629 		if (scsi_status & SS_RESIDUAL_UNDER) {
2630 			if (IS_FWI2_CAPABLE(ha) && fw_resid_len != resid_len) {
2631 				ql_dbg(ql_dbg_io, fcport->vha, 0x301d,
2632 				    "Dropped frame(s) detected (0x%x of 0x%x bytes).\n",
2633 				    resid, scsi_bufflen(cp));
2634 
2635 				res = DID_ERROR << 16 | lscsi_status;
2636 				goto check_scsi_status;
2637 			}
2638 
2639 			if (!lscsi_status &&
2640 			    ((unsigned)(scsi_bufflen(cp) - resid) <
2641 			    cp->underflow)) {
2642 				ql_dbg(ql_dbg_io, fcport->vha, 0x301e,
2643 				    "Mid-layer underflow detected (0x%x of 0x%x bytes).\n",
2644 				    resid, scsi_bufflen(cp));
2645 
2646 				res = DID_ERROR << 16;
2647 				break;
2648 			}
2649 		} else if (lscsi_status != SAM_STAT_TASK_SET_FULL &&
2650 			    lscsi_status != SAM_STAT_BUSY) {
2651 			/*
2652 			 * scsi status of task set and busy are considered to be
2653 			 * task not completed.
2654 			 */
2655 
2656 			ql_dbg(ql_dbg_io, fcport->vha, 0x301f,
2657 			    "Dropped frame(s) detected (0x%x of 0x%x bytes).\n",
2658 			    resid, scsi_bufflen(cp));
2659 
2660 			res = DID_ERROR << 16 | lscsi_status;
2661 			goto check_scsi_status;
2662 		} else {
2663 			ql_dbg(ql_dbg_io, fcport->vha, 0x3030,
2664 			    "scsi_status: 0x%x, lscsi_status: 0x%x\n",
2665 			    scsi_status, lscsi_status);
2666 		}
2667 
2668 		res = DID_OK << 16 | lscsi_status;
2669 		logit = 0;
2670 
2671 check_scsi_status:
2672 		/*
2673 		 * Check to see if SCSI Status is non zero. If so report SCSI
2674 		 * Status.
2675 		 */
2676 		if (lscsi_status != 0) {
2677 			if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
2678 				ql_dbg(ql_dbg_io, fcport->vha, 0x3020,
2679 				    "QUEUE FULL detected.\n");
2680 				logit = 1;
2681 				break;
2682 			}
2683 			if (lscsi_status != SS_CHECK_CONDITION)
2684 				break;
2685 
2686 			memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2687 			if (!(scsi_status & SS_SENSE_LEN_VALID))
2688 				break;
2689 
2690 			qla2x00_handle_sense(sp, sense_data, par_sense_len,
2691 			    sense_len, rsp, res);
2692 		}
2693 		break;
2694 
2695 	case CS_PORT_LOGGED_OUT:
2696 	case CS_PORT_CONFIG_CHG:
2697 	case CS_PORT_BUSY:
2698 	case CS_INCOMPLETE:
2699 	case CS_PORT_UNAVAILABLE:
2700 	case CS_TIMEOUT:
2701 	case CS_RESET:
2702 
2703 		/*
2704 		 * We are going to have the fc class block the rport
2705 		 * while we try to recover so instruct the mid layer
2706 		 * to requeue until the class decides how to handle this.
2707 		 */
2708 		res = DID_TRANSPORT_DISRUPTED << 16;
2709 
2710 		if (comp_status == CS_TIMEOUT) {
2711 			if (IS_FWI2_CAPABLE(ha))
2712 				break;
2713 			else if ((le16_to_cpu(sts->status_flags) &
2714 			    SF_LOGOUT_SENT) == 0)
2715 				break;
2716 		}
2717 
2718 		if (atomic_read(&fcport->state) == FCS_ONLINE) {
2719 			ql_dbg(ql_dbg_disc, fcport->vha, 0x3021,
2720 				"Port to be marked lost on fcport=%02x%02x%02x, current "
2721 				"port state= %s comp_status %x.\n", fcport->d_id.b.domain,
2722 				fcport->d_id.b.area, fcport->d_id.b.al_pa,
2723 				port_state_str[FCS_ONLINE],
2724 				comp_status);
2725 
2726 			qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
2727 			qlt_schedule_sess_for_deletion(fcport);
2728 		}
2729 
2730 		break;
2731 
2732 	case CS_ABORTED:
2733 		res = DID_RESET << 16;
2734 		break;
2735 
2736 	case CS_DIF_ERROR:
2737 		logit = qla2x00_handle_dif_error(sp, sts24);
2738 		res = cp->result;
2739 		break;
2740 
2741 	case CS_TRANSPORT:
2742 		res = DID_ERROR << 16;
2743 
2744 		if (!IS_PI_SPLIT_DET_CAPABLE(ha))
2745 			break;
2746 
2747 		if (state_flags & BIT_4)
2748 			scmd_printk(KERN_WARNING, cp,
2749 			    "Unsupported device '%s' found.\n",
2750 			    cp->device->vendor);
2751 		break;
2752 
2753 	case CS_DMA:
2754 		ql_log(ql_log_info, fcport->vha, 0x3022,
2755 		    "CS_DMA error: 0x%x-0x%x (0x%x) nexus=%ld:%d:%llu portid=%06x oxid=0x%x cdb=%10phN len=0x%x rsp_info=0x%x resid=0x%x fw_resid=0x%x sp=%p cp=%p.\n",
2756 		    comp_status, scsi_status, res, vha->host_no,
2757 		    cp->device->id, cp->device->lun, fcport->d_id.b24,
2758 		    ox_id, cp->cmnd, scsi_bufflen(cp), rsp_info_len,
2759 		    resid_len, fw_resid_len, sp, cp);
2760 		ql_dump_buffer(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe0ee,
2761 		    pkt, sizeof(*sts24));
2762 		res = DID_ERROR << 16;
2763 		break;
2764 	default:
2765 		res = DID_ERROR << 16;
2766 		break;
2767 	}
2768 
2769 out:
2770 	if (logit)
2771 		ql_dbg(ql_dbg_io, fcport->vha, 0x3022,
2772 		    "FCP command status: 0x%x-0x%x (0x%x) nexus=%ld:%d:%llu "
2773 		    "portid=%02x%02x%02x oxid=0x%x cdb=%10phN len=0x%x "
2774 		    "rsp_info=0x%x resid=0x%x fw_resid=0x%x sp=%p cp=%p.\n",
2775 		    comp_status, scsi_status, res, vha->host_no,
2776 		    cp->device->id, cp->device->lun, fcport->d_id.b.domain,
2777 		    fcport->d_id.b.area, fcport->d_id.b.al_pa, ox_id,
2778 		    cp->cmnd, scsi_bufflen(cp), rsp_info_len,
2779 		    resid_len, fw_resid_len, sp, cp);
2780 
2781 	if (rsp->status_srb == NULL)
2782 		sp->done(sp, res);
2783 }
2784 
2785 /**
2786  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
2787  * @rsp: response queue
2788  * @pkt: Entry pointer
2789  *
2790  * Extended sense data.
2791  */
2792 static void
2793 qla2x00_status_cont_entry(struct rsp_que *rsp, sts_cont_entry_t *pkt)
2794 {
2795 	uint8_t	sense_sz = 0;
2796 	struct qla_hw_data *ha = rsp->hw;
2797 	struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
2798 	srb_t *sp = rsp->status_srb;
2799 	struct scsi_cmnd *cp;
2800 	uint32_t sense_len;
2801 	uint8_t *sense_ptr;
2802 
2803 	if (!sp || !GET_CMD_SENSE_LEN(sp))
2804 		return;
2805 
2806 	sense_len = GET_CMD_SENSE_LEN(sp);
2807 	sense_ptr = GET_CMD_SENSE_PTR(sp);
2808 
2809 	cp = GET_CMD_SP(sp);
2810 	if (cp == NULL) {
2811 		ql_log(ql_log_warn, vha, 0x3025,
2812 		    "cmd is NULL: already returned to OS (sp=%p).\n", sp);
2813 
2814 		rsp->status_srb = NULL;
2815 		return;
2816 	}
2817 
2818 	if (sense_len > sizeof(pkt->data))
2819 		sense_sz = sizeof(pkt->data);
2820 	else
2821 		sense_sz = sense_len;
2822 
2823 	/* Move sense data. */
2824 	if (IS_FWI2_CAPABLE(ha))
2825 		host_to_fcp_swap(pkt->data, sizeof(pkt->data));
2826 	memcpy(sense_ptr, pkt->data, sense_sz);
2827 	ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302c,
2828 		sense_ptr, sense_sz);
2829 
2830 	sense_len -= sense_sz;
2831 	sense_ptr += sense_sz;
2832 
2833 	SET_CMD_SENSE_PTR(sp, sense_ptr);
2834 	SET_CMD_SENSE_LEN(sp, sense_len);
2835 
2836 	/* Place command on done queue. */
2837 	if (sense_len == 0) {
2838 		rsp->status_srb = NULL;
2839 		sp->done(sp, cp->result);
2840 	} else {
2841 		WARN_ON_ONCE(true);
2842 	}
2843 }
2844 
2845 /**
2846  * qla2x00_error_entry() - Process an error entry.
2847  * @vha: SCSI driver HA context
2848  * @rsp: response queue
2849  * @pkt: Entry pointer
2850  * return : 1=allow further error analysis. 0=no additional error analysis.
2851  */
2852 static int
2853 qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
2854 {
2855 	srb_t *sp;
2856 	struct qla_hw_data *ha = vha->hw;
2857 	const char func[] = "ERROR-IOCB";
2858 	uint16_t que = MSW(pkt->handle);
2859 	struct req_que *req = NULL;
2860 	int res = DID_ERROR << 16;
2861 
2862 	ql_dbg(ql_dbg_async, vha, 0x502a,
2863 	    "iocb type %xh with error status %xh, handle %xh, rspq id %d\n",
2864 	    pkt->entry_type, pkt->entry_status, pkt->handle, rsp->id);
2865 
2866 	if (que >= ha->max_req_queues || !ha->req_q_map[que])
2867 		goto fatal;
2868 
2869 	req = ha->req_q_map[que];
2870 
2871 	if (pkt->entry_status & RF_BUSY)
2872 		res = DID_BUS_BUSY << 16;
2873 
2874 	if ((pkt->handle & ~QLA_TGT_HANDLE_MASK) == QLA_TGT_SKIP_HANDLE)
2875 		return 0;
2876 
2877 	switch (pkt->entry_type) {
2878 	case NOTIFY_ACK_TYPE:
2879 	case STATUS_TYPE:
2880 	case STATUS_CONT_TYPE:
2881 	case LOGINOUT_PORT_IOCB_TYPE:
2882 	case CT_IOCB_TYPE:
2883 	case ELS_IOCB_TYPE:
2884 	case ABORT_IOCB_TYPE:
2885 	case MBX_IOCB_TYPE:
2886 	default:
2887 		sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2888 		if (sp) {
2889 			sp->done(sp, res);
2890 			return 0;
2891 		}
2892 		break;
2893 
2894 	case ABTS_RESP_24XX:
2895 	case CTIO_TYPE7:
2896 	case CTIO_CRC2:
2897 		return 1;
2898 	}
2899 fatal:
2900 	ql_log(ql_log_warn, vha, 0x5030,
2901 	    "Error entry - invalid handle/queue (%04x).\n", que);
2902 	return 0;
2903 }
2904 
2905 /**
2906  * qla24xx_mbx_completion() - Process mailbox command completions.
2907  * @vha: SCSI driver HA context
2908  * @mb0: Mailbox0 register
2909  */
2910 static void
2911 qla24xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
2912 {
2913 	uint16_t	cnt;
2914 	uint32_t	mboxes;
2915 	uint16_t __iomem *wptr;
2916 	struct qla_hw_data *ha = vha->hw;
2917 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2918 
2919 	/* Read all mbox registers? */
2920 	WARN_ON_ONCE(ha->mbx_count > 32);
2921 	mboxes = (1ULL << ha->mbx_count) - 1;
2922 	if (!ha->mcp)
2923 		ql_dbg(ql_dbg_async, vha, 0x504e, "MBX pointer ERROR.\n");
2924 	else
2925 		mboxes = ha->mcp->in_mb;
2926 
2927 	/* Load return mailbox registers. */
2928 	ha->flags.mbox_int = 1;
2929 	ha->mailbox_out[0] = mb0;
2930 	mboxes >>= 1;
2931 	wptr = (uint16_t __iomem *)&reg->mailbox1;
2932 
2933 	for (cnt = 1; cnt < ha->mbx_count; cnt++) {
2934 		if (mboxes & BIT_0)
2935 			ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
2936 
2937 		mboxes >>= 1;
2938 		wptr++;
2939 	}
2940 }
2941 
2942 static void
2943 qla24xx_abort_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
2944 	struct abort_entry_24xx *pkt)
2945 {
2946 	const char func[] = "ABT_IOCB";
2947 	srb_t *sp;
2948 	struct srb_iocb *abt;
2949 
2950 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2951 	if (!sp)
2952 		return;
2953 
2954 	abt = &sp->u.iocb_cmd;
2955 	abt->u.abt.comp_status = le16_to_cpu(pkt->nport_handle);
2956 	sp->done(sp, 0);
2957 }
2958 
2959 void qla24xx_nvme_ls4_iocb(struct scsi_qla_host *vha,
2960     struct pt_ls4_request *pkt, struct req_que *req)
2961 {
2962 	srb_t *sp;
2963 	const char func[] = "LS4_IOCB";
2964 	uint16_t comp_status;
2965 
2966 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2967 	if (!sp)
2968 		return;
2969 
2970 	comp_status = le16_to_cpu(pkt->status);
2971 	sp->done(sp, comp_status);
2972 }
2973 
2974 /**
2975  * qla24xx_process_response_queue() - Process response queue entries.
2976  * @vha: SCSI driver HA context
2977  * @rsp: response queue
2978  */
2979 void qla24xx_process_response_queue(struct scsi_qla_host *vha,
2980 	struct rsp_que *rsp)
2981 {
2982 	struct sts_entry_24xx *pkt;
2983 	struct qla_hw_data *ha = vha->hw;
2984 
2985 	if (!ha->flags.fw_started)
2986 		return;
2987 
2988 	if (rsp->qpair->cpuid != smp_processor_id())
2989 		qla_cpu_update(rsp->qpair, smp_processor_id());
2990 
2991 	while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
2992 		pkt = (struct sts_entry_24xx *)rsp->ring_ptr;
2993 
2994 		rsp->ring_index++;
2995 		if (rsp->ring_index == rsp->length) {
2996 			rsp->ring_index = 0;
2997 			rsp->ring_ptr = rsp->ring;
2998 		} else {
2999 			rsp->ring_ptr++;
3000 		}
3001 
3002 		if (pkt->entry_status != 0) {
3003 			if (qla2x00_error_entry(vha, rsp, (sts_entry_t *) pkt))
3004 				goto process_err;
3005 
3006 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
3007 			wmb();
3008 			continue;
3009 		}
3010 process_err:
3011 
3012 		switch (pkt->entry_type) {
3013 		case STATUS_TYPE:
3014 			qla2x00_status_entry(vha, rsp, pkt);
3015 			break;
3016 		case STATUS_CONT_TYPE:
3017 			qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
3018 			break;
3019 		case VP_RPT_ID_IOCB_TYPE:
3020 			qla24xx_report_id_acquisition(vha,
3021 			    (struct vp_rpt_id_entry_24xx *)pkt);
3022 			break;
3023 		case LOGINOUT_PORT_IOCB_TYPE:
3024 			qla24xx_logio_entry(vha, rsp->req,
3025 			    (struct logio_entry_24xx *)pkt);
3026 			break;
3027 		case CT_IOCB_TYPE:
3028 			qla24xx_els_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
3029 			break;
3030 		case ELS_IOCB_TYPE:
3031 			qla24xx_els_ct_entry(vha, rsp->req, pkt, ELS_IOCB_TYPE);
3032 			break;
3033 		case ABTS_RECV_24XX:
3034 			if (IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3035 			    IS_QLA28XX(ha)) {
3036 				/* ensure that the ATIO queue is empty */
3037 				qlt_handle_abts_recv(vha, rsp,
3038 				    (response_t *)pkt);
3039 				break;
3040 			} else {
3041 				qlt_24xx_process_atio_queue(vha, 1);
3042 			}
3043 			/* fall through */
3044 		case ABTS_RESP_24XX:
3045 		case CTIO_TYPE7:
3046 		case CTIO_CRC2:
3047 			qlt_response_pkt_all_vps(vha, rsp, (response_t *)pkt);
3048 			break;
3049 		case PT_LS4_REQUEST:
3050 			qla24xx_nvme_ls4_iocb(vha, (struct pt_ls4_request *)pkt,
3051 			    rsp->req);
3052 			break;
3053 		case NOTIFY_ACK_TYPE:
3054 			if (pkt->handle == QLA_TGT_SKIP_HANDLE)
3055 				qlt_response_pkt_all_vps(vha, rsp,
3056 				    (response_t *)pkt);
3057 			else
3058 				qla24xxx_nack_iocb_entry(vha, rsp->req,
3059 					(struct nack_to_isp *)pkt);
3060 			break;
3061 		case MARKER_TYPE:
3062 			/* Do nothing in this case, this check is to prevent it
3063 			 * from falling into default case
3064 			 */
3065 			break;
3066 		case ABORT_IOCB_TYPE:
3067 			qla24xx_abort_iocb_entry(vha, rsp->req,
3068 			    (struct abort_entry_24xx *)pkt);
3069 			break;
3070 		case MBX_IOCB_TYPE:
3071 			qla24xx_mbx_iocb_entry(vha, rsp->req,
3072 			    (struct mbx_24xx_entry *)pkt);
3073 			break;
3074 		case VP_CTRL_IOCB_TYPE:
3075 			qla_ctrlvp_completed(vha, rsp->req,
3076 			    (struct vp_ctrl_entry_24xx *)pkt);
3077 			break;
3078 		default:
3079 			/* Type Not Supported. */
3080 			ql_dbg(ql_dbg_async, vha, 0x5042,
3081 			    "Received unknown response pkt type %x "
3082 			    "entry status=%x.\n",
3083 			    pkt->entry_type, pkt->entry_status);
3084 			break;
3085 		}
3086 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
3087 		wmb();
3088 	}
3089 
3090 	/* Adjust ring index */
3091 	if (IS_P3P_TYPE(ha)) {
3092 		struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
3093 
3094 		WRT_REG_DWORD(&reg->rsp_q_out[0], rsp->ring_index);
3095 	} else {
3096 		WRT_REG_DWORD(rsp->rsp_q_out, rsp->ring_index);
3097 	}
3098 }
3099 
3100 static void
3101 qla2xxx_check_risc_status(scsi_qla_host_t *vha)
3102 {
3103 	int rval;
3104 	uint32_t cnt;
3105 	struct qla_hw_data *ha = vha->hw;
3106 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3107 
3108 	if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
3109 	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3110 		return;
3111 
3112 	rval = QLA_SUCCESS;
3113 	WRT_REG_DWORD(&reg->iobase_addr, 0x7C00);
3114 	RD_REG_DWORD(&reg->iobase_addr);
3115 	WRT_REG_DWORD(&reg->iobase_window, 0x0001);
3116 	for (cnt = 10000; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
3117 	    rval == QLA_SUCCESS; cnt--) {
3118 		if (cnt) {
3119 			WRT_REG_DWORD(&reg->iobase_window, 0x0001);
3120 			udelay(10);
3121 		} else
3122 			rval = QLA_FUNCTION_TIMEOUT;
3123 	}
3124 	if (rval == QLA_SUCCESS)
3125 		goto next_test;
3126 
3127 	rval = QLA_SUCCESS;
3128 	WRT_REG_DWORD(&reg->iobase_window, 0x0003);
3129 	for (cnt = 100; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
3130 	    rval == QLA_SUCCESS; cnt--) {
3131 		if (cnt) {
3132 			WRT_REG_DWORD(&reg->iobase_window, 0x0003);
3133 			udelay(10);
3134 		} else
3135 			rval = QLA_FUNCTION_TIMEOUT;
3136 	}
3137 	if (rval != QLA_SUCCESS)
3138 		goto done;
3139 
3140 next_test:
3141 	if (RD_REG_DWORD(&reg->iobase_c8) & BIT_3)
3142 		ql_log(ql_log_info, vha, 0x504c,
3143 		    "Additional code -- 0x55AA.\n");
3144 
3145 done:
3146 	WRT_REG_DWORD(&reg->iobase_window, 0x0000);
3147 	RD_REG_DWORD(&reg->iobase_window);
3148 }
3149 
3150 /**
3151  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP24xx.
3152  * @irq: interrupt number
3153  * @dev_id: SCSI driver HA context
3154  *
3155  * Called by system whenever the host adapter generates an interrupt.
3156  *
3157  * Returns handled flag.
3158  */
3159 irqreturn_t
3160 qla24xx_intr_handler(int irq, void *dev_id)
3161 {
3162 	scsi_qla_host_t	*vha;
3163 	struct qla_hw_data *ha;
3164 	struct device_reg_24xx __iomem *reg;
3165 	int		status;
3166 	unsigned long	iter;
3167 	uint32_t	stat;
3168 	uint32_t	hccr;
3169 	uint16_t	mb[8];
3170 	struct rsp_que *rsp;
3171 	unsigned long	flags;
3172 	bool process_atio = false;
3173 
3174 	rsp = (struct rsp_que *) dev_id;
3175 	if (!rsp) {
3176 		ql_log(ql_log_info, NULL, 0x5059,
3177 		    "%s: NULL response queue pointer.\n", __func__);
3178 		return IRQ_NONE;
3179 	}
3180 
3181 	ha = rsp->hw;
3182 	reg = &ha->iobase->isp24;
3183 	status = 0;
3184 
3185 	if (unlikely(pci_channel_offline(ha->pdev)))
3186 		return IRQ_HANDLED;
3187 
3188 	spin_lock_irqsave(&ha->hardware_lock, flags);
3189 	vha = pci_get_drvdata(ha->pdev);
3190 	for (iter = 50; iter--; ) {
3191 		stat = RD_REG_DWORD(&reg->host_status);
3192 		if (qla2x00_check_reg32_for_disconnect(vha, stat))
3193 			break;
3194 		if (stat & HSRX_RISC_PAUSED) {
3195 			if (unlikely(pci_channel_offline(ha->pdev)))
3196 				break;
3197 
3198 			hccr = RD_REG_DWORD(&reg->hccr);
3199 
3200 			ql_log(ql_log_warn, vha, 0x504b,
3201 			    "RISC paused -- HCCR=%x, Dumping firmware.\n",
3202 			    hccr);
3203 
3204 			qla2xxx_check_risc_status(vha);
3205 
3206 			ha->isp_ops->fw_dump(vha, 1);
3207 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3208 			break;
3209 		} else if ((stat & HSRX_RISC_INT) == 0)
3210 			break;
3211 
3212 		switch (stat & 0xff) {
3213 		case INTR_ROM_MB_SUCCESS:
3214 		case INTR_ROM_MB_FAILED:
3215 		case INTR_MB_SUCCESS:
3216 		case INTR_MB_FAILED:
3217 			qla24xx_mbx_completion(vha, MSW(stat));
3218 			status |= MBX_INTERRUPT;
3219 
3220 			break;
3221 		case INTR_ASYNC_EVENT:
3222 			mb[0] = MSW(stat);
3223 			mb[1] = RD_REG_WORD(&reg->mailbox1);
3224 			mb[2] = RD_REG_WORD(&reg->mailbox2);
3225 			mb[3] = RD_REG_WORD(&reg->mailbox3);
3226 			qla2x00_async_event(vha, rsp, mb);
3227 			break;
3228 		case INTR_RSP_QUE_UPDATE:
3229 		case INTR_RSP_QUE_UPDATE_83XX:
3230 			qla24xx_process_response_queue(vha, rsp);
3231 			break;
3232 		case INTR_ATIO_QUE_UPDATE_27XX:
3233 		case INTR_ATIO_QUE_UPDATE:
3234 			process_atio = true;
3235 			break;
3236 		case INTR_ATIO_RSP_QUE_UPDATE:
3237 			process_atio = true;
3238 			qla24xx_process_response_queue(vha, rsp);
3239 			break;
3240 		default:
3241 			ql_dbg(ql_dbg_async, vha, 0x504f,
3242 			    "Unrecognized interrupt type (%d).\n", stat * 0xff);
3243 			break;
3244 		}
3245 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
3246 		RD_REG_DWORD_RELAXED(&reg->hccr);
3247 		if (unlikely(IS_QLA83XX(ha) && (ha->pdev->revision == 1)))
3248 			ndelay(3500);
3249 	}
3250 	qla2x00_handle_mbx_completion(ha, status);
3251 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3252 
3253 	if (process_atio) {
3254 		spin_lock_irqsave(&ha->tgt.atio_lock, flags);
3255 		qlt_24xx_process_atio_queue(vha, 0);
3256 		spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
3257 	}
3258 
3259 	return IRQ_HANDLED;
3260 }
3261 
3262 static irqreturn_t
3263 qla24xx_msix_rsp_q(int irq, void *dev_id)
3264 {
3265 	struct qla_hw_data *ha;
3266 	struct rsp_que *rsp;
3267 	struct device_reg_24xx __iomem *reg;
3268 	struct scsi_qla_host *vha;
3269 	unsigned long flags;
3270 
3271 	rsp = (struct rsp_que *) dev_id;
3272 	if (!rsp) {
3273 		ql_log(ql_log_info, NULL, 0x505a,
3274 		    "%s: NULL response queue pointer.\n", __func__);
3275 		return IRQ_NONE;
3276 	}
3277 	ha = rsp->hw;
3278 	reg = &ha->iobase->isp24;
3279 
3280 	spin_lock_irqsave(&ha->hardware_lock, flags);
3281 
3282 	vha = pci_get_drvdata(ha->pdev);
3283 	qla24xx_process_response_queue(vha, rsp);
3284 	if (!ha->flags.disable_msix_handshake) {
3285 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
3286 		RD_REG_DWORD_RELAXED(&reg->hccr);
3287 	}
3288 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3289 
3290 	return IRQ_HANDLED;
3291 }
3292 
3293 static irqreturn_t
3294 qla24xx_msix_default(int irq, void *dev_id)
3295 {
3296 	scsi_qla_host_t	*vha;
3297 	struct qla_hw_data *ha;
3298 	struct rsp_que *rsp;
3299 	struct device_reg_24xx __iomem *reg;
3300 	int		status;
3301 	uint32_t	stat;
3302 	uint32_t	hccr;
3303 	uint16_t	mb[8];
3304 	unsigned long flags;
3305 	bool process_atio = false;
3306 
3307 	rsp = (struct rsp_que *) dev_id;
3308 	if (!rsp) {
3309 		ql_log(ql_log_info, NULL, 0x505c,
3310 		    "%s: NULL response queue pointer.\n", __func__);
3311 		return IRQ_NONE;
3312 	}
3313 	ha = rsp->hw;
3314 	reg = &ha->iobase->isp24;
3315 	status = 0;
3316 
3317 	spin_lock_irqsave(&ha->hardware_lock, flags);
3318 	vha = pci_get_drvdata(ha->pdev);
3319 	do {
3320 		stat = RD_REG_DWORD(&reg->host_status);
3321 		if (qla2x00_check_reg32_for_disconnect(vha, stat))
3322 			break;
3323 		if (stat & HSRX_RISC_PAUSED) {
3324 			if (unlikely(pci_channel_offline(ha->pdev)))
3325 				break;
3326 
3327 			hccr = RD_REG_DWORD(&reg->hccr);
3328 
3329 			ql_log(ql_log_info, vha, 0x5050,
3330 			    "RISC paused -- HCCR=%x, Dumping firmware.\n",
3331 			    hccr);
3332 
3333 			qla2xxx_check_risc_status(vha);
3334 
3335 			ha->isp_ops->fw_dump(vha, 1);
3336 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3337 			break;
3338 		} else if ((stat & HSRX_RISC_INT) == 0)
3339 			break;
3340 
3341 		switch (stat & 0xff) {
3342 		case INTR_ROM_MB_SUCCESS:
3343 		case INTR_ROM_MB_FAILED:
3344 		case INTR_MB_SUCCESS:
3345 		case INTR_MB_FAILED:
3346 			qla24xx_mbx_completion(vha, MSW(stat));
3347 			status |= MBX_INTERRUPT;
3348 
3349 			break;
3350 		case INTR_ASYNC_EVENT:
3351 			mb[0] = MSW(stat);
3352 			mb[1] = RD_REG_WORD(&reg->mailbox1);
3353 			mb[2] = RD_REG_WORD(&reg->mailbox2);
3354 			mb[3] = RD_REG_WORD(&reg->mailbox3);
3355 			qla2x00_async_event(vha, rsp, mb);
3356 			break;
3357 		case INTR_RSP_QUE_UPDATE:
3358 		case INTR_RSP_QUE_UPDATE_83XX:
3359 			qla24xx_process_response_queue(vha, rsp);
3360 			break;
3361 		case INTR_ATIO_QUE_UPDATE_27XX:
3362 		case INTR_ATIO_QUE_UPDATE:
3363 			process_atio = true;
3364 			break;
3365 		case INTR_ATIO_RSP_QUE_UPDATE:
3366 			process_atio = true;
3367 			qla24xx_process_response_queue(vha, rsp);
3368 			break;
3369 		default:
3370 			ql_dbg(ql_dbg_async, vha, 0x5051,
3371 			    "Unrecognized interrupt type (%d).\n", stat & 0xff);
3372 			break;
3373 		}
3374 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
3375 	} while (0);
3376 	qla2x00_handle_mbx_completion(ha, status);
3377 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3378 
3379 	if (process_atio) {
3380 		spin_lock_irqsave(&ha->tgt.atio_lock, flags);
3381 		qlt_24xx_process_atio_queue(vha, 0);
3382 		spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
3383 	}
3384 
3385 	return IRQ_HANDLED;
3386 }
3387 
3388 irqreturn_t
3389 qla2xxx_msix_rsp_q(int irq, void *dev_id)
3390 {
3391 	struct qla_hw_data *ha;
3392 	struct qla_qpair *qpair;
3393 	struct device_reg_24xx __iomem *reg;
3394 	unsigned long flags;
3395 
3396 	qpair = dev_id;
3397 	if (!qpair) {
3398 		ql_log(ql_log_info, NULL, 0x505b,
3399 		    "%s: NULL response queue pointer.\n", __func__);
3400 		return IRQ_NONE;
3401 	}
3402 	ha = qpair->hw;
3403 
3404 	/* Clear the interrupt, if enabled, for this response queue */
3405 	if (unlikely(!ha->flags.disable_msix_handshake)) {
3406 		reg = &ha->iobase->isp24;
3407 		spin_lock_irqsave(&ha->hardware_lock, flags);
3408 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
3409 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
3410 	}
3411 
3412 	queue_work(ha->wq, &qpair->q_work);
3413 
3414 	return IRQ_HANDLED;
3415 }
3416 
3417 /* Interrupt handling helpers. */
3418 
3419 struct qla_init_msix_entry {
3420 	const char *name;
3421 	irq_handler_t handler;
3422 };
3423 
3424 static const struct qla_init_msix_entry msix_entries[] = {
3425 	{ "default", qla24xx_msix_default },
3426 	{ "rsp_q", qla24xx_msix_rsp_q },
3427 	{ "atio_q", qla83xx_msix_atio_q },
3428 	{ "qpair_multiq", qla2xxx_msix_rsp_q },
3429 };
3430 
3431 static const struct qla_init_msix_entry qla82xx_msix_entries[] = {
3432 	{ "qla2xxx (default)", qla82xx_msix_default },
3433 	{ "qla2xxx (rsp_q)", qla82xx_msix_rsp_q },
3434 };
3435 
3436 static int
3437 qla24xx_enable_msix(struct qla_hw_data *ha, struct rsp_que *rsp)
3438 {
3439 	int i, ret;
3440 	struct qla_msix_entry *qentry;
3441 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
3442 	int min_vecs = QLA_BASE_VECTORS;
3443 	struct irq_affinity desc = {
3444 		.pre_vectors = QLA_BASE_VECTORS,
3445 	};
3446 
3447 	if (QLA_TGT_MODE_ENABLED() && (ql2xenablemsix != 0) &&
3448 	    IS_ATIO_MSIX_CAPABLE(ha)) {
3449 		desc.pre_vectors++;
3450 		min_vecs++;
3451 	}
3452 
3453 	if (USER_CTRL_IRQ(ha) || !ha->mqiobase) {
3454 		/* user wants to control IRQ setting for target mode */
3455 		ret = pci_alloc_irq_vectors(ha->pdev, min_vecs,
3456 		    ha->msix_count, PCI_IRQ_MSIX);
3457 	} else
3458 		ret = pci_alloc_irq_vectors_affinity(ha->pdev, min_vecs,
3459 		    ha->msix_count, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY,
3460 		    &desc);
3461 
3462 	if (ret < 0) {
3463 		ql_log(ql_log_fatal, vha, 0x00c7,
3464 		    "MSI-X: Failed to enable support, "
3465 		    "giving   up -- %d/%d.\n",
3466 		    ha->msix_count, ret);
3467 		goto msix_out;
3468 	} else if (ret < ha->msix_count) {
3469 		ql_log(ql_log_info, vha, 0x00c6,
3470 		    "MSI-X: Using %d vectors\n", ret);
3471 		ha->msix_count = ret;
3472 		/* Recalculate queue values */
3473 		if (ha->mqiobase && (ql2xmqsupport || ql2xnvmeenable)) {
3474 			ha->max_req_queues = ha->msix_count - 1;
3475 
3476 			/* ATIOQ needs 1 vector. That's 1 less QPair */
3477 			if (QLA_TGT_MODE_ENABLED())
3478 				ha->max_req_queues--;
3479 
3480 			ha->max_rsp_queues = ha->max_req_queues;
3481 
3482 			ha->max_qpairs = ha->max_req_queues - 1;
3483 			ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0190,
3484 			    "Adjusted Max no of queues pairs: %d.\n", ha->max_qpairs);
3485 		}
3486 	}
3487 	vha->irq_offset = desc.pre_vectors;
3488 	ha->msix_entries = kcalloc(ha->msix_count,
3489 				   sizeof(struct qla_msix_entry),
3490 				   GFP_KERNEL);
3491 	if (!ha->msix_entries) {
3492 		ql_log(ql_log_fatal, vha, 0x00c8,
3493 		    "Failed to allocate memory for ha->msix_entries.\n");
3494 		ret = -ENOMEM;
3495 		goto free_irqs;
3496 	}
3497 	ha->flags.msix_enabled = 1;
3498 
3499 	for (i = 0; i < ha->msix_count; i++) {
3500 		qentry = &ha->msix_entries[i];
3501 		qentry->vector = pci_irq_vector(ha->pdev, i);
3502 		qentry->entry = i;
3503 		qentry->have_irq = 0;
3504 		qentry->in_use = 0;
3505 		qentry->handle = NULL;
3506 	}
3507 
3508 	/* Enable MSI-X vectors for the base queue */
3509 	for (i = 0; i < QLA_BASE_VECTORS; i++) {
3510 		qentry = &ha->msix_entries[i];
3511 		qentry->handle = rsp;
3512 		rsp->msix = qentry;
3513 		scnprintf(qentry->name, sizeof(qentry->name),
3514 		    "qla2xxx%lu_%s", vha->host_no, msix_entries[i].name);
3515 		if (IS_P3P_TYPE(ha))
3516 			ret = request_irq(qentry->vector,
3517 				qla82xx_msix_entries[i].handler,
3518 				0, qla82xx_msix_entries[i].name, rsp);
3519 		else
3520 			ret = request_irq(qentry->vector,
3521 				msix_entries[i].handler,
3522 				0, qentry->name, rsp);
3523 		if (ret)
3524 			goto msix_register_fail;
3525 		qentry->have_irq = 1;
3526 		qentry->in_use = 1;
3527 	}
3528 
3529 	/*
3530 	 * If target mode is enable, also request the vector for the ATIO
3531 	 * queue.
3532 	 */
3533 	if (QLA_TGT_MODE_ENABLED() && (ql2xenablemsix != 0) &&
3534 	    IS_ATIO_MSIX_CAPABLE(ha)) {
3535 		qentry = &ha->msix_entries[QLA_ATIO_VECTOR];
3536 		rsp->msix = qentry;
3537 		qentry->handle = rsp;
3538 		scnprintf(qentry->name, sizeof(qentry->name),
3539 		    "qla2xxx%lu_%s", vha->host_no,
3540 		    msix_entries[QLA_ATIO_VECTOR].name);
3541 		qentry->in_use = 1;
3542 		ret = request_irq(qentry->vector,
3543 			msix_entries[QLA_ATIO_VECTOR].handler,
3544 			0, qentry->name, rsp);
3545 		qentry->have_irq = 1;
3546 	}
3547 
3548 msix_register_fail:
3549 	if (ret) {
3550 		ql_log(ql_log_fatal, vha, 0x00cb,
3551 		    "MSI-X: unable to register handler -- %x/%d.\n",
3552 		    qentry->vector, ret);
3553 		qla2x00_free_irqs(vha);
3554 		ha->mqenable = 0;
3555 		goto msix_out;
3556 	}
3557 
3558 	/* Enable MSI-X vector for response queue update for queue 0 */
3559 	if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
3560 		if (ha->msixbase && ha->mqiobase &&
3561 		    (ha->max_rsp_queues > 1 || ha->max_req_queues > 1 ||
3562 		     ql2xmqsupport))
3563 			ha->mqenable = 1;
3564 	} else
3565 		if (ha->mqiobase &&
3566 		    (ha->max_rsp_queues > 1 || ha->max_req_queues > 1 ||
3567 		     ql2xmqsupport))
3568 			ha->mqenable = 1;
3569 	ql_dbg(ql_dbg_multiq, vha, 0xc005,
3570 	    "mqiobase=%p, max_rsp_queues=%d, max_req_queues=%d.\n",
3571 	    ha->mqiobase, ha->max_rsp_queues, ha->max_req_queues);
3572 	ql_dbg(ql_dbg_init, vha, 0x0055,
3573 	    "mqiobase=%p, max_rsp_queues=%d, max_req_queues=%d.\n",
3574 	    ha->mqiobase, ha->max_rsp_queues, ha->max_req_queues);
3575 
3576 msix_out:
3577 	return ret;
3578 
3579 free_irqs:
3580 	pci_free_irq_vectors(ha->pdev);
3581 	goto msix_out;
3582 }
3583 
3584 int
3585 qla2x00_request_irqs(struct qla_hw_data *ha, struct rsp_que *rsp)
3586 {
3587 	int ret = QLA_FUNCTION_FAILED;
3588 	device_reg_t *reg = ha->iobase;
3589 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
3590 
3591 	/* If possible, enable MSI-X. */
3592 	if (ql2xenablemsix == 0 || (!IS_QLA2432(ha) && !IS_QLA2532(ha) &&
3593 	    !IS_QLA8432(ha) && !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha) &&
3594 	    !IS_QLAFX00(ha) && !IS_QLA27XX(ha) && !IS_QLA28XX(ha)))
3595 		goto skip_msi;
3596 
3597 	if (ql2xenablemsix == 2)
3598 		goto skip_msix;
3599 
3600 	if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_HP &&
3601 		(ha->pdev->subsystem_device == 0x7040 ||
3602 		ha->pdev->subsystem_device == 0x7041 ||
3603 		ha->pdev->subsystem_device == 0x1705)) {
3604 		ql_log(ql_log_warn, vha, 0x0034,
3605 		    "MSI-X: Unsupported ISP 2432 SSVID/SSDID (0x%X,0x%X).\n",
3606 			ha->pdev->subsystem_vendor,
3607 			ha->pdev->subsystem_device);
3608 		goto skip_msi;
3609 	}
3610 
3611 	if (IS_QLA2432(ha) && (ha->pdev->revision < QLA_MSIX_CHIP_REV_24XX)) {
3612 		ql_log(ql_log_warn, vha, 0x0035,
3613 		    "MSI-X; Unsupported ISP2432 (0x%X, 0x%X).\n",
3614 		    ha->pdev->revision, QLA_MSIX_CHIP_REV_24XX);
3615 		goto skip_msix;
3616 	}
3617 
3618 	ret = qla24xx_enable_msix(ha, rsp);
3619 	if (!ret) {
3620 		ql_dbg(ql_dbg_init, vha, 0x0036,
3621 		    "MSI-X: Enabled (0x%X, 0x%X).\n",
3622 		    ha->chip_revision, ha->fw_attributes);
3623 		goto clear_risc_ints;
3624 	}
3625 
3626 skip_msix:
3627 
3628 	ql_log(ql_log_info, vha, 0x0037,
3629 	    "Falling back-to MSI mode -%d.\n", ret);
3630 
3631 	if (!IS_QLA24XX(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha) &&
3632 	    !IS_QLA8001(ha) && !IS_P3P_TYPE(ha) && !IS_QLAFX00(ha) &&
3633 	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3634 		goto skip_msi;
3635 
3636 	ret = pci_alloc_irq_vectors(ha->pdev, 1, 1, PCI_IRQ_MSI);
3637 	if (!ret) {
3638 		ql_dbg(ql_dbg_init, vha, 0x0038,
3639 		    "MSI: Enabled.\n");
3640 		ha->flags.msi_enabled = 1;
3641 	} else
3642 		ql_log(ql_log_warn, vha, 0x0039,
3643 		    "Falling back-to INTa mode -- %d.\n", ret);
3644 skip_msi:
3645 
3646 	/* Skip INTx on ISP82xx. */
3647 	if (!ha->flags.msi_enabled && IS_QLA82XX(ha))
3648 		return QLA_FUNCTION_FAILED;
3649 
3650 	ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
3651 	    ha->flags.msi_enabled ? 0 : IRQF_SHARED,
3652 	    QLA2XXX_DRIVER_NAME, rsp);
3653 	if (ret) {
3654 		ql_log(ql_log_warn, vha, 0x003a,
3655 		    "Failed to reserve interrupt %d already in use.\n",
3656 		    ha->pdev->irq);
3657 		goto fail;
3658 	} else if (!ha->flags.msi_enabled) {
3659 		ql_dbg(ql_dbg_init, vha, 0x0125,
3660 		    "INTa mode: Enabled.\n");
3661 		ha->flags.mr_intr_valid = 1;
3662 	}
3663 
3664 clear_risc_ints:
3665 	if (IS_FWI2_CAPABLE(ha) || IS_QLAFX00(ha))
3666 		goto fail;
3667 
3668 	spin_lock_irq(&ha->hardware_lock);
3669 	WRT_REG_WORD(&reg->isp.semaphore, 0);
3670 	spin_unlock_irq(&ha->hardware_lock);
3671 
3672 fail:
3673 	return ret;
3674 }
3675 
3676 void
3677 qla2x00_free_irqs(scsi_qla_host_t *vha)
3678 {
3679 	struct qla_hw_data *ha = vha->hw;
3680 	struct rsp_que *rsp;
3681 	struct qla_msix_entry *qentry;
3682 	int i;
3683 
3684 	/*
3685 	 * We need to check that ha->rsp_q_map is valid in case we are called
3686 	 * from a probe failure context.
3687 	 */
3688 	if (!ha->rsp_q_map || !ha->rsp_q_map[0])
3689 		goto free_irqs;
3690 	rsp = ha->rsp_q_map[0];
3691 
3692 	if (ha->flags.msix_enabled) {
3693 		for (i = 0; i < ha->msix_count; i++) {
3694 			qentry = &ha->msix_entries[i];
3695 			if (qentry->have_irq) {
3696 				irq_set_affinity_notifier(qentry->vector, NULL);
3697 				free_irq(pci_irq_vector(ha->pdev, i), qentry->handle);
3698 			}
3699 		}
3700 		kfree(ha->msix_entries);
3701 		ha->msix_entries = NULL;
3702 		ha->flags.msix_enabled = 0;
3703 		ql_dbg(ql_dbg_init, vha, 0x0042,
3704 			"Disabled MSI-X.\n");
3705 	} else {
3706 		free_irq(pci_irq_vector(ha->pdev, 0), rsp);
3707 	}
3708 
3709 free_irqs:
3710 	pci_free_irq_vectors(ha->pdev);
3711 }
3712 
3713 int qla25xx_request_irq(struct qla_hw_data *ha, struct qla_qpair *qpair,
3714 	struct qla_msix_entry *msix, int vector_type)
3715 {
3716 	const struct qla_init_msix_entry *intr = &msix_entries[vector_type];
3717 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
3718 	int ret;
3719 
3720 	scnprintf(msix->name, sizeof(msix->name),
3721 	    "qla2xxx%lu_qpair%d", vha->host_no, qpair->id);
3722 	ret = request_irq(msix->vector, intr->handler, 0, msix->name, qpair);
3723 	if (ret) {
3724 		ql_log(ql_log_fatal, vha, 0x00e6,
3725 		    "MSI-X: Unable to register handler -- %x/%d.\n",
3726 		    msix->vector, ret);
3727 		return ret;
3728 	}
3729 	msix->have_irq = 1;
3730 	msix->handle = qpair;
3731 	return ret;
3732 }
3733