2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev);
66 static void ata_set_mode(struct ata_port *ap);
67 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
68 static unsigned int ata_dev_xfermask(struct ata_port *ap,
69 struct ata_device *dev);
71 static unsigned int ata_unique_id = 1;
72 static struct workqueue_struct *ata_wq;
74 int atapi_enabled = 1;
75 module_param(atapi_enabled, int, 0444);
76 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
79 module_param_named(fua, libata_fua, int, 0444);
80 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
82 MODULE_AUTHOR("Jeff Garzik");
83 MODULE_DESCRIPTION("Library module for ATA devices");
84 MODULE_LICENSE("GPL");
85 MODULE_VERSION(DRV_VERSION);
89 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
90 * @tf: Taskfile to convert
91 * @fis: Buffer into which data will output
92 * @pmp: Port multiplier port
94 * Converts a standard ATA taskfile to a Serial ATA
95 * FIS structure (Register - Host to Device).
98 * Inherited from caller.
101 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
103 fis[0] = 0x27; /* Register - Host to Device FIS */
104 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
105 bit 7 indicates Command FIS */
106 fis[2] = tf->command;
107 fis[3] = tf->feature;
114 fis[8] = tf->hob_lbal;
115 fis[9] = tf->hob_lbam;
116 fis[10] = tf->hob_lbah;
117 fis[11] = tf->hob_feature;
120 fis[13] = tf->hob_nsect;
131 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
132 * @fis: Buffer from which data will be input
133 * @tf: Taskfile to output
135 * Converts a serial ATA FIS structure to a standard ATA taskfile.
138 * Inherited from caller.
141 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
143 tf->command = fis[2]; /* status */
144 tf->feature = fis[3]; /* error */
151 tf->hob_lbal = fis[8];
152 tf->hob_lbam = fis[9];
153 tf->hob_lbah = fis[10];
156 tf->hob_nsect = fis[13];
159 static const u8 ata_rw_cmds[] = {
163 ATA_CMD_READ_MULTI_EXT,
164 ATA_CMD_WRITE_MULTI_EXT,
168 ATA_CMD_WRITE_MULTI_FUA_EXT,
172 ATA_CMD_PIO_READ_EXT,
173 ATA_CMD_PIO_WRITE_EXT,
186 ATA_CMD_WRITE_FUA_EXT
190 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
191 * @qc: command to examine and configure
193 * Examine the device configuration and tf->flags to calculate
194 * the proper read/write commands and protocol to use.
199 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
201 struct ata_taskfile *tf = &qc->tf;
202 struct ata_device *dev = qc->dev;
205 int index, fua, lba48, write;
207 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
208 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
209 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
211 if (dev->flags & ATA_DFLAG_PIO) {
212 tf->protocol = ATA_PROT_PIO;
213 index = dev->multi_count ? 0 : 8;
214 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
215 /* Unable to use DMA due to host limitation */
216 tf->protocol = ATA_PROT_PIO;
217 index = dev->multi_count ? 0 : 8;
219 tf->protocol = ATA_PROT_DMA;
223 cmd = ata_rw_cmds[index + fua + lba48 + write];
232 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
233 * @pio_mask: pio_mask
234 * @mwdma_mask: mwdma_mask
235 * @udma_mask: udma_mask
237 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
238 * unsigned int xfer_mask.
246 static unsigned int ata_pack_xfermask(unsigned int pio_mask,
247 unsigned int mwdma_mask,
248 unsigned int udma_mask)
250 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
251 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
252 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
255 static const struct ata_xfer_ent {
256 unsigned int shift, bits;
259 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
260 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
261 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
266 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
267 * @xfer_mask: xfer_mask of interest
269 * Return matching XFER_* value for @xfer_mask. Only the highest
270 * bit of @xfer_mask is considered.
276 * Matching XFER_* value, 0 if no match found.
278 static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
280 int highbit = fls(xfer_mask) - 1;
281 const struct ata_xfer_ent *ent;
283 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
284 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
285 return ent->base + highbit - ent->shift;
290 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
291 * @xfer_mode: XFER_* of interest
293 * Return matching xfer_mask for @xfer_mode.
299 * Matching xfer_mask, 0 if no match found.
301 static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
303 const struct ata_xfer_ent *ent;
305 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
306 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
307 return 1 << (ent->shift + xfer_mode - ent->base);
312 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
313 * @xfer_mode: XFER_* of interest
315 * Return matching xfer_shift for @xfer_mode.
321 * Matching xfer_shift, -1 if no match found.
323 static int ata_xfer_mode2shift(unsigned int xfer_mode)
325 const struct ata_xfer_ent *ent;
327 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
328 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
334 * ata_mode_string - convert xfer_mask to string
335 * @xfer_mask: mask of bits supported; only highest bit counts.
337 * Determine string which represents the highest speed
338 * (highest bit in @modemask).
344 * Constant C string representing highest speed listed in
345 * @mode_mask, or the constant C string "<n/a>".
347 static const char *ata_mode_string(unsigned int xfer_mask)
349 static const char * const xfer_mode_str[] = {
369 highbit = fls(xfer_mask) - 1;
370 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
371 return xfer_mode_str[highbit];
376 * ata_pio_devchk - PATA device presence detection
377 * @ap: ATA channel to examine
378 * @device: Device to examine (starting at zero)
380 * This technique was originally described in
381 * Hale Landis's ATADRVR (www.ata-atapi.com), and
382 * later found its way into the ATA/ATAPI spec.
384 * Write a pattern to the ATA shadow registers,
385 * and if a device is present, it will respond by
386 * correctly storing and echoing back the
387 * ATA shadow register contents.
393 static unsigned int ata_pio_devchk(struct ata_port *ap,
396 struct ata_ioports *ioaddr = &ap->ioaddr;
399 ap->ops->dev_select(ap, device);
401 outb(0x55, ioaddr->nsect_addr);
402 outb(0xaa, ioaddr->lbal_addr);
404 outb(0xaa, ioaddr->nsect_addr);
405 outb(0x55, ioaddr->lbal_addr);
407 outb(0x55, ioaddr->nsect_addr);
408 outb(0xaa, ioaddr->lbal_addr);
410 nsect = inb(ioaddr->nsect_addr);
411 lbal = inb(ioaddr->lbal_addr);
413 if ((nsect == 0x55) && (lbal == 0xaa))
414 return 1; /* we found a device */
416 return 0; /* nothing found */
420 * ata_mmio_devchk - PATA device presence detection
421 * @ap: ATA channel to examine
422 * @device: Device to examine (starting at zero)
424 * This technique was originally described in
425 * Hale Landis's ATADRVR (www.ata-atapi.com), and
426 * later found its way into the ATA/ATAPI spec.
428 * Write a pattern to the ATA shadow registers,
429 * and if a device is present, it will respond by
430 * correctly storing and echoing back the
431 * ATA shadow register contents.
437 static unsigned int ata_mmio_devchk(struct ata_port *ap,
440 struct ata_ioports *ioaddr = &ap->ioaddr;
443 ap->ops->dev_select(ap, device);
445 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
446 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
448 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
449 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
451 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
452 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
454 nsect = readb((void __iomem *) ioaddr->nsect_addr);
455 lbal = readb((void __iomem *) ioaddr->lbal_addr);
457 if ((nsect == 0x55) && (lbal == 0xaa))
458 return 1; /* we found a device */
460 return 0; /* nothing found */
464 * ata_devchk - PATA device presence detection
465 * @ap: ATA channel to examine
466 * @device: Device to examine (starting at zero)
468 * Dispatch ATA device presence detection, depending
469 * on whether we are using PIO or MMIO to talk to the
470 * ATA shadow registers.
476 static unsigned int ata_devchk(struct ata_port *ap,
479 if (ap->flags & ATA_FLAG_MMIO)
480 return ata_mmio_devchk(ap, device);
481 return ata_pio_devchk(ap, device);
485 * ata_dev_classify - determine device type based on ATA-spec signature
486 * @tf: ATA taskfile register set for device to be identified
488 * Determine from taskfile register contents whether a device is
489 * ATA or ATAPI, as per "Signature and persistence" section
490 * of ATA/PI spec (volume 1, sect 5.14).
496 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
497 * the event of failure.
500 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
502 /* Apple's open source Darwin code hints that some devices only
503 * put a proper signature into the LBA mid/high registers,
504 * So, we only check those. It's sufficient for uniqueness.
507 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
508 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
509 DPRINTK("found ATA device by sig\n");
513 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
514 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
515 DPRINTK("found ATAPI device by sig\n");
516 return ATA_DEV_ATAPI;
519 DPRINTK("unknown device\n");
520 return ATA_DEV_UNKNOWN;
524 * ata_dev_try_classify - Parse returned ATA device signature
525 * @ap: ATA channel to examine
526 * @device: Device to examine (starting at zero)
527 * @r_err: Value of error register on completion
529 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
530 * an ATA/ATAPI-defined set of values is placed in the ATA
531 * shadow registers, indicating the results of device detection
534 * Select the ATA device, and read the values from the ATA shadow
535 * registers. Then parse according to the Error register value,
536 * and the spec-defined values examined by ata_dev_classify().
542 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
546 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
548 struct ata_taskfile tf;
552 ap->ops->dev_select(ap, device);
554 memset(&tf, 0, sizeof(tf));
556 ap->ops->tf_read(ap, &tf);
561 /* see if device passed diags */
564 else if ((device == 0) && (err == 0x81))
569 /* determine if device is ATA or ATAPI */
570 class = ata_dev_classify(&tf);
572 if (class == ATA_DEV_UNKNOWN)
574 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
580 * ata_id_string - Convert IDENTIFY DEVICE page into string
581 * @id: IDENTIFY DEVICE results we will examine
582 * @s: string into which data is output
583 * @ofs: offset into identify device page
584 * @len: length of string to return. must be an even number.
586 * The strings in the IDENTIFY DEVICE page are broken up into
587 * 16-bit chunks. Run through the string, and output each
588 * 8-bit chunk linearly, regardless of platform.
594 void ata_id_string(const u16 *id, unsigned char *s,
595 unsigned int ofs, unsigned int len)
614 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
615 * @id: IDENTIFY DEVICE results we will examine
616 * @s: string into which data is output
617 * @ofs: offset into identify device page
618 * @len: length of string to return. must be an odd number.
620 * This function is identical to ata_id_string except that it
621 * trims trailing spaces and terminates the resulting string with
622 * null. @len must be actual maximum length (even number) + 1.
627 void ata_id_c_string(const u16 *id, unsigned char *s,
628 unsigned int ofs, unsigned int len)
634 ata_id_string(id, s, ofs, len - 1);
636 p = s + strnlen(s, len - 1);
637 while (p > s && p[-1] == ' ')
642 static u64 ata_id_n_sectors(const u16 *id)
644 if (ata_id_has_lba(id)) {
645 if (ata_id_has_lba48(id))
646 return ata_id_u64(id, 100);
648 return ata_id_u32(id, 60);
650 if (ata_id_current_chs_valid(id))
651 return ata_id_u32(id, 57);
653 return id[1] * id[3] * id[6];
658 * ata_noop_dev_select - Select device 0/1 on ATA bus
659 * @ap: ATA channel to manipulate
660 * @device: ATA device (numbered from zero) to select
662 * This function performs no actual function.
664 * May be used as the dev_select() entry in ata_port_operations.
669 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
675 * ata_std_dev_select - Select device 0/1 on ATA bus
676 * @ap: ATA channel to manipulate
677 * @device: ATA device (numbered from zero) to select
679 * Use the method defined in the ATA specification to
680 * make either device 0, or device 1, active on the
681 * ATA channel. Works with both PIO and MMIO.
683 * May be used as the dev_select() entry in ata_port_operations.
689 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
694 tmp = ATA_DEVICE_OBS;
696 tmp = ATA_DEVICE_OBS | ATA_DEV1;
698 if (ap->flags & ATA_FLAG_MMIO) {
699 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
701 outb(tmp, ap->ioaddr.device_addr);
703 ata_pause(ap); /* needed; also flushes, for mmio */
707 * ata_dev_select - Select device 0/1 on ATA bus
708 * @ap: ATA channel to manipulate
709 * @device: ATA device (numbered from zero) to select
710 * @wait: non-zero to wait for Status register BSY bit to clear
711 * @can_sleep: non-zero if context allows sleeping
713 * Use the method defined in the ATA specification to
714 * make either device 0, or device 1, active on the
717 * This is a high-level version of ata_std_dev_select(),
718 * which additionally provides the services of inserting
719 * the proper pauses and status polling, where needed.
725 void ata_dev_select(struct ata_port *ap, unsigned int device,
726 unsigned int wait, unsigned int can_sleep)
728 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
729 ap->id, device, wait);
734 ap->ops->dev_select(ap, device);
737 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
744 * ata_dump_id - IDENTIFY DEVICE info debugging output
745 * @id: IDENTIFY DEVICE page to dump
747 * Dump selected 16-bit words from the given IDENTIFY DEVICE
754 static inline void ata_dump_id(const u16 *id)
756 DPRINTK("49==0x%04x "
766 DPRINTK("80==0x%04x "
776 DPRINTK("88==0x%04x "
783 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
784 * @id: IDENTIFY data to compute xfer mask from
786 * Compute the xfermask for this device. This is not as trivial
787 * as it seems if we must consider early devices correctly.
789 * FIXME: pre IDE drive timing (do we care ?).
797 static unsigned int ata_id_xfermask(const u16 *id)
799 unsigned int pio_mask, mwdma_mask, udma_mask;
801 /* Usual case. Word 53 indicates word 64 is valid */
802 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
803 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
807 /* If word 64 isn't valid then Word 51 high byte holds
808 * the PIO timing number for the maximum. Turn it into
811 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
813 /* But wait.. there's more. Design your standards by
814 * committee and you too can get a free iordy field to
815 * process. However its the speeds not the modes that
816 * are supported... Note drivers using the timing API
817 * will get this right anyway
821 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
824 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
825 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
827 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
831 * ata_port_queue_task - Queue port_task
832 * @ap: The ata_port to queue port_task for
834 * Schedule @fn(@data) for execution after @delay jiffies using
835 * port_task. There is one port_task per port and it's the
836 * user(low level driver)'s responsibility to make sure that only
837 * one task is active at any given time.
839 * libata core layer takes care of synchronization between
840 * port_task and EH. ata_port_queue_task() may be ignored for EH
844 * Inherited from caller.
846 void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
851 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
854 PREPARE_WORK(&ap->port_task, fn, data);
857 rc = queue_work(ata_wq, &ap->port_task);
859 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
861 /* rc == 0 means that another user is using port task */
866 * ata_port_flush_task - Flush port_task
867 * @ap: The ata_port to flush port_task for
869 * After this function completes, port_task is guranteed not to
870 * be running or scheduled.
873 * Kernel thread context (may sleep)
875 void ata_port_flush_task(struct ata_port *ap)
881 spin_lock_irqsave(&ap->host_set->lock, flags);
882 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
883 spin_unlock_irqrestore(&ap->host_set->lock, flags);
885 DPRINTK("flush #1\n");
886 flush_workqueue(ata_wq);
889 * At this point, if a task is running, it's guaranteed to see
890 * the FLUSH flag; thus, it will never queue pio tasks again.
893 if (!cancel_delayed_work(&ap->port_task)) {
894 DPRINTK("flush #2\n");
895 flush_workqueue(ata_wq);
898 spin_lock_irqsave(&ap->host_set->lock, flags);
899 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
900 spin_unlock_irqrestore(&ap->host_set->lock, flags);
905 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
907 struct completion *waiting = qc->private_data;
909 qc->ap->ops->tf_read(qc->ap, &qc->tf);
914 * ata_exec_internal - execute libata internal command
915 * @ap: Port to which the command is sent
916 * @dev: Device to which the command is sent
917 * @tf: Taskfile registers for the command and the result
918 * @dma_dir: Data tranfer direction of the command
919 * @buf: Data buffer of the command
920 * @buflen: Length of data buffer
922 * Executes libata internal command with timeout. @tf contains
923 * command on entry and result on return. Timeout and error
924 * conditions are reported via return value. No recovery action
925 * is taken after a command times out. It's caller's duty to
926 * clean up after timeout.
929 * None. Should be called with kernel context, might sleep.
933 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
934 struct ata_taskfile *tf,
935 int dma_dir, void *buf, unsigned int buflen)
937 u8 command = tf->command;
938 struct ata_queued_cmd *qc;
939 DECLARE_COMPLETION(wait);
941 unsigned int err_mask;
943 spin_lock_irqsave(&ap->host_set->lock, flags);
945 qc = ata_qc_new_init(ap, dev);
949 qc->dma_dir = dma_dir;
950 if (dma_dir != DMA_NONE) {
951 ata_sg_init_one(qc, buf, buflen);
952 qc->nsect = buflen / ATA_SECT_SIZE;
955 qc->private_data = &wait;
956 qc->complete_fn = ata_qc_complete_internal;
958 qc->err_mask = ata_qc_issue(qc);
962 spin_unlock_irqrestore(&ap->host_set->lock, flags);
964 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
965 ata_port_flush_task(ap);
967 spin_lock_irqsave(&ap->host_set->lock, flags);
969 /* We're racing with irq here. If we lose, the
970 * following test prevents us from completing the qc
971 * again. If completion irq occurs after here but
972 * before the caller cleans up, it will result in a
973 * spurious interrupt. We can live with that.
975 if (qc->flags & ATA_QCFLAG_ACTIVE) {
976 qc->err_mask = AC_ERR_TIMEOUT;
978 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
982 spin_unlock_irqrestore(&ap->host_set->lock, flags);
986 err_mask = qc->err_mask;
994 * ata_pio_need_iordy - check if iordy needed
997 * Check if the current speed of the device requires IORDY. Used
998 * by various controllers for chip configuration.
1001 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1004 int speed = adev->pio_mode - XFER_PIO_0;
1011 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1013 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1014 pio = adev->id[ATA_ID_EIDE_PIO];
1015 /* Is the speed faster than the drive allows non IORDY ? */
1017 /* This is cycle times not frequency - watch the logic! */
1018 if (pio > 240) /* PIO2 is 240nS per cycle */
1027 * ata_dev_read_id - Read ID data from the specified device
1028 * @ap: port on which target device resides
1029 * @dev: target device
1030 * @p_class: pointer to class of the target device (may be changed)
1031 * @post_reset: is this read ID post-reset?
1032 * @p_id: read IDENTIFY page (newly allocated)
1034 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1035 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1036 * devices. This function also takes care of EDD signature
1037 * misreporting (to be removed once EDD support is gone) and
1038 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
1041 * Kernel thread context (may sleep)
1044 * 0 on success, -errno otherwise.
1046 static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
1047 unsigned int *p_class, int post_reset, u16 **p_id)
1049 unsigned int class = *p_class;
1050 unsigned int using_edd;
1051 struct ata_taskfile tf;
1052 unsigned int err_mask = 0;
1057 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1059 if (ap->ops->probe_reset ||
1060 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1065 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1067 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1070 reason = "out of memory";
1075 ata_tf_init(ap, &tf, dev->devno);
1079 tf.command = ATA_CMD_ID_ATA;
1082 tf.command = ATA_CMD_ID_ATAPI;
1086 reason = "unsupported class";
1090 tf.protocol = ATA_PROT_PIO;
1092 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1093 id, sizeof(id[0]) * ATA_ID_WORDS);
1097 reason = "I/O error";
1099 if (err_mask & ~AC_ERR_DEV)
1103 * arg! EDD works for all test cases, but seems to return
1104 * the ATA signature for some ATAPI devices. Until the
1105 * reason for this is found and fixed, we fix up the mess
1106 * here. If IDENTIFY DEVICE returns command aborted
1107 * (as ATAPI devices do), then we issue an
1108 * IDENTIFY PACKET DEVICE.
1110 * ATA software reset (SRST, the default) does not appear
1111 * to have this problem.
1113 if ((using_edd) && (class == ATA_DEV_ATA)) {
1114 u8 err = tf.feature;
1115 if (err & ATA_ABORTED) {
1116 class = ATA_DEV_ATAPI;
1123 swap_buf_le16(id, ATA_ID_WORDS);
1126 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1128 reason = "device reports illegal type";
1132 if (post_reset && class == ATA_DEV_ATA) {
1134 * The exact sequence expected by certain pre-ATA4 drives is:
1137 * INITIALIZE DEVICE PARAMETERS
1139 * Some drives were very specific about that exact sequence.
1141 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1142 err_mask = ata_dev_init_params(ap, dev);
1145 reason = "INIT_DEV_PARAMS failed";
1149 /* current CHS translation info (id[53-58]) might be
1150 * changed. reread the identify device info.
1162 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1163 ap->id, dev->devno, reason);
1168 static inline u8 ata_dev_knobble(const struct ata_port *ap,
1169 struct ata_device *dev)
1171 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1175 * ata_dev_configure - Configure the specified ATA/ATAPI device
1176 * @ap: Port on which target device resides
1177 * @dev: Target device to configure
1178 * @print_info: Enable device info printout
1180 * Configure @dev according to @dev->id. Generic and low-level
1181 * driver specific fixups are also applied.
1184 * Kernel thread context (may sleep)
1187 * 0 on success, -errno otherwise
1189 static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1192 const u16 *id = dev->id;
1193 unsigned int xfer_mask;
1196 if (!ata_dev_present(dev)) {
1197 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1198 ap->id, dev->devno);
1202 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1204 /* print device capabilities */
1206 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1207 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1208 ap->id, dev->devno, id[49], id[82], id[83],
1209 id[84], id[85], id[86], id[87], id[88]);
1211 /* initialize to-be-configured parameters */
1213 dev->max_sectors = 0;
1221 * common ATA, ATAPI feature tests
1224 /* we require DMA support (bits 8 of word 49) */
1225 if (!ata_id_has_dma(id)) {
1226 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1231 /* find max transfer mode; for printk only */
1232 xfer_mask = ata_id_xfermask(id);
1236 /* ATA-specific feature tests */
1237 if (dev->class == ATA_DEV_ATA) {
1238 dev->n_sectors = ata_id_n_sectors(id);
1240 if (ata_id_has_lba(id)) {
1241 const char *lba_desc;
1244 dev->flags |= ATA_DFLAG_LBA;
1245 if (ata_id_has_lba48(id)) {
1246 dev->flags |= ATA_DFLAG_LBA48;
1250 /* print device info to dmesg */
1252 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1253 "max %s, %Lu sectors: %s\n",
1255 ata_id_major_version(id),
1256 ata_mode_string(xfer_mask),
1257 (unsigned long long)dev->n_sectors,
1262 /* Default translation */
1263 dev->cylinders = id[1];
1265 dev->sectors = id[6];
1267 if (ata_id_current_chs_valid(id)) {
1268 /* Current CHS translation is valid. */
1269 dev->cylinders = id[54];
1270 dev->heads = id[55];
1271 dev->sectors = id[56];
1274 /* print device info to dmesg */
1276 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1277 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1279 ata_id_major_version(id),
1280 ata_mode_string(xfer_mask),
1281 (unsigned long long)dev->n_sectors,
1282 dev->cylinders, dev->heads, dev->sectors);
1288 /* ATAPI-specific feature tests */
1289 else if (dev->class == ATA_DEV_ATAPI) {
1290 rc = atapi_cdb_len(id);
1291 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1292 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1296 dev->cdb_len = (unsigned int) rc;
1298 /* print device info to dmesg */
1300 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1301 ap->id, dev->devno, ata_mode_string(xfer_mask));
1304 ap->host->max_cmd_len = 0;
1305 for (i = 0; i < ATA_MAX_DEVICES; i++)
1306 ap->host->max_cmd_len = max_t(unsigned int,
1307 ap->host->max_cmd_len,
1308 ap->device[i].cdb_len);
1310 /* limit bridge transfers to udma5, 200 sectors */
1311 if (ata_dev_knobble(ap, dev)) {
1313 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1314 ap->id, dev->devno);
1315 ap->udma_mask &= ATA_UDMA5;
1316 dev->max_sectors = ATA_MAX_SECTORS;
1319 if (ap->ops->dev_config)
1320 ap->ops->dev_config(ap, dev);
1322 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1326 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1327 ap->id, dev->devno);
1328 DPRINTK("EXIT, err\n");
1333 * ata_bus_probe - Reset and probe ATA bus
1336 * Master ATA bus probing function. Initiates a hardware-dependent
1337 * bus reset, then attempts to identify any devices found on
1341 * PCI/etc. bus probe sem.
1344 * Zero on success, non-zero on error.
1347 static int ata_bus_probe(struct ata_port *ap)
1349 unsigned int classes[ATA_MAX_DEVICES];
1350 unsigned int i, rc, found = 0;
1354 /* reset and determine device classes */
1355 for (i = 0; i < ATA_MAX_DEVICES; i++)
1356 classes[i] = ATA_DEV_UNKNOWN;
1358 if (ap->ops->probe_reset) {
1359 rc = ap->ops->probe_reset(ap, classes);
1361 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1365 ap->ops->phy_reset(ap);
1367 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1368 for (i = 0; i < ATA_MAX_DEVICES; i++)
1369 classes[i] = ap->device[i].class;
1374 for (i = 0; i < ATA_MAX_DEVICES; i++)
1375 if (classes[i] == ATA_DEV_UNKNOWN)
1376 classes[i] = ATA_DEV_NONE;
1378 /* read IDENTIFY page and configure devices */
1379 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1380 struct ata_device *dev = &ap->device[i];
1382 dev->class = classes[i];
1384 if (!ata_dev_present(dev))
1387 WARN_ON(dev->id != NULL);
1388 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1389 dev->class = ATA_DEV_NONE;
1393 if (ata_dev_configure(ap, dev, 1)) {
1394 dev->class++; /* disable device */
1402 goto err_out_disable;
1405 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1406 goto err_out_disable;
1411 ap->ops->port_disable(ap);
1416 * ata_port_probe - Mark port as enabled
1417 * @ap: Port for which we indicate enablement
1419 * Modify @ap data structure such that the system
1420 * thinks that the entire port is enabled.
1422 * LOCKING: host_set lock, or some other form of
1426 void ata_port_probe(struct ata_port *ap)
1428 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1432 * sata_print_link_status - Print SATA link status
1433 * @ap: SATA port to printk link status about
1435 * This function prints link speed and status of a SATA link.
1440 static void sata_print_link_status(struct ata_port *ap)
1445 if (!ap->ops->scr_read)
1448 sstatus = scr_read(ap, SCR_STATUS);
1450 if (sata_dev_present(ap)) {
1451 tmp = (sstatus >> 4) & 0xf;
1454 else if (tmp & (1 << 1))
1457 speed = "<unknown>";
1458 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1459 ap->id, speed, sstatus);
1461 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1467 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1468 * @ap: SATA port associated with target SATA PHY.
1470 * This function issues commands to standard SATA Sxxx
1471 * PHY registers, to wake up the phy (and device), and
1472 * clear any reset condition.
1475 * PCI/etc. bus probe sem.
1478 void __sata_phy_reset(struct ata_port *ap)
1481 unsigned long timeout = jiffies + (HZ * 5);
1483 if (ap->flags & ATA_FLAG_SATA_RESET) {
1484 /* issue phy wake/reset */
1485 scr_write_flush(ap, SCR_CONTROL, 0x301);
1486 /* Couldn't find anything in SATA I/II specs, but
1487 * AHCI-1.1 10.4.2 says at least 1 ms. */
1490 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1492 /* wait for phy to become ready, if necessary */
1495 sstatus = scr_read(ap, SCR_STATUS);
1496 if ((sstatus & 0xf) != 1)
1498 } while (time_before(jiffies, timeout));
1500 /* print link status */
1501 sata_print_link_status(ap);
1503 /* TODO: phy layer with polling, timeouts, etc. */
1504 if (sata_dev_present(ap))
1507 ata_port_disable(ap);
1509 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1512 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1513 ata_port_disable(ap);
1517 ap->cbl = ATA_CBL_SATA;
1521 * sata_phy_reset - Reset SATA bus.
1522 * @ap: SATA port associated with target SATA PHY.
1524 * This function resets the SATA bus, and then probes
1525 * the bus for devices.
1528 * PCI/etc. bus probe sem.
1531 void sata_phy_reset(struct ata_port *ap)
1533 __sata_phy_reset(ap);
1534 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1540 * ata_port_disable - Disable port.
1541 * @ap: Port to be disabled.
1543 * Modify @ap data structure such that the system
1544 * thinks that the entire port is disabled, and should
1545 * never attempt to probe or communicate with devices
1548 * LOCKING: host_set lock, or some other form of
1552 void ata_port_disable(struct ata_port *ap)
1554 ap->device[0].class = ATA_DEV_NONE;
1555 ap->device[1].class = ATA_DEV_NONE;
1556 ap->flags |= ATA_FLAG_PORT_DISABLED;
1560 * This mode timing computation functionality is ported over from
1561 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1564 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1565 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1566 * for PIO 5, which is a nonstandard extension and UDMA6, which
1567 * is currently supported only by Maxtor drives.
1570 static const struct ata_timing ata_timing[] = {
1572 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1573 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1574 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1575 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1577 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1578 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1579 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1581 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1583 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1584 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1585 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1587 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1588 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1589 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1591 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1592 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1593 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1595 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1596 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1597 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1599 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1604 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1605 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1607 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1609 q->setup = EZ(t->setup * 1000, T);
1610 q->act8b = EZ(t->act8b * 1000, T);
1611 q->rec8b = EZ(t->rec8b * 1000, T);
1612 q->cyc8b = EZ(t->cyc8b * 1000, T);
1613 q->active = EZ(t->active * 1000, T);
1614 q->recover = EZ(t->recover * 1000, T);
1615 q->cycle = EZ(t->cycle * 1000, T);
1616 q->udma = EZ(t->udma * 1000, UT);
1619 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1620 struct ata_timing *m, unsigned int what)
1622 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1623 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1624 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1625 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1626 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1627 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1628 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1629 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1632 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1634 const struct ata_timing *t;
1636 for (t = ata_timing; t->mode != speed; t++)
1637 if (t->mode == 0xFF)
1642 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1643 struct ata_timing *t, int T, int UT)
1645 const struct ata_timing *s;
1646 struct ata_timing p;
1652 if (!(s = ata_timing_find_mode(speed)))
1655 memcpy(t, s, sizeof(*s));
1658 * If the drive is an EIDE drive, it can tell us it needs extended
1659 * PIO/MW_DMA cycle timing.
1662 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1663 memset(&p, 0, sizeof(p));
1664 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1665 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1666 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1667 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1668 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1670 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1674 * Convert the timing to bus clock counts.
1677 ata_timing_quantize(t, t, T, UT);
1680 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1681 * S.M.A.R.T * and some other commands. We have to ensure that the
1682 * DMA cycle timing is slower/equal than the fastest PIO timing.
1685 if (speed > XFER_PIO_4) {
1686 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1687 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1691 * Lengthen active & recovery time so that cycle time is correct.
1694 if (t->act8b + t->rec8b < t->cyc8b) {
1695 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1696 t->rec8b = t->cyc8b - t->act8b;
1699 if (t->active + t->recover < t->cycle) {
1700 t->active += (t->cycle - (t->active + t->recover)) / 2;
1701 t->recover = t->cycle - t->active;
1707 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1709 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1712 if (dev->xfer_shift == ATA_SHIFT_PIO)
1713 dev->flags |= ATA_DFLAG_PIO;
1715 ata_dev_set_xfermode(ap, dev);
1717 if (ata_dev_revalidate(ap, dev, 0)) {
1718 printk(KERN_ERR "ata%u: failed to revalidate after set "
1719 "xfermode, disabled\n", ap->id);
1720 ata_port_disable(ap);
1723 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1724 dev->xfer_shift, (int)dev->xfer_mode);
1726 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1728 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
1731 static int ata_host_set_pio(struct ata_port *ap)
1735 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1736 struct ata_device *dev = &ap->device[i];
1738 if (!ata_dev_present(dev))
1741 if (!dev->pio_mode) {
1742 printk(KERN_WARNING "ata%u: no PIO support for device %d.\n", ap->id, i);
1746 dev->xfer_mode = dev->pio_mode;
1747 dev->xfer_shift = ATA_SHIFT_PIO;
1748 if (ap->ops->set_piomode)
1749 ap->ops->set_piomode(ap, dev);
1755 static void ata_host_set_dma(struct ata_port *ap)
1759 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1760 struct ata_device *dev = &ap->device[i];
1762 if (!ata_dev_present(dev) || !dev->dma_mode)
1765 dev->xfer_mode = dev->dma_mode;
1766 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1767 if (ap->ops->set_dmamode)
1768 ap->ops->set_dmamode(ap, dev);
1773 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1774 * @ap: port on which timings will be programmed
1776 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1779 * PCI/etc. bus probe sem.
1781 static void ata_set_mode(struct ata_port *ap)
1785 /* step 1: calculate xfer_mask */
1786 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1787 struct ata_device *dev = &ap->device[i];
1788 unsigned int xfer_mask;
1790 if (!ata_dev_present(dev))
1793 xfer_mask = ata_dev_xfermask(ap, dev);
1795 dev->pio_mode = ata_xfer_mask2mode(xfer_mask & ATA_MASK_PIO);
1796 dev->dma_mode = ata_xfer_mask2mode(xfer_mask & (ATA_MASK_MWDMA |
1800 /* step 2: always set host PIO timings */
1801 rc = ata_host_set_pio(ap);
1805 /* step 3: set host DMA timings */
1806 ata_host_set_dma(ap);
1808 /* step 4: update devices' xfer mode */
1809 for (i = 0; i < ATA_MAX_DEVICES; i++)
1810 ata_dev_set_mode(ap, &ap->device[i]);
1812 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1815 if (ap->ops->post_set_mode)
1816 ap->ops->post_set_mode(ap);
1821 ata_port_disable(ap);
1825 * ata_tf_to_host - issue ATA taskfile to host controller
1826 * @ap: port to which command is being issued
1827 * @tf: ATA taskfile register set
1829 * Issues ATA taskfile register set to ATA host controller,
1830 * with proper synchronization with interrupt handler and
1834 * spin_lock_irqsave(host_set lock)
1837 static inline void ata_tf_to_host(struct ata_port *ap,
1838 const struct ata_taskfile *tf)
1840 ap->ops->tf_load(ap, tf);
1841 ap->ops->exec_command(ap, tf);
1845 * ata_busy_sleep - sleep until BSY clears, or timeout
1846 * @ap: port containing status register to be polled
1847 * @tmout_pat: impatience timeout
1848 * @tmout: overall timeout
1850 * Sleep until ATA Status register bit BSY clears,
1851 * or a timeout occurs.
1856 unsigned int ata_busy_sleep (struct ata_port *ap,
1857 unsigned long tmout_pat, unsigned long tmout)
1859 unsigned long timer_start, timeout;
1862 status = ata_busy_wait(ap, ATA_BUSY, 300);
1863 timer_start = jiffies;
1864 timeout = timer_start + tmout_pat;
1865 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1867 status = ata_busy_wait(ap, ATA_BUSY, 3);
1870 if (status & ATA_BUSY)
1871 printk(KERN_WARNING "ata%u is slow to respond, "
1872 "please be patient\n", ap->id);
1874 timeout = timer_start + tmout;
1875 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1877 status = ata_chk_status(ap);
1880 if (status & ATA_BUSY) {
1881 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1882 ap->id, tmout / HZ);
1889 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1891 struct ata_ioports *ioaddr = &ap->ioaddr;
1892 unsigned int dev0 = devmask & (1 << 0);
1893 unsigned int dev1 = devmask & (1 << 1);
1894 unsigned long timeout;
1896 /* if device 0 was found in ata_devchk, wait for its
1900 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1902 /* if device 1 was found in ata_devchk, wait for
1903 * register access, then wait for BSY to clear
1905 timeout = jiffies + ATA_TMOUT_BOOT;
1909 ap->ops->dev_select(ap, 1);
1910 if (ap->flags & ATA_FLAG_MMIO) {
1911 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1912 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1914 nsect = inb(ioaddr->nsect_addr);
1915 lbal = inb(ioaddr->lbal_addr);
1917 if ((nsect == 1) && (lbal == 1))
1919 if (time_after(jiffies, timeout)) {
1923 msleep(50); /* give drive a breather */
1926 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1928 /* is all this really necessary? */
1929 ap->ops->dev_select(ap, 0);
1931 ap->ops->dev_select(ap, 1);
1933 ap->ops->dev_select(ap, 0);
1937 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1938 * @ap: Port to reset and probe
1940 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1941 * probe the bus. Not often used these days.
1944 * PCI/etc. bus probe sem.
1945 * Obtains host_set lock.
1949 static unsigned int ata_bus_edd(struct ata_port *ap)
1951 struct ata_taskfile tf;
1952 unsigned long flags;
1954 /* set up execute-device-diag (bus reset) taskfile */
1955 /* also, take interrupts to a known state (disabled) */
1956 DPRINTK("execute-device-diag\n");
1957 ata_tf_init(ap, &tf, 0);
1959 tf.command = ATA_CMD_EDD;
1960 tf.protocol = ATA_PROT_NODATA;
1963 spin_lock_irqsave(&ap->host_set->lock, flags);
1964 ata_tf_to_host(ap, &tf);
1965 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1967 /* spec says at least 2ms. but who knows with those
1968 * crazy ATAPI devices...
1972 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1975 static unsigned int ata_bus_softreset(struct ata_port *ap,
1976 unsigned int devmask)
1978 struct ata_ioports *ioaddr = &ap->ioaddr;
1980 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1982 /* software reset. causes dev0 to be selected */
1983 if (ap->flags & ATA_FLAG_MMIO) {
1984 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1985 udelay(20); /* FIXME: flush */
1986 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1987 udelay(20); /* FIXME: flush */
1988 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1990 outb(ap->ctl, ioaddr->ctl_addr);
1992 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1994 outb(ap->ctl, ioaddr->ctl_addr);
1997 /* spec mandates ">= 2ms" before checking status.
1998 * We wait 150ms, because that was the magic delay used for
1999 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2000 * between when the ATA command register is written, and then
2001 * status is checked. Because waiting for "a while" before
2002 * checking status is fine, post SRST, we perform this magic
2003 * delay here as well.
2007 ata_bus_post_reset(ap, devmask);
2013 * ata_bus_reset - reset host port and associated ATA channel
2014 * @ap: port to reset
2016 * This is typically the first time we actually start issuing
2017 * commands to the ATA channel. We wait for BSY to clear, then
2018 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2019 * result. Determine what devices, if any, are on the channel
2020 * by looking at the device 0/1 error register. Look at the signature
2021 * stored in each device's taskfile registers, to determine if
2022 * the device is ATA or ATAPI.
2025 * PCI/etc. bus probe sem.
2026 * Obtains host_set lock.
2029 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2032 void ata_bus_reset(struct ata_port *ap)
2034 struct ata_ioports *ioaddr = &ap->ioaddr;
2035 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2037 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2039 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2041 /* determine if device 0/1 are present */
2042 if (ap->flags & ATA_FLAG_SATA_RESET)
2045 dev0 = ata_devchk(ap, 0);
2047 dev1 = ata_devchk(ap, 1);
2051 devmask |= (1 << 0);
2053 devmask |= (1 << 1);
2055 /* select device 0 again */
2056 ap->ops->dev_select(ap, 0);
2058 /* issue bus reset */
2059 if (ap->flags & ATA_FLAG_SRST)
2060 rc = ata_bus_softreset(ap, devmask);
2061 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2062 /* set up device control */
2063 if (ap->flags & ATA_FLAG_MMIO)
2064 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2066 outb(ap->ctl, ioaddr->ctl_addr);
2067 rc = ata_bus_edd(ap);
2074 * determine by signature whether we have ATA or ATAPI devices
2076 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
2077 if ((slave_possible) && (err != 0x81))
2078 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
2080 /* re-enable interrupts */
2081 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2084 /* is double-select really necessary? */
2085 if (ap->device[1].class != ATA_DEV_NONE)
2086 ap->ops->dev_select(ap, 1);
2087 if (ap->device[0].class != ATA_DEV_NONE)
2088 ap->ops->dev_select(ap, 0);
2090 /* if no devices were detected, disable this port */
2091 if ((ap->device[0].class == ATA_DEV_NONE) &&
2092 (ap->device[1].class == ATA_DEV_NONE))
2095 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2096 /* set up device control for ATA_FLAG_SATA_RESET */
2097 if (ap->flags & ATA_FLAG_MMIO)
2098 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2100 outb(ap->ctl, ioaddr->ctl_addr);
2107 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2108 ap->ops->port_disable(ap);
2113 static int sata_phy_resume(struct ata_port *ap)
2115 unsigned long timeout = jiffies + (HZ * 5);
2118 scr_write_flush(ap, SCR_CONTROL, 0x300);
2120 /* Wait for phy to become ready, if necessary. */
2123 sstatus = scr_read(ap, SCR_STATUS);
2124 if ((sstatus & 0xf) != 1)
2126 } while (time_before(jiffies, timeout));
2132 * ata_std_probeinit - initialize probing
2133 * @ap: port to be probed
2135 * @ap is about to be probed. Initialize it. This function is
2136 * to be used as standard callback for ata_drive_probe_reset().
2138 * NOTE!!! Do not use this function as probeinit if a low level
2139 * driver implements only hardreset. Just pass NULL as probeinit
2140 * in that case. Using this function is probably okay but doing
2141 * so makes reset sequence different from the original
2142 * ->phy_reset implementation and Jeff nervous. :-P
2144 extern void ata_std_probeinit(struct ata_port *ap)
2146 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
2147 sata_phy_resume(ap);
2148 if (sata_dev_present(ap))
2149 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2154 * ata_std_softreset - reset host port via ATA SRST
2155 * @ap: port to reset
2156 * @verbose: fail verbosely
2157 * @classes: resulting classes of attached devices
2159 * Reset host port using ATA SRST. This function is to be used
2160 * as standard callback for ata_drive_*_reset() functions.
2163 * Kernel thread context (may sleep)
2166 * 0 on success, -errno otherwise.
2168 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2170 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2171 unsigned int devmask = 0, err_mask;
2176 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2177 classes[0] = ATA_DEV_NONE;
2181 /* determine if device 0/1 are present */
2182 if (ata_devchk(ap, 0))
2183 devmask |= (1 << 0);
2184 if (slave_possible && ata_devchk(ap, 1))
2185 devmask |= (1 << 1);
2187 /* select device 0 again */
2188 ap->ops->dev_select(ap, 0);
2190 /* issue bus reset */
2191 DPRINTK("about to softreset, devmask=%x\n", devmask);
2192 err_mask = ata_bus_softreset(ap, devmask);
2195 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2198 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2203 /* determine by signature whether we have ATA or ATAPI devices */
2204 classes[0] = ata_dev_try_classify(ap, 0, &err);
2205 if (slave_possible && err != 0x81)
2206 classes[1] = ata_dev_try_classify(ap, 1, &err);
2209 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2214 * sata_std_hardreset - reset host port via SATA phy reset
2215 * @ap: port to reset
2216 * @verbose: fail verbosely
2217 * @class: resulting class of attached device
2219 * SATA phy-reset host port using DET bits of SControl register.
2220 * This function is to be used as standard callback for
2221 * ata_drive_*_reset().
2224 * Kernel thread context (may sleep)
2227 * 0 on success, -errno otherwise.
2229 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2233 /* Issue phy wake/reset */
2234 scr_write_flush(ap, SCR_CONTROL, 0x301);
2237 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2238 * 10.4.2 says at least 1 ms.
2242 /* Bring phy back */
2243 sata_phy_resume(ap);
2245 /* TODO: phy layer with polling, timeouts, etc. */
2246 if (!sata_dev_present(ap)) {
2247 *class = ATA_DEV_NONE;
2248 DPRINTK("EXIT, link offline\n");
2252 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2254 printk(KERN_ERR "ata%u: COMRESET failed "
2255 "(device not ready)\n", ap->id);
2257 DPRINTK("EXIT, device not ready\n");
2261 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2263 *class = ata_dev_try_classify(ap, 0, NULL);
2265 DPRINTK("EXIT, class=%u\n", *class);
2270 * ata_std_postreset - standard postreset callback
2271 * @ap: the target ata_port
2272 * @classes: classes of attached devices
2274 * This function is invoked after a successful reset. Note that
2275 * the device might have been reset more than once using
2276 * different reset methods before postreset is invoked.
2278 * This function is to be used as standard callback for
2279 * ata_drive_*_reset().
2282 * Kernel thread context (may sleep)
2284 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2288 /* set cable type if it isn't already set */
2289 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2290 ap->cbl = ATA_CBL_SATA;
2292 /* print link status */
2293 if (ap->cbl == ATA_CBL_SATA)
2294 sata_print_link_status(ap);
2296 /* re-enable interrupts */
2297 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2300 /* is double-select really necessary? */
2301 if (classes[0] != ATA_DEV_NONE)
2302 ap->ops->dev_select(ap, 1);
2303 if (classes[1] != ATA_DEV_NONE)
2304 ap->ops->dev_select(ap, 0);
2306 /* bail out if no device is present */
2307 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2308 DPRINTK("EXIT, no device\n");
2312 /* set up device control */
2313 if (ap->ioaddr.ctl_addr) {
2314 if (ap->flags & ATA_FLAG_MMIO)
2315 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2317 outb(ap->ctl, ap->ioaddr.ctl_addr);
2324 * ata_std_probe_reset - standard probe reset method
2325 * @ap: prot to perform probe-reset
2326 * @classes: resulting classes of attached devices
2328 * The stock off-the-shelf ->probe_reset method.
2331 * Kernel thread context (may sleep)
2334 * 0 on success, -errno otherwise.
2336 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2338 ata_reset_fn_t hardreset;
2341 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2342 hardreset = sata_std_hardreset;
2344 return ata_drive_probe_reset(ap, ata_std_probeinit,
2345 ata_std_softreset, hardreset,
2346 ata_std_postreset, classes);
2349 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2350 ata_postreset_fn_t postreset,
2351 unsigned int *classes)
2355 for (i = 0; i < ATA_MAX_DEVICES; i++)
2356 classes[i] = ATA_DEV_UNKNOWN;
2358 rc = reset(ap, 0, classes);
2362 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2363 * is complete and convert all ATA_DEV_UNKNOWN to
2366 for (i = 0; i < ATA_MAX_DEVICES; i++)
2367 if (classes[i] != ATA_DEV_UNKNOWN)
2370 if (i < ATA_MAX_DEVICES)
2371 for (i = 0; i < ATA_MAX_DEVICES; i++)
2372 if (classes[i] == ATA_DEV_UNKNOWN)
2373 classes[i] = ATA_DEV_NONE;
2376 postreset(ap, classes);
2378 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2382 * ata_drive_probe_reset - Perform probe reset with given methods
2383 * @ap: port to reset
2384 * @probeinit: probeinit method (can be NULL)
2385 * @softreset: softreset method (can be NULL)
2386 * @hardreset: hardreset method (can be NULL)
2387 * @postreset: postreset method (can be NULL)
2388 * @classes: resulting classes of attached devices
2390 * Reset the specified port and classify attached devices using
2391 * given methods. This function prefers softreset but tries all
2392 * possible reset sequences to reset and classify devices. This
2393 * function is intended to be used for constructing ->probe_reset
2394 * callback by low level drivers.
2396 * Reset methods should follow the following rules.
2398 * - Return 0 on sucess, -errno on failure.
2399 * - If classification is supported, fill classes[] with
2400 * recognized class codes.
2401 * - If classification is not supported, leave classes[] alone.
2402 * - If verbose is non-zero, print error message on failure;
2403 * otherwise, shut up.
2406 * Kernel thread context (may sleep)
2409 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2410 * if classification fails, and any error code from reset
2413 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2414 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2415 ata_postreset_fn_t postreset, unsigned int *classes)
2423 rc = do_probe_reset(ap, softreset, postreset, classes);
2431 rc = do_probe_reset(ap, hardreset, postreset, classes);
2432 if (rc == 0 || rc != -ENODEV)
2436 rc = do_probe_reset(ap, softreset, postreset, classes);
2442 * ata_dev_same_device - Determine whether new ID matches configured device
2443 * @ap: port on which the device to compare against resides
2444 * @dev: device to compare against
2445 * @new_class: class of the new device
2446 * @new_id: IDENTIFY page of the new device
2448 * Compare @new_class and @new_id against @dev and determine
2449 * whether @dev is the device indicated by @new_class and
2456 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2458 static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2459 unsigned int new_class, const u16 *new_id)
2461 const u16 *old_id = dev->id;
2462 unsigned char model[2][41], serial[2][21];
2465 if (dev->class != new_class) {
2467 "ata%u: dev %u class mismatch %d != %d\n",
2468 ap->id, dev->devno, dev->class, new_class);
2472 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2473 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2474 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2475 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2476 new_n_sectors = ata_id_n_sectors(new_id);
2478 if (strcmp(model[0], model[1])) {
2480 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2481 ap->id, dev->devno, model[0], model[1]);
2485 if (strcmp(serial[0], serial[1])) {
2487 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2488 ap->id, dev->devno, serial[0], serial[1]);
2492 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2494 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2495 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2496 (unsigned long long)new_n_sectors);
2504 * ata_dev_revalidate - Revalidate ATA device
2505 * @ap: port on which the device to revalidate resides
2506 * @dev: device to revalidate
2507 * @post_reset: is this revalidation after reset?
2509 * Re-read IDENTIFY page and make sure @dev is still attached to
2513 * Kernel thread context (may sleep)
2516 * 0 on success, negative errno otherwise
2518 int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2525 if (!ata_dev_present(dev))
2531 /* allocate & read ID data */
2532 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2536 /* is the device still there? */
2537 if (!ata_dev_same_device(ap, dev, class, id)) {
2545 /* configure device according to the new ID */
2546 return ata_dev_configure(ap, dev, 0);
2549 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2550 ap->id, dev->devno, rc);
2555 static const char * const ata_dma_blacklist [] = {
2574 "Toshiba CD-ROM XM-6202B",
2575 "TOSHIBA CD-ROM XM-1702BC",
2577 "E-IDE CD-ROM CR-840",
2580 "SAMSUNG CD-ROM SC-148C",
2581 "SAMSUNG CD-ROM SC",
2583 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2587 static int ata_dma_blacklisted(const struct ata_device *dev)
2589 unsigned char model_num[41];
2592 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
2594 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2595 if (!strcmp(ata_dma_blacklist[i], model_num))
2602 * ata_dev_xfermask - Compute supported xfermask of the given device
2603 * @ap: Port on which the device to compute xfermask for resides
2604 * @dev: Device to compute xfermask for
2606 * Compute supported xfermask of @dev. This function is
2607 * responsible for applying all known limits including host
2608 * controller limits, device blacklist, etc...
2614 * Computed xfermask.
2616 static unsigned int ata_dev_xfermask(struct ata_port *ap,
2617 struct ata_device *dev)
2619 unsigned long xfer_mask;
2622 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2625 /* use port-wide xfermask for now */
2626 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2627 struct ata_device *d = &ap->device[i];
2628 if (!ata_dev_present(d))
2630 xfer_mask &= ata_id_xfermask(d->id);
2631 if (ata_dma_blacklisted(d))
2632 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2635 if (ata_dma_blacklisted(dev))
2636 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2637 "disabling DMA\n", ap->id, dev->devno);
2643 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2644 * @ap: Port associated with device @dev
2645 * @dev: Device to which command will be sent
2647 * Issue SET FEATURES - XFER MODE command to device @dev
2651 * PCI/etc. bus probe sem.
2654 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2656 struct ata_taskfile tf;
2658 /* set up set-features taskfile */
2659 DPRINTK("set features - xfer mode\n");
2661 ata_tf_init(ap, &tf, dev->devno);
2662 tf.command = ATA_CMD_SET_FEATURES;
2663 tf.feature = SETFEATURES_XFER;
2664 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2665 tf.protocol = ATA_PROT_NODATA;
2666 tf.nsect = dev->xfer_mode;
2668 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2669 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2671 ata_port_disable(ap);
2678 * ata_dev_init_params - Issue INIT DEV PARAMS command
2679 * @ap: Port associated with device @dev
2680 * @dev: Device to which command will be sent
2683 * Kernel thread context (may sleep)
2686 * 0 on success, AC_ERR_* mask otherwise.
2689 static unsigned int ata_dev_init_params(struct ata_port *ap,
2690 struct ata_device *dev)
2692 struct ata_taskfile tf;
2693 unsigned int err_mask;
2694 u16 sectors = dev->id[6];
2695 u16 heads = dev->id[3];
2697 /* Number of sectors per track 1-255. Number of heads 1-16 */
2698 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2701 /* set up init dev params taskfile */
2702 DPRINTK("init dev params \n");
2704 ata_tf_init(ap, &tf, dev->devno);
2705 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2706 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2707 tf.protocol = ATA_PROT_NODATA;
2709 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2711 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2713 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2718 * ata_sg_clean - Unmap DMA memory associated with command
2719 * @qc: Command containing DMA memory to be released
2721 * Unmap all mapped DMA memory associated with this command.
2724 * spin_lock_irqsave(host_set lock)
2727 static void ata_sg_clean(struct ata_queued_cmd *qc)
2729 struct ata_port *ap = qc->ap;
2730 struct scatterlist *sg = qc->__sg;
2731 int dir = qc->dma_dir;
2732 void *pad_buf = NULL;
2734 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2735 WARN_ON(sg == NULL);
2737 if (qc->flags & ATA_QCFLAG_SINGLE)
2738 WARN_ON(qc->n_elem > 1);
2740 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2742 /* if we padded the buffer out to 32-bit bound, and data
2743 * xfer direction is from-device, we must copy from the
2744 * pad buffer back into the supplied buffer
2746 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2747 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2749 if (qc->flags & ATA_QCFLAG_SG) {
2751 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2752 /* restore last sg */
2753 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2755 struct scatterlist *psg = &qc->pad_sgent;
2756 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2757 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2758 kunmap_atomic(addr, KM_IRQ0);
2762 dma_unmap_single(ap->host_set->dev,
2763 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2766 sg->length += qc->pad_len;
2768 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2769 pad_buf, qc->pad_len);
2772 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2777 * ata_fill_sg - Fill PCI IDE PRD table
2778 * @qc: Metadata associated with taskfile to be transferred
2780 * Fill PCI IDE PRD (scatter-gather) table with segments
2781 * associated with the current disk command.
2784 * spin_lock_irqsave(host_set lock)
2787 static void ata_fill_sg(struct ata_queued_cmd *qc)
2789 struct ata_port *ap = qc->ap;
2790 struct scatterlist *sg;
2793 WARN_ON(qc->__sg == NULL);
2794 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2797 ata_for_each_sg(sg, qc) {
2801 /* determine if physical DMA addr spans 64K boundary.
2802 * Note h/w doesn't support 64-bit, so we unconditionally
2803 * truncate dma_addr_t to u32.
2805 addr = (u32) sg_dma_address(sg);
2806 sg_len = sg_dma_len(sg);
2809 offset = addr & 0xffff;
2811 if ((offset + sg_len) > 0x10000)
2812 len = 0x10000 - offset;
2814 ap->prd[idx].addr = cpu_to_le32(addr);
2815 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2816 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2825 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2828 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2829 * @qc: Metadata associated with taskfile to check
2831 * Allow low-level driver to filter ATA PACKET commands, returning
2832 * a status indicating whether or not it is OK to use DMA for the
2833 * supplied PACKET command.
2836 * spin_lock_irqsave(host_set lock)
2838 * RETURNS: 0 when ATAPI DMA can be used
2841 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2843 struct ata_port *ap = qc->ap;
2844 int rc = 0; /* Assume ATAPI DMA is OK by default */
2846 if (ap->ops->check_atapi_dma)
2847 rc = ap->ops->check_atapi_dma(qc);
2852 * ata_qc_prep - Prepare taskfile for submission
2853 * @qc: Metadata associated with taskfile to be prepared
2855 * Prepare ATA taskfile for submission.
2858 * spin_lock_irqsave(host_set lock)
2860 void ata_qc_prep(struct ata_queued_cmd *qc)
2862 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2868 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
2871 * ata_sg_init_one - Associate command with memory buffer
2872 * @qc: Command to be associated
2873 * @buf: Memory buffer
2874 * @buflen: Length of memory buffer, in bytes.
2876 * Initialize the data-related elements of queued_cmd @qc
2877 * to point to a single memory buffer, @buf of byte length @buflen.
2880 * spin_lock_irqsave(host_set lock)
2883 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2885 struct scatterlist *sg;
2887 qc->flags |= ATA_QCFLAG_SINGLE;
2889 memset(&qc->sgent, 0, sizeof(qc->sgent));
2890 qc->__sg = &qc->sgent;
2892 qc->orig_n_elem = 1;
2896 sg_init_one(sg, buf, buflen);
2900 * ata_sg_init - Associate command with scatter-gather table.
2901 * @qc: Command to be associated
2902 * @sg: Scatter-gather table.
2903 * @n_elem: Number of elements in s/g table.
2905 * Initialize the data-related elements of queued_cmd @qc
2906 * to point to a scatter-gather table @sg, containing @n_elem
2910 * spin_lock_irqsave(host_set lock)
2913 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2914 unsigned int n_elem)
2916 qc->flags |= ATA_QCFLAG_SG;
2918 qc->n_elem = n_elem;
2919 qc->orig_n_elem = n_elem;
2923 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2924 * @qc: Command with memory buffer to be mapped.
2926 * DMA-map the memory buffer associated with queued_cmd @qc.
2929 * spin_lock_irqsave(host_set lock)
2932 * Zero on success, negative on error.
2935 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2937 struct ata_port *ap = qc->ap;
2938 int dir = qc->dma_dir;
2939 struct scatterlist *sg = qc->__sg;
2940 dma_addr_t dma_address;
2943 /* we must lengthen transfers to end on a 32-bit boundary */
2944 qc->pad_len = sg->length & 3;
2946 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2947 struct scatterlist *psg = &qc->pad_sgent;
2949 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2951 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2953 if (qc->tf.flags & ATA_TFLAG_WRITE)
2954 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2957 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2958 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2960 sg->length -= qc->pad_len;
2961 if (sg->length == 0)
2964 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2965 sg->length, qc->pad_len);
2973 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2975 if (dma_mapping_error(dma_address)) {
2977 sg->length += qc->pad_len;
2981 sg_dma_address(sg) = dma_address;
2982 sg_dma_len(sg) = sg->length;
2985 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2986 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2992 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2993 * @qc: Command with scatter-gather table to be mapped.
2995 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2998 * spin_lock_irqsave(host_set lock)
3001 * Zero on success, negative on error.
3005 static int ata_sg_setup(struct ata_queued_cmd *qc)
3007 struct ata_port *ap = qc->ap;
3008 struct scatterlist *sg = qc->__sg;
3009 struct scatterlist *lsg = &sg[qc->n_elem - 1];
3010 int n_elem, pre_n_elem, dir, trim_sg = 0;
3012 VPRINTK("ENTER, ata%u\n", ap->id);
3013 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
3015 /* we must lengthen transfers to end on a 32-bit boundary */
3016 qc->pad_len = lsg->length & 3;
3018 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3019 struct scatterlist *psg = &qc->pad_sgent;
3020 unsigned int offset;
3022 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3024 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3027 * psg->page/offset are used to copy to-be-written
3028 * data in this function or read data in ata_sg_clean.
3030 offset = lsg->offset + lsg->length - qc->pad_len;
3031 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3032 psg->offset = offset_in_page(offset);
3034 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3035 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3036 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
3037 kunmap_atomic(addr, KM_IRQ0);
3040 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3041 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3043 lsg->length -= qc->pad_len;
3044 if (lsg->length == 0)
3047 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3048 qc->n_elem - 1, lsg->length, qc->pad_len);
3051 pre_n_elem = qc->n_elem;
3052 if (trim_sg && pre_n_elem)
3061 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
3063 /* restore last sg */
3064 lsg->length += qc->pad_len;
3068 DPRINTK("%d sg elements mapped\n", n_elem);
3071 qc->n_elem = n_elem;
3077 * ata_poll_qc_complete - turn irq back on and finish qc
3078 * @qc: Command to complete
3079 * @err_mask: ATA status register content
3082 * None. (grabs host lock)
3085 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
3087 struct ata_port *ap = qc->ap;
3088 unsigned long flags;
3090 spin_lock_irqsave(&ap->host_set->lock, flags);
3091 ap->flags &= ~ATA_FLAG_NOINTR;
3093 ata_qc_complete(qc);
3094 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3098 * ata_pio_poll - poll using PIO, depending on current state
3099 * @ap: the target ata_port
3102 * None. (executing in kernel thread context)
3105 * timeout value to use
3108 static unsigned long ata_pio_poll(struct ata_port *ap)
3110 struct ata_queued_cmd *qc;
3112 unsigned int poll_state = HSM_ST_UNKNOWN;
3113 unsigned int reg_state = HSM_ST_UNKNOWN;
3115 qc = ata_qc_from_tag(ap, ap->active_tag);
3116 WARN_ON(qc == NULL);
3118 switch (ap->hsm_task_state) {
3121 poll_state = HSM_ST_POLL;
3125 case HSM_ST_LAST_POLL:
3126 poll_state = HSM_ST_LAST_POLL;
3127 reg_state = HSM_ST_LAST;
3134 status = ata_chk_status(ap);
3135 if (status & ATA_BUSY) {
3136 if (time_after(jiffies, ap->pio_task_timeout)) {
3137 qc->err_mask |= AC_ERR_TIMEOUT;
3138 ap->hsm_task_state = HSM_ST_TMOUT;
3141 ap->hsm_task_state = poll_state;
3142 return ATA_SHORT_PAUSE;
3145 ap->hsm_task_state = reg_state;
3150 * ata_pio_complete - check if drive is busy or idle
3151 * @ap: the target ata_port
3154 * None. (executing in kernel thread context)
3157 * Non-zero if qc completed, zero otherwise.
3160 static int ata_pio_complete (struct ata_port *ap)
3162 struct ata_queued_cmd *qc;
3166 * This is purely heuristic. This is a fast path. Sometimes when
3167 * we enter, BSY will be cleared in a chk-status or two. If not,
3168 * the drive is probably seeking or something. Snooze for a couple
3169 * msecs, then chk-status again. If still busy, fall back to
3170 * HSM_ST_POLL state.
3172 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3173 if (drv_stat & ATA_BUSY) {
3175 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3176 if (drv_stat & ATA_BUSY) {
3177 ap->hsm_task_state = HSM_ST_LAST_POLL;
3178 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3183 qc = ata_qc_from_tag(ap, ap->active_tag);
3184 WARN_ON(qc == NULL);
3186 drv_stat = ata_wait_idle(ap);
3187 if (!ata_ok(drv_stat)) {
3188 qc->err_mask |= __ac_err_mask(drv_stat);
3189 ap->hsm_task_state = HSM_ST_ERR;
3193 ap->hsm_task_state = HSM_ST_IDLE;
3195 WARN_ON(qc->err_mask);
3196 ata_poll_qc_complete(qc);
3198 /* another command may start at this point */
3205 * swap_buf_le16 - swap halves of 16-bit words in place
3206 * @buf: Buffer to swap
3207 * @buf_words: Number of 16-bit words in buffer.
3209 * Swap halves of 16-bit words if needed to convert from
3210 * little-endian byte order to native cpu byte order, or
3214 * Inherited from caller.
3216 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3221 for (i = 0; i < buf_words; i++)
3222 buf[i] = le16_to_cpu(buf[i]);
3223 #endif /* __BIG_ENDIAN */
3227 * ata_mmio_data_xfer - Transfer data by MMIO
3228 * @ap: port to read/write
3230 * @buflen: buffer length
3231 * @write_data: read/write
3233 * Transfer data from/to the device data register by MMIO.
3236 * Inherited from caller.
3239 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3240 unsigned int buflen, int write_data)
3243 unsigned int words = buflen >> 1;
3244 u16 *buf16 = (u16 *) buf;
3245 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3247 /* Transfer multiple of 2 bytes */
3249 for (i = 0; i < words; i++)
3250 writew(le16_to_cpu(buf16[i]), mmio);
3252 for (i = 0; i < words; i++)
3253 buf16[i] = cpu_to_le16(readw(mmio));
3256 /* Transfer trailing 1 byte, if any. */
3257 if (unlikely(buflen & 0x01)) {
3258 u16 align_buf[1] = { 0 };
3259 unsigned char *trailing_buf = buf + buflen - 1;
3262 memcpy(align_buf, trailing_buf, 1);
3263 writew(le16_to_cpu(align_buf[0]), mmio);
3265 align_buf[0] = cpu_to_le16(readw(mmio));
3266 memcpy(trailing_buf, align_buf, 1);
3272 * ata_pio_data_xfer - Transfer data by PIO
3273 * @ap: port to read/write
3275 * @buflen: buffer length
3276 * @write_data: read/write
3278 * Transfer data from/to the device data register by PIO.
3281 * Inherited from caller.
3284 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3285 unsigned int buflen, int write_data)
3287 unsigned int words = buflen >> 1;
3289 /* Transfer multiple of 2 bytes */
3291 outsw(ap->ioaddr.data_addr, buf, words);
3293 insw(ap->ioaddr.data_addr, buf, words);
3295 /* Transfer trailing 1 byte, if any. */
3296 if (unlikely(buflen & 0x01)) {
3297 u16 align_buf[1] = { 0 };
3298 unsigned char *trailing_buf = buf + buflen - 1;
3301 memcpy(align_buf, trailing_buf, 1);
3302 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3304 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3305 memcpy(trailing_buf, align_buf, 1);
3311 * ata_data_xfer - Transfer data from/to the data register.
3312 * @ap: port to read/write
3314 * @buflen: buffer length
3315 * @do_write: read/write
3317 * Transfer data from/to the device data register.
3320 * Inherited from caller.
3323 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3324 unsigned int buflen, int do_write)
3326 /* Make the crap hardware pay the costs not the good stuff */
3327 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3328 unsigned long flags;
3329 local_irq_save(flags);
3330 if (ap->flags & ATA_FLAG_MMIO)
3331 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3333 ata_pio_data_xfer(ap, buf, buflen, do_write);
3334 local_irq_restore(flags);
3336 if (ap->flags & ATA_FLAG_MMIO)
3337 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3339 ata_pio_data_xfer(ap, buf, buflen, do_write);
3344 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3345 * @qc: Command on going
3347 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3350 * Inherited from caller.
3353 static void ata_pio_sector(struct ata_queued_cmd *qc)
3355 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3356 struct scatterlist *sg = qc->__sg;
3357 struct ata_port *ap = qc->ap;
3359 unsigned int offset;
3362 if (qc->cursect == (qc->nsect - 1))
3363 ap->hsm_task_state = HSM_ST_LAST;
3365 page = sg[qc->cursg].page;
3366 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3368 /* get the current page and offset */
3369 page = nth_page(page, (offset >> PAGE_SHIFT));
3370 offset %= PAGE_SIZE;
3372 buf = kmap(page) + offset;
3377 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3382 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3384 /* do the actual data transfer */
3385 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3386 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3392 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3393 * @qc: Command on going
3394 * @bytes: number of bytes
3396 * Transfer Transfer data from/to the ATAPI device.
3399 * Inherited from caller.
3403 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3405 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3406 struct scatterlist *sg = qc->__sg;
3407 struct ata_port *ap = qc->ap;
3410 unsigned int offset, count;
3412 if (qc->curbytes + bytes >= qc->nbytes)
3413 ap->hsm_task_state = HSM_ST_LAST;
3416 if (unlikely(qc->cursg >= qc->n_elem)) {
3418 * The end of qc->sg is reached and the device expects
3419 * more data to transfer. In order not to overrun qc->sg
3420 * and fulfill length specified in the byte count register,
3421 * - for read case, discard trailing data from the device
3422 * - for write case, padding zero data to the device
3424 u16 pad_buf[1] = { 0 };
3425 unsigned int words = bytes >> 1;
3428 if (words) /* warning if bytes > 1 */
3429 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3432 for (i = 0; i < words; i++)
3433 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3435 ap->hsm_task_state = HSM_ST_LAST;
3439 sg = &qc->__sg[qc->cursg];
3442 offset = sg->offset + qc->cursg_ofs;
3444 /* get the current page and offset */
3445 page = nth_page(page, (offset >> PAGE_SHIFT));
3446 offset %= PAGE_SIZE;
3448 /* don't overrun current sg */
3449 count = min(sg->length - qc->cursg_ofs, bytes);
3451 /* don't cross page boundaries */
3452 count = min(count, (unsigned int)PAGE_SIZE - offset);
3454 buf = kmap(page) + offset;
3457 qc->curbytes += count;
3458 qc->cursg_ofs += count;
3460 if (qc->cursg_ofs == sg->length) {
3465 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3467 /* do the actual data transfer */
3468 ata_data_xfer(ap, buf, count, do_write);
3477 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3478 * @qc: Command on going
3480 * Transfer Transfer data from/to the ATAPI device.
3483 * Inherited from caller.
3486 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3488 struct ata_port *ap = qc->ap;
3489 struct ata_device *dev = qc->dev;
3490 unsigned int ireason, bc_lo, bc_hi, bytes;
3491 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3493 ap->ops->tf_read(ap, &qc->tf);
3494 ireason = qc->tf.nsect;
3495 bc_lo = qc->tf.lbam;
3496 bc_hi = qc->tf.lbah;
3497 bytes = (bc_hi << 8) | bc_lo;
3499 /* shall be cleared to zero, indicating xfer of data */
3500 if (ireason & (1 << 0))
3503 /* make sure transfer direction matches expected */
3504 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3505 if (do_write != i_write)
3508 __atapi_pio_bytes(qc, bytes);
3513 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3514 ap->id, dev->devno);
3515 qc->err_mask |= AC_ERR_HSM;
3516 ap->hsm_task_state = HSM_ST_ERR;
3520 * ata_pio_block - start PIO on a block
3521 * @ap: the target ata_port
3524 * None. (executing in kernel thread context)
3527 static void ata_pio_block(struct ata_port *ap)
3529 struct ata_queued_cmd *qc;
3533 * This is purely heuristic. This is a fast path.
3534 * Sometimes when we enter, BSY will be cleared in
3535 * a chk-status or two. If not, the drive is probably seeking
3536 * or something. Snooze for a couple msecs, then
3537 * chk-status again. If still busy, fall back to
3538 * HSM_ST_POLL state.
3540 status = ata_busy_wait(ap, ATA_BUSY, 5);
3541 if (status & ATA_BUSY) {
3543 status = ata_busy_wait(ap, ATA_BUSY, 10);
3544 if (status & ATA_BUSY) {
3545 ap->hsm_task_state = HSM_ST_POLL;
3546 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3551 qc = ata_qc_from_tag(ap, ap->active_tag);
3552 WARN_ON(qc == NULL);
3555 if (status & (ATA_ERR | ATA_DF)) {
3556 qc->err_mask |= AC_ERR_DEV;
3557 ap->hsm_task_state = HSM_ST_ERR;
3561 /* transfer data if any */
3562 if (is_atapi_taskfile(&qc->tf)) {
3563 /* DRQ=0 means no more data to transfer */
3564 if ((status & ATA_DRQ) == 0) {
3565 ap->hsm_task_state = HSM_ST_LAST;
3569 atapi_pio_bytes(qc);
3571 /* handle BSY=0, DRQ=0 as error */
3572 if ((status & ATA_DRQ) == 0) {
3573 qc->err_mask |= AC_ERR_HSM;
3574 ap->hsm_task_state = HSM_ST_ERR;
3582 static void ata_pio_error(struct ata_port *ap)
3584 struct ata_queued_cmd *qc;
3586 qc = ata_qc_from_tag(ap, ap->active_tag);
3587 WARN_ON(qc == NULL);
3589 if (qc->tf.command != ATA_CMD_PACKET)
3590 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3592 /* make sure qc->err_mask is available to
3593 * know what's wrong and recover
3595 WARN_ON(qc->err_mask == 0);
3597 ap->hsm_task_state = HSM_ST_IDLE;
3599 ata_poll_qc_complete(qc);
3602 static void ata_pio_task(void *_data)
3604 struct ata_port *ap = _data;
3605 unsigned long timeout;
3612 switch (ap->hsm_task_state) {
3621 qc_completed = ata_pio_complete(ap);
3625 case HSM_ST_LAST_POLL:
3626 timeout = ata_pio_poll(ap);
3636 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
3637 else if (!qc_completed)
3642 * atapi_packet_task - Write CDB bytes to hardware
3643 * @_data: Port to which ATAPI device is attached.
3645 * When device has indicated its readiness to accept
3646 * a CDB, this function is called. Send the CDB.
3647 * If DMA is to be performed, exit immediately.
3648 * Otherwise, we are in polling mode, so poll
3649 * status under operation succeeds or fails.
3652 * Kernel thread context (may sleep)
3655 static void atapi_packet_task(void *_data)
3657 struct ata_port *ap = _data;
3658 struct ata_queued_cmd *qc;
3661 qc = ata_qc_from_tag(ap, ap->active_tag);
3662 WARN_ON(qc == NULL);
3663 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3665 /* sleep-wait for BSY to clear */
3666 DPRINTK("busy wait\n");
3667 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3668 qc->err_mask |= AC_ERR_TIMEOUT;
3672 /* make sure DRQ is set */
3673 status = ata_chk_status(ap);
3674 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3675 qc->err_mask |= AC_ERR_HSM;
3680 DPRINTK("send cdb\n");
3681 WARN_ON(qc->dev->cdb_len < 12);
3683 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3684 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3685 unsigned long flags;
3687 /* Once we're done issuing command and kicking bmdma,
3688 * irq handler takes over. To not lose irq, we need
3689 * to clear NOINTR flag before sending cdb, but
3690 * interrupt handler shouldn't be invoked before we're
3691 * finished. Hence, the following locking.
3693 spin_lock_irqsave(&ap->host_set->lock, flags);
3694 ap->flags &= ~ATA_FLAG_NOINTR;
3695 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3696 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3697 ap->ops->bmdma_start(qc); /* initiate bmdma */
3698 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3700 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3702 /* PIO commands are handled by polling */
3703 ap->hsm_task_state = HSM_ST;
3704 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3710 ata_poll_qc_complete(qc);
3714 * ata_qc_timeout - Handle timeout of queued command
3715 * @qc: Command that timed out
3717 * Some part of the kernel (currently, only the SCSI layer)
3718 * has noticed that the active command on port @ap has not
3719 * completed after a specified length of time. Handle this
3720 * condition by disabling DMA (if necessary) and completing
3721 * transactions, with error if necessary.
3723 * This also handles the case of the "lost interrupt", where
3724 * for some reason (possibly hardware bug, possibly driver bug)
3725 * an interrupt was not delivered to the driver, even though the
3726 * transaction completed successfully.
3729 * Inherited from SCSI layer (none, can sleep)
3732 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3734 struct ata_port *ap = qc->ap;
3735 struct ata_host_set *host_set = ap->host_set;
3736 u8 host_stat = 0, drv_stat;
3737 unsigned long flags;
3741 ap->hsm_task_state = HSM_ST_IDLE;
3743 spin_lock_irqsave(&host_set->lock, flags);
3745 switch (qc->tf.protocol) {
3748 case ATA_PROT_ATAPI_DMA:
3749 host_stat = ap->ops->bmdma_status(ap);
3751 /* before we do anything else, clear DMA-Start bit */
3752 ap->ops->bmdma_stop(qc);
3758 drv_stat = ata_chk_status(ap);
3760 /* ack bmdma irq events */
3761 ap->ops->irq_clear(ap);
3763 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3764 ap->id, qc->tf.command, drv_stat, host_stat);
3766 /* complete taskfile transaction */
3767 qc->err_mask |= ac_err_mask(drv_stat);
3771 spin_unlock_irqrestore(&host_set->lock, flags);
3773 ata_eh_qc_complete(qc);
3779 * ata_eng_timeout - Handle timeout of queued command
3780 * @ap: Port on which timed-out command is active
3782 * Some part of the kernel (currently, only the SCSI layer)
3783 * has noticed that the active command on port @ap has not
3784 * completed after a specified length of time. Handle this
3785 * condition by disabling DMA (if necessary) and completing
3786 * transactions, with error if necessary.
3788 * This also handles the case of the "lost interrupt", where
3789 * for some reason (possibly hardware bug, possibly driver bug)
3790 * an interrupt was not delivered to the driver, even though the
3791 * transaction completed successfully.
3794 * Inherited from SCSI layer (none, can sleep)
3797 void ata_eng_timeout(struct ata_port *ap)
3801 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
3807 * ata_qc_new - Request an available ATA command, for queueing
3808 * @ap: Port associated with device @dev
3809 * @dev: Device from whom we request an available command structure
3815 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3817 struct ata_queued_cmd *qc = NULL;
3820 for (i = 0; i < ATA_MAX_QUEUE; i++)
3821 if (!test_and_set_bit(i, &ap->qactive)) {
3822 qc = ata_qc_from_tag(ap, i);
3833 * ata_qc_new_init - Request an available ATA command, and initialize it
3834 * @ap: Port associated with device @dev
3835 * @dev: Device from whom we request an available command structure
3841 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3842 struct ata_device *dev)
3844 struct ata_queued_cmd *qc;
3846 qc = ata_qc_new(ap);
3859 * ata_qc_free - free unused ata_queued_cmd
3860 * @qc: Command to complete
3862 * Designed to free unused ata_queued_cmd object
3863 * in case something prevents using it.
3866 * spin_lock_irqsave(host_set lock)
3868 void ata_qc_free(struct ata_queued_cmd *qc)
3870 struct ata_port *ap = qc->ap;
3873 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3877 if (likely(ata_tag_valid(tag))) {
3878 if (tag == ap->active_tag)
3879 ap->active_tag = ATA_TAG_POISON;
3880 qc->tag = ATA_TAG_POISON;
3881 clear_bit(tag, &ap->qactive);
3885 void __ata_qc_complete(struct ata_queued_cmd *qc)
3887 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3888 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3890 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3893 /* atapi: mark qc as inactive to prevent the interrupt handler
3894 * from completing the command twice later, before the error handler
3895 * is called. (when rc != 0 and atapi request sense is needed)
3897 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3899 /* call completion callback */
3900 qc->complete_fn(qc);
3903 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3905 struct ata_port *ap = qc->ap;
3907 switch (qc->tf.protocol) {
3909 case ATA_PROT_ATAPI_DMA:
3912 case ATA_PROT_ATAPI:
3914 if (ap->flags & ATA_FLAG_PIO_DMA)
3927 * ata_qc_issue - issue taskfile to device
3928 * @qc: command to issue to device
3930 * Prepare an ATA command to submission to device.
3931 * This includes mapping the data into a DMA-able
3932 * area, filling in the S/G table, and finally
3933 * writing the taskfile to hardware, starting the command.
3936 * spin_lock_irqsave(host_set lock)
3939 * Zero on success, AC_ERR_* mask on failure
3942 unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
3944 struct ata_port *ap = qc->ap;
3946 if (ata_should_dma_map(qc)) {
3947 if (qc->flags & ATA_QCFLAG_SG) {
3948 if (ata_sg_setup(qc))
3950 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3951 if (ata_sg_setup_one(qc))
3955 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3958 ap->ops->qc_prep(qc);
3960 qc->ap->active_tag = qc->tag;
3961 qc->flags |= ATA_QCFLAG_ACTIVE;
3963 return ap->ops->qc_issue(qc);
3966 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3967 return AC_ERR_SYSTEM;
3972 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3973 * @qc: command to issue to device
3975 * Using various libata functions and hooks, this function
3976 * starts an ATA command. ATA commands are grouped into
3977 * classes called "protocols", and issuing each type of protocol
3978 * is slightly different.
3980 * May be used as the qc_issue() entry in ata_port_operations.
3983 * spin_lock_irqsave(host_set lock)
3986 * Zero on success, AC_ERR_* mask on failure
3989 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3991 struct ata_port *ap = qc->ap;
3993 ata_dev_select(ap, qc->dev->devno, 1, 0);
3995 switch (qc->tf.protocol) {
3996 case ATA_PROT_NODATA:
3997 ata_tf_to_host(ap, &qc->tf);
4001 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4002 ap->ops->bmdma_setup(qc); /* set up bmdma */
4003 ap->ops->bmdma_start(qc); /* initiate bmdma */
4006 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4007 ata_qc_set_polling(qc);
4008 ata_tf_to_host(ap, &qc->tf);
4009 ap->hsm_task_state = HSM_ST;
4010 ata_port_queue_task(ap, ata_pio_task, ap, 0);
4013 case ATA_PROT_ATAPI:
4014 ata_qc_set_polling(qc);
4015 ata_tf_to_host(ap, &qc->tf);
4016 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4019 case ATA_PROT_ATAPI_NODATA:
4020 ap->flags |= ATA_FLAG_NOINTR;
4021 ata_tf_to_host(ap, &qc->tf);
4022 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4025 case ATA_PROT_ATAPI_DMA:
4026 ap->flags |= ATA_FLAG_NOINTR;
4027 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4028 ap->ops->bmdma_setup(qc); /* set up bmdma */
4029 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4034 return AC_ERR_SYSTEM;
4041 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
4042 * @qc: Info associated with this ATA transaction.
4045 * spin_lock_irqsave(host_set lock)
4048 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4050 struct ata_port *ap = qc->ap;
4051 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4053 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4055 /* load PRD table addr. */
4056 mb(); /* make sure PRD table writes are visible to controller */
4057 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4059 /* specify data direction, triple-check start bit is clear */
4060 dmactl = readb(mmio + ATA_DMA_CMD);
4061 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4063 dmactl |= ATA_DMA_WR;
4064 writeb(dmactl, mmio + ATA_DMA_CMD);
4066 /* issue r/w command */
4067 ap->ops->exec_command(ap, &qc->tf);
4071 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
4072 * @qc: Info associated with this ATA transaction.
4075 * spin_lock_irqsave(host_set lock)
4078 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4080 struct ata_port *ap = qc->ap;
4081 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4084 /* start host DMA transaction */
4085 dmactl = readb(mmio + ATA_DMA_CMD);
4086 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4088 /* Strictly, one may wish to issue a readb() here, to
4089 * flush the mmio write. However, control also passes
4090 * to the hardware at this point, and it will interrupt
4091 * us when we are to resume control. So, in effect,
4092 * we don't care when the mmio write flushes.
4093 * Further, a read of the DMA status register _immediately_
4094 * following the write may not be what certain flaky hardware
4095 * is expected, so I think it is best to not add a readb()
4096 * without first all the MMIO ATA cards/mobos.
4097 * Or maybe I'm just being paranoid.
4102 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4103 * @qc: Info associated with this ATA transaction.
4106 * spin_lock_irqsave(host_set lock)
4109 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4111 struct ata_port *ap = qc->ap;
4112 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4115 /* load PRD table addr. */
4116 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4118 /* specify data direction, triple-check start bit is clear */
4119 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4120 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4122 dmactl |= ATA_DMA_WR;
4123 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4125 /* issue r/w command */
4126 ap->ops->exec_command(ap, &qc->tf);
4130 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4131 * @qc: Info associated with this ATA transaction.
4134 * spin_lock_irqsave(host_set lock)
4137 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4139 struct ata_port *ap = qc->ap;
4142 /* start host DMA transaction */
4143 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4144 outb(dmactl | ATA_DMA_START,
4145 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4150 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4151 * @qc: Info associated with this ATA transaction.
4153 * Writes the ATA_DMA_START flag to the DMA command register.
4155 * May be used as the bmdma_start() entry in ata_port_operations.
4158 * spin_lock_irqsave(host_set lock)
4160 void ata_bmdma_start(struct ata_queued_cmd *qc)
4162 if (qc->ap->flags & ATA_FLAG_MMIO)
4163 ata_bmdma_start_mmio(qc);
4165 ata_bmdma_start_pio(qc);
4170 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4171 * @qc: Info associated with this ATA transaction.
4173 * Writes address of PRD table to device's PRD Table Address
4174 * register, sets the DMA control register, and calls
4175 * ops->exec_command() to start the transfer.
4177 * May be used as the bmdma_setup() entry in ata_port_operations.
4180 * spin_lock_irqsave(host_set lock)
4182 void ata_bmdma_setup(struct ata_queued_cmd *qc)
4184 if (qc->ap->flags & ATA_FLAG_MMIO)
4185 ata_bmdma_setup_mmio(qc);
4187 ata_bmdma_setup_pio(qc);
4192 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
4193 * @ap: Port associated with this ATA transaction.
4195 * Clear interrupt and error flags in DMA status register.
4197 * May be used as the irq_clear() entry in ata_port_operations.
4200 * spin_lock_irqsave(host_set lock)
4203 void ata_bmdma_irq_clear(struct ata_port *ap)
4205 if (!ap->ioaddr.bmdma_addr)
4208 if (ap->flags & ATA_FLAG_MMIO) {
4209 void __iomem *mmio =
4210 ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4211 writeb(readb(mmio), mmio);
4213 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4214 outb(inb(addr), addr);
4220 * ata_bmdma_status - Read PCI IDE BMDMA status
4221 * @ap: Port associated with this ATA transaction.
4223 * Read and return BMDMA status register.
4225 * May be used as the bmdma_status() entry in ata_port_operations.
4228 * spin_lock_irqsave(host_set lock)
4231 u8 ata_bmdma_status(struct ata_port *ap)
4234 if (ap->flags & ATA_FLAG_MMIO) {
4235 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4236 host_stat = readb(mmio + ATA_DMA_STATUS);
4238 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4244 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4245 * @qc: Command we are ending DMA for
4247 * Clears the ATA_DMA_START flag in the dma control register
4249 * May be used as the bmdma_stop() entry in ata_port_operations.
4252 * spin_lock_irqsave(host_set lock)
4255 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4257 struct ata_port *ap = qc->ap;
4258 if (ap->flags & ATA_FLAG_MMIO) {
4259 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4261 /* clear start/stop bit */
4262 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4263 mmio + ATA_DMA_CMD);
4265 /* clear start/stop bit */
4266 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4267 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4270 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4271 ata_altstatus(ap); /* dummy read */
4275 * ata_host_intr - Handle host interrupt for given (port, task)
4276 * @ap: Port on which interrupt arrived (possibly...)
4277 * @qc: Taskfile currently active in engine
4279 * Handle host interrupt for given queued command. Currently,
4280 * only DMA interrupts are handled. All other commands are
4281 * handled via polling with interrupts disabled (nIEN bit).
4284 * spin_lock_irqsave(host_set lock)
4287 * One if interrupt was handled, zero if not (shared irq).
4290 inline unsigned int ata_host_intr (struct ata_port *ap,
4291 struct ata_queued_cmd *qc)
4293 u8 status, host_stat;
4295 switch (qc->tf.protocol) {
4298 case ATA_PROT_ATAPI_DMA:
4299 case ATA_PROT_ATAPI:
4300 /* check status of DMA engine */
4301 host_stat = ap->ops->bmdma_status(ap);
4302 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4304 /* if it's not our irq... */
4305 if (!(host_stat & ATA_DMA_INTR))
4308 /* before we do anything else, clear DMA-Start bit */
4309 ap->ops->bmdma_stop(qc);
4313 case ATA_PROT_ATAPI_NODATA:
4314 case ATA_PROT_NODATA:
4315 /* check altstatus */
4316 status = ata_altstatus(ap);
4317 if (status & ATA_BUSY)
4320 /* check main status, clearing INTRQ */
4321 status = ata_chk_status(ap);
4322 if (unlikely(status & ATA_BUSY))
4324 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4325 ap->id, qc->tf.protocol, status);
4327 /* ack bmdma irq events */
4328 ap->ops->irq_clear(ap);
4330 /* complete taskfile transaction */
4331 qc->err_mask |= ac_err_mask(status);
4332 ata_qc_complete(qc);
4339 return 1; /* irq handled */
4342 ap->stats.idle_irq++;
4345 if ((ap->stats.idle_irq % 1000) == 0) {
4346 ata_irq_ack(ap, 0); /* debug trap */
4347 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4351 return 0; /* irq not handled */
4355 * ata_interrupt - Default ATA host interrupt handler
4356 * @irq: irq line (unused)
4357 * @dev_instance: pointer to our ata_host_set information structure
4360 * Default interrupt handler for PCI IDE devices. Calls
4361 * ata_host_intr() for each port that is not disabled.
4364 * Obtains host_set lock during operation.
4367 * IRQ_NONE or IRQ_HANDLED.
4370 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4372 struct ata_host_set *host_set = dev_instance;
4374 unsigned int handled = 0;
4375 unsigned long flags;
4377 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4378 spin_lock_irqsave(&host_set->lock, flags);
4380 for (i = 0; i < host_set->n_ports; i++) {
4381 struct ata_port *ap;
4383 ap = host_set->ports[i];
4385 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
4386 struct ata_queued_cmd *qc;
4388 qc = ata_qc_from_tag(ap, ap->active_tag);
4389 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4390 (qc->flags & ATA_QCFLAG_ACTIVE))
4391 handled |= ata_host_intr(ap, qc);
4395 spin_unlock_irqrestore(&host_set->lock, flags);
4397 return IRQ_RETVAL(handled);
4402 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4403 * without filling any other registers
4405 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4408 struct ata_taskfile tf;
4411 ata_tf_init(ap, &tf, dev->devno);
4414 tf.flags |= ATA_TFLAG_DEVICE;
4415 tf.protocol = ATA_PROT_NODATA;
4417 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4419 printk(KERN_ERR "%s: ata command failed: %d\n",
4425 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4429 if (!ata_try_flush_cache(dev))
4432 if (ata_id_has_flush_ext(dev->id))
4433 cmd = ATA_CMD_FLUSH_EXT;
4435 cmd = ATA_CMD_FLUSH;
4437 return ata_do_simple_cmd(ap, dev, cmd);
4440 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4442 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4445 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4447 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4451 * ata_device_resume - wakeup a previously suspended devices
4452 * @ap: port the device is connected to
4453 * @dev: the device to resume
4455 * Kick the drive back into action, by sending it an idle immediate
4456 * command and making sure its transfer mode matches between drive
4460 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4462 if (ap->flags & ATA_FLAG_SUSPENDED) {
4463 ap->flags &= ~ATA_FLAG_SUSPENDED;
4466 if (!ata_dev_present(dev))
4468 if (dev->class == ATA_DEV_ATA)
4469 ata_start_drive(ap, dev);
4475 * ata_device_suspend - prepare a device for suspend
4476 * @ap: port the device is connected to
4477 * @dev: the device to suspend
4479 * Flush the cache on the drive, if appropriate, then issue a
4480 * standbynow command.
4482 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4484 if (!ata_dev_present(dev))
4486 if (dev->class == ATA_DEV_ATA)
4487 ata_flush_cache(ap, dev);
4489 ata_standby_drive(ap, dev);
4490 ap->flags |= ATA_FLAG_SUSPENDED;
4495 * ata_port_start - Set port up for dma.
4496 * @ap: Port to initialize
4498 * Called just after data structures for each port are
4499 * initialized. Allocates space for PRD table.
4501 * May be used as the port_start() entry in ata_port_operations.
4504 * Inherited from caller.
4507 int ata_port_start (struct ata_port *ap)
4509 struct device *dev = ap->host_set->dev;
4512 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4516 rc = ata_pad_alloc(ap, dev);
4518 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4522 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4529 * ata_port_stop - Undo ata_port_start()
4530 * @ap: Port to shut down
4532 * Frees the PRD table.
4534 * May be used as the port_stop() entry in ata_port_operations.
4537 * Inherited from caller.
4540 void ata_port_stop (struct ata_port *ap)
4542 struct device *dev = ap->host_set->dev;
4544 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4545 ata_pad_free(ap, dev);
4548 void ata_host_stop (struct ata_host_set *host_set)
4550 if (host_set->mmio_base)
4551 iounmap(host_set->mmio_base);
4556 * ata_host_remove - Unregister SCSI host structure with upper layers
4557 * @ap: Port to unregister
4558 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4561 * Inherited from caller.
4564 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4566 struct Scsi_Host *sh = ap->host;
4571 scsi_remove_host(sh);
4573 ap->ops->port_stop(ap);
4577 * ata_host_init - Initialize an ata_port structure
4578 * @ap: Structure to initialize
4579 * @host: associated SCSI mid-layer structure
4580 * @host_set: Collection of hosts to which @ap belongs
4581 * @ent: Probe information provided by low-level driver
4582 * @port_no: Port number associated with this ata_port
4584 * Initialize a new ata_port structure, and its associated
4588 * Inherited from caller.
4591 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4592 struct ata_host_set *host_set,
4593 const struct ata_probe_ent *ent, unsigned int port_no)
4599 host->max_channel = 1;
4600 host->unique_id = ata_unique_id++;
4601 host->max_cmd_len = 12;
4603 ap->flags = ATA_FLAG_PORT_DISABLED;
4604 ap->id = host->unique_id;
4606 ap->ctl = ATA_DEVCTL_OBS;
4607 ap->host_set = host_set;
4608 ap->port_no = port_no;
4610 ent->legacy_mode ? ent->hard_port_no : port_no;
4611 ap->pio_mask = ent->pio_mask;
4612 ap->mwdma_mask = ent->mwdma_mask;
4613 ap->udma_mask = ent->udma_mask;
4614 ap->flags |= ent->host_flags;
4615 ap->ops = ent->port_ops;
4616 ap->cbl = ATA_CBL_NONE;
4617 ap->active_tag = ATA_TAG_POISON;
4618 ap->last_ctl = 0xFF;
4620 INIT_WORK(&ap->port_task, NULL, NULL);
4621 INIT_LIST_HEAD(&ap->eh_done_q);
4623 for (i = 0; i < ATA_MAX_DEVICES; i++)
4624 ap->device[i].devno = i;
4627 ap->stats.unhandled_irq = 1;
4628 ap->stats.idle_irq = 1;
4631 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4635 * ata_host_add - Attach low-level ATA driver to system
4636 * @ent: Information provided by low-level driver
4637 * @host_set: Collections of ports to which we add
4638 * @port_no: Port number associated with this host
4640 * Attach low-level ATA driver to system.
4643 * PCI/etc. bus probe sem.
4646 * New ata_port on success, for NULL on error.
4649 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4650 struct ata_host_set *host_set,
4651 unsigned int port_no)
4653 struct Scsi_Host *host;
4654 struct ata_port *ap;
4658 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4662 ap = (struct ata_port *) &host->hostdata[0];
4664 ata_host_init(ap, host, host_set, ent, port_no);
4666 rc = ap->ops->port_start(ap);
4673 scsi_host_put(host);
4678 * ata_device_add - Register hardware device with ATA and SCSI layers
4679 * @ent: Probe information describing hardware device to be registered
4681 * This function processes the information provided in the probe
4682 * information struct @ent, allocates the necessary ATA and SCSI
4683 * host information structures, initializes them, and registers
4684 * everything with requisite kernel subsystems.
4686 * This function requests irqs, probes the ATA bus, and probes
4690 * PCI/etc. bus probe sem.
4693 * Number of ports registered. Zero on error (no ports registered).
4696 int ata_device_add(const struct ata_probe_ent *ent)
4698 unsigned int count = 0, i;
4699 struct device *dev = ent->dev;
4700 struct ata_host_set *host_set;
4703 /* alloc a container for our list of ATA ports (buses) */
4704 host_set = kzalloc(sizeof(struct ata_host_set) +
4705 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4708 spin_lock_init(&host_set->lock);
4710 host_set->dev = dev;
4711 host_set->n_ports = ent->n_ports;
4712 host_set->irq = ent->irq;
4713 host_set->mmio_base = ent->mmio_base;
4714 host_set->private_data = ent->private_data;
4715 host_set->ops = ent->port_ops;
4717 /* register each port bound to this device */
4718 for (i = 0; i < ent->n_ports; i++) {
4719 struct ata_port *ap;
4720 unsigned long xfer_mode_mask;
4722 ap = ata_host_add(ent, host_set, i);
4726 host_set->ports[i] = ap;
4727 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4728 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4729 (ap->pio_mask << ATA_SHIFT_PIO);
4731 /* print per-port info to dmesg */
4732 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4733 "bmdma 0x%lX irq %lu\n",
4735 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4736 ata_mode_string(xfer_mode_mask),
4737 ap->ioaddr.cmd_addr,
4738 ap->ioaddr.ctl_addr,
4739 ap->ioaddr.bmdma_addr,
4743 host_set->ops->irq_clear(ap);
4750 /* obtain irq, that is shared between channels */
4751 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4752 DRV_NAME, host_set))
4755 /* perform each probe synchronously */
4756 DPRINTK("probe begin\n");
4757 for (i = 0; i < count; i++) {
4758 struct ata_port *ap;
4761 ap = host_set->ports[i];
4763 DPRINTK("ata%u: bus probe begin\n", ap->id);
4764 rc = ata_bus_probe(ap);
4765 DPRINTK("ata%u: bus probe end\n", ap->id);
4768 /* FIXME: do something useful here?
4769 * Current libata behavior will
4770 * tear down everything when
4771 * the module is removed
4772 * or the h/w is unplugged.
4776 rc = scsi_add_host(ap->host, dev);
4778 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4780 /* FIXME: do something useful here */
4781 /* FIXME: handle unconditional calls to
4782 * scsi_scan_host and ata_host_remove, below,
4788 /* probes are done, now scan each port's disk(s) */
4789 DPRINTK("host probe begin\n");
4790 for (i = 0; i < count; i++) {
4791 struct ata_port *ap = host_set->ports[i];
4793 ata_scsi_scan_host(ap);
4796 dev_set_drvdata(dev, host_set);
4798 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4799 return ent->n_ports; /* success */
4802 for (i = 0; i < count; i++) {
4803 ata_host_remove(host_set->ports[i], 1);
4804 scsi_host_put(host_set->ports[i]->host);
4808 VPRINTK("EXIT, returning 0\n");
4813 * ata_host_set_remove - PCI layer callback for device removal
4814 * @host_set: ATA host set that was removed
4816 * Unregister all objects associated with this host set. Free those
4820 * Inherited from calling layer (may sleep).
4823 void ata_host_set_remove(struct ata_host_set *host_set)
4825 struct ata_port *ap;
4828 for (i = 0; i < host_set->n_ports; i++) {
4829 ap = host_set->ports[i];
4830 scsi_remove_host(ap->host);
4833 free_irq(host_set->irq, host_set);
4835 for (i = 0; i < host_set->n_ports; i++) {
4836 ap = host_set->ports[i];
4838 ata_scsi_release(ap->host);
4840 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4841 struct ata_ioports *ioaddr = &ap->ioaddr;
4843 if (ioaddr->cmd_addr == 0x1f0)
4844 release_region(0x1f0, 8);
4845 else if (ioaddr->cmd_addr == 0x170)
4846 release_region(0x170, 8);
4849 scsi_host_put(ap->host);
4852 if (host_set->ops->host_stop)
4853 host_set->ops->host_stop(host_set);
4859 * ata_scsi_release - SCSI layer callback hook for host unload
4860 * @host: libata host to be unloaded
4862 * Performs all duties necessary to shut down a libata port...
4863 * Kill port kthread, disable port, and release resources.
4866 * Inherited from SCSI layer.
4872 int ata_scsi_release(struct Scsi_Host *host)
4874 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4879 ap->ops->port_disable(ap);
4880 ata_host_remove(ap, 0);
4881 for (i = 0; i < ATA_MAX_DEVICES; i++)
4882 kfree(ap->device[i].id);
4889 * ata_std_ports - initialize ioaddr with standard port offsets.
4890 * @ioaddr: IO address structure to be initialized
4892 * Utility function which initializes data_addr, error_addr,
4893 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4894 * device_addr, status_addr, and command_addr to standard offsets
4895 * relative to cmd_addr.
4897 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4900 void ata_std_ports(struct ata_ioports *ioaddr)
4902 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4903 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4904 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4905 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4906 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4907 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4908 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4909 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4910 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4911 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4917 void ata_pci_host_stop (struct ata_host_set *host_set)
4919 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4921 pci_iounmap(pdev, host_set->mmio_base);
4925 * ata_pci_remove_one - PCI layer callback for device removal
4926 * @pdev: PCI device that was removed
4928 * PCI layer indicates to libata via this hook that
4929 * hot-unplug or module unload event has occurred.
4930 * Handle this by unregistering all objects associated
4931 * with this PCI device. Free those objects. Then finally
4932 * release PCI resources and disable device.
4935 * Inherited from PCI layer (may sleep).
4938 void ata_pci_remove_one (struct pci_dev *pdev)
4940 struct device *dev = pci_dev_to_dev(pdev);
4941 struct ata_host_set *host_set = dev_get_drvdata(dev);
4943 ata_host_set_remove(host_set);
4944 pci_release_regions(pdev);
4945 pci_disable_device(pdev);
4946 dev_set_drvdata(dev, NULL);
4949 /* move to PCI subsystem */
4950 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4952 unsigned long tmp = 0;
4954 switch (bits->width) {
4957 pci_read_config_byte(pdev, bits->reg, &tmp8);
4963 pci_read_config_word(pdev, bits->reg, &tmp16);
4969 pci_read_config_dword(pdev, bits->reg, &tmp32);
4980 return (tmp == bits->val) ? 1 : 0;
4983 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4985 pci_save_state(pdev);
4986 pci_disable_device(pdev);
4987 pci_set_power_state(pdev, PCI_D3hot);
4991 int ata_pci_device_resume(struct pci_dev *pdev)
4993 pci_set_power_state(pdev, PCI_D0);
4994 pci_restore_state(pdev);
4995 pci_enable_device(pdev);
4996 pci_set_master(pdev);
4999 #endif /* CONFIG_PCI */
5002 static int __init ata_init(void)
5004 ata_wq = create_workqueue("ata");
5008 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5012 static void __exit ata_exit(void)
5014 destroy_workqueue(ata_wq);
5017 module_init(ata_init);
5018 module_exit(ata_exit);
5020 static unsigned long ratelimit_time;
5021 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5023 int ata_ratelimit(void)
5026 unsigned long flags;
5028 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5030 if (time_after(jiffies, ratelimit_time)) {
5032 ratelimit_time = jiffies + (HZ/5);
5036 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5042 * libata is essentially a library of internal helper functions for
5043 * low-level ATA host controller drivers. As such, the API/ABI is
5044 * likely to change as new drivers are added and updated.
5045 * Do not depend on ABI/API stability.
5048 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5049 EXPORT_SYMBOL_GPL(ata_std_ports);
5050 EXPORT_SYMBOL_GPL(ata_device_add);
5051 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5052 EXPORT_SYMBOL_GPL(ata_sg_init);
5053 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5054 EXPORT_SYMBOL_GPL(__ata_qc_complete);
5055 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5056 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5057 EXPORT_SYMBOL_GPL(ata_tf_load);
5058 EXPORT_SYMBOL_GPL(ata_tf_read);
5059 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5060 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5061 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5062 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5063 EXPORT_SYMBOL_GPL(ata_check_status);
5064 EXPORT_SYMBOL_GPL(ata_altstatus);
5065 EXPORT_SYMBOL_GPL(ata_exec_command);
5066 EXPORT_SYMBOL_GPL(ata_port_start);
5067 EXPORT_SYMBOL_GPL(ata_port_stop);
5068 EXPORT_SYMBOL_GPL(ata_host_stop);
5069 EXPORT_SYMBOL_GPL(ata_interrupt);
5070 EXPORT_SYMBOL_GPL(ata_qc_prep);
5071 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
5072 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5073 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5074 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5075 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5076 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5077 EXPORT_SYMBOL_GPL(ata_port_probe);
5078 EXPORT_SYMBOL_GPL(sata_phy_reset);
5079 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5080 EXPORT_SYMBOL_GPL(ata_bus_reset);
5081 EXPORT_SYMBOL_GPL(ata_std_probeinit);
5082 EXPORT_SYMBOL_GPL(ata_std_softreset);
5083 EXPORT_SYMBOL_GPL(sata_std_hardreset);
5084 EXPORT_SYMBOL_GPL(ata_std_postreset);
5085 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
5086 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
5087 EXPORT_SYMBOL_GPL(ata_dev_revalidate);
5088 EXPORT_SYMBOL_GPL(ata_port_disable);
5089 EXPORT_SYMBOL_GPL(ata_ratelimit);
5090 EXPORT_SYMBOL_GPL(ata_busy_sleep);
5091 EXPORT_SYMBOL_GPL(ata_port_queue_task);
5092 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5093 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5094 EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
5095 EXPORT_SYMBOL_GPL(ata_scsi_error);
5096 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5097 EXPORT_SYMBOL_GPL(ata_scsi_release);
5098 EXPORT_SYMBOL_GPL(ata_host_intr);
5099 EXPORT_SYMBOL_GPL(ata_dev_classify);
5100 EXPORT_SYMBOL_GPL(ata_id_string);
5101 EXPORT_SYMBOL_GPL(ata_id_c_string);
5102 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5103 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5104 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
5106 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
5107 EXPORT_SYMBOL_GPL(ata_timing_compute);
5108 EXPORT_SYMBOL_GPL(ata_timing_merge);
5111 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5112 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5113 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5114 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5115 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5116 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5117 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
5118 #endif /* CONFIG_PCI */
5120 EXPORT_SYMBOL_GPL(ata_device_suspend);
5121 EXPORT_SYMBOL_GPL(ata_device_resume);
5122 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5123 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);