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 <scsi/scsi.h>
53 #include "scsi_priv.h"
54 #include <scsi/scsi_host.h>
55 #include <linux/libata.h>
57 #include <asm/semaphore.h>
58 #include <asm/byteorder.h>
62 static unsigned int ata_busy_sleep (struct ata_port
*ap
,
63 unsigned long tmout_pat
,
65 static void ata_set_mode(struct ata_port
*ap
);
66 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
);
67 static unsigned int ata_get_mode_mask(struct ata_port
*ap
, int shift
);
68 static int fgb(u32 bitmap
);
69 static int ata_choose_xfer_mode(struct ata_port
*ap
,
71 unsigned int *xfer_shift_out
);
72 static int ata_qc_complete_noop(struct ata_queued_cmd
*qc
, u8 drv_stat
);
73 static void __ata_qc_complete(struct ata_queued_cmd
*qc
);
75 static unsigned int ata_unique_id
= 1;
76 static struct workqueue_struct
*ata_wq
;
78 MODULE_AUTHOR("Jeff Garzik");
79 MODULE_DESCRIPTION("Library module for ATA devices");
80 MODULE_LICENSE("GPL");
81 MODULE_VERSION(DRV_VERSION
);
84 * ata_tf_load - send taskfile registers to host controller
85 * @ap: Port to which output is sent
86 * @tf: ATA taskfile register set
88 * Outputs ATA taskfile to standard ATA host controller.
91 * Inherited from caller.
94 static void ata_tf_load_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
96 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
97 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
99 if (tf
->ctl
!= ap
->last_ctl
) {
100 outb(tf
->ctl
, ioaddr
->ctl_addr
);
101 ap
->last_ctl
= tf
->ctl
;
105 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
106 outb(tf
->hob_feature
, ioaddr
->feature_addr
);
107 outb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
108 outb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
109 outb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
110 outb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
111 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
120 outb(tf
->feature
, ioaddr
->feature_addr
);
121 outb(tf
->nsect
, ioaddr
->nsect_addr
);
122 outb(tf
->lbal
, ioaddr
->lbal_addr
);
123 outb(tf
->lbam
, ioaddr
->lbam_addr
);
124 outb(tf
->lbah
, ioaddr
->lbah_addr
);
125 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
133 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
134 outb(tf
->device
, ioaddr
->device_addr
);
135 VPRINTK("device 0x%X\n", tf
->device
);
142 * ata_tf_load_mmio - send taskfile registers to host controller
143 * @ap: Port to which output is sent
144 * @tf: ATA taskfile register set
146 * Outputs ATA taskfile to standard ATA host controller using MMIO.
149 * Inherited from caller.
152 static void ata_tf_load_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
154 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
155 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
157 if (tf
->ctl
!= ap
->last_ctl
) {
158 writeb(tf
->ctl
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
159 ap
->last_ctl
= tf
->ctl
;
163 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
164 writeb(tf
->hob_feature
, (void __iomem
*) ioaddr
->feature_addr
);
165 writeb(tf
->hob_nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
166 writeb(tf
->hob_lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
167 writeb(tf
->hob_lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
168 writeb(tf
->hob_lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
169 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
178 writeb(tf
->feature
, (void __iomem
*) ioaddr
->feature_addr
);
179 writeb(tf
->nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
180 writeb(tf
->lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
181 writeb(tf
->lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
182 writeb(tf
->lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
183 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
191 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
192 writeb(tf
->device
, (void __iomem
*) ioaddr
->device_addr
);
193 VPRINTK("device 0x%X\n", tf
->device
);
201 * ata_tf_load - send taskfile registers to host controller
202 * @ap: Port to which output is sent
203 * @tf: ATA taskfile register set
205 * Outputs ATA taskfile to standard ATA host controller using MMIO
206 * or PIO as indicated by the ATA_FLAG_MMIO flag.
207 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
208 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
209 * hob_lbal, hob_lbam, and hob_lbah.
211 * This function waits for idle (!BUSY and !DRQ) after writing
212 * registers. If the control register has a new value, this
213 * function also waits for idle after writing control and before
214 * writing the remaining registers.
216 * May be used as the tf_load() entry in ata_port_operations.
219 * Inherited from caller.
221 void ata_tf_load(struct ata_port
*ap
, struct ata_taskfile
*tf
)
223 if (ap
->flags
& ATA_FLAG_MMIO
)
224 ata_tf_load_mmio(ap
, tf
);
226 ata_tf_load_pio(ap
, tf
);
230 * ata_exec_command_pio - issue ATA command to host controller
231 * @ap: port to which command is being issued
232 * @tf: ATA taskfile register set
234 * Issues PIO write to ATA command register, with proper
235 * synchronization with interrupt handler / other threads.
238 * spin_lock_irqsave(host_set lock)
241 static void ata_exec_command_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
243 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
245 outb(tf
->command
, ap
->ioaddr
.command_addr
);
251 * ata_exec_command_mmio - issue ATA command to host controller
252 * @ap: port to which command is being issued
253 * @tf: ATA taskfile register set
255 * Issues MMIO write to ATA command register, with proper
256 * synchronization with interrupt handler / other threads.
259 * spin_lock_irqsave(host_set lock)
262 static void ata_exec_command_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
264 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
266 writeb(tf
->command
, (void __iomem
*) ap
->ioaddr
.command_addr
);
272 * ata_exec_command - issue ATA command to host controller
273 * @ap: port to which command is being issued
274 * @tf: ATA taskfile register set
276 * Issues PIO/MMIO write to ATA command register, with proper
277 * synchronization with interrupt handler / other threads.
280 * spin_lock_irqsave(host_set lock)
282 void ata_exec_command(struct ata_port
*ap
, struct ata_taskfile
*tf
)
284 if (ap
->flags
& ATA_FLAG_MMIO
)
285 ata_exec_command_mmio(ap
, tf
);
287 ata_exec_command_pio(ap
, tf
);
291 * ata_exec - issue ATA command to host controller
292 * @ap: port to which command is being issued
293 * @tf: ATA taskfile register set
295 * Issues PIO/MMIO write to ATA command register, with proper
296 * synchronization with interrupt handler / other threads.
299 * Obtains host_set lock.
302 static inline void ata_exec(struct ata_port
*ap
, struct ata_taskfile
*tf
)
306 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
307 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
308 ap
->ops
->exec_command(ap
, tf
);
309 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
313 * ata_tf_to_host - issue ATA taskfile to host controller
314 * @ap: port to which command is being issued
315 * @tf: ATA taskfile register set
317 * Issues ATA taskfile register set to ATA host controller,
318 * with proper synchronization with interrupt handler and
322 * Obtains host_set lock.
325 static void ata_tf_to_host(struct ata_port
*ap
, struct ata_taskfile
*tf
)
327 ap
->ops
->tf_load(ap
, tf
);
333 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
334 * @ap: port to which command is being issued
335 * @tf: ATA taskfile register set
337 * Issues ATA taskfile register set to ATA host controller,
338 * with proper synchronization with interrupt handler and
342 * spin_lock_irqsave(host_set lock)
345 void ata_tf_to_host_nolock(struct ata_port
*ap
, struct ata_taskfile
*tf
)
347 ap
->ops
->tf_load(ap
, tf
);
348 ap
->ops
->exec_command(ap
, tf
);
352 * ata_tf_read_pio - input device's ATA taskfile shadow registers
353 * @ap: Port from which input is read
354 * @tf: ATA taskfile register set for storing input
356 * Reads ATA taskfile registers for currently-selected device
360 * Inherited from caller.
363 static void ata_tf_read_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
365 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
367 tf
->nsect
= inb(ioaddr
->nsect_addr
);
368 tf
->lbal
= inb(ioaddr
->lbal_addr
);
369 tf
->lbam
= inb(ioaddr
->lbam_addr
);
370 tf
->lbah
= inb(ioaddr
->lbah_addr
);
371 tf
->device
= inb(ioaddr
->device_addr
);
373 if (tf
->flags
& ATA_TFLAG_LBA48
) {
374 outb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
375 tf
->hob_feature
= inb(ioaddr
->error_addr
);
376 tf
->hob_nsect
= inb(ioaddr
->nsect_addr
);
377 tf
->hob_lbal
= inb(ioaddr
->lbal_addr
);
378 tf
->hob_lbam
= inb(ioaddr
->lbam_addr
);
379 tf
->hob_lbah
= inb(ioaddr
->lbah_addr
);
384 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
385 * @ap: Port from which input is read
386 * @tf: ATA taskfile register set for storing input
388 * Reads ATA taskfile registers for currently-selected device
392 * Inherited from caller.
395 static void ata_tf_read_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
397 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
399 tf
->nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
400 tf
->lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
401 tf
->lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
402 tf
->lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
403 tf
->device
= readb((void __iomem
*)ioaddr
->device_addr
);
405 if (tf
->flags
& ATA_TFLAG_LBA48
) {
406 writeb(tf
->ctl
| ATA_HOB
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
407 tf
->hob_feature
= readb((void __iomem
*)ioaddr
->error_addr
);
408 tf
->hob_nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
409 tf
->hob_lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
410 tf
->hob_lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
411 tf
->hob_lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
417 * ata_tf_read - input device's ATA taskfile shadow registers
418 * @ap: Port from which input is read
419 * @tf: ATA taskfile register set for storing input
421 * Reads ATA taskfile registers for currently-selected device
424 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
425 * is set, also reads the hob registers.
427 * May be used as the tf_read() entry in ata_port_operations.
430 * Inherited from caller.
432 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
434 if (ap
->flags
& ATA_FLAG_MMIO
)
435 ata_tf_read_mmio(ap
, tf
);
437 ata_tf_read_pio(ap
, tf
);
441 * ata_check_status_pio - Read device status reg & clear interrupt
442 * @ap: port where the device is
444 * Reads ATA taskfile status register for currently-selected device
445 * and return its value. This also clears pending interrupts
449 * Inherited from caller.
451 static u8
ata_check_status_pio(struct ata_port
*ap
)
453 return inb(ap
->ioaddr
.status_addr
);
457 * ata_check_status_mmio - Read device status reg & clear interrupt
458 * @ap: port where the device is
460 * Reads ATA taskfile status register for currently-selected device
461 * via MMIO and return its value. This also clears pending interrupts
465 * Inherited from caller.
467 static u8
ata_check_status_mmio(struct ata_port
*ap
)
469 return readb((void __iomem
*) ap
->ioaddr
.status_addr
);
474 * ata_check_status - Read device status reg & clear interrupt
475 * @ap: port where the device is
477 * Reads ATA taskfile status register for currently-selected device
478 * and return its value. This also clears pending interrupts
481 * May be used as the check_status() entry in ata_port_operations.
484 * Inherited from caller.
486 u8
ata_check_status(struct ata_port
*ap
)
488 if (ap
->flags
& ATA_FLAG_MMIO
)
489 return ata_check_status_mmio(ap
);
490 return ata_check_status_pio(ap
);
495 * ata_altstatus - Read device alternate status reg
496 * @ap: port where the device is
498 * Reads ATA taskfile alternate status register for
499 * currently-selected device and return its value.
501 * Note: may NOT be used as the check_altstatus() entry in
502 * ata_port_operations.
505 * Inherited from caller.
507 u8
ata_altstatus(struct ata_port
*ap
)
509 if (ap
->ops
->check_altstatus
)
510 return ap
->ops
->check_altstatus(ap
);
512 if (ap
->flags
& ATA_FLAG_MMIO
)
513 return readb((void __iomem
*)ap
->ioaddr
.altstatus_addr
);
514 return inb(ap
->ioaddr
.altstatus_addr
);
519 * ata_chk_err - Read device error reg
520 * @ap: port where the device is
522 * Reads ATA taskfile error register for
523 * currently-selected device and return its value.
525 * Note: may NOT be used as the check_err() entry in
526 * ata_port_operations.
529 * Inherited from caller.
531 u8
ata_chk_err(struct ata_port
*ap
)
533 if (ap
->ops
->check_err
)
534 return ap
->ops
->check_err(ap
);
536 if (ap
->flags
& ATA_FLAG_MMIO
) {
537 return readb((void __iomem
*) ap
->ioaddr
.error_addr
);
539 return inb(ap
->ioaddr
.error_addr
);
543 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
544 * @tf: Taskfile to convert
545 * @fis: Buffer into which data will output
546 * @pmp: Port multiplier port
548 * Converts a standard ATA taskfile to a Serial ATA
549 * FIS structure (Register - Host to Device).
552 * Inherited from caller.
555 void ata_tf_to_fis(struct ata_taskfile
*tf
, u8
*fis
, u8 pmp
)
557 fis
[0] = 0x27; /* Register - Host to Device FIS */
558 fis
[1] = (pmp
& 0xf) | (1 << 7); /* Port multiplier number,
559 bit 7 indicates Command FIS */
560 fis
[2] = tf
->command
;
561 fis
[3] = tf
->feature
;
568 fis
[8] = tf
->hob_lbal
;
569 fis
[9] = tf
->hob_lbam
;
570 fis
[10] = tf
->hob_lbah
;
571 fis
[11] = tf
->hob_feature
;
574 fis
[13] = tf
->hob_nsect
;
585 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
586 * @fis: Buffer from which data will be input
587 * @tf: Taskfile to output
589 * Converts a standard ATA taskfile to a Serial ATA
590 * FIS structure (Register - Host to Device).
593 * Inherited from caller.
596 void ata_tf_from_fis(u8
*fis
, struct ata_taskfile
*tf
)
598 tf
->command
= fis
[2]; /* status */
599 tf
->feature
= fis
[3]; /* error */
606 tf
->hob_lbal
= fis
[8];
607 tf
->hob_lbam
= fis
[9];
608 tf
->hob_lbah
= fis
[10];
611 tf
->hob_nsect
= fis
[13];
615 * ata_prot_to_cmd - determine which read/write opcodes to use
616 * @protocol: ATA_PROT_xxx taskfile protocol
617 * @lba48: true is lba48 is present
619 * Given necessary input, determine which read/write commands
620 * to use to transfer data.
625 static int ata_prot_to_cmd(int protocol
, int lba48
)
627 int rcmd
= 0, wcmd
= 0;
632 rcmd
= ATA_CMD_PIO_READ_EXT
;
633 wcmd
= ATA_CMD_PIO_WRITE_EXT
;
635 rcmd
= ATA_CMD_PIO_READ
;
636 wcmd
= ATA_CMD_PIO_WRITE
;
642 rcmd
= ATA_CMD_READ_EXT
;
643 wcmd
= ATA_CMD_WRITE_EXT
;
646 wcmd
= ATA_CMD_WRITE
;
654 return rcmd
| (wcmd
<< 8);
658 * ata_dev_set_protocol - set taskfile protocol and r/w commands
659 * @dev: device to examine and configure
661 * Examine the device configuration, after we have
662 * read the identify-device page and configured the
663 * data transfer mode. Set internal state related to
664 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
665 * and calculate the proper read/write commands to use.
670 static void ata_dev_set_protocol(struct ata_device
*dev
)
672 int pio
= (dev
->flags
& ATA_DFLAG_PIO
);
673 int lba48
= (dev
->flags
& ATA_DFLAG_LBA48
);
677 proto
= dev
->xfer_protocol
= ATA_PROT_PIO
;
679 proto
= dev
->xfer_protocol
= ATA_PROT_DMA
;
681 cmd
= ata_prot_to_cmd(proto
, lba48
);
685 dev
->read_cmd
= cmd
& 0xff;
686 dev
->write_cmd
= (cmd
>> 8) & 0xff;
689 static const char * xfer_mode_str
[] = {
709 * ata_udma_string - convert UDMA bit offset to string
710 * @mask: mask of bits supported; only highest bit counts.
712 * Determine string which represents the highest speed
713 * (highest bit in @udma_mask).
719 * Constant C string representing highest speed listed in
720 * @udma_mask, or the constant C string "<n/a>".
723 static const char *ata_mode_string(unsigned int mask
)
727 for (i
= 7; i
>= 0; i
--)
730 for (i
= ATA_SHIFT_MWDMA
+ 2; i
>= ATA_SHIFT_MWDMA
; i
--)
733 for (i
= ATA_SHIFT_PIO
+ 4; i
>= ATA_SHIFT_PIO
; i
--)
740 return xfer_mode_str
[i
];
744 * ata_pio_devchk - PATA device presence detection
745 * @ap: ATA channel to examine
746 * @device: Device to examine (starting at zero)
748 * This technique was originally described in
749 * Hale Landis's ATADRVR (www.ata-atapi.com), and
750 * later found its way into the ATA/ATAPI spec.
752 * Write a pattern to the ATA shadow registers,
753 * and if a device is present, it will respond by
754 * correctly storing and echoing back the
755 * ATA shadow register contents.
761 static unsigned int ata_pio_devchk(struct ata_port
*ap
,
764 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
767 ap
->ops
->dev_select(ap
, device
);
769 outb(0x55, ioaddr
->nsect_addr
);
770 outb(0xaa, ioaddr
->lbal_addr
);
772 outb(0xaa, ioaddr
->nsect_addr
);
773 outb(0x55, ioaddr
->lbal_addr
);
775 outb(0x55, ioaddr
->nsect_addr
);
776 outb(0xaa, ioaddr
->lbal_addr
);
778 nsect
= inb(ioaddr
->nsect_addr
);
779 lbal
= inb(ioaddr
->lbal_addr
);
781 if ((nsect
== 0x55) && (lbal
== 0xaa))
782 return 1; /* we found a device */
784 return 0; /* nothing found */
788 * ata_mmio_devchk - PATA device presence detection
789 * @ap: ATA channel to examine
790 * @device: Device to examine (starting at zero)
792 * This technique was originally described in
793 * Hale Landis's ATADRVR (www.ata-atapi.com), and
794 * later found its way into the ATA/ATAPI spec.
796 * Write a pattern to the ATA shadow registers,
797 * and if a device is present, it will respond by
798 * correctly storing and echoing back the
799 * ATA shadow register contents.
805 static unsigned int ata_mmio_devchk(struct ata_port
*ap
,
808 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
811 ap
->ops
->dev_select(ap
, device
);
813 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
814 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
816 writeb(0xaa, (void __iomem
*) ioaddr
->nsect_addr
);
817 writeb(0x55, (void __iomem
*) ioaddr
->lbal_addr
);
819 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
820 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
822 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
823 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
825 if ((nsect
== 0x55) && (lbal
== 0xaa))
826 return 1; /* we found a device */
828 return 0; /* nothing found */
832 * ata_devchk - PATA device presence detection
833 * @ap: ATA channel to examine
834 * @device: Device to examine (starting at zero)
836 * Dispatch ATA device presence detection, depending
837 * on whether we are using PIO or MMIO to talk to the
838 * ATA shadow registers.
844 static unsigned int ata_devchk(struct ata_port
*ap
,
847 if (ap
->flags
& ATA_FLAG_MMIO
)
848 return ata_mmio_devchk(ap
, device
);
849 return ata_pio_devchk(ap
, device
);
853 * ata_dev_classify - determine device type based on ATA-spec signature
854 * @tf: ATA taskfile register set for device to be identified
856 * Determine from taskfile register contents whether a device is
857 * ATA or ATAPI, as per "Signature and persistence" section
858 * of ATA/PI spec (volume 1, sect 5.14).
864 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
865 * the event of failure.
868 unsigned int ata_dev_classify(struct ata_taskfile
*tf
)
870 /* Apple's open source Darwin code hints that some devices only
871 * put a proper signature into the LBA mid/high registers,
872 * So, we only check those. It's sufficient for uniqueness.
875 if (((tf
->lbam
== 0) && (tf
->lbah
== 0)) ||
876 ((tf
->lbam
== 0x3c) && (tf
->lbah
== 0xc3))) {
877 DPRINTK("found ATA device by sig\n");
881 if (((tf
->lbam
== 0x14) && (tf
->lbah
== 0xeb)) ||
882 ((tf
->lbam
== 0x69) && (tf
->lbah
== 0x96))) {
883 DPRINTK("found ATAPI device by sig\n");
884 return ATA_DEV_ATAPI
;
887 DPRINTK("unknown device\n");
888 return ATA_DEV_UNKNOWN
;
892 * ata_dev_try_classify - Parse returned ATA device signature
893 * @ap: ATA channel to examine
894 * @device: Device to examine (starting at zero)
896 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
897 * an ATA/ATAPI-defined set of values is placed in the ATA
898 * shadow registers, indicating the results of device detection
901 * Select the ATA device, and read the values from the ATA shadow
902 * registers. Then parse according to the Error register value,
903 * and the spec-defined values examined by ata_dev_classify().
909 static u8
ata_dev_try_classify(struct ata_port
*ap
, unsigned int device
)
911 struct ata_device
*dev
= &ap
->device
[device
];
912 struct ata_taskfile tf
;
916 ap
->ops
->dev_select(ap
, device
);
918 memset(&tf
, 0, sizeof(tf
));
920 err
= ata_chk_err(ap
);
921 ap
->ops
->tf_read(ap
, &tf
);
923 dev
->class = ATA_DEV_NONE
;
925 /* see if device passed diags */
928 else if ((device
== 0) && (err
== 0x81))
933 /* determine if device if ATA or ATAPI */
934 class = ata_dev_classify(&tf
);
935 if (class == ATA_DEV_UNKNOWN
)
937 if ((class == ATA_DEV_ATA
) && (ata_chk_status(ap
) == 0))
946 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
947 * @id: IDENTIFY DEVICE results we will examine
948 * @s: string into which data is output
949 * @ofs: offset into identify device page
950 * @len: length of string to return. must be an even number.
952 * The strings in the IDENTIFY DEVICE page are broken up into
953 * 16-bit chunks. Run through the string, and output each
954 * 8-bit chunk linearly, regardless of platform.
960 void ata_dev_id_string(u16
*id
, unsigned char *s
,
961 unsigned int ofs
, unsigned int len
)
981 * ata_noop_dev_select - Select device 0/1 on ATA bus
982 * @ap: ATA channel to manipulate
983 * @device: ATA device (numbered from zero) to select
985 * This function performs no actual function.
987 * May be used as the dev_select() entry in ata_port_operations.
992 void ata_noop_dev_select (struct ata_port
*ap
, unsigned int device
)
998 * ata_std_dev_select - Select device 0/1 on ATA bus
999 * @ap: ATA channel to manipulate
1000 * @device: ATA device (numbered from zero) to select
1002 * Use the method defined in the ATA specification to
1003 * make either device 0, or device 1, active on the
1004 * ATA channel. Works with both PIO and MMIO.
1006 * May be used as the dev_select() entry in ata_port_operations.
1012 void ata_std_dev_select (struct ata_port
*ap
, unsigned int device
)
1017 tmp
= ATA_DEVICE_OBS
;
1019 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
1021 if (ap
->flags
& ATA_FLAG_MMIO
) {
1022 writeb(tmp
, (void __iomem
*) ap
->ioaddr
.device_addr
);
1024 outb(tmp
, ap
->ioaddr
.device_addr
);
1026 ata_pause(ap
); /* needed; also flushes, for mmio */
1030 * ata_dev_select - Select device 0/1 on ATA bus
1031 * @ap: ATA channel to manipulate
1032 * @device: ATA device (numbered from zero) to select
1033 * @wait: non-zero to wait for Status register BSY bit to clear
1034 * @can_sleep: non-zero if context allows sleeping
1036 * Use the method defined in the ATA specification to
1037 * make either device 0, or device 1, active on the
1040 * This is a high-level version of ata_std_dev_select(),
1041 * which additionally provides the services of inserting
1042 * the proper pauses and status polling, where needed.
1048 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
1049 unsigned int wait
, unsigned int can_sleep
)
1051 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1052 ap
->id
, device
, wait
);
1057 ap
->ops
->dev_select(ap
, device
);
1060 if (can_sleep
&& ap
->device
[device
].class == ATA_DEV_ATAPI
)
1067 * ata_dump_id - IDENTIFY DEVICE info debugging output
1068 * @dev: Device whose IDENTIFY DEVICE page we will dump
1070 * Dump selected 16-bit words from a detected device's
1071 * IDENTIFY PAGE page.
1077 static inline void ata_dump_id(struct ata_device
*dev
)
1079 DPRINTK("49==0x%04x "
1089 DPRINTK("80==0x%04x "
1099 DPRINTK("88==0x%04x "
1106 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1107 * @ap: port on which device we wish to probe resides
1108 * @device: device bus address, starting at zero
1110 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1111 * command, and read back the 512-byte device information page.
1112 * The device information page is fed to us via the standard
1113 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1114 * using standard PIO-IN paths)
1116 * After reading the device information page, we use several
1117 * bits of information from it to initialize data structures
1118 * that will be used during the lifetime of the ata_device.
1119 * Other data from the info page is used to disqualify certain
1120 * older ATA devices we do not wish to support.
1123 * Inherited from caller. Some functions called by this function
1124 * obtain the host_set lock.
1127 static void ata_dev_identify(struct ata_port
*ap
, unsigned int device
)
1129 struct ata_device
*dev
= &ap
->device
[device
];
1132 unsigned long xfer_modes
;
1134 unsigned int using_edd
;
1135 DECLARE_COMPLETION(wait
);
1136 struct ata_queued_cmd
*qc
;
1137 unsigned long flags
;
1140 if (!ata_dev_present(dev
)) {
1141 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1146 if (ap
->flags
& (ATA_FLAG_SRST
| ATA_FLAG_SATA_RESET
))
1151 DPRINTK("ENTER, host %u, dev %u\n", ap
->id
, device
);
1153 assert (dev
->class == ATA_DEV_ATA
|| dev
->class == ATA_DEV_ATAPI
||
1154 dev
->class == ATA_DEV_NONE
);
1156 ata_dev_select(ap
, device
, 1, 1); /* select device 0/1 */
1158 qc
= ata_qc_new_init(ap
, dev
);
1161 ata_sg_init_one(qc
, dev
->id
, sizeof(dev
->id
));
1162 qc
->dma_dir
= DMA_FROM_DEVICE
;
1163 qc
->tf
.protocol
= ATA_PROT_PIO
;
1167 if (dev
->class == ATA_DEV_ATA
) {
1168 qc
->tf
.command
= ATA_CMD_ID_ATA
;
1169 DPRINTK("do ATA identify\n");
1171 qc
->tf
.command
= ATA_CMD_ID_ATAPI
;
1172 DPRINTK("do ATAPI identify\n");
1175 qc
->waiting
= &wait
;
1176 qc
->complete_fn
= ata_qc_complete_noop
;
1178 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1179 rc
= ata_qc_issue(qc
);
1180 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1185 wait_for_completion(&wait
);
1187 status
= ata_chk_status(ap
);
1188 if (status
& ATA_ERR
) {
1190 * arg! EDD works for all test cases, but seems to return
1191 * the ATA signature for some ATAPI devices. Until the
1192 * reason for this is found and fixed, we fix up the mess
1193 * here. If IDENTIFY DEVICE returns command aborted
1194 * (as ATAPI devices do), then we issue an
1195 * IDENTIFY PACKET DEVICE.
1197 * ATA software reset (SRST, the default) does not appear
1198 * to have this problem.
1200 if ((using_edd
) && (qc
->tf
.command
== ATA_CMD_ID_ATA
)) {
1201 u8 err
= ata_chk_err(ap
);
1202 if (err
& ATA_ABORTED
) {
1203 dev
->class = ATA_DEV_ATAPI
;
1214 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
1216 /* print device capabilities */
1217 printk(KERN_DEBUG
"ata%u: dev %u cfg "
1218 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1219 ap
->id
, device
, dev
->id
[49],
1220 dev
->id
[82], dev
->id
[83], dev
->id
[84],
1221 dev
->id
[85], dev
->id
[86], dev
->id
[87],
1225 * common ATA, ATAPI feature tests
1228 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1229 if (!ata_id_has_dma(dev
->id
) || !ata_id_has_lba(dev
->id
)) {
1230 printk(KERN_DEBUG
"ata%u: no dma/lba\n", ap
->id
);
1234 /* quick-n-dirty find max transfer mode; for printk only */
1235 xfer_modes
= dev
->id
[ATA_ID_UDMA_MODES
];
1237 xfer_modes
= (dev
->id
[ATA_ID_MWDMA_MODES
]) << ATA_SHIFT_MWDMA
;
1239 xfer_modes
= (dev
->id
[ATA_ID_PIO_MODES
]) << (ATA_SHIFT_PIO
+ 3);
1240 xfer_modes
|= (0x7 << ATA_SHIFT_PIO
);
1245 /* ATA-specific feature tests */
1246 if (dev
->class == ATA_DEV_ATA
) {
1247 if (!ata_id_is_ata(dev
->id
)) /* sanity check */
1250 tmp
= dev
->id
[ATA_ID_MAJOR_VER
];
1251 for (i
= 14; i
>= 1; i
--)
1255 /* we require at least ATA-3 */
1257 printk(KERN_DEBUG
"ata%u: no ATA-3\n", ap
->id
);
1261 if (ata_id_has_lba48(dev
->id
)) {
1262 dev
->flags
|= ATA_DFLAG_LBA48
;
1263 dev
->n_sectors
= ata_id_u64(dev
->id
, 100);
1265 dev
->n_sectors
= ata_id_u32(dev
->id
, 60);
1268 ap
->host
->max_cmd_len
= 16;
1270 /* print device info to dmesg */
1271 printk(KERN_INFO
"ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1273 ata_mode_string(xfer_modes
),
1274 (unsigned long long)dev
->n_sectors
,
1275 dev
->flags
& ATA_DFLAG_LBA48
? " lba48" : "");
1278 /* ATAPI-specific feature tests */
1280 if (ata_id_is_ata(dev
->id
)) /* sanity check */
1283 rc
= atapi_cdb_len(dev
->id
);
1284 if ((rc
< 12) || (rc
> ATAPI_CDB_LEN
)) {
1285 printk(KERN_WARNING
"ata%u: unsupported CDB len\n", ap
->id
);
1288 ap
->cdb_len
= (unsigned int) rc
;
1289 ap
->host
->max_cmd_len
= (unsigned char) ap
->cdb_len
;
1291 /* print device info to dmesg */
1292 printk(KERN_INFO
"ata%u: dev %u ATAPI, max %s\n",
1294 ata_mode_string(xfer_modes
));
1297 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap
));
1301 printk(KERN_WARNING
"ata%u: dev %u not supported, ignoring\n",
1304 dev
->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1305 DPRINTK("EXIT, err\n");
1309 static inline u8
ata_dev_knobble(struct ata_port
*ap
)
1311 return ((ap
->cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(ap
->device
->id
)));
1315 * ata_dev_config - Run device specific handlers and check for
1316 * SATA->PATA bridges
1323 void ata_dev_config(struct ata_port
*ap
, unsigned int i
)
1325 /* limit bridge transfers to udma5, 200 sectors */
1326 if (ata_dev_knobble(ap
)) {
1327 printk(KERN_INFO
"ata%u(%u): applying bridge limits\n",
1328 ap
->id
, ap
->device
->devno
);
1329 ap
->udma_mask
&= ATA_UDMA5
;
1330 ap
->host
->max_sectors
= ATA_MAX_SECTORS
;
1331 ap
->host
->hostt
->max_sectors
= ATA_MAX_SECTORS
;
1332 ap
->device
->flags
|= ATA_DFLAG_LOCK_SECTORS
;
1335 if (ap
->ops
->dev_config
)
1336 ap
->ops
->dev_config(ap
, &ap
->device
[i
]);
1340 * ata_bus_probe - Reset and probe ATA bus
1343 * Master ATA bus probing function. Initiates a hardware-dependent
1344 * bus reset, then attempts to identify any devices found on
1348 * PCI/etc. bus probe sem.
1351 * Zero on success, non-zero on error.
1354 static int ata_bus_probe(struct ata_port
*ap
)
1356 unsigned int i
, found
= 0;
1358 ap
->ops
->phy_reset(ap
);
1359 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1362 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1363 ata_dev_identify(ap
, i
);
1364 if (ata_dev_present(&ap
->device
[i
])) {
1366 ata_dev_config(ap
,i
);
1370 if ((!found
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1371 goto err_out_disable
;
1374 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1375 goto err_out_disable
;
1380 ap
->ops
->port_disable(ap
);
1386 * ata_port_probe - Mark port as enabled
1387 * @ap: Port for which we indicate enablement
1389 * Modify @ap data structure such that the system
1390 * thinks that the entire port is enabled.
1392 * LOCKING: host_set lock, or some other form of
1396 void ata_port_probe(struct ata_port
*ap
)
1398 ap
->flags
&= ~ATA_FLAG_PORT_DISABLED
;
1402 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1403 * @ap: SATA port associated with target SATA PHY.
1405 * This function issues commands to standard SATA Sxxx
1406 * PHY registers, to wake up the phy (and device), and
1407 * clear any reset condition.
1410 * PCI/etc. bus probe sem.
1413 void __sata_phy_reset(struct ata_port
*ap
)
1416 unsigned long timeout
= jiffies
+ (HZ
* 5);
1418 if (ap
->flags
& ATA_FLAG_SATA_RESET
) {
1419 /* issue phy wake/reset */
1420 scr_write_flush(ap
, SCR_CONTROL
, 0x301);
1421 /* Couldn't find anything in SATA I/II specs, but
1422 * AHCI-1.1 10.4.2 says at least 1 ms. */
1425 scr_write_flush(ap
, SCR_CONTROL
, 0x300); /* phy wake/clear reset */
1427 /* wait for phy to become ready, if necessary */
1430 sstatus
= scr_read(ap
, SCR_STATUS
);
1431 if ((sstatus
& 0xf) != 1)
1433 } while (time_before(jiffies
, timeout
));
1435 /* TODO: phy layer with polling, timeouts, etc. */
1436 if (sata_dev_present(ap
))
1439 sstatus
= scr_read(ap
, SCR_STATUS
);
1440 printk(KERN_INFO
"ata%u: no device found (phy stat %08x)\n",
1442 ata_port_disable(ap
);
1445 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1448 if (ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
)) {
1449 ata_port_disable(ap
);
1453 ap
->cbl
= ATA_CBL_SATA
;
1457 * sata_phy_reset - Reset SATA bus.
1458 * @ap: SATA port associated with target SATA PHY.
1460 * This function resets the SATA bus, and then probes
1461 * the bus for devices.
1464 * PCI/etc. bus probe sem.
1467 void sata_phy_reset(struct ata_port
*ap
)
1469 __sata_phy_reset(ap
);
1470 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1476 * ata_port_disable - Disable port.
1477 * @ap: Port to be disabled.
1479 * Modify @ap data structure such that the system
1480 * thinks that the entire port is disabled, and should
1481 * never attempt to probe or communicate with devices
1484 * LOCKING: host_set lock, or some other form of
1488 void ata_port_disable(struct ata_port
*ap
)
1490 ap
->device
[0].class = ATA_DEV_NONE
;
1491 ap
->device
[1].class = ATA_DEV_NONE
;
1492 ap
->flags
|= ATA_FLAG_PORT_DISABLED
;
1498 } xfer_mode_classes
[] = {
1499 { ATA_SHIFT_UDMA
, XFER_UDMA_0
},
1500 { ATA_SHIFT_MWDMA
, XFER_MW_DMA_0
},
1501 { ATA_SHIFT_PIO
, XFER_PIO_0
},
1504 static inline u8
base_from_shift(unsigned int shift
)
1508 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++)
1509 if (xfer_mode_classes
[i
].shift
== shift
)
1510 return xfer_mode_classes
[i
].base
;
1515 static void ata_dev_set_mode(struct ata_port
*ap
, struct ata_device
*dev
)
1520 if (!ata_dev_present(dev
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1523 if (dev
->xfer_shift
== ATA_SHIFT_PIO
)
1524 dev
->flags
|= ATA_DFLAG_PIO
;
1526 ata_dev_set_xfermode(ap
, dev
);
1528 base
= base_from_shift(dev
->xfer_shift
);
1529 ofs
= dev
->xfer_mode
- base
;
1530 idx
= ofs
+ dev
->xfer_shift
;
1531 WARN_ON(idx
>= ARRAY_SIZE(xfer_mode_str
));
1533 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1534 idx
, dev
->xfer_shift
, (int)dev
->xfer_mode
, (int)base
, ofs
);
1536 printk(KERN_INFO
"ata%u: dev %u configured for %s\n",
1537 ap
->id
, dev
->devno
, xfer_mode_str
[idx
]);
1540 static int ata_host_set_pio(struct ata_port
*ap
)
1546 mask
= ata_get_mode_mask(ap
, ATA_SHIFT_PIO
);
1549 printk(KERN_WARNING
"ata%u: no PIO support\n", ap
->id
);
1553 base
= base_from_shift(ATA_SHIFT_PIO
);
1554 xfer_mode
= base
+ x
;
1556 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1557 (int)base
, (int)xfer_mode
, mask
, x
);
1559 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1560 struct ata_device
*dev
= &ap
->device
[i
];
1561 if (ata_dev_present(dev
)) {
1562 dev
->pio_mode
= xfer_mode
;
1563 dev
->xfer_mode
= xfer_mode
;
1564 dev
->xfer_shift
= ATA_SHIFT_PIO
;
1565 if (ap
->ops
->set_piomode
)
1566 ap
->ops
->set_piomode(ap
, dev
);
1573 static void ata_host_set_dma(struct ata_port
*ap
, u8 xfer_mode
,
1574 unsigned int xfer_shift
)
1578 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1579 struct ata_device
*dev
= &ap
->device
[i
];
1580 if (ata_dev_present(dev
)) {
1581 dev
->dma_mode
= xfer_mode
;
1582 dev
->xfer_mode
= xfer_mode
;
1583 dev
->xfer_shift
= xfer_shift
;
1584 if (ap
->ops
->set_dmamode
)
1585 ap
->ops
->set_dmamode(ap
, dev
);
1591 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1592 * @ap: port on which timings will be programmed
1594 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1597 * PCI/etc. bus probe sem.
1600 static void ata_set_mode(struct ata_port
*ap
)
1602 unsigned int i
, xfer_shift
;
1606 /* step 1: always set host PIO timings */
1607 rc
= ata_host_set_pio(ap
);
1611 /* step 2: choose the best data xfer mode */
1612 xfer_mode
= xfer_shift
= 0;
1613 rc
= ata_choose_xfer_mode(ap
, &xfer_mode
, &xfer_shift
);
1617 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1618 if (xfer_shift
!= ATA_SHIFT_PIO
)
1619 ata_host_set_dma(ap
, xfer_mode
, xfer_shift
);
1621 /* step 4: update devices' xfer mode */
1622 ata_dev_set_mode(ap
, &ap
->device
[0]);
1623 ata_dev_set_mode(ap
, &ap
->device
[1]);
1625 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1628 if (ap
->ops
->post_set_mode
)
1629 ap
->ops
->post_set_mode(ap
);
1631 for (i
= 0; i
< 2; i
++) {
1632 struct ata_device
*dev
= &ap
->device
[i
];
1633 ata_dev_set_protocol(dev
);
1639 ata_port_disable(ap
);
1643 * ata_busy_sleep - sleep until BSY clears, or timeout
1644 * @ap: port containing status register to be polled
1645 * @tmout_pat: impatience timeout
1646 * @tmout: overall timeout
1648 * Sleep until ATA Status register bit BSY clears,
1649 * or a timeout occurs.
1655 static unsigned int ata_busy_sleep (struct ata_port
*ap
,
1656 unsigned long tmout_pat
,
1657 unsigned long tmout
)
1659 unsigned long timer_start
, timeout
;
1662 status
= ata_busy_wait(ap
, ATA_BUSY
, 300);
1663 timer_start
= jiffies
;
1664 timeout
= timer_start
+ tmout_pat
;
1665 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1667 status
= ata_busy_wait(ap
, ATA_BUSY
, 3);
1670 if (status
& ATA_BUSY
)
1671 printk(KERN_WARNING
"ata%u is slow to respond, "
1672 "please be patient\n", ap
->id
);
1674 timeout
= timer_start
+ tmout
;
1675 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1677 status
= ata_chk_status(ap
);
1680 if (status
& ATA_BUSY
) {
1681 printk(KERN_ERR
"ata%u failed to respond (%lu secs)\n",
1682 ap
->id
, tmout
/ HZ
);
1689 static void ata_bus_post_reset(struct ata_port
*ap
, unsigned int devmask
)
1691 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1692 unsigned int dev0
= devmask
& (1 << 0);
1693 unsigned int dev1
= devmask
& (1 << 1);
1694 unsigned long timeout
;
1696 /* if device 0 was found in ata_devchk, wait for its
1700 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1702 /* if device 1 was found in ata_devchk, wait for
1703 * register access, then wait for BSY to clear
1705 timeout
= jiffies
+ ATA_TMOUT_BOOT
;
1709 ap
->ops
->dev_select(ap
, 1);
1710 if (ap
->flags
& ATA_FLAG_MMIO
) {
1711 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
1712 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
1714 nsect
= inb(ioaddr
->nsect_addr
);
1715 lbal
= inb(ioaddr
->lbal_addr
);
1717 if ((nsect
== 1) && (lbal
== 1))
1719 if (time_after(jiffies
, timeout
)) {
1723 msleep(50); /* give drive a breather */
1726 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1728 /* is all this really necessary? */
1729 ap
->ops
->dev_select(ap
, 0);
1731 ap
->ops
->dev_select(ap
, 1);
1733 ap
->ops
->dev_select(ap
, 0);
1737 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1738 * @ap: Port to reset and probe
1740 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1741 * probe the bus. Not often used these days.
1744 * PCI/etc. bus probe sem.
1748 static unsigned int ata_bus_edd(struct ata_port
*ap
)
1750 struct ata_taskfile tf
;
1752 /* set up execute-device-diag (bus reset) taskfile */
1753 /* also, take interrupts to a known state (disabled) */
1754 DPRINTK("execute-device-diag\n");
1755 ata_tf_init(ap
, &tf
, 0);
1757 tf
.command
= ATA_CMD_EDD
;
1758 tf
.protocol
= ATA_PROT_NODATA
;
1761 ata_tf_to_host(ap
, &tf
);
1763 /* spec says at least 2ms. but who knows with those
1764 * crazy ATAPI devices...
1768 return ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1771 static unsigned int ata_bus_softreset(struct ata_port
*ap
,
1772 unsigned int devmask
)
1774 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1776 DPRINTK("ata%u: bus reset via SRST\n", ap
->id
);
1778 /* software reset. causes dev0 to be selected */
1779 if (ap
->flags
& ATA_FLAG_MMIO
) {
1780 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1781 udelay(20); /* FIXME: flush */
1782 writeb(ap
->ctl
| ATA_SRST
, (void __iomem
*) ioaddr
->ctl_addr
);
1783 udelay(20); /* FIXME: flush */
1784 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1786 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1788 outb(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
1790 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1793 /* spec mandates ">= 2ms" before checking status.
1794 * We wait 150ms, because that was the magic delay used for
1795 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1796 * between when the ATA command register is written, and then
1797 * status is checked. Because waiting for "a while" before
1798 * checking status is fine, post SRST, we perform this magic
1799 * delay here as well.
1803 ata_bus_post_reset(ap
, devmask
);
1809 * ata_bus_reset - reset host port and associated ATA channel
1810 * @ap: port to reset
1812 * This is typically the first time we actually start issuing
1813 * commands to the ATA channel. We wait for BSY to clear, then
1814 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1815 * result. Determine what devices, if any, are on the channel
1816 * by looking at the device 0/1 error register. Look at the signature
1817 * stored in each device's taskfile registers, to determine if
1818 * the device is ATA or ATAPI.
1821 * PCI/etc. bus probe sem.
1822 * Obtains host_set lock.
1825 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1828 void ata_bus_reset(struct ata_port
*ap
)
1830 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1831 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
1833 unsigned int dev0
, dev1
= 0, rc
= 0, devmask
= 0;
1835 DPRINTK("ENTER, host %u, port %u\n", ap
->id
, ap
->port_no
);
1837 /* determine if device 0/1 are present */
1838 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
1841 dev0
= ata_devchk(ap
, 0);
1843 dev1
= ata_devchk(ap
, 1);
1847 devmask
|= (1 << 0);
1849 devmask
|= (1 << 1);
1851 /* select device 0 again */
1852 ap
->ops
->dev_select(ap
, 0);
1854 /* issue bus reset */
1855 if (ap
->flags
& ATA_FLAG_SRST
)
1856 rc
= ata_bus_softreset(ap
, devmask
);
1857 else if ((ap
->flags
& ATA_FLAG_SATA_RESET
) == 0) {
1858 /* set up device control */
1859 if (ap
->flags
& ATA_FLAG_MMIO
)
1860 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1862 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1863 rc
= ata_bus_edd(ap
);
1870 * determine by signature whether we have ATA or ATAPI devices
1872 err
= ata_dev_try_classify(ap
, 0);
1873 if ((slave_possible
) && (err
!= 0x81))
1874 ata_dev_try_classify(ap
, 1);
1876 /* re-enable interrupts */
1877 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
1880 /* is double-select really necessary? */
1881 if (ap
->device
[1].class != ATA_DEV_NONE
)
1882 ap
->ops
->dev_select(ap
, 1);
1883 if (ap
->device
[0].class != ATA_DEV_NONE
)
1884 ap
->ops
->dev_select(ap
, 0);
1886 /* if no devices were detected, disable this port */
1887 if ((ap
->device
[0].class == ATA_DEV_NONE
) &&
1888 (ap
->device
[1].class == ATA_DEV_NONE
))
1891 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
1892 /* set up device control for ATA_FLAG_SATA_RESET */
1893 if (ap
->flags
& ATA_FLAG_MMIO
)
1894 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1896 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1903 printk(KERN_ERR
"ata%u: disabling port\n", ap
->id
);
1904 ap
->ops
->port_disable(ap
);
1909 static void ata_pr_blacklisted(struct ata_port
*ap
, struct ata_device
*dev
)
1911 printk(KERN_WARNING
"ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1912 ap
->id
, dev
->devno
);
1915 static const char * ata_dma_blacklist
[] = {
1934 "Toshiba CD-ROM XM-6202B",
1935 "TOSHIBA CD-ROM XM-1702BC",
1937 "E-IDE CD-ROM CR-840",
1940 "SAMSUNG CD-ROM SC-148C",
1941 "SAMSUNG CD-ROM SC",
1943 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1947 static int ata_dma_blacklisted(struct ata_port
*ap
, struct ata_device
*dev
)
1949 unsigned char model_num
[40];
1954 ata_dev_id_string(dev
->id
, model_num
, ATA_ID_PROD_OFS
,
1957 len
= strnlen(s
, sizeof(model_num
));
1959 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1960 while ((len
> 0) && (s
[len
- 1] == ' ')) {
1965 for (i
= 0; i
< ARRAY_SIZE(ata_dma_blacklist
); i
++)
1966 if (!strncmp(ata_dma_blacklist
[i
], s
, len
))
1972 static unsigned int ata_get_mode_mask(struct ata_port
*ap
, int shift
)
1974 struct ata_device
*master
, *slave
;
1977 master
= &ap
->device
[0];
1978 slave
= &ap
->device
[1];
1980 assert (ata_dev_present(master
) || ata_dev_present(slave
));
1982 if (shift
== ATA_SHIFT_UDMA
) {
1983 mask
= ap
->udma_mask
;
1984 if (ata_dev_present(master
)) {
1985 mask
&= (master
->id
[ATA_ID_UDMA_MODES
] & 0xff);
1986 if (ata_dma_blacklisted(ap
, master
)) {
1988 ata_pr_blacklisted(ap
, master
);
1991 if (ata_dev_present(slave
)) {
1992 mask
&= (slave
->id
[ATA_ID_UDMA_MODES
] & 0xff);
1993 if (ata_dma_blacklisted(ap
, slave
)) {
1995 ata_pr_blacklisted(ap
, slave
);
1999 else if (shift
== ATA_SHIFT_MWDMA
) {
2000 mask
= ap
->mwdma_mask
;
2001 if (ata_dev_present(master
)) {
2002 mask
&= (master
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2003 if (ata_dma_blacklisted(ap
, master
)) {
2005 ata_pr_blacklisted(ap
, master
);
2008 if (ata_dev_present(slave
)) {
2009 mask
&= (slave
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2010 if (ata_dma_blacklisted(ap
, slave
)) {
2012 ata_pr_blacklisted(ap
, slave
);
2016 else if (shift
== ATA_SHIFT_PIO
) {
2017 mask
= ap
->pio_mask
;
2018 if (ata_dev_present(master
)) {
2019 /* spec doesn't return explicit support for
2020 * PIO0-2, so we fake it
2022 u16 tmp_mode
= master
->id
[ATA_ID_PIO_MODES
] & 0x03;
2027 if (ata_dev_present(slave
)) {
2028 /* spec doesn't return explicit support for
2029 * PIO0-2, so we fake it
2031 u16 tmp_mode
= slave
->id
[ATA_ID_PIO_MODES
] & 0x03;
2038 mask
= 0xffffffff; /* shut up compiler warning */
2045 /* find greatest bit */
2046 static int fgb(u32 bitmap
)
2051 for (i
= 0; i
< 32; i
++)
2052 if (bitmap
& (1 << i
))
2059 * ata_choose_xfer_mode - attempt to find best transfer mode
2060 * @ap: Port for which an xfer mode will be selected
2061 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2062 * @xfer_shift_out: (output) bit shift that selects this mode
2064 * Based on host and device capabilities, determine the
2065 * maximum transfer mode that is amenable to all.
2068 * PCI/etc. bus probe sem.
2071 * Zero on success, negative on error.
2074 static int ata_choose_xfer_mode(struct ata_port
*ap
,
2076 unsigned int *xfer_shift_out
)
2078 unsigned int mask
, shift
;
2081 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++) {
2082 shift
= xfer_mode_classes
[i
].shift
;
2083 mask
= ata_get_mode_mask(ap
, shift
);
2087 *xfer_mode_out
= xfer_mode_classes
[i
].base
+ x
;
2088 *xfer_shift_out
= shift
;
2097 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2098 * @ap: Port associated with device @dev
2099 * @dev: Device to which command will be sent
2101 * Issue SET FEATURES - XFER MODE command to device @dev
2105 * PCI/etc. bus probe sem.
2108 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
)
2110 DECLARE_COMPLETION(wait
);
2111 struct ata_queued_cmd
*qc
;
2113 unsigned long flags
;
2115 /* set up set-features taskfile */
2116 DPRINTK("set features - xfer mode\n");
2118 qc
= ata_qc_new_init(ap
, dev
);
2121 qc
->tf
.command
= ATA_CMD_SET_FEATURES
;
2122 qc
->tf
.feature
= SETFEATURES_XFER
;
2123 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2124 qc
->tf
.protocol
= ATA_PROT_NODATA
;
2125 qc
->tf
.nsect
= dev
->xfer_mode
;
2127 qc
->waiting
= &wait
;
2128 qc
->complete_fn
= ata_qc_complete_noop
;
2130 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2131 rc
= ata_qc_issue(qc
);
2132 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2135 ata_port_disable(ap
);
2137 wait_for_completion(&wait
);
2143 * ata_sg_clean - Unmap DMA memory associated with command
2144 * @qc: Command containing DMA memory to be released
2146 * Unmap all mapped DMA memory associated with this command.
2149 * spin_lock_irqsave(host_set lock)
2152 static void ata_sg_clean(struct ata_queued_cmd
*qc
)
2154 struct ata_port
*ap
= qc
->ap
;
2155 struct scatterlist
*sg
= qc
->sg
;
2156 int dir
= qc
->dma_dir
;
2158 assert(qc
->flags
& ATA_QCFLAG_DMAMAP
);
2161 if (qc
->flags
& ATA_QCFLAG_SINGLE
)
2162 assert(qc
->n_elem
== 1);
2164 DPRINTK("unmapping %u sg elements\n", qc
->n_elem
);
2166 if (qc
->flags
& ATA_QCFLAG_SG
)
2167 dma_unmap_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2169 dma_unmap_single(ap
->host_set
->dev
, sg_dma_address(&sg
[0]),
2170 sg_dma_len(&sg
[0]), dir
);
2172 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
2177 * ata_fill_sg - Fill PCI IDE PRD table
2178 * @qc: Metadata associated with taskfile to be transferred
2180 * Fill PCI IDE PRD (scatter-gather) table with segments
2181 * associated with the current disk command.
2184 * spin_lock_irqsave(host_set lock)
2187 static void ata_fill_sg(struct ata_queued_cmd
*qc
)
2189 struct scatterlist
*sg
= qc
->sg
;
2190 struct ata_port
*ap
= qc
->ap
;
2191 unsigned int idx
, nelem
;
2194 assert(qc
->n_elem
> 0);
2197 for (nelem
= qc
->n_elem
; nelem
; nelem
--,sg
++) {
2201 /* determine if physical DMA addr spans 64K boundary.
2202 * Note h/w doesn't support 64-bit, so we unconditionally
2203 * truncate dma_addr_t to u32.
2205 addr
= (u32
) sg_dma_address(sg
);
2206 sg_len
= sg_dma_len(sg
);
2209 offset
= addr
& 0xffff;
2211 if ((offset
+ sg_len
) > 0x10000)
2212 len
= 0x10000 - offset
;
2214 ap
->prd
[idx
].addr
= cpu_to_le32(addr
);
2215 ap
->prd
[idx
].flags_len
= cpu_to_le32(len
& 0xffff);
2216 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx
, addr
, len
);
2225 ap
->prd
[idx
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
2228 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2229 * @qc: Metadata associated with taskfile to check
2231 * Allow low-level driver to filter ATA PACKET commands, returning
2232 * a status indicating whether or not it is OK to use DMA for the
2233 * supplied PACKET command.
2236 * spin_lock_irqsave(host_set lock)
2238 * RETURNS: 0 when ATAPI DMA can be used
2241 int ata_check_atapi_dma(struct ata_queued_cmd
*qc
)
2243 struct ata_port
*ap
= qc
->ap
;
2244 int rc
= 0; /* Assume ATAPI DMA is OK by default */
2246 if (ap
->ops
->check_atapi_dma
)
2247 rc
= ap
->ops
->check_atapi_dma(qc
);
2252 * ata_qc_prep - Prepare taskfile for submission
2253 * @qc: Metadata associated with taskfile to be prepared
2255 * Prepare ATA taskfile for submission.
2258 * spin_lock_irqsave(host_set lock)
2260 void ata_qc_prep(struct ata_queued_cmd
*qc
)
2262 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
2269 * ata_sg_init_one - Associate command with memory buffer
2270 * @qc: Command to be associated
2271 * @buf: Memory buffer
2272 * @buflen: Length of memory buffer, in bytes.
2274 * Initialize the data-related elements of queued_cmd @qc
2275 * to point to a single memory buffer, @buf of byte length @buflen.
2278 * spin_lock_irqsave(host_set lock)
2281 void ata_sg_init_one(struct ata_queued_cmd
*qc
, void *buf
, unsigned int buflen
)
2283 struct scatterlist
*sg
;
2285 qc
->flags
|= ATA_QCFLAG_SINGLE
;
2287 memset(&qc
->sgent
, 0, sizeof(qc
->sgent
));
2288 qc
->sg
= &qc
->sgent
;
2293 sg
->page
= virt_to_page(buf
);
2294 sg
->offset
= (unsigned long) buf
& ~PAGE_MASK
;
2295 sg
->length
= buflen
;
2299 * ata_sg_init - Associate command with scatter-gather table.
2300 * @qc: Command to be associated
2301 * @sg: Scatter-gather table.
2302 * @n_elem: Number of elements in s/g table.
2304 * Initialize the data-related elements of queued_cmd @qc
2305 * to point to a scatter-gather table @sg, containing @n_elem
2309 * spin_lock_irqsave(host_set lock)
2312 void ata_sg_init(struct ata_queued_cmd
*qc
, struct scatterlist
*sg
,
2313 unsigned int n_elem
)
2315 qc
->flags
|= ATA_QCFLAG_SG
;
2317 qc
->n_elem
= n_elem
;
2321 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2322 * @qc: Command with memory buffer to be mapped.
2324 * DMA-map the memory buffer associated with queued_cmd @qc.
2327 * spin_lock_irqsave(host_set lock)
2330 * Zero on success, negative on error.
2333 static int ata_sg_setup_one(struct ata_queued_cmd
*qc
)
2335 struct ata_port
*ap
= qc
->ap
;
2336 int dir
= qc
->dma_dir
;
2337 struct scatterlist
*sg
= qc
->sg
;
2338 dma_addr_t dma_address
;
2340 dma_address
= dma_map_single(ap
->host_set
->dev
, qc
->buf_virt
,
2342 if (dma_mapping_error(dma_address
))
2345 sg_dma_address(sg
) = dma_address
;
2346 sg_dma_len(sg
) = sg
->length
;
2348 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg
),
2349 qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2355 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2356 * @qc: Command with scatter-gather table to be mapped.
2358 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2361 * spin_lock_irqsave(host_set lock)
2364 * Zero on success, negative on error.
2368 static int ata_sg_setup(struct ata_queued_cmd
*qc
)
2370 struct ata_port
*ap
= qc
->ap
;
2371 struct scatterlist
*sg
= qc
->sg
;
2374 VPRINTK("ENTER, ata%u\n", ap
->id
);
2375 assert(qc
->flags
& ATA_QCFLAG_SG
);
2378 n_elem
= dma_map_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2382 DPRINTK("%d sg elements mapped\n", n_elem
);
2384 qc
->n_elem
= n_elem
;
2390 * ata_poll_qc_complete - turn irq back on and finish qc
2391 * @qc: Command to complete
2392 * @drv_stat: ATA status register content
2395 * None. (grabs host lock)
2398 void ata_poll_qc_complete(struct ata_queued_cmd
*qc
, u8 drv_stat
)
2400 struct ata_port
*ap
= qc
->ap
;
2401 unsigned long flags
;
2403 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2404 ap
->flags
&= ~ATA_FLAG_NOINTR
;
2406 ata_qc_complete(qc
, drv_stat
);
2407 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2415 * None. (executing in kernel thread context)
2421 static unsigned long ata_pio_poll(struct ata_port
*ap
)
2424 unsigned int poll_state
= PIO_ST_UNKNOWN
;
2425 unsigned int reg_state
= PIO_ST_UNKNOWN
;
2426 const unsigned int tmout_state
= PIO_ST_TMOUT
;
2428 switch (ap
->pio_task_state
) {
2431 poll_state
= PIO_ST_POLL
;
2435 case PIO_ST_LAST_POLL
:
2436 poll_state
= PIO_ST_LAST_POLL
;
2437 reg_state
= PIO_ST_LAST
;
2444 status
= ata_chk_status(ap
);
2445 if (status
& ATA_BUSY
) {
2446 if (time_after(jiffies
, ap
->pio_task_timeout
)) {
2447 ap
->pio_task_state
= tmout_state
;
2450 ap
->pio_task_state
= poll_state
;
2451 return ATA_SHORT_PAUSE
;
2454 ap
->pio_task_state
= reg_state
;
2459 * ata_pio_complete -
2463 * None. (executing in kernel thread context)
2466 static void ata_pio_complete (struct ata_port
*ap
)
2468 struct ata_queued_cmd
*qc
;
2472 * This is purely heuristic. This is a fast path. Sometimes when
2473 * we enter, BSY will be cleared in a chk-status or two. If not,
2474 * the drive is probably seeking or something. Snooze for a couple
2475 * msecs, then chk-status again. If still busy, fall back to
2476 * PIO_ST_POLL state.
2478 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
| ATA_DRQ
, 10);
2479 if (drv_stat
& (ATA_BUSY
| ATA_DRQ
)) {
2481 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
| ATA_DRQ
, 10);
2482 if (drv_stat
& (ATA_BUSY
| ATA_DRQ
)) {
2483 ap
->pio_task_state
= PIO_ST_LAST_POLL
;
2484 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
2489 drv_stat
= ata_wait_idle(ap
);
2490 if (!ata_ok(drv_stat
)) {
2491 ap
->pio_task_state
= PIO_ST_ERR
;
2495 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2498 ap
->pio_task_state
= PIO_ST_IDLE
;
2500 ata_poll_qc_complete(qc
, drv_stat
);
2506 * @buf: Buffer to swap
2507 * @buf_words: Number of 16-bit words in buffer.
2509 * Swap halves of 16-bit words if needed to convert from
2510 * little-endian byte order to native cpu byte order, or
2515 void swap_buf_le16(u16
*buf
, unsigned int buf_words
)
2520 for (i
= 0; i
< buf_words
; i
++)
2521 buf
[i
] = le16_to_cpu(buf
[i
]);
2522 #endif /* __BIG_ENDIAN */
2526 * ata_mmio_data_xfer - Transfer data by MMIO
2527 * @ap: port to read/write
2529 * @buflen: buffer length
2530 * @do_write: read/write
2532 * Transfer data from/to the device data register by MMIO.
2535 * Inherited from caller.
2539 static void ata_mmio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2540 unsigned int buflen
, int write_data
)
2543 unsigned int words
= buflen
>> 1;
2544 u16
*buf16
= (u16
*) buf
;
2545 void __iomem
*mmio
= (void __iomem
*)ap
->ioaddr
.data_addr
;
2547 /* Transfer multiple of 2 bytes */
2549 for (i
= 0; i
< words
; i
++)
2550 writew(le16_to_cpu(buf16
[i
]), mmio
);
2552 for (i
= 0; i
< words
; i
++)
2553 buf16
[i
] = cpu_to_le16(readw(mmio
));
2556 /* Transfer trailing 1 byte, if any. */
2557 if (unlikely(buflen
& 0x01)) {
2558 u16 align_buf
[1] = { 0 };
2559 unsigned char *trailing_buf
= buf
+ buflen
- 1;
2562 memcpy(align_buf
, trailing_buf
, 1);
2563 writew(le16_to_cpu(align_buf
[0]), mmio
);
2565 align_buf
[0] = cpu_to_le16(readw(mmio
));
2566 memcpy(trailing_buf
, align_buf
, 1);
2572 * ata_pio_data_xfer - Transfer data by PIO
2573 * @ap: port to read/write
2575 * @buflen: buffer length
2576 * @do_write: read/write
2578 * Transfer data from/to the device data register by PIO.
2581 * Inherited from caller.
2585 static void ata_pio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2586 unsigned int buflen
, int write_data
)
2588 unsigned int words
= buflen
>> 1;
2590 /* Transfer multiple of 2 bytes */
2592 outsw(ap
->ioaddr
.data_addr
, buf
, words
);
2594 insw(ap
->ioaddr
.data_addr
, buf
, words
);
2596 /* Transfer trailing 1 byte, if any. */
2597 if (unlikely(buflen
& 0x01)) {
2598 u16 align_buf
[1] = { 0 };
2599 unsigned char *trailing_buf
= buf
+ buflen
- 1;
2602 memcpy(align_buf
, trailing_buf
, 1);
2603 outw(le16_to_cpu(align_buf
[0]), ap
->ioaddr
.data_addr
);
2605 align_buf
[0] = cpu_to_le16(inw(ap
->ioaddr
.data_addr
));
2606 memcpy(trailing_buf
, align_buf
, 1);
2612 * ata_data_xfer - Transfer data from/to the data register.
2613 * @ap: port to read/write
2615 * @buflen: buffer length
2616 * @do_write: read/write
2618 * Transfer data from/to the device data register.
2621 * Inherited from caller.
2625 static void ata_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2626 unsigned int buflen
, int do_write
)
2628 if (ap
->flags
& ATA_FLAG_MMIO
)
2629 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
2631 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
2635 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2636 * @qc: Command on going
2638 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2641 * Inherited from caller.
2644 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
2646 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2647 struct scatterlist
*sg
= qc
->sg
;
2648 struct ata_port
*ap
= qc
->ap
;
2650 unsigned int offset
;
2653 if (qc
->cursect
== (qc
->nsect
- 1))
2654 ap
->pio_task_state
= PIO_ST_LAST
;
2656 page
= sg
[qc
->cursg
].page
;
2657 offset
= sg
[qc
->cursg
].offset
+ qc
->cursg_ofs
* ATA_SECT_SIZE
;
2659 /* get the current page and offset */
2660 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
2661 offset
%= PAGE_SIZE
;
2663 buf
= kmap(page
) + offset
;
2668 if ((qc
->cursg_ofs
* ATA_SECT_SIZE
) == (&sg
[qc
->cursg
])->length
) {
2673 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2675 /* do the actual data transfer */
2676 do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2677 ata_data_xfer(ap
, buf
, ATA_SECT_SIZE
, do_write
);
2683 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2684 * @qc: Command on going
2685 * @bytes: number of bytes
2687 * Transfer Transfer data from/to the ATAPI device.
2690 * Inherited from caller.
2694 static void __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
2696 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2697 struct scatterlist
*sg
= qc
->sg
;
2698 struct ata_port
*ap
= qc
->ap
;
2701 unsigned int offset
, count
;
2703 if (qc
->curbytes
+ bytes
>= qc
->nbytes
)
2704 ap
->pio_task_state
= PIO_ST_LAST
;
2707 if (unlikely(qc
->cursg
>= qc
->n_elem
)) {
2709 * The end of qc->sg is reached and the device expects
2710 * more data to transfer. In order not to overrun qc->sg
2711 * and fulfill length specified in the byte count register,
2712 * - for read case, discard trailing data from the device
2713 * - for write case, padding zero data to the device
2715 u16 pad_buf
[1] = { 0 };
2716 unsigned int words
= bytes
>> 1;
2719 if (words
) /* warning if bytes > 1 */
2720 printk(KERN_WARNING
"ata%u: %u bytes trailing data\n",
2723 for (i
= 0; i
< words
; i
++)
2724 ata_data_xfer(ap
, (unsigned char*)pad_buf
, 2, do_write
);
2726 ap
->pio_task_state
= PIO_ST_LAST
;
2730 sg
= &qc
->sg
[qc
->cursg
];
2733 offset
= sg
->offset
+ qc
->cursg_ofs
;
2735 /* get the current page and offset */
2736 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
2737 offset
%= PAGE_SIZE
;
2739 /* don't overrun current sg */
2740 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
2742 /* don't cross page boundaries */
2743 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
2745 buf
= kmap(page
) + offset
;
2748 qc
->curbytes
+= count
;
2749 qc
->cursg_ofs
+= count
;
2751 if (qc
->cursg_ofs
== sg
->length
) {
2756 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2758 /* do the actual data transfer */
2759 ata_data_xfer(ap
, buf
, count
, do_write
);
2768 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2769 * @qc: Command on going
2771 * Transfer Transfer data from/to the ATAPI device.
2774 * Inherited from caller.
2778 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
2780 struct ata_port
*ap
= qc
->ap
;
2781 struct ata_device
*dev
= qc
->dev
;
2782 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
2783 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
2785 ap
->ops
->tf_read(ap
, &qc
->tf
);
2786 ireason
= qc
->tf
.nsect
;
2787 bc_lo
= qc
->tf
.lbam
;
2788 bc_hi
= qc
->tf
.lbah
;
2789 bytes
= (bc_hi
<< 8) | bc_lo
;
2791 /* shall be cleared to zero, indicating xfer of data */
2792 if (ireason
& (1 << 0))
2795 /* make sure transfer direction matches expected */
2796 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
2797 if (do_write
!= i_write
)
2800 __atapi_pio_bytes(qc
, bytes
);
2805 printk(KERN_INFO
"ata%u: dev %u: ATAPI check failed\n",
2806 ap
->id
, dev
->devno
);
2807 ap
->pio_task_state
= PIO_ST_ERR
;
2815 * None. (executing in kernel thread context)
2818 static void ata_pio_block(struct ata_port
*ap
)
2820 struct ata_queued_cmd
*qc
;
2824 * This is purely hueristic. This is a fast path.
2825 * Sometimes when we enter, BSY will be cleared in
2826 * a chk-status or two. If not, the drive is probably seeking
2827 * or something. Snooze for a couple msecs, then
2828 * chk-status again. If still busy, fall back to
2829 * PIO_ST_POLL state.
2831 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
2832 if (status
& ATA_BUSY
) {
2834 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
2835 if (status
& ATA_BUSY
) {
2836 ap
->pio_task_state
= PIO_ST_POLL
;
2837 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
2842 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2845 if (is_atapi_taskfile(&qc
->tf
)) {
2846 /* no more data to transfer or unsupported ATAPI command */
2847 if ((status
& ATA_DRQ
) == 0) {
2848 ap
->pio_task_state
= PIO_ST_IDLE
;
2850 ata_poll_qc_complete(qc
, status
);
2854 atapi_pio_bytes(qc
);
2856 /* handle BSY=0, DRQ=0 as error */
2857 if ((status
& ATA_DRQ
) == 0) {
2858 ap
->pio_task_state
= PIO_ST_ERR
;
2866 static void ata_pio_error(struct ata_port
*ap
)
2868 struct ata_queued_cmd
*qc
;
2871 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2874 drv_stat
= ata_chk_status(ap
);
2875 printk(KERN_WARNING
"ata%u: PIO error, drv_stat 0x%x\n",
2878 ap
->pio_task_state
= PIO_ST_IDLE
;
2880 ata_poll_qc_complete(qc
, drv_stat
| ATA_ERR
);
2883 static void ata_pio_task(void *_data
)
2885 struct ata_port
*ap
= _data
;
2886 unsigned long timeout
= 0;
2888 switch (ap
->pio_task_state
) {
2897 ata_pio_complete(ap
);
2901 case PIO_ST_LAST_POLL
:
2902 timeout
= ata_pio_poll(ap
);
2912 queue_delayed_work(ata_wq
, &ap
->pio_task
,
2915 queue_work(ata_wq
, &ap
->pio_task
);
2918 static void atapi_request_sense(struct ata_port
*ap
, struct ata_device
*dev
,
2919 struct scsi_cmnd
*cmd
)
2921 DECLARE_COMPLETION(wait
);
2922 struct ata_queued_cmd
*qc
;
2923 unsigned long flags
;
2926 DPRINTK("ATAPI request sense\n");
2928 qc
= ata_qc_new_init(ap
, dev
);
2931 /* FIXME: is this needed? */
2932 memset(cmd
->sense_buffer
, 0, sizeof(cmd
->sense_buffer
));
2934 ata_sg_init_one(qc
, cmd
->sense_buffer
, sizeof(cmd
->sense_buffer
));
2935 qc
->dma_dir
= DMA_FROM_DEVICE
;
2937 memset(&qc
->cdb
, 0, ap
->cdb_len
);
2938 qc
->cdb
[0] = REQUEST_SENSE
;
2939 qc
->cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
2941 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2942 qc
->tf
.command
= ATA_CMD_PACKET
;
2944 qc
->tf
.protocol
= ATA_PROT_ATAPI
;
2945 qc
->tf
.lbam
= (8 * 1024) & 0xff;
2946 qc
->tf
.lbah
= (8 * 1024) >> 8;
2947 qc
->nbytes
= SCSI_SENSE_BUFFERSIZE
;
2949 qc
->waiting
= &wait
;
2950 qc
->complete_fn
= ata_qc_complete_noop
;
2952 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2953 rc
= ata_qc_issue(qc
);
2954 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2957 ata_port_disable(ap
);
2959 wait_for_completion(&wait
);
2965 * ata_qc_timeout - Handle timeout of queued command
2966 * @qc: Command that timed out
2968 * Some part of the kernel (currently, only the SCSI layer)
2969 * has noticed that the active command on port @ap has not
2970 * completed after a specified length of time. Handle this
2971 * condition by disabling DMA (if necessary) and completing
2972 * transactions, with error if necessary.
2974 * This also handles the case of the "lost interrupt", where
2975 * for some reason (possibly hardware bug, possibly driver bug)
2976 * an interrupt was not delivered to the driver, even though the
2977 * transaction completed successfully.
2980 * Inherited from SCSI layer (none, can sleep)
2983 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
2985 struct ata_port
*ap
= qc
->ap
;
2986 struct ata_host_set
*host_set
= ap
->host_set
;
2987 struct ata_device
*dev
= qc
->dev
;
2988 u8 host_stat
= 0, drv_stat
;
2989 unsigned long flags
;
2993 /* FIXME: doesn't this conflict with timeout handling? */
2994 if (qc
->dev
->class == ATA_DEV_ATAPI
&& qc
->scsicmd
) {
2995 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
2997 if (!(cmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
)) {
2999 /* finish completing original command */
3000 spin_lock_irqsave(&host_set
->lock
, flags
);
3001 __ata_qc_complete(qc
);
3002 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3004 atapi_request_sense(ap
, dev
, cmd
);
3006 cmd
->result
= (CHECK_CONDITION
<< 1) | (DID_OK
<< 16);
3007 scsi_finish_command(cmd
);
3013 spin_lock_irqsave(&host_set
->lock
, flags
);
3015 /* hack alert! We cannot use the supplied completion
3016 * function from inside the ->eh_strategy_handler() thread.
3017 * libata is the only user of ->eh_strategy_handler() in
3018 * any kernel, so the default scsi_done() assumes it is
3019 * not being called from the SCSI EH.
3021 qc
->scsidone
= scsi_finish_command
;
3023 switch (qc
->tf
.protocol
) {
3026 case ATA_PROT_ATAPI_DMA
:
3027 host_stat
= ap
->ops
->bmdma_status(ap
);
3029 /* before we do anything else, clear DMA-Start bit */
3030 ap
->ops
->bmdma_stop(qc
);
3036 drv_stat
= ata_chk_status(ap
);
3038 /* ack bmdma irq events */
3039 ap
->ops
->irq_clear(ap
);
3041 printk(KERN_ERR
"ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3042 ap
->id
, qc
->tf
.command
, drv_stat
, host_stat
);
3044 /* complete taskfile transaction */
3045 ata_qc_complete(qc
, drv_stat
);
3049 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3056 * ata_eng_timeout - Handle timeout of queued command
3057 * @ap: Port on which timed-out command is active
3059 * Some part of the kernel (currently, only the SCSI layer)
3060 * has noticed that the active command on port @ap has not
3061 * completed after a specified length of time. Handle this
3062 * condition by disabling DMA (if necessary) and completing
3063 * transactions, with error if necessary.
3065 * This also handles the case of the "lost interrupt", where
3066 * for some reason (possibly hardware bug, possibly driver bug)
3067 * an interrupt was not delivered to the driver, even though the
3068 * transaction completed successfully.
3071 * Inherited from SCSI layer (none, can sleep)
3074 void ata_eng_timeout(struct ata_port
*ap
)
3076 struct ata_queued_cmd
*qc
;
3080 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3082 printk(KERN_ERR
"ata%u: BUG: timeout without command\n",
3094 * ata_qc_new - Request an available ATA command, for queueing
3095 * @ap: Port associated with device @dev
3096 * @dev: Device from whom we request an available command structure
3102 static struct ata_queued_cmd
*ata_qc_new(struct ata_port
*ap
)
3104 struct ata_queued_cmd
*qc
= NULL
;
3107 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++)
3108 if (!test_and_set_bit(i
, &ap
->qactive
)) {
3109 qc
= ata_qc_from_tag(ap
, i
);
3120 * ata_qc_new_init - Request an available ATA command, and initialize it
3121 * @ap: Port associated with device @dev
3122 * @dev: Device from whom we request an available command structure
3128 struct ata_queued_cmd
*ata_qc_new_init(struct ata_port
*ap
,
3129 struct ata_device
*dev
)
3131 struct ata_queued_cmd
*qc
;
3133 qc
= ata_qc_new(ap
);
3140 qc
->cursect
= qc
->cursg
= qc
->cursg_ofs
= 0;
3142 qc
->nbytes
= qc
->curbytes
= 0;
3144 ata_tf_init(ap
, &qc
->tf
, dev
->devno
);
3146 if (dev
->flags
& ATA_DFLAG_LBA48
)
3147 qc
->tf
.flags
|= ATA_TFLAG_LBA48
;
3153 static int ata_qc_complete_noop(struct ata_queued_cmd
*qc
, u8 drv_stat
)
3158 static void __ata_qc_complete(struct ata_queued_cmd
*qc
)
3160 struct ata_port
*ap
= qc
->ap
;
3161 unsigned int tag
, do_clear
= 0;
3165 if (likely(ata_tag_valid(tag
))) {
3166 if (tag
== ap
->active_tag
)
3167 ap
->active_tag
= ATA_TAG_POISON
;
3168 qc
->tag
= ATA_TAG_POISON
;
3173 struct completion
*waiting
= qc
->waiting
;
3178 if (likely(do_clear
))
3179 clear_bit(tag
, &ap
->qactive
);
3183 * ata_qc_free - free unused ata_queued_cmd
3184 * @qc: Command to complete
3186 * Designed to free unused ata_queued_cmd object
3187 * in case something prevents using it.
3190 * spin_lock_irqsave(host_set lock)
3193 void ata_qc_free(struct ata_queued_cmd
*qc
)
3195 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3196 assert(qc
->waiting
== NULL
); /* nothing should be waiting */
3198 __ata_qc_complete(qc
);
3202 * ata_qc_complete - Complete an active ATA command
3203 * @qc: Command to complete
3204 * @drv_stat: ATA Status register contents
3206 * Indicate to the mid and upper layers that an ATA
3207 * command has completed, with either an ok or not-ok status.
3210 * spin_lock_irqsave(host_set lock)
3214 void ata_qc_complete(struct ata_queued_cmd
*qc
, u8 drv_stat
)
3218 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3219 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
3221 if (likely(qc
->flags
& ATA_QCFLAG_DMAMAP
))
3224 /* atapi: mark qc as inactive to prevent the interrupt handler
3225 * from completing the command twice later, before the error handler
3226 * is called. (when rc != 0 and atapi request sense is needed)
3228 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
3230 /* call completion callback */
3231 rc
= qc
->complete_fn(qc
, drv_stat
);
3233 /* if callback indicates not to complete command (non-zero),
3234 * return immediately
3239 __ata_qc_complete(qc
);
3244 static inline int ata_should_dma_map(struct ata_queued_cmd
*qc
)
3246 struct ata_port
*ap
= qc
->ap
;
3248 switch (qc
->tf
.protocol
) {
3250 case ATA_PROT_ATAPI_DMA
:
3253 case ATA_PROT_ATAPI
:
3255 case ATA_PROT_PIO_MULT
:
3256 if (ap
->flags
& ATA_FLAG_PIO_DMA
)
3269 * ata_qc_issue - issue taskfile to device
3270 * @qc: command to issue to device
3272 * Prepare an ATA command to submission to device.
3273 * This includes mapping the data into a DMA-able
3274 * area, filling in the S/G table, and finally
3275 * writing the taskfile to hardware, starting the command.
3278 * spin_lock_irqsave(host_set lock)
3281 * Zero on success, negative on error.
3284 int ata_qc_issue(struct ata_queued_cmd
*qc
)
3286 struct ata_port
*ap
= qc
->ap
;
3288 if (ata_should_dma_map(qc
)) {
3289 if (qc
->flags
& ATA_QCFLAG_SG
) {
3290 if (ata_sg_setup(qc
))
3292 } else if (qc
->flags
& ATA_QCFLAG_SINGLE
) {
3293 if (ata_sg_setup_one(qc
))
3297 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
3300 ap
->ops
->qc_prep(qc
);
3302 qc
->ap
->active_tag
= qc
->tag
;
3303 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
3305 return ap
->ops
->qc_issue(qc
);
3313 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3314 * @qc: command to issue to device
3316 * Using various libata functions and hooks, this function
3317 * starts an ATA command. ATA commands are grouped into
3318 * classes called "protocols", and issuing each type of protocol
3319 * is slightly different.
3321 * May be used as the qc_issue() entry in ata_port_operations.
3324 * spin_lock_irqsave(host_set lock)
3327 * Zero on success, negative on error.
3330 int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
3332 struct ata_port
*ap
= qc
->ap
;
3334 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
3336 switch (qc
->tf
.protocol
) {
3337 case ATA_PROT_NODATA
:
3338 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3342 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3343 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3344 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3347 case ATA_PROT_PIO
: /* load tf registers, initiate polling pio */
3348 ata_qc_set_polling(qc
);
3349 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3350 ap
->pio_task_state
= PIO_ST
;
3351 queue_work(ata_wq
, &ap
->pio_task
);
3354 case ATA_PROT_ATAPI
:
3355 ata_qc_set_polling(qc
);
3356 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3357 queue_work(ata_wq
, &ap
->packet_task
);
3360 case ATA_PROT_ATAPI_NODATA
:
3361 ap
->flags
|= ATA_FLAG_NOINTR
;
3362 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3363 queue_work(ata_wq
, &ap
->packet_task
);
3366 case ATA_PROT_ATAPI_DMA
:
3367 ap
->flags
|= ATA_FLAG_NOINTR
;
3368 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3369 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3370 queue_work(ata_wq
, &ap
->packet_task
);
3382 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3383 * @qc: Info associated with this ATA transaction.
3386 * spin_lock_irqsave(host_set lock)
3389 static void ata_bmdma_setup_mmio (struct ata_queued_cmd
*qc
)
3391 struct ata_port
*ap
= qc
->ap
;
3392 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3394 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3396 /* load PRD table addr. */
3397 mb(); /* make sure PRD table writes are visible to controller */
3398 writel(ap
->prd_dma
, mmio
+ ATA_DMA_TABLE_OFS
);
3400 /* specify data direction, triple-check start bit is clear */
3401 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3402 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3404 dmactl
|= ATA_DMA_WR
;
3405 writeb(dmactl
, mmio
+ ATA_DMA_CMD
);
3407 /* issue r/w command */
3408 ap
->ops
->exec_command(ap
, &qc
->tf
);
3412 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3413 * @qc: Info associated with this ATA transaction.
3416 * spin_lock_irqsave(host_set lock)
3419 static void ata_bmdma_start_mmio (struct ata_queued_cmd
*qc
)
3421 struct ata_port
*ap
= qc
->ap
;
3422 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3425 /* start host DMA transaction */
3426 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3427 writeb(dmactl
| ATA_DMA_START
, mmio
+ ATA_DMA_CMD
);
3429 /* Strictly, one may wish to issue a readb() here, to
3430 * flush the mmio write. However, control also passes
3431 * to the hardware at this point, and it will interrupt
3432 * us when we are to resume control. So, in effect,
3433 * we don't care when the mmio write flushes.
3434 * Further, a read of the DMA status register _immediately_
3435 * following the write may not be what certain flaky hardware
3436 * is expected, so I think it is best to not add a readb()
3437 * without first all the MMIO ATA cards/mobos.
3438 * Or maybe I'm just being paranoid.
3443 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3444 * @qc: Info associated with this ATA transaction.
3447 * spin_lock_irqsave(host_set lock)
3450 static void ata_bmdma_setup_pio (struct ata_queued_cmd
*qc
)
3452 struct ata_port
*ap
= qc
->ap
;
3453 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3456 /* load PRD table addr. */
3457 outl(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
3459 /* specify data direction, triple-check start bit is clear */
3460 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3461 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3463 dmactl
|= ATA_DMA_WR
;
3464 outb(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3466 /* issue r/w command */
3467 ap
->ops
->exec_command(ap
, &qc
->tf
);
3471 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3472 * @qc: Info associated with this ATA transaction.
3475 * spin_lock_irqsave(host_set lock)
3478 static void ata_bmdma_start_pio (struct ata_queued_cmd
*qc
)
3480 struct ata_port
*ap
= qc
->ap
;
3483 /* start host DMA transaction */
3484 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3485 outb(dmactl
| ATA_DMA_START
,
3486 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3491 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3492 * @qc: Info associated with this ATA transaction.
3494 * Writes the ATA_DMA_START flag to the DMA command register.
3496 * May be used as the bmdma_start() entry in ata_port_operations.
3499 * spin_lock_irqsave(host_set lock)
3501 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
3503 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3504 ata_bmdma_start_mmio(qc
);
3506 ata_bmdma_start_pio(qc
);
3511 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3512 * @qc: Info associated with this ATA transaction.
3514 * Writes address of PRD table to device's PRD Table Address
3515 * register, sets the DMA control register, and calls
3516 * ops->exec_command() to start the transfer.
3518 * May be used as the bmdma_setup() entry in ata_port_operations.
3521 * spin_lock_irqsave(host_set lock)
3523 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
3525 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3526 ata_bmdma_setup_mmio(qc
);
3528 ata_bmdma_setup_pio(qc
);
3533 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3534 * @ap: Port associated with this ATA transaction.
3536 * Clear interrupt and error flags in DMA status register.
3538 * May be used as the irq_clear() entry in ata_port_operations.
3541 * spin_lock_irqsave(host_set lock)
3544 void ata_bmdma_irq_clear(struct ata_port
*ap
)
3546 if (ap
->flags
& ATA_FLAG_MMIO
) {
3547 void __iomem
*mmio
= ((void __iomem
*) ap
->ioaddr
.bmdma_addr
) + ATA_DMA_STATUS
;
3548 writeb(readb(mmio
), mmio
);
3550 unsigned long addr
= ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
;
3551 outb(inb(addr
), addr
);
3558 * ata_bmdma_status - Read PCI IDE BMDMA status
3559 * @ap: Port associated with this ATA transaction.
3561 * Read and return BMDMA status register.
3563 * May be used as the bmdma_status() entry in ata_port_operations.
3566 * spin_lock_irqsave(host_set lock)
3569 u8
ata_bmdma_status(struct ata_port
*ap
)
3572 if (ap
->flags
& ATA_FLAG_MMIO
) {
3573 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3574 host_stat
= readb(mmio
+ ATA_DMA_STATUS
);
3576 host_stat
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
3582 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3583 * @qc: Command we are ending DMA for
3585 * Clears the ATA_DMA_START flag in the dma control register
3587 * May be used as the bmdma_stop() entry in ata_port_operations.
3590 * spin_lock_irqsave(host_set lock)
3593 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
3595 struct ata_port
*ap
= qc
->ap
;
3596 if (ap
->flags
& ATA_FLAG_MMIO
) {
3597 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3599 /* clear start/stop bit */
3600 writeb(readb(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
3601 mmio
+ ATA_DMA_CMD
);
3603 /* clear start/stop bit */
3604 outb(inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
3605 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3608 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3609 ata_altstatus(ap
); /* dummy read */
3613 * ata_host_intr - Handle host interrupt for given (port, task)
3614 * @ap: Port on which interrupt arrived (possibly...)
3615 * @qc: Taskfile currently active in engine
3617 * Handle host interrupt for given queued command. Currently,
3618 * only DMA interrupts are handled. All other commands are
3619 * handled via polling with interrupts disabled (nIEN bit).
3622 * spin_lock_irqsave(host_set lock)
3625 * One if interrupt was handled, zero if not (shared irq).
3628 inline unsigned int ata_host_intr (struct ata_port
*ap
,
3629 struct ata_queued_cmd
*qc
)
3631 u8 status
, host_stat
;
3633 switch (qc
->tf
.protocol
) {
3636 case ATA_PROT_ATAPI_DMA
:
3637 case ATA_PROT_ATAPI
:
3638 /* check status of DMA engine */
3639 host_stat
= ap
->ops
->bmdma_status(ap
);
3640 VPRINTK("ata%u: host_stat 0x%X\n", ap
->id
, host_stat
);
3642 /* if it's not our irq... */
3643 if (!(host_stat
& ATA_DMA_INTR
))
3646 /* before we do anything else, clear DMA-Start bit */
3647 ap
->ops
->bmdma_stop(qc
);
3651 case ATA_PROT_ATAPI_NODATA
:
3652 case ATA_PROT_NODATA
:
3653 /* check altstatus */
3654 status
= ata_altstatus(ap
);
3655 if (status
& ATA_BUSY
)
3658 /* check main status, clearing INTRQ */
3659 status
= ata_chk_status(ap
);
3660 if (unlikely(status
& ATA_BUSY
))
3662 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3663 ap
->id
, qc
->tf
.protocol
, status
);
3665 /* ack bmdma irq events */
3666 ap
->ops
->irq_clear(ap
);
3668 /* complete taskfile transaction */
3669 ata_qc_complete(qc
, status
);
3676 return 1; /* irq handled */
3679 ap
->stats
.idle_irq
++;
3682 if ((ap
->stats
.idle_irq
% 1000) == 0) {
3684 ata_irq_ack(ap
, 0); /* debug trap */
3685 printk(KERN_WARNING
"ata%d: irq trap\n", ap
->id
);
3688 return 0; /* irq not handled */
3692 * ata_interrupt - Default ATA host interrupt handler
3693 * @irq: irq line (unused)
3694 * @dev_instance: pointer to our ata_host_set information structure
3697 * Default interrupt handler for PCI IDE devices. Calls
3698 * ata_host_intr() for each port that is not disabled.
3701 * Obtains host_set lock during operation.
3704 * IRQ_NONE or IRQ_HANDLED.
3708 irqreturn_t
ata_interrupt (int irq
, void *dev_instance
, struct pt_regs
*regs
)
3710 struct ata_host_set
*host_set
= dev_instance
;
3712 unsigned int handled
= 0;
3713 unsigned long flags
;
3715 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3716 spin_lock_irqsave(&host_set
->lock
, flags
);
3718 for (i
= 0; i
< host_set
->n_ports
; i
++) {
3719 struct ata_port
*ap
;
3721 ap
= host_set
->ports
[i
];
3723 !(ap
->flags
& (ATA_FLAG_PORT_DISABLED
| ATA_FLAG_NOINTR
))) {
3724 struct ata_queued_cmd
*qc
;
3726 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3727 if (qc
&& (!(qc
->tf
.ctl
& ATA_NIEN
)) &&
3728 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
3729 handled
|= ata_host_intr(ap
, qc
);
3733 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3735 return IRQ_RETVAL(handled
);
3739 * atapi_packet_task - Write CDB bytes to hardware
3740 * @_data: Port to which ATAPI device is attached.
3742 * When device has indicated its readiness to accept
3743 * a CDB, this function is called. Send the CDB.
3744 * If DMA is to be performed, exit immediately.
3745 * Otherwise, we are in polling mode, so poll
3746 * status under operation succeeds or fails.
3749 * Kernel thread context (may sleep)
3752 static void atapi_packet_task(void *_data
)
3754 struct ata_port
*ap
= _data
;
3755 struct ata_queued_cmd
*qc
;
3758 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3760 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
3762 /* sleep-wait for BSY to clear */
3763 DPRINTK("busy wait\n");
3764 if (ata_busy_sleep(ap
, ATA_TMOUT_CDB_QUICK
, ATA_TMOUT_CDB
))
3767 /* make sure DRQ is set */
3768 status
= ata_chk_status(ap
);
3769 if ((status
& (ATA_BUSY
| ATA_DRQ
)) != ATA_DRQ
)
3773 DPRINTK("send cdb\n");
3774 assert(ap
->cdb_len
>= 12);
3776 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
||
3777 qc
->tf
.protocol
== ATA_PROT_ATAPI_NODATA
) {
3778 unsigned long flags
;
3780 /* Once we're done issuing command and kicking bmdma,
3781 * irq handler takes over. To not lose irq, we need
3782 * to clear NOINTR flag before sending cdb, but
3783 * interrupt handler shouldn't be invoked before we're
3784 * finished. Hence, the following locking.
3786 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
3787 ap
->flags
&= ~ATA_FLAG_NOINTR
;
3788 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
3789 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
)
3790 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3791 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
3793 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
3795 /* PIO commands are handled by polling */
3796 ap
->pio_task_state
= PIO_ST
;
3797 queue_work(ata_wq
, &ap
->pio_task
);
3803 ata_poll_qc_complete(qc
, ATA_ERR
);
3808 * ata_port_start - Set port up for dma.
3809 * @ap: Port to initialize
3811 * Called just after data structures for each port are
3812 * initialized. Allocates space for PRD table.
3814 * May be used as the port_start() entry in ata_port_operations.
3819 int ata_port_start (struct ata_port
*ap
)
3821 struct device
*dev
= ap
->host_set
->dev
;
3823 ap
->prd
= dma_alloc_coherent(dev
, ATA_PRD_TBL_SZ
, &ap
->prd_dma
, GFP_KERNEL
);
3827 DPRINTK("prd alloc, virt %p, dma %llx\n", ap
->prd
, (unsigned long long) ap
->prd_dma
);
3834 * ata_port_stop - Undo ata_port_start()
3835 * @ap: Port to shut down
3837 * Frees the PRD table.
3839 * May be used as the port_stop() entry in ata_port_operations.
3844 void ata_port_stop (struct ata_port
*ap
)
3846 struct device
*dev
= ap
->host_set
->dev
;
3848 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
3851 void ata_host_stop (struct ata_host_set
*host_set
)
3853 if (host_set
->mmio_base
)
3854 iounmap(host_set
->mmio_base
);
3859 * ata_host_remove - Unregister SCSI host structure with upper layers
3860 * @ap: Port to unregister
3861 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3866 static void ata_host_remove(struct ata_port
*ap
, unsigned int do_unregister
)
3868 struct Scsi_Host
*sh
= ap
->host
;
3873 scsi_remove_host(sh
);
3875 ap
->ops
->port_stop(ap
);
3879 * ata_host_init - Initialize an ata_port structure
3880 * @ap: Structure to initialize
3881 * @host: associated SCSI mid-layer structure
3882 * @host_set: Collection of hosts to which @ap belongs
3883 * @ent: Probe information provided by low-level driver
3884 * @port_no: Port number associated with this ata_port
3886 * Initialize a new ata_port structure, and its associated
3890 * Inherited from caller.
3894 static void ata_host_init(struct ata_port
*ap
, struct Scsi_Host
*host
,
3895 struct ata_host_set
*host_set
,
3896 struct ata_probe_ent
*ent
, unsigned int port_no
)
3902 host
->max_channel
= 1;
3903 host
->unique_id
= ata_unique_id
++;
3904 host
->max_cmd_len
= 12;
3906 scsi_assign_lock(host
, &host_set
->lock
);
3908 ap
->flags
= ATA_FLAG_PORT_DISABLED
;
3909 ap
->id
= host
->unique_id
;
3911 ap
->ctl
= ATA_DEVCTL_OBS
;
3912 ap
->host_set
= host_set
;
3913 ap
->port_no
= port_no
;
3915 ent
->legacy_mode
? ent
->hard_port_no
: port_no
;
3916 ap
->pio_mask
= ent
->pio_mask
;
3917 ap
->mwdma_mask
= ent
->mwdma_mask
;
3918 ap
->udma_mask
= ent
->udma_mask
;
3919 ap
->flags
|= ent
->host_flags
;
3920 ap
->ops
= ent
->port_ops
;
3921 ap
->cbl
= ATA_CBL_NONE
;
3922 ap
->active_tag
= ATA_TAG_POISON
;
3923 ap
->last_ctl
= 0xFF;
3925 INIT_WORK(&ap
->packet_task
, atapi_packet_task
, ap
);
3926 INIT_WORK(&ap
->pio_task
, ata_pio_task
, ap
);
3928 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
3929 ap
->device
[i
].devno
= i
;
3932 ap
->stats
.unhandled_irq
= 1;
3933 ap
->stats
.idle_irq
= 1;
3936 memcpy(&ap
->ioaddr
, &ent
->port
[port_no
], sizeof(struct ata_ioports
));
3940 * ata_host_add - Attach low-level ATA driver to system
3941 * @ent: Information provided by low-level driver
3942 * @host_set: Collections of ports to which we add
3943 * @port_no: Port number associated with this host
3945 * Attach low-level ATA driver to system.
3948 * PCI/etc. bus probe sem.
3951 * New ata_port on success, for NULL on error.
3955 static struct ata_port
* ata_host_add(struct ata_probe_ent
*ent
,
3956 struct ata_host_set
*host_set
,
3957 unsigned int port_no
)
3959 struct Scsi_Host
*host
;
3960 struct ata_port
*ap
;
3964 host
= scsi_host_alloc(ent
->sht
, sizeof(struct ata_port
));
3968 ap
= (struct ata_port
*) &host
->hostdata
[0];
3970 ata_host_init(ap
, host
, host_set
, ent
, port_no
);
3972 rc
= ap
->ops
->port_start(ap
);
3979 scsi_host_put(host
);
3984 * ata_device_add - Register hardware device with ATA and SCSI layers
3985 * @ent: Probe information describing hardware device to be registered
3987 * This function processes the information provided in the probe
3988 * information struct @ent, allocates the necessary ATA and SCSI
3989 * host information structures, initializes them, and registers
3990 * everything with requisite kernel subsystems.
3992 * This function requests irqs, probes the ATA bus, and probes
3996 * PCI/etc. bus probe sem.
3999 * Number of ports registered. Zero on error (no ports registered).
4003 int ata_device_add(struct ata_probe_ent
*ent
)
4005 unsigned int count
= 0, i
;
4006 struct device
*dev
= ent
->dev
;
4007 struct ata_host_set
*host_set
;
4010 /* alloc a container for our list of ATA ports (buses) */
4011 host_set
= kmalloc(sizeof(struct ata_host_set
) +
4012 (ent
->n_ports
* sizeof(void *)), GFP_KERNEL
);
4015 memset(host_set
, 0, sizeof(struct ata_host_set
) + (ent
->n_ports
* sizeof(void *)));
4016 spin_lock_init(&host_set
->lock
);
4018 host_set
->dev
= dev
;
4019 host_set
->n_ports
= ent
->n_ports
;
4020 host_set
->irq
= ent
->irq
;
4021 host_set
->mmio_base
= ent
->mmio_base
;
4022 host_set
->private_data
= ent
->private_data
;
4023 host_set
->ops
= ent
->port_ops
;
4025 /* register each port bound to this device */
4026 for (i
= 0; i
< ent
->n_ports
; i
++) {
4027 struct ata_port
*ap
;
4028 unsigned long xfer_mode_mask
;
4030 ap
= ata_host_add(ent
, host_set
, i
);
4034 host_set
->ports
[i
] = ap
;
4035 xfer_mode_mask
=(ap
->udma_mask
<< ATA_SHIFT_UDMA
) |
4036 (ap
->mwdma_mask
<< ATA_SHIFT_MWDMA
) |
4037 (ap
->pio_mask
<< ATA_SHIFT_PIO
);
4039 /* print per-port info to dmesg */
4040 printk(KERN_INFO
"ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4041 "bmdma 0x%lX irq %lu\n",
4043 ap
->flags
& ATA_FLAG_SATA
? 'S' : 'P',
4044 ata_mode_string(xfer_mode_mask
),
4045 ap
->ioaddr
.cmd_addr
,
4046 ap
->ioaddr
.ctl_addr
,
4047 ap
->ioaddr
.bmdma_addr
,
4051 host_set
->ops
->irq_clear(ap
);
4060 /* obtain irq, that is shared between channels */
4061 if (request_irq(ent
->irq
, ent
->port_ops
->irq_handler
, ent
->irq_flags
,
4062 DRV_NAME
, host_set
))
4065 /* perform each probe synchronously */
4066 DPRINTK("probe begin\n");
4067 for (i
= 0; i
< count
; i
++) {
4068 struct ata_port
*ap
;
4071 ap
= host_set
->ports
[i
];
4073 DPRINTK("ata%u: probe begin\n", ap
->id
);
4074 rc
= ata_bus_probe(ap
);
4075 DPRINTK("ata%u: probe end\n", ap
->id
);
4078 /* FIXME: do something useful here?
4079 * Current libata behavior will
4080 * tear down everything when
4081 * the module is removed
4082 * or the h/w is unplugged.
4086 rc
= scsi_add_host(ap
->host
, dev
);
4088 printk(KERN_ERR
"ata%u: scsi_add_host failed\n",
4090 /* FIXME: do something useful here */
4091 /* FIXME: handle unconditional calls to
4092 * scsi_scan_host and ata_host_remove, below,
4098 /* probes are done, now scan each port's disk(s) */
4099 DPRINTK("probe begin\n");
4100 for (i
= 0; i
< count
; i
++) {
4101 struct ata_port
*ap
= host_set
->ports
[i
];
4103 scsi_scan_host(ap
->host
);
4106 dev_set_drvdata(dev
, host_set
);
4108 VPRINTK("EXIT, returning %u\n", ent
->n_ports
);
4109 return ent
->n_ports
; /* success */
4112 for (i
= 0; i
< count
; i
++) {
4113 ata_host_remove(host_set
->ports
[i
], 1);
4114 scsi_host_put(host_set
->ports
[i
]->host
);
4117 VPRINTK("EXIT, returning 0\n");
4122 * ata_scsi_release - SCSI layer callback hook for host unload
4123 * @host: libata host to be unloaded
4125 * Performs all duties necessary to shut down a libata port...
4126 * Kill port kthread, disable port, and release resources.
4129 * Inherited from SCSI layer.
4135 int ata_scsi_release(struct Scsi_Host
*host
)
4137 struct ata_port
*ap
= (struct ata_port
*) &host
->hostdata
[0];
4141 ap
->ops
->port_disable(ap
);
4142 ata_host_remove(ap
, 0);
4149 * ata_std_ports - initialize ioaddr with standard port offsets.
4150 * @ioaddr: IO address structure to be initialized
4152 * Utility function which initializes data_addr, error_addr,
4153 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4154 * device_addr, status_addr, and command_addr to standard offsets
4155 * relative to cmd_addr.
4157 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4160 void ata_std_ports(struct ata_ioports
*ioaddr
)
4162 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
4163 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
4164 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
4165 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
4166 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
4167 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
4168 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
4169 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
4170 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
4171 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
4174 static struct ata_probe_ent
*
4175 ata_probe_ent_alloc(struct device
*dev
, struct ata_port_info
*port
)
4177 struct ata_probe_ent
*probe_ent
;
4179 probe_ent
= kmalloc(sizeof(*probe_ent
), GFP_KERNEL
);
4181 printk(KERN_ERR DRV_NAME
"(%s): out of memory\n",
4182 kobject_name(&(dev
->kobj
)));
4186 memset(probe_ent
, 0, sizeof(*probe_ent
));
4188 INIT_LIST_HEAD(&probe_ent
->node
);
4189 probe_ent
->dev
= dev
;
4191 probe_ent
->sht
= port
->sht
;
4192 probe_ent
->host_flags
= port
->host_flags
;
4193 probe_ent
->pio_mask
= port
->pio_mask
;
4194 probe_ent
->mwdma_mask
= port
->mwdma_mask
;
4195 probe_ent
->udma_mask
= port
->udma_mask
;
4196 probe_ent
->port_ops
= port
->port_ops
;
4204 * ata_pci_init_native_mode - Initialize native-mode driver
4205 * @pdev: pci device to be initialized
4206 * @port: array[2] of pointers to port info structures.
4208 * Utility function which allocates and initializes an
4209 * ata_probe_ent structure for a standard dual-port
4210 * PIO-based IDE controller. The returned ata_probe_ent
4211 * structure can be passed to ata_device_add(). The returned
4212 * ata_probe_ent structure should then be freed with kfree().
4216 struct ata_probe_ent
*
4217 ata_pci_init_native_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
)
4219 struct ata_probe_ent
*probe_ent
=
4220 ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
4224 probe_ent
->n_ports
= 2;
4225 probe_ent
->irq
= pdev
->irq
;
4226 probe_ent
->irq_flags
= SA_SHIRQ
;
4228 probe_ent
->port
[0].cmd_addr
= pci_resource_start(pdev
, 0);
4229 probe_ent
->port
[0].altstatus_addr
=
4230 probe_ent
->port
[0].ctl_addr
=
4231 pci_resource_start(pdev
, 1) | ATA_PCI_CTL_OFS
;
4232 probe_ent
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4);
4234 probe_ent
->port
[1].cmd_addr
= pci_resource_start(pdev
, 2);
4235 probe_ent
->port
[1].altstatus_addr
=
4236 probe_ent
->port
[1].ctl_addr
=
4237 pci_resource_start(pdev
, 3) | ATA_PCI_CTL_OFS
;
4238 probe_ent
->port
[1].bmdma_addr
= pci_resource_start(pdev
, 4) + 8;
4240 ata_std_ports(&probe_ent
->port
[0]);
4241 ata_std_ports(&probe_ent
->port
[1]);
4246 static struct ata_probe_ent
*
4247 ata_pci_init_legacy_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
,
4248 struct ata_probe_ent
**ppe2
)
4250 struct ata_probe_ent
*probe_ent
, *probe_ent2
;
4252 probe_ent
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
4255 probe_ent2
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[1]);
4261 probe_ent
->n_ports
= 1;
4262 probe_ent
->irq
= 14;
4264 probe_ent
->hard_port_no
= 0;
4265 probe_ent
->legacy_mode
= 1;
4267 probe_ent2
->n_ports
= 1;
4268 probe_ent2
->irq
= 15;
4270 probe_ent2
->hard_port_no
= 1;
4271 probe_ent2
->legacy_mode
= 1;
4273 probe_ent
->port
[0].cmd_addr
= 0x1f0;
4274 probe_ent
->port
[0].altstatus_addr
=
4275 probe_ent
->port
[0].ctl_addr
= 0x3f6;
4276 probe_ent
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4);
4278 probe_ent2
->port
[0].cmd_addr
= 0x170;
4279 probe_ent2
->port
[0].altstatus_addr
=
4280 probe_ent2
->port
[0].ctl_addr
= 0x376;
4281 probe_ent2
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4)+8;
4283 ata_std_ports(&probe_ent
->port
[0]);
4284 ata_std_ports(&probe_ent2
->port
[0]);
4291 * ata_pci_init_one - Initialize/register PCI IDE host controller
4292 * @pdev: Controller to be initialized
4293 * @port_info: Information from low-level host driver
4294 * @n_ports: Number of ports attached to host controller
4296 * This is a helper function which can be called from a driver's
4297 * xxx_init_one() probe function if the hardware uses traditional
4298 * IDE taskfile registers.
4300 * This function calls pci_enable_device(), reserves its register
4301 * regions, sets the dma mask, enables bus master mode, and calls
4305 * Inherited from PCI layer (may sleep).
4308 * Zero on success, negative on errno-based value on error.
4312 int ata_pci_init_one (struct pci_dev
*pdev
, struct ata_port_info
**port_info
,
4313 unsigned int n_ports
)
4315 struct ata_probe_ent
*probe_ent
, *probe_ent2
= NULL
;
4316 struct ata_port_info
*port
[2];
4318 unsigned int legacy_mode
= 0;
4319 int disable_dev_on_err
= 1;
4324 port
[0] = port_info
[0];
4326 port
[1] = port_info
[1];
4330 if ((port
[0]->host_flags
& ATA_FLAG_NO_LEGACY
) == 0
4331 && (pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
4332 /* TODO: support transitioning to native mode? */
4333 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
4334 mask
= (1 << 2) | (1 << 0);
4335 if ((tmp8
& mask
) != mask
)
4336 legacy_mode
= (1 << 3);
4340 if ((!legacy_mode
) && (n_ports
> 1)) {
4341 printk(KERN_ERR
"ata: BUG: native mode, n_ports > 1\n");
4345 rc
= pci_enable_device(pdev
);
4349 rc
= pci_request_regions(pdev
, DRV_NAME
);
4351 disable_dev_on_err
= 0;
4356 if (!request_region(0x1f0, 8, "libata")) {
4357 struct resource
*conflict
, res
;
4359 res
.end
= 0x1f0 + 8 - 1;
4360 conflict
= ____request_resource(&ioport_resource
, &res
);
4361 if (!strcmp(conflict
->name
, "libata"))
4362 legacy_mode
|= (1 << 0);
4364 disable_dev_on_err
= 0;
4365 printk(KERN_WARNING
"ata: 0x1f0 IDE port busy\n");
4368 legacy_mode
|= (1 << 0);
4370 if (!request_region(0x170, 8, "libata")) {
4371 struct resource
*conflict
, res
;
4373 res
.end
= 0x170 + 8 - 1;
4374 conflict
= ____request_resource(&ioport_resource
, &res
);
4375 if (!strcmp(conflict
->name
, "libata"))
4376 legacy_mode
|= (1 << 1);
4378 disable_dev_on_err
= 0;
4379 printk(KERN_WARNING
"ata: 0x170 IDE port busy\n");
4382 legacy_mode
|= (1 << 1);
4385 /* we have legacy mode, but all ports are unavailable */
4386 if (legacy_mode
== (1 << 3)) {
4388 goto err_out_regions
;
4391 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
4393 goto err_out_regions
;
4394 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
4396 goto err_out_regions
;
4399 probe_ent
= ata_pci_init_legacy_mode(pdev
, port
, &probe_ent2
);
4401 probe_ent
= ata_pci_init_native_mode(pdev
, port
);
4404 goto err_out_regions
;
4407 pci_set_master(pdev
);
4409 /* FIXME: check ata_device_add return */
4411 if (legacy_mode
& (1 << 0))
4412 ata_device_add(probe_ent
);
4413 if (legacy_mode
& (1 << 1))
4414 ata_device_add(probe_ent2
);
4416 ata_device_add(probe_ent
);
4424 if (legacy_mode
& (1 << 0))
4425 release_region(0x1f0, 8);
4426 if (legacy_mode
& (1 << 1))
4427 release_region(0x170, 8);
4428 pci_release_regions(pdev
);
4430 if (disable_dev_on_err
)
4431 pci_disable_device(pdev
);
4436 * ata_pci_remove_one - PCI layer callback for device removal
4437 * @pdev: PCI device that was removed
4439 * PCI layer indicates to libata via this hook that
4440 * hot-unplug or module unload event has occured.
4441 * Handle this by unregistering all objects associated
4442 * with this PCI device. Free those objects. Then finally
4443 * release PCI resources and disable device.
4446 * Inherited from PCI layer (may sleep).
4449 void ata_pci_remove_one (struct pci_dev
*pdev
)
4451 struct device
*dev
= pci_dev_to_dev(pdev
);
4452 struct ata_host_set
*host_set
= dev_get_drvdata(dev
);
4453 struct ata_port
*ap
;
4456 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4457 ap
= host_set
->ports
[i
];
4459 scsi_remove_host(ap
->host
);
4462 free_irq(host_set
->irq
, host_set
);
4464 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4465 ap
= host_set
->ports
[i
];
4467 ata_scsi_release(ap
->host
);
4469 if ((ap
->flags
& ATA_FLAG_NO_LEGACY
) == 0) {
4470 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
4472 if (ioaddr
->cmd_addr
== 0x1f0)
4473 release_region(0x1f0, 8);
4474 else if (ioaddr
->cmd_addr
== 0x170)
4475 release_region(0x170, 8);
4478 scsi_host_put(ap
->host
);
4481 if (host_set
->ops
->host_stop
)
4482 host_set
->ops
->host_stop(host_set
);
4486 pci_release_regions(pdev
);
4487 pci_disable_device(pdev
);
4488 dev_set_drvdata(dev
, NULL
);
4491 /* move to PCI subsystem */
4492 int pci_test_config_bits(struct pci_dev
*pdev
, struct pci_bits
*bits
)
4494 unsigned long tmp
= 0;
4496 switch (bits
->width
) {
4499 pci_read_config_byte(pdev
, bits
->reg
, &tmp8
);
4505 pci_read_config_word(pdev
, bits
->reg
, &tmp16
);
4511 pci_read_config_dword(pdev
, bits
->reg
, &tmp32
);
4522 return (tmp
== bits
->val
) ? 1 : 0;
4524 #endif /* CONFIG_PCI */
4527 static int __init
ata_init(void)
4529 ata_wq
= create_workqueue("ata");
4533 printk(KERN_DEBUG
"libata version " DRV_VERSION
" loaded.\n");
4537 static void __exit
ata_exit(void)
4539 destroy_workqueue(ata_wq
);
4542 module_init(ata_init
);
4543 module_exit(ata_exit
);
4546 * libata is essentially a library of internal helper functions for
4547 * low-level ATA host controller drivers. As such, the API/ABI is
4548 * likely to change as new drivers are added and updated.
4549 * Do not depend on ABI/API stability.
4552 EXPORT_SYMBOL_GPL(ata_std_bios_param
);
4553 EXPORT_SYMBOL_GPL(ata_std_ports
);
4554 EXPORT_SYMBOL_GPL(ata_device_add
);
4555 EXPORT_SYMBOL_GPL(ata_sg_init
);
4556 EXPORT_SYMBOL_GPL(ata_sg_init_one
);
4557 EXPORT_SYMBOL_GPL(ata_qc_complete
);
4558 EXPORT_SYMBOL_GPL(ata_qc_issue_prot
);
4559 EXPORT_SYMBOL_GPL(ata_eng_timeout
);
4560 EXPORT_SYMBOL_GPL(ata_tf_load
);
4561 EXPORT_SYMBOL_GPL(ata_tf_read
);
4562 EXPORT_SYMBOL_GPL(ata_noop_dev_select
);
4563 EXPORT_SYMBOL_GPL(ata_std_dev_select
);
4564 EXPORT_SYMBOL_GPL(ata_tf_to_fis
);
4565 EXPORT_SYMBOL_GPL(ata_tf_from_fis
);
4566 EXPORT_SYMBOL_GPL(ata_check_status
);
4567 EXPORT_SYMBOL_GPL(ata_altstatus
);
4568 EXPORT_SYMBOL_GPL(ata_chk_err
);
4569 EXPORT_SYMBOL_GPL(ata_exec_command
);
4570 EXPORT_SYMBOL_GPL(ata_port_start
);
4571 EXPORT_SYMBOL_GPL(ata_port_stop
);
4572 EXPORT_SYMBOL_GPL(ata_host_stop
);
4573 EXPORT_SYMBOL_GPL(ata_interrupt
);
4574 EXPORT_SYMBOL_GPL(ata_qc_prep
);
4575 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
4576 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
4577 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear
);
4578 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
4579 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
4580 EXPORT_SYMBOL_GPL(ata_port_probe
);
4581 EXPORT_SYMBOL_GPL(sata_phy_reset
);
4582 EXPORT_SYMBOL_GPL(__sata_phy_reset
);
4583 EXPORT_SYMBOL_GPL(ata_bus_reset
);
4584 EXPORT_SYMBOL_GPL(ata_port_disable
);
4585 EXPORT_SYMBOL_GPL(ata_scsi_ioctl
);
4586 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd
);
4587 EXPORT_SYMBOL_GPL(ata_scsi_error
);
4588 EXPORT_SYMBOL_GPL(ata_scsi_slave_config
);
4589 EXPORT_SYMBOL_GPL(ata_scsi_release
);
4590 EXPORT_SYMBOL_GPL(ata_host_intr
);
4591 EXPORT_SYMBOL_GPL(ata_dev_classify
);
4592 EXPORT_SYMBOL_GPL(ata_dev_id_string
);
4593 EXPORT_SYMBOL_GPL(ata_dev_config
);
4594 EXPORT_SYMBOL_GPL(ata_scsi_simulate
);
4597 EXPORT_SYMBOL_GPL(pci_test_config_bits
);
4598 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode
);
4599 EXPORT_SYMBOL_GPL(ata_pci_init_one
);
4600 EXPORT_SYMBOL_GPL(ata_pci_remove_one
);
4601 #endif /* CONFIG_PCI */