xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_isr.c (revision 2be6bc48df59c99d35aab16a51d4a814e9bb8c35)
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, 0);
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 		ql_log(ql_log_warn, vha, 0x50ff,
1126 		    "Dropping AEN - %04x %04x %04x %04x.\n",
1127 		    mb[0], mb[1], mb[2], mb[3]);
1128 		return;
1129 	}
1130 
1131 	/* Setup to process RIO completion. */
1132 	handle_cnt = 0;
1133 	if (IS_CNA_CAPABLE(ha))
1134 		goto skip_rio;
1135 	switch (mb[0]) {
1136 	case MBA_SCSI_COMPLETION:
1137 		handles[0] = make_handle(mb[2], mb[1]);
1138 		handle_cnt = 1;
1139 		break;
1140 	case MBA_CMPLT_1_16BIT:
1141 		handles[0] = mb[1];
1142 		handle_cnt = 1;
1143 		mb[0] = MBA_SCSI_COMPLETION;
1144 		break;
1145 	case MBA_CMPLT_2_16BIT:
1146 		handles[0] = mb[1];
1147 		handles[1] = mb[2];
1148 		handle_cnt = 2;
1149 		mb[0] = MBA_SCSI_COMPLETION;
1150 		break;
1151 	case MBA_CMPLT_3_16BIT:
1152 		handles[0] = mb[1];
1153 		handles[1] = mb[2];
1154 		handles[2] = mb[3];
1155 		handle_cnt = 3;
1156 		mb[0] = MBA_SCSI_COMPLETION;
1157 		break;
1158 	case MBA_CMPLT_4_16BIT:
1159 		handles[0] = mb[1];
1160 		handles[1] = mb[2];
1161 		handles[2] = mb[3];
1162 		handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
1163 		handle_cnt = 4;
1164 		mb[0] = MBA_SCSI_COMPLETION;
1165 		break;
1166 	case MBA_CMPLT_5_16BIT:
1167 		handles[0] = mb[1];
1168 		handles[1] = mb[2];
1169 		handles[2] = mb[3];
1170 		handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
1171 		handles[4] = (uint32_t)RD_MAILBOX_REG(ha, reg, 7);
1172 		handle_cnt = 5;
1173 		mb[0] = MBA_SCSI_COMPLETION;
1174 		break;
1175 	case MBA_CMPLT_2_32BIT:
1176 		handles[0] = make_handle(mb[2], mb[1]);
1177 		handles[1] = make_handle(RD_MAILBOX_REG(ha, reg, 7),
1178 					 RD_MAILBOX_REG(ha, reg, 6));
1179 		handle_cnt = 2;
1180 		mb[0] = MBA_SCSI_COMPLETION;
1181 		break;
1182 	default:
1183 		break;
1184 	}
1185 skip_rio:
1186 	switch (mb[0]) {
1187 	case MBA_SCSI_COMPLETION:	/* Fast Post */
1188 		if (!vha->flags.online)
1189 			break;
1190 
1191 		for (cnt = 0; cnt < handle_cnt; cnt++)
1192 			qla2x00_process_completed_request(vha, rsp->req,
1193 				handles[cnt]);
1194 		break;
1195 
1196 	case MBA_RESET:			/* Reset */
1197 		ql_dbg(ql_dbg_async, vha, 0x5002,
1198 		    "Asynchronous RESET.\n");
1199 
1200 		set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
1201 		break;
1202 
1203 	case MBA_SYSTEM_ERR:		/* System Error */
1204 		mbx = 0;
1205 
1206 		vha->hw_err_cnt++;
1207 
1208 		if (IS_QLA81XX(ha) || IS_QLA83XX(ha) ||
1209 		    IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
1210 			u16 m[4];
1211 
1212 			m[0] = rd_reg_word(&reg24->mailbox4);
1213 			m[1] = rd_reg_word(&reg24->mailbox5);
1214 			m[2] = rd_reg_word(&reg24->mailbox6);
1215 			mbx = m[3] = rd_reg_word(&reg24->mailbox7);
1216 
1217 			ql_log(ql_log_warn, vha, 0x5003,
1218 			    "ISP System Error - mbx1=%xh mbx2=%xh mbx3=%xh mbx4=%xh mbx5=%xh mbx6=%xh mbx7=%xh.\n",
1219 			    mb[1], mb[2], mb[3], m[0], m[1], m[2], m[3]);
1220 		} else
1221 			ql_log(ql_log_warn, vha, 0x5003,
1222 			    "ISP System Error - mbx1=%xh mbx2=%xh mbx3=%xh.\n ",
1223 			    mb[1], mb[2], mb[3]);
1224 
1225 		if ((IS_QLA27XX(ha) || IS_QLA28XX(ha)) &&
1226 		    rd_reg_word(&reg24->mailbox7) & BIT_8)
1227 			ha->isp_ops->mpi_fw_dump(vha, 1);
1228 		ha->isp_ops->fw_dump(vha);
1229 		ha->flags.fw_init_done = 0;
1230 		QLA_FW_STOPPED(ha);
1231 
1232 		if (IS_FWI2_CAPABLE(ha)) {
1233 			if (mb[1] == 0 && mb[2] == 0) {
1234 				ql_log(ql_log_fatal, vha, 0x5004,
1235 				    "Unrecoverable Hardware Error: adapter "
1236 				    "marked OFFLINE!\n");
1237 				vha->flags.online = 0;
1238 				vha->device_flags |= DFLG_DEV_FAILED;
1239 			} else {
1240 				/* Check to see if MPI timeout occurred */
1241 				if ((mbx & MBX_3) && (ha->port_no == 0))
1242 					set_bit(MPI_RESET_NEEDED,
1243 					    &vha->dpc_flags);
1244 
1245 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1246 			}
1247 		} else if (mb[1] == 0) {
1248 			ql_log(ql_log_fatal, vha, 0x5005,
1249 			    "Unrecoverable Hardware Error: adapter marked "
1250 			    "OFFLINE!\n");
1251 			vha->flags.online = 0;
1252 			vha->device_flags |= DFLG_DEV_FAILED;
1253 		} else
1254 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1255 		break;
1256 
1257 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
1258 		ql_log(ql_log_warn, vha, 0x5006,
1259 		    "ISP Request Transfer Error (%x).\n",  mb[1]);
1260 
1261 		vha->hw_err_cnt++;
1262 
1263 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1264 		break;
1265 
1266 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
1267 		ql_log(ql_log_warn, vha, 0x5007,
1268 		    "ISP Response Transfer Error (%x).\n", mb[1]);
1269 
1270 		vha->hw_err_cnt++;
1271 
1272 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1273 		break;
1274 
1275 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up */
1276 		ql_dbg(ql_dbg_async, vha, 0x5008,
1277 		    "Asynchronous WAKEUP_THRES (%x).\n", mb[1]);
1278 		break;
1279 
1280 	case MBA_LOOP_INIT_ERR:
1281 		ql_log(ql_log_warn, vha, 0x5090,
1282 		    "LOOP INIT ERROR (%x).\n", mb[1]);
1283 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1284 		break;
1285 
1286 	case MBA_LIP_OCCURRED:		/* Loop Initialization Procedure */
1287 		ha->flags.lip_ae = 1;
1288 
1289 		ql_dbg(ql_dbg_async, vha, 0x5009,
1290 		    "LIP occurred (%x).\n", mb[1]);
1291 
1292 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
1293 			atomic_set(&vha->loop_state, LOOP_DOWN);
1294 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1295 			qla2x00_mark_all_devices_lost(vha);
1296 		}
1297 
1298 		if (vha->vp_idx) {
1299 			atomic_set(&vha->vp_state, VP_FAILED);
1300 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
1301 		}
1302 
1303 		set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
1304 		set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
1305 
1306 		vha->flags.management_server_logged_in = 0;
1307 		qla2x00_post_aen_work(vha, FCH_EVT_LIP, mb[1]);
1308 		break;
1309 
1310 	case MBA_LOOP_UP:		/* Loop Up Event */
1311 		if (IS_QLA2100(ha) || IS_QLA2200(ha))
1312 			ha->link_data_rate = PORT_SPEED_1GB;
1313 		else
1314 			ha->link_data_rate = mb[1];
1315 
1316 		ql_log(ql_log_info, vha, 0x500a,
1317 		    "LOOP UP detected (%s Gbps).\n",
1318 		    qla2x00_get_link_speed_str(ha, ha->link_data_rate));
1319 
1320 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
1321 			if (mb[2] & BIT_0)
1322 				ql_log(ql_log_info, vha, 0x11a0,
1323 				    "FEC=enabled (link up).\n");
1324 		}
1325 
1326 		vha->flags.management_server_logged_in = 0;
1327 		qla2x00_post_aen_work(vha, FCH_EVT_LINKUP, ha->link_data_rate);
1328 
1329 		if (vha->link_down_time < vha->hw->port_down_retry_count) {
1330 			vha->short_link_down_cnt++;
1331 			vha->link_down_time = QLA2XX_MAX_LINK_DOWN_TIME;
1332 		}
1333 
1334 		break;
1335 
1336 	case MBA_LOOP_DOWN:		/* Loop Down Event */
1337 		SAVE_TOPO(ha);
1338 		ha->flags.lip_ae = 0;
1339 		ha->current_topology = 0;
1340 		vha->link_down_time = 0;
1341 
1342 		mbx = (IS_QLA81XX(ha) || IS_QLA8031(ha))
1343 			? rd_reg_word(&reg24->mailbox4) : 0;
1344 		mbx = (IS_P3P_TYPE(ha)) ? rd_reg_word(&reg82->mailbox_out[4])
1345 			: mbx;
1346 		ql_log(ql_log_info, vha, 0x500b,
1347 		    "LOOP DOWN detected (%x %x %x %x).\n",
1348 		    mb[1], mb[2], mb[3], mbx);
1349 
1350 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
1351 			atomic_set(&vha->loop_state, LOOP_DOWN);
1352 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1353 			/*
1354 			 * In case of loop down, restore WWPN from
1355 			 * NVRAM in case of FA-WWPN capable ISP
1356 			 * Restore for Physical Port only
1357 			 */
1358 			if (!vha->vp_idx) {
1359 				if (ha->flags.fawwpn_enabled &&
1360 				    (ha->current_topology == ISP_CFG_F)) {
1361 					memcpy(vha->port_name, ha->port_name, WWN_SIZE);
1362 					fc_host_port_name(vha->host) =
1363 					    wwn_to_u64(vha->port_name);
1364 					ql_dbg(ql_dbg_init + ql_dbg_verbose,
1365 					    vha, 0x00d8, "LOOP DOWN detected,"
1366 					    "restore WWPN %016llx\n",
1367 					    wwn_to_u64(vha->port_name));
1368 				}
1369 
1370 				clear_bit(VP_CONFIG_OK, &vha->vp_flags);
1371 			}
1372 
1373 			vha->device_flags |= DFLG_NO_CABLE;
1374 			qla2x00_mark_all_devices_lost(vha);
1375 		}
1376 
1377 		if (vha->vp_idx) {
1378 			atomic_set(&vha->vp_state, VP_FAILED);
1379 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
1380 		}
1381 
1382 		vha->flags.management_server_logged_in = 0;
1383 		ha->link_data_rate = PORT_SPEED_UNKNOWN;
1384 		qla2x00_post_aen_work(vha, FCH_EVT_LINKDOWN, 0);
1385 		break;
1386 
1387 	case MBA_LIP_RESET:		/* LIP reset occurred */
1388 		ql_dbg(ql_dbg_async, vha, 0x500c,
1389 		    "LIP reset occurred (%x).\n", mb[1]);
1390 
1391 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
1392 			atomic_set(&vha->loop_state, LOOP_DOWN);
1393 			atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1394 			qla2x00_mark_all_devices_lost(vha);
1395 		}
1396 
1397 		if (vha->vp_idx) {
1398 			atomic_set(&vha->vp_state, VP_FAILED);
1399 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
1400 		}
1401 
1402 		set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
1403 
1404 		ha->operating_mode = LOOP;
1405 		vha->flags.management_server_logged_in = 0;
1406 		qla2x00_post_aen_work(vha, FCH_EVT_LIPRESET, mb[1]);
1407 		break;
1408 
1409 	/* case MBA_DCBX_COMPLETE: */
1410 	case MBA_POINT_TO_POINT:	/* Point-to-Point */
1411 		ha->flags.lip_ae = 0;
1412 
1413 		if (IS_QLA2100(ha))
1414 			break;
1415 
1416 		if (IS_CNA_CAPABLE(ha)) {
1417 			ql_dbg(ql_dbg_async, vha, 0x500d,
1418 			    "DCBX Completed -- %04x %04x %04x.\n",
1419 			    mb[1], mb[2], mb[3]);
1420 			if (ha->notify_dcbx_comp && !vha->vp_idx)
1421 				complete(&ha->dcbx_comp);
1422 
1423 		} else
1424 			ql_dbg(ql_dbg_async, vha, 0x500e,
1425 			    "Asynchronous P2P MODE received.\n");
1426 
1427 		/*
1428 		 * Until there's a transition from loop down to loop up, treat
1429 		 * this as loop down only.
1430 		 */
1431 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
1432 			atomic_set(&vha->loop_state, LOOP_DOWN);
1433 			if (!atomic_read(&vha->loop_down_timer))
1434 				atomic_set(&vha->loop_down_timer,
1435 				    LOOP_DOWN_TIME);
1436 			if (!N2N_TOPO(ha))
1437 				qla2x00_mark_all_devices_lost(vha);
1438 		}
1439 
1440 		if (vha->vp_idx) {
1441 			atomic_set(&vha->vp_state, VP_FAILED);
1442 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
1443 		}
1444 
1445 		if (!(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)))
1446 			set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
1447 
1448 		set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
1449 		set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
1450 
1451 		vha->flags.management_server_logged_in = 0;
1452 		break;
1453 
1454 	case MBA_CHG_IN_CONNECTION:	/* Change in connection mode */
1455 		if (IS_QLA2100(ha))
1456 			break;
1457 
1458 		ql_dbg(ql_dbg_async, vha, 0x500f,
1459 		    "Configuration change detected: value=%x.\n", mb[1]);
1460 
1461 		if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
1462 			atomic_set(&vha->loop_state, LOOP_DOWN);
1463 			if (!atomic_read(&vha->loop_down_timer))
1464 				atomic_set(&vha->loop_down_timer,
1465 				    LOOP_DOWN_TIME);
1466 			qla2x00_mark_all_devices_lost(vha);
1467 		}
1468 
1469 		if (vha->vp_idx) {
1470 			atomic_set(&vha->vp_state, VP_FAILED);
1471 			fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
1472 		}
1473 
1474 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1475 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1476 		break;
1477 
1478 	case MBA_PORT_UPDATE:		/* Port database update */
1479 		/*
1480 		 * Handle only global and vn-port update events
1481 		 *
1482 		 * Relevant inputs:
1483 		 * mb[1] = N_Port handle of changed port
1484 		 * OR 0xffff for global event
1485 		 * mb[2] = New login state
1486 		 * 7 = Port logged out
1487 		 * mb[3] = LSB is vp_idx, 0xff = all vps
1488 		 *
1489 		 * Skip processing if:
1490 		 *       Event is global, vp_idx is NOT all vps,
1491 		 *           vp_idx does not match
1492 		 *       Event is not global, vp_idx does not match
1493 		 */
1494 		if (IS_QLA2XXX_MIDTYPE(ha) &&
1495 		    ((mb[1] == 0xffff && (mb[3] & 0xff) != 0xff) ||
1496 			(mb[1] != 0xffff)) && vha->vp_idx != (mb[3] & 0xff))
1497 			break;
1498 
1499 		if (mb[2] == 0x7) {
1500 			ql_dbg(ql_dbg_async, vha, 0x5010,
1501 			    "Port %s %04x %04x %04x.\n",
1502 			    mb[1] == 0xffff ? "unavailable" : "logout",
1503 			    mb[1], mb[2], mb[3]);
1504 
1505 			if (mb[1] == 0xffff)
1506 				goto global_port_update;
1507 
1508 			if (mb[1] == NPH_SNS_LID(ha)) {
1509 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1510 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1511 				break;
1512 			}
1513 
1514 			/* use handle_cnt for loop id/nport handle */
1515 			if (IS_FWI2_CAPABLE(ha))
1516 				handle_cnt = NPH_SNS;
1517 			else
1518 				handle_cnt = SIMPLE_NAME_SERVER;
1519 			if (mb[1] == handle_cnt) {
1520 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1521 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1522 				break;
1523 			}
1524 
1525 			/* Port logout */
1526 			fcport = qla2x00_find_fcport_by_loopid(vha, mb[1]);
1527 			if (!fcport)
1528 				break;
1529 			if (atomic_read(&fcport->state) != FCS_ONLINE)
1530 				break;
1531 			ql_dbg(ql_dbg_async, vha, 0x508a,
1532 			    "Marking port lost loopid=%04x portid=%06x.\n",
1533 			    fcport->loop_id, fcport->d_id.b24);
1534 			if (qla_ini_mode_enabled(vha)) {
1535 				fcport->logout_on_delete = 0;
1536 				qlt_schedule_sess_for_deletion(fcport);
1537 			}
1538 			break;
1539 
1540 global_port_update:
1541 			if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
1542 				atomic_set(&vha->loop_state, LOOP_DOWN);
1543 				atomic_set(&vha->loop_down_timer,
1544 				    LOOP_DOWN_TIME);
1545 				vha->device_flags |= DFLG_NO_CABLE;
1546 				qla2x00_mark_all_devices_lost(vha);
1547 			}
1548 
1549 			if (vha->vp_idx) {
1550 				atomic_set(&vha->vp_state, VP_FAILED);
1551 				fc_vport_set_state(vha->fc_vport,
1552 				    FC_VPORT_FAILED);
1553 				qla2x00_mark_all_devices_lost(vha);
1554 			}
1555 
1556 			vha->flags.management_server_logged_in = 0;
1557 			ha->link_data_rate = PORT_SPEED_UNKNOWN;
1558 			break;
1559 		}
1560 
1561 		/*
1562 		 * If PORT UPDATE is global (received LIP_OCCURRED/LIP_RESET
1563 		 * event etc. earlier indicating loop is down) then process
1564 		 * it.  Otherwise ignore it and Wait for RSCN to come in.
1565 		 */
1566 		atomic_set(&vha->loop_down_timer, 0);
1567 		if (atomic_read(&vha->loop_state) != LOOP_DOWN &&
1568 			!ha->flags.n2n_ae  &&
1569 		    atomic_read(&vha->loop_state) != LOOP_DEAD) {
1570 			ql_dbg(ql_dbg_async, vha, 0x5011,
1571 			    "Asynchronous PORT UPDATE ignored %04x/%04x/%04x.\n",
1572 			    mb[1], mb[2], mb[3]);
1573 			break;
1574 		}
1575 
1576 		ql_dbg(ql_dbg_async, vha, 0x5012,
1577 		    "Port database changed %04x %04x %04x.\n",
1578 		    mb[1], mb[2], mb[3]);
1579 
1580 		/*
1581 		 * Mark all devices as missing so we will login again.
1582 		 */
1583 		atomic_set(&vha->loop_state, LOOP_UP);
1584 		vha->scan.scan_retry = 0;
1585 
1586 		set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1587 		set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1588 		set_bit(VP_CONFIG_OK, &vha->vp_flags);
1589 		break;
1590 
1591 	case MBA_RSCN_UPDATE:		/* State Change Registration */
1592 		/* Check if the Vport has issued a SCR */
1593 		if (vha->vp_idx && test_bit(VP_SCR_NEEDED, &vha->vp_flags))
1594 			break;
1595 		/* Only handle SCNs for our Vport index. */
1596 		if (ha->flags.npiv_supported && vha->vp_idx != (mb[3] & 0xff))
1597 			break;
1598 
1599 		ql_log(ql_log_warn, vha, 0x5013,
1600 		       "RSCN database changed -- %04x %04x %04x.\n",
1601 		       mb[1], mb[2], mb[3]);
1602 
1603 		rscn_entry = ((mb[1] & 0xff) << 16) | mb[2];
1604 		host_pid = (vha->d_id.b.domain << 16) | (vha->d_id.b.area << 8)
1605 				| vha->d_id.b.al_pa;
1606 		if (rscn_entry == host_pid) {
1607 			ql_dbg(ql_dbg_async, vha, 0x5014,
1608 			    "Ignoring RSCN update to local host "
1609 			    "port ID (%06x).\n", host_pid);
1610 			break;
1611 		}
1612 
1613 		/* Ignore reserved bits from RSCN-payload. */
1614 		rscn_entry = ((mb[1] & 0x3ff) << 16) | mb[2];
1615 
1616 		/* Skip RSCNs for virtual ports on the same physical port */
1617 		if (qla2x00_is_a_vp_did(vha, rscn_entry))
1618 			break;
1619 
1620 		atomic_set(&vha->loop_down_timer, 0);
1621 		vha->flags.management_server_logged_in = 0;
1622 		{
1623 			struct event_arg ea;
1624 
1625 			memset(&ea, 0, sizeof(ea));
1626 			ea.id.b24 = rscn_entry;
1627 			ea.id.b.rsvd_1 = rscn_entry >> 24;
1628 			qla2x00_handle_rscn(vha, &ea);
1629 			qla2x00_post_aen_work(vha, FCH_EVT_RSCN, rscn_entry);
1630 		}
1631 		break;
1632 	case MBA_CONGN_NOTI_RECV:
1633 		if (!ha->flags.scm_enabled ||
1634 		    mb[1] != QLA_CON_PRIMITIVE_RECEIVED)
1635 			break;
1636 
1637 		if (mb[2] == QLA_CONGESTION_ARB_WARNING) {
1638 			ql_dbg(ql_dbg_async, vha, 0x509b,
1639 			       "Congestion Warning %04x %04x.\n", mb[1], mb[2]);
1640 		} else if (mb[2] == QLA_CONGESTION_ARB_ALARM) {
1641 			ql_log(ql_log_warn, vha, 0x509b,
1642 			       "Congestion Alarm %04x %04x.\n", mb[1], mb[2]);
1643 		}
1644 		break;
1645 	/* case MBA_RIO_RESPONSE: */
1646 	case MBA_ZIO_RESPONSE:
1647 		ql_dbg(ql_dbg_async, vha, 0x5015,
1648 		    "[R|Z]IO update completion.\n");
1649 
1650 		if (IS_FWI2_CAPABLE(ha))
1651 			qla24xx_process_response_queue(vha, rsp);
1652 		else
1653 			qla2x00_process_response_queue(rsp);
1654 		break;
1655 
1656 	case MBA_DISCARD_RND_FRAME:
1657 		ql_dbg(ql_dbg_async, vha, 0x5016,
1658 		    "Discard RND Frame -- %04x %04x %04x.\n",
1659 		    mb[1], mb[2], mb[3]);
1660 		vha->interface_err_cnt++;
1661 		break;
1662 
1663 	case MBA_TRACE_NOTIFICATION:
1664 		ql_dbg(ql_dbg_async, vha, 0x5017,
1665 		    "Trace Notification -- %04x %04x.\n", mb[1], mb[2]);
1666 		break;
1667 
1668 	case MBA_ISP84XX_ALERT:
1669 		ql_dbg(ql_dbg_async, vha, 0x5018,
1670 		    "ISP84XX Alert Notification -- %04x %04x %04x.\n",
1671 		    mb[1], mb[2], mb[3]);
1672 
1673 		spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
1674 		switch (mb[1]) {
1675 		case A84_PANIC_RECOVERY:
1676 			ql_log(ql_log_info, vha, 0x5019,
1677 			    "Alert 84XX: panic recovery %04x %04x.\n",
1678 			    mb[2], mb[3]);
1679 			break;
1680 		case A84_OP_LOGIN_COMPLETE:
1681 			ha->cs84xx->op_fw_version = mb[3] << 16 | mb[2];
1682 			ql_log(ql_log_info, vha, 0x501a,
1683 			    "Alert 84XX: firmware version %x.\n",
1684 			    ha->cs84xx->op_fw_version);
1685 			break;
1686 		case A84_DIAG_LOGIN_COMPLETE:
1687 			ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
1688 			ql_log(ql_log_info, vha, 0x501b,
1689 			    "Alert 84XX: diagnostic firmware version %x.\n",
1690 			    ha->cs84xx->diag_fw_version);
1691 			break;
1692 		case A84_GOLD_LOGIN_COMPLETE:
1693 			ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
1694 			ha->cs84xx->fw_update = 1;
1695 			ql_log(ql_log_info, vha, 0x501c,
1696 			    "Alert 84XX: gold firmware version %x.\n",
1697 			    ha->cs84xx->gold_fw_version);
1698 			break;
1699 		default:
1700 			ql_log(ql_log_warn, vha, 0x501d,
1701 			    "Alert 84xx: Invalid Alert %04x %04x %04x.\n",
1702 			    mb[1], mb[2], mb[3]);
1703 		}
1704 		spin_unlock_irqrestore(&ha->cs84xx->access_lock, flags);
1705 		break;
1706 	case MBA_DCBX_START:
1707 		ql_dbg(ql_dbg_async, vha, 0x501e,
1708 		    "DCBX Started -- %04x %04x %04x.\n",
1709 		    mb[1], mb[2], mb[3]);
1710 		break;
1711 	case MBA_DCBX_PARAM_UPDATE:
1712 		ql_dbg(ql_dbg_async, vha, 0x501f,
1713 		    "DCBX Parameters Updated -- %04x %04x %04x.\n",
1714 		    mb[1], mb[2], mb[3]);
1715 		break;
1716 	case MBA_FCF_CONF_ERR:
1717 		ql_dbg(ql_dbg_async, vha, 0x5020,
1718 		    "FCF Configuration Error -- %04x %04x %04x.\n",
1719 		    mb[1], mb[2], mb[3]);
1720 		break;
1721 	case MBA_IDC_NOTIFY:
1722 		if (IS_QLA8031(vha->hw) || IS_QLA8044(ha)) {
1723 			mb[4] = rd_reg_word(&reg24->mailbox4);
1724 			if (((mb[2] & 0x7fff) == MBC_PORT_RESET ||
1725 			    (mb[2] & 0x7fff) == MBC_SET_PORT_CONFIG) &&
1726 			    (mb[4] & INTERNAL_LOOPBACK_MASK) != 0) {
1727 				set_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags);
1728 				/*
1729 				 * Extend loop down timer since port is active.
1730 				 */
1731 				if (atomic_read(&vha->loop_state) == LOOP_DOWN)
1732 					atomic_set(&vha->loop_down_timer,
1733 					    LOOP_DOWN_TIME);
1734 				qla2xxx_wake_dpc(vha);
1735 			}
1736 		}
1737 		fallthrough;
1738 	case MBA_IDC_COMPLETE:
1739 		if (ha->notify_lb_portup_comp && !vha->vp_idx)
1740 			complete(&ha->lb_portup_comp);
1741 		fallthrough;
1742 	case MBA_IDC_TIME_EXT:
1743 		if (IS_QLA81XX(vha->hw) || IS_QLA8031(vha->hw) ||
1744 		    IS_QLA8044(ha))
1745 			qla81xx_idc_event(vha, mb[0], mb[1]);
1746 		break;
1747 
1748 	case MBA_IDC_AEN:
1749 		if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
1750 			vha->hw_err_cnt++;
1751 			qla27xx_handle_8200_aen(vha, mb);
1752 		} else if (IS_QLA83XX(ha)) {
1753 			mb[4] = rd_reg_word(&reg24->mailbox4);
1754 			mb[5] = rd_reg_word(&reg24->mailbox5);
1755 			mb[6] = rd_reg_word(&reg24->mailbox6);
1756 			mb[7] = rd_reg_word(&reg24->mailbox7);
1757 			qla83xx_handle_8200_aen(vha, mb);
1758 		} else {
1759 			ql_dbg(ql_dbg_async, vha, 0x5052,
1760 			    "skip Heartbeat processing mb0-3=[0x%04x] [0x%04x] [0x%04x] [0x%04x]\n",
1761 			    mb[0], mb[1], mb[2], mb[3]);
1762 		}
1763 		break;
1764 
1765 	case MBA_DPORT_DIAGNOSTICS:
1766 		if ((mb[1] & 0xF) == AEN_DONE_DIAG_TEST_WITH_NOERR ||
1767 		    (mb[1] & 0xF) == AEN_DONE_DIAG_TEST_WITH_ERR)
1768 			vha->dport_status &= ~DPORT_DIAG_IN_PROGRESS;
1769 		ql_dbg(ql_dbg_async, vha, 0x5052,
1770 		    "D-Port Diagnostics: %04x %04x %04x %04x\n",
1771 		    mb[0], mb[1], mb[2], mb[3]);
1772 		memcpy(vha->dport_data, mb, sizeof(vha->dport_data));
1773 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
1774 			static char *results[] = {
1775 			    "start", "done(pass)", "done(error)", "undefined" };
1776 			static char *types[] = {
1777 			    "none", "dynamic", "static", "other" };
1778 			uint result = mb[1] >> 0 & 0x3;
1779 			uint type = mb[1] >> 6 & 0x3;
1780 			uint sw = mb[1] >> 15 & 0x1;
1781 			ql_dbg(ql_dbg_async, vha, 0x5052,
1782 			    "D-Port Diagnostics: result=%s type=%s [sw=%u]\n",
1783 			    results[result], types[type], sw);
1784 			if (result == 2) {
1785 				static char *reasons[] = {
1786 				    "reserved", "unexpected reject",
1787 				    "unexpected phase", "retry exceeded",
1788 				    "timed out", "not supported",
1789 				    "user stopped" };
1790 				uint reason = mb[2] >> 0 & 0xf;
1791 				uint phase = mb[2] >> 12 & 0xf;
1792 				ql_dbg(ql_dbg_async, vha, 0x5052,
1793 				    "D-Port Diagnostics: reason=%s phase=%u \n",
1794 				    reason < 7 ? reasons[reason] : "other",
1795 				    phase >> 1);
1796 			}
1797 		}
1798 		break;
1799 
1800 	case MBA_TEMPERATURE_ALERT:
1801 		ql_dbg(ql_dbg_async, vha, 0x505e,
1802 		    "TEMPERATURE ALERT: %04x %04x %04x\n", mb[1], mb[2], mb[3]);
1803 		break;
1804 
1805 	case MBA_TRANS_INSERT:
1806 		ql_dbg(ql_dbg_async, vha, 0x5091,
1807 		    "Transceiver Insertion: %04x\n", mb[1]);
1808 		set_bit(DETECT_SFP_CHANGE, &vha->dpc_flags);
1809 		break;
1810 
1811 	case MBA_TRANS_REMOVE:
1812 		ql_dbg(ql_dbg_async, vha, 0x5091, "Transceiver Removal\n");
1813 		break;
1814 
1815 	default:
1816 		ql_dbg(ql_dbg_async, vha, 0x5057,
1817 		    "Unknown AEN:%04x %04x %04x %04x\n",
1818 		    mb[0], mb[1], mb[2], mb[3]);
1819 	}
1820 
1821 	qlt_async_event(mb[0], vha, mb);
1822 
1823 	if (!vha->vp_idx && ha->num_vhosts)
1824 		qla2x00_alert_all_vps(rsp, mb);
1825 }
1826 
1827 /**
1828  * qla2x00_process_completed_request() - Process a Fast Post response.
1829  * @vha: SCSI driver HA context
1830  * @req: request queue
1831  * @index: SRB index
1832  */
1833 void
1834 qla2x00_process_completed_request(struct scsi_qla_host *vha,
1835 				  struct req_que *req, uint32_t index)
1836 {
1837 	srb_t *sp;
1838 	struct qla_hw_data *ha = vha->hw;
1839 
1840 	/* Validate handle. */
1841 	if (index >= req->num_outstanding_cmds) {
1842 		ql_log(ql_log_warn, vha, 0x3014,
1843 		    "Invalid SCSI command index (%x).\n", index);
1844 
1845 		if (IS_P3P_TYPE(ha))
1846 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1847 		else
1848 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1849 		return;
1850 	}
1851 
1852 	sp = req->outstanding_cmds[index];
1853 	if (sp) {
1854 		/* Free outstanding command slot. */
1855 		req->outstanding_cmds[index] = NULL;
1856 
1857 		/* Save ISP completion status */
1858 		sp->done(sp, DID_OK << 16);
1859 	} else {
1860 		ql_log(ql_log_warn, vha, 0x3016, "Invalid SCSI SRB.\n");
1861 
1862 		if (IS_P3P_TYPE(ha))
1863 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1864 		else
1865 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1866 	}
1867 }
1868 
1869 static srb_t *
1870 qla_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
1871 		       struct req_que *req, void *iocb, u16 *ret_index)
1872 {
1873 	struct qla_hw_data *ha = vha->hw;
1874 	sts_entry_t *pkt = iocb;
1875 	srb_t *sp;
1876 	uint16_t index;
1877 
1878 	if (pkt->handle == QLA_SKIP_HANDLE)
1879 		return NULL;
1880 
1881 	index = LSW(pkt->handle);
1882 	if (index >= req->num_outstanding_cmds) {
1883 		ql_log(ql_log_warn, vha, 0x5031,
1884 			   "%s: Invalid command index (%x) type %8ph.\n",
1885 			   func, index, iocb);
1886 		if (IS_P3P_TYPE(ha))
1887 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1888 		else
1889 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1890 		return NULL;
1891 	}
1892 	sp = req->outstanding_cmds[index];
1893 	if (!sp) {
1894 		ql_log(ql_log_warn, vha, 0x5032,
1895 			"%s: Invalid completion handle (%x) -- timed-out.\n",
1896 			func, index);
1897 		return NULL;
1898 	}
1899 	if (sp->handle != index) {
1900 		ql_log(ql_log_warn, vha, 0x5033,
1901 			"%s: SRB handle (%x) mismatch %x.\n", func,
1902 			sp->handle, index);
1903 		return NULL;
1904 	}
1905 
1906 	*ret_index = index;
1907 	qla_put_fw_resources(sp->qpair, &sp->iores);
1908 	return sp;
1909 }
1910 
1911 srb_t *
1912 qla2x00_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
1913 			   struct req_que *req, void *iocb)
1914 {
1915 	uint16_t index;
1916 	srb_t *sp;
1917 
1918 	sp = qla_get_sp_from_handle(vha, func, req, iocb, &index);
1919 	if (sp)
1920 		req->outstanding_cmds[index] = NULL;
1921 
1922 	return sp;
1923 }
1924 
1925 static void
1926 qla2x00_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1927     struct mbx_entry *mbx)
1928 {
1929 	const char func[] = "MBX-IOCB";
1930 	const char *type;
1931 	fc_port_t *fcport;
1932 	srb_t *sp;
1933 	struct srb_iocb *lio;
1934 	uint16_t *data;
1935 	uint16_t status;
1936 
1937 	sp = qla2x00_get_sp_from_handle(vha, func, req, mbx);
1938 	if (!sp)
1939 		return;
1940 
1941 	lio = &sp->u.iocb_cmd;
1942 	type = sp->name;
1943 	fcport = sp->fcport;
1944 	data = lio->u.logio.data;
1945 
1946 	data[0] = MBS_COMMAND_ERROR;
1947 	data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
1948 	    QLA_LOGIO_LOGIN_RETRIED : 0;
1949 	if (mbx->entry_status) {
1950 		ql_dbg(ql_dbg_async, vha, 0x5043,
1951 		    "Async-%s error entry - hdl=%x portid=%02x%02x%02x "
1952 		    "entry-status=%x status=%x state-flag=%x "
1953 		    "status-flags=%x.\n", type, sp->handle,
1954 		    fcport->d_id.b.domain, fcport->d_id.b.area,
1955 		    fcport->d_id.b.al_pa, mbx->entry_status,
1956 		    le16_to_cpu(mbx->status), le16_to_cpu(mbx->state_flags),
1957 		    le16_to_cpu(mbx->status_flags));
1958 
1959 		ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x5029,
1960 		    mbx, sizeof(*mbx));
1961 
1962 		goto logio_done;
1963 	}
1964 
1965 	status = le16_to_cpu(mbx->status);
1966 	if (status == 0x30 && sp->type == SRB_LOGIN_CMD &&
1967 	    le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE)
1968 		status = 0;
1969 	if (!status && le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE) {
1970 		ql_dbg(ql_dbg_async, vha, 0x5045,
1971 		    "Async-%s complete - hdl=%x portid=%02x%02x%02x mbx1=%x.\n",
1972 		    type, sp->handle, fcport->d_id.b.domain,
1973 		    fcport->d_id.b.area, fcport->d_id.b.al_pa,
1974 		    le16_to_cpu(mbx->mb1));
1975 
1976 		data[0] = MBS_COMMAND_COMPLETE;
1977 		if (sp->type == SRB_LOGIN_CMD) {
1978 			fcport->port_type = FCT_TARGET;
1979 			if (le16_to_cpu(mbx->mb1) & BIT_0)
1980 				fcport->port_type = FCT_INITIATOR;
1981 			else if (le16_to_cpu(mbx->mb1) & BIT_1)
1982 				fcport->flags |= FCF_FCP2_DEVICE;
1983 		}
1984 		goto logio_done;
1985 	}
1986 
1987 	data[0] = le16_to_cpu(mbx->mb0);
1988 	switch (data[0]) {
1989 	case MBS_PORT_ID_USED:
1990 		data[1] = le16_to_cpu(mbx->mb1);
1991 		break;
1992 	case MBS_LOOP_ID_USED:
1993 		break;
1994 	default:
1995 		data[0] = MBS_COMMAND_ERROR;
1996 		break;
1997 	}
1998 
1999 	ql_log(ql_log_warn, vha, 0x5046,
2000 	    "Async-%s failed - hdl=%x portid=%02x%02x%02x status=%x "
2001 	    "mb0=%x mb1=%x mb2=%x mb6=%x mb7=%x.\n", type, sp->handle,
2002 	    fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
2003 	    status, le16_to_cpu(mbx->mb0), le16_to_cpu(mbx->mb1),
2004 	    le16_to_cpu(mbx->mb2), le16_to_cpu(mbx->mb6),
2005 	    le16_to_cpu(mbx->mb7));
2006 
2007 logio_done:
2008 	sp->done(sp, 0);
2009 }
2010 
2011 static void
2012 qla24xx_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
2013     struct mbx_24xx_entry *pkt)
2014 {
2015 	const char func[] = "MBX-IOCB2";
2016 	struct qla_hw_data *ha = vha->hw;
2017 	srb_t *sp;
2018 	struct srb_iocb *si;
2019 	u16 sz, i;
2020 	int res;
2021 
2022 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2023 	if (!sp)
2024 		return;
2025 
2026 	if (sp->type == SRB_SCSI_CMD ||
2027 	    sp->type == SRB_NVME_CMD ||
2028 	    sp->type == SRB_TM_CMD) {
2029 		ql_log(ql_log_warn, vha, 0x509d,
2030 			"Inconsistent event entry type %d\n", sp->type);
2031 		if (IS_P3P_TYPE(ha))
2032 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
2033 		else
2034 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2035 		return;
2036 	}
2037 
2038 	si = &sp->u.iocb_cmd;
2039 	sz = min(ARRAY_SIZE(pkt->mb), ARRAY_SIZE(sp->u.iocb_cmd.u.mbx.in_mb));
2040 
2041 	for (i = 0; i < sz; i++)
2042 		si->u.mbx.in_mb[i] = pkt->mb[i];
2043 
2044 	res = (si->u.mbx.in_mb[0] & MBS_MASK);
2045 
2046 	sp->done(sp, res);
2047 }
2048 
2049 static void
2050 qla24xxx_nack_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
2051     struct nack_to_isp *pkt)
2052 {
2053 	const char func[] = "nack";
2054 	srb_t *sp;
2055 	int res = 0;
2056 
2057 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2058 	if (!sp)
2059 		return;
2060 
2061 	if (pkt->u.isp2x.status != cpu_to_le16(NOTIFY_ACK_SUCCESS))
2062 		res = QLA_FUNCTION_FAILED;
2063 
2064 	sp->done(sp, res);
2065 }
2066 
2067 static void
2068 qla2x00_ct_entry(scsi_qla_host_t *vha, struct req_que *req,
2069     sts_entry_t *pkt, int iocb_type)
2070 {
2071 	const char func[] = "CT_IOCB";
2072 	const char *type;
2073 	srb_t *sp;
2074 	struct bsg_job *bsg_job;
2075 	struct fc_bsg_reply *bsg_reply;
2076 	uint16_t comp_status;
2077 	int res = 0;
2078 
2079 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2080 	if (!sp)
2081 		return;
2082 
2083 	switch (sp->type) {
2084 	case SRB_CT_CMD:
2085 	    bsg_job = sp->u.bsg_job;
2086 	    bsg_reply = bsg_job->reply;
2087 
2088 	    type = "ct pass-through";
2089 
2090 	    comp_status = le16_to_cpu(pkt->comp_status);
2091 
2092 	    /*
2093 	     * return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
2094 	     * fc payload  to the caller
2095 	     */
2096 	    bsg_reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
2097 	    bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2098 
2099 	    if (comp_status != CS_COMPLETE) {
2100 		    if (comp_status == CS_DATA_UNDERRUN) {
2101 			    res = DID_OK << 16;
2102 			    bsg_reply->reply_payload_rcv_len =
2103 				le16_to_cpu(pkt->rsp_info_len);
2104 
2105 			    ql_log(ql_log_warn, vha, 0x5048,
2106 				"CT pass-through-%s error comp_status=0x%x total_byte=0x%x.\n",
2107 				type, comp_status,
2108 				bsg_reply->reply_payload_rcv_len);
2109 		    } else {
2110 			    ql_log(ql_log_warn, vha, 0x5049,
2111 				"CT pass-through-%s error comp_status=0x%x.\n",
2112 				type, comp_status);
2113 			    res = DID_ERROR << 16;
2114 			    bsg_reply->reply_payload_rcv_len = 0;
2115 		    }
2116 		    ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x5035,
2117 			pkt, sizeof(*pkt));
2118 	    } else {
2119 		    res = DID_OK << 16;
2120 		    bsg_reply->reply_payload_rcv_len =
2121 			bsg_job->reply_payload.payload_len;
2122 		    bsg_job->reply_len = 0;
2123 	    }
2124 	    break;
2125 	case SRB_CT_PTHRU_CMD:
2126 	    /*
2127 	     * borrowing sts_entry_24xx.comp_status.
2128 	     * same location as ct_entry_24xx.comp_status
2129 	     */
2130 	     res = qla2x00_chk_ms_status(vha, (ms_iocb_entry_t *)pkt,
2131 		 (struct ct_sns_rsp *)sp->u.iocb_cmd.u.ctarg.rsp,
2132 		 sp->name);
2133 	     break;
2134 	}
2135 
2136 	sp->done(sp, res);
2137 }
2138 
2139 static void
2140 qla24xx_els_ct_entry(scsi_qla_host_t *v, struct req_que *req,
2141     struct sts_entry_24xx *pkt, int iocb_type)
2142 {
2143 	struct els_sts_entry_24xx *ese = (struct els_sts_entry_24xx *)pkt;
2144 	const char func[] = "ELS_CT_IOCB";
2145 	const char *type;
2146 	srb_t *sp;
2147 	struct bsg_job *bsg_job;
2148 	struct fc_bsg_reply *bsg_reply;
2149 	uint16_t comp_status;
2150 	uint32_t fw_status[3];
2151 	int res, logit = 1;
2152 	struct srb_iocb *els;
2153 	uint n;
2154 	scsi_qla_host_t *vha;
2155 	struct els_sts_entry_24xx *e = (struct els_sts_entry_24xx *)pkt;
2156 
2157 	sp = qla2x00_get_sp_from_handle(v, func, req, pkt);
2158 	if (!sp)
2159 		return;
2160 	bsg_job = sp->u.bsg_job;
2161 	vha = sp->vha;
2162 
2163 	type = NULL;
2164 
2165 	comp_status = fw_status[0] = le16_to_cpu(pkt->comp_status);
2166 	fw_status[1] = le32_to_cpu(((struct els_sts_entry_24xx *)pkt)->error_subcode_1);
2167 	fw_status[2] = le32_to_cpu(((struct els_sts_entry_24xx *)pkt)->error_subcode_2);
2168 
2169 	switch (sp->type) {
2170 	case SRB_ELS_CMD_RPT:
2171 	case SRB_ELS_CMD_HST:
2172 		type = "rpt hst";
2173 		break;
2174 	case SRB_ELS_CMD_HST_NOLOGIN:
2175 		type = "els";
2176 		{
2177 			struct els_entry_24xx *els = (void *)pkt;
2178 			struct qla_bsg_auth_els_request *p =
2179 				(struct qla_bsg_auth_els_request *)bsg_job->request;
2180 
2181 			ql_dbg(ql_dbg_user, vha, 0x700f,
2182 			     "%s %s. portid=%02x%02x%02x status %x xchg %x bsg ptr %p\n",
2183 			     __func__, sc_to_str(p->e.sub_cmd),
2184 			     e->d_id[2], e->d_id[1], e->d_id[0],
2185 			     comp_status, p->e.extra_rx_xchg_address, bsg_job);
2186 
2187 			if (!(le16_to_cpu(els->control_flags) & ECF_PAYLOAD_DESCR_MASK)) {
2188 				if (sp->remap.remapped) {
2189 					n = sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2190 						bsg_job->reply_payload.sg_cnt,
2191 						sp->remap.rsp.buf,
2192 						sp->remap.rsp.len);
2193 					ql_dbg(ql_dbg_user + ql_dbg_verbose, vha, 0x700e,
2194 					   "%s: SG copied %x of %x\n",
2195 					   __func__, n, sp->remap.rsp.len);
2196 				} else {
2197 					ql_dbg(ql_dbg_user, vha, 0x700f,
2198 					   "%s: NOT REMAPPED (error)...!!!\n",
2199 					   __func__);
2200 				}
2201 			}
2202 		}
2203 		break;
2204 	case SRB_CT_CMD:
2205 		type = "ct pass-through";
2206 		break;
2207 	case SRB_ELS_DCMD:
2208 		type = "Driver ELS logo";
2209 		if (iocb_type != ELS_IOCB_TYPE) {
2210 			ql_dbg(ql_dbg_user, vha, 0x5047,
2211 			    "Completing %s: (%p) type=%d.\n",
2212 			    type, sp, sp->type);
2213 			sp->done(sp, 0);
2214 			return;
2215 		}
2216 		break;
2217 	case SRB_CT_PTHRU_CMD:
2218 		/* borrowing sts_entry_24xx.comp_status.
2219 		   same location as ct_entry_24xx.comp_status
2220 		 */
2221 		res = qla2x00_chk_ms_status(sp->vha, (ms_iocb_entry_t *)pkt,
2222 			(struct ct_sns_rsp *)sp->u.iocb_cmd.u.ctarg.rsp,
2223 			sp->name);
2224 		sp->done(sp, res);
2225 		return;
2226 	default:
2227 		ql_dbg(ql_dbg_user, vha, 0x503e,
2228 		    "Unrecognized SRB: (%p) type=%d.\n", sp, sp->type);
2229 		return;
2230 	}
2231 
2232 	if (iocb_type == ELS_IOCB_TYPE) {
2233 		els = &sp->u.iocb_cmd;
2234 		els->u.els_plogi.fw_status[0] = cpu_to_le32(fw_status[0]);
2235 		els->u.els_plogi.fw_status[1] = cpu_to_le32(fw_status[1]);
2236 		els->u.els_plogi.fw_status[2] = cpu_to_le32(fw_status[2]);
2237 		els->u.els_plogi.comp_status = cpu_to_le16(fw_status[0]);
2238 		if (comp_status == CS_COMPLETE) {
2239 			res =  DID_OK << 16;
2240 		} else {
2241 			if (comp_status == CS_DATA_UNDERRUN) {
2242 				res =  DID_OK << 16;
2243 				els->u.els_plogi.len = cpu_to_le16(le32_to_cpu(
2244 					ese->total_byte_count));
2245 
2246 				if (sp->remap.remapped &&
2247 				    ((u8 *)sp->remap.rsp.buf)[0] == ELS_LS_ACC) {
2248 					ql_dbg(ql_dbg_user, vha, 0x503f,
2249 					    "%s IOCB Done LS_ACC %02x%02x%02x -> %02x%02x%02x",
2250 					    __func__, e->s_id[0], e->s_id[2], e->s_id[1],
2251 					    e->d_id[2], e->d_id[1], e->d_id[0]);
2252 					logit = 0;
2253 				}
2254 
2255 			} else if (comp_status == CS_PORT_LOGGED_OUT) {
2256 				ql_dbg(ql_dbg_disc, vha, 0x911e,
2257 				       "%s %d schedule session deletion\n",
2258 				       __func__, __LINE__);
2259 
2260 				els->u.els_plogi.len = 0;
2261 				res = DID_IMM_RETRY << 16;
2262 				qlt_schedule_sess_for_deletion(sp->fcport);
2263 			} else {
2264 				els->u.els_plogi.len = 0;
2265 				res = DID_ERROR << 16;
2266 			}
2267 
2268 			if (sp->remap.remapped &&
2269 			    ((u8 *)sp->remap.rsp.buf)[0] == ELS_LS_RJT) {
2270 				if (logit) {
2271 					ql_dbg(ql_dbg_user, vha, 0x503f,
2272 					    "%s IOCB Done LS_RJT hdl=%x comp_status=0x%x\n",
2273 					    type, sp->handle, comp_status);
2274 
2275 					ql_dbg(ql_dbg_user, vha, 0x503f,
2276 					    "subcode 1=0x%x subcode 2=0x%x bytes=0x%x %02x%02x%02x -> %02x%02x%02x\n",
2277 					    fw_status[1], fw_status[2],
2278 					    le32_to_cpu(((struct els_sts_entry_24xx *)
2279 						pkt)->total_byte_count),
2280 					    e->s_id[0], e->s_id[2], e->s_id[1],
2281 					    e->d_id[2], e->d_id[1], e->d_id[0]);
2282 				}
2283 				if (sp->fcport && sp->fcport->flags & FCF_FCSP_DEVICE &&
2284 				    sp->type == SRB_ELS_CMD_HST_NOLOGIN) {
2285 					ql_dbg(ql_dbg_edif, vha, 0x911e,
2286 					    "%s rcv reject. Sched delete\n", __func__);
2287 					qlt_schedule_sess_for_deletion(sp->fcport);
2288 				}
2289 			} else if (logit) {
2290 				ql_log(ql_log_info, vha, 0x503f,
2291 				    "%s IOCB Done hdl=%x comp_status=0x%x\n",
2292 				    type, sp->handle, comp_status);
2293 				ql_log(ql_log_info, vha, 0x503f,
2294 				    "subcode 1=0x%x subcode 2=0x%x bytes=0x%x %02x%02x%02x -> %02x%02x%02x\n",
2295 				    fw_status[1], fw_status[2],
2296 				    le32_to_cpu(((struct els_sts_entry_24xx *)
2297 				    pkt)->total_byte_count),
2298 				    e->s_id[0], e->s_id[2], e->s_id[1],
2299 				    e->d_id[2], e->d_id[1], e->d_id[0]);
2300 			}
2301 		}
2302 		goto els_ct_done;
2303 	}
2304 
2305 	/* return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
2306 	 * fc payload  to the caller
2307 	 */
2308 	bsg_job = sp->u.bsg_job;
2309 	bsg_reply = bsg_job->reply;
2310 	bsg_reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
2311 	bsg_job->reply_len = sizeof(struct fc_bsg_reply) + sizeof(fw_status);
2312 
2313 	if (comp_status != CS_COMPLETE) {
2314 		if (comp_status == CS_DATA_UNDERRUN) {
2315 			res = DID_OK << 16;
2316 			bsg_reply->reply_payload_rcv_len =
2317 				le32_to_cpu(ese->total_byte_count);
2318 
2319 			ql_dbg(ql_dbg_user, vha, 0x503f,
2320 			    "ELS-CT pass-through-%s error hdl=%x comp_status-status=0x%x "
2321 			    "error subcode 1=0x%x error subcode 2=0x%x total_byte = 0x%x.\n",
2322 			    type, sp->handle, comp_status, fw_status[1], fw_status[2],
2323 			    le32_to_cpu(ese->total_byte_count));
2324 		} else {
2325 			ql_dbg(ql_dbg_user, vha, 0x5040,
2326 			    "ELS-CT pass-through-%s error hdl=%x comp_status-status=0x%x "
2327 			    "error subcode 1=0x%x error subcode 2=0x%x.\n",
2328 			    type, sp->handle, comp_status,
2329 			    le32_to_cpu(ese->error_subcode_1),
2330 			    le32_to_cpu(ese->error_subcode_2));
2331 			res = DID_ERROR << 16;
2332 			bsg_reply->reply_payload_rcv_len = 0;
2333 		}
2334 		memcpy(bsg_job->reply + sizeof(struct fc_bsg_reply),
2335 		       fw_status, sizeof(fw_status));
2336 		ql_dump_buffer(ql_dbg_user + ql_dbg_buffer, vha, 0x5056,
2337 		    pkt, sizeof(*pkt));
2338 	}
2339 	else {
2340 		res =  DID_OK << 16;
2341 		bsg_reply->reply_payload_rcv_len = bsg_job->reply_payload.payload_len;
2342 		bsg_job->reply_len = 0;
2343 	}
2344 els_ct_done:
2345 
2346 	sp->done(sp, res);
2347 }
2348 
2349 static void
2350 qla24xx_logio_entry(scsi_qla_host_t *vha, struct req_que *req,
2351     struct logio_entry_24xx *logio)
2352 {
2353 	const char func[] = "LOGIO-IOCB";
2354 	const char *type;
2355 	fc_port_t *fcport;
2356 	srb_t *sp;
2357 	struct srb_iocb *lio;
2358 	uint16_t *data;
2359 	uint32_t iop[2];
2360 	int logit = 1;
2361 
2362 	sp = qla2x00_get_sp_from_handle(vha, func, req, logio);
2363 	if (!sp)
2364 		return;
2365 
2366 	lio = &sp->u.iocb_cmd;
2367 	type = sp->name;
2368 	fcport = sp->fcport;
2369 	data = lio->u.logio.data;
2370 
2371 	data[0] = MBS_COMMAND_ERROR;
2372 	data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
2373 		QLA_LOGIO_LOGIN_RETRIED : 0;
2374 	if (logio->entry_status) {
2375 		ql_log(ql_log_warn, fcport->vha, 0x5034,
2376 		    "Async-%s error entry - %8phC hdl=%x"
2377 		    "portid=%02x%02x%02x entry-status=%x.\n",
2378 		    type, fcport->port_name, sp->handle, fcport->d_id.b.domain,
2379 		    fcport->d_id.b.area, fcport->d_id.b.al_pa,
2380 		    logio->entry_status);
2381 		ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x504d,
2382 		    logio, sizeof(*logio));
2383 
2384 		goto logio_done;
2385 	}
2386 
2387 	if (le16_to_cpu(logio->comp_status) == CS_COMPLETE) {
2388 		ql_dbg(ql_dbg_async, sp->vha, 0x5036,
2389 		    "Async-%s complete: handle=%x pid=%06x wwpn=%8phC iop0=%x\n",
2390 		    type, sp->handle, fcport->d_id.b24, fcport->port_name,
2391 		    le32_to_cpu(logio->io_parameter[0]));
2392 
2393 		vha->hw->exch_starvation = 0;
2394 		data[0] = MBS_COMMAND_COMPLETE;
2395 
2396 		if (sp->type == SRB_PRLI_CMD) {
2397 			lio->u.logio.iop[0] =
2398 			    le32_to_cpu(logio->io_parameter[0]);
2399 			lio->u.logio.iop[1] =
2400 			    le32_to_cpu(logio->io_parameter[1]);
2401 			goto logio_done;
2402 		}
2403 
2404 		if (sp->type != SRB_LOGIN_CMD)
2405 			goto logio_done;
2406 
2407 		lio->u.logio.iop[1] = le32_to_cpu(logio->io_parameter[5]);
2408 		if (le32_to_cpu(logio->io_parameter[5]) & LIO_COMM_FEAT_FCSP)
2409 			fcport->flags |= FCF_FCSP_DEVICE;
2410 
2411 		iop[0] = le32_to_cpu(logio->io_parameter[0]);
2412 		if (iop[0] & BIT_4) {
2413 			fcport->port_type = FCT_TARGET;
2414 			if (iop[0] & BIT_8)
2415 				fcport->flags |= FCF_FCP2_DEVICE;
2416 		} else if (iop[0] & BIT_5)
2417 			fcport->port_type = FCT_INITIATOR;
2418 
2419 		if (iop[0] & BIT_7)
2420 			fcport->flags |= FCF_CONF_COMP_SUPPORTED;
2421 
2422 		if (logio->io_parameter[7] || logio->io_parameter[8])
2423 			fcport->supported_classes |= FC_COS_CLASS2;
2424 		if (logio->io_parameter[9] || logio->io_parameter[10])
2425 			fcport->supported_classes |= FC_COS_CLASS3;
2426 
2427 		goto logio_done;
2428 	}
2429 
2430 	iop[0] = le32_to_cpu(logio->io_parameter[0]);
2431 	iop[1] = le32_to_cpu(logio->io_parameter[1]);
2432 	lio->u.logio.iop[0] = iop[0];
2433 	lio->u.logio.iop[1] = iop[1];
2434 	switch (iop[0]) {
2435 	case LSC_SCODE_PORTID_USED:
2436 		data[0] = MBS_PORT_ID_USED;
2437 		data[1] = LSW(iop[1]);
2438 		logit = 0;
2439 		break;
2440 	case LSC_SCODE_NPORT_USED:
2441 		data[0] = MBS_LOOP_ID_USED;
2442 		logit = 0;
2443 		break;
2444 	case LSC_SCODE_CMD_FAILED:
2445 		if (iop[1] == 0x0606) {
2446 			/*
2447 			 * PLOGI/PRLI Completed. We must have Recv PLOGI/PRLI,
2448 			 * Target side acked.
2449 			 */
2450 			data[0] = MBS_COMMAND_COMPLETE;
2451 			goto logio_done;
2452 		}
2453 		data[0] = MBS_COMMAND_ERROR;
2454 		break;
2455 	case LSC_SCODE_NOXCB:
2456 		vha->hw->exch_starvation++;
2457 		if (vha->hw->exch_starvation > 5) {
2458 			ql_log(ql_log_warn, vha, 0xd046,
2459 			    "Exchange starvation. Resetting RISC\n");
2460 
2461 			vha->hw->exch_starvation = 0;
2462 
2463 			if (IS_P3P_TYPE(vha->hw))
2464 				set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
2465 			else
2466 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2467 			qla2xxx_wake_dpc(vha);
2468 		}
2469 		fallthrough;
2470 	default:
2471 		data[0] = MBS_COMMAND_ERROR;
2472 		break;
2473 	}
2474 
2475 	if (logit)
2476 		ql_log(ql_log_warn, sp->vha, 0x5037, "Async-%s failed: "
2477 		       "handle=%x pid=%06x wwpn=%8phC comp_status=%x iop0=%x iop1=%x\n",
2478 		       type, sp->handle, fcport->d_id.b24, fcport->port_name,
2479 		       le16_to_cpu(logio->comp_status),
2480 		       le32_to_cpu(logio->io_parameter[0]),
2481 		       le32_to_cpu(logio->io_parameter[1]));
2482 	else
2483 		ql_dbg(ql_dbg_disc, sp->vha, 0x5037, "Async-%s failed: "
2484 		       "handle=%x pid=%06x wwpn=%8phC comp_status=%x iop0=%x iop1=%x\n",
2485 		       type, sp->handle, fcport->d_id.b24, fcport->port_name,
2486 		       le16_to_cpu(logio->comp_status),
2487 		       le32_to_cpu(logio->io_parameter[0]),
2488 		       le32_to_cpu(logio->io_parameter[1]));
2489 
2490 logio_done:
2491 	sp->done(sp, 0);
2492 }
2493 
2494 static void
2495 qla24xx_tm_iocb_entry(scsi_qla_host_t *vha, struct req_que *req, void *tsk)
2496 {
2497 	const char func[] = "TMF-IOCB";
2498 	const char *type;
2499 	fc_port_t *fcport;
2500 	srb_t *sp;
2501 	struct srb_iocb *iocb;
2502 	struct sts_entry_24xx *sts = (struct sts_entry_24xx *)tsk;
2503 	u16 comp_status;
2504 
2505 	sp = qla2x00_get_sp_from_handle(vha, func, req, tsk);
2506 	if (!sp)
2507 		return;
2508 
2509 	comp_status = le16_to_cpu(sts->comp_status);
2510 	iocb = &sp->u.iocb_cmd;
2511 	type = sp->name;
2512 	fcport = sp->fcport;
2513 	iocb->u.tmf.data = QLA_SUCCESS;
2514 
2515 	if (sts->entry_status) {
2516 		ql_log(ql_log_warn, fcport->vha, 0x5038,
2517 		    "Async-%s error - hdl=%x entry-status(%x).\n",
2518 		    type, sp->handle, sts->entry_status);
2519 		iocb->u.tmf.data = QLA_FUNCTION_FAILED;
2520 	} else if (sts->comp_status != cpu_to_le16(CS_COMPLETE)) {
2521 		ql_log(ql_log_warn, fcport->vha, 0x5039,
2522 		    "Async-%s error - hdl=%x completion status(%x).\n",
2523 		    type, sp->handle, comp_status);
2524 		iocb->u.tmf.data = QLA_FUNCTION_FAILED;
2525 	} else if ((le16_to_cpu(sts->scsi_status) &
2526 	    SS_RESPONSE_INFO_LEN_VALID)) {
2527 		host_to_fcp_swap(sts->data, sizeof(sts->data));
2528 		if (le32_to_cpu(sts->rsp_data_len) < 4) {
2529 			ql_log(ql_log_warn, fcport->vha, 0x503b,
2530 			    "Async-%s error - hdl=%x not enough response(%d).\n",
2531 			    type, sp->handle, sts->rsp_data_len);
2532 		} else if (sts->data[3]) {
2533 			ql_log(ql_log_warn, fcport->vha, 0x503c,
2534 			    "Async-%s error - hdl=%x response(%x).\n",
2535 			    type, sp->handle, sts->data[3]);
2536 			iocb->u.tmf.data = QLA_FUNCTION_FAILED;
2537 		}
2538 	}
2539 
2540 	switch (comp_status) {
2541 	case CS_PORT_LOGGED_OUT:
2542 	case CS_PORT_CONFIG_CHG:
2543 	case CS_PORT_BUSY:
2544 	case CS_INCOMPLETE:
2545 	case CS_PORT_UNAVAILABLE:
2546 	case CS_RESET:
2547 		if (atomic_read(&fcport->state) == FCS_ONLINE) {
2548 			ql_dbg(ql_dbg_disc, fcport->vha, 0x3021,
2549 			       "-Port to be marked lost on fcport=%02x%02x%02x, current port state= %s comp_status %x.\n",
2550 			       fcport->d_id.b.domain, fcport->d_id.b.area,
2551 			       fcport->d_id.b.al_pa,
2552 			       port_state_str[FCS_ONLINE],
2553 			       comp_status);
2554 
2555 			qlt_schedule_sess_for_deletion(fcport);
2556 		}
2557 		break;
2558 
2559 	default:
2560 		break;
2561 	}
2562 
2563 	if (iocb->u.tmf.data != QLA_SUCCESS)
2564 		ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, sp->vha, 0x5055,
2565 		    sts, sizeof(*sts));
2566 
2567 	sp->done(sp, 0);
2568 }
2569 
2570 static void qla24xx_nvme_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
2571     void *tsk, srb_t *sp)
2572 {
2573 	fc_port_t *fcport;
2574 	struct srb_iocb *iocb;
2575 	struct sts_entry_24xx *sts = (struct sts_entry_24xx *)tsk;
2576 	uint16_t        state_flags;
2577 	struct nvmefc_fcp_req *fd;
2578 	uint16_t        ret = QLA_SUCCESS;
2579 	__le16		comp_status = sts->comp_status;
2580 	int		logit = 0;
2581 
2582 	iocb = &sp->u.iocb_cmd;
2583 	fcport = sp->fcport;
2584 	iocb->u.nvme.comp_status = comp_status;
2585 	state_flags  = le16_to_cpu(sts->state_flags);
2586 	fd = iocb->u.nvme.desc;
2587 
2588 	if (unlikely(iocb->u.nvme.aen_op))
2589 		atomic_dec(&sp->vha->hw->nvme_active_aen_cnt);
2590 	else
2591 		sp->qpair->cmd_completion_cnt++;
2592 
2593 	if (unlikely(comp_status != CS_COMPLETE))
2594 		logit = 1;
2595 
2596 	fd->transferred_length = fd->payload_length -
2597 	    le32_to_cpu(sts->residual_len);
2598 
2599 	/*
2600 	 * State flags: Bit 6 and 0.
2601 	 * If 0 is set, we don't care about 6.
2602 	 * both cases resp was dma'd to host buffer
2603 	 * if both are 0, that is good path case.
2604 	 * if six is set and 0 is clear, we need to
2605 	 * copy resp data from status iocb to resp buffer.
2606 	 */
2607 	if (!(state_flags & (SF_FCP_RSP_DMA | SF_NVME_ERSP))) {
2608 		iocb->u.nvme.rsp_pyld_len = 0;
2609 	} else if ((state_flags & (SF_FCP_RSP_DMA | SF_NVME_ERSP)) ==
2610 			(SF_FCP_RSP_DMA | SF_NVME_ERSP)) {
2611 		/* Response already DMA'd to fd->rspaddr. */
2612 		iocb->u.nvme.rsp_pyld_len = sts->nvme_rsp_pyld_len;
2613 	} else if ((state_flags & SF_FCP_RSP_DMA)) {
2614 		/*
2615 		 * Non-zero value in first 12 bytes of NVMe_RSP IU, treat this
2616 		 * as an error.
2617 		 */
2618 		iocb->u.nvme.rsp_pyld_len = 0;
2619 		fd->transferred_length = 0;
2620 		ql_dbg(ql_dbg_io, fcport->vha, 0x307a,
2621 			"Unexpected values in NVMe_RSP IU.\n");
2622 		logit = 1;
2623 	} else if (state_flags & SF_NVME_ERSP) {
2624 		uint32_t *inbuf, *outbuf;
2625 		uint16_t iter;
2626 
2627 		inbuf = (uint32_t *)&sts->nvme_ersp_data;
2628 		outbuf = (uint32_t *)fd->rspaddr;
2629 		iocb->u.nvme.rsp_pyld_len = sts->nvme_rsp_pyld_len;
2630 		if (unlikely(le16_to_cpu(iocb->u.nvme.rsp_pyld_len) >
2631 		    sizeof(struct nvme_fc_ersp_iu))) {
2632 			if (ql_mask_match(ql_dbg_io)) {
2633 				WARN_ONCE(1, "Unexpected response payload length %u.\n",
2634 				    iocb->u.nvme.rsp_pyld_len);
2635 				ql_log(ql_log_warn, fcport->vha, 0x5100,
2636 				    "Unexpected response payload length %u.\n",
2637 				    iocb->u.nvme.rsp_pyld_len);
2638 			}
2639 			iocb->u.nvme.rsp_pyld_len =
2640 				cpu_to_le16(sizeof(struct nvme_fc_ersp_iu));
2641 		}
2642 		iter = le16_to_cpu(iocb->u.nvme.rsp_pyld_len) >> 2;
2643 		for (; iter; iter--)
2644 			*outbuf++ = swab32(*inbuf++);
2645 	}
2646 
2647 	if (state_flags & SF_NVME_ERSP) {
2648 		struct nvme_fc_ersp_iu *rsp_iu = fd->rspaddr;
2649 		u32 tgt_xfer_len;
2650 
2651 		tgt_xfer_len = be32_to_cpu(rsp_iu->xfrd_len);
2652 		if (fd->transferred_length != tgt_xfer_len) {
2653 			ql_log(ql_log_warn, fcport->vha, 0x3079,
2654 			       "Dropped frame(s) detected (sent/rcvd=%u/%u).\n",
2655 			       tgt_xfer_len, fd->transferred_length);
2656 			logit = 1;
2657 		} else if (le16_to_cpu(comp_status) == CS_DATA_UNDERRUN) {
2658 			/*
2659 			 * Do not log if this is just an underflow and there
2660 			 * is no data loss.
2661 			 */
2662 			logit = 0;
2663 		}
2664 	}
2665 
2666 	if (unlikely(logit))
2667 		ql_dbg(ql_dbg_io, fcport->vha, 0x5060,
2668 		   "NVME-%s ERR Handling - hdl=%x status(%x) tr_len:%x resid=%x  ox_id=%x\n",
2669 		   sp->name, sp->handle, comp_status,
2670 		   fd->transferred_length, le32_to_cpu(sts->residual_len),
2671 		   sts->ox_id);
2672 
2673 	/*
2674 	 * If transport error then Failure (HBA rejects request)
2675 	 * otherwise transport will handle.
2676 	 */
2677 	switch (le16_to_cpu(comp_status)) {
2678 	case CS_COMPLETE:
2679 		break;
2680 
2681 	case CS_RESET:
2682 	case CS_PORT_UNAVAILABLE:
2683 	case CS_PORT_LOGGED_OUT:
2684 		fcport->nvme_flag |= NVME_FLAG_RESETTING;
2685 		if (atomic_read(&fcport->state) == FCS_ONLINE) {
2686 			ql_dbg(ql_dbg_disc, fcport->vha, 0x3021,
2687 			       "Port to be marked lost on fcport=%06x, current "
2688 			       "port state= %s comp_status %x.\n",
2689 			       fcport->d_id.b24, port_state_str[FCS_ONLINE],
2690 			       comp_status);
2691 
2692 			qlt_schedule_sess_for_deletion(fcport);
2693 		}
2694 		fallthrough;
2695 	case CS_ABORTED:
2696 	case CS_PORT_BUSY:
2697 		fd->transferred_length = 0;
2698 		iocb->u.nvme.rsp_pyld_len = 0;
2699 		ret = QLA_ABORTED;
2700 		break;
2701 	case CS_DATA_UNDERRUN:
2702 		break;
2703 	default:
2704 		ret = QLA_FUNCTION_FAILED;
2705 		break;
2706 	}
2707 	sp->done(sp, ret);
2708 }
2709 
2710 static void qla_ctrlvp_completed(scsi_qla_host_t *vha, struct req_que *req,
2711     struct vp_ctrl_entry_24xx *vce)
2712 {
2713 	const char func[] = "CTRLVP-IOCB";
2714 	srb_t *sp;
2715 	int rval = QLA_SUCCESS;
2716 
2717 	sp = qla2x00_get_sp_from_handle(vha, func, req, vce);
2718 	if (!sp)
2719 		return;
2720 
2721 	if (vce->entry_status != 0) {
2722 		ql_dbg(ql_dbg_vport, vha, 0x10c4,
2723 		    "%s: Failed to complete IOCB -- error status (%x)\n",
2724 		    sp->name, vce->entry_status);
2725 		rval = QLA_FUNCTION_FAILED;
2726 	} else if (vce->comp_status != cpu_to_le16(CS_COMPLETE)) {
2727 		ql_dbg(ql_dbg_vport, vha, 0x10c5,
2728 		    "%s: Failed to complete IOCB -- completion status (%x) vpidx %x\n",
2729 		    sp->name, le16_to_cpu(vce->comp_status),
2730 		    le16_to_cpu(vce->vp_idx_failed));
2731 		rval = QLA_FUNCTION_FAILED;
2732 	} else {
2733 		ql_dbg(ql_dbg_vport, vha, 0x10c6,
2734 		    "Done %s.\n", __func__);
2735 	}
2736 
2737 	sp->rc = rval;
2738 	sp->done(sp, rval);
2739 }
2740 
2741 /* Process a single response queue entry. */
2742 static void qla2x00_process_response_entry(struct scsi_qla_host *vha,
2743 					   struct rsp_que *rsp,
2744 					   sts_entry_t *pkt)
2745 {
2746 	sts21_entry_t *sts21_entry;
2747 	sts22_entry_t *sts22_entry;
2748 	uint16_t handle_cnt;
2749 	uint16_t cnt;
2750 
2751 	switch (pkt->entry_type) {
2752 	case STATUS_TYPE:
2753 		qla2x00_status_entry(vha, rsp, pkt);
2754 		break;
2755 	case STATUS_TYPE_21:
2756 		sts21_entry = (sts21_entry_t *)pkt;
2757 		handle_cnt = sts21_entry->handle_count;
2758 		for (cnt = 0; cnt < handle_cnt; cnt++)
2759 			qla2x00_process_completed_request(vha, rsp->req,
2760 						sts21_entry->handle[cnt]);
2761 		break;
2762 	case STATUS_TYPE_22:
2763 		sts22_entry = (sts22_entry_t *)pkt;
2764 		handle_cnt = sts22_entry->handle_count;
2765 		for (cnt = 0; cnt < handle_cnt; cnt++)
2766 			qla2x00_process_completed_request(vha, rsp->req,
2767 						sts22_entry->handle[cnt]);
2768 		break;
2769 	case STATUS_CONT_TYPE:
2770 		qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
2771 		break;
2772 	case MBX_IOCB_TYPE:
2773 		qla2x00_mbx_iocb_entry(vha, rsp->req, (struct mbx_entry *)pkt);
2774 		break;
2775 	case CT_IOCB_TYPE:
2776 		qla2x00_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
2777 		break;
2778 	default:
2779 		/* Type Not Supported. */
2780 		ql_log(ql_log_warn, vha, 0x504a,
2781 		       "Received unknown response pkt type %x entry status=%x.\n",
2782 		       pkt->entry_type, pkt->entry_status);
2783 		break;
2784 	}
2785 }
2786 
2787 /**
2788  * qla2x00_process_response_queue() - Process response queue entries.
2789  * @rsp: response queue
2790  */
2791 void
2792 qla2x00_process_response_queue(struct rsp_que *rsp)
2793 {
2794 	struct scsi_qla_host *vha;
2795 	struct qla_hw_data *ha = rsp->hw;
2796 	struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2797 	sts_entry_t	*pkt;
2798 
2799 	vha = pci_get_drvdata(ha->pdev);
2800 
2801 	if (!vha->flags.online)
2802 		return;
2803 
2804 	while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
2805 		pkt = (sts_entry_t *)rsp->ring_ptr;
2806 
2807 		rsp->ring_index++;
2808 		if (rsp->ring_index == rsp->length) {
2809 			rsp->ring_index = 0;
2810 			rsp->ring_ptr = rsp->ring;
2811 		} else {
2812 			rsp->ring_ptr++;
2813 		}
2814 
2815 		if (pkt->entry_status != 0) {
2816 			qla2x00_error_entry(vha, rsp, pkt);
2817 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
2818 			wmb();
2819 			continue;
2820 		}
2821 
2822 		qla2x00_process_response_entry(vha, rsp, pkt);
2823 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
2824 		wmb();
2825 	}
2826 
2827 	/* Adjust ring index */
2828 	wrt_reg_word(ISP_RSP_Q_OUT(ha, reg), rsp->ring_index);
2829 }
2830 
2831 static inline void
2832 qla2x00_handle_sense(srb_t *sp, uint8_t *sense_data, uint32_t par_sense_len,
2833 		     uint32_t sense_len, struct rsp_que *rsp, int res)
2834 {
2835 	struct scsi_qla_host *vha = sp->vha;
2836 	struct scsi_cmnd *cp = GET_CMD_SP(sp);
2837 	uint32_t track_sense_len;
2838 
2839 	if (sense_len >= SCSI_SENSE_BUFFERSIZE)
2840 		sense_len = SCSI_SENSE_BUFFERSIZE;
2841 
2842 	SET_CMD_SENSE_LEN(sp, sense_len);
2843 	SET_CMD_SENSE_PTR(sp, cp->sense_buffer);
2844 	track_sense_len = sense_len;
2845 
2846 	if (sense_len > par_sense_len)
2847 		sense_len = par_sense_len;
2848 
2849 	memcpy(cp->sense_buffer, sense_data, sense_len);
2850 
2851 	SET_CMD_SENSE_PTR(sp, cp->sense_buffer + sense_len);
2852 	track_sense_len -= sense_len;
2853 	SET_CMD_SENSE_LEN(sp, track_sense_len);
2854 
2855 	if (track_sense_len != 0) {
2856 		rsp->status_srb = sp;
2857 		cp->result = res;
2858 	}
2859 
2860 	if (sense_len) {
2861 		ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x301c,
2862 		    "Check condition Sense data, nexus%ld:%d:%llu cmd=%p.\n",
2863 		    sp->vha->host_no, cp->device->id, cp->device->lun,
2864 		    cp);
2865 		ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302b,
2866 		    cp->sense_buffer, sense_len);
2867 	}
2868 }
2869 
2870 struct scsi_dif_tuple {
2871 	__be16 guard;       /* Checksum */
2872 	__be16 app_tag;         /* APPL identifier */
2873 	__be32 ref_tag;         /* Target LBA or indirect LBA */
2874 };
2875 
2876 /*
2877  * Checks the guard or meta-data for the type of error
2878  * detected by the HBA. In case of errors, we set the
2879  * ASC/ASCQ fields in the sense buffer with ILLEGAL_REQUEST
2880  * to indicate to the kernel that the HBA detected error.
2881  */
2882 static inline int
2883 qla2x00_handle_dif_error(srb_t *sp, struct sts_entry_24xx *sts24)
2884 {
2885 	struct scsi_qla_host *vha = sp->vha;
2886 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
2887 	uint8_t		*ap = &sts24->data[12];
2888 	uint8_t		*ep = &sts24->data[20];
2889 	uint32_t	e_ref_tag, a_ref_tag;
2890 	uint16_t	e_app_tag, a_app_tag;
2891 	uint16_t	e_guard, a_guard;
2892 
2893 	/*
2894 	 * swab32 of the "data" field in the beginning of qla2x00_status_entry()
2895 	 * would make guard field appear at offset 2
2896 	 */
2897 	a_guard   = get_unaligned_le16(ap + 2);
2898 	a_app_tag = get_unaligned_le16(ap + 0);
2899 	a_ref_tag = get_unaligned_le32(ap + 4);
2900 	e_guard   = get_unaligned_le16(ep + 2);
2901 	e_app_tag = get_unaligned_le16(ep + 0);
2902 	e_ref_tag = get_unaligned_le32(ep + 4);
2903 
2904 	ql_dbg(ql_dbg_io, vha, 0x3023,
2905 	    "iocb(s) %p Returned STATUS.\n", sts24);
2906 
2907 	ql_dbg(ql_dbg_io, vha, 0x3024,
2908 	    "DIF ERROR in cmd 0x%x lba 0x%llx act ref"
2909 	    " tag=0x%x, exp ref_tag=0x%x, act app tag=0x%x, exp app"
2910 	    " tag=0x%x, act guard=0x%x, exp guard=0x%x.\n",
2911 	    cmd->cmnd[0], (u64)scsi_get_lba(cmd), a_ref_tag, e_ref_tag,
2912 	    a_app_tag, e_app_tag, a_guard, e_guard);
2913 
2914 	/*
2915 	 * Ignore sector if:
2916 	 * For type     3: ref & app tag is all 'f's
2917 	 * For type 0,1,2: app tag is all 'f's
2918 	 */
2919 	if (a_app_tag == be16_to_cpu(T10_PI_APP_ESCAPE) &&
2920 	    (scsi_get_prot_type(cmd) != SCSI_PROT_DIF_TYPE3 ||
2921 	     a_ref_tag == be32_to_cpu(T10_PI_REF_ESCAPE))) {
2922 		uint32_t blocks_done, resid;
2923 		sector_t lba_s = scsi_get_lba(cmd);
2924 
2925 		/* 2TB boundary case covered automatically with this */
2926 		blocks_done = e_ref_tag - (uint32_t)lba_s + 1;
2927 
2928 		resid = scsi_bufflen(cmd) - (blocks_done *
2929 		    cmd->device->sector_size);
2930 
2931 		scsi_set_resid(cmd, resid);
2932 		cmd->result = DID_OK << 16;
2933 
2934 		/* Update protection tag */
2935 		if (scsi_prot_sg_count(cmd)) {
2936 			uint32_t i, j = 0, k = 0, num_ent;
2937 			struct scatterlist *sg;
2938 			struct t10_pi_tuple *spt;
2939 
2940 			/* Patch the corresponding protection tags */
2941 			scsi_for_each_prot_sg(cmd, sg,
2942 			    scsi_prot_sg_count(cmd), i) {
2943 				num_ent = sg_dma_len(sg) / 8;
2944 				if (k + num_ent < blocks_done) {
2945 					k += num_ent;
2946 					continue;
2947 				}
2948 				j = blocks_done - k - 1;
2949 				k = blocks_done;
2950 				break;
2951 			}
2952 
2953 			if (k != blocks_done) {
2954 				ql_log(ql_log_warn, vha, 0x302f,
2955 				    "unexpected tag values tag:lba=%x:%llx)\n",
2956 				    e_ref_tag, (unsigned long long)lba_s);
2957 				return 1;
2958 			}
2959 
2960 			spt = page_address(sg_page(sg)) + sg->offset;
2961 			spt += j;
2962 
2963 			spt->app_tag = T10_PI_APP_ESCAPE;
2964 			if (scsi_get_prot_type(cmd) == SCSI_PROT_DIF_TYPE3)
2965 				spt->ref_tag = T10_PI_REF_ESCAPE;
2966 		}
2967 
2968 		return 0;
2969 	}
2970 
2971 	/* check guard */
2972 	if (e_guard != a_guard) {
2973 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
2974 		set_host_byte(cmd, DID_ABORT);
2975 		return 1;
2976 	}
2977 
2978 	/* check ref tag */
2979 	if (e_ref_tag != a_ref_tag) {
2980 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
2981 		set_host_byte(cmd, DID_ABORT);
2982 		return 1;
2983 	}
2984 
2985 	/* check appl tag */
2986 	if (e_app_tag != a_app_tag) {
2987 		scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
2988 		set_host_byte(cmd, DID_ABORT);
2989 		return 1;
2990 	}
2991 
2992 	return 1;
2993 }
2994 
2995 static void
2996 qla25xx_process_bidir_status_iocb(scsi_qla_host_t *vha, void *pkt,
2997 				  struct req_que *req, uint32_t index)
2998 {
2999 	struct qla_hw_data *ha = vha->hw;
3000 	srb_t *sp;
3001 	uint16_t	comp_status;
3002 	uint16_t	scsi_status;
3003 	uint16_t thread_id;
3004 	uint32_t rval = EXT_STATUS_OK;
3005 	struct bsg_job *bsg_job = NULL;
3006 	struct fc_bsg_request *bsg_request;
3007 	struct fc_bsg_reply *bsg_reply;
3008 	sts_entry_t *sts = pkt;
3009 	struct sts_entry_24xx *sts24 = pkt;
3010 
3011 	/* Validate handle. */
3012 	if (index >= req->num_outstanding_cmds) {
3013 		ql_log(ql_log_warn, vha, 0x70af,
3014 		    "Invalid SCSI completion handle 0x%x.\n", index);
3015 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3016 		return;
3017 	}
3018 
3019 	sp = req->outstanding_cmds[index];
3020 	if (!sp) {
3021 		ql_log(ql_log_warn, vha, 0x70b0,
3022 		    "Req:%d: Invalid ISP SCSI completion handle(0x%x)\n",
3023 		    req->id, index);
3024 
3025 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3026 		return;
3027 	}
3028 
3029 	/* Free outstanding command slot. */
3030 	req->outstanding_cmds[index] = NULL;
3031 	bsg_job = sp->u.bsg_job;
3032 	bsg_request = bsg_job->request;
3033 	bsg_reply = bsg_job->reply;
3034 
3035 	if (IS_FWI2_CAPABLE(ha)) {
3036 		comp_status = le16_to_cpu(sts24->comp_status);
3037 		scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
3038 	} else {
3039 		comp_status = le16_to_cpu(sts->comp_status);
3040 		scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
3041 	}
3042 
3043 	thread_id = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
3044 	switch (comp_status) {
3045 	case CS_COMPLETE:
3046 		if (scsi_status == 0) {
3047 			bsg_reply->reply_payload_rcv_len =
3048 					bsg_job->reply_payload.payload_len;
3049 			vha->qla_stats.input_bytes +=
3050 				bsg_reply->reply_payload_rcv_len;
3051 			vha->qla_stats.input_requests++;
3052 			rval = EXT_STATUS_OK;
3053 		}
3054 		goto done;
3055 
3056 	case CS_DATA_OVERRUN:
3057 		ql_dbg(ql_dbg_user, vha, 0x70b1,
3058 		    "Command completed with data overrun thread_id=%d\n",
3059 		    thread_id);
3060 		rval = EXT_STATUS_DATA_OVERRUN;
3061 		break;
3062 
3063 	case CS_DATA_UNDERRUN:
3064 		ql_dbg(ql_dbg_user, vha, 0x70b2,
3065 		    "Command completed with data underrun thread_id=%d\n",
3066 		    thread_id);
3067 		rval = EXT_STATUS_DATA_UNDERRUN;
3068 		break;
3069 	case CS_BIDIR_RD_OVERRUN:
3070 		ql_dbg(ql_dbg_user, vha, 0x70b3,
3071 		    "Command completed with read data overrun thread_id=%d\n",
3072 		    thread_id);
3073 		rval = EXT_STATUS_DATA_OVERRUN;
3074 		break;
3075 
3076 	case CS_BIDIR_RD_WR_OVERRUN:
3077 		ql_dbg(ql_dbg_user, vha, 0x70b4,
3078 		    "Command completed with read and write data overrun "
3079 		    "thread_id=%d\n", thread_id);
3080 		rval = EXT_STATUS_DATA_OVERRUN;
3081 		break;
3082 
3083 	case CS_BIDIR_RD_OVERRUN_WR_UNDERRUN:
3084 		ql_dbg(ql_dbg_user, vha, 0x70b5,
3085 		    "Command completed with read data over and write data "
3086 		    "underrun thread_id=%d\n", thread_id);
3087 		rval = EXT_STATUS_DATA_OVERRUN;
3088 		break;
3089 
3090 	case CS_BIDIR_RD_UNDERRUN:
3091 		ql_dbg(ql_dbg_user, vha, 0x70b6,
3092 		    "Command completed with read data underrun "
3093 		    "thread_id=%d\n", thread_id);
3094 		rval = EXT_STATUS_DATA_UNDERRUN;
3095 		break;
3096 
3097 	case CS_BIDIR_RD_UNDERRUN_WR_OVERRUN:
3098 		ql_dbg(ql_dbg_user, vha, 0x70b7,
3099 		    "Command completed with read data under and write data "
3100 		    "overrun thread_id=%d\n", thread_id);
3101 		rval = EXT_STATUS_DATA_UNDERRUN;
3102 		break;
3103 
3104 	case CS_BIDIR_RD_WR_UNDERRUN:
3105 		ql_dbg(ql_dbg_user, vha, 0x70b8,
3106 		    "Command completed with read and write data underrun "
3107 		    "thread_id=%d\n", thread_id);
3108 		rval = EXT_STATUS_DATA_UNDERRUN;
3109 		break;
3110 
3111 	case CS_BIDIR_DMA:
3112 		ql_dbg(ql_dbg_user, vha, 0x70b9,
3113 		    "Command completed with data DMA error thread_id=%d\n",
3114 		    thread_id);
3115 		rval = EXT_STATUS_DMA_ERR;
3116 		break;
3117 
3118 	case CS_TIMEOUT:
3119 		ql_dbg(ql_dbg_user, vha, 0x70ba,
3120 		    "Command completed with timeout thread_id=%d\n",
3121 		    thread_id);
3122 		rval = EXT_STATUS_TIMEOUT;
3123 		break;
3124 	default:
3125 		ql_dbg(ql_dbg_user, vha, 0x70bb,
3126 		    "Command completed with completion status=0x%x "
3127 		    "thread_id=%d\n", comp_status, thread_id);
3128 		rval = EXT_STATUS_ERR;
3129 		break;
3130 	}
3131 	bsg_reply->reply_payload_rcv_len = 0;
3132 
3133 done:
3134 	/* Return the vendor specific reply to API */
3135 	bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
3136 	bsg_job->reply_len = sizeof(struct fc_bsg_reply);
3137 	/* Always return DID_OK, bsg will send the vendor specific response
3138 	 * in this case only */
3139 	sp->done(sp, DID_OK << 16);
3140 
3141 }
3142 
3143 /**
3144  * qla2x00_status_entry() - Process a Status IOCB entry.
3145  * @vha: SCSI driver HA context
3146  * @rsp: response queue
3147  * @pkt: Entry pointer
3148  */
3149 static void
3150 qla2x00_status_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, void *pkt)
3151 {
3152 	srb_t		*sp;
3153 	fc_port_t	*fcport;
3154 	struct scsi_cmnd *cp;
3155 	sts_entry_t *sts = pkt;
3156 	struct sts_entry_24xx *sts24 = pkt;
3157 	uint16_t	comp_status;
3158 	uint16_t	scsi_status;
3159 	uint16_t	ox_id;
3160 	uint8_t		lscsi_status;
3161 	int32_t		resid;
3162 	uint32_t sense_len, par_sense_len, rsp_info_len, resid_len,
3163 	    fw_resid_len;
3164 	uint8_t		*rsp_info, *sense_data;
3165 	struct qla_hw_data *ha = vha->hw;
3166 	uint32_t handle;
3167 	uint16_t que;
3168 	struct req_que *req;
3169 	int logit = 1;
3170 	int res = 0;
3171 	uint16_t state_flags = 0;
3172 	uint16_t sts_qual = 0;
3173 
3174 	if (IS_FWI2_CAPABLE(ha)) {
3175 		comp_status = le16_to_cpu(sts24->comp_status);
3176 		scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
3177 		state_flags = le16_to_cpu(sts24->state_flags);
3178 	} else {
3179 		comp_status = le16_to_cpu(sts->comp_status);
3180 		scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
3181 	}
3182 	handle = (uint32_t) LSW(sts->handle);
3183 	que = MSW(sts->handle);
3184 	req = ha->req_q_map[que];
3185 
3186 	/* Check for invalid queue pointer */
3187 	if (req == NULL ||
3188 	    que >= find_first_zero_bit(ha->req_qid_map, ha->max_req_queues)) {
3189 		ql_dbg(ql_dbg_io, vha, 0x3059,
3190 		    "Invalid status handle (0x%x): Bad req pointer. req=%p, "
3191 		    "que=%u.\n", sts->handle, req, que);
3192 		return;
3193 	}
3194 
3195 	/* Validate handle. */
3196 	if (handle < req->num_outstanding_cmds) {
3197 		sp = req->outstanding_cmds[handle];
3198 		if (!sp) {
3199 			ql_dbg(ql_dbg_io, vha, 0x3075,
3200 			    "%s(%ld): Already returned command for status handle (0x%x).\n",
3201 			    __func__, vha->host_no, sts->handle);
3202 			return;
3203 		}
3204 	} else {
3205 		ql_dbg(ql_dbg_io, vha, 0x3017,
3206 		    "Invalid status handle, out of range (0x%x).\n",
3207 		    sts->handle);
3208 
3209 		if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
3210 			if (IS_P3P_TYPE(ha))
3211 				set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3212 			else
3213 				set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3214 			qla2xxx_wake_dpc(vha);
3215 		}
3216 		return;
3217 	}
3218 	qla_put_fw_resources(sp->qpair, &sp->iores);
3219 
3220 	if (sp->cmd_type != TYPE_SRB) {
3221 		req->outstanding_cmds[handle] = NULL;
3222 		ql_dbg(ql_dbg_io, vha, 0x3015,
3223 		    "Unknown sp->cmd_type %x %p).\n",
3224 		    sp->cmd_type, sp);
3225 		return;
3226 	}
3227 
3228 	/* NVME completion. */
3229 	if (sp->type == SRB_NVME_CMD) {
3230 		req->outstanding_cmds[handle] = NULL;
3231 		qla24xx_nvme_iocb_entry(vha, req, pkt, sp);
3232 		return;
3233 	}
3234 
3235 	if (unlikely((state_flags & BIT_1) && (sp->type == SRB_BIDI_CMD))) {
3236 		qla25xx_process_bidir_status_iocb(vha, pkt, req, handle);
3237 		return;
3238 	}
3239 
3240 	/* Task Management completion. */
3241 	if (sp->type == SRB_TM_CMD) {
3242 		qla24xx_tm_iocb_entry(vha, req, pkt);
3243 		return;
3244 	}
3245 
3246 	/* Fast path completion. */
3247 	qla_chk_edif_rx_sa_delete_pending(vha, sp, sts24);
3248 	sp->qpair->cmd_completion_cnt++;
3249 
3250 	if (comp_status == CS_COMPLETE && scsi_status == 0) {
3251 		qla2x00_process_completed_request(vha, req, handle);
3252 
3253 		return;
3254 	}
3255 
3256 	cp = GET_CMD_SP(sp);
3257 	if (cp == NULL) {
3258 		ql_dbg(ql_dbg_io, vha, 0x3018,
3259 		    "Command already returned (0x%x/%p).\n",
3260 		    sts->handle, sp);
3261 
3262 		req->outstanding_cmds[handle] = NULL;
3263 		return;
3264 	}
3265 
3266 	lscsi_status = scsi_status & STATUS_MASK;
3267 
3268 	fcport = sp->fcport;
3269 
3270 	ox_id = 0;
3271 	sense_len = par_sense_len = rsp_info_len = resid_len =
3272 	    fw_resid_len = 0;
3273 	if (IS_FWI2_CAPABLE(ha)) {
3274 		if (scsi_status & SS_SENSE_LEN_VALID)
3275 			sense_len = le32_to_cpu(sts24->sense_len);
3276 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
3277 			rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
3278 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER))
3279 			resid_len = le32_to_cpu(sts24->rsp_residual_count);
3280 		if (comp_status == CS_DATA_UNDERRUN)
3281 			fw_resid_len = le32_to_cpu(sts24->residual_len);
3282 		rsp_info = sts24->data;
3283 		sense_data = sts24->data;
3284 		host_to_fcp_swap(sts24->data, sizeof(sts24->data));
3285 		ox_id = le16_to_cpu(sts24->ox_id);
3286 		par_sense_len = sizeof(sts24->data);
3287 		sts_qual = le16_to_cpu(sts24->status_qualifier);
3288 	} else {
3289 		if (scsi_status & SS_SENSE_LEN_VALID)
3290 			sense_len = le16_to_cpu(sts->req_sense_length);
3291 		if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
3292 			rsp_info_len = le16_to_cpu(sts->rsp_info_len);
3293 		resid_len = le32_to_cpu(sts->residual_length);
3294 		rsp_info = sts->rsp_info;
3295 		sense_data = sts->req_sense_data;
3296 		par_sense_len = sizeof(sts->req_sense_data);
3297 	}
3298 
3299 	/* Check for any FCP transport errors. */
3300 	if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
3301 		/* Sense data lies beyond any FCP RESPONSE data. */
3302 		if (IS_FWI2_CAPABLE(ha)) {
3303 			sense_data += rsp_info_len;
3304 			par_sense_len -= rsp_info_len;
3305 		}
3306 		if (rsp_info_len > 3 && rsp_info[3]) {
3307 			ql_dbg(ql_dbg_io, fcport->vha, 0x3019,
3308 			    "FCP I/O protocol failure (0x%x/0x%x).\n",
3309 			    rsp_info_len, rsp_info[3]);
3310 
3311 			res = DID_BUS_BUSY << 16;
3312 			goto out;
3313 		}
3314 	}
3315 
3316 	/* Check for overrun. */
3317 	if (IS_FWI2_CAPABLE(ha) && comp_status == CS_COMPLETE &&
3318 	    scsi_status & SS_RESIDUAL_OVER)
3319 		comp_status = CS_DATA_OVERRUN;
3320 
3321 	/*
3322 	 * Check retry_delay_timer value if we receive a busy or
3323 	 * queue full.
3324 	 */
3325 	if (unlikely(lscsi_status == SAM_STAT_TASK_SET_FULL ||
3326 		     lscsi_status == SAM_STAT_BUSY))
3327 		qla2x00_set_retry_delay_timestamp(fcport, sts_qual);
3328 
3329 	/*
3330 	 * Based on Host and scsi status generate status code for Linux
3331 	 */
3332 	switch (comp_status) {
3333 	case CS_COMPLETE:
3334 	case CS_QUEUE_FULL:
3335 		if (scsi_status == 0) {
3336 			res = DID_OK << 16;
3337 			break;
3338 		}
3339 		if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
3340 			resid = resid_len;
3341 			scsi_set_resid(cp, resid);
3342 
3343 			if (!lscsi_status &&
3344 			    ((unsigned)(scsi_bufflen(cp) - resid) <
3345 			     cp->underflow)) {
3346 				ql_dbg(ql_dbg_io, fcport->vha, 0x301a,
3347 				    "Mid-layer underflow detected (0x%x of 0x%x bytes).\n",
3348 				    resid, scsi_bufflen(cp));
3349 
3350 				res = DID_ERROR << 16;
3351 				break;
3352 			}
3353 		}
3354 		res = DID_OK << 16 | lscsi_status;
3355 
3356 		if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
3357 			ql_dbg(ql_dbg_io, fcport->vha, 0x301b,
3358 			    "QUEUE FULL detected.\n");
3359 			break;
3360 		}
3361 		logit = 0;
3362 		if (lscsi_status != SS_CHECK_CONDITION)
3363 			break;
3364 
3365 		memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
3366 		if (!(scsi_status & SS_SENSE_LEN_VALID))
3367 			break;
3368 
3369 		qla2x00_handle_sense(sp, sense_data, par_sense_len, sense_len,
3370 		    rsp, res);
3371 		break;
3372 
3373 	case CS_DATA_UNDERRUN:
3374 		/* Use F/W calculated residual length. */
3375 		resid = IS_FWI2_CAPABLE(ha) ? fw_resid_len : resid_len;
3376 		scsi_set_resid(cp, resid);
3377 		if (scsi_status & SS_RESIDUAL_UNDER) {
3378 			if (IS_FWI2_CAPABLE(ha) && fw_resid_len != resid_len) {
3379 				ql_log(ql_log_warn, fcport->vha, 0x301d,
3380 				       "Dropped frame(s) detected (0x%x of 0x%x bytes).\n",
3381 				       resid, scsi_bufflen(cp));
3382 
3383 				res = DID_ERROR << 16 | lscsi_status;
3384 				goto check_scsi_status;
3385 			}
3386 
3387 			if (!lscsi_status &&
3388 			    ((unsigned)(scsi_bufflen(cp) - resid) <
3389 			    cp->underflow)) {
3390 				ql_dbg(ql_dbg_io, fcport->vha, 0x301e,
3391 				    "Mid-layer underflow detected (0x%x of 0x%x bytes).\n",
3392 				    resid, scsi_bufflen(cp));
3393 
3394 				res = DID_ERROR << 16;
3395 				break;
3396 			}
3397 		} else if (lscsi_status != SAM_STAT_TASK_SET_FULL &&
3398 			    lscsi_status != SAM_STAT_BUSY) {
3399 			/*
3400 			 * scsi status of task set and busy are considered to be
3401 			 * task not completed.
3402 			 */
3403 
3404 			ql_log(ql_log_warn, fcport->vha, 0x301f,
3405 			       "Dropped frame(s) detected (0x%x of 0x%x bytes).\n",
3406 			       resid, scsi_bufflen(cp));
3407 
3408 			vha->interface_err_cnt++;
3409 
3410 			res = DID_ERROR << 16 | lscsi_status;
3411 			goto check_scsi_status;
3412 		} else {
3413 			ql_dbg(ql_dbg_io, fcport->vha, 0x3030,
3414 			    "scsi_status: 0x%x, lscsi_status: 0x%x\n",
3415 			    scsi_status, lscsi_status);
3416 		}
3417 
3418 		res = DID_OK << 16 | lscsi_status;
3419 		logit = 0;
3420 
3421 check_scsi_status:
3422 		/*
3423 		 * Check to see if SCSI Status is non zero. If so report SCSI
3424 		 * Status.
3425 		 */
3426 		if (lscsi_status != 0) {
3427 			if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
3428 				ql_dbg(ql_dbg_io, fcport->vha, 0x3020,
3429 				    "QUEUE FULL detected.\n");
3430 				logit = 1;
3431 				break;
3432 			}
3433 			if (lscsi_status != SS_CHECK_CONDITION)
3434 				break;
3435 
3436 			memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
3437 			if (!(scsi_status & SS_SENSE_LEN_VALID))
3438 				break;
3439 
3440 			qla2x00_handle_sense(sp, sense_data, par_sense_len,
3441 			    sense_len, rsp, res);
3442 		}
3443 		break;
3444 
3445 	case CS_PORT_LOGGED_OUT:
3446 	case CS_PORT_CONFIG_CHG:
3447 	case CS_PORT_BUSY:
3448 	case CS_INCOMPLETE:
3449 	case CS_PORT_UNAVAILABLE:
3450 	case CS_TIMEOUT:
3451 	case CS_RESET:
3452 	case CS_EDIF_INV_REQ:
3453 
3454 		/*
3455 		 * We are going to have the fc class block the rport
3456 		 * while we try to recover so instruct the mid layer
3457 		 * to requeue until the class decides how to handle this.
3458 		 */
3459 		res = DID_TRANSPORT_DISRUPTED << 16;
3460 
3461 		if (comp_status == CS_TIMEOUT) {
3462 			if (IS_FWI2_CAPABLE(ha))
3463 				break;
3464 			else if ((le16_to_cpu(sts->status_flags) &
3465 			    SF_LOGOUT_SENT) == 0)
3466 				break;
3467 		}
3468 
3469 		if (atomic_read(&fcport->state) == FCS_ONLINE) {
3470 			ql_dbg(ql_dbg_disc, fcport->vha, 0x3021,
3471 				"Port to be marked lost on fcport=%02x%02x%02x, current "
3472 				"port state= %s comp_status %x.\n", fcport->d_id.b.domain,
3473 				fcport->d_id.b.area, fcport->d_id.b.al_pa,
3474 				port_state_str[FCS_ONLINE],
3475 				comp_status);
3476 
3477 			qlt_schedule_sess_for_deletion(fcport);
3478 		}
3479 
3480 		break;
3481 
3482 	case CS_ABORTED:
3483 		res = DID_RESET << 16;
3484 		break;
3485 
3486 	case CS_DIF_ERROR:
3487 		logit = qla2x00_handle_dif_error(sp, sts24);
3488 		res = cp->result;
3489 		break;
3490 
3491 	case CS_TRANSPORT:
3492 		res = DID_ERROR << 16;
3493 		vha->hw_err_cnt++;
3494 
3495 		if (!IS_PI_SPLIT_DET_CAPABLE(ha))
3496 			break;
3497 
3498 		if (state_flags & BIT_4)
3499 			scmd_printk(KERN_WARNING, cp,
3500 			    "Unsupported device '%s' found.\n",
3501 			    cp->device->vendor);
3502 		break;
3503 
3504 	case CS_DMA:
3505 		ql_log(ql_log_info, fcport->vha, 0x3022,
3506 		    "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",
3507 		    comp_status, scsi_status, res, vha->host_no,
3508 		    cp->device->id, cp->device->lun, fcport->d_id.b24,
3509 		    ox_id, cp->cmnd, scsi_bufflen(cp), rsp_info_len,
3510 		    resid_len, fw_resid_len, sp, cp);
3511 		ql_dump_buffer(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe0ee,
3512 		    pkt, sizeof(*sts24));
3513 		res = DID_ERROR << 16;
3514 		vha->hw_err_cnt++;
3515 		break;
3516 	default:
3517 		res = DID_ERROR << 16;
3518 		break;
3519 	}
3520 
3521 out:
3522 	if (logit)
3523 		ql_dbg(ql_dbg_io, fcport->vha, 0x3022,
3524 		       "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",
3525 		       comp_status, scsi_status, res, vha->host_no,
3526 		       cp->device->id, cp->device->lun, fcport->d_id.b.domain,
3527 		       fcport->d_id.b.area, fcport->d_id.b.al_pa, ox_id,
3528 		       cp->cmnd, scsi_bufflen(cp), rsp_info_len,
3529 		       resid_len, fw_resid_len, sp, cp);
3530 
3531 	if (rsp->status_srb == NULL)
3532 		sp->done(sp, res);
3533 
3534 	/* for io's, clearing of outstanding_cmds[handle] means scsi_done was called */
3535 	req->outstanding_cmds[handle] = NULL;
3536 }
3537 
3538 /**
3539  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
3540  * @rsp: response queue
3541  * @pkt: Entry pointer
3542  *
3543  * Extended sense data.
3544  */
3545 static void
3546 qla2x00_status_cont_entry(struct rsp_que *rsp, sts_cont_entry_t *pkt)
3547 {
3548 	uint8_t	sense_sz = 0;
3549 	struct qla_hw_data *ha = rsp->hw;
3550 	struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
3551 	srb_t *sp = rsp->status_srb;
3552 	struct scsi_cmnd *cp;
3553 	uint32_t sense_len;
3554 	uint8_t *sense_ptr;
3555 
3556 	if (!sp || !GET_CMD_SENSE_LEN(sp))
3557 		return;
3558 
3559 	sense_len = GET_CMD_SENSE_LEN(sp);
3560 	sense_ptr = GET_CMD_SENSE_PTR(sp);
3561 
3562 	cp = GET_CMD_SP(sp);
3563 	if (cp == NULL) {
3564 		ql_log(ql_log_warn, vha, 0x3025,
3565 		    "cmd is NULL: already returned to OS (sp=%p).\n", sp);
3566 
3567 		rsp->status_srb = NULL;
3568 		return;
3569 	}
3570 
3571 	if (sense_len > sizeof(pkt->data))
3572 		sense_sz = sizeof(pkt->data);
3573 	else
3574 		sense_sz = sense_len;
3575 
3576 	/* Move sense data. */
3577 	if (IS_FWI2_CAPABLE(ha))
3578 		host_to_fcp_swap(pkt->data, sizeof(pkt->data));
3579 	memcpy(sense_ptr, pkt->data, sense_sz);
3580 	ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302c,
3581 		sense_ptr, sense_sz);
3582 
3583 	sense_len -= sense_sz;
3584 	sense_ptr += sense_sz;
3585 
3586 	SET_CMD_SENSE_PTR(sp, sense_ptr);
3587 	SET_CMD_SENSE_LEN(sp, sense_len);
3588 
3589 	/* Place command on done queue. */
3590 	if (sense_len == 0) {
3591 		rsp->status_srb = NULL;
3592 		sp->done(sp, cp->result);
3593 	}
3594 }
3595 
3596 /**
3597  * qla2x00_error_entry() - Process an error entry.
3598  * @vha: SCSI driver HA context
3599  * @rsp: response queue
3600  * @pkt: Entry pointer
3601  * return : 1=allow further error analysis. 0=no additional error analysis.
3602  */
3603 static int
3604 qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
3605 {
3606 	srb_t *sp;
3607 	struct qla_hw_data *ha = vha->hw;
3608 	const char func[] = "ERROR-IOCB";
3609 	uint16_t que = MSW(pkt->handle);
3610 	struct req_que *req = NULL;
3611 	int res = DID_ERROR << 16;
3612 	u16 index;
3613 
3614 	ql_dbg(ql_dbg_async, vha, 0x502a,
3615 	    "iocb type %xh with error status %xh, handle %xh, rspq id %d\n",
3616 	    pkt->entry_type, pkt->entry_status, pkt->handle, rsp->id);
3617 
3618 	if (que >= ha->max_req_queues || !ha->req_q_map[que])
3619 		goto fatal;
3620 
3621 	req = ha->req_q_map[que];
3622 
3623 	if (pkt->entry_status & RF_BUSY)
3624 		res = DID_BUS_BUSY << 16;
3625 
3626 	if ((pkt->handle & ~QLA_TGT_HANDLE_MASK) == QLA_TGT_SKIP_HANDLE)
3627 		return 0;
3628 
3629 	switch (pkt->entry_type) {
3630 	case NOTIFY_ACK_TYPE:
3631 	case STATUS_CONT_TYPE:
3632 	case LOGINOUT_PORT_IOCB_TYPE:
3633 	case CT_IOCB_TYPE:
3634 	case ELS_IOCB_TYPE:
3635 	case ABORT_IOCB_TYPE:
3636 	case MBX_IOCB_TYPE:
3637 	default:
3638 		sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
3639 		if (sp) {
3640 			sp->done(sp, res);
3641 			return 0;
3642 		}
3643 		break;
3644 
3645 	case SA_UPDATE_IOCB_TYPE:
3646 	case ABTS_RESP_24XX:
3647 	case CTIO_TYPE7:
3648 	case CTIO_CRC2:
3649 		return 1;
3650 	case STATUS_TYPE:
3651 		sp = qla_get_sp_from_handle(vha, func, req, pkt, &index);
3652 		if (sp) {
3653 			sp->done(sp, res);
3654 			req->outstanding_cmds[index] = NULL;
3655 			return 0;
3656 		}
3657 		break;
3658 	}
3659 fatal:
3660 	ql_log(ql_log_warn, vha, 0x5030,
3661 	    "Error entry - invalid handle/queue (%04x).\n", que);
3662 	return 0;
3663 }
3664 
3665 /**
3666  * qla24xx_mbx_completion() - Process mailbox command completions.
3667  * @vha: SCSI driver HA context
3668  * @mb0: Mailbox0 register
3669  */
3670 static void
3671 qla24xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
3672 {
3673 	uint16_t	cnt;
3674 	uint32_t	mboxes;
3675 	__le16 __iomem *wptr;
3676 	struct qla_hw_data *ha = vha->hw;
3677 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
3678 
3679 	/* Read all mbox registers? */
3680 	WARN_ON_ONCE(ha->mbx_count > 32);
3681 	mboxes = (1ULL << ha->mbx_count) - 1;
3682 	if (!ha->mcp)
3683 		ql_dbg(ql_dbg_async, vha, 0x504e, "MBX pointer ERROR.\n");
3684 	else
3685 		mboxes = ha->mcp->in_mb;
3686 
3687 	/* Load return mailbox registers. */
3688 	ha->flags.mbox_int = 1;
3689 	ha->mailbox_out[0] = mb0;
3690 	mboxes >>= 1;
3691 	wptr = &reg->mailbox1;
3692 
3693 	for (cnt = 1; cnt < ha->mbx_count; cnt++) {
3694 		if (mboxes & BIT_0)
3695 			ha->mailbox_out[cnt] = rd_reg_word(wptr);
3696 
3697 		mboxes >>= 1;
3698 		wptr++;
3699 	}
3700 }
3701 
3702 static void
3703 qla24xx_abort_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
3704 	struct abort_entry_24xx *pkt)
3705 {
3706 	const char func[] = "ABT_IOCB";
3707 	srb_t *sp;
3708 	srb_t *orig_sp = NULL;
3709 	struct srb_iocb *abt;
3710 
3711 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
3712 	if (!sp)
3713 		return;
3714 
3715 	abt = &sp->u.iocb_cmd;
3716 	abt->u.abt.comp_status = pkt->comp_status;
3717 	orig_sp = sp->cmd_sp;
3718 	/* Need to pass original sp */
3719 	if (orig_sp)
3720 		qla_nvme_abort_process_comp_status(pkt, orig_sp);
3721 
3722 	sp->done(sp, 0);
3723 }
3724 
3725 void qla24xx_nvme_ls4_iocb(struct scsi_qla_host *vha,
3726     struct pt_ls4_request *pkt, struct req_que *req)
3727 {
3728 	srb_t *sp;
3729 	const char func[] = "LS4_IOCB";
3730 	uint16_t comp_status;
3731 
3732 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
3733 	if (!sp)
3734 		return;
3735 
3736 	comp_status = le16_to_cpu(pkt->status);
3737 	sp->done(sp, comp_status);
3738 }
3739 
3740 /**
3741  * qla_chk_cont_iocb_avail - check for all continuation iocbs are available
3742  *   before iocb processing can start.
3743  * @vha: host adapter pointer
3744  * @rsp: respond queue
3745  * @pkt: head iocb describing how many continuation iocb
3746  * Return: 0 all iocbs has arrived, xx- all iocbs have not arrived.
3747  */
3748 static int qla_chk_cont_iocb_avail(struct scsi_qla_host *vha,
3749 	struct rsp_que *rsp, response_t *pkt, u32 rsp_q_in)
3750 {
3751 	int start_pkt_ring_index;
3752 	u32 iocb_cnt = 0;
3753 	int rc = 0;
3754 
3755 	if (pkt->entry_count == 1)
3756 		return rc;
3757 
3758 	/* ring_index was pre-increment. set it back to current pkt */
3759 	if (rsp->ring_index == 0)
3760 		start_pkt_ring_index = rsp->length - 1;
3761 	else
3762 		start_pkt_ring_index = rsp->ring_index - 1;
3763 
3764 	if (rsp_q_in < start_pkt_ring_index)
3765 		/* q in ptr is wrapped */
3766 		iocb_cnt = rsp->length - start_pkt_ring_index + rsp_q_in;
3767 	else
3768 		iocb_cnt = rsp_q_in - start_pkt_ring_index;
3769 
3770 	if (iocb_cnt < pkt->entry_count)
3771 		rc = -EIO;
3772 
3773 	ql_dbg(ql_dbg_init, vha, 0x5091,
3774 	       "%s - ring %p pkt %p entry count %d iocb_cnt %d rsp_q_in %d rc %d\n",
3775 	       __func__, rsp->ring, pkt, pkt->entry_count, iocb_cnt, rsp_q_in, rc);
3776 
3777 	return rc;
3778 }
3779 
3780 static void qla_marker_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
3781 	struct mrk_entry_24xx *pkt)
3782 {
3783 	const char func[] = "MRK-IOCB";
3784 	srb_t *sp;
3785 	int res = QLA_SUCCESS;
3786 
3787 	if (!IS_FWI2_CAPABLE(vha->hw))
3788 		return;
3789 
3790 	sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
3791 	if (!sp)
3792 		return;
3793 
3794 	if (pkt->entry_status) {
3795 		ql_dbg(ql_dbg_taskm, vha, 0x8025, "marker failure.\n");
3796 		res = QLA_COMMAND_ERROR;
3797 	}
3798 	sp->u.iocb_cmd.u.tmf.data = res;
3799 	sp->done(sp, res);
3800 }
3801 
3802 /**
3803  * qla24xx_process_response_queue() - Process response queue entries.
3804  * @vha: SCSI driver HA context
3805  * @rsp: response queue
3806  */
3807 void qla24xx_process_response_queue(struct scsi_qla_host *vha,
3808 	struct rsp_que *rsp)
3809 {
3810 	struct sts_entry_24xx *pkt;
3811 	struct qla_hw_data *ha = vha->hw;
3812 	struct purex_entry_24xx *purex_entry;
3813 	struct purex_item *pure_item;
3814 	u16 rsp_in = 0, cur_ring_index;
3815 	int is_shadow_hba;
3816 
3817 	if (!ha->flags.fw_started)
3818 		return;
3819 
3820 	if (rsp->qpair->cpuid != smp_processor_id() || !rsp->qpair->rcv_intr) {
3821 		rsp->qpair->rcv_intr = 1;
3822 
3823 		if (!rsp->qpair->cpu_mapped)
3824 			qla_cpu_update(rsp->qpair, raw_smp_processor_id());
3825 	}
3826 
3827 #define __update_rsp_in(_is_shadow_hba, _rsp, _rsp_in)			\
3828 	do {								\
3829 		_rsp_in = _is_shadow_hba ? *(_rsp)->in_ptr :		\
3830 				rd_reg_dword_relaxed((_rsp)->rsp_q_in);	\
3831 	} while (0)
3832 
3833 	is_shadow_hba = IS_SHADOW_REG_CAPABLE(ha);
3834 
3835 	__update_rsp_in(is_shadow_hba, rsp, rsp_in);
3836 
3837 	while (rsp->ring_index != rsp_in &&
3838 		       rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
3839 		pkt = (struct sts_entry_24xx *)rsp->ring_ptr;
3840 		cur_ring_index = rsp->ring_index;
3841 
3842 		rsp->ring_index++;
3843 		if (rsp->ring_index == rsp->length) {
3844 			rsp->ring_index = 0;
3845 			rsp->ring_ptr = rsp->ring;
3846 		} else {
3847 			rsp->ring_ptr++;
3848 		}
3849 
3850 		if (pkt->entry_status != 0) {
3851 			if (qla2x00_error_entry(vha, rsp, (sts_entry_t *) pkt))
3852 				goto process_err;
3853 
3854 			((response_t *)pkt)->signature = RESPONSE_PROCESSED;
3855 			wmb();
3856 			continue;
3857 		}
3858 process_err:
3859 
3860 		switch (pkt->entry_type) {
3861 		case STATUS_TYPE:
3862 			qla2x00_status_entry(vha, rsp, pkt);
3863 			break;
3864 		case STATUS_CONT_TYPE:
3865 			qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
3866 			break;
3867 		case VP_RPT_ID_IOCB_TYPE:
3868 			qla24xx_report_id_acquisition(vha,
3869 			    (struct vp_rpt_id_entry_24xx *)pkt);
3870 			break;
3871 		case LOGINOUT_PORT_IOCB_TYPE:
3872 			qla24xx_logio_entry(vha, rsp->req,
3873 			    (struct logio_entry_24xx *)pkt);
3874 			break;
3875 		case CT_IOCB_TYPE:
3876 			qla24xx_els_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
3877 			break;
3878 		case ELS_IOCB_TYPE:
3879 			qla24xx_els_ct_entry(vha, rsp->req, pkt, ELS_IOCB_TYPE);
3880 			break;
3881 		case ABTS_RECV_24XX:
3882 			if (qla_ini_mode_enabled(vha)) {
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_abts);
3888 				break;
3889 			}
3890 			if (IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3891 			    IS_QLA28XX(ha)) {
3892 				/* ensure that the ATIO queue is empty */
3893 				qlt_handle_abts_recv(vha, rsp,
3894 				    (response_t *)pkt);
3895 				break;
3896 			} else {
3897 				qlt_24xx_process_atio_queue(vha, 1);
3898 			}
3899 			fallthrough;
3900 		case ABTS_RESP_24XX:
3901 		case CTIO_TYPE7:
3902 		case CTIO_CRC2:
3903 			qlt_response_pkt_all_vps(vha, rsp, (response_t *)pkt);
3904 			break;
3905 		case PT_LS4_REQUEST:
3906 			qla24xx_nvme_ls4_iocb(vha, (struct pt_ls4_request *)pkt,
3907 			    rsp->req);
3908 			break;
3909 		case NOTIFY_ACK_TYPE:
3910 			if (pkt->handle == QLA_TGT_SKIP_HANDLE)
3911 				qlt_response_pkt_all_vps(vha, rsp,
3912 				    (response_t *)pkt);
3913 			else
3914 				qla24xxx_nack_iocb_entry(vha, rsp->req,
3915 					(struct nack_to_isp *)pkt);
3916 			break;
3917 		case MARKER_TYPE:
3918 			qla_marker_iocb_entry(vha, rsp->req, (struct mrk_entry_24xx *)pkt);
3919 			break;
3920 		case ABORT_IOCB_TYPE:
3921 			qla24xx_abort_iocb_entry(vha, rsp->req,
3922 			    (struct abort_entry_24xx *)pkt);
3923 			break;
3924 		case MBX_IOCB_TYPE:
3925 			qla24xx_mbx_iocb_entry(vha, rsp->req,
3926 			    (struct mbx_24xx_entry *)pkt);
3927 			break;
3928 		case VP_CTRL_IOCB_TYPE:
3929 			qla_ctrlvp_completed(vha, rsp->req,
3930 			    (struct vp_ctrl_entry_24xx *)pkt);
3931 			break;
3932 		case PUREX_IOCB_TYPE:
3933 			purex_entry = (void *)pkt;
3934 			switch (purex_entry->els_frame_payload[3]) {
3935 			case ELS_RDP:
3936 				pure_item = qla24xx_copy_std_pkt(vha, pkt);
3937 				if (!pure_item)
3938 					break;
3939 				qla24xx_queue_purex_item(vha, pure_item,
3940 						 qla24xx_process_purex_rdp);
3941 				break;
3942 			case ELS_FPIN:
3943 				if (!vha->hw->flags.scm_enabled) {
3944 					ql_log(ql_log_warn, vha, 0x5094,
3945 					       "SCM not active for this port\n");
3946 					break;
3947 				}
3948 				pure_item = qla27xx_copy_fpin_pkt(vha,
3949 							  (void **)&pkt, &rsp);
3950 				__update_rsp_in(is_shadow_hba, rsp, rsp_in);
3951 				if (!pure_item)
3952 					break;
3953 				qla24xx_queue_purex_item(vha, pure_item,
3954 						 qla27xx_process_purex_fpin);
3955 				break;
3956 
3957 			case ELS_AUTH_ELS:
3958 				if (qla_chk_cont_iocb_avail(vha, rsp, (response_t *)pkt, rsp_in)) {
3959 					/*
3960 					 * ring_ptr and ring_index were
3961 					 * pre-incremented above. Reset them
3962 					 * back to current. Wait for next
3963 					 * interrupt with all IOCBs to arrive
3964 					 * and re-process.
3965 					 */
3966 					rsp->ring_ptr = (response_t *)pkt;
3967 					rsp->ring_index = cur_ring_index;
3968 
3969 					ql_dbg(ql_dbg_init, vha, 0x5091,
3970 					    "Defer processing ELS opcode %#x...\n",
3971 					    purex_entry->els_frame_payload[3]);
3972 					return;
3973 				}
3974 				qla24xx_auth_els(vha, (void **)&pkt, &rsp);
3975 				break;
3976 			default:
3977 				ql_log(ql_log_warn, vha, 0x509c,
3978 				       "Discarding ELS Request opcode 0x%x\n",
3979 				       purex_entry->els_frame_payload[3]);
3980 			}
3981 			break;
3982 		case SA_UPDATE_IOCB_TYPE:
3983 			qla28xx_sa_update_iocb_entry(vha, rsp->req,
3984 				(struct sa_update_28xx *)pkt);
3985 			break;
3986 
3987 		default:
3988 			/* Type Not Supported. */
3989 			ql_dbg(ql_dbg_async, vha, 0x5042,
3990 			       "Received unknown response pkt type 0x%x entry status=%x.\n",
3991 			       pkt->entry_type, pkt->entry_status);
3992 			break;
3993 		}
3994 		((response_t *)pkt)->signature = RESPONSE_PROCESSED;
3995 		wmb();
3996 	}
3997 
3998 	/* Adjust ring index */
3999 	if (IS_P3P_TYPE(ha)) {
4000 		struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
4001 
4002 		wrt_reg_dword(&reg->rsp_q_out[0], rsp->ring_index);
4003 	} else {
4004 		wrt_reg_dword(rsp->rsp_q_out, rsp->ring_index);
4005 	}
4006 }
4007 
4008 static void
4009 qla2xxx_check_risc_status(scsi_qla_host_t *vha)
4010 {
4011 	int rval;
4012 	uint32_t cnt;
4013 	struct qla_hw_data *ha = vha->hw;
4014 	struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
4015 
4016 	if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
4017 	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
4018 		return;
4019 
4020 	rval = QLA_SUCCESS;
4021 	wrt_reg_dword(&reg->iobase_addr, 0x7C00);
4022 	rd_reg_dword(&reg->iobase_addr);
4023 	wrt_reg_dword(&reg->iobase_window, 0x0001);
4024 	for (cnt = 10000; (rd_reg_dword(&reg->iobase_window) & BIT_0) == 0 &&
4025 	    rval == QLA_SUCCESS; cnt--) {
4026 		if (cnt) {
4027 			wrt_reg_dword(&reg->iobase_window, 0x0001);
4028 			udelay(10);
4029 		} else
4030 			rval = QLA_FUNCTION_TIMEOUT;
4031 	}
4032 	if (rval == QLA_SUCCESS)
4033 		goto next_test;
4034 
4035 	rval = QLA_SUCCESS;
4036 	wrt_reg_dword(&reg->iobase_window, 0x0003);
4037 	for (cnt = 100; (rd_reg_dword(&reg->iobase_window) & BIT_0) == 0 &&
4038 	    rval == QLA_SUCCESS; cnt--) {
4039 		if (cnt) {
4040 			wrt_reg_dword(&reg->iobase_window, 0x0003);
4041 			udelay(10);
4042 		} else
4043 			rval = QLA_FUNCTION_TIMEOUT;
4044 	}
4045 	if (rval != QLA_SUCCESS)
4046 		goto done;
4047 
4048 next_test:
4049 	if (rd_reg_dword(&reg->iobase_c8) & BIT_3)
4050 		ql_log(ql_log_info, vha, 0x504c,
4051 		    "Additional code -- 0x55AA.\n");
4052 
4053 done:
4054 	wrt_reg_dword(&reg->iobase_window, 0x0000);
4055 	rd_reg_dword(&reg->iobase_window);
4056 }
4057 
4058 /**
4059  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP24xx.
4060  * @irq: interrupt number
4061  * @dev_id: SCSI driver HA context
4062  *
4063  * Called by system whenever the host adapter generates an interrupt.
4064  *
4065  * Returns handled flag.
4066  */
4067 irqreturn_t
4068 qla24xx_intr_handler(int irq, void *dev_id)
4069 {
4070 	scsi_qla_host_t	*vha;
4071 	struct qla_hw_data *ha;
4072 	struct device_reg_24xx __iomem *reg;
4073 	int		status;
4074 	unsigned long	iter;
4075 	uint32_t	stat;
4076 	uint32_t	hccr;
4077 	uint16_t	mb[8];
4078 	struct rsp_que *rsp;
4079 	unsigned long	flags;
4080 	bool process_atio = false;
4081 
4082 	rsp = (struct rsp_que *) dev_id;
4083 	if (!rsp) {
4084 		ql_log(ql_log_info, NULL, 0x5059,
4085 		    "%s: NULL response queue pointer.\n", __func__);
4086 		return IRQ_NONE;
4087 	}
4088 
4089 	ha = rsp->hw;
4090 	reg = &ha->iobase->isp24;
4091 	status = 0;
4092 
4093 	if (unlikely(pci_channel_offline(ha->pdev)))
4094 		return IRQ_HANDLED;
4095 
4096 	spin_lock_irqsave(&ha->hardware_lock, flags);
4097 	vha = pci_get_drvdata(ha->pdev);
4098 	for (iter = 50; iter--; ) {
4099 		stat = rd_reg_dword(&reg->host_status);
4100 		if (qla2x00_check_reg32_for_disconnect(vha, stat))
4101 			break;
4102 		if (stat & HSRX_RISC_PAUSED) {
4103 			if (unlikely(pci_channel_offline(ha->pdev)))
4104 				break;
4105 
4106 			hccr = rd_reg_dword(&reg->hccr);
4107 
4108 			ql_log(ql_log_warn, vha, 0x504b,
4109 			    "RISC paused -- HCCR=%x, Dumping firmware.\n",
4110 			    hccr);
4111 
4112 			qla2xxx_check_risc_status(vha);
4113 
4114 			ha->isp_ops->fw_dump(vha);
4115 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4116 			break;
4117 		} else if ((stat & HSRX_RISC_INT) == 0)
4118 			break;
4119 
4120 		switch (stat & 0xff) {
4121 		case INTR_ROM_MB_SUCCESS:
4122 		case INTR_ROM_MB_FAILED:
4123 		case INTR_MB_SUCCESS:
4124 		case INTR_MB_FAILED:
4125 			qla24xx_mbx_completion(vha, MSW(stat));
4126 			status |= MBX_INTERRUPT;
4127 
4128 			break;
4129 		case INTR_ASYNC_EVENT:
4130 			mb[0] = MSW(stat);
4131 			mb[1] = rd_reg_word(&reg->mailbox1);
4132 			mb[2] = rd_reg_word(&reg->mailbox2);
4133 			mb[3] = rd_reg_word(&reg->mailbox3);
4134 			qla2x00_async_event(vha, rsp, mb);
4135 			break;
4136 		case INTR_RSP_QUE_UPDATE:
4137 		case INTR_RSP_QUE_UPDATE_83XX:
4138 			qla24xx_process_response_queue(vha, rsp);
4139 			break;
4140 		case INTR_ATIO_QUE_UPDATE_27XX:
4141 		case INTR_ATIO_QUE_UPDATE:
4142 			process_atio = true;
4143 			break;
4144 		case INTR_ATIO_RSP_QUE_UPDATE:
4145 			process_atio = true;
4146 			qla24xx_process_response_queue(vha, rsp);
4147 			break;
4148 		default:
4149 			ql_dbg(ql_dbg_async, vha, 0x504f,
4150 			    "Unrecognized interrupt type (%d).\n", stat * 0xff);
4151 			break;
4152 		}
4153 		wrt_reg_dword(&reg->hccr, HCCRX_CLR_RISC_INT);
4154 		rd_reg_dword_relaxed(&reg->hccr);
4155 		if (unlikely(IS_QLA83XX(ha) && (ha->pdev->revision == 1)))
4156 			ndelay(3500);
4157 	}
4158 	qla2x00_handle_mbx_completion(ha, status);
4159 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4160 
4161 	if (process_atio) {
4162 		spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4163 		qlt_24xx_process_atio_queue(vha, 0);
4164 		spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4165 	}
4166 
4167 	return IRQ_HANDLED;
4168 }
4169 
4170 static irqreturn_t
4171 qla24xx_msix_rsp_q(int irq, void *dev_id)
4172 {
4173 	struct qla_hw_data *ha;
4174 	struct rsp_que *rsp;
4175 	struct device_reg_24xx __iomem *reg;
4176 	struct scsi_qla_host *vha;
4177 	unsigned long flags;
4178 
4179 	rsp = (struct rsp_que *) dev_id;
4180 	if (!rsp) {
4181 		ql_log(ql_log_info, NULL, 0x505a,
4182 		    "%s: NULL response queue pointer.\n", __func__);
4183 		return IRQ_NONE;
4184 	}
4185 	ha = rsp->hw;
4186 	reg = &ha->iobase->isp24;
4187 
4188 	spin_lock_irqsave(&ha->hardware_lock, flags);
4189 
4190 	vha = pci_get_drvdata(ha->pdev);
4191 	qla24xx_process_response_queue(vha, rsp);
4192 	if (!ha->flags.disable_msix_handshake) {
4193 		wrt_reg_dword(&reg->hccr, HCCRX_CLR_RISC_INT);
4194 		rd_reg_dword_relaxed(&reg->hccr);
4195 	}
4196 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4197 
4198 	return IRQ_HANDLED;
4199 }
4200 
4201 static irqreturn_t
4202 qla24xx_msix_default(int irq, void *dev_id)
4203 {
4204 	scsi_qla_host_t	*vha;
4205 	struct qla_hw_data *ha;
4206 	struct rsp_que *rsp;
4207 	struct device_reg_24xx __iomem *reg;
4208 	int		status;
4209 	uint32_t	stat;
4210 	uint32_t	hccr;
4211 	uint16_t	mb[8];
4212 	unsigned long flags;
4213 	bool process_atio = false;
4214 
4215 	rsp = (struct rsp_que *) dev_id;
4216 	if (!rsp) {
4217 		ql_log(ql_log_info, NULL, 0x505c,
4218 		    "%s: NULL response queue pointer.\n", __func__);
4219 		return IRQ_NONE;
4220 	}
4221 	ha = rsp->hw;
4222 	reg = &ha->iobase->isp24;
4223 	status = 0;
4224 
4225 	spin_lock_irqsave(&ha->hardware_lock, flags);
4226 	vha = pci_get_drvdata(ha->pdev);
4227 	do {
4228 		stat = rd_reg_dword(&reg->host_status);
4229 		if (qla2x00_check_reg32_for_disconnect(vha, stat))
4230 			break;
4231 		if (stat & HSRX_RISC_PAUSED) {
4232 			if (unlikely(pci_channel_offline(ha->pdev)))
4233 				break;
4234 
4235 			hccr = rd_reg_dword(&reg->hccr);
4236 
4237 			ql_log(ql_log_info, vha, 0x5050,
4238 			    "RISC paused -- HCCR=%x, Dumping firmware.\n",
4239 			    hccr);
4240 
4241 			qla2xxx_check_risc_status(vha);
4242 			vha->hw_err_cnt++;
4243 
4244 			ha->isp_ops->fw_dump(vha);
4245 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4246 			break;
4247 		} else if ((stat & HSRX_RISC_INT) == 0)
4248 			break;
4249 
4250 		switch (stat & 0xff) {
4251 		case INTR_ROM_MB_SUCCESS:
4252 		case INTR_ROM_MB_FAILED:
4253 		case INTR_MB_SUCCESS:
4254 		case INTR_MB_FAILED:
4255 			qla24xx_mbx_completion(vha, MSW(stat));
4256 			status |= MBX_INTERRUPT;
4257 
4258 			break;
4259 		case INTR_ASYNC_EVENT:
4260 			mb[0] = MSW(stat);
4261 			mb[1] = rd_reg_word(&reg->mailbox1);
4262 			mb[2] = rd_reg_word(&reg->mailbox2);
4263 			mb[3] = rd_reg_word(&reg->mailbox3);
4264 			qla2x00_async_event(vha, rsp, mb);
4265 			break;
4266 		case INTR_RSP_QUE_UPDATE:
4267 		case INTR_RSP_QUE_UPDATE_83XX:
4268 			qla24xx_process_response_queue(vha, rsp);
4269 			break;
4270 		case INTR_ATIO_QUE_UPDATE_27XX:
4271 		case INTR_ATIO_QUE_UPDATE:
4272 			process_atio = true;
4273 			break;
4274 		case INTR_ATIO_RSP_QUE_UPDATE:
4275 			process_atio = true;
4276 			qla24xx_process_response_queue(vha, rsp);
4277 			break;
4278 		default:
4279 			ql_dbg(ql_dbg_async, vha, 0x5051,
4280 			    "Unrecognized interrupt type (%d).\n", stat & 0xff);
4281 			break;
4282 		}
4283 		wrt_reg_dword(&reg->hccr, HCCRX_CLR_RISC_INT);
4284 	} while (0);
4285 	qla2x00_handle_mbx_completion(ha, status);
4286 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4287 
4288 	if (process_atio) {
4289 		spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4290 		qlt_24xx_process_atio_queue(vha, 0);
4291 		spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4292 	}
4293 
4294 	return IRQ_HANDLED;
4295 }
4296 
4297 irqreturn_t
4298 qla2xxx_msix_rsp_q(int irq, void *dev_id)
4299 {
4300 	struct qla_hw_data *ha;
4301 	struct qla_qpair *qpair;
4302 
4303 	qpair = dev_id;
4304 	if (!qpair) {
4305 		ql_log(ql_log_info, NULL, 0x505b,
4306 		    "%s: NULL response queue pointer.\n", __func__);
4307 		return IRQ_NONE;
4308 	}
4309 	ha = qpair->hw;
4310 
4311 	queue_work_on(smp_processor_id(), ha->wq, &qpair->q_work);
4312 
4313 	return IRQ_HANDLED;
4314 }
4315 
4316 irqreturn_t
4317 qla2xxx_msix_rsp_q_hs(int irq, void *dev_id)
4318 {
4319 	struct qla_hw_data *ha;
4320 	struct qla_qpair *qpair;
4321 	struct device_reg_24xx __iomem *reg;
4322 	unsigned long flags;
4323 
4324 	qpair = dev_id;
4325 	if (!qpair) {
4326 		ql_log(ql_log_info, NULL, 0x505b,
4327 		    "%s: NULL response queue pointer.\n", __func__);
4328 		return IRQ_NONE;
4329 	}
4330 	ha = qpair->hw;
4331 
4332 	reg = &ha->iobase->isp24;
4333 	spin_lock_irqsave(&ha->hardware_lock, flags);
4334 	wrt_reg_dword(&reg->hccr, HCCRX_CLR_RISC_INT);
4335 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4336 
4337 	queue_work_on(smp_processor_id(), ha->wq, &qpair->q_work);
4338 
4339 	return IRQ_HANDLED;
4340 }
4341 
4342 /* Interrupt handling helpers. */
4343 
4344 struct qla_init_msix_entry {
4345 	const char *name;
4346 	irq_handler_t handler;
4347 };
4348 
4349 static const struct qla_init_msix_entry msix_entries[] = {
4350 	{ "default", qla24xx_msix_default },
4351 	{ "rsp_q", qla24xx_msix_rsp_q },
4352 	{ "atio_q", qla83xx_msix_atio_q },
4353 	{ "qpair_multiq", qla2xxx_msix_rsp_q },
4354 	{ "qpair_multiq_hs", qla2xxx_msix_rsp_q_hs },
4355 };
4356 
4357 static const struct qla_init_msix_entry qla82xx_msix_entries[] = {
4358 	{ "qla2xxx (default)", qla82xx_msix_default },
4359 	{ "qla2xxx (rsp_q)", qla82xx_msix_rsp_q },
4360 };
4361 
4362 static int
4363 qla24xx_enable_msix(struct qla_hw_data *ha, struct rsp_que *rsp)
4364 {
4365 	int i, ret;
4366 	struct qla_msix_entry *qentry;
4367 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
4368 	int min_vecs = QLA_BASE_VECTORS;
4369 	struct irq_affinity desc = {
4370 		.pre_vectors = QLA_BASE_VECTORS,
4371 	};
4372 
4373 	if (QLA_TGT_MODE_ENABLED() && (ql2xenablemsix != 0) &&
4374 	    IS_ATIO_MSIX_CAPABLE(ha)) {
4375 		desc.pre_vectors++;
4376 		min_vecs++;
4377 	}
4378 
4379 	if (USER_CTRL_IRQ(ha) || !ha->mqiobase) {
4380 		/* user wants to control IRQ setting for target mode */
4381 		ret = pci_alloc_irq_vectors(ha->pdev, min_vecs,
4382 		    min((u16)ha->msix_count, (u16)(num_online_cpus() + min_vecs)),
4383 		    PCI_IRQ_MSIX);
4384 	} else
4385 		ret = pci_alloc_irq_vectors_affinity(ha->pdev, min_vecs,
4386 		    min((u16)ha->msix_count, (u16)(num_online_cpus() + min_vecs)),
4387 		    PCI_IRQ_MSIX | PCI_IRQ_AFFINITY,
4388 		    &desc);
4389 
4390 	if (ret < 0) {
4391 		ql_log(ql_log_fatal, vha, 0x00c7,
4392 		    "MSI-X: Failed to enable support, "
4393 		    "giving   up -- %d/%d.\n",
4394 		    ha->msix_count, ret);
4395 		goto msix_out;
4396 	} else if (ret < ha->msix_count) {
4397 		ql_log(ql_log_info, vha, 0x00c6,
4398 		    "MSI-X: Using %d vectors\n", ret);
4399 		ha->msix_count = ret;
4400 		/* Recalculate queue values */
4401 		if (ha->mqiobase && (ql2xmqsupport || ql2xnvmeenable)) {
4402 			ha->max_req_queues = ha->msix_count - 1;
4403 
4404 			/* ATIOQ needs 1 vector. That's 1 less QPair */
4405 			if (QLA_TGT_MODE_ENABLED())
4406 				ha->max_req_queues--;
4407 
4408 			ha->max_rsp_queues = ha->max_req_queues;
4409 
4410 			ha->max_qpairs = ha->max_req_queues - 1;
4411 			ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0190,
4412 			    "Adjusted Max no of queues pairs: %d.\n", ha->max_qpairs);
4413 		}
4414 	}
4415 	vha->irq_offset = desc.pre_vectors;
4416 	ha->msix_entries = kcalloc(ha->msix_count,
4417 				   sizeof(struct qla_msix_entry),
4418 				   GFP_KERNEL);
4419 	if (!ha->msix_entries) {
4420 		ql_log(ql_log_fatal, vha, 0x00c8,
4421 		    "Failed to allocate memory for ha->msix_entries.\n");
4422 		ret = -ENOMEM;
4423 		goto free_irqs;
4424 	}
4425 	ha->flags.msix_enabled = 1;
4426 
4427 	for (i = 0; i < ha->msix_count; i++) {
4428 		qentry = &ha->msix_entries[i];
4429 		qentry->vector = pci_irq_vector(ha->pdev, i);
4430 		qentry->vector_base0 = i;
4431 		qentry->entry = i;
4432 		qentry->have_irq = 0;
4433 		qentry->in_use = 0;
4434 		qentry->handle = NULL;
4435 	}
4436 
4437 	/* Enable MSI-X vectors for the base queue */
4438 	for (i = 0; i < QLA_BASE_VECTORS; i++) {
4439 		qentry = &ha->msix_entries[i];
4440 		qentry->handle = rsp;
4441 		rsp->msix = qentry;
4442 		scnprintf(qentry->name, sizeof(qentry->name),
4443 		    "qla2xxx%lu_%s", vha->host_no, msix_entries[i].name);
4444 		if (IS_P3P_TYPE(ha))
4445 			ret = request_irq(qentry->vector,
4446 				qla82xx_msix_entries[i].handler,
4447 				0, qla82xx_msix_entries[i].name, rsp);
4448 		else
4449 			ret = request_irq(qentry->vector,
4450 				msix_entries[i].handler,
4451 				0, qentry->name, rsp);
4452 		if (ret)
4453 			goto msix_register_fail;
4454 		qentry->have_irq = 1;
4455 		qentry->in_use = 1;
4456 	}
4457 
4458 	/*
4459 	 * If target mode is enable, also request the vector for the ATIO
4460 	 * queue.
4461 	 */
4462 	if (QLA_TGT_MODE_ENABLED() && (ql2xenablemsix != 0) &&
4463 	    IS_ATIO_MSIX_CAPABLE(ha)) {
4464 		qentry = &ha->msix_entries[QLA_ATIO_VECTOR];
4465 		rsp->msix = qentry;
4466 		qentry->handle = rsp;
4467 		scnprintf(qentry->name, sizeof(qentry->name),
4468 		    "qla2xxx%lu_%s", vha->host_no,
4469 		    msix_entries[QLA_ATIO_VECTOR].name);
4470 		qentry->in_use = 1;
4471 		ret = request_irq(qentry->vector,
4472 			msix_entries[QLA_ATIO_VECTOR].handler,
4473 			0, qentry->name, rsp);
4474 		qentry->have_irq = 1;
4475 	}
4476 
4477 msix_register_fail:
4478 	if (ret) {
4479 		ql_log(ql_log_fatal, vha, 0x00cb,
4480 		    "MSI-X: unable to register handler -- %x/%d.\n",
4481 		    qentry->vector, ret);
4482 		qla2x00_free_irqs(vha);
4483 		ha->mqenable = 0;
4484 		goto msix_out;
4485 	}
4486 
4487 	/* Enable MSI-X vector for response queue update for queue 0 */
4488 	if (IS_MQUE_CAPABLE(ha) &&
4489 	    (ha->msixbase && ha->mqiobase && ha->max_qpairs))
4490 		ha->mqenable = 1;
4491 	else
4492 		ha->mqenable = 0;
4493 
4494 	ql_dbg(ql_dbg_multiq, vha, 0xc005,
4495 	    "mqiobase=%p, max_rsp_queues=%d, max_req_queues=%d.\n",
4496 	    ha->mqiobase, ha->max_rsp_queues, ha->max_req_queues);
4497 	ql_dbg(ql_dbg_init, vha, 0x0055,
4498 	    "mqiobase=%p, max_rsp_queues=%d, max_req_queues=%d.\n",
4499 	    ha->mqiobase, ha->max_rsp_queues, ha->max_req_queues);
4500 
4501 msix_out:
4502 	return ret;
4503 
4504 free_irqs:
4505 	pci_free_irq_vectors(ha->pdev);
4506 	goto msix_out;
4507 }
4508 
4509 int
4510 qla2x00_request_irqs(struct qla_hw_data *ha, struct rsp_que *rsp)
4511 {
4512 	int ret = QLA_FUNCTION_FAILED;
4513 	device_reg_t *reg = ha->iobase;
4514 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
4515 
4516 	/* If possible, enable MSI-X. */
4517 	if (ql2xenablemsix == 0 || (!IS_QLA2432(ha) && !IS_QLA2532(ha) &&
4518 	    !IS_QLA8432(ha) && !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha) &&
4519 	    !IS_QLAFX00(ha) && !IS_QLA27XX(ha) && !IS_QLA28XX(ha)))
4520 		goto skip_msi;
4521 
4522 	if (ql2xenablemsix == 2)
4523 		goto skip_msix;
4524 
4525 	if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_HP &&
4526 		(ha->pdev->subsystem_device == 0x7040 ||
4527 		ha->pdev->subsystem_device == 0x7041 ||
4528 		ha->pdev->subsystem_device == 0x1705)) {
4529 		ql_log(ql_log_warn, vha, 0x0034,
4530 		    "MSI-X: Unsupported ISP 2432 SSVID/SSDID (0x%X,0x%X).\n",
4531 			ha->pdev->subsystem_vendor,
4532 			ha->pdev->subsystem_device);
4533 		goto skip_msi;
4534 	}
4535 
4536 	if (IS_QLA2432(ha) && (ha->pdev->revision < QLA_MSIX_CHIP_REV_24XX)) {
4537 		ql_log(ql_log_warn, vha, 0x0035,
4538 		    "MSI-X; Unsupported ISP2432 (0x%X, 0x%X).\n",
4539 		    ha->pdev->revision, QLA_MSIX_CHIP_REV_24XX);
4540 		goto skip_msix;
4541 	}
4542 
4543 	ret = qla24xx_enable_msix(ha, rsp);
4544 	if (!ret) {
4545 		ql_dbg(ql_dbg_init, vha, 0x0036,
4546 		    "MSI-X: Enabled (0x%X, 0x%X).\n",
4547 		    ha->chip_revision, ha->fw_attributes);
4548 		goto clear_risc_ints;
4549 	}
4550 
4551 skip_msix:
4552 
4553 	ql_log(ql_log_info, vha, 0x0037,
4554 	    "Falling back-to MSI mode -- ret=%d.\n", ret);
4555 
4556 	if (!IS_QLA24XX(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha) &&
4557 	    !IS_QLA8001(ha) && !IS_P3P_TYPE(ha) && !IS_QLAFX00(ha) &&
4558 	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
4559 		goto skip_msi;
4560 
4561 	ret = pci_alloc_irq_vectors(ha->pdev, 1, 1, PCI_IRQ_MSI);
4562 	if (ret > 0) {
4563 		ql_dbg(ql_dbg_init, vha, 0x0038,
4564 		    "MSI: Enabled.\n");
4565 		ha->flags.msi_enabled = 1;
4566 	} else
4567 		ql_log(ql_log_warn, vha, 0x0039,
4568 		    "Falling back-to INTa mode -- ret=%d.\n", ret);
4569 skip_msi:
4570 
4571 	/* Skip INTx on ISP82xx. */
4572 	if (!ha->flags.msi_enabled && IS_QLA82XX(ha))
4573 		return QLA_FUNCTION_FAILED;
4574 
4575 	ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
4576 	    ha->flags.msi_enabled ? 0 : IRQF_SHARED,
4577 	    QLA2XXX_DRIVER_NAME, rsp);
4578 	if (ret) {
4579 		ql_log(ql_log_warn, vha, 0x003a,
4580 		    "Failed to reserve interrupt %d already in use.\n",
4581 		    ha->pdev->irq);
4582 		goto fail;
4583 	} else if (!ha->flags.msi_enabled) {
4584 		ql_dbg(ql_dbg_init, vha, 0x0125,
4585 		    "INTa mode: Enabled.\n");
4586 		ha->flags.mr_intr_valid = 1;
4587 		/* Set max_qpair to 0, as MSI-X and MSI in not enabled */
4588 		ha->max_qpairs = 0;
4589 	}
4590 
4591 clear_risc_ints:
4592 	if (IS_FWI2_CAPABLE(ha) || IS_QLAFX00(ha))
4593 		goto fail;
4594 
4595 	spin_lock_irq(&ha->hardware_lock);
4596 	wrt_reg_word(&reg->isp.semaphore, 0);
4597 	spin_unlock_irq(&ha->hardware_lock);
4598 
4599 fail:
4600 	return ret;
4601 }
4602 
4603 void
4604 qla2x00_free_irqs(scsi_qla_host_t *vha)
4605 {
4606 	struct qla_hw_data *ha = vha->hw;
4607 	struct rsp_que *rsp;
4608 	struct qla_msix_entry *qentry;
4609 	int i;
4610 
4611 	/*
4612 	 * We need to check that ha->rsp_q_map is valid in case we are called
4613 	 * from a probe failure context.
4614 	 */
4615 	if (!ha->rsp_q_map || !ha->rsp_q_map[0])
4616 		goto free_irqs;
4617 	rsp = ha->rsp_q_map[0];
4618 
4619 	if (ha->flags.msix_enabled) {
4620 		for (i = 0; i < ha->msix_count; i++) {
4621 			qentry = &ha->msix_entries[i];
4622 			if (qentry->have_irq) {
4623 				irq_set_affinity_notifier(qentry->vector, NULL);
4624 				free_irq(pci_irq_vector(ha->pdev, i), qentry->handle);
4625 			}
4626 		}
4627 		kfree(ha->msix_entries);
4628 		ha->msix_entries = NULL;
4629 		ha->flags.msix_enabled = 0;
4630 		ql_dbg(ql_dbg_init, vha, 0x0042,
4631 			"Disabled MSI-X.\n");
4632 	} else {
4633 		free_irq(pci_irq_vector(ha->pdev, 0), rsp);
4634 	}
4635 
4636 free_irqs:
4637 	pci_free_irq_vectors(ha->pdev);
4638 }
4639 
4640 int qla25xx_request_irq(struct qla_hw_data *ha, struct qla_qpair *qpair,
4641 	struct qla_msix_entry *msix, int vector_type)
4642 {
4643 	const struct qla_init_msix_entry *intr = &msix_entries[vector_type];
4644 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
4645 	int ret;
4646 
4647 	scnprintf(msix->name, sizeof(msix->name),
4648 	    "qla2xxx%lu_qpair%d", vha->host_no, qpair->id);
4649 	ret = request_irq(msix->vector, intr->handler, 0, msix->name, qpair);
4650 	if (ret) {
4651 		ql_log(ql_log_fatal, vha, 0x00e6,
4652 		    "MSI-X: Unable to register handler -- %x/%d.\n",
4653 		    msix->vector, ret);
4654 		return ret;
4655 	}
4656 	msix->have_irq = 1;
4657 	msix->handle = qpair;
4658 	qla_mapq_init_qp_cpu_map(ha, msix, qpair);
4659 	return ret;
4660 }
4661