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 void ata_dev_reread_id(struct ata_port
*ap
, struct ata_device
*dev
);
65 static unsigned int ata_dev_init_params(struct ata_port
*ap
,
66 struct ata_device
*dev
);
67 static void ata_set_mode(struct ata_port
*ap
);
68 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
);
69 static unsigned int ata_get_mode_mask(const struct ata_port
*ap
, int shift
);
70 static int fgb(u32 bitmap
);
71 static int ata_choose_xfer_mode(const struct ata_port
*ap
,
73 unsigned int *xfer_shift_out
);
75 static unsigned int ata_unique_id
= 1;
76 static struct workqueue_struct
*ata_wq
;
78 int atapi_enabled
= 0;
79 module_param(atapi_enabled
, int, 0444);
80 MODULE_PARM_DESC(atapi_enabled
, "Enable discovery of ATAPI devices (0=off, 1=on)");
82 MODULE_AUTHOR("Jeff Garzik");
83 MODULE_DESCRIPTION("Library module for ATA devices");
84 MODULE_LICENSE("GPL");
85 MODULE_VERSION(DRV_VERSION
);
89 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
90 * @tf: Taskfile to convert
91 * @fis: Buffer into which data will output
92 * @pmp: Port multiplier port
94 * Converts a standard ATA taskfile to a Serial ATA
95 * FIS structure (Register - Host to Device).
98 * Inherited from caller.
101 void ata_tf_to_fis(const struct ata_taskfile
*tf
, u8
*fis
, u8 pmp
)
103 fis
[0] = 0x27; /* Register - Host to Device FIS */
104 fis
[1] = (pmp
& 0xf) | (1 << 7); /* Port multiplier number,
105 bit 7 indicates Command FIS */
106 fis
[2] = tf
->command
;
107 fis
[3] = tf
->feature
;
114 fis
[8] = tf
->hob_lbal
;
115 fis
[9] = tf
->hob_lbam
;
116 fis
[10] = tf
->hob_lbah
;
117 fis
[11] = tf
->hob_feature
;
120 fis
[13] = tf
->hob_nsect
;
131 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
132 * @fis: Buffer from which data will be input
133 * @tf: Taskfile to output
135 * Converts a serial ATA FIS structure to a standard ATA taskfile.
138 * Inherited from caller.
141 void ata_tf_from_fis(const u8
*fis
, struct ata_taskfile
*tf
)
143 tf
->command
= fis
[2]; /* status */
144 tf
->feature
= fis
[3]; /* error */
151 tf
->hob_lbal
= fis
[8];
152 tf
->hob_lbam
= fis
[9];
153 tf
->hob_lbah
= fis
[10];
156 tf
->hob_nsect
= fis
[13];
159 static const u8 ata_rw_cmds
[] = {
163 ATA_CMD_READ_MULTI_EXT
,
164 ATA_CMD_WRITE_MULTI_EXT
,
168 ATA_CMD_WRITE_MULTI_FUA_EXT
,
172 ATA_CMD_PIO_READ_EXT
,
173 ATA_CMD_PIO_WRITE_EXT
,
186 ATA_CMD_WRITE_FUA_EXT
190 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
191 * @qc: command to examine and configure
193 * Examine the device configuration and tf->flags to calculate
194 * the proper read/write commands and protocol to use.
199 int ata_rwcmd_protocol(struct ata_queued_cmd
*qc
)
201 struct ata_taskfile
*tf
= &qc
->tf
;
202 struct ata_device
*dev
= qc
->dev
;
205 int index
, fua
, lba48
, write
;
207 fua
= (tf
->flags
& ATA_TFLAG_FUA
) ? 4 : 0;
208 lba48
= (tf
->flags
& ATA_TFLAG_LBA48
) ? 2 : 0;
209 write
= (tf
->flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
211 if (dev
->flags
& ATA_DFLAG_PIO
) {
212 tf
->protocol
= ATA_PROT_PIO
;
213 index
= dev
->multi_count
? 0 : 8;
214 } else if (lba48
&& (qc
->ap
->flags
& ATA_FLAG_PIO_LBA48
)) {
215 /* Unable to use DMA due to host limitation */
216 tf
->protocol
= ATA_PROT_PIO
;
217 index
= dev
->multi_count
? 0 : 8;
219 tf
->protocol
= ATA_PROT_DMA
;
223 cmd
= ata_rw_cmds
[index
+ fua
+ lba48
+ write
];
231 static const char * const xfer_mode_str
[] = {
251 * ata_udma_string - convert UDMA bit offset to string
252 * @mask: mask of bits supported; only highest bit counts.
254 * Determine string which represents the highest speed
255 * (highest bit in @udma_mask).
261 * Constant C string representing highest speed listed in
262 * @udma_mask, or the constant C string "<n/a>".
265 static const char *ata_mode_string(unsigned int mask
)
269 for (i
= 7; i
>= 0; i
--)
272 for (i
= ATA_SHIFT_MWDMA
+ 2; i
>= ATA_SHIFT_MWDMA
; i
--)
275 for (i
= ATA_SHIFT_PIO
+ 4; i
>= ATA_SHIFT_PIO
; i
--)
282 return xfer_mode_str
[i
];
286 * ata_pio_devchk - PATA device presence detection
287 * @ap: ATA channel to examine
288 * @device: Device to examine (starting at zero)
290 * This technique was originally described in
291 * Hale Landis's ATADRVR (www.ata-atapi.com), and
292 * later found its way into the ATA/ATAPI spec.
294 * Write a pattern to the ATA shadow registers,
295 * and if a device is present, it will respond by
296 * correctly storing and echoing back the
297 * ATA shadow register contents.
303 static unsigned int ata_pio_devchk(struct ata_port
*ap
,
306 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
309 ap
->ops
->dev_select(ap
, device
);
311 outb(0x55, ioaddr
->nsect_addr
);
312 outb(0xaa, ioaddr
->lbal_addr
);
314 outb(0xaa, ioaddr
->nsect_addr
);
315 outb(0x55, ioaddr
->lbal_addr
);
317 outb(0x55, ioaddr
->nsect_addr
);
318 outb(0xaa, ioaddr
->lbal_addr
);
320 nsect
= inb(ioaddr
->nsect_addr
);
321 lbal
= inb(ioaddr
->lbal_addr
);
323 if ((nsect
== 0x55) && (lbal
== 0xaa))
324 return 1; /* we found a device */
326 return 0; /* nothing found */
330 * ata_mmio_devchk - PATA device presence detection
331 * @ap: ATA channel to examine
332 * @device: Device to examine (starting at zero)
334 * This technique was originally described in
335 * Hale Landis's ATADRVR (www.ata-atapi.com), and
336 * later found its way into the ATA/ATAPI spec.
338 * Write a pattern to the ATA shadow registers,
339 * and if a device is present, it will respond by
340 * correctly storing and echoing back the
341 * ATA shadow register contents.
347 static unsigned int ata_mmio_devchk(struct ata_port
*ap
,
350 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
353 ap
->ops
->dev_select(ap
, device
);
355 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
356 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
358 writeb(0xaa, (void __iomem
*) ioaddr
->nsect_addr
);
359 writeb(0x55, (void __iomem
*) ioaddr
->lbal_addr
);
361 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
362 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
364 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
365 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
367 if ((nsect
== 0x55) && (lbal
== 0xaa))
368 return 1; /* we found a device */
370 return 0; /* nothing found */
374 * ata_devchk - PATA device presence detection
375 * @ap: ATA channel to examine
376 * @device: Device to examine (starting at zero)
378 * Dispatch ATA device presence detection, depending
379 * on whether we are using PIO or MMIO to talk to the
380 * ATA shadow registers.
386 static unsigned int ata_devchk(struct ata_port
*ap
,
389 if (ap
->flags
& ATA_FLAG_MMIO
)
390 return ata_mmio_devchk(ap
, device
);
391 return ata_pio_devchk(ap
, device
);
395 * ata_dev_classify - determine device type based on ATA-spec signature
396 * @tf: ATA taskfile register set for device to be identified
398 * Determine from taskfile register contents whether a device is
399 * ATA or ATAPI, as per "Signature and persistence" section
400 * of ATA/PI spec (volume 1, sect 5.14).
406 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
407 * the event of failure.
410 unsigned int ata_dev_classify(const struct ata_taskfile
*tf
)
412 /* Apple's open source Darwin code hints that some devices only
413 * put a proper signature into the LBA mid/high registers,
414 * So, we only check those. It's sufficient for uniqueness.
417 if (((tf
->lbam
== 0) && (tf
->lbah
== 0)) ||
418 ((tf
->lbam
== 0x3c) && (tf
->lbah
== 0xc3))) {
419 DPRINTK("found ATA device by sig\n");
423 if (((tf
->lbam
== 0x14) && (tf
->lbah
== 0xeb)) ||
424 ((tf
->lbam
== 0x69) && (tf
->lbah
== 0x96))) {
425 DPRINTK("found ATAPI device by sig\n");
426 return ATA_DEV_ATAPI
;
429 DPRINTK("unknown device\n");
430 return ATA_DEV_UNKNOWN
;
434 * ata_dev_try_classify - Parse returned ATA device signature
435 * @ap: ATA channel to examine
436 * @device: Device to examine (starting at zero)
437 * @r_err: Value of error register on completion
439 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
440 * an ATA/ATAPI-defined set of values is placed in the ATA
441 * shadow registers, indicating the results of device detection
444 * Select the ATA device, and read the values from the ATA shadow
445 * registers. Then parse according to the Error register value,
446 * and the spec-defined values examined by ata_dev_classify().
452 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
456 ata_dev_try_classify(struct ata_port
*ap
, unsigned int device
, u8
*r_err
)
458 struct ata_taskfile tf
;
462 ap
->ops
->dev_select(ap
, device
);
464 memset(&tf
, 0, sizeof(tf
));
466 ap
->ops
->tf_read(ap
, &tf
);
471 /* see if device passed diags */
474 else if ((device
== 0) && (err
== 0x81))
479 /* determine if device is ATA or ATAPI */
480 class = ata_dev_classify(&tf
);
482 if (class == ATA_DEV_UNKNOWN
)
484 if ((class == ATA_DEV_ATA
) && (ata_chk_status(ap
) == 0))
490 * ata_id_string - Convert IDENTIFY DEVICE page into string
491 * @id: IDENTIFY DEVICE results we will examine
492 * @s: string into which data is output
493 * @ofs: offset into identify device page
494 * @len: length of string to return. must be an even number.
496 * The strings in the IDENTIFY DEVICE page are broken up into
497 * 16-bit chunks. Run through the string, and output each
498 * 8-bit chunk linearly, regardless of platform.
504 void ata_id_string(const u16
*id
, unsigned char *s
,
505 unsigned int ofs
, unsigned int len
)
524 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
525 * @id: IDENTIFY DEVICE results we will examine
526 * @s: string into which data is output
527 * @ofs: offset into identify device page
528 * @len: length of string to return. must be an odd number.
530 * This function is identical to ata_id_string except that it
531 * trims trailing spaces and terminates the resulting string with
532 * null. @len must be actual maximum length (even number) + 1.
537 void ata_id_c_string(const u16
*id
, unsigned char *s
,
538 unsigned int ofs
, unsigned int len
)
544 ata_id_string(id
, s
, ofs
, len
- 1);
546 p
= s
+ strnlen(s
, len
- 1);
547 while (p
> s
&& p
[-1] == ' ')
552 static u64
ata_id_n_sectors(const u16
*id
)
554 if (ata_id_has_lba(id
)) {
555 if (ata_id_has_lba48(id
))
556 return ata_id_u64(id
, 100);
558 return ata_id_u32(id
, 60);
560 if (ata_id_current_chs_valid(id
))
561 return ata_id_u32(id
, 57);
563 return id
[1] * id
[3] * id
[6];
568 * ata_noop_dev_select - Select device 0/1 on ATA bus
569 * @ap: ATA channel to manipulate
570 * @device: ATA device (numbered from zero) to select
572 * This function performs no actual function.
574 * May be used as the dev_select() entry in ata_port_operations.
579 void ata_noop_dev_select (struct ata_port
*ap
, unsigned int device
)
585 * ata_std_dev_select - Select device 0/1 on ATA bus
586 * @ap: ATA channel to manipulate
587 * @device: ATA device (numbered from zero) to select
589 * Use the method defined in the ATA specification to
590 * make either device 0, or device 1, active on the
591 * ATA channel. Works with both PIO and MMIO.
593 * May be used as the dev_select() entry in ata_port_operations.
599 void ata_std_dev_select (struct ata_port
*ap
, unsigned int device
)
604 tmp
= ATA_DEVICE_OBS
;
606 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
608 if (ap
->flags
& ATA_FLAG_MMIO
) {
609 writeb(tmp
, (void __iomem
*) ap
->ioaddr
.device_addr
);
611 outb(tmp
, ap
->ioaddr
.device_addr
);
613 ata_pause(ap
); /* needed; also flushes, for mmio */
617 * ata_dev_select - Select device 0/1 on ATA bus
618 * @ap: ATA channel to manipulate
619 * @device: ATA device (numbered from zero) to select
620 * @wait: non-zero to wait for Status register BSY bit to clear
621 * @can_sleep: non-zero if context allows sleeping
623 * Use the method defined in the ATA specification to
624 * make either device 0, or device 1, active on the
627 * This is a high-level version of ata_std_dev_select(),
628 * which additionally provides the services of inserting
629 * the proper pauses and status polling, where needed.
635 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
636 unsigned int wait
, unsigned int can_sleep
)
638 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
639 ap
->id
, device
, wait
);
644 ap
->ops
->dev_select(ap
, device
);
647 if (can_sleep
&& ap
->device
[device
].class == ATA_DEV_ATAPI
)
654 * ata_dump_id - IDENTIFY DEVICE info debugging output
655 * @id: IDENTIFY DEVICE page to dump
657 * Dump selected 16-bit words from the given IDENTIFY DEVICE
664 static inline void ata_dump_id(const u16
*id
)
666 DPRINTK("49==0x%04x "
676 DPRINTK("80==0x%04x "
686 DPRINTK("88==0x%04x "
693 * Compute the PIO modes available for this device. This is not as
694 * trivial as it seems if we must consider early devices correctly.
696 * FIXME: pre IDE drive timing (do we care ?).
699 static unsigned int ata_pio_modes(const struct ata_device
*adev
)
703 /* Usual case. Word 53 indicates word 64 is valid */
704 if (adev
->id
[ATA_ID_FIELD_VALID
] & (1 << 1)) {
705 modes
= adev
->id
[ATA_ID_PIO_MODES
] & 0x03;
711 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
712 number for the maximum. Turn it into a mask and return it */
713 modes
= (2 << ((adev
->id
[ATA_ID_OLD_PIO_MODES
] >> 8) & 0xFF)) - 1 ;
715 /* But wait.. there's more. Design your standards by committee and
716 you too can get a free iordy field to process. However its the
717 speeds not the modes that are supported... Note drivers using the
718 timing API will get this right anyway */
722 ata_queue_packet_task(struct ata_port
*ap
)
724 if (!(ap
->flags
& ATA_FLAG_FLUSH_PIO_TASK
))
725 queue_work(ata_wq
, &ap
->packet_task
);
729 ata_queue_pio_task(struct ata_port
*ap
)
731 if (!(ap
->flags
& ATA_FLAG_FLUSH_PIO_TASK
))
732 queue_work(ata_wq
, &ap
->pio_task
);
736 ata_queue_delayed_pio_task(struct ata_port
*ap
, unsigned long delay
)
738 if (!(ap
->flags
& ATA_FLAG_FLUSH_PIO_TASK
))
739 queue_delayed_work(ata_wq
, &ap
->pio_task
, delay
);
743 * ata_flush_pio_tasks - Flush pio_task and packet_task
744 * @ap: the target ata_port
746 * After this function completes, pio_task and packet_task are
747 * guranteed not to be running or scheduled.
750 * Kernel thread context (may sleep)
753 static void ata_flush_pio_tasks(struct ata_port
*ap
)
760 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
761 ap
->flags
|= ATA_FLAG_FLUSH_PIO_TASK
;
762 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
764 DPRINTK("flush #1\n");
765 flush_workqueue(ata_wq
);
768 * At this point, if a task is running, it's guaranteed to see
769 * the FLUSH flag; thus, it will never queue pio tasks again.
772 tmp
|= cancel_delayed_work(&ap
->pio_task
);
773 tmp
|= cancel_delayed_work(&ap
->packet_task
);
775 DPRINTK("flush #2\n");
776 flush_workqueue(ata_wq
);
779 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
780 ap
->flags
&= ~ATA_FLAG_FLUSH_PIO_TASK
;
781 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
786 void ata_qc_complete_internal(struct ata_queued_cmd
*qc
)
788 struct completion
*waiting
= qc
->private_data
;
790 qc
->ap
->ops
->tf_read(qc
->ap
, &qc
->tf
);
795 * ata_exec_internal - execute libata internal command
796 * @ap: Port to which the command is sent
797 * @dev: Device to which the command is sent
798 * @tf: Taskfile registers for the command and the result
799 * @dma_dir: Data tranfer direction of the command
800 * @buf: Data buffer of the command
801 * @buflen: Length of data buffer
803 * Executes libata internal command with timeout. @tf contains
804 * command on entry and result on return. Timeout and error
805 * conditions are reported via return value. No recovery action
806 * is taken after a command times out. It's caller's duty to
807 * clean up after timeout.
810 * None. Should be called with kernel context, might sleep.
814 ata_exec_internal(struct ata_port
*ap
, struct ata_device
*dev
,
815 struct ata_taskfile
*tf
,
816 int dma_dir
, void *buf
, unsigned int buflen
)
818 u8 command
= tf
->command
;
819 struct ata_queued_cmd
*qc
;
820 DECLARE_COMPLETION(wait
);
822 unsigned int err_mask
;
824 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
826 qc
= ata_qc_new_init(ap
, dev
);
830 qc
->dma_dir
= dma_dir
;
831 if (dma_dir
!= DMA_NONE
) {
832 ata_sg_init_one(qc
, buf
, buflen
);
833 qc
->nsect
= buflen
/ ATA_SECT_SIZE
;
836 qc
->private_data
= &wait
;
837 qc
->complete_fn
= ata_qc_complete_internal
;
839 qc
->err_mask
= ata_qc_issue(qc
);
843 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
845 if (!wait_for_completion_timeout(&wait
, ATA_TMOUT_INTERNAL
)) {
846 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
848 /* We're racing with irq here. If we lose, the
849 * following test prevents us from completing the qc
850 * again. If completion irq occurs after here but
851 * before the caller cleans up, it will result in a
852 * spurious interrupt. We can live with that.
854 if (qc
->flags
& ATA_QCFLAG_ACTIVE
) {
855 qc
->err_mask
= AC_ERR_TIMEOUT
;
857 printk(KERN_WARNING
"ata%u: qc timeout (cmd 0x%x)\n",
861 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
865 err_mask
= qc
->err_mask
;
873 * ata_pio_need_iordy - check if iordy needed
876 * Check if the current speed of the device requires IORDY. Used
877 * by various controllers for chip configuration.
880 unsigned int ata_pio_need_iordy(const struct ata_device
*adev
)
883 int speed
= adev
->pio_mode
- XFER_PIO_0
;
890 /* If we have no drive specific rule, then PIO 2 is non IORDY */
892 if (adev
->id
[ATA_ID_FIELD_VALID
] & 2) { /* EIDE */
893 pio
= adev
->id
[ATA_ID_EIDE_PIO
];
894 /* Is the speed faster than the drive allows non IORDY ? */
896 /* This is cycle times not frequency - watch the logic! */
897 if (pio
> 240) /* PIO2 is 240nS per cycle */
906 * ata_dev_identify - obtain IDENTIFY x DEVICE page
907 * @ap: port on which device we wish to probe resides
908 * @device: device bus address, starting at zero
910 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
911 * command, and read back the 512-byte device information page.
912 * The device information page is fed to us via the standard
913 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
914 * using standard PIO-IN paths)
916 * After reading the device information page, we use several
917 * bits of information from it to initialize data structures
918 * that will be used during the lifetime of the ata_device.
919 * Other data from the info page is used to disqualify certain
920 * older ATA devices we do not wish to support.
923 * Inherited from caller. Some functions called by this function
924 * obtain the host_set lock.
927 static void ata_dev_identify(struct ata_port
*ap
, unsigned int device
)
929 struct ata_device
*dev
= &ap
->device
[device
];
930 unsigned int major_version
;
931 unsigned long xfer_modes
;
932 unsigned int using_edd
;
933 struct ata_taskfile tf
;
934 unsigned int err_mask
;
937 if (!ata_dev_present(dev
)) {
938 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
943 if (ap
->ops
->probe_reset
||
944 ap
->flags
& (ATA_FLAG_SRST
| ATA_FLAG_SATA_RESET
))
949 DPRINTK("ENTER, host %u, dev %u\n", ap
->id
, device
);
951 WARN_ON(dev
->class != ATA_DEV_ATA
&& dev
->class != ATA_DEV_ATAPI
&&
952 dev
->class != ATA_DEV_NONE
);
954 ata_dev_select(ap
, device
, 1, 1); /* select device 0/1 */
957 ata_tf_init(ap
, &tf
, device
);
959 if (dev
->class == ATA_DEV_ATA
) {
960 tf
.command
= ATA_CMD_ID_ATA
;
961 DPRINTK("do ATA identify\n");
963 tf
.command
= ATA_CMD_ID_ATAPI
;
964 DPRINTK("do ATAPI identify\n");
967 tf
.protocol
= ATA_PROT_PIO
;
969 err_mask
= ata_exec_internal(ap
, dev
, &tf
, DMA_FROM_DEVICE
,
970 dev
->id
, sizeof(dev
->id
));
973 if (err_mask
& ~AC_ERR_DEV
)
977 * arg! EDD works for all test cases, but seems to return
978 * the ATA signature for some ATAPI devices. Until the
979 * reason for this is found and fixed, we fix up the mess
980 * here. If IDENTIFY DEVICE returns command aborted
981 * (as ATAPI devices do), then we issue an
982 * IDENTIFY PACKET DEVICE.
984 * ATA software reset (SRST, the default) does not appear
985 * to have this problem.
987 if ((using_edd
) && (dev
->class == ATA_DEV_ATA
)) {
989 if (err
& ATA_ABORTED
) {
990 dev
->class = ATA_DEV_ATAPI
;
997 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
999 /* print device capabilities */
1000 printk(KERN_DEBUG
"ata%u: dev %u cfg "
1001 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1002 ap
->id
, device
, dev
->id
[49],
1003 dev
->id
[82], dev
->id
[83], dev
->id
[84],
1004 dev
->id
[85], dev
->id
[86], dev
->id
[87],
1008 * common ATA, ATAPI feature tests
1011 /* we require DMA support (bits 8 of word 49) */
1012 if (!ata_id_has_dma(dev
->id
)) {
1013 printk(KERN_DEBUG
"ata%u: no dma\n", ap
->id
);
1017 /* quick-n-dirty find max transfer mode; for printk only */
1018 xfer_modes
= dev
->id
[ATA_ID_UDMA_MODES
];
1020 xfer_modes
= (dev
->id
[ATA_ID_MWDMA_MODES
]) << ATA_SHIFT_MWDMA
;
1022 xfer_modes
= ata_pio_modes(dev
);
1024 ata_dump_id(dev
->id
);
1026 /* ATA-specific feature tests */
1027 if (dev
->class == ATA_DEV_ATA
) {
1028 dev
->n_sectors
= ata_id_n_sectors(dev
->id
);
1030 if (!ata_id_is_ata(dev
->id
)) /* sanity check */
1033 /* get major version */
1034 major_version
= ata_id_major_version(dev
->id
);
1037 * The exact sequence expected by certain pre-ATA4 drives is:
1040 * INITIALIZE DEVICE PARAMETERS
1042 * Some drives were very specific about that exact sequence.
1044 if (major_version
< 4 || (!ata_id_has_lba(dev
->id
))) {
1045 err_mask
= ata_dev_init_params(ap
, dev
);
1047 printk(KERN_ERR
"ata%u: failed to init "
1048 "parameters, disabled\n", ap
->id
);
1052 /* current CHS translation info (id[53-58]) might be
1053 * changed. reread the identify device info.
1055 ata_dev_reread_id(ap
, dev
);
1058 if (ata_id_has_lba(dev
->id
)) {
1059 dev
->flags
|= ATA_DFLAG_LBA
;
1061 if (ata_id_has_lba48(dev
->id
))
1062 dev
->flags
|= ATA_DFLAG_LBA48
;
1064 /* print device info to dmesg */
1065 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1068 ata_mode_string(xfer_modes
),
1069 (unsigned long long)dev
->n_sectors
,
1070 dev
->flags
& ATA_DFLAG_LBA48
? " LBA48" : " LBA");
1074 /* Default translation */
1075 dev
->cylinders
= dev
->id
[1];
1076 dev
->heads
= dev
->id
[3];
1077 dev
->sectors
= dev
->id
[6];
1079 if (ata_id_current_chs_valid(dev
->id
)) {
1080 /* Current CHS translation is valid. */
1081 dev
->cylinders
= dev
->id
[54];
1082 dev
->heads
= dev
->id
[55];
1083 dev
->sectors
= dev
->id
[56];
1086 /* print device info to dmesg */
1087 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1090 ata_mode_string(xfer_modes
),
1091 (unsigned long long)dev
->n_sectors
,
1092 (int)dev
->cylinders
, (int)dev
->heads
, (int)dev
->sectors
);
1099 /* ATAPI-specific feature tests */
1100 else if (dev
->class == ATA_DEV_ATAPI
) {
1101 if (ata_id_is_ata(dev
->id
)) /* sanity check */
1104 rc
= atapi_cdb_len(dev
->id
);
1105 if ((rc
< 12) || (rc
> ATAPI_CDB_LEN
)) {
1106 printk(KERN_WARNING
"ata%u: unsupported CDB len\n", ap
->id
);
1109 dev
->cdb_len
= (unsigned int) rc
;
1111 /* print device info to dmesg */
1112 printk(KERN_INFO
"ata%u: dev %u ATAPI, max %s\n",
1114 ata_mode_string(xfer_modes
));
1117 ap
->host
->max_cmd_len
= 0;
1118 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1119 ap
->host
->max_cmd_len
= max_t(unsigned int,
1120 ap
->host
->max_cmd_len
,
1121 ap
->device
[i
].cdb_len
);
1123 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap
));
1127 printk(KERN_WARNING
"ata%u: dev %u not supported, ignoring\n",
1130 dev
->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1131 DPRINTK("EXIT, err\n");
1135 static inline u8
ata_dev_knobble(const struct ata_port
*ap
,
1136 struct ata_device
*dev
)
1138 return ((ap
->cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(dev
->id
)));
1142 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1149 void ata_dev_config(struct ata_port
*ap
, unsigned int i
)
1151 /* limit bridge transfers to udma5, 200 sectors */
1152 if (ata_dev_knobble(ap
, &ap
->device
[i
])) {
1153 printk(KERN_INFO
"ata%u(%u): applying bridge limits\n",
1155 ap
->udma_mask
&= ATA_UDMA5
;
1156 ap
->device
[i
].max_sectors
= ATA_MAX_SECTORS
;
1159 if (ap
->ops
->dev_config
)
1160 ap
->ops
->dev_config(ap
, &ap
->device
[i
]);
1164 * ata_bus_probe - Reset and probe ATA bus
1167 * Master ATA bus probing function. Initiates a hardware-dependent
1168 * bus reset, then attempts to identify any devices found on
1172 * PCI/etc. bus probe sem.
1175 * Zero on success, non-zero on error.
1178 static int ata_bus_probe(struct ata_port
*ap
)
1180 unsigned int i
, found
= 0;
1182 if (ap
->ops
->probe_reset
) {
1183 unsigned int classes
[ATA_MAX_DEVICES
];
1188 rc
= ap
->ops
->probe_reset(ap
, classes
);
1190 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1191 if (classes
[i
] == ATA_DEV_UNKNOWN
)
1192 classes
[i
] = ATA_DEV_NONE
;
1193 ap
->device
[i
].class = classes
[i
];
1196 printk(KERN_ERR
"ata%u: probe reset failed, "
1197 "disabling port\n", ap
->id
);
1198 ata_port_disable(ap
);
1201 ap
->ops
->phy_reset(ap
);
1203 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1206 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1207 ata_dev_identify(ap
, i
);
1208 if (ata_dev_present(&ap
->device
[i
])) {
1210 ata_dev_config(ap
,i
);
1214 if ((!found
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1215 goto err_out_disable
;
1218 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1219 goto err_out_disable
;
1224 ap
->ops
->port_disable(ap
);
1230 * ata_port_probe - Mark port as enabled
1231 * @ap: Port for which we indicate enablement
1233 * Modify @ap data structure such that the system
1234 * thinks that the entire port is enabled.
1236 * LOCKING: host_set lock, or some other form of
1240 void ata_port_probe(struct ata_port
*ap
)
1242 ap
->flags
&= ~ATA_FLAG_PORT_DISABLED
;
1246 * sata_print_link_status - Print SATA link status
1247 * @ap: SATA port to printk link status about
1249 * This function prints link speed and status of a SATA link.
1254 static void sata_print_link_status(struct ata_port
*ap
)
1259 if (!ap
->ops
->scr_read
)
1262 sstatus
= scr_read(ap
, SCR_STATUS
);
1264 if (sata_dev_present(ap
)) {
1265 tmp
= (sstatus
>> 4) & 0xf;
1268 else if (tmp
& (1 << 1))
1271 speed
= "<unknown>";
1272 printk(KERN_INFO
"ata%u: SATA link up %s Gbps (SStatus %X)\n",
1273 ap
->id
, speed
, sstatus
);
1275 printk(KERN_INFO
"ata%u: SATA link down (SStatus %X)\n",
1281 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1282 * @ap: SATA port associated with target SATA PHY.
1284 * This function issues commands to standard SATA Sxxx
1285 * PHY registers, to wake up the phy (and device), and
1286 * clear any reset condition.
1289 * PCI/etc. bus probe sem.
1292 void __sata_phy_reset(struct ata_port
*ap
)
1295 unsigned long timeout
= jiffies
+ (HZ
* 5);
1297 if (ap
->flags
& ATA_FLAG_SATA_RESET
) {
1298 /* issue phy wake/reset */
1299 scr_write_flush(ap
, SCR_CONTROL
, 0x301);
1300 /* Couldn't find anything in SATA I/II specs, but
1301 * AHCI-1.1 10.4.2 says at least 1 ms. */
1304 scr_write_flush(ap
, SCR_CONTROL
, 0x300); /* phy wake/clear reset */
1306 /* wait for phy to become ready, if necessary */
1309 sstatus
= scr_read(ap
, SCR_STATUS
);
1310 if ((sstatus
& 0xf) != 1)
1312 } while (time_before(jiffies
, timeout
));
1314 /* print link status */
1315 sata_print_link_status(ap
);
1317 /* TODO: phy layer with polling, timeouts, etc. */
1318 if (sata_dev_present(ap
))
1321 ata_port_disable(ap
);
1323 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1326 if (ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
)) {
1327 ata_port_disable(ap
);
1331 ap
->cbl
= ATA_CBL_SATA
;
1335 * sata_phy_reset - Reset SATA bus.
1336 * @ap: SATA port associated with target SATA PHY.
1338 * This function resets the SATA bus, and then probes
1339 * the bus for devices.
1342 * PCI/etc. bus probe sem.
1345 void sata_phy_reset(struct ata_port
*ap
)
1347 __sata_phy_reset(ap
);
1348 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1354 * ata_port_disable - Disable port.
1355 * @ap: Port to be disabled.
1357 * Modify @ap data structure such that the system
1358 * thinks that the entire port is disabled, and should
1359 * never attempt to probe or communicate with devices
1362 * LOCKING: host_set lock, or some other form of
1366 void ata_port_disable(struct ata_port
*ap
)
1368 ap
->device
[0].class = ATA_DEV_NONE
;
1369 ap
->device
[1].class = ATA_DEV_NONE
;
1370 ap
->flags
|= ATA_FLAG_PORT_DISABLED
;
1374 * This mode timing computation functionality is ported over from
1375 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1378 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1379 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1380 * for PIO 5, which is a nonstandard extension and UDMA6, which
1381 * is currently supported only by Maxtor drives.
1384 static const struct ata_timing ata_timing
[] = {
1386 { XFER_UDMA_6
, 0, 0, 0, 0, 0, 0, 0, 15 },
1387 { XFER_UDMA_5
, 0, 0, 0, 0, 0, 0, 0, 20 },
1388 { XFER_UDMA_4
, 0, 0, 0, 0, 0, 0, 0, 30 },
1389 { XFER_UDMA_3
, 0, 0, 0, 0, 0, 0, 0, 45 },
1391 { XFER_UDMA_2
, 0, 0, 0, 0, 0, 0, 0, 60 },
1392 { XFER_UDMA_1
, 0, 0, 0, 0, 0, 0, 0, 80 },
1393 { XFER_UDMA_0
, 0, 0, 0, 0, 0, 0, 0, 120 },
1395 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1397 { XFER_MW_DMA_2
, 25, 0, 0, 0, 70, 25, 120, 0 },
1398 { XFER_MW_DMA_1
, 45, 0, 0, 0, 80, 50, 150, 0 },
1399 { XFER_MW_DMA_0
, 60, 0, 0, 0, 215, 215, 480, 0 },
1401 { XFER_SW_DMA_2
, 60, 0, 0, 0, 120, 120, 240, 0 },
1402 { XFER_SW_DMA_1
, 90, 0, 0, 0, 240, 240, 480, 0 },
1403 { XFER_SW_DMA_0
, 120, 0, 0, 0, 480, 480, 960, 0 },
1405 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1406 { XFER_PIO_4
, 25, 70, 25, 120, 70, 25, 120, 0 },
1407 { XFER_PIO_3
, 30, 80, 70, 180, 80, 70, 180, 0 },
1409 { XFER_PIO_2
, 30, 290, 40, 330, 100, 90, 240, 0 },
1410 { XFER_PIO_1
, 50, 290, 93, 383, 125, 100, 383, 0 },
1411 { XFER_PIO_0
, 70, 290, 240, 600, 165, 150, 600, 0 },
1413 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1418 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1419 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1421 static void ata_timing_quantize(const struct ata_timing
*t
, struct ata_timing
*q
, int T
, int UT
)
1423 q
->setup
= EZ(t
->setup
* 1000, T
);
1424 q
->act8b
= EZ(t
->act8b
* 1000, T
);
1425 q
->rec8b
= EZ(t
->rec8b
* 1000, T
);
1426 q
->cyc8b
= EZ(t
->cyc8b
* 1000, T
);
1427 q
->active
= EZ(t
->active
* 1000, T
);
1428 q
->recover
= EZ(t
->recover
* 1000, T
);
1429 q
->cycle
= EZ(t
->cycle
* 1000, T
);
1430 q
->udma
= EZ(t
->udma
* 1000, UT
);
1433 void ata_timing_merge(const struct ata_timing
*a
, const struct ata_timing
*b
,
1434 struct ata_timing
*m
, unsigned int what
)
1436 if (what
& ATA_TIMING_SETUP
) m
->setup
= max(a
->setup
, b
->setup
);
1437 if (what
& ATA_TIMING_ACT8B
) m
->act8b
= max(a
->act8b
, b
->act8b
);
1438 if (what
& ATA_TIMING_REC8B
) m
->rec8b
= max(a
->rec8b
, b
->rec8b
);
1439 if (what
& ATA_TIMING_CYC8B
) m
->cyc8b
= max(a
->cyc8b
, b
->cyc8b
);
1440 if (what
& ATA_TIMING_ACTIVE
) m
->active
= max(a
->active
, b
->active
);
1441 if (what
& ATA_TIMING_RECOVER
) m
->recover
= max(a
->recover
, b
->recover
);
1442 if (what
& ATA_TIMING_CYCLE
) m
->cycle
= max(a
->cycle
, b
->cycle
);
1443 if (what
& ATA_TIMING_UDMA
) m
->udma
= max(a
->udma
, b
->udma
);
1446 static const struct ata_timing
* ata_timing_find_mode(unsigned short speed
)
1448 const struct ata_timing
*t
;
1450 for (t
= ata_timing
; t
->mode
!= speed
; t
++)
1451 if (t
->mode
== 0xFF)
1456 int ata_timing_compute(struct ata_device
*adev
, unsigned short speed
,
1457 struct ata_timing
*t
, int T
, int UT
)
1459 const struct ata_timing
*s
;
1460 struct ata_timing p
;
1466 if (!(s
= ata_timing_find_mode(speed
)))
1469 memcpy(t
, s
, sizeof(*s
));
1472 * If the drive is an EIDE drive, it can tell us it needs extended
1473 * PIO/MW_DMA cycle timing.
1476 if (adev
->id
[ATA_ID_FIELD_VALID
] & 2) { /* EIDE drive */
1477 memset(&p
, 0, sizeof(p
));
1478 if(speed
>= XFER_PIO_0
&& speed
<= XFER_SW_DMA_0
) {
1479 if (speed
<= XFER_PIO_2
) p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO
];
1480 else p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO_IORDY
];
1481 } else if(speed
>= XFER_MW_DMA_0
&& speed
<= XFER_MW_DMA_2
) {
1482 p
.cycle
= adev
->id
[ATA_ID_EIDE_DMA_MIN
];
1484 ata_timing_merge(&p
, t
, t
, ATA_TIMING_CYCLE
| ATA_TIMING_CYC8B
);
1488 * Convert the timing to bus clock counts.
1491 ata_timing_quantize(t
, t
, T
, UT
);
1494 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1495 * S.M.A.R.T * and some other commands. We have to ensure that the
1496 * DMA cycle timing is slower/equal than the fastest PIO timing.
1499 if (speed
> XFER_PIO_4
) {
1500 ata_timing_compute(adev
, adev
->pio_mode
, &p
, T
, UT
);
1501 ata_timing_merge(&p
, t
, t
, ATA_TIMING_ALL
);
1505 * Lengthen active & recovery time so that cycle time is correct.
1508 if (t
->act8b
+ t
->rec8b
< t
->cyc8b
) {
1509 t
->act8b
+= (t
->cyc8b
- (t
->act8b
+ t
->rec8b
)) / 2;
1510 t
->rec8b
= t
->cyc8b
- t
->act8b
;
1513 if (t
->active
+ t
->recover
< t
->cycle
) {
1514 t
->active
+= (t
->cycle
- (t
->active
+ t
->recover
)) / 2;
1515 t
->recover
= t
->cycle
- t
->active
;
1521 static const struct {
1524 } xfer_mode_classes
[] = {
1525 { ATA_SHIFT_UDMA
, XFER_UDMA_0
},
1526 { ATA_SHIFT_MWDMA
, XFER_MW_DMA_0
},
1527 { ATA_SHIFT_PIO
, XFER_PIO_0
},
1530 static u8
base_from_shift(unsigned int shift
)
1534 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++)
1535 if (xfer_mode_classes
[i
].shift
== shift
)
1536 return xfer_mode_classes
[i
].base
;
1541 static void ata_dev_set_mode(struct ata_port
*ap
, struct ata_device
*dev
)
1546 if (!ata_dev_present(dev
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1549 if (dev
->xfer_shift
== ATA_SHIFT_PIO
)
1550 dev
->flags
|= ATA_DFLAG_PIO
;
1552 ata_dev_set_xfermode(ap
, dev
);
1554 base
= base_from_shift(dev
->xfer_shift
);
1555 ofs
= dev
->xfer_mode
- base
;
1556 idx
= ofs
+ dev
->xfer_shift
;
1557 WARN_ON(idx
>= ARRAY_SIZE(xfer_mode_str
));
1559 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1560 idx
, dev
->xfer_shift
, (int)dev
->xfer_mode
, (int)base
, ofs
);
1562 printk(KERN_INFO
"ata%u: dev %u configured for %s\n",
1563 ap
->id
, dev
->devno
, xfer_mode_str
[idx
]);
1566 static int ata_host_set_pio(struct ata_port
*ap
)
1572 mask
= ata_get_mode_mask(ap
, ATA_SHIFT_PIO
);
1575 printk(KERN_WARNING
"ata%u: no PIO support\n", ap
->id
);
1579 base
= base_from_shift(ATA_SHIFT_PIO
);
1580 xfer_mode
= base
+ x
;
1582 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1583 (int)base
, (int)xfer_mode
, mask
, x
);
1585 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1586 struct ata_device
*dev
= &ap
->device
[i
];
1587 if (ata_dev_present(dev
)) {
1588 dev
->pio_mode
= xfer_mode
;
1589 dev
->xfer_mode
= xfer_mode
;
1590 dev
->xfer_shift
= ATA_SHIFT_PIO
;
1591 if (ap
->ops
->set_piomode
)
1592 ap
->ops
->set_piomode(ap
, dev
);
1599 static void ata_host_set_dma(struct ata_port
*ap
, u8 xfer_mode
,
1600 unsigned int xfer_shift
)
1604 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1605 struct ata_device
*dev
= &ap
->device
[i
];
1606 if (ata_dev_present(dev
)) {
1607 dev
->dma_mode
= xfer_mode
;
1608 dev
->xfer_mode
= xfer_mode
;
1609 dev
->xfer_shift
= xfer_shift
;
1610 if (ap
->ops
->set_dmamode
)
1611 ap
->ops
->set_dmamode(ap
, dev
);
1617 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1618 * @ap: port on which timings will be programmed
1620 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1623 * PCI/etc. bus probe sem.
1625 static void ata_set_mode(struct ata_port
*ap
)
1627 unsigned int xfer_shift
;
1631 /* step 1: always set host PIO timings */
1632 rc
= ata_host_set_pio(ap
);
1636 /* step 2: choose the best data xfer mode */
1637 xfer_mode
= xfer_shift
= 0;
1638 rc
= ata_choose_xfer_mode(ap
, &xfer_mode
, &xfer_shift
);
1642 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1643 if (xfer_shift
!= ATA_SHIFT_PIO
)
1644 ata_host_set_dma(ap
, xfer_mode
, xfer_shift
);
1646 /* step 4: update devices' xfer mode */
1647 ata_dev_set_mode(ap
, &ap
->device
[0]);
1648 ata_dev_set_mode(ap
, &ap
->device
[1]);
1650 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1653 if (ap
->ops
->post_set_mode
)
1654 ap
->ops
->post_set_mode(ap
);
1659 ata_port_disable(ap
);
1663 * ata_tf_to_host - issue ATA taskfile to host controller
1664 * @ap: port to which command is being issued
1665 * @tf: ATA taskfile register set
1667 * Issues ATA taskfile register set to ATA host controller,
1668 * with proper synchronization with interrupt handler and
1672 * spin_lock_irqsave(host_set lock)
1675 static inline void ata_tf_to_host(struct ata_port
*ap
,
1676 const struct ata_taskfile
*tf
)
1678 ap
->ops
->tf_load(ap
, tf
);
1679 ap
->ops
->exec_command(ap
, tf
);
1683 * ata_busy_sleep - sleep until BSY clears, or timeout
1684 * @ap: port containing status register to be polled
1685 * @tmout_pat: impatience timeout
1686 * @tmout: overall timeout
1688 * Sleep until ATA Status register bit BSY clears,
1689 * or a timeout occurs.
1694 unsigned int ata_busy_sleep (struct ata_port
*ap
,
1695 unsigned long tmout_pat
, unsigned long tmout
)
1697 unsigned long timer_start
, timeout
;
1700 status
= ata_busy_wait(ap
, ATA_BUSY
, 300);
1701 timer_start
= jiffies
;
1702 timeout
= timer_start
+ tmout_pat
;
1703 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1705 status
= ata_busy_wait(ap
, ATA_BUSY
, 3);
1708 if (status
& ATA_BUSY
)
1709 printk(KERN_WARNING
"ata%u is slow to respond, "
1710 "please be patient\n", ap
->id
);
1712 timeout
= timer_start
+ tmout
;
1713 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1715 status
= ata_chk_status(ap
);
1718 if (status
& ATA_BUSY
) {
1719 printk(KERN_ERR
"ata%u failed to respond (%lu secs)\n",
1720 ap
->id
, tmout
/ HZ
);
1727 static void ata_bus_post_reset(struct ata_port
*ap
, unsigned int devmask
)
1729 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1730 unsigned int dev0
= devmask
& (1 << 0);
1731 unsigned int dev1
= devmask
& (1 << 1);
1732 unsigned long timeout
;
1734 /* if device 0 was found in ata_devchk, wait for its
1738 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1740 /* if device 1 was found in ata_devchk, wait for
1741 * register access, then wait for BSY to clear
1743 timeout
= jiffies
+ ATA_TMOUT_BOOT
;
1747 ap
->ops
->dev_select(ap
, 1);
1748 if (ap
->flags
& ATA_FLAG_MMIO
) {
1749 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
1750 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
1752 nsect
= inb(ioaddr
->nsect_addr
);
1753 lbal
= inb(ioaddr
->lbal_addr
);
1755 if ((nsect
== 1) && (lbal
== 1))
1757 if (time_after(jiffies
, timeout
)) {
1761 msleep(50); /* give drive a breather */
1764 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1766 /* is all this really necessary? */
1767 ap
->ops
->dev_select(ap
, 0);
1769 ap
->ops
->dev_select(ap
, 1);
1771 ap
->ops
->dev_select(ap
, 0);
1775 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1776 * @ap: Port to reset and probe
1778 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1779 * probe the bus. Not often used these days.
1782 * PCI/etc. bus probe sem.
1783 * Obtains host_set lock.
1787 static unsigned int ata_bus_edd(struct ata_port
*ap
)
1789 struct ata_taskfile tf
;
1790 unsigned long flags
;
1792 /* set up execute-device-diag (bus reset) taskfile */
1793 /* also, take interrupts to a known state (disabled) */
1794 DPRINTK("execute-device-diag\n");
1795 ata_tf_init(ap
, &tf
, 0);
1797 tf
.command
= ATA_CMD_EDD
;
1798 tf
.protocol
= ATA_PROT_NODATA
;
1801 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1802 ata_tf_to_host(ap
, &tf
);
1803 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1805 /* spec says at least 2ms. but who knows with those
1806 * crazy ATAPI devices...
1810 return ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1813 static unsigned int ata_bus_softreset(struct ata_port
*ap
,
1814 unsigned int devmask
)
1816 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1818 DPRINTK("ata%u: bus reset via SRST\n", ap
->id
);
1820 /* software reset. causes dev0 to be selected */
1821 if (ap
->flags
& ATA_FLAG_MMIO
) {
1822 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1823 udelay(20); /* FIXME: flush */
1824 writeb(ap
->ctl
| ATA_SRST
, (void __iomem
*) ioaddr
->ctl_addr
);
1825 udelay(20); /* FIXME: flush */
1826 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1828 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1830 outb(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
1832 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1835 /* spec mandates ">= 2ms" before checking status.
1836 * We wait 150ms, because that was the magic delay used for
1837 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1838 * between when the ATA command register is written, and then
1839 * status is checked. Because waiting for "a while" before
1840 * checking status is fine, post SRST, we perform this magic
1841 * delay here as well.
1845 ata_bus_post_reset(ap
, devmask
);
1851 * ata_bus_reset - reset host port and associated ATA channel
1852 * @ap: port to reset
1854 * This is typically the first time we actually start issuing
1855 * commands to the ATA channel. We wait for BSY to clear, then
1856 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1857 * result. Determine what devices, if any, are on the channel
1858 * by looking at the device 0/1 error register. Look at the signature
1859 * stored in each device's taskfile registers, to determine if
1860 * the device is ATA or ATAPI.
1863 * PCI/etc. bus probe sem.
1864 * Obtains host_set lock.
1867 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1870 void ata_bus_reset(struct ata_port
*ap
)
1872 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1873 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
1875 unsigned int dev0
, dev1
= 0, rc
= 0, devmask
= 0;
1877 DPRINTK("ENTER, host %u, port %u\n", ap
->id
, ap
->port_no
);
1879 /* determine if device 0/1 are present */
1880 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
1883 dev0
= ata_devchk(ap
, 0);
1885 dev1
= ata_devchk(ap
, 1);
1889 devmask
|= (1 << 0);
1891 devmask
|= (1 << 1);
1893 /* select device 0 again */
1894 ap
->ops
->dev_select(ap
, 0);
1896 /* issue bus reset */
1897 if (ap
->flags
& ATA_FLAG_SRST
)
1898 rc
= ata_bus_softreset(ap
, devmask
);
1899 else if ((ap
->flags
& ATA_FLAG_SATA_RESET
) == 0) {
1900 /* set up device control */
1901 if (ap
->flags
& ATA_FLAG_MMIO
)
1902 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1904 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1905 rc
= ata_bus_edd(ap
);
1912 * determine by signature whether we have ATA or ATAPI devices
1914 ap
->device
[0].class = ata_dev_try_classify(ap
, 0, &err
);
1915 if ((slave_possible
) && (err
!= 0x81))
1916 ap
->device
[1].class = ata_dev_try_classify(ap
, 1, &err
);
1918 /* re-enable interrupts */
1919 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
1922 /* is double-select really necessary? */
1923 if (ap
->device
[1].class != ATA_DEV_NONE
)
1924 ap
->ops
->dev_select(ap
, 1);
1925 if (ap
->device
[0].class != ATA_DEV_NONE
)
1926 ap
->ops
->dev_select(ap
, 0);
1928 /* if no devices were detected, disable this port */
1929 if ((ap
->device
[0].class == ATA_DEV_NONE
) &&
1930 (ap
->device
[1].class == ATA_DEV_NONE
))
1933 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
1934 /* set up device control for ATA_FLAG_SATA_RESET */
1935 if (ap
->flags
& ATA_FLAG_MMIO
)
1936 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1938 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1945 printk(KERN_ERR
"ata%u: disabling port\n", ap
->id
);
1946 ap
->ops
->port_disable(ap
);
1951 static int sata_phy_resume(struct ata_port
*ap
)
1953 unsigned long timeout
= jiffies
+ (HZ
* 5);
1956 scr_write_flush(ap
, SCR_CONTROL
, 0x300);
1958 /* Wait for phy to become ready, if necessary. */
1961 sstatus
= scr_read(ap
, SCR_STATUS
);
1962 if ((sstatus
& 0xf) != 1)
1964 } while (time_before(jiffies
, timeout
));
1970 * ata_std_probeinit - initialize probing
1971 * @ap: port to be probed
1973 * @ap is about to be probed. Initialize it. This function is
1974 * to be used as standard callback for ata_drive_probe_reset().
1976 * NOTE!!! Do not use this function as probeinit if a low level
1977 * driver implements only hardreset. Just pass NULL as probeinit
1978 * in that case. Using this function is probably okay but doing
1979 * so makes reset sequence different from the original
1980 * ->phy_reset implementation and Jeff nervous. :-P
1982 extern void ata_std_probeinit(struct ata_port
*ap
)
1984 if (ap
->flags
& ATA_FLAG_SATA
&& ap
->ops
->scr_read
) {
1985 sata_phy_resume(ap
);
1986 if (sata_dev_present(ap
))
1987 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1992 * ata_std_softreset - reset host port via ATA SRST
1993 * @ap: port to reset
1994 * @verbose: fail verbosely
1995 * @classes: resulting classes of attached devices
1997 * Reset host port using ATA SRST. This function is to be used
1998 * as standard callback for ata_drive_*_reset() functions.
2001 * Kernel thread context (may sleep)
2004 * 0 on success, -errno otherwise.
2006 int ata_std_softreset(struct ata_port
*ap
, int verbose
, unsigned int *classes
)
2008 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
2009 unsigned int devmask
= 0, err_mask
;
2014 if (ap
->ops
->scr_read
&& !sata_dev_present(ap
)) {
2015 classes
[0] = ATA_DEV_NONE
;
2019 /* determine if device 0/1 are present */
2020 if (ata_devchk(ap
, 0))
2021 devmask
|= (1 << 0);
2022 if (slave_possible
&& ata_devchk(ap
, 1))
2023 devmask
|= (1 << 1);
2025 /* select device 0 again */
2026 ap
->ops
->dev_select(ap
, 0);
2028 /* issue bus reset */
2029 DPRINTK("about to softreset, devmask=%x\n", devmask
);
2030 err_mask
= ata_bus_softreset(ap
, devmask
);
2033 printk(KERN_ERR
"ata%u: SRST failed (err_mask=0x%x)\n",
2036 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2041 /* determine by signature whether we have ATA or ATAPI devices */
2042 classes
[0] = ata_dev_try_classify(ap
, 0, &err
);
2043 if (slave_possible
&& err
!= 0x81)
2044 classes
[1] = ata_dev_try_classify(ap
, 1, &err
);
2047 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes
[0], classes
[1]);
2052 * sata_std_hardreset - reset host port via SATA phy reset
2053 * @ap: port to reset
2054 * @verbose: fail verbosely
2055 * @class: resulting class of attached device
2057 * SATA phy-reset host port using DET bits of SControl register.
2058 * This function is to be used as standard callback for
2059 * ata_drive_*_reset().
2062 * Kernel thread context (may sleep)
2065 * 0 on success, -errno otherwise.
2067 int sata_std_hardreset(struct ata_port
*ap
, int verbose
, unsigned int *class)
2071 /* Issue phy wake/reset */
2072 scr_write_flush(ap
, SCR_CONTROL
, 0x301);
2075 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2076 * 10.4.2 says at least 1 ms.
2080 /* Bring phy back */
2081 sata_phy_resume(ap
);
2083 /* TODO: phy layer with polling, timeouts, etc. */
2084 if (!sata_dev_present(ap
)) {
2085 *class = ATA_DEV_NONE
;
2086 DPRINTK("EXIT, link offline\n");
2090 if (ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
)) {
2092 printk(KERN_ERR
"ata%u: COMRESET failed "
2093 "(device not ready)\n", ap
->id
);
2095 DPRINTK("EXIT, device not ready\n");
2099 ap
->ops
->dev_select(ap
, 0); /* probably unnecessary */
2101 *class = ata_dev_try_classify(ap
, 0, NULL
);
2103 DPRINTK("EXIT, class=%u\n", *class);
2108 * ata_std_postreset - standard postreset callback
2109 * @ap: the target ata_port
2110 * @classes: classes of attached devices
2112 * This function is invoked after a successful reset. Note that
2113 * the device might have been reset more than once using
2114 * different reset methods before postreset is invoked.
2115 * postreset is also reponsible for setting cable type.
2117 * This function is to be used as standard callback for
2118 * ata_drive_*_reset().
2121 * Kernel thread context (may sleep)
2123 void ata_std_postreset(struct ata_port
*ap
, unsigned int *classes
)
2127 /* set cable type */
2128 if (ap
->cbl
== ATA_CBL_NONE
&& ap
->flags
& ATA_FLAG_SATA
)
2129 ap
->cbl
= ATA_CBL_SATA
;
2131 /* print link status */
2132 if (ap
->cbl
== ATA_CBL_SATA
)
2133 sata_print_link_status(ap
);
2135 /* re-enable interrupts */
2136 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
2139 /* is double-select really necessary? */
2140 if (classes
[0] != ATA_DEV_NONE
)
2141 ap
->ops
->dev_select(ap
, 1);
2142 if (classes
[1] != ATA_DEV_NONE
)
2143 ap
->ops
->dev_select(ap
, 0);
2145 /* bail out if no device is present */
2146 if (classes
[0] == ATA_DEV_NONE
&& classes
[1] == ATA_DEV_NONE
) {
2147 DPRINTK("EXIT, no device\n");
2151 /* set up device control */
2152 if (ap
->ioaddr
.ctl_addr
) {
2153 if (ap
->flags
& ATA_FLAG_MMIO
)
2154 writeb(ap
->ctl
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
2156 outb(ap
->ctl
, ap
->ioaddr
.ctl_addr
);
2163 * ata_std_probe_reset - standard probe reset method
2164 * @ap: prot to perform probe-reset
2165 * @classes: resulting classes of attached devices
2167 * The stock off-the-shelf ->probe_reset method.
2170 * Kernel thread context (may sleep)
2173 * 0 on success, -errno otherwise.
2175 int ata_std_probe_reset(struct ata_port
*ap
, unsigned int *classes
)
2177 ata_reset_fn_t hardreset
;
2180 if (ap
->flags
& ATA_FLAG_SATA
&& ap
->ops
->scr_read
)
2181 hardreset
= sata_std_hardreset
;
2183 return ata_drive_probe_reset(ap
, ata_std_probeinit
,
2184 ata_std_softreset
, hardreset
,
2185 ata_std_postreset
, classes
);
2188 static int do_probe_reset(struct ata_port
*ap
, ata_reset_fn_t reset
,
2189 ata_postreset_fn_t postreset
,
2190 unsigned int *classes
)
2194 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
2195 classes
[i
] = ATA_DEV_UNKNOWN
;
2197 rc
= reset(ap
, 0, classes
);
2201 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2202 * is complete and convert all ATA_DEV_UNKNOWN to
2205 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
2206 if (classes
[i
] != ATA_DEV_UNKNOWN
)
2209 if (i
< ATA_MAX_DEVICES
)
2210 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
2211 if (classes
[i
] == ATA_DEV_UNKNOWN
)
2212 classes
[i
] = ATA_DEV_NONE
;
2215 postreset(ap
, classes
);
2217 return classes
[0] != ATA_DEV_UNKNOWN
? 0 : -ENODEV
;
2221 * ata_drive_probe_reset - Perform probe reset with given methods
2222 * @ap: port to reset
2223 * @probeinit: probeinit method (can be NULL)
2224 * @softreset: softreset method (can be NULL)
2225 * @hardreset: hardreset method (can be NULL)
2226 * @postreset: postreset method (can be NULL)
2227 * @classes: resulting classes of attached devices
2229 * Reset the specified port and classify attached devices using
2230 * given methods. This function prefers softreset but tries all
2231 * possible reset sequences to reset and classify devices. This
2232 * function is intended to be used for constructing ->probe_reset
2233 * callback by low level drivers.
2235 * Reset methods should follow the following rules.
2237 * - Return 0 on sucess, -errno on failure.
2238 * - If classification is supported, fill classes[] with
2239 * recognized class codes.
2240 * - If classification is not supported, leave classes[] alone.
2241 * - If verbose is non-zero, print error message on failure;
2242 * otherwise, shut up.
2245 * Kernel thread context (may sleep)
2248 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2249 * if classification fails, and any error code from reset
2252 int ata_drive_probe_reset(struct ata_port
*ap
, ata_probeinit_fn_t probeinit
,
2253 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
2254 ata_postreset_fn_t postreset
, unsigned int *classes
)
2262 rc
= do_probe_reset(ap
, softreset
, postreset
, classes
);
2270 rc
= do_probe_reset(ap
, hardreset
, postreset
, classes
);
2271 if (rc
== 0 || rc
!= -ENODEV
)
2275 rc
= do_probe_reset(ap
, softreset
, postreset
, classes
);
2280 static void ata_pr_blacklisted(const struct ata_port
*ap
,
2281 const struct ata_device
*dev
)
2283 printk(KERN_WARNING
"ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2284 ap
->id
, dev
->devno
);
2287 static const char * const ata_dma_blacklist
[] = {
2306 "Toshiba CD-ROM XM-6202B",
2307 "TOSHIBA CD-ROM XM-1702BC",
2309 "E-IDE CD-ROM CR-840",
2312 "SAMSUNG CD-ROM SC-148C",
2313 "SAMSUNG CD-ROM SC",
2315 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2319 static int ata_dma_blacklisted(const struct ata_device
*dev
)
2321 unsigned char model_num
[41];
2324 ata_id_c_string(dev
->id
, model_num
, ATA_ID_PROD_OFS
, sizeof(model_num
));
2326 for (i
= 0; i
< ARRAY_SIZE(ata_dma_blacklist
); i
++)
2327 if (!strcmp(ata_dma_blacklist
[i
], model_num
))
2333 static unsigned int ata_get_mode_mask(const struct ata_port
*ap
, int shift
)
2335 const struct ata_device
*master
, *slave
;
2338 master
= &ap
->device
[0];
2339 slave
= &ap
->device
[1];
2341 WARN_ON(!ata_dev_present(master
) && !ata_dev_present(slave
));
2343 if (shift
== ATA_SHIFT_UDMA
) {
2344 mask
= ap
->udma_mask
;
2345 if (ata_dev_present(master
)) {
2346 mask
&= (master
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2347 if (ata_dma_blacklisted(master
)) {
2349 ata_pr_blacklisted(ap
, master
);
2352 if (ata_dev_present(slave
)) {
2353 mask
&= (slave
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2354 if (ata_dma_blacklisted(slave
)) {
2356 ata_pr_blacklisted(ap
, slave
);
2360 else if (shift
== ATA_SHIFT_MWDMA
) {
2361 mask
= ap
->mwdma_mask
;
2362 if (ata_dev_present(master
)) {
2363 mask
&= (master
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2364 if (ata_dma_blacklisted(master
)) {
2366 ata_pr_blacklisted(ap
, master
);
2369 if (ata_dev_present(slave
)) {
2370 mask
&= (slave
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2371 if (ata_dma_blacklisted(slave
)) {
2373 ata_pr_blacklisted(ap
, slave
);
2377 else if (shift
== ATA_SHIFT_PIO
) {
2378 mask
= ap
->pio_mask
;
2379 if (ata_dev_present(master
)) {
2380 /* spec doesn't return explicit support for
2381 * PIO0-2, so we fake it
2383 u16 tmp_mode
= master
->id
[ATA_ID_PIO_MODES
] & 0x03;
2388 if (ata_dev_present(slave
)) {
2389 /* spec doesn't return explicit support for
2390 * PIO0-2, so we fake it
2392 u16 tmp_mode
= slave
->id
[ATA_ID_PIO_MODES
] & 0x03;
2399 mask
= 0xffffffff; /* shut up compiler warning */
2406 /* find greatest bit */
2407 static int fgb(u32 bitmap
)
2412 for (i
= 0; i
< 32; i
++)
2413 if (bitmap
& (1 << i
))
2420 * ata_choose_xfer_mode - attempt to find best transfer mode
2421 * @ap: Port for which an xfer mode will be selected
2422 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2423 * @xfer_shift_out: (output) bit shift that selects this mode
2425 * Based on host and device capabilities, determine the
2426 * maximum transfer mode that is amenable to all.
2429 * PCI/etc. bus probe sem.
2432 * Zero on success, negative on error.
2435 static int ata_choose_xfer_mode(const struct ata_port
*ap
,
2437 unsigned int *xfer_shift_out
)
2439 unsigned int mask
, shift
;
2442 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++) {
2443 shift
= xfer_mode_classes
[i
].shift
;
2444 mask
= ata_get_mode_mask(ap
, shift
);
2448 *xfer_mode_out
= xfer_mode_classes
[i
].base
+ x
;
2449 *xfer_shift_out
= shift
;
2458 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2459 * @ap: Port associated with device @dev
2460 * @dev: Device to which command will be sent
2462 * Issue SET FEATURES - XFER MODE command to device @dev
2466 * PCI/etc. bus probe sem.
2469 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
)
2471 struct ata_taskfile tf
;
2473 /* set up set-features taskfile */
2474 DPRINTK("set features - xfer mode\n");
2476 ata_tf_init(ap
, &tf
, dev
->devno
);
2477 tf
.command
= ATA_CMD_SET_FEATURES
;
2478 tf
.feature
= SETFEATURES_XFER
;
2479 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2480 tf
.protocol
= ATA_PROT_NODATA
;
2481 tf
.nsect
= dev
->xfer_mode
;
2483 if (ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0)) {
2484 printk(KERN_ERR
"ata%u: failed to set xfermode, disabled\n",
2486 ata_port_disable(ap
);
2493 * ata_dev_reread_id - Reread the device identify device info
2494 * @ap: port where the device is
2495 * @dev: device to reread the identify device info
2500 static void ata_dev_reread_id(struct ata_port
*ap
, struct ata_device
*dev
)
2502 struct ata_taskfile tf
;
2504 ata_tf_init(ap
, &tf
, dev
->devno
);
2506 if (dev
->class == ATA_DEV_ATA
) {
2507 tf
.command
= ATA_CMD_ID_ATA
;
2508 DPRINTK("do ATA identify\n");
2510 tf
.command
= ATA_CMD_ID_ATAPI
;
2511 DPRINTK("do ATAPI identify\n");
2514 tf
.flags
|= ATA_TFLAG_DEVICE
;
2515 tf
.protocol
= ATA_PROT_PIO
;
2517 if (ata_exec_internal(ap
, dev
, &tf
, DMA_FROM_DEVICE
,
2518 dev
->id
, sizeof(dev
->id
)))
2521 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
2523 ata_dump_id(dev
->id
);
2529 printk(KERN_ERR
"ata%u: failed to reread ID, disabled\n", ap
->id
);
2530 ata_port_disable(ap
);
2534 * ata_dev_init_params - Issue INIT DEV PARAMS command
2535 * @ap: Port associated with device @dev
2536 * @dev: Device to which command will be sent
2539 * Kernel thread context (may sleep)
2542 * 0 on success, AC_ERR_* mask otherwise.
2545 static unsigned int ata_dev_init_params(struct ata_port
*ap
,
2546 struct ata_device
*dev
)
2548 struct ata_taskfile tf
;
2549 unsigned int err_mask
;
2550 u16 sectors
= dev
->id
[6];
2551 u16 heads
= dev
->id
[3];
2553 /* Number of sectors per track 1-255. Number of heads 1-16 */
2554 if (sectors
< 1 || sectors
> 255 || heads
< 1 || heads
> 16)
2557 /* set up init dev params taskfile */
2558 DPRINTK("init dev params \n");
2560 ata_tf_init(ap
, &tf
, dev
->devno
);
2561 tf
.command
= ATA_CMD_INIT_DEV_PARAMS
;
2562 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2563 tf
.protocol
= ATA_PROT_NODATA
;
2565 tf
.device
|= (heads
- 1) & 0x0f; /* max head = num. of heads - 1 */
2567 err_mask
= ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0);
2569 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
2574 * ata_sg_clean - Unmap DMA memory associated with command
2575 * @qc: Command containing DMA memory to be released
2577 * Unmap all mapped DMA memory associated with this command.
2580 * spin_lock_irqsave(host_set lock)
2583 static void ata_sg_clean(struct ata_queued_cmd
*qc
)
2585 struct ata_port
*ap
= qc
->ap
;
2586 struct scatterlist
*sg
= qc
->__sg
;
2587 int dir
= qc
->dma_dir
;
2588 void *pad_buf
= NULL
;
2590 WARN_ON(!(qc
->flags
& ATA_QCFLAG_DMAMAP
));
2591 WARN_ON(sg
== NULL
);
2593 if (qc
->flags
& ATA_QCFLAG_SINGLE
)
2594 WARN_ON(qc
->n_elem
!= 1);
2596 VPRINTK("unmapping %u sg elements\n", qc
->n_elem
);
2598 /* if we padded the buffer out to 32-bit bound, and data
2599 * xfer direction is from-device, we must copy from the
2600 * pad buffer back into the supplied buffer
2602 if (qc
->pad_len
&& !(qc
->tf
.flags
& ATA_TFLAG_WRITE
))
2603 pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2605 if (qc
->flags
& ATA_QCFLAG_SG
) {
2607 dma_unmap_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2608 /* restore last sg */
2609 sg
[qc
->orig_n_elem
- 1].length
+= qc
->pad_len
;
2611 struct scatterlist
*psg
= &qc
->pad_sgent
;
2612 void *addr
= kmap_atomic(psg
->page
, KM_IRQ0
);
2613 memcpy(addr
+ psg
->offset
, pad_buf
, qc
->pad_len
);
2614 kunmap_atomic(addr
, KM_IRQ0
);
2617 if (sg_dma_len(&sg
[0]) > 0)
2618 dma_unmap_single(ap
->host_set
->dev
,
2619 sg_dma_address(&sg
[0]), sg_dma_len(&sg
[0]),
2622 sg
->length
+= qc
->pad_len
;
2624 memcpy(qc
->buf_virt
+ sg
->length
- qc
->pad_len
,
2625 pad_buf
, qc
->pad_len
);
2628 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
2633 * ata_fill_sg - Fill PCI IDE PRD table
2634 * @qc: Metadata associated with taskfile to be transferred
2636 * Fill PCI IDE PRD (scatter-gather) table with segments
2637 * associated with the current disk command.
2640 * spin_lock_irqsave(host_set lock)
2643 static void ata_fill_sg(struct ata_queued_cmd
*qc
)
2645 struct ata_port
*ap
= qc
->ap
;
2646 struct scatterlist
*sg
;
2649 WARN_ON(qc
->__sg
== NULL
);
2650 WARN_ON(qc
->n_elem
== 0);
2653 ata_for_each_sg(sg
, qc
) {
2657 /* determine if physical DMA addr spans 64K boundary.
2658 * Note h/w doesn't support 64-bit, so we unconditionally
2659 * truncate dma_addr_t to u32.
2661 addr
= (u32
) sg_dma_address(sg
);
2662 sg_len
= sg_dma_len(sg
);
2665 offset
= addr
& 0xffff;
2667 if ((offset
+ sg_len
) > 0x10000)
2668 len
= 0x10000 - offset
;
2670 ap
->prd
[idx
].addr
= cpu_to_le32(addr
);
2671 ap
->prd
[idx
].flags_len
= cpu_to_le32(len
& 0xffff);
2672 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx
, addr
, len
);
2681 ap
->prd
[idx
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
2684 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2685 * @qc: Metadata associated with taskfile to check
2687 * Allow low-level driver to filter ATA PACKET commands, returning
2688 * a status indicating whether or not it is OK to use DMA for the
2689 * supplied PACKET command.
2692 * spin_lock_irqsave(host_set lock)
2694 * RETURNS: 0 when ATAPI DMA can be used
2697 int ata_check_atapi_dma(struct ata_queued_cmd
*qc
)
2699 struct ata_port
*ap
= qc
->ap
;
2700 int rc
= 0; /* Assume ATAPI DMA is OK by default */
2702 if (ap
->ops
->check_atapi_dma
)
2703 rc
= ap
->ops
->check_atapi_dma(qc
);
2708 * ata_qc_prep - Prepare taskfile for submission
2709 * @qc: Metadata associated with taskfile to be prepared
2711 * Prepare ATA taskfile for submission.
2714 * spin_lock_irqsave(host_set lock)
2716 void ata_qc_prep(struct ata_queued_cmd
*qc
)
2718 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
2725 * ata_sg_init_one - Associate command with memory buffer
2726 * @qc: Command to be associated
2727 * @buf: Memory buffer
2728 * @buflen: Length of memory buffer, in bytes.
2730 * Initialize the data-related elements of queued_cmd @qc
2731 * to point to a single memory buffer, @buf of byte length @buflen.
2734 * spin_lock_irqsave(host_set lock)
2737 void ata_sg_init_one(struct ata_queued_cmd
*qc
, void *buf
, unsigned int buflen
)
2739 struct scatterlist
*sg
;
2741 qc
->flags
|= ATA_QCFLAG_SINGLE
;
2743 memset(&qc
->sgent
, 0, sizeof(qc
->sgent
));
2744 qc
->__sg
= &qc
->sgent
;
2746 qc
->orig_n_elem
= 1;
2750 sg_init_one(sg
, buf
, buflen
);
2754 * ata_sg_init - Associate command with scatter-gather table.
2755 * @qc: Command to be associated
2756 * @sg: Scatter-gather table.
2757 * @n_elem: Number of elements in s/g table.
2759 * Initialize the data-related elements of queued_cmd @qc
2760 * to point to a scatter-gather table @sg, containing @n_elem
2764 * spin_lock_irqsave(host_set lock)
2767 void ata_sg_init(struct ata_queued_cmd
*qc
, struct scatterlist
*sg
,
2768 unsigned int n_elem
)
2770 qc
->flags
|= ATA_QCFLAG_SG
;
2772 qc
->n_elem
= n_elem
;
2773 qc
->orig_n_elem
= n_elem
;
2777 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2778 * @qc: Command with memory buffer to be mapped.
2780 * DMA-map the memory buffer associated with queued_cmd @qc.
2783 * spin_lock_irqsave(host_set lock)
2786 * Zero on success, negative on error.
2789 static int ata_sg_setup_one(struct ata_queued_cmd
*qc
)
2791 struct ata_port
*ap
= qc
->ap
;
2792 int dir
= qc
->dma_dir
;
2793 struct scatterlist
*sg
= qc
->__sg
;
2794 dma_addr_t dma_address
;
2796 /* we must lengthen transfers to end on a 32-bit boundary */
2797 qc
->pad_len
= sg
->length
& 3;
2799 void *pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2800 struct scatterlist
*psg
= &qc
->pad_sgent
;
2802 WARN_ON(qc
->dev
->class != ATA_DEV_ATAPI
);
2804 memset(pad_buf
, 0, ATA_DMA_PAD_SZ
);
2806 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
)
2807 memcpy(pad_buf
, qc
->buf_virt
+ sg
->length
- qc
->pad_len
,
2810 sg_dma_address(psg
) = ap
->pad_dma
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2811 sg_dma_len(psg
) = ATA_DMA_PAD_SZ
;
2813 sg
->length
-= qc
->pad_len
;
2815 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2816 sg
->length
, qc
->pad_len
);
2820 sg_dma_address(sg
) = 0;
2824 dma_address
= dma_map_single(ap
->host_set
->dev
, qc
->buf_virt
,
2826 if (dma_mapping_error(dma_address
)) {
2828 sg
->length
+= qc
->pad_len
;
2832 sg_dma_address(sg
) = dma_address
;
2834 sg_dma_len(sg
) = sg
->length
;
2836 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg
),
2837 qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2843 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2844 * @qc: Command with scatter-gather table to be mapped.
2846 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2849 * spin_lock_irqsave(host_set lock)
2852 * Zero on success, negative on error.
2856 static int ata_sg_setup(struct ata_queued_cmd
*qc
)
2858 struct ata_port
*ap
= qc
->ap
;
2859 struct scatterlist
*sg
= qc
->__sg
;
2860 struct scatterlist
*lsg
= &sg
[qc
->n_elem
- 1];
2861 int n_elem
, pre_n_elem
, dir
, trim_sg
= 0;
2863 VPRINTK("ENTER, ata%u\n", ap
->id
);
2864 WARN_ON(!(qc
->flags
& ATA_QCFLAG_SG
));
2866 /* we must lengthen transfers to end on a 32-bit boundary */
2867 qc
->pad_len
= lsg
->length
& 3;
2869 void *pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2870 struct scatterlist
*psg
= &qc
->pad_sgent
;
2871 unsigned int offset
;
2873 WARN_ON(qc
->dev
->class != ATA_DEV_ATAPI
);
2875 memset(pad_buf
, 0, ATA_DMA_PAD_SZ
);
2878 * psg->page/offset are used to copy to-be-written
2879 * data in this function or read data in ata_sg_clean.
2881 offset
= lsg
->offset
+ lsg
->length
- qc
->pad_len
;
2882 psg
->page
= nth_page(lsg
->page
, offset
>> PAGE_SHIFT
);
2883 psg
->offset
= offset_in_page(offset
);
2885 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
) {
2886 void *addr
= kmap_atomic(psg
->page
, KM_IRQ0
);
2887 memcpy(pad_buf
, addr
+ psg
->offset
, qc
->pad_len
);
2888 kunmap_atomic(addr
, KM_IRQ0
);
2891 sg_dma_address(psg
) = ap
->pad_dma
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2892 sg_dma_len(psg
) = ATA_DMA_PAD_SZ
;
2894 lsg
->length
-= qc
->pad_len
;
2895 if (lsg
->length
== 0)
2898 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2899 qc
->n_elem
- 1, lsg
->length
, qc
->pad_len
);
2902 pre_n_elem
= qc
->n_elem
;
2903 if (trim_sg
&& pre_n_elem
)
2912 n_elem
= dma_map_sg(ap
->host_set
->dev
, sg
, pre_n_elem
, dir
);
2914 /* restore last sg */
2915 lsg
->length
+= qc
->pad_len
;
2919 DPRINTK("%d sg elements mapped\n", n_elem
);
2922 qc
->n_elem
= n_elem
;
2928 * ata_poll_qc_complete - turn irq back on and finish qc
2929 * @qc: Command to complete
2930 * @err_mask: ATA status register content
2933 * None. (grabs host lock)
2936 void ata_poll_qc_complete(struct ata_queued_cmd
*qc
)
2938 struct ata_port
*ap
= qc
->ap
;
2939 unsigned long flags
;
2941 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2942 ap
->flags
&= ~ATA_FLAG_NOINTR
;
2944 ata_qc_complete(qc
);
2945 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2949 * ata_pio_poll - poll using PIO, depending on current state
2950 * @ap: the target ata_port
2953 * None. (executing in kernel thread context)
2956 * timeout value to use
2959 static unsigned long ata_pio_poll(struct ata_port
*ap
)
2961 struct ata_queued_cmd
*qc
;
2963 unsigned int poll_state
= HSM_ST_UNKNOWN
;
2964 unsigned int reg_state
= HSM_ST_UNKNOWN
;
2966 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2967 WARN_ON(qc
== NULL
);
2969 switch (ap
->hsm_task_state
) {
2972 poll_state
= HSM_ST_POLL
;
2976 case HSM_ST_LAST_POLL
:
2977 poll_state
= HSM_ST_LAST_POLL
;
2978 reg_state
= HSM_ST_LAST
;
2985 status
= ata_chk_status(ap
);
2986 if (status
& ATA_BUSY
) {
2987 if (time_after(jiffies
, ap
->pio_task_timeout
)) {
2988 qc
->err_mask
|= AC_ERR_TIMEOUT
;
2989 ap
->hsm_task_state
= HSM_ST_TMOUT
;
2992 ap
->hsm_task_state
= poll_state
;
2993 return ATA_SHORT_PAUSE
;
2996 ap
->hsm_task_state
= reg_state
;
3001 * ata_pio_complete - check if drive is busy or idle
3002 * @ap: the target ata_port
3005 * None. (executing in kernel thread context)
3008 * Non-zero if qc completed, zero otherwise.
3011 static int ata_pio_complete (struct ata_port
*ap
)
3013 struct ata_queued_cmd
*qc
;
3017 * This is purely heuristic. This is a fast path. Sometimes when
3018 * we enter, BSY will be cleared in a chk-status or two. If not,
3019 * the drive is probably seeking or something. Snooze for a couple
3020 * msecs, then chk-status again. If still busy, fall back to
3021 * HSM_ST_POLL state.
3023 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
, 10);
3024 if (drv_stat
& ATA_BUSY
) {
3026 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
, 10);
3027 if (drv_stat
& ATA_BUSY
) {
3028 ap
->hsm_task_state
= HSM_ST_LAST_POLL
;
3029 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
3034 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3035 WARN_ON(qc
== NULL
);
3037 drv_stat
= ata_wait_idle(ap
);
3038 if (!ata_ok(drv_stat
)) {
3039 qc
->err_mask
|= __ac_err_mask(drv_stat
);
3040 ap
->hsm_task_state
= HSM_ST_ERR
;
3044 ap
->hsm_task_state
= HSM_ST_IDLE
;
3046 WARN_ON(qc
->err_mask
);
3047 ata_poll_qc_complete(qc
);
3049 /* another command may start at this point */
3056 * swap_buf_le16 - swap halves of 16-bit words in place
3057 * @buf: Buffer to swap
3058 * @buf_words: Number of 16-bit words in buffer.
3060 * Swap halves of 16-bit words if needed to convert from
3061 * little-endian byte order to native cpu byte order, or
3065 * Inherited from caller.
3067 void swap_buf_le16(u16
*buf
, unsigned int buf_words
)
3072 for (i
= 0; i
< buf_words
; i
++)
3073 buf
[i
] = le16_to_cpu(buf
[i
]);
3074 #endif /* __BIG_ENDIAN */
3078 * ata_mmio_data_xfer - Transfer data by MMIO
3079 * @ap: port to read/write
3081 * @buflen: buffer length
3082 * @write_data: read/write
3084 * Transfer data from/to the device data register by MMIO.
3087 * Inherited from caller.
3090 static void ata_mmio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3091 unsigned int buflen
, int write_data
)
3094 unsigned int words
= buflen
>> 1;
3095 u16
*buf16
= (u16
*) buf
;
3096 void __iomem
*mmio
= (void __iomem
*)ap
->ioaddr
.data_addr
;
3098 /* Transfer multiple of 2 bytes */
3100 for (i
= 0; i
< words
; i
++)
3101 writew(le16_to_cpu(buf16
[i
]), mmio
);
3103 for (i
= 0; i
< words
; i
++)
3104 buf16
[i
] = cpu_to_le16(readw(mmio
));
3107 /* Transfer trailing 1 byte, if any. */
3108 if (unlikely(buflen
& 0x01)) {
3109 u16 align_buf
[1] = { 0 };
3110 unsigned char *trailing_buf
= buf
+ buflen
- 1;
3113 memcpy(align_buf
, trailing_buf
, 1);
3114 writew(le16_to_cpu(align_buf
[0]), mmio
);
3116 align_buf
[0] = cpu_to_le16(readw(mmio
));
3117 memcpy(trailing_buf
, align_buf
, 1);
3123 * ata_pio_data_xfer - Transfer data by PIO
3124 * @ap: port to read/write
3126 * @buflen: buffer length
3127 * @write_data: read/write
3129 * Transfer data from/to the device data register by PIO.
3132 * Inherited from caller.
3135 static void ata_pio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3136 unsigned int buflen
, int write_data
)
3138 unsigned int words
= buflen
>> 1;
3140 /* Transfer multiple of 2 bytes */
3142 outsw(ap
->ioaddr
.data_addr
, buf
, words
);
3144 insw(ap
->ioaddr
.data_addr
, buf
, words
);
3146 /* Transfer trailing 1 byte, if any. */
3147 if (unlikely(buflen
& 0x01)) {
3148 u16 align_buf
[1] = { 0 };
3149 unsigned char *trailing_buf
= buf
+ buflen
- 1;
3152 memcpy(align_buf
, trailing_buf
, 1);
3153 outw(le16_to_cpu(align_buf
[0]), ap
->ioaddr
.data_addr
);
3155 align_buf
[0] = cpu_to_le16(inw(ap
->ioaddr
.data_addr
));
3156 memcpy(trailing_buf
, align_buf
, 1);
3162 * ata_data_xfer - Transfer data from/to the data register.
3163 * @ap: port to read/write
3165 * @buflen: buffer length
3166 * @do_write: read/write
3168 * Transfer data from/to the device data register.
3171 * Inherited from caller.
3174 static void ata_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3175 unsigned int buflen
, int do_write
)
3177 /* Make the crap hardware pay the costs not the good stuff */
3178 if (unlikely(ap
->flags
& ATA_FLAG_IRQ_MASK
)) {
3179 unsigned long flags
;
3180 local_irq_save(flags
);
3181 if (ap
->flags
& ATA_FLAG_MMIO
)
3182 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
3184 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
3185 local_irq_restore(flags
);
3187 if (ap
->flags
& ATA_FLAG_MMIO
)
3188 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
3190 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
3195 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3196 * @qc: Command on going
3198 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3201 * Inherited from caller.
3204 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
3206 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3207 struct scatterlist
*sg
= qc
->__sg
;
3208 struct ata_port
*ap
= qc
->ap
;
3210 unsigned int offset
;
3213 if (qc
->cursect
== (qc
->nsect
- 1))
3214 ap
->hsm_task_state
= HSM_ST_LAST
;
3216 page
= sg
[qc
->cursg
].page
;
3217 offset
= sg
[qc
->cursg
].offset
+ qc
->cursg_ofs
* ATA_SECT_SIZE
;
3219 /* get the current page and offset */
3220 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
3221 offset
%= PAGE_SIZE
;
3223 buf
= kmap(page
) + offset
;
3228 if ((qc
->cursg_ofs
* ATA_SECT_SIZE
) == (&sg
[qc
->cursg
])->length
) {
3233 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
3235 /* do the actual data transfer */
3236 do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3237 ata_data_xfer(ap
, buf
, ATA_SECT_SIZE
, do_write
);
3243 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3244 * @qc: Command on going
3245 * @bytes: number of bytes
3247 * Transfer Transfer data from/to the ATAPI device.
3250 * Inherited from caller.
3254 static void __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
3256 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3257 struct scatterlist
*sg
= qc
->__sg
;
3258 struct ata_port
*ap
= qc
->ap
;
3261 unsigned int offset
, count
;
3263 if (qc
->curbytes
+ bytes
>= qc
->nbytes
)
3264 ap
->hsm_task_state
= HSM_ST_LAST
;
3267 if (unlikely(qc
->cursg
>= qc
->n_elem
)) {
3269 * The end of qc->sg is reached and the device expects
3270 * more data to transfer. In order not to overrun qc->sg
3271 * and fulfill length specified in the byte count register,
3272 * - for read case, discard trailing data from the device
3273 * - for write case, padding zero data to the device
3275 u16 pad_buf
[1] = { 0 };
3276 unsigned int words
= bytes
>> 1;
3279 if (words
) /* warning if bytes > 1 */
3280 printk(KERN_WARNING
"ata%u: %u bytes trailing data\n",
3283 for (i
= 0; i
< words
; i
++)
3284 ata_data_xfer(ap
, (unsigned char*)pad_buf
, 2, do_write
);
3286 ap
->hsm_task_state
= HSM_ST_LAST
;
3290 sg
= &qc
->__sg
[qc
->cursg
];
3293 offset
= sg
->offset
+ qc
->cursg_ofs
;
3295 /* get the current page and offset */
3296 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
3297 offset
%= PAGE_SIZE
;
3299 /* don't overrun current sg */
3300 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
3302 /* don't cross page boundaries */
3303 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
3305 buf
= kmap(page
) + offset
;
3308 qc
->curbytes
+= count
;
3309 qc
->cursg_ofs
+= count
;
3311 if (qc
->cursg_ofs
== sg
->length
) {
3316 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
3318 /* do the actual data transfer */
3319 ata_data_xfer(ap
, buf
, count
, do_write
);
3328 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3329 * @qc: Command on going
3331 * Transfer Transfer data from/to the ATAPI device.
3334 * Inherited from caller.
3337 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
3339 struct ata_port
*ap
= qc
->ap
;
3340 struct ata_device
*dev
= qc
->dev
;
3341 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
3342 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
3344 ap
->ops
->tf_read(ap
, &qc
->tf
);
3345 ireason
= qc
->tf
.nsect
;
3346 bc_lo
= qc
->tf
.lbam
;
3347 bc_hi
= qc
->tf
.lbah
;
3348 bytes
= (bc_hi
<< 8) | bc_lo
;
3350 /* shall be cleared to zero, indicating xfer of data */
3351 if (ireason
& (1 << 0))
3354 /* make sure transfer direction matches expected */
3355 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
3356 if (do_write
!= i_write
)
3359 __atapi_pio_bytes(qc
, bytes
);
3364 printk(KERN_INFO
"ata%u: dev %u: ATAPI check failed\n",
3365 ap
->id
, dev
->devno
);
3366 qc
->err_mask
|= AC_ERR_HSM
;
3367 ap
->hsm_task_state
= HSM_ST_ERR
;
3371 * ata_pio_block - start PIO on a block
3372 * @ap: the target ata_port
3375 * None. (executing in kernel thread context)
3378 static void ata_pio_block(struct ata_port
*ap
)
3380 struct ata_queued_cmd
*qc
;
3384 * This is purely heuristic. This is a fast path.
3385 * Sometimes when we enter, BSY will be cleared in
3386 * a chk-status or two. If not, the drive is probably seeking
3387 * or something. Snooze for a couple msecs, then
3388 * chk-status again. If still busy, fall back to
3389 * HSM_ST_POLL state.
3391 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
3392 if (status
& ATA_BUSY
) {
3394 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
3395 if (status
& ATA_BUSY
) {
3396 ap
->hsm_task_state
= HSM_ST_POLL
;
3397 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
3402 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3403 WARN_ON(qc
== NULL
);
3406 if (status
& (ATA_ERR
| ATA_DF
)) {
3407 qc
->err_mask
|= AC_ERR_DEV
;
3408 ap
->hsm_task_state
= HSM_ST_ERR
;
3412 /* transfer data if any */
3413 if (is_atapi_taskfile(&qc
->tf
)) {
3414 /* DRQ=0 means no more data to transfer */
3415 if ((status
& ATA_DRQ
) == 0) {
3416 ap
->hsm_task_state
= HSM_ST_LAST
;
3420 atapi_pio_bytes(qc
);
3422 /* handle BSY=0, DRQ=0 as error */
3423 if ((status
& ATA_DRQ
) == 0) {
3424 qc
->err_mask
|= AC_ERR_HSM
;
3425 ap
->hsm_task_state
= HSM_ST_ERR
;
3433 static void ata_pio_error(struct ata_port
*ap
)
3435 struct ata_queued_cmd
*qc
;
3437 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3438 WARN_ON(qc
== NULL
);
3440 if (qc
->tf
.command
!= ATA_CMD_PACKET
)
3441 printk(KERN_WARNING
"ata%u: PIO error\n", ap
->id
);
3443 /* make sure qc->err_mask is available to
3444 * know what's wrong and recover
3446 WARN_ON(qc
->err_mask
== 0);
3448 ap
->hsm_task_state
= HSM_ST_IDLE
;
3450 ata_poll_qc_complete(qc
);
3453 static void ata_pio_task(void *_data
)
3455 struct ata_port
*ap
= _data
;
3456 unsigned long timeout
;
3463 switch (ap
->hsm_task_state
) {
3472 qc_completed
= ata_pio_complete(ap
);
3476 case HSM_ST_LAST_POLL
:
3477 timeout
= ata_pio_poll(ap
);
3487 ata_queue_delayed_pio_task(ap
, timeout
);
3488 else if (!qc_completed
)
3493 * ata_qc_timeout - Handle timeout of queued command
3494 * @qc: Command that timed out
3496 * Some part of the kernel (currently, only the SCSI layer)
3497 * has noticed that the active command on port @ap has not
3498 * completed after a specified length of time. Handle this
3499 * condition by disabling DMA (if necessary) and completing
3500 * transactions, with error if necessary.
3502 * This also handles the case of the "lost interrupt", where
3503 * for some reason (possibly hardware bug, possibly driver bug)
3504 * an interrupt was not delivered to the driver, even though the
3505 * transaction completed successfully.
3508 * Inherited from SCSI layer (none, can sleep)
3511 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
3513 struct ata_port
*ap
= qc
->ap
;
3514 struct ata_host_set
*host_set
= ap
->host_set
;
3515 u8 host_stat
= 0, drv_stat
;
3516 unsigned long flags
;
3520 ata_flush_pio_tasks(ap
);
3521 ap
->hsm_task_state
= HSM_ST_IDLE
;
3523 spin_lock_irqsave(&host_set
->lock
, flags
);
3525 switch (qc
->tf
.protocol
) {
3528 case ATA_PROT_ATAPI_DMA
:
3529 host_stat
= ap
->ops
->bmdma_status(ap
);
3531 /* before we do anything else, clear DMA-Start bit */
3532 ap
->ops
->bmdma_stop(qc
);
3538 drv_stat
= ata_chk_status(ap
);
3540 /* ack bmdma irq events */
3541 ap
->ops
->irq_clear(ap
);
3543 printk(KERN_ERR
"ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3544 ap
->id
, qc
->tf
.command
, drv_stat
, host_stat
);
3546 /* complete taskfile transaction */
3547 qc
->err_mask
|= ac_err_mask(drv_stat
);
3551 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3553 ata_eh_qc_complete(qc
);
3559 * ata_eng_timeout - Handle timeout of queued command
3560 * @ap: Port on which timed-out command is active
3562 * Some part of the kernel (currently, only the SCSI layer)
3563 * has noticed that the active command on port @ap has not
3564 * completed after a specified length of time. Handle this
3565 * condition by disabling DMA (if necessary) and completing
3566 * transactions, with error if necessary.
3568 * This also handles the case of the "lost interrupt", where
3569 * for some reason (possibly hardware bug, possibly driver bug)
3570 * an interrupt was not delivered to the driver, even though the
3571 * transaction completed successfully.
3574 * Inherited from SCSI layer (none, can sleep)
3577 void ata_eng_timeout(struct ata_port
*ap
)
3581 ata_qc_timeout(ata_qc_from_tag(ap
, ap
->active_tag
));
3587 * ata_qc_new - Request an available ATA command, for queueing
3588 * @ap: Port associated with device @dev
3589 * @dev: Device from whom we request an available command structure
3595 static struct ata_queued_cmd
*ata_qc_new(struct ata_port
*ap
)
3597 struct ata_queued_cmd
*qc
= NULL
;
3600 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++)
3601 if (!test_and_set_bit(i
, &ap
->qactive
)) {
3602 qc
= ata_qc_from_tag(ap
, i
);
3613 * ata_qc_new_init - Request an available ATA command, and initialize it
3614 * @ap: Port associated with device @dev
3615 * @dev: Device from whom we request an available command structure
3621 struct ata_queued_cmd
*ata_qc_new_init(struct ata_port
*ap
,
3622 struct ata_device
*dev
)
3624 struct ata_queued_cmd
*qc
;
3626 qc
= ata_qc_new(ap
);
3639 * ata_qc_free - free unused ata_queued_cmd
3640 * @qc: Command to complete
3642 * Designed to free unused ata_queued_cmd object
3643 * in case something prevents using it.
3646 * spin_lock_irqsave(host_set lock)
3648 void ata_qc_free(struct ata_queued_cmd
*qc
)
3650 struct ata_port
*ap
= qc
->ap
;
3653 WARN_ON(qc
== NULL
); /* ata_qc_from_tag _might_ return NULL */
3657 if (likely(ata_tag_valid(tag
))) {
3658 if (tag
== ap
->active_tag
)
3659 ap
->active_tag
= ATA_TAG_POISON
;
3660 qc
->tag
= ATA_TAG_POISON
;
3661 clear_bit(tag
, &ap
->qactive
);
3665 void __ata_qc_complete(struct ata_queued_cmd
*qc
)
3667 WARN_ON(qc
== NULL
); /* ata_qc_from_tag _might_ return NULL */
3668 WARN_ON(!(qc
->flags
& ATA_QCFLAG_ACTIVE
));
3670 if (likely(qc
->flags
& ATA_QCFLAG_DMAMAP
))
3673 /* atapi: mark qc as inactive to prevent the interrupt handler
3674 * from completing the command twice later, before the error handler
3675 * is called. (when rc != 0 and atapi request sense is needed)
3677 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
3679 /* call completion callback */
3680 qc
->complete_fn(qc
);
3683 static inline int ata_should_dma_map(struct ata_queued_cmd
*qc
)
3685 struct ata_port
*ap
= qc
->ap
;
3687 switch (qc
->tf
.protocol
) {
3689 case ATA_PROT_ATAPI_DMA
:
3692 case ATA_PROT_ATAPI
:
3694 case ATA_PROT_PIO_MULT
:
3695 if (ap
->flags
& ATA_FLAG_PIO_DMA
)
3708 * ata_qc_issue - issue taskfile to device
3709 * @qc: command to issue to device
3711 * Prepare an ATA command to submission to device.
3712 * This includes mapping the data into a DMA-able
3713 * area, filling in the S/G table, and finally
3714 * writing the taskfile to hardware, starting the command.
3717 * spin_lock_irqsave(host_set lock)
3720 * Zero on success, AC_ERR_* mask on failure
3723 unsigned int ata_qc_issue(struct ata_queued_cmd
*qc
)
3725 struct ata_port
*ap
= qc
->ap
;
3727 if (ata_should_dma_map(qc
)) {
3728 if (qc
->flags
& ATA_QCFLAG_SG
) {
3729 if (ata_sg_setup(qc
))
3731 } else if (qc
->flags
& ATA_QCFLAG_SINGLE
) {
3732 if (ata_sg_setup_one(qc
))
3736 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
3739 ap
->ops
->qc_prep(qc
);
3741 qc
->ap
->active_tag
= qc
->tag
;
3742 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
3744 return ap
->ops
->qc_issue(qc
);
3747 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
3748 return AC_ERR_SYSTEM
;
3753 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3754 * @qc: command to issue to device
3756 * Using various libata functions and hooks, this function
3757 * starts an ATA command. ATA commands are grouped into
3758 * classes called "protocols", and issuing each type of protocol
3759 * is slightly different.
3761 * May be used as the qc_issue() entry in ata_port_operations.
3764 * spin_lock_irqsave(host_set lock)
3767 * Zero on success, AC_ERR_* mask on failure
3770 unsigned int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
3772 struct ata_port
*ap
= qc
->ap
;
3774 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
3776 switch (qc
->tf
.protocol
) {
3777 case ATA_PROT_NODATA
:
3778 ata_tf_to_host(ap
, &qc
->tf
);
3782 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3783 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3784 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3787 case ATA_PROT_PIO
: /* load tf registers, initiate polling pio */
3788 ata_qc_set_polling(qc
);
3789 ata_tf_to_host(ap
, &qc
->tf
);
3790 ap
->hsm_task_state
= HSM_ST
;
3791 ata_queue_pio_task(ap
);
3794 case ATA_PROT_ATAPI
:
3795 ata_qc_set_polling(qc
);
3796 ata_tf_to_host(ap
, &qc
->tf
);
3797 ata_queue_packet_task(ap
);
3800 case ATA_PROT_ATAPI_NODATA
:
3801 ap
->flags
|= ATA_FLAG_NOINTR
;
3802 ata_tf_to_host(ap
, &qc
->tf
);
3803 ata_queue_packet_task(ap
);
3806 case ATA_PROT_ATAPI_DMA
:
3807 ap
->flags
|= ATA_FLAG_NOINTR
;
3808 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3809 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3810 ata_queue_packet_task(ap
);
3815 return AC_ERR_SYSTEM
;
3822 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3823 * @qc: Info associated with this ATA transaction.
3826 * spin_lock_irqsave(host_set lock)
3829 static void ata_bmdma_setup_mmio (struct ata_queued_cmd
*qc
)
3831 struct ata_port
*ap
= qc
->ap
;
3832 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3834 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3836 /* load PRD table addr. */
3837 mb(); /* make sure PRD table writes are visible to controller */
3838 writel(ap
->prd_dma
, mmio
+ ATA_DMA_TABLE_OFS
);
3840 /* specify data direction, triple-check start bit is clear */
3841 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3842 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3844 dmactl
|= ATA_DMA_WR
;
3845 writeb(dmactl
, mmio
+ ATA_DMA_CMD
);
3847 /* issue r/w command */
3848 ap
->ops
->exec_command(ap
, &qc
->tf
);
3852 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3853 * @qc: Info associated with this ATA transaction.
3856 * spin_lock_irqsave(host_set lock)
3859 static void ata_bmdma_start_mmio (struct ata_queued_cmd
*qc
)
3861 struct ata_port
*ap
= qc
->ap
;
3862 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3865 /* start host DMA transaction */
3866 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3867 writeb(dmactl
| ATA_DMA_START
, mmio
+ ATA_DMA_CMD
);
3869 /* Strictly, one may wish to issue a readb() here, to
3870 * flush the mmio write. However, control also passes
3871 * to the hardware at this point, and it will interrupt
3872 * us when we are to resume control. So, in effect,
3873 * we don't care when the mmio write flushes.
3874 * Further, a read of the DMA status register _immediately_
3875 * following the write may not be what certain flaky hardware
3876 * is expected, so I think it is best to not add a readb()
3877 * without first all the MMIO ATA cards/mobos.
3878 * Or maybe I'm just being paranoid.
3883 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3884 * @qc: Info associated with this ATA transaction.
3887 * spin_lock_irqsave(host_set lock)
3890 static void ata_bmdma_setup_pio (struct ata_queued_cmd
*qc
)
3892 struct ata_port
*ap
= qc
->ap
;
3893 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3896 /* load PRD table addr. */
3897 outl(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
3899 /* specify data direction, triple-check start bit is clear */
3900 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3901 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3903 dmactl
|= ATA_DMA_WR
;
3904 outb(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3906 /* issue r/w command */
3907 ap
->ops
->exec_command(ap
, &qc
->tf
);
3911 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3912 * @qc: Info associated with this ATA transaction.
3915 * spin_lock_irqsave(host_set lock)
3918 static void ata_bmdma_start_pio (struct ata_queued_cmd
*qc
)
3920 struct ata_port
*ap
= qc
->ap
;
3923 /* start host DMA transaction */
3924 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3925 outb(dmactl
| ATA_DMA_START
,
3926 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3931 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3932 * @qc: Info associated with this ATA transaction.
3934 * Writes the ATA_DMA_START flag to the DMA command register.
3936 * May be used as the bmdma_start() entry in ata_port_operations.
3939 * spin_lock_irqsave(host_set lock)
3941 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
3943 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3944 ata_bmdma_start_mmio(qc
);
3946 ata_bmdma_start_pio(qc
);
3951 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3952 * @qc: Info associated with this ATA transaction.
3954 * Writes address of PRD table to device's PRD Table Address
3955 * register, sets the DMA control register, and calls
3956 * ops->exec_command() to start the transfer.
3958 * May be used as the bmdma_setup() entry in ata_port_operations.
3961 * spin_lock_irqsave(host_set lock)
3963 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
3965 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3966 ata_bmdma_setup_mmio(qc
);
3968 ata_bmdma_setup_pio(qc
);
3973 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3974 * @ap: Port associated with this ATA transaction.
3976 * Clear interrupt and error flags in DMA status register.
3978 * May be used as the irq_clear() entry in ata_port_operations.
3981 * spin_lock_irqsave(host_set lock)
3984 void ata_bmdma_irq_clear(struct ata_port
*ap
)
3986 if (ap
->flags
& ATA_FLAG_MMIO
) {
3987 void __iomem
*mmio
= ((void __iomem
*) ap
->ioaddr
.bmdma_addr
) + ATA_DMA_STATUS
;
3988 writeb(readb(mmio
), mmio
);
3990 unsigned long addr
= ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
;
3991 outb(inb(addr
), addr
);
3998 * ata_bmdma_status - Read PCI IDE BMDMA status
3999 * @ap: Port associated with this ATA transaction.
4001 * Read and return BMDMA status register.
4003 * May be used as the bmdma_status() entry in ata_port_operations.
4006 * spin_lock_irqsave(host_set lock)
4009 u8
ata_bmdma_status(struct ata_port
*ap
)
4012 if (ap
->flags
& ATA_FLAG_MMIO
) {
4013 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
4014 host_stat
= readb(mmio
+ ATA_DMA_STATUS
);
4016 host_stat
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
4022 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4023 * @qc: Command we are ending DMA for
4025 * Clears the ATA_DMA_START flag in the dma control register
4027 * May be used as the bmdma_stop() entry in ata_port_operations.
4030 * spin_lock_irqsave(host_set lock)
4033 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
4035 struct ata_port
*ap
= qc
->ap
;
4036 if (ap
->flags
& ATA_FLAG_MMIO
) {
4037 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
4039 /* clear start/stop bit */
4040 writeb(readb(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
4041 mmio
+ ATA_DMA_CMD
);
4043 /* clear start/stop bit */
4044 outb(inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
4045 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
4048 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4049 ata_altstatus(ap
); /* dummy read */
4053 * ata_host_intr - Handle host interrupt for given (port, task)
4054 * @ap: Port on which interrupt arrived (possibly...)
4055 * @qc: Taskfile currently active in engine
4057 * Handle host interrupt for given queued command. Currently,
4058 * only DMA interrupts are handled. All other commands are
4059 * handled via polling with interrupts disabled (nIEN bit).
4062 * spin_lock_irqsave(host_set lock)
4065 * One if interrupt was handled, zero if not (shared irq).
4068 inline unsigned int ata_host_intr (struct ata_port
*ap
,
4069 struct ata_queued_cmd
*qc
)
4071 u8 status
, host_stat
;
4073 switch (qc
->tf
.protocol
) {
4076 case ATA_PROT_ATAPI_DMA
:
4077 case ATA_PROT_ATAPI
:
4078 /* check status of DMA engine */
4079 host_stat
= ap
->ops
->bmdma_status(ap
);
4080 VPRINTK("ata%u: host_stat 0x%X\n", ap
->id
, host_stat
);
4082 /* if it's not our irq... */
4083 if (!(host_stat
& ATA_DMA_INTR
))
4086 /* before we do anything else, clear DMA-Start bit */
4087 ap
->ops
->bmdma_stop(qc
);
4091 case ATA_PROT_ATAPI_NODATA
:
4092 case ATA_PROT_NODATA
:
4093 /* check altstatus */
4094 status
= ata_altstatus(ap
);
4095 if (status
& ATA_BUSY
)
4098 /* check main status, clearing INTRQ */
4099 status
= ata_chk_status(ap
);
4100 if (unlikely(status
& ATA_BUSY
))
4102 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4103 ap
->id
, qc
->tf
.protocol
, status
);
4105 /* ack bmdma irq events */
4106 ap
->ops
->irq_clear(ap
);
4108 /* complete taskfile transaction */
4109 qc
->err_mask
|= ac_err_mask(status
);
4110 ata_qc_complete(qc
);
4117 return 1; /* irq handled */
4120 ap
->stats
.idle_irq
++;
4123 if ((ap
->stats
.idle_irq
% 1000) == 0) {
4125 ata_irq_ack(ap
, 0); /* debug trap */
4126 printk(KERN_WARNING
"ata%d: irq trap\n", ap
->id
);
4129 return 0; /* irq not handled */
4133 * ata_interrupt - Default ATA host interrupt handler
4134 * @irq: irq line (unused)
4135 * @dev_instance: pointer to our ata_host_set information structure
4138 * Default interrupt handler for PCI IDE devices. Calls
4139 * ata_host_intr() for each port that is not disabled.
4142 * Obtains host_set lock during operation.
4145 * IRQ_NONE or IRQ_HANDLED.
4148 irqreturn_t
ata_interrupt (int irq
, void *dev_instance
, struct pt_regs
*regs
)
4150 struct ata_host_set
*host_set
= dev_instance
;
4152 unsigned int handled
= 0;
4153 unsigned long flags
;
4155 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4156 spin_lock_irqsave(&host_set
->lock
, flags
);
4158 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4159 struct ata_port
*ap
;
4161 ap
= host_set
->ports
[i
];
4163 !(ap
->flags
& (ATA_FLAG_PORT_DISABLED
| ATA_FLAG_NOINTR
))) {
4164 struct ata_queued_cmd
*qc
;
4166 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
4167 if (qc
&& (!(qc
->tf
.ctl
& ATA_NIEN
)) &&
4168 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
4169 handled
|= ata_host_intr(ap
, qc
);
4173 spin_unlock_irqrestore(&host_set
->lock
, flags
);
4175 return IRQ_RETVAL(handled
);
4179 * atapi_packet_task - Write CDB bytes to hardware
4180 * @_data: Port to which ATAPI device is attached.
4182 * When device has indicated its readiness to accept
4183 * a CDB, this function is called. Send the CDB.
4184 * If DMA is to be performed, exit immediately.
4185 * Otherwise, we are in polling mode, so poll
4186 * status under operation succeeds or fails.
4189 * Kernel thread context (may sleep)
4192 static void atapi_packet_task(void *_data
)
4194 struct ata_port
*ap
= _data
;
4195 struct ata_queued_cmd
*qc
;
4198 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
4199 WARN_ON(qc
== NULL
);
4200 WARN_ON(!(qc
->flags
& ATA_QCFLAG_ACTIVE
));
4202 /* sleep-wait for BSY to clear */
4203 DPRINTK("busy wait\n");
4204 if (ata_busy_sleep(ap
, ATA_TMOUT_CDB_QUICK
, ATA_TMOUT_CDB
)) {
4205 qc
->err_mask
|= AC_ERR_TIMEOUT
;
4209 /* make sure DRQ is set */
4210 status
= ata_chk_status(ap
);
4211 if ((status
& (ATA_BUSY
| ATA_DRQ
)) != ATA_DRQ
) {
4212 qc
->err_mask
|= AC_ERR_HSM
;
4217 DPRINTK("send cdb\n");
4218 WARN_ON(qc
->dev
->cdb_len
< 12);
4220 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
||
4221 qc
->tf
.protocol
== ATA_PROT_ATAPI_NODATA
) {
4222 unsigned long flags
;
4224 /* Once we're done issuing command and kicking bmdma,
4225 * irq handler takes over. To not lose irq, we need
4226 * to clear NOINTR flag before sending cdb, but
4227 * interrupt handler shouldn't be invoked before we're
4228 * finished. Hence, the following locking.
4230 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
4231 ap
->flags
&= ~ATA_FLAG_NOINTR
;
4232 ata_data_xfer(ap
, qc
->cdb
, qc
->dev
->cdb_len
, 1);
4233 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
)
4234 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
4235 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
4237 ata_data_xfer(ap
, qc
->cdb
, qc
->dev
->cdb_len
, 1);
4239 /* PIO commands are handled by polling */
4240 ap
->hsm_task_state
= HSM_ST
;
4241 ata_queue_pio_task(ap
);
4247 ata_poll_qc_complete(qc
);
4252 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4253 * without filling any other registers
4255 static int ata_do_simple_cmd(struct ata_port
*ap
, struct ata_device
*dev
,
4258 struct ata_taskfile tf
;
4261 ata_tf_init(ap
, &tf
, dev
->devno
);
4264 tf
.flags
|= ATA_TFLAG_DEVICE
;
4265 tf
.protocol
= ATA_PROT_NODATA
;
4267 err
= ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0);
4269 printk(KERN_ERR
"%s: ata command failed: %d\n",
4275 static int ata_flush_cache(struct ata_port
*ap
, struct ata_device
*dev
)
4279 if (!ata_try_flush_cache(dev
))
4282 if (ata_id_has_flush_ext(dev
->id
))
4283 cmd
= ATA_CMD_FLUSH_EXT
;
4285 cmd
= ATA_CMD_FLUSH
;
4287 return ata_do_simple_cmd(ap
, dev
, cmd
);
4290 static int ata_standby_drive(struct ata_port
*ap
, struct ata_device
*dev
)
4292 return ata_do_simple_cmd(ap
, dev
, ATA_CMD_STANDBYNOW1
);
4295 static int ata_start_drive(struct ata_port
*ap
, struct ata_device
*dev
)
4297 return ata_do_simple_cmd(ap
, dev
, ATA_CMD_IDLEIMMEDIATE
);
4301 * ata_device_resume - wakeup a previously suspended devices
4302 * @ap: port the device is connected to
4303 * @dev: the device to resume
4305 * Kick the drive back into action, by sending it an idle immediate
4306 * command and making sure its transfer mode matches between drive
4310 int ata_device_resume(struct ata_port
*ap
, struct ata_device
*dev
)
4312 if (ap
->flags
& ATA_FLAG_SUSPENDED
) {
4313 ap
->flags
&= ~ATA_FLAG_SUSPENDED
;
4316 if (!ata_dev_present(dev
))
4318 if (dev
->class == ATA_DEV_ATA
)
4319 ata_start_drive(ap
, dev
);
4325 * ata_device_suspend - prepare a device for suspend
4326 * @ap: port the device is connected to
4327 * @dev: the device to suspend
4329 * Flush the cache on the drive, if appropriate, then issue a
4330 * standbynow command.
4332 int ata_device_suspend(struct ata_port
*ap
, struct ata_device
*dev
)
4334 if (!ata_dev_present(dev
))
4336 if (dev
->class == ATA_DEV_ATA
)
4337 ata_flush_cache(ap
, dev
);
4339 ata_standby_drive(ap
, dev
);
4340 ap
->flags
|= ATA_FLAG_SUSPENDED
;
4345 * ata_port_start - Set port up for dma.
4346 * @ap: Port to initialize
4348 * Called just after data structures for each port are
4349 * initialized. Allocates space for PRD table.
4351 * May be used as the port_start() entry in ata_port_operations.
4354 * Inherited from caller.
4357 int ata_port_start (struct ata_port
*ap
)
4359 struct device
*dev
= ap
->host_set
->dev
;
4362 ap
->prd
= dma_alloc_coherent(dev
, ATA_PRD_TBL_SZ
, &ap
->prd_dma
, GFP_KERNEL
);
4366 rc
= ata_pad_alloc(ap
, dev
);
4368 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
4372 DPRINTK("prd alloc, virt %p, dma %llx\n", ap
->prd
, (unsigned long long) ap
->prd_dma
);
4379 * ata_port_stop - Undo ata_port_start()
4380 * @ap: Port to shut down
4382 * Frees the PRD table.
4384 * May be used as the port_stop() entry in ata_port_operations.
4387 * Inherited from caller.
4390 void ata_port_stop (struct ata_port
*ap
)
4392 struct device
*dev
= ap
->host_set
->dev
;
4394 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
4395 ata_pad_free(ap
, dev
);
4398 void ata_host_stop (struct ata_host_set
*host_set
)
4400 if (host_set
->mmio_base
)
4401 iounmap(host_set
->mmio_base
);
4406 * ata_host_remove - Unregister SCSI host structure with upper layers
4407 * @ap: Port to unregister
4408 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4411 * Inherited from caller.
4414 static void ata_host_remove(struct ata_port
*ap
, unsigned int do_unregister
)
4416 struct Scsi_Host
*sh
= ap
->host
;
4421 scsi_remove_host(sh
);
4423 ap
->ops
->port_stop(ap
);
4427 * ata_host_init - Initialize an ata_port structure
4428 * @ap: Structure to initialize
4429 * @host: associated SCSI mid-layer structure
4430 * @host_set: Collection of hosts to which @ap belongs
4431 * @ent: Probe information provided by low-level driver
4432 * @port_no: Port number associated with this ata_port
4434 * Initialize a new ata_port structure, and its associated
4438 * Inherited from caller.
4441 static void ata_host_init(struct ata_port
*ap
, struct Scsi_Host
*host
,
4442 struct ata_host_set
*host_set
,
4443 const struct ata_probe_ent
*ent
, unsigned int port_no
)
4449 host
->max_channel
= 1;
4450 host
->unique_id
= ata_unique_id
++;
4451 host
->max_cmd_len
= 12;
4453 ap
->flags
= ATA_FLAG_PORT_DISABLED
;
4454 ap
->id
= host
->unique_id
;
4456 ap
->ctl
= ATA_DEVCTL_OBS
;
4457 ap
->host_set
= host_set
;
4458 ap
->port_no
= port_no
;
4460 ent
->legacy_mode
? ent
->hard_port_no
: port_no
;
4461 ap
->pio_mask
= ent
->pio_mask
;
4462 ap
->mwdma_mask
= ent
->mwdma_mask
;
4463 ap
->udma_mask
= ent
->udma_mask
;
4464 ap
->flags
|= ent
->host_flags
;
4465 ap
->ops
= ent
->port_ops
;
4466 ap
->cbl
= ATA_CBL_NONE
;
4467 ap
->active_tag
= ATA_TAG_POISON
;
4468 ap
->last_ctl
= 0xFF;
4470 INIT_WORK(&ap
->packet_task
, atapi_packet_task
, ap
);
4471 INIT_WORK(&ap
->pio_task
, ata_pio_task
, ap
);
4472 INIT_LIST_HEAD(&ap
->eh_done_q
);
4474 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
4475 ap
->device
[i
].devno
= i
;
4478 ap
->stats
.unhandled_irq
= 1;
4479 ap
->stats
.idle_irq
= 1;
4482 memcpy(&ap
->ioaddr
, &ent
->port
[port_no
], sizeof(struct ata_ioports
));
4486 * ata_host_add - Attach low-level ATA driver to system
4487 * @ent: Information provided by low-level driver
4488 * @host_set: Collections of ports to which we add
4489 * @port_no: Port number associated with this host
4491 * Attach low-level ATA driver to system.
4494 * PCI/etc. bus probe sem.
4497 * New ata_port on success, for NULL on error.
4500 static struct ata_port
* ata_host_add(const struct ata_probe_ent
*ent
,
4501 struct ata_host_set
*host_set
,
4502 unsigned int port_no
)
4504 struct Scsi_Host
*host
;
4505 struct ata_port
*ap
;
4509 host
= scsi_host_alloc(ent
->sht
, sizeof(struct ata_port
));
4513 ap
= (struct ata_port
*) &host
->hostdata
[0];
4515 ata_host_init(ap
, host
, host_set
, ent
, port_no
);
4517 rc
= ap
->ops
->port_start(ap
);
4524 scsi_host_put(host
);
4529 * ata_device_add - Register hardware device with ATA and SCSI layers
4530 * @ent: Probe information describing hardware device to be registered
4532 * This function processes the information provided in the probe
4533 * information struct @ent, allocates the necessary ATA and SCSI
4534 * host information structures, initializes them, and registers
4535 * everything with requisite kernel subsystems.
4537 * This function requests irqs, probes the ATA bus, and probes
4541 * PCI/etc. bus probe sem.
4544 * Number of ports registered. Zero on error (no ports registered).
4547 int ata_device_add(const struct ata_probe_ent
*ent
)
4549 unsigned int count
= 0, i
;
4550 struct device
*dev
= ent
->dev
;
4551 struct ata_host_set
*host_set
;
4554 /* alloc a container for our list of ATA ports (buses) */
4555 host_set
= kzalloc(sizeof(struct ata_host_set
) +
4556 (ent
->n_ports
* sizeof(void *)), GFP_KERNEL
);
4559 spin_lock_init(&host_set
->lock
);
4561 host_set
->dev
= dev
;
4562 host_set
->n_ports
= ent
->n_ports
;
4563 host_set
->irq
= ent
->irq
;
4564 host_set
->mmio_base
= ent
->mmio_base
;
4565 host_set
->private_data
= ent
->private_data
;
4566 host_set
->ops
= ent
->port_ops
;
4568 /* register each port bound to this device */
4569 for (i
= 0; i
< ent
->n_ports
; i
++) {
4570 struct ata_port
*ap
;
4571 unsigned long xfer_mode_mask
;
4573 ap
= ata_host_add(ent
, host_set
, i
);
4577 host_set
->ports
[i
] = ap
;
4578 xfer_mode_mask
=(ap
->udma_mask
<< ATA_SHIFT_UDMA
) |
4579 (ap
->mwdma_mask
<< ATA_SHIFT_MWDMA
) |
4580 (ap
->pio_mask
<< ATA_SHIFT_PIO
);
4582 /* print per-port info to dmesg */
4583 printk(KERN_INFO
"ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4584 "bmdma 0x%lX irq %lu\n",
4586 ap
->flags
& ATA_FLAG_SATA
? 'S' : 'P',
4587 ata_mode_string(xfer_mode_mask
),
4588 ap
->ioaddr
.cmd_addr
,
4589 ap
->ioaddr
.ctl_addr
,
4590 ap
->ioaddr
.bmdma_addr
,
4594 host_set
->ops
->irq_clear(ap
);
4601 /* obtain irq, that is shared between channels */
4602 if (request_irq(ent
->irq
, ent
->port_ops
->irq_handler
, ent
->irq_flags
,
4603 DRV_NAME
, host_set
))
4606 /* perform each probe synchronously */
4607 DPRINTK("probe begin\n");
4608 for (i
= 0; i
< count
; i
++) {
4609 struct ata_port
*ap
;
4612 ap
= host_set
->ports
[i
];
4614 DPRINTK("ata%u: bus probe begin\n", ap
->id
);
4615 rc
= ata_bus_probe(ap
);
4616 DPRINTK("ata%u: bus probe end\n", ap
->id
);
4619 /* FIXME: do something useful here?
4620 * Current libata behavior will
4621 * tear down everything when
4622 * the module is removed
4623 * or the h/w is unplugged.
4627 rc
= scsi_add_host(ap
->host
, dev
);
4629 printk(KERN_ERR
"ata%u: scsi_add_host failed\n",
4631 /* FIXME: do something useful here */
4632 /* FIXME: handle unconditional calls to
4633 * scsi_scan_host and ata_host_remove, below,
4639 /* probes are done, now scan each port's disk(s) */
4640 DPRINTK("host probe begin\n");
4641 for (i
= 0; i
< count
; i
++) {
4642 struct ata_port
*ap
= host_set
->ports
[i
];
4644 ata_scsi_scan_host(ap
);
4647 dev_set_drvdata(dev
, host_set
);
4649 VPRINTK("EXIT, returning %u\n", ent
->n_ports
);
4650 return ent
->n_ports
; /* success */
4653 for (i
= 0; i
< count
; i
++) {
4654 ata_host_remove(host_set
->ports
[i
], 1);
4655 scsi_host_put(host_set
->ports
[i
]->host
);
4659 VPRINTK("EXIT, returning 0\n");
4664 * ata_host_set_remove - PCI layer callback for device removal
4665 * @host_set: ATA host set that was removed
4667 * Unregister all objects associated with this host set. Free those
4671 * Inherited from calling layer (may sleep).
4674 void ata_host_set_remove(struct ata_host_set
*host_set
)
4676 struct ata_port
*ap
;
4679 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4680 ap
= host_set
->ports
[i
];
4681 scsi_remove_host(ap
->host
);
4684 free_irq(host_set
->irq
, host_set
);
4686 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4687 ap
= host_set
->ports
[i
];
4689 ata_scsi_release(ap
->host
);
4691 if ((ap
->flags
& ATA_FLAG_NO_LEGACY
) == 0) {
4692 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
4694 if (ioaddr
->cmd_addr
== 0x1f0)
4695 release_region(0x1f0, 8);
4696 else if (ioaddr
->cmd_addr
== 0x170)
4697 release_region(0x170, 8);
4700 scsi_host_put(ap
->host
);
4703 if (host_set
->ops
->host_stop
)
4704 host_set
->ops
->host_stop(host_set
);
4710 * ata_scsi_release - SCSI layer callback hook for host unload
4711 * @host: libata host to be unloaded
4713 * Performs all duties necessary to shut down a libata port...
4714 * Kill port kthread, disable port, and release resources.
4717 * Inherited from SCSI layer.
4723 int ata_scsi_release(struct Scsi_Host
*host
)
4725 struct ata_port
*ap
= (struct ata_port
*) &host
->hostdata
[0];
4729 ap
->ops
->port_disable(ap
);
4730 ata_host_remove(ap
, 0);
4737 * ata_std_ports - initialize ioaddr with standard port offsets.
4738 * @ioaddr: IO address structure to be initialized
4740 * Utility function which initializes data_addr, error_addr,
4741 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4742 * device_addr, status_addr, and command_addr to standard offsets
4743 * relative to cmd_addr.
4745 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4748 void ata_std_ports(struct ata_ioports
*ioaddr
)
4750 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
4751 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
4752 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
4753 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
4754 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
4755 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
4756 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
4757 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
4758 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
4759 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
4765 void ata_pci_host_stop (struct ata_host_set
*host_set
)
4767 struct pci_dev
*pdev
= to_pci_dev(host_set
->dev
);
4769 pci_iounmap(pdev
, host_set
->mmio_base
);
4773 * ata_pci_remove_one - PCI layer callback for device removal
4774 * @pdev: PCI device that was removed
4776 * PCI layer indicates to libata via this hook that
4777 * hot-unplug or module unload event has occurred.
4778 * Handle this by unregistering all objects associated
4779 * with this PCI device. Free those objects. Then finally
4780 * release PCI resources and disable device.
4783 * Inherited from PCI layer (may sleep).
4786 void ata_pci_remove_one (struct pci_dev
*pdev
)
4788 struct device
*dev
= pci_dev_to_dev(pdev
);
4789 struct ata_host_set
*host_set
= dev_get_drvdata(dev
);
4791 ata_host_set_remove(host_set
);
4792 pci_release_regions(pdev
);
4793 pci_disable_device(pdev
);
4794 dev_set_drvdata(dev
, NULL
);
4797 /* move to PCI subsystem */
4798 int pci_test_config_bits(struct pci_dev
*pdev
, const struct pci_bits
*bits
)
4800 unsigned long tmp
= 0;
4802 switch (bits
->width
) {
4805 pci_read_config_byte(pdev
, bits
->reg
, &tmp8
);
4811 pci_read_config_word(pdev
, bits
->reg
, &tmp16
);
4817 pci_read_config_dword(pdev
, bits
->reg
, &tmp32
);
4828 return (tmp
== bits
->val
) ? 1 : 0;
4831 int ata_pci_device_suspend(struct pci_dev
*pdev
, pm_message_t state
)
4833 pci_save_state(pdev
);
4834 pci_disable_device(pdev
);
4835 pci_set_power_state(pdev
, PCI_D3hot
);
4839 int ata_pci_device_resume(struct pci_dev
*pdev
)
4841 pci_set_power_state(pdev
, PCI_D0
);
4842 pci_restore_state(pdev
);
4843 pci_enable_device(pdev
);
4844 pci_set_master(pdev
);
4847 #endif /* CONFIG_PCI */
4850 static int __init
ata_init(void)
4852 ata_wq
= create_workqueue("ata");
4856 printk(KERN_DEBUG
"libata version " DRV_VERSION
" loaded.\n");
4860 static void __exit
ata_exit(void)
4862 destroy_workqueue(ata_wq
);
4865 module_init(ata_init
);
4866 module_exit(ata_exit
);
4868 static unsigned long ratelimit_time
;
4869 static spinlock_t ata_ratelimit_lock
= SPIN_LOCK_UNLOCKED
;
4871 int ata_ratelimit(void)
4874 unsigned long flags
;
4876 spin_lock_irqsave(&ata_ratelimit_lock
, flags
);
4878 if (time_after(jiffies
, ratelimit_time
)) {
4880 ratelimit_time
= jiffies
+ (HZ
/5);
4884 spin_unlock_irqrestore(&ata_ratelimit_lock
, flags
);
4890 * libata is essentially a library of internal helper functions for
4891 * low-level ATA host controller drivers. As such, the API/ABI is
4892 * likely to change as new drivers are added and updated.
4893 * Do not depend on ABI/API stability.
4896 EXPORT_SYMBOL_GPL(ata_std_bios_param
);
4897 EXPORT_SYMBOL_GPL(ata_std_ports
);
4898 EXPORT_SYMBOL_GPL(ata_device_add
);
4899 EXPORT_SYMBOL_GPL(ata_host_set_remove
);
4900 EXPORT_SYMBOL_GPL(ata_sg_init
);
4901 EXPORT_SYMBOL_GPL(ata_sg_init_one
);
4902 EXPORT_SYMBOL_GPL(__ata_qc_complete
);
4903 EXPORT_SYMBOL_GPL(ata_qc_issue_prot
);
4904 EXPORT_SYMBOL_GPL(ata_eng_timeout
);
4905 EXPORT_SYMBOL_GPL(ata_tf_load
);
4906 EXPORT_SYMBOL_GPL(ata_tf_read
);
4907 EXPORT_SYMBOL_GPL(ata_noop_dev_select
);
4908 EXPORT_SYMBOL_GPL(ata_std_dev_select
);
4909 EXPORT_SYMBOL_GPL(ata_tf_to_fis
);
4910 EXPORT_SYMBOL_GPL(ata_tf_from_fis
);
4911 EXPORT_SYMBOL_GPL(ata_check_status
);
4912 EXPORT_SYMBOL_GPL(ata_altstatus
);
4913 EXPORT_SYMBOL_GPL(ata_exec_command
);
4914 EXPORT_SYMBOL_GPL(ata_port_start
);
4915 EXPORT_SYMBOL_GPL(ata_port_stop
);
4916 EXPORT_SYMBOL_GPL(ata_host_stop
);
4917 EXPORT_SYMBOL_GPL(ata_interrupt
);
4918 EXPORT_SYMBOL_GPL(ata_qc_prep
);
4919 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
4920 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
4921 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear
);
4922 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
4923 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
4924 EXPORT_SYMBOL_GPL(ata_port_probe
);
4925 EXPORT_SYMBOL_GPL(sata_phy_reset
);
4926 EXPORT_SYMBOL_GPL(__sata_phy_reset
);
4927 EXPORT_SYMBOL_GPL(ata_bus_reset
);
4928 EXPORT_SYMBOL_GPL(ata_std_probeinit
);
4929 EXPORT_SYMBOL_GPL(ata_std_softreset
);
4930 EXPORT_SYMBOL_GPL(sata_std_hardreset
);
4931 EXPORT_SYMBOL_GPL(ata_std_postreset
);
4932 EXPORT_SYMBOL_GPL(ata_std_probe_reset
);
4933 EXPORT_SYMBOL_GPL(ata_drive_probe_reset
);
4934 EXPORT_SYMBOL_GPL(ata_port_disable
);
4935 EXPORT_SYMBOL_GPL(ata_ratelimit
);
4936 EXPORT_SYMBOL_GPL(ata_busy_sleep
);
4937 EXPORT_SYMBOL_GPL(ata_scsi_ioctl
);
4938 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd
);
4939 EXPORT_SYMBOL_GPL(ata_scsi_timed_out
);
4940 EXPORT_SYMBOL_GPL(ata_scsi_error
);
4941 EXPORT_SYMBOL_GPL(ata_scsi_slave_config
);
4942 EXPORT_SYMBOL_GPL(ata_scsi_release
);
4943 EXPORT_SYMBOL_GPL(ata_host_intr
);
4944 EXPORT_SYMBOL_GPL(ata_dev_classify
);
4945 EXPORT_SYMBOL_GPL(ata_id_string
);
4946 EXPORT_SYMBOL_GPL(ata_id_c_string
);
4947 EXPORT_SYMBOL_GPL(ata_dev_config
);
4948 EXPORT_SYMBOL_GPL(ata_scsi_simulate
);
4949 EXPORT_SYMBOL_GPL(ata_eh_qc_complete
);
4950 EXPORT_SYMBOL_GPL(ata_eh_qc_retry
);
4952 EXPORT_SYMBOL_GPL(ata_pio_need_iordy
);
4953 EXPORT_SYMBOL_GPL(ata_timing_compute
);
4954 EXPORT_SYMBOL_GPL(ata_timing_merge
);
4957 EXPORT_SYMBOL_GPL(pci_test_config_bits
);
4958 EXPORT_SYMBOL_GPL(ata_pci_host_stop
);
4959 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode
);
4960 EXPORT_SYMBOL_GPL(ata_pci_init_one
);
4961 EXPORT_SYMBOL_GPL(ata_pci_remove_one
);
4962 EXPORT_SYMBOL_GPL(ata_pci_device_suspend
);
4963 EXPORT_SYMBOL_GPL(ata_pci_device_resume
);
4964 #endif /* CONFIG_PCI */
4966 EXPORT_SYMBOL_GPL(ata_device_suspend
);
4967 EXPORT_SYMBOL_GPL(ata_device_resume
);
4968 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend
);
4969 EXPORT_SYMBOL_GPL(ata_scsi_device_resume
);