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