2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
42 #include <linux/highmem.h>
43 #include <linux/spinlock.h>
44 #include <linux/blkdev.h>
45 #include <linux/delay.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/completion.h>
49 #include <linux/suspend.h>
50 #include <linux/workqueue.h>
51 #include <scsi/scsi.h>
53 #include "scsi_priv.h"
54 #include <scsi/scsi_host.h>
55 #include <linux/libata.h>
57 #include <asm/semaphore.h>
58 #include <asm/byteorder.h>
62 static unsigned int ata_busy_sleep (struct ata_port
*ap
,
63 unsigned long tmout_pat
,
65 static void ata_set_mode(struct ata_port
*ap
);
66 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
);
67 static unsigned int ata_get_mode_mask(struct ata_port
*ap
, int shift
);
68 static int fgb(u32 bitmap
);
69 static int ata_choose_xfer_mode(struct ata_port
*ap
,
71 unsigned int *xfer_shift_out
);
72 static int ata_qc_complete_noop(struct ata_queued_cmd
*qc
, u8 drv_stat
);
73 static void __ata_qc_complete(struct ata_queued_cmd
*qc
);
75 static unsigned int ata_unique_id
= 1;
76 static struct workqueue_struct
*ata_wq
;
78 MODULE_AUTHOR("Jeff Garzik");
79 MODULE_DESCRIPTION("Library module for ATA devices");
80 MODULE_LICENSE("GPL");
81 MODULE_VERSION(DRV_VERSION
);
84 * ata_tf_load - send taskfile registers to host controller
85 * @ap: Port to which output is sent
86 * @tf: ATA taskfile register set
88 * Outputs ATA taskfile to standard ATA host controller.
91 * Inherited from caller.
94 static void ata_tf_load_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
96 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
97 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
99 if (tf
->ctl
!= ap
->last_ctl
) {
100 outb(tf
->ctl
, ioaddr
->ctl_addr
);
101 ap
->last_ctl
= tf
->ctl
;
105 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
106 outb(tf
->hob_feature
, ioaddr
->feature_addr
);
107 outb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
108 outb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
109 outb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
110 outb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
111 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
120 outb(tf
->feature
, ioaddr
->feature_addr
);
121 outb(tf
->nsect
, ioaddr
->nsect_addr
);
122 outb(tf
->lbal
, ioaddr
->lbal_addr
);
123 outb(tf
->lbam
, ioaddr
->lbam_addr
);
124 outb(tf
->lbah
, ioaddr
->lbah_addr
);
125 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
133 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
134 outb(tf
->device
, ioaddr
->device_addr
);
135 VPRINTK("device 0x%X\n", tf
->device
);
142 * ata_tf_load_mmio - send taskfile registers to host controller
143 * @ap: Port to which output is sent
144 * @tf: ATA taskfile register set
146 * Outputs ATA taskfile to standard ATA host controller using MMIO.
149 * Inherited from caller.
152 static void ata_tf_load_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
154 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
155 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
157 if (tf
->ctl
!= ap
->last_ctl
) {
158 writeb(tf
->ctl
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
159 ap
->last_ctl
= tf
->ctl
;
163 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
164 writeb(tf
->hob_feature
, (void __iomem
*) ioaddr
->feature_addr
);
165 writeb(tf
->hob_nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
166 writeb(tf
->hob_lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
167 writeb(tf
->hob_lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
168 writeb(tf
->hob_lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
169 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
178 writeb(tf
->feature
, (void __iomem
*) ioaddr
->feature_addr
);
179 writeb(tf
->nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
180 writeb(tf
->lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
181 writeb(tf
->lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
182 writeb(tf
->lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
183 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
191 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
192 writeb(tf
->device
, (void __iomem
*) ioaddr
->device_addr
);
193 VPRINTK("device 0x%X\n", tf
->device
);
201 * ata_tf_load - send taskfile registers to host controller
202 * @ap: Port to which output is sent
203 * @tf: ATA taskfile register set
205 * Outputs ATA taskfile to standard ATA host controller using MMIO
206 * or PIO as indicated by the ATA_FLAG_MMIO flag.
207 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
208 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
209 * hob_lbal, hob_lbam, and hob_lbah.
211 * This function waits for idle (!BUSY and !DRQ) after writing
212 * registers. If the control register has a new value, this
213 * function also waits for idle after writing control and before
214 * writing the remaining registers.
216 * May be used as the tf_load() entry in ata_port_operations.
219 * Inherited from caller.
221 void ata_tf_load(struct ata_port
*ap
, struct ata_taskfile
*tf
)
223 if (ap
->flags
& ATA_FLAG_MMIO
)
224 ata_tf_load_mmio(ap
, tf
);
226 ata_tf_load_pio(ap
, tf
);
230 * ata_exec_command_pio - issue ATA command to host controller
231 * @ap: port to which command is being issued
232 * @tf: ATA taskfile register set
234 * Issues PIO write to ATA command register, with proper
235 * synchronization with interrupt handler / other threads.
238 * spin_lock_irqsave(host_set lock)
241 static void ata_exec_command_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
243 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
245 outb(tf
->command
, ap
->ioaddr
.command_addr
);
251 * ata_exec_command_mmio - issue ATA command to host controller
252 * @ap: port to which command is being issued
253 * @tf: ATA taskfile register set
255 * Issues MMIO write to ATA command register, with proper
256 * synchronization with interrupt handler / other threads.
259 * spin_lock_irqsave(host_set lock)
262 static void ata_exec_command_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
264 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
266 writeb(tf
->command
, (void __iomem
*) ap
->ioaddr
.command_addr
);
272 * ata_exec_command - issue ATA command to host controller
273 * @ap: port to which command is being issued
274 * @tf: ATA taskfile register set
276 * Issues PIO/MMIO write to ATA command register, with proper
277 * synchronization with interrupt handler / other threads.
280 * spin_lock_irqsave(host_set lock)
282 void ata_exec_command(struct ata_port
*ap
, struct ata_taskfile
*tf
)
284 if (ap
->flags
& ATA_FLAG_MMIO
)
285 ata_exec_command_mmio(ap
, tf
);
287 ata_exec_command_pio(ap
, tf
);
291 * ata_exec - issue ATA command to host controller
292 * @ap: port to which command is being issued
293 * @tf: ATA taskfile register set
295 * Issues PIO/MMIO write to ATA command register, with proper
296 * synchronization with interrupt handler / other threads.
299 * Obtains host_set lock.
302 static inline void ata_exec(struct ata_port
*ap
, struct ata_taskfile
*tf
)
306 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
307 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
308 ap
->ops
->exec_command(ap
, tf
);
309 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
313 * ata_tf_to_host - issue ATA taskfile to host controller
314 * @ap: port to which command is being issued
315 * @tf: ATA taskfile register set
317 * Issues ATA taskfile register set to ATA host controller,
318 * with proper synchronization with interrupt handler and
322 * Obtains host_set lock.
325 static void ata_tf_to_host(struct ata_port
*ap
, struct ata_taskfile
*tf
)
327 ap
->ops
->tf_load(ap
, tf
);
333 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
334 * @ap: port to which command is being issued
335 * @tf: ATA taskfile register set
337 * Issues ATA taskfile register set to ATA host controller,
338 * with proper synchronization with interrupt handler and
342 * spin_lock_irqsave(host_set lock)
345 void ata_tf_to_host_nolock(struct ata_port
*ap
, struct ata_taskfile
*tf
)
347 ap
->ops
->tf_load(ap
, tf
);
348 ap
->ops
->exec_command(ap
, tf
);
352 * ata_tf_read_pio - input device's ATA taskfile shadow registers
353 * @ap: Port from which input is read
354 * @tf: ATA taskfile register set for storing input
356 * Reads ATA taskfile registers for currently-selected device
360 * Inherited from caller.
363 static void ata_tf_read_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
365 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
367 tf
->nsect
= inb(ioaddr
->nsect_addr
);
368 tf
->lbal
= inb(ioaddr
->lbal_addr
);
369 tf
->lbam
= inb(ioaddr
->lbam_addr
);
370 tf
->lbah
= inb(ioaddr
->lbah_addr
);
371 tf
->device
= inb(ioaddr
->device_addr
);
373 if (tf
->flags
& ATA_TFLAG_LBA48
) {
374 outb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
375 tf
->hob_feature
= inb(ioaddr
->error_addr
);
376 tf
->hob_nsect
= inb(ioaddr
->nsect_addr
);
377 tf
->hob_lbal
= inb(ioaddr
->lbal_addr
);
378 tf
->hob_lbam
= inb(ioaddr
->lbam_addr
);
379 tf
->hob_lbah
= inb(ioaddr
->lbah_addr
);
384 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
385 * @ap: Port from which input is read
386 * @tf: ATA taskfile register set for storing input
388 * Reads ATA taskfile registers for currently-selected device
392 * Inherited from caller.
395 static void ata_tf_read_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
397 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
399 tf
->nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
400 tf
->lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
401 tf
->lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
402 tf
->lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
403 tf
->device
= readb((void __iomem
*)ioaddr
->device_addr
);
405 if (tf
->flags
& ATA_TFLAG_LBA48
) {
406 writeb(tf
->ctl
| ATA_HOB
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
407 tf
->hob_feature
= readb((void __iomem
*)ioaddr
->error_addr
);
408 tf
->hob_nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
409 tf
->hob_lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
410 tf
->hob_lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
411 tf
->hob_lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
417 * ata_tf_read - input device's ATA taskfile shadow registers
418 * @ap: Port from which input is read
419 * @tf: ATA taskfile register set for storing input
421 * Reads ATA taskfile registers for currently-selected device
424 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
425 * is set, also reads the hob registers.
427 * May be used as the tf_read() entry in ata_port_operations.
430 * Inherited from caller.
432 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
434 if (ap
->flags
& ATA_FLAG_MMIO
)
435 ata_tf_read_mmio(ap
, tf
);
437 ata_tf_read_pio(ap
, tf
);
441 * ata_check_status_pio - Read device status reg & clear interrupt
442 * @ap: port where the device is
444 * Reads ATA taskfile status register for currently-selected device
445 * and return its value. This also clears pending interrupts
449 * Inherited from caller.
451 static u8
ata_check_status_pio(struct ata_port
*ap
)
453 return inb(ap
->ioaddr
.status_addr
);
457 * ata_check_status_mmio - Read device status reg & clear interrupt
458 * @ap: port where the device is
460 * Reads ATA taskfile status register for currently-selected device
461 * via MMIO and return its value. This also clears pending interrupts
465 * Inherited from caller.
467 static u8
ata_check_status_mmio(struct ata_port
*ap
)
469 return readb((void __iomem
*) ap
->ioaddr
.status_addr
);
474 * ata_check_status - Read device status reg & clear interrupt
475 * @ap: port where the device is
477 * Reads ATA taskfile status register for currently-selected device
478 * and return its value. This also clears pending interrupts
481 * May be used as the check_status() entry in ata_port_operations.
484 * Inherited from caller.
486 u8
ata_check_status(struct ata_port
*ap
)
488 if (ap
->flags
& ATA_FLAG_MMIO
)
489 return ata_check_status_mmio(ap
);
490 return ata_check_status_pio(ap
);
495 * ata_altstatus - Read device alternate status reg
496 * @ap: port where the device is
498 * Reads ATA taskfile alternate status register for
499 * currently-selected device and return its value.
501 * Note: may NOT be used as the check_altstatus() entry in
502 * ata_port_operations.
505 * Inherited from caller.
507 u8
ata_altstatus(struct ata_port
*ap
)
509 if (ap
->ops
->check_altstatus
)
510 return ap
->ops
->check_altstatus(ap
);
512 if (ap
->flags
& ATA_FLAG_MMIO
)
513 return readb((void __iomem
*)ap
->ioaddr
.altstatus_addr
);
514 return inb(ap
->ioaddr
.altstatus_addr
);
519 * ata_chk_err - Read device error reg
520 * @ap: port where the device is
522 * Reads ATA taskfile error register for
523 * currently-selected device and return its value.
525 * Note: may NOT be used as the check_err() entry in
526 * ata_port_operations.
529 * Inherited from caller.
531 u8
ata_chk_err(struct ata_port
*ap
)
533 if (ap
->ops
->check_err
)
534 return ap
->ops
->check_err(ap
);
536 if (ap
->flags
& ATA_FLAG_MMIO
) {
537 return readb((void __iomem
*) ap
->ioaddr
.error_addr
);
539 return inb(ap
->ioaddr
.error_addr
);
543 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
544 * @tf: Taskfile to convert
545 * @fis: Buffer into which data will output
546 * @pmp: Port multiplier port
548 * Converts a standard ATA taskfile to a Serial ATA
549 * FIS structure (Register - Host to Device).
552 * Inherited from caller.
555 void ata_tf_to_fis(struct ata_taskfile
*tf
, u8
*fis
, u8 pmp
)
557 fis
[0] = 0x27; /* Register - Host to Device FIS */
558 fis
[1] = (pmp
& 0xf) | (1 << 7); /* Port multiplier number,
559 bit 7 indicates Command FIS */
560 fis
[2] = tf
->command
;
561 fis
[3] = tf
->feature
;
568 fis
[8] = tf
->hob_lbal
;
569 fis
[9] = tf
->hob_lbam
;
570 fis
[10] = tf
->hob_lbah
;
571 fis
[11] = tf
->hob_feature
;
574 fis
[13] = tf
->hob_nsect
;
585 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
586 * @fis: Buffer from which data will be input
587 * @tf: Taskfile to output
589 * Converts a standard ATA taskfile to a Serial ATA
590 * FIS structure (Register - Host to Device).
593 * Inherited from caller.
596 void ata_tf_from_fis(u8
*fis
, struct ata_taskfile
*tf
)
598 tf
->command
= fis
[2]; /* status */
599 tf
->feature
= fis
[3]; /* error */
606 tf
->hob_lbal
= fis
[8];
607 tf
->hob_lbam
= fis
[9];
608 tf
->hob_lbah
= fis
[10];
611 tf
->hob_nsect
= fis
[13];
615 * ata_prot_to_cmd - determine which read/write opcodes to use
616 * @protocol: ATA_PROT_xxx taskfile protocol
617 * @lba48: true is lba48 is present
619 * Given necessary input, determine which read/write commands
620 * to use to transfer data.
625 static int ata_prot_to_cmd(int protocol
, int lba48
)
627 int rcmd
= 0, wcmd
= 0;
632 rcmd
= ATA_CMD_PIO_READ_EXT
;
633 wcmd
= ATA_CMD_PIO_WRITE_EXT
;
635 rcmd
= ATA_CMD_PIO_READ
;
636 wcmd
= ATA_CMD_PIO_WRITE
;
642 rcmd
= ATA_CMD_READ_EXT
;
643 wcmd
= ATA_CMD_WRITE_EXT
;
646 wcmd
= ATA_CMD_WRITE
;
654 return rcmd
| (wcmd
<< 8);
658 * ata_dev_set_protocol - set taskfile protocol and r/w commands
659 * @dev: device to examine and configure
661 * Examine the device configuration, after we have
662 * read the identify-device page and configured the
663 * data transfer mode. Set internal state related to
664 * the ATA taskfile protocol (pio, pio mult, dma, etc.)
665 * and calculate the proper read/write commands to use.
670 static void ata_dev_set_protocol(struct ata_device
*dev
)
672 int pio
= (dev
->flags
& ATA_DFLAG_PIO
);
673 int lba48
= (dev
->flags
& ATA_DFLAG_LBA48
);
677 proto
= dev
->xfer_protocol
= ATA_PROT_PIO
;
679 proto
= dev
->xfer_protocol
= ATA_PROT_DMA
;
681 cmd
= ata_prot_to_cmd(proto
, lba48
);
685 dev
->read_cmd
= cmd
& 0xff;
686 dev
->write_cmd
= (cmd
>> 8) & 0xff;
689 static const char * xfer_mode_str
[] = {
709 * ata_udma_string - convert UDMA bit offset to string
710 * @mask: mask of bits supported; only highest bit counts.
712 * Determine string which represents the highest speed
713 * (highest bit in @udma_mask).
719 * Constant C string representing highest speed listed in
720 * @udma_mask, or the constant C string "<n/a>".
723 static const char *ata_mode_string(unsigned int mask
)
727 for (i
= 7; i
>= 0; i
--)
730 for (i
= ATA_SHIFT_MWDMA
+ 2; i
>= ATA_SHIFT_MWDMA
; i
--)
733 for (i
= ATA_SHIFT_PIO
+ 4; i
>= ATA_SHIFT_PIO
; i
--)
740 return xfer_mode_str
[i
];
744 * ata_pio_devchk - PATA device presence detection
745 * @ap: ATA channel to examine
746 * @device: Device to examine (starting at zero)
748 * This technique was originally described in
749 * Hale Landis's ATADRVR (www.ata-atapi.com), and
750 * later found its way into the ATA/ATAPI spec.
752 * Write a pattern to the ATA shadow registers,
753 * and if a device is present, it will respond by
754 * correctly storing and echoing back the
755 * ATA shadow register contents.
761 static unsigned int ata_pio_devchk(struct ata_port
*ap
,
764 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
767 ap
->ops
->dev_select(ap
, device
);
769 outb(0x55, ioaddr
->nsect_addr
);
770 outb(0xaa, ioaddr
->lbal_addr
);
772 outb(0xaa, ioaddr
->nsect_addr
);
773 outb(0x55, ioaddr
->lbal_addr
);
775 outb(0x55, ioaddr
->nsect_addr
);
776 outb(0xaa, ioaddr
->lbal_addr
);
778 nsect
= inb(ioaddr
->nsect_addr
);
779 lbal
= inb(ioaddr
->lbal_addr
);
781 if ((nsect
== 0x55) && (lbal
== 0xaa))
782 return 1; /* we found a device */
784 return 0; /* nothing found */
788 * ata_mmio_devchk - PATA device presence detection
789 * @ap: ATA channel to examine
790 * @device: Device to examine (starting at zero)
792 * This technique was originally described in
793 * Hale Landis's ATADRVR (www.ata-atapi.com), and
794 * later found its way into the ATA/ATAPI spec.
796 * Write a pattern to the ATA shadow registers,
797 * and if a device is present, it will respond by
798 * correctly storing and echoing back the
799 * ATA shadow register contents.
805 static unsigned int ata_mmio_devchk(struct ata_port
*ap
,
808 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
811 ap
->ops
->dev_select(ap
, device
);
813 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
814 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
816 writeb(0xaa, (void __iomem
*) ioaddr
->nsect_addr
);
817 writeb(0x55, (void __iomem
*) ioaddr
->lbal_addr
);
819 writeb(0x55, (void __iomem
*) ioaddr
->nsect_addr
);
820 writeb(0xaa, (void __iomem
*) ioaddr
->lbal_addr
);
822 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
823 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
825 if ((nsect
== 0x55) && (lbal
== 0xaa))
826 return 1; /* we found a device */
828 return 0; /* nothing found */
832 * ata_devchk - PATA device presence detection
833 * @ap: ATA channel to examine
834 * @device: Device to examine (starting at zero)
836 * Dispatch ATA device presence detection, depending
837 * on whether we are using PIO or MMIO to talk to the
838 * ATA shadow registers.
844 static unsigned int ata_devchk(struct ata_port
*ap
,
847 if (ap
->flags
& ATA_FLAG_MMIO
)
848 return ata_mmio_devchk(ap
, device
);
849 return ata_pio_devchk(ap
, device
);
853 * ata_dev_classify - determine device type based on ATA-spec signature
854 * @tf: ATA taskfile register set for device to be identified
856 * Determine from taskfile register contents whether a device is
857 * ATA or ATAPI, as per "Signature and persistence" section
858 * of ATA/PI spec (volume 1, sect 5.14).
864 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
865 * the event of failure.
868 unsigned int ata_dev_classify(struct ata_taskfile
*tf
)
870 /* Apple's open source Darwin code hints that some devices only
871 * put a proper signature into the LBA mid/high registers,
872 * So, we only check those. It's sufficient for uniqueness.
875 if (((tf
->lbam
== 0) && (tf
->lbah
== 0)) ||
876 ((tf
->lbam
== 0x3c) && (tf
->lbah
== 0xc3))) {
877 DPRINTK("found ATA device by sig\n");
881 if (((tf
->lbam
== 0x14) && (tf
->lbah
== 0xeb)) ||
882 ((tf
->lbam
== 0x69) && (tf
->lbah
== 0x96))) {
883 DPRINTK("found ATAPI device by sig\n");
884 return ATA_DEV_ATAPI
;
887 DPRINTK("unknown device\n");
888 return ATA_DEV_UNKNOWN
;
892 * ata_dev_try_classify - Parse returned ATA device signature
893 * @ap: ATA channel to examine
894 * @device: Device to examine (starting at zero)
896 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
897 * an ATA/ATAPI-defined set of values is placed in the ATA
898 * shadow registers, indicating the results of device detection
901 * Select the ATA device, and read the values from the ATA shadow
902 * registers. Then parse according to the Error register value,
903 * and the spec-defined values examined by ata_dev_classify().
909 static u8
ata_dev_try_classify(struct ata_port
*ap
, unsigned int device
)
911 struct ata_device
*dev
= &ap
->device
[device
];
912 struct ata_taskfile tf
;
916 ap
->ops
->dev_select(ap
, device
);
918 memset(&tf
, 0, sizeof(tf
));
920 err
= ata_chk_err(ap
);
921 ap
->ops
->tf_read(ap
, &tf
);
923 dev
->class = ATA_DEV_NONE
;
925 /* see if device passed diags */
928 else if ((device
== 0) && (err
== 0x81))
933 /* determine if device if ATA or ATAPI */
934 class = ata_dev_classify(&tf
);
935 if (class == ATA_DEV_UNKNOWN
)
937 if ((class == ATA_DEV_ATA
) && (ata_chk_status(ap
) == 0))
946 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
947 * @id: IDENTIFY DEVICE results we will examine
948 * @s: string into which data is output
949 * @ofs: offset into identify device page
950 * @len: length of string to return. must be an even number.
952 * The strings in the IDENTIFY DEVICE page are broken up into
953 * 16-bit chunks. Run through the string, and output each
954 * 8-bit chunk linearly, regardless of platform.
960 void ata_dev_id_string(u16
*id
, unsigned char *s
,
961 unsigned int ofs
, unsigned int len
)
981 * ata_noop_dev_select - Select device 0/1 on ATA bus
982 * @ap: ATA channel to manipulate
983 * @device: ATA device (numbered from zero) to select
985 * This function performs no actual function.
987 * May be used as the dev_select() entry in ata_port_operations.
992 void ata_noop_dev_select (struct ata_port
*ap
, unsigned int device
)
998 * ata_std_dev_select - Select device 0/1 on ATA bus
999 * @ap: ATA channel to manipulate
1000 * @device: ATA device (numbered from zero) to select
1002 * Use the method defined in the ATA specification to
1003 * make either device 0, or device 1, active on the
1004 * ATA channel. Works with both PIO and MMIO.
1006 * May be used as the dev_select() entry in ata_port_operations.
1012 void ata_std_dev_select (struct ata_port
*ap
, unsigned int device
)
1017 tmp
= ATA_DEVICE_OBS
;
1019 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
1021 if (ap
->flags
& ATA_FLAG_MMIO
) {
1022 writeb(tmp
, (void __iomem
*) ap
->ioaddr
.device_addr
);
1024 outb(tmp
, ap
->ioaddr
.device_addr
);
1026 ata_pause(ap
); /* needed; also flushes, for mmio */
1030 * ata_dev_select - Select device 0/1 on ATA bus
1031 * @ap: ATA channel to manipulate
1032 * @device: ATA device (numbered from zero) to select
1033 * @wait: non-zero to wait for Status register BSY bit to clear
1034 * @can_sleep: non-zero if context allows sleeping
1036 * Use the method defined in the ATA specification to
1037 * make either device 0, or device 1, active on the
1040 * This is a high-level version of ata_std_dev_select(),
1041 * which additionally provides the services of inserting
1042 * the proper pauses and status polling, where needed.
1048 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
1049 unsigned int wait
, unsigned int can_sleep
)
1051 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1052 ap
->id
, device
, wait
);
1057 ap
->ops
->dev_select(ap
, device
);
1060 if (can_sleep
&& ap
->device
[device
].class == ATA_DEV_ATAPI
)
1067 * ata_dump_id - IDENTIFY DEVICE info debugging output
1068 * @dev: Device whose IDENTIFY DEVICE page we will dump
1070 * Dump selected 16-bit words from a detected device's
1071 * IDENTIFY PAGE page.
1077 static inline void ata_dump_id(struct ata_device
*dev
)
1079 DPRINTK("49==0x%04x "
1089 DPRINTK("80==0x%04x "
1099 DPRINTK("88==0x%04x "
1106 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1107 * @ap: port on which device we wish to probe resides
1108 * @device: device bus address, starting at zero
1110 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1111 * command, and read back the 512-byte device information page.
1112 * The device information page is fed to us via the standard
1113 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1114 * using standard PIO-IN paths)
1116 * After reading the device information page, we use several
1117 * bits of information from it to initialize data structures
1118 * that will be used during the lifetime of the ata_device.
1119 * Other data from the info page is used to disqualify certain
1120 * older ATA devices we do not wish to support.
1123 * Inherited from caller. Some functions called by this function
1124 * obtain the host_set lock.
1127 static void ata_dev_identify(struct ata_port
*ap
, unsigned int device
)
1129 struct ata_device
*dev
= &ap
->device
[device
];
1132 unsigned long xfer_modes
;
1134 unsigned int using_edd
;
1135 DECLARE_COMPLETION(wait
);
1136 struct ata_queued_cmd
*qc
;
1137 unsigned long flags
;
1140 if (!ata_dev_present(dev
)) {
1141 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1146 if (ap
->flags
& (ATA_FLAG_SRST
| ATA_FLAG_SATA_RESET
))
1151 DPRINTK("ENTER, host %u, dev %u\n", ap
->id
, device
);
1153 assert (dev
->class == ATA_DEV_ATA
|| dev
->class == ATA_DEV_ATAPI
||
1154 dev
->class == ATA_DEV_NONE
);
1156 ata_dev_select(ap
, device
, 1, 1); /* select device 0/1 */
1158 qc
= ata_qc_new_init(ap
, dev
);
1161 ata_sg_init_one(qc
, dev
->id
, sizeof(dev
->id
));
1162 qc
->dma_dir
= DMA_FROM_DEVICE
;
1163 qc
->tf
.protocol
= ATA_PROT_PIO
;
1167 if (dev
->class == ATA_DEV_ATA
) {
1168 qc
->tf
.command
= ATA_CMD_ID_ATA
;
1169 DPRINTK("do ATA identify\n");
1171 qc
->tf
.command
= ATA_CMD_ID_ATAPI
;
1172 DPRINTK("do ATAPI identify\n");
1175 qc
->waiting
= &wait
;
1176 qc
->complete_fn
= ata_qc_complete_noop
;
1178 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1179 rc
= ata_qc_issue(qc
);
1180 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1185 wait_for_completion(&wait
);
1187 status
= ata_chk_status(ap
);
1188 if (status
& ATA_ERR
) {
1190 * arg! EDD works for all test cases, but seems to return
1191 * the ATA signature for some ATAPI devices. Until the
1192 * reason for this is found and fixed, we fix up the mess
1193 * here. If IDENTIFY DEVICE returns command aborted
1194 * (as ATAPI devices do), then we issue an
1195 * IDENTIFY PACKET DEVICE.
1197 * ATA software reset (SRST, the default) does not appear
1198 * to have this problem.
1200 if ((using_edd
) && (qc
->tf
.command
== ATA_CMD_ID_ATA
)) {
1201 u8 err
= ata_chk_err(ap
);
1202 if (err
& ATA_ABORTED
) {
1203 dev
->class = ATA_DEV_ATAPI
;
1214 swap_buf_le16(dev
->id
, ATA_ID_WORDS
);
1216 /* print device capabilities */
1217 printk(KERN_DEBUG
"ata%u: dev %u cfg "
1218 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1219 ap
->id
, device
, dev
->id
[49],
1220 dev
->id
[82], dev
->id
[83], dev
->id
[84],
1221 dev
->id
[85], dev
->id
[86], dev
->id
[87],
1225 * common ATA, ATAPI feature tests
1228 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
1229 if (!ata_id_has_dma(dev
->id
) || !ata_id_has_lba(dev
->id
)) {
1230 printk(KERN_DEBUG
"ata%u: no dma/lba\n", ap
->id
);
1234 /* quick-n-dirty find max transfer mode; for printk only */
1235 xfer_modes
= dev
->id
[ATA_ID_UDMA_MODES
];
1237 xfer_modes
= (dev
->id
[ATA_ID_MWDMA_MODES
]) << ATA_SHIFT_MWDMA
;
1239 xfer_modes
= (dev
->id
[ATA_ID_PIO_MODES
]) << (ATA_SHIFT_PIO
+ 3);
1240 xfer_modes
|= (0x7 << ATA_SHIFT_PIO
);
1245 /* ATA-specific feature tests */
1246 if (dev
->class == ATA_DEV_ATA
) {
1247 if (!ata_id_is_ata(dev
->id
)) /* sanity check */
1250 tmp
= dev
->id
[ATA_ID_MAJOR_VER
];
1251 for (i
= 14; i
>= 1; i
--)
1255 /* we require at least ATA-3 */
1257 printk(KERN_DEBUG
"ata%u: no ATA-3\n", ap
->id
);
1261 if (ata_id_has_lba48(dev
->id
)) {
1262 dev
->flags
|= ATA_DFLAG_LBA48
;
1263 dev
->n_sectors
= ata_id_u64(dev
->id
, 100);
1265 dev
->n_sectors
= ata_id_u32(dev
->id
, 60);
1268 ap
->host
->max_cmd_len
= 16;
1270 /* print device info to dmesg */
1271 printk(KERN_INFO
"ata%u: dev %u ATA, max %s, %Lu sectors:%s\n",
1273 ata_mode_string(xfer_modes
),
1274 (unsigned long long)dev
->n_sectors
,
1275 dev
->flags
& ATA_DFLAG_LBA48
? " lba48" : "");
1278 /* ATAPI-specific feature tests */
1280 if (ata_id_is_ata(dev
->id
)) /* sanity check */
1283 rc
= atapi_cdb_len(dev
->id
);
1284 if ((rc
< 12) || (rc
> ATAPI_CDB_LEN
)) {
1285 printk(KERN_WARNING
"ata%u: unsupported CDB len\n", ap
->id
);
1288 ap
->cdb_len
= (unsigned int) rc
;
1289 ap
->host
->max_cmd_len
= (unsigned char) ap
->cdb_len
;
1291 /* print device info to dmesg */
1292 printk(KERN_INFO
"ata%u: dev %u ATAPI, max %s\n",
1294 ata_mode_string(xfer_modes
));
1297 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap
));
1301 printk(KERN_WARNING
"ata%u: dev %u not supported, ignoring\n",
1304 dev
->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1305 DPRINTK("EXIT, err\n");
1309 static inline u8
ata_dev_knobble(struct ata_port
*ap
)
1311 return ((ap
->cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(ap
->device
->id
)));
1315 * ata_dev_config - Run device specific handlers and check for
1316 * SATA->PATA bridges
1323 void ata_dev_config(struct ata_port
*ap
, unsigned int i
)
1325 /* limit bridge transfers to udma5, 200 sectors */
1326 if (ata_dev_knobble(ap
)) {
1327 printk(KERN_INFO
"ata%u(%u): applying bridge limits\n",
1328 ap
->id
, ap
->device
->devno
);
1329 ap
->udma_mask
&= ATA_UDMA5
;
1330 ap
->host
->max_sectors
= ATA_MAX_SECTORS
;
1331 ap
->host
->hostt
->max_sectors
= ATA_MAX_SECTORS
;
1332 ap
->device
->flags
|= ATA_DFLAG_LOCK_SECTORS
;
1335 if (ap
->ops
->dev_config
)
1336 ap
->ops
->dev_config(ap
, &ap
->device
[i
]);
1340 * ata_bus_probe - Reset and probe ATA bus
1343 * Master ATA bus probing function. Initiates a hardware-dependent
1344 * bus reset, then attempts to identify any devices found on
1348 * PCI/etc. bus probe sem.
1351 * Zero on success, non-zero on error.
1354 static int ata_bus_probe(struct ata_port
*ap
)
1356 unsigned int i
, found
= 0;
1358 ap
->ops
->phy_reset(ap
);
1359 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1362 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1363 ata_dev_identify(ap
, i
);
1364 if (ata_dev_present(&ap
->device
[i
])) {
1366 ata_dev_config(ap
,i
);
1370 if ((!found
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1371 goto err_out_disable
;
1374 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1375 goto err_out_disable
;
1380 ap
->ops
->port_disable(ap
);
1386 * ata_port_probe - Mark port as enabled
1387 * @ap: Port for which we indicate enablement
1389 * Modify @ap data structure such that the system
1390 * thinks that the entire port is enabled.
1392 * LOCKING: host_set lock, or some other form of
1396 void ata_port_probe(struct ata_port
*ap
)
1398 ap
->flags
&= ~ATA_FLAG_PORT_DISABLED
;
1402 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1403 * @ap: SATA port associated with target SATA PHY.
1405 * This function issues commands to standard SATA Sxxx
1406 * PHY registers, to wake up the phy (and device), and
1407 * clear any reset condition.
1410 * PCI/etc. bus probe sem.
1413 void __sata_phy_reset(struct ata_port
*ap
)
1416 unsigned long timeout
= jiffies
+ (HZ
* 5);
1418 if (ap
->flags
& ATA_FLAG_SATA_RESET
) {
1419 /* issue phy wake/reset */
1420 scr_write_flush(ap
, SCR_CONTROL
, 0x301);
1421 /* Couldn't find anything in SATA I/II specs, but
1422 * AHCI-1.1 10.4.2 says at least 1 ms. */
1425 scr_write_flush(ap
, SCR_CONTROL
, 0x300); /* phy wake/clear reset */
1427 /* wait for phy to become ready, if necessary */
1430 sstatus
= scr_read(ap
, SCR_STATUS
);
1431 if ((sstatus
& 0xf) != 1)
1433 } while (time_before(jiffies
, timeout
));
1435 /* TODO: phy layer with polling, timeouts, etc. */
1436 if (sata_dev_present(ap
))
1439 sstatus
= scr_read(ap
, SCR_STATUS
);
1440 printk(KERN_INFO
"ata%u: no device found (phy stat %08x)\n",
1442 ata_port_disable(ap
);
1445 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1448 if (ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
)) {
1449 ata_port_disable(ap
);
1453 ap
->cbl
= ATA_CBL_SATA
;
1457 * sata_phy_reset - Reset SATA bus.
1458 * @ap: SATA port associated with target SATA PHY.
1460 * This function resets the SATA bus, and then probes
1461 * the bus for devices.
1464 * PCI/etc. bus probe sem.
1467 void sata_phy_reset(struct ata_port
*ap
)
1469 __sata_phy_reset(ap
);
1470 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1476 * ata_port_disable - Disable port.
1477 * @ap: Port to be disabled.
1479 * Modify @ap data structure such that the system
1480 * thinks that the entire port is disabled, and should
1481 * never attempt to probe or communicate with devices
1484 * LOCKING: host_set lock, or some other form of
1488 void ata_port_disable(struct ata_port
*ap
)
1490 ap
->device
[0].class = ATA_DEV_NONE
;
1491 ap
->device
[1].class = ATA_DEV_NONE
;
1492 ap
->flags
|= ATA_FLAG_PORT_DISABLED
;
1498 } xfer_mode_classes
[] = {
1499 { ATA_SHIFT_UDMA
, XFER_UDMA_0
},
1500 { ATA_SHIFT_MWDMA
, XFER_MW_DMA_0
},
1501 { ATA_SHIFT_PIO
, XFER_PIO_0
},
1504 static inline u8
base_from_shift(unsigned int shift
)
1508 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++)
1509 if (xfer_mode_classes
[i
].shift
== shift
)
1510 return xfer_mode_classes
[i
].base
;
1515 static void ata_dev_set_mode(struct ata_port
*ap
, struct ata_device
*dev
)
1520 if (!ata_dev_present(dev
) || (ap
->flags
& ATA_FLAG_PORT_DISABLED
))
1523 if (dev
->xfer_shift
== ATA_SHIFT_PIO
)
1524 dev
->flags
|= ATA_DFLAG_PIO
;
1526 ata_dev_set_xfermode(ap
, dev
);
1528 base
= base_from_shift(dev
->xfer_shift
);
1529 ofs
= dev
->xfer_mode
- base
;
1530 idx
= ofs
+ dev
->xfer_shift
;
1531 WARN_ON(idx
>= ARRAY_SIZE(xfer_mode_str
));
1533 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1534 idx
, dev
->xfer_shift
, (int)dev
->xfer_mode
, (int)base
, ofs
);
1536 printk(KERN_INFO
"ata%u: dev %u configured for %s\n",
1537 ap
->id
, dev
->devno
, xfer_mode_str
[idx
]);
1540 static int ata_host_set_pio(struct ata_port
*ap
)
1546 mask
= ata_get_mode_mask(ap
, ATA_SHIFT_PIO
);
1549 printk(KERN_WARNING
"ata%u: no PIO support\n", ap
->id
);
1553 base
= base_from_shift(ATA_SHIFT_PIO
);
1554 xfer_mode
= base
+ x
;
1556 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1557 (int)base
, (int)xfer_mode
, mask
, x
);
1559 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1560 struct ata_device
*dev
= &ap
->device
[i
];
1561 if (ata_dev_present(dev
)) {
1562 dev
->pio_mode
= xfer_mode
;
1563 dev
->xfer_mode
= xfer_mode
;
1564 dev
->xfer_shift
= ATA_SHIFT_PIO
;
1565 if (ap
->ops
->set_piomode
)
1566 ap
->ops
->set_piomode(ap
, dev
);
1573 static void ata_host_set_dma(struct ata_port
*ap
, u8 xfer_mode
,
1574 unsigned int xfer_shift
)
1578 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1579 struct ata_device
*dev
= &ap
->device
[i
];
1580 if (ata_dev_present(dev
)) {
1581 dev
->dma_mode
= xfer_mode
;
1582 dev
->xfer_mode
= xfer_mode
;
1583 dev
->xfer_shift
= xfer_shift
;
1584 if (ap
->ops
->set_dmamode
)
1585 ap
->ops
->set_dmamode(ap
, dev
);
1591 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1592 * @ap: port on which timings will be programmed
1594 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1597 * PCI/etc. bus probe sem.
1600 static void ata_set_mode(struct ata_port
*ap
)
1602 unsigned int i
, xfer_shift
;
1606 /* step 1: always set host PIO timings */
1607 rc
= ata_host_set_pio(ap
);
1611 /* step 2: choose the best data xfer mode */
1612 xfer_mode
= xfer_shift
= 0;
1613 rc
= ata_choose_xfer_mode(ap
, &xfer_mode
, &xfer_shift
);
1617 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1618 if (xfer_shift
!= ATA_SHIFT_PIO
)
1619 ata_host_set_dma(ap
, xfer_mode
, xfer_shift
);
1621 /* step 4: update devices' xfer mode */
1622 ata_dev_set_mode(ap
, &ap
->device
[0]);
1623 ata_dev_set_mode(ap
, &ap
->device
[1]);
1625 if (ap
->flags
& ATA_FLAG_PORT_DISABLED
)
1628 if (ap
->ops
->post_set_mode
)
1629 ap
->ops
->post_set_mode(ap
);
1631 for (i
= 0; i
< 2; i
++) {
1632 struct ata_device
*dev
= &ap
->device
[i
];
1633 ata_dev_set_protocol(dev
);
1639 ata_port_disable(ap
);
1643 * ata_busy_sleep - sleep until BSY clears, or timeout
1644 * @ap: port containing status register to be polled
1645 * @tmout_pat: impatience timeout
1646 * @tmout: overall timeout
1648 * Sleep until ATA Status register bit BSY clears,
1649 * or a timeout occurs.
1655 static unsigned int ata_busy_sleep (struct ata_port
*ap
,
1656 unsigned long tmout_pat
,
1657 unsigned long tmout
)
1659 unsigned long timer_start
, timeout
;
1662 status
= ata_busy_wait(ap
, ATA_BUSY
, 300);
1663 timer_start
= jiffies
;
1664 timeout
= timer_start
+ tmout_pat
;
1665 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1667 status
= ata_busy_wait(ap
, ATA_BUSY
, 3);
1670 if (status
& ATA_BUSY
)
1671 printk(KERN_WARNING
"ata%u is slow to respond, "
1672 "please be patient\n", ap
->id
);
1674 timeout
= timer_start
+ tmout
;
1675 while ((status
& ATA_BUSY
) && (time_before(jiffies
, timeout
))) {
1677 status
= ata_chk_status(ap
);
1680 if (status
& ATA_BUSY
) {
1681 printk(KERN_ERR
"ata%u failed to respond (%lu secs)\n",
1682 ap
->id
, tmout
/ HZ
);
1689 static void ata_bus_post_reset(struct ata_port
*ap
, unsigned int devmask
)
1691 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1692 unsigned int dev0
= devmask
& (1 << 0);
1693 unsigned int dev1
= devmask
& (1 << 1);
1694 unsigned long timeout
;
1696 /* if device 0 was found in ata_devchk, wait for its
1700 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1702 /* if device 1 was found in ata_devchk, wait for
1703 * register access, then wait for BSY to clear
1705 timeout
= jiffies
+ ATA_TMOUT_BOOT
;
1709 ap
->ops
->dev_select(ap
, 1);
1710 if (ap
->flags
& ATA_FLAG_MMIO
) {
1711 nsect
= readb((void __iomem
*) ioaddr
->nsect_addr
);
1712 lbal
= readb((void __iomem
*) ioaddr
->lbal_addr
);
1714 nsect
= inb(ioaddr
->nsect_addr
);
1715 lbal
= inb(ioaddr
->lbal_addr
);
1717 if ((nsect
== 1) && (lbal
== 1))
1719 if (time_after(jiffies
, timeout
)) {
1723 msleep(50); /* give drive a breather */
1726 ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1728 /* is all this really necessary? */
1729 ap
->ops
->dev_select(ap
, 0);
1731 ap
->ops
->dev_select(ap
, 1);
1733 ap
->ops
->dev_select(ap
, 0);
1737 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1738 * @ap: Port to reset and probe
1740 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1741 * probe the bus. Not often used these days.
1744 * PCI/etc. bus probe sem.
1748 static unsigned int ata_bus_edd(struct ata_port
*ap
)
1750 struct ata_taskfile tf
;
1752 /* set up execute-device-diag (bus reset) taskfile */
1753 /* also, take interrupts to a known state (disabled) */
1754 DPRINTK("execute-device-diag\n");
1755 ata_tf_init(ap
, &tf
, 0);
1757 tf
.command
= ATA_CMD_EDD
;
1758 tf
.protocol
= ATA_PROT_NODATA
;
1761 ata_tf_to_host(ap
, &tf
);
1763 /* spec says at least 2ms. but who knows with those
1764 * crazy ATAPI devices...
1768 return ata_busy_sleep(ap
, ATA_TMOUT_BOOT_QUICK
, ATA_TMOUT_BOOT
);
1771 static unsigned int ata_bus_softreset(struct ata_port
*ap
,
1772 unsigned int devmask
)
1774 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1776 DPRINTK("ata%u: bus reset via SRST\n", ap
->id
);
1778 /* software reset. causes dev0 to be selected */
1779 if (ap
->flags
& ATA_FLAG_MMIO
) {
1780 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1781 udelay(20); /* FIXME: flush */
1782 writeb(ap
->ctl
| ATA_SRST
, (void __iomem
*) ioaddr
->ctl_addr
);
1783 udelay(20); /* FIXME: flush */
1784 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1786 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1788 outb(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
1790 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1793 /* spec mandates ">= 2ms" before checking status.
1794 * We wait 150ms, because that was the magic delay used for
1795 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1796 * between when the ATA command register is written, and then
1797 * status is checked. Because waiting for "a while" before
1798 * checking status is fine, post SRST, we perform this magic
1799 * delay here as well.
1803 ata_bus_post_reset(ap
, devmask
);
1809 * ata_bus_reset - reset host port and associated ATA channel
1810 * @ap: port to reset
1812 * This is typically the first time we actually start issuing
1813 * commands to the ATA channel. We wait for BSY to clear, then
1814 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1815 * result. Determine what devices, if any, are on the channel
1816 * by looking at the device 0/1 error register. Look at the signature
1817 * stored in each device's taskfile registers, to determine if
1818 * the device is ATA or ATAPI.
1821 * PCI/etc. bus probe sem.
1822 * Obtains host_set lock.
1825 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1828 void ata_bus_reset(struct ata_port
*ap
)
1830 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1831 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
1833 unsigned int dev0
, dev1
= 0, rc
= 0, devmask
= 0;
1835 DPRINTK("ENTER, host %u, port %u\n", ap
->id
, ap
->port_no
);
1837 /* determine if device 0/1 are present */
1838 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
1841 dev0
= ata_devchk(ap
, 0);
1843 dev1
= ata_devchk(ap
, 1);
1847 devmask
|= (1 << 0);
1849 devmask
|= (1 << 1);
1851 /* select device 0 again */
1852 ap
->ops
->dev_select(ap
, 0);
1854 /* issue bus reset */
1855 if (ap
->flags
& ATA_FLAG_SRST
)
1856 rc
= ata_bus_softreset(ap
, devmask
);
1857 else if ((ap
->flags
& ATA_FLAG_SATA_RESET
) == 0) {
1858 /* set up device control */
1859 if (ap
->flags
& ATA_FLAG_MMIO
)
1860 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1862 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1863 rc
= ata_bus_edd(ap
);
1870 * determine by signature whether we have ATA or ATAPI devices
1872 err
= ata_dev_try_classify(ap
, 0);
1873 if ((slave_possible
) && (err
!= 0x81))
1874 ata_dev_try_classify(ap
, 1);
1876 /* re-enable interrupts */
1877 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
1880 /* is double-select really necessary? */
1881 if (ap
->device
[1].class != ATA_DEV_NONE
)
1882 ap
->ops
->dev_select(ap
, 1);
1883 if (ap
->device
[0].class != ATA_DEV_NONE
)
1884 ap
->ops
->dev_select(ap
, 0);
1886 /* if no devices were detected, disable this port */
1887 if ((ap
->device
[0].class == ATA_DEV_NONE
) &&
1888 (ap
->device
[1].class == ATA_DEV_NONE
))
1891 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
1892 /* set up device control for ATA_FLAG_SATA_RESET */
1893 if (ap
->flags
& ATA_FLAG_MMIO
)
1894 writeb(ap
->ctl
, (void __iomem
*) ioaddr
->ctl_addr
);
1896 outb(ap
->ctl
, ioaddr
->ctl_addr
);
1903 printk(KERN_ERR
"ata%u: disabling port\n", ap
->id
);
1904 ap
->ops
->port_disable(ap
);
1909 static void ata_pr_blacklisted(struct ata_port
*ap
, struct ata_device
*dev
)
1911 printk(KERN_WARNING
"ata%u: dev %u is on DMA blacklist, disabling DMA\n",
1912 ap
->id
, dev
->devno
);
1915 static const char * ata_dma_blacklist
[] = {
1934 "Toshiba CD-ROM XM-6202B",
1935 "TOSHIBA CD-ROM XM-1702BC",
1937 "E-IDE CD-ROM CR-840",
1940 "SAMSUNG CD-ROM SC-148C",
1941 "SAMSUNG CD-ROM SC",
1943 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
1947 static int ata_dma_blacklisted(struct ata_port
*ap
, struct ata_device
*dev
)
1949 unsigned char model_num
[40];
1954 ata_dev_id_string(dev
->id
, model_num
, ATA_ID_PROD_OFS
,
1957 len
= strnlen(s
, sizeof(model_num
));
1959 /* ATAPI specifies that empty space is blank-filled; remove blanks */
1960 while ((len
> 0) && (s
[len
- 1] == ' ')) {
1965 for (i
= 0; i
< ARRAY_SIZE(ata_dma_blacklist
); i
++)
1966 if (!strncmp(ata_dma_blacklist
[i
], s
, len
))
1972 static unsigned int ata_get_mode_mask(struct ata_port
*ap
, int shift
)
1974 struct ata_device
*master
, *slave
;
1977 master
= &ap
->device
[0];
1978 slave
= &ap
->device
[1];
1980 assert (ata_dev_present(master
) || ata_dev_present(slave
));
1982 if (shift
== ATA_SHIFT_UDMA
) {
1983 mask
= ap
->udma_mask
;
1984 if (ata_dev_present(master
)) {
1985 mask
&= (master
->id
[ATA_ID_UDMA_MODES
] & 0xff);
1986 if (ata_dma_blacklisted(ap
, master
)) {
1988 ata_pr_blacklisted(ap
, master
);
1991 if (ata_dev_present(slave
)) {
1992 mask
&= (slave
->id
[ATA_ID_UDMA_MODES
] & 0xff);
1993 if (ata_dma_blacklisted(ap
, slave
)) {
1995 ata_pr_blacklisted(ap
, slave
);
1999 else if (shift
== ATA_SHIFT_MWDMA
) {
2000 mask
= ap
->mwdma_mask
;
2001 if (ata_dev_present(master
)) {
2002 mask
&= (master
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2003 if (ata_dma_blacklisted(ap
, master
)) {
2005 ata_pr_blacklisted(ap
, master
);
2008 if (ata_dev_present(slave
)) {
2009 mask
&= (slave
->id
[ATA_ID_MWDMA_MODES
] & 0x07);
2010 if (ata_dma_blacklisted(ap
, slave
)) {
2012 ata_pr_blacklisted(ap
, slave
);
2016 else if (shift
== ATA_SHIFT_PIO
) {
2017 mask
= ap
->pio_mask
;
2018 if (ata_dev_present(master
)) {
2019 /* spec doesn't return explicit support for
2020 * PIO0-2, so we fake it
2022 u16 tmp_mode
= master
->id
[ATA_ID_PIO_MODES
] & 0x03;
2027 if (ata_dev_present(slave
)) {
2028 /* spec doesn't return explicit support for
2029 * PIO0-2, so we fake it
2031 u16 tmp_mode
= slave
->id
[ATA_ID_PIO_MODES
] & 0x03;
2038 mask
= 0xffffffff; /* shut up compiler warning */
2045 /* find greatest bit */
2046 static int fgb(u32 bitmap
)
2051 for (i
= 0; i
< 32; i
++)
2052 if (bitmap
& (1 << i
))
2059 * ata_choose_xfer_mode - attempt to find best transfer mode
2060 * @ap: Port for which an xfer mode will be selected
2061 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2062 * @xfer_shift_out: (output) bit shift that selects this mode
2064 * Based on host and device capabilities, determine the
2065 * maximum transfer mode that is amenable to all.
2068 * PCI/etc. bus probe sem.
2071 * Zero on success, negative on error.
2074 static int ata_choose_xfer_mode(struct ata_port
*ap
,
2076 unsigned int *xfer_shift_out
)
2078 unsigned int mask
, shift
;
2081 for (i
= 0; i
< ARRAY_SIZE(xfer_mode_classes
); i
++) {
2082 shift
= xfer_mode_classes
[i
].shift
;
2083 mask
= ata_get_mode_mask(ap
, shift
);
2087 *xfer_mode_out
= xfer_mode_classes
[i
].base
+ x
;
2088 *xfer_shift_out
= shift
;
2097 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2098 * @ap: Port associated with device @dev
2099 * @dev: Device to which command will be sent
2101 * Issue SET FEATURES - XFER MODE command to device @dev
2105 * PCI/etc. bus probe sem.
2108 static void ata_dev_set_xfermode(struct ata_port
*ap
, struct ata_device
*dev
)
2110 DECLARE_COMPLETION(wait
);
2111 struct ata_queued_cmd
*qc
;
2113 unsigned long flags
;
2115 /* set up set-features taskfile */
2116 DPRINTK("set features - xfer mode\n");
2118 qc
= ata_qc_new_init(ap
, dev
);
2121 qc
->tf
.command
= ATA_CMD_SET_FEATURES
;
2122 qc
->tf
.feature
= SETFEATURES_XFER
;
2123 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2124 qc
->tf
.protocol
= ATA_PROT_NODATA
;
2125 qc
->tf
.nsect
= dev
->xfer_mode
;
2127 qc
->waiting
= &wait
;
2128 qc
->complete_fn
= ata_qc_complete_noop
;
2130 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2131 rc
= ata_qc_issue(qc
);
2132 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2135 ata_port_disable(ap
);
2137 wait_for_completion(&wait
);
2143 * ata_sg_clean - Unmap DMA memory associated with command
2144 * @qc: Command containing DMA memory to be released
2146 * Unmap all mapped DMA memory associated with this command.
2149 * spin_lock_irqsave(host_set lock)
2152 static void ata_sg_clean(struct ata_queued_cmd
*qc
)
2154 struct ata_port
*ap
= qc
->ap
;
2155 struct scatterlist
*sg
= qc
->sg
;
2156 int dir
= qc
->dma_dir
;
2158 assert(qc
->flags
& ATA_QCFLAG_DMAMAP
);
2161 if (qc
->flags
& ATA_QCFLAG_SINGLE
)
2162 assert(qc
->n_elem
== 1);
2164 DPRINTK("unmapping %u sg elements\n", qc
->n_elem
);
2166 if (qc
->flags
& ATA_QCFLAG_SG
)
2167 dma_unmap_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2169 dma_unmap_single(ap
->host_set
->dev
, sg_dma_address(&sg
[0]),
2170 sg_dma_len(&sg
[0]), dir
);
2172 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
2177 * ata_fill_sg - Fill PCI IDE PRD table
2178 * @qc: Metadata associated with taskfile to be transferred
2180 * Fill PCI IDE PRD (scatter-gather) table with segments
2181 * associated with the current disk command.
2184 * spin_lock_irqsave(host_set lock)
2187 static void ata_fill_sg(struct ata_queued_cmd
*qc
)
2189 struct scatterlist
*sg
= qc
->sg
;
2190 struct ata_port
*ap
= qc
->ap
;
2191 unsigned int idx
, nelem
;
2194 assert(qc
->n_elem
> 0);
2197 for (nelem
= qc
->n_elem
; nelem
; nelem
--,sg
++) {
2201 /* determine if physical DMA addr spans 64K boundary.
2202 * Note h/w doesn't support 64-bit, so we unconditionally
2203 * truncate dma_addr_t to u32.
2205 addr
= (u32
) sg_dma_address(sg
);
2206 sg_len
= sg_dma_len(sg
);
2209 offset
= addr
& 0xffff;
2211 if ((offset
+ sg_len
) > 0x10000)
2212 len
= 0x10000 - offset
;
2214 ap
->prd
[idx
].addr
= cpu_to_le32(addr
);
2215 ap
->prd
[idx
].flags_len
= cpu_to_le32(len
& 0xffff);
2216 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx
, addr
, len
);
2225 ap
->prd
[idx
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
2228 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2229 * @qc: Metadata associated with taskfile to check
2231 * Allow low-level driver to filter ATA PACKET commands, returning
2232 * a status indicating whether or not it is OK to use DMA for the
2233 * supplied PACKET command.
2236 * spin_lock_irqsave(host_set lock)
2238 * RETURNS: 0 when ATAPI DMA can be used
2241 int ata_check_atapi_dma(struct ata_queued_cmd
*qc
)
2243 struct ata_port
*ap
= qc
->ap
;
2244 int rc
= 0; /* Assume ATAPI DMA is OK by default */
2246 if (ap
->ops
->check_atapi_dma
)
2247 rc
= ap
->ops
->check_atapi_dma(qc
);
2252 * ata_qc_prep - Prepare taskfile for submission
2253 * @qc: Metadata associated with taskfile to be prepared
2255 * Prepare ATA taskfile for submission.
2258 * spin_lock_irqsave(host_set lock)
2260 void ata_qc_prep(struct ata_queued_cmd
*qc
)
2262 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
2269 * ata_sg_init_one - Associate command with memory buffer
2270 * @qc: Command to be associated
2271 * @buf: Memory buffer
2272 * @buflen: Length of memory buffer, in bytes.
2274 * Initialize the data-related elements of queued_cmd @qc
2275 * to point to a single memory buffer, @buf of byte length @buflen.
2278 * spin_lock_irqsave(host_set lock)
2281 void ata_sg_init_one(struct ata_queued_cmd
*qc
, void *buf
, unsigned int buflen
)
2283 struct scatterlist
*sg
;
2285 qc
->flags
|= ATA_QCFLAG_SINGLE
;
2287 memset(&qc
->sgent
, 0, sizeof(qc
->sgent
));
2288 qc
->sg
= &qc
->sgent
;
2293 sg
->page
= virt_to_page(buf
);
2294 sg
->offset
= (unsigned long) buf
& ~PAGE_MASK
;
2295 sg
->length
= buflen
;
2299 * ata_sg_init - Associate command with scatter-gather table.
2300 * @qc: Command to be associated
2301 * @sg: Scatter-gather table.
2302 * @n_elem: Number of elements in s/g table.
2304 * Initialize the data-related elements of queued_cmd @qc
2305 * to point to a scatter-gather table @sg, containing @n_elem
2309 * spin_lock_irqsave(host_set lock)
2312 void ata_sg_init(struct ata_queued_cmd
*qc
, struct scatterlist
*sg
,
2313 unsigned int n_elem
)
2315 qc
->flags
|= ATA_QCFLAG_SG
;
2317 qc
->n_elem
= n_elem
;
2321 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2322 * @qc: Command with memory buffer to be mapped.
2324 * DMA-map the memory buffer associated with queued_cmd @qc.
2327 * spin_lock_irqsave(host_set lock)
2330 * Zero on success, negative on error.
2333 static int ata_sg_setup_one(struct ata_queued_cmd
*qc
)
2335 struct ata_port
*ap
= qc
->ap
;
2336 int dir
= qc
->dma_dir
;
2337 struct scatterlist
*sg
= qc
->sg
;
2338 dma_addr_t dma_address
;
2340 dma_address
= dma_map_single(ap
->host_set
->dev
, qc
->buf_virt
,
2342 if (dma_mapping_error(dma_address
))
2345 sg_dma_address(sg
) = dma_address
;
2346 sg_dma_len(sg
) = sg
->length
;
2348 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg
),
2349 qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2355 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2356 * @qc: Command with scatter-gather table to be mapped.
2358 * DMA-map the scatter-gather table associated with queued_cmd @qc.
2361 * spin_lock_irqsave(host_set lock)
2364 * Zero on success, negative on error.
2368 static int ata_sg_setup(struct ata_queued_cmd
*qc
)
2370 struct ata_port
*ap
= qc
->ap
;
2371 struct scatterlist
*sg
= qc
->sg
;
2374 VPRINTK("ENTER, ata%u\n", ap
->id
);
2375 assert(qc
->flags
& ATA_QCFLAG_SG
);
2378 n_elem
= dma_map_sg(ap
->host_set
->dev
, sg
, qc
->n_elem
, dir
);
2382 DPRINTK("%d sg elements mapped\n", n_elem
);
2384 qc
->n_elem
= n_elem
;
2394 * None. (executing in kernel thread context)
2400 static unsigned long ata_pio_poll(struct ata_port
*ap
)
2403 unsigned int poll_state
= PIO_ST_UNKNOWN
;
2404 unsigned int reg_state
= PIO_ST_UNKNOWN
;
2405 const unsigned int tmout_state
= PIO_ST_TMOUT
;
2407 switch (ap
->pio_task_state
) {
2410 poll_state
= PIO_ST_POLL
;
2414 case PIO_ST_LAST_POLL
:
2415 poll_state
= PIO_ST_LAST_POLL
;
2416 reg_state
= PIO_ST_LAST
;
2423 status
= ata_chk_status(ap
);
2424 if (status
& ATA_BUSY
) {
2425 if (time_after(jiffies
, ap
->pio_task_timeout
)) {
2426 ap
->pio_task_state
= tmout_state
;
2429 ap
->pio_task_state
= poll_state
;
2430 return ATA_SHORT_PAUSE
;
2433 ap
->pio_task_state
= reg_state
;
2438 * ata_pio_complete -
2442 * None. (executing in kernel thread context)
2445 static void ata_pio_complete (struct ata_port
*ap
)
2447 struct ata_queued_cmd
*qc
;
2451 * This is purely hueristic. This is a fast path.
2452 * Sometimes when we enter, BSY will be cleared in
2453 * a chk-status or two. If not, the drive is probably seeking
2454 * or something. Snooze for a couple msecs, then
2455 * chk-status again. If still busy, fall back to
2456 * PIO_ST_POLL state.
2458 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
| ATA_DRQ
, 10);
2459 if (drv_stat
& (ATA_BUSY
| ATA_DRQ
)) {
2461 drv_stat
= ata_busy_wait(ap
, ATA_BUSY
| ATA_DRQ
, 10);
2462 if (drv_stat
& (ATA_BUSY
| ATA_DRQ
)) {
2463 ap
->pio_task_state
= PIO_ST_LAST_POLL
;
2464 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
2469 drv_stat
= ata_wait_idle(ap
);
2470 if (!ata_ok(drv_stat
)) {
2471 ap
->pio_task_state
= PIO_ST_ERR
;
2475 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2478 ap
->pio_task_state
= PIO_ST_IDLE
;
2482 ata_qc_complete(qc
, drv_stat
);
2488 * @buf: Buffer to swap
2489 * @buf_words: Number of 16-bit words in buffer.
2491 * Swap halves of 16-bit words if needed to convert from
2492 * little-endian byte order to native cpu byte order, or
2497 void swap_buf_le16(u16
*buf
, unsigned int buf_words
)
2502 for (i
= 0; i
< buf_words
; i
++)
2503 buf
[i
] = le16_to_cpu(buf
[i
]);
2504 #endif /* __BIG_ENDIAN */
2507 static void ata_mmio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2508 unsigned int buflen
, int write_data
)
2511 unsigned int words
= buflen
>> 1;
2512 u16
*buf16
= (u16
*) buf
;
2513 void __iomem
*mmio
= (void __iomem
*)ap
->ioaddr
.data_addr
;
2516 for (i
= 0; i
< words
; i
++)
2517 writew(le16_to_cpu(buf16
[i
]), mmio
);
2519 for (i
= 0; i
< words
; i
++)
2520 buf16
[i
] = cpu_to_le16(readw(mmio
));
2524 static void ata_pio_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2525 unsigned int buflen
, int write_data
)
2527 unsigned int dwords
= buflen
>> 1;
2530 outsw(ap
->ioaddr
.data_addr
, buf
, dwords
);
2532 insw(ap
->ioaddr
.data_addr
, buf
, dwords
);
2535 static void ata_data_xfer(struct ata_port
*ap
, unsigned char *buf
,
2536 unsigned int buflen
, int do_write
)
2538 if (ap
->flags
& ATA_FLAG_MMIO
)
2539 ata_mmio_data_xfer(ap
, buf
, buflen
, do_write
);
2541 ata_pio_data_xfer(ap
, buf
, buflen
, do_write
);
2544 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
2546 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2547 struct scatterlist
*sg
= qc
->sg
;
2548 struct ata_port
*ap
= qc
->ap
;
2550 unsigned int offset
;
2553 if (qc
->cursect
== (qc
->nsect
- 1))
2554 ap
->pio_task_state
= PIO_ST_LAST
;
2556 page
= sg
[qc
->cursg
].page
;
2557 offset
= sg
[qc
->cursg
].offset
+ qc
->cursg_ofs
* ATA_SECT_SIZE
;
2559 /* get the current page and offset */
2560 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
2561 offset
%= PAGE_SIZE
;
2563 buf
= kmap(page
) + offset
;
2568 if ((qc
->cursg_ofs
* ATA_SECT_SIZE
) == (&sg
[qc
->cursg
])->length
) {
2573 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2575 /* do the actual data transfer */
2576 do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2577 ata_data_xfer(ap
, buf
, ATA_SECT_SIZE
, do_write
);
2582 static void __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
2584 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2585 struct scatterlist
*sg
= qc
->sg
;
2586 struct ata_port
*ap
= qc
->ap
;
2589 unsigned int offset
, count
;
2591 if (qc
->curbytes
== qc
->nbytes
- bytes
)
2592 ap
->pio_task_state
= PIO_ST_LAST
;
2595 sg
= &qc
->sg
[qc
->cursg
];
2598 offset
= sg
->offset
+ qc
->cursg_ofs
;
2600 /* get the current page and offset */
2601 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
2602 offset
%= PAGE_SIZE
;
2604 /* don't overrun current sg */
2605 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
2607 /* don't cross page boundaries */
2608 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
2610 buf
= kmap(page
) + offset
;
2613 qc
->curbytes
+= count
;
2614 qc
->cursg_ofs
+= count
;
2616 if (qc
->cursg_ofs
== sg
->length
) {
2621 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
2623 /* do the actual data transfer */
2624 ata_data_xfer(ap
, buf
, count
, do_write
);
2633 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
2635 struct ata_port
*ap
= qc
->ap
;
2636 struct ata_device
*dev
= qc
->dev
;
2637 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
2638 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
2640 ap
->ops
->tf_read(ap
, &qc
->tf
);
2641 ireason
= qc
->tf
.nsect
;
2642 bc_lo
= qc
->tf
.lbam
;
2643 bc_hi
= qc
->tf
.lbah
;
2644 bytes
= (bc_hi
<< 8) | bc_lo
;
2646 /* shall be cleared to zero, indicating xfer of data */
2647 if (ireason
& (1 << 0))
2650 /* make sure transfer direction matches expected */
2651 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
2652 if (do_write
!= i_write
)
2655 __atapi_pio_bytes(qc
, bytes
);
2660 printk(KERN_INFO
"ata%u: dev %u: ATAPI check failed\n",
2661 ap
->id
, dev
->devno
);
2662 ap
->pio_task_state
= PIO_ST_ERR
;
2670 * None. (executing in kernel thread context)
2673 static void ata_pio_block(struct ata_port
*ap
)
2675 struct ata_queued_cmd
*qc
;
2679 * This is purely hueristic. This is a fast path.
2680 * Sometimes when we enter, BSY will be cleared in
2681 * a chk-status or two. If not, the drive is probably seeking
2682 * or something. Snooze for a couple msecs, then
2683 * chk-status again. If still busy, fall back to
2684 * PIO_ST_POLL state.
2686 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
2687 if (status
& ATA_BUSY
) {
2689 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
2690 if (status
& ATA_BUSY
) {
2691 ap
->pio_task_state
= PIO_ST_POLL
;
2692 ap
->pio_task_timeout
= jiffies
+ ATA_TMOUT_PIO
;
2697 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2700 if (is_atapi_taskfile(&qc
->tf
)) {
2701 /* no more data to transfer or unsupported ATAPI command */
2702 if ((status
& ATA_DRQ
) == 0) {
2703 ap
->pio_task_state
= PIO_ST_IDLE
;
2707 ata_qc_complete(qc
, status
);
2711 atapi_pio_bytes(qc
);
2713 /* handle BSY=0, DRQ=0 as error */
2714 if ((status
& ATA_DRQ
) == 0) {
2715 ap
->pio_task_state
= PIO_ST_ERR
;
2723 static void ata_pio_error(struct ata_port
*ap
)
2725 struct ata_queued_cmd
*qc
;
2728 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2731 drv_stat
= ata_chk_status(ap
);
2732 printk(KERN_WARNING
"ata%u: PIO error, drv_stat 0x%x\n",
2735 ap
->pio_task_state
= PIO_ST_IDLE
;
2739 ata_qc_complete(qc
, drv_stat
| ATA_ERR
);
2742 static void ata_pio_task(void *_data
)
2744 struct ata_port
*ap
= _data
;
2745 unsigned long timeout
= 0;
2747 switch (ap
->pio_task_state
) {
2756 ata_pio_complete(ap
);
2760 case PIO_ST_LAST_POLL
:
2761 timeout
= ata_pio_poll(ap
);
2771 queue_delayed_work(ata_wq
, &ap
->pio_task
,
2774 queue_work(ata_wq
, &ap
->pio_task
);
2777 static void atapi_request_sense(struct ata_port
*ap
, struct ata_device
*dev
,
2778 struct scsi_cmnd
*cmd
)
2780 DECLARE_COMPLETION(wait
);
2781 struct ata_queued_cmd
*qc
;
2782 unsigned long flags
;
2785 DPRINTK("ATAPI request sense\n");
2787 qc
= ata_qc_new_init(ap
, dev
);
2790 /* FIXME: is this needed? */
2791 memset(cmd
->sense_buffer
, 0, sizeof(cmd
->sense_buffer
));
2793 ata_sg_init_one(qc
, cmd
->sense_buffer
, sizeof(cmd
->sense_buffer
));
2794 qc
->dma_dir
= DMA_FROM_DEVICE
;
2796 memset(&qc
->cdb
, 0, ap
->cdb_len
);
2797 qc
->cdb
[0] = REQUEST_SENSE
;
2798 qc
->cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
2800 qc
->tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2801 qc
->tf
.command
= ATA_CMD_PACKET
;
2803 qc
->tf
.protocol
= ATA_PROT_ATAPI
;
2804 qc
->tf
.lbam
= (8 * 1024) & 0xff;
2805 qc
->tf
.lbah
= (8 * 1024) >> 8;
2806 qc
->nbytes
= SCSI_SENSE_BUFFERSIZE
;
2808 qc
->waiting
= &wait
;
2809 qc
->complete_fn
= ata_qc_complete_noop
;
2811 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
2812 rc
= ata_qc_issue(qc
);
2813 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
2816 ata_port_disable(ap
);
2818 wait_for_completion(&wait
);
2824 * ata_qc_timeout - Handle timeout of queued command
2825 * @qc: Command that timed out
2827 * Some part of the kernel (currently, only the SCSI layer)
2828 * has noticed that the active command on port @ap has not
2829 * completed after a specified length of time. Handle this
2830 * condition by disabling DMA (if necessary) and completing
2831 * transactions, with error if necessary.
2833 * This also handles the case of the "lost interrupt", where
2834 * for some reason (possibly hardware bug, possibly driver bug)
2835 * an interrupt was not delivered to the driver, even though the
2836 * transaction completed successfully.
2839 * Inherited from SCSI layer (none, can sleep)
2842 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
2844 struct ata_port
*ap
= qc
->ap
;
2845 struct ata_device
*dev
= qc
->dev
;
2846 u8 host_stat
= 0, drv_stat
;
2850 /* FIXME: doesn't this conflict with timeout handling? */
2851 if (qc
->dev
->class == ATA_DEV_ATAPI
&& qc
->scsicmd
) {
2852 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
2854 if (!(cmd
->eh_eflags
& SCSI_EH_CANCEL_CMD
)) {
2856 /* finish completing original command */
2857 __ata_qc_complete(qc
);
2859 atapi_request_sense(ap
, dev
, cmd
);
2861 cmd
->result
= (CHECK_CONDITION
<< 1) | (DID_OK
<< 16);
2862 scsi_finish_command(cmd
);
2868 /* hack alert! We cannot use the supplied completion
2869 * function from inside the ->eh_strategy_handler() thread.
2870 * libata is the only user of ->eh_strategy_handler() in
2871 * any kernel, so the default scsi_done() assumes it is
2872 * not being called from the SCSI EH.
2874 qc
->scsidone
= scsi_finish_command
;
2876 switch (qc
->tf
.protocol
) {
2879 case ATA_PROT_ATAPI_DMA
:
2880 host_stat
= ap
->ops
->bmdma_status(ap
);
2882 /* before we do anything else, clear DMA-Start bit */
2883 ap
->ops
->bmdma_stop(ap
);
2889 drv_stat
= ata_chk_status(ap
);
2891 /* ack bmdma irq events */
2892 ap
->ops
->irq_clear(ap
);
2894 printk(KERN_ERR
"ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
2895 ap
->id
, qc
->tf
.command
, drv_stat
, host_stat
);
2897 /* complete taskfile transaction */
2898 ata_qc_complete(qc
, drv_stat
);
2906 * ata_eng_timeout - Handle timeout of queued command
2907 * @ap: Port on which timed-out command is active
2909 * Some part of the kernel (currently, only the SCSI layer)
2910 * has noticed that the active command on port @ap has not
2911 * completed after a specified length of time. Handle this
2912 * condition by disabling DMA (if necessary) and completing
2913 * transactions, with error if necessary.
2915 * This also handles the case of the "lost interrupt", where
2916 * for some reason (possibly hardware bug, possibly driver bug)
2917 * an interrupt was not delivered to the driver, even though the
2918 * transaction completed successfully.
2921 * Inherited from SCSI layer (none, can sleep)
2924 void ata_eng_timeout(struct ata_port
*ap
)
2926 struct ata_queued_cmd
*qc
;
2930 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
2932 printk(KERN_ERR
"ata%u: BUG: timeout without command\n",
2944 * ata_qc_new - Request an available ATA command, for queueing
2945 * @ap: Port associated with device @dev
2946 * @dev: Device from whom we request an available command structure
2952 static struct ata_queued_cmd
*ata_qc_new(struct ata_port
*ap
)
2954 struct ata_queued_cmd
*qc
= NULL
;
2957 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++)
2958 if (!test_and_set_bit(i
, &ap
->qactive
)) {
2959 qc
= ata_qc_from_tag(ap
, i
);
2970 * ata_qc_new_init - Request an available ATA command, and initialize it
2971 * @ap: Port associated with device @dev
2972 * @dev: Device from whom we request an available command structure
2978 struct ata_queued_cmd
*ata_qc_new_init(struct ata_port
*ap
,
2979 struct ata_device
*dev
)
2981 struct ata_queued_cmd
*qc
;
2983 qc
= ata_qc_new(ap
);
2990 qc
->cursect
= qc
->cursg
= qc
->cursg_ofs
= 0;
2992 qc
->nbytes
= qc
->curbytes
= 0;
2994 ata_tf_init(ap
, &qc
->tf
, dev
->devno
);
2996 if (dev
->flags
& ATA_DFLAG_LBA48
)
2997 qc
->tf
.flags
|= ATA_TFLAG_LBA48
;
3003 static int ata_qc_complete_noop(struct ata_queued_cmd
*qc
, u8 drv_stat
)
3008 static void __ata_qc_complete(struct ata_queued_cmd
*qc
)
3010 struct ata_port
*ap
= qc
->ap
;
3011 unsigned int tag
, do_clear
= 0;
3015 if (likely(ata_tag_valid(tag
))) {
3016 if (tag
== ap
->active_tag
)
3017 ap
->active_tag
= ATA_TAG_POISON
;
3018 qc
->tag
= ATA_TAG_POISON
;
3023 struct completion
*waiting
= qc
->waiting
;
3028 if (likely(do_clear
))
3029 clear_bit(tag
, &ap
->qactive
);
3033 * ata_qc_free - free unused ata_queued_cmd
3034 * @qc: Command to complete
3036 * Designed to free unused ata_queued_cmd object
3037 * in case something prevents using it.
3040 * spin_lock_irqsave(host_set lock)
3043 void ata_qc_free(struct ata_queued_cmd
*qc
)
3045 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3046 assert(qc
->waiting
== NULL
); /* nothing should be waiting */
3048 __ata_qc_complete(qc
);
3052 * ata_qc_complete - Complete an active ATA command
3053 * @qc: Command to complete
3054 * @drv_stat: ATA Status register contents
3056 * Indicate to the mid and upper layers that an ATA
3057 * command has completed, with either an ok or not-ok status.
3060 * spin_lock_irqsave(host_set lock)
3064 void ata_qc_complete(struct ata_queued_cmd
*qc
, u8 drv_stat
)
3068 assert(qc
!= NULL
); /* ata_qc_from_tag _might_ return NULL */
3069 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
3071 if (likely(qc
->flags
& ATA_QCFLAG_DMAMAP
))
3074 /* call completion callback */
3075 rc
= qc
->complete_fn(qc
, drv_stat
);
3076 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
3078 /* if callback indicates not to complete command (non-zero),
3079 * return immediately
3084 __ata_qc_complete(qc
);
3089 static inline int ata_should_dma_map(struct ata_queued_cmd
*qc
)
3091 struct ata_port
*ap
= qc
->ap
;
3093 switch (qc
->tf
.protocol
) {
3095 case ATA_PROT_ATAPI_DMA
:
3098 case ATA_PROT_ATAPI
:
3100 case ATA_PROT_PIO_MULT
:
3101 if (ap
->flags
& ATA_FLAG_PIO_DMA
)
3114 * ata_qc_issue - issue taskfile to device
3115 * @qc: command to issue to device
3117 * Prepare an ATA command to submission to device.
3118 * This includes mapping the data into a DMA-able
3119 * area, filling in the S/G table, and finally
3120 * writing the taskfile to hardware, starting the command.
3123 * spin_lock_irqsave(host_set lock)
3126 * Zero on success, negative on error.
3129 int ata_qc_issue(struct ata_queued_cmd
*qc
)
3131 struct ata_port
*ap
= qc
->ap
;
3133 if (ata_should_dma_map(qc
)) {
3134 if (qc
->flags
& ATA_QCFLAG_SG
) {
3135 if (ata_sg_setup(qc
))
3137 } else if (qc
->flags
& ATA_QCFLAG_SINGLE
) {
3138 if (ata_sg_setup_one(qc
))
3142 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
3145 ap
->ops
->qc_prep(qc
);
3147 qc
->ap
->active_tag
= qc
->tag
;
3148 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
3150 return ap
->ops
->qc_issue(qc
);
3158 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3159 * @qc: command to issue to device
3161 * Using various libata functions and hooks, this function
3162 * starts an ATA command. ATA commands are grouped into
3163 * classes called "protocols", and issuing each type of protocol
3164 * is slightly different.
3166 * May be used as the qc_issue() entry in ata_port_operations.
3169 * spin_lock_irqsave(host_set lock)
3172 * Zero on success, negative on error.
3175 int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
3177 struct ata_port
*ap
= qc
->ap
;
3179 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
3181 switch (qc
->tf
.protocol
) {
3182 case ATA_PROT_NODATA
:
3183 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3187 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3188 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3189 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3192 case ATA_PROT_PIO
: /* load tf registers, initiate polling pio */
3193 ata_qc_set_polling(qc
);
3194 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3195 ap
->pio_task_state
= PIO_ST
;
3196 queue_work(ata_wq
, &ap
->pio_task
);
3199 case ATA_PROT_ATAPI
:
3200 ata_qc_set_polling(qc
);
3201 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3202 queue_work(ata_wq
, &ap
->packet_task
);
3205 case ATA_PROT_ATAPI_NODATA
:
3206 ata_tf_to_host_nolock(ap
, &qc
->tf
);
3207 queue_work(ata_wq
, &ap
->packet_task
);
3210 case ATA_PROT_ATAPI_DMA
:
3211 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
3212 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
3213 queue_work(ata_wq
, &ap
->packet_task
);
3225 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
3226 * @qc: Info associated with this ATA transaction.
3229 * spin_lock_irqsave(host_set lock)
3232 static void ata_bmdma_setup_mmio (struct ata_queued_cmd
*qc
)
3234 struct ata_port
*ap
= qc
->ap
;
3235 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3237 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3239 /* load PRD table addr. */
3240 mb(); /* make sure PRD table writes are visible to controller */
3241 writel(ap
->prd_dma
, mmio
+ ATA_DMA_TABLE_OFS
);
3243 /* specify data direction, triple-check start bit is clear */
3244 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3245 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3247 dmactl
|= ATA_DMA_WR
;
3248 writeb(dmactl
, mmio
+ ATA_DMA_CMD
);
3250 /* issue r/w command */
3251 ap
->ops
->exec_command(ap
, &qc
->tf
);
3255 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3256 * @qc: Info associated with this ATA transaction.
3259 * spin_lock_irqsave(host_set lock)
3262 static void ata_bmdma_start_mmio (struct ata_queued_cmd
*qc
)
3264 struct ata_port
*ap
= qc
->ap
;
3265 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3268 /* start host DMA transaction */
3269 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
3270 writeb(dmactl
| ATA_DMA_START
, mmio
+ ATA_DMA_CMD
);
3272 /* Strictly, one may wish to issue a readb() here, to
3273 * flush the mmio write. However, control also passes
3274 * to the hardware at this point, and it will interrupt
3275 * us when we are to resume control. So, in effect,
3276 * we don't care when the mmio write flushes.
3277 * Further, a read of the DMA status register _immediately_
3278 * following the write may not be what certain flaky hardware
3279 * is expected, so I think it is best to not add a readb()
3280 * without first all the MMIO ATA cards/mobos.
3281 * Or maybe I'm just being paranoid.
3286 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3287 * @qc: Info associated with this ATA transaction.
3290 * spin_lock_irqsave(host_set lock)
3293 static void ata_bmdma_setup_pio (struct ata_queued_cmd
*qc
)
3295 struct ata_port
*ap
= qc
->ap
;
3296 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
3299 /* load PRD table addr. */
3300 outl(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
3302 /* specify data direction, triple-check start bit is clear */
3303 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3304 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
3306 dmactl
|= ATA_DMA_WR
;
3307 outb(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3309 /* issue r/w command */
3310 ap
->ops
->exec_command(ap
, &qc
->tf
);
3314 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3315 * @qc: Info associated with this ATA transaction.
3318 * spin_lock_irqsave(host_set lock)
3321 static void ata_bmdma_start_pio (struct ata_queued_cmd
*qc
)
3323 struct ata_port
*ap
= qc
->ap
;
3326 /* start host DMA transaction */
3327 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3328 outb(dmactl
| ATA_DMA_START
,
3329 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3334 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3335 * @qc: Info associated with this ATA transaction.
3337 * Writes the ATA_DMA_START flag to the DMA command register.
3339 * May be used as the bmdma_start() entry in ata_port_operations.
3342 * spin_lock_irqsave(host_set lock)
3344 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
3346 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3347 ata_bmdma_start_mmio(qc
);
3349 ata_bmdma_start_pio(qc
);
3354 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3355 * @qc: Info associated with this ATA transaction.
3357 * Writes address of PRD table to device's PRD Table Address
3358 * register, sets the DMA control register, and calls
3359 * ops->exec_command() to start the transfer.
3361 * May be used as the bmdma_setup() entry in ata_port_operations.
3364 * spin_lock_irqsave(host_set lock)
3366 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
3368 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
3369 ata_bmdma_setup_mmio(qc
);
3371 ata_bmdma_setup_pio(qc
);
3376 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
3377 * @ap: Port associated with this ATA transaction.
3379 * Clear interrupt and error flags in DMA status register.
3381 * May be used as the irq_clear() entry in ata_port_operations.
3384 * spin_lock_irqsave(host_set lock)
3387 void ata_bmdma_irq_clear(struct ata_port
*ap
)
3389 if (ap
->flags
& ATA_FLAG_MMIO
) {
3390 void __iomem
*mmio
= ((void __iomem
*) ap
->ioaddr
.bmdma_addr
) + ATA_DMA_STATUS
;
3391 writeb(readb(mmio
), mmio
);
3393 unsigned long addr
= ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
;
3394 outb(inb(addr
), addr
);
3401 * ata_bmdma_status - Read PCI IDE BMDMA status
3402 * @ap: Port associated with this ATA transaction.
3404 * Read and return BMDMA status register.
3406 * May be used as the bmdma_status() entry in ata_port_operations.
3409 * spin_lock_irqsave(host_set lock)
3412 u8
ata_bmdma_status(struct ata_port
*ap
)
3415 if (ap
->flags
& ATA_FLAG_MMIO
) {
3416 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3417 host_stat
= readb(mmio
+ ATA_DMA_STATUS
);
3419 host_stat
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
3425 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3426 * @ap: Port associated with this ATA transaction.
3428 * Clears the ATA_DMA_START flag in the dma control register
3430 * May be used as the bmdma_stop() entry in ata_port_operations.
3433 * spin_lock_irqsave(host_set lock)
3436 void ata_bmdma_stop(struct ata_port
*ap
)
3438 if (ap
->flags
& ATA_FLAG_MMIO
) {
3439 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
3441 /* clear start/stop bit */
3442 writeb(readb(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
3443 mmio
+ ATA_DMA_CMD
);
3445 /* clear start/stop bit */
3446 outb(inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
3447 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
3450 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3451 ata_altstatus(ap
); /* dummy read */
3455 * ata_host_intr - Handle host interrupt for given (port, task)
3456 * @ap: Port on which interrupt arrived (possibly...)
3457 * @qc: Taskfile currently active in engine
3459 * Handle host interrupt for given queued command. Currently,
3460 * only DMA interrupts are handled. All other commands are
3461 * handled via polling with interrupts disabled (nIEN bit).
3464 * spin_lock_irqsave(host_set lock)
3467 * One if interrupt was handled, zero if not (shared irq).
3470 inline unsigned int ata_host_intr (struct ata_port
*ap
,
3471 struct ata_queued_cmd
*qc
)
3473 u8 status
, host_stat
;
3475 switch (qc
->tf
.protocol
) {
3478 case ATA_PROT_ATAPI_DMA
:
3479 case ATA_PROT_ATAPI
:
3480 /* check status of DMA engine */
3481 host_stat
= ap
->ops
->bmdma_status(ap
);
3482 VPRINTK("ata%u: host_stat 0x%X\n", ap
->id
, host_stat
);
3484 /* if it's not our irq... */
3485 if (!(host_stat
& ATA_DMA_INTR
))
3488 /* before we do anything else, clear DMA-Start bit */
3489 ap
->ops
->bmdma_stop(ap
);
3493 case ATA_PROT_ATAPI_NODATA
:
3494 case ATA_PROT_NODATA
:
3495 /* check altstatus */
3496 status
= ata_altstatus(ap
);
3497 if (status
& ATA_BUSY
)
3500 /* check main status, clearing INTRQ */
3501 status
= ata_chk_status(ap
);
3502 if (unlikely(status
& ATA_BUSY
))
3504 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3505 ap
->id
, qc
->tf
.protocol
, status
);
3507 /* ack bmdma irq events */
3508 ap
->ops
->irq_clear(ap
);
3510 /* complete taskfile transaction */
3511 ata_qc_complete(qc
, status
);
3518 return 1; /* irq handled */
3521 ap
->stats
.idle_irq
++;
3524 if ((ap
->stats
.idle_irq
% 1000) == 0) {
3526 ata_irq_ack(ap
, 0); /* debug trap */
3527 printk(KERN_WARNING
"ata%d: irq trap\n", ap
->id
);
3530 return 0; /* irq not handled */
3534 * ata_interrupt - Default ATA host interrupt handler
3535 * @irq: irq line (unused)
3536 * @dev_instance: pointer to our ata_host_set information structure
3539 * Default interrupt handler for PCI IDE devices. Calls
3540 * ata_host_intr() for each port that is not disabled.
3543 * Obtains host_set lock during operation.
3546 * IRQ_NONE or IRQ_HANDLED.
3550 irqreturn_t
ata_interrupt (int irq
, void *dev_instance
, struct pt_regs
*regs
)
3552 struct ata_host_set
*host_set
= dev_instance
;
3554 unsigned int handled
= 0;
3555 unsigned long flags
;
3557 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3558 spin_lock_irqsave(&host_set
->lock
, flags
);
3560 for (i
= 0; i
< host_set
->n_ports
; i
++) {
3561 struct ata_port
*ap
;
3563 ap
= host_set
->ports
[i
];
3564 if (ap
&& (!(ap
->flags
& ATA_FLAG_PORT_DISABLED
))) {
3565 struct ata_queued_cmd
*qc
;
3567 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3568 if (qc
&& (!(qc
->tf
.ctl
& ATA_NIEN
)) &&
3569 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
3570 handled
|= ata_host_intr(ap
, qc
);
3574 spin_unlock_irqrestore(&host_set
->lock
, flags
);
3576 return IRQ_RETVAL(handled
);
3580 * atapi_packet_task - Write CDB bytes to hardware
3581 * @_data: Port to which ATAPI device is attached.
3583 * When device has indicated its readiness to accept
3584 * a CDB, this function is called. Send the CDB.
3585 * If DMA is to be performed, exit immediately.
3586 * Otherwise, we are in polling mode, so poll
3587 * status under operation succeeds or fails.
3590 * Kernel thread context (may sleep)
3593 static void atapi_packet_task(void *_data
)
3595 struct ata_port
*ap
= _data
;
3596 struct ata_queued_cmd
*qc
;
3599 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
3601 assert(qc
->flags
& ATA_QCFLAG_ACTIVE
);
3603 /* sleep-wait for BSY to clear */
3604 DPRINTK("busy wait\n");
3605 if (ata_busy_sleep(ap
, ATA_TMOUT_CDB_QUICK
, ATA_TMOUT_CDB
))
3608 /* make sure DRQ is set */
3609 status
= ata_chk_status(ap
);
3610 if ((status
& (ATA_BUSY
| ATA_DRQ
)) != ATA_DRQ
)
3614 DPRINTK("send cdb\n");
3615 assert(ap
->cdb_len
>= 12);
3616 ata_data_xfer(ap
, qc
->cdb
, ap
->cdb_len
, 1);
3618 /* if we are DMA'ing, irq handler takes over from here */
3619 if (qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
)
3620 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
3622 /* non-data commands are also handled via irq */
3623 else if (qc
->tf
.protocol
== ATA_PROT_ATAPI_NODATA
) {
3627 /* PIO commands are handled by polling */
3629 ap
->pio_task_state
= PIO_ST
;
3630 queue_work(ata_wq
, &ap
->pio_task
);
3636 ata_qc_complete(qc
, ATA_ERR
);
3641 * ata_port_start - Set port up for dma.
3642 * @ap: Port to initialize
3644 * Called just after data structures for each port are
3645 * initialized. Allocates space for PRD table.
3647 * May be used as the port_start() entry in ata_port_operations.
3652 int ata_port_start (struct ata_port
*ap
)
3654 struct device
*dev
= ap
->host_set
->dev
;
3656 ap
->prd
= dma_alloc_coherent(dev
, ATA_PRD_TBL_SZ
, &ap
->prd_dma
, GFP_KERNEL
);
3660 DPRINTK("prd alloc, virt %p, dma %llx\n", ap
->prd
, (unsigned long long) ap
->prd_dma
);
3667 * ata_port_stop - Undo ata_port_start()
3668 * @ap: Port to shut down
3670 * Frees the PRD table.
3672 * May be used as the port_stop() entry in ata_port_operations.
3677 void ata_port_stop (struct ata_port
*ap
)
3679 struct device
*dev
= ap
->host_set
->dev
;
3681 dma_free_coherent(dev
, ATA_PRD_TBL_SZ
, ap
->prd
, ap
->prd_dma
);
3684 void ata_host_stop (struct ata_host_set
*host_set
)
3686 if (host_set
->mmio_base
)
3687 iounmap(host_set
->mmio_base
);
3692 * ata_host_remove - Unregister SCSI host structure with upper layers
3693 * @ap: Port to unregister
3694 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
3699 static void ata_host_remove(struct ata_port
*ap
, unsigned int do_unregister
)
3701 struct Scsi_Host
*sh
= ap
->host
;
3706 scsi_remove_host(sh
);
3708 ap
->ops
->port_stop(ap
);
3712 * ata_host_init - Initialize an ata_port structure
3713 * @ap: Structure to initialize
3714 * @host: associated SCSI mid-layer structure
3715 * @host_set: Collection of hosts to which @ap belongs
3716 * @ent: Probe information provided by low-level driver
3717 * @port_no: Port number associated with this ata_port
3719 * Initialize a new ata_port structure, and its associated
3723 * Inherited from caller.
3727 static void ata_host_init(struct ata_port
*ap
, struct Scsi_Host
*host
,
3728 struct ata_host_set
*host_set
,
3729 struct ata_probe_ent
*ent
, unsigned int port_no
)
3735 host
->max_channel
= 1;
3736 host
->unique_id
= ata_unique_id
++;
3737 host
->max_cmd_len
= 12;
3739 scsi_assign_lock(host
, &host_set
->lock
);
3741 ap
->flags
= ATA_FLAG_PORT_DISABLED
;
3742 ap
->id
= host
->unique_id
;
3744 ap
->ctl
= ATA_DEVCTL_OBS
;
3745 ap
->host_set
= host_set
;
3746 ap
->port_no
= port_no
;
3748 ent
->legacy_mode
? ent
->hard_port_no
: port_no
;
3749 ap
->pio_mask
= ent
->pio_mask
;
3750 ap
->mwdma_mask
= ent
->mwdma_mask
;
3751 ap
->udma_mask
= ent
->udma_mask
;
3752 ap
->flags
|= ent
->host_flags
;
3753 ap
->ops
= ent
->port_ops
;
3754 ap
->cbl
= ATA_CBL_NONE
;
3755 ap
->active_tag
= ATA_TAG_POISON
;
3756 ap
->last_ctl
= 0xFF;
3758 INIT_WORK(&ap
->packet_task
, atapi_packet_task
, ap
);
3759 INIT_WORK(&ap
->pio_task
, ata_pio_task
, ap
);
3761 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
3762 ap
->device
[i
].devno
= i
;
3765 ap
->stats
.unhandled_irq
= 1;
3766 ap
->stats
.idle_irq
= 1;
3769 memcpy(&ap
->ioaddr
, &ent
->port
[port_no
], sizeof(struct ata_ioports
));
3773 * ata_host_add - Attach low-level ATA driver to system
3774 * @ent: Information provided by low-level driver
3775 * @host_set: Collections of ports to which we add
3776 * @port_no: Port number associated with this host
3778 * Attach low-level ATA driver to system.
3781 * PCI/etc. bus probe sem.
3784 * New ata_port on success, for NULL on error.
3788 static struct ata_port
* ata_host_add(struct ata_probe_ent
*ent
,
3789 struct ata_host_set
*host_set
,
3790 unsigned int port_no
)
3792 struct Scsi_Host
*host
;
3793 struct ata_port
*ap
;
3797 host
= scsi_host_alloc(ent
->sht
, sizeof(struct ata_port
));
3801 ap
= (struct ata_port
*) &host
->hostdata
[0];
3803 ata_host_init(ap
, host
, host_set
, ent
, port_no
);
3805 rc
= ap
->ops
->port_start(ap
);
3812 scsi_host_put(host
);
3817 * ata_device_add - Register hardware device with ATA and SCSI layers
3818 * @ent: Probe information describing hardware device to be registered
3820 * This function processes the information provided in the probe
3821 * information struct @ent, allocates the necessary ATA and SCSI
3822 * host information structures, initializes them, and registers
3823 * everything with requisite kernel subsystems.
3825 * This function requests irqs, probes the ATA bus, and probes
3829 * PCI/etc. bus probe sem.
3832 * Number of ports registered. Zero on error (no ports registered).
3836 int ata_device_add(struct ata_probe_ent
*ent
)
3838 unsigned int count
= 0, i
;
3839 struct device
*dev
= ent
->dev
;
3840 struct ata_host_set
*host_set
;
3843 /* alloc a container for our list of ATA ports (buses) */
3844 host_set
= kmalloc(sizeof(struct ata_host_set
) +
3845 (ent
->n_ports
* sizeof(void *)), GFP_KERNEL
);
3848 memset(host_set
, 0, sizeof(struct ata_host_set
) + (ent
->n_ports
* sizeof(void *)));
3849 spin_lock_init(&host_set
->lock
);
3851 host_set
->dev
= dev
;
3852 host_set
->n_ports
= ent
->n_ports
;
3853 host_set
->irq
= ent
->irq
;
3854 host_set
->mmio_base
= ent
->mmio_base
;
3855 host_set
->private_data
= ent
->private_data
;
3856 host_set
->ops
= ent
->port_ops
;
3858 /* register each port bound to this device */
3859 for (i
= 0; i
< ent
->n_ports
; i
++) {
3860 struct ata_port
*ap
;
3861 unsigned long xfer_mode_mask
;
3863 ap
= ata_host_add(ent
, host_set
, i
);
3867 host_set
->ports
[i
] = ap
;
3868 xfer_mode_mask
=(ap
->udma_mask
<< ATA_SHIFT_UDMA
) |
3869 (ap
->mwdma_mask
<< ATA_SHIFT_MWDMA
) |
3870 (ap
->pio_mask
<< ATA_SHIFT_PIO
);
3872 /* print per-port info to dmesg */
3873 printk(KERN_INFO
"ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
3874 "bmdma 0x%lX irq %lu\n",
3876 ap
->flags
& ATA_FLAG_SATA
? 'S' : 'P',
3877 ata_mode_string(xfer_mode_mask
),
3878 ap
->ioaddr
.cmd_addr
,
3879 ap
->ioaddr
.ctl_addr
,
3880 ap
->ioaddr
.bmdma_addr
,
3884 host_set
->ops
->irq_clear(ap
);
3893 /* obtain irq, that is shared between channels */
3894 if (request_irq(ent
->irq
, ent
->port_ops
->irq_handler
, ent
->irq_flags
,
3895 DRV_NAME
, host_set
))
3898 /* perform each probe synchronously */
3899 DPRINTK("probe begin\n");
3900 for (i
= 0; i
< count
; i
++) {
3901 struct ata_port
*ap
;
3904 ap
= host_set
->ports
[i
];
3906 DPRINTK("ata%u: probe begin\n", ap
->id
);
3907 rc
= ata_bus_probe(ap
);
3908 DPRINTK("ata%u: probe end\n", ap
->id
);
3911 /* FIXME: do something useful here?
3912 * Current libata behavior will
3913 * tear down everything when
3914 * the module is removed
3915 * or the h/w is unplugged.
3919 rc
= scsi_add_host(ap
->host
, dev
);
3921 printk(KERN_ERR
"ata%u: scsi_add_host failed\n",
3923 /* FIXME: do something useful here */
3924 /* FIXME: handle unconditional calls to
3925 * scsi_scan_host and ata_host_remove, below,
3931 /* probes are done, now scan each port's disk(s) */
3932 DPRINTK("probe begin\n");
3933 for (i
= 0; i
< count
; i
++) {
3934 struct ata_port
*ap
= host_set
->ports
[i
];
3936 scsi_scan_host(ap
->host
);
3939 dev_set_drvdata(dev
, host_set
);
3941 VPRINTK("EXIT, returning %u\n", ent
->n_ports
);
3942 return ent
->n_ports
; /* success */
3945 for (i
= 0; i
< count
; i
++) {
3946 ata_host_remove(host_set
->ports
[i
], 1);
3947 scsi_host_put(host_set
->ports
[i
]->host
);
3950 VPRINTK("EXIT, returning 0\n");
3955 * ata_scsi_release - SCSI layer callback hook for host unload
3956 * @host: libata host to be unloaded
3958 * Performs all duties necessary to shut down a libata port...
3959 * Kill port kthread, disable port, and release resources.
3962 * Inherited from SCSI layer.
3968 int ata_scsi_release(struct Scsi_Host
*host
)
3970 struct ata_port
*ap
= (struct ata_port
*) &host
->hostdata
[0];
3974 ap
->ops
->port_disable(ap
);
3975 ata_host_remove(ap
, 0);
3982 * ata_std_ports - initialize ioaddr with standard port offsets.
3983 * @ioaddr: IO address structure to be initialized
3985 * Utility function which initializes data_addr, error_addr,
3986 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
3987 * device_addr, status_addr, and command_addr to standard offsets
3988 * relative to cmd_addr.
3990 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
3993 void ata_std_ports(struct ata_ioports
*ioaddr
)
3995 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
3996 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
3997 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
3998 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
3999 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
4000 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
4001 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
4002 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
4003 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
4004 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
4007 static struct ata_probe_ent
*
4008 ata_probe_ent_alloc(struct device
*dev
, struct ata_port_info
*port
)
4010 struct ata_probe_ent
*probe_ent
;
4012 probe_ent
= kmalloc(sizeof(*probe_ent
), GFP_KERNEL
);
4014 printk(KERN_ERR DRV_NAME
"(%s): out of memory\n",
4015 kobject_name(&(dev
->kobj
)));
4019 memset(probe_ent
, 0, sizeof(*probe_ent
));
4021 INIT_LIST_HEAD(&probe_ent
->node
);
4022 probe_ent
->dev
= dev
;
4024 probe_ent
->sht
= port
->sht
;
4025 probe_ent
->host_flags
= port
->host_flags
;
4026 probe_ent
->pio_mask
= port
->pio_mask
;
4027 probe_ent
->mwdma_mask
= port
->mwdma_mask
;
4028 probe_ent
->udma_mask
= port
->udma_mask
;
4029 probe_ent
->port_ops
= port
->port_ops
;
4037 * ata_pci_init_native_mode - Initialize native-mode driver
4038 * @pdev: pci device to be initialized
4039 * @port: array[2] of pointers to port info structures.
4041 * Utility function which allocates and initializes an
4042 * ata_probe_ent structure for a standard dual-port
4043 * PIO-based IDE controller. The returned ata_probe_ent
4044 * structure can be passed to ata_device_add(). The returned
4045 * ata_probe_ent structure should then be freed with kfree().
4049 struct ata_probe_ent
*
4050 ata_pci_init_native_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
)
4052 struct ata_probe_ent
*probe_ent
=
4053 ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
4057 probe_ent
->n_ports
= 2;
4058 probe_ent
->irq
= pdev
->irq
;
4059 probe_ent
->irq_flags
= SA_SHIRQ
;
4061 probe_ent
->port
[0].cmd_addr
= pci_resource_start(pdev
, 0);
4062 probe_ent
->port
[0].altstatus_addr
=
4063 probe_ent
->port
[0].ctl_addr
=
4064 pci_resource_start(pdev
, 1) | ATA_PCI_CTL_OFS
;
4065 probe_ent
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4);
4067 probe_ent
->port
[1].cmd_addr
= pci_resource_start(pdev
, 2);
4068 probe_ent
->port
[1].altstatus_addr
=
4069 probe_ent
->port
[1].ctl_addr
=
4070 pci_resource_start(pdev
, 3) | ATA_PCI_CTL_OFS
;
4071 probe_ent
->port
[1].bmdma_addr
= pci_resource_start(pdev
, 4) + 8;
4073 ata_std_ports(&probe_ent
->port
[0]);
4074 ata_std_ports(&probe_ent
->port
[1]);
4079 static struct ata_probe_ent
*
4080 ata_pci_init_legacy_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
,
4081 struct ata_probe_ent
**ppe2
)
4083 struct ata_probe_ent
*probe_ent
, *probe_ent2
;
4085 probe_ent
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
4088 probe_ent2
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[1]);
4094 probe_ent
->n_ports
= 1;
4095 probe_ent
->irq
= 14;
4097 probe_ent
->hard_port_no
= 0;
4098 probe_ent
->legacy_mode
= 1;
4100 probe_ent2
->n_ports
= 1;
4101 probe_ent2
->irq
= 15;
4103 probe_ent2
->hard_port_no
= 1;
4104 probe_ent2
->legacy_mode
= 1;
4106 probe_ent
->port
[0].cmd_addr
= 0x1f0;
4107 probe_ent
->port
[0].altstatus_addr
=
4108 probe_ent
->port
[0].ctl_addr
= 0x3f6;
4109 probe_ent
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4);
4111 probe_ent2
->port
[0].cmd_addr
= 0x170;
4112 probe_ent2
->port
[0].altstatus_addr
=
4113 probe_ent2
->port
[0].ctl_addr
= 0x376;
4114 probe_ent2
->port
[0].bmdma_addr
= pci_resource_start(pdev
, 4)+8;
4116 ata_std_ports(&probe_ent
->port
[0]);
4117 ata_std_ports(&probe_ent2
->port
[0]);
4124 * ata_pci_init_one - Initialize/register PCI IDE host controller
4125 * @pdev: Controller to be initialized
4126 * @port_info: Information from low-level host driver
4127 * @n_ports: Number of ports attached to host controller
4129 * This is a helper function which can be called from a driver's
4130 * xxx_init_one() probe function if the hardware uses traditional
4131 * IDE taskfile registers.
4133 * This function calls pci_enable_device(), reserves its register
4134 * regions, sets the dma mask, enables bus master mode, and calls
4138 * Inherited from PCI layer (may sleep).
4141 * Zero on success, negative on errno-based value on error.
4145 int ata_pci_init_one (struct pci_dev
*pdev
, struct ata_port_info
**port_info
,
4146 unsigned int n_ports
)
4148 struct ata_probe_ent
*probe_ent
, *probe_ent2
= NULL
;
4149 struct ata_port_info
*port
[2];
4151 unsigned int legacy_mode
= 0;
4152 int disable_dev_on_err
= 1;
4157 port
[0] = port_info
[0];
4159 port
[1] = port_info
[1];
4163 if ((port
[0]->host_flags
& ATA_FLAG_NO_LEGACY
) == 0
4164 && (pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
4165 /* TODO: support transitioning to native mode? */
4166 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
4167 mask
= (1 << 2) | (1 << 0);
4168 if ((tmp8
& mask
) != mask
)
4169 legacy_mode
= (1 << 3);
4173 if ((!legacy_mode
) && (n_ports
> 1)) {
4174 printk(KERN_ERR
"ata: BUG: native mode, n_ports > 1\n");
4178 rc
= pci_enable_device(pdev
);
4182 rc
= pci_request_regions(pdev
, DRV_NAME
);
4184 disable_dev_on_err
= 0;
4189 if (!request_region(0x1f0, 8, "libata")) {
4190 struct resource
*conflict
, res
;
4192 res
.end
= 0x1f0 + 8 - 1;
4193 conflict
= ____request_resource(&ioport_resource
, &res
);
4194 if (!strcmp(conflict
->name
, "libata"))
4195 legacy_mode
|= (1 << 0);
4197 disable_dev_on_err
= 0;
4198 printk(KERN_WARNING
"ata: 0x1f0 IDE port busy\n");
4201 legacy_mode
|= (1 << 0);
4203 if (!request_region(0x170, 8, "libata")) {
4204 struct resource
*conflict
, res
;
4206 res
.end
= 0x170 + 8 - 1;
4207 conflict
= ____request_resource(&ioport_resource
, &res
);
4208 if (!strcmp(conflict
->name
, "libata"))
4209 legacy_mode
|= (1 << 1);
4211 disable_dev_on_err
= 0;
4212 printk(KERN_WARNING
"ata: 0x170 IDE port busy\n");
4215 legacy_mode
|= (1 << 1);
4218 /* we have legacy mode, but all ports are unavailable */
4219 if (legacy_mode
== (1 << 3)) {
4221 goto err_out_regions
;
4224 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
4226 goto err_out_regions
;
4227 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
4229 goto err_out_regions
;
4232 probe_ent
= ata_pci_init_legacy_mode(pdev
, port
, &probe_ent2
);
4234 probe_ent
= ata_pci_init_native_mode(pdev
, port
);
4237 goto err_out_regions
;
4240 pci_set_master(pdev
);
4242 /* FIXME: check ata_device_add return */
4244 if (legacy_mode
& (1 << 0))
4245 ata_device_add(probe_ent
);
4246 if (legacy_mode
& (1 << 1))
4247 ata_device_add(probe_ent2
);
4249 ata_device_add(probe_ent
);
4257 if (legacy_mode
& (1 << 0))
4258 release_region(0x1f0, 8);
4259 if (legacy_mode
& (1 << 1))
4260 release_region(0x170, 8);
4261 pci_release_regions(pdev
);
4263 if (disable_dev_on_err
)
4264 pci_disable_device(pdev
);
4269 * ata_pci_remove_one - PCI layer callback for device removal
4270 * @pdev: PCI device that was removed
4272 * PCI layer indicates to libata via this hook that
4273 * hot-unplug or module unload event has occured.
4274 * Handle this by unregistering all objects associated
4275 * with this PCI device. Free those objects. Then finally
4276 * release PCI resources and disable device.
4279 * Inherited from PCI layer (may sleep).
4282 void ata_pci_remove_one (struct pci_dev
*pdev
)
4284 struct device
*dev
= pci_dev_to_dev(pdev
);
4285 struct ata_host_set
*host_set
= dev_get_drvdata(dev
);
4286 struct ata_port
*ap
;
4289 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4290 ap
= host_set
->ports
[i
];
4292 scsi_remove_host(ap
->host
);
4295 free_irq(host_set
->irq
, host_set
);
4297 for (i
= 0; i
< host_set
->n_ports
; i
++) {
4298 ap
= host_set
->ports
[i
];
4300 ata_scsi_release(ap
->host
);
4302 if ((ap
->flags
& ATA_FLAG_NO_LEGACY
) == 0) {
4303 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
4305 if (ioaddr
->cmd_addr
== 0x1f0)
4306 release_region(0x1f0, 8);
4307 else if (ioaddr
->cmd_addr
== 0x170)
4308 release_region(0x170, 8);
4311 scsi_host_put(ap
->host
);
4314 if (host_set
->ops
->host_stop
)
4315 host_set
->ops
->host_stop(host_set
);
4319 pci_release_regions(pdev
);
4320 pci_disable_device(pdev
);
4321 dev_set_drvdata(dev
, NULL
);
4324 /* move to PCI subsystem */
4325 int pci_test_config_bits(struct pci_dev
*pdev
, struct pci_bits
*bits
)
4327 unsigned long tmp
= 0;
4329 switch (bits
->width
) {
4332 pci_read_config_byte(pdev
, bits
->reg
, &tmp8
);
4338 pci_read_config_word(pdev
, bits
->reg
, &tmp16
);
4344 pci_read_config_dword(pdev
, bits
->reg
, &tmp32
);
4355 return (tmp
== bits
->val
) ? 1 : 0;
4357 #endif /* CONFIG_PCI */
4360 static int __init
ata_init(void)
4362 ata_wq
= create_workqueue("ata");
4366 printk(KERN_DEBUG
"libata version " DRV_VERSION
" loaded.\n");
4370 static void __exit
ata_exit(void)
4372 destroy_workqueue(ata_wq
);
4375 module_init(ata_init
);
4376 module_exit(ata_exit
);
4379 * libata is essentially a library of internal helper functions for
4380 * low-level ATA host controller drivers. As such, the API/ABI is
4381 * likely to change as new drivers are added and updated.
4382 * Do not depend on ABI/API stability.
4385 EXPORT_SYMBOL_GPL(ata_std_bios_param
);
4386 EXPORT_SYMBOL_GPL(ata_std_ports
);
4387 EXPORT_SYMBOL_GPL(ata_device_add
);
4388 EXPORT_SYMBOL_GPL(ata_sg_init
);
4389 EXPORT_SYMBOL_GPL(ata_sg_init_one
);
4390 EXPORT_SYMBOL_GPL(ata_qc_complete
);
4391 EXPORT_SYMBOL_GPL(ata_qc_issue_prot
);
4392 EXPORT_SYMBOL_GPL(ata_eng_timeout
);
4393 EXPORT_SYMBOL_GPL(ata_tf_load
);
4394 EXPORT_SYMBOL_GPL(ata_tf_read
);
4395 EXPORT_SYMBOL_GPL(ata_noop_dev_select
);
4396 EXPORT_SYMBOL_GPL(ata_std_dev_select
);
4397 EXPORT_SYMBOL_GPL(ata_tf_to_fis
);
4398 EXPORT_SYMBOL_GPL(ata_tf_from_fis
);
4399 EXPORT_SYMBOL_GPL(ata_check_status
);
4400 EXPORT_SYMBOL_GPL(ata_altstatus
);
4401 EXPORT_SYMBOL_GPL(ata_chk_err
);
4402 EXPORT_SYMBOL_GPL(ata_exec_command
);
4403 EXPORT_SYMBOL_GPL(ata_port_start
);
4404 EXPORT_SYMBOL_GPL(ata_port_stop
);
4405 EXPORT_SYMBOL_GPL(ata_host_stop
);
4406 EXPORT_SYMBOL_GPL(ata_interrupt
);
4407 EXPORT_SYMBOL_GPL(ata_qc_prep
);
4408 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
4409 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
4410 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear
);
4411 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
4412 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
4413 EXPORT_SYMBOL_GPL(ata_port_probe
);
4414 EXPORT_SYMBOL_GPL(sata_phy_reset
);
4415 EXPORT_SYMBOL_GPL(__sata_phy_reset
);
4416 EXPORT_SYMBOL_GPL(ata_bus_reset
);
4417 EXPORT_SYMBOL_GPL(ata_port_disable
);
4418 EXPORT_SYMBOL_GPL(ata_scsi_ioctl
);
4419 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd
);
4420 EXPORT_SYMBOL_GPL(ata_scsi_error
);
4421 EXPORT_SYMBOL_GPL(ata_scsi_slave_config
);
4422 EXPORT_SYMBOL_GPL(ata_scsi_release
);
4423 EXPORT_SYMBOL_GPL(ata_host_intr
);
4424 EXPORT_SYMBOL_GPL(ata_dev_classify
);
4425 EXPORT_SYMBOL_GPL(ata_dev_id_string
);
4426 EXPORT_SYMBOL_GPL(ata_dev_config
);
4427 EXPORT_SYMBOL_GPL(ata_scsi_simulate
);
4430 EXPORT_SYMBOL_GPL(pci_test_config_bits
);
4431 EXPORT_SYMBOL_GPL(ata_pci_init_native_mode
);
4432 EXPORT_SYMBOL_GPL(ata_pci_init_one
);
4433 EXPORT_SYMBOL_GPL(ata_pci_remove_one
);
4434 #endif /* CONFIG_PCI */
This page took 0.138568 seconds and 5 git commands to generate.