xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_isr.c (revision aeb64ff3)
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 		if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
1231 			ha->flags.fw_init_done = 0;
1232 			ql_log(ql_log_warn, vha, 0xffff,
1233 			    "MPI Heartbeat stop. Chip reset needed. MB0[%xh] MB1[%xh] MB2[%xh] MB3[%xh]\n",
1234 			    mb[0], mb[1], mb[2], mb[3]);
1235 
1236 			if ((mb[1] & BIT_8) ||
1237 			    (mb[2] & BIT_8)) {
1238 				ql_log(ql_log_warn, vha, 0xd013,
1239 				    "MPI Heartbeat stop. FW dump needed\n");
1240 				ha->fw_dump_mpi = 1;
1241 				ha->isp_ops->fw_dump(vha, 1);
1242 			}
1243 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1244 			qla2xxx_wake_dpc(vha);
1245 		} else if (IS_QLA83XX(ha)) {
1246 			mb[4] = RD_REG_WORD(&reg24->mailbox4);
1247 			mb[5] = RD_REG_WORD(&reg24->mailbox5);
1248 			mb[6] = RD_REG_WORD(&reg24->mailbox6);
1249 			mb[7] = RD_REG_WORD(&reg24->mailbox7);
1250 			qla83xx_handle_8200_aen(vha, mb);
1251 		} else {
1252 			ql_dbg(ql_dbg_async, vha, 0x5052,
1253 			    "skip Heartbeat processing mb0-3=[0x%04x] [0x%04x] [0x%04x] [0x%04x]\n",
1254 			    mb[0], mb[1], mb[2], mb[3]);
1255 		}
1256 		break;
1257 
1258 	case MBA_DPORT_DIAGNOSTICS:
1259 		ql_dbg(ql_dbg_async, vha, 0x5052,
1260 		    "D-Port Diagnostics: %04x result=%s\n",
1261 		    mb[0],
1262 		    mb[1] == 0 ? "start" :
1263 		    mb[1] == 1 ? "done (pass)" :
1264 		    mb[1] == 2 ? "done (error)" : "other");
1265 		break;
1266 
1267 	case MBA_TEMPERATURE_ALERT:
1268 		ql_dbg(ql_dbg_async, vha, 0x505e,
1269 		    "TEMPERATURE ALERT: %04x %04x %04x\n", mb[1], mb[2], mb[3]);
1270 		if (mb[1] == 0x12)
1271 			schedule_work(&ha->board_disable);
1272 		break;
1273 
1274 	case MBA_TRANS_INSERT:
1275 		ql_dbg(ql_dbg_async, vha, 0x5091,
1276 		    "Transceiver Insertion: %04x\n", mb[1]);
1277 		break;
1278 
1279 	default:
1280 		ql_dbg(ql_dbg_async, vha, 0x5057,
1281 		    "Unknown AEN:%04x %04x %04x %04x\n",
1282 		    mb[0], mb[1], mb[2], mb[3]);
1283 	}
1284 
1285 	qlt_async_event(mb[0], vha, mb);
1286 
1287 	if (!vha->vp_idx && ha->num_vhosts)
1288 		qla2x00_alert_all_vps(rsp, mb);
1289 }
1290 
1291 /**
1292  * qla2x00_process_completed_request() - Process a Fast Post response.
1293  * @vha: SCSI driver HA context
1294  * @req: request queue
1295  * @index: SRB index
1296  */
1297 void
1298 qla2x00_process_completed_request(struct scsi_qla_host *vha,
1299 				  struct req_que *req, uint32_t index)
1300 {
1301 	srb_t *sp;
1302 	struct qla_hw_data *ha = vha->hw;
1303 
1304 	/* Validate handle. */
1305 	if (index >= req->num_outstanding_cmds) {
1306 		ql_log(ql_log_warn, vha, 0x3014,
1307 		    "Invalid SCSI command index (%x).\n", index);
1308 
1309 		if (IS_P3P_TYPE(ha))
1310 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1311 		else
1312 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1313 		return;
1314 	}
1315 
1316 	sp = req->outstanding_cmds[index];
1317 	if (sp) {
1318 		/* Free outstanding command slot. */
1319 		req->outstanding_cmds[index] = NULL;
1320 
1321 		/* Save ISP completion status */
1322 		sp->done(sp, DID_OK << 16);
1323 	} else {
1324 		ql_log(ql_log_warn, vha, 0x3016, "Invalid SCSI SRB.\n");
1325 
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 	}
1331 }
1332 
1333 srb_t *
1334 qla2x00_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
1335     struct req_que *req, void *iocb)
1336 {
1337 	struct qla_hw_data *ha = vha->hw;
1338 	sts_entry_t *pkt = iocb;
1339 	srb_t *sp = NULL;
1340 	uint16_t index;
1341 
1342 	index = LSW(pkt->handle);
1343 	if (index >= req->num_outstanding_cmds) {
1344 		ql_log(ql_log_warn, vha, 0x5031,
1345 			   "Invalid command index (%x) type %8ph.\n",
1346 			   index, iocb);
1347 		if (IS_P3P_TYPE(ha))
1348 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1349 		else
1350 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1351 		goto done;
1352 	}
1353 	sp = req->outstanding_cmds[index];
1354 	if (!sp) {
1355 		ql_log(ql_log_warn, vha, 0x5032,
1356 		    "Invalid completion handle (%x) -- timed-out.\n", index);
1357 		return sp;
1358 	}
1359 	if (sp->handle != index) {
1360 		ql_log(ql_log_warn, vha, 0x5033,
1361 		    "SRB handle (%x) mismatch %x.\n", sp->handle, index);
1362 		return NULL;
1363 	}
1364 
1365 	req->outstanding_cmds[index] = NULL;
1366 
1367 done:
1368 	return sp;
1369 }
1370 
1371 static void
1372 qla2x00_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1373     struct mbx_entry *mbx)
1374 {
1375 	const char func[] = "MBX-IOCB";
1376 	const char *type;
1377 	fc_port_t *fcport;
1378 	srb_t *sp;
1379 	struct srb_iocb *lio;
1380 	uint16_t *data;
1381 	uint16_t status;
1382 
1383 	sp = qla2x00_get_sp_from_handle(vha, func, req, mbx);
1384 	if (!sp)
1385 		return;
1386 
1387 	lio = &sp->u.iocb_cmd;
1388 	type = sp->name;
1389 	fcport = sp->fcport;
1390 	data = lio->u.logio.data;
1391 
1392 	data[0] = MBS_COMMAND_ERROR;
1393 	data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
1394 	    QLA_LOGIO_LOGIN_RETRIED : 0;
1395 	if (mbx->entry_status) {
1396 		ql_dbg(ql_dbg_async, vha, 0x5043,
1397 		    "Async-%s error entry - hdl=%x portid=%02x%02x%02x "
1398 		    "entry-status=%x status=%x state-flag=%x "
1399 		    "status-flags=%x.\n", type, sp->handle,
1400 		    fcport->d_id.b.domain, fcport->d_id.b.area,
1401 		    fcport->d_id.b.al_pa, mbx->entry_status,
1402 		    le16_to_cpu(mbx->status), le16_to_cpu(mbx->state_flags),
1403 		    le16_to_cpu(mbx->status_flags));
1404 
1405 		ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x5029,
1406 		    mbx, sizeof(*mbx));
1407 
1408 		goto logio_done;
1409 	}
1410 
1411 	status = le16_to_cpu(mbx->status);
1412 	if (status == 0x30 && sp->type == SRB_LOGIN_CMD &&
1413 	    le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE)
1414 		status = 0;
1415 	if (!status && le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE) {
1416 		ql_dbg(ql_dbg_async, vha, 0x5045,
1417 		    "Async-%s complete - hdl=%x portid=%02x%02x%02x mbx1=%x.\n",
1418 		    type, sp->handle, fcport->d_id.b.domain,
1419 		    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1420 		    le16_to_cpu(mbx->mb1));
1421 
1422 		data[0] = MBS_COMMAND_COMPLETE;
1423 		if (sp->type == SRB_LOGIN_CMD) {
1424 			fcport->port_type = FCT_TARGET;
1425 			if (le16_to_cpu(mbx->mb1) & BIT_0)
1426 				fcport->port_type = FCT_INITIATOR;
1427 			else if (le16_to_cpu(mbx->mb1) & BIT_1)
1428 				fcport->flags |= FCF_FCP2_DEVICE;
1429 		}
1430 		goto logio_done;
1431 	}
1432 
1433 	data[0] = le16_to_cpu(mbx->mb0);
1434 	switch (data[0]) {
1435 	case MBS_PORT_ID_USED:
1436 		data[1] = le16_to_cpu(mbx->mb1);
1437 		break;
1438 	case MBS_LOOP_ID_USED:
1439 		break;
1440 	default:
1441 		data[0] = MBS_COMMAND_ERROR;
1442 		break;
1443 	}
1444 
1445 	ql_log(ql_log_warn, vha, 0x5046,
1446 	    "Async-%s failed - hdl=%x portid=%02x%02x%02x status=%x "
1447 	    "mb0=%x mb1=%x mb2=%x mb6=%x mb7=%x.\n", type, sp->handle,
1448 	    fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
1449 	    status, le16_to_cpu(mbx->mb0), le16_to_cpu(mbx->mb1),
1450 	    le16_to_cpu(mbx->mb2), le16_to_cpu(mbx->mb6),
1451 	    le16_to_cpu(mbx->mb7));
1452 
1453 logio_done:
1454 	sp->done(sp, 0);
1455 }
1456 
1457 static void
1458 qla24xx_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1459     struct mbx_24xx_entry *pkt)
1460 {
1461 	const char func[] = "MBX-IOCB2";
1462 	srb_t *sp;
1463 	struct srb_iocb *si;
1464 	u16 sz, i;
1465 	int res;
1466 
1467 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1468 	if (!sp)
1469 		return;
1470 
1471 	si = &sp->u.iocb_cmd;
1472 	sz = min(ARRAY_SIZE(pkt->mb), ARRAY_SIZE(sp->u.iocb_cmd.u.mbx.in_mb));
1473 
1474 	for (i = 0; i < sz; i++)
1475 		si->u.mbx.in_mb[i] = le16_to_cpu(pkt->mb[i]);
1476 
1477 	res = (si->u.mbx.in_mb[0] & MBS_MASK);
1478 
1479 	sp->done(sp, res);
1480 }
1481 
1482 static void
1483 qla24xxx_nack_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1484     struct nack_to_isp *pkt)
1485 {
1486 	const char func[] = "nack";
1487 	srb_t *sp;
1488 	int res = 0;
1489 
1490 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1491 	if (!sp)
1492 		return;
1493 
1494 	if (pkt->u.isp2x.status != cpu_to_le16(NOTIFY_ACK_SUCCESS))
1495 		res = QLA_FUNCTION_FAILED;
1496 
1497 	sp->done(sp, res);
1498 }
1499 
1500 static void
1501 qla2x00_ct_entry(scsi_qla_host_t *vha, struct req_que *req,
1502     sts_entry_t *pkt, int iocb_type)
1503 {
1504 	const char func[] = "CT_IOCB";
1505 	const char *type;
1506 	srb_t *sp;
1507 	struct bsg_job *bsg_job;
1508 	struct fc_bsg_reply *bsg_reply;
1509 	uint16_t comp_status;
1510 	int res = 0;
1511 
1512 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1513 	if (!sp)
1514 		return;
1515 
1516 	switch (sp->type) {
1517 	case SRB_CT_CMD:
1518 	    bsg_job = sp->u.bsg_job;
1519 	    bsg_reply = bsg_job->reply;
1520 
1521 	    type = "ct pass-through";
1522 
1523 	    comp_status = le16_to_cpu(pkt->comp_status);
1524 
1525 	    /*
1526 	     * return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
1527 	     * fc payload  to the caller
1528 	     */
1529 	    bsg_reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
1530 	    bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1531 
1532 	    if (comp_status != CS_COMPLETE) {
1533 		    if (comp_status == CS_DATA_UNDERRUN) {
1534 			    res = DID_OK << 16;
1535 			    bsg_reply->reply_payload_rcv_len =
1536 				le16_to_cpu(pkt->rsp_info_len);
1537 
1538 			    ql_log(ql_log_warn, vha, 0x5048,
1539 				"CT pass-through-%s error comp_status=0x%x total_byte=0x%x.\n",
1540 				type, comp_status,
1541 				bsg_reply->reply_payload_rcv_len);
1542 		    } else {
1543 			    ql_log(ql_log_warn, vha, 0x5049,
1544 				"CT pass-through-%s error comp_status=0x%x.\n",
1545 				type, comp_status);
1546 			    res = DID_ERROR << 16;
1547 			    bsg_reply->reply_payload_rcv_len = 0;
1548 		    }
1549 		    ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x5035,
1550 			pkt, sizeof(*pkt));
1551 	    } else {
1552 		    res = DID_OK << 16;
1553 		    bsg_reply->reply_payload_rcv_len =
1554 			bsg_job->reply_payload.payload_len;
1555 		    bsg_job->reply_len = 0;
1556 	    }
1557 	    break;
1558 	case SRB_CT_PTHRU_CMD:
1559 	    /*
1560 	     * borrowing sts_entry_24xx.comp_status.
1561 	     * same location as ct_entry_24xx.comp_status
1562 	     */
1563 	     res = qla2x00_chk_ms_status(vha, (ms_iocb_entry_t *)pkt,
1564 		 (struct ct_sns_rsp *)sp->u.iocb_cmd.u.ctarg.rsp,
1565 		 sp->name);
1566 	     break;
1567 	}
1568 
1569 	sp->done(sp, res);
1570 }
1571 
1572 static void
1573 qla24xx_els_ct_entry(scsi_qla_host_t *vha, struct req_que *req,
1574     struct sts_entry_24xx *pkt, int iocb_type)
1575 {
1576 	const char func[] = "ELS_CT_IOCB";
1577 	const char *type;
1578 	srb_t *sp;
1579 	struct bsg_job *bsg_job;
1580 	struct fc_bsg_reply *bsg_reply;
1581 	uint16_t comp_status;
1582 	uint32_t fw_status[3];
1583 	int res;
1584 	struct srb_iocb *els;
1585 
1586 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1587 	if (!sp)
1588 		return;
1589 
1590 	type = NULL;
1591 	switch (sp->type) {
1592 	case SRB_ELS_CMD_RPT:
1593 	case SRB_ELS_CMD_HST:
1594 		type = "els";
1595 		break;
1596 	case SRB_CT_CMD:
1597 		type = "ct pass-through";
1598 		break;
1599 	case SRB_ELS_DCMD:
1600 		type = "Driver ELS logo";
1601 		if (iocb_type != ELS_IOCB_TYPE) {
1602 			ql_dbg(ql_dbg_user, vha, 0x5047,
1603 			    "Completing %s: (%p) type=%d.\n",
1604 			    type, sp, sp->type);
1605 			sp->done(sp, 0);
1606 			return;
1607 		}
1608 		break;
1609 	case SRB_CT_PTHRU_CMD:
1610 		/* borrowing sts_entry_24xx.comp_status.
1611 		   same location as ct_entry_24xx.comp_status
1612 		 */
1613 		res = qla2x00_chk_ms_status(sp->vha, (ms_iocb_entry_t *)pkt,
1614 			(struct ct_sns_rsp *)sp->u.iocb_cmd.u.ctarg.rsp,
1615 			sp->name);
1616 		sp->done(sp, res);
1617 		return;
1618 	default:
1619 		ql_dbg(ql_dbg_user, vha, 0x503e,
1620 		    "Unrecognized SRB: (%p) type=%d.\n", sp, sp->type);
1621 		return;
1622 	}
1623 
1624 	comp_status = fw_status[0] = le16_to_cpu(pkt->comp_status);
1625 	fw_status[1] = le16_to_cpu(((struct els_sts_entry_24xx *)pkt)->error_subcode_1);
1626 	fw_status[2] = le16_to_cpu(((struct els_sts_entry_24xx *)pkt)->error_subcode_2);
1627 
1628 	if (iocb_type == ELS_IOCB_TYPE) {
1629 		els = &sp->u.iocb_cmd;
1630 		els->u.els_plogi.fw_status[0] = fw_status[0];
1631 		els->u.els_plogi.fw_status[1] = fw_status[1];
1632 		els->u.els_plogi.fw_status[2] = fw_status[2];
1633 		els->u.els_plogi.comp_status = fw_status[0];
1634 		if (comp_status == CS_COMPLETE) {
1635 			res =  DID_OK << 16;
1636 		} else {
1637 			if (comp_status == CS_DATA_UNDERRUN) {
1638 				res =  DID_OK << 16;
1639 				els->u.els_plogi.len =
1640 				le16_to_cpu(((struct els_sts_entry_24xx *)
1641 					pkt)->total_byte_count);
1642 			} else {
1643 				els->u.els_plogi.len = 0;
1644 				res = DID_ERROR << 16;
1645 			}
1646 		}
1647 		ql_dbg(ql_dbg_user, vha, 0x503f,
1648 		    "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",
1649 		    type, sp->handle, comp_status, fw_status[1], fw_status[2],
1650 		    le16_to_cpu(((struct els_sts_entry_24xx *)
1651 			pkt)->total_byte_count));
1652 		goto els_ct_done;
1653 	}
1654 
1655 	/* return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
1656 	 * fc payload  to the caller
1657 	 */
1658 	bsg_job = sp->u.bsg_job;
1659 	bsg_reply = bsg_job->reply;
1660 	bsg_reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
1661 	bsg_job->reply_len = sizeof(struct fc_bsg_reply) + sizeof(fw_status);
1662 
1663 	if (comp_status != CS_COMPLETE) {
1664 		if (comp_status == CS_DATA_UNDERRUN) {
1665 			res = DID_OK << 16;
1666 			bsg_reply->reply_payload_rcv_len =
1667 			    le16_to_cpu(((struct els_sts_entry_24xx *)pkt)->total_byte_count);
1668 
1669 			ql_dbg(ql_dbg_user, vha, 0x503f,
1670 			    "ELS-CT pass-through-%s error hdl=%x comp_status-status=0x%x "
1671 			    "error subcode 1=0x%x error subcode 2=0x%x total_byte = 0x%x.\n",
1672 			    type, sp->handle, comp_status, fw_status[1], fw_status[2],
1673 			    le16_to_cpu(((struct els_sts_entry_24xx *)
1674 				pkt)->total_byte_count));
1675 		} else {
1676 			ql_dbg(ql_dbg_user, vha, 0x5040,
1677 			    "ELS-CT pass-through-%s error hdl=%x comp_status-status=0x%x "
1678 			    "error subcode 1=0x%x error subcode 2=0x%x.\n",
1679 			    type, sp->handle, comp_status,
1680 			    le16_to_cpu(((struct els_sts_entry_24xx *)
1681 				pkt)->error_subcode_1),
1682 			    le16_to_cpu(((struct els_sts_entry_24xx *)
1683 				    pkt)->error_subcode_2));
1684 			res = DID_ERROR << 16;
1685 			bsg_reply->reply_payload_rcv_len = 0;
1686 		}
1687 		memcpy(bsg_job->reply + sizeof(struct fc_bsg_reply),
1688 		       fw_status, sizeof(fw_status));
1689 		ql_dump_buffer(ql_dbg_user + ql_dbg_buffer, vha, 0x5056,
1690 		    pkt, sizeof(*pkt));
1691 	}
1692 	else {
1693 		res =  DID_OK << 16;
1694 		bsg_reply->reply_payload_rcv_len = bsg_job->reply_payload.payload_len;
1695 		bsg_job->reply_len = 0;
1696 	}
1697 els_ct_done:
1698 
1699 	sp->done(sp, res);
1700 }
1701 
1702 static void
1703 qla24xx_logio_entry(scsi_qla_host_t *vha, struct req_que *req,
1704     struct logio_entry_24xx *logio)
1705 {
1706 	const char func[] = "LOGIO-IOCB";
1707 	const char *type;
1708 	fc_port_t *fcport;
1709 	srb_t *sp;
1710 	struct srb_iocb *lio;
1711 	uint16_t *data;
1712 	uint32_t iop[2];
1713 
1714 	sp = qla2x00_get_sp_from_handle(vha, func, req, logio);
1715 	if (!sp)
1716 		return;
1717 
1718 	lio = &sp->u.iocb_cmd;
1719 	type = sp->name;
1720 	fcport = sp->fcport;
1721 	data = lio->u.logio.data;
1722 
1723 	data[0] = MBS_COMMAND_ERROR;
1724 	data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
1725 		QLA_LOGIO_LOGIN_RETRIED : 0;
1726 	if (logio->entry_status) {
1727 		ql_log(ql_log_warn, fcport->vha, 0x5034,
1728 		    "Async-%s error entry - %8phC hdl=%x"
1729 		    "portid=%02x%02x%02x entry-status=%x.\n",
1730 		    type, fcport->port_name, sp->handle, fcport->d_id.b.domain,
1731 		    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1732 		    logio->entry_status);
1733 		ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x504d,
1734 		    logio, sizeof(*logio));
1735 
1736 		goto logio_done;
1737 	}
1738 
1739 	if (le16_to_cpu(logio->comp_status) == CS_COMPLETE) {
1740 		ql_dbg(ql_dbg_async, fcport->vha, 0x5036,
1741 		    "Async-%s complete - %8phC hdl=%x portid=%02x%02x%02x "
1742 		    "iop0=%x.\n", type, fcport->port_name, sp->handle,
1743 		    fcport->d_id.b.domain,
1744 		    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1745 		    le32_to_cpu(logio->io_parameter[0]));
1746 
1747 		vha->hw->exch_starvation = 0;
1748 		data[0] = MBS_COMMAND_COMPLETE;
1749 
1750 		if (sp->type == SRB_PRLI_CMD) {
1751 			lio->u.logio.iop[0] =
1752 			    le32_to_cpu(logio->io_parameter[0]);
1753 			lio->u.logio.iop[1] =
1754 			    le32_to_cpu(logio->io_parameter[1]);
1755 			goto logio_done;
1756 		}
1757 
1758 		if (sp->type != SRB_LOGIN_CMD)
1759 			goto logio_done;
1760 
1761 		iop[0] = le32_to_cpu(logio->io_parameter[0]);
1762 		if (iop[0] & BIT_4) {
1763 			fcport->port_type = FCT_TARGET;
1764 			if (iop[0] & BIT_8)
1765 				fcport->flags |= FCF_FCP2_DEVICE;
1766 		} else if (iop[0] & BIT_5)
1767 			fcport->port_type = FCT_INITIATOR;
1768 
1769 		if (iop[0] & BIT_7)
1770 			fcport->flags |= FCF_CONF_COMP_SUPPORTED;
1771 
1772 		if (logio->io_parameter[7] || logio->io_parameter[8])
1773 			fcport->supported_classes |= FC_COS_CLASS2;
1774 		if (logio->io_parameter[9] || logio->io_parameter[10])
1775 			fcport->supported_classes |= FC_COS_CLASS3;
1776 
1777 		goto logio_done;
1778 	}
1779 
1780 	iop[0] = le32_to_cpu(logio->io_parameter[0]);
1781 	iop[1] = le32_to_cpu(logio->io_parameter[1]);
1782 	lio->u.logio.iop[0] = iop[0];
1783 	lio->u.logio.iop[1] = iop[1];
1784 	switch (iop[0]) {
1785 	case LSC_SCODE_PORTID_USED:
1786 		data[0] = MBS_PORT_ID_USED;
1787 		data[1] = LSW(iop[1]);
1788 		break;
1789 	case LSC_SCODE_NPORT_USED:
1790 		data[0] = MBS_LOOP_ID_USED;
1791 		break;
1792 	case LSC_SCODE_CMD_FAILED:
1793 		if (iop[1] == 0x0606) {
1794 			/*
1795 			 * PLOGI/PRLI Completed. We must have Recv PLOGI/PRLI,
1796 			 * Target side acked.
1797 			 */
1798 			data[0] = MBS_COMMAND_COMPLETE;
1799 			goto logio_done;
1800 		}
1801 		data[0] = MBS_COMMAND_ERROR;
1802 		break;
1803 	case LSC_SCODE_NOXCB:
1804 		vha->hw->exch_starvation++;
1805 		if (vha->hw->exch_starvation > 5) {
1806 			ql_log(ql_log_warn, vha, 0xd046,
1807 			    "Exchange starvation. Resetting RISC\n");
1808 
1809 			vha->hw->exch_starvation = 0;
1810 
1811 			if (IS_P3P_TYPE(vha->hw))
1812 				set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1813 			else
1814 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1815 			qla2xxx_wake_dpc(vha);
1816 		}
1817 		/* fall through */
1818 	default:
1819 		data[0] = MBS_COMMAND_ERROR;
1820 		break;
1821 	}
1822 
1823 	ql_dbg(ql_dbg_async, fcport->vha, 0x5037,
1824 	    "Async-%s failed - %8phC hdl=%x portid=%02x%02x%02x comp=%x "
1825 	    "iop0=%x iop1=%x.\n", type, fcport->port_name,
1826 		sp->handle, fcport->d_id.b.domain,
1827 	    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1828 	    le16_to_cpu(logio->comp_status),
1829 	    le32_to_cpu(logio->io_parameter[0]),
1830 	    le32_to_cpu(logio->io_parameter[1]));
1831 
1832 logio_done:
1833 	sp->done(sp, 0);
1834 }
1835 
1836 static void
1837 qla24xx_tm_iocb_entry(scsi_qla_host_t *vha, struct req_que *req, void *tsk)
1838 {
1839 	const char func[] = "TMF-IOCB";
1840 	const char *type;
1841 	fc_port_t *fcport;
1842 	srb_t *sp;
1843 	struct srb_iocb *iocb;
1844 	struct sts_entry_24xx *sts = (struct sts_entry_24xx *)tsk;
1845 
1846 	sp = qla2x00_get_sp_from_handle(vha, func, req, tsk);
1847 	if (!sp)
1848 		return;
1849 
1850 	iocb = &sp->u.iocb_cmd;
1851 	type = sp->name;
1852 	fcport = sp->fcport;
1853 	iocb->u.tmf.data = QLA_SUCCESS;
1854 
1855 	if (sts->entry_status) {
1856 		ql_log(ql_log_warn, fcport->vha, 0x5038,
1857 		    "Async-%s error - hdl=%x entry-status(%x).\n",
1858 		    type, sp->handle, sts->entry_status);
1859 		iocb->u.tmf.data = QLA_FUNCTION_FAILED;
1860 	} else if (sts->comp_status != cpu_to_le16(CS_COMPLETE)) {
1861 		ql_log(ql_log_warn, fcport->vha, 0x5039,
1862 		    "Async-%s error - hdl=%x completion status(%x).\n",
1863 		    type, sp->handle, sts->comp_status);
1864 		iocb->u.tmf.data = QLA_FUNCTION_FAILED;
1865 	} else if ((le16_to_cpu(sts->scsi_status) &
1866 	    SS_RESPONSE_INFO_LEN_VALID)) {
1867 		if (le32_to_cpu(sts->rsp_data_len) < 4) {
1868 			ql_log(ql_log_warn, fcport->vha, 0x503b,
1869 			    "Async-%s error - hdl=%x not enough response(%d).\n",
1870 			    type, sp->handle, sts->rsp_data_len);
1871 		} else if (sts->data[3]) {
1872 			ql_log(ql_log_warn, fcport->vha, 0x503c,
1873 			    "Async-%s error - hdl=%x response(%x).\n",
1874 			    type, sp->handle, sts->data[3]);
1875 			iocb->u.tmf.data = QLA_FUNCTION_FAILED;
1876 		}
1877 	}
1878 
1879 	if (iocb->u.tmf.data != QLA_SUCCESS)
1880 		ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, sp->vha, 0x5055,
1881 		    sts, sizeof(*sts));
1882 
1883 	sp->done(sp, 0);
1884 }
1885 
1886 static void qla24xx_nvme_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1887     void *tsk, srb_t *sp)
1888 {
1889 	fc_port_t *fcport;
1890 	struct srb_iocb *iocb;
1891 	struct sts_entry_24xx *sts = (struct sts_entry_24xx *)tsk;
1892 	uint16_t        state_flags;
1893 	struct nvmefc_fcp_req *fd;
1894 	uint16_t        ret = QLA_SUCCESS;
1895 	uint16_t	comp_status = le16_to_cpu(sts->comp_status);
1896 
1897 	iocb = &sp->u.iocb_cmd;
1898 	fcport = sp->fcport;
1899 	iocb->u.nvme.comp_status = comp_status;
1900 	state_flags  = le16_to_cpu(sts->state_flags);
1901 	fd = iocb->u.nvme.desc;
1902 
1903 	if (unlikely(iocb->u.nvme.aen_op))
1904 		atomic_dec(&sp->vha->hw->nvme_active_aen_cnt);
1905 
1906 	/*
1907 	 * State flags: Bit 6 and 0.
1908 	 * If 0 is set, we don't care about 6.
1909 	 * both cases resp was dma'd to host buffer
1910 	 * if both are 0, that is good path case.
1911 	 * if six is set and 0 is clear, we need to
1912 	 * copy resp data from status iocb to resp buffer.
1913 	 */
1914 	if (!(state_flags & (SF_FCP_RSP_DMA | SF_NVME_ERSP))) {
1915 		iocb->u.nvme.rsp_pyld_len = 0;
1916 	} else if ((state_flags & SF_FCP_RSP_DMA)) {
1917 		iocb->u.nvme.rsp_pyld_len = le16_to_cpu(sts->nvme_rsp_pyld_len);
1918 	} else if (state_flags & SF_NVME_ERSP) {
1919 		uint32_t *inbuf, *outbuf;
1920 		uint16_t iter;
1921 
1922 		inbuf = (uint32_t *)&sts->nvme_ersp_data;
1923 		outbuf = (uint32_t *)fd->rspaddr;
1924 		iocb->u.nvme.rsp_pyld_len = le16_to_cpu(sts->nvme_rsp_pyld_len);
1925 		iter = iocb->u.nvme.rsp_pyld_len >> 2;
1926 		for (; iter; iter--)
1927 			*outbuf++ = swab32(*inbuf++);
1928 	} else { /* unhandled case */
1929 	    ql_log(ql_log_warn, fcport->vha, 0x503a,
1930 		"NVME-%s error. Unhandled state_flags of %x\n",
1931 		sp->name, state_flags);
1932 	}
1933 
1934 	fd->transferred_length = fd->payload_length -
1935 	    le32_to_cpu(sts->residual_len);
1936 
1937 	if (unlikely(comp_status != CS_COMPLETE))
1938 		ql_log(ql_log_warn, fcport->vha, 0x5060,
1939 		   "NVME-%s ERR Handling - hdl=%x status(%x) tr_len:%x resid=%x  ox_id=%x\n",
1940 		   sp->name, sp->handle, comp_status,
1941 		   fd->transferred_length, le32_to_cpu(sts->residual_len),
1942 		   sts->ox_id);
1943 
1944 	/*
1945 	 * If transport error then Failure (HBA rejects request)
1946 	 * otherwise transport will handle.
1947 	 */
1948 	switch (comp_status) {
1949 	case CS_COMPLETE:
1950 		break;
1951 
1952 	case CS_RESET:
1953 	case CS_PORT_UNAVAILABLE:
1954 	case CS_PORT_LOGGED_OUT:
1955 		fcport->nvme_flag |= NVME_FLAG_RESETTING;
1956 		/* fall through */
1957 	case CS_ABORTED:
1958 	case CS_PORT_BUSY:
1959 		fd->transferred_length = 0;
1960 		iocb->u.nvme.rsp_pyld_len = 0;
1961 		ret = QLA_ABORTED;
1962 		break;
1963 	case CS_DATA_UNDERRUN:
1964 		break;
1965 	default:
1966 		ret = QLA_FUNCTION_FAILED;
1967 		break;
1968 	}
1969 	sp->done(sp, ret);
1970 }
1971 
1972 static void qla_ctrlvp_completed(scsi_qla_host_t *vha, struct req_que *req,
1973     struct vp_ctrl_entry_24xx *vce)
1974 {
1975 	const char func[] = "CTRLVP-IOCB";
1976 	srb_t *sp;
1977 	int rval = QLA_SUCCESS;
1978 
1979 	sp = qla2x00_get_sp_from_handle(vha, func, req, vce);
1980 	if (!sp)
1981 		return;
1982 
1983 	if (vce->entry_status != 0) {
1984 		ql_dbg(ql_dbg_vport, vha, 0x10c4,
1985 		    "%s: Failed to complete IOCB -- error status (%x)\n",
1986 		    sp->name, vce->entry_status);
1987 		rval = QLA_FUNCTION_FAILED;
1988 	} else if (vce->comp_status != cpu_to_le16(CS_COMPLETE)) {
1989 		ql_dbg(ql_dbg_vport, vha, 0x10c5,
1990 		    "%s: Failed to complete IOCB -- completion status (%x) vpidx %x\n",
1991 		    sp->name, le16_to_cpu(vce->comp_status),
1992 		    le16_to_cpu(vce->vp_idx_failed));
1993 		rval = QLA_FUNCTION_FAILED;
1994 	} else {
1995 		ql_dbg(ql_dbg_vport, vha, 0x10c6,
1996 		    "Done %s.\n", __func__);
1997 	}
1998 
1999 	sp->rc = rval;
2000 	sp->done(sp, rval);
2001 }
2002 
2003 /* Process a single response queue entry. */
2004 static void qla2x00_process_response_entry(struct scsi_qla_host *vha,
2005 					   struct rsp_que *rsp,
2006 					   sts_entry_t *pkt)
2007 {
2008 	sts21_entry_t *sts21_entry;
2009 	sts22_entry_t *sts22_entry;
2010 	uint16_t handle_cnt;
2011 	uint16_t cnt;
2012 
2013 	switch (pkt->entry_type) {
2014 	case STATUS_TYPE:
2015 		qla2x00_status_entry(vha, rsp, pkt);
2016 		break;
2017 	case STATUS_TYPE_21:
2018 		sts21_entry = (sts21_entry_t *)pkt;
2019 		handle_cnt = sts21_entry->handle_count;
2020 		for (cnt = 0; cnt < handle_cnt; cnt++)
2021 			qla2x00_process_completed_request(vha, rsp->req,
2022 						sts21_entry->handle[cnt]);
2023 		break;
2024 	case STATUS_TYPE_22:
2025 		sts22_entry = (sts22_entry_t *)pkt;
2026 		handle_cnt = sts22_entry->handle_count;
2027 		for (cnt = 0; cnt < handle_cnt; cnt++)
2028 			qla2x00_process_completed_request(vha, rsp->req,
2029 						sts22_entry->handle[cnt]);
2030 		break;
2031 	case STATUS_CONT_TYPE:
2032 		qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
2033 		break;
2034 	case MBX_IOCB_TYPE:
2035 		qla2x00_mbx_iocb_entry(vha, rsp->req, (struct mbx_entry *)pkt);
2036 		break;
2037 	case CT_IOCB_TYPE:
2038 		qla2x00_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
2039 		break;
2040 	default:
2041 		/* Type Not Supported. */
2042 		ql_log(ql_log_warn, vha, 0x504a,
2043 		       "Received unknown response pkt type %x entry status=%x.\n",
2044 		       pkt->entry_type, pkt->entry_status);
2045 		break;
2046 	}
2047 }
2048 
2049 /**
2050  * qla2x00_process_response_queue() - Process response queue entries.
2051  * @rsp: response queue
2052  */
2053 void
2054 qla2x00_process_response_queue(struct rsp_que *rsp)
2055 {
2056 	struct scsi_qla_host *vha;
2057 	struct qla_hw_data *ha = rsp->hw;
2058 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2059 	sts_entry_t	*pkt;
2060 
2061 	vha = pci_get_drvdata(ha->pdev);
2062 
2063 	if (!vha->flags.online)
2064 		return;
2065 
2066 	while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
2067 		pkt = (sts_entry_t *)rsp->ring_ptr;
2068 
2069 		rsp->ring_index++;
2070 		if (rsp->ring_index == rsp->length) {
2071 			rsp->ring_index = 0;
2072 			rsp->ring_ptr = rsp->ring;
2073 		} else {
2074 			rsp->ring_ptr++;
2075 		}
2076 
2077 		if (pkt->entry_status != 0) {
2078 			qla2x00_error_entry(vha, rsp, pkt);
2079 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
2080 			wmb();
2081 			continue;
2082 		}
2083 
2084 		qla2x00_process_response_entry(vha, rsp, pkt);
2085 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
2086 		wmb();
2087 	}
2088 
2089 	/* Adjust ring index */
2090 	WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), rsp->ring_index);
2091 }
2092 
2093 static inline void
2094 qla2x00_handle_sense(srb_t *sp, uint8_t *sense_data, uint32_t par_sense_len,
2095 		     uint32_t sense_len, struct rsp_que *rsp, int res)
2096 {
2097 	struct scsi_qla_host *vha = sp->vha;
2098 	struct scsi_cmnd *cp = GET_CMD_SP(sp);
2099 	uint32_t track_sense_len;
2100 
2101 	if (sense_len >= SCSI_SENSE_BUFFERSIZE)
2102 		sense_len = SCSI_SENSE_BUFFERSIZE;
2103 
2104 	SET_CMD_SENSE_LEN(sp, sense_len);
2105 	SET_CMD_SENSE_PTR(sp, cp->sense_buffer);
2106 	track_sense_len = sense_len;
2107 
2108 	if (sense_len > par_sense_len)
2109 		sense_len = par_sense_len;
2110 
2111 	memcpy(cp->sense_buffer, sense_data, sense_len);
2112 
2113 	SET_CMD_SENSE_PTR(sp, cp->sense_buffer + sense_len);
2114 	track_sense_len -= sense_len;
2115 	SET_CMD_SENSE_LEN(sp, track_sense_len);
2116 
2117 	if (track_sense_len != 0) {
2118 		rsp->status_srb = sp;
2119 		cp->result = res;
2120 	}
2121 
2122 	if (sense_len) {
2123 		ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x301c,
2124 		    "Check condition Sense data, nexus%ld:%d:%llu cmd=%p.\n",
2125 		    sp->vha->host_no, cp->device->id, cp->device->lun,
2126 		    cp);
2127 		ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302b,
2128 		    cp->sense_buffer, sense_len);
2129 	}
2130 }
2131 
2132 struct scsi_dif_tuple {
2133 	__be16 guard;       /* Checksum */
2134 	__be16 app_tag;         /* APPL identifier */
2135 	__be32 ref_tag;         /* Target LBA or indirect LBA */
2136 };
2137 
2138 /*
2139  * Checks the guard or meta-data for the type of error
2140  * detected by the HBA. In case of errors, we set the
2141  * ASC/ASCQ fields in the sense buffer with ILLEGAL_REQUEST
2142  * to indicate to the kernel that the HBA detected error.
2143  */
2144 static inline int
2145 qla2x00_handle_dif_error(srb_t *sp, struct sts_entry_24xx *sts24)
2146 {
2147 	struct scsi_qla_host *vha = sp->vha;
2148 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
2149 	uint8_t		*ap = &sts24->data[12];
2150 	uint8_t		*ep = &sts24->data[20];
2151 	uint32_t	e_ref_tag, a_ref_tag;
2152 	uint16_t	e_app_tag, a_app_tag;
2153 	uint16_t	e_guard, a_guard;
2154 
2155 	/*
2156 	 * swab32 of the "data" field in the beginning of qla2x00_status_entry()
2157 	 * would make guard field appear at offset 2
2158 	 */
2159 	a_guard   = le16_to_cpu(*(uint16_t *)(ap + 2));
2160 	a_app_tag = le16_to_cpu(*(uint16_t *)(ap + 0));
2161 	a_ref_tag = le32_to_cpu(*(uint32_t *)(ap + 4));
2162 	e_guard   = le16_to_cpu(*(uint16_t *)(ep + 2));
2163 	e_app_tag = le16_to_cpu(*(uint16_t *)(ep + 0));
2164 	e_ref_tag = le32_to_cpu(*(uint32_t *)(ep + 4));
2165 
2166 	ql_dbg(ql_dbg_io, vha, 0x3023,
2167 	    "iocb(s) %p Returned STATUS.\n", sts24);
2168 
2169 	ql_dbg(ql_dbg_io, vha, 0x3024,
2170 	    "DIF ERROR in cmd 0x%x lba 0x%llx act ref"
2171 	    " tag=0x%x, exp ref_tag=0x%x, act app tag=0x%x, exp app"
2172 	    " tag=0x%x, act guard=0x%x, exp guard=0x%x.\n",
2173 	    cmd->cmnd[0], (u64)scsi_get_lba(cmd), a_ref_tag, e_ref_tag,
2174 	    a_app_tag, e_app_tag, a_guard, e_guard);
2175 
2176 	/*
2177 	 * Ignore sector if:
2178 	 * For type     3: ref & app tag is all 'f's
2179 	 * For type 0,1,2: app tag is all 'f's
2180 	 */
2181 	if ((a_app_tag == T10_PI_APP_ESCAPE) &&
2182 	    ((scsi_get_prot_type(cmd) != SCSI_PROT_DIF_TYPE3) ||
2183 	     (a_ref_tag == T10_PI_REF_ESCAPE))) {
2184 		uint32_t blocks_done, resid;
2185 		sector_t lba_s = scsi_get_lba(cmd);
2186 
2187 		/* 2TB boundary case covered automatically with this */
2188 		blocks_done = e_ref_tag - (uint32_t)lba_s + 1;
2189 
2190 		resid = scsi_bufflen(cmd) - (blocks_done *
2191 		    cmd->device->sector_size);
2192 
2193 		scsi_set_resid(cmd, resid);
2194 		cmd->result = DID_OK << 16;
2195 
2196 		/* Update protection tag */
2197 		if (scsi_prot_sg_count(cmd)) {
2198 			uint32_t i, j = 0, k = 0, num_ent;
2199 			struct scatterlist *sg;
2200 			struct t10_pi_tuple *spt;
2201 
2202 			/* Patch the corresponding protection tags */
2203 			scsi_for_each_prot_sg(cmd, sg,
2204 			    scsi_prot_sg_count(cmd), i) {
2205 				num_ent = sg_dma_len(sg) / 8;
2206 				if (k + num_ent < blocks_done) {
2207 					k += num_ent;
2208 					continue;
2209 				}
2210 				j = blocks_done - k - 1;
2211 				k = blocks_done;
2212 				break;
2213 			}
2214 
2215 			if (k != blocks_done) {
2216 				ql_log(ql_log_warn, vha, 0x302f,
2217 				    "unexpected tag values tag:lba=%x:%llx)\n",
2218 				    e_ref_tag, (unsigned long long)lba_s);
2219 				return 1;
2220 			}
2221 
2222 			spt = page_address(sg_page(sg)) + sg->offset;
2223 			spt += j;
2224 
2225 			spt->app_tag = T10_PI_APP_ESCAPE;
2226 			if (scsi_get_prot_type(cmd) == SCSI_PROT_DIF_TYPE3)
2227 				spt->ref_tag = T10_PI_REF_ESCAPE;
2228 		}
2229 
2230 		return 0;
2231 	}
2232 
2233 	/* check guard */
2234 	if (e_guard != a_guard) {
2235 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2236 		    0x10, 0x1);
2237 		set_driver_byte(cmd, DRIVER_SENSE);
2238 		set_host_byte(cmd, DID_ABORT);
2239 		cmd->result |= SAM_STAT_CHECK_CONDITION;
2240 		return 1;
2241 	}
2242 
2243 	/* check ref tag */
2244 	if (e_ref_tag != a_ref_tag) {
2245 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2246 		    0x10, 0x3);
2247 		set_driver_byte(cmd, DRIVER_SENSE);
2248 		set_host_byte(cmd, DID_ABORT);
2249 		cmd->result |= SAM_STAT_CHECK_CONDITION;
2250 		return 1;
2251 	}
2252 
2253 	/* check appl tag */
2254 	if (e_app_tag != a_app_tag) {
2255 		scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2256 		    0x10, 0x2);
2257 		set_driver_byte(cmd, DRIVER_SENSE);
2258 		set_host_byte(cmd, DID_ABORT);
2259 		cmd->result |= SAM_STAT_CHECK_CONDITION;
2260 		return 1;
2261 	}
2262 
2263 	return 1;
2264 }
2265 
2266 static void
2267 qla25xx_process_bidir_status_iocb(scsi_qla_host_t *vha, void *pkt,
2268 				  struct req_que *req, uint32_t index)
2269 {
2270 	struct qla_hw_data *ha = vha->hw;
2271 	srb_t *sp;
2272 	uint16_t	comp_status;
2273 	uint16_t	scsi_status;
2274 	uint16_t thread_id;
2275 	uint32_t rval = EXT_STATUS_OK;
2276 	struct bsg_job *bsg_job = NULL;
2277 	struct fc_bsg_request *bsg_request;
2278 	struct fc_bsg_reply *bsg_reply;
2279 	sts_entry_t *sts = pkt;
2280 	struct sts_entry_24xx *sts24 = pkt;
2281 
2282 	/* Validate handle. */
2283 	if (index >= req->num_outstanding_cmds) {
2284 		ql_log(ql_log_warn, vha, 0x70af,
2285 		    "Invalid SCSI completion handle 0x%x.\n", index);
2286 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2287 		return;
2288 	}
2289 
2290 	sp = req->outstanding_cmds[index];
2291 	if (!sp) {
2292 		ql_log(ql_log_warn, vha, 0x70b0,
2293 		    "Req:%d: Invalid ISP SCSI completion handle(0x%x)\n",
2294 		    req->id, index);
2295 
2296 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2297 		return;
2298 	}
2299 
2300 	/* Free outstanding command slot. */
2301 	req->outstanding_cmds[index] = NULL;
2302 	bsg_job = sp->u.bsg_job;
2303 	bsg_request = bsg_job->request;
2304 	bsg_reply = bsg_job->reply;
2305 
2306 	if (IS_FWI2_CAPABLE(ha)) {
2307 		comp_status = le16_to_cpu(sts24->comp_status);
2308 		scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
2309 	} else {
2310 		comp_status = le16_to_cpu(sts->comp_status);
2311 		scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
2312 	}
2313 
2314 	thread_id = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
2315 	switch (comp_status) {
2316 	case CS_COMPLETE:
2317 		if (scsi_status == 0) {
2318 			bsg_reply->reply_payload_rcv_len =
2319 					bsg_job->reply_payload.payload_len;
2320 			vha->qla_stats.input_bytes +=
2321 				bsg_reply->reply_payload_rcv_len;
2322 			vha->qla_stats.input_requests++;
2323 			rval = EXT_STATUS_OK;
2324 		}
2325 		goto done;
2326 
2327 	case CS_DATA_OVERRUN:
2328 		ql_dbg(ql_dbg_user, vha, 0x70b1,
2329 		    "Command completed with data overrun thread_id=%d\n",
2330 		    thread_id);
2331 		rval = EXT_STATUS_DATA_OVERRUN;
2332 		break;
2333 
2334 	case CS_DATA_UNDERRUN:
2335 		ql_dbg(ql_dbg_user, vha, 0x70b2,
2336 		    "Command completed with data underrun thread_id=%d\n",
2337 		    thread_id);
2338 		rval = EXT_STATUS_DATA_UNDERRUN;
2339 		break;
2340 	case CS_BIDIR_RD_OVERRUN:
2341 		ql_dbg(ql_dbg_user, vha, 0x70b3,
2342 		    "Command completed with read data overrun thread_id=%d\n",
2343 		    thread_id);
2344 		rval = EXT_STATUS_DATA_OVERRUN;
2345 		break;
2346 
2347 	case CS_BIDIR_RD_WR_OVERRUN:
2348 		ql_dbg(ql_dbg_user, vha, 0x70b4,
2349 		    "Command completed with read and write data overrun "
2350 		    "thread_id=%d\n", thread_id);
2351 		rval = EXT_STATUS_DATA_OVERRUN;
2352 		break;
2353 
2354 	case CS_BIDIR_RD_OVERRUN_WR_UNDERRUN:
2355 		ql_dbg(ql_dbg_user, vha, 0x70b5,
2356 		    "Command completed with read data over and write data "
2357 		    "underrun thread_id=%d\n", thread_id);
2358 		rval = EXT_STATUS_DATA_OVERRUN;
2359 		break;
2360 
2361 	case CS_BIDIR_RD_UNDERRUN:
2362 		ql_dbg(ql_dbg_user, vha, 0x70b6,
2363 		    "Command completed with read data underrun "
2364 		    "thread_id=%d\n", thread_id);
2365 		rval = EXT_STATUS_DATA_UNDERRUN;
2366 		break;
2367 
2368 	case CS_BIDIR_RD_UNDERRUN_WR_OVERRUN:
2369 		ql_dbg(ql_dbg_user, vha, 0x70b7,
2370 		    "Command completed with read data under and write data "
2371 		    "overrun thread_id=%d\n", thread_id);
2372 		rval = EXT_STATUS_DATA_UNDERRUN;
2373 		break;
2374 
2375 	case CS_BIDIR_RD_WR_UNDERRUN:
2376 		ql_dbg(ql_dbg_user, vha, 0x70b8,
2377 		    "Command completed with read and write data underrun "
2378 		    "thread_id=%d\n", thread_id);
2379 		rval = EXT_STATUS_DATA_UNDERRUN;
2380 		break;
2381 
2382 	case CS_BIDIR_DMA:
2383 		ql_dbg(ql_dbg_user, vha, 0x70b9,
2384 		    "Command completed with data DMA error thread_id=%d\n",
2385 		    thread_id);
2386 		rval = EXT_STATUS_DMA_ERR;
2387 		break;
2388 
2389 	case CS_TIMEOUT:
2390 		ql_dbg(ql_dbg_user, vha, 0x70ba,
2391 		    "Command completed with timeout thread_id=%d\n",
2392 		    thread_id);
2393 		rval = EXT_STATUS_TIMEOUT;
2394 		break;
2395 	default:
2396 		ql_dbg(ql_dbg_user, vha, 0x70bb,
2397 		    "Command completed with completion status=0x%x "
2398 		    "thread_id=%d\n", comp_status, thread_id);
2399 		rval = EXT_STATUS_ERR;
2400 		break;
2401 	}
2402 	bsg_reply->reply_payload_rcv_len = 0;
2403 
2404 done:
2405 	/* Return the vendor specific reply to API */
2406 	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
2407 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2408 	/* Always return DID_OK, bsg will send the vendor specific response
2409 	 * in this case only */
2410 	sp->done(sp, DID_OK << 16);
2411 
2412 }
2413 
2414 /**
2415  * qla2x00_status_entry() - Process a Status IOCB entry.
2416  * @vha: SCSI driver HA context
2417  * @rsp: response queue
2418  * @pkt: Entry pointer
2419  */
2420 static void
2421 qla2x00_status_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, void *pkt)
2422 {
2423 	srb_t		*sp;
2424 	fc_port_t	*fcport;
2425 	struct scsi_cmnd *cp;
2426 	sts_entry_t *sts = pkt;
2427 	struct sts_entry_24xx *sts24 = pkt;
2428 	uint16_t	comp_status;
2429 	uint16_t	scsi_status;
2430 	uint16_t	ox_id;
2431 	uint8_t		lscsi_status;
2432 	int32_t		resid;
2433 	uint32_t sense_len, par_sense_len, rsp_info_len, resid_len,
2434 	    fw_resid_len;
2435 	uint8_t		*rsp_info, *sense_data;
2436 	struct qla_hw_data *ha = vha->hw;
2437 	uint32_t handle;
2438 	uint16_t que;
2439 	struct req_que *req;
2440 	int logit = 1;
2441 	int res = 0;
2442 	uint16_t state_flags = 0;
2443 	uint16_t retry_delay = 0;
2444 
2445 	if (IS_FWI2_CAPABLE(ha)) {
2446 		comp_status = le16_to_cpu(sts24->comp_status);
2447 		scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
2448 		state_flags = le16_to_cpu(sts24->state_flags);
2449 	} else {
2450 		comp_status = le16_to_cpu(sts->comp_status);
2451 		scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
2452 	}
2453 	handle = (uint32_t) LSW(sts->handle);
2454 	que = MSW(sts->handle);
2455 	req = ha->req_q_map[que];
2456 
2457 	/* Check for invalid queue pointer */
2458 	if (req == NULL ||
2459 	    que >= find_first_zero_bit(ha->req_qid_map, ha->max_req_queues)) {
2460 		ql_dbg(ql_dbg_io, vha, 0x3059,
2461 		    "Invalid status handle (0x%x): Bad req pointer. req=%p, "
2462 		    "que=%u.\n", sts->handle, req, que);
2463 		return;
2464 	}
2465 
2466 	/* Validate handle. */
2467 	if (handle < req->num_outstanding_cmds) {
2468 		sp = req->outstanding_cmds[handle];
2469 		if (!sp) {
2470 			ql_dbg(ql_dbg_io, vha, 0x3075,
2471 			    "%s(%ld): Already returned command for status handle (0x%x).\n",
2472 			    __func__, vha->host_no, sts->handle);
2473 			return;
2474 		}
2475 	} else {
2476 		ql_dbg(ql_dbg_io, vha, 0x3017,
2477 		    "Invalid status handle, out of range (0x%x).\n",
2478 		    sts->handle);
2479 
2480 		if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
2481 			if (IS_P3P_TYPE(ha))
2482 				set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
2483 			else
2484 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2485 			qla2xxx_wake_dpc(vha);
2486 		}
2487 		return;
2488 	}
2489 
2490 	if (sp->abort)
2491 		sp->aborted = 1;
2492 	else
2493 		sp->completed = 1;
2494 
2495 	if (sp->cmd_type != TYPE_SRB) {
2496 		req->outstanding_cmds[handle] = NULL;
2497 		ql_dbg(ql_dbg_io, vha, 0x3015,
2498 		    "Unknown sp->cmd_type %x %p).\n",
2499 		    sp->cmd_type, sp);
2500 		return;
2501 	}
2502 
2503 	/* NVME completion. */
2504 	if (sp->type == SRB_NVME_CMD) {
2505 		req->outstanding_cmds[handle] = NULL;
2506 		qla24xx_nvme_iocb_entry(vha, req, pkt, sp);
2507 		return;
2508 	}
2509 
2510 	if (unlikely((state_flags & BIT_1) && (sp->type == SRB_BIDI_CMD))) {
2511 		qla25xx_process_bidir_status_iocb(vha, pkt, req, handle);
2512 		return;
2513 	}
2514 
2515 	/* Task Management completion. */
2516 	if (sp->type == SRB_TM_CMD) {
2517 		qla24xx_tm_iocb_entry(vha, req, pkt);
2518 		return;
2519 	}
2520 
2521 	/* Fast path completion. */
2522 	if (comp_status == CS_COMPLETE && scsi_status == 0) {
2523 		qla2x00_process_completed_request(vha, req, handle);
2524 
2525 		return;
2526 	}
2527 
2528 	req->outstanding_cmds[handle] = NULL;
2529 	cp = GET_CMD_SP(sp);
2530 	if (cp == NULL) {
2531 		ql_dbg(ql_dbg_io, vha, 0x3018,
2532 		    "Command already returned (0x%x/%p).\n",
2533 		    sts->handle, sp);
2534 
2535 		return;
2536 	}
2537 
2538 	lscsi_status = scsi_status & STATUS_MASK;
2539 
2540 	fcport = sp->fcport;
2541 
2542 	ox_id = 0;
2543 	sense_len = par_sense_len = rsp_info_len = resid_len =
2544 	    fw_resid_len = 0;
2545 	if (IS_FWI2_CAPABLE(ha)) {
2546 		if (scsi_status & SS_SENSE_LEN_VALID)
2547 			sense_len = le32_to_cpu(sts24->sense_len);
2548 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
2549 			rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
2550 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER))
2551 			resid_len = le32_to_cpu(sts24->rsp_residual_count);
2552 		if (comp_status == CS_DATA_UNDERRUN)
2553 			fw_resid_len = le32_to_cpu(sts24->residual_len);
2554 		rsp_info = sts24->data;
2555 		sense_data = sts24->data;
2556 		host_to_fcp_swap(sts24->data, sizeof(sts24->data));
2557 		ox_id = le16_to_cpu(sts24->ox_id);
2558 		par_sense_len = sizeof(sts24->data);
2559 		/* Valid values of the retry delay timer are 0x1-0xffef */
2560 		if (sts24->retry_delay > 0 && sts24->retry_delay < 0xfff1) {
2561 			retry_delay = sts24->retry_delay & 0x3fff;
2562 			ql_dbg(ql_dbg_io, sp->vha, 0x3033,
2563 			    "%s: scope=%#x retry_delay=%#x\n", __func__,
2564 			    sts24->retry_delay >> 14, retry_delay);
2565 		}
2566 	} else {
2567 		if (scsi_status & SS_SENSE_LEN_VALID)
2568 			sense_len = le16_to_cpu(sts->req_sense_length);
2569 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
2570 			rsp_info_len = le16_to_cpu(sts->rsp_info_len);
2571 		resid_len = le32_to_cpu(sts->residual_length);
2572 		rsp_info = sts->rsp_info;
2573 		sense_data = sts->req_sense_data;
2574 		par_sense_len = sizeof(sts->req_sense_data);
2575 	}
2576 
2577 	/* Check for any FCP transport errors. */
2578 	if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
2579 		/* Sense data lies beyond any FCP RESPONSE data. */
2580 		if (IS_FWI2_CAPABLE(ha)) {
2581 			sense_data += rsp_info_len;
2582 			par_sense_len -= rsp_info_len;
2583 		}
2584 		if (rsp_info_len > 3 && rsp_info[3]) {
2585 			ql_dbg(ql_dbg_io, fcport->vha, 0x3019,
2586 			    "FCP I/O protocol failure (0x%x/0x%x).\n",
2587 			    rsp_info_len, rsp_info[3]);
2588 
2589 			res = DID_BUS_BUSY << 16;
2590 			goto out;
2591 		}
2592 	}
2593 
2594 	/* Check for overrun. */
2595 	if (IS_FWI2_CAPABLE(ha) && comp_status == CS_COMPLETE &&
2596 	    scsi_status & SS_RESIDUAL_OVER)
2597 		comp_status = CS_DATA_OVERRUN;
2598 
2599 	/*
2600 	 * Check retry_delay_timer value if we receive a busy or
2601 	 * queue full.
2602 	 */
2603 	if (lscsi_status == SAM_STAT_TASK_SET_FULL ||
2604 	    lscsi_status == SAM_STAT_BUSY)
2605 		qla2x00_set_retry_delay_timestamp(fcport, retry_delay);
2606 
2607 	/*
2608 	 * Based on Host and scsi status generate status code for Linux
2609 	 */
2610 	switch (comp_status) {
2611 	case CS_COMPLETE:
2612 	case CS_QUEUE_FULL:
2613 		if (scsi_status == 0) {
2614 			res = DID_OK << 16;
2615 			break;
2616 		}
2617 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
2618 			resid = resid_len;
2619 			scsi_set_resid(cp, resid);
2620 
2621 			if (!lscsi_status &&
2622 			    ((unsigned)(scsi_bufflen(cp) - resid) <
2623 			     cp->underflow)) {
2624 				ql_dbg(ql_dbg_io, fcport->vha, 0x301a,
2625 				    "Mid-layer underflow detected (0x%x of 0x%x bytes).\n",
2626 				    resid, scsi_bufflen(cp));
2627 
2628 				res = DID_ERROR << 16;
2629 				break;
2630 			}
2631 		}
2632 		res = DID_OK << 16 | lscsi_status;
2633 
2634 		if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
2635 			ql_dbg(ql_dbg_io, fcport->vha, 0x301b,
2636 			    "QUEUE FULL detected.\n");
2637 			break;
2638 		}
2639 		logit = 0;
2640 		if (lscsi_status != SS_CHECK_CONDITION)
2641 			break;
2642 
2643 		memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2644 		if (!(scsi_status & SS_SENSE_LEN_VALID))
2645 			break;
2646 
2647 		qla2x00_handle_sense(sp, sense_data, par_sense_len, sense_len,
2648 		    rsp, res);
2649 		break;
2650 
2651 	case CS_DATA_UNDERRUN:
2652 		/* Use F/W calculated residual length. */
2653 		resid = IS_FWI2_CAPABLE(ha) ? fw_resid_len : resid_len;
2654 		scsi_set_resid(cp, resid);
2655 		if (scsi_status & SS_RESIDUAL_UNDER) {
2656 			if (IS_FWI2_CAPABLE(ha) && fw_resid_len != resid_len) {
2657 				ql_dbg(ql_dbg_io, fcport->vha, 0x301d,
2658 				    "Dropped frame(s) detected (0x%x of 0x%x bytes).\n",
2659 				    resid, scsi_bufflen(cp));
2660 
2661 				res = DID_ERROR << 16 | lscsi_status;
2662 				goto check_scsi_status;
2663 			}
2664 
2665 			if (!lscsi_status &&
2666 			    ((unsigned)(scsi_bufflen(cp) - resid) <
2667 			    cp->underflow)) {
2668 				ql_dbg(ql_dbg_io, fcport->vha, 0x301e,
2669 				    "Mid-layer underflow detected (0x%x of 0x%x bytes).\n",
2670 				    resid, scsi_bufflen(cp));
2671 
2672 				res = DID_ERROR << 16;
2673 				break;
2674 			}
2675 		} else if (lscsi_status != SAM_STAT_TASK_SET_FULL &&
2676 			    lscsi_status != SAM_STAT_BUSY) {
2677 			/*
2678 			 * scsi status of task set and busy are considered to be
2679 			 * task not completed.
2680 			 */
2681 
2682 			ql_dbg(ql_dbg_io, fcport->vha, 0x301f,
2683 			    "Dropped frame(s) detected (0x%x of 0x%x bytes).\n",
2684 			    resid, scsi_bufflen(cp));
2685 
2686 			res = DID_ERROR << 16 | lscsi_status;
2687 			goto check_scsi_status;
2688 		} else {
2689 			ql_dbg(ql_dbg_io, fcport->vha, 0x3030,
2690 			    "scsi_status: 0x%x, lscsi_status: 0x%x\n",
2691 			    scsi_status, lscsi_status);
2692 		}
2693 
2694 		res = DID_OK << 16 | lscsi_status;
2695 		logit = 0;
2696 
2697 check_scsi_status:
2698 		/*
2699 		 * Check to see if SCSI Status is non zero. If so report SCSI
2700 		 * Status.
2701 		 */
2702 		if (lscsi_status != 0) {
2703 			if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
2704 				ql_dbg(ql_dbg_io, fcport->vha, 0x3020,
2705 				    "QUEUE FULL detected.\n");
2706 				logit = 1;
2707 				break;
2708 			}
2709 			if (lscsi_status != SS_CHECK_CONDITION)
2710 				break;
2711 
2712 			memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2713 			if (!(scsi_status & SS_SENSE_LEN_VALID))
2714 				break;
2715 
2716 			qla2x00_handle_sense(sp, sense_data, par_sense_len,
2717 			    sense_len, rsp, res);
2718 		}
2719 		break;
2720 
2721 	case CS_PORT_LOGGED_OUT:
2722 	case CS_PORT_CONFIG_CHG:
2723 	case CS_PORT_BUSY:
2724 	case CS_INCOMPLETE:
2725 	case CS_PORT_UNAVAILABLE:
2726 	case CS_TIMEOUT:
2727 	case CS_RESET:
2728 
2729 		/*
2730 		 * We are going to have the fc class block the rport
2731 		 * while we try to recover so instruct the mid layer
2732 		 * to requeue until the class decides how to handle this.
2733 		 */
2734 		res = DID_TRANSPORT_DISRUPTED << 16;
2735 
2736 		if (comp_status == CS_TIMEOUT) {
2737 			if (IS_FWI2_CAPABLE(ha))
2738 				break;
2739 			else if ((le16_to_cpu(sts->status_flags) &
2740 			    SF_LOGOUT_SENT) == 0)
2741 				break;
2742 		}
2743 
2744 		if (atomic_read(&fcport->state) == FCS_ONLINE) {
2745 			ql_dbg(ql_dbg_disc, fcport->vha, 0x3021,
2746 				"Port to be marked lost on fcport=%02x%02x%02x, current "
2747 				"port state= %s comp_status %x.\n", fcport->d_id.b.domain,
2748 				fcport->d_id.b.area, fcport->d_id.b.al_pa,
2749 				port_state_str[FCS_ONLINE],
2750 				comp_status);
2751 
2752 			qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
2753 			qlt_schedule_sess_for_deletion(fcport);
2754 		}
2755 
2756 		break;
2757 
2758 	case CS_ABORTED:
2759 		res = DID_RESET << 16;
2760 		break;
2761 
2762 	case CS_DIF_ERROR:
2763 		logit = qla2x00_handle_dif_error(sp, sts24);
2764 		res = cp->result;
2765 		break;
2766 
2767 	case CS_TRANSPORT:
2768 		res = DID_ERROR << 16;
2769 
2770 		if (!IS_PI_SPLIT_DET_CAPABLE(ha))
2771 			break;
2772 
2773 		if (state_flags & BIT_4)
2774 			scmd_printk(KERN_WARNING, cp,
2775 			    "Unsupported device '%s' found.\n",
2776 			    cp->device->vendor);
2777 		break;
2778 
2779 	case CS_DMA:
2780 		ql_log(ql_log_info, fcport->vha, 0x3022,
2781 		    "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",
2782 		    comp_status, scsi_status, res, vha->host_no,
2783 		    cp->device->id, cp->device->lun, fcport->d_id.b24,
2784 		    ox_id, cp->cmnd, scsi_bufflen(cp), rsp_info_len,
2785 		    resid_len, fw_resid_len, sp, cp);
2786 		ql_dump_buffer(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe0ee,
2787 		    pkt, sizeof(*sts24));
2788 		res = DID_ERROR << 16;
2789 		break;
2790 	default:
2791 		res = DID_ERROR << 16;
2792 		break;
2793 	}
2794 
2795 out:
2796 	if (logit)
2797 		ql_dbg(ql_dbg_io, fcport->vha, 0x3022,
2798 		    "FCP command status: 0x%x-0x%x (0x%x) nexus=%ld:%d:%llu "
2799 		    "portid=%02x%02x%02x oxid=0x%x cdb=%10phN len=0x%x "
2800 		    "rsp_info=0x%x resid=0x%x fw_resid=0x%x sp=%p cp=%p.\n",
2801 		    comp_status, scsi_status, res, vha->host_no,
2802 		    cp->device->id, cp->device->lun, fcport->d_id.b.domain,
2803 		    fcport->d_id.b.area, fcport->d_id.b.al_pa, ox_id,
2804 		    cp->cmnd, scsi_bufflen(cp), rsp_info_len,
2805 		    resid_len, fw_resid_len, sp, cp);
2806 
2807 	if (rsp->status_srb == NULL)
2808 		sp->done(sp, res);
2809 }
2810 
2811 /**
2812  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
2813  * @rsp: response queue
2814  * @pkt: Entry pointer
2815  *
2816  * Extended sense data.
2817  */
2818 static void
2819 qla2x00_status_cont_entry(struct rsp_que *rsp, sts_cont_entry_t *pkt)
2820 {
2821 	uint8_t	sense_sz = 0;
2822 	struct qla_hw_data *ha = rsp->hw;
2823 	struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
2824 	srb_t *sp = rsp->status_srb;
2825 	struct scsi_cmnd *cp;
2826 	uint32_t sense_len;
2827 	uint8_t *sense_ptr;
2828 
2829 	if (!sp || !GET_CMD_SENSE_LEN(sp))
2830 		return;
2831 
2832 	sense_len = GET_CMD_SENSE_LEN(sp);
2833 	sense_ptr = GET_CMD_SENSE_PTR(sp);
2834 
2835 	cp = GET_CMD_SP(sp);
2836 	if (cp == NULL) {
2837 		ql_log(ql_log_warn, vha, 0x3025,
2838 		    "cmd is NULL: already returned to OS (sp=%p).\n", sp);
2839 
2840 		rsp->status_srb = NULL;
2841 		return;
2842 	}
2843 
2844 	if (sense_len > sizeof(pkt->data))
2845 		sense_sz = sizeof(pkt->data);
2846 	else
2847 		sense_sz = sense_len;
2848 
2849 	/* Move sense data. */
2850 	if (IS_FWI2_CAPABLE(ha))
2851 		host_to_fcp_swap(pkt->data, sizeof(pkt->data));
2852 	memcpy(sense_ptr, pkt->data, sense_sz);
2853 	ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302c,
2854 		sense_ptr, sense_sz);
2855 
2856 	sense_len -= sense_sz;
2857 	sense_ptr += sense_sz;
2858 
2859 	SET_CMD_SENSE_PTR(sp, sense_ptr);
2860 	SET_CMD_SENSE_LEN(sp, sense_len);
2861 
2862 	/* Place command on done queue. */
2863 	if (sense_len == 0) {
2864 		rsp->status_srb = NULL;
2865 		sp->done(sp, cp->result);
2866 	}
2867 }
2868 
2869 /**
2870  * qla2x00_error_entry() - Process an error entry.
2871  * @vha: SCSI driver HA context
2872  * @rsp: response queue
2873  * @pkt: Entry pointer
2874  * return : 1=allow further error analysis. 0=no additional error analysis.
2875  */
2876 static int
2877 qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
2878 {
2879 	srb_t *sp;
2880 	struct qla_hw_data *ha = vha->hw;
2881 	const char func[] = "ERROR-IOCB";
2882 	uint16_t que = MSW(pkt->handle);
2883 	struct req_que *req = NULL;
2884 	int res = DID_ERROR << 16;
2885 
2886 	ql_dbg(ql_dbg_async, vha, 0x502a,
2887 	    "iocb type %xh with error status %xh, handle %xh, rspq id %d\n",
2888 	    pkt->entry_type, pkt->entry_status, pkt->handle, rsp->id);
2889 
2890 	if (que >= ha->max_req_queues || !ha->req_q_map[que])
2891 		goto fatal;
2892 
2893 	req = ha->req_q_map[que];
2894 
2895 	if (pkt->entry_status & RF_BUSY)
2896 		res = DID_BUS_BUSY << 16;
2897 
2898 	if ((pkt->handle & ~QLA_TGT_HANDLE_MASK) == QLA_TGT_SKIP_HANDLE)
2899 		return 0;
2900 
2901 	switch (pkt->entry_type) {
2902 	case NOTIFY_ACK_TYPE:
2903 	case STATUS_TYPE:
2904 	case STATUS_CONT_TYPE:
2905 	case LOGINOUT_PORT_IOCB_TYPE:
2906 	case CT_IOCB_TYPE:
2907 	case ELS_IOCB_TYPE:
2908 	case ABORT_IOCB_TYPE:
2909 	case MBX_IOCB_TYPE:
2910 	default:
2911 		sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2912 		if (sp) {
2913 			sp->done(sp, res);
2914 			return 0;
2915 		}
2916 		break;
2917 
2918 	case ABTS_RESP_24XX:
2919 	case CTIO_TYPE7:
2920 	case CTIO_CRC2:
2921 		return 1;
2922 	}
2923 fatal:
2924 	ql_log(ql_log_warn, vha, 0x5030,
2925 	    "Error entry - invalid handle/queue (%04x).\n", que);
2926 	return 0;
2927 }
2928 
2929 /**
2930  * qla24xx_mbx_completion() - Process mailbox command completions.
2931  * @vha: SCSI driver HA context
2932  * @mb0: Mailbox0 register
2933  */
2934 static void
2935 qla24xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
2936 {
2937 	uint16_t	cnt;
2938 	uint32_t	mboxes;
2939 	uint16_t __iomem *wptr;
2940 	struct qla_hw_data *ha = vha->hw;
2941 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2942 
2943 	/* Read all mbox registers? */
2944 	WARN_ON_ONCE(ha->mbx_count > 32);
2945 	mboxes = (1ULL << ha->mbx_count) - 1;
2946 	if (!ha->mcp)
2947 		ql_dbg(ql_dbg_async, vha, 0x504e, "MBX pointer ERROR.\n");
2948 	else
2949 		mboxes = ha->mcp->in_mb;
2950 
2951 	/* Load return mailbox registers. */
2952 	ha->flags.mbox_int = 1;
2953 	ha->mailbox_out[0] = mb0;
2954 	mboxes >>= 1;
2955 	wptr = (uint16_t __iomem *)&reg->mailbox1;
2956 
2957 	for (cnt = 1; cnt < ha->mbx_count; cnt++) {
2958 		if (mboxes & BIT_0)
2959 			ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
2960 
2961 		mboxes >>= 1;
2962 		wptr++;
2963 	}
2964 }
2965 
2966 static void
2967 qla24xx_abort_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
2968 	struct abort_entry_24xx *pkt)
2969 {
2970 	const char func[] = "ABT_IOCB";
2971 	srb_t *sp;
2972 	struct srb_iocb *abt;
2973 
2974 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2975 	if (!sp)
2976 		return;
2977 
2978 	abt = &sp->u.iocb_cmd;
2979 	abt->u.abt.comp_status = le16_to_cpu(pkt->nport_handle);
2980 	sp->done(sp, 0);
2981 }
2982 
2983 void qla24xx_nvme_ls4_iocb(struct scsi_qla_host *vha,
2984     struct pt_ls4_request *pkt, struct req_que *req)
2985 {
2986 	srb_t *sp;
2987 	const char func[] = "LS4_IOCB";
2988 	uint16_t comp_status;
2989 
2990 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2991 	if (!sp)
2992 		return;
2993 
2994 	comp_status = le16_to_cpu(pkt->status);
2995 	sp->done(sp, comp_status);
2996 }
2997 
2998 /**
2999  * qla24xx_process_response_queue() - Process response queue entries.
3000  * @vha: SCSI driver HA context
3001  * @rsp: response queue
3002  */
3003 void qla24xx_process_response_queue(struct scsi_qla_host *vha,
3004 	struct rsp_que *rsp)
3005 {
3006 	struct sts_entry_24xx *pkt;
3007 	struct qla_hw_data *ha = vha->hw;
3008 
3009 	if (!ha->flags.fw_started)
3010 		return;
3011 
3012 	if (rsp->qpair->cpuid != smp_processor_id())
3013 		qla_cpu_update(rsp->qpair, smp_processor_id());
3014 
3015 	while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
3016 		pkt = (struct sts_entry_24xx *)rsp->ring_ptr;
3017 
3018 		rsp->ring_index++;
3019 		if (rsp->ring_index == rsp->length) {
3020 			rsp->ring_index = 0;
3021 			rsp->ring_ptr = rsp->ring;
3022 		} else {
3023 			rsp->ring_ptr++;
3024 		}
3025 
3026 		if (pkt->entry_status != 0) {
3027 			if (qla2x00_error_entry(vha, rsp, (sts_entry_t *) pkt))
3028 				goto process_err;
3029 
3030 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
3031 			wmb();
3032 			continue;
3033 		}
3034 process_err:
3035 
3036 		switch (pkt->entry_type) {
3037 		case STATUS_TYPE:
3038 			qla2x00_status_entry(vha, rsp, pkt);
3039 			break;
3040 		case STATUS_CONT_TYPE:
3041 			qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
3042 			break;
3043 		case VP_RPT_ID_IOCB_TYPE:
3044 			qla24xx_report_id_acquisition(vha,
3045 			    (struct vp_rpt_id_entry_24xx *)pkt);
3046 			break;
3047 		case LOGINOUT_PORT_IOCB_TYPE:
3048 			qla24xx_logio_entry(vha, rsp->req,
3049 			    (struct logio_entry_24xx *)pkt);
3050 			break;
3051 		case CT_IOCB_TYPE:
3052 			qla24xx_els_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
3053 			break;
3054 		case ELS_IOCB_TYPE:
3055 			qla24xx_els_ct_entry(vha, rsp->req, pkt, ELS_IOCB_TYPE);
3056 			break;
3057 		case ABTS_RECV_24XX:
3058 			if (IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3059 			    IS_QLA28XX(ha)) {
3060 				/* ensure that the ATIO queue is empty */
3061 				qlt_handle_abts_recv(vha, rsp,
3062 				    (response_t *)pkt);
3063 				break;
3064 			} else {
3065 				qlt_24xx_process_atio_queue(vha, 1);
3066 			}
3067 			/* fall through */
3068 		case ABTS_RESP_24XX:
3069 		case CTIO_TYPE7:
3070 		case CTIO_CRC2:
3071 			qlt_response_pkt_all_vps(vha, rsp, (response_t *)pkt);
3072 			break;
3073 		case PT_LS4_REQUEST:
3074 			qla24xx_nvme_ls4_iocb(vha, (struct pt_ls4_request *)pkt,
3075 			    rsp->req);
3076 			break;
3077 		case NOTIFY_ACK_TYPE:
3078 			if (pkt->handle == QLA_TGT_SKIP_HANDLE)
3079 				qlt_response_pkt_all_vps(vha, rsp,
3080 				    (response_t *)pkt);
3081 			else
3082 				qla24xxx_nack_iocb_entry(vha, rsp->req,
3083 					(struct nack_to_isp *)pkt);
3084 			break;
3085 		case MARKER_TYPE:
3086 			/* Do nothing in this case, this check is to prevent it
3087 			 * from falling into default case
3088 			 */
3089 			break;
3090 		case ABORT_IOCB_TYPE:
3091 			qla24xx_abort_iocb_entry(vha, rsp->req,
3092 			    (struct abort_entry_24xx *)pkt);
3093 			break;
3094 		case MBX_IOCB_TYPE:
3095 			qla24xx_mbx_iocb_entry(vha, rsp->req,
3096 			    (struct mbx_24xx_entry *)pkt);
3097 			break;
3098 		case VP_CTRL_IOCB_TYPE:
3099 			qla_ctrlvp_completed(vha, rsp->req,
3100 			    (struct vp_ctrl_entry_24xx *)pkt);
3101 			break;
3102 		default:
3103 			/* Type Not Supported. */
3104 			ql_dbg(ql_dbg_async, vha, 0x5042,
3105 			    "Received unknown response pkt type %x "
3106 			    "entry status=%x.\n",
3107 			    pkt->entry_type, pkt->entry_status);
3108 			break;
3109 		}
3110 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
3111 		wmb();
3112 	}
3113 
3114 	/* Adjust ring index */
3115 	if (IS_P3P_TYPE(ha)) {
3116 		struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
3117 
3118 		WRT_REG_DWORD(&reg->rsp_q_out[0], rsp->ring_index);
3119 	} else {
3120 		WRT_REG_DWORD(rsp->rsp_q_out, rsp->ring_index);
3121 	}
3122 }
3123 
3124 static void
3125 qla2xxx_check_risc_status(scsi_qla_host_t *vha)
3126 {
3127 	int rval;
3128 	uint32_t cnt;
3129 	struct qla_hw_data *ha = vha->hw;
3130 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3131 
3132 	if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
3133 	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3134 		return;
3135 
3136 	rval = QLA_SUCCESS;
3137 	WRT_REG_DWORD(&reg->iobase_addr, 0x7C00);
3138 	RD_REG_DWORD(&reg->iobase_addr);
3139 	WRT_REG_DWORD(&reg->iobase_window, 0x0001);
3140 	for (cnt = 10000; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
3141 	    rval == QLA_SUCCESS; cnt--) {
3142 		if (cnt) {
3143 			WRT_REG_DWORD(&reg->iobase_window, 0x0001);
3144 			udelay(10);
3145 		} else
3146 			rval = QLA_FUNCTION_TIMEOUT;
3147 	}
3148 	if (rval == QLA_SUCCESS)
3149 		goto next_test;
3150 
3151 	rval = QLA_SUCCESS;
3152 	WRT_REG_DWORD(&reg->iobase_window, 0x0003);
3153 	for (cnt = 100; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
3154 	    rval == QLA_SUCCESS; cnt--) {
3155 		if (cnt) {
3156 			WRT_REG_DWORD(&reg->iobase_window, 0x0003);
3157 			udelay(10);
3158 		} else
3159 			rval = QLA_FUNCTION_TIMEOUT;
3160 	}
3161 	if (rval != QLA_SUCCESS)
3162 		goto done;
3163 
3164 next_test:
3165 	if (RD_REG_DWORD(&reg->iobase_c8) & BIT_3)
3166 		ql_log(ql_log_info, vha, 0x504c,
3167 		    "Additional code -- 0x55AA.\n");
3168 
3169 done:
3170 	WRT_REG_DWORD(&reg->iobase_window, 0x0000);
3171 	RD_REG_DWORD(&reg->iobase_window);
3172 }
3173 
3174 /**
3175  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP24xx.
3176  * @irq: interrupt number
3177  * @dev_id: SCSI driver HA context
3178  *
3179  * Called by system whenever the host adapter generates an interrupt.
3180  *
3181  * Returns handled flag.
3182  */
3183 irqreturn_t
3184 qla24xx_intr_handler(int irq, void *dev_id)
3185 {
3186 	scsi_qla_host_t	*vha;
3187 	struct qla_hw_data *ha;
3188 	struct device_reg_24xx __iomem *reg;
3189 	int		status;
3190 	unsigned long	iter;
3191 	uint32_t	stat;
3192 	uint32_t	hccr;
3193 	uint16_t	mb[8];
3194 	struct rsp_que *rsp;
3195 	unsigned long	flags;
3196 	bool process_atio = false;
3197 
3198 	rsp = (struct rsp_que *) dev_id;
3199 	if (!rsp) {
3200 		ql_log(ql_log_info, NULL, 0x5059,
3201 		    "%s: NULL response queue pointer.\n", __func__);
3202 		return IRQ_NONE;
3203 	}
3204 
3205 	ha = rsp->hw;
3206 	reg = &ha->iobase->isp24;
3207 	status = 0;
3208 
3209 	if (unlikely(pci_channel_offline(ha->pdev)))
3210 		return IRQ_HANDLED;
3211 
3212 	spin_lock_irqsave(&ha->hardware_lock, flags);
3213 	vha = pci_get_drvdata(ha->pdev);
3214 	for (iter = 50; iter--; ) {
3215 		stat = RD_REG_DWORD(&reg->host_status);
3216 		if (qla2x00_check_reg32_for_disconnect(vha, stat))
3217 			break;
3218 		if (stat & HSRX_RISC_PAUSED) {
3219 			if (unlikely(pci_channel_offline(ha->pdev)))
3220 				break;
3221 
3222 			hccr = RD_REG_DWORD(&reg->hccr);
3223 
3224 			ql_log(ql_log_warn, vha, 0x504b,
3225 			    "RISC paused -- HCCR=%x, Dumping firmware.\n",
3226 			    hccr);
3227 
3228 			qla2xxx_check_risc_status(vha);
3229 
3230 			ha->isp_ops->fw_dump(vha, 1);
3231 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3232 			break;
3233 		} else if ((stat & HSRX_RISC_INT) == 0)
3234 			break;
3235 
3236 		switch (stat & 0xff) {
3237 		case INTR_ROM_MB_SUCCESS:
3238 		case INTR_ROM_MB_FAILED:
3239 		case INTR_MB_SUCCESS:
3240 		case INTR_MB_FAILED:
3241 			qla24xx_mbx_completion(vha, MSW(stat));
3242 			status |= MBX_INTERRUPT;
3243 
3244 			break;
3245 		case INTR_ASYNC_EVENT:
3246 			mb[0] = MSW(stat);
3247 			mb[1] = RD_REG_WORD(&reg->mailbox1);
3248 			mb[2] = RD_REG_WORD(&reg->mailbox2);
3249 			mb[3] = RD_REG_WORD(&reg->mailbox3);
3250 			qla2x00_async_event(vha, rsp, mb);
3251 			break;
3252 		case INTR_RSP_QUE_UPDATE:
3253 		case INTR_RSP_QUE_UPDATE_83XX:
3254 			qla24xx_process_response_queue(vha, rsp);
3255 			break;
3256 		case INTR_ATIO_QUE_UPDATE_27XX:
3257 		case INTR_ATIO_QUE_UPDATE:
3258 			process_atio = true;
3259 			break;
3260 		case INTR_ATIO_RSP_QUE_UPDATE:
3261 			process_atio = true;
3262 			qla24xx_process_response_queue(vha, rsp);
3263 			break;
3264 		default:
3265 			ql_dbg(ql_dbg_async, vha, 0x504f,
3266 			    "Unrecognized interrupt type (%d).\n", stat * 0xff);
3267 			break;
3268 		}
3269 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
3270 		RD_REG_DWORD_RELAXED(&reg->hccr);
3271 		if (unlikely(IS_QLA83XX(ha) && (ha->pdev->revision == 1)))
3272 			ndelay(3500);
3273 	}
3274 	qla2x00_handle_mbx_completion(ha, status);
3275 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3276 
3277 	if (process_atio) {
3278 		spin_lock_irqsave(&ha->tgt.atio_lock, flags);
3279 		qlt_24xx_process_atio_queue(vha, 0);
3280 		spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
3281 	}
3282 
3283 	return IRQ_HANDLED;
3284 }
3285 
3286 static irqreturn_t
3287 qla24xx_msix_rsp_q(int irq, void *dev_id)
3288 {
3289 	struct qla_hw_data *ha;
3290 	struct rsp_que *rsp;
3291 	struct device_reg_24xx __iomem *reg;
3292 	struct scsi_qla_host *vha;
3293 	unsigned long flags;
3294 
3295 	rsp = (struct rsp_que *) dev_id;
3296 	if (!rsp) {
3297 		ql_log(ql_log_info, NULL, 0x505a,
3298 		    "%s: NULL response queue pointer.\n", __func__);
3299 		return IRQ_NONE;
3300 	}
3301 	ha = rsp->hw;
3302 	reg = &ha->iobase->isp24;
3303 
3304 	spin_lock_irqsave(&ha->hardware_lock, flags);
3305 
3306 	vha = pci_get_drvdata(ha->pdev);
3307 	qla24xx_process_response_queue(vha, rsp);
3308 	if (!ha->flags.disable_msix_handshake) {
3309 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
3310 		RD_REG_DWORD_RELAXED(&reg->hccr);
3311 	}
3312 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3313 
3314 	return IRQ_HANDLED;
3315 }
3316 
3317 static irqreturn_t
3318 qla24xx_msix_default(int irq, void *dev_id)
3319 {
3320 	scsi_qla_host_t	*vha;
3321 	struct qla_hw_data *ha;
3322 	struct rsp_que *rsp;
3323 	struct device_reg_24xx __iomem *reg;
3324 	int		status;
3325 	uint32_t	stat;
3326 	uint32_t	hccr;
3327 	uint16_t	mb[8];
3328 	unsigned long flags;
3329 	bool process_atio = false;
3330 
3331 	rsp = (struct rsp_que *) dev_id;
3332 	if (!rsp) {
3333 		ql_log(ql_log_info, NULL, 0x505c,
3334 		    "%s: NULL response queue pointer.\n", __func__);
3335 		return IRQ_NONE;
3336 	}
3337 	ha = rsp->hw;
3338 	reg = &ha->iobase->isp24;
3339 	status = 0;
3340 
3341 	spin_lock_irqsave(&ha->hardware_lock, flags);
3342 	vha = pci_get_drvdata(ha->pdev);
3343 	do {
3344 		stat = RD_REG_DWORD(&reg->host_status);
3345 		if (qla2x00_check_reg32_for_disconnect(vha, stat))
3346 			break;
3347 		if (stat & HSRX_RISC_PAUSED) {
3348 			if (unlikely(pci_channel_offline(ha->pdev)))
3349 				break;
3350 
3351 			hccr = RD_REG_DWORD(&reg->hccr);
3352 
3353 			ql_log(ql_log_info, vha, 0x5050,
3354 			    "RISC paused -- HCCR=%x, Dumping firmware.\n",
3355 			    hccr);
3356 
3357 			qla2xxx_check_risc_status(vha);
3358 
3359 			ha->isp_ops->fw_dump(vha, 1);
3360 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3361 			break;
3362 		} else if ((stat & HSRX_RISC_INT) == 0)
3363 			break;
3364 
3365 		switch (stat & 0xff) {
3366 		case INTR_ROM_MB_SUCCESS:
3367 		case INTR_ROM_MB_FAILED:
3368 		case INTR_MB_SUCCESS:
3369 		case INTR_MB_FAILED:
3370 			qla24xx_mbx_completion(vha, MSW(stat));
3371 			status |= MBX_INTERRUPT;
3372 
3373 			break;
3374 		case INTR_ASYNC_EVENT:
3375 			mb[0] = MSW(stat);
3376 			mb[1] = RD_REG_WORD(&reg->mailbox1);
3377 			mb[2] = RD_REG_WORD(&reg->mailbox2);
3378 			mb[3] = RD_REG_WORD(&reg->mailbox3);
3379 			qla2x00_async_event(vha, rsp, mb);
3380 			break;
3381 		case INTR_RSP_QUE_UPDATE:
3382 		case INTR_RSP_QUE_UPDATE_83XX:
3383 			qla24xx_process_response_queue(vha, rsp);
3384 			break;
3385 		case INTR_ATIO_QUE_UPDATE_27XX:
3386 		case INTR_ATIO_QUE_UPDATE:
3387 			process_atio = true;
3388 			break;
3389 		case INTR_ATIO_RSP_QUE_UPDATE:
3390 			process_atio = true;
3391 			qla24xx_process_response_queue(vha, rsp);
3392 			break;
3393 		default:
3394 			ql_dbg(ql_dbg_async, vha, 0x5051,
3395 			    "Unrecognized interrupt type (%d).\n", stat & 0xff);
3396 			break;
3397 		}
3398 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
3399 	} while (0);
3400 	qla2x00_handle_mbx_completion(ha, status);
3401 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3402 
3403 	if (process_atio) {
3404 		spin_lock_irqsave(&ha->tgt.atio_lock, flags);
3405 		qlt_24xx_process_atio_queue(vha, 0);
3406 		spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
3407 	}
3408 
3409 	return IRQ_HANDLED;
3410 }
3411 
3412 irqreturn_t
3413 qla2xxx_msix_rsp_q(int irq, void *dev_id)
3414 {
3415 	struct qla_hw_data *ha;
3416 	struct qla_qpair *qpair;
3417 	struct device_reg_24xx __iomem *reg;
3418 	unsigned long flags;
3419 
3420 	qpair = dev_id;
3421 	if (!qpair) {
3422 		ql_log(ql_log_info, NULL, 0x505b,
3423 		    "%s: NULL response queue pointer.\n", __func__);
3424 		return IRQ_NONE;
3425 	}
3426 	ha = qpair->hw;
3427 
3428 	/* Clear the interrupt, if enabled, for this response queue */
3429 	if (unlikely(!ha->flags.disable_msix_handshake)) {
3430 		reg = &ha->iobase->isp24;
3431 		spin_lock_irqsave(&ha->hardware_lock, flags);
3432 		WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
3433 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
3434 	}
3435 
3436 	queue_work(ha->wq, &qpair->q_work);
3437 
3438 	return IRQ_HANDLED;
3439 }
3440 
3441 /* Interrupt handling helpers. */
3442 
3443 struct qla_init_msix_entry {
3444 	const char *name;
3445 	irq_handler_t handler;
3446 };
3447 
3448 static const struct qla_init_msix_entry msix_entries[] = {
3449 	{ "default", qla24xx_msix_default },
3450 	{ "rsp_q", qla24xx_msix_rsp_q },
3451 	{ "atio_q", qla83xx_msix_atio_q },
3452 	{ "qpair_multiq", qla2xxx_msix_rsp_q },
3453 };
3454 
3455 static const struct qla_init_msix_entry qla82xx_msix_entries[] = {
3456 	{ "qla2xxx (default)", qla82xx_msix_default },
3457 	{ "qla2xxx (rsp_q)", qla82xx_msix_rsp_q },
3458 };
3459 
3460 static int
3461 qla24xx_enable_msix(struct qla_hw_data *ha, struct rsp_que *rsp)
3462 {
3463 	int i, ret;
3464 	struct qla_msix_entry *qentry;
3465 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
3466 	int min_vecs = QLA_BASE_VECTORS;
3467 	struct irq_affinity desc = {
3468 		.pre_vectors = QLA_BASE_VECTORS,
3469 	};
3470 
3471 	if (QLA_TGT_MODE_ENABLED() && (ql2xenablemsix != 0) &&
3472 	    IS_ATIO_MSIX_CAPABLE(ha)) {
3473 		desc.pre_vectors++;
3474 		min_vecs++;
3475 	}
3476 
3477 	if (USER_CTRL_IRQ(ha) || !ha->mqiobase) {
3478 		/* user wants to control IRQ setting for target mode */
3479 		ret = pci_alloc_irq_vectors(ha->pdev, min_vecs,
3480 		    ha->msix_count, PCI_IRQ_MSIX);
3481 	} else
3482 		ret = pci_alloc_irq_vectors_affinity(ha->pdev, min_vecs,
3483 		    ha->msix_count, PCI_IRQ_MSIX | PCI_IRQ_AFFINITY,
3484 		    &desc);
3485 
3486 	if (ret < 0) {
3487 		ql_log(ql_log_fatal, vha, 0x00c7,
3488 		    "MSI-X: Failed to enable support, "
3489 		    "giving   up -- %d/%d.\n",
3490 		    ha->msix_count, ret);
3491 		goto msix_out;
3492 	} else if (ret < ha->msix_count) {
3493 		ql_log(ql_log_info, vha, 0x00c6,
3494 		    "MSI-X: Using %d vectors\n", ret);
3495 		ha->msix_count = ret;
3496 		/* Recalculate queue values */
3497 		if (ha->mqiobase && (ql2xmqsupport || ql2xnvmeenable)) {
3498 			ha->max_req_queues = ha->msix_count - 1;
3499 
3500 			/* ATIOQ needs 1 vector. That's 1 less QPair */
3501 			if (QLA_TGT_MODE_ENABLED())
3502 				ha->max_req_queues--;
3503 
3504 			ha->max_rsp_queues = ha->max_req_queues;
3505 
3506 			ha->max_qpairs = ha->max_req_queues - 1;
3507 			ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0190,
3508 			    "Adjusted Max no of queues pairs: %d.\n", ha->max_qpairs);
3509 		}
3510 	}
3511 	vha->irq_offset = desc.pre_vectors;
3512 	ha->msix_entries = kcalloc(ha->msix_count,
3513 				   sizeof(struct qla_msix_entry),
3514 				   GFP_KERNEL);
3515 	if (!ha->msix_entries) {
3516 		ql_log(ql_log_fatal, vha, 0x00c8,
3517 		    "Failed to allocate memory for ha->msix_entries.\n");
3518 		ret = -ENOMEM;
3519 		goto free_irqs;
3520 	}
3521 	ha->flags.msix_enabled = 1;
3522 
3523 	for (i = 0; i < ha->msix_count; i++) {
3524 		qentry = &ha->msix_entries[i];
3525 		qentry->vector = pci_irq_vector(ha->pdev, i);
3526 		qentry->entry = i;
3527 		qentry->have_irq = 0;
3528 		qentry->in_use = 0;
3529 		qentry->handle = NULL;
3530 	}
3531 
3532 	/* Enable MSI-X vectors for the base queue */
3533 	for (i = 0; i < QLA_BASE_VECTORS; i++) {
3534 		qentry = &ha->msix_entries[i];
3535 		qentry->handle = rsp;
3536 		rsp->msix = qentry;
3537 		scnprintf(qentry->name, sizeof(qentry->name),
3538 		    "qla2xxx%lu_%s", vha->host_no, msix_entries[i].name);
3539 		if (IS_P3P_TYPE(ha))
3540 			ret = request_irq(qentry->vector,
3541 				qla82xx_msix_entries[i].handler,
3542 				0, qla82xx_msix_entries[i].name, rsp);
3543 		else
3544 			ret = request_irq(qentry->vector,
3545 				msix_entries[i].handler,
3546 				0, qentry->name, rsp);
3547 		if (ret)
3548 			goto msix_register_fail;
3549 		qentry->have_irq = 1;
3550 		qentry->in_use = 1;
3551 	}
3552 
3553 	/*
3554 	 * If target mode is enable, also request the vector for the ATIO
3555 	 * queue.
3556 	 */
3557 	if (QLA_TGT_MODE_ENABLED() && (ql2xenablemsix != 0) &&
3558 	    IS_ATIO_MSIX_CAPABLE(ha)) {
3559 		qentry = &ha->msix_entries[QLA_ATIO_VECTOR];
3560 		rsp->msix = qentry;
3561 		qentry->handle = rsp;
3562 		scnprintf(qentry->name, sizeof(qentry->name),
3563 		    "qla2xxx%lu_%s", vha->host_no,
3564 		    msix_entries[QLA_ATIO_VECTOR].name);
3565 		qentry->in_use = 1;
3566 		ret = request_irq(qentry->vector,
3567 			msix_entries[QLA_ATIO_VECTOR].handler,
3568 			0, qentry->name, rsp);
3569 		qentry->have_irq = 1;
3570 	}
3571 
3572 msix_register_fail:
3573 	if (ret) {
3574 		ql_log(ql_log_fatal, vha, 0x00cb,
3575 		    "MSI-X: unable to register handler -- %x/%d.\n",
3576 		    qentry->vector, ret);
3577 		qla2x00_free_irqs(vha);
3578 		ha->mqenable = 0;
3579 		goto msix_out;
3580 	}
3581 
3582 	/* Enable MSI-X vector for response queue update for queue 0 */
3583 	if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
3584 		if (ha->msixbase && ha->mqiobase &&
3585 		    (ha->max_rsp_queues > 1 || ha->max_req_queues > 1 ||
3586 		     ql2xmqsupport))
3587 			ha->mqenable = 1;
3588 	} else
3589 		if (ha->mqiobase &&
3590 		    (ha->max_rsp_queues > 1 || ha->max_req_queues > 1 ||
3591 		     ql2xmqsupport))
3592 			ha->mqenable = 1;
3593 	ql_dbg(ql_dbg_multiq, vha, 0xc005,
3594 	    "mqiobase=%p, max_rsp_queues=%d, max_req_queues=%d.\n",
3595 	    ha->mqiobase, ha->max_rsp_queues, ha->max_req_queues);
3596 	ql_dbg(ql_dbg_init, vha, 0x0055,
3597 	    "mqiobase=%p, max_rsp_queues=%d, max_req_queues=%d.\n",
3598 	    ha->mqiobase, ha->max_rsp_queues, ha->max_req_queues);
3599 
3600 msix_out:
3601 	return ret;
3602 
3603 free_irqs:
3604 	pci_free_irq_vectors(ha->pdev);
3605 	goto msix_out;
3606 }
3607 
3608 int
3609 qla2x00_request_irqs(struct qla_hw_data *ha, struct rsp_que *rsp)
3610 {
3611 	int ret = QLA_FUNCTION_FAILED;
3612 	device_reg_t *reg = ha->iobase;
3613 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
3614 
3615 	/* If possible, enable MSI-X. */
3616 	if (ql2xenablemsix == 0 || (!IS_QLA2432(ha) && !IS_QLA2532(ha) &&
3617 	    !IS_QLA8432(ha) && !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha) &&
3618 	    !IS_QLAFX00(ha) && !IS_QLA27XX(ha) && !IS_QLA28XX(ha)))
3619 		goto skip_msi;
3620 
3621 	if (ql2xenablemsix == 2)
3622 		goto skip_msix;
3623 
3624 	if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_HP &&
3625 		(ha->pdev->subsystem_device == 0x7040 ||
3626 		ha->pdev->subsystem_device == 0x7041 ||
3627 		ha->pdev->subsystem_device == 0x1705)) {
3628 		ql_log(ql_log_warn, vha, 0x0034,
3629 		    "MSI-X: Unsupported ISP 2432 SSVID/SSDID (0x%X,0x%X).\n",
3630 			ha->pdev->subsystem_vendor,
3631 			ha->pdev->subsystem_device);
3632 		goto skip_msi;
3633 	}
3634 
3635 	if (IS_QLA2432(ha) && (ha->pdev->revision < QLA_MSIX_CHIP_REV_24XX)) {
3636 		ql_log(ql_log_warn, vha, 0x0035,
3637 		    "MSI-X; Unsupported ISP2432 (0x%X, 0x%X).\n",
3638 		    ha->pdev->revision, QLA_MSIX_CHIP_REV_24XX);
3639 		goto skip_msix;
3640 	}
3641 
3642 	ret = qla24xx_enable_msix(ha, rsp);
3643 	if (!ret) {
3644 		ql_dbg(ql_dbg_init, vha, 0x0036,
3645 		    "MSI-X: Enabled (0x%X, 0x%X).\n",
3646 		    ha->chip_revision, ha->fw_attributes);
3647 		goto clear_risc_ints;
3648 	}
3649 
3650 skip_msix:
3651 
3652 	ql_log(ql_log_info, vha, 0x0037,
3653 	    "Falling back-to MSI mode -- ret=%d.\n", ret);
3654 
3655 	if (!IS_QLA24XX(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha) &&
3656 	    !IS_QLA8001(ha) && !IS_P3P_TYPE(ha) && !IS_QLAFX00(ha) &&
3657 	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3658 		goto skip_msi;
3659 
3660 	ret = pci_alloc_irq_vectors(ha->pdev, 1, 1, PCI_IRQ_MSI);
3661 	if (ret > 0) {
3662 		ql_dbg(ql_dbg_init, vha, 0x0038,
3663 		    "MSI: Enabled.\n");
3664 		ha->flags.msi_enabled = 1;
3665 	} else
3666 		ql_log(ql_log_warn, vha, 0x0039,
3667 		    "Falling back-to INTa mode -- ret=%d.\n", ret);
3668 skip_msi:
3669 
3670 	/* Skip INTx on ISP82xx. */
3671 	if (!ha->flags.msi_enabled && IS_QLA82XX(ha))
3672 		return QLA_FUNCTION_FAILED;
3673 
3674 	ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
3675 	    ha->flags.msi_enabled ? 0 : IRQF_SHARED,
3676 	    QLA2XXX_DRIVER_NAME, rsp);
3677 	if (ret) {
3678 		ql_log(ql_log_warn, vha, 0x003a,
3679 		    "Failed to reserve interrupt %d already in use.\n",
3680 		    ha->pdev->irq);
3681 		goto fail;
3682 	} else if (!ha->flags.msi_enabled) {
3683 		ql_dbg(ql_dbg_init, vha, 0x0125,
3684 		    "INTa mode: Enabled.\n");
3685 		ha->flags.mr_intr_valid = 1;
3686 	}
3687 
3688 clear_risc_ints:
3689 	if (IS_FWI2_CAPABLE(ha) || IS_QLAFX00(ha))
3690 		goto fail;
3691 
3692 	spin_lock_irq(&ha->hardware_lock);
3693 	WRT_REG_WORD(&reg->isp.semaphore, 0);
3694 	spin_unlock_irq(&ha->hardware_lock);
3695 
3696 fail:
3697 	return ret;
3698 }
3699 
3700 void
3701 qla2x00_free_irqs(scsi_qla_host_t *vha)
3702 {
3703 	struct qla_hw_data *ha = vha->hw;
3704 	struct rsp_que *rsp;
3705 	struct qla_msix_entry *qentry;
3706 	int i;
3707 
3708 	/*
3709 	 * We need to check that ha->rsp_q_map is valid in case we are called
3710 	 * from a probe failure context.
3711 	 */
3712 	if (!ha->rsp_q_map || !ha->rsp_q_map[0])
3713 		goto free_irqs;
3714 	rsp = ha->rsp_q_map[0];
3715 
3716 	if (ha->flags.msix_enabled) {
3717 		for (i = 0; i < ha->msix_count; i++) {
3718 			qentry = &ha->msix_entries[i];
3719 			if (qentry->have_irq) {
3720 				irq_set_affinity_notifier(qentry->vector, NULL);
3721 				free_irq(pci_irq_vector(ha->pdev, i), qentry->handle);
3722 			}
3723 		}
3724 		kfree(ha->msix_entries);
3725 		ha->msix_entries = NULL;
3726 		ha->flags.msix_enabled = 0;
3727 		ql_dbg(ql_dbg_init, vha, 0x0042,
3728 			"Disabled MSI-X.\n");
3729 	} else {
3730 		free_irq(pci_irq_vector(ha->pdev, 0), rsp);
3731 	}
3732 
3733 free_irqs:
3734 	pci_free_irq_vectors(ha->pdev);
3735 }
3736 
3737 int qla25xx_request_irq(struct qla_hw_data *ha, struct qla_qpair *qpair,
3738 	struct qla_msix_entry *msix, int vector_type)
3739 {
3740 	const struct qla_init_msix_entry *intr = &msix_entries[vector_type];
3741 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
3742 	int ret;
3743 
3744 	scnprintf(msix->name, sizeof(msix->name),
3745 	    "qla2xxx%lu_qpair%d", vha->host_no, qpair->id);
3746 	ret = request_irq(msix->vector, intr->handler, 0, msix->name, qpair);
3747 	if (ret) {
3748 		ql_log(ql_log_fatal, vha, 0x00e6,
3749 		    "MSI-X: Unable to register handler -- %x/%d.\n",
3750 		    msix->vector, ret);
3751 		return ret;
3752 	}
3753 	msix->have_irq = 1;
3754 	msix->handle = qpair;
3755 	return ret;
3756 }
3757