blob: 782b30d0eea1741f746f4cdcadbdf373d7d77d4e [file] [log] [blame]
Andrew Vasquezfa90c542005-10-27 11:10:08 -07001/*
2 * QLogic Fibre Channel HBA Driver
Andrew Vasquez01e58d82008-04-03 13:13:13 -07003 * Copyright (c) 2003-2008 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "qla_def.h"
8
9#include <linux/blkdev.h>
10#include <linux/delay.h>
11
12#include <scsi/scsi_tcq.h>
13
Anirban Chakraborty73208df2008-12-09 16:45:39 -080014static request_t *qla2x00_req_pkt(struct scsi_qla_host *, struct req_que *,
15 struct rsp_que *rsp);
16static void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -070018static void qla25xx_set_que(srb_t *, struct rsp_que **);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/**
20 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
21 * @cmd: SCSI command
22 *
23 * Returns the proper CF_* direction based on CDB.
24 */
25static inline uint16_t
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070026qla2x00_get_cmd_direction(srb_t *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 uint16_t cflags;
29
30 cflags = 0;
31
32 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070033 if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 cflags = CF_WRITE;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080035 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070036 scsi_bufflen(sp->cmd);
37 } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 cflags = CF_READ;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080039 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070040 scsi_bufflen(sp->cmd);
41 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return (cflags);
43}
44
45/**
46 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
47 * Continuation Type 0 IOCBs to allocate.
48 *
49 * @dsds: number of data segment decriptors needed
50 *
51 * Returns the number of IOCB entries needed to store @dsds.
52 */
53uint16_t
54qla2x00_calc_iocbs_32(uint16_t dsds)
55{
56 uint16_t iocbs;
57
58 iocbs = 1;
59 if (dsds > 3) {
60 iocbs += (dsds - 3) / 7;
61 if ((dsds - 3) % 7)
62 iocbs++;
63 }
64 return (iocbs);
65}
66
67/**
68 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
69 * Continuation Type 1 IOCBs to allocate.
70 *
71 * @dsds: number of data segment decriptors needed
72 *
73 * Returns the number of IOCB entries needed to store @dsds.
74 */
75uint16_t
76qla2x00_calc_iocbs_64(uint16_t dsds)
77{
78 uint16_t iocbs;
79
80 iocbs = 1;
81 if (dsds > 2) {
82 iocbs += (dsds - 2) / 5;
83 if ((dsds - 2) % 5)
84 iocbs++;
85 }
86 return (iocbs);
87}
88
89/**
90 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
91 * @ha: HA context
92 *
93 * Returns a pointer to the Continuation Type 0 IOCB packet.
94 */
95static inline cont_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070096qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 cont_entry_t *cont_pkt;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070099 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800101 req->ring_index++;
102 if (req->ring_index == req->length) {
103 req->ring_index = 0;
104 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800106 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800109 cont_pkt = (cont_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /* Load packet defaults. */
112 *((uint32_t *)(&cont_pkt->entry_type)) =
113 __constant_cpu_to_le32(CONTINUE_TYPE);
114
115 return (cont_pkt);
116}
117
118/**
119 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
120 * @ha: HA context
121 *
122 * Returns a pointer to the continuation type 1 IOCB packet.
123 */
124static inline cont_a64_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700125qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 cont_a64_entry_t *cont_pkt;
128
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700129 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800131 req->ring_index++;
132 if (req->ring_index == req->length) {
133 req->ring_index = 0;
134 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800136 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800139 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* Load packet defaults. */
142 *((uint32_t *)(&cont_pkt->entry_type)) =
143 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
144
145 return (cont_pkt);
146}
147
Arun Easibad75002010-05-04 15:01:30 -0700148static inline int
149qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
150{
151 uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
152
153 /* We only support T10 DIF right now */
154 if (guard != SHOST_DIX_GUARD_CRC) {
155 DEBUG2(printk(KERN_ERR "Unsupported guard: %d\n", guard));
156 return 0;
157 }
158
159 /* We always use DIFF Bundling for best performance */
160 *fw_prot_opts = 0;
161
162 /* Translate SCSI opcode to a protection opcode */
163 switch (scsi_get_prot_op(sp->cmd)) {
164 case SCSI_PROT_READ_STRIP:
165 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
166 break;
167 case SCSI_PROT_WRITE_INSERT:
168 *fw_prot_opts |= PO_MODE_DIF_INSERT;
169 break;
170 case SCSI_PROT_READ_INSERT:
171 *fw_prot_opts |= PO_MODE_DIF_INSERT;
172 break;
173 case SCSI_PROT_WRITE_STRIP:
174 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
175 break;
176 case SCSI_PROT_READ_PASS:
177 *fw_prot_opts |= PO_MODE_DIF_PASS;
178 break;
179 case SCSI_PROT_WRITE_PASS:
180 *fw_prot_opts |= PO_MODE_DIF_PASS;
181 break;
182 default: /* Normal Request */
183 *fw_prot_opts |= PO_MODE_DIF_PASS;
184 break;
185 }
186
187 return scsi_prot_sg_count(sp->cmd);
188}
189
190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
192 * capable IOCB types.
193 *
194 * @sp: SRB command to process
195 * @cmd_pkt: Command type 2 IOCB
196 * @tot_dsds: Total number of segments to transfer
197 */
198void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
199 uint16_t tot_dsds)
200{
201 uint16_t avail_dsds;
202 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800203 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900205 struct scatterlist *sg;
206 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 cmd = sp->cmd;
209
210 /* Update entry type to indicate Command Type 2 IOCB */
211 *((uint32_t *)(&cmd_pkt->entry_type)) =
212 __constant_cpu_to_le32(COMMAND_TYPE);
213
214 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900215 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
217 return;
218 }
219
Andrew Vasquez444786d2009-01-05 11:18:10 -0800220 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700221 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* Three DSDs are available in the Command Type 2 IOCB */
224 avail_dsds = 3;
225 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
226
227 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900228 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
229 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900231 /* Allocate additional continuation packets? */
232 if (avail_dsds == 0) {
233 /*
234 * Seven DSDs are available in the Continuation
235 * Type 0 IOCB.
236 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700237 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900238 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
239 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900241
242 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
243 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
244 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246}
247
248/**
249 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
250 * capable IOCB types.
251 *
252 * @sp: SRB command to process
253 * @cmd_pkt: Command type 3 IOCB
254 * @tot_dsds: Total number of segments to transfer
255 */
256void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
257 uint16_t tot_dsds)
258{
259 uint16_t avail_dsds;
260 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800261 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900263 struct scatterlist *sg;
264 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 cmd = sp->cmd;
267
268 /* Update entry type to indicate Command Type 3 IOCB */
269 *((uint32_t *)(&cmd_pkt->entry_type)) =
270 __constant_cpu_to_le32(COMMAND_A64_TYPE);
271
272 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900273 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
275 return;
276 }
277
Andrew Vasquez444786d2009-01-05 11:18:10 -0800278 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700279 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* Two DSDs are available in the Command Type 3 IOCB */
282 avail_dsds = 2;
283 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
284
285 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900286 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
287 dma_addr_t sle_dma;
288 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900290 /* Allocate additional continuation packets? */
291 if (avail_dsds == 0) {
292 /*
293 * Five DSDs are available in the Continuation
294 * Type 1 IOCB.
295 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700296 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900297 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
298 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900300
301 sle_dma = sg_dma_address(sg);
302 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
303 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
304 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
305 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307}
308
309/**
310 * qla2x00_start_scsi() - Send a SCSI command to the ISP
311 * @sp: command to send to the ISP
312 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700313 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
315int
316qla2x00_start_scsi(srb_t *sp)
317{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900318 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800320 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct scsi_cmnd *cmd;
322 uint32_t *clr_ptr;
323 uint32_t index;
324 uint32_t handle;
325 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 uint16_t cnt;
327 uint16_t req_cnt;
328 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700329 struct device_reg_2xxx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800330 struct qla_hw_data *ha;
331 struct req_que *req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800332 struct rsp_que *rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Setup device pointers. */
335 ret = 0;
Andrew Vasquez444786d2009-01-05 11:18:10 -0800336 vha = sp->fcport->vha;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800337 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700338 reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 cmd = sp->cmd;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800340 req = ha->req_q_map[0];
341 rsp = ha->rsp_q_map[0];
83021922005-04-17 15:10:41 -0500342 /* So we know we haven't pci_map'ed anything yet */
343 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800346 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800347 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
348 != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return (QLA_FUNCTION_FAILED);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800350 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
353 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700354 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800357 handle = req->current_outstanding_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
359 handle++;
360 if (handle == MAX_OUTSTANDING_COMMANDS)
361 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800362 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 }
365 if (index == MAX_OUTSTANDING_COMMANDS)
366 goto queuing_error;
367
83021922005-04-17 15:10:41 -0500368 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700369 if (scsi_sg_count(cmd)) {
370 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
371 scsi_sg_count(cmd), cmd->sc_data_direction);
372 if (unlikely(!nseg))
373 goto queuing_error;
374 } else
375 nseg = 0;
376
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900377 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700380 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800381 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800383 if (req->ring_index < cnt)
384 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800386 req->cnt = req->length -
387 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800389 if (req->cnt < (req_cnt + 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 goto queuing_error;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800393 req->current_outstanding_cmd = handle;
394 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -0700395 sp->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800397 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800399 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 cmd_pkt->handle = handle;
401 /* Zero out remaining portion of packet. */
402 clr_ptr = (uint32_t *)cmd_pkt + 2;
403 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
404 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
405
bdf79622005-04-17 15:06:53 -0500406 /* Set target ID and LUN number*/
407 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
408 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* Update tagged queuing modifier */
411 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* Load SCSI command packet. */
414 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900415 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700418 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* Set total data segment count. */
421 cmd_pkt->entry_count = (uint8_t)req_cnt;
422 wmb();
423
424 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800425 req->ring_index++;
426 if (req->ring_index == req->length) {
427 req->ring_index = 0;
428 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800430 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800435 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
437
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700438 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800439 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800440 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
441 qla2x00_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700442
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700443 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return (QLA_SUCCESS);
445
446queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900447 if (tot_dsds)
448 scsi_dma_unmap(cmd);
449
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700450 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 return (QLA_FUNCTION_FAILED);
453}
454
455/**
456 * qla2x00_marker() - Send a marker IOCB to the firmware.
457 * @ha: HA context
458 * @loop_id: loop ID
459 * @lun: LUN
460 * @type: marker modifier
461 *
462 * Can be called from both normal and interrupt context.
463 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700464 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700466int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800467__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
468 struct rsp_que *rsp, uint16_t loop_id,
469 uint16_t lun, uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700471 mrk_entry_t *mrk;
472 struct mrk_entry_24xx *mrk24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800473 struct qla_hw_data *ha = vha->hw;
474 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700476 mrk24 = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800477 mrk = (mrk_entry_t *)qla2x00_req_pkt(vha, req, rsp);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700478 if (mrk == NULL) {
479 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800480 __func__, base_vha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 return (QLA_FUNCTION_FAILED);
483 }
484
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700485 mrk->entry_type = MARKER_TYPE;
486 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700488 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700489 mrk24 = (struct mrk_entry_24xx *) mrk;
490 mrk24->nport_handle = cpu_to_le16(loop_id);
491 mrk24->lun[1] = LSB(lun);
492 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700493 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800494 mrk24->vp_index = vha->vp_idx;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700495 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700496 } else {
497 SET_TARGET_ID(ha, mrk->target, loop_id);
498 mrk->lun = cpu_to_le16(lun);
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 wmb();
502
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800503 qla2x00_isp_cmd(vha, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 return (QLA_SUCCESS);
506}
507
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700508int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800509qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
510 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
511 uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 int ret;
514 unsigned long flags = 0;
515
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800516 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
517 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
518 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 return (ret);
521}
522
523/**
524 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
525 * @ha: HA context
526 *
527 * Note: The caller must hold the hardware lock before calling this routine.
528 *
529 * Returns NULL if function failed, else, a pointer to the request packet.
530 */
531static request_t *
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800532qla2x00_req_pkt(struct scsi_qla_host *vha, struct req_que *req,
533 struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800535 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800536 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 request_t *pkt = NULL;
538 uint16_t cnt;
539 uint32_t *dword_ptr;
540 uint32_t timer;
541 uint16_t req_cnt = 1;
542
543 /* Wait 1 second for slot. */
544 for (timer = HZ; timer; timer--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800545 if ((req_cnt + 2) >= req->cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Calculate number of free request entries. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800547 if (ha->mqenable)
548 cnt = (uint16_t)
549 RD_REG_DWORD(&reg->isp25mq.req_q_out);
550 else {
Giridhar Malavalia9083012010-04-12 17:59:55 -0700551 if (IS_QLA82XX(ha))
552 cnt = (uint16_t)RD_REG_DWORD(
553 &reg->isp82.req_q_out);
554 else if (IS_FWI2_CAPABLE(ha))
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800555 cnt = (uint16_t)RD_REG_DWORD(
556 &reg->isp24.req_q_out);
557 else
558 cnt = qla2x00_debounce_register(
559 ISP_REQ_Q_OUT(ha, &reg->isp));
560 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800561 if (req->ring_index < cnt)
562 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800564 req->cnt = req->length -
565 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 /* If room for request in request ring. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800568 if ((req_cnt + 2) < req->cnt) {
569 req->cnt--;
570 pkt = req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* Zero out packet. */
573 dword_ptr = (uint32_t *)pkt;
574 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
575 *dword_ptr++ = 0;
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* Set entry count. */
578 pkt->entry_count = 1;
579
580 break;
581 }
582
583 /* Release ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800584 spin_unlock_irq(&ha->hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 udelay(2); /* 2 us */
587
588 /* Check for pending interrupts. */
589 /* During init we issue marker directly */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800590 if (!vha->marker_needed && !vha->flags.init_done)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800591 qla2x00_poll(rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock_irq(&ha->hardware_lock);
593 }
594 if (!pkt) {
595 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
596 }
597
598 return (pkt);
599}
600
601/**
602 * qla2x00_isp_cmd() - Modify the request ring pointer.
603 * @ha: HA context
604 *
605 * Note: The caller must hold the hardware lock before calling this routine.
606 */
Adrian Bunk413975a2006-06-30 02:33:06 -0700607static void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800608qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800610 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800611 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800612 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 DEBUG5(printk("%s(): IOCB data:\n", __func__));
615 DEBUG5(qla2x00_dump_buffer(
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800616 (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800619 req->ring_index++;
620 if (req->ring_index == req->length) {
621 req->ring_index = 0;
622 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800624 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* Set chip new ring index. */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700627 if (IS_QLA82XX(ha)) {
628 uint32_t dbval = 0x04 | (ha->portnum << 5);
629
630 /* write, read and verify logic */
631 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
632 if (ql2xdbwr)
633 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
634 else {
635 WRT_REG_DWORD(
636 (unsigned long __iomem *)ha->nxdb_wr_ptr,
637 dbval);
638 wmb();
639 while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
640 WRT_REG_DWORD((unsigned long __iomem *)
641 ha->nxdb_wr_ptr, dbval);
642 wmb();
643 }
644 }
645 } else if (ha->mqenable) {
646 /* Set chip new ring index. */
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800647 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
648 RD_REG_DWORD(&ioreg->hccr);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700649 } else {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800650 if (IS_FWI2_CAPABLE(ha)) {
651 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
652 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
653 } else {
654 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
655 req->ring_index);
656 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
657 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700658 }
659
660}
661
662/**
663 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
664 * Continuation Type 1 IOCBs to allocate.
665 *
666 * @dsds: number of data segment decriptors needed
667 *
668 * Returns the number of IOCB entries needed to store @dsds.
669 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700670inline uint16_t
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700671qla24xx_calc_iocbs(uint16_t dsds)
672{
673 uint16_t iocbs;
674
675 iocbs = 1;
676 if (dsds > 1) {
677 iocbs += (dsds - 1) / 5;
678 if ((dsds - 1) % 5)
679 iocbs++;
680 }
Arun Easibad75002010-05-04 15:01:30 -0700681 DEBUG3(printk(KERN_DEBUG "%s(): Required PKT(s) = %d\n",
682 __func__, iocbs));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700683 return iocbs;
684}
685
686/**
687 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
688 * IOCB types.
689 *
690 * @sp: SRB command to process
691 * @cmd_pkt: Command type 3 IOCB
692 * @tot_dsds: Total number of segments to transfer
693 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700694inline void
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700695qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
696 uint16_t tot_dsds)
697{
698 uint16_t avail_dsds;
699 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800700 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700701 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900702 struct scatterlist *sg;
703 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800704 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700705
706 cmd = sp->cmd;
707
708 /* Update entry type to indicate Command Type 3 IOCB */
709 *((uint32_t *)(&cmd_pkt->entry_type)) =
710 __constant_cpu_to_le32(COMMAND_TYPE_7);
711
712 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900713 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700714 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
715 return;
716 }
717
Andrew Vasquez444786d2009-01-05 11:18:10 -0800718 vha = sp->fcport->vha;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700719 req = vha->req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700720
721 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700722 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700723 cmd_pkt->task_mgmt_flags =
724 __constant_cpu_to_le16(TMF_WRITE_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800725 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700726 scsi_bufflen(sp->cmd);
727 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700728 cmd_pkt->task_mgmt_flags =
729 __constant_cpu_to_le16(TMF_READ_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800730 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700731 scsi_bufflen(sp->cmd);
732 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700733
734 /* One DSD is available in the Command Type 3 IOCB */
735 avail_dsds = 1;
736 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
737
738 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700739
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900740 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
741 dma_addr_t sle_dma;
742 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700743
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900744 /* Allocate additional continuation packets? */
745 if (avail_dsds == 0) {
746 /*
747 * Five DSDs are available in the Continuation
748 * Type 1 IOCB.
749 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700750 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900751 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
752 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700753 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900754
755 sle_dma = sg_dma_address(sg);
756 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
757 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
758 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
759 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700760 }
761}
762
Arun Easibad75002010-05-04 15:01:30 -0700763struct fw_dif_context {
764 uint32_t ref_tag;
765 uint16_t app_tag;
766 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
767 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
768};
769
770/*
771 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
772 *
773 */
774static inline void
775qla24xx_set_t10dif_tags(struct scsi_cmnd *cmd, struct fw_dif_context *pkt,
776 unsigned int protcnt)
777{
778 struct sd_dif_tuple *spt;
779 unsigned char op = scsi_get_prot_op(cmd);
780
781 switch (scsi_get_prot_type(cmd)) {
782 /* For TYPE 0 protection: no checking */
783 case SCSI_PROT_DIF_TYPE0:
784 pkt->ref_tag_mask[0] = 0x00;
785 pkt->ref_tag_mask[1] = 0x00;
786 pkt->ref_tag_mask[2] = 0x00;
787 pkt->ref_tag_mask[3] = 0x00;
788 break;
789
790 /*
791 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
792 * match LBA in CDB + N
793 */
794 case SCSI_PROT_DIF_TYPE2:
795 break;
796
797 /* For Type 3 protection: 16 bit GUARD only */
798 case SCSI_PROT_DIF_TYPE3:
799 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
800 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
801 0x00;
802 break;
803
804 /*
805 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
806 * 16 bit app tag.
807 */
808 case SCSI_PROT_DIF_TYPE1:
809 if (!ql2xenablehba_err_chk)
810 break;
811
812 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
813 op == SCSI_PROT_WRITE_PASS)) {
814 spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
815 scsi_prot_sglist(cmd)[0].offset;
816 DEBUG18(printk(KERN_DEBUG
817 "%s(): LBA from user %p, lba = 0x%x\n",
818 __func__, spt, (int)spt->ref_tag));
819 pkt->ref_tag = swab32(spt->ref_tag);
820 pkt->app_tag_mask[0] = 0x0;
821 pkt->app_tag_mask[1] = 0x0;
822 } else {
823 pkt->ref_tag = cpu_to_le32((uint32_t)
824 (0xffffffff & scsi_get_lba(cmd)));
825 pkt->app_tag = __constant_cpu_to_le16(0);
826 pkt->app_tag_mask[0] = 0x0;
827 pkt->app_tag_mask[1] = 0x0;
828 }
829 /* enable ALL bytes of the ref tag */
830 pkt->ref_tag_mask[0] = 0xff;
831 pkt->ref_tag_mask[1] = 0xff;
832 pkt->ref_tag_mask[2] = 0xff;
833 pkt->ref_tag_mask[3] = 0xff;
834 break;
835 }
836
837 DEBUG18(printk(KERN_DEBUG
838 "%s(): Setting protection Tags: (BIG) ref tag = 0x%x,"
839 " app tag = 0x%x, prot SG count %d , cmd lba 0x%x,"
840 " prot_type=%u\n", __func__, pkt->ref_tag, pkt->app_tag, protcnt,
841 (int)scsi_get_lba(cmd), scsi_get_prot_type(cmd)));
842}
843
844
845static int
846qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
847 uint16_t tot_dsds)
848{
849 void *next_dsd;
850 uint8_t avail_dsds = 0;
851 uint32_t dsd_list_len;
852 struct dsd_dma *dsd_ptr;
853 struct scatterlist *sg;
854 uint32_t *cur_dsd = dsd;
855 int i;
856 uint16_t used_dsds = tot_dsds;
857
858 uint8_t *cp;
859
860 scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
861 dma_addr_t sle_dma;
862
863 /* Allocate additional continuation packets? */
864 if (avail_dsds == 0) {
865 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
866 QLA_DSDS_PER_IOCB : used_dsds;
867 dsd_list_len = (avail_dsds + 1) * 12;
868 used_dsds -= avail_dsds;
869
870 /* allocate tracking DS */
871 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
872 if (!dsd_ptr)
873 return 1;
874
875 /* allocate new list */
876 dsd_ptr->dsd_addr = next_dsd =
877 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
878 &dsd_ptr->dsd_list_dma);
879
880 if (!next_dsd) {
881 /*
882 * Need to cleanup only this dsd_ptr, rest
883 * will be done by sp_free_dma()
884 */
885 kfree(dsd_ptr);
886 return 1;
887 }
888
889 list_add_tail(&dsd_ptr->list,
890 &((struct crc_context *)sp->ctx)->dsd_list);
891
892 sp->flags |= SRB_CRC_CTX_DSD_VALID;
893
894 /* add new list to cmd iocb or last list */
895 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
896 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
897 *cur_dsd++ = dsd_list_len;
898 cur_dsd = (uint32_t *)next_dsd;
899 }
900 sle_dma = sg_dma_address(sg);
901 DEBUG18(printk("%s(): %p, sg entry %d - addr =0x%x 0x%x,"
902 " len =%d\n", __func__ , cur_dsd, i, LSD(sle_dma),
903 MSD(sle_dma), sg_dma_len(sg)));
904 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
905 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
906 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
907 avail_dsds--;
908
909 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
910 cp = page_address(sg_page(sg)) + sg->offset;
911 DEBUG18(printk("%s(): User Data buffer= %p:\n",
912 __func__ , cp));
913 }
914 }
915 /* Null termination */
916 *cur_dsd++ = 0;
917 *cur_dsd++ = 0;
918 *cur_dsd++ = 0;
919 return 0;
920}
921
922static int
923qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
924 uint32_t *dsd,
925 uint16_t tot_dsds)
926{
927 void *next_dsd;
928 uint8_t avail_dsds = 0;
929 uint32_t dsd_list_len;
930 struct dsd_dma *dsd_ptr;
931 struct scatterlist *sg;
932 int i;
933 struct scsi_cmnd *cmd;
934 uint32_t *cur_dsd = dsd;
935 uint16_t used_dsds = tot_dsds;
936
937 uint8_t *cp;
938
939
940 cmd = sp->cmd;
941 scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
942 dma_addr_t sle_dma;
943
944 /* Allocate additional continuation packets? */
945 if (avail_dsds == 0) {
946 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
947 QLA_DSDS_PER_IOCB : used_dsds;
948 dsd_list_len = (avail_dsds + 1) * 12;
949 used_dsds -= avail_dsds;
950
951 /* allocate tracking DS */
952 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
953 if (!dsd_ptr)
954 return 1;
955
956 /* allocate new list */
957 dsd_ptr->dsd_addr = next_dsd =
958 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
959 &dsd_ptr->dsd_list_dma);
960
961 if (!next_dsd) {
962 /*
963 * Need to cleanup only this dsd_ptr, rest
964 * will be done by sp_free_dma()
965 */
966 kfree(dsd_ptr);
967 return 1;
968 }
969
970 list_add_tail(&dsd_ptr->list,
971 &((struct crc_context *)sp->ctx)->dsd_list);
972
973 sp->flags |= SRB_CRC_CTX_DSD_VALID;
974
975 /* add new list to cmd iocb or last list */
976 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
977 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
978 *cur_dsd++ = dsd_list_len;
979 cur_dsd = (uint32_t *)next_dsd;
980 }
981 sle_dma = sg_dma_address(sg);
982 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
983 DEBUG18(printk(KERN_DEBUG
984 "%s(): %p, sg entry %d - addr =0x%x"
985 "0x%x, len =%d\n", __func__ , cur_dsd, i,
986 LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg)));
987 }
988 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
989 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
990 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
991
992 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
993 cp = page_address(sg_page(sg)) + sg->offset;
994 DEBUG18(printk("%s(): Protection Data buffer = %p:\n",
995 __func__ , cp));
996 }
997 avail_dsds--;
998 }
999 /* Null termination */
1000 *cur_dsd++ = 0;
1001 *cur_dsd++ = 0;
1002 *cur_dsd++ = 0;
1003 return 0;
1004}
1005
1006/**
1007 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1008 * Type 6 IOCB types.
1009 *
1010 * @sp: SRB command to process
1011 * @cmd_pkt: Command type 3 IOCB
1012 * @tot_dsds: Total number of segments to transfer
1013 */
1014static inline int
1015qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1016 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1017{
1018 uint32_t *cur_dsd, *fcp_dl;
1019 scsi_qla_host_t *vha;
1020 struct scsi_cmnd *cmd;
1021 struct scatterlist *cur_seg;
1022 int sgc;
1023 uint32_t total_bytes;
1024 uint32_t data_bytes;
1025 uint32_t dif_bytes;
1026 uint8_t bundling = 1;
1027 uint16_t blk_size;
1028 uint8_t *clr_ptr;
1029 struct crc_context *crc_ctx_pkt = NULL;
1030 struct qla_hw_data *ha;
1031 uint8_t additional_fcpcdb_len;
1032 uint16_t fcp_cmnd_len;
1033 struct fcp_cmnd *fcp_cmnd;
1034 dma_addr_t crc_ctx_dma;
1035
1036 cmd = sp->cmd;
1037
1038 sgc = 0;
1039 /* Update entry type to indicate Command Type CRC_2 IOCB */
1040 *((uint32_t *)(&cmd_pkt->entry_type)) =
1041 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1042
1043 /* No data transfer */
1044 data_bytes = scsi_bufflen(cmd);
1045 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1046 DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
1047 __func__, data_bytes));
1048 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1049 return QLA_SUCCESS;
1050 }
1051
1052 vha = sp->fcport->vha;
1053 ha = vha->hw;
1054
1055 DEBUG18(printk(KERN_DEBUG
1056 "%s(%ld): Executing cmd sp %p, pid=%ld, prot_op=%u.\n", __func__,
1057 vha->host_no, sp, cmd->serial_number, scsi_get_prot_op(sp->cmd)));
1058
1059 cmd_pkt->vp_index = sp->fcport->vp_idx;
1060
1061 /* Set transfer direction */
1062 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1063 cmd_pkt->control_flags =
1064 __constant_cpu_to_le16(CF_WRITE_DATA);
1065 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1066 cmd_pkt->control_flags =
1067 __constant_cpu_to_le16(CF_READ_DATA);
1068 }
1069
1070 tot_prot_dsds = scsi_prot_sg_count(cmd);
1071 if (!tot_prot_dsds)
1072 bundling = 0;
1073
1074 /* Allocate CRC context from global pool */
1075 crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
1076 GFP_ATOMIC, &crc_ctx_dma);
1077
1078 if (!crc_ctx_pkt)
1079 goto crc_queuing_error;
1080
1081 /* Zero out CTX area. */
1082 clr_ptr = (uint8_t *)crc_ctx_pkt;
1083 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1084
1085 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1086
1087 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1088
1089 /* Set handle */
1090 crc_ctx_pkt->handle = cmd_pkt->handle;
1091
1092 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1093
1094 qla24xx_set_t10dif_tags(cmd, (struct fw_dif_context *)
1095 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1096
1097 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1098 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1099 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1100
1101 /* Determine SCSI command length -- align to 4 byte boundary */
1102 if (cmd->cmd_len > 16) {
1103 DEBUG18(printk(KERN_INFO "%s(): **** SCSI CMD > 16\n",
1104 __func__));
1105 additional_fcpcdb_len = cmd->cmd_len - 16;
1106 if ((cmd->cmd_len % 4) != 0) {
1107 /* SCSI cmd > 16 bytes must be multiple of 4 */
1108 goto crc_queuing_error;
1109 }
1110 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1111 } else {
1112 additional_fcpcdb_len = 0;
1113 fcp_cmnd_len = 12 + 16 + 4;
1114 }
1115
1116 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1117
1118 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1119 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1120 fcp_cmnd->additional_cdb_len |= 1;
1121 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1122 fcp_cmnd->additional_cdb_len |= 2;
1123
1124 int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
1125 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1126 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1127 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1128 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1129 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1130 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1131 fcp_cmnd->task_attribute = 0;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02001132 fcp_cmnd->task_management = 0;
Arun Easibad75002010-05-04 15:01:30 -07001133
1134 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1135
1136 DEBUG18(printk(KERN_INFO "%s(%ld): Total SG(s) Entries %d, Data"
1137 "entries %d, data bytes %d, Protection entries %d\n",
1138 __func__, vha->host_no, tot_dsds, (tot_dsds-tot_prot_dsds),
1139 data_bytes, tot_prot_dsds));
1140
1141 /* Compute dif len and adjust data len to incude protection */
1142 total_bytes = data_bytes;
1143 dif_bytes = 0;
1144 blk_size = cmd->device->sector_size;
1145 if (scsi_get_prot_type(cmd) == SCSI_PROT_DIF_TYPE1) {
1146 dif_bytes = (data_bytes / blk_size) * 8;
1147 total_bytes += dif_bytes;
1148 }
1149
1150 if (!ql2xenablehba_err_chk)
1151 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1152
1153 if (!bundling) {
1154 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1155 } else {
1156 /*
1157 * Configure Bundling if we need to fetch interlaving
1158 * protection PCI accesses
1159 */
1160 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1161 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1162 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1163 tot_prot_dsds);
1164 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1165 }
1166
1167 /* Finish the common fields of CRC pkt */
1168 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1169 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1170 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1171 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1172 /* Fibre channel byte count */
1173 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1174 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1175 additional_fcpcdb_len);
1176 *fcp_dl = htonl(total_bytes);
1177
1178 DEBUG18(printk(KERN_INFO "%s(%ld): dif bytes = 0x%x (%d), total bytes"
1179 " = 0x%x (%d), dat block size =0x%x (%d)\n", __func__,
1180 vha->host_no, dif_bytes, dif_bytes, total_bytes, total_bytes,
1181 crc_ctx_pkt->blk_size, crc_ctx_pkt->blk_size));
1182
1183 /* Walks data segments */
1184
1185 cmd_pkt->control_flags |=
1186 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
1187 if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
1188 (tot_dsds - tot_prot_dsds)))
1189 goto crc_queuing_error;
1190
1191 if (bundling && tot_prot_dsds) {
1192 /* Walks dif segments */
1193 cur_seg = scsi_prot_sglist(cmd);
1194 cmd_pkt->control_flags |=
1195 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1196 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1197 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1198 tot_prot_dsds))
1199 goto crc_queuing_error;
1200 }
1201 return QLA_SUCCESS;
1202
1203crc_queuing_error:
1204 DEBUG18(qla_printk(KERN_INFO, ha,
1205 "CMD sent FAILED crc_q error:sp = %p\n", sp));
1206 /* Cleanup will be performed by the caller */
1207
1208 return QLA_FUNCTION_FAILED;
1209}
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001210
1211/**
1212 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1213 * @sp: command to send to the ISP
1214 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07001215 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001216 */
1217int
1218qla24xx_start_scsi(srb_t *sp)
1219{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001220 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001221 unsigned long flags;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001222 uint32_t *clr_ptr;
1223 uint32_t index;
1224 uint32_t handle;
1225 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001226 uint16_t cnt;
1227 uint16_t req_cnt;
1228 uint16_t tot_dsds;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001229 struct req_que *req = NULL;
1230 struct rsp_que *rsp = NULL;
1231 struct scsi_cmnd *cmd = sp->cmd;
Andrew Vasquez444786d2009-01-05 11:18:10 -08001232 struct scsi_qla_host *vha = sp->fcport->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001233 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001234
1235 /* Setup device pointers. */
1236 ret = 0;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001237
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001238 qla25xx_set_que(sp, &rsp);
1239 req = vha->req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001240
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001241 /* So we know we haven't pci_map'ed anything yet */
1242 tot_dsds = 0;
1243
1244 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001245 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001246 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
1247 != QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001248 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001249 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001250 }
1251
1252 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001253 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001254
1255 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001256 handle = req->current_outstanding_cmd;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001257 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1258 handle++;
1259 if (handle == MAX_OUTSTANDING_COMMANDS)
1260 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001261 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001262 break;
1263 }
1264 if (index == MAX_OUTSTANDING_COMMANDS)
1265 goto queuing_error;
1266
1267 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001268 if (scsi_sg_count(cmd)) {
1269 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1270 scsi_sg_count(cmd), cmd->sc_data_direction);
1271 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001272 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001273 } else
1274 nseg = 0;
1275
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001276 tot_dsds = nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001277
1278 req_cnt = qla24xx_calc_iocbs(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001279 if (req->cnt < (req_cnt + 2)) {
Andrew Vasquez08029992009-03-24 09:07:55 -07001280 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001281
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001282 if (req->ring_index < cnt)
1283 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001284 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001285 req->cnt = req->length -
1286 (req->ring_index - cnt);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001287 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001288 if (req->cnt < (req_cnt + 2))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001289 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001290
1291 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001292 req->current_outstanding_cmd = handle;
1293 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -07001294 sp->handle = handle;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001295 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001296 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001297
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001298 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001299 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001300
1301 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -05001302 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001303 clr_ptr = (uint32_t *)cmd_pkt + 2;
1304 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1305 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1306
1307 /* Set NPORT-ID and LUN number*/
1308 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1309 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1310 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1311 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001312 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001313
Andrew Vasquez661c3f62005-10-27 11:09:58 -07001314 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -08001315 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001316
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001317 /* Load SCSI command packet. */
1318 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1319 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1320
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001321 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001322
1323 /* Build IOCB segments */
1324 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1325
1326 /* Set total data segment count. */
1327 cmd_pkt->entry_count = (uint8_t)req_cnt;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001328 /* Specify response queue number where completion should happen */
1329 cmd_pkt->entry_status = (uint8_t) rsp->id;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001330 wmb();
1331
1332 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001333 req->ring_index++;
1334 if (req->ring_index == req->length) {
1335 req->ring_index = 0;
1336 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001337 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001338 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001339
1340 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001341
1342 /* Set chip new ring index. */
Andrew Vasquez08029992009-03-24 09:07:55 -07001343 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1344 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001345
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001346 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001347 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001348 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001349 qla24xx_process_response_queue(vha, rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001350
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001351 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001352 return QLA_SUCCESS;
1353
1354queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001355 if (tot_dsds)
1356 scsi_dma_unmap(cmd);
1357
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001358 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001359
1360 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001362
Arun Easibad75002010-05-04 15:01:30 -07001363
1364/**
1365 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1366 * @sp: command to send to the ISP
1367 *
1368 * Returns non-zero if a failure occurred, else zero.
1369 */
1370int
1371qla24xx_dif_start_scsi(srb_t *sp)
1372{
1373 int nseg;
1374 unsigned long flags;
1375 uint32_t *clr_ptr;
1376 uint32_t index;
1377 uint32_t handle;
1378 uint16_t cnt;
1379 uint16_t req_cnt = 0;
1380 uint16_t tot_dsds;
1381 uint16_t tot_prot_dsds;
1382 uint16_t fw_prot_opts = 0;
1383 struct req_que *req = NULL;
1384 struct rsp_que *rsp = NULL;
1385 struct scsi_cmnd *cmd = sp->cmd;
1386 struct scsi_qla_host *vha = sp->fcport->vha;
1387 struct qla_hw_data *ha = vha->hw;
1388 struct cmd_type_crc_2 *cmd_pkt;
1389 uint32_t status = 0;
1390
1391#define QDSS_GOT_Q_SPACE BIT_0
1392
1393 /* Only process protection in this routine */
1394 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL)
1395 return qla24xx_start_scsi(sp);
1396
1397 /* Setup device pointers. */
1398
1399 qla25xx_set_que(sp, &rsp);
1400 req = vha->req;
1401
1402 /* So we know we haven't pci_map'ed anything yet */
1403 tot_dsds = 0;
1404
1405 /* Send marker if required */
1406 if (vha->marker_needed != 0) {
1407 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1408 QLA_SUCCESS)
1409 return QLA_FUNCTION_FAILED;
1410 vha->marker_needed = 0;
1411 }
1412
1413 /* Acquire ring specific lock */
1414 spin_lock_irqsave(&ha->hardware_lock, flags);
1415
1416 /* Check for room in outstanding command list. */
1417 handle = req->current_outstanding_cmd;
1418 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1419 handle++;
1420 if (handle == MAX_OUTSTANDING_COMMANDS)
1421 handle = 1;
1422 if (!req->outstanding_cmds[handle])
1423 break;
1424 }
1425
1426 if (index == MAX_OUTSTANDING_COMMANDS)
1427 goto queuing_error;
1428
1429 /* Compute number of required data segments */
1430 /* Map the sg table so we have an accurate count of sg entries needed */
1431 if (scsi_sg_count(cmd)) {
1432 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1433 scsi_sg_count(cmd), cmd->sc_data_direction);
1434 if (unlikely(!nseg))
1435 goto queuing_error;
1436 else
1437 sp->flags |= SRB_DMA_VALID;
1438 } else
1439 nseg = 0;
1440
1441 /* number of required data segments */
1442 tot_dsds = nseg;
1443
1444 /* Compute number of required protection segments */
1445 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1446 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1447 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1448 if (unlikely(!nseg))
1449 goto queuing_error;
1450 else
1451 sp->flags |= SRB_CRC_PROT_DMA_VALID;
1452 } else {
1453 nseg = 0;
1454 }
1455
1456 req_cnt = 1;
1457 /* Total Data and protection sg segment(s) */
1458 tot_prot_dsds = nseg;
1459 tot_dsds += nseg;
1460 if (req->cnt < (req_cnt + 2)) {
1461 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
1462
1463 if (req->ring_index < cnt)
1464 req->cnt = cnt - req->ring_index;
1465 else
1466 req->cnt = req->length -
1467 (req->ring_index - cnt);
1468 }
1469
1470 if (req->cnt < (req_cnt + 2))
1471 goto queuing_error;
1472
1473 status |= QDSS_GOT_Q_SPACE;
1474
1475 /* Build header part of command packet (excluding the OPCODE). */
1476 req->current_outstanding_cmd = handle;
1477 req->outstanding_cmds[handle] = sp;
1478 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1479 req->cnt -= req_cnt;
1480
1481 /* Fill-in common area */
1482 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1483 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1484
1485 clr_ptr = (uint32_t *)cmd_pkt + 2;
1486 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1487
1488 /* Set NPORT-ID and LUN number*/
1489 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1490 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1491 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1492 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1493
1494 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
1495 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1496
1497 /* Total Data and protection segment(s) */
1498 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1499
1500 /* Build IOCB segments and adjust for data protection segments */
1501 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1502 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1503 QLA_SUCCESS)
1504 goto queuing_error;
1505
1506 cmd_pkt->entry_count = (uint8_t)req_cnt;
1507 /* Specify response queue number where completion should happen */
1508 cmd_pkt->entry_status = (uint8_t) rsp->id;
1509 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1510 wmb();
1511
1512 /* Adjust ring index. */
1513 req->ring_index++;
1514 if (req->ring_index == req->length) {
1515 req->ring_index = 0;
1516 req->ring_ptr = req->ring;
1517 } else
1518 req->ring_ptr++;
1519
1520 /* Set chip new ring index. */
1521 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1522 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1523
1524 /* Manage unprocessed RIO/ZIO commands in response queue. */
1525 if (vha->flags.process_response_queue &&
1526 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1527 qla24xx_process_response_queue(vha, rsp);
1528
1529 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1530
1531 return QLA_SUCCESS;
1532
1533queuing_error:
1534 if (status & QDSS_GOT_Q_SPACE) {
1535 req->outstanding_cmds[handle] = NULL;
1536 req->cnt += req_cnt;
1537 }
1538 /* Cleanup will be performed by the caller (queuecommand) */
1539
1540 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1541
1542 DEBUG18(qla_printk(KERN_INFO, ha,
1543 "CMD sent FAILED SCSI prot_op:%02x\n", scsi_get_prot_op(cmd)));
1544 return QLA_FUNCTION_FAILED;
1545}
1546
1547
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001548static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001549{
1550 struct scsi_cmnd *cmd = sp->cmd;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001551 struct qla_hw_data *ha = sp->fcport->vha->hw;
1552 int affinity = cmd->request->cpu;
1553
Anirban Chakraborty7163ea82009-08-05 09:18:40 -07001554 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001555 affinity < ha->max_rsp_queues - 1)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001556 *rsp = ha->rsp_q_map[affinity + 1];
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001557 else
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001558 *rsp = ha->rsp_q_map[0];
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001559}
Andrew Vasquezac280b62009-08-20 11:06:05 -07001560
1561/* Generic Control-SRB manipulation functions. */
1562
1563static void *
1564qla2x00_alloc_iocbs(srb_t *sp)
1565{
1566 scsi_qla_host_t *vha = sp->fcport->vha;
1567 struct qla_hw_data *ha = vha->hw;
1568 struct req_que *req = ha->req_q_map[0];
1569 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1570 uint32_t index, handle;
1571 request_t *pkt;
1572 uint16_t cnt, req_cnt;
1573
1574 pkt = NULL;
1575 req_cnt = 1;
1576
1577 /* Check for room in outstanding command list. */
1578 handle = req->current_outstanding_cmd;
1579 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1580 handle++;
1581 if (handle == MAX_OUTSTANDING_COMMANDS)
1582 handle = 1;
1583 if (!req->outstanding_cmds[handle])
1584 break;
1585 }
1586 if (index == MAX_OUTSTANDING_COMMANDS)
1587 goto queuing_error;
1588
1589 /* Check for room on request queue. */
1590 if (req->cnt < req_cnt) {
1591 if (ha->mqenable)
1592 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
1593 else if (IS_FWI2_CAPABLE(ha))
1594 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
1595 else
1596 cnt = qla2x00_debounce_register(
1597 ISP_REQ_Q_OUT(ha, &reg->isp));
1598
1599 if (req->ring_index < cnt)
1600 req->cnt = cnt - req->ring_index;
1601 else
1602 req->cnt = req->length -
1603 (req->ring_index - cnt);
1604 }
1605 if (req->cnt < req_cnt)
1606 goto queuing_error;
1607
1608 /* Prep packet */
1609 req->current_outstanding_cmd = handle;
1610 req->outstanding_cmds[handle] = sp;
1611 req->cnt -= req_cnt;
1612
1613 pkt = req->ring_ptr;
1614 memset(pkt, 0, REQUEST_ENTRY_SIZE);
1615 pkt->entry_count = req_cnt;
1616 pkt->handle = handle;
1617 sp->handle = handle;
1618
1619queuing_error:
1620 return pkt;
1621}
1622
1623static void
1624qla2x00_start_iocbs(srb_t *sp)
1625{
1626 struct qla_hw_data *ha = sp->fcport->vha->hw;
1627 struct req_que *req = ha->req_q_map[0];
1628 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1629 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
1630
Giridhar Malavalia9083012010-04-12 17:59:55 -07001631 if (IS_QLA82XX(ha)) {
1632 qla82xx_start_iocbs(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001633 } else {
Giridhar Malavalia9083012010-04-12 17:59:55 -07001634 /* Adjust ring index. */
1635 req->ring_index++;
1636 if (req->ring_index == req->length) {
1637 req->ring_index = 0;
1638 req->ring_ptr = req->ring;
1639 } else
1640 req->ring_ptr++;
1641
1642 /* Set chip new ring index. */
1643 if (ha->mqenable) {
1644 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
1645 RD_REG_DWORD(&ioreg->hccr);
1646 } else if (IS_QLA82XX(ha)) {
1647 qla82xx_start_iocbs(sp);
1648 } else if (IS_FWI2_CAPABLE(ha)) {
1649 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
1650 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
1651 } else {
1652 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
1653 req->ring_index);
1654 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
1655 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001656 }
1657}
1658
1659static void
1660qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1661{
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001662 struct srb_ctx *ctx = sp->ctx;
1663 struct srb_iocb *lio = ctx->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001664
1665 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1666 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001667 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001668 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001669 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001670 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1671 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1672 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1673 logio->port_id[1] = sp->fcport->d_id.b.area;
1674 logio->port_id[2] = sp->fcport->d_id.b.domain;
1675 logio->vp_index = sp->fcport->vp_idx;
1676}
1677
1678static void
1679qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1680{
1681 struct qla_hw_data *ha = sp->fcport->vha->hw;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001682 struct srb_ctx *ctx = sp->ctx;
1683 struct srb_iocb *lio = ctx->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001684 uint16_t opts;
1685
1686 mbx->entry_type = MBX_IOCB_TYPE;;
1687 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1688 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001689 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1690 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001691 if (HAS_EXTENDED_IDS(ha)) {
1692 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1693 mbx->mb10 = cpu_to_le16(opts);
1694 } else {
1695 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1696 }
1697 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1698 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1699 sp->fcport->d_id.b.al_pa);
1700 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1701}
1702
1703static void
1704qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1705{
1706 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1707 logio->control_flags =
1708 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1709 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1710 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1711 logio->port_id[1] = sp->fcport->d_id.b.area;
1712 logio->port_id[2] = sp->fcport->d_id.b.domain;
1713 logio->vp_index = sp->fcport->vp_idx;
1714}
1715
1716static void
1717qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1718{
1719 struct qla_hw_data *ha = sp->fcport->vha->hw;
1720
1721 mbx->entry_type = MBX_IOCB_TYPE;;
1722 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1723 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1724 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1725 cpu_to_le16(sp->fcport->loop_id):
1726 cpu_to_le16(sp->fcport->loop_id << 8);
1727 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1728 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1729 sp->fcport->d_id.b.al_pa);
1730 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1731 /* Implicit: mbx->mbx10 = 0. */
1732}
1733
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001734static void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001735qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1736{
1737 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1738 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1739 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1740 logio->vp_index = sp->fcport->vp_idx;
1741}
1742
1743static void
1744qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1745{
1746 struct qla_hw_data *ha = sp->fcport->vha->hw;
1747
1748 mbx->entry_type = MBX_IOCB_TYPE;
1749 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1750 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1751 if (HAS_EXTENDED_IDS(ha)) {
1752 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1753 mbx->mb10 = cpu_to_le16(BIT_0);
1754 } else {
1755 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1756 }
1757 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1758 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1759 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1760 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
1761 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1762}
1763
1764static void
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001765qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
1766{
1767 uint32_t flags;
1768 unsigned int lun;
1769 struct fc_port *fcport = sp->fcport;
1770 scsi_qla_host_t *vha = fcport->vha;
1771 struct qla_hw_data *ha = vha->hw;
1772 struct srb_ctx *ctx = sp->ctx;
1773 struct srb_iocb *iocb = ctx->u.iocb_cmd;
1774 struct req_que *req = vha->req;
1775
1776 flags = iocb->u.tmf.flags;
1777 lun = iocb->u.tmf.lun;
1778
1779 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
1780 tsk->entry_count = 1;
1781 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
1782 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
1783 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
1784 tsk->control_flags = cpu_to_le32(flags);
1785 tsk->port_id[0] = fcport->d_id.b.al_pa;
1786 tsk->port_id[1] = fcport->d_id.b.area;
1787 tsk->port_id[2] = fcport->d_id.b.domain;
1788 tsk->vp_index = fcport->vp_idx;
1789
1790 if (flags == TCF_LUN_RESET) {
1791 int_to_scsilun(lun, &tsk->lun);
1792 host_to_fcp_swap((uint8_t *)&tsk->lun,
1793 sizeof(tsk->lun));
1794 }
1795}
1796
1797static void
1798qla24xx_marker_iocb(srb_t *sp, struct mrk_entry_24xx *mrk)
1799{
1800 uint16_t lun;
1801 uint8_t modif;
1802 struct fc_port *fcport = sp->fcport;
1803 scsi_qla_host_t *vha = fcport->vha;
1804 struct srb_ctx *ctx = sp->ctx;
1805 struct srb_iocb *iocb = ctx->u.iocb_cmd;
1806 struct req_que *req = vha->req;
1807
1808 lun = iocb->u.marker.lun;
1809 modif = iocb->u.marker.modif;
1810 mrk->entry_type = MARKER_TYPE;
1811 mrk->modifier = modif;
1812 if (modif != MK_SYNC_ALL) {
1813 mrk->nport_handle = cpu_to_le16(fcport->loop_id);
1814 mrk->lun[1] = LSB(lun);
1815 mrk->lun[2] = MSB(lun);
1816 host_to_fcp_swap(mrk->lun, sizeof(mrk->lun));
1817 mrk->vp_index = vha->vp_idx;
1818 mrk->handle = MAKE_HANDLE(req->id, mrk->handle);
1819 }
1820}
1821
1822static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001823qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
1824{
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001825 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001826
1827 els_iocb->entry_type = ELS_IOCB_TYPE;
1828 els_iocb->entry_count = 1;
1829 els_iocb->sys_define = 0;
1830 els_iocb->entry_status = 0;
1831 els_iocb->handle = sp->handle;
1832 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1833 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
1834 els_iocb->vp_index = sp->fcport->vp_idx;
1835 els_iocb->sof_type = EST_SOFI3;
1836 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
1837
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001838 els_iocb->opcode =
1839 (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
1840 bsg_job->request->rqst_data.r_els.els_code :
1841 bsg_job->request->rqst_data.h_els.command_code;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001842 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
1843 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
1844 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
1845 els_iocb->control_flags = 0;
1846 els_iocb->rx_byte_count =
1847 cpu_to_le32(bsg_job->reply_payload.payload_len);
1848 els_iocb->tx_byte_count =
1849 cpu_to_le32(bsg_job->request_payload.payload_len);
1850
1851 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
1852 (bsg_job->request_payload.sg_list)));
1853 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
1854 (bsg_job->request_payload.sg_list)));
1855 els_iocb->tx_len = cpu_to_le32(sg_dma_len
1856 (bsg_job->request_payload.sg_list));
1857
1858 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
1859 (bsg_job->reply_payload.sg_list)));
1860 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
1861 (bsg_job->reply_payload.sg_list)));
1862 els_iocb->rx_len = cpu_to_le32(sg_dma_len
1863 (bsg_job->reply_payload.sg_list));
1864}
1865
1866static void
1867qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
1868{
1869 uint16_t avail_dsds;
1870 uint32_t *cur_dsd;
1871 struct scatterlist *sg;
1872 int index;
1873 uint16_t tot_dsds;
1874 scsi_qla_host_t *vha = sp->fcport->vha;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001875 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001876 int loop_iterartion = 0;
1877 int cont_iocb_prsnt = 0;
1878 int entry_count = 1;
1879
1880 ct_iocb->entry_type = CT_IOCB_TYPE;
1881 ct_iocb->entry_status = 0;
1882 ct_iocb->sys_define = 0;
1883 ct_iocb->handle = sp->handle;
1884
1885 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1886 ct_iocb->vp_index = sp->fcport->vp_idx;
1887 ct_iocb->comp_status = __constant_cpu_to_le16(0);
1888
1889 ct_iocb->cmd_dsd_count =
1890 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
1891 ct_iocb->timeout = 0;
1892 ct_iocb->rsp_dsd_count =
1893 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
1894 ct_iocb->rsp_byte_count =
1895 cpu_to_le32(bsg_job->reply_payload.payload_len);
1896 ct_iocb->cmd_byte_count =
1897 cpu_to_le32(bsg_job->request_payload.payload_len);
1898 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
1899 (bsg_job->request_payload.sg_list)));
1900 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
1901 (bsg_job->request_payload.sg_list)));
1902 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
1903 (bsg_job->request_payload.sg_list));
1904
1905 avail_dsds = 1;
1906 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
1907 index = 0;
1908 tot_dsds = bsg_job->reply_payload.sg_cnt;
1909
1910 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
1911 dma_addr_t sle_dma;
1912 cont_a64_entry_t *cont_pkt;
1913
1914 /* Allocate additional continuation packets? */
1915 if (avail_dsds == 0) {
1916 /*
1917 * Five DSDs are available in the Cont.
1918 * Type 1 IOCB.
1919 */
1920 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
1921 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
1922 avail_dsds = 5;
1923 cont_iocb_prsnt = 1;
1924 entry_count++;
1925 }
1926
1927 sle_dma = sg_dma_address(sg);
1928 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1929 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1930 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1931 loop_iterartion++;
1932 avail_dsds--;
1933 }
1934 ct_iocb->entry_count = entry_count;
1935}
1936
Andrew Vasquezac280b62009-08-20 11:06:05 -07001937int
1938qla2x00_start_sp(srb_t *sp)
1939{
1940 int rval;
1941 struct qla_hw_data *ha = sp->fcport->vha->hw;
1942 void *pkt;
1943 struct srb_ctx *ctx = sp->ctx;
1944 unsigned long flags;
1945
1946 rval = QLA_FUNCTION_FAILED;
1947 spin_lock_irqsave(&ha->hardware_lock, flags);
1948 pkt = qla2x00_alloc_iocbs(sp);
1949 if (!pkt)
1950 goto done;
1951
1952 rval = QLA_SUCCESS;
1953 switch (ctx->type) {
1954 case SRB_LOGIN_CMD:
1955 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001956 qla24xx_login_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07001957 qla2x00_login_iocb(sp, pkt);
1958 break;
1959 case SRB_LOGOUT_CMD:
1960 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001961 qla24xx_logout_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07001962 qla2x00_logout_iocb(sp, pkt);
1963 break;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001964 case SRB_ELS_CMD_RPT:
1965 case SRB_ELS_CMD_HST:
1966 qla24xx_els_iocb(sp, pkt);
1967 break;
1968 case SRB_CT_CMD:
1969 qla24xx_ct_iocb(sp, pkt);
1970 break;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001971 case SRB_ADISC_CMD:
1972 IS_FWI2_CAPABLE(ha) ?
1973 qla24xx_adisc_iocb(sp, pkt) :
1974 qla2x00_adisc_iocb(sp, pkt);
1975 break;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001976 case SRB_TM_CMD:
1977 qla24xx_tm_iocb(sp, pkt);
1978 break;
1979 case SRB_MARKER_CMD:
1980 qla24xx_marker_iocb(sp, pkt);
1981 break;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001982 default:
1983 break;
1984 }
1985
1986 wmb();
1987 qla2x00_start_iocbs(sp);
1988done:
1989 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1990 return rval;
1991}