blob: 6e14c8eaca82061a1acb391fa0e06dab90471f14 [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;
Seokmann Ju246de422008-07-10 16:55:55 -0700272 scsi_qla_host_t *ha, *pha;
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;
Seokmann Ju246de422008-07-10 16:55:55 -0700286 pha = to_qla_parent(ha);
Andrew Vasquez3d716442005-07-06 10:30:26 -0700287 reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 cmd = sp->cmd;
83021922005-04-17 15:10:41 -0500289 /* So we know we haven't pci_map'ed anything yet */
290 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Send marker if required */
293 if (ha->marker_needed != 0) {
294 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
295 return (QLA_FUNCTION_FAILED);
296 }
297 ha->marker_needed = 0;
298 }
299
300 /* Acquire ring specific lock */
Seokmann Ju246de422008-07-10 16:55:55 -0700301 spin_lock_irqsave(&pha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Check for room in outstanding command list. */
304 handle = ha->current_outstanding_cmd;
305 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
306 handle++;
307 if (handle == MAX_OUTSTANDING_COMMANDS)
308 handle = 1;
Andrew Vasquez700ca0e2007-09-20 14:07:46 -0700309 if (!ha->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 }
312 if (index == MAX_OUTSTANDING_COMMANDS)
313 goto queuing_error;
314
83021922005-04-17 15:10:41 -0500315 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700316 if (scsi_sg_count(cmd)) {
317 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
318 scsi_sg_count(cmd), cmd->sc_data_direction);
319 if (unlikely(!nseg))
320 goto queuing_error;
321 } else
322 nseg = 0;
323
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900324 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700327 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (ha->req_q_cnt < (req_cnt + 2)) {
329 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
330 if (ha->req_ring_index < cnt)
331 ha->req_q_cnt = cnt - ha->req_ring_index;
332 else
333 ha->req_q_cnt = ha->request_q_length -
334 (ha->req_ring_index - cnt);
335 }
336 if (ha->req_q_cnt < (req_cnt + 2))
337 goto queuing_error;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* Build command packet */
340 ha->current_outstanding_cmd = handle;
341 ha->outstanding_cmds[handle] = sp;
342 sp->ha = ha;
343 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
344 ha->req_q_cnt -= req_cnt;
345
346 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
347 cmd_pkt->handle = handle;
348 /* Zero out remaining portion of packet. */
349 clr_ptr = (uint32_t *)cmd_pkt + 2;
350 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
351 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
352
bdf79622005-04-17 15:06:53 -0500353 /* Set target ID and LUN number*/
354 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
355 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Update tagged queuing modifier */
358 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Load SCSI command packet. */
361 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900362 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700365 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* Set total data segment count. */
368 cmd_pkt->entry_count = (uint8_t)req_cnt;
369 wmb();
370
371 /* Adjust ring index. */
372 ha->req_ring_index++;
373 if (ha->req_ring_index == ha->request_q_length) {
374 ha->req_ring_index = 0;
375 ha->request_ring_ptr = ha->request_ring;
376 } else
377 ha->request_ring_ptr++;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /* Set chip new ring index. */
382 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
383 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
384
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700385 /* Manage unprocessed RIO/ZIO commands in response queue. */
386 if (ha->flags.process_response_queue &&
387 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
388 qla2x00_process_response_queue(ha);
389
Seokmann Ju246de422008-07-10 16:55:55 -0700390 spin_unlock_irqrestore(&pha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return (QLA_SUCCESS);
392
393queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900394 if (tot_dsds)
395 scsi_dma_unmap(cmd);
396
Seokmann Ju246de422008-07-10 16:55:55 -0700397 spin_unlock_irqrestore(&pha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return (QLA_FUNCTION_FAILED);
400}
401
402/**
403 * qla2x00_marker() - Send a marker IOCB to the firmware.
404 * @ha: HA context
405 * @loop_id: loop ID
406 * @lun: LUN
407 * @type: marker modifier
408 *
409 * Can be called from both normal and interrupt context.
410 *
411 * Returns non-zero if a failure occured, else zero.
412 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700413int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414__qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
415 uint8_t type)
416{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700417 mrk_entry_t *mrk;
418 struct mrk_entry_24xx *mrk24;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700419 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700421 mrk24 = NULL;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700422 mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700423 if (mrk == NULL) {
424 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
425 __func__, ha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 return (QLA_FUNCTION_FAILED);
428 }
429
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700430 mrk->entry_type = MARKER_TYPE;
431 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700433 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700434 mrk24 = (struct mrk_entry_24xx *) mrk;
435 mrk24->nport_handle = cpu_to_le16(loop_id);
436 mrk24->lun[1] = LSB(lun);
437 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700438 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700439 mrk24->vp_index = ha->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700440 } else {
441 SET_TARGET_ID(ha, mrk->target, loop_id);
442 mrk->lun = cpu_to_le16(lun);
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 wmb();
446
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700447 qla2x00_isp_cmd(pha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return (QLA_SUCCESS);
450}
451
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700452int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
454 uint8_t type)
455{
456 int ret;
457 unsigned long flags = 0;
Seokmann Ju246de422008-07-10 16:55:55 -0700458 scsi_qla_host_t *pha = to_qla_parent(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Seokmann Ju246de422008-07-10 16:55:55 -0700460 spin_lock_irqsave(&pha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 ret = __qla2x00_marker(ha, loop_id, lun, type);
Seokmann Ju246de422008-07-10 16:55:55 -0700462 spin_unlock_irqrestore(&pha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 return (ret);
465}
466
467/**
468 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
469 * @ha: HA context
470 *
471 * Note: The caller must hold the hardware lock before calling this routine.
472 *
473 * Returns NULL if function failed, else, a pointer to the request packet.
474 */
475static request_t *
476qla2x00_req_pkt(scsi_qla_host_t *ha)
477{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700478 device_reg_t __iomem *reg = ha->iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 request_t *pkt = NULL;
480 uint16_t cnt;
481 uint32_t *dword_ptr;
482 uint32_t timer;
483 uint16_t req_cnt = 1;
484
485 /* Wait 1 second for slot. */
486 for (timer = HZ; timer; timer--) {
487 if ((req_cnt + 2) >= ha->req_q_cnt) {
488 /* Calculate number of free request entries. */
Andrew Vasqueze4289242007-07-19 15:05:56 -0700489 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700490 cnt = (uint16_t)RD_REG_DWORD(
491 &reg->isp24.req_q_out);
492 else
493 cnt = qla2x00_debounce_register(
494 ISP_REQ_Q_OUT(ha, &reg->isp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (ha->req_ring_index < cnt)
496 ha->req_q_cnt = cnt - ha->req_ring_index;
497 else
498 ha->req_q_cnt = ha->request_q_length -
499 (ha->req_ring_index - cnt);
500 }
501 /* If room for request in request ring. */
502 if ((req_cnt + 2) < ha->req_q_cnt) {
503 ha->req_q_cnt--;
504 pkt = ha->request_ring_ptr;
505
506 /* Zero out packet. */
507 dword_ptr = (uint32_t *)pkt;
508 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
509 *dword_ptr++ = 0;
510
511 /* Set system defined field. */
512 pkt->sys_define = (uint8_t)ha->req_ring_index;
513
514 /* Set entry count. */
515 pkt->entry_count = 1;
516
517 break;
518 }
519
520 /* Release ring specific lock */
521 spin_unlock(&ha->hardware_lock);
522
523 udelay(2); /* 2 us */
524
525 /* Check for pending interrupts. */
526 /* During init we issue marker directly */
Andrew Vasqueza3a63d52007-10-19 15:59:14 -0700527 if (!ha->marker_needed && !ha->flags.init_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 qla2x00_poll(ha);
529
530 spin_lock_irq(&ha->hardware_lock);
531 }
532 if (!pkt) {
533 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
534 }
535
536 return (pkt);
537}
538
539/**
540 * qla2x00_isp_cmd() - Modify the request ring pointer.
541 * @ha: HA context
542 *
543 * Note: The caller must hold the hardware lock before calling this routine.
544 */
Adrian Bunk413975a2006-06-30 02:33:06 -0700545static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546qla2x00_isp_cmd(scsi_qla_host_t *ha)
547{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700548 device_reg_t __iomem *reg = ha->iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 DEBUG5(printk("%s(): IOCB data:\n", __func__));
551 DEBUG5(qla2x00_dump_buffer(
552 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
553
554 /* Adjust ring index. */
555 ha->req_ring_index++;
556 if (ha->req_ring_index == ha->request_q_length) {
557 ha->req_ring_index = 0;
558 ha->request_ring_ptr = ha->request_ring;
559 } else
560 ha->request_ring_ptr++;
561
562 /* Set chip new ring index. */
Andrew Vasqueze4289242007-07-19 15:05:56 -0700563 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700564 WRT_REG_DWORD(&reg->isp24.req_q_in, ha->req_ring_index);
565 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
566 } else {
567 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp), ha->req_ring_index);
568 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
569 }
570
571}
572
573/**
574 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
575 * Continuation Type 1 IOCBs to allocate.
576 *
577 * @dsds: number of data segment decriptors needed
578 *
579 * Returns the number of IOCB entries needed to store @dsds.
580 */
581static inline uint16_t
582qla24xx_calc_iocbs(uint16_t dsds)
583{
584 uint16_t iocbs;
585
586 iocbs = 1;
587 if (dsds > 1) {
588 iocbs += (dsds - 1) / 5;
589 if ((dsds - 1) % 5)
590 iocbs++;
591 }
592 return iocbs;
593}
594
595/**
596 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
597 * IOCB types.
598 *
599 * @sp: SRB command to process
600 * @cmd_pkt: Command type 3 IOCB
601 * @tot_dsds: Total number of segments to transfer
602 */
603static inline void
604qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
605 uint16_t tot_dsds)
606{
607 uint16_t avail_dsds;
608 uint32_t *cur_dsd;
609 scsi_qla_host_t *ha;
610 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900611 struct scatterlist *sg;
612 int i;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700613
614 cmd = sp->cmd;
615
616 /* Update entry type to indicate Command Type 3 IOCB */
617 *((uint32_t *)(&cmd_pkt->entry_type)) =
618 __constant_cpu_to_le32(COMMAND_TYPE_7);
619
620 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900621 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700622 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
623 return;
624 }
625
626 ha = sp->ha;
627
628 /* Set transfer direction */
629 if (cmd->sc_data_direction == DMA_TO_DEVICE)
630 cmd_pkt->task_mgmt_flags =
631 __constant_cpu_to_le16(TMF_WRITE_DATA);
632 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
633 cmd_pkt->task_mgmt_flags =
634 __constant_cpu_to_le16(TMF_READ_DATA);
635
636 /* One DSD is available in the Command Type 3 IOCB */
637 avail_dsds = 1;
638 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
639
640 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700641
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900642 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
643 dma_addr_t sle_dma;
644 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700645
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900646 /* Allocate additional continuation packets? */
647 if (avail_dsds == 0) {
648 /*
649 * Five DSDs are available in the Continuation
650 * Type 1 IOCB.
651 */
652 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
653 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
654 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700655 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900656
657 sle_dma = sg_dma_address(sg);
658 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
659 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
660 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
661 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700662 }
663}
664
665
666/**
667 * qla24xx_start_scsi() - Send a SCSI command to the ISP
668 * @sp: command to send to the ISP
669 *
670 * Returns non-zero if a failure occured, else zero.
671 */
672int
673qla24xx_start_scsi(srb_t *sp)
674{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900675 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700676 unsigned long flags;
Seokmann Ju246de422008-07-10 16:55:55 -0700677 scsi_qla_host_t *ha, *pha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700678 struct scsi_cmnd *cmd;
679 uint32_t *clr_ptr;
680 uint32_t index;
681 uint32_t handle;
682 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700683 uint16_t cnt;
684 uint16_t req_cnt;
685 uint16_t tot_dsds;
Linus Torvaldsdb776a12005-07-26 14:50:02 -0700686 struct device_reg_24xx __iomem *reg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700687
688 /* Setup device pointers. */
689 ret = 0;
690 ha = sp->ha;
Seokmann Ju246de422008-07-10 16:55:55 -0700691 pha = to_qla_parent(ha);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700692 reg = &ha->iobase->isp24;
693 cmd = sp->cmd;
694 /* So we know we haven't pci_map'ed anything yet */
695 tot_dsds = 0;
696
697 /* Send marker if required */
698 if (ha->marker_needed != 0) {
699 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
700 return QLA_FUNCTION_FAILED;
701 }
702 ha->marker_needed = 0;
703 }
704
705 /* Acquire ring specific lock */
Seokmann Ju246de422008-07-10 16:55:55 -0700706 spin_lock_irqsave(&pha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700707
708 /* Check for room in outstanding command list. */
709 handle = ha->current_outstanding_cmd;
710 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
711 handle++;
712 if (handle == MAX_OUTSTANDING_COMMANDS)
713 handle = 1;
Andrew Vasquez700ca0e2007-09-20 14:07:46 -0700714 if (!ha->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700715 break;
716 }
717 if (index == MAX_OUTSTANDING_COMMANDS)
718 goto queuing_error;
719
720 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700721 if (scsi_sg_count(cmd)) {
722 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
723 scsi_sg_count(cmd), cmd->sc_data_direction);
724 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700725 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700726 } else
727 nseg = 0;
728
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900729 tot_dsds = nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700730
731 req_cnt = qla24xx_calc_iocbs(tot_dsds);
732 if (ha->req_q_cnt < (req_cnt + 2)) {
733 cnt = (uint16_t)RD_REG_DWORD_RELAXED(&reg->req_q_out);
734 if (ha->req_ring_index < cnt)
735 ha->req_q_cnt = cnt - ha->req_ring_index;
736 else
737 ha->req_q_cnt = ha->request_q_length -
738 (ha->req_ring_index - cnt);
739 }
Andrew Vasquez131736d2005-08-26 19:09:20 -0700740 if (ha->req_q_cnt < (req_cnt + 2))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700741 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700742
743 /* Build command packet. */
744 ha->current_outstanding_cmd = handle;
745 ha->outstanding_cmds[handle] = sp;
746 sp->ha = ha;
747 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
748 ha->req_q_cnt -= req_cnt;
749
750 cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
751 cmd_pkt->handle = handle;
752
753 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -0500754 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700755 clr_ptr = (uint32_t *)cmd_pkt + 2;
756 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
757 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
758
759 /* Set NPORT-ID and LUN number*/
760 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
761 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
762 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
763 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700764 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700765
Andrew Vasquez661c3f62005-10-27 11:09:58 -0700766 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -0800767 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700768
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700769 /* Load SCSI command packet. */
770 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
771 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
772
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900773 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700774
775 /* Build IOCB segments */
776 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
777
778 /* Set total data segment count. */
779 cmd_pkt->entry_count = (uint8_t)req_cnt;
780 wmb();
781
782 /* Adjust ring index. */
783 ha->req_ring_index++;
784 if (ha->req_ring_index == ha->request_q_length) {
785 ha->req_ring_index = 0;
786 ha->request_ring_ptr = ha->request_ring;
787 } else
788 ha->request_ring_ptr++;
789
790 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700791
792 /* Set chip new ring index. */
793 WRT_REG_DWORD(&reg->req_q_in, ha->req_ring_index);
794 RD_REG_DWORD_RELAXED(&reg->req_q_in); /* PCI Posting. */
795
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700796 /* Manage unprocessed RIO/ZIO commands in response queue. */
797 if (ha->flags.process_response_queue &&
798 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
799 qla24xx_process_response_queue(ha);
800
Seokmann Ju246de422008-07-10 16:55:55 -0700801 spin_unlock_irqrestore(&pha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700802 return QLA_SUCCESS;
803
804queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900805 if (tot_dsds)
806 scsi_dma_unmap(cmd);
807
Seokmann Ju246de422008-07-10 16:55:55 -0700808 spin_unlock_irqrestore(&pha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700809
810 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}