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/config.h>
36 #include <linux/kernel.h>
37 #include <linux/pci.h>
38 #include <linux/libata.h>
43 * ata_tf_load_pio - send taskfile registers to host controller
44 * @ap: Port to which output is sent
45 * @tf: ATA taskfile register set
47 * Outputs ATA taskfile to standard ATA host controller.
50 * Inherited from caller.
53 static void ata_tf_load_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
55 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
56 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
58 if (tf
->ctl
!= ap
->last_ctl
) {
59 outb(tf
->ctl
, ioaddr
->ctl_addr
);
60 ap
->last_ctl
= tf
->ctl
;
64 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
65 outb(tf
->hob_feature
, ioaddr
->feature_addr
);
66 outb(tf
->hob_nsect
, ioaddr
->nsect_addr
);
67 outb(tf
->hob_lbal
, ioaddr
->lbal_addr
);
68 outb(tf
->hob_lbam
, ioaddr
->lbam_addr
);
69 outb(tf
->hob_lbah
, ioaddr
->lbah_addr
);
70 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
79 outb(tf
->feature
, ioaddr
->feature_addr
);
80 outb(tf
->nsect
, ioaddr
->nsect_addr
);
81 outb(tf
->lbal
, ioaddr
->lbal_addr
);
82 outb(tf
->lbam
, ioaddr
->lbam_addr
);
83 outb(tf
->lbah
, ioaddr
->lbah_addr
);
84 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
92 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
93 outb(tf
->device
, ioaddr
->device_addr
);
94 VPRINTK("device 0x%X\n", tf
->device
);
101 * ata_tf_load_mmio - send taskfile registers to host controller
102 * @ap: Port to which output is sent
103 * @tf: ATA taskfile register set
105 * Outputs ATA taskfile to standard ATA host controller using MMIO.
108 * Inherited from caller.
111 static void ata_tf_load_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
113 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
114 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
116 if (tf
->ctl
!= ap
->last_ctl
) {
117 writeb(tf
->ctl
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
118 ap
->last_ctl
= tf
->ctl
;
122 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
123 writeb(tf
->hob_feature
, (void __iomem
*) ioaddr
->feature_addr
);
124 writeb(tf
->hob_nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
125 writeb(tf
->hob_lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
126 writeb(tf
->hob_lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
127 writeb(tf
->hob_lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
128 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
137 writeb(tf
->feature
, (void __iomem
*) ioaddr
->feature_addr
);
138 writeb(tf
->nsect
, (void __iomem
*) ioaddr
->nsect_addr
);
139 writeb(tf
->lbal
, (void __iomem
*) ioaddr
->lbal_addr
);
140 writeb(tf
->lbam
, (void __iomem
*) ioaddr
->lbam_addr
);
141 writeb(tf
->lbah
, (void __iomem
*) ioaddr
->lbah_addr
);
142 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
150 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
151 writeb(tf
->device
, (void __iomem
*) ioaddr
->device_addr
);
152 VPRINTK("device 0x%X\n", tf
->device
);
160 * ata_tf_load - send taskfile registers to host controller
161 * @ap: Port to which output is sent
162 * @tf: ATA taskfile register set
164 * Outputs ATA taskfile to standard ATA host controller using MMIO
165 * or PIO as indicated by the ATA_FLAG_MMIO flag.
166 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
167 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
168 * hob_lbal, hob_lbam, and hob_lbah.
170 * This function waits for idle (!BUSY and !DRQ) after writing
171 * registers. If the control register has a new value, this
172 * function also waits for idle after writing control and before
173 * writing the remaining registers.
175 * May be used as the tf_load() entry in ata_port_operations.
178 * Inherited from caller.
180 void ata_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
182 if (ap
->flags
& ATA_FLAG_MMIO
)
183 ata_tf_load_mmio(ap
, tf
);
185 ata_tf_load_pio(ap
, tf
);
189 * ata_exec_command_pio - issue ATA command to host controller
190 * @ap: port to which command is being issued
191 * @tf: ATA taskfile register set
193 * Issues PIO write to ATA command register, with proper
194 * synchronization with interrupt handler / other threads.
197 * spin_lock_irqsave(host_set lock)
200 static void ata_exec_command_pio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
202 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
204 outb(tf
->command
, ap
->ioaddr
.command_addr
);
210 * ata_exec_command_mmio - issue ATA command to host controller
211 * @ap: port to which command is being issued
212 * @tf: ATA taskfile register set
214 * Issues MMIO write to ATA command register, with proper
215 * synchronization with interrupt handler / other threads.
217 * FIXME: missing write posting for 400nS delay enforcement
220 * spin_lock_irqsave(host_set lock)
223 static void ata_exec_command_mmio(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
225 DPRINTK("ata%u: cmd 0x%X\n", ap
->id
, tf
->command
);
227 writeb(tf
->command
, (void __iomem
*) ap
->ioaddr
.command_addr
);
233 * ata_exec_command - issue ATA command to host controller
234 * @ap: port to which command is being issued
235 * @tf: ATA taskfile register set
237 * Issues PIO/MMIO write to ATA command register, with proper
238 * synchronization with interrupt handler / other threads.
241 * spin_lock_irqsave(host_set lock)
243 void ata_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
245 if (ap
->flags
& ATA_FLAG_MMIO
)
246 ata_exec_command_mmio(ap
, tf
);
248 ata_exec_command_pio(ap
, tf
);
252 * ata_tf_read_pio - input device's ATA taskfile shadow registers
253 * @ap: Port from which input is read
254 * @tf: ATA taskfile register set for storing input
256 * Reads ATA taskfile registers for currently-selected device
260 * Inherited from caller.
263 static void ata_tf_read_pio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
265 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
267 tf
->command
= ata_check_status(ap
);
268 tf
->feature
= inb(ioaddr
->error_addr
);
269 tf
->nsect
= inb(ioaddr
->nsect_addr
);
270 tf
->lbal
= inb(ioaddr
->lbal_addr
);
271 tf
->lbam
= inb(ioaddr
->lbam_addr
);
272 tf
->lbah
= inb(ioaddr
->lbah_addr
);
273 tf
->device
= inb(ioaddr
->device_addr
);
275 if (tf
->flags
& ATA_TFLAG_LBA48
) {
276 outb(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
277 tf
->hob_feature
= inb(ioaddr
->error_addr
);
278 tf
->hob_nsect
= inb(ioaddr
->nsect_addr
);
279 tf
->hob_lbal
= inb(ioaddr
->lbal_addr
);
280 tf
->hob_lbam
= inb(ioaddr
->lbam_addr
);
281 tf
->hob_lbah
= inb(ioaddr
->lbah_addr
);
286 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
287 * @ap: Port from which input is read
288 * @tf: ATA taskfile register set for storing input
290 * Reads ATA taskfile registers for currently-selected device
294 * Inherited from caller.
297 static void ata_tf_read_mmio(struct ata_port
*ap
, struct ata_taskfile
*tf
)
299 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
301 tf
->command
= ata_check_status(ap
);
302 tf
->feature
= readb((void __iomem
*)ioaddr
->error_addr
);
303 tf
->nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
304 tf
->lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
305 tf
->lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
306 tf
->lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
307 tf
->device
= readb((void __iomem
*)ioaddr
->device_addr
);
309 if (tf
->flags
& ATA_TFLAG_LBA48
) {
310 writeb(tf
->ctl
| ATA_HOB
, (void __iomem
*) ap
->ioaddr
.ctl_addr
);
311 tf
->hob_feature
= readb((void __iomem
*)ioaddr
->error_addr
);
312 tf
->hob_nsect
= readb((void __iomem
*)ioaddr
->nsect_addr
);
313 tf
->hob_lbal
= readb((void __iomem
*)ioaddr
->lbal_addr
);
314 tf
->hob_lbam
= readb((void __iomem
*)ioaddr
->lbam_addr
);
315 tf
->hob_lbah
= readb((void __iomem
*)ioaddr
->lbah_addr
);
321 * ata_tf_read - input device's ATA taskfile shadow registers
322 * @ap: Port from which input is read
323 * @tf: ATA taskfile register set for storing input
325 * Reads ATA taskfile registers for currently-selected device
328 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
329 * is set, also reads the hob registers.
331 * May be used as the tf_read() entry in ata_port_operations.
334 * Inherited from caller.
336 void ata_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
338 if (ap
->flags
& ATA_FLAG_MMIO
)
339 ata_tf_read_mmio(ap
, tf
);
341 ata_tf_read_pio(ap
, tf
);
345 * ata_check_status_pio - Read device status reg & clear interrupt
346 * @ap: port where the device is
348 * Reads ATA taskfile status register for currently-selected device
349 * and return its value. This also clears pending interrupts
353 * Inherited from caller.
355 static u8
ata_check_status_pio(struct ata_port
*ap
)
357 return inb(ap
->ioaddr
.status_addr
);
361 * ata_check_status_mmio - Read device status reg & clear interrupt
362 * @ap: port where the device is
364 * Reads ATA taskfile status register for currently-selected device
365 * via MMIO and return its value. This also clears pending interrupts
369 * Inherited from caller.
371 static u8
ata_check_status_mmio(struct ata_port
*ap
)
373 return readb((void __iomem
*) ap
->ioaddr
.status_addr
);
378 * ata_check_status - Read device status reg & clear interrupt
379 * @ap: port where the device is
381 * Reads ATA taskfile status register for currently-selected device
382 * and return its value. This also clears pending interrupts
385 * May be used as the check_status() entry in ata_port_operations.
388 * Inherited from caller.
390 u8
ata_check_status(struct ata_port
*ap
)
392 if (ap
->flags
& ATA_FLAG_MMIO
)
393 return ata_check_status_mmio(ap
);
394 return ata_check_status_pio(ap
);
399 * ata_altstatus - Read device alternate status reg
400 * @ap: port where the device is
402 * Reads ATA taskfile alternate status register for
403 * currently-selected device and return its value.
405 * Note: may NOT be used as the check_altstatus() entry in
406 * ata_port_operations.
409 * Inherited from caller.
411 u8
ata_altstatus(struct ata_port
*ap
)
413 if (ap
->ops
->check_altstatus
)
414 return ap
->ops
->check_altstatus(ap
);
416 if (ap
->flags
& ATA_FLAG_MMIO
)
417 return readb((void __iomem
*)ap
->ioaddr
.altstatus_addr
);
418 return inb(ap
->ioaddr
.altstatus_addr
);
422 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
423 * @qc: Info associated with this ATA transaction.
426 * spin_lock_irqsave(host_set lock)
429 static void ata_bmdma_setup_mmio (struct ata_queued_cmd
*qc
)
431 struct ata_port
*ap
= qc
->ap
;
432 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
434 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
436 /* load PRD table addr. */
437 mb(); /* make sure PRD table writes are visible to controller */
438 writel(ap
->prd_dma
, mmio
+ ATA_DMA_TABLE_OFS
);
440 /* specify data direction, triple-check start bit is clear */
441 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
442 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
444 dmactl
|= ATA_DMA_WR
;
445 writeb(dmactl
, mmio
+ ATA_DMA_CMD
);
447 /* issue r/w command */
448 ap
->ops
->exec_command(ap
, &qc
->tf
);
452 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
453 * @qc: Info associated with this ATA transaction.
456 * spin_lock_irqsave(host_set lock)
459 static void ata_bmdma_start_mmio (struct ata_queued_cmd
*qc
)
461 struct ata_port
*ap
= qc
->ap
;
462 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
465 /* start host DMA transaction */
466 dmactl
= readb(mmio
+ ATA_DMA_CMD
);
467 writeb(dmactl
| ATA_DMA_START
, mmio
+ ATA_DMA_CMD
);
469 /* Strictly, one may wish to issue a readb() here, to
470 * flush the mmio write. However, control also passes
471 * to the hardware at this point, and it will interrupt
472 * us when we are to resume control. So, in effect,
473 * we don't care when the mmio write flushes.
474 * Further, a read of the DMA status register _immediately_
475 * following the write may not be what certain flaky hardware
476 * is expected, so I think it is best to not add a readb()
477 * without first all the MMIO ATA cards/mobos.
478 * Or maybe I'm just being paranoid.
483 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
484 * @qc: Info associated with this ATA transaction.
487 * spin_lock_irqsave(host_set lock)
490 static void ata_bmdma_setup_pio (struct ata_queued_cmd
*qc
)
492 struct ata_port
*ap
= qc
->ap
;
493 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
496 /* load PRD table addr. */
497 outl(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
499 /* specify data direction, triple-check start bit is clear */
500 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
501 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
503 dmactl
|= ATA_DMA_WR
;
504 outb(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
506 /* issue r/w command */
507 ap
->ops
->exec_command(ap
, &qc
->tf
);
511 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
512 * @qc: Info associated with this ATA transaction.
515 * spin_lock_irqsave(host_set lock)
518 static void ata_bmdma_start_pio (struct ata_queued_cmd
*qc
)
520 struct ata_port
*ap
= qc
->ap
;
523 /* start host DMA transaction */
524 dmactl
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
525 outb(dmactl
| ATA_DMA_START
,
526 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
531 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
532 * @qc: Info associated with this ATA transaction.
534 * Writes the ATA_DMA_START flag to the DMA command register.
536 * May be used as the bmdma_start() entry in ata_port_operations.
539 * spin_lock_irqsave(host_set lock)
541 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
543 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
544 ata_bmdma_start_mmio(qc
);
546 ata_bmdma_start_pio(qc
);
551 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
552 * @qc: Info associated with this ATA transaction.
554 * Writes address of PRD table to device's PRD Table Address
555 * register, sets the DMA control register, and calls
556 * ops->exec_command() to start the transfer.
558 * May be used as the bmdma_setup() entry in ata_port_operations.
561 * spin_lock_irqsave(host_set lock)
563 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
565 if (qc
->ap
->flags
& ATA_FLAG_MMIO
)
566 ata_bmdma_setup_mmio(qc
);
568 ata_bmdma_setup_pio(qc
);
573 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
574 * @ap: Port associated with this ATA transaction.
576 * Clear interrupt and error flags in DMA status register.
578 * May be used as the irq_clear() entry in ata_port_operations.
581 * spin_lock_irqsave(host_set lock)
584 void ata_bmdma_irq_clear(struct ata_port
*ap
)
586 if (!ap
->ioaddr
.bmdma_addr
)
589 if (ap
->flags
& ATA_FLAG_MMIO
) {
591 ((void __iomem
*) ap
->ioaddr
.bmdma_addr
) + ATA_DMA_STATUS
;
592 writeb(readb(mmio
), mmio
);
594 unsigned long addr
= ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
;
595 outb(inb(addr
), addr
);
601 * ata_bmdma_status - Read PCI IDE BMDMA status
602 * @ap: Port associated with this ATA transaction.
604 * Read and return BMDMA status register.
606 * May be used as the bmdma_status() entry in ata_port_operations.
609 * spin_lock_irqsave(host_set lock)
612 u8
ata_bmdma_status(struct ata_port
*ap
)
615 if (ap
->flags
& ATA_FLAG_MMIO
) {
616 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
617 host_stat
= readb(mmio
+ ATA_DMA_STATUS
);
619 host_stat
= inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
625 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
626 * @qc: Command we are ending DMA for
628 * Clears the ATA_DMA_START flag in the dma control register
630 * May be used as the bmdma_stop() entry in ata_port_operations.
633 * spin_lock_irqsave(host_set lock)
636 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
638 struct ata_port
*ap
= qc
->ap
;
639 if (ap
->flags
& ATA_FLAG_MMIO
) {
640 void __iomem
*mmio
= (void __iomem
*) ap
->ioaddr
.bmdma_addr
;
642 /* clear start/stop bit */
643 writeb(readb(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
646 /* clear start/stop bit */
647 outb(inb(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
648 ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
651 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
652 ata_altstatus(ap
); /* dummy read */
656 * ata_bmdma_freeze - Freeze BMDMA controller port
657 * @ap: port to freeze
659 * Freeze BMDMA controller port.
662 * Inherited from caller.
664 void ata_bmdma_freeze(struct ata_port
*ap
)
666 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
669 ap
->last_ctl
= ap
->ctl
;
671 if (ap
->flags
& ATA_FLAG_MMIO
)
672 writeb(ap
->ctl
, (void __iomem
*)ioaddr
->ctl_addr
);
674 outb(ap
->ctl
, ioaddr
->ctl_addr
);
678 * ata_bmdma_thaw - Thaw BMDMA controller port
681 * Thaw BMDMA controller port.
684 * Inherited from caller.
686 void ata_bmdma_thaw(struct ata_port
*ap
)
688 /* clear & re-enable interrupts */
690 ap
->ops
->irq_clear(ap
);
691 if (ap
->ioaddr
.ctl_addr
) /* FIXME: hack. create a hook instead */
696 * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
697 * @ap: port to handle error for
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_reset_fn_t softreset
,
714 ata_reset_fn_t hardreset
, ata_postreset_fn_t postreset
)
716 struct ata_host_set
*host_set
= ap
->host_set
;
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(&host_set
->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(&host_set
->lock
, flags
);
759 ata_eh_thaw_port(ap
);
761 /* PIO and DMA engines have been stopped, perform recovery */
762 ata_do_eh(ap
, 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_softreset
, hardreset
, ata_std_postreset
);
786 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
788 * @qc: internal command to clean up
791 * Kernel thread context (may sleep)
793 void ata_bmdma_post_internal_cmd(struct ata_queued_cmd
*qc
)
799 static struct ata_probe_ent
*
800 ata_probe_ent_alloc(struct device
*dev
, const struct ata_port_info
*port
)
802 struct ata_probe_ent
*probe_ent
;
804 probe_ent
= kzalloc(sizeof(*probe_ent
), GFP_KERNEL
);
806 printk(KERN_ERR DRV_NAME
"(%s): out of memory\n",
807 kobject_name(&(dev
->kobj
)));
811 INIT_LIST_HEAD(&probe_ent
->node
);
812 probe_ent
->dev
= dev
;
814 probe_ent
->sht
= port
->sht
;
815 probe_ent
->host_flags
= port
->host_flags
;
816 probe_ent
->pio_mask
= port
->pio_mask
;
817 probe_ent
->mwdma_mask
= port
->mwdma_mask
;
818 probe_ent
->udma_mask
= port
->udma_mask
;
819 probe_ent
->port_ops
= port
->port_ops
;
826 * ata_pci_init_native_mode - Initialize native-mode driver
827 * @pdev: pci device to be initialized
828 * @port: array[2] of pointers to port info structures.
829 * @ports: bitmap of ports present
831 * Utility function which allocates and initializes an
832 * ata_probe_ent structure for a standard dual-port
833 * PIO-based IDE controller. The returned ata_probe_ent
834 * structure can be passed to ata_device_add(). The returned
835 * ata_probe_ent structure should then be freed with kfree().
837 * The caller need only pass the address of the primary port, the
838 * secondary will be deduced automatically. If the device has non
839 * standard secondary port mappings this function can be called twice,
840 * once for each interface.
843 struct ata_probe_ent
*
844 ata_pci_init_native_mode(struct pci_dev
*pdev
, struct ata_port_info
**port
, int ports
)
846 struct ata_probe_ent
*probe_ent
=
847 ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
[0]);
854 probe_ent
->irq
= pdev
->irq
;
855 probe_ent
->irq_flags
= SA_SHIRQ
;
856 probe_ent
->private_data
= port
[0]->private_data
;
858 if (ports
& ATA_PORT_PRIMARY
) {
859 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 0);
860 probe_ent
->port
[p
].altstatus_addr
=
861 probe_ent
->port
[p
].ctl_addr
=
862 pci_resource_start(pdev
, 1) | ATA_PCI_CTL_OFS
;
863 bmdma
= pci_resource_start(pdev
, 4);
865 if (inb(bmdma
+ 2) & 0x80)
866 probe_ent
->host_set_flags
|= ATA_HOST_SIMPLEX
;
867 probe_ent
->port
[p
].bmdma_addr
= bmdma
;
869 ata_std_ports(&probe_ent
->port
[p
]);
873 if (ports
& ATA_PORT_SECONDARY
) {
874 probe_ent
->port
[p
].cmd_addr
= pci_resource_start(pdev
, 2);
875 probe_ent
->port
[p
].altstatus_addr
=
876 probe_ent
->port
[p
].ctl_addr
=
877 pci_resource_start(pdev
, 3) | ATA_PCI_CTL_OFS
;
878 bmdma
= pci_resource_start(pdev
, 4);
881 if(inb(bmdma
+ 2) & 0x80)
882 probe_ent
->host_set_flags
|= ATA_HOST_SIMPLEX
;
883 probe_ent
->port
[p
].bmdma_addr
= bmdma
;
885 ata_std_ports(&probe_ent
->port
[p
]);
889 probe_ent
->n_ports
= p
;
894 static struct ata_probe_ent
*ata_pci_init_legacy_port(struct pci_dev
*pdev
,
895 struct ata_port_info
*port
, int port_num
)
897 struct ata_probe_ent
*probe_ent
;
900 probe_ent
= ata_probe_ent_alloc(pci_dev_to_dev(pdev
), port
);
904 probe_ent
->legacy_mode
= 1;
905 probe_ent
->n_ports
= 1;
906 probe_ent
->hard_port_no
= port_num
;
907 probe_ent
->private_data
= port
->private_data
;
913 probe_ent
->port
[0].cmd_addr
= 0x1f0;
914 probe_ent
->port
[0].altstatus_addr
=
915 probe_ent
->port
[0].ctl_addr
= 0x3f6;
919 probe_ent
->port
[0].cmd_addr
= 0x170;
920 probe_ent
->port
[0].altstatus_addr
=
921 probe_ent
->port
[0].ctl_addr
= 0x376;
925 bmdma
= pci_resource_start(pdev
, 4);
927 bmdma
+= 8 * port_num
;
928 probe_ent
->port
[0].bmdma_addr
= bmdma
;
929 if (inb(bmdma
+ 2) & 0x80)
930 probe_ent
->host_set_flags
|= ATA_HOST_SIMPLEX
;
932 ata_std_ports(&probe_ent
->port
[0]);
939 * ata_pci_init_one - Initialize/register PCI IDE host controller
940 * @pdev: Controller to be initialized
941 * @port_info: Information from low-level host driver
942 * @n_ports: Number of ports attached to host controller
944 * This is a helper function which can be called from a driver's
945 * xxx_init_one() probe function if the hardware uses traditional
946 * IDE taskfile registers.
948 * This function calls pci_enable_device(), reserves its register
949 * regions, sets the dma mask, enables bus master mode, and calls
953 * Inherited from PCI layer (may sleep).
956 * Zero on success, negative on errno-based value on error.
959 int ata_pci_init_one (struct pci_dev
*pdev
, struct ata_port_info
**port_info
,
960 unsigned int n_ports
)
962 struct ata_probe_ent
*probe_ent
= NULL
, *probe_ent2
= NULL
;
963 struct ata_port_info
*port
[2];
965 unsigned int legacy_mode
= 0;
966 int disable_dev_on_err
= 1;
971 port
[0] = port_info
[0];
973 port
[1] = port_info
[1];
977 if ((port
[0]->host_flags
& ATA_FLAG_NO_LEGACY
) == 0
978 && (pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
979 /* TODO: What if one channel is in native mode ... */
980 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
981 mask
= (1 << 2) | (1 << 0);
982 if ((tmp8
& mask
) != mask
)
983 legacy_mode
= (1 << 3);
987 if ((!legacy_mode
) && (n_ports
> 2)) {
988 printk(KERN_ERR
"ata: BUG: native mode, n_ports > 2\n");
993 /* FIXME: Really for ATA it isn't safe because the device may be
994 multi-purpose and we want to leave it alone if it was already
995 enabled. Secondly for shared use as Arjan says we want refcounting
997 Checking dev->is_enabled is insufficient as this is not set at
998 boot for the primary video which is BIOS enabled
1001 rc
= pci_enable_device(pdev
);
1005 rc
= pci_request_regions(pdev
, DRV_NAME
);
1007 disable_dev_on_err
= 0;
1011 /* FIXME: Should use platform specific mappers for legacy port ranges */
1013 if (!request_region(0x1f0, 8, "libata")) {
1014 struct resource
*conflict
, res
;
1016 res
.end
= 0x1f0 + 8 - 1;
1017 conflict
= ____request_resource(&ioport_resource
, &res
);
1018 if (!strcmp(conflict
->name
, "libata"))
1019 legacy_mode
|= (1 << 0);
1021 disable_dev_on_err
= 0;
1022 printk(KERN_WARNING
"ata: 0x1f0 IDE port busy\n");
1025 legacy_mode
|= (1 << 0);
1027 if (!request_region(0x170, 8, "libata")) {
1028 struct resource
*conflict
, res
;
1030 res
.end
= 0x170 + 8 - 1;
1031 conflict
= ____request_resource(&ioport_resource
, &res
);
1032 if (!strcmp(conflict
->name
, "libata"))
1033 legacy_mode
|= (1 << 1);
1035 disable_dev_on_err
= 0;
1036 printk(KERN_WARNING
"ata: 0x170 IDE port busy\n");
1039 legacy_mode
|= (1 << 1);
1042 /* we have legacy mode, but all ports are unavailable */
1043 if (legacy_mode
== (1 << 3)) {
1045 goto err_out_regions
;
1048 /* FIXME: If we get no DMA mask we should fall back to PIO */
1049 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
1051 goto err_out_regions
;
1052 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
1054 goto err_out_regions
;
1057 if (legacy_mode
& (1 << 0))
1058 probe_ent
= ata_pci_init_legacy_port(pdev
, port
[0], 0);
1059 if (legacy_mode
& (1 << 1))
1060 probe_ent2
= ata_pci_init_legacy_port(pdev
, port
[1], 1);
1063 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
| ATA_PORT_SECONDARY
);
1065 probe_ent
= ata_pci_init_native_mode(pdev
, port
, ATA_PORT_PRIMARY
);
1067 if (!probe_ent
&& !probe_ent2
) {
1069 goto err_out_regions
;
1072 pci_set_master(pdev
);
1074 /* FIXME: check ata_device_add return */
1076 if (legacy_mode
& (1 << 0))
1077 ata_device_add(probe_ent
);
1078 if (legacy_mode
& (1 << 1))
1079 ata_device_add(probe_ent2
);
1081 ata_device_add(probe_ent
);
1089 if (legacy_mode
& (1 << 0))
1090 release_region(0x1f0, 8);
1091 if (legacy_mode
& (1 << 1))
1092 release_region(0x170, 8);
1093 pci_release_regions(pdev
);
1095 if (disable_dev_on_err
)
1096 pci_disable_device(pdev
);
1101 * ata_pci_clear_simplex - attempt to kick device out of simplex
1104 * Some PCI ATA devices report simplex mode but in fact can be told to
1105 * enter non simplex mode. This implements the neccessary logic to
1106 * perform the task on such devices. Calling it on other devices will
1107 * have -undefined- behaviour.
1110 int ata_pci_clear_simplex(struct pci_dev
*pdev
)
1112 unsigned long bmdma
= pci_resource_start(pdev
, 4);
1118 simplex
= inb(bmdma
+ 0x02);
1119 outb(simplex
& 0x60, bmdma
+ 0x02);
1120 simplex
= inb(bmdma
+ 0x02);
1126 unsigned long ata_pci_default_filter(const struct ata_port
*ap
, struct ata_device
*adev
, unsigned long xfer_mask
)
1128 /* Filter out DMA modes if the device has been configured by
1129 the BIOS as PIO only */
1131 if (ap
->ioaddr
.bmdma_addr
== 0)
1132 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
1136 #endif /* CONFIG_PCI */