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