blob: d57669aa4615d33d80fe87cfe1f9550555466518 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
Adrian Bunk413975a2006-06-30 02:33:06 -070015static void qla2x00_isp_cmd(scsi_qla_host_t *ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/**
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
24qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
25{
26 uint16_t cflags;
27
28 cflags = 0;
29
30 /* Set transfer direction */
31 if (cmd->sc_data_direction == DMA_TO_DEVICE)
32 cflags = CF_WRITE;
33 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
34 cflags = CF_READ;
35 return (cflags);
36}
37
38/**
39 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
40 * Continuation Type 0 IOCBs to allocate.
41 *
42 * @dsds: number of data segment decriptors needed
43 *
44 * Returns the number of IOCB entries needed to store @dsds.
45 */
46uint16_t
47qla2x00_calc_iocbs_32(uint16_t dsds)
48{
49 uint16_t iocbs;
50
51 iocbs = 1;
52 if (dsds > 3) {
53 iocbs += (dsds - 3) / 7;
54 if ((dsds - 3) % 7)
55 iocbs++;
56 }
57 return (iocbs);
58}
59
60/**
61 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
62 * Continuation Type 1 IOCBs to allocate.
63 *
64 * @dsds: number of data segment decriptors needed
65 *
66 * Returns the number of IOCB entries needed to store @dsds.
67 */
68uint16_t
69qla2x00_calc_iocbs_64(uint16_t dsds)
70{
71 uint16_t iocbs;
72
73 iocbs = 1;
74 if (dsds > 2) {
75 iocbs += (dsds - 2) / 5;
76 if ((dsds - 2) % 5)
77 iocbs++;
78 }
79 return (iocbs);
80}
81
82/**
83 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
84 * @ha: HA context
85 *
86 * Returns a pointer to the Continuation Type 0 IOCB packet.
87 */
88static inline cont_entry_t *
89qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
90{
91 cont_entry_t *cont_pkt;
92
93 /* Adjust ring index. */
94 ha->req_ring_index++;
95 if (ha->req_ring_index == ha->request_q_length) {
96 ha->req_ring_index = 0;
97 ha->request_ring_ptr = ha->request_ring;
98 } else {
99 ha->request_ring_ptr++;
100 }
101
102 cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
103
104 /* Load packet defaults. */
105 *((uint32_t *)(&cont_pkt->entry_type)) =
106 __constant_cpu_to_le32(CONTINUE_TYPE);
107
108 return (cont_pkt);
109}
110
111/**
112 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
113 * @ha: HA context
114 *
115 * Returns a pointer to the continuation type 1 IOCB packet.
116 */
117static inline cont_a64_entry_t *
118qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
119{
120 cont_a64_entry_t *cont_pkt;
121
122 /* Adjust ring index. */
123 ha->req_ring_index++;
124 if (ha->req_ring_index == ha->request_q_length) {
125 ha->req_ring_index = 0;
126 ha->request_ring_ptr = ha->request_ring;
127 } else {
128 ha->request_ring_ptr++;
129 }
130
131 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
132
133 /* Load packet defaults. */
134 *((uint32_t *)(&cont_pkt->entry_type)) =
135 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
136
137 return (cont_pkt);
138}
139
140/**
141 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
142 * capable IOCB types.
143 *
144 * @sp: SRB command to process
145 * @cmd_pkt: Command type 2 IOCB
146 * @tot_dsds: Total number of segments to transfer
147 */
148void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
149 uint16_t tot_dsds)
150{
151 uint16_t avail_dsds;
152 uint32_t *cur_dsd;
153 scsi_qla_host_t *ha;
154 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900155 struct scatterlist *sg;
156 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 cmd = sp->cmd;
159
160 /* Update entry type to indicate Command Type 2 IOCB */
161 *((uint32_t *)(&cmd_pkt->entry_type)) =
162 __constant_cpu_to_le32(COMMAND_TYPE);
163
164 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900165 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
167 return;
168 }
169
170 ha = sp->ha;
171
172 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
173
174 /* Three DSDs are available in the Command Type 2 IOCB */
175 avail_dsds = 3;
176 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
177
178 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900179 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
180 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900182 /* Allocate additional continuation packets? */
183 if (avail_dsds == 0) {
184 /*
185 * Seven DSDs are available in the Continuation
186 * Type 0 IOCB.
187 */
188 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
189 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
190 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900192
193 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
194 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
195 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197}
198
199/**
200 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
201 * capable IOCB types.
202 *
203 * @sp: SRB command to process
204 * @cmd_pkt: Command type 3 IOCB
205 * @tot_dsds: Total number of segments to transfer
206 */
207void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
208 uint16_t tot_dsds)
209{
210 uint16_t avail_dsds;
211 uint32_t *cur_dsd;
212 scsi_qla_host_t *ha;
213 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900214 struct scatterlist *sg;
215 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 cmd = sp->cmd;
218
219 /* Update entry type to indicate Command Type 3 IOCB */
220 *((uint32_t *)(&cmd_pkt->entry_type)) =
221 __constant_cpu_to_le32(COMMAND_A64_TYPE);
222
223 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900224 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
226 return;
227 }
228
229 ha = sp->ha;
230
231 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
232
233 /* Two DSDs are available in the Command Type 3 IOCB */
234 avail_dsds = 2;
235 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
236
237 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900238 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
239 dma_addr_t sle_dma;
240 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900242 /* Allocate additional continuation packets? */
243 if (avail_dsds == 0) {
244 /*
245 * Five DSDs are available in the Continuation
246 * Type 1 IOCB.
247 */
248 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
249 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
250 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900252
253 sle_dma = sg_dma_address(sg);
254 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
255 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
256 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
257 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259}
260
261/**
262 * qla2x00_start_scsi() - Send a SCSI command to the ISP
263 * @sp: command to send to the ISP
264 *
265 * Returns non-zero if a failure occured, else zero.
266 */
267int
268qla2x00_start_scsi(srb_t *sp)
269{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900270 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unsigned long flags;
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700272 scsi_qla_host_t *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct scsi_cmnd *cmd;
274 uint32_t *clr_ptr;
275 uint32_t index;
276 uint32_t handle;
277 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 uint16_t cnt;
279 uint16_t req_cnt;
280 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700281 struct device_reg_2xxx __iomem *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* Setup device pointers. */
284 ret = 0;
bdf79622005-04-17 15:06:53 -0500285 ha = sp->ha;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700286 reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 cmd = sp->cmd;
83021922005-04-17 15:10:41 -0500288 /* So we know we haven't pci_map'ed anything yet */
289 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* Send marker if required */
292 if (ha->marker_needed != 0) {
293 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
294 return (QLA_FUNCTION_FAILED);
295 }
296 ha->marker_needed = 0;
297 }
298
299 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700300 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 /* Check for room in outstanding command list. */
303 handle = ha->current_outstanding_cmd;
304 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
305 handle++;
306 if (handle == MAX_OUTSTANDING_COMMANDS)
307 handle = 1;
Andrew Vasquez700ca0e2007-09-20 14:07:46 -0700308 if (!ha->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 break;
310 }
311 if (index == MAX_OUTSTANDING_COMMANDS)
312 goto queuing_error;
313
83021922005-04-17 15:10:41 -0500314 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700315 if (scsi_sg_count(cmd)) {
316 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
317 scsi_sg_count(cmd), cmd->sc_data_direction);
318 if (unlikely(!nseg))
319 goto queuing_error;
320 } else
321 nseg = 0;
322
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900323 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700326 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (ha->req_q_cnt < (req_cnt + 2)) {
328 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
329 if (ha->req_ring_index < cnt)
330 ha->req_q_cnt = cnt - ha->req_ring_index;
331 else
332 ha->req_q_cnt = ha->request_q_length -
333 (ha->req_ring_index - cnt);
334 }
335 if (ha->req_q_cnt < (req_cnt + 2))
336 goto queuing_error;
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* Build command packet */
339 ha->current_outstanding_cmd = handle;
340 ha->outstanding_cmds[handle] = sp;
341 sp->ha = ha;
342 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
343 ha->req_q_cnt -= req_cnt;
344
345 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
346 cmd_pkt->handle = handle;
347 /* Zero out remaining portion of packet. */
348 clr_ptr = (uint32_t *)cmd_pkt + 2;
349 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
350 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
351
bdf79622005-04-17 15:06:53 -0500352 /* Set target ID and LUN number*/
353 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
354 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* Update tagged queuing modifier */
357 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Load SCSI command packet. */
360 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900361 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700364 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* Set total data segment count. */
367 cmd_pkt->entry_count = (uint8_t)req_cnt;
368 wmb();
369
370 /* Adjust ring index. */
371 ha->req_ring_index++;
372 if (ha->req_ring_index == ha->request_q_length) {
373 ha->req_ring_index = 0;
374 ha->request_ring_ptr = ha->request_ring;
375 } else
376 ha->request_ring_ptr++;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* Set chip new ring index. */
381 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
382 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
383
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700384 /* Manage unprocessed RIO/ZIO commands in response queue. */
385 if (ha->flags.process_response_queue &&
386 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
387 qla2x00_process_response_queue(ha);
388
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700389 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return (QLA_SUCCESS);
391
392queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900393 if (tot_dsds)
394 scsi_dma_unmap(cmd);
395
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700396 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 return (QLA_FUNCTION_FAILED);
399}
400
401/**
402 * qla2x00_marker() - Send a marker IOCB to the firmware.
403 * @ha: HA context
404 * @loop_id: loop ID
405 * @lun: LUN
406 * @type: marker modifier
407 *
408 * Can be called from both normal and interrupt context.
409 *
410 * Returns non-zero if a failure occured, else zero.
411 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700412int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413__qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
414 uint8_t type)
415{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700416 mrk_entry_t *mrk;
417 struct mrk_entry_24xx *mrk24;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700418 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700420 mrk24 = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700421 mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700422 if (mrk == NULL) {
423 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
424 __func__, ha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return (QLA_FUNCTION_FAILED);
427 }
428
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700429 mrk->entry_type = MARKER_TYPE;
430 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700432 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700433 mrk24 = (struct mrk_entry_24xx *) mrk;
434 mrk24->nport_handle = cpu_to_le16(loop_id);
435 mrk24->lun[1] = LSB(lun);
436 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700437 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700438 mrk24->vp_index = ha->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700439 } else {
440 SET_TARGET_ID(ha, mrk->target, loop_id);
441 mrk->lun = cpu_to_le16(lun);
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444 wmb();
445
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700446 qla2x00_isp_cmd(pha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 return (QLA_SUCCESS);
449}
450
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700451int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
453 uint8_t type)
454{
455 int ret;
456 unsigned long flags = 0;
Seokmann Ju246de422008-07-10 16:55:55 -0700457 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Seokmann Ju246de422008-07-10 16:55:55 -0700459 spin_lock_irqsave(&pha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 ret = __qla2x00_marker(ha, loop_id, lun, type);
Seokmann Ju246de422008-07-10 16:55:55 -0700461 spin_unlock_irqrestore(&pha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 return (ret);
464}
465
466/**
467 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
468 * @ha: HA context
469 *
470 * Note: The caller must hold the hardware lock before calling this routine.
471 *
472 * Returns NULL if function failed, else, a pointer to the request packet.
473 */
474static request_t *
475qla2x00_req_pkt(scsi_qla_host_t *ha)
476{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700477 device_reg_t __iomem *reg = ha->iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 request_t *pkt = NULL;
479 uint16_t cnt;
480 uint32_t *dword_ptr;
481 uint32_t timer;
482 uint16_t req_cnt = 1;
483
484 /* Wait 1 second for slot. */
485 for (timer = HZ; timer; timer--) {
486 if ((req_cnt + 2) >= ha->req_q_cnt) {
487 /* Calculate number of free request entries. */
Andrew Vasqueze4289242007-07-19 15:05:56 -0700488 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700489 cnt = (uint16_t)RD_REG_DWORD(
490 &reg->isp24.req_q_out);
491 else
492 cnt = qla2x00_debounce_register(
493 ISP_REQ_Q_OUT(ha, &reg->isp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (ha->req_ring_index < cnt)
495 ha->req_q_cnt = cnt - ha->req_ring_index;
496 else
497 ha->req_q_cnt = ha->request_q_length -
498 (ha->req_ring_index - cnt);
499 }
500 /* If room for request in request ring. */
501 if ((req_cnt + 2) < ha->req_q_cnt) {
502 ha->req_q_cnt--;
503 pkt = ha->request_ring_ptr;
504
505 /* Zero out packet. */
506 dword_ptr = (uint32_t *)pkt;
507 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
508 *dword_ptr++ = 0;
509
510 /* Set system defined field. */
511 pkt->sys_define = (uint8_t)ha->req_ring_index;
512
513 /* Set entry count. */
514 pkt->entry_count = 1;
515
516 break;
517 }
518
519 /* Release ring specific lock */
520 spin_unlock(&ha->hardware_lock);
521
522 udelay(2); /* 2 us */
523
524 /* Check for pending interrupts. */
525 /* During init we issue marker directly */
Andrew Vasqueza3a63d52007-10-19 15:59:14 -0700526 if (!ha->marker_needed && !ha->flags.init_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 qla2x00_poll(ha);
528
529 spin_lock_irq(&ha->hardware_lock);
530 }
531 if (!pkt) {
532 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
533 }
534
535 return (pkt);
536}
537
538/**
539 * qla2x00_isp_cmd() - Modify the request ring pointer.
540 * @ha: HA context
541 *
542 * Note: The caller must hold the hardware lock before calling this routine.
543 */
Adrian Bunk413975a2006-06-30 02:33:06 -0700544static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545qla2x00_isp_cmd(scsi_qla_host_t *ha)
546{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700547 device_reg_t __iomem *reg = ha->iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 DEBUG5(printk("%s(): IOCB data:\n", __func__));
550 DEBUG5(qla2x00_dump_buffer(
551 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
552
553 /* Adjust ring index. */
554 ha->req_ring_index++;
555 if (ha->req_ring_index == ha->request_q_length) {
556 ha->req_ring_index = 0;
557 ha->request_ring_ptr = ha->request_ring;
558 } else
559 ha->request_ring_ptr++;
560
561 /* Set chip new ring index. */
Andrew Vasqueze4289242007-07-19 15:05:56 -0700562 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700563 WRT_REG_DWORD(&reg->isp24.req_q_in, ha->req_ring_index);
564 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
565 } else {
566 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp), ha->req_ring_index);
567 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
568 }
569
570}
571
572/**
573 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
574 * Continuation Type 1 IOCBs to allocate.
575 *
576 * @dsds: number of data segment decriptors needed
577 *
578 * Returns the number of IOCB entries needed to store @dsds.
579 */
580static inline uint16_t
581qla24xx_calc_iocbs(uint16_t dsds)
582{
583 uint16_t iocbs;
584
585 iocbs = 1;
586 if (dsds > 1) {
587 iocbs += (dsds - 1) / 5;
588 if ((dsds - 1) % 5)
589 iocbs++;
590 }
591 return iocbs;
592}
593
594/**
595 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
596 * IOCB types.
597 *
598 * @sp: SRB command to process
599 * @cmd_pkt: Command type 3 IOCB
600 * @tot_dsds: Total number of segments to transfer
601 */
602static inline void
603qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
604 uint16_t tot_dsds)
605{
606 uint16_t avail_dsds;
607 uint32_t *cur_dsd;
608 scsi_qla_host_t *ha;
609 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900610 struct scatterlist *sg;
611 int i;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700612
613 cmd = sp->cmd;
614
615 /* Update entry type to indicate Command Type 3 IOCB */
616 *((uint32_t *)(&cmd_pkt->entry_type)) =
617 __constant_cpu_to_le32(COMMAND_TYPE_7);
618
619 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900620 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700621 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
622 return;
623 }
624
625 ha = sp->ha;
626
627 /* Set transfer direction */
628 if (cmd->sc_data_direction == DMA_TO_DEVICE)
629 cmd_pkt->task_mgmt_flags =
630 __constant_cpu_to_le16(TMF_WRITE_DATA);
631 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
632 cmd_pkt->task_mgmt_flags =
633 __constant_cpu_to_le16(TMF_READ_DATA);
634
635 /* One DSD is available in the Command Type 3 IOCB */
636 avail_dsds = 1;
637 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
638
639 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700640
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900641 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
642 dma_addr_t sle_dma;
643 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700644
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900645 /* Allocate additional continuation packets? */
646 if (avail_dsds == 0) {
647 /*
648 * Five DSDs are available in the Continuation
649 * Type 1 IOCB.
650 */
651 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
652 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
653 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700654 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900655
656 sle_dma = sg_dma_address(sg);
657 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
658 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
659 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
660 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700661 }
662}
663
664
665/**
666 * qla24xx_start_scsi() - Send a SCSI command to the ISP
667 * @sp: command to send to the ISP
668 *
669 * Returns non-zero if a failure occured, else zero.
670 */
671int
672qla24xx_start_scsi(srb_t *sp)
673{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900674 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700675 unsigned long flags;
Seokmann Ju246de422008-07-10 16:55:55 -0700676 scsi_qla_host_t *ha, *pha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700677 struct scsi_cmnd *cmd;
678 uint32_t *clr_ptr;
679 uint32_t index;
680 uint32_t handle;
681 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700682 uint16_t cnt;
683 uint16_t req_cnt;
684 uint16_t tot_dsds;
Linus Torvaldsdb776a12005-07-26 14:50:02 -0700685 struct device_reg_24xx __iomem *reg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700686
687 /* Setup device pointers. */
688 ret = 0;
689 ha = sp->ha;
Seokmann Ju246de422008-07-10 16:55:55 -0700690 pha = to_qla_parent(ha);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700691 reg = &ha->iobase->isp24;
692 cmd = sp->cmd;
693 /* So we know we haven't pci_map'ed anything yet */
694 tot_dsds = 0;
695
696 /* Send marker if required */
697 if (ha->marker_needed != 0) {
698 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
699 return QLA_FUNCTION_FAILED;
700 }
701 ha->marker_needed = 0;
702 }
703
704 /* Acquire ring specific lock */
Seokmann Ju246de422008-07-10 16:55:55 -0700705 spin_lock_irqsave(&pha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700706
707 /* Check for room in outstanding command list. */
708 handle = ha->current_outstanding_cmd;
709 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
710 handle++;
711 if (handle == MAX_OUTSTANDING_COMMANDS)
712 handle = 1;
Andrew Vasquez700ca0e2007-09-20 14:07:46 -0700713 if (!ha->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700714 break;
715 }
716 if (index == MAX_OUTSTANDING_COMMANDS)
717 goto queuing_error;
718
719 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700720 if (scsi_sg_count(cmd)) {
721 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
722 scsi_sg_count(cmd), cmd->sc_data_direction);
723 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700724 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700725 } else
726 nseg = 0;
727
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900728 tot_dsds = nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700729
730 req_cnt = qla24xx_calc_iocbs(tot_dsds);
731 if (ha->req_q_cnt < (req_cnt + 2)) {
732 cnt = (uint16_t)RD_REG_DWORD_RELAXED(&reg->req_q_out);
733 if (ha->req_ring_index < cnt)
734 ha->req_q_cnt = cnt - ha->req_ring_index;
735 else
736 ha->req_q_cnt = ha->request_q_length -
737 (ha->req_ring_index - cnt);
738 }
Andrew Vasquez131736d2005-08-26 19:09:20 -0700739 if (ha->req_q_cnt < (req_cnt + 2))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700740 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700741
742 /* Build command packet. */
743 ha->current_outstanding_cmd = handle;
744 ha->outstanding_cmds[handle] = sp;
745 sp->ha = ha;
746 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
747 ha->req_q_cnt -= req_cnt;
748
749 cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
750 cmd_pkt->handle = handle;
751
752 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -0500753 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700754 clr_ptr = (uint32_t *)cmd_pkt + 2;
755 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
756 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
757
758 /* Set NPORT-ID and LUN number*/
759 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
760 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
761 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
762 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700763 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700764
Andrew Vasquez661c3f62005-10-27 11:09:58 -0700765 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -0800766 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700767
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700768 /* Load SCSI command packet. */
769 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
770 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
771
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900772 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700773
774 /* Build IOCB segments */
775 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
776
777 /* Set total data segment count. */
778 cmd_pkt->entry_count = (uint8_t)req_cnt;
779 wmb();
780
781 /* Adjust ring index. */
782 ha->req_ring_index++;
783 if (ha->req_ring_index == ha->request_q_length) {
784 ha->req_ring_index = 0;
785 ha->request_ring_ptr = ha->request_ring;
786 } else
787 ha->request_ring_ptr++;
788
789 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700790
791 /* Set chip new ring index. */
792 WRT_REG_DWORD(&reg->req_q_in, ha->req_ring_index);
793 RD_REG_DWORD_RELAXED(&reg->req_q_in); /* PCI Posting. */
794
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700795 /* Manage unprocessed RIO/ZIO commands in response queue. */
796 if (ha->flags.process_response_queue &&
797 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
798 qla24xx_process_response_queue(ha);
799
Seokmann Ju246de422008-07-10 16:55:55 -0700800 spin_unlock_irqrestore(&pha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700801 return QLA_SUCCESS;
802
803queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900804 if (tot_dsds)
805 scsi_dma_unmap(cmd);
806
Seokmann Ju246de422008-07-10 16:55:55 -0700807 spin_unlock_irqrestore(&pha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700808
809 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}