2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev,
68 static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
69 struct ata_device *dev);
70 static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
72 static unsigned int ata_unique_id = 1;
73 static struct workqueue_struct *ata_wq;
75 int atapi_enabled = 1;
76 module_param(atapi_enabled, int, 0444);
77 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80 module_param_named(fua, libata_fua, int, 0444);
81 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
83 MODULE_AUTHOR("Jeff Garzik");
84 MODULE_DESCRIPTION("Library module for ATA devices");
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(DRV_VERSION);
90 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
91 * @tf: Taskfile to convert
92 * @fis: Buffer into which data will output
93 * @pmp: Port multiplier port
95 * Converts a standard ATA taskfile to a Serial ATA
96 * FIS structure (Register - Host to Device).
99 * Inherited from caller.
102 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
104 fis[0] = 0x27; /* Register - Host to Device FIS */
105 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
106 bit 7 indicates Command FIS */
107 fis[2] = tf->command;
108 fis[3] = tf->feature;
115 fis[8] = tf->hob_lbal;
116 fis[9] = tf->hob_lbam;
117 fis[10] = tf->hob_lbah;
118 fis[11] = tf->hob_feature;
121 fis[13] = tf->hob_nsect;
132 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
133 * @fis: Buffer from which data will be input
134 * @tf: Taskfile to output
136 * Converts a serial ATA FIS structure to a standard ATA taskfile.
139 * Inherited from caller.
142 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
144 tf->command = fis[2]; /* status */
145 tf->feature = fis[3]; /* error */
152 tf->hob_lbal = fis[8];
153 tf->hob_lbam = fis[9];
154 tf->hob_lbah = fis[10];
157 tf->hob_nsect = fis[13];
160 static const u8 ata_rw_cmds[] = {
164 ATA_CMD_READ_MULTI_EXT,
165 ATA_CMD_WRITE_MULTI_EXT,
169 ATA_CMD_WRITE_MULTI_FUA_EXT,
173 ATA_CMD_PIO_READ_EXT,
174 ATA_CMD_PIO_WRITE_EXT,
187 ATA_CMD_WRITE_FUA_EXT
191 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
192 * @qc: command to examine and configure
194 * Examine the device configuration and tf->flags to calculate
195 * the proper read/write commands and protocol to use.
200 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
202 struct ata_taskfile *tf = &qc->tf;
203 struct ata_device *dev = qc->dev;
206 int index, fua, lba48, write;
208 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
209 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
210 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
212 if (dev->flags & ATA_DFLAG_PIO) {
213 tf->protocol = ATA_PROT_PIO;
214 index = dev->multi_count ? 0 : 8;
215 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
216 /* Unable to use DMA due to host limitation */
217 tf->protocol = ATA_PROT_PIO;
218 index = dev->multi_count ? 0 : 8;
220 tf->protocol = ATA_PROT_DMA;
224 cmd = ata_rw_cmds[index + fua + lba48 + write];
233 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
234 * @pio_mask: pio_mask
235 * @mwdma_mask: mwdma_mask
236 * @udma_mask: udma_mask
238 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
239 * unsigned int xfer_mask.
247 static unsigned int ata_pack_xfermask(unsigned int pio_mask,
248 unsigned int mwdma_mask,
249 unsigned int udma_mask)
251 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
252 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
253 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
257 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
258 * @xfer_mask: xfer_mask to unpack
259 * @pio_mask: resulting pio_mask
260 * @mwdma_mask: resulting mwdma_mask
261 * @udma_mask: resulting udma_mask
263 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
264 * Any NULL distination masks will be ignored.
266 static void ata_unpack_xfermask(unsigned int xfer_mask,
267 unsigned int *pio_mask,
268 unsigned int *mwdma_mask,
269 unsigned int *udma_mask)
272 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
274 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
276 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
279 static const struct ata_xfer_ent {
283 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
284 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
285 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
290 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
291 * @xfer_mask: xfer_mask of interest
293 * Return matching XFER_* value for @xfer_mask. Only the highest
294 * bit of @xfer_mask is considered.
300 * Matching XFER_* value, 0 if no match found.
302 static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
304 int highbit = fls(xfer_mask) - 1;
305 const struct ata_xfer_ent *ent;
307 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
308 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
309 return ent->base + highbit - ent->shift;
314 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
315 * @xfer_mode: XFER_* of interest
317 * Return matching xfer_mask for @xfer_mode.
323 * Matching xfer_mask, 0 if no match found.
325 static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
327 const struct ata_xfer_ent *ent;
329 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
330 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
331 return 1 << (ent->shift + xfer_mode - ent->base);
336 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
337 * @xfer_mode: XFER_* of interest
339 * Return matching xfer_shift for @xfer_mode.
345 * Matching xfer_shift, -1 if no match found.
347 static int ata_xfer_mode2shift(unsigned int xfer_mode)
349 const struct ata_xfer_ent *ent;
351 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
352 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
358 * ata_mode_string - convert xfer_mask to string
359 * @xfer_mask: mask of bits supported; only highest bit counts.
361 * Determine string which represents the highest speed
362 * (highest bit in @modemask).
368 * Constant C string representing highest speed listed in
369 * @mode_mask, or the constant C string "<n/a>".
371 static const char *ata_mode_string(unsigned int xfer_mask)
373 static const char * const xfer_mode_str[] = {
393 highbit = fls(xfer_mask) - 1;
394 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
395 return xfer_mode_str[highbit];
399 static const char *sata_spd_string(unsigned int spd)
401 static const char * const spd_str[] = {
406 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
408 return spd_str[spd - 1];
411 void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
413 if (ata_dev_enabled(dev)) {
414 printk(KERN_WARNING "ata%u: dev %u disabled\n",
421 * ata_pio_devchk - PATA device presence detection
422 * @ap: ATA channel to examine
423 * @device: Device to examine (starting at zero)
425 * This technique was originally described in
426 * Hale Landis's ATADRVR (www.ata-atapi.com), and
427 * later found its way into the ATA/ATAPI spec.
429 * Write a pattern to the ATA shadow registers,
430 * and if a device is present, it will respond by
431 * correctly storing and echoing back the
432 * ATA shadow register contents.
438 static unsigned int ata_pio_devchk(struct ata_port *ap,
441 struct ata_ioports *ioaddr = &ap->ioaddr;
444 ap->ops->dev_select(ap, device);
446 outb(0x55, ioaddr->nsect_addr);
447 outb(0xaa, ioaddr->lbal_addr);
449 outb(0xaa, ioaddr->nsect_addr);
450 outb(0x55, ioaddr->lbal_addr);
452 outb(0x55, ioaddr->nsect_addr);
453 outb(0xaa, ioaddr->lbal_addr);
455 nsect = inb(ioaddr->nsect_addr);
456 lbal = inb(ioaddr->lbal_addr);
458 if ((nsect == 0x55) && (lbal == 0xaa))
459 return 1; /* we found a device */
461 return 0; /* nothing found */
465 * ata_mmio_devchk - PATA device presence detection
466 * @ap: ATA channel to examine
467 * @device: Device to examine (starting at zero)
469 * This technique was originally described in
470 * Hale Landis's ATADRVR (www.ata-atapi.com), and
471 * later found its way into the ATA/ATAPI spec.
473 * Write a pattern to the ATA shadow registers,
474 * and if a device is present, it will respond by
475 * correctly storing and echoing back the
476 * ATA shadow register contents.
482 static unsigned int ata_mmio_devchk(struct ata_port *ap,
485 struct ata_ioports *ioaddr = &ap->ioaddr;
488 ap->ops->dev_select(ap, device);
490 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
491 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
493 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
494 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
496 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
497 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
499 nsect = readb((void __iomem *) ioaddr->nsect_addr);
500 lbal = readb((void __iomem *) ioaddr->lbal_addr);
502 if ((nsect == 0x55) && (lbal == 0xaa))
503 return 1; /* we found a device */
505 return 0; /* nothing found */
509 * ata_devchk - PATA device presence detection
510 * @ap: ATA channel to examine
511 * @device: Device to examine (starting at zero)
513 * Dispatch ATA device presence detection, depending
514 * on whether we are using PIO or MMIO to talk to the
515 * ATA shadow registers.
521 static unsigned int ata_devchk(struct ata_port *ap,
524 if (ap->flags & ATA_FLAG_MMIO)
525 return ata_mmio_devchk(ap, device);
526 return ata_pio_devchk(ap, device);
530 * ata_dev_classify - determine device type based on ATA-spec signature
531 * @tf: ATA taskfile register set for device to be identified
533 * Determine from taskfile register contents whether a device is
534 * ATA or ATAPI, as per "Signature and persistence" section
535 * of ATA/PI spec (volume 1, sect 5.14).
541 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
542 * the event of failure.
545 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
547 /* Apple's open source Darwin code hints that some devices only
548 * put a proper signature into the LBA mid/high registers,
549 * So, we only check those. It's sufficient for uniqueness.
552 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
553 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
554 DPRINTK("found ATA device by sig\n");
558 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
559 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
560 DPRINTK("found ATAPI device by sig\n");
561 return ATA_DEV_ATAPI;
564 DPRINTK("unknown device\n");
565 return ATA_DEV_UNKNOWN;
569 * ata_dev_try_classify - Parse returned ATA device signature
570 * @ap: ATA channel to examine
571 * @device: Device to examine (starting at zero)
572 * @r_err: Value of error register on completion
574 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
575 * an ATA/ATAPI-defined set of values is placed in the ATA
576 * shadow registers, indicating the results of device detection
579 * Select the ATA device, and read the values from the ATA shadow
580 * registers. Then parse according to the Error register value,
581 * and the spec-defined values examined by ata_dev_classify().
587 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
591 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
593 struct ata_taskfile tf;
597 ap->ops->dev_select(ap, device);
599 memset(&tf, 0, sizeof(tf));
601 ap->ops->tf_read(ap, &tf);
606 /* see if device passed diags */
609 else if ((device == 0) && (err == 0x81))
614 /* determine if device is ATA or ATAPI */
615 class = ata_dev_classify(&tf);
617 if (class == ATA_DEV_UNKNOWN)
619 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
625 * ata_id_string - Convert IDENTIFY DEVICE page into string
626 * @id: IDENTIFY DEVICE results we will examine
627 * @s: string into which data is output
628 * @ofs: offset into identify device page
629 * @len: length of string to return. must be an even number.
631 * The strings in the IDENTIFY DEVICE page are broken up into
632 * 16-bit chunks. Run through the string, and output each
633 * 8-bit chunk linearly, regardless of platform.
639 void ata_id_string(const u16 *id, unsigned char *s,
640 unsigned int ofs, unsigned int len)
659 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
660 * @id: IDENTIFY DEVICE results we will examine
661 * @s: string into which data is output
662 * @ofs: offset into identify device page
663 * @len: length of string to return. must be an odd number.
665 * This function is identical to ata_id_string except that it
666 * trims trailing spaces and terminates the resulting string with
667 * null. @len must be actual maximum length (even number) + 1.
672 void ata_id_c_string(const u16 *id, unsigned char *s,
673 unsigned int ofs, unsigned int len)
679 ata_id_string(id, s, ofs, len - 1);
681 p = s + strnlen(s, len - 1);
682 while (p > s && p[-1] == ' ')
687 static u64 ata_id_n_sectors(const u16 *id)
689 if (ata_id_has_lba(id)) {
690 if (ata_id_has_lba48(id))
691 return ata_id_u64(id, 100);
693 return ata_id_u32(id, 60);
695 if (ata_id_current_chs_valid(id))
696 return ata_id_u32(id, 57);
698 return id[1] * id[3] * id[6];
703 * ata_noop_dev_select - Select device 0/1 on ATA bus
704 * @ap: ATA channel to manipulate
705 * @device: ATA device (numbered from zero) to select
707 * This function performs no actual function.
709 * May be used as the dev_select() entry in ata_port_operations.
714 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
720 * ata_std_dev_select - Select device 0/1 on ATA bus
721 * @ap: ATA channel to manipulate
722 * @device: ATA device (numbered from zero) to select
724 * Use the method defined in the ATA specification to
725 * make either device 0, or device 1, active on the
726 * ATA channel. Works with both PIO and MMIO.
728 * May be used as the dev_select() entry in ata_port_operations.
734 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
739 tmp = ATA_DEVICE_OBS;
741 tmp = ATA_DEVICE_OBS | ATA_DEV1;
743 if (ap->flags & ATA_FLAG_MMIO) {
744 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
746 outb(tmp, ap->ioaddr.device_addr);
748 ata_pause(ap); /* needed; also flushes, for mmio */
752 * ata_dev_select - Select device 0/1 on ATA bus
753 * @ap: ATA channel to manipulate
754 * @device: ATA device (numbered from zero) to select
755 * @wait: non-zero to wait for Status register BSY bit to clear
756 * @can_sleep: non-zero if context allows sleeping
758 * Use the method defined in the ATA specification to
759 * make either device 0, or device 1, active on the
762 * This is a high-level version of ata_std_dev_select(),
763 * which additionally provides the services of inserting
764 * the proper pauses and status polling, where needed.
770 void ata_dev_select(struct ata_port *ap, unsigned int device,
771 unsigned int wait, unsigned int can_sleep)
773 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
774 ap->id, device, wait);
779 ap->ops->dev_select(ap, device);
782 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
789 * ata_dump_id - IDENTIFY DEVICE info debugging output
790 * @id: IDENTIFY DEVICE page to dump
792 * Dump selected 16-bit words from the given IDENTIFY DEVICE
799 static inline void ata_dump_id(const u16 *id)
801 DPRINTK("49==0x%04x "
811 DPRINTK("80==0x%04x "
821 DPRINTK("88==0x%04x "
828 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
829 * @id: IDENTIFY data to compute xfer mask from
831 * Compute the xfermask for this device. This is not as trivial
832 * as it seems if we must consider early devices correctly.
834 * FIXME: pre IDE drive timing (do we care ?).
842 static unsigned int ata_id_xfermask(const u16 *id)
844 unsigned int pio_mask, mwdma_mask, udma_mask;
846 /* Usual case. Word 53 indicates word 64 is valid */
847 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
848 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
852 /* If word 64 isn't valid then Word 51 high byte holds
853 * the PIO timing number for the maximum. Turn it into
856 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
858 /* But wait.. there's more. Design your standards by
859 * committee and you too can get a free iordy field to
860 * process. However its the speeds not the modes that
861 * are supported... Note drivers using the timing API
862 * will get this right anyway
866 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
869 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
870 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
872 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
876 * ata_port_queue_task - Queue port_task
877 * @ap: The ata_port to queue port_task for
879 * Schedule @fn(@data) for execution after @delay jiffies using
880 * port_task. There is one port_task per port and it's the
881 * user(low level driver)'s responsibility to make sure that only
882 * one task is active at any given time.
884 * libata core layer takes care of synchronization between
885 * port_task and EH. ata_port_queue_task() may be ignored for EH
889 * Inherited from caller.
891 void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
896 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
899 PREPARE_WORK(&ap->port_task, fn, data);
902 rc = queue_work(ata_wq, &ap->port_task);
904 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
906 /* rc == 0 means that another user is using port task */
911 * ata_port_flush_task - Flush port_task
912 * @ap: The ata_port to flush port_task for
914 * After this function completes, port_task is guranteed not to
915 * be running or scheduled.
918 * Kernel thread context (may sleep)
920 void ata_port_flush_task(struct ata_port *ap)
926 spin_lock_irqsave(&ap->host_set->lock, flags);
927 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
928 spin_unlock_irqrestore(&ap->host_set->lock, flags);
930 DPRINTK("flush #1\n");
931 flush_workqueue(ata_wq);
934 * At this point, if a task is running, it's guaranteed to see
935 * the FLUSH flag; thus, it will never queue pio tasks again.
938 if (!cancel_delayed_work(&ap->port_task)) {
939 DPRINTK("flush #2\n");
940 flush_workqueue(ata_wq);
943 spin_lock_irqsave(&ap->host_set->lock, flags);
944 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
945 spin_unlock_irqrestore(&ap->host_set->lock, flags);
950 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
952 struct completion *waiting = qc->private_data;
954 qc->ap->ops->tf_read(qc->ap, &qc->tf);
959 * ata_exec_internal - execute libata internal command
960 * @ap: Port to which the command is sent
961 * @dev: Device to which the command is sent
962 * @tf: Taskfile registers for the command and the result
963 * @cdb: CDB for packet command
964 * @dma_dir: Data tranfer direction of the command
965 * @buf: Data buffer of the command
966 * @buflen: Length of data buffer
968 * Executes libata internal command with timeout. @tf contains
969 * command on entry and result on return. Timeout and error
970 * conditions are reported via return value. No recovery action
971 * is taken after a command times out. It's caller's duty to
972 * clean up after timeout.
975 * None. Should be called with kernel context, might sleep.
978 unsigned ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
979 struct ata_taskfile *tf, const u8 *cdb,
980 int dma_dir, void *buf, unsigned int buflen)
982 u8 command = tf->command;
983 struct ata_queued_cmd *qc;
984 DECLARE_COMPLETION(wait);
986 unsigned int err_mask;
988 spin_lock_irqsave(&ap->host_set->lock, flags);
990 qc = ata_qc_new_init(ap, dev);
995 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
996 qc->dma_dir = dma_dir;
997 if (dma_dir != DMA_NONE) {
998 ata_sg_init_one(qc, buf, buflen);
999 qc->nsect = buflen / ATA_SECT_SIZE;
1002 qc->private_data = &wait;
1003 qc->complete_fn = ata_qc_complete_internal;
1007 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1009 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1010 ata_port_flush_task(ap);
1012 spin_lock_irqsave(&ap->host_set->lock, flags);
1014 /* We're racing with irq here. If we lose, the
1015 * following test prevents us from completing the qc
1016 * again. If completion irq occurs after here but
1017 * before the caller cleans up, it will result in a
1018 * spurious interrupt. We can live with that.
1020 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1021 qc->err_mask = AC_ERR_TIMEOUT;
1022 ata_qc_complete(qc);
1023 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1027 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1031 err_mask = qc->err_mask;
1035 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1036 * Until those drivers are fixed, we detect the condition
1037 * here, fail the command with AC_ERR_SYSTEM and reenable the
1040 * Note that this doesn't change any behavior as internal
1041 * command failure results in disabling the device in the
1042 * higher layer for LLDDs without new reset/EH callbacks.
1044 * Kill the following code as soon as those drivers are fixed.
1046 if (ap->flags & ATA_FLAG_DISABLED) {
1047 err_mask |= AC_ERR_SYSTEM;
1055 * ata_pio_need_iordy - check if iordy needed
1058 * Check if the current speed of the device requires IORDY. Used
1059 * by various controllers for chip configuration.
1062 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1065 int speed = adev->pio_mode - XFER_PIO_0;
1072 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1074 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1075 pio = adev->id[ATA_ID_EIDE_PIO];
1076 /* Is the speed faster than the drive allows non IORDY ? */
1078 /* This is cycle times not frequency - watch the logic! */
1079 if (pio > 240) /* PIO2 is 240nS per cycle */
1088 * ata_dev_read_id - Read ID data from the specified device
1089 * @ap: port on which target device resides
1090 * @dev: target device
1091 * @p_class: pointer to class of the target device (may be changed)
1092 * @post_reset: is this read ID post-reset?
1093 * @p_id: read IDENTIFY page (newly allocated)
1095 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1096 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1097 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1098 * for pre-ATA4 drives.
1101 * Kernel thread context (may sleep)
1104 * 0 on success, -errno otherwise.
1106 static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
1107 unsigned int *p_class, int post_reset, u16 **p_id)
1109 unsigned int class = *p_class;
1110 struct ata_taskfile tf;
1111 unsigned int err_mask = 0;
1116 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1118 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1120 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1123 reason = "out of memory";
1128 ata_tf_init(ap, &tf, dev->devno);
1132 tf.command = ATA_CMD_ID_ATA;
1135 tf.command = ATA_CMD_ID_ATAPI;
1139 reason = "unsupported class";
1143 tf.protocol = ATA_PROT_PIO;
1145 err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_FROM_DEVICE,
1146 id, sizeof(id[0]) * ATA_ID_WORDS);
1149 reason = "I/O error";
1153 swap_buf_le16(id, ATA_ID_WORDS);
1156 if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
1158 reason = "device reports illegal type";
1162 if (post_reset && class == ATA_DEV_ATA) {
1164 * The exact sequence expected by certain pre-ATA4 drives is:
1167 * INITIALIZE DEVICE PARAMETERS
1169 * Some drives were very specific about that exact sequence.
1171 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1172 err_mask = ata_dev_init_params(ap, dev, id[3], id[6]);
1175 reason = "INIT_DEV_PARAMS failed";
1179 /* current CHS translation info (id[53-58]) might be
1180 * changed. reread the identify device info.
1192 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1193 ap->id, dev->devno, reason);
1198 static inline u8 ata_dev_knobble(const struct ata_port *ap,
1199 struct ata_device *dev)
1201 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1205 * ata_dev_configure - Configure the specified ATA/ATAPI device
1206 * @ap: Port on which target device resides
1207 * @dev: Target device to configure
1208 * @print_info: Enable device info printout
1210 * Configure @dev according to @dev->id. Generic and low-level
1211 * driver specific fixups are also applied.
1214 * Kernel thread context (may sleep)
1217 * 0 on success, -errno otherwise
1219 static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1222 const u16 *id = dev->id;
1223 unsigned int xfer_mask;
1226 if (!ata_dev_enabled(dev)) {
1227 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1228 ap->id, dev->devno);
1232 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1234 /* print device capabilities */
1236 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1237 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1238 ap->id, dev->devno, id[49], id[82], id[83],
1239 id[84], id[85], id[86], id[87], id[88]);
1241 /* initialize to-be-configured parameters */
1242 dev->flags &= ~ATA_DFLAG_CFG_MASK;
1243 dev->max_sectors = 0;
1251 * common ATA, ATAPI feature tests
1254 /* find max transfer mode; for printk only */
1255 xfer_mask = ata_id_xfermask(id);
1259 /* ATA-specific feature tests */
1260 if (dev->class == ATA_DEV_ATA) {
1261 dev->n_sectors = ata_id_n_sectors(id);
1263 if (ata_id_has_lba(id)) {
1264 const char *lba_desc;
1267 dev->flags |= ATA_DFLAG_LBA;
1268 if (ata_id_has_lba48(id)) {
1269 dev->flags |= ATA_DFLAG_LBA48;
1273 /* print device info to dmesg */
1275 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1276 "max %s, %Lu sectors: %s\n",
1278 ata_id_major_version(id),
1279 ata_mode_string(xfer_mask),
1280 (unsigned long long)dev->n_sectors,
1285 /* Default translation */
1286 dev->cylinders = id[1];
1288 dev->sectors = id[6];
1290 if (ata_id_current_chs_valid(id)) {
1291 /* Current CHS translation is valid. */
1292 dev->cylinders = id[54];
1293 dev->heads = id[55];
1294 dev->sectors = id[56];
1297 /* print device info to dmesg */
1299 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1300 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1302 ata_id_major_version(id),
1303 ata_mode_string(xfer_mask),
1304 (unsigned long long)dev->n_sectors,
1305 dev->cylinders, dev->heads, dev->sectors);
1311 /* ATAPI-specific feature tests */
1312 else if (dev->class == ATA_DEV_ATAPI) {
1313 rc = atapi_cdb_len(id);
1314 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1315 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1319 dev->cdb_len = (unsigned int) rc;
1321 /* print device info to dmesg */
1323 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1324 ap->id, dev->devno, ata_mode_string(xfer_mask));
1327 ap->host->max_cmd_len = 0;
1328 for (i = 0; i < ATA_MAX_DEVICES; i++)
1329 ap->host->max_cmd_len = max_t(unsigned int,
1330 ap->host->max_cmd_len,
1331 ap->device[i].cdb_len);
1333 /* limit bridge transfers to udma5, 200 sectors */
1334 if (ata_dev_knobble(ap, dev)) {
1336 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1337 ap->id, dev->devno);
1338 dev->udma_mask &= ATA_UDMA5;
1339 dev->max_sectors = ATA_MAX_SECTORS;
1342 if (ap->ops->dev_config)
1343 ap->ops->dev_config(ap, dev);
1345 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1349 DPRINTK("EXIT, err\n");
1354 * ata_bus_probe - Reset and probe ATA bus
1357 * Master ATA bus probing function. Initiates a hardware-dependent
1358 * bus reset, then attempts to identify any devices found on
1362 * PCI/etc. bus probe sem.
1365 * Zero on success, negative errno otherwise.
1368 static int ata_bus_probe(struct ata_port *ap)
1370 unsigned int classes[ATA_MAX_DEVICES];
1371 int tries[ATA_MAX_DEVICES];
1372 int i, rc, down_xfermask;
1373 struct ata_device *dev;
1377 for (i = 0; i < ATA_MAX_DEVICES; i++)
1378 tries[i] = ATA_PROBE_MAX_TRIES;
1383 /* reset and determine device classes */
1384 for (i = 0; i < ATA_MAX_DEVICES; i++)
1385 classes[i] = ATA_DEV_UNKNOWN;
1387 if (ap->ops->probe_reset) {
1388 rc = ap->ops->probe_reset(ap, classes);
1390 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1394 ap->ops->phy_reset(ap);
1396 if (!(ap->flags & ATA_FLAG_DISABLED))
1397 for (i = 0; i < ATA_MAX_DEVICES; i++)
1398 classes[i] = ap->device[i].class;
1403 for (i = 0; i < ATA_MAX_DEVICES; i++)
1404 if (classes[i] == ATA_DEV_UNKNOWN)
1405 classes[i] = ATA_DEV_NONE;
1407 /* read IDENTIFY page and configure devices */
1408 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1409 dev = &ap->device[i];
1410 dev->class = classes[i];
1413 ata_down_xfermask_limit(ap, dev, 1);
1414 ata_dev_disable(ap, dev);
1417 if (!ata_dev_enabled(dev))
1422 rc = ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id);
1426 rc = ata_dev_configure(ap, dev, 1);
1431 /* configure transfer mode */
1432 if (ap->ops->set_mode) {
1433 /* FIXME: make ->set_mode handle no device case and
1434 * return error code and failing device on failure as
1435 * ata_set_mode() does.
1437 for (i = 0; i < ATA_MAX_DEVICES; i++)
1438 if (ata_dev_enabled(&ap->device[i])) {
1439 ap->ops->set_mode(ap);
1444 rc = ata_set_mode(ap, &dev);
1451 for (i = 0; i < ATA_MAX_DEVICES; i++)
1452 if (ata_dev_enabled(&ap->device[i]))
1455 /* no device present, disable port */
1456 ata_port_disable(ap);
1457 ap->ops->port_disable(ap);
1464 tries[dev->devno] = 0;
1467 ata_down_sata_spd_limit(ap);
1470 tries[dev->devno]--;
1471 if (down_xfermask &&
1472 ata_down_xfermask_limit(ap, dev, tries[dev->devno] == 1))
1473 tries[dev->devno] = 0;
1480 * ata_port_probe - Mark port as enabled
1481 * @ap: Port for which we indicate enablement
1483 * Modify @ap data structure such that the system
1484 * thinks that the entire port is enabled.
1486 * LOCKING: host_set lock, or some other form of
1490 void ata_port_probe(struct ata_port *ap)
1492 ap->flags &= ~ATA_FLAG_DISABLED;
1496 * sata_print_link_status - Print SATA link status
1497 * @ap: SATA port to printk link status about
1499 * This function prints link speed and status of a SATA link.
1504 static void sata_print_link_status(struct ata_port *ap)
1506 u32 sstatus, scontrol, tmp;
1508 if (!ap->ops->scr_read)
1511 sstatus = scr_read(ap, SCR_STATUS);
1512 scontrol = scr_read(ap, SCR_CONTROL);
1514 if (sata_dev_present(ap)) {
1515 tmp = (sstatus >> 4) & 0xf;
1517 "ata%u: SATA link up %s (SStatus %X SControl %X)\n",
1518 ap->id, sata_spd_string(tmp), sstatus, scontrol);
1521 "ata%u: SATA link down (SStatus %X SControl %X)\n",
1522 ap->id, sstatus, scontrol);
1527 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1528 * @ap: SATA port associated with target SATA PHY.
1530 * This function issues commands to standard SATA Sxxx
1531 * PHY registers, to wake up the phy (and device), and
1532 * clear any reset condition.
1535 * PCI/etc. bus probe sem.
1538 void __sata_phy_reset(struct ata_port *ap)
1541 unsigned long timeout = jiffies + (HZ * 5);
1543 if (ap->flags & ATA_FLAG_SATA_RESET) {
1544 /* issue phy wake/reset */
1545 scr_write_flush(ap, SCR_CONTROL, 0x301);
1546 /* Couldn't find anything in SATA I/II specs, but
1547 * AHCI-1.1 10.4.2 says at least 1 ms. */
1550 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1552 /* wait for phy to become ready, if necessary */
1555 sstatus = scr_read(ap, SCR_STATUS);
1556 if ((sstatus & 0xf) != 1)
1558 } while (time_before(jiffies, timeout));
1560 /* print link status */
1561 sata_print_link_status(ap);
1563 /* TODO: phy layer with polling, timeouts, etc. */
1564 if (sata_dev_present(ap))
1567 ata_port_disable(ap);
1569 if (ap->flags & ATA_FLAG_DISABLED)
1572 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1573 ata_port_disable(ap);
1577 ap->cbl = ATA_CBL_SATA;
1581 * sata_phy_reset - Reset SATA bus.
1582 * @ap: SATA port associated with target SATA PHY.
1584 * This function resets the SATA bus, and then probes
1585 * the bus for devices.
1588 * PCI/etc. bus probe sem.
1591 void sata_phy_reset(struct ata_port *ap)
1593 __sata_phy_reset(ap);
1594 if (ap->flags & ATA_FLAG_DISABLED)
1600 * ata_dev_pair - return other device on cable
1604 * Obtain the other device on the same cable, or if none is
1605 * present NULL is returned
1608 struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
1610 struct ata_device *pair = &ap->device[1 - adev->devno];
1611 if (!ata_dev_enabled(pair))
1617 * ata_port_disable - Disable port.
1618 * @ap: Port to be disabled.
1620 * Modify @ap data structure such that the system
1621 * thinks that the entire port is disabled, and should
1622 * never attempt to probe or communicate with devices
1625 * LOCKING: host_set lock, or some other form of
1629 void ata_port_disable(struct ata_port *ap)
1631 ap->device[0].class = ATA_DEV_NONE;
1632 ap->device[1].class = ATA_DEV_NONE;
1633 ap->flags |= ATA_FLAG_DISABLED;
1637 * ata_down_sata_spd_limit - adjust SATA spd limit downward
1638 * @ap: Port to adjust SATA spd limit for
1640 * Adjust SATA spd limit of @ap downward. Note that this
1641 * function only adjusts the limit. The change must be applied
1642 * using ata_set_sata_spd().
1645 * Inherited from caller.
1648 * 0 on success, negative errno on failure
1650 int ata_down_sata_spd_limit(struct ata_port *ap)
1655 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1658 mask = ap->sata_spd_limit;
1661 highbit = fls(mask) - 1;
1662 mask &= ~(1 << highbit);
1664 spd = (scr_read(ap, SCR_STATUS) >> 4) & 0xf;
1668 mask &= (1 << spd) - 1;
1672 ap->sata_spd_limit = mask;
1674 printk(KERN_WARNING "ata%u: limiting SATA link speed to %s\n",
1675 ap->id, sata_spd_string(fls(mask)));
1680 static int __ata_set_sata_spd_needed(struct ata_port *ap, u32 *scontrol)
1684 if (ap->sata_spd_limit == UINT_MAX)
1687 limit = fls(ap->sata_spd_limit);
1689 spd = (*scontrol >> 4) & 0xf;
1690 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1692 return spd != limit;
1696 * ata_set_sata_spd_needed - is SATA spd configuration needed
1697 * @ap: Port in question
1699 * Test whether the spd limit in SControl matches
1700 * @ap->sata_spd_limit. This function is used to determine
1701 * whether hardreset is necessary to apply SATA spd
1705 * Inherited from caller.
1708 * 1 if SATA spd configuration is needed, 0 otherwise.
1710 int ata_set_sata_spd_needed(struct ata_port *ap)
1714 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1717 scontrol = scr_read(ap, SCR_CONTROL);
1719 return __ata_set_sata_spd_needed(ap, &scontrol);
1723 * ata_set_sata_spd - set SATA spd according to spd limit
1724 * @ap: Port to set SATA spd for
1726 * Set SATA spd of @ap according to sata_spd_limit.
1729 * Inherited from caller.
1732 * 0 if spd doesn't need to be changed, 1 if spd has been
1733 * changed. -EOPNOTSUPP if SCR registers are inaccessible.
1735 static int ata_set_sata_spd(struct ata_port *ap)
1739 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1742 scontrol = scr_read(ap, SCR_CONTROL);
1743 if (!__ata_set_sata_spd_needed(ap, &scontrol))
1746 scr_write(ap, SCR_CONTROL, scontrol);
1751 * This mode timing computation functionality is ported over from
1752 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1755 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1756 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1757 * for PIO 5, which is a nonstandard extension and UDMA6, which
1758 * is currently supported only by Maxtor drives.
1761 static const struct ata_timing ata_timing[] = {
1763 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1764 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1765 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1766 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1768 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1769 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1770 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1772 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1774 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1775 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1776 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1778 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1779 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1780 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1782 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1783 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1784 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1786 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1787 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1788 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1790 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1795 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1796 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1798 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1800 q->setup = EZ(t->setup * 1000, T);
1801 q->act8b = EZ(t->act8b * 1000, T);
1802 q->rec8b = EZ(t->rec8b * 1000, T);
1803 q->cyc8b = EZ(t->cyc8b * 1000, T);
1804 q->active = EZ(t->active * 1000, T);
1805 q->recover = EZ(t->recover * 1000, T);
1806 q->cycle = EZ(t->cycle * 1000, T);
1807 q->udma = EZ(t->udma * 1000, UT);
1810 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1811 struct ata_timing *m, unsigned int what)
1813 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1814 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1815 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1816 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1817 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1818 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1819 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1820 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1823 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1825 const struct ata_timing *t;
1827 for (t = ata_timing; t->mode != speed; t++)
1828 if (t->mode == 0xFF)
1833 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1834 struct ata_timing *t, int T, int UT)
1836 const struct ata_timing *s;
1837 struct ata_timing p;
1843 if (!(s = ata_timing_find_mode(speed)))
1846 memcpy(t, s, sizeof(*s));
1849 * If the drive is an EIDE drive, it can tell us it needs extended
1850 * PIO/MW_DMA cycle timing.
1853 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1854 memset(&p, 0, sizeof(p));
1855 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1856 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1857 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1858 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1859 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1861 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1865 * Convert the timing to bus clock counts.
1868 ata_timing_quantize(t, t, T, UT);
1871 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1872 * S.M.A.R.T * and some other commands. We have to ensure that the
1873 * DMA cycle timing is slower/equal than the fastest PIO timing.
1876 if (speed > XFER_PIO_4) {
1877 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1878 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1882 * Lengthen active & recovery time so that cycle time is correct.
1885 if (t->act8b + t->rec8b < t->cyc8b) {
1886 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1887 t->rec8b = t->cyc8b - t->act8b;
1890 if (t->active + t->recover < t->cycle) {
1891 t->active += (t->cycle - (t->active + t->recover)) / 2;
1892 t->recover = t->cycle - t->active;
1899 * ata_down_xfermask_limit - adjust dev xfer masks downward
1900 * @ap: Port associated with device @dev
1901 * @dev: Device to adjust xfer masks
1902 * @force_pio0: Force PIO0
1904 * Adjust xfer masks of @dev downward. Note that this function
1905 * does not apply the change. Invoking ata_set_mode() afterwards
1906 * will apply the limit.
1909 * Inherited from caller.
1912 * 0 on success, negative errno on failure
1914 int ata_down_xfermask_limit(struct ata_port *ap, struct ata_device *dev,
1917 unsigned long xfer_mask;
1920 xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
1925 /* don't gear down to MWDMA from UDMA, go directly to PIO */
1926 if (xfer_mask & ATA_MASK_UDMA)
1927 xfer_mask &= ~ATA_MASK_MWDMA;
1929 highbit = fls(xfer_mask) - 1;
1930 xfer_mask &= ~(1 << highbit);
1932 xfer_mask &= 1 << ATA_SHIFT_PIO;
1936 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
1939 printk(KERN_WARNING "ata%u: dev %u limiting speed to %s\n",
1940 ap->id, dev->devno, ata_mode_string(xfer_mask));
1948 static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1950 unsigned int err_mask;
1953 dev->flags &= ~ATA_DFLAG_PIO;
1954 if (dev->xfer_shift == ATA_SHIFT_PIO)
1955 dev->flags |= ATA_DFLAG_PIO;
1957 err_mask = ata_dev_set_xfermode(ap, dev);
1960 "ata%u: failed to set xfermode (err_mask=0x%x)\n",
1965 rc = ata_dev_revalidate(ap, dev, 0);
1969 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1970 dev->xfer_shift, (int)dev->xfer_mode);
1972 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1974 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
1979 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1980 * @ap: port on which timings will be programmed
1981 * @r_failed_dev: out paramter for failed device
1983 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
1984 * ata_set_mode() fails, pointer to the failing device is
1985 * returned in @r_failed_dev.
1988 * PCI/etc. bus probe sem.
1991 * 0 on success, negative errno otherwise
1993 int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
1995 struct ata_device *dev;
1996 int i, rc = 0, used_dma = 0, found = 0;
1998 /* step 1: calculate xfer_mask */
1999 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2000 unsigned int pio_mask, dma_mask;
2002 dev = &ap->device[i];
2004 if (!ata_dev_enabled(dev))
2007 ata_dev_xfermask(ap, dev);
2009 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2010 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2011 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2012 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
2021 /* step 2: always set host PIO timings */
2022 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2023 dev = &ap->device[i];
2024 if (!ata_dev_enabled(dev))
2027 if (!dev->pio_mode) {
2028 printk(KERN_WARNING "ata%u: dev %u no PIO support\n",
2029 ap->id, dev->devno);
2034 dev->xfer_mode = dev->pio_mode;
2035 dev->xfer_shift = ATA_SHIFT_PIO;
2036 if (ap->ops->set_piomode)
2037 ap->ops->set_piomode(ap, dev);
2040 /* step 3: set host DMA timings */
2041 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2042 dev = &ap->device[i];
2044 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2047 dev->xfer_mode = dev->dma_mode;
2048 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2049 if (ap->ops->set_dmamode)
2050 ap->ops->set_dmamode(ap, dev);
2053 /* step 4: update devices' xfer mode */
2054 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2055 dev = &ap->device[i];
2057 if (!ata_dev_enabled(dev))
2060 rc = ata_dev_set_mode(ap, dev);
2065 /* Record simplex status. If we selected DMA then the other
2066 * host channels are not permitted to do so.
2068 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
2069 ap->host_set->simplex_claimed = 1;
2071 /* step5: chip specific finalisation */
2072 if (ap->ops->post_set_mode)
2073 ap->ops->post_set_mode(ap);
2077 *r_failed_dev = dev;
2082 * ata_tf_to_host - issue ATA taskfile to host controller
2083 * @ap: port to which command is being issued
2084 * @tf: ATA taskfile register set
2086 * Issues ATA taskfile register set to ATA host controller,
2087 * with proper synchronization with interrupt handler and
2091 * spin_lock_irqsave(host_set lock)
2094 static inline void ata_tf_to_host(struct ata_port *ap,
2095 const struct ata_taskfile *tf)
2097 ap->ops->tf_load(ap, tf);
2098 ap->ops->exec_command(ap, tf);
2102 * ata_busy_sleep - sleep until BSY clears, or timeout
2103 * @ap: port containing status register to be polled
2104 * @tmout_pat: impatience timeout
2105 * @tmout: overall timeout
2107 * Sleep until ATA Status register bit BSY clears,
2108 * or a timeout occurs.
2113 unsigned int ata_busy_sleep (struct ata_port *ap,
2114 unsigned long tmout_pat, unsigned long tmout)
2116 unsigned long timer_start, timeout;
2119 status = ata_busy_wait(ap, ATA_BUSY, 300);
2120 timer_start = jiffies;
2121 timeout = timer_start + tmout_pat;
2122 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2124 status = ata_busy_wait(ap, ATA_BUSY, 3);
2127 if (status & ATA_BUSY)
2128 printk(KERN_WARNING "ata%u is slow to respond, "
2129 "please be patient\n", ap->id);
2131 timeout = timer_start + tmout;
2132 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2134 status = ata_chk_status(ap);
2137 if (status & ATA_BUSY) {
2138 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2139 ap->id, tmout / HZ);
2146 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2148 struct ata_ioports *ioaddr = &ap->ioaddr;
2149 unsigned int dev0 = devmask & (1 << 0);
2150 unsigned int dev1 = devmask & (1 << 1);
2151 unsigned long timeout;
2153 /* if device 0 was found in ata_devchk, wait for its
2157 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2159 /* if device 1 was found in ata_devchk, wait for
2160 * register access, then wait for BSY to clear
2162 timeout = jiffies + ATA_TMOUT_BOOT;
2166 ap->ops->dev_select(ap, 1);
2167 if (ap->flags & ATA_FLAG_MMIO) {
2168 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2169 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2171 nsect = inb(ioaddr->nsect_addr);
2172 lbal = inb(ioaddr->lbal_addr);
2174 if ((nsect == 1) && (lbal == 1))
2176 if (time_after(jiffies, timeout)) {
2180 msleep(50); /* give drive a breather */
2183 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2185 /* is all this really necessary? */
2186 ap->ops->dev_select(ap, 0);
2188 ap->ops->dev_select(ap, 1);
2190 ap->ops->dev_select(ap, 0);
2193 static unsigned int ata_bus_softreset(struct ata_port *ap,
2194 unsigned int devmask)
2196 struct ata_ioports *ioaddr = &ap->ioaddr;
2198 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2200 /* software reset. causes dev0 to be selected */
2201 if (ap->flags & ATA_FLAG_MMIO) {
2202 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2203 udelay(20); /* FIXME: flush */
2204 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2205 udelay(20); /* FIXME: flush */
2206 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2208 outb(ap->ctl, ioaddr->ctl_addr);
2210 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2212 outb(ap->ctl, ioaddr->ctl_addr);
2215 /* spec mandates ">= 2ms" before checking status.
2216 * We wait 150ms, because that was the magic delay used for
2217 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2218 * between when the ATA command register is written, and then
2219 * status is checked. Because waiting for "a while" before
2220 * checking status is fine, post SRST, we perform this magic
2221 * delay here as well.
2223 * Old drivers/ide uses the 2mS rule and then waits for ready
2227 /* Before we perform post reset processing we want to see if
2228 * the bus shows 0xFF because the odd clown forgets the D7
2229 * pulldown resistor.
2231 if (ata_check_status(ap) == 0xFF)
2232 return AC_ERR_OTHER;
2234 ata_bus_post_reset(ap, devmask);
2240 * ata_bus_reset - reset host port and associated ATA channel
2241 * @ap: port to reset
2243 * This is typically the first time we actually start issuing
2244 * commands to the ATA channel. We wait for BSY to clear, then
2245 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2246 * result. Determine what devices, if any, are on the channel
2247 * by looking at the device 0/1 error register. Look at the signature
2248 * stored in each device's taskfile registers, to determine if
2249 * the device is ATA or ATAPI.
2252 * PCI/etc. bus probe sem.
2253 * Obtains host_set lock.
2256 * Sets ATA_FLAG_DISABLED if bus reset fails.
2259 void ata_bus_reset(struct ata_port *ap)
2261 struct ata_ioports *ioaddr = &ap->ioaddr;
2262 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2264 unsigned int dev0, dev1 = 0, devmask = 0;
2266 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2268 /* determine if device 0/1 are present */
2269 if (ap->flags & ATA_FLAG_SATA_RESET)
2272 dev0 = ata_devchk(ap, 0);
2274 dev1 = ata_devchk(ap, 1);
2278 devmask |= (1 << 0);
2280 devmask |= (1 << 1);
2282 /* select device 0 again */
2283 ap->ops->dev_select(ap, 0);
2285 /* issue bus reset */
2286 if (ap->flags & ATA_FLAG_SRST)
2287 if (ata_bus_softreset(ap, devmask))
2291 * determine by signature whether we have ATA or ATAPI devices
2293 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
2294 if ((slave_possible) && (err != 0x81))
2295 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
2297 /* re-enable interrupts */
2298 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2301 /* is double-select really necessary? */
2302 if (ap->device[1].class != ATA_DEV_NONE)
2303 ap->ops->dev_select(ap, 1);
2304 if (ap->device[0].class != ATA_DEV_NONE)
2305 ap->ops->dev_select(ap, 0);
2307 /* if no devices were detected, disable this port */
2308 if ((ap->device[0].class == ATA_DEV_NONE) &&
2309 (ap->device[1].class == ATA_DEV_NONE))
2312 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2313 /* set up device control for ATA_FLAG_SATA_RESET */
2314 if (ap->flags & ATA_FLAG_MMIO)
2315 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2317 outb(ap->ctl, ioaddr->ctl_addr);
2324 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2325 ap->ops->port_disable(ap);
2330 static int sata_phy_resume(struct ata_port *ap)
2332 unsigned long timeout = jiffies + (HZ * 5);
2333 u32 scontrol, sstatus;
2335 scontrol = scr_read(ap, SCR_CONTROL);
2336 scontrol = (scontrol & 0x0f0) | 0x300;
2337 scr_write_flush(ap, SCR_CONTROL, scontrol);
2339 /* Wait for phy to become ready, if necessary. */
2342 sstatus = scr_read(ap, SCR_STATUS);
2343 if ((sstatus & 0xf) != 1)
2345 } while (time_before(jiffies, timeout));
2351 * ata_std_probeinit - initialize probing
2352 * @ap: port to be probed
2354 * @ap is about to be probed. Initialize it. This function is
2355 * to be used as standard callback for ata_drive_probe_reset().
2357 * NOTE!!! Do not use this function as probeinit if a low level
2358 * driver implements only hardreset. Just pass NULL as probeinit
2359 * in that case. Using this function is probably okay but doing
2360 * so makes reset sequence different from the original
2361 * ->phy_reset implementation and Jeff nervous. :-P
2363 void ata_std_probeinit(struct ata_port *ap)
2365 if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
2368 sata_phy_resume(ap);
2370 spd = (scr_read(ap, SCR_CONTROL) & 0xf0) >> 4;
2372 ap->sata_spd_limit &= (1 << spd) - 1;
2374 if (sata_dev_present(ap))
2375 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2380 * ata_std_softreset - reset host port via ATA SRST
2381 * @ap: port to reset
2382 * @verbose: fail verbosely
2383 * @classes: resulting classes of attached devices
2385 * Reset host port using ATA SRST. This function is to be used
2386 * as standard callback for ata_drive_*_reset() functions.
2389 * Kernel thread context (may sleep)
2392 * 0 on success, -errno otherwise.
2394 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2396 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2397 unsigned int devmask = 0, err_mask;
2402 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2403 classes[0] = ATA_DEV_NONE;
2407 /* determine if device 0/1 are present */
2408 if (ata_devchk(ap, 0))
2409 devmask |= (1 << 0);
2410 if (slave_possible && ata_devchk(ap, 1))
2411 devmask |= (1 << 1);
2413 /* select device 0 again */
2414 ap->ops->dev_select(ap, 0);
2416 /* issue bus reset */
2417 DPRINTK("about to softreset, devmask=%x\n", devmask);
2418 err_mask = ata_bus_softreset(ap, devmask);
2421 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2424 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2429 /* determine by signature whether we have ATA or ATAPI devices */
2430 classes[0] = ata_dev_try_classify(ap, 0, &err);
2431 if (slave_possible && err != 0x81)
2432 classes[1] = ata_dev_try_classify(ap, 1, &err);
2435 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2440 * sata_std_hardreset - reset host port via SATA phy reset
2441 * @ap: port to reset
2442 * @verbose: fail verbosely
2443 * @class: resulting class of attached device
2445 * SATA phy-reset host port using DET bits of SControl register.
2446 * This function is to be used as standard callback for
2447 * ata_drive_*_reset().
2450 * Kernel thread context (may sleep)
2453 * 0 on success, -errno otherwise.
2455 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2461 if (ata_set_sata_spd_needed(ap)) {
2462 /* SATA spec says nothing about how to reconfigure
2463 * spd. To be on the safe side, turn off phy during
2464 * reconfiguration. This works for at least ICH7 AHCI
2467 scontrol = scr_read(ap, SCR_CONTROL);
2468 scontrol = (scontrol & 0x0f0) | 0x302;
2469 scr_write_flush(ap, SCR_CONTROL, scontrol);
2471 ata_set_sata_spd(ap);
2474 /* issue phy wake/reset */
2475 scontrol = scr_read(ap, SCR_CONTROL);
2476 scontrol = (scontrol & 0x0f0) | 0x301;
2477 scr_write_flush(ap, SCR_CONTROL, scontrol);
2479 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
2480 * 10.4.2 says at least 1 ms.
2484 /* bring phy back */
2485 sata_phy_resume(ap);
2487 /* TODO: phy layer with polling, timeouts, etc. */
2488 if (!sata_dev_present(ap)) {
2489 *class = ATA_DEV_NONE;
2490 DPRINTK("EXIT, link offline\n");
2494 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2496 printk(KERN_ERR "ata%u: COMRESET failed "
2497 "(device not ready)\n", ap->id);
2499 DPRINTK("EXIT, device not ready\n");
2503 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2505 *class = ata_dev_try_classify(ap, 0, NULL);
2507 DPRINTK("EXIT, class=%u\n", *class);
2512 * ata_std_postreset - standard postreset callback
2513 * @ap: the target ata_port
2514 * @classes: classes of attached devices
2516 * This function is invoked after a successful reset. Note that
2517 * the device might have been reset more than once using
2518 * different reset methods before postreset is invoked.
2520 * This function is to be used as standard callback for
2521 * ata_drive_*_reset().
2524 * Kernel thread context (may sleep)
2526 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2530 /* set cable type if it isn't already set */
2531 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2532 ap->cbl = ATA_CBL_SATA;
2534 /* print link status */
2535 if (ap->cbl == ATA_CBL_SATA)
2536 sata_print_link_status(ap);
2538 /* re-enable interrupts */
2539 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2542 /* is double-select really necessary? */
2543 if (classes[0] != ATA_DEV_NONE)
2544 ap->ops->dev_select(ap, 1);
2545 if (classes[1] != ATA_DEV_NONE)
2546 ap->ops->dev_select(ap, 0);
2548 /* bail out if no device is present */
2549 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2550 DPRINTK("EXIT, no device\n");
2554 /* set up device control */
2555 if (ap->ioaddr.ctl_addr) {
2556 if (ap->flags & ATA_FLAG_MMIO)
2557 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2559 outb(ap->ctl, ap->ioaddr.ctl_addr);
2566 * ata_std_probe_reset - standard probe reset method
2567 * @ap: prot to perform probe-reset
2568 * @classes: resulting classes of attached devices
2570 * The stock off-the-shelf ->probe_reset method.
2573 * Kernel thread context (may sleep)
2576 * 0 on success, -errno otherwise.
2578 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2580 ata_reset_fn_t hardreset;
2583 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2584 hardreset = sata_std_hardreset;
2586 return ata_drive_probe_reset(ap, ata_std_probeinit,
2587 ata_std_softreset, hardreset,
2588 ata_std_postreset, classes);
2591 int ata_do_reset(struct ata_port *ap,
2592 ata_reset_fn_t reset, ata_postreset_fn_t postreset,
2593 int verbose, unsigned int *classes)
2597 for (i = 0; i < ATA_MAX_DEVICES; i++)
2598 classes[i] = ATA_DEV_UNKNOWN;
2600 rc = reset(ap, verbose, classes);
2604 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2605 * is complete and convert all ATA_DEV_UNKNOWN to
2608 for (i = 0; i < ATA_MAX_DEVICES; i++)
2609 if (classes[i] != ATA_DEV_UNKNOWN)
2612 if (i < ATA_MAX_DEVICES)
2613 for (i = 0; i < ATA_MAX_DEVICES; i++)
2614 if (classes[i] == ATA_DEV_UNKNOWN)
2615 classes[i] = ATA_DEV_NONE;
2618 postreset(ap, classes);
2624 * ata_drive_probe_reset - Perform probe reset with given methods
2625 * @ap: port to reset
2626 * @probeinit: probeinit method (can be NULL)
2627 * @softreset: softreset method (can be NULL)
2628 * @hardreset: hardreset method (can be NULL)
2629 * @postreset: postreset method (can be NULL)
2630 * @classes: resulting classes of attached devices
2632 * Reset the specified port and classify attached devices using
2633 * given methods. This function prefers softreset but tries all
2634 * possible reset sequences to reset and classify devices. This
2635 * function is intended to be used for constructing ->probe_reset
2636 * callback by low level drivers.
2638 * Reset methods should follow the following rules.
2640 * - Return 0 on sucess, -errno on failure.
2641 * - If classification is supported, fill classes[] with
2642 * recognized class codes.
2643 * - If classification is not supported, leave classes[] alone.
2644 * - If verbose is non-zero, print error message on failure;
2645 * otherwise, shut up.
2648 * Kernel thread context (may sleep)
2651 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2652 * if classification fails, and any error code from reset
2655 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2656 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2657 ata_postreset_fn_t postreset, unsigned int *classes)
2664 if (softreset && !ata_set_sata_spd_needed(ap)) {
2665 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
2666 if (rc == 0 && classes[0] != ATA_DEV_UNKNOWN)
2668 printk(KERN_INFO "ata%u: softreset failed, will try "
2669 "hardreset in 5 secs\n", ap->id);
2677 rc = ata_do_reset(ap, hardreset, postreset, 0, classes);
2679 if (classes[0] != ATA_DEV_UNKNOWN)
2684 if (ata_down_sata_spd_limit(ap))
2687 printk(KERN_INFO "ata%u: hardreset failed, will retry "
2688 "in 5 secs\n", ap->id);
2693 printk(KERN_INFO "ata%u: hardreset succeeded without "
2694 "classification, will retry softreset in 5 secs\n",
2698 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
2702 if (rc == 0 && classes[0] == ATA_DEV_UNKNOWN)
2708 * ata_dev_same_device - Determine whether new ID matches configured device
2709 * @ap: port on which the device to compare against resides
2710 * @dev: device to compare against
2711 * @new_class: class of the new device
2712 * @new_id: IDENTIFY page of the new device
2714 * Compare @new_class and @new_id against @dev and determine
2715 * whether @dev is the device indicated by @new_class and
2722 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2724 static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2725 unsigned int new_class, const u16 *new_id)
2727 const u16 *old_id = dev->id;
2728 unsigned char model[2][41], serial[2][21];
2731 if (dev->class != new_class) {
2733 "ata%u: dev %u class mismatch %d != %d\n",
2734 ap->id, dev->devno, dev->class, new_class);
2738 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2739 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2740 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2741 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2742 new_n_sectors = ata_id_n_sectors(new_id);
2744 if (strcmp(model[0], model[1])) {
2746 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2747 ap->id, dev->devno, model[0], model[1]);
2751 if (strcmp(serial[0], serial[1])) {
2753 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2754 ap->id, dev->devno, serial[0], serial[1]);
2758 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2760 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2761 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2762 (unsigned long long)new_n_sectors);
2770 * ata_dev_revalidate - Revalidate ATA device
2771 * @ap: port on which the device to revalidate resides
2772 * @dev: device to revalidate
2773 * @post_reset: is this revalidation after reset?
2775 * Re-read IDENTIFY page and make sure @dev is still attached to
2779 * Kernel thread context (may sleep)
2782 * 0 on success, negative errno otherwise
2784 int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2787 unsigned int class = dev->class;
2791 if (!ata_dev_enabled(dev)) {
2796 /* allocate & read ID data */
2797 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2801 /* is the device still there? */
2802 if (!ata_dev_same_device(ap, dev, class, id)) {
2810 /* configure device according to the new ID */
2811 rc = ata_dev_configure(ap, dev, 0);
2816 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2817 ap->id, dev->devno, rc);
2822 static const char * const ata_dma_blacklist [] = {
2823 "WDC AC11000H", NULL,
2824 "WDC AC22100H", NULL,
2825 "WDC AC32500H", NULL,
2826 "WDC AC33100H", NULL,
2827 "WDC AC31600H", NULL,
2828 "WDC AC32100H", "24.09P07",
2829 "WDC AC23200L", "21.10N21",
2830 "Compaq CRD-8241B", NULL,
2835 "SanDisk SDP3B", NULL,
2836 "SanDisk SDP3B-64", NULL,
2837 "SANYO CD-ROM CRD", NULL,
2838 "HITACHI CDR-8", NULL,
2839 "HITACHI CDR-8335", NULL,
2840 "HITACHI CDR-8435", NULL,
2841 "Toshiba CD-ROM XM-6202B", NULL,
2842 "TOSHIBA CD-ROM XM-1702BC", NULL,
2844 "E-IDE CD-ROM CR-840", NULL,
2845 "CD-ROM Drive/F5A", NULL,
2846 "WPI CDD-820", NULL,
2847 "SAMSUNG CD-ROM SC-148C", NULL,
2848 "SAMSUNG CD-ROM SC", NULL,
2849 "SanDisk SDP3B-64", NULL,
2850 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2851 "_NEC DV5800A", NULL,
2852 "SAMSUNG CD-ROM SN-124", "N001"
2855 static int ata_strim(char *s, size_t len)
2857 len = strnlen(s, len);
2859 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2860 while ((len > 0) && (s[len - 1] == ' ')) {
2867 static int ata_dma_blacklisted(const struct ata_device *dev)
2869 unsigned char model_num[40];
2870 unsigned char model_rev[16];
2871 unsigned int nlen, rlen;
2874 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2876 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2878 nlen = ata_strim(model_num, sizeof(model_num));
2879 rlen = ata_strim(model_rev, sizeof(model_rev));
2881 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2882 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2883 if (ata_dma_blacklist[i+1] == NULL)
2885 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2893 * ata_dev_xfermask - Compute supported xfermask of the given device
2894 * @ap: Port on which the device to compute xfermask for resides
2895 * @dev: Device to compute xfermask for
2897 * Compute supported xfermask of @dev and store it in
2898 * dev->*_mask. This function is responsible for applying all
2899 * known limits including host controller limits, device
2902 * FIXME: The current implementation limits all transfer modes to
2903 * the fastest of the lowested device on the port. This is not
2904 * required on most controllers.
2909 static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
2911 struct ata_host_set *hs = ap->host_set;
2912 unsigned long xfer_mask;
2915 xfer_mask = ata_pack_xfermask(ap->pio_mask,
2916 ap->mwdma_mask, ap->udma_mask);
2918 /* Apply cable rule here. Don't apply it early because when
2919 * we handle hot plug the cable type can itself change.
2921 if (ap->cbl == ATA_CBL_PATA40)
2922 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
2924 /* FIXME: Use port-wide xfermask for now */
2925 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2926 struct ata_device *d = &ap->device[i];
2928 if (ata_dev_absent(d))
2931 if (ata_dev_disabled(d)) {
2932 /* to avoid violating device selection timing */
2933 xfer_mask &= ata_pack_xfermask(d->pio_mask,
2934 UINT_MAX, UINT_MAX);
2938 xfer_mask &= ata_pack_xfermask(d->pio_mask,
2939 d->mwdma_mask, d->udma_mask);
2940 xfer_mask &= ata_id_xfermask(d->id);
2941 if (ata_dma_blacklisted(d))
2942 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2945 if (ata_dma_blacklisted(dev))
2946 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2947 "disabling DMA\n", ap->id, dev->devno);
2949 if (hs->flags & ATA_HOST_SIMPLEX) {
2950 if (hs->simplex_claimed)
2951 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2954 if (ap->ops->mode_filter)
2955 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
2957 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
2958 &dev->mwdma_mask, &dev->udma_mask);
2962 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2963 * @ap: Port associated with device @dev
2964 * @dev: Device to which command will be sent
2966 * Issue SET FEATURES - XFER MODE command to device @dev
2970 * PCI/etc. bus probe sem.
2973 * 0 on success, AC_ERR_* mask otherwise.
2976 static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
2977 struct ata_device *dev)
2979 struct ata_taskfile tf;
2980 unsigned int err_mask;
2982 /* set up set-features taskfile */
2983 DPRINTK("set features - xfer mode\n");
2985 ata_tf_init(ap, &tf, dev->devno);
2986 tf.command = ATA_CMD_SET_FEATURES;
2987 tf.feature = SETFEATURES_XFER;
2988 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2989 tf.protocol = ATA_PROT_NODATA;
2990 tf.nsect = dev->xfer_mode;
2992 err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
2994 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2999 * ata_dev_init_params - Issue INIT DEV PARAMS command
3000 * @ap: Port associated with device @dev
3001 * @dev: Device to which command will be sent
3004 * Kernel thread context (may sleep)
3007 * 0 on success, AC_ERR_* mask otherwise.
3010 static unsigned int ata_dev_init_params(struct ata_port *ap,
3011 struct ata_device *dev,
3015 struct ata_taskfile tf;
3016 unsigned int err_mask;
3018 /* Number of sectors per track 1-255. Number of heads 1-16 */
3019 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
3020 return AC_ERR_INVALID;
3022 /* set up init dev params taskfile */
3023 DPRINTK("init dev params \n");
3025 ata_tf_init(ap, &tf, dev->devno);
3026 tf.command = ATA_CMD_INIT_DEV_PARAMS;
3027 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3028 tf.protocol = ATA_PROT_NODATA;
3030 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
3032 err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
3034 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3039 * ata_sg_clean - Unmap DMA memory associated with command
3040 * @qc: Command containing DMA memory to be released
3042 * Unmap all mapped DMA memory associated with this command.
3045 * spin_lock_irqsave(host_set lock)
3048 static void ata_sg_clean(struct ata_queued_cmd *qc)
3050 struct ata_port *ap = qc->ap;
3051 struct scatterlist *sg = qc->__sg;
3052 int dir = qc->dma_dir;
3053 void *pad_buf = NULL;
3055 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
3056 WARN_ON(sg == NULL);
3058 if (qc->flags & ATA_QCFLAG_SINGLE)
3059 WARN_ON(qc->n_elem > 1);
3061 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
3063 /* if we padded the buffer out to 32-bit bound, and data
3064 * xfer direction is from-device, we must copy from the
3065 * pad buffer back into the supplied buffer
3067 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
3068 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3070 if (qc->flags & ATA_QCFLAG_SG) {
3072 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
3073 /* restore last sg */
3074 sg[qc->orig_n_elem - 1].length += qc->pad_len;
3076 struct scatterlist *psg = &qc->pad_sgent;
3077 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3078 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
3079 kunmap_atomic(addr, KM_IRQ0);
3083 dma_unmap_single(ap->dev,
3084 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
3087 sg->length += qc->pad_len;
3089 memcpy(qc->buf_virt + sg->length - qc->pad_len,
3090 pad_buf, qc->pad_len);
3093 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3098 * ata_fill_sg - Fill PCI IDE PRD table
3099 * @qc: Metadata associated with taskfile to be transferred
3101 * Fill PCI IDE PRD (scatter-gather) table with segments
3102 * associated with the current disk command.
3105 * spin_lock_irqsave(host_set lock)
3108 static void ata_fill_sg(struct ata_queued_cmd *qc)
3110 struct ata_port *ap = qc->ap;
3111 struct scatterlist *sg;
3114 WARN_ON(qc->__sg == NULL);
3115 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
3118 ata_for_each_sg(sg, qc) {
3122 /* determine if physical DMA addr spans 64K boundary.
3123 * Note h/w doesn't support 64-bit, so we unconditionally
3124 * truncate dma_addr_t to u32.
3126 addr = (u32) sg_dma_address(sg);
3127 sg_len = sg_dma_len(sg);
3130 offset = addr & 0xffff;
3132 if ((offset + sg_len) > 0x10000)
3133 len = 0x10000 - offset;
3135 ap->prd[idx].addr = cpu_to_le32(addr);
3136 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3137 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3146 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3149 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3150 * @qc: Metadata associated with taskfile to check
3152 * Allow low-level driver to filter ATA PACKET commands, returning
3153 * a status indicating whether or not it is OK to use DMA for the
3154 * supplied PACKET command.
3157 * spin_lock_irqsave(host_set lock)
3159 * RETURNS: 0 when ATAPI DMA can be used
3162 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3164 struct ata_port *ap = qc->ap;
3165 int rc = 0; /* Assume ATAPI DMA is OK by default */
3167 if (ap->ops->check_atapi_dma)
3168 rc = ap->ops->check_atapi_dma(qc);
3173 * ata_qc_prep - Prepare taskfile for submission
3174 * @qc: Metadata associated with taskfile to be prepared
3176 * Prepare ATA taskfile for submission.
3179 * spin_lock_irqsave(host_set lock)
3181 void ata_qc_prep(struct ata_queued_cmd *qc)
3183 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3189 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3192 * ata_sg_init_one - Associate command with memory buffer
3193 * @qc: Command to be associated
3194 * @buf: Memory buffer
3195 * @buflen: Length of memory buffer, in bytes.
3197 * Initialize the data-related elements of queued_cmd @qc
3198 * to point to a single memory buffer, @buf of byte length @buflen.
3201 * spin_lock_irqsave(host_set lock)
3204 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3206 struct scatterlist *sg;
3208 qc->flags |= ATA_QCFLAG_SINGLE;
3210 memset(&qc->sgent, 0, sizeof(qc->sgent));
3211 qc->__sg = &qc->sgent;
3213 qc->orig_n_elem = 1;
3217 sg_init_one(sg, buf, buflen);
3221 * ata_sg_init - Associate command with scatter-gather table.
3222 * @qc: Command to be associated
3223 * @sg: Scatter-gather table.
3224 * @n_elem: Number of elements in s/g table.
3226 * Initialize the data-related elements of queued_cmd @qc
3227 * to point to a scatter-gather table @sg, containing @n_elem
3231 * spin_lock_irqsave(host_set lock)
3234 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3235 unsigned int n_elem)
3237 qc->flags |= ATA_QCFLAG_SG;
3239 qc->n_elem = n_elem;
3240 qc->orig_n_elem = n_elem;
3244 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3245 * @qc: Command with memory buffer to be mapped.
3247 * DMA-map the memory buffer associated with queued_cmd @qc.
3250 * spin_lock_irqsave(host_set lock)
3253 * Zero on success, negative on error.
3256 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3258 struct ata_port *ap = qc->ap;
3259 int dir = qc->dma_dir;
3260 struct scatterlist *sg = qc->__sg;
3261 dma_addr_t dma_address;
3264 /* we must lengthen transfers to end on a 32-bit boundary */
3265 qc->pad_len = sg->length & 3;
3267 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3268 struct scatterlist *psg = &qc->pad_sgent;
3270 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3272 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3274 if (qc->tf.flags & ATA_TFLAG_WRITE)
3275 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3278 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3279 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3281 sg->length -= qc->pad_len;
3282 if (sg->length == 0)
3285 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3286 sg->length, qc->pad_len);
3294 dma_address = dma_map_single(ap->dev, qc->buf_virt,
3296 if (dma_mapping_error(dma_address)) {
3298 sg->length += qc->pad_len;
3302 sg_dma_address(sg) = dma_address;
3303 sg_dma_len(sg) = sg->length;
3306 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3307 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3313 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3314 * @qc: Command with scatter-gather table to be mapped.
3316 * DMA-map the scatter-gather table associated with queued_cmd @qc.
3319 * spin_lock_irqsave(host_set lock)
3322 * Zero on success, negative on error.
3326 static int ata_sg_setup(struct ata_queued_cmd *qc)
3328 struct ata_port *ap = qc->ap;
3329 struct scatterlist *sg = qc->__sg;
3330 struct scatterlist *lsg = &sg[qc->n_elem - 1];
3331 int n_elem, pre_n_elem, dir, trim_sg = 0;
3333 VPRINTK("ENTER, ata%u\n", ap->id);
3334 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
3336 /* we must lengthen transfers to end on a 32-bit boundary */
3337 qc->pad_len = lsg->length & 3;
3339 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3340 struct scatterlist *psg = &qc->pad_sgent;
3341 unsigned int offset;
3343 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3345 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3348 * psg->page/offset are used to copy to-be-written
3349 * data in this function or read data in ata_sg_clean.
3351 offset = lsg->offset + lsg->length - qc->pad_len;
3352 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3353 psg->offset = offset_in_page(offset);
3355 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3356 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3357 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
3358 kunmap_atomic(addr, KM_IRQ0);
3361 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3362 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3364 lsg->length -= qc->pad_len;
3365 if (lsg->length == 0)
3368 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3369 qc->n_elem - 1, lsg->length, qc->pad_len);
3372 pre_n_elem = qc->n_elem;
3373 if (trim_sg && pre_n_elem)
3382 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
3384 /* restore last sg */
3385 lsg->length += qc->pad_len;
3389 DPRINTK("%d sg elements mapped\n", n_elem);
3392 qc->n_elem = n_elem;
3398 * ata_poll_qc_complete - turn irq back on and finish qc
3399 * @qc: Command to complete
3400 * @err_mask: ATA status register content
3403 * None. (grabs host lock)
3406 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
3408 struct ata_port *ap = qc->ap;
3409 unsigned long flags;
3411 spin_lock_irqsave(&ap->host_set->lock, flags);
3412 ap->flags &= ~ATA_FLAG_NOINTR;
3414 ata_qc_complete(qc);
3415 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3419 * ata_pio_poll - poll using PIO, depending on current state
3420 * @qc: qc in progress
3423 * None. (executing in kernel thread context)
3426 * timeout value to use
3428 static unsigned long ata_pio_poll(struct ata_queued_cmd *qc)
3430 struct ata_port *ap = qc->ap;
3432 unsigned int poll_state = HSM_ST_UNKNOWN;
3433 unsigned int reg_state = HSM_ST_UNKNOWN;
3435 switch (ap->hsm_task_state) {
3438 poll_state = HSM_ST_POLL;
3442 case HSM_ST_LAST_POLL:
3443 poll_state = HSM_ST_LAST_POLL;
3444 reg_state = HSM_ST_LAST;
3451 status = ata_chk_status(ap);
3452 if (status & ATA_BUSY) {
3453 if (time_after(jiffies, ap->pio_task_timeout)) {
3454 qc->err_mask |= AC_ERR_TIMEOUT;
3455 ap->hsm_task_state = HSM_ST_TMOUT;
3458 ap->hsm_task_state = poll_state;
3459 return ATA_SHORT_PAUSE;
3462 ap->hsm_task_state = reg_state;
3467 * ata_pio_complete - check if drive is busy or idle
3468 * @qc: qc to complete
3471 * None. (executing in kernel thread context)
3474 * Non-zero if qc completed, zero otherwise.
3476 static int ata_pio_complete(struct ata_queued_cmd *qc)
3478 struct ata_port *ap = qc->ap;
3482 * This is purely heuristic. This is a fast path. Sometimes when
3483 * we enter, BSY will be cleared in a chk-status or two. If not,
3484 * the drive is probably seeking or something. Snooze for a couple
3485 * msecs, then chk-status again. If still busy, fall back to
3486 * HSM_ST_POLL state.
3488 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3489 if (drv_stat & ATA_BUSY) {
3491 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3492 if (drv_stat & ATA_BUSY) {
3493 ap->hsm_task_state = HSM_ST_LAST_POLL;
3494 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3499 drv_stat = ata_wait_idle(ap);
3500 if (!ata_ok(drv_stat)) {
3501 qc->err_mask |= __ac_err_mask(drv_stat);
3502 ap->hsm_task_state = HSM_ST_ERR;
3506 ap->hsm_task_state = HSM_ST_IDLE;
3508 WARN_ON(qc->err_mask);
3509 ata_poll_qc_complete(qc);
3511 /* another command may start at this point */
3518 * swap_buf_le16 - swap halves of 16-bit words in place
3519 * @buf: Buffer to swap
3520 * @buf_words: Number of 16-bit words in buffer.
3522 * Swap halves of 16-bit words if needed to convert from
3523 * little-endian byte order to native cpu byte order, or
3527 * Inherited from caller.
3529 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3534 for (i = 0; i < buf_words; i++)
3535 buf[i] = le16_to_cpu(buf[i]);
3536 #endif /* __BIG_ENDIAN */
3540 * ata_mmio_data_xfer - Transfer data by MMIO
3541 * @ap: port to read/write
3543 * @buflen: buffer length
3544 * @write_data: read/write
3546 * Transfer data from/to the device data register by MMIO.
3549 * Inherited from caller.
3552 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3553 unsigned int buflen, int write_data)
3556 unsigned int words = buflen >> 1;
3557 u16 *buf16 = (u16 *) buf;
3558 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3560 /* Transfer multiple of 2 bytes */
3562 for (i = 0; i < words; i++)
3563 writew(le16_to_cpu(buf16[i]), mmio);
3565 for (i = 0; i < words; i++)
3566 buf16[i] = cpu_to_le16(readw(mmio));
3569 /* Transfer trailing 1 byte, if any. */
3570 if (unlikely(buflen & 0x01)) {
3571 u16 align_buf[1] = { 0 };
3572 unsigned char *trailing_buf = buf + buflen - 1;
3575 memcpy(align_buf, trailing_buf, 1);
3576 writew(le16_to_cpu(align_buf[0]), mmio);
3578 align_buf[0] = cpu_to_le16(readw(mmio));
3579 memcpy(trailing_buf, align_buf, 1);
3585 * ata_pio_data_xfer - Transfer data by PIO
3586 * @ap: port to read/write
3588 * @buflen: buffer length
3589 * @write_data: read/write
3591 * Transfer data from/to the device data register by PIO.
3594 * Inherited from caller.
3597 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3598 unsigned int buflen, int write_data)
3600 unsigned int words = buflen >> 1;
3602 /* Transfer multiple of 2 bytes */
3604 outsw(ap->ioaddr.data_addr, buf, words);
3606 insw(ap->ioaddr.data_addr, buf, words);
3608 /* Transfer trailing 1 byte, if any. */
3609 if (unlikely(buflen & 0x01)) {
3610 u16 align_buf[1] = { 0 };
3611 unsigned char *trailing_buf = buf + buflen - 1;
3614 memcpy(align_buf, trailing_buf, 1);
3615 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3617 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3618 memcpy(trailing_buf, align_buf, 1);
3624 * ata_data_xfer - Transfer data from/to the data register.
3625 * @ap: port to read/write
3627 * @buflen: buffer length
3628 * @do_write: read/write
3630 * Transfer data from/to the device data register.
3633 * Inherited from caller.
3636 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3637 unsigned int buflen, int do_write)
3639 /* Make the crap hardware pay the costs not the good stuff */
3640 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3641 unsigned long flags;
3642 local_irq_save(flags);
3643 if (ap->flags & ATA_FLAG_MMIO)
3644 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3646 ata_pio_data_xfer(ap, buf, buflen, do_write);
3647 local_irq_restore(flags);
3649 if (ap->flags & ATA_FLAG_MMIO)
3650 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3652 ata_pio_data_xfer(ap, buf, buflen, do_write);
3657 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3658 * @qc: Command on going
3660 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3663 * Inherited from caller.
3666 static void ata_pio_sector(struct ata_queued_cmd *qc)
3668 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3669 struct scatterlist *sg = qc->__sg;
3670 struct ata_port *ap = qc->ap;
3672 unsigned int offset;
3675 if (qc->cursect == (qc->nsect - 1))
3676 ap->hsm_task_state = HSM_ST_LAST;
3678 page = sg[qc->cursg].page;
3679 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3681 /* get the current page and offset */
3682 page = nth_page(page, (offset >> PAGE_SHIFT));
3683 offset %= PAGE_SIZE;
3685 buf = kmap(page) + offset;
3690 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3695 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3697 /* do the actual data transfer */
3698 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3699 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3705 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3706 * @qc: Command on going
3707 * @bytes: number of bytes
3709 * Transfer Transfer data from/to the ATAPI device.
3712 * Inherited from caller.
3716 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3718 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3719 struct scatterlist *sg = qc->__sg;
3720 struct ata_port *ap = qc->ap;
3723 unsigned int offset, count;
3725 if (qc->curbytes + bytes >= qc->nbytes)
3726 ap->hsm_task_state = HSM_ST_LAST;
3729 if (unlikely(qc->cursg >= qc->n_elem)) {
3731 * The end of qc->sg is reached and the device expects
3732 * more data to transfer. In order not to overrun qc->sg
3733 * and fulfill length specified in the byte count register,
3734 * - for read case, discard trailing data from the device
3735 * - for write case, padding zero data to the device
3737 u16 pad_buf[1] = { 0 };
3738 unsigned int words = bytes >> 1;
3741 if (words) /* warning if bytes > 1 */
3742 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3745 for (i = 0; i < words; i++)
3746 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3748 ap->hsm_task_state = HSM_ST_LAST;
3752 sg = &qc->__sg[qc->cursg];
3755 offset = sg->offset + qc->cursg_ofs;
3757 /* get the current page and offset */
3758 page = nth_page(page, (offset >> PAGE_SHIFT));
3759 offset %= PAGE_SIZE;
3761 /* don't overrun current sg */
3762 count = min(sg->length - qc->cursg_ofs, bytes);
3764 /* don't cross page boundaries */
3765 count = min(count, (unsigned int)PAGE_SIZE - offset);
3767 buf = kmap(page) + offset;
3770 qc->curbytes += count;
3771 qc->cursg_ofs += count;
3773 if (qc->cursg_ofs == sg->length) {
3778 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3780 /* do the actual data transfer */
3781 ata_data_xfer(ap, buf, count, do_write);
3790 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3791 * @qc: Command on going
3793 * Transfer Transfer data from/to the ATAPI device.
3796 * Inherited from caller.
3799 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3801 struct ata_port *ap = qc->ap;
3802 struct ata_device *dev = qc->dev;
3803 unsigned int ireason, bc_lo, bc_hi, bytes;
3804 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3806 ap->ops->tf_read(ap, &qc->tf);
3807 ireason = qc->tf.nsect;
3808 bc_lo = qc->tf.lbam;
3809 bc_hi = qc->tf.lbah;
3810 bytes = (bc_hi << 8) | bc_lo;
3812 /* shall be cleared to zero, indicating xfer of data */
3813 if (ireason & (1 << 0))
3816 /* make sure transfer direction matches expected */
3817 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3818 if (do_write != i_write)
3821 __atapi_pio_bytes(qc, bytes);
3826 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3827 ap->id, dev->devno);
3828 qc->err_mask |= AC_ERR_HSM;
3829 ap->hsm_task_state = HSM_ST_ERR;
3833 * ata_pio_block - start PIO on a block
3834 * @qc: qc to transfer block for
3837 * None. (executing in kernel thread context)
3839 static void ata_pio_block(struct ata_queued_cmd *qc)
3841 struct ata_port *ap = qc->ap;
3845 * This is purely heuristic. This is a fast path.
3846 * Sometimes when we enter, BSY will be cleared in
3847 * a chk-status or two. If not, the drive is probably seeking
3848 * or something. Snooze for a couple msecs, then
3849 * chk-status again. If still busy, fall back to
3850 * HSM_ST_POLL state.
3852 status = ata_busy_wait(ap, ATA_BUSY, 5);
3853 if (status & ATA_BUSY) {
3855 status = ata_busy_wait(ap, ATA_BUSY, 10);
3856 if (status & ATA_BUSY) {
3857 ap->hsm_task_state = HSM_ST_POLL;
3858 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3864 if (status & (ATA_ERR | ATA_DF)) {
3865 qc->err_mask |= AC_ERR_DEV;
3866 ap->hsm_task_state = HSM_ST_ERR;
3870 /* transfer data if any */
3871 if (is_atapi_taskfile(&qc->tf)) {
3872 /* DRQ=0 means no more data to transfer */
3873 if ((status & ATA_DRQ) == 0) {
3874 ap->hsm_task_state = HSM_ST_LAST;
3878 atapi_pio_bytes(qc);
3880 /* handle BSY=0, DRQ=0 as error */
3881 if ((status & ATA_DRQ) == 0) {
3882 qc->err_mask |= AC_ERR_HSM;
3883 ap->hsm_task_state = HSM_ST_ERR;
3891 static void ata_pio_error(struct ata_queued_cmd *qc)
3893 struct ata_port *ap = qc->ap;
3895 if (qc->tf.command != ATA_CMD_PACKET)
3896 printk(KERN_WARNING "ata%u: dev %u PIO error\n",
3897 ap->id, qc->dev->devno);
3899 /* make sure qc->err_mask is available to
3900 * know what's wrong and recover
3902 WARN_ON(qc->err_mask == 0);
3904 ap->hsm_task_state = HSM_ST_IDLE;
3906 ata_poll_qc_complete(qc);
3909 static void ata_pio_task(void *_data)
3911 struct ata_queued_cmd *qc = _data;
3912 struct ata_port *ap = qc->ap;
3913 unsigned long timeout;
3920 switch (ap->hsm_task_state) {
3929 qc_completed = ata_pio_complete(qc);
3933 case HSM_ST_LAST_POLL:
3934 timeout = ata_pio_poll(qc);
3944 ata_port_queue_task(ap, ata_pio_task, qc, timeout);
3945 else if (!qc_completed)
3950 * atapi_packet_task - Write CDB bytes to hardware
3951 * @_data: qc in progress
3953 * When device has indicated its readiness to accept
3954 * a CDB, this function is called. Send the CDB.
3955 * If DMA is to be performed, exit immediately.
3956 * Otherwise, we are in polling mode, so poll
3957 * status under operation succeeds or fails.
3960 * Kernel thread context (may sleep)
3962 static void atapi_packet_task(void *_data)
3964 struct ata_queued_cmd *qc = _data;
3965 struct ata_port *ap = qc->ap;
3968 /* sleep-wait for BSY to clear */
3969 DPRINTK("busy wait\n");
3970 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3971 qc->err_mask |= AC_ERR_TIMEOUT;
3975 /* make sure DRQ is set */
3976 status = ata_chk_status(ap);
3977 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3978 qc->err_mask |= AC_ERR_HSM;
3983 DPRINTK("send cdb\n");
3984 WARN_ON(qc->dev->cdb_len < 12);
3986 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3987 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3988 unsigned long flags;
3990 /* Once we're done issuing command and kicking bmdma,
3991 * irq handler takes over. To not lose irq, we need
3992 * to clear NOINTR flag before sending cdb, but
3993 * interrupt handler shouldn't be invoked before we're
3994 * finished. Hence, the following locking.
3996 spin_lock_irqsave(&ap->host_set->lock, flags);
3997 ap->flags &= ~ATA_FLAG_NOINTR;
3998 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3999 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4000 ap->ops->bmdma_start(qc); /* initiate bmdma */
4001 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4003 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
4005 /* PIO commands are handled by polling */
4006 ap->hsm_task_state = HSM_ST;
4007 ata_port_queue_task(ap, ata_pio_task, qc, 0);
4013 ata_poll_qc_complete(qc);
4017 * ata_qc_new - Request an available ATA command, for queueing
4018 * @ap: Port associated with device @dev
4019 * @dev: Device from whom we request an available command structure
4025 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4027 struct ata_queued_cmd *qc = NULL;
4030 for (i = 0; i < ATA_MAX_QUEUE; i++)
4031 if (!test_and_set_bit(i, &ap->qactive)) {
4032 qc = ata_qc_from_tag(ap, i);
4043 * ata_qc_new_init - Request an available ATA command, and initialize it
4044 * @ap: Port associated with device @dev
4045 * @dev: Device from whom we request an available command structure
4051 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
4052 struct ata_device *dev)
4054 struct ata_queued_cmd *qc;
4056 qc = ata_qc_new(ap);
4069 * ata_qc_free - free unused ata_queued_cmd
4070 * @qc: Command to complete
4072 * Designed to free unused ata_queued_cmd object
4073 * in case something prevents using it.
4076 * spin_lock_irqsave(host_set lock)
4078 void ata_qc_free(struct ata_queued_cmd *qc)
4080 struct ata_port *ap = qc->ap;
4083 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4087 if (likely(ata_tag_valid(tag))) {
4088 if (tag == ap->active_tag)
4089 ap->active_tag = ATA_TAG_POISON;
4090 qc->tag = ATA_TAG_POISON;
4091 clear_bit(tag, &ap->qactive);
4095 void __ata_qc_complete(struct ata_queued_cmd *qc)
4097 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4098 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4100 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4103 /* atapi: mark qc as inactive to prevent the interrupt handler
4104 * from completing the command twice later, before the error handler
4105 * is called. (when rc != 0 and atapi request sense is needed)
4107 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4109 /* call completion callback */
4110 qc->complete_fn(qc);
4113 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4115 struct ata_port *ap = qc->ap;
4117 switch (qc->tf.protocol) {
4119 case ATA_PROT_ATAPI_DMA:
4122 case ATA_PROT_ATAPI:
4124 if (ap->flags & ATA_FLAG_PIO_DMA)
4137 * ata_qc_issue - issue taskfile to device
4138 * @qc: command to issue to device
4140 * Prepare an ATA command to submission to device.
4141 * This includes mapping the data into a DMA-able
4142 * area, filling in the S/G table, and finally
4143 * writing the taskfile to hardware, starting the command.
4146 * spin_lock_irqsave(host_set lock)
4148 void ata_qc_issue(struct ata_queued_cmd *qc)
4150 struct ata_port *ap = qc->ap;
4152 qc->ap->active_tag = qc->tag;
4153 qc->flags |= ATA_QCFLAG_ACTIVE;
4155 if (ata_should_dma_map(qc)) {
4156 if (qc->flags & ATA_QCFLAG_SG) {
4157 if (ata_sg_setup(qc))
4159 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4160 if (ata_sg_setup_one(qc))
4164 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4167 ap->ops->qc_prep(qc);
4169 qc->err_mask |= ap->ops->qc_issue(qc);
4170 if (unlikely(qc->err_mask))
4175 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4176 qc->err_mask |= AC_ERR_SYSTEM;
4178 ata_qc_complete(qc);
4182 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4183 * @qc: command to issue to device
4185 * Using various libata functions and hooks, this function
4186 * starts an ATA command. ATA commands are grouped into
4187 * classes called "protocols", and issuing each type of protocol
4188 * is slightly different.
4190 * May be used as the qc_issue() entry in ata_port_operations.
4193 * spin_lock_irqsave(host_set lock)
4196 * Zero on success, AC_ERR_* mask on failure
4199 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
4201 struct ata_port *ap = qc->ap;
4203 ata_dev_select(ap, qc->dev->devno, 1, 0);
4205 switch (qc->tf.protocol) {
4206 case ATA_PROT_NODATA:
4207 ata_tf_to_host(ap, &qc->tf);
4211 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4212 ap->ops->bmdma_setup(qc); /* set up bmdma */
4213 ap->ops->bmdma_start(qc); /* initiate bmdma */
4216 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4217 ata_qc_set_polling(qc);
4218 ata_tf_to_host(ap, &qc->tf);
4219 ap->hsm_task_state = HSM_ST;
4220 ata_port_queue_task(ap, ata_pio_task, qc, 0);
4223 case ATA_PROT_ATAPI:
4224 ata_qc_set_polling(qc);
4225 ata_tf_to_host(ap, &qc->tf);
4226 ata_port_queue_task(ap, atapi_packet_task, qc, 0);
4229 case ATA_PROT_ATAPI_NODATA:
4230 ap->flags |= ATA_FLAG_NOINTR;
4231 ata_tf_to_host(ap, &qc->tf);
4232 ata_port_queue_task(ap, atapi_packet_task, qc, 0);
4235 case ATA_PROT_ATAPI_DMA:
4236 ap->flags |= ATA_FLAG_NOINTR;
4237 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4238 ap->ops->bmdma_setup(qc); /* set up bmdma */
4239 ata_port_queue_task(ap, atapi_packet_task, qc, 0);
4244 return AC_ERR_SYSTEM;
4251 * ata_host_intr - Handle host interrupt for given (port, task)
4252 * @ap: Port on which interrupt arrived (possibly...)
4253 * @qc: Taskfile currently active in engine
4255 * Handle host interrupt for given queued command. Currently,
4256 * only DMA interrupts are handled. All other commands are
4257 * handled via polling with interrupts disabled (nIEN bit).
4260 * spin_lock_irqsave(host_set lock)
4263 * One if interrupt was handled, zero if not (shared irq).
4266 inline unsigned int ata_host_intr (struct ata_port *ap,
4267 struct ata_queued_cmd *qc)
4269 u8 status, host_stat;
4271 switch (qc->tf.protocol) {
4274 case ATA_PROT_ATAPI_DMA:
4275 case ATA_PROT_ATAPI:
4276 /* check status of DMA engine */
4277 host_stat = ap->ops->bmdma_status(ap);
4278 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4280 /* if it's not our irq... */
4281 if (!(host_stat & ATA_DMA_INTR))
4284 /* before we do anything else, clear DMA-Start bit */
4285 ap->ops->bmdma_stop(qc);
4289 case ATA_PROT_ATAPI_NODATA:
4290 case ATA_PROT_NODATA:
4291 /* check altstatus */
4292 status = ata_altstatus(ap);
4293 if (status & ATA_BUSY)
4296 /* check main status, clearing INTRQ */
4297 status = ata_chk_status(ap);
4298 if (unlikely(status & ATA_BUSY))
4300 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4301 ap->id, qc->tf.protocol, status);
4303 /* ack bmdma irq events */
4304 ap->ops->irq_clear(ap);
4306 /* complete taskfile transaction */
4307 qc->err_mask |= ac_err_mask(status);
4308 ata_qc_complete(qc);
4315 return 1; /* irq handled */
4318 ap->stats.idle_irq++;
4321 if ((ap->stats.idle_irq % 1000) == 0) {
4322 ata_irq_ack(ap, 0); /* debug trap */
4323 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4327 return 0; /* irq not handled */
4331 * ata_interrupt - Default ATA host interrupt handler
4332 * @irq: irq line (unused)
4333 * @dev_instance: pointer to our ata_host_set information structure
4336 * Default interrupt handler for PCI IDE devices. Calls
4337 * ata_host_intr() for each port that is not disabled.
4340 * Obtains host_set lock during operation.
4343 * IRQ_NONE or IRQ_HANDLED.
4346 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4348 struct ata_host_set *host_set = dev_instance;
4350 unsigned int handled = 0;
4351 unsigned long flags;
4353 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4354 spin_lock_irqsave(&host_set->lock, flags);
4356 for (i = 0; i < host_set->n_ports; i++) {
4357 struct ata_port *ap;
4359 ap = host_set->ports[i];
4361 !(ap->flags & (ATA_FLAG_DISABLED | ATA_FLAG_NOINTR))) {
4362 struct ata_queued_cmd *qc;
4364 qc = ata_qc_from_tag(ap, ap->active_tag);
4365 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4366 (qc->flags & ATA_QCFLAG_ACTIVE))
4367 handled |= ata_host_intr(ap, qc);
4371 spin_unlock_irqrestore(&host_set->lock, flags);
4373 return IRQ_RETVAL(handled);
4378 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4379 * without filling any other registers
4381 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4384 struct ata_taskfile tf;
4387 ata_tf_init(ap, &tf, dev->devno);
4390 tf.flags |= ATA_TFLAG_DEVICE;
4391 tf.protocol = ATA_PROT_NODATA;
4393 err = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0);
4395 printk(KERN_ERR "%s: ata command failed: %d\n",
4401 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4405 if (!ata_try_flush_cache(dev))
4408 if (ata_id_has_flush_ext(dev->id))
4409 cmd = ATA_CMD_FLUSH_EXT;
4411 cmd = ATA_CMD_FLUSH;
4413 return ata_do_simple_cmd(ap, dev, cmd);
4416 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4418 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4421 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4423 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4427 * ata_device_resume - wakeup a previously suspended devices
4428 * @ap: port the device is connected to
4429 * @dev: the device to resume
4431 * Kick the drive back into action, by sending it an idle immediate
4432 * command and making sure its transfer mode matches between drive
4436 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4438 if (ap->flags & ATA_FLAG_SUSPENDED) {
4439 struct ata_device *failed_dev;
4440 ap->flags &= ~ATA_FLAG_SUSPENDED;
4441 while (ata_set_mode(ap, &failed_dev))
4442 ata_dev_disable(ap, failed_dev);
4444 if (!ata_dev_enabled(dev))
4446 if (dev->class == ATA_DEV_ATA)
4447 ata_start_drive(ap, dev);
4453 * ata_device_suspend - prepare a device for suspend
4454 * @ap: port the device is connected to
4455 * @dev: the device to suspend
4457 * Flush the cache on the drive, if appropriate, then issue a
4458 * standbynow command.
4460 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
4462 if (!ata_dev_enabled(dev))
4464 if (dev->class == ATA_DEV_ATA)
4465 ata_flush_cache(ap, dev);
4467 if (state.event != PM_EVENT_FREEZE)
4468 ata_standby_drive(ap, dev);
4469 ap->flags |= ATA_FLAG_SUSPENDED;
4474 * ata_port_start - Set port up for dma.
4475 * @ap: Port to initialize
4477 * Called just after data structures for each port are
4478 * initialized. Allocates space for PRD table.
4480 * May be used as the port_start() entry in ata_port_operations.
4483 * Inherited from caller.
4486 int ata_port_start (struct ata_port *ap)
4488 struct device *dev = ap->dev;
4491 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4495 rc = ata_pad_alloc(ap, dev);
4497 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4501 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4508 * ata_port_stop - Undo ata_port_start()
4509 * @ap: Port to shut down
4511 * Frees the PRD table.
4513 * May be used as the port_stop() entry in ata_port_operations.
4516 * Inherited from caller.
4519 void ata_port_stop (struct ata_port *ap)
4521 struct device *dev = ap->dev;
4523 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4524 ata_pad_free(ap, dev);
4527 void ata_host_stop (struct ata_host_set *host_set)
4529 if (host_set->mmio_base)
4530 iounmap(host_set->mmio_base);
4535 * ata_host_remove - Unregister SCSI host structure with upper layers
4536 * @ap: Port to unregister
4537 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4540 * Inherited from caller.
4543 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4545 struct Scsi_Host *sh = ap->host;
4550 scsi_remove_host(sh);
4552 ap->ops->port_stop(ap);
4556 * ata_host_init - Initialize an ata_port structure
4557 * @ap: Structure to initialize
4558 * @host: associated SCSI mid-layer structure
4559 * @host_set: Collection of hosts to which @ap belongs
4560 * @ent: Probe information provided by low-level driver
4561 * @port_no: Port number associated with this ata_port
4563 * Initialize a new ata_port structure, and its associated
4567 * Inherited from caller.
4570 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4571 struct ata_host_set *host_set,
4572 const struct ata_probe_ent *ent, unsigned int port_no)
4578 host->max_channel = 1;
4579 host->unique_id = ata_unique_id++;
4580 host->max_cmd_len = 12;
4582 ap->flags = ATA_FLAG_DISABLED;
4583 ap->id = host->unique_id;
4585 ap->ctl = ATA_DEVCTL_OBS;
4586 ap->host_set = host_set;
4588 ap->port_no = port_no;
4590 ent->legacy_mode ? ent->hard_port_no : port_no;
4591 ap->pio_mask = ent->pio_mask;
4592 ap->mwdma_mask = ent->mwdma_mask;
4593 ap->udma_mask = ent->udma_mask;
4594 ap->flags |= ent->host_flags;
4595 ap->ops = ent->port_ops;
4596 ap->cbl = ATA_CBL_NONE;
4597 ap->sata_spd_limit = UINT_MAX;
4598 ap->active_tag = ATA_TAG_POISON;
4599 ap->last_ctl = 0xFF;
4601 INIT_WORK(&ap->port_task, NULL, NULL);
4602 INIT_LIST_HEAD(&ap->eh_done_q);
4604 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4605 struct ata_device *dev = &ap->device[i];
4607 dev->pio_mask = UINT_MAX;
4608 dev->mwdma_mask = UINT_MAX;
4609 dev->udma_mask = UINT_MAX;
4613 ap->stats.unhandled_irq = 1;
4614 ap->stats.idle_irq = 1;
4617 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4621 * ata_host_add - Attach low-level ATA driver to system
4622 * @ent: Information provided by low-level driver
4623 * @host_set: Collections of ports to which we add
4624 * @port_no: Port number associated with this host
4626 * Attach low-level ATA driver to system.
4629 * PCI/etc. bus probe sem.
4632 * New ata_port on success, for NULL on error.
4635 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4636 struct ata_host_set *host_set,
4637 unsigned int port_no)
4639 struct Scsi_Host *host;
4640 struct ata_port *ap;
4645 if (!ent->port_ops->probe_reset &&
4646 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
4647 printk(KERN_ERR "ata%u: no reset mechanism available\n",
4652 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4656 host->transportt = &ata_scsi_transport_template;
4658 ap = (struct ata_port *) &host->hostdata[0];
4660 ata_host_init(ap, host, host_set, ent, port_no);
4662 rc = ap->ops->port_start(ap);
4669 scsi_host_put(host);
4674 * ata_device_add - Register hardware device with ATA and SCSI layers
4675 * @ent: Probe information describing hardware device to be registered
4677 * This function processes the information provided in the probe
4678 * information struct @ent, allocates the necessary ATA and SCSI
4679 * host information structures, initializes them, and registers
4680 * everything with requisite kernel subsystems.
4682 * This function requests irqs, probes the ATA bus, and probes
4686 * PCI/etc. bus probe sem.
4689 * Number of ports registered. Zero on error (no ports registered).
4692 int ata_device_add(const struct ata_probe_ent *ent)
4694 unsigned int count = 0, i;
4695 struct device *dev = ent->dev;
4696 struct ata_host_set *host_set;
4699 /* alloc a container for our list of ATA ports (buses) */
4700 host_set = kzalloc(sizeof(struct ata_host_set) +
4701 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4704 spin_lock_init(&host_set->lock);
4706 host_set->dev = dev;
4707 host_set->n_ports = ent->n_ports;
4708 host_set->irq = ent->irq;
4709 host_set->mmio_base = ent->mmio_base;
4710 host_set->private_data = ent->private_data;
4711 host_set->ops = ent->port_ops;
4712 host_set->flags = ent->host_set_flags;
4714 /* register each port bound to this device */
4715 for (i = 0; i < ent->n_ports; i++) {
4716 struct ata_port *ap;
4717 unsigned long xfer_mode_mask;
4719 ap = ata_host_add(ent, host_set, i);
4723 host_set->ports[i] = ap;
4724 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4725 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4726 (ap->pio_mask << ATA_SHIFT_PIO);
4728 /* print per-port info to dmesg */
4729 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4730 "bmdma 0x%lX irq %lu\n",
4732 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4733 ata_mode_string(xfer_mode_mask),
4734 ap->ioaddr.cmd_addr,
4735 ap->ioaddr.ctl_addr,
4736 ap->ioaddr.bmdma_addr,
4740 host_set->ops->irq_clear(ap);
4747 /* obtain irq, that is shared between channels */
4748 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4749 DRV_NAME, host_set))
4752 /* perform each probe synchronously */
4753 DPRINTK("probe begin\n");
4754 for (i = 0; i < count; i++) {
4755 struct ata_port *ap;
4758 ap = host_set->ports[i];
4760 DPRINTK("ata%u: bus probe begin\n", ap->id);
4761 rc = ata_bus_probe(ap);
4762 DPRINTK("ata%u: bus probe end\n", ap->id);
4765 /* FIXME: do something useful here?
4766 * Current libata behavior will
4767 * tear down everything when
4768 * the module is removed
4769 * or the h/w is unplugged.
4773 rc = scsi_add_host(ap->host, dev);
4775 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4777 /* FIXME: do something useful here */
4778 /* FIXME: handle unconditional calls to
4779 * scsi_scan_host and ata_host_remove, below,
4785 /* probes are done, now scan each port's disk(s) */
4786 DPRINTK("host probe begin\n");
4787 for (i = 0; i < count; i++) {
4788 struct ata_port *ap = host_set->ports[i];
4790 ata_scsi_scan_host(ap);
4793 dev_set_drvdata(dev, host_set);
4795 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4796 return ent->n_ports; /* success */
4799 for (i = 0; i < count; i++) {
4800 ata_host_remove(host_set->ports[i], 1);
4801 scsi_host_put(host_set->ports[i]->host);
4805 VPRINTK("EXIT, returning 0\n");
4810 * ata_host_set_remove - PCI layer callback for device removal
4811 * @host_set: ATA host set that was removed
4813 * Unregister all objects associated with this host set. Free those
4817 * Inherited from calling layer (may sleep).
4820 void ata_host_set_remove(struct ata_host_set *host_set)
4822 struct ata_port *ap;
4825 for (i = 0; i < host_set->n_ports; i++) {
4826 ap = host_set->ports[i];
4827 scsi_remove_host(ap->host);
4830 free_irq(host_set->irq, host_set);
4832 for (i = 0; i < host_set->n_ports; i++) {
4833 ap = host_set->ports[i];
4835 ata_scsi_release(ap->host);
4837 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4838 struct ata_ioports *ioaddr = &ap->ioaddr;
4840 if (ioaddr->cmd_addr == 0x1f0)
4841 release_region(0x1f0, 8);
4842 else if (ioaddr->cmd_addr == 0x170)
4843 release_region(0x170, 8);
4846 scsi_host_put(ap->host);
4849 if (host_set->ops->host_stop)
4850 host_set->ops->host_stop(host_set);
4856 * ata_scsi_release - SCSI layer callback hook for host unload
4857 * @host: libata host to be unloaded
4859 * Performs all duties necessary to shut down a libata port...
4860 * Kill port kthread, disable port, and release resources.
4863 * Inherited from SCSI layer.
4869 int ata_scsi_release(struct Scsi_Host *host)
4871 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4876 ap->ops->port_disable(ap);
4877 ata_host_remove(ap, 0);
4878 for (i = 0; i < ATA_MAX_DEVICES; i++)
4879 kfree(ap->device[i].id);
4886 * ata_std_ports - initialize ioaddr with standard port offsets.
4887 * @ioaddr: IO address structure to be initialized
4889 * Utility function which initializes data_addr, error_addr,
4890 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4891 * device_addr, status_addr, and command_addr to standard offsets
4892 * relative to cmd_addr.
4894 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4897 void ata_std_ports(struct ata_ioports *ioaddr)
4899 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4900 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4901 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4902 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4903 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4904 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4905 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4906 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4907 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4908 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4914 void ata_pci_host_stop (struct ata_host_set *host_set)
4916 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4918 pci_iounmap(pdev, host_set->mmio_base);
4922 * ata_pci_remove_one - PCI layer callback for device removal
4923 * @pdev: PCI device that was removed
4925 * PCI layer indicates to libata via this hook that
4926 * hot-unplug or module unload event has occurred.
4927 * Handle this by unregistering all objects associated
4928 * with this PCI device. Free those objects. Then finally
4929 * release PCI resources and disable device.
4932 * Inherited from PCI layer (may sleep).
4935 void ata_pci_remove_one (struct pci_dev *pdev)
4937 struct device *dev = pci_dev_to_dev(pdev);
4938 struct ata_host_set *host_set = dev_get_drvdata(dev);
4940 ata_host_set_remove(host_set);
4941 pci_release_regions(pdev);
4942 pci_disable_device(pdev);
4943 dev_set_drvdata(dev, NULL);
4946 /* move to PCI subsystem */
4947 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4949 unsigned long tmp = 0;
4951 switch (bits->width) {
4954 pci_read_config_byte(pdev, bits->reg, &tmp8);
4960 pci_read_config_word(pdev, bits->reg, &tmp16);
4966 pci_read_config_dword(pdev, bits->reg, &tmp32);
4977 return (tmp == bits->val) ? 1 : 0;
4980 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4982 pci_save_state(pdev);
4983 pci_disable_device(pdev);
4984 pci_set_power_state(pdev, PCI_D3hot);
4988 int ata_pci_device_resume(struct pci_dev *pdev)
4990 pci_set_power_state(pdev, PCI_D0);
4991 pci_restore_state(pdev);
4992 pci_enable_device(pdev);
4993 pci_set_master(pdev);
4996 #endif /* CONFIG_PCI */
4999 static int __init ata_init(void)
5001 ata_wq = create_workqueue("ata");
5005 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5009 static void __exit ata_exit(void)
5011 destroy_workqueue(ata_wq);
5014 module_init(ata_init);
5015 module_exit(ata_exit);
5017 static unsigned long ratelimit_time;
5018 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5020 int ata_ratelimit(void)
5023 unsigned long flags;
5025 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5027 if (time_after(jiffies, ratelimit_time)) {
5029 ratelimit_time = jiffies + (HZ/5);
5033 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5039 * libata is essentially a library of internal helper functions for
5040 * low-level ATA host controller drivers. As such, the API/ABI is
5041 * likely to change as new drivers are added and updated.
5042 * Do not depend on ABI/API stability.
5045 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5046 EXPORT_SYMBOL_GPL(ata_std_ports);
5047 EXPORT_SYMBOL_GPL(ata_device_add);
5048 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5049 EXPORT_SYMBOL_GPL(ata_sg_init);
5050 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5051 EXPORT_SYMBOL_GPL(__ata_qc_complete);
5052 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5053 EXPORT_SYMBOL_GPL(ata_tf_load);
5054 EXPORT_SYMBOL_GPL(ata_tf_read);
5055 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5056 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5057 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5058 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5059 EXPORT_SYMBOL_GPL(ata_check_status);
5060 EXPORT_SYMBOL_GPL(ata_altstatus);
5061 EXPORT_SYMBOL_GPL(ata_exec_command);
5062 EXPORT_SYMBOL_GPL(ata_port_start);
5063 EXPORT_SYMBOL_GPL(ata_port_stop);
5064 EXPORT_SYMBOL_GPL(ata_host_stop);
5065 EXPORT_SYMBOL_GPL(ata_interrupt);
5066 EXPORT_SYMBOL_GPL(ata_qc_prep);
5067 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
5068 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5069 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5070 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5071 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5072 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5073 EXPORT_SYMBOL_GPL(ata_port_probe);
5074 EXPORT_SYMBOL_GPL(sata_phy_reset);
5075 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5076 EXPORT_SYMBOL_GPL(ata_bus_reset);
5077 EXPORT_SYMBOL_GPL(ata_std_probeinit);
5078 EXPORT_SYMBOL_GPL(ata_std_softreset);
5079 EXPORT_SYMBOL_GPL(sata_std_hardreset);
5080 EXPORT_SYMBOL_GPL(ata_std_postreset);
5081 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
5082 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
5083 EXPORT_SYMBOL_GPL(ata_dev_revalidate);
5084 EXPORT_SYMBOL_GPL(ata_dev_classify);
5085 EXPORT_SYMBOL_GPL(ata_dev_pair);
5086 EXPORT_SYMBOL_GPL(ata_port_disable);
5087 EXPORT_SYMBOL_GPL(ata_ratelimit);
5088 EXPORT_SYMBOL_GPL(ata_busy_sleep);
5089 EXPORT_SYMBOL_GPL(ata_port_queue_task);
5090 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5091 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5092 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5093 EXPORT_SYMBOL_GPL(ata_scsi_release);
5094 EXPORT_SYMBOL_GPL(ata_host_intr);
5095 EXPORT_SYMBOL_GPL(ata_id_string);
5096 EXPORT_SYMBOL_GPL(ata_id_c_string);
5097 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5099 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
5100 EXPORT_SYMBOL_GPL(ata_timing_compute);
5101 EXPORT_SYMBOL_GPL(ata_timing_merge);
5104 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5105 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5106 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5107 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5108 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5109 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5110 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
5111 EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5112 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
5113 #endif /* CONFIG_PCI */
5115 EXPORT_SYMBOL_GPL(ata_device_suspend);
5116 EXPORT_SYMBOL_GPL(ata_device_resume);
5117 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5118 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
5120 EXPORT_SYMBOL_GPL(ata_scsi_error);
5121 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5122 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5123 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);