xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_iocb.c (revision 144679df)
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 
9 #include <linux/blkdev.h>
10 #include <linux/delay.h>
11 
12 #include <scsi/scsi_tcq.h>
13 
14 /**
15  * qla2x00_get_cmd_direction() - Determine control_flag data direction.
16  * @sp: SCSI command
17  *
18  * Returns the proper CF_* direction based on CDB.
19  */
20 static inline uint16_t
21 qla2x00_get_cmd_direction(srb_t *sp)
22 {
23 	uint16_t cflags;
24 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
25 	struct scsi_qla_host *vha = sp->vha;
26 
27 	cflags = 0;
28 
29 	/* Set transfer direction */
30 	if (cmd->sc_data_direction == DMA_TO_DEVICE) {
31 		cflags = CF_WRITE;
32 		vha->qla_stats.output_bytes += scsi_bufflen(cmd);
33 		vha->qla_stats.output_requests++;
34 	} else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
35 		cflags = CF_READ;
36 		vha->qla_stats.input_bytes += scsi_bufflen(cmd);
37 		vha->qla_stats.input_requests++;
38 	}
39 	return (cflags);
40 }
41 
42 /**
43  * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
44  * Continuation Type 0 IOCBs to allocate.
45  *
46  * @dsds: number of data segment descriptors needed
47  *
48  * Returns the number of IOCB entries needed to store @dsds.
49  */
50 uint16_t
51 qla2x00_calc_iocbs_32(uint16_t dsds)
52 {
53 	uint16_t iocbs;
54 
55 	iocbs = 1;
56 	if (dsds > 3) {
57 		iocbs += (dsds - 3) / 7;
58 		if ((dsds - 3) % 7)
59 			iocbs++;
60 	}
61 	return (iocbs);
62 }
63 
64 /**
65  * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
66  * Continuation Type 1 IOCBs to allocate.
67  *
68  * @dsds: number of data segment descriptors needed
69  *
70  * Returns the number of IOCB entries needed to store @dsds.
71  */
72 uint16_t
73 qla2x00_calc_iocbs_64(uint16_t dsds)
74 {
75 	uint16_t iocbs;
76 
77 	iocbs = 1;
78 	if (dsds > 2) {
79 		iocbs += (dsds - 2) / 5;
80 		if ((dsds - 2) % 5)
81 			iocbs++;
82 	}
83 	return (iocbs);
84 }
85 
86 /**
87  * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
88  * @vha: HA context
89  *
90  * Returns a pointer to the Continuation Type 0 IOCB packet.
91  */
92 static inline cont_entry_t *
93 qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
94 {
95 	cont_entry_t *cont_pkt;
96 	struct req_que *req = vha->req;
97 	/* Adjust ring index. */
98 	req->ring_index++;
99 	if (req->ring_index == req->length) {
100 		req->ring_index = 0;
101 		req->ring_ptr = req->ring;
102 	} else {
103 		req->ring_ptr++;
104 	}
105 
106 	cont_pkt = (cont_entry_t *)req->ring_ptr;
107 
108 	/* Load packet defaults. */
109 	put_unaligned_le32(CONTINUE_TYPE, &cont_pkt->entry_type);
110 
111 	return (cont_pkt);
112 }
113 
114 /**
115  * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
116  * @vha: HA context
117  * @req: request queue
118  *
119  * Returns a pointer to the continuation type 1 IOCB packet.
120  */
121 cont_a64_entry_t *
122 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
123 {
124 	cont_a64_entry_t *cont_pkt;
125 
126 	/* Adjust ring index. */
127 	req->ring_index++;
128 	if (req->ring_index == req->length) {
129 		req->ring_index = 0;
130 		req->ring_ptr = req->ring;
131 	} else {
132 		req->ring_ptr++;
133 	}
134 
135 	cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
136 
137 	/* Load packet defaults. */
138 	put_unaligned_le32(IS_QLAFX00(vha->hw) ? CONTINUE_A64_TYPE_FX00 :
139 			   CONTINUE_A64_TYPE, &cont_pkt->entry_type);
140 
141 	return (cont_pkt);
142 }
143 
144 inline int
145 qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
146 {
147 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
148 
149 	/* We always use DIFF Bundling for best performance */
150 	*fw_prot_opts = 0;
151 
152 	/* Translate SCSI opcode to a protection opcode */
153 	switch (scsi_get_prot_op(cmd)) {
154 	case SCSI_PROT_READ_STRIP:
155 		*fw_prot_opts |= PO_MODE_DIF_REMOVE;
156 		break;
157 	case SCSI_PROT_WRITE_INSERT:
158 		*fw_prot_opts |= PO_MODE_DIF_INSERT;
159 		break;
160 	case SCSI_PROT_READ_INSERT:
161 		*fw_prot_opts |= PO_MODE_DIF_INSERT;
162 		break;
163 	case SCSI_PROT_WRITE_STRIP:
164 		*fw_prot_opts |= PO_MODE_DIF_REMOVE;
165 		break;
166 	case SCSI_PROT_READ_PASS:
167 	case SCSI_PROT_WRITE_PASS:
168 		if (cmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
169 			*fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
170 		else
171 			*fw_prot_opts |= PO_MODE_DIF_PASS;
172 		break;
173 	default:	/* Normal Request */
174 		*fw_prot_opts |= PO_MODE_DIF_PASS;
175 		break;
176 	}
177 
178 	if (!(cmd->prot_flags & SCSI_PROT_GUARD_CHECK))
179 		*fw_prot_opts |= PO_DISABLE_GUARD_CHECK;
180 
181 	return scsi_prot_sg_count(cmd);
182 }
183 
184 /*
185  * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
186  * capable IOCB types.
187  *
188  * @sp: SRB command to process
189  * @cmd_pkt: Command type 2 IOCB
190  * @tot_dsds: Total number of segments to transfer
191  */
192 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
193     uint16_t tot_dsds)
194 {
195 	uint16_t	avail_dsds;
196 	struct dsd32	*cur_dsd;
197 	scsi_qla_host_t	*vha;
198 	struct scsi_cmnd *cmd;
199 	struct scatterlist *sg;
200 	int i;
201 
202 	cmd = GET_CMD_SP(sp);
203 
204 	/* Update entry type to indicate Command Type 2 IOCB */
205 	put_unaligned_le32(COMMAND_TYPE, &cmd_pkt->entry_type);
206 
207 	/* No data transfer */
208 	if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
209 		cmd_pkt->byte_count = cpu_to_le32(0);
210 		return;
211 	}
212 
213 	vha = sp->vha;
214 	cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
215 
216 	/* Three DSDs are available in the Command Type 2 IOCB */
217 	avail_dsds = ARRAY_SIZE(cmd_pkt->dsd32);
218 	cur_dsd = cmd_pkt->dsd32;
219 
220 	/* Load data segments */
221 	scsi_for_each_sg(cmd, sg, tot_dsds, i) {
222 		cont_entry_t *cont_pkt;
223 
224 		/* Allocate additional continuation packets? */
225 		if (avail_dsds == 0) {
226 			/*
227 			 * Seven DSDs are available in the Continuation
228 			 * Type 0 IOCB.
229 			 */
230 			cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
231 			cur_dsd = cont_pkt->dsd;
232 			avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
233 		}
234 
235 		append_dsd32(&cur_dsd, sg);
236 		avail_dsds--;
237 	}
238 }
239 
240 /**
241  * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
242  * capable IOCB types.
243  *
244  * @sp: SRB command to process
245  * @cmd_pkt: Command type 3 IOCB
246  * @tot_dsds: Total number of segments to transfer
247  */
248 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
249     uint16_t tot_dsds)
250 {
251 	uint16_t	avail_dsds;
252 	struct dsd64	*cur_dsd;
253 	scsi_qla_host_t	*vha;
254 	struct scsi_cmnd *cmd;
255 	struct scatterlist *sg;
256 	int i;
257 
258 	cmd = GET_CMD_SP(sp);
259 
260 	/* Update entry type to indicate Command Type 3 IOCB */
261 	put_unaligned_le32(COMMAND_A64_TYPE, &cmd_pkt->entry_type);
262 
263 	/* No data transfer */
264 	if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
265 		cmd_pkt->byte_count = cpu_to_le32(0);
266 		return;
267 	}
268 
269 	vha = sp->vha;
270 	cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
271 
272 	/* Two DSDs are available in the Command Type 3 IOCB */
273 	avail_dsds = ARRAY_SIZE(cmd_pkt->dsd64);
274 	cur_dsd = cmd_pkt->dsd64;
275 
276 	/* Load data segments */
277 	scsi_for_each_sg(cmd, sg, tot_dsds, i) {
278 		cont_a64_entry_t *cont_pkt;
279 
280 		/* Allocate additional continuation packets? */
281 		if (avail_dsds == 0) {
282 			/*
283 			 * Five DSDs are available in the Continuation
284 			 * Type 1 IOCB.
285 			 */
286 			cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
287 			cur_dsd = cont_pkt->dsd;
288 			avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
289 		}
290 
291 		append_dsd64(&cur_dsd, sg);
292 		avail_dsds--;
293 	}
294 }
295 
296 /*
297  * Find the first handle that is not in use, starting from
298  * req->current_outstanding_cmd + 1. The caller must hold the lock that is
299  * associated with @req.
300  */
301 uint32_t qla2xxx_get_next_handle(struct req_que *req)
302 {
303 	uint32_t index, handle = req->current_outstanding_cmd;
304 
305 	for (index = 1; index < req->num_outstanding_cmds; index++) {
306 		handle++;
307 		if (handle == req->num_outstanding_cmds)
308 			handle = 1;
309 		if (!req->outstanding_cmds[handle])
310 			return handle;
311 	}
312 
313 	return 0;
314 }
315 
316 /**
317  * qla2x00_start_scsi() - Send a SCSI command to the ISP
318  * @sp: command to send to the ISP
319  *
320  * Returns non-zero if a failure occurred, else zero.
321  */
322 int
323 qla2x00_start_scsi(srb_t *sp)
324 {
325 	int		nseg;
326 	unsigned long   flags;
327 	scsi_qla_host_t	*vha;
328 	struct scsi_cmnd *cmd;
329 	uint32_t	*clr_ptr;
330 	uint32_t	handle;
331 	cmd_entry_t	*cmd_pkt;
332 	uint16_t	cnt;
333 	uint16_t	req_cnt;
334 	uint16_t	tot_dsds;
335 	struct device_reg_2xxx __iomem *reg;
336 	struct qla_hw_data *ha;
337 	struct req_que *req;
338 	struct rsp_que *rsp;
339 
340 	/* Setup device pointers. */
341 	vha = sp->vha;
342 	ha = vha->hw;
343 	reg = &ha->iobase->isp;
344 	cmd = GET_CMD_SP(sp);
345 	req = ha->req_q_map[0];
346 	rsp = ha->rsp_q_map[0];
347 	/* So we know we haven't pci_map'ed anything yet */
348 	tot_dsds = 0;
349 
350 	/* Send marker if required */
351 	if (vha->marker_needed != 0) {
352 		if (qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL) !=
353 		    QLA_SUCCESS) {
354 			return (QLA_FUNCTION_FAILED);
355 		}
356 		vha->marker_needed = 0;
357 	}
358 
359 	/* Acquire ring specific lock */
360 	spin_lock_irqsave(&ha->hardware_lock, flags);
361 
362 	handle = qla2xxx_get_next_handle(req);
363 	if (handle == 0)
364 		goto queuing_error;
365 
366 	/* Map the sg table so we have an accurate count of sg entries needed */
367 	if (scsi_sg_count(cmd)) {
368 		nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
369 		    scsi_sg_count(cmd), cmd->sc_data_direction);
370 		if (unlikely(!nseg))
371 			goto queuing_error;
372 	} else
373 		nseg = 0;
374 
375 	tot_dsds = nseg;
376 
377 	/* Calculate the number of request entries needed. */
378 	req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
379 	if (req->cnt < (req_cnt + 2)) {
380 		cnt = rd_reg_word_relaxed(ISP_REQ_Q_OUT(ha, reg));
381 		if (req->ring_index < cnt)
382 			req->cnt = cnt - req->ring_index;
383 		else
384 			req->cnt = req->length -
385 			    (req->ring_index - cnt);
386 		/* If still no head room then bail out */
387 		if (req->cnt < (req_cnt + 2))
388 			goto queuing_error;
389 	}
390 
391 	/* Build command packet */
392 	req->current_outstanding_cmd = handle;
393 	req->outstanding_cmds[handle] = sp;
394 	sp->handle = handle;
395 	cmd->host_scribble = (unsigned char *)(unsigned long)handle;
396 	req->cnt -= req_cnt;
397 
398 	cmd_pkt = (cmd_entry_t *)req->ring_ptr;
399 	cmd_pkt->handle = handle;
400 	/* Zero out remaining portion of packet. */
401 	clr_ptr = (uint32_t *)cmd_pkt + 2;
402 	memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
403 	cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
404 
405 	/* Set target ID and LUN number*/
406 	SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
407 	cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
408 	cmd_pkt->control_flags = cpu_to_le16(CF_SIMPLE_TAG);
409 
410 	/* Load SCSI command packet. */
411 	memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
412 	cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
413 
414 	/* Build IOCB segments */
415 	ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
416 
417 	/* Set total data segment count. */
418 	cmd_pkt->entry_count = (uint8_t)req_cnt;
419 	wmb();
420 
421 	/* Adjust ring index. */
422 	req->ring_index++;
423 	if (req->ring_index == req->length) {
424 		req->ring_index = 0;
425 		req->ring_ptr = req->ring;
426 	} else
427 		req->ring_ptr++;
428 
429 	sp->flags |= SRB_DMA_VALID;
430 
431 	/* Set chip new ring index. */
432 	wrt_reg_word(ISP_REQ_Q_IN(ha, reg), req->ring_index);
433 	rd_reg_word_relaxed(ISP_REQ_Q_IN(ha, reg));	/* PCI Posting. */
434 
435 	/* Manage unprocessed RIO/ZIO commands in response queue. */
436 	if (vha->flags.process_response_queue &&
437 	    rsp->ring_ptr->signature != RESPONSE_PROCESSED)
438 		qla2x00_process_response_queue(rsp);
439 
440 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
441 	return (QLA_SUCCESS);
442 
443 queuing_error:
444 	if (tot_dsds)
445 		scsi_dma_unmap(cmd);
446 
447 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
448 
449 	return (QLA_FUNCTION_FAILED);
450 }
451 
452 /**
453  * qla2x00_start_iocbs() - Execute the IOCB command
454  * @vha: HA context
455  * @req: request queue
456  */
457 void
458 qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
459 {
460 	struct qla_hw_data *ha = vha->hw;
461 	device_reg_t *reg = ISP_QUE_REG(ha, req->id);
462 
463 	if (IS_P3P_TYPE(ha)) {
464 		qla82xx_start_iocbs(vha);
465 	} else {
466 		/* Adjust ring index. */
467 		req->ring_index++;
468 		if (req->ring_index == req->length) {
469 			req->ring_index = 0;
470 			req->ring_ptr = req->ring;
471 		} else
472 			req->ring_ptr++;
473 
474 		/* Set chip new ring index. */
475 		if (ha->mqenable || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
476 			wrt_reg_dword(req->req_q_in, req->ring_index);
477 		} else if (IS_QLA83XX(ha)) {
478 			wrt_reg_dword(req->req_q_in, req->ring_index);
479 			rd_reg_dword_relaxed(&ha->iobase->isp24.hccr);
480 		} else if (IS_QLAFX00(ha)) {
481 			wrt_reg_dword(&reg->ispfx00.req_q_in, req->ring_index);
482 			rd_reg_dword_relaxed(&reg->ispfx00.req_q_in);
483 			QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code);
484 		} else if (IS_FWI2_CAPABLE(ha)) {
485 			wrt_reg_dword(&reg->isp24.req_q_in, req->ring_index);
486 			rd_reg_dword_relaxed(&reg->isp24.req_q_in);
487 		} else {
488 			wrt_reg_word(ISP_REQ_Q_IN(ha, &reg->isp),
489 				req->ring_index);
490 			rd_reg_word_relaxed(ISP_REQ_Q_IN(ha, &reg->isp));
491 		}
492 	}
493 }
494 
495 /**
496  * __qla2x00_marker() - Send a marker IOCB to the firmware.
497  * @vha: HA context
498  * @qpair: queue pair pointer
499  * @loop_id: loop ID
500  * @lun: LUN
501  * @type: marker modifier
502  *
503  * Can be called from both normal and interrupt context.
504  *
505  * Returns non-zero if a failure occurred, else zero.
506  */
507 static int
508 __qla2x00_marker(struct scsi_qla_host *vha, struct qla_qpair *qpair,
509     uint16_t loop_id, uint64_t lun, uint8_t type)
510 {
511 	mrk_entry_t *mrk;
512 	struct mrk_entry_24xx *mrk24 = NULL;
513 	struct req_que *req = qpair->req;
514 	struct qla_hw_data *ha = vha->hw;
515 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
516 
517 	mrk = (mrk_entry_t *)__qla2x00_alloc_iocbs(qpair, NULL);
518 	if (mrk == NULL) {
519 		ql_log(ql_log_warn, base_vha, 0x3026,
520 		    "Failed to allocate Marker IOCB.\n");
521 
522 		return (QLA_FUNCTION_FAILED);
523 	}
524 
525 	mrk24 = (struct mrk_entry_24xx *)mrk;
526 
527 	mrk->entry_type = MARKER_TYPE;
528 	mrk->modifier = type;
529 	if (type != MK_SYNC_ALL) {
530 		if (IS_FWI2_CAPABLE(ha)) {
531 			mrk24->nport_handle = cpu_to_le16(loop_id);
532 			int_to_scsilun(lun, (struct scsi_lun *)&mrk24->lun);
533 			host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
534 			mrk24->vp_index = vha->vp_idx;
535 		} else {
536 			SET_TARGET_ID(ha, mrk->target, loop_id);
537 			mrk->lun = cpu_to_le16((uint16_t)lun);
538 		}
539 	}
540 
541 	if (IS_FWI2_CAPABLE(ha))
542 		mrk24->handle = QLA_SKIP_HANDLE;
543 
544 	wmb();
545 
546 	qla2x00_start_iocbs(vha, req);
547 
548 	return (QLA_SUCCESS);
549 }
550 
551 int
552 qla2x00_marker(struct scsi_qla_host *vha, struct qla_qpair *qpair,
553     uint16_t loop_id, uint64_t lun, uint8_t type)
554 {
555 	int ret;
556 	unsigned long flags = 0;
557 
558 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
559 	ret = __qla2x00_marker(vha, qpair, loop_id, lun, type);
560 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
561 
562 	return (ret);
563 }
564 
565 /*
566  * qla2x00_issue_marker
567  *
568  * Issue marker
569  * Caller CAN have hardware lock held as specified by ha_locked parameter.
570  * Might release it, then reaquire.
571  */
572 int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
573 {
574 	if (ha_locked) {
575 		if (__qla2x00_marker(vha, vha->hw->base_qpair, 0, 0,
576 					MK_SYNC_ALL) != QLA_SUCCESS)
577 			return QLA_FUNCTION_FAILED;
578 	} else {
579 		if (qla2x00_marker(vha, vha->hw->base_qpair, 0, 0,
580 					MK_SYNC_ALL) != QLA_SUCCESS)
581 			return QLA_FUNCTION_FAILED;
582 	}
583 	vha->marker_needed = 0;
584 
585 	return QLA_SUCCESS;
586 }
587 
588 static inline int
589 qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
590 	uint16_t tot_dsds)
591 {
592 	struct dsd64 *cur_dsd = NULL, *next_dsd;
593 	scsi_qla_host_t	*vha;
594 	struct qla_hw_data *ha;
595 	struct scsi_cmnd *cmd;
596 	struct	scatterlist *cur_seg;
597 	uint8_t avail_dsds;
598 	uint8_t first_iocb = 1;
599 	uint32_t dsd_list_len;
600 	struct dsd_dma *dsd_ptr;
601 	struct ct6_dsd *ctx;
602 	struct qla_qpair *qpair = sp->qpair;
603 
604 	cmd = GET_CMD_SP(sp);
605 
606 	/* Update entry type to indicate Command Type 3 IOCB */
607 	put_unaligned_le32(COMMAND_TYPE_6, &cmd_pkt->entry_type);
608 
609 	/* No data transfer */
610 	if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
611 		cmd_pkt->byte_count = cpu_to_le32(0);
612 		return 0;
613 	}
614 
615 	vha = sp->vha;
616 	ha = vha->hw;
617 
618 	/* Set transfer direction */
619 	if (cmd->sc_data_direction == DMA_TO_DEVICE) {
620 		cmd_pkt->control_flags = cpu_to_le16(CF_WRITE_DATA);
621 		qpair->counters.output_bytes += scsi_bufflen(cmd);
622 		qpair->counters.output_requests++;
623 	} else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
624 		cmd_pkt->control_flags = cpu_to_le16(CF_READ_DATA);
625 		qpair->counters.input_bytes += scsi_bufflen(cmd);
626 		qpair->counters.input_requests++;
627 	}
628 
629 	cur_seg = scsi_sglist(cmd);
630 	ctx = &sp->u.scmd.ct6_ctx;
631 
632 	while (tot_dsds) {
633 		avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
634 		    QLA_DSDS_PER_IOCB : tot_dsds;
635 		tot_dsds -= avail_dsds;
636 		dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
637 
638 		dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
639 		    struct dsd_dma, list);
640 		next_dsd = dsd_ptr->dsd_addr;
641 		list_del(&dsd_ptr->list);
642 		ha->gbl_dsd_avail--;
643 		list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
644 		ctx->dsd_use_cnt++;
645 		ha->gbl_dsd_inuse++;
646 
647 		if (first_iocb) {
648 			first_iocb = 0;
649 			put_unaligned_le64(dsd_ptr->dsd_list_dma,
650 					   &cmd_pkt->fcp_dsd.address);
651 			cmd_pkt->fcp_dsd.length = cpu_to_le32(dsd_list_len);
652 		} else {
653 			put_unaligned_le64(dsd_ptr->dsd_list_dma,
654 					   &cur_dsd->address);
655 			cur_dsd->length = cpu_to_le32(dsd_list_len);
656 			cur_dsd++;
657 		}
658 		cur_dsd = next_dsd;
659 		while (avail_dsds) {
660 			append_dsd64(&cur_dsd, cur_seg);
661 			cur_seg = sg_next(cur_seg);
662 			avail_dsds--;
663 		}
664 	}
665 
666 	/* Null termination */
667 	cur_dsd->address = 0;
668 	cur_dsd->length = 0;
669 	cur_dsd++;
670 	cmd_pkt->control_flags |= cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
671 	return 0;
672 }
673 
674 /*
675  * qla24xx_calc_dsd_lists() - Determine number of DSD list required
676  * for Command Type 6.
677  *
678  * @dsds: number of data segment descriptors needed
679  *
680  * Returns the number of dsd list needed to store @dsds.
681  */
682 static inline uint16_t
683 qla24xx_calc_dsd_lists(uint16_t dsds)
684 {
685 	uint16_t dsd_lists = 0;
686 
687 	dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
688 	if (dsds % QLA_DSDS_PER_IOCB)
689 		dsd_lists++;
690 	return dsd_lists;
691 }
692 
693 
694 /**
695  * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
696  * IOCB types.
697  *
698  * @sp: SRB command to process
699  * @cmd_pkt: Command type 3 IOCB
700  * @tot_dsds: Total number of segments to transfer
701  * @req: pointer to request queue
702  */
703 inline void
704 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
705 	uint16_t tot_dsds, struct req_que *req)
706 {
707 	uint16_t	avail_dsds;
708 	struct dsd64	*cur_dsd;
709 	scsi_qla_host_t	*vha;
710 	struct scsi_cmnd *cmd;
711 	struct scatterlist *sg;
712 	int i;
713 	struct qla_qpair *qpair = sp->qpair;
714 
715 	cmd = GET_CMD_SP(sp);
716 
717 	/* Update entry type to indicate Command Type 3 IOCB */
718 	put_unaligned_le32(COMMAND_TYPE_7, &cmd_pkt->entry_type);
719 
720 	/* No data transfer */
721 	if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
722 		cmd_pkt->byte_count = cpu_to_le32(0);
723 		return;
724 	}
725 
726 	vha = sp->vha;
727 
728 	/* Set transfer direction */
729 	if (cmd->sc_data_direction == DMA_TO_DEVICE) {
730 		cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_WRITE_DATA);
731 		qpair->counters.output_bytes += scsi_bufflen(cmd);
732 		qpair->counters.output_requests++;
733 	} else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
734 		cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_READ_DATA);
735 		qpair->counters.input_bytes += scsi_bufflen(cmd);
736 		qpair->counters.input_requests++;
737 	}
738 
739 	/* One DSD is available in the Command Type 3 IOCB */
740 	avail_dsds = 1;
741 	cur_dsd = &cmd_pkt->dsd;
742 
743 	/* Load data segments */
744 
745 	scsi_for_each_sg(cmd, sg, tot_dsds, i) {
746 		cont_a64_entry_t *cont_pkt;
747 
748 		/* Allocate additional continuation packets? */
749 		if (avail_dsds == 0) {
750 			/*
751 			 * Five DSDs are available in the Continuation
752 			 * Type 1 IOCB.
753 			 */
754 			cont_pkt = qla2x00_prep_cont_type1_iocb(vha, req);
755 			cur_dsd = cont_pkt->dsd;
756 			avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
757 		}
758 
759 		append_dsd64(&cur_dsd, sg);
760 		avail_dsds--;
761 	}
762 }
763 
764 struct fw_dif_context {
765 	__le32	ref_tag;
766 	__le16	app_tag;
767 	uint8_t ref_tag_mask[4];	/* Validation/Replacement Mask*/
768 	uint8_t app_tag_mask[2];	/* Validation/Replacement Mask*/
769 };
770 
771 /*
772  * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
773  *
774  */
775 static inline void
776 qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
777     unsigned int protcnt)
778 {
779 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
780 
781 	pkt->ref_tag = cpu_to_le32(scsi_prot_ref_tag(cmd));
782 
783 	if (cmd->prot_flags & SCSI_PROT_REF_CHECK &&
784 	    qla2x00_hba_err_chk_enabled(sp)) {
785 		pkt->ref_tag_mask[0] = 0xff;
786 		pkt->ref_tag_mask[1] = 0xff;
787 		pkt->ref_tag_mask[2] = 0xff;
788 		pkt->ref_tag_mask[3] = 0xff;
789 	}
790 
791 	pkt->app_tag = cpu_to_le16(0);
792 	pkt->app_tag_mask[0] = 0x0;
793 	pkt->app_tag_mask[1] = 0x0;
794 }
795 
796 int
797 qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
798 	uint32_t *partial)
799 {
800 	struct scatterlist *sg;
801 	uint32_t cumulative_partial, sg_len;
802 	dma_addr_t sg_dma_addr;
803 
804 	if (sgx->num_bytes == sgx->tot_bytes)
805 		return 0;
806 
807 	sg = sgx->cur_sg;
808 	cumulative_partial = sgx->tot_partial;
809 
810 	sg_dma_addr = sg_dma_address(sg);
811 	sg_len = sg_dma_len(sg);
812 
813 	sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
814 
815 	if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
816 		sgx->dma_len = (blk_sz - cumulative_partial);
817 		sgx->tot_partial = 0;
818 		sgx->num_bytes += blk_sz;
819 		*partial = 0;
820 	} else {
821 		sgx->dma_len = sg_len - sgx->bytes_consumed;
822 		sgx->tot_partial += sgx->dma_len;
823 		*partial = 1;
824 	}
825 
826 	sgx->bytes_consumed += sgx->dma_len;
827 
828 	if (sg_len == sgx->bytes_consumed) {
829 		sg = sg_next(sg);
830 		sgx->num_sg++;
831 		sgx->cur_sg = sg;
832 		sgx->bytes_consumed = 0;
833 	}
834 
835 	return 1;
836 }
837 
838 int
839 qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
840 	struct dsd64 *dsd, uint16_t tot_dsds, struct qla_tc_param *tc)
841 {
842 	void *next_dsd;
843 	uint8_t avail_dsds = 0;
844 	uint32_t dsd_list_len;
845 	struct dsd_dma *dsd_ptr;
846 	struct scatterlist *sg_prot;
847 	struct dsd64 *cur_dsd = dsd;
848 	uint16_t	used_dsds = tot_dsds;
849 	uint32_t	prot_int; /* protection interval */
850 	uint32_t	partial;
851 	struct qla2_sgx sgx;
852 	dma_addr_t	sle_dma;
853 	uint32_t	sle_dma_len, tot_prot_dma_len = 0;
854 	struct scsi_cmnd *cmd;
855 
856 	memset(&sgx, 0, sizeof(struct qla2_sgx));
857 	if (sp) {
858 		cmd = GET_CMD_SP(sp);
859 		prot_int = scsi_prot_interval(cmd);
860 
861 		sgx.tot_bytes = scsi_bufflen(cmd);
862 		sgx.cur_sg = scsi_sglist(cmd);
863 		sgx.sp = sp;
864 
865 		sg_prot = scsi_prot_sglist(cmd);
866 	} else if (tc) {
867 		prot_int      = tc->blk_sz;
868 		sgx.tot_bytes = tc->bufflen;
869 		sgx.cur_sg    = tc->sg;
870 		sg_prot	      = tc->prot_sg;
871 	} else {
872 		BUG();
873 		return 1;
874 	}
875 
876 	while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
877 
878 		sle_dma = sgx.dma_addr;
879 		sle_dma_len = sgx.dma_len;
880 alloc_and_fill:
881 		/* Allocate additional continuation packets? */
882 		if (avail_dsds == 0) {
883 			avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
884 					QLA_DSDS_PER_IOCB : used_dsds;
885 			dsd_list_len = (avail_dsds + 1) * 12;
886 			used_dsds -= avail_dsds;
887 
888 			/* allocate tracking DS */
889 			dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
890 			if (!dsd_ptr)
891 				return 1;
892 
893 			/* allocate new list */
894 			dsd_ptr->dsd_addr = next_dsd =
895 			    dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
896 				&dsd_ptr->dsd_list_dma);
897 
898 			if (!next_dsd) {
899 				/*
900 				 * Need to cleanup only this dsd_ptr, rest
901 				 * will be done by sp_free_dma()
902 				 */
903 				kfree(dsd_ptr);
904 				return 1;
905 			}
906 
907 			if (sp) {
908 				list_add_tail(&dsd_ptr->list,
909 					      &sp->u.scmd.crc_ctx->dsd_list);
910 
911 				sp->flags |= SRB_CRC_CTX_DSD_VALID;
912 			} else {
913 				list_add_tail(&dsd_ptr->list,
914 				    &(tc->ctx->dsd_list));
915 				*tc->ctx_dsd_alloced = 1;
916 			}
917 
918 
919 			/* add new list to cmd iocb or last list */
920 			put_unaligned_le64(dsd_ptr->dsd_list_dma,
921 					   &cur_dsd->address);
922 			cur_dsd->length = cpu_to_le32(dsd_list_len);
923 			cur_dsd = next_dsd;
924 		}
925 		put_unaligned_le64(sle_dma, &cur_dsd->address);
926 		cur_dsd->length = cpu_to_le32(sle_dma_len);
927 		cur_dsd++;
928 		avail_dsds--;
929 
930 		if (partial == 0) {
931 			/* Got a full protection interval */
932 			sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
933 			sle_dma_len = 8;
934 
935 			tot_prot_dma_len += sle_dma_len;
936 			if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
937 				tot_prot_dma_len = 0;
938 				sg_prot = sg_next(sg_prot);
939 			}
940 
941 			partial = 1; /* So as to not re-enter this block */
942 			goto alloc_and_fill;
943 		}
944 	}
945 	/* Null termination */
946 	cur_dsd->address = 0;
947 	cur_dsd->length = 0;
948 	cur_dsd++;
949 	return 0;
950 }
951 
952 int
953 qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp,
954 	struct dsd64 *dsd, uint16_t tot_dsds, struct qla_tc_param *tc)
955 {
956 	void *next_dsd;
957 	uint8_t avail_dsds = 0;
958 	uint32_t dsd_list_len;
959 	struct dsd_dma *dsd_ptr;
960 	struct scatterlist *sg, *sgl;
961 	struct dsd64 *cur_dsd = dsd;
962 	int	i;
963 	uint16_t	used_dsds = tot_dsds;
964 	struct scsi_cmnd *cmd;
965 
966 	if (sp) {
967 		cmd = GET_CMD_SP(sp);
968 		sgl = scsi_sglist(cmd);
969 	} else if (tc) {
970 		sgl = tc->sg;
971 	} else {
972 		BUG();
973 		return 1;
974 	}
975 
976 
977 	for_each_sg(sgl, sg, tot_dsds, i) {
978 		/* Allocate additional continuation packets? */
979 		if (avail_dsds == 0) {
980 			avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
981 					QLA_DSDS_PER_IOCB : used_dsds;
982 			dsd_list_len = (avail_dsds + 1) * 12;
983 			used_dsds -= avail_dsds;
984 
985 			/* allocate tracking DS */
986 			dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
987 			if (!dsd_ptr)
988 				return 1;
989 
990 			/* allocate new list */
991 			dsd_ptr->dsd_addr = next_dsd =
992 			    dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
993 				&dsd_ptr->dsd_list_dma);
994 
995 			if (!next_dsd) {
996 				/*
997 				 * Need to cleanup only this dsd_ptr, rest
998 				 * will be done by sp_free_dma()
999 				 */
1000 				kfree(dsd_ptr);
1001 				return 1;
1002 			}
1003 
1004 			if (sp) {
1005 				list_add_tail(&dsd_ptr->list,
1006 					      &sp->u.scmd.crc_ctx->dsd_list);
1007 
1008 				sp->flags |= SRB_CRC_CTX_DSD_VALID;
1009 			} else {
1010 				list_add_tail(&dsd_ptr->list,
1011 				    &(tc->ctx->dsd_list));
1012 				*tc->ctx_dsd_alloced = 1;
1013 			}
1014 
1015 			/* add new list to cmd iocb or last list */
1016 			put_unaligned_le64(dsd_ptr->dsd_list_dma,
1017 					   &cur_dsd->address);
1018 			cur_dsd->length = cpu_to_le32(dsd_list_len);
1019 			cur_dsd = next_dsd;
1020 		}
1021 		append_dsd64(&cur_dsd, sg);
1022 		avail_dsds--;
1023 
1024 	}
1025 	/* Null termination */
1026 	cur_dsd->address = 0;
1027 	cur_dsd->length = 0;
1028 	cur_dsd++;
1029 	return 0;
1030 }
1031 
1032 int
1033 qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1034 	struct dsd64 *cur_dsd, uint16_t tot_dsds, struct qla_tgt_cmd *tc)
1035 {
1036 	struct dsd_dma *dsd_ptr = NULL, *dif_dsd, *nxt_dsd;
1037 	struct scatterlist *sg, *sgl;
1038 	struct crc_context *difctx = NULL;
1039 	struct scsi_qla_host *vha;
1040 	uint dsd_list_len;
1041 	uint avail_dsds = 0;
1042 	uint used_dsds = tot_dsds;
1043 	bool dif_local_dma_alloc = false;
1044 	bool direction_to_device = false;
1045 	int i;
1046 
1047 	if (sp) {
1048 		struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1049 
1050 		sgl = scsi_prot_sglist(cmd);
1051 		vha = sp->vha;
1052 		difctx = sp->u.scmd.crc_ctx;
1053 		direction_to_device = cmd->sc_data_direction == DMA_TO_DEVICE;
1054 		ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe021,
1055 		  "%s: scsi_cmnd: %p, crc_ctx: %p, sp: %p\n",
1056 			__func__, cmd, difctx, sp);
1057 	} else if (tc) {
1058 		vha = tc->vha;
1059 		sgl = tc->prot_sg;
1060 		difctx = tc->ctx;
1061 		direction_to_device = tc->dma_data_direction == DMA_TO_DEVICE;
1062 	} else {
1063 		BUG();
1064 		return 1;
1065 	}
1066 
1067 	ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe021,
1068 	    "%s: enter (write=%u)\n", __func__, direction_to_device);
1069 
1070 	/* if initiator doing write or target doing read */
1071 	if (direction_to_device) {
1072 		for_each_sg(sgl, sg, tot_dsds, i) {
1073 			u64 sle_phys = sg_phys(sg);
1074 
1075 			/* If SGE addr + len flips bits in upper 32-bits */
1076 			if (MSD(sle_phys + sg->length) ^ MSD(sle_phys)) {
1077 				ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe022,
1078 				    "%s: page boundary crossing (phys=%llx len=%x)\n",
1079 				    __func__, sle_phys, sg->length);
1080 
1081 				if (difctx) {
1082 					ha->dif_bundle_crossed_pages++;
1083 					dif_local_dma_alloc = true;
1084 				} else {
1085 					ql_dbg(ql_dbg_tgt + ql_dbg_verbose,
1086 					    vha, 0xe022,
1087 					    "%s: difctx pointer is NULL\n",
1088 					    __func__);
1089 				}
1090 				break;
1091 			}
1092 		}
1093 		ha->dif_bundle_writes++;
1094 	} else {
1095 		ha->dif_bundle_reads++;
1096 	}
1097 
1098 	if (ql2xdifbundlinginternalbuffers)
1099 		dif_local_dma_alloc = direction_to_device;
1100 
1101 	if (dif_local_dma_alloc) {
1102 		u32 track_difbundl_buf = 0;
1103 		u32 ldma_sg_len = 0;
1104 		u8 ldma_needed = 1;
1105 
1106 		difctx->no_dif_bundl = 0;
1107 		difctx->dif_bundl_len = 0;
1108 
1109 		/* Track DSD buffers */
1110 		INIT_LIST_HEAD(&difctx->ldif_dsd_list);
1111 		/* Track local DMA buffers */
1112 		INIT_LIST_HEAD(&difctx->ldif_dma_hndl_list);
1113 
1114 		for_each_sg(sgl, sg, tot_dsds, i) {
1115 			u32 sglen = sg_dma_len(sg);
1116 
1117 			ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe023,
1118 			    "%s: sg[%x] (phys=%llx sglen=%x) ldma_sg_len: %x dif_bundl_len: %x ldma_needed: %x\n",
1119 			    __func__, i, (u64)sg_phys(sg), sglen, ldma_sg_len,
1120 			    difctx->dif_bundl_len, ldma_needed);
1121 
1122 			while (sglen) {
1123 				u32 xfrlen = 0;
1124 
1125 				if (ldma_needed) {
1126 					/*
1127 					 * Allocate list item to store
1128 					 * the DMA buffers
1129 					 */
1130 					dsd_ptr = kzalloc(sizeof(*dsd_ptr),
1131 					    GFP_ATOMIC);
1132 					if (!dsd_ptr) {
1133 						ql_dbg(ql_dbg_tgt, vha, 0xe024,
1134 						    "%s: failed alloc dsd_ptr\n",
1135 						    __func__);
1136 						return 1;
1137 					}
1138 					ha->dif_bundle_kallocs++;
1139 
1140 					/* allocate dma buffer */
1141 					dsd_ptr->dsd_addr = dma_pool_alloc
1142 						(ha->dif_bundl_pool, GFP_ATOMIC,
1143 						 &dsd_ptr->dsd_list_dma);
1144 					if (!dsd_ptr->dsd_addr) {
1145 						ql_dbg(ql_dbg_tgt, vha, 0xe024,
1146 						    "%s: failed alloc ->dsd_ptr\n",
1147 						    __func__);
1148 						/*
1149 						 * need to cleanup only this
1150 						 * dsd_ptr rest will be done
1151 						 * by sp_free_dma()
1152 						 */
1153 						kfree(dsd_ptr);
1154 						ha->dif_bundle_kallocs--;
1155 						return 1;
1156 					}
1157 					ha->dif_bundle_dma_allocs++;
1158 					ldma_needed = 0;
1159 					difctx->no_dif_bundl++;
1160 					list_add_tail(&dsd_ptr->list,
1161 					    &difctx->ldif_dma_hndl_list);
1162 				}
1163 
1164 				/* xfrlen is min of dma pool size and sglen */
1165 				xfrlen = (sglen >
1166 				   (DIF_BUNDLING_DMA_POOL_SIZE - ldma_sg_len)) ?
1167 				    DIF_BUNDLING_DMA_POOL_SIZE - ldma_sg_len :
1168 				    sglen;
1169 
1170 				/* replace with local allocated dma buffer */
1171 				sg_pcopy_to_buffer(sgl, sg_nents(sgl),
1172 				    dsd_ptr->dsd_addr + ldma_sg_len, xfrlen,
1173 				    difctx->dif_bundl_len);
1174 				difctx->dif_bundl_len += xfrlen;
1175 				sglen -= xfrlen;
1176 				ldma_sg_len += xfrlen;
1177 				if (ldma_sg_len == DIF_BUNDLING_DMA_POOL_SIZE ||
1178 				    sg_is_last(sg)) {
1179 					ldma_needed = 1;
1180 					ldma_sg_len = 0;
1181 				}
1182 			}
1183 		}
1184 
1185 		track_difbundl_buf = used_dsds = difctx->no_dif_bundl;
1186 		ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe025,
1187 		    "dif_bundl_len=%x, no_dif_bundl=%x track_difbundl_buf: %x\n",
1188 		    difctx->dif_bundl_len, difctx->no_dif_bundl,
1189 		    track_difbundl_buf);
1190 
1191 		if (sp)
1192 			sp->flags |= SRB_DIF_BUNDL_DMA_VALID;
1193 		else
1194 			tc->prot_flags = DIF_BUNDL_DMA_VALID;
1195 
1196 		list_for_each_entry_safe(dif_dsd, nxt_dsd,
1197 		    &difctx->ldif_dma_hndl_list, list) {
1198 			u32 sglen = (difctx->dif_bundl_len >
1199 			    DIF_BUNDLING_DMA_POOL_SIZE) ?
1200 			    DIF_BUNDLING_DMA_POOL_SIZE : difctx->dif_bundl_len;
1201 
1202 			BUG_ON(track_difbundl_buf == 0);
1203 
1204 			/* Allocate additional continuation packets? */
1205 			if (avail_dsds == 0) {
1206 				ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha,
1207 				    0xe024,
1208 				    "%s: adding continuation iocb's\n",
1209 				    __func__);
1210 				avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1211 				    QLA_DSDS_PER_IOCB : used_dsds;
1212 				dsd_list_len = (avail_dsds + 1) * 12;
1213 				used_dsds -= avail_dsds;
1214 
1215 				/* allocate tracking DS */
1216 				dsd_ptr = kzalloc(sizeof(*dsd_ptr), GFP_ATOMIC);
1217 				if (!dsd_ptr) {
1218 					ql_dbg(ql_dbg_tgt, vha, 0xe026,
1219 					    "%s: failed alloc dsd_ptr\n",
1220 					    __func__);
1221 					return 1;
1222 				}
1223 				ha->dif_bundle_kallocs++;
1224 
1225 				difctx->no_ldif_dsd++;
1226 				/* allocate new list */
1227 				dsd_ptr->dsd_addr =
1228 				    dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1229 					&dsd_ptr->dsd_list_dma);
1230 				if (!dsd_ptr->dsd_addr) {
1231 					ql_dbg(ql_dbg_tgt, vha, 0xe026,
1232 					    "%s: failed alloc ->dsd_addr\n",
1233 					    __func__);
1234 					/*
1235 					 * need to cleanup only this dsd_ptr
1236 					 *  rest will be done by sp_free_dma()
1237 					 */
1238 					kfree(dsd_ptr);
1239 					ha->dif_bundle_kallocs--;
1240 					return 1;
1241 				}
1242 				ha->dif_bundle_dma_allocs++;
1243 
1244 				if (sp) {
1245 					list_add_tail(&dsd_ptr->list,
1246 					    &difctx->ldif_dsd_list);
1247 					sp->flags |= SRB_CRC_CTX_DSD_VALID;
1248 				} else {
1249 					list_add_tail(&dsd_ptr->list,
1250 					    &difctx->ldif_dsd_list);
1251 					tc->ctx_dsd_alloced = 1;
1252 				}
1253 
1254 				/* add new list to cmd iocb or last list */
1255 				put_unaligned_le64(dsd_ptr->dsd_list_dma,
1256 						   &cur_dsd->address);
1257 				cur_dsd->length = cpu_to_le32(dsd_list_len);
1258 				cur_dsd = dsd_ptr->dsd_addr;
1259 			}
1260 			put_unaligned_le64(dif_dsd->dsd_list_dma,
1261 					   &cur_dsd->address);
1262 			cur_dsd->length = cpu_to_le32(sglen);
1263 			cur_dsd++;
1264 			avail_dsds--;
1265 			difctx->dif_bundl_len -= sglen;
1266 			track_difbundl_buf--;
1267 		}
1268 
1269 		ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe026,
1270 		    "%s: no_ldif_dsd:%x, no_dif_bundl:%x\n", __func__,
1271 			difctx->no_ldif_dsd, difctx->no_dif_bundl);
1272 	} else {
1273 		for_each_sg(sgl, sg, tot_dsds, i) {
1274 			/* Allocate additional continuation packets? */
1275 			if (avail_dsds == 0) {
1276 				avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1277 				    QLA_DSDS_PER_IOCB : used_dsds;
1278 				dsd_list_len = (avail_dsds + 1) * 12;
1279 				used_dsds -= avail_dsds;
1280 
1281 				/* allocate tracking DS */
1282 				dsd_ptr = kzalloc(sizeof(*dsd_ptr), GFP_ATOMIC);
1283 				if (!dsd_ptr) {
1284 					ql_dbg(ql_dbg_tgt + ql_dbg_verbose,
1285 					    vha, 0xe027,
1286 					    "%s: failed alloc dsd_dma...\n",
1287 					    __func__);
1288 					return 1;
1289 				}
1290 
1291 				/* allocate new list */
1292 				dsd_ptr->dsd_addr =
1293 				    dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1294 					&dsd_ptr->dsd_list_dma);
1295 				if (!dsd_ptr->dsd_addr) {
1296 					/* need to cleanup only this dsd_ptr */
1297 					/* rest will be done by sp_free_dma() */
1298 					kfree(dsd_ptr);
1299 					return 1;
1300 				}
1301 
1302 				if (sp) {
1303 					list_add_tail(&dsd_ptr->list,
1304 					    &difctx->dsd_list);
1305 					sp->flags |= SRB_CRC_CTX_DSD_VALID;
1306 				} else {
1307 					list_add_tail(&dsd_ptr->list,
1308 					    &difctx->dsd_list);
1309 					tc->ctx_dsd_alloced = 1;
1310 				}
1311 
1312 				/* add new list to cmd iocb or last list */
1313 				put_unaligned_le64(dsd_ptr->dsd_list_dma,
1314 						   &cur_dsd->address);
1315 				cur_dsd->length = cpu_to_le32(dsd_list_len);
1316 				cur_dsd = dsd_ptr->dsd_addr;
1317 			}
1318 			append_dsd64(&cur_dsd, sg);
1319 			avail_dsds--;
1320 		}
1321 	}
1322 	/* Null termination */
1323 	cur_dsd->address = 0;
1324 	cur_dsd->length = 0;
1325 	cur_dsd++;
1326 	return 0;
1327 }
1328 
1329 /**
1330  * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1331  *							Type 6 IOCB types.
1332  *
1333  * @sp: SRB command to process
1334  * @cmd_pkt: Command type 3 IOCB
1335  * @tot_dsds: Total number of segments to transfer
1336  * @tot_prot_dsds: Total number of segments with protection information
1337  * @fw_prot_opts: Protection options to be passed to firmware
1338  */
1339 static inline int
1340 qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1341     uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1342 {
1343 	struct dsd64		*cur_dsd;
1344 	__be32			*fcp_dl;
1345 	scsi_qla_host_t		*vha;
1346 	struct scsi_cmnd	*cmd;
1347 	uint32_t		total_bytes = 0;
1348 	uint32_t		data_bytes;
1349 	uint32_t		dif_bytes;
1350 	uint8_t			bundling = 1;
1351 	uint16_t		blk_size;
1352 	struct crc_context	*crc_ctx_pkt = NULL;
1353 	struct qla_hw_data	*ha;
1354 	uint8_t			additional_fcpcdb_len;
1355 	uint16_t		fcp_cmnd_len;
1356 	struct fcp_cmnd		*fcp_cmnd;
1357 	dma_addr_t		crc_ctx_dma;
1358 
1359 	cmd = GET_CMD_SP(sp);
1360 
1361 	/* Update entry type to indicate Command Type CRC_2 IOCB */
1362 	put_unaligned_le32(COMMAND_TYPE_CRC_2, &cmd_pkt->entry_type);
1363 
1364 	vha = sp->vha;
1365 	ha = vha->hw;
1366 
1367 	/* No data transfer */
1368 	data_bytes = scsi_bufflen(cmd);
1369 	if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1370 		cmd_pkt->byte_count = cpu_to_le32(0);
1371 		return QLA_SUCCESS;
1372 	}
1373 
1374 	cmd_pkt->vp_index = sp->vha->vp_idx;
1375 
1376 	/* Set transfer direction */
1377 	if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1378 		cmd_pkt->control_flags =
1379 		    cpu_to_le16(CF_WRITE_DATA);
1380 	} else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1381 		cmd_pkt->control_flags =
1382 		    cpu_to_le16(CF_READ_DATA);
1383 	}
1384 
1385 	if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1386 	    (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1387 	    (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1388 	    (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
1389 		bundling = 0;
1390 
1391 	/* Allocate CRC context from global pool */
1392 	crc_ctx_pkt = sp->u.scmd.crc_ctx =
1393 	    dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
1394 
1395 	if (!crc_ctx_pkt)
1396 		goto crc_queuing_error;
1397 
1398 	crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1399 
1400 	sp->flags |= SRB_CRC_CTX_DMA_VALID;
1401 
1402 	/* Set handle */
1403 	crc_ctx_pkt->handle = cmd_pkt->handle;
1404 
1405 	INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1406 
1407 	qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
1408 	    &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1409 
1410 	put_unaligned_le64(crc_ctx_dma, &cmd_pkt->crc_context_address);
1411 	cmd_pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
1412 
1413 	/* Determine SCSI command length -- align to 4 byte boundary */
1414 	if (cmd->cmd_len > 16) {
1415 		additional_fcpcdb_len = cmd->cmd_len - 16;
1416 		if ((cmd->cmd_len % 4) != 0) {
1417 			/* SCSI cmd > 16 bytes must be multiple of 4 */
1418 			goto crc_queuing_error;
1419 		}
1420 		fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1421 	} else {
1422 		additional_fcpcdb_len = 0;
1423 		fcp_cmnd_len = 12 + 16 + 4;
1424 	}
1425 
1426 	fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1427 
1428 	fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1429 	if (cmd->sc_data_direction == DMA_TO_DEVICE)
1430 		fcp_cmnd->additional_cdb_len |= 1;
1431 	else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1432 		fcp_cmnd->additional_cdb_len |= 2;
1433 
1434 	int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
1435 	memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1436 	cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1437 	put_unaligned_le64(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF,
1438 			   &cmd_pkt->fcp_cmnd_dseg_address);
1439 	fcp_cmnd->task_management = 0;
1440 	fcp_cmnd->task_attribute = TSK_SIMPLE;
1441 
1442 	cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1443 
1444 	/* Compute dif len and adjust data len to incude protection */
1445 	dif_bytes = 0;
1446 	blk_size = cmd->device->sector_size;
1447 	dif_bytes = (data_bytes / blk_size) * 8;
1448 
1449 	switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
1450 	case SCSI_PROT_READ_INSERT:
1451 	case SCSI_PROT_WRITE_STRIP:
1452 		total_bytes = data_bytes;
1453 		data_bytes += dif_bytes;
1454 		break;
1455 
1456 	case SCSI_PROT_READ_STRIP:
1457 	case SCSI_PROT_WRITE_INSERT:
1458 	case SCSI_PROT_READ_PASS:
1459 	case SCSI_PROT_WRITE_PASS:
1460 		total_bytes = data_bytes + dif_bytes;
1461 		break;
1462 	default:
1463 		BUG();
1464 	}
1465 
1466 	if (!qla2x00_hba_err_chk_enabled(sp))
1467 		fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1468 	/* HBA error checking enabled */
1469 	else if (IS_PI_UNINIT_CAPABLE(ha)) {
1470 		if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
1471 		    || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1472 			SCSI_PROT_DIF_TYPE2))
1473 			fw_prot_opts |= BIT_10;
1474 		else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1475 		    SCSI_PROT_DIF_TYPE3)
1476 			fw_prot_opts |= BIT_11;
1477 	}
1478 
1479 	if (!bundling) {
1480 		cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
1481 	} else {
1482 		/*
1483 		 * Configure Bundling if we need to fetch interlaving
1484 		 * protection PCI accesses
1485 		 */
1486 		fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1487 		crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1488 		crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1489 							tot_prot_dsds);
1490 		cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
1491 	}
1492 
1493 	/* Finish the common fields of CRC pkt */
1494 	crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1495 	crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1496 	crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1497 	crc_ctx_pkt->guard_seed = cpu_to_le16(0);
1498 	/* Fibre channel byte count */
1499 	cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1500 	fcp_dl = (__be32 *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1501 	    additional_fcpcdb_len);
1502 	*fcp_dl = htonl(total_bytes);
1503 
1504 	if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1505 		cmd_pkt->byte_count = cpu_to_le32(0);
1506 		return QLA_SUCCESS;
1507 	}
1508 	/* Walks data segments */
1509 
1510 	cmd_pkt->control_flags |= cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
1511 
1512 	if (!bundling && tot_prot_dsds) {
1513 		if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1514 			cur_dsd, tot_dsds, NULL))
1515 			goto crc_queuing_error;
1516 	} else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
1517 			(tot_dsds - tot_prot_dsds), NULL))
1518 		goto crc_queuing_error;
1519 
1520 	if (bundling && tot_prot_dsds) {
1521 		/* Walks dif segments */
1522 		cmd_pkt->control_flags |= cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1523 		cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
1524 		if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1525 				tot_prot_dsds, NULL))
1526 			goto crc_queuing_error;
1527 	}
1528 	return QLA_SUCCESS;
1529 
1530 crc_queuing_error:
1531 	/* Cleanup will be performed by the caller */
1532 
1533 	return QLA_FUNCTION_FAILED;
1534 }
1535 
1536 /**
1537  * qla24xx_start_scsi() - Send a SCSI command to the ISP
1538  * @sp: command to send to the ISP
1539  *
1540  * Returns non-zero if a failure occurred, else zero.
1541  */
1542 int
1543 qla24xx_start_scsi(srb_t *sp)
1544 {
1545 	int		nseg;
1546 	unsigned long   flags;
1547 	uint32_t	*clr_ptr;
1548 	uint32_t	handle;
1549 	struct cmd_type_7 *cmd_pkt;
1550 	uint16_t	cnt;
1551 	uint16_t	req_cnt;
1552 	uint16_t	tot_dsds;
1553 	struct req_que *req = NULL;
1554 	struct rsp_que *rsp;
1555 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1556 	struct scsi_qla_host *vha = sp->vha;
1557 	struct qla_hw_data *ha = vha->hw;
1558 
1559 	if (sp->fcport->edif.enable  && (sp->fcport->flags & FCF_FCSP_DEVICE))
1560 		return qla28xx_start_scsi_edif(sp);
1561 
1562 	/* Setup device pointers. */
1563 	req = vha->req;
1564 	rsp = req->rsp;
1565 
1566 	/* So we know we haven't pci_map'ed anything yet */
1567 	tot_dsds = 0;
1568 
1569 	/* Send marker if required */
1570 	if (vha->marker_needed != 0) {
1571 		if (qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL) !=
1572 		    QLA_SUCCESS)
1573 			return QLA_FUNCTION_FAILED;
1574 		vha->marker_needed = 0;
1575 	}
1576 
1577 	/* Acquire ring specific lock */
1578 	spin_lock_irqsave(&ha->hardware_lock, flags);
1579 
1580 	handle = qla2xxx_get_next_handle(req);
1581 	if (handle == 0)
1582 		goto queuing_error;
1583 
1584 	/* Map the sg table so we have an accurate count of sg entries needed */
1585 	if (scsi_sg_count(cmd)) {
1586 		nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1587 		    scsi_sg_count(cmd), cmd->sc_data_direction);
1588 		if (unlikely(!nseg))
1589 			goto queuing_error;
1590 	} else
1591 		nseg = 0;
1592 
1593 	tot_dsds = nseg;
1594 	req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
1595 
1596 	sp->iores.res_type = RESOURCE_IOCB | RESOURCE_EXCH;
1597 	sp->iores.exch_cnt = 1;
1598 	sp->iores.iocb_cnt = req_cnt;
1599 	if (qla_get_fw_resources(sp->qpair, &sp->iores))
1600 		goto queuing_error;
1601 
1602 	if (req->cnt < (req_cnt + 2)) {
1603 		if (IS_SHADOW_REG_CAPABLE(ha)) {
1604 			cnt = *req->out_ptr;
1605 		} else {
1606 			cnt = rd_reg_dword_relaxed(req->req_q_out);
1607 			if (qla2x00_check_reg16_for_disconnect(vha, cnt))
1608 				goto queuing_error;
1609 		}
1610 
1611 		if (req->ring_index < cnt)
1612 			req->cnt = cnt - req->ring_index;
1613 		else
1614 			req->cnt = req->length -
1615 				(req->ring_index - cnt);
1616 		if (req->cnt < (req_cnt + 2))
1617 			goto queuing_error;
1618 	}
1619 
1620 	/* Build command packet. */
1621 	req->current_outstanding_cmd = handle;
1622 	req->outstanding_cmds[handle] = sp;
1623 	sp->handle = handle;
1624 	cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1625 	req->cnt -= req_cnt;
1626 
1627 	cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
1628 	cmd_pkt->handle = make_handle(req->id, handle);
1629 
1630 	/* Zero out remaining portion of packet. */
1631 	/*    tagged queuing modifier -- default is TSK_SIMPLE (0). */
1632 	clr_ptr = (uint32_t *)cmd_pkt + 2;
1633 	memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1634 	cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1635 
1636 	/* Set NPORT-ID and LUN number*/
1637 	cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1638 	cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1639 	cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1640 	cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1641 	cmd_pkt->vp_index = sp->vha->vp_idx;
1642 
1643 	int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
1644 	host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1645 
1646 	cmd_pkt->task = TSK_SIMPLE;
1647 
1648 	/* Load SCSI command packet. */
1649 	memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1650 	host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1651 
1652 	cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1653 
1654 	/* Build IOCB segments */
1655 	qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);
1656 
1657 	/* Set total data segment count. */
1658 	cmd_pkt->entry_count = (uint8_t)req_cnt;
1659 	wmb();
1660 	/* Adjust ring index. */
1661 	req->ring_index++;
1662 	if (req->ring_index == req->length) {
1663 		req->ring_index = 0;
1664 		req->ring_ptr = req->ring;
1665 	} else
1666 		req->ring_ptr++;
1667 
1668 	sp->qpair->cmd_cnt++;
1669 	sp->flags |= SRB_DMA_VALID;
1670 
1671 	/* Set chip new ring index. */
1672 	wrt_reg_dword(req->req_q_in, req->ring_index);
1673 
1674 	/* Manage unprocessed RIO/ZIO commands in response queue. */
1675 	if (vha->flags.process_response_queue &&
1676 	    rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1677 		qla24xx_process_response_queue(vha, rsp);
1678 
1679 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1680 	return QLA_SUCCESS;
1681 
1682 queuing_error:
1683 	if (tot_dsds)
1684 		scsi_dma_unmap(cmd);
1685 
1686 	qla_put_fw_resources(sp->qpair, &sp->iores);
1687 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1688 
1689 	return QLA_FUNCTION_FAILED;
1690 }
1691 
1692 /**
1693  * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1694  * @sp: command to send to the ISP
1695  *
1696  * Returns non-zero if a failure occurred, else zero.
1697  */
1698 int
1699 qla24xx_dif_start_scsi(srb_t *sp)
1700 {
1701 	int			nseg;
1702 	unsigned long		flags;
1703 	uint32_t		*clr_ptr;
1704 	uint32_t		handle;
1705 	uint16_t		cnt;
1706 	uint16_t		req_cnt = 0;
1707 	uint16_t		tot_dsds;
1708 	uint16_t		tot_prot_dsds;
1709 	uint16_t		fw_prot_opts = 0;
1710 	struct req_que		*req = NULL;
1711 	struct rsp_que		*rsp = NULL;
1712 	struct scsi_cmnd	*cmd = GET_CMD_SP(sp);
1713 	struct scsi_qla_host	*vha = sp->vha;
1714 	struct qla_hw_data	*ha = vha->hw;
1715 	struct cmd_type_crc_2	*cmd_pkt;
1716 	uint32_t		status = 0;
1717 
1718 #define QDSS_GOT_Q_SPACE	BIT_0
1719 
1720 	/* Only process protection or >16 cdb in this routine */
1721 	if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1722 		if (cmd->cmd_len <= 16)
1723 			return qla24xx_start_scsi(sp);
1724 	}
1725 
1726 	/* Setup device pointers. */
1727 	req = vha->req;
1728 	rsp = req->rsp;
1729 
1730 	/* So we know we haven't pci_map'ed anything yet */
1731 	tot_dsds = 0;
1732 
1733 	/* Send marker if required */
1734 	if (vha->marker_needed != 0) {
1735 		if (qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL) !=
1736 		    QLA_SUCCESS)
1737 			return QLA_FUNCTION_FAILED;
1738 		vha->marker_needed = 0;
1739 	}
1740 
1741 	/* Acquire ring specific lock */
1742 	spin_lock_irqsave(&ha->hardware_lock, flags);
1743 
1744 	handle = qla2xxx_get_next_handle(req);
1745 	if (handle == 0)
1746 		goto queuing_error;
1747 
1748 	/* Compute number of required data segments */
1749 	/* Map the sg table so we have an accurate count of sg entries needed */
1750 	if (scsi_sg_count(cmd)) {
1751 		nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1752 		    scsi_sg_count(cmd), cmd->sc_data_direction);
1753 		if (unlikely(!nseg))
1754 			goto queuing_error;
1755 		else
1756 			sp->flags |= SRB_DMA_VALID;
1757 
1758 		if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1759 		    (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1760 			struct qla2_sgx sgx;
1761 			uint32_t	partial;
1762 
1763 			memset(&sgx, 0, sizeof(struct qla2_sgx));
1764 			sgx.tot_bytes = scsi_bufflen(cmd);
1765 			sgx.cur_sg = scsi_sglist(cmd);
1766 			sgx.sp = sp;
1767 
1768 			nseg = 0;
1769 			while (qla24xx_get_one_block_sg(
1770 			    cmd->device->sector_size, &sgx, &partial))
1771 				nseg++;
1772 		}
1773 	} else
1774 		nseg = 0;
1775 
1776 	/* number of required data segments */
1777 	tot_dsds = nseg;
1778 
1779 	/* Compute number of required protection segments */
1780 	if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1781 		nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1782 		    scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1783 		if (unlikely(!nseg))
1784 			goto queuing_error;
1785 		else
1786 			sp->flags |= SRB_CRC_PROT_DMA_VALID;
1787 
1788 		if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1789 		    (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1790 			nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1791 		}
1792 	} else {
1793 		nseg = 0;
1794 	}
1795 
1796 	req_cnt = 1;
1797 	/* Total Data and protection sg segment(s) */
1798 	tot_prot_dsds = nseg;
1799 	tot_dsds += nseg;
1800 
1801 	sp->iores.res_type = RESOURCE_IOCB | RESOURCE_EXCH;
1802 	sp->iores.exch_cnt = 1;
1803 	sp->iores.iocb_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
1804 	if (qla_get_fw_resources(sp->qpair, &sp->iores))
1805 		goto queuing_error;
1806 
1807 	if (req->cnt < (req_cnt + 2)) {
1808 		if (IS_SHADOW_REG_CAPABLE(ha)) {
1809 			cnt = *req->out_ptr;
1810 		} else {
1811 			cnt = rd_reg_dword_relaxed(req->req_q_out);
1812 			if (qla2x00_check_reg16_for_disconnect(vha, cnt))
1813 				goto queuing_error;
1814 		}
1815 		if (req->ring_index < cnt)
1816 			req->cnt = cnt - req->ring_index;
1817 		else
1818 			req->cnt = req->length -
1819 				(req->ring_index - cnt);
1820 		if (req->cnt < (req_cnt + 2))
1821 			goto queuing_error;
1822 	}
1823 
1824 	status |= QDSS_GOT_Q_SPACE;
1825 
1826 	/* Build header part of command packet (excluding the OPCODE). */
1827 	req->current_outstanding_cmd = handle;
1828 	req->outstanding_cmds[handle] = sp;
1829 	sp->handle = handle;
1830 	cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1831 	req->cnt -= req_cnt;
1832 
1833 	/* Fill-in common area */
1834 	cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1835 	cmd_pkt->handle = make_handle(req->id, handle);
1836 
1837 	clr_ptr = (uint32_t *)cmd_pkt + 2;
1838 	memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1839 
1840 	/* Set NPORT-ID and LUN number*/
1841 	cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1842 	cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1843 	cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1844 	cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1845 
1846 	int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
1847 	host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1848 
1849 	/* Total Data and protection segment(s) */
1850 	cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1851 
1852 	/* Build IOCB segments and adjust for data protection segments */
1853 	if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1854 	    req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1855 		QLA_SUCCESS)
1856 		goto queuing_error;
1857 
1858 	cmd_pkt->entry_count = (uint8_t)req_cnt;
1859 	/* Specify response queue number where completion should happen */
1860 	cmd_pkt->entry_status = (uint8_t) rsp->id;
1861 	cmd_pkt->timeout = cpu_to_le16(0);
1862 	wmb();
1863 
1864 	/* Adjust ring index. */
1865 	req->ring_index++;
1866 	if (req->ring_index == req->length) {
1867 		req->ring_index = 0;
1868 		req->ring_ptr = req->ring;
1869 	} else
1870 		req->ring_ptr++;
1871 
1872 	sp->qpair->cmd_cnt++;
1873 	/* Set chip new ring index. */
1874 	wrt_reg_dword(req->req_q_in, req->ring_index);
1875 
1876 	/* Manage unprocessed RIO/ZIO commands in response queue. */
1877 	if (vha->flags.process_response_queue &&
1878 	    rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1879 		qla24xx_process_response_queue(vha, rsp);
1880 
1881 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1882 
1883 	return QLA_SUCCESS;
1884 
1885 queuing_error:
1886 	if (status & QDSS_GOT_Q_SPACE) {
1887 		req->outstanding_cmds[handle] = NULL;
1888 		req->cnt += req_cnt;
1889 	}
1890 	/* Cleanup will be performed by the caller (queuecommand) */
1891 
1892 	qla_put_fw_resources(sp->qpair, &sp->iores);
1893 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1894 
1895 	return QLA_FUNCTION_FAILED;
1896 }
1897 
1898 /**
1899  * qla2xxx_start_scsi_mq() - Send a SCSI command to the ISP
1900  * @sp: command to send to the ISP
1901  *
1902  * Returns non-zero if a failure occurred, else zero.
1903  */
1904 static int
1905 qla2xxx_start_scsi_mq(srb_t *sp)
1906 {
1907 	int		nseg;
1908 	unsigned long   flags;
1909 	uint32_t	*clr_ptr;
1910 	uint32_t	handle;
1911 	struct cmd_type_7 *cmd_pkt;
1912 	uint16_t	cnt;
1913 	uint16_t	req_cnt;
1914 	uint16_t	tot_dsds;
1915 	struct req_que *req = NULL;
1916 	struct rsp_que *rsp;
1917 	struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1918 	struct scsi_qla_host *vha = sp->fcport->vha;
1919 	struct qla_hw_data *ha = vha->hw;
1920 	struct qla_qpair *qpair = sp->qpair;
1921 
1922 	if (sp->fcport->edif.enable && (sp->fcport->flags & FCF_FCSP_DEVICE))
1923 		return qla28xx_start_scsi_edif(sp);
1924 
1925 	/* Acquire qpair specific lock */
1926 	spin_lock_irqsave(&qpair->qp_lock, flags);
1927 
1928 	/* Setup qpair pointers */
1929 	req = qpair->req;
1930 	rsp = qpair->rsp;
1931 
1932 	/* So we know we haven't pci_map'ed anything yet */
1933 	tot_dsds = 0;
1934 
1935 	/* Send marker if required */
1936 	if (vha->marker_needed != 0) {
1937 		if (__qla2x00_marker(vha, qpair, 0, 0, MK_SYNC_ALL) !=
1938 		    QLA_SUCCESS) {
1939 			spin_unlock_irqrestore(&qpair->qp_lock, flags);
1940 			return QLA_FUNCTION_FAILED;
1941 		}
1942 		vha->marker_needed = 0;
1943 	}
1944 
1945 	handle = qla2xxx_get_next_handle(req);
1946 	if (handle == 0)
1947 		goto queuing_error;
1948 
1949 	/* Map the sg table so we have an accurate count of sg entries needed */
1950 	if (scsi_sg_count(cmd)) {
1951 		nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1952 		    scsi_sg_count(cmd), cmd->sc_data_direction);
1953 		if (unlikely(!nseg))
1954 			goto queuing_error;
1955 	} else
1956 		nseg = 0;
1957 
1958 	tot_dsds = nseg;
1959 	req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
1960 
1961 	sp->iores.res_type = RESOURCE_IOCB | RESOURCE_EXCH;
1962 	sp->iores.exch_cnt = 1;
1963 	sp->iores.iocb_cnt = req_cnt;
1964 	if (qla_get_fw_resources(sp->qpair, &sp->iores))
1965 		goto queuing_error;
1966 
1967 	if (req->cnt < (req_cnt + 2)) {
1968 		if (IS_SHADOW_REG_CAPABLE(ha)) {
1969 			cnt = *req->out_ptr;
1970 		} else {
1971 			cnt = rd_reg_dword_relaxed(req->req_q_out);
1972 			if (qla2x00_check_reg16_for_disconnect(vha, cnt))
1973 				goto queuing_error;
1974 		}
1975 
1976 		if (req->ring_index < cnt)
1977 			req->cnt = cnt - req->ring_index;
1978 		else
1979 			req->cnt = req->length -
1980 				(req->ring_index - cnt);
1981 		if (req->cnt < (req_cnt + 2))
1982 			goto queuing_error;
1983 	}
1984 
1985 	/* Build command packet. */
1986 	req->current_outstanding_cmd = handle;
1987 	req->outstanding_cmds[handle] = sp;
1988 	sp->handle = handle;
1989 	cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1990 	req->cnt -= req_cnt;
1991 
1992 	cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
1993 	cmd_pkt->handle = make_handle(req->id, handle);
1994 
1995 	/* Zero out remaining portion of packet. */
1996 	/*    tagged queuing modifier -- default is TSK_SIMPLE (0). */
1997 	clr_ptr = (uint32_t *)cmd_pkt + 2;
1998 	memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1999 	cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2000 
2001 	/* Set NPORT-ID and LUN number*/
2002 	cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2003 	cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2004 	cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2005 	cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2006 	cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
2007 
2008 	int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
2009 	host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2010 
2011 	cmd_pkt->task = TSK_SIMPLE;
2012 
2013 	/* Load SCSI command packet. */
2014 	memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
2015 	host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
2016 
2017 	cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2018 
2019 	/* Build IOCB segments */
2020 	qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);
2021 
2022 	/* Set total data segment count. */
2023 	cmd_pkt->entry_count = (uint8_t)req_cnt;
2024 	wmb();
2025 	/* Adjust ring index. */
2026 	req->ring_index++;
2027 	if (req->ring_index == req->length) {
2028 		req->ring_index = 0;
2029 		req->ring_ptr = req->ring;
2030 	} else
2031 		req->ring_ptr++;
2032 
2033 	sp->qpair->cmd_cnt++;
2034 	sp->flags |= SRB_DMA_VALID;
2035 
2036 	/* Set chip new ring index. */
2037 	wrt_reg_dword(req->req_q_in, req->ring_index);
2038 
2039 	/* Manage unprocessed RIO/ZIO commands in response queue. */
2040 	if (vha->flags.process_response_queue &&
2041 	    rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2042 		qla24xx_process_response_queue(vha, rsp);
2043 
2044 	spin_unlock_irqrestore(&qpair->qp_lock, flags);
2045 	return QLA_SUCCESS;
2046 
2047 queuing_error:
2048 	if (tot_dsds)
2049 		scsi_dma_unmap(cmd);
2050 
2051 	qla_put_fw_resources(sp->qpair, &sp->iores);
2052 	spin_unlock_irqrestore(&qpair->qp_lock, flags);
2053 
2054 	return QLA_FUNCTION_FAILED;
2055 }
2056 
2057 
2058 /**
2059  * qla2xxx_dif_start_scsi_mq() - Send a SCSI command to the ISP
2060  * @sp: command to send to the ISP
2061  *
2062  * Returns non-zero if a failure occurred, else zero.
2063  */
2064 int
2065 qla2xxx_dif_start_scsi_mq(srb_t *sp)
2066 {
2067 	int			nseg;
2068 	unsigned long		flags;
2069 	uint32_t		*clr_ptr;
2070 	uint32_t		handle;
2071 	uint16_t		cnt;
2072 	uint16_t		req_cnt = 0;
2073 	uint16_t		tot_dsds;
2074 	uint16_t		tot_prot_dsds;
2075 	uint16_t		fw_prot_opts = 0;
2076 	struct req_que		*req = NULL;
2077 	struct rsp_que		*rsp = NULL;
2078 	struct scsi_cmnd	*cmd = GET_CMD_SP(sp);
2079 	struct scsi_qla_host	*vha = sp->fcport->vha;
2080 	struct qla_hw_data	*ha = vha->hw;
2081 	struct cmd_type_crc_2	*cmd_pkt;
2082 	uint32_t		status = 0;
2083 	struct qla_qpair	*qpair = sp->qpair;
2084 
2085 #define QDSS_GOT_Q_SPACE	BIT_0
2086 
2087 	/* Check for host side state */
2088 	if (!qpair->online) {
2089 		cmd->result = DID_NO_CONNECT << 16;
2090 		return QLA_INTERFACE_ERROR;
2091 	}
2092 
2093 	if (!qpair->difdix_supported &&
2094 		scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2095 		cmd->result = DID_NO_CONNECT << 16;
2096 		return QLA_INTERFACE_ERROR;
2097 	}
2098 
2099 	/* Only process protection or >16 cdb in this routine */
2100 	if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
2101 		if (cmd->cmd_len <= 16)
2102 			return qla2xxx_start_scsi_mq(sp);
2103 	}
2104 
2105 	spin_lock_irqsave(&qpair->qp_lock, flags);
2106 
2107 	/* Setup qpair pointers */
2108 	rsp = qpair->rsp;
2109 	req = qpair->req;
2110 
2111 	/* So we know we haven't pci_map'ed anything yet */
2112 	tot_dsds = 0;
2113 
2114 	/* Send marker if required */
2115 	if (vha->marker_needed != 0) {
2116 		if (__qla2x00_marker(vha, qpair, 0, 0, MK_SYNC_ALL) !=
2117 		    QLA_SUCCESS) {
2118 			spin_unlock_irqrestore(&qpair->qp_lock, flags);
2119 			return QLA_FUNCTION_FAILED;
2120 		}
2121 		vha->marker_needed = 0;
2122 	}
2123 
2124 	handle = qla2xxx_get_next_handle(req);
2125 	if (handle == 0)
2126 		goto queuing_error;
2127 
2128 	/* Compute number of required data segments */
2129 	/* Map the sg table so we have an accurate count of sg entries needed */
2130 	if (scsi_sg_count(cmd)) {
2131 		nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2132 		    scsi_sg_count(cmd), cmd->sc_data_direction);
2133 		if (unlikely(!nseg))
2134 			goto queuing_error;
2135 		else
2136 			sp->flags |= SRB_DMA_VALID;
2137 
2138 		if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
2139 		    (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
2140 			struct qla2_sgx sgx;
2141 			uint32_t	partial;
2142 
2143 			memset(&sgx, 0, sizeof(struct qla2_sgx));
2144 			sgx.tot_bytes = scsi_bufflen(cmd);
2145 			sgx.cur_sg = scsi_sglist(cmd);
2146 			sgx.sp = sp;
2147 
2148 			nseg = 0;
2149 			while (qla24xx_get_one_block_sg(
2150 			    cmd->device->sector_size, &sgx, &partial))
2151 				nseg++;
2152 		}
2153 	} else
2154 		nseg = 0;
2155 
2156 	/* number of required data segments */
2157 	tot_dsds = nseg;
2158 
2159 	/* Compute number of required protection segments */
2160 	if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
2161 		nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
2162 		    scsi_prot_sg_count(cmd), cmd->sc_data_direction);
2163 		if (unlikely(!nseg))
2164 			goto queuing_error;
2165 		else
2166 			sp->flags |= SRB_CRC_PROT_DMA_VALID;
2167 
2168 		if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
2169 		    (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
2170 			nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
2171 		}
2172 	} else {
2173 		nseg = 0;
2174 	}
2175 
2176 	req_cnt = 1;
2177 	/* Total Data and protection sg segment(s) */
2178 	tot_prot_dsds = nseg;
2179 	tot_dsds += nseg;
2180 
2181 	sp->iores.res_type = RESOURCE_IOCB | RESOURCE_EXCH;
2182 	sp->iores.exch_cnt = 1;
2183 	sp->iores.iocb_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2184 	if (qla_get_fw_resources(sp->qpair, &sp->iores))
2185 		goto queuing_error;
2186 
2187 	if (req->cnt < (req_cnt + 2)) {
2188 		if (IS_SHADOW_REG_CAPABLE(ha)) {
2189 			cnt = *req->out_ptr;
2190 		} else {
2191 			cnt = rd_reg_dword_relaxed(req->req_q_out);
2192 			if (qla2x00_check_reg16_for_disconnect(vha, cnt))
2193 				goto queuing_error;
2194 		}
2195 
2196 		if (req->ring_index < cnt)
2197 			req->cnt = cnt - req->ring_index;
2198 		else
2199 			req->cnt = req->length -
2200 				(req->ring_index - cnt);
2201 		if (req->cnt < (req_cnt + 2))
2202 			goto queuing_error;
2203 	}
2204 
2205 	status |= QDSS_GOT_Q_SPACE;
2206 
2207 	/* Build header part of command packet (excluding the OPCODE). */
2208 	req->current_outstanding_cmd = handle;
2209 	req->outstanding_cmds[handle] = sp;
2210 	sp->handle = handle;
2211 	cmd->host_scribble = (unsigned char *)(unsigned long)handle;
2212 	req->cnt -= req_cnt;
2213 
2214 	/* Fill-in common area */
2215 	cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
2216 	cmd_pkt->handle = make_handle(req->id, handle);
2217 
2218 	clr_ptr = (uint32_t *)cmd_pkt + 2;
2219 	memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2220 
2221 	/* Set NPORT-ID and LUN number*/
2222 	cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2223 	cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2224 	cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2225 	cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2226 
2227 	int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
2228 	host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2229 
2230 	/* Total Data and protection segment(s) */
2231 	cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2232 
2233 	/* Build IOCB segments and adjust for data protection segments */
2234 	if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
2235 	    req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
2236 		QLA_SUCCESS)
2237 		goto queuing_error;
2238 
2239 	cmd_pkt->entry_count = (uint8_t)req_cnt;
2240 	cmd_pkt->timeout = cpu_to_le16(0);
2241 	wmb();
2242 
2243 	/* Adjust ring index. */
2244 	req->ring_index++;
2245 	if (req->ring_index == req->length) {
2246 		req->ring_index = 0;
2247 		req->ring_ptr = req->ring;
2248 	} else
2249 		req->ring_ptr++;
2250 
2251 	sp->qpair->cmd_cnt++;
2252 	/* Set chip new ring index. */
2253 	wrt_reg_dword(req->req_q_in, req->ring_index);
2254 
2255 	/* Manage unprocessed RIO/ZIO commands in response queue. */
2256 	if (vha->flags.process_response_queue &&
2257 	    rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2258 		qla24xx_process_response_queue(vha, rsp);
2259 
2260 	spin_unlock_irqrestore(&qpair->qp_lock, flags);
2261 
2262 	return QLA_SUCCESS;
2263 
2264 queuing_error:
2265 	if (status & QDSS_GOT_Q_SPACE) {
2266 		req->outstanding_cmds[handle] = NULL;
2267 		req->cnt += req_cnt;
2268 	}
2269 	/* Cleanup will be performed by the caller (queuecommand) */
2270 
2271 	qla_put_fw_resources(sp->qpair, &sp->iores);
2272 	spin_unlock_irqrestore(&qpair->qp_lock, flags);
2273 
2274 	return QLA_FUNCTION_FAILED;
2275 }
2276 
2277 /* Generic Control-SRB manipulation functions. */
2278 
2279 /* hardware_lock assumed to be held. */
2280 
2281 void *
2282 __qla2x00_alloc_iocbs(struct qla_qpair *qpair, srb_t *sp)
2283 {
2284 	scsi_qla_host_t *vha = qpair->vha;
2285 	struct qla_hw_data *ha = vha->hw;
2286 	struct req_que *req = qpair->req;
2287 	device_reg_t *reg = ISP_QUE_REG(ha, req->id);
2288 	uint32_t handle;
2289 	request_t *pkt;
2290 	uint16_t cnt, req_cnt;
2291 
2292 	pkt = NULL;
2293 	req_cnt = 1;
2294 	handle = 0;
2295 
2296 	if (sp && (sp->type != SRB_SCSI_CMD)) {
2297 		/* Adjust entry-counts as needed. */
2298 		req_cnt = sp->iocbs;
2299 	}
2300 
2301 	/* Check for room on request queue. */
2302 	if (req->cnt < req_cnt + 2) {
2303 		if (qpair->use_shadow_reg)
2304 			cnt = *req->out_ptr;
2305 		else if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
2306 		    IS_QLA28XX(ha))
2307 			cnt = rd_reg_dword(&reg->isp25mq.req_q_out);
2308 		else if (IS_P3P_TYPE(ha))
2309 			cnt = rd_reg_dword(reg->isp82.req_q_out);
2310 		else if (IS_FWI2_CAPABLE(ha))
2311 			cnt = rd_reg_dword(&reg->isp24.req_q_out);
2312 		else if (IS_QLAFX00(ha))
2313 			cnt = rd_reg_dword(&reg->ispfx00.req_q_out);
2314 		else
2315 			cnt = qla2x00_debounce_register(
2316 			    ISP_REQ_Q_OUT(ha, &reg->isp));
2317 
2318 		if (!qpair->use_shadow_reg && cnt == ISP_REG16_DISCONNECT) {
2319 			qla_schedule_eeh_work(vha);
2320 			return NULL;
2321 		}
2322 
2323 		if  (req->ring_index < cnt)
2324 			req->cnt = cnt - req->ring_index;
2325 		else
2326 			req->cnt = req->length -
2327 			    (req->ring_index - cnt);
2328 	}
2329 	if (req->cnt < req_cnt + 2)
2330 		goto queuing_error;
2331 
2332 	if (sp) {
2333 		handle = qla2xxx_get_next_handle(req);
2334 		if (handle == 0) {
2335 			ql_log(ql_log_warn, vha, 0x700b,
2336 			    "No room on outstanding cmd array.\n");
2337 			goto queuing_error;
2338 		}
2339 
2340 		/* Prep command array. */
2341 		req->current_outstanding_cmd = handle;
2342 		req->outstanding_cmds[handle] = sp;
2343 		sp->handle = handle;
2344 	}
2345 
2346 	/* Prep packet */
2347 	req->cnt -= req_cnt;
2348 	pkt = req->ring_ptr;
2349 	memset(pkt, 0, REQUEST_ENTRY_SIZE);
2350 	if (IS_QLAFX00(ha)) {
2351 		wrt_reg_byte((u8 __force __iomem *)&pkt->entry_count, req_cnt);
2352 		wrt_reg_dword((__le32 __force __iomem *)&pkt->handle, handle);
2353 	} else {
2354 		pkt->entry_count = req_cnt;
2355 		pkt->handle = handle;
2356 	}
2357 
2358 	return pkt;
2359 
2360 queuing_error:
2361 	qpair->tgt_counters.num_alloc_iocb_failed++;
2362 	return pkt;
2363 }
2364 
2365 void *
2366 qla2x00_alloc_iocbs_ready(struct qla_qpair *qpair, srb_t *sp)
2367 {
2368 	scsi_qla_host_t *vha = qpair->vha;
2369 
2370 	if (qla2x00_reset_active(vha))
2371 		return NULL;
2372 
2373 	return __qla2x00_alloc_iocbs(qpair, sp);
2374 }
2375 
2376 void *
2377 qla2x00_alloc_iocbs(struct scsi_qla_host *vha, srb_t *sp)
2378 {
2379 	return __qla2x00_alloc_iocbs(vha->hw->base_qpair, sp);
2380 }
2381 
2382 static void
2383 qla24xx_prli_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2384 {
2385 	struct srb_iocb *lio = &sp->u.iocb_cmd;
2386 
2387 	logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2388 	logio->control_flags = cpu_to_le16(LCF_COMMAND_PRLI);
2389 	if (lio->u.logio.flags & SRB_LOGIN_NVME_PRLI) {
2390 		logio->control_flags |= cpu_to_le16(LCF_NVME_PRLI);
2391 		if (sp->vha->flags.nvme_first_burst)
2392 			logio->io_parameter[0] =
2393 				cpu_to_le32(NVME_PRLI_SP_FIRST_BURST);
2394 		if (sp->vha->flags.nvme2_enabled) {
2395 			/* Set service parameter BIT_7 for NVME CONF support */
2396 			logio->io_parameter[0] |=
2397 				cpu_to_le32(NVME_PRLI_SP_CONF);
2398 			/* Set service parameter BIT_8 for SLER support */
2399 			logio->io_parameter[0] |=
2400 				cpu_to_le32(NVME_PRLI_SP_SLER);
2401 			/* Set service parameter BIT_9 for PI control support */
2402 			logio->io_parameter[0] |=
2403 				cpu_to_le32(NVME_PRLI_SP_PI_CTRL);
2404 		}
2405 	}
2406 
2407 	logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2408 	logio->port_id[0] = sp->fcport->d_id.b.al_pa;
2409 	logio->port_id[1] = sp->fcport->d_id.b.area;
2410 	logio->port_id[2] = sp->fcport->d_id.b.domain;
2411 	logio->vp_index = sp->vha->vp_idx;
2412 }
2413 
2414 static void
2415 qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2416 {
2417 	struct srb_iocb *lio = &sp->u.iocb_cmd;
2418 
2419 	logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2420 	logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
2421 
2422 	if (lio->u.logio.flags & SRB_LOGIN_PRLI_ONLY) {
2423 		logio->control_flags = cpu_to_le16(LCF_COMMAND_PRLI);
2424 	} else {
2425 		logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
2426 		if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
2427 			logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
2428 		if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
2429 			logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
2430 		if (lio->u.logio.flags & SRB_LOGIN_FCSP) {
2431 			logio->control_flags |=
2432 			    cpu_to_le16(LCF_COMMON_FEAT | LCF_SKIP_PRLI);
2433 			logio->io_parameter[0] =
2434 			    cpu_to_le32(LIO_COMM_FEAT_FCSP | LIO_COMM_FEAT_CIO);
2435 		}
2436 	}
2437 	logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2438 	logio->port_id[0] = sp->fcport->d_id.b.al_pa;
2439 	logio->port_id[1] = sp->fcport->d_id.b.area;
2440 	logio->port_id[2] = sp->fcport->d_id.b.domain;
2441 	logio->vp_index = sp->vha->vp_idx;
2442 }
2443 
2444 static void
2445 qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
2446 {
2447 	struct qla_hw_data *ha = sp->vha->hw;
2448 	struct srb_iocb *lio = &sp->u.iocb_cmd;
2449 	uint16_t opts;
2450 
2451 	mbx->entry_type = MBX_IOCB_TYPE;
2452 	SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
2453 	mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
2454 	opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
2455 	opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
2456 	if (HAS_EXTENDED_IDS(ha)) {
2457 		mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
2458 		mbx->mb10 = cpu_to_le16(opts);
2459 	} else {
2460 		mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
2461 	}
2462 	mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
2463 	mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
2464 	    sp->fcport->d_id.b.al_pa);
2465 	mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
2466 }
2467 
2468 static void
2469 qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2470 {
2471 	u16 control_flags = LCF_COMMAND_LOGO;
2472 	logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2473 
2474 	if (sp->fcport->explicit_logout) {
2475 		control_flags |= LCF_EXPL_LOGO|LCF_FREE_NPORT;
2476 	} else {
2477 		control_flags |= LCF_IMPL_LOGO;
2478 
2479 		if (!sp->fcport->keep_nport_handle)
2480 			control_flags |= LCF_FREE_NPORT;
2481 	}
2482 
2483 	logio->control_flags = cpu_to_le16(control_flags);
2484 	logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2485 	logio->port_id[0] = sp->fcport->d_id.b.al_pa;
2486 	logio->port_id[1] = sp->fcport->d_id.b.area;
2487 	logio->port_id[2] = sp->fcport->d_id.b.domain;
2488 	logio->vp_index = sp->vha->vp_idx;
2489 }
2490 
2491 static void
2492 qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
2493 {
2494 	struct qla_hw_data *ha = sp->vha->hw;
2495 
2496 	mbx->entry_type = MBX_IOCB_TYPE;
2497 	SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
2498 	mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
2499 	mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
2500 	    cpu_to_le16(sp->fcport->loop_id) :
2501 	    cpu_to_le16(sp->fcport->loop_id << 8);
2502 	mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
2503 	mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
2504 	    sp->fcport->d_id.b.al_pa);
2505 	mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
2506 	/* Implicit: mbx->mbx10 = 0. */
2507 }
2508 
2509 static void
2510 qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2511 {
2512 	logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2513 	logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
2514 	logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2515 	logio->vp_index = sp->vha->vp_idx;
2516 }
2517 
2518 static void
2519 qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
2520 {
2521 	struct qla_hw_data *ha = sp->vha->hw;
2522 
2523 	mbx->entry_type = MBX_IOCB_TYPE;
2524 	SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
2525 	mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
2526 	if (HAS_EXTENDED_IDS(ha)) {
2527 		mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
2528 		mbx->mb10 = cpu_to_le16(BIT_0);
2529 	} else {
2530 		mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
2531 	}
2532 	mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
2533 	mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
2534 	mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
2535 	mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
2536 	mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
2537 }
2538 
2539 static void
2540 qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
2541 {
2542 	uint32_t flags;
2543 	uint64_t lun;
2544 	struct fc_port *fcport = sp->fcport;
2545 	scsi_qla_host_t *vha = fcport->vha;
2546 	struct qla_hw_data *ha = vha->hw;
2547 	struct srb_iocb *iocb = &sp->u.iocb_cmd;
2548 	struct req_que *req = sp->qpair->req;
2549 
2550 	flags = iocb->u.tmf.flags;
2551 	lun = iocb->u.tmf.lun;
2552 
2553 	tsk->entry_type = TSK_MGMT_IOCB_TYPE;
2554 	tsk->entry_count = 1;
2555 	tsk->handle = make_handle(req->id, tsk->handle);
2556 	tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2557 	tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2558 	tsk->control_flags = cpu_to_le32(flags);
2559 	tsk->port_id[0] = fcport->d_id.b.al_pa;
2560 	tsk->port_id[1] = fcport->d_id.b.area;
2561 	tsk->port_id[2] = fcport->d_id.b.domain;
2562 	tsk->vp_index = fcport->vha->vp_idx;
2563 
2564 	if (flags & (TCF_LUN_RESET | TCF_ABORT_TASK_SET|
2565 	    TCF_CLEAR_TASK_SET|TCF_CLEAR_ACA)) {
2566 		int_to_scsilun(lun, &tsk->lun);
2567 		host_to_fcp_swap((uint8_t *)&tsk->lun,
2568 			sizeof(tsk->lun));
2569 	}
2570 }
2571 
2572 static void
2573 qla2x00_async_done(struct srb *sp, int res)
2574 {
2575 	if (del_timer(&sp->u.iocb_cmd.timer)) {
2576 		/*
2577 		 * Successfully cancelled the timeout handler
2578 		 * ref: TMR
2579 		 */
2580 		if (kref_put(&sp->cmd_kref, qla2x00_sp_release))
2581 			return;
2582 	}
2583 	sp->async_done(sp, res);
2584 }
2585 
2586 void
2587 qla2x00_sp_release(struct kref *kref)
2588 {
2589 	struct srb *sp = container_of(kref, struct srb, cmd_kref);
2590 
2591 	sp->free(sp);
2592 }
2593 
2594 void
2595 qla2x00_init_async_sp(srb_t *sp, unsigned long tmo,
2596 		     void (*done)(struct srb *sp, int res))
2597 {
2598 	timer_setup(&sp->u.iocb_cmd.timer, qla2x00_sp_timeout, 0);
2599 	sp->done = qla2x00_async_done;
2600 	sp->async_done = done;
2601 	sp->free = qla2x00_sp_free;
2602 	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
2603 	sp->u.iocb_cmd.timer.expires = jiffies + tmo * HZ;
2604 	if (IS_QLAFX00(sp->vha->hw) && sp->type == SRB_FXIOCB_DCMD)
2605 		init_completion(&sp->u.iocb_cmd.u.fxiocb.fxiocb_comp);
2606 	sp->start_timer = 1;
2607 }
2608 
2609 static void qla2x00_els_dcmd_sp_free(srb_t *sp)
2610 {
2611 	struct srb_iocb *elsio = &sp->u.iocb_cmd;
2612 
2613 	kfree(sp->fcport);
2614 
2615 	if (elsio->u.els_logo.els_logo_pyld)
2616 		dma_free_coherent(&sp->vha->hw->pdev->dev, DMA_POOL_SIZE,
2617 		    elsio->u.els_logo.els_logo_pyld,
2618 		    elsio->u.els_logo.els_logo_pyld_dma);
2619 
2620 	del_timer(&elsio->timer);
2621 	qla2x00_rel_sp(sp);
2622 }
2623 
2624 static void
2625 qla2x00_els_dcmd_iocb_timeout(void *data)
2626 {
2627 	srb_t *sp = data;
2628 	fc_port_t *fcport = sp->fcport;
2629 	struct scsi_qla_host *vha = sp->vha;
2630 	struct srb_iocb *lio = &sp->u.iocb_cmd;
2631 	unsigned long flags = 0;
2632 	int res, h;
2633 
2634 	ql_dbg(ql_dbg_io, vha, 0x3069,
2635 	    "%s Timeout, hdl=%x, portid=%02x%02x%02x\n",
2636 	    sp->name, sp->handle, fcport->d_id.b.domain, fcport->d_id.b.area,
2637 	    fcport->d_id.b.al_pa);
2638 
2639 	/* Abort the exchange */
2640 	res = qla24xx_async_abort_cmd(sp, false);
2641 	if (res) {
2642 		ql_dbg(ql_dbg_io, vha, 0x3070,
2643 		    "mbx abort_command failed.\n");
2644 		spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
2645 		for (h = 1; h < sp->qpair->req->num_outstanding_cmds; h++) {
2646 			if (sp->qpair->req->outstanding_cmds[h] == sp) {
2647 				sp->qpair->req->outstanding_cmds[h] = NULL;
2648 				break;
2649 			}
2650 		}
2651 		spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
2652 		complete(&lio->u.els_logo.comp);
2653 	} else {
2654 		ql_dbg(ql_dbg_io, vha, 0x3071,
2655 		    "mbx abort_command success.\n");
2656 	}
2657 }
2658 
2659 static void qla2x00_els_dcmd_sp_done(srb_t *sp, int res)
2660 {
2661 	fc_port_t *fcport = sp->fcport;
2662 	struct srb_iocb *lio = &sp->u.iocb_cmd;
2663 	struct scsi_qla_host *vha = sp->vha;
2664 
2665 	ql_dbg(ql_dbg_io, vha, 0x3072,
2666 	    "%s hdl=%x, portid=%02x%02x%02x done\n",
2667 	    sp->name, sp->handle, fcport->d_id.b.domain,
2668 	    fcport->d_id.b.area, fcport->d_id.b.al_pa);
2669 
2670 	complete(&lio->u.els_logo.comp);
2671 }
2672 
2673 int
2674 qla24xx_els_dcmd_iocb(scsi_qla_host_t *vha, int els_opcode,
2675     port_id_t remote_did)
2676 {
2677 	srb_t *sp;
2678 	fc_port_t *fcport = NULL;
2679 	struct srb_iocb *elsio = NULL;
2680 	struct qla_hw_data *ha = vha->hw;
2681 	struct els_logo_payload logo_pyld;
2682 	int rval = QLA_SUCCESS;
2683 
2684 	fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2685 	if (!fcport) {
2686 	       ql_log(ql_log_info, vha, 0x70e5, "fcport allocation failed\n");
2687 	       return -ENOMEM;
2688 	}
2689 
2690 	/* Alloc SRB structure
2691 	 * ref: INIT
2692 	 */
2693 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
2694 	if (!sp) {
2695 		kfree(fcport);
2696 		ql_log(ql_log_info, vha, 0x70e6,
2697 		 "SRB allocation failed\n");
2698 		return -ENOMEM;
2699 	}
2700 
2701 	elsio = &sp->u.iocb_cmd;
2702 	fcport->loop_id = 0xFFFF;
2703 	fcport->d_id.b.domain = remote_did.b.domain;
2704 	fcport->d_id.b.area = remote_did.b.area;
2705 	fcport->d_id.b.al_pa = remote_did.b.al_pa;
2706 
2707 	ql_dbg(ql_dbg_io, vha, 0x3073, "portid=%02x%02x%02x done\n",
2708 	    fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
2709 
2710 	sp->type = SRB_ELS_DCMD;
2711 	sp->name = "ELS_DCMD";
2712 	sp->fcport = fcport;
2713 	qla2x00_init_async_sp(sp, ELS_DCMD_TIMEOUT,
2714 			      qla2x00_els_dcmd_sp_done);
2715 	sp->free = qla2x00_els_dcmd_sp_free;
2716 	sp->u.iocb_cmd.timeout = qla2x00_els_dcmd_iocb_timeout;
2717 	init_completion(&sp->u.iocb_cmd.u.els_logo.comp);
2718 
2719 	elsio->u.els_logo.els_logo_pyld = dma_alloc_coherent(&ha->pdev->dev,
2720 			    DMA_POOL_SIZE, &elsio->u.els_logo.els_logo_pyld_dma,
2721 			    GFP_KERNEL);
2722 
2723 	if (!elsio->u.els_logo.els_logo_pyld) {
2724 		/* ref: INIT */
2725 		kref_put(&sp->cmd_kref, qla2x00_sp_release);
2726 		return QLA_FUNCTION_FAILED;
2727 	}
2728 
2729 	memset(&logo_pyld, 0, sizeof(struct els_logo_payload));
2730 
2731 	elsio->u.els_logo.els_cmd = els_opcode;
2732 	logo_pyld.opcode = els_opcode;
2733 	logo_pyld.s_id[0] = vha->d_id.b.al_pa;
2734 	logo_pyld.s_id[1] = vha->d_id.b.area;
2735 	logo_pyld.s_id[2] = vha->d_id.b.domain;
2736 	host_to_fcp_swap(logo_pyld.s_id, sizeof(uint32_t));
2737 	memcpy(&logo_pyld.wwpn, vha->port_name, WWN_SIZE);
2738 
2739 	memcpy(elsio->u.els_logo.els_logo_pyld, &logo_pyld,
2740 	    sizeof(struct els_logo_payload));
2741 	ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x3075, "LOGO buffer:");
2742 	ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x010a,
2743 		       elsio->u.els_logo.els_logo_pyld,
2744 		       sizeof(*elsio->u.els_logo.els_logo_pyld));
2745 
2746 	rval = qla2x00_start_sp(sp);
2747 	if (rval != QLA_SUCCESS) {
2748 		/* ref: INIT */
2749 		kref_put(&sp->cmd_kref, qla2x00_sp_release);
2750 		return QLA_FUNCTION_FAILED;
2751 	}
2752 
2753 	ql_dbg(ql_dbg_io, vha, 0x3074,
2754 	    "%s LOGO sent, hdl=%x, loopid=%x, portid=%02x%02x%02x.\n",
2755 	    sp->name, sp->handle, fcport->loop_id, fcport->d_id.b.domain,
2756 	    fcport->d_id.b.area, fcport->d_id.b.al_pa);
2757 
2758 	wait_for_completion(&elsio->u.els_logo.comp);
2759 
2760 	/* ref: INIT */
2761 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
2762 	return rval;
2763 }
2764 
2765 static void
2766 qla24xx_els_logo_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2767 {
2768 	scsi_qla_host_t *vha = sp->vha;
2769 	struct srb_iocb *elsio = &sp->u.iocb_cmd;
2770 
2771 	els_iocb->entry_type = ELS_IOCB_TYPE;
2772 	els_iocb->entry_count = 1;
2773 	els_iocb->sys_define = 0;
2774 	els_iocb->entry_status = 0;
2775 	els_iocb->handle = sp->handle;
2776 	els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2777 	els_iocb->tx_dsd_count = cpu_to_le16(1);
2778 	els_iocb->vp_index = vha->vp_idx;
2779 	els_iocb->sof_type = EST_SOFI3;
2780 	els_iocb->rx_dsd_count = 0;
2781 	els_iocb->opcode = elsio->u.els_logo.els_cmd;
2782 
2783 	els_iocb->d_id[0] = sp->fcport->d_id.b.al_pa;
2784 	els_iocb->d_id[1] = sp->fcport->d_id.b.area;
2785 	els_iocb->d_id[2] = sp->fcport->d_id.b.domain;
2786 	/* For SID the byte order is different than DID */
2787 	els_iocb->s_id[1] = vha->d_id.b.al_pa;
2788 	els_iocb->s_id[2] = vha->d_id.b.area;
2789 	els_iocb->s_id[0] = vha->d_id.b.domain;
2790 
2791 	if (elsio->u.els_logo.els_cmd == ELS_DCMD_PLOGI) {
2792 		if (vha->hw->flags.edif_enabled)
2793 			els_iocb->control_flags = cpu_to_le16(ECF_SEC_LOGIN);
2794 		else
2795 			els_iocb->control_flags = 0;
2796 		els_iocb->tx_byte_count = els_iocb->tx_len =
2797 			cpu_to_le32(sizeof(struct els_plogi_payload));
2798 		put_unaligned_le64(elsio->u.els_plogi.els_plogi_pyld_dma,
2799 				   &els_iocb->tx_address);
2800 		els_iocb->rx_dsd_count = cpu_to_le16(1);
2801 		els_iocb->rx_byte_count = els_iocb->rx_len =
2802 			cpu_to_le32(sizeof(struct els_plogi_payload));
2803 		put_unaligned_le64(elsio->u.els_plogi.els_resp_pyld_dma,
2804 				   &els_iocb->rx_address);
2805 
2806 		ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x3073,
2807 		    "PLOGI ELS IOCB:\n");
2808 		ql_dump_buffer(ql_log_info, vha, 0x0109,
2809 		    (uint8_t *)els_iocb,
2810 		    sizeof(*els_iocb));
2811 	} else {
2812 		els_iocb->tx_byte_count =
2813 			cpu_to_le32(sizeof(struct els_logo_payload));
2814 		put_unaligned_le64(elsio->u.els_logo.els_logo_pyld_dma,
2815 				   &els_iocb->tx_address);
2816 		els_iocb->tx_len = cpu_to_le32(sizeof(struct els_logo_payload));
2817 
2818 		els_iocb->rx_byte_count = 0;
2819 		els_iocb->rx_address = 0;
2820 		els_iocb->rx_len = 0;
2821 		ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x3076,
2822 		       "LOGO ELS IOCB:");
2823 		ql_dump_buffer(ql_log_info, vha, 0x010b,
2824 			       els_iocb,
2825 			       sizeof(*els_iocb));
2826 	}
2827 
2828 	sp->vha->qla_stats.control_requests++;
2829 }
2830 
2831 void
2832 qla2x00_els_dcmd2_iocb_timeout(void *data)
2833 {
2834 	srb_t *sp = data;
2835 	fc_port_t *fcport = sp->fcport;
2836 	struct scsi_qla_host *vha = sp->vha;
2837 	unsigned long flags = 0;
2838 	int res, h;
2839 
2840 	ql_dbg(ql_dbg_io + ql_dbg_disc, vha, 0x3069,
2841 	    "%s hdl=%x ELS Timeout, %8phC portid=%06x\n",
2842 	    sp->name, sp->handle, fcport->port_name, fcport->d_id.b24);
2843 
2844 	/* Abort the exchange */
2845 	res = qla24xx_async_abort_cmd(sp, false);
2846 	ql_dbg(ql_dbg_io, vha, 0x3070,
2847 	    "mbx abort_command %s\n",
2848 	    (res == QLA_SUCCESS) ? "successful" : "failed");
2849 	if (res) {
2850 		spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
2851 		for (h = 1; h < sp->qpair->req->num_outstanding_cmds; h++) {
2852 			if (sp->qpair->req->outstanding_cmds[h] == sp) {
2853 				sp->qpair->req->outstanding_cmds[h] = NULL;
2854 				break;
2855 			}
2856 		}
2857 		spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
2858 		sp->done(sp, QLA_FUNCTION_TIMEOUT);
2859 	}
2860 }
2861 
2862 void qla2x00_els_dcmd2_free(scsi_qla_host_t *vha, struct els_plogi *els_plogi)
2863 {
2864 	if (els_plogi->els_plogi_pyld)
2865 		dma_free_coherent(&vha->hw->pdev->dev,
2866 				  els_plogi->tx_size,
2867 				  els_plogi->els_plogi_pyld,
2868 				  els_plogi->els_plogi_pyld_dma);
2869 
2870 	if (els_plogi->els_resp_pyld)
2871 		dma_free_coherent(&vha->hw->pdev->dev,
2872 				  els_plogi->rx_size,
2873 				  els_plogi->els_resp_pyld,
2874 				  els_plogi->els_resp_pyld_dma);
2875 }
2876 
2877 static void qla2x00_els_dcmd2_sp_done(srb_t *sp, int res)
2878 {
2879 	fc_port_t *fcport = sp->fcport;
2880 	struct srb_iocb *lio = &sp->u.iocb_cmd;
2881 	struct scsi_qla_host *vha = sp->vha;
2882 	struct event_arg ea;
2883 	struct qla_work_evt *e;
2884 	struct fc_port *conflict_fcport;
2885 	port_id_t cid;	/* conflict Nport id */
2886 	const __le32 *fw_status = sp->u.iocb_cmd.u.els_plogi.fw_status;
2887 	u16 lid;
2888 
2889 	ql_dbg(ql_dbg_disc, vha, 0x3072,
2890 	    "%s ELS done rc %d hdl=%x, portid=%06x %8phC\n",
2891 	    sp->name, res, sp->handle, fcport->d_id.b24, fcport->port_name);
2892 
2893 	fcport->flags &= ~(FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE);
2894 	/* For edif, set logout on delete to ensure any residual key from FW is flushed.*/
2895 	fcport->logout_on_delete = 1;
2896 	fcport->chip_reset = vha->hw->base_qpair->chip_reset;
2897 
2898 	if (sp->flags & SRB_WAKEUP_ON_COMP)
2899 		complete(&lio->u.els_plogi.comp);
2900 	else {
2901 		switch (le32_to_cpu(fw_status[0])) {
2902 		case CS_DATA_UNDERRUN:
2903 		case CS_COMPLETE:
2904 			memset(&ea, 0, sizeof(ea));
2905 			ea.fcport = fcport;
2906 			ea.rc = res;
2907 			qla_handle_els_plogi_done(vha, &ea);
2908 			break;
2909 
2910 		case CS_IOCB_ERROR:
2911 			switch (le32_to_cpu(fw_status[1])) {
2912 			case LSC_SCODE_PORTID_USED:
2913 				lid = le32_to_cpu(fw_status[2]) & 0xffff;
2914 				qlt_find_sess_invalidate_other(vha,
2915 				    wwn_to_u64(fcport->port_name),
2916 				    fcport->d_id, lid, &conflict_fcport);
2917 				if (conflict_fcport) {
2918 					/*
2919 					 * Another fcport shares the same
2920 					 * loop_id & nport id; conflict
2921 					 * fcport needs to finish cleanup
2922 					 * before this fcport can proceed
2923 					 * to login.
2924 					 */
2925 					conflict_fcport->conflict = fcport;
2926 					fcport->login_pause = 1;
2927 					ql_dbg(ql_dbg_disc, vha, 0x20ed,
2928 					    "%s %d %8phC pid %06x inuse with lid %#x.\n",
2929 					    __func__, __LINE__,
2930 					    fcport->port_name,
2931 					    fcport->d_id.b24, lid);
2932 				} else {
2933 					ql_dbg(ql_dbg_disc, vha, 0x20ed,
2934 					    "%s %d %8phC pid %06x inuse with lid %#x sched del\n",
2935 					    __func__, __LINE__,
2936 					    fcport->port_name,
2937 					    fcport->d_id.b24, lid);
2938 					qla2x00_clear_loop_id(fcport);
2939 					set_bit(lid, vha->hw->loop_id_map);
2940 					fcport->loop_id = lid;
2941 					fcport->keep_nport_handle = 0;
2942 					qlt_schedule_sess_for_deletion(fcport);
2943 				}
2944 				break;
2945 
2946 			case LSC_SCODE_NPORT_USED:
2947 				cid.b.domain = (le32_to_cpu(fw_status[2]) >> 16)
2948 					& 0xff;
2949 				cid.b.area   = (le32_to_cpu(fw_status[2]) >>  8)
2950 					& 0xff;
2951 				cid.b.al_pa  = le32_to_cpu(fw_status[2]) & 0xff;
2952 				cid.b.rsvd_1 = 0;
2953 
2954 				ql_dbg(ql_dbg_disc, vha, 0x20ec,
2955 				    "%s %d %8phC lid %#x in use with pid %06x post gnl\n",
2956 				    __func__, __LINE__, fcport->port_name,
2957 				    fcport->loop_id, cid.b24);
2958 				set_bit(fcport->loop_id,
2959 				    vha->hw->loop_id_map);
2960 				fcport->loop_id = FC_NO_LOOP_ID;
2961 				qla24xx_post_gnl_work(vha, fcport);
2962 				break;
2963 
2964 			case LSC_SCODE_NOXCB:
2965 				vha->hw->exch_starvation++;
2966 				if (vha->hw->exch_starvation > 5) {
2967 					ql_log(ql_log_warn, vha, 0xd046,
2968 					    "Exchange starvation. Resetting RISC\n");
2969 					vha->hw->exch_starvation = 0;
2970 					set_bit(ISP_ABORT_NEEDED,
2971 					    &vha->dpc_flags);
2972 					qla2xxx_wake_dpc(vha);
2973 					break;
2974 				}
2975 				fallthrough;
2976 			default:
2977 				ql_dbg(ql_dbg_disc, vha, 0x20eb,
2978 				    "%s %8phC cmd error fw_status 0x%x 0x%x 0x%x\n",
2979 				    __func__, sp->fcport->port_name,
2980 				    fw_status[0], fw_status[1], fw_status[2]);
2981 
2982 				fcport->flags &= ~FCF_ASYNC_SENT;
2983 				qlt_schedule_sess_for_deletion(fcport);
2984 				break;
2985 			}
2986 			break;
2987 
2988 		default:
2989 			ql_dbg(ql_dbg_disc, vha, 0x20eb,
2990 			    "%s %8phC cmd error 2 fw_status 0x%x 0x%x 0x%x\n",
2991 			    __func__, sp->fcport->port_name,
2992 			    fw_status[0], fw_status[1], fw_status[2]);
2993 
2994 			sp->fcport->flags &= ~FCF_ASYNC_SENT;
2995 			qlt_schedule_sess_for_deletion(fcport);
2996 			break;
2997 		}
2998 
2999 		e = qla2x00_alloc_work(vha, QLA_EVT_UNMAP);
3000 		if (!e) {
3001 			struct srb_iocb *elsio = &sp->u.iocb_cmd;
3002 
3003 			qla2x00_els_dcmd2_free(vha, &elsio->u.els_plogi);
3004 			/* ref: INIT */
3005 			kref_put(&sp->cmd_kref, qla2x00_sp_release);
3006 			return;
3007 		}
3008 		e->u.iosb.sp = sp;
3009 		qla2x00_post_work(vha, e);
3010 	}
3011 }
3012 
3013 int
3014 qla24xx_els_dcmd2_iocb(scsi_qla_host_t *vha, int els_opcode,
3015     fc_port_t *fcport, bool wait)
3016 {
3017 	srb_t *sp;
3018 	struct srb_iocb *elsio = NULL;
3019 	struct qla_hw_data *ha = vha->hw;
3020 	int rval = QLA_SUCCESS;
3021 	void	*ptr, *resp_ptr;
3022 
3023 	/* Alloc SRB structure
3024 	 * ref: INIT
3025 	 */
3026 	sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
3027 	if (!sp) {
3028 		ql_log(ql_log_info, vha, 0x70e6,
3029 		 "SRB allocation failed\n");
3030 		fcport->flags &= ~FCF_ASYNC_ACTIVE;
3031 		return -ENOMEM;
3032 	}
3033 
3034 	fcport->flags |= FCF_ASYNC_SENT;
3035 	qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_PEND);
3036 	elsio = &sp->u.iocb_cmd;
3037 	ql_dbg(ql_dbg_io, vha, 0x3073,
3038 	       "%s Enter: PLOGI portid=%06x\n", __func__, fcport->d_id.b24);
3039 
3040 	if (wait)
3041 		sp->flags = SRB_WAKEUP_ON_COMP;
3042 
3043 	sp->type = SRB_ELS_DCMD;
3044 	sp->name = "ELS_DCMD";
3045 	sp->fcport = fcport;
3046 	qla2x00_init_async_sp(sp, ELS_DCMD_TIMEOUT + 2,
3047 			     qla2x00_els_dcmd2_sp_done);
3048 	sp->u.iocb_cmd.timeout = qla2x00_els_dcmd2_iocb_timeout;
3049 
3050 	elsio->u.els_plogi.tx_size = elsio->u.els_plogi.rx_size = DMA_POOL_SIZE;
3051 
3052 	ptr = elsio->u.els_plogi.els_plogi_pyld =
3053 	    dma_alloc_coherent(&ha->pdev->dev, elsio->u.els_plogi.tx_size,
3054 		&elsio->u.els_plogi.els_plogi_pyld_dma, GFP_KERNEL);
3055 
3056 	if (!elsio->u.els_plogi.els_plogi_pyld) {
3057 		rval = QLA_FUNCTION_FAILED;
3058 		goto out;
3059 	}
3060 
3061 	resp_ptr = elsio->u.els_plogi.els_resp_pyld =
3062 	    dma_alloc_coherent(&ha->pdev->dev, elsio->u.els_plogi.rx_size,
3063 		&elsio->u.els_plogi.els_resp_pyld_dma, GFP_KERNEL);
3064 
3065 	if (!elsio->u.els_plogi.els_resp_pyld) {
3066 		rval = QLA_FUNCTION_FAILED;
3067 		goto out;
3068 	}
3069 
3070 	ql_dbg(ql_dbg_io, vha, 0x3073, "PLOGI %p %p\n", ptr, resp_ptr);
3071 
3072 	memset(ptr, 0, sizeof(struct els_plogi_payload));
3073 	memset(resp_ptr, 0, sizeof(struct els_plogi_payload));
3074 	memcpy(elsio->u.els_plogi.els_plogi_pyld->data,
3075 	    &ha->plogi_els_payld.fl_csp, LOGIN_TEMPLATE_SIZE);
3076 
3077 	elsio->u.els_plogi.els_cmd = els_opcode;
3078 	elsio->u.els_plogi.els_plogi_pyld->opcode = els_opcode;
3079 
3080 	if (els_opcode == ELS_DCMD_PLOGI && DBELL_ACTIVE(vha)) {
3081 		struct fc_els_flogi *p = ptr;
3082 
3083 		p->fl_csp.sp_features |= cpu_to_be16(FC_SP_FT_SEC);
3084 	}
3085 
3086 	ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x3073, "PLOGI buffer:\n");
3087 	ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x0109,
3088 	    (uint8_t *)elsio->u.els_plogi.els_plogi_pyld,
3089 	    sizeof(*elsio->u.els_plogi.els_plogi_pyld));
3090 
3091 	init_completion(&elsio->u.els_plogi.comp);
3092 	rval = qla2x00_start_sp(sp);
3093 	if (rval != QLA_SUCCESS) {
3094 		rval = QLA_FUNCTION_FAILED;
3095 	} else {
3096 		ql_dbg(ql_dbg_disc, vha, 0x3074,
3097 		    "%s PLOGI sent, hdl=%x, loopid=%x, to port_id %06x from port_id %06x\n",
3098 		    sp->name, sp->handle, fcport->loop_id,
3099 		    fcport->d_id.b24, vha->d_id.b24);
3100 	}
3101 
3102 	if (wait) {
3103 		wait_for_completion(&elsio->u.els_plogi.comp);
3104 
3105 		if (elsio->u.els_plogi.comp_status != CS_COMPLETE)
3106 			rval = QLA_FUNCTION_FAILED;
3107 	} else {
3108 		goto done;
3109 	}
3110 
3111 out:
3112 	fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
3113 	qla2x00_els_dcmd2_free(vha, &elsio->u.els_plogi);
3114 	/* ref: INIT */
3115 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
3116 done:
3117 	return rval;
3118 }
3119 
3120 /* it is assume qpair lock is held */
3121 void qla_els_pt_iocb(struct scsi_qla_host *vha,
3122 	struct els_entry_24xx *els_iocb,
3123 	struct qla_els_pt_arg *a)
3124 {
3125 	els_iocb->entry_type = ELS_IOCB_TYPE;
3126 	els_iocb->entry_count = 1;
3127 	els_iocb->sys_define = 0;
3128 	els_iocb->entry_status = 0;
3129 	els_iocb->handle = QLA_SKIP_HANDLE;
3130 	els_iocb->nport_handle = a->nport_handle;
3131 	els_iocb->rx_xchg_address = a->rx_xchg_address;
3132 	els_iocb->tx_dsd_count = cpu_to_le16(1);
3133 	els_iocb->vp_index = a->vp_idx;
3134 	els_iocb->sof_type = EST_SOFI3;
3135 	els_iocb->rx_dsd_count = cpu_to_le16(0);
3136 	els_iocb->opcode = a->els_opcode;
3137 
3138 	els_iocb->d_id[0] = a->did.b.al_pa;
3139 	els_iocb->d_id[1] = a->did.b.area;
3140 	els_iocb->d_id[2] = a->did.b.domain;
3141 	/* For SID the byte order is different than DID */
3142 	els_iocb->s_id[1] = vha->d_id.b.al_pa;
3143 	els_iocb->s_id[2] = vha->d_id.b.area;
3144 	els_iocb->s_id[0] = vha->d_id.b.domain;
3145 
3146 	els_iocb->control_flags = cpu_to_le16(a->control_flags);
3147 
3148 	els_iocb->tx_byte_count = cpu_to_le32(a->tx_byte_count);
3149 	els_iocb->tx_len = cpu_to_le32(a->tx_len);
3150 	put_unaligned_le64(a->tx_addr, &els_iocb->tx_address);
3151 
3152 	els_iocb->rx_byte_count = cpu_to_le32(a->rx_byte_count);
3153 	els_iocb->rx_len = cpu_to_le32(a->rx_len);
3154 	put_unaligned_le64(a->rx_addr, &els_iocb->rx_address);
3155 }
3156 
3157 static void
3158 qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
3159 {
3160 	struct bsg_job *bsg_job = sp->u.bsg_job;
3161 	struct fc_bsg_request *bsg_request = bsg_job->request;
3162 
3163         els_iocb->entry_type = ELS_IOCB_TYPE;
3164         els_iocb->entry_count = 1;
3165         els_iocb->sys_define = 0;
3166         els_iocb->entry_status = 0;
3167         els_iocb->handle = sp->handle;
3168 	els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3169 	els_iocb->tx_dsd_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
3170 	els_iocb->vp_index = sp->vha->vp_idx;
3171         els_iocb->sof_type = EST_SOFI3;
3172 	els_iocb->rx_dsd_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
3173 
3174 	els_iocb->opcode =
3175 	    sp->type == SRB_ELS_CMD_RPT ?
3176 	    bsg_request->rqst_data.r_els.els_code :
3177 	    bsg_request->rqst_data.h_els.command_code;
3178 	els_iocb->d_id[0] = sp->fcport->d_id.b.al_pa;
3179 	els_iocb->d_id[1] = sp->fcport->d_id.b.area;
3180 	els_iocb->d_id[2] = sp->fcport->d_id.b.domain;
3181         els_iocb->control_flags = 0;
3182         els_iocb->rx_byte_count =
3183             cpu_to_le32(bsg_job->reply_payload.payload_len);
3184         els_iocb->tx_byte_count =
3185             cpu_to_le32(bsg_job->request_payload.payload_len);
3186 
3187 	put_unaligned_le64(sg_dma_address(bsg_job->request_payload.sg_list),
3188 			   &els_iocb->tx_address);
3189         els_iocb->tx_len = cpu_to_le32(sg_dma_len
3190             (bsg_job->request_payload.sg_list));
3191 
3192 	put_unaligned_le64(sg_dma_address(bsg_job->reply_payload.sg_list),
3193 			   &els_iocb->rx_address);
3194         els_iocb->rx_len = cpu_to_le32(sg_dma_len
3195             (bsg_job->reply_payload.sg_list));
3196 
3197 	sp->vha->qla_stats.control_requests++;
3198 }
3199 
3200 static void
3201 qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
3202 {
3203 	uint16_t        avail_dsds;
3204 	struct dsd64	*cur_dsd;
3205 	struct scatterlist *sg;
3206 	int index;
3207 	uint16_t tot_dsds;
3208 	scsi_qla_host_t *vha = sp->vha;
3209 	struct qla_hw_data *ha = vha->hw;
3210 	struct bsg_job *bsg_job = sp->u.bsg_job;
3211 	int entry_count = 1;
3212 
3213 	memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
3214 	ct_iocb->entry_type = CT_IOCB_TYPE;
3215 	ct_iocb->entry_status = 0;
3216 	ct_iocb->handle1 = sp->handle;
3217 	SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
3218 	ct_iocb->status = cpu_to_le16(0);
3219 	ct_iocb->control_flags = cpu_to_le16(0);
3220 	ct_iocb->timeout = 0;
3221 	ct_iocb->cmd_dsd_count =
3222 	    cpu_to_le16(bsg_job->request_payload.sg_cnt);
3223 	ct_iocb->total_dsd_count =
3224 	    cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
3225 	ct_iocb->req_bytecount =
3226 	    cpu_to_le32(bsg_job->request_payload.payload_len);
3227 	ct_iocb->rsp_bytecount =
3228 	    cpu_to_le32(bsg_job->reply_payload.payload_len);
3229 
3230 	put_unaligned_le64(sg_dma_address(bsg_job->request_payload.sg_list),
3231 			   &ct_iocb->req_dsd.address);
3232 	ct_iocb->req_dsd.length = ct_iocb->req_bytecount;
3233 
3234 	put_unaligned_le64(sg_dma_address(bsg_job->reply_payload.sg_list),
3235 			   &ct_iocb->rsp_dsd.address);
3236 	ct_iocb->rsp_dsd.length = ct_iocb->rsp_bytecount;
3237 
3238 	avail_dsds = 1;
3239 	cur_dsd = &ct_iocb->rsp_dsd;
3240 	index = 0;
3241 	tot_dsds = bsg_job->reply_payload.sg_cnt;
3242 
3243 	for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
3244 		cont_a64_entry_t *cont_pkt;
3245 
3246 		/* Allocate additional continuation packets? */
3247 		if (avail_dsds == 0) {
3248 			/*
3249 			* Five DSDs are available in the Cont.
3250 			* Type 1 IOCB.
3251 			       */
3252 			cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
3253 			    vha->hw->req_q_map[0]);
3254 			cur_dsd = cont_pkt->dsd;
3255 			avail_dsds = 5;
3256 			entry_count++;
3257 		}
3258 
3259 		append_dsd64(&cur_dsd, sg);
3260 		avail_dsds--;
3261 	}
3262 	ct_iocb->entry_count = entry_count;
3263 
3264 	sp->vha->qla_stats.control_requests++;
3265 }
3266 
3267 static void
3268 qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
3269 {
3270 	uint16_t        avail_dsds;
3271 	struct dsd64	*cur_dsd;
3272 	struct scatterlist *sg;
3273 	int index;
3274 	uint16_t cmd_dsds, rsp_dsds;
3275 	scsi_qla_host_t *vha = sp->vha;
3276 	struct qla_hw_data *ha = vha->hw;
3277 	struct bsg_job *bsg_job = sp->u.bsg_job;
3278 	int entry_count = 1;
3279 	cont_a64_entry_t *cont_pkt = NULL;
3280 
3281 	ct_iocb->entry_type = CT_IOCB_TYPE;
3282         ct_iocb->entry_status = 0;
3283         ct_iocb->sys_define = 0;
3284         ct_iocb->handle = sp->handle;
3285 
3286 	ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3287 	ct_iocb->vp_index = sp->vha->vp_idx;
3288 	ct_iocb->comp_status = cpu_to_le16(0);
3289 
3290 	cmd_dsds = bsg_job->request_payload.sg_cnt;
3291 	rsp_dsds = bsg_job->reply_payload.sg_cnt;
3292 
3293 	ct_iocb->cmd_dsd_count = cpu_to_le16(cmd_dsds);
3294         ct_iocb->timeout = 0;
3295 	ct_iocb->rsp_dsd_count = cpu_to_le16(rsp_dsds);
3296         ct_iocb->cmd_byte_count =
3297             cpu_to_le32(bsg_job->request_payload.payload_len);
3298 
3299 	avail_dsds = 2;
3300 	cur_dsd = ct_iocb->dsd;
3301 	index = 0;
3302 
3303 	for_each_sg(bsg_job->request_payload.sg_list, sg, cmd_dsds, index) {
3304 		/* Allocate additional continuation packets? */
3305 		if (avail_dsds == 0) {
3306 			/*
3307 			 * Five DSDs are available in the Cont.
3308 			 * Type 1 IOCB.
3309 			 */
3310 			cont_pkt = qla2x00_prep_cont_type1_iocb(
3311 			    vha, ha->req_q_map[0]);
3312 			cur_dsd = cont_pkt->dsd;
3313 			avail_dsds = 5;
3314 			entry_count++;
3315 		}
3316 
3317 		append_dsd64(&cur_dsd, sg);
3318 		avail_dsds--;
3319 	}
3320 
3321 	index = 0;
3322 
3323 	for_each_sg(bsg_job->reply_payload.sg_list, sg, rsp_dsds, index) {
3324 		/* Allocate additional continuation packets? */
3325 		if (avail_dsds == 0) {
3326 			/*
3327 			* Five DSDs are available in the Cont.
3328 			* Type 1 IOCB.
3329 			       */
3330 			cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
3331 			    ha->req_q_map[0]);
3332 			cur_dsd = cont_pkt->dsd;
3333 			avail_dsds = 5;
3334 			entry_count++;
3335 		}
3336 
3337 		append_dsd64(&cur_dsd, sg);
3338 		avail_dsds--;
3339 	}
3340         ct_iocb->entry_count = entry_count;
3341 }
3342 
3343 /*
3344  * qla82xx_start_scsi() - Send a SCSI command to the ISP
3345  * @sp: command to send to the ISP
3346  *
3347  * Returns non-zero if a failure occurred, else zero.
3348  */
3349 int
3350 qla82xx_start_scsi(srb_t *sp)
3351 {
3352 	int		nseg;
3353 	unsigned long   flags;
3354 	struct scsi_cmnd *cmd;
3355 	uint32_t	*clr_ptr;
3356 	uint32_t	handle;
3357 	uint16_t	cnt;
3358 	uint16_t	req_cnt;
3359 	uint16_t	tot_dsds;
3360 	struct device_reg_82xx __iomem *reg;
3361 	uint32_t dbval;
3362 	__be32 *fcp_dl;
3363 	uint8_t additional_cdb_len;
3364 	struct ct6_dsd *ctx;
3365 	struct scsi_qla_host *vha = sp->vha;
3366 	struct qla_hw_data *ha = vha->hw;
3367 	struct req_que *req = NULL;
3368 	struct rsp_que *rsp = NULL;
3369 
3370 	/* Setup device pointers. */
3371 	reg = &ha->iobase->isp82;
3372 	cmd = GET_CMD_SP(sp);
3373 	req = vha->req;
3374 	rsp = ha->rsp_q_map[0];
3375 
3376 	/* So we know we haven't pci_map'ed anything yet */
3377 	tot_dsds = 0;
3378 
3379 	dbval = 0x04 | (ha->portnum << 5);
3380 
3381 	/* Send marker if required */
3382 	if (vha->marker_needed != 0) {
3383 		if (qla2x00_marker(vha, ha->base_qpair,
3384 			0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
3385 			ql_log(ql_log_warn, vha, 0x300c,
3386 			    "qla2x00_marker failed for cmd=%p.\n", cmd);
3387 			return QLA_FUNCTION_FAILED;
3388 		}
3389 		vha->marker_needed = 0;
3390 	}
3391 
3392 	/* Acquire ring specific lock */
3393 	spin_lock_irqsave(&ha->hardware_lock, flags);
3394 
3395 	handle = qla2xxx_get_next_handle(req);
3396 	if (handle == 0)
3397 		goto queuing_error;
3398 
3399 	/* Map the sg table so we have an accurate count of sg entries needed */
3400 	if (scsi_sg_count(cmd)) {
3401 		nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
3402 		    scsi_sg_count(cmd), cmd->sc_data_direction);
3403 		if (unlikely(!nseg))
3404 			goto queuing_error;
3405 	} else
3406 		nseg = 0;
3407 
3408 	tot_dsds = nseg;
3409 
3410 	if (tot_dsds > ql2xshiftctondsd) {
3411 		struct cmd_type_6 *cmd_pkt;
3412 		uint16_t more_dsd_lists = 0;
3413 		struct dsd_dma *dsd_ptr;
3414 		uint16_t i;
3415 
3416 		more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
3417 		if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
3418 			ql_dbg(ql_dbg_io, vha, 0x300d,
3419 			    "Num of DSD list %d is than %d for cmd=%p.\n",
3420 			    more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
3421 			    cmd);
3422 			goto queuing_error;
3423 		}
3424 
3425 		if (more_dsd_lists <= ha->gbl_dsd_avail)
3426 			goto sufficient_dsds;
3427 		else
3428 			more_dsd_lists -= ha->gbl_dsd_avail;
3429 
3430 		for (i = 0; i < more_dsd_lists; i++) {
3431 			dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
3432 			if (!dsd_ptr) {
3433 				ql_log(ql_log_fatal, vha, 0x300e,
3434 				    "Failed to allocate memory for dsd_dma "
3435 				    "for cmd=%p.\n", cmd);
3436 				goto queuing_error;
3437 			}
3438 
3439 			dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
3440 				GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
3441 			if (!dsd_ptr->dsd_addr) {
3442 				kfree(dsd_ptr);
3443 				ql_log(ql_log_fatal, vha, 0x300f,
3444 				    "Failed to allocate memory for dsd_addr "
3445 				    "for cmd=%p.\n", cmd);
3446 				goto queuing_error;
3447 			}
3448 			list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
3449 			ha->gbl_dsd_avail++;
3450 		}
3451 
3452 sufficient_dsds:
3453 		req_cnt = 1;
3454 
3455 		if (req->cnt < (req_cnt + 2)) {
3456 			cnt = (uint16_t)rd_reg_dword_relaxed(
3457 				&reg->req_q_out[0]);
3458 			if (req->ring_index < cnt)
3459 				req->cnt = cnt - req->ring_index;
3460 			else
3461 				req->cnt = req->length -
3462 					(req->ring_index - cnt);
3463 			if (req->cnt < (req_cnt + 2))
3464 				goto queuing_error;
3465 		}
3466 
3467 		ctx = &sp->u.scmd.ct6_ctx;
3468 
3469 		memset(ctx, 0, sizeof(struct ct6_dsd));
3470 		ctx->fcp_cmnd = dma_pool_zalloc(ha->fcp_cmnd_dma_pool,
3471 			GFP_ATOMIC, &ctx->fcp_cmnd_dma);
3472 		if (!ctx->fcp_cmnd) {
3473 			ql_log(ql_log_fatal, vha, 0x3011,
3474 			    "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
3475 			goto queuing_error;
3476 		}
3477 
3478 		/* Initialize the DSD list and dma handle */
3479 		INIT_LIST_HEAD(&ctx->dsd_list);
3480 		ctx->dsd_use_cnt = 0;
3481 
3482 		if (cmd->cmd_len > 16) {
3483 			additional_cdb_len = cmd->cmd_len - 16;
3484 			if ((cmd->cmd_len % 4) != 0) {
3485 				/* SCSI command bigger than 16 bytes must be
3486 				 * multiple of 4
3487 				 */
3488 				ql_log(ql_log_warn, vha, 0x3012,
3489 				    "scsi cmd len %d not multiple of 4 "
3490 				    "for cmd=%p.\n", cmd->cmd_len, cmd);
3491 				goto queuing_error_fcp_cmnd;
3492 			}
3493 			ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
3494 		} else {
3495 			additional_cdb_len = 0;
3496 			ctx->fcp_cmnd_len = 12 + 16 + 4;
3497 		}
3498 
3499 		cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
3500 		cmd_pkt->handle = make_handle(req->id, handle);
3501 
3502 		/* Zero out remaining portion of packet. */
3503 		/*    tagged queuing modifier -- default is TSK_SIMPLE (0). */
3504 		clr_ptr = (uint32_t *)cmd_pkt + 2;
3505 		memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
3506 		cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
3507 
3508 		/* Set NPORT-ID and LUN number*/
3509 		cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3510 		cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
3511 		cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
3512 		cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
3513 		cmd_pkt->vp_index = sp->vha->vp_idx;
3514 
3515 		/* Build IOCB segments */
3516 		if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
3517 			goto queuing_error_fcp_cmnd;
3518 
3519 		int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
3520 		host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
3521 
3522 		/* build FCP_CMND IU */
3523 		int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
3524 		ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
3525 
3526 		if (cmd->sc_data_direction == DMA_TO_DEVICE)
3527 			ctx->fcp_cmnd->additional_cdb_len |= 1;
3528 		else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
3529 			ctx->fcp_cmnd->additional_cdb_len |= 2;
3530 
3531 		/* Populate the FCP_PRIO. */
3532 		if (ha->flags.fcp_prio_enabled)
3533 			ctx->fcp_cmnd->task_attribute |=
3534 			    sp->fcport->fcp_prio << 3;
3535 
3536 		memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
3537 
3538 		fcp_dl = (__be32 *)(ctx->fcp_cmnd->cdb + 16 +
3539 		    additional_cdb_len);
3540 		*fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
3541 
3542 		cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
3543 		put_unaligned_le64(ctx->fcp_cmnd_dma,
3544 				   &cmd_pkt->fcp_cmnd_dseg_address);
3545 
3546 		sp->flags |= SRB_FCP_CMND_DMA_VALID;
3547 		cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
3548 		/* Set total data segment count. */
3549 		cmd_pkt->entry_count = (uint8_t)req_cnt;
3550 		/* Specify response queue number where
3551 		 * completion should happen
3552 		 */
3553 		cmd_pkt->entry_status = (uint8_t) rsp->id;
3554 	} else {
3555 		struct cmd_type_7 *cmd_pkt;
3556 
3557 		req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
3558 		if (req->cnt < (req_cnt + 2)) {
3559 			cnt = (uint16_t)rd_reg_dword_relaxed(
3560 			    &reg->req_q_out[0]);
3561 			if (req->ring_index < cnt)
3562 				req->cnt = cnt - req->ring_index;
3563 			else
3564 				req->cnt = req->length -
3565 					(req->ring_index - cnt);
3566 		}
3567 		if (req->cnt < (req_cnt + 2))
3568 			goto queuing_error;
3569 
3570 		cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
3571 		cmd_pkt->handle = make_handle(req->id, handle);
3572 
3573 		/* Zero out remaining portion of packet. */
3574 		/* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
3575 		clr_ptr = (uint32_t *)cmd_pkt + 2;
3576 		memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
3577 		cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
3578 
3579 		/* Set NPORT-ID and LUN number*/
3580 		cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3581 		cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
3582 		cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
3583 		cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
3584 		cmd_pkt->vp_index = sp->vha->vp_idx;
3585 
3586 		int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
3587 		host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
3588 		    sizeof(cmd_pkt->lun));
3589 
3590 		/* Populate the FCP_PRIO. */
3591 		if (ha->flags.fcp_prio_enabled)
3592 			cmd_pkt->task |= sp->fcport->fcp_prio << 3;
3593 
3594 		/* Load SCSI command packet. */
3595 		memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
3596 		host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
3597 
3598 		cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
3599 
3600 		/* Build IOCB segments */
3601 		qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);
3602 
3603 		/* Set total data segment count. */
3604 		cmd_pkt->entry_count = (uint8_t)req_cnt;
3605 		/* Specify response queue number where
3606 		 * completion should happen.
3607 		 */
3608 		cmd_pkt->entry_status = (uint8_t) rsp->id;
3609 
3610 	}
3611 	/* Build command packet. */
3612 	req->current_outstanding_cmd = handle;
3613 	req->outstanding_cmds[handle] = sp;
3614 	sp->handle = handle;
3615 	cmd->host_scribble = (unsigned char *)(unsigned long)handle;
3616 	req->cnt -= req_cnt;
3617 	wmb();
3618 
3619 	/* Adjust ring index. */
3620 	req->ring_index++;
3621 	if (req->ring_index == req->length) {
3622 		req->ring_index = 0;
3623 		req->ring_ptr = req->ring;
3624 	} else
3625 		req->ring_ptr++;
3626 
3627 	sp->flags |= SRB_DMA_VALID;
3628 
3629 	/* Set chip new ring index. */
3630 	/* write, read and verify logic */
3631 	dbval = dbval | (req->id << 8) | (req->ring_index << 16);
3632 	if (ql2xdbwr)
3633 		qla82xx_wr_32(ha, (uintptr_t __force)ha->nxdb_wr_ptr, dbval);
3634 	else {
3635 		wrt_reg_dword(ha->nxdb_wr_ptr, dbval);
3636 		wmb();
3637 		while (rd_reg_dword(ha->nxdb_rd_ptr) != dbval) {
3638 			wrt_reg_dword(ha->nxdb_wr_ptr, dbval);
3639 			wmb();
3640 		}
3641 	}
3642 
3643 	/* Manage unprocessed RIO/ZIO commands in response queue. */
3644 	if (vha->flags.process_response_queue &&
3645 	    rsp->ring_ptr->signature != RESPONSE_PROCESSED)
3646 		qla24xx_process_response_queue(vha, rsp);
3647 
3648 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3649 	return QLA_SUCCESS;
3650 
3651 queuing_error_fcp_cmnd:
3652 	dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
3653 queuing_error:
3654 	if (tot_dsds)
3655 		scsi_dma_unmap(cmd);
3656 
3657 	if (sp->u.scmd.crc_ctx) {
3658 		mempool_free(sp->u.scmd.crc_ctx, ha->ctx_mempool);
3659 		sp->u.scmd.crc_ctx = NULL;
3660 	}
3661 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3662 
3663 	return QLA_FUNCTION_FAILED;
3664 }
3665 
3666 static void
3667 qla24xx_abort_iocb(srb_t *sp, struct abort_entry_24xx *abt_iocb)
3668 {
3669 	struct srb_iocb *aio = &sp->u.iocb_cmd;
3670 	scsi_qla_host_t *vha = sp->vha;
3671 	struct req_que *req = sp->qpair->req;
3672 	srb_t *orig_sp = sp->cmd_sp;
3673 
3674 	memset(abt_iocb, 0, sizeof(struct abort_entry_24xx));
3675 	abt_iocb->entry_type = ABORT_IOCB_TYPE;
3676 	abt_iocb->entry_count = 1;
3677 	abt_iocb->handle = make_handle(req->id, sp->handle);
3678 	if (sp->fcport) {
3679 		abt_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3680 		abt_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
3681 		abt_iocb->port_id[1] = sp->fcport->d_id.b.area;
3682 		abt_iocb->port_id[2] = sp->fcport->d_id.b.domain;
3683 	}
3684 	abt_iocb->handle_to_abort =
3685 		make_handle(le16_to_cpu(aio->u.abt.req_que_no),
3686 			    aio->u.abt.cmd_hndl);
3687 	abt_iocb->vp_index = vha->vp_idx;
3688 	abt_iocb->req_que_no = aio->u.abt.req_que_no;
3689 
3690 	/* need to pass original sp */
3691 	if (orig_sp)
3692 		qla_nvme_abort_set_option(abt_iocb, orig_sp);
3693 
3694 	/* Send the command to the firmware */
3695 	wmb();
3696 }
3697 
3698 static void
3699 qla2x00_mb_iocb(srb_t *sp, struct mbx_24xx_entry *mbx)
3700 {
3701 	int i, sz;
3702 
3703 	mbx->entry_type = MBX_IOCB_TYPE;
3704 	mbx->handle = sp->handle;
3705 	sz = min(ARRAY_SIZE(mbx->mb), ARRAY_SIZE(sp->u.iocb_cmd.u.mbx.out_mb));
3706 
3707 	for (i = 0; i < sz; i++)
3708 		mbx->mb[i] = sp->u.iocb_cmd.u.mbx.out_mb[i];
3709 }
3710 
3711 static void
3712 qla2x00_ctpthru_cmd_iocb(srb_t *sp, struct ct_entry_24xx *ct_pkt)
3713 {
3714 	sp->u.iocb_cmd.u.ctarg.iocb = ct_pkt;
3715 	qla24xx_prep_ms_iocb(sp->vha, &sp->u.iocb_cmd.u.ctarg);
3716 	ct_pkt->handle = sp->handle;
3717 }
3718 
3719 static void qla2x00_send_notify_ack_iocb(srb_t *sp,
3720 	struct nack_to_isp *nack)
3721 {
3722 	struct imm_ntfy_from_isp *ntfy = sp->u.iocb_cmd.u.nack.ntfy;
3723 
3724 	nack->entry_type = NOTIFY_ACK_TYPE;
3725 	nack->entry_count = 1;
3726 	nack->ox_id = ntfy->ox_id;
3727 
3728 	nack->u.isp24.handle = sp->handle;
3729 	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3730 	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3731 		nack->u.isp24.flags = ntfy->u.isp24.flags &
3732 			cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3733 	}
3734 	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3735 	nack->u.isp24.status = ntfy->u.isp24.status;
3736 	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3737 	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3738 	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3739 	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3740 	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3741 	nack->u.isp24.srr_flags = 0;
3742 	nack->u.isp24.srr_reject_code = 0;
3743 	nack->u.isp24.srr_reject_code_expl = 0;
3744 	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3745 
3746 	if (ntfy->u.isp24.status_subcode == ELS_PLOGI &&
3747 	    (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP) &&
3748 	    sp->vha->hw->flags.edif_enabled) {
3749 		ql_dbg(ql_dbg_disc, sp->vha, 0x3074,
3750 		    "%s PLOGI NACK sent with FC SECURITY bit, hdl=%x, loopid=%x, to pid %06x\n",
3751 		    sp->name, sp->handle, sp->fcport->loop_id,
3752 		    sp->fcport->d_id.b24);
3753 		nack->u.isp24.flags |= cpu_to_le16(NOTIFY_ACK_FLAGS_FCSP);
3754 	}
3755 }
3756 
3757 /*
3758  * Build NVME LS request
3759  */
3760 static void
3761 qla_nvme_ls(srb_t *sp, struct pt_ls4_request *cmd_pkt)
3762 {
3763 	struct srb_iocb *nvme;
3764 
3765 	nvme = &sp->u.iocb_cmd;
3766 	cmd_pkt->entry_type = PT_LS4_REQUEST;
3767 	cmd_pkt->entry_count = 1;
3768 	cmd_pkt->control_flags = cpu_to_le16(CF_LS4_ORIGINATOR << CF_LS4_SHIFT);
3769 
3770 	cmd_pkt->timeout = cpu_to_le16(nvme->u.nvme.timeout_sec);
3771 	cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3772 	cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
3773 
3774 	cmd_pkt->tx_dseg_count = cpu_to_le16(1);
3775 	cmd_pkt->tx_byte_count = cpu_to_le32(nvme->u.nvme.cmd_len);
3776 	cmd_pkt->dsd[0].length = cpu_to_le32(nvme->u.nvme.cmd_len);
3777 	put_unaligned_le64(nvme->u.nvme.cmd_dma, &cmd_pkt->dsd[0].address);
3778 
3779 	cmd_pkt->rx_dseg_count = cpu_to_le16(1);
3780 	cmd_pkt->rx_byte_count = cpu_to_le32(nvme->u.nvme.rsp_len);
3781 	cmd_pkt->dsd[1].length = cpu_to_le32(nvme->u.nvme.rsp_len);
3782 	put_unaligned_le64(nvme->u.nvme.rsp_dma, &cmd_pkt->dsd[1].address);
3783 }
3784 
3785 static void
3786 qla25xx_ctrlvp_iocb(srb_t *sp, struct vp_ctrl_entry_24xx *vce)
3787 {
3788 	int map, pos;
3789 
3790 	vce->entry_type = VP_CTRL_IOCB_TYPE;
3791 	vce->handle = sp->handle;
3792 	vce->entry_count = 1;
3793 	vce->command = cpu_to_le16(sp->u.iocb_cmd.u.ctrlvp.cmd);
3794 	vce->vp_count = cpu_to_le16(1);
3795 
3796 	/*
3797 	 * index map in firmware starts with 1; decrement index
3798 	 * this is ok as we never use index 0
3799 	 */
3800 	map = (sp->u.iocb_cmd.u.ctrlvp.vp_index - 1) / 8;
3801 	pos = (sp->u.iocb_cmd.u.ctrlvp.vp_index - 1) & 7;
3802 	vce->vp_idx_map[map] |= 1 << pos;
3803 }
3804 
3805 static void
3806 qla24xx_prlo_iocb(srb_t *sp, struct logio_entry_24xx *logio)
3807 {
3808 	logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
3809 	logio->control_flags =
3810 	    cpu_to_le16(LCF_COMMAND_PRLO|LCF_IMPL_PRLO);
3811 
3812 	logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3813 	logio->port_id[0] = sp->fcport->d_id.b.al_pa;
3814 	logio->port_id[1] = sp->fcport->d_id.b.area;
3815 	logio->port_id[2] = sp->fcport->d_id.b.domain;
3816 	logio->vp_index = sp->fcport->vha->vp_idx;
3817 }
3818 
3819 static int qla_get_iocbs_resource(struct srb *sp)
3820 {
3821 	bool get_exch;
3822 	bool push_it_through = false;
3823 
3824 	if (!ql2xenforce_iocb_limit) {
3825 		sp->iores.res_type = RESOURCE_NONE;
3826 		return 0;
3827 	}
3828 	sp->iores.res_type = RESOURCE_NONE;
3829 
3830 	switch (sp->type) {
3831 	case SRB_TM_CMD:
3832 	case SRB_PRLI_CMD:
3833 	case SRB_ADISC_CMD:
3834 		push_it_through = true;
3835 		fallthrough;
3836 	case SRB_LOGIN_CMD:
3837 	case SRB_ELS_CMD_RPT:
3838 	case SRB_ELS_CMD_HST:
3839 	case SRB_ELS_CMD_HST_NOLOGIN:
3840 	case SRB_CT_CMD:
3841 	case SRB_NVME_LS:
3842 	case SRB_ELS_DCMD:
3843 		get_exch = true;
3844 		break;
3845 
3846 	case SRB_FXIOCB_DCMD:
3847 	case SRB_FXIOCB_BCMD:
3848 		sp->iores.res_type = RESOURCE_NONE;
3849 		return 0;
3850 
3851 	case SRB_SA_UPDATE:
3852 	case SRB_SA_REPLACE:
3853 	case SRB_MB_IOCB:
3854 	case SRB_ABT_CMD:
3855 	case SRB_NACK_PLOGI:
3856 	case SRB_NACK_PRLI:
3857 	case SRB_NACK_LOGO:
3858 	case SRB_LOGOUT_CMD:
3859 	case SRB_CTRL_VP:
3860 	case SRB_MARKER:
3861 	default:
3862 		push_it_through = true;
3863 		get_exch = false;
3864 	}
3865 
3866 	sp->iores.res_type |= RESOURCE_IOCB;
3867 	sp->iores.iocb_cnt = 1;
3868 	if (get_exch) {
3869 		sp->iores.res_type |= RESOURCE_EXCH;
3870 		sp->iores.exch_cnt = 1;
3871 	}
3872 	if (push_it_through)
3873 		sp->iores.res_type |= RESOURCE_FORCE;
3874 
3875 	return qla_get_fw_resources(sp->qpair, &sp->iores);
3876 }
3877 
3878 static void
3879 qla_marker_iocb(srb_t *sp, struct mrk_entry_24xx *mrk)
3880 {
3881 	mrk->entry_type = MARKER_TYPE;
3882 	mrk->modifier = sp->u.iocb_cmd.u.tmf.modifier;
3883 	if (sp->u.iocb_cmd.u.tmf.modifier != MK_SYNC_ALL) {
3884 		mrk->nport_handle = cpu_to_le16(sp->u.iocb_cmd.u.tmf.loop_id);
3885 		int_to_scsilun(sp->u.iocb_cmd.u.tmf.lun, (struct scsi_lun *)&mrk->lun);
3886 		host_to_fcp_swap(mrk->lun, sizeof(mrk->lun));
3887 		mrk->vp_index = sp->u.iocb_cmd.u.tmf.vp_index;
3888 	}
3889 }
3890 
3891 int
3892 qla2x00_start_sp(srb_t *sp)
3893 {
3894 	int rval = QLA_SUCCESS;
3895 	scsi_qla_host_t *vha = sp->vha;
3896 	struct qla_hw_data *ha = vha->hw;
3897 	struct qla_qpair *qp = sp->qpair;
3898 	void *pkt;
3899 	unsigned long flags;
3900 
3901 	if (vha->hw->flags.eeh_busy)
3902 		return -EIO;
3903 
3904 	spin_lock_irqsave(qp->qp_lock_ptr, flags);
3905 	rval = qla_get_iocbs_resource(sp);
3906 	if (rval) {
3907 		spin_unlock_irqrestore(qp->qp_lock_ptr, flags);
3908 		return -EAGAIN;
3909 	}
3910 
3911 	pkt = __qla2x00_alloc_iocbs(sp->qpair, sp);
3912 	if (!pkt) {
3913 		rval = EAGAIN;
3914 		ql_log(ql_log_warn, vha, 0x700c,
3915 		    "qla2x00_alloc_iocbs failed.\n");
3916 		goto done;
3917 	}
3918 
3919 	switch (sp->type) {
3920 	case SRB_LOGIN_CMD:
3921 		IS_FWI2_CAPABLE(ha) ?
3922 		    qla24xx_login_iocb(sp, pkt) :
3923 		    qla2x00_login_iocb(sp, pkt);
3924 		break;
3925 	case SRB_PRLI_CMD:
3926 		qla24xx_prli_iocb(sp, pkt);
3927 		break;
3928 	case SRB_LOGOUT_CMD:
3929 		IS_FWI2_CAPABLE(ha) ?
3930 		    qla24xx_logout_iocb(sp, pkt) :
3931 		    qla2x00_logout_iocb(sp, pkt);
3932 		break;
3933 	case SRB_ELS_CMD_RPT:
3934 	case SRB_ELS_CMD_HST:
3935 		qla24xx_els_iocb(sp, pkt);
3936 		break;
3937 	case SRB_ELS_CMD_HST_NOLOGIN:
3938 		qla_els_pt_iocb(sp->vha, pkt,  &sp->u.bsg_cmd.u.els_arg);
3939 		((struct els_entry_24xx *)pkt)->handle = sp->handle;
3940 		break;
3941 	case SRB_CT_CMD:
3942 		IS_FWI2_CAPABLE(ha) ?
3943 		    qla24xx_ct_iocb(sp, pkt) :
3944 		    qla2x00_ct_iocb(sp, pkt);
3945 		break;
3946 	case SRB_ADISC_CMD:
3947 		IS_FWI2_CAPABLE(ha) ?
3948 		    qla24xx_adisc_iocb(sp, pkt) :
3949 		    qla2x00_adisc_iocb(sp, pkt);
3950 		break;
3951 	case SRB_TM_CMD:
3952 		IS_QLAFX00(ha) ?
3953 		    qlafx00_tm_iocb(sp, pkt) :
3954 		    qla24xx_tm_iocb(sp, pkt);
3955 		break;
3956 	case SRB_FXIOCB_DCMD:
3957 	case SRB_FXIOCB_BCMD:
3958 		qlafx00_fxdisc_iocb(sp, pkt);
3959 		break;
3960 	case SRB_NVME_LS:
3961 		qla_nvme_ls(sp, pkt);
3962 		break;
3963 	case SRB_ABT_CMD:
3964 		IS_QLAFX00(ha) ?
3965 			qlafx00_abort_iocb(sp, pkt) :
3966 			qla24xx_abort_iocb(sp, pkt);
3967 		break;
3968 	case SRB_ELS_DCMD:
3969 		qla24xx_els_logo_iocb(sp, pkt);
3970 		break;
3971 	case SRB_CT_PTHRU_CMD:
3972 		qla2x00_ctpthru_cmd_iocb(sp, pkt);
3973 		break;
3974 	case SRB_MB_IOCB:
3975 		qla2x00_mb_iocb(sp, pkt);
3976 		break;
3977 	case SRB_NACK_PLOGI:
3978 	case SRB_NACK_PRLI:
3979 	case SRB_NACK_LOGO:
3980 		qla2x00_send_notify_ack_iocb(sp, pkt);
3981 		break;
3982 	case SRB_CTRL_VP:
3983 		qla25xx_ctrlvp_iocb(sp, pkt);
3984 		break;
3985 	case SRB_PRLO_CMD:
3986 		qla24xx_prlo_iocb(sp, pkt);
3987 		break;
3988 	case SRB_SA_UPDATE:
3989 		qla24xx_sa_update_iocb(sp, pkt);
3990 		break;
3991 	case SRB_SA_REPLACE:
3992 		qla24xx_sa_replace_iocb(sp, pkt);
3993 		break;
3994 	case SRB_MARKER:
3995 		qla_marker_iocb(sp, pkt);
3996 		break;
3997 	default:
3998 		break;
3999 	}
4000 
4001 	if (sp->start_timer) {
4002 		/* ref: TMR timer ref
4003 		 * this code should be just before start_iocbs function
4004 		 * This will make sure that caller function don't to do
4005 		 * kref_put even on failure
4006 		 */
4007 		kref_get(&sp->cmd_kref);
4008 		add_timer(&sp->u.iocb_cmd.timer);
4009 	}
4010 
4011 	wmb();
4012 	qla2x00_start_iocbs(vha, qp->req);
4013 done:
4014 	if (rval)
4015 		qla_put_fw_resources(sp->qpair, &sp->iores);
4016 	spin_unlock_irqrestore(qp->qp_lock_ptr, flags);
4017 	return rval;
4018 }
4019 
4020 static void
4021 qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha,
4022 				struct cmd_bidir *cmd_pkt, uint32_t tot_dsds)
4023 {
4024 	uint16_t avail_dsds;
4025 	struct dsd64 *cur_dsd;
4026 	uint32_t req_data_len = 0;
4027 	uint32_t rsp_data_len = 0;
4028 	struct scatterlist *sg;
4029 	int index;
4030 	int entry_count = 1;
4031 	struct bsg_job *bsg_job = sp->u.bsg_job;
4032 
4033 	/*Update entry type to indicate bidir command */
4034 	put_unaligned_le32(COMMAND_BIDIRECTIONAL, &cmd_pkt->entry_type);
4035 
4036 	/* Set the transfer direction, in this set both flags
4037 	 * Also set the BD_WRAP_BACK flag, firmware will take care
4038 	 * assigning DID=SID for outgoing pkts.
4039 	 */
4040 	cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
4041 	cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
4042 	cmd_pkt->control_flags = cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA |
4043 							BD_WRAP_BACK);
4044 
4045 	req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
4046 	cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len);
4047 	cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len);
4048 	cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2);
4049 
4050 	vha->bidi_stats.transfer_bytes += req_data_len;
4051 	vha->bidi_stats.io_count++;
4052 
4053 	vha->qla_stats.output_bytes += req_data_len;
4054 	vha->qla_stats.output_requests++;
4055 
4056 	/* Only one dsd is available for bidirectional IOCB, remaining dsds
4057 	 * are bundled in continuation iocb
4058 	 */
4059 	avail_dsds = 1;
4060 	cur_dsd = &cmd_pkt->fcp_dsd;
4061 
4062 	index = 0;
4063 
4064 	for_each_sg(bsg_job->request_payload.sg_list, sg,
4065 				bsg_job->request_payload.sg_cnt, index) {
4066 		cont_a64_entry_t *cont_pkt;
4067 
4068 		/* Allocate additional continuation packets */
4069 		if (avail_dsds == 0) {
4070 			/* Continuation type 1 IOCB can accomodate
4071 			 * 5 DSDS
4072 			 */
4073 			cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
4074 			cur_dsd = cont_pkt->dsd;
4075 			avail_dsds = 5;
4076 			entry_count++;
4077 		}
4078 		append_dsd64(&cur_dsd, sg);
4079 		avail_dsds--;
4080 	}
4081 	/* For read request DSD will always goes to continuation IOCB
4082 	 * and follow the write DSD. If there is room on the current IOCB
4083 	 * then it is added to that IOCB else new continuation IOCB is
4084 	 * allocated.
4085 	 */
4086 	for_each_sg(bsg_job->reply_payload.sg_list, sg,
4087 				bsg_job->reply_payload.sg_cnt, index) {
4088 		cont_a64_entry_t *cont_pkt;
4089 
4090 		/* Allocate additional continuation packets */
4091 		if (avail_dsds == 0) {
4092 			/* Continuation type 1 IOCB can accomodate
4093 			 * 5 DSDS
4094 			 */
4095 			cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
4096 			cur_dsd = cont_pkt->dsd;
4097 			avail_dsds = 5;
4098 			entry_count++;
4099 		}
4100 		append_dsd64(&cur_dsd, sg);
4101 		avail_dsds--;
4102 	}
4103 	/* This value should be same as number of IOCB required for this cmd */
4104 	cmd_pkt->entry_count = entry_count;
4105 }
4106 
4107 int
4108 qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds)
4109 {
4110 
4111 	struct qla_hw_data *ha = vha->hw;
4112 	unsigned long flags;
4113 	uint32_t handle;
4114 	uint16_t req_cnt;
4115 	uint16_t cnt;
4116 	uint32_t *clr_ptr;
4117 	struct cmd_bidir *cmd_pkt = NULL;
4118 	struct rsp_que *rsp;
4119 	struct req_que *req;
4120 	int rval = EXT_STATUS_OK;
4121 
4122 	rval = QLA_SUCCESS;
4123 
4124 	rsp = ha->rsp_q_map[0];
4125 	req = vha->req;
4126 
4127 	/* Send marker if required */
4128 	if (vha->marker_needed != 0) {
4129 		if (qla2x00_marker(vha, ha->base_qpair,
4130 			0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
4131 			return EXT_STATUS_MAILBOX;
4132 		vha->marker_needed = 0;
4133 	}
4134 
4135 	/* Acquire ring specific lock */
4136 	spin_lock_irqsave(&ha->hardware_lock, flags);
4137 
4138 	handle = qla2xxx_get_next_handle(req);
4139 	if (handle == 0) {
4140 		rval = EXT_STATUS_BUSY;
4141 		goto queuing_error;
4142 	}
4143 
4144 	/* Calculate number of IOCB required */
4145 	req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
4146 
4147 	/* Check for room on request queue. */
4148 	if (req->cnt < req_cnt + 2) {
4149 		if (IS_SHADOW_REG_CAPABLE(ha)) {
4150 			cnt = *req->out_ptr;
4151 		} else {
4152 			cnt = rd_reg_dword_relaxed(req->req_q_out);
4153 			if (qla2x00_check_reg16_for_disconnect(vha, cnt))
4154 				goto queuing_error;
4155 		}
4156 
4157 		if  (req->ring_index < cnt)
4158 			req->cnt = cnt - req->ring_index;
4159 		else
4160 			req->cnt = req->length -
4161 				(req->ring_index - cnt);
4162 	}
4163 	if (req->cnt < req_cnt + 2) {
4164 		rval = EXT_STATUS_BUSY;
4165 		goto queuing_error;
4166 	}
4167 
4168 	cmd_pkt = (struct cmd_bidir *)req->ring_ptr;
4169 	cmd_pkt->handle = make_handle(req->id, handle);
4170 
4171 	/* Zero out remaining portion of packet. */
4172 	/* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
4173 	clr_ptr = (uint32_t *)cmd_pkt + 2;
4174 	memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
4175 
4176 	/* Set NPORT-ID  (of vha)*/
4177 	cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id);
4178 	cmd_pkt->port_id[0] = vha->d_id.b.al_pa;
4179 	cmd_pkt->port_id[1] = vha->d_id.b.area;
4180 	cmd_pkt->port_id[2] = vha->d_id.b.domain;
4181 
4182 	qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds);
4183 	cmd_pkt->entry_status = (uint8_t) rsp->id;
4184 	/* Build command packet. */
4185 	req->current_outstanding_cmd = handle;
4186 	req->outstanding_cmds[handle] = sp;
4187 	sp->handle = handle;
4188 	req->cnt -= req_cnt;
4189 
4190 	/* Send the command to the firmware */
4191 	wmb();
4192 	qla2x00_start_iocbs(vha, req);
4193 queuing_error:
4194 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
4195 
4196 	return rval;
4197 }
4198