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 void ata_pio_error(struct ata_port *ap);
69 static unsigned int ata_dev_xfermask(struct ata_port *ap,
70 struct ata_device *dev);
72 static unsigned int ata_unique_id = 1;
73 static struct workqueue_struct *ata_wq;
75 int atapi_enabled = 0;
76 module_param(atapi_enabled, int, 0444);
77 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80 module_param_named(fua, libata_fua, int, 0444);
81 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
83 MODULE_AUTHOR("Jeff Garzik");
84 MODULE_DESCRIPTION("Library module for ATA devices");
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(DRV_VERSION);
90 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
91 * @tf: Taskfile to convert
92 * @fis: Buffer into which data will output
93 * @pmp: Port multiplier port
95 * Converts a standard ATA taskfile to a Serial ATA
96 * FIS structure (Register - Host to Device).
99 * Inherited from caller.
102 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
104 fis[0] = 0x27; /* Register - Host to Device FIS */
105 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
106 bit 7 indicates Command FIS */
107 fis[2] = tf->command;
108 fis[3] = tf->feature;
115 fis[8] = tf->hob_lbal;
116 fis[9] = tf->hob_lbam;
117 fis[10] = tf->hob_lbah;
118 fis[11] = tf->hob_feature;
121 fis[13] = tf->hob_nsect;
132 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
133 * @fis: Buffer from which data will be input
134 * @tf: Taskfile to output
136 * Converts a serial ATA FIS structure to a standard ATA taskfile.
139 * Inherited from caller.
142 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
144 tf->command = fis[2]; /* status */
145 tf->feature = fis[3]; /* error */
152 tf->hob_lbal = fis[8];
153 tf->hob_lbam = fis[9];
154 tf->hob_lbah = fis[10];
157 tf->hob_nsect = fis[13];
160 static const u8 ata_rw_cmds[] = {
164 ATA_CMD_READ_MULTI_EXT,
165 ATA_CMD_WRITE_MULTI_EXT,
169 ATA_CMD_WRITE_MULTI_FUA_EXT,
173 ATA_CMD_PIO_READ_EXT,
174 ATA_CMD_PIO_WRITE_EXT,
187 ATA_CMD_WRITE_FUA_EXT
191 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
192 * @qc: command to examine and configure
194 * Examine the device configuration and tf->flags to calculate
195 * the proper read/write commands and protocol to use.
200 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
202 struct ata_taskfile *tf = &qc->tf;
203 struct ata_device *dev = qc->dev;
206 int index, fua, lba48, write;
208 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
209 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
210 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
212 if (dev->flags & ATA_DFLAG_PIO) {
213 tf->protocol = ATA_PROT_PIO;
214 index = dev->multi_count ? 0 : 8;
215 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
216 /* Unable to use DMA due to host limitation */
217 tf->protocol = ATA_PROT_PIO;
218 index = dev->multi_count ? 0 : 8;
220 tf->protocol = ATA_PROT_DMA;
224 cmd = ata_rw_cmds[index + fua + lba48 + write];
233 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
234 * @pio_mask: pio_mask
235 * @mwdma_mask: mwdma_mask
236 * @udma_mask: udma_mask
238 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
239 * unsigned int xfer_mask.
247 static unsigned int ata_pack_xfermask(unsigned int pio_mask,
248 unsigned int mwdma_mask,
249 unsigned int udma_mask)
251 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
252 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
253 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
256 static const struct ata_xfer_ent {
257 unsigned int shift, bits;
260 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
261 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
262 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
267 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
268 * @xfer_mask: xfer_mask of interest
270 * Return matching XFER_* value for @xfer_mask. Only the highest
271 * bit of @xfer_mask is considered.
277 * Matching XFER_* value, 0 if no match found.
279 static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
281 int highbit = fls(xfer_mask) - 1;
282 const struct ata_xfer_ent *ent;
284 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
285 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
286 return ent->base + highbit - ent->shift;
291 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
292 * @xfer_mode: XFER_* of interest
294 * Return matching xfer_mask for @xfer_mode.
300 * Matching xfer_mask, 0 if no match found.
302 static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
304 const struct ata_xfer_ent *ent;
306 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
307 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
308 return 1 << (ent->shift + xfer_mode - ent->base);
313 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
314 * @xfer_mode: XFER_* of interest
316 * Return matching xfer_shift for @xfer_mode.
322 * Matching xfer_shift, -1 if no match found.
324 static int ata_xfer_mode2shift(unsigned int xfer_mode)
326 const struct ata_xfer_ent *ent;
328 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
329 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
335 * ata_mode_string - convert xfer_mask to string
336 * @xfer_mask: mask of bits supported; only highest bit counts.
338 * Determine string which represents the highest speed
339 * (highest bit in @modemask).
345 * Constant C string representing highest speed listed in
346 * @mode_mask, or the constant C string "<n/a>".
348 static const char *ata_mode_string(unsigned int xfer_mask)
350 static const char * const xfer_mode_str[] = {
370 highbit = fls(xfer_mask) - 1;
371 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
372 return xfer_mode_str[highbit];
377 * ata_pio_devchk - PATA device presence detection
378 * @ap: ATA channel to examine
379 * @device: Device to examine (starting at zero)
381 * This technique was originally described in
382 * Hale Landis's ATADRVR (www.ata-atapi.com), and
383 * later found its way into the ATA/ATAPI spec.
385 * Write a pattern to the ATA shadow registers,
386 * and if a device is present, it will respond by
387 * correctly storing and echoing back the
388 * ATA shadow register contents.
394 static unsigned int ata_pio_devchk(struct ata_port *ap,
397 struct ata_ioports *ioaddr = &ap->ioaddr;
400 ap->ops->dev_select(ap, device);
402 outb(0x55, ioaddr->nsect_addr);
403 outb(0xaa, ioaddr->lbal_addr);
405 outb(0xaa, ioaddr->nsect_addr);
406 outb(0x55, ioaddr->lbal_addr);
408 outb(0x55, ioaddr->nsect_addr);
409 outb(0xaa, ioaddr->lbal_addr);
411 nsect = inb(ioaddr->nsect_addr);
412 lbal = inb(ioaddr->lbal_addr);
414 if ((nsect == 0x55) && (lbal == 0xaa))
415 return 1; /* we found a device */
417 return 0; /* nothing found */
421 * ata_mmio_devchk - PATA device presence detection
422 * @ap: ATA channel to examine
423 * @device: Device to examine (starting at zero)
425 * This technique was originally described in
426 * Hale Landis's ATADRVR (www.ata-atapi.com), and
427 * later found its way into the ATA/ATAPI spec.
429 * Write a pattern to the ATA shadow registers,
430 * and if a device is present, it will respond by
431 * correctly storing and echoing back the
432 * ATA shadow register contents.
438 static unsigned int ata_mmio_devchk(struct ata_port *ap,
441 struct ata_ioports *ioaddr = &ap->ioaddr;
444 ap->ops->dev_select(ap, device);
446 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
447 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
449 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
450 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
452 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
453 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
455 nsect = readb((void __iomem *) ioaddr->nsect_addr);
456 lbal = readb((void __iomem *) ioaddr->lbal_addr);
458 if ((nsect == 0x55) && (lbal == 0xaa))
459 return 1; /* we found a device */
461 return 0; /* nothing found */
465 * ata_devchk - PATA device presence detection
466 * @ap: ATA channel to examine
467 * @device: Device to examine (starting at zero)
469 * Dispatch ATA device presence detection, depending
470 * on whether we are using PIO or MMIO to talk to the
471 * ATA shadow registers.
477 static unsigned int ata_devchk(struct ata_port *ap,
480 if (ap->flags & ATA_FLAG_MMIO)
481 return ata_mmio_devchk(ap, device);
482 return ata_pio_devchk(ap, device);
486 * ata_dev_classify - determine device type based on ATA-spec signature
487 * @tf: ATA taskfile register set for device to be identified
489 * Determine from taskfile register contents whether a device is
490 * ATA or ATAPI, as per "Signature and persistence" section
491 * of ATA/PI spec (volume 1, sect 5.14).
497 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
498 * the event of failure.
501 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
503 /* Apple's open source Darwin code hints that some devices only
504 * put a proper signature into the LBA mid/high registers,
505 * So, we only check those. It's sufficient for uniqueness.
508 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
509 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
510 DPRINTK("found ATA device by sig\n");
514 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
515 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
516 DPRINTK("found ATAPI device by sig\n");
517 return ATA_DEV_ATAPI;
520 DPRINTK("unknown device\n");
521 return ATA_DEV_UNKNOWN;
525 * ata_dev_try_classify - Parse returned ATA device signature
526 * @ap: ATA channel to examine
527 * @device: Device to examine (starting at zero)
528 * @r_err: Value of error register on completion
530 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
531 * an ATA/ATAPI-defined set of values is placed in the ATA
532 * shadow registers, indicating the results of device detection
535 * Select the ATA device, and read the values from the ATA shadow
536 * registers. Then parse according to the Error register value,
537 * and the spec-defined values examined by ata_dev_classify().
543 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
547 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
549 struct ata_taskfile tf;
553 ap->ops->dev_select(ap, device);
555 memset(&tf, 0, sizeof(tf));
557 ap->ops->tf_read(ap, &tf);
562 /* see if device passed diags */
565 else if ((device == 0) && (err == 0x81))
570 /* determine if device is ATA or ATAPI */
571 class = ata_dev_classify(&tf);
573 if (class == ATA_DEV_UNKNOWN)
575 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
581 * ata_id_string - Convert IDENTIFY DEVICE page into string
582 * @id: IDENTIFY DEVICE results we will examine
583 * @s: string into which data is output
584 * @ofs: offset into identify device page
585 * @len: length of string to return. must be an even number.
587 * The strings in the IDENTIFY DEVICE page are broken up into
588 * 16-bit chunks. Run through the string, and output each
589 * 8-bit chunk linearly, regardless of platform.
595 void ata_id_string(const u16 *id, unsigned char *s,
596 unsigned int ofs, unsigned int len)
615 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
616 * @id: IDENTIFY DEVICE results we will examine
617 * @s: string into which data is output
618 * @ofs: offset into identify device page
619 * @len: length of string to return. must be an odd number.
621 * This function is identical to ata_id_string except that it
622 * trims trailing spaces and terminates the resulting string with
623 * null. @len must be actual maximum length (even number) + 1.
628 void ata_id_c_string(const u16 *id, unsigned char *s,
629 unsigned int ofs, unsigned int len)
635 ata_id_string(id, s, ofs, len - 1);
637 p = s + strnlen(s, len - 1);
638 while (p > s && p[-1] == ' ')
643 static u64 ata_id_n_sectors(const u16 *id)
645 if (ata_id_has_lba(id)) {
646 if (ata_id_has_lba48(id))
647 return ata_id_u64(id, 100);
649 return ata_id_u32(id, 60);
651 if (ata_id_current_chs_valid(id))
652 return ata_id_u32(id, 57);
654 return id[1] * id[3] * id[6];
659 * ata_noop_dev_select - Select device 0/1 on ATA bus
660 * @ap: ATA channel to manipulate
661 * @device: ATA device (numbered from zero) to select
663 * This function performs no actual function.
665 * May be used as the dev_select() entry in ata_port_operations.
670 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
676 * ata_std_dev_select - Select device 0/1 on ATA bus
677 * @ap: ATA channel to manipulate
678 * @device: ATA device (numbered from zero) to select
680 * Use the method defined in the ATA specification to
681 * make either device 0, or device 1, active on the
682 * ATA channel. Works with both PIO and MMIO.
684 * May be used as the dev_select() entry in ata_port_operations.
690 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
695 tmp = ATA_DEVICE_OBS;
697 tmp = ATA_DEVICE_OBS | ATA_DEV1;
699 if (ap->flags & ATA_FLAG_MMIO) {
700 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
702 outb(tmp, ap->ioaddr.device_addr);
704 ata_pause(ap); /* needed; also flushes, for mmio */
708 * ata_dev_select - Select device 0/1 on ATA bus
709 * @ap: ATA channel to manipulate
710 * @device: ATA device (numbered from zero) to select
711 * @wait: non-zero to wait for Status register BSY bit to clear
712 * @can_sleep: non-zero if context allows sleeping
714 * Use the method defined in the ATA specification to
715 * make either device 0, or device 1, active on the
718 * This is a high-level version of ata_std_dev_select(),
719 * which additionally provides the services of inserting
720 * the proper pauses and status polling, where needed.
726 void ata_dev_select(struct ata_port *ap, unsigned int device,
727 unsigned int wait, unsigned int can_sleep)
729 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
730 ap->id, device, wait);
735 ap->ops->dev_select(ap, device);
738 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
745 * ata_dump_id - IDENTIFY DEVICE info debugging output
746 * @id: IDENTIFY DEVICE page to dump
748 * Dump selected 16-bit words from the given IDENTIFY DEVICE
755 static inline void ata_dump_id(const u16 *id)
757 DPRINTK("49==0x%04x "
767 DPRINTK("80==0x%04x "
777 DPRINTK("88==0x%04x "
784 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
785 * @id: IDENTIFY data to compute xfer mask from
787 * Compute the xfermask for this device. This is not as trivial
788 * as it seems if we must consider early devices correctly.
790 * FIXME: pre IDE drive timing (do we care ?).
798 static unsigned int ata_id_xfermask(const u16 *id)
800 unsigned int pio_mask, mwdma_mask, udma_mask;
802 /* Usual case. Word 53 indicates word 64 is valid */
803 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
804 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
808 /* If word 64 isn't valid then Word 51 high byte holds
809 * the PIO timing number for the maximum. Turn it into
812 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
814 /* But wait.. there's more. Design your standards by
815 * committee and you too can get a free iordy field to
816 * process. However its the speeds not the modes that
817 * are supported... Note drivers using the timing API
818 * will get this right anyway
822 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
823 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
825 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
829 * ata_port_queue_task - Queue port_task
830 * @ap: The ata_port to queue port_task for
832 * Schedule @fn(@data) for execution after @delay jiffies using
833 * port_task. There is one port_task per port and it's the
834 * user(low level driver)'s responsibility to make sure that only
835 * one task is active at any given time.
837 * libata core layer takes care of synchronization between
838 * port_task and EH. ata_port_queue_task() may be ignored for EH
842 * Inherited from caller.
844 void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
849 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
852 PREPARE_WORK(&ap->port_task, fn, data);
855 rc = queue_work(ata_wq, &ap->port_task);
857 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
859 /* rc == 0 means that another user is using port task */
864 * ata_port_flush_task - Flush port_task
865 * @ap: The ata_port to flush port_task for
867 * After this function completes, port_task is guranteed not to
868 * be running or scheduled.
871 * Kernel thread context (may sleep)
873 void ata_port_flush_task(struct ata_port *ap)
879 spin_lock_irqsave(&ap->host_set->lock, flags);
880 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
881 spin_unlock_irqrestore(&ap->host_set->lock, flags);
883 DPRINTK("flush #1\n");
884 flush_workqueue(ata_wq);
887 * At this point, if a task is running, it's guaranteed to see
888 * the FLUSH flag; thus, it will never queue pio tasks again.
891 if (!cancel_delayed_work(&ap->port_task)) {
892 DPRINTK("flush #2\n");
893 flush_workqueue(ata_wq);
896 spin_lock_irqsave(&ap->host_set->lock, flags);
897 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
898 spin_unlock_irqrestore(&ap->host_set->lock, flags);
903 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
905 struct completion *waiting = qc->private_data;
907 qc->ap->ops->tf_read(qc->ap, &qc->tf);
912 * ata_exec_internal - execute libata internal command
913 * @ap: Port to which the command is sent
914 * @dev: Device to which the command is sent
915 * @tf: Taskfile registers for the command and the result
916 * @dma_dir: Data tranfer direction of the command
917 * @buf: Data buffer of the command
918 * @buflen: Length of data buffer
920 * Executes libata internal command with timeout. @tf contains
921 * command on entry and result on return. Timeout and error
922 * conditions are reported via return value. No recovery action
923 * is taken after a command times out. It's caller's duty to
924 * clean up after timeout.
927 * None. Should be called with kernel context, might sleep.
931 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
932 struct ata_taskfile *tf,
933 int dma_dir, void *buf, unsigned int buflen)
935 u8 command = tf->command;
936 struct ata_queued_cmd *qc;
937 DECLARE_COMPLETION(wait);
939 unsigned int err_mask;
941 spin_lock_irqsave(&ap->host_set->lock, flags);
943 qc = ata_qc_new_init(ap, dev);
947 qc->dma_dir = dma_dir;
948 if (dma_dir != DMA_NONE) {
949 ata_sg_init_one(qc, buf, buflen);
950 qc->nsect = buflen / ATA_SECT_SIZE;
953 qc->private_data = &wait;
954 qc->complete_fn = ata_qc_complete_internal;
956 qc->err_mask = ata_qc_issue(qc);
960 spin_unlock_irqrestore(&ap->host_set->lock, flags);
962 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
963 spin_lock_irqsave(&ap->host_set->lock, flags);
965 /* We're racing with irq here. If we lose, the
966 * following test prevents us from completing the qc
967 * again. If completion irq occurs after here but
968 * before the caller cleans up, it will result in a
969 * spurious interrupt. We can live with that.
971 if (qc->flags & ATA_QCFLAG_ACTIVE) {
972 qc->err_mask = AC_ERR_TIMEOUT;
974 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
978 spin_unlock_irqrestore(&ap->host_set->lock, flags);
982 err_mask = qc->err_mask;
990 * ata_pio_need_iordy - check if iordy needed
993 * Check if the current speed of the device requires IORDY. Used
994 * by various controllers for chip configuration.
997 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1000 int speed = adev->pio_mode - XFER_PIO_0;
1007 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1009 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1010 pio = adev->id[ATA_ID_EIDE_PIO];
1011 /* Is the speed faster than the drive allows non IORDY ? */
1013 /* This is cycle times not frequency - watch the logic! */
1014 if (pio > 240) /* PIO2 is 240nS per cycle */
1023 * ata_dev_read_id - Read ID data from the specified device
1024 * @ap: port on which target device resides
1025 * @dev: target device
1026 * @p_class: pointer to class of the target device (may be changed)
1027 * @post_reset: is this read ID post-reset?
1028 * @p_id: read IDENTIFY page (newly allocated)
1030 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1031 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1032 * devices. This function also takes care of EDD signature
1033 * misreporting (to be removed once EDD support is gone) and
1034 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
1037 * Kernel thread context (may sleep)
1040 * 0 on success, -errno otherwise.
1042 static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
1043 unsigned int *p_class, int post_reset, u16 **p_id)
1045 unsigned int class = *p_class;
1046 unsigned int using_edd;
1047 struct ata_taskfile tf;
1048 unsigned int err_mask = 0;
1053 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1055 if (ap->ops->probe_reset ||
1056 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1061 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1063 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1066 reason = "out of memory";
1071 ata_tf_init(ap, &tf, dev->devno);
1075 tf.command = ATA_CMD_ID_ATA;
1078 tf.command = ATA_CMD_ID_ATAPI;
1082 reason = "unsupported class";
1086 tf.protocol = ATA_PROT_PIO;
1088 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1089 id, sizeof(id[0]) * ATA_ID_WORDS);
1093 reason = "I/O error";
1095 if (err_mask & ~AC_ERR_DEV)
1099 * arg! EDD works for all test cases, but seems to return
1100 * the ATA signature for some ATAPI devices. Until the
1101 * reason for this is found and fixed, we fix up the mess
1102 * here. If IDENTIFY DEVICE returns command aborted
1103 * (as ATAPI devices do), then we issue an
1104 * IDENTIFY PACKET DEVICE.
1106 * ATA software reset (SRST, the default) does not appear
1107 * to have this problem.
1109 if ((using_edd) && (class == ATA_DEV_ATA)) {
1110 u8 err = tf.feature;
1111 if (err & ATA_ABORTED) {
1112 class = ATA_DEV_ATAPI;
1119 swap_buf_le16(id, ATA_ID_WORDS);
1121 /* print device capabilities */
1122 printk(KERN_DEBUG "ata%u: dev %u cfg "
1123 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1125 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1128 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1130 reason = "device reports illegal type";
1134 if (post_reset && class == ATA_DEV_ATA) {
1136 * The exact sequence expected by certain pre-ATA4 drives is:
1139 * INITIALIZE DEVICE PARAMETERS
1141 * Some drives were very specific about that exact sequence.
1143 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1144 err_mask = ata_dev_init_params(ap, dev);
1147 reason = "INIT_DEV_PARAMS failed";
1151 /* current CHS translation info (id[53-58]) might be
1152 * changed. reread the identify device info.
1164 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1165 ap->id, dev->devno, reason);
1170 static inline u8 ata_dev_knobble(const struct ata_port *ap,
1171 struct ata_device *dev)
1173 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1177 * ata_dev_configure - Configure the specified ATA/ATAPI device
1178 * @ap: Port on which target device resides
1179 * @dev: Target device to configure
1180 * @print_info: Enable device info printout
1182 * Configure @dev according to @dev->id. Generic and low-level
1183 * driver specific fixups are also applied.
1186 * Kernel thread context (may sleep)
1189 * 0 on success, -errno otherwise
1191 static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1194 unsigned int xfer_mask;
1197 if (!ata_dev_present(dev)) {
1198 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1199 ap->id, dev->devno);
1203 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1205 /* initialize to-be-configured parameters */
1207 dev->max_sectors = 0;
1215 * common ATA, ATAPI feature tests
1218 /* we require DMA support (bits 8 of word 49) */
1219 if (!ata_id_has_dma(dev->id)) {
1220 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1225 /* find max transfer mode; for printk only */
1226 xfer_mask = ata_id_xfermask(dev->id);
1228 ata_dump_id(dev->id);
1230 /* ATA-specific feature tests */
1231 if (dev->class == ATA_DEV_ATA) {
1232 dev->n_sectors = ata_id_n_sectors(dev->id);
1234 if (ata_id_has_lba(dev->id)) {
1235 const char *lba_desc;
1238 dev->flags |= ATA_DFLAG_LBA;
1239 if (ata_id_has_lba48(dev->id)) {
1240 dev->flags |= ATA_DFLAG_LBA48;
1244 /* print device info to dmesg */
1246 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1247 "max %s, %Lu sectors: %s\n",
1249 ata_id_major_version(dev->id),
1250 ata_mode_string(xfer_mask),
1251 (unsigned long long)dev->n_sectors,
1256 /* Default translation */
1257 dev->cylinders = dev->id[1];
1258 dev->heads = dev->id[3];
1259 dev->sectors = dev->id[6];
1261 if (ata_id_current_chs_valid(dev->id)) {
1262 /* Current CHS translation is valid. */
1263 dev->cylinders = dev->id[54];
1264 dev->heads = dev->id[55];
1265 dev->sectors = dev->id[56];
1268 /* print device info to dmesg */
1270 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1271 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1273 ata_id_major_version(dev->id),
1274 ata_mode_string(xfer_mask),
1275 (unsigned long long)dev->n_sectors,
1276 dev->cylinders, dev->heads, dev->sectors);
1279 if (dev->id[59] & 0x100) {
1280 dev->multi_count = dev->id[59] & 0xff;
1281 DPRINTK("ata%u: dev %u multi count %u\n",
1282 ap->id, device, dev->multi_count);
1287 /* ATAPI-specific feature tests */
1288 else if (dev->class == ATA_DEV_ATAPI) {
1289 rc = atapi_cdb_len(dev->id);
1290 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1291 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1295 dev->cdb_len = (unsigned int) rc;
1297 if (ata_id_cdb_intr(dev->id))
1298 dev->flags |= ATA_DFLAG_CDB_INTR;
1300 /* print device info to dmesg */
1302 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1303 ap->id, dev->devno, ata_mode_string(xfer_mask));
1306 ap->host->max_cmd_len = 0;
1307 for (i = 0; i < ATA_MAX_DEVICES; i++)
1308 ap->host->max_cmd_len = max_t(unsigned int,
1309 ap->host->max_cmd_len,
1310 ap->device[i].cdb_len);
1312 /* limit bridge transfers to udma5, 200 sectors */
1313 if (ata_dev_knobble(ap, dev)) {
1315 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1316 ap->id, dev->devno);
1317 ap->udma_mask &= ATA_UDMA5;
1318 dev->max_sectors = ATA_MAX_SECTORS;
1321 if (ap->ops->dev_config)
1322 ap->ops->dev_config(ap, dev);
1324 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1328 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1329 ap->id, dev->devno);
1330 DPRINTK("EXIT, err\n");
1335 * ata_bus_probe - Reset and probe ATA bus
1338 * Master ATA bus probing function. Initiates a hardware-dependent
1339 * bus reset, then attempts to identify any devices found on
1343 * PCI/etc. bus probe sem.
1346 * Zero on success, non-zero on error.
1349 static int ata_bus_probe(struct ata_port *ap)
1351 unsigned int classes[ATA_MAX_DEVICES];
1352 unsigned int i, rc, found = 0;
1357 if (ap->ops->probe_reset) {
1358 for (i = 0; i < ATA_MAX_DEVICES; i++)
1359 classes[i] = ATA_DEV_UNKNOWN;
1361 rc = ap->ops->probe_reset(ap, classes);
1363 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1367 for (i = 0; i < ATA_MAX_DEVICES; i++)
1368 if (classes[i] == ATA_DEV_UNKNOWN)
1369 classes[i] = ATA_DEV_NONE;
1371 ap->ops->phy_reset(ap);
1373 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1374 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1375 classes[i] = ap->device[i].class;
1377 ap->device[i].class = ATA_DEV_UNKNOWN;
1382 /* read IDENTIFY page and configure devices */
1383 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1384 struct ata_device *dev = &ap->device[i];
1386 dev->class = classes[i];
1388 if (!ata_dev_present(dev))
1391 WARN_ON(dev->id != NULL);
1392 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1393 dev->class = ATA_DEV_NONE;
1397 if (ata_dev_configure(ap, dev, 1)) {
1398 dev->class++; /* disable device */
1406 goto err_out_disable;
1409 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1410 goto err_out_disable;
1415 ap->ops->port_disable(ap);
1420 * ata_port_probe - Mark port as enabled
1421 * @ap: Port for which we indicate enablement
1423 * Modify @ap data structure such that the system
1424 * thinks that the entire port is enabled.
1426 * LOCKING: host_set lock, or some other form of
1430 void ata_port_probe(struct ata_port *ap)
1432 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1436 * sata_print_link_status - Print SATA link status
1437 * @ap: SATA port to printk link status about
1439 * This function prints link speed and status of a SATA link.
1444 static void sata_print_link_status(struct ata_port *ap)
1449 if (!ap->ops->scr_read)
1452 sstatus = scr_read(ap, SCR_STATUS);
1454 if (sata_dev_present(ap)) {
1455 tmp = (sstatus >> 4) & 0xf;
1458 else if (tmp & (1 << 1))
1461 speed = "<unknown>";
1462 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1463 ap->id, speed, sstatus);
1465 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1471 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1472 * @ap: SATA port associated with target SATA PHY.
1474 * This function issues commands to standard SATA Sxxx
1475 * PHY registers, to wake up the phy (and device), and
1476 * clear any reset condition.
1479 * PCI/etc. bus probe sem.
1482 void __sata_phy_reset(struct ata_port *ap)
1485 unsigned long timeout = jiffies + (HZ * 5);
1487 if (ap->flags & ATA_FLAG_SATA_RESET) {
1488 /* issue phy wake/reset */
1489 scr_write_flush(ap, SCR_CONTROL, 0x301);
1490 /* Couldn't find anything in SATA I/II specs, but
1491 * AHCI-1.1 10.4.2 says at least 1 ms. */
1494 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1496 /* wait for phy to become ready, if necessary */
1499 sstatus = scr_read(ap, SCR_STATUS);
1500 if ((sstatus & 0xf) != 1)
1502 } while (time_before(jiffies, timeout));
1504 /* print link status */
1505 sata_print_link_status(ap);
1507 /* TODO: phy layer with polling, timeouts, etc. */
1508 if (sata_dev_present(ap))
1511 ata_port_disable(ap);
1513 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1516 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1517 ata_port_disable(ap);
1521 ap->cbl = ATA_CBL_SATA;
1525 * sata_phy_reset - Reset SATA bus.
1526 * @ap: SATA port associated with target SATA PHY.
1528 * This function resets the SATA bus, and then probes
1529 * the bus for devices.
1532 * PCI/etc. bus probe sem.
1535 void sata_phy_reset(struct ata_port *ap)
1537 __sata_phy_reset(ap);
1538 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1544 * ata_port_disable - Disable port.
1545 * @ap: Port to be disabled.
1547 * Modify @ap data structure such that the system
1548 * thinks that the entire port is disabled, and should
1549 * never attempt to probe or communicate with devices
1552 * LOCKING: host_set lock, or some other form of
1556 void ata_port_disable(struct ata_port *ap)
1558 ap->device[0].class = ATA_DEV_NONE;
1559 ap->device[1].class = ATA_DEV_NONE;
1560 ap->flags |= ATA_FLAG_PORT_DISABLED;
1564 * This mode timing computation functionality is ported over from
1565 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1568 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1569 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1570 * for PIO 5, which is a nonstandard extension and UDMA6, which
1571 * is currently supported only by Maxtor drives.
1574 static const struct ata_timing ata_timing[] = {
1576 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1577 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1578 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1579 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1581 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1582 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1583 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1585 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1587 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1588 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1589 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1591 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1592 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1593 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1595 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1596 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1597 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1599 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1600 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1601 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1603 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1608 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1609 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1611 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1613 q->setup = EZ(t->setup * 1000, T);
1614 q->act8b = EZ(t->act8b * 1000, T);
1615 q->rec8b = EZ(t->rec8b * 1000, T);
1616 q->cyc8b = EZ(t->cyc8b * 1000, T);
1617 q->active = EZ(t->active * 1000, T);
1618 q->recover = EZ(t->recover * 1000, T);
1619 q->cycle = EZ(t->cycle * 1000, T);
1620 q->udma = EZ(t->udma * 1000, UT);
1623 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1624 struct ata_timing *m, unsigned int what)
1626 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1627 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1628 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1629 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1630 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1631 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1632 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1633 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1636 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1638 const struct ata_timing *t;
1640 for (t = ata_timing; t->mode != speed; t++)
1641 if (t->mode == 0xFF)
1646 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1647 struct ata_timing *t, int T, int UT)
1649 const struct ata_timing *s;
1650 struct ata_timing p;
1656 if (!(s = ata_timing_find_mode(speed)))
1659 memcpy(t, s, sizeof(*s));
1662 * If the drive is an EIDE drive, it can tell us it needs extended
1663 * PIO/MW_DMA cycle timing.
1666 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1667 memset(&p, 0, sizeof(p));
1668 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1669 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1670 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1671 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1672 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1674 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1678 * Convert the timing to bus clock counts.
1681 ata_timing_quantize(t, t, T, UT);
1684 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1685 * S.M.A.R.T * and some other commands. We have to ensure that the
1686 * DMA cycle timing is slower/equal than the fastest PIO timing.
1689 if (speed > XFER_PIO_4) {
1690 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1691 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1695 * Lengthen active & recovery time so that cycle time is correct.
1698 if (t->act8b + t->rec8b < t->cyc8b) {
1699 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1700 t->rec8b = t->cyc8b - t->act8b;
1703 if (t->active + t->recover < t->cycle) {
1704 t->active += (t->cycle - (t->active + t->recover)) / 2;
1705 t->recover = t->cycle - t->active;
1711 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1713 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1716 if (dev->xfer_shift == ATA_SHIFT_PIO)
1717 dev->flags |= ATA_DFLAG_PIO;
1719 ata_dev_set_xfermode(ap, dev);
1721 if (ata_dev_revalidate(ap, dev, 0)) {
1722 printk(KERN_ERR "ata%u: failed to revalidate after set "
1723 "xfermode, disabled\n", ap->id);
1724 ata_port_disable(ap);
1727 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1728 dev->xfer_shift, (int)dev->xfer_mode);
1730 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1732 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
1735 static int ata_host_set_pio(struct ata_port *ap)
1739 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1740 struct ata_device *dev = &ap->device[i];
1742 if (!ata_dev_present(dev))
1745 if (!dev->pio_mode) {
1746 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1750 dev->xfer_mode = dev->pio_mode;
1751 dev->xfer_shift = ATA_SHIFT_PIO;
1752 if (ap->ops->set_piomode)
1753 ap->ops->set_piomode(ap, dev);
1759 static void ata_host_set_dma(struct ata_port *ap)
1763 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1764 struct ata_device *dev = &ap->device[i];
1766 if (!ata_dev_present(dev) || !dev->dma_mode)
1769 dev->xfer_mode = dev->dma_mode;
1770 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1771 if (ap->ops->set_dmamode)
1772 ap->ops->set_dmamode(ap, dev);
1777 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1778 * @ap: port on which timings will be programmed
1780 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1783 * PCI/etc. bus probe sem.
1785 static void ata_set_mode(struct ata_port *ap)
1789 /* step 1: calculate xfer_mask */
1790 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1791 struct ata_device *dev = &ap->device[i];
1792 unsigned int xfer_mask;
1794 if (!ata_dev_present(dev))
1797 xfer_mask = ata_dev_xfermask(ap, dev);
1799 dev->pio_mode = ata_xfer_mask2mode(xfer_mask & ATA_MASK_PIO);
1800 dev->dma_mode = ata_xfer_mask2mode(xfer_mask & (ATA_MASK_MWDMA |
1804 /* step 2: always set host PIO timings */
1805 rc = ata_host_set_pio(ap);
1809 /* step 3: set host DMA timings */
1810 ata_host_set_dma(ap);
1812 /* step 4: update devices' xfer mode */
1813 for (i = 0; i < ATA_MAX_DEVICES; i++)
1814 ata_dev_set_mode(ap, &ap->device[i]);
1816 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1819 if (ap->ops->post_set_mode)
1820 ap->ops->post_set_mode(ap);
1825 ata_port_disable(ap);
1829 * ata_tf_to_host - issue ATA taskfile to host controller
1830 * @ap: port to which command is being issued
1831 * @tf: ATA taskfile register set
1833 * Issues ATA taskfile register set to ATA host controller,
1834 * with proper synchronization with interrupt handler and
1838 * spin_lock_irqsave(host_set lock)
1841 static inline void ata_tf_to_host(struct ata_port *ap,
1842 const struct ata_taskfile *tf)
1844 ap->ops->tf_load(ap, tf);
1845 ap->ops->exec_command(ap, tf);
1849 * ata_busy_sleep - sleep until BSY clears, or timeout
1850 * @ap: port containing status register to be polled
1851 * @tmout_pat: impatience timeout
1852 * @tmout: overall timeout
1854 * Sleep until ATA Status register bit BSY clears,
1855 * or a timeout occurs.
1860 unsigned int ata_busy_sleep (struct ata_port *ap,
1861 unsigned long tmout_pat, unsigned long tmout)
1863 unsigned long timer_start, timeout;
1866 status = ata_busy_wait(ap, ATA_BUSY, 300);
1867 timer_start = jiffies;
1868 timeout = timer_start + tmout_pat;
1869 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1871 status = ata_busy_wait(ap, ATA_BUSY, 3);
1874 if (status & ATA_BUSY)
1875 printk(KERN_WARNING "ata%u is slow to respond, "
1876 "please be patient\n", ap->id);
1878 timeout = timer_start + tmout;
1879 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1881 status = ata_chk_status(ap);
1884 if (status & ATA_BUSY) {
1885 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1886 ap->id, tmout / HZ);
1893 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1895 struct ata_ioports *ioaddr = &ap->ioaddr;
1896 unsigned int dev0 = devmask & (1 << 0);
1897 unsigned int dev1 = devmask & (1 << 1);
1898 unsigned long timeout;
1900 /* if device 0 was found in ata_devchk, wait for its
1904 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1906 /* if device 1 was found in ata_devchk, wait for
1907 * register access, then wait for BSY to clear
1909 timeout = jiffies + ATA_TMOUT_BOOT;
1913 ap->ops->dev_select(ap, 1);
1914 if (ap->flags & ATA_FLAG_MMIO) {
1915 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1916 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1918 nsect = inb(ioaddr->nsect_addr);
1919 lbal = inb(ioaddr->lbal_addr);
1921 if ((nsect == 1) && (lbal == 1))
1923 if (time_after(jiffies, timeout)) {
1927 msleep(50); /* give drive a breather */
1930 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1932 /* is all this really necessary? */
1933 ap->ops->dev_select(ap, 0);
1935 ap->ops->dev_select(ap, 1);
1937 ap->ops->dev_select(ap, 0);
1941 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1942 * @ap: Port to reset and probe
1944 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1945 * probe the bus. Not often used these days.
1948 * PCI/etc. bus probe sem.
1949 * Obtains host_set lock.
1953 static unsigned int ata_bus_edd(struct ata_port *ap)
1955 struct ata_taskfile tf;
1956 unsigned long flags;
1958 /* set up execute-device-diag (bus reset) taskfile */
1959 /* also, take interrupts to a known state (disabled) */
1960 DPRINTK("execute-device-diag\n");
1961 ata_tf_init(ap, &tf, 0);
1963 tf.command = ATA_CMD_EDD;
1964 tf.protocol = ATA_PROT_NODATA;
1967 spin_lock_irqsave(&ap->host_set->lock, flags);
1968 ata_tf_to_host(ap, &tf);
1969 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1971 /* spec says at least 2ms. but who knows with those
1972 * crazy ATAPI devices...
1976 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1979 static unsigned int ata_bus_softreset(struct ata_port *ap,
1980 unsigned int devmask)
1982 struct ata_ioports *ioaddr = &ap->ioaddr;
1984 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1986 /* software reset. causes dev0 to be selected */
1987 if (ap->flags & ATA_FLAG_MMIO) {
1988 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1989 udelay(20); /* FIXME: flush */
1990 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1991 udelay(20); /* FIXME: flush */
1992 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1994 outb(ap->ctl, ioaddr->ctl_addr);
1996 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1998 outb(ap->ctl, ioaddr->ctl_addr);
2001 /* spec mandates ">= 2ms" before checking status.
2002 * We wait 150ms, because that was the magic delay used for
2003 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2004 * between when the ATA command register is written, and then
2005 * status is checked. Because waiting for "a while" before
2006 * checking status is fine, post SRST, we perform this magic
2007 * delay here as well.
2011 ata_bus_post_reset(ap, devmask);
2017 * ata_bus_reset - reset host port and associated ATA channel
2018 * @ap: port to reset
2020 * This is typically the first time we actually start issuing
2021 * commands to the ATA channel. We wait for BSY to clear, then
2022 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2023 * result. Determine what devices, if any, are on the channel
2024 * by looking at the device 0/1 error register. Look at the signature
2025 * stored in each device's taskfile registers, to determine if
2026 * the device is ATA or ATAPI.
2029 * PCI/etc. bus probe sem.
2030 * Obtains host_set lock.
2033 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2036 void ata_bus_reset(struct ata_port *ap)
2038 struct ata_ioports *ioaddr = &ap->ioaddr;
2039 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2041 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2043 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2045 /* determine if device 0/1 are present */
2046 if (ap->flags & ATA_FLAG_SATA_RESET)
2049 dev0 = ata_devchk(ap, 0);
2051 dev1 = ata_devchk(ap, 1);
2055 devmask |= (1 << 0);
2057 devmask |= (1 << 1);
2059 /* select device 0 again */
2060 ap->ops->dev_select(ap, 0);
2062 /* issue bus reset */
2063 if (ap->flags & ATA_FLAG_SRST)
2064 rc = ata_bus_softreset(ap, devmask);
2065 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2066 /* set up device control */
2067 if (ap->flags & ATA_FLAG_MMIO)
2068 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2070 outb(ap->ctl, ioaddr->ctl_addr);
2071 rc = ata_bus_edd(ap);
2078 * determine by signature whether we have ATA or ATAPI devices
2080 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
2081 if ((slave_possible) && (err != 0x81))
2082 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
2084 /* re-enable interrupts */
2085 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2088 /* is double-select really necessary? */
2089 if (ap->device[1].class != ATA_DEV_NONE)
2090 ap->ops->dev_select(ap, 1);
2091 if (ap->device[0].class != ATA_DEV_NONE)
2092 ap->ops->dev_select(ap, 0);
2094 /* if no devices were detected, disable this port */
2095 if ((ap->device[0].class == ATA_DEV_NONE) &&
2096 (ap->device[1].class == ATA_DEV_NONE))
2099 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2100 /* set up device control for ATA_FLAG_SATA_RESET */
2101 if (ap->flags & ATA_FLAG_MMIO)
2102 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2104 outb(ap->ctl, ioaddr->ctl_addr);
2111 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2112 ap->ops->port_disable(ap);
2117 static int sata_phy_resume(struct ata_port *ap)
2119 unsigned long timeout = jiffies + (HZ * 5);
2122 scr_write_flush(ap, SCR_CONTROL, 0x300);
2124 /* Wait for phy to become ready, if necessary. */
2127 sstatus = scr_read(ap, SCR_STATUS);
2128 if ((sstatus & 0xf) != 1)
2130 } while (time_before(jiffies, timeout));
2136 * ata_std_probeinit - initialize probing
2137 * @ap: port to be probed
2139 * @ap is about to be probed. Initialize it. This function is
2140 * to be used as standard callback for ata_drive_probe_reset().
2142 * NOTE!!! Do not use this function as probeinit if a low level
2143 * driver implements only hardreset. Just pass NULL as probeinit
2144 * in that case. Using this function is probably okay but doing
2145 * so makes reset sequence different from the original
2146 * ->phy_reset implementation and Jeff nervous. :-P
2148 extern void ata_std_probeinit(struct ata_port *ap)
2150 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
2151 sata_phy_resume(ap);
2152 if (sata_dev_present(ap))
2153 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2158 * ata_std_softreset - reset host port via ATA SRST
2159 * @ap: port to reset
2160 * @verbose: fail verbosely
2161 * @classes: resulting classes of attached devices
2163 * Reset host port using ATA SRST. This function is to be used
2164 * as standard callback for ata_drive_*_reset() functions.
2167 * Kernel thread context (may sleep)
2170 * 0 on success, -errno otherwise.
2172 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2174 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2175 unsigned int devmask = 0, err_mask;
2180 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2181 classes[0] = ATA_DEV_NONE;
2185 /* determine if device 0/1 are present */
2186 if (ata_devchk(ap, 0))
2187 devmask |= (1 << 0);
2188 if (slave_possible && ata_devchk(ap, 1))
2189 devmask |= (1 << 1);
2191 /* select device 0 again */
2192 ap->ops->dev_select(ap, 0);
2194 /* issue bus reset */
2195 DPRINTK("about to softreset, devmask=%x\n", devmask);
2196 err_mask = ata_bus_softreset(ap, devmask);
2199 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2202 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2207 /* determine by signature whether we have ATA or ATAPI devices */
2208 classes[0] = ata_dev_try_classify(ap, 0, &err);
2209 if (slave_possible && err != 0x81)
2210 classes[1] = ata_dev_try_classify(ap, 1, &err);
2213 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2218 * sata_std_hardreset - reset host port via SATA phy reset
2219 * @ap: port to reset
2220 * @verbose: fail verbosely
2221 * @class: resulting class of attached device
2223 * SATA phy-reset host port using DET bits of SControl register.
2224 * This function is to be used as standard callback for
2225 * ata_drive_*_reset().
2228 * Kernel thread context (may sleep)
2231 * 0 on success, -errno otherwise.
2233 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2237 /* Issue phy wake/reset */
2238 scr_write_flush(ap, SCR_CONTROL, 0x301);
2241 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2242 * 10.4.2 says at least 1 ms.
2246 /* Bring phy back */
2247 sata_phy_resume(ap);
2249 /* TODO: phy layer with polling, timeouts, etc. */
2250 if (!sata_dev_present(ap)) {
2251 *class = ATA_DEV_NONE;
2252 DPRINTK("EXIT, link offline\n");
2256 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2258 printk(KERN_ERR "ata%u: COMRESET failed "
2259 "(device not ready)\n", ap->id);
2261 DPRINTK("EXIT, device not ready\n");
2265 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2267 *class = ata_dev_try_classify(ap, 0, NULL);
2269 DPRINTK("EXIT, class=%u\n", *class);
2274 * ata_std_postreset - standard postreset callback
2275 * @ap: the target ata_port
2276 * @classes: classes of attached devices
2278 * This function is invoked after a successful reset. Note that
2279 * the device might have been reset more than once using
2280 * different reset methods before postreset is invoked.
2282 * This function is to be used as standard callback for
2283 * ata_drive_*_reset().
2286 * Kernel thread context (may sleep)
2288 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2292 /* set cable type if it isn't already set */
2293 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2294 ap->cbl = ATA_CBL_SATA;
2296 /* print link status */
2297 if (ap->cbl == ATA_CBL_SATA)
2298 sata_print_link_status(ap);
2300 /* re-enable interrupts */
2301 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2304 /* is double-select really necessary? */
2305 if (classes[0] != ATA_DEV_NONE)
2306 ap->ops->dev_select(ap, 1);
2307 if (classes[1] != ATA_DEV_NONE)
2308 ap->ops->dev_select(ap, 0);
2310 /* bail out if no device is present */
2311 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2312 DPRINTK("EXIT, no device\n");
2316 /* set up device control */
2317 if (ap->ioaddr.ctl_addr) {
2318 if (ap->flags & ATA_FLAG_MMIO)
2319 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2321 outb(ap->ctl, ap->ioaddr.ctl_addr);
2328 * ata_std_probe_reset - standard probe reset method
2329 * @ap: prot to perform probe-reset
2330 * @classes: resulting classes of attached devices
2332 * The stock off-the-shelf ->probe_reset method.
2335 * Kernel thread context (may sleep)
2338 * 0 on success, -errno otherwise.
2340 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2342 ata_reset_fn_t hardreset;
2345 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2346 hardreset = sata_std_hardreset;
2348 return ata_drive_probe_reset(ap, ata_std_probeinit,
2349 ata_std_softreset, hardreset,
2350 ata_std_postreset, classes);
2353 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2354 ata_postreset_fn_t postreset,
2355 unsigned int *classes)
2359 for (i = 0; i < ATA_MAX_DEVICES; i++)
2360 classes[i] = ATA_DEV_UNKNOWN;
2362 rc = reset(ap, 0, classes);
2366 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2367 * is complete and convert all ATA_DEV_UNKNOWN to
2370 for (i = 0; i < ATA_MAX_DEVICES; i++)
2371 if (classes[i] != ATA_DEV_UNKNOWN)
2374 if (i < ATA_MAX_DEVICES)
2375 for (i = 0; i < ATA_MAX_DEVICES; i++)
2376 if (classes[i] == ATA_DEV_UNKNOWN)
2377 classes[i] = ATA_DEV_NONE;
2380 postreset(ap, classes);
2382 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2386 * ata_drive_probe_reset - Perform probe reset with given methods
2387 * @ap: port to reset
2388 * @probeinit: probeinit method (can be NULL)
2389 * @softreset: softreset method (can be NULL)
2390 * @hardreset: hardreset method (can be NULL)
2391 * @postreset: postreset method (can be NULL)
2392 * @classes: resulting classes of attached devices
2394 * Reset the specified port and classify attached devices using
2395 * given methods. This function prefers softreset but tries all
2396 * possible reset sequences to reset and classify devices. This
2397 * function is intended to be used for constructing ->probe_reset
2398 * callback by low level drivers.
2400 * Reset methods should follow the following rules.
2402 * - Return 0 on sucess, -errno on failure.
2403 * - If classification is supported, fill classes[] with
2404 * recognized class codes.
2405 * - If classification is not supported, leave classes[] alone.
2406 * - If verbose is non-zero, print error message on failure;
2407 * otherwise, shut up.
2410 * Kernel thread context (may sleep)
2413 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2414 * if classification fails, and any error code from reset
2417 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2418 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2419 ata_postreset_fn_t postreset, unsigned int *classes)
2427 rc = do_probe_reset(ap, softreset, postreset, classes);
2435 rc = do_probe_reset(ap, hardreset, postreset, classes);
2436 if (rc == 0 || rc != -ENODEV)
2440 rc = do_probe_reset(ap, softreset, postreset, classes);
2446 * ata_dev_same_device - Determine whether new ID matches configured device
2447 * @ap: port on which the device to compare against resides
2448 * @dev: device to compare against
2449 * @new_class: class of the new device
2450 * @new_id: IDENTIFY page of the new device
2452 * Compare @new_class and @new_id against @dev and determine
2453 * whether @dev is the device indicated by @new_class and
2460 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2462 static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2463 unsigned int new_class, const u16 *new_id)
2465 const u16 *old_id = dev->id;
2466 unsigned char model[2][41], serial[2][21];
2469 if (dev->class != new_class) {
2471 "ata%u: dev %u class mismatch %d != %d\n",
2472 ap->id, dev->devno, dev->class, new_class);
2476 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2477 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2478 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2479 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2480 new_n_sectors = ata_id_n_sectors(new_id);
2482 if (strcmp(model[0], model[1])) {
2484 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2485 ap->id, dev->devno, model[0], model[1]);
2489 if (strcmp(serial[0], serial[1])) {
2491 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2492 ap->id, dev->devno, serial[0], serial[1]);
2496 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2498 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2499 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2500 (unsigned long long)new_n_sectors);
2508 * ata_dev_revalidate - Revalidate ATA device
2509 * @ap: port on which the device to revalidate resides
2510 * @dev: device to revalidate
2511 * @post_reset: is this revalidation after reset?
2513 * Re-read IDENTIFY page and make sure @dev is still attached to
2517 * Kernel thread context (may sleep)
2520 * 0 on success, negative errno otherwise
2522 int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2529 if (!ata_dev_present(dev))
2535 /* allocate & read ID data */
2536 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2540 /* is the device still there? */
2541 if (!ata_dev_same_device(ap, dev, class, id)) {
2549 /* configure device according to the new ID */
2550 return ata_dev_configure(ap, dev, 0);
2553 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2554 ap->id, dev->devno, rc);
2559 static const char * const ata_dma_blacklist [] = {
2578 "Toshiba CD-ROM XM-6202B",
2579 "TOSHIBA CD-ROM XM-1702BC",
2581 "E-IDE CD-ROM CR-840",
2584 "SAMSUNG CD-ROM SC-148C",
2585 "SAMSUNG CD-ROM SC",
2587 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2591 static int ata_dma_blacklisted(const struct ata_device *dev)
2593 unsigned char model_num[41];
2596 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
2598 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2599 if (!strcmp(ata_dma_blacklist[i], model_num))
2606 * ata_dev_xfermask - Compute supported xfermask of the given device
2607 * @ap: Port on which the device to compute xfermask for resides
2608 * @dev: Device to compute xfermask for
2610 * Compute supported xfermask of @dev. This function is
2611 * responsible for applying all known limits including host
2612 * controller limits, device blacklist, etc...
2618 * Computed xfermask.
2620 static unsigned int ata_dev_xfermask(struct ata_port *ap,
2621 struct ata_device *dev)
2623 unsigned long xfer_mask;
2626 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2629 /* use port-wide xfermask for now */
2630 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2631 struct ata_device *d = &ap->device[i];
2632 if (!ata_dev_present(d))
2634 xfer_mask &= ata_id_xfermask(d->id);
2635 if (ata_dma_blacklisted(d))
2636 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2639 if (ata_dma_blacklisted(dev))
2640 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2641 "disabling DMA\n", ap->id, dev->devno);
2647 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2648 * @ap: Port associated with device @dev
2649 * @dev: Device to which command will be sent
2651 * Issue SET FEATURES - XFER MODE command to device @dev
2655 * PCI/etc. bus probe sem.
2658 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2660 struct ata_taskfile tf;
2662 /* set up set-features taskfile */
2663 DPRINTK("set features - xfer mode\n");
2665 ata_tf_init(ap, &tf, dev->devno);
2666 tf.command = ATA_CMD_SET_FEATURES;
2667 tf.feature = SETFEATURES_XFER;
2668 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2669 tf.protocol = ATA_PROT_NODATA;
2670 tf.nsect = dev->xfer_mode;
2672 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2673 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2675 ata_port_disable(ap);
2682 * ata_dev_init_params - Issue INIT DEV PARAMS command
2683 * @ap: Port associated with device @dev
2684 * @dev: Device to which command will be sent
2687 * Kernel thread context (may sleep)
2690 * 0 on success, AC_ERR_* mask otherwise.
2693 static unsigned int ata_dev_init_params(struct ata_port *ap,
2694 struct ata_device *dev)
2696 struct ata_taskfile tf;
2697 unsigned int err_mask;
2698 u16 sectors = dev->id[6];
2699 u16 heads = dev->id[3];
2701 /* Number of sectors per track 1-255. Number of heads 1-16 */
2702 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2705 /* set up init dev params taskfile */
2706 DPRINTK("init dev params \n");
2708 ata_tf_init(ap, &tf, dev->devno);
2709 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2710 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2711 tf.protocol = ATA_PROT_NODATA;
2713 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2715 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2717 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2722 * ata_sg_clean - Unmap DMA memory associated with command
2723 * @qc: Command containing DMA memory to be released
2725 * Unmap all mapped DMA memory associated with this command.
2728 * spin_lock_irqsave(host_set lock)
2731 static void ata_sg_clean(struct ata_queued_cmd *qc)
2733 struct ata_port *ap = qc->ap;
2734 struct scatterlist *sg = qc->__sg;
2735 int dir = qc->dma_dir;
2736 void *pad_buf = NULL;
2738 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2739 WARN_ON(sg == NULL);
2741 if (qc->flags & ATA_QCFLAG_SINGLE)
2742 WARN_ON(qc->n_elem > 1);
2744 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2746 /* if we padded the buffer out to 32-bit bound, and data
2747 * xfer direction is from-device, we must copy from the
2748 * pad buffer back into the supplied buffer
2750 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2751 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2753 if (qc->flags & ATA_QCFLAG_SG) {
2755 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2756 /* restore last sg */
2757 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2759 struct scatterlist *psg = &qc->pad_sgent;
2760 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2761 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2762 kunmap_atomic(addr, KM_IRQ0);
2766 dma_unmap_single(ap->host_set->dev,
2767 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2770 sg->length += qc->pad_len;
2772 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2773 pad_buf, qc->pad_len);
2776 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2781 * ata_fill_sg - Fill PCI IDE PRD table
2782 * @qc: Metadata associated with taskfile to be transferred
2784 * Fill PCI IDE PRD (scatter-gather) table with segments
2785 * associated with the current disk command.
2788 * spin_lock_irqsave(host_set lock)
2791 static void ata_fill_sg(struct ata_queued_cmd *qc)
2793 struct ata_port *ap = qc->ap;
2794 struct scatterlist *sg;
2797 WARN_ON(qc->__sg == NULL);
2798 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2801 ata_for_each_sg(sg, qc) {
2805 /* determine if physical DMA addr spans 64K boundary.
2806 * Note h/w doesn't support 64-bit, so we unconditionally
2807 * truncate dma_addr_t to u32.
2809 addr = (u32) sg_dma_address(sg);
2810 sg_len = sg_dma_len(sg);
2813 offset = addr & 0xffff;
2815 if ((offset + sg_len) > 0x10000)
2816 len = 0x10000 - offset;
2818 ap->prd[idx].addr = cpu_to_le32(addr);
2819 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2820 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2829 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2832 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2833 * @qc: Metadata associated with taskfile to check
2835 * Allow low-level driver to filter ATA PACKET commands, returning
2836 * a status indicating whether or not it is OK to use DMA for the
2837 * supplied PACKET command.
2840 * spin_lock_irqsave(host_set lock)
2842 * RETURNS: 0 when ATAPI DMA can be used
2845 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2847 struct ata_port *ap = qc->ap;
2848 int rc = 0; /* Assume ATAPI DMA is OK by default */
2850 if (ap->ops->check_atapi_dma)
2851 rc = ap->ops->check_atapi_dma(qc);
2856 * ata_qc_prep - Prepare taskfile for submission
2857 * @qc: Metadata associated with taskfile to be prepared
2859 * Prepare ATA taskfile for submission.
2862 * spin_lock_irqsave(host_set lock)
2864 void ata_qc_prep(struct ata_queued_cmd *qc)
2866 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2873 * ata_sg_init_one - Associate command with memory buffer
2874 * @qc: Command to be associated
2875 * @buf: Memory buffer
2876 * @buflen: Length of memory buffer, in bytes.
2878 * Initialize the data-related elements of queued_cmd @qc
2879 * to point to a single memory buffer, @buf of byte length @buflen.
2882 * spin_lock_irqsave(host_set lock)
2885 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2887 struct scatterlist *sg;
2889 qc->flags |= ATA_QCFLAG_SINGLE;
2891 memset(&qc->sgent, 0, sizeof(qc->sgent));
2892 qc->__sg = &qc->sgent;
2894 qc->orig_n_elem = 1;
2898 sg_init_one(sg, buf, buflen);
2902 * ata_sg_init - Associate command with scatter-gather table.
2903 * @qc: Command to be associated
2904 * @sg: Scatter-gather table.
2905 * @n_elem: Number of elements in s/g table.
2907 * Initialize the data-related elements of queued_cmd @qc
2908 * to point to a scatter-gather table @sg, containing @n_elem
2912 * spin_lock_irqsave(host_set lock)
2915 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2916 unsigned int n_elem)
2918 qc->flags |= ATA_QCFLAG_SG;
2920 qc->n_elem = n_elem;
2921 qc->orig_n_elem = n_elem;
2925 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2926 * @qc: Command with memory buffer to be mapped.
2928 * DMA-map the memory buffer associated with queued_cmd @qc.
2931 * spin_lock_irqsave(host_set lock)
2934 * Zero on success, negative on error.
2937 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2939 struct ata_port *ap = qc->ap;
2940 int dir = qc->dma_dir;
2941 struct scatterlist *sg = qc->__sg;
2942 dma_addr_t dma_address;
2945 /* we must lengthen transfers to end on a 32-bit boundary */
2946 qc->pad_len = sg->length & 3;
2948 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2949 struct scatterlist *psg = &qc->pad_sgent;
2951 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2953 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2955 if (qc->tf.flags & ATA_TFLAG_WRITE)
2956 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2959 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2960 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2962 sg->length -= qc->pad_len;
2963 if (sg->length == 0)
2966 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2967 sg->length, qc->pad_len);
2975 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2977 if (dma_mapping_error(dma_address)) {
2979 sg->length += qc->pad_len;
2983 sg_dma_address(sg) = dma_address;
2984 sg_dma_len(sg) = sg->length;
2987 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2988 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2994 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2995 * @qc: Command with scatter-gather table to be mapped.
2997 * DMA-map the scatter-gather table associated with queued_cmd @qc.
3000 * spin_lock_irqsave(host_set lock)
3003 * Zero on success, negative on error.
3007 static int ata_sg_setup(struct ata_queued_cmd *qc)
3009 struct ata_port *ap = qc->ap;
3010 struct scatterlist *sg = qc->__sg;
3011 struct scatterlist *lsg = &sg[qc->n_elem - 1];
3012 int n_elem, pre_n_elem, dir, trim_sg = 0;
3014 VPRINTK("ENTER, ata%u\n", ap->id);
3015 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
3017 /* we must lengthen transfers to end on a 32-bit boundary */
3018 qc->pad_len = lsg->length & 3;
3020 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3021 struct scatterlist *psg = &qc->pad_sgent;
3022 unsigned int offset;
3024 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3026 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3029 * psg->page/offset are used to copy to-be-written
3030 * data in this function or read data in ata_sg_clean.
3032 offset = lsg->offset + lsg->length - qc->pad_len;
3033 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3034 psg->offset = offset_in_page(offset);
3036 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3037 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3038 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
3039 kunmap_atomic(addr, KM_IRQ0);
3042 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3043 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3045 lsg->length -= qc->pad_len;
3046 if (lsg->length == 0)
3049 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3050 qc->n_elem - 1, lsg->length, qc->pad_len);
3053 pre_n_elem = qc->n_elem;
3054 if (trim_sg && pre_n_elem)
3063 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
3065 /* restore last sg */
3066 lsg->length += qc->pad_len;
3070 DPRINTK("%d sg elements mapped\n", n_elem);
3073 qc->n_elem = n_elem;
3079 * ata_poll_qc_complete - turn irq back on and finish qc
3080 * @qc: Command to complete
3081 * @err_mask: ATA status register content
3084 * None. (grabs host lock)
3087 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
3089 struct ata_port *ap = qc->ap;
3090 unsigned long flags;
3092 spin_lock_irqsave(&ap->host_set->lock, flags);
3094 ata_qc_complete(qc);
3095 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3099 * ata_pio_poll - poll using PIO, depending on current state
3100 * @ap: the target ata_port
3103 * None. (executing in kernel thread context)
3106 * timeout value to use
3109 static unsigned long ata_pio_poll(struct ata_port *ap)
3111 struct ata_queued_cmd *qc;
3113 unsigned int poll_state = HSM_ST_UNKNOWN;
3114 unsigned int reg_state = HSM_ST_UNKNOWN;
3116 qc = ata_qc_from_tag(ap, ap->active_tag);
3117 WARN_ON(qc == NULL);
3119 switch (ap->hsm_task_state) {
3122 poll_state = HSM_ST_POLL;
3126 case HSM_ST_LAST_POLL:
3127 poll_state = HSM_ST_LAST_POLL;
3128 reg_state = HSM_ST_LAST;
3135 status = ata_chk_status(ap);
3136 if (status & ATA_BUSY) {
3137 if (time_after(jiffies, ap->pio_task_timeout)) {
3138 qc->err_mask |= AC_ERR_TIMEOUT;
3139 ap->hsm_task_state = HSM_ST_TMOUT;
3142 ap->hsm_task_state = poll_state;
3143 return ATA_SHORT_PAUSE;
3146 ap->hsm_task_state = reg_state;
3151 * ata_pio_complete - check if drive is busy or idle
3152 * @ap: the target ata_port
3155 * None. (executing in kernel thread context)
3158 * Zero if qc completed.
3159 * Non-zero if has next.
3162 static int ata_pio_complete (struct ata_port *ap)
3164 struct ata_queued_cmd *qc;
3168 * This is purely heuristic. This is a fast path. Sometimes when
3169 * we enter, BSY will be cleared in a chk-status or two. If not,
3170 * the drive is probably seeking or something. Snooze for a couple
3171 * msecs, then chk-status again. If still busy, fall back to
3172 * HSM_ST_LAST_POLL state.
3174 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3175 if (drv_stat & ATA_BUSY) {
3177 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3178 if (drv_stat & ATA_BUSY) {
3179 ap->hsm_task_state = HSM_ST_LAST_POLL;
3180 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3185 qc = ata_qc_from_tag(ap, ap->active_tag);
3186 WARN_ON(qc == NULL);
3188 drv_stat = ata_wait_idle(ap);
3189 if (!ata_ok(drv_stat)) {
3190 qc->err_mask |= __ac_err_mask(drv_stat);
3191 ap->hsm_task_state = HSM_ST_ERR;
3195 ap->hsm_task_state = HSM_ST_IDLE;
3197 WARN_ON(qc->err_mask);
3198 ata_poll_qc_complete(qc);
3200 /* another command may start at this point */
3207 * swap_buf_le16 - swap halves of 16-bit words in place
3208 * @buf: Buffer to swap
3209 * @buf_words: Number of 16-bit words in buffer.
3211 * Swap halves of 16-bit words if needed to convert from
3212 * little-endian byte order to native cpu byte order, or
3216 * Inherited from caller.
3218 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3223 for (i = 0; i < buf_words; i++)
3224 buf[i] = le16_to_cpu(buf[i]);
3225 #endif /* __BIG_ENDIAN */
3229 * ata_mmio_data_xfer - Transfer data by MMIO
3230 * @ap: port to read/write
3232 * @buflen: buffer length
3233 * @write_data: read/write
3235 * Transfer data from/to the device data register by MMIO.
3238 * Inherited from caller.
3241 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3242 unsigned int buflen, int write_data)
3245 unsigned int words = buflen >> 1;
3246 u16 *buf16 = (u16 *) buf;
3247 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3249 /* Transfer multiple of 2 bytes */
3251 for (i = 0; i < words; i++)
3252 writew(le16_to_cpu(buf16[i]), mmio);
3254 for (i = 0; i < words; i++)
3255 buf16[i] = cpu_to_le16(readw(mmio));
3258 /* Transfer trailing 1 byte, if any. */
3259 if (unlikely(buflen & 0x01)) {
3260 u16 align_buf[1] = { 0 };
3261 unsigned char *trailing_buf = buf + buflen - 1;
3264 memcpy(align_buf, trailing_buf, 1);
3265 writew(le16_to_cpu(align_buf[0]), mmio);
3267 align_buf[0] = cpu_to_le16(readw(mmio));
3268 memcpy(trailing_buf, align_buf, 1);
3274 * ata_pio_data_xfer - Transfer data by PIO
3275 * @ap: port to read/write
3277 * @buflen: buffer length
3278 * @write_data: read/write
3280 * Transfer data from/to the device data register by PIO.
3283 * Inherited from caller.
3286 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3287 unsigned int buflen, int write_data)
3289 unsigned int words = buflen >> 1;
3291 /* Transfer multiple of 2 bytes */
3293 outsw(ap->ioaddr.data_addr, buf, words);
3295 insw(ap->ioaddr.data_addr, buf, words);
3297 /* Transfer trailing 1 byte, if any. */
3298 if (unlikely(buflen & 0x01)) {
3299 u16 align_buf[1] = { 0 };
3300 unsigned char *trailing_buf = buf + buflen - 1;
3303 memcpy(align_buf, trailing_buf, 1);
3304 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3306 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3307 memcpy(trailing_buf, align_buf, 1);
3313 * ata_data_xfer - Transfer data from/to the data register.
3314 * @ap: port to read/write
3316 * @buflen: buffer length
3317 * @do_write: read/write
3319 * Transfer data from/to the device data register.
3322 * Inherited from caller.
3325 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3326 unsigned int buflen, int do_write)
3328 /* Make the crap hardware pay the costs not the good stuff */
3329 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3330 unsigned long flags;
3331 local_irq_save(flags);
3332 if (ap->flags & ATA_FLAG_MMIO)
3333 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3335 ata_pio_data_xfer(ap, buf, buflen, do_write);
3336 local_irq_restore(flags);
3338 if (ap->flags & ATA_FLAG_MMIO)
3339 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3341 ata_pio_data_xfer(ap, buf, buflen, do_write);
3346 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3347 * @qc: Command on going
3349 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3352 * Inherited from caller.
3355 static void ata_pio_sector(struct ata_queued_cmd *qc)
3357 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3358 struct scatterlist *sg = qc->__sg;
3359 struct ata_port *ap = qc->ap;
3361 unsigned int offset;
3364 if (qc->cursect == (qc->nsect - 1))
3365 ap->hsm_task_state = HSM_ST_LAST;
3367 page = sg[qc->cursg].page;
3368 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3370 /* get the current page and offset */
3371 page = nth_page(page, (offset >> PAGE_SHIFT));
3372 offset %= PAGE_SIZE;
3374 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3376 if (PageHighMem(page)) {
3377 unsigned long flags;
3379 local_irq_save(flags);
3380 buf = kmap_atomic(page, KM_IRQ0);
3382 /* do the actual data transfer */
3383 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3385 kunmap_atomic(buf, KM_IRQ0);
3386 local_irq_restore(flags);
3388 buf = page_address(page);
3389 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3395 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3402 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3403 * @qc: Command on going
3405 * Transfer one or many ATA_SECT_SIZE of data from/to the
3406 * ATA device for the DRQ request.
3409 * Inherited from caller.
3412 static void ata_pio_sectors(struct ata_queued_cmd *qc)
3414 if (is_multi_taskfile(&qc->tf)) {
3415 /* READ/WRITE MULTIPLE */
3418 WARN_ON(qc->dev->multi_count == 0);
3420 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3428 * atapi_send_cdb - Write CDB bytes to hardware
3429 * @ap: Port to which ATAPI device is attached.
3430 * @qc: Taskfile currently active
3432 * When device has indicated its readiness to accept
3433 * a CDB, this function is called. Send the CDB.
3439 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3442 DPRINTK("send cdb\n");
3443 WARN_ON(qc->dev->cdb_len < 12);
3445 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3446 ata_altstatus(ap); /* flush */
3448 switch (qc->tf.protocol) {
3449 case ATA_PROT_ATAPI:
3450 ap->hsm_task_state = HSM_ST;
3452 case ATA_PROT_ATAPI_NODATA:
3453 ap->hsm_task_state = HSM_ST_LAST;
3455 case ATA_PROT_ATAPI_DMA:
3456 ap->hsm_task_state = HSM_ST_LAST;
3457 /* initiate bmdma */
3458 ap->ops->bmdma_start(qc);
3464 * ata_pio_first_block - Write first data block to hardware
3465 * @ap: Port to which ATA/ATAPI device is attached.
3467 * When device has indicated its readiness to accept
3468 * the data, this function sends out the CDB or
3469 * the first data block by PIO.
3471 * - If polling, ata_pio_task() handles the rest.
3472 * - Otherwise, interrupt handler takes over.
3475 * Kernel thread context (may sleep)
3478 * Zero if irq handler takes over
3479 * Non-zero if has next (polling).
3482 static int ata_pio_first_block(struct ata_port *ap)
3484 struct ata_queued_cmd *qc;
3486 unsigned long flags;
3489 qc = ata_qc_from_tag(ap, ap->active_tag);
3490 WARN_ON(qc == NULL);
3491 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
3493 /* if polling, we will stay in the work queue after sending the data.
3494 * otherwise, interrupt handler takes over after sending the data.
3496 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3498 /* sleep-wait for BSY to clear */
3499 DPRINTK("busy wait\n");
3500 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
3501 qc->err_mask |= AC_ERR_TIMEOUT;
3502 ap->hsm_task_state = HSM_ST_TMOUT;
3506 /* make sure DRQ is set */
3507 status = ata_chk_status(ap);
3508 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3509 /* device status error */
3510 qc->err_mask |= AC_ERR_HSM;
3511 ap->hsm_task_state = HSM_ST_ERR;
3515 /* Send the CDB (atapi) or the first data block (ata pio out).
3516 * During the state transition, interrupt handler shouldn't
3517 * be invoked before the data transfer is complete and
3518 * hsm_task_state is changed. Hence, the following locking.
3520 spin_lock_irqsave(&ap->host_set->lock, flags);
3522 if (qc->tf.protocol == ATA_PROT_PIO) {
3523 /* PIO data out protocol.
3524 * send first data block.
3527 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3528 * so, the state is changed here before ata_pio_sectors().
3530 ap->hsm_task_state = HSM_ST;
3531 ata_pio_sectors(qc);
3532 ata_altstatus(ap); /* flush */
3535 atapi_send_cdb(ap, qc);
3537 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3539 /* if polling, ata_pio_task() handles the rest.
3540 * otherwise, interrupt handler takes over from here.
3545 return 1; /* has next */
3549 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3550 * @qc: Command on going
3551 * @bytes: number of bytes
3553 * Transfer Transfer data from/to the ATAPI device.
3556 * Inherited from caller.
3560 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3562 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3563 struct scatterlist *sg = qc->__sg;
3564 struct ata_port *ap = qc->ap;
3567 unsigned int offset, count;
3569 if (qc->curbytes + bytes >= qc->nbytes)
3570 ap->hsm_task_state = HSM_ST_LAST;
3573 if (unlikely(qc->cursg >= qc->n_elem)) {
3575 * The end of qc->sg is reached and the device expects
3576 * more data to transfer. In order not to overrun qc->sg
3577 * and fulfill length specified in the byte count register,
3578 * - for read case, discard trailing data from the device
3579 * - for write case, padding zero data to the device
3581 u16 pad_buf[1] = { 0 };
3582 unsigned int words = bytes >> 1;
3585 if (words) /* warning if bytes > 1 */
3586 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3589 for (i = 0; i < words; i++)
3590 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3592 ap->hsm_task_state = HSM_ST_LAST;
3596 sg = &qc->__sg[qc->cursg];
3599 offset = sg->offset + qc->cursg_ofs;
3601 /* get the current page and offset */
3602 page = nth_page(page, (offset >> PAGE_SHIFT));
3603 offset %= PAGE_SIZE;
3605 /* don't overrun current sg */
3606 count = min(sg->length - qc->cursg_ofs, bytes);
3608 /* don't cross page boundaries */
3609 count = min(count, (unsigned int)PAGE_SIZE - offset);
3611 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3613 if (PageHighMem(page)) {
3614 unsigned long flags;
3616 local_irq_save(flags);
3617 buf = kmap_atomic(page, KM_IRQ0);
3619 /* do the actual data transfer */
3620 ata_data_xfer(ap, buf + offset, count, do_write);
3622 kunmap_atomic(buf, KM_IRQ0);
3623 local_irq_restore(flags);
3625 buf = page_address(page);
3626 ata_data_xfer(ap, buf + offset, count, do_write);
3630 qc->curbytes += count;
3631 qc->cursg_ofs += count;
3633 if (qc->cursg_ofs == sg->length) {
3643 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3644 * @qc: Command on going
3646 * Transfer Transfer data from/to the ATAPI device.
3649 * Inherited from caller.
3652 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3654 struct ata_port *ap = qc->ap;
3655 struct ata_device *dev = qc->dev;
3656 unsigned int ireason, bc_lo, bc_hi, bytes;
3657 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3659 ap->ops->tf_read(ap, &qc->tf);
3660 ireason = qc->tf.nsect;
3661 bc_lo = qc->tf.lbam;
3662 bc_hi = qc->tf.lbah;
3663 bytes = (bc_hi << 8) | bc_lo;
3665 /* shall be cleared to zero, indicating xfer of data */
3666 if (ireason & (1 << 0))
3669 /* make sure transfer direction matches expected */
3670 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3671 if (do_write != i_write)
3674 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3676 __atapi_pio_bytes(qc, bytes);
3681 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3682 ap->id, dev->devno);
3683 qc->err_mask |= AC_ERR_HSM;
3684 ap->hsm_task_state = HSM_ST_ERR;
3688 * ata_pio_block - start PIO on a block
3689 * @ap: the target ata_port
3692 * None. (executing in kernel thread context)
3695 static void ata_pio_block(struct ata_port *ap)
3697 struct ata_queued_cmd *qc;
3701 * This is purely heuristic. This is a fast path.
3702 * Sometimes when we enter, BSY will be cleared in
3703 * a chk-status or two. If not, the drive is probably seeking
3704 * or something. Snooze for a couple msecs, then
3705 * chk-status again. If still busy, fall back to
3706 * HSM_ST_POLL state.
3708 status = ata_busy_wait(ap, ATA_BUSY, 5);
3709 if (status & ATA_BUSY) {
3711 status = ata_busy_wait(ap, ATA_BUSY, 10);
3712 if (status & ATA_BUSY) {
3713 ap->hsm_task_state = HSM_ST_POLL;
3714 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3719 qc = ata_qc_from_tag(ap, ap->active_tag);
3720 WARN_ON(qc == NULL);
3723 if (status & (ATA_ERR | ATA_DF)) {
3724 qc->err_mask |= AC_ERR_DEV;
3725 ap->hsm_task_state = HSM_ST_ERR;
3729 /* transfer data if any */
3730 if (is_atapi_taskfile(&qc->tf)) {
3731 /* DRQ=0 means no more data to transfer */
3732 if ((status & ATA_DRQ) == 0) {
3733 ap->hsm_task_state = HSM_ST_LAST;
3737 atapi_pio_bytes(qc);
3739 /* handle BSY=0, DRQ=0 as error */
3740 if ((status & ATA_DRQ) == 0) {
3741 qc->err_mask |= AC_ERR_HSM;
3742 ap->hsm_task_state = HSM_ST_ERR;
3746 ata_pio_sectors(qc);
3749 ata_altstatus(ap); /* flush */
3752 static void ata_pio_error(struct ata_port *ap)
3754 struct ata_queued_cmd *qc;
3756 qc = ata_qc_from_tag(ap, ap->active_tag);
3757 WARN_ON(qc == NULL);
3759 if (qc->tf.command != ATA_CMD_PACKET)
3760 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3762 /* make sure qc->err_mask is available to
3763 * know what's wrong and recover
3765 WARN_ON(qc->err_mask == 0);
3767 ap->hsm_task_state = HSM_ST_IDLE;
3769 ata_poll_qc_complete(qc);
3772 static void ata_pio_task(void *_data)
3774 struct ata_port *ap = _data;
3775 unsigned long timeout;
3782 switch (ap->hsm_task_state) {
3784 has_next = ata_pio_first_block(ap);
3792 has_next = ata_pio_complete(ap);
3796 case HSM_ST_LAST_POLL:
3797 timeout = ata_pio_poll(ap);
3811 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
3812 else if (!qc_completed)
3817 * atapi_packet_task - Write CDB bytes to hardware
3818 * @_data: Port to which ATAPI device is attached.
3820 * When device has indicated its readiness to accept
3821 * a CDB, this function is called. Send the CDB.
3822 * If DMA is to be performed, exit immediately.
3823 * Otherwise, we are in polling mode, so poll
3824 * status under operation succeeds or fails.
3827 * Kernel thread context (may sleep)
3830 static void atapi_packet_task(void *_data)
3832 struct ata_port *ap = _data;
3833 struct ata_queued_cmd *qc;
3836 qc = ata_qc_from_tag(ap, ap->active_tag);
3837 WARN_ON(qc == NULL);
3838 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3840 /* sleep-wait for BSY to clear */
3841 DPRINTK("busy wait\n");
3842 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3843 qc->err_mask |= AC_ERR_TIMEOUT;
3847 /* make sure DRQ is set */
3848 status = ata_chk_status(ap);
3849 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3850 qc->err_mask |= AC_ERR_HSM;
3855 DPRINTK("send cdb\n");
3856 WARN_ON(qc->dev->cdb_len < 12);
3858 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3859 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3860 unsigned long flags;
3862 /* Once we're done issuing command and kicking bmdma,
3863 * irq handler takes over. To not lose irq, we need
3864 * to clear NOINTR flag before sending cdb, but
3865 * interrupt handler shouldn't be invoked before we're
3866 * finished. Hence, the following locking.
3868 spin_lock_irqsave(&ap->host_set->lock, flags);
3869 ap->flags &= ~ATA_FLAG_NOINTR;
3870 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3871 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3872 ap->ops->bmdma_start(qc); /* initiate bmdma */
3873 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3875 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3877 /* PIO commands are handled by polling */
3878 ap->hsm_task_state = HSM_ST;
3879 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3885 ata_poll_qc_complete(qc);
3889 * ata_qc_timeout - Handle timeout of queued command
3890 * @qc: Command that timed out
3892 * Some part of the kernel (currently, only the SCSI layer)
3893 * has noticed that the active command on port @ap has not
3894 * completed after a specified length of time. Handle this
3895 * condition by disabling DMA (if necessary) and completing
3896 * transactions, with error if necessary.
3898 * This also handles the case of the "lost interrupt", where
3899 * for some reason (possibly hardware bug, possibly driver bug)
3900 * an interrupt was not delivered to the driver, even though the
3901 * transaction completed successfully.
3904 * Inherited from SCSI layer (none, can sleep)
3907 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3909 struct ata_port *ap = qc->ap;
3910 struct ata_host_set *host_set = ap->host_set;
3911 u8 host_stat = 0, drv_stat;
3912 unsigned long flags;
3916 ap->hsm_task_state = HSM_ST_IDLE;
3918 spin_lock_irqsave(&host_set->lock, flags);
3920 switch (qc->tf.protocol) {
3923 case ATA_PROT_ATAPI_DMA:
3924 host_stat = ap->ops->bmdma_status(ap);
3926 /* before we do anything else, clear DMA-Start bit */
3927 ap->ops->bmdma_stop(qc);
3933 drv_stat = ata_chk_status(ap);
3935 /* ack bmdma irq events */
3936 ap->ops->irq_clear(ap);
3938 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3939 ap->id, qc->tf.command, drv_stat, host_stat);
3941 ap->hsm_task_state = HSM_ST_IDLE;
3943 /* complete taskfile transaction */
3944 qc->err_mask |= AC_ERR_TIMEOUT;
3948 spin_unlock_irqrestore(&host_set->lock, flags);
3950 ata_eh_qc_complete(qc);
3956 * ata_eng_timeout - Handle timeout of queued command
3957 * @ap: Port on which timed-out command is active
3959 * Some part of the kernel (currently, only the SCSI layer)
3960 * has noticed that the active command on port @ap has not
3961 * completed after a specified length of time. Handle this
3962 * condition by disabling DMA (if necessary) and completing
3963 * transactions, with error if necessary.
3965 * This also handles the case of the "lost interrupt", where
3966 * for some reason (possibly hardware bug, possibly driver bug)
3967 * an interrupt was not delivered to the driver, even though the
3968 * transaction completed successfully.
3971 * Inherited from SCSI layer (none, can sleep)
3974 void ata_eng_timeout(struct ata_port *ap)
3978 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
3984 * ata_qc_new - Request an available ATA command, for queueing
3985 * @ap: Port associated with device @dev
3986 * @dev: Device from whom we request an available command structure
3992 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3994 struct ata_queued_cmd *qc = NULL;
3997 for (i = 0; i < ATA_MAX_QUEUE; i++)
3998 if (!test_and_set_bit(i, &ap->qactive)) {
3999 qc = ata_qc_from_tag(ap, i);
4010 * ata_qc_new_init - Request an available ATA command, and initialize it
4011 * @ap: Port associated with device @dev
4012 * @dev: Device from whom we request an available command structure
4018 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
4019 struct ata_device *dev)
4021 struct ata_queued_cmd *qc;
4023 qc = ata_qc_new(ap);
4036 * ata_qc_free - free unused ata_queued_cmd
4037 * @qc: Command to complete
4039 * Designed to free unused ata_queued_cmd object
4040 * in case something prevents using it.
4043 * spin_lock_irqsave(host_set lock)
4045 void ata_qc_free(struct ata_queued_cmd *qc)
4047 struct ata_port *ap = qc->ap;
4050 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4054 if (likely(ata_tag_valid(tag))) {
4055 if (tag == ap->active_tag)
4056 ap->active_tag = ATA_TAG_POISON;
4057 qc->tag = ATA_TAG_POISON;
4058 clear_bit(tag, &ap->qactive);
4062 void __ata_qc_complete(struct ata_queued_cmd *qc)
4064 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4065 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4067 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4070 /* atapi: mark qc as inactive to prevent the interrupt handler
4071 * from completing the command twice later, before the error handler
4072 * is called. (when rc != 0 and atapi request sense is needed)
4074 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4076 /* call completion callback */
4077 qc->complete_fn(qc);
4080 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4082 struct ata_port *ap = qc->ap;
4084 switch (qc->tf.protocol) {
4086 case ATA_PROT_ATAPI_DMA:
4089 case ATA_PROT_ATAPI:
4091 case ATA_PROT_PIO_MULT:
4092 if (ap->flags & ATA_FLAG_PIO_DMA)
4105 * ata_qc_issue - issue taskfile to device
4106 * @qc: command to issue to device
4108 * Prepare an ATA command to submission to device.
4109 * This includes mapping the data into a DMA-able
4110 * area, filling in the S/G table, and finally
4111 * writing the taskfile to hardware, starting the command.
4114 * spin_lock_irqsave(host_set lock)
4117 * Zero on success, AC_ERR_* mask on failure
4120 unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
4122 struct ata_port *ap = qc->ap;
4124 if (ata_should_dma_map(qc)) {
4125 if (qc->flags & ATA_QCFLAG_SG) {
4126 if (ata_sg_setup(qc))
4128 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4129 if (ata_sg_setup_one(qc))
4133 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4136 ap->ops->qc_prep(qc);
4138 qc->ap->active_tag = qc->tag;
4139 qc->flags |= ATA_QCFLAG_ACTIVE;
4141 return ap->ops->qc_issue(qc);
4144 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4145 return AC_ERR_SYSTEM;
4150 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4151 * @qc: command to issue to device
4153 * Using various libata functions and hooks, this function
4154 * starts an ATA command. ATA commands are grouped into
4155 * classes called "protocols", and issuing each type of protocol
4156 * is slightly different.
4158 * May be used as the qc_issue() entry in ata_port_operations.
4161 * spin_lock_irqsave(host_set lock)
4164 * Zero on success, AC_ERR_* mask on failure
4167 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
4169 struct ata_port *ap = qc->ap;
4171 /* Use polling pio if the LLD doesn't handle
4172 * interrupt driven pio and atapi CDB interrupt.
4174 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4175 switch (qc->tf.protocol) {
4177 case ATA_PROT_ATAPI:
4178 case ATA_PROT_ATAPI_NODATA:
4179 qc->tf.flags |= ATA_TFLAG_POLLING;
4181 case ATA_PROT_ATAPI_DMA:
4182 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
4190 /* select the device */
4191 ata_dev_select(ap, qc->dev->devno, 1, 0);
4193 /* start the command */
4194 switch (qc->tf.protocol) {
4195 case ATA_PROT_NODATA:
4196 if (qc->tf.flags & ATA_TFLAG_POLLING)
4197 ata_qc_set_polling(qc);
4199 ata_tf_to_host(ap, &qc->tf);
4200 ap->hsm_task_state = HSM_ST_LAST;
4202 if (qc->tf.flags & ATA_TFLAG_POLLING)
4203 ata_queue_pio_task(ap);
4208 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
4210 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4211 ap->ops->bmdma_setup(qc); /* set up bmdma */
4212 ap->ops->bmdma_start(qc); /* initiate bmdma */
4213 ap->hsm_task_state = HSM_ST_LAST;
4217 if (qc->tf.flags & ATA_TFLAG_POLLING)
4218 ata_qc_set_polling(qc);
4220 ata_tf_to_host(ap, &qc->tf);
4222 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4223 /* PIO data out protocol */
4224 ap->hsm_task_state = HSM_ST_FIRST;
4225 ata_port_queue_task(ap, ata_pio_task, ap, 0);
4227 /* always send first data block using
4228 * the ata_pio_task() codepath.
4231 /* PIO data in protocol */
4232 ap->hsm_task_state = HSM_ST;
4234 if (qc->tf.flags & ATA_TFLAG_POLLING)
4235 ata_port_queue_task(ap, ata_pio_task, ap, 0);
4237 /* if polling, ata_pio_task() handles the rest.
4238 * otherwise, interrupt handler takes over from here.
4244 case ATA_PROT_ATAPI:
4245 case ATA_PROT_ATAPI_NODATA:
4246 if (qc->tf.flags & ATA_TFLAG_POLLING)
4247 ata_qc_set_polling(qc);
4249 ata_tf_to_host(ap, &qc->tf);
4251 ap->hsm_task_state = HSM_ST_FIRST;
4253 /* send cdb by polling if no cdb interrupt */
4254 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4255 (qc->tf.flags & ATA_TFLAG_POLLING))
4256 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4259 case ATA_PROT_ATAPI_DMA:
4260 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
4262 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4263 ap->ops->bmdma_setup(qc); /* set up bmdma */
4264 ap->hsm_task_state = HSM_ST_FIRST;
4266 /* send cdb by polling if no cdb interrupt */
4267 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4268 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4273 return AC_ERR_SYSTEM;
4280 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
4281 * @qc: Info associated with this ATA transaction.
4284 * spin_lock_irqsave(host_set lock)
4287 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4289 struct ata_port *ap = qc->ap;
4290 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4292 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4294 /* load PRD table addr. */
4295 mb(); /* make sure PRD table writes are visible to controller */
4296 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4298 /* specify data direction, triple-check start bit is clear */
4299 dmactl = readb(mmio + ATA_DMA_CMD);
4300 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4302 dmactl |= ATA_DMA_WR;
4303 writeb(dmactl, mmio + ATA_DMA_CMD);
4305 /* issue r/w command */
4306 ap->ops->exec_command(ap, &qc->tf);
4310 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
4311 * @qc: Info associated with this ATA transaction.
4314 * spin_lock_irqsave(host_set lock)
4317 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4319 struct ata_port *ap = qc->ap;
4320 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4323 /* start host DMA transaction */
4324 dmactl = readb(mmio + ATA_DMA_CMD);
4325 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4327 /* Strictly, one may wish to issue a readb() here, to
4328 * flush the mmio write. However, control also passes
4329 * to the hardware at this point, and it will interrupt
4330 * us when we are to resume control. So, in effect,
4331 * we don't care when the mmio write flushes.
4332 * Further, a read of the DMA status register _immediately_
4333 * following the write may not be what certain flaky hardware
4334 * is expected, so I think it is best to not add a readb()
4335 * without first all the MMIO ATA cards/mobos.
4336 * Or maybe I'm just being paranoid.
4341 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4342 * @qc: Info associated with this ATA transaction.
4345 * spin_lock_irqsave(host_set lock)
4348 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4350 struct ata_port *ap = qc->ap;
4351 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4354 /* load PRD table addr. */
4355 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4357 /* specify data direction, triple-check start bit is clear */
4358 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4359 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4361 dmactl |= ATA_DMA_WR;
4362 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4364 /* issue r/w command */
4365 ap->ops->exec_command(ap, &qc->tf);
4369 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4370 * @qc: Info associated with this ATA transaction.
4373 * spin_lock_irqsave(host_set lock)
4376 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4378 struct ata_port *ap = qc->ap;
4381 /* start host DMA transaction */
4382 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4383 outb(dmactl | ATA_DMA_START,
4384 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4389 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4390 * @qc: Info associated with this ATA transaction.
4392 * Writes the ATA_DMA_START flag to the DMA command register.
4394 * May be used as the bmdma_start() entry in ata_port_operations.
4397 * spin_lock_irqsave(host_set lock)
4399 void ata_bmdma_start(struct ata_queued_cmd *qc)
4401 if (qc->ap->flags & ATA_FLAG_MMIO)
4402 ata_bmdma_start_mmio(qc);
4404 ata_bmdma_start_pio(qc);
4409 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4410 * @qc: Info associated with this ATA transaction.
4412 * Writes address of PRD table to device's PRD Table Address
4413 * register, sets the DMA control register, and calls
4414 * ops->exec_command() to start the transfer.
4416 * May be used as the bmdma_setup() entry in ata_port_operations.
4419 * spin_lock_irqsave(host_set lock)
4421 void ata_bmdma_setup(struct ata_queued_cmd *qc)
4423 if (qc->ap->flags & ATA_FLAG_MMIO)
4424 ata_bmdma_setup_mmio(qc);
4426 ata_bmdma_setup_pio(qc);
4431 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
4432 * @ap: Port associated with this ATA transaction.
4434 * Clear interrupt and error flags in DMA status register.
4436 * May be used as the irq_clear() entry in ata_port_operations.
4439 * spin_lock_irqsave(host_set lock)
4442 void ata_bmdma_irq_clear(struct ata_port *ap)
4444 if (ap->flags & ATA_FLAG_MMIO) {
4445 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4446 writeb(readb(mmio), mmio);
4448 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4449 outb(inb(addr), addr);
4456 * ata_bmdma_status - Read PCI IDE BMDMA status
4457 * @ap: Port associated with this ATA transaction.
4459 * Read and return BMDMA status register.
4461 * May be used as the bmdma_status() entry in ata_port_operations.
4464 * spin_lock_irqsave(host_set lock)
4467 u8 ata_bmdma_status(struct ata_port *ap)
4470 if (ap->flags & ATA_FLAG_MMIO) {
4471 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4472 host_stat = readb(mmio + ATA_DMA_STATUS);
4474 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4480 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4481 * @qc: Command we are ending DMA for
4483 * Clears the ATA_DMA_START flag in the dma control register
4485 * May be used as the bmdma_stop() entry in ata_port_operations.
4488 * spin_lock_irqsave(host_set lock)
4491 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4493 struct ata_port *ap = qc->ap;
4494 if (ap->flags & ATA_FLAG_MMIO) {
4495 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4497 /* clear start/stop bit */
4498 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4499 mmio + ATA_DMA_CMD);
4501 /* clear start/stop bit */
4502 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4503 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4506 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4507 ata_altstatus(ap); /* dummy read */
4511 * ata_host_intr - Handle host interrupt for given (port, task)
4512 * @ap: Port on which interrupt arrived (possibly...)
4513 * @qc: Taskfile currently active in engine
4515 * Handle host interrupt for given queued command. Currently,
4516 * only DMA interrupts are handled. All other commands are
4517 * handled via polling with interrupts disabled (nIEN bit).
4520 * spin_lock_irqsave(host_set lock)
4523 * One if interrupt was handled, zero if not (shared irq).
4526 inline unsigned int ata_host_intr (struct ata_port *ap,
4527 struct ata_queued_cmd *qc)
4529 u8 status, host_stat = 0;
4531 VPRINTK("ata%u: protocol %d task_state %d\n",
4532 ap->id, qc->tf.protocol, ap->hsm_task_state);
4534 /* Check whether we are expecting interrupt in this state */
4535 switch (ap->hsm_task_state) {
4537 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4538 * The flag was turned on only for atapi devices.
4539 * No need to check is_atapi_taskfile(&qc->tf) again.
4541 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4545 if (qc->tf.protocol == ATA_PROT_DMA ||
4546 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4547 /* check status of DMA engine */
4548 host_stat = ap->ops->bmdma_status(ap);
4549 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4551 /* if it's not our irq... */
4552 if (!(host_stat & ATA_DMA_INTR))
4555 /* before we do anything else, clear DMA-Start bit */
4556 ap->ops->bmdma_stop(qc);
4558 if (unlikely(host_stat & ATA_DMA_ERR)) {
4559 /* error when transfering data to/from memory */
4560 qc->err_mask |= AC_ERR_HOST_BUS;
4561 ap->hsm_task_state = HSM_ST_ERR;
4571 /* check altstatus */
4572 status = ata_altstatus(ap);
4573 if (status & ATA_BUSY)
4576 /* check main status, clearing INTRQ */
4577 status = ata_chk_status(ap);
4578 if (unlikely(status & ATA_BUSY))
4581 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4582 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4584 /* ack bmdma irq events */
4585 ap->ops->irq_clear(ap);
4588 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4589 qc->err_mask |= AC_ERR_DEV;
4590 ap->hsm_task_state = HSM_ST_ERR;
4594 switch (ap->hsm_task_state) {
4596 /* Some pre-ATAPI-4 devices assert INTRQ
4597 * at this state when ready to receive CDB.
4600 /* check device status */
4601 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4602 /* Wrong status. Let EH handle this */
4603 qc->err_mask |= AC_ERR_HSM;
4604 ap->hsm_task_state = HSM_ST_ERR;
4608 atapi_send_cdb(ap, qc);
4613 /* complete command or read/write the data register */
4614 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4615 /* ATAPI PIO protocol */
4616 if ((status & ATA_DRQ) == 0) {
4617 /* no more data to transfer */
4618 ap->hsm_task_state = HSM_ST_LAST;
4622 atapi_pio_bytes(qc);
4624 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4625 /* bad ireason reported by device */
4629 /* ATA PIO protocol */
4630 if (unlikely((status & ATA_DRQ) == 0)) {
4631 /* handle BSY=0, DRQ=0 as error */
4632 qc->err_mask |= AC_ERR_HSM;
4633 ap->hsm_task_state = HSM_ST_ERR;
4637 ata_pio_sectors(qc);
4639 if (ap->hsm_task_state == HSM_ST_LAST &&
4640 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4643 status = ata_chk_status(ap);
4648 ata_altstatus(ap); /* flush */
4652 if (unlikely(status & ATA_DRQ)) {
4653 /* handle DRQ=1 as error */
4654 qc->err_mask |= AC_ERR_HSM;
4655 ap->hsm_task_state = HSM_ST_ERR;
4659 /* no more data to transfer */
4660 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4663 ap->hsm_task_state = HSM_ST_IDLE;
4665 /* complete taskfile transaction */
4666 qc->err_mask |= ac_err_mask(status);
4667 ata_qc_complete(qc);
4671 if (qc->tf.command != ATA_CMD_PACKET)
4672 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4673 ap->id, status, host_stat);
4675 /* make sure qc->err_mask is available to
4676 * know what's wrong and recover
4678 WARN_ON(qc->err_mask == 0);
4680 ap->hsm_task_state = HSM_ST_IDLE;
4681 ata_qc_complete(qc);
4687 return 1; /* irq handled */
4690 ap->stats.idle_irq++;
4693 if ((ap->stats.idle_irq % 1000) == 0) {
4695 ata_irq_ack(ap, 0); /* debug trap */
4696 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4699 return 0; /* irq not handled */
4703 * ata_interrupt - Default ATA host interrupt handler
4704 * @irq: irq line (unused)
4705 * @dev_instance: pointer to our ata_host_set information structure
4708 * Default interrupt handler for PCI IDE devices. Calls
4709 * ata_host_intr() for each port that is not disabled.
4712 * Obtains host_set lock during operation.
4715 * IRQ_NONE or IRQ_HANDLED.
4718 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4720 struct ata_host_set *host_set = dev_instance;
4722 unsigned int handled = 0;
4723 unsigned long flags;
4725 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4726 spin_lock_irqsave(&host_set->lock, flags);
4728 for (i = 0; i < host_set->n_ports; i++) {
4729 struct ata_port *ap;
4731 ap = host_set->ports[i];
4733 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
4734 struct ata_queued_cmd *qc;
4736 qc = ata_qc_from_tag(ap, ap->active_tag);
4737 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
4738 (qc->flags & ATA_QCFLAG_ACTIVE))
4739 handled |= ata_host_intr(ap, qc);
4743 spin_unlock_irqrestore(&host_set->lock, flags);
4745 return IRQ_RETVAL(handled);
4750 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4751 * without filling any other registers
4753 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4756 struct ata_taskfile tf;
4759 ata_tf_init(ap, &tf, dev->devno);
4762 tf.flags |= ATA_TFLAG_DEVICE;
4763 tf.protocol = ATA_PROT_NODATA;
4765 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4767 printk(KERN_ERR "%s: ata command failed: %d\n",
4773 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4777 if (!ata_try_flush_cache(dev))
4780 if (ata_id_has_flush_ext(dev->id))
4781 cmd = ATA_CMD_FLUSH_EXT;
4783 cmd = ATA_CMD_FLUSH;
4785 return ata_do_simple_cmd(ap, dev, cmd);
4788 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4790 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4793 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4795 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4799 * ata_device_resume - wakeup a previously suspended devices
4800 * @ap: port the device is connected to
4801 * @dev: the device to resume
4803 * Kick the drive back into action, by sending it an idle immediate
4804 * command and making sure its transfer mode matches between drive
4808 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4810 if (ap->flags & ATA_FLAG_SUSPENDED) {
4811 ap->flags &= ~ATA_FLAG_SUSPENDED;
4814 if (!ata_dev_present(dev))
4816 if (dev->class == ATA_DEV_ATA)
4817 ata_start_drive(ap, dev);
4823 * ata_device_suspend - prepare a device for suspend
4824 * @ap: port the device is connected to
4825 * @dev: the device to suspend
4827 * Flush the cache on the drive, if appropriate, then issue a
4828 * standbynow command.
4830 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4832 if (!ata_dev_present(dev))
4834 if (dev->class == ATA_DEV_ATA)
4835 ata_flush_cache(ap, dev);
4837 ata_standby_drive(ap, dev);
4838 ap->flags |= ATA_FLAG_SUSPENDED;
4843 * ata_port_start - Set port up for dma.
4844 * @ap: Port to initialize
4846 * Called just after data structures for each port are
4847 * initialized. Allocates space for PRD table.
4849 * May be used as the port_start() entry in ata_port_operations.
4852 * Inherited from caller.
4855 int ata_port_start (struct ata_port *ap)
4857 struct device *dev = ap->host_set->dev;
4860 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4864 rc = ata_pad_alloc(ap, dev);
4866 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4870 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4877 * ata_port_stop - Undo ata_port_start()
4878 * @ap: Port to shut down
4880 * Frees the PRD table.
4882 * May be used as the port_stop() entry in ata_port_operations.
4885 * Inherited from caller.
4888 void ata_port_stop (struct ata_port *ap)
4890 struct device *dev = ap->host_set->dev;
4892 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4893 ata_pad_free(ap, dev);
4896 void ata_host_stop (struct ata_host_set *host_set)
4898 if (host_set->mmio_base)
4899 iounmap(host_set->mmio_base);
4904 * ata_host_remove - Unregister SCSI host structure with upper layers
4905 * @ap: Port to unregister
4906 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4909 * Inherited from caller.
4912 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4914 struct Scsi_Host *sh = ap->host;
4919 scsi_remove_host(sh);
4921 ap->ops->port_stop(ap);
4925 * ata_host_init - Initialize an ata_port structure
4926 * @ap: Structure to initialize
4927 * @host: associated SCSI mid-layer structure
4928 * @host_set: Collection of hosts to which @ap belongs
4929 * @ent: Probe information provided by low-level driver
4930 * @port_no: Port number associated with this ata_port
4932 * Initialize a new ata_port structure, and its associated
4936 * Inherited from caller.
4939 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4940 struct ata_host_set *host_set,
4941 const struct ata_probe_ent *ent, unsigned int port_no)
4947 host->max_channel = 1;
4948 host->unique_id = ata_unique_id++;
4949 host->max_cmd_len = 12;
4951 ap->flags = ATA_FLAG_PORT_DISABLED;
4952 ap->id = host->unique_id;
4954 ap->ctl = ATA_DEVCTL_OBS;
4955 ap->host_set = host_set;
4956 ap->port_no = port_no;
4958 ent->legacy_mode ? ent->hard_port_no : port_no;
4959 ap->pio_mask = ent->pio_mask;
4960 ap->mwdma_mask = ent->mwdma_mask;
4961 ap->udma_mask = ent->udma_mask;
4962 ap->flags |= ent->host_flags;
4963 ap->ops = ent->port_ops;
4964 ap->cbl = ATA_CBL_NONE;
4965 ap->active_tag = ATA_TAG_POISON;
4966 ap->last_ctl = 0xFF;
4968 INIT_WORK(&ap->port_task, NULL, NULL);
4969 INIT_LIST_HEAD(&ap->eh_done_q);
4971 for (i = 0; i < ATA_MAX_DEVICES; i++)
4972 ap->device[i].devno = i;
4975 ap->stats.unhandled_irq = 1;
4976 ap->stats.idle_irq = 1;
4979 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4983 * ata_host_add - Attach low-level ATA driver to system
4984 * @ent: Information provided by low-level driver
4985 * @host_set: Collections of ports to which we add
4986 * @port_no: Port number associated with this host
4988 * Attach low-level ATA driver to system.
4991 * PCI/etc. bus probe sem.
4994 * New ata_port on success, for NULL on error.
4997 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4998 struct ata_host_set *host_set,
4999 unsigned int port_no)
5001 struct Scsi_Host *host;
5002 struct ata_port *ap;
5006 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
5010 ap = (struct ata_port *) &host->hostdata[0];
5012 ata_host_init(ap, host, host_set, ent, port_no);
5014 rc = ap->ops->port_start(ap);
5021 scsi_host_put(host);
5026 * ata_device_add - Register hardware device with ATA and SCSI layers
5027 * @ent: Probe information describing hardware device to be registered
5029 * This function processes the information provided in the probe
5030 * information struct @ent, allocates the necessary ATA and SCSI
5031 * host information structures, initializes them, and registers
5032 * everything with requisite kernel subsystems.
5034 * This function requests irqs, probes the ATA bus, and probes
5038 * PCI/etc. bus probe sem.
5041 * Number of ports registered. Zero on error (no ports registered).
5044 int ata_device_add(const struct ata_probe_ent *ent)
5046 unsigned int count = 0, i;
5047 struct device *dev = ent->dev;
5048 struct ata_host_set *host_set;
5051 /* alloc a container for our list of ATA ports (buses) */
5052 host_set = kzalloc(sizeof(struct ata_host_set) +
5053 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
5056 spin_lock_init(&host_set->lock);
5058 host_set->dev = dev;
5059 host_set->n_ports = ent->n_ports;
5060 host_set->irq = ent->irq;
5061 host_set->mmio_base = ent->mmio_base;
5062 host_set->private_data = ent->private_data;
5063 host_set->ops = ent->port_ops;
5065 /* register each port bound to this device */
5066 for (i = 0; i < ent->n_ports; i++) {
5067 struct ata_port *ap;
5068 unsigned long xfer_mode_mask;
5070 ap = ata_host_add(ent, host_set, i);
5074 host_set->ports[i] = ap;
5075 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
5076 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
5077 (ap->pio_mask << ATA_SHIFT_PIO);
5079 /* print per-port info to dmesg */
5080 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
5081 "bmdma 0x%lX irq %lu\n",
5083 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
5084 ata_mode_string(xfer_mode_mask),
5085 ap->ioaddr.cmd_addr,
5086 ap->ioaddr.ctl_addr,
5087 ap->ioaddr.bmdma_addr,
5091 host_set->ops->irq_clear(ap);
5098 /* obtain irq, that is shared between channels */
5099 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
5100 DRV_NAME, host_set))
5103 /* perform each probe synchronously */
5104 DPRINTK("probe begin\n");
5105 for (i = 0; i < count; i++) {
5106 struct ata_port *ap;
5109 ap = host_set->ports[i];
5111 DPRINTK("ata%u: bus probe begin\n", ap->id);
5112 rc = ata_bus_probe(ap);
5113 DPRINTK("ata%u: bus probe end\n", ap->id);
5116 /* FIXME: do something useful here?
5117 * Current libata behavior will
5118 * tear down everything when
5119 * the module is removed
5120 * or the h/w is unplugged.
5124 rc = scsi_add_host(ap->host, dev);
5126 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
5128 /* FIXME: do something useful here */
5129 /* FIXME: handle unconditional calls to
5130 * scsi_scan_host and ata_host_remove, below,
5136 /* probes are done, now scan each port's disk(s) */
5137 DPRINTK("host probe begin\n");
5138 for (i = 0; i < count; i++) {
5139 struct ata_port *ap = host_set->ports[i];
5141 ata_scsi_scan_host(ap);
5144 dev_set_drvdata(dev, host_set);
5146 VPRINTK("EXIT, returning %u\n", ent->n_ports);
5147 return ent->n_ports; /* success */
5150 for (i = 0; i < count; i++) {
5151 ata_host_remove(host_set->ports[i], 1);
5152 scsi_host_put(host_set->ports[i]->host);
5156 VPRINTK("EXIT, returning 0\n");
5161 * ata_host_set_remove - PCI layer callback for device removal
5162 * @host_set: ATA host set that was removed
5164 * Unregister all objects associated with this host set. Free those
5168 * Inherited from calling layer (may sleep).
5171 void ata_host_set_remove(struct ata_host_set *host_set)
5173 struct ata_port *ap;
5176 for (i = 0; i < host_set->n_ports; i++) {
5177 ap = host_set->ports[i];
5178 scsi_remove_host(ap->host);
5181 free_irq(host_set->irq, host_set);
5183 for (i = 0; i < host_set->n_ports; i++) {
5184 ap = host_set->ports[i];
5186 ata_scsi_release(ap->host);
5188 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5189 struct ata_ioports *ioaddr = &ap->ioaddr;
5191 if (ioaddr->cmd_addr == 0x1f0)
5192 release_region(0x1f0, 8);
5193 else if (ioaddr->cmd_addr == 0x170)
5194 release_region(0x170, 8);
5197 scsi_host_put(ap->host);
5200 if (host_set->ops->host_stop)
5201 host_set->ops->host_stop(host_set);
5207 * ata_scsi_release - SCSI layer callback hook for host unload
5208 * @host: libata host to be unloaded
5210 * Performs all duties necessary to shut down a libata port...
5211 * Kill port kthread, disable port, and release resources.
5214 * Inherited from SCSI layer.
5220 int ata_scsi_release(struct Scsi_Host *host)
5222 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
5227 ap->ops->port_disable(ap);
5228 ata_host_remove(ap, 0);
5229 for (i = 0; i < ATA_MAX_DEVICES; i++)
5230 kfree(ap->device[i].id);
5237 * ata_std_ports - initialize ioaddr with standard port offsets.
5238 * @ioaddr: IO address structure to be initialized
5240 * Utility function which initializes data_addr, error_addr,
5241 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5242 * device_addr, status_addr, and command_addr to standard offsets
5243 * relative to cmd_addr.
5245 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
5248 void ata_std_ports(struct ata_ioports *ioaddr)
5250 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5251 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5252 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5253 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5254 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5255 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5256 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5257 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5258 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5259 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5265 void ata_pci_host_stop (struct ata_host_set *host_set)
5267 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5269 pci_iounmap(pdev, host_set->mmio_base);
5273 * ata_pci_remove_one - PCI layer callback for device removal
5274 * @pdev: PCI device that was removed
5276 * PCI layer indicates to libata via this hook that
5277 * hot-unplug or module unload event has occurred.
5278 * Handle this by unregistering all objects associated
5279 * with this PCI device. Free those objects. Then finally
5280 * release PCI resources and disable device.
5283 * Inherited from PCI layer (may sleep).
5286 void ata_pci_remove_one (struct pci_dev *pdev)
5288 struct device *dev = pci_dev_to_dev(pdev);
5289 struct ata_host_set *host_set = dev_get_drvdata(dev);
5291 ata_host_set_remove(host_set);
5292 pci_release_regions(pdev);
5293 pci_disable_device(pdev);
5294 dev_set_drvdata(dev, NULL);
5297 /* move to PCI subsystem */
5298 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5300 unsigned long tmp = 0;
5302 switch (bits->width) {
5305 pci_read_config_byte(pdev, bits->reg, &tmp8);
5311 pci_read_config_word(pdev, bits->reg, &tmp16);
5317 pci_read_config_dword(pdev, bits->reg, &tmp32);
5328 return (tmp == bits->val) ? 1 : 0;
5331 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5333 pci_save_state(pdev);
5334 pci_disable_device(pdev);
5335 pci_set_power_state(pdev, PCI_D3hot);
5339 int ata_pci_device_resume(struct pci_dev *pdev)
5341 pci_set_power_state(pdev, PCI_D0);
5342 pci_restore_state(pdev);
5343 pci_enable_device(pdev);
5344 pci_set_master(pdev);
5347 #endif /* CONFIG_PCI */
5350 static int __init ata_init(void)
5352 ata_wq = create_workqueue("ata");
5356 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5360 static void __exit ata_exit(void)
5362 destroy_workqueue(ata_wq);
5365 module_init(ata_init);
5366 module_exit(ata_exit);
5368 static unsigned long ratelimit_time;
5369 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5371 int ata_ratelimit(void)
5374 unsigned long flags;
5376 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5378 if (time_after(jiffies, ratelimit_time)) {
5380 ratelimit_time = jiffies + (HZ/5);
5384 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5390 * libata is essentially a library of internal helper functions for
5391 * low-level ATA host controller drivers. As such, the API/ABI is
5392 * likely to change as new drivers are added and updated.
5393 * Do not depend on ABI/API stability.
5396 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5397 EXPORT_SYMBOL_GPL(ata_std_ports);
5398 EXPORT_SYMBOL_GPL(ata_device_add);
5399 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5400 EXPORT_SYMBOL_GPL(ata_sg_init);
5401 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5402 EXPORT_SYMBOL_GPL(__ata_qc_complete);
5403 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5404 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5405 EXPORT_SYMBOL_GPL(ata_tf_load);
5406 EXPORT_SYMBOL_GPL(ata_tf_read);
5407 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5408 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5409 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5410 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5411 EXPORT_SYMBOL_GPL(ata_check_status);
5412 EXPORT_SYMBOL_GPL(ata_altstatus);
5413 EXPORT_SYMBOL_GPL(ata_exec_command);
5414 EXPORT_SYMBOL_GPL(ata_port_start);
5415 EXPORT_SYMBOL_GPL(ata_port_stop);
5416 EXPORT_SYMBOL_GPL(ata_host_stop);
5417 EXPORT_SYMBOL_GPL(ata_interrupt);
5418 EXPORT_SYMBOL_GPL(ata_qc_prep);
5419 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5420 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5421 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5422 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5423 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5424 EXPORT_SYMBOL_GPL(ata_port_probe);
5425 EXPORT_SYMBOL_GPL(sata_phy_reset);
5426 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5427 EXPORT_SYMBOL_GPL(ata_bus_reset);
5428 EXPORT_SYMBOL_GPL(ata_std_probeinit);
5429 EXPORT_SYMBOL_GPL(ata_std_softreset);
5430 EXPORT_SYMBOL_GPL(sata_std_hardreset);
5431 EXPORT_SYMBOL_GPL(ata_std_postreset);
5432 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
5433 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
5434 EXPORT_SYMBOL_GPL(ata_dev_revalidate);
5435 EXPORT_SYMBOL_GPL(ata_port_disable);
5436 EXPORT_SYMBOL_GPL(ata_ratelimit);
5437 EXPORT_SYMBOL_GPL(ata_busy_sleep);
5438 EXPORT_SYMBOL_GPL(ata_port_queue_task);
5439 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5440 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5441 EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
5442 EXPORT_SYMBOL_GPL(ata_scsi_error);
5443 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5444 EXPORT_SYMBOL_GPL(ata_scsi_release);
5445 EXPORT_SYMBOL_GPL(ata_host_intr);
5446 EXPORT_SYMBOL_GPL(ata_dev_classify);
5447 EXPORT_SYMBOL_GPL(ata_id_string);
5448 EXPORT_SYMBOL_GPL(ata_id_c_string);
5449 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5450 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5451 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
5453 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
5454 EXPORT_SYMBOL_GPL(ata_timing_compute);
5455 EXPORT_SYMBOL_GPL(ata_timing_merge);
5458 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5459 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5460 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5461 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5462 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5463 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5464 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
5465 #endif /* CONFIG_PCI */
5467 EXPORT_SYMBOL_GPL(ata_device_suspend);
5468 EXPORT_SYMBOL_GPL(ata_device_resume);
5469 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5470 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);