2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <linux/jiffies.h>
52 #include <linux/scatterlist.h>
53 #include <scsi/scsi.h>
54 #include "scsi_priv.h"
55 #include <scsi/scsi_cmnd.h>
56 #include <scsi/scsi_host.h>
57 #include <linux/libata.h>
59 #include <asm/semaphore.h>
60 #include <asm/byteorder.h>
64 static void ata_dev_reread_id(struct ata_port
*ap
, struct ata_device
*dev
);
65 static void ata_dev_init_params(struct ata_port
*ap
, struct ata_device
*dev
);
66 static void ata_set_mode(struct ata_port
*ap
);
67 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
);
68 static unsigned int ata_get_mode_mask(const struct ata_port
*ap
, int shift
);
69 static int fgb(u32 bitmap
);
70 static int ata_choose_xfer_mode(const struct ata_port
*ap
,
72 unsigned int *xfer_shift_out
);
74 static unsigned int ata_unique_id
= 1;
75 static struct workqueue_struct
*ata_wq
;
77 int atapi_enabled
= 0;
78 module_param(atapi_enabled
, int, 0444);
79 MODULE_PARM_DESC(atapi_enabled
, "Enable discovery of ATAPI devices (0=off, 1=on)");
81 MODULE_AUTHOR("Jeff Garzik");
82 MODULE_DESCRIPTION("Library module for ATA devices");
83 MODULE_LICENSE("GPL");
84 MODULE_VERSION(DRV_VERSION
);
87 * ata_tf_load_pio - send taskfile registers to host controller
88 * @ap: Port to which output is sent
89 * @tf: ATA taskfile register set
91 * Outputs ATA taskfile to standard ATA host controller.
94 * Inherited from caller.
97 static void ata_tf_load_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
99 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
100 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
102 if (tf
->ctl
!= ap
->last_ctl
) {
103 outb(tf
->ctl
, ioaddr
->ctl_addr
);
104 ap
->last_ctl
= tf
->ctl
;
108 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
109 outb(tf
->hob_feature
, ioaddr
->feature_addr
);
110 outb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
111 outb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
112 outb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
113 outb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
114 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
123 outb(tf
->feature
, ioaddr
->feature_addr
);
124 outb(tf
->nsect
, ioaddr
->nsect_addr
);
125 outb(tf
->lbal
, ioaddr
->lbal_addr
);
126 outb(tf
->lbam
, ioaddr
->lbam_addr
);
127 outb(tf
->lbah
, ioaddr
->lbah_addr
);
128 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
136 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
137 outb(tf
->device
, ioaddr
->device_addr
);
138 VPRINTK("device 0x%X\n", tf
->device
);
145 * ata_tf_load_mmio - send taskfile registers to host controller
146 * @ap: Port to which output is sent
147 * @tf: ATA taskfile register set
149 * Outputs ATA taskfile to standard ATA host controller using MMIO.
152 * Inherited from caller.
155 static void ata_tf_load_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
157 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
158 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
160 if (tf
->ctl
!= ap
->last_ctl
) {
161 writeb(tf
->ctl
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
162 ap
->last_ctl
= tf
->ctl
;
166 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
167 writeb(tf
->hob_feature
, (void __iomem
*) ioaddr
->feature_addr
);
168 writeb(tf
->hob_nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
169 writeb(tf
->hob_lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
170 writeb(tf
->hob_lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
171 writeb(tf
->hob_lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
172 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
181 writeb(tf
->feature
, (void __iomem
*) ioaddr
->feature_addr
);
182 writeb(tf
->nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
183 writeb(tf
->lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
184 writeb(tf
->lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
185 writeb(tf
->lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
186 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
194 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
195 writeb(tf
->device
, (void __iomem
*) ioaddr
->device_addr
);
196 VPRINTK("device 0x%X\n", tf
->device
);
204 * ata_tf_load - send taskfile registers to host controller
205 * @ap: Port to which output is sent
206 * @tf: ATA taskfile register set
208 * Outputs ATA taskfile to standard ATA host controller using MMIO
209 * or PIO as indicated by the ATA_FLAG_MMIO flag.
210 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
211 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
212 * hob_lbal, hob_lbam, and hob_lbah.
214 * This function waits for idle (!BUSY and !DRQ) after writing
215 * registers. If the control register has a new value, this
216 * function also waits for idle after writing control and before
217 * writing the remaining registers.
219 * May be used as the tf_load() entry in ata_port_operations.
222 * Inherited from caller.
224 void ata_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
226 if (ap
->flags
& ATA_FLAG_MMIO
)
227 ata_tf_load_mmio(ap
, tf
);
229 ata_tf_load_pio(ap
, tf
);
233 * ata_exec_command_pio - issue ATA command to host controller
234 * @ap: port to which command is being issued
235 * @tf: ATA taskfile register set
237 * Issues PIO write to ATA command register, with proper
238 * synchronization with interrupt handler / other threads.
241 * spin_lock_irqsave(host_set lock)
244 static void ata_exec_command_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
246 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
248 outb(tf
->command
, ap
->ioaddr
.command_addr
);
254 * ata_exec_command_mmio - issue ATA command to host controller
255 * @ap: port to which command is being issued
256 * @tf: ATA taskfile register set
258 * Issues MMIO write to ATA command register, with proper
259 * synchronization with interrupt handler / other threads.
262 * spin_lock_irqsave(host_set lock)
265 static void ata_exec_command_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
267 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
269 writeb(tf
->command
, (void __iomem
*) ap
->ioaddr
.command_addr
);
275 * ata_exec_command - issue ATA command to host controller
276 * @ap: port to which command is being issued
277 * @tf: ATA taskfile register set
279 * Issues PIO/MMIO write to ATA command register, with proper
280 * synchronization with interrupt handler / other threads.
283 * spin_lock_irqsave(host_set lock)
285 void ata_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
287 if (ap
->flags
& ATA_FLAG_MMIO
)
288 ata_exec_command_mmio(ap
, tf
);
290 ata_exec_command_pio(ap
, tf
);
294 * ata_tf_to_host - issue ATA taskfile to host controller
295 * @ap: port to which command is being issued
296 * @tf: ATA taskfile register set
298 * Issues ATA taskfile register set to ATA host controller,
299 * with proper synchronization with interrupt handler and
303 * spin_lock_irqsave(host_set lock)
306 static inline void ata_tf_to_host(struct ata_port
*ap
,
307 const struct ata_taskfile
*tf
)
309 ap
->ops
->tf_load(ap
, tf
);
310 ap
->ops
->exec_command(ap
, tf
);
314 * ata_tf_read_pio - input device's ATA taskfile shadow registers
315 * @ap: Port from which input is read
316 * @tf: ATA taskfile register set for storing input
318 * Reads ATA taskfile registers for currently-selected device
322 * Inherited from caller.
325 static void ata_tf_read_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
327 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
329 tf
->command
= ata_check_status(ap
);
330 tf
->feature
= inb(ioaddr
->error_addr
);
331 tf
->nsect
= inb(ioaddr
->nsect_addr
);
332 tf
->lbal
= inb(ioaddr
->lbal_addr
);
333 tf
->lbam
= inb(ioaddr
->lbam_addr
);
334 tf
->lbah
= inb(ioaddr
->lbah_addr
);
335 tf
->device
= inb(ioaddr
->device_addr
);
337 if (tf
->flags
& ATA_TFLAG_LBA48
) {
338 outb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
339 tf
->hob_feature
= inb(ioaddr
->error_addr
);
340 tf
->hob_nsect
= inb(ioaddr
->nsect_addr
);
341 tf
->hob_lbal
= inb(ioaddr
->lbal_addr
);
342 tf
->hob_lbam
= inb(ioaddr
->lbam_addr
);
343 tf
->hob_lbah
= inb(ioaddr
->lbah_addr
);
348 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
349 * @ap: Port from which input is read
350 * @tf: ATA taskfile register set for storing input
352 * Reads ATA taskfile registers for currently-selected device
356 * Inherited from caller.
359 static void ata_tf_read_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
361 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
363 tf
->command
= ata_check_status(ap
);
364 tf
->feature
= readb((void __iomem
*)ioaddr
->error_addr
);
365 tf
->nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
366 tf
->lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
367 tf
->lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
368 tf
->lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
369 tf
->device
= readb((void __iomem
*)ioaddr
->device_addr
);
371 if (tf
->flags
& ATA_TFLAG_LBA48
) {
372 writeb(tf
->ctl
| ATA_HOB
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
373 tf
->hob_feature
= readb((void __iomem
*)ioaddr
->error_addr
);
374 tf
->hob_nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
375 tf
->hob_lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
376 tf
->hob_lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
377 tf
->hob_lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
383 * ata_tf_read - input device's ATA taskfile shadow registers
384 * @ap: Port from which input is read
385 * @tf: ATA taskfile register set for storing input
387 * Reads ATA taskfile registers for currently-selected device
390 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
391 * is set, also reads the hob registers.
393 * May be used as the tf_read() entry in ata_port_operations.
396 * Inherited from caller.
398 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
400 if (ap
->flags
& ATA_FLAG_MMIO
)
401 ata_tf_read_mmio(ap
, tf
);
403 ata_tf_read_pio(ap
, tf
);
407 * ata_check_status_pio - Read device status reg & clear interrupt
408 * @ap: port where the device is
410 * Reads ATA taskfile status register for currently-selected device
411 * and return its value. This also clears pending interrupts
415 * Inherited from caller.
417 static u8
ata_check_status_pio(struct ata_port
*ap
)
419 return inb(ap
->ioaddr
.status_addr
);
423 * ata_check_status_mmio - Read device status reg & clear interrupt
424 * @ap: port where the device is
426 * Reads ATA taskfile status register for currently-selected device
427 * via MMIO and return its value. This also clears pending interrupts
431 * Inherited from caller.
433 static u8
ata_check_status_mmio(struct ata_port
*ap
)
435 return readb((void __iomem
*) ap
->ioaddr
.status_addr
);
440 * ata_check_status - Read device status reg & clear interrupt
441 * @ap: port where the device is
443 * Reads ATA taskfile status register for currently-selected device
444 * and return its value. This also clears pending interrupts
447 * May be used as the check_status() entry in ata_port_operations.
450 * Inherited from caller.
452 u8
ata_check_status(struct ata_port
*ap
)
454 if (ap
->flags
& ATA_FLAG_MMIO
)
455 return ata_check_status_mmio(ap
);
456 return ata_check_status_pio(ap
);
461 * ata_altstatus - Read device alternate status reg
462 * @ap: port where the device is
464 * Reads ATA taskfile alternate status register for
465 * currently-selected device and return its value.
467 * Note: may NOT be used as the check_altstatus() entry in
468 * ata_port_operations.
471 * Inherited from caller.
473 u8
ata_altstatus(struct ata_port
*ap
)
475 if (ap
->ops
->check_altstatus
)
476 return ap
->ops
->check_altstatus(ap
);
478 if (ap
->flags
& ATA_FLAG_MMIO
)
479 return readb((void __iomem
*)ap
->ioaddr
.altstatus_addr
);
480 return inb(ap
->ioaddr
.altstatus_addr
);
485 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
486 * @tf: Taskfile to convert
487 * @fis: Buffer into which data will output
488 * @pmp: Port multiplier port
490 * Converts a standard ATA taskfile to a Serial ATA
491 * FIS structure (Register - Host to Device).
494 * Inherited from caller.
497 void ata_tf_to_fis(const struct ata_taskfile
*tf
, u8
*fis
, u8 pmp
)
499 fis
[0] = 0x27; /* Register - Host to Device FIS */
500 fis
[1] = (pmp
& 0xf) | (1 << 7); /* Port multiplier number,
501 bit 7 indicates Command FIS */
502 fis
[2] = tf
->command
;
503 fis
[3] = tf
->feature
;
510 fis
[8] = tf
->hob_lbal
;
511 fis
[9] = tf
->hob_lbam
;
512 fis
[10] = tf
->hob_lbah
;
513 fis
[11] = tf
->hob_feature
;
516 fis
[13] = tf
->hob_nsect
;
527 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
528 * @fis: Buffer from which data will be input
529 * @tf: Taskfile to output
531 * Converts a serial ATA FIS structure to a standard ATA taskfile.
534 * Inherited from caller.
537 void ata_tf_from_fis(const u8
*fis
, struct ata_taskfile
*tf
)
539 tf
->command
= fis
[2]; /* status */
540 tf
->feature
= fis
[3]; /* error */
547 tf
->hob_lbal
= fis
[8];
548 tf
->hob_lbam
= fis
[9];
549 tf
->hob_lbah
= fis
[10];
552 tf
->hob_nsect
= fis
[13];
555 static const u8 ata_rw_cmds
[] = {
559 ATA_CMD_READ_MULTI_EXT
,
560 ATA_CMD_WRITE_MULTI_EXT
,
564 ATA_CMD_WRITE_MULTI_FUA_EXT
,
568 ATA_CMD_PIO_READ_EXT
,
569 ATA_CMD_PIO_WRITE_EXT
,
582 ATA_CMD_WRITE_FUA_EXT
586 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
587 * @qc: command to examine and configure
589 * Examine the device configuration and tf->flags to calculate
590 * the proper read/write commands and protocol to use.
595 int ata_rwcmd_protocol(struct ata_queued_cmd
*qc
)
597 struct ata_taskfile
*tf
= &qc
->tf
;
598 struct ata_device
*dev
= qc
->dev
;
601 int index
, fua
, lba48
, write
;
603 fua
= (tf
->flags
& ATA_TFLAG_FUA
) ? 4 : 0;
604 lba48
= (tf
->flags
& ATA_TFLAG_LBA48
) ? 2 : 0;
605 write
= (tf
->flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
607 if (dev
->flags
& ATA_DFLAG_PIO
) {
608 tf
->protocol
= ATA_PROT_PIO
;
609 index
= dev
->multi_count
? 0 : 8;
610 } else if (lba48
&& (qc
->ap
->flags
& ATA_FLAG_PIO_LBA48
)) {
611 /* Unable to use DMA due to host limitation */
612 tf
->protocol
= ATA_PROT_PIO
;
613 index
= dev
->multi_count
? 0 : 4;
615 tf
->protocol
= ATA_PROT_DMA
;
619 cmd
= ata_rw_cmds
[index
+ fua
+ lba48
+ write
];
627 static const char * const xfer_mode_str
[] = {
647 * ata_udma_string - convert UDMA bit offset to string
648 * @mask: mask of bits supported; only highest bit counts.
650 * Determine string which represents the highest speed
651 * (highest bit in @udma_mask).
657 * Constant C string representing highest speed listed in
658 * @udma_mask, or the constant C string "<n/a>".
661 static const char *ata_mode_string(unsigned int mask
)
665 for (i
= 7; i
>= 0; i
--)
668 for (i
= ATA_SHIFT_MWDMA
+ 2; i
>= ATA_SHIFT_MWDMA
; i
--)
671 for (i
= ATA_SHIFT_PIO
+ 4; i
>= ATA_SHIFT_PIO
; i
--)
678 return xfer_mode_str
[i
];
682 * ata_pio_devchk - PATA device presence detection
683 * @ap: ATA channel to examine
684 * @device: Device to examine (starting at zero)
686 * This technique was originally described in
687 * Hale Landis's ATADRVR (www.ata-atapi.com), and
688 * later found its way into the ATA/ATAPI spec.
690 * Write a pattern to the ATA shadow registers,
691 * and if a device is present, it will respond by
692 * correctly storing and echoing back the
693 * ATA shadow register contents.
699 static unsigned int ata_pio_devchk(struct ata_port
*ap
,
702 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
705 ap
->ops
->dev_select(ap
, device
);
707 outb(0x55, ioaddr
->nsect_addr
);
708 outb(0xaa, ioaddr
->lbal_addr
);
710 outb(0xaa, ioaddr
->nsect_addr
);
711 outb(0x55, ioaddr
->lbal_addr
);
713 outb(0x55, ioaddr
->nsect_addr
);
714 outb(0xaa, ioaddr
->lbal_addr
);
716 nsect
= inb(ioaddr
->nsect_addr
);
717 lbal
= inb(ioaddr
->lbal_addr
);
719 if ((nsect
== 0x55) && (lbal
== 0xaa))
720 return 1; /* we found a device */
722 return 0; /* nothing found */
726 * ata_mmio_devchk - PATA device presence detection
727 * @ap: ATA channel to examine
728 * @device: Device to examine (starting at zero)
730 * This technique was originally described in
731 * Hale Landis's ATADRVR (www.ata-atapi.com), and
732 * later found its way into the ATA/ATAPI spec.
734 * Write a pattern to the ATA shadow registers,
735 * and if a device is present, it will respond by
736 * correctly storing and echoing back the
737 * ATA shadow register contents.
743 static unsigned int ata_mmio_devchk(struct ata_port
*ap
,
746 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
749 ap
->ops
->dev_select(ap
, device
);
751 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
752 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
754 writeb(0xaa, (void __iomem
*) ioaddr
->nsect_addr
);
755 writeb(0x55, (void __iomem
*) ioaddr
->lbal_addr
);
757 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
758 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
760 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
761 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
763 if ((nsect
== 0x55) && (lbal
== 0xaa))
764 return 1; /* we found a device */
766 return 0; /* nothing found */
770 * ata_devchk - PATA device presence detection
771 * @ap: ATA channel to examine
772 * @device: Device to examine (starting at zero)
774 * Dispatch ATA device presence detection, depending
775 * on whether we are using PIO or MMIO to talk to the
776 * ATA shadow registers.
782 static unsigned int ata_devchk(struct ata_port
*ap
,
785 if (ap
->flags
& ATA_FLAG_MMIO
)
786 return ata_mmio_devchk(ap
, device
);
787 return ata_pio_devchk(ap
, device
);
791 * ata_dev_classify - determine device type based on ATA-spec signature
792 * @tf: ATA taskfile register set for device to be identified
794 * Determine from taskfile register contents whether a device is
795 * ATA or ATAPI, as per "Signature and persistence" section
796 * of ATA/PI spec (volume 1, sect 5.14).
802 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
803 * the event of failure.
806 unsigned int ata_dev_classify(const struct ata_taskfile
*tf
)
808 /* Apple's open source Darwin code hints that some devices only
809 * put a proper signature into the LBA mid/high registers,
810 * So, we only check those. It's sufficient for uniqueness.
813 if (((tf
->lbam
== 0) && (tf
->lbah
== 0)) ||
814 ((tf
->lbam
== 0x3c) && (tf
->lbah
== 0xc3))) {
815 DPRINTK("found ATA device by sig\n");
819 if (((tf
->lbam
== 0x14) && (tf
->lbah
== 0xeb)) ||
820 ((tf
->lbam
== 0x69) && (tf
->lbah
== 0x96))) {
821 DPRINTK("found ATAPI device by sig\n");
822 return ATA_DEV_ATAPI
;
825 DPRINTK("unknown device\n");
826 return ATA_DEV_UNKNOWN
;
830 * ata_dev_try_classify - Parse returned ATA device signature
831 * @ap: ATA channel to examine
832 * @device: Device to examine (starting at zero)
833 * @r_err: Value of error register on completion
835 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
836 * an ATA/ATAPI-defined set of values is placed in the ATA
837 * shadow registers, indicating the results of device detection
840 * Select the ATA device, and read the values from the ATA shadow
841 * registers. Then parse according to the Error register value,
842 * and the spec-defined values examined by ata_dev_classify().
848 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
852 ata_dev_try_classify(struct ata_port
*ap
, unsigned int device
, u8
*r_err
)
854 struct ata_taskfile tf
;
858 ap
->ops
->dev_select(ap
, device
);
860 memset(&tf
, 0, sizeof(tf
));
862 ap
->ops
->tf_read(ap
, &tf
);
867 /* see if device passed diags */
870 else if ((device
== 0) && (err
== 0x81))
875 /* determine if device is ATA or ATAPI */
876 class = ata_dev_classify(&tf
);
878 if (class == ATA_DEV_UNKNOWN
)
880 if ((class == ATA_DEV_ATA
) && (ata_chk_status(ap
) == 0))
886 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
887 * @id: IDENTIFY DEVICE results we will examine
888 * @s: string into which data is output
889 * @ofs: offset into identify device page
890 * @len: length of string to return. must be an even number.
892 * The strings in the IDENTIFY DEVICE page are broken up into
893 * 16-bit chunks. Run through the string, and output each
894 * 8-bit chunk linearly, regardless of platform.
900 void ata_dev_id_string(const u16
*id
, unsigned char *s
,
901 unsigned int ofs
, unsigned int len
)
921 * ata_noop_dev_select - Select device 0/1 on ATA bus
922 * @ap: ATA channel to manipulate
923 * @device: ATA device (numbered from zero) to select
925 * This function performs no actual function.
927 * May be used as the dev_select() entry in ata_port_operations.
932 void ata_noop_dev_select (struct ata_port
*ap
, unsigned int device
)
938 * ata_std_dev_select - Select device 0/1 on ATA bus
939 * @ap: ATA channel to manipulate
940 * @device: ATA device (numbered from zero) to select
942 * Use the method defined in the ATA specification to
943 * make either device 0, or device 1, active on the
944 * ATA channel. Works with both PIO and MMIO.
946 * May be used as the dev_select() entry in ata_port_operations.
952 void ata_std_dev_select (struct ata_port
*ap
, unsigned int device
)
957 tmp
= ATA_DEVICE_OBS
;
959 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
961 if (ap
->flags
& ATA_FLAG_MMIO
) {
962 writeb(tmp
, (void __iomem
*) ap
->ioaddr
.device_addr
);
964 outb(tmp
, ap
->ioaddr
.device_addr
);
966 ata_pause(ap
); /* needed; also flushes, for mmio */
970 * ata_dev_select - Select device 0/1 on ATA bus
971 * @ap: ATA channel to manipulate
972 * @device: ATA device (numbered from zero) to select
973 * @wait: non-zero to wait for Status register BSY bit to clear
974 * @can_sleep: non-zero if context allows sleeping
976 * Use the method defined in the ATA specification to
977 * make either device 0, or device 1, active on the
980 * This is a high-level version of ata_std_dev_select(),
981 * which additionally provides the services of inserting
982 * the proper pauses and status polling, where needed.
988 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
989 unsigned int wait
, unsigned int can_sleep
)
991 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
992 ap
->id
, device
, wait
);
997 ap
->ops
->dev_select(ap
, device
);
1000 if (can_sleep
&& ap
->device
[device
].class == ATA_DEV_ATAPI
)
1007 * ata_dump_id - IDENTIFY DEVICE info debugging output
1008 * @dev: Device whose IDENTIFY DEVICE page we will dump
1010 * Dump selected 16-bit words from a detected device's
1011 * IDENTIFY PAGE page.
1017 static inline void ata_dump_id(const struct ata_device
*dev
)
1019 DPRINTK("49==0x%04x "
1029 DPRINTK("80==0x%04x "
1039 DPRINTK("88==0x%04x "
1046 * Compute the PIO modes available for this device. This is not as
1047 * trivial as it seems if we must consider early devices correctly.
1049 * FIXME: pre IDE drive timing (do we care ?).
1052 static unsigned int ata_pio_modes(const struct ata_device
*adev
)
1056 /* Usual case. Word 53 indicates word 64 is valid */
1057 if (adev
->id
[ATA_ID_FIELD_VALID
] & (1 << 1)) {
1058 modes
= adev
->id
[ATA_ID_PIO_MODES
] & 0x03;
1064 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
1065 number for the maximum. Turn it into a mask and return it */
1066 modes
= (2 << ((adev
->id
[ATA_ID_OLD_PIO_MODES
] >> 8) & 0xFF)) - 1 ;
1068 /* But wait.. there's more. Design your standards by committee and
1069 you too can get a free iordy field to process. However its the
1070 speeds not the modes that are supported... Note drivers using the
1071 timing API will get this right anyway */
1075 ata_queue_packet_task(struct ata_port
*ap
)
1077 queue_work(ata_wq
, &ap
->packet_task
);
1081 ata_queue_pio_task(struct ata_port
*ap
)
1083 queue_work(ata_wq
, &ap
->pio_task
);
1087 ata_queue_delayed_pio_task(struct ata_port
*ap
, unsigned long delay
)
1089 queue_delayed_work(ata_wq
, &ap
->pio_task
, delay
);
1092 void ata_qc_complete_internal(struct ata_queued_cmd
*qc
)
1094 struct completion
*waiting
= qc
->private_data
;
1096 qc
->ap
->ops
->tf_read(qc
->ap
, &qc
->tf
);
1101 * ata_exec_internal - execute libata internal command
1102 * @ap: Port to which the command is sent
1103 * @dev: Device to which the command is sent
1104 * @tf: Taskfile registers for the command and the result
1105 * @dma_dir: Data tranfer direction of the command
1106 * @buf: Data buffer of the command
1107 * @buflen: Length of data buffer
1109 * Executes libata internal command with timeout. @tf contains
1110 * command on entry and result on return. Timeout and error
1111 * conditions are reported via return value. No recovery action
1112 * is taken after a command times out. It's caller's duty to
1113 * clean up after timeout.
1116 * None. Should be called with kernel context, might sleep.
1120 ata_exec_internal(struct ata_port
*ap
, struct ata_device
*dev
,
1121 struct ata_taskfile
*tf
,
1122 int dma_dir
, void *buf
, unsigned int buflen
)
1124 u8 command
= tf
->command
;
1125 struct ata_queued_cmd
*qc
;
1126 DECLARE_COMPLETION(wait
);
1127 unsigned long flags
;
1128 unsigned int err_mask
;
1130 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1132 qc
= ata_qc_new_init(ap
, dev
);
1136 qc
->dma_dir
= dma_dir
;
1137 if (dma_dir
!= DMA_NONE
) {
1138 ata_sg_init_one(qc
, buf
, buflen
);
1139 qc
->nsect
= buflen
/ ATA_SECT_SIZE
;
1142 qc
->private_data
= &wait
;
1143 qc
->complete_fn
= ata_qc_complete_internal
;
1145 qc
->err_mask
= ata_qc_issue(qc
);
1147 ata_qc_complete(qc
);
1149 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1151 if (!wait_for_completion_timeout(&wait
, ATA_TMOUT_INTERNAL
)) {
1152 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1154 /* We're racing with irq here. If we lose, the
1155 * following test prevents us from completing the qc
1156 * again. If completion irq occurs after here but
1157 * before the caller cleans up, it will result in a
1158 * spurious interrupt. We can live with that.
1160 if (qc
->flags
& ATA_QCFLAG_ACTIVE
) {
1161 qc
->err_mask
= AC_ERR_TIMEOUT
;
1162 ata_qc_complete(qc
);
1163 printk(KERN_WARNING
"ata%u: qc timeout (cmd 0x%x)\n",
1167 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1171 err_mask
= qc
->err_mask
;
1179 * ata_pio_need_iordy - check if iordy needed
1182 * Check if the current speed of the device requires IORDY. Used
1183 * by various controllers for chip configuration.
1186 unsigned int ata_pio_need_iordy(const struct ata_device
*adev
)
1189 int speed
= adev
->pio_mode
- XFER_PIO_0
;
1196 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1198 if (adev
->id
[ATA_ID_FIELD_VALID
] & 2) { /* EIDE */
1199 pio
= adev
->id
[ATA_ID_EIDE_PIO
];
1200 /* Is the speed faster than the drive allows non IORDY ? */
1202 /* This is cycle times not frequency - watch the logic! */
1203 if (pio
> 240) /* PIO2 is 240nS per cycle */
1212 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1213 * @ap: port on which device we wish to probe resides
1214 * @device: device bus address, starting at zero
1216 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1217 * command, and read back the 512-byte device information page.
1218 * The device information page is fed to us via the standard
1219 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1220 * using standard PIO-IN paths)
1222 * After reading the device information page, we use several
1223 * bits of information from it to initialize data structures
1224 * that will be used during the lifetime of the ata_device.
1225 * Other data from the info page is used to disqualify certain
1226 * older ATA devices we do not wish to support.
1229 * Inherited from caller. Some functions called by this function
1230 * obtain the host_set lock.
1233 static void ata_dev_identify(struct ata_port
*ap
, unsigned int device
)
1235 struct ata_device
*dev
= &ap
->device
[device
];
1236 unsigned int major_version
;
1238 unsigned long xfer_modes
;
1239 unsigned int using_edd
;
1240 struct ata_taskfile tf
;
1241 unsigned int err_mask
;
1244 if (!ata_dev_present(dev
)) {
1245 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1250 if (ap
->flags
& (ATA_FLAG_SRST
| ATA_FLAG_SATA_RESET
))
1255 DPRINTK("ENTER, host %u, dev %u\n", ap
->id
, device
);
1257 assert (dev
->class == ATA_DEV_ATA
|| dev
->class == ATA_DEV_ATAPI
||
1258 dev
->class == ATA_DEV_NONE
);
1260 ata_dev_select(ap
, device
, 1, 1); /* select device 0/1 */
1263 ata_tf_init(ap
, &tf
, device
);
1265 if (dev
->class == ATA_DEV_ATA
) {
1266 tf
.command
= ATA_CMD_ID_ATA
;
1267 DPRINTK("do ATA identify\n");
1269 tf
.command
= ATA_CMD_ID_ATAPI
;
1270 DPRINTK("do ATAPI identify\n");
1273 tf
.protocol
= ATA_PROT_PIO
;
1275 err_mask
= ata_exec_internal(ap
, dev
, &tf
, DMA_FROM_DEVICE
,
1276 dev
->id
, sizeof(dev
->id
));
1279 if (err_mask
& ~AC_ERR_DEV
)
1283 * arg! EDD works for all test cases, but seems to return
1284 * the ATA signature for some ATAPI devices. Until the
1285 * reason for this is found and fixed, we fix up the mess
1286 * here. If IDENTIFY DEVICE returns command aborted
1287 * (as ATAPI devices do), then we issue an
1288 * IDENTIFY PACKET DEVICE.
1290 * ATA software reset (SRST, the default) does not appear
1291 * to have this problem.
1293 if ((using_edd
) && (dev
->class == ATA_DEV_ATA
)) {
1294 u8 err
= tf
.feature
;
1295 if (err
& ATA_ABORTED
) {
1296 dev
->class = ATA_DEV_ATAPI
;
1303 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
1305 /* print device capabilities */
1306 printk(KERN_DEBUG
"ata%u: dev %u cfg "
1307 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1308 ap
->id
, device
, dev
->id
[49],
1309 dev
->id
[82], dev
->id
[83], dev
->id
[84],
1310 dev
->id
[85], dev
->id
[86], dev
->id
[87],
1314 * common ATA, ATAPI feature tests
1317 /* we require DMA support (bits 8 of word 49) */
1318 if (!ata_id_has_dma(dev
->id
)) {
1319 printk(KERN_DEBUG
"ata%u: no dma\n", ap
->id
);
1323 /* quick-n-dirty find max transfer mode; for printk only */
1324 xfer_modes
= dev
->id
[ATA_ID_UDMA_MODES
];
1326 xfer_modes
= (dev
->id
[ATA_ID_MWDMA_MODES
]) << ATA_SHIFT_MWDMA
;
1328 xfer_modes
= ata_pio_modes(dev
);
1332 /* ATA-specific feature tests */
1333 if (dev
->class == ATA_DEV_ATA
) {
1334 if (!ata_id_is_ata(dev
->id
)) /* sanity check */
1337 /* get major version */
1338 tmp
= dev
->id
[ATA_ID_MAJOR_VER
];
1339 for (major_version
= 14; major_version
>= 1; major_version
--)
1340 if (tmp
& (1 << major_version
))
1344 * The exact sequence expected by certain pre-ATA4 drives is:
1347 * INITIALIZE DEVICE PARAMETERS
1349 * Some drives were very specific about that exact sequence.
1351 if (major_version
< 4 || (!ata_id_has_lba(dev
->id
))) {
1352 ata_dev_init_params(ap
, dev
);
1354 /* current CHS translation info (id[53-58]) might be
1355 * changed. reread the identify device info.
1357 ata_dev_reread_id(ap
, dev
);
1360 if (ata_id_has_lba(dev
->id
)) {
1361 dev
->flags
|= ATA_DFLAG_LBA
;
1363 if (ata_id_has_lba48(dev
->id
)) {
1364 dev
->flags
|= ATA_DFLAG_LBA48
;
1365 dev
->n_sectors
= ata_id_u64(dev
->id
, 100);
1367 dev
->n_sectors
= ata_id_u32(dev
->id
, 60);
1370 /* print device info to dmesg */
1371 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1374 ata_mode_string(xfer_modes
),
1375 (unsigned long long)dev
->n_sectors
,
1376 dev
->flags
& ATA_DFLAG_LBA48
? " LBA48" : " LBA");
1380 /* Default translation */
1381 dev
->cylinders
= dev
->id
[1];
1382 dev
->heads
= dev
->id
[3];
1383 dev
->sectors
= dev
->id
[6];
1384 dev
->n_sectors
= dev
->cylinders
* dev
->heads
* dev
->sectors
;
1386 if (ata_id_current_chs_valid(dev
->id
)) {
1387 /* Current CHS translation is valid. */
1388 dev
->cylinders
= dev
->id
[54];
1389 dev
->heads
= dev
->id
[55];
1390 dev
->sectors
= dev
->id
[56];
1392 dev
->n_sectors
= ata_id_u32(dev
->id
, 57);
1395 /* print device info to dmesg */
1396 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1399 ata_mode_string(xfer_modes
),
1400 (unsigned long long)dev
->n_sectors
,
1401 (int)dev
->cylinders
, (int)dev
->heads
, (int)dev
->sectors
);
1405 ap
->host
->max_cmd_len
= 16;
1408 /* ATAPI-specific feature tests */
1409 else if (dev
->class == ATA_DEV_ATAPI
) {
1410 if (ata_id_is_ata(dev
->id
)) /* sanity check */
1413 rc
= atapi_cdb_len(dev
->id
);
1414 if ((rc
< 12) || (rc
> ATAPI_CDB_LEN
)) {
1415 printk(KERN_WARNING
"ata%u: unsupported CDB len\n", ap
->id
);
1418 ap
->cdb_len
= (unsigned int) rc
;
1419 ap
->host
->max_cmd_len
= (unsigned char) ap
->cdb_len
;
1421 /* print device info to dmesg */
1422 printk(KERN_INFO
"ata%u: dev %u ATAPI, max %s\n",
1424 ata_mode_string(xfer_modes
));
1427 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap
));
1431 printk(KERN_WARNING
"ata%u: dev %u not supported, ignoring\n",
1434 dev
->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1435 DPRINTK("EXIT, err\n");
1439 static inline u8
ata_dev_knobble(const struct ata_port
*ap
)
1441 return ((ap
->cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(ap
->device
->id
)));
1445 * ata_dev_config - Run device specific handlers and check for
1446 * SATA->PATA bridges
1453 void ata_dev_config(struct ata_port
*ap
, unsigned int i
)
1455 /* limit bridge transfers to udma5, 200 sectors */
1456 if (ata_dev_knobble(ap
)) {
1457 printk(KERN_INFO
"ata%u(%u): applying bridge limits\n",
1458 ap
->id
, ap
->device
->devno
);
1459 ap
->udma_mask
&= ATA_UDMA5
;
1460 ap
->host
->max_sectors
= ATA_MAX_SECTORS
;
1461 ap
->host
->hostt
->max_sectors
= ATA_MAX_SECTORS
;
1462 ap
->device
[i
].flags
|= ATA_DFLAG_LOCK_SECTORS
;
1465 if (ap
->ops
->dev_config
)
1466 ap
->ops
->dev_config(ap
, &ap
->device
[i
]);
1470 * ata_bus_probe - Reset and probe ATA bus
1473 * Master ATA bus probing function. Initiates a hardware-dependent
1474 * bus reset, then attempts to identify any devices found on
1478 * PCI/etc. bus probe sem.
1481 * Zero on success, non-zero on error.
1484 static int ata_bus_probe(struct ata_port
*ap
)
1486 unsigned int i
, found
= 0;
1488 ap
->ops
->phy_reset(ap
);
1489 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1492 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1493 ata_dev_identify(ap
, i
);
1494 if (ata_dev_present(&ap
->device
[i
])) {
1496 ata_dev_config(ap
,i
);
1500 if ((!found
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1501 goto err_out_disable
;
1504 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1505 goto err_out_disable
;
1510 ap
->ops
->port_disable(ap
);
1516 * ata_port_probe - Mark port as enabled
1517 * @ap: Port for which we indicate enablement
1519 * Modify @ap data structure such that the system
1520 * thinks that the entire port is enabled.
1522 * LOCKING: host_set lock, or some other form of
1526 void ata_port_probe(struct ata_port
*ap
)
1528 ap
->flags
&= ~ATA_FLAG_PORT_DISABLED
;
1532 * sata_print_link_status - Print SATA link status
1533 * @ap: SATA port to printk link status about
1535 * This function prints link speed and status of a SATA link.
1540 static void sata_print_link_status(struct ata_port
*ap
)
1545 if (!ap
->ops
->scr_read
)
1548 sstatus
= scr_read(ap
, SCR_STATUS
);
1550 if (sata_dev_present(ap
)) {
1551 tmp
= (sstatus
>> 4) & 0xf;
1554 else if (tmp
& (1 << 1))
1557 speed
= "<unknown>";
1558 printk(KERN_INFO
"ata%u: SATA link up %s Gbps (SStatus %X)\n",
1559 ap
->id
, speed
, sstatus
);
1561 printk(KERN_INFO
"ata%u: SATA link down (SStatus %X)\n",
1567 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1568 * @ap: SATA port associated with target SATA PHY.
1570 * This function issues commands to standard SATA Sxxx
1571 * PHY registers, to wake up the phy (and device), and
1572 * clear any reset condition.
1575 * PCI/etc. bus probe sem.
1578 void __sata_phy_reset(struct ata_port
*ap
)
1581 unsigned long timeout
= jiffies
+ (HZ
* 5);
1583 if (ap
->flags
& ATA_FLAG_SATA_RESET
) {
1584 /* issue phy wake/reset */
1585 scr_write_flush(ap
, SCR_CONTROL
, 0x301);
1586 /* Couldn't find anything in SATA I/II specs, but
1587 * AHCI-1.1 10.4.2 says at least 1 ms. */
1590 scr_write_flush(ap
, SCR_CONTROL
, 0x300); /* phy wake/clear reset */
1592 /* wait for phy to become ready, if necessary */
1595 sstatus
= scr_read(ap
, SCR_STATUS
);
1596 if ((sstatus
& 0xf) != 1)
1598 } while (time_before(jiffies
, timeout
));
1600 /* print link status */
1601 sata_print_link_status(ap
);
1603 /* TODO: phy layer with polling, timeouts, etc. */
1604 if (sata_dev_present(ap
))
1607 ata_port_disable(ap
);
1609 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1612 if (ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
)) {
1613 ata_port_disable(ap
);
1617 ap
->cbl
= ATA_CBL_SATA
;
1621 * sata_phy_reset - Reset SATA bus.
1622 * @ap: SATA port associated with target SATA PHY.
1624 * This function resets the SATA bus, and then probes
1625 * the bus for devices.
1628 * PCI/etc. bus probe sem.
1631 void sata_phy_reset(struct ata_port
*ap
)
1633 __sata_phy_reset(ap
);
1634 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1640 * ata_port_disable - Disable port.
1641 * @ap: Port to be disabled.
1643 * Modify @ap data structure such that the system
1644 * thinks that the entire port is disabled, and should
1645 * never attempt to probe or communicate with devices
1648 * LOCKING: host_set lock, or some other form of
1652 void ata_port_disable(struct ata_port
*ap
)
1654 ap
->device
[0].class = ATA_DEV_NONE
;
1655 ap
->device
[1].class = ATA_DEV_NONE
;
1656 ap
->flags
|= ATA_FLAG_PORT_DISABLED
;
1660 * This mode timing computation functionality is ported over from
1661 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1664 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1665 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1666 * for PIO 5, which is a nonstandard extension and UDMA6, which
1667 * is currently supported only by Maxtor drives.
1670 static const struct ata_timing ata_timing
[] = {
1672 { XFER_UDMA_6
, 0, 0, 0, 0, 0, 0, 0, 15 },
1673 { XFER_UDMA_5
, 0, 0, 0, 0, 0, 0, 0, 20 },
1674 { XFER_UDMA_4
, 0, 0, 0, 0, 0, 0, 0, 30 },
1675 { XFER_UDMA_3
, 0, 0, 0, 0, 0, 0, 0, 45 },
1677 { XFER_UDMA_2
, 0, 0, 0, 0, 0, 0, 0, 60 },
1678 { XFER_UDMA_1
, 0, 0, 0, 0, 0, 0, 0, 80 },
1679 { XFER_UDMA_0
, 0, 0, 0, 0, 0, 0, 0, 120 },
1681 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1683 { XFER_MW_DMA_2
, 25, 0, 0, 0, 70, 25, 120, 0 },
1684 { XFER_MW_DMA_1
, 45, 0, 0, 0, 80, 50, 150, 0 },
1685 { XFER_MW_DMA_0
, 60, 0, 0, 0, 215, 215, 480, 0 },
1687 { XFER_SW_DMA_2
, 60, 0, 0, 0, 120, 120, 240, 0 },
1688 { XFER_SW_DMA_1
, 90, 0, 0, 0, 240, 240, 480, 0 },
1689 { XFER_SW_DMA_0
, 120, 0, 0, 0, 480, 480, 960, 0 },
1691 /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1692 { XFER_PIO_4
, 25, 70, 25, 120, 70, 25, 120, 0 },
1693 { XFER_PIO_3
, 30, 80, 70, 180, 80, 70, 180, 0 },
1695 { XFER_PIO_2
, 30, 290, 40, 330, 100, 90, 240, 0 },
1696 { XFER_PIO_1
, 50, 290, 93, 383, 125, 100, 383, 0 },
1697 { XFER_PIO_0
, 70, 290, 240, 600, 165, 150, 600, 0 },
1699 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1704 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1705 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1707 static void ata_timing_quantize(const struct ata_timing
*t
, struct ata_timing
*q
, int T
, int UT
)
1709 q
->setup
= EZ(t
->setup
* 1000, T
);
1710 q
->act8b
= EZ(t
->act8b
* 1000, T
);
1711 q
->rec8b
= EZ(t
->rec8b
* 1000, T
);
1712 q
->cyc8b
= EZ(t
->cyc8b
* 1000, T
);
1713 q
->active
= EZ(t
->active
* 1000, T
);
1714 q
->recover
= EZ(t
->recover
* 1000, T
);
1715 q
->cycle
= EZ(t
->cycle
* 1000, T
);
1716 q
->udma
= EZ(t
->udma
* 1000, UT
);
1719 void ata_timing_merge(const struct ata_timing
*a
, const struct ata_timing
*b
,
1720 struct ata_timing
*m
, unsigned int what
)
1722 if (what
& ATA_TIMING_SETUP
) m
->setup
= max(a
->setup
, b
->setup
);
1723 if (what
& ATA_TIMING_ACT8B
) m
->act8b
= max(a
->act8b
, b
->act8b
);
1724 if (what
& ATA_TIMING_REC8B
) m
->rec8b
= max(a
->rec8b
, b
->rec8b
);
1725 if (what
& ATA_TIMING_CYC8B
) m
->cyc8b
= max(a
->cyc8b
, b
->cyc8b
);
1726 if (what
& ATA_TIMING_ACTIVE
) m
->active
= max(a
->active
, b
->active
);
1727 if (what
& ATA_TIMING_RECOVER
) m
->recover
= max(a
->recover
, b
->recover
);
1728 if (what
& ATA_TIMING_CYCLE
) m
->cycle
= max(a
->cycle
, b
->cycle
);
1729 if (what
& ATA_TIMING_UDMA
) m
->udma
= max(a
->udma
, b
->udma
);
1732 static const struct ata_timing
* ata_timing_find_mode(unsigned short speed
)
1734 const struct ata_timing
*t
;
1736 for (t
= ata_timing
; t
->mode
!= speed
; t
++)
1737 if (t
->mode
== 0xFF)
1742 int ata_timing_compute(struct ata_device
*adev
, unsigned short speed
,
1743 struct ata_timing
*t
, int T
, int UT
)
1745 const struct ata_timing
*s
;
1746 struct ata_timing p
;
1752 if (!(s
= ata_timing_find_mode(speed
)))
1755 memcpy(t
, s
, sizeof(*s
));
1758 * If the drive is an EIDE drive, it can tell us it needs extended
1759 * PIO/MW_DMA cycle timing.
1762 if (adev
->id
[ATA_ID_FIELD_VALID
] & 2) { /* EIDE drive */
1763 memset(&p
, 0, sizeof(p
));
1764 if(speed
>= XFER_PIO_0
&& speed
<= XFER_SW_DMA_0
) {
1765 if (speed
<= XFER_PIO_2
) p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO
];
1766 else p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO_IORDY
];
1767 } else if(speed
>= XFER_MW_DMA_0
&& speed
<= XFER_MW_DMA_2
) {
1768 p
.cycle
= adev
->id
[ATA_ID_EIDE_DMA_MIN
];
1770 ata_timing_merge(&p
, t
, t
, ATA_TIMING_CYCLE
| ATA_TIMING_CYC8B
);
1774 * Convert the timing to bus clock counts.
1777 ata_timing_quantize(t
, t
, T
, UT
);
1780 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1781 * and some other commands. We have to ensure that the DMA cycle timing is
1782 * slower/equal than the fastest PIO timing.
1785 if (speed
> XFER_PIO_4
) {
1786 ata_timing_compute(adev
, adev
->pio_mode
, &p
, T
, UT
);
1787 ata_timing_merge(&p
, t
, t
, ATA_TIMING_ALL
);
1791 * Lenghten active & recovery time so that cycle time is correct.
1794 if (t
->act8b
+ t
->rec8b
< t
->cyc8b
) {
1795 t
->act8b
+= (t
->cyc8b
- (t
->act8b
+ t
->rec8b
)) / 2;
1796 t
->rec8b
= t
->cyc8b
- t
->act8b
;
1799 if (t
->active
+ t
->recover
< t
->cycle
) {
1800 t
->active
+= (t
->cycle
- (t
->active
+ t
->recover
)) / 2;
1801 t
->recover
= t
->cycle
- t
->active
;
1807 static const struct {
1810 } xfer_mode_classes
[] = {
1811 { ATA_SHIFT_UDMA
, XFER_UDMA_0
},
1812 { ATA_SHIFT_MWDMA
, XFER_MW_DMA_0
},
1813 { ATA_SHIFT_PIO
, XFER_PIO_0
},
1816 static u8
base_from_shift(unsigned int shift
)
1820 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++)
1821 if (xfer_mode_classes
[i
].shift
== shift
)
1822 return xfer_mode_classes
[i
].base
;
1827 static void ata_dev_set_mode(struct ata_port
*ap
, struct ata_device
*dev
)
1832 if (!ata_dev_present(dev
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1835 if (dev
->xfer_shift
== ATA_SHIFT_PIO
)
1836 dev
->flags
|= ATA_DFLAG_PIO
;
1838 ata_dev_set_xfermode(ap
, dev
);
1840 base
= base_from_shift(dev
->xfer_shift
);
1841 ofs
= dev
->xfer_mode
- base
;
1842 idx
= ofs
+ dev
->xfer_shift
;
1843 WARN_ON(idx
>= ARRAY_SIZE(xfer_mode_str
));
1845 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1846 idx
, dev
->xfer_shift
, (int)dev
->xfer_mode
, (int)base
, ofs
);
1848 printk(KERN_INFO
"ata%u: dev %u configured for %s\n",
1849 ap
->id
, dev
->devno
, xfer_mode_str
[idx
]);
1852 static int ata_host_set_pio(struct ata_port
*ap
)
1858 mask
= ata_get_mode_mask(ap
, ATA_SHIFT_PIO
);
1861 printk(KERN_WARNING
"ata%u: no PIO support\n", ap
->id
);
1865 base
= base_from_shift(ATA_SHIFT_PIO
);
1866 xfer_mode
= base
+ x
;
1868 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1869 (int)base
, (int)xfer_mode
, mask
, x
);
1871 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1872 struct ata_device
*dev
= &ap
->device
[i
];
1873 if (ata_dev_present(dev
)) {
1874 dev
->pio_mode
= xfer_mode
;
1875 dev
->xfer_mode
= xfer_mode
;
1876 dev
->xfer_shift
= ATA_SHIFT_PIO
;
1877 if (ap
->ops
->set_piomode
)
1878 ap
->ops
->set_piomode(ap
, dev
);
1885 static void ata_host_set_dma(struct ata_port
*ap
, u8 xfer_mode
,
1886 unsigned int xfer_shift
)
1890 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1891 struct ata_device
*dev
= &ap
->device
[i
];
1892 if (ata_dev_present(dev
)) {
1893 dev
->dma_mode
= xfer_mode
;
1894 dev
->xfer_mode
= xfer_mode
;
1895 dev
->xfer_shift
= xfer_shift
;
1896 if (ap
->ops
->set_dmamode
)
1897 ap
->ops
->set_dmamode(ap
, dev
);
1903 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1904 * @ap: port on which timings will be programmed
1906 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1909 * PCI/etc. bus probe sem.
1912 static void ata_set_mode(struct ata_port
*ap
)
1914 unsigned int xfer_shift
;
1918 /* step 1: always set host PIO timings */
1919 rc
= ata_host_set_pio(ap
);
1923 /* step 2: choose the best data xfer mode */
1924 xfer_mode
= xfer_shift
= 0;
1925 rc
= ata_choose_xfer_mode(ap
, &xfer_mode
, &xfer_shift
);
1929 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1930 if (xfer_shift
!= ATA_SHIFT_PIO
)
1931 ata_host_set_dma(ap
, xfer_mode
, xfer_shift
);
1933 /* step 4: update devices' xfer mode */
1934 ata_dev_set_mode(ap
, &ap
->device
[0]);
1935 ata_dev_set_mode(ap
, &ap
->device
[1]);
1937 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1940 if (ap
->ops
->post_set_mode
)
1941 ap
->ops
->post_set_mode(ap
);
1946 ata_port_disable(ap
);
1950 * ata_busy_sleep - sleep until BSY clears, or timeout
1951 * @ap: port containing status register to be polled
1952 * @tmout_pat: impatience timeout
1953 * @tmout: overall timeout
1955 * Sleep until ATA Status register bit BSY clears,
1956 * or a timeout occurs.
1962 unsigned int ata_busy_sleep (struct ata_port
*ap
,
1963 unsigned long tmout_pat
, unsigned long tmout
)
1965 unsigned long timer_start
, timeout
;
1968 status
= ata_busy_wait(ap
, ATA_BUSY
, 300);
1969 timer_start
= jiffies
;
1970 timeout
= timer_start
+ tmout_pat
;
1971 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1973 status
= ata_busy_wait(ap
, ATA_BUSY
, 3);
1976 if (status
& ATA_BUSY
)
1977 printk(KERN_WARNING
"ata%u is slow to respond, "
1978 "please be patient\n", ap
->id
);
1980 timeout
= timer_start
+ tmout
;
1981 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1983 status
= ata_chk_status(ap
);
1986 if (status
& ATA_BUSY
) {
1987 printk(KERN_ERR
"ata%u failed to respond (%lu secs)\n",
1988 ap
->id
, tmout
/ HZ
);
1995 static void ata_bus_post_reset(struct ata_port
*ap
, unsigned int devmask
)
1997 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1998 unsigned int dev0
= devmask
& (1 << 0);
1999 unsigned int dev1
= devmask
& (1 << 1);
2000 unsigned long timeout
;
2002 /* if device 0 was found in ata_devchk, wait for its
2006 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
2008 /* if device 1 was found in ata_devchk, wait for
2009 * register access, then wait for BSY to clear
2011 timeout
= jiffies
+ ATA_TMOUT_BOOT
;
2015 ap
->ops
->dev_select(ap
, 1);
2016 if (ap
->flags
& ATA_FLAG_MMIO
) {
2017 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
2018 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
2020 nsect
= inb(ioaddr
->nsect_addr
);
2021 lbal
= inb(ioaddr
->lbal_addr
);
2023 if ((nsect
== 1) && (lbal
== 1))
2025 if (time_after(jiffies
, timeout
)) {
2029 msleep(50); /* give drive a breather */
2032 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
2034 /* is all this really necessary? */
2035 ap
->ops
->dev_select(ap
, 0);
2037 ap
->ops
->dev_select(ap
, 1);
2039 ap
->ops
->dev_select(ap
, 0);
2043 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2044 * @ap: Port to reset and probe
2046 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2047 * probe the bus. Not often used these days.
2050 * PCI/etc. bus probe sem.
2051 * Obtains host_set lock.
2055 static unsigned int ata_bus_edd(struct ata_port
*ap
)
2057 struct ata_taskfile tf
;
2058 unsigned long flags
;
2060 /* set up execute-device-diag (bus reset) taskfile */
2061 /* also, take interrupts to a known state (disabled) */
2062 DPRINTK("execute-device-diag\n");
2063 ata_tf_init(ap
, &tf
, 0);
2065 tf
.command
= ATA_CMD_EDD
;
2066 tf
.protocol
= ATA_PROT_NODATA
;
2069 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2070 ata_tf_to_host(ap
, &tf
);
2071 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2073 /* spec says at least 2ms. but who knows with those
2074 * crazy ATAPI devices...
2078 return ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
2081 static unsigned int ata_bus_softreset(struct ata_port
*ap
,
2082 unsigned int devmask
)
2084 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
2086 DPRINTK("ata%u: bus reset via SRST\n", ap
->id
);
2088 /* software reset. causes dev0 to be selected */
2089 if (ap
->flags
& ATA_FLAG_MMIO
) {
2090 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2091 udelay(20); /* FIXME: flush */
2092 writeb(ap
->ctl
| ATA_SRST
, (void __iomem
*) ioaddr
->ctl_addr
);
2093 udelay(20); /* FIXME: flush */
2094 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2096 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2098 outb(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
2100 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2103 /* spec mandates ">= 2ms" before checking status.
2104 * We wait 150ms, because that was the magic delay used for
2105 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2106 * between when the ATA command register is written, and then
2107 * status is checked. Because waiting for "a while" before
2108 * checking status is fine, post SRST, we perform this magic
2109 * delay here as well.
2113 ata_bus_post_reset(ap
, devmask
);
2119 * ata_bus_reset - reset host port and associated ATA channel
2120 * @ap: port to reset
2122 * This is typically the first time we actually start issuing
2123 * commands to the ATA channel. We wait for BSY to clear, then
2124 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2125 * result. Determine what devices, if any, are on the channel
2126 * by looking at the device 0/1 error register. Look at the signature
2127 * stored in each device's taskfile registers, to determine if
2128 * the device is ATA or ATAPI.
2131 * PCI/etc. bus probe sem.
2132 * Obtains host_set lock.
2135 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2138 void ata_bus_reset(struct ata_port
*ap
)
2140 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
2141 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
2143 unsigned int dev0
, dev1
= 0, rc
= 0, devmask
= 0;
2145 DPRINTK("ENTER, host %u, port %u\n", ap
->id
, ap
->port_no
);
2147 /* determine if device 0/1 are present */
2148 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
2151 dev0
= ata_devchk(ap
, 0);
2153 dev1
= ata_devchk(ap
, 1);
2157 devmask
|= (1 << 0);
2159 devmask
|= (1 << 1);
2161 /* select device 0 again */
2162 ap
->ops
->dev_select(ap
, 0);
2164 /* issue bus reset */
2165 if (ap
->flags
& ATA_FLAG_SRST
)
2166 rc
= ata_bus_softreset(ap
, devmask
);
2167 else if ((ap
->flags
& ATA_FLAG_SATA_RESET
) == 0) {
2168 /* set up device control */
2169 if (ap
->flags
& ATA_FLAG_MMIO
)
2170 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2172 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2173 rc
= ata_bus_edd(ap
);
2180 * determine by signature whether we have ATA or ATAPI devices
2182 ap
->device
[0].class = ata_dev_try_classify(ap
, 0, &err
);
2183 if ((slave_possible
) && (err
!= 0x81))
2184 ap
->device
[1].class = ata_dev_try_classify(ap
, 1, &err
);
2186 /* re-enable interrupts */
2187 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
2190 /* is double-select really necessary? */
2191 if (ap
->device
[1].class != ATA_DEV_NONE
)
2192 ap
->ops
->dev_select(ap
, 1);
2193 if (ap
->device
[0].class != ATA_DEV_NONE
)
2194 ap
->ops
->dev_select(ap
, 0);
2196 /* if no devices were detected, disable this port */
2197 if ((ap
->device
[0].class == ATA_DEV_NONE
) &&
2198 (ap
->device
[1].class == ATA_DEV_NONE
))
2201 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
2202 /* set up device control for ATA_FLAG_SATA_RESET */
2203 if (ap
->flags
& ATA_FLAG_MMIO
)
2204 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
2206 outb(ap
->ctl
, ioaddr
->ctl_addr
);
2213 printk(KERN_ERR
"ata%u: disabling port\n", ap
->id
);
2214 ap
->ops
->port_disable(ap
);
2219 static void ata_pr_blacklisted(const struct ata_port
*ap
,
2220 const struct ata_device
*dev
)
2222 printk(KERN_WARNING
"ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2223 ap
->id
, dev
->devno
);
2226 static const char * const ata_dma_blacklist
[] = {
2245 "Toshiba CD-ROM XM-6202B",
2246 "TOSHIBA CD-ROM XM-1702BC",
2248 "E-IDE CD-ROM CR-840",
2251 "SAMSUNG CD-ROM SC-148C",
2252 "SAMSUNG CD-ROM SC",
2254 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2258 static int ata_dma_blacklisted(const struct ata_device
*dev
)
2260 unsigned char model_num
[40];
2265 ata_dev_id_string(dev
->id
, model_num
, ATA_ID_PROD_OFS
,
2268 len
= strnlen(s
, sizeof(model_num
));
2270 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2271 while ((len
> 0) && (s
[len
- 1] == ' ')) {
2276 for (i
= 0; i
< ARRAY_SIZE(ata_dma_blacklist
); i
++)
2277 if (!strncmp(ata_dma_blacklist
[i
], s
, len
))
2283 static unsigned int ata_get_mode_mask(const struct ata_port
*ap
, int shift
)
2285 const struct ata_device
*master
, *slave
;
2288 master
= &ap
->device
[0];
2289 slave
= &ap
->device
[1];
2291 assert (ata_dev_present(master
) || ata_dev_present(slave
));
2293 if (shift
== ATA_SHIFT_UDMA
) {
2294 mask
= ap
->udma_mask
;
2295 if (ata_dev_present(master
)) {
2296 mask
&= (master
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2297 if (ata_dma_blacklisted(master
)) {
2299 ata_pr_blacklisted(ap
, master
);
2302 if (ata_dev_present(slave
)) {
2303 mask
&= (slave
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2304 if (ata_dma_blacklisted(slave
)) {
2306 ata_pr_blacklisted(ap
, slave
);
2310 else if (shift
== ATA_SHIFT_MWDMA
) {
2311 mask
= ap
->mwdma_mask
;
2312 if (ata_dev_present(master
)) {
2313 mask
&= (master
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2314 if (ata_dma_blacklisted(master
)) {
2316 ata_pr_blacklisted(ap
, master
);
2319 if (ata_dev_present(slave
)) {
2320 mask
&= (slave
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2321 if (ata_dma_blacklisted(slave
)) {
2323 ata_pr_blacklisted(ap
, slave
);
2327 else if (shift
== ATA_SHIFT_PIO
) {
2328 mask
= ap
->pio_mask
;
2329 if (ata_dev_present(master
)) {
2330 /* spec doesn't return explicit support for
2331 * PIO0-2, so we fake it
2333 u16 tmp_mode
= master
->id
[ATA_ID_PIO_MODES
] & 0x03;
2338 if (ata_dev_present(slave
)) {
2339 /* spec doesn't return explicit support for
2340 * PIO0-2, so we fake it
2342 u16 tmp_mode
= slave
->id
[ATA_ID_PIO_MODES
] & 0x03;
2349 mask
= 0xffffffff; /* shut up compiler warning */
2356 /* find greatest bit */
2357 static int fgb(u32 bitmap
)
2362 for (i
= 0; i
< 32; i
++)
2363 if (bitmap
& (1 << i
))
2370 * ata_choose_xfer_mode - attempt to find best transfer mode
2371 * @ap: Port for which an xfer mode will be selected
2372 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2373 * @xfer_shift_out: (output) bit shift that selects this mode
2375 * Based on host and device capabilities, determine the
2376 * maximum transfer mode that is amenable to all.
2379 * PCI/etc. bus probe sem.
2382 * Zero on success, negative on error.
2385 static int ata_choose_xfer_mode(const struct ata_port
*ap
,
2387 unsigned int *xfer_shift_out
)
2389 unsigned int mask
, shift
;
2392 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++) {
2393 shift
= xfer_mode_classes
[i
].shift
;
2394 mask
= ata_get_mode_mask(ap
, shift
);
2398 *xfer_mode_out
= xfer_mode_classes
[i
].base
+ x
;
2399 *xfer_shift_out
= shift
;
2408 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2409 * @ap: Port associated with device @dev
2410 * @dev: Device to which command will be sent
2412 * Issue SET FEATURES - XFER MODE command to device @dev
2416 * PCI/etc. bus probe sem.
2419 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
)
2421 struct ata_taskfile tf
;
2423 /* set up set-features taskfile */
2424 DPRINTK("set features - xfer mode\n");
2426 ata_tf_init(ap
, &tf
, dev
->devno
);
2427 tf
.command
= ATA_CMD_SET_FEATURES
;
2428 tf
.feature
= SETFEATURES_XFER
;
2429 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2430 tf
.protocol
= ATA_PROT_NODATA
;
2431 tf
.nsect
= dev
->xfer_mode
;
2433 if (ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0)) {
2434 printk(KERN_ERR
"ata%u: failed to set xfermode, disabled\n",
2436 ata_port_disable(ap
);
2443 * ata_dev_reread_id - Reread the device identify device info
2444 * @ap: port where the device is
2445 * @dev: device to reread the identify device info
2450 static void ata_dev_reread_id(struct ata_port
*ap
, struct ata_device
*dev
)
2452 struct ata_taskfile tf
;
2454 ata_tf_init(ap
, &tf
, dev
->devno
);
2456 if (dev
->class == ATA_DEV_ATA
) {
2457 tf
.command
= ATA_CMD_ID_ATA
;
2458 DPRINTK("do ATA identify\n");
2460 tf
.command
= ATA_CMD_ID_ATAPI
;
2461 DPRINTK("do ATAPI identify\n");
2464 tf
.flags
|= ATA_TFLAG_DEVICE
;
2465 tf
.protocol
= ATA_PROT_PIO
;
2467 if (ata_exec_internal(ap
, dev
, &tf
, DMA_FROM_DEVICE
,
2468 dev
->id
, sizeof(dev
->id
)))
2471 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
2479 printk(KERN_ERR
"ata%u: failed to reread ID, disabled\n", ap
->id
);
2480 ata_port_disable(ap
);
2484 * ata_dev_init_params - Issue INIT DEV PARAMS command
2485 * @ap: Port associated with device @dev
2486 * @dev: Device to which command will be sent
2491 static void ata_dev_init_params(struct ata_port
*ap
, struct ata_device
*dev
)
2493 struct ata_taskfile tf
;
2494 u16 sectors
= dev
->id
[6];
2495 u16 heads
= dev
->id
[3];
2497 /* Number of sectors per track 1-255. Number of heads 1-16 */
2498 if (sectors
< 1 || sectors
> 255 || heads
< 1 || heads
> 16)
2501 /* set up init dev params taskfile */
2502 DPRINTK("init dev params \n");
2504 ata_tf_init(ap
, &tf
, dev
->devno
);
2505 tf
.command
= ATA_CMD_INIT_DEV_PARAMS
;
2506 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2507 tf
.protocol
= ATA_PROT_NODATA
;
2509 tf
.device
|= (heads
- 1) & 0x0f; /* max head = num. of heads - 1 */
2511 if (ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0)) {
2512 printk(KERN_ERR
"ata%u: failed to init parameters, disabled\n",
2514 ata_port_disable(ap
);
2521 * ata_sg_clean - Unmap DMA memory associated with command
2522 * @qc: Command containing DMA memory to be released
2524 * Unmap all mapped DMA memory associated with this command.
2527 * spin_lock_irqsave(host_set lock)
2530 static void ata_sg_clean(struct ata_queued_cmd
*qc
)
2532 struct ata_port
*ap
= qc
->ap
;
2533 struct scatterlist
*sg
= qc
->__sg
;
2534 int dir
= qc
->dma_dir
;
2535 void *pad_buf
= NULL
;
2537 assert(qc
->flags
& ATA_QCFLAG_DMAMAP
);
2540 if (qc
->flags
& ATA_QCFLAG_SINGLE
)
2541 assert(qc
->n_elem
== 1);
2543 VPRINTK("unmapping %u sg elements\n", qc
->n_elem
);
2545 /* if we padded the buffer out to 32-bit bound, and data
2546 * xfer direction is from-device, we must copy from the
2547 * pad buffer back into the supplied buffer
2549 if (qc
->pad_len
&& !(qc
->tf
.flags
& ATA_TFLAG_WRITE
))
2550 pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2552 if (qc
->flags
& ATA_QCFLAG_SG
) {
2554 dma_unmap_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2555 /* restore last sg */
2556 sg
[qc
->orig_n_elem
- 1].length
+= qc
->pad_len
;
2558 struct scatterlist
*psg
= &qc
->pad_sgent
;
2559 void *addr
= kmap_atomic(psg
->page
, KM_IRQ0
);
2560 memcpy(addr
+ psg
->offset
, pad_buf
, qc
->pad_len
);
2561 kunmap_atomic(addr
, KM_IRQ0
);
2564 if (sg_dma_len(&sg
[0]) > 0)
2565 dma_unmap_single(ap
->host_set
->dev
,
2566 sg_dma_address(&sg
[0]), sg_dma_len(&sg
[0]),
2569 sg
->length
+= qc
->pad_len
;
2571 memcpy(qc
->buf_virt
+ sg
->length
- qc
->pad_len
,
2572 pad_buf
, qc
->pad_len
);
2575 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
2580 * ata_fill_sg - Fill PCI IDE PRD table
2581 * @qc: Metadata associated with taskfile to be transferred
2583 * Fill PCI IDE PRD (scatter-gather) table with segments
2584 * associated with the current disk command.
2587 * spin_lock_irqsave(host_set lock)
2590 static void ata_fill_sg(struct ata_queued_cmd
*qc
)
2592 struct ata_port
*ap
= qc
->ap
;
2593 struct scatterlist
*sg
;
2596 assert(qc
->__sg
!= NULL
);
2597 assert(qc
->n_elem
> 0);
2600 ata_for_each_sg(sg
, qc
) {
2604 /* determine if physical DMA addr spans 64K boundary.
2605 * Note h/w doesn't support 64-bit, so we unconditionally
2606 * truncate dma_addr_t to u32.
2608 addr
= (u32
) sg_dma_address(sg
);
2609 sg_len
= sg_dma_len(sg
);
2612 offset
= addr
& 0xffff;
2614 if ((offset
+ sg_len
) > 0x10000)
2615 len
= 0x10000 - offset
;
2617 ap
->prd
[idx
].addr
= cpu_to_le32(addr
);
2618 ap
->prd
[idx
].flags_len
= cpu_to_le32(len
& 0xffff);
2619 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx
, addr
, len
);
2628 ap
->prd
[idx
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
2631 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2632 * @qc: Metadata associated with taskfile to check
2634 * Allow low-level driver to filter ATA PACKET commands, returning
2635 * a status indicating whether or not it is OK to use DMA for the
2636 * supplied PACKET command.
2639 * spin_lock_irqsave(host_set lock)
2641 * RETURNS: 0 when ATAPI DMA can be used
2644 int ata_check_atapi_dma(struct ata_queued_cmd
*qc
)
2646 struct ata_port
*ap
= qc
->ap
;
2647 int rc
= 0; /* Assume ATAPI DMA is OK by default */
2649 if (ap
->ops
->check_atapi_dma
)
2650 rc
= ap
->ops
->check_atapi_dma(qc
);
2655 * ata_qc_prep - Prepare taskfile for submission
2656 * @qc: Metadata associated with taskfile to be prepared
2658 * Prepare ATA taskfile for submission.
2661 * spin_lock_irqsave(host_set lock)
2663 void ata_qc_prep(struct ata_queued_cmd
*qc
)
2665 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
2672 * ata_sg_init_one - Associate command with memory buffer
2673 * @qc: Command to be associated
2674 * @buf: Memory buffer
2675 * @buflen: Length of memory buffer, in bytes.
2677 * Initialize the data-related elements of queued_cmd @qc
2678 * to point to a single memory buffer, @buf of byte length @buflen.
2681 * spin_lock_irqsave(host_set lock)
2684 void ata_sg_init_one(struct ata_queued_cmd
*qc
, void *buf
, unsigned int buflen
)
2686 struct scatterlist
*sg
;
2688 qc
->flags
|= ATA_QCFLAG_SINGLE
;
2690 memset(&qc
->sgent
, 0, sizeof(qc
->sgent
));
2691 qc
->__sg
= &qc
->sgent
;
2693 qc
->orig_n_elem
= 1;
2697 sg_init_one(sg
, buf
, buflen
);
2701 * ata_sg_init - Associate command with scatter-gather table.
2702 * @qc: Command to be associated
2703 * @sg: Scatter-gather table.
2704 * @n_elem: Number of elements in s/g table.
2706 * Initialize the data-related elements of queued_cmd @qc
2707 * to point to a scatter-gather table @sg, containing @n_elem
2711 * spin_lock_irqsave(host_set lock)
2714 void ata_sg_init(struct ata_queued_cmd
*qc
, struct scatterlist
*sg
,
2715 unsigned int n_elem
)
2717 qc
->flags
|= ATA_QCFLAG_SG
;
2719 qc
->n_elem
= n_elem
;
2720 qc
->orig_n_elem
= n_elem
;
2724 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2725 * @qc: Command with memory buffer to be mapped.
2727 * DMA-map the memory buffer associated with queued_cmd @qc.
2730 * spin_lock_irqsave(host_set lock)
2733 * Zero on success, negative on error.
2736 static int ata_sg_setup_one(struct ata_queued_cmd
*qc
)
2738 struct ata_port
*ap
= qc
->ap
;
2739 int dir
= qc
->dma_dir
;
2740 struct scatterlist
*sg
= qc
->__sg
;
2741 dma_addr_t dma_address
;
2743 /* we must lengthen transfers to end on a 32-bit boundary */
2744 qc
->pad_len
= sg
->length
& 3;
2746 void *pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2747 struct scatterlist
*psg
= &qc
->pad_sgent
;
2749 assert(qc
->dev
->class == ATA_DEV_ATAPI
);
2751 memset(pad_buf
, 0, ATA_DMA_PAD_SZ
);
2753 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
)
2754 memcpy(pad_buf
, qc
->buf_virt
+ sg
->length
- qc
->pad_len
,
2757 sg_dma_address(psg
) = ap
->pad_dma
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2758 sg_dma_len(psg
) = ATA_DMA_PAD_SZ
;
2760 sg
->length
-= qc
->pad_len
;
2762 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2763 sg
->length
, qc
->pad_len
);
2767 sg_dma_address(sg
) = 0;
2771 dma_address
= dma_map_single(ap
->host_set
->dev
, qc
->buf_virt
,
2773 if (dma_mapping_error(dma_address
)) {
2775 sg
->length
+= qc
->pad_len
;
2779 sg_dma_address(sg
) = dma_address
;
2781 sg_dma_len(sg
) = sg
->length
;
2783 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg
),
2784 qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2790 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2791 * @qc: Command with scatter-gather table to be mapped.
2793 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2796 * spin_lock_irqsave(host_set lock)
2799 * Zero on success, negative on error.
2803 static int ata_sg_setup(struct ata_queued_cmd
*qc
)
2805 struct ata_port
*ap
= qc
->ap
;
2806 struct scatterlist
*sg
= qc
->__sg
;
2807 struct scatterlist
*lsg
= &sg
[qc
->n_elem
- 1];
2808 int n_elem
, pre_n_elem
, dir
, trim_sg
= 0;
2810 VPRINTK("ENTER, ata%u\n", ap
->id
);
2811 assert(qc
->flags
& ATA_QCFLAG_SG
);
2813 /* we must lengthen transfers to end on a 32-bit boundary */
2814 qc
->pad_len
= lsg
->length
& 3;
2816 void *pad_buf
= ap
->pad
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2817 struct scatterlist
*psg
= &qc
->pad_sgent
;
2818 unsigned int offset
;
2820 assert(qc
->dev
->class == ATA_DEV_ATAPI
);
2822 memset(pad_buf
, 0, ATA_DMA_PAD_SZ
);
2825 * psg->page/offset are used to copy to-be-written
2826 * data in this function or read data in ata_sg_clean.
2828 offset
= lsg
->offset
+ lsg
->length
- qc
->pad_len
;
2829 psg
->page
= nth_page(lsg
->page
, offset
>> PAGE_SHIFT
);
2830 psg
->offset
= offset_in_page(offset
);
2832 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
) {
2833 void *addr
= kmap_atomic(psg
->page
, KM_IRQ0
);
2834 memcpy(pad_buf
, addr
+ psg
->offset
, qc
->pad_len
);
2835 kunmap_atomic(addr
, KM_IRQ0
);
2838 sg_dma_address(psg
) = ap
->pad_dma
+ (qc
->tag
* ATA_DMA_PAD_SZ
);
2839 sg_dma_len(psg
) = ATA_DMA_PAD_SZ
;
2841 lsg
->length
-= qc
->pad_len
;
2842 if (lsg
->length
== 0)
2845 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2846 qc
->n_elem
- 1, lsg
->length
, qc
->pad_len
);
2849 pre_n_elem
= qc
->n_elem
;
2850 if (trim_sg
&& pre_n_elem
)
2859 n_elem
= dma_map_sg(ap
->host_set
->dev
, sg
, pre_n_elem
, dir
);
2861 /* restore last sg */
2862 lsg
->length
+= qc
->pad_len
;
2866 DPRINTK("%d sg elements mapped\n", n_elem
);
2869 qc
->n_elem
= n_elem
;
2875 * ata_poll_qc_complete - turn irq back on and finish qc
2876 * @qc: Command to complete
2877 * @err_mask: ATA status register content
2880 * None. (grabs host lock)
2883 void ata_poll_qc_complete(struct ata_queued_cmd
*qc
)
2885 struct ata_port
*ap
= qc
->ap
;
2886 unsigned long flags
;
2888 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2889 ap
->flags
&= ~ATA_FLAG_NOINTR
;
2891 ata_qc_complete(qc
);
2892 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2897 * @ap: the target ata_port
2900 * None. (executing in kernel thread context)
2903 * timeout value to use
2906 static unsigned long ata_pio_poll(struct ata_port
*ap
)
2908 struct ata_queued_cmd
*qc
;
2910 unsigned int poll_state
= HSM_ST_UNKNOWN
;
2911 unsigned int reg_state
= HSM_ST_UNKNOWN
;
2913 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2916 switch (ap
->hsm_task_state
) {
2919 poll_state
= HSM_ST_POLL
;
2923 case HSM_ST_LAST_POLL
:
2924 poll_state
= HSM_ST_LAST_POLL
;
2925 reg_state
= HSM_ST_LAST
;
2932 status
= ata_chk_status(ap
);
2933 if (status
& ATA_BUSY
) {
2934 if (time_after(jiffies
, ap
->pio_task_timeout
)) {
2935 qc
->err_mask
|= AC_ERR_TIMEOUT
;
2936 ap
->hsm_task_state
= HSM_ST_TMOUT
;
2939 ap
->hsm_task_state
= poll_state
;
2940 return ATA_SHORT_PAUSE
;
2943 ap
->hsm_task_state
= reg_state
;
2948 * ata_pio_complete - check if drive is busy or idle
2949 * @ap: the target ata_port
2952 * None. (executing in kernel thread context)
2955 * Non-zero if qc completed, zero otherwise.
2958 static int ata_pio_complete (struct ata_port
*ap
)
2960 struct ata_queued_cmd
*qc
;
2964 * This is purely heuristic. This is a fast path. Sometimes when
2965 * we enter, BSY will be cleared in a chk-status or two. If not,
2966 * the drive is probably seeking or something. Snooze for a couple
2967 * msecs, then chk-status again. If still busy, fall back to
2968 * HSM_ST_POLL state.
2970 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
, 10);
2971 if (drv_stat
& ATA_BUSY
) {
2973 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
, 10);
2974 if (drv_stat
& ATA_BUSY
) {
2975 ap
->hsm_task_state
= HSM_ST_LAST_POLL
;
2976 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
2981 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2984 drv_stat
= ata_wait_idle(ap
);
2985 if (!ata_ok(drv_stat
)) {
2986 qc
->err_mask
|= __ac_err_mask(drv_stat
);
2987 ap
->hsm_task_state
= HSM_ST_ERR
;
2991 ap
->hsm_task_state
= HSM_ST_IDLE
;
2993 assert(qc
->err_mask
== 0);
2994 ata_poll_qc_complete(qc
);
2996 /* another command may start at this point */
3003 * swap_buf_le16 - swap halves of 16-words in place
3004 * @buf: Buffer to swap
3005 * @buf_words: Number of 16-bit words in buffer.
3007 * Swap halves of 16-bit words if needed to convert from
3008 * little-endian byte order to native cpu byte order, or
3012 * Inherited from caller.
3014 void swap_buf_le16(u16
*buf
, unsigned int buf_words
)
3019 for (i
= 0; i
< buf_words
; i
++)
3020 buf
[i
] = le16_to_cpu(buf
[i
]);
3021 #endif /* __BIG_ENDIAN */
3025 * ata_mmio_data_xfer - Transfer data by MMIO
3026 * @ap: port to read/write
3028 * @buflen: buffer length
3029 * @write_data: read/write
3031 * Transfer data from/to the device data register by MMIO.
3034 * Inherited from caller.
3037 static void ata_mmio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3038 unsigned int buflen
, int write_data
)
3041 unsigned int words
= buflen
>> 1;
3042 u16
*buf16
= (u16
*) buf
;
3043 void __iomem
*mmio
= (void __iomem
*)ap
->ioaddr
.data_addr
;
3045 /* Transfer multiple of 2 bytes */
3047 for (i
= 0; i
< words
; i
++)
3048 writew(le16_to_cpu(buf16
[i
]), mmio
);
3050 for (i
= 0; i
< words
; i
++)
3051 buf16
[i
] = cpu_to_le16(readw(mmio
));
3054 /* Transfer trailing 1 byte, if any. */
3055 if (unlikely(buflen
& 0x01)) {
3056 u16 align_buf
[1] = { 0 };
3057 unsigned char *trailing_buf
= buf
+ buflen
- 1;
3060 memcpy(align_buf
, trailing_buf
, 1);
3061 writew(le16_to_cpu(align_buf
[0]), mmio
);
3063 align_buf
[0] = cpu_to_le16(readw(mmio
));
3064 memcpy(trailing_buf
, align_buf
, 1);
3070 * ata_pio_data_xfer - Transfer data by PIO
3071 * @ap: port to read/write
3073 * @buflen: buffer length
3074 * @write_data: read/write
3076 * Transfer data from/to the device data register by PIO.
3079 * Inherited from caller.
3082 static void ata_pio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3083 unsigned int buflen
, int write_data
)
3085 unsigned int words
= buflen
>> 1;
3087 /* Transfer multiple of 2 bytes */
3089 outsw(ap
->ioaddr
.data_addr
, buf
, words
);
3091 insw(ap
->ioaddr
.data_addr
, buf
, words
);
3093 /* Transfer trailing 1 byte, if any. */
3094 if (unlikely(buflen
& 0x01)) {
3095 u16 align_buf
[1] = { 0 };
3096 unsigned char *trailing_buf
= buf
+ buflen
- 1;
3099 memcpy(align_buf
, trailing_buf
, 1);
3100 outw(le16_to_cpu(align_buf
[0]), ap
->ioaddr
.data_addr
);
3102 align_buf
[0] = cpu_to_le16(inw(ap
->ioaddr
.data_addr
));
3103 memcpy(trailing_buf
, align_buf
, 1);
3109 * ata_data_xfer - Transfer data from/to the data register.
3110 * @ap: port to read/write
3112 * @buflen: buffer length
3113 * @do_write: read/write
3115 * Transfer data from/to the device data register.
3118 * Inherited from caller.
3121 static void ata_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
3122 unsigned int buflen
, int do_write
)
3124 /* Make the crap hardware pay the costs not the good stuff */
3125 if (unlikely(ap
->flags
& ATA_FLAG_IRQ_MASK
)) {
3126 unsigned long flags
;
3127 local_irq_save(flags
);
3128 if (ap
->flags
& ATA_FLAG_MMIO
)
3129 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
3131 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
3132 local_irq_restore(flags
);
3134 if (ap
->flags
& ATA_FLAG_MMIO
)
3135 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
3137 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
3142 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3143 * @qc: Command on going
3145 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3148 * Inherited from caller.
3151 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
3153 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3154 struct scatterlist
*sg
= qc
->__sg
;
3155 struct ata_port
*ap
= qc
->ap
;
3157 unsigned int offset
;
3160 if (qc
->cursect
== (qc
->nsect
- 1))
3161 ap
->hsm_task_state
= HSM_ST_LAST
;
3163 page
= sg
[qc
->cursg
].page
;
3164 offset
= sg
[qc
->cursg
].offset
+ qc
->cursg_ofs
* ATA_SECT_SIZE
;
3166 /* get the current page and offset */
3167 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
3168 offset
%= PAGE_SIZE
;
3170 buf
= kmap(page
) + offset
;
3175 if ((qc
->cursg_ofs
* ATA_SECT_SIZE
) == (&sg
[qc
->cursg
])->length
) {
3180 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
3182 /* do the actual data transfer */
3183 do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3184 ata_data_xfer(ap
, buf
, ATA_SECT_SIZE
, do_write
);
3190 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3191 * @qc: Command on going
3192 * @bytes: number of bytes
3194 * Transfer Transfer data from/to the ATAPI device.
3197 * Inherited from caller.
3201 static void __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
3203 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3204 struct scatterlist
*sg
= qc
->__sg
;
3205 struct ata_port
*ap
= qc
->ap
;
3208 unsigned int offset
, count
;
3210 if (qc
->curbytes
+ bytes
>= qc
->nbytes
)
3211 ap
->hsm_task_state
= HSM_ST_LAST
;
3214 if (unlikely(qc
->cursg
>= qc
->n_elem
)) {
3216 * The end of qc->sg is reached and the device expects
3217 * more data to transfer. In order not to overrun qc->sg
3218 * and fulfill length specified in the byte count register,
3219 * - for read case, discard trailing data from the device
3220 * - for write case, padding zero data to the device
3222 u16 pad_buf
[1] = { 0 };
3223 unsigned int words
= bytes
>> 1;
3226 if (words
) /* warning if bytes > 1 */
3227 printk(KERN_WARNING
"ata%u: %u bytes trailing data\n",
3230 for (i
= 0; i
< words
; i
++)
3231 ata_data_xfer(ap
, (unsigned char*)pad_buf
, 2, do_write
);
3233 ap
->hsm_task_state
= HSM_ST_LAST
;
3237 sg
= &qc
->__sg
[qc
->cursg
];
3240 offset
= sg
->offset
+ qc
->cursg_ofs
;
3242 /* get the current page and offset */
3243 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
3244 offset
%= PAGE_SIZE
;
3246 /* don't overrun current sg */
3247 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
3249 /* don't cross page boundaries */
3250 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
3252 buf
= kmap(page
) + offset
;
3255 qc
->curbytes
+= count
;
3256 qc
->cursg_ofs
+= count
;
3258 if (qc
->cursg_ofs
== sg
->length
) {
3263 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
3265 /* do the actual data transfer */
3266 ata_data_xfer(ap
, buf
, count
, do_write
);
3275 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3276 * @qc: Command on going
3278 * Transfer Transfer data from/to the ATAPI device.
3281 * Inherited from caller.
3284 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
3286 struct ata_port
*ap
= qc
->ap
;
3287 struct ata_device
*dev
= qc
->dev
;
3288 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
3289 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
3291 ap
->ops
->tf_read(ap
, &qc
->tf
);
3292 ireason
= qc
->tf
.nsect
;
3293 bc_lo
= qc
->tf
.lbam
;
3294 bc_hi
= qc
->tf
.lbah
;
3295 bytes
= (bc_hi
<< 8) | bc_lo
;
3297 /* shall be cleared to zero, indicating xfer of data */
3298 if (ireason
& (1 << 0))
3301 /* make sure transfer direction matches expected */
3302 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
3303 if (do_write
!= i_write
)
3306 __atapi_pio_bytes(qc
, bytes
);
3311 printk(KERN_INFO
"ata%u: dev %u: ATAPI check failed\n",
3312 ap
->id
, dev
->devno
);
3313 qc
->err_mask
|= AC_ERR_HSM
;
3314 ap
->hsm_task_state
= HSM_ST_ERR
;
3318 * ata_pio_block - start PIO on a block
3319 * @ap: the target ata_port
3322 * None. (executing in kernel thread context)
3325 static void ata_pio_block(struct ata_port
*ap
)
3327 struct ata_queued_cmd
*qc
;
3331 * This is purely heuristic. This is a fast path.
3332 * Sometimes when we enter, BSY will be cleared in
3333 * a chk-status or two. If not, the drive is probably seeking
3334 * or something. Snooze for a couple msecs, then
3335 * chk-status again. If still busy, fall back to
3336 * HSM_ST_POLL state.
3338 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
3339 if (status
& ATA_BUSY
) {
3341 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
3342 if (status
& ATA_BUSY
) {
3343 ap
->hsm_task_state
= HSM_ST_POLL
;
3344 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
3349 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3353 if (status
& (ATA_ERR
| ATA_DF
)) {
3354 qc
->err_mask
|= AC_ERR_DEV
;
3355 ap
->hsm_task_state
= HSM_ST_ERR
;
3359 /* transfer data if any */
3360 if (is_atapi_taskfile(&qc
->tf
)) {
3361 /* DRQ=0 means no more data to transfer */
3362 if ((status
& ATA_DRQ
) == 0) {
3363 ap
->hsm_task_state
= HSM_ST_LAST
;
3367 atapi_pio_bytes(qc
);
3369 /* handle BSY=0, DRQ=0 as error */
3370 if ((status
& ATA_DRQ
) == 0) {
3371 qc
->err_mask
|= AC_ERR_HSM
;
3372 ap
->hsm_task_state
= HSM_ST_ERR
;
3380 static void ata_pio_error(struct ata_port
*ap
)
3382 struct ata_queued_cmd
*qc
;
3384 printk(KERN_WARNING
"ata%u: PIO error\n", ap
->id
);
3386 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3389 /* make sure qc->err_mask is available to
3390 * know what's wrong and recover
3392 assert(qc
->err_mask
);
3394 ap
->hsm_task_state
= HSM_ST_IDLE
;
3396 ata_poll_qc_complete(qc
);
3399 static void ata_pio_task(void *_data
)
3401 struct ata_port
*ap
= _data
;
3402 unsigned long timeout
;
3409 switch (ap
->hsm_task_state
) {
3418 qc_completed
= ata_pio_complete(ap
);
3422 case HSM_ST_LAST_POLL
:
3423 timeout
= ata_pio_poll(ap
);
3433 ata_queue_delayed_pio_task(ap
, timeout
);
3434 else if (!qc_completed
)
3439 * ata_qc_timeout - Handle timeout of queued command
3440 * @qc: Command that timed out
3442 * Some part of the kernel (currently, only the SCSI layer)
3443 * has noticed that the active command on port @ap has not
3444 * completed after a specified length of time. Handle this
3445 * condition by disabling DMA (if necessary) and completing
3446 * transactions, with error if necessary.
3448 * This also handles the case of the "lost interrupt", where
3449 * for some reason (possibly hardware bug, possibly driver bug)
3450 * an interrupt was not delivered to the driver, even though the
3451 * transaction completed successfully.
3454 * Inherited from SCSI layer (none, can sleep)
3457 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
3459 struct ata_port
*ap
= qc
->ap
;
3460 struct ata_host_set
*host_set
= ap
->host_set
;
3461 u8 host_stat
= 0, drv_stat
;
3462 unsigned long flags
;
3466 spin_lock_irqsave(&host_set
->lock
, flags
);
3468 switch (qc
->tf
.protocol
) {
3471 case ATA_PROT_ATAPI_DMA
:
3472 host_stat
= ap
->ops
->bmdma_status(ap
);
3474 /* before we do anything else, clear DMA-Start bit */
3475 ap
->ops
->bmdma_stop(qc
);
3481 drv_stat
= ata_chk_status(ap
);
3483 /* ack bmdma irq events */
3484 ap
->ops
->irq_clear(ap
);
3486 printk(KERN_ERR
"ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3487 ap
->id
, qc
->tf
.command
, drv_stat
, host_stat
);
3489 /* complete taskfile transaction */
3490 qc
->err_mask
|= ac_err_mask(drv_stat
);
3494 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3496 ata_eh_qc_complete(qc
);
3502 * ata_eng_timeout - Handle timeout of queued command
3503 * @ap: Port on which timed-out command is active
3505 * Some part of the kernel (currently, only the SCSI layer)
3506 * has noticed that the active command on port @ap has not
3507 * completed after a specified length of time. Handle this
3508 * condition by disabling DMA (if necessary) and completing
3509 * transactions, with error if necessary.
3511 * This also handles the case of the "lost interrupt", where
3512 * for some reason (possibly hardware bug, possibly driver bug)
3513 * an interrupt was not delivered to the driver, even though the
3514 * transaction completed successfully.
3517 * Inherited from SCSI layer (none, can sleep)
3520 void ata_eng_timeout(struct ata_port
*ap
)
3522 struct ata_queued_cmd
*qc
;
3526 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3530 printk(KERN_ERR
"ata%u: BUG: timeout without command\n",
3540 * ata_qc_new - Request an available ATA command, for queueing
3541 * @ap: Port associated with device @dev
3542 * @dev: Device from whom we request an available command structure
3548 static struct ata_queued_cmd
*ata_qc_new(struct ata_port
*ap
)
3550 struct ata_queued_cmd
*qc
= NULL
;
3553 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++)
3554 if (!test_and_set_bit(i
, &ap
->qactive
)) {
3555 qc
= ata_qc_from_tag(ap
, i
);
3566 * ata_qc_new_init - Request an available ATA command, and initialize it
3567 * @ap: Port associated with device @dev
3568 * @dev: Device from whom we request an available command structure
3574 struct ata_queued_cmd
*ata_qc_new_init(struct ata_port
*ap
,
3575 struct ata_device
*dev
)
3577 struct ata_queued_cmd
*qc
;
3579 qc
= ata_qc_new(ap
);
3592 * ata_qc_free - free unused ata_queued_cmd
3593 * @qc: Command to complete
3595 * Designed to free unused ata_queued_cmd object
3596 * in case something prevents using it.
3599 * spin_lock_irqsave(host_set lock)
3601 void ata_qc_free(struct ata_queued_cmd
*qc
)
3603 struct ata_port
*ap
= qc
->ap
;
3606 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3610 if (likely(ata_tag_valid(tag
))) {
3611 if (tag
== ap
->active_tag
)
3612 ap
->active_tag
= ATA_TAG_POISON
;
3613 qc
->tag
= ATA_TAG_POISON
;
3614 clear_bit(tag
, &ap
->qactive
);
3619 * ata_qc_complete - Complete an active ATA command
3620 * @qc: Command to complete
3621 * @err_mask: ATA Status register contents
3623 * Indicate to the mid and upper layers that an ATA
3624 * command has completed, with either an ok or not-ok status.
3627 * spin_lock_irqsave(host_set lock)
3630 void ata_qc_complete(struct ata_queued_cmd
*qc
)
3632 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3633 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
3635 if (likely(qc
->flags
& ATA_QCFLAG_DMAMAP
))
3638 /* atapi: mark qc as inactive to prevent the interrupt handler
3639 * from completing the command twice later, before the error handler
3640 * is called. (when rc != 0 and atapi request sense is needed)
3642 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
3644 /* call completion callback */
3645 qc
->complete_fn(qc
);
3648 static inline int ata_should_dma_map(struct ata_queued_cmd
*qc
)
3650 struct ata_port
*ap
= qc
->ap
;
3652 switch (qc
->tf
.protocol
) {
3654 case ATA_PROT_ATAPI_DMA
:
3657 case ATA_PROT_ATAPI
:
3659 case ATA_PROT_PIO_MULT
:
3660 if (ap
->flags
& ATA_FLAG_PIO_DMA
)
3673 * ata_qc_issue - issue taskfile to device
3674 * @qc: command to issue to device
3676 * Prepare an ATA command to submission to device.
3677 * This includes mapping the data into a DMA-able
3678 * area, filling in the S/G table, and finally
3679 * writing the taskfile to hardware, starting the command.
3682 * spin_lock_irqsave(host_set lock)
3685 * Zero on success, AC_ERR_* mask on failure
3688 unsigned int ata_qc_issue(struct ata_queued_cmd
*qc
)
3690 struct ata_port
*ap
= qc
->ap
;
3692 if (ata_should_dma_map(qc
)) {
3693 if (qc
->flags
& ATA_QCFLAG_SG
) {
3694 if (ata_sg_setup(qc
))
3696 } else if (qc
->flags
& ATA_QCFLAG_SINGLE
) {
3697 if (ata_sg_setup_one(qc
))
3701 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
3704 ap
->ops
->qc_prep(qc
);
3706 qc
->ap
->active_tag
= qc
->tag
;
3707 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
3709 return ap
->ops
->qc_issue(qc
);
3712 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
3713 return AC_ERR_SYSTEM
;
3718 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3719 * @qc: command to issue to device
3721 * Using various libata functions and hooks, this function
3722 * starts an ATA command. ATA commands are grouped into
3723 * classes called "protocols", and issuing each type of protocol
3724 * is slightly different.
3726 * May be used as the qc_issue() entry in ata_port_operations.
3729 * spin_lock_irqsave(host_set lock)
3732 * Zero on success, AC_ERR_* mask on failure
3735 unsigned int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
3737 struct ata_port
*ap
= qc
->ap
;
3739 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
3741 switch (qc
->tf
.protocol
) {
3742 case ATA_PROT_NODATA
:
3743 ata_tf_to_host(ap
, &qc
->tf
);
3747 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3748 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3749 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3752 case ATA_PROT_PIO
: /* load tf registers, initiate polling pio */
3753 ata_qc_set_polling(qc
);
3754 ata_tf_to_host(ap
, &qc
->tf
);
3755 ap
->hsm_task_state
= HSM_ST
;
3756 ata_queue_pio_task(ap
);
3759 case ATA_PROT_ATAPI
:
3760 ata_qc_set_polling(qc
);
3761 ata_tf_to_host(ap
, &qc
->tf
);
3762 ata_queue_packet_task(ap
);
3765 case ATA_PROT_ATAPI_NODATA
:
3766 ap
->flags
|= ATA_FLAG_NOINTR
;
3767 ata_tf_to_host(ap
, &qc
->tf
);
3768 ata_queue_packet_task(ap
);
3771 case ATA_PROT_ATAPI_DMA
:
3772 ap
->flags
|= ATA_FLAG_NOINTR
;
3773 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3774 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3775 ata_queue_packet_task(ap
);
3780 return AC_ERR_SYSTEM
;
3787 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3788 * @qc: Info associated with this ATA transaction.
3791 * spin_lock_irqsave(host_set lock)
3794 static void ata_bmdma_setup_mmio (struct ata_queued_cmd
*qc
)
3796 struct ata_port
*ap
= qc
->ap
;
3797 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3799 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3801 /* load PRD table addr. */
3802 mb(); /* make sure PRD table writes are visible to controller */
3803 writel(ap
->prd_dma
, mmio
+ ATA_DMA_TABLE_OFS
);
3805 /* specify data direction, triple-check start bit is clear */
3806 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3807 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3809 dmactl
|= ATA_DMA_WR
;
3810 writeb(dmactl
, mmio
+ ATA_DMA_CMD
);
3812 /* issue r/w command */
3813 ap
->ops
->exec_command(ap
, &qc
->tf
);
3817 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3818 * @qc: Info associated with this ATA transaction.
3821 * spin_lock_irqsave(host_set lock)
3824 static void ata_bmdma_start_mmio (struct ata_queued_cmd
*qc
)
3826 struct ata_port
*ap
= qc
->ap
;
3827 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3830 /* start host DMA transaction */
3831 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3832 writeb(dmactl
| ATA_DMA_START
, mmio
+ ATA_DMA_CMD
);
3834 /* Strictly, one may wish to issue a readb() here, to
3835 * flush the mmio write. However, control also passes
3836 * to the hardware at this point, and it will interrupt
3837 * us when we are to resume control. So, in effect,
3838 * we don't care when the mmio write flushes.
3839 * Further, a read of the DMA status register _immediately_
3840 * following the write may not be what certain flaky hardware
3841 * is expected, so I think it is best to not add a readb()
3842 * without first all the MMIO ATA cards/mobos.
3843 * Or maybe I'm just being paranoid.
3848 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3849 * @qc: Info associated with this ATA transaction.
3852 * spin_lock_irqsave(host_set lock)
3855 static void ata_bmdma_setup_pio (struct ata_queued_cmd
*qc
)
3857 struct ata_port
*ap
= qc
->ap
;
3858 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3861 /* load PRD table addr. */
3862 outl(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
3864 /* specify data direction, triple-check start bit is clear */
3865 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3866 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3868 dmactl
|= ATA_DMA_WR
;
3869 outb(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3871 /* issue r/w command */
3872 ap
->ops
->exec_command(ap
, &qc
->tf
);
3876 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3877 * @qc: Info associated with this ATA transaction.
3880 * spin_lock_irqsave(host_set lock)
3883 static void ata_bmdma_start_pio (struct ata_queued_cmd
*qc
)
3885 struct ata_port
*ap
= qc
->ap
;
3888 /* start host DMA transaction */
3889 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3890 outb(dmactl
| ATA_DMA_START
,
3891 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3896 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3897 * @qc: Info associated with this ATA transaction.
3899 * Writes the ATA_DMA_START flag to the DMA command register.
3901 * May be used as the bmdma_start() entry in ata_port_operations.
3904 * spin_lock_irqsave(host_set lock)
3906 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
3908 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3909 ata_bmdma_start_mmio(qc
);
3911 ata_bmdma_start_pio(qc
);
3916 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3917 * @qc: Info associated with this ATA transaction.
3919 * Writes address of PRD table to device's PRD Table Address
3920 * register, sets the DMA control register, and calls
3921 * ops->exec_command() to start the transfer.
3923 * May be used as the bmdma_setup() entry in ata_port_operations.
3926 * spin_lock_irqsave(host_set lock)
3928 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
3930 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3931 ata_bmdma_setup_mmio(qc
);
3933 ata_bmdma_setup_pio(qc
);
3938 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3939 * @ap: Port associated with this ATA transaction.
3941 * Clear interrupt and error flags in DMA status register.
3943 * May be used as the irq_clear() entry in ata_port_operations.
3946 * spin_lock_irqsave(host_set lock)
3949 void ata_bmdma_irq_clear(struct ata_port
*ap
)
3951 if (ap
->flags
& ATA_FLAG_MMIO
) {
3952 void __iomem
*mmio
= ((void __iomem
*) ap
->ioaddr
.bmdma_addr
) + ATA_DMA_STATUS
;
3953 writeb(readb(mmio
), mmio
);
3955 unsigned long addr
= ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
;
3956 outb(inb(addr
), addr
);
3963 * ata_bmdma_status - Read PCI IDE BMDMA status
3964 * @ap: Port associated with this ATA transaction.
3966 * Read and return BMDMA status register.
3968 * May be used as the bmdma_status() entry in ata_port_operations.
3971 * spin_lock_irqsave(host_set lock)
3974 u8
ata_bmdma_status(struct ata_port
*ap
)
3977 if (ap
->flags
& ATA_FLAG_MMIO
) {
3978 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3979 host_stat
= readb(mmio
+ ATA_DMA_STATUS
);
3981 host_stat
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
3987 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3988 * @qc: Command we are ending DMA for
3990 * Clears the ATA_DMA_START flag in the dma control register
3992 * May be used as the bmdma_stop() entry in ata_port_operations.
3995 * spin_lock_irqsave(host_set lock)
3998 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
4000 struct ata_port
*ap
= qc
->ap
;
4001 if (ap
->flags
& ATA_FLAG_MMIO
) {
4002 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
4004 /* clear start/stop bit */
4005 writeb(readb(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
4006 mmio
+ ATA_DMA_CMD
);
4008 /* clear start/stop bit */
4009 outb(inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
4010 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
4013 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4014 ata_altstatus(ap
); /* dummy read */
4018 * ata_host_intr - Handle host interrupt for given (port, task)
4019 * @ap: Port on which interrupt arrived (possibly...)
4020 * @qc: Taskfile currently active in engine
4022 * Handle host interrupt for given queued command. Currently,
4023 * only DMA interrupts are handled. All other commands are
4024 * handled via polling with interrupts disabled (nIEN bit).
4027 * spin_lock_irqsave(host_set lock)
4030 * One if interrupt was handled, zero if not (shared irq).
4033 inline unsigned int ata_host_intr (struct ata_port
*ap
,
4034 struct ata_queued_cmd
*qc
)
4036 u8 status
, host_stat
;
4038 switch (qc
->tf
.protocol
) {
4041 case ATA_PROT_ATAPI_DMA
:
4042 case ATA_PROT_ATAPI
:
4043 /* check status of DMA engine */
4044 host_stat
= ap
->ops
->bmdma_status(ap
);
4045 VPRINTK("ata%u: host_stat 0x%X\n", ap
->id
, host_stat
);
4047 /* if it's not our irq... */
4048 if (!(host_stat
& ATA_DMA_INTR
))
4051 /* before we do anything else, clear DMA-Start bit */
4052 ap
->ops
->bmdma_stop(qc
);
4056 case ATA_PROT_ATAPI_NODATA
:
4057 case ATA_PROT_NODATA
:
4058 /* check altstatus */
4059 status
= ata_altstatus(ap
);
4060 if (status
& ATA_BUSY
)
4063 /* check main status, clearing INTRQ */
4064 status
= ata_chk_status(ap
);
4065 if (unlikely(status
& ATA_BUSY
))
4067 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4068 ap
->id
, qc
->tf
.protocol
, status
);
4070 /* ack bmdma irq events */
4071 ap
->ops
->irq_clear(ap
);
4073 /* complete taskfile transaction */
4074 qc
->err_mask
|= ac_err_mask(status
);
4075 ata_qc_complete(qc
);
4082 return 1; /* irq handled */
4085 ap
->stats
.idle_irq
++;
4088 if ((ap
->stats
.idle_irq
% 1000) == 0) {
4090 ata_irq_ack(ap
, 0); /* debug trap */
4091 printk(KERN_WARNING
"ata%d: irq trap\n", ap
->id
);
4094 return 0; /* irq not handled */
4098 * ata_interrupt - Default ATA host interrupt handler
4099 * @irq: irq line (unused)
4100 * @dev_instance: pointer to our ata_host_set information structure
4103 * Default interrupt handler for PCI IDE devices. Calls
4104 * ata_host_intr() for each port that is not disabled.
4107 * Obtains host_set lock during operation.
4110 * IRQ_NONE or IRQ_HANDLED.
4113 irqreturn_t
ata_interrupt (int irq
, void *dev_instance
, struct pt_regs
*regs
)
4115 struct ata_host_set
*host_set
= dev_instance
;
4117 unsigned int handled
= 0;
4118 unsigned long flags
;
4120 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4121 spin_lock_irqsave(&host_set
->lock
, flags
);
4123 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4124 struct ata_port
*ap
;
4126 ap
= host_set
->ports
[i
];
4128 !(ap
->flags
& (ATA_FLAG_PORT_DISABLED
| ATA_FLAG_NOINTR
))) {
4129 struct ata_queued_cmd
*qc
;
4131 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
4132 if (qc
&& (!(qc
->tf
.ctl
& ATA_NIEN
)) &&
4133 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
4134 handled
|= ata_host_intr(ap
, qc
);
4138 spin_unlock_irqrestore(&host_set
->lock
, flags
);
4140 return IRQ_RETVAL(handled
);
4144 * atapi_packet_task - Write CDB bytes to hardware
4145 * @_data: Port to which ATAPI device is attached.
4147 * When device has indicated its readiness to accept
4148 * a CDB, this function is called. Send the CDB.
4149 * If DMA is to be performed, exit immediately.
4150 * Otherwise, we are in polling mode, so poll
4151 * status under operation succeeds or fails.
4154 * Kernel thread context (may sleep)
4157 static void atapi_packet_task(void *_data
)
4159 struct ata_port
*ap
= _data
;
4160 struct ata_queued_cmd
*qc
;
4163 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
4165 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
4167 /* sleep-wait for BSY to clear */
4168 DPRINTK("busy wait\n");
4169 if (ata_busy_sleep(ap
, ATA_TMOUT_CDB_QUICK
, ATA_TMOUT_CDB
)) {
4170 qc
->err_mask
|= AC_ERR_TIMEOUT
;
4174 /* make sure DRQ is set */
4175 status
= ata_chk_status(ap
);
4176 if ((status
& (ATA_BUSY
| ATA_DRQ
)) != ATA_DRQ
) {
4177 qc
->err_mask
|= AC_ERR_HSM
;
4182 DPRINTK("send cdb\n");
4183 assert(ap
->cdb_len
>= 12);
4185 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
||
4186 qc
->tf
.protocol
== ATA_PROT_ATAPI_NODATA
) {
4187 unsigned long flags
;
4189 /* Once we're done issuing command and kicking bmdma,
4190 * irq handler takes over. To not lose irq, we need
4191 * to clear NOINTR flag before sending cdb, but
4192 * interrupt handler shouldn't be invoked before we're
4193 * finished. Hence, the following locking.
4195 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
4196 ap
->flags
&= ~ATA_FLAG_NOINTR
;
4197 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
4198 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
)
4199 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
4200 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
4202 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
4204 /* PIO commands are handled by polling */
4205 ap
->hsm_task_state
= HSM_ST
;
4206 ata_queue_pio_task(ap
);
4212 ata_poll_qc_complete(qc
);
4217 * ata_port_start - Set port up for dma.
4218 * @ap: Port to initialize
4220 * Called just after data structures for each port are
4221 * initialized. Allocates space for PRD table.
4223 * May be used as the port_start() entry in ata_port_operations.
4226 * Inherited from caller.
4230 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4231 * without filling any other registers
4233 static int ata_do_simple_cmd(struct ata_port
*ap
, struct ata_device
*dev
,
4236 struct ata_taskfile tf
;
4239 ata_tf_init(ap
, &tf
, dev
->devno
);
4242 tf
.flags
|= ATA_TFLAG_DEVICE
;
4243 tf
.protocol
= ATA_PROT_NODATA
;
4245 err
= ata_exec_internal(ap
, dev
, &tf
, DMA_NONE
, NULL
, 0);
4247 printk(KERN_ERR
"%s: ata command failed: %d\n",
4253 static int ata_flush_cache(struct ata_port
*ap
, struct ata_device
*dev
)
4257 if (!ata_try_flush_cache(dev
))
4260 if (ata_id_has_flush_ext(dev
->id
))
4261 cmd
= ATA_CMD_FLUSH_EXT
;
4263 cmd
= ATA_CMD_FLUSH
;
4265 return ata_do_simple_cmd(ap
, dev
, cmd
);
4268 static int ata_standby_drive(struct ata_port
*ap
, struct ata_device
*dev
)
4270 return ata_do_simple_cmd(ap
, dev
, ATA_CMD_STANDBYNOW1
);
4273 static int ata_start_drive(struct ata_port
*ap
, struct ata_device
*dev
)
4275 return ata_do_simple_cmd(ap
, dev
, ATA_CMD_IDLEIMMEDIATE
);
4279 * ata_device_resume - wakeup a previously suspended devices
4281 * Kick the drive back into action, by sending it an idle immediate
4282 * command and making sure its transfer mode matches between drive
4286 int ata_device_resume(struct ata_port
*ap
, struct ata_device
*dev
)
4288 if (ap
->flags
& ATA_FLAG_SUSPENDED
) {
4289 ap
->flags
&= ~ATA_FLAG_SUSPENDED
;
4292 if (!ata_dev_present(dev
))
4294 if (dev
->class == ATA_DEV_ATA
)
4295 ata_start_drive(ap
, dev
);
4301 * ata_device_suspend - prepare a device for suspend
4303 * Flush the cache on the drive, if appropriate, then issue a
4304 * standbynow command.
4307 int ata_device_suspend(struct ata_port
*ap
, struct ata_device
*dev
)
4309 if (!ata_dev_present(dev
))
4311 if (dev
->class == ATA_DEV_ATA
)
4312 ata_flush_cache(ap
, dev
);
4314 ata_standby_drive(ap
, dev
);
4315 ap
->flags
|= ATA_FLAG_SUSPENDED
;
4319 int ata_port_start (struct ata_port
*ap
)
4321 struct device
*dev
= ap
->host_set
->dev
;
4324 ap
->prd
= dma_alloc_coherent(dev
, ATA_PRD_TBL_SZ
, &ap
->prd_dma
, GFP_KERNEL
);
4328 rc
= ata_pad_alloc(ap
, dev
);
4330 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
4334 DPRINTK("prd alloc, virt %p, dma %llx\n", ap
->prd
, (unsigned long long) ap
->prd_dma
);
4341 * ata_port_stop - Undo ata_port_start()
4342 * @ap: Port to shut down
4344 * Frees the PRD table.
4346 * May be used as the port_stop() entry in ata_port_operations.
4349 * Inherited from caller.
4352 void ata_port_stop (struct ata_port
*ap
)
4354 struct device
*dev
= ap
->host_set
->dev
;
4356 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
4357 ata_pad_free(ap
, dev
);
4360 void ata_host_stop (struct ata_host_set
*host_set
)
4362 if (host_set
->mmio_base
)
4363 iounmap(host_set
->mmio_base
);
4368 * ata_host_remove - Unregister SCSI host structure with upper layers
4369 * @ap: Port to unregister
4370 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4373 * Inherited from caller.
4376 static void ata_host_remove(struct ata_port
*ap
, unsigned int do_unregister
)
4378 struct Scsi_Host
*sh
= ap
->host
;
4383 scsi_remove_host(sh
);
4385 ap
->ops
->port_stop(ap
);
4389 * ata_host_init - Initialize an ata_port structure
4390 * @ap: Structure to initialize
4391 * @host: associated SCSI mid-layer structure
4392 * @host_set: Collection of hosts to which @ap belongs
4393 * @ent: Probe information provided by low-level driver
4394 * @port_no: Port number associated with this ata_port
4396 * Initialize a new ata_port structure, and its associated
4400 * Inherited from caller.
4403 static void ata_host_init(struct ata_port
*ap
, struct Scsi_Host
*host
,
4404 struct ata_host_set
*host_set
,
4405 const struct ata_probe_ent
*ent
, unsigned int port_no
)
4411 host
->max_channel
= 1;
4412 host
->unique_id
= ata_unique_id
++;
4413 host
->max_cmd_len
= 12;
4415 ap
->flags
= ATA_FLAG_PORT_DISABLED
;
4416 ap
->id
= host
->unique_id
;
4418 ap
->ctl
= ATA_DEVCTL_OBS
;
4419 ap
->host_set
= host_set
;
4420 ap
->port_no
= port_no
;
4422 ent
->legacy_mode
? ent
->hard_port_no
: port_no
;
4423 ap
->pio_mask
= ent
->pio_mask
;
4424 ap
->mwdma_mask
= ent
->mwdma_mask
;
4425 ap
->udma_mask
= ent
->udma_mask
;
4426 ap
->flags
|= ent
->host_flags
;
4427 ap
->ops
= ent
->port_ops
;
4428 ap
->cbl
= ATA_CBL_NONE
;
4429 ap
->active_tag
= ATA_TAG_POISON
;
4430 ap
->last_ctl
= 0xFF;
4432 INIT_WORK(&ap
->packet_task
, atapi_packet_task
, ap
);
4433 INIT_WORK(&ap
->pio_task
, ata_pio_task
, ap
);
4434 INIT_LIST_HEAD(&ap
->eh_done_q
);
4436 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
4437 ap
->device
[i
].devno
= i
;
4440 ap
->stats
.unhandled_irq
= 1;
4441 ap
->stats
.idle_irq
= 1;
4444 memcpy(&ap
->ioaddr
, &ent
->port
[port_no
], sizeof(struct ata_ioports
));
4448 * ata_host_add - Attach low-level ATA driver to system
4449 * @ent: Information provided by low-level driver
4450 * @host_set: Collections of ports to which we add
4451 * @port_no: Port number associated with this host
4453 * Attach low-level ATA driver to system.
4456 * PCI/etc. bus probe sem.
4459 * New ata_port on success, for NULL on error.
4462 static struct ata_port
* ata_host_add(const struct ata_probe_ent
*ent
,
4463 struct ata_host_set
*host_set
,
4464 unsigned int port_no
)
4466 struct Scsi_Host
*host
;
4467 struct ata_port
*ap
;
4471 host
= scsi_host_alloc(ent
->sht
, sizeof(struct ata_port
));
4475 ap
= (struct ata_port
*) &host
->hostdata
[0];
4477 ata_host_init(ap
, host
, host_set
, ent
, port_no
);
4479 rc
= ap
->ops
->port_start(ap
);
4486 scsi_host_put(host
);
4491 * ata_device_add - Register hardware device with ATA and SCSI layers
4492 * @ent: Probe information describing hardware device to be registered
4494 * This function processes the information provided in the probe
4495 * information struct @ent, allocates the necessary ATA and SCSI
4496 * host information structures, initializes them, and registers
4497 * everything with requisite kernel subsystems.
4499 * This function requests irqs, probes the ATA bus, and probes
4503 * PCI/etc. bus probe sem.
4506 * Number of ports registered. Zero on error (no ports registered).
4509 int ata_device_add(const struct ata_probe_ent
*ent
)
4511 unsigned int count
= 0, i
;
4512 struct device
*dev
= ent
->dev
;
4513 struct ata_host_set
*host_set
;
4516 /* alloc a container for our list of ATA ports (buses) */
4517 host_set
= kzalloc(sizeof(struct ata_host_set
) +
4518 (ent
->n_ports
* sizeof(void *)), GFP_KERNEL
);
4521 spin_lock_init(&host_set
->lock
);
4523 host_set
->dev
= dev
;
4524 host_set
->n_ports
= ent
->n_ports
;
4525 host_set
->irq
= ent
->irq
;
4526 host_set
->mmio_base
= ent
->mmio_base
;
4527 host_set
->private_data
= ent
->private_data
;
4528 host_set
->ops
= ent
->port_ops
;
4530 /* register each port bound to this device */
4531 for (i
= 0; i
< ent
->n_ports
; i
++) {
4532 struct ata_port
*ap
;
4533 unsigned long xfer_mode_mask
;
4535 ap
= ata_host_add(ent
, host_set
, i
);
4539 host_set
->ports
[i
] = ap
;
4540 xfer_mode_mask
=(ap
->udma_mask
<< ATA_SHIFT_UDMA
) |
4541 (ap
->mwdma_mask
<< ATA_SHIFT_MWDMA
) |
4542 (ap
->pio_mask
<< ATA_SHIFT_PIO
);
4544 /* print per-port info to dmesg */
4545 printk(KERN_INFO
"ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4546 "bmdma 0x%lX irq %lu\n",
4548 ap
->flags
& ATA_FLAG_SATA
? 'S' : 'P',
4549 ata_mode_string(xfer_mode_mask
),
4550 ap
->ioaddr
.cmd_addr
,
4551 ap
->ioaddr
.ctl_addr
,
4552 ap
->ioaddr
.bmdma_addr
,
4556 host_set
->ops
->irq_clear(ap
);
4563 /* obtain irq, that is shared between channels */
4564 if (request_irq(ent
->irq
, ent
->port_ops
->irq_handler
, ent
->irq_flags
,
4565 DRV_NAME
, host_set
))
4568 /* perform each probe synchronously */
4569 DPRINTK("probe begin\n");
4570 for (i
= 0; i
< count
; i
++) {
4571 struct ata_port
*ap
;
4574 ap
= host_set
->ports
[i
];
4576 DPRINTK("ata%u: probe begin\n", ap
->id
);
4577 rc
= ata_bus_probe(ap
);
4578 DPRINTK("ata%u: probe end\n", ap
->id
);
4581 /* FIXME: do something useful here?
4582 * Current libata behavior will
4583 * tear down everything when
4584 * the module is removed
4585 * or the h/w is unplugged.
4589 rc
= scsi_add_host(ap
->host
, dev
);
4591 printk(KERN_ERR
"ata%u: scsi_add_host failed\n",
4593 /* FIXME: do something useful here */
4594 /* FIXME: handle unconditional calls to
4595 * scsi_scan_host and ata_host_remove, below,
4601 /* probes are done, now scan each port's disk(s) */
4602 DPRINTK("probe begin\n");
4603 for (i
= 0; i
< count
; i
++) {
4604 struct ata_port
*ap
= host_set
->ports
[i
];
4606 ata_scsi_scan_host(ap
);
4609 dev_set_drvdata(dev
, host_set
);
4611 VPRINTK("EXIT, returning %u\n", ent
->n_ports
);
4612 return ent
->n_ports
; /* success */
4615 for (i
= 0; i
< count
; i
++) {
4616 ata_host_remove(host_set
->ports
[i
], 1);
4617 scsi_host_put(host_set
->ports
[i
]->host
);
4621 VPRINTK("EXIT, returning 0\n");
4626 * ata_host_set_remove - PCI layer callback for device removal
4627 * @host_set: ATA host set that was removed
4629 * Unregister all objects associated with this host set. Free those
4633 * Inherited from calling layer (may sleep).
4636 void ata_host_set_remove(struct ata_host_set
*host_set
)
4638 struct ata_port
*ap
;
4641 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4642 ap
= host_set
->ports
[i
];
4643 scsi_remove_host(ap
->host
);
4646 free_irq(host_set
->irq
, host_set
);
4648 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4649 ap
= host_set
->ports
[i
];
4651 ata_scsi_release(ap
->host
);
4653 if ((ap
->flags
& ATA_FLAG_NO_LEGACY
) == 0) {
4654 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
4656 if (ioaddr
->cmd_addr
== 0x1f0)
4657 release_region(0x1f0, 8);
4658 else if (ioaddr
->cmd_addr
== 0x170)
4659 release_region(0x170, 8);
4662 scsi_host_put(ap
->host
);
4665 if (host_set
->ops
->host_stop
)
4666 host_set
->ops
->host_stop(host_set
);
4672 * ata_scsi_release - SCSI layer callback hook for host unload
4673 * @host: libata host to be unloaded
4675 * Performs all duties necessary to shut down a libata port...
4676 * Kill port kthread, disable port, and release resources.
4679 * Inherited from SCSI layer.
4685 int ata_scsi_release(struct Scsi_Host
*host
)
4687 struct ata_port
*ap
= (struct ata_port
*) &host
->hostdata
[0];
4691 ap
->ops
->port_disable(ap
);
4692 ata_host_remove(ap
, 0);
4699 * ata_std_ports - initialize ioaddr with standard port offsets.
4700 * @ioaddr: IO address structure to be initialized
4702 * Utility function which initializes data_addr, error_addr,
4703 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4704 * device_addr, status_addr, and command_addr to standard offsets
4705 * relative to cmd_addr.
4707 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4710 void ata_std_ports(struct ata_ioports
*ioaddr
)
4712 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
4713 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
4714 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
4715 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
4716 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
4717 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
4718 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
4719 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
4720 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
4721 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
4724 static struct ata_probe_ent
*
4725 ata_probe_ent_alloc(struct device
*dev
, const struct ata_port_info
*port
)
4727 struct ata_probe_ent
*probe_ent
;
4729 probe_ent
= kzalloc(sizeof(*probe_ent
), GFP_KERNEL
);
4731 printk(KERN_ERR DRV_NAME
"(%s): out of memory\n",
4732 kobject_name(&(dev
->kobj
)));
4736 INIT_LIST_HEAD(&probe_ent
->node
);
4737 probe_ent
->dev
= dev
;
4739 probe_ent
->sht
= port
->sht
;
4740 probe_ent
->host_flags
= port
->host_flags
;
4741 probe_ent
->pio_mask
= port
->pio_mask
;
4742 probe_ent
->mwdma_mask
= port
->mwdma_mask
;
4743 probe_ent
->udma_mask
= port
->udma_mask
;
4744 probe_ent
->port_ops
= port
->port_ops
;
4753 void ata_pci_host_stop (struct ata_host_set
*host_set
)
4755 struct pci_dev
*pdev
= to_pci_dev(host_set
->dev
);
4757 pci_iounmap(pdev
, host_set
->mmio_base
);
4761 * ata_pci_init_native_mode - Initialize native-mode driver
4762 * @pdev: pci device to be initialized
4763 * @port: array[2] of pointers to port info structures.
4764 * @ports: bitmap of ports present
4766 * Utility function which allocates and initializes an
4767 * ata_probe_ent structure for a standard dual-port
4768 * PIO-based IDE controller. The returned ata_probe_ent
4769 * structure can be passed to ata_device_add(). The returned
4770 * ata_probe_ent structure should then be freed with kfree().
4772 * The caller need only pass the address of the primary port, the
4773 * secondary will be deduced automatically. If the device has non
4774 * standard secondary port mappings this function can be called twice,
4775 * once for each interface.
4778 struct ata_probe_ent
*
4779 ata_pci_init_native_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
, int ports
)
4781 struct ata_probe_ent
*probe_ent
=
4782 ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
4788 probe_ent
->irq
= pdev
->irq
;
4789 probe_ent
->irq_flags
= SA_SHIRQ
;
4790 probe_ent
->private_data
= port
[0]->private_data
;
4792 if (ports
& ATA_PORT_PRIMARY
) {
4793 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 0);
4794 probe_ent
->port
[p
].altstatus_addr
=
4795 probe_ent
->port
[p
].ctl_addr
=
4796 pci_resource_start(pdev
, 1) | ATA_PCI_CTL_OFS
;
4797 probe_ent
->port
[p
].bmdma_addr
= pci_resource_start(pdev
, 4);
4798 ata_std_ports(&probe_ent
->port
[p
]);
4802 if (ports
& ATA_PORT_SECONDARY
) {
4803 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 2);
4804 probe_ent
->port
[p
].altstatus_addr
=
4805 probe_ent
->port
[p
].ctl_addr
=
4806 pci_resource_start(pdev
, 3) | ATA_PCI_CTL_OFS
;
4807 probe_ent
->port
[p
].bmdma_addr
= pci_resource_start(pdev
, 4) + 8;
4808 ata_std_ports(&probe_ent
->port
[p
]);
4812 probe_ent
->n_ports
= p
;
4816 static struct ata_probe_ent
*ata_pci_init_legacy_port(struct pci_dev
*pdev
, struct ata_port_info
*port
, int port_num
)
4818 struct ata_probe_ent
*probe_ent
;
4820 probe_ent
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
);
4824 probe_ent
->legacy_mode
= 1;
4825 probe_ent
->n_ports
= 1;
4826 probe_ent
->hard_port_no
= port_num
;
4827 probe_ent
->private_data
= port
->private_data
;
4832 probe_ent
->irq
= 14;
4833 probe_ent
->port
[0].cmd_addr
= 0x1f0;
4834 probe_ent
->port
[0].altstatus_addr
=
4835 probe_ent
->port
[0].ctl_addr
= 0x3f6;
4838 probe_ent
->irq
= 15;
4839 probe_ent
->port
[0].cmd_addr
= 0x170;
4840 probe_ent
->port
[0].altstatus_addr
=
4841 probe_ent
->port
[0].ctl_addr
= 0x376;
4844 probe_ent
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4) + 8 * port_num
;
4845 ata_std_ports(&probe_ent
->port
[0]);
4850 * ata_pci_init_one - Initialize/register PCI IDE host controller
4851 * @pdev: Controller to be initialized
4852 * @port_info: Information from low-level host driver
4853 * @n_ports: Number of ports attached to host controller
4855 * This is a helper function which can be called from a driver's
4856 * xxx_init_one() probe function if the hardware uses traditional
4857 * IDE taskfile registers.
4859 * This function calls pci_enable_device(), reserves its register
4860 * regions, sets the dma mask, enables bus master mode, and calls
4864 * Inherited from PCI layer (may sleep).
4867 * Zero on success, negative on errno-based value on error.
4870 int ata_pci_init_one (struct pci_dev
*pdev
, struct ata_port_info
**port_info
,
4871 unsigned int n_ports
)
4873 struct ata_probe_ent
*probe_ent
= NULL
, *probe_ent2
= NULL
;
4874 struct ata_port_info
*port
[2];
4876 unsigned int legacy_mode
= 0;
4877 int disable_dev_on_err
= 1;
4882 port
[0] = port_info
[0];
4884 port
[1] = port_info
[1];
4888 if ((port
[0]->host_flags
& ATA_FLAG_NO_LEGACY
) == 0
4889 && (pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
4890 /* TODO: What if one channel is in native mode ... */
4891 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
4892 mask
= (1 << 2) | (1 << 0);
4893 if ((tmp8
& mask
) != mask
)
4894 legacy_mode
= (1 << 3);
4898 if ((!legacy_mode
) && (n_ports
> 2)) {
4899 printk(KERN_ERR
"ata: BUG: native mode, n_ports > 2\n");
4904 /* FIXME: Really for ATA it isn't safe because the device may be
4905 multi-purpose and we want to leave it alone if it was already
4906 enabled. Secondly for shared use as Arjan says we want refcounting
4908 Checking dev->is_enabled is insufficient as this is not set at
4909 boot for the primary video which is BIOS enabled
4912 rc
= pci_enable_device(pdev
);
4916 rc
= pci_request_regions(pdev
, DRV_NAME
);
4918 disable_dev_on_err
= 0;
4922 /* FIXME: Should use platform specific mappers for legacy port ranges */
4924 if (!request_region(0x1f0, 8, "libata")) {
4925 struct resource
*conflict
, res
;
4927 res
.end
= 0x1f0 + 8 - 1;
4928 conflict
= ____request_resource(&ioport_resource
, &res
);
4929 if (!strcmp(conflict
->name
, "libata"))
4930 legacy_mode
|= (1 << 0);
4932 disable_dev_on_err
= 0;
4933 printk(KERN_WARNING
"ata: 0x1f0 IDE port busy\n");
4936 legacy_mode
|= (1 << 0);
4938 if (!request_region(0x170, 8, "libata")) {
4939 struct resource
*conflict
, res
;
4941 res
.end
= 0x170 + 8 - 1;
4942 conflict
= ____request_resource(&ioport_resource
, &res
);
4943 if (!strcmp(conflict
->name
, "libata"))
4944 legacy_mode
|= (1 << 1);
4946 disable_dev_on_err
= 0;
4947 printk(KERN_WARNING
"ata: 0x170 IDE port busy\n");
4950 legacy_mode
|= (1 << 1);
4953 /* we have legacy mode, but all ports are unavailable */
4954 if (legacy_mode
== (1 << 3)) {
4956 goto err_out_regions
;
4959 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
4961 goto err_out_regions
;
4962 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
4964 goto err_out_regions
;
4967 if (legacy_mode
& (1 << 0))
4968 probe_ent
= ata_pci_init_legacy_port(pdev
, port
[0], 0);
4969 if (legacy_mode
& (1 << 1))
4970 probe_ent2
= ata_pci_init_legacy_port(pdev
, port
[1], 1);
4973 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
| ATA_PORT_SECONDARY
);
4975 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
);
4977 if (!probe_ent
&& !probe_ent2
) {
4979 goto err_out_regions
;
4982 pci_set_master(pdev
);
4984 /* FIXME: check ata_device_add return */
4986 if (legacy_mode
& (1 << 0))
4987 ata_device_add(probe_ent
);
4988 if (legacy_mode
& (1 << 1))
4989 ata_device_add(probe_ent2
);
4991 ata_device_add(probe_ent
);
4999 if (legacy_mode
& (1 << 0))
5000 release_region(0x1f0, 8);
5001 if (legacy_mode
& (1 << 1))
5002 release_region(0x170, 8);
5003 pci_release_regions(pdev
);
5005 if (disable_dev_on_err
)
5006 pci_disable_device(pdev
);
5011 * ata_pci_remove_one - PCI layer callback for device removal
5012 * @pdev: PCI device that was removed
5014 * PCI layer indicates to libata via this hook that
5015 * hot-unplug or module unload event has occurred.
5016 * Handle this by unregistering all objects associated
5017 * with this PCI device. Free those objects. Then finally
5018 * release PCI resources and disable device.
5021 * Inherited from PCI layer (may sleep).
5024 void ata_pci_remove_one (struct pci_dev
*pdev
)
5026 struct device
*dev
= pci_dev_to_dev(pdev
);
5027 struct ata_host_set
*host_set
= dev_get_drvdata(dev
);
5029 ata_host_set_remove(host_set
);
5030 pci_release_regions(pdev
);
5031 pci_disable_device(pdev
);
5032 dev_set_drvdata(dev
, NULL
);
5035 /* move to PCI subsystem */
5036 int pci_test_config_bits(struct pci_dev
*pdev
, const struct pci_bits
*bits
)
5038 unsigned long tmp
= 0;
5040 switch (bits
->width
) {
5043 pci_read_config_byte(pdev
, bits
->reg
, &tmp8
);
5049 pci_read_config_word(pdev
, bits
->reg
, &tmp16
);
5055 pci_read_config_dword(pdev
, bits
->reg
, &tmp32
);
5066 return (tmp
== bits
->val
) ? 1 : 0;
5069 int ata_pci_device_suspend(struct pci_dev
*pdev
, pm_message_t state
)
5071 pci_save_state(pdev
);
5072 pci_disable_device(pdev
);
5073 pci_set_power_state(pdev
, PCI_D3hot
);
5077 int ata_pci_device_resume(struct pci_dev
*pdev
)
5079 pci_set_power_state(pdev
, PCI_D0
);
5080 pci_restore_state(pdev
);
5081 pci_enable_device(pdev
);
5082 pci_set_master(pdev
);
5085 #endif /* CONFIG_PCI */
5088 static int __init
ata_init(void)
5090 ata_wq
= create_workqueue("ata");
5094 printk(KERN_DEBUG
"libata version " DRV_VERSION
" loaded.\n");
5098 static void __exit
ata_exit(void)
5100 destroy_workqueue(ata_wq
);
5103 module_init(ata_init
);
5104 module_exit(ata_exit
);
5106 static unsigned long ratelimit_time
;
5107 static spinlock_t ata_ratelimit_lock
= SPIN_LOCK_UNLOCKED
;
5109 int ata_ratelimit(void)
5112 unsigned long flags
;
5114 spin_lock_irqsave(&ata_ratelimit_lock
, flags
);
5116 if (time_after(jiffies
, ratelimit_time
)) {
5118 ratelimit_time
= jiffies
+ (HZ
/5);
5122 spin_unlock_irqrestore(&ata_ratelimit_lock
, flags
);
5128 * libata is essentially a library of internal helper functions for
5129 * low-level ATA host controller drivers. As such, the API/ABI is
5130 * likely to change as new drivers are added and updated.
5131 * Do not depend on ABI/API stability.
5134 EXPORT_SYMBOL_GPL(ata_std_bios_param
);
5135 EXPORT_SYMBOL_GPL(ata_std_ports
);
5136 EXPORT_SYMBOL_GPL(ata_device_add
);
5137 EXPORT_SYMBOL_GPL(ata_host_set_remove
);
5138 EXPORT_SYMBOL_GPL(ata_sg_init
);
5139 EXPORT_SYMBOL_GPL(ata_sg_init_one
);
5140 EXPORT_SYMBOL_GPL(ata_qc_complete
);
5141 EXPORT_SYMBOL_GPL(ata_qc_issue_prot
);
5142 EXPORT_SYMBOL_GPL(ata_eng_timeout
);
5143 EXPORT_SYMBOL_GPL(ata_tf_load
);
5144 EXPORT_SYMBOL_GPL(ata_tf_read
);
5145 EXPORT_SYMBOL_GPL(ata_noop_dev_select
);
5146 EXPORT_SYMBOL_GPL(ata_std_dev_select
);
5147 EXPORT_SYMBOL_GPL(ata_tf_to_fis
);
5148 EXPORT_SYMBOL_GPL(ata_tf_from_fis
);
5149 EXPORT_SYMBOL_GPL(ata_check_status
);
5150 EXPORT_SYMBOL_GPL(ata_altstatus
);
5151 EXPORT_SYMBOL_GPL(ata_exec_command
);
5152 EXPORT_SYMBOL_GPL(ata_port_start
);
5153 EXPORT_SYMBOL_GPL(ata_port_stop
);
5154 EXPORT_SYMBOL_GPL(ata_host_stop
);
5155 EXPORT_SYMBOL_GPL(ata_interrupt
);
5156 EXPORT_SYMBOL_GPL(ata_qc_prep
);
5157 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
5158 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
5159 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear
);
5160 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
5161 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
5162 EXPORT_SYMBOL_GPL(ata_port_probe
);
5163 EXPORT_SYMBOL_GPL(sata_phy_reset
);
5164 EXPORT_SYMBOL_GPL(__sata_phy_reset
);
5165 EXPORT_SYMBOL_GPL(ata_bus_reset
);
5166 EXPORT_SYMBOL_GPL(ata_port_disable
);
5167 EXPORT_SYMBOL_GPL(ata_ratelimit
);
5168 EXPORT_SYMBOL_GPL(ata_busy_sleep
);
5169 EXPORT_SYMBOL_GPL(ata_scsi_ioctl
);
5170 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd
);
5171 EXPORT_SYMBOL_GPL(ata_scsi_error
);
5172 EXPORT_SYMBOL_GPL(ata_scsi_slave_config
);
5173 EXPORT_SYMBOL_GPL(ata_scsi_release
);
5174 EXPORT_SYMBOL_GPL(ata_host_intr
);
5175 EXPORT_SYMBOL_GPL(ata_dev_classify
);
5176 EXPORT_SYMBOL_GPL(ata_dev_id_string
);
5177 EXPORT_SYMBOL_GPL(ata_dev_config
);
5178 EXPORT_SYMBOL_GPL(ata_scsi_simulate
);
5179 EXPORT_SYMBOL_GPL(ata_eh_qc_complete
);
5180 EXPORT_SYMBOL_GPL(ata_eh_qc_retry
);
5182 EXPORT_SYMBOL_GPL(ata_pio_need_iordy
);
5183 EXPORT_SYMBOL_GPL(ata_timing_compute
);
5184 EXPORT_SYMBOL_GPL(ata_timing_merge
);
5187 EXPORT_SYMBOL_GPL(pci_test_config_bits
);
5188 EXPORT_SYMBOL_GPL(ata_pci_host_stop
);
5189 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode
);
5190 EXPORT_SYMBOL_GPL(ata_pci_init_one
);
5191 EXPORT_SYMBOL_GPL(ata_pci_remove_one
);
5192 EXPORT_SYMBOL_GPL(ata_pci_device_suspend
);
5193 EXPORT_SYMBOL_GPL(ata_pci_device_resume
);
5194 #endif /* CONFIG_PCI */
5196 EXPORT_SYMBOL_GPL(ata_device_suspend
);
5197 EXPORT_SYMBOL_GPL(ata_device_resume
);
5198 EXPORT_SYMBOL_GPL(ata_scsi_device_suspend
);
5199 EXPORT_SYMBOL_GPL(ata_scsi_device_resume
);