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 void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
65 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
66 static void ata_set_mode(struct ata_port *ap);
67 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
68 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
69 static int fgb(u32 bitmap);
70 static int ata_choose_xfer_mode(const struct ata_port *ap,
72 unsigned int *xfer_shift_out);
74 static unsigned int ata_unique_id = 1;
75 static struct workqueue_struct *ata_wq;
77 int atapi_enabled = 0;
78 module_param(atapi_enabled, int, 0444);
79 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
81 MODULE_AUTHOR("Jeff Garzik");
82 MODULE_DESCRIPTION("Library module for ATA devices");
83 MODULE_LICENSE("GPL");
84 MODULE_VERSION(DRV_VERSION);
88 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
89 * @tf: Taskfile to convert
90 * @fis: Buffer into which data will output
91 * @pmp: Port multiplier port
93 * Converts a standard ATA taskfile to a Serial ATA
94 * FIS structure (Register - Host to Device).
97 * Inherited from caller.
100 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
102 fis[0] = 0x27; /* Register - Host to Device FIS */
103 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
104 bit 7 indicates Command FIS */
105 fis[2] = tf->command;
106 fis[3] = tf->feature;
113 fis[8] = tf->hob_lbal;
114 fis[9] = tf->hob_lbam;
115 fis[10] = tf->hob_lbah;
116 fis[11] = tf->hob_feature;
119 fis[13] = tf->hob_nsect;
130 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
131 * @fis: Buffer from which data will be input
132 * @tf: Taskfile to output
134 * Converts a serial ATA FIS structure to a standard ATA taskfile.
137 * Inherited from caller.
140 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
142 tf->command = fis[2]; /* status */
143 tf->feature = fis[3]; /* error */
150 tf->hob_lbal = fis[8];
151 tf->hob_lbam = fis[9];
152 tf->hob_lbah = fis[10];
155 tf->hob_nsect = fis[13];
158 static const u8 ata_rw_cmds[] = {
162 ATA_CMD_READ_MULTI_EXT,
163 ATA_CMD_WRITE_MULTI_EXT,
167 ATA_CMD_WRITE_MULTI_FUA_EXT,
171 ATA_CMD_PIO_READ_EXT,
172 ATA_CMD_PIO_WRITE_EXT,
185 ATA_CMD_WRITE_FUA_EXT
189 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
190 * @qc: command to examine and configure
192 * Examine the device configuration and tf->flags to calculate
193 * the proper read/write commands and protocol to use.
198 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
200 struct ata_taskfile *tf = &qc->tf;
201 struct ata_device *dev = qc->dev;
204 int index, fua, lba48, write;
206 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
207 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
208 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
210 if (dev->flags & ATA_DFLAG_PIO) {
211 tf->protocol = ATA_PROT_PIO;
212 index = dev->multi_count ? 0 : 8;
213 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
214 /* Unable to use DMA due to host limitation */
215 tf->protocol = ATA_PROT_PIO;
216 index = dev->multi_count ? 0 : 4;
218 tf->protocol = ATA_PROT_DMA;
222 cmd = ata_rw_cmds[index + fua + lba48 + write];
230 static const char * const xfer_mode_str[] = {
250 * ata_udma_string - convert UDMA bit offset to string
251 * @mask: mask of bits supported; only highest bit counts.
253 * Determine string which represents the highest speed
254 * (highest bit in @udma_mask).
260 * Constant C string representing highest speed listed in
261 * @udma_mask, or the constant C string "<n/a>".
264 static const char *ata_mode_string(unsigned int mask)
268 for (i = 7; i >= 0; i--)
271 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
274 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
281 return xfer_mode_str[i];
285 * ata_pio_devchk - PATA device presence detection
286 * @ap: ATA channel to examine
287 * @device: Device to examine (starting at zero)
289 * This technique was originally described in
290 * Hale Landis's ATADRVR (www.ata-atapi.com), and
291 * later found its way into the ATA/ATAPI spec.
293 * Write a pattern to the ATA shadow registers,
294 * and if a device is present, it will respond by
295 * correctly storing and echoing back the
296 * ATA shadow register contents.
302 static unsigned int ata_pio_devchk(struct ata_port *ap,
305 struct ata_ioports *ioaddr = &ap->ioaddr;
308 ap->ops->dev_select(ap, device);
310 outb(0x55, ioaddr->nsect_addr);
311 outb(0xaa, ioaddr->lbal_addr);
313 outb(0xaa, ioaddr->nsect_addr);
314 outb(0x55, ioaddr->lbal_addr);
316 outb(0x55, ioaddr->nsect_addr);
317 outb(0xaa, ioaddr->lbal_addr);
319 nsect = inb(ioaddr->nsect_addr);
320 lbal = inb(ioaddr->lbal_addr);
322 if ((nsect == 0x55) && (lbal == 0xaa))
323 return 1; /* we found a device */
325 return 0; /* nothing found */
329 * ata_mmio_devchk - PATA device presence detection
330 * @ap: ATA channel to examine
331 * @device: Device to examine (starting at zero)
333 * This technique was originally described in
334 * Hale Landis's ATADRVR (www.ata-atapi.com), and
335 * later found its way into the ATA/ATAPI spec.
337 * Write a pattern to the ATA shadow registers,
338 * and if a device is present, it will respond by
339 * correctly storing and echoing back the
340 * ATA shadow register contents.
346 static unsigned int ata_mmio_devchk(struct ata_port *ap,
349 struct ata_ioports *ioaddr = &ap->ioaddr;
352 ap->ops->dev_select(ap, device);
354 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
355 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
357 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
358 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
360 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
361 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
363 nsect = readb((void __iomem *) ioaddr->nsect_addr);
364 lbal = readb((void __iomem *) ioaddr->lbal_addr);
366 if ((nsect == 0x55) && (lbal == 0xaa))
367 return 1; /* we found a device */
369 return 0; /* nothing found */
373 * ata_devchk - PATA device presence detection
374 * @ap: ATA channel to examine
375 * @device: Device to examine (starting at zero)
377 * Dispatch ATA device presence detection, depending
378 * on whether we are using PIO or MMIO to talk to the
379 * ATA shadow registers.
385 static unsigned int ata_devchk(struct ata_port *ap,
388 if (ap->flags & ATA_FLAG_MMIO)
389 return ata_mmio_devchk(ap, device);
390 return ata_pio_devchk(ap, device);
394 * ata_dev_classify - determine device type based on ATA-spec signature
395 * @tf: ATA taskfile register set for device to be identified
397 * Determine from taskfile register contents whether a device is
398 * ATA or ATAPI, as per "Signature and persistence" section
399 * of ATA/PI spec (volume 1, sect 5.14).
405 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
406 * the event of failure.
409 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
411 /* Apple's open source Darwin code hints that some devices only
412 * put a proper signature into the LBA mid/high registers,
413 * So, we only check those. It's sufficient for uniqueness.
416 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
417 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
418 DPRINTK("found ATA device by sig\n");
422 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
423 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
424 DPRINTK("found ATAPI device by sig\n");
425 return ATA_DEV_ATAPI;
428 DPRINTK("unknown device\n");
429 return ATA_DEV_UNKNOWN;
433 * ata_dev_try_classify - Parse returned ATA device signature
434 * @ap: ATA channel to examine
435 * @device: Device to examine (starting at zero)
436 * @r_err: Value of error register on completion
438 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
439 * an ATA/ATAPI-defined set of values is placed in the ATA
440 * shadow registers, indicating the results of device detection
443 * Select the ATA device, and read the values from the ATA shadow
444 * registers. Then parse according to the Error register value,
445 * and the spec-defined values examined by ata_dev_classify().
451 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
455 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
457 struct ata_taskfile tf;
461 ap->ops->dev_select(ap, device);
463 memset(&tf, 0, sizeof(tf));
465 ap->ops->tf_read(ap, &tf);
470 /* see if device passed diags */
473 else if ((device == 0) && (err == 0x81))
478 /* determine if device is ATA or ATAPI */
479 class = ata_dev_classify(&tf);
481 if (class == ATA_DEV_UNKNOWN)
483 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
489 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
490 * @id: IDENTIFY DEVICE results we will examine
491 * @s: string into which data is output
492 * @ofs: offset into identify device page
493 * @len: length of string to return. must be an even number.
495 * The strings in the IDENTIFY DEVICE page are broken up into
496 * 16-bit chunks. Run through the string, and output each
497 * 8-bit chunk linearly, regardless of platform.
503 void ata_dev_id_string(const u16 *id, unsigned char *s,
504 unsigned int ofs, unsigned int len)
524 * ata_noop_dev_select - Select device 0/1 on ATA bus
525 * @ap: ATA channel to manipulate
526 * @device: ATA device (numbered from zero) to select
528 * This function performs no actual function.
530 * May be used as the dev_select() entry in ata_port_operations.
535 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
541 * ata_std_dev_select - Select device 0/1 on ATA bus
542 * @ap: ATA channel to manipulate
543 * @device: ATA device (numbered from zero) to select
545 * Use the method defined in the ATA specification to
546 * make either device 0, or device 1, active on the
547 * ATA channel. Works with both PIO and MMIO.
549 * May be used as the dev_select() entry in ata_port_operations.
555 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
560 tmp = ATA_DEVICE_OBS;
562 tmp = ATA_DEVICE_OBS | ATA_DEV1;
564 if (ap->flags & ATA_FLAG_MMIO) {
565 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
567 outb(tmp, ap->ioaddr.device_addr);
569 ata_pause(ap); /* needed; also flushes, for mmio */
573 * ata_dev_select - Select device 0/1 on ATA bus
574 * @ap: ATA channel to manipulate
575 * @device: ATA device (numbered from zero) to select
576 * @wait: non-zero to wait for Status register BSY bit to clear
577 * @can_sleep: non-zero if context allows sleeping
579 * Use the method defined in the ATA specification to
580 * make either device 0, or device 1, active on the
583 * This is a high-level version of ata_std_dev_select(),
584 * which additionally provides the services of inserting
585 * the proper pauses and status polling, where needed.
591 void ata_dev_select(struct ata_port *ap, unsigned int device,
592 unsigned int wait, unsigned int can_sleep)
594 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
595 ap->id, device, wait);
600 ap->ops->dev_select(ap, device);
603 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
610 * ata_dump_id - IDENTIFY DEVICE info debugging output
611 * @dev: Device whose IDENTIFY DEVICE page we will dump
613 * Dump selected 16-bit words from a detected device's
614 * IDENTIFY PAGE page.
620 static inline void ata_dump_id(const struct ata_device *dev)
622 DPRINTK("49==0x%04x "
632 DPRINTK("80==0x%04x "
642 DPRINTK("88==0x%04x "
649 * Compute the PIO modes available for this device. This is not as
650 * trivial as it seems if we must consider early devices correctly.
652 * FIXME: pre IDE drive timing (do we care ?).
655 static unsigned int ata_pio_modes(const struct ata_device *adev)
659 /* Usual case. Word 53 indicates word 64 is valid */
660 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
661 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
667 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
668 number for the maximum. Turn it into a mask and return it */
669 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
671 /* But wait.. there's more. Design your standards by committee and
672 you too can get a free iordy field to process. However its the
673 speeds not the modes that are supported... Note drivers using the
674 timing API will get this right anyway */
678 ata_queue_packet_task(struct ata_port *ap)
680 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
681 queue_work(ata_wq, &ap->packet_task);
685 ata_queue_pio_task(struct ata_port *ap)
687 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
688 queue_work(ata_wq, &ap->pio_task);
692 ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
694 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
695 queue_delayed_work(ata_wq, &ap->pio_task, delay);
699 * ata_flush_pio_tasks - Flush pio_task and packet_task
700 * @ap: the target ata_port
702 * After this function completes, pio_task and packet_task are
703 * guranteed not to be running or scheduled.
706 * Kernel thread context (may sleep)
709 static void ata_flush_pio_tasks(struct ata_port *ap)
716 spin_lock_irqsave(&ap->host_set->lock, flags);
717 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
718 spin_unlock_irqrestore(&ap->host_set->lock, flags);
720 DPRINTK("flush #1\n");
721 flush_workqueue(ata_wq);
724 * At this point, if a task is running, it's guaranteed to see
725 * the FLUSH flag; thus, it will never queue pio tasks again.
728 tmp |= cancel_delayed_work(&ap->pio_task);
729 tmp |= cancel_delayed_work(&ap->packet_task);
731 DPRINTK("flush #2\n");
732 flush_workqueue(ata_wq);
735 spin_lock_irqsave(&ap->host_set->lock, flags);
736 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
737 spin_unlock_irqrestore(&ap->host_set->lock, flags);
742 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
744 struct completion *waiting = qc->private_data;
746 qc->ap->ops->tf_read(qc->ap, &qc->tf);
751 * ata_exec_internal - execute libata internal command
752 * @ap: Port to which the command is sent
753 * @dev: Device to which the command is sent
754 * @tf: Taskfile registers for the command and the result
755 * @dma_dir: Data tranfer direction of the command
756 * @buf: Data buffer of the command
757 * @buflen: Length of data buffer
759 * Executes libata internal command with timeout. @tf contains
760 * command on entry and result on return. Timeout and error
761 * conditions are reported via return value. No recovery action
762 * is taken after a command times out. It's caller's duty to
763 * clean up after timeout.
766 * None. Should be called with kernel context, might sleep.
770 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
771 struct ata_taskfile *tf,
772 int dma_dir, void *buf, unsigned int buflen)
774 u8 command = tf->command;
775 struct ata_queued_cmd *qc;
776 DECLARE_COMPLETION(wait);
778 unsigned int err_mask;
780 spin_lock_irqsave(&ap->host_set->lock, flags);
782 qc = ata_qc_new_init(ap, dev);
786 qc->dma_dir = dma_dir;
787 if (dma_dir != DMA_NONE) {
788 ata_sg_init_one(qc, buf, buflen);
789 qc->nsect = buflen / ATA_SECT_SIZE;
792 qc->private_data = &wait;
793 qc->complete_fn = ata_qc_complete_internal;
795 qc->err_mask = ata_qc_issue(qc);
799 spin_unlock_irqrestore(&ap->host_set->lock, flags);
801 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
802 spin_lock_irqsave(&ap->host_set->lock, flags);
804 /* We're racing with irq here. If we lose, the
805 * following test prevents us from completing the qc
806 * again. If completion irq occurs after here but
807 * before the caller cleans up, it will result in a
808 * spurious interrupt. We can live with that.
810 if (qc->flags & ATA_QCFLAG_ACTIVE) {
811 qc->err_mask = AC_ERR_TIMEOUT;
813 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
817 spin_unlock_irqrestore(&ap->host_set->lock, flags);
821 err_mask = qc->err_mask;
829 * ata_pio_need_iordy - check if iordy needed
832 * Check if the current speed of the device requires IORDY. Used
833 * by various controllers for chip configuration.
836 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
839 int speed = adev->pio_mode - XFER_PIO_0;
846 /* If we have no drive specific rule, then PIO 2 is non IORDY */
848 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
849 pio = adev->id[ATA_ID_EIDE_PIO];
850 /* Is the speed faster than the drive allows non IORDY ? */
852 /* This is cycle times not frequency - watch the logic! */
853 if (pio > 240) /* PIO2 is 240nS per cycle */
862 * ata_dev_identify - obtain IDENTIFY x DEVICE page
863 * @ap: port on which device we wish to probe resides
864 * @device: device bus address, starting at zero
866 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
867 * command, and read back the 512-byte device information page.
868 * The device information page is fed to us via the standard
869 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
870 * using standard PIO-IN paths)
872 * After reading the device information page, we use several
873 * bits of information from it to initialize data structures
874 * that will be used during the lifetime of the ata_device.
875 * Other data from the info page is used to disqualify certain
876 * older ATA devices we do not wish to support.
879 * Inherited from caller. Some functions called by this function
880 * obtain the host_set lock.
883 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
885 struct ata_device *dev = &ap->device[device];
886 unsigned int major_version;
888 unsigned long xfer_modes;
889 unsigned int using_edd;
890 struct ata_taskfile tf;
891 unsigned int err_mask;
894 if (!ata_dev_present(dev)) {
895 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
900 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
905 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
907 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
908 dev->class == ATA_DEV_NONE);
910 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
913 ata_tf_init(ap, &tf, device);
915 if (dev->class == ATA_DEV_ATA) {
916 tf.command = ATA_CMD_ID_ATA;
917 DPRINTK("do ATA identify\n");
919 tf.command = ATA_CMD_ID_ATAPI;
920 DPRINTK("do ATAPI identify\n");
923 tf.protocol = ATA_PROT_PIO;
925 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
926 dev->id, sizeof(dev->id));
929 if (err_mask & ~AC_ERR_DEV)
933 * arg! EDD works for all test cases, but seems to return
934 * the ATA signature for some ATAPI devices. Until the
935 * reason for this is found and fixed, we fix up the mess
936 * here. If IDENTIFY DEVICE returns command aborted
937 * (as ATAPI devices do), then we issue an
938 * IDENTIFY PACKET DEVICE.
940 * ATA software reset (SRST, the default) does not appear
941 * to have this problem.
943 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
945 if (err & ATA_ABORTED) {
946 dev->class = ATA_DEV_ATAPI;
953 swap_buf_le16(dev->id, ATA_ID_WORDS);
955 /* print device capabilities */
956 printk(KERN_DEBUG "ata%u: dev %u cfg "
957 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
958 ap->id, device, dev->id[49],
959 dev->id[82], dev->id[83], dev->id[84],
960 dev->id[85], dev->id[86], dev->id[87],
964 * common ATA, ATAPI feature tests
967 /* we require DMA support (bits 8 of word 49) */
968 if (!ata_id_has_dma(dev->id)) {
969 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
973 /* quick-n-dirty find max transfer mode; for printk only */
974 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
976 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
978 xfer_modes = ata_pio_modes(dev);
982 /* ATA-specific feature tests */
983 if (dev->class == ATA_DEV_ATA) {
984 if (!ata_id_is_ata(dev->id)) /* sanity check */
987 /* get major version */
988 tmp = dev->id[ATA_ID_MAJOR_VER];
989 for (major_version = 14; major_version >= 1; major_version--)
990 if (tmp & (1 << major_version))
994 * The exact sequence expected by certain pre-ATA4 drives is:
997 * INITIALIZE DEVICE PARAMETERS
999 * Some drives were very specific about that exact sequence.
1001 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1002 ata_dev_init_params(ap, dev);
1004 /* current CHS translation info (id[53-58]) might be
1005 * changed. reread the identify device info.
1007 ata_dev_reread_id(ap, dev);
1010 if (ata_id_has_lba(dev->id)) {
1011 dev->flags |= ATA_DFLAG_LBA;
1013 if (ata_id_has_lba48(dev->id)) {
1014 dev->flags |= ATA_DFLAG_LBA48;
1015 dev->n_sectors = ata_id_u64(dev->id, 100);
1017 dev->n_sectors = ata_id_u32(dev->id, 60);
1020 /* print device info to dmesg */
1021 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1024 ata_mode_string(xfer_modes),
1025 (unsigned long long)dev->n_sectors,
1026 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1030 /* Default translation */
1031 dev->cylinders = dev->id[1];
1032 dev->heads = dev->id[3];
1033 dev->sectors = dev->id[6];
1034 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1036 if (ata_id_current_chs_valid(dev->id)) {
1037 /* Current CHS translation is valid. */
1038 dev->cylinders = dev->id[54];
1039 dev->heads = dev->id[55];
1040 dev->sectors = dev->id[56];
1042 dev->n_sectors = ata_id_u32(dev->id, 57);
1045 /* print device info to dmesg */
1046 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1049 ata_mode_string(xfer_modes),
1050 (unsigned long long)dev->n_sectors,
1051 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1055 ap->host->max_cmd_len = 16;
1058 /* ATAPI-specific feature tests */
1059 else if (dev->class == ATA_DEV_ATAPI) {
1060 if (ata_id_is_ata(dev->id)) /* sanity check */
1063 rc = atapi_cdb_len(dev->id);
1064 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1065 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1068 ap->cdb_len = (unsigned int) rc;
1069 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1071 /* print device info to dmesg */
1072 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1074 ata_mode_string(xfer_modes));
1077 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1081 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1084 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1085 DPRINTK("EXIT, err\n");
1089 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1091 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1095 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1102 void ata_dev_config(struct ata_port *ap, unsigned int i)
1104 /* limit bridge transfers to udma5, 200 sectors */
1105 if (ata_dev_knobble(ap)) {
1106 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1107 ap->id, ap->device->devno);
1108 ap->udma_mask &= ATA_UDMA5;
1109 ap->host->max_sectors = ATA_MAX_SECTORS;
1110 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1111 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
1114 if (ap->ops->dev_config)
1115 ap->ops->dev_config(ap, &ap->device[i]);
1119 * ata_bus_probe - Reset and probe ATA bus
1122 * Master ATA bus probing function. Initiates a hardware-dependent
1123 * bus reset, then attempts to identify any devices found on
1127 * PCI/etc. bus probe sem.
1130 * Zero on success, non-zero on error.
1133 static int ata_bus_probe(struct ata_port *ap)
1135 unsigned int i, found = 0;
1137 if (ap->ops->probe_reset) {
1138 unsigned int classes[ATA_MAX_DEVICES];
1143 rc = ap->ops->probe_reset(ap, classes);
1145 for (i = 0; i < ATA_MAX_DEVICES; i++)
1146 ap->device[i].class = classes[i];
1148 printk(KERN_ERR "ata%u: probe reset failed, "
1149 "disabling port\n", ap->id);
1150 ata_port_disable(ap);
1153 ap->ops->phy_reset(ap);
1155 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1158 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1159 ata_dev_identify(ap, i);
1160 if (ata_dev_present(&ap->device[i])) {
1162 ata_dev_config(ap,i);
1166 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1167 goto err_out_disable;
1170 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1171 goto err_out_disable;
1176 ap->ops->port_disable(ap);
1182 * ata_port_probe - Mark port as enabled
1183 * @ap: Port for which we indicate enablement
1185 * Modify @ap data structure such that the system
1186 * thinks that the entire port is enabled.
1188 * LOCKING: host_set lock, or some other form of
1192 void ata_port_probe(struct ata_port *ap)
1194 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1198 * sata_print_link_status - Print SATA link status
1199 * @ap: SATA port to printk link status about
1201 * This function prints link speed and status of a SATA link.
1206 static void sata_print_link_status(struct ata_port *ap)
1211 if (!ap->ops->scr_read)
1214 sstatus = scr_read(ap, SCR_STATUS);
1216 if (sata_dev_present(ap)) {
1217 tmp = (sstatus >> 4) & 0xf;
1220 else if (tmp & (1 << 1))
1223 speed = "<unknown>";
1224 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1225 ap->id, speed, sstatus);
1227 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1233 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1234 * @ap: SATA port associated with target SATA PHY.
1236 * This function issues commands to standard SATA Sxxx
1237 * PHY registers, to wake up the phy (and device), and
1238 * clear any reset condition.
1241 * PCI/etc. bus probe sem.
1244 void __sata_phy_reset(struct ata_port *ap)
1247 unsigned long timeout = jiffies + (HZ * 5);
1249 if (ap->flags & ATA_FLAG_SATA_RESET) {
1250 /* issue phy wake/reset */
1251 scr_write_flush(ap, SCR_CONTROL, 0x301);
1252 /* Couldn't find anything in SATA I/II specs, but
1253 * AHCI-1.1 10.4.2 says at least 1 ms. */
1256 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1258 /* wait for phy to become ready, if necessary */
1261 sstatus = scr_read(ap, SCR_STATUS);
1262 if ((sstatus & 0xf) != 1)
1264 } while (time_before(jiffies, timeout));
1266 /* print link status */
1267 sata_print_link_status(ap);
1269 /* TODO: phy layer with polling, timeouts, etc. */
1270 if (sata_dev_present(ap))
1273 ata_port_disable(ap);
1275 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1278 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1279 ata_port_disable(ap);
1283 ap->cbl = ATA_CBL_SATA;
1287 * sata_phy_reset - Reset SATA bus.
1288 * @ap: SATA port associated with target SATA PHY.
1290 * This function resets the SATA bus, and then probes
1291 * the bus for devices.
1294 * PCI/etc. bus probe sem.
1297 void sata_phy_reset(struct ata_port *ap)
1299 __sata_phy_reset(ap);
1300 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1306 * ata_port_disable - Disable port.
1307 * @ap: Port to be disabled.
1309 * Modify @ap data structure such that the system
1310 * thinks that the entire port is disabled, and should
1311 * never attempt to probe or communicate with devices
1314 * LOCKING: host_set lock, or some other form of
1318 void ata_port_disable(struct ata_port *ap)
1320 ap->device[0].class = ATA_DEV_NONE;
1321 ap->device[1].class = ATA_DEV_NONE;
1322 ap->flags |= ATA_FLAG_PORT_DISABLED;
1326 * This mode timing computation functionality is ported over from
1327 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1330 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1331 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1332 * for PIO 5, which is a nonstandard extension and UDMA6, which
1333 * is currently supported only by Maxtor drives.
1336 static const struct ata_timing ata_timing[] = {
1338 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1339 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1340 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1341 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1343 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1344 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1345 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1347 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1349 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1350 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1351 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1353 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1354 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1355 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1357 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1358 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1359 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1361 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1362 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1363 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1365 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1370 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1371 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1373 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1375 q->setup = EZ(t->setup * 1000, T);
1376 q->act8b = EZ(t->act8b * 1000, T);
1377 q->rec8b = EZ(t->rec8b * 1000, T);
1378 q->cyc8b = EZ(t->cyc8b * 1000, T);
1379 q->active = EZ(t->active * 1000, T);
1380 q->recover = EZ(t->recover * 1000, T);
1381 q->cycle = EZ(t->cycle * 1000, T);
1382 q->udma = EZ(t->udma * 1000, UT);
1385 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1386 struct ata_timing *m, unsigned int what)
1388 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1389 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1390 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1391 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1392 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1393 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1394 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1395 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1398 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1400 const struct ata_timing *t;
1402 for (t = ata_timing; t->mode != speed; t++)
1403 if (t->mode == 0xFF)
1408 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1409 struct ata_timing *t, int T, int UT)
1411 const struct ata_timing *s;
1412 struct ata_timing p;
1418 if (!(s = ata_timing_find_mode(speed)))
1421 memcpy(t, s, sizeof(*s));
1424 * If the drive is an EIDE drive, it can tell us it needs extended
1425 * PIO/MW_DMA cycle timing.
1428 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1429 memset(&p, 0, sizeof(p));
1430 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1431 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1432 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1433 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1434 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1436 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1440 * Convert the timing to bus clock counts.
1443 ata_timing_quantize(t, t, T, UT);
1446 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1447 * S.M.A.R.T * and some other commands. We have to ensure that the
1448 * DMA cycle timing is slower/equal than the fastest PIO timing.
1451 if (speed > XFER_PIO_4) {
1452 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1453 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1457 * Lengthen active & recovery time so that cycle time is correct.
1460 if (t->act8b + t->rec8b < t->cyc8b) {
1461 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1462 t->rec8b = t->cyc8b - t->act8b;
1465 if (t->active + t->recover < t->cycle) {
1466 t->active += (t->cycle - (t->active + t->recover)) / 2;
1467 t->recover = t->cycle - t->active;
1473 static const struct {
1476 } xfer_mode_classes[] = {
1477 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1478 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1479 { ATA_SHIFT_PIO, XFER_PIO_0 },
1482 static u8 base_from_shift(unsigned int shift)
1486 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1487 if (xfer_mode_classes[i].shift == shift)
1488 return xfer_mode_classes[i].base;
1493 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1498 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1501 if (dev->xfer_shift == ATA_SHIFT_PIO)
1502 dev->flags |= ATA_DFLAG_PIO;
1504 ata_dev_set_xfermode(ap, dev);
1506 base = base_from_shift(dev->xfer_shift);
1507 ofs = dev->xfer_mode - base;
1508 idx = ofs + dev->xfer_shift;
1509 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1511 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1512 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1514 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1515 ap->id, dev->devno, xfer_mode_str[idx]);
1518 static int ata_host_set_pio(struct ata_port *ap)
1524 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1527 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1531 base = base_from_shift(ATA_SHIFT_PIO);
1532 xfer_mode = base + x;
1534 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1535 (int)base, (int)xfer_mode, mask, x);
1537 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1538 struct ata_device *dev = &ap->device[i];
1539 if (ata_dev_present(dev)) {
1540 dev->pio_mode = xfer_mode;
1541 dev->xfer_mode = xfer_mode;
1542 dev->xfer_shift = ATA_SHIFT_PIO;
1543 if (ap->ops->set_piomode)
1544 ap->ops->set_piomode(ap, dev);
1551 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1552 unsigned int xfer_shift)
1556 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1557 struct ata_device *dev = &ap->device[i];
1558 if (ata_dev_present(dev)) {
1559 dev->dma_mode = xfer_mode;
1560 dev->xfer_mode = xfer_mode;
1561 dev->xfer_shift = xfer_shift;
1562 if (ap->ops->set_dmamode)
1563 ap->ops->set_dmamode(ap, dev);
1569 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1570 * @ap: port on which timings will be programmed
1572 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1575 * PCI/etc. bus probe sem.
1577 static void ata_set_mode(struct ata_port *ap)
1579 unsigned int xfer_shift;
1583 /* step 1: always set host PIO timings */
1584 rc = ata_host_set_pio(ap);
1588 /* step 2: choose the best data xfer mode */
1589 xfer_mode = xfer_shift = 0;
1590 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1594 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1595 if (xfer_shift != ATA_SHIFT_PIO)
1596 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1598 /* step 4: update devices' xfer mode */
1599 ata_dev_set_mode(ap, &ap->device[0]);
1600 ata_dev_set_mode(ap, &ap->device[1]);
1602 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1605 if (ap->ops->post_set_mode)
1606 ap->ops->post_set_mode(ap);
1611 ata_port_disable(ap);
1615 * ata_tf_to_host - issue ATA taskfile to host controller
1616 * @ap: port to which command is being issued
1617 * @tf: ATA taskfile register set
1619 * Issues ATA taskfile register set to ATA host controller,
1620 * with proper synchronization with interrupt handler and
1624 * spin_lock_irqsave(host_set lock)
1627 static inline void ata_tf_to_host(struct ata_port *ap,
1628 const struct ata_taskfile *tf)
1630 ap->ops->tf_load(ap, tf);
1631 ap->ops->exec_command(ap, tf);
1635 * ata_busy_sleep - sleep until BSY clears, or timeout
1636 * @ap: port containing status register to be polled
1637 * @tmout_pat: impatience timeout
1638 * @tmout: overall timeout
1640 * Sleep until ATA Status register bit BSY clears,
1641 * or a timeout occurs.
1646 unsigned int ata_busy_sleep (struct ata_port *ap,
1647 unsigned long tmout_pat, unsigned long tmout)
1649 unsigned long timer_start, timeout;
1652 status = ata_busy_wait(ap, ATA_BUSY, 300);
1653 timer_start = jiffies;
1654 timeout = timer_start + tmout_pat;
1655 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1657 status = ata_busy_wait(ap, ATA_BUSY, 3);
1660 if (status & ATA_BUSY)
1661 printk(KERN_WARNING "ata%u is slow to respond, "
1662 "please be patient\n", ap->id);
1664 timeout = timer_start + tmout;
1665 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1667 status = ata_chk_status(ap);
1670 if (status & ATA_BUSY) {
1671 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1672 ap->id, tmout / HZ);
1679 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1681 struct ata_ioports *ioaddr = &ap->ioaddr;
1682 unsigned int dev0 = devmask & (1 << 0);
1683 unsigned int dev1 = devmask & (1 << 1);
1684 unsigned long timeout;
1686 /* if device 0 was found in ata_devchk, wait for its
1690 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1692 /* if device 1 was found in ata_devchk, wait for
1693 * register access, then wait for BSY to clear
1695 timeout = jiffies + ATA_TMOUT_BOOT;
1699 ap->ops->dev_select(ap, 1);
1700 if (ap->flags & ATA_FLAG_MMIO) {
1701 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1702 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1704 nsect = inb(ioaddr->nsect_addr);
1705 lbal = inb(ioaddr->lbal_addr);
1707 if ((nsect == 1) && (lbal == 1))
1709 if (time_after(jiffies, timeout)) {
1713 msleep(50); /* give drive a breather */
1716 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1718 /* is all this really necessary? */
1719 ap->ops->dev_select(ap, 0);
1721 ap->ops->dev_select(ap, 1);
1723 ap->ops->dev_select(ap, 0);
1727 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1728 * @ap: Port to reset and probe
1730 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1731 * probe the bus. Not often used these days.
1734 * PCI/etc. bus probe sem.
1735 * Obtains host_set lock.
1739 static unsigned int ata_bus_edd(struct ata_port *ap)
1741 struct ata_taskfile tf;
1742 unsigned long flags;
1744 /* set up execute-device-diag (bus reset) taskfile */
1745 /* also, take interrupts to a known state (disabled) */
1746 DPRINTK("execute-device-diag\n");
1747 ata_tf_init(ap, &tf, 0);
1749 tf.command = ATA_CMD_EDD;
1750 tf.protocol = ATA_PROT_NODATA;
1753 spin_lock_irqsave(&ap->host_set->lock, flags);
1754 ata_tf_to_host(ap, &tf);
1755 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1757 /* spec says at least 2ms. but who knows with those
1758 * crazy ATAPI devices...
1762 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1765 static unsigned int ata_bus_softreset(struct ata_port *ap,
1766 unsigned int devmask)
1768 struct ata_ioports *ioaddr = &ap->ioaddr;
1770 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1772 /* software reset. causes dev0 to be selected */
1773 if (ap->flags & ATA_FLAG_MMIO) {
1774 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1775 udelay(20); /* FIXME: flush */
1776 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1777 udelay(20); /* FIXME: flush */
1778 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1780 outb(ap->ctl, ioaddr->ctl_addr);
1782 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1784 outb(ap->ctl, ioaddr->ctl_addr);
1787 /* spec mandates ">= 2ms" before checking status.
1788 * We wait 150ms, because that was the magic delay used for
1789 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1790 * between when the ATA command register is written, and then
1791 * status is checked. Because waiting for "a while" before
1792 * checking status is fine, post SRST, we perform this magic
1793 * delay here as well.
1797 ata_bus_post_reset(ap, devmask);
1803 * ata_bus_reset - reset host port and associated ATA channel
1804 * @ap: port to reset
1806 * This is typically the first time we actually start issuing
1807 * commands to the ATA channel. We wait for BSY to clear, then
1808 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1809 * result. Determine what devices, if any, are on the channel
1810 * by looking at the device 0/1 error register. Look at the signature
1811 * stored in each device's taskfile registers, to determine if
1812 * the device is ATA or ATAPI.
1815 * PCI/etc. bus probe sem.
1816 * Obtains host_set lock.
1819 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1822 void ata_bus_reset(struct ata_port *ap)
1824 struct ata_ioports *ioaddr = &ap->ioaddr;
1825 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1827 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1829 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1831 /* determine if device 0/1 are present */
1832 if (ap->flags & ATA_FLAG_SATA_RESET)
1835 dev0 = ata_devchk(ap, 0);
1837 dev1 = ata_devchk(ap, 1);
1841 devmask |= (1 << 0);
1843 devmask |= (1 << 1);
1845 /* select device 0 again */
1846 ap->ops->dev_select(ap, 0);
1848 /* issue bus reset */
1849 if (ap->flags & ATA_FLAG_SRST)
1850 rc = ata_bus_softreset(ap, devmask);
1851 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1852 /* set up device control */
1853 if (ap->flags & ATA_FLAG_MMIO)
1854 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1856 outb(ap->ctl, ioaddr->ctl_addr);
1857 rc = ata_bus_edd(ap);
1864 * determine by signature whether we have ATA or ATAPI devices
1866 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1867 if ((slave_possible) && (err != 0x81))
1868 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1870 /* re-enable interrupts */
1871 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1874 /* is double-select really necessary? */
1875 if (ap->device[1].class != ATA_DEV_NONE)
1876 ap->ops->dev_select(ap, 1);
1877 if (ap->device[0].class != ATA_DEV_NONE)
1878 ap->ops->dev_select(ap, 0);
1880 /* if no devices were detected, disable this port */
1881 if ((ap->device[0].class == ATA_DEV_NONE) &&
1882 (ap->device[1].class == ATA_DEV_NONE))
1885 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1886 /* set up device control for ATA_FLAG_SATA_RESET */
1887 if (ap->flags & ATA_FLAG_MMIO)
1888 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1890 outb(ap->ctl, ioaddr->ctl_addr);
1897 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1898 ap->ops->port_disable(ap);
1903 static int sata_phy_resume(struct ata_port *ap)
1905 unsigned long timeout = jiffies + (HZ * 5);
1908 scr_write_flush(ap, SCR_CONTROL, 0x300);
1910 /* Wait for phy to become ready, if necessary. */
1913 sstatus = scr_read(ap, SCR_STATUS);
1914 if ((sstatus & 0xf) != 1)
1916 } while (time_before(jiffies, timeout));
1922 * ata_std_probeinit - initialize probing
1923 * @ap: port to be probed
1925 * @ap is about to be probed. Initialize it. This function is
1926 * to be used as standard callback for ata_drive_probe_reset().
1928 extern void ata_std_probeinit(struct ata_port *ap)
1930 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
1931 sata_phy_resume(ap);
1935 * ata_std_softreset - reset host port via ATA SRST
1936 * @ap: port to reset
1937 * @verbose: fail verbosely
1938 * @classes: resulting classes of attached devices
1940 * Reset host port using ATA SRST. This function is to be used
1941 * as standard callback for ata_drive_*_reset() functions.
1944 * Kernel thread context (may sleep)
1947 * 0 on success, -errno otherwise.
1949 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
1951 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1952 unsigned int devmask = 0, err_mask;
1957 /* determine if device 0/1 are present */
1958 if (ata_devchk(ap, 0))
1959 devmask |= (1 << 0);
1960 if (slave_possible && ata_devchk(ap, 1))
1961 devmask |= (1 << 1);
1963 /* devchk reports device presence without actual device on
1964 * most SATA controllers. Check SStatus and turn devmask off
1965 * if link is offline. Note that we should continue resetting
1966 * even when it seems like there's no device.
1968 if (ap->ops->scr_read && !sata_dev_present(ap))
1971 /* select device 0 again */
1972 ap->ops->dev_select(ap, 0);
1974 /* issue bus reset */
1975 DPRINTK("about to softreset, devmask=%x\n", devmask);
1976 err_mask = ata_bus_softreset(ap, devmask);
1979 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
1982 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
1987 /* determine by signature whether we have ATA or ATAPI devices */
1988 classes[0] = ata_dev_try_classify(ap, 0, &err);
1989 if (slave_possible && err != 0x81)
1990 classes[1] = ata_dev_try_classify(ap, 1, &err);
1992 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
1997 * sata_std_hardreset - reset host port via SATA phy reset
1998 * @ap: port to reset
1999 * @verbose: fail verbosely
2000 * @class: resulting class of attached device
2002 * SATA phy-reset host port using DET bits of SControl register.
2003 * This function is to be used as standard callback for
2004 * ata_drive_*_reset().
2007 * Kernel thread context (may sleep)
2010 * 0 on success, -errno otherwise.
2012 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2018 /* Issue phy wake/reset */
2019 scr_write_flush(ap, SCR_CONTROL, 0x301);
2022 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2023 * 10.4.2 says at least 1 ms.
2027 /* Bring phy back */
2028 sata_phy_resume(ap);
2031 serror = scr_read(ap, SCR_ERROR);
2032 scr_write(ap, SCR_ERROR, serror);
2034 /* TODO: phy layer with polling, timeouts, etc. */
2035 if (!sata_dev_present(ap)) {
2036 *class = ATA_DEV_NONE;
2037 DPRINTK("EXIT, link offline\n");
2041 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2043 printk(KERN_ERR "ata%u: COMRESET failed "
2044 "(device not ready)\n", ap->id);
2046 DPRINTK("EXIT, device not ready\n");
2050 *class = ata_dev_try_classify(ap, 0, NULL);
2052 DPRINTK("EXIT, class=%u\n", *class);
2057 * ata_std_postreset - standard postreset callback
2058 * @ap: the target ata_port
2059 * @classes: classes of attached devices
2061 * This function is invoked after a successful reset. Note that
2062 * the device might have been reset more than once using
2063 * different reset methods before postreset is invoked.
2064 * postreset is also reponsible for setting cable type.
2066 * This function is to be used as standard callback for
2067 * ata_drive_*_reset().
2070 * Kernel thread context (may sleep)
2072 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2076 /* set cable type */
2077 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2078 ap->cbl = ATA_CBL_SATA;
2080 /* print link status */
2081 if (ap->cbl == ATA_CBL_SATA)
2082 sata_print_link_status(ap);
2084 /* bail out if no device is present */
2085 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2086 DPRINTK("EXIT, no device\n");
2090 /* is double-select really necessary? */
2091 if (classes[0] != ATA_DEV_NONE)
2092 ap->ops->dev_select(ap, 1);
2093 if (classes[1] != ATA_DEV_NONE)
2094 ap->ops->dev_select(ap, 0);
2096 /* re-enable interrupts & set up device control */
2097 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2104 * ata_std_probe_reset - standard probe reset method
2105 * @ap: prot to perform probe-reset
2106 * @classes: resulting classes of attached devices
2108 * The stock off-the-shelf ->probe_reset method.
2111 * Kernel thread context (may sleep)
2114 * 0 on success, -errno otherwise.
2116 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2118 ata_reset_fn_t hardreset;
2121 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2122 hardreset = sata_std_hardreset;
2124 return ata_drive_probe_reset(ap, ata_std_probeinit,
2125 ata_std_softreset, hardreset,
2126 ata_std_postreset, classes);
2129 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2130 ata_postreset_fn_t postreset,
2131 unsigned int *classes)
2135 for (i = 0; i < ATA_MAX_DEVICES; i++)
2136 classes[i] = ATA_DEV_UNKNOWN;
2138 rc = reset(ap, 0, classes);
2142 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2143 * is complete and convert all ATA_DEV_UNKNOWN to
2146 for (i = 0; i < ATA_MAX_DEVICES; i++)
2147 if (classes[i] != ATA_DEV_UNKNOWN)
2150 if (i < ATA_MAX_DEVICES)
2151 for (i = 0; i < ATA_MAX_DEVICES; i++)
2152 if (classes[i] == ATA_DEV_UNKNOWN)
2153 classes[i] = ATA_DEV_NONE;
2156 postreset(ap, classes);
2158 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2162 * ata_drive_probe_reset - Perform probe reset with given methods
2163 * @ap: port to reset
2164 * @probeinit: probeinit method (can be NULL)
2165 * @softreset: softreset method (can be NULL)
2166 * @hardreset: hardreset method (can be NULL)
2167 * @postreset: postreset method (can be NULL)
2168 * @classes: resulting classes of attached devices
2170 * Reset the specified port and classify attached devices using
2171 * given methods. This function prefers softreset but tries all
2172 * possible reset sequences to reset and classify devices. This
2173 * function is intended to be used for constructing ->probe_reset
2174 * callback by low level drivers.
2176 * Reset methods should follow the following rules.
2178 * - Return 0 on sucess, -errno on failure.
2179 * - If classification is supported, fill classes[] with
2180 * recognized class codes.
2181 * - If classification is not supported, leave classes[] alone.
2182 * - If verbose is non-zero, print error message on failure;
2183 * otherwise, shut up.
2186 * Kernel thread context (may sleep)
2189 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2190 * if classification fails, and any error code from reset
2193 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2194 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2195 ata_postreset_fn_t postreset, unsigned int *classes)
2203 rc = do_probe_reset(ap, softreset, postreset, classes);
2211 rc = do_probe_reset(ap, hardreset, postreset, classes);
2212 if (rc == 0 || rc != -ENODEV)
2216 rc = do_probe_reset(ap, softreset, postreset, classes);
2221 static void ata_pr_blacklisted(const struct ata_port *ap,
2222 const struct ata_device *dev)
2224 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2225 ap->id, dev->devno);
2228 static const char * const ata_dma_blacklist [] = {
2247 "Toshiba CD-ROM XM-6202B",
2248 "TOSHIBA CD-ROM XM-1702BC",
2250 "E-IDE CD-ROM CR-840",
2253 "SAMSUNG CD-ROM SC-148C",
2254 "SAMSUNG CD-ROM SC",
2256 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2260 static int ata_dma_blacklisted(const struct ata_device *dev)
2262 unsigned char model_num[40];
2267 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2270 len = strnlen(s, sizeof(model_num));
2272 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2273 while ((len > 0) && (s[len - 1] == ' ')) {
2278 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2279 if (!strncmp(ata_dma_blacklist[i], s, len))
2285 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2287 const struct ata_device *master, *slave;
2290 master = &ap->device[0];
2291 slave = &ap->device[1];
2293 assert (ata_dev_present(master) || ata_dev_present(slave));
2295 if (shift == ATA_SHIFT_UDMA) {
2296 mask = ap->udma_mask;
2297 if (ata_dev_present(master)) {
2298 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2299 if (ata_dma_blacklisted(master)) {
2301 ata_pr_blacklisted(ap, master);
2304 if (ata_dev_present(slave)) {
2305 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2306 if (ata_dma_blacklisted(slave)) {
2308 ata_pr_blacklisted(ap, slave);
2312 else if (shift == ATA_SHIFT_MWDMA) {
2313 mask = ap->mwdma_mask;
2314 if (ata_dev_present(master)) {
2315 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2316 if (ata_dma_blacklisted(master)) {
2318 ata_pr_blacklisted(ap, master);
2321 if (ata_dev_present(slave)) {
2322 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2323 if (ata_dma_blacklisted(slave)) {
2325 ata_pr_blacklisted(ap, slave);
2329 else if (shift == ATA_SHIFT_PIO) {
2330 mask = ap->pio_mask;
2331 if (ata_dev_present(master)) {
2332 /* spec doesn't return explicit support for
2333 * PIO0-2, so we fake it
2335 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2340 if (ata_dev_present(slave)) {
2341 /* spec doesn't return explicit support for
2342 * PIO0-2, so we fake it
2344 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2351 mask = 0xffffffff; /* shut up compiler warning */
2358 /* find greatest bit */
2359 static int fgb(u32 bitmap)
2364 for (i = 0; i < 32; i++)
2365 if (bitmap & (1 << i))
2372 * ata_choose_xfer_mode - attempt to find best transfer mode
2373 * @ap: Port for which an xfer mode will be selected
2374 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2375 * @xfer_shift_out: (output) bit shift that selects this mode
2377 * Based on host and device capabilities, determine the
2378 * maximum transfer mode that is amenable to all.
2381 * PCI/etc. bus probe sem.
2384 * Zero on success, negative on error.
2387 static int ata_choose_xfer_mode(const struct ata_port *ap,
2389 unsigned int *xfer_shift_out)
2391 unsigned int mask, shift;
2394 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2395 shift = xfer_mode_classes[i].shift;
2396 mask = ata_get_mode_mask(ap, shift);
2400 *xfer_mode_out = xfer_mode_classes[i].base + x;
2401 *xfer_shift_out = shift;
2410 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2411 * @ap: Port associated with device @dev
2412 * @dev: Device to which command will be sent
2414 * Issue SET FEATURES - XFER MODE command to device @dev
2418 * PCI/etc. bus probe sem.
2421 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2423 struct ata_taskfile tf;
2425 /* set up set-features taskfile */
2426 DPRINTK("set features - xfer mode\n");
2428 ata_tf_init(ap, &tf, dev->devno);
2429 tf.command = ATA_CMD_SET_FEATURES;
2430 tf.feature = SETFEATURES_XFER;
2431 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2432 tf.protocol = ATA_PROT_NODATA;
2433 tf.nsect = dev->xfer_mode;
2435 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2436 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2438 ata_port_disable(ap);
2445 * ata_dev_reread_id - Reread the device identify device info
2446 * @ap: port where the device is
2447 * @dev: device to reread the identify device info
2452 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2454 struct ata_taskfile tf;
2456 ata_tf_init(ap, &tf, dev->devno);
2458 if (dev->class == ATA_DEV_ATA) {
2459 tf.command = ATA_CMD_ID_ATA;
2460 DPRINTK("do ATA identify\n");
2462 tf.command = ATA_CMD_ID_ATAPI;
2463 DPRINTK("do ATAPI identify\n");
2466 tf.flags |= ATA_TFLAG_DEVICE;
2467 tf.protocol = ATA_PROT_PIO;
2469 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2470 dev->id, sizeof(dev->id)))
2473 swap_buf_le16(dev->id, ATA_ID_WORDS);
2481 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
2482 ata_port_disable(ap);
2486 * ata_dev_init_params - Issue INIT DEV PARAMS command
2487 * @ap: Port associated with device @dev
2488 * @dev: Device to which command will be sent
2493 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2495 struct ata_taskfile tf;
2496 u16 sectors = dev->id[6];
2497 u16 heads = dev->id[3];
2499 /* Number of sectors per track 1-255. Number of heads 1-16 */
2500 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2503 /* set up init dev params taskfile */
2504 DPRINTK("init dev params \n");
2506 ata_tf_init(ap, &tf, dev->devno);
2507 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2508 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2509 tf.protocol = ATA_PROT_NODATA;
2511 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2513 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2514 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2516 ata_port_disable(ap);
2523 * ata_sg_clean - Unmap DMA memory associated with command
2524 * @qc: Command containing DMA memory to be released
2526 * Unmap all mapped DMA memory associated with this command.
2529 * spin_lock_irqsave(host_set lock)
2532 static void ata_sg_clean(struct ata_queued_cmd *qc)
2534 struct ata_port *ap = qc->ap;
2535 struct scatterlist *sg = qc->__sg;
2536 int dir = qc->dma_dir;
2537 void *pad_buf = NULL;
2539 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2542 if (qc->flags & ATA_QCFLAG_SINGLE)
2543 assert(qc->n_elem == 1);
2545 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2547 /* if we padded the buffer out to 32-bit bound, and data
2548 * xfer direction is from-device, we must copy from the
2549 * pad buffer back into the supplied buffer
2551 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2552 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2554 if (qc->flags & ATA_QCFLAG_SG) {
2556 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2557 /* restore last sg */
2558 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2560 struct scatterlist *psg = &qc->pad_sgent;
2561 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2562 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2563 kunmap_atomic(addr, KM_IRQ0);
2566 if (sg_dma_len(&sg[0]) > 0)
2567 dma_unmap_single(ap->host_set->dev,
2568 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2571 sg->length += qc->pad_len;
2573 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2574 pad_buf, qc->pad_len);
2577 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2582 * ata_fill_sg - Fill PCI IDE PRD table
2583 * @qc: Metadata associated with taskfile to be transferred
2585 * Fill PCI IDE PRD (scatter-gather) table with segments
2586 * associated with the current disk command.
2589 * spin_lock_irqsave(host_set lock)
2592 static void ata_fill_sg(struct ata_queued_cmd *qc)
2594 struct ata_port *ap = qc->ap;
2595 struct scatterlist *sg;
2598 assert(qc->__sg != NULL);
2599 assert(qc->n_elem > 0);
2602 ata_for_each_sg(sg, qc) {
2606 /* determine if physical DMA addr spans 64K boundary.
2607 * Note h/w doesn't support 64-bit, so we unconditionally
2608 * truncate dma_addr_t to u32.
2610 addr = (u32) sg_dma_address(sg);
2611 sg_len = sg_dma_len(sg);
2614 offset = addr & 0xffff;
2616 if ((offset + sg_len) > 0x10000)
2617 len = 0x10000 - offset;
2619 ap->prd[idx].addr = cpu_to_le32(addr);
2620 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2621 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2630 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2633 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2634 * @qc: Metadata associated with taskfile to check
2636 * Allow low-level driver to filter ATA PACKET commands, returning
2637 * a status indicating whether or not it is OK to use DMA for the
2638 * supplied PACKET command.
2641 * spin_lock_irqsave(host_set lock)
2643 * RETURNS: 0 when ATAPI DMA can be used
2646 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2648 struct ata_port *ap = qc->ap;
2649 int rc = 0; /* Assume ATAPI DMA is OK by default */
2651 if (ap->ops->check_atapi_dma)
2652 rc = ap->ops->check_atapi_dma(qc);
2657 * ata_qc_prep - Prepare taskfile for submission
2658 * @qc: Metadata associated with taskfile to be prepared
2660 * Prepare ATA taskfile for submission.
2663 * spin_lock_irqsave(host_set lock)
2665 void ata_qc_prep(struct ata_queued_cmd *qc)
2667 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2674 * ata_sg_init_one - Associate command with memory buffer
2675 * @qc: Command to be associated
2676 * @buf: Memory buffer
2677 * @buflen: Length of memory buffer, in bytes.
2679 * Initialize the data-related elements of queued_cmd @qc
2680 * to point to a single memory buffer, @buf of byte length @buflen.
2683 * spin_lock_irqsave(host_set lock)
2686 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2688 struct scatterlist *sg;
2690 qc->flags |= ATA_QCFLAG_SINGLE;
2692 memset(&qc->sgent, 0, sizeof(qc->sgent));
2693 qc->__sg = &qc->sgent;
2695 qc->orig_n_elem = 1;
2699 sg_init_one(sg, buf, buflen);
2703 * ata_sg_init - Associate command with scatter-gather table.
2704 * @qc: Command to be associated
2705 * @sg: Scatter-gather table.
2706 * @n_elem: Number of elements in s/g table.
2708 * Initialize the data-related elements of queued_cmd @qc
2709 * to point to a scatter-gather table @sg, containing @n_elem
2713 * spin_lock_irqsave(host_set lock)
2716 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2717 unsigned int n_elem)
2719 qc->flags |= ATA_QCFLAG_SG;
2721 qc->n_elem = n_elem;
2722 qc->orig_n_elem = n_elem;
2726 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2727 * @qc: Command with memory buffer to be mapped.
2729 * DMA-map the memory buffer associated with queued_cmd @qc.
2732 * spin_lock_irqsave(host_set lock)
2735 * Zero on success, negative on error.
2738 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2740 struct ata_port *ap = qc->ap;
2741 int dir = qc->dma_dir;
2742 struct scatterlist *sg = qc->__sg;
2743 dma_addr_t dma_address;
2745 /* we must lengthen transfers to end on a 32-bit boundary */
2746 qc->pad_len = sg->length & 3;
2748 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2749 struct scatterlist *psg = &qc->pad_sgent;
2751 assert(qc->dev->class == ATA_DEV_ATAPI);
2753 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2755 if (qc->tf.flags & ATA_TFLAG_WRITE)
2756 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2759 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2760 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2762 sg->length -= qc->pad_len;
2764 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2765 sg->length, qc->pad_len);
2769 sg_dma_address(sg) = 0;
2773 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2775 if (dma_mapping_error(dma_address)) {
2777 sg->length += qc->pad_len;
2781 sg_dma_address(sg) = dma_address;
2783 sg_dma_len(sg) = sg->length;
2785 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2786 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2792 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2793 * @qc: Command with scatter-gather table to be mapped.
2795 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2798 * spin_lock_irqsave(host_set lock)
2801 * Zero on success, negative on error.
2805 static int ata_sg_setup(struct ata_queued_cmd *qc)
2807 struct ata_port *ap = qc->ap;
2808 struct scatterlist *sg = qc->__sg;
2809 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2810 int n_elem, pre_n_elem, dir, trim_sg = 0;
2812 VPRINTK("ENTER, ata%u\n", ap->id);
2813 assert(qc->flags & ATA_QCFLAG_SG);
2815 /* we must lengthen transfers to end on a 32-bit boundary */
2816 qc->pad_len = lsg->length & 3;
2818 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2819 struct scatterlist *psg = &qc->pad_sgent;
2820 unsigned int offset;
2822 assert(qc->dev->class == ATA_DEV_ATAPI);
2824 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2827 * psg->page/offset are used to copy to-be-written
2828 * data in this function or read data in ata_sg_clean.
2830 offset = lsg->offset + lsg->length - qc->pad_len;
2831 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2832 psg->offset = offset_in_page(offset);
2834 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2835 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2836 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2837 kunmap_atomic(addr, KM_IRQ0);
2840 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2841 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2843 lsg->length -= qc->pad_len;
2844 if (lsg->length == 0)
2847 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2848 qc->n_elem - 1, lsg->length, qc->pad_len);
2851 pre_n_elem = qc->n_elem;
2852 if (trim_sg && pre_n_elem)
2861 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
2863 /* restore last sg */
2864 lsg->length += qc->pad_len;
2868 DPRINTK("%d sg elements mapped\n", n_elem);
2871 qc->n_elem = n_elem;
2877 * ata_poll_qc_complete - turn irq back on and finish qc
2878 * @qc: Command to complete
2879 * @err_mask: ATA status register content
2882 * None. (grabs host lock)
2885 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
2887 struct ata_port *ap = qc->ap;
2888 unsigned long flags;
2890 spin_lock_irqsave(&ap->host_set->lock, flags);
2891 ap->flags &= ~ATA_FLAG_NOINTR;
2893 ata_qc_complete(qc);
2894 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2898 * ata_pio_poll - poll using PIO, depending on current state
2899 * @ap: the target ata_port
2902 * None. (executing in kernel thread context)
2905 * timeout value to use
2908 static unsigned long ata_pio_poll(struct ata_port *ap)
2910 struct ata_queued_cmd *qc;
2912 unsigned int poll_state = HSM_ST_UNKNOWN;
2913 unsigned int reg_state = HSM_ST_UNKNOWN;
2915 qc = ata_qc_from_tag(ap, ap->active_tag);
2918 switch (ap->hsm_task_state) {
2921 poll_state = HSM_ST_POLL;
2925 case HSM_ST_LAST_POLL:
2926 poll_state = HSM_ST_LAST_POLL;
2927 reg_state = HSM_ST_LAST;
2934 status = ata_chk_status(ap);
2935 if (status & ATA_BUSY) {
2936 if (time_after(jiffies, ap->pio_task_timeout)) {
2937 qc->err_mask |= AC_ERR_TIMEOUT;
2938 ap->hsm_task_state = HSM_ST_TMOUT;
2941 ap->hsm_task_state = poll_state;
2942 return ATA_SHORT_PAUSE;
2945 ap->hsm_task_state = reg_state;
2950 * ata_pio_complete - check if drive is busy or idle
2951 * @ap: the target ata_port
2954 * None. (executing in kernel thread context)
2957 * Non-zero if qc completed, zero otherwise.
2960 static int ata_pio_complete (struct ata_port *ap)
2962 struct ata_queued_cmd *qc;
2966 * This is purely heuristic. This is a fast path. Sometimes when
2967 * we enter, BSY will be cleared in a chk-status or two. If not,
2968 * the drive is probably seeking or something. Snooze for a couple
2969 * msecs, then chk-status again. If still busy, fall back to
2970 * HSM_ST_POLL state.
2972 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2973 if (drv_stat & ATA_BUSY) {
2975 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2976 if (drv_stat & ATA_BUSY) {
2977 ap->hsm_task_state = HSM_ST_LAST_POLL;
2978 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2983 qc = ata_qc_from_tag(ap, ap->active_tag);
2986 drv_stat = ata_wait_idle(ap);
2987 if (!ata_ok(drv_stat)) {
2988 qc->err_mask |= __ac_err_mask(drv_stat);
2989 ap->hsm_task_state = HSM_ST_ERR;
2993 ap->hsm_task_state = HSM_ST_IDLE;
2995 assert(qc->err_mask == 0);
2996 ata_poll_qc_complete(qc);
2998 /* another command may start at this point */
3005 * swap_buf_le16 - swap halves of 16-bit words in place
3006 * @buf: Buffer to swap
3007 * @buf_words: Number of 16-bit words in buffer.
3009 * Swap halves of 16-bit words if needed to convert from
3010 * little-endian byte order to native cpu byte order, or
3014 * Inherited from caller.
3016 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3021 for (i = 0; i < buf_words; i++)
3022 buf[i] = le16_to_cpu(buf[i]);
3023 #endif /* __BIG_ENDIAN */
3027 * ata_mmio_data_xfer - Transfer data by MMIO
3028 * @ap: port to read/write
3030 * @buflen: buffer length
3031 * @write_data: read/write
3033 * Transfer data from/to the device data register by MMIO.
3036 * Inherited from caller.
3039 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3040 unsigned int buflen, int write_data)
3043 unsigned int words = buflen >> 1;
3044 u16 *buf16 = (u16 *) buf;
3045 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3047 /* Transfer multiple of 2 bytes */
3049 for (i = 0; i < words; i++)
3050 writew(le16_to_cpu(buf16[i]), mmio);
3052 for (i = 0; i < words; i++)
3053 buf16[i] = cpu_to_le16(readw(mmio));
3056 /* Transfer trailing 1 byte, if any. */
3057 if (unlikely(buflen & 0x01)) {
3058 u16 align_buf[1] = { 0 };
3059 unsigned char *trailing_buf = buf + buflen - 1;
3062 memcpy(align_buf, trailing_buf, 1);
3063 writew(le16_to_cpu(align_buf[0]), mmio);
3065 align_buf[0] = cpu_to_le16(readw(mmio));
3066 memcpy(trailing_buf, align_buf, 1);
3072 * ata_pio_data_xfer - Transfer data by PIO
3073 * @ap: port to read/write
3075 * @buflen: buffer length
3076 * @write_data: read/write
3078 * Transfer data from/to the device data register by PIO.
3081 * Inherited from caller.
3084 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3085 unsigned int buflen, int write_data)
3087 unsigned int words = buflen >> 1;
3089 /* Transfer multiple of 2 bytes */
3091 outsw(ap->ioaddr.data_addr, buf, words);
3093 insw(ap->ioaddr.data_addr, buf, words);
3095 /* Transfer trailing 1 byte, if any. */
3096 if (unlikely(buflen & 0x01)) {
3097 u16 align_buf[1] = { 0 };
3098 unsigned char *trailing_buf = buf + buflen - 1;
3101 memcpy(align_buf, trailing_buf, 1);
3102 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3104 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3105 memcpy(trailing_buf, align_buf, 1);
3111 * ata_data_xfer - Transfer data from/to the data register.
3112 * @ap: port to read/write
3114 * @buflen: buffer length
3115 * @do_write: read/write
3117 * Transfer data from/to the device data register.
3120 * Inherited from caller.
3123 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3124 unsigned int buflen, int do_write)
3126 /* Make the crap hardware pay the costs not the good stuff */
3127 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3128 unsigned long flags;
3129 local_irq_save(flags);
3130 if (ap->flags & ATA_FLAG_MMIO)
3131 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3133 ata_pio_data_xfer(ap, buf, buflen, do_write);
3134 local_irq_restore(flags);
3136 if (ap->flags & ATA_FLAG_MMIO)
3137 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3139 ata_pio_data_xfer(ap, buf, buflen, do_write);
3144 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3145 * @qc: Command on going
3147 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3150 * Inherited from caller.
3153 static void ata_pio_sector(struct ata_queued_cmd *qc)
3155 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3156 struct scatterlist *sg = qc->__sg;
3157 struct ata_port *ap = qc->ap;
3159 unsigned int offset;
3162 if (qc->cursect == (qc->nsect - 1))
3163 ap->hsm_task_state = HSM_ST_LAST;
3165 page = sg[qc->cursg].page;
3166 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3168 /* get the current page and offset */
3169 page = nth_page(page, (offset >> PAGE_SHIFT));
3170 offset %= PAGE_SIZE;
3172 buf = kmap(page) + offset;
3177 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3182 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3184 /* do the actual data transfer */
3185 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3186 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3192 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3193 * @qc: Command on going
3194 * @bytes: number of bytes
3196 * Transfer Transfer data from/to the ATAPI device.
3199 * Inherited from caller.
3203 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3205 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3206 struct scatterlist *sg = qc->__sg;
3207 struct ata_port *ap = qc->ap;
3210 unsigned int offset, count;
3212 if (qc->curbytes + bytes >= qc->nbytes)
3213 ap->hsm_task_state = HSM_ST_LAST;
3216 if (unlikely(qc->cursg >= qc->n_elem)) {
3218 * The end of qc->sg is reached and the device expects
3219 * more data to transfer. In order not to overrun qc->sg
3220 * and fulfill length specified in the byte count register,
3221 * - for read case, discard trailing data from the device
3222 * - for write case, padding zero data to the device
3224 u16 pad_buf[1] = { 0 };
3225 unsigned int words = bytes >> 1;
3228 if (words) /* warning if bytes > 1 */
3229 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3232 for (i = 0; i < words; i++)
3233 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3235 ap->hsm_task_state = HSM_ST_LAST;
3239 sg = &qc->__sg[qc->cursg];
3242 offset = sg->offset + qc->cursg_ofs;
3244 /* get the current page and offset */
3245 page = nth_page(page, (offset >> PAGE_SHIFT));
3246 offset %= PAGE_SIZE;
3248 /* don't overrun current sg */
3249 count = min(sg->length - qc->cursg_ofs, bytes);
3251 /* don't cross page boundaries */
3252 count = min(count, (unsigned int)PAGE_SIZE - offset);
3254 buf = kmap(page) + offset;
3257 qc->curbytes += count;
3258 qc->cursg_ofs += count;
3260 if (qc->cursg_ofs == sg->length) {
3265 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3267 /* do the actual data transfer */
3268 ata_data_xfer(ap, buf, count, do_write);
3277 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3278 * @qc: Command on going
3280 * Transfer Transfer data from/to the ATAPI device.
3283 * Inherited from caller.
3286 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3288 struct ata_port *ap = qc->ap;
3289 struct ata_device *dev = qc->dev;
3290 unsigned int ireason, bc_lo, bc_hi, bytes;
3291 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3293 ap->ops->tf_read(ap, &qc->tf);
3294 ireason = qc->tf.nsect;
3295 bc_lo = qc->tf.lbam;
3296 bc_hi = qc->tf.lbah;
3297 bytes = (bc_hi << 8) | bc_lo;
3299 /* shall be cleared to zero, indicating xfer of data */
3300 if (ireason & (1 << 0))
3303 /* make sure transfer direction matches expected */
3304 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3305 if (do_write != i_write)
3308 __atapi_pio_bytes(qc, bytes);
3313 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3314 ap->id, dev->devno);
3315 qc->err_mask |= AC_ERR_HSM;
3316 ap->hsm_task_state = HSM_ST_ERR;
3320 * ata_pio_block - start PIO on a block
3321 * @ap: the target ata_port
3324 * None. (executing in kernel thread context)
3327 static void ata_pio_block(struct ata_port *ap)
3329 struct ata_queued_cmd *qc;
3333 * This is purely heuristic. This is a fast path.
3334 * Sometimes when we enter, BSY will be cleared in
3335 * a chk-status or two. If not, the drive is probably seeking
3336 * or something. Snooze for a couple msecs, then
3337 * chk-status again. If still busy, fall back to
3338 * HSM_ST_POLL state.
3340 status = ata_busy_wait(ap, ATA_BUSY, 5);
3341 if (status & ATA_BUSY) {
3343 status = ata_busy_wait(ap, ATA_BUSY, 10);
3344 if (status & ATA_BUSY) {
3345 ap->hsm_task_state = HSM_ST_POLL;
3346 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3351 qc = ata_qc_from_tag(ap, ap->active_tag);
3355 if (status & (ATA_ERR | ATA_DF)) {
3356 qc->err_mask |= AC_ERR_DEV;
3357 ap->hsm_task_state = HSM_ST_ERR;
3361 /* transfer data if any */
3362 if (is_atapi_taskfile(&qc->tf)) {
3363 /* DRQ=0 means no more data to transfer */
3364 if ((status & ATA_DRQ) == 0) {
3365 ap->hsm_task_state = HSM_ST_LAST;
3369 atapi_pio_bytes(qc);
3371 /* handle BSY=0, DRQ=0 as error */
3372 if ((status & ATA_DRQ) == 0) {
3373 qc->err_mask |= AC_ERR_HSM;
3374 ap->hsm_task_state = HSM_ST_ERR;
3382 static void ata_pio_error(struct ata_port *ap)
3384 struct ata_queued_cmd *qc;
3386 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3388 qc = ata_qc_from_tag(ap, ap->active_tag);
3391 /* make sure qc->err_mask is available to
3392 * know what's wrong and recover
3394 assert(qc->err_mask);
3396 ap->hsm_task_state = HSM_ST_IDLE;
3398 ata_poll_qc_complete(qc);
3401 static void ata_pio_task(void *_data)
3403 struct ata_port *ap = _data;
3404 unsigned long timeout;
3411 switch (ap->hsm_task_state) {
3420 qc_completed = ata_pio_complete(ap);
3424 case HSM_ST_LAST_POLL:
3425 timeout = ata_pio_poll(ap);
3435 ata_queue_delayed_pio_task(ap, timeout);
3436 else if (!qc_completed)
3441 * ata_qc_timeout - Handle timeout of queued command
3442 * @qc: Command that timed out
3444 * Some part of the kernel (currently, only the SCSI layer)
3445 * has noticed that the active command on port @ap has not
3446 * completed after a specified length of time. Handle this
3447 * condition by disabling DMA (if necessary) and completing
3448 * transactions, with error if necessary.
3450 * This also handles the case of the "lost interrupt", where
3451 * for some reason (possibly hardware bug, possibly driver bug)
3452 * an interrupt was not delivered to the driver, even though the
3453 * transaction completed successfully.
3456 * Inherited from SCSI layer (none, can sleep)
3459 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3461 struct ata_port *ap = qc->ap;
3462 struct ata_host_set *host_set = ap->host_set;
3463 u8 host_stat = 0, drv_stat;
3464 unsigned long flags;
3468 ata_flush_pio_tasks(ap);
3469 ap->hsm_task_state = HSM_ST_IDLE;
3471 spin_lock_irqsave(&host_set->lock, flags);
3473 switch (qc->tf.protocol) {
3476 case ATA_PROT_ATAPI_DMA:
3477 host_stat = ap->ops->bmdma_status(ap);
3479 /* before we do anything else, clear DMA-Start bit */
3480 ap->ops->bmdma_stop(qc);
3486 drv_stat = ata_chk_status(ap);
3488 /* ack bmdma irq events */
3489 ap->ops->irq_clear(ap);
3491 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3492 ap->id, qc->tf.command, drv_stat, host_stat);
3494 /* complete taskfile transaction */
3495 qc->err_mask |= ac_err_mask(drv_stat);
3499 spin_unlock_irqrestore(&host_set->lock, flags);
3501 ata_eh_qc_complete(qc);
3507 * ata_eng_timeout - Handle timeout of queued command
3508 * @ap: Port on which timed-out command is active
3510 * Some part of the kernel (currently, only the SCSI layer)
3511 * has noticed that the active command on port @ap has not
3512 * completed after a specified length of time. Handle this
3513 * condition by disabling DMA (if necessary) and completing
3514 * transactions, with error if necessary.
3516 * This also handles the case of the "lost interrupt", where
3517 * for some reason (possibly hardware bug, possibly driver bug)
3518 * an interrupt was not delivered to the driver, even though the
3519 * transaction completed successfully.
3522 * Inherited from SCSI layer (none, can sleep)
3525 void ata_eng_timeout(struct ata_port *ap)
3527 struct ata_queued_cmd *qc;
3531 qc = ata_qc_from_tag(ap, ap->active_tag);
3535 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3545 * ata_qc_new - Request an available ATA command, for queueing
3546 * @ap: Port associated with device @dev
3547 * @dev: Device from whom we request an available command structure
3553 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3555 struct ata_queued_cmd *qc = NULL;
3558 for (i = 0; i < ATA_MAX_QUEUE; i++)
3559 if (!test_and_set_bit(i, &ap->qactive)) {
3560 qc = ata_qc_from_tag(ap, i);
3571 * ata_qc_new_init - Request an available ATA command, and initialize it
3572 * @ap: Port associated with device @dev
3573 * @dev: Device from whom we request an available command structure
3579 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3580 struct ata_device *dev)
3582 struct ata_queued_cmd *qc;
3584 qc = ata_qc_new(ap);
3597 * ata_qc_free - free unused ata_queued_cmd
3598 * @qc: Command to complete
3600 * Designed to free unused ata_queued_cmd object
3601 * in case something prevents using it.
3604 * spin_lock_irqsave(host_set lock)
3606 void ata_qc_free(struct ata_queued_cmd *qc)
3608 struct ata_port *ap = qc->ap;
3611 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3615 if (likely(ata_tag_valid(tag))) {
3616 if (tag == ap->active_tag)
3617 ap->active_tag = ATA_TAG_POISON;
3618 qc->tag = ATA_TAG_POISON;
3619 clear_bit(tag, &ap->qactive);
3623 inline void __ata_qc_complete(struct ata_queued_cmd *qc)
3625 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3626 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3628 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3631 /* atapi: mark qc as inactive to prevent the interrupt handler
3632 * from completing the command twice later, before the error handler
3633 * is called. (when rc != 0 and atapi request sense is needed)
3635 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3637 /* call completion callback */
3638 qc->complete_fn(qc);
3642 * ata_qc_complete - Complete an active ATA command
3643 * @qc: Command to complete
3644 * @err_mask: ATA Status register contents
3646 * Indicate to the mid and upper layers that an ATA
3647 * command has completed, with either an ok or not-ok status.
3650 * spin_lock_irqsave(host_set lock)
3652 void ata_qc_complete(struct ata_queued_cmd *qc)
3654 if (unlikely(qc->flags & ATA_QCFLAG_EH_SCHEDULED))
3657 __ata_qc_complete(qc);
3660 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3662 struct ata_port *ap = qc->ap;
3664 switch (qc->tf.protocol) {
3666 case ATA_PROT_ATAPI_DMA:
3669 case ATA_PROT_ATAPI:
3671 case ATA_PROT_PIO_MULT:
3672 if (ap->flags & ATA_FLAG_PIO_DMA)
3685 * ata_qc_issue - issue taskfile to device
3686 * @qc: command to issue to device
3688 * Prepare an ATA command to submission to device.
3689 * This includes mapping the data into a DMA-able
3690 * area, filling in the S/G table, and finally
3691 * writing the taskfile to hardware, starting the command.
3694 * spin_lock_irqsave(host_set lock)
3697 * Zero on success, AC_ERR_* mask on failure
3700 unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
3702 struct ata_port *ap = qc->ap;
3704 if (ata_should_dma_map(qc)) {
3705 if (qc->flags & ATA_QCFLAG_SG) {
3706 if (ata_sg_setup(qc))
3708 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3709 if (ata_sg_setup_one(qc))
3713 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3716 ap->ops->qc_prep(qc);
3718 qc->ap->active_tag = qc->tag;
3719 qc->flags |= ATA_QCFLAG_ACTIVE;
3721 return ap->ops->qc_issue(qc);
3724 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3725 return AC_ERR_SYSTEM;
3730 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3731 * @qc: command to issue to device
3733 * Using various libata functions and hooks, this function
3734 * starts an ATA command. ATA commands are grouped into
3735 * classes called "protocols", and issuing each type of protocol
3736 * is slightly different.
3738 * May be used as the qc_issue() entry in ata_port_operations.
3741 * spin_lock_irqsave(host_set lock)
3744 * Zero on success, AC_ERR_* mask on failure
3747 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3749 struct ata_port *ap = qc->ap;
3751 ata_dev_select(ap, qc->dev->devno, 1, 0);
3753 switch (qc->tf.protocol) {
3754 case ATA_PROT_NODATA:
3755 ata_tf_to_host(ap, &qc->tf);
3759 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3760 ap->ops->bmdma_setup(qc); /* set up bmdma */
3761 ap->ops->bmdma_start(qc); /* initiate bmdma */
3764 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3765 ata_qc_set_polling(qc);
3766 ata_tf_to_host(ap, &qc->tf);
3767 ap->hsm_task_state = HSM_ST;
3768 ata_queue_pio_task(ap);
3771 case ATA_PROT_ATAPI:
3772 ata_qc_set_polling(qc);
3773 ata_tf_to_host(ap, &qc->tf);
3774 ata_queue_packet_task(ap);
3777 case ATA_PROT_ATAPI_NODATA:
3778 ap->flags |= ATA_FLAG_NOINTR;
3779 ata_tf_to_host(ap, &qc->tf);
3780 ata_queue_packet_task(ap);
3783 case ATA_PROT_ATAPI_DMA:
3784 ap->flags |= ATA_FLAG_NOINTR;
3785 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3786 ap->ops->bmdma_setup(qc); /* set up bmdma */
3787 ata_queue_packet_task(ap);
3792 return AC_ERR_SYSTEM;
3799 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3800 * @qc: Info associated with this ATA transaction.
3803 * spin_lock_irqsave(host_set lock)
3806 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3808 struct ata_port *ap = qc->ap;
3809 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3811 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3813 /* load PRD table addr. */
3814 mb(); /* make sure PRD table writes are visible to controller */
3815 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3817 /* specify data direction, triple-check start bit is clear */
3818 dmactl = readb(mmio + ATA_DMA_CMD);
3819 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3821 dmactl |= ATA_DMA_WR;
3822 writeb(dmactl, mmio + ATA_DMA_CMD);
3824 /* issue r/w command */
3825 ap->ops->exec_command(ap, &qc->tf);
3829 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3830 * @qc: Info associated with this ATA transaction.
3833 * spin_lock_irqsave(host_set lock)
3836 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3838 struct ata_port *ap = qc->ap;
3839 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3842 /* start host DMA transaction */
3843 dmactl = readb(mmio + ATA_DMA_CMD);
3844 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3846 /* Strictly, one may wish to issue a readb() here, to
3847 * flush the mmio write. However, control also passes
3848 * to the hardware at this point, and it will interrupt
3849 * us when we are to resume control. So, in effect,
3850 * we don't care when the mmio write flushes.
3851 * Further, a read of the DMA status register _immediately_
3852 * following the write may not be what certain flaky hardware
3853 * is expected, so I think it is best to not add a readb()
3854 * without first all the MMIO ATA cards/mobos.
3855 * Or maybe I'm just being paranoid.
3860 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3861 * @qc: Info associated with this ATA transaction.
3864 * spin_lock_irqsave(host_set lock)
3867 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3869 struct ata_port *ap = qc->ap;
3870 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3873 /* load PRD table addr. */
3874 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3876 /* specify data direction, triple-check start bit is clear */
3877 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3878 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3880 dmactl |= ATA_DMA_WR;
3881 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3883 /* issue r/w command */
3884 ap->ops->exec_command(ap, &qc->tf);
3888 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3889 * @qc: Info associated with this ATA transaction.
3892 * spin_lock_irqsave(host_set lock)
3895 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3897 struct ata_port *ap = qc->ap;
3900 /* start host DMA transaction */
3901 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3902 outb(dmactl | ATA_DMA_START,
3903 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3908 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3909 * @qc: Info associated with this ATA transaction.
3911 * Writes the ATA_DMA_START flag to the DMA command register.
3913 * May be used as the bmdma_start() entry in ata_port_operations.
3916 * spin_lock_irqsave(host_set lock)
3918 void ata_bmdma_start(struct ata_queued_cmd *qc)
3920 if (qc->ap->flags & ATA_FLAG_MMIO)
3921 ata_bmdma_start_mmio(qc);
3923 ata_bmdma_start_pio(qc);
3928 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3929 * @qc: Info associated with this ATA transaction.
3931 * Writes address of PRD table to device's PRD Table Address
3932 * register, sets the DMA control register, and calls
3933 * ops->exec_command() to start the transfer.
3935 * May be used as the bmdma_setup() entry in ata_port_operations.
3938 * spin_lock_irqsave(host_set lock)
3940 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3942 if (qc->ap->flags & ATA_FLAG_MMIO)
3943 ata_bmdma_setup_mmio(qc);
3945 ata_bmdma_setup_pio(qc);
3950 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3951 * @ap: Port associated with this ATA transaction.
3953 * Clear interrupt and error flags in DMA status register.
3955 * May be used as the irq_clear() entry in ata_port_operations.
3958 * spin_lock_irqsave(host_set lock)
3961 void ata_bmdma_irq_clear(struct ata_port *ap)
3963 if (ap->flags & ATA_FLAG_MMIO) {
3964 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3965 writeb(readb(mmio), mmio);
3967 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3968 outb(inb(addr), addr);
3975 * ata_bmdma_status - Read PCI IDE BMDMA status
3976 * @ap: Port associated with this ATA transaction.
3978 * Read and return BMDMA status register.
3980 * May be used as the bmdma_status() entry in ata_port_operations.
3983 * spin_lock_irqsave(host_set lock)
3986 u8 ata_bmdma_status(struct ata_port *ap)
3989 if (ap->flags & ATA_FLAG_MMIO) {
3990 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3991 host_stat = readb(mmio + ATA_DMA_STATUS);
3993 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3999 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4000 * @qc: Command we are ending DMA for
4002 * Clears the ATA_DMA_START flag in the dma control register
4004 * May be used as the bmdma_stop() entry in ata_port_operations.
4007 * spin_lock_irqsave(host_set lock)
4010 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4012 struct ata_port *ap = qc->ap;
4013 if (ap->flags & ATA_FLAG_MMIO) {
4014 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4016 /* clear start/stop bit */
4017 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4018 mmio + ATA_DMA_CMD);
4020 /* clear start/stop bit */
4021 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4022 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4025 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4026 ata_altstatus(ap); /* dummy read */
4030 * ata_host_intr - Handle host interrupt for given (port, task)
4031 * @ap: Port on which interrupt arrived (possibly...)
4032 * @qc: Taskfile currently active in engine
4034 * Handle host interrupt for given queued command. Currently,
4035 * only DMA interrupts are handled. All other commands are
4036 * handled via polling with interrupts disabled (nIEN bit).
4039 * spin_lock_irqsave(host_set lock)
4042 * One if interrupt was handled, zero if not (shared irq).
4045 inline unsigned int ata_host_intr (struct ata_port *ap,
4046 struct ata_queued_cmd *qc)
4048 u8 status, host_stat;
4050 switch (qc->tf.protocol) {
4053 case ATA_PROT_ATAPI_DMA:
4054 case ATA_PROT_ATAPI:
4055 /* check status of DMA engine */
4056 host_stat = ap->ops->bmdma_status(ap);
4057 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4059 /* if it's not our irq... */
4060 if (!(host_stat & ATA_DMA_INTR))
4063 /* before we do anything else, clear DMA-Start bit */
4064 ap->ops->bmdma_stop(qc);
4068 case ATA_PROT_ATAPI_NODATA:
4069 case ATA_PROT_NODATA:
4070 /* check altstatus */
4071 status = ata_altstatus(ap);
4072 if (status & ATA_BUSY)
4075 /* check main status, clearing INTRQ */
4076 status = ata_chk_status(ap);
4077 if (unlikely(status & ATA_BUSY))
4079 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4080 ap->id, qc->tf.protocol, status);
4082 /* ack bmdma irq events */
4083 ap->ops->irq_clear(ap);
4085 /* complete taskfile transaction */
4086 qc->err_mask |= ac_err_mask(status);
4087 ata_qc_complete(qc);
4094 return 1; /* irq handled */
4097 ap->stats.idle_irq++;
4100 if ((ap->stats.idle_irq % 1000) == 0) {
4102 ata_irq_ack(ap, 0); /* debug trap */
4103 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4106 return 0; /* irq not handled */
4110 * ata_interrupt - Default ATA host interrupt handler
4111 * @irq: irq line (unused)
4112 * @dev_instance: pointer to our ata_host_set information structure
4115 * Default interrupt handler for PCI IDE devices. Calls
4116 * ata_host_intr() for each port that is not disabled.
4119 * Obtains host_set lock during operation.
4122 * IRQ_NONE or IRQ_HANDLED.
4125 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4127 struct ata_host_set *host_set = dev_instance;
4129 unsigned int handled = 0;
4130 unsigned long flags;
4132 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4133 spin_lock_irqsave(&host_set->lock, flags);
4135 for (i = 0; i < host_set->n_ports; i++) {
4136 struct ata_port *ap;
4138 ap = host_set->ports[i];
4140 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
4141 struct ata_queued_cmd *qc;
4143 qc = ata_qc_from_tag(ap, ap->active_tag);
4144 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4145 (qc->flags & ATA_QCFLAG_ACTIVE))
4146 handled |= ata_host_intr(ap, qc);
4150 spin_unlock_irqrestore(&host_set->lock, flags);
4152 return IRQ_RETVAL(handled);
4156 * atapi_packet_task - Write CDB bytes to hardware
4157 * @_data: Port to which ATAPI device is attached.
4159 * When device has indicated its readiness to accept
4160 * a CDB, this function is called. Send the CDB.
4161 * If DMA is to be performed, exit immediately.
4162 * Otherwise, we are in polling mode, so poll
4163 * status under operation succeeds or fails.
4166 * Kernel thread context (may sleep)
4169 static void atapi_packet_task(void *_data)
4171 struct ata_port *ap = _data;
4172 struct ata_queued_cmd *qc;
4175 qc = ata_qc_from_tag(ap, ap->active_tag);
4177 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4179 /* sleep-wait for BSY to clear */
4180 DPRINTK("busy wait\n");
4181 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4182 qc->err_mask |= AC_ERR_TIMEOUT;
4186 /* make sure DRQ is set */
4187 status = ata_chk_status(ap);
4188 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4189 qc->err_mask |= AC_ERR_HSM;
4194 DPRINTK("send cdb\n");
4195 assert(ap->cdb_len >= 12);
4197 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4198 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4199 unsigned long flags;
4201 /* Once we're done issuing command and kicking bmdma,
4202 * irq handler takes over. To not lose irq, we need
4203 * to clear NOINTR flag before sending cdb, but
4204 * interrupt handler shouldn't be invoked before we're
4205 * finished. Hence, the following locking.
4207 spin_lock_irqsave(&ap->host_set->lock, flags);
4208 ap->flags &= ~ATA_FLAG_NOINTR;
4209 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4210 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4211 ap->ops->bmdma_start(qc); /* initiate bmdma */
4212 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4214 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4216 /* PIO commands are handled by polling */
4217 ap->hsm_task_state = HSM_ST;
4218 ata_queue_pio_task(ap);
4224 ata_poll_qc_complete(qc);
4229 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4230 * without filling any other registers
4232 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4235 struct ata_taskfile tf;
4238 ata_tf_init(ap, &tf, dev->devno);
4241 tf.flags |= ATA_TFLAG_DEVICE;
4242 tf.protocol = ATA_PROT_NODATA;
4244 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4246 printk(KERN_ERR "%s: ata command failed: %d\n",
4252 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4256 if (!ata_try_flush_cache(dev))
4259 if (ata_id_has_flush_ext(dev->id))
4260 cmd = ATA_CMD_FLUSH_EXT;
4262 cmd = ATA_CMD_FLUSH;
4264 return ata_do_simple_cmd(ap, dev, cmd);
4267 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4269 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4272 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4274 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4278 * ata_device_resume - wakeup a previously suspended devices
4279 * @ap: port the device is connected to
4280 * @dev: the device to resume
4282 * Kick the drive back into action, by sending it an idle immediate
4283 * command and making sure its transfer mode matches between drive
4287 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4289 if (ap->flags & ATA_FLAG_SUSPENDED) {
4290 ap->flags &= ~ATA_FLAG_SUSPENDED;
4293 if (!ata_dev_present(dev))
4295 if (dev->class == ATA_DEV_ATA)
4296 ata_start_drive(ap, dev);
4302 * ata_device_suspend - prepare a device for suspend
4303 * @ap: port the device is connected to
4304 * @dev: the device to suspend
4306 * Flush the cache on the drive, if appropriate, then issue a
4307 * standbynow command.
4309 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4311 if (!ata_dev_present(dev))
4313 if (dev->class == ATA_DEV_ATA)
4314 ata_flush_cache(ap, dev);
4316 ata_standby_drive(ap, dev);
4317 ap->flags |= ATA_FLAG_SUSPENDED;
4322 * ata_port_start - Set port up for dma.
4323 * @ap: Port to initialize
4325 * Called just after data structures for each port are
4326 * initialized. Allocates space for PRD table.
4328 * May be used as the port_start() entry in ata_port_operations.
4331 * Inherited from caller.
4334 int ata_port_start (struct ata_port *ap)
4336 struct device *dev = ap->host_set->dev;
4339 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4343 rc = ata_pad_alloc(ap, dev);
4345 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4349 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4356 * ata_port_stop - Undo ata_port_start()
4357 * @ap: Port to shut down
4359 * Frees the PRD table.
4361 * May be used as the port_stop() entry in ata_port_operations.
4364 * Inherited from caller.
4367 void ata_port_stop (struct ata_port *ap)
4369 struct device *dev = ap->host_set->dev;
4371 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4372 ata_pad_free(ap, dev);
4375 void ata_host_stop (struct ata_host_set *host_set)
4377 if (host_set->mmio_base)
4378 iounmap(host_set->mmio_base);
4383 * ata_host_remove - Unregister SCSI host structure with upper layers
4384 * @ap: Port to unregister
4385 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4388 * Inherited from caller.
4391 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4393 struct Scsi_Host *sh = ap->host;
4398 scsi_remove_host(sh);
4400 ap->ops->port_stop(ap);
4404 * ata_host_init - Initialize an ata_port structure
4405 * @ap: Structure to initialize
4406 * @host: associated SCSI mid-layer structure
4407 * @host_set: Collection of hosts to which @ap belongs
4408 * @ent: Probe information provided by low-level driver
4409 * @port_no: Port number associated with this ata_port
4411 * Initialize a new ata_port structure, and its associated
4415 * Inherited from caller.
4418 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4419 struct ata_host_set *host_set,
4420 const struct ata_probe_ent *ent, unsigned int port_no)
4426 host->max_channel = 1;
4427 host->unique_id = ata_unique_id++;
4428 host->max_cmd_len = 12;
4430 ap->flags = ATA_FLAG_PORT_DISABLED;
4431 ap->id = host->unique_id;
4433 ap->ctl = ATA_DEVCTL_OBS;
4434 ap->host_set = host_set;
4435 ap->port_no = port_no;
4437 ent->legacy_mode ? ent->hard_port_no : port_no;
4438 ap->pio_mask = ent->pio_mask;
4439 ap->mwdma_mask = ent->mwdma_mask;
4440 ap->udma_mask = ent->udma_mask;
4441 ap->flags |= ent->host_flags;
4442 ap->ops = ent->port_ops;
4443 ap->cbl = ATA_CBL_NONE;
4444 ap->active_tag = ATA_TAG_POISON;
4445 ap->last_ctl = 0xFF;
4447 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4448 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4449 INIT_LIST_HEAD(&ap->eh_done_q);
4451 for (i = 0; i < ATA_MAX_DEVICES; i++)
4452 ap->device[i].devno = i;
4455 ap->stats.unhandled_irq = 1;
4456 ap->stats.idle_irq = 1;
4459 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4463 * ata_host_add - Attach low-level ATA driver to system
4464 * @ent: Information provided by low-level driver
4465 * @host_set: Collections of ports to which we add
4466 * @port_no: Port number associated with this host
4468 * Attach low-level ATA driver to system.
4471 * PCI/etc. bus probe sem.
4474 * New ata_port on success, for NULL on error.
4477 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4478 struct ata_host_set *host_set,
4479 unsigned int port_no)
4481 struct Scsi_Host *host;
4482 struct ata_port *ap;
4486 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4490 ap = (struct ata_port *) &host->hostdata[0];
4492 ata_host_init(ap, host, host_set, ent, port_no);
4494 rc = ap->ops->port_start(ap);
4501 scsi_host_put(host);
4506 * ata_device_add - Register hardware device with ATA and SCSI layers
4507 * @ent: Probe information describing hardware device to be registered
4509 * This function processes the information provided in the probe
4510 * information struct @ent, allocates the necessary ATA and SCSI
4511 * host information structures, initializes them, and registers
4512 * everything with requisite kernel subsystems.
4514 * This function requests irqs, probes the ATA bus, and probes
4518 * PCI/etc. bus probe sem.
4521 * Number of ports registered. Zero on error (no ports registered).
4524 int ata_device_add(const struct ata_probe_ent *ent)
4526 unsigned int count = 0, i;
4527 struct device *dev = ent->dev;
4528 struct ata_host_set *host_set;
4531 /* alloc a container for our list of ATA ports (buses) */
4532 host_set = kzalloc(sizeof(struct ata_host_set) +
4533 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4536 spin_lock_init(&host_set->lock);
4538 host_set->dev = dev;
4539 host_set->n_ports = ent->n_ports;
4540 host_set->irq = ent->irq;
4541 host_set->mmio_base = ent->mmio_base;
4542 host_set->private_data = ent->private_data;
4543 host_set->ops = ent->port_ops;
4545 /* register each port bound to this device */
4546 for (i = 0; i < ent->n_ports; i++) {
4547 struct ata_port *ap;
4548 unsigned long xfer_mode_mask;
4550 ap = ata_host_add(ent, host_set, i);
4554 host_set->ports[i] = ap;
4555 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4556 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4557 (ap->pio_mask << ATA_SHIFT_PIO);
4559 /* print per-port info to dmesg */
4560 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4561 "bmdma 0x%lX irq %lu\n",
4563 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4564 ata_mode_string(xfer_mode_mask),
4565 ap->ioaddr.cmd_addr,
4566 ap->ioaddr.ctl_addr,
4567 ap->ioaddr.bmdma_addr,
4571 host_set->ops->irq_clear(ap);
4578 /* obtain irq, that is shared between channels */
4579 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4580 DRV_NAME, host_set))
4583 /* perform each probe synchronously */
4584 DPRINTK("probe begin\n");
4585 for (i = 0; i < count; i++) {
4586 struct ata_port *ap;
4589 ap = host_set->ports[i];
4591 DPRINTK("ata%u: bus probe begin\n", ap->id);
4592 rc = ata_bus_probe(ap);
4593 DPRINTK("ata%u: bus probe end\n", ap->id);
4596 /* FIXME: do something useful here?
4597 * Current libata behavior will
4598 * tear down everything when
4599 * the module is removed
4600 * or the h/w is unplugged.
4604 rc = scsi_add_host(ap->host, dev);
4606 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4608 /* FIXME: do something useful here */
4609 /* FIXME: handle unconditional calls to
4610 * scsi_scan_host and ata_host_remove, below,
4616 /* probes are done, now scan each port's disk(s) */
4617 DPRINTK("host probe begin\n");
4618 for (i = 0; i < count; i++) {
4619 struct ata_port *ap = host_set->ports[i];
4621 ata_scsi_scan_host(ap);
4624 dev_set_drvdata(dev, host_set);
4626 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4627 return ent->n_ports; /* success */
4630 for (i = 0; i < count; i++) {
4631 ata_host_remove(host_set->ports[i], 1);
4632 scsi_host_put(host_set->ports[i]->host);
4636 VPRINTK("EXIT, returning 0\n");
4641 * ata_host_set_remove - PCI layer callback for device removal
4642 * @host_set: ATA host set that was removed
4644 * Unregister all objects associated with this host set. Free those
4648 * Inherited from calling layer (may sleep).
4651 void ata_host_set_remove(struct ata_host_set *host_set)
4653 struct ata_port *ap;
4656 for (i = 0; i < host_set->n_ports; i++) {
4657 ap = host_set->ports[i];
4658 scsi_remove_host(ap->host);
4661 free_irq(host_set->irq, host_set);
4663 for (i = 0; i < host_set->n_ports; i++) {
4664 ap = host_set->ports[i];
4666 ata_scsi_release(ap->host);
4668 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4669 struct ata_ioports *ioaddr = &ap->ioaddr;
4671 if (ioaddr->cmd_addr == 0x1f0)
4672 release_region(0x1f0, 8);
4673 else if (ioaddr->cmd_addr == 0x170)
4674 release_region(0x170, 8);
4677 scsi_host_put(ap->host);
4680 if (host_set->ops->host_stop)
4681 host_set->ops->host_stop(host_set);
4687 * ata_scsi_release - SCSI layer callback hook for host unload
4688 * @host: libata host to be unloaded
4690 * Performs all duties necessary to shut down a libata port...
4691 * Kill port kthread, disable port, and release resources.
4694 * Inherited from SCSI layer.
4700 int ata_scsi_release(struct Scsi_Host *host)
4702 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4706 ap->ops->port_disable(ap);
4707 ata_host_remove(ap, 0);
4714 * ata_std_ports - initialize ioaddr with standard port offsets.
4715 * @ioaddr: IO address structure to be initialized
4717 * Utility function which initializes data_addr, error_addr,
4718 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4719 * device_addr, status_addr, and command_addr to standard offsets
4720 * relative to cmd_addr.
4722 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4725 void ata_std_ports(struct ata_ioports *ioaddr)
4727 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4728 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4729 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4730 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4731 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4732 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4733 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4734 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4735 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4736 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4742 void ata_pci_host_stop (struct ata_host_set *host_set)
4744 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4746 pci_iounmap(pdev, host_set->mmio_base);
4750 * ata_pci_remove_one - PCI layer callback for device removal
4751 * @pdev: PCI device that was removed
4753 * PCI layer indicates to libata via this hook that
4754 * hot-unplug or module unload event has occurred.
4755 * Handle this by unregistering all objects associated
4756 * with this PCI device. Free those objects. Then finally
4757 * release PCI resources and disable device.
4760 * Inherited from PCI layer (may sleep).
4763 void ata_pci_remove_one (struct pci_dev *pdev)
4765 struct device *dev = pci_dev_to_dev(pdev);
4766 struct ata_host_set *host_set = dev_get_drvdata(dev);
4768 ata_host_set_remove(host_set);
4769 pci_release_regions(pdev);
4770 pci_disable_device(pdev);
4771 dev_set_drvdata(dev, NULL);
4774 /* move to PCI subsystem */
4775 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4777 unsigned long tmp = 0;
4779 switch (bits->width) {
4782 pci_read_config_byte(pdev, bits->reg, &tmp8);
4788 pci_read_config_word(pdev, bits->reg, &tmp16);
4794 pci_read_config_dword(pdev, bits->reg, &tmp32);
4805 return (tmp == bits->val) ? 1 : 0;
4808 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4810 pci_save_state(pdev);
4811 pci_disable_device(pdev);
4812 pci_set_power_state(pdev, PCI_D3hot);
4816 int ata_pci_device_resume(struct pci_dev *pdev)
4818 pci_set_power_state(pdev, PCI_D0);
4819 pci_restore_state(pdev);
4820 pci_enable_device(pdev);
4821 pci_set_master(pdev);
4824 #endif /* CONFIG_PCI */
4827 static int __init ata_init(void)
4829 ata_wq = create_workqueue("ata");
4833 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4837 static void __exit ata_exit(void)
4839 destroy_workqueue(ata_wq);
4842 module_init(ata_init);
4843 module_exit(ata_exit);
4845 static unsigned long ratelimit_time;
4846 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4848 int ata_ratelimit(void)
4851 unsigned long flags;
4853 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4855 if (time_after(jiffies, ratelimit_time)) {
4857 ratelimit_time = jiffies + (HZ/5);
4861 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4867 * libata is essentially a library of internal helper functions for
4868 * low-level ATA host controller drivers. As such, the API/ABI is
4869 * likely to change as new drivers are added and updated.
4870 * Do not depend on ABI/API stability.
4873 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4874 EXPORT_SYMBOL_GPL(ata_std_ports);
4875 EXPORT_SYMBOL_GPL(ata_device_add);
4876 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4877 EXPORT_SYMBOL_GPL(ata_sg_init);
4878 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4879 EXPORT_SYMBOL_GPL(ata_qc_complete);
4880 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4881 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4882 EXPORT_SYMBOL_GPL(ata_tf_load);
4883 EXPORT_SYMBOL_GPL(ata_tf_read);
4884 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4885 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4886 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4887 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4888 EXPORT_SYMBOL_GPL(ata_check_status);
4889 EXPORT_SYMBOL_GPL(ata_altstatus);
4890 EXPORT_SYMBOL_GPL(ata_exec_command);
4891 EXPORT_SYMBOL_GPL(ata_port_start);
4892 EXPORT_SYMBOL_GPL(ata_port_stop);
4893 EXPORT_SYMBOL_GPL(ata_host_stop);
4894 EXPORT_SYMBOL_GPL(ata_interrupt);
4895 EXPORT_SYMBOL_GPL(ata_qc_prep);
4896 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4897 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4898 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4899 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4900 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4901 EXPORT_SYMBOL_GPL(ata_port_probe);
4902 EXPORT_SYMBOL_GPL(sata_phy_reset);
4903 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4904 EXPORT_SYMBOL_GPL(ata_bus_reset);
4905 EXPORT_SYMBOL_GPL(ata_std_probeinit);
4906 EXPORT_SYMBOL_GPL(ata_std_softreset);
4907 EXPORT_SYMBOL_GPL(sata_std_hardreset);
4908 EXPORT_SYMBOL_GPL(ata_std_postreset);
4909 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
4910 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
4911 EXPORT_SYMBOL_GPL(ata_port_disable);
4912 EXPORT_SYMBOL_GPL(ata_ratelimit);
4913 EXPORT_SYMBOL_GPL(ata_busy_sleep);
4914 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4915 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4916 EXPORT_SYMBOL_GPL(ata_scsi_error);
4917 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4918 EXPORT_SYMBOL_GPL(ata_scsi_release);
4919 EXPORT_SYMBOL_GPL(ata_host_intr);
4920 EXPORT_SYMBOL_GPL(ata_dev_classify);
4921 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4922 EXPORT_SYMBOL_GPL(ata_dev_config);
4923 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4924 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4925 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
4927 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
4928 EXPORT_SYMBOL_GPL(ata_timing_compute);
4929 EXPORT_SYMBOL_GPL(ata_timing_merge);
4932 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4933 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4934 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4935 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4936 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4937 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4938 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
4939 #endif /* CONFIG_PCI */
4941 EXPORT_SYMBOL_GPL(ata_device_suspend);
4942 EXPORT_SYMBOL_GPL(ata_device_resume);
4943 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4944 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);