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