[SERIAL] Ensure 8250_pci quirks are not marked __devinit
[linux-2.6/verdex.git] / drivers / scsi / qla2xxx / qla_iocb.c
blob8f0f4a298357c48d516160a6db822c5eb5789f73
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
9 #include <linux/blkdev.h>
10 #include <linux/delay.h>
12 #include <scsi/scsi_tcq.h>
14 static inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd *cmd);
15 static inline cont_entry_t *qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *);
16 static inline cont_a64_entry_t *qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *);
17 static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
19 /**
20 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
21 * @cmd: SCSI command
23 * Returns the proper CF_* direction based on CDB.
25 static inline uint16_t
26 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
28 uint16_t cflags;
30 cflags = 0;
32 /* Set transfer direction */
33 if (cmd->sc_data_direction == DMA_TO_DEVICE)
34 cflags = CF_WRITE;
35 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
36 cflags = CF_READ;
37 return (cflags);
40 /**
41 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
42 * Continuation Type 0 IOCBs to allocate.
44 * @dsds: number of data segment decriptors needed
46 * Returns the number of IOCB entries needed to store @dsds.
48 uint16_t
49 qla2x00_calc_iocbs_32(uint16_t dsds)
51 uint16_t iocbs;
53 iocbs = 1;
54 if (dsds > 3) {
55 iocbs += (dsds - 3) / 7;
56 if ((dsds - 3) % 7)
57 iocbs++;
59 return (iocbs);
62 /**
63 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
64 * Continuation Type 1 IOCBs to allocate.
66 * @dsds: number of data segment decriptors needed
68 * Returns the number of IOCB entries needed to store @dsds.
70 uint16_t
71 qla2x00_calc_iocbs_64(uint16_t dsds)
73 uint16_t iocbs;
75 iocbs = 1;
76 if (dsds > 2) {
77 iocbs += (dsds - 2) / 5;
78 if ((dsds - 2) % 5)
79 iocbs++;
81 return (iocbs);
84 /**
85 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
86 * @ha: HA context
88 * Returns a pointer to the Continuation Type 0 IOCB packet.
90 static inline cont_entry_t *
91 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
93 cont_entry_t *cont_pkt;
95 /* Adjust ring index. */
96 ha->req_ring_index++;
97 if (ha->req_ring_index == ha->request_q_length) {
98 ha->req_ring_index = 0;
99 ha->request_ring_ptr = ha->request_ring;
100 } else {
101 ha->request_ring_ptr++;
104 cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
106 /* Load packet defaults. */
107 *((uint32_t *)(&cont_pkt->entry_type)) =
108 __constant_cpu_to_le32(CONTINUE_TYPE);
110 return (cont_pkt);
114 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
115 * @ha: HA context
117 * Returns a pointer to the continuation type 1 IOCB packet.
119 static inline cont_a64_entry_t *
120 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
122 cont_a64_entry_t *cont_pkt;
124 /* Adjust ring index. */
125 ha->req_ring_index++;
126 if (ha->req_ring_index == ha->request_q_length) {
127 ha->req_ring_index = 0;
128 ha->request_ring_ptr = ha->request_ring;
129 } else {
130 ha->request_ring_ptr++;
133 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
135 /* Load packet defaults. */
136 *((uint32_t *)(&cont_pkt->entry_type)) =
137 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
139 return (cont_pkt);
143 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
144 * capable IOCB types.
146 * @sp: SRB command to process
147 * @cmd_pkt: Command type 2 IOCB
148 * @tot_dsds: Total number of segments to transfer
150 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
151 uint16_t tot_dsds)
153 uint16_t avail_dsds;
154 uint32_t *cur_dsd;
155 scsi_qla_host_t *ha;
156 struct scsi_cmnd *cmd;
158 cmd = sp->cmd;
160 /* Update entry type to indicate Command Type 2 IOCB */
161 *((uint32_t *)(&cmd_pkt->entry_type)) =
162 __constant_cpu_to_le32(COMMAND_TYPE);
164 /* No data transfer */
165 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
166 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
167 return;
170 ha = sp->ha;
172 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
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;
178 /* Load data segments */
179 if (cmd->use_sg != 0) {
180 struct scatterlist *cur_seg;
181 struct scatterlist *end_seg;
183 cur_seg = (struct scatterlist *)cmd->request_buffer;
184 end_seg = cur_seg + tot_dsds;
185 while (cur_seg < end_seg) {
186 cont_entry_t *cont_pkt;
188 /* Allocate additional continuation packets? */
189 if (avail_dsds == 0) {
191 * Seven DSDs are available in the Continuation
192 * Type 0 IOCB.
194 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
195 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
196 avail_dsds = 7;
199 *cur_dsd++ = cpu_to_le32(sg_dma_address(cur_seg));
200 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
201 avail_dsds--;
203 cur_seg++;
205 } else {
206 *cur_dsd++ = cpu_to_le32(sp->dma_handle);
207 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
212 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
213 * capable IOCB types.
215 * @sp: SRB command to process
216 * @cmd_pkt: Command type 3 IOCB
217 * @tot_dsds: Total number of segments to transfer
219 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
220 uint16_t tot_dsds)
222 uint16_t avail_dsds;
223 uint32_t *cur_dsd;
224 scsi_qla_host_t *ha;
225 struct scsi_cmnd *cmd;
227 cmd = sp->cmd;
229 /* Update entry type to indicate Command Type 3 IOCB */
230 *((uint32_t *)(&cmd_pkt->entry_type)) =
231 __constant_cpu_to_le32(COMMAND_A64_TYPE);
233 /* No data transfer */
234 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
235 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
236 return;
239 ha = sp->ha;
241 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
243 /* Two DSDs are available in the Command Type 3 IOCB */
244 avail_dsds = 2;
245 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
247 /* Load data segments */
248 if (cmd->use_sg != 0) {
249 struct scatterlist *cur_seg;
250 struct scatterlist *end_seg;
252 cur_seg = (struct scatterlist *)cmd->request_buffer;
253 end_seg = cur_seg + tot_dsds;
254 while (cur_seg < end_seg) {
255 dma_addr_t sle_dma;
256 cont_a64_entry_t *cont_pkt;
258 /* Allocate additional continuation packets? */
259 if (avail_dsds == 0) {
261 * Five DSDs are available in the Continuation
262 * Type 1 IOCB.
264 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
265 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
266 avail_dsds = 5;
269 sle_dma = sg_dma_address(cur_seg);
270 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
271 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
272 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
273 avail_dsds--;
275 cur_seg++;
277 } else {
278 *cur_dsd++ = cpu_to_le32(LSD(sp->dma_handle));
279 *cur_dsd++ = cpu_to_le32(MSD(sp->dma_handle));
280 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
285 * qla2x00_start_scsi() - Send a SCSI command to the ISP
286 * @sp: command to send to the ISP
288 * Returns non-zero if a failure occured, else zero.
291 qla2x00_start_scsi(srb_t *sp)
293 int ret;
294 unsigned long flags;
295 scsi_qla_host_t *ha;
296 struct scsi_cmnd *cmd;
297 uint32_t *clr_ptr;
298 uint32_t index;
299 uint32_t handle;
300 cmd_entry_t *cmd_pkt;
301 struct scatterlist *sg;
302 uint16_t cnt;
303 uint16_t req_cnt;
304 uint16_t tot_dsds;
305 struct device_reg_2xxx __iomem *reg;
307 /* Setup device pointers. */
308 ret = 0;
309 ha = sp->ha;
310 reg = &ha->iobase->isp;
311 cmd = sp->cmd;
312 /* So we know we haven't pci_map'ed anything yet */
313 tot_dsds = 0;
315 /* Send marker if required */
316 if (ha->marker_needed != 0) {
317 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
318 return (QLA_FUNCTION_FAILED);
320 ha->marker_needed = 0;
323 /* Acquire ring specific lock */
324 spin_lock_irqsave(&ha->hardware_lock, flags);
326 /* Check for room in outstanding command list. */
327 handle = ha->current_outstanding_cmd;
328 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
329 handle++;
330 if (handle == MAX_OUTSTANDING_COMMANDS)
331 handle = 1;
332 if (ha->outstanding_cmds[handle] == 0)
333 break;
335 if (index == MAX_OUTSTANDING_COMMANDS)
336 goto queuing_error;
338 /* Map the sg table so we have an accurate count of sg entries needed */
339 if (cmd->use_sg) {
340 sg = (struct scatterlist *) cmd->request_buffer;
341 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
342 cmd->sc_data_direction);
343 if (tot_dsds == 0)
344 goto queuing_error;
345 } else if (cmd->request_bufflen) {
346 dma_addr_t req_dma;
348 req_dma = pci_map_single(ha->pdev, cmd->request_buffer,
349 cmd->request_bufflen, cmd->sc_data_direction);
350 if (dma_mapping_error(req_dma))
351 goto queuing_error;
353 sp->dma_handle = req_dma;
354 tot_dsds = 1;
357 /* Calculate the number of request entries needed. */
358 req_cnt = ha->isp_ops.calc_req_entries(tot_dsds);
359 if (ha->req_q_cnt < (req_cnt + 2)) {
360 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
361 if (ha->req_ring_index < cnt)
362 ha->req_q_cnt = cnt - ha->req_ring_index;
363 else
364 ha->req_q_cnt = ha->request_q_length -
365 (ha->req_ring_index - cnt);
367 if (ha->req_q_cnt < (req_cnt + 2))
368 goto queuing_error;
370 /* Build command packet */
371 ha->current_outstanding_cmd = handle;
372 ha->outstanding_cmds[handle] = sp;
373 sp->ha = ha;
374 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
375 ha->req_q_cnt -= req_cnt;
377 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
378 cmd_pkt->handle = handle;
379 /* Zero out remaining portion of packet. */
380 clr_ptr = (uint32_t *)cmd_pkt + 2;
381 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
382 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
384 /* Set target ID and LUN number*/
385 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
386 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
388 /* Update tagged queuing modifier */
389 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
391 /* Load SCSI command packet. */
392 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
393 cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
395 /* Build IOCB segments */
396 ha->isp_ops.build_iocbs(sp, cmd_pkt, tot_dsds);
398 /* Set total data segment count. */
399 cmd_pkt->entry_count = (uint8_t)req_cnt;
400 wmb();
402 /* Adjust ring index. */
403 ha->req_ring_index++;
404 if (ha->req_ring_index == ha->request_q_length) {
405 ha->req_ring_index = 0;
406 ha->request_ring_ptr = ha->request_ring;
407 } else
408 ha->request_ring_ptr++;
410 sp->flags |= SRB_DMA_VALID;
411 sp->state = SRB_ACTIVE_STATE;
413 /* Set chip new ring index. */
414 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
415 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
417 /* Manage unprocessed RIO/ZIO commands in response queue. */
418 if (ha->flags.process_response_queue &&
419 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
420 qla2x00_process_response_queue(ha);
422 spin_unlock_irqrestore(&ha->hardware_lock, flags);
423 return (QLA_SUCCESS);
425 queuing_error:
426 if (cmd->use_sg && tot_dsds) {
427 sg = (struct scatterlist *) cmd->request_buffer;
428 pci_unmap_sg(ha->pdev, sg, cmd->use_sg,
429 cmd->sc_data_direction);
430 } else if (tot_dsds) {
431 pci_unmap_single(ha->pdev, sp->dma_handle,
432 cmd->request_bufflen, cmd->sc_data_direction);
434 spin_unlock_irqrestore(&ha->hardware_lock, flags);
436 return (QLA_FUNCTION_FAILED);
440 * qla2x00_marker() - Send a marker IOCB to the firmware.
441 * @ha: HA context
442 * @loop_id: loop ID
443 * @lun: LUN
444 * @type: marker modifier
446 * Can be called from both normal and interrupt context.
448 * Returns non-zero if a failure occured, else zero.
451 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
452 uint8_t type)
454 mrk_entry_t *mrk;
455 struct mrk_entry_24xx *mrk24;
457 mrk24 = NULL;
458 mrk = (mrk_entry_t *)qla2x00_req_pkt(ha);
459 if (mrk == NULL) {
460 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
461 __func__, ha->host_no));
463 return (QLA_FUNCTION_FAILED);
466 mrk->entry_type = MARKER_TYPE;
467 mrk->modifier = type;
468 if (type != MK_SYNC_ALL) {
469 if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
470 mrk24 = (struct mrk_entry_24xx *) mrk;
471 mrk24->nport_handle = cpu_to_le16(loop_id);
472 mrk24->lun[1] = LSB(lun);
473 mrk24->lun[2] = MSB(lun);
474 } else {
475 SET_TARGET_ID(ha, mrk->target, loop_id);
476 mrk->lun = cpu_to_le16(lun);
479 wmb();
481 qla2x00_isp_cmd(ha);
483 return (QLA_SUCCESS);
487 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
488 uint8_t type)
490 int ret;
491 unsigned long flags = 0;
493 spin_lock_irqsave(&ha->hardware_lock, flags);
494 ret = __qla2x00_marker(ha, loop_id, lun, type);
495 spin_unlock_irqrestore(&ha->hardware_lock, flags);
497 return (ret);
501 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
502 * @ha: HA context
504 * Note: The caller must hold the hardware lock before calling this routine.
506 * Returns NULL if function failed, else, a pointer to the request packet.
508 static request_t *
509 qla2x00_req_pkt(scsi_qla_host_t *ha)
511 device_reg_t __iomem *reg = ha->iobase;
512 request_t *pkt = NULL;
513 uint16_t cnt;
514 uint32_t *dword_ptr;
515 uint32_t timer;
516 uint16_t req_cnt = 1;
518 /* Wait 1 second for slot. */
519 for (timer = HZ; timer; timer--) {
520 if ((req_cnt + 2) >= ha->req_q_cnt) {
521 /* Calculate number of free request entries. */
522 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
523 cnt = (uint16_t)RD_REG_DWORD(
524 &reg->isp24.req_q_out);
525 else
526 cnt = qla2x00_debounce_register(
527 ISP_REQ_Q_OUT(ha, &reg->isp));
528 if (ha->req_ring_index < cnt)
529 ha->req_q_cnt = cnt - ha->req_ring_index;
530 else
531 ha->req_q_cnt = ha->request_q_length -
532 (ha->req_ring_index - cnt);
534 /* If room for request in request ring. */
535 if ((req_cnt + 2) < ha->req_q_cnt) {
536 ha->req_q_cnt--;
537 pkt = ha->request_ring_ptr;
539 /* Zero out packet. */
540 dword_ptr = (uint32_t *)pkt;
541 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
542 *dword_ptr++ = 0;
544 /* Set system defined field. */
545 pkt->sys_define = (uint8_t)ha->req_ring_index;
547 /* Set entry count. */
548 pkt->entry_count = 1;
550 break;
553 /* Release ring specific lock */
554 spin_unlock(&ha->hardware_lock);
556 udelay(2); /* 2 us */
558 /* Check for pending interrupts. */
559 /* During init we issue marker directly */
560 if (!ha->marker_needed)
561 qla2x00_poll(ha);
563 spin_lock_irq(&ha->hardware_lock);
565 if (!pkt) {
566 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
569 return (pkt);
573 * qla2x00_isp_cmd() - Modify the request ring pointer.
574 * @ha: HA context
576 * Note: The caller must hold the hardware lock before calling this routine.
578 void
579 qla2x00_isp_cmd(scsi_qla_host_t *ha)
581 device_reg_t __iomem *reg = ha->iobase;
583 DEBUG5(printk("%s(): IOCB data:\n", __func__));
584 DEBUG5(qla2x00_dump_buffer(
585 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
587 /* Adjust ring index. */
588 ha->req_ring_index++;
589 if (ha->req_ring_index == ha->request_q_length) {
590 ha->req_ring_index = 0;
591 ha->request_ring_ptr = ha->request_ring;
592 } else
593 ha->request_ring_ptr++;
595 /* Set chip new ring index. */
596 if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
597 WRT_REG_DWORD(&reg->isp24.req_q_in, ha->req_ring_index);
598 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
599 } else {
600 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp), ha->req_ring_index);
601 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
607 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
608 * Continuation Type 1 IOCBs to allocate.
610 * @dsds: number of data segment decriptors needed
612 * Returns the number of IOCB entries needed to store @dsds.
614 static inline uint16_t
615 qla24xx_calc_iocbs(uint16_t dsds)
617 uint16_t iocbs;
619 iocbs = 1;
620 if (dsds > 1) {
621 iocbs += (dsds - 1) / 5;
622 if ((dsds - 1) % 5)
623 iocbs++;
625 return iocbs;
629 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
630 * IOCB types.
632 * @sp: SRB command to process
633 * @cmd_pkt: Command type 3 IOCB
634 * @tot_dsds: Total number of segments to transfer
636 static inline void
637 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
638 uint16_t tot_dsds)
640 uint16_t avail_dsds;
641 uint32_t *cur_dsd;
642 scsi_qla_host_t *ha;
643 struct scsi_cmnd *cmd;
645 cmd = sp->cmd;
647 /* Update entry type to indicate Command Type 3 IOCB */
648 *((uint32_t *)(&cmd_pkt->entry_type)) =
649 __constant_cpu_to_le32(COMMAND_TYPE_7);
651 /* No data transfer */
652 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
653 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
654 return;
657 ha = sp->ha;
659 /* Set transfer direction */
660 if (cmd->sc_data_direction == DMA_TO_DEVICE)
661 cmd_pkt->task_mgmt_flags =
662 __constant_cpu_to_le16(TMF_WRITE_DATA);
663 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
664 cmd_pkt->task_mgmt_flags =
665 __constant_cpu_to_le16(TMF_READ_DATA);
667 /* One DSD is available in the Command Type 3 IOCB */
668 avail_dsds = 1;
669 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
671 /* Load data segments */
672 if (cmd->use_sg != 0) {
673 struct scatterlist *cur_seg;
674 struct scatterlist *end_seg;
676 cur_seg = (struct scatterlist *)cmd->request_buffer;
677 end_seg = cur_seg + tot_dsds;
678 while (cur_seg < end_seg) {
679 dma_addr_t sle_dma;
680 cont_a64_entry_t *cont_pkt;
682 /* Allocate additional continuation packets? */
683 if (avail_dsds == 0) {
685 * Five DSDs are available in the Continuation
686 * Type 1 IOCB.
688 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
689 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
690 avail_dsds = 5;
693 sle_dma = sg_dma_address(cur_seg);
694 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
695 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
696 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
697 avail_dsds--;
699 cur_seg++;
701 } else {
702 *cur_dsd++ = cpu_to_le32(LSD(sp->dma_handle));
703 *cur_dsd++ = cpu_to_le32(MSD(sp->dma_handle));
704 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
710 * qla24xx_start_scsi() - Send a SCSI command to the ISP
711 * @sp: command to send to the ISP
713 * Returns non-zero if a failure occured, else zero.
716 qla24xx_start_scsi(srb_t *sp)
718 int ret;
719 unsigned long flags;
720 scsi_qla_host_t *ha;
721 struct scsi_cmnd *cmd;
722 uint32_t *clr_ptr;
723 uint32_t index;
724 uint32_t handle;
725 struct cmd_type_7 *cmd_pkt;
726 struct scatterlist *sg;
727 uint16_t cnt;
728 uint16_t req_cnt;
729 uint16_t tot_dsds;
730 struct device_reg_24xx __iomem *reg;
732 /* Setup device pointers. */
733 ret = 0;
734 ha = sp->ha;
735 reg = &ha->iobase->isp24;
736 cmd = sp->cmd;
737 /* So we know we haven't pci_map'ed anything yet */
738 tot_dsds = 0;
740 /* Send marker if required */
741 if (ha->marker_needed != 0) {
742 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
743 return QLA_FUNCTION_FAILED;
745 ha->marker_needed = 0;
748 /* Acquire ring specific lock */
749 spin_lock_irqsave(&ha->hardware_lock, flags);
751 /* Check for room in outstanding command list. */
752 handle = ha->current_outstanding_cmd;
753 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
754 handle++;
755 if (handle == MAX_OUTSTANDING_COMMANDS)
756 handle = 1;
757 if (ha->outstanding_cmds[handle] == 0)
758 break;
760 if (index == MAX_OUTSTANDING_COMMANDS)
761 goto queuing_error;
763 /* Map the sg table so we have an accurate count of sg entries needed */
764 if (cmd->use_sg) {
765 sg = (struct scatterlist *) cmd->request_buffer;
766 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
767 cmd->sc_data_direction);
768 if (tot_dsds == 0)
769 goto queuing_error;
770 } else if (cmd->request_bufflen) {
771 dma_addr_t req_dma;
773 req_dma = pci_map_single(ha->pdev, cmd->request_buffer,
774 cmd->request_bufflen, cmd->sc_data_direction);
775 if (dma_mapping_error(req_dma))
776 goto queuing_error;
778 sp->dma_handle = req_dma;
779 tot_dsds = 1;
782 req_cnt = qla24xx_calc_iocbs(tot_dsds);
783 if (ha->req_q_cnt < (req_cnt + 2)) {
784 cnt = (uint16_t)RD_REG_DWORD_RELAXED(&reg->req_q_out);
785 if (ha->req_ring_index < cnt)
786 ha->req_q_cnt = cnt - ha->req_ring_index;
787 else
788 ha->req_q_cnt = ha->request_q_length -
789 (ha->req_ring_index - cnt);
791 if (ha->req_q_cnt < (req_cnt + 2))
792 goto queuing_error;
794 /* Build command packet. */
795 ha->current_outstanding_cmd = handle;
796 ha->outstanding_cmds[handle] = sp;
797 sp->ha = ha;
798 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
799 ha->req_q_cnt -= req_cnt;
801 cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
802 cmd_pkt->handle = handle;
804 /* Zero out remaining portion of packet. */
805 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
806 clr_ptr = (uint32_t *)cmd_pkt + 2;
807 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
808 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
810 /* Set NPORT-ID and LUN number*/
811 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
812 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
813 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
814 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
816 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
817 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
819 /* Load SCSI command packet. */
820 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
821 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
823 cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
825 /* Build IOCB segments */
826 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
828 /* Set total data segment count. */
829 cmd_pkt->entry_count = (uint8_t)req_cnt;
830 wmb();
832 /* Adjust ring index. */
833 ha->req_ring_index++;
834 if (ha->req_ring_index == ha->request_q_length) {
835 ha->req_ring_index = 0;
836 ha->request_ring_ptr = ha->request_ring;
837 } else
838 ha->request_ring_ptr++;
840 sp->flags |= SRB_DMA_VALID;
841 sp->state = SRB_ACTIVE_STATE;
843 /* Set chip new ring index. */
844 WRT_REG_DWORD(&reg->req_q_in, ha->req_ring_index);
845 RD_REG_DWORD_RELAXED(&reg->req_q_in); /* PCI Posting. */
847 /* Manage unprocessed RIO/ZIO commands in response queue. */
848 if (ha->flags.process_response_queue &&
849 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
850 qla24xx_process_response_queue(ha);
852 spin_unlock_irqrestore(&ha->hardware_lock, flags);
853 return QLA_SUCCESS;
855 queuing_error:
856 if (cmd->use_sg && tot_dsds) {
857 sg = (struct scatterlist *) cmd->request_buffer;
858 pci_unmap_sg(ha->pdev, sg, cmd->use_sg,
859 cmd->sc_data_direction);
860 } else if (tot_dsds) {
861 pci_unmap_single(ha->pdev, sp->dma_handle,
862 cmd->request_bufflen, cmd->sc_data_direction);
864 spin_unlock_irqrestore(&ha->hardware_lock, flags);
866 return QLA_FUNCTION_FAILED;