]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/qla2xxx/qla_iocb.c
[SCSI] qla2xxx: suppress uninitialized-var warning
[net-next-2.6.git] / drivers / scsi / qla2xxx / qla_iocb.c
CommitLineData
fa90c54f
AV
1/*
2 * QLogic Fibre Channel HBA Driver
01e58d8e 3 * Copyright (c) 2003-2008 QLogic Corporation
1da177e4 4 *
fa90c54f
AV
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
1da177e4
LT
7#include "qla_def.h"
8
9#include <linux/blkdev.h>
10#include <linux/delay.h>
11
12#include <scsi/scsi_tcq.h>
13
1da177e4 14static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
413975a0 15static void qla2x00_isp_cmd(scsi_qla_host_t *ha);
1da177e4
LT
16
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;
385d70b4
FT
155 struct scatterlist *sg;
156 int i;
1da177e4
LT
157
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 */
385d70b4 165 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
166 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 */
385d70b4
FT
179 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
180 cont_entry_t *cont_pkt;
181
182 /* 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;
1da177e4 191 }
385d70b4
FT
192
193 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
194 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
195 avail_dsds--;
1da177e4
LT
196 }
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;
385d70b4
FT
214 struct scatterlist *sg;
215 int i;
1da177e4
LT
216
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 */
385d70b4 224 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
225 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 */
385d70b4
FT
238 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
239 dma_addr_t sle_dma;
240 cont_a64_entry_t *cont_pkt;
241
242 /* 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;
1da177e4 251 }
385d70b4
FT
252
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--;
1da177e4
LT
258 }
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{
385d70b4 270 int ret, nseg;
1da177e4 271 unsigned long flags;
246de42c 272 scsi_qla_host_t *ha, *pha;
1da177e4
LT
273 struct scsi_cmnd *cmd;
274 uint32_t *clr_ptr;
275 uint32_t index;
276 uint32_t handle;
277 cmd_entry_t *cmd_pkt;
1da177e4
LT
278 uint16_t cnt;
279 uint16_t req_cnt;
280 uint16_t tot_dsds;
3d71644c 281 struct device_reg_2xxx __iomem *reg;
1da177e4
LT
282
283 /* Setup device pointers. */
284 ret = 0;
bdf79621 285 ha = sp->ha;
246de42c 286 pha = to_qla_parent(ha);
3d71644c 287 reg = &ha->iobase->isp;
1da177e4 288 cmd = sp->cmd;
83021920
AV
289 /* So we know we haven't pci_map'ed anything yet */
290 tot_dsds = 0;
1da177e4
LT
291
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 */
246de42c 301 spin_lock_irqsave(&pha->hardware_lock, flags);
1da177e4
LT
302
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;
700ca0e7 309 if (!ha->outstanding_cmds[handle])
1da177e4
LT
310 break;
311 }
312 if (index == MAX_OUTSTANDING_COMMANDS)
313 goto queuing_error;
314
83021920 315 /* Map the sg table so we have an accurate count of sg entries needed */
2c3dfe3f
SJ
316 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
385d70b4 324 tot_dsds = nseg;
83021920 325
1da177e4 326 /* Calculate the number of request entries needed. */
fd34f556 327 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
1da177e4
LT
328 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
1da177e4
LT
339 /* 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
bdf79621
AV
353 /* 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);
1da177e4
LT
356
357 /* Update tagged queuing modifier */
358 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
1da177e4 359
1da177e4
LT
360 /* Load SCSI command packet. */
361 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
385d70b4 362 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1da177e4
LT
363
364 /* Build IOCB segments */
fd34f556 365 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
1da177e4
LT
366
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
1da177e4 379 sp->flags |= SRB_DMA_VALID;
1da177e4
LT
380
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
4fdfefe5
AV
385 /* 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
246de42c 390 spin_unlock_irqrestore(&pha->hardware_lock, flags);
1da177e4
LT
391 return (QLA_SUCCESS);
392
393queuing_error:
385d70b4
FT
394 if (tot_dsds)
395 scsi_dma_unmap(cmd);
396
246de42c 397 spin_unlock_irqrestore(&pha->hardware_lock, flags);
1da177e4
LT
398
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 */
fa2a1ce5 413int
1da177e4
LT
414__qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
415 uint8_t type)
416{
2b6c0cee
AV
417 mrk_entry_t *mrk;
418 struct mrk_entry_24xx *mrk24;
2c3dfe3f 419 scsi_qla_host_t *pha = to_qla_parent(ha);
1da177e4 420
2b6c0cee 421 mrk24 = NULL;
2c3dfe3f 422 mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
2b6c0cee
AV
423 if (mrk == NULL) {
424 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
425 __func__, ha->host_no));
1da177e4
LT
426
427 return (QLA_FUNCTION_FAILED);
428 }
429
2b6c0cee
AV
430 mrk->entry_type = MARKER_TYPE;
431 mrk->modifier = type;
1da177e4 432 if (type != MK_SYNC_ALL) {
e428924c 433 if (IS_FWI2_CAPABLE(ha)) {
2b6c0cee
AV
434 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);
b797b6de 438 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
2c3dfe3f 439 mrk24->vp_index = ha->vp_idx;
2b6c0cee
AV
440 } else {
441 SET_TARGET_ID(ha, mrk->target, loop_id);
442 mrk->lun = cpu_to_le16(lun);
443 }
1da177e4
LT
444 }
445 wmb();
446
2c3dfe3f 447 qla2x00_isp_cmd(pha);
1da177e4
LT
448
449 return (QLA_SUCCESS);
450}
451
fa2a1ce5 452int
1da177e4
LT
453qla2x00_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;
246de42c 458 scsi_qla_host_t *pha = to_qla_parent(ha);
1da177e4 459
246de42c 460 spin_lock_irqsave(&pha->hardware_lock, flags);
1da177e4 461 ret = __qla2x00_marker(ha, loop_id, lun, type);
246de42c 462 spin_unlock_irqrestore(&pha->hardware_lock, flags);
1da177e4
LT
463
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{
2b6c0cee 478 device_reg_t __iomem *reg = ha->iobase;
1da177e4
LT
479 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. */
e428924c 489 if (IS_FWI2_CAPABLE(ha))
2b6c0cee
AV
490 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));
1da177e4
LT
495 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 */
a3a63d55 527 if (!ha->marker_needed && !ha->flags.init_done)
1da177e4
LT
528 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 */
413975a0 545static void
1da177e4
LT
546qla2x00_isp_cmd(scsi_qla_host_t *ha)
547{
2b6c0cee 548 device_reg_t __iomem *reg = ha->iobase;
1da177e4
LT
549
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. */
e428924c 563 if (IS_FWI2_CAPABLE(ha)) {
2b6c0cee
AV
564 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;
385d70b4
FT
611 struct scatterlist *sg;
612 int i;
2b6c0cee
AV
613
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 */
385d70b4 621 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
2b6c0cee
AV
622 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 */
385d70b4
FT
641
642 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
643 dma_addr_t sle_dma;
644 cont_a64_entry_t *cont_pkt;
645
646 /* 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;
2b6c0cee 655 }
385d70b4
FT
656
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--;
2b6c0cee
AV
662 }
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{
385d70b4 675 int ret, nseg;
2b6c0cee 676 unsigned long flags;
246de42c 677 scsi_qla_host_t *ha, *pha;
2b6c0cee
AV
678 struct scsi_cmnd *cmd;
679 uint32_t *clr_ptr;
680 uint32_t index;
681 uint32_t handle;
682 struct cmd_type_7 *cmd_pkt;
2b6c0cee
AV
683 uint16_t cnt;
684 uint16_t req_cnt;
685 uint16_t tot_dsds;
db776a14 686 struct device_reg_24xx __iomem *reg;
2b6c0cee
AV
687
688 /* Setup device pointers. */
689 ret = 0;
690 ha = sp->ha;
246de42c 691 pha = to_qla_parent(ha);
2b6c0cee
AV
692 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 */
246de42c 706 spin_lock_irqsave(&pha->hardware_lock, flags);
2b6c0cee
AV
707
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;
700ca0e7 714 if (!ha->outstanding_cmds[handle])
2b6c0cee
AV
715 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 */
2c3dfe3f
SJ
721 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))
2b6c0cee 725 goto queuing_error;
2c3dfe3f
SJ
726 } else
727 nseg = 0;
728
385d70b4 729 tot_dsds = nseg;
2b6c0cee
AV
730
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 }
131736d3 740 if (ha->req_q_cnt < (req_cnt + 2))
2b6c0cee 741 goto queuing_error;
2b6c0cee
AV
742
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. */
72df8325 754 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2b6c0cee
AV
755 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;
2c3dfe3f 764 cmd_pkt->vp_index = sp->fcport->vp_idx;
2b6c0cee 765
661c3f6c 766 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
0d4be124 767 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2b6c0cee 768
2b6c0cee
AV
769 /* 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
385d70b4 773 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2b6c0cee
AV
774
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;
2b6c0cee
AV
791
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
4fdfefe5
AV
796 /* 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
246de42c 801 spin_unlock_irqrestore(&pha->hardware_lock, flags);
2b6c0cee
AV
802 return QLA_SUCCESS;
803
804queuing_error:
385d70b4
FT
805 if (tot_dsds)
806 scsi_dma_unmap(cmd);
807
246de42c 808 spin_unlock_irqrestore(&pha->hardware_lock, flags);
2b6c0cee
AV
809
810 return QLA_FUNCTION_FAILED;
1da177e4 811}