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