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