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 int ata_down_sata_spd_limit(struct ata_port *ap);
69 static int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev);
70 static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
71 struct ata_device *dev);
72 static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
74 static unsigned int ata_unique_id = 1;
75 static struct workqueue_struct *ata_wq;
77 int atapi_enabled = 1;
78 module_param(atapi_enabled, int, 0444);
79 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
82 module_param_named(fua, libata_fua, int, 0444);
83 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
85 MODULE_AUTHOR("Jeff Garzik");
86 MODULE_DESCRIPTION("Library module for ATA devices");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
92 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
93 * @tf: Taskfile to convert
94 * @fis: Buffer into which data will output
95 * @pmp: Port multiplier port
97 * Converts a standard ATA taskfile to a Serial ATA
98 * FIS structure (Register - Host to Device).
101 * Inherited from caller.
104 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
106 fis[0] = 0x27; /* Register - Host to Device FIS */
107 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
108 bit 7 indicates Command FIS */
109 fis[2] = tf->command;
110 fis[3] = tf->feature;
117 fis[8] = tf->hob_lbal;
118 fis[9] = tf->hob_lbam;
119 fis[10] = tf->hob_lbah;
120 fis[11] = tf->hob_feature;
123 fis[13] = tf->hob_nsect;
134 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
135 * @fis: Buffer from which data will be input
136 * @tf: Taskfile to output
138 * Converts a serial ATA FIS structure to a standard ATA taskfile.
141 * Inherited from caller.
144 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
146 tf->command = fis[2]; /* status */
147 tf->feature = fis[3]; /* error */
154 tf->hob_lbal = fis[8];
155 tf->hob_lbam = fis[9];
156 tf->hob_lbah = fis[10];
159 tf->hob_nsect = fis[13];
162 static const u8 ata_rw_cmds[] = {
166 ATA_CMD_READ_MULTI_EXT,
167 ATA_CMD_WRITE_MULTI_EXT,
171 ATA_CMD_WRITE_MULTI_FUA_EXT,
175 ATA_CMD_PIO_READ_EXT,
176 ATA_CMD_PIO_WRITE_EXT,
189 ATA_CMD_WRITE_FUA_EXT
193 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
194 * @qc: command to examine and configure
196 * Examine the device configuration and tf->flags to calculate
197 * the proper read/write commands and protocol to use.
202 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
204 struct ata_taskfile *tf = &qc->tf;
205 struct ata_device *dev = qc->dev;
208 int index, fua, lba48, write;
210 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
211 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
212 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
214 if (dev->flags & ATA_DFLAG_PIO) {
215 tf->protocol = ATA_PROT_PIO;
216 index = dev->multi_count ? 0 : 8;
217 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
218 /* Unable to use DMA due to host limitation */
219 tf->protocol = ATA_PROT_PIO;
220 index = dev->multi_count ? 0 : 8;
222 tf->protocol = ATA_PROT_DMA;
226 cmd = ata_rw_cmds[index + fua + lba48 + write];
235 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
236 * @pio_mask: pio_mask
237 * @mwdma_mask: mwdma_mask
238 * @udma_mask: udma_mask
240 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
241 * unsigned int xfer_mask.
249 static unsigned int ata_pack_xfermask(unsigned int pio_mask,
250 unsigned int mwdma_mask,
251 unsigned int udma_mask)
253 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
254 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
255 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
259 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
260 * @xfer_mask: xfer_mask to unpack
261 * @pio_mask: resulting pio_mask
262 * @mwdma_mask: resulting mwdma_mask
263 * @udma_mask: resulting udma_mask
265 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
266 * Any NULL distination masks will be ignored.
268 static void ata_unpack_xfermask(unsigned int xfer_mask,
269 unsigned int *pio_mask,
270 unsigned int *mwdma_mask,
271 unsigned int *udma_mask)
274 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
276 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
278 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
281 static const struct ata_xfer_ent {
285 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
286 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
287 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
292 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
293 * @xfer_mask: xfer_mask of interest
295 * Return matching XFER_* value for @xfer_mask. Only the highest
296 * bit of @xfer_mask is considered.
302 * Matching XFER_* value, 0 if no match found.
304 static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
306 int highbit = fls(xfer_mask) - 1;
307 const struct ata_xfer_ent *ent;
309 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
310 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
311 return ent->base + highbit - ent->shift;
316 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
317 * @xfer_mode: XFER_* of interest
319 * Return matching xfer_mask for @xfer_mode.
325 * Matching xfer_mask, 0 if no match found.
327 static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
329 const struct ata_xfer_ent *ent;
331 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
332 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
333 return 1 << (ent->shift + xfer_mode - ent->base);
338 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
339 * @xfer_mode: XFER_* of interest
341 * Return matching xfer_shift for @xfer_mode.
347 * Matching xfer_shift, -1 if no match found.
349 static int ata_xfer_mode2shift(unsigned int xfer_mode)
351 const struct ata_xfer_ent *ent;
353 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
354 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
360 * ata_mode_string - convert xfer_mask to string
361 * @xfer_mask: mask of bits supported; only highest bit counts.
363 * Determine string which represents the highest speed
364 * (highest bit in @modemask).
370 * Constant C string representing highest speed listed in
371 * @mode_mask, or the constant C string "<n/a>".
373 static const char *ata_mode_string(unsigned int xfer_mask)
375 static const char * const xfer_mode_str[] = {
395 highbit = fls(xfer_mask) - 1;
396 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
397 return xfer_mode_str[highbit];
401 static const char *sata_spd_string(unsigned int spd)
403 static const char * const spd_str[] = {
408 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
410 return spd_str[spd - 1];
413 static void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
415 if (ata_dev_enabled(dev)) {
416 printk(KERN_WARNING "ata%u: dev %u disabled\n",
423 * ata_pio_devchk - PATA device presence detection
424 * @ap: ATA channel to examine
425 * @device: Device to examine (starting at zero)
427 * This technique was originally described in
428 * Hale Landis's ATADRVR (www.ata-atapi.com), and
429 * later found its way into the ATA/ATAPI spec.
431 * Write a pattern to the ATA shadow registers,
432 * and if a device is present, it will respond by
433 * correctly storing and echoing back the
434 * ATA shadow register contents.
440 static unsigned int ata_pio_devchk(struct ata_port *ap,
443 struct ata_ioports *ioaddr = &ap->ioaddr;
446 ap->ops->dev_select(ap, device);
448 outb(0x55, ioaddr->nsect_addr);
449 outb(0xaa, ioaddr->lbal_addr);
451 outb(0xaa, ioaddr->nsect_addr);
452 outb(0x55, ioaddr->lbal_addr);
454 outb(0x55, ioaddr->nsect_addr);
455 outb(0xaa, ioaddr->lbal_addr);
457 nsect = inb(ioaddr->nsect_addr);
458 lbal = inb(ioaddr->lbal_addr);
460 if ((nsect == 0x55) && (lbal == 0xaa))
461 return 1; /* we found a device */
463 return 0; /* nothing found */
467 * ata_mmio_devchk - PATA device presence detection
468 * @ap: ATA channel to examine
469 * @device: Device to examine (starting at zero)
471 * This technique was originally described in
472 * Hale Landis's ATADRVR (www.ata-atapi.com), and
473 * later found its way into the ATA/ATAPI spec.
475 * Write a pattern to the ATA shadow registers,
476 * and if a device is present, it will respond by
477 * correctly storing and echoing back the
478 * ATA shadow register contents.
484 static unsigned int ata_mmio_devchk(struct ata_port *ap,
487 struct ata_ioports *ioaddr = &ap->ioaddr;
490 ap->ops->dev_select(ap, device);
492 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
493 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
495 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
496 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
498 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
499 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
501 nsect = readb((void __iomem *) ioaddr->nsect_addr);
502 lbal = readb((void __iomem *) ioaddr->lbal_addr);
504 if ((nsect == 0x55) && (lbal == 0xaa))
505 return 1; /* we found a device */
507 return 0; /* nothing found */
511 * ata_devchk - PATA device presence detection
512 * @ap: ATA channel to examine
513 * @device: Device to examine (starting at zero)
515 * Dispatch ATA device presence detection, depending
516 * on whether we are using PIO or MMIO to talk to the
517 * ATA shadow registers.
523 static unsigned int ata_devchk(struct ata_port *ap,
526 if (ap->flags & ATA_FLAG_MMIO)
527 return ata_mmio_devchk(ap, device);
528 return ata_pio_devchk(ap, device);
532 * ata_dev_classify - determine device type based on ATA-spec signature
533 * @tf: ATA taskfile register set for device to be identified
535 * Determine from taskfile register contents whether a device is
536 * ATA or ATAPI, as per "Signature and persistence" section
537 * of ATA/PI spec (volume 1, sect 5.14).
543 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
544 * the event of failure.
547 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
549 /* Apple's open source Darwin code hints that some devices only
550 * put a proper signature into the LBA mid/high registers,
551 * So, we only check those. It's sufficient for uniqueness.
554 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
555 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
556 DPRINTK("found ATA device by sig\n");
560 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
561 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
562 DPRINTK("found ATAPI device by sig\n");
563 return ATA_DEV_ATAPI;
566 DPRINTK("unknown device\n");
567 return ATA_DEV_UNKNOWN;
571 * ata_dev_try_classify - Parse returned ATA device signature
572 * @ap: ATA channel to examine
573 * @device: Device to examine (starting at zero)
574 * @r_err: Value of error register on completion
576 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
577 * an ATA/ATAPI-defined set of values is placed in the ATA
578 * shadow registers, indicating the results of device detection
581 * Select the ATA device, and read the values from the ATA shadow
582 * registers. Then parse according to the Error register value,
583 * and the spec-defined values examined by ata_dev_classify().
589 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
593 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
595 struct ata_taskfile tf;
599 ap->ops->dev_select(ap, device);
601 memset(&tf, 0, sizeof(tf));
603 ap->ops->tf_read(ap, &tf);
608 /* see if device passed diags */
611 else if ((device == 0) && (err == 0x81))
616 /* determine if device is ATA or ATAPI */
617 class = ata_dev_classify(&tf);
619 if (class == ATA_DEV_UNKNOWN)
621 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
627 * ata_id_string - Convert IDENTIFY DEVICE page into string
628 * @id: IDENTIFY DEVICE results we will examine
629 * @s: string into which data is output
630 * @ofs: offset into identify device page
631 * @len: length of string to return. must be an even number.
633 * The strings in the IDENTIFY DEVICE page are broken up into
634 * 16-bit chunks. Run through the string, and output each
635 * 8-bit chunk linearly, regardless of platform.
641 void ata_id_string(const u16 *id, unsigned char *s,
642 unsigned int ofs, unsigned int len)
661 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
662 * @id: IDENTIFY DEVICE results we will examine
663 * @s: string into which data is output
664 * @ofs: offset into identify device page
665 * @len: length of string to return. must be an odd number.
667 * This function is identical to ata_id_string except that it
668 * trims trailing spaces and terminates the resulting string with
669 * null. @len must be actual maximum length (even number) + 1.
674 void ata_id_c_string(const u16 *id, unsigned char *s,
675 unsigned int ofs, unsigned int len)
681 ata_id_string(id, s, ofs, len - 1);
683 p = s + strnlen(s, len - 1);
684 while (p > s && p[-1] == ' ')
689 static u64 ata_id_n_sectors(const u16 *id)
691 if (ata_id_has_lba(id)) {
692 if (ata_id_has_lba48(id))
693 return ata_id_u64(id, 100);
695 return ata_id_u32(id, 60);
697 if (ata_id_current_chs_valid(id))
698 return ata_id_u32(id, 57);
700 return id[1] * id[3] * id[6];
705 * ata_noop_dev_select - Select device 0/1 on ATA bus
706 * @ap: ATA channel to manipulate
707 * @device: ATA device (numbered from zero) to select
709 * This function performs no actual function.
711 * May be used as the dev_select() entry in ata_port_operations.
716 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
722 * ata_std_dev_select - Select device 0/1 on ATA bus
723 * @ap: ATA channel to manipulate
724 * @device: ATA device (numbered from zero) to select
726 * Use the method defined in the ATA specification to
727 * make either device 0, or device 1, active on the
728 * ATA channel. Works with both PIO and MMIO.
730 * May be used as the dev_select() entry in ata_port_operations.
736 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
741 tmp = ATA_DEVICE_OBS;
743 tmp = ATA_DEVICE_OBS | ATA_DEV1;
745 if (ap->flags & ATA_FLAG_MMIO) {
746 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
748 outb(tmp, ap->ioaddr.device_addr);
750 ata_pause(ap); /* needed; also flushes, for mmio */
754 * ata_dev_select - Select device 0/1 on ATA bus
755 * @ap: ATA channel to manipulate
756 * @device: ATA device (numbered from zero) to select
757 * @wait: non-zero to wait for Status register BSY bit to clear
758 * @can_sleep: non-zero if context allows sleeping
760 * Use the method defined in the ATA specification to
761 * make either device 0, or device 1, active on the
764 * This is a high-level version of ata_std_dev_select(),
765 * which additionally provides the services of inserting
766 * the proper pauses and status polling, where needed.
772 void ata_dev_select(struct ata_port *ap, unsigned int device,
773 unsigned int wait, unsigned int can_sleep)
775 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
776 ap->id, device, wait);
781 ap->ops->dev_select(ap, device);
784 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
791 * ata_dump_id - IDENTIFY DEVICE info debugging output
792 * @id: IDENTIFY DEVICE page to dump
794 * Dump selected 16-bit words from the given IDENTIFY DEVICE
801 static inline void ata_dump_id(const u16 *id)
803 DPRINTK("49==0x%04x "
813 DPRINTK("80==0x%04x "
823 DPRINTK("88==0x%04x "
830 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
831 * @id: IDENTIFY data to compute xfer mask from
833 * Compute the xfermask for this device. This is not as trivial
834 * as it seems if we must consider early devices correctly.
836 * FIXME: pre IDE drive timing (do we care ?).
844 static unsigned int ata_id_xfermask(const u16 *id)
846 unsigned int pio_mask, mwdma_mask, udma_mask;
848 /* Usual case. Word 53 indicates word 64 is valid */
849 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
850 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
854 /* If word 64 isn't valid then Word 51 high byte holds
855 * the PIO timing number for the maximum. Turn it into
858 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
860 /* But wait.. there's more. Design your standards by
861 * committee and you too can get a free iordy field to
862 * process. However its the speeds not the modes that
863 * are supported... Note drivers using the timing API
864 * will get this right anyway
868 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
871 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
872 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
874 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
878 * ata_port_queue_task - Queue port_task
879 * @ap: The ata_port to queue port_task for
881 * Schedule @fn(@data) for execution after @delay jiffies using
882 * port_task. There is one port_task per port and it's the
883 * user(low level driver)'s responsibility to make sure that only
884 * one task is active at any given time.
886 * libata core layer takes care of synchronization between
887 * port_task and EH. ata_port_queue_task() may be ignored for EH
891 * Inherited from caller.
893 void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
898 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
901 PREPARE_WORK(&ap->port_task, fn, data);
904 rc = queue_work(ata_wq, &ap->port_task);
906 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
908 /* rc == 0 means that another user is using port task */
913 * ata_port_flush_task - Flush port_task
914 * @ap: The ata_port to flush port_task for
916 * After this function completes, port_task is guranteed not to
917 * be running or scheduled.
920 * Kernel thread context (may sleep)
922 void ata_port_flush_task(struct ata_port *ap)
928 spin_lock_irqsave(&ap->host_set->lock, flags);
929 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
930 spin_unlock_irqrestore(&ap->host_set->lock, flags);
932 DPRINTK("flush #1\n");
933 flush_workqueue(ata_wq);
936 * At this point, if a task is running, it's guaranteed to see
937 * the FLUSH flag; thus, it will never queue pio tasks again.
940 if (!cancel_delayed_work(&ap->port_task)) {
941 DPRINTK("flush #2\n");
942 flush_workqueue(ata_wq);
945 spin_lock_irqsave(&ap->host_set->lock, flags);
946 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
947 spin_unlock_irqrestore(&ap->host_set->lock, flags);
952 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
954 struct completion *waiting = qc->private_data;
956 qc->ap->ops->tf_read(qc->ap, &qc->tf);
961 * ata_exec_internal - execute libata internal command
962 * @ap: Port to which the command is sent
963 * @dev: Device to which the command is sent
964 * @tf: Taskfile registers for the command and the result
965 * @dma_dir: Data tranfer direction of the command
966 * @buf: Data buffer of the command
967 * @buflen: Length of data buffer
969 * Executes libata internal command with timeout. @tf contains
970 * command on entry and result on return. Timeout and error
971 * conditions are reported via return value. No recovery action
972 * is taken after a command times out. It's caller's duty to
973 * clean up after timeout.
976 * None. Should be called with kernel context, might sleep.
980 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
981 struct ata_taskfile *tf,
982 int dma_dir, void *buf, unsigned int buflen)
984 u8 command = tf->command;
985 struct ata_queued_cmd *qc;
986 DECLARE_COMPLETION(wait);
988 unsigned int err_mask;
990 spin_lock_irqsave(&ap->host_set->lock, flags);
992 qc = ata_qc_new_init(ap, dev);
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_PORT_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, 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 */
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 i, rc, found = 0;
1372 struct ata_device *dev;
1376 /* reset and determine device classes */
1377 for (i = 0; i < ATA_MAX_DEVICES; i++)
1378 classes[i] = ATA_DEV_UNKNOWN;
1380 if (ap->ops->probe_reset) {
1381 rc = ap->ops->probe_reset(ap, classes);
1383 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1387 ap->ops->phy_reset(ap);
1389 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1390 for (i = 0; i < ATA_MAX_DEVICES; i++)
1391 classes[i] = ap->device[i].class;
1396 for (i = 0; i < ATA_MAX_DEVICES; i++)
1397 if (classes[i] == ATA_DEV_UNKNOWN)
1398 classes[i] = ATA_DEV_NONE;
1400 /* read IDENTIFY page and configure devices */
1401 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1402 dev = &ap->device[i];
1403 dev->class = classes[i];
1405 if (!ata_dev_enabled(dev))
1408 WARN_ON(dev->id != NULL);
1409 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1410 dev->class = ATA_DEV_NONE;
1414 if (ata_dev_configure(ap, dev, 1)) {
1415 ata_dev_disable(ap, dev);
1422 /* configure transfer mode */
1423 if (ap->ops->set_mode) {
1424 /* FIXME: make ->set_mode handle no device case and
1425 * return error code and failing device on failure as
1426 * ata_set_mode() does.
1429 ap->ops->set_mode(ap);
1432 while (ata_set_mode(ap, &dev))
1433 ata_dev_disable(ap, dev);
1436 for (i = 0; i < ATA_MAX_DEVICES; i++)
1437 if (ata_dev_enabled(&ap->device[i]))
1440 /* no device present, disable port */
1441 ata_port_disable(ap);
1442 ap->ops->port_disable(ap);
1447 * ata_port_probe - Mark port as enabled
1448 * @ap: Port for which we indicate enablement
1450 * Modify @ap data structure such that the system
1451 * thinks that the entire port is enabled.
1453 * LOCKING: host_set lock, or some other form of
1457 void ata_port_probe(struct ata_port *ap)
1459 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1463 * sata_print_link_status - Print SATA link status
1464 * @ap: SATA port to printk link status about
1466 * This function prints link speed and status of a SATA link.
1471 static void sata_print_link_status(struct ata_port *ap)
1475 if (!ap->ops->scr_read)
1478 sstatus = scr_read(ap, SCR_STATUS);
1480 if (sata_dev_present(ap)) {
1481 tmp = (sstatus >> 4) & 0xf;
1482 printk(KERN_INFO "ata%u: SATA link up %s (SStatus %X)\n",
1483 ap->id, sata_spd_string(tmp), sstatus);
1485 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1491 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1492 * @ap: SATA port associated with target SATA PHY.
1494 * This function issues commands to standard SATA Sxxx
1495 * PHY registers, to wake up the phy (and device), and
1496 * clear any reset condition.
1499 * PCI/etc. bus probe sem.
1502 void __sata_phy_reset(struct ata_port *ap)
1505 unsigned long timeout = jiffies + (HZ * 5);
1507 if (ap->flags & ATA_FLAG_SATA_RESET) {
1508 /* issue phy wake/reset */
1509 scr_write_flush(ap, SCR_CONTROL, 0x301);
1510 /* Couldn't find anything in SATA I/II specs, but
1511 * AHCI-1.1 10.4.2 says at least 1 ms. */
1514 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1516 /* wait for phy to become ready, if necessary */
1519 sstatus = scr_read(ap, SCR_STATUS);
1520 if ((sstatus & 0xf) != 1)
1522 } while (time_before(jiffies, timeout));
1524 /* print link status */
1525 sata_print_link_status(ap);
1527 /* TODO: phy layer with polling, timeouts, etc. */
1528 if (sata_dev_present(ap))
1531 ata_port_disable(ap);
1533 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1536 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1537 ata_port_disable(ap);
1541 ap->cbl = ATA_CBL_SATA;
1545 * sata_phy_reset - Reset SATA bus.
1546 * @ap: SATA port associated with target SATA PHY.
1548 * This function resets the SATA bus, and then probes
1549 * the bus for devices.
1552 * PCI/etc. bus probe sem.
1555 void sata_phy_reset(struct ata_port *ap)
1557 __sata_phy_reset(ap);
1558 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1564 * ata_dev_pair - return other device on cable
1568 * Obtain the other device on the same cable, or if none is
1569 * present NULL is returned
1572 struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
1574 struct ata_device *pair = &ap->device[1 - adev->devno];
1575 if (!ata_dev_enabled(pair))
1581 * ata_port_disable - Disable port.
1582 * @ap: Port to be disabled.
1584 * Modify @ap data structure such that the system
1585 * thinks that the entire port is disabled, and should
1586 * never attempt to probe or communicate with devices
1589 * LOCKING: host_set lock, or some other form of
1593 void ata_port_disable(struct ata_port *ap)
1595 ap->device[0].class = ATA_DEV_NONE;
1596 ap->device[1].class = ATA_DEV_NONE;
1597 ap->flags |= ATA_FLAG_PORT_DISABLED;
1601 * ata_down_sata_spd_limit - adjust SATA spd limit downward
1602 * @ap: Port to adjust SATA spd limit for
1604 * Adjust SATA spd limit of @ap downward. Note that this
1605 * function only adjusts the limit. The change must be applied
1606 * using ata_set_sata_spd().
1609 * Inherited from caller.
1612 * 0 on success, negative errno on failure
1614 static int ata_down_sata_spd_limit(struct ata_port *ap)
1619 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1622 mask = ap->sata_spd_limit;
1625 highbit = fls(mask) - 1;
1626 mask &= ~(1 << highbit);
1628 spd = (scr_read(ap, SCR_STATUS) >> 4) & 0xf;
1632 mask &= (1 << spd) - 1;
1636 ap->sata_spd_limit = mask;
1638 printk(KERN_WARNING "ata%u: limiting SATA link speed to %s\n",
1639 ap->id, sata_spd_string(fls(mask)));
1644 static int __ata_set_sata_spd_needed(struct ata_port *ap, u32 *scontrol)
1648 if (ap->sata_spd_limit == UINT_MAX)
1651 limit = fls(ap->sata_spd_limit);
1653 spd = (*scontrol >> 4) & 0xf;
1654 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1656 return spd != limit;
1660 * ata_set_sata_spd_needed - is SATA spd configuration needed
1661 * @ap: Port in question
1663 * Test whether the spd limit in SControl matches
1664 * @ap->sata_spd_limit. This function is used to determine
1665 * whether hardreset is necessary to apply SATA spd
1669 * Inherited from caller.
1672 * 1 if SATA spd configuration is needed, 0 otherwise.
1674 static int ata_set_sata_spd_needed(struct ata_port *ap)
1678 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1681 scontrol = scr_read(ap, SCR_CONTROL);
1683 return __ata_set_sata_spd_needed(ap, &scontrol);
1687 * ata_set_sata_spd - set SATA spd according to spd limit
1688 * @ap: Port to set SATA spd for
1690 * Set SATA spd of @ap according to sata_spd_limit.
1693 * Inherited from caller.
1696 * 0 if spd doesn't need to be changed, 1 if spd has been
1697 * changed. -EOPNOTSUPP if SCR registers are inaccessible.
1699 static int ata_set_sata_spd(struct ata_port *ap)
1703 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1706 scontrol = scr_read(ap, SCR_CONTROL);
1707 if (!__ata_set_sata_spd_needed(ap, &scontrol))
1710 scr_write(ap, SCR_CONTROL, scontrol);
1715 * This mode timing computation functionality is ported over from
1716 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1719 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1720 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1721 * for PIO 5, which is a nonstandard extension and UDMA6, which
1722 * is currently supported only by Maxtor drives.
1725 static const struct ata_timing ata_timing[] = {
1727 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1728 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1729 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1730 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1732 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1733 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1734 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1736 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1738 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1739 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1740 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1742 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1743 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1744 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1746 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1747 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1748 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1750 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1751 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1752 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1754 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1759 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1760 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1762 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1764 q->setup = EZ(t->setup * 1000, T);
1765 q->act8b = EZ(t->act8b * 1000, T);
1766 q->rec8b = EZ(t->rec8b * 1000, T);
1767 q->cyc8b = EZ(t->cyc8b * 1000, T);
1768 q->active = EZ(t->active * 1000, T);
1769 q->recover = EZ(t->recover * 1000, T);
1770 q->cycle = EZ(t->cycle * 1000, T);
1771 q->udma = EZ(t->udma * 1000, UT);
1774 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1775 struct ata_timing *m, unsigned int what)
1777 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1778 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1779 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1780 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1781 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1782 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1783 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1784 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1787 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1789 const struct ata_timing *t;
1791 for (t = ata_timing; t->mode != speed; t++)
1792 if (t->mode == 0xFF)
1797 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1798 struct ata_timing *t, int T, int UT)
1800 const struct ata_timing *s;
1801 struct ata_timing p;
1807 if (!(s = ata_timing_find_mode(speed)))
1810 memcpy(t, s, sizeof(*s));
1813 * If the drive is an EIDE drive, it can tell us it needs extended
1814 * PIO/MW_DMA cycle timing.
1817 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1818 memset(&p, 0, sizeof(p));
1819 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1820 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1821 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1822 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1823 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1825 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1829 * Convert the timing to bus clock counts.
1832 ata_timing_quantize(t, t, T, UT);
1835 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1836 * S.M.A.R.T * and some other commands. We have to ensure that the
1837 * DMA cycle timing is slower/equal than the fastest PIO timing.
1840 if (speed > XFER_PIO_4) {
1841 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1842 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1846 * Lengthen active & recovery time so that cycle time is correct.
1849 if (t->act8b + t->rec8b < t->cyc8b) {
1850 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1851 t->rec8b = t->cyc8b - t->act8b;
1854 if (t->active + t->recover < t->cycle) {
1855 t->active += (t->cycle - (t->active + t->recover)) / 2;
1856 t->recover = t->cycle - t->active;
1862 static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1864 unsigned int err_mask;
1867 if (dev->xfer_shift == ATA_SHIFT_PIO)
1868 dev->flags |= ATA_DFLAG_PIO;
1870 err_mask = ata_dev_set_xfermode(ap, dev);
1873 "ata%u: failed to set xfermode (err_mask=0x%x)\n",
1878 rc = ata_dev_revalidate(ap, dev, 0);
1881 "ata%u: failed to revalidate after set xfermode\n",
1886 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1887 dev->xfer_shift, (int)dev->xfer_mode);
1889 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1891 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
1896 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1897 * @ap: port on which timings will be programmed
1898 * @r_failed_dev: out paramter for failed device
1900 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
1901 * ata_set_mode() fails, pointer to the failing device is
1902 * returned in @r_failed_dev.
1905 * PCI/etc. bus probe sem.
1908 * 0 on success, negative errno otherwise
1910 static int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
1912 struct ata_device *dev;
1913 int i, rc = 0, used_dma = 0, found = 0;
1915 /* step 1: calculate xfer_mask */
1916 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1917 unsigned int pio_mask, dma_mask;
1919 dev = &ap->device[i];
1921 if (!ata_dev_enabled(dev))
1924 ata_dev_xfermask(ap, dev);
1926 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
1927 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
1928 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
1929 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
1938 /* step 2: always set host PIO timings */
1939 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1940 dev = &ap->device[i];
1941 if (!ata_dev_enabled(dev))
1944 if (!dev->pio_mode) {
1945 printk(KERN_WARNING "ata%u: dev %u no PIO support\n",
1946 ap->id, dev->devno);
1951 dev->xfer_mode = dev->pio_mode;
1952 dev->xfer_shift = ATA_SHIFT_PIO;
1953 if (ap->ops->set_piomode)
1954 ap->ops->set_piomode(ap, dev);
1957 /* step 3: set host DMA timings */
1958 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1959 dev = &ap->device[i];
1961 if (!ata_dev_enabled(dev) || !dev->dma_mode)
1964 dev->xfer_mode = dev->dma_mode;
1965 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1966 if (ap->ops->set_dmamode)
1967 ap->ops->set_dmamode(ap, dev);
1970 /* step 4: update devices' xfer mode */
1971 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1972 dev = &ap->device[i];
1974 if (!ata_dev_enabled(dev))
1977 rc = ata_dev_set_mode(ap, dev);
1982 /* Record simplex status. If we selected DMA then the other
1983 * host channels are not permitted to do so.
1985 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
1986 ap->host_set->simplex_claimed = 1;
1988 /* step5: chip specific finalisation */
1989 if (ap->ops->post_set_mode)
1990 ap->ops->post_set_mode(ap);
1994 *r_failed_dev = dev;
1999 * ata_tf_to_host - issue ATA taskfile to host controller
2000 * @ap: port to which command is being issued
2001 * @tf: ATA taskfile register set
2003 * Issues ATA taskfile register set to ATA host controller,
2004 * with proper synchronization with interrupt handler and
2008 * spin_lock_irqsave(host_set lock)
2011 static inline void ata_tf_to_host(struct ata_port *ap,
2012 const struct ata_taskfile *tf)
2014 ap->ops->tf_load(ap, tf);
2015 ap->ops->exec_command(ap, tf);
2019 * ata_busy_sleep - sleep until BSY clears, or timeout
2020 * @ap: port containing status register to be polled
2021 * @tmout_pat: impatience timeout
2022 * @tmout: overall timeout
2024 * Sleep until ATA Status register bit BSY clears,
2025 * or a timeout occurs.
2030 unsigned int ata_busy_sleep (struct ata_port *ap,
2031 unsigned long tmout_pat, unsigned long tmout)
2033 unsigned long timer_start, timeout;
2036 status = ata_busy_wait(ap, ATA_BUSY, 300);
2037 timer_start = jiffies;
2038 timeout = timer_start + tmout_pat;
2039 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2041 status = ata_busy_wait(ap, ATA_BUSY, 3);
2044 if (status & ATA_BUSY)
2045 printk(KERN_WARNING "ata%u is slow to respond, "
2046 "please be patient\n", ap->id);
2048 timeout = timer_start + tmout;
2049 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2051 status = ata_chk_status(ap);
2054 if (status & ATA_BUSY) {
2055 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2056 ap->id, tmout / HZ);
2063 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2065 struct ata_ioports *ioaddr = &ap->ioaddr;
2066 unsigned int dev0 = devmask & (1 << 0);
2067 unsigned int dev1 = devmask & (1 << 1);
2068 unsigned long timeout;
2070 /* if device 0 was found in ata_devchk, wait for its
2074 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2076 /* if device 1 was found in ata_devchk, wait for
2077 * register access, then wait for BSY to clear
2079 timeout = jiffies + ATA_TMOUT_BOOT;
2083 ap->ops->dev_select(ap, 1);
2084 if (ap->flags & ATA_FLAG_MMIO) {
2085 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2086 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2088 nsect = inb(ioaddr->nsect_addr);
2089 lbal = inb(ioaddr->lbal_addr);
2091 if ((nsect == 1) && (lbal == 1))
2093 if (time_after(jiffies, timeout)) {
2097 msleep(50); /* give drive a breather */
2100 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2102 /* is all this really necessary? */
2103 ap->ops->dev_select(ap, 0);
2105 ap->ops->dev_select(ap, 1);
2107 ap->ops->dev_select(ap, 0);
2110 static unsigned int ata_bus_softreset(struct ata_port *ap,
2111 unsigned int devmask)
2113 struct ata_ioports *ioaddr = &ap->ioaddr;
2115 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2117 /* software reset. causes dev0 to be selected */
2118 if (ap->flags & ATA_FLAG_MMIO) {
2119 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2120 udelay(20); /* FIXME: flush */
2121 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2122 udelay(20); /* FIXME: flush */
2123 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2125 outb(ap->ctl, ioaddr->ctl_addr);
2127 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2129 outb(ap->ctl, ioaddr->ctl_addr);
2132 /* spec mandates ">= 2ms" before checking status.
2133 * We wait 150ms, because that was the magic delay used for
2134 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2135 * between when the ATA command register is written, and then
2136 * status is checked. Because waiting for "a while" before
2137 * checking status is fine, post SRST, we perform this magic
2138 * delay here as well.
2140 * Old drivers/ide uses the 2mS rule and then waits for ready
2144 /* Before we perform post reset processing we want to see if
2145 * the bus shows 0xFF because the odd clown forgets the D7
2146 * pulldown resistor.
2148 if (ata_check_status(ap) == 0xFF)
2149 return AC_ERR_OTHER;
2151 ata_bus_post_reset(ap, devmask);
2157 * ata_bus_reset - reset host port and associated ATA channel
2158 * @ap: port to reset
2160 * This is typically the first time we actually start issuing
2161 * commands to the ATA channel. We wait for BSY to clear, then
2162 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2163 * result. Determine what devices, if any, are on the channel
2164 * by looking at the device 0/1 error register. Look at the signature
2165 * stored in each device's taskfile registers, to determine if
2166 * the device is ATA or ATAPI.
2169 * PCI/etc. bus probe sem.
2170 * Obtains host_set lock.
2173 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2176 void ata_bus_reset(struct ata_port *ap)
2178 struct ata_ioports *ioaddr = &ap->ioaddr;
2179 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2181 unsigned int dev0, dev1 = 0, devmask = 0;
2183 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2185 /* determine if device 0/1 are present */
2186 if (ap->flags & ATA_FLAG_SATA_RESET)
2189 dev0 = ata_devchk(ap, 0);
2191 dev1 = ata_devchk(ap, 1);
2195 devmask |= (1 << 0);
2197 devmask |= (1 << 1);
2199 /* select device 0 again */
2200 ap->ops->dev_select(ap, 0);
2202 /* issue bus reset */
2203 if (ap->flags & ATA_FLAG_SRST)
2204 if (ata_bus_softreset(ap, devmask))
2208 * determine by signature whether we have ATA or ATAPI devices
2210 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
2211 if ((slave_possible) && (err != 0x81))
2212 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
2214 /* re-enable interrupts */
2215 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2218 /* is double-select really necessary? */
2219 if (ap->device[1].class != ATA_DEV_NONE)
2220 ap->ops->dev_select(ap, 1);
2221 if (ap->device[0].class != ATA_DEV_NONE)
2222 ap->ops->dev_select(ap, 0);
2224 /* if no devices were detected, disable this port */
2225 if ((ap->device[0].class == ATA_DEV_NONE) &&
2226 (ap->device[1].class == ATA_DEV_NONE))
2229 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2230 /* set up device control for ATA_FLAG_SATA_RESET */
2231 if (ap->flags & ATA_FLAG_MMIO)
2232 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2234 outb(ap->ctl, ioaddr->ctl_addr);
2241 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2242 ap->ops->port_disable(ap);
2247 static int sata_phy_resume(struct ata_port *ap)
2249 unsigned long timeout = jiffies + (HZ * 5);
2250 u32 scontrol, sstatus;
2252 scontrol = scr_read(ap, SCR_CONTROL);
2253 scontrol = (scontrol & 0x0f0) | 0x300;
2254 scr_write_flush(ap, SCR_CONTROL, scontrol);
2256 /* Wait for phy to become ready, if necessary. */
2259 sstatus = scr_read(ap, SCR_STATUS);
2260 if ((sstatus & 0xf) != 1)
2262 } while (time_before(jiffies, timeout));
2268 * ata_std_probeinit - initialize probing
2269 * @ap: port to be probed
2271 * @ap is about to be probed. Initialize it. This function is
2272 * to be used as standard callback for ata_drive_probe_reset().
2274 * NOTE!!! Do not use this function as probeinit if a low level
2275 * driver implements only hardreset. Just pass NULL as probeinit
2276 * in that case. Using this function is probably okay but doing
2277 * so makes reset sequence different from the original
2278 * ->phy_reset implementation and Jeff nervous. :-P
2280 void ata_std_probeinit(struct ata_port *ap)
2282 if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
2285 sata_phy_resume(ap);
2287 spd = (scr_read(ap, SCR_CONTROL) & 0xf0) >> 4;
2289 ap->sata_spd_limit &= (1 << spd) - 1;
2291 if (sata_dev_present(ap))
2292 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2297 * ata_std_softreset - reset host port via ATA SRST
2298 * @ap: port to reset
2299 * @verbose: fail verbosely
2300 * @classes: resulting classes of attached devices
2302 * Reset host port using ATA SRST. This function is to be used
2303 * as standard callback for ata_drive_*_reset() functions.
2306 * Kernel thread context (may sleep)
2309 * 0 on success, -errno otherwise.
2311 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2313 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2314 unsigned int devmask = 0, err_mask;
2319 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2320 classes[0] = ATA_DEV_NONE;
2324 /* determine if device 0/1 are present */
2325 if (ata_devchk(ap, 0))
2326 devmask |= (1 << 0);
2327 if (slave_possible && ata_devchk(ap, 1))
2328 devmask |= (1 << 1);
2330 /* select device 0 again */
2331 ap->ops->dev_select(ap, 0);
2333 /* issue bus reset */
2334 DPRINTK("about to softreset, devmask=%x\n", devmask);
2335 err_mask = ata_bus_softreset(ap, devmask);
2338 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2341 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2346 /* determine by signature whether we have ATA or ATAPI devices */
2347 classes[0] = ata_dev_try_classify(ap, 0, &err);
2348 if (slave_possible && err != 0x81)
2349 classes[1] = ata_dev_try_classify(ap, 1, &err);
2352 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2357 * sata_std_hardreset - reset host port via SATA phy reset
2358 * @ap: port to reset
2359 * @verbose: fail verbosely
2360 * @class: resulting class of attached device
2362 * SATA phy-reset host port using DET bits of SControl register.
2363 * This function is to be used as standard callback for
2364 * ata_drive_*_reset().
2367 * Kernel thread context (may sleep)
2370 * 0 on success, -errno otherwise.
2372 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2378 if (ata_set_sata_spd_needed(ap)) {
2379 /* SATA spec says nothing about how to reconfigure
2380 * spd. To be on the safe side, turn off phy during
2381 * reconfiguration. This works for at least ICH7 AHCI
2384 scontrol = scr_read(ap, SCR_CONTROL);
2385 scontrol = (scontrol & 0x0f0) | 0x302;
2386 scr_write_flush(ap, SCR_CONTROL, scontrol);
2388 ata_set_sata_spd(ap);
2391 /* issue phy wake/reset */
2392 scontrol = scr_read(ap, SCR_CONTROL);
2393 scontrol = (scontrol & 0x0f0) | 0x301;
2394 scr_write_flush(ap, SCR_CONTROL, scontrol);
2396 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
2397 * 10.4.2 says at least 1 ms.
2401 /* bring phy back */
2402 sata_phy_resume(ap);
2404 /* TODO: phy layer with polling, timeouts, etc. */
2405 if (!sata_dev_present(ap)) {
2406 *class = ATA_DEV_NONE;
2407 DPRINTK("EXIT, link offline\n");
2411 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2413 printk(KERN_ERR "ata%u: COMRESET failed "
2414 "(device not ready)\n", ap->id);
2416 DPRINTK("EXIT, device not ready\n");
2420 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2422 *class = ata_dev_try_classify(ap, 0, NULL);
2424 DPRINTK("EXIT, class=%u\n", *class);
2429 * ata_std_postreset - standard postreset callback
2430 * @ap: the target ata_port
2431 * @classes: classes of attached devices
2433 * This function is invoked after a successful reset. Note that
2434 * the device might have been reset more than once using
2435 * different reset methods before postreset is invoked.
2437 * This function is to be used as standard callback for
2438 * ata_drive_*_reset().
2441 * Kernel thread context (may sleep)
2443 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2447 /* set cable type if it isn't already set */
2448 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2449 ap->cbl = ATA_CBL_SATA;
2451 /* print link status */
2452 if (ap->cbl == ATA_CBL_SATA)
2453 sata_print_link_status(ap);
2455 /* re-enable interrupts */
2456 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2459 /* is double-select really necessary? */
2460 if (classes[0] != ATA_DEV_NONE)
2461 ap->ops->dev_select(ap, 1);
2462 if (classes[1] != ATA_DEV_NONE)
2463 ap->ops->dev_select(ap, 0);
2465 /* bail out if no device is present */
2466 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2467 DPRINTK("EXIT, no device\n");
2471 /* set up device control */
2472 if (ap->ioaddr.ctl_addr) {
2473 if (ap->flags & ATA_FLAG_MMIO)
2474 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2476 outb(ap->ctl, ap->ioaddr.ctl_addr);
2483 * ata_std_probe_reset - standard probe reset method
2484 * @ap: prot to perform probe-reset
2485 * @classes: resulting classes of attached devices
2487 * The stock off-the-shelf ->probe_reset method.
2490 * Kernel thread context (may sleep)
2493 * 0 on success, -errno otherwise.
2495 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2497 ata_reset_fn_t hardreset;
2500 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2501 hardreset = sata_std_hardreset;
2503 return ata_drive_probe_reset(ap, ata_std_probeinit,
2504 ata_std_softreset, hardreset,
2505 ata_std_postreset, classes);
2508 static int ata_do_reset(struct ata_port *ap,
2509 ata_reset_fn_t reset, ata_postreset_fn_t postreset,
2510 int verbose, unsigned int *classes)
2514 for (i = 0; i < ATA_MAX_DEVICES; i++)
2515 classes[i] = ATA_DEV_UNKNOWN;
2517 rc = reset(ap, verbose, classes);
2521 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2522 * is complete and convert all ATA_DEV_UNKNOWN to
2525 for (i = 0; i < ATA_MAX_DEVICES; i++)
2526 if (classes[i] != ATA_DEV_UNKNOWN)
2529 if (i < ATA_MAX_DEVICES)
2530 for (i = 0; i < ATA_MAX_DEVICES; i++)
2531 if (classes[i] == ATA_DEV_UNKNOWN)
2532 classes[i] = ATA_DEV_NONE;
2535 postreset(ap, classes);
2541 * ata_drive_probe_reset - Perform probe reset with given methods
2542 * @ap: port to reset
2543 * @probeinit: probeinit method (can be NULL)
2544 * @softreset: softreset method (can be NULL)
2545 * @hardreset: hardreset method (can be NULL)
2546 * @postreset: postreset method (can be NULL)
2547 * @classes: resulting classes of attached devices
2549 * Reset the specified port and classify attached devices using
2550 * given methods. This function prefers softreset but tries all
2551 * possible reset sequences to reset and classify devices. This
2552 * function is intended to be used for constructing ->probe_reset
2553 * callback by low level drivers.
2555 * Reset methods should follow the following rules.
2557 * - Return 0 on sucess, -errno on failure.
2558 * - If classification is supported, fill classes[] with
2559 * recognized class codes.
2560 * - If classification is not supported, leave classes[] alone.
2561 * - If verbose is non-zero, print error message on failure;
2562 * otherwise, shut up.
2565 * Kernel thread context (may sleep)
2568 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2569 * if classification fails, and any error code from reset
2572 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2573 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2574 ata_postreset_fn_t postreset, unsigned int *classes)
2581 if (softreset && !ata_set_sata_spd_needed(ap)) {
2582 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
2583 if (rc == 0 && classes[0] != ATA_DEV_UNKNOWN)
2585 printk(KERN_INFO "ata%u: softreset failed, will try "
2586 "hardreset in 5 secs\n", ap->id);
2594 rc = ata_do_reset(ap, hardreset, postreset, 0, classes);
2596 if (classes[0] != ATA_DEV_UNKNOWN)
2601 if (ata_down_sata_spd_limit(ap))
2604 printk(KERN_INFO "ata%u: hardreset failed, will retry "
2605 "in 5 secs\n", ap->id);
2610 printk(KERN_INFO "ata%u: hardreset succeeded without "
2611 "classification, will retry softreset in 5 secs\n",
2615 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
2619 if (rc == 0 && classes[0] == ATA_DEV_UNKNOWN)
2625 * ata_dev_same_device - Determine whether new ID matches configured device
2626 * @ap: port on which the device to compare against resides
2627 * @dev: device to compare against
2628 * @new_class: class of the new device
2629 * @new_id: IDENTIFY page of the new device
2631 * Compare @new_class and @new_id against @dev and determine
2632 * whether @dev is the device indicated by @new_class and
2639 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2641 static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2642 unsigned int new_class, const u16 *new_id)
2644 const u16 *old_id = dev->id;
2645 unsigned char model[2][41], serial[2][21];
2648 if (dev->class != new_class) {
2650 "ata%u: dev %u class mismatch %d != %d\n",
2651 ap->id, dev->devno, dev->class, new_class);
2655 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2656 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2657 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2658 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2659 new_n_sectors = ata_id_n_sectors(new_id);
2661 if (strcmp(model[0], model[1])) {
2663 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2664 ap->id, dev->devno, model[0], model[1]);
2668 if (strcmp(serial[0], serial[1])) {
2670 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2671 ap->id, dev->devno, serial[0], serial[1]);
2675 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2677 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2678 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2679 (unsigned long long)new_n_sectors);
2687 * ata_dev_revalidate - Revalidate ATA device
2688 * @ap: port on which the device to revalidate resides
2689 * @dev: device to revalidate
2690 * @post_reset: is this revalidation after reset?
2692 * Re-read IDENTIFY page and make sure @dev is still attached to
2696 * Kernel thread context (may sleep)
2699 * 0 on success, negative errno otherwise
2701 int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2708 if (!ata_dev_enabled(dev))
2714 /* allocate & read ID data */
2715 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2719 /* is the device still there? */
2720 if (!ata_dev_same_device(ap, dev, class, id)) {
2728 /* configure device according to the new ID */
2729 return ata_dev_configure(ap, dev, 0);
2732 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2733 ap->id, dev->devno, rc);
2738 static const char * const ata_dma_blacklist [] = {
2739 "WDC AC11000H", NULL,
2740 "WDC AC22100H", NULL,
2741 "WDC AC32500H", NULL,
2742 "WDC AC33100H", NULL,
2743 "WDC AC31600H", NULL,
2744 "WDC AC32100H", "24.09P07",
2745 "WDC AC23200L", "21.10N21",
2746 "Compaq CRD-8241B", NULL,
2751 "SanDisk SDP3B", NULL,
2752 "SanDisk SDP3B-64", NULL,
2753 "SANYO CD-ROM CRD", NULL,
2754 "HITACHI CDR-8", NULL,
2755 "HITACHI CDR-8335", NULL,
2756 "HITACHI CDR-8435", NULL,
2757 "Toshiba CD-ROM XM-6202B", NULL,
2758 "TOSHIBA CD-ROM XM-1702BC", NULL,
2760 "E-IDE CD-ROM CR-840", NULL,
2761 "CD-ROM Drive/F5A", NULL,
2762 "WPI CDD-820", NULL,
2763 "SAMSUNG CD-ROM SC-148C", NULL,
2764 "SAMSUNG CD-ROM SC", NULL,
2765 "SanDisk SDP3B-64", NULL,
2766 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2767 "_NEC DV5800A", NULL,
2768 "SAMSUNG CD-ROM SN-124", "N001"
2771 static int ata_strim(char *s, size_t len)
2773 len = strnlen(s, len);
2775 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2776 while ((len > 0) && (s[len - 1] == ' ')) {
2783 static int ata_dma_blacklisted(const struct ata_device *dev)
2785 unsigned char model_num[40];
2786 unsigned char model_rev[16];
2787 unsigned int nlen, rlen;
2790 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2792 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2794 nlen = ata_strim(model_num, sizeof(model_num));
2795 rlen = ata_strim(model_rev, sizeof(model_rev));
2797 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2798 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2799 if (ata_dma_blacklist[i+1] == NULL)
2801 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2809 * ata_dev_xfermask - Compute supported xfermask of the given device
2810 * @ap: Port on which the device to compute xfermask for resides
2811 * @dev: Device to compute xfermask for
2813 * Compute supported xfermask of @dev and store it in
2814 * dev->*_mask. This function is responsible for applying all
2815 * known limits including host controller limits, device
2818 * FIXME: The current implementation limits all transfer modes to
2819 * the fastest of the lowested device on the port. This is not
2820 * required on most controllers.
2825 static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
2827 struct ata_host_set *hs = ap->host_set;
2828 unsigned long xfer_mask;
2831 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2834 /* FIXME: Use port-wide xfermask for now */
2835 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2836 struct ata_device *d = &ap->device[i];
2837 if (!ata_dev_enabled(d))
2839 xfer_mask &= ata_pack_xfermask(d->pio_mask, d->mwdma_mask,
2841 xfer_mask &= ata_id_xfermask(d->id);
2842 if (ata_dma_blacklisted(d))
2843 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2844 /* Apply cable rule here. Don't apply it early because when
2845 we handle hot plug the cable type can itself change */
2846 if (ap->cbl == ATA_CBL_PATA40)
2847 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
2850 if (ata_dma_blacklisted(dev))
2851 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2852 "disabling DMA\n", ap->id, dev->devno);
2854 if (hs->flags & ATA_HOST_SIMPLEX) {
2855 if (hs->simplex_claimed)
2856 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2858 if (ap->ops->mode_filter)
2859 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
2861 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2866 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2867 * @ap: Port associated with device @dev
2868 * @dev: Device to which command will be sent
2870 * Issue SET FEATURES - XFER MODE command to device @dev
2874 * PCI/etc. bus probe sem.
2877 * 0 on success, AC_ERR_* mask otherwise.
2880 static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
2881 struct ata_device *dev)
2883 struct ata_taskfile tf;
2884 unsigned int err_mask;
2886 /* set up set-features taskfile */
2887 DPRINTK("set features - xfer mode\n");
2889 ata_tf_init(ap, &tf, dev->devno);
2890 tf.command = ATA_CMD_SET_FEATURES;
2891 tf.feature = SETFEATURES_XFER;
2892 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2893 tf.protocol = ATA_PROT_NODATA;
2894 tf.nsect = dev->xfer_mode;
2896 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2898 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2903 * ata_dev_init_params - Issue INIT DEV PARAMS command
2904 * @ap: Port associated with device @dev
2905 * @dev: Device to which command will be sent
2908 * Kernel thread context (may sleep)
2911 * 0 on success, AC_ERR_* mask otherwise.
2914 static unsigned int ata_dev_init_params(struct ata_port *ap,
2915 struct ata_device *dev,
2919 struct ata_taskfile tf;
2920 unsigned int err_mask;
2922 /* Number of sectors per track 1-255. Number of heads 1-16 */
2923 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2924 return AC_ERR_INVALID;
2926 /* set up init dev params taskfile */
2927 DPRINTK("init dev params \n");
2929 ata_tf_init(ap, &tf, dev->devno);
2930 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2931 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2932 tf.protocol = ATA_PROT_NODATA;
2934 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2936 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2938 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2943 * ata_sg_clean - Unmap DMA memory associated with command
2944 * @qc: Command containing DMA memory to be released
2946 * Unmap all mapped DMA memory associated with this command.
2949 * spin_lock_irqsave(host_set lock)
2952 static void ata_sg_clean(struct ata_queued_cmd *qc)
2954 struct ata_port *ap = qc->ap;
2955 struct scatterlist *sg = qc->__sg;
2956 int dir = qc->dma_dir;
2957 void *pad_buf = NULL;
2959 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2960 WARN_ON(sg == NULL);
2962 if (qc->flags & ATA_QCFLAG_SINGLE)
2963 WARN_ON(qc->n_elem > 1);
2965 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2967 /* if we padded the buffer out to 32-bit bound, and data
2968 * xfer direction is from-device, we must copy from the
2969 * pad buffer back into the supplied buffer
2971 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2972 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2974 if (qc->flags & ATA_QCFLAG_SG) {
2976 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
2977 /* restore last sg */
2978 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2980 struct scatterlist *psg = &qc->pad_sgent;
2981 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2982 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2983 kunmap_atomic(addr, KM_IRQ0);
2987 dma_unmap_single(ap->dev,
2988 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2991 sg->length += qc->pad_len;
2993 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2994 pad_buf, qc->pad_len);
2997 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3002 * ata_fill_sg - Fill PCI IDE PRD table
3003 * @qc: Metadata associated with taskfile to be transferred
3005 * Fill PCI IDE PRD (scatter-gather) table with segments
3006 * associated with the current disk command.
3009 * spin_lock_irqsave(host_set lock)
3012 static void ata_fill_sg(struct ata_queued_cmd *qc)
3014 struct ata_port *ap = qc->ap;
3015 struct scatterlist *sg;
3018 WARN_ON(qc->__sg == NULL);
3019 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
3022 ata_for_each_sg(sg, qc) {
3026 /* determine if physical DMA addr spans 64K boundary.
3027 * Note h/w doesn't support 64-bit, so we unconditionally
3028 * truncate dma_addr_t to u32.
3030 addr = (u32) sg_dma_address(sg);
3031 sg_len = sg_dma_len(sg);
3034 offset = addr & 0xffff;
3036 if ((offset + sg_len) > 0x10000)
3037 len = 0x10000 - offset;
3039 ap->prd[idx].addr = cpu_to_le32(addr);
3040 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3041 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3050 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3053 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3054 * @qc: Metadata associated with taskfile to check
3056 * Allow low-level driver to filter ATA PACKET commands, returning
3057 * a status indicating whether or not it is OK to use DMA for the
3058 * supplied PACKET command.
3061 * spin_lock_irqsave(host_set lock)
3063 * RETURNS: 0 when ATAPI DMA can be used
3066 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3068 struct ata_port *ap = qc->ap;
3069 int rc = 0; /* Assume ATAPI DMA is OK by default */
3071 if (ap->ops->check_atapi_dma)
3072 rc = ap->ops->check_atapi_dma(qc);
3077 * ata_qc_prep - Prepare taskfile for submission
3078 * @qc: Metadata associated with taskfile to be prepared
3080 * Prepare ATA taskfile for submission.
3083 * spin_lock_irqsave(host_set lock)
3085 void ata_qc_prep(struct ata_queued_cmd *qc)
3087 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3093 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3096 * ata_sg_init_one - Associate command with memory buffer
3097 * @qc: Command to be associated
3098 * @buf: Memory buffer
3099 * @buflen: Length of memory buffer, in bytes.
3101 * Initialize the data-related elements of queued_cmd @qc
3102 * to point to a single memory buffer, @buf of byte length @buflen.
3105 * spin_lock_irqsave(host_set lock)
3108 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3110 struct scatterlist *sg;
3112 qc->flags |= ATA_QCFLAG_SINGLE;
3114 memset(&qc->sgent, 0, sizeof(qc->sgent));
3115 qc->__sg = &qc->sgent;
3117 qc->orig_n_elem = 1;
3121 sg_init_one(sg, buf, buflen);
3125 * ata_sg_init - Associate command with scatter-gather table.
3126 * @qc: Command to be associated
3127 * @sg: Scatter-gather table.
3128 * @n_elem: Number of elements in s/g table.
3130 * Initialize the data-related elements of queued_cmd @qc
3131 * to point to a scatter-gather table @sg, containing @n_elem
3135 * spin_lock_irqsave(host_set lock)
3138 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3139 unsigned int n_elem)
3141 qc->flags |= ATA_QCFLAG_SG;
3143 qc->n_elem = n_elem;
3144 qc->orig_n_elem = n_elem;
3148 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3149 * @qc: Command with memory buffer to be mapped.
3151 * DMA-map the memory buffer associated with queued_cmd @qc.
3154 * spin_lock_irqsave(host_set lock)
3157 * Zero on success, negative on error.
3160 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3162 struct ata_port *ap = qc->ap;
3163 int dir = qc->dma_dir;
3164 struct scatterlist *sg = qc->__sg;
3165 dma_addr_t dma_address;
3168 /* we must lengthen transfers to end on a 32-bit boundary */
3169 qc->pad_len = sg->length & 3;
3171 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3172 struct scatterlist *psg = &qc->pad_sgent;
3174 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3176 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3178 if (qc->tf.flags & ATA_TFLAG_WRITE)
3179 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3182 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3183 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3185 sg->length -= qc->pad_len;
3186 if (sg->length == 0)
3189 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3190 sg->length, qc->pad_len);
3198 dma_address = dma_map_single(ap->dev, qc->buf_virt,
3200 if (dma_mapping_error(dma_address)) {
3202 sg->length += qc->pad_len;
3206 sg_dma_address(sg) = dma_address;
3207 sg_dma_len(sg) = sg->length;
3210 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3211 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3217 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3218 * @qc: Command with scatter-gather table to be mapped.
3220 * DMA-map the scatter-gather table associated with queued_cmd @qc.
3223 * spin_lock_irqsave(host_set lock)
3226 * Zero on success, negative on error.
3230 static int ata_sg_setup(struct ata_queued_cmd *qc)
3232 struct ata_port *ap = qc->ap;
3233 struct scatterlist *sg = qc->__sg;
3234 struct scatterlist *lsg = &sg[qc->n_elem - 1];
3235 int n_elem, pre_n_elem, dir, trim_sg = 0;
3237 VPRINTK("ENTER, ata%u\n", ap->id);
3238 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
3240 /* we must lengthen transfers to end on a 32-bit boundary */
3241 qc->pad_len = lsg->length & 3;
3243 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3244 struct scatterlist *psg = &qc->pad_sgent;
3245 unsigned int offset;
3247 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3249 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3252 * psg->page/offset are used to copy to-be-written
3253 * data in this function or read data in ata_sg_clean.
3255 offset = lsg->offset + lsg->length - qc->pad_len;
3256 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3257 psg->offset = offset_in_page(offset);
3259 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3260 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3261 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
3262 kunmap_atomic(addr, KM_IRQ0);
3265 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3266 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3268 lsg->length -= qc->pad_len;
3269 if (lsg->length == 0)
3272 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3273 qc->n_elem - 1, lsg->length, qc->pad_len);
3276 pre_n_elem = qc->n_elem;
3277 if (trim_sg && pre_n_elem)
3286 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
3288 /* restore last sg */
3289 lsg->length += qc->pad_len;
3293 DPRINTK("%d sg elements mapped\n", n_elem);
3296 qc->n_elem = n_elem;
3302 * ata_poll_qc_complete - turn irq back on and finish qc
3303 * @qc: Command to complete
3304 * @err_mask: ATA status register content
3307 * None. (grabs host lock)
3310 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
3312 struct ata_port *ap = qc->ap;
3313 unsigned long flags;
3315 spin_lock_irqsave(&ap->host_set->lock, flags);
3316 ap->flags &= ~ATA_FLAG_NOINTR;
3318 ata_qc_complete(qc);
3319 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3323 * ata_pio_poll - poll using PIO, depending on current state
3324 * @ap: the target ata_port
3327 * None. (executing in kernel thread context)
3330 * timeout value to use
3333 static unsigned long ata_pio_poll(struct ata_port *ap)
3335 struct ata_queued_cmd *qc;
3337 unsigned int poll_state = HSM_ST_UNKNOWN;
3338 unsigned int reg_state = HSM_ST_UNKNOWN;
3340 qc = ata_qc_from_tag(ap, ap->active_tag);
3341 WARN_ON(qc == NULL);
3343 switch (ap->hsm_task_state) {
3346 poll_state = HSM_ST_POLL;
3350 case HSM_ST_LAST_POLL:
3351 poll_state = HSM_ST_LAST_POLL;
3352 reg_state = HSM_ST_LAST;
3359 status = ata_chk_status(ap);
3360 if (status & ATA_BUSY) {
3361 if (time_after(jiffies, ap->pio_task_timeout)) {
3362 qc->err_mask |= AC_ERR_TIMEOUT;
3363 ap->hsm_task_state = HSM_ST_TMOUT;
3366 ap->hsm_task_state = poll_state;
3367 return ATA_SHORT_PAUSE;
3370 ap->hsm_task_state = reg_state;
3375 * ata_pio_complete - check if drive is busy or idle
3376 * @ap: the target ata_port
3379 * None. (executing in kernel thread context)
3382 * Non-zero if qc completed, zero otherwise.
3385 static int ata_pio_complete (struct ata_port *ap)
3387 struct ata_queued_cmd *qc;
3391 * This is purely heuristic. This is a fast path. Sometimes when
3392 * we enter, BSY will be cleared in a chk-status or two. If not,
3393 * the drive is probably seeking or something. Snooze for a couple
3394 * msecs, then chk-status again. If still busy, fall back to
3395 * HSM_ST_POLL state.
3397 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3398 if (drv_stat & ATA_BUSY) {
3400 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3401 if (drv_stat & ATA_BUSY) {
3402 ap->hsm_task_state = HSM_ST_LAST_POLL;
3403 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3408 qc = ata_qc_from_tag(ap, ap->active_tag);
3409 WARN_ON(qc == NULL);
3411 drv_stat = ata_wait_idle(ap);
3412 if (!ata_ok(drv_stat)) {
3413 qc->err_mask |= __ac_err_mask(drv_stat);
3414 ap->hsm_task_state = HSM_ST_ERR;
3418 ap->hsm_task_state = HSM_ST_IDLE;
3420 WARN_ON(qc->err_mask);
3421 ata_poll_qc_complete(qc);
3423 /* another command may start at this point */
3430 * swap_buf_le16 - swap halves of 16-bit words in place
3431 * @buf: Buffer to swap
3432 * @buf_words: Number of 16-bit words in buffer.
3434 * Swap halves of 16-bit words if needed to convert from
3435 * little-endian byte order to native cpu byte order, or
3439 * Inherited from caller.
3441 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3446 for (i = 0; i < buf_words; i++)
3447 buf[i] = le16_to_cpu(buf[i]);
3448 #endif /* __BIG_ENDIAN */
3452 * ata_mmio_data_xfer - Transfer data by MMIO
3453 * @ap: port to read/write
3455 * @buflen: buffer length
3456 * @write_data: read/write
3458 * Transfer data from/to the device data register by MMIO.
3461 * Inherited from caller.
3464 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3465 unsigned int buflen, int write_data)
3468 unsigned int words = buflen >> 1;
3469 u16 *buf16 = (u16 *) buf;
3470 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3472 /* Transfer multiple of 2 bytes */
3474 for (i = 0; i < words; i++)
3475 writew(le16_to_cpu(buf16[i]), mmio);
3477 for (i = 0; i < words; i++)
3478 buf16[i] = cpu_to_le16(readw(mmio));
3481 /* Transfer trailing 1 byte, if any. */
3482 if (unlikely(buflen & 0x01)) {
3483 u16 align_buf[1] = { 0 };
3484 unsigned char *trailing_buf = buf + buflen - 1;
3487 memcpy(align_buf, trailing_buf, 1);
3488 writew(le16_to_cpu(align_buf[0]), mmio);
3490 align_buf[0] = cpu_to_le16(readw(mmio));
3491 memcpy(trailing_buf, align_buf, 1);
3497 * ata_pio_data_xfer - Transfer data by PIO
3498 * @ap: port to read/write
3500 * @buflen: buffer length
3501 * @write_data: read/write
3503 * Transfer data from/to the device data register by PIO.
3506 * Inherited from caller.
3509 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3510 unsigned int buflen, int write_data)
3512 unsigned int words = buflen >> 1;
3514 /* Transfer multiple of 2 bytes */
3516 outsw(ap->ioaddr.data_addr, buf, words);
3518 insw(ap->ioaddr.data_addr, buf, words);
3520 /* Transfer trailing 1 byte, if any. */
3521 if (unlikely(buflen & 0x01)) {
3522 u16 align_buf[1] = { 0 };
3523 unsigned char *trailing_buf = buf + buflen - 1;
3526 memcpy(align_buf, trailing_buf, 1);
3527 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3529 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3530 memcpy(trailing_buf, align_buf, 1);
3536 * ata_data_xfer - Transfer data from/to the data register.
3537 * @ap: port to read/write
3539 * @buflen: buffer length
3540 * @do_write: read/write
3542 * Transfer data from/to the device data register.
3545 * Inherited from caller.
3548 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3549 unsigned int buflen, int do_write)
3551 /* Make the crap hardware pay the costs not the good stuff */
3552 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3553 unsigned long flags;
3554 local_irq_save(flags);
3555 if (ap->flags & ATA_FLAG_MMIO)
3556 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3558 ata_pio_data_xfer(ap, buf, buflen, do_write);
3559 local_irq_restore(flags);
3561 if (ap->flags & ATA_FLAG_MMIO)
3562 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3564 ata_pio_data_xfer(ap, buf, buflen, do_write);
3569 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3570 * @qc: Command on going
3572 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3575 * Inherited from caller.
3578 static void ata_pio_sector(struct ata_queued_cmd *qc)
3580 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3581 struct scatterlist *sg = qc->__sg;
3582 struct ata_port *ap = qc->ap;
3584 unsigned int offset;
3587 if (qc->cursect == (qc->nsect - 1))
3588 ap->hsm_task_state = HSM_ST_LAST;
3590 page = sg[qc->cursg].page;
3591 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3593 /* get the current page and offset */
3594 page = nth_page(page, (offset >> PAGE_SHIFT));
3595 offset %= PAGE_SIZE;
3597 buf = kmap(page) + offset;
3602 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3607 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3609 /* do the actual data transfer */
3610 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3611 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3617 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3618 * @qc: Command on going
3619 * @bytes: number of bytes
3621 * Transfer Transfer data from/to the ATAPI device.
3624 * Inherited from caller.
3628 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3630 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3631 struct scatterlist *sg = qc->__sg;
3632 struct ata_port *ap = qc->ap;
3635 unsigned int offset, count;
3637 if (qc->curbytes + bytes >= qc->nbytes)
3638 ap->hsm_task_state = HSM_ST_LAST;
3641 if (unlikely(qc->cursg >= qc->n_elem)) {
3643 * The end of qc->sg is reached and the device expects
3644 * more data to transfer. In order not to overrun qc->sg
3645 * and fulfill length specified in the byte count register,
3646 * - for read case, discard trailing data from the device
3647 * - for write case, padding zero data to the device
3649 u16 pad_buf[1] = { 0 };
3650 unsigned int words = bytes >> 1;
3653 if (words) /* warning if bytes > 1 */
3654 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3657 for (i = 0; i < words; i++)
3658 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3660 ap->hsm_task_state = HSM_ST_LAST;
3664 sg = &qc->__sg[qc->cursg];
3667 offset = sg->offset + qc->cursg_ofs;
3669 /* get the current page and offset */
3670 page = nth_page(page, (offset >> PAGE_SHIFT));
3671 offset %= PAGE_SIZE;
3673 /* don't overrun current sg */
3674 count = min(sg->length - qc->cursg_ofs, bytes);
3676 /* don't cross page boundaries */
3677 count = min(count, (unsigned int)PAGE_SIZE - offset);
3679 buf = kmap(page) + offset;
3682 qc->curbytes += count;
3683 qc->cursg_ofs += count;
3685 if (qc->cursg_ofs == sg->length) {
3690 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3692 /* do the actual data transfer */
3693 ata_data_xfer(ap, buf, count, do_write);
3702 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3703 * @qc: Command on going
3705 * Transfer Transfer data from/to the ATAPI device.
3708 * Inherited from caller.
3711 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3713 struct ata_port *ap = qc->ap;
3714 struct ata_device *dev = qc->dev;
3715 unsigned int ireason, bc_lo, bc_hi, bytes;
3716 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3718 ap->ops->tf_read(ap, &qc->tf);
3719 ireason = qc->tf.nsect;
3720 bc_lo = qc->tf.lbam;
3721 bc_hi = qc->tf.lbah;
3722 bytes = (bc_hi << 8) | bc_lo;
3724 /* shall be cleared to zero, indicating xfer of data */
3725 if (ireason & (1 << 0))
3728 /* make sure transfer direction matches expected */
3729 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3730 if (do_write != i_write)
3733 __atapi_pio_bytes(qc, bytes);
3738 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3739 ap->id, dev->devno);
3740 qc->err_mask |= AC_ERR_HSM;
3741 ap->hsm_task_state = HSM_ST_ERR;
3745 * ata_pio_block - start PIO on a block
3746 * @ap: the target ata_port
3749 * None. (executing in kernel thread context)
3752 static void ata_pio_block(struct ata_port *ap)
3754 struct ata_queued_cmd *qc;
3758 * This is purely heuristic. This is a fast path.
3759 * Sometimes when we enter, BSY will be cleared in
3760 * a chk-status or two. If not, the drive is probably seeking
3761 * or something. Snooze for a couple msecs, then
3762 * chk-status again. If still busy, fall back to
3763 * HSM_ST_POLL state.
3765 status = ata_busy_wait(ap, ATA_BUSY, 5);
3766 if (status & ATA_BUSY) {
3768 status = ata_busy_wait(ap, ATA_BUSY, 10);
3769 if (status & ATA_BUSY) {
3770 ap->hsm_task_state = HSM_ST_POLL;
3771 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3776 qc = ata_qc_from_tag(ap, ap->active_tag);
3777 WARN_ON(qc == NULL);
3780 if (status & (ATA_ERR | ATA_DF)) {
3781 qc->err_mask |= AC_ERR_DEV;
3782 ap->hsm_task_state = HSM_ST_ERR;
3786 /* transfer data if any */
3787 if (is_atapi_taskfile(&qc->tf)) {
3788 /* DRQ=0 means no more data to transfer */
3789 if ((status & ATA_DRQ) == 0) {
3790 ap->hsm_task_state = HSM_ST_LAST;
3794 atapi_pio_bytes(qc);
3796 /* handle BSY=0, DRQ=0 as error */
3797 if ((status & ATA_DRQ) == 0) {
3798 qc->err_mask |= AC_ERR_HSM;
3799 ap->hsm_task_state = HSM_ST_ERR;
3807 static void ata_pio_error(struct ata_port *ap)
3809 struct ata_queued_cmd *qc;
3811 qc = ata_qc_from_tag(ap, ap->active_tag);
3812 WARN_ON(qc == NULL);
3814 if (qc->tf.command != ATA_CMD_PACKET)
3815 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3817 /* make sure qc->err_mask is available to
3818 * know what's wrong and recover
3820 WARN_ON(qc->err_mask == 0);
3822 ap->hsm_task_state = HSM_ST_IDLE;
3824 ata_poll_qc_complete(qc);
3827 static void ata_pio_task(void *_data)
3829 struct ata_port *ap = _data;
3830 unsigned long timeout;
3837 switch (ap->hsm_task_state) {
3846 qc_completed = ata_pio_complete(ap);
3850 case HSM_ST_LAST_POLL:
3851 timeout = ata_pio_poll(ap);
3861 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
3862 else if (!qc_completed)
3867 * atapi_packet_task - Write CDB bytes to hardware
3868 * @_data: Port to which ATAPI device is attached.
3870 * When device has indicated its readiness to accept
3871 * a CDB, this function is called. Send the CDB.
3872 * If DMA is to be performed, exit immediately.
3873 * Otherwise, we are in polling mode, so poll
3874 * status under operation succeeds or fails.
3877 * Kernel thread context (may sleep)
3880 static void atapi_packet_task(void *_data)
3882 struct ata_port *ap = _data;
3883 struct ata_queued_cmd *qc;
3886 qc = ata_qc_from_tag(ap, ap->active_tag);
3887 WARN_ON(qc == NULL);
3888 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3890 /* sleep-wait for BSY to clear */
3891 DPRINTK("busy wait\n");
3892 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3893 qc->err_mask |= AC_ERR_TIMEOUT;
3897 /* make sure DRQ is set */
3898 status = ata_chk_status(ap);
3899 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3900 qc->err_mask |= AC_ERR_HSM;
3905 DPRINTK("send cdb\n");
3906 WARN_ON(qc->dev->cdb_len < 12);
3908 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3909 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3910 unsigned long flags;
3912 /* Once we're done issuing command and kicking bmdma,
3913 * irq handler takes over. To not lose irq, we need
3914 * to clear NOINTR flag before sending cdb, but
3915 * interrupt handler shouldn't be invoked before we're
3916 * finished. Hence, the following locking.
3918 spin_lock_irqsave(&ap->host_set->lock, flags);
3919 ap->flags &= ~ATA_FLAG_NOINTR;
3920 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3921 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3922 ap->ops->bmdma_start(qc); /* initiate bmdma */
3923 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3925 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3927 /* PIO commands are handled by polling */
3928 ap->hsm_task_state = HSM_ST;
3929 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3935 ata_poll_qc_complete(qc);
3939 * ata_qc_timeout - Handle timeout of queued command
3940 * @qc: Command that timed out
3942 * Some part of the kernel (currently, only the SCSI layer)
3943 * has noticed that the active command on port @ap has not
3944 * completed after a specified length of time. Handle this
3945 * condition by disabling DMA (if necessary) and completing
3946 * transactions, with error if necessary.
3948 * This also handles the case of the "lost interrupt", where
3949 * for some reason (possibly hardware bug, possibly driver bug)
3950 * an interrupt was not delivered to the driver, even though the
3951 * transaction completed successfully.
3954 * Inherited from SCSI layer (none, can sleep)
3957 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3959 struct ata_port *ap = qc->ap;
3960 struct ata_host_set *host_set = ap->host_set;
3961 u8 host_stat = 0, drv_stat;
3962 unsigned long flags;
3966 ap->hsm_task_state = HSM_ST_IDLE;
3968 spin_lock_irqsave(&host_set->lock, flags);
3970 switch (qc->tf.protocol) {
3973 case ATA_PROT_ATAPI_DMA:
3974 host_stat = ap->ops->bmdma_status(ap);
3976 /* before we do anything else, clear DMA-Start bit */
3977 ap->ops->bmdma_stop(qc);
3983 drv_stat = ata_chk_status(ap);
3985 /* ack bmdma irq events */
3986 ap->ops->irq_clear(ap);
3988 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3989 ap->id, qc->tf.command, drv_stat, host_stat);
3991 /* complete taskfile transaction */
3992 qc->err_mask |= ac_err_mask(drv_stat);
3996 spin_unlock_irqrestore(&host_set->lock, flags);
3998 ata_eh_qc_complete(qc);
4004 * ata_eng_timeout - Handle timeout of queued command
4005 * @ap: Port on which timed-out command is active
4007 * Some part of the kernel (currently, only the SCSI layer)
4008 * has noticed that the active command on port @ap has not
4009 * completed after a specified length of time. Handle this
4010 * condition by disabling DMA (if necessary) and completing
4011 * transactions, with error if necessary.
4013 * This also handles the case of the "lost interrupt", where
4014 * for some reason (possibly hardware bug, possibly driver bug)
4015 * an interrupt was not delivered to the driver, even though the
4016 * transaction completed successfully.
4019 * Inherited from SCSI layer (none, can sleep)
4022 void ata_eng_timeout(struct ata_port *ap)
4026 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
4032 * ata_qc_new - Request an available ATA command, for queueing
4033 * @ap: Port associated with device @dev
4034 * @dev: Device from whom we request an available command structure
4040 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4042 struct ata_queued_cmd *qc = NULL;
4045 for (i = 0; i < ATA_MAX_QUEUE; i++)
4046 if (!test_and_set_bit(i, &ap->qactive)) {
4047 qc = ata_qc_from_tag(ap, i);
4058 * ata_qc_new_init - Request an available ATA command, and initialize it
4059 * @ap: Port associated with device @dev
4060 * @dev: Device from whom we request an available command structure
4066 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
4067 struct ata_device *dev)
4069 struct ata_queued_cmd *qc;
4071 qc = ata_qc_new(ap);
4084 * ata_qc_free - free unused ata_queued_cmd
4085 * @qc: Command to complete
4087 * Designed to free unused ata_queued_cmd object
4088 * in case something prevents using it.
4091 * spin_lock_irqsave(host_set lock)
4093 void ata_qc_free(struct ata_queued_cmd *qc)
4095 struct ata_port *ap = qc->ap;
4098 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4102 if (likely(ata_tag_valid(tag))) {
4103 if (tag == ap->active_tag)
4104 ap->active_tag = ATA_TAG_POISON;
4105 qc->tag = ATA_TAG_POISON;
4106 clear_bit(tag, &ap->qactive);
4110 void __ata_qc_complete(struct ata_queued_cmd *qc)
4112 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4113 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4115 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4118 /* atapi: mark qc as inactive to prevent the interrupt handler
4119 * from completing the command twice later, before the error handler
4120 * is called. (when rc != 0 and atapi request sense is needed)
4122 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4124 /* call completion callback */
4125 qc->complete_fn(qc);
4128 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4130 struct ata_port *ap = qc->ap;
4132 switch (qc->tf.protocol) {
4134 case ATA_PROT_ATAPI_DMA:
4137 case ATA_PROT_ATAPI:
4139 if (ap->flags & ATA_FLAG_PIO_DMA)
4152 * ata_qc_issue - issue taskfile to device
4153 * @qc: command to issue to device
4155 * Prepare an ATA command to submission to device.
4156 * This includes mapping the data into a DMA-able
4157 * area, filling in the S/G table, and finally
4158 * writing the taskfile to hardware, starting the command.
4161 * spin_lock_irqsave(host_set lock)
4163 void ata_qc_issue(struct ata_queued_cmd *qc)
4165 struct ata_port *ap = qc->ap;
4167 qc->ap->active_tag = qc->tag;
4168 qc->flags |= ATA_QCFLAG_ACTIVE;
4170 if (ata_should_dma_map(qc)) {
4171 if (qc->flags & ATA_QCFLAG_SG) {
4172 if (ata_sg_setup(qc))
4174 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4175 if (ata_sg_setup_one(qc))
4179 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4182 ap->ops->qc_prep(qc);
4184 qc->err_mask |= ap->ops->qc_issue(qc);
4185 if (unlikely(qc->err_mask))
4190 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4191 qc->err_mask |= AC_ERR_SYSTEM;
4193 ata_qc_complete(qc);
4197 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4198 * @qc: command to issue to device
4200 * Using various libata functions and hooks, this function
4201 * starts an ATA command. ATA commands are grouped into
4202 * classes called "protocols", and issuing each type of protocol
4203 * is slightly different.
4205 * May be used as the qc_issue() entry in ata_port_operations.
4208 * spin_lock_irqsave(host_set lock)
4211 * Zero on success, AC_ERR_* mask on failure
4214 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
4216 struct ata_port *ap = qc->ap;
4218 ata_dev_select(ap, qc->dev->devno, 1, 0);
4220 switch (qc->tf.protocol) {
4221 case ATA_PROT_NODATA:
4222 ata_tf_to_host(ap, &qc->tf);
4226 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4227 ap->ops->bmdma_setup(qc); /* set up bmdma */
4228 ap->ops->bmdma_start(qc); /* initiate bmdma */
4231 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4232 ata_qc_set_polling(qc);
4233 ata_tf_to_host(ap, &qc->tf);
4234 ap->hsm_task_state = HSM_ST;
4235 ata_port_queue_task(ap, ata_pio_task, ap, 0);
4238 case ATA_PROT_ATAPI:
4239 ata_qc_set_polling(qc);
4240 ata_tf_to_host(ap, &qc->tf);
4241 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4244 case ATA_PROT_ATAPI_NODATA:
4245 ap->flags |= ATA_FLAG_NOINTR;
4246 ata_tf_to_host(ap, &qc->tf);
4247 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4250 case ATA_PROT_ATAPI_DMA:
4251 ap->flags |= ATA_FLAG_NOINTR;
4252 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4253 ap->ops->bmdma_setup(qc); /* set up bmdma */
4254 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4259 return AC_ERR_SYSTEM;
4266 * ata_host_intr - Handle host interrupt for given (port, task)
4267 * @ap: Port on which interrupt arrived (possibly...)
4268 * @qc: Taskfile currently active in engine
4270 * Handle host interrupt for given queued command. Currently,
4271 * only DMA interrupts are handled. All other commands are
4272 * handled via polling with interrupts disabled (nIEN bit).
4275 * spin_lock_irqsave(host_set lock)
4278 * One if interrupt was handled, zero if not (shared irq).
4281 inline unsigned int ata_host_intr (struct ata_port *ap,
4282 struct ata_queued_cmd *qc)
4284 u8 status, host_stat;
4286 switch (qc->tf.protocol) {
4289 case ATA_PROT_ATAPI_DMA:
4290 case ATA_PROT_ATAPI:
4291 /* check status of DMA engine */
4292 host_stat = ap->ops->bmdma_status(ap);
4293 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4295 /* if it's not our irq... */
4296 if (!(host_stat & ATA_DMA_INTR))
4299 /* before we do anything else, clear DMA-Start bit */
4300 ap->ops->bmdma_stop(qc);
4304 case ATA_PROT_ATAPI_NODATA:
4305 case ATA_PROT_NODATA:
4306 /* check altstatus */
4307 status = ata_altstatus(ap);
4308 if (status & ATA_BUSY)
4311 /* check main status, clearing INTRQ */
4312 status = ata_chk_status(ap);
4313 if (unlikely(status & ATA_BUSY))
4315 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4316 ap->id, qc->tf.protocol, status);
4318 /* ack bmdma irq events */
4319 ap->ops->irq_clear(ap);
4321 /* complete taskfile transaction */
4322 qc->err_mask |= ac_err_mask(status);
4323 ata_qc_complete(qc);
4330 return 1; /* irq handled */
4333 ap->stats.idle_irq++;
4336 if ((ap->stats.idle_irq % 1000) == 0) {
4337 ata_irq_ack(ap, 0); /* debug trap */
4338 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4342 return 0; /* irq not handled */
4346 * ata_interrupt - Default ATA host interrupt handler
4347 * @irq: irq line (unused)
4348 * @dev_instance: pointer to our ata_host_set information structure
4351 * Default interrupt handler for PCI IDE devices. Calls
4352 * ata_host_intr() for each port that is not disabled.
4355 * Obtains host_set lock during operation.
4358 * IRQ_NONE or IRQ_HANDLED.
4361 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4363 struct ata_host_set *host_set = dev_instance;
4365 unsigned int handled = 0;
4366 unsigned long flags;
4368 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4369 spin_lock_irqsave(&host_set->lock, flags);
4371 for (i = 0; i < host_set->n_ports; i++) {
4372 struct ata_port *ap;
4374 ap = host_set->ports[i];
4376 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
4377 struct ata_queued_cmd *qc;
4379 qc = ata_qc_from_tag(ap, ap->active_tag);
4380 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4381 (qc->flags & ATA_QCFLAG_ACTIVE))
4382 handled |= ata_host_intr(ap, qc);
4386 spin_unlock_irqrestore(&host_set->lock, flags);
4388 return IRQ_RETVAL(handled);
4393 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4394 * without filling any other registers
4396 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4399 struct ata_taskfile tf;
4402 ata_tf_init(ap, &tf, dev->devno);
4405 tf.flags |= ATA_TFLAG_DEVICE;
4406 tf.protocol = ATA_PROT_NODATA;
4408 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4410 printk(KERN_ERR "%s: ata command failed: %d\n",
4416 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4420 if (!ata_try_flush_cache(dev))
4423 if (ata_id_has_flush_ext(dev->id))
4424 cmd = ATA_CMD_FLUSH_EXT;
4426 cmd = ATA_CMD_FLUSH;
4428 return ata_do_simple_cmd(ap, dev, cmd);
4431 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4433 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4436 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4438 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4442 * ata_device_resume - wakeup a previously suspended devices
4443 * @ap: port the device is connected to
4444 * @dev: the device to resume
4446 * Kick the drive back into action, by sending it an idle immediate
4447 * command and making sure its transfer mode matches between drive
4451 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4453 if (ap->flags & ATA_FLAG_SUSPENDED) {
4454 struct ata_device *failed_dev;
4455 ap->flags &= ~ATA_FLAG_SUSPENDED;
4456 while (ata_set_mode(ap, &failed_dev))
4457 ata_dev_disable(ap, failed_dev);
4459 if (!ata_dev_enabled(dev))
4461 if (dev->class == ATA_DEV_ATA)
4462 ata_start_drive(ap, dev);
4468 * ata_device_suspend - prepare a device for suspend
4469 * @ap: port the device is connected to
4470 * @dev: the device to suspend
4472 * Flush the cache on the drive, if appropriate, then issue a
4473 * standbynow command.
4475 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
4477 if (!ata_dev_enabled(dev))
4479 if (dev->class == ATA_DEV_ATA)
4480 ata_flush_cache(ap, dev);
4482 if (state.event != PM_EVENT_FREEZE)
4483 ata_standby_drive(ap, dev);
4484 ap->flags |= ATA_FLAG_SUSPENDED;
4489 * ata_port_start - Set port up for dma.
4490 * @ap: Port to initialize
4492 * Called just after data structures for each port are
4493 * initialized. Allocates space for PRD table.
4495 * May be used as the port_start() entry in ata_port_operations.
4498 * Inherited from caller.
4501 int ata_port_start (struct ata_port *ap)
4503 struct device *dev = ap->dev;
4506 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4510 rc = ata_pad_alloc(ap, dev);
4512 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4516 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4523 * ata_port_stop - Undo ata_port_start()
4524 * @ap: Port to shut down
4526 * Frees the PRD table.
4528 * May be used as the port_stop() entry in ata_port_operations.
4531 * Inherited from caller.
4534 void ata_port_stop (struct ata_port *ap)
4536 struct device *dev = ap->dev;
4538 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4539 ata_pad_free(ap, dev);
4542 void ata_host_stop (struct ata_host_set *host_set)
4544 if (host_set->mmio_base)
4545 iounmap(host_set->mmio_base);
4550 * ata_host_remove - Unregister SCSI host structure with upper layers
4551 * @ap: Port to unregister
4552 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4555 * Inherited from caller.
4558 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4560 struct Scsi_Host *sh = ap->host;
4565 scsi_remove_host(sh);
4567 ap->ops->port_stop(ap);
4571 * ata_host_init - Initialize an ata_port structure
4572 * @ap: Structure to initialize
4573 * @host: associated SCSI mid-layer structure
4574 * @host_set: Collection of hosts to which @ap belongs
4575 * @ent: Probe information provided by low-level driver
4576 * @port_no: Port number associated with this ata_port
4578 * Initialize a new ata_port structure, and its associated
4582 * Inherited from caller.
4585 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4586 struct ata_host_set *host_set,
4587 const struct ata_probe_ent *ent, unsigned int port_no)
4593 host->max_channel = 1;
4594 host->unique_id = ata_unique_id++;
4595 host->max_cmd_len = 12;
4597 ap->flags = ATA_FLAG_PORT_DISABLED;
4598 ap->id = host->unique_id;
4600 ap->ctl = ATA_DEVCTL_OBS;
4601 ap->host_set = host_set;
4603 ap->port_no = port_no;
4605 ent->legacy_mode ? ent->hard_port_no : port_no;
4606 ap->pio_mask = ent->pio_mask;
4607 ap->mwdma_mask = ent->mwdma_mask;
4608 ap->udma_mask = ent->udma_mask;
4609 ap->flags |= ent->host_flags;
4610 ap->ops = ent->port_ops;
4611 ap->cbl = ATA_CBL_NONE;
4612 ap->sata_spd_limit = UINT_MAX;
4613 ap->active_tag = ATA_TAG_POISON;
4614 ap->last_ctl = 0xFF;
4616 INIT_WORK(&ap->port_task, NULL, NULL);
4617 INIT_LIST_HEAD(&ap->eh_done_q);
4619 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4620 struct ata_device *dev = &ap->device[i];
4622 dev->pio_mask = UINT_MAX;
4623 dev->mwdma_mask = UINT_MAX;
4624 dev->udma_mask = UINT_MAX;
4628 ap->stats.unhandled_irq = 1;
4629 ap->stats.idle_irq = 1;
4632 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4636 * ata_host_add - Attach low-level ATA driver to system
4637 * @ent: Information provided by low-level driver
4638 * @host_set: Collections of ports to which we add
4639 * @port_no: Port number associated with this host
4641 * Attach low-level ATA driver to system.
4644 * PCI/etc. bus probe sem.
4647 * New ata_port on success, for NULL on error.
4650 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4651 struct ata_host_set *host_set,
4652 unsigned int port_no)
4654 struct Scsi_Host *host;
4655 struct ata_port *ap;
4660 if (!ent->port_ops->probe_reset &&
4661 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
4662 printk(KERN_ERR "ata%u: no reset mechanism available\n",
4667 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4671 host->transportt = &ata_scsi_transport_template;
4673 ap = (struct ata_port *) &host->hostdata[0];
4675 ata_host_init(ap, host, host_set, ent, port_no);
4677 rc = ap->ops->port_start(ap);
4684 scsi_host_put(host);
4689 * ata_device_add - Register hardware device with ATA and SCSI layers
4690 * @ent: Probe information describing hardware device to be registered
4692 * This function processes the information provided in the probe
4693 * information struct @ent, allocates the necessary ATA and SCSI
4694 * host information structures, initializes them, and registers
4695 * everything with requisite kernel subsystems.
4697 * This function requests irqs, probes the ATA bus, and probes
4701 * PCI/etc. bus probe sem.
4704 * Number of ports registered. Zero on error (no ports registered).
4707 int ata_device_add(const struct ata_probe_ent *ent)
4709 unsigned int count = 0, i;
4710 struct device *dev = ent->dev;
4711 struct ata_host_set *host_set;
4714 /* alloc a container for our list of ATA ports (buses) */
4715 host_set = kzalloc(sizeof(struct ata_host_set) +
4716 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4719 spin_lock_init(&host_set->lock);
4721 host_set->dev = dev;
4722 host_set->n_ports = ent->n_ports;
4723 host_set->irq = ent->irq;
4724 host_set->mmio_base = ent->mmio_base;
4725 host_set->private_data = ent->private_data;
4726 host_set->ops = ent->port_ops;
4727 host_set->flags = ent->host_set_flags;
4729 /* register each port bound to this device */
4730 for (i = 0; i < ent->n_ports; i++) {
4731 struct ata_port *ap;
4732 unsigned long xfer_mode_mask;
4734 ap = ata_host_add(ent, host_set, i);
4738 host_set->ports[i] = ap;
4739 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4740 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4741 (ap->pio_mask << ATA_SHIFT_PIO);
4743 /* print per-port info to dmesg */
4744 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4745 "bmdma 0x%lX irq %lu\n",
4747 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4748 ata_mode_string(xfer_mode_mask),
4749 ap->ioaddr.cmd_addr,
4750 ap->ioaddr.ctl_addr,
4751 ap->ioaddr.bmdma_addr,
4755 host_set->ops->irq_clear(ap);
4762 /* obtain irq, that is shared between channels */
4763 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4764 DRV_NAME, host_set))
4767 /* perform each probe synchronously */
4768 DPRINTK("probe begin\n");
4769 for (i = 0; i < count; i++) {
4770 struct ata_port *ap;
4773 ap = host_set->ports[i];
4775 DPRINTK("ata%u: bus probe begin\n", ap->id);
4776 rc = ata_bus_probe(ap);
4777 DPRINTK("ata%u: bus probe end\n", ap->id);
4780 /* FIXME: do something useful here?
4781 * Current libata behavior will
4782 * tear down everything when
4783 * the module is removed
4784 * or the h/w is unplugged.
4788 rc = scsi_add_host(ap->host, dev);
4790 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4792 /* FIXME: do something useful here */
4793 /* FIXME: handle unconditional calls to
4794 * scsi_scan_host and ata_host_remove, below,
4800 /* probes are done, now scan each port's disk(s) */
4801 DPRINTK("host probe begin\n");
4802 for (i = 0; i < count; i++) {
4803 struct ata_port *ap = host_set->ports[i];
4805 ata_scsi_scan_host(ap);
4808 dev_set_drvdata(dev, host_set);
4810 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4811 return ent->n_ports; /* success */
4814 for (i = 0; i < count; i++) {
4815 ata_host_remove(host_set->ports[i], 1);
4816 scsi_host_put(host_set->ports[i]->host);
4820 VPRINTK("EXIT, returning 0\n");
4825 * ata_host_set_remove - PCI layer callback for device removal
4826 * @host_set: ATA host set that was removed
4828 * Unregister all objects associated with this host set. Free those
4832 * Inherited from calling layer (may sleep).
4835 void ata_host_set_remove(struct ata_host_set *host_set)
4837 struct ata_port *ap;
4840 for (i = 0; i < host_set->n_ports; i++) {
4841 ap = host_set->ports[i];
4842 scsi_remove_host(ap->host);
4845 free_irq(host_set->irq, host_set);
4847 for (i = 0; i < host_set->n_ports; i++) {
4848 ap = host_set->ports[i];
4850 ata_scsi_release(ap->host);
4852 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4853 struct ata_ioports *ioaddr = &ap->ioaddr;
4855 if (ioaddr->cmd_addr == 0x1f0)
4856 release_region(0x1f0, 8);
4857 else if (ioaddr->cmd_addr == 0x170)
4858 release_region(0x170, 8);
4861 scsi_host_put(ap->host);
4864 if (host_set->ops->host_stop)
4865 host_set->ops->host_stop(host_set);
4871 * ata_scsi_release - SCSI layer callback hook for host unload
4872 * @host: libata host to be unloaded
4874 * Performs all duties necessary to shut down a libata port...
4875 * Kill port kthread, disable port, and release resources.
4878 * Inherited from SCSI layer.
4884 int ata_scsi_release(struct Scsi_Host *host)
4886 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4891 ap->ops->port_disable(ap);
4892 ata_host_remove(ap, 0);
4893 for (i = 0; i < ATA_MAX_DEVICES; i++)
4894 kfree(ap->device[i].id);
4901 * ata_std_ports - initialize ioaddr with standard port offsets.
4902 * @ioaddr: IO address structure to be initialized
4904 * Utility function which initializes data_addr, error_addr,
4905 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4906 * device_addr, status_addr, and command_addr to standard offsets
4907 * relative to cmd_addr.
4909 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4912 void ata_std_ports(struct ata_ioports *ioaddr)
4914 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4915 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4916 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4917 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4918 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4919 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4920 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4921 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4922 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4923 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4929 void ata_pci_host_stop (struct ata_host_set *host_set)
4931 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4933 pci_iounmap(pdev, host_set->mmio_base);
4937 * ata_pci_remove_one - PCI layer callback for device removal
4938 * @pdev: PCI device that was removed
4940 * PCI layer indicates to libata via this hook that
4941 * hot-unplug or module unload event has occurred.
4942 * Handle this by unregistering all objects associated
4943 * with this PCI device. Free those objects. Then finally
4944 * release PCI resources and disable device.
4947 * Inherited from PCI layer (may sleep).
4950 void ata_pci_remove_one (struct pci_dev *pdev)
4952 struct device *dev = pci_dev_to_dev(pdev);
4953 struct ata_host_set *host_set = dev_get_drvdata(dev);
4955 ata_host_set_remove(host_set);
4956 pci_release_regions(pdev);
4957 pci_disable_device(pdev);
4958 dev_set_drvdata(dev, NULL);
4961 /* move to PCI subsystem */
4962 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4964 unsigned long tmp = 0;
4966 switch (bits->width) {
4969 pci_read_config_byte(pdev, bits->reg, &tmp8);
4975 pci_read_config_word(pdev, bits->reg, &tmp16);
4981 pci_read_config_dword(pdev, bits->reg, &tmp32);
4992 return (tmp == bits->val) ? 1 : 0;
4995 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4997 pci_save_state(pdev);
4998 pci_disable_device(pdev);
4999 pci_set_power_state(pdev, PCI_D3hot);
5003 int ata_pci_device_resume(struct pci_dev *pdev)
5005 pci_set_power_state(pdev, PCI_D0);
5006 pci_restore_state(pdev);
5007 pci_enable_device(pdev);
5008 pci_set_master(pdev);
5011 #endif /* CONFIG_PCI */
5014 static int __init ata_init(void)
5016 ata_wq = create_workqueue("ata");
5020 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5024 static void __exit ata_exit(void)
5026 destroy_workqueue(ata_wq);
5029 module_init(ata_init);
5030 module_exit(ata_exit);
5032 static unsigned long ratelimit_time;
5033 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5035 int ata_ratelimit(void)
5038 unsigned long flags;
5040 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5042 if (time_after(jiffies, ratelimit_time)) {
5044 ratelimit_time = jiffies + (HZ/5);
5048 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5054 * libata is essentially a library of internal helper functions for
5055 * low-level ATA host controller drivers. As such, the API/ABI is
5056 * likely to change as new drivers are added and updated.
5057 * Do not depend on ABI/API stability.
5060 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5061 EXPORT_SYMBOL_GPL(ata_std_ports);
5062 EXPORT_SYMBOL_GPL(ata_device_add);
5063 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5064 EXPORT_SYMBOL_GPL(ata_sg_init);
5065 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5066 EXPORT_SYMBOL_GPL(__ata_qc_complete);
5067 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5068 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5069 EXPORT_SYMBOL_GPL(ata_tf_load);
5070 EXPORT_SYMBOL_GPL(ata_tf_read);
5071 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5072 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5073 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5074 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5075 EXPORT_SYMBOL_GPL(ata_check_status);
5076 EXPORT_SYMBOL_GPL(ata_altstatus);
5077 EXPORT_SYMBOL_GPL(ata_exec_command);
5078 EXPORT_SYMBOL_GPL(ata_port_start);
5079 EXPORT_SYMBOL_GPL(ata_port_stop);
5080 EXPORT_SYMBOL_GPL(ata_host_stop);
5081 EXPORT_SYMBOL_GPL(ata_interrupt);
5082 EXPORT_SYMBOL_GPL(ata_qc_prep);
5083 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
5084 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5085 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5086 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5087 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5088 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5089 EXPORT_SYMBOL_GPL(ata_port_probe);
5090 EXPORT_SYMBOL_GPL(sata_phy_reset);
5091 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5092 EXPORT_SYMBOL_GPL(ata_bus_reset);
5093 EXPORT_SYMBOL_GPL(ata_std_probeinit);
5094 EXPORT_SYMBOL_GPL(ata_std_softreset);
5095 EXPORT_SYMBOL_GPL(sata_std_hardreset);
5096 EXPORT_SYMBOL_GPL(ata_std_postreset);
5097 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
5098 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
5099 EXPORT_SYMBOL_GPL(ata_dev_revalidate);
5100 EXPORT_SYMBOL_GPL(ata_dev_classify);
5101 EXPORT_SYMBOL_GPL(ata_dev_pair);
5102 EXPORT_SYMBOL_GPL(ata_port_disable);
5103 EXPORT_SYMBOL_GPL(ata_ratelimit);
5104 EXPORT_SYMBOL_GPL(ata_busy_sleep);
5105 EXPORT_SYMBOL_GPL(ata_port_queue_task);
5106 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5107 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5108 EXPORT_SYMBOL_GPL(ata_scsi_error);
5109 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5110 EXPORT_SYMBOL_GPL(ata_scsi_release);
5111 EXPORT_SYMBOL_GPL(ata_host_intr);
5112 EXPORT_SYMBOL_GPL(ata_id_string);
5113 EXPORT_SYMBOL_GPL(ata_id_c_string);
5114 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5115 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5116 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
5118 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
5119 EXPORT_SYMBOL_GPL(ata_timing_compute);
5120 EXPORT_SYMBOL_GPL(ata_timing_merge);
5123 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5124 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5125 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5126 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5127 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5128 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5129 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
5130 EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5131 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
5132 #endif /* CONFIG_PCI */
5134 EXPORT_SYMBOL_GPL(ata_device_suspend);
5135 EXPORT_SYMBOL_GPL(ata_device_resume);
5136 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5137 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);