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
.valid
.out
.tf
= IDE_VALID_OUT_TF
| IDE_VALID_DEVICE
;
51 cmd
.valid
.in
.tf
= IDE_VALID_IN_TF
| IDE_VALID_DEVICE
;
52 cmd
.protocol
= ATA_PROT_PIO
;
54 return ide_raw_taskfile(drive
, &cmd
, buf
, 1);
57 static ide_startstop_t
task_no_data_intr(ide_drive_t
*);
58 static ide_startstop_t
pre_task_out_intr(ide_drive_t
*, struct ide_cmd
*);
59 static ide_startstop_t
task_pio_intr(ide_drive_t
*);
61 ide_startstop_t
do_rw_taskfile(ide_drive_t
*drive
, struct ide_cmd
*orig_cmd
)
63 ide_hwif_t
*hwif
= drive
->hwif
;
64 struct ide_cmd
*cmd
= &hwif
->cmd
;
65 struct ide_taskfile
*tf
= &cmd
->tf
;
66 ide_handler_t
*handler
= NULL
;
67 const struct ide_tp_ops
*tp_ops
= hwif
->tp_ops
;
68 const struct ide_dma_ops
*dma_ops
= hwif
->dma_ops
;
70 if (orig_cmd
->protocol
== ATA_PROT_PIO
&&
71 (orig_cmd
->tf_flags
& IDE_TFLAG_MULTI_PIO
) &&
72 drive
->mult_count
== 0) {
73 printk(KERN_ERR
"%s: multimode not set!\n", drive
->name
);
77 if (orig_cmd
->ftf_flags
& IDE_FTFLAG_FLAGGED
)
78 orig_cmd
->ftf_flags
|= IDE_FTFLAG_SET_IN_FLAGS
;
80 memcpy(cmd
, orig_cmd
, sizeof(*cmd
));
82 if ((cmd
->tf_flags
& IDE_TFLAG_DMA_PIO_FALLBACK
) == 0) {
83 ide_tf_dump(drive
->name
, tf
);
84 tp_ops
->write_devctl(hwif
, ATA_DEVCTL_OBS
);
85 SELECT_MASK(drive
, 0);
87 if (cmd
->ftf_flags
& IDE_FTFLAG_OUT_DATA
) {
88 u8 data
[2] = { tf
->data
, tf
->hob_data
};
90 tp_ops
->output_data(drive
, cmd
, data
, 2);
92 tp_ops
->tf_load(drive
, cmd
);
95 switch (cmd
->protocol
) {
97 if (cmd
->tf_flags
& IDE_TFLAG_WRITE
) {
98 tp_ops
->exec_command(hwif
, tf
->command
);
99 ndelay(400); /* FIXME */
100 return pre_task_out_intr(drive
, cmd
);
102 handler
= task_pio_intr
;
104 case ATA_PROT_NODATA
:
106 handler
= task_no_data_intr
;
107 ide_execute_command(drive
, cmd
, handler
, WAIT_WORSTCASE
);
110 if (ide_dma_prepare(drive
, cmd
))
112 hwif
->expiry
= dma_ops
->dma_timer_expiry
;
113 ide_execute_command(drive
, cmd
, ide_dma_intr
, 2 * WAIT_CMD
);
114 dma_ops
->dma_start(drive
);
119 EXPORT_SYMBOL_GPL(do_rw_taskfile
);
121 static ide_startstop_t
task_no_data_intr(ide_drive_t
*drive
)
123 ide_hwif_t
*hwif
= drive
->hwif
;
124 struct ide_cmd
*cmd
= &hwif
->cmd
;
125 struct ide_taskfile
*tf
= &cmd
->tf
;
126 int custom
= (cmd
->tf_flags
& IDE_TFLAG_CUSTOM_HANDLER
) ? 1 : 0;
127 int retries
= (custom
&& tf
->command
== ATA_CMD_INIT_DEV_PARAMS
) ? 5 : 1;
130 local_irq_enable_in_hardirq();
133 stat
= hwif
->tp_ops
->read_status(hwif
);
134 if ((stat
& ATA_BUSY
) == 0 || retries
-- == 0)
139 if (!OK_STAT(stat
, ATA_DRDY
, BAD_STAT
)) {
140 if (custom
&& tf
->command
== ATA_CMD_SET_MULTI
) {
141 drive
->mult_req
= drive
->mult_count
= 0;
142 drive
->special
.b
.recalibrate
= 1;
143 (void)ide_dump_status(drive
, __func__
, stat
);
145 } else if (custom
&& tf
->command
== ATA_CMD_INIT_DEV_PARAMS
) {
146 if ((stat
& (ATA_ERR
| ATA_DRQ
)) == 0) {
147 ide_set_handler(drive
, &task_no_data_intr
,
152 return ide_error(drive
, "task_no_data_intr", stat
);
155 if (custom
&& tf
->command
== ATA_CMD_SET_MULTI
)
156 drive
->mult_count
= drive
->mult_req
;
158 if (custom
== 0 || tf
->command
== ATA_CMD_IDLEIMMEDIATE
||
159 tf
->command
== ATA_CMD_CHK_POWER
) {
160 struct request
*rq
= hwif
->rq
;
162 if (blk_pm_request(rq
))
163 ide_complete_pm_rq(drive
, rq
);
165 ide_finish_cmd(drive
, cmd
, stat
);
171 static u8
wait_drive_not_busy(ide_drive_t
*drive
)
173 ide_hwif_t
*hwif
= drive
->hwif
;
178 * Last sector was transfered, wait until device is ready. This can
179 * take up to 6 ms on some ATAPI devices, so we will wait max 10 ms.
181 for (retries
= 0; retries
< 1000; retries
++) {
182 stat
= hwif
->tp_ops
->read_status(hwif
);
191 printk(KERN_ERR
"%s: drive still BUSY!\n", drive
->name
);
196 void ide_pio_bytes(ide_drive_t
*drive
, struct ide_cmd
*cmd
,
197 unsigned int write
, unsigned int len
)
199 ide_hwif_t
*hwif
= drive
->hwif
;
200 struct scatterlist
*sg
= hwif
->sg_table
;
201 struct scatterlist
*cursg
= cmd
->cursg
;
209 cursg
= cmd
->cursg
= sg
;
212 unsigned nr_bytes
= min(len
, cursg
->length
- cmd
->cursg_ofs
);
214 if (nr_bytes
> PAGE_SIZE
)
215 nr_bytes
= PAGE_SIZE
;
217 page
= sg_page(cursg
);
218 offset
= cursg
->offset
+ cmd
->cursg_ofs
;
220 /* get the current page and offset */
221 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
224 if (PageHighMem(page
))
225 local_irq_save(flags
);
227 buf
= kmap_atomic(page
, KM_BIO_SRC_IRQ
) + offset
;
229 cmd
->nleft
-= nr_bytes
;
230 cmd
->cursg_ofs
+= nr_bytes
;
232 if (cmd
->cursg_ofs
== cursg
->length
) {
233 cursg
= cmd
->cursg
= sg_next(cmd
->cursg
);
237 /* do the actual data transfer */
239 hwif
->tp_ops
->output_data(drive
, cmd
, buf
, nr_bytes
);
241 hwif
->tp_ops
->input_data(drive
, cmd
, buf
, nr_bytes
);
243 kunmap_atomic(buf
, KM_BIO_SRC_IRQ
);
245 if (PageHighMem(page
))
246 local_irq_restore(flags
);
251 EXPORT_SYMBOL_GPL(ide_pio_bytes
);
253 static void ide_pio_datablock(ide_drive_t
*drive
, struct ide_cmd
*cmd
,
256 unsigned int nr_bytes
;
258 u8 saved_io_32bit
= drive
->io_32bit
;
260 if (cmd
->tf_flags
& IDE_TFLAG_FS
)
263 if (cmd
->tf_flags
& IDE_TFLAG_IO_16BIT
)
266 touch_softlockup_watchdog();
268 if (cmd
->tf_flags
& IDE_TFLAG_MULTI_PIO
)
269 nr_bytes
= min_t(unsigned, cmd
->nleft
, drive
->mult_count
<< 9);
271 nr_bytes
= SECTOR_SIZE
;
273 ide_pio_bytes(drive
, cmd
, write
, nr_bytes
);
275 drive
->io_32bit
= saved_io_32bit
;
278 static void ide_error_cmd(ide_drive_t
*drive
, struct ide_cmd
*cmd
)
280 if (cmd
->tf_flags
& IDE_TFLAG_FS
) {
281 int nr_bytes
= cmd
->nbytes
- cmd
->nleft
;
283 if (cmd
->protocol
== ATA_PROT_PIO
&&
284 ((cmd
->tf_flags
& IDE_TFLAG_WRITE
) || cmd
->nleft
== 0)) {
285 if (cmd
->tf_flags
& IDE_TFLAG_MULTI_PIO
)
286 nr_bytes
-= drive
->mult_count
<< 9;
288 nr_bytes
-= SECTOR_SIZE
;
292 ide_complete_rq(drive
, 0, nr_bytes
);
296 void ide_finish_cmd(ide_drive_t
*drive
, struct ide_cmd
*cmd
, u8 stat
)
298 struct request
*rq
= drive
->hwif
->rq
;
299 u8 err
= ide_read_error(drive
);
301 ide_complete_cmd(drive
, cmd
, stat
, err
);
303 ide_complete_rq(drive
, err
? -EIO
: 0, blk_rq_bytes(rq
));
307 * Handler for command with PIO data phase.
309 static ide_startstop_t
task_pio_intr(ide_drive_t
*drive
)
311 ide_hwif_t
*hwif
= drive
->hwif
;
312 struct ide_cmd
*cmd
= &drive
->hwif
->cmd
;
313 u8 stat
= hwif
->tp_ops
->read_status(hwif
);
314 u8 write
= !!(cmd
->tf_flags
& IDE_TFLAG_WRITE
);
321 /* Didn't want any data? Odd. */
322 if ((stat
& ATA_DRQ
) == 0) {
323 /* Command all done? */
324 if (OK_STAT(stat
, ATA_DRDY
, ATA_BUSY
))
327 /* Assume it was a spurious irq */
331 if (!OK_STAT(stat
, DRIVE_READY
, drive
->bad_wstat
))
334 /* Deal with unexpected ATA data phase. */
335 if (((stat
& ATA_DRQ
) == 0) ^ (cmd
->nleft
== 0))
339 if (write
&& cmd
->nleft
== 0)
342 /* Still data left to transfer. */
343 ide_pio_datablock(drive
, cmd
, write
);
345 /* Are we done? Check status and finish transfer. */
346 if (write
== 0 && cmd
->nleft
== 0) {
347 stat
= wait_drive_not_busy(drive
);
348 if (!OK_STAT(stat
, 0, BAD_STAT
))
354 /* Still data left to transfer. */
355 ide_set_handler(drive
, &task_pio_intr
, WAIT_WORSTCASE
);
358 if ((cmd
->tf_flags
& IDE_TFLAG_FS
) == 0)
359 ide_finish_cmd(drive
, cmd
, stat
);
361 ide_complete_rq(drive
, 0, cmd
->rq
->nr_sectors
<< 9);
364 ide_error_cmd(drive
, cmd
);
365 return ide_error(drive
, __func__
, stat
);
368 static ide_startstop_t
pre_task_out_intr(ide_drive_t
*drive
,
371 ide_startstop_t startstop
;
373 if (ide_wait_stat(&startstop
, drive
, ATA_DRQ
,
374 drive
->bad_wstat
, WAIT_DRQ
)) {
375 printk(KERN_ERR
"%s: no DRQ after issuing %sWRITE%s\n",
377 (cmd
->tf_flags
& IDE_TFLAG_MULTI_PIO
) ? "MULT" : "",
378 (drive
->dev_flags
& IDE_DFLAG_LBA48
) ? "_EXT" : "");
382 if ((drive
->dev_flags
& IDE_DFLAG_UNMASK
) == 0)
385 ide_set_handler(drive
, &task_pio_intr
, WAIT_WORSTCASE
);
387 ide_pio_datablock(drive
, cmd
, 1);
392 int ide_raw_taskfile(ide_drive_t
*drive
, struct ide_cmd
*cmd
, u8
*buf
,
398 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
399 rq
->cmd_type
= REQ_TYPE_ATA_TASKFILE
;
403 * (ks) We transfer currently only whole sectors.
404 * This is suffient for now. But, it would be great,
405 * if we would find a solution to transfer any size.
406 * To support special commands like READ LONG.
408 rq
->hard_nr_sectors
= rq
->nr_sectors
= nsect
;
409 rq
->hard_cur_sectors
= rq
->current_nr_sectors
= nsect
;
411 if (cmd
->tf_flags
& IDE_TFLAG_WRITE
)
412 rq
->cmd_flags
|= REQ_RW
;
417 error
= blk_execute_rq(drive
->queue
, NULL
, rq
, 0);
423 EXPORT_SYMBOL(ide_raw_taskfile
);
425 int ide_no_data_taskfile(ide_drive_t
*drive
, struct ide_cmd
*cmd
)
427 cmd
->protocol
= ATA_PROT_NODATA
;
429 return ide_raw_taskfile(drive
, cmd
, NULL
, 0);
431 EXPORT_SYMBOL_GPL(ide_no_data_taskfile
);
433 #ifdef CONFIG_IDE_TASK_IOCTL
434 int ide_taskfile_ioctl(ide_drive_t
*drive
, unsigned long arg
)
436 ide_task_request_t
*req_task
;
442 int tasksize
= sizeof(struct ide_task_request_s
);
443 unsigned int taskin
= 0;
444 unsigned int taskout
= 0;
446 char __user
*buf
= (char __user
*)arg
;
448 // printk("IDE Taskfile ...\n");
450 req_task
= kzalloc(tasksize
, GFP_KERNEL
);
451 if (req_task
== NULL
) return -ENOMEM
;
452 if (copy_from_user(req_task
, buf
, tasksize
)) {
457 taskout
= req_task
->out_size
;
458 taskin
= req_task
->in_size
;
460 if (taskin
> 65536 || taskout
> 65536) {
466 int outtotal
= tasksize
;
467 outbuf
= kzalloc(taskout
, GFP_KERNEL
);
468 if (outbuf
== NULL
) {
472 if (copy_from_user(outbuf
, buf
+ outtotal
, taskout
)) {
479 int intotal
= tasksize
+ taskout
;
480 inbuf
= kzalloc(taskin
, GFP_KERNEL
);
485 if (copy_from_user(inbuf
, buf
+ intotal
, taskin
)) {
491 memset(&cmd
, 0, sizeof(cmd
));
493 memcpy(&cmd
.tf_array
[0], req_task
->hob_ports
,
494 HDIO_DRIVE_HOB_HDR_SIZE
- 2);
495 memcpy(&cmd
.tf_array
[6], req_task
->io_ports
,
496 HDIO_DRIVE_TASK_HDR_SIZE
);
498 cmd
.valid
.out
.tf
= IDE_VALID_DEVICE
;
499 cmd
.valid
.in
.tf
= IDE_VALID_DEVICE
| IDE_VALID_IN_TF
;
500 cmd
.tf_flags
= IDE_TFLAG_IO_16BIT
;
502 if (drive
->dev_flags
& IDE_DFLAG_LBA48
) {
503 cmd
.tf_flags
|= IDE_TFLAG_LBA48
;
504 cmd
.valid
.in
.hob
= IDE_VALID_IN_HOB
;
507 if (req_task
->out_flags
.all
) {
508 cmd
.ftf_flags
|= IDE_FTFLAG_FLAGGED
;
510 if (req_task
->out_flags
.b
.data
)
511 cmd
.ftf_flags
|= IDE_FTFLAG_OUT_DATA
;
513 if (req_task
->out_flags
.b
.nsector_hob
)
514 cmd
.valid
.out
.hob
|= IDE_VALID_NSECT
;
515 if (req_task
->out_flags
.b
.sector_hob
)
516 cmd
.valid
.out
.hob
|= IDE_VALID_LBAL
;
517 if (req_task
->out_flags
.b
.lcyl_hob
)
518 cmd
.valid
.out
.hob
|= IDE_VALID_LBAM
;
519 if (req_task
->out_flags
.b
.hcyl_hob
)
520 cmd
.valid
.out
.hob
|= IDE_VALID_LBAH
;
522 if (req_task
->out_flags
.b
.error_feature
)
523 cmd
.valid
.out
.tf
|= IDE_VALID_FEATURE
;
524 if (req_task
->out_flags
.b
.nsector
)
525 cmd
.valid
.out
.tf
|= IDE_VALID_NSECT
;
526 if (req_task
->out_flags
.b
.sector
)
527 cmd
.valid
.out
.tf
|= IDE_VALID_LBAL
;
528 if (req_task
->out_flags
.b
.lcyl
)
529 cmd
.valid
.out
.tf
|= IDE_VALID_LBAM
;
530 if (req_task
->out_flags
.b
.hcyl
)
531 cmd
.valid
.out
.tf
|= IDE_VALID_LBAH
;
533 cmd
.valid
.out
.tf
|= IDE_VALID_OUT_TF
;
534 if (cmd
.tf_flags
& IDE_TFLAG_LBA48
)
535 cmd
.valid
.out
.hob
|= IDE_VALID_OUT_HOB
;
538 if (req_task
->in_flags
.b
.data
)
539 cmd
.ftf_flags
|= IDE_FTFLAG_IN_DATA
;
541 if (req_task
->req_cmd
== IDE_DRIVE_TASK_RAW_WRITE
) {
542 /* fixup data phase if needed */
543 if (req_task
->data_phase
== TASKFILE_IN_DMAQ
||
544 req_task
->data_phase
== TASKFILE_IN_DMA
)
545 cmd
.tf_flags
|= IDE_TFLAG_WRITE
;
548 cmd
.protocol
= ATA_PROT_DMA
;
550 switch (req_task
->data_phase
) {
551 case TASKFILE_MULTI_OUT
:
552 if (!drive
->mult_count
) {
553 /* (hs): give up if multcount is not set */
554 printk(KERN_ERR
"%s: %s Multimode Write " \
555 "multcount is not set\n",
556 drive
->name
, __func__
);
560 cmd
.tf_flags
|= IDE_TFLAG_MULTI_PIO
;
563 cmd
.protocol
= ATA_PROT_PIO
;
565 case TASKFILE_OUT_DMAQ
:
566 case TASKFILE_OUT_DMA
:
567 cmd
.tf_flags
|= IDE_TFLAG_WRITE
;
568 nsect
= taskout
/ SECTOR_SIZE
;
571 case TASKFILE_MULTI_IN
:
572 if (!drive
->mult_count
) {
573 /* (hs): give up if multcount is not set */
574 printk(KERN_ERR
"%s: %s Multimode Read failure " \
575 "multcount is not set\n",
576 drive
->name
, __func__
);
580 cmd
.tf_flags
|= IDE_TFLAG_MULTI_PIO
;
583 cmd
.protocol
= ATA_PROT_PIO
;
585 case TASKFILE_IN_DMAQ
:
586 case TASKFILE_IN_DMA
:
587 nsect
= taskin
/ SECTOR_SIZE
;
590 case TASKFILE_NO_DATA
:
591 cmd
.protocol
= ATA_PROT_NODATA
;
598 if (req_task
->req_cmd
== IDE_DRIVE_TASK_NO_DATA
)
601 nsect
= (cmd
.tf
.hob_nsect
<< 8) | cmd
.tf
.nsect
;
604 printk(KERN_ERR
"%s: in/out command without data\n",
611 err
= ide_raw_taskfile(drive
, &cmd
, data_buf
, nsect
);
613 memcpy(req_task
->hob_ports
, &cmd
.tf_array
[0],
614 HDIO_DRIVE_HOB_HDR_SIZE
- 2);
615 memcpy(req_task
->io_ports
, &cmd
.tf_array
[6],
616 HDIO_DRIVE_TASK_HDR_SIZE
);
618 if ((cmd
.ftf_flags
& IDE_FTFLAG_SET_IN_FLAGS
) &&
619 req_task
->in_flags
.all
== 0) {
620 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
621 if (drive
->dev_flags
& IDE_DFLAG_LBA48
)
622 req_task
->in_flags
.all
|= (IDE_HOB_STD_IN_FLAGS
<< 8);
625 if (copy_to_user(buf
, req_task
, tasksize
)) {
630 int outtotal
= tasksize
;
631 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
637 int intotal
= tasksize
+ taskout
;
638 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
648 // printk("IDE Taskfile ioctl ended. rc = %i\n", err);