2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static unsigned int ata_busy_sleep (struct ata_port
*ap
,
65 unsigned long tmout_pat
,
67 static void ata_dev_reread_id(struct ata_port
*ap
, struct ata_device
*dev
);
68 static void ata_dev_init_params(struct ata_port
*ap
, struct ata_device
*dev
);
69 static void ata_set_mode(struct ata_port
*ap
);
70 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
);
71 static unsigned int ata_get_mode_mask(const struct ata_port
*ap
, int shift
);
72 static int fgb(u32 bitmap
);
73 static int ata_choose_xfer_mode(const struct ata_port
*ap
,
75 unsigned int *xfer_shift_out
);
76 static void __ata_qc_complete(struct ata_queued_cmd
*qc
);
78 static unsigned int ata_unique_id
= 1;
79 static struct workqueue_struct
*ata_wq
;
81 int atapi_enabled
= 0;
82 module_param(atapi_enabled
, int, 0444);
83 MODULE_PARM_DESC(atapi_enabled
, "Enable discovery of ATAPI devices (0=off, 1=on)");
86 module_param_named(fua
, libata_fua
, int, 0444);
87 MODULE_PARM_DESC(fua
, "FUA support (0=off, 1=on)");
89 MODULE_AUTHOR("Jeff Garzik");
90 MODULE_DESCRIPTION("Library module for ATA devices");
91 MODULE_LICENSE("GPL");
92 MODULE_VERSION(DRV_VERSION
);
95 * ata_tf_load_pio - send taskfile registers to host controller
96 * @ap: Port to which output is sent
97 * @tf: ATA taskfile register set
99 * Outputs ATA taskfile to standard ATA host controller.
102 * Inherited from caller.
105 static void ata_tf_load_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
107 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
108 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
110 if (tf
->ctl
!= ap
->last_ctl
) {
111 outb(tf
->ctl
, ioaddr
->ctl_addr
);
112 ap
->last_ctl
= tf
->ctl
;
116 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
117 outb(tf
->hob_feature
, ioaddr
->feature_addr
);
118 outb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
119 outb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
120 outb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
121 outb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
122 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
131 outb(tf
->feature
, ioaddr
->feature_addr
);
132 outb(tf
->nsect
, ioaddr
->nsect_addr
);
133 outb(tf
->lbal
, ioaddr
->lbal_addr
);
134 outb(tf
->lbam
, ioaddr
->lbam_addr
);
135 outb(tf
->lbah
, ioaddr
->lbah_addr
);
136 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
144 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
145 outb(tf
->device
, ioaddr
->device_addr
);
146 VPRINTK("device 0x%X\n", tf
->device
);
153 * ata_tf_load_mmio - send taskfile registers to host controller
154 * @ap: Port to which output is sent
155 * @tf: ATA taskfile register set
157 * Outputs ATA taskfile to standard ATA host controller using MMIO.
160 * Inherited from caller.
163 static void ata_tf_load_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
165 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
166 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
168 if (tf
->ctl
!= ap
->last_ctl
) {
169 writeb(tf
->ctl
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
170 ap
->last_ctl
= tf
->ctl
;
174 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
175 writeb(tf
->hob_feature
, (void __iomem
*) ioaddr
->feature_addr
);
176 writeb(tf
->hob_nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
177 writeb(tf
->hob_lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
178 writeb(tf
->hob_lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
179 writeb(tf
->hob_lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
180 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
189 writeb(tf
->feature
, (void __iomem
*) ioaddr
->feature_addr
);
190 writeb(tf
->nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
191 writeb(tf
->lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
192 writeb(tf
->lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
193 writeb(tf
->lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
194 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
202 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
203 writeb(tf
->device
, (void __iomem
*) ioaddr
->device_addr
);
204 VPRINTK("device 0x%X\n", tf
->device
);
212 * ata_tf_load - send taskfile registers to host controller
213 * @ap: Port to which output is sent
214 * @tf: ATA taskfile register set
216 * Outputs ATA taskfile to standard ATA host controller using MMIO
217 * or PIO as indicated by the ATA_FLAG_MMIO flag.
218 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
219 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
220 * hob_lbal, hob_lbam, and hob_lbah.
222 * This function waits for idle (!BUSY and !DRQ) after writing
223 * registers. If the control register has a new value, this
224 * function also waits for idle after writing control and before
225 * writing the remaining registers.
227 * May be used as the tf_load() entry in ata_port_operations.
230 * Inherited from caller.
232 void ata_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
234 if (ap
->flags
& ATA_FLAG_MMIO
)
235 ata_tf_load_mmio(ap
, tf
);
237 ata_tf_load_pio(ap
, tf
);
241 * ata_exec_command_pio - issue ATA command to host controller
242 * @ap: port to which command is being issued
243 * @tf: ATA taskfile register set
245 * Issues PIO write to ATA command register, with proper
246 * synchronization with interrupt handler / other threads.
249 * spin_lock_irqsave(host_set lock)
252 static void ata_exec_command_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
254 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
256 outb(tf
->command
, ap
->ioaddr
.command_addr
);
262 * ata_exec_command_mmio - issue ATA command to host controller
263 * @ap: port to which command is being issued
264 * @tf: ATA taskfile register set
266 * Issues MMIO write to ATA command register, with proper
267 * synchronization with interrupt handler / other threads.
270 * spin_lock_irqsave(host_set lock)
273 static void ata_exec_command_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
275 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
277 writeb(tf
->command
, (void __iomem
*) ap
->ioaddr
.command_addr
);
283 * ata_exec_command - issue ATA command to host controller
284 * @ap: port to which command is being issued
285 * @tf: ATA taskfile register set
287 * Issues PIO/MMIO write to ATA command register, with proper
288 * synchronization with interrupt handler / other threads.
291 * spin_lock_irqsave(host_set lock)
293 void ata_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
295 if (ap
->flags
& ATA_FLAG_MMIO
)
296 ata_exec_command_mmio(ap
, tf
);
298 ata_exec_command_pio(ap
, tf
);
302 * ata_tf_to_host - issue ATA taskfile to host controller
303 * @ap: port to which command is being issued
304 * @tf: ATA taskfile register set
306 * Issues ATA taskfile register set to ATA host controller,
307 * with proper synchronization with interrupt handler and
311 * spin_lock_irqsave(host_set lock)
314 static inline void ata_tf_to_host(struct ata_port
*ap
,
315 const struct ata_taskfile
*tf
)
317 ap
->ops
->tf_load(ap
, tf
);
318 ap
->ops
->exec_command(ap
, tf
);
322 * ata_tf_read_pio - input device's ATA taskfile shadow registers
323 * @ap: Port from which input is read
324 * @tf: ATA taskfile register set for storing input
326 * Reads ATA taskfile registers for currently-selected device
330 * Inherited from caller.
333 static void ata_tf_read_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
335 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
337 tf
->command
= ata_check_status(ap
);
338 tf
->feature
= inb(ioaddr
->error_addr
);
339 tf
->nsect
= inb(ioaddr
->nsect_addr
);
340 tf
->lbal
= inb(ioaddr
->lbal_addr
);
341 tf
->lbam
= inb(ioaddr
->lbam_addr
);
342 tf
->lbah
= inb(ioaddr
->lbah_addr
);
343 tf
->device
= inb(ioaddr
->device_addr
);
345 if (tf
->flags
& ATA_TFLAG_LBA48
) {
346 outb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
347 tf
->hob_feature
= inb(ioaddr
->error_addr
);
348 tf
->hob_nsect
= inb(ioaddr
->nsect_addr
);
349 tf
->hob_lbal
= inb(ioaddr
->lbal_addr
);
350 tf
->hob_lbam
= inb(ioaddr
->lbam_addr
);
351 tf
->hob_lbah
= inb(ioaddr
->lbah_addr
);
356 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
357 * @ap: Port from which input is read
358 * @tf: ATA taskfile register set for storing input
360 * Reads ATA taskfile registers for currently-selected device
364 * Inherited from caller.
367 static void ata_tf_read_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
369 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
371 tf
->command
= ata_check_status(ap
);
372 tf
->feature
= readb((void __iomem
*)ioaddr
->error_addr
);
373 tf
->nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
374 tf
->lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
375 tf
->lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
376 tf
->lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
377 tf
->device
= readb((void __iomem
*)ioaddr
->device_addr
);
379 if (tf
->flags
& ATA_TFLAG_LBA48
) {
380 writeb(tf
->ctl
| ATA_HOB
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
381 tf
->hob_feature
= readb((void __iomem
*)ioaddr
->error_addr
);
382 tf
->hob_nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
383 tf
->hob_lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
384 tf
->hob_lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
385 tf
->hob_lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
391 * ata_tf_read - input device's ATA taskfile shadow registers
392 * @ap: Port from which input is read
393 * @tf: ATA taskfile register set for storing input
395 * Reads ATA taskfile registers for currently-selected device
398 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
399 * is set, also reads the hob registers.
401 * May be used as the tf_read() entry in ata_port_operations.
404 * Inherited from caller.
406 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
408 if (ap
->flags
& ATA_FLAG_MMIO
)
409 ata_tf_read_mmio(ap
, tf
);
411 ata_tf_read_pio(ap
, tf
);
415 * ata_check_status_pio - Read device status reg & clear interrupt
416 * @ap: port where the device is
418 * Reads ATA taskfile status register for currently-selected device
419 * and return its value. This also clears pending interrupts
423 * Inherited from caller.
425 static u8
ata_check_status_pio(struct ata_port
*ap
)
427 return inb(ap
->ioaddr
.status_addr
);
431 * ata_check_status_mmio - Read device status reg & clear interrupt
432 * @ap: port where the device is
434 * Reads ATA taskfile status register for currently-selected device
435 * via MMIO and return its value. This also clears pending interrupts
439 * Inherited from caller.
441 static u8
ata_check_status_mmio(struct ata_port
*ap
)
443 return readb((void __iomem
*) ap
->ioaddr
.status_addr
);
448 * ata_check_status - Read device status reg & clear interrupt
449 * @ap: port where the device is
451 * Reads ATA taskfile status register for currently-selected device
452 * and return its value. This also clears pending interrupts
455 * May be used as the check_status() entry in ata_port_operations.
458 * Inherited from caller.
460 u8
ata_check_status(struct ata_port
*ap
)
462 if (ap
->flags
& ATA_FLAG_MMIO
)
463 return ata_check_status_mmio(ap
);
464 return ata_check_status_pio(ap
);
469 * ata_altstatus - Read device alternate status reg
470 * @ap: port where the device is
472 * Reads ATA taskfile alternate status register for
473 * currently-selected device and return its value.
475 * Note: may NOT be used as the check_altstatus() entry in
476 * ata_port_operations.
479 * Inherited from caller.
481 u8
ata_altstatus(struct ata_port
*ap
)
483 if (ap
->ops
->check_altstatus
)
484 return ap
->ops
->check_altstatus(ap
);
486 if (ap
->flags
& ATA_FLAG_MMIO
)
487 return readb((void __iomem
*)ap
->ioaddr
.altstatus_addr
);
488 return inb(ap
->ioaddr
.altstatus_addr
);
493 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
494 * @tf: Taskfile to convert
495 * @fis: Buffer into which data will output
496 * @pmp: Port multiplier port
498 * Converts a standard ATA taskfile to a Serial ATA
499 * FIS structure (Register - Host to Device).
502 * Inherited from caller.
505 void ata_tf_to_fis(const struct ata_taskfile
*tf
, u8
*fis
, u8 pmp
)
507 fis
[0] = 0x27; /* Register - Host to Device FIS */
508 fis
[1] = (pmp
& 0xf) | (1 << 7); /* Port multiplier number,
509 bit 7 indicates Command FIS */
510 fis
[2] = tf
->command
;
511 fis
[3] = tf
->feature
;
518 fis
[8] = tf
->hob_lbal
;
519 fis
[9] = tf
->hob_lbam
;
520 fis
[10] = tf
->hob_lbah
;
521 fis
[11] = tf
->hob_feature
;
524 fis
[13] = tf
->hob_nsect
;
535 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
536 * @fis: Buffer from which data will be input
537 * @tf: Taskfile to output
539 * Converts a serial ATA FIS structure to a standard ATA taskfile.
542 * Inherited from caller.
545 void ata_tf_from_fis(const u8
*fis
, struct ata_taskfile
*tf
)
547 tf
->command
= fis
[2]; /* status */
548 tf
->feature
= fis
[3]; /* error */
555 tf
->hob_lbal
= fis
[8];
556 tf
->hob_lbam
= fis
[9];
557 tf
->hob_lbah
= fis
[10];
560 tf
->hob_nsect
= fis
[13];
563 static const u8 ata_rw_cmds
[] = {
567 ATA_CMD_READ_MULTI_EXT
,
568 ATA_CMD_WRITE_MULTI_EXT
,
572 ATA_CMD_WRITE_MULTI_FUA_EXT
,
576 ATA_CMD_PIO_READ_EXT
,
577 ATA_CMD_PIO_WRITE_EXT
,
590 ATA_CMD_WRITE_FUA_EXT
594 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
595 * @qc: command to examine and configure
597 * Examine the device configuration and tf->flags to calculate
598 * the proper read/write commands and protocol to use.
603 int ata_rwcmd_protocol(struct ata_queued_cmd
*qc
)
605 struct ata_taskfile
*tf
= &qc
->tf
;
606 struct ata_device
*dev
= qc
->dev
;
609 int index
, fua
, lba48
, write
;
611 fua
= (tf
->flags
& ATA_TFLAG_FUA
) ? 4 : 0;
612 lba48
= (tf
->flags
& ATA_TFLAG_LBA48
) ? 2 : 0;
613 write
= (tf
->flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
615 if (dev
->flags
& ATA_DFLAG_PIO
) {
616 tf
->protocol
= ATA_PROT_PIO
;
617 index
= dev
->multi_count
? 0 : 8;
618 } else if (lba48
&& (qc
->ap
->flags
& ATA_FLAG_PIO_LBA48
)) {
619 /* Unable to use DMA due to host limitation */
620 tf
->protocol
= ATA_PROT_PIO
;
621 index
= dev
->multi_count
? 0 : 8;
623 tf
->protocol
= ATA_PROT_DMA
;
627 cmd
= ata_rw_cmds
[index
+ fua
+ lba48
+ write
];
635 static const char * const xfer_mode_str
[] = {
655 * ata_udma_string - convert UDMA bit offset to string
656 * @mask: mask of bits supported; only highest bit counts.
658 * Determine string which represents the highest speed
659 * (highest bit in @udma_mask).
665 * Constant C string representing highest speed listed in
666 * @udma_mask, or the constant C string "<n/a>".
669 static const char *ata_mode_string(unsigned int mask
)
673 for (i
= 7; i
>= 0; i
--)
676 for (i
= ATA_SHIFT_MWDMA
+ 2; i
>= ATA_SHIFT_MWDMA
; i
--)
679 for (i
= ATA_SHIFT_PIO
+ 4; i
>= ATA_SHIFT_PIO
; i
--)
686 return xfer_mode_str
[i
];
690 * ata_pio_devchk - PATA device presence detection
691 * @ap: ATA channel to examine
692 * @device: Device to examine (starting at zero)
694 * This technique was originally described in
695 * Hale Landis's ATADRVR (www.ata-atapi.com), and
696 * later found its way into the ATA/ATAPI spec.
698 * Write a pattern to the ATA shadow registers,
699 * and if a device is present, it will respond by
700 * correctly storing and echoing back the
701 * ATA shadow register contents.
707 static unsigned int ata_pio_devchk(struct ata_port
*ap
,
710 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
713 ap
->ops
->dev_select(ap
, device
);
715 outb(0x55, ioaddr
->nsect_addr
);
716 outb(0xaa, ioaddr
->lbal_addr
);
718 outb(0xaa, ioaddr
->nsect_addr
);
719 outb(0x55, ioaddr
->lbal_addr
);
721 outb(0x55, ioaddr
->nsect_addr
);
722 outb(0xaa, ioaddr
->lbal_addr
);
724 nsect
= inb(ioaddr
->nsect_addr
);
725 lbal
= inb(ioaddr
->lbal_addr
);
727 if ((nsect
== 0x55) && (lbal
== 0xaa))
728 return 1; /* we found a device */
730 return 0; /* nothing found */
734 * ata_mmio_devchk - PATA device presence detection
735 * @ap: ATA channel to examine
736 * @device: Device to examine (starting at zero)
738 * This technique was originally described in
739 * Hale Landis's ATADRVR (www.ata-atapi.com), and
740 * later found its way into the ATA/ATAPI spec.
742 * Write a pattern to the ATA shadow registers,
743 * and if a device is present, it will respond by
744 * correctly storing and echoing back the
745 * ATA shadow register contents.
751 static unsigned int ata_mmio_devchk(struct ata_port
*ap
,
754 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
757 ap
->ops
->dev_select(ap
, device
);
759 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
760 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
762 writeb(0xaa, (void __iomem
*) ioaddr
->nsect_addr
);
763 writeb(0x55, (void __iomem
*) ioaddr
->lbal_addr
);
765 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
766 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
768 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
769 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
771 if ((nsect
== 0x55) && (lbal
== 0xaa))
772 return 1; /* we found a device */
774 return 0; /* nothing found */
778 * ata_devchk - PATA device presence detection
779 * @ap: ATA channel to examine
780 * @device: Device to examine (starting at zero)
782 * Dispatch ATA device presence detection, depending
783 * on whether we are using PIO or MMIO to talk to the
784 * ATA shadow registers.
790 static unsigned int ata_devchk(struct ata_port
*ap
,
793 if (ap
->flags
& ATA_FLAG_MMIO
)
794 return ata_mmio_devchk(ap
, device
);
795 return ata_pio_devchk(ap
, device
);
799 * ata_dev_classify - determine device type based on ATA-spec signature
800 * @tf: ATA taskfile register set for device to be identified
802 * Determine from taskfile register contents whether a device is
803 * ATA or ATAPI, as per "Signature and persistence" section
804 * of ATA/PI spec (volume 1, sect 5.14).
810 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
811 * the event of failure.
814 unsigned int ata_dev_classify(const struct ata_taskfile
*tf
)
816 /* Apple's open source Darwin code hints that some devices only
817 * put a proper signature into the LBA mid/high registers,
818 * So, we only check those. It's sufficient for uniqueness.
821 if (((tf
->lbam
== 0) && (tf
->lbah
== 0)) ||
822 ((tf
->lbam
== 0x3c) && (tf
->lbah
== 0xc3))) {
823 DPRINTK("found ATA device by sig\n");
827 if (((tf
->lbam
== 0x14) && (tf
->lbah
== 0xeb)) ||
828 ((tf
->lbam
== 0x69) && (tf
->lbah
== 0x96))) {
829 DPRINTK("found ATAPI device by sig\n");
830 return ATA_DEV_ATAPI
;
833 DPRINTK("unknown device\n");
834 return ATA_DEV_UNKNOWN
;
838 * ata_dev_try_classify - Parse returned ATA device signature
839 * @ap: ATA channel to examine
840 * @device: Device to examine (starting at zero)
842 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
843 * an ATA/ATAPI-defined set of values is placed in the ATA
844 * shadow registers, indicating the results of device detection
847 * Select the ATA device, and read the values from the ATA shadow
848 * registers. Then parse according to the Error register value,
849 * and the spec-defined values examined by ata_dev_classify().
855 static u8
ata_dev_try_classify(struct ata_port
*ap
, unsigned int device
)
857 struct ata_device
*dev
= &ap
->device
[device
];
858 struct ata_taskfile tf
;
862 ap
->ops
->dev_select(ap
, device
);
864 memset(&tf
, 0, sizeof(tf
));
866 ap
->ops
->tf_read(ap
, &tf
);
869 dev
->class = ATA_DEV_NONE
;
871 /* see if device passed diags */
874 else if ((device
== 0) && (err
== 0x81))
879 /* determine if device if ATA or ATAPI */
880 class = ata_dev_classify(&tf
);
881 if (class == ATA_DEV_UNKNOWN
)
883 if ((class == ATA_DEV_ATA
) && (ata_chk_status(ap
) == 0))
892 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
893 * @id: IDENTIFY DEVICE results we will examine
894 * @s: string into which data is output
895 * @ofs: offset into identify device page
896 * @len: length of string to return. must be an even number.
898 * The strings in the IDENTIFY DEVICE page are broken up into
899 * 16-bit chunks. Run through the string, and output each
900 * 8-bit chunk linearly, regardless of platform.
906 void ata_dev_id_string(const u16
*id
, unsigned char *s
,
907 unsigned int ofs
, unsigned int len
)
927 * ata_noop_dev_select - Select device 0/1 on ATA bus
928 * @ap: ATA channel to manipulate
929 * @device: ATA device (numbered from zero) to select
931 * This function performs no actual function.
933 * May be used as the dev_select() entry in ata_port_operations.
938 void ata_noop_dev_select (struct ata_port
*ap
, unsigned int device
)
944 * ata_std_dev_select - Select device 0/1 on ATA bus
945 * @ap: ATA channel to manipulate
946 * @device: ATA device (numbered from zero) to select
948 * Use the method defined in the ATA specification to
949 * make either device 0, or device 1, active on the
950 * ATA channel. Works with both PIO and MMIO.
952 * May be used as the dev_select() entry in ata_port_operations.
958 void ata_std_dev_select (struct ata_port
*ap
, unsigned int device
)
963 tmp
= ATA_DEVICE_OBS
;
965 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
967 if (ap
->flags
& ATA_FLAG_MMIO
) {
968 writeb(tmp
, (void __iomem
*) ap
->ioaddr
.device_addr
);
970 outb(tmp
, ap
->ioaddr
.device_addr
);
972 ata_pause(ap
); /* needed; also flushes, for mmio */
976 * ata_dev_select - Select device 0/1 on ATA bus
977 * @ap: ATA channel to manipulate
978 * @device: ATA device (numbered from zero) to select
979 * @wait: non-zero to wait for Status register BSY bit to clear
980 * @can_sleep: non-zero if context allows sleeping
982 * Use the method defined in the ATA specification to
983 * make either device 0, or device 1, active on the
986 * This is a high-level version of ata_std_dev_select(),
987 * which additionally provides the services of inserting
988 * the proper pauses and status polling, where needed.
994 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
995 unsigned int wait
, unsigned int can_sleep
)
997 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
998 ap
->id
, device
, wait
);
1003 ap
->ops
->dev_select(ap
, device
);
1006 if (can_sleep
&& ap
->device
[device
].class == ATA_DEV_ATAPI
)
1013 * ata_dump_id - IDENTIFY DEVICE info debugging output
1014 * @dev: Device whose IDENTIFY DEVICE page we will dump
1016 * Dump selected 16-bit words from a detected device's
1017 * IDENTIFY PAGE page.
1023 static inline void ata_dump_id(const struct ata_device
*dev
)
1025 DPRINTK("49==0x%04x "
1035 DPRINTK("80==0x%04x "
1045 DPRINTK("88==0x%04x "
1052 * Compute the PIO modes available for this device. This is not as
1053 * trivial as it seems if we must consider early devices correctly.
1055 * FIXME: pre IDE drive timing (do we care ?).
1058 static unsigned int ata_pio_modes(const struct ata_device
*adev
)
1062 /* Usual case. Word 53 indicates word 64 is valid */
1063 if (adev
->id
[ATA_ID_FIELD_VALID
] & (1 << 1)) {
1064 modes
= adev
->id
[ATA_ID_PIO_MODES
] & 0x03;
1070 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
1071 number for the maximum. Turn it into a mask and return it */
1072 modes
= (2 << ((adev
->id
[ATA_ID_OLD_PIO_MODES
] >> 8) & 0xFF)) - 1 ;
1074 /* But wait.. there's more. Design your standards by committee and
1075 you too can get a free iordy field to process. However its the
1076 speeds not the modes that are supported... Note drivers using the
1077 timing API will get this right anyway */
1080 struct ata_exec_internal_arg
{
1081 unsigned int err_mask
;
1082 struct ata_taskfile
*tf
;
1083 struct completion
*waiting
;
1086 int ata_qc_complete_internal(struct ata_queued_cmd
*qc
)
1088 struct ata_exec_internal_arg
*arg
= qc
->private_data
;
1089 struct completion
*waiting
= arg
->waiting
;
1091 if (!(qc
->err_mask
& ~AC_ERR_DEV
))
1092 qc
->ap
->ops
->tf_read(qc
->ap
, arg
->tf
);
1093 arg
->err_mask
= qc
->err_mask
;
1094 arg
->waiting
= NULL
;
1101 * ata_exec_internal - execute libata internal command
1102 * @ap: Port to which the command is sent
1103 * @dev: Device to which the command is sent
1104 * @tf: Taskfile registers for the command and the result
1105 * @dma_dir: Data tranfer direction of the command
1106 * @buf: Data buffer of the command
1107 * @buflen: Length of data buffer
1109 * Executes libata internal command with timeout. @tf contains
1110 * command on entry and result on return. Timeout and error
1111 * conditions are reported via return value. No recovery action
1112 * is taken after a command times out. It's caller's duty to
1113 * clean up after timeout.
1116 * None. Should be called with kernel context, might sleep.
1120 ata_exec_internal(struct ata_port
*ap
, struct ata_device
*dev
,
1121 struct ata_taskfile
*tf
,
1122 int dma_dir
, void *buf
, unsigned int buflen
)
1124 u8 command
= tf
->command
;
1125 struct ata_queued_cmd
*qc
;
1126 DECLARE_COMPLETION(wait
);
1127 unsigned long flags
;
1128 struct ata_exec_internal_arg arg
;
1130 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1132 qc
= ata_qc_new_init(ap
, dev
);
1136 qc
->dma_dir
= dma_dir
;
1137 if (dma_dir
!= DMA_NONE
) {
1138 ata_sg_init_one(qc
, buf
, buflen
);
1139 qc
->nsect
= buflen
/ ATA_SECT_SIZE
;
1142 arg
.waiting
= &wait
;
1144 qc
->private_data
= &arg
;
1145 qc
->complete_fn
= ata_qc_complete_internal
;
1147 if (ata_qc_issue(qc
))
1150 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1152 if (!wait_for_completion_timeout(&wait
, ATA_TMOUT_INTERNAL
)) {
1153 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1155 /* We're racing with irq here. If we lose, the
1156 * following test prevents us from completing the qc
1157 * again. If completion irq occurs after here but
1158 * before the caller cleans up, it will result in a
1159 * spurious interrupt. We can live with that.
1162 qc
->err_mask
= AC_ERR_OTHER
;
1163 ata_qc_complete(qc
);
1164 printk(KERN_WARNING
"ata%u: qc timeout (cmd 0x%x)\n",
1168 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1171 return arg
.err_mask
;
1175 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1176 return AC_ERR_OTHER
;
1180 * ata_pio_need_iordy - check if iordy needed
1183 * Check if the current speed of the device requires IORDY. Used
1184 * by various controllers for chip configuration.
1187 unsigned int ata_pio_need_iordy(const struct ata_device
*adev
)
1190 int speed
= adev
->pio_mode
- XFER_PIO_0
;
1197 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1199 if (adev
->id
[ATA_ID_FIELD_VALID
] & 2) { /* EIDE */
1200 pio
= adev
->id
[ATA_ID_EIDE_PIO
];
1201 /* Is the speed faster than the drive allows non IORDY ? */
1203 /* This is cycle times not frequency - watch the logic! */
1204 if (pio
> 240) /* PIO2 is 240nS per cycle */
1213 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1214 * @ap: port on which device we wish to probe resides
1215 * @device: device bus address, starting at zero
1217 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1218 * command, and read back the 512-byte device information page.
1219 * The device information page is fed to us via the standard
1220 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1221 * using standard PIO-IN paths)
1223 * After reading the device information page, we use several
1224 * bits of information from it to initialize data structures
1225 * that will be used during the lifetime of the ata_device.
1226 * Other data from the info page is used to disqualify certain
1227 * older ATA devices we do not wish to support.
1230 * Inherited from caller. Some functions called by this function
1231 * obtain the host_set lock.
1234 static void ata_dev_identify(struct ata_port
*ap
, unsigned int device
)
1236 struct ata_device
*dev
= &ap
->device
[device
];
1237 unsigned int major_version
;
1239 unsigned long xfer_modes
;
1240 unsigned int using_edd
;
1241 struct ata_taskfile tf
;
1242 unsigned int err_mask
;
1245 if (!ata_dev_present(dev
)) {
1246 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1251 if (ap
->flags
& (ATA_FLAG_SRST
| ATA_FLAG_SATA_RESET
))
1256 DPRINTK("ENTER, host %u, dev %u\n", ap
->id
, device
);
1258 assert (dev
->class == ATA_DEV_ATA
|| dev
->class == ATA_DEV_ATAPI
||
1259 dev
->class == ATA_DEV_NONE
);
1261 ata_dev_select(ap
, device
, 1, 1); /* select device 0/1 */
1264 ata_tf_init(ap
, &tf
, device
);
1266 if (dev
->class == ATA_DEV_ATA
) {
1267 tf
.command
= ATA_CMD_ID_ATA
;
1268 DPRINTK("do ATA identify\n");
1270 tf
.command
= ATA_CMD_ID_ATAPI
;
1271 DPRINTK("do ATAPI identify\n");
1274 tf
.protocol
= ATA_PROT_PIO
;
1276 err_mask
= ata_exec_internal(ap
, dev
, &tf
, DMA_FROM_DEVICE
,
1277 dev
->id
, sizeof(dev
->id
));
1280 if (err_mask
& ~AC_ERR_DEV
)
1284 * arg! EDD works for all test cases, but seems to return
1285 * the ATA signature for some ATAPI devices. Until the
1286 * reason for this is found and fixed, we fix up the mess
1287 * here. If IDENTIFY DEVICE returns command aborted
1288 * (as ATAPI devices do), then we issue an
1289 * IDENTIFY PACKET DEVICE.
1291 * ATA software reset (SRST, the default) does not appear
1292 * to have this problem.
1294 if ((using_edd
) && (dev
->class == ATA_DEV_ATA
)) {
1295 u8 err
= tf
.feature
;
1296 if (err
& ATA_ABORTED
) {
1297 dev
->class = ATA_DEV_ATAPI
;
1304 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
1306 /* print device capabilities */
1307 printk(KERN_DEBUG
"ata%u: dev %u cfg "
1308 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1309 ap
->id
, device
, dev
->id
[49],
1310 dev
->id
[82], dev
->id
[83], dev
->id
[84],
1311 dev
->id
[85], dev
->id
[86], dev
->id
[87],
1315 * common ATA, ATAPI feature tests
1318 /* we require DMA support (bits 8 of word 49) */
1319 if (!ata_id_has_dma(dev
->id
)) {
1320 printk(KERN_DEBUG
"ata%u: no dma\n", ap
->id
);
1324 /* quick-n-dirty find max transfer mode; for printk only */
1325 xfer_modes
= dev
->id
[ATA_ID_UDMA_MODES
];
1327 xfer_modes
= (dev
->id
[ATA_ID_MWDMA_MODES
]) << ATA_SHIFT_MWDMA
;
1329 xfer_modes
= ata_pio_modes(dev
);
1333 /* ATA-specific feature tests */
1334 if (dev
->class == ATA_DEV_ATA
) {
1335 if (!ata_id_is_ata(dev
->id
)) /* sanity check */
1338 /* get major version */
1339 tmp
= dev
->id
[ATA_ID_MAJOR_VER
];
1340 for (major_version
= 14; major_version
>= 1; major_version
--)
1341 if (tmp
& (1 << major_version
))
1345 * The exact sequence expected by certain pre-ATA4 drives is:
1348 * INITIALIZE DEVICE PARAMETERS
1350 * Some drives were very specific about that exact sequence.
1352 if (major_version
< 4 || (!ata_id_has_lba(dev
->id
))) {
1353 ata_dev_init_params(ap
, dev
);
1355 /* current CHS translation info (id[53-58]) might be
1356 * changed. reread the identify device info.
1358 ata_dev_reread_id(ap
, dev
);
1361 if (ata_id_has_lba(dev
->id
)) {
1362 dev
->flags
|= ATA_DFLAG_LBA
;
1364 if (ata_id_has_lba48(dev
->id
)) {
1365 dev
->flags
|= ATA_DFLAG_LBA48
;
1366 dev
->n_sectors
= ata_id_u64(dev
->id
, 100);
1368 dev
->n_sectors
= ata_id_u32(dev
->id
, 60);
1371 /* print device info to dmesg */
1372 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1375 ata_mode_string(xfer_modes
),
1376 (unsigned long long)dev
->n_sectors
,
1377 dev
->flags
& ATA_DFLAG_LBA48
? " LBA48" : " LBA");
1381 /* Default translation */
1382 dev
->cylinders
= dev
->id
[1];
1383 dev
->heads
= dev
->id
[3];
1384 dev
->sectors
= dev
->id
[6];
1385 dev
->n_sectors
= dev
->cylinders
* dev
->heads
* dev
->sectors
;
1387 if (ata_id_current_chs_valid(dev
->id
)) {
1388 /* Current CHS translation is valid. */
1389 dev
->cylinders
= dev
->id
[54];
1390 dev
->heads
= dev
->id
[55];
1391 dev
->sectors
= dev
->id
[56];
1393 dev
->n_sectors
= ata_id_u32(dev
->id
, 57);
1396 /* print device info to dmesg */
1397 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1400 ata_mode_string(xfer_modes
),
1401 (unsigned long long)dev
->n_sectors
,
1402 (int)dev
->cylinders
, (int)dev
->heads
, (int)dev
->sectors
);
1406 ap
->host
->max_cmd_len
= 16;
1409 /* ATAPI-specific feature tests */
1410 else if (dev
->class == ATA_DEV_ATAPI
) {
1411 if (ata_id_is_ata(dev
->id
)) /* sanity check */
1414 rc
= atapi_cdb_len(dev
->id
);
1415 if ((rc
< 12) || (rc
> ATAPI_CDB_LEN
)) {
1416 printk(KERN_WARNING
"ata%u: unsupported CDB len\n", ap
->id
);
1419 ap
->cdb_len
= (unsigned int) rc
;
1420 ap
->host
->max_cmd_len
= (unsigned char) ap
->cdb_len
;
1422 /* print device info to dmesg */
1423 printk(KERN_INFO
"ata%u: dev %u ATAPI, max %s\n",
1425 ata_mode_string(xfer_modes
));
1428 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap
));
1432 printk(KERN_WARNING
"ata%u: dev %u not supported, ignoring\n",
1435 dev
->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1436 DPRINTK("EXIT, err\n");
1440 static inline u8
ata_dev_knobble(const struct ata_port
*ap
)
1442 return ((ap
->cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(ap
->device
->id
)));
1446 * ata_dev_config - Run device specific handlers and check for
1447 * SATA->PATA bridges
1454 void ata_dev_config(struct ata_port
*ap
, unsigned int i
)
1456 /* limit bridge transfers to udma5, 200 sectors */
1457 if (ata_dev_knobble(ap
)) {
1458 printk(KERN_INFO
"ata%u(%u): applying bridge limits\n",
1459 ap
->id
, ap
->device
->devno
);
1460 ap
->udma_mask
&= ATA_UDMA5
;
1461 ap
->host
->max_sectors
= ATA_MAX_SECTORS
;
1462 ap
->host
->hostt
->max_sectors
= ATA_MAX_SECTORS
;
1463 ap
->device
[i
].flags
|= ATA_DFLAG_LOCK_SECTORS
;
1466 if (ap
->ops
->dev_config
)
1467 ap
->ops
->dev_config(ap
, &ap
->device
[i
]);
1471 * ata_bus_probe - Reset and probe ATA bus
1474 * Master ATA bus probing function. Initiates a hardware-dependent
1475 * bus reset, then attempts to identify any devices found on
1479 * PCI/etc. bus probe sem.
1482 * Zero on success, non-zero on error.
1485 static int ata_bus_probe(struct ata_port
*ap
)
1487 unsigned int i
, found
= 0;
1489 ap
->ops
->phy_reset(ap
);
1490 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1493 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1494 ata_dev_identify(ap
, i
);
1495 if (ata_dev_present(&ap
->device
[i
])) {
1497 ata_dev_config(ap
,i
);
1501 if ((!found
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1502 goto err_out_disable
;
1505 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1506 goto err_out_disable
;
1511 ap
->ops
->port_disable(ap
);
1517 * ata_port_probe - Mark port as enabled
1518 * @ap: Port for which we indicate enablement
1520 * Modify @ap data structure such that the system
1521 * thinks that the entire port is enabled.
1523 * LOCKING: host_set lock, or some other form of
1527 void ata_port_probe(struct ata_port
*ap
)
1529 ap
->flags
&= ~ATA_FLAG_PORT_DISABLED
;
1533 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1534 * @ap: SATA port associated with target SATA PHY.
1536 * This function issues commands to standard SATA Sxxx
1537 * PHY registers, to wake up the phy (and device), and
1538 * clear any reset condition.
1541 * PCI/etc. bus probe sem.
1544 void __sata_phy_reset(struct ata_port
*ap
)
1547 unsigned long timeout
= jiffies
+ (HZ
* 5);
1549 if (ap
->flags
& ATA_FLAG_SATA_RESET
) {
1550 /* issue phy wake/reset */
1551 scr_write_flush(ap
, SCR_CONTROL
, 0x301);
1552 /* Couldn't find anything in SATA I/II specs, but
1553 * AHCI-1.1 10.4.2 says at least 1 ms. */
1556 scr_write_flush(ap
, SCR_CONTROL
, 0x300); /* phy wake/clear reset */
1558 /* wait for phy to become ready, if necessary */
1561 sstatus
= scr_read(ap
, SCR_STATUS
);
1562 if ((sstatus
& 0xf) != 1)
1564 } while (time_before(jiffies
, timeout
));
1566 /* TODO: phy layer with polling, timeouts, etc. */
1567 sstatus
= scr_read(ap
, SCR_STATUS
);
1568 if (sata_dev_present(ap
)) {
1572 tmp
= (sstatus
>> 4) & 0xf;
1575 else if (tmp
& (1 << 1))
1578 speed
= "<unknown>";
1579 printk(KERN_INFO
"ata%u: SATA link up %s Gbps (SStatus %X)\n",
1580 ap
->id
, speed
, sstatus
);
1583 printk(KERN_INFO
"ata%u: SATA link down (SStatus %X)\n",
1585 ata_port_disable(ap
);
1588 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1591 if (ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
)) {
1592 ata_port_disable(ap
);
1596 ap
->cbl
= ATA_CBL_SATA
;
1600 * sata_phy_reset - Reset SATA bus.
1601 * @ap: SATA port associated with target SATA PHY.
1603 * This function resets the SATA bus, and then probes
1604 * the bus for devices.
1607 * PCI/etc. bus probe sem.
1610 void sata_phy_reset(struct ata_port
*ap
)
1612 __sata_phy_reset(ap
);
1613 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1619 * ata_port_disable - Disable port.
1620 * @ap: Port to be disabled.
1622 * Modify @ap data structure such that the system
1623 * thinks that the entire port is disabled, and should
1624 * never attempt to probe or communicate with devices
1627 * LOCKING: host_set lock, or some other form of
1631 void ata_port_disable(struct ata_port
*ap
)
1633 ap
->device
[0].class = ATA_DEV_NONE
;
1634 ap
->device
[1].class = ATA_DEV_NONE
;
1635 ap
->flags
|= ATA_FLAG_PORT_DISABLED
;
1639 * This mode timing computation functionality is ported over from
1640 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1643 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1644 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1645 * for PIO 5, which is a nonstandard extension and UDMA6, which
1646 * is currently supported only by Maxtor drives.
1649 static const struct ata_timing ata_timing
[] = {
1651 { XFER_UDMA_6
, 0, 0, 0, 0, 0, 0, 0, 15 },
1652 { XFER_UDMA_5
, 0, 0, 0, 0, 0, 0, 0, 20 },
1653 { XFER_UDMA_4
, 0, 0, 0, 0, 0, 0, 0, 30 },
1654 { XFER_UDMA_3
, 0, 0, 0, 0, 0, 0, 0, 45 },
1656 { XFER_UDMA_2
, 0, 0, 0, 0, 0, 0, 0, 60 },
1657 { XFER_UDMA_1
, 0, 0, 0, 0, 0, 0, 0, 80 },
1658 { XFER_UDMA_0
, 0, 0, 0, 0, 0, 0, 0, 120 },
1660 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1662 { XFER_MW_DMA_2
, 25, 0, 0, 0, 70, 25, 120, 0 },
1663 { XFER_MW_DMA_1
, 45, 0, 0, 0, 80, 50, 150, 0 },
1664 { XFER_MW_DMA_0
, 60, 0, 0, 0, 215, 215, 480, 0 },
1666 { XFER_SW_DMA_2
, 60, 0, 0, 0, 120, 120, 240, 0 },
1667 { XFER_SW_DMA_1
, 90, 0, 0, 0, 240, 240, 480, 0 },
1668 { XFER_SW_DMA_0
, 120, 0, 0, 0, 480, 480, 960, 0 },
1670 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1671 { XFER_PIO_4
, 25, 70, 25, 120, 70, 25, 120, 0 },
1672 { XFER_PIO_3
, 30, 80, 70, 180, 80, 70, 180, 0 },
1674 { XFER_PIO_2
, 30, 290, 40, 330, 100, 90, 240, 0 },
1675 { XFER_PIO_1
, 50, 290, 93, 383, 125, 100, 383, 0 },
1676 { XFER_PIO_0
, 70, 290, 240, 600, 165, 150, 600, 0 },
1678 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1683 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1684 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1686 static void ata_timing_quantize(const struct ata_timing
*t
, struct ata_timing
*q
, int T
, int UT
)
1688 q
->setup
= EZ(t
->setup
* 1000, T
);
1689 q
->act8b
= EZ(t
->act8b
* 1000, T
);
1690 q
->rec8b
= EZ(t
->rec8b
* 1000, T
);
1691 q
->cyc8b
= EZ(t
->cyc8b
* 1000, T
);
1692 q
->active
= EZ(t
->active
* 1000, T
);
1693 q
->recover
= EZ(t
->recover
* 1000, T
);
1694 q
->cycle
= EZ(t
->cycle
* 1000, T
);
1695 q
->udma
= EZ(t
->udma
* 1000, UT
);
1698 void ata_timing_merge(const struct ata_timing
*a
, const struct ata_timing
*b
,
1699 struct ata_timing
*m
, unsigned int what
)
1701 if (what
& ATA_TIMING_SETUP
) m
->setup
= max(a
->setup
, b
->setup
);
1702 if (what
& ATA_TIMING_ACT8B
) m
->act8b
= max(a
->act8b
, b
->act8b
);
1703 if (what
& ATA_TIMING_REC8B
) m
->rec8b
= max(a
->rec8b
, b
->rec8b
);
1704 if (what
& ATA_TIMING_CYC8B
) m
->cyc8b
= max(a
->cyc8b
, b
->cyc8b
);
1705 if (what
& ATA_TIMING_ACTIVE
) m
->active
= max(a
->active
, b
->active
);
1706 if (what
& ATA_TIMING_RECOVER
) m
->recover
= max(a
->recover
, b
->recover
);
1707 if (what
& ATA_TIMING_CYCLE
) m
->cycle
= max(a
->cycle
, b
->cycle
);
1708 if (what
& ATA_TIMING_UDMA
) m
->udma
= max(a
->udma
, b
->udma
);
1711 static const struct ata_timing
* ata_timing_find_mode(unsigned short speed
)
1713 const struct ata_timing
*t
;
1715 for (t
= ata_timing
; t
->mode
!= speed
; t
++)
1716 if (t
->mode
== 0xFF)
1721 int ata_timing_compute(struct ata_device
*adev
, unsigned short speed
,
1722 struct ata_timing
*t
, int T
, int UT
)
1724 const struct ata_timing
*s
;
1725 struct ata_timing p
;
1731 if (!(s
= ata_timing_find_mode(speed
)))
1734 memcpy(t
, s
, sizeof(*s
));
1737 * If the drive is an EIDE drive, it can tell us it needs extended
1738 * PIO/MW_DMA cycle timing.
1741 if (adev
->id
[ATA_ID_FIELD_VALID
] & 2) { /* EIDE drive */
1742 memset(&p
, 0, sizeof(p
));
1743 if(speed
>= XFER_PIO_0
&& speed
<= XFER_SW_DMA_0
) {
1744 if (speed
<= XFER_PIO_2
) p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO
];
1745 else p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO_IORDY
];
1746 } else if(speed
>= XFER_MW_DMA_0
&& speed
<= XFER_MW_DMA_2
) {
1747 p
.cycle
= adev
->id
[ATA_ID_EIDE_DMA_MIN
];
1749 ata_timing_merge(&p
, t
, t
, ATA_TIMING_CYCLE
| ATA_TIMING_CYC8B
);
1753 * Convert the timing to bus clock counts.
1756 ata_timing_quantize(t
, t
, T
, UT
);
1759 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1760 * and some other commands. We have to ensure that the DMA cycle timing is
1761 * slower/equal than the fastest PIO timing.
1764 if (speed
> XFER_PIO_4
) {
1765 ata_timing_compute(adev
, adev
->pio_mode
, &p
, T
, UT
);
1766 ata_timing_merge(&p
, t
, t
, ATA_TIMING_ALL
);
1770 * Lenghten active & recovery time so that cycle time is correct.
1773 if (t
->act8b
+ t
->rec8b
< t
->cyc8b
) {
1774 t
->act8b
+= (t
->cyc8b
- (t
->act8b
+ t
->rec8b
)) / 2;
1775 t
->rec8b
= t
->cyc8b
- t
->act8b
;
1778 if (t
->active
+ t
->recover
< t
->cycle
) {
1779 t
->active
+= (t
->cycle
- (t
->active
+ t
->recover
)) / 2;
1780 t
->recover
= t
->cycle
- t
->active
;
1786 static const struct {
1789 } xfer_mode_classes
[] = {
1790 { ATA_SHIFT_UDMA
, XFER_UDMA_0
},
1791 { ATA_SHIFT_MWDMA
, XFER_MW_DMA_0
},
1792 { ATA_SHIFT_PIO
, XFER_PIO_0
},
1795 static u8
base_from_shift(unsigned int shift
)
1799 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++)
1800 if (xfer_mode_classes
[i
].shift
== shift
)
1801 return xfer_mode_classes
[i
].base
;
1806 static void ata_dev_set_mode(struct ata_port
*ap
, struct ata_device
*dev
)
1811 if (!ata_dev_present(dev
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1814 if (dev
->xfer_shift
== ATA_SHIFT_PIO
)
1815 dev
->flags
|= ATA_DFLAG_PIO
;
1817 ata_dev_set_xfermode(ap
, dev
);
1819 base
= base_from_shift(dev
->xfer_shift
);
1820 ofs
= dev
->xfer_mode
- base
;
1821 idx
= ofs
+ dev
->xfer_shift
;
1822 WARN_ON(idx
>= ARRAY_SIZE(xfer_mode_str
));
1824 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1825 idx
, dev
->xfer_shift
, (int)dev
->xfer_mode
, (int)base
, ofs
);
1827 printk(KERN_INFO
"ata%u: dev %u configured for %s\n",
1828 ap
->id
, dev
->devno
, xfer_mode_str
[idx
]);
1831 static int ata_host_set_pio(struct ata_port
*ap
)
1837 mask
= ata_get_mode_mask(ap
, ATA_SHIFT_PIO
);
1840 printk(KERN_WARNING
"ata%u: no PIO support\n", ap
->id
);
1844 base
= base_from_shift(ATA_SHIFT_PIO
);
1845 xfer_mode
= base
+ x
;
1847 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1848 (int)base
, (int)xfer_mode
, mask
, x
);
1850 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1851 struct ata_device
*dev
= &ap
->device
[i
];
1852 if (ata_dev_present(dev
)) {
1853 dev
->pio_mode
= xfer_mode
;
1854 dev
->xfer_mode
= xfer_mode
;
1855 dev
->xfer_shift
= ATA_SHIFT_PIO
;
1856 if (ap
->ops
->set_piomode
)
1857 ap
->ops
->set_piomode(ap
, dev
);
1864 static void ata_host_set_dma(struct ata_port
*ap
, u8 xfer_mode
,
1865 unsigned int xfer_shift
)
1869 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1870 struct ata_device
*dev
= &ap
->device
[i
];
1871 if (ata_dev_present(dev
)) {
1872 dev
->dma_mode
= xfer_mode
;
1873 dev
->xfer_mode
= xfer_mode
;
1874 dev
->xfer_shift
= xfer_shift
;
1875 if (ap
->ops
->set_dmamode
)
1876 ap
->ops
->set_dmamode(ap
, dev
);
1882 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1883 * @ap: port on which timings will be programmed
1885 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1888 * PCI/etc. bus probe sem.
1891 static void ata_set_mode(struct ata_port
*ap
)
1893 unsigned int xfer_shift
;
1897 /* step 1: always set host PIO timings */
1898 rc
= ata_host_set_pio(ap
);
1902 /* step 2: choose the best data xfer mode */
1903 xfer_mode
= xfer_shift
= 0;
1904 rc
= ata_choose_xfer_mode(ap
, &xfer_mode
, &xfer_shift
);
1908 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1909 if (xfer_shift
!= ATA_SHIFT_PIO
)
1910 ata_host_set_dma(ap
, xfer_mode
, xfer_shift
);
1912 /* step 4: update devices' xfer mode */
1913 ata_dev_set_mode(ap
, &ap
->device
[0]);
1914 ata_dev_set_mode(ap
, &ap
->device
[1]);
1916 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1919 if (ap
->ops
->post_set_mode
)
1920 ap
->ops
->post_set_mode(ap
);
1925 ata_port_disable(ap
);
1929 * ata_busy_sleep - sleep until BSY clears, or timeout
1930 * @ap: port containing status register to be polled
1931 * @tmout_pat: impatience timeout
1932 * @tmout: overall timeout
1934 * Sleep until ATA Status register bit BSY clears,
1935 * or a timeout occurs.
1941 static unsigned int ata_busy_sleep (struct ata_port
*ap
,
1942 unsigned long tmout_pat
,
1943 unsigned long tmout
)
1945 unsigned long timer_start
, timeout
;
1948 status
= ata_busy_wait(ap
, ATA_BUSY
, 300);
1949 timer_start
= jiffies
;
1950 timeout
= timer_start
+ tmout_pat
;
1951 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1953 status
= ata_busy_wait(ap
, ATA_BUSY
, 3);
1956 if (status
& ATA_BUSY
)
1957 printk(KERN_WARNING
"ata%u is slow to respond, "
1958 "please be patient\n", ap
->id
);
1960 timeout
= timer_start
+ tmout
;
1961 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1963 status
= ata_chk_status(ap
);
1966 if (status
& ATA_BUSY
) {
1967 printk(KERN_ERR
"ata%u failed to respond (%lu secs)\n",
1968 ap
->id
, tmout
/ HZ
);
1975 static void ata_bus_post_reset(struct ata_port
*ap
, unsigned int devmask
)
1977 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1978 unsigned int dev0
= devmask
& (1 << 0);
1979 unsigned int dev1
= devmask
& (1 << 1);
1980 unsigned long timeout
;
1982 /* if device 0 was found in ata_devchk, wait for its
1986 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1988 /* if device 1 was found in ata_devchk, wait for
1989 * register access, then wait for BSY to clear
1991 timeout
= jiffies
+ ATA_TMOUT_BOOT
;
1995 ap
->ops
->dev_select(ap
, 1);
1996 if (ap
->flags
& ATA_FLAG_MMIO
) {
1997 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
1998 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
2000 nsect
= inb(ioaddr
->nsect_addr
);
2001 lbal
= inb(ioaddr
->lbal_addr
);
2003 if ((nsect
== 1) && (lbal
== 1))
2005 if (time_after(jiffies
, timeout
)) {
2009 msleep(50); /* give drive a breather */
2012 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
2014 /* is all this really necessary? */
2015 ap
->ops
->dev_select(ap
, 0);
2017 ap
->ops
->dev_select(ap
, 1);
2019 ap
->ops
->dev_select(ap
, 0);
2023 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2024 * @ap: Port to reset and probe
2026 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2027 * probe the bus. Not often used these days.
2030 * PCI/etc. bus probe sem.
2031 * Obtains host_set lock.
2035 static unsigned int ata_bus_edd(struct ata_port
*ap
)
2037 struct ata_taskfile tf
;
2038 unsigned long flags
;
2040 /* set up execute-device-diag (bus reset) taskfile */
2041 /* also, take interrupts to a known state (disabled) */
2042 DPRINTK("execute-device-diag\n");
2043 ata_tf_init(ap
, &tf
, 0);
2045 tf
.command
= ATA_CMD_EDD
;
2046 tf
.protocol
= ATA_PROT_NODATA
;
2049 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2050 ata_tf_to_host(ap
, &tf
);
2051 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2053 /* spec says at least 2ms. but who knows with those
2054 * crazy ATAPI devices...
2058 return ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
2061 static unsigned int ata_bus_softreset(struct ata_port
*ap
,
2062 unsigned int devmask
)
2064 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
2066 DPRINTK("ata%u: bus reset via SRST\n", ap
->id
);
2068 /* software reset. causes dev0 to be selected */
2069 if (ap
->flags
& ATA_FLAG_MMIO
) {
2070 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2071 udelay(20); /* FIXME: flush */
2072 writeb(ap
->ctl
| ATA_SRST
, (void __iomem
*) ioaddr
->ctl_addr
);
2073 udelay(20); /* FIXME: flush */
2074 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2076 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2078 outb(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
2080 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2083 /* spec mandates ">= 2ms" before checking status.
2084 * We wait 150ms, because that was the magic delay used for
2085 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2086 * between when the ATA command register is written, and then
2087 * status is checked. Because waiting for "a while" before
2088 * checking status is fine, post SRST, we perform this magic
2089 * delay here as well.
2093 ata_bus_post_reset(ap
, devmask
);
2099 * ata_bus_reset - reset host port and associated ATA channel
2100 * @ap: port to reset
2102 * This is typically the first time we actually start issuing
2103 * commands to the ATA channel. We wait for BSY to clear, then
2104 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2105 * result. Determine what devices, if any, are on the channel
2106 * by looking at the device 0/1 error register. Look at the signature
2107 * stored in each device's taskfile registers, to determine if
2108 * the device is ATA or ATAPI.
2111 * PCI/etc. bus probe sem.
2112 * Obtains host_set lock.
2115 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2118 void ata_bus_reset(struct ata_port
*ap
)
2120 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
2121 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
2123 unsigned int dev0
, dev1
= 0, rc
= 0, devmask
= 0;
2125 DPRINTK("ENTER, host %u, port %u\n", ap
->id
, ap
->port_no
);
2127 /* determine if device 0/1 are present */
2128 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
2131 dev0
= ata_devchk(ap
, 0);
2133 dev1
= ata_devchk(ap
, 1);
2137 devmask
|= (1 << 0);
2139 devmask
|= (1 << 1);
2141 /* select device 0 again */
2142 ap
->ops
->dev_select(ap
, 0);
2144 /* issue bus reset */
2145 if (ap
->flags
& ATA_FLAG_SRST
)
2146 rc
= ata_bus_softreset(ap
, devmask
);
2147 else if ((ap
->flags
& ATA_FLAG_SATA_RESET
) == 0) {
2148 /* set up device control */
2149 if (ap
->flags
& ATA_FLAG_MMIO
)
2150 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2152 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2153 rc
= ata_bus_edd(ap
);
2160 * determine by signature whether we have ATA or ATAPI devices
2162 err
= ata_dev_try_classify(ap
, 0);
2163 if ((slave_possible
) && (err
!= 0x81))
2164 ata_dev_try_classify(ap
, 1);
2166 /* re-enable interrupts */
2167 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
2170 /* is double-select really necessary? */
2171 if (ap
->device
[1].class != ATA_DEV_NONE
)
2172 ap
->ops
->dev_select(ap
, 1);
2173 if (ap
->device
[0].class != ATA_DEV_NONE
)
2174 ap
->ops
->dev_select(ap
, 0);
2176 /* if no devices were detected, disable this port */
2177 if ((ap
->device
[0].class == ATA_DEV_NONE
) &&
2178 (ap
->device
[1].class == ATA_DEV_NONE
))
2181 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
2182 /* set up device control for ATA_FLAG_SATA_RESET */
2183 if (ap
->flags
& ATA_FLAG_MMIO
)
2184 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2186 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2193 printk(KERN_ERR
"ata%u: disabling port\n", ap
->id
);
2194 ap
->ops
->port_disable(ap
);
2199 static void ata_pr_blacklisted(const struct ata_port
*ap
,
2200 const struct ata_device
*dev
)
2202 printk(KERN_WARNING
"ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2203 ap
->id
, dev
->devno
);
2206 static const char * const ata_dma_blacklist
[] = {
2225 "Toshiba CD-ROM XM-6202B",
2226 "TOSHIBA CD-ROM XM-1702BC",
2228 "E-IDE CD-ROM CR-840",
2231 "SAMSUNG CD-ROM SC-148C",
2232 "SAMSUNG CD-ROM SC",
2234 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2238 static int ata_dma_blacklisted(const struct ata_device
*dev
)
2240 unsigned char model_num
[40];
2245 ata_dev_id_string(dev
->id
, model_num
, ATA_ID_PROD_OFS
,
2248 len
= strnlen(s
, sizeof(model_num
));
2250 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2251 while ((len
> 0) && (s
[len
- 1] == ' ')) {
2256 for (i
= 0; i
< ARRAY_SIZE(ata_dma_blacklist
); i
++)
2257 if (!strncmp(ata_dma_blacklist
[i
], s
, len
))
2263 static unsigned int ata_get_mode_mask(const struct ata_port
*ap
, int shift
)
2265 const struct ata_device
*master
, *slave
;
2268 master
= &ap
->device
[0];
2269 slave
= &ap
->device
[1];
2271 assert (ata_dev_present(master
) || ata_dev_present(slave
));
2273 if (shift
== ATA_SHIFT_UDMA
) {
2274 mask
= ap
->udma_mask
;
2275 if (ata_dev_present(master
)) {
2276 mask
&= (master
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2277 if (ata_dma_blacklisted(master
)) {
2279 ata_pr_blacklisted(ap
, master
);
2282 if (ata_dev_present(slave
)) {
2283 mask
&= (slave
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2284 if (ata_dma_blacklisted(slave
)) {
2286 ata_pr_blacklisted(ap
, slave
);
2290 else if (shift
== ATA_SHIFT_MWDMA
) {
2291 mask
= ap
->mwdma_mask
;
2292 if (ata_dev_present(master
)) {
2293 mask
&= (master
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2294 if (ata_dma_blacklisted(master
)) {
2296 ata_pr_blacklisted(ap
, master
);
2299 if (ata_dev_present(slave
)) {
2300 mask
&= (slave
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2301 if (ata_dma_blacklisted(slave
)) {
2303 ata_pr_blacklisted(ap
, slave
);
2307 else if (shift
== ATA_SHIFT_PIO
) {
2308 mask
= ap
->pio_mask
;
2309 if (ata_dev_present(master
)) {
2310 /* spec doesn't return explicit support for
2311 * PIO0-2, so we fake it
2313 u16 tmp_mode
= master
->id
[ATA_ID_PIO_MODES
] & 0x03;
2318 if (ata_dev_present(slave
)) {
2319 /* spec doesn't return explicit support for
2320 * PIO0-2, so we fake it
2322 u16 tmp_mode
= slave
->id
[ATA_ID_PIO_MODES
] & 0x03;
2329 mask
= 0xffffffff; /* shut up compiler warning */
2336 /* find greatest bit */
2337 static int fgb(u32 bitmap
)
2342 for (i
= 0; i
< 32; i
++)
2343 if (bitmap
& (1 << i
))
2350 * ata_choose_xfer_mode - attempt to find best transfer mode
2351 * @ap: Port for which an xfer mode will be selected
2352 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2353 * @xfer_shift_out: (output) bit shift that selects this mode
2355 * Based on host and device capabilities, determine the
2356 * maximum transfer mode that is amenable to all.
2359 * PCI/etc. bus probe sem.
2362 * Zero on success, negative on error.
2365 static int ata_choose_xfer_mode(const struct ata_port
*ap
,
2367 unsigned int *xfer_shift_out
)
2369 unsigned int mask
, shift
;
2372 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++) {
2373 shift
= xfer_mode_classes
[i
].shift
;
2374 mask
= ata_get_mode_mask(ap
, shift
);
2378 *xfer_mode_out
= xfer_mode_classes
[i
].base
+ x
;
2379 *xfer_shift_out
= shift
;
2388 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2389 * @ap: Port associated with device @dev
2390 * @dev: Device to which command will be sent
2392 * Issue SET FEATURES - XFER MODE command to device @dev
2396 * PCI/etc. bus probe sem.
2399 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
)
2401 struct ata_taskfile tf
;
2403 /* set up set-features taskfile */
2404 DPRINTK("set features - xfer mode\n");
2406 ata_tf_init(ap
, &tf
, dev
->devno
);
2407 tf
.command
= ATA_CMD_SET_FEATURES
;
2408 tf
.feature
= SETFEATURES_XFER
;
2409 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2410 tf
.protocol
= ATA_PROT_NODATA
;
2411 tf
.nsect
= dev
->xfer_mode
;
2413 if (ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0)) {
2414 printk(KERN_ERR
"ata%u: failed to set xfermode, disabled\n",
2416 ata_port_disable(ap
);
2423 * ata_dev_reread_id - Reread the device identify device info
2424 * @ap: port where the device is
2425 * @dev: device to reread the identify device info
2430 static void ata_dev_reread_id(struct ata_port
*ap
, struct ata_device
*dev
)
2432 struct ata_taskfile tf
;
2434 ata_tf_init(ap
, &tf
, dev
->devno
);
2436 if (dev
->class == ATA_DEV_ATA
) {
2437 tf
.command
= ATA_CMD_ID_ATA
;
2438 DPRINTK("do ATA identify\n");
2440 tf
.command
= ATA_CMD_ID_ATAPI
;
2441 DPRINTK("do ATAPI identify\n");
2444 tf
.flags
|= ATA_TFLAG_DEVICE
;
2445 tf
.protocol
= ATA_PROT_PIO
;
2447 if (ata_exec_internal(ap
, dev
, &tf
, DMA_FROM_DEVICE
,
2448 dev
->id
, sizeof(dev
->id
)))
2451 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
2459 printk(KERN_ERR
"ata%u: failed to reread ID, disabled\n", ap
->id
);
2460 ata_port_disable(ap
);
2464 * ata_dev_init_params - Issue INIT DEV PARAMS command
2465 * @ap: Port associated with device @dev
2466 * @dev: Device to which command will be sent
2471 static void ata_dev_init_params(struct ata_port
*ap
, struct ata_device
*dev
)
2473 struct ata_taskfile tf
;
2474 u16 sectors
= dev
->id
[6];
2475 u16 heads
= dev
->id
[3];
2477 /* Number of sectors per track 1-255. Number of heads 1-16 */
2478 if (sectors
< 1 || sectors
> 255 || heads
< 1 || heads
> 16)
2481 /* set up init dev params taskfile */
2482 DPRINTK("init dev params \n");
2484 ata_tf_init(ap
, &tf
, dev
->devno
);
2485 tf
.command
= ATA_CMD_INIT_DEV_PARAMS
;
2486 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2487 tf
.protocol
= ATA_PROT_NODATA
;
2489 tf
.device
|= (heads
- 1) & 0x0f; /* max head = num. of heads - 1 */
2491 if (ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0)) {
2492 printk(KERN_ERR
"ata%u: failed to init parameters, disabled\n",
2494 ata_port_disable(ap
);
2501 * ata_sg_clean - Unmap DMA memory associated with command
2502 * @qc: Command containing DMA memory to be released
2504 * Unmap all mapped DMA memory associated with this command.
2507 * spin_lock_irqsave(host_set lock)
2510 static void ata_sg_clean(struct ata_queued_cmd
*qc
)
2512 struct ata_port
*ap
= qc
->ap
;
2513 struct scatterlist
*sg
= qc
->__sg
;
2514 int dir
= qc
->dma_dir
;
2515 void *pad_buf
= NULL
;
2517 assert(qc
->flags
& ATA_QCFLAG_DMAMAP
);
2520 if (qc
->flags
& ATA_QCFLAG_SINGLE
)
2521 assert(qc
->n_elem
<= 1);
2523 VPRINTK("unmapping %u sg elements\n", qc
->n_elem
);
2525 /* if we padded the buffer out to 32-bit bound, and data
2526 * xfer direction is from-device, we must copy from the
2527 * pad buffer back into the supplied buffer
2529 if (qc
->pad_len
&& !(qc
->tf
.flags
& ATA_TFLAG_WRITE
))
2530 pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2532 if (qc
->flags
& ATA_QCFLAG_SG
) {
2534 dma_unmap_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2535 /* restore last sg */
2536 sg
[qc
->orig_n_elem
- 1].length
+= qc
->pad_len
;
2538 struct scatterlist
*psg
= &qc
->pad_sgent
;
2539 void *addr
= kmap_atomic(psg
->page
, KM_IRQ0
);
2540 memcpy(addr
+ psg
->offset
, pad_buf
, qc
->pad_len
);
2541 kunmap_atomic(addr
, KM_IRQ0
);
2545 dma_unmap_single(ap
->host_set
->dev
,
2546 sg_dma_address(&sg
[0]), sg_dma_len(&sg
[0]),
2549 sg
->length
+= qc
->pad_len
;
2551 memcpy(qc
->buf_virt
+ sg
->length
- qc
->pad_len
,
2552 pad_buf
, qc
->pad_len
);
2555 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
2560 * ata_fill_sg - Fill PCI IDE PRD table
2561 * @qc: Metadata associated with taskfile to be transferred
2563 * Fill PCI IDE PRD (scatter-gather) table with segments
2564 * associated with the current disk command.
2567 * spin_lock_irqsave(host_set lock)
2570 static void ata_fill_sg(struct ata_queued_cmd
*qc
)
2572 struct ata_port
*ap
= qc
->ap
;
2573 struct scatterlist
*sg
;
2576 assert(qc
->__sg
!= NULL
);
2577 assert(qc
->n_elem
> 0 || qc
->pad_len
> 0);
2580 ata_for_each_sg(sg
, qc
) {
2584 /* determine if physical DMA addr spans 64K boundary.
2585 * Note h/w doesn't support 64-bit, so we unconditionally
2586 * truncate dma_addr_t to u32.
2588 addr
= (u32
) sg_dma_address(sg
);
2589 sg_len
= sg_dma_len(sg
);
2592 offset
= addr
& 0xffff;
2594 if ((offset
+ sg_len
) > 0x10000)
2595 len
= 0x10000 - offset
;
2597 ap
->prd
[idx
].addr
= cpu_to_le32(addr
);
2598 ap
->prd
[idx
].flags_len
= cpu_to_le32(len
& 0xffff);
2599 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx
, addr
, len
);
2608 ap
->prd
[idx
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
2611 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2612 * @qc: Metadata associated with taskfile to check
2614 * Allow low-level driver to filter ATA PACKET commands, returning
2615 * a status indicating whether or not it is OK to use DMA for the
2616 * supplied PACKET command.
2619 * spin_lock_irqsave(host_set lock)
2621 * RETURNS: 0 when ATAPI DMA can be used
2624 int ata_check_atapi_dma(struct ata_queued_cmd
*qc
)
2626 struct ata_port
*ap
= qc
->ap
;
2627 int rc
= 0; /* Assume ATAPI DMA is OK by default */
2629 if (ap
->ops
->check_atapi_dma
)
2630 rc
= ap
->ops
->check_atapi_dma(qc
);
2635 * ata_qc_prep - Prepare taskfile for submission
2636 * @qc: Metadata associated with taskfile to be prepared
2638 * Prepare ATA taskfile for submission.
2641 * spin_lock_irqsave(host_set lock)
2643 void ata_qc_prep(struct ata_queued_cmd
*qc
)
2645 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
2652 * ata_sg_init_one - Associate command with memory buffer
2653 * @qc: Command to be associated
2654 * @buf: Memory buffer
2655 * @buflen: Length of memory buffer, in bytes.
2657 * Initialize the data-related elements of queued_cmd @qc
2658 * to point to a single memory buffer, @buf of byte length @buflen.
2661 * spin_lock_irqsave(host_set lock)
2664 void ata_sg_init_one(struct ata_queued_cmd
*qc
, void *buf
, unsigned int buflen
)
2666 struct scatterlist
*sg
;
2668 qc
->flags
|= ATA_QCFLAG_SINGLE
;
2670 memset(&qc
->sgent
, 0, sizeof(qc
->sgent
));
2671 qc
->__sg
= &qc
->sgent
;
2673 qc
->orig_n_elem
= 1;
2677 sg_init_one(sg
, buf
, buflen
);
2681 * ata_sg_init - Associate command with scatter-gather table.
2682 * @qc: Command to be associated
2683 * @sg: Scatter-gather table.
2684 * @n_elem: Number of elements in s/g table.
2686 * Initialize the data-related elements of queued_cmd @qc
2687 * to point to a scatter-gather table @sg, containing @n_elem
2691 * spin_lock_irqsave(host_set lock)
2694 void ata_sg_init(struct ata_queued_cmd
*qc
, struct scatterlist
*sg
,
2695 unsigned int n_elem
)
2697 qc
->flags
|= ATA_QCFLAG_SG
;
2699 qc
->n_elem
= n_elem
;
2700 qc
->orig_n_elem
= n_elem
;
2704 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2705 * @qc: Command with memory buffer to be mapped.
2707 * DMA-map the memory buffer associated with queued_cmd @qc.
2710 * spin_lock_irqsave(host_set lock)
2713 * Zero on success, negative on error.
2716 static int ata_sg_setup_one(struct ata_queued_cmd
*qc
)
2718 struct ata_port
*ap
= qc
->ap
;
2719 int dir
= qc
->dma_dir
;
2720 struct scatterlist
*sg
= qc
->__sg
;
2721 dma_addr_t dma_address
;
2724 /* we must lengthen transfers to end on a 32-bit boundary */
2725 qc
->pad_len
= sg
->length
& 3;
2727 void *pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2728 struct scatterlist
*psg
= &qc
->pad_sgent
;
2730 assert(qc
->dev
->class == ATA_DEV_ATAPI
);
2732 memset(pad_buf
, 0, ATA_DMA_PAD_SZ
);
2734 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
)
2735 memcpy(pad_buf
, qc
->buf_virt
+ sg
->length
- qc
->pad_len
,
2738 sg_dma_address(psg
) = ap
->pad_dma
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2739 sg_dma_len(psg
) = ATA_DMA_PAD_SZ
;
2741 sg
->length
-= qc
->pad_len
;
2742 if (sg
->length
== 0)
2745 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2746 sg
->length
, qc
->pad_len
);
2754 dma_address
= dma_map_single(ap
->host_set
->dev
, qc
->buf_virt
,
2756 if (dma_mapping_error(dma_address
)) {
2758 sg
->length
+= qc
->pad_len
;
2762 sg_dma_address(sg
) = dma_address
;
2763 sg_dma_len(sg
) = sg
->length
;
2766 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg
),
2767 qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2773 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2774 * @qc: Command with scatter-gather table to be mapped.
2776 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2779 * spin_lock_irqsave(host_set lock)
2782 * Zero on success, negative on error.
2786 static int ata_sg_setup(struct ata_queued_cmd
*qc
)
2788 struct ata_port
*ap
= qc
->ap
;
2789 struct scatterlist
*sg
= qc
->__sg
;
2790 struct scatterlist
*lsg
= &sg
[qc
->n_elem
- 1];
2791 int n_elem
, pre_n_elem
, dir
, trim_sg
= 0;
2793 VPRINTK("ENTER, ata%u\n", ap
->id
);
2794 assert(qc
->flags
& ATA_QCFLAG_SG
);
2796 /* we must lengthen transfers to end on a 32-bit boundary */
2797 qc
->pad_len
= lsg
->length
& 3;
2799 void *pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2800 struct scatterlist
*psg
= &qc
->pad_sgent
;
2801 unsigned int offset
;
2803 assert(qc
->dev
->class == ATA_DEV_ATAPI
);
2805 memset(pad_buf
, 0, ATA_DMA_PAD_SZ
);
2808 * psg->page/offset are used to copy to-be-written
2809 * data in this function or read data in ata_sg_clean.
2811 offset
= lsg
->offset
+ lsg
->length
- qc
->pad_len
;
2812 psg
->page
= nth_page(lsg
->page
, offset
>> PAGE_SHIFT
);
2813 psg
->offset
= offset_in_page(offset
);
2815 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
) {
2816 void *addr
= kmap_atomic(psg
->page
, KM_IRQ0
);
2817 memcpy(pad_buf
, addr
+ psg
->offset
, qc
->pad_len
);
2818 kunmap_atomic(addr
, KM_IRQ0
);
2821 sg_dma_address(psg
) = ap
->pad_dma
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2822 sg_dma_len(psg
) = ATA_DMA_PAD_SZ
;
2824 lsg
->length
-= qc
->pad_len
;
2825 if (lsg
->length
== 0)
2828 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2829 qc
->n_elem
- 1, lsg
->length
, qc
->pad_len
);
2832 pre_n_elem
= qc
->n_elem
;
2833 if (trim_sg
&& pre_n_elem
)
2842 n_elem
= dma_map_sg(ap
->host_set
->dev
, sg
, pre_n_elem
, dir
);
2844 /* restore last sg */
2845 lsg
->length
+= qc
->pad_len
;
2849 DPRINTK("%d sg elements mapped\n", n_elem
);
2852 qc
->n_elem
= n_elem
;
2858 * ata_poll_qc_complete - turn irq back on and finish qc
2859 * @qc: Command to complete
2860 * @err_mask: ATA status register content
2863 * None. (grabs host lock)
2866 void ata_poll_qc_complete(struct ata_queued_cmd
*qc
)
2868 struct ata_port
*ap
= qc
->ap
;
2869 unsigned long flags
;
2871 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2872 ap
->flags
&= ~ATA_FLAG_NOINTR
;
2874 ata_qc_complete(qc
);
2875 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2880 * @ap: the target ata_port
2883 * None. (executing in kernel thread context)
2886 * timeout value to use
2889 static unsigned long ata_pio_poll(struct ata_port
*ap
)
2891 struct ata_queued_cmd
*qc
;
2893 unsigned int poll_state
= HSM_ST_UNKNOWN
;
2894 unsigned int reg_state
= HSM_ST_UNKNOWN
;
2896 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2899 switch (ap
->hsm_task_state
) {
2902 poll_state
= HSM_ST_POLL
;
2906 case HSM_ST_LAST_POLL
:
2907 poll_state
= HSM_ST_LAST_POLL
;
2908 reg_state
= HSM_ST_LAST
;
2915 status
= ata_chk_status(ap
);
2916 if (status
& ATA_BUSY
) {
2917 if (time_after(jiffies
, ap
->pio_task_timeout
)) {
2918 qc
->err_mask
|= AC_ERR_ATA_BUS
;
2919 ap
->hsm_task_state
= HSM_ST_TMOUT
;
2922 ap
->hsm_task_state
= poll_state
;
2923 return ATA_SHORT_PAUSE
;
2926 ap
->hsm_task_state
= reg_state
;
2931 * ata_pio_complete - check if drive is busy or idle
2932 * @ap: the target ata_port
2935 * None. (executing in kernel thread context)
2938 * Non-zero if qc completed, zero otherwise.
2941 static int ata_pio_complete (struct ata_port
*ap
)
2943 struct ata_queued_cmd
*qc
;
2947 * This is purely heuristic. This is a fast path. Sometimes when
2948 * we enter, BSY will be cleared in a chk-status or two. If not,
2949 * the drive is probably seeking or something. Snooze for a couple
2950 * msecs, then chk-status again. If still busy, fall back to
2951 * HSM_ST_POLL state.
2953 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
, 10);
2954 if (drv_stat
& ATA_BUSY
) {
2956 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
, 10);
2957 if (drv_stat
& ATA_BUSY
) {
2958 ap
->hsm_task_state
= HSM_ST_LAST_POLL
;
2959 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
2964 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2967 drv_stat
= ata_wait_idle(ap
);
2968 if (!ata_ok(drv_stat
)) {
2969 qc
->err_mask
|= __ac_err_mask(drv_stat
);
2970 ap
->hsm_task_state
= HSM_ST_ERR
;
2974 ap
->hsm_task_state
= HSM_ST_IDLE
;
2976 assert(qc
->err_mask
== 0);
2977 ata_poll_qc_complete(qc
);
2979 /* another command may start at this point */
2986 * swap_buf_le16 - swap halves of 16-words in place
2987 * @buf: Buffer to swap
2988 * @buf_words: Number of 16-bit words in buffer.
2990 * Swap halves of 16-bit words if needed to convert from
2991 * little-endian byte order to native cpu byte order, or
2995 * Inherited from caller.
2997 void swap_buf_le16(u16
*buf
, unsigned int buf_words
)
3002 for (i
= 0; i
< buf_words
; i
++)
3003 buf
[i
] = le16_to_cpu(buf
[i
]);
3004 #endif /* __BIG_ENDIAN */
3008 * ata_mmio_data_xfer - Transfer data by MMIO
3009 * @ap: port to read/write
3011 * @buflen: buffer length
3012 * @write_data: read/write
3014 * Transfer data from/to the device data register by MMIO.
3017 * Inherited from caller.
3020 static void ata_mmio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3021 unsigned int buflen
, int write_data
)
3024 unsigned int words
= buflen
>> 1;
3025 u16
*buf16
= (u16
*) buf
;
3026 void __iomem
*mmio
= (void __iomem
*)ap
->ioaddr
.data_addr
;
3028 /* Transfer multiple of 2 bytes */
3030 for (i
= 0; i
< words
; i
++)
3031 writew(le16_to_cpu(buf16
[i
]), mmio
);
3033 for (i
= 0; i
< words
; i
++)
3034 buf16
[i
] = cpu_to_le16(readw(mmio
));
3037 /* Transfer trailing 1 byte, if any. */
3038 if (unlikely(buflen
& 0x01)) {
3039 u16 align_buf
[1] = { 0 };
3040 unsigned char *trailing_buf
= buf
+ buflen
- 1;
3043 memcpy(align_buf
, trailing_buf
, 1);
3044 writew(le16_to_cpu(align_buf
[0]), mmio
);
3046 align_buf
[0] = cpu_to_le16(readw(mmio
));
3047 memcpy(trailing_buf
, align_buf
, 1);
3053 * ata_pio_data_xfer - Transfer data by PIO
3054 * @ap: port to read/write
3056 * @buflen: buffer length
3057 * @write_data: read/write
3059 * Transfer data from/to the device data register by PIO.
3062 * Inherited from caller.
3065 static void ata_pio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3066 unsigned int buflen
, int write_data
)
3068 unsigned int words
= buflen
>> 1;
3070 /* Transfer multiple of 2 bytes */
3072 outsw(ap
->ioaddr
.data_addr
, buf
, words
);
3074 insw(ap
->ioaddr
.data_addr
, buf
, words
);
3076 /* Transfer trailing 1 byte, if any. */
3077 if (unlikely(buflen
& 0x01)) {
3078 u16 align_buf
[1] = { 0 };
3079 unsigned char *trailing_buf
= buf
+ buflen
- 1;
3082 memcpy(align_buf
, trailing_buf
, 1);
3083 outw(le16_to_cpu(align_buf
[0]), ap
->ioaddr
.data_addr
);
3085 align_buf
[0] = cpu_to_le16(inw(ap
->ioaddr
.data_addr
));
3086 memcpy(trailing_buf
, align_buf
, 1);
3092 * ata_data_xfer - Transfer data from/to the data register.
3093 * @ap: port to read/write
3095 * @buflen: buffer length
3096 * @do_write: read/write
3098 * Transfer data from/to the device data register.
3101 * Inherited from caller.
3104 static void ata_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3105 unsigned int buflen
, int do_write
)
3107 /* Make the crap hardware pay the costs not the good stuff */
3108 if (unlikely(ap
->flags
& ATA_FLAG_IRQ_MASK
)) {
3109 unsigned long flags
;
3110 local_irq_save(flags
);
3111 if (ap
->flags
& ATA_FLAG_MMIO
)
3112 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
3114 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
3115 local_irq_restore(flags
);
3117 if (ap
->flags
& ATA_FLAG_MMIO
)
3118 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
3120 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
3125 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3126 * @qc: Command on going
3128 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3131 * Inherited from caller.
3134 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
3136 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3137 struct scatterlist
*sg
= qc
->__sg
;
3138 struct ata_port
*ap
= qc
->ap
;
3140 unsigned int offset
;
3143 if (qc
->cursect
== (qc
->nsect
- 1))
3144 ap
->hsm_task_state
= HSM_ST_LAST
;
3146 page
= sg
[qc
->cursg
].page
;
3147 offset
= sg
[qc
->cursg
].offset
+ qc
->cursg_ofs
* ATA_SECT_SIZE
;
3149 /* get the current page and offset */
3150 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
3151 offset
%= PAGE_SIZE
;
3153 buf
= kmap(page
) + offset
;
3158 if ((qc
->cursg_ofs
* ATA_SECT_SIZE
) == (&sg
[qc
->cursg
])->length
) {
3163 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
3165 /* do the actual data transfer */
3166 do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3167 ata_data_xfer(ap
, buf
, ATA_SECT_SIZE
, do_write
);
3173 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3174 * @qc: Command on going
3175 * @bytes: number of bytes
3177 * Transfer Transfer data from/to the ATAPI device.
3180 * Inherited from caller.
3184 static void __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
3186 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3187 struct scatterlist
*sg
= qc
->__sg
;
3188 struct ata_port
*ap
= qc
->ap
;
3191 unsigned int offset
, count
;
3193 if (qc
->curbytes
+ bytes
>= qc
->nbytes
)
3194 ap
->hsm_task_state
= HSM_ST_LAST
;
3197 if (unlikely(qc
->cursg
>= qc
->n_elem
)) {
3199 * The end of qc->sg is reached and the device expects
3200 * more data to transfer. In order not to overrun qc->sg
3201 * and fulfill length specified in the byte count register,
3202 * - for read case, discard trailing data from the device
3203 * - for write case, padding zero data to the device
3205 u16 pad_buf
[1] = { 0 };
3206 unsigned int words
= bytes
>> 1;
3209 if (words
) /* warning if bytes > 1 */
3210 printk(KERN_WARNING
"ata%u: %u bytes trailing data\n",
3213 for (i
= 0; i
< words
; i
++)
3214 ata_data_xfer(ap
, (unsigned char*)pad_buf
, 2, do_write
);
3216 ap
->hsm_task_state
= HSM_ST_LAST
;
3220 sg
= &qc
->__sg
[qc
->cursg
];
3223 offset
= sg
->offset
+ qc
->cursg_ofs
;
3225 /* get the current page and offset */
3226 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
3227 offset
%= PAGE_SIZE
;
3229 /* don't overrun current sg */
3230 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
3232 /* don't cross page boundaries */
3233 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
3235 buf
= kmap(page
) + offset
;
3238 qc
->curbytes
+= count
;
3239 qc
->cursg_ofs
+= count
;
3241 if (qc
->cursg_ofs
== sg
->length
) {
3246 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
3248 /* do the actual data transfer */
3249 ata_data_xfer(ap
, buf
, count
, do_write
);
3258 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3259 * @qc: Command on going
3261 * Transfer Transfer data from/to the ATAPI device.
3264 * Inherited from caller.
3267 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
3269 struct ata_port
*ap
= qc
->ap
;
3270 struct ata_device
*dev
= qc
->dev
;
3271 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
3272 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
3274 ap
->ops
->tf_read(ap
, &qc
->tf
);
3275 ireason
= qc
->tf
.nsect
;
3276 bc_lo
= qc
->tf
.lbam
;
3277 bc_hi
= qc
->tf
.lbah
;
3278 bytes
= (bc_hi
<< 8) | bc_lo
;
3280 /* shall be cleared to zero, indicating xfer of data */
3281 if (ireason
& (1 << 0))
3284 /* make sure transfer direction matches expected */
3285 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
3286 if (do_write
!= i_write
)
3289 __atapi_pio_bytes(qc
, bytes
);
3294 printk(KERN_INFO
"ata%u: dev %u: ATAPI check failed\n",
3295 ap
->id
, dev
->devno
);
3296 qc
->err_mask
|= AC_ERR_ATA_BUS
;
3297 ap
->hsm_task_state
= HSM_ST_ERR
;
3301 * ata_pio_block - start PIO on a block
3302 * @ap: the target ata_port
3305 * None. (executing in kernel thread context)
3308 static void ata_pio_block(struct ata_port
*ap
)
3310 struct ata_queued_cmd
*qc
;
3314 * This is purely heuristic. This is a fast path.
3315 * Sometimes when we enter, BSY will be cleared in
3316 * a chk-status or two. If not, the drive is probably seeking
3317 * or something. Snooze for a couple msecs, then
3318 * chk-status again. If still busy, fall back to
3319 * HSM_ST_POLL state.
3321 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
3322 if (status
& ATA_BUSY
) {
3324 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
3325 if (status
& ATA_BUSY
) {
3326 ap
->hsm_task_state
= HSM_ST_POLL
;
3327 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
3332 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3336 if (status
& (ATA_ERR
| ATA_DF
)) {
3337 qc
->err_mask
|= AC_ERR_DEV
;
3338 ap
->hsm_task_state
= HSM_ST_ERR
;
3342 /* transfer data if any */
3343 if (is_atapi_taskfile(&qc
->tf
)) {
3344 /* DRQ=0 means no more data to transfer */
3345 if ((status
& ATA_DRQ
) == 0) {
3346 ap
->hsm_task_state
= HSM_ST_LAST
;
3350 atapi_pio_bytes(qc
);
3352 /* handle BSY=0, DRQ=0 as error */
3353 if ((status
& ATA_DRQ
) == 0) {
3354 qc
->err_mask
|= AC_ERR_ATA_BUS
;
3355 ap
->hsm_task_state
= HSM_ST_ERR
;
3363 static void ata_pio_error(struct ata_port
*ap
)
3365 struct ata_queued_cmd
*qc
;
3367 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3370 if (qc
->tf
.command
!= ATA_CMD_PACKET
)
3371 printk(KERN_WARNING
"ata%u: PIO error\n", ap
->id
);
3373 /* make sure qc->err_mask is available to
3374 * know what's wrong and recover
3376 assert(qc
->err_mask
);
3378 ap
->hsm_task_state
= HSM_ST_IDLE
;
3380 ata_poll_qc_complete(qc
);
3383 static void ata_pio_task(void *_data
)
3385 struct ata_port
*ap
= _data
;
3386 unsigned long timeout
;
3393 switch (ap
->hsm_task_state
) {
3402 qc_completed
= ata_pio_complete(ap
);
3406 case HSM_ST_LAST_POLL
:
3407 timeout
= ata_pio_poll(ap
);
3417 queue_delayed_work(ata_wq
, &ap
->pio_task
, timeout
);
3418 else if (!qc_completed
)
3423 * ata_qc_timeout - Handle timeout of queued command
3424 * @qc: Command that timed out
3426 * Some part of the kernel (currently, only the SCSI layer)
3427 * has noticed that the active command on port @ap has not
3428 * completed after a specified length of time. Handle this
3429 * condition by disabling DMA (if necessary) and completing
3430 * transactions, with error if necessary.
3432 * This also handles the case of the "lost interrupt", where
3433 * for some reason (possibly hardware bug, possibly driver bug)
3434 * an interrupt was not delivered to the driver, even though the
3435 * transaction completed successfully.
3438 * Inherited from SCSI layer (none, can sleep)
3441 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
3443 struct ata_port
*ap
= qc
->ap
;
3444 struct ata_host_set
*host_set
= ap
->host_set
;
3445 u8 host_stat
= 0, drv_stat
;
3446 unsigned long flags
;
3450 spin_lock_irqsave(&host_set
->lock
, flags
);
3452 /* hack alert! We cannot use the supplied completion
3453 * function from inside the ->eh_strategy_handler() thread.
3454 * libata is the only user of ->eh_strategy_handler() in
3455 * any kernel, so the default scsi_done() assumes it is
3456 * not being called from the SCSI EH.
3458 qc
->scsidone
= scsi_finish_command
;
3460 switch (qc
->tf
.protocol
) {
3463 case ATA_PROT_ATAPI_DMA
:
3464 host_stat
= ap
->ops
->bmdma_status(ap
);
3466 /* before we do anything else, clear DMA-Start bit */
3467 ap
->ops
->bmdma_stop(qc
);
3473 drv_stat
= ata_chk_status(ap
);
3475 /* ack bmdma irq events */
3476 ap
->ops
->irq_clear(ap
);
3478 printk(KERN_ERR
"ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3479 ap
->id
, qc
->tf
.command
, drv_stat
, host_stat
);
3481 /* complete taskfile transaction */
3482 qc
->err_mask
|= ac_err_mask(drv_stat
);
3483 ata_qc_complete(qc
);
3487 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3493 * ata_eng_timeout - Handle timeout of queued command
3494 * @ap: Port on which timed-out command is active
3496 * Some part of the kernel (currently, only the SCSI layer)
3497 * has noticed that the active command on port @ap has not
3498 * completed after a specified length of time. Handle this
3499 * condition by disabling DMA (if necessary) and completing
3500 * transactions, with error if necessary.
3502 * This also handles the case of the "lost interrupt", where
3503 * for some reason (possibly hardware bug, possibly driver bug)
3504 * an interrupt was not delivered to the driver, even though the
3505 * transaction completed successfully.
3508 * Inherited from SCSI layer (none, can sleep)
3511 void ata_eng_timeout(struct ata_port
*ap
)
3513 struct ata_queued_cmd
*qc
;
3517 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3521 printk(KERN_ERR
"ata%u: BUG: timeout without command\n",
3531 * ata_qc_new - Request an available ATA command, for queueing
3532 * @ap: Port associated with device @dev
3533 * @dev: Device from whom we request an available command structure
3539 static struct ata_queued_cmd
*ata_qc_new(struct ata_port
*ap
)
3541 struct ata_queued_cmd
*qc
= NULL
;
3544 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++)
3545 if (!test_and_set_bit(i
, &ap
->qactive
)) {
3546 qc
= ata_qc_from_tag(ap
, i
);
3557 * ata_qc_new_init - Request an available ATA command, and initialize it
3558 * @ap: Port associated with device @dev
3559 * @dev: Device from whom we request an available command structure
3565 struct ata_queued_cmd
*ata_qc_new_init(struct ata_port
*ap
,
3566 struct ata_device
*dev
)
3568 struct ata_queued_cmd
*qc
;
3570 qc
= ata_qc_new(ap
);
3582 static void __ata_qc_complete(struct ata_queued_cmd
*qc
)
3584 struct ata_port
*ap
= qc
->ap
;
3589 if (likely(ata_tag_valid(tag
))) {
3590 if (tag
== ap
->active_tag
)
3591 ap
->active_tag
= ATA_TAG_POISON
;
3592 qc
->tag
= ATA_TAG_POISON
;
3593 clear_bit(tag
, &ap
->qactive
);
3598 * ata_qc_free - free unused ata_queued_cmd
3599 * @qc: Command to complete
3601 * Designed to free unused ata_queued_cmd object
3602 * in case something prevents using it.
3605 * spin_lock_irqsave(host_set lock)
3607 void ata_qc_free(struct ata_queued_cmd
*qc
)
3609 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3611 __ata_qc_complete(qc
);
3615 * ata_qc_complete - Complete an active ATA command
3616 * @qc: Command to complete
3617 * @err_mask: ATA Status register contents
3619 * Indicate to the mid and upper layers that an ATA
3620 * command has completed, with either an ok or not-ok status.
3623 * spin_lock_irqsave(host_set lock)
3626 void ata_qc_complete(struct ata_queued_cmd
*qc
)
3630 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3631 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
3633 if (likely(qc
->flags
& ATA_QCFLAG_DMAMAP
))
3636 /* atapi: mark qc as inactive to prevent the interrupt handler
3637 * from completing the command twice later, before the error handler
3638 * is called. (when rc != 0 and atapi request sense is needed)
3640 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
3642 /* call completion callback */
3643 rc
= qc
->complete_fn(qc
);
3645 /* if callback indicates not to complete command (non-zero),
3646 * return immediately
3651 __ata_qc_complete(qc
);
3656 static inline int ata_should_dma_map(struct ata_queued_cmd
*qc
)
3658 struct ata_port
*ap
= qc
->ap
;
3660 switch (qc
->tf
.protocol
) {
3662 case ATA_PROT_ATAPI_DMA
:
3665 case ATA_PROT_ATAPI
:
3667 case ATA_PROT_PIO_MULT
:
3668 if (ap
->flags
& ATA_FLAG_PIO_DMA
)
3681 * ata_qc_issue - issue taskfile to device
3682 * @qc: command to issue to device
3684 * Prepare an ATA command to submission to device.
3685 * This includes mapping the data into a DMA-able
3686 * area, filling in the S/G table, and finally
3687 * writing the taskfile to hardware, starting the command.
3690 * spin_lock_irqsave(host_set lock)
3693 * Zero on success, negative on error.
3696 int ata_qc_issue(struct ata_queued_cmd
*qc
)
3698 struct ata_port
*ap
= qc
->ap
;
3700 if (ata_should_dma_map(qc
)) {
3701 if (qc
->flags
& ATA_QCFLAG_SG
) {
3702 if (ata_sg_setup(qc
))
3704 } else if (qc
->flags
& ATA_QCFLAG_SINGLE
) {
3705 if (ata_sg_setup_one(qc
))
3709 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
3712 ap
->ops
->qc_prep(qc
);
3714 qc
->ap
->active_tag
= qc
->tag
;
3715 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
3717 return ap
->ops
->qc_issue(qc
);
3725 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3726 * @qc: command to issue to device
3728 * Using various libata functions and hooks, this function
3729 * starts an ATA command. ATA commands are grouped into
3730 * classes called "protocols", and issuing each type of protocol
3731 * is slightly different.
3733 * May be used as the qc_issue() entry in ata_port_operations.
3736 * spin_lock_irqsave(host_set lock)
3739 * Zero on success, negative on error.
3742 int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
3744 struct ata_port
*ap
= qc
->ap
;
3746 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
3748 switch (qc
->tf
.protocol
) {
3749 case ATA_PROT_NODATA
:
3750 ata_tf_to_host(ap
, &qc
->tf
);
3754 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3755 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3756 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3759 case ATA_PROT_PIO
: /* load tf registers, initiate polling pio */
3760 ata_qc_set_polling(qc
);
3761 ata_tf_to_host(ap
, &qc
->tf
);
3762 ap
->hsm_task_state
= HSM_ST
;
3763 queue_work(ata_wq
, &ap
->pio_task
);
3766 case ATA_PROT_ATAPI
:
3767 ata_qc_set_polling(qc
);
3768 ata_tf_to_host(ap
, &qc
->tf
);
3769 queue_work(ata_wq
, &ap
->packet_task
);
3772 case ATA_PROT_ATAPI_NODATA
:
3773 ap
->flags
|= ATA_FLAG_NOINTR
;
3774 ata_tf_to_host(ap
, &qc
->tf
);
3775 queue_work(ata_wq
, &ap
->packet_task
);
3778 case ATA_PROT_ATAPI_DMA
:
3779 ap
->flags
|= ATA_FLAG_NOINTR
;
3780 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3781 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3782 queue_work(ata_wq
, &ap
->packet_task
);
3794 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3795 * @qc: Info associated with this ATA transaction.
3798 * spin_lock_irqsave(host_set lock)
3801 static void ata_bmdma_setup_mmio (struct ata_queued_cmd
*qc
)
3803 struct ata_port
*ap
= qc
->ap
;
3804 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3806 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3808 /* load PRD table addr. */
3809 mb(); /* make sure PRD table writes are visible to controller */
3810 writel(ap
->prd_dma
, mmio
+ ATA_DMA_TABLE_OFS
);
3812 /* specify data direction, triple-check start bit is clear */
3813 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3814 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3816 dmactl
|= ATA_DMA_WR
;
3817 writeb(dmactl
, mmio
+ ATA_DMA_CMD
);
3819 /* issue r/w command */
3820 ap
->ops
->exec_command(ap
, &qc
->tf
);
3824 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3825 * @qc: Info associated with this ATA transaction.
3828 * spin_lock_irqsave(host_set lock)
3831 static void ata_bmdma_start_mmio (struct ata_queued_cmd
*qc
)
3833 struct ata_port
*ap
= qc
->ap
;
3834 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3837 /* start host DMA transaction */
3838 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3839 writeb(dmactl
| ATA_DMA_START
, mmio
+ ATA_DMA_CMD
);
3841 /* Strictly, one may wish to issue a readb() here, to
3842 * flush the mmio write. However, control also passes
3843 * to the hardware at this point, and it will interrupt
3844 * us when we are to resume control. So, in effect,
3845 * we don't care when the mmio write flushes.
3846 * Further, a read of the DMA status register _immediately_
3847 * following the write may not be what certain flaky hardware
3848 * is expected, so I think it is best to not add a readb()
3849 * without first all the MMIO ATA cards/mobos.
3850 * Or maybe I'm just being paranoid.
3855 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3856 * @qc: Info associated with this ATA transaction.
3859 * spin_lock_irqsave(host_set lock)
3862 static void ata_bmdma_setup_pio (struct ata_queued_cmd
*qc
)
3864 struct ata_port
*ap
= qc
->ap
;
3865 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3868 /* load PRD table addr. */
3869 outl(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
3871 /* specify data direction, triple-check start bit is clear */
3872 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3873 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3875 dmactl
|= ATA_DMA_WR
;
3876 outb(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3878 /* issue r/w command */
3879 ap
->ops
->exec_command(ap
, &qc
->tf
);
3883 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3884 * @qc: Info associated with this ATA transaction.
3887 * spin_lock_irqsave(host_set lock)
3890 static void ata_bmdma_start_pio (struct ata_queued_cmd
*qc
)
3892 struct ata_port
*ap
= qc
->ap
;
3895 /* start host DMA transaction */
3896 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3897 outb(dmactl
| ATA_DMA_START
,
3898 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3903 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3904 * @qc: Info associated with this ATA transaction.
3906 * Writes the ATA_DMA_START flag to the DMA command register.
3908 * May be used as the bmdma_start() entry in ata_port_operations.
3911 * spin_lock_irqsave(host_set lock)
3913 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
3915 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3916 ata_bmdma_start_mmio(qc
);
3918 ata_bmdma_start_pio(qc
);
3923 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3924 * @qc: Info associated with this ATA transaction.
3926 * Writes address of PRD table to device's PRD Table Address
3927 * register, sets the DMA control register, and calls
3928 * ops->exec_command() to start the transfer.
3930 * May be used as the bmdma_setup() entry in ata_port_operations.
3933 * spin_lock_irqsave(host_set lock)
3935 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
3937 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3938 ata_bmdma_setup_mmio(qc
);
3940 ata_bmdma_setup_pio(qc
);
3945 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3946 * @ap: Port associated with this ATA transaction.
3948 * Clear interrupt and error flags in DMA status register.
3950 * May be used as the irq_clear() entry in ata_port_operations.
3953 * spin_lock_irqsave(host_set lock)
3956 void ata_bmdma_irq_clear(struct ata_port
*ap
)
3958 if (ap
->flags
& ATA_FLAG_MMIO
) {
3959 void __iomem
*mmio
= ((void __iomem
*) ap
->ioaddr
.bmdma_addr
) + ATA_DMA_STATUS
;
3960 writeb(readb(mmio
), mmio
);
3962 unsigned long addr
= ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
;
3963 outb(inb(addr
), addr
);
3970 * ata_bmdma_status - Read PCI IDE BMDMA status
3971 * @ap: Port associated with this ATA transaction.
3973 * Read and return BMDMA status register.
3975 * May be used as the bmdma_status() entry in ata_port_operations.
3978 * spin_lock_irqsave(host_set lock)
3981 u8
ata_bmdma_status(struct ata_port
*ap
)
3984 if (ap
->flags
& ATA_FLAG_MMIO
) {
3985 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3986 host_stat
= readb(mmio
+ ATA_DMA_STATUS
);
3988 host_stat
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
3994 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3995 * @qc: Command we are ending DMA for
3997 * Clears the ATA_DMA_START flag in the dma control register
3999 * May be used as the bmdma_stop() entry in ata_port_operations.
4002 * spin_lock_irqsave(host_set lock)
4005 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
4007 struct ata_port
*ap
= qc
->ap
;
4008 if (ap
->flags
& ATA_FLAG_MMIO
) {
4009 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
4011 /* clear start/stop bit */
4012 writeb(readb(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
4013 mmio
+ ATA_DMA_CMD
);
4015 /* clear start/stop bit */
4016 outb(inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
4017 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
4020 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4021 ata_altstatus(ap
); /* dummy read */
4025 * ata_host_intr - Handle host interrupt for given (port, task)
4026 * @ap: Port on which interrupt arrived (possibly...)
4027 * @qc: Taskfile currently active in engine
4029 * Handle host interrupt for given queued command. Currently,
4030 * only DMA interrupts are handled. All other commands are
4031 * handled via polling with interrupts disabled (nIEN bit).
4034 * spin_lock_irqsave(host_set lock)
4037 * One if interrupt was handled, zero if not (shared irq).
4040 inline unsigned int ata_host_intr (struct ata_port
*ap
,
4041 struct ata_queued_cmd
*qc
)
4043 u8 status
, host_stat
;
4045 switch (qc
->tf
.protocol
) {
4048 case ATA_PROT_ATAPI_DMA
:
4049 case ATA_PROT_ATAPI
:
4050 /* check status of DMA engine */
4051 host_stat
= ap
->ops
->bmdma_status(ap
);
4052 VPRINTK("ata%u: host_stat 0x%X\n", ap
->id
, host_stat
);
4054 /* if it's not our irq... */
4055 if (!(host_stat
& ATA_DMA_INTR
))
4058 /* before we do anything else, clear DMA-Start bit */
4059 ap
->ops
->bmdma_stop(qc
);
4063 case ATA_PROT_ATAPI_NODATA
:
4064 case ATA_PROT_NODATA
:
4065 /* check altstatus */
4066 status
= ata_altstatus(ap
);
4067 if (status
& ATA_BUSY
)
4070 /* check main status, clearing INTRQ */
4071 status
= ata_chk_status(ap
);
4072 if (unlikely(status
& ATA_BUSY
))
4074 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4075 ap
->id
, qc
->tf
.protocol
, status
);
4077 /* ack bmdma irq events */
4078 ap
->ops
->irq_clear(ap
);
4080 /* complete taskfile transaction */
4081 qc
->err_mask
|= ac_err_mask(status
);
4082 ata_qc_complete(qc
);
4089 return 1; /* irq handled */
4092 ap
->stats
.idle_irq
++;
4095 if ((ap
->stats
.idle_irq
% 1000) == 0) {
4097 ata_irq_ack(ap
, 0); /* debug trap */
4098 printk(KERN_WARNING
"ata%d: irq trap\n", ap
->id
);
4101 return 0; /* irq not handled */
4105 * ata_interrupt - Default ATA host interrupt handler
4106 * @irq: irq line (unused)
4107 * @dev_instance: pointer to our ata_host_set information structure
4110 * Default interrupt handler for PCI IDE devices. Calls
4111 * ata_host_intr() for each port that is not disabled.
4114 * Obtains host_set lock during operation.
4117 * IRQ_NONE or IRQ_HANDLED.
4120 irqreturn_t
ata_interrupt (int irq
, void *dev_instance
, struct pt_regs
*regs
)
4122 struct ata_host_set
*host_set
= dev_instance
;
4124 unsigned int handled
= 0;
4125 unsigned long flags
;
4127 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4128 spin_lock_irqsave(&host_set
->lock
, flags
);
4130 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4131 struct ata_port
*ap
;
4133 ap
= host_set
->ports
[i
];
4135 !(ap
->flags
& (ATA_FLAG_PORT_DISABLED
| ATA_FLAG_NOINTR
))) {
4136 struct ata_queued_cmd
*qc
;
4138 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
4139 if (qc
&& (!(qc
->tf
.ctl
& ATA_NIEN
)) &&
4140 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
4141 handled
|= ata_host_intr(ap
, qc
);
4145 spin_unlock_irqrestore(&host_set
->lock
, flags
);
4147 return IRQ_RETVAL(handled
);
4151 * atapi_packet_task - Write CDB bytes to hardware
4152 * @_data: Port to which ATAPI device is attached.
4154 * When device has indicated its readiness to accept
4155 * a CDB, this function is called. Send the CDB.
4156 * If DMA is to be performed, exit immediately.
4157 * Otherwise, we are in polling mode, so poll
4158 * status under operation succeeds or fails.
4161 * Kernel thread context (may sleep)
4164 static void atapi_packet_task(void *_data
)
4166 struct ata_port
*ap
= _data
;
4167 struct ata_queued_cmd
*qc
;
4170 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
4172 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
4174 /* sleep-wait for BSY to clear */
4175 DPRINTK("busy wait\n");
4176 if (ata_busy_sleep(ap
, ATA_TMOUT_CDB_QUICK
, ATA_TMOUT_CDB
)) {
4177 qc
->err_mask
|= AC_ERR_ATA_BUS
;
4181 /* make sure DRQ is set */
4182 status
= ata_chk_status(ap
);
4183 if ((status
& (ATA_BUSY
| ATA_DRQ
)) != ATA_DRQ
) {
4184 qc
->err_mask
|= AC_ERR_ATA_BUS
;
4189 DPRINTK("send cdb\n");
4190 assert(ap
->cdb_len
>= 12);
4192 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
||
4193 qc
->tf
.protocol
== ATA_PROT_ATAPI_NODATA
) {
4194 unsigned long flags
;
4196 /* Once we're done issuing command and kicking bmdma,
4197 * irq handler takes over. To not lose irq, we need
4198 * to clear NOINTR flag before sending cdb, but
4199 * interrupt handler shouldn't be invoked before we're
4200 * finished. Hence, the following locking.
4202 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
4203 ap
->flags
&= ~ATA_FLAG_NOINTR
;
4204 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
4205 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
)
4206 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
4207 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
4209 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
4211 /* PIO commands are handled by polling */
4212 ap
->hsm_task_state
= HSM_ST
;
4213 queue_work(ata_wq
, &ap
->pio_task
);
4219 ata_poll_qc_complete(qc
);
4224 * ata_port_start - Set port up for dma.
4225 * @ap: Port to initialize
4227 * Called just after data structures for each port are
4228 * initialized. Allocates space for PRD table.
4230 * May be used as the port_start() entry in ata_port_operations.
4233 * Inherited from caller.
4237 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4238 * without filling any other registers
4240 static int ata_do_simple_cmd(struct ata_port
*ap
, struct ata_device
*dev
,
4243 struct ata_taskfile tf
;
4246 ata_tf_init(ap
, &tf
, dev
->devno
);
4249 tf
.flags
|= ATA_TFLAG_DEVICE
;
4250 tf
.protocol
= ATA_PROT_NODATA
;
4252 err
= ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0);
4254 printk(KERN_ERR
"%s: ata command failed: %d\n",
4260 static int ata_flush_cache(struct ata_port
*ap
, struct ata_device
*dev
)
4264 if (!ata_try_flush_cache(dev
))
4267 if (ata_id_has_flush_ext(dev
->id
))
4268 cmd
= ATA_CMD_FLUSH_EXT
;
4270 cmd
= ATA_CMD_FLUSH
;
4272 return ata_do_simple_cmd(ap
, dev
, cmd
);
4275 static int ata_standby_drive(struct ata_port
*ap
, struct ata_device
*dev
)
4277 return ata_do_simple_cmd(ap
, dev
, ATA_CMD_STANDBYNOW1
);
4280 static int ata_start_drive(struct ata_port
*ap
, struct ata_device
*dev
)
4282 return ata_do_simple_cmd(ap
, dev
, ATA_CMD_IDLEIMMEDIATE
);
4286 * ata_device_resume - wakeup a previously suspended devices
4288 * Kick the drive back into action, by sending it an idle immediate
4289 * command and making sure its transfer mode matches between drive
4293 int ata_device_resume(struct ata_port
*ap
, struct ata_device
*dev
)
4295 if (ap
->flags
& ATA_FLAG_SUSPENDED
) {
4296 ap
->flags
&= ~ATA_FLAG_SUSPENDED
;
4299 if (!ata_dev_present(dev
))
4301 if (dev
->class == ATA_DEV_ATA
)
4302 ata_start_drive(ap
, dev
);
4308 * ata_device_suspend - prepare a device for suspend
4310 * Flush the cache on the drive, if appropriate, then issue a
4311 * standbynow command.
4314 int ata_device_suspend(struct ata_port
*ap
, struct ata_device
*dev
)
4316 if (!ata_dev_present(dev
))
4318 if (dev
->class == ATA_DEV_ATA
)
4319 ata_flush_cache(ap
, dev
);
4321 ata_standby_drive(ap
, dev
);
4322 ap
->flags
|= ATA_FLAG_SUSPENDED
;
4326 int ata_port_start (struct ata_port
*ap
)
4328 struct device
*dev
= ap
->host_set
->dev
;
4331 ap
->prd
= dma_alloc_coherent(dev
, ATA_PRD_TBL_SZ
, &ap
->prd_dma
, GFP_KERNEL
);
4335 rc
= ata_pad_alloc(ap
, dev
);
4337 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
4341 DPRINTK("prd alloc, virt %p, dma %llx\n", ap
->prd
, (unsigned long long) ap
->prd_dma
);
4348 * ata_port_stop - Undo ata_port_start()
4349 * @ap: Port to shut down
4351 * Frees the PRD table.
4353 * May be used as the port_stop() entry in ata_port_operations.
4356 * Inherited from caller.
4359 void ata_port_stop (struct ata_port
*ap
)
4361 struct device
*dev
= ap
->host_set
->dev
;
4363 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
4364 ata_pad_free(ap
, dev
);
4367 void ata_host_stop (struct ata_host_set
*host_set
)
4369 if (host_set
->mmio_base
)
4370 iounmap(host_set
->mmio_base
);
4375 * ata_host_remove - Unregister SCSI host structure with upper layers
4376 * @ap: Port to unregister
4377 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4380 * Inherited from caller.
4383 static void ata_host_remove(struct ata_port
*ap
, unsigned int do_unregister
)
4385 struct Scsi_Host
*sh
= ap
->host
;
4390 scsi_remove_host(sh
);
4392 ap
->ops
->port_stop(ap
);
4396 * ata_host_init - Initialize an ata_port structure
4397 * @ap: Structure to initialize
4398 * @host: associated SCSI mid-layer structure
4399 * @host_set: Collection of hosts to which @ap belongs
4400 * @ent: Probe information provided by low-level driver
4401 * @port_no: Port number associated with this ata_port
4403 * Initialize a new ata_port structure, and its associated
4407 * Inherited from caller.
4410 static void ata_host_init(struct ata_port
*ap
, struct Scsi_Host
*host
,
4411 struct ata_host_set
*host_set
,
4412 const struct ata_probe_ent
*ent
, unsigned int port_no
)
4418 host
->max_channel
= 1;
4419 host
->unique_id
= ata_unique_id
++;
4420 host
->max_cmd_len
= 12;
4422 ap
->flags
= ATA_FLAG_PORT_DISABLED
;
4423 ap
->id
= host
->unique_id
;
4425 ap
->ctl
= ATA_DEVCTL_OBS
;
4426 ap
->host_set
= host_set
;
4427 ap
->port_no
= port_no
;
4429 ent
->legacy_mode
? ent
->hard_port_no
: port_no
;
4430 ap
->pio_mask
= ent
->pio_mask
;
4431 ap
->mwdma_mask
= ent
->mwdma_mask
;
4432 ap
->udma_mask
= ent
->udma_mask
;
4433 ap
->flags
|= ent
->host_flags
;
4434 ap
->ops
= ent
->port_ops
;
4435 ap
->cbl
= ATA_CBL_NONE
;
4436 ap
->active_tag
= ATA_TAG_POISON
;
4437 ap
->last_ctl
= 0xFF;
4439 INIT_WORK(&ap
->packet_task
, atapi_packet_task
, ap
);
4440 INIT_WORK(&ap
->pio_task
, ata_pio_task
, ap
);
4442 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
4443 ap
->device
[i
].devno
= i
;
4446 ap
->stats
.unhandled_irq
= 1;
4447 ap
->stats
.idle_irq
= 1;
4450 memcpy(&ap
->ioaddr
, &ent
->port
[port_no
], sizeof(struct ata_ioports
));
4454 * ata_host_add - Attach low-level ATA driver to system
4455 * @ent: Information provided by low-level driver
4456 * @host_set: Collections of ports to which we add
4457 * @port_no: Port number associated with this host
4459 * Attach low-level ATA driver to system.
4462 * PCI/etc. bus probe sem.
4465 * New ata_port on success, for NULL on error.
4468 static struct ata_port
* ata_host_add(const struct ata_probe_ent
*ent
,
4469 struct ata_host_set
*host_set
,
4470 unsigned int port_no
)
4472 struct Scsi_Host
*host
;
4473 struct ata_port
*ap
;
4477 host
= scsi_host_alloc(ent
->sht
, sizeof(struct ata_port
));
4481 ap
= (struct ata_port
*) &host
->hostdata
[0];
4483 ata_host_init(ap
, host
, host_set
, ent
, port_no
);
4485 rc
= ap
->ops
->port_start(ap
);
4492 scsi_host_put(host
);
4497 * ata_device_add - Register hardware device with ATA and SCSI layers
4498 * @ent: Probe information describing hardware device to be registered
4500 * This function processes the information provided in the probe
4501 * information struct @ent, allocates the necessary ATA and SCSI
4502 * host information structures, initializes them, and registers
4503 * everything with requisite kernel subsystems.
4505 * This function requests irqs, probes the ATA bus, and probes
4509 * PCI/etc. bus probe sem.
4512 * Number of ports registered. Zero on error (no ports registered).
4515 int ata_device_add(const struct ata_probe_ent
*ent
)
4517 unsigned int count
= 0, i
;
4518 struct device
*dev
= ent
->dev
;
4519 struct ata_host_set
*host_set
;
4522 /* alloc a container for our list of ATA ports (buses) */
4523 host_set
= kzalloc(sizeof(struct ata_host_set
) +
4524 (ent
->n_ports
* sizeof(void *)), GFP_KERNEL
);
4527 spin_lock_init(&host_set
->lock
);
4529 host_set
->dev
= dev
;
4530 host_set
->n_ports
= ent
->n_ports
;
4531 host_set
->irq
= ent
->irq
;
4532 host_set
->mmio_base
= ent
->mmio_base
;
4533 host_set
->private_data
= ent
->private_data
;
4534 host_set
->ops
= ent
->port_ops
;
4536 /* register each port bound to this device */
4537 for (i
= 0; i
< ent
->n_ports
; i
++) {
4538 struct ata_port
*ap
;
4539 unsigned long xfer_mode_mask
;
4541 ap
= ata_host_add(ent
, host_set
, i
);
4545 host_set
->ports
[i
] = ap
;
4546 xfer_mode_mask
=(ap
->udma_mask
<< ATA_SHIFT_UDMA
) |
4547 (ap
->mwdma_mask
<< ATA_SHIFT_MWDMA
) |
4548 (ap
->pio_mask
<< ATA_SHIFT_PIO
);
4550 /* print per-port info to dmesg */
4551 printk(KERN_INFO
"ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4552 "bmdma 0x%lX irq %lu\n",
4554 ap
->flags
& ATA_FLAG_SATA
? 'S' : 'P',
4555 ata_mode_string(xfer_mode_mask
),
4556 ap
->ioaddr
.cmd_addr
,
4557 ap
->ioaddr
.ctl_addr
,
4558 ap
->ioaddr
.bmdma_addr
,
4562 host_set
->ops
->irq_clear(ap
);
4569 /* obtain irq, that is shared between channels */
4570 if (request_irq(ent
->irq
, ent
->port_ops
->irq_handler
, ent
->irq_flags
,
4571 DRV_NAME
, host_set
))
4574 /* perform each probe synchronously */
4575 DPRINTK("probe begin\n");
4576 for (i
= 0; i
< count
; i
++) {
4577 struct ata_port
*ap
;
4580 ap
= host_set
->ports
[i
];
4582 DPRINTK("ata%u: probe begin\n", ap
->id
);
4583 rc
= ata_bus_probe(ap
);
4584 DPRINTK("ata%u: probe end\n", ap
->id
);
4587 /* FIXME: do something useful here?
4588 * Current libata behavior will
4589 * tear down everything when
4590 * the module is removed
4591 * or the h/w is unplugged.
4595 rc
= scsi_add_host(ap
->host
, dev
);
4597 printk(KERN_ERR
"ata%u: scsi_add_host failed\n",
4599 /* FIXME: do something useful here */
4600 /* FIXME: handle unconditional calls to
4601 * scsi_scan_host and ata_host_remove, below,
4607 /* probes are done, now scan each port's disk(s) */
4608 DPRINTK("probe begin\n");
4609 for (i
= 0; i
< count
; i
++) {
4610 struct ata_port
*ap
= host_set
->ports
[i
];
4612 ata_scsi_scan_host(ap
);
4615 dev_set_drvdata(dev
, host_set
);
4617 VPRINTK("EXIT, returning %u\n", ent
->n_ports
);
4618 return ent
->n_ports
; /* success */
4621 for (i
= 0; i
< count
; i
++) {
4622 ata_host_remove(host_set
->ports
[i
], 1);
4623 scsi_host_put(host_set
->ports
[i
]->host
);
4627 VPRINTK("EXIT, returning 0\n");
4632 * ata_host_set_remove - PCI layer callback for device removal
4633 * @host_set: ATA host set that was removed
4635 * Unregister all objects associated with this host set. Free those
4639 * Inherited from calling layer (may sleep).
4642 void ata_host_set_remove(struct ata_host_set
*host_set
)
4644 struct ata_port
*ap
;
4647 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4648 ap
= host_set
->ports
[i
];
4649 scsi_remove_host(ap
->host
);
4652 free_irq(host_set
->irq
, host_set
);
4654 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4655 ap
= host_set
->ports
[i
];
4657 ata_scsi_release(ap
->host
);
4659 if ((ap
->flags
& ATA_FLAG_NO_LEGACY
) == 0) {
4660 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
4662 if (ioaddr
->cmd_addr
== 0x1f0)
4663 release_region(0x1f0, 8);
4664 else if (ioaddr
->cmd_addr
== 0x170)
4665 release_region(0x170, 8);
4668 scsi_host_put(ap
->host
);
4671 if (host_set
->ops
->host_stop
)
4672 host_set
->ops
->host_stop(host_set
);
4678 * ata_scsi_release - SCSI layer callback hook for host unload
4679 * @host: libata host to be unloaded
4681 * Performs all duties necessary to shut down a libata port...
4682 * Kill port kthread, disable port, and release resources.
4685 * Inherited from SCSI layer.
4691 int ata_scsi_release(struct Scsi_Host
*host
)
4693 struct ata_port
*ap
= (struct ata_port
*) &host
->hostdata
[0];
4697 ap
->ops
->port_disable(ap
);
4698 ata_host_remove(ap
, 0);
4705 * ata_std_ports - initialize ioaddr with standard port offsets.
4706 * @ioaddr: IO address structure to be initialized
4708 * Utility function which initializes data_addr, error_addr,
4709 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4710 * device_addr, status_addr, and command_addr to standard offsets
4711 * relative to cmd_addr.
4713 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4716 void ata_std_ports(struct ata_ioports
*ioaddr
)
4718 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
4719 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
4720 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
4721 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
4722 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
4723 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
4724 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
4725 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
4726 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
4727 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
4730 static struct ata_probe_ent
*
4731 ata_probe_ent_alloc(struct device
*dev
, const struct ata_port_info
*port
)
4733 struct ata_probe_ent
*probe_ent
;
4735 probe_ent
= kzalloc(sizeof(*probe_ent
), GFP_KERNEL
);
4737 printk(KERN_ERR DRV_NAME
"(%s): out of memory\n",
4738 kobject_name(&(dev
->kobj
)));
4742 INIT_LIST_HEAD(&probe_ent
->node
);
4743 probe_ent
->dev
= dev
;
4745 probe_ent
->sht
= port
->sht
;
4746 probe_ent
->host_flags
= port
->host_flags
;
4747 probe_ent
->pio_mask
= port
->pio_mask
;
4748 probe_ent
->mwdma_mask
= port
->mwdma_mask
;
4749 probe_ent
->udma_mask
= port
->udma_mask
;
4750 probe_ent
->port_ops
= port
->port_ops
;
4759 void ata_pci_host_stop (struct ata_host_set
*host_set
)
4761 struct pci_dev
*pdev
= to_pci_dev(host_set
->dev
);
4763 pci_iounmap(pdev
, host_set
->mmio_base
);
4767 * ata_pci_init_native_mode - Initialize native-mode driver
4768 * @pdev: pci device to be initialized
4769 * @port: array[2] of pointers to port info structures.
4770 * @ports: bitmap of ports present
4772 * Utility function which allocates and initializes an
4773 * ata_probe_ent structure for a standard dual-port
4774 * PIO-based IDE controller. The returned ata_probe_ent
4775 * structure can be passed to ata_device_add(). The returned
4776 * ata_probe_ent structure should then be freed with kfree().
4778 * The caller need only pass the address of the primary port, the
4779 * secondary will be deduced automatically. If the device has non
4780 * standard secondary port mappings this function can be called twice,
4781 * once for each interface.
4784 struct ata_probe_ent
*
4785 ata_pci_init_native_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
, int ports
)
4787 struct ata_probe_ent
*probe_ent
=
4788 ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
4794 probe_ent
->irq
= pdev
->irq
;
4795 probe_ent
->irq_flags
= SA_SHIRQ
;
4796 probe_ent
->private_data
= port
[0]->private_data
;
4798 if (ports
& ATA_PORT_PRIMARY
) {
4799 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 0);
4800 probe_ent
->port
[p
].altstatus_addr
=
4801 probe_ent
->port
[p
].ctl_addr
=
4802 pci_resource_start(pdev
, 1) | ATA_PCI_CTL_OFS
;
4803 probe_ent
->port
[p
].bmdma_addr
= pci_resource_start(pdev
, 4);
4804 ata_std_ports(&probe_ent
->port
[p
]);
4808 if (ports
& ATA_PORT_SECONDARY
) {
4809 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 2);
4810 probe_ent
->port
[p
].altstatus_addr
=
4811 probe_ent
->port
[p
].ctl_addr
=
4812 pci_resource_start(pdev
, 3) | ATA_PCI_CTL_OFS
;
4813 probe_ent
->port
[p
].bmdma_addr
= pci_resource_start(pdev
, 4) + 8;
4814 ata_std_ports(&probe_ent
->port
[p
]);
4818 probe_ent
->n_ports
= p
;
4822 static struct ata_probe_ent
*ata_pci_init_legacy_port(struct pci_dev
*pdev
, struct ata_port_info
*port
, int port_num
)
4824 struct ata_probe_ent
*probe_ent
;
4826 probe_ent
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
);
4830 probe_ent
->legacy_mode
= 1;
4831 probe_ent
->n_ports
= 1;
4832 probe_ent
->hard_port_no
= port_num
;
4833 probe_ent
->private_data
= port
->private_data
;
4838 probe_ent
->irq
= 14;
4839 probe_ent
->port
[0].cmd_addr
= 0x1f0;
4840 probe_ent
->port
[0].altstatus_addr
=
4841 probe_ent
->port
[0].ctl_addr
= 0x3f6;
4844 probe_ent
->irq
= 15;
4845 probe_ent
->port
[0].cmd_addr
= 0x170;
4846 probe_ent
->port
[0].altstatus_addr
=
4847 probe_ent
->port
[0].ctl_addr
= 0x376;
4850 probe_ent
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4) + 8 * port_num
;
4851 ata_std_ports(&probe_ent
->port
[0]);
4856 * ata_pci_init_one - Initialize/register PCI IDE host controller
4857 * @pdev: Controller to be initialized
4858 * @port_info: Information from low-level host driver
4859 * @n_ports: Number of ports attached to host controller
4861 * This is a helper function which can be called from a driver's
4862 * xxx_init_one() probe function if the hardware uses traditional
4863 * IDE taskfile registers.
4865 * This function calls pci_enable_device(), reserves its register
4866 * regions, sets the dma mask, enables bus master mode, and calls
4870 * Inherited from PCI layer (may sleep).
4873 * Zero on success, negative on errno-based value on error.
4876 int ata_pci_init_one (struct pci_dev
*pdev
, struct ata_port_info
**port_info
,
4877 unsigned int n_ports
)
4879 struct ata_probe_ent
*probe_ent
= NULL
, *probe_ent2
= NULL
;
4880 struct ata_port_info
*port
[2];
4882 unsigned int legacy_mode
= 0;
4883 int disable_dev_on_err
= 1;
4888 port
[0] = port_info
[0];
4890 port
[1] = port_info
[1];
4894 if ((port
[0]->host_flags
& ATA_FLAG_NO_LEGACY
) == 0
4895 && (pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
4896 /* TODO: What if one channel is in native mode ... */
4897 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
4898 mask
= (1 << 2) | (1 << 0);
4899 if ((tmp8
& mask
) != mask
)
4900 legacy_mode
= (1 << 3);
4904 if ((!legacy_mode
) && (n_ports
> 2)) {
4905 printk(KERN_ERR
"ata: BUG: native mode, n_ports > 2\n");
4910 /* FIXME: Really for ATA it isn't safe because the device may be
4911 multi-purpose and we want to leave it alone if it was already
4912 enabled. Secondly for shared use as Arjan says we want refcounting
4914 Checking dev->is_enabled is insufficient as this is not set at
4915 boot for the primary video which is BIOS enabled
4918 rc
= pci_enable_device(pdev
);
4922 rc
= pci_request_regions(pdev
, DRV_NAME
);
4924 disable_dev_on_err
= 0;
4928 /* FIXME: Should use platform specific mappers for legacy port ranges */
4930 if (!request_region(0x1f0, 8, "libata")) {
4931 struct resource
*conflict
, res
;
4933 res
.end
= 0x1f0 + 8 - 1;
4934 conflict
= ____request_resource(&ioport_resource
, &res
);
4935 if (!strcmp(conflict
->name
, "libata"))
4936 legacy_mode
|= (1 << 0);
4938 disable_dev_on_err
= 0;
4939 printk(KERN_WARNING
"ata: 0x1f0 IDE port busy\n");
4942 legacy_mode
|= (1 << 0);
4944 if (!request_region(0x170, 8, "libata")) {
4945 struct resource
*conflict
, res
;
4947 res
.end
= 0x170 + 8 - 1;
4948 conflict
= ____request_resource(&ioport_resource
, &res
);
4949 if (!strcmp(conflict
->name
, "libata"))
4950 legacy_mode
|= (1 << 1);
4952 disable_dev_on_err
= 0;
4953 printk(KERN_WARNING
"ata: 0x170 IDE port busy\n");
4956 legacy_mode
|= (1 << 1);
4959 /* we have legacy mode, but all ports are unavailable */
4960 if (legacy_mode
== (1 << 3)) {
4962 goto err_out_regions
;
4965 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
4967 goto err_out_regions
;
4968 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
4970 goto err_out_regions
;
4973 if (legacy_mode
& (1 << 0))
4974 probe_ent
= ata_pci_init_legacy_port(pdev
, port
[0], 0);
4975 if (legacy_mode
& (1 << 1))
4976 probe_ent2
= ata_pci_init_legacy_port(pdev
, port
[1], 1);
4979 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
| ATA_PORT_SECONDARY
);
4981 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
);
4983 if (!probe_ent
&& !probe_ent2
) {
4985 goto err_out_regions
;
4988 pci_set_master(pdev
);
4990 /* FIXME: check ata_device_add return */
4992 if (legacy_mode
& (1 << 0))
4993 ata_device_add(probe_ent
);
4994 if (legacy_mode
& (1 << 1))
4995 ata_device_add(probe_ent2
);
4997 ata_device_add(probe_ent
);
5005 if (legacy_mode
& (1 << 0))
5006 release_region(0x1f0, 8);
5007 if (legacy_mode
& (1 << 1))
5008 release_region(0x170, 8);
5009 pci_release_regions(pdev
);
5011 if (disable_dev_on_err
)
5012 pci_disable_device(pdev
);
5017 * ata_pci_remove_one - PCI layer callback for device removal
5018 * @pdev: PCI device that was removed
5020 * PCI layer indicates to libata via this hook that
5021 * hot-unplug or module unload event has occurred.
5022 * Handle this by unregistering all objects associated
5023 * with this PCI device. Free those objects. Then finally
5024 * release PCI resources and disable device.
5027 * Inherited from PCI layer (may sleep).
5030 void ata_pci_remove_one (struct pci_dev
*pdev
)
5032 struct device
*dev
= pci_dev_to_dev(pdev
);
5033 struct ata_host_set
*host_set
= dev_get_drvdata(dev
);
5035 ata_host_set_remove(host_set
);
5036 pci_release_regions(pdev
);
5037 pci_disable_device(pdev
);
5038 dev_set_drvdata(dev
, NULL
);
5041 /* move to PCI subsystem */
5042 int pci_test_config_bits(struct pci_dev
*pdev
, const struct pci_bits
*bits
)
5044 unsigned long tmp
= 0;
5046 switch (bits
->width
) {
5049 pci_read_config_byte(pdev
, bits
->reg
, &tmp8
);
5055 pci_read_config_word(pdev
, bits
->reg
, &tmp16
);
5061 pci_read_config_dword(pdev
, bits
->reg
, &tmp32
);
5072 return (tmp
== bits
->val
) ? 1 : 0;
5075 int ata_pci_device_suspend(struct pci_dev
*pdev
, pm_message_t state
)
5077 pci_save_state(pdev
);
5078 pci_disable_device(pdev
);
5079 pci_set_power_state(pdev
, PCI_D3hot
);
5083 int ata_pci_device_resume(struct pci_dev
*pdev
)
5085 pci_set_power_state(pdev
, PCI_D0
);
5086 pci_restore_state(pdev
);
5087 pci_enable_device(pdev
);
5088 pci_set_master(pdev
);
5091 #endif /* CONFIG_PCI */
5094 static int __init
ata_init(void)
5096 ata_wq
= create_workqueue("ata");
5100 printk(KERN_DEBUG
"libata version " DRV_VERSION
" loaded.\n");
5104 static void __exit
ata_exit(void)
5106 destroy_workqueue(ata_wq
);
5109 module_init(ata_init
);
5110 module_exit(ata_exit
);
5112 static unsigned long ratelimit_time
;
5113 static spinlock_t ata_ratelimit_lock
= SPIN_LOCK_UNLOCKED
;
5115 int ata_ratelimit(void)
5118 unsigned long flags
;
5120 spin_lock_irqsave(&ata_ratelimit_lock
, flags
);
5122 if (time_after(jiffies
, ratelimit_time
)) {
5124 ratelimit_time
= jiffies
+ (HZ
/5);
5128 spin_unlock_irqrestore(&ata_ratelimit_lock
, flags
);
5134 * libata is essentially a library of internal helper functions for
5135 * low-level ATA host controller drivers. As such, the API/ABI is
5136 * likely to change as new drivers are added and updated.
5137 * Do not depend on ABI/API stability.
5140 EXPORT_SYMBOL_GPL(ata_std_bios_param
);
5141 EXPORT_SYMBOL_GPL(ata_std_ports
);
5142 EXPORT_SYMBOL_GPL(ata_device_add
);
5143 EXPORT_SYMBOL_GPL(ata_host_set_remove
);
5144 EXPORT_SYMBOL_GPL(ata_sg_init
);
5145 EXPORT_SYMBOL_GPL(ata_sg_init_one
);
5146 EXPORT_SYMBOL_GPL(ata_qc_complete
);
5147 EXPORT_SYMBOL_GPL(ata_qc_issue_prot
);
5148 EXPORT_SYMBOL_GPL(ata_eng_timeout
);
5149 EXPORT_SYMBOL_GPL(ata_tf_load
);
5150 EXPORT_SYMBOL_GPL(ata_tf_read
);
5151 EXPORT_SYMBOL_GPL(ata_noop_dev_select
);
5152 EXPORT_SYMBOL_GPL(ata_std_dev_select
);
5153 EXPORT_SYMBOL_GPL(ata_tf_to_fis
);
5154 EXPORT_SYMBOL_GPL(ata_tf_from_fis
);
5155 EXPORT_SYMBOL_GPL(ata_check_status
);
5156 EXPORT_SYMBOL_GPL(ata_altstatus
);
5157 EXPORT_SYMBOL_GPL(ata_exec_command
);
5158 EXPORT_SYMBOL_GPL(ata_port_start
);
5159 EXPORT_SYMBOL_GPL(ata_port_stop
);
5160 EXPORT_SYMBOL_GPL(ata_host_stop
);
5161 EXPORT_SYMBOL_GPL(ata_interrupt
);
5162 EXPORT_SYMBOL_GPL(ata_qc_prep
);
5163 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
5164 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
5165 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear
);
5166 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
5167 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
5168 EXPORT_SYMBOL_GPL(ata_port_probe
);
5169 EXPORT_SYMBOL_GPL(sata_phy_reset
);
5170 EXPORT_SYMBOL_GPL(__sata_phy_reset
);
5171 EXPORT_SYMBOL_GPL(ata_bus_reset
);
5172 EXPORT_SYMBOL_GPL(ata_port_disable
);
5173 EXPORT_SYMBOL_GPL(ata_ratelimit
);
5174 EXPORT_SYMBOL_GPL(ata_scsi_ioctl
);
5175 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd
);
5176 EXPORT_SYMBOL_GPL(ata_scsi_error
);
5177 EXPORT_SYMBOL_GPL(ata_scsi_slave_config
);
5178 EXPORT_SYMBOL_GPL(ata_scsi_release
);
5179 EXPORT_SYMBOL_GPL(ata_host_intr
);
5180 EXPORT_SYMBOL_GPL(ata_dev_classify
);
5181 EXPORT_SYMBOL_GPL(ata_dev_id_string
);
5182 EXPORT_SYMBOL_GPL(ata_dev_config
);
5183 EXPORT_SYMBOL_GPL(ata_scsi_simulate
);
5185 EXPORT_SYMBOL_GPL(ata_pio_need_iordy
);
5186 EXPORT_SYMBOL_GPL(ata_timing_compute
);
5187 EXPORT_SYMBOL_GPL(ata_timing_merge
);
5190 EXPORT_SYMBOL_GPL(pci_test_config_bits
);
5191 EXPORT_SYMBOL_GPL(ata_pci_host_stop
);
5192 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode
);
5193 EXPORT_SYMBOL_GPL(ata_pci_init_one
);
5194 EXPORT_SYMBOL_GPL(ata_pci_remove_one
);
5195 EXPORT_SYMBOL_GPL(ata_pci_device_suspend
);
5196 EXPORT_SYMBOL_GPL(ata_pci_device_resume
);
5197 #endif /* CONFIG_PCI */
5199 EXPORT_SYMBOL_GPL(ata_device_suspend
);
5200 EXPORT_SYMBOL_GPL(ata_device_resume
);
5201 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend
);
5202 EXPORT_SYMBOL_GPL(ata_scsi_device_resume
);