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