libata: irq-pio build fixes
[linux-2.6/zen-sources.git] / drivers / scsi / libata-core.c
blob1a00c80b96d449328ee5c436d150888ebf645b40
1 /*
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
6 * on emails.
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)
15 * any later version.
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>
41 #include <linux/mm.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>
58 #include <asm/io.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
62 #include "libata.h"
64 static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev);
66 static void ata_set_mode(struct ata_port *ap);
67 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
68 static void ata_pio_error(struct ata_port *ap);
69 static unsigned int ata_dev_xfermask(struct ata_port *ap,
70 struct ata_device *dev);
72 static unsigned int ata_unique_id = 1;
73 static struct workqueue_struct *ata_wq;
75 int atapi_enabled = 0;
76 module_param(atapi_enabled, int, 0444);
77 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
79 int libata_fua = 0;
80 module_param_named(fua, libata_fua, int, 0444);
81 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
83 MODULE_AUTHOR("Jeff Garzik");
84 MODULE_DESCRIPTION("Library module for ATA devices");
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(DRV_VERSION);
89 /**
90 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
91 * @tf: Taskfile to convert
92 * @fis: Buffer into which data will output
93 * @pmp: Port multiplier port
95 * Converts a standard ATA taskfile to a Serial ATA
96 * FIS structure (Register - Host to Device).
98 * LOCKING:
99 * Inherited from caller.
102 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
104 fis[0] = 0x27; /* Register - Host to Device FIS */
105 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
106 bit 7 indicates Command FIS */
107 fis[2] = tf->command;
108 fis[3] = tf->feature;
110 fis[4] = tf->lbal;
111 fis[5] = tf->lbam;
112 fis[6] = tf->lbah;
113 fis[7] = tf->device;
115 fis[8] = tf->hob_lbal;
116 fis[9] = tf->hob_lbam;
117 fis[10] = tf->hob_lbah;
118 fis[11] = tf->hob_feature;
120 fis[12] = tf->nsect;
121 fis[13] = tf->hob_nsect;
122 fis[14] = 0;
123 fis[15] = tf->ctl;
125 fis[16] = 0;
126 fis[17] = 0;
127 fis[18] = 0;
128 fis[19] = 0;
132 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
133 * @fis: Buffer from which data will be input
134 * @tf: Taskfile to output
136 * Converts a serial ATA FIS structure to a standard ATA taskfile.
138 * LOCKING:
139 * Inherited from caller.
142 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
144 tf->command = fis[2]; /* status */
145 tf->feature = fis[3]; /* error */
147 tf->lbal = fis[4];
148 tf->lbam = fis[5];
149 tf->lbah = fis[6];
150 tf->device = fis[7];
152 tf->hob_lbal = fis[8];
153 tf->hob_lbam = fis[9];
154 tf->hob_lbah = fis[10];
156 tf->nsect = fis[12];
157 tf->hob_nsect = fis[13];
160 static const u8 ata_rw_cmds[] = {
161 /* pio multi */
162 ATA_CMD_READ_MULTI,
163 ATA_CMD_WRITE_MULTI,
164 ATA_CMD_READ_MULTI_EXT,
165 ATA_CMD_WRITE_MULTI_EXT,
169 ATA_CMD_WRITE_MULTI_FUA_EXT,
170 /* pio */
171 ATA_CMD_PIO_READ,
172 ATA_CMD_PIO_WRITE,
173 ATA_CMD_PIO_READ_EXT,
174 ATA_CMD_PIO_WRITE_EXT,
179 /* dma */
180 ATA_CMD_READ,
181 ATA_CMD_WRITE,
182 ATA_CMD_READ_EXT,
183 ATA_CMD_WRITE_EXT,
187 ATA_CMD_WRITE_FUA_EXT
191 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
192 * @qc: command to examine and configure
194 * Examine the device configuration and tf->flags to calculate
195 * the proper read/write commands and protocol to use.
197 * LOCKING:
198 * caller.
200 int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
202 struct ata_taskfile *tf = &qc->tf;
203 struct ata_device *dev = qc->dev;
204 u8 cmd;
206 int index, fua, lba48, write;
208 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
209 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
210 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
212 if (dev->flags & ATA_DFLAG_PIO) {
213 tf->protocol = ATA_PROT_PIO;
214 index = dev->multi_count ? 0 : 8;
215 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
216 /* Unable to use DMA due to host limitation */
217 tf->protocol = ATA_PROT_PIO;
218 index = dev->multi_count ? 0 : 8;
219 } else {
220 tf->protocol = ATA_PROT_DMA;
221 index = 16;
224 cmd = ata_rw_cmds[index + fua + lba48 + write];
225 if (cmd) {
226 tf->command = cmd;
227 return 0;
229 return -1;
233 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
234 * @pio_mask: pio_mask
235 * @mwdma_mask: mwdma_mask
236 * @udma_mask: udma_mask
238 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
239 * unsigned int xfer_mask.
241 * LOCKING:
242 * None.
244 * RETURNS:
245 * Packed xfer_mask.
247 static unsigned int ata_pack_xfermask(unsigned int pio_mask,
248 unsigned int mwdma_mask,
249 unsigned int udma_mask)
251 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
252 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
253 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
256 static const struct ata_xfer_ent {
257 unsigned int shift, bits;
258 u8 base;
259 } ata_xfer_tbl[] = {
260 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
261 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
262 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
263 { -1, },
267 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
268 * @xfer_mask: xfer_mask of interest
270 * Return matching XFER_* value for @xfer_mask. Only the highest
271 * bit of @xfer_mask is considered.
273 * LOCKING:
274 * None.
276 * RETURNS:
277 * Matching XFER_* value, 0 if no match found.
279 static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
281 int highbit = fls(xfer_mask) - 1;
282 const struct ata_xfer_ent *ent;
284 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
285 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
286 return ent->base + highbit - ent->shift;
287 return 0;
291 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
292 * @xfer_mode: XFER_* of interest
294 * Return matching xfer_mask for @xfer_mode.
296 * LOCKING:
297 * None.
299 * RETURNS:
300 * Matching xfer_mask, 0 if no match found.
302 static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
304 const struct ata_xfer_ent *ent;
306 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
307 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
308 return 1 << (ent->shift + xfer_mode - ent->base);
309 return 0;
313 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
314 * @xfer_mode: XFER_* of interest
316 * Return matching xfer_shift for @xfer_mode.
318 * LOCKING:
319 * None.
321 * RETURNS:
322 * Matching xfer_shift, -1 if no match found.
324 static int ata_xfer_mode2shift(unsigned int xfer_mode)
326 const struct ata_xfer_ent *ent;
328 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
329 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
330 return ent->shift;
331 return -1;
335 * ata_mode_string - convert xfer_mask to string
336 * @xfer_mask: mask of bits supported; only highest bit counts.
338 * Determine string which represents the highest speed
339 * (highest bit in @modemask).
341 * LOCKING:
342 * None.
344 * RETURNS:
345 * Constant C string representing highest speed listed in
346 * @mode_mask, or the constant C string "<n/a>".
348 static const char *ata_mode_string(unsigned int xfer_mask)
350 static const char * const xfer_mode_str[] = {
351 "PIO0",
352 "PIO1",
353 "PIO2",
354 "PIO3",
355 "PIO4",
356 "MWDMA0",
357 "MWDMA1",
358 "MWDMA2",
359 "UDMA/16",
360 "UDMA/25",
361 "UDMA/33",
362 "UDMA/44",
363 "UDMA/66",
364 "UDMA/100",
365 "UDMA/133",
366 "UDMA7",
368 int highbit;
370 highbit = fls(xfer_mask) - 1;
371 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
372 return xfer_mode_str[highbit];
373 return "<n/a>";
377 * ata_pio_devchk - PATA device presence detection
378 * @ap: ATA channel to examine
379 * @device: Device to examine (starting at zero)
381 * This technique was originally described in
382 * Hale Landis's ATADRVR (www.ata-atapi.com), and
383 * later found its way into the ATA/ATAPI spec.
385 * Write a pattern to the ATA shadow registers,
386 * and if a device is present, it will respond by
387 * correctly storing and echoing back the
388 * ATA shadow register contents.
390 * LOCKING:
391 * caller.
394 static unsigned int ata_pio_devchk(struct ata_port *ap,
395 unsigned int device)
397 struct ata_ioports *ioaddr = &ap->ioaddr;
398 u8 nsect, lbal;
400 ap->ops->dev_select(ap, device);
402 outb(0x55, ioaddr->nsect_addr);
403 outb(0xaa, ioaddr->lbal_addr);
405 outb(0xaa, ioaddr->nsect_addr);
406 outb(0x55, ioaddr->lbal_addr);
408 outb(0x55, ioaddr->nsect_addr);
409 outb(0xaa, ioaddr->lbal_addr);
411 nsect = inb(ioaddr->nsect_addr);
412 lbal = inb(ioaddr->lbal_addr);
414 if ((nsect == 0x55) && (lbal == 0xaa))
415 return 1; /* we found a device */
417 return 0; /* nothing found */
421 * ata_mmio_devchk - PATA device presence detection
422 * @ap: ATA channel to examine
423 * @device: Device to examine (starting at zero)
425 * This technique was originally described in
426 * Hale Landis's ATADRVR (www.ata-atapi.com), and
427 * later found its way into the ATA/ATAPI spec.
429 * Write a pattern to the ATA shadow registers,
430 * and if a device is present, it will respond by
431 * correctly storing and echoing back the
432 * ATA shadow register contents.
434 * LOCKING:
435 * caller.
438 static unsigned int ata_mmio_devchk(struct ata_port *ap,
439 unsigned int device)
441 struct ata_ioports *ioaddr = &ap->ioaddr;
442 u8 nsect, lbal;
444 ap->ops->dev_select(ap, device);
446 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
447 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
449 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
450 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
452 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
453 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
455 nsect = readb((void __iomem *) ioaddr->nsect_addr);
456 lbal = readb((void __iomem *) ioaddr->lbal_addr);
458 if ((nsect == 0x55) && (lbal == 0xaa))
459 return 1; /* we found a device */
461 return 0; /* nothing found */
465 * ata_devchk - PATA device presence detection
466 * @ap: ATA channel to examine
467 * @device: Device to examine (starting at zero)
469 * Dispatch ATA device presence detection, depending
470 * on whether we are using PIO or MMIO to talk to the
471 * ATA shadow registers.
473 * LOCKING:
474 * caller.
477 static unsigned int ata_devchk(struct ata_port *ap,
478 unsigned int device)
480 if (ap->flags & ATA_FLAG_MMIO)
481 return ata_mmio_devchk(ap, device);
482 return ata_pio_devchk(ap, device);
486 * ata_dev_classify - determine device type based on ATA-spec signature
487 * @tf: ATA taskfile register set for device to be identified
489 * Determine from taskfile register contents whether a device is
490 * ATA or ATAPI, as per "Signature and persistence" section
491 * of ATA/PI spec (volume 1, sect 5.14).
493 * LOCKING:
494 * None.
496 * RETURNS:
497 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
498 * the event of failure.
501 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
503 /* Apple's open source Darwin code hints that some devices only
504 * put a proper signature into the LBA mid/high registers,
505 * So, we only check those. It's sufficient for uniqueness.
508 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
509 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
510 DPRINTK("found ATA device by sig\n");
511 return ATA_DEV_ATA;
514 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
515 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
516 DPRINTK("found ATAPI device by sig\n");
517 return ATA_DEV_ATAPI;
520 DPRINTK("unknown device\n");
521 return ATA_DEV_UNKNOWN;
525 * ata_dev_try_classify - Parse returned ATA device signature
526 * @ap: ATA channel to examine
527 * @device: Device to examine (starting at zero)
528 * @r_err: Value of error register on completion
530 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
531 * an ATA/ATAPI-defined set of values is placed in the ATA
532 * shadow registers, indicating the results of device detection
533 * and diagnostics.
535 * Select the ATA device, and read the values from the ATA shadow
536 * registers. Then parse according to the Error register value,
537 * and the spec-defined values examined by ata_dev_classify().
539 * LOCKING:
540 * caller.
542 * RETURNS:
543 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
546 static unsigned int
547 ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
549 struct ata_taskfile tf;
550 unsigned int class;
551 u8 err;
553 ap->ops->dev_select(ap, device);
555 memset(&tf, 0, sizeof(tf));
557 ap->ops->tf_read(ap, &tf);
558 err = tf.feature;
559 if (r_err)
560 *r_err = err;
562 /* see if device passed diags */
563 if (err == 1)
564 /* do nothing */ ;
565 else if ((device == 0) && (err == 0x81))
566 /* do nothing */ ;
567 else
568 return ATA_DEV_NONE;
570 /* determine if device is ATA or ATAPI */
571 class = ata_dev_classify(&tf);
573 if (class == ATA_DEV_UNKNOWN)
574 return ATA_DEV_NONE;
575 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
576 return ATA_DEV_NONE;
577 return class;
581 * ata_id_string - Convert IDENTIFY DEVICE page into string
582 * @id: IDENTIFY DEVICE results we will examine
583 * @s: string into which data is output
584 * @ofs: offset into identify device page
585 * @len: length of string to return. must be an even number.
587 * The strings in the IDENTIFY DEVICE page are broken up into
588 * 16-bit chunks. Run through the string, and output each
589 * 8-bit chunk linearly, regardless of platform.
591 * LOCKING:
592 * caller.
595 void ata_id_string(const u16 *id, unsigned char *s,
596 unsigned int ofs, unsigned int len)
598 unsigned int c;
600 while (len > 0) {
601 c = id[ofs] >> 8;
602 *s = c;
603 s++;
605 c = id[ofs] & 0xff;
606 *s = c;
607 s++;
609 ofs++;
610 len -= 2;
615 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
616 * @id: IDENTIFY DEVICE results we will examine
617 * @s: string into which data is output
618 * @ofs: offset into identify device page
619 * @len: length of string to return. must be an odd number.
621 * This function is identical to ata_id_string except that it
622 * trims trailing spaces and terminates the resulting string with
623 * null. @len must be actual maximum length (even number) + 1.
625 * LOCKING:
626 * caller.
628 void ata_id_c_string(const u16 *id, unsigned char *s,
629 unsigned int ofs, unsigned int len)
631 unsigned char *p;
633 WARN_ON(!(len & 1));
635 ata_id_string(id, s, ofs, len - 1);
637 p = s + strnlen(s, len - 1);
638 while (p > s && p[-1] == ' ')
639 p--;
640 *p = '\0';
643 static u64 ata_id_n_sectors(const u16 *id)
645 if (ata_id_has_lba(id)) {
646 if (ata_id_has_lba48(id))
647 return ata_id_u64(id, 100);
648 else
649 return ata_id_u32(id, 60);
650 } else {
651 if (ata_id_current_chs_valid(id))
652 return ata_id_u32(id, 57);
653 else
654 return id[1] * id[3] * id[6];
659 * ata_noop_dev_select - Select device 0/1 on ATA bus
660 * @ap: ATA channel to manipulate
661 * @device: ATA device (numbered from zero) to select
663 * This function performs no actual function.
665 * May be used as the dev_select() entry in ata_port_operations.
667 * LOCKING:
668 * caller.
670 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
676 * ata_std_dev_select - Select device 0/1 on ATA bus
677 * @ap: ATA channel to manipulate
678 * @device: ATA device (numbered from zero) to select
680 * Use the method defined in the ATA specification to
681 * make either device 0, or device 1, active on the
682 * ATA channel. Works with both PIO and MMIO.
684 * May be used as the dev_select() entry in ata_port_operations.
686 * LOCKING:
687 * caller.
690 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
692 u8 tmp;
694 if (device == 0)
695 tmp = ATA_DEVICE_OBS;
696 else
697 tmp = ATA_DEVICE_OBS | ATA_DEV1;
699 if (ap->flags & ATA_FLAG_MMIO) {
700 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
701 } else {
702 outb(tmp, ap->ioaddr.device_addr);
704 ata_pause(ap); /* needed; also flushes, for mmio */
708 * ata_dev_select - Select device 0/1 on ATA bus
709 * @ap: ATA channel to manipulate
710 * @device: ATA device (numbered from zero) to select
711 * @wait: non-zero to wait for Status register BSY bit to clear
712 * @can_sleep: non-zero if context allows sleeping
714 * Use the method defined in the ATA specification to
715 * make either device 0, or device 1, active on the
716 * ATA channel.
718 * This is a high-level version of ata_std_dev_select(),
719 * which additionally provides the services of inserting
720 * the proper pauses and status polling, where needed.
722 * LOCKING:
723 * caller.
726 void ata_dev_select(struct ata_port *ap, unsigned int device,
727 unsigned int wait, unsigned int can_sleep)
729 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
730 ap->id, device, wait);
732 if (wait)
733 ata_wait_idle(ap);
735 ap->ops->dev_select(ap, device);
737 if (wait) {
738 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
739 msleep(150);
740 ata_wait_idle(ap);
745 * ata_dump_id - IDENTIFY DEVICE info debugging output
746 * @id: IDENTIFY DEVICE page to dump
748 * Dump selected 16-bit words from the given IDENTIFY DEVICE
749 * page.
751 * LOCKING:
752 * caller.
755 static inline void ata_dump_id(const u16 *id)
757 DPRINTK("49==0x%04x "
758 "53==0x%04x "
759 "63==0x%04x "
760 "64==0x%04x "
761 "75==0x%04x \n",
762 id[49],
763 id[53],
764 id[63],
765 id[64],
766 id[75]);
767 DPRINTK("80==0x%04x "
768 "81==0x%04x "
769 "82==0x%04x "
770 "83==0x%04x "
771 "84==0x%04x \n",
772 id[80],
773 id[81],
774 id[82],
775 id[83],
776 id[84]);
777 DPRINTK("88==0x%04x "
778 "93==0x%04x\n",
779 id[88],
780 id[93]);
784 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
785 * @id: IDENTIFY data to compute xfer mask from
787 * Compute the xfermask for this device. This is not as trivial
788 * as it seems if we must consider early devices correctly.
790 * FIXME: pre IDE drive timing (do we care ?).
792 * LOCKING:
793 * None.
795 * RETURNS:
796 * Computed xfermask
798 static unsigned int ata_id_xfermask(const u16 *id)
800 unsigned int pio_mask, mwdma_mask, udma_mask;
802 /* Usual case. Word 53 indicates word 64 is valid */
803 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
804 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
805 pio_mask <<= 3;
806 pio_mask |= 0x7;
807 } else {
808 /* If word 64 isn't valid then Word 51 high byte holds
809 * the PIO timing number for the maximum. Turn it into
810 * a mask.
812 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
814 /* But wait.. there's more. Design your standards by
815 * committee and you too can get a free iordy field to
816 * process. However its the speeds not the modes that
817 * are supported... Note drivers using the timing API
818 * will get this right anyway
822 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
823 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
825 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
829 * ata_port_queue_task - Queue port_task
830 * @ap: The ata_port to queue port_task for
832 * Schedule @fn(@data) for execution after @delay jiffies using
833 * port_task. There is one port_task per port and it's the
834 * user(low level driver)'s responsibility to make sure that only
835 * one task is active at any given time.
837 * libata core layer takes care of synchronization between
838 * port_task and EH. ata_port_queue_task() may be ignored for EH
839 * synchronization.
841 * LOCKING:
842 * Inherited from caller.
844 void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
845 unsigned long delay)
847 int rc;
849 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
850 return;
852 PREPARE_WORK(&ap->port_task, fn, data);
854 if (!delay)
855 rc = queue_work(ata_wq, &ap->port_task);
856 else
857 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
859 /* rc == 0 means that another user is using port task */
860 WARN_ON(rc == 0);
864 * ata_port_flush_task - Flush port_task
865 * @ap: The ata_port to flush port_task for
867 * After this function completes, port_task is guranteed not to
868 * be running or scheduled.
870 * LOCKING:
871 * Kernel thread context (may sleep)
873 void ata_port_flush_task(struct ata_port *ap)
875 unsigned long flags;
877 DPRINTK("ENTER\n");
879 spin_lock_irqsave(&ap->host_set->lock, flags);
880 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
881 spin_unlock_irqrestore(&ap->host_set->lock, flags);
883 DPRINTK("flush #1\n");
884 flush_workqueue(ata_wq);
887 * At this point, if a task is running, it's guaranteed to see
888 * the FLUSH flag; thus, it will never queue pio tasks again.
889 * Cancel and flush.
891 if (!cancel_delayed_work(&ap->port_task)) {
892 DPRINTK("flush #2\n");
893 flush_workqueue(ata_wq);
896 spin_lock_irqsave(&ap->host_set->lock, flags);
897 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
898 spin_unlock_irqrestore(&ap->host_set->lock, flags);
900 DPRINTK("EXIT\n");
903 void ata_qc_complete_internal(struct ata_queued_cmd *qc)
905 struct completion *waiting = qc->private_data;
907 qc->ap->ops->tf_read(qc->ap, &qc->tf);
908 complete(waiting);
912 * ata_exec_internal - execute libata internal command
913 * @ap: Port to which the command is sent
914 * @dev: Device to which the command is sent
915 * @tf: Taskfile registers for the command and the result
916 * @dma_dir: Data tranfer direction of the command
917 * @buf: Data buffer of the command
918 * @buflen: Length of data buffer
920 * Executes libata internal command with timeout. @tf contains
921 * command on entry and result on return. Timeout and error
922 * conditions are reported via return value. No recovery action
923 * is taken after a command times out. It's caller's duty to
924 * clean up after timeout.
926 * LOCKING:
927 * None. Should be called with kernel context, might sleep.
930 static unsigned
931 ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
932 struct ata_taskfile *tf,
933 int dma_dir, void *buf, unsigned int buflen)
935 u8 command = tf->command;
936 struct ata_queued_cmd *qc;
937 DECLARE_COMPLETION(wait);
938 unsigned long flags;
939 unsigned int err_mask;
941 spin_lock_irqsave(&ap->host_set->lock, flags);
943 qc = ata_qc_new_init(ap, dev);
944 BUG_ON(qc == NULL);
946 qc->tf = *tf;
947 qc->dma_dir = dma_dir;
948 if (dma_dir != DMA_NONE) {
949 ata_sg_init_one(qc, buf, buflen);
950 qc->nsect = buflen / ATA_SECT_SIZE;
953 qc->private_data = &wait;
954 qc->complete_fn = ata_qc_complete_internal;
956 qc->err_mask = ata_qc_issue(qc);
957 if (qc->err_mask)
958 ata_qc_complete(qc);
960 spin_unlock_irqrestore(&ap->host_set->lock, flags);
962 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
963 spin_lock_irqsave(&ap->host_set->lock, flags);
965 /* We're racing with irq here. If we lose, the
966 * following test prevents us from completing the qc
967 * again. If completion irq occurs after here but
968 * before the caller cleans up, it will result in a
969 * spurious interrupt. We can live with that.
971 if (qc->flags & ATA_QCFLAG_ACTIVE) {
972 qc->err_mask = AC_ERR_TIMEOUT;
973 ata_qc_complete(qc);
974 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
975 ap->id, command);
978 spin_unlock_irqrestore(&ap->host_set->lock, flags);
981 *tf = qc->tf;
982 err_mask = qc->err_mask;
984 ata_qc_free(qc);
986 return err_mask;
990 * ata_pio_need_iordy - check if iordy needed
991 * @adev: ATA device
993 * Check if the current speed of the device requires IORDY. Used
994 * by various controllers for chip configuration.
997 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
999 int pio;
1000 int speed = adev->pio_mode - XFER_PIO_0;
1002 if (speed < 2)
1003 return 0;
1004 if (speed > 2)
1005 return 1;
1007 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1009 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1010 pio = adev->id[ATA_ID_EIDE_PIO];
1011 /* Is the speed faster than the drive allows non IORDY ? */
1012 if (pio) {
1013 /* This is cycle times not frequency - watch the logic! */
1014 if (pio > 240) /* PIO2 is 240nS per cycle */
1015 return 1;
1016 return 0;
1019 return 0;
1023 * ata_dev_read_id - Read ID data from the specified device
1024 * @ap: port on which target device resides
1025 * @dev: target device
1026 * @p_class: pointer to class of the target device (may be changed)
1027 * @post_reset: is this read ID post-reset?
1028 * @p_id: read IDENTIFY page (newly allocated)
1030 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1031 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1032 * devices. This function also takes care of EDD signature
1033 * misreporting (to be removed once EDD support is gone) and
1034 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
1036 * LOCKING:
1037 * Kernel thread context (may sleep)
1039 * RETURNS:
1040 * 0 on success, -errno otherwise.
1042 static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
1043 unsigned int *p_class, int post_reset, u16 **p_id)
1045 unsigned int class = *p_class;
1046 unsigned int using_edd;
1047 struct ata_taskfile tf;
1048 unsigned int err_mask = 0;
1049 u16 *id;
1050 const char *reason;
1051 int rc;
1053 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1055 if (ap->ops->probe_reset ||
1056 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1057 using_edd = 0;
1058 else
1059 using_edd = 1;
1061 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1063 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1064 if (id == NULL) {
1065 rc = -ENOMEM;
1066 reason = "out of memory";
1067 goto err_out;
1070 retry:
1071 ata_tf_init(ap, &tf, dev->devno);
1073 switch (class) {
1074 case ATA_DEV_ATA:
1075 tf.command = ATA_CMD_ID_ATA;
1076 break;
1077 case ATA_DEV_ATAPI:
1078 tf.command = ATA_CMD_ID_ATAPI;
1079 break;
1080 default:
1081 rc = -ENODEV;
1082 reason = "unsupported class";
1083 goto err_out;
1086 tf.protocol = ATA_PROT_PIO;
1088 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1089 id, sizeof(id[0]) * ATA_ID_WORDS);
1091 if (err_mask) {
1092 rc = -EIO;
1093 reason = "I/O error";
1095 if (err_mask & ~AC_ERR_DEV)
1096 goto err_out;
1099 * arg! EDD works for all test cases, but seems to return
1100 * the ATA signature for some ATAPI devices. Until the
1101 * reason for this is found and fixed, we fix up the mess
1102 * here. If IDENTIFY DEVICE returns command aborted
1103 * (as ATAPI devices do), then we issue an
1104 * IDENTIFY PACKET DEVICE.
1106 * ATA software reset (SRST, the default) does not appear
1107 * to have this problem.
1109 if ((using_edd) && (class == ATA_DEV_ATA)) {
1110 u8 err = tf.feature;
1111 if (err & ATA_ABORTED) {
1112 class = ATA_DEV_ATAPI;
1113 goto retry;
1116 goto err_out;
1119 swap_buf_le16(id, ATA_ID_WORDS);
1121 /* print device capabilities */
1122 printk(KERN_DEBUG "ata%u: dev %u cfg "
1123 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1124 ap->id, dev->devno,
1125 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1127 /* sanity check */
1128 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1129 rc = -EINVAL;
1130 reason = "device reports illegal type";
1131 goto err_out;
1134 if (post_reset && class == ATA_DEV_ATA) {
1136 * The exact sequence expected by certain pre-ATA4 drives is:
1137 * SRST RESET
1138 * IDENTIFY
1139 * INITIALIZE DEVICE PARAMETERS
1140 * anything else..
1141 * Some drives were very specific about that exact sequence.
1143 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1144 err_mask = ata_dev_init_params(ap, dev);
1145 if (err_mask) {
1146 rc = -EIO;
1147 reason = "INIT_DEV_PARAMS failed";
1148 goto err_out;
1151 /* current CHS translation info (id[53-58]) might be
1152 * changed. reread the identify device info.
1154 post_reset = 0;
1155 goto retry;
1159 *p_class = class;
1160 *p_id = id;
1161 return 0;
1163 err_out:
1164 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1165 ap->id, dev->devno, reason);
1166 kfree(id);
1167 return rc;
1170 static inline u8 ata_dev_knobble(const struct ata_port *ap,
1171 struct ata_device *dev)
1173 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1177 * ata_dev_configure - Configure the specified ATA/ATAPI device
1178 * @ap: Port on which target device resides
1179 * @dev: Target device to configure
1180 * @print_info: Enable device info printout
1182 * Configure @dev according to @dev->id. Generic and low-level
1183 * driver specific fixups are also applied.
1185 * LOCKING:
1186 * Kernel thread context (may sleep)
1188 * RETURNS:
1189 * 0 on success, -errno otherwise
1191 static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1192 int print_info)
1194 unsigned int xfer_mask;
1195 int i, rc;
1197 if (!ata_dev_present(dev)) {
1198 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1199 ap->id, dev->devno);
1200 return 0;
1203 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1205 /* initialize to-be-configured parameters */
1206 dev->flags = 0;
1207 dev->max_sectors = 0;
1208 dev->cdb_len = 0;
1209 dev->n_sectors = 0;
1210 dev->cylinders = 0;
1211 dev->heads = 0;
1212 dev->sectors = 0;
1215 * common ATA, ATAPI feature tests
1218 /* we require DMA support (bits 8 of word 49) */
1219 if (!ata_id_has_dma(dev->id)) {
1220 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1221 rc = -EINVAL;
1222 goto err_out_nosup;
1225 /* find max transfer mode; for printk only */
1226 xfer_mask = ata_id_xfermask(dev->id);
1228 ata_dump_id(dev->id);
1230 /* ATA-specific feature tests */
1231 if (dev->class == ATA_DEV_ATA) {
1232 dev->n_sectors = ata_id_n_sectors(dev->id);
1234 if (ata_id_has_lba(dev->id)) {
1235 const char *lba_desc;
1237 lba_desc = "LBA";
1238 dev->flags |= ATA_DFLAG_LBA;
1239 if (ata_id_has_lba48(dev->id)) {
1240 dev->flags |= ATA_DFLAG_LBA48;
1241 lba_desc = "LBA48";
1244 /* print device info to dmesg */
1245 if (print_info)
1246 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1247 "max %s, %Lu sectors: %s\n",
1248 ap->id, dev->devno,
1249 ata_id_major_version(dev->id),
1250 ata_mode_string(xfer_mask),
1251 (unsigned long long)dev->n_sectors,
1252 lba_desc);
1253 } else {
1254 /* CHS */
1256 /* Default translation */
1257 dev->cylinders = dev->id[1];
1258 dev->heads = dev->id[3];
1259 dev->sectors = dev->id[6];
1261 if (ata_id_current_chs_valid(dev->id)) {
1262 /* Current CHS translation is valid. */
1263 dev->cylinders = dev->id[54];
1264 dev->heads = dev->id[55];
1265 dev->sectors = dev->id[56];
1268 /* print device info to dmesg */
1269 if (print_info)
1270 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1271 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1272 ap->id, dev->devno,
1273 ata_id_major_version(dev->id),
1274 ata_mode_string(xfer_mask),
1275 (unsigned long long)dev->n_sectors,
1276 dev->cylinders, dev->heads, dev->sectors);
1279 if (dev->id[59] & 0x100) {
1280 dev->multi_count = dev->id[59] & 0xff;
1281 DPRINTK("ata%u: dev %u multi count %u\n",
1282 ap->id, device, dev->multi_count);
1287 /* ATAPI-specific feature tests */
1288 else if (dev->class == ATA_DEV_ATAPI) {
1289 rc = atapi_cdb_len(dev->id);
1290 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1291 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1292 rc = -EINVAL;
1293 goto err_out_nosup;
1295 dev->cdb_len = (unsigned int) rc;
1297 if (ata_id_cdb_intr(dev->id))
1298 dev->flags |= ATA_DFLAG_CDB_INTR;
1300 /* print device info to dmesg */
1301 if (print_info)
1302 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1303 ap->id, dev->devno, ata_mode_string(xfer_mask));
1306 ap->host->max_cmd_len = 0;
1307 for (i = 0; i < ATA_MAX_DEVICES; i++)
1308 ap->host->max_cmd_len = max_t(unsigned int,
1309 ap->host->max_cmd_len,
1310 ap->device[i].cdb_len);
1312 /* limit bridge transfers to udma5, 200 sectors */
1313 if (ata_dev_knobble(ap, dev)) {
1314 if (print_info)
1315 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1316 ap->id, dev->devno);
1317 ap->udma_mask &= ATA_UDMA5;
1318 dev->max_sectors = ATA_MAX_SECTORS;
1321 if (ap->ops->dev_config)
1322 ap->ops->dev_config(ap, dev);
1324 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1325 return 0;
1327 err_out_nosup:
1328 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1329 ap->id, dev->devno);
1330 DPRINTK("EXIT, err\n");
1331 return rc;
1335 * ata_bus_probe - Reset and probe ATA bus
1336 * @ap: Bus to probe
1338 * Master ATA bus probing function. Initiates a hardware-dependent
1339 * bus reset, then attempts to identify any devices found on
1340 * the bus.
1342 * LOCKING:
1343 * PCI/etc. bus probe sem.
1345 * RETURNS:
1346 * Zero on success, non-zero on error.
1349 static int ata_bus_probe(struct ata_port *ap)
1351 unsigned int classes[ATA_MAX_DEVICES];
1352 unsigned int i, rc, found = 0;
1354 ata_port_probe(ap);
1356 /* reset */
1357 if (ap->ops->probe_reset) {
1358 for (i = 0; i < ATA_MAX_DEVICES; i++)
1359 classes[i] = ATA_DEV_UNKNOWN;
1361 rc = ap->ops->probe_reset(ap, classes);
1362 if (rc) {
1363 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1364 return rc;
1367 for (i = 0; i < ATA_MAX_DEVICES; i++)
1368 if (classes[i] == ATA_DEV_UNKNOWN)
1369 classes[i] = ATA_DEV_NONE;
1370 } else {
1371 ap->ops->phy_reset(ap);
1373 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1374 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1375 classes[i] = ap->device[i].class;
1376 else
1377 ap->device[i].class = ATA_DEV_UNKNOWN;
1379 ata_port_probe(ap);
1382 /* read IDENTIFY page and configure devices */
1383 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1384 struct ata_device *dev = &ap->device[i];
1386 dev->class = classes[i];
1388 if (!ata_dev_present(dev))
1389 continue;
1391 WARN_ON(dev->id != NULL);
1392 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1393 dev->class = ATA_DEV_NONE;
1394 continue;
1397 if (ata_dev_configure(ap, dev, 1)) {
1398 dev->class++; /* disable device */
1399 continue;
1402 found = 1;
1405 if (!found)
1406 goto err_out_disable;
1408 ata_set_mode(ap);
1409 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1410 goto err_out_disable;
1412 return 0;
1414 err_out_disable:
1415 ap->ops->port_disable(ap);
1416 return -1;
1420 * ata_port_probe - Mark port as enabled
1421 * @ap: Port for which we indicate enablement
1423 * Modify @ap data structure such that the system
1424 * thinks that the entire port is enabled.
1426 * LOCKING: host_set lock, or some other form of
1427 * serialization.
1430 void ata_port_probe(struct ata_port *ap)
1432 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1436 * sata_print_link_status - Print SATA link status
1437 * @ap: SATA port to printk link status about
1439 * This function prints link speed and status of a SATA link.
1441 * LOCKING:
1442 * None.
1444 static void sata_print_link_status(struct ata_port *ap)
1446 u32 sstatus, tmp;
1447 const char *speed;
1449 if (!ap->ops->scr_read)
1450 return;
1452 sstatus = scr_read(ap, SCR_STATUS);
1454 if (sata_dev_present(ap)) {
1455 tmp = (sstatus >> 4) & 0xf;
1456 if (tmp & (1 << 0))
1457 speed = "1.5";
1458 else if (tmp & (1 << 1))
1459 speed = "3.0";
1460 else
1461 speed = "<unknown>";
1462 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1463 ap->id, speed, sstatus);
1464 } else {
1465 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1466 ap->id, sstatus);
1471 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1472 * @ap: SATA port associated with target SATA PHY.
1474 * This function issues commands to standard SATA Sxxx
1475 * PHY registers, to wake up the phy (and device), and
1476 * clear any reset condition.
1478 * LOCKING:
1479 * PCI/etc. bus probe sem.
1482 void __sata_phy_reset(struct ata_port *ap)
1484 u32 sstatus;
1485 unsigned long timeout = jiffies + (HZ * 5);
1487 if (ap->flags & ATA_FLAG_SATA_RESET) {
1488 /* issue phy wake/reset */
1489 scr_write_flush(ap, SCR_CONTROL, 0x301);
1490 /* Couldn't find anything in SATA I/II specs, but
1491 * AHCI-1.1 10.4.2 says at least 1 ms. */
1492 mdelay(1);
1494 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1496 /* wait for phy to become ready, if necessary */
1497 do {
1498 msleep(200);
1499 sstatus = scr_read(ap, SCR_STATUS);
1500 if ((sstatus & 0xf) != 1)
1501 break;
1502 } while (time_before(jiffies, timeout));
1504 /* print link status */
1505 sata_print_link_status(ap);
1507 /* TODO: phy layer with polling, timeouts, etc. */
1508 if (sata_dev_present(ap))
1509 ata_port_probe(ap);
1510 else
1511 ata_port_disable(ap);
1513 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1514 return;
1516 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1517 ata_port_disable(ap);
1518 return;
1521 ap->cbl = ATA_CBL_SATA;
1525 * sata_phy_reset - Reset SATA bus.
1526 * @ap: SATA port associated with target SATA PHY.
1528 * This function resets the SATA bus, and then probes
1529 * the bus for devices.
1531 * LOCKING:
1532 * PCI/etc. bus probe sem.
1535 void sata_phy_reset(struct ata_port *ap)
1537 __sata_phy_reset(ap);
1538 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1539 return;
1540 ata_bus_reset(ap);
1544 * ata_port_disable - Disable port.
1545 * @ap: Port to be disabled.
1547 * Modify @ap data structure such that the system
1548 * thinks that the entire port is disabled, and should
1549 * never attempt to probe or communicate with devices
1550 * on this port.
1552 * LOCKING: host_set lock, or some other form of
1553 * serialization.
1556 void ata_port_disable(struct ata_port *ap)
1558 ap->device[0].class = ATA_DEV_NONE;
1559 ap->device[1].class = ATA_DEV_NONE;
1560 ap->flags |= ATA_FLAG_PORT_DISABLED;
1564 * This mode timing computation functionality is ported over from
1565 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1568 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1569 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1570 * for PIO 5, which is a nonstandard extension and UDMA6, which
1571 * is currently supported only by Maxtor drives.
1574 static const struct ata_timing ata_timing[] = {
1576 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1577 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1578 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1579 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1581 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1582 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1583 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1585 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1587 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1588 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1589 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1591 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1592 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1593 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1595 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1596 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1597 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1599 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1600 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1601 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1603 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1605 { 0xFF }
1608 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1609 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1611 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1613 q->setup = EZ(t->setup * 1000, T);
1614 q->act8b = EZ(t->act8b * 1000, T);
1615 q->rec8b = EZ(t->rec8b * 1000, T);
1616 q->cyc8b = EZ(t->cyc8b * 1000, T);
1617 q->active = EZ(t->active * 1000, T);
1618 q->recover = EZ(t->recover * 1000, T);
1619 q->cycle = EZ(t->cycle * 1000, T);
1620 q->udma = EZ(t->udma * 1000, UT);
1623 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1624 struct ata_timing *m, unsigned int what)
1626 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1627 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1628 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1629 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1630 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1631 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1632 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1633 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1636 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1638 const struct ata_timing *t;
1640 for (t = ata_timing; t->mode != speed; t++)
1641 if (t->mode == 0xFF)
1642 return NULL;
1643 return t;
1646 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1647 struct ata_timing *t, int T, int UT)
1649 const struct ata_timing *s;
1650 struct ata_timing p;
1653 * Find the mode.
1656 if (!(s = ata_timing_find_mode(speed)))
1657 return -EINVAL;
1659 memcpy(t, s, sizeof(*s));
1662 * If the drive is an EIDE drive, it can tell us it needs extended
1663 * PIO/MW_DMA cycle timing.
1666 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1667 memset(&p, 0, sizeof(p));
1668 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1669 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1670 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1671 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1672 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1674 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1678 * Convert the timing to bus clock counts.
1681 ata_timing_quantize(t, t, T, UT);
1684 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1685 * S.M.A.R.T * and some other commands. We have to ensure that the
1686 * DMA cycle timing is slower/equal than the fastest PIO timing.
1689 if (speed > XFER_PIO_4) {
1690 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1691 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1695 * Lengthen active & recovery time so that cycle time is correct.
1698 if (t->act8b + t->rec8b < t->cyc8b) {
1699 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1700 t->rec8b = t->cyc8b - t->act8b;
1703 if (t->active + t->recover < t->cycle) {
1704 t->active += (t->cycle - (t->active + t->recover)) / 2;
1705 t->recover = t->cycle - t->active;
1708 return 0;
1711 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1713 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1714 return;
1716 if (dev->xfer_shift == ATA_SHIFT_PIO)
1717 dev->flags |= ATA_DFLAG_PIO;
1719 ata_dev_set_xfermode(ap, dev);
1721 if (ata_dev_revalidate(ap, dev, 0)) {
1722 printk(KERN_ERR "ata%u: failed to revalidate after set "
1723 "xfermode, disabled\n", ap->id);
1724 ata_port_disable(ap);
1727 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1728 dev->xfer_shift, (int)dev->xfer_mode);
1730 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1731 ap->id, dev->devno,
1732 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
1735 static int ata_host_set_pio(struct ata_port *ap)
1737 int i;
1739 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1740 struct ata_device *dev = &ap->device[i];
1742 if (!ata_dev_present(dev))
1743 continue;
1745 if (!dev->pio_mode) {
1746 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1747 return -1;
1750 dev->xfer_mode = dev->pio_mode;
1751 dev->xfer_shift = ATA_SHIFT_PIO;
1752 if (ap->ops->set_piomode)
1753 ap->ops->set_piomode(ap, dev);
1756 return 0;
1759 static void ata_host_set_dma(struct ata_port *ap)
1761 int i;
1763 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1764 struct ata_device *dev = &ap->device[i];
1766 if (!ata_dev_present(dev) || !dev->dma_mode)
1767 continue;
1769 dev->xfer_mode = dev->dma_mode;
1770 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1771 if (ap->ops->set_dmamode)
1772 ap->ops->set_dmamode(ap, dev);
1777 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1778 * @ap: port on which timings will be programmed
1780 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1782 * LOCKING:
1783 * PCI/etc. bus probe sem.
1785 static void ata_set_mode(struct ata_port *ap)
1787 int i, rc;
1789 /* step 1: calculate xfer_mask */
1790 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1791 struct ata_device *dev = &ap->device[i];
1792 unsigned int xfer_mask;
1794 if (!ata_dev_present(dev))
1795 continue;
1797 xfer_mask = ata_dev_xfermask(ap, dev);
1799 dev->pio_mode = ata_xfer_mask2mode(xfer_mask & ATA_MASK_PIO);
1800 dev->dma_mode = ata_xfer_mask2mode(xfer_mask & (ATA_MASK_MWDMA |
1801 ATA_MASK_UDMA));
1804 /* step 2: always set host PIO timings */
1805 rc = ata_host_set_pio(ap);
1806 if (rc)
1807 goto err_out;
1809 /* step 3: set host DMA timings */
1810 ata_host_set_dma(ap);
1812 /* step 4: update devices' xfer mode */
1813 for (i = 0; i < ATA_MAX_DEVICES; i++)
1814 ata_dev_set_mode(ap, &ap->device[i]);
1816 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1817 return;
1819 if (ap->ops->post_set_mode)
1820 ap->ops->post_set_mode(ap);
1822 return;
1824 err_out:
1825 ata_port_disable(ap);
1829 * ata_tf_to_host - issue ATA taskfile to host controller
1830 * @ap: port to which command is being issued
1831 * @tf: ATA taskfile register set
1833 * Issues ATA taskfile register set to ATA host controller,
1834 * with proper synchronization with interrupt handler and
1835 * other threads.
1837 * LOCKING:
1838 * spin_lock_irqsave(host_set lock)
1841 static inline void ata_tf_to_host(struct ata_port *ap,
1842 const struct ata_taskfile *tf)
1844 ap->ops->tf_load(ap, tf);
1845 ap->ops->exec_command(ap, tf);
1849 * ata_busy_sleep - sleep until BSY clears, or timeout
1850 * @ap: port containing status register to be polled
1851 * @tmout_pat: impatience timeout
1852 * @tmout: overall timeout
1854 * Sleep until ATA Status register bit BSY clears,
1855 * or a timeout occurs.
1857 * LOCKING: None.
1860 unsigned int ata_busy_sleep (struct ata_port *ap,
1861 unsigned long tmout_pat, unsigned long tmout)
1863 unsigned long timer_start, timeout;
1864 u8 status;
1866 status = ata_busy_wait(ap, ATA_BUSY, 300);
1867 timer_start = jiffies;
1868 timeout = timer_start + tmout_pat;
1869 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1870 msleep(50);
1871 status = ata_busy_wait(ap, ATA_BUSY, 3);
1874 if (status & ATA_BUSY)
1875 printk(KERN_WARNING "ata%u is slow to respond, "
1876 "please be patient\n", ap->id);
1878 timeout = timer_start + tmout;
1879 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1880 msleep(50);
1881 status = ata_chk_status(ap);
1884 if (status & ATA_BUSY) {
1885 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1886 ap->id, tmout / HZ);
1887 return 1;
1890 return 0;
1893 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1895 struct ata_ioports *ioaddr = &ap->ioaddr;
1896 unsigned int dev0 = devmask & (1 << 0);
1897 unsigned int dev1 = devmask & (1 << 1);
1898 unsigned long timeout;
1900 /* if device 0 was found in ata_devchk, wait for its
1901 * BSY bit to clear
1903 if (dev0)
1904 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1906 /* if device 1 was found in ata_devchk, wait for
1907 * register access, then wait for BSY to clear
1909 timeout = jiffies + ATA_TMOUT_BOOT;
1910 while (dev1) {
1911 u8 nsect, lbal;
1913 ap->ops->dev_select(ap, 1);
1914 if (ap->flags & ATA_FLAG_MMIO) {
1915 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1916 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1917 } else {
1918 nsect = inb(ioaddr->nsect_addr);
1919 lbal = inb(ioaddr->lbal_addr);
1921 if ((nsect == 1) && (lbal == 1))
1922 break;
1923 if (time_after(jiffies, timeout)) {
1924 dev1 = 0;
1925 break;
1927 msleep(50); /* give drive a breather */
1929 if (dev1)
1930 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1932 /* is all this really necessary? */
1933 ap->ops->dev_select(ap, 0);
1934 if (dev1)
1935 ap->ops->dev_select(ap, 1);
1936 if (dev0)
1937 ap->ops->dev_select(ap, 0);
1941 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1942 * @ap: Port to reset and probe
1944 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1945 * probe the bus. Not often used these days.
1947 * LOCKING:
1948 * PCI/etc. bus probe sem.
1949 * Obtains host_set lock.
1953 static unsigned int ata_bus_edd(struct ata_port *ap)
1955 struct ata_taskfile tf;
1956 unsigned long flags;
1958 /* set up execute-device-diag (bus reset) taskfile */
1959 /* also, take interrupts to a known state (disabled) */
1960 DPRINTK("execute-device-diag\n");
1961 ata_tf_init(ap, &tf, 0);
1962 tf.ctl |= ATA_NIEN;
1963 tf.command = ATA_CMD_EDD;
1964 tf.protocol = ATA_PROT_NODATA;
1966 /* do bus reset */
1967 spin_lock_irqsave(&ap->host_set->lock, flags);
1968 ata_tf_to_host(ap, &tf);
1969 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1971 /* spec says at least 2ms. but who knows with those
1972 * crazy ATAPI devices...
1974 msleep(150);
1976 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1979 static unsigned int ata_bus_softreset(struct ata_port *ap,
1980 unsigned int devmask)
1982 struct ata_ioports *ioaddr = &ap->ioaddr;
1984 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1986 /* software reset. causes dev0 to be selected */
1987 if (ap->flags & ATA_FLAG_MMIO) {
1988 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1989 udelay(20); /* FIXME: flush */
1990 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1991 udelay(20); /* FIXME: flush */
1992 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1993 } else {
1994 outb(ap->ctl, ioaddr->ctl_addr);
1995 udelay(10);
1996 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1997 udelay(10);
1998 outb(ap->ctl, ioaddr->ctl_addr);
2001 /* spec mandates ">= 2ms" before checking status.
2002 * We wait 150ms, because that was the magic delay used for
2003 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2004 * between when the ATA command register is written, and then
2005 * status is checked. Because waiting for "a while" before
2006 * checking status is fine, post SRST, we perform this magic
2007 * delay here as well.
2009 msleep(150);
2011 ata_bus_post_reset(ap, devmask);
2013 return 0;
2017 * ata_bus_reset - reset host port and associated ATA channel
2018 * @ap: port to reset
2020 * This is typically the first time we actually start issuing
2021 * commands to the ATA channel. We wait for BSY to clear, then
2022 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2023 * result. Determine what devices, if any, are on the channel
2024 * by looking at the device 0/1 error register. Look at the signature
2025 * stored in each device's taskfile registers, to determine if
2026 * the device is ATA or ATAPI.
2028 * LOCKING:
2029 * PCI/etc. bus probe sem.
2030 * Obtains host_set lock.
2032 * SIDE EFFECTS:
2033 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2036 void ata_bus_reset(struct ata_port *ap)
2038 struct ata_ioports *ioaddr = &ap->ioaddr;
2039 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2040 u8 err;
2041 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2043 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2045 /* determine if device 0/1 are present */
2046 if (ap->flags & ATA_FLAG_SATA_RESET)
2047 dev0 = 1;
2048 else {
2049 dev0 = ata_devchk(ap, 0);
2050 if (slave_possible)
2051 dev1 = ata_devchk(ap, 1);
2054 if (dev0)
2055 devmask |= (1 << 0);
2056 if (dev1)
2057 devmask |= (1 << 1);
2059 /* select device 0 again */
2060 ap->ops->dev_select(ap, 0);
2062 /* issue bus reset */
2063 if (ap->flags & ATA_FLAG_SRST)
2064 rc = ata_bus_softreset(ap, devmask);
2065 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2066 /* set up device control */
2067 if (ap->flags & ATA_FLAG_MMIO)
2068 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2069 else
2070 outb(ap->ctl, ioaddr->ctl_addr);
2071 rc = ata_bus_edd(ap);
2074 if (rc)
2075 goto err_out;
2078 * determine by signature whether we have ATA or ATAPI devices
2080 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
2081 if ((slave_possible) && (err != 0x81))
2082 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
2084 /* re-enable interrupts */
2085 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2086 ata_irq_on(ap);
2088 /* is double-select really necessary? */
2089 if (ap->device[1].class != ATA_DEV_NONE)
2090 ap->ops->dev_select(ap, 1);
2091 if (ap->device[0].class != ATA_DEV_NONE)
2092 ap->ops->dev_select(ap, 0);
2094 /* if no devices were detected, disable this port */
2095 if ((ap->device[0].class == ATA_DEV_NONE) &&
2096 (ap->device[1].class == ATA_DEV_NONE))
2097 goto err_out;
2099 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2100 /* set up device control for ATA_FLAG_SATA_RESET */
2101 if (ap->flags & ATA_FLAG_MMIO)
2102 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2103 else
2104 outb(ap->ctl, ioaddr->ctl_addr);
2107 DPRINTK("EXIT\n");
2108 return;
2110 err_out:
2111 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2112 ap->ops->port_disable(ap);
2114 DPRINTK("EXIT\n");
2117 static int sata_phy_resume(struct ata_port *ap)
2119 unsigned long timeout = jiffies + (HZ * 5);
2120 u32 sstatus;
2122 scr_write_flush(ap, SCR_CONTROL, 0x300);
2124 /* Wait for phy to become ready, if necessary. */
2125 do {
2126 msleep(200);
2127 sstatus = scr_read(ap, SCR_STATUS);
2128 if ((sstatus & 0xf) != 1)
2129 return 0;
2130 } while (time_before(jiffies, timeout));
2132 return -1;
2136 * ata_std_probeinit - initialize probing
2137 * @ap: port to be probed
2139 * @ap is about to be probed. Initialize it. This function is
2140 * to be used as standard callback for ata_drive_probe_reset().
2142 * NOTE!!! Do not use this function as probeinit if a low level
2143 * driver implements only hardreset. Just pass NULL as probeinit
2144 * in that case. Using this function is probably okay but doing
2145 * so makes reset sequence different from the original
2146 * ->phy_reset implementation and Jeff nervous. :-P
2148 extern void ata_std_probeinit(struct ata_port *ap)
2150 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
2151 sata_phy_resume(ap);
2152 if (sata_dev_present(ap))
2153 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2158 * ata_std_softreset - reset host port via ATA SRST
2159 * @ap: port to reset
2160 * @verbose: fail verbosely
2161 * @classes: resulting classes of attached devices
2163 * Reset host port using ATA SRST. This function is to be used
2164 * as standard callback for ata_drive_*_reset() functions.
2166 * LOCKING:
2167 * Kernel thread context (may sleep)
2169 * RETURNS:
2170 * 0 on success, -errno otherwise.
2172 int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2174 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2175 unsigned int devmask = 0, err_mask;
2176 u8 err;
2178 DPRINTK("ENTER\n");
2180 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2181 classes[0] = ATA_DEV_NONE;
2182 goto out;
2185 /* determine if device 0/1 are present */
2186 if (ata_devchk(ap, 0))
2187 devmask |= (1 << 0);
2188 if (slave_possible && ata_devchk(ap, 1))
2189 devmask |= (1 << 1);
2191 /* select device 0 again */
2192 ap->ops->dev_select(ap, 0);
2194 /* issue bus reset */
2195 DPRINTK("about to softreset, devmask=%x\n", devmask);
2196 err_mask = ata_bus_softreset(ap, devmask);
2197 if (err_mask) {
2198 if (verbose)
2199 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2200 ap->id, err_mask);
2201 else
2202 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2203 err_mask);
2204 return -EIO;
2207 /* determine by signature whether we have ATA or ATAPI devices */
2208 classes[0] = ata_dev_try_classify(ap, 0, &err);
2209 if (slave_possible && err != 0x81)
2210 classes[1] = ata_dev_try_classify(ap, 1, &err);
2212 out:
2213 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2214 return 0;
2218 * sata_std_hardreset - reset host port via SATA phy reset
2219 * @ap: port to reset
2220 * @verbose: fail verbosely
2221 * @class: resulting class of attached device
2223 * SATA phy-reset host port using DET bits of SControl register.
2224 * This function is to be used as standard callback for
2225 * ata_drive_*_reset().
2227 * LOCKING:
2228 * Kernel thread context (may sleep)
2230 * RETURNS:
2231 * 0 on success, -errno otherwise.
2233 int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2235 DPRINTK("ENTER\n");
2237 /* Issue phy wake/reset */
2238 scr_write_flush(ap, SCR_CONTROL, 0x301);
2241 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2242 * 10.4.2 says at least 1 ms.
2244 msleep(1);
2246 /* Bring phy back */
2247 sata_phy_resume(ap);
2249 /* TODO: phy layer with polling, timeouts, etc. */
2250 if (!sata_dev_present(ap)) {
2251 *class = ATA_DEV_NONE;
2252 DPRINTK("EXIT, link offline\n");
2253 return 0;
2256 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2257 if (verbose)
2258 printk(KERN_ERR "ata%u: COMRESET failed "
2259 "(device not ready)\n", ap->id);
2260 else
2261 DPRINTK("EXIT, device not ready\n");
2262 return -EIO;
2265 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2267 *class = ata_dev_try_classify(ap, 0, NULL);
2269 DPRINTK("EXIT, class=%u\n", *class);
2270 return 0;
2274 * ata_std_postreset - standard postreset callback
2275 * @ap: the target ata_port
2276 * @classes: classes of attached devices
2278 * This function is invoked after a successful reset. Note that
2279 * the device might have been reset more than once using
2280 * different reset methods before postreset is invoked.
2282 * This function is to be used as standard callback for
2283 * ata_drive_*_reset().
2285 * LOCKING:
2286 * Kernel thread context (may sleep)
2288 void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2290 DPRINTK("ENTER\n");
2292 /* set cable type if it isn't already set */
2293 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2294 ap->cbl = ATA_CBL_SATA;
2296 /* print link status */
2297 if (ap->cbl == ATA_CBL_SATA)
2298 sata_print_link_status(ap);
2300 /* re-enable interrupts */
2301 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2302 ata_irq_on(ap);
2304 /* is double-select really necessary? */
2305 if (classes[0] != ATA_DEV_NONE)
2306 ap->ops->dev_select(ap, 1);
2307 if (classes[1] != ATA_DEV_NONE)
2308 ap->ops->dev_select(ap, 0);
2310 /* bail out if no device is present */
2311 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2312 DPRINTK("EXIT, no device\n");
2313 return;
2316 /* set up device control */
2317 if (ap->ioaddr.ctl_addr) {
2318 if (ap->flags & ATA_FLAG_MMIO)
2319 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2320 else
2321 outb(ap->ctl, ap->ioaddr.ctl_addr);
2324 DPRINTK("EXIT\n");
2328 * ata_std_probe_reset - standard probe reset method
2329 * @ap: prot to perform probe-reset
2330 * @classes: resulting classes of attached devices
2332 * The stock off-the-shelf ->probe_reset method.
2334 * LOCKING:
2335 * Kernel thread context (may sleep)
2337 * RETURNS:
2338 * 0 on success, -errno otherwise.
2340 int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2342 ata_reset_fn_t hardreset;
2344 hardreset = NULL;
2345 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2346 hardreset = sata_std_hardreset;
2348 return ata_drive_probe_reset(ap, ata_std_probeinit,
2349 ata_std_softreset, hardreset,
2350 ata_std_postreset, classes);
2353 static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2354 ata_postreset_fn_t postreset,
2355 unsigned int *classes)
2357 int i, rc;
2359 for (i = 0; i < ATA_MAX_DEVICES; i++)
2360 classes[i] = ATA_DEV_UNKNOWN;
2362 rc = reset(ap, 0, classes);
2363 if (rc)
2364 return rc;
2366 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2367 * is complete and convert all ATA_DEV_UNKNOWN to
2368 * ATA_DEV_NONE.
2370 for (i = 0; i < ATA_MAX_DEVICES; i++)
2371 if (classes[i] != ATA_DEV_UNKNOWN)
2372 break;
2374 if (i < ATA_MAX_DEVICES)
2375 for (i = 0; i < ATA_MAX_DEVICES; i++)
2376 if (classes[i] == ATA_DEV_UNKNOWN)
2377 classes[i] = ATA_DEV_NONE;
2379 if (postreset)
2380 postreset(ap, classes);
2382 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2386 * ata_drive_probe_reset - Perform probe reset with given methods
2387 * @ap: port to reset
2388 * @probeinit: probeinit method (can be NULL)
2389 * @softreset: softreset method (can be NULL)
2390 * @hardreset: hardreset method (can be NULL)
2391 * @postreset: postreset method (can be NULL)
2392 * @classes: resulting classes of attached devices
2394 * Reset the specified port and classify attached devices using
2395 * given methods. This function prefers softreset but tries all
2396 * possible reset sequences to reset and classify devices. This
2397 * function is intended to be used for constructing ->probe_reset
2398 * callback by low level drivers.
2400 * Reset methods should follow the following rules.
2402 * - Return 0 on sucess, -errno on failure.
2403 * - If classification is supported, fill classes[] with
2404 * recognized class codes.
2405 * - If classification is not supported, leave classes[] alone.
2406 * - If verbose is non-zero, print error message on failure;
2407 * otherwise, shut up.
2409 * LOCKING:
2410 * Kernel thread context (may sleep)
2412 * RETURNS:
2413 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2414 * if classification fails, and any error code from reset
2415 * methods.
2417 int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2418 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2419 ata_postreset_fn_t postreset, unsigned int *classes)
2421 int rc = -EINVAL;
2423 if (probeinit)
2424 probeinit(ap);
2426 if (softreset) {
2427 rc = do_probe_reset(ap, softreset, postreset, classes);
2428 if (rc == 0)
2429 return 0;
2432 if (!hardreset)
2433 return rc;
2435 rc = do_probe_reset(ap, hardreset, postreset, classes);
2436 if (rc == 0 || rc != -ENODEV)
2437 return rc;
2439 if (softreset)
2440 rc = do_probe_reset(ap, softreset, postreset, classes);
2442 return rc;
2446 * ata_dev_same_device - Determine whether new ID matches configured device
2447 * @ap: port on which the device to compare against resides
2448 * @dev: device to compare against
2449 * @new_class: class of the new device
2450 * @new_id: IDENTIFY page of the new device
2452 * Compare @new_class and @new_id against @dev and determine
2453 * whether @dev is the device indicated by @new_class and
2454 * @new_id.
2456 * LOCKING:
2457 * None.
2459 * RETURNS:
2460 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2462 static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2463 unsigned int new_class, const u16 *new_id)
2465 const u16 *old_id = dev->id;
2466 unsigned char model[2][41], serial[2][21];
2467 u64 new_n_sectors;
2469 if (dev->class != new_class) {
2470 printk(KERN_INFO
2471 "ata%u: dev %u class mismatch %d != %d\n",
2472 ap->id, dev->devno, dev->class, new_class);
2473 return 0;
2476 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2477 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2478 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2479 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2480 new_n_sectors = ata_id_n_sectors(new_id);
2482 if (strcmp(model[0], model[1])) {
2483 printk(KERN_INFO
2484 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2485 ap->id, dev->devno, model[0], model[1]);
2486 return 0;
2489 if (strcmp(serial[0], serial[1])) {
2490 printk(KERN_INFO
2491 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2492 ap->id, dev->devno, serial[0], serial[1]);
2493 return 0;
2496 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2497 printk(KERN_INFO
2498 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2499 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2500 (unsigned long long)new_n_sectors);
2501 return 0;
2504 return 1;
2508 * ata_dev_revalidate - Revalidate ATA device
2509 * @ap: port on which the device to revalidate resides
2510 * @dev: device to revalidate
2511 * @post_reset: is this revalidation after reset?
2513 * Re-read IDENTIFY page and make sure @dev is still attached to
2514 * the port.
2516 * LOCKING:
2517 * Kernel thread context (may sleep)
2519 * RETURNS:
2520 * 0 on success, negative errno otherwise
2522 int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2523 int post_reset)
2525 unsigned int class;
2526 u16 *id;
2527 int rc;
2529 if (!ata_dev_present(dev))
2530 return -ENODEV;
2532 class = dev->class;
2533 id = NULL;
2535 /* allocate & read ID data */
2536 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2537 if (rc)
2538 goto fail;
2540 /* is the device still there? */
2541 if (!ata_dev_same_device(ap, dev, class, id)) {
2542 rc = -ENODEV;
2543 goto fail;
2546 kfree(dev->id);
2547 dev->id = id;
2549 /* configure device according to the new ID */
2550 return ata_dev_configure(ap, dev, 0);
2552 fail:
2553 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2554 ap->id, dev->devno, rc);
2555 kfree(id);
2556 return rc;
2559 static const char * const ata_dma_blacklist [] = {
2560 "WDC AC11000H",
2561 "WDC AC22100H",
2562 "WDC AC32500H",
2563 "WDC AC33100H",
2564 "WDC AC31600H",
2565 "WDC AC32100H",
2566 "WDC AC23200L",
2567 "Compaq CRD-8241B",
2568 "CRD-8400B",
2569 "CRD-8480B",
2570 "CRD-8482B",
2571 "CRD-84",
2572 "SanDisk SDP3B",
2573 "SanDisk SDP3B-64",
2574 "SANYO CD-ROM CRD",
2575 "HITACHI CDR-8",
2576 "HITACHI CDR-8335",
2577 "HITACHI CDR-8435",
2578 "Toshiba CD-ROM XM-6202B",
2579 "TOSHIBA CD-ROM XM-1702BC",
2580 "CD-532E-A",
2581 "E-IDE CD-ROM CR-840",
2582 "CD-ROM Drive/F5A",
2583 "WPI CDD-820",
2584 "SAMSUNG CD-ROM SC-148C",
2585 "SAMSUNG CD-ROM SC",
2586 "SanDisk SDP3B-64",
2587 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2588 "_NEC DV5800A",
2591 static int ata_dma_blacklisted(const struct ata_device *dev)
2593 unsigned char model_num[41];
2594 int i;
2596 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
2598 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2599 if (!strcmp(ata_dma_blacklist[i], model_num))
2600 return 1;
2602 return 0;
2606 * ata_dev_xfermask - Compute supported xfermask of the given device
2607 * @ap: Port on which the device to compute xfermask for resides
2608 * @dev: Device to compute xfermask for
2610 * Compute supported xfermask of @dev. This function is
2611 * responsible for applying all known limits including host
2612 * controller limits, device blacklist, etc...
2614 * LOCKING:
2615 * None.
2617 * RETURNS:
2618 * Computed xfermask.
2620 static unsigned int ata_dev_xfermask(struct ata_port *ap,
2621 struct ata_device *dev)
2623 unsigned long xfer_mask;
2624 int i;
2626 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2627 ap->udma_mask);
2629 /* use port-wide xfermask for now */
2630 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2631 struct ata_device *d = &ap->device[i];
2632 if (!ata_dev_present(d))
2633 continue;
2634 xfer_mask &= ata_id_xfermask(d->id);
2635 if (ata_dma_blacklisted(d))
2636 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2639 if (ata_dma_blacklisted(dev))
2640 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2641 "disabling DMA\n", ap->id, dev->devno);
2643 return xfer_mask;
2647 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2648 * @ap: Port associated with device @dev
2649 * @dev: Device to which command will be sent
2651 * Issue SET FEATURES - XFER MODE command to device @dev
2652 * on port @ap.
2654 * LOCKING:
2655 * PCI/etc. bus probe sem.
2658 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2660 struct ata_taskfile tf;
2662 /* set up set-features taskfile */
2663 DPRINTK("set features - xfer mode\n");
2665 ata_tf_init(ap, &tf, dev->devno);
2666 tf.command = ATA_CMD_SET_FEATURES;
2667 tf.feature = SETFEATURES_XFER;
2668 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2669 tf.protocol = ATA_PROT_NODATA;
2670 tf.nsect = dev->xfer_mode;
2672 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2673 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2674 ap->id);
2675 ata_port_disable(ap);
2678 DPRINTK("EXIT\n");
2682 * ata_dev_init_params - Issue INIT DEV PARAMS command
2683 * @ap: Port associated with device @dev
2684 * @dev: Device to which command will be sent
2686 * LOCKING:
2687 * Kernel thread context (may sleep)
2689 * RETURNS:
2690 * 0 on success, AC_ERR_* mask otherwise.
2693 static unsigned int ata_dev_init_params(struct ata_port *ap,
2694 struct ata_device *dev)
2696 struct ata_taskfile tf;
2697 unsigned int err_mask;
2698 u16 sectors = dev->id[6];
2699 u16 heads = dev->id[3];
2701 /* Number of sectors per track 1-255. Number of heads 1-16 */
2702 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2703 return 0;
2705 /* set up init dev params taskfile */
2706 DPRINTK("init dev params \n");
2708 ata_tf_init(ap, &tf, dev->devno);
2709 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2710 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2711 tf.protocol = ATA_PROT_NODATA;
2712 tf.nsect = sectors;
2713 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2715 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2717 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2718 return err_mask;
2722 * ata_sg_clean - Unmap DMA memory associated with command
2723 * @qc: Command containing DMA memory to be released
2725 * Unmap all mapped DMA memory associated with this command.
2727 * LOCKING:
2728 * spin_lock_irqsave(host_set lock)
2731 static void ata_sg_clean(struct ata_queued_cmd *qc)
2733 struct ata_port *ap = qc->ap;
2734 struct scatterlist *sg = qc->__sg;
2735 int dir = qc->dma_dir;
2736 void *pad_buf = NULL;
2738 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2739 WARN_ON(sg == NULL);
2741 if (qc->flags & ATA_QCFLAG_SINGLE)
2742 WARN_ON(qc->n_elem > 1);
2744 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2746 /* if we padded the buffer out to 32-bit bound, and data
2747 * xfer direction is from-device, we must copy from the
2748 * pad buffer back into the supplied buffer
2750 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2751 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2753 if (qc->flags & ATA_QCFLAG_SG) {
2754 if (qc->n_elem)
2755 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2756 /* restore last sg */
2757 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2758 if (pad_buf) {
2759 struct scatterlist *psg = &qc->pad_sgent;
2760 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2761 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2762 kunmap_atomic(addr, KM_IRQ0);
2764 } else {
2765 if (qc->n_elem)
2766 dma_unmap_single(ap->host_set->dev,
2767 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2768 dir);
2769 /* restore sg */
2770 sg->length += qc->pad_len;
2771 if (pad_buf)
2772 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2773 pad_buf, qc->pad_len);
2776 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2777 qc->__sg = NULL;
2781 * ata_fill_sg - Fill PCI IDE PRD table
2782 * @qc: Metadata associated with taskfile to be transferred
2784 * Fill PCI IDE PRD (scatter-gather) table with segments
2785 * associated with the current disk command.
2787 * LOCKING:
2788 * spin_lock_irqsave(host_set lock)
2791 static void ata_fill_sg(struct ata_queued_cmd *qc)
2793 struct ata_port *ap = qc->ap;
2794 struct scatterlist *sg;
2795 unsigned int idx;
2797 WARN_ON(qc->__sg == NULL);
2798 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2800 idx = 0;
2801 ata_for_each_sg(sg, qc) {
2802 u32 addr, offset;
2803 u32 sg_len, len;
2805 /* determine if physical DMA addr spans 64K boundary.
2806 * Note h/w doesn't support 64-bit, so we unconditionally
2807 * truncate dma_addr_t to u32.
2809 addr = (u32) sg_dma_address(sg);
2810 sg_len = sg_dma_len(sg);
2812 while (sg_len) {
2813 offset = addr & 0xffff;
2814 len = sg_len;
2815 if ((offset + sg_len) > 0x10000)
2816 len = 0x10000 - offset;
2818 ap->prd[idx].addr = cpu_to_le32(addr);
2819 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2820 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2822 idx++;
2823 sg_len -= len;
2824 addr += len;
2828 if (idx)
2829 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2832 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2833 * @qc: Metadata associated with taskfile to check
2835 * Allow low-level driver to filter ATA PACKET commands, returning
2836 * a status indicating whether or not it is OK to use DMA for the
2837 * supplied PACKET command.
2839 * LOCKING:
2840 * spin_lock_irqsave(host_set lock)
2842 * RETURNS: 0 when ATAPI DMA can be used
2843 * nonzero otherwise
2845 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2847 struct ata_port *ap = qc->ap;
2848 int rc = 0; /* Assume ATAPI DMA is OK by default */
2850 if (ap->ops->check_atapi_dma)
2851 rc = ap->ops->check_atapi_dma(qc);
2853 return rc;
2856 * ata_qc_prep - Prepare taskfile for submission
2857 * @qc: Metadata associated with taskfile to be prepared
2859 * Prepare ATA taskfile for submission.
2861 * LOCKING:
2862 * spin_lock_irqsave(host_set lock)
2864 void ata_qc_prep(struct ata_queued_cmd *qc)
2866 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2867 return;
2869 ata_fill_sg(qc);
2873 * ata_sg_init_one - Associate command with memory buffer
2874 * @qc: Command to be associated
2875 * @buf: Memory buffer
2876 * @buflen: Length of memory buffer, in bytes.
2878 * Initialize the data-related elements of queued_cmd @qc
2879 * to point to a single memory buffer, @buf of byte length @buflen.
2881 * LOCKING:
2882 * spin_lock_irqsave(host_set lock)
2885 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2887 struct scatterlist *sg;
2889 qc->flags |= ATA_QCFLAG_SINGLE;
2891 memset(&qc->sgent, 0, sizeof(qc->sgent));
2892 qc->__sg = &qc->sgent;
2893 qc->n_elem = 1;
2894 qc->orig_n_elem = 1;
2895 qc->buf_virt = buf;
2897 sg = qc->__sg;
2898 sg_init_one(sg, buf, buflen);
2902 * ata_sg_init - Associate command with scatter-gather table.
2903 * @qc: Command to be associated
2904 * @sg: Scatter-gather table.
2905 * @n_elem: Number of elements in s/g table.
2907 * Initialize the data-related elements of queued_cmd @qc
2908 * to point to a scatter-gather table @sg, containing @n_elem
2909 * elements.
2911 * LOCKING:
2912 * spin_lock_irqsave(host_set lock)
2915 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2916 unsigned int n_elem)
2918 qc->flags |= ATA_QCFLAG_SG;
2919 qc->__sg = sg;
2920 qc->n_elem = n_elem;
2921 qc->orig_n_elem = n_elem;
2925 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2926 * @qc: Command with memory buffer to be mapped.
2928 * DMA-map the memory buffer associated with queued_cmd @qc.
2930 * LOCKING:
2931 * spin_lock_irqsave(host_set lock)
2933 * RETURNS:
2934 * Zero on success, negative on error.
2937 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2939 struct ata_port *ap = qc->ap;
2940 int dir = qc->dma_dir;
2941 struct scatterlist *sg = qc->__sg;
2942 dma_addr_t dma_address;
2943 int trim_sg = 0;
2945 /* we must lengthen transfers to end on a 32-bit boundary */
2946 qc->pad_len = sg->length & 3;
2947 if (qc->pad_len) {
2948 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2949 struct scatterlist *psg = &qc->pad_sgent;
2951 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2953 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2955 if (qc->tf.flags & ATA_TFLAG_WRITE)
2956 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2957 qc->pad_len);
2959 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2960 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2961 /* trim sg */
2962 sg->length -= qc->pad_len;
2963 if (sg->length == 0)
2964 trim_sg = 1;
2966 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2967 sg->length, qc->pad_len);
2970 if (trim_sg) {
2971 qc->n_elem--;
2972 goto skip_map;
2975 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2976 sg->length, dir);
2977 if (dma_mapping_error(dma_address)) {
2978 /* restore sg */
2979 sg->length += qc->pad_len;
2980 return -1;
2983 sg_dma_address(sg) = dma_address;
2984 sg_dma_len(sg) = sg->length;
2986 skip_map:
2987 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2988 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2990 return 0;
2994 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2995 * @qc: Command with scatter-gather table to be mapped.
2997 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2999 * LOCKING:
3000 * spin_lock_irqsave(host_set lock)
3002 * RETURNS:
3003 * Zero on success, negative on error.
3007 static int ata_sg_setup(struct ata_queued_cmd *qc)
3009 struct ata_port *ap = qc->ap;
3010 struct scatterlist *sg = qc->__sg;
3011 struct scatterlist *lsg = &sg[qc->n_elem - 1];
3012 int n_elem, pre_n_elem, dir, trim_sg = 0;
3014 VPRINTK("ENTER, ata%u\n", ap->id);
3015 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
3017 /* we must lengthen transfers to end on a 32-bit boundary */
3018 qc->pad_len = lsg->length & 3;
3019 if (qc->pad_len) {
3020 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3021 struct scatterlist *psg = &qc->pad_sgent;
3022 unsigned int offset;
3024 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
3026 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3029 * psg->page/offset are used to copy to-be-written
3030 * data in this function or read data in ata_sg_clean.
3032 offset = lsg->offset + lsg->length - qc->pad_len;
3033 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3034 psg->offset = offset_in_page(offset);
3036 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3037 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3038 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
3039 kunmap_atomic(addr, KM_IRQ0);
3042 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3043 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3044 /* trim last sg */
3045 lsg->length -= qc->pad_len;
3046 if (lsg->length == 0)
3047 trim_sg = 1;
3049 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3050 qc->n_elem - 1, lsg->length, qc->pad_len);
3053 pre_n_elem = qc->n_elem;
3054 if (trim_sg && pre_n_elem)
3055 pre_n_elem--;
3057 if (!pre_n_elem) {
3058 n_elem = 0;
3059 goto skip_map;
3062 dir = qc->dma_dir;
3063 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
3064 if (n_elem < 1) {
3065 /* restore last sg */
3066 lsg->length += qc->pad_len;
3067 return -1;
3070 DPRINTK("%d sg elements mapped\n", n_elem);
3072 skip_map:
3073 qc->n_elem = n_elem;
3075 return 0;
3079 * ata_poll_qc_complete - turn irq back on and finish qc
3080 * @qc: Command to complete
3081 * @err_mask: ATA status register content
3083 * LOCKING:
3084 * None. (grabs host lock)
3087 void ata_poll_qc_complete(struct ata_queued_cmd *qc)
3089 struct ata_port *ap = qc->ap;
3090 unsigned long flags;
3092 spin_lock_irqsave(&ap->host_set->lock, flags);
3093 ata_irq_on(ap);
3094 ata_qc_complete(qc);
3095 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3099 * ata_pio_poll - poll using PIO, depending on current state
3100 * @ap: the target ata_port
3102 * LOCKING:
3103 * None. (executing in kernel thread context)
3105 * RETURNS:
3106 * timeout value to use
3109 static unsigned long ata_pio_poll(struct ata_port *ap)
3111 struct ata_queued_cmd *qc;
3112 u8 status;
3113 unsigned int poll_state = HSM_ST_UNKNOWN;
3114 unsigned int reg_state = HSM_ST_UNKNOWN;
3116 qc = ata_qc_from_tag(ap, ap->active_tag);
3117 WARN_ON(qc == NULL);
3119 switch (ap->hsm_task_state) {
3120 case HSM_ST:
3121 case HSM_ST_POLL:
3122 poll_state = HSM_ST_POLL;
3123 reg_state = HSM_ST;
3124 break;
3125 case HSM_ST_LAST:
3126 case HSM_ST_LAST_POLL:
3127 poll_state = HSM_ST_LAST_POLL;
3128 reg_state = HSM_ST_LAST;
3129 break;
3130 default:
3131 BUG();
3132 break;
3135 status = ata_chk_status(ap);
3136 if (status & ATA_BUSY) {
3137 if (time_after(jiffies, ap->pio_task_timeout)) {
3138 qc->err_mask |= AC_ERR_TIMEOUT;
3139 ap->hsm_task_state = HSM_ST_TMOUT;
3140 return 0;
3142 ap->hsm_task_state = poll_state;
3143 return ATA_SHORT_PAUSE;
3146 ap->hsm_task_state = reg_state;
3147 return 0;
3151 * ata_pio_complete - check if drive is busy or idle
3152 * @ap: the target ata_port
3154 * LOCKING:
3155 * None. (executing in kernel thread context)
3157 * RETURNS:
3158 * Zero if qc completed.
3159 * Non-zero if has next.
3162 static int ata_pio_complete (struct ata_port *ap)
3164 struct ata_queued_cmd *qc;
3165 u8 drv_stat;
3168 * This is purely heuristic. This is a fast path. Sometimes when
3169 * we enter, BSY will be cleared in a chk-status or two. If not,
3170 * the drive is probably seeking or something. Snooze for a couple
3171 * msecs, then chk-status again. If still busy, fall back to
3172 * HSM_ST_LAST_POLL state.
3174 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3175 if (drv_stat & ATA_BUSY) {
3176 msleep(2);
3177 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3178 if (drv_stat & ATA_BUSY) {
3179 ap->hsm_task_state = HSM_ST_LAST_POLL;
3180 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3181 return 1;
3185 qc = ata_qc_from_tag(ap, ap->active_tag);
3186 WARN_ON(qc == NULL);
3188 drv_stat = ata_wait_idle(ap);
3189 if (!ata_ok(drv_stat)) {
3190 qc->err_mask |= __ac_err_mask(drv_stat);
3191 ap->hsm_task_state = HSM_ST_ERR;
3192 return 1;
3195 ap->hsm_task_state = HSM_ST_IDLE;
3197 WARN_ON(qc->err_mask);
3198 ata_poll_qc_complete(qc);
3200 /* another command may start at this point */
3202 return 0;
3207 * swap_buf_le16 - swap halves of 16-bit words in place
3208 * @buf: Buffer to swap
3209 * @buf_words: Number of 16-bit words in buffer.
3211 * Swap halves of 16-bit words if needed to convert from
3212 * little-endian byte order to native cpu byte order, or
3213 * vice-versa.
3215 * LOCKING:
3216 * Inherited from caller.
3218 void swap_buf_le16(u16 *buf, unsigned int buf_words)
3220 #ifdef __BIG_ENDIAN
3221 unsigned int i;
3223 for (i = 0; i < buf_words; i++)
3224 buf[i] = le16_to_cpu(buf[i]);
3225 #endif /* __BIG_ENDIAN */
3229 * ata_mmio_data_xfer - Transfer data by MMIO
3230 * @ap: port to read/write
3231 * @buf: data buffer
3232 * @buflen: buffer length
3233 * @write_data: read/write
3235 * Transfer data from/to the device data register by MMIO.
3237 * LOCKING:
3238 * Inherited from caller.
3241 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3242 unsigned int buflen, int write_data)
3244 unsigned int i;
3245 unsigned int words = buflen >> 1;
3246 u16 *buf16 = (u16 *) buf;
3247 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3249 /* Transfer multiple of 2 bytes */
3250 if (write_data) {
3251 for (i = 0; i < words; i++)
3252 writew(le16_to_cpu(buf16[i]), mmio);
3253 } else {
3254 for (i = 0; i < words; i++)
3255 buf16[i] = cpu_to_le16(readw(mmio));
3258 /* Transfer trailing 1 byte, if any. */
3259 if (unlikely(buflen & 0x01)) {
3260 u16 align_buf[1] = { 0 };
3261 unsigned char *trailing_buf = buf + buflen - 1;
3263 if (write_data) {
3264 memcpy(align_buf, trailing_buf, 1);
3265 writew(le16_to_cpu(align_buf[0]), mmio);
3266 } else {
3267 align_buf[0] = cpu_to_le16(readw(mmio));
3268 memcpy(trailing_buf, align_buf, 1);
3274 * ata_pio_data_xfer - Transfer data by PIO
3275 * @ap: port to read/write
3276 * @buf: data buffer
3277 * @buflen: buffer length
3278 * @write_data: read/write
3280 * Transfer data from/to the device data register by PIO.
3282 * LOCKING:
3283 * Inherited from caller.
3286 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3287 unsigned int buflen, int write_data)
3289 unsigned int words = buflen >> 1;
3291 /* Transfer multiple of 2 bytes */
3292 if (write_data)
3293 outsw(ap->ioaddr.data_addr, buf, words);
3294 else
3295 insw(ap->ioaddr.data_addr, buf, words);
3297 /* Transfer trailing 1 byte, if any. */
3298 if (unlikely(buflen & 0x01)) {
3299 u16 align_buf[1] = { 0 };
3300 unsigned char *trailing_buf = buf + buflen - 1;
3302 if (write_data) {
3303 memcpy(align_buf, trailing_buf, 1);
3304 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3305 } else {
3306 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3307 memcpy(trailing_buf, align_buf, 1);
3313 * ata_data_xfer - Transfer data from/to the data register.
3314 * @ap: port to read/write
3315 * @buf: data buffer
3316 * @buflen: buffer length
3317 * @do_write: read/write
3319 * Transfer data from/to the device data register.
3321 * LOCKING:
3322 * Inherited from caller.
3325 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3326 unsigned int buflen, int do_write)
3328 /* Make the crap hardware pay the costs not the good stuff */
3329 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3330 unsigned long flags;
3331 local_irq_save(flags);
3332 if (ap->flags & ATA_FLAG_MMIO)
3333 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3334 else
3335 ata_pio_data_xfer(ap, buf, buflen, do_write);
3336 local_irq_restore(flags);
3337 } else {
3338 if (ap->flags & ATA_FLAG_MMIO)
3339 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3340 else
3341 ata_pio_data_xfer(ap, buf, buflen, do_write);
3346 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3347 * @qc: Command on going
3349 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3351 * LOCKING:
3352 * Inherited from caller.
3355 static void ata_pio_sector(struct ata_queued_cmd *qc)
3357 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3358 struct scatterlist *sg = qc->__sg;
3359 struct ata_port *ap = qc->ap;
3360 struct page *page;
3361 unsigned int offset;
3362 unsigned char *buf;
3364 if (qc->cursect == (qc->nsect - 1))
3365 ap->hsm_task_state = HSM_ST_LAST;
3367 page = sg[qc->cursg].page;
3368 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3370 /* get the current page and offset */
3371 page = nth_page(page, (offset >> PAGE_SHIFT));
3372 offset %= PAGE_SIZE;
3374 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3376 if (PageHighMem(page)) {
3377 unsigned long flags;
3379 local_irq_save(flags);
3380 buf = kmap_atomic(page, KM_IRQ0);
3382 /* do the actual data transfer */
3383 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3385 kunmap_atomic(buf, KM_IRQ0);
3386 local_irq_restore(flags);
3387 } else {
3388 buf = page_address(page);
3389 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3392 qc->cursect++;
3393 qc->cursg_ofs++;
3395 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3396 qc->cursg++;
3397 qc->cursg_ofs = 0;
3402 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3403 * @qc: Command on going
3405 * Transfer one or many ATA_SECT_SIZE of data from/to the
3406 * ATA device for the DRQ request.
3408 * LOCKING:
3409 * Inherited from caller.
3412 static void ata_pio_sectors(struct ata_queued_cmd *qc)
3414 if (is_multi_taskfile(&qc->tf)) {
3415 /* READ/WRITE MULTIPLE */
3416 unsigned int nsect;
3418 WARN_ON(qc->dev->multi_count == 0);
3420 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3421 while (nsect--)
3422 ata_pio_sector(qc);
3423 } else
3424 ata_pio_sector(qc);
3428 * atapi_send_cdb - Write CDB bytes to hardware
3429 * @ap: Port to which ATAPI device is attached.
3430 * @qc: Taskfile currently active
3432 * When device has indicated its readiness to accept
3433 * a CDB, this function is called. Send the CDB.
3435 * LOCKING:
3436 * caller.
3439 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3441 /* send SCSI cdb */
3442 DPRINTK("send cdb\n");
3443 WARN_ON(qc->dev->cdb_len < 12);
3445 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3446 ata_altstatus(ap); /* flush */
3448 switch (qc->tf.protocol) {
3449 case ATA_PROT_ATAPI:
3450 ap->hsm_task_state = HSM_ST;
3451 break;
3452 case ATA_PROT_ATAPI_NODATA:
3453 ap->hsm_task_state = HSM_ST_LAST;
3454 break;
3455 case ATA_PROT_ATAPI_DMA:
3456 ap->hsm_task_state = HSM_ST_LAST;
3457 /* initiate bmdma */
3458 ap->ops->bmdma_start(qc);
3459 break;
3464 * ata_pio_first_block - Write first data block to hardware
3465 * @ap: Port to which ATA/ATAPI device is attached.
3467 * When device has indicated its readiness to accept
3468 * the data, this function sends out the CDB or
3469 * the first data block by PIO.
3470 * After this,
3471 * - If polling, ata_pio_task() handles the rest.
3472 * - Otherwise, interrupt handler takes over.
3474 * LOCKING:
3475 * Kernel thread context (may sleep)
3477 * RETURNS:
3478 * Zero if irq handler takes over
3479 * Non-zero if has next (polling).
3482 static int ata_pio_first_block(struct ata_port *ap)
3484 struct ata_queued_cmd *qc;
3485 u8 status;
3486 unsigned long flags;
3487 int has_next;
3489 qc = ata_qc_from_tag(ap, ap->active_tag);
3490 WARN_ON(qc == NULL);
3491 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
3493 /* if polling, we will stay in the work queue after sending the data.
3494 * otherwise, interrupt handler takes over after sending the data.
3496 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3498 /* sleep-wait for BSY to clear */
3499 DPRINTK("busy wait\n");
3500 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
3501 qc->err_mask |= AC_ERR_TIMEOUT;
3502 ap->hsm_task_state = HSM_ST_TMOUT;
3503 goto err_out;
3506 /* make sure DRQ is set */
3507 status = ata_chk_status(ap);
3508 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3509 /* device status error */
3510 qc->err_mask |= AC_ERR_HSM;
3511 ap->hsm_task_state = HSM_ST_ERR;
3512 goto err_out;
3515 /* Send the CDB (atapi) or the first data block (ata pio out).
3516 * During the state transition, interrupt handler shouldn't
3517 * be invoked before the data transfer is complete and
3518 * hsm_task_state is changed. Hence, the following locking.
3520 spin_lock_irqsave(&ap->host_set->lock, flags);
3522 if (qc->tf.protocol == ATA_PROT_PIO) {
3523 /* PIO data out protocol.
3524 * send first data block.
3527 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3528 * so, the state is changed here before ata_pio_sectors().
3530 ap->hsm_task_state = HSM_ST;
3531 ata_pio_sectors(qc);
3532 ata_altstatus(ap); /* flush */
3533 } else
3534 /* send CDB */
3535 atapi_send_cdb(ap, qc);
3537 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3539 /* if polling, ata_pio_task() handles the rest.
3540 * otherwise, interrupt handler takes over from here.
3542 return has_next;
3544 err_out:
3545 return 1; /* has next */
3549 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3550 * @qc: Command on going
3551 * @bytes: number of bytes
3553 * Transfer Transfer data from/to the ATAPI device.
3555 * LOCKING:
3556 * Inherited from caller.
3560 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3562 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3563 struct scatterlist *sg = qc->__sg;
3564 struct ata_port *ap = qc->ap;
3565 struct page *page;
3566 unsigned char *buf;
3567 unsigned int offset, count;
3569 if (qc->curbytes + bytes >= qc->nbytes)
3570 ap->hsm_task_state = HSM_ST_LAST;
3572 next_sg:
3573 if (unlikely(qc->cursg >= qc->n_elem)) {
3575 * The end of qc->sg is reached and the device expects
3576 * more data to transfer. In order not to overrun qc->sg
3577 * and fulfill length specified in the byte count register,
3578 * - for read case, discard trailing data from the device
3579 * - for write case, padding zero data to the device
3581 u16 pad_buf[1] = { 0 };
3582 unsigned int words = bytes >> 1;
3583 unsigned int i;
3585 if (words) /* warning if bytes > 1 */
3586 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3587 ap->id, bytes);
3589 for (i = 0; i < words; i++)
3590 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3592 ap->hsm_task_state = HSM_ST_LAST;
3593 return;
3596 sg = &qc->__sg[qc->cursg];
3598 page = sg->page;
3599 offset = sg->offset + qc->cursg_ofs;
3601 /* get the current page and offset */
3602 page = nth_page(page, (offset >> PAGE_SHIFT));
3603 offset %= PAGE_SIZE;
3605 /* don't overrun current sg */
3606 count = min(sg->length - qc->cursg_ofs, bytes);
3608 /* don't cross page boundaries */
3609 count = min(count, (unsigned int)PAGE_SIZE - offset);
3611 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3613 if (PageHighMem(page)) {
3614 unsigned long flags;
3616 local_irq_save(flags);
3617 buf = kmap_atomic(page, KM_IRQ0);
3619 /* do the actual data transfer */
3620 ata_data_xfer(ap, buf + offset, count, do_write);
3622 kunmap_atomic(buf, KM_IRQ0);
3623 local_irq_restore(flags);
3624 } else {
3625 buf = page_address(page);
3626 ata_data_xfer(ap, buf + offset, count, do_write);
3629 bytes -= count;
3630 qc->curbytes += count;
3631 qc->cursg_ofs += count;
3633 if (qc->cursg_ofs == sg->length) {
3634 qc->cursg++;
3635 qc->cursg_ofs = 0;
3638 if (bytes)
3639 goto next_sg;
3643 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3644 * @qc: Command on going
3646 * Transfer Transfer data from/to the ATAPI device.
3648 * LOCKING:
3649 * Inherited from caller.
3652 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3654 struct ata_port *ap = qc->ap;
3655 struct ata_device *dev = qc->dev;
3656 unsigned int ireason, bc_lo, bc_hi, bytes;
3657 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3659 ap->ops->tf_read(ap, &qc->tf);
3660 ireason = qc->tf.nsect;
3661 bc_lo = qc->tf.lbam;
3662 bc_hi = qc->tf.lbah;
3663 bytes = (bc_hi << 8) | bc_lo;
3665 /* shall be cleared to zero, indicating xfer of data */
3666 if (ireason & (1 << 0))
3667 goto err_out;
3669 /* make sure transfer direction matches expected */
3670 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3671 if (do_write != i_write)
3672 goto err_out;
3674 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3676 __atapi_pio_bytes(qc, bytes);
3678 return;
3680 err_out:
3681 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3682 ap->id, dev->devno);
3683 qc->err_mask |= AC_ERR_HSM;
3684 ap->hsm_task_state = HSM_ST_ERR;
3688 * ata_pio_block - start PIO on a block
3689 * @ap: the target ata_port
3691 * LOCKING:
3692 * None. (executing in kernel thread context)
3695 static void ata_pio_block(struct ata_port *ap)
3697 struct ata_queued_cmd *qc;
3698 u8 status;
3701 * This is purely heuristic. This is a fast path.
3702 * Sometimes when we enter, BSY will be cleared in
3703 * a chk-status or two. If not, the drive is probably seeking
3704 * or something. Snooze for a couple msecs, then
3705 * chk-status again. If still busy, fall back to
3706 * HSM_ST_POLL state.
3708 status = ata_busy_wait(ap, ATA_BUSY, 5);
3709 if (status & ATA_BUSY) {
3710 msleep(2);
3711 status = ata_busy_wait(ap, ATA_BUSY, 10);
3712 if (status & ATA_BUSY) {
3713 ap->hsm_task_state = HSM_ST_POLL;
3714 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3715 return;
3719 qc = ata_qc_from_tag(ap, ap->active_tag);
3720 WARN_ON(qc == NULL);
3722 /* check error */
3723 if (status & (ATA_ERR | ATA_DF)) {
3724 qc->err_mask |= AC_ERR_DEV;
3725 ap->hsm_task_state = HSM_ST_ERR;
3726 return;
3729 /* transfer data if any */
3730 if (is_atapi_taskfile(&qc->tf)) {
3731 /* DRQ=0 means no more data to transfer */
3732 if ((status & ATA_DRQ) == 0) {
3733 ap->hsm_task_state = HSM_ST_LAST;
3734 return;
3737 atapi_pio_bytes(qc);
3738 } else {
3739 /* handle BSY=0, DRQ=0 as error */
3740 if ((status & ATA_DRQ) == 0) {
3741 qc->err_mask |= AC_ERR_HSM;
3742 ap->hsm_task_state = HSM_ST_ERR;
3743 return;
3746 ata_pio_sectors(qc);
3749 ata_altstatus(ap); /* flush */
3752 static void ata_pio_error(struct ata_port *ap)
3754 struct ata_queued_cmd *qc;
3756 qc = ata_qc_from_tag(ap, ap->active_tag);
3757 WARN_ON(qc == NULL);
3759 if (qc->tf.command != ATA_CMD_PACKET)
3760 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3762 /* make sure qc->err_mask is available to
3763 * know what's wrong and recover
3765 WARN_ON(qc->err_mask == 0);
3767 ap->hsm_task_state = HSM_ST_IDLE;
3769 ata_poll_qc_complete(qc);
3772 static void ata_pio_task(void *_data)
3774 struct ata_port *ap = _data;
3775 unsigned long timeout;
3776 int has_next;
3778 fsm_start:
3779 timeout = 0;
3780 has_next = 1;
3782 switch (ap->hsm_task_state) {
3783 case HSM_ST_FIRST:
3784 has_next = ata_pio_first_block(ap);
3785 break;
3787 case HSM_ST:
3788 ata_pio_block(ap);
3789 break;
3791 case HSM_ST_LAST:
3792 has_next = ata_pio_complete(ap);
3793 break;
3795 case HSM_ST_POLL:
3796 case HSM_ST_LAST_POLL:
3797 timeout = ata_pio_poll(ap);
3798 break;
3800 case HSM_ST_TMOUT:
3801 case HSM_ST_ERR:
3802 ata_pio_error(ap);
3803 return;
3805 default:
3806 BUG();
3807 return;
3810 if (timeout)
3811 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
3812 else if (has_next)
3813 goto fsm_start;
3817 * atapi_packet_task - Write CDB bytes to hardware
3818 * @_data: Port to which ATAPI device is attached.
3820 * When device has indicated its readiness to accept
3821 * a CDB, this function is called. Send the CDB.
3822 * If DMA is to be performed, exit immediately.
3823 * Otherwise, we are in polling mode, so poll
3824 * status under operation succeeds or fails.
3826 * LOCKING:
3827 * Kernel thread context (may sleep)
3830 static void atapi_packet_task(void *_data)
3832 struct ata_port *ap = _data;
3833 struct ata_queued_cmd *qc;
3834 u8 status;
3836 qc = ata_qc_from_tag(ap, ap->active_tag);
3837 WARN_ON(qc == NULL);
3838 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3840 /* sleep-wait for BSY to clear */
3841 DPRINTK("busy wait\n");
3842 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3843 qc->err_mask |= AC_ERR_TIMEOUT;
3844 goto err_out;
3847 /* make sure DRQ is set */
3848 status = ata_chk_status(ap);
3849 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3850 qc->err_mask |= AC_ERR_HSM;
3851 goto err_out;
3854 /* send SCSI cdb */
3855 DPRINTK("send cdb\n");
3856 WARN_ON(qc->dev->cdb_len < 12);
3858 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3859 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3860 unsigned long flags;
3862 /* Once we're done issuing command and kicking bmdma,
3863 * irq handler takes over. To not lose irq, we need
3864 * to clear NOINTR flag before sending cdb, but
3865 * interrupt handler shouldn't be invoked before we're
3866 * finished. Hence, the following locking.
3868 spin_lock_irqsave(&ap->host_set->lock, flags);
3869 #warning FIXME
3870 /* ap->flags &= ~ATA_FLAG_NOINTR; */
3871 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3872 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3873 ap->ops->bmdma_start(qc); /* initiate bmdma */
3874 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3875 } else {
3876 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3878 /* PIO commands are handled by polling */
3879 ap->hsm_task_state = HSM_ST;
3880 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3883 return;
3885 err_out:
3886 ata_poll_qc_complete(qc);
3890 * ata_qc_timeout - Handle timeout of queued command
3891 * @qc: Command that timed out
3893 * Some part of the kernel (currently, only the SCSI layer)
3894 * has noticed that the active command on port @ap has not
3895 * completed after a specified length of time. Handle this
3896 * condition by disabling DMA (if necessary) and completing
3897 * transactions, with error if necessary.
3899 * This also handles the case of the "lost interrupt", where
3900 * for some reason (possibly hardware bug, possibly driver bug)
3901 * an interrupt was not delivered to the driver, even though the
3902 * transaction completed successfully.
3904 * LOCKING:
3905 * Inherited from SCSI layer (none, can sleep)
3908 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3910 struct ata_port *ap = qc->ap;
3911 struct ata_host_set *host_set = ap->host_set;
3912 u8 host_stat = 0, drv_stat;
3913 unsigned long flags;
3915 DPRINTK("ENTER\n");
3917 ap->hsm_task_state = HSM_ST_IDLE;
3919 spin_lock_irqsave(&host_set->lock, flags);
3921 switch (qc->tf.protocol) {
3923 case ATA_PROT_DMA:
3924 case ATA_PROT_ATAPI_DMA:
3925 host_stat = ap->ops->bmdma_status(ap);
3927 /* before we do anything else, clear DMA-Start bit */
3928 ap->ops->bmdma_stop(qc);
3930 /* fall through */
3932 default:
3933 ata_altstatus(ap);
3934 drv_stat = ata_chk_status(ap);
3936 /* ack bmdma irq events */
3937 ap->ops->irq_clear(ap);
3939 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3940 ap->id, qc->tf.command, drv_stat, host_stat);
3942 ap->hsm_task_state = HSM_ST_IDLE;
3944 /* complete taskfile transaction */
3945 qc->err_mask |= AC_ERR_TIMEOUT;
3946 break;
3949 spin_unlock_irqrestore(&host_set->lock, flags);
3951 ata_eh_qc_complete(qc);
3953 DPRINTK("EXIT\n");
3957 * ata_eng_timeout - Handle timeout of queued command
3958 * @ap: Port on which timed-out command is active
3960 * Some part of the kernel (currently, only the SCSI layer)
3961 * has noticed that the active command on port @ap has not
3962 * completed after a specified length of time. Handle this
3963 * condition by disabling DMA (if necessary) and completing
3964 * transactions, with error if necessary.
3966 * This also handles the case of the "lost interrupt", where
3967 * for some reason (possibly hardware bug, possibly driver bug)
3968 * an interrupt was not delivered to the driver, even though the
3969 * transaction completed successfully.
3971 * LOCKING:
3972 * Inherited from SCSI layer (none, can sleep)
3975 void ata_eng_timeout(struct ata_port *ap)
3977 DPRINTK("ENTER\n");
3979 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
3981 DPRINTK("EXIT\n");
3985 * ata_qc_new - Request an available ATA command, for queueing
3986 * @ap: Port associated with device @dev
3987 * @dev: Device from whom we request an available command structure
3989 * LOCKING:
3990 * None.
3993 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3995 struct ata_queued_cmd *qc = NULL;
3996 unsigned int i;
3998 for (i = 0; i < ATA_MAX_QUEUE; i++)
3999 if (!test_and_set_bit(i, &ap->qactive)) {
4000 qc = ata_qc_from_tag(ap, i);
4001 break;
4004 if (qc)
4005 qc->tag = i;
4007 return qc;
4011 * ata_qc_new_init - Request an available ATA command, and initialize it
4012 * @ap: Port associated with device @dev
4013 * @dev: Device from whom we request an available command structure
4015 * LOCKING:
4016 * None.
4019 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
4020 struct ata_device *dev)
4022 struct ata_queued_cmd *qc;
4024 qc = ata_qc_new(ap);
4025 if (qc) {
4026 qc->scsicmd = NULL;
4027 qc->ap = ap;
4028 qc->dev = dev;
4030 ata_qc_reinit(qc);
4033 return qc;
4037 * ata_qc_free - free unused ata_queued_cmd
4038 * @qc: Command to complete
4040 * Designed to free unused ata_queued_cmd object
4041 * in case something prevents using it.
4043 * LOCKING:
4044 * spin_lock_irqsave(host_set lock)
4046 void ata_qc_free(struct ata_queued_cmd *qc)
4048 struct ata_port *ap = qc->ap;
4049 unsigned int tag;
4051 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4053 qc->flags = 0;
4054 tag = qc->tag;
4055 if (likely(ata_tag_valid(tag))) {
4056 if (tag == ap->active_tag)
4057 ap->active_tag = ATA_TAG_POISON;
4058 qc->tag = ATA_TAG_POISON;
4059 clear_bit(tag, &ap->qactive);
4063 void __ata_qc_complete(struct ata_queued_cmd *qc)
4065 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4066 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4068 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4069 ata_sg_clean(qc);
4071 /* atapi: mark qc as inactive to prevent the interrupt handler
4072 * from completing the command twice later, before the error handler
4073 * is called. (when rc != 0 and atapi request sense is needed)
4075 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4077 /* call completion callback */
4078 qc->complete_fn(qc);
4081 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4083 struct ata_port *ap = qc->ap;
4085 switch (qc->tf.protocol) {
4086 case ATA_PROT_DMA:
4087 case ATA_PROT_ATAPI_DMA:
4088 return 1;
4090 case ATA_PROT_ATAPI:
4091 case ATA_PROT_PIO:
4092 case ATA_PROT_PIO_MULT:
4093 if (ap->flags & ATA_FLAG_PIO_DMA)
4094 return 1;
4096 /* fall through */
4098 default:
4099 return 0;
4102 /* never reached */
4106 * ata_qc_issue - issue taskfile to device
4107 * @qc: command to issue to device
4109 * Prepare an ATA command to submission to device.
4110 * This includes mapping the data into a DMA-able
4111 * area, filling in the S/G table, and finally
4112 * writing the taskfile to hardware, starting the command.
4114 * LOCKING:
4115 * spin_lock_irqsave(host_set lock)
4117 * RETURNS:
4118 * Zero on success, AC_ERR_* mask on failure
4121 unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
4123 struct ata_port *ap = qc->ap;
4125 if (ata_should_dma_map(qc)) {
4126 if (qc->flags & ATA_QCFLAG_SG) {
4127 if (ata_sg_setup(qc))
4128 goto sg_err;
4129 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4130 if (ata_sg_setup_one(qc))
4131 goto sg_err;
4133 } else {
4134 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4137 ap->ops->qc_prep(qc);
4139 qc->ap->active_tag = qc->tag;
4140 qc->flags |= ATA_QCFLAG_ACTIVE;
4142 return ap->ops->qc_issue(qc);
4144 sg_err:
4145 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4146 return AC_ERR_SYSTEM;
4151 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4152 * @qc: command to issue to device
4154 * Using various libata functions and hooks, this function
4155 * starts an ATA command. ATA commands are grouped into
4156 * classes called "protocols", and issuing each type of protocol
4157 * is slightly different.
4159 * May be used as the qc_issue() entry in ata_port_operations.
4161 * LOCKING:
4162 * spin_lock_irqsave(host_set lock)
4164 * RETURNS:
4165 * Zero on success, AC_ERR_* mask on failure
4168 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
4170 struct ata_port *ap = qc->ap;
4172 /* Use polling pio if the LLD doesn't handle
4173 * interrupt driven pio and atapi CDB interrupt.
4175 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4176 switch (qc->tf.protocol) {
4177 case ATA_PROT_PIO:
4178 case ATA_PROT_ATAPI:
4179 case ATA_PROT_ATAPI_NODATA:
4180 qc->tf.flags |= ATA_TFLAG_POLLING;
4181 break;
4182 case ATA_PROT_ATAPI_DMA:
4183 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
4184 BUG();
4185 break;
4186 default:
4187 break;
4191 /* select the device */
4192 ata_dev_select(ap, qc->dev->devno, 1, 0);
4194 /* start the command */
4195 switch (qc->tf.protocol) {
4196 case ATA_PROT_NODATA:
4197 if (qc->tf.flags & ATA_TFLAG_POLLING)
4198 ata_qc_set_polling(qc);
4200 ata_tf_to_host(ap, &qc->tf);
4201 ap->hsm_task_state = HSM_ST_LAST;
4203 if (qc->tf.flags & ATA_TFLAG_POLLING)
4204 ata_port_queue_task(ap, ata_pio_task, ap, 0);
4206 break;
4208 case ATA_PROT_DMA:
4209 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
4211 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4212 ap->ops->bmdma_setup(qc); /* set up bmdma */
4213 ap->ops->bmdma_start(qc); /* initiate bmdma */
4214 ap->hsm_task_state = HSM_ST_LAST;
4215 break;
4217 case ATA_PROT_PIO:
4218 if (qc->tf.flags & ATA_TFLAG_POLLING)
4219 ata_qc_set_polling(qc);
4221 ata_tf_to_host(ap, &qc->tf);
4223 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4224 /* PIO data out protocol */
4225 ap->hsm_task_state = HSM_ST_FIRST;
4226 ata_port_queue_task(ap, ata_pio_task, ap, 0);
4228 /* always send first data block using
4229 * the ata_pio_task() codepath.
4231 } else {
4232 /* PIO data in protocol */
4233 ap->hsm_task_state = HSM_ST;
4235 if (qc->tf.flags & ATA_TFLAG_POLLING)
4236 ata_port_queue_task(ap, ata_pio_task, ap, 0);
4238 /* if polling, ata_pio_task() handles the rest.
4239 * otherwise, interrupt handler takes over from here.
4243 break;
4245 case ATA_PROT_ATAPI:
4246 case ATA_PROT_ATAPI_NODATA:
4247 if (qc->tf.flags & ATA_TFLAG_POLLING)
4248 ata_qc_set_polling(qc);
4250 ata_tf_to_host(ap, &qc->tf);
4252 ap->hsm_task_state = HSM_ST_FIRST;
4254 /* send cdb by polling if no cdb interrupt */
4255 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4256 (qc->tf.flags & ATA_TFLAG_POLLING))
4257 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4258 break;
4260 case ATA_PROT_ATAPI_DMA:
4261 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
4263 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4264 ap->ops->bmdma_setup(qc); /* set up bmdma */
4265 ap->hsm_task_state = HSM_ST_FIRST;
4267 /* send cdb by polling if no cdb interrupt */
4268 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4269 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
4270 break;
4272 default:
4273 WARN_ON(1);
4274 return AC_ERR_SYSTEM;
4277 return 0;
4281 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
4282 * @qc: Info associated with this ATA transaction.
4284 * LOCKING:
4285 * spin_lock_irqsave(host_set lock)
4288 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4290 struct ata_port *ap = qc->ap;
4291 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4292 u8 dmactl;
4293 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4295 /* load PRD table addr. */
4296 mb(); /* make sure PRD table writes are visible to controller */
4297 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4299 /* specify data direction, triple-check start bit is clear */
4300 dmactl = readb(mmio + ATA_DMA_CMD);
4301 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4302 if (!rw)
4303 dmactl |= ATA_DMA_WR;
4304 writeb(dmactl, mmio + ATA_DMA_CMD);
4306 /* issue r/w command */
4307 ap->ops->exec_command(ap, &qc->tf);
4311 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
4312 * @qc: Info associated with this ATA transaction.
4314 * LOCKING:
4315 * spin_lock_irqsave(host_set lock)
4318 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4320 struct ata_port *ap = qc->ap;
4321 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4322 u8 dmactl;
4324 /* start host DMA transaction */
4325 dmactl = readb(mmio + ATA_DMA_CMD);
4326 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4328 /* Strictly, one may wish to issue a readb() here, to
4329 * flush the mmio write. However, control also passes
4330 * to the hardware at this point, and it will interrupt
4331 * us when we are to resume control. So, in effect,
4332 * we don't care when the mmio write flushes.
4333 * Further, a read of the DMA status register _immediately_
4334 * following the write may not be what certain flaky hardware
4335 * is expected, so I think it is best to not add a readb()
4336 * without first all the MMIO ATA cards/mobos.
4337 * Or maybe I'm just being paranoid.
4342 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4343 * @qc: Info associated with this ATA transaction.
4345 * LOCKING:
4346 * spin_lock_irqsave(host_set lock)
4349 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4351 struct ata_port *ap = qc->ap;
4352 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4353 u8 dmactl;
4355 /* load PRD table addr. */
4356 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4358 /* specify data direction, triple-check start bit is clear */
4359 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4360 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4361 if (!rw)
4362 dmactl |= ATA_DMA_WR;
4363 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4365 /* issue r/w command */
4366 ap->ops->exec_command(ap, &qc->tf);
4370 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4371 * @qc: Info associated with this ATA transaction.
4373 * LOCKING:
4374 * spin_lock_irqsave(host_set lock)
4377 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4379 struct ata_port *ap = qc->ap;
4380 u8 dmactl;
4382 /* start host DMA transaction */
4383 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4384 outb(dmactl | ATA_DMA_START,
4385 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4390 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4391 * @qc: Info associated with this ATA transaction.
4393 * Writes the ATA_DMA_START flag to the DMA command register.
4395 * May be used as the bmdma_start() entry in ata_port_operations.
4397 * LOCKING:
4398 * spin_lock_irqsave(host_set lock)
4400 void ata_bmdma_start(struct ata_queued_cmd *qc)
4402 if (qc->ap->flags & ATA_FLAG_MMIO)
4403 ata_bmdma_start_mmio(qc);
4404 else
4405 ata_bmdma_start_pio(qc);
4410 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4411 * @qc: Info associated with this ATA transaction.
4413 * Writes address of PRD table to device's PRD Table Address
4414 * register, sets the DMA control register, and calls
4415 * ops->exec_command() to start the transfer.
4417 * May be used as the bmdma_setup() entry in ata_port_operations.
4419 * LOCKING:
4420 * spin_lock_irqsave(host_set lock)
4422 void ata_bmdma_setup(struct ata_queued_cmd *qc)
4424 if (qc->ap->flags & ATA_FLAG_MMIO)
4425 ata_bmdma_setup_mmio(qc);
4426 else
4427 ata_bmdma_setup_pio(qc);
4432 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
4433 * @ap: Port associated with this ATA transaction.
4435 * Clear interrupt and error flags in DMA status register.
4437 * May be used as the irq_clear() entry in ata_port_operations.
4439 * LOCKING:
4440 * spin_lock_irqsave(host_set lock)
4443 void ata_bmdma_irq_clear(struct ata_port *ap)
4445 if (ap->flags & ATA_FLAG_MMIO) {
4446 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4447 writeb(readb(mmio), mmio);
4448 } else {
4449 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4450 outb(inb(addr), addr);
4457 * ata_bmdma_status - Read PCI IDE BMDMA status
4458 * @ap: Port associated with this ATA transaction.
4460 * Read and return BMDMA status register.
4462 * May be used as the bmdma_status() entry in ata_port_operations.
4464 * LOCKING:
4465 * spin_lock_irqsave(host_set lock)
4468 u8 ata_bmdma_status(struct ata_port *ap)
4470 u8 host_stat;
4471 if (ap->flags & ATA_FLAG_MMIO) {
4472 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4473 host_stat = readb(mmio + ATA_DMA_STATUS);
4474 } else
4475 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
4476 return host_stat;
4481 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
4482 * @qc: Command we are ending DMA for
4484 * Clears the ATA_DMA_START flag in the dma control register
4486 * May be used as the bmdma_stop() entry in ata_port_operations.
4488 * LOCKING:
4489 * spin_lock_irqsave(host_set lock)
4492 void ata_bmdma_stop(struct ata_queued_cmd *qc)
4494 struct ata_port *ap = qc->ap;
4495 if (ap->flags & ATA_FLAG_MMIO) {
4496 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4498 /* clear start/stop bit */
4499 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4500 mmio + ATA_DMA_CMD);
4501 } else {
4502 /* clear start/stop bit */
4503 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4504 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4507 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4508 ata_altstatus(ap); /* dummy read */
4512 * ata_host_intr - Handle host interrupt for given (port, task)
4513 * @ap: Port on which interrupt arrived (possibly...)
4514 * @qc: Taskfile currently active in engine
4516 * Handle host interrupt for given queued command. Currently,
4517 * only DMA interrupts are handled. All other commands are
4518 * handled via polling with interrupts disabled (nIEN bit).
4520 * LOCKING:
4521 * spin_lock_irqsave(host_set lock)
4523 * RETURNS:
4524 * One if interrupt was handled, zero if not (shared irq).
4527 inline unsigned int ata_host_intr (struct ata_port *ap,
4528 struct ata_queued_cmd *qc)
4530 u8 status, host_stat = 0;
4532 VPRINTK("ata%u: protocol %d task_state %d\n",
4533 ap->id, qc->tf.protocol, ap->hsm_task_state);
4535 /* Check whether we are expecting interrupt in this state */
4536 switch (ap->hsm_task_state) {
4537 case HSM_ST_FIRST:
4538 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4539 * The flag was turned on only for atapi devices.
4540 * No need to check is_atapi_taskfile(&qc->tf) again.
4542 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4543 goto idle_irq;
4544 break;
4545 case HSM_ST_LAST:
4546 if (qc->tf.protocol == ATA_PROT_DMA ||
4547 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4548 /* check status of DMA engine */
4549 host_stat = ap->ops->bmdma_status(ap);
4550 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4552 /* if it's not our irq... */
4553 if (!(host_stat & ATA_DMA_INTR))
4554 goto idle_irq;
4556 /* before we do anything else, clear DMA-Start bit */
4557 ap->ops->bmdma_stop(qc);
4559 if (unlikely(host_stat & ATA_DMA_ERR)) {
4560 /* error when transfering data to/from memory */
4561 qc->err_mask |= AC_ERR_HOST_BUS;
4562 ap->hsm_task_state = HSM_ST_ERR;
4565 break;
4566 case HSM_ST:
4567 break;
4568 default:
4569 goto idle_irq;
4572 /* check altstatus */
4573 status = ata_altstatus(ap);
4574 if (status & ATA_BUSY)
4575 goto idle_irq;
4577 /* check main status, clearing INTRQ */
4578 status = ata_chk_status(ap);
4579 if (unlikely(status & ATA_BUSY))
4580 goto idle_irq;
4582 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4583 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4585 /* ack bmdma irq events */
4586 ap->ops->irq_clear(ap);
4588 /* check error */
4589 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4590 qc->err_mask |= AC_ERR_DEV;
4591 ap->hsm_task_state = HSM_ST_ERR;
4594 fsm_start:
4595 switch (ap->hsm_task_state) {
4596 case HSM_ST_FIRST:
4597 /* Some pre-ATAPI-4 devices assert INTRQ
4598 * at this state when ready to receive CDB.
4601 /* check device status */
4602 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4603 /* Wrong status. Let EH handle this */
4604 qc->err_mask |= AC_ERR_HSM;
4605 ap->hsm_task_state = HSM_ST_ERR;
4606 goto fsm_start;
4609 atapi_send_cdb(ap, qc);
4611 break;
4613 case HSM_ST:
4614 /* complete command or read/write the data register */
4615 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4616 /* ATAPI PIO protocol */
4617 if ((status & ATA_DRQ) == 0) {
4618 /* no more data to transfer */
4619 ap->hsm_task_state = HSM_ST_LAST;
4620 goto fsm_start;
4623 atapi_pio_bytes(qc);
4625 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4626 /* bad ireason reported by device */
4627 goto fsm_start;
4629 } else {
4630 /* ATA PIO protocol */
4631 if (unlikely((status & ATA_DRQ) == 0)) {
4632 /* handle BSY=0, DRQ=0 as error */
4633 qc->err_mask |= AC_ERR_HSM;
4634 ap->hsm_task_state = HSM_ST_ERR;
4635 goto fsm_start;
4638 ata_pio_sectors(qc);
4640 if (ap->hsm_task_state == HSM_ST_LAST &&
4641 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4642 /* all data read */
4643 ata_altstatus(ap);
4644 status = ata_chk_status(ap);
4645 goto fsm_start;
4649 ata_altstatus(ap); /* flush */
4650 break;
4652 case HSM_ST_LAST:
4653 if (unlikely(status & ATA_DRQ)) {
4654 /* handle DRQ=1 as error */
4655 qc->err_mask |= AC_ERR_HSM;
4656 ap->hsm_task_state = HSM_ST_ERR;
4657 goto fsm_start;
4660 /* no more data to transfer */
4661 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4662 ap->id, status);
4664 ap->hsm_task_state = HSM_ST_IDLE;
4666 /* complete taskfile transaction */
4667 qc->err_mask |= ac_err_mask(status);
4668 ata_qc_complete(qc);
4669 break;
4671 case HSM_ST_ERR:
4672 if (qc->tf.command != ATA_CMD_PACKET)
4673 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4674 ap->id, status, host_stat);
4676 /* make sure qc->err_mask is available to
4677 * know what's wrong and recover
4679 WARN_ON(qc->err_mask == 0);
4681 ap->hsm_task_state = HSM_ST_IDLE;
4682 ata_qc_complete(qc);
4683 break;
4684 default:
4685 goto idle_irq;
4688 return 1; /* irq handled */
4690 idle_irq:
4691 ap->stats.idle_irq++;
4693 #ifdef ATA_IRQ_TRAP
4694 if ((ap->stats.idle_irq % 1000) == 0) {
4695 handled = 1;
4696 ata_irq_ack(ap, 0); /* debug trap */
4697 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4699 #endif
4700 return 0; /* irq not handled */
4704 * ata_interrupt - Default ATA host interrupt handler
4705 * @irq: irq line (unused)
4706 * @dev_instance: pointer to our ata_host_set information structure
4707 * @regs: unused
4709 * Default interrupt handler for PCI IDE devices. Calls
4710 * ata_host_intr() for each port that is not disabled.
4712 * LOCKING:
4713 * Obtains host_set lock during operation.
4715 * RETURNS:
4716 * IRQ_NONE or IRQ_HANDLED.
4719 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4721 struct ata_host_set *host_set = dev_instance;
4722 unsigned int i;
4723 unsigned int handled = 0;
4724 unsigned long flags;
4726 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4727 spin_lock_irqsave(&host_set->lock, flags);
4729 for (i = 0; i < host_set->n_ports; i++) {
4730 struct ata_port *ap;
4732 ap = host_set->ports[i];
4733 if (ap &&
4734 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
4735 struct ata_queued_cmd *qc;
4737 qc = ata_qc_from_tag(ap, ap->active_tag);
4738 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
4739 (qc->flags & ATA_QCFLAG_ACTIVE))
4740 handled |= ata_host_intr(ap, qc);
4744 spin_unlock_irqrestore(&host_set->lock, flags);
4746 return IRQ_RETVAL(handled);
4751 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4752 * without filling any other registers
4754 static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4755 u8 cmd)
4757 struct ata_taskfile tf;
4758 int err;
4760 ata_tf_init(ap, &tf, dev->devno);
4762 tf.command = cmd;
4763 tf.flags |= ATA_TFLAG_DEVICE;
4764 tf.protocol = ATA_PROT_NODATA;
4766 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4767 if (err)
4768 printk(KERN_ERR "%s: ata command failed: %d\n",
4769 __FUNCTION__, err);
4771 return err;
4774 static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4776 u8 cmd;
4778 if (!ata_try_flush_cache(dev))
4779 return 0;
4781 if (ata_id_has_flush_ext(dev->id))
4782 cmd = ATA_CMD_FLUSH_EXT;
4783 else
4784 cmd = ATA_CMD_FLUSH;
4786 return ata_do_simple_cmd(ap, dev, cmd);
4789 static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4791 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4794 static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4796 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4800 * ata_device_resume - wakeup a previously suspended devices
4801 * @ap: port the device is connected to
4802 * @dev: the device to resume
4804 * Kick the drive back into action, by sending it an idle immediate
4805 * command and making sure its transfer mode matches between drive
4806 * and host.
4809 int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4811 if (ap->flags & ATA_FLAG_SUSPENDED) {
4812 ap->flags &= ~ATA_FLAG_SUSPENDED;
4813 ata_set_mode(ap);
4815 if (!ata_dev_present(dev))
4816 return 0;
4817 if (dev->class == ATA_DEV_ATA)
4818 ata_start_drive(ap, dev);
4820 return 0;
4824 * ata_device_suspend - prepare a device for suspend
4825 * @ap: port the device is connected to
4826 * @dev: the device to suspend
4828 * Flush the cache on the drive, if appropriate, then issue a
4829 * standbynow command.
4831 int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4833 if (!ata_dev_present(dev))
4834 return 0;
4835 if (dev->class == ATA_DEV_ATA)
4836 ata_flush_cache(ap, dev);
4838 ata_standby_drive(ap, dev);
4839 ap->flags |= ATA_FLAG_SUSPENDED;
4840 return 0;
4844 * ata_port_start - Set port up for dma.
4845 * @ap: Port to initialize
4847 * Called just after data structures for each port are
4848 * initialized. Allocates space for PRD table.
4850 * May be used as the port_start() entry in ata_port_operations.
4852 * LOCKING:
4853 * Inherited from caller.
4856 int ata_port_start (struct ata_port *ap)
4858 struct device *dev = ap->host_set->dev;
4859 int rc;
4861 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4862 if (!ap->prd)
4863 return -ENOMEM;
4865 rc = ata_pad_alloc(ap, dev);
4866 if (rc) {
4867 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4868 return rc;
4871 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4873 return 0;
4878 * ata_port_stop - Undo ata_port_start()
4879 * @ap: Port to shut down
4881 * Frees the PRD table.
4883 * May be used as the port_stop() entry in ata_port_operations.
4885 * LOCKING:
4886 * Inherited from caller.
4889 void ata_port_stop (struct ata_port *ap)
4891 struct device *dev = ap->host_set->dev;
4893 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4894 ata_pad_free(ap, dev);
4897 void ata_host_stop (struct ata_host_set *host_set)
4899 if (host_set->mmio_base)
4900 iounmap(host_set->mmio_base);
4905 * ata_host_remove - Unregister SCSI host structure with upper layers
4906 * @ap: Port to unregister
4907 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4909 * LOCKING:
4910 * Inherited from caller.
4913 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4915 struct Scsi_Host *sh = ap->host;
4917 DPRINTK("ENTER\n");
4919 if (do_unregister)
4920 scsi_remove_host(sh);
4922 ap->ops->port_stop(ap);
4926 * ata_host_init - Initialize an ata_port structure
4927 * @ap: Structure to initialize
4928 * @host: associated SCSI mid-layer structure
4929 * @host_set: Collection of hosts to which @ap belongs
4930 * @ent: Probe information provided by low-level driver
4931 * @port_no: Port number associated with this ata_port
4933 * Initialize a new ata_port structure, and its associated
4934 * scsi_host.
4936 * LOCKING:
4937 * Inherited from caller.
4940 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4941 struct ata_host_set *host_set,
4942 const struct ata_probe_ent *ent, unsigned int port_no)
4944 unsigned int i;
4946 host->max_id = 16;
4947 host->max_lun = 1;
4948 host->max_channel = 1;
4949 host->unique_id = ata_unique_id++;
4950 host->max_cmd_len = 12;
4952 ap->flags = ATA_FLAG_PORT_DISABLED;
4953 ap->id = host->unique_id;
4954 ap->host = host;
4955 ap->ctl = ATA_DEVCTL_OBS;
4956 ap->host_set = host_set;
4957 ap->port_no = port_no;
4958 ap->hard_port_no =
4959 ent->legacy_mode ? ent->hard_port_no : port_no;
4960 ap->pio_mask = ent->pio_mask;
4961 ap->mwdma_mask = ent->mwdma_mask;
4962 ap->udma_mask = ent->udma_mask;
4963 ap->flags |= ent->host_flags;
4964 ap->ops = ent->port_ops;
4965 ap->cbl = ATA_CBL_NONE;
4966 ap->active_tag = ATA_TAG_POISON;
4967 ap->last_ctl = 0xFF;
4969 INIT_WORK(&ap->port_task, NULL, NULL);
4970 INIT_LIST_HEAD(&ap->eh_done_q);
4972 for (i = 0; i < ATA_MAX_DEVICES; i++)
4973 ap->device[i].devno = i;
4975 #ifdef ATA_IRQ_TRAP
4976 ap->stats.unhandled_irq = 1;
4977 ap->stats.idle_irq = 1;
4978 #endif
4980 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4984 * ata_host_add - Attach low-level ATA driver to system
4985 * @ent: Information provided by low-level driver
4986 * @host_set: Collections of ports to which we add
4987 * @port_no: Port number associated with this host
4989 * Attach low-level ATA driver to system.
4991 * LOCKING:
4992 * PCI/etc. bus probe sem.
4994 * RETURNS:
4995 * New ata_port on success, for NULL on error.
4998 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4999 struct ata_host_set *host_set,
5000 unsigned int port_no)
5002 struct Scsi_Host *host;
5003 struct ata_port *ap;
5004 int rc;
5006 DPRINTK("ENTER\n");
5007 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
5008 if (!host)
5009 return NULL;
5011 ap = (struct ata_port *) &host->hostdata[0];
5013 ata_host_init(ap, host, host_set, ent, port_no);
5015 rc = ap->ops->port_start(ap);
5016 if (rc)
5017 goto err_out;
5019 return ap;
5021 err_out:
5022 scsi_host_put(host);
5023 return NULL;
5027 * ata_device_add - Register hardware device with ATA and SCSI layers
5028 * @ent: Probe information describing hardware device to be registered
5030 * This function processes the information provided in the probe
5031 * information struct @ent, allocates the necessary ATA and SCSI
5032 * host information structures, initializes them, and registers
5033 * everything with requisite kernel subsystems.
5035 * This function requests irqs, probes the ATA bus, and probes
5036 * the SCSI bus.
5038 * LOCKING:
5039 * PCI/etc. bus probe sem.
5041 * RETURNS:
5042 * Number of ports registered. Zero on error (no ports registered).
5045 int ata_device_add(const struct ata_probe_ent *ent)
5047 unsigned int count = 0, i;
5048 struct device *dev = ent->dev;
5049 struct ata_host_set *host_set;
5051 DPRINTK("ENTER\n");
5052 /* alloc a container for our list of ATA ports (buses) */
5053 host_set = kzalloc(sizeof(struct ata_host_set) +
5054 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
5055 if (!host_set)
5056 return 0;
5057 spin_lock_init(&host_set->lock);
5059 host_set->dev = dev;
5060 host_set->n_ports = ent->n_ports;
5061 host_set->irq = ent->irq;
5062 host_set->mmio_base = ent->mmio_base;
5063 host_set->private_data = ent->private_data;
5064 host_set->ops = ent->port_ops;
5066 /* register each port bound to this device */
5067 for (i = 0; i < ent->n_ports; i++) {
5068 struct ata_port *ap;
5069 unsigned long xfer_mode_mask;
5071 ap = ata_host_add(ent, host_set, i);
5072 if (!ap)
5073 goto err_out;
5075 host_set->ports[i] = ap;
5076 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
5077 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
5078 (ap->pio_mask << ATA_SHIFT_PIO);
5080 /* print per-port info to dmesg */
5081 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
5082 "bmdma 0x%lX irq %lu\n",
5083 ap->id,
5084 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
5085 ata_mode_string(xfer_mode_mask),
5086 ap->ioaddr.cmd_addr,
5087 ap->ioaddr.ctl_addr,
5088 ap->ioaddr.bmdma_addr,
5089 ent->irq);
5091 ata_chk_status(ap);
5092 host_set->ops->irq_clear(ap);
5093 count++;
5096 if (!count)
5097 goto err_free_ret;
5099 /* obtain irq, that is shared between channels */
5100 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
5101 DRV_NAME, host_set))
5102 goto err_out;
5104 /* perform each probe synchronously */
5105 DPRINTK("probe begin\n");
5106 for (i = 0; i < count; i++) {
5107 struct ata_port *ap;
5108 int rc;
5110 ap = host_set->ports[i];
5112 DPRINTK("ata%u: bus probe begin\n", ap->id);
5113 rc = ata_bus_probe(ap);
5114 DPRINTK("ata%u: bus probe end\n", ap->id);
5116 if (rc) {
5117 /* FIXME: do something useful here?
5118 * Current libata behavior will
5119 * tear down everything when
5120 * the module is removed
5121 * or the h/w is unplugged.
5125 rc = scsi_add_host(ap->host, dev);
5126 if (rc) {
5127 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
5128 ap->id);
5129 /* FIXME: do something useful here */
5130 /* FIXME: handle unconditional calls to
5131 * scsi_scan_host and ata_host_remove, below,
5132 * at the very least
5137 /* probes are done, now scan each port's disk(s) */
5138 DPRINTK("host probe begin\n");
5139 for (i = 0; i < count; i++) {
5140 struct ata_port *ap = host_set->ports[i];
5142 ata_scsi_scan_host(ap);
5145 dev_set_drvdata(dev, host_set);
5147 VPRINTK("EXIT, returning %u\n", ent->n_ports);
5148 return ent->n_ports; /* success */
5150 err_out:
5151 for (i = 0; i < count; i++) {
5152 ata_host_remove(host_set->ports[i], 1);
5153 scsi_host_put(host_set->ports[i]->host);
5155 err_free_ret:
5156 kfree(host_set);
5157 VPRINTK("EXIT, returning 0\n");
5158 return 0;
5162 * ata_host_set_remove - PCI layer callback for device removal
5163 * @host_set: ATA host set that was removed
5165 * Unregister all objects associated with this host set. Free those
5166 * objects.
5168 * LOCKING:
5169 * Inherited from calling layer (may sleep).
5172 void ata_host_set_remove(struct ata_host_set *host_set)
5174 struct ata_port *ap;
5175 unsigned int i;
5177 for (i = 0; i < host_set->n_ports; i++) {
5178 ap = host_set->ports[i];
5179 scsi_remove_host(ap->host);
5182 free_irq(host_set->irq, host_set);
5184 for (i = 0; i < host_set->n_ports; i++) {
5185 ap = host_set->ports[i];
5187 ata_scsi_release(ap->host);
5189 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5190 struct ata_ioports *ioaddr = &ap->ioaddr;
5192 if (ioaddr->cmd_addr == 0x1f0)
5193 release_region(0x1f0, 8);
5194 else if (ioaddr->cmd_addr == 0x170)
5195 release_region(0x170, 8);
5198 scsi_host_put(ap->host);
5201 if (host_set->ops->host_stop)
5202 host_set->ops->host_stop(host_set);
5204 kfree(host_set);
5208 * ata_scsi_release - SCSI layer callback hook for host unload
5209 * @host: libata host to be unloaded
5211 * Performs all duties necessary to shut down a libata port...
5212 * Kill port kthread, disable port, and release resources.
5214 * LOCKING:
5215 * Inherited from SCSI layer.
5217 * RETURNS:
5218 * One.
5221 int ata_scsi_release(struct Scsi_Host *host)
5223 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
5224 int i;
5226 DPRINTK("ENTER\n");
5228 ap->ops->port_disable(ap);
5229 ata_host_remove(ap, 0);
5230 for (i = 0; i < ATA_MAX_DEVICES; i++)
5231 kfree(ap->device[i].id);
5233 DPRINTK("EXIT\n");
5234 return 1;
5238 * ata_std_ports - initialize ioaddr with standard port offsets.
5239 * @ioaddr: IO address structure to be initialized
5241 * Utility function which initializes data_addr, error_addr,
5242 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5243 * device_addr, status_addr, and command_addr to standard offsets
5244 * relative to cmd_addr.
5246 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
5249 void ata_std_ports(struct ata_ioports *ioaddr)
5251 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5252 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5253 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5254 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5255 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5256 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5257 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5258 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5259 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5260 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5264 #ifdef CONFIG_PCI
5266 void ata_pci_host_stop (struct ata_host_set *host_set)
5268 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5270 pci_iounmap(pdev, host_set->mmio_base);
5274 * ata_pci_remove_one - PCI layer callback for device removal
5275 * @pdev: PCI device that was removed
5277 * PCI layer indicates to libata via this hook that
5278 * hot-unplug or module unload event has occurred.
5279 * Handle this by unregistering all objects associated
5280 * with this PCI device. Free those objects. Then finally
5281 * release PCI resources and disable device.
5283 * LOCKING:
5284 * Inherited from PCI layer (may sleep).
5287 void ata_pci_remove_one (struct pci_dev *pdev)
5289 struct device *dev = pci_dev_to_dev(pdev);
5290 struct ata_host_set *host_set = dev_get_drvdata(dev);
5292 ata_host_set_remove(host_set);
5293 pci_release_regions(pdev);
5294 pci_disable_device(pdev);
5295 dev_set_drvdata(dev, NULL);
5298 /* move to PCI subsystem */
5299 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5301 unsigned long tmp = 0;
5303 switch (bits->width) {
5304 case 1: {
5305 u8 tmp8 = 0;
5306 pci_read_config_byte(pdev, bits->reg, &tmp8);
5307 tmp = tmp8;
5308 break;
5310 case 2: {
5311 u16 tmp16 = 0;
5312 pci_read_config_word(pdev, bits->reg, &tmp16);
5313 tmp = tmp16;
5314 break;
5316 case 4: {
5317 u32 tmp32 = 0;
5318 pci_read_config_dword(pdev, bits->reg, &tmp32);
5319 tmp = tmp32;
5320 break;
5323 default:
5324 return -EINVAL;
5327 tmp &= bits->mask;
5329 return (tmp == bits->val) ? 1 : 0;
5332 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5334 pci_save_state(pdev);
5335 pci_disable_device(pdev);
5336 pci_set_power_state(pdev, PCI_D3hot);
5337 return 0;
5340 int ata_pci_device_resume(struct pci_dev *pdev)
5342 pci_set_power_state(pdev, PCI_D0);
5343 pci_restore_state(pdev);
5344 pci_enable_device(pdev);
5345 pci_set_master(pdev);
5346 return 0;
5348 #endif /* CONFIG_PCI */
5351 static int __init ata_init(void)
5353 ata_wq = create_workqueue("ata");
5354 if (!ata_wq)
5355 return -ENOMEM;
5357 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5358 return 0;
5361 static void __exit ata_exit(void)
5363 destroy_workqueue(ata_wq);
5366 module_init(ata_init);
5367 module_exit(ata_exit);
5369 static unsigned long ratelimit_time;
5370 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5372 int ata_ratelimit(void)
5374 int rc;
5375 unsigned long flags;
5377 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5379 if (time_after(jiffies, ratelimit_time)) {
5380 rc = 1;
5381 ratelimit_time = jiffies + (HZ/5);
5382 } else
5383 rc = 0;
5385 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5387 return rc;
5391 * libata is essentially a library of internal helper functions for
5392 * low-level ATA host controller drivers. As such, the API/ABI is
5393 * likely to change as new drivers are added and updated.
5394 * Do not depend on ABI/API stability.
5397 EXPORT_SYMBOL_GPL(ata_std_bios_param);
5398 EXPORT_SYMBOL_GPL(ata_std_ports);
5399 EXPORT_SYMBOL_GPL(ata_device_add);
5400 EXPORT_SYMBOL_GPL(ata_host_set_remove);
5401 EXPORT_SYMBOL_GPL(ata_sg_init);
5402 EXPORT_SYMBOL_GPL(ata_sg_init_one);
5403 EXPORT_SYMBOL_GPL(__ata_qc_complete);
5404 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5405 EXPORT_SYMBOL_GPL(ata_eng_timeout);
5406 EXPORT_SYMBOL_GPL(ata_tf_load);
5407 EXPORT_SYMBOL_GPL(ata_tf_read);
5408 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5409 EXPORT_SYMBOL_GPL(ata_std_dev_select);
5410 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5411 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5412 EXPORT_SYMBOL_GPL(ata_check_status);
5413 EXPORT_SYMBOL_GPL(ata_altstatus);
5414 EXPORT_SYMBOL_GPL(ata_exec_command);
5415 EXPORT_SYMBOL_GPL(ata_port_start);
5416 EXPORT_SYMBOL_GPL(ata_port_stop);
5417 EXPORT_SYMBOL_GPL(ata_host_stop);
5418 EXPORT_SYMBOL_GPL(ata_interrupt);
5419 EXPORT_SYMBOL_GPL(ata_qc_prep);
5420 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5421 EXPORT_SYMBOL_GPL(ata_bmdma_start);
5422 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5423 EXPORT_SYMBOL_GPL(ata_bmdma_status);
5424 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5425 EXPORT_SYMBOL_GPL(ata_port_probe);
5426 EXPORT_SYMBOL_GPL(sata_phy_reset);
5427 EXPORT_SYMBOL_GPL(__sata_phy_reset);
5428 EXPORT_SYMBOL_GPL(ata_bus_reset);
5429 EXPORT_SYMBOL_GPL(ata_std_probeinit);
5430 EXPORT_SYMBOL_GPL(ata_std_softreset);
5431 EXPORT_SYMBOL_GPL(sata_std_hardreset);
5432 EXPORT_SYMBOL_GPL(ata_std_postreset);
5433 EXPORT_SYMBOL_GPL(ata_std_probe_reset);
5434 EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
5435 EXPORT_SYMBOL_GPL(ata_dev_revalidate);
5436 EXPORT_SYMBOL_GPL(ata_port_disable);
5437 EXPORT_SYMBOL_GPL(ata_ratelimit);
5438 EXPORT_SYMBOL_GPL(ata_busy_sleep);
5439 EXPORT_SYMBOL_GPL(ata_port_queue_task);
5440 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5441 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5442 EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
5443 EXPORT_SYMBOL_GPL(ata_scsi_error);
5444 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5445 EXPORT_SYMBOL_GPL(ata_scsi_release);
5446 EXPORT_SYMBOL_GPL(ata_host_intr);
5447 EXPORT_SYMBOL_GPL(ata_dev_classify);
5448 EXPORT_SYMBOL_GPL(ata_id_string);
5449 EXPORT_SYMBOL_GPL(ata_id_c_string);
5450 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5451 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5452 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
5454 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
5455 EXPORT_SYMBOL_GPL(ata_timing_compute);
5456 EXPORT_SYMBOL_GPL(ata_timing_merge);
5458 #ifdef CONFIG_PCI
5459 EXPORT_SYMBOL_GPL(pci_test_config_bits);
5460 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
5461 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5462 EXPORT_SYMBOL_GPL(ata_pci_init_one);
5463 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5464 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5465 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
5466 #endif /* CONFIG_PCI */
5468 EXPORT_SYMBOL_GPL(ata_device_suspend);
5469 EXPORT_SYMBOL_GPL(ata_device_resume);
5470 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5471 EXPORT_SYMBOL_GPL(ata_scsi_device_resume);