blob: 4c1ba6263eb32c1a228f2044b314c4e902f1c677 [file] [log] [blame]
Andrew Vasquezfa90c542005-10-27 11:10:08 -07001/*
2 * QLogic Fibre Channel HBA Driver
Giridhar Malavalide7c5d02010-07-23 15:28:36 +05003 * Copyright (c) 2003-2010 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 void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -070016static void qla25xx_set_que(srb_t *, struct rsp_que **);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017/**
18 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
19 * @cmd: SCSI command
20 *
21 * Returns the proper CF_* direction based on CDB.
22 */
23static inline uint16_t
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070024qla2x00_get_cmd_direction(srb_t *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 uint16_t cflags;
27
28 cflags = 0;
29
30 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070031 if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 cflags = CF_WRITE;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080033 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070034 scsi_bufflen(sp->cmd);
35 } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 cflags = CF_READ;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080037 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070038 scsi_bufflen(sp->cmd);
39 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return (cflags);
41}
42
43/**
44 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
45 * Continuation Type 0 IOCBs to allocate.
46 *
47 * @dsds: number of data segment decriptors needed
48 *
49 * Returns the number of IOCB entries needed to store @dsds.
50 */
51uint16_t
52qla2x00_calc_iocbs_32(uint16_t dsds)
53{
54 uint16_t iocbs;
55
56 iocbs = 1;
57 if (dsds > 3) {
58 iocbs += (dsds - 3) / 7;
59 if ((dsds - 3) % 7)
60 iocbs++;
61 }
62 return (iocbs);
63}
64
65/**
66 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
67 * Continuation Type 1 IOCBs to allocate.
68 *
69 * @dsds: number of data segment decriptors needed
70 *
71 * Returns the number of IOCB entries needed to store @dsds.
72 */
73uint16_t
74qla2x00_calc_iocbs_64(uint16_t dsds)
75{
76 uint16_t iocbs;
77
78 iocbs = 1;
79 if (dsds > 2) {
80 iocbs += (dsds - 2) / 5;
81 if ((dsds - 2) % 5)
82 iocbs++;
83 }
84 return (iocbs);
85}
86
87/**
88 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
89 * @ha: HA context
90 *
91 * Returns a pointer to the Continuation Type 0 IOCB packet.
92 */
93static inline cont_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070094qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 cont_entry_t *cont_pkt;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070097 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080099 req->ring_index++;
100 if (req->ring_index == req->length) {
101 req->ring_index = 0;
102 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800104 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800107 cont_pkt = (cont_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* Load packet defaults. */
110 *((uint32_t *)(&cont_pkt->entry_type)) =
111 __constant_cpu_to_le32(CONTINUE_TYPE);
112
113 return (cont_pkt);
114}
115
116/**
117 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
118 * @ha: HA context
119 *
120 * Returns a pointer to the continuation type 1 IOCB packet.
121 */
122static inline cont_a64_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700123qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 cont_a64_entry_t *cont_pkt;
126
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700127 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800129 req->ring_index++;
130 if (req->ring_index == req->length) {
131 req->ring_index = 0;
132 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800134 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800137 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* Load packet defaults. */
140 *((uint32_t *)(&cont_pkt->entry_type)) =
141 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
142
143 return (cont_pkt);
144}
145
Arun Easibad75002010-05-04 15:01:30 -0700146static inline int
147qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
148{
149 uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
150
151 /* We only support T10 DIF right now */
152 if (guard != SHOST_DIX_GUARD_CRC) {
153 DEBUG2(printk(KERN_ERR "Unsupported guard: %d\n", guard));
154 return 0;
155 }
156
157 /* We always use DIFF Bundling for best performance */
158 *fw_prot_opts = 0;
159
160 /* Translate SCSI opcode to a protection opcode */
161 switch (scsi_get_prot_op(sp->cmd)) {
162 case SCSI_PROT_READ_STRIP:
163 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
164 break;
165 case SCSI_PROT_WRITE_INSERT:
166 *fw_prot_opts |= PO_MODE_DIF_INSERT;
167 break;
168 case SCSI_PROT_READ_INSERT:
169 *fw_prot_opts |= PO_MODE_DIF_INSERT;
170 break;
171 case SCSI_PROT_WRITE_STRIP:
172 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
173 break;
174 case SCSI_PROT_READ_PASS:
175 *fw_prot_opts |= PO_MODE_DIF_PASS;
176 break;
177 case SCSI_PROT_WRITE_PASS:
178 *fw_prot_opts |= PO_MODE_DIF_PASS;
179 break;
180 default: /* Normal Request */
181 *fw_prot_opts |= PO_MODE_DIF_PASS;
182 break;
183 }
184
185 return scsi_prot_sg_count(sp->cmd);
186}
187
188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
190 * capable IOCB types.
191 *
192 * @sp: SRB command to process
193 * @cmd_pkt: Command type 2 IOCB
194 * @tot_dsds: Total number of segments to transfer
195 */
196void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
197 uint16_t tot_dsds)
198{
199 uint16_t avail_dsds;
200 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800201 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900203 struct scatterlist *sg;
204 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 cmd = sp->cmd;
207
208 /* Update entry type to indicate Command Type 2 IOCB */
209 *((uint32_t *)(&cmd_pkt->entry_type)) =
210 __constant_cpu_to_le32(COMMAND_TYPE);
211
212 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900213 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
215 return;
216 }
217
Andrew Vasquez444786d2009-01-05 11:18:10 -0800218 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700219 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* Three DSDs are available in the Command Type 2 IOCB */
222 avail_dsds = 3;
223 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
224
225 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900226 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
227 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900229 /* Allocate additional continuation packets? */
230 if (avail_dsds == 0) {
231 /*
232 * Seven DSDs are available in the Continuation
233 * Type 0 IOCB.
234 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700235 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900236 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
237 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900239
240 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
241 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
242 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244}
245
246/**
247 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
248 * capable IOCB types.
249 *
250 * @sp: SRB command to process
251 * @cmd_pkt: Command type 3 IOCB
252 * @tot_dsds: Total number of segments to transfer
253 */
254void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
255 uint16_t tot_dsds)
256{
257 uint16_t avail_dsds;
258 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800259 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900261 struct scatterlist *sg;
262 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 cmd = sp->cmd;
265
266 /* Update entry type to indicate Command Type 3 IOCB */
267 *((uint32_t *)(&cmd_pkt->entry_type)) =
268 __constant_cpu_to_le32(COMMAND_A64_TYPE);
269
270 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900271 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
273 return;
274 }
275
Andrew Vasquez444786d2009-01-05 11:18:10 -0800276 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700277 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* Two DSDs are available in the Command Type 3 IOCB */
280 avail_dsds = 2;
281 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
282
283 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900284 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
285 dma_addr_t sle_dma;
286 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900288 /* Allocate additional continuation packets? */
289 if (avail_dsds == 0) {
290 /*
291 * Five DSDs are available in the Continuation
292 * Type 1 IOCB.
293 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700294 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900295 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
296 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900298
299 sle_dma = sg_dma_address(sg);
300 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
301 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
302 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
303 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305}
306
307/**
308 * qla2x00_start_scsi() - Send a SCSI command to the ISP
309 * @sp: command to send to the ISP
310 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700311 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313int
314qla2x00_start_scsi(srb_t *sp)
315{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900316 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800318 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct scsi_cmnd *cmd;
320 uint32_t *clr_ptr;
321 uint32_t index;
322 uint32_t handle;
323 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 uint16_t cnt;
325 uint16_t req_cnt;
326 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700327 struct device_reg_2xxx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800328 struct qla_hw_data *ha;
329 struct req_que *req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800330 struct rsp_que *rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Setup device pointers. */
333 ret = 0;
Andrew Vasquez444786d2009-01-05 11:18:10 -0800334 vha = sp->fcport->vha;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800335 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700336 reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 cmd = sp->cmd;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800338 req = ha->req_q_map[0];
339 rsp = ha->rsp_q_map[0];
83021922005-04-17 15:10:41 -0500340 /* So we know we haven't pci_map'ed anything yet */
341 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800344 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800345 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
346 != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return (QLA_FUNCTION_FAILED);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800348 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700352 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800355 handle = req->current_outstanding_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
357 handle++;
358 if (handle == MAX_OUTSTANDING_COMMANDS)
359 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800360 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 break;
362 }
363 if (index == MAX_OUTSTANDING_COMMANDS)
364 goto queuing_error;
365
83021922005-04-17 15:10:41 -0500366 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700367 if (scsi_sg_count(cmd)) {
368 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
369 scsi_sg_count(cmd), cmd->sc_data_direction);
370 if (unlikely(!nseg))
371 goto queuing_error;
372 } else
373 nseg = 0;
374
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900375 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700378 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800379 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800381 if (req->ring_index < cnt)
382 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800384 req->cnt = req->length -
385 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800387 if (req->cnt < (req_cnt + 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 goto queuing_error;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800391 req->current_outstanding_cmd = handle;
392 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -0700393 sp->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800395 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800397 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 cmd_pkt->handle = handle;
399 /* Zero out remaining portion of packet. */
400 clr_ptr = (uint32_t *)cmd_pkt + 2;
401 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
402 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
403
bdf79622005-04-17 15:06:53 -0500404 /* Set target ID and LUN number*/
405 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
406 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* Update tagged queuing modifier */
409 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* Load SCSI command packet. */
412 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900413 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700416 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* Set total data segment count. */
419 cmd_pkt->entry_count = (uint8_t)req_cnt;
420 wmb();
421
422 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800423 req->ring_index++;
424 if (req->ring_index == req->length) {
425 req->ring_index = 0;
426 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800428 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800433 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
435
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700436 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800437 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800438 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
439 qla2x00_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700440
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700441 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return (QLA_SUCCESS);
443
444queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900445 if (tot_dsds)
446 scsi_dma_unmap(cmd);
447
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700448 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 return (QLA_FUNCTION_FAILED);
451}
452
453/**
454 * qla2x00_marker() - Send a marker IOCB to the firmware.
455 * @ha: HA context
456 * @loop_id: loop ID
457 * @lun: LUN
458 * @type: marker modifier
459 *
460 * Can be called from both normal and interrupt context.
461 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700462 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
Andrew Vasquez3dbe7562010-07-23 15:28:37 +0500464static int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800465__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
466 struct rsp_que *rsp, uint16_t loop_id,
467 uint16_t lun, uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700469 mrk_entry_t *mrk;
470 struct mrk_entry_24xx *mrk24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800471 struct qla_hw_data *ha = vha->hw;
472 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700474 mrk24 = NULL;
Giridhar Malavalid94d10e2010-07-23 15:28:23 +0500475 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700476 if (mrk == NULL) {
477 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800478 __func__, base_vha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 return (QLA_FUNCTION_FAILED);
481 }
482
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700483 mrk->entry_type = MARKER_TYPE;
484 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700486 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700487 mrk24 = (struct mrk_entry_24xx *) mrk;
488 mrk24->nport_handle = cpu_to_le16(loop_id);
489 mrk24->lun[1] = LSB(lun);
490 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700491 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800492 mrk24->vp_index = vha->vp_idx;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700493 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700494 } else {
495 SET_TARGET_ID(ha, mrk->target, loop_id);
496 mrk->lun = cpu_to_le16(lun);
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 wmb();
500
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800501 qla2x00_isp_cmd(vha, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 return (QLA_SUCCESS);
504}
505
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700506int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800507qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
508 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
509 uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 int ret;
512 unsigned long flags = 0;
513
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800514 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
515 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
516 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 return (ret);
519}
520
521/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * qla2x00_isp_cmd() - Modify the request ring pointer.
523 * @ha: HA context
524 *
525 * Note: The caller must hold the hardware lock before calling this routine.
526 */
Adrian Bunk413975a2006-06-30 02:33:06 -0700527static void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800528qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800530 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800531 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800532 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 DEBUG5(printk("%s(): IOCB data:\n", __func__));
535 DEBUG5(qla2x00_dump_buffer(
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800536 (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800539 req->ring_index++;
540 if (req->ring_index == req->length) {
541 req->ring_index = 0;
542 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800544 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /* Set chip new ring index. */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700547 if (IS_QLA82XX(ha)) {
548 uint32_t dbval = 0x04 | (ha->portnum << 5);
549
550 /* write, read and verify logic */
551 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
552 if (ql2xdbwr)
553 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
554 else {
555 WRT_REG_DWORD(
556 (unsigned long __iomem *)ha->nxdb_wr_ptr,
557 dbval);
558 wmb();
559 while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
560 WRT_REG_DWORD((unsigned long __iomem *)
561 ha->nxdb_wr_ptr, dbval);
562 wmb();
563 }
564 }
565 } else if (ha->mqenable) {
566 /* Set chip new ring index. */
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800567 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
568 RD_REG_DWORD(&ioreg->hccr);
Giridhar Malavalia9083012010-04-12 17:59:55 -0700569 } else {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800570 if (IS_FWI2_CAPABLE(ha)) {
571 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
572 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
573 } else {
574 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
575 req->ring_index);
576 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
577 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700578 }
579
580}
581
582/**
583 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
584 * Continuation Type 1 IOCBs to allocate.
585 *
586 * @dsds: number of data segment decriptors needed
587 *
588 * Returns the number of IOCB entries needed to store @dsds.
589 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700590inline uint16_t
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700591qla24xx_calc_iocbs(uint16_t dsds)
592{
593 uint16_t iocbs;
594
595 iocbs = 1;
596 if (dsds > 1) {
597 iocbs += (dsds - 1) / 5;
598 if ((dsds - 1) % 5)
599 iocbs++;
600 }
Arun Easibad75002010-05-04 15:01:30 -0700601 DEBUG3(printk(KERN_DEBUG "%s(): Required PKT(s) = %d\n",
602 __func__, iocbs));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700603 return iocbs;
604}
605
606/**
607 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
608 * IOCB types.
609 *
610 * @sp: SRB command to process
611 * @cmd_pkt: Command type 3 IOCB
612 * @tot_dsds: Total number of segments to transfer
613 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700614inline void
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700615qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
616 uint16_t tot_dsds)
617{
618 uint16_t avail_dsds;
619 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800620 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700621 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900622 struct scatterlist *sg;
623 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800624 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700625
626 cmd = sp->cmd;
627
628 /* Update entry type to indicate Command Type 3 IOCB */
629 *((uint32_t *)(&cmd_pkt->entry_type)) =
630 __constant_cpu_to_le32(COMMAND_TYPE_7);
631
632 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900633 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700634 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
635 return;
636 }
637
Andrew Vasquez444786d2009-01-05 11:18:10 -0800638 vha = sp->fcport->vha;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700639 req = vha->req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700640
641 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700642 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700643 cmd_pkt->task_mgmt_flags =
644 __constant_cpu_to_le16(TMF_WRITE_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800645 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700646 scsi_bufflen(sp->cmd);
647 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700648 cmd_pkt->task_mgmt_flags =
649 __constant_cpu_to_le16(TMF_READ_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800650 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700651 scsi_bufflen(sp->cmd);
652 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700653
654 /* One DSD is available in the Command Type 3 IOCB */
655 avail_dsds = 1;
656 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
657
658 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700659
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900660 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
661 dma_addr_t sle_dma;
662 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700663
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900664 /* Allocate additional continuation packets? */
665 if (avail_dsds == 0) {
666 /*
667 * Five DSDs are available in the Continuation
668 * Type 1 IOCB.
669 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700670 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900671 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
672 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700673 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900674
675 sle_dma = sg_dma_address(sg);
676 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
677 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
678 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
679 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700680 }
681}
682
Arun Easibad75002010-05-04 15:01:30 -0700683struct fw_dif_context {
684 uint32_t ref_tag;
685 uint16_t app_tag;
686 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
687 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
688};
689
690/*
691 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
692 *
693 */
694static inline void
695qla24xx_set_t10dif_tags(struct scsi_cmnd *cmd, struct fw_dif_context *pkt,
696 unsigned int protcnt)
697{
698 struct sd_dif_tuple *spt;
699 unsigned char op = scsi_get_prot_op(cmd);
700
701 switch (scsi_get_prot_type(cmd)) {
702 /* For TYPE 0 protection: no checking */
703 case SCSI_PROT_DIF_TYPE0:
704 pkt->ref_tag_mask[0] = 0x00;
705 pkt->ref_tag_mask[1] = 0x00;
706 pkt->ref_tag_mask[2] = 0x00;
707 pkt->ref_tag_mask[3] = 0x00;
708 break;
709
710 /*
711 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
712 * match LBA in CDB + N
713 */
714 case SCSI_PROT_DIF_TYPE2:
Arun Easi0c470872010-07-23 15:28:38 +0500715 if (!ql2xenablehba_err_chk)
716 break;
717
718 if (scsi_prot_sg_count(cmd)) {
719 spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
720 scsi_prot_sglist(cmd)[0].offset;
721 pkt->app_tag = swab32(spt->app_tag);
722 pkt->app_tag_mask[0] = 0xff;
723 pkt->app_tag_mask[1] = 0xff;
724 }
725
726 pkt->ref_tag = cpu_to_le32((uint32_t)
727 (0xffffffff & scsi_get_lba(cmd)));
728
729 /* enable ALL bytes of the ref tag */
730 pkt->ref_tag_mask[0] = 0xff;
731 pkt->ref_tag_mask[1] = 0xff;
732 pkt->ref_tag_mask[2] = 0xff;
733 pkt->ref_tag_mask[3] = 0xff;
Arun Easibad75002010-05-04 15:01:30 -0700734 break;
735
736 /* For Type 3 protection: 16 bit GUARD only */
737 case SCSI_PROT_DIF_TYPE3:
738 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
739 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
740 0x00;
741 break;
742
743 /*
744 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
745 * 16 bit app tag.
746 */
747 case SCSI_PROT_DIF_TYPE1:
748 if (!ql2xenablehba_err_chk)
749 break;
750
751 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
752 op == SCSI_PROT_WRITE_PASS)) {
753 spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
754 scsi_prot_sglist(cmd)[0].offset;
755 DEBUG18(printk(KERN_DEBUG
756 "%s(): LBA from user %p, lba = 0x%x\n",
757 __func__, spt, (int)spt->ref_tag));
758 pkt->ref_tag = swab32(spt->ref_tag);
759 pkt->app_tag_mask[0] = 0x0;
760 pkt->app_tag_mask[1] = 0x0;
761 } else {
762 pkt->ref_tag = cpu_to_le32((uint32_t)
763 (0xffffffff & scsi_get_lba(cmd)));
764 pkt->app_tag = __constant_cpu_to_le16(0);
765 pkt->app_tag_mask[0] = 0x0;
766 pkt->app_tag_mask[1] = 0x0;
767 }
768 /* enable ALL bytes of the ref tag */
769 pkt->ref_tag_mask[0] = 0xff;
770 pkt->ref_tag_mask[1] = 0xff;
771 pkt->ref_tag_mask[2] = 0xff;
772 pkt->ref_tag_mask[3] = 0xff;
773 break;
774 }
775
776 DEBUG18(printk(KERN_DEBUG
777 "%s(): Setting protection Tags: (BIG) ref tag = 0x%x,"
778 " app tag = 0x%x, prot SG count %d , cmd lba 0x%x,"
779 " prot_type=%u\n", __func__, pkt->ref_tag, pkt->app_tag, protcnt,
780 (int)scsi_get_lba(cmd), scsi_get_prot_type(cmd)));
781}
782
783
784static int
785qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
786 uint16_t tot_dsds)
787{
788 void *next_dsd;
789 uint8_t avail_dsds = 0;
790 uint32_t dsd_list_len;
791 struct dsd_dma *dsd_ptr;
792 struct scatterlist *sg;
793 uint32_t *cur_dsd = dsd;
794 int i;
795 uint16_t used_dsds = tot_dsds;
796
797 uint8_t *cp;
798
799 scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
800 dma_addr_t sle_dma;
801
802 /* Allocate additional continuation packets? */
803 if (avail_dsds == 0) {
804 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
805 QLA_DSDS_PER_IOCB : used_dsds;
806 dsd_list_len = (avail_dsds + 1) * 12;
807 used_dsds -= avail_dsds;
808
809 /* allocate tracking DS */
810 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
811 if (!dsd_ptr)
812 return 1;
813
814 /* allocate new list */
815 dsd_ptr->dsd_addr = next_dsd =
816 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
817 &dsd_ptr->dsd_list_dma);
818
819 if (!next_dsd) {
820 /*
821 * Need to cleanup only this dsd_ptr, rest
822 * will be done by sp_free_dma()
823 */
824 kfree(dsd_ptr);
825 return 1;
826 }
827
828 list_add_tail(&dsd_ptr->list,
829 &((struct crc_context *)sp->ctx)->dsd_list);
830
831 sp->flags |= SRB_CRC_CTX_DSD_VALID;
832
833 /* add new list to cmd iocb or last list */
834 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
835 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
836 *cur_dsd++ = dsd_list_len;
837 cur_dsd = (uint32_t *)next_dsd;
838 }
839 sle_dma = sg_dma_address(sg);
840 DEBUG18(printk("%s(): %p, sg entry %d - addr =0x%x 0x%x,"
841 " len =%d\n", __func__ , cur_dsd, i, LSD(sle_dma),
842 MSD(sle_dma), sg_dma_len(sg)));
843 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
844 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
845 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
846 avail_dsds--;
847
848 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
849 cp = page_address(sg_page(sg)) + sg->offset;
850 DEBUG18(printk("%s(): User Data buffer= %p:\n",
851 __func__ , cp));
852 }
853 }
854 /* Null termination */
855 *cur_dsd++ = 0;
856 *cur_dsd++ = 0;
857 *cur_dsd++ = 0;
858 return 0;
859}
860
861static int
862qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
863 uint32_t *dsd,
864 uint16_t tot_dsds)
865{
866 void *next_dsd;
867 uint8_t avail_dsds = 0;
868 uint32_t dsd_list_len;
869 struct dsd_dma *dsd_ptr;
870 struct scatterlist *sg;
871 int i;
872 struct scsi_cmnd *cmd;
873 uint32_t *cur_dsd = dsd;
874 uint16_t used_dsds = tot_dsds;
875
876 uint8_t *cp;
877
878
879 cmd = sp->cmd;
880 scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
881 dma_addr_t sle_dma;
882
883 /* Allocate additional continuation packets? */
884 if (avail_dsds == 0) {
885 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
886 QLA_DSDS_PER_IOCB : used_dsds;
887 dsd_list_len = (avail_dsds + 1) * 12;
888 used_dsds -= avail_dsds;
889
890 /* allocate tracking DS */
891 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
892 if (!dsd_ptr)
893 return 1;
894
895 /* allocate new list */
896 dsd_ptr->dsd_addr = next_dsd =
897 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
898 &dsd_ptr->dsd_list_dma);
899
900 if (!next_dsd) {
901 /*
902 * Need to cleanup only this dsd_ptr, rest
903 * will be done by sp_free_dma()
904 */
905 kfree(dsd_ptr);
906 return 1;
907 }
908
909 list_add_tail(&dsd_ptr->list,
910 &((struct crc_context *)sp->ctx)->dsd_list);
911
912 sp->flags |= SRB_CRC_CTX_DSD_VALID;
913
914 /* add new list to cmd iocb or last list */
915 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
916 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
917 *cur_dsd++ = dsd_list_len;
918 cur_dsd = (uint32_t *)next_dsd;
919 }
920 sle_dma = sg_dma_address(sg);
921 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
922 DEBUG18(printk(KERN_DEBUG
923 "%s(): %p, sg entry %d - addr =0x%x"
924 "0x%x, len =%d\n", __func__ , cur_dsd, i,
925 LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg)));
926 }
927 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
928 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
929 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
930
931 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
932 cp = page_address(sg_page(sg)) + sg->offset;
933 DEBUG18(printk("%s(): Protection Data buffer = %p:\n",
934 __func__ , cp));
935 }
936 avail_dsds--;
937 }
938 /* Null termination */
939 *cur_dsd++ = 0;
940 *cur_dsd++ = 0;
941 *cur_dsd++ = 0;
942 return 0;
943}
944
945/**
946 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
947 * Type 6 IOCB types.
948 *
949 * @sp: SRB command to process
950 * @cmd_pkt: Command type 3 IOCB
951 * @tot_dsds: Total number of segments to transfer
952 */
953static inline int
954qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
955 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
956{
957 uint32_t *cur_dsd, *fcp_dl;
958 scsi_qla_host_t *vha;
959 struct scsi_cmnd *cmd;
960 struct scatterlist *cur_seg;
961 int sgc;
962 uint32_t total_bytes;
963 uint32_t data_bytes;
964 uint32_t dif_bytes;
965 uint8_t bundling = 1;
966 uint16_t blk_size;
967 uint8_t *clr_ptr;
968 struct crc_context *crc_ctx_pkt = NULL;
969 struct qla_hw_data *ha;
970 uint8_t additional_fcpcdb_len;
971 uint16_t fcp_cmnd_len;
972 struct fcp_cmnd *fcp_cmnd;
973 dma_addr_t crc_ctx_dma;
974
975 cmd = sp->cmd;
976
977 sgc = 0;
978 /* Update entry type to indicate Command Type CRC_2 IOCB */
979 *((uint32_t *)(&cmd_pkt->entry_type)) =
980 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
981
982 /* No data transfer */
983 data_bytes = scsi_bufflen(cmd);
984 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
985 DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
986 __func__, data_bytes));
987 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
988 return QLA_SUCCESS;
989 }
990
991 vha = sp->fcport->vha;
992 ha = vha->hw;
993
994 DEBUG18(printk(KERN_DEBUG
Madhuranath Iyengar09d1dc22010-10-15 11:27:44 -0700995 "%s(%ld): Executing cmd sp %p, prot_op=%u.\n", __func__,
996 vha->host_no, sp, scsi_get_prot_op(sp->cmd)));
Arun Easibad75002010-05-04 15:01:30 -0700997
998 cmd_pkt->vp_index = sp->fcport->vp_idx;
999
1000 /* Set transfer direction */
1001 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1002 cmd_pkt->control_flags =
1003 __constant_cpu_to_le16(CF_WRITE_DATA);
1004 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1005 cmd_pkt->control_flags =
1006 __constant_cpu_to_le16(CF_READ_DATA);
1007 }
1008
1009 tot_prot_dsds = scsi_prot_sg_count(cmd);
1010 if (!tot_prot_dsds)
1011 bundling = 0;
1012
1013 /* Allocate CRC context from global pool */
1014 crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
1015 GFP_ATOMIC, &crc_ctx_dma);
1016
1017 if (!crc_ctx_pkt)
1018 goto crc_queuing_error;
1019
1020 /* Zero out CTX area. */
1021 clr_ptr = (uint8_t *)crc_ctx_pkt;
1022 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1023
1024 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1025
1026 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1027
1028 /* Set handle */
1029 crc_ctx_pkt->handle = cmd_pkt->handle;
1030
1031 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1032
1033 qla24xx_set_t10dif_tags(cmd, (struct fw_dif_context *)
1034 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1035
1036 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1037 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1038 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1039
1040 /* Determine SCSI command length -- align to 4 byte boundary */
1041 if (cmd->cmd_len > 16) {
1042 DEBUG18(printk(KERN_INFO "%s(): **** SCSI CMD > 16\n",
1043 __func__));
1044 additional_fcpcdb_len = cmd->cmd_len - 16;
1045 if ((cmd->cmd_len % 4) != 0) {
1046 /* SCSI cmd > 16 bytes must be multiple of 4 */
1047 goto crc_queuing_error;
1048 }
1049 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1050 } else {
1051 additional_fcpcdb_len = 0;
1052 fcp_cmnd_len = 12 + 16 + 4;
1053 }
1054
1055 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1056
1057 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1058 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1059 fcp_cmnd->additional_cdb_len |= 1;
1060 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1061 fcp_cmnd->additional_cdb_len |= 2;
1062
1063 int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
Mike Hernandez85727e12010-11-23 16:52:46 -08001064 host_to_fcp_swap((uint8_t *)&fcp_cmnd->lun, sizeof(fcp_cmnd->lun));
Arun Easibad75002010-05-04 15:01:30 -07001065 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1066 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1067 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1068 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1069 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1070 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1071 fcp_cmnd->task_attribute = 0;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02001072 fcp_cmnd->task_management = 0;
Arun Easibad75002010-05-04 15:01:30 -07001073
1074 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1075
1076 DEBUG18(printk(KERN_INFO "%s(%ld): Total SG(s) Entries %d, Data"
1077 "entries %d, data bytes %d, Protection entries %d\n",
1078 __func__, vha->host_no, tot_dsds, (tot_dsds-tot_prot_dsds),
1079 data_bytes, tot_prot_dsds));
1080
1081 /* Compute dif len and adjust data len to incude protection */
1082 total_bytes = data_bytes;
1083 dif_bytes = 0;
1084 blk_size = cmd->device->sector_size;
Arun Easi0c470872010-07-23 15:28:38 +05001085 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
Arun Easibad75002010-05-04 15:01:30 -07001086 dif_bytes = (data_bytes / blk_size) * 8;
1087 total_bytes += dif_bytes;
1088 }
1089
1090 if (!ql2xenablehba_err_chk)
1091 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1092
1093 if (!bundling) {
1094 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1095 } else {
1096 /*
1097 * Configure Bundling if we need to fetch interlaving
1098 * protection PCI accesses
1099 */
1100 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1101 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1102 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1103 tot_prot_dsds);
1104 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1105 }
1106
1107 /* Finish the common fields of CRC pkt */
1108 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1109 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1110 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1111 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1112 /* Fibre channel byte count */
1113 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1114 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1115 additional_fcpcdb_len);
1116 *fcp_dl = htonl(total_bytes);
1117
1118 DEBUG18(printk(KERN_INFO "%s(%ld): dif bytes = 0x%x (%d), total bytes"
1119 " = 0x%x (%d), dat block size =0x%x (%d)\n", __func__,
1120 vha->host_no, dif_bytes, dif_bytes, total_bytes, total_bytes,
1121 crc_ctx_pkt->blk_size, crc_ctx_pkt->blk_size));
1122
Arun Easi0c470872010-07-23 15:28:38 +05001123 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1124 DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
1125 __func__, data_bytes));
1126 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1127 return QLA_SUCCESS;
1128 }
Arun Easibad75002010-05-04 15:01:30 -07001129 /* Walks data segments */
1130
1131 cmd_pkt->control_flags |=
1132 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
1133 if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
1134 (tot_dsds - tot_prot_dsds)))
1135 goto crc_queuing_error;
1136
1137 if (bundling && tot_prot_dsds) {
1138 /* Walks dif segments */
1139 cur_seg = scsi_prot_sglist(cmd);
1140 cmd_pkt->control_flags |=
1141 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1142 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1143 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1144 tot_prot_dsds))
1145 goto crc_queuing_error;
1146 }
1147 return QLA_SUCCESS;
1148
1149crc_queuing_error:
1150 DEBUG18(qla_printk(KERN_INFO, ha,
1151 "CMD sent FAILED crc_q error:sp = %p\n", sp));
1152 /* Cleanup will be performed by the caller */
1153
1154 return QLA_FUNCTION_FAILED;
1155}
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001156
1157/**
1158 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1159 * @sp: command to send to the ISP
1160 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07001161 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001162 */
1163int
1164qla24xx_start_scsi(srb_t *sp)
1165{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001166 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001167 unsigned long flags;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001168 uint32_t *clr_ptr;
1169 uint32_t index;
1170 uint32_t handle;
1171 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001172 uint16_t cnt;
1173 uint16_t req_cnt;
1174 uint16_t tot_dsds;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001175 struct req_que *req = NULL;
1176 struct rsp_que *rsp = NULL;
1177 struct scsi_cmnd *cmd = sp->cmd;
Andrew Vasquez444786d2009-01-05 11:18:10 -08001178 struct scsi_qla_host *vha = sp->fcport->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001179 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001180
1181 /* Setup device pointers. */
1182 ret = 0;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001183
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001184 qla25xx_set_que(sp, &rsp);
1185 req = vha->req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001186
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001187 /* So we know we haven't pci_map'ed anything yet */
1188 tot_dsds = 0;
1189
1190 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001191 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001192 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
1193 != QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001194 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001195 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001196 }
1197
1198 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001199 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001200
1201 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001202 handle = req->current_outstanding_cmd;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001203 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1204 handle++;
1205 if (handle == MAX_OUTSTANDING_COMMANDS)
1206 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001207 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001208 break;
1209 }
1210 if (index == MAX_OUTSTANDING_COMMANDS)
1211 goto queuing_error;
1212
1213 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001214 if (scsi_sg_count(cmd)) {
1215 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1216 scsi_sg_count(cmd), cmd->sc_data_direction);
1217 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001218 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001219 } else
1220 nseg = 0;
1221
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001222 tot_dsds = nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001223
1224 req_cnt = qla24xx_calc_iocbs(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001225 if (req->cnt < (req_cnt + 2)) {
Andrew Vasquez08029992009-03-24 09:07:55 -07001226 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001227
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001228 if (req->ring_index < cnt)
1229 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001230 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001231 req->cnt = req->length -
1232 (req->ring_index - cnt);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001233 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001234 if (req->cnt < (req_cnt + 2))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001235 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001236
1237 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001238 req->current_outstanding_cmd = handle;
1239 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -07001240 sp->handle = handle;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001241 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001242 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001243
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001244 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001245 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001246
1247 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -05001248 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001249 clr_ptr = (uint32_t *)cmd_pkt + 2;
1250 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1251 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1252
1253 /* Set NPORT-ID and LUN number*/
1254 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1255 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1256 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1257 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001258 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001259
Andrew Vasquez661c3f62005-10-27 11:09:58 -07001260 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -08001261 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001262
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001263 /* Load SCSI command packet. */
1264 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1265 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1266
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001267 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001268
1269 /* Build IOCB segments */
1270 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1271
1272 /* Set total data segment count. */
1273 cmd_pkt->entry_count = (uint8_t)req_cnt;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001274 /* Specify response queue number where completion should happen */
1275 cmd_pkt->entry_status = (uint8_t) rsp->id;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001276 wmb();
1277
1278 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001279 req->ring_index++;
1280 if (req->ring_index == req->length) {
1281 req->ring_index = 0;
1282 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001283 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001284 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001285
1286 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001287
1288 /* Set chip new ring index. */
Andrew Vasquez08029992009-03-24 09:07:55 -07001289 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1290 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001291
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001292 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001293 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001294 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001295 qla24xx_process_response_queue(vha, rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001296
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001297 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001298 return QLA_SUCCESS;
1299
1300queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001301 if (tot_dsds)
1302 scsi_dma_unmap(cmd);
1303
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001304 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001305
1306 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001308
Arun Easibad75002010-05-04 15:01:30 -07001309
1310/**
1311 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1312 * @sp: command to send to the ISP
1313 *
1314 * Returns non-zero if a failure occurred, else zero.
1315 */
1316int
1317qla24xx_dif_start_scsi(srb_t *sp)
1318{
1319 int nseg;
1320 unsigned long flags;
1321 uint32_t *clr_ptr;
1322 uint32_t index;
1323 uint32_t handle;
1324 uint16_t cnt;
1325 uint16_t req_cnt = 0;
1326 uint16_t tot_dsds;
1327 uint16_t tot_prot_dsds;
1328 uint16_t fw_prot_opts = 0;
1329 struct req_que *req = NULL;
1330 struct rsp_que *rsp = NULL;
1331 struct scsi_cmnd *cmd = sp->cmd;
1332 struct scsi_qla_host *vha = sp->fcport->vha;
1333 struct qla_hw_data *ha = vha->hw;
1334 struct cmd_type_crc_2 *cmd_pkt;
1335 uint32_t status = 0;
1336
1337#define QDSS_GOT_Q_SPACE BIT_0
1338
Arun Easi0c470872010-07-23 15:28:38 +05001339 /* Only process protection or >16 cdb in this routine */
1340 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1341 if (cmd->cmd_len <= 16)
1342 return qla24xx_start_scsi(sp);
1343 }
Arun Easibad75002010-05-04 15:01:30 -07001344
1345 /* Setup device pointers. */
1346
1347 qla25xx_set_que(sp, &rsp);
1348 req = vha->req;
1349
1350 /* So we know we haven't pci_map'ed anything yet */
1351 tot_dsds = 0;
1352
1353 /* Send marker if required */
1354 if (vha->marker_needed != 0) {
1355 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1356 QLA_SUCCESS)
1357 return QLA_FUNCTION_FAILED;
1358 vha->marker_needed = 0;
1359 }
1360
1361 /* Acquire ring specific lock */
1362 spin_lock_irqsave(&ha->hardware_lock, flags);
1363
1364 /* Check for room in outstanding command list. */
1365 handle = req->current_outstanding_cmd;
1366 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1367 handle++;
1368 if (handle == MAX_OUTSTANDING_COMMANDS)
1369 handle = 1;
1370 if (!req->outstanding_cmds[handle])
1371 break;
1372 }
1373
1374 if (index == MAX_OUTSTANDING_COMMANDS)
1375 goto queuing_error;
1376
1377 /* Compute number of required data segments */
1378 /* Map the sg table so we have an accurate count of sg entries needed */
1379 if (scsi_sg_count(cmd)) {
1380 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1381 scsi_sg_count(cmd), cmd->sc_data_direction);
1382 if (unlikely(!nseg))
1383 goto queuing_error;
1384 else
1385 sp->flags |= SRB_DMA_VALID;
1386 } else
1387 nseg = 0;
1388
1389 /* number of required data segments */
1390 tot_dsds = nseg;
1391
1392 /* Compute number of required protection segments */
1393 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1394 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1395 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1396 if (unlikely(!nseg))
1397 goto queuing_error;
1398 else
1399 sp->flags |= SRB_CRC_PROT_DMA_VALID;
1400 } else {
1401 nseg = 0;
1402 }
1403
1404 req_cnt = 1;
1405 /* Total Data and protection sg segment(s) */
1406 tot_prot_dsds = nseg;
1407 tot_dsds += nseg;
1408 if (req->cnt < (req_cnt + 2)) {
1409 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
1410
1411 if (req->ring_index < cnt)
1412 req->cnt = cnt - req->ring_index;
1413 else
1414 req->cnt = req->length -
1415 (req->ring_index - cnt);
1416 }
1417
1418 if (req->cnt < (req_cnt + 2))
1419 goto queuing_error;
1420
1421 status |= QDSS_GOT_Q_SPACE;
1422
1423 /* Build header part of command packet (excluding the OPCODE). */
1424 req->current_outstanding_cmd = handle;
1425 req->outstanding_cmds[handle] = sp;
1426 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1427 req->cnt -= req_cnt;
1428
1429 /* Fill-in common area */
1430 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1431 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1432
1433 clr_ptr = (uint32_t *)cmd_pkt + 2;
1434 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1435
1436 /* Set NPORT-ID and LUN number*/
1437 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1438 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1439 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1440 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1441
1442 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
1443 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1444
1445 /* Total Data and protection segment(s) */
1446 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1447
1448 /* Build IOCB segments and adjust for data protection segments */
1449 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1450 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1451 QLA_SUCCESS)
1452 goto queuing_error;
1453
1454 cmd_pkt->entry_count = (uint8_t)req_cnt;
1455 /* Specify response queue number where completion should happen */
1456 cmd_pkt->entry_status = (uint8_t) rsp->id;
1457 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1458 wmb();
1459
1460 /* Adjust ring index. */
1461 req->ring_index++;
1462 if (req->ring_index == req->length) {
1463 req->ring_index = 0;
1464 req->ring_ptr = req->ring;
1465 } else
1466 req->ring_ptr++;
1467
1468 /* Set chip new ring index. */
1469 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1470 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1471
1472 /* Manage unprocessed RIO/ZIO commands in response queue. */
1473 if (vha->flags.process_response_queue &&
1474 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1475 qla24xx_process_response_queue(vha, rsp);
1476
1477 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1478
1479 return QLA_SUCCESS;
1480
1481queuing_error:
1482 if (status & QDSS_GOT_Q_SPACE) {
1483 req->outstanding_cmds[handle] = NULL;
1484 req->cnt += req_cnt;
1485 }
1486 /* Cleanup will be performed by the caller (queuecommand) */
1487
1488 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1489
1490 DEBUG18(qla_printk(KERN_INFO, ha,
1491 "CMD sent FAILED SCSI prot_op:%02x\n", scsi_get_prot_op(cmd)));
1492 return QLA_FUNCTION_FAILED;
1493}
1494
1495
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001496static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001497{
1498 struct scsi_cmnd *cmd = sp->cmd;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001499 struct qla_hw_data *ha = sp->fcport->vha->hw;
1500 int affinity = cmd->request->cpu;
1501
Anirban Chakraborty7163ea82009-08-05 09:18:40 -07001502 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001503 affinity < ha->max_rsp_queues - 1)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001504 *rsp = ha->rsp_q_map[affinity + 1];
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001505 else
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001506 *rsp = ha->rsp_q_map[0];
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001507}
Andrew Vasquezac280b62009-08-20 11:06:05 -07001508
1509/* Generic Control-SRB manipulation functions. */
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001510void *
1511qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001512{
Andrew Vasquezac280b62009-08-20 11:06:05 -07001513 struct qla_hw_data *ha = vha->hw;
1514 struct req_que *req = ha->req_q_map[0];
1515 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1516 uint32_t index, handle;
1517 request_t *pkt;
1518 uint16_t cnt, req_cnt;
1519
1520 pkt = NULL;
1521 req_cnt = 1;
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001522 handle = 0;
1523
1524 if (!sp)
1525 goto skip_cmd_array;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001526
1527 /* Check for room in outstanding command list. */
1528 handle = req->current_outstanding_cmd;
1529 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1530 handle++;
1531 if (handle == MAX_OUTSTANDING_COMMANDS)
1532 handle = 1;
1533 if (!req->outstanding_cmds[handle])
1534 break;
1535 }
1536 if (index == MAX_OUTSTANDING_COMMANDS)
1537 goto queuing_error;
1538
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001539 /* Prep command array. */
1540 req->current_outstanding_cmd = handle;
1541 req->outstanding_cmds[handle] = sp;
1542 sp->handle = handle;
1543
1544skip_cmd_array:
Andrew Vasquezac280b62009-08-20 11:06:05 -07001545 /* Check for room on request queue. */
1546 if (req->cnt < req_cnt) {
1547 if (ha->mqenable)
1548 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001549 else if (IS_QLA82XX(ha))
1550 cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001551 else if (IS_FWI2_CAPABLE(ha))
1552 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
1553 else
1554 cnt = qla2x00_debounce_register(
1555 ISP_REQ_Q_OUT(ha, &reg->isp));
1556
1557 if (req->ring_index < cnt)
1558 req->cnt = cnt - req->ring_index;
1559 else
1560 req->cnt = req->length -
1561 (req->ring_index - cnt);
1562 }
1563 if (req->cnt < req_cnt)
1564 goto queuing_error;
1565
1566 /* Prep packet */
Andrew Vasquezac280b62009-08-20 11:06:05 -07001567 req->cnt -= req_cnt;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001568 pkt = req->ring_ptr;
1569 memset(pkt, 0, REQUEST_ENTRY_SIZE);
1570 pkt->entry_count = req_cnt;
1571 pkt->handle = handle;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001572
1573queuing_error:
1574 return pkt;
1575}
1576
1577static void
1578qla2x00_start_iocbs(srb_t *sp)
1579{
1580 struct qla_hw_data *ha = sp->fcport->vha->hw;
1581 struct req_que *req = ha->req_q_map[0];
1582 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1583 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
1584
Giridhar Malavalia9083012010-04-12 17:59:55 -07001585 if (IS_QLA82XX(ha)) {
1586 qla82xx_start_iocbs(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001587 } else {
Giridhar Malavalia9083012010-04-12 17:59:55 -07001588 /* Adjust ring index. */
1589 req->ring_index++;
1590 if (req->ring_index == req->length) {
1591 req->ring_index = 0;
1592 req->ring_ptr = req->ring;
1593 } else
1594 req->ring_ptr++;
1595
1596 /* Set chip new ring index. */
1597 if (ha->mqenable) {
1598 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
1599 RD_REG_DWORD(&ioreg->hccr);
1600 } else if (IS_QLA82XX(ha)) {
1601 qla82xx_start_iocbs(sp);
1602 } else if (IS_FWI2_CAPABLE(ha)) {
1603 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
1604 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
1605 } else {
1606 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
1607 req->ring_index);
1608 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
1609 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001610 }
1611}
1612
1613static void
1614qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1615{
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001616 struct srb_ctx *ctx = sp->ctx;
1617 struct srb_iocb *lio = ctx->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001618
1619 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1620 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001621 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001622 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001623 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001624 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1625 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1626 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1627 logio->port_id[1] = sp->fcport->d_id.b.area;
1628 logio->port_id[2] = sp->fcport->d_id.b.domain;
1629 logio->vp_index = sp->fcport->vp_idx;
1630}
1631
1632static void
1633qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1634{
1635 struct qla_hw_data *ha = sp->fcport->vha->hw;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001636 struct srb_ctx *ctx = sp->ctx;
1637 struct srb_iocb *lio = ctx->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001638 uint16_t opts;
1639
Giridhar Malavalib9637522010-05-28 15:08:15 -07001640 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001641 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1642 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001643 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1644 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001645 if (HAS_EXTENDED_IDS(ha)) {
1646 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1647 mbx->mb10 = cpu_to_le16(opts);
1648 } else {
1649 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1650 }
1651 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1652 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1653 sp->fcport->d_id.b.al_pa);
1654 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1655}
1656
1657static void
1658qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1659{
1660 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1661 logio->control_flags =
1662 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1663 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1664 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1665 logio->port_id[1] = sp->fcport->d_id.b.area;
1666 logio->port_id[2] = sp->fcport->d_id.b.domain;
1667 logio->vp_index = sp->fcport->vp_idx;
1668}
1669
1670static void
1671qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1672{
1673 struct qla_hw_data *ha = sp->fcport->vha->hw;
1674
Giridhar Malavalib9637522010-05-28 15:08:15 -07001675 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001676 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1677 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1678 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1679 cpu_to_le16(sp->fcport->loop_id):
1680 cpu_to_le16(sp->fcport->loop_id << 8);
1681 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1682 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1683 sp->fcport->d_id.b.al_pa);
1684 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1685 /* Implicit: mbx->mbx10 = 0. */
1686}
1687
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001688static void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001689qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1690{
1691 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1692 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1693 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1694 logio->vp_index = sp->fcport->vp_idx;
1695}
1696
1697static void
1698qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1699{
1700 struct qla_hw_data *ha = sp->fcport->vha->hw;
1701
1702 mbx->entry_type = MBX_IOCB_TYPE;
1703 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1704 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1705 if (HAS_EXTENDED_IDS(ha)) {
1706 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1707 mbx->mb10 = cpu_to_le16(BIT_0);
1708 } else {
1709 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1710 }
1711 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1712 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1713 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1714 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
1715 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1716}
1717
1718static void
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001719qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
1720{
1721 uint32_t flags;
1722 unsigned int lun;
1723 struct fc_port *fcport = sp->fcport;
1724 scsi_qla_host_t *vha = fcport->vha;
1725 struct qla_hw_data *ha = vha->hw;
1726 struct srb_ctx *ctx = sp->ctx;
1727 struct srb_iocb *iocb = ctx->u.iocb_cmd;
1728 struct req_que *req = vha->req;
1729
1730 flags = iocb->u.tmf.flags;
1731 lun = iocb->u.tmf.lun;
1732
1733 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
1734 tsk->entry_count = 1;
1735 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
1736 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
1737 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
1738 tsk->control_flags = cpu_to_le32(flags);
1739 tsk->port_id[0] = fcport->d_id.b.al_pa;
1740 tsk->port_id[1] = fcport->d_id.b.area;
1741 tsk->port_id[2] = fcport->d_id.b.domain;
1742 tsk->vp_index = fcport->vp_idx;
1743
1744 if (flags == TCF_LUN_RESET) {
1745 int_to_scsilun(lun, &tsk->lun);
1746 host_to_fcp_swap((uint8_t *)&tsk->lun,
1747 sizeof(tsk->lun));
1748 }
1749}
1750
1751static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001752qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
1753{
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001754 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001755
1756 els_iocb->entry_type = ELS_IOCB_TYPE;
1757 els_iocb->entry_count = 1;
1758 els_iocb->sys_define = 0;
1759 els_iocb->entry_status = 0;
1760 els_iocb->handle = sp->handle;
1761 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1762 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
1763 els_iocb->vp_index = sp->fcport->vp_idx;
1764 els_iocb->sof_type = EST_SOFI3;
1765 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
1766
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001767 els_iocb->opcode =
1768 (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
1769 bsg_job->request->rqst_data.r_els.els_code :
1770 bsg_job->request->rqst_data.h_els.command_code;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001771 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
1772 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
1773 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
1774 els_iocb->control_flags = 0;
1775 els_iocb->rx_byte_count =
1776 cpu_to_le32(bsg_job->reply_payload.payload_len);
1777 els_iocb->tx_byte_count =
1778 cpu_to_le32(bsg_job->request_payload.payload_len);
1779
1780 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
1781 (bsg_job->request_payload.sg_list)));
1782 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
1783 (bsg_job->request_payload.sg_list)));
1784 els_iocb->tx_len = cpu_to_le32(sg_dma_len
1785 (bsg_job->request_payload.sg_list));
1786
1787 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
1788 (bsg_job->reply_payload.sg_list)));
1789 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
1790 (bsg_job->reply_payload.sg_list)));
1791 els_iocb->rx_len = cpu_to_le32(sg_dma_len
1792 (bsg_job->reply_payload.sg_list));
1793}
1794
1795static void
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05001796qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
1797{
1798 uint16_t avail_dsds;
1799 uint32_t *cur_dsd;
1800 struct scatterlist *sg;
1801 int index;
1802 uint16_t tot_dsds;
1803 scsi_qla_host_t *vha = sp->fcport->vha;
1804 struct qla_hw_data *ha = vha->hw;
1805 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
1806 int loop_iterartion = 0;
1807 int cont_iocb_prsnt = 0;
1808 int entry_count = 1;
1809
1810 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
1811 ct_iocb->entry_type = CT_IOCB_TYPE;
1812 ct_iocb->entry_status = 0;
1813 ct_iocb->handle1 = sp->handle;
1814 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
1815 ct_iocb->status = __constant_cpu_to_le16(0);
1816 ct_iocb->control_flags = __constant_cpu_to_le16(0);
1817 ct_iocb->timeout = 0;
1818 ct_iocb->cmd_dsd_count =
1819 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
1820 ct_iocb->total_dsd_count =
1821 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
1822 ct_iocb->req_bytecount =
1823 cpu_to_le32(bsg_job->request_payload.payload_len);
1824 ct_iocb->rsp_bytecount =
1825 cpu_to_le32(bsg_job->reply_payload.payload_len);
1826
1827 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
1828 (bsg_job->request_payload.sg_list)));
1829 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
1830 (bsg_job->request_payload.sg_list)));
1831 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
1832
1833 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
1834 (bsg_job->reply_payload.sg_list)));
1835 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
1836 (bsg_job->reply_payload.sg_list)));
1837 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
1838
1839 avail_dsds = 1;
1840 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
1841 index = 0;
1842 tot_dsds = bsg_job->reply_payload.sg_cnt;
1843
1844 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
1845 dma_addr_t sle_dma;
1846 cont_a64_entry_t *cont_pkt;
1847
1848 /* Allocate additional continuation packets? */
1849 if (avail_dsds == 0) {
1850 /*
1851 * Five DSDs are available in the Cont.
1852 * Type 1 IOCB.
1853 */
1854 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
1855 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
1856 avail_dsds = 5;
1857 cont_iocb_prsnt = 1;
1858 entry_count++;
1859 }
1860
1861 sle_dma = sg_dma_address(sg);
1862 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1863 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1864 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1865 loop_iterartion++;
1866 avail_dsds--;
1867 }
1868 ct_iocb->entry_count = entry_count;
1869}
1870
1871static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001872qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
1873{
1874 uint16_t avail_dsds;
1875 uint32_t *cur_dsd;
1876 struct scatterlist *sg;
1877 int index;
1878 uint16_t tot_dsds;
1879 scsi_qla_host_t *vha = sp->fcport->vha;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001880 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001881 int loop_iterartion = 0;
1882 int cont_iocb_prsnt = 0;
1883 int entry_count = 1;
1884
1885 ct_iocb->entry_type = CT_IOCB_TYPE;
1886 ct_iocb->entry_status = 0;
1887 ct_iocb->sys_define = 0;
1888 ct_iocb->handle = sp->handle;
1889
1890 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1891 ct_iocb->vp_index = sp->fcport->vp_idx;
1892 ct_iocb->comp_status = __constant_cpu_to_le16(0);
1893
1894 ct_iocb->cmd_dsd_count =
1895 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
1896 ct_iocb->timeout = 0;
1897 ct_iocb->rsp_dsd_count =
1898 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
1899 ct_iocb->rsp_byte_count =
1900 cpu_to_le32(bsg_job->reply_payload.payload_len);
1901 ct_iocb->cmd_byte_count =
1902 cpu_to_le32(bsg_job->request_payload.payload_len);
1903 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
1904 (bsg_job->request_payload.sg_list)));
1905 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
1906 (bsg_job->request_payload.sg_list)));
1907 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
1908 (bsg_job->request_payload.sg_list));
1909
1910 avail_dsds = 1;
1911 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
1912 index = 0;
1913 tot_dsds = bsg_job->reply_payload.sg_cnt;
1914
1915 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
1916 dma_addr_t sle_dma;
1917 cont_a64_entry_t *cont_pkt;
1918
1919 /* Allocate additional continuation packets? */
1920 if (avail_dsds == 0) {
1921 /*
1922 * Five DSDs are available in the Cont.
1923 * Type 1 IOCB.
1924 */
1925 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
1926 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
1927 avail_dsds = 5;
1928 cont_iocb_prsnt = 1;
1929 entry_count++;
1930 }
1931
1932 sle_dma = sg_dma_address(sg);
1933 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1934 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1935 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1936 loop_iterartion++;
1937 avail_dsds--;
1938 }
1939 ct_iocb->entry_count = entry_count;
1940}
1941
Andrew Vasquezac280b62009-08-20 11:06:05 -07001942int
1943qla2x00_start_sp(srb_t *sp)
1944{
1945 int rval;
1946 struct qla_hw_data *ha = sp->fcport->vha->hw;
1947 void *pkt;
1948 struct srb_ctx *ctx = sp->ctx;
1949 unsigned long flags;
1950
1951 rval = QLA_FUNCTION_FAILED;
1952 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001953 pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001954 if (!pkt)
1955 goto done;
1956
1957 rval = QLA_SUCCESS;
1958 switch (ctx->type) {
1959 case SRB_LOGIN_CMD:
1960 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001961 qla24xx_login_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07001962 qla2x00_login_iocb(sp, pkt);
1963 break;
1964 case SRB_LOGOUT_CMD:
1965 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001966 qla24xx_logout_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07001967 qla2x00_logout_iocb(sp, pkt);
1968 break;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001969 case SRB_ELS_CMD_RPT:
1970 case SRB_ELS_CMD_HST:
1971 qla24xx_els_iocb(sp, pkt);
1972 break;
1973 case SRB_CT_CMD:
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05001974 IS_FWI2_CAPABLE(ha) ?
1975 qla24xx_ct_iocb(sp, pkt) :
1976 qla2x00_ct_iocb(sp, pkt);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001977 break;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001978 case SRB_ADISC_CMD:
1979 IS_FWI2_CAPABLE(ha) ?
1980 qla24xx_adisc_iocb(sp, pkt) :
1981 qla2x00_adisc_iocb(sp, pkt);
1982 break;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001983 case SRB_TM_CMD:
1984 qla24xx_tm_iocb(sp, pkt);
1985 break;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001986 default:
1987 break;
1988 }
1989
1990 wmb();
1991 qla2x00_start_iocbs(sp);
1992done:
1993 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1994 return rval;
1995}