2 * libata-bmdma.c - helper library for PCI IDE BMDMA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2006 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/kernel.h>
36 #include <linux/pci.h>
37 #include <linux/libata.h>
42 * ata_tf_load_pio - send taskfile registers to host controller
43 * @ap: Port to which output is sent
44 * @tf: ATA taskfile register set
46 * Outputs ATA taskfile to standard ATA host controller.
49 * Inherited from caller.
52 static void ata_tf_load_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
54 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
55 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
57 if (tf
->ctl
!= ap
->last_ctl
) {
58 outb(tf
->ctl
, ioaddr
->ctl_addr
);
59 ap
->last_ctl
= tf
->ctl
;
63 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
64 outb(tf
->hob_feature
, ioaddr
->feature_addr
);
65 outb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
66 outb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
67 outb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
68 outb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
69 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
78 outb(tf
->feature
, ioaddr
->feature_addr
);
79 outb(tf
->nsect
, ioaddr
->nsect_addr
);
80 outb(tf
->lbal
, ioaddr
->lbal_addr
);
81 outb(tf
->lbam
, ioaddr
->lbam_addr
);
82 outb(tf
->lbah
, ioaddr
->lbah_addr
);
83 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
91 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
92 outb(tf
->device
, ioaddr
->device_addr
);
93 VPRINTK("device 0x%X\n", tf
->device
);
100 * ata_tf_load_mmio - send taskfile registers to host controller
101 * @ap: Port to which output is sent
102 * @tf: ATA taskfile register set
104 * Outputs ATA taskfile to standard ATA host controller using MMIO.
107 * Inherited from caller.
110 static void ata_tf_load_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
112 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
113 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
115 if (tf
->ctl
!= ap
->last_ctl
) {
116 writeb(tf
->ctl
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
117 ap
->last_ctl
= tf
->ctl
;
121 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
122 writeb(tf
->hob_feature
, (void __iomem
*) ioaddr
->feature_addr
);
123 writeb(tf
->hob_nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
124 writeb(tf
->hob_lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
125 writeb(tf
->hob_lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
126 writeb(tf
->hob_lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
127 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
136 writeb(tf
->feature
, (void __iomem
*) ioaddr
->feature_addr
);
137 writeb(tf
->nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
138 writeb(tf
->lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
139 writeb(tf
->lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
140 writeb(tf
->lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
141 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
149 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
150 writeb(tf
->device
, (void __iomem
*) ioaddr
->device_addr
);
151 VPRINTK("device 0x%X\n", tf
->device
);
159 * ata_tf_load - send taskfile registers to host controller
160 * @ap: Port to which output is sent
161 * @tf: ATA taskfile register set
163 * Outputs ATA taskfile to standard ATA host controller using MMIO
164 * or PIO as indicated by the ATA_FLAG_MMIO flag.
165 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
166 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
167 * hob_lbal, hob_lbam, and hob_lbah.
169 * This function waits for idle (!BUSY and !DRQ) after writing
170 * registers. If the control register has a new value, this
171 * function also waits for idle after writing control and before
172 * writing the remaining registers.
174 * May be used as the tf_load() entry in ata_port_operations.
177 * Inherited from caller.
179 void ata_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
181 if (ap
->flags
& ATA_FLAG_MMIO
)
182 ata_tf_load_mmio(ap
, tf
);
184 ata_tf_load_pio(ap
, tf
);
188 * ata_exec_command_pio - issue ATA command to host controller
189 * @ap: port to which command is being issued
190 * @tf: ATA taskfile register set
192 * Issues PIO write to ATA command register, with proper
193 * synchronization with interrupt handler / other threads.
196 * spin_lock_irqsave(host_set lock)
199 static void ata_exec_command_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
201 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
203 outb(tf
->command
, ap
->ioaddr
.command_addr
);
209 * ata_exec_command_mmio - issue ATA command to host controller
210 * @ap: port to which command is being issued
211 * @tf: ATA taskfile register set
213 * Issues MMIO write to ATA command register, with proper
214 * synchronization with interrupt handler / other threads.
216 * FIXME: missing write posting for 400nS delay enforcement
219 * spin_lock_irqsave(host_set lock)
222 static void ata_exec_command_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
224 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
226 writeb(tf
->command
, (void __iomem
*) ap
->ioaddr
.command_addr
);
232 * ata_exec_command - issue ATA command to host controller
233 * @ap: port to which command is being issued
234 * @tf: ATA taskfile register set
236 * Issues PIO/MMIO write to ATA command register, with proper
237 * synchronization with interrupt handler / other threads.
240 * spin_lock_irqsave(host_set lock)
242 void ata_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
244 if (ap
->flags
& ATA_FLAG_MMIO
)
245 ata_exec_command_mmio(ap
, tf
);
247 ata_exec_command_pio(ap
, tf
);
251 * ata_tf_read_pio - input device's ATA taskfile shadow registers
252 * @ap: Port from which input is read
253 * @tf: ATA taskfile register set for storing input
255 * Reads ATA taskfile registers for currently-selected device
259 * Inherited from caller.
262 static void ata_tf_read_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
264 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
266 tf
->command
= ata_check_status(ap
);
267 tf
->feature
= inb(ioaddr
->error_addr
);
268 tf
->nsect
= inb(ioaddr
->nsect_addr
);
269 tf
->lbal
= inb(ioaddr
->lbal_addr
);
270 tf
->lbam
= inb(ioaddr
->lbam_addr
);
271 tf
->lbah
= inb(ioaddr
->lbah_addr
);
272 tf
->device
= inb(ioaddr
->device_addr
);
274 if (tf
->flags
& ATA_TFLAG_LBA48
) {
275 outb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
276 tf
->hob_feature
= inb(ioaddr
->error_addr
);
277 tf
->hob_nsect
= inb(ioaddr
->nsect_addr
);
278 tf
->hob_lbal
= inb(ioaddr
->lbal_addr
);
279 tf
->hob_lbam
= inb(ioaddr
->lbam_addr
);
280 tf
->hob_lbah
= inb(ioaddr
->lbah_addr
);
285 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
286 * @ap: Port from which input is read
287 * @tf: ATA taskfile register set for storing input
289 * Reads ATA taskfile registers for currently-selected device
293 * Inherited from caller.
296 static void ata_tf_read_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
298 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
300 tf
->command
= ata_check_status(ap
);
301 tf
->feature
= readb((void __iomem
*)ioaddr
->error_addr
);
302 tf
->nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
303 tf
->lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
304 tf
->lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
305 tf
->lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
306 tf
->device
= readb((void __iomem
*)ioaddr
->device_addr
);
308 if (tf
->flags
& ATA_TFLAG_LBA48
) {
309 writeb(tf
->ctl
| ATA_HOB
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
310 tf
->hob_feature
= readb((void __iomem
*)ioaddr
->error_addr
);
311 tf
->hob_nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
312 tf
->hob_lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
313 tf
->hob_lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
314 tf
->hob_lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
320 * ata_tf_read - input device's ATA taskfile shadow registers
321 * @ap: Port from which input is read
322 * @tf: ATA taskfile register set for storing input
324 * Reads ATA taskfile registers for currently-selected device
327 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
328 * is set, also reads the hob registers.
330 * May be used as the tf_read() entry in ata_port_operations.
333 * Inherited from caller.
335 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
337 if (ap
->flags
& ATA_FLAG_MMIO
)
338 ata_tf_read_mmio(ap
, tf
);
340 ata_tf_read_pio(ap
, tf
);
344 * ata_check_status_pio - Read device status reg & clear interrupt
345 * @ap: port where the device is
347 * Reads ATA taskfile status register for currently-selected device
348 * and return its value. This also clears pending interrupts
352 * Inherited from caller.
354 static u8
ata_check_status_pio(struct ata_port
*ap
)
356 return inb(ap
->ioaddr
.status_addr
);
360 * ata_check_status_mmio - Read device status reg & clear interrupt
361 * @ap: port where the device is
363 * Reads ATA taskfile status register for currently-selected device
364 * via MMIO and return its value. This also clears pending interrupts
368 * Inherited from caller.
370 static u8
ata_check_status_mmio(struct ata_port
*ap
)
372 return readb((void __iomem
*) ap
->ioaddr
.status_addr
);
377 * ata_check_status - Read device status reg & clear interrupt
378 * @ap: port where the device is
380 * Reads ATA taskfile status register for currently-selected device
381 * and return its value. This also clears pending interrupts
384 * May be used as the check_status() entry in ata_port_operations.
387 * Inherited from caller.
389 u8
ata_check_status(struct ata_port
*ap
)
391 if (ap
->flags
& ATA_FLAG_MMIO
)
392 return ata_check_status_mmio(ap
);
393 return ata_check_status_pio(ap
);
398 * ata_altstatus - Read device alternate status reg
399 * @ap: port where the device is
401 * Reads ATA taskfile alternate status register for
402 * currently-selected device and return its value.
404 * Note: may NOT be used as the check_altstatus() entry in
405 * ata_port_operations.
408 * Inherited from caller.
410 u8
ata_altstatus(struct ata_port
*ap
)
412 if (ap
->ops
->check_altstatus
)
413 return ap
->ops
->check_altstatus(ap
);
415 if (ap
->flags
& ATA_FLAG_MMIO
)
416 return readb((void __iomem
*)ap
->ioaddr
.altstatus_addr
);
417 return inb(ap
->ioaddr
.altstatus_addr
);
421 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
422 * @qc: Info associated with this ATA transaction.
425 * spin_lock_irqsave(host_set lock)
428 static void ata_bmdma_setup_mmio (struct ata_queued_cmd
*qc
)
430 struct ata_port
*ap
= qc
->ap
;
431 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
433 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
435 /* load PRD table addr. */
436 mb(); /* make sure PRD table writes are visible to controller */
437 writel(ap
->prd_dma
, mmio
+ ATA_DMA_TABLE_OFS
);
439 /* specify data direction, triple-check start bit is clear */
440 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
441 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
443 dmactl
|= ATA_DMA_WR
;
444 writeb(dmactl
, mmio
+ ATA_DMA_CMD
);
446 /* issue r/w command */
447 ap
->ops
->exec_command(ap
, &qc
->tf
);
451 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
452 * @qc: Info associated with this ATA transaction.
455 * spin_lock_irqsave(host_set lock)
458 static void ata_bmdma_start_mmio (struct ata_queued_cmd
*qc
)
460 struct ata_port
*ap
= qc
->ap
;
461 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
464 /* start host DMA transaction */
465 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
466 writeb(dmactl
| ATA_DMA_START
, mmio
+ ATA_DMA_CMD
);
468 /* Strictly, one may wish to issue a readb() here, to
469 * flush the mmio write. However, control also passes
470 * to the hardware at this point, and it will interrupt
471 * us when we are to resume control. So, in effect,
472 * we don't care when the mmio write flushes.
473 * Further, a read of the DMA status register _immediately_
474 * following the write may not be what certain flaky hardware
475 * is expected, so I think it is best to not add a readb()
476 * without first all the MMIO ATA cards/mobos.
477 * Or maybe I'm just being paranoid.
482 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
483 * @qc: Info associated with this ATA transaction.
486 * spin_lock_irqsave(host_set lock)
489 static void ata_bmdma_setup_pio (struct ata_queued_cmd
*qc
)
491 struct ata_port
*ap
= qc
->ap
;
492 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
495 /* load PRD table addr. */
496 outl(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
498 /* specify data direction, triple-check start bit is clear */
499 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
500 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
502 dmactl
|= ATA_DMA_WR
;
503 outb(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
505 /* issue r/w command */
506 ap
->ops
->exec_command(ap
, &qc
->tf
);
510 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
511 * @qc: Info associated with this ATA transaction.
514 * spin_lock_irqsave(host_set lock)
517 static void ata_bmdma_start_pio (struct ata_queued_cmd
*qc
)
519 struct ata_port
*ap
= qc
->ap
;
522 /* start host DMA transaction */
523 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
524 outb(dmactl
| ATA_DMA_START
,
525 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
530 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
531 * @qc: Info associated with this ATA transaction.
533 * Writes the ATA_DMA_START flag to the DMA command register.
535 * May be used as the bmdma_start() entry in ata_port_operations.
538 * spin_lock_irqsave(host_set lock)
540 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
542 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
543 ata_bmdma_start_mmio(qc
);
545 ata_bmdma_start_pio(qc
);
550 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
551 * @qc: Info associated with this ATA transaction.
553 * Writes address of PRD table to device's PRD Table Address
554 * register, sets the DMA control register, and calls
555 * ops->exec_command() to start the transfer.
557 * May be used as the bmdma_setup() entry in ata_port_operations.
560 * spin_lock_irqsave(host_set lock)
562 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
564 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
565 ata_bmdma_setup_mmio(qc
);
567 ata_bmdma_setup_pio(qc
);
572 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
573 * @ap: Port associated with this ATA transaction.
575 * Clear interrupt and error flags in DMA status register.
577 * May be used as the irq_clear() entry in ata_port_operations.
580 * spin_lock_irqsave(host_set lock)
583 void ata_bmdma_irq_clear(struct ata_port
*ap
)
585 if (!ap
->ioaddr
.bmdma_addr
)
588 if (ap
->flags
& ATA_FLAG_MMIO
) {
590 ((void __iomem
*) ap
->ioaddr
.bmdma_addr
) + ATA_DMA_STATUS
;
591 writeb(readb(mmio
), mmio
);
593 unsigned long addr
= ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
;
594 outb(inb(addr
), addr
);
600 * ata_bmdma_status - Read PCI IDE BMDMA status
601 * @ap: Port associated with this ATA transaction.
603 * Read and return BMDMA status register.
605 * May be used as the bmdma_status() entry in ata_port_operations.
608 * spin_lock_irqsave(host_set lock)
611 u8
ata_bmdma_status(struct ata_port
*ap
)
614 if (ap
->flags
& ATA_FLAG_MMIO
) {
615 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
616 host_stat
= readb(mmio
+ ATA_DMA_STATUS
);
618 host_stat
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
624 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
625 * @qc: Command we are ending DMA for
627 * Clears the ATA_DMA_START flag in the dma control register
629 * May be used as the bmdma_stop() entry in ata_port_operations.
632 * spin_lock_irqsave(host_set lock)
635 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
637 struct ata_port
*ap
= qc
->ap
;
638 if (ap
->flags
& ATA_FLAG_MMIO
) {
639 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
641 /* clear start/stop bit */
642 writeb(readb(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
645 /* clear start/stop bit */
646 outb(inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
647 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
650 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
651 ata_altstatus(ap
); /* dummy read */
655 * ata_bmdma_freeze - Freeze BMDMA controller port
656 * @ap: port to freeze
658 * Freeze BMDMA controller port.
661 * Inherited from caller.
663 void ata_bmdma_freeze(struct ata_port
*ap
)
665 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
668 ap
->last_ctl
= ap
->ctl
;
670 if (ap
->flags
& ATA_FLAG_MMIO
)
671 writeb(ap
->ctl
, (void __iomem
*)ioaddr
->ctl_addr
);
673 outb(ap
->ctl
, ioaddr
->ctl_addr
);
677 * ata_bmdma_thaw - Thaw BMDMA controller port
680 * Thaw BMDMA controller port.
683 * Inherited from caller.
685 void ata_bmdma_thaw(struct ata_port
*ap
)
687 /* clear & re-enable interrupts */
689 ap
->ops
->irq_clear(ap
);
690 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
695 * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
696 * @ap: port to handle error for
697 * @prereset: prereset method (can be NULL)
698 * @softreset: softreset method (can be NULL)
699 * @hardreset: hardreset method (can be NULL)
700 * @postreset: postreset method (can be NULL)
702 * Handle error for ATA BMDMA controller. It can handle both
703 * PATA and SATA controllers. Many controllers should be able to
704 * use this EH as-is or with some added handling before and
707 * This function is intended to be used for constructing
708 * ->error_handler callback by low level drivers.
711 * Kernel thread context (may sleep)
713 void ata_bmdma_drive_eh(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
714 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
715 ata_postreset_fn_t postreset
)
717 struct ata_eh_context
*ehc
= &ap
->eh_context
;
718 struct ata_queued_cmd
*qc
;
722 qc
= __ata_qc_from_tag(ap
, ap
->active_tag
);
723 if (qc
&& !(qc
->flags
& ATA_QCFLAG_FAILED
))
726 /* reset PIO HSM and stop DMA engine */
727 spin_lock_irqsave(ap
->lock
, flags
);
729 ap
->hsm_task_state
= HSM_ST_IDLE
;
731 if (qc
&& (qc
->tf
.protocol
== ATA_PROT_DMA
||
732 qc
->tf
.protocol
== ATA_PROT_ATAPI_DMA
)) {
735 host_stat
= ata_bmdma_status(ap
);
737 ata_ehi_push_desc(&ehc
->i
, "BMDMA stat 0x%x", host_stat
);
739 /* BMDMA controllers indicate host bus error by
740 * setting DMA_ERR bit and timing out. As it wasn't
741 * really a timeout event, adjust error mask and
742 * cancel frozen state.
744 if (qc
->err_mask
== AC_ERR_TIMEOUT
&& host_stat
& ATA_DMA_ERR
) {
745 qc
->err_mask
= AC_ERR_HOST_BUS
;
749 ap
->ops
->bmdma_stop(qc
);
754 ap
->ops
->irq_clear(ap
);
756 spin_unlock_irqrestore(ap
->lock
, flags
);
759 ata_eh_thaw_port(ap
);
761 /* PIO and DMA engines have been stopped, perform recovery */
762 ata_do_eh(ap
, prereset
, softreset
, hardreset
, postreset
);
766 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
767 * @ap: port to handle error for
769 * Stock error handler for BMDMA controller.
772 * Kernel thread context (may sleep)
774 void ata_bmdma_error_handler(struct ata_port
*ap
)
776 ata_reset_fn_t hardreset
;
779 if (sata_scr_valid(ap
))
780 hardreset
= sata_std_hardreset
;
782 ata_bmdma_drive_eh(ap
, ata_std_prereset
, ata_std_softreset
, hardreset
,
787 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
789 * @qc: internal command to clean up
792 * Kernel thread context (may sleep)
794 void ata_bmdma_post_internal_cmd(struct ata_queued_cmd
*qc
)
800 static struct ata_probe_ent
*
801 ata_probe_ent_alloc(struct device
*dev
, const struct ata_port_info
*port
)
803 struct ata_probe_ent
*probe_ent
;
805 probe_ent
= kzalloc(sizeof(*probe_ent
), GFP_KERNEL
);
807 printk(KERN_ERR DRV_NAME
"(%s): out of memory\n",
808 kobject_name(&(dev
->kobj
)));
812 INIT_LIST_HEAD(&probe_ent
->node
);
813 probe_ent
->dev
= dev
;
815 probe_ent
->sht
= port
->sht
;
816 probe_ent
->host_flags
= port
->host_flags
;
817 probe_ent
->pio_mask
= port
->pio_mask
;
818 probe_ent
->mwdma_mask
= port
->mwdma_mask
;
819 probe_ent
->udma_mask
= port
->udma_mask
;
820 probe_ent
->port_ops
= port
->port_ops
;
827 * ata_pci_init_native_mode - Initialize native-mode driver
828 * @pdev: pci device to be initialized
829 * @port: array[2] of pointers to port info structures.
830 * @ports: bitmap of ports present
832 * Utility function which allocates and initializes an
833 * ata_probe_ent structure for a standard dual-port
834 * PIO-based IDE controller. The returned ata_probe_ent
835 * structure can be passed to ata_device_add(). The returned
836 * ata_probe_ent structure should then be freed with kfree().
838 * The caller need only pass the address of the primary port, the
839 * secondary will be deduced automatically. If the device has non
840 * standard secondary port mappings this function can be called twice,
841 * once for each interface.
844 struct ata_probe_ent
*
845 ata_pci_init_native_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
, int ports
)
847 struct ata_probe_ent
*probe_ent
=
848 ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
855 probe_ent
->irq
= pdev
->irq
;
856 probe_ent
->irq_flags
= IRQF_SHARED
;
857 probe_ent
->private_data
= port
[0]->private_data
;
859 if (ports
& ATA_PORT_PRIMARY
) {
860 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 0);
861 probe_ent
->port
[p
].altstatus_addr
=
862 probe_ent
->port
[p
].ctl_addr
=
863 pci_resource_start(pdev
, 1) | ATA_PCI_CTL_OFS
;
864 bmdma
= pci_resource_start(pdev
, 4);
866 if (inb(bmdma
+ 2) & 0x80)
867 probe_ent
->host_set_flags
|= ATA_HOST_SIMPLEX
;
868 probe_ent
->port
[p
].bmdma_addr
= bmdma
;
870 ata_std_ports(&probe_ent
->port
[p
]);
874 if (ports
& ATA_PORT_SECONDARY
) {
875 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 2);
876 probe_ent
->port
[p
].altstatus_addr
=
877 probe_ent
->port
[p
].ctl_addr
=
878 pci_resource_start(pdev
, 3) | ATA_PCI_CTL_OFS
;
879 bmdma
= pci_resource_start(pdev
, 4);
882 if(inb(bmdma
+ 2) & 0x80)
883 probe_ent
->host_set_flags
|= ATA_HOST_SIMPLEX
;
884 probe_ent
->port
[p
].bmdma_addr
= bmdma
;
886 ata_std_ports(&probe_ent
->port
[p
]);
890 probe_ent
->n_ports
= p
;
895 static struct ata_probe_ent
*ata_pci_init_legacy_port(struct pci_dev
*pdev
,
896 struct ata_port_info
*port
, int port_num
)
898 struct ata_probe_ent
*probe_ent
;
901 probe_ent
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
);
905 probe_ent
->legacy_mode
= 1;
906 probe_ent
->n_ports
= 1;
907 probe_ent
->hard_port_no
= port_num
;
908 probe_ent
->private_data
= port
->private_data
;
914 probe_ent
->port
[0].cmd_addr
= 0x1f0;
915 probe_ent
->port
[0].altstatus_addr
=
916 probe_ent
->port
[0].ctl_addr
= 0x3f6;
920 probe_ent
->port
[0].cmd_addr
= 0x170;
921 probe_ent
->port
[0].altstatus_addr
=
922 probe_ent
->port
[0].ctl_addr
= 0x376;
926 bmdma
= pci_resource_start(pdev
, 4);
928 bmdma
+= 8 * port_num
;
929 probe_ent
->port
[0].bmdma_addr
= bmdma
;
930 if (inb(bmdma
+ 2) & 0x80)
931 probe_ent
->host_set_flags
|= ATA_HOST_SIMPLEX
;
933 ata_std_ports(&probe_ent
->port
[0]);
940 * ata_pci_init_one - Initialize/register PCI IDE host controller
941 * @pdev: Controller to be initialized
942 * @port_info: Information from low-level host driver
943 * @n_ports: Number of ports attached to host controller
945 * This is a helper function which can be called from a driver's
946 * xxx_init_one() probe function if the hardware uses traditional
947 * IDE taskfile registers.
949 * This function calls pci_enable_device(), reserves its register
950 * regions, sets the dma mask, enables bus master mode, and calls
954 * Inherited from PCI layer (may sleep).
957 * Zero on success, negative on errno-based value on error.
960 int ata_pci_init_one (struct pci_dev
*pdev
, struct ata_port_info
**port_info
,
961 unsigned int n_ports
)
963 struct ata_probe_ent
*probe_ent
= NULL
, *probe_ent2
= NULL
;
964 struct ata_port_info
*port
[2];
966 unsigned int legacy_mode
= 0;
967 int disable_dev_on_err
= 1;
972 port
[0] = port_info
[0];
974 port
[1] = port_info
[1];
978 if ((port
[0]->host_flags
& ATA_FLAG_NO_LEGACY
) == 0
979 && (pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
980 /* TODO: What if one channel is in native mode ... */
981 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
982 mask
= (1 << 2) | (1 << 0);
983 if ((tmp8
& mask
) != mask
)
984 legacy_mode
= (1 << 3);
988 if ((!legacy_mode
) && (n_ports
> 2)) {
989 printk(KERN_ERR
"ata: BUG: native mode, n_ports > 2\n");
994 /* FIXME: Really for ATA it isn't safe because the device may be
995 multi-purpose and we want to leave it alone if it was already
996 enabled. Secondly for shared use as Arjan says we want refcounting
998 Checking dev->is_enabled is insufficient as this is not set at
999 boot for the primary video which is BIOS enabled
1002 rc
= pci_enable_device(pdev
);
1006 rc
= pci_request_regions(pdev
, DRV_NAME
);
1008 disable_dev_on_err
= 0;
1012 /* FIXME: Should use platform specific mappers for legacy port ranges */
1014 if (!request_region(0x1f0, 8, "libata")) {
1015 struct resource
*conflict
, res
;
1017 res
.end
= 0x1f0 + 8 - 1;
1018 conflict
= ____request_resource(&ioport_resource
, &res
);
1019 if (!strcmp(conflict
->name
, "libata"))
1020 legacy_mode
|= (1 << 0);
1022 disable_dev_on_err
= 0;
1023 printk(KERN_WARNING
"ata: 0x1f0 IDE port busy\n");
1026 legacy_mode
|= (1 << 0);
1028 if (!request_region(0x170, 8, "libata")) {
1029 struct resource
*conflict
, res
;
1031 res
.end
= 0x170 + 8 - 1;
1032 conflict
= ____request_resource(&ioport_resource
, &res
);
1033 if (!strcmp(conflict
->name
, "libata"))
1034 legacy_mode
|= (1 << 1);
1036 disable_dev_on_err
= 0;
1037 printk(KERN_WARNING
"ata: 0x170 IDE port busy\n");
1040 legacy_mode
|= (1 << 1);
1043 /* we have legacy mode, but all ports are unavailable */
1044 if (legacy_mode
== (1 << 3)) {
1046 goto err_out_regions
;
1049 /* FIXME: If we get no DMA mask we should fall back to PIO */
1050 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
1052 goto err_out_regions
;
1053 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
1055 goto err_out_regions
;
1058 if (legacy_mode
& (1 << 0))
1059 probe_ent
= ata_pci_init_legacy_port(pdev
, port
[0], 0);
1060 if (legacy_mode
& (1 << 1))
1061 probe_ent2
= ata_pci_init_legacy_port(pdev
, port
[1], 1);
1064 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
| ATA_PORT_SECONDARY
);
1066 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
);
1068 if (!probe_ent
&& !probe_ent2
) {
1070 goto err_out_regions
;
1073 pci_set_master(pdev
);
1075 /* FIXME: check ata_device_add return */
1077 struct device
*dev
= &pdev
->dev
;
1078 struct ata_host_set
*host_set
= NULL
;
1080 if (legacy_mode
& (1 << 0)) {
1081 ata_device_add(probe_ent
);
1082 host_set
= dev_get_drvdata(dev
);
1085 if (legacy_mode
& (1 << 1)) {
1086 ata_device_add(probe_ent2
);
1088 host_set
->next
= dev_get_drvdata(dev
);
1089 dev_set_drvdata(dev
, host_set
);
1093 ata_device_add(probe_ent
);
1101 if (legacy_mode
& (1 << 0))
1102 release_region(0x1f0, 8);
1103 if (legacy_mode
& (1 << 1))
1104 release_region(0x170, 8);
1105 pci_release_regions(pdev
);
1107 if (disable_dev_on_err
)
1108 pci_disable_device(pdev
);
1113 * ata_pci_clear_simplex - attempt to kick device out of simplex
1116 * Some PCI ATA devices report simplex mode but in fact can be told to
1117 * enter non simplex mode. This implements the neccessary logic to
1118 * perform the task on such devices. Calling it on other devices will
1119 * have -undefined- behaviour.
1122 int ata_pci_clear_simplex(struct pci_dev
*pdev
)
1124 unsigned long bmdma
= pci_resource_start(pdev
, 4);
1130 simplex
= inb(bmdma
+ 0x02);
1131 outb(simplex
& 0x60, bmdma
+ 0x02);
1132 simplex
= inb(bmdma
+ 0x02);
1138 unsigned long ata_pci_default_filter(const struct ata_port
*ap
, struct ata_device
*adev
, unsigned long xfer_mask
)
1140 /* Filter out DMA modes if the device has been configured by
1141 the BIOS as PIO only */
1143 if (ap
->ioaddr
.bmdma_addr
== 0)
1144 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
1148 #endif /* CONFIG_PCI */