[PATCH] libata: fix comments on ata_tf_from_fis()
[linux-2.6/kvm.git] / drivers / scsi / libata-core.c
blobd81db3a3d4b9a82a916659574a20f35328329ab4
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_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
66 unsigned long tmout);
67 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
68 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
69 static void ata_set_mode(struct ata_port *ap);
70 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
71 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
72 static int fgb(u32 bitmap);
73 static int ata_choose_xfer_mode(const struct ata_port *ap,
74 u8 *xfer_mode_out,
75 unsigned int *xfer_shift_out);
76 static void __ata_qc_complete(struct ata_queued_cmd *qc);
78 static unsigned int ata_unique_id = 1;
79 static struct workqueue_struct *ata_wq;
81 int atapi_enabled = 0;
82 module_param(atapi_enabled, int, 0444);
83 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
85 MODULE_AUTHOR("Jeff Garzik");
86 MODULE_DESCRIPTION("Library module for ATA devices");
87 MODULE_LICENSE("GPL");
88 MODULE_VERSION(DRV_VERSION);
90 /**
91 * ata_tf_load_pio - send taskfile registers to host controller
92 * @ap: Port to which output is sent
93 * @tf: ATA taskfile register set
95 * Outputs ATA taskfile to standard ATA host controller.
97 * LOCKING:
98 * Inherited from caller.
101 static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
103 struct ata_ioports *ioaddr = &ap->ioaddr;
104 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
106 if (tf->ctl != ap->last_ctl) {
107 outb(tf->ctl, ioaddr->ctl_addr);
108 ap->last_ctl = tf->ctl;
109 ata_wait_idle(ap);
112 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
113 outb(tf->hob_feature, ioaddr->feature_addr);
114 outb(tf->hob_nsect, ioaddr->nsect_addr);
115 outb(tf->hob_lbal, ioaddr->lbal_addr);
116 outb(tf->hob_lbam, ioaddr->lbam_addr);
117 outb(tf->hob_lbah, ioaddr->lbah_addr);
118 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
119 tf->hob_feature,
120 tf->hob_nsect,
121 tf->hob_lbal,
122 tf->hob_lbam,
123 tf->hob_lbah);
126 if (is_addr) {
127 outb(tf->feature, ioaddr->feature_addr);
128 outb(tf->nsect, ioaddr->nsect_addr);
129 outb(tf->lbal, ioaddr->lbal_addr);
130 outb(tf->lbam, ioaddr->lbam_addr);
131 outb(tf->lbah, ioaddr->lbah_addr);
132 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
133 tf->feature,
134 tf->nsect,
135 tf->lbal,
136 tf->lbam,
137 tf->lbah);
140 if (tf->flags & ATA_TFLAG_DEVICE) {
141 outb(tf->device, ioaddr->device_addr);
142 VPRINTK("device 0x%X\n", tf->device);
145 ata_wait_idle(ap);
149 * ata_tf_load_mmio - send taskfile registers to host controller
150 * @ap: Port to which output is sent
151 * @tf: ATA taskfile register set
153 * Outputs ATA taskfile to standard ATA host controller using MMIO.
155 * LOCKING:
156 * Inherited from caller.
159 static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
161 struct ata_ioports *ioaddr = &ap->ioaddr;
162 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
164 if (tf->ctl != ap->last_ctl) {
165 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
166 ap->last_ctl = tf->ctl;
167 ata_wait_idle(ap);
170 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
171 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
172 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
173 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
174 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
175 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
176 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
177 tf->hob_feature,
178 tf->hob_nsect,
179 tf->hob_lbal,
180 tf->hob_lbam,
181 tf->hob_lbah);
184 if (is_addr) {
185 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
186 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
187 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
188 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
189 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
190 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
191 tf->feature,
192 tf->nsect,
193 tf->lbal,
194 tf->lbam,
195 tf->lbah);
198 if (tf->flags & ATA_TFLAG_DEVICE) {
199 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
200 VPRINTK("device 0x%X\n", tf->device);
203 ata_wait_idle(ap);
208 * ata_tf_load - send taskfile registers to host controller
209 * @ap: Port to which output is sent
210 * @tf: ATA taskfile register set
212 * Outputs ATA taskfile to standard ATA host controller using MMIO
213 * or PIO as indicated by the ATA_FLAG_MMIO flag.
214 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
215 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
216 * hob_lbal, hob_lbam, and hob_lbah.
218 * This function waits for idle (!BUSY and !DRQ) after writing
219 * registers. If the control register has a new value, this
220 * function also waits for idle after writing control and before
221 * writing the remaining registers.
223 * May be used as the tf_load() entry in ata_port_operations.
225 * LOCKING:
226 * Inherited from caller.
228 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
230 if (ap->flags & ATA_FLAG_MMIO)
231 ata_tf_load_mmio(ap, tf);
232 else
233 ata_tf_load_pio(ap, tf);
237 * ata_exec_command_pio - issue ATA command to host controller
238 * @ap: port to which command is being issued
239 * @tf: ATA taskfile register set
241 * Issues PIO write to ATA command register, with proper
242 * synchronization with interrupt handler / other threads.
244 * LOCKING:
245 * spin_lock_irqsave(host_set lock)
248 static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
250 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
252 outb(tf->command, ap->ioaddr.command_addr);
253 ata_pause(ap);
258 * ata_exec_command_mmio - issue ATA command to host controller
259 * @ap: port to which command is being issued
260 * @tf: ATA taskfile register set
262 * Issues MMIO write to ATA command register, with proper
263 * synchronization with interrupt handler / other threads.
265 * LOCKING:
266 * spin_lock_irqsave(host_set lock)
269 static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
271 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
273 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
274 ata_pause(ap);
279 * ata_exec_command - issue ATA command to host controller
280 * @ap: port to which command is being issued
281 * @tf: ATA taskfile register set
283 * Issues PIO/MMIO write to ATA command register, with proper
284 * synchronization with interrupt handler / other threads.
286 * LOCKING:
287 * spin_lock_irqsave(host_set lock)
289 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
291 if (ap->flags & ATA_FLAG_MMIO)
292 ata_exec_command_mmio(ap, tf);
293 else
294 ata_exec_command_pio(ap, tf);
298 * ata_tf_to_host - issue ATA taskfile to host controller
299 * @ap: port to which command is being issued
300 * @tf: ATA taskfile register set
302 * Issues ATA taskfile register set to ATA host controller,
303 * with proper synchronization with interrupt handler and
304 * other threads.
306 * LOCKING:
307 * spin_lock_irqsave(host_set lock)
310 static inline void ata_tf_to_host(struct ata_port *ap,
311 const struct ata_taskfile *tf)
313 ap->ops->tf_load(ap, tf);
314 ap->ops->exec_command(ap, tf);
318 * ata_tf_read_pio - input device's ATA taskfile shadow registers
319 * @ap: Port from which input is read
320 * @tf: ATA taskfile register set for storing input
322 * Reads ATA taskfile registers for currently-selected device
323 * into @tf.
325 * LOCKING:
326 * Inherited from caller.
329 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
331 struct ata_ioports *ioaddr = &ap->ioaddr;
333 tf->command = ata_check_status(ap);
334 tf->feature = inb(ioaddr->error_addr);
335 tf->nsect = inb(ioaddr->nsect_addr);
336 tf->lbal = inb(ioaddr->lbal_addr);
337 tf->lbam = inb(ioaddr->lbam_addr);
338 tf->lbah = inb(ioaddr->lbah_addr);
339 tf->device = inb(ioaddr->device_addr);
341 if (tf->flags & ATA_TFLAG_LBA48) {
342 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
343 tf->hob_feature = inb(ioaddr->error_addr);
344 tf->hob_nsect = inb(ioaddr->nsect_addr);
345 tf->hob_lbal = inb(ioaddr->lbal_addr);
346 tf->hob_lbam = inb(ioaddr->lbam_addr);
347 tf->hob_lbah = inb(ioaddr->lbah_addr);
352 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
353 * @ap: Port from which input is read
354 * @tf: ATA taskfile register set for storing input
356 * Reads ATA taskfile registers for currently-selected device
357 * into @tf via MMIO.
359 * LOCKING:
360 * Inherited from caller.
363 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
365 struct ata_ioports *ioaddr = &ap->ioaddr;
367 tf->command = ata_check_status(ap);
368 tf->feature = readb((void __iomem *)ioaddr->error_addr);
369 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
370 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
371 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
372 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
373 tf->device = readb((void __iomem *)ioaddr->device_addr);
375 if (tf->flags & ATA_TFLAG_LBA48) {
376 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
377 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
378 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
379 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
380 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
381 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
387 * ata_tf_read - input device's ATA taskfile shadow registers
388 * @ap: Port from which input is read
389 * @tf: ATA taskfile register set for storing input
391 * Reads ATA taskfile registers for currently-selected device
392 * into @tf.
394 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
395 * is set, also reads the hob registers.
397 * May be used as the tf_read() entry in ata_port_operations.
399 * LOCKING:
400 * Inherited from caller.
402 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
404 if (ap->flags & ATA_FLAG_MMIO)
405 ata_tf_read_mmio(ap, tf);
406 else
407 ata_tf_read_pio(ap, tf);
411 * ata_check_status_pio - Read device status reg & clear interrupt
412 * @ap: port where the device is
414 * Reads ATA taskfile status register for currently-selected device
415 * and return its value. This also clears pending interrupts
416 * from this device
418 * LOCKING:
419 * Inherited from caller.
421 static u8 ata_check_status_pio(struct ata_port *ap)
423 return inb(ap->ioaddr.status_addr);
427 * ata_check_status_mmio - Read device status reg & clear interrupt
428 * @ap: port where the device is
430 * Reads ATA taskfile status register for currently-selected device
431 * via MMIO and return its value. This also clears pending interrupts
432 * from this device
434 * LOCKING:
435 * Inherited from caller.
437 static u8 ata_check_status_mmio(struct ata_port *ap)
439 return readb((void __iomem *) ap->ioaddr.status_addr);
444 * ata_check_status - Read device status reg & clear interrupt
445 * @ap: port where the device is
447 * Reads ATA taskfile status register for currently-selected device
448 * and return its value. This also clears pending interrupts
449 * from this device
451 * May be used as the check_status() entry in ata_port_operations.
453 * LOCKING:
454 * Inherited from caller.
456 u8 ata_check_status(struct ata_port *ap)
458 if (ap->flags & ATA_FLAG_MMIO)
459 return ata_check_status_mmio(ap);
460 return ata_check_status_pio(ap);
465 * ata_altstatus - Read device alternate status reg
466 * @ap: port where the device is
468 * Reads ATA taskfile alternate status register for
469 * currently-selected device and return its value.
471 * Note: may NOT be used as the check_altstatus() entry in
472 * ata_port_operations.
474 * LOCKING:
475 * Inherited from caller.
477 u8 ata_altstatus(struct ata_port *ap)
479 if (ap->ops->check_altstatus)
480 return ap->ops->check_altstatus(ap);
482 if (ap->flags & ATA_FLAG_MMIO)
483 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
484 return inb(ap->ioaddr.altstatus_addr);
489 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
490 * @tf: Taskfile to convert
491 * @fis: Buffer into which data will output
492 * @pmp: Port multiplier port
494 * Converts a standard ATA taskfile to a Serial ATA
495 * FIS structure (Register - Host to Device).
497 * LOCKING:
498 * Inherited from caller.
501 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
503 fis[0] = 0x27; /* Register - Host to Device FIS */
504 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
505 bit 7 indicates Command FIS */
506 fis[2] = tf->command;
507 fis[3] = tf->feature;
509 fis[4] = tf->lbal;
510 fis[5] = tf->lbam;
511 fis[6] = tf->lbah;
512 fis[7] = tf->device;
514 fis[8] = tf->hob_lbal;
515 fis[9] = tf->hob_lbam;
516 fis[10] = tf->hob_lbah;
517 fis[11] = tf->hob_feature;
519 fis[12] = tf->nsect;
520 fis[13] = tf->hob_nsect;
521 fis[14] = 0;
522 fis[15] = tf->ctl;
524 fis[16] = 0;
525 fis[17] = 0;
526 fis[18] = 0;
527 fis[19] = 0;
531 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
532 * @fis: Buffer from which data will be input
533 * @tf: Taskfile to output
535 * Converts a serial ATA FIS structure to a standard ATA taskfile.
537 * LOCKING:
538 * Inherited from caller.
541 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
543 tf->command = fis[2]; /* status */
544 tf->feature = fis[3]; /* error */
546 tf->lbal = fis[4];
547 tf->lbam = fis[5];
548 tf->lbah = fis[6];
549 tf->device = fis[7];
551 tf->hob_lbal = fis[8];
552 tf->hob_lbam = fis[9];
553 tf->hob_lbah = fis[10];
555 tf->nsect = fis[12];
556 tf->hob_nsect = fis[13];
559 static const u8 ata_rw_cmds[] = {
560 /* pio multi */
561 ATA_CMD_READ_MULTI,
562 ATA_CMD_WRITE_MULTI,
563 ATA_CMD_READ_MULTI_EXT,
564 ATA_CMD_WRITE_MULTI_EXT,
565 /* pio */
566 ATA_CMD_PIO_READ,
567 ATA_CMD_PIO_WRITE,
568 ATA_CMD_PIO_READ_EXT,
569 ATA_CMD_PIO_WRITE_EXT,
570 /* dma */
571 ATA_CMD_READ,
572 ATA_CMD_WRITE,
573 ATA_CMD_READ_EXT,
574 ATA_CMD_WRITE_EXT
578 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
579 * @qc: command to examine and configure
581 * Examine the device configuration and tf->flags to calculate
582 * the proper read/write commands and protocol to use.
584 * LOCKING:
585 * caller.
587 void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
589 struct ata_taskfile *tf = &qc->tf;
590 struct ata_device *dev = qc->dev;
592 int index, lba48, write;
594 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
595 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
597 if (dev->flags & ATA_DFLAG_PIO) {
598 tf->protocol = ATA_PROT_PIO;
599 index = dev->multi_count ? 0 : 4;
600 } else {
601 tf->protocol = ATA_PROT_DMA;
602 index = 8;
605 tf->command = ata_rw_cmds[index + lba48 + write];
608 static const char * xfer_mode_str[] = {
609 "UDMA/16",
610 "UDMA/25",
611 "UDMA/33",
612 "UDMA/44",
613 "UDMA/66",
614 "UDMA/100",
615 "UDMA/133",
616 "UDMA7",
617 "MWDMA0",
618 "MWDMA1",
619 "MWDMA2",
620 "PIO0",
621 "PIO1",
622 "PIO2",
623 "PIO3",
624 "PIO4",
628 * ata_udma_string - convert UDMA bit offset to string
629 * @mask: mask of bits supported; only highest bit counts.
631 * Determine string which represents the highest speed
632 * (highest bit in @udma_mask).
634 * LOCKING:
635 * None.
637 * RETURNS:
638 * Constant C string representing highest speed listed in
639 * @udma_mask, or the constant C string "<n/a>".
642 static const char *ata_mode_string(unsigned int mask)
644 int i;
646 for (i = 7; i >= 0; i--)
647 if (mask & (1 << i))
648 goto out;
649 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
650 if (mask & (1 << i))
651 goto out;
652 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
653 if (mask & (1 << i))
654 goto out;
656 return "<n/a>";
658 out:
659 return xfer_mode_str[i];
663 * ata_pio_devchk - PATA device presence detection
664 * @ap: ATA channel to examine
665 * @device: Device to examine (starting at zero)
667 * This technique was originally described in
668 * Hale Landis's ATADRVR (www.ata-atapi.com), and
669 * later found its way into the ATA/ATAPI spec.
671 * Write a pattern to the ATA shadow registers,
672 * and if a device is present, it will respond by
673 * correctly storing and echoing back the
674 * ATA shadow register contents.
676 * LOCKING:
677 * caller.
680 static unsigned int ata_pio_devchk(struct ata_port *ap,
681 unsigned int device)
683 struct ata_ioports *ioaddr = &ap->ioaddr;
684 u8 nsect, lbal;
686 ap->ops->dev_select(ap, device);
688 outb(0x55, ioaddr->nsect_addr);
689 outb(0xaa, ioaddr->lbal_addr);
691 outb(0xaa, ioaddr->nsect_addr);
692 outb(0x55, ioaddr->lbal_addr);
694 outb(0x55, ioaddr->nsect_addr);
695 outb(0xaa, ioaddr->lbal_addr);
697 nsect = inb(ioaddr->nsect_addr);
698 lbal = inb(ioaddr->lbal_addr);
700 if ((nsect == 0x55) && (lbal == 0xaa))
701 return 1; /* we found a device */
703 return 0; /* nothing found */
707 * ata_mmio_devchk - PATA device presence detection
708 * @ap: ATA channel to examine
709 * @device: Device to examine (starting at zero)
711 * This technique was originally described in
712 * Hale Landis's ATADRVR (www.ata-atapi.com), and
713 * later found its way into the ATA/ATAPI spec.
715 * Write a pattern to the ATA shadow registers,
716 * and if a device is present, it will respond by
717 * correctly storing and echoing back the
718 * ATA shadow register contents.
720 * LOCKING:
721 * caller.
724 static unsigned int ata_mmio_devchk(struct ata_port *ap,
725 unsigned int device)
727 struct ata_ioports *ioaddr = &ap->ioaddr;
728 u8 nsect, lbal;
730 ap->ops->dev_select(ap, device);
732 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
733 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
735 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
736 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
738 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
739 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
741 nsect = readb((void __iomem *) ioaddr->nsect_addr);
742 lbal = readb((void __iomem *) ioaddr->lbal_addr);
744 if ((nsect == 0x55) && (lbal == 0xaa))
745 return 1; /* we found a device */
747 return 0; /* nothing found */
751 * ata_devchk - PATA device presence detection
752 * @ap: ATA channel to examine
753 * @device: Device to examine (starting at zero)
755 * Dispatch ATA device presence detection, depending
756 * on whether we are using PIO or MMIO to talk to the
757 * ATA shadow registers.
759 * LOCKING:
760 * caller.
763 static unsigned int ata_devchk(struct ata_port *ap,
764 unsigned int device)
766 if (ap->flags & ATA_FLAG_MMIO)
767 return ata_mmio_devchk(ap, device);
768 return ata_pio_devchk(ap, device);
772 * ata_dev_classify - determine device type based on ATA-spec signature
773 * @tf: ATA taskfile register set for device to be identified
775 * Determine from taskfile register contents whether a device is
776 * ATA or ATAPI, as per "Signature and persistence" section
777 * of ATA/PI spec (volume 1, sect 5.14).
779 * LOCKING:
780 * None.
782 * RETURNS:
783 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
784 * the event of failure.
787 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
789 /* Apple's open source Darwin code hints that some devices only
790 * put a proper signature into the LBA mid/high registers,
791 * So, we only check those. It's sufficient for uniqueness.
794 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
795 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
796 DPRINTK("found ATA device by sig\n");
797 return ATA_DEV_ATA;
800 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
801 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
802 DPRINTK("found ATAPI device by sig\n");
803 return ATA_DEV_ATAPI;
806 DPRINTK("unknown device\n");
807 return ATA_DEV_UNKNOWN;
811 * ata_dev_try_classify - Parse returned ATA device signature
812 * @ap: ATA channel to examine
813 * @device: Device to examine (starting at zero)
815 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
816 * an ATA/ATAPI-defined set of values is placed in the ATA
817 * shadow registers, indicating the results of device detection
818 * and diagnostics.
820 * Select the ATA device, and read the values from the ATA shadow
821 * registers. Then parse according to the Error register value,
822 * and the spec-defined values examined by ata_dev_classify().
824 * LOCKING:
825 * caller.
828 static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
830 struct ata_device *dev = &ap->device[device];
831 struct ata_taskfile tf;
832 unsigned int class;
833 u8 err;
835 ap->ops->dev_select(ap, device);
837 memset(&tf, 0, sizeof(tf));
839 ap->ops->tf_read(ap, &tf);
840 err = tf.feature;
842 dev->class = ATA_DEV_NONE;
844 /* see if device passed diags */
845 if (err == 1)
846 /* do nothing */ ;
847 else if ((device == 0) && (err == 0x81))
848 /* do nothing */ ;
849 else
850 return err;
852 /* determine if device if ATA or ATAPI */
853 class = ata_dev_classify(&tf);
854 if (class == ATA_DEV_UNKNOWN)
855 return err;
856 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
857 return err;
859 dev->class = class;
861 return err;
865 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
866 * @id: IDENTIFY DEVICE results we will examine
867 * @s: string into which data is output
868 * @ofs: offset into identify device page
869 * @len: length of string to return. must be an even number.
871 * The strings in the IDENTIFY DEVICE page are broken up into
872 * 16-bit chunks. Run through the string, and output each
873 * 8-bit chunk linearly, regardless of platform.
875 * LOCKING:
876 * caller.
879 void ata_dev_id_string(const u16 *id, unsigned char *s,
880 unsigned int ofs, unsigned int len)
882 unsigned int c;
884 while (len > 0) {
885 c = id[ofs] >> 8;
886 *s = c;
887 s++;
889 c = id[ofs] & 0xff;
890 *s = c;
891 s++;
893 ofs++;
894 len -= 2;
900 * ata_noop_dev_select - Select device 0/1 on ATA bus
901 * @ap: ATA channel to manipulate
902 * @device: ATA device (numbered from zero) to select
904 * This function performs no actual function.
906 * May be used as the dev_select() entry in ata_port_operations.
908 * LOCKING:
909 * caller.
911 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
917 * ata_std_dev_select - Select device 0/1 on ATA bus
918 * @ap: ATA channel to manipulate
919 * @device: ATA device (numbered from zero) to select
921 * Use the method defined in the ATA specification to
922 * make either device 0, or device 1, active on the
923 * ATA channel. Works with both PIO and MMIO.
925 * May be used as the dev_select() entry in ata_port_operations.
927 * LOCKING:
928 * caller.
931 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
933 u8 tmp;
935 if (device == 0)
936 tmp = ATA_DEVICE_OBS;
937 else
938 tmp = ATA_DEVICE_OBS | ATA_DEV1;
940 if (ap->flags & ATA_FLAG_MMIO) {
941 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
942 } else {
943 outb(tmp, ap->ioaddr.device_addr);
945 ata_pause(ap); /* needed; also flushes, for mmio */
949 * ata_dev_select - Select device 0/1 on ATA bus
950 * @ap: ATA channel to manipulate
951 * @device: ATA device (numbered from zero) to select
952 * @wait: non-zero to wait for Status register BSY bit to clear
953 * @can_sleep: non-zero if context allows sleeping
955 * Use the method defined in the ATA specification to
956 * make either device 0, or device 1, active on the
957 * ATA channel.
959 * This is a high-level version of ata_std_dev_select(),
960 * which additionally provides the services of inserting
961 * the proper pauses and status polling, where needed.
963 * LOCKING:
964 * caller.
967 void ata_dev_select(struct ata_port *ap, unsigned int device,
968 unsigned int wait, unsigned int can_sleep)
970 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
971 ap->id, device, wait);
973 if (wait)
974 ata_wait_idle(ap);
976 ap->ops->dev_select(ap, device);
978 if (wait) {
979 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
980 msleep(150);
981 ata_wait_idle(ap);
986 * ata_dump_id - IDENTIFY DEVICE info debugging output
987 * @dev: Device whose IDENTIFY DEVICE page we will dump
989 * Dump selected 16-bit words from a detected device's
990 * IDENTIFY PAGE page.
992 * LOCKING:
993 * caller.
996 static inline void ata_dump_id(const struct ata_device *dev)
998 DPRINTK("49==0x%04x "
999 "53==0x%04x "
1000 "63==0x%04x "
1001 "64==0x%04x "
1002 "75==0x%04x \n",
1003 dev->id[49],
1004 dev->id[53],
1005 dev->id[63],
1006 dev->id[64],
1007 dev->id[75]);
1008 DPRINTK("80==0x%04x "
1009 "81==0x%04x "
1010 "82==0x%04x "
1011 "83==0x%04x "
1012 "84==0x%04x \n",
1013 dev->id[80],
1014 dev->id[81],
1015 dev->id[82],
1016 dev->id[83],
1017 dev->id[84]);
1018 DPRINTK("88==0x%04x "
1019 "93==0x%04x\n",
1020 dev->id[88],
1021 dev->id[93]);
1025 * Compute the PIO modes available for this device. This is not as
1026 * trivial as it seems if we must consider early devices correctly.
1028 * FIXME: pre IDE drive timing (do we care ?).
1031 static unsigned int ata_pio_modes(const struct ata_device *adev)
1033 u16 modes;
1035 /* Usual case. Word 53 indicates word 88 is valid */
1036 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1037 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1038 modes <<= 3;
1039 modes |= 0x7;
1040 return modes;
1043 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1044 for the maximum. Turn it into a mask and return it */
1045 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1046 return modes;
1050 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1051 * @ap: port on which device we wish to probe resides
1052 * @device: device bus address, starting at zero
1054 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1055 * command, and read back the 512-byte device information page.
1056 * The device information page is fed to us via the standard
1057 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1058 * using standard PIO-IN paths)
1060 * After reading the device information page, we use several
1061 * bits of information from it to initialize data structures
1062 * that will be used during the lifetime of the ata_device.
1063 * Other data from the info page is used to disqualify certain
1064 * older ATA devices we do not wish to support.
1066 * LOCKING:
1067 * Inherited from caller. Some functions called by this function
1068 * obtain the host_set lock.
1071 static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1073 struct ata_device *dev = &ap->device[device];
1074 unsigned int major_version;
1075 u16 tmp;
1076 unsigned long xfer_modes;
1077 unsigned int using_edd;
1078 DECLARE_COMPLETION(wait);
1079 struct ata_queued_cmd *qc;
1080 unsigned long flags;
1081 int rc;
1083 if (!ata_dev_present(dev)) {
1084 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1085 ap->id, device);
1086 return;
1089 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1090 using_edd = 0;
1091 else
1092 using_edd = 1;
1094 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1096 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1097 dev->class == ATA_DEV_NONE);
1099 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1101 qc = ata_qc_new_init(ap, dev);
1102 BUG_ON(qc == NULL);
1104 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1105 qc->dma_dir = DMA_FROM_DEVICE;
1106 qc->tf.protocol = ATA_PROT_PIO;
1107 qc->nsect = 1;
1109 retry:
1110 if (dev->class == ATA_DEV_ATA) {
1111 qc->tf.command = ATA_CMD_ID_ATA;
1112 DPRINTK("do ATA identify\n");
1113 } else {
1114 qc->tf.command = ATA_CMD_ID_ATAPI;
1115 DPRINTK("do ATAPI identify\n");
1118 qc->waiting = &wait;
1119 qc->complete_fn = ata_qc_complete_noop;
1121 spin_lock_irqsave(&ap->host_set->lock, flags);
1122 rc = ata_qc_issue(qc);
1123 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1125 if (rc)
1126 goto err_out;
1127 else
1128 wait_for_completion(&wait);
1130 spin_lock_irqsave(&ap->host_set->lock, flags);
1131 ap->ops->tf_read(ap, &qc->tf);
1132 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1134 if (qc->tf.command & ATA_ERR) {
1136 * arg! EDD works for all test cases, but seems to return
1137 * the ATA signature for some ATAPI devices. Until the
1138 * reason for this is found and fixed, we fix up the mess
1139 * here. If IDENTIFY DEVICE returns command aborted
1140 * (as ATAPI devices do), then we issue an
1141 * IDENTIFY PACKET DEVICE.
1143 * ATA software reset (SRST, the default) does not appear
1144 * to have this problem.
1146 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
1147 u8 err = qc->tf.feature;
1148 if (err & ATA_ABORTED) {
1149 dev->class = ATA_DEV_ATAPI;
1150 qc->cursg = 0;
1151 qc->cursg_ofs = 0;
1152 qc->cursect = 0;
1153 qc->nsect = 1;
1154 goto retry;
1157 goto err_out;
1160 swap_buf_le16(dev->id, ATA_ID_WORDS);
1162 /* print device capabilities */
1163 printk(KERN_DEBUG "ata%u: dev %u cfg "
1164 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1165 ap->id, device, dev->id[49],
1166 dev->id[82], dev->id[83], dev->id[84],
1167 dev->id[85], dev->id[86], dev->id[87],
1168 dev->id[88]);
1171 * common ATA, ATAPI feature tests
1174 /* we require DMA support (bits 8 of word 49) */
1175 if (!ata_id_has_dma(dev->id)) {
1176 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1177 goto err_out_nosup;
1180 /* quick-n-dirty find max transfer mode; for printk only */
1181 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1182 if (!xfer_modes)
1183 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1184 if (!xfer_modes)
1185 xfer_modes = ata_pio_modes(dev);
1187 ata_dump_id(dev);
1189 /* ATA-specific feature tests */
1190 if (dev->class == ATA_DEV_ATA) {
1191 if (!ata_id_is_ata(dev->id)) /* sanity check */
1192 goto err_out_nosup;
1194 /* get major version */
1195 tmp = dev->id[ATA_ID_MAJOR_VER];
1196 for (major_version = 14; major_version >= 1; major_version--)
1197 if (tmp & (1 << major_version))
1198 break;
1201 * The exact sequence expected by certain pre-ATA4 drives is:
1202 * SRST RESET
1203 * IDENTIFY
1204 * INITIALIZE DEVICE PARAMETERS
1205 * anything else..
1206 * Some drives were very specific about that exact sequence.
1208 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1209 ata_dev_init_params(ap, dev);
1211 /* current CHS translation info (id[53-58]) might be
1212 * changed. reread the identify device info.
1214 ata_dev_reread_id(ap, dev);
1217 if (ata_id_has_lba(dev->id)) {
1218 dev->flags |= ATA_DFLAG_LBA;
1220 if (ata_id_has_lba48(dev->id)) {
1221 dev->flags |= ATA_DFLAG_LBA48;
1222 dev->n_sectors = ata_id_u64(dev->id, 100);
1223 } else {
1224 dev->n_sectors = ata_id_u32(dev->id, 60);
1227 /* print device info to dmesg */
1228 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1229 ap->id, device,
1230 major_version,
1231 ata_mode_string(xfer_modes),
1232 (unsigned long long)dev->n_sectors,
1233 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1234 } else {
1235 /* CHS */
1237 /* Default translation */
1238 dev->cylinders = dev->id[1];
1239 dev->heads = dev->id[3];
1240 dev->sectors = dev->id[6];
1241 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1243 if (ata_id_current_chs_valid(dev->id)) {
1244 /* Current CHS translation is valid. */
1245 dev->cylinders = dev->id[54];
1246 dev->heads = dev->id[55];
1247 dev->sectors = dev->id[56];
1249 dev->n_sectors = ata_id_u32(dev->id, 57);
1252 /* print device info to dmesg */
1253 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1254 ap->id, device,
1255 major_version,
1256 ata_mode_string(xfer_modes),
1257 (unsigned long long)dev->n_sectors,
1258 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1262 ap->host->max_cmd_len = 16;
1265 /* ATAPI-specific feature tests */
1266 else {
1267 if (ata_id_is_ata(dev->id)) /* sanity check */
1268 goto err_out_nosup;
1270 rc = atapi_cdb_len(dev->id);
1271 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1272 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1273 goto err_out_nosup;
1275 ap->cdb_len = (unsigned int) rc;
1276 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1278 /* print device info to dmesg */
1279 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1280 ap->id, device,
1281 ata_mode_string(xfer_modes));
1284 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1285 return;
1287 err_out_nosup:
1288 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1289 ap->id, device);
1290 err_out:
1291 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1292 DPRINTK("EXIT, err\n");
1296 static inline u8 ata_dev_knobble(const struct ata_port *ap)
1298 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1302 * ata_dev_config - Run device specific handlers and check for
1303 * SATA->PATA bridges
1304 * @ap: Bus
1305 * @i: Device
1307 * LOCKING:
1310 void ata_dev_config(struct ata_port *ap, unsigned int i)
1312 /* limit bridge transfers to udma5, 200 sectors */
1313 if (ata_dev_knobble(ap)) {
1314 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1315 ap->id, ap->device->devno);
1316 ap->udma_mask &= ATA_UDMA5;
1317 ap->host->max_sectors = ATA_MAX_SECTORS;
1318 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1319 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1322 if (ap->ops->dev_config)
1323 ap->ops->dev_config(ap, &ap->device[i]);
1327 * ata_bus_probe - Reset and probe ATA bus
1328 * @ap: Bus to probe
1330 * Master ATA bus probing function. Initiates a hardware-dependent
1331 * bus reset, then attempts to identify any devices found on
1332 * the bus.
1334 * LOCKING:
1335 * PCI/etc. bus probe sem.
1337 * RETURNS:
1338 * Zero on success, non-zero on error.
1341 static int ata_bus_probe(struct ata_port *ap)
1343 unsigned int i, found = 0;
1345 ap->ops->phy_reset(ap);
1346 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1347 goto err_out;
1349 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1350 ata_dev_identify(ap, i);
1351 if (ata_dev_present(&ap->device[i])) {
1352 found = 1;
1353 ata_dev_config(ap,i);
1357 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1358 goto err_out_disable;
1360 ata_set_mode(ap);
1361 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1362 goto err_out_disable;
1364 return 0;
1366 err_out_disable:
1367 ap->ops->port_disable(ap);
1368 err_out:
1369 return -1;
1373 * ata_port_probe - Mark port as enabled
1374 * @ap: Port for which we indicate enablement
1376 * Modify @ap data structure such that the system
1377 * thinks that the entire port is enabled.
1379 * LOCKING: host_set lock, or some other form of
1380 * serialization.
1383 void ata_port_probe(struct ata_port *ap)
1385 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1389 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1390 * @ap: SATA port associated with target SATA PHY.
1392 * This function issues commands to standard SATA Sxxx
1393 * PHY registers, to wake up the phy (and device), and
1394 * clear any reset condition.
1396 * LOCKING:
1397 * PCI/etc. bus probe sem.
1400 void __sata_phy_reset(struct ata_port *ap)
1402 u32 sstatus;
1403 unsigned long timeout = jiffies + (HZ * 5);
1405 if (ap->flags & ATA_FLAG_SATA_RESET) {
1406 /* issue phy wake/reset */
1407 scr_write_flush(ap, SCR_CONTROL, 0x301);
1408 /* Couldn't find anything in SATA I/II specs, but
1409 * AHCI-1.1 10.4.2 says at least 1 ms. */
1410 mdelay(1);
1412 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1414 /* wait for phy to become ready, if necessary */
1415 do {
1416 msleep(200);
1417 sstatus = scr_read(ap, SCR_STATUS);
1418 if ((sstatus & 0xf) != 1)
1419 break;
1420 } while (time_before(jiffies, timeout));
1422 /* TODO: phy layer with polling, timeouts, etc. */
1423 if (sata_dev_present(ap))
1424 ata_port_probe(ap);
1425 else {
1426 sstatus = scr_read(ap, SCR_STATUS);
1427 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1428 ap->id, sstatus);
1429 ata_port_disable(ap);
1432 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1433 return;
1435 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1436 ata_port_disable(ap);
1437 return;
1440 ap->cbl = ATA_CBL_SATA;
1444 * sata_phy_reset - Reset SATA bus.
1445 * @ap: SATA port associated with target SATA PHY.
1447 * This function resets the SATA bus, and then probes
1448 * the bus for devices.
1450 * LOCKING:
1451 * PCI/etc. bus probe sem.
1454 void sata_phy_reset(struct ata_port *ap)
1456 __sata_phy_reset(ap);
1457 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1458 return;
1459 ata_bus_reset(ap);
1463 * ata_port_disable - Disable port.
1464 * @ap: Port to be disabled.
1466 * Modify @ap data structure such that the system
1467 * thinks that the entire port is disabled, and should
1468 * never attempt to probe or communicate with devices
1469 * on this port.
1471 * LOCKING: host_set lock, or some other form of
1472 * serialization.
1475 void ata_port_disable(struct ata_port *ap)
1477 ap->device[0].class = ATA_DEV_NONE;
1478 ap->device[1].class = ATA_DEV_NONE;
1479 ap->flags |= ATA_FLAG_PORT_DISABLED;
1483 * This mode timing computation functionality is ported over from
1484 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1487 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1488 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1489 * for PIO 5, which is a nonstandard extension and UDMA6, which
1490 * is currently supported only by Maxtor drives.
1493 static const struct ata_timing ata_timing[] = {
1495 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1496 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1497 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1498 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1500 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1501 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1502 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1504 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1506 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1507 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1508 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1510 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1511 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1512 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1514 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1515 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1516 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1518 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1519 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1520 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1522 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1524 { 0xFF }
1527 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1528 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1530 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1532 q->setup = EZ(t->setup * 1000, T);
1533 q->act8b = EZ(t->act8b * 1000, T);
1534 q->rec8b = EZ(t->rec8b * 1000, T);
1535 q->cyc8b = EZ(t->cyc8b * 1000, T);
1536 q->active = EZ(t->active * 1000, T);
1537 q->recover = EZ(t->recover * 1000, T);
1538 q->cycle = EZ(t->cycle * 1000, T);
1539 q->udma = EZ(t->udma * 1000, UT);
1542 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1543 struct ata_timing *m, unsigned int what)
1545 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1546 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1547 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1548 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1549 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1550 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1551 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1552 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1555 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1557 const struct ata_timing *t;
1559 for (t = ata_timing; t->mode != speed; t++)
1560 if (t->mode == 0xFF)
1561 return NULL;
1562 return t;
1565 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1566 struct ata_timing *t, int T, int UT)
1568 const struct ata_timing *s;
1569 struct ata_timing p;
1572 * Find the mode.
1575 if (!(s = ata_timing_find_mode(speed)))
1576 return -EINVAL;
1579 * If the drive is an EIDE drive, it can tell us it needs extended
1580 * PIO/MW_DMA cycle timing.
1583 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1584 memset(&p, 0, sizeof(p));
1585 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1586 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1587 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1588 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1589 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1591 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1595 * Convert the timing to bus clock counts.
1598 ata_timing_quantize(s, t, T, UT);
1601 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1602 * and some other commands. We have to ensure that the DMA cycle timing is
1603 * slower/equal than the fastest PIO timing.
1606 if (speed > XFER_PIO_4) {
1607 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1608 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1612 * Lenghten active & recovery time so that cycle time is correct.
1615 if (t->act8b + t->rec8b < t->cyc8b) {
1616 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1617 t->rec8b = t->cyc8b - t->act8b;
1620 if (t->active + t->recover < t->cycle) {
1621 t->active += (t->cycle - (t->active + t->recover)) / 2;
1622 t->recover = t->cycle - t->active;
1625 return 0;
1628 static const struct {
1629 unsigned int shift;
1630 u8 base;
1631 } xfer_mode_classes[] = {
1632 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1633 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1634 { ATA_SHIFT_PIO, XFER_PIO_0 },
1637 static inline u8 base_from_shift(unsigned int shift)
1639 int i;
1641 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1642 if (xfer_mode_classes[i].shift == shift)
1643 return xfer_mode_classes[i].base;
1645 return 0xff;
1648 static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1650 int ofs, idx;
1651 u8 base;
1653 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1654 return;
1656 if (dev->xfer_shift == ATA_SHIFT_PIO)
1657 dev->flags |= ATA_DFLAG_PIO;
1659 ata_dev_set_xfermode(ap, dev);
1661 base = base_from_shift(dev->xfer_shift);
1662 ofs = dev->xfer_mode - base;
1663 idx = ofs + dev->xfer_shift;
1664 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1666 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1667 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1669 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1670 ap->id, dev->devno, xfer_mode_str[idx]);
1673 static int ata_host_set_pio(struct ata_port *ap)
1675 unsigned int mask;
1676 int x, i;
1677 u8 base, xfer_mode;
1679 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1680 x = fgb(mask);
1681 if (x < 0) {
1682 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1683 return -1;
1686 base = base_from_shift(ATA_SHIFT_PIO);
1687 xfer_mode = base + x;
1689 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1690 (int)base, (int)xfer_mode, mask, x);
1692 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1693 struct ata_device *dev = &ap->device[i];
1694 if (ata_dev_present(dev)) {
1695 dev->pio_mode = xfer_mode;
1696 dev->xfer_mode = xfer_mode;
1697 dev->xfer_shift = ATA_SHIFT_PIO;
1698 if (ap->ops->set_piomode)
1699 ap->ops->set_piomode(ap, dev);
1703 return 0;
1706 static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1707 unsigned int xfer_shift)
1709 int i;
1711 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1712 struct ata_device *dev = &ap->device[i];
1713 if (ata_dev_present(dev)) {
1714 dev->dma_mode = xfer_mode;
1715 dev->xfer_mode = xfer_mode;
1716 dev->xfer_shift = xfer_shift;
1717 if (ap->ops->set_dmamode)
1718 ap->ops->set_dmamode(ap, dev);
1724 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1725 * @ap: port on which timings will be programmed
1727 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1729 * LOCKING:
1730 * PCI/etc. bus probe sem.
1733 static void ata_set_mode(struct ata_port *ap)
1735 unsigned int xfer_shift;
1736 u8 xfer_mode;
1737 int rc;
1739 /* step 1: always set host PIO timings */
1740 rc = ata_host_set_pio(ap);
1741 if (rc)
1742 goto err_out;
1744 /* step 2: choose the best data xfer mode */
1745 xfer_mode = xfer_shift = 0;
1746 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1747 if (rc)
1748 goto err_out;
1750 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1751 if (xfer_shift != ATA_SHIFT_PIO)
1752 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1754 /* step 4: update devices' xfer mode */
1755 ata_dev_set_mode(ap, &ap->device[0]);
1756 ata_dev_set_mode(ap, &ap->device[1]);
1758 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1759 return;
1761 if (ap->ops->post_set_mode)
1762 ap->ops->post_set_mode(ap);
1764 return;
1766 err_out:
1767 ata_port_disable(ap);
1771 * ata_busy_sleep - sleep until BSY clears, or timeout
1772 * @ap: port containing status register to be polled
1773 * @tmout_pat: impatience timeout
1774 * @tmout: overall timeout
1776 * Sleep until ATA Status register bit BSY clears,
1777 * or a timeout occurs.
1779 * LOCKING: None.
1783 static unsigned int ata_busy_sleep (struct ata_port *ap,
1784 unsigned long tmout_pat,
1785 unsigned long tmout)
1787 unsigned long timer_start, timeout;
1788 u8 status;
1790 status = ata_busy_wait(ap, ATA_BUSY, 300);
1791 timer_start = jiffies;
1792 timeout = timer_start + tmout_pat;
1793 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1794 msleep(50);
1795 status = ata_busy_wait(ap, ATA_BUSY, 3);
1798 if (status & ATA_BUSY)
1799 printk(KERN_WARNING "ata%u is slow to respond, "
1800 "please be patient\n", ap->id);
1802 timeout = timer_start + tmout;
1803 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1804 msleep(50);
1805 status = ata_chk_status(ap);
1808 if (status & ATA_BUSY) {
1809 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1810 ap->id, tmout / HZ);
1811 return 1;
1814 return 0;
1817 static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1819 struct ata_ioports *ioaddr = &ap->ioaddr;
1820 unsigned int dev0 = devmask & (1 << 0);
1821 unsigned int dev1 = devmask & (1 << 1);
1822 unsigned long timeout;
1824 /* if device 0 was found in ata_devchk, wait for its
1825 * BSY bit to clear
1827 if (dev0)
1828 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1830 /* if device 1 was found in ata_devchk, wait for
1831 * register access, then wait for BSY to clear
1833 timeout = jiffies + ATA_TMOUT_BOOT;
1834 while (dev1) {
1835 u8 nsect, lbal;
1837 ap->ops->dev_select(ap, 1);
1838 if (ap->flags & ATA_FLAG_MMIO) {
1839 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1840 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1841 } else {
1842 nsect = inb(ioaddr->nsect_addr);
1843 lbal = inb(ioaddr->lbal_addr);
1845 if ((nsect == 1) && (lbal == 1))
1846 break;
1847 if (time_after(jiffies, timeout)) {
1848 dev1 = 0;
1849 break;
1851 msleep(50); /* give drive a breather */
1853 if (dev1)
1854 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1856 /* is all this really necessary? */
1857 ap->ops->dev_select(ap, 0);
1858 if (dev1)
1859 ap->ops->dev_select(ap, 1);
1860 if (dev0)
1861 ap->ops->dev_select(ap, 0);
1865 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1866 * @ap: Port to reset and probe
1868 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1869 * probe the bus. Not often used these days.
1871 * LOCKING:
1872 * PCI/etc. bus probe sem.
1873 * Obtains host_set lock.
1877 static unsigned int ata_bus_edd(struct ata_port *ap)
1879 struct ata_taskfile tf;
1880 unsigned long flags;
1882 /* set up execute-device-diag (bus reset) taskfile */
1883 /* also, take interrupts to a known state (disabled) */
1884 DPRINTK("execute-device-diag\n");
1885 ata_tf_init(ap, &tf, 0);
1886 tf.ctl |= ATA_NIEN;
1887 tf.command = ATA_CMD_EDD;
1888 tf.protocol = ATA_PROT_NODATA;
1890 /* do bus reset */
1891 spin_lock_irqsave(&ap->host_set->lock, flags);
1892 ata_tf_to_host(ap, &tf);
1893 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1895 /* spec says at least 2ms. but who knows with those
1896 * crazy ATAPI devices...
1898 msleep(150);
1900 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1903 static unsigned int ata_bus_softreset(struct ata_port *ap,
1904 unsigned int devmask)
1906 struct ata_ioports *ioaddr = &ap->ioaddr;
1908 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1910 /* software reset. causes dev0 to be selected */
1911 if (ap->flags & ATA_FLAG_MMIO) {
1912 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1913 udelay(20); /* FIXME: flush */
1914 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1915 udelay(20); /* FIXME: flush */
1916 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1917 } else {
1918 outb(ap->ctl, ioaddr->ctl_addr);
1919 udelay(10);
1920 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1921 udelay(10);
1922 outb(ap->ctl, ioaddr->ctl_addr);
1925 /* spec mandates ">= 2ms" before checking status.
1926 * We wait 150ms, because that was the magic delay used for
1927 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1928 * between when the ATA command register is written, and then
1929 * status is checked. Because waiting for "a while" before
1930 * checking status is fine, post SRST, we perform this magic
1931 * delay here as well.
1933 msleep(150);
1935 ata_bus_post_reset(ap, devmask);
1937 return 0;
1941 * ata_bus_reset - reset host port and associated ATA channel
1942 * @ap: port to reset
1944 * This is typically the first time we actually start issuing
1945 * commands to the ATA channel. We wait for BSY to clear, then
1946 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1947 * result. Determine what devices, if any, are on the channel
1948 * by looking at the device 0/1 error register. Look at the signature
1949 * stored in each device's taskfile registers, to determine if
1950 * the device is ATA or ATAPI.
1952 * LOCKING:
1953 * PCI/etc. bus probe sem.
1954 * Obtains host_set lock.
1956 * SIDE EFFECTS:
1957 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1960 void ata_bus_reset(struct ata_port *ap)
1962 struct ata_ioports *ioaddr = &ap->ioaddr;
1963 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1964 u8 err;
1965 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1967 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1969 /* determine if device 0/1 are present */
1970 if (ap->flags & ATA_FLAG_SATA_RESET)
1971 dev0 = 1;
1972 else {
1973 dev0 = ata_devchk(ap, 0);
1974 if (slave_possible)
1975 dev1 = ata_devchk(ap, 1);
1978 if (dev0)
1979 devmask |= (1 << 0);
1980 if (dev1)
1981 devmask |= (1 << 1);
1983 /* select device 0 again */
1984 ap->ops->dev_select(ap, 0);
1986 /* issue bus reset */
1987 if (ap->flags & ATA_FLAG_SRST)
1988 rc = ata_bus_softreset(ap, devmask);
1989 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1990 /* set up device control */
1991 if (ap->flags & ATA_FLAG_MMIO)
1992 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1993 else
1994 outb(ap->ctl, ioaddr->ctl_addr);
1995 rc = ata_bus_edd(ap);
1998 if (rc)
1999 goto err_out;
2002 * determine by signature whether we have ATA or ATAPI devices
2004 err = ata_dev_try_classify(ap, 0);
2005 if ((slave_possible) && (err != 0x81))
2006 ata_dev_try_classify(ap, 1);
2008 /* re-enable interrupts */
2009 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2010 ata_irq_on(ap);
2012 /* is double-select really necessary? */
2013 if (ap->device[1].class != ATA_DEV_NONE)
2014 ap->ops->dev_select(ap, 1);
2015 if (ap->device[0].class != ATA_DEV_NONE)
2016 ap->ops->dev_select(ap, 0);
2018 /* if no devices were detected, disable this port */
2019 if ((ap->device[0].class == ATA_DEV_NONE) &&
2020 (ap->device[1].class == ATA_DEV_NONE))
2021 goto err_out;
2023 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2024 /* set up device control for ATA_FLAG_SATA_RESET */
2025 if (ap->flags & ATA_FLAG_MMIO)
2026 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2027 else
2028 outb(ap->ctl, ioaddr->ctl_addr);
2031 DPRINTK("EXIT\n");
2032 return;
2034 err_out:
2035 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2036 ap->ops->port_disable(ap);
2038 DPRINTK("EXIT\n");
2041 static void ata_pr_blacklisted(const struct ata_port *ap,
2042 const struct ata_device *dev)
2044 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2045 ap->id, dev->devno);
2048 static const char * ata_dma_blacklist [] = {
2049 "WDC AC11000H",
2050 "WDC AC22100H",
2051 "WDC AC32500H",
2052 "WDC AC33100H",
2053 "WDC AC31600H",
2054 "WDC AC32100H",
2055 "WDC AC23200L",
2056 "Compaq CRD-8241B",
2057 "CRD-8400B",
2058 "CRD-8480B",
2059 "CRD-8482B",
2060 "CRD-84",
2061 "SanDisk SDP3B",
2062 "SanDisk SDP3B-64",
2063 "SANYO CD-ROM CRD",
2064 "HITACHI CDR-8",
2065 "HITACHI CDR-8335",
2066 "HITACHI CDR-8435",
2067 "Toshiba CD-ROM XM-6202B",
2068 "TOSHIBA CD-ROM XM-1702BC",
2069 "CD-532E-A",
2070 "E-IDE CD-ROM CR-840",
2071 "CD-ROM Drive/F5A",
2072 "WPI CDD-820",
2073 "SAMSUNG CD-ROM SC-148C",
2074 "SAMSUNG CD-ROM SC",
2075 "SanDisk SDP3B-64",
2076 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2077 "_NEC DV5800A",
2080 static int ata_dma_blacklisted(const struct ata_device *dev)
2082 unsigned char model_num[40];
2083 char *s;
2084 unsigned int len;
2085 int i;
2087 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2088 sizeof(model_num));
2089 s = &model_num[0];
2090 len = strnlen(s, sizeof(model_num));
2092 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2093 while ((len > 0) && (s[len - 1] == ' ')) {
2094 len--;
2095 s[len] = 0;
2098 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2099 if (!strncmp(ata_dma_blacklist[i], s, len))
2100 return 1;
2102 return 0;
2105 static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2107 const struct ata_device *master, *slave;
2108 unsigned int mask;
2110 master = &ap->device[0];
2111 slave = &ap->device[1];
2113 assert (ata_dev_present(master) || ata_dev_present(slave));
2115 if (shift == ATA_SHIFT_UDMA) {
2116 mask = ap->udma_mask;
2117 if (ata_dev_present(master)) {
2118 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2119 if (ata_dma_blacklisted(master)) {
2120 mask = 0;
2121 ata_pr_blacklisted(ap, master);
2124 if (ata_dev_present(slave)) {
2125 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2126 if (ata_dma_blacklisted(slave)) {
2127 mask = 0;
2128 ata_pr_blacklisted(ap, slave);
2132 else if (shift == ATA_SHIFT_MWDMA) {
2133 mask = ap->mwdma_mask;
2134 if (ata_dev_present(master)) {
2135 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2136 if (ata_dma_blacklisted(master)) {
2137 mask = 0;
2138 ata_pr_blacklisted(ap, master);
2141 if (ata_dev_present(slave)) {
2142 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2143 if (ata_dma_blacklisted(slave)) {
2144 mask = 0;
2145 ata_pr_blacklisted(ap, slave);
2149 else if (shift == ATA_SHIFT_PIO) {
2150 mask = ap->pio_mask;
2151 if (ata_dev_present(master)) {
2152 /* spec doesn't return explicit support for
2153 * PIO0-2, so we fake it
2155 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2156 tmp_mode <<= 3;
2157 tmp_mode |= 0x7;
2158 mask &= tmp_mode;
2160 if (ata_dev_present(slave)) {
2161 /* spec doesn't return explicit support for
2162 * PIO0-2, so we fake it
2164 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2165 tmp_mode <<= 3;
2166 tmp_mode |= 0x7;
2167 mask &= tmp_mode;
2170 else {
2171 mask = 0xffffffff; /* shut up compiler warning */
2172 BUG();
2175 return mask;
2178 /* find greatest bit */
2179 static int fgb(u32 bitmap)
2181 unsigned int i;
2182 int x = -1;
2184 for (i = 0; i < 32; i++)
2185 if (bitmap & (1 << i))
2186 x = i;
2188 return x;
2192 * ata_choose_xfer_mode - attempt to find best transfer mode
2193 * @ap: Port for which an xfer mode will be selected
2194 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2195 * @xfer_shift_out: (output) bit shift that selects this mode
2197 * Based on host and device capabilities, determine the
2198 * maximum transfer mode that is amenable to all.
2200 * LOCKING:
2201 * PCI/etc. bus probe sem.
2203 * RETURNS:
2204 * Zero on success, negative on error.
2207 static int ata_choose_xfer_mode(const struct ata_port *ap,
2208 u8 *xfer_mode_out,
2209 unsigned int *xfer_shift_out)
2211 unsigned int mask, shift;
2212 int x, i;
2214 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2215 shift = xfer_mode_classes[i].shift;
2216 mask = ata_get_mode_mask(ap, shift);
2218 x = fgb(mask);
2219 if (x >= 0) {
2220 *xfer_mode_out = xfer_mode_classes[i].base + x;
2221 *xfer_shift_out = shift;
2222 return 0;
2226 return -1;
2230 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2231 * @ap: Port associated with device @dev
2232 * @dev: Device to which command will be sent
2234 * Issue SET FEATURES - XFER MODE command to device @dev
2235 * on port @ap.
2237 * LOCKING:
2238 * PCI/etc. bus probe sem.
2241 static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2243 DECLARE_COMPLETION(wait);
2244 struct ata_queued_cmd *qc;
2245 int rc;
2246 unsigned long flags;
2248 /* set up set-features taskfile */
2249 DPRINTK("set features - xfer mode\n");
2251 qc = ata_qc_new_init(ap, dev);
2252 BUG_ON(qc == NULL);
2254 qc->tf.command = ATA_CMD_SET_FEATURES;
2255 qc->tf.feature = SETFEATURES_XFER;
2256 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2257 qc->tf.protocol = ATA_PROT_NODATA;
2258 qc->tf.nsect = dev->xfer_mode;
2260 qc->waiting = &wait;
2261 qc->complete_fn = ata_qc_complete_noop;
2263 spin_lock_irqsave(&ap->host_set->lock, flags);
2264 rc = ata_qc_issue(qc);
2265 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2267 if (rc)
2268 ata_port_disable(ap);
2269 else
2270 wait_for_completion(&wait);
2272 DPRINTK("EXIT\n");
2276 * ata_dev_reread_id - Reread the device identify device info
2277 * @ap: port where the device is
2278 * @dev: device to reread the identify device info
2280 * LOCKING:
2283 static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2285 DECLARE_COMPLETION(wait);
2286 struct ata_queued_cmd *qc;
2287 unsigned long flags;
2288 int rc;
2290 qc = ata_qc_new_init(ap, dev);
2291 BUG_ON(qc == NULL);
2293 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2294 qc->dma_dir = DMA_FROM_DEVICE;
2296 if (dev->class == ATA_DEV_ATA) {
2297 qc->tf.command = ATA_CMD_ID_ATA;
2298 DPRINTK("do ATA identify\n");
2299 } else {
2300 qc->tf.command = ATA_CMD_ID_ATAPI;
2301 DPRINTK("do ATAPI identify\n");
2304 qc->tf.flags |= ATA_TFLAG_DEVICE;
2305 qc->tf.protocol = ATA_PROT_PIO;
2306 qc->nsect = 1;
2308 qc->waiting = &wait;
2309 qc->complete_fn = ata_qc_complete_noop;
2311 spin_lock_irqsave(&ap->host_set->lock, flags);
2312 rc = ata_qc_issue(qc);
2313 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2315 if (rc)
2316 goto err_out;
2318 wait_for_completion(&wait);
2320 swap_buf_le16(dev->id, ATA_ID_WORDS);
2322 ata_dump_id(dev);
2324 DPRINTK("EXIT\n");
2326 return;
2327 err_out:
2328 ata_port_disable(ap);
2332 * ata_dev_init_params - Issue INIT DEV PARAMS command
2333 * @ap: Port associated with device @dev
2334 * @dev: Device to which command will be sent
2336 * LOCKING:
2339 static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2341 DECLARE_COMPLETION(wait);
2342 struct ata_queued_cmd *qc;
2343 int rc;
2344 unsigned long flags;
2345 u16 sectors = dev->id[6];
2346 u16 heads = dev->id[3];
2348 /* Number of sectors per track 1-255. Number of heads 1-16 */
2349 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2350 return;
2352 /* set up init dev params taskfile */
2353 DPRINTK("init dev params \n");
2355 qc = ata_qc_new_init(ap, dev);
2356 BUG_ON(qc == NULL);
2358 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2359 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2360 qc->tf.protocol = ATA_PROT_NODATA;
2361 qc->tf.nsect = sectors;
2362 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2364 qc->waiting = &wait;
2365 qc->complete_fn = ata_qc_complete_noop;
2367 spin_lock_irqsave(&ap->host_set->lock, flags);
2368 rc = ata_qc_issue(qc);
2369 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2371 if (rc)
2372 ata_port_disable(ap);
2373 else
2374 wait_for_completion(&wait);
2376 DPRINTK("EXIT\n");
2380 * ata_sg_clean - Unmap DMA memory associated with command
2381 * @qc: Command containing DMA memory to be released
2383 * Unmap all mapped DMA memory associated with this command.
2385 * LOCKING:
2386 * spin_lock_irqsave(host_set lock)
2389 static void ata_sg_clean(struct ata_queued_cmd *qc)
2391 struct ata_port *ap = qc->ap;
2392 struct scatterlist *sg = qc->__sg;
2393 int dir = qc->dma_dir;
2394 void *pad_buf = NULL;
2396 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2397 assert(sg != NULL);
2399 if (qc->flags & ATA_QCFLAG_SINGLE)
2400 assert(qc->n_elem == 1);
2402 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2404 /* if we padded the buffer out to 32-bit bound, and data
2405 * xfer direction is from-device, we must copy from the
2406 * pad buffer back into the supplied buffer
2408 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2409 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2411 if (qc->flags & ATA_QCFLAG_SG) {
2412 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2413 /* restore last sg */
2414 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2415 if (pad_buf) {
2416 struct scatterlist *psg = &qc->pad_sgent;
2417 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2418 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2419 kunmap_atomic(psg->page, KM_IRQ0);
2421 } else {
2422 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2423 sg_dma_len(&sg[0]), dir);
2424 /* restore sg */
2425 sg->length += qc->pad_len;
2426 if (pad_buf)
2427 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2428 pad_buf, qc->pad_len);
2431 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2432 qc->__sg = NULL;
2436 * ata_fill_sg - Fill PCI IDE PRD table
2437 * @qc: Metadata associated with taskfile to be transferred
2439 * Fill PCI IDE PRD (scatter-gather) table with segments
2440 * associated with the current disk command.
2442 * LOCKING:
2443 * spin_lock_irqsave(host_set lock)
2446 static void ata_fill_sg(struct ata_queued_cmd *qc)
2448 struct ata_port *ap = qc->ap;
2449 struct scatterlist *sg;
2450 unsigned int idx;
2452 assert(qc->__sg != NULL);
2453 assert(qc->n_elem > 0);
2455 idx = 0;
2456 ata_for_each_sg(sg, qc) {
2457 u32 addr, offset;
2458 u32 sg_len, len;
2460 /* determine if physical DMA addr spans 64K boundary.
2461 * Note h/w doesn't support 64-bit, so we unconditionally
2462 * truncate dma_addr_t to u32.
2464 addr = (u32) sg_dma_address(sg);
2465 sg_len = sg_dma_len(sg);
2467 while (sg_len) {
2468 offset = addr & 0xffff;
2469 len = sg_len;
2470 if ((offset + sg_len) > 0x10000)
2471 len = 0x10000 - offset;
2473 ap->prd[idx].addr = cpu_to_le32(addr);
2474 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2475 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2477 idx++;
2478 sg_len -= len;
2479 addr += len;
2483 if (idx)
2484 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2487 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2488 * @qc: Metadata associated with taskfile to check
2490 * Allow low-level driver to filter ATA PACKET commands, returning
2491 * a status indicating whether or not it is OK to use DMA for the
2492 * supplied PACKET command.
2494 * LOCKING:
2495 * spin_lock_irqsave(host_set lock)
2497 * RETURNS: 0 when ATAPI DMA can be used
2498 * nonzero otherwise
2500 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2502 struct ata_port *ap = qc->ap;
2503 int rc = 0; /* Assume ATAPI DMA is OK by default */
2505 if (ap->ops->check_atapi_dma)
2506 rc = ap->ops->check_atapi_dma(qc);
2508 return rc;
2511 * ata_qc_prep - Prepare taskfile for submission
2512 * @qc: Metadata associated with taskfile to be prepared
2514 * Prepare ATA taskfile for submission.
2516 * LOCKING:
2517 * spin_lock_irqsave(host_set lock)
2519 void ata_qc_prep(struct ata_queued_cmd *qc)
2521 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2522 return;
2524 ata_fill_sg(qc);
2528 * ata_sg_init_one - Associate command with memory buffer
2529 * @qc: Command to be associated
2530 * @buf: Memory buffer
2531 * @buflen: Length of memory buffer, in bytes.
2533 * Initialize the data-related elements of queued_cmd @qc
2534 * to point to a single memory buffer, @buf of byte length @buflen.
2536 * LOCKING:
2537 * spin_lock_irqsave(host_set lock)
2540 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2542 struct scatterlist *sg;
2544 qc->flags |= ATA_QCFLAG_SINGLE;
2546 memset(&qc->sgent, 0, sizeof(qc->sgent));
2547 qc->__sg = &qc->sgent;
2548 qc->n_elem = 1;
2549 qc->orig_n_elem = 1;
2550 qc->buf_virt = buf;
2552 sg = qc->__sg;
2553 sg_init_one(sg, buf, buflen);
2557 * ata_sg_init - Associate command with scatter-gather table.
2558 * @qc: Command to be associated
2559 * @sg: Scatter-gather table.
2560 * @n_elem: Number of elements in s/g table.
2562 * Initialize the data-related elements of queued_cmd @qc
2563 * to point to a scatter-gather table @sg, containing @n_elem
2564 * elements.
2566 * LOCKING:
2567 * spin_lock_irqsave(host_set lock)
2570 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2571 unsigned int n_elem)
2573 qc->flags |= ATA_QCFLAG_SG;
2574 qc->__sg = sg;
2575 qc->n_elem = n_elem;
2576 qc->orig_n_elem = n_elem;
2580 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2581 * @qc: Command with memory buffer to be mapped.
2583 * DMA-map the memory buffer associated with queued_cmd @qc.
2585 * LOCKING:
2586 * spin_lock_irqsave(host_set lock)
2588 * RETURNS:
2589 * Zero on success, negative on error.
2592 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2594 struct ata_port *ap = qc->ap;
2595 int dir = qc->dma_dir;
2596 struct scatterlist *sg = qc->__sg;
2597 dma_addr_t dma_address;
2599 /* we must lengthen transfers to end on a 32-bit boundary */
2600 qc->pad_len = sg->length & 3;
2601 if (qc->pad_len) {
2602 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2603 struct scatterlist *psg = &qc->pad_sgent;
2605 assert(qc->dev->class == ATA_DEV_ATAPI);
2607 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2609 if (qc->tf.flags & ATA_TFLAG_WRITE)
2610 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2611 qc->pad_len);
2613 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2614 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2615 /* trim sg */
2616 sg->length -= qc->pad_len;
2618 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2619 sg->length, qc->pad_len);
2622 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
2623 sg->length, dir);
2624 if (dma_mapping_error(dma_address)) {
2625 /* restore sg */
2626 sg->length += qc->pad_len;
2627 return -1;
2630 sg_dma_address(sg) = dma_address;
2631 sg_dma_len(sg) = sg->length;
2633 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2634 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2636 return 0;
2640 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2641 * @qc: Command with scatter-gather table to be mapped.
2643 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2645 * LOCKING:
2646 * spin_lock_irqsave(host_set lock)
2648 * RETURNS:
2649 * Zero on success, negative on error.
2653 static int ata_sg_setup(struct ata_queued_cmd *qc)
2655 struct ata_port *ap = qc->ap;
2656 struct scatterlist *sg = qc->__sg;
2657 struct scatterlist *lsg = &sg[qc->n_elem - 1];
2658 int n_elem, dir;
2660 VPRINTK("ENTER, ata%u\n", ap->id);
2661 assert(qc->flags & ATA_QCFLAG_SG);
2663 /* we must lengthen transfers to end on a 32-bit boundary */
2664 qc->pad_len = lsg->length & 3;
2665 if (qc->pad_len) {
2666 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2667 struct scatterlist *psg = &qc->pad_sgent;
2668 unsigned int offset;
2670 assert(qc->dev->class == ATA_DEV_ATAPI);
2672 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2675 * psg->page/offset are used to copy to-be-written
2676 * data in this function or read data in ata_sg_clean.
2678 offset = lsg->offset + lsg->length - qc->pad_len;
2679 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2680 psg->offset = offset_in_page(offset);
2682 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2683 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2684 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2685 kunmap_atomic(psg->page, KM_IRQ0);
2688 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2689 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2690 /* trim last sg */
2691 lsg->length -= qc->pad_len;
2693 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2694 qc->n_elem - 1, lsg->length, qc->pad_len);
2697 dir = qc->dma_dir;
2698 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2699 if (n_elem < 1) {
2700 /* restore last sg */
2701 lsg->length += qc->pad_len;
2702 return -1;
2705 DPRINTK("%d sg elements mapped\n", n_elem);
2707 qc->n_elem = n_elem;
2709 return 0;
2713 * ata_poll_qc_complete - turn irq back on and finish qc
2714 * @qc: Command to complete
2715 * @err_mask: ATA status register content
2717 * LOCKING:
2718 * None. (grabs host lock)
2721 void ata_poll_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
2723 struct ata_port *ap = qc->ap;
2724 unsigned long flags;
2726 spin_lock_irqsave(&ap->host_set->lock, flags);
2727 ap->flags &= ~ATA_FLAG_NOINTR;
2728 ata_irq_on(ap);
2729 ata_qc_complete(qc, err_mask);
2730 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2734 * ata_pio_poll -
2735 * @ap: the target ata_port
2737 * LOCKING:
2738 * None. (executing in kernel thread context)
2740 * RETURNS:
2741 * timeout value to use
2744 static unsigned long ata_pio_poll(struct ata_port *ap)
2746 u8 status;
2747 unsigned int poll_state = HSM_ST_UNKNOWN;
2748 unsigned int reg_state = HSM_ST_UNKNOWN;
2750 switch (ap->hsm_task_state) {
2751 case HSM_ST:
2752 case HSM_ST_POLL:
2753 poll_state = HSM_ST_POLL;
2754 reg_state = HSM_ST;
2755 break;
2756 case HSM_ST_LAST:
2757 case HSM_ST_LAST_POLL:
2758 poll_state = HSM_ST_LAST_POLL;
2759 reg_state = HSM_ST_LAST;
2760 break;
2761 default:
2762 BUG();
2763 break;
2766 status = ata_chk_status(ap);
2767 if (status & ATA_BUSY) {
2768 if (time_after(jiffies, ap->pio_task_timeout)) {
2769 ap->hsm_task_state = HSM_ST_TMOUT;
2770 return 0;
2772 ap->hsm_task_state = poll_state;
2773 return ATA_SHORT_PAUSE;
2776 ap->hsm_task_state = reg_state;
2777 return 0;
2781 * ata_pio_complete - check if drive is busy or idle
2782 * @ap: the target ata_port
2784 * LOCKING:
2785 * None. (executing in kernel thread context)
2787 * RETURNS:
2788 * Non-zero if qc completed, zero otherwise.
2791 static int ata_pio_complete (struct ata_port *ap)
2793 struct ata_queued_cmd *qc;
2794 u8 drv_stat;
2797 * This is purely heuristic. This is a fast path. Sometimes when
2798 * we enter, BSY will be cleared in a chk-status or two. If not,
2799 * the drive is probably seeking or something. Snooze for a couple
2800 * msecs, then chk-status again. If still busy, fall back to
2801 * HSM_ST_POLL state.
2803 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2804 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2805 msleep(2);
2806 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2807 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2808 ap->hsm_task_state = HSM_ST_LAST_POLL;
2809 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
2810 return 0;
2814 drv_stat = ata_wait_idle(ap);
2815 if (!ata_ok(drv_stat)) {
2816 ap->hsm_task_state = HSM_ST_ERR;
2817 return 0;
2820 qc = ata_qc_from_tag(ap, ap->active_tag);
2821 assert(qc != NULL);
2823 ap->hsm_task_state = HSM_ST_IDLE;
2825 ata_poll_qc_complete(qc, 0);
2827 /* another command may start at this point */
2829 return 1;
2834 * swap_buf_le16 - swap halves of 16-words in place
2835 * @buf: Buffer to swap
2836 * @buf_words: Number of 16-bit words in buffer.
2838 * Swap halves of 16-bit words if needed to convert from
2839 * little-endian byte order to native cpu byte order, or
2840 * vice-versa.
2842 * LOCKING:
2843 * Inherited from caller.
2845 void swap_buf_le16(u16 *buf, unsigned int buf_words)
2847 #ifdef __BIG_ENDIAN
2848 unsigned int i;
2850 for (i = 0; i < buf_words; i++)
2851 buf[i] = le16_to_cpu(buf[i]);
2852 #endif /* __BIG_ENDIAN */
2856 * ata_mmio_data_xfer - Transfer data by MMIO
2857 * @ap: port to read/write
2858 * @buf: data buffer
2859 * @buflen: buffer length
2860 * @write_data: read/write
2862 * Transfer data from/to the device data register by MMIO.
2864 * LOCKING:
2865 * Inherited from caller.
2868 static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2869 unsigned int buflen, int write_data)
2871 unsigned int i;
2872 unsigned int words = buflen >> 1;
2873 u16 *buf16 = (u16 *) buf;
2874 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2876 /* Transfer multiple of 2 bytes */
2877 if (write_data) {
2878 for (i = 0; i < words; i++)
2879 writew(le16_to_cpu(buf16[i]), mmio);
2880 } else {
2881 for (i = 0; i < words; i++)
2882 buf16[i] = cpu_to_le16(readw(mmio));
2885 /* Transfer trailing 1 byte, if any. */
2886 if (unlikely(buflen & 0x01)) {
2887 u16 align_buf[1] = { 0 };
2888 unsigned char *trailing_buf = buf + buflen - 1;
2890 if (write_data) {
2891 memcpy(align_buf, trailing_buf, 1);
2892 writew(le16_to_cpu(align_buf[0]), mmio);
2893 } else {
2894 align_buf[0] = cpu_to_le16(readw(mmio));
2895 memcpy(trailing_buf, align_buf, 1);
2901 * ata_pio_data_xfer - Transfer data by PIO
2902 * @ap: port to read/write
2903 * @buf: data buffer
2904 * @buflen: buffer length
2905 * @write_data: read/write
2907 * Transfer data from/to the device data register by PIO.
2909 * LOCKING:
2910 * Inherited from caller.
2913 static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2914 unsigned int buflen, int write_data)
2916 unsigned int words = buflen >> 1;
2918 /* Transfer multiple of 2 bytes */
2919 if (write_data)
2920 outsw(ap->ioaddr.data_addr, buf, words);
2921 else
2922 insw(ap->ioaddr.data_addr, buf, words);
2924 /* Transfer trailing 1 byte, if any. */
2925 if (unlikely(buflen & 0x01)) {
2926 u16 align_buf[1] = { 0 };
2927 unsigned char *trailing_buf = buf + buflen - 1;
2929 if (write_data) {
2930 memcpy(align_buf, trailing_buf, 1);
2931 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2932 } else {
2933 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2934 memcpy(trailing_buf, align_buf, 1);
2940 * ata_data_xfer - Transfer data from/to the data register.
2941 * @ap: port to read/write
2942 * @buf: data buffer
2943 * @buflen: buffer length
2944 * @do_write: read/write
2946 * Transfer data from/to the device data register.
2948 * LOCKING:
2949 * Inherited from caller.
2952 static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2953 unsigned int buflen, int do_write)
2955 if (ap->flags & ATA_FLAG_MMIO)
2956 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2957 else
2958 ata_pio_data_xfer(ap, buf, buflen, do_write);
2962 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2963 * @qc: Command on going
2965 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2967 * LOCKING:
2968 * Inherited from caller.
2971 static void ata_pio_sector(struct ata_queued_cmd *qc)
2973 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2974 struct scatterlist *sg = qc->__sg;
2975 struct ata_port *ap = qc->ap;
2976 struct page *page;
2977 unsigned int offset;
2978 unsigned char *buf;
2980 if (qc->cursect == (qc->nsect - 1))
2981 ap->hsm_task_state = HSM_ST_LAST;
2983 page = sg[qc->cursg].page;
2984 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2986 /* get the current page and offset */
2987 page = nth_page(page, (offset >> PAGE_SHIFT));
2988 offset %= PAGE_SIZE;
2990 buf = kmap(page) + offset;
2992 qc->cursect++;
2993 qc->cursg_ofs++;
2995 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
2996 qc->cursg++;
2997 qc->cursg_ofs = 0;
3000 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3002 /* do the actual data transfer */
3003 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3004 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3006 kunmap(page);
3010 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3011 * @qc: Command on going
3012 * @bytes: number of bytes
3014 * Transfer Transfer data from/to the ATAPI device.
3016 * LOCKING:
3017 * Inherited from caller.
3021 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3023 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3024 struct scatterlist *sg = qc->__sg;
3025 struct ata_port *ap = qc->ap;
3026 struct page *page;
3027 unsigned char *buf;
3028 unsigned int offset, count;
3030 if (qc->curbytes + bytes >= qc->nbytes)
3031 ap->hsm_task_state = HSM_ST_LAST;
3033 next_sg:
3034 if (unlikely(qc->cursg >= qc->n_elem)) {
3036 * The end of qc->sg is reached and the device expects
3037 * more data to transfer. In order not to overrun qc->sg
3038 * and fulfill length specified in the byte count register,
3039 * - for read case, discard trailing data from the device
3040 * - for write case, padding zero data to the device
3042 u16 pad_buf[1] = { 0 };
3043 unsigned int words = bytes >> 1;
3044 unsigned int i;
3046 if (words) /* warning if bytes > 1 */
3047 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
3048 ap->id, bytes);
3050 for (i = 0; i < words; i++)
3051 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3053 ap->hsm_task_state = HSM_ST_LAST;
3054 return;
3057 sg = &qc->__sg[qc->cursg];
3059 page = sg->page;
3060 offset = sg->offset + qc->cursg_ofs;
3062 /* get the current page and offset */
3063 page = nth_page(page, (offset >> PAGE_SHIFT));
3064 offset %= PAGE_SIZE;
3066 /* don't overrun current sg */
3067 count = min(sg->length - qc->cursg_ofs, bytes);
3069 /* don't cross page boundaries */
3070 count = min(count, (unsigned int)PAGE_SIZE - offset);
3072 buf = kmap(page) + offset;
3074 bytes -= count;
3075 qc->curbytes += count;
3076 qc->cursg_ofs += count;
3078 if (qc->cursg_ofs == sg->length) {
3079 qc->cursg++;
3080 qc->cursg_ofs = 0;
3083 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3085 /* do the actual data transfer */
3086 ata_data_xfer(ap, buf, count, do_write);
3088 kunmap(page);
3090 if (bytes)
3091 goto next_sg;
3095 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3096 * @qc: Command on going
3098 * Transfer Transfer data from/to the ATAPI device.
3100 * LOCKING:
3101 * Inherited from caller.
3104 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3106 struct ata_port *ap = qc->ap;
3107 struct ata_device *dev = qc->dev;
3108 unsigned int ireason, bc_lo, bc_hi, bytes;
3109 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3111 ap->ops->tf_read(ap, &qc->tf);
3112 ireason = qc->tf.nsect;
3113 bc_lo = qc->tf.lbam;
3114 bc_hi = qc->tf.lbah;
3115 bytes = (bc_hi << 8) | bc_lo;
3117 /* shall be cleared to zero, indicating xfer of data */
3118 if (ireason & (1 << 0))
3119 goto err_out;
3121 /* make sure transfer direction matches expected */
3122 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3123 if (do_write != i_write)
3124 goto err_out;
3126 __atapi_pio_bytes(qc, bytes);
3128 return;
3130 err_out:
3131 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3132 ap->id, dev->devno);
3133 ap->hsm_task_state = HSM_ST_ERR;
3137 * ata_pio_block - start PIO on a block
3138 * @ap: the target ata_port
3140 * LOCKING:
3141 * None. (executing in kernel thread context)
3144 static void ata_pio_block(struct ata_port *ap)
3146 struct ata_queued_cmd *qc;
3147 u8 status;
3150 * This is purely heuristic. This is a fast path.
3151 * Sometimes when we enter, BSY will be cleared in
3152 * a chk-status or two. If not, the drive is probably seeking
3153 * or something. Snooze for a couple msecs, then
3154 * chk-status again. If still busy, fall back to
3155 * HSM_ST_POLL state.
3157 status = ata_busy_wait(ap, ATA_BUSY, 5);
3158 if (status & ATA_BUSY) {
3159 msleep(2);
3160 status = ata_busy_wait(ap, ATA_BUSY, 10);
3161 if (status & ATA_BUSY) {
3162 ap->hsm_task_state = HSM_ST_POLL;
3163 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3164 return;
3168 qc = ata_qc_from_tag(ap, ap->active_tag);
3169 assert(qc != NULL);
3171 if (is_atapi_taskfile(&qc->tf)) {
3172 /* no more data to transfer or unsupported ATAPI command */
3173 if ((status & ATA_DRQ) == 0) {
3174 ap->hsm_task_state = HSM_ST_LAST;
3175 return;
3178 atapi_pio_bytes(qc);
3179 } else {
3180 /* handle BSY=0, DRQ=0 as error */
3181 if ((status & ATA_DRQ) == 0) {
3182 ap->hsm_task_state = HSM_ST_ERR;
3183 return;
3186 ata_pio_sector(qc);
3190 static void ata_pio_error(struct ata_port *ap)
3192 struct ata_queued_cmd *qc;
3194 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3196 qc = ata_qc_from_tag(ap, ap->active_tag);
3197 assert(qc != NULL);
3199 ap->hsm_task_state = HSM_ST_IDLE;
3201 ata_poll_qc_complete(qc, AC_ERR_ATA_BUS);
3204 static void ata_pio_task(void *_data)
3206 struct ata_port *ap = _data;
3207 unsigned long timeout;
3208 int qc_completed;
3210 fsm_start:
3211 timeout = 0;
3212 qc_completed = 0;
3214 switch (ap->hsm_task_state) {
3215 case HSM_ST_IDLE:
3216 return;
3218 case HSM_ST:
3219 ata_pio_block(ap);
3220 break;
3222 case HSM_ST_LAST:
3223 qc_completed = ata_pio_complete(ap);
3224 break;
3226 case HSM_ST_POLL:
3227 case HSM_ST_LAST_POLL:
3228 timeout = ata_pio_poll(ap);
3229 break;
3231 case HSM_ST_TMOUT:
3232 case HSM_ST_ERR:
3233 ata_pio_error(ap);
3234 return;
3237 if (timeout)
3238 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3239 else if (!qc_completed)
3240 goto fsm_start;
3244 * ata_qc_timeout - Handle timeout of queued command
3245 * @qc: Command that timed out
3247 * Some part of the kernel (currently, only the SCSI layer)
3248 * has noticed that the active command on port @ap has not
3249 * completed after a specified length of time. Handle this
3250 * condition by disabling DMA (if necessary) and completing
3251 * transactions, with error if necessary.
3253 * This also handles the case of the "lost interrupt", where
3254 * for some reason (possibly hardware bug, possibly driver bug)
3255 * an interrupt was not delivered to the driver, even though the
3256 * transaction completed successfully.
3258 * LOCKING:
3259 * Inherited from SCSI layer (none, can sleep)
3262 static void ata_qc_timeout(struct ata_queued_cmd *qc)
3264 struct ata_port *ap = qc->ap;
3265 struct ata_host_set *host_set = ap->host_set;
3266 struct ata_device *dev = qc->dev;
3267 u8 host_stat = 0, drv_stat;
3268 unsigned long flags;
3270 DPRINTK("ENTER\n");
3272 /* FIXME: doesn't this conflict with timeout handling? */
3273 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3274 struct scsi_cmnd *cmd = qc->scsicmd;
3276 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
3278 /* finish completing original command */
3279 spin_lock_irqsave(&host_set->lock, flags);
3280 __ata_qc_complete(qc);
3281 spin_unlock_irqrestore(&host_set->lock, flags);
3283 atapi_request_sense(ap, dev, cmd);
3285 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3286 scsi_finish_command(cmd);
3288 goto out;
3292 spin_lock_irqsave(&host_set->lock, flags);
3294 /* hack alert! We cannot use the supplied completion
3295 * function from inside the ->eh_strategy_handler() thread.
3296 * libata is the only user of ->eh_strategy_handler() in
3297 * any kernel, so the default scsi_done() assumes it is
3298 * not being called from the SCSI EH.
3300 qc->scsidone = scsi_finish_command;
3302 switch (qc->tf.protocol) {
3304 case ATA_PROT_DMA:
3305 case ATA_PROT_ATAPI_DMA:
3306 host_stat = ap->ops->bmdma_status(ap);
3308 /* before we do anything else, clear DMA-Start bit */
3309 ap->ops->bmdma_stop(qc);
3311 /* fall through */
3313 default:
3314 ata_altstatus(ap);
3315 drv_stat = ata_chk_status(ap);
3317 /* ack bmdma irq events */
3318 ap->ops->irq_clear(ap);
3320 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3321 ap->id, qc->tf.command, drv_stat, host_stat);
3323 /* complete taskfile transaction */
3324 ata_qc_complete(qc, ac_err_mask(drv_stat));
3325 break;
3328 spin_unlock_irqrestore(&host_set->lock, flags);
3330 out:
3331 DPRINTK("EXIT\n");
3335 * ata_eng_timeout - Handle timeout of queued command
3336 * @ap: Port on which timed-out command is active
3338 * Some part of the kernel (currently, only the SCSI layer)
3339 * has noticed that the active command on port @ap has not
3340 * completed after a specified length of time. Handle this
3341 * condition by disabling DMA (if necessary) and completing
3342 * transactions, with error if necessary.
3344 * This also handles the case of the "lost interrupt", where
3345 * for some reason (possibly hardware bug, possibly driver bug)
3346 * an interrupt was not delivered to the driver, even though the
3347 * transaction completed successfully.
3349 * LOCKING:
3350 * Inherited from SCSI layer (none, can sleep)
3353 void ata_eng_timeout(struct ata_port *ap)
3355 struct ata_queued_cmd *qc;
3357 DPRINTK("ENTER\n");
3359 qc = ata_qc_from_tag(ap, ap->active_tag);
3360 if (qc)
3361 ata_qc_timeout(qc);
3362 else {
3363 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3364 ap->id);
3365 goto out;
3368 out:
3369 DPRINTK("EXIT\n");
3373 * ata_qc_new - Request an available ATA command, for queueing
3374 * @ap: Port associated with device @dev
3375 * @dev: Device from whom we request an available command structure
3377 * LOCKING:
3378 * None.
3381 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3383 struct ata_queued_cmd *qc = NULL;
3384 unsigned int i;
3386 for (i = 0; i < ATA_MAX_QUEUE; i++)
3387 if (!test_and_set_bit(i, &ap->qactive)) {
3388 qc = ata_qc_from_tag(ap, i);
3389 break;
3392 if (qc)
3393 qc->tag = i;
3395 return qc;
3399 * ata_qc_new_init - Request an available ATA command, and initialize it
3400 * @ap: Port associated with device @dev
3401 * @dev: Device from whom we request an available command structure
3403 * LOCKING:
3404 * None.
3407 struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3408 struct ata_device *dev)
3410 struct ata_queued_cmd *qc;
3412 qc = ata_qc_new(ap);
3413 if (qc) {
3414 qc->__sg = NULL;
3415 qc->flags = 0;
3416 qc->scsicmd = NULL;
3417 qc->ap = ap;
3418 qc->dev = dev;
3419 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3420 qc->nsect = 0;
3421 qc->nbytes = qc->curbytes = 0;
3423 ata_tf_init(ap, &qc->tf, dev->devno);
3426 return qc;
3429 int ata_qc_complete_noop(struct ata_queued_cmd *qc, unsigned int err_mask)
3431 return 0;
3434 static void __ata_qc_complete(struct ata_queued_cmd *qc)
3436 struct ata_port *ap = qc->ap;
3437 unsigned int tag, do_clear = 0;
3439 qc->flags = 0;
3440 tag = qc->tag;
3441 if (likely(ata_tag_valid(tag))) {
3442 if (tag == ap->active_tag)
3443 ap->active_tag = ATA_TAG_POISON;
3444 qc->tag = ATA_TAG_POISON;
3445 do_clear = 1;
3448 if (qc->waiting) {
3449 struct completion *waiting = qc->waiting;
3450 qc->waiting = NULL;
3451 complete(waiting);
3454 if (likely(do_clear))
3455 clear_bit(tag, &ap->qactive);
3459 * ata_qc_free - free unused ata_queued_cmd
3460 * @qc: Command to complete
3462 * Designed to free unused ata_queued_cmd object
3463 * in case something prevents using it.
3465 * LOCKING:
3466 * spin_lock_irqsave(host_set lock)
3468 void ata_qc_free(struct ata_queued_cmd *qc)
3470 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3471 assert(qc->waiting == NULL); /* nothing should be waiting */
3473 __ata_qc_complete(qc);
3477 * ata_qc_complete - Complete an active ATA command
3478 * @qc: Command to complete
3479 * @err_mask: ATA Status register contents
3481 * Indicate to the mid and upper layers that an ATA
3482 * command has completed, with either an ok or not-ok status.
3484 * LOCKING:
3485 * spin_lock_irqsave(host_set lock)
3488 void ata_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
3490 int rc;
3492 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3493 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3495 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3496 ata_sg_clean(qc);
3498 /* atapi: mark qc as inactive to prevent the interrupt handler
3499 * from completing the command twice later, before the error handler
3500 * is called. (when rc != 0 and atapi request sense is needed)
3502 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3504 /* call completion callback */
3505 rc = qc->complete_fn(qc, err_mask);
3507 /* if callback indicates not to complete command (non-zero),
3508 * return immediately
3510 if (rc != 0)
3511 return;
3513 __ata_qc_complete(qc);
3515 VPRINTK("EXIT\n");
3518 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3520 struct ata_port *ap = qc->ap;
3522 switch (qc->tf.protocol) {
3523 case ATA_PROT_DMA:
3524 case ATA_PROT_ATAPI_DMA:
3525 return 1;
3527 case ATA_PROT_ATAPI:
3528 case ATA_PROT_PIO:
3529 case ATA_PROT_PIO_MULT:
3530 if (ap->flags & ATA_FLAG_PIO_DMA)
3531 return 1;
3533 /* fall through */
3535 default:
3536 return 0;
3539 /* never reached */
3543 * ata_qc_issue - issue taskfile to device
3544 * @qc: command to issue to device
3546 * Prepare an ATA command to submission to device.
3547 * This includes mapping the data into a DMA-able
3548 * area, filling in the S/G table, and finally
3549 * writing the taskfile to hardware, starting the command.
3551 * LOCKING:
3552 * spin_lock_irqsave(host_set lock)
3554 * RETURNS:
3555 * Zero on success, negative on error.
3558 int ata_qc_issue(struct ata_queued_cmd *qc)
3560 struct ata_port *ap = qc->ap;
3562 if (ata_should_dma_map(qc)) {
3563 if (qc->flags & ATA_QCFLAG_SG) {
3564 if (ata_sg_setup(qc))
3565 goto err_out;
3566 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3567 if (ata_sg_setup_one(qc))
3568 goto err_out;
3570 } else {
3571 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3574 ap->ops->qc_prep(qc);
3576 qc->ap->active_tag = qc->tag;
3577 qc->flags |= ATA_QCFLAG_ACTIVE;
3579 return ap->ops->qc_issue(qc);
3581 err_out:
3582 return -1;
3587 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3588 * @qc: command to issue to device
3590 * Using various libata functions and hooks, this function
3591 * starts an ATA command. ATA commands are grouped into
3592 * classes called "protocols", and issuing each type of protocol
3593 * is slightly different.
3595 * May be used as the qc_issue() entry in ata_port_operations.
3597 * LOCKING:
3598 * spin_lock_irqsave(host_set lock)
3600 * RETURNS:
3601 * Zero on success, negative on error.
3604 int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3606 struct ata_port *ap = qc->ap;
3608 ata_dev_select(ap, qc->dev->devno, 1, 0);
3610 switch (qc->tf.protocol) {
3611 case ATA_PROT_NODATA:
3612 ata_tf_to_host(ap, &qc->tf);
3613 break;
3615 case ATA_PROT_DMA:
3616 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3617 ap->ops->bmdma_setup(qc); /* set up bmdma */
3618 ap->ops->bmdma_start(qc); /* initiate bmdma */
3619 break;
3621 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3622 ata_qc_set_polling(qc);
3623 ata_tf_to_host(ap, &qc->tf);
3624 ap->hsm_task_state = HSM_ST;
3625 queue_work(ata_wq, &ap->pio_task);
3626 break;
3628 case ATA_PROT_ATAPI:
3629 ata_qc_set_polling(qc);
3630 ata_tf_to_host(ap, &qc->tf);
3631 queue_work(ata_wq, &ap->packet_task);
3632 break;
3634 case ATA_PROT_ATAPI_NODATA:
3635 ap->flags |= ATA_FLAG_NOINTR;
3636 ata_tf_to_host(ap, &qc->tf);
3637 queue_work(ata_wq, &ap->packet_task);
3638 break;
3640 case ATA_PROT_ATAPI_DMA:
3641 ap->flags |= ATA_FLAG_NOINTR;
3642 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3643 ap->ops->bmdma_setup(qc); /* set up bmdma */
3644 queue_work(ata_wq, &ap->packet_task);
3645 break;
3647 default:
3648 WARN_ON(1);
3649 return -1;
3652 return 0;
3656 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3657 * @qc: Info associated with this ATA transaction.
3659 * LOCKING:
3660 * spin_lock_irqsave(host_set lock)
3663 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3665 struct ata_port *ap = qc->ap;
3666 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3667 u8 dmactl;
3668 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3670 /* load PRD table addr. */
3671 mb(); /* make sure PRD table writes are visible to controller */
3672 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3674 /* specify data direction, triple-check start bit is clear */
3675 dmactl = readb(mmio + ATA_DMA_CMD);
3676 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3677 if (!rw)
3678 dmactl |= ATA_DMA_WR;
3679 writeb(dmactl, mmio + ATA_DMA_CMD);
3681 /* issue r/w command */
3682 ap->ops->exec_command(ap, &qc->tf);
3686 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3687 * @qc: Info associated with this ATA transaction.
3689 * LOCKING:
3690 * spin_lock_irqsave(host_set lock)
3693 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3695 struct ata_port *ap = qc->ap;
3696 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3697 u8 dmactl;
3699 /* start host DMA transaction */
3700 dmactl = readb(mmio + ATA_DMA_CMD);
3701 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3703 /* Strictly, one may wish to issue a readb() here, to
3704 * flush the mmio write. However, control also passes
3705 * to the hardware at this point, and it will interrupt
3706 * us when we are to resume control. So, in effect,
3707 * we don't care when the mmio write flushes.
3708 * Further, a read of the DMA status register _immediately_
3709 * following the write may not be what certain flaky hardware
3710 * is expected, so I think it is best to not add a readb()
3711 * without first all the MMIO ATA cards/mobos.
3712 * Or maybe I'm just being paranoid.
3717 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3718 * @qc: Info associated with this ATA transaction.
3720 * LOCKING:
3721 * spin_lock_irqsave(host_set lock)
3724 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3726 struct ata_port *ap = qc->ap;
3727 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3728 u8 dmactl;
3730 /* load PRD table addr. */
3731 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3733 /* specify data direction, triple-check start bit is clear */
3734 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3735 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3736 if (!rw)
3737 dmactl |= ATA_DMA_WR;
3738 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3740 /* issue r/w command */
3741 ap->ops->exec_command(ap, &qc->tf);
3745 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3746 * @qc: Info associated with this ATA transaction.
3748 * LOCKING:
3749 * spin_lock_irqsave(host_set lock)
3752 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3754 struct ata_port *ap = qc->ap;
3755 u8 dmactl;
3757 /* start host DMA transaction */
3758 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3759 outb(dmactl | ATA_DMA_START,
3760 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3765 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3766 * @qc: Info associated with this ATA transaction.
3768 * Writes the ATA_DMA_START flag to the DMA command register.
3770 * May be used as the bmdma_start() entry in ata_port_operations.
3772 * LOCKING:
3773 * spin_lock_irqsave(host_set lock)
3775 void ata_bmdma_start(struct ata_queued_cmd *qc)
3777 if (qc->ap->flags & ATA_FLAG_MMIO)
3778 ata_bmdma_start_mmio(qc);
3779 else
3780 ata_bmdma_start_pio(qc);
3785 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3786 * @qc: Info associated with this ATA transaction.
3788 * Writes address of PRD table to device's PRD Table Address
3789 * register, sets the DMA control register, and calls
3790 * ops->exec_command() to start the transfer.
3792 * May be used as the bmdma_setup() entry in ata_port_operations.
3794 * LOCKING:
3795 * spin_lock_irqsave(host_set lock)
3797 void ata_bmdma_setup(struct ata_queued_cmd *qc)
3799 if (qc->ap->flags & ATA_FLAG_MMIO)
3800 ata_bmdma_setup_mmio(qc);
3801 else
3802 ata_bmdma_setup_pio(qc);
3807 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3808 * @ap: Port associated with this ATA transaction.
3810 * Clear interrupt and error flags in DMA status register.
3812 * May be used as the irq_clear() entry in ata_port_operations.
3814 * LOCKING:
3815 * spin_lock_irqsave(host_set lock)
3818 void ata_bmdma_irq_clear(struct ata_port *ap)
3820 if (ap->flags & ATA_FLAG_MMIO) {
3821 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3822 writeb(readb(mmio), mmio);
3823 } else {
3824 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3825 outb(inb(addr), addr);
3832 * ata_bmdma_status - Read PCI IDE BMDMA status
3833 * @ap: Port associated with this ATA transaction.
3835 * Read and return BMDMA status register.
3837 * May be used as the bmdma_status() entry in ata_port_operations.
3839 * LOCKING:
3840 * spin_lock_irqsave(host_set lock)
3843 u8 ata_bmdma_status(struct ata_port *ap)
3845 u8 host_stat;
3846 if (ap->flags & ATA_FLAG_MMIO) {
3847 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3848 host_stat = readb(mmio + ATA_DMA_STATUS);
3849 } else
3850 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3851 return host_stat;
3856 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3857 * @qc: Command we are ending DMA for
3859 * Clears the ATA_DMA_START flag in the dma control register
3861 * May be used as the bmdma_stop() entry in ata_port_operations.
3863 * LOCKING:
3864 * spin_lock_irqsave(host_set lock)
3867 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3869 struct ata_port *ap = qc->ap;
3870 if (ap->flags & ATA_FLAG_MMIO) {
3871 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3873 /* clear start/stop bit */
3874 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3875 mmio + ATA_DMA_CMD);
3876 } else {
3877 /* clear start/stop bit */
3878 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3879 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3882 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3883 ata_altstatus(ap); /* dummy read */
3887 * ata_host_intr - Handle host interrupt for given (port, task)
3888 * @ap: Port on which interrupt arrived (possibly...)
3889 * @qc: Taskfile currently active in engine
3891 * Handle host interrupt for given queued command. Currently,
3892 * only DMA interrupts are handled. All other commands are
3893 * handled via polling with interrupts disabled (nIEN bit).
3895 * LOCKING:
3896 * spin_lock_irqsave(host_set lock)
3898 * RETURNS:
3899 * One if interrupt was handled, zero if not (shared irq).
3902 inline unsigned int ata_host_intr (struct ata_port *ap,
3903 struct ata_queued_cmd *qc)
3905 u8 status, host_stat;
3907 switch (qc->tf.protocol) {
3909 case ATA_PROT_DMA:
3910 case ATA_PROT_ATAPI_DMA:
3911 case ATA_PROT_ATAPI:
3912 /* check status of DMA engine */
3913 host_stat = ap->ops->bmdma_status(ap);
3914 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3916 /* if it's not our irq... */
3917 if (!(host_stat & ATA_DMA_INTR))
3918 goto idle_irq;
3920 /* before we do anything else, clear DMA-Start bit */
3921 ap->ops->bmdma_stop(qc);
3923 /* fall through */
3925 case ATA_PROT_ATAPI_NODATA:
3926 case ATA_PROT_NODATA:
3927 /* check altstatus */
3928 status = ata_altstatus(ap);
3929 if (status & ATA_BUSY)
3930 goto idle_irq;
3932 /* check main status, clearing INTRQ */
3933 status = ata_chk_status(ap);
3934 if (unlikely(status & ATA_BUSY))
3935 goto idle_irq;
3936 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3937 ap->id, qc->tf.protocol, status);
3939 /* ack bmdma irq events */
3940 ap->ops->irq_clear(ap);
3942 /* complete taskfile transaction */
3943 ata_qc_complete(qc, ac_err_mask(status));
3944 break;
3946 default:
3947 goto idle_irq;
3950 return 1; /* irq handled */
3952 idle_irq:
3953 ap->stats.idle_irq++;
3955 #ifdef ATA_IRQ_TRAP
3956 if ((ap->stats.idle_irq % 1000) == 0) {
3957 handled = 1;
3958 ata_irq_ack(ap, 0); /* debug trap */
3959 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3961 #endif
3962 return 0; /* irq not handled */
3966 * ata_interrupt - Default ATA host interrupt handler
3967 * @irq: irq line (unused)
3968 * @dev_instance: pointer to our ata_host_set information structure
3969 * @regs: unused
3971 * Default interrupt handler for PCI IDE devices. Calls
3972 * ata_host_intr() for each port that is not disabled.
3974 * LOCKING:
3975 * Obtains host_set lock during operation.
3977 * RETURNS:
3978 * IRQ_NONE or IRQ_HANDLED.
3981 irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3983 struct ata_host_set *host_set = dev_instance;
3984 unsigned int i;
3985 unsigned int handled = 0;
3986 unsigned long flags;
3988 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3989 spin_lock_irqsave(&host_set->lock, flags);
3991 for (i = 0; i < host_set->n_ports; i++) {
3992 struct ata_port *ap;
3994 ap = host_set->ports[i];
3995 if (ap &&
3996 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
3997 struct ata_queued_cmd *qc;
3999 qc = ata_qc_from_tag(ap, ap->active_tag);
4000 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4001 (qc->flags & ATA_QCFLAG_ACTIVE))
4002 handled |= ata_host_intr(ap, qc);
4006 spin_unlock_irqrestore(&host_set->lock, flags);
4008 return IRQ_RETVAL(handled);
4012 * atapi_packet_task - Write CDB bytes to hardware
4013 * @_data: Port to which ATAPI device is attached.
4015 * When device has indicated its readiness to accept
4016 * a CDB, this function is called. Send the CDB.
4017 * If DMA is to be performed, exit immediately.
4018 * Otherwise, we are in polling mode, so poll
4019 * status under operation succeeds or fails.
4021 * LOCKING:
4022 * Kernel thread context (may sleep)
4025 static void atapi_packet_task(void *_data)
4027 struct ata_port *ap = _data;
4028 struct ata_queued_cmd *qc;
4029 u8 status;
4031 qc = ata_qc_from_tag(ap, ap->active_tag);
4032 assert(qc != NULL);
4033 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4035 /* sleep-wait for BSY to clear */
4036 DPRINTK("busy wait\n");
4037 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
4038 goto err_out_status;
4040 /* make sure DRQ is set */
4041 status = ata_chk_status(ap);
4042 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
4043 goto err_out;
4045 /* send SCSI cdb */
4046 DPRINTK("send cdb\n");
4047 assert(ap->cdb_len >= 12);
4049 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4050 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4051 unsigned long flags;
4053 /* Once we're done issuing command and kicking bmdma,
4054 * irq handler takes over. To not lose irq, we need
4055 * to clear NOINTR flag before sending cdb, but
4056 * interrupt handler shouldn't be invoked before we're
4057 * finished. Hence, the following locking.
4059 spin_lock_irqsave(&ap->host_set->lock, flags);
4060 ap->flags &= ~ATA_FLAG_NOINTR;
4061 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4062 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4063 ap->ops->bmdma_start(qc); /* initiate bmdma */
4064 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4065 } else {
4066 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4068 /* PIO commands are handled by polling */
4069 ap->hsm_task_state = HSM_ST;
4070 queue_work(ata_wq, &ap->pio_task);
4073 return;
4075 err_out_status:
4076 status = ata_chk_status(ap);
4077 err_out:
4078 ata_poll_qc_complete(qc, __ac_err_mask(status));
4083 * ata_port_start - Set port up for dma.
4084 * @ap: Port to initialize
4086 * Called just after data structures for each port are
4087 * initialized. Allocates space for PRD table.
4089 * May be used as the port_start() entry in ata_port_operations.
4091 * LOCKING:
4092 * Inherited from caller.
4095 int ata_port_start (struct ata_port *ap)
4097 struct device *dev = ap->host_set->dev;
4098 int rc;
4100 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4101 if (!ap->prd)
4102 return -ENOMEM;
4104 rc = ata_pad_alloc(ap, dev);
4105 if (rc) {
4106 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4107 return rc;
4110 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4112 return 0;
4117 * ata_port_stop - Undo ata_port_start()
4118 * @ap: Port to shut down
4120 * Frees the PRD table.
4122 * May be used as the port_stop() entry in ata_port_operations.
4124 * LOCKING:
4125 * Inherited from caller.
4128 void ata_port_stop (struct ata_port *ap)
4130 struct device *dev = ap->host_set->dev;
4132 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4133 ata_pad_free(ap, dev);
4136 void ata_host_stop (struct ata_host_set *host_set)
4138 if (host_set->mmio_base)
4139 iounmap(host_set->mmio_base);
4144 * ata_host_remove - Unregister SCSI host structure with upper layers
4145 * @ap: Port to unregister
4146 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4148 * LOCKING:
4149 * Inherited from caller.
4152 static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4154 struct Scsi_Host *sh = ap->host;
4156 DPRINTK("ENTER\n");
4158 if (do_unregister)
4159 scsi_remove_host(sh);
4161 ap->ops->port_stop(ap);
4165 * ata_host_init - Initialize an ata_port structure
4166 * @ap: Structure to initialize
4167 * @host: associated SCSI mid-layer structure
4168 * @host_set: Collection of hosts to which @ap belongs
4169 * @ent: Probe information provided by low-level driver
4170 * @port_no: Port number associated with this ata_port
4172 * Initialize a new ata_port structure, and its associated
4173 * scsi_host.
4175 * LOCKING:
4176 * Inherited from caller.
4179 static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4180 struct ata_host_set *host_set,
4181 const struct ata_probe_ent *ent, unsigned int port_no)
4183 unsigned int i;
4185 host->max_id = 16;
4186 host->max_lun = 1;
4187 host->max_channel = 1;
4188 host->unique_id = ata_unique_id++;
4189 host->max_cmd_len = 12;
4191 ap->flags = ATA_FLAG_PORT_DISABLED;
4192 ap->id = host->unique_id;
4193 ap->host = host;
4194 ap->ctl = ATA_DEVCTL_OBS;
4195 ap->host_set = host_set;
4196 ap->port_no = port_no;
4197 ap->hard_port_no =
4198 ent->legacy_mode ? ent->hard_port_no : port_no;
4199 ap->pio_mask = ent->pio_mask;
4200 ap->mwdma_mask = ent->mwdma_mask;
4201 ap->udma_mask = ent->udma_mask;
4202 ap->flags |= ent->host_flags;
4203 ap->ops = ent->port_ops;
4204 ap->cbl = ATA_CBL_NONE;
4205 ap->active_tag = ATA_TAG_POISON;
4206 ap->last_ctl = 0xFF;
4208 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4209 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4211 for (i = 0; i < ATA_MAX_DEVICES; i++)
4212 ap->device[i].devno = i;
4214 #ifdef ATA_IRQ_TRAP
4215 ap->stats.unhandled_irq = 1;
4216 ap->stats.idle_irq = 1;
4217 #endif
4219 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4223 * ata_host_add - Attach low-level ATA driver to system
4224 * @ent: Information provided by low-level driver
4225 * @host_set: Collections of ports to which we add
4226 * @port_no: Port number associated with this host
4228 * Attach low-level ATA driver to system.
4230 * LOCKING:
4231 * PCI/etc. bus probe sem.
4233 * RETURNS:
4234 * New ata_port on success, for NULL on error.
4237 static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
4238 struct ata_host_set *host_set,
4239 unsigned int port_no)
4241 struct Scsi_Host *host;
4242 struct ata_port *ap;
4243 int rc;
4245 DPRINTK("ENTER\n");
4246 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4247 if (!host)
4248 return NULL;
4250 ap = (struct ata_port *) &host->hostdata[0];
4252 ata_host_init(ap, host, host_set, ent, port_no);
4254 rc = ap->ops->port_start(ap);
4255 if (rc)
4256 goto err_out;
4258 return ap;
4260 err_out:
4261 scsi_host_put(host);
4262 return NULL;
4266 * ata_device_add - Register hardware device with ATA and SCSI layers
4267 * @ent: Probe information describing hardware device to be registered
4269 * This function processes the information provided in the probe
4270 * information struct @ent, allocates the necessary ATA and SCSI
4271 * host information structures, initializes them, and registers
4272 * everything with requisite kernel subsystems.
4274 * This function requests irqs, probes the ATA bus, and probes
4275 * the SCSI bus.
4277 * LOCKING:
4278 * PCI/etc. bus probe sem.
4280 * RETURNS:
4281 * Number of ports registered. Zero on error (no ports registered).
4284 int ata_device_add(const struct ata_probe_ent *ent)
4286 unsigned int count = 0, i;
4287 struct device *dev = ent->dev;
4288 struct ata_host_set *host_set;
4290 DPRINTK("ENTER\n");
4291 /* alloc a container for our list of ATA ports (buses) */
4292 host_set = kzalloc(sizeof(struct ata_host_set) +
4293 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4294 if (!host_set)
4295 return 0;
4296 spin_lock_init(&host_set->lock);
4298 host_set->dev = dev;
4299 host_set->n_ports = ent->n_ports;
4300 host_set->irq = ent->irq;
4301 host_set->mmio_base = ent->mmio_base;
4302 host_set->private_data = ent->private_data;
4303 host_set->ops = ent->port_ops;
4305 /* register each port bound to this device */
4306 for (i = 0; i < ent->n_ports; i++) {
4307 struct ata_port *ap;
4308 unsigned long xfer_mode_mask;
4310 ap = ata_host_add(ent, host_set, i);
4311 if (!ap)
4312 goto err_out;
4314 host_set->ports[i] = ap;
4315 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4316 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4317 (ap->pio_mask << ATA_SHIFT_PIO);
4319 /* print per-port info to dmesg */
4320 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4321 "bmdma 0x%lX irq %lu\n",
4322 ap->id,
4323 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4324 ata_mode_string(xfer_mode_mask),
4325 ap->ioaddr.cmd_addr,
4326 ap->ioaddr.ctl_addr,
4327 ap->ioaddr.bmdma_addr,
4328 ent->irq);
4330 ata_chk_status(ap);
4331 host_set->ops->irq_clear(ap);
4332 count++;
4335 if (!count)
4336 goto err_free_ret;
4338 /* obtain irq, that is shared between channels */
4339 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4340 DRV_NAME, host_set))
4341 goto err_out;
4343 /* perform each probe synchronously */
4344 DPRINTK("probe begin\n");
4345 for (i = 0; i < count; i++) {
4346 struct ata_port *ap;
4347 int rc;
4349 ap = host_set->ports[i];
4351 DPRINTK("ata%u: probe begin\n", ap->id);
4352 rc = ata_bus_probe(ap);
4353 DPRINTK("ata%u: probe end\n", ap->id);
4355 if (rc) {
4356 /* FIXME: do something useful here?
4357 * Current libata behavior will
4358 * tear down everything when
4359 * the module is removed
4360 * or the h/w is unplugged.
4364 rc = scsi_add_host(ap->host, dev);
4365 if (rc) {
4366 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4367 ap->id);
4368 /* FIXME: do something useful here */
4369 /* FIXME: handle unconditional calls to
4370 * scsi_scan_host and ata_host_remove, below,
4371 * at the very least
4376 /* probes are done, now scan each port's disk(s) */
4377 DPRINTK("probe begin\n");
4378 for (i = 0; i < count; i++) {
4379 struct ata_port *ap = host_set->ports[i];
4381 ata_scsi_scan_host(ap);
4384 dev_set_drvdata(dev, host_set);
4386 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4387 return ent->n_ports; /* success */
4389 err_out:
4390 for (i = 0; i < count; i++) {
4391 ata_host_remove(host_set->ports[i], 1);
4392 scsi_host_put(host_set->ports[i]->host);
4394 err_free_ret:
4395 kfree(host_set);
4396 VPRINTK("EXIT, returning 0\n");
4397 return 0;
4401 * ata_host_set_remove - PCI layer callback for device removal
4402 * @host_set: ATA host set that was removed
4404 * Unregister all objects associated with this host set. Free those
4405 * objects.
4407 * LOCKING:
4408 * Inherited from calling layer (may sleep).
4411 void ata_host_set_remove(struct ata_host_set *host_set)
4413 struct ata_port *ap;
4414 unsigned int i;
4416 for (i = 0; i < host_set->n_ports; i++) {
4417 ap = host_set->ports[i];
4418 scsi_remove_host(ap->host);
4421 free_irq(host_set->irq, host_set);
4423 for (i = 0; i < host_set->n_ports; i++) {
4424 ap = host_set->ports[i];
4426 ata_scsi_release(ap->host);
4428 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4429 struct ata_ioports *ioaddr = &ap->ioaddr;
4431 if (ioaddr->cmd_addr == 0x1f0)
4432 release_region(0x1f0, 8);
4433 else if (ioaddr->cmd_addr == 0x170)
4434 release_region(0x170, 8);
4437 scsi_host_put(ap->host);
4440 if (host_set->ops->host_stop)
4441 host_set->ops->host_stop(host_set);
4443 kfree(host_set);
4447 * ata_scsi_release - SCSI layer callback hook for host unload
4448 * @host: libata host to be unloaded
4450 * Performs all duties necessary to shut down a libata port...
4451 * Kill port kthread, disable port, and release resources.
4453 * LOCKING:
4454 * Inherited from SCSI layer.
4456 * RETURNS:
4457 * One.
4460 int ata_scsi_release(struct Scsi_Host *host)
4462 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4464 DPRINTK("ENTER\n");
4466 ap->ops->port_disable(ap);
4467 ata_host_remove(ap, 0);
4469 DPRINTK("EXIT\n");
4470 return 1;
4474 * ata_std_ports - initialize ioaddr with standard port offsets.
4475 * @ioaddr: IO address structure to be initialized
4477 * Utility function which initializes data_addr, error_addr,
4478 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4479 * device_addr, status_addr, and command_addr to standard offsets
4480 * relative to cmd_addr.
4482 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4485 void ata_std_ports(struct ata_ioports *ioaddr)
4487 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4488 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4489 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4490 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4491 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4492 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4493 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4494 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4495 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4496 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4499 static struct ata_probe_ent *
4500 ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
4502 struct ata_probe_ent *probe_ent;
4504 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
4505 if (!probe_ent) {
4506 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4507 kobject_name(&(dev->kobj)));
4508 return NULL;
4511 INIT_LIST_HEAD(&probe_ent->node);
4512 probe_ent->dev = dev;
4514 probe_ent->sht = port->sht;
4515 probe_ent->host_flags = port->host_flags;
4516 probe_ent->pio_mask = port->pio_mask;
4517 probe_ent->mwdma_mask = port->mwdma_mask;
4518 probe_ent->udma_mask = port->udma_mask;
4519 probe_ent->port_ops = port->port_ops;
4521 return probe_ent;
4526 #ifdef CONFIG_PCI
4528 void ata_pci_host_stop (struct ata_host_set *host_set)
4530 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4532 pci_iounmap(pdev, host_set->mmio_base);
4536 * ata_pci_init_native_mode - Initialize native-mode driver
4537 * @pdev: pci device to be initialized
4538 * @port: array[2] of pointers to port info structures.
4539 * @ports: bitmap of ports present
4541 * Utility function which allocates and initializes an
4542 * ata_probe_ent structure for a standard dual-port
4543 * PIO-based IDE controller. The returned ata_probe_ent
4544 * structure can be passed to ata_device_add(). The returned
4545 * ata_probe_ent structure should then be freed with kfree().
4547 * The caller need only pass the address of the primary port, the
4548 * secondary will be deduced automatically. If the device has non
4549 * standard secondary port mappings this function can be called twice,
4550 * once for each interface.
4553 struct ata_probe_ent *
4554 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4556 struct ata_probe_ent *probe_ent =
4557 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4558 int p = 0;
4560 if (!probe_ent)
4561 return NULL;
4563 probe_ent->irq = pdev->irq;
4564 probe_ent->irq_flags = SA_SHIRQ;
4565 probe_ent->private_data = port[0]->private_data;
4567 if (ports & ATA_PORT_PRIMARY) {
4568 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4569 probe_ent->port[p].altstatus_addr =
4570 probe_ent->port[p].ctl_addr =
4571 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4572 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4573 ata_std_ports(&probe_ent->port[p]);
4574 p++;
4577 if (ports & ATA_PORT_SECONDARY) {
4578 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4579 probe_ent->port[p].altstatus_addr =
4580 probe_ent->port[p].ctl_addr =
4581 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4582 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4583 ata_std_ports(&probe_ent->port[p]);
4584 p++;
4587 probe_ent->n_ports = p;
4588 return probe_ent;
4591 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
4593 struct ata_probe_ent *probe_ent;
4595 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
4596 if (!probe_ent)
4597 return NULL;
4599 probe_ent->legacy_mode = 1;
4600 probe_ent->n_ports = 1;
4601 probe_ent->hard_port_no = port_num;
4602 probe_ent->private_data = port->private_data;
4604 switch(port_num)
4606 case 0:
4607 probe_ent->irq = 14;
4608 probe_ent->port[0].cmd_addr = 0x1f0;
4609 probe_ent->port[0].altstatus_addr =
4610 probe_ent->port[0].ctl_addr = 0x3f6;
4611 break;
4612 case 1:
4613 probe_ent->irq = 15;
4614 probe_ent->port[0].cmd_addr = 0x170;
4615 probe_ent->port[0].altstatus_addr =
4616 probe_ent->port[0].ctl_addr = 0x376;
4617 break;
4619 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4620 ata_std_ports(&probe_ent->port[0]);
4621 return probe_ent;
4625 * ata_pci_init_one - Initialize/register PCI IDE host controller
4626 * @pdev: Controller to be initialized
4627 * @port_info: Information from low-level host driver
4628 * @n_ports: Number of ports attached to host controller
4630 * This is a helper function which can be called from a driver's
4631 * xxx_init_one() probe function if the hardware uses traditional
4632 * IDE taskfile registers.
4634 * This function calls pci_enable_device(), reserves its register
4635 * regions, sets the dma mask, enables bus master mode, and calls
4636 * ata_device_add()
4638 * LOCKING:
4639 * Inherited from PCI layer (may sleep).
4641 * RETURNS:
4642 * Zero on success, negative on errno-based value on error.
4645 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4646 unsigned int n_ports)
4648 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4649 struct ata_port_info *port[2];
4650 u8 tmp8, mask;
4651 unsigned int legacy_mode = 0;
4652 int disable_dev_on_err = 1;
4653 int rc;
4655 DPRINTK("ENTER\n");
4657 port[0] = port_info[0];
4658 if (n_ports > 1)
4659 port[1] = port_info[1];
4660 else
4661 port[1] = port[0];
4663 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4664 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4665 /* TODO: What if one channel is in native mode ... */
4666 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4667 mask = (1 << 2) | (1 << 0);
4668 if ((tmp8 & mask) != mask)
4669 legacy_mode = (1 << 3);
4672 /* FIXME... */
4673 if ((!legacy_mode) && (n_ports > 2)) {
4674 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4675 n_ports = 2;
4676 /* For now */
4679 /* FIXME: Really for ATA it isn't safe because the device may be
4680 multi-purpose and we want to leave it alone if it was already
4681 enabled. Secondly for shared use as Arjan says we want refcounting
4683 Checking dev->is_enabled is insufficient as this is not set at
4684 boot for the primary video which is BIOS enabled
4687 rc = pci_enable_device(pdev);
4688 if (rc)
4689 return rc;
4691 rc = pci_request_regions(pdev, DRV_NAME);
4692 if (rc) {
4693 disable_dev_on_err = 0;
4694 goto err_out;
4697 /* FIXME: Should use platform specific mappers for legacy port ranges */
4698 if (legacy_mode) {
4699 if (!request_region(0x1f0, 8, "libata")) {
4700 struct resource *conflict, res;
4701 res.start = 0x1f0;
4702 res.end = 0x1f0 + 8 - 1;
4703 conflict = ____request_resource(&ioport_resource, &res);
4704 if (!strcmp(conflict->name, "libata"))
4705 legacy_mode |= (1 << 0);
4706 else {
4707 disable_dev_on_err = 0;
4708 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4710 } else
4711 legacy_mode |= (1 << 0);
4713 if (!request_region(0x170, 8, "libata")) {
4714 struct resource *conflict, res;
4715 res.start = 0x170;
4716 res.end = 0x170 + 8 - 1;
4717 conflict = ____request_resource(&ioport_resource, &res);
4718 if (!strcmp(conflict->name, "libata"))
4719 legacy_mode |= (1 << 1);
4720 else {
4721 disable_dev_on_err = 0;
4722 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4724 } else
4725 legacy_mode |= (1 << 1);
4728 /* we have legacy mode, but all ports are unavailable */
4729 if (legacy_mode == (1 << 3)) {
4730 rc = -EBUSY;
4731 goto err_out_regions;
4734 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4735 if (rc)
4736 goto err_out_regions;
4737 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4738 if (rc)
4739 goto err_out_regions;
4741 if (legacy_mode) {
4742 if (legacy_mode & (1 << 0))
4743 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
4744 if (legacy_mode & (1 << 1))
4745 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
4746 } else {
4747 if (n_ports == 2)
4748 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4749 else
4750 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4752 if (!probe_ent && !probe_ent2) {
4753 rc = -ENOMEM;
4754 goto err_out_regions;
4757 pci_set_master(pdev);
4759 /* FIXME: check ata_device_add return */
4760 if (legacy_mode) {
4761 if (legacy_mode & (1 << 0))
4762 ata_device_add(probe_ent);
4763 if (legacy_mode & (1 << 1))
4764 ata_device_add(probe_ent2);
4765 } else
4766 ata_device_add(probe_ent);
4768 kfree(probe_ent);
4769 kfree(probe_ent2);
4771 return 0;
4773 err_out_regions:
4774 if (legacy_mode & (1 << 0))
4775 release_region(0x1f0, 8);
4776 if (legacy_mode & (1 << 1))
4777 release_region(0x170, 8);
4778 pci_release_regions(pdev);
4779 err_out:
4780 if (disable_dev_on_err)
4781 pci_disable_device(pdev);
4782 return rc;
4786 * ata_pci_remove_one - PCI layer callback for device removal
4787 * @pdev: PCI device that was removed
4789 * PCI layer indicates to libata via this hook that
4790 * hot-unplug or module unload event has occurred.
4791 * Handle this by unregistering all objects associated
4792 * with this PCI device. Free those objects. Then finally
4793 * release PCI resources and disable device.
4795 * LOCKING:
4796 * Inherited from PCI layer (may sleep).
4799 void ata_pci_remove_one (struct pci_dev *pdev)
4801 struct device *dev = pci_dev_to_dev(pdev);
4802 struct ata_host_set *host_set = dev_get_drvdata(dev);
4804 ata_host_set_remove(host_set);
4805 pci_release_regions(pdev);
4806 pci_disable_device(pdev);
4807 dev_set_drvdata(dev, NULL);
4810 /* move to PCI subsystem */
4811 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
4813 unsigned long tmp = 0;
4815 switch (bits->width) {
4816 case 1: {
4817 u8 tmp8 = 0;
4818 pci_read_config_byte(pdev, bits->reg, &tmp8);
4819 tmp = tmp8;
4820 break;
4822 case 2: {
4823 u16 tmp16 = 0;
4824 pci_read_config_word(pdev, bits->reg, &tmp16);
4825 tmp = tmp16;
4826 break;
4828 case 4: {
4829 u32 tmp32 = 0;
4830 pci_read_config_dword(pdev, bits->reg, &tmp32);
4831 tmp = tmp32;
4832 break;
4835 default:
4836 return -EINVAL;
4839 tmp &= bits->mask;
4841 return (tmp == bits->val) ? 1 : 0;
4843 #endif /* CONFIG_PCI */
4846 static int __init ata_init(void)
4848 ata_wq = create_workqueue("ata");
4849 if (!ata_wq)
4850 return -ENOMEM;
4852 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4853 return 0;
4856 static void __exit ata_exit(void)
4858 destroy_workqueue(ata_wq);
4861 module_init(ata_init);
4862 module_exit(ata_exit);
4864 static unsigned long ratelimit_time;
4865 static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4867 int ata_ratelimit(void)
4869 int rc;
4870 unsigned long flags;
4872 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4874 if (time_after(jiffies, ratelimit_time)) {
4875 rc = 1;
4876 ratelimit_time = jiffies + (HZ/5);
4877 } else
4878 rc = 0;
4880 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4882 return rc;
4886 * libata is essentially a library of internal helper functions for
4887 * low-level ATA host controller drivers. As such, the API/ABI is
4888 * likely to change as new drivers are added and updated.
4889 * Do not depend on ABI/API stability.
4892 EXPORT_SYMBOL_GPL(ata_std_bios_param);
4893 EXPORT_SYMBOL_GPL(ata_std_ports);
4894 EXPORT_SYMBOL_GPL(ata_device_add);
4895 EXPORT_SYMBOL_GPL(ata_host_set_remove);
4896 EXPORT_SYMBOL_GPL(ata_sg_init);
4897 EXPORT_SYMBOL_GPL(ata_sg_init_one);
4898 EXPORT_SYMBOL_GPL(ata_qc_complete);
4899 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4900 EXPORT_SYMBOL_GPL(ata_eng_timeout);
4901 EXPORT_SYMBOL_GPL(ata_tf_load);
4902 EXPORT_SYMBOL_GPL(ata_tf_read);
4903 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4904 EXPORT_SYMBOL_GPL(ata_std_dev_select);
4905 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4906 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4907 EXPORT_SYMBOL_GPL(ata_check_status);
4908 EXPORT_SYMBOL_GPL(ata_altstatus);
4909 EXPORT_SYMBOL_GPL(ata_exec_command);
4910 EXPORT_SYMBOL_GPL(ata_port_start);
4911 EXPORT_SYMBOL_GPL(ata_port_stop);
4912 EXPORT_SYMBOL_GPL(ata_host_stop);
4913 EXPORT_SYMBOL_GPL(ata_interrupt);
4914 EXPORT_SYMBOL_GPL(ata_qc_prep);
4915 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4916 EXPORT_SYMBOL_GPL(ata_bmdma_start);
4917 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4918 EXPORT_SYMBOL_GPL(ata_bmdma_status);
4919 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4920 EXPORT_SYMBOL_GPL(ata_port_probe);
4921 EXPORT_SYMBOL_GPL(sata_phy_reset);
4922 EXPORT_SYMBOL_GPL(__sata_phy_reset);
4923 EXPORT_SYMBOL_GPL(ata_bus_reset);
4924 EXPORT_SYMBOL_GPL(ata_port_disable);
4925 EXPORT_SYMBOL_GPL(ata_ratelimit);
4926 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4927 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4928 EXPORT_SYMBOL_GPL(ata_scsi_error);
4929 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4930 EXPORT_SYMBOL_GPL(ata_scsi_release);
4931 EXPORT_SYMBOL_GPL(ata_host_intr);
4932 EXPORT_SYMBOL_GPL(ata_dev_classify);
4933 EXPORT_SYMBOL_GPL(ata_dev_id_string);
4934 EXPORT_SYMBOL_GPL(ata_dev_config);
4935 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4937 EXPORT_SYMBOL_GPL(ata_timing_compute);
4938 EXPORT_SYMBOL_GPL(ata_timing_merge);
4940 #ifdef CONFIG_PCI
4941 EXPORT_SYMBOL_GPL(pci_test_config_bits);
4942 EXPORT_SYMBOL_GPL(ata_pci_host_stop);
4943 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4944 EXPORT_SYMBOL_GPL(ata_pci_init_one);
4945 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4946 #endif /* CONFIG_PCI */