2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev);
66 static void ata_set_mode(struct ata_port *ap);
67 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
68 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
69 static int fgb(u32 bitmap);
70 static int ata_choose_xfer_mode(const struct ata_port *ap,
72 unsigned int *xfer_shift_out);
74 static unsigned int ata_unique_id = 1;
75 static struct workqueue_struct *ata_wq;
77 int atapi_enabled = 0;
78 module_param(atapi_enabled, int, 0444);
79 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
82 module_param_named(fua, libata_fua, int, 0444);
83 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
85 MODULE_AUTHOR("Jeff Garzik");
86 MODULE_DESCRIPTION("Library module for ATA devices");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
92 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
93 * @tf: Taskfile to convert
94 * @fis: Buffer into which data will output
95 * @pmp: Port multiplier port
97 * Converts a standard ATA taskfile to a Serial ATA
98 * FIS structure (Register - Host to Device).
101 * Inherited from caller.
104 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
106 fis[0] = 0x27; /* Register - Host to Device FIS */
107 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
108 bit 7 indicates Command FIS */
109 fis[2] = tf->command;
110 fis[3] = tf->feature;
117 fis[8] = tf->hob_lbal;
118 fis[9] = tf->hob_lbam;
119 fis[10] = tf->hob_lbah;
120 fis[11] = tf->hob_feature;
123 fis[13] = tf->hob_nsect;
134 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
135 * @fis: Buffer from which data will be input
136 * @tf: Taskfile to output
138 * Converts a serial ATA FIS structure to a standard ATA taskfile.
141 * Inherited from caller.
144 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
146 tf->command = fis[2]; /* status */
147 tf->feature = fis[3]; /* error */
154 tf->hob_lbal = fis[8];
155 tf->hob_lbam = fis[9];
156 tf->hob_lbah = fis[10];
159 tf->hob_nsect = fis[13];
162 static const u8 ata_rw_cmds[] = {
166 ATA_CMD_READ_MULTI_EXT,
167 ATA_CMD_WRITE_MULTI_EXT,
171 ATA_CMD_WRITE_MULTI_FUA_EXT,
175 ATA_CMD_PIO_READ_EXT,
176 ATA_CMD_PIO_WRITE_EXT,
189 ATA_CMD_WRITE_FUA_EXT
193 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
194 * @qc: command to examine and configure
196 * Examine the device configuration and tf->flags to calculate
197 * the proper read/write commands and protocol to use.
202 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
204 struct ata_taskfile *tf = &qc->tf;
205 struct ata_device *dev = qc->dev;
208 int index, fua, lba48, write;
210 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
211 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
212 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
214 if (dev->flags & ATA_DFLAG_PIO) {
215 tf->protocol = ATA_PROT_PIO;
216 index = dev->multi_count ? 0 : 8;
217 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
218 /* Unable to use DMA due to host limitation */
219 tf->protocol = ATA_PROT_PIO;
220 index = dev->multi_count ? 0 : 8;
222 tf->protocol = ATA_PROT_DMA;
226 cmd = ata_rw_cmds[index + fua + lba48 + write];
234 static const char * const xfer_mode_str[] = {
254 * ata_udma_string - convert UDMA bit offset to string
255 * @mask: mask of bits supported; only highest bit counts.
257 * Determine string which represents the highest speed
258 * (highest bit in @udma_mask).
264 * Constant C string representing highest speed listed in
265 * @udma_mask, or the constant C string "<n/a>".
268 static const char *ata_mode_string(unsigned int mask)
272 for (i = 7; i >= 0; i--)
275 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
278 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
285 return xfer_mode_str[i];
289 * ata_pio_devchk - PATA device presence detection
290 * @ap: ATA channel to examine
291 * @device: Device to examine (starting at zero)
293 * This technique was originally described in
294 * Hale Landis's ATADRVR (www.ata-atapi.com), and
295 * later found its way into the ATA/ATAPI spec.
297 * Write a pattern to the ATA shadow registers,
298 * and if a device is present, it will respond by
299 * correctly storing and echoing back the
300 * ATA shadow register contents.
306 static unsigned int ata_pio_devchk(struct ata_port *ap,
309 struct ata_ioports *ioaddr = &ap->ioaddr;
312 ap->ops->dev_select(ap, device);
314 outb(0x55, ioaddr->nsect_addr);
315 outb(0xaa, ioaddr->lbal_addr);
317 outb(0xaa, ioaddr->nsect_addr);
318 outb(0x55, ioaddr->lbal_addr);
320 outb(0x55, ioaddr->nsect_addr);
321 outb(0xaa, ioaddr->lbal_addr);
323 nsect = inb(ioaddr->nsect_addr);
324 lbal = inb(ioaddr->lbal_addr);
326 if ((nsect == 0x55) && (lbal == 0xaa))
327 return 1; /* we found a device */
329 return 0; /* nothing found */
333 * ata_mmio_devchk - PATA device presence detection
334 * @ap: ATA channel to examine
335 * @device: Device to examine (starting at zero)
337 * This technique was originally described in
338 * Hale Landis's ATADRVR (www.ata-atapi.com), and
339 * later found its way into the ATA/ATAPI spec.
341 * Write a pattern to the ATA shadow registers,
342 * and if a device is present, it will respond by
343 * correctly storing and echoing back the
344 * ATA shadow register contents.
350 static unsigned int ata_mmio_devchk(struct ata_port *ap,
353 struct ata_ioports *ioaddr = &ap->ioaddr;
356 ap->ops->dev_select(ap, device);
358 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
359 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
361 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
362 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
364 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
365 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
367 nsect = readb((void __iomem *) ioaddr->nsect_addr);
368 lbal = readb((void __iomem *) ioaddr->lbal_addr);
370 if ((nsect == 0x55) && (lbal == 0xaa))
371 return 1; /* we found a device */
373 return 0; /* nothing found */
377 * ata_devchk - PATA device presence detection
378 * @ap: ATA channel to examine
379 * @device: Device to examine (starting at zero)
381 * Dispatch ATA device presence detection, depending
382 * on whether we are using PIO or MMIO to talk to the
383 * ATA shadow registers.
389 static unsigned int ata_devchk(struct ata_port *ap,
392 if (ap->flags & ATA_FLAG_MMIO)
393 return ata_mmio_devchk(ap, device);
394 return ata_pio_devchk(ap, device);
398 * ata_dev_classify - determine device type based on ATA-spec signature
399 * @tf: ATA taskfile register set for device to be identified
401 * Determine from taskfile register contents whether a device is
402 * ATA or ATAPI, as per "Signature and persistence" section
403 * of ATA/PI spec (volume 1, sect 5.14).
409 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
410 * the event of failure.
413 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
415 /* Apple's open source Darwin code hints that some devices only
416 * put a proper signature into the LBA mid/high registers,
417 * So, we only check those. It's sufficient for uniqueness.
420 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
421 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
422 DPRINTK("found ATA device by sig\n");
426 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
427 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
428 DPRINTK("found ATAPI device by sig\n");
429 return ATA_DEV_ATAPI;
432 DPRINTK("unknown device\n");
433 return ATA_DEV_UNKNOWN;
437 * ata_dev_try_classify - Parse returned ATA device signature
438 * @ap: ATA channel to examine
439 * @device: Device to examine (starting at zero)
440 * @r_err: Value of error register on completion
442 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
443 * an ATA/ATAPI-defined set of values is placed in the ATA
444 * shadow registers, indicating the results of device detection
447 * Select the ATA device, and read the values from the ATA shadow
448 * registers. Then parse according to the Error register value,
449 * and the spec-defined values examined by ata_dev_classify().
455 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
459 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
461 struct ata_taskfile tf;
465 ap->ops->dev_select(ap, device);
467 memset(&tf, 0, sizeof(tf));
469 ap->ops->tf_read(ap, &tf);
474 /* see if device passed diags */
477 else if ((device == 0) && (err == 0x81))
482 /* determine if device is ATA or ATAPI */
483 class = ata_dev_classify(&tf);
485 if (class == ATA_DEV_UNKNOWN)
487 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
493 * ata_id_string - Convert IDENTIFY DEVICE page into string
494 * @id: IDENTIFY DEVICE results we will examine
495 * @s: string into which data is output
496 * @ofs: offset into identify device page
497 * @len: length of string to return. must be an even number.
499 * The strings in the IDENTIFY DEVICE page are broken up into
500 * 16-bit chunks. Run through the string, and output each
501 * 8-bit chunk linearly, regardless of platform.
507 void ata_id_string(const u16 *id, unsigned char *s,
508 unsigned int ofs, unsigned int len)
527 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
528 * @id: IDENTIFY DEVICE results we will examine
529 * @s: string into which data is output
530 * @ofs: offset into identify device page
531 * @len: length of string to return. must be an odd number.
533 * This function is identical to ata_id_string except that it
534 * trims trailing spaces and terminates the resulting string with
535 * null. @len must be actual maximum length (even number) + 1.
540 void ata_id_c_string(const u16 *id, unsigned char *s,
541 unsigned int ofs, unsigned int len)
547 ata_id_string(id, s, ofs, len - 1);
549 p = s + strnlen(s, len - 1);
550 while (p > s && p[-1] == ' ')
555 static u64 ata_id_n_sectors(const u16 *id)
557 if (ata_id_has_lba(id)) {
558 if (ata_id_has_lba48(id))
559 return ata_id_u64(id, 100);
561 return ata_id_u32(id, 60);
563 if (ata_id_current_chs_valid(id))
564 return ata_id_u32(id, 57);
566 return id[1] * id[3] * id[6];
571 * ata_noop_dev_select - Select device 0/1 on ATA bus
572 * @ap: ATA channel to manipulate
573 * @device: ATA device (numbered from zero) to select
575 * This function performs no actual function.
577 * May be used as the dev_select() entry in ata_port_operations.
582 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
588 * ata_std_dev_select - Select device 0/1 on ATA bus
589 * @ap: ATA channel to manipulate
590 * @device: ATA device (numbered from zero) to select
592 * Use the method defined in the ATA specification to
593 * make either device 0, or device 1, active on the
594 * ATA channel. Works with both PIO and MMIO.
596 * May be used as the dev_select() entry in ata_port_operations.
602 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
607 tmp = ATA_DEVICE_OBS;
609 tmp = ATA_DEVICE_OBS | ATA_DEV1;
611 if (ap->flags & ATA_FLAG_MMIO) {
612 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
614 outb(tmp, ap->ioaddr.device_addr);
616 ata_pause(ap); /* needed; also flushes, for mmio */
620 * ata_dev_select - Select device 0/1 on ATA bus
621 * @ap: ATA channel to manipulate
622 * @device: ATA device (numbered from zero) to select
623 * @wait: non-zero to wait for Status register BSY bit to clear
624 * @can_sleep: non-zero if context allows sleeping
626 * Use the method defined in the ATA specification to
627 * make either device 0, or device 1, active on the
630 * This is a high-level version of ata_std_dev_select(),
631 * which additionally provides the services of inserting
632 * the proper pauses and status polling, where needed.
638 void ata_dev_select(struct ata_port *ap, unsigned int device,
639 unsigned int wait, unsigned int can_sleep)
641 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
642 ap->id, device, wait);
647 ap->ops->dev_select(ap, device);
650 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
657 * ata_dump_id - IDENTIFY DEVICE info debugging output
658 * @id: IDENTIFY DEVICE page to dump
660 * Dump selected 16-bit words from the given IDENTIFY DEVICE
667 static inline void ata_dump_id(const u16 *id)
669 DPRINTK("49==0x%04x "
679 DPRINTK("80==0x%04x "
689 DPRINTK("88==0x%04x "
696 * Compute the PIO modes available for this device. This is not as
697 * trivial as it seems if we must consider early devices correctly.
699 * FIXME: pre IDE drive timing (do we care ?).
702 static unsigned int ata_pio_modes(const struct ata_device *adev)
706 /* Usual case. Word 53 indicates word 64 is valid */
707 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
708 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
714 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
715 number for the maximum. Turn it into a mask and return it */
716 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
718 /* But wait.. there's more. Design your standards by committee and
719 you too can get a free iordy field to process. However its the
720 speeds not the modes that are supported... Note drivers using the
721 timing API will get this right anyway */
725 ata_queue_packet_task(struct ata_port *ap)
727 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
728 queue_work(ata_wq, &ap->packet_task);
732 ata_queue_pio_task(struct ata_port *ap)
734 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
735 queue_work(ata_wq, &ap->pio_task);
739 ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
741 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
742 queue_delayed_work(ata_wq, &ap->pio_task, delay);
746 * ata_flush_pio_tasks - Flush pio_task and packet_task
747 * @ap: the target ata_port
749 * After this function completes, pio_task and packet_task are
750 * guranteed not to be running or scheduled.
753 * Kernel thread context (may sleep)
756 static void ata_flush_pio_tasks(struct ata_port *ap)
763 spin_lock_irqsave(&ap->host_set->lock, flags);
764 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
765 spin_unlock_irqrestore(&ap->host_set->lock, flags);
767 DPRINTK("flush #1\n");
768 flush_workqueue(ata_wq);
771 * At this point, if a task is running, it's guaranteed to see
772 * the FLUSH flag; thus, it will never queue pio tasks again.
775 tmp |= cancel_delayed_work(&ap->pio_task);
776 tmp |= cancel_delayed_work(&ap->packet_task);
778 DPRINTK("flush #2\n");
779 flush_workqueue(ata_wq);
782 spin_lock_irqsave(&ap->host_set->lock, flags);
783 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
784 spin_unlock_irqrestore(&ap->host_set->lock, flags);
789 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
791 struct completion *waiting = qc->private_data;
793 qc->ap->ops->tf_read(qc->ap, &qc->tf);
798 * ata_exec_internal - execute libata internal command
799 * @ap: Port to which the command is sent
800 * @dev: Device to which the command is sent
801 * @tf: Taskfile registers for the command and the result
802 * @dma_dir: Data tranfer direction of the command
803 * @buf: Data buffer of the command
804 * @buflen: Length of data buffer
806 * Executes libata internal command with timeout. @tf contains
807 * command on entry and result on return. Timeout and error
808 * conditions are reported via return value. No recovery action
809 * is taken after a command times out. It's caller's duty to
810 * clean up after timeout.
813 * None. Should be called with kernel context, might sleep.
817 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
818 struct ata_taskfile *tf,
819 int dma_dir, void *buf, unsigned int buflen)
821 u8 command = tf->command;
822 struct ata_queued_cmd *qc;
823 DECLARE_COMPLETION(wait);
825 unsigned int err_mask;
827 spin_lock_irqsave(&ap->host_set->lock, flags);
829 qc = ata_qc_new_init(ap, dev);
833 qc->dma_dir = dma_dir;
834 if (dma_dir != DMA_NONE) {
835 ata_sg_init_one(qc, buf, buflen);
836 qc->nsect = buflen / ATA_SECT_SIZE;
839 qc->private_data = &wait;
840 qc->complete_fn = ata_qc_complete_internal;
842 qc->err_mask = ata_qc_issue(qc);
846 spin_unlock_irqrestore(&ap->host_set->lock, flags);
848 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
849 spin_lock_irqsave(&ap->host_set->lock, flags);
851 /* We're racing with irq here. If we lose, the
852 * following test prevents us from completing the qc
853 * again. If completion irq occurs after here but
854 * before the caller cleans up, it will result in a
855 * spurious interrupt. We can live with that.
857 if (qc->flags & ATA_QCFLAG_ACTIVE) {
858 qc->err_mask = AC_ERR_TIMEOUT;
860 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
864 spin_unlock_irqrestore(&ap->host_set->lock, flags);
868 err_mask = qc->err_mask;
876 * ata_pio_need_iordy - check if iordy needed
879 * Check if the current speed of the device requires IORDY. Used
880 * by various controllers for chip configuration.
883 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
886 int speed = adev->pio_mode - XFER_PIO_0;
893 /* If we have no drive specific rule, then PIO 2 is non IORDY */
895 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
896 pio = adev->id[ATA_ID_EIDE_PIO];
897 /* Is the speed faster than the drive allows non IORDY ? */
899 /* This is cycle times not frequency - watch the logic! */
900 if (pio > 240) /* PIO2 is 240nS per cycle */
909 * ata_dev_read_id - Read ID data from the specified device
910 * @ap: port on which target device resides
911 * @dev: target device
912 * @p_class: pointer to class of the target device (may be changed)
913 * @post_reset: is this read ID post-reset?
914 * @p_id: read IDENTIFY page (newly allocated)
916 * Read ID data from the specified device. ATA_CMD_ID_ATA is
917 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
918 * devices. This function also takes care of EDD signature
919 * misreporting (to be removed once EDD support is gone) and
920 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
923 * Kernel thread context (may sleep)
926 * 0 on success, -errno otherwise.
928 static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
929 unsigned int *p_class, int post_reset, u16 **p_id)
931 unsigned int class = *p_class;
932 unsigned int using_edd;
933 struct ata_taskfile tf;
934 unsigned int err_mask = 0;
939 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
941 if (ap->ops->probe_reset ||
942 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
947 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
949 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
952 reason = "out of memory";
957 ata_tf_init(ap, &tf, dev->devno);
961 tf.command = ATA_CMD_ID_ATA;
964 tf.command = ATA_CMD_ID_ATAPI;
968 reason = "unsupported class";
972 tf.protocol = ATA_PROT_PIO;
974 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
975 id, sizeof(id[0]) * ATA_ID_WORDS);
979 reason = "I/O error";
981 if (err_mask & ~AC_ERR_DEV)
985 * arg! EDD works for all test cases, but seems to return
986 * the ATA signature for some ATAPI devices. Until the
987 * reason for this is found and fixed, we fix up the mess
988 * here. If IDENTIFY DEVICE returns command aborted
989 * (as ATAPI devices do), then we issue an
990 * IDENTIFY PACKET DEVICE.
992 * ATA software reset (SRST, the default) does not appear
993 * to have this problem.
995 if ((using_edd) && (class == ATA_DEV_ATA)) {
997 if (err & ATA_ABORTED) {
998 class = ATA_DEV_ATAPI;
1005 swap_buf_le16(id, ATA_ID_WORDS);
1007 /* print device capabilities */
1008 printk(KERN_DEBUG "ata%u: dev %u cfg "
1009 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1011 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1014 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1016 reason = "device reports illegal type";
1020 if (post_reset && class == ATA_DEV_ATA) {
1022 * The exact sequence expected by certain pre-ATA4 drives is:
1025 * INITIALIZE DEVICE PARAMETERS
1027 * Some drives were very specific about that exact sequence.
1029 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1030 err_mask = ata_dev_init_params(ap, dev);
1033 reason = "INIT_DEV_PARAMS failed";
1037 /* current CHS translation info (id[53-58]) might be
1038 * changed. reread the identify device info.
1050 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1051 ap->id, dev->devno, reason);
1056 static inline u8 ata_dev_knobble(const struct ata_port *ap,
1057 struct ata_device *dev)
1059 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1063 * ata_dev_configure - Configure the specified ATA/ATAPI device
1064 * @ap: Port on which target device resides
1065 * @dev: Target device to configure
1067 * Configure @dev according to @dev->id. Generic and low-level
1068 * driver specific fixups are also applied.
1071 * Kernel thread context (may sleep)
1074 * 0 on success, -errno otherwise
1076 static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev)
1078 unsigned long xfer_modes;
1081 if (!ata_dev_present(dev)) {
1082 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1083 ap->id, dev->devno);
1087 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1090 * common ATA, ATAPI feature tests
1093 /* we require DMA support (bits 8 of word 49) */
1094 if (!ata_id_has_dma(dev->id)) {
1095 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1100 /* quick-n-dirty find max transfer mode; for printk only */
1101 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1103 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1105 xfer_modes = ata_pio_modes(dev);
1107 ata_dump_id(dev->id);
1109 /* ATA-specific feature tests */
1110 if (dev->class == ATA_DEV_ATA) {
1111 dev->n_sectors = ata_id_n_sectors(dev->id);
1113 if (ata_id_has_lba(dev->id)) {
1114 dev->flags |= ATA_DFLAG_LBA;
1116 if (ata_id_has_lba48(dev->id))
1117 dev->flags |= ATA_DFLAG_LBA48;
1119 /* print device info to dmesg */
1120 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1122 ata_id_major_version(dev->id),
1123 ata_mode_string(xfer_modes),
1124 (unsigned long long)dev->n_sectors,
1125 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1129 /* Default translation */
1130 dev->cylinders = dev->id[1];
1131 dev->heads = dev->id[3];
1132 dev->sectors = dev->id[6];
1134 if (ata_id_current_chs_valid(dev->id)) {
1135 /* Current CHS translation is valid. */
1136 dev->cylinders = dev->id[54];
1137 dev->heads = dev->id[55];
1138 dev->sectors = dev->id[56];
1141 /* print device info to dmesg */
1142 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1144 ata_id_major_version(dev->id),
1145 ata_mode_string(xfer_modes),
1146 (unsigned long long)dev->n_sectors,
1147 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1154 /* ATAPI-specific feature tests */
1155 else if (dev->class == ATA_DEV_ATAPI) {
1156 rc = atapi_cdb_len(dev->id);
1157 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1158 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1162 dev->cdb_len = (unsigned int) rc;
1164 /* print device info to dmesg */
1165 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1167 ata_mode_string(xfer_modes));
1170 ap->host->max_cmd_len = 0;
1171 for (i = 0; i < ATA_MAX_DEVICES; i++)
1172 ap->host->max_cmd_len = max_t(unsigned int,
1173 ap->host->max_cmd_len,
1174 ap->device[i].cdb_len);
1176 /* limit bridge transfers to udma5, 200 sectors */
1177 if (ata_dev_knobble(ap, dev)) {
1178 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1179 ap->id, dev->devno);
1180 ap->udma_mask &= ATA_UDMA5;
1181 dev->max_sectors = ATA_MAX_SECTORS;
1184 if (ap->ops->dev_config)
1185 ap->ops->dev_config(ap, dev);
1187 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1191 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1192 ap->id, dev->devno);
1193 DPRINTK("EXIT, err\n");
1198 * ata_bus_probe - Reset and probe ATA bus
1201 * Master ATA bus probing function. Initiates a hardware-dependent
1202 * bus reset, then attempts to identify any devices found on
1206 * PCI/etc. bus probe sem.
1209 * Zero on success, non-zero on error.
1212 static int ata_bus_probe(struct ata_port *ap)
1214 unsigned int classes[ATA_MAX_DEVICES];
1215 unsigned int i, rc, found = 0;
1220 if (ap->ops->probe_reset) {
1221 rc = ap->ops->probe_reset(ap, classes);
1223 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1227 for (i = 0; i < ATA_MAX_DEVICES; i++)
1228 if (classes[i] == ATA_DEV_UNKNOWN)
1229 classes[i] = ATA_DEV_NONE;
1231 ap->ops->phy_reset(ap);
1233 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1234 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1235 classes[i] = ap->device[i].class;
1237 ap->device[i].class = ATA_DEV_UNKNOWN;
1242 /* read IDENTIFY page and configure devices */
1243 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1244 struct ata_device *dev = &ap->device[i];
1246 dev->class = classes[i];
1248 if (!ata_dev_present(dev))
1251 WARN_ON(dev->id != NULL);
1252 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1253 dev->class = ATA_DEV_NONE;
1257 if (ata_dev_configure(ap, dev)) {
1258 dev->class++; /* disable device */
1266 goto err_out_disable;
1269 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1270 goto err_out_disable;
1275 ap->ops->port_disable(ap);
1280 * ata_port_probe - Mark port as enabled
1281 * @ap: Port for which we indicate enablement
1283 * Modify @ap data structure such that the system
1284 * thinks that the entire port is enabled.
1286 * LOCKING: host_set lock, or some other form of
1290 void ata_port_probe(struct ata_port *ap)
1292 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1296 * sata_print_link_status - Print SATA link status
1297 * @ap: SATA port to printk link status about
1299 * This function prints link speed and status of a SATA link.
1304 static void sata_print_link_status(struct ata_port *ap)
1309 if (!ap->ops->scr_read)
1312 sstatus = scr_read(ap, SCR_STATUS);
1314 if (sata_dev_present(ap)) {
1315 tmp = (sstatus >> 4) & 0xf;
1318 else if (tmp & (1 << 1))
1321 speed = "<unknown>";
1322 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1323 ap->id, speed, sstatus);
1325 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1331 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1332 * @ap: SATA port associated with target SATA PHY.
1334 * This function issues commands to standard SATA Sxxx
1335 * PHY registers, to wake up the phy (and device), and
1336 * clear any reset condition.
1339 * PCI/etc. bus probe sem.
1342 void __sata_phy_reset(struct ata_port *ap)
1345 unsigned long timeout = jiffies + (HZ * 5);
1347 if (ap->flags & ATA_FLAG_SATA_RESET) {
1348 /* issue phy wake/reset */
1349 scr_write_flush(ap, SCR_CONTROL, 0x301);
1350 /* Couldn't find anything in SATA I/II specs, but
1351 * AHCI-1.1 10.4.2 says at least 1 ms. */
1354 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1356 /* wait for phy to become ready, if necessary */
1359 sstatus = scr_read(ap, SCR_STATUS);
1360 if ((sstatus & 0xf) != 1)
1362 } while (time_before(jiffies, timeout));
1364 /* print link status */
1365 sata_print_link_status(ap);
1367 /* TODO: phy layer with polling, timeouts, etc. */
1368 if (sata_dev_present(ap))
1371 ata_port_disable(ap);
1373 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1376 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1377 ata_port_disable(ap);
1381 ap->cbl = ATA_CBL_SATA;
1385 * sata_phy_reset - Reset SATA bus.
1386 * @ap: SATA port associated with target SATA PHY.
1388 * This function resets the SATA bus, and then probes
1389 * the bus for devices.
1392 * PCI/etc. bus probe sem.
1395 void sata_phy_reset(struct ata_port *ap)
1397 __sata_phy_reset(ap);
1398 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1404 * ata_port_disable - Disable port.
1405 * @ap: Port to be disabled.
1407 * Modify @ap data structure such that the system
1408 * thinks that the entire port is disabled, and should
1409 * never attempt to probe or communicate with devices
1412 * LOCKING: host_set lock, or some other form of
1416 void ata_port_disable(struct ata_port *ap)
1418 ap->device[0].class = ATA_DEV_NONE;
1419 ap->device[1].class = ATA_DEV_NONE;
1420 ap->flags |= ATA_FLAG_PORT_DISABLED;
1424 * This mode timing computation functionality is ported over from
1425 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1428 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1429 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1430 * for PIO 5, which is a nonstandard extension and UDMA6, which
1431 * is currently supported only by Maxtor drives.
1434 static const struct ata_timing ata_timing[] = {
1436 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1437 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1438 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1439 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1441 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1442 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1443 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1445 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1447 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1448 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1449 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1451 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1452 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1453 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1455 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1456 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1457 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1459 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1460 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1461 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1463 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1468 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1469 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1471 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1473 q->setup = EZ(t->setup * 1000, T);
1474 q->act8b = EZ(t->act8b * 1000, T);
1475 q->rec8b = EZ(t->rec8b * 1000, T);
1476 q->cyc8b = EZ(t->cyc8b * 1000, T);
1477 q->active = EZ(t->active * 1000, T);
1478 q->recover = EZ(t->recover * 1000, T);
1479 q->cycle = EZ(t->cycle * 1000, T);
1480 q->udma = EZ(t->udma * 1000, UT);
1483 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1484 struct ata_timing *m, unsigned int what)
1486 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1487 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1488 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1489 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1490 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1491 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1492 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1493 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1496 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1498 const struct ata_timing *t;
1500 for (t = ata_timing; t->mode != speed; t++)
1501 if (t->mode == 0xFF)
1506 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1507 struct ata_timing *t, int T, int UT)
1509 const struct ata_timing *s;
1510 struct ata_timing p;
1516 if (!(s = ata_timing_find_mode(speed)))
1519 memcpy(t, s, sizeof(*s));
1522 * If the drive is an EIDE drive, it can tell us it needs extended
1523 * PIO/MW_DMA cycle timing.
1526 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1527 memset(&p, 0, sizeof(p));
1528 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1529 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1530 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1531 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1532 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1534 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1538 * Convert the timing to bus clock counts.
1541 ata_timing_quantize(t, t, T, UT);
1544 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1545 * S.M.A.R.T * and some other commands. We have to ensure that the
1546 * DMA cycle timing is slower/equal than the fastest PIO timing.
1549 if (speed > XFER_PIO_4) {
1550 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1551 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1555 * Lengthen active & recovery time so that cycle time is correct.
1558 if (t->act8b + t->rec8b < t->cyc8b) {
1559 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1560 t->rec8b = t->cyc8b - t->act8b;
1563 if (t->active + t->recover < t->cycle) {
1564 t->active += (t->cycle - (t->active + t->recover)) / 2;
1565 t->recover = t->cycle - t->active;
1571 static const struct {
1574 } xfer_mode_classes[] = {
1575 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1576 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1577 { ATA_SHIFT_PIO, XFER_PIO_0 },
1580 static u8 base_from_shift(unsigned int shift)
1584 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1585 if (xfer_mode_classes[i].shift == shift)
1586 return xfer_mode_classes[i].base;
1591 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1596 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1599 if (dev->xfer_shift == ATA_SHIFT_PIO)
1600 dev->flags |= ATA_DFLAG_PIO;
1602 ata_dev_set_xfermode(ap, dev);
1604 base = base_from_shift(dev->xfer_shift);
1605 ofs = dev->xfer_mode - base;
1606 idx = ofs + dev->xfer_shift;
1607 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1609 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1610 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1612 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1613 ap->id, dev->devno, xfer_mode_str[idx]);
1616 static int ata_host_set_pio(struct ata_port *ap)
1622 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1625 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1629 base = base_from_shift(ATA_SHIFT_PIO);
1630 xfer_mode = base + x;
1632 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1633 (int)base, (int)xfer_mode, mask, x);
1635 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1636 struct ata_device *dev = &ap->device[i];
1637 if (ata_dev_present(dev)) {
1638 dev->pio_mode = xfer_mode;
1639 dev->xfer_mode = xfer_mode;
1640 dev->xfer_shift = ATA_SHIFT_PIO;
1641 if (ap->ops->set_piomode)
1642 ap->ops->set_piomode(ap, dev);
1649 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1650 unsigned int xfer_shift)
1654 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1655 struct ata_device *dev = &ap->device[i];
1656 if (ata_dev_present(dev)) {
1657 dev->dma_mode = xfer_mode;
1658 dev->xfer_mode = xfer_mode;
1659 dev->xfer_shift = xfer_shift;
1660 if (ap->ops->set_dmamode)
1661 ap->ops->set_dmamode(ap, dev);
1667 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1668 * @ap: port on which timings will be programmed
1670 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1673 * PCI/etc. bus probe sem.
1675 static void ata_set_mode(struct ata_port *ap)
1677 unsigned int xfer_shift;
1681 /* step 1: always set host PIO timings */
1682 rc = ata_host_set_pio(ap);
1686 /* step 2: choose the best data xfer mode */
1687 xfer_mode = xfer_shift = 0;
1688 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1692 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1693 if (xfer_shift != ATA_SHIFT_PIO)
1694 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1696 /* step 4: update devices' xfer mode */
1697 ata_dev_set_mode(ap, &ap->device[0]);
1698 ata_dev_set_mode(ap, &ap->device[1]);
1700 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1703 if (ap->ops->post_set_mode)
1704 ap->ops->post_set_mode(ap);
1709 ata_port_disable(ap);
1713 * ata_tf_to_host - issue ATA taskfile to host controller
1714 * @ap: port to which command is being issued
1715 * @tf: ATA taskfile register set
1717 * Issues ATA taskfile register set to ATA host controller,
1718 * with proper synchronization with interrupt handler and
1722 * spin_lock_irqsave(host_set lock)
1725 static inline void ata_tf_to_host(struct ata_port *ap,
1726 const struct ata_taskfile *tf)
1728 ap->ops->tf_load(ap, tf);
1729 ap->ops->exec_command(ap, tf);
1733 * ata_busy_sleep - sleep until BSY clears, or timeout
1734 * @ap: port containing status register to be polled
1735 * @tmout_pat: impatience timeout
1736 * @tmout: overall timeout
1738 * Sleep until ATA Status register bit BSY clears,
1739 * or a timeout occurs.
1744 unsigned int ata_busy_sleep (struct ata_port *ap,
1745 unsigned long tmout_pat, unsigned long tmout)
1747 unsigned long timer_start, timeout;
1750 status = ata_busy_wait(ap, ATA_BUSY, 300);
1751 timer_start = jiffies;
1752 timeout = timer_start + tmout_pat;
1753 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1755 status = ata_busy_wait(ap, ATA_BUSY, 3);
1758 if (status & ATA_BUSY)
1759 printk(KERN_WARNING "ata%u is slow to respond, "
1760 "please be patient\n", ap->id);
1762 timeout = timer_start + tmout;
1763 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1765 status = ata_chk_status(ap);
1768 if (status & ATA_BUSY) {
1769 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1770 ap->id, tmout / HZ);
1777 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1779 struct ata_ioports *ioaddr = &ap->ioaddr;
1780 unsigned int dev0 = devmask & (1 << 0);
1781 unsigned int dev1 = devmask & (1 << 1);
1782 unsigned long timeout;
1784 /* if device 0 was found in ata_devchk, wait for its
1788 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1790 /* if device 1 was found in ata_devchk, wait for
1791 * register access, then wait for BSY to clear
1793 timeout = jiffies + ATA_TMOUT_BOOT;
1797 ap->ops->dev_select(ap, 1);
1798 if (ap->flags & ATA_FLAG_MMIO) {
1799 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1800 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1802 nsect = inb(ioaddr->nsect_addr);
1803 lbal = inb(ioaddr->lbal_addr);
1805 if ((nsect == 1) && (lbal == 1))
1807 if (time_after(jiffies, timeout)) {
1811 msleep(50); /* give drive a breather */
1814 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1816 /* is all this really necessary? */
1817 ap->ops->dev_select(ap, 0);
1819 ap->ops->dev_select(ap, 1);
1821 ap->ops->dev_select(ap, 0);
1825 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1826 * @ap: Port to reset and probe
1828 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1829 * probe the bus. Not often used these days.
1832 * PCI/etc. bus probe sem.
1833 * Obtains host_set lock.
1837 static unsigned int ata_bus_edd(struct ata_port *ap)
1839 struct ata_taskfile tf;
1840 unsigned long flags;
1842 /* set up execute-device-diag (bus reset) taskfile */
1843 /* also, take interrupts to a known state (disabled) */
1844 DPRINTK("execute-device-diag\n");
1845 ata_tf_init(ap, &tf, 0);
1847 tf.command = ATA_CMD_EDD;
1848 tf.protocol = ATA_PROT_NODATA;
1851 spin_lock_irqsave(&ap->host_set->lock, flags);
1852 ata_tf_to_host(ap, &tf);
1853 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1855 /* spec says at least 2ms. but who knows with those
1856 * crazy ATAPI devices...
1860 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1863 static unsigned int ata_bus_softreset(struct ata_port *ap,
1864 unsigned int devmask)
1866 struct ata_ioports *ioaddr = &ap->ioaddr;
1868 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1870 /* software reset. causes dev0 to be selected */
1871 if (ap->flags & ATA_FLAG_MMIO) {
1872 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1873 udelay(20); /* FIXME: flush */
1874 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1875 udelay(20); /* FIXME: flush */
1876 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1878 outb(ap->ctl, ioaddr->ctl_addr);
1880 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1882 outb(ap->ctl, ioaddr->ctl_addr);
1885 /* spec mandates ">= 2ms" before checking status.
1886 * We wait 150ms, because that was the magic delay used for
1887 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1888 * between when the ATA command register is written, and then
1889 * status is checked. Because waiting for "a while" before
1890 * checking status is fine, post SRST, we perform this magic
1891 * delay here as well.
1895 ata_bus_post_reset(ap, devmask);
1901 * ata_bus_reset - reset host port and associated ATA channel
1902 * @ap: port to reset
1904 * This is typically the first time we actually start issuing
1905 * commands to the ATA channel. We wait for BSY to clear, then
1906 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1907 * result. Determine what devices, if any, are on the channel
1908 * by looking at the device 0/1 error register. Look at the signature
1909 * stored in each device's taskfile registers, to determine if
1910 * the device is ATA or ATAPI.
1913 * PCI/etc. bus probe sem.
1914 * Obtains host_set lock.
1917 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1920 void ata_bus_reset(struct ata_port *ap)
1922 struct ata_ioports *ioaddr = &ap->ioaddr;
1923 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1925 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1927 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1929 /* determine if device 0/1 are present */
1930 if (ap->flags & ATA_FLAG_SATA_RESET)
1933 dev0 = ata_devchk(ap, 0);
1935 dev1 = ata_devchk(ap, 1);
1939 devmask |= (1 << 0);
1941 devmask |= (1 << 1);
1943 /* select device 0 again */
1944 ap->ops->dev_select(ap, 0);
1946 /* issue bus reset */
1947 if (ap->flags & ATA_FLAG_SRST)
1948 rc = ata_bus_softreset(ap, devmask);
1949 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1950 /* set up device control */
1951 if (ap->flags & ATA_FLAG_MMIO)
1952 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1954 outb(ap->ctl, ioaddr->ctl_addr);
1955 rc = ata_bus_edd(ap);
1962 * determine by signature whether we have ATA or ATAPI devices
1964 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1965 if ((slave_possible) && (err != 0x81))
1966 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1968 /* re-enable interrupts */
1969 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1972 /* is double-select really necessary? */
1973 if (ap->device[1].class != ATA_DEV_NONE)
1974 ap->ops->dev_select(ap, 1);
1975 if (ap->device[0].class != ATA_DEV_NONE)
1976 ap->ops->dev_select(ap, 0);
1978 /* if no devices were detected, disable this port */
1979 if ((ap->device[0].class == ATA_DEV_NONE) &&
1980 (ap->device[1].class == ATA_DEV_NONE))
1983 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1984 /* set up device control for ATA_FLAG_SATA_RESET */
1985 if (ap->flags & ATA_FLAG_MMIO)
1986 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1988 outb(ap->ctl, ioaddr->ctl_addr);
1995 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1996 ap->ops->port_disable(ap);
2001 static int sata_phy_resume(struct ata_port *ap)
2003 unsigned long timeout = jiffies + (HZ * 5);
2006 scr_write_flush(ap, SCR_CONTROL, 0x300);
2008 /* Wait for phy to become ready, if necessary. */
2011 sstatus = scr_read(ap, SCR_STATUS);
2012 if ((sstatus & 0xf) != 1)
2014 } while (time_before(jiffies, timeout));
2020 * ata_std_probeinit - initialize probing
2021 * @ap: port to be probed
2023 * @ap is about to be probed. Initialize it. This function is
2024 * to be used as standard callback for ata_drive_probe_reset().
2026 * NOTE!!! Do not use this function as probeinit if a low level
2027 * driver implements only hardreset. Just pass NULL as probeinit
2028 * in that case. Using this function is probably okay but doing
2029 * so makes reset sequence different from the original
2030 * ->phy_reset implementation and Jeff nervous. :-P
2032 extern void ata_std_probeinit(struct ata_port *ap)
2034 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
2035 sata_phy_resume(ap);
2036 if (sata_dev_present(ap))
2037 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2042 * ata_std_softreset - reset host port via ATA SRST
2043 * @ap: port to reset
2044 * @verbose: fail verbosely
2045 * @classes: resulting classes of attached devices
2047 * Reset host port using ATA SRST. This function is to be used
2048 * as standard callback for ata_drive_*_reset() functions.
2051 * Kernel thread context (may sleep)
2054 * 0 on success, -errno otherwise.
2056 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2058 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2059 unsigned int devmask = 0, err_mask;
2064 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2065 classes[0] = ATA_DEV_NONE;
2069 /* determine if device 0/1 are present */
2070 if (ata_devchk(ap, 0))
2071 devmask |= (1 << 0);
2072 if (slave_possible && ata_devchk(ap, 1))
2073 devmask |= (1 << 1);
2075 /* select device 0 again */
2076 ap->ops->dev_select(ap, 0);
2078 /* issue bus reset */
2079 DPRINTK("about to softreset, devmask=%x\n", devmask);
2080 err_mask = ata_bus_softreset(ap, devmask);
2083 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2086 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2091 /* determine by signature whether we have ATA or ATAPI devices */
2092 classes[0] = ata_dev_try_classify(ap, 0, &err);
2093 if (slave_possible && err != 0x81)
2094 classes[1] = ata_dev_try_classify(ap, 1, &err);
2097 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2102 * sata_std_hardreset - reset host port via SATA phy reset
2103 * @ap: port to reset
2104 * @verbose: fail verbosely
2105 * @class: resulting class of attached device
2107 * SATA phy-reset host port using DET bits of SControl register.
2108 * This function is to be used as standard callback for
2109 * ata_drive_*_reset().
2112 * Kernel thread context (may sleep)
2115 * 0 on success, -errno otherwise.
2117 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2121 /* Issue phy wake/reset */
2122 scr_write_flush(ap, SCR_CONTROL, 0x301);
2125 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2126 * 10.4.2 says at least 1 ms.
2130 /* Bring phy back */
2131 sata_phy_resume(ap);
2133 /* TODO: phy layer with polling, timeouts, etc. */
2134 if (!sata_dev_present(ap)) {
2135 *class = ATA_DEV_NONE;
2136 DPRINTK("EXIT, link offline\n");
2140 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2142 printk(KERN_ERR "ata%u: COMRESET failed "
2143 "(device not ready)\n", ap->id);
2145 DPRINTK("EXIT, device not ready\n");
2149 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2151 *class = ata_dev_try_classify(ap, 0, NULL);
2153 DPRINTK("EXIT, class=%u\n", *class);
2158 * ata_std_postreset - standard postreset callback
2159 * @ap: the target ata_port
2160 * @classes: classes of attached devices
2162 * This function is invoked after a successful reset. Note that
2163 * the device might have been reset more than once using
2164 * different reset methods before postreset is invoked.
2166 * This function is to be used as standard callback for
2167 * ata_drive_*_reset().
2170 * Kernel thread context (may sleep)
2172 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2176 /* set cable type if it isn't already set */
2177 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2178 ap->cbl = ATA_CBL_SATA;
2180 /* print link status */
2181 if (ap->cbl == ATA_CBL_SATA)
2182 sata_print_link_status(ap);
2184 /* re-enable interrupts */
2185 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2188 /* is double-select really necessary? */
2189 if (classes[0] != ATA_DEV_NONE)
2190 ap->ops->dev_select(ap, 1);
2191 if (classes[1] != ATA_DEV_NONE)
2192 ap->ops->dev_select(ap, 0);
2194 /* bail out if no device is present */
2195 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2196 DPRINTK("EXIT, no device\n");
2200 /* set up device control */
2201 if (ap->ioaddr.ctl_addr) {
2202 if (ap->flags & ATA_FLAG_MMIO)
2203 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2205 outb(ap->ctl, ap->ioaddr.ctl_addr);
2212 * ata_std_probe_reset - standard probe reset method
2213 * @ap: prot to perform probe-reset
2214 * @classes: resulting classes of attached devices
2216 * The stock off-the-shelf ->probe_reset method.
2219 * Kernel thread context (may sleep)
2222 * 0 on success, -errno otherwise.
2224 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2226 ata_reset_fn_t hardreset;
2229 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2230 hardreset = sata_std_hardreset;
2232 return ata_drive_probe_reset(ap, ata_std_probeinit,
2233 ata_std_softreset, hardreset,
2234 ata_std_postreset, classes);
2237 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2238 ata_postreset_fn_t postreset,
2239 unsigned int *classes)
2243 for (i = 0; i < ATA_MAX_DEVICES; i++)
2244 classes[i] = ATA_DEV_UNKNOWN;
2246 rc = reset(ap, 0, classes);
2250 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2251 * is complete and convert all ATA_DEV_UNKNOWN to
2254 for (i = 0; i < ATA_MAX_DEVICES; i++)
2255 if (classes[i] != ATA_DEV_UNKNOWN)
2258 if (i < ATA_MAX_DEVICES)
2259 for (i = 0; i < ATA_MAX_DEVICES; i++)
2260 if (classes[i] == ATA_DEV_UNKNOWN)
2261 classes[i] = ATA_DEV_NONE;
2264 postreset(ap, classes);
2266 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2270 * ata_drive_probe_reset - Perform probe reset with given methods
2271 * @ap: port to reset
2272 * @probeinit: probeinit method (can be NULL)
2273 * @softreset: softreset method (can be NULL)
2274 * @hardreset: hardreset method (can be NULL)
2275 * @postreset: postreset method (can be NULL)
2276 * @classes: resulting classes of attached devices
2278 * Reset the specified port and classify attached devices using
2279 * given methods. This function prefers softreset but tries all
2280 * possible reset sequences to reset and classify devices. This
2281 * function is intended to be used for constructing ->probe_reset
2282 * callback by low level drivers.
2284 * Reset methods should follow the following rules.
2286 * - Return 0 on sucess, -errno on failure.
2287 * - If classification is supported, fill classes[] with
2288 * recognized class codes.
2289 * - If classification is not supported, leave classes[] alone.
2290 * - If verbose is non-zero, print error message on failure;
2291 * otherwise, shut up.
2294 * Kernel thread context (may sleep)
2297 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2298 * if classification fails, and any error code from reset
2301 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2302 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2303 ata_postreset_fn_t postreset, unsigned int *classes)
2311 rc = do_probe_reset(ap, softreset, postreset, classes);
2319 rc = do_probe_reset(ap, hardreset, postreset, classes);
2320 if (rc == 0 || rc != -ENODEV)
2324 rc = do_probe_reset(ap, softreset, postreset, classes);
2329 static void ata_pr_blacklisted(const struct ata_port *ap,
2330 const struct ata_device *dev)
2332 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2333 ap->id, dev->devno);
2336 static const char * const ata_dma_blacklist [] = {
2355 "Toshiba CD-ROM XM-6202B",
2356 "TOSHIBA CD-ROM XM-1702BC",
2358 "E-IDE CD-ROM CR-840",
2361 "SAMSUNG CD-ROM SC-148C",
2362 "SAMSUNG CD-ROM SC",
2364 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2368 static int ata_dma_blacklisted(const struct ata_device *dev)
2370 unsigned char model_num[41];
2373 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
2375 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2376 if (!strcmp(ata_dma_blacklist[i], model_num))
2382 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2384 const struct ata_device *master, *slave;
2387 master = &ap->device[0];
2388 slave = &ap->device[1];
2390 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
2392 if (shift == ATA_SHIFT_UDMA) {
2393 mask = ap->udma_mask;
2394 if (ata_dev_present(master)) {
2395 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2396 if (ata_dma_blacklisted(master)) {
2398 ata_pr_blacklisted(ap, master);
2401 if (ata_dev_present(slave)) {
2402 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2403 if (ata_dma_blacklisted(slave)) {
2405 ata_pr_blacklisted(ap, slave);
2409 else if (shift == ATA_SHIFT_MWDMA) {
2410 mask = ap->mwdma_mask;
2411 if (ata_dev_present(master)) {
2412 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2413 if (ata_dma_blacklisted(master)) {
2415 ata_pr_blacklisted(ap, master);
2418 if (ata_dev_present(slave)) {
2419 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2420 if (ata_dma_blacklisted(slave)) {
2422 ata_pr_blacklisted(ap, slave);
2426 else if (shift == ATA_SHIFT_PIO) {
2427 mask = ap->pio_mask;
2428 if (ata_dev_present(master)) {
2429 /* spec doesn't return explicit support for
2430 * PIO0-2, so we fake it
2432 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2437 if (ata_dev_present(slave)) {
2438 /* spec doesn't return explicit support for
2439 * PIO0-2, so we fake it
2441 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2448 mask = 0xffffffff; /* shut up compiler warning */
2455 /* find greatest bit */
2456 static int fgb(u32 bitmap)
2461 for (i = 0; i < 32; i++)
2462 if (bitmap & (1 << i))
2469 * ata_choose_xfer_mode - attempt to find best transfer mode
2470 * @ap: Port for which an xfer mode will be selected
2471 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2472 * @xfer_shift_out: (output) bit shift that selects this mode
2474 * Based on host and device capabilities, determine the
2475 * maximum transfer mode that is amenable to all.
2478 * PCI/etc. bus probe sem.
2481 * Zero on success, negative on error.
2484 static int ata_choose_xfer_mode(const struct ata_port *ap,
2486 unsigned int *xfer_shift_out)
2488 unsigned int mask, shift;
2491 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2492 shift = xfer_mode_classes[i].shift;
2493 mask = ata_get_mode_mask(ap, shift);
2497 *xfer_mode_out = xfer_mode_classes[i].base + x;
2498 *xfer_shift_out = shift;
2507 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2508 * @ap: Port associated with device @dev
2509 * @dev: Device to which command will be sent
2511 * Issue SET FEATURES - XFER MODE command to device @dev
2515 * PCI/etc. bus probe sem.
2518 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2520 struct ata_taskfile tf;
2522 /* set up set-features taskfile */
2523 DPRINTK("set features - xfer mode\n");
2525 ata_tf_init(ap, &tf, dev->devno);
2526 tf.command = ATA_CMD_SET_FEATURES;
2527 tf.feature = SETFEATURES_XFER;
2528 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2529 tf.protocol = ATA_PROT_NODATA;
2530 tf.nsect = dev->xfer_mode;
2532 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2533 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2535 ata_port_disable(ap);
2542 * ata_dev_init_params - Issue INIT DEV PARAMS command
2543 * @ap: Port associated with device @dev
2544 * @dev: Device to which command will be sent
2547 * Kernel thread context (may sleep)
2550 * 0 on success, AC_ERR_* mask otherwise.
2553 static unsigned int ata_dev_init_params(struct ata_port *ap,
2554 struct ata_device *dev)
2556 struct ata_taskfile tf;
2557 unsigned int err_mask;
2558 u16 sectors = dev->id[6];
2559 u16 heads = dev->id[3];
2561 /* Number of sectors per track 1-255. Number of heads 1-16 */
2562 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2565 /* set up init dev params taskfile */
2566 DPRINTK("init dev params \n");
2568 ata_tf_init(ap, &tf, dev->devno);
2569 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2570 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2571 tf.protocol = ATA_PROT_NODATA;
2573 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2575 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2577 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2582 * ata_sg_clean - Unmap DMA memory associated with command
2583 * @qc: Command containing DMA memory to be released
2585 * Unmap all mapped DMA memory associated with this command.
2588 * spin_lock_irqsave(host_set lock)
2591 static void ata_sg_clean(struct ata_queued_cmd *qc)
2593 struct ata_port *ap = qc->ap;
2594 struct scatterlist *sg = qc->__sg;
2595 int dir = qc->dma_dir;
2596 void *pad_buf = NULL;
2598 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2599 WARN_ON(sg == NULL);
2601 if (qc->flags & ATA_QCFLAG_SINGLE)
2602 WARN_ON(qc->n_elem > 1);
2604 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2606 /* if we padded the buffer out to 32-bit bound, and data
2607 * xfer direction is from-device, we must copy from the
2608 * pad buffer back into the supplied buffer
2610 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2611 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2613 if (qc->flags & ATA_QCFLAG_SG) {
2615 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2616 /* restore last sg */
2617 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2619 struct scatterlist *psg = &qc->pad_sgent;
2620 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2621 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2622 kunmap_atomic(addr, KM_IRQ0);
2626 dma_unmap_single(ap->host_set->dev,
2627 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2630 sg->length += qc->pad_len;
2632 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2633 pad_buf, qc->pad_len);
2636 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2641 * ata_fill_sg - Fill PCI IDE PRD table
2642 * @qc: Metadata associated with taskfile to be transferred
2644 * Fill PCI IDE PRD (scatter-gather) table with segments
2645 * associated with the current disk command.
2648 * spin_lock_irqsave(host_set lock)
2651 static void ata_fill_sg(struct ata_queued_cmd *qc)
2653 struct ata_port *ap = qc->ap;
2654 struct scatterlist *sg;
2657 WARN_ON(qc->__sg == NULL);
2658 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2661 ata_for_each_sg(sg, qc) {
2665 /* determine if physical DMA addr spans 64K boundary.
2666 * Note h/w doesn't support 64-bit, so we unconditionally
2667 * truncate dma_addr_t to u32.
2669 addr = (u32) sg_dma_address(sg);
2670 sg_len = sg_dma_len(sg);
2673 offset = addr & 0xffff;
2675 if ((offset + sg_len) > 0x10000)
2676 len = 0x10000 - offset;
2678 ap->prd[idx].addr = cpu_to_le32(addr);
2679 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2680 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2689 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2692 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2693 * @qc: Metadata associated with taskfile to check
2695 * Allow low-level driver to filter ATA PACKET commands, returning
2696 * a status indicating whether or not it is OK to use DMA for the
2697 * supplied PACKET command.
2700 * spin_lock_irqsave(host_set lock)
2702 * RETURNS: 0 when ATAPI DMA can be used
2705 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2707 struct ata_port *ap = qc->ap;
2708 int rc = 0; /* Assume ATAPI DMA is OK by default */
2710 if (ap->ops->check_atapi_dma)
2711 rc = ap->ops->check_atapi_dma(qc);
2716 * ata_qc_prep - Prepare taskfile for submission
2717 * @qc: Metadata associated with taskfile to be prepared
2719 * Prepare ATA taskfile for submission.
2722 * spin_lock_irqsave(host_set lock)
2724 void ata_qc_prep(struct ata_queued_cmd *qc)
2726 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2733 * ata_sg_init_one - Associate command with memory buffer
2734 * @qc: Command to be associated
2735 * @buf: Memory buffer
2736 * @buflen: Length of memory buffer, in bytes.
2738 * Initialize the data-related elements of queued_cmd @qc
2739 * to point to a single memory buffer, @buf of byte length @buflen.
2742 * spin_lock_irqsave(host_set lock)
2745 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2747 struct scatterlist *sg;
2749 qc->flags |= ATA_QCFLAG_SINGLE;
2751 memset(&qc->sgent, 0, sizeof(qc->sgent));
2752 qc->__sg = &qc->sgent;
2754 qc->orig_n_elem = 1;
2758 sg_init_one(sg, buf, buflen);
2762 * ata_sg_init - Associate command with scatter-gather table.
2763 * @qc: Command to be associated
2764 * @sg: Scatter-gather table.
2765 * @n_elem: Number of elements in s/g table.
2767 * Initialize the data-related elements of queued_cmd @qc
2768 * to point to a scatter-gather table @sg, containing @n_elem
2772 * spin_lock_irqsave(host_set lock)
2775 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2776 unsigned int n_elem)
2778 qc->flags |= ATA_QCFLAG_SG;
2780 qc->n_elem = n_elem;
2781 qc->orig_n_elem = n_elem;
2785 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2786 * @qc: Command with memory buffer to be mapped.
2788 * DMA-map the memory buffer associated with queued_cmd @qc.
2791 * spin_lock_irqsave(host_set lock)
2794 * Zero on success, negative on error.
2797 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2799 struct ata_port *ap = qc->ap;
2800 int dir = qc->dma_dir;
2801 struct scatterlist *sg = qc->__sg;
2802 dma_addr_t dma_address;
2805 /* we must lengthen transfers to end on a 32-bit boundary */
2806 qc->pad_len = sg->length & 3;
2808 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2809 struct scatterlist *psg = &qc->pad_sgent;
2811 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2813 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2815 if (qc->tf.flags & ATA_TFLAG_WRITE)
2816 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2819 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2820 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2822 sg->length -= qc->pad_len;
2823 if (sg->length == 0)
2826 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2827 sg->length, qc->pad_len);
2835 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2837 if (dma_mapping_error(dma_address)) {
2839 sg->length += qc->pad_len;
2843 sg_dma_address(sg) = dma_address;
2844 sg_dma_len(sg) = sg->length;
2847 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2848 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2854 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2855 * @qc: Command with scatter-gather table to be mapped.
2857 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2860 * spin_lock_irqsave(host_set lock)
2863 * Zero on success, negative on error.
2867 static int ata_sg_setup(struct ata_queued_cmd *qc)
2869 struct ata_port *ap = qc->ap;
2870 struct scatterlist *sg = qc->__sg;
2871 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2872 int n_elem, pre_n_elem, dir, trim_sg = 0;
2874 VPRINTK("ENTER, ata%u\n", ap->id);
2875 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
2877 /* we must lengthen transfers to end on a 32-bit boundary */
2878 qc->pad_len = lsg->length & 3;
2880 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2881 struct scatterlist *psg = &qc->pad_sgent;
2882 unsigned int offset;
2884 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2886 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2889 * psg->page/offset are used to copy to-be-written
2890 * data in this function or read data in ata_sg_clean.
2892 offset = lsg->offset + lsg->length - qc->pad_len;
2893 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2894 psg->offset = offset_in_page(offset);
2896 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2897 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2898 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2899 kunmap_atomic(addr, KM_IRQ0);
2902 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2903 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2905 lsg->length -= qc->pad_len;
2906 if (lsg->length == 0)
2909 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2910 qc->n_elem - 1, lsg->length, qc->pad_len);
2913 pre_n_elem = qc->n_elem;
2914 if (trim_sg && pre_n_elem)
2923 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2925 /* restore last sg */
2926 lsg->length += qc->pad_len;
2930 DPRINTK("%d sg elements mapped\n", n_elem);
2933 qc->n_elem = n_elem;
2939 * ata_poll_qc_complete - turn irq back on and finish qc
2940 * @qc: Command to complete
2941 * @err_mask: ATA status register content
2944 * None. (grabs host lock)
2947 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
2949 struct ata_port *ap = qc->ap;
2950 unsigned long flags;
2952 spin_lock_irqsave(&ap->host_set->lock, flags);
2953 ap->flags &= ~ATA_FLAG_NOINTR;
2955 ata_qc_complete(qc);
2956 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2960 * ata_pio_poll - poll using PIO, depending on current state
2961 * @ap: the target ata_port
2964 * None. (executing in kernel thread context)
2967 * timeout value to use
2970 static unsigned long ata_pio_poll(struct ata_port *ap)
2972 struct ata_queued_cmd *qc;
2974 unsigned int poll_state = HSM_ST_UNKNOWN;
2975 unsigned int reg_state = HSM_ST_UNKNOWN;
2977 qc = ata_qc_from_tag(ap, ap->active_tag);
2978 WARN_ON(qc == NULL);
2980 switch (ap->hsm_task_state) {
2983 poll_state = HSM_ST_POLL;
2987 case HSM_ST_LAST_POLL:
2988 poll_state = HSM_ST_LAST_POLL;
2989 reg_state = HSM_ST_LAST;
2996 status = ata_chk_status(ap);
2997 if (status & ATA_BUSY) {
2998 if (time_after(jiffies, ap->pio_task_timeout)) {
2999 qc->err_mask |= AC_ERR_TIMEOUT;
3000 ap->hsm_task_state = HSM_ST_TMOUT;
3003 ap->hsm_task_state = poll_state;
3004 return ATA_SHORT_PAUSE;
3007 ap->hsm_task_state = reg_state;
3012 * ata_pio_complete - check if drive is busy or idle
3013 * @ap: the target ata_port
3016 * None. (executing in kernel thread context)
3019 * Non-zero if qc completed, zero otherwise.
3022 static int ata_pio_complete (struct ata_port *ap)
3024 struct ata_queued_cmd *qc;
3028 * This is purely heuristic. This is a fast path. Sometimes when
3029 * we enter, BSY will be cleared in a chk-status or two. If not,
3030 * the drive is probably seeking or something. Snooze for a couple
3031 * msecs, then chk-status again. If still busy, fall back to
3032 * HSM_ST_POLL state.
3034 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3035 if (drv_stat & ATA_BUSY) {
3037 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3038 if (drv_stat & ATA_BUSY) {
3039 ap->hsm_task_state = HSM_ST_LAST_POLL;
3040 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3045 qc = ata_qc_from_tag(ap, ap->active_tag);
3046 WARN_ON(qc == NULL);
3048 drv_stat = ata_wait_idle(ap);
3049 if (!ata_ok(drv_stat)) {
3050 qc->err_mask |= __ac_err_mask(drv_stat);
3051 ap->hsm_task_state = HSM_ST_ERR;
3055 ap->hsm_task_state = HSM_ST_IDLE;
3057 WARN_ON(qc->err_mask);
3058 ata_poll_qc_complete(qc);
3060 /* another command may start at this point */
3067 * swap_buf_le16 - swap halves of 16-bit words in place
3068 * @buf: Buffer to swap
3069 * @buf_words: Number of 16-bit words in buffer.
3071 * Swap halves of 16-bit words if needed to convert from
3072 * little-endian byte order to native cpu byte order, or
3076 * Inherited from caller.
3078 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3083 for (i = 0; i < buf_words; i++)
3084 buf[i] = le16_to_cpu(buf[i]);
3085 #endif /* __BIG_ENDIAN */
3089 * ata_mmio_data_xfer - Transfer data by MMIO
3090 * @ap: port to read/write
3092 * @buflen: buffer length
3093 * @write_data: read/write
3095 * Transfer data from/to the device data register by MMIO.
3098 * Inherited from caller.
3101 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3102 unsigned int buflen, int write_data)
3105 unsigned int words = buflen >> 1;
3106 u16 *buf16 = (u16 *) buf;
3107 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3109 /* Transfer multiple of 2 bytes */
3111 for (i = 0; i < words; i++)
3112 writew(le16_to_cpu(buf16[i]), mmio);
3114 for (i = 0; i < words; i++)
3115 buf16[i] = cpu_to_le16(readw(mmio));
3118 /* Transfer trailing 1 byte, if any. */
3119 if (unlikely(buflen & 0x01)) {
3120 u16 align_buf[1] = { 0 };
3121 unsigned char *trailing_buf = buf + buflen - 1;
3124 memcpy(align_buf, trailing_buf, 1);
3125 writew(le16_to_cpu(align_buf[0]), mmio);
3127 align_buf[0] = cpu_to_le16(readw(mmio));
3128 memcpy(trailing_buf, align_buf, 1);
3134 * ata_pio_data_xfer - Transfer data by PIO
3135 * @ap: port to read/write
3137 * @buflen: buffer length
3138 * @write_data: read/write
3140 * Transfer data from/to the device data register by PIO.
3143 * Inherited from caller.
3146 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3147 unsigned int buflen, int write_data)
3149 unsigned int words = buflen >> 1;
3151 /* Transfer multiple of 2 bytes */
3153 outsw(ap->ioaddr.data_addr, buf, words);
3155 insw(ap->ioaddr.data_addr, buf, words);
3157 /* Transfer trailing 1 byte, if any. */
3158 if (unlikely(buflen & 0x01)) {
3159 u16 align_buf[1] = { 0 };
3160 unsigned char *trailing_buf = buf + buflen - 1;
3163 memcpy(align_buf, trailing_buf, 1);
3164 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3166 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3167 memcpy(trailing_buf, align_buf, 1);
3173 * ata_data_xfer - Transfer data from/to the data register.
3174 * @ap: port to read/write
3176 * @buflen: buffer length
3177 * @do_write: read/write
3179 * Transfer data from/to the device data register.
3182 * Inherited from caller.
3185 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3186 unsigned int buflen, int do_write)
3188 /* Make the crap hardware pay the costs not the good stuff */
3189 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3190 unsigned long flags;
3191 local_irq_save(flags);
3192 if (ap->flags & ATA_FLAG_MMIO)
3193 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3195 ata_pio_data_xfer(ap, buf, buflen, do_write);
3196 local_irq_restore(flags);
3198 if (ap->flags & ATA_FLAG_MMIO)
3199 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3201 ata_pio_data_xfer(ap, buf, buflen, do_write);
3206 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3207 * @qc: Command on going
3209 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3212 * Inherited from caller.
3215 static void ata_pio_sector(struct ata_queued_cmd *qc)
3217 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3218 struct scatterlist *sg = qc->__sg;
3219 struct ata_port *ap = qc->ap;
3221 unsigned int offset;
3224 if (qc->cursect == (qc->nsect - 1))
3225 ap->hsm_task_state = HSM_ST_LAST;
3227 page = sg[qc->cursg].page;
3228 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3230 /* get the current page and offset */
3231 page = nth_page(page, (offset >> PAGE_SHIFT));
3232 offset %= PAGE_SIZE;
3234 buf = kmap(page) + offset;
3239 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3244 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3246 /* do the actual data transfer */
3247 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3248 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3254 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3255 * @qc: Command on going
3256 * @bytes: number of bytes
3258 * Transfer Transfer data from/to the ATAPI device.
3261 * Inherited from caller.
3265 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3267 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3268 struct scatterlist *sg = qc->__sg;
3269 struct ata_port *ap = qc->ap;
3272 unsigned int offset, count;
3274 if (qc->curbytes + bytes >= qc->nbytes)
3275 ap->hsm_task_state = HSM_ST_LAST;
3278 if (unlikely(qc->cursg >= qc->n_elem)) {
3280 * The end of qc->sg is reached and the device expects
3281 * more data to transfer. In order not to overrun qc->sg
3282 * and fulfill length specified in the byte count register,
3283 * - for read case, discard trailing data from the device
3284 * - for write case, padding zero data to the device
3286 u16 pad_buf[1] = { 0 };
3287 unsigned int words = bytes >> 1;
3290 if (words) /* warning if bytes > 1 */
3291 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3294 for (i = 0; i < words; i++)
3295 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3297 ap->hsm_task_state = HSM_ST_LAST;
3301 sg = &qc->__sg[qc->cursg];
3304 offset = sg->offset + qc->cursg_ofs;
3306 /* get the current page and offset */
3307 page = nth_page(page, (offset >> PAGE_SHIFT));
3308 offset %= PAGE_SIZE;
3310 /* don't overrun current sg */
3311 count = min(sg->length - qc->cursg_ofs, bytes);
3313 /* don't cross page boundaries */
3314 count = min(count, (unsigned int)PAGE_SIZE - offset);
3316 buf = kmap(page) + offset;
3319 qc->curbytes += count;
3320 qc->cursg_ofs += count;
3322 if (qc->cursg_ofs == sg->length) {
3327 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3329 /* do the actual data transfer */
3330 ata_data_xfer(ap, buf, count, do_write);
3339 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3340 * @qc: Command on going
3342 * Transfer Transfer data from/to the ATAPI device.
3345 * Inherited from caller.
3348 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3350 struct ata_port *ap = qc->ap;
3351 struct ata_device *dev = qc->dev;
3352 unsigned int ireason, bc_lo, bc_hi, bytes;
3353 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3355 ap->ops->tf_read(ap, &qc->tf);
3356 ireason = qc->tf.nsect;
3357 bc_lo = qc->tf.lbam;
3358 bc_hi = qc->tf.lbah;
3359 bytes = (bc_hi << 8) | bc_lo;
3361 /* shall be cleared to zero, indicating xfer of data */
3362 if (ireason & (1 << 0))
3365 /* make sure transfer direction matches expected */
3366 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3367 if (do_write != i_write)
3370 __atapi_pio_bytes(qc, bytes);
3375 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3376 ap->id, dev->devno);
3377 qc->err_mask |= AC_ERR_HSM;
3378 ap->hsm_task_state = HSM_ST_ERR;
3382 * ata_pio_block - start PIO on a block
3383 * @ap: the target ata_port
3386 * None. (executing in kernel thread context)
3389 static void ata_pio_block(struct ata_port *ap)
3391 struct ata_queued_cmd *qc;
3395 * This is purely heuristic. This is a fast path.
3396 * Sometimes when we enter, BSY will be cleared in
3397 * a chk-status or two. If not, the drive is probably seeking
3398 * or something. Snooze for a couple msecs, then
3399 * chk-status again. If still busy, fall back to
3400 * HSM_ST_POLL state.
3402 status = ata_busy_wait(ap, ATA_BUSY, 5);
3403 if (status & ATA_BUSY) {
3405 status = ata_busy_wait(ap, ATA_BUSY, 10);
3406 if (status & ATA_BUSY) {
3407 ap->hsm_task_state = HSM_ST_POLL;
3408 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3413 qc = ata_qc_from_tag(ap, ap->active_tag);
3414 WARN_ON(qc == NULL);
3417 if (status & (ATA_ERR | ATA_DF)) {
3418 qc->err_mask |= AC_ERR_DEV;
3419 ap->hsm_task_state = HSM_ST_ERR;
3423 /* transfer data if any */
3424 if (is_atapi_taskfile(&qc->tf)) {
3425 /* DRQ=0 means no more data to transfer */
3426 if ((status & ATA_DRQ) == 0) {
3427 ap->hsm_task_state = HSM_ST_LAST;
3431 atapi_pio_bytes(qc);
3433 /* handle BSY=0, DRQ=0 as error */
3434 if ((status & ATA_DRQ) == 0) {
3435 qc->err_mask |= AC_ERR_HSM;
3436 ap->hsm_task_state = HSM_ST_ERR;
3444 static void ata_pio_error(struct ata_port *ap)
3446 struct ata_queued_cmd *qc;
3448 qc = ata_qc_from_tag(ap, ap->active_tag);
3449 WARN_ON(qc == NULL);
3451 if (qc->tf.command != ATA_CMD_PACKET)
3452 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3454 /* make sure qc->err_mask is available to
3455 * know what's wrong and recover
3457 WARN_ON(qc->err_mask == 0);
3459 ap->hsm_task_state = HSM_ST_IDLE;
3461 ata_poll_qc_complete(qc);
3464 static void ata_pio_task(void *_data)
3466 struct ata_port *ap = _data;
3467 unsigned long timeout;
3474 switch (ap->hsm_task_state) {
3483 qc_completed = ata_pio_complete(ap);
3487 case HSM_ST_LAST_POLL:
3488 timeout = ata_pio_poll(ap);
3498 ata_queue_delayed_pio_task(ap, timeout);
3499 else if (!qc_completed)
3504 * ata_qc_timeout - Handle timeout of queued command
3505 * @qc: Command that timed out
3507 * Some part of the kernel (currently, only the SCSI layer)
3508 * has noticed that the active command on port @ap has not
3509 * completed after a specified length of time. Handle this
3510 * condition by disabling DMA (if necessary) and completing
3511 * transactions, with error if necessary.
3513 * This also handles the case of the "lost interrupt", where
3514 * for some reason (possibly hardware bug, possibly driver bug)
3515 * an interrupt was not delivered to the driver, even though the
3516 * transaction completed successfully.
3519 * Inherited from SCSI layer (none, can sleep)
3522 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3524 struct ata_port *ap = qc->ap;
3525 struct ata_host_set *host_set = ap->host_set;
3526 u8 host_stat = 0, drv_stat;
3527 unsigned long flags;
3531 ata_flush_pio_tasks(ap);
3532 ap->hsm_task_state = HSM_ST_IDLE;
3534 spin_lock_irqsave(&host_set->lock, flags);
3536 switch (qc->tf.protocol) {
3539 case ATA_PROT_ATAPI_DMA:
3540 host_stat = ap->ops->bmdma_status(ap);
3542 /* before we do anything else, clear DMA-Start bit */
3543 ap->ops->bmdma_stop(qc);
3549 drv_stat = ata_chk_status(ap);
3551 /* ack bmdma irq events */
3552 ap->ops->irq_clear(ap);
3554 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3555 ap->id, qc->tf.command, drv_stat, host_stat);
3557 /* complete taskfile transaction */
3558 qc->err_mask |= ac_err_mask(drv_stat);
3562 spin_unlock_irqrestore(&host_set->lock, flags);
3564 ata_eh_qc_complete(qc);
3570 * ata_eng_timeout - Handle timeout of queued command
3571 * @ap: Port on which timed-out command is active
3573 * Some part of the kernel (currently, only the SCSI layer)
3574 * has noticed that the active command on port @ap has not
3575 * completed after a specified length of time. Handle this
3576 * condition by disabling DMA (if necessary) and completing
3577 * transactions, with error if necessary.
3579 * This also handles the case of the "lost interrupt", where
3580 * for some reason (possibly hardware bug, possibly driver bug)
3581 * an interrupt was not delivered to the driver, even though the
3582 * transaction completed successfully.
3585 * Inherited from SCSI layer (none, can sleep)
3588 void ata_eng_timeout(struct ata_port *ap)
3592 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
3598 * ata_qc_new - Request an available ATA command, for queueing
3599 * @ap: Port associated with device @dev
3600 * @dev: Device from whom we request an available command structure
3606 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3608 struct ata_queued_cmd *qc = NULL;
3611 for (i = 0; i < ATA_MAX_QUEUE; i++)
3612 if (!test_and_set_bit(i, &ap->qactive)) {
3613 qc = ata_qc_from_tag(ap, i);
3624 * ata_qc_new_init - Request an available ATA command, and initialize it
3625 * @ap: Port associated with device @dev
3626 * @dev: Device from whom we request an available command structure
3632 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3633 struct ata_device *dev)
3635 struct ata_queued_cmd *qc;
3637 qc = ata_qc_new(ap);
3650 * ata_qc_free - free unused ata_queued_cmd
3651 * @qc: Command to complete
3653 * Designed to free unused ata_queued_cmd object
3654 * in case something prevents using it.
3657 * spin_lock_irqsave(host_set lock)
3659 void ata_qc_free(struct ata_queued_cmd *qc)
3661 struct ata_port *ap = qc->ap;
3664 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3668 if (likely(ata_tag_valid(tag))) {
3669 if (tag == ap->active_tag)
3670 ap->active_tag = ATA_TAG_POISON;
3671 qc->tag = ATA_TAG_POISON;
3672 clear_bit(tag, &ap->qactive);
3676 void __ata_qc_complete(struct ata_queued_cmd *qc)
3678 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3679 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3681 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3684 /* atapi: mark qc as inactive to prevent the interrupt handler
3685 * from completing the command twice later, before the error handler
3686 * is called. (when rc != 0 and atapi request sense is needed)
3688 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3690 /* call completion callback */
3691 qc->complete_fn(qc);
3694 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3696 struct ata_port *ap = qc->ap;
3698 switch (qc->tf.protocol) {
3700 case ATA_PROT_ATAPI_DMA:
3703 case ATA_PROT_ATAPI:
3705 case ATA_PROT_PIO_MULT:
3706 if (ap->flags & ATA_FLAG_PIO_DMA)
3719 * ata_qc_issue - issue taskfile to device
3720 * @qc: command to issue to device
3722 * Prepare an ATA command to submission to device.
3723 * This includes mapping the data into a DMA-able
3724 * area, filling in the S/G table, and finally
3725 * writing the taskfile to hardware, starting the command.
3728 * spin_lock_irqsave(host_set lock)
3731 * Zero on success, AC_ERR_* mask on failure
3734 unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
3736 struct ata_port *ap = qc->ap;
3738 if (ata_should_dma_map(qc)) {
3739 if (qc->flags & ATA_QCFLAG_SG) {
3740 if (ata_sg_setup(qc))
3742 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3743 if (ata_sg_setup_one(qc))
3747 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3750 ap->ops->qc_prep(qc);
3752 qc->ap->active_tag = qc->tag;
3753 qc->flags |= ATA_QCFLAG_ACTIVE;
3755 return ap->ops->qc_issue(qc);
3758 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3759 return AC_ERR_SYSTEM;
3764 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3765 * @qc: command to issue to device
3767 * Using various libata functions and hooks, this function
3768 * starts an ATA command. ATA commands are grouped into
3769 * classes called "protocols", and issuing each type of protocol
3770 * is slightly different.
3772 * May be used as the qc_issue() entry in ata_port_operations.
3775 * spin_lock_irqsave(host_set lock)
3778 * Zero on success, AC_ERR_* mask on failure
3781 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3783 struct ata_port *ap = qc->ap;
3785 ata_dev_select(ap, qc->dev->devno, 1, 0);
3787 switch (qc->tf.protocol) {
3788 case ATA_PROT_NODATA:
3789 ata_tf_to_host(ap, &qc->tf);
3793 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3794 ap->ops->bmdma_setup(qc); /* set up bmdma */
3795 ap->ops->bmdma_start(qc); /* initiate bmdma */
3798 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3799 ata_qc_set_polling(qc);
3800 ata_tf_to_host(ap, &qc->tf);
3801 ap->hsm_task_state = HSM_ST;
3802 ata_queue_pio_task(ap);
3805 case ATA_PROT_ATAPI:
3806 ata_qc_set_polling(qc);
3807 ata_tf_to_host(ap, &qc->tf);
3808 ata_queue_packet_task(ap);
3811 case ATA_PROT_ATAPI_NODATA:
3812 ap->flags |= ATA_FLAG_NOINTR;
3813 ata_tf_to_host(ap, &qc->tf);
3814 ata_queue_packet_task(ap);
3817 case ATA_PROT_ATAPI_DMA:
3818 ap->flags |= ATA_FLAG_NOINTR;
3819 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3820 ap->ops->bmdma_setup(qc); /* set up bmdma */
3821 ata_queue_packet_task(ap);
3826 return AC_ERR_SYSTEM;
3833 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3834 * @qc: Info associated with this ATA transaction.
3837 * spin_lock_irqsave(host_set lock)
3840 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3842 struct ata_port *ap = qc->ap;
3843 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3845 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3847 /* load PRD table addr. */
3848 mb(); /* make sure PRD table writes are visible to controller */
3849 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3851 /* specify data direction, triple-check start bit is clear */
3852 dmactl = readb(mmio + ATA_DMA_CMD);
3853 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3855 dmactl |= ATA_DMA_WR;
3856 writeb(dmactl, mmio + ATA_DMA_CMD);
3858 /* issue r/w command */
3859 ap->ops->exec_command(ap, &qc->tf);
3863 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3864 * @qc: Info associated with this ATA transaction.
3867 * spin_lock_irqsave(host_set lock)
3870 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3872 struct ata_port *ap = qc->ap;
3873 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3876 /* start host DMA transaction */
3877 dmactl = readb(mmio + ATA_DMA_CMD);
3878 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3880 /* Strictly, one may wish to issue a readb() here, to
3881 * flush the mmio write. However, control also passes
3882 * to the hardware at this point, and it will interrupt
3883 * us when we are to resume control. So, in effect,
3884 * we don't care when the mmio write flushes.
3885 * Further, a read of the DMA status register _immediately_
3886 * following the write may not be what certain flaky hardware
3887 * is expected, so I think it is best to not add a readb()
3888 * without first all the MMIO ATA cards/mobos.
3889 * Or maybe I'm just being paranoid.
3894 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3895 * @qc: Info associated with this ATA transaction.
3898 * spin_lock_irqsave(host_set lock)
3901 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3903 struct ata_port *ap = qc->ap;
3904 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3907 /* load PRD table addr. */
3908 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3910 /* specify data direction, triple-check start bit is clear */
3911 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3912 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3914 dmactl |= ATA_DMA_WR;
3915 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3917 /* issue r/w command */
3918 ap->ops->exec_command(ap, &qc->tf);
3922 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3923 * @qc: Info associated with this ATA transaction.
3926 * spin_lock_irqsave(host_set lock)
3929 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3931 struct ata_port *ap = qc->ap;
3934 /* start host DMA transaction */
3935 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3936 outb(dmactl | ATA_DMA_START,
3937 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3942 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3943 * @qc: Info associated with this ATA transaction.
3945 * Writes the ATA_DMA_START flag to the DMA command register.
3947 * May be used as the bmdma_start() entry in ata_port_operations.
3950 * spin_lock_irqsave(host_set lock)
3952 void ata_bmdma_start(struct ata_queued_cmd *qc)
3954 if (qc->ap->flags & ATA_FLAG_MMIO)
3955 ata_bmdma_start_mmio(qc);
3957 ata_bmdma_start_pio(qc);
3962 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3963 * @qc: Info associated with this ATA transaction.
3965 * Writes address of PRD table to device's PRD Table Address
3966 * register, sets the DMA control register, and calls
3967 * ops->exec_command() to start the transfer.
3969 * May be used as the bmdma_setup() entry in ata_port_operations.
3972 * spin_lock_irqsave(host_set lock)
3974 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3976 if (qc->ap->flags & ATA_FLAG_MMIO)
3977 ata_bmdma_setup_mmio(qc);
3979 ata_bmdma_setup_pio(qc);
3984 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3985 * @ap: Port associated with this ATA transaction.
3987 * Clear interrupt and error flags in DMA status register.
3989 * May be used as the irq_clear() entry in ata_port_operations.
3992 * spin_lock_irqsave(host_set lock)
3995 void ata_bmdma_irq_clear(struct ata_port *ap)
3997 if (ap->flags & ATA_FLAG_MMIO) {
3998 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3999 writeb(readb(mmio), mmio);
4001 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4002 outb(inb(addr), addr);
4009 * ata_bmdma_status - Read PCI IDE BMDMA status
4010 * @ap: Port associated with this ATA transaction.
4012 * Read and return BMDMA status register.
4014 * May be used as the bmdma_status() entry in ata_port_operations.
4017 * spin_lock_irqsave(host_set lock)
4020 u8 ata_bmdma_status(struct ata_port *ap)
4023 if (ap->flags & ATA_FLAG_MMIO) {
4024 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4025 host_stat = readb(mmio + ATA_DMA_STATUS);
4027 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4033 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4034 * @qc: Command we are ending DMA for
4036 * Clears the ATA_DMA_START flag in the dma control register
4038 * May be used as the bmdma_stop() entry in ata_port_operations.
4041 * spin_lock_irqsave(host_set lock)
4044 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4046 struct ata_port *ap = qc->ap;
4047 if (ap->flags & ATA_FLAG_MMIO) {
4048 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4050 /* clear start/stop bit */
4051 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4052 mmio + ATA_DMA_CMD);
4054 /* clear start/stop bit */
4055 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4056 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4059 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4060 ata_altstatus(ap); /* dummy read */
4064 * ata_host_intr - Handle host interrupt for given (port, task)
4065 * @ap: Port on which interrupt arrived (possibly...)
4066 * @qc: Taskfile currently active in engine
4068 * Handle host interrupt for given queued command. Currently,
4069 * only DMA interrupts are handled. All other commands are
4070 * handled via polling with interrupts disabled (nIEN bit).
4073 * spin_lock_irqsave(host_set lock)
4076 * One if interrupt was handled, zero if not (shared irq).
4079 inline unsigned int ata_host_intr (struct ata_port *ap,
4080 struct ata_queued_cmd *qc)
4082 u8 status, host_stat;
4084 switch (qc->tf.protocol) {
4087 case ATA_PROT_ATAPI_DMA:
4088 case ATA_PROT_ATAPI:
4089 /* check status of DMA engine */
4090 host_stat = ap->ops->bmdma_status(ap);
4091 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4093 /* if it's not our irq... */
4094 if (!(host_stat & ATA_DMA_INTR))
4097 /* before we do anything else, clear DMA-Start bit */
4098 ap->ops->bmdma_stop(qc);
4102 case ATA_PROT_ATAPI_NODATA:
4103 case ATA_PROT_NODATA:
4104 /* check altstatus */
4105 status = ata_altstatus(ap);
4106 if (status & ATA_BUSY)
4109 /* check main status, clearing INTRQ */
4110 status = ata_chk_status(ap);
4111 if (unlikely(status & ATA_BUSY))
4113 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4114 ap->id, qc->tf.protocol, status);
4116 /* ack bmdma irq events */
4117 ap->ops->irq_clear(ap);
4119 /* complete taskfile transaction */
4120 qc->err_mask |= ac_err_mask(status);
4121 ata_qc_complete(qc);
4128 return 1; /* irq handled */
4131 ap->stats.idle_irq++;
4134 if ((ap->stats.idle_irq % 1000) == 0) {
4136 ata_irq_ack(ap, 0); /* debug trap */
4137 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4140 return 0; /* irq not handled */
4144 * ata_interrupt - Default ATA host interrupt handler
4145 * @irq: irq line (unused)
4146 * @dev_instance: pointer to our ata_host_set information structure
4149 * Default interrupt handler for PCI IDE devices. Calls
4150 * ata_host_intr() for each port that is not disabled.
4153 * Obtains host_set lock during operation.
4156 * IRQ_NONE or IRQ_HANDLED.
4159 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4161 struct ata_host_set *host_set = dev_instance;
4163 unsigned int handled = 0;
4164 unsigned long flags;
4166 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4167 spin_lock_irqsave(&host_set->lock, flags);
4169 for (i = 0; i < host_set->n_ports; i++) {
4170 struct ata_port *ap;
4172 ap = host_set->ports[i];
4174 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
4175 struct ata_queued_cmd *qc;
4177 qc = ata_qc_from_tag(ap, ap->active_tag);
4178 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4179 (qc->flags & ATA_QCFLAG_ACTIVE))
4180 handled |= ata_host_intr(ap, qc);
4184 spin_unlock_irqrestore(&host_set->lock, flags);
4186 return IRQ_RETVAL(handled);
4190 * atapi_packet_task - Write CDB bytes to hardware
4191 * @_data: Port to which ATAPI device is attached.
4193 * When device has indicated its readiness to accept
4194 * a CDB, this function is called. Send the CDB.
4195 * If DMA is to be performed, exit immediately.
4196 * Otherwise, we are in polling mode, so poll
4197 * status under operation succeeds or fails.
4200 * Kernel thread context (may sleep)
4203 static void atapi_packet_task(void *_data)
4205 struct ata_port *ap = _data;
4206 struct ata_queued_cmd *qc;
4209 qc = ata_qc_from_tag(ap, ap->active_tag);
4210 WARN_ON(qc == NULL);
4211 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4213 /* sleep-wait for BSY to clear */
4214 DPRINTK("busy wait\n");
4215 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4216 qc->err_mask |= AC_ERR_TIMEOUT;
4220 /* make sure DRQ is set */
4221 status = ata_chk_status(ap);
4222 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4223 qc->err_mask |= AC_ERR_HSM;
4228 DPRINTK("send cdb\n");
4229 WARN_ON(qc->dev->cdb_len < 12);
4231 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4232 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4233 unsigned long flags;
4235 /* Once we're done issuing command and kicking bmdma,
4236 * irq handler takes over. To not lose irq, we need
4237 * to clear NOINTR flag before sending cdb, but
4238 * interrupt handler shouldn't be invoked before we're
4239 * finished. Hence, the following locking.
4241 spin_lock_irqsave(&ap->host_set->lock, flags);
4242 ap->flags &= ~ATA_FLAG_NOINTR;
4243 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
4244 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4245 ap->ops->bmdma_start(qc); /* initiate bmdma */
4246 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4248 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
4250 /* PIO commands are handled by polling */
4251 ap->hsm_task_state = HSM_ST;
4252 ata_queue_pio_task(ap);
4258 ata_poll_qc_complete(qc);
4263 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4264 * without filling any other registers
4266 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4269 struct ata_taskfile tf;
4272 ata_tf_init(ap, &tf, dev->devno);
4275 tf.flags |= ATA_TFLAG_DEVICE;
4276 tf.protocol = ATA_PROT_NODATA;
4278 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4280 printk(KERN_ERR "%s: ata command failed: %d\n",
4286 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4290 if (!ata_try_flush_cache(dev))
4293 if (ata_id_has_flush_ext(dev->id))
4294 cmd = ATA_CMD_FLUSH_EXT;
4296 cmd = ATA_CMD_FLUSH;
4298 return ata_do_simple_cmd(ap, dev, cmd);
4301 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4303 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4306 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4308 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4312 * ata_device_resume - wakeup a previously suspended devices
4313 * @ap: port the device is connected to
4314 * @dev: the device to resume
4316 * Kick the drive back into action, by sending it an idle immediate
4317 * command and making sure its transfer mode matches between drive
4321 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4323 if (ap->flags & ATA_FLAG_SUSPENDED) {
4324 ap->flags &= ~ATA_FLAG_SUSPENDED;
4327 if (!ata_dev_present(dev))
4329 if (dev->class == ATA_DEV_ATA)
4330 ata_start_drive(ap, dev);
4336 * ata_device_suspend - prepare a device for suspend
4337 * @ap: port the device is connected to
4338 * @dev: the device to suspend
4340 * Flush the cache on the drive, if appropriate, then issue a
4341 * standbynow command.
4343 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4345 if (!ata_dev_present(dev))
4347 if (dev->class == ATA_DEV_ATA)
4348 ata_flush_cache(ap, dev);
4350 ata_standby_drive(ap, dev);
4351 ap->flags |= ATA_FLAG_SUSPENDED;
4356 * ata_port_start - Set port up for dma.
4357 * @ap: Port to initialize
4359 * Called just after data structures for each port are
4360 * initialized. Allocates space for PRD table.
4362 * May be used as the port_start() entry in ata_port_operations.
4365 * Inherited from caller.
4368 int ata_port_start (struct ata_port *ap)
4370 struct device *dev = ap->host_set->dev;
4373 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4377 rc = ata_pad_alloc(ap, dev);
4379 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4383 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4390 * ata_port_stop - Undo ata_port_start()
4391 * @ap: Port to shut down
4393 * Frees the PRD table.
4395 * May be used as the port_stop() entry in ata_port_operations.
4398 * Inherited from caller.
4401 void ata_port_stop (struct ata_port *ap)
4403 struct device *dev = ap->host_set->dev;
4405 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4406 ata_pad_free(ap, dev);
4409 void ata_host_stop (struct ata_host_set *host_set)
4411 if (host_set->mmio_base)
4412 iounmap(host_set->mmio_base);
4417 * ata_host_remove - Unregister SCSI host structure with upper layers
4418 * @ap: Port to unregister
4419 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4422 * Inherited from caller.
4425 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4427 struct Scsi_Host *sh = ap->host;
4432 scsi_remove_host(sh);
4434 ap->ops->port_stop(ap);
4438 * ata_host_init - Initialize an ata_port structure
4439 * @ap: Structure to initialize
4440 * @host: associated SCSI mid-layer structure
4441 * @host_set: Collection of hosts to which @ap belongs
4442 * @ent: Probe information provided by low-level driver
4443 * @port_no: Port number associated with this ata_port
4445 * Initialize a new ata_port structure, and its associated
4449 * Inherited from caller.
4452 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4453 struct ata_host_set *host_set,
4454 const struct ata_probe_ent *ent, unsigned int port_no)
4460 host->max_channel = 1;
4461 host->unique_id = ata_unique_id++;
4462 host->max_cmd_len = 12;
4464 ap->flags = ATA_FLAG_PORT_DISABLED;
4465 ap->id = host->unique_id;
4467 ap->ctl = ATA_DEVCTL_OBS;
4468 ap->host_set = host_set;
4469 ap->port_no = port_no;
4471 ent->legacy_mode ? ent->hard_port_no : port_no;
4472 ap->pio_mask = ent->pio_mask;
4473 ap->mwdma_mask = ent->mwdma_mask;
4474 ap->udma_mask = ent->udma_mask;
4475 ap->flags |= ent->host_flags;
4476 ap->ops = ent->port_ops;
4477 ap->cbl = ATA_CBL_NONE;
4478 ap->active_tag = ATA_TAG_POISON;
4479 ap->last_ctl = 0xFF;
4481 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4482 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4483 INIT_LIST_HEAD(&ap->eh_done_q);
4485 for (i = 0; i < ATA_MAX_DEVICES; i++)
4486 ap->device[i].devno = i;
4489 ap->stats.unhandled_irq = 1;
4490 ap->stats.idle_irq = 1;
4493 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4497 * ata_host_add - Attach low-level ATA driver to system
4498 * @ent: Information provided by low-level driver
4499 * @host_set: Collections of ports to which we add
4500 * @port_no: Port number associated with this host
4502 * Attach low-level ATA driver to system.
4505 * PCI/etc. bus probe sem.
4508 * New ata_port on success, for NULL on error.
4511 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4512 struct ata_host_set *host_set,
4513 unsigned int port_no)
4515 struct Scsi_Host *host;
4516 struct ata_port *ap;
4520 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4524 ap = (struct ata_port *) &host->hostdata[0];
4526 ata_host_init(ap, host, host_set, ent, port_no);
4528 rc = ap->ops->port_start(ap);
4535 scsi_host_put(host);
4540 * ata_device_add - Register hardware device with ATA and SCSI layers
4541 * @ent: Probe information describing hardware device to be registered
4543 * This function processes the information provided in the probe
4544 * information struct @ent, allocates the necessary ATA and SCSI
4545 * host information structures, initializes them, and registers
4546 * everything with requisite kernel subsystems.
4548 * This function requests irqs, probes the ATA bus, and probes
4552 * PCI/etc. bus probe sem.
4555 * Number of ports registered. Zero on error (no ports registered).
4558 int ata_device_add(const struct ata_probe_ent *ent)
4560 unsigned int count = 0, i;
4561 struct device *dev = ent->dev;
4562 struct ata_host_set *host_set;
4565 /* alloc a container for our list of ATA ports (buses) */
4566 host_set = kzalloc(sizeof(struct ata_host_set) +
4567 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4570 spin_lock_init(&host_set->lock);
4572 host_set->dev = dev;
4573 host_set->n_ports = ent->n_ports;
4574 host_set->irq = ent->irq;
4575 host_set->mmio_base = ent->mmio_base;
4576 host_set->private_data = ent->private_data;
4577 host_set->ops = ent->port_ops;
4579 /* register each port bound to this device */
4580 for (i = 0; i < ent->n_ports; i++) {
4581 struct ata_port *ap;
4582 unsigned long xfer_mode_mask;
4584 ap = ata_host_add(ent, host_set, i);
4588 host_set->ports[i] = ap;
4589 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4590 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4591 (ap->pio_mask << ATA_SHIFT_PIO);
4593 /* print per-port info to dmesg */
4594 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4595 "bmdma 0x%lX irq %lu\n",
4597 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4598 ata_mode_string(xfer_mode_mask),
4599 ap->ioaddr.cmd_addr,
4600 ap->ioaddr.ctl_addr,
4601 ap->ioaddr.bmdma_addr,
4605 host_set->ops->irq_clear(ap);
4612 /* obtain irq, that is shared between channels */
4613 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4614 DRV_NAME, host_set))
4617 /* perform each probe synchronously */
4618 DPRINTK("probe begin\n");
4619 for (i = 0; i < count; i++) {
4620 struct ata_port *ap;
4623 ap = host_set->ports[i];
4625 DPRINTK("ata%u: bus probe begin\n", ap->id);
4626 rc = ata_bus_probe(ap);
4627 DPRINTK("ata%u: bus probe end\n", ap->id);
4630 /* FIXME: do something useful here?
4631 * Current libata behavior will
4632 * tear down everything when
4633 * the module is removed
4634 * or the h/w is unplugged.
4638 rc = scsi_add_host(ap->host, dev);
4640 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4642 /* FIXME: do something useful here */
4643 /* FIXME: handle unconditional calls to
4644 * scsi_scan_host and ata_host_remove, below,
4650 /* probes are done, now scan each port's disk(s) */
4651 DPRINTK("host probe begin\n");
4652 for (i = 0; i < count; i++) {
4653 struct ata_port *ap = host_set->ports[i];
4655 ata_scsi_scan_host(ap);
4658 dev_set_drvdata(dev, host_set);
4660 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4661 return ent->n_ports; /* success */
4664 for (i = 0; i < count; i++) {
4665 ata_host_remove(host_set->ports[i], 1);
4666 scsi_host_put(host_set->ports[i]->host);
4670 VPRINTK("EXIT, returning 0\n");
4675 * ata_host_set_remove - PCI layer callback for device removal
4676 * @host_set: ATA host set that was removed
4678 * Unregister all objects associated with this host set. Free those
4682 * Inherited from calling layer (may sleep).
4685 void ata_host_set_remove(struct ata_host_set *host_set)
4687 struct ata_port *ap;
4690 for (i = 0; i < host_set->n_ports; i++) {
4691 ap = host_set->ports[i];
4692 scsi_remove_host(ap->host);
4695 free_irq(host_set->irq, host_set);
4697 for (i = 0; i < host_set->n_ports; i++) {
4698 ap = host_set->ports[i];
4700 ata_scsi_release(ap->host);
4702 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4703 struct ata_ioports *ioaddr = &ap->ioaddr;
4705 if (ioaddr->cmd_addr == 0x1f0)
4706 release_region(0x1f0, 8);
4707 else if (ioaddr->cmd_addr == 0x170)
4708 release_region(0x170, 8);
4711 scsi_host_put(ap->host);
4714 if (host_set->ops->host_stop)
4715 host_set->ops->host_stop(host_set);
4721 * ata_scsi_release - SCSI layer callback hook for host unload
4722 * @host: libata host to be unloaded
4724 * Performs all duties necessary to shut down a libata port...
4725 * Kill port kthread, disable port, and release resources.
4728 * Inherited from SCSI layer.
4734 int ata_scsi_release(struct Scsi_Host *host)
4736 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4741 ap->ops->port_disable(ap);
4742 ata_host_remove(ap, 0);
4743 for (i = 0; i < ATA_MAX_DEVICES; i++)
4744 kfree(ap->device[i].id);
4751 * ata_std_ports - initialize ioaddr with standard port offsets.
4752 * @ioaddr: IO address structure to be initialized
4754 * Utility function which initializes data_addr, error_addr,
4755 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4756 * device_addr, status_addr, and command_addr to standard offsets
4757 * relative to cmd_addr.
4759 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4762 void ata_std_ports(struct ata_ioports *ioaddr)
4764 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4765 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4766 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4767 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4768 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4769 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4770 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4771 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4772 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4773 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4779 void ata_pci_host_stop (struct ata_host_set *host_set)
4781 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4783 pci_iounmap(pdev, host_set->mmio_base);
4787 * ata_pci_remove_one - PCI layer callback for device removal
4788 * @pdev: PCI device that was removed
4790 * PCI layer indicates to libata via this hook that
4791 * hot-unplug or module unload event has occurred.
4792 * Handle this by unregistering all objects associated
4793 * with this PCI device. Free those objects. Then finally
4794 * release PCI resources and disable device.
4797 * Inherited from PCI layer (may sleep).
4800 void ata_pci_remove_one (struct pci_dev *pdev)
4802 struct device *dev = pci_dev_to_dev(pdev);
4803 struct ata_host_set *host_set = dev_get_drvdata(dev);
4805 ata_host_set_remove(host_set);
4806 pci_release_regions(pdev);
4807 pci_disable_device(pdev);
4808 dev_set_drvdata(dev, NULL);
4811 /* move to PCI subsystem */
4812 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4814 unsigned long tmp = 0;
4816 switch (bits->width) {
4819 pci_read_config_byte(pdev, bits->reg, &tmp8);
4825 pci_read_config_word(pdev, bits->reg, &tmp16);
4831 pci_read_config_dword(pdev, bits->reg, &tmp32);
4842 return (tmp == bits->val) ? 1 : 0;
4845 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4847 pci_save_state(pdev);
4848 pci_disable_device(pdev);
4849 pci_set_power_state(pdev, PCI_D3hot);
4853 int ata_pci_device_resume(struct pci_dev *pdev)
4855 pci_set_power_state(pdev, PCI_D0);
4856 pci_restore_state(pdev);
4857 pci_enable_device(pdev);
4858 pci_set_master(pdev);
4861 #endif /* CONFIG_PCI */
4864 static int __init ata_init(void)
4866 ata_wq = create_workqueue("ata");
4870 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4874 static void __exit ata_exit(void)
4876 destroy_workqueue(ata_wq);
4879 module_init(ata_init);
4880 module_exit(ata_exit);
4882 static unsigned long ratelimit_time;
4883 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4885 int ata_ratelimit(void)
4888 unsigned long flags;
4890 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4892 if (time_after(jiffies, ratelimit_time)) {
4894 ratelimit_time = jiffies + (HZ/5);
4898 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4904 * libata is essentially a library of internal helper functions for
4905 * low-level ATA host controller drivers. As such, the API/ABI is
4906 * likely to change as new drivers are added and updated.
4907 * Do not depend on ABI/API stability.
4910 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4911 EXPORT_SYMBOL_GPL(ata_std_ports);
4912 EXPORT_SYMBOL_GPL(ata_device_add);
4913 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4914 EXPORT_SYMBOL_GPL(ata_sg_init);
4915 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4916 EXPORT_SYMBOL_GPL(__ata_qc_complete);
4917 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4918 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4919 EXPORT_SYMBOL_GPL(ata_tf_load);
4920 EXPORT_SYMBOL_GPL(ata_tf_read);
4921 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4922 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4923 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4924 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4925 EXPORT_SYMBOL_GPL(ata_check_status);
4926 EXPORT_SYMBOL_GPL(ata_altstatus);
4927 EXPORT_SYMBOL_GPL(ata_exec_command);
4928 EXPORT_SYMBOL_GPL(ata_port_start);
4929 EXPORT_SYMBOL_GPL(ata_port_stop);
4930 EXPORT_SYMBOL_GPL(ata_host_stop);
4931 EXPORT_SYMBOL_GPL(ata_interrupt);
4932 EXPORT_SYMBOL_GPL(ata_qc_prep);
4933 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4934 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4935 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4936 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4937 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4938 EXPORT_SYMBOL_GPL(ata_port_probe);
4939 EXPORT_SYMBOL_GPL(sata_phy_reset);
4940 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4941 EXPORT_SYMBOL_GPL(ata_bus_reset);
4942 EXPORT_SYMBOL_GPL(ata_std_probeinit);
4943 EXPORT_SYMBOL_GPL(ata_std_softreset);
4944 EXPORT_SYMBOL_GPL(sata_std_hardreset);
4945 EXPORT_SYMBOL_GPL(ata_std_postreset);
4946 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
4947 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
4948 EXPORT_SYMBOL_GPL(ata_port_disable);
4949 EXPORT_SYMBOL_GPL(ata_ratelimit);
4950 EXPORT_SYMBOL_GPL(ata_busy_sleep);
4951 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4952 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4953 EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
4954 EXPORT_SYMBOL_GPL(ata_scsi_error);
4955 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4956 EXPORT_SYMBOL_GPL(ata_scsi_release);
4957 EXPORT_SYMBOL_GPL(ata_host_intr);
4958 EXPORT_SYMBOL_GPL(ata_dev_classify);
4959 EXPORT_SYMBOL_GPL(ata_id_string);
4960 EXPORT_SYMBOL_GPL(ata_id_c_string);
4961 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4962 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4963 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
4965 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
4966 EXPORT_SYMBOL_GPL(ata_timing_compute);
4967 EXPORT_SYMBOL_GPL(ata_timing_merge);
4970 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4971 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4972 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4973 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4974 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4975 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4976 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
4977 #endif /* CONFIG_PCI */
4979 EXPORT_SYMBOL_GPL(ata_device_suspend);
4980 EXPORT_SYMBOL_GPL(ata_device_resume);
4981 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4982 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);