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 /* debounce timing parameters in msecs { interval, duration, timeout } */
65 const unsigned long sata_deb_timing_boot[] = { 5, 100, 2000 };
66 const unsigned long sata_deb_timing_eh[] = { 25, 500, 2000 };
67 const unsigned long sata_deb_timing_before_fsrst[] = { 100, 2000, 5000 };
69 static unsigned int ata_dev_init_params(struct ata_device *dev,
70 u16 heads, u16 sectors);
71 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
72 static void ata_dev_xfermask(struct ata_device *dev);
74 static unsigned int ata_unique_id = 1;
75 static struct workqueue_struct *ata_wq;
77 struct workqueue_struct *ata_aux_wq;
79 int atapi_enabled = 1;
80 module_param(atapi_enabled, int, 0444);
81 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84 module_param(atapi_dmadir, int, 0444);
85 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
88 module_param_named(fua, libata_fua, int, 0444);
89 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
91 static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
92 module_param(ata_probe_timeout, int, 0444);
93 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
95 MODULE_AUTHOR("Jeff Garzik");
96 MODULE_DESCRIPTION("Library module for ATA devices");
97 MODULE_LICENSE("GPL");
98 MODULE_VERSION(DRV_VERSION);
102 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
103 * @tf: Taskfile to convert
104 * @fis: Buffer into which data will output
105 * @pmp: Port multiplier port
107 * Converts a standard ATA taskfile to a Serial ATA
108 * FIS structure (Register - Host to Device).
111 * Inherited from caller.
114 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
116 fis[0] = 0x27; /* Register - Host to Device FIS */
117 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
118 bit 7 indicates Command FIS */
119 fis[2] = tf->command;
120 fis[3] = tf->feature;
127 fis[8] = tf->hob_lbal;
128 fis[9] = tf->hob_lbam;
129 fis[10] = tf->hob_lbah;
130 fis[11] = tf->hob_feature;
133 fis[13] = tf->hob_nsect;
144 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
145 * @fis: Buffer from which data will be input
146 * @tf: Taskfile to output
148 * Converts a serial ATA FIS structure to a standard ATA taskfile.
151 * Inherited from caller.
154 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
156 tf->command = fis[2]; /* status */
157 tf->feature = fis[3]; /* error */
164 tf->hob_lbal = fis[8];
165 tf->hob_lbam = fis[9];
166 tf->hob_lbah = fis[10];
169 tf->hob_nsect = fis[13];
172 static const u8 ata_rw_cmds[] = {
176 ATA_CMD_READ_MULTI_EXT,
177 ATA_CMD_WRITE_MULTI_EXT,
181 ATA_CMD_WRITE_MULTI_FUA_EXT,
185 ATA_CMD_PIO_READ_EXT,
186 ATA_CMD_PIO_WRITE_EXT,
199 ATA_CMD_WRITE_FUA_EXT
203 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
204 * @qc: command to examine and configure
206 * Examine the device configuration and tf->flags to calculate
207 * the proper read/write commands and protocol to use.
212 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
214 struct ata_taskfile *tf = &qc->tf;
215 struct ata_device *dev = qc->dev;
218 int index, fua, lba48, write;
220 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
221 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
222 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
224 if (dev->flags & ATA_DFLAG_PIO) {
225 tf->protocol = ATA_PROT_PIO;
226 index = dev->multi_count ? 0 : 8;
227 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
228 /* Unable to use DMA due to host limitation */
229 tf->protocol = ATA_PROT_PIO;
230 index = dev->multi_count ? 0 : 8;
232 tf->protocol = ATA_PROT_DMA;
236 cmd = ata_rw_cmds[index + fua + lba48 + write];
245 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
246 * @pio_mask: pio_mask
247 * @mwdma_mask: mwdma_mask
248 * @udma_mask: udma_mask
250 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
251 * unsigned int xfer_mask.
259 static unsigned int ata_pack_xfermask(unsigned int pio_mask,
260 unsigned int mwdma_mask,
261 unsigned int udma_mask)
263 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
264 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
265 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
269 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
270 * @xfer_mask: xfer_mask to unpack
271 * @pio_mask: resulting pio_mask
272 * @mwdma_mask: resulting mwdma_mask
273 * @udma_mask: resulting udma_mask
275 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
276 * Any NULL distination masks will be ignored.
278 static void ata_unpack_xfermask(unsigned int xfer_mask,
279 unsigned int *pio_mask,
280 unsigned int *mwdma_mask,
281 unsigned int *udma_mask)
284 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
286 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
288 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
291 static const struct ata_xfer_ent {
295 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
296 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
297 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
302 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
303 * @xfer_mask: xfer_mask of interest
305 * Return matching XFER_* value for @xfer_mask. Only the highest
306 * bit of @xfer_mask is considered.
312 * Matching XFER_* value, 0 if no match found.
314 static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
316 int highbit = fls(xfer_mask) - 1;
317 const struct ata_xfer_ent *ent;
319 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
320 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
321 return ent->base + highbit - ent->shift;
326 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
327 * @xfer_mode: XFER_* of interest
329 * Return matching xfer_mask for @xfer_mode.
335 * Matching xfer_mask, 0 if no match found.
337 static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
339 const struct ata_xfer_ent *ent;
341 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
342 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
343 return 1 << (ent->shift + xfer_mode - ent->base);
348 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
349 * @xfer_mode: XFER_* of interest
351 * Return matching xfer_shift for @xfer_mode.
357 * Matching xfer_shift, -1 if no match found.
359 static int ata_xfer_mode2shift(unsigned int xfer_mode)
361 const struct ata_xfer_ent *ent;
363 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
364 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
370 * ata_mode_string - convert xfer_mask to string
371 * @xfer_mask: mask of bits supported; only highest bit counts.
373 * Determine string which represents the highest speed
374 * (highest bit in @modemask).
380 * Constant C string representing highest speed listed in
381 * @mode_mask, or the constant C string "<n/a>".
383 static const char *ata_mode_string(unsigned int xfer_mask)
385 static const char * const xfer_mode_str[] = {
405 highbit = fls(xfer_mask) - 1;
406 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
407 return xfer_mode_str[highbit];
411 static const char *sata_spd_string(unsigned int spd)
413 static const char * const spd_str[] = {
418 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
420 return spd_str[spd - 1];
423 void ata_dev_disable(struct ata_device *dev)
425 if (ata_dev_enabled(dev) && ata_msg_drv(dev->ap)) {
426 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
432 * ata_pio_devchk - PATA device presence detection
433 * @ap: ATA channel to examine
434 * @device: Device to examine (starting at zero)
436 * This technique was originally described in
437 * Hale Landis's ATADRVR (www.ata-atapi.com), and
438 * later found its way into the ATA/ATAPI spec.
440 * Write a pattern to the ATA shadow registers,
441 * and if a device is present, it will respond by
442 * correctly storing and echoing back the
443 * ATA shadow register contents.
449 static unsigned int ata_pio_devchk(struct ata_port *ap,
452 struct ata_ioports *ioaddr = &ap->ioaddr;
455 ap->ops->dev_select(ap, device);
457 outb(0x55, ioaddr->nsect_addr);
458 outb(0xaa, ioaddr->lbal_addr);
460 outb(0xaa, ioaddr->nsect_addr);
461 outb(0x55, ioaddr->lbal_addr);
463 outb(0x55, ioaddr->nsect_addr);
464 outb(0xaa, ioaddr->lbal_addr);
466 nsect = inb(ioaddr->nsect_addr);
467 lbal = inb(ioaddr->lbal_addr);
469 if ((nsect == 0x55) && (lbal == 0xaa))
470 return 1; /* we found a device */
472 return 0; /* nothing found */
476 * ata_mmio_devchk - PATA device presence detection
477 * @ap: ATA channel to examine
478 * @device: Device to examine (starting at zero)
480 * This technique was originally described in
481 * Hale Landis's ATADRVR (www.ata-atapi.com), and
482 * later found its way into the ATA/ATAPI spec.
484 * Write a pattern to the ATA shadow registers,
485 * and if a device is present, it will respond by
486 * correctly storing and echoing back the
487 * ATA shadow register contents.
493 static unsigned int ata_mmio_devchk(struct ata_port *ap,
496 struct ata_ioports *ioaddr = &ap->ioaddr;
499 ap->ops->dev_select(ap, device);
501 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
502 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
504 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
505 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
507 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
508 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
510 nsect = readb((void __iomem *) ioaddr->nsect_addr);
511 lbal = readb((void __iomem *) ioaddr->lbal_addr);
513 if ((nsect == 0x55) && (lbal == 0xaa))
514 return 1; /* we found a device */
516 return 0; /* nothing found */
520 * ata_devchk - PATA device presence detection
521 * @ap: ATA channel to examine
522 * @device: Device to examine (starting at zero)
524 * Dispatch ATA device presence detection, depending
525 * on whether we are using PIO or MMIO to talk to the
526 * ATA shadow registers.
532 static unsigned int ata_devchk(struct ata_port *ap,
535 if (ap->flags & ATA_FLAG_MMIO)
536 return ata_mmio_devchk(ap, device);
537 return ata_pio_devchk(ap, device);
541 * ata_dev_classify - determine device type based on ATA-spec signature
542 * @tf: ATA taskfile register set for device to be identified
544 * Determine from taskfile register contents whether a device is
545 * ATA or ATAPI, as per "Signature and persistence" section
546 * of ATA/PI spec (volume 1, sect 5.14).
552 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
553 * the event of failure.
556 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
558 /* Apple's open source Darwin code hints that some devices only
559 * put a proper signature into the LBA mid/high registers,
560 * So, we only check those. It's sufficient for uniqueness.
563 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
564 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
565 DPRINTK("found ATA device by sig\n");
569 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
570 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
571 DPRINTK("found ATAPI device by sig\n");
572 return ATA_DEV_ATAPI;
575 DPRINTK("unknown device\n");
576 return ATA_DEV_UNKNOWN;
580 * ata_dev_try_classify - Parse returned ATA device signature
581 * @ap: ATA channel to examine
582 * @device: Device to examine (starting at zero)
583 * @r_err: Value of error register on completion
585 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
586 * an ATA/ATAPI-defined set of values is placed in the ATA
587 * shadow registers, indicating the results of device detection
590 * Select the ATA device, and read the values from the ATA shadow
591 * registers. Then parse according to the Error register value,
592 * and the spec-defined values examined by ata_dev_classify().
598 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
602 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
604 struct ata_taskfile tf;
608 ap->ops->dev_select(ap, device);
610 memset(&tf, 0, sizeof(tf));
612 ap->ops->tf_read(ap, &tf);
617 /* see if device passed diags */
620 else if ((device == 0) && (err == 0x81))
625 /* determine if device is ATA or ATAPI */
626 class = ata_dev_classify(&tf);
628 if (class == ATA_DEV_UNKNOWN)
630 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
636 * ata_id_string - Convert IDENTIFY DEVICE page into string
637 * @id: IDENTIFY DEVICE results we will examine
638 * @s: string into which data is output
639 * @ofs: offset into identify device page
640 * @len: length of string to return. must be an even number.
642 * The strings in the IDENTIFY DEVICE page are broken up into
643 * 16-bit chunks. Run through the string, and output each
644 * 8-bit chunk linearly, regardless of platform.
650 void ata_id_string(const u16 *id, unsigned char *s,
651 unsigned int ofs, unsigned int len)
670 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
671 * @id: IDENTIFY DEVICE results we will examine
672 * @s: string into which data is output
673 * @ofs: offset into identify device page
674 * @len: length of string to return. must be an odd number.
676 * This function is identical to ata_id_string except that it
677 * trims trailing spaces and terminates the resulting string with
678 * null. @len must be actual maximum length (even number) + 1.
683 void ata_id_c_string(const u16 *id, unsigned char *s,
684 unsigned int ofs, unsigned int len)
690 ata_id_string(id, s, ofs, len - 1);
692 p = s + strnlen(s, len - 1);
693 while (p > s && p[-1] == ' ')
698 static u64 ata_id_n_sectors(const u16 *id)
700 if (ata_id_has_lba(id)) {
701 if (ata_id_has_lba48(id))
702 return ata_id_u64(id, 100);
704 return ata_id_u32(id, 60);
706 if (ata_id_current_chs_valid(id))
707 return ata_id_u32(id, 57);
709 return id[1] * id[3] * id[6];
714 * ata_noop_dev_select - Select device 0/1 on ATA bus
715 * @ap: ATA channel to manipulate
716 * @device: ATA device (numbered from zero) to select
718 * This function performs no actual function.
720 * May be used as the dev_select() entry in ata_port_operations.
725 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
731 * ata_std_dev_select - Select device 0/1 on ATA bus
732 * @ap: ATA channel to manipulate
733 * @device: ATA device (numbered from zero) to select
735 * Use the method defined in the ATA specification to
736 * make either device 0, or device 1, active on the
737 * ATA channel. Works with both PIO and MMIO.
739 * May be used as the dev_select() entry in ata_port_operations.
745 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
750 tmp = ATA_DEVICE_OBS;
752 tmp = ATA_DEVICE_OBS | ATA_DEV1;
754 if (ap->flags & ATA_FLAG_MMIO) {
755 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
757 outb(tmp, ap->ioaddr.device_addr);
759 ata_pause(ap); /* needed; also flushes, for mmio */
763 * ata_dev_select - Select device 0/1 on ATA bus
764 * @ap: ATA channel to manipulate
765 * @device: ATA device (numbered from zero) to select
766 * @wait: non-zero to wait for Status register BSY bit to clear
767 * @can_sleep: non-zero if context allows sleeping
769 * Use the method defined in the ATA specification to
770 * make either device 0, or device 1, active on the
773 * This is a high-level version of ata_std_dev_select(),
774 * which additionally provides the services of inserting
775 * the proper pauses and status polling, where needed.
781 void ata_dev_select(struct ata_port *ap, unsigned int device,
782 unsigned int wait, unsigned int can_sleep)
784 if (ata_msg_probe(ap))
785 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, ata%u: "
786 "device %u, wait %u\n", ap->id, device, wait);
791 ap->ops->dev_select(ap, device);
794 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
801 * ata_dump_id - IDENTIFY DEVICE info debugging output
802 * @id: IDENTIFY DEVICE page to dump
804 * Dump selected 16-bit words from the given IDENTIFY DEVICE
811 static inline void ata_dump_id(const u16 *id)
813 DPRINTK("49==0x%04x "
823 DPRINTK("80==0x%04x "
833 DPRINTK("88==0x%04x "
840 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
841 * @id: IDENTIFY data to compute xfer mask from
843 * Compute the xfermask for this device. This is not as trivial
844 * as it seems if we must consider early devices correctly.
846 * FIXME: pre IDE drive timing (do we care ?).
854 static unsigned int ata_id_xfermask(const u16 *id)
856 unsigned int pio_mask, mwdma_mask, udma_mask;
858 /* Usual case. Word 53 indicates word 64 is valid */
859 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
860 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
864 /* If word 64 isn't valid then Word 51 high byte holds
865 * the PIO timing number for the maximum. Turn it into
868 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
870 /* But wait.. there's more. Design your standards by
871 * committee and you too can get a free iordy field to
872 * process. However its the speeds not the modes that
873 * are supported... Note drivers using the timing API
874 * will get this right anyway
878 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
881 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
882 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
884 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
888 * ata_port_queue_task - Queue port_task
889 * @ap: The ata_port to queue port_task for
890 * @fn: workqueue function to be scheduled
891 * @data: data value to pass to workqueue function
892 * @delay: delay time for workqueue function
894 * Schedule @fn(@data) for execution after @delay jiffies using
895 * port_task. There is one port_task per port and it's the
896 * user(low level driver)'s responsibility to make sure that only
897 * one task is active at any given time.
899 * libata core layer takes care of synchronization between
900 * port_task and EH. ata_port_queue_task() may be ignored for EH
904 * Inherited from caller.
906 void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
911 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
914 PREPARE_WORK(&ap->port_task, fn, data);
917 rc = queue_work(ata_wq, &ap->port_task);
919 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
921 /* rc == 0 means that another user is using port task */
926 * ata_port_flush_task - Flush port_task
927 * @ap: The ata_port to flush port_task for
929 * After this function completes, port_task is guranteed not to
930 * be running or scheduled.
933 * Kernel thread context (may sleep)
935 void ata_port_flush_task(struct ata_port *ap)
941 spin_lock_irqsave(ap->lock, flags);
942 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
943 spin_unlock_irqrestore(ap->lock, flags);
945 DPRINTK("flush #1\n");
946 flush_workqueue(ata_wq);
949 * At this point, if a task is running, it's guaranteed to see
950 * the FLUSH flag; thus, it will never queue pio tasks again.
953 if (!cancel_delayed_work(&ap->port_task)) {
955 ata_port_printk(ap, KERN_DEBUG, "%s: flush #2\n",
957 flush_workqueue(ata_wq);
960 spin_lock_irqsave(ap->lock, flags);
961 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
962 spin_unlock_irqrestore(ap->lock, flags);
965 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
968 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
970 struct completion *waiting = qc->private_data;
976 * ata_exec_internal - execute libata internal command
977 * @dev: Device to which the command is sent
978 * @tf: Taskfile registers for the command and the result
979 * @cdb: CDB for packet command
980 * @dma_dir: Data tranfer direction of the command
981 * @buf: Data buffer of the command
982 * @buflen: Length of data buffer
984 * Executes libata internal command with timeout. @tf contains
985 * command on entry and result on return. Timeout and error
986 * conditions are reported via return value. No recovery action
987 * is taken after a command times out. It's caller's duty to
988 * clean up after timeout.
991 * None. Should be called with kernel context, might sleep.
994 * Zero on success, AC_ERR_* mask on failure
996 unsigned ata_exec_internal(struct ata_device *dev,
997 struct ata_taskfile *tf, const u8 *cdb,
998 int dma_dir, void *buf, unsigned int buflen)
1000 struct ata_port *ap = dev->ap;
1001 u8 command = tf->command;
1002 struct ata_queued_cmd *qc;
1003 unsigned int tag, preempted_tag;
1004 u32 preempted_sactive, preempted_qc_active;
1005 DECLARE_COMPLETION(wait);
1006 unsigned long flags;
1007 unsigned int err_mask;
1010 spin_lock_irqsave(ap->lock, flags);
1012 /* no internal command while frozen */
1013 if (ap->flags & ATA_FLAG_FROZEN) {
1014 spin_unlock_irqrestore(ap->lock, flags);
1015 return AC_ERR_SYSTEM;
1018 /* initialize internal qc */
1020 /* XXX: Tag 0 is used for drivers with legacy EH as some
1021 * drivers choke if any other tag is given. This breaks
1022 * ata_tag_internal() test for those drivers. Don't use new
1023 * EH stuff without converting to it.
1025 if (ap->ops->error_handler)
1026 tag = ATA_TAG_INTERNAL;
1030 if (test_and_set_bit(tag, &ap->qc_allocated))
1032 qc = __ata_qc_from_tag(ap, tag);
1040 preempted_tag = ap->active_tag;
1041 preempted_sactive = ap->sactive;
1042 preempted_qc_active = ap->qc_active;
1043 ap->active_tag = ATA_TAG_POISON;
1047 /* prepare & issue qc */
1050 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1051 qc->flags |= ATA_QCFLAG_RESULT_TF;
1052 qc->dma_dir = dma_dir;
1053 if (dma_dir != DMA_NONE) {
1054 ata_sg_init_one(qc, buf, buflen);
1055 qc->nsect = buflen / ATA_SECT_SIZE;
1058 qc->private_data = &wait;
1059 qc->complete_fn = ata_qc_complete_internal;
1063 spin_unlock_irqrestore(ap->lock, flags);
1065 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
1067 ata_port_flush_task(ap);
1070 spin_lock_irqsave(ap->lock, flags);
1072 /* We're racing with irq here. If we lose, the
1073 * following test prevents us from completing the qc
1074 * twice. If we win, the port is frozen and will be
1075 * cleaned up by ->post_internal_cmd().
1077 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1078 qc->err_mask |= AC_ERR_TIMEOUT;
1080 if (ap->ops->error_handler)
1081 ata_port_freeze(ap);
1083 ata_qc_complete(qc);
1085 if (ata_msg_warn(ap))
1086 ata_dev_printk(dev, KERN_WARNING,
1087 "qc timeout (cmd 0x%x)\n", command);
1090 spin_unlock_irqrestore(ap->lock, flags);
1093 /* do post_internal_cmd */
1094 if (ap->ops->post_internal_cmd)
1095 ap->ops->post_internal_cmd(qc);
1097 if (qc->flags & ATA_QCFLAG_FAILED && !qc->err_mask) {
1098 if (ata_msg_warn(ap))
1099 ata_dev_printk(dev, KERN_WARNING,
1100 "zero err_mask for failed "
1101 "internal command, assuming AC_ERR_OTHER\n");
1102 qc->err_mask |= AC_ERR_OTHER;
1106 spin_lock_irqsave(ap->lock, flags);
1108 *tf = qc->result_tf;
1109 err_mask = qc->err_mask;
1112 ap->active_tag = preempted_tag;
1113 ap->sactive = preempted_sactive;
1114 ap->qc_active = preempted_qc_active;
1116 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1117 * Until those drivers are fixed, we detect the condition
1118 * here, fail the command with AC_ERR_SYSTEM and reenable the
1121 * Note that this doesn't change any behavior as internal
1122 * command failure results in disabling the device in the
1123 * higher layer for LLDDs without new reset/EH callbacks.
1125 * Kill the following code as soon as those drivers are fixed.
1127 if (ap->flags & ATA_FLAG_DISABLED) {
1128 err_mask |= AC_ERR_SYSTEM;
1132 spin_unlock_irqrestore(ap->lock, flags);
1138 * ata_do_simple_cmd - execute simple internal command
1139 * @dev: Device to which the command is sent
1140 * @cmd: Opcode to execute
1142 * Execute a 'simple' command, that only consists of the opcode
1143 * 'cmd' itself, without filling any other registers
1146 * Kernel thread context (may sleep).
1149 * Zero on success, AC_ERR_* mask on failure
1151 unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1153 struct ata_taskfile tf;
1155 ata_tf_init(dev, &tf);
1158 tf.flags |= ATA_TFLAG_DEVICE;
1159 tf.protocol = ATA_PROT_NODATA;
1161 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
1165 * ata_pio_need_iordy - check if iordy needed
1168 * Check if the current speed of the device requires IORDY. Used
1169 * by various controllers for chip configuration.
1172 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1175 int speed = adev->pio_mode - XFER_PIO_0;
1182 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1184 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1185 pio = adev->id[ATA_ID_EIDE_PIO];
1186 /* Is the speed faster than the drive allows non IORDY ? */
1188 /* This is cycle times not frequency - watch the logic! */
1189 if (pio > 240) /* PIO2 is 240nS per cycle */
1198 * ata_dev_read_id - Read ID data from the specified device
1199 * @dev: target device
1200 * @p_class: pointer to class of the target device (may be changed)
1201 * @post_reset: is this read ID post-reset?
1202 * @id: buffer to read IDENTIFY data into
1204 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1205 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1206 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1207 * for pre-ATA4 drives.
1210 * Kernel thread context (may sleep)
1213 * 0 on success, -errno otherwise.
1215 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1216 int post_reset, u16 *id)
1218 struct ata_port *ap = dev->ap;
1219 unsigned int class = *p_class;
1220 struct ata_taskfile tf;
1221 unsigned int err_mask = 0;
1225 if (ata_msg_ctl(ap))
1226 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
1227 __FUNCTION__, ap->id, dev->devno);
1229 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1232 ata_tf_init(dev, &tf);
1236 tf.command = ATA_CMD_ID_ATA;
1239 tf.command = ATA_CMD_ID_ATAPI;
1243 reason = "unsupported class";
1247 tf.protocol = ATA_PROT_PIO;
1249 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1250 id, sizeof(id[0]) * ATA_ID_WORDS);
1253 reason = "I/O error";
1257 swap_buf_le16(id, ATA_ID_WORDS);
1260 if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
1262 reason = "device reports illegal type";
1266 if (post_reset && class == ATA_DEV_ATA) {
1268 * The exact sequence expected by certain pre-ATA4 drives is:
1271 * INITIALIZE DEVICE PARAMETERS
1273 * Some drives were very specific about that exact sequence.
1275 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1276 err_mask = ata_dev_init_params(dev, id[3], id[6]);
1279 reason = "INIT_DEV_PARAMS failed";
1283 /* current CHS translation info (id[53-58]) might be
1284 * changed. reread the identify device info.
1296 if (ata_msg_warn(ap))
1297 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
1298 "(%s, err_mask=0x%x)\n", reason, err_mask);
1302 static inline u8 ata_dev_knobble(struct ata_device *dev)
1304 return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1307 static void ata_dev_config_ncq(struct ata_device *dev,
1308 char *desc, size_t desc_sz)
1310 struct ata_port *ap = dev->ap;
1311 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1313 if (!ata_id_has_ncq(dev->id)) {
1318 if (ap->flags & ATA_FLAG_NCQ) {
1319 hdepth = min(ap->host->can_queue, ATA_MAX_QUEUE - 1);
1320 dev->flags |= ATA_DFLAG_NCQ;
1323 if (hdepth >= ddepth)
1324 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1326 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1330 * ata_dev_configure - Configure the specified ATA/ATAPI device
1331 * @dev: Target device to configure
1332 * @print_info: Enable device info printout
1334 * Configure @dev according to @dev->id. Generic and low-level
1335 * driver specific fixups are also applied.
1338 * Kernel thread context (may sleep)
1341 * 0 on success, -errno otherwise
1343 int ata_dev_configure(struct ata_device *dev, int print_info)
1345 struct ata_port *ap = dev->ap;
1346 const u16 *id = dev->id;
1347 unsigned int xfer_mask;
1350 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
1351 ata_dev_printk(dev, KERN_INFO,
1352 "%s: ENTER/EXIT (host %u, dev %u) -- nodev\n",
1353 __FUNCTION__, ap->id, dev->devno);
1357 if (ata_msg_probe(ap))
1358 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
1359 __FUNCTION__, ap->id, dev->devno);
1361 /* print device capabilities */
1362 if (ata_msg_probe(ap))
1363 ata_dev_printk(dev, KERN_DEBUG,
1364 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1365 "85:%04x 86:%04x 87:%04x 88:%04x\n",
1367 id[49], id[82], id[83], id[84],
1368 id[85], id[86], id[87], id[88]);
1370 /* initialize to-be-configured parameters */
1371 dev->flags &= ~ATA_DFLAG_CFG_MASK;
1372 dev->max_sectors = 0;
1380 * common ATA, ATAPI feature tests
1383 /* find max transfer mode; for printk only */
1384 xfer_mask = ata_id_xfermask(id);
1386 if (ata_msg_probe(ap))
1389 /* ATA-specific feature tests */
1390 if (dev->class == ATA_DEV_ATA) {
1391 dev->n_sectors = ata_id_n_sectors(id);
1393 if (ata_id_has_lba(id)) {
1394 const char *lba_desc;
1398 dev->flags |= ATA_DFLAG_LBA;
1399 if (ata_id_has_lba48(id)) {
1400 dev->flags |= ATA_DFLAG_LBA48;
1405 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1407 /* print device info to dmesg */
1408 if (ata_msg_info(ap))
1409 ata_dev_printk(dev, KERN_INFO, "ATA-%d, "
1410 "max %s, %Lu sectors: %s %s\n",
1411 ata_id_major_version(id),
1412 ata_mode_string(xfer_mask),
1413 (unsigned long long)dev->n_sectors,
1414 lba_desc, ncq_desc);
1418 /* Default translation */
1419 dev->cylinders = id[1];
1421 dev->sectors = id[6];
1423 if (ata_id_current_chs_valid(id)) {
1424 /* Current CHS translation is valid. */
1425 dev->cylinders = id[54];
1426 dev->heads = id[55];
1427 dev->sectors = id[56];
1430 /* print device info to dmesg */
1431 if (ata_msg_info(ap))
1432 ata_dev_printk(dev, KERN_INFO, "ATA-%d, "
1433 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1434 ata_id_major_version(id),
1435 ata_mode_string(xfer_mask),
1436 (unsigned long long)dev->n_sectors,
1437 dev->cylinders, dev->heads,
1441 if (dev->id[59] & 0x100) {
1442 dev->multi_count = dev->id[59] & 0xff;
1443 if (ata_msg_info(ap))
1444 ata_dev_printk(dev, KERN_INFO,
1445 "ata%u: dev %u multi count %u\n",
1446 ap->id, dev->devno, dev->multi_count);
1452 /* ATAPI-specific feature tests */
1453 else if (dev->class == ATA_DEV_ATAPI) {
1454 char *cdb_intr_string = "";
1456 rc = atapi_cdb_len(id);
1457 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1458 if (ata_msg_warn(ap))
1459 ata_dev_printk(dev, KERN_WARNING,
1460 "unsupported CDB len\n");
1464 dev->cdb_len = (unsigned int) rc;
1466 if (ata_id_cdb_intr(dev->id)) {
1467 dev->flags |= ATA_DFLAG_CDB_INTR;
1468 cdb_intr_string = ", CDB intr";
1471 /* print device info to dmesg */
1472 if (ata_msg_info(ap))
1473 ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
1474 ata_mode_string(xfer_mask),
1478 ap->host->max_cmd_len = 0;
1479 for (i = 0; i < ATA_MAX_DEVICES; i++)
1480 ap->host->max_cmd_len = max_t(unsigned int,
1481 ap->host->max_cmd_len,
1482 ap->device[i].cdb_len);
1484 /* limit bridge transfers to udma5, 200 sectors */
1485 if (ata_dev_knobble(dev)) {
1486 if (ata_msg_info(ap))
1487 ata_dev_printk(dev, KERN_INFO,
1488 "applying bridge limits\n");
1489 dev->udma_mask &= ATA_UDMA5;
1490 dev->max_sectors = ATA_MAX_SECTORS;
1493 if (ap->ops->dev_config)
1494 ap->ops->dev_config(ap, dev);
1496 if (ata_msg_probe(ap))
1497 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
1498 __FUNCTION__, ata_chk_status(ap));
1502 if (ata_msg_probe(ap))
1503 ata_dev_printk(dev, KERN_DEBUG,
1504 "%s: EXIT, err\n", __FUNCTION__);
1509 * ata_bus_probe - Reset and probe ATA bus
1512 * Master ATA bus probing function. Initiates a hardware-dependent
1513 * bus reset, then attempts to identify any devices found on
1517 * PCI/etc. bus probe sem.
1520 * Zero on success, negative errno otherwise.
1523 static int ata_bus_probe(struct ata_port *ap)
1525 unsigned int classes[ATA_MAX_DEVICES];
1526 int tries[ATA_MAX_DEVICES];
1527 int i, rc, down_xfermask;
1528 struct ata_device *dev;
1532 for (i = 0; i < ATA_MAX_DEVICES; i++)
1533 tries[i] = ATA_PROBE_MAX_TRIES;
1538 /* reset and determine device classes */
1539 ap->ops->phy_reset(ap);
1541 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1542 dev = &ap->device[i];
1544 if (!(ap->flags & ATA_FLAG_DISABLED) &&
1545 dev->class != ATA_DEV_UNKNOWN)
1546 classes[dev->devno] = dev->class;
1548 classes[dev->devno] = ATA_DEV_NONE;
1550 dev->class = ATA_DEV_UNKNOWN;
1555 /* after the reset the device state is PIO 0 and the controller
1556 state is undefined. Record the mode */
1558 for (i = 0; i < ATA_MAX_DEVICES; i++)
1559 ap->device[i].pio_mode = XFER_PIO_0;
1561 /* read IDENTIFY page and configure devices */
1562 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1563 dev = &ap->device[i];
1566 dev->class = classes[i];
1568 if (!ata_dev_enabled(dev))
1571 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1575 rc = ata_dev_configure(dev, 1);
1580 /* configure transfer mode */
1581 rc = ata_set_mode(ap, &dev);
1587 for (i = 0; i < ATA_MAX_DEVICES; i++)
1588 if (ata_dev_enabled(&ap->device[i]))
1591 /* no device present, disable port */
1592 ata_port_disable(ap);
1593 ap->ops->port_disable(ap);
1600 tries[dev->devno] = 0;
1603 sata_down_spd_limit(ap);
1606 tries[dev->devno]--;
1607 if (down_xfermask &&
1608 ata_down_xfermask_limit(dev, tries[dev->devno] == 1))
1609 tries[dev->devno] = 0;
1612 if (!tries[dev->devno]) {
1613 ata_down_xfermask_limit(dev, 1);
1614 ata_dev_disable(dev);
1621 * ata_port_probe - Mark port as enabled
1622 * @ap: Port for which we indicate enablement
1624 * Modify @ap data structure such that the system
1625 * thinks that the entire port is enabled.
1627 * LOCKING: host_set lock, or some other form of
1631 void ata_port_probe(struct ata_port *ap)
1633 ap->flags &= ~ATA_FLAG_DISABLED;
1637 * sata_print_link_status - Print SATA link status
1638 * @ap: SATA port to printk link status about
1640 * This function prints link speed and status of a SATA link.
1645 static void sata_print_link_status(struct ata_port *ap)
1647 u32 sstatus, scontrol, tmp;
1649 if (sata_scr_read(ap, SCR_STATUS, &sstatus))
1651 sata_scr_read(ap, SCR_CONTROL, &scontrol);
1653 if (ata_port_online(ap)) {
1654 tmp = (sstatus >> 4) & 0xf;
1655 ata_port_printk(ap, KERN_INFO,
1656 "SATA link up %s (SStatus %X SControl %X)\n",
1657 sata_spd_string(tmp), sstatus, scontrol);
1659 ata_port_printk(ap, KERN_INFO,
1660 "SATA link down (SStatus %X SControl %X)\n",
1666 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1667 * @ap: SATA port associated with target SATA PHY.
1669 * This function issues commands to standard SATA Sxxx
1670 * PHY registers, to wake up the phy (and device), and
1671 * clear any reset condition.
1674 * PCI/etc. bus probe sem.
1677 void __sata_phy_reset(struct ata_port *ap)
1680 unsigned long timeout = jiffies + (HZ * 5);
1682 if (ap->flags & ATA_FLAG_SATA_RESET) {
1683 /* issue phy wake/reset */
1684 sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
1685 /* Couldn't find anything in SATA I/II specs, but
1686 * AHCI-1.1 10.4.2 says at least 1 ms. */
1689 /* phy wake/clear reset */
1690 sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
1692 /* wait for phy to become ready, if necessary */
1695 sata_scr_read(ap, SCR_STATUS, &sstatus);
1696 if ((sstatus & 0xf) != 1)
1698 } while (time_before(jiffies, timeout));
1700 /* print link status */
1701 sata_print_link_status(ap);
1703 /* TODO: phy layer with polling, timeouts, etc. */
1704 if (!ata_port_offline(ap))
1707 ata_port_disable(ap);
1709 if (ap->flags & ATA_FLAG_DISABLED)
1712 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1713 ata_port_disable(ap);
1717 ap->cbl = ATA_CBL_SATA;
1721 * sata_phy_reset - Reset SATA bus.
1722 * @ap: SATA port associated with target SATA PHY.
1724 * This function resets the SATA bus, and then probes
1725 * the bus for devices.
1728 * PCI/etc. bus probe sem.
1731 void sata_phy_reset(struct ata_port *ap)
1733 __sata_phy_reset(ap);
1734 if (ap->flags & ATA_FLAG_DISABLED)
1740 * ata_dev_pair - return other device on cable
1743 * Obtain the other device on the same cable, or if none is
1744 * present NULL is returned
1747 struct ata_device *ata_dev_pair(struct ata_device *adev)
1749 struct ata_port *ap = adev->ap;
1750 struct ata_device *pair = &ap->device[1 - adev->devno];
1751 if (!ata_dev_enabled(pair))
1757 * ata_port_disable - Disable port.
1758 * @ap: Port to be disabled.
1760 * Modify @ap data structure such that the system
1761 * thinks that the entire port is disabled, and should
1762 * never attempt to probe or communicate with devices
1765 * LOCKING: host_set lock, or some other form of
1769 void ata_port_disable(struct ata_port *ap)
1771 ap->device[0].class = ATA_DEV_NONE;
1772 ap->device[1].class = ATA_DEV_NONE;
1773 ap->flags |= ATA_FLAG_DISABLED;
1777 * sata_down_spd_limit - adjust SATA spd limit downward
1778 * @ap: Port to adjust SATA spd limit for
1780 * Adjust SATA spd limit of @ap downward. Note that this
1781 * function only adjusts the limit. The change must be applied
1782 * using sata_set_spd().
1785 * Inherited from caller.
1788 * 0 on success, negative errno on failure
1790 int sata_down_spd_limit(struct ata_port *ap)
1792 u32 sstatus, spd, mask;
1795 rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
1799 mask = ap->sata_spd_limit;
1802 highbit = fls(mask) - 1;
1803 mask &= ~(1 << highbit);
1805 spd = (sstatus >> 4) & 0xf;
1809 mask &= (1 << spd) - 1;
1813 ap->sata_spd_limit = mask;
1815 ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
1816 sata_spd_string(fls(mask)));
1821 static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
1825 if (ap->sata_spd_limit == UINT_MAX)
1828 limit = fls(ap->sata_spd_limit);
1830 spd = (*scontrol >> 4) & 0xf;
1831 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1833 return spd != limit;
1837 * sata_set_spd_needed - is SATA spd configuration needed
1838 * @ap: Port in question
1840 * Test whether the spd limit in SControl matches
1841 * @ap->sata_spd_limit. This function is used to determine
1842 * whether hardreset is necessary to apply SATA spd
1846 * Inherited from caller.
1849 * 1 if SATA spd configuration is needed, 0 otherwise.
1851 int sata_set_spd_needed(struct ata_port *ap)
1855 if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
1858 return __sata_set_spd_needed(ap, &scontrol);
1862 * sata_set_spd - set SATA spd according to spd limit
1863 * @ap: Port to set SATA spd for
1865 * Set SATA spd of @ap according to sata_spd_limit.
1868 * Inherited from caller.
1871 * 0 if spd doesn't need to be changed, 1 if spd has been
1872 * changed. Negative errno if SCR registers are inaccessible.
1874 int sata_set_spd(struct ata_port *ap)
1879 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
1882 if (!__sata_set_spd_needed(ap, &scontrol))
1885 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
1892 * This mode timing computation functionality is ported over from
1893 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1896 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1897 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1898 * for PIO 5, which is a nonstandard extension and UDMA6, which
1899 * is currently supported only by Maxtor drives.
1902 static const struct ata_timing ata_timing[] = {
1904 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1905 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1906 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1907 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1909 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1910 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1911 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1913 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1915 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1916 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1917 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1919 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1920 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1921 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1923 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1924 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1925 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1927 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1928 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1929 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1931 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1936 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1937 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1939 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1941 q->setup = EZ(t->setup * 1000, T);
1942 q->act8b = EZ(t->act8b * 1000, T);
1943 q->rec8b = EZ(t->rec8b * 1000, T);
1944 q->cyc8b = EZ(t->cyc8b * 1000, T);
1945 q->active = EZ(t->active * 1000, T);
1946 q->recover = EZ(t->recover * 1000, T);
1947 q->cycle = EZ(t->cycle * 1000, T);
1948 q->udma = EZ(t->udma * 1000, UT);
1951 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1952 struct ata_timing *m, unsigned int what)
1954 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1955 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1956 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1957 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1958 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1959 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1960 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1961 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1964 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1966 const struct ata_timing *t;
1968 for (t = ata_timing; t->mode != speed; t++)
1969 if (t->mode == 0xFF)
1974 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1975 struct ata_timing *t, int T, int UT)
1977 const struct ata_timing *s;
1978 struct ata_timing p;
1984 if (!(s = ata_timing_find_mode(speed)))
1987 memcpy(t, s, sizeof(*s));
1990 * If the drive is an EIDE drive, it can tell us it needs extended
1991 * PIO/MW_DMA cycle timing.
1994 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1995 memset(&p, 0, sizeof(p));
1996 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1997 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1998 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1999 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2000 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2002 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2006 * Convert the timing to bus clock counts.
2009 ata_timing_quantize(t, t, T, UT);
2012 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2013 * S.M.A.R.T * and some other commands. We have to ensure that the
2014 * DMA cycle timing is slower/equal than the fastest PIO timing.
2017 if (speed > XFER_PIO_4) {
2018 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2019 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2023 * Lengthen active & recovery time so that cycle time is correct.
2026 if (t->act8b + t->rec8b < t->cyc8b) {
2027 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2028 t->rec8b = t->cyc8b - t->act8b;
2031 if (t->active + t->recover < t->cycle) {
2032 t->active += (t->cycle - (t->active + t->recover)) / 2;
2033 t->recover = t->cycle - t->active;
2040 * ata_down_xfermask_limit - adjust dev xfer masks downward
2041 * @dev: Device to adjust xfer masks
2042 * @force_pio0: Force PIO0
2044 * Adjust xfer masks of @dev downward. Note that this function
2045 * does not apply the change. Invoking ata_set_mode() afterwards
2046 * will apply the limit.
2049 * Inherited from caller.
2052 * 0 on success, negative errno on failure
2054 int ata_down_xfermask_limit(struct ata_device *dev, int force_pio0)
2056 unsigned long xfer_mask;
2059 xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
2064 /* don't gear down to MWDMA from UDMA, go directly to PIO */
2065 if (xfer_mask & ATA_MASK_UDMA)
2066 xfer_mask &= ~ATA_MASK_MWDMA;
2068 highbit = fls(xfer_mask) - 1;
2069 xfer_mask &= ~(1 << highbit);
2071 xfer_mask &= 1 << ATA_SHIFT_PIO;
2075 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2078 ata_dev_printk(dev, KERN_WARNING, "limiting speed to %s\n",
2079 ata_mode_string(xfer_mask));
2087 static int ata_dev_set_mode(struct ata_device *dev)
2089 unsigned int err_mask;
2092 dev->flags &= ~ATA_DFLAG_PIO;
2093 if (dev->xfer_shift == ATA_SHIFT_PIO)
2094 dev->flags |= ATA_DFLAG_PIO;
2096 err_mask = ata_dev_set_xfermode(dev);
2098 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2099 "(err_mask=0x%x)\n", err_mask);
2103 rc = ata_dev_revalidate(dev, 0);
2107 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2108 dev->xfer_shift, (int)dev->xfer_mode);
2110 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2111 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
2116 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2117 * @ap: port on which timings will be programmed
2118 * @r_failed_dev: out paramter for failed device
2120 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2121 * ata_set_mode() fails, pointer to the failing device is
2122 * returned in @r_failed_dev.
2125 * PCI/etc. bus probe sem.
2128 * 0 on success, negative errno otherwise
2130 int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
2132 struct ata_device *dev;
2133 int i, rc = 0, used_dma = 0, found = 0;
2135 /* has private set_mode? */
2136 if (ap->ops->set_mode) {
2137 /* FIXME: make ->set_mode handle no device case and
2138 * return error code and failing device on failure.
2140 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2141 if (ata_dev_enabled(&ap->device[i])) {
2142 ap->ops->set_mode(ap);
2149 /* step 1: calculate xfer_mask */
2150 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2151 unsigned int pio_mask, dma_mask;
2153 dev = &ap->device[i];
2155 if (!ata_dev_enabled(dev))
2158 ata_dev_xfermask(dev);
2160 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2161 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2162 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2163 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
2172 /* step 2: always set host PIO timings */
2173 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2174 dev = &ap->device[i];
2175 if (!ata_dev_enabled(dev))
2178 if (!dev->pio_mode) {
2179 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
2184 dev->xfer_mode = dev->pio_mode;
2185 dev->xfer_shift = ATA_SHIFT_PIO;
2186 if (ap->ops->set_piomode)
2187 ap->ops->set_piomode(ap, dev);
2190 /* step 3: set host DMA timings */
2191 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2192 dev = &ap->device[i];
2194 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2197 dev->xfer_mode = dev->dma_mode;
2198 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2199 if (ap->ops->set_dmamode)
2200 ap->ops->set_dmamode(ap, dev);
2203 /* step 4: update devices' xfer mode */
2204 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2205 dev = &ap->device[i];
2207 if (!ata_dev_enabled(dev))
2210 rc = ata_dev_set_mode(dev);
2215 /* Record simplex status. If we selected DMA then the other
2216 * host channels are not permitted to do so.
2218 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
2219 ap->host_set->simplex_claimed = 1;
2221 /* step5: chip specific finalisation */
2222 if (ap->ops->post_set_mode)
2223 ap->ops->post_set_mode(ap);
2227 *r_failed_dev = dev;
2232 * ata_tf_to_host - issue ATA taskfile to host controller
2233 * @ap: port to which command is being issued
2234 * @tf: ATA taskfile register set
2236 * Issues ATA taskfile register set to ATA host controller,
2237 * with proper synchronization with interrupt handler and
2241 * spin_lock_irqsave(host_set lock)
2244 static inline void ata_tf_to_host(struct ata_port *ap,
2245 const struct ata_taskfile *tf)
2247 ap->ops->tf_load(ap, tf);
2248 ap->ops->exec_command(ap, tf);
2252 * ata_busy_sleep - sleep until BSY clears, or timeout
2253 * @ap: port containing status register to be polled
2254 * @tmout_pat: impatience timeout
2255 * @tmout: overall timeout
2257 * Sleep until ATA Status register bit BSY clears,
2258 * or a timeout occurs.
2263 unsigned int ata_busy_sleep (struct ata_port *ap,
2264 unsigned long tmout_pat, unsigned long tmout)
2266 unsigned long timer_start, timeout;
2269 status = ata_busy_wait(ap, ATA_BUSY, 300);
2270 timer_start = jiffies;
2271 timeout = timer_start + tmout_pat;
2272 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2274 status = ata_busy_wait(ap, ATA_BUSY, 3);
2277 if (status & ATA_BUSY)
2278 ata_port_printk(ap, KERN_WARNING,
2279 "port is slow to respond, please be patient\n");
2281 timeout = timer_start + tmout;
2282 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2284 status = ata_chk_status(ap);
2287 if (status & ATA_BUSY) {
2288 ata_port_printk(ap, KERN_ERR, "port failed to respond "
2289 "(%lu secs)\n", tmout / HZ);
2296 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2298 struct ata_ioports *ioaddr = &ap->ioaddr;
2299 unsigned int dev0 = devmask & (1 << 0);
2300 unsigned int dev1 = devmask & (1 << 1);
2301 unsigned long timeout;
2303 /* if device 0 was found in ata_devchk, wait for its
2307 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2309 /* if device 1 was found in ata_devchk, wait for
2310 * register access, then wait for BSY to clear
2312 timeout = jiffies + ATA_TMOUT_BOOT;
2316 ap->ops->dev_select(ap, 1);
2317 if (ap->flags & ATA_FLAG_MMIO) {
2318 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2319 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2321 nsect = inb(ioaddr->nsect_addr);
2322 lbal = inb(ioaddr->lbal_addr);
2324 if ((nsect == 1) && (lbal == 1))
2326 if (time_after(jiffies, timeout)) {
2330 msleep(50); /* give drive a breather */
2333 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2335 /* is all this really necessary? */
2336 ap->ops->dev_select(ap, 0);
2338 ap->ops->dev_select(ap, 1);
2340 ap->ops->dev_select(ap, 0);
2343 static unsigned int ata_bus_softreset(struct ata_port *ap,
2344 unsigned int devmask)
2346 struct ata_ioports *ioaddr = &ap->ioaddr;
2348 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2350 /* software reset. causes dev0 to be selected */
2351 if (ap->flags & ATA_FLAG_MMIO) {
2352 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2353 udelay(20); /* FIXME: flush */
2354 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2355 udelay(20); /* FIXME: flush */
2356 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2358 outb(ap->ctl, ioaddr->ctl_addr);
2360 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2362 outb(ap->ctl, ioaddr->ctl_addr);
2365 /* spec mandates ">= 2ms" before checking status.
2366 * We wait 150ms, because that was the magic delay used for
2367 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2368 * between when the ATA command register is written, and then
2369 * status is checked. Because waiting for "a while" before
2370 * checking status is fine, post SRST, we perform this magic
2371 * delay here as well.
2373 * Old drivers/ide uses the 2mS rule and then waits for ready
2377 /* Before we perform post reset processing we want to see if
2378 * the bus shows 0xFF because the odd clown forgets the D7
2379 * pulldown resistor.
2381 if (ata_check_status(ap) == 0xFF) {
2382 ata_port_printk(ap, KERN_ERR, "SRST failed (status 0xFF)\n");
2383 return AC_ERR_OTHER;
2386 ata_bus_post_reset(ap, devmask);
2392 * ata_bus_reset - reset host port and associated ATA channel
2393 * @ap: port to reset
2395 * This is typically the first time we actually start issuing
2396 * commands to the ATA channel. We wait for BSY to clear, then
2397 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2398 * result. Determine what devices, if any, are on the channel
2399 * by looking at the device 0/1 error register. Look at the signature
2400 * stored in each device's taskfile registers, to determine if
2401 * the device is ATA or ATAPI.
2404 * PCI/etc. bus probe sem.
2405 * Obtains host_set lock.
2408 * Sets ATA_FLAG_DISABLED if bus reset fails.
2411 void ata_bus_reset(struct ata_port *ap)
2413 struct ata_ioports *ioaddr = &ap->ioaddr;
2414 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2416 unsigned int dev0, dev1 = 0, devmask = 0;
2418 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2420 /* determine if device 0/1 are present */
2421 if (ap->flags & ATA_FLAG_SATA_RESET)
2424 dev0 = ata_devchk(ap, 0);
2426 dev1 = ata_devchk(ap, 1);
2430 devmask |= (1 << 0);
2432 devmask |= (1 << 1);
2434 /* select device 0 again */
2435 ap->ops->dev_select(ap, 0);
2437 /* issue bus reset */
2438 if (ap->flags & ATA_FLAG_SRST)
2439 if (ata_bus_softreset(ap, devmask))
2443 * determine by signature whether we have ATA or ATAPI devices
2445 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
2446 if ((slave_possible) && (err != 0x81))
2447 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
2449 /* re-enable interrupts */
2450 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2453 /* is double-select really necessary? */
2454 if (ap->device[1].class != ATA_DEV_NONE)
2455 ap->ops->dev_select(ap, 1);
2456 if (ap->device[0].class != ATA_DEV_NONE)
2457 ap->ops->dev_select(ap, 0);
2459 /* if no devices were detected, disable this port */
2460 if ((ap->device[0].class == ATA_DEV_NONE) &&
2461 (ap->device[1].class == ATA_DEV_NONE))
2464 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2465 /* set up device control for ATA_FLAG_SATA_RESET */
2466 if (ap->flags & ATA_FLAG_MMIO)
2467 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2469 outb(ap->ctl, ioaddr->ctl_addr);
2476 ata_port_printk(ap, KERN_ERR, "disabling port\n");
2477 ap->ops->port_disable(ap);
2483 * sata_phy_debounce - debounce SATA phy status
2484 * @ap: ATA port to debounce SATA phy status for
2485 * @params: timing parameters { interval, duratinon, timeout } in msec
2487 * Make sure SStatus of @ap reaches stable state, determined by
2488 * holding the same value where DET is not 1 for @duration polled
2489 * every @interval, before @timeout. Timeout constraints the
2490 * beginning of the stable state. Because, after hot unplugging,
2491 * DET gets stuck at 1 on some controllers, this functions waits
2492 * until timeout then returns 0 if DET is stable at 1.
2495 * Kernel thread context (may sleep)
2498 * 0 on success, -errno on failure.
2500 int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
2502 unsigned long interval_msec = params[0];
2503 unsigned long duration = params[1] * HZ / 1000;
2504 unsigned long timeout = jiffies + params[2] * HZ / 1000;
2505 unsigned long last_jiffies;
2509 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
2514 last_jiffies = jiffies;
2517 msleep(interval_msec);
2518 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
2524 if (cur == 1 && time_before(jiffies, timeout))
2526 if (time_after(jiffies, last_jiffies + duration))
2531 /* unstable, start over */
2533 last_jiffies = jiffies;
2536 if (time_after(jiffies, timeout))
2542 * sata_phy_resume - resume SATA phy
2543 * @ap: ATA port to resume SATA phy for
2544 * @params: timing parameters { interval, duratinon, timeout } in msec
2546 * Resume SATA phy of @ap and debounce it.
2549 * Kernel thread context (may sleep)
2552 * 0 on success, -errno on failure.
2554 int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
2559 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2562 scontrol = (scontrol & 0x0f0) | 0x300;
2564 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
2567 /* Some PHYs react badly if SStatus is pounded immediately
2568 * after resuming. Delay 200ms before debouncing.
2572 return sata_phy_debounce(ap, params);
2575 static void ata_wait_spinup(struct ata_port *ap)
2577 struct ata_eh_context *ehc = &ap->eh_context;
2578 unsigned long end, secs;
2581 /* first, debounce phy if SATA */
2582 if (ap->cbl == ATA_CBL_SATA) {
2583 rc = sata_phy_debounce(ap, sata_deb_timing_eh);
2585 /* if debounced successfully and offline, no need to wait */
2586 if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
2590 /* okay, let's give the drive time to spin up */
2591 end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
2592 secs = ((end - jiffies) + HZ - 1) / HZ;
2594 if (time_after(jiffies, end))
2598 ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
2599 "(%lu secs)\n", secs);
2601 schedule_timeout_uninterruptible(end - jiffies);
2605 * ata_std_prereset - prepare for reset
2606 * @ap: ATA port to be reset
2608 * @ap is about to be reset. Initialize it.
2611 * Kernel thread context (may sleep)
2614 * 0 on success, -errno otherwise.
2616 int ata_std_prereset(struct ata_port *ap)
2618 struct ata_eh_context *ehc = &ap->eh_context;
2619 const unsigned long *timing;
2623 if (ehc->i.flags & ATA_EHI_HOTPLUGGED) {
2624 if (ap->flags & ATA_FLAG_HRST_TO_RESUME)
2625 ehc->i.action |= ATA_EH_HARDRESET;
2626 if (ap->flags & ATA_FLAG_SKIP_D2H_BSY)
2627 ata_wait_spinup(ap);
2630 /* if we're about to do hardreset, nothing more to do */
2631 if (ehc->i.action & ATA_EH_HARDRESET)
2634 /* if SATA, resume phy */
2635 if (ap->cbl == ATA_CBL_SATA) {
2636 if (ap->flags & ATA_FLAG_LOADING)
2637 timing = sata_deb_timing_boot;
2639 timing = sata_deb_timing_eh;
2641 rc = sata_phy_resume(ap, timing);
2642 if (rc && rc != -EOPNOTSUPP) {
2643 /* phy resume failed */
2644 ata_port_printk(ap, KERN_WARNING, "failed to resume "
2645 "link for reset (errno=%d)\n", rc);
2650 /* Wait for !BSY if the controller can wait for the first D2H
2651 * Reg FIS and we don't know that no device is attached.
2653 if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
2654 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2660 * ata_std_softreset - reset host port via ATA SRST
2661 * @ap: port to reset
2662 * @classes: resulting classes of attached devices
2664 * Reset host port using ATA SRST.
2667 * Kernel thread context (may sleep)
2670 * 0 on success, -errno otherwise.
2672 int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
2674 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2675 unsigned int devmask = 0, err_mask;
2680 if (ata_port_offline(ap)) {
2681 classes[0] = ATA_DEV_NONE;
2685 /* determine if device 0/1 are present */
2686 if (ata_devchk(ap, 0))
2687 devmask |= (1 << 0);
2688 if (slave_possible && ata_devchk(ap, 1))
2689 devmask |= (1 << 1);
2691 /* select device 0 again */
2692 ap->ops->dev_select(ap, 0);
2694 /* issue bus reset */
2695 DPRINTK("about to softreset, devmask=%x\n", devmask);
2696 err_mask = ata_bus_softreset(ap, devmask);
2698 ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
2703 /* determine by signature whether we have ATA or ATAPI devices */
2704 classes[0] = ata_dev_try_classify(ap, 0, &err);
2705 if (slave_possible && err != 0x81)
2706 classes[1] = ata_dev_try_classify(ap, 1, &err);
2709 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2714 * sata_std_hardreset - reset host port via SATA phy reset
2715 * @ap: port to reset
2716 * @class: resulting class of attached device
2718 * SATA phy-reset host port using DET bits of SControl register.
2721 * Kernel thread context (may sleep)
2724 * 0 on success, -errno otherwise.
2726 int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
2733 if (sata_set_spd_needed(ap)) {
2734 /* SATA spec says nothing about how to reconfigure
2735 * spd. To be on the safe side, turn off phy during
2736 * reconfiguration. This works for at least ICH7 AHCI
2739 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2742 scontrol = (scontrol & 0x0f0) | 0x302;
2744 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
2750 /* issue phy wake/reset */
2751 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2754 scontrol = (scontrol & 0x0f0) | 0x301;
2756 if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
2759 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
2760 * 10.4.2 says at least 1 ms.
2764 /* bring phy back */
2765 sata_phy_resume(ap, sata_deb_timing_eh);
2767 /* TODO: phy layer with polling, timeouts, etc. */
2768 if (ata_port_offline(ap)) {
2769 *class = ATA_DEV_NONE;
2770 DPRINTK("EXIT, link offline\n");
2774 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2775 ata_port_printk(ap, KERN_ERR,
2776 "COMRESET failed (device not ready)\n");
2780 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2782 *class = ata_dev_try_classify(ap, 0, NULL);
2784 DPRINTK("EXIT, class=%u\n", *class);
2789 * ata_std_postreset - standard postreset callback
2790 * @ap: the target ata_port
2791 * @classes: classes of attached devices
2793 * This function is invoked after a successful reset. Note that
2794 * the device might have been reset more than once using
2795 * different reset methods before postreset is invoked.
2798 * Kernel thread context (may sleep)
2800 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2806 /* print link status */
2807 sata_print_link_status(ap);
2810 if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
2811 sata_scr_write(ap, SCR_ERROR, serror);
2813 /* re-enable interrupts */
2814 if (!ap->ops->error_handler) {
2815 /* FIXME: hack. create a hook instead */
2816 if (ap->ioaddr.ctl_addr)
2820 /* is double-select really necessary? */
2821 if (classes[0] != ATA_DEV_NONE)
2822 ap->ops->dev_select(ap, 1);
2823 if (classes[1] != ATA_DEV_NONE)
2824 ap->ops->dev_select(ap, 0);
2826 /* bail out if no device is present */
2827 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2828 DPRINTK("EXIT, no device\n");
2832 /* set up device control */
2833 if (ap->ioaddr.ctl_addr) {
2834 if (ap->flags & ATA_FLAG_MMIO)
2835 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2837 outb(ap->ctl, ap->ioaddr.ctl_addr);
2844 * ata_dev_same_device - Determine whether new ID matches configured device
2845 * @dev: device to compare against
2846 * @new_class: class of the new device
2847 * @new_id: IDENTIFY page of the new device
2849 * Compare @new_class and @new_id against @dev and determine
2850 * whether @dev is the device indicated by @new_class and
2857 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2859 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
2862 const u16 *old_id = dev->id;
2863 unsigned char model[2][41], serial[2][21];
2866 if (dev->class != new_class) {
2867 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
2868 dev->class, new_class);
2872 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2873 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2874 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2875 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2876 new_n_sectors = ata_id_n_sectors(new_id);
2878 if (strcmp(model[0], model[1])) {
2879 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
2880 "'%s' != '%s'\n", model[0], model[1]);
2884 if (strcmp(serial[0], serial[1])) {
2885 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
2886 "'%s' != '%s'\n", serial[0], serial[1]);
2890 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2891 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
2893 (unsigned long long)dev->n_sectors,
2894 (unsigned long long)new_n_sectors);
2902 * ata_dev_revalidate - Revalidate ATA device
2903 * @dev: device to revalidate
2904 * @post_reset: is this revalidation after reset?
2906 * Re-read IDENTIFY page and make sure @dev is still attached to
2910 * Kernel thread context (may sleep)
2913 * 0 on success, negative errno otherwise
2915 int ata_dev_revalidate(struct ata_device *dev, int post_reset)
2917 unsigned int class = dev->class;
2918 u16 *id = (void *)dev->ap->sector_buf;
2921 if (!ata_dev_enabled(dev)) {
2927 rc = ata_dev_read_id(dev, &class, post_reset, id);
2931 /* is the device still there? */
2932 if (!ata_dev_same_device(dev, class, id)) {
2937 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
2939 /* configure device according to the new ID */
2940 rc = ata_dev_configure(dev, 0);
2945 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
2949 static const char * const ata_dma_blacklist [] = {
2950 "WDC AC11000H", NULL,
2951 "WDC AC22100H", NULL,
2952 "WDC AC32500H", NULL,
2953 "WDC AC33100H", NULL,
2954 "WDC AC31600H", NULL,
2955 "WDC AC32100H", "24.09P07",
2956 "WDC AC23200L", "21.10N21",
2957 "Compaq CRD-8241B", NULL,
2962 "SanDisk SDP3B", NULL,
2963 "SanDisk SDP3B-64", NULL,
2964 "SANYO CD-ROM CRD", NULL,
2965 "HITACHI CDR-8", NULL,
2966 "HITACHI CDR-8335", NULL,
2967 "HITACHI CDR-8435", NULL,
2968 "Toshiba CD-ROM XM-6202B", NULL,
2969 "TOSHIBA CD-ROM XM-1702BC", NULL,
2971 "E-IDE CD-ROM CR-840", NULL,
2972 "CD-ROM Drive/F5A", NULL,
2973 "WPI CDD-820", NULL,
2974 "SAMSUNG CD-ROM SC-148C", NULL,
2975 "SAMSUNG CD-ROM SC", NULL,
2976 "SanDisk SDP3B-64", NULL,
2977 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2978 "_NEC DV5800A", NULL,
2979 "SAMSUNG CD-ROM SN-124", "N001"
2982 static int ata_strim(char *s, size_t len)
2984 len = strnlen(s, len);
2986 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2987 while ((len > 0) && (s[len - 1] == ' ')) {
2994 static int ata_dma_blacklisted(const struct ata_device *dev)
2996 unsigned char model_num[40];
2997 unsigned char model_rev[16];
2998 unsigned int nlen, rlen;
3001 /* We don't support polling DMA.
3002 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3003 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3005 if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
3006 (dev->flags & ATA_DFLAG_CDB_INTR))
3009 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
3011 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
3013 nlen = ata_strim(model_num, sizeof(model_num));
3014 rlen = ata_strim(model_rev, sizeof(model_rev));
3016 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
3017 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
3018 if (ata_dma_blacklist[i+1] == NULL)
3020 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
3028 * ata_dev_xfermask - Compute supported xfermask of the given device
3029 * @dev: Device to compute xfermask for
3031 * Compute supported xfermask of @dev and store it in
3032 * dev->*_mask. This function is responsible for applying all
3033 * known limits including host controller limits, device
3036 * FIXME: The current implementation limits all transfer modes to
3037 * the fastest of the lowested device on the port. This is not
3038 * required on most controllers.
3043 static void ata_dev_xfermask(struct ata_device *dev)
3045 struct ata_port *ap = dev->ap;
3046 struct ata_host_set *hs = ap->host_set;
3047 unsigned long xfer_mask;
3050 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3051 ap->mwdma_mask, ap->udma_mask);
3053 /* Apply cable rule here. Don't apply it early because when
3054 * we handle hot plug the cable type can itself change.
3056 if (ap->cbl == ATA_CBL_PATA40)
3057 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
3059 /* FIXME: Use port-wide xfermask for now */
3060 for (i = 0; i < ATA_MAX_DEVICES; i++) {
3061 struct ata_device *d = &ap->device[i];
3063 if (ata_dev_absent(d))
3066 if (ata_dev_disabled(d)) {
3067 /* to avoid violating device selection timing */
3068 xfer_mask &= ata_pack_xfermask(d->pio_mask,
3069 UINT_MAX, UINT_MAX);
3073 xfer_mask &= ata_pack_xfermask(d->pio_mask,
3074 d->mwdma_mask, d->udma_mask);
3075 xfer_mask &= ata_id_xfermask(d->id);
3076 if (ata_dma_blacklisted(d))
3077 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3080 if (ata_dma_blacklisted(dev))
3081 ata_dev_printk(dev, KERN_WARNING,
3082 "device is on DMA blacklist, disabling DMA\n");
3084 if (hs->flags & ATA_HOST_SIMPLEX) {
3085 if (hs->simplex_claimed)
3086 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3089 if (ap->ops->mode_filter)
3090 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
3092 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
3093 &dev->mwdma_mask, &dev->udma_mask);
3097 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
3098 * @dev: Device to which command will be sent
3100 * Issue SET FEATURES - XFER MODE command to device @dev
3104 * PCI/etc. bus probe sem.
3107 * 0 on success, AC_ERR_* mask otherwise.
3110 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
3112 struct ata_taskfile tf;
3113 unsigned int err_mask;
3115 /* set up set-features taskfile */
3116 DPRINTK("set features - xfer mode\n");
3118 ata_tf_init(dev, &tf);
3119 tf.command = ATA_CMD_SET_FEATURES;
3120 tf.feature = SETFEATURES_XFER;
3121 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3122 tf.protocol = ATA_PROT_NODATA;
3123 tf.nsect = dev->xfer_mode;
3125 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
3127 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3132 * ata_dev_init_params - Issue INIT DEV PARAMS command
3133 * @dev: Device to which command will be sent
3134 * @heads: Number of heads (taskfile parameter)
3135 * @sectors: Number of sectors (taskfile parameter)
3138 * Kernel thread context (may sleep)
3141 * 0 on success, AC_ERR_* mask otherwise.
3143 static unsigned int ata_dev_init_params(struct ata_device *dev,
3144 u16 heads, u16 sectors)
3146 struct ata_taskfile tf;
3147 unsigned int err_mask;
3149 /* Number of sectors per track 1-255. Number of heads 1-16 */
3150 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
3151 return AC_ERR_INVALID;
3153 /* set up init dev params taskfile */
3154 DPRINTK("init dev params \n");
3156 ata_tf_init(dev, &tf);
3157 tf.command = ATA_CMD_INIT_DEV_PARAMS;
3158 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3159 tf.protocol = ATA_PROT_NODATA;
3161 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
3163 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
3165 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3170 * ata_sg_clean - Unmap DMA memory associated with command
3171 * @qc: Command containing DMA memory to be released
3173 * Unmap all mapped DMA memory associated with this command.
3176 * spin_lock_irqsave(host_set lock)
3179 static void ata_sg_clean(struct ata_queued_cmd *qc)
3181 struct ata_port *ap = qc->ap;
3182 struct scatterlist *sg = qc->__sg;
3183 int dir = qc->dma_dir;
3184 void *pad_buf = NULL;
3186 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
3187 WARN_ON(sg == NULL);
3189 if (qc->flags & ATA_QCFLAG_SINGLE)
3190 WARN_ON(qc->n_elem > 1);
3192 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
3194 /* if we padded the buffer out to 32-bit bound, and data
3195 * xfer direction is from-device, we must copy from the
3196 * pad buffer back into the supplied buffer
3198 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
3199 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3201 if (qc->flags & ATA_QCFLAG_SG) {
3203 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
3204 /* restore last sg */
3205 sg[qc->orig_n_elem - 1].length += qc->pad_len;
3207 struct scatterlist *psg = &qc->pad_sgent;
3208 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3209 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
3210 kunmap_atomic(addr, KM_IRQ0);
3214 dma_unmap_single(ap->dev,
3215 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
3218 sg->length += qc->pad_len;
3220 memcpy(qc->buf_virt + sg->length - qc->pad_len,
3221 pad_buf, qc->pad_len);
3224 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3229 * ata_fill_sg - Fill PCI IDE PRD table
3230 * @qc: Metadata associated with taskfile to be transferred
3232 * Fill PCI IDE PRD (scatter-gather) table with segments
3233 * associated with the current disk command.
3236 * spin_lock_irqsave(host_set lock)
3239 static void ata_fill_sg(struct ata_queued_cmd *qc)
3241 struct ata_port *ap = qc->ap;
3242 struct scatterlist *sg;
3245 WARN_ON(qc->__sg == NULL);
3246 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
3249 ata_for_each_sg(sg, qc) {
3253 /* determine if physical DMA addr spans 64K boundary.
3254 * Note h/w doesn't support 64-bit, so we unconditionally
3255 * truncate dma_addr_t to u32.
3257 addr = (u32) sg_dma_address(sg);
3258 sg_len = sg_dma_len(sg);
3261 offset = addr & 0xffff;
3263 if ((offset + sg_len) > 0x10000)
3264 len = 0x10000 - offset;
3266 ap->prd[idx].addr = cpu_to_le32(addr);
3267 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3268 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3277 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3280 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3281 * @qc: Metadata associated with taskfile to check
3283 * Allow low-level driver to filter ATA PACKET commands, returning
3284 * a status indicating whether or not it is OK to use DMA for the
3285 * supplied PACKET command.
3288 * spin_lock_irqsave(host_set lock)
3290 * RETURNS: 0 when ATAPI DMA can be used
3293 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3295 struct ata_port *ap = qc->ap;
3296 int rc = 0; /* Assume ATAPI DMA is OK by default */
3298 if (ap->ops->check_atapi_dma)
3299 rc = ap->ops->check_atapi_dma(qc);
3304 * ata_qc_prep - Prepare taskfile for submission
3305 * @qc: Metadata associated with taskfile to be prepared
3307 * Prepare ATA taskfile for submission.
3310 * spin_lock_irqsave(host_set lock)
3312 void ata_qc_prep(struct ata_queued_cmd *qc)
3314 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3320 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3323 * ata_sg_init_one - Associate command with memory buffer
3324 * @qc: Command to be associated
3325 * @buf: Memory buffer
3326 * @buflen: Length of memory buffer, in bytes.
3328 * Initialize the data-related elements of queued_cmd @qc
3329 * to point to a single memory buffer, @buf of byte length @buflen.
3332 * spin_lock_irqsave(host_set lock)
3335 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3337 struct scatterlist *sg;
3339 qc->flags |= ATA_QCFLAG_SINGLE;
3341 memset(&qc->sgent, 0, sizeof(qc->sgent));
3342 qc->__sg = &qc->sgent;
3344 qc->orig_n_elem = 1;
3346 qc->nbytes = buflen;
3349 sg_init_one(sg, buf, buflen);
3353 * ata_sg_init - Associate command with scatter-gather table.
3354 * @qc: Command to be associated
3355 * @sg: Scatter-gather table.
3356 * @n_elem: Number of elements in s/g table.
3358 * Initialize the data-related elements of queued_cmd @qc
3359 * to point to a scatter-gather table @sg, containing @n_elem
3363 * spin_lock_irqsave(host_set lock)
3366 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3367 unsigned int n_elem)
3369 qc->flags |= ATA_QCFLAG_SG;
3371 qc->n_elem = n_elem;
3372 qc->orig_n_elem = n_elem;
3376 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3377 * @qc: Command with memory buffer to be mapped.
3379 * DMA-map the memory buffer associated with queued_cmd @qc.
3382 * spin_lock_irqsave(host_set lock)
3385 * Zero on success, negative on error.
3388 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3390 struct ata_port *ap = qc->ap;
3391 int dir = qc->dma_dir;
3392 struct scatterlist *sg = qc->__sg;
3393 dma_addr_t dma_address;
3396 /* we must lengthen transfers to end on a 32-bit boundary */
3397 qc->pad_len = sg->length & 3;
3399 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3400 struct scatterlist *psg = &qc->pad_sgent;
3402 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3404 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3406 if (qc->tf.flags & ATA_TFLAG_WRITE)
3407 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3410 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3411 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3413 sg->length -= qc->pad_len;
3414 if (sg->length == 0)
3417 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3418 sg->length, qc->pad_len);
3426 dma_address = dma_map_single(ap->dev, qc->buf_virt,
3428 if (dma_mapping_error(dma_address)) {
3430 sg->length += qc->pad_len;
3434 sg_dma_address(sg) = dma_address;
3435 sg_dma_len(sg) = sg->length;
3438 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3439 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3445 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3446 * @qc: Command with scatter-gather table to be mapped.
3448 * DMA-map the scatter-gather table associated with queued_cmd @qc.
3451 * spin_lock_irqsave(host_set lock)
3454 * Zero on success, negative on error.
3458 static int ata_sg_setup(struct ata_queued_cmd *qc)
3460 struct ata_port *ap = qc->ap;
3461 struct scatterlist *sg = qc->__sg;
3462 struct scatterlist *lsg = &sg[qc->n_elem - 1];
3463 int n_elem, pre_n_elem, dir, trim_sg = 0;
3465 VPRINTK("ENTER, ata%u\n", ap->id);
3466 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
3468 /* we must lengthen transfers to end on a 32-bit boundary */
3469 qc->pad_len = lsg->length & 3;
3471 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3472 struct scatterlist *psg = &qc->pad_sgent;
3473 unsigned int offset;
3475 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3477 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3480 * psg->page/offset are used to copy to-be-written
3481 * data in this function or read data in ata_sg_clean.
3483 offset = lsg->offset + lsg->length - qc->pad_len;
3484 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3485 psg->offset = offset_in_page(offset);
3487 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3488 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3489 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
3490 kunmap_atomic(addr, KM_IRQ0);
3493 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3494 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3496 lsg->length -= qc->pad_len;
3497 if (lsg->length == 0)
3500 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3501 qc->n_elem - 1, lsg->length, qc->pad_len);
3504 pre_n_elem = qc->n_elem;
3505 if (trim_sg && pre_n_elem)
3514 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
3516 /* restore last sg */
3517 lsg->length += qc->pad_len;
3521 DPRINTK("%d sg elements mapped\n", n_elem);
3524 qc->n_elem = n_elem;
3530 * swap_buf_le16 - swap halves of 16-bit words in place
3531 * @buf: Buffer to swap
3532 * @buf_words: Number of 16-bit words in buffer.
3534 * Swap halves of 16-bit words if needed to convert from
3535 * little-endian byte order to native cpu byte order, or
3539 * Inherited from caller.
3541 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3546 for (i = 0; i < buf_words; i++)
3547 buf[i] = le16_to_cpu(buf[i]);
3548 #endif /* __BIG_ENDIAN */
3552 * ata_mmio_data_xfer - Transfer data by MMIO
3553 * @adev: device for this I/O
3555 * @buflen: buffer length
3556 * @write_data: read/write
3558 * Transfer data from/to the device data register by MMIO.
3561 * Inherited from caller.
3564 void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
3565 unsigned int buflen, int write_data)
3567 struct ata_port *ap = adev->ap;
3569 unsigned int words = buflen >> 1;
3570 u16 *buf16 = (u16 *) buf;
3571 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3573 /* Transfer multiple of 2 bytes */
3575 for (i = 0; i < words; i++)
3576 writew(le16_to_cpu(buf16[i]), mmio);
3578 for (i = 0; i < words; i++)
3579 buf16[i] = cpu_to_le16(readw(mmio));
3582 /* Transfer trailing 1 byte, if any. */
3583 if (unlikely(buflen & 0x01)) {
3584 u16 align_buf[1] = { 0 };
3585 unsigned char *trailing_buf = buf + buflen - 1;
3588 memcpy(align_buf, trailing_buf, 1);
3589 writew(le16_to_cpu(align_buf[0]), mmio);
3591 align_buf[0] = cpu_to_le16(readw(mmio));
3592 memcpy(trailing_buf, align_buf, 1);
3598 * ata_pio_data_xfer - Transfer data by PIO
3599 * @adev: device to target
3601 * @buflen: buffer length
3602 * @write_data: read/write
3604 * Transfer data from/to the device data register by PIO.
3607 * Inherited from caller.
3610 void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
3611 unsigned int buflen, int write_data)
3613 struct ata_port *ap = adev->ap;
3614 unsigned int words = buflen >> 1;
3616 /* Transfer multiple of 2 bytes */
3618 outsw(ap->ioaddr.data_addr, buf, words);
3620 insw(ap->ioaddr.data_addr, buf, words);
3622 /* Transfer trailing 1 byte, if any. */
3623 if (unlikely(buflen & 0x01)) {
3624 u16 align_buf[1] = { 0 };
3625 unsigned char *trailing_buf = buf + buflen - 1;
3628 memcpy(align_buf, trailing_buf, 1);
3629 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3631 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3632 memcpy(trailing_buf, align_buf, 1);
3638 * ata_pio_data_xfer_noirq - Transfer data by PIO
3639 * @adev: device to target
3641 * @buflen: buffer length
3642 * @write_data: read/write
3644 * Transfer data from/to the device data register by PIO. Do the
3645 * transfer with interrupts disabled.
3648 * Inherited from caller.
3651 void ata_pio_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
3652 unsigned int buflen, int write_data)
3654 unsigned long flags;
3655 local_irq_save(flags);
3656 ata_pio_data_xfer(adev, buf, buflen, write_data);
3657 local_irq_restore(flags);
3662 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3663 * @qc: Command on going
3665 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3668 * Inherited from caller.
3671 static void ata_pio_sector(struct ata_queued_cmd *qc)
3673 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3674 struct scatterlist *sg = qc->__sg;
3675 struct ata_port *ap = qc->ap;
3677 unsigned int offset;
3680 if (qc->cursect == (qc->nsect - 1))
3681 ap->hsm_task_state = HSM_ST_LAST;
3683 page = sg[qc->cursg].page;
3684 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3686 /* get the current page and offset */
3687 page = nth_page(page, (offset >> PAGE_SHIFT));
3688 offset %= PAGE_SIZE;
3690 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3692 if (PageHighMem(page)) {
3693 unsigned long flags;
3695 /* FIXME: use a bounce buffer */
3696 local_irq_save(flags);
3697 buf = kmap_atomic(page, KM_IRQ0);
3699 /* do the actual data transfer */
3700 ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
3702 kunmap_atomic(buf, KM_IRQ0);
3703 local_irq_restore(flags);
3705 buf = page_address(page);
3706 ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
3712 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3719 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3720 * @qc: Command on going
3722 * Transfer one or many ATA_SECT_SIZE of data from/to the
3723 * ATA device for the DRQ request.
3726 * Inherited from caller.
3729 static void ata_pio_sectors(struct ata_queued_cmd *qc)
3731 if (is_multi_taskfile(&qc->tf)) {
3732 /* READ/WRITE MULTIPLE */
3735 WARN_ON(qc->dev->multi_count == 0);
3737 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3745 * atapi_send_cdb - Write CDB bytes to hardware
3746 * @ap: Port to which ATAPI device is attached.
3747 * @qc: Taskfile currently active
3749 * When device has indicated its readiness to accept
3750 * a CDB, this function is called. Send the CDB.
3756 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3759 DPRINTK("send cdb\n");
3760 WARN_ON(qc->dev->cdb_len < 12);
3762 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
3763 ata_altstatus(ap); /* flush */
3765 switch (qc->tf.protocol) {
3766 case ATA_PROT_ATAPI:
3767 ap->hsm_task_state = HSM_ST;
3769 case ATA_PROT_ATAPI_NODATA:
3770 ap->hsm_task_state = HSM_ST_LAST;
3772 case ATA_PROT_ATAPI_DMA:
3773 ap->hsm_task_state = HSM_ST_LAST;
3774 /* initiate bmdma */
3775 ap->ops->bmdma_start(qc);
3781 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3782 * @qc: Command on going
3783 * @bytes: number of bytes
3785 * Transfer Transfer data from/to the ATAPI device.
3788 * Inherited from caller.
3792 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3794 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3795 struct scatterlist *sg = qc->__sg;
3796 struct ata_port *ap = qc->ap;
3799 unsigned int offset, count;
3801 if (qc->curbytes + bytes >= qc->nbytes)
3802 ap->hsm_task_state = HSM_ST_LAST;
3805 if (unlikely(qc->cursg >= qc->n_elem)) {
3807 * The end of qc->sg is reached and the device expects
3808 * more data to transfer. In order not to overrun qc->sg
3809 * and fulfill length specified in the byte count register,
3810 * - for read case, discard trailing data from the device
3811 * - for write case, padding zero data to the device
3813 u16 pad_buf[1] = { 0 };
3814 unsigned int words = bytes >> 1;
3817 if (words) /* warning if bytes > 1 */
3818 ata_dev_printk(qc->dev, KERN_WARNING,
3819 "%u bytes trailing data\n", bytes);
3821 for (i = 0; i < words; i++)
3822 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
3824 ap->hsm_task_state = HSM_ST_LAST;
3828 sg = &qc->__sg[qc->cursg];
3831 offset = sg->offset + qc->cursg_ofs;
3833 /* get the current page and offset */
3834 page = nth_page(page, (offset >> PAGE_SHIFT));
3835 offset %= PAGE_SIZE;
3837 /* don't overrun current sg */
3838 count = min(sg->length - qc->cursg_ofs, bytes);
3840 /* don't cross page boundaries */
3841 count = min(count, (unsigned int)PAGE_SIZE - offset);
3843 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3845 if (PageHighMem(page)) {
3846 unsigned long flags;
3848 /* FIXME: use bounce buffer */
3849 local_irq_save(flags);
3850 buf = kmap_atomic(page, KM_IRQ0);
3852 /* do the actual data transfer */
3853 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
3855 kunmap_atomic(buf, KM_IRQ0);
3856 local_irq_restore(flags);
3858 buf = page_address(page);
3859 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
3863 qc->curbytes += count;
3864 qc->cursg_ofs += count;
3866 if (qc->cursg_ofs == sg->length) {
3876 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3877 * @qc: Command on going
3879 * Transfer Transfer data from/to the ATAPI device.
3882 * Inherited from caller.
3885 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3887 struct ata_port *ap = qc->ap;
3888 struct ata_device *dev = qc->dev;
3889 unsigned int ireason, bc_lo, bc_hi, bytes;
3890 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3892 /* Abuse qc->result_tf for temp storage of intermediate TF
3893 * here to save some kernel stack usage.
3894 * For normal completion, qc->result_tf is not relevant. For
3895 * error, qc->result_tf is later overwritten by ata_qc_complete().
3896 * So, the correctness of qc->result_tf is not affected.
3898 ap->ops->tf_read(ap, &qc->result_tf);
3899 ireason = qc->result_tf.nsect;
3900 bc_lo = qc->result_tf.lbam;
3901 bc_hi = qc->result_tf.lbah;
3902 bytes = (bc_hi << 8) | bc_lo;
3904 /* shall be cleared to zero, indicating xfer of data */
3905 if (ireason & (1 << 0))
3908 /* make sure transfer direction matches expected */
3909 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3910 if (do_write != i_write)
3913 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3915 __atapi_pio_bytes(qc, bytes);
3920 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
3921 qc->err_mask |= AC_ERR_HSM;
3922 ap->hsm_task_state = HSM_ST_ERR;
3926 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
3927 * @ap: the target ata_port
3931 * 1 if ok in workqueue, 0 otherwise.
3934 static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
3936 if (qc->tf.flags & ATA_TFLAG_POLLING)
3939 if (ap->hsm_task_state == HSM_ST_FIRST) {
3940 if (qc->tf.protocol == ATA_PROT_PIO &&
3941 (qc->tf.flags & ATA_TFLAG_WRITE))
3944 if (is_atapi_taskfile(&qc->tf) &&
3945 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3953 * ata_hsm_qc_complete - finish a qc running on standard HSM
3954 * @qc: Command to complete
3955 * @in_wq: 1 if called from workqueue, 0 otherwise
3957 * Finish @qc which is running on standard HSM.
3960 * If @in_wq is zero, spin_lock_irqsave(host_set lock).
3961 * Otherwise, none on entry and grabs host lock.
3963 static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
3965 struct ata_port *ap = qc->ap;
3966 unsigned long flags;
3968 if (ap->ops->error_handler) {
3970 spin_lock_irqsave(ap->lock, flags);
3972 /* EH might have kicked in while host_set lock
3975 qc = ata_qc_from_tag(ap, qc->tag);
3977 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
3979 ata_qc_complete(qc);
3981 ata_port_freeze(ap);
3984 spin_unlock_irqrestore(ap->lock, flags);
3986 if (likely(!(qc->err_mask & AC_ERR_HSM)))
3987 ata_qc_complete(qc);
3989 ata_port_freeze(ap);
3993 spin_lock_irqsave(ap->lock, flags);
3995 ata_qc_complete(qc);
3996 spin_unlock_irqrestore(ap->lock, flags);
3998 ata_qc_complete(qc);
4001 ata_altstatus(ap); /* flush */
4005 * ata_hsm_move - move the HSM to the next state.
4006 * @ap: the target ata_port
4008 * @status: current device status
4009 * @in_wq: 1 if called from workqueue, 0 otherwise
4012 * 1 when poll next status needed, 0 otherwise.
4014 int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
4015 u8 status, int in_wq)
4017 unsigned long flags = 0;
4020 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
4022 /* Make sure ata_qc_issue_prot() does not throw things
4023 * like DMA polling into the workqueue. Notice that
4024 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
4026 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
4029 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4030 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4032 switch (ap->hsm_task_state) {
4034 /* Send first data block or PACKET CDB */
4036 /* If polling, we will stay in the work queue after
4037 * sending the data. Otherwise, interrupt handler
4038 * takes over after sending the data.
4040 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
4042 /* check device status */
4043 if (unlikely((status & ATA_DRQ) == 0)) {
4044 /* handle BSY=0, DRQ=0 as error */
4045 if (likely(status & (ATA_ERR | ATA_DF)))
4046 /* device stops HSM for abort/error */
4047 qc->err_mask |= AC_ERR_DEV;
4049 /* HSM violation. Let EH handle this */
4050 qc->err_mask |= AC_ERR_HSM;
4052 ap->hsm_task_state = HSM_ST_ERR;
4056 /* Device should not ask for data transfer (DRQ=1)
4057 * when it finds something wrong.
4058 * We ignore DRQ here and stop the HSM by
4059 * changing hsm_task_state to HSM_ST_ERR and
4060 * let the EH abort the command or reset the device.
4062 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4063 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
4065 qc->err_mask |= AC_ERR_HSM;
4066 ap->hsm_task_state = HSM_ST_ERR;
4070 /* Send the CDB (atapi) or the first data block (ata pio out).
4071 * During the state transition, interrupt handler shouldn't
4072 * be invoked before the data transfer is complete and
4073 * hsm_task_state is changed. Hence, the following locking.
4076 spin_lock_irqsave(ap->lock, flags);
4078 if (qc->tf.protocol == ATA_PROT_PIO) {
4079 /* PIO data out protocol.
4080 * send first data block.
4083 /* ata_pio_sectors() might change the state
4084 * to HSM_ST_LAST. so, the state is changed here
4085 * before ata_pio_sectors().
4087 ap->hsm_task_state = HSM_ST;
4088 ata_pio_sectors(qc);
4089 ata_altstatus(ap); /* flush */
4092 atapi_send_cdb(ap, qc);
4095 spin_unlock_irqrestore(ap->lock, flags);
4097 /* if polling, ata_pio_task() handles the rest.
4098 * otherwise, interrupt handler takes over from here.
4103 /* complete command or read/write the data register */
4104 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4105 /* ATAPI PIO protocol */
4106 if ((status & ATA_DRQ) == 0) {
4107 /* No more data to transfer or device error.
4108 * Device error will be tagged in HSM_ST_LAST.
4110 ap->hsm_task_state = HSM_ST_LAST;
4114 /* Device should not ask for data transfer (DRQ=1)
4115 * when it finds something wrong.
4116 * We ignore DRQ here and stop the HSM by
4117 * changing hsm_task_state to HSM_ST_ERR and
4118 * let the EH abort the command or reset the device.
4120 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4121 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
4123 qc->err_mask |= AC_ERR_HSM;
4124 ap->hsm_task_state = HSM_ST_ERR;
4128 atapi_pio_bytes(qc);
4130 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4131 /* bad ireason reported by device */
4135 /* ATA PIO protocol */
4136 if (unlikely((status & ATA_DRQ) == 0)) {
4137 /* handle BSY=0, DRQ=0 as error */
4138 if (likely(status & (ATA_ERR | ATA_DF)))
4139 /* device stops HSM for abort/error */
4140 qc->err_mask |= AC_ERR_DEV;
4142 /* HSM violation. Let EH handle this */
4143 qc->err_mask |= AC_ERR_HSM;
4145 ap->hsm_task_state = HSM_ST_ERR;
4149 /* For PIO reads, some devices may ask for
4150 * data transfer (DRQ=1) alone with ERR=1.
4151 * We respect DRQ here and transfer one
4152 * block of junk data before changing the
4153 * hsm_task_state to HSM_ST_ERR.
4155 * For PIO writes, ERR=1 DRQ=1 doesn't make
4156 * sense since the data block has been
4157 * transferred to the device.
4159 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4160 /* data might be corrputed */
4161 qc->err_mask |= AC_ERR_DEV;
4163 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
4164 ata_pio_sectors(qc);
4166 status = ata_wait_idle(ap);
4169 if (status & (ATA_BUSY | ATA_DRQ))
4170 qc->err_mask |= AC_ERR_HSM;
4172 /* ata_pio_sectors() might change the
4173 * state to HSM_ST_LAST. so, the state
4174 * is changed after ata_pio_sectors().
4176 ap->hsm_task_state = HSM_ST_ERR;
4180 ata_pio_sectors(qc);
4182 if (ap->hsm_task_state == HSM_ST_LAST &&
4183 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4186 status = ata_wait_idle(ap);
4191 ata_altstatus(ap); /* flush */
4196 if (unlikely(!ata_ok(status))) {
4197 qc->err_mask |= __ac_err_mask(status);
4198 ap->hsm_task_state = HSM_ST_ERR;
4202 /* no more data to transfer */
4203 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
4204 ap->id, qc->dev->devno, status);
4206 WARN_ON(qc->err_mask);
4208 ap->hsm_task_state = HSM_ST_IDLE;
4210 /* complete taskfile transaction */
4211 ata_hsm_qc_complete(qc, in_wq);
4217 /* make sure qc->err_mask is available to
4218 * know what's wrong and recover
4220 WARN_ON(qc->err_mask == 0);
4222 ap->hsm_task_state = HSM_ST_IDLE;
4224 /* complete taskfile transaction */
4225 ata_hsm_qc_complete(qc, in_wq);
4237 static void ata_pio_task(void *_data)
4239 struct ata_queued_cmd *qc = _data;
4240 struct ata_port *ap = qc->ap;
4245 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
4248 * This is purely heuristic. This is a fast path.
4249 * Sometimes when we enter, BSY will be cleared in
4250 * a chk-status or two. If not, the drive is probably seeking
4251 * or something. Snooze for a couple msecs, then
4252 * chk-status again. If still busy, queue delayed work.
4254 status = ata_busy_wait(ap, ATA_BUSY, 5);
4255 if (status & ATA_BUSY) {
4257 status = ata_busy_wait(ap, ATA_BUSY, 10);
4258 if (status & ATA_BUSY) {
4259 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
4265 poll_next = ata_hsm_move(ap, qc, status, 1);
4267 /* another command or interrupt handler
4268 * may be running at this point.
4275 * ata_qc_new - Request an available ATA command, for queueing
4276 * @ap: Port associated with device @dev
4277 * @dev: Device from whom we request an available command structure
4283 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4285 struct ata_queued_cmd *qc = NULL;
4288 /* no command while frozen */
4289 if (unlikely(ap->flags & ATA_FLAG_FROZEN))
4292 /* the last tag is reserved for internal command. */
4293 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
4294 if (!test_and_set_bit(i, &ap->qc_allocated)) {
4295 qc = __ata_qc_from_tag(ap, i);
4306 * ata_qc_new_init - Request an available ATA command, and initialize it
4307 * @dev: Device from whom we request an available command structure
4313 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4315 struct ata_port *ap = dev->ap;
4316 struct ata_queued_cmd *qc;
4318 qc = ata_qc_new(ap);
4331 * ata_qc_free - free unused ata_queued_cmd
4332 * @qc: Command to complete
4334 * Designed to free unused ata_queued_cmd object
4335 * in case something prevents using it.
4338 * spin_lock_irqsave(host_set lock)
4340 void ata_qc_free(struct ata_queued_cmd *qc)
4342 struct ata_port *ap = qc->ap;
4345 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4349 if (likely(ata_tag_valid(tag))) {
4350 qc->tag = ATA_TAG_POISON;
4351 clear_bit(tag, &ap->qc_allocated);
4355 void __ata_qc_complete(struct ata_queued_cmd *qc)
4357 struct ata_port *ap = qc->ap;
4359 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4360 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4362 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4365 /* command should be marked inactive atomically with qc completion */
4366 if (qc->tf.protocol == ATA_PROT_NCQ)
4367 ap->sactive &= ~(1 << qc->tag);
4369 ap->active_tag = ATA_TAG_POISON;
4371 /* atapi: mark qc as inactive to prevent the interrupt handler
4372 * from completing the command twice later, before the error handler
4373 * is called. (when rc != 0 and atapi request sense is needed)
4375 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4376 ap->qc_active &= ~(1 << qc->tag);
4378 /* call completion callback */
4379 qc->complete_fn(qc);
4383 * ata_qc_complete - Complete an active ATA command
4384 * @qc: Command to complete
4385 * @err_mask: ATA Status register contents
4387 * Indicate to the mid and upper layers that an ATA
4388 * command has completed, with either an ok or not-ok status.
4391 * spin_lock_irqsave(host_set lock)
4393 void ata_qc_complete(struct ata_queued_cmd *qc)
4395 struct ata_port *ap = qc->ap;
4397 /* XXX: New EH and old EH use different mechanisms to
4398 * synchronize EH with regular execution path.
4400 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4401 * Normal execution path is responsible for not accessing a
4402 * failed qc. libata core enforces the rule by returning NULL
4403 * from ata_qc_from_tag() for failed qcs.
4405 * Old EH depends on ata_qc_complete() nullifying completion
4406 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4407 * not synchronize with interrupt handler. Only PIO task is
4410 if (ap->ops->error_handler) {
4411 WARN_ON(ap->flags & ATA_FLAG_FROZEN);
4413 if (unlikely(qc->err_mask))
4414 qc->flags |= ATA_QCFLAG_FAILED;
4416 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4417 if (!ata_tag_internal(qc->tag)) {
4418 /* always fill result TF for failed qc */
4419 ap->ops->tf_read(ap, &qc->result_tf);
4420 ata_qc_schedule_eh(qc);
4425 /* read result TF if requested */
4426 if (qc->flags & ATA_QCFLAG_RESULT_TF)
4427 ap->ops->tf_read(ap, &qc->result_tf);
4429 __ata_qc_complete(qc);
4431 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4434 /* read result TF if failed or requested */
4435 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
4436 ap->ops->tf_read(ap, &qc->result_tf);
4438 __ata_qc_complete(qc);
4443 * ata_qc_complete_multiple - Complete multiple qcs successfully
4444 * @ap: port in question
4445 * @qc_active: new qc_active mask
4446 * @finish_qc: LLDD callback invoked before completing a qc
4448 * Complete in-flight commands. This functions is meant to be
4449 * called from low-level driver's interrupt routine to complete
4450 * requests normally. ap->qc_active and @qc_active is compared
4451 * and commands are completed accordingly.
4454 * spin_lock_irqsave(host_set lock)
4457 * Number of completed commands on success, -errno otherwise.
4459 int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
4460 void (*finish_qc)(struct ata_queued_cmd *))
4466 done_mask = ap->qc_active ^ qc_active;
4468 if (unlikely(done_mask & qc_active)) {
4469 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4470 "(%08x->%08x)\n", ap->qc_active, qc_active);
4474 for (i = 0; i < ATA_MAX_QUEUE; i++) {
4475 struct ata_queued_cmd *qc;
4477 if (!(done_mask & (1 << i)))
4480 if ((qc = ata_qc_from_tag(ap, i))) {
4483 ata_qc_complete(qc);
4491 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4493 struct ata_port *ap = qc->ap;
4495 switch (qc->tf.protocol) {
4498 case ATA_PROT_ATAPI_DMA:
4501 case ATA_PROT_ATAPI:
4503 if (ap->flags & ATA_FLAG_PIO_DMA)
4516 * ata_qc_issue - issue taskfile to device
4517 * @qc: command to issue to device
4519 * Prepare an ATA command to submission to device.
4520 * This includes mapping the data into a DMA-able
4521 * area, filling in the S/G table, and finally
4522 * writing the taskfile to hardware, starting the command.
4525 * spin_lock_irqsave(host_set lock)
4527 void ata_qc_issue(struct ata_queued_cmd *qc)
4529 struct ata_port *ap = qc->ap;
4531 /* Make sure only one non-NCQ command is outstanding. The
4532 * check is skipped for old EH because it reuses active qc to
4533 * request ATAPI sense.
4535 WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
4537 if (qc->tf.protocol == ATA_PROT_NCQ) {
4538 WARN_ON(ap->sactive & (1 << qc->tag));
4539 ap->sactive |= 1 << qc->tag;
4541 WARN_ON(ap->sactive);
4542 ap->active_tag = qc->tag;
4545 qc->flags |= ATA_QCFLAG_ACTIVE;
4546 ap->qc_active |= 1 << qc->tag;
4548 if (ata_should_dma_map(qc)) {
4549 if (qc->flags & ATA_QCFLAG_SG) {
4550 if (ata_sg_setup(qc))
4552 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4553 if (ata_sg_setup_one(qc))
4557 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4560 ap->ops->qc_prep(qc);
4562 qc->err_mask |= ap->ops->qc_issue(qc);
4563 if (unlikely(qc->err_mask))
4568 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4569 qc->err_mask |= AC_ERR_SYSTEM;
4571 ata_qc_complete(qc);
4575 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4576 * @qc: command to issue to device
4578 * Using various libata functions and hooks, this function
4579 * starts an ATA command. ATA commands are grouped into
4580 * classes called "protocols", and issuing each type of protocol
4581 * is slightly different.
4583 * May be used as the qc_issue() entry in ata_port_operations.
4586 * spin_lock_irqsave(host_set lock)
4589 * Zero on success, AC_ERR_* mask on failure
4592 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
4594 struct ata_port *ap = qc->ap;
4596 /* Use polling pio if the LLD doesn't handle
4597 * interrupt driven pio and atapi CDB interrupt.
4599 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4600 switch (qc->tf.protocol) {
4602 case ATA_PROT_ATAPI:
4603 case ATA_PROT_ATAPI_NODATA:
4604 qc->tf.flags |= ATA_TFLAG_POLLING;
4606 case ATA_PROT_ATAPI_DMA:
4607 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
4608 /* see ata_dma_blacklisted() */
4616 /* select the device */
4617 ata_dev_select(ap, qc->dev->devno, 1, 0);
4619 /* start the command */
4620 switch (qc->tf.protocol) {
4621 case ATA_PROT_NODATA:
4622 if (qc->tf.flags & ATA_TFLAG_POLLING)
4623 ata_qc_set_polling(qc);
4625 ata_tf_to_host(ap, &qc->tf);
4626 ap->hsm_task_state = HSM_ST_LAST;
4628 if (qc->tf.flags & ATA_TFLAG_POLLING)
4629 ata_port_queue_task(ap, ata_pio_task, qc, 0);
4634 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
4636 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4637 ap->ops->bmdma_setup(qc); /* set up bmdma */
4638 ap->ops->bmdma_start(qc); /* initiate bmdma */
4639 ap->hsm_task_state = HSM_ST_LAST;
4643 if (qc->tf.flags & ATA_TFLAG_POLLING)
4644 ata_qc_set_polling(qc);
4646 ata_tf_to_host(ap, &qc->tf);
4648 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4649 /* PIO data out protocol */
4650 ap->hsm_task_state = HSM_ST_FIRST;
4651 ata_port_queue_task(ap, ata_pio_task, qc, 0);
4653 /* always send first data block using
4654 * the ata_pio_task() codepath.
4657 /* PIO data in protocol */
4658 ap->hsm_task_state = HSM_ST;
4660 if (qc->tf.flags & ATA_TFLAG_POLLING)
4661 ata_port_queue_task(ap, ata_pio_task, qc, 0);
4663 /* if polling, ata_pio_task() handles the rest.
4664 * otherwise, interrupt handler takes over from here.
4670 case ATA_PROT_ATAPI:
4671 case ATA_PROT_ATAPI_NODATA:
4672 if (qc->tf.flags & ATA_TFLAG_POLLING)
4673 ata_qc_set_polling(qc);
4675 ata_tf_to_host(ap, &qc->tf);
4677 ap->hsm_task_state = HSM_ST_FIRST;
4679 /* send cdb by polling if no cdb interrupt */
4680 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4681 (qc->tf.flags & ATA_TFLAG_POLLING))
4682 ata_port_queue_task(ap, ata_pio_task, qc, 0);
4685 case ATA_PROT_ATAPI_DMA:
4686 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
4688 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4689 ap->ops->bmdma_setup(qc); /* set up bmdma */
4690 ap->hsm_task_state = HSM_ST_FIRST;
4692 /* send cdb by polling if no cdb interrupt */
4693 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4694 ata_port_queue_task(ap, ata_pio_task, qc, 0);
4699 return AC_ERR_SYSTEM;
4706 * ata_host_intr - Handle host interrupt for given (port, task)
4707 * @ap: Port on which interrupt arrived (possibly...)
4708 * @qc: Taskfile currently active in engine
4710 * Handle host interrupt for given queued command. Currently,
4711 * only DMA interrupts are handled. All other commands are
4712 * handled via polling with interrupts disabled (nIEN bit).
4715 * spin_lock_irqsave(host_set lock)
4718 * One if interrupt was handled, zero if not (shared irq).
4721 inline unsigned int ata_host_intr (struct ata_port *ap,
4722 struct ata_queued_cmd *qc)
4724 u8 status, host_stat = 0;
4726 VPRINTK("ata%u: protocol %d task_state %d\n",
4727 ap->id, qc->tf.protocol, ap->hsm_task_state);
4729 /* Check whether we are expecting interrupt in this state */
4730 switch (ap->hsm_task_state) {
4732 /* Some pre-ATAPI-4 devices assert INTRQ
4733 * at this state when ready to receive CDB.
4736 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4737 * The flag was turned on only for atapi devices.
4738 * No need to check is_atapi_taskfile(&qc->tf) again.
4740 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4744 if (qc->tf.protocol == ATA_PROT_DMA ||
4745 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4746 /* check status of DMA engine */
4747 host_stat = ap->ops->bmdma_status(ap);
4748 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4750 /* if it's not our irq... */
4751 if (!(host_stat & ATA_DMA_INTR))
4754 /* before we do anything else, clear DMA-Start bit */
4755 ap->ops->bmdma_stop(qc);
4757 if (unlikely(host_stat & ATA_DMA_ERR)) {
4758 /* error when transfering data to/from memory */
4759 qc->err_mask |= AC_ERR_HOST_BUS;
4760 ap->hsm_task_state = HSM_ST_ERR;
4770 /* check altstatus */
4771 status = ata_altstatus(ap);
4772 if (status & ATA_BUSY)
4775 /* check main status, clearing INTRQ */
4776 status = ata_chk_status(ap);
4777 if (unlikely(status & ATA_BUSY))
4780 /* ack bmdma irq events */
4781 ap->ops->irq_clear(ap);
4783 ata_hsm_move(ap, qc, status, 0);
4784 return 1; /* irq handled */
4787 ap->stats.idle_irq++;
4790 if ((ap->stats.idle_irq % 1000) == 0) {
4791 ata_irq_ack(ap, 0); /* debug trap */
4792 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
4796 return 0; /* irq not handled */
4800 * ata_interrupt - Default ATA host interrupt handler
4801 * @irq: irq line (unused)
4802 * @dev_instance: pointer to our ata_host_set information structure
4805 * Default interrupt handler for PCI IDE devices. Calls
4806 * ata_host_intr() for each port that is not disabled.
4809 * Obtains host_set lock during operation.
4812 * IRQ_NONE or IRQ_HANDLED.
4815 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4817 struct ata_host_set *host_set = dev_instance;
4819 unsigned int handled = 0;
4820 unsigned long flags;
4822 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4823 spin_lock_irqsave(&host_set->lock, flags);
4825 for (i = 0; i < host_set->n_ports; i++) {
4826 struct ata_port *ap;
4828 ap = host_set->ports[i];
4830 !(ap->flags & ATA_FLAG_DISABLED)) {
4831 struct ata_queued_cmd *qc;
4833 qc = ata_qc_from_tag(ap, ap->active_tag);
4834 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
4835 (qc->flags & ATA_QCFLAG_ACTIVE))
4836 handled |= ata_host_intr(ap, qc);
4840 spin_unlock_irqrestore(&host_set->lock, flags);
4842 return IRQ_RETVAL(handled);
4846 * sata_scr_valid - test whether SCRs are accessible
4847 * @ap: ATA port to test SCR accessibility for
4849 * Test whether SCRs are accessible for @ap.
4855 * 1 if SCRs are accessible, 0 otherwise.
4857 int sata_scr_valid(struct ata_port *ap)
4859 return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
4863 * sata_scr_read - read SCR register of the specified port
4864 * @ap: ATA port to read SCR for
4866 * @val: Place to store read value
4868 * Read SCR register @reg of @ap into *@val. This function is
4869 * guaranteed to succeed if the cable type of the port is SATA
4870 * and the port implements ->scr_read.
4876 * 0 on success, negative errno on failure.
4878 int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
4880 if (sata_scr_valid(ap)) {
4881 *val = ap->ops->scr_read(ap, reg);
4888 * sata_scr_write - write SCR register of the specified port
4889 * @ap: ATA port to write SCR for
4890 * @reg: SCR to write
4891 * @val: value to write
4893 * Write @val to SCR register @reg of @ap. This function is
4894 * guaranteed to succeed if the cable type of the port is SATA
4895 * and the port implements ->scr_read.
4901 * 0 on success, negative errno on failure.
4903 int sata_scr_write(struct ata_port *ap, int reg, u32 val)
4905 if (sata_scr_valid(ap)) {
4906 ap->ops->scr_write(ap, reg, val);
4913 * sata_scr_write_flush - write SCR register of the specified port and flush
4914 * @ap: ATA port to write SCR for
4915 * @reg: SCR to write
4916 * @val: value to write
4918 * This function is identical to sata_scr_write() except that this
4919 * function performs flush after writing to the register.
4925 * 0 on success, negative errno on failure.
4927 int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
4929 if (sata_scr_valid(ap)) {
4930 ap->ops->scr_write(ap, reg, val);
4931 ap->ops->scr_read(ap, reg);
4938 * ata_port_online - test whether the given port is online
4939 * @ap: ATA port to test
4941 * Test whether @ap is online. Note that this function returns 0
4942 * if online status of @ap cannot be obtained, so
4943 * ata_port_online(ap) != !ata_port_offline(ap).
4949 * 1 if the port online status is available and online.
4951 int ata_port_online(struct ata_port *ap)
4955 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
4961 * ata_port_offline - test whether the given port is offline
4962 * @ap: ATA port to test
4964 * Test whether @ap is offline. Note that this function returns
4965 * 0 if offline status of @ap cannot be obtained, so
4966 * ata_port_online(ap) != !ata_port_offline(ap).
4972 * 1 if the port offline status is available and offline.
4974 int ata_port_offline(struct ata_port *ap)
4978 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
4983 int ata_flush_cache(struct ata_device *dev)
4985 unsigned int err_mask;
4988 if (!ata_try_flush_cache(dev))
4991 if (ata_id_has_flush_ext(dev->id))
4992 cmd = ATA_CMD_FLUSH_EXT;
4994 cmd = ATA_CMD_FLUSH;
4996 err_mask = ata_do_simple_cmd(dev, cmd);
4998 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
5005 static int ata_standby_drive(struct ata_device *dev)
5007 unsigned int err_mask;
5009 err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
5011 ata_dev_printk(dev, KERN_ERR, "failed to standby drive "
5012 "(err_mask=0x%x)\n", err_mask);
5019 static int ata_start_drive(struct ata_device *dev)
5021 unsigned int err_mask;
5023 err_mask = ata_do_simple_cmd(dev, ATA_CMD_IDLEIMMEDIATE);
5025 ata_dev_printk(dev, KERN_ERR, "failed to start drive "
5026 "(err_mask=0x%x)\n", err_mask);
5034 * ata_device_resume - wakeup a previously suspended devices
5035 * @dev: the device to resume
5037 * Kick the drive back into action, by sending it an idle immediate
5038 * command and making sure its transfer mode matches between drive
5042 int ata_device_resume(struct ata_device *dev)
5044 struct ata_port *ap = dev->ap;
5046 if (ap->flags & ATA_FLAG_SUSPENDED) {
5047 struct ata_device *failed_dev;
5049 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
5050 ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 200000);
5052 ap->flags &= ~ATA_FLAG_SUSPENDED;
5053 while (ata_set_mode(ap, &failed_dev))
5054 ata_dev_disable(failed_dev);
5056 if (!ata_dev_enabled(dev))
5058 if (dev->class == ATA_DEV_ATA)
5059 ata_start_drive(dev);
5065 * ata_device_suspend - prepare a device for suspend
5066 * @dev: the device to suspend
5067 * @state: target power management state
5069 * Flush the cache on the drive, if appropriate, then issue a
5070 * standbynow command.
5072 int ata_device_suspend(struct ata_device *dev, pm_message_t state)
5074 struct ata_port *ap = dev->ap;
5076 if (!ata_dev_enabled(dev))
5078 if (dev->class == ATA_DEV_ATA)
5079 ata_flush_cache(dev);
5081 if (state.event != PM_EVENT_FREEZE)
5082 ata_standby_drive(dev);
5083 ap->flags |= ATA_FLAG_SUSPENDED;
5088 * ata_port_start - Set port up for dma.
5089 * @ap: Port to initialize
5091 * Called just after data structures for each port are
5092 * initialized. Allocates space for PRD table.
5094 * May be used as the port_start() entry in ata_port_operations.
5097 * Inherited from caller.
5100 int ata_port_start (struct ata_port *ap)
5102 struct device *dev = ap->dev;
5105 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
5109 rc = ata_pad_alloc(ap, dev);
5111 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
5115 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
5122 * ata_port_stop - Undo ata_port_start()
5123 * @ap: Port to shut down
5125 * Frees the PRD table.
5127 * May be used as the port_stop() entry in ata_port_operations.
5130 * Inherited from caller.
5133 void ata_port_stop (struct ata_port *ap)
5135 struct device *dev = ap->dev;
5137 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
5138 ata_pad_free(ap, dev);
5141 void ata_host_stop (struct ata_host_set *host_set)
5143 if (host_set->mmio_base)
5144 iounmap(host_set->mmio_base);
5149 * ata_host_remove - Unregister SCSI host structure with upper layers
5150 * @ap: Port to unregister
5151 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
5154 * Inherited from caller.
5157 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
5159 struct Scsi_Host *sh = ap->host;
5164 scsi_remove_host(sh);
5166 ap->ops->port_stop(ap);
5170 * ata_dev_init - Initialize an ata_device structure
5171 * @dev: Device structure to initialize
5173 * Initialize @dev in preparation for probing.
5176 * Inherited from caller.
5178 void ata_dev_init(struct ata_device *dev)
5180 struct ata_port *ap = dev->ap;
5181 unsigned long flags;
5183 /* SATA spd limit is bound to the first device */
5184 ap->sata_spd_limit = ap->hw_sata_spd_limit;
5186 /* High bits of dev->flags are used to record warm plug
5187 * requests which occur asynchronously. Synchronize using
5190 spin_lock_irqsave(ap->lock, flags);
5191 dev->flags &= ~ATA_DFLAG_INIT_MASK;
5192 spin_unlock_irqrestore(ap->lock, flags);
5194 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
5195 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
5196 dev->pio_mask = UINT_MAX;
5197 dev->mwdma_mask = UINT_MAX;
5198 dev->udma_mask = UINT_MAX;
5202 * ata_host_init - Initialize an ata_port structure
5203 * @ap: Structure to initialize
5204 * @host: associated SCSI mid-layer structure
5205 * @host_set: Collection of hosts to which @ap belongs
5206 * @ent: Probe information provided by low-level driver
5207 * @port_no: Port number associated with this ata_port
5209 * Initialize a new ata_port structure, and its associated
5213 * Inherited from caller.
5215 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
5216 struct ata_host_set *host_set,
5217 const struct ata_probe_ent *ent, unsigned int port_no)
5223 host->max_channel = 1;
5224 host->unique_id = ata_unique_id++;
5225 host->max_cmd_len = 12;
5227 ap->lock = &host_set->lock;
5228 ap->flags = ATA_FLAG_DISABLED;
5229 ap->id = host->unique_id;
5231 ap->ctl = ATA_DEVCTL_OBS;
5232 ap->host_set = host_set;
5234 ap->port_no = port_no;
5236 ent->legacy_mode ? ent->hard_port_no : port_no;
5237 ap->pio_mask = ent->pio_mask;
5238 ap->mwdma_mask = ent->mwdma_mask;
5239 ap->udma_mask = ent->udma_mask;
5240 ap->flags |= ent->host_flags;
5241 ap->ops = ent->port_ops;
5242 ap->hw_sata_spd_limit = UINT_MAX;
5243 ap->active_tag = ATA_TAG_POISON;
5244 ap->last_ctl = 0xFF;
5246 #if defined(ATA_VERBOSE_DEBUG)
5247 /* turn on all debugging levels */
5248 ap->msg_enable = 0x00FF;
5249 #elif defined(ATA_DEBUG)
5250 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5252 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5255 INIT_WORK(&ap->port_task, NULL, NULL);
5256 INIT_WORK(&ap->hotplug_task, ata_scsi_hotplug, ap);
5257 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan, ap);
5258 INIT_LIST_HEAD(&ap->eh_done_q);
5259 init_waitqueue_head(&ap->eh_wait_q);
5261 /* set cable type */
5262 ap->cbl = ATA_CBL_NONE;
5263 if (ap->flags & ATA_FLAG_SATA)
5264 ap->cbl = ATA_CBL_SATA;
5266 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5267 struct ata_device *dev = &ap->device[i];
5274 ap->stats.unhandled_irq = 1;
5275 ap->stats.idle_irq = 1;
5278 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
5282 * ata_host_add - Attach low-level ATA driver to system
5283 * @ent: Information provided by low-level driver
5284 * @host_set: Collections of ports to which we add
5285 * @port_no: Port number associated with this host
5287 * Attach low-level ATA driver to system.
5290 * PCI/etc. bus probe sem.
5293 * New ata_port on success, for NULL on error.
5296 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
5297 struct ata_host_set *host_set,
5298 unsigned int port_no)
5300 struct Scsi_Host *host;
5301 struct ata_port *ap;
5306 if (!ent->port_ops->error_handler &&
5307 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
5308 printk(KERN_ERR "ata%u: no reset mechanism available\n",
5313 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
5317 host->transportt = &ata_scsi_transport_template;
5319 ap = ata_shost_to_port(host);
5321 ata_host_init(ap, host, host_set, ent, port_no);
5323 rc = ap->ops->port_start(ap);
5330 scsi_host_put(host);
5335 * ata_device_add - Register hardware device with ATA and SCSI layers
5336 * @ent: Probe information describing hardware device to be registered
5338 * This function processes the information provided in the probe
5339 * information struct @ent, allocates the necessary ATA and SCSI
5340 * host information structures, initializes them, and registers
5341 * everything with requisite kernel subsystems.
5343 * This function requests irqs, probes the ATA bus, and probes
5347 * PCI/etc. bus probe sem.
5350 * Number of ports registered. Zero on error (no ports registered).
5352 int ata_device_add(const struct ata_probe_ent *ent)
5354 unsigned int count = 0, i;
5355 struct device *dev = ent->dev;
5356 struct ata_host_set *host_set;
5360 /* alloc a container for our list of ATA ports (buses) */
5361 host_set = kzalloc(sizeof(struct ata_host_set) +
5362 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
5365 spin_lock_init(&host_set->lock);
5367 host_set->dev = dev;
5368 host_set->n_ports = ent->n_ports;
5369 host_set->irq = ent->irq;
5370 host_set->mmio_base = ent->mmio_base;
5371 host_set->private_data = ent->private_data;
5372 host_set->ops = ent->port_ops;
5373 host_set->flags = ent->host_set_flags;
5375 /* register each port bound to this device */
5376 for (i = 0; i < ent->n_ports; i++) {
5377 struct ata_port *ap;
5378 unsigned long xfer_mode_mask;
5380 ap = ata_host_add(ent, host_set, i);
5384 host_set->ports[i] = ap;
5385 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
5386 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
5387 (ap->pio_mask << ATA_SHIFT_PIO);
5389 /* print per-port info to dmesg */
5390 ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%lX "
5391 "ctl 0x%lX bmdma 0x%lX irq %lu\n",
5392 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
5393 ata_mode_string(xfer_mode_mask),
5394 ap->ioaddr.cmd_addr,
5395 ap->ioaddr.ctl_addr,
5396 ap->ioaddr.bmdma_addr,
5400 host_set->ops->irq_clear(ap);
5401 ata_eh_freeze_port(ap); /* freeze port before requesting IRQ */
5408 /* obtain irq, that is shared between channels */
5409 rc = request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
5410 DRV_NAME, host_set);
5412 dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
5417 /* perform each probe synchronously */
5418 DPRINTK("probe begin\n");
5419 for (i = 0; i < count; i++) {
5420 struct ata_port *ap;
5424 ap = host_set->ports[i];
5426 /* init sata_spd_limit to the current value */
5427 if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
5428 int spd = (scontrol >> 4) & 0xf;
5429 ap->hw_sata_spd_limit &= (1 << spd) - 1;
5431 ap->sata_spd_limit = ap->hw_sata_spd_limit;
5433 rc = scsi_add_host(ap->host, dev);
5435 ata_port_printk(ap, KERN_ERR, "scsi_add_host failed\n");
5436 /* FIXME: do something useful here */
5437 /* FIXME: handle unconditional calls to
5438 * scsi_scan_host and ata_host_remove, below,
5443 if (ap->ops->error_handler) {
5444 unsigned long flags;
5448 /* kick EH for boot probing */
5449 spin_lock_irqsave(ap->lock, flags);
5451 ap->eh_info.probe_mask = (1 << ATA_MAX_DEVICES) - 1;
5452 ap->eh_info.action |= ATA_EH_SOFTRESET;
5454 ap->flags |= ATA_FLAG_LOADING;
5455 ata_port_schedule_eh(ap);
5457 spin_unlock_irqrestore(ap->lock, flags);
5459 /* wait for EH to finish */
5460 ata_port_wait_eh(ap);
5462 DPRINTK("ata%u: bus probe begin\n", ap->id);
5463 rc = ata_bus_probe(ap);
5464 DPRINTK("ata%u: bus probe end\n", ap->id);
5467 /* FIXME: do something useful here?
5468 * Current libata behavior will
5469 * tear down everything when
5470 * the module is removed
5471 * or the h/w is unplugged.
5477 /* probes are done, now scan each port's disk(s) */
5478 DPRINTK("host probe begin\n");
5479 for (i = 0; i < count; i++) {
5480 struct ata_port *ap = host_set->ports[i];
5482 ata_scsi_scan_host(ap);
5485 dev_set_drvdata(dev, host_set);
5487 VPRINTK("EXIT, returning %u\n", ent->n_ports);
5488 return ent->n_ports; /* success */
5491 for (i = 0; i < count; i++) {
5492 ata_host_remove(host_set->ports[i], 1);
5493 scsi_host_put(host_set->ports[i]->host);
5497 VPRINTK("EXIT, returning 0\n");
5502 * ata_port_detach - Detach ATA port in prepration of device removal
5503 * @ap: ATA port to be detached
5505 * Detach all ATA devices and the associated SCSI devices of @ap;
5506 * then, remove the associated SCSI host. @ap is guaranteed to
5507 * be quiescent on return from this function.
5510 * Kernel thread context (may sleep).
5512 void ata_port_detach(struct ata_port *ap)
5514 unsigned long flags;
5517 if (!ap->ops->error_handler)
5520 /* tell EH we're leaving & flush EH */
5521 spin_lock_irqsave(ap->lock, flags);
5522 ap->flags |= ATA_FLAG_UNLOADING;
5523 spin_unlock_irqrestore(ap->lock, flags);
5525 ata_port_wait_eh(ap);
5527 /* EH is now guaranteed to see UNLOADING, so no new device
5528 * will be attached. Disable all existing devices.
5530 spin_lock_irqsave(ap->lock, flags);
5532 for (i = 0; i < ATA_MAX_DEVICES; i++)
5533 ata_dev_disable(&ap->device[i]);
5535 spin_unlock_irqrestore(ap->lock, flags);
5537 /* Final freeze & EH. All in-flight commands are aborted. EH
5538 * will be skipped and retrials will be terminated with bad
5541 spin_lock_irqsave(ap->lock, flags);
5542 ata_port_freeze(ap); /* won't be thawed */
5543 spin_unlock_irqrestore(ap->lock, flags);
5545 ata_port_wait_eh(ap);
5547 /* Flush hotplug task. The sequence is similar to
5548 * ata_port_flush_task().
5550 flush_workqueue(ata_aux_wq);
5551 cancel_delayed_work(&ap->hotplug_task);
5552 flush_workqueue(ata_aux_wq);
5554 /* remove the associated SCSI host */
5555 scsi_remove_host(ap->host);
5559 * ata_host_set_remove - PCI layer callback for device removal
5560 * @host_set: ATA host set that was removed
5562 * Unregister all objects associated with this host set. Free those
5566 * Inherited from calling layer (may sleep).
5569 void ata_host_set_remove(struct ata_host_set *host_set)
5573 for (i = 0; i < host_set->n_ports; i++)
5574 ata_port_detach(host_set->ports[i]);
5576 free_irq(host_set->irq, host_set);
5578 for (i = 0; i < host_set->n_ports; i++) {
5579 struct ata_port *ap = host_set->ports[i];
5581 ata_scsi_release(ap->host);
5583 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5584 struct ata_ioports *ioaddr = &ap->ioaddr;
5586 if (ioaddr->cmd_addr == 0x1f0)
5587 release_region(0x1f0, 8);
5588 else if (ioaddr->cmd_addr == 0x170)
5589 release_region(0x170, 8);
5592 scsi_host_put(ap->host);
5595 if (host_set->ops->host_stop)
5596 host_set->ops->host_stop(host_set);
5602 * ata_scsi_release - SCSI layer callback hook for host unload
5603 * @host: libata host to be unloaded
5605 * Performs all duties necessary to shut down a libata port...
5606 * Kill port kthread, disable port, and release resources.
5609 * Inherited from SCSI layer.
5615 int ata_scsi_release(struct Scsi_Host *host)
5617 struct ata_port *ap = ata_shost_to_port(host);
5621 ap->ops->port_disable(ap);
5622 ata_host_remove(ap, 0);
5629 * ata_std_ports - initialize ioaddr with standard port offsets.
5630 * @ioaddr: IO address structure to be initialized
5632 * Utility function which initializes data_addr, error_addr,
5633 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5634 * device_addr, status_addr, and command_addr to standard offsets
5635 * relative to cmd_addr.
5637 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
5640 void ata_std_ports(struct ata_ioports *ioaddr)
5642 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5643 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5644 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5645 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5646 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5647 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5648 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5649 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5650 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5651 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5657 void ata_pci_host_stop (struct ata_host_set *host_set)
5659 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5661 pci_iounmap(pdev, host_set->mmio_base);
5665 * ata_pci_remove_one - PCI layer callback for device removal
5666 * @pdev: PCI device that was removed
5668 * PCI layer indicates to libata via this hook that
5669 * hot-unplug or module unload event has occurred.
5670 * Handle this by unregistering all objects associated
5671 * with this PCI device. Free those objects. Then finally
5672 * release PCI resources and disable device.
5675 * Inherited from PCI layer (may sleep).
5678 void ata_pci_remove_one (struct pci_dev *pdev)
5680 struct device *dev = pci_dev_to_dev(pdev);
5681 struct ata_host_set *host_set = dev_get_drvdata(dev);
5682 struct ata_host_set *host_set2 = host_set->next;
5684 ata_host_set_remove(host_set);
5686 ata_host_set_remove(host_set2);
5688 pci_release_regions(pdev);
5689 pci_disable_device(pdev);
5690 dev_set_drvdata(dev, NULL);
5693 /* move to PCI subsystem */
5694 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5696 unsigned long tmp = 0;
5698 switch (bits->width) {
5701 pci_read_config_byte(pdev, bits->reg, &tmp8);
5707 pci_read_config_word(pdev, bits->reg, &tmp16);
5713 pci_read_config_dword(pdev, bits->reg, &tmp32);
5724 return (tmp == bits->val) ? 1 : 0;
5727 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5729 pci_save_state(pdev);
5730 pci_disable_device(pdev);
5731 pci_set_power_state(pdev, PCI_D3hot);
5735 int ata_pci_device_resume(struct pci_dev *pdev)
5737 pci_set_power_state(pdev, PCI_D0);
5738 pci_restore_state(pdev);
5739 pci_enable_device(pdev);
5740 pci_set_master(pdev);
5743 #endif /* CONFIG_PCI */
5746 static int __init ata_init(void)
5748 ata_probe_timeout *= HZ;
5749 ata_wq = create_workqueue("ata");
5753 ata_aux_wq = create_singlethread_workqueue("ata_aux");
5755 destroy_workqueue(ata_wq);
5759 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5763 static void __exit ata_exit(void)
5765 destroy_workqueue(ata_wq);
5766 destroy_workqueue(ata_aux_wq);
5769 module_init(ata_init);
5770 module_exit(ata_exit);
5772 static unsigned long ratelimit_time;
5773 static DEFINE_SPINLOCK(ata_ratelimit_lock);
5775 int ata_ratelimit(void)
5778 unsigned long flags;
5780 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5782 if (time_after(jiffies, ratelimit_time)) {
5784 ratelimit_time = jiffies + (HZ/5);
5788 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5794 * ata_wait_register - wait until register value changes
5795 * @reg: IO-mapped register
5796 * @mask: Mask to apply to read register value
5797 * @val: Wait condition
5798 * @interval_msec: polling interval in milliseconds
5799 * @timeout_msec: timeout in milliseconds
5801 * Waiting for some bits of register to change is a common
5802 * operation for ATA controllers. This function reads 32bit LE
5803 * IO-mapped register @reg and tests for the following condition.
5805 * (*@reg & mask) != val
5807 * If the condition is met, it returns; otherwise, the process is
5808 * repeated after @interval_msec until timeout.
5811 * Kernel thread context (may sleep)
5814 * The final register value.
5816 u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
5817 unsigned long interval_msec,
5818 unsigned long timeout_msec)
5820 unsigned long timeout;
5823 tmp = ioread32(reg);
5825 /* Calculate timeout _after_ the first read to make sure
5826 * preceding writes reach the controller before starting to
5827 * eat away the timeout.
5829 timeout = jiffies + (timeout_msec * HZ) / 1000;
5831 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
5832 msleep(interval_msec);
5833 tmp = ioread32(reg);
5840 * libata is essentially a library of internal helper functions for
5841 * low-level ATA host controller drivers. As such, the API/ABI is
5842 * likely to change as new drivers are added and updated.
5843 * Do not depend on ABI/API stability.
5846 EXPORT_SYMBOL_GPL(sata_deb_timing_boot);
5847 EXPORT_SYMBOL_GPL(sata_deb_timing_eh);
5848 EXPORT_SYMBOL_GPL(sata_deb_timing_before_fsrst);
5849 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5850 EXPORT_SYMBOL_GPL(ata_std_ports);
5851 EXPORT_SYMBOL_GPL(ata_device_add);
5852 EXPORT_SYMBOL_GPL(ata_port_detach);
5853 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5854 EXPORT_SYMBOL_GPL(ata_sg_init);
5855 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5856 EXPORT_SYMBOL_GPL(ata_hsm_move);
5857 EXPORT_SYMBOL_GPL(ata_qc_complete);
5858 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
5859 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5860 EXPORT_SYMBOL_GPL(ata_tf_load);
5861 EXPORT_SYMBOL_GPL(ata_tf_read);
5862 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5863 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5864 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5865 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5866 EXPORT_SYMBOL_GPL(ata_check_status);
5867 EXPORT_SYMBOL_GPL(ata_altstatus);
5868 EXPORT_SYMBOL_GPL(ata_exec_command);
5869 EXPORT_SYMBOL_GPL(ata_port_start);
5870 EXPORT_SYMBOL_GPL(ata_port_stop);
5871 EXPORT_SYMBOL_GPL(ata_host_stop);
5872 EXPORT_SYMBOL_GPL(ata_interrupt);
5873 EXPORT_SYMBOL_GPL(ata_mmio_data_xfer);
5874 EXPORT_SYMBOL_GPL(ata_pio_data_xfer);
5875 EXPORT_SYMBOL_GPL(ata_pio_data_xfer_noirq);
5876 EXPORT_SYMBOL_GPL(ata_qc_prep);
5877 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
5878 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5879 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5880 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5881 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5882 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5883 EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
5884 EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
5885 EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
5886 EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
5887 EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
5888 EXPORT_SYMBOL_GPL(ata_port_probe);
5889 EXPORT_SYMBOL_GPL(sata_set_spd);
5890 EXPORT_SYMBOL_GPL(sata_phy_debounce);
5891 EXPORT_SYMBOL_GPL(sata_phy_resume);
5892 EXPORT_SYMBOL_GPL(sata_phy_reset);
5893 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5894 EXPORT_SYMBOL_GPL(ata_bus_reset);
5895 EXPORT_SYMBOL_GPL(ata_std_prereset);
5896 EXPORT_SYMBOL_GPL(ata_std_softreset);
5897 EXPORT_SYMBOL_GPL(sata_std_hardreset);
5898 EXPORT_SYMBOL_GPL(ata_std_postreset);
5899 EXPORT_SYMBOL_GPL(ata_dev_revalidate);
5900 EXPORT_SYMBOL_GPL(ata_dev_classify);
5901 EXPORT_SYMBOL_GPL(ata_dev_pair);
5902 EXPORT_SYMBOL_GPL(ata_port_disable);
5903 EXPORT_SYMBOL_GPL(ata_ratelimit);
5904 EXPORT_SYMBOL_GPL(ata_wait_register);
5905 EXPORT_SYMBOL_GPL(ata_busy_sleep);
5906 EXPORT_SYMBOL_GPL(ata_port_queue_task);
5907 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5908 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5909 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5910 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
5911 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
5912 EXPORT_SYMBOL_GPL(ata_scsi_release);
5913 EXPORT_SYMBOL_GPL(ata_host_intr);
5914 EXPORT_SYMBOL_GPL(sata_scr_valid);
5915 EXPORT_SYMBOL_GPL(sata_scr_read);
5916 EXPORT_SYMBOL_GPL(sata_scr_write);
5917 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
5918 EXPORT_SYMBOL_GPL(ata_port_online);
5919 EXPORT_SYMBOL_GPL(ata_port_offline);
5920 EXPORT_SYMBOL_GPL(ata_id_string);
5921 EXPORT_SYMBOL_GPL(ata_id_c_string);
5922 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5924 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
5925 EXPORT_SYMBOL_GPL(ata_timing_compute);
5926 EXPORT_SYMBOL_GPL(ata_timing_merge);
5929 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5930 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5931 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5932 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5933 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5934 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5935 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
5936 EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5937 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
5938 #endif /* CONFIG_PCI */
5940 EXPORT_SYMBOL_GPL(ata_device_suspend);
5941 EXPORT_SYMBOL_GPL(ata_device_resume);
5942 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5943 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
5945 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5946 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
5947 EXPORT_SYMBOL_GPL(ata_port_abort);
5948 EXPORT_SYMBOL_GPL(ata_port_freeze);
5949 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
5950 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
5951 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5952 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
5953 EXPORT_SYMBOL_GPL(ata_do_eh);