2 * Copyright (C) 2000-2002 Michael Cornwell <cornwell@acm.org>
3 * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
4 * Copyright (C) 2001-2002 Klaus Smolin
5 * IBM Storage Technology Division
6 * Copyright (C) 2003-2004, 2007 Bartlomiej Zolnierkiewicz
8 * The big the bad and the ugly.
11 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/hdreg.h>
20 #include <linux/ide.h>
21 #include <linux/scatterlist.h>
23 #include <asm/uaccess.h>
26 void ide_tf_dump(const char *s
, struct ide_taskfile
*tf
)
29 printk("%s: tf: feat 0x%02x nsect 0x%02x lbal 0x%02x "
30 "lbam 0x%02x lbah 0x%02x dev 0x%02x cmd 0x%02x\n",
31 s
, tf
->feature
, tf
->nsect
, tf
->lbal
,
32 tf
->lbam
, tf
->lbah
, tf
->device
, tf
->command
);
33 printk("%s: hob: nsect 0x%02x lbal 0x%02x "
34 "lbam 0x%02x lbah 0x%02x\n",
35 s
, tf
->hob_nsect
, tf
->hob_lbal
,
36 tf
->hob_lbam
, tf
->hob_lbah
);
40 int taskfile_lib_get_identify (ide_drive_t
*drive
, u8
*buf
)
44 memset(&cmd
, 0, sizeof(cmd
));
46 if (drive
->media
== ide_disk
)
47 cmd
.tf
.command
= ATA_CMD_ID_ATA
;
49 cmd
.tf
.command
= ATA_CMD_ID_ATAPI
;
50 cmd
.tf_flags
= IDE_TFLAG_TF
| IDE_TFLAG_DEVICE
;
51 cmd
.protocol
= ATA_PROT_PIO
;
53 return ide_raw_taskfile(drive
, &cmd
, buf
, 1);
56 static ide_startstop_t
task_no_data_intr(ide_drive_t
*);
57 static ide_startstop_t
pre_task_out_intr(ide_drive_t
*, struct ide_cmd
*);
58 static ide_startstop_t
task_pio_intr(ide_drive_t
*);
60 ide_startstop_t
do_rw_taskfile(ide_drive_t
*drive
, struct ide_cmd
*orig_cmd
)
62 ide_hwif_t
*hwif
= drive
->hwif
;
63 struct ide_cmd
*cmd
= &hwif
->cmd
;
64 struct ide_taskfile
*tf
= &cmd
->tf
;
65 ide_handler_t
*handler
= NULL
;
66 const struct ide_tp_ops
*tp_ops
= hwif
->tp_ops
;
67 const struct ide_dma_ops
*dma_ops
= hwif
->dma_ops
;
69 if (orig_cmd
->protocol
== ATA_PROT_PIO
&&
70 (orig_cmd
->tf_flags
& IDE_TFLAG_MULTI_PIO
) &&
71 drive
->mult_count
== 0) {
72 printk(KERN_ERR
"%s: multimode not set!\n", drive
->name
);
76 if (orig_cmd
->ftf_flags
& IDE_FTFLAG_FLAGGED
)
77 orig_cmd
->ftf_flags
|= IDE_FTFLAG_SET_IN_FLAGS
;
79 memcpy(cmd
, orig_cmd
, sizeof(*cmd
));
81 if ((cmd
->tf_flags
& IDE_TFLAG_DMA_PIO_FALLBACK
) == 0) {
82 ide_tf_dump(drive
->name
, tf
);
83 tp_ops
->write_devctl(hwif
, ATA_DEVCTL_OBS
);
84 SELECT_MASK(drive
, 0);
86 if (cmd
->ftf_flags
& IDE_FTFLAG_OUT_DATA
) {
87 u8 data
[2] = { tf
->data
, tf
->hob_data
};
89 tp_ops
->output_data(drive
, cmd
, data
, 2);
91 tp_ops
->tf_load(drive
, cmd
);
94 switch (cmd
->protocol
) {
96 if (cmd
->tf_flags
& IDE_TFLAG_WRITE
) {
97 tp_ops
->exec_command(hwif
, tf
->command
);
98 ndelay(400); /* FIXME */
99 return pre_task_out_intr(drive
, cmd
);
101 handler
= task_pio_intr
;
103 case ATA_PROT_NODATA
:
105 handler
= task_no_data_intr
;
106 ide_execute_command(drive
, cmd
, handler
, WAIT_WORSTCASE
);
109 if (ide_dma_prepare(drive
, cmd
))
111 hwif
->expiry
= dma_ops
->dma_timer_expiry
;
112 ide_execute_command(drive
, cmd
, ide_dma_intr
, 2 * WAIT_CMD
);
113 dma_ops
->dma_start(drive
);
118 EXPORT_SYMBOL_GPL(do_rw_taskfile
);
120 static ide_startstop_t
task_no_data_intr(ide_drive_t
*drive
)
122 ide_hwif_t
*hwif
= drive
->hwif
;
123 struct ide_cmd
*cmd
= &hwif
->cmd
;
124 struct ide_taskfile
*tf
= &cmd
->tf
;
125 int custom
= (cmd
->tf_flags
& IDE_TFLAG_CUSTOM_HANDLER
) ? 1 : 0;
126 int retries
= (custom
&& tf
->command
== ATA_CMD_INIT_DEV_PARAMS
) ? 5 : 1;
129 local_irq_enable_in_hardirq();
132 stat
= hwif
->tp_ops
->read_status(hwif
);
133 if ((stat
& ATA_BUSY
) == 0 || retries
-- == 0)
138 if (!OK_STAT(stat
, ATA_DRDY
, BAD_STAT
)) {
139 if (custom
&& tf
->command
== ATA_CMD_SET_MULTI
) {
140 drive
->mult_req
= drive
->mult_count
= 0;
141 drive
->special
.b
.recalibrate
= 1;
142 (void)ide_dump_status(drive
, __func__
, stat
);
144 } else if (custom
&& tf
->command
== ATA_CMD_INIT_DEV_PARAMS
) {
145 if ((stat
& (ATA_ERR
| ATA_DRQ
)) == 0) {
146 ide_set_handler(drive
, &task_no_data_intr
,
151 return ide_error(drive
, "task_no_data_intr", stat
);
154 if (custom
&& tf
->command
== ATA_CMD_SET_MULTI
)
155 drive
->mult_count
= drive
->mult_req
;
157 if (custom
== 0 || tf
->command
== ATA_CMD_IDLEIMMEDIATE
||
158 tf
->command
== ATA_CMD_CHK_POWER
) {
159 struct request
*rq
= hwif
->rq
;
161 if (blk_pm_request(rq
))
162 ide_complete_pm_rq(drive
, rq
);
164 ide_finish_cmd(drive
, cmd
, stat
);
170 static u8
wait_drive_not_busy(ide_drive_t
*drive
)
172 ide_hwif_t
*hwif
= drive
->hwif
;
177 * Last sector was transfered, wait until device is ready. This can
178 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
180 for (retries
= 0; retries
< 1000; retries
++) {
181 stat
= hwif
->tp_ops
->read_status(hwif
);
190 printk(KERN_ERR
"%s: drive still BUSY!\n", drive
->name
);
195 void ide_pio_bytes(ide_drive_t
*drive
, struct ide_cmd
*cmd
,
196 unsigned int write
, unsigned int len
)
198 ide_hwif_t
*hwif
= drive
->hwif
;
199 struct scatterlist
*sg
= hwif
->sg_table
;
200 struct scatterlist
*cursg
= cmd
->cursg
;
208 cursg
= cmd
->cursg
= sg
;
211 unsigned nr_bytes
= min(len
, cursg
->length
- cmd
->cursg_ofs
);
213 if (nr_bytes
> PAGE_SIZE
)
214 nr_bytes
= PAGE_SIZE
;
216 page
= sg_page(cursg
);
217 offset
= cursg
->offset
+ cmd
->cursg_ofs
;
219 /* get the current page and offset */
220 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
223 if (PageHighMem(page
))
224 local_irq_save(flags
);
226 buf
= kmap_atomic(page
, KM_BIO_SRC_IRQ
) + offset
;
228 cmd
->nleft
-= nr_bytes
;
229 cmd
->cursg_ofs
+= nr_bytes
;
231 if (cmd
->cursg_ofs
== cursg
->length
) {
232 cursg
= cmd
->cursg
= sg_next(cmd
->cursg
);
236 /* do the actual data transfer */
238 hwif
->tp_ops
->output_data(drive
, cmd
, buf
, nr_bytes
);
240 hwif
->tp_ops
->input_data(drive
, cmd
, buf
, nr_bytes
);
242 kunmap_atomic(buf
, KM_BIO_SRC_IRQ
);
244 if (PageHighMem(page
))
245 local_irq_restore(flags
);
250 EXPORT_SYMBOL_GPL(ide_pio_bytes
);
252 static void ide_pio_datablock(ide_drive_t
*drive
, struct ide_cmd
*cmd
,
255 unsigned int nr_bytes
;
257 u8 saved_io_32bit
= drive
->io_32bit
;
259 if (cmd
->tf_flags
& IDE_TFLAG_FS
)
262 if (cmd
->tf_flags
& IDE_TFLAG_IO_16BIT
)
265 touch_softlockup_watchdog();
267 if (cmd
->tf_flags
& IDE_TFLAG_MULTI_PIO
)
268 nr_bytes
= min_t(unsigned, cmd
->nleft
, drive
->mult_count
<< 9);
270 nr_bytes
= SECTOR_SIZE
;
272 ide_pio_bytes(drive
, cmd
, write
, nr_bytes
);
274 drive
->io_32bit
= saved_io_32bit
;
277 static void ide_error_cmd(ide_drive_t
*drive
, struct ide_cmd
*cmd
)
279 if (cmd
->tf_flags
& IDE_TFLAG_FS
) {
280 int nr_bytes
= cmd
->nbytes
- cmd
->nleft
;
282 if (cmd
->protocol
== ATA_PROT_PIO
&&
283 ((cmd
->tf_flags
& IDE_TFLAG_WRITE
) || cmd
->nleft
== 0)) {
284 if (cmd
->tf_flags
& IDE_TFLAG_MULTI_PIO
)
285 nr_bytes
-= drive
->mult_count
<< 9;
287 nr_bytes
-= SECTOR_SIZE
;
291 ide_complete_rq(drive
, 0, nr_bytes
);
295 void ide_finish_cmd(ide_drive_t
*drive
, struct ide_cmd
*cmd
, u8 stat
)
297 struct request
*rq
= drive
->hwif
->rq
;
298 u8 err
= ide_read_error(drive
);
300 ide_complete_cmd(drive
, cmd
, stat
, err
);
302 ide_complete_rq(drive
, err
? -EIO
: 0, blk_rq_bytes(rq
));
306 * Handler for command with PIO data phase.
308 static ide_startstop_t
task_pio_intr(ide_drive_t
*drive
)
310 ide_hwif_t
*hwif
= drive
->hwif
;
311 struct ide_cmd
*cmd
= &drive
->hwif
->cmd
;
312 u8 stat
= hwif
->tp_ops
->read_status(hwif
);
313 u8 write
= !!(cmd
->tf_flags
& IDE_TFLAG_WRITE
);
320 /* Didn't want any data? Odd. */
321 if ((stat
& ATA_DRQ
) == 0) {
322 /* Command all done? */
323 if (OK_STAT(stat
, ATA_DRDY
, ATA_BUSY
))
326 /* Assume it was a spurious irq */
330 if (!OK_STAT(stat
, DRIVE_READY
, drive
->bad_wstat
))
333 /* Deal with unexpected ATA data phase. */
334 if (((stat
& ATA_DRQ
) == 0) ^ (cmd
->nleft
== 0))
338 if (write
&& cmd
->nleft
== 0)
341 /* Still data left to transfer. */
342 ide_pio_datablock(drive
, cmd
, write
);
344 /* Are we done? Check status and finish transfer. */
345 if (write
== 0 && cmd
->nleft
== 0) {
346 stat
= wait_drive_not_busy(drive
);
347 if (!OK_STAT(stat
, 0, BAD_STAT
))
353 /* Still data left to transfer. */
354 ide_set_handler(drive
, &task_pio_intr
, WAIT_WORSTCASE
);
357 if ((cmd
->tf_flags
& IDE_TFLAG_FS
) == 0)
358 ide_finish_cmd(drive
, cmd
, stat
);
360 ide_complete_rq(drive
, 0, cmd
->rq
->nr_sectors
<< 9);
363 ide_error_cmd(drive
, cmd
);
364 return ide_error(drive
, __func__
, stat
);
367 static ide_startstop_t
pre_task_out_intr(ide_drive_t
*drive
,
370 ide_startstop_t startstop
;
372 if (ide_wait_stat(&startstop
, drive
, ATA_DRQ
,
373 drive
->bad_wstat
, WAIT_DRQ
)) {
374 printk(KERN_ERR
"%s: no DRQ after issuing %sWRITE%s\n",
376 (cmd
->tf_flags
& IDE_TFLAG_MULTI_PIO
) ? "MULT" : "",
377 (drive
->dev_flags
& IDE_DFLAG_LBA48
) ? "_EXT" : "");
381 if ((drive
->dev_flags
& IDE_DFLAG_UNMASK
) == 0)
384 ide_set_handler(drive
, &task_pio_intr
, WAIT_WORSTCASE
);
386 ide_pio_datablock(drive
, cmd
, 1);
391 int ide_raw_taskfile(ide_drive_t
*drive
, struct ide_cmd
*cmd
, u8
*buf
,
397 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
398 rq
->cmd_type
= REQ_TYPE_ATA_TASKFILE
;
402 * (ks) We transfer currently only whole sectors.
403 * This is suffient for now. But, it would be great,
404 * if we would find a solution to transfer any size.
405 * To support special commands like READ LONG.
407 rq
->hard_nr_sectors
= rq
->nr_sectors
= nsect
;
408 rq
->hard_cur_sectors
= rq
->current_nr_sectors
= nsect
;
410 if (cmd
->tf_flags
& IDE_TFLAG_WRITE
)
411 rq
->cmd_flags
|= REQ_RW
;
416 error
= blk_execute_rq(drive
->queue
, NULL
, rq
, 0);
422 EXPORT_SYMBOL(ide_raw_taskfile
);
424 int ide_no_data_taskfile(ide_drive_t
*drive
, struct ide_cmd
*cmd
)
426 cmd
->protocol
= ATA_PROT_NODATA
;
428 return ide_raw_taskfile(drive
, cmd
, NULL
, 0);
430 EXPORT_SYMBOL_GPL(ide_no_data_taskfile
);
432 #ifdef CONFIG_IDE_TASK_IOCTL
433 int ide_taskfile_ioctl(ide_drive_t
*drive
, unsigned long arg
)
435 ide_task_request_t
*req_task
;
441 int tasksize
= sizeof(struct ide_task_request_s
);
442 unsigned int taskin
= 0;
443 unsigned int taskout
= 0;
445 char __user
*buf
= (char __user
*)arg
;
447 // printk("IDE Taskfile ...\n");
449 req_task
= kzalloc(tasksize
, GFP_KERNEL
);
450 if (req_task
== NULL
) return -ENOMEM
;
451 if (copy_from_user(req_task
, buf
, tasksize
)) {
456 taskout
= req_task
->out_size
;
457 taskin
= req_task
->in_size
;
459 if (taskin
> 65536 || taskout
> 65536) {
465 int outtotal
= tasksize
;
466 outbuf
= kzalloc(taskout
, GFP_KERNEL
);
467 if (outbuf
== NULL
) {
471 if (copy_from_user(outbuf
, buf
+ outtotal
, taskout
)) {
478 int intotal
= tasksize
+ taskout
;
479 inbuf
= kzalloc(taskin
, GFP_KERNEL
);
484 if (copy_from_user(inbuf
, buf
+ intotal
, taskin
)) {
490 memset(&cmd
, 0, sizeof(cmd
));
492 memcpy(&cmd
.tf_array
[0], req_task
->hob_ports
,
493 HDIO_DRIVE_HOB_HDR_SIZE
- 2);
494 memcpy(&cmd
.tf_array
[6], req_task
->io_ports
,
495 HDIO_DRIVE_TASK_HDR_SIZE
);
497 cmd
.tf_flags
= IDE_TFLAG_IO_16BIT
| IDE_TFLAG_DEVICE
|
500 if (drive
->dev_flags
& IDE_DFLAG_LBA48
)
501 cmd
.tf_flags
|= (IDE_TFLAG_LBA48
| IDE_TFLAG_IN_HOB
);
503 if (req_task
->out_flags
.all
) {
504 cmd
.ftf_flags
|= IDE_FTFLAG_FLAGGED
;
506 if (req_task
->out_flags
.b
.data
)
507 cmd
.ftf_flags
|= IDE_FTFLAG_OUT_DATA
;
509 if (req_task
->out_flags
.b
.nsector_hob
)
510 cmd
.tf_flags
|= IDE_TFLAG_OUT_HOB_NSECT
;
511 if (req_task
->out_flags
.b
.sector_hob
)
512 cmd
.tf_flags
|= IDE_TFLAG_OUT_HOB_LBAL
;
513 if (req_task
->out_flags
.b
.lcyl_hob
)
514 cmd
.tf_flags
|= IDE_TFLAG_OUT_HOB_LBAM
;
515 if (req_task
->out_flags
.b
.hcyl_hob
)
516 cmd
.tf_flags
|= IDE_TFLAG_OUT_HOB_LBAH
;
518 if (req_task
->out_flags
.b
.error_feature
)
519 cmd
.tf_flags
|= IDE_TFLAG_OUT_FEATURE
;
520 if (req_task
->out_flags
.b
.nsector
)
521 cmd
.tf_flags
|= IDE_TFLAG_OUT_NSECT
;
522 if (req_task
->out_flags
.b
.sector
)
523 cmd
.tf_flags
|= IDE_TFLAG_OUT_LBAL
;
524 if (req_task
->out_flags
.b
.lcyl
)
525 cmd
.tf_flags
|= IDE_TFLAG_OUT_LBAM
;
526 if (req_task
->out_flags
.b
.hcyl
)
527 cmd
.tf_flags
|= IDE_TFLAG_OUT_LBAH
;
529 cmd
.tf_flags
|= IDE_TFLAG_OUT_TF
;
530 if (cmd
.tf_flags
& IDE_TFLAG_LBA48
)
531 cmd
.tf_flags
|= IDE_TFLAG_OUT_HOB
;
534 if (req_task
->in_flags
.b
.data
)
535 cmd
.ftf_flags
|= IDE_FTFLAG_IN_DATA
;
537 if (req_task
->req_cmd
== IDE_DRIVE_TASK_RAW_WRITE
) {
538 /* fixup data phase if needed */
539 if (req_task
->data_phase
== TASKFILE_IN_DMAQ
||
540 req_task
->data_phase
== TASKFILE_IN_DMA
)
541 cmd
.tf_flags
|= IDE_TFLAG_WRITE
;
544 cmd
.protocol
= ATA_PROT_DMA
;
546 switch (req_task
->data_phase
) {
547 case TASKFILE_MULTI_OUT
:
548 if (!drive
->mult_count
) {
549 /* (hs): give up if multcount is not set */
550 printk(KERN_ERR
"%s: %s Multimode Write " \
551 "multcount is not set\n",
552 drive
->name
, __func__
);
556 cmd
.tf_flags
|= IDE_TFLAG_MULTI_PIO
;
559 cmd
.protocol
= ATA_PROT_PIO
;
561 case TASKFILE_OUT_DMAQ
:
562 case TASKFILE_OUT_DMA
:
563 cmd
.tf_flags
|= IDE_TFLAG_WRITE
;
564 nsect
= taskout
/ SECTOR_SIZE
;
567 case TASKFILE_MULTI_IN
:
568 if (!drive
->mult_count
) {
569 /* (hs): give up if multcount is not set */
570 printk(KERN_ERR
"%s: %s Multimode Read failure " \
571 "multcount is not set\n",
572 drive
->name
, __func__
);
576 cmd
.tf_flags
|= IDE_TFLAG_MULTI_PIO
;
579 cmd
.protocol
= ATA_PROT_PIO
;
581 case TASKFILE_IN_DMAQ
:
582 case TASKFILE_IN_DMA
:
583 nsect
= taskin
/ SECTOR_SIZE
;
586 case TASKFILE_NO_DATA
:
587 cmd
.protocol
= ATA_PROT_NODATA
;
594 if (req_task
->req_cmd
== IDE_DRIVE_TASK_NO_DATA
)
597 nsect
= (cmd
.tf
.hob_nsect
<< 8) | cmd
.tf
.nsect
;
600 printk(KERN_ERR
"%s: in/out command without data\n",
607 err
= ide_raw_taskfile(drive
, &cmd
, data_buf
, nsect
);
609 memcpy(req_task
->hob_ports
, &cmd
.tf_array
[0],
610 HDIO_DRIVE_HOB_HDR_SIZE
- 2);
611 memcpy(req_task
->io_ports
, &cmd
.tf_array
[6],
612 HDIO_DRIVE_TASK_HDR_SIZE
);
614 if ((cmd
.ftf_flags
& IDE_FTFLAG_SET_IN_FLAGS
) &&
615 req_task
->in_flags
.all
== 0) {
616 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
617 if (drive
->dev_flags
& IDE_DFLAG_LBA48
)
618 req_task
->in_flags
.all
|= (IDE_HOB_STD_IN_FLAGS
<< 8);
621 if (copy_to_user(buf
, req_task
, tasksize
)) {
626 int outtotal
= tasksize
;
627 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
633 int intotal
= tasksize
+ taskout
;
634 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
644 // printk("IDE Taskfile ioctl ended. rc = %i\n", err);