[PATCH] libata: move BMDMA host status recording from EH to interrupt handler
[linux-2.6/kvm.git] / drivers / ata / libata-sff.c
blob95ff186f0ccd713d337ec5eadb9ee062f7255984
1 /*
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
6 * on emails.
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)
15 * any later version.
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>
39 #include "libata.h"
41 /**
42 * ata_irq_on - Enable interrupts on a port.
43 * @ap: Port on which interrupts are enabled.
45 * Enable interrupts on a legacy IDE device using MMIO or PIO,
46 * wait for idle, clear any pending interrupts.
48 * LOCKING:
49 * Inherited from caller.
51 u8 ata_irq_on(struct ata_port *ap)
53 struct ata_ioports *ioaddr = &ap->ioaddr;
54 u8 tmp;
56 ap->ctl &= ~ATA_NIEN;
57 ap->last_ctl = ap->ctl;
59 if (ap->flags & ATA_FLAG_MMIO)
60 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
61 else
62 outb(ap->ctl, ioaddr->ctl_addr);
63 tmp = ata_wait_idle(ap);
65 ap->ops->irq_clear(ap);
67 return tmp;
70 /**
71 * ata_tf_load_pio - send taskfile registers to host controller
72 * @ap: Port to which output is sent
73 * @tf: ATA taskfile register set
75 * Outputs ATA taskfile to standard ATA host controller.
77 * LOCKING:
78 * Inherited from caller.
81 static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
83 struct ata_ioports *ioaddr = &ap->ioaddr;
84 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
86 if (tf->ctl != ap->last_ctl) {
87 outb(tf->ctl, ioaddr->ctl_addr);
88 ap->last_ctl = tf->ctl;
89 ata_wait_idle(ap);
92 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
93 outb(tf->hob_feature, ioaddr->feature_addr);
94 outb(tf->hob_nsect, ioaddr->nsect_addr);
95 outb(tf->hob_lbal, ioaddr->lbal_addr);
96 outb(tf->hob_lbam, ioaddr->lbam_addr);
97 outb(tf->hob_lbah, ioaddr->lbah_addr);
98 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
99 tf->hob_feature,
100 tf->hob_nsect,
101 tf->hob_lbal,
102 tf->hob_lbam,
103 tf->hob_lbah);
106 if (is_addr) {
107 outb(tf->feature, ioaddr->feature_addr);
108 outb(tf->nsect, ioaddr->nsect_addr);
109 outb(tf->lbal, ioaddr->lbal_addr);
110 outb(tf->lbam, ioaddr->lbam_addr);
111 outb(tf->lbah, ioaddr->lbah_addr);
112 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
113 tf->feature,
114 tf->nsect,
115 tf->lbal,
116 tf->lbam,
117 tf->lbah);
120 if (tf->flags & ATA_TFLAG_DEVICE) {
121 outb(tf->device, ioaddr->device_addr);
122 VPRINTK("device 0x%X\n", tf->device);
125 ata_wait_idle(ap);
129 * ata_tf_load_mmio - send taskfile registers to host controller
130 * @ap: Port to which output is sent
131 * @tf: ATA taskfile register set
133 * Outputs ATA taskfile to standard ATA host controller using MMIO.
135 * LOCKING:
136 * Inherited from caller.
139 static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
141 struct ata_ioports *ioaddr = &ap->ioaddr;
142 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
144 if (tf->ctl != ap->last_ctl) {
145 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
146 ap->last_ctl = tf->ctl;
147 ata_wait_idle(ap);
150 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
151 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
152 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
153 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
154 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
155 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
156 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
157 tf->hob_feature,
158 tf->hob_nsect,
159 tf->hob_lbal,
160 tf->hob_lbam,
161 tf->hob_lbah);
164 if (is_addr) {
165 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
166 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
167 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
168 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
169 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
170 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
171 tf->feature,
172 tf->nsect,
173 tf->lbal,
174 tf->lbam,
175 tf->lbah);
178 if (tf->flags & ATA_TFLAG_DEVICE) {
179 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
180 VPRINTK("device 0x%X\n", tf->device);
183 ata_wait_idle(ap);
188 * ata_tf_load - send taskfile registers to host controller
189 * @ap: Port to which output is sent
190 * @tf: ATA taskfile register set
192 * Outputs ATA taskfile to standard ATA host controller using MMIO
193 * or PIO as indicated by the ATA_FLAG_MMIO flag.
194 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
195 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
196 * hob_lbal, hob_lbam, and hob_lbah.
198 * This function waits for idle (!BUSY and !DRQ) after writing
199 * registers. If the control register has a new value, this
200 * function also waits for idle after writing control and before
201 * writing the remaining registers.
203 * May be used as the tf_load() entry in ata_port_operations.
205 * LOCKING:
206 * Inherited from caller.
208 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
210 if (ap->flags & ATA_FLAG_MMIO)
211 ata_tf_load_mmio(ap, tf);
212 else
213 ata_tf_load_pio(ap, tf);
217 * ata_exec_command_pio - issue ATA command to host controller
218 * @ap: port to which command is being issued
219 * @tf: ATA taskfile register set
221 * Issues PIO write to ATA command register, with proper
222 * synchronization with interrupt handler / other threads.
224 * LOCKING:
225 * spin_lock_irqsave(host lock)
228 static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
230 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
232 outb(tf->command, ap->ioaddr.command_addr);
233 ata_pause(ap);
238 * ata_exec_command_mmio - issue ATA command to host controller
239 * @ap: port to which command is being issued
240 * @tf: ATA taskfile register set
242 * Issues MMIO write to ATA command register, with proper
243 * synchronization with interrupt handler / other threads.
245 * FIXME: missing write posting for 400nS delay enforcement
247 * LOCKING:
248 * spin_lock_irqsave(host lock)
251 static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
253 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
255 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
256 ata_pause(ap);
261 * ata_exec_command - issue ATA command to host controller
262 * @ap: port to which command is being issued
263 * @tf: ATA taskfile register set
265 * Issues PIO/MMIO write to ATA command register, with proper
266 * synchronization with interrupt handler / other threads.
268 * LOCKING:
269 * spin_lock_irqsave(host lock)
271 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
273 if (ap->flags & ATA_FLAG_MMIO)
274 ata_exec_command_mmio(ap, tf);
275 else
276 ata_exec_command_pio(ap, tf);
280 * ata_tf_read_pio - input device's ATA taskfile shadow registers
281 * @ap: Port from which input is read
282 * @tf: ATA taskfile register set for storing input
284 * Reads ATA taskfile registers for currently-selected device
285 * into @tf.
287 * LOCKING:
288 * Inherited from caller.
291 static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
293 struct ata_ioports *ioaddr = &ap->ioaddr;
295 tf->command = ata_check_status(ap);
296 tf->feature = inb(ioaddr->error_addr);
297 tf->nsect = inb(ioaddr->nsect_addr);
298 tf->lbal = inb(ioaddr->lbal_addr);
299 tf->lbam = inb(ioaddr->lbam_addr);
300 tf->lbah = inb(ioaddr->lbah_addr);
301 tf->device = inb(ioaddr->device_addr);
303 if (tf->flags & ATA_TFLAG_LBA48) {
304 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
305 tf->hob_feature = inb(ioaddr->error_addr);
306 tf->hob_nsect = inb(ioaddr->nsect_addr);
307 tf->hob_lbal = inb(ioaddr->lbal_addr);
308 tf->hob_lbam = inb(ioaddr->lbam_addr);
309 tf->hob_lbah = inb(ioaddr->lbah_addr);
314 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
315 * @ap: Port from which input is read
316 * @tf: ATA taskfile register set for storing input
318 * Reads ATA taskfile registers for currently-selected device
319 * into @tf via MMIO.
321 * LOCKING:
322 * Inherited from caller.
325 static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
327 struct ata_ioports *ioaddr = &ap->ioaddr;
329 tf->command = ata_check_status(ap);
330 tf->feature = readb((void __iomem *)ioaddr->error_addr);
331 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
332 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
333 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
334 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
335 tf->device = readb((void __iomem *)ioaddr->device_addr);
337 if (tf->flags & ATA_TFLAG_LBA48) {
338 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
339 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
340 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
341 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
342 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
343 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
349 * ata_tf_read - input device's ATA taskfile shadow registers
350 * @ap: Port from which input is read
351 * @tf: ATA taskfile register set for storing input
353 * Reads ATA taskfile registers for currently-selected device
354 * into @tf.
356 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
357 * is set, also reads the hob registers.
359 * May be used as the tf_read() entry in ata_port_operations.
361 * LOCKING:
362 * Inherited from caller.
364 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
366 if (ap->flags & ATA_FLAG_MMIO)
367 ata_tf_read_mmio(ap, tf);
368 else
369 ata_tf_read_pio(ap, tf);
373 * ata_check_status_pio - Read device status reg & clear interrupt
374 * @ap: port where the device is
376 * Reads ATA taskfile status register for currently-selected device
377 * and return its value. This also clears pending interrupts
378 * from this device
380 * LOCKING:
381 * Inherited from caller.
383 static u8 ata_check_status_pio(struct ata_port *ap)
385 return inb(ap->ioaddr.status_addr);
389 * ata_check_status_mmio - Read device status reg & clear interrupt
390 * @ap: port where the device is
392 * Reads ATA taskfile status register for currently-selected device
393 * via MMIO and return its value. This also clears pending interrupts
394 * from this device
396 * LOCKING:
397 * Inherited from caller.
399 static u8 ata_check_status_mmio(struct ata_port *ap)
401 return readb((void __iomem *) ap->ioaddr.status_addr);
406 * ata_check_status - Read device status reg & clear interrupt
407 * @ap: port where the device is
409 * Reads ATA taskfile status register for currently-selected device
410 * and return its value. This also clears pending interrupts
411 * from this device
413 * May be used as the check_status() entry in ata_port_operations.
415 * LOCKING:
416 * Inherited from caller.
418 u8 ata_check_status(struct ata_port *ap)
420 if (ap->flags & ATA_FLAG_MMIO)
421 return ata_check_status_mmio(ap);
422 return ata_check_status_pio(ap);
427 * ata_altstatus - Read device alternate status reg
428 * @ap: port where the device is
430 * Reads ATA taskfile alternate status register for
431 * currently-selected device and return its value.
433 * Note: may NOT be used as the check_altstatus() entry in
434 * ata_port_operations.
436 * LOCKING:
437 * Inherited from caller.
439 u8 ata_altstatus(struct ata_port *ap)
441 if (ap->ops->check_altstatus)
442 return ap->ops->check_altstatus(ap);
444 if (ap->flags & ATA_FLAG_MMIO)
445 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
446 return inb(ap->ioaddr.altstatus_addr);
450 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
451 * @qc: Info associated with this ATA transaction.
453 * LOCKING:
454 * spin_lock_irqsave(host lock)
457 static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
459 struct ata_port *ap = qc->ap;
460 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
461 u8 dmactl;
462 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
464 /* load PRD table addr. */
465 mb(); /* make sure PRD table writes are visible to controller */
466 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
468 /* specify data direction, triple-check start bit is clear */
469 dmactl = readb(mmio + ATA_DMA_CMD);
470 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
471 if (!rw)
472 dmactl |= ATA_DMA_WR;
473 writeb(dmactl, mmio + ATA_DMA_CMD);
475 /* issue r/w command */
476 ap->ops->exec_command(ap, &qc->tf);
480 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
481 * @qc: Info associated with this ATA transaction.
483 * LOCKING:
484 * spin_lock_irqsave(host lock)
487 static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
489 struct ata_port *ap = qc->ap;
490 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
491 u8 dmactl;
493 /* start host DMA transaction */
494 dmactl = readb(mmio + ATA_DMA_CMD);
495 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
497 /* Strictly, one may wish to issue a readb() here, to
498 * flush the mmio write. However, control also passes
499 * to the hardware at this point, and it will interrupt
500 * us when we are to resume control. So, in effect,
501 * we don't care when the mmio write flushes.
502 * Further, a read of the DMA status register _immediately_
503 * following the write may not be what certain flaky hardware
504 * is expected, so I think it is best to not add a readb()
505 * without first all the MMIO ATA cards/mobos.
506 * Or maybe I'm just being paranoid.
511 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
512 * @qc: Info associated with this ATA transaction.
514 * LOCKING:
515 * spin_lock_irqsave(host lock)
518 static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
520 struct ata_port *ap = qc->ap;
521 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
522 u8 dmactl;
524 /* load PRD table addr. */
525 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
527 /* specify data direction, triple-check start bit is clear */
528 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
529 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
530 if (!rw)
531 dmactl |= ATA_DMA_WR;
532 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
534 /* issue r/w command */
535 ap->ops->exec_command(ap, &qc->tf);
539 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
540 * @qc: Info associated with this ATA transaction.
542 * LOCKING:
543 * spin_lock_irqsave(host lock)
546 static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
548 struct ata_port *ap = qc->ap;
549 u8 dmactl;
551 /* start host DMA transaction */
552 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
553 outb(dmactl | ATA_DMA_START,
554 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
559 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
560 * @qc: Info associated with this ATA transaction.
562 * Writes the ATA_DMA_START flag to the DMA command register.
564 * May be used as the bmdma_start() entry in ata_port_operations.
566 * LOCKING:
567 * spin_lock_irqsave(host lock)
569 void ata_bmdma_start(struct ata_queued_cmd *qc)
571 if (qc->ap->flags & ATA_FLAG_MMIO)
572 ata_bmdma_start_mmio(qc);
573 else
574 ata_bmdma_start_pio(qc);
579 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
580 * @qc: Info associated with this ATA transaction.
582 * Writes address of PRD table to device's PRD Table Address
583 * register, sets the DMA control register, and calls
584 * ops->exec_command() to start the transfer.
586 * May be used as the bmdma_setup() entry in ata_port_operations.
588 * LOCKING:
589 * spin_lock_irqsave(host lock)
591 void ata_bmdma_setup(struct ata_queued_cmd *qc)
593 if (qc->ap->flags & ATA_FLAG_MMIO)
594 ata_bmdma_setup_mmio(qc);
595 else
596 ata_bmdma_setup_pio(qc);
601 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
602 * @ap: Port associated with this ATA transaction.
604 * Clear interrupt and error flags in DMA status register.
606 * May be used as the irq_clear() entry in ata_port_operations.
608 * LOCKING:
609 * spin_lock_irqsave(host lock)
612 void ata_bmdma_irq_clear(struct ata_port *ap)
614 if (!ap->ioaddr.bmdma_addr)
615 return;
617 if (ap->flags & ATA_FLAG_MMIO) {
618 void __iomem *mmio =
619 ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
620 writeb(readb(mmio), mmio);
621 } else {
622 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
623 outb(inb(addr), addr);
629 * ata_bmdma_status - Read PCI IDE BMDMA status
630 * @ap: Port associated with this ATA transaction.
632 * Read and return BMDMA status register.
634 * May be used as the bmdma_status() entry in ata_port_operations.
636 * LOCKING:
637 * spin_lock_irqsave(host lock)
640 u8 ata_bmdma_status(struct ata_port *ap)
642 u8 host_stat;
643 if (ap->flags & ATA_FLAG_MMIO) {
644 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
645 host_stat = readb(mmio + ATA_DMA_STATUS);
646 } else
647 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
648 return host_stat;
653 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
654 * @qc: Command we are ending DMA for
656 * Clears the ATA_DMA_START flag in the dma control register
658 * May be used as the bmdma_stop() entry in ata_port_operations.
660 * LOCKING:
661 * spin_lock_irqsave(host lock)
664 void ata_bmdma_stop(struct ata_queued_cmd *qc)
666 struct ata_port *ap = qc->ap;
667 if (ap->flags & ATA_FLAG_MMIO) {
668 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
670 /* clear start/stop bit */
671 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
672 mmio + ATA_DMA_CMD);
673 } else {
674 /* clear start/stop bit */
675 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
676 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
679 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
680 ata_altstatus(ap); /* dummy read */
684 * ata_bmdma_freeze - Freeze BMDMA controller port
685 * @ap: port to freeze
687 * Freeze BMDMA controller port.
689 * LOCKING:
690 * Inherited from caller.
692 void ata_bmdma_freeze(struct ata_port *ap)
694 struct ata_ioports *ioaddr = &ap->ioaddr;
696 ap->ctl |= ATA_NIEN;
697 ap->last_ctl = ap->ctl;
699 if (ap->flags & ATA_FLAG_MMIO)
700 writeb(ap->ctl, (void __iomem *)ioaddr->ctl_addr);
701 else
702 outb(ap->ctl, ioaddr->ctl_addr);
706 * ata_bmdma_thaw - Thaw BMDMA controller port
707 * @ap: port to thaw
709 * Thaw BMDMA controller port.
711 * LOCKING:
712 * Inherited from caller.
714 void ata_bmdma_thaw(struct ata_port *ap)
716 /* clear & re-enable interrupts */
717 ata_chk_status(ap);
718 ap->ops->irq_clear(ap);
719 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
720 ata_irq_on(ap);
724 * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
725 * @ap: port to handle error for
726 * @prereset: prereset method (can be NULL)
727 * @softreset: softreset method (can be NULL)
728 * @hardreset: hardreset method (can be NULL)
729 * @postreset: postreset method (can be NULL)
731 * Handle error for ATA BMDMA controller. It can handle both
732 * PATA and SATA controllers. Many controllers should be able to
733 * use this EH as-is or with some added handling before and
734 * after.
736 * This function is intended to be used for constructing
737 * ->error_handler callback by low level drivers.
739 * LOCKING:
740 * Kernel thread context (may sleep)
742 void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
743 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
744 ata_postreset_fn_t postreset)
746 struct ata_queued_cmd *qc;
747 unsigned long flags;
748 int thaw = 0;
750 qc = __ata_qc_from_tag(ap, ap->active_tag);
751 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
752 qc = NULL;
754 /* reset PIO HSM and stop DMA engine */
755 spin_lock_irqsave(ap->lock, flags);
757 ap->hsm_task_state = HSM_ST_IDLE;
759 if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
760 qc->tf.protocol == ATA_PROT_ATAPI_DMA)) {
761 u8 host_stat;
763 host_stat = ap->ops->bmdma_status(ap);
765 /* BMDMA controllers indicate host bus error by
766 * setting DMA_ERR bit and timing out. As it wasn't
767 * really a timeout event, adjust error mask and
768 * cancel frozen state.
770 if (qc->err_mask == AC_ERR_TIMEOUT && host_stat & ATA_DMA_ERR) {
771 qc->err_mask = AC_ERR_HOST_BUS;
772 thaw = 1;
775 ap->ops->bmdma_stop(qc);
778 ata_altstatus(ap);
779 ata_chk_status(ap);
780 ap->ops->irq_clear(ap);
782 spin_unlock_irqrestore(ap->lock, flags);
784 if (thaw)
785 ata_eh_thaw_port(ap);
787 /* PIO and DMA engines have been stopped, perform recovery */
788 ata_do_eh(ap, prereset, softreset, hardreset, postreset);
792 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
793 * @ap: port to handle error for
795 * Stock error handler for BMDMA controller.
797 * LOCKING:
798 * Kernel thread context (may sleep)
800 void ata_bmdma_error_handler(struct ata_port *ap)
802 ata_reset_fn_t hardreset;
804 hardreset = NULL;
805 if (sata_scr_valid(ap))
806 hardreset = sata_std_hardreset;
808 ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
809 ata_std_postreset);
813 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
814 * BMDMA controller
815 * @qc: internal command to clean up
817 * LOCKING:
818 * Kernel thread context (may sleep)
820 void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
822 ata_bmdma_stop(qc);
825 #ifdef CONFIG_PCI
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]);
849 int p = 0;
850 unsigned long bmdma;
852 if (!probe_ent)
853 return NULL;
855 probe_ent->irq = pdev->irq;
856 probe_ent->irq_flags = IRQF_SHARED;
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);
864 if (bmdma) {
865 if (inb(bmdma + 2) & 0x80)
866 probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
867 probe_ent->port[p].bmdma_addr = bmdma;
869 ata_std_ports(&probe_ent->port[p]);
870 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);
879 if (bmdma) {
880 bmdma += 8;
881 if(inb(bmdma + 2) & 0x80)
882 probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
883 probe_ent->port[p].bmdma_addr = bmdma;
885 ata_std_ports(&probe_ent->port[p]);
886 probe_ent->pinfo2 = port[1];
887 p++;
890 probe_ent->n_ports = p;
891 return probe_ent;
895 static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev,
896 struct ata_port_info **port, int port_mask)
898 struct ata_probe_ent *probe_ent;
899 unsigned long bmdma = pci_resource_start(pdev, 4);
901 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
902 if (!probe_ent)
903 return NULL;
905 probe_ent->n_ports = 2;
906 probe_ent->irq_flags = IRQF_SHARED;
908 if (port_mask & ATA_PORT_PRIMARY) {
909 probe_ent->irq = ATA_PRIMARY_IRQ;
910 probe_ent->port[0].cmd_addr = ATA_PRIMARY_CMD;
911 probe_ent->port[0].altstatus_addr =
912 probe_ent->port[0].ctl_addr = ATA_PRIMARY_CTL;
913 if (bmdma) {
914 probe_ent->port[0].bmdma_addr = bmdma;
915 if (inb(bmdma + 2) & 0x80)
916 probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
918 ata_std_ports(&probe_ent->port[0]);
919 } else
920 probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY;
922 if (port_mask & ATA_PORT_SECONDARY) {
923 if (probe_ent->irq)
924 probe_ent->irq2 = ATA_SECONDARY_IRQ;
925 else
926 probe_ent->irq = ATA_SECONDARY_IRQ;
927 probe_ent->port[1].cmd_addr = ATA_SECONDARY_CMD;
928 probe_ent->port[1].altstatus_addr =
929 probe_ent->port[1].ctl_addr = ATA_SECONDARY_CTL;
930 if (bmdma) {
931 probe_ent->port[1].bmdma_addr = bmdma + 8;
932 if (inb(bmdma + 10) & 0x80)
933 probe_ent->_host_flags |= ATA_HOST_SIMPLEX;
935 ata_std_ports(&probe_ent->port[1]);
937 /* FIXME: could be pointing to stack area; must copy */
938 probe_ent->pinfo2 = port[1];
939 } else
940 probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY;
942 return probe_ent;
947 * ata_pci_init_one - Initialize/register PCI IDE host controller
948 * @pdev: Controller to be initialized
949 * @port_info: Information from low-level host driver
950 * @n_ports: Number of ports attached to host controller
952 * This is a helper function which can be called from a driver's
953 * xxx_init_one() probe function if the hardware uses traditional
954 * IDE taskfile registers.
956 * This function calls pci_enable_device(), reserves its register
957 * regions, sets the dma mask, enables bus master mode, and calls
958 * ata_device_add()
960 * ASSUMPTION:
961 * Nobody makes a single channel controller that appears solely as
962 * the secondary legacy port on PCI.
964 * LOCKING:
965 * Inherited from PCI layer (may sleep).
967 * RETURNS:
968 * Zero on success, negative on errno-based value on error.
971 int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
972 unsigned int n_ports)
974 struct ata_probe_ent *probe_ent = NULL;
975 struct ata_port_info *port[2];
976 u8 mask;
977 unsigned int legacy_mode = 0;
978 int disable_dev_on_err = 1;
979 int rc;
981 DPRINTK("ENTER\n");
983 BUG_ON(n_ports < 1 || n_ports > 2);
985 port[0] = port_info[0];
986 if (n_ports > 1)
987 port[1] = port_info[1];
988 else
989 port[1] = port[0];
991 /* FIXME: Really for ATA it isn't safe because the device may be
992 multi-purpose and we want to leave it alone if it was already
993 enabled. Secondly for shared use as Arjan says we want refcounting
995 Checking dev->is_enabled is insufficient as this is not set at
996 boot for the primary video which is BIOS enabled
999 rc = pci_enable_device(pdev);
1000 if (rc)
1001 return rc;
1003 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
1004 u8 tmp8;
1006 /* TODO: What if one channel is in native mode ... */
1007 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
1008 mask = (1 << 2) | (1 << 0);
1009 if ((tmp8 & mask) != mask)
1010 legacy_mode = (1 << 3);
1011 #if defined(CONFIG_NO_ATA_LEGACY)
1012 /* Some platforms with PCI limits cannot address compat
1013 port space. In that case we punt if their firmware has
1014 left a device in compatibility mode */
1015 if (legacy_mode) {
1016 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
1017 return -EOPNOTSUPP;
1019 #endif
1022 rc = pci_request_regions(pdev, DRV_NAME);
1023 if (rc) {
1024 disable_dev_on_err = 0;
1025 goto err_out;
1028 if (legacy_mode) {
1029 if (!request_region(ATA_PRIMARY_CMD, 8, "libata")) {
1030 struct resource *conflict, res;
1031 res.start = ATA_PRIMARY_CMD;
1032 res.end = ATA_PRIMARY_CMD + 8 - 1;
1033 conflict = ____request_resource(&ioport_resource, &res);
1034 while (conflict->child)
1035 conflict = ____request_resource(conflict, &res);
1036 if (!strcmp(conflict->name, "libata"))
1037 legacy_mode |= ATA_PORT_PRIMARY;
1038 else {
1039 disable_dev_on_err = 0;
1040 printk(KERN_WARNING "ata: 0x%0X IDE port busy\n" \
1041 "ata: conflict with %s\n",
1042 ATA_PRIMARY_CMD,
1043 conflict->name);
1045 } else
1046 legacy_mode |= ATA_PORT_PRIMARY;
1048 if (!request_region(ATA_SECONDARY_CMD, 8, "libata")) {
1049 struct resource *conflict, res;
1050 res.start = ATA_SECONDARY_CMD;
1051 res.end = ATA_SECONDARY_CMD + 8 - 1;
1052 conflict = ____request_resource(&ioport_resource, &res);
1053 while (conflict->child)
1054 conflict = ____request_resource(conflict, &res);
1055 if (!strcmp(conflict->name, "libata"))
1056 legacy_mode |= ATA_PORT_SECONDARY;
1057 else {
1058 disable_dev_on_err = 0;
1059 printk(KERN_WARNING "ata: 0x%X IDE port busy\n" \
1060 "ata: conflict with %s\n",
1061 ATA_SECONDARY_CMD,
1062 conflict->name);
1064 } else
1065 legacy_mode |= ATA_PORT_SECONDARY;
1068 /* we have legacy mode, but all ports are unavailable */
1069 if (legacy_mode == (1 << 3)) {
1070 rc = -EBUSY;
1071 goto err_out_regions;
1074 /* TODO: If we get no DMA mask we should fall back to PIO */
1075 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
1076 if (rc)
1077 goto err_out_regions;
1078 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
1079 if (rc)
1080 goto err_out_regions;
1082 if (legacy_mode) {
1083 probe_ent = ata_pci_init_legacy_port(pdev, port, legacy_mode);
1084 } else {
1085 if (n_ports == 2)
1086 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
1087 else
1088 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
1090 if (!probe_ent) {
1091 rc = -ENOMEM;
1092 goto err_out_regions;
1095 pci_set_master(pdev);
1097 if (!ata_device_add(probe_ent)) {
1098 rc = -ENODEV;
1099 goto err_out_ent;
1102 kfree(probe_ent);
1104 return 0;
1106 err_out_ent:
1107 kfree(probe_ent);
1108 err_out_regions:
1109 if (legacy_mode & ATA_PORT_PRIMARY)
1110 release_region(ATA_PRIMARY_CMD, 8);
1111 if (legacy_mode & ATA_PORT_SECONDARY)
1112 release_region(ATA_SECONDARY_CMD, 8);
1113 pci_release_regions(pdev);
1114 err_out:
1115 if (disable_dev_on_err)
1116 pci_disable_device(pdev);
1117 return rc;
1121 * ata_pci_clear_simplex - attempt to kick device out of simplex
1122 * @pdev: PCI device
1124 * Some PCI ATA devices report simplex mode but in fact can be told to
1125 * enter non simplex mode. This implements the neccessary logic to
1126 * perform the task on such devices. Calling it on other devices will
1127 * have -undefined- behaviour.
1130 int ata_pci_clear_simplex(struct pci_dev *pdev)
1132 unsigned long bmdma = pci_resource_start(pdev, 4);
1133 u8 simplex;
1135 if (bmdma == 0)
1136 return -ENOENT;
1138 simplex = inb(bmdma + 0x02);
1139 outb(simplex & 0x60, bmdma + 0x02);
1140 simplex = inb(bmdma + 0x02);
1141 if (simplex & 0x80)
1142 return -EOPNOTSUPP;
1143 return 0;
1146 unsigned long ata_pci_default_filter(const struct ata_port *ap, struct ata_device *adev, unsigned long xfer_mask)
1148 /* Filter out DMA modes if the device has been configured by
1149 the BIOS as PIO only */
1151 if (ap->ioaddr.bmdma_addr == 0)
1152 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
1153 return xfer_mask;
1156 #endif /* CONFIG_PCI */