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