Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/gregkh/driver...
[linux-2.6/verdex.git] / drivers / scsi / qla2xxx / qla_iocb.c
blobaf964bb3d87052c3f61807344f79135339471b9d
1 /******************************************************************************
2 * QLOGIC LINUX SOFTWARE
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2004 QLogic Corporation
6 * (www.qlogic.com)
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 ******************************************************************************/
20 #include "qla_def.h"
22 #include <linux/blkdev.h>
23 #include <linux/delay.h>
25 #include <scsi/scsi_tcq.h>
27 static inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd *cmd);
28 static inline cont_entry_t *qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *);
29 static inline cont_a64_entry_t *qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *);
30 static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
32 /**
33 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
34 * @cmd: SCSI command
36 * Returns the proper CF_* direction based on CDB.
38 static inline uint16_t
39 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
41 uint16_t cflags;
43 cflags = 0;
45 /* Set transfer direction */
46 if (cmd->sc_data_direction == DMA_TO_DEVICE)
47 cflags = CF_WRITE;
48 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
49 cflags = CF_READ;
50 return (cflags);
53 /**
54 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
55 * Continuation Type 0 IOCBs to allocate.
57 * @dsds: number of data segment decriptors needed
59 * Returns the number of IOCB entries needed to store @dsds.
61 uint16_t
62 qla2x00_calc_iocbs_32(uint16_t dsds)
64 uint16_t iocbs;
66 iocbs = 1;
67 if (dsds > 3) {
68 iocbs += (dsds - 3) / 7;
69 if ((dsds - 3) % 7)
70 iocbs++;
72 return (iocbs);
75 /**
76 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
77 * Continuation Type 1 IOCBs to allocate.
79 * @dsds: number of data segment decriptors needed
81 * Returns the number of IOCB entries needed to store @dsds.
83 uint16_t
84 qla2x00_calc_iocbs_64(uint16_t dsds)
86 uint16_t iocbs;
88 iocbs = 1;
89 if (dsds > 2) {
90 iocbs += (dsds - 2) / 5;
91 if ((dsds - 2) % 5)
92 iocbs++;
94 return (iocbs);
97 /**
98 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
99 * @ha: HA context
101 * Returns a pointer to the Continuation Type 0 IOCB packet.
103 static inline cont_entry_t *
104 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
106 cont_entry_t *cont_pkt;
108 /* Adjust ring index. */
109 ha->req_ring_index++;
110 if (ha->req_ring_index == ha->request_q_length) {
111 ha->req_ring_index = 0;
112 ha->request_ring_ptr = ha->request_ring;
113 } else {
114 ha->request_ring_ptr++;
117 cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
119 /* Load packet defaults. */
120 *((uint32_t *)(&cont_pkt->entry_type)) =
121 __constant_cpu_to_le32(CONTINUE_TYPE);
123 return (cont_pkt);
127 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
128 * @ha: HA context
130 * Returns a pointer to the continuation type 1 IOCB packet.
132 static inline cont_a64_entry_t *
133 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
135 cont_a64_entry_t *cont_pkt;
137 /* Adjust ring index. */
138 ha->req_ring_index++;
139 if (ha->req_ring_index == ha->request_q_length) {
140 ha->req_ring_index = 0;
141 ha->request_ring_ptr = ha->request_ring;
142 } else {
143 ha->request_ring_ptr++;
146 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
148 /* Load packet defaults. */
149 *((uint32_t *)(&cont_pkt->entry_type)) =
150 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
152 return (cont_pkt);
156 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
157 * capable IOCB types.
159 * @sp: SRB command to process
160 * @cmd_pkt: Command type 2 IOCB
161 * @tot_dsds: Total number of segments to transfer
163 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
164 uint16_t tot_dsds)
166 uint16_t avail_dsds;
167 uint32_t *cur_dsd;
168 scsi_qla_host_t *ha;
169 struct scsi_cmnd *cmd;
171 cmd = sp->cmd;
173 /* Update entry type to indicate Command Type 2 IOCB */
174 *((uint32_t *)(&cmd_pkt->entry_type)) =
175 __constant_cpu_to_le32(COMMAND_TYPE);
177 /* No data transfer */
178 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
179 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
180 return;
183 ha = sp->ha;
185 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
187 /* Three DSDs are available in the Command Type 2 IOCB */
188 avail_dsds = 3;
189 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
191 /* Load data segments */
192 if (cmd->use_sg != 0) {
193 struct scatterlist *cur_seg;
194 struct scatterlist *end_seg;
196 cur_seg = (struct scatterlist *)cmd->request_buffer;
197 end_seg = cur_seg + tot_dsds;
198 while (cur_seg < end_seg) {
199 cont_entry_t *cont_pkt;
201 /* Allocate additional continuation packets? */
202 if (avail_dsds == 0) {
204 * Seven DSDs are available in the Continuation
205 * Type 0 IOCB.
207 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
208 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
209 avail_dsds = 7;
212 *cur_dsd++ = cpu_to_le32(sg_dma_address(cur_seg));
213 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
214 avail_dsds--;
216 cur_seg++;
218 } else {
219 *cur_dsd++ = cpu_to_le32(sp->dma_handle);
220 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
225 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
226 * capable IOCB types.
228 * @sp: SRB command to process
229 * @cmd_pkt: Command type 3 IOCB
230 * @tot_dsds: Total number of segments to transfer
232 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
233 uint16_t tot_dsds)
235 uint16_t avail_dsds;
236 uint32_t *cur_dsd;
237 scsi_qla_host_t *ha;
238 struct scsi_cmnd *cmd;
240 cmd = sp->cmd;
242 /* Update entry type to indicate Command Type 3 IOCB */
243 *((uint32_t *)(&cmd_pkt->entry_type)) =
244 __constant_cpu_to_le32(COMMAND_A64_TYPE);
246 /* No data transfer */
247 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
248 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
249 return;
252 ha = sp->ha;
254 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
256 /* Two DSDs are available in the Command Type 3 IOCB */
257 avail_dsds = 2;
258 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
260 /* Load data segments */
261 if (cmd->use_sg != 0) {
262 struct scatterlist *cur_seg;
263 struct scatterlist *end_seg;
265 cur_seg = (struct scatterlist *)cmd->request_buffer;
266 end_seg = cur_seg + tot_dsds;
267 while (cur_seg < end_seg) {
268 dma_addr_t sle_dma;
269 cont_a64_entry_t *cont_pkt;
271 /* Allocate additional continuation packets? */
272 if (avail_dsds == 0) {
274 * Five DSDs are available in the Continuation
275 * Type 1 IOCB.
277 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
278 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
279 avail_dsds = 5;
282 sle_dma = sg_dma_address(cur_seg);
283 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
284 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
285 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
286 avail_dsds--;
288 cur_seg++;
290 } else {
291 *cur_dsd++ = cpu_to_le32(LSD(sp->dma_handle));
292 *cur_dsd++ = cpu_to_le32(MSD(sp->dma_handle));
293 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
298 * qla2x00_start_scsi() - Send a SCSI command to the ISP
299 * @sp: command to send to the ISP
301 * Returns non-zero if a failure occured, else zero.
304 qla2x00_start_scsi(srb_t *sp)
306 int ret;
307 unsigned long flags;
308 scsi_qla_host_t *ha;
309 struct scsi_cmnd *cmd;
310 uint32_t *clr_ptr;
311 uint32_t index;
312 uint32_t handle;
313 cmd_entry_t *cmd_pkt;
314 struct scatterlist *sg;
315 uint16_t cnt;
316 uint16_t req_cnt;
317 uint16_t tot_dsds;
318 device_reg_t __iomem *reg;
319 char tag[2];
321 /* Setup device pointers. */
322 ret = 0;
323 ha = sp->ha;
324 reg = ha->iobase;
325 cmd = sp->cmd;
326 /* So we know we haven't pci_map'ed anything yet */
327 tot_dsds = 0;
329 /* Send marker if required */
330 if (ha->marker_needed != 0) {
331 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
332 return (QLA_FUNCTION_FAILED);
334 ha->marker_needed = 0;
337 /* Acquire ring specific lock */
338 spin_lock_irqsave(&ha->hardware_lock, flags);
340 /* Check for room in outstanding command list. */
341 handle = ha->current_outstanding_cmd;
342 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
343 handle++;
344 if (handle == MAX_OUTSTANDING_COMMANDS)
345 handle = 1;
346 if (ha->outstanding_cmds[handle] == 0)
347 break;
349 if (index == MAX_OUTSTANDING_COMMANDS)
350 goto queuing_error;
352 /* Map the sg table so we have an accurate count of sg entries needed */
353 if (cmd->use_sg) {
354 sg = (struct scatterlist *) cmd->request_buffer;
355 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
356 cmd->sc_data_direction);
357 if (tot_dsds == 0)
358 goto queuing_error;
359 } else if (cmd->request_bufflen) {
360 dma_addr_t req_dma;
362 req_dma = pci_map_single(ha->pdev, cmd->request_buffer,
363 cmd->request_bufflen, cmd->sc_data_direction);
364 if (dma_mapping_error(req_dma))
365 goto queuing_error;
367 sp->dma_handle = req_dma;
368 tot_dsds = 1;
371 /* Calculate the number of request entries needed. */
372 req_cnt = (ha->calc_request_entries)(tot_dsds);
373 if (ha->req_q_cnt < (req_cnt + 2)) {
374 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
375 if (ha->req_ring_index < cnt)
376 ha->req_q_cnt = cnt - ha->req_ring_index;
377 else
378 ha->req_q_cnt = ha->request_q_length -
379 (ha->req_ring_index - cnt);
381 if (ha->req_q_cnt < (req_cnt + 2))
382 goto queuing_error;
384 /* Build command packet */
385 ha->current_outstanding_cmd = handle;
386 ha->outstanding_cmds[handle] = sp;
387 sp->ha = ha;
388 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
389 ha->req_q_cnt -= req_cnt;
391 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
392 cmd_pkt->handle = handle;
393 /* Zero out remaining portion of packet. */
394 clr_ptr = (uint32_t *)cmd_pkt + 2;
395 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
396 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
398 /* Set target ID and LUN number*/
399 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
400 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
402 /* Update tagged queuing modifier */
403 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
404 if (scsi_populate_tag_msg(cmd, tag)) {
405 switch (tag[0]) {
406 case MSG_HEAD_TAG:
407 cmd_pkt->control_flags =
408 __constant_cpu_to_le16(CF_HEAD_TAG);
409 break;
410 case MSG_ORDERED_TAG:
411 cmd_pkt->control_flags =
412 __constant_cpu_to_le16(CF_ORDERED_TAG);
413 break;
417 /* Load SCSI command packet. */
418 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
419 cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
421 /* Build IOCB segments */
422 (ha->build_scsi_iocbs)(sp, cmd_pkt, tot_dsds);
424 /* Set total data segment count. */
425 cmd_pkt->entry_count = (uint8_t)req_cnt;
426 wmb();
428 /* Adjust ring index. */
429 ha->req_ring_index++;
430 if (ha->req_ring_index == ha->request_q_length) {
431 ha->req_ring_index = 0;
432 ha->request_ring_ptr = ha->request_ring;
433 } else
434 ha->request_ring_ptr++;
436 ha->actthreads++;
437 ha->total_ios++;
438 sp->flags |= SRB_DMA_VALID;
439 sp->state = SRB_ACTIVE_STATE;
440 sp->u_start = jiffies;
442 /* Set chip new ring index. */
443 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
444 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
446 spin_unlock_irqrestore(&ha->hardware_lock, flags);
447 return (QLA_SUCCESS);
449 queuing_error:
450 if (cmd->use_sg && tot_dsds) {
451 sg = (struct scatterlist *) cmd->request_buffer;
452 pci_unmap_sg(ha->pdev, sg, cmd->use_sg,
453 cmd->sc_data_direction);
454 } else if (tot_dsds) {
455 pci_unmap_single(ha->pdev, sp->dma_handle,
456 cmd->request_bufflen, cmd->sc_data_direction);
458 spin_unlock_irqrestore(&ha->hardware_lock, flags);
460 return (QLA_FUNCTION_FAILED);
464 * qla2x00_marker() - Send a marker IOCB to the firmware.
465 * @ha: HA context
466 * @loop_id: loop ID
467 * @lun: LUN
468 * @type: marker modifier
470 * Can be called from both normal and interrupt context.
472 * Returns non-zero if a failure occured, else zero.
475 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
476 uint8_t type)
478 mrk_entry_t *pkt;
480 pkt = (mrk_entry_t *)qla2x00_req_pkt(ha);
481 if (pkt == NULL) {
482 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
484 return (QLA_FUNCTION_FAILED);
487 pkt->entry_type = MARKER_TYPE;
488 pkt->modifier = type;
490 if (type != MK_SYNC_ALL) {
491 pkt->lun = cpu_to_le16(lun);
492 SET_TARGET_ID(ha, pkt->target, loop_id);
494 wmb();
496 /* Issue command to ISP */
497 qla2x00_isp_cmd(ha);
499 return (QLA_SUCCESS);
502 int
503 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
504 uint8_t type)
506 int ret;
507 unsigned long flags = 0;
509 spin_lock_irqsave(&ha->hardware_lock, flags);
510 ret = __qla2x00_marker(ha, loop_id, lun, type);
511 spin_unlock_irqrestore(&ha->hardware_lock, flags);
513 return (ret);
517 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
518 * @ha: HA context
520 * Note: The caller must hold the hardware lock before calling this routine.
522 * Returns NULL if function failed, else, a pointer to the request packet.
524 static request_t *
525 qla2x00_req_pkt(scsi_qla_host_t *ha)
527 device_reg_t __iomem *reg = ha->iobase;
528 request_t *pkt = NULL;
529 uint16_t cnt;
530 uint32_t *dword_ptr;
531 uint32_t timer;
532 uint16_t req_cnt = 1;
534 /* Wait 1 second for slot. */
535 for (timer = HZ; timer; timer--) {
536 if ((req_cnt + 2) >= ha->req_q_cnt) {
537 /* Calculate number of free request entries. */
538 cnt = qla2x00_debounce_register(ISP_REQ_Q_OUT(ha, reg));
539 if (ha->req_ring_index < cnt)
540 ha->req_q_cnt = cnt - ha->req_ring_index;
541 else
542 ha->req_q_cnt = ha->request_q_length -
543 (ha->req_ring_index - cnt);
545 /* If room for request in request ring. */
546 if ((req_cnt + 2) < ha->req_q_cnt) {
547 ha->req_q_cnt--;
548 pkt = ha->request_ring_ptr;
550 /* Zero out packet. */
551 dword_ptr = (uint32_t *)pkt;
552 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
553 *dword_ptr++ = 0;
555 /* Set system defined field. */
556 pkt->sys_define = (uint8_t)ha->req_ring_index;
558 /* Set entry count. */
559 pkt->entry_count = 1;
561 break;
564 /* Release ring specific lock */
565 spin_unlock(&ha->hardware_lock);
567 udelay(2); /* 2 us */
569 /* Check for pending interrupts. */
570 /* During init we issue marker directly */
571 if (!ha->marker_needed)
572 qla2x00_poll(ha);
574 spin_lock_irq(&ha->hardware_lock);
576 if (!pkt) {
577 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
580 return (pkt);
584 * qla2x00_isp_cmd() - Modify the request ring pointer.
585 * @ha: HA context
587 * Note: The caller must hold the hardware lock before calling this routine.
589 void
590 qla2x00_isp_cmd(scsi_qla_host_t *ha)
592 device_reg_t __iomem *reg = ha->iobase;
594 DEBUG5(printk("%s(): IOCB data:\n", __func__));
595 DEBUG5(qla2x00_dump_buffer(
596 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
598 /* Adjust ring index. */
599 ha->req_ring_index++;
600 if (ha->req_ring_index == ha->request_q_length) {
601 ha->req_ring_index = 0;
602 ha->request_ring_ptr = ha->request_ring;
603 } else
604 ha->request_ring_ptr++;
606 /* Set chip new ring index. */
607 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
608 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */