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_dev_init_params(struct ata_port
*ap
, struct ata_device
*dev
);
66 static void ata_set_mode(struct ata_port
*ap
);
67 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
);
68 static unsigned int ata_get_mode_mask(struct ata_port
*ap
, int shift
);
69 static int fgb(u32 bitmap
);
70 static int ata_choose_xfer_mode(struct ata_port
*ap
,
72 unsigned int *xfer_shift_out
);
73 static int ata_qc_complete_noop(struct ata_queued_cmd
*qc
, u8 drv_stat
);
74 static void __ata_qc_complete(struct ata_queued_cmd
*qc
);
76 static unsigned int ata_unique_id
= 1;
77 static struct workqueue_struct
*ata_wq
;
79 int atapi_enabled
= 0;
80 module_param(atapi_enabled
, int, 0444);
81 MODULE_PARM_DESC(atapi_enabled
, "Enable discovery of ATAPI devices (0=off, 1=on)");
83 MODULE_AUTHOR("Jeff Garzik");
84 MODULE_DESCRIPTION("Library module for ATA devices");
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(DRV_VERSION
);
89 * ata_tf_load - send taskfile registers to host controller
90 * @ap: Port to which output is sent
91 * @tf: ATA taskfile register set
93 * Outputs ATA taskfile to standard ATA host controller.
96 * Inherited from caller.
99 static void ata_tf_load_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
101 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
102 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
104 if (tf
->ctl
!= ap
->last_ctl
) {
105 outb(tf
->ctl
, ioaddr
->ctl_addr
);
106 ap
->last_ctl
= tf
->ctl
;
110 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
111 outb(tf
->hob_feature
, ioaddr
->feature_addr
);
112 outb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
113 outb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
114 outb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
115 outb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
116 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
125 outb(tf
->feature
, ioaddr
->feature_addr
);
126 outb(tf
->nsect
, ioaddr
->nsect_addr
);
127 outb(tf
->lbal
, ioaddr
->lbal_addr
);
128 outb(tf
->lbam
, ioaddr
->lbam_addr
);
129 outb(tf
->lbah
, ioaddr
->lbah_addr
);
130 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
138 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
139 outb(tf
->device
, ioaddr
->device_addr
);
140 VPRINTK("device 0x%X\n", tf
->device
);
147 * ata_tf_load_mmio - send taskfile registers to host controller
148 * @ap: Port to which output is sent
149 * @tf: ATA taskfile register set
151 * Outputs ATA taskfile to standard ATA host controller using MMIO.
154 * Inherited from caller.
157 static void ata_tf_load_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
159 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
160 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
162 if (tf
->ctl
!= ap
->last_ctl
) {
163 writeb(tf
->ctl
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
164 ap
->last_ctl
= tf
->ctl
;
168 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
169 writeb(tf
->hob_feature
, (void __iomem
*) ioaddr
->feature_addr
);
170 writeb(tf
->hob_nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
171 writeb(tf
->hob_lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
172 writeb(tf
->hob_lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
173 writeb(tf
->hob_lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
174 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
183 writeb(tf
->feature
, (void __iomem
*) ioaddr
->feature_addr
);
184 writeb(tf
->nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
185 writeb(tf
->lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
186 writeb(tf
->lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
187 writeb(tf
->lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
188 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
196 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
197 writeb(tf
->device
, (void __iomem
*) ioaddr
->device_addr
);
198 VPRINTK("device 0x%X\n", tf
->device
);
206 * ata_tf_load - send taskfile registers to host controller
207 * @ap: Port to which output is sent
208 * @tf: ATA taskfile register set
210 * Outputs ATA taskfile to standard ATA host controller using MMIO
211 * or PIO as indicated by the ATA_FLAG_MMIO flag.
212 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
213 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
214 * hob_lbal, hob_lbam, and hob_lbah.
216 * This function waits for idle (!BUSY and !DRQ) after writing
217 * registers. If the control register has a new value, this
218 * function also waits for idle after writing control and before
219 * writing the remaining registers.
221 * May be used as the tf_load() entry in ata_port_operations.
224 * Inherited from caller.
226 void ata_tf_load(struct ata_port
*ap
, struct ata_taskfile
*tf
)
228 if (ap
->flags
& ATA_FLAG_MMIO
)
229 ata_tf_load_mmio(ap
, tf
);
231 ata_tf_load_pio(ap
, tf
);
235 * ata_exec_command_pio - issue ATA command to host controller
236 * @ap: port to which command is being issued
237 * @tf: ATA taskfile register set
239 * Issues PIO write to ATA command register, with proper
240 * synchronization with interrupt handler / other threads.
243 * spin_lock_irqsave(host_set lock)
246 static void ata_exec_command_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
248 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
250 outb(tf
->command
, ap
->ioaddr
.command_addr
);
256 * ata_exec_command_mmio - issue ATA command to host controller
257 * @ap: port to which command is being issued
258 * @tf: ATA taskfile register set
260 * Issues MMIO write to ATA command register, with proper
261 * synchronization with interrupt handler / other threads.
264 * spin_lock_irqsave(host_set lock)
267 static void ata_exec_command_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
269 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
271 writeb(tf
->command
, (void __iomem
*) ap
->ioaddr
.command_addr
);
277 * ata_exec_command - issue ATA command to host controller
278 * @ap: port to which command is being issued
279 * @tf: ATA taskfile register set
281 * Issues PIO/MMIO write to ATA command register, with proper
282 * synchronization with interrupt handler / other threads.
285 * spin_lock_irqsave(host_set lock)
287 void ata_exec_command(struct ata_port
*ap
, struct ata_taskfile
*tf
)
289 if (ap
->flags
& ATA_FLAG_MMIO
)
290 ata_exec_command_mmio(ap
, tf
);
292 ata_exec_command_pio(ap
, tf
);
296 * ata_exec - issue ATA command to host controller
297 * @ap: port to which command is being issued
298 * @tf: ATA taskfile register set
300 * Issues PIO/MMIO write to ATA command register, with proper
301 * synchronization with interrupt handler / other threads.
304 * Obtains host_set lock.
307 static inline void ata_exec(struct ata_port
*ap
, struct ata_taskfile
*tf
)
311 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
312 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
313 ap
->ops
->exec_command(ap
, tf
);
314 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
318 * ata_tf_to_host - issue ATA taskfile to host controller
319 * @ap: port to which command is being issued
320 * @tf: ATA taskfile register set
322 * Issues ATA taskfile register set to ATA host controller,
323 * with proper synchronization with interrupt handler and
327 * Obtains host_set lock.
330 static void ata_tf_to_host(struct ata_port
*ap
, struct ata_taskfile
*tf
)
332 ap
->ops
->tf_load(ap
, tf
);
338 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
339 * @ap: port to which command is being issued
340 * @tf: ATA taskfile register set
342 * Issues ATA taskfile register set to ATA host controller,
343 * with proper synchronization with interrupt handler and
347 * spin_lock_irqsave(host_set lock)
350 void ata_tf_to_host_nolock(struct ata_port
*ap
, struct ata_taskfile
*tf
)
352 ap
->ops
->tf_load(ap
, tf
);
353 ap
->ops
->exec_command(ap
, tf
);
357 * ata_tf_read_pio - input device's ATA taskfile shadow registers
358 * @ap: Port from which input is read
359 * @tf: ATA taskfile register set for storing input
361 * Reads ATA taskfile registers for currently-selected device
365 * Inherited from caller.
368 static void ata_tf_read_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
370 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
372 tf
->nsect
= inb(ioaddr
->nsect_addr
);
373 tf
->lbal
= inb(ioaddr
->lbal_addr
);
374 tf
->lbam
= inb(ioaddr
->lbam_addr
);
375 tf
->lbah
= inb(ioaddr
->lbah_addr
);
376 tf
->device
= inb(ioaddr
->device_addr
);
378 if (tf
->flags
& ATA_TFLAG_LBA48
) {
379 outb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
380 tf
->hob_feature
= inb(ioaddr
->error_addr
);
381 tf
->hob_nsect
= inb(ioaddr
->nsect_addr
);
382 tf
->hob_lbal
= inb(ioaddr
->lbal_addr
);
383 tf
->hob_lbam
= inb(ioaddr
->lbam_addr
);
384 tf
->hob_lbah
= inb(ioaddr
->lbah_addr
);
389 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
390 * @ap: Port from which input is read
391 * @tf: ATA taskfile register set for storing input
393 * Reads ATA taskfile registers for currently-selected device
397 * Inherited from caller.
400 static void ata_tf_read_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
402 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
404 tf
->nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
405 tf
->lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
406 tf
->lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
407 tf
->lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
408 tf
->device
= readb((void __iomem
*)ioaddr
->device_addr
);
410 if (tf
->flags
& ATA_TFLAG_LBA48
) {
411 writeb(tf
->ctl
| ATA_HOB
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
412 tf
->hob_feature
= readb((void __iomem
*)ioaddr
->error_addr
);
413 tf
->hob_nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
414 tf
->hob_lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
415 tf
->hob_lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
416 tf
->hob_lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
422 * ata_tf_read - input device's ATA taskfile shadow registers
423 * @ap: Port from which input is read
424 * @tf: ATA taskfile register set for storing input
426 * Reads ATA taskfile registers for currently-selected device
429 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
430 * is set, also reads the hob registers.
432 * May be used as the tf_read() entry in ata_port_operations.
435 * Inherited from caller.
437 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
439 if (ap
->flags
& ATA_FLAG_MMIO
)
440 ata_tf_read_mmio(ap
, tf
);
442 ata_tf_read_pio(ap
, tf
);
446 * ata_check_status_pio - Read device status reg & clear interrupt
447 * @ap: port where the device is
449 * Reads ATA taskfile status register for currently-selected device
450 * and return its value. This also clears pending interrupts
454 * Inherited from caller.
456 static u8
ata_check_status_pio(struct ata_port
*ap
)
458 return inb(ap
->ioaddr
.status_addr
);
462 * ata_check_status_mmio - Read device status reg & clear interrupt
463 * @ap: port where the device is
465 * Reads ATA taskfile status register for currently-selected device
466 * via MMIO and return its value. This also clears pending interrupts
470 * Inherited from caller.
472 static u8
ata_check_status_mmio(struct ata_port
*ap
)
474 return readb((void __iomem
*) ap
->ioaddr
.status_addr
);
479 * ata_check_status - Read device status reg & clear interrupt
480 * @ap: port where the device is
482 * Reads ATA taskfile status register for currently-selected device
483 * and return its value. This also clears pending interrupts
486 * May be used as the check_status() entry in ata_port_operations.
489 * Inherited from caller.
491 u8
ata_check_status(struct ata_port
*ap
)
493 if (ap
->flags
& ATA_FLAG_MMIO
)
494 return ata_check_status_mmio(ap
);
495 return ata_check_status_pio(ap
);
500 * ata_altstatus - Read device alternate status reg
501 * @ap: port where the device is
503 * Reads ATA taskfile alternate status register for
504 * currently-selected device and return its value.
506 * Note: may NOT be used as the check_altstatus() entry in
507 * ata_port_operations.
510 * Inherited from caller.
512 u8
ata_altstatus(struct ata_port
*ap
)
514 if (ap
->ops
->check_altstatus
)
515 return ap
->ops
->check_altstatus(ap
);
517 if (ap
->flags
& ATA_FLAG_MMIO
)
518 return readb((void __iomem
*)ap
->ioaddr
.altstatus_addr
);
519 return inb(ap
->ioaddr
.altstatus_addr
);
524 * ata_chk_err - Read device error reg
525 * @ap: port where the device is
527 * Reads ATA taskfile error register for
528 * currently-selected device and return its value.
530 * Note: may NOT be used as the check_err() entry in
531 * ata_port_operations.
534 * Inherited from caller.
536 u8
ata_chk_err(struct ata_port
*ap
)
538 if (ap
->ops
->check_err
)
539 return ap
->ops
->check_err(ap
);
541 if (ap
->flags
& ATA_FLAG_MMIO
) {
542 return readb((void __iomem
*) ap
->ioaddr
.error_addr
);
544 return inb(ap
->ioaddr
.error_addr
);
548 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
549 * @tf: Taskfile to convert
550 * @fis: Buffer into which data will output
551 * @pmp: Port multiplier port
553 * Converts a standard ATA taskfile to a Serial ATA
554 * FIS structure (Register - Host to Device).
557 * Inherited from caller.
560 void ata_tf_to_fis(struct ata_taskfile
*tf
, u8
*fis
, u8 pmp
)
562 fis
[0] = 0x27; /* Register - Host to Device FIS */
563 fis
[1] = (pmp
& 0xf) | (1 << 7); /* Port multiplier number,
564 bit 7 indicates Command FIS */
565 fis
[2] = tf
->command
;
566 fis
[3] = tf
->feature
;
573 fis
[8] = tf
->hob_lbal
;
574 fis
[9] = tf
->hob_lbam
;
575 fis
[10] = tf
->hob_lbah
;
576 fis
[11] = tf
->hob_feature
;
579 fis
[13] = tf
->hob_nsect
;
590 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
591 * @fis: Buffer from which data will be input
592 * @tf: Taskfile to output
594 * Converts a standard ATA taskfile to a Serial ATA
595 * FIS structure (Register - Host to Device).
598 * Inherited from caller.
601 void ata_tf_from_fis(u8
*fis
, struct ata_taskfile
*tf
)
603 tf
->command
= fis
[2]; /* status */
604 tf
->feature
= fis
[3]; /* error */
611 tf
->hob_lbal
= fis
[8];
612 tf
->hob_lbam
= fis
[9];
613 tf
->hob_lbah
= fis
[10];
616 tf
->hob_nsect
= fis
[13];
620 * ata_prot_to_cmd - determine which read/write opcodes to use
621 * @protocol: ATA_PROT_xxx taskfile protocol
622 * @lba48: true is lba48 is present
624 * Given necessary input, determine which read/write commands
625 * to use to transfer data.
630 static int ata_prot_to_cmd(int protocol
, int lba48
)
632 int rcmd
= 0, wcmd
= 0;
637 rcmd
= ATA_CMD_PIO_READ_EXT
;
638 wcmd
= ATA_CMD_PIO_WRITE_EXT
;
640 rcmd
= ATA_CMD_PIO_READ
;
641 wcmd
= ATA_CMD_PIO_WRITE
;
647 rcmd
= ATA_CMD_READ_EXT
;
648 wcmd
= ATA_CMD_WRITE_EXT
;
651 wcmd
= ATA_CMD_WRITE
;
659 return rcmd
| (wcmd
<< 8);
663 * ata_dev_set_protocol - set taskfile protocol and r/w commands
664 * @dev: device to examine and configure
666 * Examine the device configuration, after we have
667 * read the identify-device page and configured the
668 * data transfer mode. Set internal state related to
669 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
670 * and calculate the proper read/write commands to use.
675 static void ata_dev_set_protocol(struct ata_device
*dev
)
677 int pio
= (dev
->flags
& ATA_DFLAG_PIO
);
678 int lba48
= (dev
->flags
& ATA_DFLAG_LBA48
);
682 proto
= dev
->xfer_protocol
= ATA_PROT_PIO
;
684 proto
= dev
->xfer_protocol
= ATA_PROT_DMA
;
686 cmd
= ata_prot_to_cmd(proto
, lba48
);
690 dev
->read_cmd
= cmd
& 0xff;
691 dev
->write_cmd
= (cmd
>> 8) & 0xff;
694 static const char * xfer_mode_str
[] = {
714 * ata_udma_string - convert UDMA bit offset to string
715 * @mask: mask of bits supported; only highest bit counts.
717 * Determine string which represents the highest speed
718 * (highest bit in @udma_mask).
724 * Constant C string representing highest speed listed in
725 * @udma_mask, or the constant C string "<n/a>".
728 static const char *ata_mode_string(unsigned int mask
)
732 for (i
= 7; i
>= 0; i
--)
735 for (i
= ATA_SHIFT_MWDMA
+ 2; i
>= ATA_SHIFT_MWDMA
; i
--)
738 for (i
= ATA_SHIFT_PIO
+ 4; i
>= ATA_SHIFT_PIO
; i
--)
745 return xfer_mode_str
[i
];
749 * ata_pio_devchk - PATA device presence detection
750 * @ap: ATA channel to examine
751 * @device: Device to examine (starting at zero)
753 * This technique was originally described in
754 * Hale Landis's ATADRVR (www.ata-atapi.com), and
755 * later found its way into the ATA/ATAPI spec.
757 * Write a pattern to the ATA shadow registers,
758 * and if a device is present, it will respond by
759 * correctly storing and echoing back the
760 * ATA shadow register contents.
766 static unsigned int ata_pio_devchk(struct ata_port
*ap
,
769 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
772 ap
->ops
->dev_select(ap
, device
);
774 outb(0x55, ioaddr
->nsect_addr
);
775 outb(0xaa, ioaddr
->lbal_addr
);
777 outb(0xaa, ioaddr
->nsect_addr
);
778 outb(0x55, ioaddr
->lbal_addr
);
780 outb(0x55, ioaddr
->nsect_addr
);
781 outb(0xaa, ioaddr
->lbal_addr
);
783 nsect
= inb(ioaddr
->nsect_addr
);
784 lbal
= inb(ioaddr
->lbal_addr
);
786 if ((nsect
== 0x55) && (lbal
== 0xaa))
787 return 1; /* we found a device */
789 return 0; /* nothing found */
793 * ata_mmio_devchk - PATA device presence detection
794 * @ap: ATA channel to examine
795 * @device: Device to examine (starting at zero)
797 * This technique was originally described in
798 * Hale Landis's ATADRVR (www.ata-atapi.com), and
799 * later found its way into the ATA/ATAPI spec.
801 * Write a pattern to the ATA shadow registers,
802 * and if a device is present, it will respond by
803 * correctly storing and echoing back the
804 * ATA shadow register contents.
810 static unsigned int ata_mmio_devchk(struct ata_port
*ap
,
813 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
816 ap
->ops
->dev_select(ap
, device
);
818 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
819 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
821 writeb(0xaa, (void __iomem
*) ioaddr
->nsect_addr
);
822 writeb(0x55, (void __iomem
*) ioaddr
->lbal_addr
);
824 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
825 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
827 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
828 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
830 if ((nsect
== 0x55) && (lbal
== 0xaa))
831 return 1; /* we found a device */
833 return 0; /* nothing found */
837 * ata_devchk - PATA device presence detection
838 * @ap: ATA channel to examine
839 * @device: Device to examine (starting at zero)
841 * Dispatch ATA device presence detection, depending
842 * on whether we are using PIO or MMIO to talk to the
843 * ATA shadow registers.
849 static unsigned int ata_devchk(struct ata_port
*ap
,
852 if (ap
->flags
& ATA_FLAG_MMIO
)
853 return ata_mmio_devchk(ap
, device
);
854 return ata_pio_devchk(ap
, device
);
858 * ata_dev_classify - determine device type based on ATA-spec signature
859 * @tf: ATA taskfile register set for device to be identified
861 * Determine from taskfile register contents whether a device is
862 * ATA or ATAPI, as per "Signature and persistence" section
863 * of ATA/PI spec (volume 1, sect 5.14).
869 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
870 * the event of failure.
873 unsigned int ata_dev_classify(struct ata_taskfile
*tf
)
875 /* Apple's open source Darwin code hints that some devices only
876 * put a proper signature into the LBA mid/high registers,
877 * So, we only check those. It's sufficient for uniqueness.
880 if (((tf
->lbam
== 0) && (tf
->lbah
== 0)) ||
881 ((tf
->lbam
== 0x3c) && (tf
->lbah
== 0xc3))) {
882 DPRINTK("found ATA device by sig\n");
886 if (((tf
->lbam
== 0x14) && (tf
->lbah
== 0xeb)) ||
887 ((tf
->lbam
== 0x69) && (tf
->lbah
== 0x96))) {
888 DPRINTK("found ATAPI device by sig\n");
889 return ATA_DEV_ATAPI
;
892 DPRINTK("unknown device\n");
893 return ATA_DEV_UNKNOWN
;
897 * ata_dev_try_classify - Parse returned ATA device signature
898 * @ap: ATA channel to examine
899 * @device: Device to examine (starting at zero)
901 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
902 * an ATA/ATAPI-defined set of values is placed in the ATA
903 * shadow registers, indicating the results of device detection
906 * Select the ATA device, and read the values from the ATA shadow
907 * registers. Then parse according to the Error register value,
908 * and the spec-defined values examined by ata_dev_classify().
914 static u8
ata_dev_try_classify(struct ata_port
*ap
, unsigned int device
)
916 struct ata_device
*dev
= &ap
->device
[device
];
917 struct ata_taskfile tf
;
921 ap
->ops
->dev_select(ap
, device
);
923 memset(&tf
, 0, sizeof(tf
));
925 err
= ata_chk_err(ap
);
926 ap
->ops
->tf_read(ap
, &tf
);
928 dev
->class = ATA_DEV_NONE
;
930 /* see if device passed diags */
933 else if ((device
== 0) && (err
== 0x81))
938 /* determine if device if ATA or ATAPI */
939 class = ata_dev_classify(&tf
);
940 if (class == ATA_DEV_UNKNOWN
)
942 if ((class == ATA_DEV_ATA
) && (ata_chk_status(ap
) == 0))
951 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
952 * @id: IDENTIFY DEVICE results we will examine
953 * @s: string into which data is output
954 * @ofs: offset into identify device page
955 * @len: length of string to return. must be an even number.
957 * The strings in the IDENTIFY DEVICE page are broken up into
958 * 16-bit chunks. Run through the string, and output each
959 * 8-bit chunk linearly, regardless of platform.
965 void ata_dev_id_string(u16
*id
, unsigned char *s
,
966 unsigned int ofs
, unsigned int len
)
986 * ata_noop_dev_select - Select device 0/1 on ATA bus
987 * @ap: ATA channel to manipulate
988 * @device: ATA device (numbered from zero) to select
990 * This function performs no actual function.
992 * May be used as the dev_select() entry in ata_port_operations.
997 void ata_noop_dev_select (struct ata_port
*ap
, unsigned int device
)
1003 * ata_std_dev_select - Select device 0/1 on ATA bus
1004 * @ap: ATA channel to manipulate
1005 * @device: ATA device (numbered from zero) to select
1007 * Use the method defined in the ATA specification to
1008 * make either device 0, or device 1, active on the
1009 * ATA channel. Works with both PIO and MMIO.
1011 * May be used as the dev_select() entry in ata_port_operations.
1017 void ata_std_dev_select (struct ata_port
*ap
, unsigned int device
)
1022 tmp
= ATA_DEVICE_OBS
;
1024 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
1026 if (ap
->flags
& ATA_FLAG_MMIO
) {
1027 writeb(tmp
, (void __iomem
*) ap
->ioaddr
.device_addr
);
1029 outb(tmp
, ap
->ioaddr
.device_addr
);
1031 ata_pause(ap
); /* needed; also flushes, for mmio */
1035 * ata_dev_select - Select device 0/1 on ATA bus
1036 * @ap: ATA channel to manipulate
1037 * @device: ATA device (numbered from zero) to select
1038 * @wait: non-zero to wait for Status register BSY bit to clear
1039 * @can_sleep: non-zero if context allows sleeping
1041 * Use the method defined in the ATA specification to
1042 * make either device 0, or device 1, active on the
1045 * This is a high-level version of ata_std_dev_select(),
1046 * which additionally provides the services of inserting
1047 * the proper pauses and status polling, where needed.
1053 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
1054 unsigned int wait
, unsigned int can_sleep
)
1056 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1057 ap
->id
, device
, wait
);
1062 ap
->ops
->dev_select(ap
, device
);
1065 if (can_sleep
&& ap
->device
[device
].class == ATA_DEV_ATAPI
)
1072 * ata_dump_id - IDENTIFY DEVICE info debugging output
1073 * @dev: Device whose IDENTIFY DEVICE page we will dump
1075 * Dump selected 16-bit words from a detected device's
1076 * IDENTIFY PAGE page.
1082 static inline void ata_dump_id(struct ata_device
*dev
)
1084 DPRINTK("49==0x%04x "
1094 DPRINTK("80==0x%04x "
1104 DPRINTK("88==0x%04x "
1111 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1112 * @ap: port on which device we wish to probe resides
1113 * @device: device bus address, starting at zero
1115 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1116 * command, and read back the 512-byte device information page.
1117 * The device information page is fed to us via the standard
1118 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1119 * using standard PIO-IN paths)
1121 * After reading the device information page, we use several
1122 * bits of information from it to initialize data structures
1123 * that will be used during the lifetime of the ata_device.
1124 * Other data from the info page is used to disqualify certain
1125 * older ATA devices we do not wish to support.
1128 * Inherited from caller. Some functions called by this function
1129 * obtain the host_set lock.
1132 static void ata_dev_identify(struct ata_port
*ap
, unsigned int device
)
1134 struct ata_device
*dev
= &ap
->device
[device
];
1135 unsigned int major_version
;
1137 unsigned long xfer_modes
;
1139 unsigned int using_edd
;
1140 DECLARE_COMPLETION(wait
);
1141 struct ata_queued_cmd
*qc
;
1142 unsigned long flags
;
1145 if (!ata_dev_present(dev
)) {
1146 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1151 if (ap
->flags
& (ATA_FLAG_SRST
| ATA_FLAG_SATA_RESET
))
1156 DPRINTK("ENTER, host %u, dev %u\n", ap
->id
, device
);
1158 assert (dev
->class == ATA_DEV_ATA
|| dev
->class == ATA_DEV_ATAPI
||
1159 dev
->class == ATA_DEV_NONE
);
1161 ata_dev_select(ap
, device
, 1, 1); /* select device 0/1 */
1163 qc
= ata_qc_new_init(ap
, dev
);
1166 ata_sg_init_one(qc
, dev
->id
, sizeof(dev
->id
));
1167 qc
->dma_dir
= DMA_FROM_DEVICE
;
1168 qc
->tf
.protocol
= ATA_PROT_PIO
;
1172 if (dev
->class == ATA_DEV_ATA
) {
1173 qc
->tf
.command
= ATA_CMD_ID_ATA
;
1174 DPRINTK("do ATA identify\n");
1176 qc
->tf
.command
= ATA_CMD_ID_ATAPI
;
1177 DPRINTK("do ATAPI identify\n");
1180 qc
->waiting
= &wait
;
1181 qc
->complete_fn
= ata_qc_complete_noop
;
1183 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1184 rc
= ata_qc_issue(qc
);
1185 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1190 wait_for_completion(&wait
);
1192 status
= ata_chk_status(ap
);
1193 if (status
& ATA_ERR
) {
1195 * arg! EDD works for all test cases, but seems to return
1196 * the ATA signature for some ATAPI devices. Until the
1197 * reason for this is found and fixed, we fix up the mess
1198 * here. If IDENTIFY DEVICE returns command aborted
1199 * (as ATAPI devices do), then we issue an
1200 * IDENTIFY PACKET DEVICE.
1202 * ATA software reset (SRST, the default) does not appear
1203 * to have this problem.
1205 if ((using_edd
) && (qc
->tf
.command
== ATA_CMD_ID_ATA
)) {
1206 u8 err
= ata_chk_err(ap
);
1207 if (err
& ATA_ABORTED
) {
1208 dev
->class = ATA_DEV_ATAPI
;
1219 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
1221 /* print device capabilities */
1222 printk(KERN_DEBUG
"ata%u: dev %u cfg "
1223 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1224 ap
->id
, device
, dev
->id
[49],
1225 dev
->id
[82], dev
->id
[83], dev
->id
[84],
1226 dev
->id
[85], dev
->id
[86], dev
->id
[87],
1230 * common ATA, ATAPI feature tests
1233 /* we require DMA support (bits 8 of word 49) */
1234 if (!ata_id_has_dma(dev
->id
)) {
1235 printk(KERN_DEBUG
"ata%u: no dma\n", ap
->id
);
1239 /* quick-n-dirty find max transfer mode; for printk only */
1240 xfer_modes
= dev
->id
[ATA_ID_UDMA_MODES
];
1242 xfer_modes
= (dev
->id
[ATA_ID_MWDMA_MODES
]) << ATA_SHIFT_MWDMA
;
1244 xfer_modes
= (dev
->id
[ATA_ID_PIO_MODES
]) << (ATA_SHIFT_PIO
+ 3);
1245 xfer_modes
|= (0x7 << ATA_SHIFT_PIO
);
1250 /* ATA-specific feature tests */
1251 if (dev
->class == ATA_DEV_ATA
) {
1252 if (!ata_id_is_ata(dev
->id
)) /* sanity check */
1255 /* get major version */
1256 tmp
= dev
->id
[ATA_ID_MAJOR_VER
];
1257 for (major_version
= 14; major_version
>= 1; major_version
--)
1258 if (tmp
& (1 << major_version
))
1262 * The exact sequence expected by certain pre-ATA4 drives is:
1265 * INITIALIZE DEVICE PARAMETERS
1267 * Some drives were very specific about that exact sequence.
1269 if (major_version
< 4 || (!ata_id_has_lba(dev
->id
)))
1270 ata_dev_init_params(ap
, dev
);
1272 if (ata_id_has_lba(dev
->id
)) {
1273 dev
->flags
|= ATA_DFLAG_LBA
;
1275 if (ata_id_has_lba48(dev
->id
)) {
1276 dev
->flags
|= ATA_DFLAG_LBA48
;
1277 dev
->n_sectors
= ata_id_u64(dev
->id
, 100);
1279 dev
->n_sectors
= ata_id_u32(dev
->id
, 60);
1282 /* print device info to dmesg */
1283 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1286 ata_mode_string(xfer_modes
),
1287 (unsigned long long)dev
->n_sectors
,
1288 dev
->flags
& ATA_DFLAG_LBA48
? " LBA48" : " LBA");
1292 /* Default translation */
1293 dev
->cylinders
= dev
->id
[1];
1294 dev
->heads
= dev
->id
[3];
1295 dev
->sectors
= dev
->id
[6];
1296 dev
->n_sectors
= dev
->cylinders
* dev
->heads
* dev
->sectors
;
1298 if (ata_id_current_chs_valid(dev
->id
)) {
1299 /* Current CHS translation is valid. */
1300 dev
->cylinders
= dev
->id
[54];
1301 dev
->heads
= dev
->id
[55];
1302 dev
->sectors
= dev
->id
[56];
1304 dev
->n_sectors
= ata_id_u32(dev
->id
, 57);
1307 /* print device info to dmesg */
1308 printk(KERN_INFO
"ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1311 ata_mode_string(xfer_modes
),
1312 (unsigned long long)dev
->n_sectors
,
1313 (int)dev
->cylinders
, (int)dev
->heads
, (int)dev
->sectors
);
1317 ap
->host
->max_cmd_len
= 16;
1320 /* ATAPI-specific feature tests */
1322 if (ata_id_is_ata(dev
->id
)) /* sanity check */
1325 rc
= atapi_cdb_len(dev
->id
);
1326 if ((rc
< 12) || (rc
> ATAPI_CDB_LEN
)) {
1327 printk(KERN_WARNING
"ata%u: unsupported CDB len\n", ap
->id
);
1330 ap
->cdb_len
= (unsigned int) rc
;
1331 ap
->host
->max_cmd_len
= (unsigned char) ap
->cdb_len
;
1333 /* print device info to dmesg */
1334 printk(KERN_INFO
"ata%u: dev %u ATAPI, max %s\n",
1336 ata_mode_string(xfer_modes
));
1339 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap
));
1343 printk(KERN_WARNING
"ata%u: dev %u not supported, ignoring\n",
1346 dev
->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1347 DPRINTK("EXIT, err\n");
1351 static inline u8
ata_dev_knobble(struct ata_port
*ap
)
1353 return ((ap
->cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(ap
->device
->id
)));
1357 * ata_dev_config - Run device specific handlers and check for
1358 * SATA->PATA bridges
1365 void ata_dev_config(struct ata_port
*ap
, unsigned int i
)
1367 /* limit bridge transfers to udma5, 200 sectors */
1368 if (ata_dev_knobble(ap
)) {
1369 printk(KERN_INFO
"ata%u(%u): applying bridge limits\n",
1370 ap
->id
, ap
->device
->devno
);
1371 ap
->udma_mask
&= ATA_UDMA5
;
1372 ap
->host
->max_sectors
= ATA_MAX_SECTORS
;
1373 ap
->host
->hostt
->max_sectors
= ATA_MAX_SECTORS
;
1374 ap
->device
->flags
|= ATA_DFLAG_LOCK_SECTORS
;
1377 if (ap
->ops
->dev_config
)
1378 ap
->ops
->dev_config(ap
, &ap
->device
[i
]);
1382 * ata_bus_probe - Reset and probe ATA bus
1385 * Master ATA bus probing function. Initiates a hardware-dependent
1386 * bus reset, then attempts to identify any devices found on
1390 * PCI/etc. bus probe sem.
1393 * Zero on success, non-zero on error.
1396 static int ata_bus_probe(struct ata_port
*ap
)
1398 unsigned int i
, found
= 0;
1400 ap
->ops
->phy_reset(ap
);
1401 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1404 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1405 ata_dev_identify(ap
, i
);
1406 if (ata_dev_present(&ap
->device
[i
])) {
1408 ata_dev_config(ap
,i
);
1412 if ((!found
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1413 goto err_out_disable
;
1416 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1417 goto err_out_disable
;
1422 ap
->ops
->port_disable(ap
);
1428 * ata_port_probe - Mark port as enabled
1429 * @ap: Port for which we indicate enablement
1431 * Modify @ap data structure such that the system
1432 * thinks that the entire port is enabled.
1434 * LOCKING: host_set lock, or some other form of
1438 void ata_port_probe(struct ata_port
*ap
)
1440 ap
->flags
&= ~ATA_FLAG_PORT_DISABLED
;
1444 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1445 * @ap: SATA port associated with target SATA PHY.
1447 * This function issues commands to standard SATA Sxxx
1448 * PHY registers, to wake up the phy (and device), and
1449 * clear any reset condition.
1452 * PCI/etc. bus probe sem.
1455 void __sata_phy_reset(struct ata_port
*ap
)
1458 unsigned long timeout
= jiffies
+ (HZ
* 5);
1460 if (ap
->flags
& ATA_FLAG_SATA_RESET
) {
1461 /* issue phy wake/reset */
1462 scr_write_flush(ap
, SCR_CONTROL
, 0x301);
1463 /* Couldn't find anything in SATA I/II specs, but
1464 * AHCI-1.1 10.4.2 says at least 1 ms. */
1467 scr_write_flush(ap
, SCR_CONTROL
, 0x300); /* phy wake/clear reset */
1469 /* wait for phy to become ready, if necessary */
1472 sstatus
= scr_read(ap
, SCR_STATUS
);
1473 if ((sstatus
& 0xf) != 1)
1475 } while (time_before(jiffies
, timeout
));
1477 /* TODO: phy layer with polling, timeouts, etc. */
1478 if (sata_dev_present(ap
))
1481 sstatus
= scr_read(ap
, SCR_STATUS
);
1482 printk(KERN_INFO
"ata%u: no device found (phy stat %08x)\n",
1484 ata_port_disable(ap
);
1487 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1490 if (ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
)) {
1491 ata_port_disable(ap
);
1495 ap
->cbl
= ATA_CBL_SATA
;
1499 * sata_phy_reset - Reset SATA bus.
1500 * @ap: SATA port associated with target SATA PHY.
1502 * This function resets the SATA bus, and then probes
1503 * the bus for devices.
1506 * PCI/etc. bus probe sem.
1509 void sata_phy_reset(struct ata_port
*ap
)
1511 __sata_phy_reset(ap
);
1512 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1518 * ata_port_disable - Disable port.
1519 * @ap: Port to be disabled.
1521 * Modify @ap data structure such that the system
1522 * thinks that the entire port is disabled, and should
1523 * never attempt to probe or communicate with devices
1526 * LOCKING: host_set lock, or some other form of
1530 void ata_port_disable(struct ata_port
*ap
)
1532 ap
->device
[0].class = ATA_DEV_NONE
;
1533 ap
->device
[1].class = ATA_DEV_NONE
;
1534 ap
->flags
|= ATA_FLAG_PORT_DISABLED
;
1540 } xfer_mode_classes
[] = {
1541 { ATA_SHIFT_UDMA
, XFER_UDMA_0
},
1542 { ATA_SHIFT_MWDMA
, XFER_MW_DMA_0
},
1543 { ATA_SHIFT_PIO
, XFER_PIO_0
},
1546 static inline u8
base_from_shift(unsigned int shift
)
1550 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++)
1551 if (xfer_mode_classes
[i
].shift
== shift
)
1552 return xfer_mode_classes
[i
].base
;
1557 static void ata_dev_set_mode(struct ata_port
*ap
, struct ata_device
*dev
)
1562 if (!ata_dev_present(dev
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1565 if (dev
->xfer_shift
== ATA_SHIFT_PIO
)
1566 dev
->flags
|= ATA_DFLAG_PIO
;
1568 ata_dev_set_xfermode(ap
, dev
);
1570 base
= base_from_shift(dev
->xfer_shift
);
1571 ofs
= dev
->xfer_mode
- base
;
1572 idx
= ofs
+ dev
->xfer_shift
;
1573 WARN_ON(idx
>= ARRAY_SIZE(xfer_mode_str
));
1575 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1576 idx
, dev
->xfer_shift
, (int)dev
->xfer_mode
, (int)base
, ofs
);
1578 printk(KERN_INFO
"ata%u: dev %u configured for %s\n",
1579 ap
->id
, dev
->devno
, xfer_mode_str
[idx
]);
1582 static int ata_host_set_pio(struct ata_port
*ap
)
1588 mask
= ata_get_mode_mask(ap
, ATA_SHIFT_PIO
);
1591 printk(KERN_WARNING
"ata%u: no PIO support\n", ap
->id
);
1595 base
= base_from_shift(ATA_SHIFT_PIO
);
1596 xfer_mode
= base
+ x
;
1598 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1599 (int)base
, (int)xfer_mode
, mask
, x
);
1601 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1602 struct ata_device
*dev
= &ap
->device
[i
];
1603 if (ata_dev_present(dev
)) {
1604 dev
->pio_mode
= xfer_mode
;
1605 dev
->xfer_mode
= xfer_mode
;
1606 dev
->xfer_shift
= ATA_SHIFT_PIO
;
1607 if (ap
->ops
->set_piomode
)
1608 ap
->ops
->set_piomode(ap
, dev
);
1615 static void ata_host_set_dma(struct ata_port
*ap
, u8 xfer_mode
,
1616 unsigned int xfer_shift
)
1620 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1621 struct ata_device
*dev
= &ap
->device
[i
];
1622 if (ata_dev_present(dev
)) {
1623 dev
->dma_mode
= xfer_mode
;
1624 dev
->xfer_mode
= xfer_mode
;
1625 dev
->xfer_shift
= xfer_shift
;
1626 if (ap
->ops
->set_dmamode
)
1627 ap
->ops
->set_dmamode(ap
, dev
);
1633 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1634 * @ap: port on which timings will be programmed
1636 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1639 * PCI/etc. bus probe sem.
1642 static void ata_set_mode(struct ata_port
*ap
)
1644 unsigned int i
, xfer_shift
;
1648 /* step 1: always set host PIO timings */
1649 rc
= ata_host_set_pio(ap
);
1653 /* step 2: choose the best data xfer mode */
1654 xfer_mode
= xfer_shift
= 0;
1655 rc
= ata_choose_xfer_mode(ap
, &xfer_mode
, &xfer_shift
);
1659 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1660 if (xfer_shift
!= ATA_SHIFT_PIO
)
1661 ata_host_set_dma(ap
, xfer_mode
, xfer_shift
);
1663 /* step 4: update devices' xfer mode */
1664 ata_dev_set_mode(ap
, &ap
->device
[0]);
1665 ata_dev_set_mode(ap
, &ap
->device
[1]);
1667 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1670 if (ap
->ops
->post_set_mode
)
1671 ap
->ops
->post_set_mode(ap
);
1673 for (i
= 0; i
< 2; i
++) {
1674 struct ata_device
*dev
= &ap
->device
[i
];
1675 ata_dev_set_protocol(dev
);
1681 ata_port_disable(ap
);
1685 * ata_busy_sleep - sleep until BSY clears, or timeout
1686 * @ap: port containing status register to be polled
1687 * @tmout_pat: impatience timeout
1688 * @tmout: overall timeout
1690 * Sleep until ATA Status register bit BSY clears,
1691 * or a timeout occurs.
1697 static unsigned int ata_busy_sleep (struct ata_port
*ap
,
1698 unsigned long tmout_pat
,
1699 unsigned long tmout
)
1701 unsigned long timer_start
, timeout
;
1704 status
= ata_busy_wait(ap
, ATA_BUSY
, 300);
1705 timer_start
= jiffies
;
1706 timeout
= timer_start
+ tmout_pat
;
1707 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1709 status
= ata_busy_wait(ap
, ATA_BUSY
, 3);
1712 if (status
& ATA_BUSY
)
1713 printk(KERN_WARNING
"ata%u is slow to respond, "
1714 "please be patient\n", ap
->id
);
1716 timeout
= timer_start
+ tmout
;
1717 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1719 status
= ata_chk_status(ap
);
1722 if (status
& ATA_BUSY
) {
1723 printk(KERN_ERR
"ata%u failed to respond (%lu secs)\n",
1724 ap
->id
, tmout
/ HZ
);
1731 static void ata_bus_post_reset(struct ata_port
*ap
, unsigned int devmask
)
1733 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1734 unsigned int dev0
= devmask
& (1 << 0);
1735 unsigned int dev1
= devmask
& (1 << 1);
1736 unsigned long timeout
;
1738 /* if device 0 was found in ata_devchk, wait for its
1742 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1744 /* if device 1 was found in ata_devchk, wait for
1745 * register access, then wait for BSY to clear
1747 timeout
= jiffies
+ ATA_TMOUT_BOOT
;
1751 ap
->ops
->dev_select(ap
, 1);
1752 if (ap
->flags
& ATA_FLAG_MMIO
) {
1753 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
1754 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
1756 nsect
= inb(ioaddr
->nsect_addr
);
1757 lbal
= inb(ioaddr
->lbal_addr
);
1759 if ((nsect
== 1) && (lbal
== 1))
1761 if (time_after(jiffies
, timeout
)) {
1765 msleep(50); /* give drive a breather */
1768 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1770 /* is all this really necessary? */
1771 ap
->ops
->dev_select(ap
, 0);
1773 ap
->ops
->dev_select(ap
, 1);
1775 ap
->ops
->dev_select(ap
, 0);
1779 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1780 * @ap: Port to reset and probe
1782 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1783 * probe the bus. Not often used these days.
1786 * PCI/etc. bus probe sem.
1790 static unsigned int ata_bus_edd(struct ata_port
*ap
)
1792 struct ata_taskfile tf
;
1794 /* set up execute-device-diag (bus reset) taskfile */
1795 /* also, take interrupts to a known state (disabled) */
1796 DPRINTK("execute-device-diag\n");
1797 ata_tf_init(ap
, &tf
, 0);
1799 tf
.command
= ATA_CMD_EDD
;
1800 tf
.protocol
= ATA_PROT_NODATA
;
1803 ata_tf_to_host(ap
, &tf
);
1805 /* spec says at least 2ms. but who knows with those
1806 * crazy ATAPI devices...
1810 return ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1813 static unsigned int ata_bus_softreset(struct ata_port
*ap
,
1814 unsigned int devmask
)
1816 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1818 DPRINTK("ata%u: bus reset via SRST\n", ap
->id
);
1820 /* software reset. causes dev0 to be selected */
1821 if (ap
->flags
& ATA_FLAG_MMIO
) {
1822 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1823 udelay(20); /* FIXME: flush */
1824 writeb(ap
->ctl
| ATA_SRST
, (void __iomem
*) ioaddr
->ctl_addr
);
1825 udelay(20); /* FIXME: flush */
1826 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1828 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1830 outb(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
1832 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1835 /* spec mandates ">= 2ms" before checking status.
1836 * We wait 150ms, because that was the magic delay used for
1837 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1838 * between when the ATA command register is written, and then
1839 * status is checked. Because waiting for "a while" before
1840 * checking status is fine, post SRST, we perform this magic
1841 * delay here as well.
1845 ata_bus_post_reset(ap
, devmask
);
1851 * ata_bus_reset - reset host port and associated ATA channel
1852 * @ap: port to reset
1854 * This is typically the first time we actually start issuing
1855 * commands to the ATA channel. We wait for BSY to clear, then
1856 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1857 * result. Determine what devices, if any, are on the channel
1858 * by looking at the device 0/1 error register. Look at the signature
1859 * stored in each device's taskfile registers, to determine if
1860 * the device is ATA or ATAPI.
1863 * PCI/etc. bus probe sem.
1864 * Obtains host_set lock.
1867 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1870 void ata_bus_reset(struct ata_port
*ap
)
1872 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1873 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
1875 unsigned int dev0
, dev1
= 0, rc
= 0, devmask
= 0;
1877 DPRINTK("ENTER, host %u, port %u\n", ap
->id
, ap
->port_no
);
1879 /* determine if device 0/1 are present */
1880 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
1883 dev0
= ata_devchk(ap
, 0);
1885 dev1
= ata_devchk(ap
, 1);
1889 devmask
|= (1 << 0);
1891 devmask
|= (1 << 1);
1893 /* select device 0 again */
1894 ap
->ops
->dev_select(ap
, 0);
1896 /* issue bus reset */
1897 if (ap
->flags
& ATA_FLAG_SRST
)
1898 rc
= ata_bus_softreset(ap
, devmask
);
1899 else if ((ap
->flags
& ATA_FLAG_SATA_RESET
) == 0) {
1900 /* set up device control */
1901 if (ap
->flags
& ATA_FLAG_MMIO
)
1902 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1904 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1905 rc
= ata_bus_edd(ap
);
1912 * determine by signature whether we have ATA or ATAPI devices
1914 err
= ata_dev_try_classify(ap
, 0);
1915 if ((slave_possible
) && (err
!= 0x81))
1916 ata_dev_try_classify(ap
, 1);
1918 /* re-enable interrupts */
1919 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
1922 /* is double-select really necessary? */
1923 if (ap
->device
[1].class != ATA_DEV_NONE
)
1924 ap
->ops
->dev_select(ap
, 1);
1925 if (ap
->device
[0].class != ATA_DEV_NONE
)
1926 ap
->ops
->dev_select(ap
, 0);
1928 /* if no devices were detected, disable this port */
1929 if ((ap
->device
[0].class == ATA_DEV_NONE
) &&
1930 (ap
->device
[1].class == ATA_DEV_NONE
))
1933 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
1934 /* set up device control for ATA_FLAG_SATA_RESET */
1935 if (ap
->flags
& ATA_FLAG_MMIO
)
1936 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1938 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1945 printk(KERN_ERR
"ata%u: disabling port\n", ap
->id
);
1946 ap
->ops
->port_disable(ap
);
1951 static void ata_pr_blacklisted(struct ata_port
*ap
, struct ata_device
*dev
)
1953 printk(KERN_WARNING
"ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1954 ap
->id
, dev
->devno
);
1957 static const char * ata_dma_blacklist
[] = {
1976 "Toshiba CD-ROM XM-6202B",
1977 "TOSHIBA CD-ROM XM-1702BC",
1979 "E-IDE CD-ROM CR-840",
1982 "SAMSUNG CD-ROM SC-148C",
1983 "SAMSUNG CD-ROM SC",
1985 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1989 static int ata_dma_blacklisted(struct ata_port
*ap
, struct ata_device
*dev
)
1991 unsigned char model_num
[40];
1996 ata_dev_id_string(dev
->id
, model_num
, ATA_ID_PROD_OFS
,
1999 len
= strnlen(s
, sizeof(model_num
));
2001 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2002 while ((len
> 0) && (s
[len
- 1] == ' ')) {
2007 for (i
= 0; i
< ARRAY_SIZE(ata_dma_blacklist
); i
++)
2008 if (!strncmp(ata_dma_blacklist
[i
], s
, len
))
2014 static unsigned int ata_get_mode_mask(struct ata_port
*ap
, int shift
)
2016 struct ata_device
*master
, *slave
;
2019 master
= &ap
->device
[0];
2020 slave
= &ap
->device
[1];
2022 assert (ata_dev_present(master
) || ata_dev_present(slave
));
2024 if (shift
== ATA_SHIFT_UDMA
) {
2025 mask
= ap
->udma_mask
;
2026 if (ata_dev_present(master
)) {
2027 mask
&= (master
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2028 if (ata_dma_blacklisted(ap
, master
)) {
2030 ata_pr_blacklisted(ap
, master
);
2033 if (ata_dev_present(slave
)) {
2034 mask
&= (slave
->id
[ATA_ID_UDMA_MODES
] & 0xff);
2035 if (ata_dma_blacklisted(ap
, slave
)) {
2037 ata_pr_blacklisted(ap
, slave
);
2041 else if (shift
== ATA_SHIFT_MWDMA
) {
2042 mask
= ap
->mwdma_mask
;
2043 if (ata_dev_present(master
)) {
2044 mask
&= (master
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2045 if (ata_dma_blacklisted(ap
, master
)) {
2047 ata_pr_blacklisted(ap
, master
);
2050 if (ata_dev_present(slave
)) {
2051 mask
&= (slave
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2052 if (ata_dma_blacklisted(ap
, slave
)) {
2054 ata_pr_blacklisted(ap
, slave
);
2058 else if (shift
== ATA_SHIFT_PIO
) {
2059 mask
= ap
->pio_mask
;
2060 if (ata_dev_present(master
)) {
2061 /* spec doesn't return explicit support for
2062 * PIO0-2, so we fake it
2064 u16 tmp_mode
= master
->id
[ATA_ID_PIO_MODES
] & 0x03;
2069 if (ata_dev_present(slave
)) {
2070 /* spec doesn't return explicit support for
2071 * PIO0-2, so we fake it
2073 u16 tmp_mode
= slave
->id
[ATA_ID_PIO_MODES
] & 0x03;
2080 mask
= 0xffffffff; /* shut up compiler warning */
2087 /* find greatest bit */
2088 static int fgb(u32 bitmap
)
2093 for (i
= 0; i
< 32; i
++)
2094 if (bitmap
& (1 << i
))
2101 * ata_choose_xfer_mode - attempt to find best transfer mode
2102 * @ap: Port for which an xfer mode will be selected
2103 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2104 * @xfer_shift_out: (output) bit shift that selects this mode
2106 * Based on host and device capabilities, determine the
2107 * maximum transfer mode that is amenable to all.
2110 * PCI/etc. bus probe sem.
2113 * Zero on success, negative on error.
2116 static int ata_choose_xfer_mode(struct ata_port
*ap
,
2118 unsigned int *xfer_shift_out
)
2120 unsigned int mask
, shift
;
2123 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++) {
2124 shift
= xfer_mode_classes
[i
].shift
;
2125 mask
= ata_get_mode_mask(ap
, shift
);
2129 *xfer_mode_out
= xfer_mode_classes
[i
].base
+ x
;
2130 *xfer_shift_out
= shift
;
2139 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2140 * @ap: Port associated with device @dev
2141 * @dev: Device to which command will be sent
2143 * Issue SET FEATURES - XFER MODE command to device @dev
2147 * PCI/etc. bus probe sem.
2150 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
)
2152 DECLARE_COMPLETION(wait
);
2153 struct ata_queued_cmd
*qc
;
2155 unsigned long flags
;
2157 /* set up set-features taskfile */
2158 DPRINTK("set features - xfer mode\n");
2160 qc
= ata_qc_new_init(ap
, dev
);
2163 qc
->tf
.command
= ATA_CMD_SET_FEATURES
;
2164 qc
->tf
.feature
= SETFEATURES_XFER
;
2165 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2166 qc
->tf
.protocol
= ATA_PROT_NODATA
;
2167 qc
->tf
.nsect
= dev
->xfer_mode
;
2169 qc
->waiting
= &wait
;
2170 qc
->complete_fn
= ata_qc_complete_noop
;
2172 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2173 rc
= ata_qc_issue(qc
);
2174 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2177 ata_port_disable(ap
);
2179 wait_for_completion(&wait
);
2185 * ata_dev_init_params - Issue INIT DEV PARAMS command
2186 * @ap: Port associated with device @dev
2187 * @dev: Device to which command will be sent
2192 static void ata_dev_init_params(struct ata_port
*ap
, struct ata_device
*dev
)
2194 DECLARE_COMPLETION(wait
);
2195 struct ata_queued_cmd
*qc
;
2197 unsigned long flags
;
2198 u16 sectors
= dev
->id
[6];
2199 u16 heads
= dev
->id
[3];
2201 /* Number of sectors per track 1-255. Number of heads 1-16 */
2202 if (sectors
< 1 || sectors
> 255 || heads
< 1 || heads
> 16)
2205 /* set up init dev params taskfile */
2206 DPRINTK("init dev params \n");
2208 qc
= ata_qc_new_init(ap
, dev
);
2211 qc
->tf
.command
= ATA_CMD_INIT_DEV_PARAMS
;
2212 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2213 qc
->tf
.protocol
= ATA_PROT_NODATA
;
2214 qc
->tf
.nsect
= sectors
;
2215 qc
->tf
.device
|= (heads
- 1) & 0x0f; /* max head = num. of heads - 1 */
2217 qc
->waiting
= &wait
;
2218 qc
->complete_fn
= ata_qc_complete_noop
;
2220 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2221 rc
= ata_qc_issue(qc
);
2222 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2225 ata_port_disable(ap
);
2227 wait_for_completion(&wait
);
2233 * ata_sg_clean - Unmap DMA memory associated with command
2234 * @qc: Command containing DMA memory to be released
2236 * Unmap all mapped DMA memory associated with this command.
2239 * spin_lock_irqsave(host_set lock)
2242 static void ata_sg_clean(struct ata_queued_cmd
*qc
)
2244 struct ata_port
*ap
= qc
->ap
;
2245 struct scatterlist
*sg
= qc
->sg
;
2246 int dir
= qc
->dma_dir
;
2248 assert(qc
->flags
& ATA_QCFLAG_DMAMAP
);
2251 if (qc
->flags
& ATA_QCFLAG_SINGLE
)
2252 assert(qc
->n_elem
== 1);
2254 DPRINTK("unmapping %u sg elements\n", qc
->n_elem
);
2256 if (qc
->flags
& ATA_QCFLAG_SG
)
2257 dma_unmap_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2259 dma_unmap_single(ap
->host_set
->dev
, sg_dma_address(&sg
[0]),
2260 sg_dma_len(&sg
[0]), dir
);
2262 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
2267 * ata_fill_sg - Fill PCI IDE PRD table
2268 * @qc: Metadata associated with taskfile to be transferred
2270 * Fill PCI IDE PRD (scatter-gather) table with segments
2271 * associated with the current disk command.
2274 * spin_lock_irqsave(host_set lock)
2277 static void ata_fill_sg(struct ata_queued_cmd
*qc
)
2279 struct scatterlist
*sg
= qc
->sg
;
2280 struct ata_port
*ap
= qc
->ap
;
2281 unsigned int idx
, nelem
;
2284 assert(qc
->n_elem
> 0);
2287 for (nelem
= qc
->n_elem
; nelem
; nelem
--,sg
++) {
2291 /* determine if physical DMA addr spans 64K boundary.
2292 * Note h/w doesn't support 64-bit, so we unconditionally
2293 * truncate dma_addr_t to u32.
2295 addr
= (u32
) sg_dma_address(sg
);
2296 sg_len
= sg_dma_len(sg
);
2299 offset
= addr
& 0xffff;
2301 if ((offset
+ sg_len
) > 0x10000)
2302 len
= 0x10000 - offset
;
2304 ap
->prd
[idx
].addr
= cpu_to_le32(addr
);
2305 ap
->prd
[idx
].flags_len
= cpu_to_le32(len
& 0xffff);
2306 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx
, addr
, len
);
2315 ap
->prd
[idx
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
2318 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2319 * @qc: Metadata associated with taskfile to check
2321 * Allow low-level driver to filter ATA PACKET commands, returning
2322 * a status indicating whether or not it is OK to use DMA for the
2323 * supplied PACKET command.
2326 * spin_lock_irqsave(host_set lock)
2328 * RETURNS: 0 when ATAPI DMA can be used
2331 int ata_check_atapi_dma(struct ata_queued_cmd
*qc
)
2333 struct ata_port
*ap
= qc
->ap
;
2334 int rc
= 0; /* Assume ATAPI DMA is OK by default */
2336 if (ap
->ops
->check_atapi_dma
)
2337 rc
= ap
->ops
->check_atapi_dma(qc
);
2342 * ata_qc_prep - Prepare taskfile for submission
2343 * @qc: Metadata associated with taskfile to be prepared
2345 * Prepare ATA taskfile for submission.
2348 * spin_lock_irqsave(host_set lock)
2350 void ata_qc_prep(struct ata_queued_cmd
*qc
)
2352 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
2359 * ata_sg_init_one - Associate command with memory buffer
2360 * @qc: Command to be associated
2361 * @buf: Memory buffer
2362 * @buflen: Length of memory buffer, in bytes.
2364 * Initialize the data-related elements of queued_cmd @qc
2365 * to point to a single memory buffer, @buf of byte length @buflen.
2368 * spin_lock_irqsave(host_set lock)
2371 void ata_sg_init_one(struct ata_queued_cmd
*qc
, void *buf
, unsigned int buflen
)
2373 struct scatterlist
*sg
;
2375 qc
->flags
|= ATA_QCFLAG_SINGLE
;
2377 memset(&qc
->sgent
, 0, sizeof(qc
->sgent
));
2378 qc
->sg
= &qc
->sgent
;
2383 sg
->page
= virt_to_page(buf
);
2384 sg
->offset
= (unsigned long) buf
& ~PAGE_MASK
;
2385 sg
->length
= buflen
;
2389 * ata_sg_init - Associate command with scatter-gather table.
2390 * @qc: Command to be associated
2391 * @sg: Scatter-gather table.
2392 * @n_elem: Number of elements in s/g table.
2394 * Initialize the data-related elements of queued_cmd @qc
2395 * to point to a scatter-gather table @sg, containing @n_elem
2399 * spin_lock_irqsave(host_set lock)
2402 void ata_sg_init(struct ata_queued_cmd
*qc
, struct scatterlist
*sg
,
2403 unsigned int n_elem
)
2405 qc
->flags
|= ATA_QCFLAG_SG
;
2407 qc
->n_elem
= n_elem
;
2411 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2412 * @qc: Command with memory buffer to be mapped.
2414 * DMA-map the memory buffer associated with queued_cmd @qc.
2417 * spin_lock_irqsave(host_set lock)
2420 * Zero on success, negative on error.
2423 static int ata_sg_setup_one(struct ata_queued_cmd
*qc
)
2425 struct ata_port
*ap
= qc
->ap
;
2426 int dir
= qc
->dma_dir
;
2427 struct scatterlist
*sg
= qc
->sg
;
2428 dma_addr_t dma_address
;
2430 dma_address
= dma_map_single(ap
->host_set
->dev
, qc
->buf_virt
,
2432 if (dma_mapping_error(dma_address
))
2435 sg_dma_address(sg
) = dma_address
;
2436 sg_dma_len(sg
) = sg
->length
;
2438 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg
),
2439 qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2445 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2446 * @qc: Command with scatter-gather table to be mapped.
2448 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2451 * spin_lock_irqsave(host_set lock)
2454 * Zero on success, negative on error.
2458 static int ata_sg_setup(struct ata_queued_cmd
*qc
)
2460 struct ata_port
*ap
= qc
->ap
;
2461 struct scatterlist
*sg
= qc
->sg
;
2464 VPRINTK("ENTER, ata%u\n", ap
->id
);
2465 assert(qc
->flags
& ATA_QCFLAG_SG
);
2468 n_elem
= dma_map_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2472 DPRINTK("%d sg elements mapped\n", n_elem
);
2474 qc
->n_elem
= n_elem
;
2480 * ata_poll_qc_complete - turn irq back on and finish qc
2481 * @qc: Command to complete
2482 * @drv_stat: ATA status register content
2485 * None. (grabs host lock)
2488 void ata_poll_qc_complete(struct ata_queued_cmd
*qc
, u8 drv_stat
)
2490 struct ata_port
*ap
= qc
->ap
;
2491 unsigned long flags
;
2493 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2494 ap
->flags
&= ~ATA_FLAG_NOINTR
;
2496 ata_qc_complete(qc
, drv_stat
);
2497 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2505 * None. (executing in kernel thread context)
2511 static unsigned long ata_pio_poll(struct ata_port
*ap
)
2514 unsigned int poll_state
= HSM_ST_UNKNOWN
;
2515 unsigned int reg_state
= HSM_ST_UNKNOWN
;
2516 const unsigned int tmout_state
= HSM_ST_TMOUT
;
2518 switch (ap
->hsm_task_state
) {
2521 poll_state
= HSM_ST_POLL
;
2525 case HSM_ST_LAST_POLL
:
2526 poll_state
= HSM_ST_LAST_POLL
;
2527 reg_state
= HSM_ST_LAST
;
2534 status
= ata_chk_status(ap
);
2535 if (status
& ATA_BUSY
) {
2536 if (time_after(jiffies
, ap
->pio_task_timeout
)) {
2537 ap
->hsm_task_state
= tmout_state
;
2540 ap
->hsm_task_state
= poll_state
;
2541 return ATA_SHORT_PAUSE
;
2544 ap
->hsm_task_state
= reg_state
;
2549 * ata_pio_complete -
2553 * None. (executing in kernel thread context)
2556 * Non-zero if qc completed, zero otherwise.
2559 static int ata_pio_complete (struct ata_port
*ap
)
2561 struct ata_queued_cmd
*qc
;
2565 * This is purely heuristic. This is a fast path. Sometimes when
2566 * we enter, BSY will be cleared in a chk-status or two. If not,
2567 * the drive is probably seeking or something. Snooze for a couple
2568 * msecs, then chk-status again. If still busy, fall back to
2569 * HSM_ST_POLL state.
2571 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
| ATA_DRQ
, 10);
2572 if (drv_stat
& (ATA_BUSY
| ATA_DRQ
)) {
2574 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
| ATA_DRQ
, 10);
2575 if (drv_stat
& (ATA_BUSY
| ATA_DRQ
)) {
2576 ap
->hsm_task_state
= HSM_ST_LAST_POLL
;
2577 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
2582 drv_stat
= ata_wait_idle(ap
);
2583 if (!ata_ok(drv_stat
)) {
2584 ap
->hsm_task_state
= HSM_ST_ERR
;
2588 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2591 ap
->hsm_task_state
= HSM_ST_IDLE
;
2593 ata_poll_qc_complete(qc
, drv_stat
);
2595 /* another command may start at this point */
2603 * @buf: Buffer to swap
2604 * @buf_words: Number of 16-bit words in buffer.
2606 * Swap halves of 16-bit words if needed to convert from
2607 * little-endian byte order to native cpu byte order, or
2612 void swap_buf_le16(u16
*buf
, unsigned int buf_words
)
2617 for (i
= 0; i
< buf_words
; i
++)
2618 buf
[i
] = le16_to_cpu(buf
[i
]);
2619 #endif /* __BIG_ENDIAN */
2623 * ata_mmio_data_xfer - Transfer data by MMIO
2624 * @ap: port to read/write
2626 * @buflen: buffer length
2627 * @write_data: read/write
2629 * Transfer data from/to the device data register by MMIO.
2632 * Inherited from caller.
2636 static void ata_mmio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2637 unsigned int buflen
, int write_data
)
2640 unsigned int words
= buflen
>> 1;
2641 u16
*buf16
= (u16
*) buf
;
2642 void __iomem
*mmio
= (void __iomem
*)ap
->ioaddr
.data_addr
;
2644 /* Transfer multiple of 2 bytes */
2646 for (i
= 0; i
< words
; i
++)
2647 writew(le16_to_cpu(buf16
[i
]), mmio
);
2649 for (i
= 0; i
< words
; i
++)
2650 buf16
[i
] = cpu_to_le16(readw(mmio
));
2653 /* Transfer trailing 1 byte, if any. */
2654 if (unlikely(buflen
& 0x01)) {
2655 u16 align_buf
[1] = { 0 };
2656 unsigned char *trailing_buf
= buf
+ buflen
- 1;
2659 memcpy(align_buf
, trailing_buf
, 1);
2660 writew(le16_to_cpu(align_buf
[0]), mmio
);
2662 align_buf
[0] = cpu_to_le16(readw(mmio
));
2663 memcpy(trailing_buf
, align_buf
, 1);
2669 * ata_pio_data_xfer - Transfer data by PIO
2670 * @ap: port to read/write
2672 * @buflen: buffer length
2673 * @write_data: read/write
2675 * Transfer data from/to the device data register by PIO.
2678 * Inherited from caller.
2682 static void ata_pio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2683 unsigned int buflen
, int write_data
)
2685 unsigned int words
= buflen
>> 1;
2687 /* Transfer multiple of 2 bytes */
2689 outsw(ap
->ioaddr
.data_addr
, buf
, words
);
2691 insw(ap
->ioaddr
.data_addr
, buf
, words
);
2693 /* Transfer trailing 1 byte, if any. */
2694 if (unlikely(buflen
& 0x01)) {
2695 u16 align_buf
[1] = { 0 };
2696 unsigned char *trailing_buf
= buf
+ buflen
- 1;
2699 memcpy(align_buf
, trailing_buf
, 1);
2700 outw(le16_to_cpu(align_buf
[0]), ap
->ioaddr
.data_addr
);
2702 align_buf
[0] = cpu_to_le16(inw(ap
->ioaddr
.data_addr
));
2703 memcpy(trailing_buf
, align_buf
, 1);
2709 * ata_data_xfer - Transfer data from/to the data register.
2710 * @ap: port to read/write
2712 * @buflen: buffer length
2713 * @do_write: read/write
2715 * Transfer data from/to the device data register.
2718 * Inherited from caller.
2722 static void ata_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2723 unsigned int buflen
, int do_write
)
2725 if (ap
->flags
& ATA_FLAG_MMIO
)
2726 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
2728 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
2732 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2733 * @qc: Command on going
2735 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2738 * Inherited from caller.
2741 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
2743 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2744 struct scatterlist
*sg
= qc
->sg
;
2745 struct ata_port
*ap
= qc
->ap
;
2747 unsigned int offset
;
2750 if (qc
->cursect
== (qc
->nsect
- 1))
2751 ap
->hsm_task_state
= HSM_ST_LAST
;
2753 page
= sg
[qc
->cursg
].page
;
2754 offset
= sg
[qc
->cursg
].offset
+ qc
->cursg_ofs
* ATA_SECT_SIZE
;
2756 /* get the current page and offset */
2757 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
2758 offset
%= PAGE_SIZE
;
2760 buf
= kmap(page
) + offset
;
2765 if ((qc
->cursg_ofs
* ATA_SECT_SIZE
) == (&sg
[qc
->cursg
])->length
) {
2770 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2772 /* do the actual data transfer */
2773 do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2774 ata_data_xfer(ap
, buf
, ATA_SECT_SIZE
, do_write
);
2780 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2781 * @qc: Command on going
2782 * @bytes: number of bytes
2784 * Transfer Transfer data from/to the ATAPI device.
2787 * Inherited from caller.
2791 static void __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
2793 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2794 struct scatterlist
*sg
= qc
->sg
;
2795 struct ata_port
*ap
= qc
->ap
;
2798 unsigned int offset
, count
;
2800 if (qc
->curbytes
+ bytes
>= qc
->nbytes
)
2801 ap
->hsm_task_state
= HSM_ST_LAST
;
2804 if (unlikely(qc
->cursg
>= qc
->n_elem
)) {
2806 * The end of qc->sg is reached and the device expects
2807 * more data to transfer. In order not to overrun qc->sg
2808 * and fulfill length specified in the byte count register,
2809 * - for read case, discard trailing data from the device
2810 * - for write case, padding zero data to the device
2812 u16 pad_buf
[1] = { 0 };
2813 unsigned int words
= bytes
>> 1;
2816 if (words
) /* warning if bytes > 1 */
2817 printk(KERN_WARNING
"ata%u: %u bytes trailing data\n",
2820 for (i
= 0; i
< words
; i
++)
2821 ata_data_xfer(ap
, (unsigned char*)pad_buf
, 2, do_write
);
2823 ap
->hsm_task_state
= HSM_ST_LAST
;
2827 sg
= &qc
->sg
[qc
->cursg
];
2830 offset
= sg
->offset
+ qc
->cursg_ofs
;
2832 /* get the current page and offset */
2833 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
2834 offset
%= PAGE_SIZE
;
2836 /* don't overrun current sg */
2837 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
2839 /* don't cross page boundaries */
2840 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
2842 buf
= kmap(page
) + offset
;
2845 qc
->curbytes
+= count
;
2846 qc
->cursg_ofs
+= count
;
2848 if (qc
->cursg_ofs
== sg
->length
) {
2853 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2855 /* do the actual data transfer */
2856 ata_data_xfer(ap
, buf
, count
, do_write
);
2865 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
2866 * @qc: Command on going
2868 * Transfer Transfer data from/to the ATAPI device.
2871 * Inherited from caller.
2875 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
2877 struct ata_port
*ap
= qc
->ap
;
2878 struct ata_device
*dev
= qc
->dev
;
2879 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
2880 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
2882 ap
->ops
->tf_read(ap
, &qc
->tf
);
2883 ireason
= qc
->tf
.nsect
;
2884 bc_lo
= qc
->tf
.lbam
;
2885 bc_hi
= qc
->tf
.lbah
;
2886 bytes
= (bc_hi
<< 8) | bc_lo
;
2888 /* shall be cleared to zero, indicating xfer of data */
2889 if (ireason
& (1 << 0))
2892 /* make sure transfer direction matches expected */
2893 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
2894 if (do_write
!= i_write
)
2897 __atapi_pio_bytes(qc
, bytes
);
2902 printk(KERN_INFO
"ata%u: dev %u: ATAPI check failed\n",
2903 ap
->id
, dev
->devno
);
2904 ap
->hsm_task_state
= HSM_ST_ERR
;
2912 * None. (executing in kernel thread context)
2915 static void ata_pio_block(struct ata_port
*ap
)
2917 struct ata_queued_cmd
*qc
;
2921 * This is purely hueristic. This is a fast path.
2922 * Sometimes when we enter, BSY will be cleared in
2923 * a chk-status or two. If not, the drive is probably seeking
2924 * or something. Snooze for a couple msecs, then
2925 * chk-status again. If still busy, fall back to
2926 * HSM_ST_POLL state.
2928 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
2929 if (status
& ATA_BUSY
) {
2931 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
2932 if (status
& ATA_BUSY
) {
2933 ap
->hsm_task_state
= HSM_ST_POLL
;
2934 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
2939 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2942 if (is_atapi_taskfile(&qc
->tf
)) {
2943 /* no more data to transfer or unsupported ATAPI command */
2944 if ((status
& ATA_DRQ
) == 0) {
2945 ap
->hsm_task_state
= HSM_ST_LAST
;
2949 atapi_pio_bytes(qc
);
2951 /* handle BSY=0, DRQ=0 as error */
2952 if ((status
& ATA_DRQ
) == 0) {
2953 ap
->hsm_task_state
= HSM_ST_ERR
;
2961 static void ata_pio_error(struct ata_port
*ap
)
2963 struct ata_queued_cmd
*qc
;
2966 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2969 drv_stat
= ata_chk_status(ap
);
2970 printk(KERN_WARNING
"ata%u: PIO error, drv_stat 0x%x\n",
2973 ap
->hsm_task_state
= HSM_ST_IDLE
;
2975 ata_poll_qc_complete(qc
, drv_stat
| ATA_ERR
);
2978 static void ata_pio_task(void *_data
)
2980 struct ata_port
*ap
= _data
;
2981 unsigned long timeout
;
2988 switch (ap
->hsm_task_state
) {
2997 qc_completed
= ata_pio_complete(ap
);
3001 case HSM_ST_LAST_POLL
:
3002 timeout
= ata_pio_poll(ap
);
3012 queue_delayed_work(ata_wq
, &ap
->pio_task
, timeout
);
3013 else if (!qc_completed
)
3017 static void atapi_request_sense(struct ata_port
*ap
, struct ata_device
*dev
,
3018 struct scsi_cmnd
*cmd
)
3020 DECLARE_COMPLETION(wait
);
3021 struct ata_queued_cmd
*qc
;
3022 unsigned long flags
;
3025 DPRINTK("ATAPI request sense\n");
3027 qc
= ata_qc_new_init(ap
, dev
);
3030 /* FIXME: is this needed? */
3031 memset(cmd
->sense_buffer
, 0, sizeof(cmd
->sense_buffer
));
3033 ata_sg_init_one(qc
, cmd
->sense_buffer
, sizeof(cmd
->sense_buffer
));
3034 qc
->dma_dir
= DMA_FROM_DEVICE
;
3036 memset(&qc
->cdb
, 0, ap
->cdb_len
);
3037 qc
->cdb
[0] = REQUEST_SENSE
;
3038 qc
->cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
3040 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
3041 qc
->tf
.command
= ATA_CMD_PACKET
;
3043 qc
->tf
.protocol
= ATA_PROT_ATAPI
;
3044 qc
->tf
.lbam
= (8 * 1024) & 0xff;
3045 qc
->tf
.lbah
= (8 * 1024) >> 8;
3046 qc
->nbytes
= SCSI_SENSE_BUFFERSIZE
;
3048 qc
->waiting
= &wait
;
3049 qc
->complete_fn
= ata_qc_complete_noop
;
3051 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
3052 rc
= ata_qc_issue(qc
);
3053 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
3056 ata_port_disable(ap
);
3058 wait_for_completion(&wait
);
3064 * ata_qc_timeout - Handle timeout of queued command
3065 * @qc: Command that timed out
3067 * Some part of the kernel (currently, only the SCSI layer)
3068 * has noticed that the active command on port @ap has not
3069 * completed after a specified length of time. Handle this
3070 * condition by disabling DMA (if necessary) and completing
3071 * transactions, with error if necessary.
3073 * This also handles the case of the "lost interrupt", where
3074 * for some reason (possibly hardware bug, possibly driver bug)
3075 * an interrupt was not delivered to the driver, even though the
3076 * transaction completed successfully.
3079 * Inherited from SCSI layer (none, can sleep)
3082 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
3084 struct ata_port
*ap
= qc
->ap
;
3085 struct ata_host_set
*host_set
= ap
->host_set
;
3086 struct ata_device
*dev
= qc
->dev
;
3087 u8 host_stat
= 0, drv_stat
;
3088 unsigned long flags
;
3092 /* FIXME: doesn't this conflict with timeout handling? */
3093 if (qc
->dev
->class == ATA_DEV_ATAPI
&& qc
->scsicmd
) {
3094 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
3096 if (!(cmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
)) {
3098 /* finish completing original command */
3099 spin_lock_irqsave(&host_set
->lock
, flags
);
3100 __ata_qc_complete(qc
);
3101 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3103 atapi_request_sense(ap
, dev
, cmd
);
3105 cmd
->result
= (CHECK_CONDITION
<< 1) | (DID_OK
<< 16);
3106 scsi_finish_command(cmd
);
3112 spin_lock_irqsave(&host_set
->lock
, flags
);
3114 /* hack alert! We cannot use the supplied completion
3115 * function from inside the ->eh_strategy_handler() thread.
3116 * libata is the only user of ->eh_strategy_handler() in
3117 * any kernel, so the default scsi_done() assumes it is
3118 * not being called from the SCSI EH.
3120 qc
->scsidone
= scsi_finish_command
;
3122 switch (qc
->tf
.protocol
) {
3125 case ATA_PROT_ATAPI_DMA
:
3126 host_stat
= ap
->ops
->bmdma_status(ap
);
3128 /* before we do anything else, clear DMA-Start bit */
3129 ap
->ops
->bmdma_stop(qc
);
3135 drv_stat
= ata_chk_status(ap
);
3137 /* ack bmdma irq events */
3138 ap
->ops
->irq_clear(ap
);
3140 printk(KERN_ERR
"ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3141 ap
->id
, qc
->tf
.command
, drv_stat
, host_stat
);
3143 /* complete taskfile transaction */
3144 ata_qc_complete(qc
, drv_stat
);
3148 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3155 * ata_eng_timeout - Handle timeout of queued command
3156 * @ap: Port on which timed-out command is active
3158 * Some part of the kernel (currently, only the SCSI layer)
3159 * has noticed that the active command on port @ap has not
3160 * completed after a specified length of time. Handle this
3161 * condition by disabling DMA (if necessary) and completing
3162 * transactions, with error if necessary.
3164 * This also handles the case of the "lost interrupt", where
3165 * for some reason (possibly hardware bug, possibly driver bug)
3166 * an interrupt was not delivered to the driver, even though the
3167 * transaction completed successfully.
3170 * Inherited from SCSI layer (none, can sleep)
3173 void ata_eng_timeout(struct ata_port
*ap
)
3175 struct ata_queued_cmd
*qc
;
3179 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3181 printk(KERN_ERR
"ata%u: BUG: timeout without command\n",
3193 * ata_qc_new - Request an available ATA command, for queueing
3194 * @ap: Port associated with device @dev
3195 * @dev: Device from whom we request an available command structure
3201 static struct ata_queued_cmd
*ata_qc_new(struct ata_port
*ap
)
3203 struct ata_queued_cmd
*qc
= NULL
;
3206 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++)
3207 if (!test_and_set_bit(i
, &ap
->qactive
)) {
3208 qc
= ata_qc_from_tag(ap
, i
);
3219 * ata_qc_new_init - Request an available ATA command, and initialize it
3220 * @ap: Port associated with device @dev
3221 * @dev: Device from whom we request an available command structure
3227 struct ata_queued_cmd
*ata_qc_new_init(struct ata_port
*ap
,
3228 struct ata_device
*dev
)
3230 struct ata_queued_cmd
*qc
;
3232 qc
= ata_qc_new(ap
);
3239 qc
->cursect
= qc
->cursg
= qc
->cursg_ofs
= 0;
3241 qc
->nbytes
= qc
->curbytes
= 0;
3243 ata_tf_init(ap
, &qc
->tf
, dev
->devno
);
3245 if (dev
->flags
& ATA_DFLAG_LBA
) {
3246 qc
->tf
.flags
|= ATA_TFLAG_LBA
;
3248 if (dev
->flags
& ATA_DFLAG_LBA48
)
3249 qc
->tf
.flags
|= ATA_TFLAG_LBA48
;
3256 static int ata_qc_complete_noop(struct ata_queued_cmd
*qc
, u8 drv_stat
)
3261 static void __ata_qc_complete(struct ata_queued_cmd
*qc
)
3263 struct ata_port
*ap
= qc
->ap
;
3264 unsigned int tag
, do_clear
= 0;
3268 if (likely(ata_tag_valid(tag
))) {
3269 if (tag
== ap
->active_tag
)
3270 ap
->active_tag
= ATA_TAG_POISON
;
3271 qc
->tag
= ATA_TAG_POISON
;
3276 struct completion
*waiting
= qc
->waiting
;
3281 if (likely(do_clear
))
3282 clear_bit(tag
, &ap
->qactive
);
3286 * ata_qc_free - free unused ata_queued_cmd
3287 * @qc: Command to complete
3289 * Designed to free unused ata_queued_cmd object
3290 * in case something prevents using it.
3293 * spin_lock_irqsave(host_set lock)
3296 void ata_qc_free(struct ata_queued_cmd
*qc
)
3298 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3299 assert(qc
->waiting
== NULL
); /* nothing should be waiting */
3301 __ata_qc_complete(qc
);
3305 * ata_qc_complete - Complete an active ATA command
3306 * @qc: Command to complete
3307 * @drv_stat: ATA Status register contents
3309 * Indicate to the mid and upper layers that an ATA
3310 * command has completed, with either an ok or not-ok status.
3313 * spin_lock_irqsave(host_set lock)
3317 void ata_qc_complete(struct ata_queued_cmd
*qc
, u8 drv_stat
)
3321 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3322 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
3324 if (likely(qc
->flags
& ATA_QCFLAG_DMAMAP
))
3327 /* atapi: mark qc as inactive to prevent the interrupt handler
3328 * from completing the command twice later, before the error handler
3329 * is called. (when rc != 0 and atapi request sense is needed)
3331 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
3333 /* call completion callback */
3334 rc
= qc
->complete_fn(qc
, drv_stat
);
3336 /* if callback indicates not to complete command (non-zero),
3337 * return immediately
3342 __ata_qc_complete(qc
);
3347 static inline int ata_should_dma_map(struct ata_queued_cmd
*qc
)
3349 struct ata_port
*ap
= qc
->ap
;
3351 switch (qc
->tf
.protocol
) {
3353 case ATA_PROT_ATAPI_DMA
:
3356 case ATA_PROT_ATAPI
:
3358 case ATA_PROT_PIO_MULT
:
3359 if (ap
->flags
& ATA_FLAG_PIO_DMA
)
3372 * ata_qc_issue - issue taskfile to device
3373 * @qc: command to issue to device
3375 * Prepare an ATA command to submission to device.
3376 * This includes mapping the data into a DMA-able
3377 * area, filling in the S/G table, and finally
3378 * writing the taskfile to hardware, starting the command.
3381 * spin_lock_irqsave(host_set lock)
3384 * Zero on success, negative on error.
3387 int ata_qc_issue(struct ata_queued_cmd
*qc
)
3389 struct ata_port
*ap
= qc
->ap
;
3391 if (ata_should_dma_map(qc
)) {
3392 if (qc
->flags
& ATA_QCFLAG_SG
) {
3393 if (ata_sg_setup(qc
))
3395 } else if (qc
->flags
& ATA_QCFLAG_SINGLE
) {
3396 if (ata_sg_setup_one(qc
))
3400 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
3403 ap
->ops
->qc_prep(qc
);
3405 qc
->ap
->active_tag
= qc
->tag
;
3406 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
3408 return ap
->ops
->qc_issue(qc
);
3416 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3417 * @qc: command to issue to device
3419 * Using various libata functions and hooks, this function
3420 * starts an ATA command. ATA commands are grouped into
3421 * classes called "protocols", and issuing each type of protocol
3422 * is slightly different.
3424 * May be used as the qc_issue() entry in ata_port_operations.
3427 * spin_lock_irqsave(host_set lock)
3430 * Zero on success, negative on error.
3433 int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
3435 struct ata_port
*ap
= qc
->ap
;
3437 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
3439 switch (qc
->tf
.protocol
) {
3440 case ATA_PROT_NODATA
:
3441 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3445 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3446 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3447 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3450 case ATA_PROT_PIO
: /* load tf registers, initiate polling pio */
3451 ata_qc_set_polling(qc
);
3452 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3453 ap
->hsm_task_state
= HSM_ST
;
3454 queue_work(ata_wq
, &ap
->pio_task
);
3457 case ATA_PROT_ATAPI
:
3458 ata_qc_set_polling(qc
);
3459 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3460 queue_work(ata_wq
, &ap
->packet_task
);
3463 case ATA_PROT_ATAPI_NODATA
:
3464 ap
->flags
|= ATA_FLAG_NOINTR
;
3465 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3466 queue_work(ata_wq
, &ap
->packet_task
);
3469 case ATA_PROT_ATAPI_DMA
:
3470 ap
->flags
|= ATA_FLAG_NOINTR
;
3471 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3472 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3473 queue_work(ata_wq
, &ap
->packet_task
);
3485 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3486 * @qc: Info associated with this ATA transaction.
3489 * spin_lock_irqsave(host_set lock)
3492 static void ata_bmdma_setup_mmio (struct ata_queued_cmd
*qc
)
3494 struct ata_port
*ap
= qc
->ap
;
3495 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3497 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3499 /* load PRD table addr. */
3500 mb(); /* make sure PRD table writes are visible to controller */
3501 writel(ap
->prd_dma
, mmio
+ ATA_DMA_TABLE_OFS
);
3503 /* specify data direction, triple-check start bit is clear */
3504 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3505 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3507 dmactl
|= ATA_DMA_WR
;
3508 writeb(dmactl
, mmio
+ ATA_DMA_CMD
);
3510 /* issue r/w command */
3511 ap
->ops
->exec_command(ap
, &qc
->tf
);
3515 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
3516 * @qc: Info associated with this ATA transaction.
3519 * spin_lock_irqsave(host_set lock)
3522 static void ata_bmdma_start_mmio (struct ata_queued_cmd
*qc
)
3524 struct ata_port
*ap
= qc
->ap
;
3525 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3528 /* start host DMA transaction */
3529 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3530 writeb(dmactl
| ATA_DMA_START
, mmio
+ ATA_DMA_CMD
);
3532 /* Strictly, one may wish to issue a readb() here, to
3533 * flush the mmio write. However, control also passes
3534 * to the hardware at this point, and it will interrupt
3535 * us when we are to resume control. So, in effect,
3536 * we don't care when the mmio write flushes.
3537 * Further, a read of the DMA status register _immediately_
3538 * following the write may not be what certain flaky hardware
3539 * is expected, so I think it is best to not add a readb()
3540 * without first all the MMIO ATA cards/mobos.
3541 * Or maybe I'm just being paranoid.
3546 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3547 * @qc: Info associated with this ATA transaction.
3550 * spin_lock_irqsave(host_set lock)
3553 static void ata_bmdma_setup_pio (struct ata_queued_cmd
*qc
)
3555 struct ata_port
*ap
= qc
->ap
;
3556 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3559 /* load PRD table addr. */
3560 outl(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
3562 /* specify data direction, triple-check start bit is clear */
3563 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3564 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3566 dmactl
|= ATA_DMA_WR
;
3567 outb(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3569 /* issue r/w command */
3570 ap
->ops
->exec_command(ap
, &qc
->tf
);
3574 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3575 * @qc: Info associated with this ATA transaction.
3578 * spin_lock_irqsave(host_set lock)
3581 static void ata_bmdma_start_pio (struct ata_queued_cmd
*qc
)
3583 struct ata_port
*ap
= qc
->ap
;
3586 /* start host DMA transaction */
3587 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3588 outb(dmactl
| ATA_DMA_START
,
3589 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3594 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3595 * @qc: Info associated with this ATA transaction.
3597 * Writes the ATA_DMA_START flag to the DMA command register.
3599 * May be used as the bmdma_start() entry in ata_port_operations.
3602 * spin_lock_irqsave(host_set lock)
3604 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
3606 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3607 ata_bmdma_start_mmio(qc
);
3609 ata_bmdma_start_pio(qc
);
3614 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3615 * @qc: Info associated with this ATA transaction.
3617 * Writes address of PRD table to device's PRD Table Address
3618 * register, sets the DMA control register, and calls
3619 * ops->exec_command() to start the transfer.
3621 * May be used as the bmdma_setup() entry in ata_port_operations.
3624 * spin_lock_irqsave(host_set lock)
3626 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
3628 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3629 ata_bmdma_setup_mmio(qc
);
3631 ata_bmdma_setup_pio(qc
);
3636 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3637 * @ap: Port associated with this ATA transaction.
3639 * Clear interrupt and error flags in DMA status register.
3641 * May be used as the irq_clear() entry in ata_port_operations.
3644 * spin_lock_irqsave(host_set lock)
3647 void ata_bmdma_irq_clear(struct ata_port
*ap
)
3649 if (ap
->flags
& ATA_FLAG_MMIO
) {
3650 void __iomem
*mmio
= ((void __iomem
*) ap
->ioaddr
.bmdma_addr
) + ATA_DMA_STATUS
;
3651 writeb(readb(mmio
), mmio
);
3653 unsigned long addr
= ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
;
3654 outb(inb(addr
), addr
);
3661 * ata_bmdma_status - Read PCI IDE BMDMA status
3662 * @ap: Port associated with this ATA transaction.
3664 * Read and return BMDMA status register.
3666 * May be used as the bmdma_status() entry in ata_port_operations.
3669 * spin_lock_irqsave(host_set lock)
3672 u8
ata_bmdma_status(struct ata_port
*ap
)
3675 if (ap
->flags
& ATA_FLAG_MMIO
) {
3676 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3677 host_stat
= readb(mmio
+ ATA_DMA_STATUS
);
3679 host_stat
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
3685 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3686 * @qc: Command we are ending DMA for
3688 * Clears the ATA_DMA_START flag in the dma control register
3690 * May be used as the bmdma_stop() entry in ata_port_operations.
3693 * spin_lock_irqsave(host_set lock)
3696 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
3698 struct ata_port
*ap
= qc
->ap
;
3699 if (ap
->flags
& ATA_FLAG_MMIO
) {
3700 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3702 /* clear start/stop bit */
3703 writeb(readb(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
3704 mmio
+ ATA_DMA_CMD
);
3706 /* clear start/stop bit */
3707 outb(inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
3708 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3711 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3712 ata_altstatus(ap
); /* dummy read */
3716 * ata_host_intr - Handle host interrupt for given (port, task)
3717 * @ap: Port on which interrupt arrived (possibly...)
3718 * @qc: Taskfile currently active in engine
3720 * Handle host interrupt for given queued command. Currently,
3721 * only DMA interrupts are handled. All other commands are
3722 * handled via polling with interrupts disabled (nIEN bit).
3725 * spin_lock_irqsave(host_set lock)
3728 * One if interrupt was handled, zero if not (shared irq).
3731 inline unsigned int ata_host_intr (struct ata_port
*ap
,
3732 struct ata_queued_cmd
*qc
)
3734 u8 status
, host_stat
;
3736 switch (qc
->tf
.protocol
) {
3739 case ATA_PROT_ATAPI_DMA
:
3740 case ATA_PROT_ATAPI
:
3741 /* check status of DMA engine */
3742 host_stat
= ap
->ops
->bmdma_status(ap
);
3743 VPRINTK("ata%u: host_stat 0x%X\n", ap
->id
, host_stat
);
3745 /* if it's not our irq... */
3746 if (!(host_stat
& ATA_DMA_INTR
))
3749 /* before we do anything else, clear DMA-Start bit */
3750 ap
->ops
->bmdma_stop(qc
);
3754 case ATA_PROT_ATAPI_NODATA
:
3755 case ATA_PROT_NODATA
:
3756 /* check altstatus */
3757 status
= ata_altstatus(ap
);
3758 if (status
& ATA_BUSY
)
3761 /* check main status, clearing INTRQ */
3762 status
= ata_chk_status(ap
);
3763 if (unlikely(status
& ATA_BUSY
))
3765 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3766 ap
->id
, qc
->tf
.protocol
, status
);
3768 /* ack bmdma irq events */
3769 ap
->ops
->irq_clear(ap
);
3771 /* complete taskfile transaction */
3772 ata_qc_complete(qc
, status
);
3779 return 1; /* irq handled */
3782 ap
->stats
.idle_irq
++;
3785 if ((ap
->stats
.idle_irq
% 1000) == 0) {
3787 ata_irq_ack(ap
, 0); /* debug trap */
3788 printk(KERN_WARNING
"ata%d: irq trap\n", ap
->id
);
3791 return 0; /* irq not handled */
3795 * ata_interrupt - Default ATA host interrupt handler
3796 * @irq: irq line (unused)
3797 * @dev_instance: pointer to our ata_host_set information structure
3800 * Default interrupt handler for PCI IDE devices. Calls
3801 * ata_host_intr() for each port that is not disabled.
3804 * Obtains host_set lock during operation.
3807 * IRQ_NONE or IRQ_HANDLED.
3811 irqreturn_t
ata_interrupt (int irq
, void *dev_instance
, struct pt_regs
*regs
)
3813 struct ata_host_set
*host_set
= dev_instance
;
3815 unsigned int handled
= 0;
3816 unsigned long flags
;
3818 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3819 spin_lock_irqsave(&host_set
->lock
, flags
);
3821 for (i
= 0; i
< host_set
->n_ports
; i
++) {
3822 struct ata_port
*ap
;
3824 ap
= host_set
->ports
[i
];
3826 !(ap
->flags
& (ATA_FLAG_PORT_DISABLED
| ATA_FLAG_NOINTR
))) {
3827 struct ata_queued_cmd
*qc
;
3829 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3830 if (qc
&& (!(qc
->tf
.ctl
& ATA_NIEN
)) &&
3831 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
3832 handled
|= ata_host_intr(ap
, qc
);
3836 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3838 return IRQ_RETVAL(handled
);
3842 * atapi_packet_task - Write CDB bytes to hardware
3843 * @_data: Port to which ATAPI device is attached.
3845 * When device has indicated its readiness to accept
3846 * a CDB, this function is called. Send the CDB.
3847 * If DMA is to be performed, exit immediately.
3848 * Otherwise, we are in polling mode, so poll
3849 * status under operation succeeds or fails.
3852 * Kernel thread context (may sleep)
3855 static void atapi_packet_task(void *_data
)
3857 struct ata_port
*ap
= _data
;
3858 struct ata_queued_cmd
*qc
;
3861 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3863 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
3865 /* sleep-wait for BSY to clear */
3866 DPRINTK("busy wait\n");
3867 if (ata_busy_sleep(ap
, ATA_TMOUT_CDB_QUICK
, ATA_TMOUT_CDB
))
3870 /* make sure DRQ is set */
3871 status
= ata_chk_status(ap
);
3872 if ((status
& (ATA_BUSY
| ATA_DRQ
)) != ATA_DRQ
)
3876 DPRINTK("send cdb\n");
3877 assert(ap
->cdb_len
>= 12);
3879 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
||
3880 qc
->tf
.protocol
== ATA_PROT_ATAPI_NODATA
) {
3881 unsigned long flags
;
3883 /* Once we're done issuing command and kicking bmdma,
3884 * irq handler takes over. To not lose irq, we need
3885 * to clear NOINTR flag before sending cdb, but
3886 * interrupt handler shouldn't be invoked before we're
3887 * finished. Hence, the following locking.
3889 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
3890 ap
->flags
&= ~ATA_FLAG_NOINTR
;
3891 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
3892 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
)
3893 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3894 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
3896 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
3898 /* PIO commands are handled by polling */
3899 ap
->hsm_task_state
= HSM_ST
;
3900 queue_work(ata_wq
, &ap
->pio_task
);
3906 ata_poll_qc_complete(qc
, ATA_ERR
);
3911 * ata_port_start - Set port up for dma.
3912 * @ap: Port to initialize
3914 * Called just after data structures for each port are
3915 * initialized. Allocates space for PRD table.
3917 * May be used as the port_start() entry in ata_port_operations.
3922 int ata_port_start (struct ata_port
*ap
)
3924 struct device
*dev
= ap
->host_set
->dev
;
3926 ap
->prd
= dma_alloc_coherent(dev
, ATA_PRD_TBL_SZ
, &ap
->prd_dma
, GFP_KERNEL
);
3930 DPRINTK("prd alloc, virt %p, dma %llx\n", ap
->prd
, (unsigned long long) ap
->prd_dma
);
3937 * ata_port_stop - Undo ata_port_start()
3938 * @ap: Port to shut down
3940 * Frees the PRD table.
3942 * May be used as the port_stop() entry in ata_port_operations.
3947 void ata_port_stop (struct ata_port
*ap
)
3949 struct device
*dev
= ap
->host_set
->dev
;
3951 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
3954 void ata_host_stop (struct ata_host_set
*host_set
)
3956 if (host_set
->mmio_base
)
3957 iounmap(host_set
->mmio_base
);
3962 * ata_host_remove - Unregister SCSI host structure with upper layers
3963 * @ap: Port to unregister
3964 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3969 static void ata_host_remove(struct ata_port
*ap
, unsigned int do_unregister
)
3971 struct Scsi_Host
*sh
= ap
->host
;
3976 scsi_remove_host(sh
);
3978 ap
->ops
->port_stop(ap
);
3982 * ata_host_init - Initialize an ata_port structure
3983 * @ap: Structure to initialize
3984 * @host: associated SCSI mid-layer structure
3985 * @host_set: Collection of hosts to which @ap belongs
3986 * @ent: Probe information provided by low-level driver
3987 * @port_no: Port number associated with this ata_port
3989 * Initialize a new ata_port structure, and its associated
3993 * Inherited from caller.
3997 static void ata_host_init(struct ata_port
*ap
, struct Scsi_Host
*host
,
3998 struct ata_host_set
*host_set
,
3999 struct ata_probe_ent
*ent
, unsigned int port_no
)
4005 host
->max_channel
= 1;
4006 host
->unique_id
= ata_unique_id
++;
4007 host
->max_cmd_len
= 12;
4009 scsi_assign_lock(host
, &host_set
->lock
);
4011 ap
->flags
= ATA_FLAG_PORT_DISABLED
;
4012 ap
->id
= host
->unique_id
;
4014 ap
->ctl
= ATA_DEVCTL_OBS
;
4015 ap
->host_set
= host_set
;
4016 ap
->port_no
= port_no
;
4018 ent
->legacy_mode
? ent
->hard_port_no
: port_no
;
4019 ap
->pio_mask
= ent
->pio_mask
;
4020 ap
->mwdma_mask
= ent
->mwdma_mask
;
4021 ap
->udma_mask
= ent
->udma_mask
;
4022 ap
->flags
|= ent
->host_flags
;
4023 ap
->ops
= ent
->port_ops
;
4024 ap
->cbl
= ATA_CBL_NONE
;
4025 ap
->active_tag
= ATA_TAG_POISON
;
4026 ap
->last_ctl
= 0xFF;
4028 INIT_WORK(&ap
->packet_task
, atapi_packet_task
, ap
);
4029 INIT_WORK(&ap
->pio_task
, ata_pio_task
, ap
);
4031 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
4032 ap
->device
[i
].devno
= i
;
4035 ap
->stats
.unhandled_irq
= 1;
4036 ap
->stats
.idle_irq
= 1;
4039 memcpy(&ap
->ioaddr
, &ent
->port
[port_no
], sizeof(struct ata_ioports
));
4043 * ata_host_add - Attach low-level ATA driver to system
4044 * @ent: Information provided by low-level driver
4045 * @host_set: Collections of ports to which we add
4046 * @port_no: Port number associated with this host
4048 * Attach low-level ATA driver to system.
4051 * PCI/etc. bus probe sem.
4054 * New ata_port on success, for NULL on error.
4058 static struct ata_port
* ata_host_add(struct ata_probe_ent
*ent
,
4059 struct ata_host_set
*host_set
,
4060 unsigned int port_no
)
4062 struct Scsi_Host
*host
;
4063 struct ata_port
*ap
;
4067 host
= scsi_host_alloc(ent
->sht
, sizeof(struct ata_port
));
4071 ap
= (struct ata_port
*) &host
->hostdata
[0];
4073 ata_host_init(ap
, host
, host_set
, ent
, port_no
);
4075 rc
= ap
->ops
->port_start(ap
);
4082 scsi_host_put(host
);
4087 * ata_device_add - Register hardware device with ATA and SCSI layers
4088 * @ent: Probe information describing hardware device to be registered
4090 * This function processes the information provided in the probe
4091 * information struct @ent, allocates the necessary ATA and SCSI
4092 * host information structures, initializes them, and registers
4093 * everything with requisite kernel subsystems.
4095 * This function requests irqs, probes the ATA bus, and probes
4099 * PCI/etc. bus probe sem.
4102 * Number of ports registered. Zero on error (no ports registered).
4106 int ata_device_add(struct ata_probe_ent
*ent
)
4108 unsigned int count
= 0, i
;
4109 struct device
*dev
= ent
->dev
;
4110 struct ata_host_set
*host_set
;
4113 /* alloc a container for our list of ATA ports (buses) */
4114 host_set
= kmalloc(sizeof(struct ata_host_set
) +
4115 (ent
->n_ports
* sizeof(void *)), GFP_KERNEL
);
4118 memset(host_set
, 0, sizeof(struct ata_host_set
) + (ent
->n_ports
* sizeof(void *)));
4119 spin_lock_init(&host_set
->lock
);
4121 host_set
->dev
= dev
;
4122 host_set
->n_ports
= ent
->n_ports
;
4123 host_set
->irq
= ent
->irq
;
4124 host_set
->mmio_base
= ent
->mmio_base
;
4125 host_set
->private_data
= ent
->private_data
;
4126 host_set
->ops
= ent
->port_ops
;
4128 /* register each port bound to this device */
4129 for (i
= 0; i
< ent
->n_ports
; i
++) {
4130 struct ata_port
*ap
;
4131 unsigned long xfer_mode_mask
;
4133 ap
= ata_host_add(ent
, host_set
, i
);
4137 host_set
->ports
[i
] = ap
;
4138 xfer_mode_mask
=(ap
->udma_mask
<< ATA_SHIFT_UDMA
) |
4139 (ap
->mwdma_mask
<< ATA_SHIFT_MWDMA
) |
4140 (ap
->pio_mask
<< ATA_SHIFT_PIO
);
4142 /* print per-port info to dmesg */
4143 printk(KERN_INFO
"ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4144 "bmdma 0x%lX irq %lu\n",
4146 ap
->flags
& ATA_FLAG_SATA
? 'S' : 'P',
4147 ata_mode_string(xfer_mode_mask
),
4148 ap
->ioaddr
.cmd_addr
,
4149 ap
->ioaddr
.ctl_addr
,
4150 ap
->ioaddr
.bmdma_addr
,
4154 host_set
->ops
->irq_clear(ap
);
4163 /* obtain irq, that is shared between channels */
4164 if (request_irq(ent
->irq
, ent
->port_ops
->irq_handler
, ent
->irq_flags
,
4165 DRV_NAME
, host_set
))
4168 /* perform each probe synchronously */
4169 DPRINTK("probe begin\n");
4170 for (i
= 0; i
< count
; i
++) {
4171 struct ata_port
*ap
;
4174 ap
= host_set
->ports
[i
];
4176 DPRINTK("ata%u: probe begin\n", ap
->id
);
4177 rc
= ata_bus_probe(ap
);
4178 DPRINTK("ata%u: probe end\n", ap
->id
);
4181 /* FIXME: do something useful here?
4182 * Current libata behavior will
4183 * tear down everything when
4184 * the module is removed
4185 * or the h/w is unplugged.
4189 rc
= scsi_add_host(ap
->host
, dev
);
4191 printk(KERN_ERR
"ata%u: scsi_add_host failed\n",
4193 /* FIXME: do something useful here */
4194 /* FIXME: handle unconditional calls to
4195 * scsi_scan_host and ata_host_remove, below,
4201 /* probes are done, now scan each port's disk(s) */
4202 DPRINTK("probe begin\n");
4203 for (i
= 0; i
< count
; i
++) {
4204 struct ata_port
*ap
= host_set
->ports
[i
];
4206 ata_scsi_scan_host(ap
);
4209 dev_set_drvdata(dev
, host_set
);
4211 VPRINTK("EXIT, returning %u\n", ent
->n_ports
);
4212 return ent
->n_ports
; /* success */
4215 for (i
= 0; i
< count
; i
++) {
4216 ata_host_remove(host_set
->ports
[i
], 1);
4217 scsi_host_put(host_set
->ports
[i
]->host
);
4220 VPRINTK("EXIT, returning 0\n");
4225 * ata_host_set_remove - PCI layer callback for device removal
4226 * @host_set: ATA host set that was removed
4228 * Unregister all objects associated with this host set. Free those
4232 * Inherited from calling layer (may sleep).
4236 void ata_host_set_remove(struct ata_host_set
*host_set
)
4238 struct ata_port
*ap
;
4241 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4242 ap
= host_set
->ports
[i
];
4243 scsi_remove_host(ap
->host
);
4246 free_irq(host_set
->irq
, host_set
);
4248 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4249 ap
= host_set
->ports
[i
];
4251 ata_scsi_release(ap
->host
);
4253 if ((ap
->flags
& ATA_FLAG_NO_LEGACY
) == 0) {
4254 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
4256 if (ioaddr
->cmd_addr
== 0x1f0)
4257 release_region(0x1f0, 8);
4258 else if (ioaddr
->cmd_addr
== 0x170)
4259 release_region(0x170, 8);
4262 scsi_host_put(ap
->host
);
4265 if (host_set
->ops
->host_stop
)
4266 host_set
->ops
->host_stop(host_set
);
4272 * ata_scsi_release - SCSI layer callback hook for host unload
4273 * @host: libata host to be unloaded
4275 * Performs all duties necessary to shut down a libata port...
4276 * Kill port kthread, disable port, and release resources.
4279 * Inherited from SCSI layer.
4285 int ata_scsi_release(struct Scsi_Host
*host
)
4287 struct ata_port
*ap
= (struct ata_port
*) &host
->hostdata
[0];
4291 ap
->ops
->port_disable(ap
);
4292 ata_host_remove(ap
, 0);
4299 * ata_std_ports - initialize ioaddr with standard port offsets.
4300 * @ioaddr: IO address structure to be initialized
4302 * Utility function which initializes data_addr, error_addr,
4303 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4304 * device_addr, status_addr, and command_addr to standard offsets
4305 * relative to cmd_addr.
4307 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
4310 void ata_std_ports(struct ata_ioports
*ioaddr
)
4312 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
4313 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
4314 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
4315 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
4316 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
4317 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
4318 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
4319 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
4320 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
4321 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
4324 static struct ata_probe_ent
*
4325 ata_probe_ent_alloc(struct device
*dev
, struct ata_port_info
*port
)
4327 struct ata_probe_ent
*probe_ent
;
4329 probe_ent
= kmalloc(sizeof(*probe_ent
), GFP_KERNEL
);
4331 printk(KERN_ERR DRV_NAME
"(%s): out of memory\n",
4332 kobject_name(&(dev
->kobj
)));
4336 memset(probe_ent
, 0, sizeof(*probe_ent
));
4338 INIT_LIST_HEAD(&probe_ent
->node
);
4339 probe_ent
->dev
= dev
;
4341 probe_ent
->sht
= port
->sht
;
4342 probe_ent
->host_flags
= port
->host_flags
;
4343 probe_ent
->pio_mask
= port
->pio_mask
;
4344 probe_ent
->mwdma_mask
= port
->mwdma_mask
;
4345 probe_ent
->udma_mask
= port
->udma_mask
;
4346 probe_ent
->port_ops
= port
->port_ops
;
4355 void ata_pci_host_stop (struct ata_host_set
*host_set
)
4357 struct pci_dev
*pdev
= to_pci_dev(host_set
->dev
);
4359 pci_iounmap(pdev
, host_set
->mmio_base
);
4363 * ata_pci_init_native_mode - Initialize native-mode driver
4364 * @pdev: pci device to be initialized
4365 * @port: array[2] of pointers to port info structures.
4366 * @ports: bitmap of ports present
4368 * Utility function which allocates and initializes an
4369 * ata_probe_ent structure for a standard dual-port
4370 * PIO-based IDE controller. The returned ata_probe_ent
4371 * structure can be passed to ata_device_add(). The returned
4372 * ata_probe_ent structure should then be freed with kfree().
4374 * The caller need only pass the address of the primary port, the
4375 * secondary will be deduced automatically. If the device has non
4376 * standard secondary port mappings this function can be called twice,
4377 * once for each interface.
4380 struct ata_probe_ent
*
4381 ata_pci_init_native_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
, int ports
)
4383 struct ata_probe_ent
*probe_ent
=
4384 ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
4390 probe_ent
->irq
= pdev
->irq
;
4391 probe_ent
->irq_flags
= SA_SHIRQ
;
4393 if (ports
& ATA_PORT_PRIMARY
) {
4394 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 0);
4395 probe_ent
->port
[p
].altstatus_addr
=
4396 probe_ent
->port
[p
].ctl_addr
=
4397 pci_resource_start(pdev
, 1) | ATA_PCI_CTL_OFS
;
4398 probe_ent
->port
[p
].bmdma_addr
= pci_resource_start(pdev
, 4);
4399 ata_std_ports(&probe_ent
->port
[p
]);
4403 if (ports
& ATA_PORT_SECONDARY
) {
4404 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 2);
4405 probe_ent
->port
[p
].altstatus_addr
=
4406 probe_ent
->port
[p
].ctl_addr
=
4407 pci_resource_start(pdev
, 3) | ATA_PCI_CTL_OFS
;
4408 probe_ent
->port
[p
].bmdma_addr
= pci_resource_start(pdev
, 4) + 8;
4409 ata_std_ports(&probe_ent
->port
[p
]);
4413 probe_ent
->n_ports
= p
;
4417 static struct ata_probe_ent
*ata_pci_init_legacy_port(struct pci_dev
*pdev
, struct ata_port_info
**port
, int port_num
)
4419 struct ata_probe_ent
*probe_ent
;
4421 probe_ent
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
4426 probe_ent
->legacy_mode
= 1;
4427 probe_ent
->n_ports
= 1;
4428 probe_ent
->hard_port_no
= port_num
;
4433 probe_ent
->irq
= 14;
4434 probe_ent
->port
[0].cmd_addr
= 0x1f0;
4435 probe_ent
->port
[0].altstatus_addr
=
4436 probe_ent
->port
[0].ctl_addr
= 0x3f6;
4439 probe_ent
->irq
= 15;
4440 probe_ent
->port
[0].cmd_addr
= 0x170;
4441 probe_ent
->port
[0].altstatus_addr
=
4442 probe_ent
->port
[0].ctl_addr
= 0x376;
4445 probe_ent
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4) + 8 * port_num
;
4446 ata_std_ports(&probe_ent
->port
[0]);
4451 * ata_pci_init_one - Initialize/register PCI IDE host controller
4452 * @pdev: Controller to be initialized
4453 * @port_info: Information from low-level host driver
4454 * @n_ports: Number of ports attached to host controller
4456 * This is a helper function which can be called from a driver's
4457 * xxx_init_one() probe function if the hardware uses traditional
4458 * IDE taskfile registers.
4460 * This function calls pci_enable_device(), reserves its register
4461 * regions, sets the dma mask, enables bus master mode, and calls
4465 * Inherited from PCI layer (may sleep).
4468 * Zero on success, negative on errno-based value on error.
4472 int ata_pci_init_one (struct pci_dev
*pdev
, struct ata_port_info
**port_info
,
4473 unsigned int n_ports
)
4475 struct ata_probe_ent
*probe_ent
= NULL
, *probe_ent2
= NULL
;
4476 struct ata_port_info
*port
[2];
4478 unsigned int legacy_mode
= 0;
4479 int disable_dev_on_err
= 1;
4484 port
[0] = port_info
[0];
4486 port
[1] = port_info
[1];
4490 if ((port
[0]->host_flags
& ATA_FLAG_NO_LEGACY
) == 0
4491 && (pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
4492 /* TODO: What if one channel is in native mode ... */
4493 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
4494 mask
= (1 << 2) | (1 << 0);
4495 if ((tmp8
& mask
) != mask
)
4496 legacy_mode
= (1 << 3);
4500 if ((!legacy_mode
) && (n_ports
> 2)) {
4501 printk(KERN_ERR
"ata: BUG: native mode, n_ports > 2\n");
4506 /* FIXME: Really for ATA it isn't safe because the device may be
4507 multi-purpose and we want to leave it alone if it was already
4508 enabled. Secondly for shared use as Arjan says we want refcounting
4510 Checking dev->is_enabled is insufficient as this is not set at
4511 boot for the primary video which is BIOS enabled
4514 rc
= pci_enable_device(pdev
);
4518 rc
= pci_request_regions(pdev
, DRV_NAME
);
4520 disable_dev_on_err
= 0;
4524 /* FIXME: Should use platform specific mappers for legacy port ranges */
4526 if (!request_region(0x1f0, 8, "libata")) {
4527 struct resource
*conflict
, res
;
4529 res
.end
= 0x1f0 + 8 - 1;
4530 conflict
= ____request_resource(&ioport_resource
, &res
);
4531 if (!strcmp(conflict
->name
, "libata"))
4532 legacy_mode
|= (1 << 0);
4534 disable_dev_on_err
= 0;
4535 printk(KERN_WARNING
"ata: 0x1f0 IDE port busy\n");
4538 legacy_mode
|= (1 << 0);
4540 if (!request_region(0x170, 8, "libata")) {
4541 struct resource
*conflict
, res
;
4543 res
.end
= 0x170 + 8 - 1;
4544 conflict
= ____request_resource(&ioport_resource
, &res
);
4545 if (!strcmp(conflict
->name
, "libata"))
4546 legacy_mode
|= (1 << 1);
4548 disable_dev_on_err
= 0;
4549 printk(KERN_WARNING
"ata: 0x170 IDE port busy\n");
4552 legacy_mode
|= (1 << 1);
4555 /* we have legacy mode, but all ports are unavailable */
4556 if (legacy_mode
== (1 << 3)) {
4558 goto err_out_regions
;
4561 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
4563 goto err_out_regions
;
4564 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
4566 goto err_out_regions
;
4569 if (legacy_mode
& (1 << 0))
4570 probe_ent
= ata_pci_init_legacy_port(pdev
, port
, 0);
4571 if (legacy_mode
& (1 << 1))
4572 probe_ent2
= ata_pci_init_legacy_port(pdev
, port
, 1);
4575 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
| ATA_PORT_SECONDARY
);
4577 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
);
4579 if (!probe_ent
&& !probe_ent2
) {
4581 goto err_out_regions
;
4584 pci_set_master(pdev
);
4586 /* FIXME: check ata_device_add return */
4588 if (legacy_mode
& (1 << 0))
4589 ata_device_add(probe_ent
);
4590 if (legacy_mode
& (1 << 1))
4591 ata_device_add(probe_ent2
);
4593 ata_device_add(probe_ent
);
4601 if (legacy_mode
& (1 << 0))
4602 release_region(0x1f0, 8);
4603 if (legacy_mode
& (1 << 1))
4604 release_region(0x170, 8);
4605 pci_release_regions(pdev
);
4607 if (disable_dev_on_err
)
4608 pci_disable_device(pdev
);
4613 * ata_pci_remove_one - PCI layer callback for device removal
4614 * @pdev: PCI device that was removed
4616 * PCI layer indicates to libata via this hook that
4617 * hot-unplug or module unload event has occured.
4618 * Handle this by unregistering all objects associated
4619 * with this PCI device. Free those objects. Then finally
4620 * release PCI resources and disable device.
4623 * Inherited from PCI layer (may sleep).
4626 void ata_pci_remove_one (struct pci_dev
*pdev
)
4628 struct device
*dev
= pci_dev_to_dev(pdev
);
4629 struct ata_host_set
*host_set
= dev_get_drvdata(dev
);
4631 ata_host_set_remove(host_set
);
4632 pci_release_regions(pdev
);
4633 pci_disable_device(pdev
);
4634 dev_set_drvdata(dev
, NULL
);
4637 /* move to PCI subsystem */
4638 int pci_test_config_bits(struct pci_dev
*pdev
, struct pci_bits
*bits
)
4640 unsigned long tmp
= 0;
4642 switch (bits
->width
) {
4645 pci_read_config_byte(pdev
, bits
->reg
, &tmp8
);
4651 pci_read_config_word(pdev
, bits
->reg
, &tmp16
);
4657 pci_read_config_dword(pdev
, bits
->reg
, &tmp32
);
4668 return (tmp
== bits
->val
) ? 1 : 0;
4670 #endif /* CONFIG_PCI */
4673 static int __init
ata_init(void)
4675 ata_wq
= create_workqueue("ata");
4679 printk(KERN_DEBUG
"libata version " DRV_VERSION
" loaded.\n");
4683 static void __exit
ata_exit(void)
4685 destroy_workqueue(ata_wq
);
4688 module_init(ata_init
);
4689 module_exit(ata_exit
);
4692 * libata is essentially a library of internal helper functions for
4693 * low-level ATA host controller drivers. As such, the API/ABI is
4694 * likely to change as new drivers are added and updated.
4695 * Do not depend on ABI/API stability.
4698 EXPORT_SYMBOL_GPL(ata_std_bios_param
);
4699 EXPORT_SYMBOL_GPL(ata_std_ports
);
4700 EXPORT_SYMBOL_GPL(ata_device_add
);
4701 EXPORT_SYMBOL_GPL(ata_host_set_remove
);
4702 EXPORT_SYMBOL_GPL(ata_sg_init
);
4703 EXPORT_SYMBOL_GPL(ata_sg_init_one
);
4704 EXPORT_SYMBOL_GPL(ata_qc_complete
);
4705 EXPORT_SYMBOL_GPL(ata_qc_issue_prot
);
4706 EXPORT_SYMBOL_GPL(ata_eng_timeout
);
4707 EXPORT_SYMBOL_GPL(ata_tf_load
);
4708 EXPORT_SYMBOL_GPL(ata_tf_read
);
4709 EXPORT_SYMBOL_GPL(ata_noop_dev_select
);
4710 EXPORT_SYMBOL_GPL(ata_std_dev_select
);
4711 EXPORT_SYMBOL_GPL(ata_tf_to_fis
);
4712 EXPORT_SYMBOL_GPL(ata_tf_from_fis
);
4713 EXPORT_SYMBOL_GPL(ata_check_status
);
4714 EXPORT_SYMBOL_GPL(ata_altstatus
);
4715 EXPORT_SYMBOL_GPL(ata_chk_err
);
4716 EXPORT_SYMBOL_GPL(ata_exec_command
);
4717 EXPORT_SYMBOL_GPL(ata_port_start
);
4718 EXPORT_SYMBOL_GPL(ata_port_stop
);
4719 EXPORT_SYMBOL_GPL(ata_host_stop
);
4720 EXPORT_SYMBOL_GPL(ata_interrupt
);
4721 EXPORT_SYMBOL_GPL(ata_qc_prep
);
4722 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
4723 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
4724 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear
);
4725 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
4726 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
4727 EXPORT_SYMBOL_GPL(ata_port_probe
);
4728 EXPORT_SYMBOL_GPL(sata_phy_reset
);
4729 EXPORT_SYMBOL_GPL(__sata_phy_reset
);
4730 EXPORT_SYMBOL_GPL(ata_bus_reset
);
4731 EXPORT_SYMBOL_GPL(ata_port_disable
);
4732 EXPORT_SYMBOL_GPL(ata_scsi_ioctl
);
4733 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd
);
4734 EXPORT_SYMBOL_GPL(ata_scsi_error
);
4735 EXPORT_SYMBOL_GPL(ata_scsi_slave_config
);
4736 EXPORT_SYMBOL_GPL(ata_scsi_release
);
4737 EXPORT_SYMBOL_GPL(ata_host_intr
);
4738 EXPORT_SYMBOL_GPL(ata_dev_classify
);
4739 EXPORT_SYMBOL_GPL(ata_dev_id_string
);
4740 EXPORT_SYMBOL_GPL(ata_dev_config
);
4741 EXPORT_SYMBOL_GPL(ata_scsi_simulate
);
4744 EXPORT_SYMBOL_GPL(pci_test_config_bits
);
4745 EXPORT_SYMBOL_GPL(ata_pci_host_stop
);
4746 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode
);
4747 EXPORT_SYMBOL_GPL(ata_pci_init_one
);
4748 EXPORT_SYMBOL_GPL(ata_pci_remove_one
);
4749 #endif /* CONFIG_PCI */
This page took 0.362875 seconds and 5 git commands to generate.