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)");
85 MODULE_AUTHOR("Jeff Garzik");
86 MODULE_DESCRIPTION("Library module for ATA devices");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION
);
91 * ata_tf_load_pio - send taskfile registers to host controller
92 * @ap: Port to which output is sent
93 * @tf: ATA taskfile register set
95 * Outputs ATA taskfile to standard ATA host controller.
98 * Inherited from caller.
101 static void ata_tf_load_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
103 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
104 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
106 if (tf
->ctl
!= ap
->last_ctl
) {
107 outb(tf
->ctl
, ioaddr
->ctl_addr
);
108 ap
->last_ctl
= tf
->ctl
;
112 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
113 outb(tf
->hob_feature
, ioaddr
->feature_addr
);
114 outb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
115 outb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
116 outb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
117 outb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
118 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
127 outb(tf
->feature
, ioaddr
->feature_addr
);
128 outb(tf
->nsect
, ioaddr
->nsect_addr
);
129 outb(tf
->lbal
, ioaddr
->lbal_addr
);
130 outb(tf
->lbam
, ioaddr
->lbam_addr
);
131 outb(tf
->lbah
, ioaddr
->lbah_addr
);
132 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
140 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
141 outb(tf
->device
, ioaddr
->device_addr
);
142 VPRINTK("device 0x%X\n", tf
->device
);
149 * ata_tf_load_mmio - send taskfile registers to host controller
150 * @ap: Port to which output is sent
151 * @tf: ATA taskfile register set
153 * Outputs ATA taskfile to standard ATA host controller using MMIO.
156 * Inherited from caller.
159 static void ata_tf_load_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
161 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
162 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
164 if (tf
->ctl
!= ap
->last_ctl
) {
165 writeb(tf
->ctl
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
166 ap
->last_ctl
= tf
->ctl
;
170 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
171 writeb(tf
->hob_feature
, (void __iomem
*) ioaddr
->feature_addr
);
172 writeb(tf
->hob_nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
173 writeb(tf
->hob_lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
174 writeb(tf
->hob_lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
175 writeb(tf
->hob_lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
176 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
185 writeb(tf
->feature
, (void __iomem
*) ioaddr
->feature_addr
);
186 writeb(tf
->nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
187 writeb(tf
->lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
188 writeb(tf
->lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
189 writeb(tf
->lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
190 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
198 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
199 writeb(tf
->device
, (void __iomem
*) ioaddr
->device_addr
);
200 VPRINTK("device 0x%X\n", tf
->device
);
208 * ata_tf_load - send taskfile registers to host controller
209 * @ap: Port to which output is sent
210 * @tf: ATA taskfile register set
212 * Outputs ATA taskfile to standard ATA host controller using MMIO
213 * or PIO as indicated by the ATA_FLAG_MMIO flag.
214 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
215 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
216 * hob_lbal, hob_lbam, and hob_lbah.
218 * This function waits for idle (!BUSY and !DRQ) after writing
219 * registers. If the control register has a new value, this
220 * function also waits for idle after writing control and before
221 * writing the remaining registers.
223 * May be used as the tf_load() entry in ata_port_operations.
226 * Inherited from caller.
228 void ata_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
230 if (ap
->flags
& ATA_FLAG_MMIO
)
231 ata_tf_load_mmio(ap
, tf
);
233 ata_tf_load_pio(ap
, tf
);
237 * ata_exec_command_pio - issue ATA command to host controller
238 * @ap: port to which command is being issued
239 * @tf: ATA taskfile register set
241 * Issues PIO write to ATA command register, with proper
242 * synchronization with interrupt handler / other threads.
245 * spin_lock_irqsave(host_set lock)
248 static void ata_exec_command_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
250 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
252 outb(tf
->command
, ap
->ioaddr
.command_addr
);
258 * ata_exec_command_mmio - issue ATA command to host controller
259 * @ap: port to which command is being issued
260 * @tf: ATA taskfile register set
262 * Issues MMIO write to ATA command register, with proper
263 * synchronization with interrupt handler / other threads.
266 * spin_lock_irqsave(host_set lock)
269 static void ata_exec_command_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
271 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
273 writeb(tf
->command
, (void __iomem
*) ap
->ioaddr
.command_addr
);
279 * ata_exec_command - issue ATA command to host controller
280 * @ap: port to which command is being issued
281 * @tf: ATA taskfile register set
283 * Issues PIO/MMIO write to ATA command register, with proper
284 * synchronization with interrupt handler / other threads.
287 * spin_lock_irqsave(host_set lock)
289 void ata_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
291 if (ap
->flags
& ATA_FLAG_MMIO
)
292 ata_exec_command_mmio(ap
, tf
);
294 ata_exec_command_pio(ap
, tf
);
298 * ata_tf_to_host - issue ATA taskfile to host controller
299 * @ap: port to which command is being issued
300 * @tf: ATA taskfile register set
302 * Issues ATA taskfile register set to ATA host controller,
303 * with proper synchronization with interrupt handler and
307 * spin_lock_irqsave(host_set lock)
310 static inline void ata_tf_to_host(struct ata_port
*ap
,
311 const struct ata_taskfile
*tf
)
313 ap
->ops
->tf_load(ap
, tf
);
314 ap
->ops
->exec_command(ap
, tf
);
318 * ata_tf_read_pio - input device's ATA taskfile shadow registers
319 * @ap: Port from which input is read
320 * @tf: ATA taskfile register set for storing input
322 * Reads ATA taskfile registers for currently-selected device
326 * Inherited from caller.
329 static void ata_tf_read_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
331 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
333 tf
->command
= ata_check_status(ap
);
334 tf
->feature
= inb(ioaddr
->error_addr
);
335 tf
->nsect
= inb(ioaddr
->nsect_addr
);
336 tf
->lbal
= inb(ioaddr
->lbal_addr
);
337 tf
->lbam
= inb(ioaddr
->lbam_addr
);
338 tf
->lbah
= inb(ioaddr
->lbah_addr
);
339 tf
->device
= inb(ioaddr
->device_addr
);
341 if (tf
->flags
& ATA_TFLAG_LBA48
) {
342 outb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
343 tf
->hob_feature
= inb(ioaddr
->error_addr
);
344 tf
->hob_nsect
= inb(ioaddr
->nsect_addr
);
345 tf
->hob_lbal
= inb(ioaddr
->lbal_addr
);
346 tf
->hob_lbam
= inb(ioaddr
->lbam_addr
);
347 tf
->hob_lbah
= inb(ioaddr
->lbah_addr
);
352 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
353 * @ap: Port from which input is read
354 * @tf: ATA taskfile register set for storing input
356 * Reads ATA taskfile registers for currently-selected device
360 * Inherited from caller.
363 static void ata_tf_read_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
365 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
367 tf
->command
= ata_check_status(ap
);
368 tf
->feature
= readb((void __iomem
*)ioaddr
->error_addr
);
369 tf
->nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
370 tf
->lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
371 tf
->lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
372 tf
->lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
373 tf
->device
= readb((void __iomem
*)ioaddr
->device_addr
);
375 if (tf
->flags
& ATA_TFLAG_LBA48
) {
376 writeb(tf
->ctl
| ATA_HOB
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
377 tf
->hob_feature
= readb((void __iomem
*)ioaddr
->error_addr
);
378 tf
->hob_nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
379 tf
->hob_lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
380 tf
->hob_lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
381 tf
->hob_lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
387 * ata_tf_read - input device's ATA taskfile shadow registers
388 * @ap: Port from which input is read
389 * @tf: ATA taskfile register set for storing input
391 * Reads ATA taskfile registers for currently-selected device
394 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
395 * is set, also reads the hob registers.
397 * May be used as the tf_read() entry in ata_port_operations.
400 * Inherited from caller.
402 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
404 if (ap
->flags
& ATA_FLAG_MMIO
)
405 ata_tf_read_mmio(ap
, tf
);
407 ata_tf_read_pio(ap
, tf
);
411 * ata_check_status_pio - Read device status reg & clear interrupt
412 * @ap: port where the device is
414 * Reads ATA taskfile status register for currently-selected device
415 * and return its value. This also clears pending interrupts
419 * Inherited from caller.
421 static u8
ata_check_status_pio(struct ata_port
*ap
)
423 return inb(ap
->ioaddr
.status_addr
);
427 * ata_check_status_mmio - Read device status reg & clear interrupt
428 * @ap: port where the device is
430 * Reads ATA taskfile status register for currently-selected device
431 * via MMIO and return its value. This also clears pending interrupts
435 * Inherited from caller.
437 static u8
ata_check_status_mmio(struct ata_port
*ap
)
439 return readb((void __iomem
*) ap
->ioaddr
.status_addr
);
444 * ata_check_status - Read device status reg & clear interrupt
445 * @ap: port where the device is
447 * Reads ATA taskfile status register for currently-selected device
448 * and return its value. This also clears pending interrupts
451 * May be used as the check_status() entry in ata_port_operations.
454 * Inherited from caller.
456 u8
ata_check_status(struct ata_port
*ap
)
458 if (ap
->flags
& ATA_FLAG_MMIO
)
459 return ata_check_status_mmio(ap
);
460 return ata_check_status_pio(ap
);
465 * ata_altstatus - Read device alternate status reg
466 * @ap: port where the device is
468 * Reads ATA taskfile alternate status register for
469 * currently-selected device and return its value.
471 * Note: may NOT be used as the check_altstatus() entry in
472 * ata_port_operations.
475 * Inherited from caller.
477 u8
ata_altstatus(struct ata_port
*ap
)
479 if (ap
->ops
->check_altstatus
)
480 return ap
->ops
->check_altstatus(ap
);
482 if (ap
->flags
& ATA_FLAG_MMIO
)
483 return readb((void __iomem
*)ap
->ioaddr
.altstatus_addr
);
484 return inb(ap
->ioaddr
.altstatus_addr
);
489 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
490 * @tf: Taskfile to convert
491 * @fis: Buffer into which data will output
492 * @pmp: Port multiplier port
494 * Converts a standard ATA taskfile to a Serial ATA
495 * FIS structure (Register - Host to Device).
498 * Inherited from caller.
501 void ata_tf_to_fis(const struct ata_taskfile
*tf
, u8
*fis
, u8 pmp
)
503 fis
[0] = 0x27; /* Register - Host to Device FIS */
504 fis
[1] = (pmp
& 0xf) | (1 << 7); /* Port multiplier number,
505 bit 7 indicates Command FIS */
506 fis
[2] = tf
->command
;
507 fis
[3] = tf
->feature
;
514 fis
[8] = tf
->hob_lbal
;
515 fis
[9] = tf
->hob_lbam
;
516 fis
[10] = tf
->hob_lbah
;
517 fis
[11] = tf
->hob_feature
;
520 fis
[13] = tf
->hob_nsect
;
531 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
532 * @fis: Buffer from which data will be input
533 * @tf: Taskfile to output
535 * Converts a serial ATA FIS structure to a standard ATA taskfile.
538 * Inherited from caller.
541 void ata_tf_from_fis(const u8
*fis
, struct ata_taskfile
*tf
)
543 tf
->command
= fis
[2]; /* status */
544 tf
->feature
= fis
[3]; /* error */
551 tf
->hob_lbal
= fis
[8];
552 tf
->hob_lbam
= fis
[9];
553 tf
->hob_lbah
= fis
[10];
556 tf
->hob_nsect
= fis
[13];
559 static const u8 ata_rw_cmds
[] = {
563 ATA_CMD_READ_MULTI_EXT
,
564 ATA_CMD_WRITE_MULTI_EXT
,
568 ATA_CMD_WRITE_MULTI_FUA_EXT
,
572 ATA_CMD_PIO_READ_EXT
,
573 ATA_CMD_PIO_WRITE_EXT
,
586 ATA_CMD_WRITE_FUA_EXT
590 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
591 * @qc: command to examine and configure
593 * Examine the device configuration and tf->flags to calculate
594 * the proper read/write commands and protocol to use.
599 int ata_rwcmd_protocol(struct ata_queued_cmd
*qc
)
601 struct ata_taskfile
*tf
= &qc
->tf
;
602 struct ata_device
*dev
= qc
->dev
;
605 int index
, fua
, lba48
, write
;
607 fua
= (tf
->flags
& ATA_TFLAG_FUA
) ? 4 : 0;
608 lba48
= (tf
->flags
& ATA_TFLAG_LBA48
) ? 2 : 0;
609 write
= (tf
->flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
611 if (dev
->flags
& ATA_DFLAG_PIO
) {
612 tf
->protocol
= ATA_PROT_PIO
;
613 index
= dev
->multi_count
? 0 : 8;
615 tf
->protocol
= ATA_PROT_DMA
;
619 cmd
= ata_rw_cmds
[index
+ fua
+ lba48
+ write
];
627 static const char * const xfer_mode_str
[] = {
647 * ata_udma_string - convert UDMA bit offset to string
648 * @mask: mask of bits supported; only highest bit counts.
650 * Determine string which represents the highest speed
651 * (highest bit in @udma_mask).
657 * Constant C string representing highest speed listed in
658 * @udma_mask, or the constant C string "<n/a>".
661 static const char *ata_mode_string(unsigned int mask
)
665 for (i
= 7; i
>= 0; i
--)
668 for (i
= ATA_SHIFT_MWDMA
+ 2; i
>= ATA_SHIFT_MWDMA
; i
--)
671 for (i
= ATA_SHIFT_PIO
+ 4; i
>= ATA_SHIFT_PIO
; i
--)
678 return xfer_mode_str
[i
];
682 * ata_pio_devchk - PATA device presence detection
683 * @ap: ATA channel to examine
684 * @device: Device to examine (starting at zero)
686 * This technique was originally described in
687 * Hale Landis's ATADRVR (www.ata-atapi.com), and
688 * later found its way into the ATA/ATAPI spec.
690 * Write a pattern to the ATA shadow registers,
691 * and if a device is present, it will respond by
692 * correctly storing and echoing back the
693 * ATA shadow register contents.
699 static unsigned int ata_pio_devchk(struct ata_port
*ap
,
702 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
705 ap
->ops
->dev_select(ap
, device
);
707 outb(0x55, ioaddr
->nsect_addr
);
708 outb(0xaa, ioaddr
->lbal_addr
);
710 outb(0xaa, ioaddr
->nsect_addr
);
711 outb(0x55, ioaddr
->lbal_addr
);
713 outb(0x55, ioaddr
->nsect_addr
);
714 outb(0xaa, ioaddr
->lbal_addr
);
716 nsect
= inb(ioaddr
->nsect_addr
);
717 lbal
= inb(ioaddr
->lbal_addr
);
719 if ((nsect
== 0x55) && (lbal
== 0xaa))
720 return 1; /* we found a device */
722 return 0; /* nothing found */
726 * ata_mmio_devchk - PATA device presence detection
727 * @ap: ATA channel to examine
728 * @device: Device to examine (starting at zero)
730 * This technique was originally described in
731 * Hale Landis's ATADRVR (www.ata-atapi.com), and
732 * later found its way into the ATA/ATAPI spec.
734 * Write a pattern to the ATA shadow registers,
735 * and if a device is present, it will respond by
736 * correctly storing and echoing back the
737 * ATA shadow register contents.
743 static unsigned int ata_mmio_devchk(struct ata_port
*ap
,
746 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
749 ap
->ops
->dev_select(ap
, device
);
751 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
752 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
754 writeb(0xaa, (void __iomem
*) ioaddr
->nsect_addr
);
755 writeb(0x55, (void __iomem
*) ioaddr
->lbal_addr
);
757 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
758 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
760 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
761 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
763 if ((nsect
== 0x55) && (lbal
== 0xaa))
764 return 1; /* we found a device */
766 return 0; /* nothing found */
770 * ata_devchk - PATA device presence detection
771 * @ap: ATA channel to examine
772 * @device: Device to examine (starting at zero)
774 * Dispatch ATA device presence detection, depending
775 * on whether we are using PIO or MMIO to talk to the
776 * ATA shadow registers.
782 static unsigned int ata_devchk(struct ata_port
*ap
,
785 if (ap
->flags
& ATA_FLAG_MMIO
)
786 return ata_mmio_devchk(ap
, device
);
787 return ata_pio_devchk(ap
, device
);
791 * ata_dev_classify - determine device type based on ATA-spec signature
792 * @tf: ATA taskfile register set for device to be identified
794 * Determine from taskfile register contents whether a device is
795 * ATA or ATAPI, as per "Signature and persistence" section
796 * of ATA/PI spec (volume 1, sect 5.14).
802 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
803 * the event of failure.
806 unsigned int ata_dev_classify(const struct ata_taskfile
*tf
)
808 /* Apple's open source Darwin code hints that some devices only
809 * put a proper signature into the LBA mid/high registers,
810 * So, we only check those. It's sufficient for uniqueness.
813 if (((tf
->lbam
== 0) && (tf
->lbah
== 0)) ||
814 ((tf
->lbam
== 0x3c) && (tf
->lbah
== 0xc3))) {
815 DPRINTK("found ATA device by sig\n");
819 if (((tf
->lbam
== 0x14) && (tf
->lbah
== 0xeb)) ||
820 ((tf
->lbam
== 0x69) && (tf
->lbah
== 0x96))) {
821 DPRINTK("found ATAPI device by sig\n");
822 return ATA_DEV_ATAPI
;
825 DPRINTK("unknown device\n");
826 return ATA_DEV_UNKNOWN
;
830 * ata_dev_try_classify - Parse returned ATA device signature
831 * @ap: ATA channel to examine
832 * @device: Device to examine (starting at zero)
834 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
835 * an ATA/ATAPI-defined set of values is placed in the ATA
836 * shadow registers, indicating the results of device detection
839 * Select the ATA device, and read the values from the ATA shadow
840 * registers. Then parse according to the Error register value,
841 * and the spec-defined values examined by ata_dev_classify().
847 static u8
ata_dev_try_classify(struct ata_port
*ap
, unsigned int device
)
849 struct ata_device
*dev
= &ap
->device
[device
];
850 struct ata_taskfile tf
;
854 ap
->ops
->dev_select(ap
, device
);
856 memset(&tf
, 0, sizeof(tf
));
858 ap
->ops
->tf_read(ap
, &tf
);
861 dev
->class = ATA_DEV_NONE
;
863 /* see if device passed diags */
866 else if ((device
== 0) && (err
== 0x81))
871 /* determine if device if ATA or ATAPI */
872 class = ata_dev_classify(&tf
);
873 if (class == ATA_DEV_UNKNOWN
)
875 if ((class == ATA_DEV_ATA
) && (ata_chk_status(ap
) == 0))
884 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
885 * @id: IDENTIFY DEVICE results we will examine
886 * @s: string into which data is output
887 * @ofs: offset into identify device page
888 * @len: length of string to return. must be an even number.
890 * The strings in the IDENTIFY DEVICE page are broken up into
891 * 16-bit chunks. Run through the string, and output each
892 * 8-bit chunk linearly, regardless of platform.
898 void ata_dev_id_string(const u16
*id
, unsigned char *s
,
899 unsigned int ofs
, unsigned int len
)
919 * ata_noop_dev_select - Select device 0/1 on ATA bus
920 * @ap: ATA channel to manipulate
921 * @device: ATA device (numbered from zero) to select
923 * This function performs no actual function.
925 * May be used as the dev_select() entry in ata_port_operations.
930 void ata_noop_dev_select (struct ata_port
*ap
, unsigned int device
)
936 * ata_std_dev_select - Select device 0/1 on ATA bus
937 * @ap: ATA channel to manipulate
938 * @device: ATA device (numbered from zero) to select
940 * Use the method defined in the ATA specification to
941 * make either device 0, or device 1, active on the
942 * ATA channel. Works with both PIO and MMIO.
944 * May be used as the dev_select() entry in ata_port_operations.
950 void ata_std_dev_select (struct ata_port
*ap
, unsigned int device
)
955 tmp
= ATA_DEVICE_OBS
;
957 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
959 if (ap
->flags
& ATA_FLAG_MMIO
) {
960 writeb(tmp
, (void __iomem
*) ap
->ioaddr
.device_addr
);
962 outb(tmp
, ap
->ioaddr
.device_addr
);
964 ata_pause(ap
); /* needed; also flushes, for mmio */
968 * ata_dev_select - Select device 0/1 on ATA bus
969 * @ap: ATA channel to manipulate
970 * @device: ATA device (numbered from zero) to select
971 * @wait: non-zero to wait for Status register BSY bit to clear
972 * @can_sleep: non-zero if context allows sleeping
974 * Use the method defined in the ATA specification to
975 * make either device 0, or device 1, active on the
978 * This is a high-level version of ata_std_dev_select(),
979 * which additionally provides the services of inserting
980 * the proper pauses and status polling, where needed.
986 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
987 unsigned int wait
, unsigned int can_sleep
)
989 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
990 ap
->id
, device
, wait
);
995 ap
->ops
->dev_select(ap
, device
);
998 if (can_sleep
&& ap
->device
[device
].class == ATA_DEV_ATAPI
)
1005 * ata_dump_id - IDENTIFY DEVICE info debugging output
1006 * @dev: Device whose IDENTIFY DEVICE page we will dump
1008 * Dump selected 16-bit words from a detected device's
1009 * IDENTIFY PAGE page.
1015 static inline void ata_dump_id(const struct ata_device
*dev
)
1017 DPRINTK("49==0x%04x "
1027 DPRINTK("80==0x%04x "
1037 DPRINTK("88==0x%04x "
1044 * Compute the PIO modes available for this device. This is not as
1045 * trivial as it seems if we must consider early devices correctly.
1047 * FIXME: pre IDE drive timing (do we care ?).
1050 static unsigned int ata_pio_modes(const struct ata_device
*adev
)
1054 /* Usual case. Word 53 indicates word 88 is valid */
1055 if (adev
->id
[ATA_ID_FIELD_VALID
] & (1 << 2)) {
1056 modes
= adev
->id
[ATA_ID_PIO_MODES
] & 0x03;
1062 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1063 for the maximum. Turn it into a mask and return it */
1064 modes
= (2 << (adev
->id
[ATA_ID_OLD_PIO_MODES
] & 0xFF)) - 1 ;
1068 struct ata_exec_internal_arg
{
1069 unsigned int err_mask
;
1070 struct ata_taskfile
*tf
;
1071 struct completion
*waiting
;
1074 int ata_qc_complete_internal(struct ata_queued_cmd
*qc
)
1076 struct ata_exec_internal_arg
*arg
= qc
->private_data
;
1077 struct completion
*waiting
= arg
->waiting
;
1079 if (!(qc
->err_mask
& ~AC_ERR_DEV
))
1080 qc
->ap
->ops
->tf_read(qc
->ap
, arg
->tf
);
1081 arg
->err_mask
= qc
->err_mask
;
1082 arg
->waiting
= NULL
;
1089 * ata_exec_internal - execute libata internal command
1090 * @ap: Port to which the command is sent
1091 * @dev: Device to which the command is sent
1092 * @tf: Taskfile registers for the command and the result
1093 * @dma_dir: Data tranfer direction of the command
1094 * @buf: Data buffer of the command
1095 * @buflen: Length of data buffer
1097 * Executes libata internal command with timeout. @tf contains
1098 * command on entry and result on return. Timeout and error
1099 * conditions are reported via return value. No recovery action
1100 * is taken after a command times out. It's caller's duty to
1101 * clean up after timeout.
1104 * None. Should be called with kernel context, might sleep.
1108 ata_exec_internal(struct ata_port
*ap
, struct ata_device
*dev
,
1109 struct ata_taskfile
*tf
,
1110 int dma_dir
, void *buf
, unsigned int buflen
)
1112 u8 command
= tf
->command
;
1113 struct ata_queued_cmd
*qc
;
1114 DECLARE_COMPLETION(wait
);
1115 unsigned long flags
;
1116 struct ata_exec_internal_arg arg
;
1118 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1120 qc
= ata_qc_new_init(ap
, dev
);
1124 qc
->dma_dir
= dma_dir
;
1125 if (dma_dir
!= DMA_NONE
) {
1126 ata_sg_init_one(qc
, buf
, buflen
);
1127 qc
->nsect
= buflen
/ ATA_SECT_SIZE
;
1130 arg
.waiting
= &wait
;
1132 qc
->private_data
= &arg
;
1133 qc
->complete_fn
= ata_qc_complete_internal
;
1135 if (ata_qc_issue(qc
))
1138 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1140 if (!wait_for_completion_timeout(&wait
, ATA_TMOUT_INTERNAL
)) {
1141 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1143 /* We're racing with irq here. If we lose, the
1144 * following test prevents us from completing the qc
1145 * again. If completion irq occurs after here but
1146 * before the caller cleans up, it will result in a
1147 * spurious interrupt. We can live with that.
1150 qc
->err_mask
= AC_ERR_OTHER
;
1151 ata_qc_complete(qc
);
1152 printk(KERN_WARNING
"ata%u: qc timeout (cmd 0x%x)\n",
1156 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1159 return arg
.err_mask
;
1163 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1164 return AC_ERR_OTHER
;
1168 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1169 * @ap: port on which device we wish to probe resides
1170 * @device: device bus address, starting at zero
1172 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1173 * command, and read back the 512-byte device information page.
1174 * The device information page is fed to us via the standard
1175 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1176 * using standard PIO-IN paths)
1178 * After reading the device information page, we use several
1179 * bits of information from it to initialize data structures
1180 * that will be used during the lifetime of the ata_device.
1181 * Other data from the info page is used to disqualify certain
1182 * older ATA devices we do not wish to support.
1185 * Inherited from caller. Some functions called by this function
1186 * obtain the host_set lock.
1189 static void ata_dev_identify(struct ata_port
*ap
, unsigned int device
)
1191 struct ata_device
*dev
= &ap
->device
[device
];
1192 unsigned int major_version
;
1194 unsigned long xfer_modes
;
1195 unsigned int using_edd
;
1196 struct ata_taskfile tf
;
1197 unsigned int err_mask
;
1200 if (!ata_dev_present(dev
)) {
1201 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1206 if (ap
->flags
& (ATA_FLAG_SRST
| ATA_FLAG_SATA_RESET
))
1211 DPRINTK("ENTER, host %u, dev %u\n", ap
->id
, device
);
1213 assert (dev
->class == ATA_DEV_ATA
|| dev
->class == ATA_DEV_ATAPI
||
1214 dev
->class == ATA_DEV_NONE
);
1216 ata_dev_select(ap
, device
, 1, 1); /* select device 0/1 */
1219 ata_tf_init(ap
, &tf
, device
);
1221 if (dev
->class == ATA_DEV_ATA
) {
1222 tf
.command
= ATA_CMD_ID_ATA
;
1223 DPRINTK("do ATA identify\n");
1225 tf
.command
= ATA_CMD_ID_ATAPI
;
1226 DPRINTK("do ATAPI identify\n");
1229 tf
.protocol
= ATA_PROT_PIO
;
1231 err_mask
= ata_exec_internal(ap
, dev
, &tf
, DMA_FROM_DEVICE
,
1232 dev
->id
, sizeof(dev
->id
));
1235 if (err_mask
& ~AC_ERR_DEV
)
1239 * arg! EDD works for all test cases, but seems to return
1240 * the ATA signature for some ATAPI devices. Until the
1241 * reason for this is found and fixed, we fix up the mess
1242 * here. If IDENTIFY DEVICE returns command aborted
1243 * (as ATAPI devices do), then we issue an
1244 * IDENTIFY PACKET DEVICE.
1246 * ATA software reset (SRST, the default) does not appear
1247 * to have this problem.
1249 if ((using_edd
) && (dev
->class == ATA_DEV_ATA
)) {
1250 u8 err
= tf
.feature
;
1251 if (err
& ATA_ABORTED
) {
1252 dev
->class = ATA_DEV_ATAPI
;
1259 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
1261 /* print device capabilities */
1262 printk(KERN_DEBUG
"ata%u: dev %u cfg "
1263 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1264 ap
->id
, device
, dev
->id
[49],
1265 dev
->id
[82], dev
->id
[83], dev
->id
[84],
1266 dev
->id
[85], dev
->id
[86], dev
->id
[87],
1270 * common ATA, ATAPI feature tests
1273 /* we require DMA support (bits 8 of word 49) */
1274 if (!ata_id_has_dma(dev
->id
)) {
1275 printk(KERN_DEBUG
"ata%u: no dma\n", ap
->id
);
1279 /* quick-n-dirty find max transfer mode; for printk only */
1280 xfer_modes
= dev
->id
[ATA_ID_UDMA_MODES
];
1282 xfer_modes
= (dev
->id
[ATA_ID_MWDMA_MODES
]) << ATA_SHIFT_MWDMA
;
1284 xfer_modes
= ata_pio_modes(dev
);
1288 /* ATA-specific feature tests */
1289 if (dev
->class == ATA_DEV_ATA
) {
1290 if (!ata_id_is_ata(dev
->id
)) /* sanity check */
1293 /* get major version */
1294 tmp
= dev
->id
[ATA_ID_MAJOR_VER
];
1295 for (major_version
= 14; major_version
>= 1; major_version
--)
1296 if (tmp
& (1 << major_version
))
1300 * The exact sequence expected by certain pre-ATA4 drives is:
1303 * INITIALIZE DEVICE PARAMETERS
1305 * Some drives were very specific about that exact sequence.
1307 if (major_version
< 4 || (!ata_id_has_lba(dev
->id
))) {
1308 ata_dev_init_params(ap
, dev
);
1310 /* current CHS translation info (id[53-58]) might be
1311 * changed. reread the identify device info.
1313 ata_dev_reread_id(ap
, dev
);
1316 if (ata_id_has_lba(dev
->id
)) {
1317 dev
->flags
|= ATA_DFLAG_LBA
;
1319 if (ata_id_has_lba48(dev
->id
)) {
1320 dev
->flags
|= ATA_DFLAG_LBA48
;
1321 dev
->n_sectors
= ata_id_u64(dev
->id
, 100);
1323 dev
->n_sectors
= ata_id_u32(dev
->id
, 60);
1326 /* print device info to dmesg */
1327 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1330 ata_mode_string(xfer_modes
),
1331 (unsigned long long)dev
->n_sectors
,
1332 dev
->flags
& ATA_DFLAG_LBA48
? " LBA48" : " LBA");
1336 /* Default translation */
1337 dev
->cylinders
= dev
->id
[1];
1338 dev
->heads
= dev
->id
[3];
1339 dev
->sectors
= dev
->id
[6];
1340 dev
->n_sectors
= dev
->cylinders
* dev
->heads
* dev
->sectors
;
1342 if (ata_id_current_chs_valid(dev
->id
)) {
1343 /* Current CHS translation is valid. */
1344 dev
->cylinders
= dev
->id
[54];
1345 dev
->heads
= dev
->id
[55];
1346 dev
->sectors
= dev
->id
[56];
1348 dev
->n_sectors
= ata_id_u32(dev
->id
, 57);
1351 /* print device info to dmesg */
1352 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1355 ata_mode_string(xfer_modes
),
1356 (unsigned long long)dev
->n_sectors
,
1357 (int)dev
->cylinders
, (int)dev
->heads
, (int)dev
->sectors
);
1361 ap
->host
->max_cmd_len
= 16;
1364 /* ATAPI-specific feature tests */
1365 else if (dev
->class == ATA_DEV_ATAPI
) {
1366 if (ata_id_is_ata(dev
->id
)) /* sanity check */
1369 rc
= atapi_cdb_len(dev
->id
);
1370 if ((rc
< 12) || (rc
> ATAPI_CDB_LEN
)) {
1371 printk(KERN_WARNING
"ata%u: unsupported CDB len\n", ap
->id
);
1374 ap
->cdb_len
= (unsigned int) rc
;
1375 ap
->host
->max_cmd_len
= (unsigned char) ap
->cdb_len
;
1377 /* print device info to dmesg */
1378 printk(KERN_INFO
"ata%u: dev %u ATAPI, max %s\n",
1380 ata_mode_string(xfer_modes
));
1383 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap
));
1387 printk(KERN_WARNING
"ata%u: dev %u not supported, ignoring\n",
1390 dev
->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1391 DPRINTK("EXIT, err\n");
1395 static inline u8
ata_dev_knobble(const struct ata_port
*ap
)
1397 return ((ap
->cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(ap
->device
->id
)));
1401 * ata_dev_config - Run device specific handlers and check for
1402 * SATA->PATA bridges
1409 void ata_dev_config(struct ata_port
*ap
, unsigned int i
)
1411 /* limit bridge transfers to udma5, 200 sectors */
1412 if (ata_dev_knobble(ap
)) {
1413 printk(KERN_INFO
"ata%u(%u): applying bridge limits\n",
1414 ap
->id
, ap
->device
->devno
);
1415 ap
->udma_mask
&= ATA_UDMA5
;
1416 ap
->host
->max_sectors
= ATA_MAX_SECTORS
;
1417 ap
->host
->hostt
->max_sectors
= ATA_MAX_SECTORS
;
1418 ap
->device
->flags
|= ATA_DFLAG_LOCK_SECTORS
;
1421 if (ap
->ops
->dev_config
)
1422 ap
->ops
->dev_config(ap
, &ap
->device
[i
]);
1426 * ata_bus_probe - Reset and probe ATA bus
1429 * Master ATA bus probing function. Initiates a hardware-dependent
1430 * bus reset, then attempts to identify any devices found on
1434 * PCI/etc. bus probe sem.
1437 * Zero on success, non-zero on error.
1440 static int ata_bus_probe(struct ata_port
*ap
)
1442 unsigned int i
, found
= 0;
1444 ap
->ops
->phy_reset(ap
);
1445 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1448 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1449 ata_dev_identify(ap
, i
);
1450 if (ata_dev_present(&ap
->device
[i
])) {
1452 ata_dev_config(ap
,i
);
1456 if ((!found
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1457 goto err_out_disable
;
1460 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1461 goto err_out_disable
;
1466 ap
->ops
->port_disable(ap
);
1472 * ata_port_probe - Mark port as enabled
1473 * @ap: Port for which we indicate enablement
1475 * Modify @ap data structure such that the system
1476 * thinks that the entire port is enabled.
1478 * LOCKING: host_set lock, or some other form of
1482 void ata_port_probe(struct ata_port
*ap
)
1484 ap
->flags
&= ~ATA_FLAG_PORT_DISABLED
;
1488 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1489 * @ap: SATA port associated with target SATA PHY.
1491 * This function issues commands to standard SATA Sxxx
1492 * PHY registers, to wake up the phy (and device), and
1493 * clear any reset condition.
1496 * PCI/etc. bus probe sem.
1499 void __sata_phy_reset(struct ata_port
*ap
)
1502 unsigned long timeout
= jiffies
+ (HZ
* 5);
1504 if (ap
->flags
& ATA_FLAG_SATA_RESET
) {
1505 /* issue phy wake/reset */
1506 scr_write_flush(ap
, SCR_CONTROL
, 0x301);
1507 /* Couldn't find anything in SATA I/II specs, but
1508 * AHCI-1.1 10.4.2 says at least 1 ms. */
1511 scr_write_flush(ap
, SCR_CONTROL
, 0x300); /* phy wake/clear reset */
1513 /* wait for phy to become ready, if necessary */
1516 sstatus
= scr_read(ap
, SCR_STATUS
);
1517 if ((sstatus
& 0xf) != 1)
1519 } while (time_before(jiffies
, timeout
));
1521 /* TODO: phy layer with polling, timeouts, etc. */
1522 sstatus
= scr_read(ap
, SCR_STATUS
);
1523 if (sata_dev_present(ap
)) {
1527 tmp
= (sstatus
>> 4) & 0xf;
1530 else if (tmp
& (1 << 1))
1533 speed
= "<unknown>";
1534 printk(KERN_INFO
"ata%u: SATA link up %s Gbps (SStatus %X)\n",
1535 ap
->id
, speed
, sstatus
);
1538 printk(KERN_INFO
"ata%u: SATA link down (SStatus %X)\n",
1540 ata_port_disable(ap
);
1543 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1546 if (ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
)) {
1547 ata_port_disable(ap
);
1551 ap
->cbl
= ATA_CBL_SATA
;
1555 * sata_phy_reset - Reset SATA bus.
1556 * @ap: SATA port associated with target SATA PHY.
1558 * This function resets the SATA bus, and then probes
1559 * the bus for devices.
1562 * PCI/etc. bus probe sem.
1565 void sata_phy_reset(struct ata_port
*ap
)
1567 __sata_phy_reset(ap
);
1568 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1574 * ata_port_disable - Disable port.
1575 * @ap: Port to be disabled.
1577 * Modify @ap data structure such that the system
1578 * thinks that the entire port is disabled, and should
1579 * never attempt to probe or communicate with devices
1582 * LOCKING: host_set lock, or some other form of
1586 void ata_port_disable(struct ata_port
*ap
)
1588 ap
->device
[0].class = ATA_DEV_NONE
;
1589 ap
->device
[1].class = ATA_DEV_NONE
;
1590 ap
->flags
|= ATA_FLAG_PORT_DISABLED
;
1594 * This mode timing computation functionality is ported over from
1595 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1598 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1599 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1600 * for PIO 5, which is a nonstandard extension and UDMA6, which
1601 * is currently supported only by Maxtor drives.
1604 static const struct ata_timing ata_timing
[] = {
1606 { XFER_UDMA_6
, 0, 0, 0, 0, 0, 0, 0, 15 },
1607 { XFER_UDMA_5
, 0, 0, 0, 0, 0, 0, 0, 20 },
1608 { XFER_UDMA_4
, 0, 0, 0, 0, 0, 0, 0, 30 },
1609 { XFER_UDMA_3
, 0, 0, 0, 0, 0, 0, 0, 45 },
1611 { XFER_UDMA_2
, 0, 0, 0, 0, 0, 0, 0, 60 },
1612 { XFER_UDMA_1
, 0, 0, 0, 0, 0, 0, 0, 80 },
1613 { XFER_UDMA_0
, 0, 0, 0, 0, 0, 0, 0, 120 },
1615 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1617 { XFER_MW_DMA_2
, 25, 0, 0, 0, 70, 25, 120, 0 },
1618 { XFER_MW_DMA_1
, 45, 0, 0, 0, 80, 50, 150, 0 },
1619 { XFER_MW_DMA_0
, 60, 0, 0, 0, 215, 215, 480, 0 },
1621 { XFER_SW_DMA_2
, 60, 0, 0, 0, 120, 120, 240, 0 },
1622 { XFER_SW_DMA_1
, 90, 0, 0, 0, 240, 240, 480, 0 },
1623 { XFER_SW_DMA_0
, 120, 0, 0, 0, 480, 480, 960, 0 },
1625 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1626 { XFER_PIO_4
, 25, 70, 25, 120, 70, 25, 120, 0 },
1627 { XFER_PIO_3
, 30, 80, 70, 180, 80, 70, 180, 0 },
1629 { XFER_PIO_2
, 30, 290, 40, 330, 100, 90, 240, 0 },
1630 { XFER_PIO_1
, 50, 290, 93, 383, 125, 100, 383, 0 },
1631 { XFER_PIO_0
, 70, 290, 240, 600, 165, 150, 600, 0 },
1633 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1638 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1639 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1641 static void ata_timing_quantize(const struct ata_timing
*t
, struct ata_timing
*q
, int T
, int UT
)
1643 q
->setup
= EZ(t
->setup
* 1000, T
);
1644 q
->act8b
= EZ(t
->act8b
* 1000, T
);
1645 q
->rec8b
= EZ(t
->rec8b
* 1000, T
);
1646 q
->cyc8b
= EZ(t
->cyc8b
* 1000, T
);
1647 q
->active
= EZ(t
->active
* 1000, T
);
1648 q
->recover
= EZ(t
->recover
* 1000, T
);
1649 q
->cycle
= EZ(t
->cycle
* 1000, T
);
1650 q
->udma
= EZ(t
->udma
* 1000, UT
);
1653 void ata_timing_merge(const struct ata_timing
*a
, const struct ata_timing
*b
,
1654 struct ata_timing
*m
, unsigned int what
)
1656 if (what
& ATA_TIMING_SETUP
) m
->setup
= max(a
->setup
, b
->setup
);
1657 if (what
& ATA_TIMING_ACT8B
) m
->act8b
= max(a
->act8b
, b
->act8b
);
1658 if (what
& ATA_TIMING_REC8B
) m
->rec8b
= max(a
->rec8b
, b
->rec8b
);
1659 if (what
& ATA_TIMING_CYC8B
) m
->cyc8b
= max(a
->cyc8b
, b
->cyc8b
);
1660 if (what
& ATA_TIMING_ACTIVE
) m
->active
= max(a
->active
, b
->active
);
1661 if (what
& ATA_TIMING_RECOVER
) m
->recover
= max(a
->recover
, b
->recover
);
1662 if (what
& ATA_TIMING_CYCLE
) m
->cycle
= max(a
->cycle
, b
->cycle
);
1663 if (what
& ATA_TIMING_UDMA
) m
->udma
= max(a
->udma
, b
->udma
);
1666 static const struct ata_timing
* ata_timing_find_mode(unsigned short speed
)
1668 const struct ata_timing
*t
;
1670 for (t
= ata_timing
; t
->mode
!= speed
; t
++)
1671 if (t
->mode
== 0xFF)
1676 int ata_timing_compute(struct ata_device
*adev
, unsigned short speed
,
1677 struct ata_timing
*t
, int T
, int UT
)
1679 const struct ata_timing
*s
;
1680 struct ata_timing p
;
1686 if (!(s
= ata_timing_find_mode(speed
)))
1689 memcpy(t
, s
, sizeof(*s
));
1692 * If the drive is an EIDE drive, it can tell us it needs extended
1693 * PIO/MW_DMA cycle timing.
1696 if (adev
->id
[ATA_ID_FIELD_VALID
] & 2) { /* EIDE drive */
1697 memset(&p
, 0, sizeof(p
));
1698 if(speed
>= XFER_PIO_0
&& speed
<= XFER_SW_DMA_0
) {
1699 if (speed
<= XFER_PIO_2
) p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO
];
1700 else p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO_IORDY
];
1701 } else if(speed
>= XFER_MW_DMA_0
&& speed
<= XFER_MW_DMA_2
) {
1702 p
.cycle
= adev
->id
[ATA_ID_EIDE_DMA_MIN
];
1704 ata_timing_merge(&p
, t
, t
, ATA_TIMING_CYCLE
| ATA_TIMING_CYC8B
);
1708 * Convert the timing to bus clock counts.
1711 ata_timing_quantize(t
, t
, T
, UT
);
1714 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1715 * and some other commands. We have to ensure that the DMA cycle timing is
1716 * slower/equal than the fastest PIO timing.
1719 if (speed
> XFER_PIO_4
) {
1720 ata_timing_compute(adev
, adev
->pio_mode
, &p
, T
, UT
);
1721 ata_timing_merge(&p
, t
, t
, ATA_TIMING_ALL
);
1725 * Lenghten active & recovery time so that cycle time is correct.
1728 if (t
->act8b
+ t
->rec8b
< t
->cyc8b
) {
1729 t
->act8b
+= (t
->cyc8b
- (t
->act8b
+ t
->rec8b
)) / 2;
1730 t
->rec8b
= t
->cyc8b
- t
->act8b
;
1733 if (t
->active
+ t
->recover
< t
->cycle
) {
1734 t
->active
+= (t
->cycle
- (t
->active
+ t
->recover
)) / 2;
1735 t
->recover
= t
->cycle
- t
->active
;
1741 static const struct {
1744 } xfer_mode_classes
[] = {
1745 { ATA_SHIFT_UDMA
, XFER_UDMA_0
},
1746 { ATA_SHIFT_MWDMA
, XFER_MW_DMA_0
},
1747 { ATA_SHIFT_PIO
, XFER_PIO_0
},
1750 static inline u8
base_from_shift(unsigned int shift
)
1754 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++)
1755 if (xfer_mode_classes
[i
].shift
== shift
)
1756 return xfer_mode_classes
[i
].base
;
1761 static void ata_dev_set_mode(struct ata_port
*ap
, struct ata_device
*dev
)
1766 if (!ata_dev_present(dev
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1769 if (dev
->xfer_shift
== ATA_SHIFT_PIO
)
1770 dev
->flags
|= ATA_DFLAG_PIO
;
1772 ata_dev_set_xfermode(ap
, dev
);
1774 base
= base_from_shift(dev
->xfer_shift
);
1775 ofs
= dev
->xfer_mode
- base
;
1776 idx
= ofs
+ dev
->xfer_shift
;
1777 WARN_ON(idx
>= ARRAY_SIZE(xfer_mode_str
));
1779 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1780 idx
, dev
->xfer_shift
, (int)dev
->xfer_mode
, (int)base
, ofs
);
1782 printk(KERN_INFO
"ata%u: dev %u configured for %s\n",
1783 ap
->id
, dev
->devno
, xfer_mode_str
[idx
]);
1786 static int ata_host_set_pio(struct ata_port
*ap
)
1792 mask
= ata_get_mode_mask(ap
, ATA_SHIFT_PIO
);
1795 printk(KERN_WARNING
"ata%u: no PIO support\n", ap
->id
);
1799 base
= base_from_shift(ATA_SHIFT_PIO
);
1800 xfer_mode
= base
+ x
;
1802 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1803 (int)base
, (int)xfer_mode
, mask
, x
);
1805 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1806 struct ata_device
*dev
= &ap
->device
[i
];
1807 if (ata_dev_present(dev
)) {
1808 dev
->pio_mode
= xfer_mode
;
1809 dev
->xfer_mode
= xfer_mode
;
1810 dev
->xfer_shift
= ATA_SHIFT_PIO
;
1811 if (ap
->ops
->set_piomode
)
1812 ap
->ops
->set_piomode(ap
, dev
);
1819 static void ata_host_set_dma(struct ata_port
*ap
, u8 xfer_mode
,
1820 unsigned int xfer_shift
)
1824 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1825 struct ata_device
*dev
= &ap
->device
[i
];
1826 if (ata_dev_present(dev
)) {
1827 dev
->dma_mode
= xfer_mode
;
1828 dev
->xfer_mode
= xfer_mode
;
1829 dev
->xfer_shift
= xfer_shift
;
1830 if (ap
->ops
->set_dmamode
)
1831 ap
->ops
->set_dmamode(ap
, dev
);
1837 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1838 * @ap: port on which timings will be programmed
1840 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1843 * PCI/etc. bus probe sem.
1846 static void ata_set_mode(struct ata_port
*ap
)
1848 unsigned int xfer_shift
;
1852 /* step 1: always set host PIO timings */
1853 rc
= ata_host_set_pio(ap
);
1857 /* step 2: choose the best data xfer mode */
1858 xfer_mode
= xfer_shift
= 0;
1859 rc
= ata_choose_xfer_mode(ap
, &xfer_mode
, &xfer_shift
);
1863 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1864 if (xfer_shift
!= ATA_SHIFT_PIO
)
1865 ata_host_set_dma(ap
, xfer_mode
, xfer_shift
);
1867 /* step 4: update devices' xfer mode */
1868 ata_dev_set_mode(ap
, &ap
->device
[0]);
1869 ata_dev_set_mode(ap
, &ap
->device
[1]);
1871 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1874 if (ap
->ops
->post_set_mode
)
1875 ap
->ops
->post_set_mode(ap
);
1880 ata_port_disable(ap
);
1884 * ata_busy_sleep - sleep until BSY clears, or timeout
1885 * @ap: port containing status register to be polled
1886 * @tmout_pat: impatience timeout
1887 * @tmout: overall timeout
1889 * Sleep until ATA Status register bit BSY clears,
1890 * or a timeout occurs.
1896 static unsigned int ata_busy_sleep (struct ata_port
*ap
,
1897 unsigned long tmout_pat
,
1898 unsigned long tmout
)
1900 unsigned long timer_start
, timeout
;
1903 status
= ata_busy_wait(ap
, ATA_BUSY
, 300);
1904 timer_start
= jiffies
;
1905 timeout
= timer_start
+ tmout_pat
;
1906 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1908 status
= ata_busy_wait(ap
, ATA_BUSY
, 3);
1911 if (status
& ATA_BUSY
)
1912 printk(KERN_WARNING
"ata%u is slow to respond, "
1913 "please be patient\n", ap
->id
);
1915 timeout
= timer_start
+ tmout
;
1916 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1918 status
= ata_chk_status(ap
);
1921 if (status
& ATA_BUSY
) {
1922 printk(KERN_ERR
"ata%u failed to respond (%lu secs)\n",
1923 ap
->id
, tmout
/ HZ
);
1930 static void ata_bus_post_reset(struct ata_port
*ap
, unsigned int devmask
)
1932 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1933 unsigned int dev0
= devmask
& (1 << 0);
1934 unsigned int dev1
= devmask
& (1 << 1);
1935 unsigned long timeout
;
1937 /* if device 0 was found in ata_devchk, wait for its
1941 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1943 /* if device 1 was found in ata_devchk, wait for
1944 * register access, then wait for BSY to clear
1946 timeout
= jiffies
+ ATA_TMOUT_BOOT
;
1950 ap
->ops
->dev_select(ap
, 1);
1951 if (ap
->flags
& ATA_FLAG_MMIO
) {
1952 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
1953 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
1955 nsect
= inb(ioaddr
->nsect_addr
);
1956 lbal
= inb(ioaddr
->lbal_addr
);
1958 if ((nsect
== 1) && (lbal
== 1))
1960 if (time_after(jiffies
, timeout
)) {
1964 msleep(50); /* give drive a breather */
1967 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1969 /* is all this really necessary? */
1970 ap
->ops
->dev_select(ap
, 0);
1972 ap
->ops
->dev_select(ap
, 1);
1974 ap
->ops
->dev_select(ap
, 0);
1978 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1979 * @ap: Port to reset and probe
1981 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1982 * probe the bus. Not often used these days.
1985 * PCI/etc. bus probe sem.
1986 * Obtains host_set lock.
1990 static unsigned int ata_bus_edd(struct ata_port
*ap
)
1992 struct ata_taskfile tf
;
1993 unsigned long flags
;
1995 /* set up execute-device-diag (bus reset) taskfile */
1996 /* also, take interrupts to a known state (disabled) */
1997 DPRINTK("execute-device-diag\n");
1998 ata_tf_init(ap
, &tf
, 0);
2000 tf
.command
= ATA_CMD_EDD
;
2001 tf
.protocol
= ATA_PROT_NODATA
;
2004 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2005 ata_tf_to_host(ap
, &tf
);
2006 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2008 /* spec says at least 2ms. but who knows with those
2009 * crazy ATAPI devices...
2013 return ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
2016 static unsigned int ata_bus_softreset(struct ata_port
*ap
,
2017 unsigned int devmask
)
2019 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
2021 DPRINTK("ata%u: bus reset via SRST\n", ap
->id
);
2023 /* software reset. causes dev0 to be selected */
2024 if (ap
->flags
& ATA_FLAG_MMIO
) {
2025 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2026 udelay(20); /* FIXME: flush */
2027 writeb(ap
->ctl
| ATA_SRST
, (void __iomem
*) ioaddr
->ctl_addr
);
2028 udelay(20); /* FIXME: flush */
2029 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2031 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2033 outb(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
2035 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2038 /* spec mandates ">= 2ms" before checking status.
2039 * We wait 150ms, because that was the magic delay used for
2040 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2041 * between when the ATA command register is written, and then
2042 * status is checked. Because waiting for "a while" before
2043 * checking status is fine, post SRST, we perform this magic
2044 * delay here as well.
2048 ata_bus_post_reset(ap
, devmask
);
2054 * ata_bus_reset - reset host port and associated ATA channel
2055 * @ap: port to reset
2057 * This is typically the first time we actually start issuing
2058 * commands to the ATA channel. We wait for BSY to clear, then
2059 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2060 * result. Determine what devices, if any, are on the channel
2061 * by looking at the device 0/1 error register. Look at the signature
2062 * stored in each device's taskfile registers, to determine if
2063 * the device is ATA or ATAPI.
2066 * PCI/etc. bus probe sem.
2067 * Obtains host_set lock.
2070 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2073 void ata_bus_reset(struct ata_port
*ap
)
2075 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
2076 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
2078 unsigned int dev0
, dev1
= 0, rc
= 0, devmask
= 0;
2080 DPRINTK("ENTER, host %u, port %u\n", ap
->id
, ap
->port_no
);
2082 /* determine if device 0/1 are present */
2083 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
2086 dev0
= ata_devchk(ap
, 0);
2088 dev1
= ata_devchk(ap
, 1);
2092 devmask
|= (1 << 0);
2094 devmask
|= (1 << 1);
2096 /* select device 0 again */
2097 ap
->ops
->dev_select(ap
, 0);
2099 /* issue bus reset */
2100 if (ap
->flags
& ATA_FLAG_SRST
)
2101 rc
= ata_bus_softreset(ap
, devmask
);
2102 else if ((ap
->flags
& ATA_FLAG_SATA_RESET
) == 0) {
2103 /* set up device control */
2104 if (ap
->flags
& ATA_FLAG_MMIO
)
2105 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2107 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2108 rc
= ata_bus_edd(ap
);
2115 * determine by signature whether we have ATA or ATAPI devices
2117 err
= ata_dev_try_classify(ap
, 0);
2118 if ((slave_possible
) && (err
!= 0x81))
2119 ata_dev_try_classify(ap
, 1);
2121 /* re-enable interrupts */
2122 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
2125 /* is double-select really necessary? */
2126 if (ap
->device
[1].class != ATA_DEV_NONE
)
2127 ap
->ops
->dev_select(ap
, 1);
2128 if (ap
->device
[0].class != ATA_DEV_NONE
)
2129 ap
->ops
->dev_select(ap
, 0);
2131 /* if no devices were detected, disable this port */
2132 if ((ap
->device
[0].class == ATA_DEV_NONE
) &&
2133 (ap
->device
[1].class == ATA_DEV_NONE
))
2136 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
2137 /* set up device control for ATA_FLAG_SATA_RESET */
2138 if (ap
->flags
& ATA_FLAG_MMIO
)
2139 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2141 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2148 printk(KERN_ERR
"ata%u: disabling port\n", ap
->id
);
2149 ap
->ops
->port_disable(ap
);
2154 static void ata_pr_blacklisted(const struct ata_port
*ap
,
2155 const struct ata_device
*dev
)
2157 printk(KERN_WARNING
"ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2158 ap
->id
, dev
->devno
);
2161 static const char * const ata_dma_blacklist
[] = {
2180 "Toshiba CD-ROM XM-6202B",
2181 "TOSHIBA CD-ROM XM-1702BC",
2183 "E-IDE CD-ROM CR-840",
2186 "SAMSUNG CD-ROM SC-148C",
2187 "SAMSUNG CD-ROM SC",
2189 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2193 static int ata_dma_blacklisted(const struct ata_device
*dev
)
2195 unsigned char model_num
[40];
2200 ata_dev_id_string(dev
->id
, model_num
, ATA_ID_PROD_OFS
,
2203 len
= strnlen(s
, sizeof(model_num
));
2205 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2206 while ((len
> 0) && (s
[len
- 1] == ' ')) {
2211 for (i
= 0; i
< ARRAY_SIZE(ata_dma_blacklist
); i
++)
2212 if (!strncmp(ata_dma_blacklist
[i
], s
, len
))
2218 static unsigned int ata_get_mode_mask(const struct ata_port
*ap
, int shift
)
2220 const struct ata_device
*master
, *slave
;
2223 master
= &ap
->device
[0];
2224 slave
= &ap
->device
[1];
2226 assert (ata_dev_present(master
) || ata_dev_present(slave
));
2228 if (shift
== ATA_SHIFT_UDMA
) {
2229 mask
= ap
->udma_mask
;
2230 if (ata_dev_present(master
)) {
2231 mask
&= (master
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2232 if (ata_dma_blacklisted(master
)) {
2234 ata_pr_blacklisted(ap
, master
);
2237 if (ata_dev_present(slave
)) {
2238 mask
&= (slave
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2239 if (ata_dma_blacklisted(slave
)) {
2241 ata_pr_blacklisted(ap
, slave
);
2245 else if (shift
== ATA_SHIFT_MWDMA
) {
2246 mask
= ap
->mwdma_mask
;
2247 if (ata_dev_present(master
)) {
2248 mask
&= (master
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2249 if (ata_dma_blacklisted(master
)) {
2251 ata_pr_blacklisted(ap
, master
);
2254 if (ata_dev_present(slave
)) {
2255 mask
&= (slave
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2256 if (ata_dma_blacklisted(slave
)) {
2258 ata_pr_blacklisted(ap
, slave
);
2262 else if (shift
== ATA_SHIFT_PIO
) {
2263 mask
= ap
->pio_mask
;
2264 if (ata_dev_present(master
)) {
2265 /* spec doesn't return explicit support for
2266 * PIO0-2, so we fake it
2268 u16 tmp_mode
= master
->id
[ATA_ID_PIO_MODES
] & 0x03;
2273 if (ata_dev_present(slave
)) {
2274 /* spec doesn't return explicit support for
2275 * PIO0-2, so we fake it
2277 u16 tmp_mode
= slave
->id
[ATA_ID_PIO_MODES
] & 0x03;
2284 mask
= 0xffffffff; /* shut up compiler warning */
2291 /* find greatest bit */
2292 static int fgb(u32 bitmap
)
2297 for (i
= 0; i
< 32; i
++)
2298 if (bitmap
& (1 << i
))
2305 * ata_choose_xfer_mode - attempt to find best transfer mode
2306 * @ap: Port for which an xfer mode will be selected
2307 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2308 * @xfer_shift_out: (output) bit shift that selects this mode
2310 * Based on host and device capabilities, determine the
2311 * maximum transfer mode that is amenable to all.
2314 * PCI/etc. bus probe sem.
2317 * Zero on success, negative on error.
2320 static int ata_choose_xfer_mode(const struct ata_port
*ap
,
2322 unsigned int *xfer_shift_out
)
2324 unsigned int mask
, shift
;
2327 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++) {
2328 shift
= xfer_mode_classes
[i
].shift
;
2329 mask
= ata_get_mode_mask(ap
, shift
);
2333 *xfer_mode_out
= xfer_mode_classes
[i
].base
+ x
;
2334 *xfer_shift_out
= shift
;
2343 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2344 * @ap: Port associated with device @dev
2345 * @dev: Device to which command will be sent
2347 * Issue SET FEATURES - XFER MODE command to device @dev
2351 * PCI/etc. bus probe sem.
2354 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
)
2356 struct ata_taskfile tf
;
2358 /* set up set-features taskfile */
2359 DPRINTK("set features - xfer mode\n");
2361 ata_tf_init(ap
, &tf
, dev
->devno
);
2362 tf
.command
= ATA_CMD_SET_FEATURES
;
2363 tf
.feature
= SETFEATURES_XFER
;
2364 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2365 tf
.protocol
= ATA_PROT_NODATA
;
2366 tf
.nsect
= dev
->xfer_mode
;
2368 if (ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0)) {
2369 printk(KERN_ERR
"ata%u: failed to set xfermode, disabled\n",
2371 ata_port_disable(ap
);
2378 * ata_dev_reread_id - Reread the device identify device info
2379 * @ap: port where the device is
2380 * @dev: device to reread the identify device info
2385 static void ata_dev_reread_id(struct ata_port
*ap
, struct ata_device
*dev
)
2387 struct ata_taskfile tf
;
2389 ata_tf_init(ap
, &tf
, dev
->devno
);
2391 if (dev
->class == ATA_DEV_ATA
) {
2392 tf
.command
= ATA_CMD_ID_ATA
;
2393 DPRINTK("do ATA identify\n");
2395 tf
.command
= ATA_CMD_ID_ATAPI
;
2396 DPRINTK("do ATAPI identify\n");
2399 tf
.flags
|= ATA_TFLAG_DEVICE
;
2400 tf
.protocol
= ATA_PROT_PIO
;
2402 if (ata_exec_internal(ap
, dev
, &tf
, DMA_FROM_DEVICE
,
2403 dev
->id
, sizeof(dev
->id
)))
2406 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
2414 printk(KERN_ERR
"ata%u: failed to reread ID, disabled\n", ap
->id
);
2415 ata_port_disable(ap
);
2419 * ata_dev_init_params - Issue INIT DEV PARAMS command
2420 * @ap: Port associated with device @dev
2421 * @dev: Device to which command will be sent
2426 static void ata_dev_init_params(struct ata_port
*ap
, struct ata_device
*dev
)
2428 struct ata_taskfile tf
;
2429 u16 sectors
= dev
->id
[6];
2430 u16 heads
= dev
->id
[3];
2432 /* Number of sectors per track 1-255. Number of heads 1-16 */
2433 if (sectors
< 1 || sectors
> 255 || heads
< 1 || heads
> 16)
2436 /* set up init dev params taskfile */
2437 DPRINTK("init dev params \n");
2439 ata_tf_init(ap
, &tf
, dev
->devno
);
2440 tf
.command
= ATA_CMD_INIT_DEV_PARAMS
;
2441 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2442 tf
.protocol
= ATA_PROT_NODATA
;
2444 tf
.device
|= (heads
- 1) & 0x0f; /* max head = num. of heads - 1 */
2446 if (ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0)) {
2447 printk(KERN_ERR
"ata%u: failed to init parameters, disabled\n",
2449 ata_port_disable(ap
);
2456 * ata_sg_clean - Unmap DMA memory associated with command
2457 * @qc: Command containing DMA memory to be released
2459 * Unmap all mapped DMA memory associated with this command.
2462 * spin_lock_irqsave(host_set lock)
2465 static void ata_sg_clean(struct ata_queued_cmd
*qc
)
2467 struct ata_port
*ap
= qc
->ap
;
2468 struct scatterlist
*sg
= qc
->__sg
;
2469 int dir
= qc
->dma_dir
;
2470 void *pad_buf
= NULL
;
2472 assert(qc
->flags
& ATA_QCFLAG_DMAMAP
);
2475 if (qc
->flags
& ATA_QCFLAG_SINGLE
)
2476 assert(qc
->n_elem
== 1);
2478 VPRINTK("unmapping %u sg elements\n", qc
->n_elem
);
2480 /* if we padded the buffer out to 32-bit bound, and data
2481 * xfer direction is from-device, we must copy from the
2482 * pad buffer back into the supplied buffer
2484 if (qc
->pad_len
&& !(qc
->tf
.flags
& ATA_TFLAG_WRITE
))
2485 pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2487 if (qc
->flags
& ATA_QCFLAG_SG
) {
2489 dma_unmap_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2490 /* restore last sg */
2491 sg
[qc
->orig_n_elem
- 1].length
+= qc
->pad_len
;
2493 struct scatterlist
*psg
= &qc
->pad_sgent
;
2494 void *addr
= kmap_atomic(psg
->page
, KM_IRQ0
);
2495 memcpy(addr
+ psg
->offset
, pad_buf
, qc
->pad_len
);
2496 kunmap_atomic(addr
, KM_IRQ0
);
2499 if (sg_dma_len(&sg
[0]) > 0)
2500 dma_unmap_single(ap
->host_set
->dev
,
2501 sg_dma_address(&sg
[0]), sg_dma_len(&sg
[0]),
2504 sg
->length
+= qc
->pad_len
;
2506 memcpy(qc
->buf_virt
+ sg
->length
- qc
->pad_len
,
2507 pad_buf
, qc
->pad_len
);
2510 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
2515 * ata_fill_sg - Fill PCI IDE PRD table
2516 * @qc: Metadata associated with taskfile to be transferred
2518 * Fill PCI IDE PRD (scatter-gather) table with segments
2519 * associated with the current disk command.
2522 * spin_lock_irqsave(host_set lock)
2525 static void ata_fill_sg(struct ata_queued_cmd
*qc
)
2527 struct ata_port
*ap
= qc
->ap
;
2528 struct scatterlist
*sg
;
2531 assert(qc
->__sg
!= NULL
);
2532 assert(qc
->n_elem
> 0);
2535 ata_for_each_sg(sg
, qc
) {
2539 /* determine if physical DMA addr spans 64K boundary.
2540 * Note h/w doesn't support 64-bit, so we unconditionally
2541 * truncate dma_addr_t to u32.
2543 addr
= (u32
) sg_dma_address(sg
);
2544 sg_len
= sg_dma_len(sg
);
2547 offset
= addr
& 0xffff;
2549 if ((offset
+ sg_len
) > 0x10000)
2550 len
= 0x10000 - offset
;
2552 ap
->prd
[idx
].addr
= cpu_to_le32(addr
);
2553 ap
->prd
[idx
].flags_len
= cpu_to_le32(len
& 0xffff);
2554 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx
, addr
, len
);
2563 ap
->prd
[idx
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
2566 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2567 * @qc: Metadata associated with taskfile to check
2569 * Allow low-level driver to filter ATA PACKET commands, returning
2570 * a status indicating whether or not it is OK to use DMA for the
2571 * supplied PACKET command.
2574 * spin_lock_irqsave(host_set lock)
2576 * RETURNS: 0 when ATAPI DMA can be used
2579 int ata_check_atapi_dma(struct ata_queued_cmd
*qc
)
2581 struct ata_port
*ap
= qc
->ap
;
2582 int rc
= 0; /* Assume ATAPI DMA is OK by default */
2584 if (ap
->ops
->check_atapi_dma
)
2585 rc
= ap
->ops
->check_atapi_dma(qc
);
2590 * ata_qc_prep - Prepare taskfile for submission
2591 * @qc: Metadata associated with taskfile to be prepared
2593 * Prepare ATA taskfile for submission.
2596 * spin_lock_irqsave(host_set lock)
2598 void ata_qc_prep(struct ata_queued_cmd
*qc
)
2600 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
2607 * ata_sg_init_one - Associate command with memory buffer
2608 * @qc: Command to be associated
2609 * @buf: Memory buffer
2610 * @buflen: Length of memory buffer, in bytes.
2612 * Initialize the data-related elements of queued_cmd @qc
2613 * to point to a single memory buffer, @buf of byte length @buflen.
2616 * spin_lock_irqsave(host_set lock)
2619 void ata_sg_init_one(struct ata_queued_cmd
*qc
, void *buf
, unsigned int buflen
)
2621 struct scatterlist
*sg
;
2623 qc
->flags
|= ATA_QCFLAG_SINGLE
;
2625 memset(&qc
->sgent
, 0, sizeof(qc
->sgent
));
2626 qc
->__sg
= &qc
->sgent
;
2628 qc
->orig_n_elem
= 1;
2632 sg_init_one(sg
, buf
, buflen
);
2636 * ata_sg_init - Associate command with scatter-gather table.
2637 * @qc: Command to be associated
2638 * @sg: Scatter-gather table.
2639 * @n_elem: Number of elements in s/g table.
2641 * Initialize the data-related elements of queued_cmd @qc
2642 * to point to a scatter-gather table @sg, containing @n_elem
2646 * spin_lock_irqsave(host_set lock)
2649 void ata_sg_init(struct ata_queued_cmd
*qc
, struct scatterlist
*sg
,
2650 unsigned int n_elem
)
2652 qc
->flags
|= ATA_QCFLAG_SG
;
2654 qc
->n_elem
= n_elem
;
2655 qc
->orig_n_elem
= n_elem
;
2659 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2660 * @qc: Command with memory buffer to be mapped.
2662 * DMA-map the memory buffer associated with queued_cmd @qc.
2665 * spin_lock_irqsave(host_set lock)
2668 * Zero on success, negative on error.
2671 static int ata_sg_setup_one(struct ata_queued_cmd
*qc
)
2673 struct ata_port
*ap
= qc
->ap
;
2674 int dir
= qc
->dma_dir
;
2675 struct scatterlist
*sg
= qc
->__sg
;
2676 dma_addr_t dma_address
;
2678 /* we must lengthen transfers to end on a 32-bit boundary */
2679 qc
->pad_len
= sg
->length
& 3;
2681 void *pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2682 struct scatterlist
*psg
= &qc
->pad_sgent
;
2684 assert(qc
->dev
->class == ATA_DEV_ATAPI
);
2686 memset(pad_buf
, 0, ATA_DMA_PAD_SZ
);
2688 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
)
2689 memcpy(pad_buf
, qc
->buf_virt
+ sg
->length
- qc
->pad_len
,
2692 sg_dma_address(psg
) = ap
->pad_dma
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2693 sg_dma_len(psg
) = ATA_DMA_PAD_SZ
;
2695 sg
->length
-= qc
->pad_len
;
2697 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2698 sg
->length
, qc
->pad_len
);
2702 sg_dma_address(sg
) = 0;
2706 dma_address
= dma_map_single(ap
->host_set
->dev
, qc
->buf_virt
,
2708 if (dma_mapping_error(dma_address
)) {
2710 sg
->length
+= qc
->pad_len
;
2714 sg_dma_address(sg
) = dma_address
;
2716 sg_dma_len(sg
) = sg
->length
;
2718 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg
),
2719 qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2725 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2726 * @qc: Command with scatter-gather table to be mapped.
2728 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2731 * spin_lock_irqsave(host_set lock)
2734 * Zero on success, negative on error.
2738 static int ata_sg_setup(struct ata_queued_cmd
*qc
)
2740 struct ata_port
*ap
= qc
->ap
;
2741 struct scatterlist
*sg
= qc
->__sg
;
2742 struct scatterlist
*lsg
= &sg
[qc
->n_elem
- 1];
2743 int n_elem
, pre_n_elem
, dir
, trim_sg
= 0;
2745 VPRINTK("ENTER, ata%u\n", ap
->id
);
2746 assert(qc
->flags
& ATA_QCFLAG_SG
);
2748 /* we must lengthen transfers to end on a 32-bit boundary */
2749 qc
->pad_len
= lsg
->length
& 3;
2751 void *pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2752 struct scatterlist
*psg
= &qc
->pad_sgent
;
2753 unsigned int offset
;
2755 assert(qc
->dev
->class == ATA_DEV_ATAPI
);
2757 memset(pad_buf
, 0, ATA_DMA_PAD_SZ
);
2760 * psg->page/offset are used to copy to-be-written
2761 * data in this function or read data in ata_sg_clean.
2763 offset
= lsg
->offset
+ lsg
->length
- qc
->pad_len
;
2764 psg
->page
= nth_page(lsg
->page
, offset
>> PAGE_SHIFT
);
2765 psg
->offset
= offset_in_page(offset
);
2767 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
) {
2768 void *addr
= kmap_atomic(psg
->page
, KM_IRQ0
);
2769 memcpy(pad_buf
, addr
+ psg
->offset
, qc
->pad_len
);
2770 kunmap_atomic(addr
, KM_IRQ0
);
2773 sg_dma_address(psg
) = ap
->pad_dma
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2774 sg_dma_len(psg
) = ATA_DMA_PAD_SZ
;
2776 lsg
->length
-= qc
->pad_len
;
2777 if (lsg
->length
== 0)
2780 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2781 qc
->n_elem
- 1, lsg
->length
, qc
->pad_len
);
2784 pre_n_elem
= qc
->n_elem
;
2785 if (trim_sg
&& pre_n_elem
)
2794 n_elem
= dma_map_sg(ap
->host_set
->dev
, sg
, pre_n_elem
, dir
);
2796 /* restore last sg */
2797 lsg
->length
+= qc
->pad_len
;
2801 DPRINTK("%d sg elements mapped\n", n_elem
);
2804 qc
->n_elem
= n_elem
;
2810 * ata_poll_qc_complete - turn irq back on and finish qc
2811 * @qc: Command to complete
2812 * @err_mask: ATA status register content
2815 * None. (grabs host lock)
2818 void ata_poll_qc_complete(struct ata_queued_cmd
*qc
)
2820 struct ata_port
*ap
= qc
->ap
;
2821 unsigned long flags
;
2823 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2824 ap
->flags
&= ~ATA_FLAG_NOINTR
;
2826 ata_qc_complete(qc
);
2827 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2832 * @ap: the target ata_port
2835 * None. (executing in kernel thread context)
2838 * timeout value to use
2841 static unsigned long ata_pio_poll(struct ata_port
*ap
)
2843 struct ata_queued_cmd
*qc
;
2845 unsigned int poll_state
= HSM_ST_UNKNOWN
;
2846 unsigned int reg_state
= HSM_ST_UNKNOWN
;
2848 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2851 switch (ap
->hsm_task_state
) {
2854 poll_state
= HSM_ST_POLL
;
2858 case HSM_ST_LAST_POLL
:
2859 poll_state
= HSM_ST_LAST_POLL
;
2860 reg_state
= HSM_ST_LAST
;
2867 status
= ata_chk_status(ap
);
2868 if (status
& ATA_BUSY
) {
2869 if (time_after(jiffies
, ap
->pio_task_timeout
)) {
2870 qc
->err_mask
|= AC_ERR_ATA_BUS
;
2871 ap
->hsm_task_state
= HSM_ST_TMOUT
;
2874 ap
->hsm_task_state
= poll_state
;
2875 return ATA_SHORT_PAUSE
;
2878 ap
->hsm_task_state
= reg_state
;
2883 * ata_pio_complete - check if drive is busy or idle
2884 * @ap: the target ata_port
2887 * None. (executing in kernel thread context)
2890 * Non-zero if qc completed, zero otherwise.
2893 static int ata_pio_complete (struct ata_port
*ap
)
2895 struct ata_queued_cmd
*qc
;
2899 * This is purely heuristic. This is a fast path. Sometimes when
2900 * we enter, BSY will be cleared in a chk-status or two. If not,
2901 * the drive is probably seeking or something. Snooze for a couple
2902 * msecs, then chk-status again. If still busy, fall back to
2903 * HSM_ST_POLL state.
2905 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
, 10);
2906 if (drv_stat
& ATA_BUSY
) {
2908 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
, 10);
2909 if (drv_stat
& ATA_BUSY
) {
2910 ap
->hsm_task_state
= HSM_ST_LAST_POLL
;
2911 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
2916 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2919 drv_stat
= ata_wait_idle(ap
);
2920 if (!ata_ok(drv_stat
)) {
2921 qc
->err_mask
|= __ac_err_mask(drv_stat
);
2922 ap
->hsm_task_state
= HSM_ST_ERR
;
2926 ap
->hsm_task_state
= HSM_ST_IDLE
;
2928 assert(qc
->err_mask
== 0);
2929 ata_poll_qc_complete(qc
);
2931 /* another command may start at this point */
2938 * swap_buf_le16 - swap halves of 16-words in place
2939 * @buf: Buffer to swap
2940 * @buf_words: Number of 16-bit words in buffer.
2942 * Swap halves of 16-bit words if needed to convert from
2943 * little-endian byte order to native cpu byte order, or
2947 * Inherited from caller.
2949 void swap_buf_le16(u16
*buf
, unsigned int buf_words
)
2954 for (i
= 0; i
< buf_words
; i
++)
2955 buf
[i
] = le16_to_cpu(buf
[i
]);
2956 #endif /* __BIG_ENDIAN */
2960 * ata_mmio_data_xfer - Transfer data by MMIO
2961 * @ap: port to read/write
2963 * @buflen: buffer length
2964 * @write_data: read/write
2966 * Transfer data from/to the device data register by MMIO.
2969 * Inherited from caller.
2972 static void ata_mmio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2973 unsigned int buflen
, int write_data
)
2976 unsigned int words
= buflen
>> 1;
2977 u16
*buf16
= (u16
*) buf
;
2978 void __iomem
*mmio
= (void __iomem
*)ap
->ioaddr
.data_addr
;
2980 /* Transfer multiple of 2 bytes */
2982 for (i
= 0; i
< words
; i
++)
2983 writew(le16_to_cpu(buf16
[i
]), mmio
);
2985 for (i
= 0; i
< words
; i
++)
2986 buf16
[i
] = cpu_to_le16(readw(mmio
));
2989 /* Transfer trailing 1 byte, if any. */
2990 if (unlikely(buflen
& 0x01)) {
2991 u16 align_buf
[1] = { 0 };
2992 unsigned char *trailing_buf
= buf
+ buflen
- 1;
2995 memcpy(align_buf
, trailing_buf
, 1);
2996 writew(le16_to_cpu(align_buf
[0]), mmio
);
2998 align_buf
[0] = cpu_to_le16(readw(mmio
));
2999 memcpy(trailing_buf
, align_buf
, 1);
3005 * ata_pio_data_xfer - Transfer data by PIO
3006 * @ap: port to read/write
3008 * @buflen: buffer length
3009 * @write_data: read/write
3011 * Transfer data from/to the device data register by PIO.
3014 * Inherited from caller.
3017 static void ata_pio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3018 unsigned int buflen
, int write_data
)
3020 unsigned int words
= buflen
>> 1;
3022 /* Transfer multiple of 2 bytes */
3024 outsw(ap
->ioaddr
.data_addr
, buf
, words
);
3026 insw(ap
->ioaddr
.data_addr
, buf
, words
);
3028 /* Transfer trailing 1 byte, if any. */
3029 if (unlikely(buflen
& 0x01)) {
3030 u16 align_buf
[1] = { 0 };
3031 unsigned char *trailing_buf
= buf
+ buflen
- 1;
3034 memcpy(align_buf
, trailing_buf
, 1);
3035 outw(le16_to_cpu(align_buf
[0]), ap
->ioaddr
.data_addr
);
3037 align_buf
[0] = cpu_to_le16(inw(ap
->ioaddr
.data_addr
));
3038 memcpy(trailing_buf
, align_buf
, 1);
3044 * ata_data_xfer - Transfer data from/to the data register.
3045 * @ap: port to read/write
3047 * @buflen: buffer length
3048 * @do_write: read/write
3050 * Transfer data from/to the device data register.
3053 * Inherited from caller.
3056 static void ata_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3057 unsigned int buflen
, int do_write
)
3059 if (ap
->flags
& ATA_FLAG_MMIO
)
3060 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
3062 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
3066 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3067 * @qc: Command on going
3069 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3072 * Inherited from caller.
3075 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
3077 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3078 struct scatterlist
*sg
= qc
->__sg
;
3079 struct ata_port
*ap
= qc
->ap
;
3081 unsigned int offset
;
3084 if (qc
->cursect
== (qc
->nsect
- 1))
3085 ap
->hsm_task_state
= HSM_ST_LAST
;
3087 page
= sg
[qc
->cursg
].page
;
3088 offset
= sg
[qc
->cursg
].offset
+ qc
->cursg_ofs
* ATA_SECT_SIZE
;
3090 /* get the current page and offset */
3091 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
3092 offset
%= PAGE_SIZE
;
3094 buf
= kmap(page
) + offset
;
3099 if ((qc
->cursg_ofs
* ATA_SECT_SIZE
) == (&sg
[qc
->cursg
])->length
) {
3104 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
3106 /* do the actual data transfer */
3107 do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3108 ata_data_xfer(ap
, buf
, ATA_SECT_SIZE
, do_write
);
3114 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3115 * @qc: Command on going
3116 * @bytes: number of bytes
3118 * Transfer Transfer data from/to the ATAPI device.
3121 * Inherited from caller.
3125 static void __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
3127 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3128 struct scatterlist
*sg
= qc
->__sg
;
3129 struct ata_port
*ap
= qc
->ap
;
3132 unsigned int offset
, count
;
3134 if (qc
->curbytes
+ bytes
>= qc
->nbytes
)
3135 ap
->hsm_task_state
= HSM_ST_LAST
;
3138 if (unlikely(qc
->cursg
>= qc
->n_elem
)) {
3140 * The end of qc->sg is reached and the device expects
3141 * more data to transfer. In order not to overrun qc->sg
3142 * and fulfill length specified in the byte count register,
3143 * - for read case, discard trailing data from the device
3144 * - for write case, padding zero data to the device
3146 u16 pad_buf
[1] = { 0 };
3147 unsigned int words
= bytes
>> 1;
3150 if (words
) /* warning if bytes > 1 */
3151 printk(KERN_WARNING
"ata%u: %u bytes trailing data\n",
3154 for (i
= 0; i
< words
; i
++)
3155 ata_data_xfer(ap
, (unsigned char*)pad_buf
, 2, do_write
);
3157 ap
->hsm_task_state
= HSM_ST_LAST
;
3161 sg
= &qc
->__sg
[qc
->cursg
];
3164 offset
= sg
->offset
+ qc
->cursg_ofs
;
3166 /* get the current page and offset */
3167 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
3168 offset
%= PAGE_SIZE
;
3170 /* don't overrun current sg */
3171 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
3173 /* don't cross page boundaries */
3174 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
3176 buf
= kmap(page
) + offset
;
3179 qc
->curbytes
+= count
;
3180 qc
->cursg_ofs
+= count
;
3182 if (qc
->cursg_ofs
== sg
->length
) {
3187 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
3189 /* do the actual data transfer */
3190 ata_data_xfer(ap
, buf
, count
, do_write
);
3199 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3200 * @qc: Command on going
3202 * Transfer Transfer data from/to the ATAPI device.
3205 * Inherited from caller.
3208 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
3210 struct ata_port
*ap
= qc
->ap
;
3211 struct ata_device
*dev
= qc
->dev
;
3212 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
3213 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
3215 ap
->ops
->tf_read(ap
, &qc
->tf
);
3216 ireason
= qc
->tf
.nsect
;
3217 bc_lo
= qc
->tf
.lbam
;
3218 bc_hi
= qc
->tf
.lbah
;
3219 bytes
= (bc_hi
<< 8) | bc_lo
;
3221 /* shall be cleared to zero, indicating xfer of data */
3222 if (ireason
& (1 << 0))
3225 /* make sure transfer direction matches expected */
3226 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
3227 if (do_write
!= i_write
)
3230 __atapi_pio_bytes(qc
, bytes
);
3235 printk(KERN_INFO
"ata%u: dev %u: ATAPI check failed\n",
3236 ap
->id
, dev
->devno
);
3237 qc
->err_mask
|= AC_ERR_ATA_BUS
;
3238 ap
->hsm_task_state
= HSM_ST_ERR
;
3242 * ata_pio_block - start PIO on a block
3243 * @ap: the target ata_port
3246 * None. (executing in kernel thread context)
3249 static void ata_pio_block(struct ata_port
*ap
)
3251 struct ata_queued_cmd
*qc
;
3255 * This is purely heuristic. This is a fast path.
3256 * Sometimes when we enter, BSY will be cleared in
3257 * a chk-status or two. If not, the drive is probably seeking
3258 * or something. Snooze for a couple msecs, then
3259 * chk-status again. If still busy, fall back to
3260 * HSM_ST_POLL state.
3262 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
3263 if (status
& ATA_BUSY
) {
3265 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
3266 if (status
& ATA_BUSY
) {
3267 ap
->hsm_task_state
= HSM_ST_POLL
;
3268 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
3273 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3277 if (status
& (ATA_ERR
| ATA_DF
)) {
3278 qc
->err_mask
|= AC_ERR_DEV
;
3279 ap
->hsm_task_state
= HSM_ST_ERR
;
3283 /* transfer data if any */
3284 if (is_atapi_taskfile(&qc
->tf
)) {
3285 /* DRQ=0 means no more data to transfer */
3286 if ((status
& ATA_DRQ
) == 0) {
3287 ap
->hsm_task_state
= HSM_ST_LAST
;
3291 atapi_pio_bytes(qc
);
3293 /* handle BSY=0, DRQ=0 as error */
3294 if ((status
& ATA_DRQ
) == 0) {
3295 qc
->err_mask
|= AC_ERR_ATA_BUS
;
3296 ap
->hsm_task_state
= HSM_ST_ERR
;
3304 static void ata_pio_error(struct ata_port
*ap
)
3306 struct ata_queued_cmd
*qc
;
3308 printk(KERN_WARNING
"ata%u: PIO error\n", ap
->id
);
3310 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3313 /* make sure qc->err_mask is available to
3314 * know what's wrong and recover
3316 assert(qc
->err_mask
);
3318 ap
->hsm_task_state
= HSM_ST_IDLE
;
3320 ata_poll_qc_complete(qc
);
3323 static void ata_pio_task(void *_data
)
3325 struct ata_port
*ap
= _data
;
3326 unsigned long timeout
;
3333 switch (ap
->hsm_task_state
) {
3342 qc_completed
= ata_pio_complete(ap
);
3346 case HSM_ST_LAST_POLL
:
3347 timeout
= ata_pio_poll(ap
);
3357 queue_delayed_work(ata_wq
, &ap
->pio_task
, timeout
);
3358 else if (!qc_completed
)
3363 * ata_qc_timeout - Handle timeout of queued command
3364 * @qc: Command that timed out
3366 * Some part of the kernel (currently, only the SCSI layer)
3367 * has noticed that the active command on port @ap has not
3368 * completed after a specified length of time. Handle this
3369 * condition by disabling DMA (if necessary) and completing
3370 * transactions, with error if necessary.
3372 * This also handles the case of the "lost interrupt", where
3373 * for some reason (possibly hardware bug, possibly driver bug)
3374 * an interrupt was not delivered to the driver, even though the
3375 * transaction completed successfully.
3378 * Inherited from SCSI layer (none, can sleep)
3381 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
3383 struct ata_port
*ap
= qc
->ap
;
3384 struct ata_host_set
*host_set
= ap
->host_set
;
3385 u8 host_stat
= 0, drv_stat
;
3386 unsigned long flags
;
3390 spin_lock_irqsave(&host_set
->lock
, flags
);
3392 /* hack alert! We cannot use the supplied completion
3393 * function from inside the ->eh_strategy_handler() thread.
3394 * libata is the only user of ->eh_strategy_handler() in
3395 * any kernel, so the default scsi_done() assumes it is
3396 * not being called from the SCSI EH.
3398 qc
->scsidone
= scsi_finish_command
;
3400 switch (qc
->tf
.protocol
) {
3403 case ATA_PROT_ATAPI_DMA
:
3404 host_stat
= ap
->ops
->bmdma_status(ap
);
3406 /* before we do anything else, clear DMA-Start bit */
3407 ap
->ops
->bmdma_stop(qc
);
3413 drv_stat
= ata_chk_status(ap
);
3415 /* ack bmdma irq events */
3416 ap
->ops
->irq_clear(ap
);
3418 printk(KERN_ERR
"ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3419 ap
->id
, qc
->tf
.command
, drv_stat
, host_stat
);
3421 /* complete taskfile transaction */
3422 qc
->err_mask
|= ac_err_mask(drv_stat
);
3423 ata_qc_complete(qc
);
3427 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3433 * ata_eng_timeout - Handle timeout of queued command
3434 * @ap: Port on which timed-out command is active
3436 * Some part of the kernel (currently, only the SCSI layer)
3437 * has noticed that the active command on port @ap has not
3438 * completed after a specified length of time. Handle this
3439 * condition by disabling DMA (if necessary) and completing
3440 * transactions, with error if necessary.
3442 * This also handles the case of the "lost interrupt", where
3443 * for some reason (possibly hardware bug, possibly driver bug)
3444 * an interrupt was not delivered to the driver, even though the
3445 * transaction completed successfully.
3448 * Inherited from SCSI layer (none, can sleep)
3451 void ata_eng_timeout(struct ata_port
*ap
)
3453 struct ata_queued_cmd
*qc
;
3457 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3461 printk(KERN_ERR
"ata%u: BUG: timeout without command\n",
3471 * ata_qc_new - Request an available ATA command, for queueing
3472 * @ap: Port associated with device @dev
3473 * @dev: Device from whom we request an available command structure
3479 static struct ata_queued_cmd
*ata_qc_new(struct ata_port
*ap
)
3481 struct ata_queued_cmd
*qc
= NULL
;
3484 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++)
3485 if (!test_and_set_bit(i
, &ap
->qactive
)) {
3486 qc
= ata_qc_from_tag(ap
, i
);
3497 * ata_qc_new_init - Request an available ATA command, and initialize it
3498 * @ap: Port associated with device @dev
3499 * @dev: Device from whom we request an available command structure
3505 struct ata_queued_cmd
*ata_qc_new_init(struct ata_port
*ap
,
3506 struct ata_device
*dev
)
3508 struct ata_queued_cmd
*qc
;
3510 qc
= ata_qc_new(ap
);
3522 static void __ata_qc_complete(struct ata_queued_cmd
*qc
)
3524 struct ata_port
*ap
= qc
->ap
;
3529 if (likely(ata_tag_valid(tag
))) {
3530 if (tag
== ap
->active_tag
)
3531 ap
->active_tag
= ATA_TAG_POISON
;
3532 qc
->tag
= ATA_TAG_POISON
;
3533 clear_bit(tag
, &ap
->qactive
);
3538 * ata_qc_free - free unused ata_queued_cmd
3539 * @qc: Command to complete
3541 * Designed to free unused ata_queued_cmd object
3542 * in case something prevents using it.
3545 * spin_lock_irqsave(host_set lock)
3547 void ata_qc_free(struct ata_queued_cmd
*qc
)
3549 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3551 __ata_qc_complete(qc
);
3555 * ata_qc_complete - Complete an active ATA command
3556 * @qc: Command to complete
3557 * @err_mask: ATA Status register contents
3559 * Indicate to the mid and upper layers that an ATA
3560 * command has completed, with either an ok or not-ok status.
3563 * spin_lock_irqsave(host_set lock)
3566 void ata_qc_complete(struct ata_queued_cmd
*qc
)
3570 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3571 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
3573 if (likely(qc
->flags
& ATA_QCFLAG_DMAMAP
))
3576 /* atapi: mark qc as inactive to prevent the interrupt handler
3577 * from completing the command twice later, before the error handler
3578 * is called. (when rc != 0 and atapi request sense is needed)
3580 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
3582 /* call completion callback */
3583 rc
= qc
->complete_fn(qc
);
3585 /* if callback indicates not to complete command (non-zero),
3586 * return immediately
3591 __ata_qc_complete(qc
);
3596 static inline int ata_should_dma_map(struct ata_queued_cmd
*qc
)
3598 struct ata_port
*ap
= qc
->ap
;
3600 switch (qc
->tf
.protocol
) {
3602 case ATA_PROT_ATAPI_DMA
:
3605 case ATA_PROT_ATAPI
:
3607 case ATA_PROT_PIO_MULT
:
3608 if (ap
->flags
& ATA_FLAG_PIO_DMA
)
3621 * ata_qc_issue - issue taskfile to device
3622 * @qc: command to issue to device
3624 * Prepare an ATA command to submission to device.
3625 * This includes mapping the data into a DMA-able
3626 * area, filling in the S/G table, and finally
3627 * writing the taskfile to hardware, starting the command.
3630 * spin_lock_irqsave(host_set lock)
3633 * Zero on success, negative on error.
3636 int ata_qc_issue(struct ata_queued_cmd
*qc
)
3638 struct ata_port
*ap
= qc
->ap
;
3640 if (ata_should_dma_map(qc
)) {
3641 if (qc
->flags
& ATA_QCFLAG_SG
) {
3642 if (ata_sg_setup(qc
))
3644 } else if (qc
->flags
& ATA_QCFLAG_SINGLE
) {
3645 if (ata_sg_setup_one(qc
))
3649 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
3652 ap
->ops
->qc_prep(qc
);
3654 qc
->ap
->active_tag
= qc
->tag
;
3655 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
3657 return ap
->ops
->qc_issue(qc
);
3665 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3666 * @qc: command to issue to device
3668 * Using various libata functions and hooks, this function
3669 * starts an ATA command. ATA commands are grouped into
3670 * classes called "protocols", and issuing each type of protocol
3671 * is slightly different.
3673 * May be used as the qc_issue() entry in ata_port_operations.
3676 * spin_lock_irqsave(host_set lock)
3679 * Zero on success, negative on error.
3682 int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
3684 struct ata_port
*ap
= qc
->ap
;
3686 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
3688 switch (qc
->tf
.protocol
) {
3689 case ATA_PROT_NODATA
:
3690 ata_tf_to_host(ap
, &qc
->tf
);
3694 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3695 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3696 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3699 case ATA_PROT_PIO
: /* load tf registers, initiate polling pio */
3700 ata_qc_set_polling(qc
);
3701 ata_tf_to_host(ap
, &qc
->tf
);
3702 ap
->hsm_task_state
= HSM_ST
;
3703 queue_work(ata_wq
, &ap
->pio_task
);
3706 case ATA_PROT_ATAPI
:
3707 ata_qc_set_polling(qc
);
3708 ata_tf_to_host(ap
, &qc
->tf
);
3709 queue_work(ata_wq
, &ap
->packet_task
);
3712 case ATA_PROT_ATAPI_NODATA
:
3713 ap
->flags
|= ATA_FLAG_NOINTR
;
3714 ata_tf_to_host(ap
, &qc
->tf
);
3715 queue_work(ata_wq
, &ap
->packet_task
);
3718 case ATA_PROT_ATAPI_DMA
:
3719 ap
->flags
|= ATA_FLAG_NOINTR
;
3720 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3721 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3722 queue_work(ata_wq
, &ap
->packet_task
);
3734 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3735 * @qc: Info associated with this ATA transaction.
3738 * spin_lock_irqsave(host_set lock)
3741 static void ata_bmdma_setup_mmio (struct ata_queued_cmd
*qc
)
3743 struct ata_port
*ap
= qc
->ap
;
3744 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3746 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3748 /* load PRD table addr. */
3749 mb(); /* make sure PRD table writes are visible to controller */
3750 writel(ap
->prd_dma
, mmio
+ ATA_DMA_TABLE_OFS
);
3752 /* specify data direction, triple-check start bit is clear */
3753 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3754 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3756 dmactl
|= ATA_DMA_WR
;
3757 writeb(dmactl
, mmio
+ ATA_DMA_CMD
);
3759 /* issue r/w command */
3760 ap
->ops
->exec_command(ap
, &qc
->tf
);
3764 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3765 * @qc: Info associated with this ATA transaction.
3768 * spin_lock_irqsave(host_set lock)
3771 static void ata_bmdma_start_mmio (struct ata_queued_cmd
*qc
)
3773 struct ata_port
*ap
= qc
->ap
;
3774 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3777 /* start host DMA transaction */
3778 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3779 writeb(dmactl
| ATA_DMA_START
, mmio
+ ATA_DMA_CMD
);
3781 /* Strictly, one may wish to issue a readb() here, to
3782 * flush the mmio write. However, control also passes
3783 * to the hardware at this point, and it will interrupt
3784 * us when we are to resume control. So, in effect,
3785 * we don't care when the mmio write flushes.
3786 * Further, a read of the DMA status register _immediately_
3787 * following the write may not be what certain flaky hardware
3788 * is expected, so I think it is best to not add a readb()
3789 * without first all the MMIO ATA cards/mobos.
3790 * Or maybe I'm just being paranoid.
3795 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3796 * @qc: Info associated with this ATA transaction.
3799 * spin_lock_irqsave(host_set lock)
3802 static void ata_bmdma_setup_pio (struct ata_queued_cmd
*qc
)
3804 struct ata_port
*ap
= qc
->ap
;
3805 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3808 /* load PRD table addr. */
3809 outl(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
3811 /* specify data direction, triple-check start bit is clear */
3812 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3813 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3815 dmactl
|= ATA_DMA_WR
;
3816 outb(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3818 /* issue r/w command */
3819 ap
->ops
->exec_command(ap
, &qc
->tf
);
3823 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3824 * @qc: Info associated with this ATA transaction.
3827 * spin_lock_irqsave(host_set lock)
3830 static void ata_bmdma_start_pio (struct ata_queued_cmd
*qc
)
3832 struct ata_port
*ap
= qc
->ap
;
3835 /* start host DMA transaction */
3836 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3837 outb(dmactl
| ATA_DMA_START
,
3838 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3843 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3844 * @qc: Info associated with this ATA transaction.
3846 * Writes the ATA_DMA_START flag to the DMA command register.
3848 * May be used as the bmdma_start() entry in ata_port_operations.
3851 * spin_lock_irqsave(host_set lock)
3853 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
3855 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3856 ata_bmdma_start_mmio(qc
);
3858 ata_bmdma_start_pio(qc
);
3863 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3864 * @qc: Info associated with this ATA transaction.
3866 * Writes address of PRD table to device's PRD Table Address
3867 * register, sets the DMA control register, and calls
3868 * ops->exec_command() to start the transfer.
3870 * May be used as the bmdma_setup() entry in ata_port_operations.
3873 * spin_lock_irqsave(host_set lock)
3875 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
3877 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3878 ata_bmdma_setup_mmio(qc
);
3880 ata_bmdma_setup_pio(qc
);
3885 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3886 * @ap: Port associated with this ATA transaction.
3888 * Clear interrupt and error flags in DMA status register.
3890 * May be used as the irq_clear() entry in ata_port_operations.
3893 * spin_lock_irqsave(host_set lock)
3896 void ata_bmdma_irq_clear(struct ata_port
*ap
)
3898 if (ap
->flags
& ATA_FLAG_MMIO
) {
3899 void __iomem
*mmio
= ((void __iomem
*) ap
->ioaddr
.bmdma_addr
) + ATA_DMA_STATUS
;
3900 writeb(readb(mmio
), mmio
);
3902 unsigned long addr
= ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
;
3903 outb(inb(addr
), addr
);
3910 * ata_bmdma_status - Read PCI IDE BMDMA status
3911 * @ap: Port associated with this ATA transaction.
3913 * Read and return BMDMA status register.
3915 * May be used as the bmdma_status() entry in ata_port_operations.
3918 * spin_lock_irqsave(host_set lock)
3921 u8
ata_bmdma_status(struct ata_port
*ap
)
3924 if (ap
->flags
& ATA_FLAG_MMIO
) {
3925 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3926 host_stat
= readb(mmio
+ ATA_DMA_STATUS
);
3928 host_stat
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
3934 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3935 * @qc: Command we are ending DMA for
3937 * Clears the ATA_DMA_START flag in the dma control register
3939 * May be used as the bmdma_stop() entry in ata_port_operations.
3942 * spin_lock_irqsave(host_set lock)
3945 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
3947 struct ata_port
*ap
= qc
->ap
;
3948 if (ap
->flags
& ATA_FLAG_MMIO
) {
3949 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3951 /* clear start/stop bit */
3952 writeb(readb(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
3953 mmio
+ ATA_DMA_CMD
);
3955 /* clear start/stop bit */
3956 outb(inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
3957 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3960 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3961 ata_altstatus(ap
); /* dummy read */
3965 * ata_host_intr - Handle host interrupt for given (port, task)
3966 * @ap: Port on which interrupt arrived (possibly...)
3967 * @qc: Taskfile currently active in engine
3969 * Handle host interrupt for given queued command. Currently,
3970 * only DMA interrupts are handled. All other commands are
3971 * handled via polling with interrupts disabled (nIEN bit).
3974 * spin_lock_irqsave(host_set lock)
3977 * One if interrupt was handled, zero if not (shared irq).
3980 inline unsigned int ata_host_intr (struct ata_port
*ap
,
3981 struct ata_queued_cmd
*qc
)
3983 u8 status
, host_stat
;
3985 switch (qc
->tf
.protocol
) {
3988 case ATA_PROT_ATAPI_DMA
:
3989 case ATA_PROT_ATAPI
:
3990 /* check status of DMA engine */
3991 host_stat
= ap
->ops
->bmdma_status(ap
);
3992 VPRINTK("ata%u: host_stat 0x%X\n", ap
->id
, host_stat
);
3994 /* if it's not our irq... */
3995 if (!(host_stat
& ATA_DMA_INTR
))
3998 /* before we do anything else, clear DMA-Start bit */
3999 ap
->ops
->bmdma_stop(qc
);
4003 case ATA_PROT_ATAPI_NODATA
:
4004 case ATA_PROT_NODATA
:
4005 /* check altstatus */
4006 status
= ata_altstatus(ap
);
4007 if (status
& ATA_BUSY
)
4010 /* check main status, clearing INTRQ */
4011 status
= ata_chk_status(ap
);
4012 if (unlikely(status
& ATA_BUSY
))
4014 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4015 ap
->id
, qc
->tf
.protocol
, status
);
4017 /* ack bmdma irq events */
4018 ap
->ops
->irq_clear(ap
);
4020 /* complete taskfile transaction */
4021 qc
->err_mask
|= ac_err_mask(status
);
4022 ata_qc_complete(qc
);
4029 return 1; /* irq handled */
4032 ap
->stats
.idle_irq
++;
4035 if ((ap
->stats
.idle_irq
% 1000) == 0) {
4037 ata_irq_ack(ap
, 0); /* debug trap */
4038 printk(KERN_WARNING
"ata%d: irq trap\n", ap
->id
);
4041 return 0; /* irq not handled */
4045 * ata_interrupt - Default ATA host interrupt handler
4046 * @irq: irq line (unused)
4047 * @dev_instance: pointer to our ata_host_set information structure
4050 * Default interrupt handler for PCI IDE devices. Calls
4051 * ata_host_intr() for each port that is not disabled.
4054 * Obtains host_set lock during operation.
4057 * IRQ_NONE or IRQ_HANDLED.
4060 irqreturn_t
ata_interrupt (int irq
, void *dev_instance
, struct pt_regs
*regs
)
4062 struct ata_host_set
*host_set
= dev_instance
;
4064 unsigned int handled
= 0;
4065 unsigned long flags
;
4067 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4068 spin_lock_irqsave(&host_set
->lock
, flags
);
4070 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4071 struct ata_port
*ap
;
4073 ap
= host_set
->ports
[i
];
4075 !(ap
->flags
& (ATA_FLAG_PORT_DISABLED
| ATA_FLAG_NOINTR
))) {
4076 struct ata_queued_cmd
*qc
;
4078 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
4079 if (qc
&& (!(qc
->tf
.ctl
& ATA_NIEN
)) &&
4080 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
4081 handled
|= ata_host_intr(ap
, qc
);
4085 spin_unlock_irqrestore(&host_set
->lock
, flags
);
4087 return IRQ_RETVAL(handled
);
4091 * atapi_packet_task - Write CDB bytes to hardware
4092 * @_data: Port to which ATAPI device is attached.
4094 * When device has indicated its readiness to accept
4095 * a CDB, this function is called. Send the CDB.
4096 * If DMA is to be performed, exit immediately.
4097 * Otherwise, we are in polling mode, so poll
4098 * status under operation succeeds or fails.
4101 * Kernel thread context (may sleep)
4104 static void atapi_packet_task(void *_data
)
4106 struct ata_port
*ap
= _data
;
4107 struct ata_queued_cmd
*qc
;
4110 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
4112 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
4114 /* sleep-wait for BSY to clear */
4115 DPRINTK("busy wait\n");
4116 if (ata_busy_sleep(ap
, ATA_TMOUT_CDB_QUICK
, ATA_TMOUT_CDB
)) {
4117 qc
->err_mask
|= AC_ERR_ATA_BUS
;
4121 /* make sure DRQ is set */
4122 status
= ata_chk_status(ap
);
4123 if ((status
& (ATA_BUSY
| ATA_DRQ
)) != ATA_DRQ
) {
4124 qc
->err_mask
|= AC_ERR_ATA_BUS
;
4129 DPRINTK("send cdb\n");
4130 assert(ap
->cdb_len
>= 12);
4132 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
||
4133 qc
->tf
.protocol
== ATA_PROT_ATAPI_NODATA
) {
4134 unsigned long flags
;
4136 /* Once we're done issuing command and kicking bmdma,
4137 * irq handler takes over. To not lose irq, we need
4138 * to clear NOINTR flag before sending cdb, but
4139 * interrupt handler shouldn't be invoked before we're
4140 * finished. Hence, the following locking.
4142 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
4143 ap
->flags
&= ~ATA_FLAG_NOINTR
;
4144 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
4145 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
)
4146 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
4147 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
4149 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
4151 /* PIO commands are handled by polling */
4152 ap
->hsm_task_state
= HSM_ST
;
4153 queue_work(ata_wq
, &ap
->pio_task
);
4159 ata_poll_qc_complete(qc
);
4164 * ata_port_start - Set port up for dma.
4165 * @ap: Port to initialize
4167 * Called just after data structures for each port are
4168 * initialized. Allocates space for PRD table.
4170 * May be used as the port_start() entry in ata_port_operations.
4173 * Inherited from caller.
4177 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4178 * without filling any other registers
4180 static int ata_do_simple_cmd(struct ata_port
*ap
, struct ata_device
*dev
,
4183 struct ata_taskfile tf
;
4186 ata_tf_init(ap
, &tf
, dev
->devno
);
4189 tf
.flags
|= ATA_TFLAG_DEVICE
;
4190 tf
.protocol
= ATA_PROT_NODATA
;
4192 err
= ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0);
4194 printk(KERN_ERR
"%s: ata command failed: %d\n",
4200 static int ata_flush_cache(struct ata_port
*ap
, struct ata_device
*dev
)
4204 if (!ata_try_flush_cache(dev
))
4207 if (ata_id_has_flush_ext(dev
->id
))
4208 cmd
= ATA_CMD_FLUSH_EXT
;
4210 cmd
= ATA_CMD_FLUSH
;
4212 return ata_do_simple_cmd(ap
, dev
, cmd
);
4215 static int ata_standby_drive(struct ata_port
*ap
, struct ata_device
*dev
)
4217 return ata_do_simple_cmd(ap
, dev
, ATA_CMD_STANDBYNOW1
);
4220 static int ata_start_drive(struct ata_port
*ap
, struct ata_device
*dev
)
4222 return ata_do_simple_cmd(ap
, dev
, ATA_CMD_IDLEIMMEDIATE
);
4226 * ata_device_resume - wakeup a previously suspended devices
4228 * Kick the drive back into action, by sending it an idle immediate
4229 * command and making sure its transfer mode matches between drive
4233 int ata_device_resume(struct ata_port
*ap
, struct ata_device
*dev
)
4235 if (ap
->flags
& ATA_FLAG_SUSPENDED
) {
4236 ap
->flags
&= ~ATA_FLAG_SUSPENDED
;
4239 if (!ata_dev_present(dev
))
4241 if (dev
->class == ATA_DEV_ATA
)
4242 ata_start_drive(ap
, dev
);
4248 * ata_device_suspend - prepare a device for suspend
4250 * Flush the cache on the drive, if appropriate, then issue a
4251 * standbynow command.
4254 int ata_device_suspend(struct ata_port
*ap
, struct ata_device
*dev
)
4256 if (!ata_dev_present(dev
))
4258 if (dev
->class == ATA_DEV_ATA
)
4259 ata_flush_cache(ap
, dev
);
4261 ata_standby_drive(ap
, dev
);
4262 ap
->flags
|= ATA_FLAG_SUSPENDED
;
4266 int ata_port_start (struct ata_port
*ap
)
4268 struct device
*dev
= ap
->host_set
->dev
;
4271 ap
->prd
= dma_alloc_coherent(dev
, ATA_PRD_TBL_SZ
, &ap
->prd_dma
, GFP_KERNEL
);
4275 rc
= ata_pad_alloc(ap
, dev
);
4277 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
4281 DPRINTK("prd alloc, virt %p, dma %llx\n", ap
->prd
, (unsigned long long) ap
->prd_dma
);
4288 * ata_port_stop - Undo ata_port_start()
4289 * @ap: Port to shut down
4291 * Frees the PRD table.
4293 * May be used as the port_stop() entry in ata_port_operations.
4296 * Inherited from caller.
4299 void ata_port_stop (struct ata_port
*ap
)
4301 struct device
*dev
= ap
->host_set
->dev
;
4303 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
4304 ata_pad_free(ap
, dev
);
4307 void ata_host_stop (struct ata_host_set
*host_set
)
4309 if (host_set
->mmio_base
)
4310 iounmap(host_set
->mmio_base
);
4315 * ata_host_remove - Unregister SCSI host structure with upper layers
4316 * @ap: Port to unregister
4317 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4320 * Inherited from caller.
4323 static void ata_host_remove(struct ata_port
*ap
, unsigned int do_unregister
)
4325 struct Scsi_Host
*sh
= ap
->host
;
4330 scsi_remove_host(sh
);
4332 ap
->ops
->port_stop(ap
);
4336 * ata_host_init - Initialize an ata_port structure
4337 * @ap: Structure to initialize
4338 * @host: associated SCSI mid-layer structure
4339 * @host_set: Collection of hosts to which @ap belongs
4340 * @ent: Probe information provided by low-level driver
4341 * @port_no: Port number associated with this ata_port
4343 * Initialize a new ata_port structure, and its associated
4347 * Inherited from caller.
4350 static void ata_host_init(struct ata_port
*ap
, struct Scsi_Host
*host
,
4351 struct ata_host_set
*host_set
,
4352 const struct ata_probe_ent
*ent
, unsigned int port_no
)
4358 host
->max_channel
= 1;
4359 host
->unique_id
= ata_unique_id
++;
4360 host
->max_cmd_len
= 12;
4362 ap
->flags
= ATA_FLAG_PORT_DISABLED
;
4363 ap
->id
= host
->unique_id
;
4365 ap
->ctl
= ATA_DEVCTL_OBS
;
4366 ap
->host_set
= host_set
;
4367 ap
->port_no
= port_no
;
4369 ent
->legacy_mode
? ent
->hard_port_no
: port_no
;
4370 ap
->pio_mask
= ent
->pio_mask
;
4371 ap
->mwdma_mask
= ent
->mwdma_mask
;
4372 ap
->udma_mask
= ent
->udma_mask
;
4373 ap
->flags
|= ent
->host_flags
;
4374 ap
->ops
= ent
->port_ops
;
4375 ap
->cbl
= ATA_CBL_NONE
;
4376 ap
->active_tag
= ATA_TAG_POISON
;
4377 ap
->last_ctl
= 0xFF;
4379 INIT_WORK(&ap
->packet_task
, atapi_packet_task
, ap
);
4380 INIT_WORK(&ap
->pio_task
, ata_pio_task
, ap
);
4382 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
4383 ap
->device
[i
].devno
= i
;
4386 ap
->stats
.unhandled_irq
= 1;
4387 ap
->stats
.idle_irq
= 1;
4390 memcpy(&ap
->ioaddr
, &ent
->port
[port_no
], sizeof(struct ata_ioports
));
4394 * ata_host_add - Attach low-level ATA driver to system
4395 * @ent: Information provided by low-level driver
4396 * @host_set: Collections of ports to which we add
4397 * @port_no: Port number associated with this host
4399 * Attach low-level ATA driver to system.
4402 * PCI/etc. bus probe sem.
4405 * New ata_port on success, for NULL on error.
4408 static struct ata_port
* ata_host_add(const struct ata_probe_ent
*ent
,
4409 struct ata_host_set
*host_set
,
4410 unsigned int port_no
)
4412 struct Scsi_Host
*host
;
4413 struct ata_port
*ap
;
4417 host
= scsi_host_alloc(ent
->sht
, sizeof(struct ata_port
));
4421 ap
= (struct ata_port
*) &host
->hostdata
[0];
4423 ata_host_init(ap
, host
, host_set
, ent
, port_no
);
4425 rc
= ap
->ops
->port_start(ap
);
4432 scsi_host_put(host
);
4437 * ata_device_add - Register hardware device with ATA and SCSI layers
4438 * @ent: Probe information describing hardware device to be registered
4440 * This function processes the information provided in the probe
4441 * information struct @ent, allocates the necessary ATA and SCSI
4442 * host information structures, initializes them, and registers
4443 * everything with requisite kernel subsystems.
4445 * This function requests irqs, probes the ATA bus, and probes
4449 * PCI/etc. bus probe sem.
4452 * Number of ports registered. Zero on error (no ports registered).
4455 int ata_device_add(const struct ata_probe_ent
*ent
)
4457 unsigned int count
= 0, i
;
4458 struct device
*dev
= ent
->dev
;
4459 struct ata_host_set
*host_set
;
4462 /* alloc a container for our list of ATA ports (buses) */
4463 host_set
= kzalloc(sizeof(struct ata_host_set
) +
4464 (ent
->n_ports
* sizeof(void *)), GFP_KERNEL
);
4467 spin_lock_init(&host_set
->lock
);
4469 host_set
->dev
= dev
;
4470 host_set
->n_ports
= ent
->n_ports
;
4471 host_set
->irq
= ent
->irq
;
4472 host_set
->mmio_base
= ent
->mmio_base
;
4473 host_set
->private_data
= ent
->private_data
;
4474 host_set
->ops
= ent
->port_ops
;
4476 /* register each port bound to this device */
4477 for (i
= 0; i
< ent
->n_ports
; i
++) {
4478 struct ata_port
*ap
;
4479 unsigned long xfer_mode_mask
;
4481 ap
= ata_host_add(ent
, host_set
, i
);
4485 host_set
->ports
[i
] = ap
;
4486 xfer_mode_mask
=(ap
->udma_mask
<< ATA_SHIFT_UDMA
) |
4487 (ap
->mwdma_mask
<< ATA_SHIFT_MWDMA
) |
4488 (ap
->pio_mask
<< ATA_SHIFT_PIO
);
4490 /* print per-port info to dmesg */
4491 printk(KERN_INFO
"ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4492 "bmdma 0x%lX irq %lu\n",
4494 ap
->flags
& ATA_FLAG_SATA
? 'S' : 'P',
4495 ata_mode_string(xfer_mode_mask
),
4496 ap
->ioaddr
.cmd_addr
,
4497 ap
->ioaddr
.ctl_addr
,
4498 ap
->ioaddr
.bmdma_addr
,
4502 host_set
->ops
->irq_clear(ap
);
4509 /* obtain irq, that is shared between channels */
4510 if (request_irq(ent
->irq
, ent
->port_ops
->irq_handler
, ent
->irq_flags
,
4511 DRV_NAME
, host_set
))
4514 /* perform each probe synchronously */
4515 DPRINTK("probe begin\n");
4516 for (i
= 0; i
< count
; i
++) {
4517 struct ata_port
*ap
;
4520 ap
= host_set
->ports
[i
];
4522 DPRINTK("ata%u: probe begin\n", ap
->id
);
4523 rc
= ata_bus_probe(ap
);
4524 DPRINTK("ata%u: probe end\n", ap
->id
);
4527 /* FIXME: do something useful here?
4528 * Current libata behavior will
4529 * tear down everything when
4530 * the module is removed
4531 * or the h/w is unplugged.
4535 rc
= scsi_add_host(ap
->host
, dev
);
4537 printk(KERN_ERR
"ata%u: scsi_add_host failed\n",
4539 /* FIXME: do something useful here */
4540 /* FIXME: handle unconditional calls to
4541 * scsi_scan_host and ata_host_remove, below,
4547 /* probes are done, now scan each port's disk(s) */
4548 DPRINTK("probe begin\n");
4549 for (i
= 0; i
< count
; i
++) {
4550 struct ata_port
*ap
= host_set
->ports
[i
];
4552 ata_scsi_scan_host(ap
);
4555 dev_set_drvdata(dev
, host_set
);
4557 VPRINTK("EXIT, returning %u\n", ent
->n_ports
);
4558 return ent
->n_ports
; /* success */
4561 for (i
= 0; i
< count
; i
++) {
4562 ata_host_remove(host_set
->ports
[i
], 1);
4563 scsi_host_put(host_set
->ports
[i
]->host
);
4567 VPRINTK("EXIT, returning 0\n");
4572 * ata_host_set_remove - PCI layer callback for device removal
4573 * @host_set: ATA host set that was removed
4575 * Unregister all objects associated with this host set. Free those
4579 * Inherited from calling layer (may sleep).
4582 void ata_host_set_remove(struct ata_host_set
*host_set
)
4584 struct ata_port
*ap
;
4587 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4588 ap
= host_set
->ports
[i
];
4589 scsi_remove_host(ap
->host
);
4592 free_irq(host_set
->irq
, host_set
);
4594 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4595 ap
= host_set
->ports
[i
];
4597 ata_scsi_release(ap
->host
);
4599 if ((ap
->flags
& ATA_FLAG_NO_LEGACY
) == 0) {
4600 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
4602 if (ioaddr
->cmd_addr
== 0x1f0)
4603 release_region(0x1f0, 8);
4604 else if (ioaddr
->cmd_addr
== 0x170)
4605 release_region(0x170, 8);
4608 scsi_host_put(ap
->host
);
4611 if (host_set
->ops
->host_stop
)
4612 host_set
->ops
->host_stop(host_set
);
4618 * ata_scsi_release - SCSI layer callback hook for host unload
4619 * @host: libata host to be unloaded
4621 * Performs all duties necessary to shut down a libata port...
4622 * Kill port kthread, disable port, and release resources.
4625 * Inherited from SCSI layer.
4631 int ata_scsi_release(struct Scsi_Host
*host
)
4633 struct ata_port
*ap
= (struct ata_port
*) &host
->hostdata
[0];
4637 ap
->ops
->port_disable(ap
);
4638 ata_host_remove(ap
, 0);
4645 * ata_std_ports - initialize ioaddr with standard port offsets.
4646 * @ioaddr: IO address structure to be initialized
4648 * Utility function which initializes data_addr, error_addr,
4649 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4650 * device_addr, status_addr, and command_addr to standard offsets
4651 * relative to cmd_addr.
4653 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4656 void ata_std_ports(struct ata_ioports
*ioaddr
)
4658 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
4659 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
4660 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
4661 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
4662 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
4663 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
4664 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
4665 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
4666 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
4667 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
4670 static struct ata_probe_ent
*
4671 ata_probe_ent_alloc(struct device
*dev
, const struct ata_port_info
*port
)
4673 struct ata_probe_ent
*probe_ent
;
4675 probe_ent
= kzalloc(sizeof(*probe_ent
), GFP_KERNEL
);
4677 printk(KERN_ERR DRV_NAME
"(%s): out of memory\n",
4678 kobject_name(&(dev
->kobj
)));
4682 INIT_LIST_HEAD(&probe_ent
->node
);
4683 probe_ent
->dev
= dev
;
4685 probe_ent
->sht
= port
->sht
;
4686 probe_ent
->host_flags
= port
->host_flags
;
4687 probe_ent
->pio_mask
= port
->pio_mask
;
4688 probe_ent
->mwdma_mask
= port
->mwdma_mask
;
4689 probe_ent
->udma_mask
= port
->udma_mask
;
4690 probe_ent
->port_ops
= port
->port_ops
;
4699 void ata_pci_host_stop (struct ata_host_set
*host_set
)
4701 struct pci_dev
*pdev
= to_pci_dev(host_set
->dev
);
4703 pci_iounmap(pdev
, host_set
->mmio_base
);
4707 * ata_pci_init_native_mode - Initialize native-mode driver
4708 * @pdev: pci device to be initialized
4709 * @port: array[2] of pointers to port info structures.
4710 * @ports: bitmap of ports present
4712 * Utility function which allocates and initializes an
4713 * ata_probe_ent structure for a standard dual-port
4714 * PIO-based IDE controller. The returned ata_probe_ent
4715 * structure can be passed to ata_device_add(). The returned
4716 * ata_probe_ent structure should then be freed with kfree().
4718 * The caller need only pass the address of the primary port, the
4719 * secondary will be deduced automatically. If the device has non
4720 * standard secondary port mappings this function can be called twice,
4721 * once for each interface.
4724 struct ata_probe_ent
*
4725 ata_pci_init_native_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
, int ports
)
4727 struct ata_probe_ent
*probe_ent
=
4728 ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
4734 probe_ent
->irq
= pdev
->irq
;
4735 probe_ent
->irq_flags
= SA_SHIRQ
;
4736 probe_ent
->private_data
= port
[0]->private_data
;
4738 if (ports
& ATA_PORT_PRIMARY
) {
4739 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 0);
4740 probe_ent
->port
[p
].altstatus_addr
=
4741 probe_ent
->port
[p
].ctl_addr
=
4742 pci_resource_start(pdev
, 1) | ATA_PCI_CTL_OFS
;
4743 probe_ent
->port
[p
].bmdma_addr
= pci_resource_start(pdev
, 4);
4744 ata_std_ports(&probe_ent
->port
[p
]);
4748 if (ports
& ATA_PORT_SECONDARY
) {
4749 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 2);
4750 probe_ent
->port
[p
].altstatus_addr
=
4751 probe_ent
->port
[p
].ctl_addr
=
4752 pci_resource_start(pdev
, 3) | ATA_PCI_CTL_OFS
;
4753 probe_ent
->port
[p
].bmdma_addr
= pci_resource_start(pdev
, 4) + 8;
4754 ata_std_ports(&probe_ent
->port
[p
]);
4758 probe_ent
->n_ports
= p
;
4762 static struct ata_probe_ent
*ata_pci_init_legacy_port(struct pci_dev
*pdev
, struct ata_port_info
*port
, int port_num
)
4764 struct ata_probe_ent
*probe_ent
;
4766 probe_ent
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
);
4770 probe_ent
->legacy_mode
= 1;
4771 probe_ent
->n_ports
= 1;
4772 probe_ent
->hard_port_no
= port_num
;
4773 probe_ent
->private_data
= port
->private_data
;
4778 probe_ent
->irq
= 14;
4779 probe_ent
->port
[0].cmd_addr
= 0x1f0;
4780 probe_ent
->port
[0].altstatus_addr
=
4781 probe_ent
->port
[0].ctl_addr
= 0x3f6;
4784 probe_ent
->irq
= 15;
4785 probe_ent
->port
[0].cmd_addr
= 0x170;
4786 probe_ent
->port
[0].altstatus_addr
=
4787 probe_ent
->port
[0].ctl_addr
= 0x376;
4790 probe_ent
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4) + 8 * port_num
;
4791 ata_std_ports(&probe_ent
->port
[0]);
4796 * ata_pci_init_one - Initialize/register PCI IDE host controller
4797 * @pdev: Controller to be initialized
4798 * @port_info: Information from low-level host driver
4799 * @n_ports: Number of ports attached to host controller
4801 * This is a helper function which can be called from a driver's
4802 * xxx_init_one() probe function if the hardware uses traditional
4803 * IDE taskfile registers.
4805 * This function calls pci_enable_device(), reserves its register
4806 * regions, sets the dma mask, enables bus master mode, and calls
4810 * Inherited from PCI layer (may sleep).
4813 * Zero on success, negative on errno-based value on error.
4816 int ata_pci_init_one (struct pci_dev
*pdev
, struct ata_port_info
**port_info
,
4817 unsigned int n_ports
)
4819 struct ata_probe_ent
*probe_ent
= NULL
, *probe_ent2
= NULL
;
4820 struct ata_port_info
*port
[2];
4822 unsigned int legacy_mode
= 0;
4823 int disable_dev_on_err
= 1;
4828 port
[0] = port_info
[0];
4830 port
[1] = port_info
[1];
4834 if ((port
[0]->host_flags
& ATA_FLAG_NO_LEGACY
) == 0
4835 && (pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
4836 /* TODO: What if one channel is in native mode ... */
4837 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
4838 mask
= (1 << 2) | (1 << 0);
4839 if ((tmp8
& mask
) != mask
)
4840 legacy_mode
= (1 << 3);
4844 if ((!legacy_mode
) && (n_ports
> 2)) {
4845 printk(KERN_ERR
"ata: BUG: native mode, n_ports > 2\n");
4850 /* FIXME: Really for ATA it isn't safe because the device may be
4851 multi-purpose and we want to leave it alone if it was already
4852 enabled. Secondly for shared use as Arjan says we want refcounting
4854 Checking dev->is_enabled is insufficient as this is not set at
4855 boot for the primary video which is BIOS enabled
4858 rc
= pci_enable_device(pdev
);
4862 rc
= pci_request_regions(pdev
, DRV_NAME
);
4864 disable_dev_on_err
= 0;
4868 /* FIXME: Should use platform specific mappers for legacy port ranges */
4870 if (!request_region(0x1f0, 8, "libata")) {
4871 struct resource
*conflict
, res
;
4873 res
.end
= 0x1f0 + 8 - 1;
4874 conflict
= ____request_resource(&ioport_resource
, &res
);
4875 if (!strcmp(conflict
->name
, "libata"))
4876 legacy_mode
|= (1 << 0);
4878 disable_dev_on_err
= 0;
4879 printk(KERN_WARNING
"ata: 0x1f0 IDE port busy\n");
4882 legacy_mode
|= (1 << 0);
4884 if (!request_region(0x170, 8, "libata")) {
4885 struct resource
*conflict
, res
;
4887 res
.end
= 0x170 + 8 - 1;
4888 conflict
= ____request_resource(&ioport_resource
, &res
);
4889 if (!strcmp(conflict
->name
, "libata"))
4890 legacy_mode
|= (1 << 1);
4892 disable_dev_on_err
= 0;
4893 printk(KERN_WARNING
"ata: 0x170 IDE port busy\n");
4896 legacy_mode
|= (1 << 1);
4899 /* we have legacy mode, but all ports are unavailable */
4900 if (legacy_mode
== (1 << 3)) {
4902 goto err_out_regions
;
4905 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
4907 goto err_out_regions
;
4908 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
4910 goto err_out_regions
;
4913 if (legacy_mode
& (1 << 0))
4914 probe_ent
= ata_pci_init_legacy_port(pdev
, port
[0], 0);
4915 if (legacy_mode
& (1 << 1))
4916 probe_ent2
= ata_pci_init_legacy_port(pdev
, port
[1], 1);
4919 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
| ATA_PORT_SECONDARY
);
4921 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
);
4923 if (!probe_ent
&& !probe_ent2
) {
4925 goto err_out_regions
;
4928 pci_set_master(pdev
);
4930 /* FIXME: check ata_device_add return */
4932 if (legacy_mode
& (1 << 0))
4933 ata_device_add(probe_ent
);
4934 if (legacy_mode
& (1 << 1))
4935 ata_device_add(probe_ent2
);
4937 ata_device_add(probe_ent
);
4945 if (legacy_mode
& (1 << 0))
4946 release_region(0x1f0, 8);
4947 if (legacy_mode
& (1 << 1))
4948 release_region(0x170, 8);
4949 pci_release_regions(pdev
);
4951 if (disable_dev_on_err
)
4952 pci_disable_device(pdev
);
4957 * ata_pci_remove_one - PCI layer callback for device removal
4958 * @pdev: PCI device that was removed
4960 * PCI layer indicates to libata via this hook that
4961 * hot-unplug or module unload event has occurred.
4962 * Handle this by unregistering all objects associated
4963 * with this PCI device. Free those objects. Then finally
4964 * release PCI resources and disable device.
4967 * Inherited from PCI layer (may sleep).
4970 void ata_pci_remove_one (struct pci_dev
*pdev
)
4972 struct device
*dev
= pci_dev_to_dev(pdev
);
4973 struct ata_host_set
*host_set
= dev_get_drvdata(dev
);
4975 ata_host_set_remove(host_set
);
4976 pci_release_regions(pdev
);
4977 pci_disable_device(pdev
);
4978 dev_set_drvdata(dev
, NULL
);
4981 /* move to PCI subsystem */
4982 int pci_test_config_bits(struct pci_dev
*pdev
, const struct pci_bits
*bits
)
4984 unsigned long tmp
= 0;
4986 switch (bits
->width
) {
4989 pci_read_config_byte(pdev
, bits
->reg
, &tmp8
);
4995 pci_read_config_word(pdev
, bits
->reg
, &tmp16
);
5001 pci_read_config_dword(pdev
, bits
->reg
, &tmp32
);
5012 return (tmp
== bits
->val
) ? 1 : 0;
5015 int ata_pci_device_suspend(struct pci_dev
*pdev
, pm_message_t state
)
5017 pci_save_state(pdev
);
5018 pci_disable_device(pdev
);
5019 pci_set_power_state(pdev
, PCI_D3hot
);
5023 int ata_pci_device_resume(struct pci_dev
*pdev
)
5025 pci_set_power_state(pdev
, PCI_D0
);
5026 pci_restore_state(pdev
);
5027 pci_enable_device(pdev
);
5028 pci_set_master(pdev
);
5031 #endif /* CONFIG_PCI */
5034 static int __init
ata_init(void)
5036 ata_wq
= create_workqueue("ata");
5040 printk(KERN_DEBUG
"libata version " DRV_VERSION
" loaded.\n");
5044 static void __exit
ata_exit(void)
5046 destroy_workqueue(ata_wq
);
5049 module_init(ata_init
);
5050 module_exit(ata_exit
);
5052 static unsigned long ratelimit_time
;
5053 static spinlock_t ata_ratelimit_lock
= SPIN_LOCK_UNLOCKED
;
5055 int ata_ratelimit(void)
5058 unsigned long flags
;
5060 spin_lock_irqsave(&ata_ratelimit_lock
, flags
);
5062 if (time_after(jiffies
, ratelimit_time
)) {
5064 ratelimit_time
= jiffies
+ (HZ
/5);
5068 spin_unlock_irqrestore(&ata_ratelimit_lock
, flags
);
5074 * libata is essentially a library of internal helper functions for
5075 * low-level ATA host controller drivers. As such, the API/ABI is
5076 * likely to change as new drivers are added and updated.
5077 * Do not depend on ABI/API stability.
5080 EXPORT_SYMBOL_GPL(ata_std_bios_param
);
5081 EXPORT_SYMBOL_GPL(ata_std_ports
);
5082 EXPORT_SYMBOL_GPL(ata_device_add
);
5083 EXPORT_SYMBOL_GPL(ata_host_set_remove
);
5084 EXPORT_SYMBOL_GPL(ata_sg_init
);
5085 EXPORT_SYMBOL_GPL(ata_sg_init_one
);
5086 EXPORT_SYMBOL_GPL(ata_qc_complete
);
5087 EXPORT_SYMBOL_GPL(ata_qc_issue_prot
);
5088 EXPORT_SYMBOL_GPL(ata_eng_timeout
);
5089 EXPORT_SYMBOL_GPL(ata_tf_load
);
5090 EXPORT_SYMBOL_GPL(ata_tf_read
);
5091 EXPORT_SYMBOL_GPL(ata_noop_dev_select
);
5092 EXPORT_SYMBOL_GPL(ata_std_dev_select
);
5093 EXPORT_SYMBOL_GPL(ata_tf_to_fis
);
5094 EXPORT_SYMBOL_GPL(ata_tf_from_fis
);
5095 EXPORT_SYMBOL_GPL(ata_check_status
);
5096 EXPORT_SYMBOL_GPL(ata_altstatus
);
5097 EXPORT_SYMBOL_GPL(ata_exec_command
);
5098 EXPORT_SYMBOL_GPL(ata_port_start
);
5099 EXPORT_SYMBOL_GPL(ata_port_stop
);
5100 EXPORT_SYMBOL_GPL(ata_host_stop
);
5101 EXPORT_SYMBOL_GPL(ata_interrupt
);
5102 EXPORT_SYMBOL_GPL(ata_qc_prep
);
5103 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
5104 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
5105 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear
);
5106 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
5107 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
5108 EXPORT_SYMBOL_GPL(ata_port_probe
);
5109 EXPORT_SYMBOL_GPL(sata_phy_reset
);
5110 EXPORT_SYMBOL_GPL(__sata_phy_reset
);
5111 EXPORT_SYMBOL_GPL(ata_bus_reset
);
5112 EXPORT_SYMBOL_GPL(ata_port_disable
);
5113 EXPORT_SYMBOL_GPL(ata_ratelimit
);
5114 EXPORT_SYMBOL_GPL(ata_scsi_ioctl
);
5115 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd
);
5116 EXPORT_SYMBOL_GPL(ata_scsi_error
);
5117 EXPORT_SYMBOL_GPL(ata_scsi_slave_config
);
5118 EXPORT_SYMBOL_GPL(ata_scsi_release
);
5119 EXPORT_SYMBOL_GPL(ata_host_intr
);
5120 EXPORT_SYMBOL_GPL(ata_dev_classify
);
5121 EXPORT_SYMBOL_GPL(ata_dev_id_string
);
5122 EXPORT_SYMBOL_GPL(ata_dev_config
);
5123 EXPORT_SYMBOL_GPL(ata_scsi_simulate
);
5125 EXPORT_SYMBOL_GPL(ata_timing_compute
);
5126 EXPORT_SYMBOL_GPL(ata_timing_merge
);
5129 EXPORT_SYMBOL_GPL(pci_test_config_bits
);
5130 EXPORT_SYMBOL_GPL(ata_pci_host_stop
);
5131 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode
);
5132 EXPORT_SYMBOL_GPL(ata_pci_init_one
);
5133 EXPORT_SYMBOL_GPL(ata_pci_remove_one
);
5134 EXPORT_SYMBOL_GPL(ata_pci_device_suspend
);
5135 EXPORT_SYMBOL_GPL(ata_pci_device_resume
);
5136 #endif /* CONFIG_PCI */
5138 EXPORT_SYMBOL_GPL(ata_device_suspend
);
5139 EXPORT_SYMBOL_GPL(ata_device_resume
);
5140 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend
);
5141 EXPORT_SYMBOL_GPL(ata_scsi_device_resume
);