Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / ata / libata-sff.c
blobfab8cc6eaf3b06a387deaf4bb925cd2b8a9b08ca
1 /*
2 * libata-sff.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 <<<<<<< HEAD:drivers/ata/libata-sff.c
60 iowrite8(ap->ctl, ioaddr->ctl_addr);
61 =======
62 if (ioaddr->ctl_addr)
63 iowrite8(ap->ctl, ioaddr->ctl_addr);
64 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-sff.c
65 tmp = ata_wait_idle(ap);
67 ap->ops->irq_clear(ap);
69 return tmp;
72 /**
73 * ata_tf_load - send taskfile registers to host controller
74 * @ap: Port to which output is sent
75 * @tf: ATA taskfile register set
77 * Outputs ATA taskfile to standard ATA host controller.
79 * LOCKING:
80 * Inherited from caller.
83 void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
85 struct ata_ioports *ioaddr = &ap->ioaddr;
86 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
88 if (tf->ctl != ap->last_ctl) {
89 <<<<<<< HEAD:drivers/ata/libata-sff.c
90 iowrite8(tf->ctl, ioaddr->ctl_addr);
91 =======
92 if (ioaddr->ctl_addr)
93 iowrite8(tf->ctl, ioaddr->ctl_addr);
94 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-sff.c
95 ap->last_ctl = tf->ctl;
96 ata_wait_idle(ap);
99 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
100 <<<<<<< HEAD:drivers/ata/libata-sff.c
101 =======
102 WARN_ON(!ioaddr->ctl_addr);
103 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-sff.c
104 iowrite8(tf->hob_feature, ioaddr->feature_addr);
105 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
106 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
107 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
108 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
109 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
110 tf->hob_feature,
111 tf->hob_nsect,
112 tf->hob_lbal,
113 tf->hob_lbam,
114 tf->hob_lbah);
117 if (is_addr) {
118 iowrite8(tf->feature, ioaddr->feature_addr);
119 iowrite8(tf->nsect, ioaddr->nsect_addr);
120 iowrite8(tf->lbal, ioaddr->lbal_addr);
121 iowrite8(tf->lbam, ioaddr->lbam_addr);
122 iowrite8(tf->lbah, ioaddr->lbah_addr);
123 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
124 tf->feature,
125 tf->nsect,
126 tf->lbal,
127 tf->lbam,
128 tf->lbah);
131 if (tf->flags & ATA_TFLAG_DEVICE) {
132 iowrite8(tf->device, ioaddr->device_addr);
133 VPRINTK("device 0x%X\n", tf->device);
136 ata_wait_idle(ap);
140 * ata_exec_command - issue ATA command to host controller
141 * @ap: port to which command is being issued
142 * @tf: ATA taskfile register set
144 * Issues ATA command, with proper synchronization with interrupt
145 * handler / other threads.
147 * LOCKING:
148 * spin_lock_irqsave(host lock)
150 void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
152 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
154 iowrite8(tf->command, ap->ioaddr.command_addr);
155 ata_pause(ap);
159 * ata_tf_read - input device's ATA taskfile shadow registers
160 * @ap: Port from which input is read
161 * @tf: ATA taskfile register set for storing input
163 * Reads ATA taskfile registers for currently-selected device
164 * into @tf. Assumes the device has a fully SFF compliant task file
165 * layout and behaviour. If you device does not (eg has a different
166 * status method) then you will need to provide a replacement tf_read
168 * LOCKING:
169 * Inherited from caller.
171 void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
173 struct ata_ioports *ioaddr = &ap->ioaddr;
175 tf->command = ata_check_status(ap);
176 tf->feature = ioread8(ioaddr->error_addr);
177 tf->nsect = ioread8(ioaddr->nsect_addr);
178 tf->lbal = ioread8(ioaddr->lbal_addr);
179 tf->lbam = ioread8(ioaddr->lbam_addr);
180 tf->lbah = ioread8(ioaddr->lbah_addr);
181 tf->device = ioread8(ioaddr->device_addr);
183 if (tf->flags & ATA_TFLAG_LBA48) {
184 <<<<<<< HEAD:drivers/ata/libata-sff.c
185 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
186 tf->hob_feature = ioread8(ioaddr->error_addr);
187 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
188 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
189 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
190 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
191 iowrite8(tf->ctl, ioaddr->ctl_addr);
192 ap->last_ctl = tf->ctl;
193 =======
194 if (likely(ioaddr->ctl_addr)) {
195 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
196 tf->hob_feature = ioread8(ioaddr->error_addr);
197 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
198 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
199 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
200 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
201 iowrite8(tf->ctl, ioaddr->ctl_addr);
202 ap->last_ctl = tf->ctl;
203 } else
204 WARN_ON(1);
205 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-sff.c
210 * ata_check_status - Read device status reg & clear interrupt
211 * @ap: port where the device is
213 * Reads ATA taskfile status register for currently-selected device
214 * and return its value. This also clears pending interrupts
215 * from this device
217 * LOCKING:
218 * Inherited from caller.
220 u8 ata_check_status(struct ata_port *ap)
222 return ioread8(ap->ioaddr.status_addr);
226 * ata_altstatus - Read device alternate status reg
227 * @ap: port where the device is
229 * Reads ATA taskfile alternate status register for
230 * currently-selected device and return its value.
232 * Note: may NOT be used as the check_altstatus() entry in
233 * ata_port_operations.
235 * LOCKING:
236 * Inherited from caller.
238 u8 ata_altstatus(struct ata_port *ap)
240 if (ap->ops->check_altstatus)
241 return ap->ops->check_altstatus(ap);
243 return ioread8(ap->ioaddr.altstatus_addr);
247 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
248 * @qc: Info associated with this ATA transaction.
250 * LOCKING:
251 * spin_lock_irqsave(host lock)
253 void ata_bmdma_setup(struct ata_queued_cmd *qc)
255 struct ata_port *ap = qc->ap;
256 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
257 u8 dmactl;
259 /* load PRD table addr. */
260 mb(); /* make sure PRD table writes are visible to controller */
261 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
263 /* specify data direction, triple-check start bit is clear */
264 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
265 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
266 if (!rw)
267 dmactl |= ATA_DMA_WR;
268 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
270 /* issue r/w command */
271 ap->ops->exec_command(ap, &qc->tf);
275 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
276 * @qc: Info associated with this ATA transaction.
278 * LOCKING:
279 * spin_lock_irqsave(host lock)
281 void ata_bmdma_start(struct ata_queued_cmd *qc)
283 struct ata_port *ap = qc->ap;
284 u8 dmactl;
286 /* start host DMA transaction */
287 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
288 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
290 /* Strictly, one may wish to issue an ioread8() here, to
291 * flush the mmio write. However, control also passes
292 * to the hardware at this point, and it will interrupt
293 * us when we are to resume control. So, in effect,
294 * we don't care when the mmio write flushes.
295 * Further, a read of the DMA status register _immediately_
296 * following the write may not be what certain flaky hardware
297 * is expected, so I think it is best to not add a readb()
298 * without first all the MMIO ATA cards/mobos.
299 * Or maybe I'm just being paranoid.
301 * FIXME: The posting of this write means I/O starts are
302 * unneccessarily delayed for MMIO
307 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
308 * @ap: Port associated with this ATA transaction.
310 * Clear interrupt and error flags in DMA status register.
312 * May be used as the irq_clear() entry in ata_port_operations.
314 * LOCKING:
315 * spin_lock_irqsave(host lock)
317 void ata_bmdma_irq_clear(struct ata_port *ap)
319 void __iomem *mmio = ap->ioaddr.bmdma_addr;
321 if (!mmio)
322 return;
324 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
328 * ata_bmdma_status - Read PCI IDE BMDMA status
329 * @ap: Port associated with this ATA transaction.
331 * Read and return BMDMA status register.
333 * May be used as the bmdma_status() entry in ata_port_operations.
335 * LOCKING:
336 * spin_lock_irqsave(host lock)
338 u8 ata_bmdma_status(struct ata_port *ap)
340 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
344 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
345 * @qc: Command we are ending DMA for
347 * Clears the ATA_DMA_START flag in the dma control register
349 * May be used as the bmdma_stop() entry in ata_port_operations.
351 * LOCKING:
352 * spin_lock_irqsave(host lock)
354 void ata_bmdma_stop(struct ata_queued_cmd *qc)
356 struct ata_port *ap = qc->ap;
357 void __iomem *mmio = ap->ioaddr.bmdma_addr;
359 /* clear start/stop bit */
360 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
361 mmio + ATA_DMA_CMD);
363 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
364 ata_altstatus(ap); /* dummy read */
368 * ata_bmdma_freeze - Freeze BMDMA controller port
369 * @ap: port to freeze
371 * Freeze BMDMA controller port.
373 * LOCKING:
374 * Inherited from caller.
376 void ata_bmdma_freeze(struct ata_port *ap)
378 struct ata_ioports *ioaddr = &ap->ioaddr;
380 ap->ctl |= ATA_NIEN;
381 ap->last_ctl = ap->ctl;
383 <<<<<<< HEAD:drivers/ata/libata-sff.c
384 iowrite8(ap->ctl, ioaddr->ctl_addr);
385 =======
386 if (ioaddr->ctl_addr)
387 iowrite8(ap->ctl, ioaddr->ctl_addr);
388 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-sff.c
390 /* Under certain circumstances, some controllers raise IRQ on
391 * ATA_NIEN manipulation. Also, many controllers fail to mask
392 * previously pending IRQ on ATA_NIEN assertion. Clear it.
394 ata_chk_status(ap);
396 ap->ops->irq_clear(ap);
400 * ata_bmdma_thaw - Thaw BMDMA controller port
401 * @ap: port to thaw
403 * Thaw BMDMA controller port.
405 * LOCKING:
406 * Inherited from caller.
408 void ata_bmdma_thaw(struct ata_port *ap)
410 /* clear & re-enable interrupts */
411 ata_chk_status(ap);
412 ap->ops->irq_clear(ap);
413 ap->ops->irq_on(ap);
417 * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
418 * @ap: port to handle error for
419 * @prereset: prereset method (can be NULL)
420 * @softreset: softreset method (can be NULL)
421 * @hardreset: hardreset method (can be NULL)
422 * @postreset: postreset method (can be NULL)
424 * Handle error for ATA BMDMA controller. It can handle both
425 * PATA and SATA controllers. Many controllers should be able to
426 * use this EH as-is or with some added handling before and
427 * after.
429 * This function is intended to be used for constructing
430 * ->error_handler callback by low level drivers.
432 * LOCKING:
433 * Kernel thread context (may sleep)
435 void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
436 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
437 ata_postreset_fn_t postreset)
439 struct ata_queued_cmd *qc;
440 unsigned long flags;
441 int thaw = 0;
443 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
444 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
445 qc = NULL;
447 /* reset PIO HSM and stop DMA engine */
448 spin_lock_irqsave(ap->lock, flags);
450 ap->hsm_task_state = HSM_ST_IDLE;
452 if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
453 qc->tf.protocol == ATAPI_PROT_DMA)) {
454 u8 host_stat;
456 host_stat = ap->ops->bmdma_status(ap);
458 /* BMDMA controllers indicate host bus error by
459 * setting DMA_ERR bit and timing out. As it wasn't
460 * really a timeout event, adjust error mask and
461 * cancel frozen state.
463 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
464 qc->err_mask = AC_ERR_HOST_BUS;
465 thaw = 1;
468 ap->ops->bmdma_stop(qc);
471 ata_altstatus(ap);
472 ata_chk_status(ap);
473 ap->ops->irq_clear(ap);
475 spin_unlock_irqrestore(ap->lock, flags);
477 if (thaw)
478 ata_eh_thaw_port(ap);
480 /* PIO and DMA engines have been stopped, perform recovery */
481 ata_do_eh(ap, prereset, softreset, hardreset, postreset);
485 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
486 * @ap: port to handle error for
488 * Stock error handler for BMDMA controller.
490 * LOCKING:
491 * Kernel thread context (may sleep)
493 void ata_bmdma_error_handler(struct ata_port *ap)
495 <<<<<<< HEAD:drivers/ata/libata-sff.c
496 ata_reset_fn_t hardreset;
497 =======
498 ata_reset_fn_t softreset = NULL, hardreset = NULL;
499 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-sff.c
501 <<<<<<< HEAD:drivers/ata/libata-sff.c
502 hardreset = NULL;
503 =======
504 if (ap->ioaddr.ctl_addr)
505 softreset = ata_std_softreset;
506 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-sff.c
507 if (sata_scr_valid(&ap->link))
508 hardreset = sata_std_hardreset;
510 <<<<<<< HEAD:drivers/ata/libata-sff.c
511 ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
512 =======
513 ata_bmdma_drive_eh(ap, ata_std_prereset, softreset, hardreset,
514 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-sff.c
515 ata_std_postreset);
519 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
520 * BMDMA controller
521 * @qc: internal command to clean up
523 * LOCKING:
524 * Kernel thread context (may sleep)
526 void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
528 if (qc->ap->ioaddr.bmdma_addr)
529 ata_bmdma_stop(qc);
533 * ata_sff_port_start - Set port up for dma.
534 * @ap: Port to initialize
536 * Called just after data structures for each port are
537 * initialized. Allocates space for PRD table if the device
538 * is DMA capable SFF.
540 * May be used as the port_start() entry in ata_port_operations.
542 * LOCKING:
543 * Inherited from caller.
546 int ata_sff_port_start(struct ata_port *ap)
548 if (ap->ioaddr.bmdma_addr)
549 return ata_port_start(ap);
550 return 0;
553 #ifdef CONFIG_PCI
555 static int ata_resources_present(struct pci_dev *pdev, int port)
557 int i;
559 /* Check the PCI resources for this channel are enabled */
560 port = port * 2;
561 for (i = 0; i < 2; i ++) {
562 if (pci_resource_start(pdev, port + i) == 0 ||
563 pci_resource_len(pdev, port + i) == 0)
564 return 0;
566 return 1;
570 * ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host
571 * @host: target ATA host
573 * Acquire PCI BMDMA resources and initialize @host accordingly.
575 * LOCKING:
576 * Inherited from calling layer (may sleep).
578 * RETURNS:
579 * 0 on success, -errno otherwise.
581 int ata_pci_init_bmdma(struct ata_host *host)
583 struct device *gdev = host->dev;
584 struct pci_dev *pdev = to_pci_dev(gdev);
585 int i, rc;
587 /* No BAR4 allocation: No DMA */
588 if (pci_resource_start(pdev, 4) == 0)
589 return 0;
591 /* TODO: If we get no DMA mask we should fall back to PIO */
592 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
593 if (rc)
594 return rc;
595 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
596 if (rc)
597 return rc;
599 /* request and iomap DMA region */
600 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
601 if (rc) {
602 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
603 return -ENOMEM;
605 host->iomap = pcim_iomap_table(pdev);
607 for (i = 0; i < 2; i++) {
608 struct ata_port *ap = host->ports[i];
609 void __iomem *bmdma = host->iomap[4] + 8 * i;
611 if (ata_port_is_dummy(ap))
612 continue;
614 ap->ioaddr.bmdma_addr = bmdma;
615 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
616 (ioread8(bmdma + 2) & 0x80))
617 host->flags |= ATA_HOST_SIMPLEX;
619 ata_port_desc(ap, "bmdma 0x%llx",
620 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
623 return 0;
627 * ata_pci_init_sff_host - acquire native PCI ATA resources and init host
628 * @host: target ATA host
630 * Acquire native PCI ATA resources for @host and initialize the
631 * first two ports of @host accordingly. Ports marked dummy are
632 * skipped and allocation failure makes the port dummy.
634 * Note that native PCI resources are valid even for legacy hosts
635 * as we fix up pdev resources array early in boot, so this
636 * function can be used for both native and legacy SFF hosts.
638 * LOCKING:
639 * Inherited from calling layer (may sleep).
641 * RETURNS:
642 * 0 if at least one port is initialized, -ENODEV if no port is
643 * available.
645 int ata_pci_init_sff_host(struct ata_host *host)
647 struct device *gdev = host->dev;
648 struct pci_dev *pdev = to_pci_dev(gdev);
649 unsigned int mask = 0;
650 int i, rc;
652 /* request, iomap BARs and init port addresses accordingly */
653 for (i = 0; i < 2; i++) {
654 struct ata_port *ap = host->ports[i];
655 int base = i * 2;
656 void __iomem * const *iomap;
658 if (ata_port_is_dummy(ap))
659 continue;
661 /* Discard disabled ports. Some controllers show
662 * their unused channels this way. Disabled ports are
663 * made dummy.
665 if (!ata_resources_present(pdev, i)) {
666 ap->ops = &ata_dummy_port_ops;
667 continue;
670 rc = pcim_iomap_regions(pdev, 0x3 << base,
671 dev_driver_string(gdev));
672 if (rc) {
673 dev_printk(KERN_WARNING, gdev,
674 "failed to request/iomap BARs for port %d "
675 "(errno=%d)\n", i, rc);
676 if (rc == -EBUSY)
677 pcim_pin_device(pdev);
678 ap->ops = &ata_dummy_port_ops;
679 continue;
681 host->iomap = iomap = pcim_iomap_table(pdev);
683 ap->ioaddr.cmd_addr = iomap[base];
684 ap->ioaddr.altstatus_addr =
685 ap->ioaddr.ctl_addr = (void __iomem *)
686 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
687 ata_std_ports(&ap->ioaddr);
689 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
690 (unsigned long long)pci_resource_start(pdev, base),
691 (unsigned long long)pci_resource_start(pdev, base + 1));
693 mask |= 1 << i;
696 if (!mask) {
697 dev_printk(KERN_ERR, gdev, "no available native port\n");
698 return -ENODEV;
701 return 0;
705 * ata_pci_prepare_sff_host - helper to prepare native PCI ATA host
706 * @pdev: target PCI device
707 * @ppi: array of port_info, must be enough for two ports
708 * @r_host: out argument for the initialized ATA host
710 * Helper to allocate ATA host for @pdev, acquire all native PCI
711 * resources and initialize it accordingly in one go.
713 * LOCKING:
714 * Inherited from calling layer (may sleep).
716 * RETURNS:
717 * 0 on success, -errno otherwise.
719 int ata_pci_prepare_sff_host(struct pci_dev *pdev,
720 const struct ata_port_info * const * ppi,
721 struct ata_host **r_host)
723 struct ata_host *host;
724 int rc;
726 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
727 return -ENOMEM;
729 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
730 if (!host) {
731 dev_printk(KERN_ERR, &pdev->dev,
732 "failed to allocate ATA host\n");
733 rc = -ENOMEM;
734 goto err_out;
737 rc = ata_pci_init_sff_host(host);
738 if (rc)
739 goto err_out;
741 /* init DMA related stuff */
742 rc = ata_pci_init_bmdma(host);
743 if (rc)
744 goto err_bmdma;
746 devres_remove_group(&pdev->dev, NULL);
747 *r_host = host;
748 return 0;
750 err_bmdma:
751 /* This is necessary because PCI and iomap resources are
752 * merged and releasing the top group won't release the
753 * acquired resources if some of those have been acquired
754 * before entering this function.
756 pcim_iounmap_regions(pdev, 0xf);
757 err_out:
758 devres_release_group(&pdev->dev, NULL);
759 return rc;
763 * ata_pci_activate_sff_host - start SFF host, request IRQ and register it
764 * @host: target SFF ATA host
765 * @irq_handler: irq_handler used when requesting IRQ(s)
766 * @sht: scsi_host_template to use when registering the host
768 * This is the counterpart of ata_host_activate() for SFF ATA
769 * hosts. This separate helper is necessary because SFF hosts
770 * use two separate interrupts in legacy mode.
772 * LOCKING:
773 * Inherited from calling layer (may sleep).
775 * RETURNS:
776 * 0 on success, -errno otherwise.
778 int ata_pci_activate_sff_host(struct ata_host *host,
779 irq_handler_t irq_handler,
780 struct scsi_host_template *sht)
782 struct device *dev = host->dev;
783 struct pci_dev *pdev = to_pci_dev(dev);
784 const char *drv_name = dev_driver_string(host->dev);
785 int legacy_mode = 0, rc;
787 rc = ata_host_start(host);
788 if (rc)
789 return rc;
791 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
792 u8 tmp8, mask;
794 /* TODO: What if one channel is in native mode ... */
795 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
796 mask = (1 << 2) | (1 << 0);
797 if ((tmp8 & mask) != mask)
798 legacy_mode = 1;
799 #if defined(CONFIG_NO_ATA_LEGACY)
800 /* Some platforms with PCI limits cannot address compat
801 port space. In that case we punt if their firmware has
802 left a device in compatibility mode */
803 if (legacy_mode) {
804 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
805 return -EOPNOTSUPP;
807 #endif
810 if (!devres_open_group(dev, NULL, GFP_KERNEL))
811 return -ENOMEM;
813 if (!legacy_mode && pdev->irq) {
814 rc = devm_request_irq(dev, pdev->irq, irq_handler,
815 IRQF_SHARED, drv_name, host);
816 if (rc)
817 goto out;
819 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
820 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
821 } else if (legacy_mode) {
822 if (!ata_port_is_dummy(host->ports[0])) {
823 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
824 irq_handler, IRQF_SHARED,
825 drv_name, host);
826 if (rc)
827 goto out;
829 ata_port_desc(host->ports[0], "irq %d",
830 ATA_PRIMARY_IRQ(pdev));
833 if (!ata_port_is_dummy(host->ports[1])) {
834 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
835 irq_handler, IRQF_SHARED,
836 drv_name, host);
837 if (rc)
838 goto out;
840 ata_port_desc(host->ports[1], "irq %d",
841 ATA_SECONDARY_IRQ(pdev));
845 rc = ata_host_register(host, sht);
846 out:
847 if (rc == 0)
848 devres_remove_group(dev, NULL);
849 else
850 devres_release_group(dev, NULL);
852 return rc;
856 * ata_pci_init_one - Initialize/register PCI IDE host controller
857 * @pdev: Controller to be initialized
858 * @ppi: array of port_info, must be enough for two ports
860 * This is a helper function which can be called from a driver's
861 * xxx_init_one() probe function if the hardware uses traditional
862 * IDE taskfile registers.
864 * This function calls pci_enable_device(), reserves its register
865 * regions, sets the dma mask, enables bus master mode, and calls
866 * ata_device_add()
868 * ASSUMPTION:
869 * Nobody makes a single channel controller that appears solely as
870 * the secondary legacy port on PCI.
872 * LOCKING:
873 * Inherited from PCI layer (may sleep).
875 * RETURNS:
876 * Zero on success, negative on errno-based value on error.
878 int ata_pci_init_one(struct pci_dev *pdev,
879 const struct ata_port_info * const * ppi)
881 struct device *dev = &pdev->dev;
882 const struct ata_port_info *pi = NULL;
883 struct ata_host *host = NULL;
884 int i, rc;
886 DPRINTK("ENTER\n");
888 /* look up the first valid port_info */
889 for (i = 0; i < 2 && ppi[i]; i++) {
890 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
891 pi = ppi[i];
892 break;
896 if (!pi) {
897 dev_printk(KERN_ERR, &pdev->dev,
898 "no valid port_info specified\n");
899 return -EINVAL;
902 if (!devres_open_group(dev, NULL, GFP_KERNEL))
903 return -ENOMEM;
905 rc = pcim_enable_device(pdev);
906 if (rc)
907 goto out;
909 /* prepare and activate SFF host */
910 rc = ata_pci_prepare_sff_host(pdev, ppi, &host);
911 if (rc)
912 goto out;
914 pci_set_master(pdev);
915 rc = ata_pci_activate_sff_host(host, pi->port_ops->irq_handler,
916 pi->sht);
917 out:
918 if (rc == 0)
919 devres_remove_group(&pdev->dev, NULL);
920 else
921 devres_release_group(&pdev->dev, NULL);
923 return rc;
927 * ata_pci_clear_simplex - attempt to kick device out of simplex
928 * @pdev: PCI device
930 * Some PCI ATA devices report simplex mode but in fact can be told to
931 * enter non simplex mode. This implements the necessary logic to
932 * perform the task on such devices. Calling it on other devices will
933 * have -undefined- behaviour.
936 int ata_pci_clear_simplex(struct pci_dev *pdev)
938 unsigned long bmdma = pci_resource_start(pdev, 4);
939 u8 simplex;
941 if (bmdma == 0)
942 return -ENOENT;
944 simplex = inb(bmdma + 0x02);
945 outb(simplex & 0x60, bmdma + 0x02);
946 simplex = inb(bmdma + 0x02);
947 if (simplex & 0x80)
948 return -EOPNOTSUPP;
949 return 0;
952 unsigned long ata_pci_default_filter(struct ata_device *adev, unsigned long xfer_mask)
954 /* Filter out DMA modes if the device has been configured by
955 the BIOS as PIO only */
957 if (adev->link->ap->ioaddr.bmdma_addr == NULL)
958 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
959 return xfer_mask;
962 #endif /* CONFIG_PCI */