blob: ce84805ba5f14e80093efa1056ddd3a18de36f20 [file] [log] [blame]
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001/*
2 * libata-bmdma.c - helper library for PCI IDE BMDMA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2006 Jeff Garzik
10 *
11 *
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.
16 *
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.
21 *
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.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
33 */
34
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050035#include <linux/kernel.h>
36#include <linux/pci.h>
37#include <linux/libata.h>
38
39#include "libata.h"
40
41/**
Tejun Heo90088bb2006-10-09 11:10:26 +090042 * ata_irq_on - Enable interrupts on a port.
43 * @ap: Port on which interrupts are enabled.
44 *
45 * Enable interrupts on a legacy IDE device using MMIO or PIO,
46 * wait for idle, clear any pending interrupts.
47 *
48 * LOCKING:
49 * Inherited from caller.
50 */
51u8 ata_irq_on(struct ata_port *ap)
52{
53 struct ata_ioports *ioaddr = &ap->ioaddr;
54 u8 tmp;
55
56 ap->ctl &= ~ATA_NIEN;
57 ap->last_ctl = ap->ctl;
58
Tejun Heo0d5ff562007-02-01 15:06:36 +090059 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo90088bb2006-10-09 11:10:26 +090060 tmp = ata_wait_idle(ap);
61
62 ap->ops->irq_clear(ap);
63
64 return tmp;
65}
66
Akira Iguchi83625002007-01-26 16:27:32 +090067u8 ata_dummy_irq_on (struct ata_port *ap) { return 0; }
68
69/**
70 * ata_irq_ack - Acknowledge a device interrupt.
71 * @ap: Port on which interrupts are enabled.
72 *
73 * Wait up to 10 ms for legacy IDE device to become idle (BUSY
74 * or BUSY+DRQ clear). Obtain dma status and port status from
75 * device. Clear the interrupt. Return port status.
76 *
77 * LOCKING:
78 */
79
80u8 ata_irq_ack(struct ata_port *ap, unsigned int chk_drq)
81{
82 unsigned int bits = chk_drq ? ATA_BUSY | ATA_DRQ : ATA_BUSY;
Alan Coxd92e74d2007-06-07 16:19:15 +010083 u8 host_stat = 0, post_stat = 0, status;
Akira Iguchi83625002007-01-26 16:27:32 +090084
85 status = ata_busy_wait(ap, bits, 1000);
86 if (status & bits)
87 if (ata_msg_err(ap))
88 printk(KERN_ERR "abnormal status 0x%X\n", status);
89
Alan Coxd92e74d2007-06-07 16:19:15 +010090 if (ap->ioaddr.bmdma_addr) {
91 /* get controller status; clear intr, err bits */
92 host_stat = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
93 iowrite8(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
94 ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Akira Iguchi83625002007-01-26 16:27:32 +090095
Alan Coxd92e74d2007-06-07 16:19:15 +010096 post_stat = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
97 }
Akira Iguchi83625002007-01-26 16:27:32 +090098 if (ata_msg_intr(ap))
99 printk(KERN_INFO "%s: irq ack: host_stat 0x%X, new host_stat 0x%X, drv_stat 0x%X\n",
100 __FUNCTION__,
101 host_stat, post_stat, status);
Akira Iguchi83625002007-01-26 16:27:32 +0900102 return status;
103}
104
105u8 ata_dummy_irq_ack(struct ata_port *ap, unsigned int chk_drq) { return 0; }
106
Tejun Heo90088bb2006-10-09 11:10:26 +0900107/**
Tejun Heo0d5ff562007-02-01 15:06:36 +0900108 * ata_tf_load - send taskfile registers to host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500109 * @ap: Port to which output is sent
110 * @tf: ATA taskfile register set
111 *
112 * Outputs ATA taskfile to standard ATA host controller.
113 *
114 * LOCKING:
115 * Inherited from caller.
116 */
117
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500118void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
119{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900120 struct ata_ioports *ioaddr = &ap->ioaddr;
121 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
122
123 if (tf->ctl != ap->last_ctl) {
124 iowrite8(tf->ctl, ioaddr->ctl_addr);
125 ap->last_ctl = tf->ctl;
126 ata_wait_idle(ap);
127 }
128
129 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
130 iowrite8(tf->hob_feature, ioaddr->feature_addr);
131 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
132 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
133 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
134 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
135 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
136 tf->hob_feature,
137 tf->hob_nsect,
138 tf->hob_lbal,
139 tf->hob_lbam,
140 tf->hob_lbah);
141 }
142
143 if (is_addr) {
144 iowrite8(tf->feature, ioaddr->feature_addr);
145 iowrite8(tf->nsect, ioaddr->nsect_addr);
146 iowrite8(tf->lbal, ioaddr->lbal_addr);
147 iowrite8(tf->lbam, ioaddr->lbam_addr);
148 iowrite8(tf->lbah, ioaddr->lbah_addr);
149 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
150 tf->feature,
151 tf->nsect,
152 tf->lbal,
153 tf->lbam,
154 tf->lbah);
155 }
156
157 if (tf->flags & ATA_TFLAG_DEVICE) {
158 iowrite8(tf->device, ioaddr->device_addr);
159 VPRINTK("device 0x%X\n", tf->device);
160 }
161
162 ata_wait_idle(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500163}
164
165/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500166 * ata_exec_command - issue ATA command to host controller
167 * @ap: port to which command is being issued
168 * @tf: ATA taskfile register set
169 *
Tejun Heo0d5ff562007-02-01 15:06:36 +0900170 * Issues ATA command, with proper synchronization with interrupt
171 * handler / other threads.
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500172 *
173 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400174 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500175 */
176void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
177{
Tejun Heo44877b42007-02-21 01:06:51 +0900178 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900179
180 iowrite8(tf->command, ap->ioaddr.command_addr);
181 ata_pause(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500182}
183
184/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500185 * ata_tf_read - input device's ATA taskfile shadow registers
186 * @ap: Port from which input is read
187 * @tf: ATA taskfile register set for storing input
188 *
189 * Reads ATA taskfile registers for currently-selected device
190 * into @tf.
191 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500192 * LOCKING:
193 * Inherited from caller.
194 */
195void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
196{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900197 struct ata_ioports *ioaddr = &ap->ioaddr;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500198
Tejun Heo0d5ff562007-02-01 15:06:36 +0900199 tf->command = ata_check_status(ap);
200 tf->feature = ioread8(ioaddr->error_addr);
201 tf->nsect = ioread8(ioaddr->nsect_addr);
202 tf->lbal = ioread8(ioaddr->lbal_addr);
203 tf->lbam = ioread8(ioaddr->lbam_addr);
204 tf->lbah = ioread8(ioaddr->lbah_addr);
205 tf->device = ioread8(ioaddr->device_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500206
Tejun Heo0d5ff562007-02-01 15:06:36 +0900207 if (tf->flags & ATA_TFLAG_LBA48) {
208 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
209 tf->hob_feature = ioread8(ioaddr->error_addr);
210 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
211 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
212 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
213 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
214 }
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500215}
216
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500217/**
218 * ata_check_status - Read device status reg & clear interrupt
219 * @ap: port where the device is
220 *
221 * Reads ATA taskfile status register for currently-selected device
222 * and return its value. This also clears pending interrupts
223 * from this device
224 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500225 * LOCKING:
226 * Inherited from caller.
227 */
228u8 ata_check_status(struct ata_port *ap)
229{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900230 return ioread8(ap->ioaddr.status_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500231}
232
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500233/**
234 * ata_altstatus - Read device alternate status reg
235 * @ap: port where the device is
236 *
237 * Reads ATA taskfile alternate status register for
238 * currently-selected device and return its value.
239 *
240 * Note: may NOT be used as the check_altstatus() entry in
241 * ata_port_operations.
242 *
243 * LOCKING:
244 * Inherited from caller.
245 */
246u8 ata_altstatus(struct ata_port *ap)
247{
248 if (ap->ops->check_altstatus)
249 return ap->ops->check_altstatus(ap);
250
Tejun Heo0d5ff562007-02-01 15:06:36 +0900251 return ioread8(ap->ioaddr.altstatus_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500252}
253
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500254/**
Tejun Heo0d5ff562007-02-01 15:06:36 +0900255 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500256 * @qc: Info associated with this ATA transaction.
257 *
258 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400259 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500260 */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900261void ata_bmdma_setup(struct ata_queued_cmd *qc)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500262{
263 struct ata_port *ap = qc->ap;
264 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
265 u8 dmactl;
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500266
267 /* load PRD table addr. */
268 mb(); /* make sure PRD table writes are visible to controller */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900269 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500270
271 /* specify data direction, triple-check start bit is clear */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900272 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500273 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
274 if (!rw)
275 dmactl |= ATA_DMA_WR;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900276 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500277
278 /* issue r/w command */
279 ap->ops->exec_command(ap, &qc->tf);
280}
281
282/**
Tejun Heo0d5ff562007-02-01 15:06:36 +0900283 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500284 * @qc: Info associated with this ATA transaction.
285 *
286 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400287 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500288 */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900289void ata_bmdma_start (struct ata_queued_cmd *qc)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500290{
291 struct ata_port *ap = qc->ap;
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500292 u8 dmactl;
293
294 /* start host DMA transaction */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900295 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
296 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500297
298 /* Strictly, one may wish to issue a readb() here, to
299 * flush the mmio write. However, control also passes
300 * to the hardware at this point, and it will interrupt
301 * us when we are to resume control. So, in effect,
302 * we don't care when the mmio write flushes.
303 * Further, a read of the DMA status register _immediately_
304 * following the write may not be what certain flaky hardware
305 * is expected, so I think it is best to not add a readb()
306 * without first all the MMIO ATA cards/mobos.
307 * Or maybe I'm just being paranoid.
308 */
309}
310
311/**
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500312 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
313 * @ap: Port associated with this ATA transaction.
314 *
315 * Clear interrupt and error flags in DMA status register.
316 *
317 * May be used as the irq_clear() entry in ata_port_operations.
318 *
319 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400320 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500321 */
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500322void ata_bmdma_irq_clear(struct ata_port *ap)
323{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900324 void __iomem *mmio = ap->ioaddr.bmdma_addr;
325
326 if (!mmio)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500327 return;
328
Tejun Heo0d5ff562007-02-01 15:06:36 +0900329 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500330}
331
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500332/**
333 * ata_bmdma_status - Read PCI IDE BMDMA status
334 * @ap: Port associated with this ATA transaction.
335 *
336 * Read and return BMDMA status register.
337 *
338 * May be used as the bmdma_status() entry in ata_port_operations.
339 *
340 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400341 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500342 */
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500343u8 ata_bmdma_status(struct ata_port *ap)
344{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900345 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500346}
347
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500348/**
349 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
350 * @qc: Command we are ending DMA for
351 *
352 * Clears the ATA_DMA_START flag in the dma control register
353 *
354 * May be used as the bmdma_stop() entry in ata_port_operations.
355 *
356 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400357 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500358 */
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500359void ata_bmdma_stop(struct ata_queued_cmd *qc)
360{
361 struct ata_port *ap = qc->ap;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900362 void __iomem *mmio = ap->ioaddr.bmdma_addr;
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500363
Tejun Heo0d5ff562007-02-01 15:06:36 +0900364 /* clear start/stop bit */
365 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
366 mmio + ATA_DMA_CMD);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500367
368 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
369 ata_altstatus(ap); /* dummy read */
370}
371
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900372/**
373 * ata_bmdma_freeze - Freeze BMDMA controller port
374 * @ap: port to freeze
375 *
376 * Freeze BMDMA controller port.
377 *
378 * LOCKING:
379 * Inherited from caller.
380 */
381void ata_bmdma_freeze(struct ata_port *ap)
382{
383 struct ata_ioports *ioaddr = &ap->ioaddr;
384
385 ap->ctl |= ATA_NIEN;
386 ap->last_ctl = ap->ctl;
387
Tejun Heo0d5ff562007-02-01 15:06:36 +0900388 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +0900389
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.
393 */
394 ata_chk_status(ap);
395
396 ap->ops->irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900397}
398
399/**
400 * ata_bmdma_thaw - Thaw BMDMA controller port
401 * @ap: port to thaw
402 *
403 * Thaw BMDMA controller port.
404 *
405 * LOCKING:
406 * Inherited from caller.
407 */
408void ata_bmdma_thaw(struct ata_port *ap)
409{
410 /* clear & re-enable interrupts */
411 ata_chk_status(ap);
412 ap->ops->irq_clear(ap);
Akira Iguchi83625002007-01-26 16:27:32 +0900413 ap->ops->irq_on(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900414}
415
416/**
417 * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
418 * @ap: port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +0900419 * @prereset: prereset method (can be NULL)
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900420 * @softreset: softreset method (can be NULL)
421 * @hardreset: hardreset method (can be NULL)
422 * @postreset: postreset method (can be NULL)
423 *
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.
428 *
429 * This function is intended to be used for constructing
430 * ->error_handler callback by low level drivers.
431 *
432 * LOCKING:
433 * Kernel thread context (may sleep)
434 */
Tejun Heof5914a42006-05-31 18:27:48 +0900435void 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)
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900438{
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900439 struct ata_queued_cmd *qc;
440 unsigned long flags;
441 int thaw = 0;
442
443 qc = __ata_qc_from_tag(ap, ap->active_tag);
444 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
445 qc = NULL;
446
447 /* reset PIO HSM and stop DMA engine */
Jeff Garzikba6a1302006-06-22 23:46:10 -0400448 spin_lock_irqsave(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900449
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900450 ap->hsm_task_state = HSM_ST_IDLE;
451
452 if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
453 qc->tf.protocol == ATA_PROT_ATAPI_DMA)) {
454 u8 host_stat;
455
Robert Hancockfbbb2622006-10-27 19:08:41 -0700456 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900457
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900458 /* 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.
462 */
Alan18d90de2007-01-24 11:42:38 +0000463 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900464 qc->err_mask = AC_ERR_HOST_BUS;
465 thaw = 1;
466 }
467
468 ap->ops->bmdma_stop(qc);
469 }
470
471 ata_altstatus(ap);
472 ata_chk_status(ap);
473 ap->ops->irq_clear(ap);
474
Jeff Garzikba6a1302006-06-22 23:46:10 -0400475 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900476
477 if (thaw)
478 ata_eh_thaw_port(ap);
479
480 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heof5914a42006-05-31 18:27:48 +0900481 ata_do_eh(ap, prereset, softreset, hardreset, postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900482}
483
484/**
485 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
486 * @ap: port to handle error for
487 *
488 * Stock error handler for BMDMA controller.
489 *
490 * LOCKING:
491 * Kernel thread context (may sleep)
492 */
493void ata_bmdma_error_handler(struct ata_port *ap)
494{
495 ata_reset_fn_t hardreset;
496
497 hardreset = NULL;
498 if (sata_scr_valid(ap))
499 hardreset = sata_std_hardreset;
500
Tejun Heof5914a42006-05-31 18:27:48 +0900501 ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
502 ata_std_postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900503}
504
505/**
506 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
507 * BMDMA controller
508 * @qc: internal command to clean up
509 *
510 * LOCKING:
511 * Kernel thread context (may sleep)
512 */
513void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
514{
Alan61dd08c2007-01-25 15:09:05 +0000515 if (qc->ap->ioaddr.bmdma_addr)
516 ata_bmdma_stop(qc);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900517}
518
Alan Coxd92e74d2007-06-07 16:19:15 +0100519/**
520 * ata_sff_port_start - Set port up for dma.
521 * @ap: Port to initialize
522 *
523 * Called just after data structures for each port are
524 * initialized. Allocates space for PRD table if the device
525 * is DMA capable SFF.
526 *
527 * May be used as the port_start() entry in ata_port_operations.
528 *
529 * LOCKING:
530 * Inherited from caller.
531 */
532
533int ata_sff_port_start(struct ata_port *ap)
534{
535 if (ap->ioaddr.bmdma_addr)
536 return ata_port_start(ap);
537 return 0;
538}
539
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500540#ifdef CONFIG_PCI
Alan4112e162007-01-08 12:10:05 +0000541
542static int ata_resources_present(struct pci_dev *pdev, int port)
543{
544 int i;
Jeff Garzika84471f2007-02-26 05:51:33 -0500545
Alan4112e162007-01-08 12:10:05 +0000546 /* Check the PCI resources for this channel are enabled */
547 port = port * 2;
548 for (i = 0; i < 2; i ++) {
549 if (pci_resource_start(pdev, port + i) == 0 ||
Tejun Heo55a6ade2007-03-09 19:43:35 +0900550 pci_resource_len(pdev, port + i) == 0)
551 return 0;
Alan4112e162007-01-08 12:10:05 +0000552 }
553 return 1;
554}
Jeff Garzika84471f2007-02-26 05:51:33 -0500555
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500556/**
Tejun Heo0f834de2007-04-17 23:44:07 +0900557 * ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host
558 * @host: target ATA host
559 *
560 * Acquire PCI BMDMA resources and initialize @host accordingly.
561 *
562 * LOCKING:
563 * Inherited from calling layer (may sleep).
564 *
565 * RETURNS:
566 * 0 on success, -errno otherwise.
567 */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200568int ata_pci_init_bmdma(struct ata_host *host)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500569{
Tejun Heo0f834de2007-04-17 23:44:07 +0900570 struct device *gdev = host->dev;
571 struct pci_dev *pdev = to_pci_dev(gdev);
572 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500573
Tejun Heo0f834de2007-04-17 23:44:07 +0900574 /* TODO: If we get no DMA mask we should fall back to PIO */
575 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
576 if (rc)
577 return rc;
578 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
579 if (rc)
580 return rc;
581
582 /* request and iomap DMA region */
583 rc = pcim_iomap_regions(pdev, 1 << 4, DRV_NAME);
584 if (rc) {
585 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
586 return -ENOMEM;
587 }
588 host->iomap = pcim_iomap_table(pdev);
589
Tejun Heo1626aeb2007-05-04 12:43:58 +0200590 for (i = 0; i < 2; i++) {
Tejun Heo0f834de2007-04-17 23:44:07 +0900591 struct ata_port *ap = host->ports[i];
Tejun Heo0f834de2007-04-17 23:44:07 +0900592 void __iomem *bmdma = host->iomap[4] + 8 * i;
593
594 if (ata_port_is_dummy(ap))
595 continue;
596
Tejun Heo21b0ad42007-04-17 23:44:07 +0900597 ap->ioaddr.bmdma_addr = bmdma;
Tejun Heo0f834de2007-04-17 23:44:07 +0900598 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
599 (ioread8(bmdma + 2) & 0x80))
600 host->flags |= ATA_HOST_SIMPLEX;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900601 }
602
Tejun Heo0f834de2007-04-17 23:44:07 +0900603 return 0;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500604}
605
Tejun Heod491b272007-04-17 23:44:07 +0900606/**
607 * ata_pci_init_native_host - acquire native ATA resources and init host
608 * @host: target ATA host
Tejun Heod491b272007-04-17 23:44:07 +0900609 *
Tejun Heo1626aeb2007-05-04 12:43:58 +0200610 * Acquire native PCI ATA resources for @host and initialize the
611 * first two ports of @host accordingly. Ports marked dummy are
612 * skipped and allocation failure makes the port dummy.
Tejun Heod491b272007-04-17 23:44:07 +0900613 *
614 * LOCKING:
615 * Inherited from calling layer (may sleep).
616 *
617 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +0200618 * 0 if at least one port is initialized, -ENODEV if no port is
619 * available.
Tejun Heod491b272007-04-17 23:44:07 +0900620 */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200621int ata_pci_init_native_host(struct ata_host *host)
Tejun Heod491b272007-04-17 23:44:07 +0900622{
623 struct device *gdev = host->dev;
624 struct pci_dev *pdev = to_pci_dev(gdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +0200625 unsigned int mask = 0;
Tejun Heod491b272007-04-17 23:44:07 +0900626 int i, rc;
627
Tejun Heod491b272007-04-17 23:44:07 +0900628 /* request, iomap BARs and init port addresses accordingly */
629 for (i = 0; i < 2; i++) {
630 struct ata_port *ap = host->ports[i];
631 int base = i * 2;
632 void __iomem * const *iomap;
633
Tejun Heo1626aeb2007-05-04 12:43:58 +0200634 if (ata_port_is_dummy(ap))
Tejun Heod491b272007-04-17 23:44:07 +0900635 continue;
636
Tejun Heo1626aeb2007-05-04 12:43:58 +0200637 /* Discard disabled ports. Some controllers show
638 * their unused channels this way. Disabled ports are
639 * made dummy.
640 */
641 if (!ata_resources_present(pdev, i)) {
642 ap->ops = &ata_dummy_port_ops;
643 continue;
644 }
645
Tejun Heod491b272007-04-17 23:44:07 +0900646 rc = pcim_iomap_regions(pdev, 0x3 << base, DRV_NAME);
647 if (rc) {
Tejun Heo1626aeb2007-05-04 12:43:58 +0200648 dev_printk(KERN_WARNING, gdev,
649 "failed to request/iomap BARs for port %d "
650 "(errno=%d)\n", i, rc);
Tejun Heod491b272007-04-17 23:44:07 +0900651 if (rc == -EBUSY)
652 pcim_pin_device(pdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +0200653 ap->ops = &ata_dummy_port_ops;
654 continue;
Tejun Heod491b272007-04-17 23:44:07 +0900655 }
656 host->iomap = iomap = pcim_iomap_table(pdev);
657
658 ap->ioaddr.cmd_addr = iomap[base];
659 ap->ioaddr.altstatus_addr =
660 ap->ioaddr.ctl_addr = (void __iomem *)
661 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
662 ata_std_ports(&ap->ioaddr);
Tejun Heo1626aeb2007-05-04 12:43:58 +0200663
664 mask |= 1 << i;
665 }
666
667 if (!mask) {
668 dev_printk(KERN_ERR, gdev, "no available native port\n");
669 return -ENODEV;
Tejun Heod491b272007-04-17 23:44:07 +0900670 }
671
672 return 0;
673}
674
Tejun Heo21b0ad42007-04-17 23:44:07 +0900675/**
676 * ata_pci_prepare_native_host - helper to prepare native PCI ATA host
677 * @pdev: target PCI device
Tejun Heo1626aeb2007-05-04 12:43:58 +0200678 * @ppi: array of port_info, must be enough for two ports
Tejun Heo21b0ad42007-04-17 23:44:07 +0900679 * @r_host: out argument for the initialized ATA host
680 *
681 * Helper to allocate ATA host for @pdev, acquire all native PCI
682 * resources and initialize it accordingly in one go.
683 *
684 * LOCKING:
685 * Inherited from calling layer (may sleep).
686 *
687 * RETURNS:
688 * 0 on success, -errno otherwise.
689 */
690int ata_pci_prepare_native_host(struct pci_dev *pdev,
691 const struct ata_port_info * const * ppi,
Tejun Heo1626aeb2007-05-04 12:43:58 +0200692 struct ata_host **r_host)
Tejun Heo21b0ad42007-04-17 23:44:07 +0900693{
694 struct ata_host *host;
Tejun Heo21b0ad42007-04-17 23:44:07 +0900695 int rc;
696
697 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
698 return -ENOMEM;
699
700 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
701 if (!host) {
702 dev_printk(KERN_ERR, &pdev->dev,
703 "failed to allocate ATA host\n");
704 rc = -ENOMEM;
705 goto err_out;
706 }
707
Tejun Heo1626aeb2007-05-04 12:43:58 +0200708 rc = ata_pci_init_native_host(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +0900709 if (rc)
710 goto err_out;
711
712 /* init DMA related stuff */
713 rc = ata_pci_init_bmdma(host);
714 if (rc)
715 goto err_bmdma;
716
717 devres_remove_group(&pdev->dev, NULL);
718 *r_host = host;
719 return 0;
720
721 err_bmdma:
722 /* This is necessary because PCI and iomap resources are
723 * merged and releasing the top group won't release the
724 * acquired resources if some of those have been acquired
725 * before entering this function.
726 */
727 pcim_iounmap_regions(pdev, 0xf);
728 err_out:
729 devres_release_group(&pdev->dev, NULL);
730 return rc;
731}
732
Tejun Heo0f834de2007-04-17 23:44:07 +0900733struct ata_legacy_devres {
734 unsigned int mask;
735 unsigned long cmd_port[2];
736 void __iomem * cmd_addr[2];
737 void __iomem * ctl_addr[2];
738 unsigned int irq[2];
739 void * irq_dev_id[2];
740};
741
742static void ata_legacy_free_irqs(struct ata_legacy_devres *legacy_dr)
743{
744 int i;
745
746 for (i = 0; i < 2; i++) {
747 if (!legacy_dr->irq[i])
748 continue;
749
750 free_irq(legacy_dr->irq[i], legacy_dr->irq_dev_id[i]);
751 legacy_dr->irq[i] = 0;
752 legacy_dr->irq_dev_id[i] = NULL;
753 }
754}
755
756static void ata_legacy_release(struct device *gdev, void *res)
757{
758 struct ata_legacy_devres *this = res;
759 int i;
760
761 ata_legacy_free_irqs(this);
762
763 for (i = 0; i < 2; i++) {
764 if (this->cmd_addr[i])
765 ioport_unmap(this->cmd_addr[i]);
766 if (this->ctl_addr[i])
767 ioport_unmap(this->ctl_addr[i]);
768 if (this->cmd_port[i])
769 release_region(this->cmd_port[i], 8);
770 }
771}
772
773static int ata_init_legacy_port(struct ata_port *ap,
774 struct ata_legacy_devres *legacy_dr)
775{
776 struct ata_host *host = ap->host;
777 int port_no = ap->port_no;
778 unsigned long cmd_port, ctl_port;
779
780 if (port_no == 0) {
781 cmd_port = ATA_PRIMARY_CMD;
782 ctl_port = ATA_PRIMARY_CTL;
783 } else {
784 cmd_port = ATA_SECONDARY_CMD;
785 ctl_port = ATA_SECONDARY_CTL;
786 }
787
788 /* request cmd_port */
789 if (request_region(cmd_port, 8, "libata"))
790 legacy_dr->cmd_port[port_no] = cmd_port;
791 else {
792 dev_printk(KERN_WARNING, host->dev,
793 "0x%0lX IDE port busy\n", cmd_port);
794 return -EBUSY;
795 }
796
797 /* iomap cmd and ctl ports */
798 legacy_dr->cmd_addr[port_no] = ioport_map(cmd_port, 8);
799 legacy_dr->ctl_addr[port_no] = ioport_map(ctl_port, 1);
Tejun Heo1626aeb2007-05-04 12:43:58 +0200800 if (!legacy_dr->cmd_addr[port_no] || !legacy_dr->ctl_addr[port_no]) {
801 dev_printk(KERN_WARNING, host->dev,
802 "failed to map cmd/ctl ports\n");
Tejun Heo0f834de2007-04-17 23:44:07 +0900803 return -ENOMEM;
Tejun Heo1626aeb2007-05-04 12:43:58 +0200804 }
Tejun Heo0f834de2007-04-17 23:44:07 +0900805
806 /* init IO addresses */
807 ap->ioaddr.cmd_addr = legacy_dr->cmd_addr[port_no];
808 ap->ioaddr.altstatus_addr = legacy_dr->ctl_addr[port_no];
809 ap->ioaddr.ctl_addr = legacy_dr->ctl_addr[port_no];
810 ata_std_ports(&ap->ioaddr);
811
812 return 0;
813}
814
815/**
816 * ata_init_legacy_host - acquire legacy ATA resources and init ATA host
817 * @host: target ATA host
Tejun Heo0f834de2007-04-17 23:44:07 +0900818 * @was_busy: out parameter, indicates whether any port was busy
819 *
Tejun Heo1626aeb2007-05-04 12:43:58 +0200820 * Acquire legacy ATA resources for the first two ports of @host
821 * and initialize it accordingly. Ports marked dummy are skipped
822 * and resource acquistion failure makes the port dummy.
Tejun Heo0f834de2007-04-17 23:44:07 +0900823 *
824 * LOCKING:
825 * Inherited from calling layer (may sleep).
826 *
827 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +0200828 * 0 if at least one port is initialized, -ENODEV if no port is
829 * available.
Tejun Heo0f834de2007-04-17 23:44:07 +0900830 */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200831static int ata_init_legacy_host(struct ata_host *host, int *was_busy)
Tejun Heo0f834de2007-04-17 23:44:07 +0900832{
833 struct device *gdev = host->dev;
834 struct ata_legacy_devres *legacy_dr;
835 int i, rc;
836
837 if (!devres_open_group(gdev, NULL, GFP_KERNEL))
838 return -ENOMEM;
839
840 rc = -ENOMEM;
841 legacy_dr = devres_alloc(ata_legacy_release, sizeof(*legacy_dr),
842 GFP_KERNEL);
843 if (!legacy_dr)
844 goto err_out;
845 devres_add(gdev, legacy_dr);
846
847 for (i = 0; i < 2; i++) {
Tejun Heo1626aeb2007-05-04 12:43:58 +0200848 if (ata_port_is_dummy(host->ports[i]))
849 continue;
850
Tejun Heo0f834de2007-04-17 23:44:07 +0900851 rc = ata_init_legacy_port(host->ports[i], legacy_dr);
852 if (rc == 0)
853 legacy_dr->mask |= 1 << i;
Tejun Heo1626aeb2007-05-04 12:43:58 +0200854 else {
855 if (rc == -EBUSY)
856 (*was_busy)++;
857 host->ports[i]->ops = &ata_dummy_port_ops;
858 }
Tejun Heo0f834de2007-04-17 23:44:07 +0900859 }
860
Tejun Heo1626aeb2007-05-04 12:43:58 +0200861 if (!legacy_dr->mask) {
862 dev_printk(KERN_ERR, gdev, "no available legacy port\n");
863 return -ENODEV;
864 }
Tejun Heo0f834de2007-04-17 23:44:07 +0900865
866 devres_remove_group(gdev, NULL);
867 return 0;
868
869 err_out:
870 devres_release_group(gdev, NULL);
871 return rc;
872}
873
874/**
875 * ata_request_legacy_irqs - request legacy ATA IRQs
876 * @host: target ATA host
877 * @handler: array of IRQ handlers
878 * @irq_flags: array of IRQ flags
879 * @dev_id: array of IRQ dev_ids
880 *
881 * Request legacy IRQs for non-dummy legacy ports in @host. All
882 * IRQ parameters are passed as array to allow ports to have
883 * separate IRQ handlers.
884 *
885 * LOCKING:
886 * Inherited from calling layer (may sleep).
887 *
888 * RETURNS:
889 * 0 on success, -errno otherwise.
890 */
891static int ata_request_legacy_irqs(struct ata_host *host,
892 irq_handler_t const *handler,
893 const unsigned int *irq_flags,
894 void * const *dev_id)
895{
896 struct device *gdev = host->dev;
897 struct ata_legacy_devres *legacy_dr;
898 int i, rc;
899
900 legacy_dr = devres_find(host->dev, ata_legacy_release, NULL, NULL);
901 BUG_ON(!legacy_dr);
902
Tejun Heo1626aeb2007-05-04 12:43:58 +0200903 for (i = 0; i < 2; i++) {
Tejun Heo0f834de2007-04-17 23:44:07 +0900904 unsigned int irq;
905
906 /* FIXME: ATA_*_IRQ() should take generic device not pci_dev */
907 if (i == 0)
908 irq = ATA_PRIMARY_IRQ(to_pci_dev(gdev));
909 else
910 irq = ATA_SECONDARY_IRQ(to_pci_dev(gdev));
911
912 if (!(legacy_dr->mask & (1 << i)))
913 continue;
914
915 if (!handler[i]) {
916 dev_printk(KERN_ERR, gdev,
917 "NULL handler specified for port %d\n", i);
918 rc = -EINVAL;
919 goto err_out;
920 }
921
922 rc = request_irq(irq, handler[i], irq_flags[i], DRV_NAME,
923 dev_id[i]);
924 if (rc) {
925 dev_printk(KERN_ERR, gdev,
926 "irq %u request failed (errno=%d)\n", irq, rc);
927 goto err_out;
928 }
929
930 /* record irq allocation in legacy_dr */
931 legacy_dr->irq[i] = irq;
932 legacy_dr->irq_dev_id[i] = dev_id[i];
933
934 /* only used to print info */
935 if (i == 0)
936 host->irq = irq;
937 else
938 host->irq2 = irq;
939 }
940
941 return 0;
942
943 err_out:
944 ata_legacy_free_irqs(legacy_dr);
945 return rc;
946}
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500947
948/**
949 * ata_pci_init_one - Initialize/register PCI IDE host controller
950 * @pdev: Controller to be initialized
Tejun Heo1626aeb2007-05-04 12:43:58 +0200951 * @ppi: array of port_info, must be enough for two ports
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500952 *
953 * This is a helper function which can be called from a driver's
954 * xxx_init_one() probe function if the hardware uses traditional
955 * IDE taskfile registers.
956 *
957 * This function calls pci_enable_device(), reserves its register
958 * regions, sets the dma mask, enables bus master mode, and calls
959 * ata_device_add()
960 *
Alan Cox2ec7df02006-08-10 16:59:10 +0900961 * ASSUMPTION:
962 * Nobody makes a single channel controller that appears solely as
963 * the secondary legacy port on PCI.
964 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500965 * LOCKING:
966 * Inherited from PCI layer (may sleep).
967 *
968 * RETURNS:
969 * Zero on success, negative on errno-based value on error.
970 */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200971int ata_pci_init_one(struct pci_dev *pdev,
972 const struct ata_port_info * const * ppi)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500973{
Tejun Heof0d36ef2007-01-20 16:00:28 +0900974 struct device *dev = &pdev->dev;
Tejun Heo1626aeb2007-05-04 12:43:58 +0200975 const struct ata_port_info *pi = NULL;
Tejun Heo0f834de2007-04-17 23:44:07 +0900976 struct ata_host *host = NULL;
Jeff Garzikc791c302006-09-28 03:40:11 -0400977 u8 mask;
Tejun Heo1626aeb2007-05-04 12:43:58 +0200978 int legacy_mode = 0;
979 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500980
981 DPRINTK("ENTER\n");
982
Tejun Heo1626aeb2007-05-04 12:43:58 +0200983 /* look up the first valid port_info */
984 for (i = 0; i < 2 && ppi[i]; i++) {
985 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
986 pi = ppi[i];
987 break;
988 }
989 }
990
991 if (!pi) {
992 dev_printk(KERN_ERR, &pdev->dev,
993 "no valid port_info specified\n");
994 return -EINVAL;
995 }
996
Tejun Heof0d36ef2007-01-20 16:00:28 +0900997 if (!devres_open_group(dev, NULL, GFP_KERNEL))
998 return -ENOMEM;
999
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001000 /* FIXME: Really for ATA it isn't safe because the device may be
1001 multi-purpose and we want to leave it alone if it was already
1002 enabled. Secondly for shared use as Arjan says we want refcounting
1003
1004 Checking dev->is_enabled is insufficient as this is not set at
1005 boot for the primary video which is BIOS enabled
Tejun Heod491b272007-04-17 23:44:07 +09001006 */
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001007
Tejun Heof0d36ef2007-01-20 16:00:28 +09001008 rc = pcim_enable_device(pdev);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001009 if (rc)
Tejun Heof0d36ef2007-01-20 16:00:28 +09001010 goto err_out;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001011
Jeff Garzikc791c302006-09-28 03:40:11 -04001012 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
1013 u8 tmp8;
1014
1015 /* TODO: What if one channel is in native mode ... */
1016 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
1017 mask = (1 << 2) | (1 << 0);
1018 if ((tmp8 & mask) != mask)
Tejun Heo1626aeb2007-05-04 12:43:58 +02001019 legacy_mode = 1;
Alan Cox8eb166b2006-10-16 16:24:50 +01001020#if defined(CONFIG_NO_ATA_LEGACY)
1021 /* Some platforms with PCI limits cannot address compat
1022 port space. In that case we punt if their firmware has
1023 left a device in compatibility mode */
1024 if (legacy_mode) {
1025 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
Tejun Heof0d36ef2007-01-20 16:00:28 +09001026 rc = -EOPNOTSUPP;
1027 goto err_out;
Alan Cox8eb166b2006-10-16 16:24:50 +01001028 }
1029#endif
Jeff Garzikc791c302006-09-28 03:40:11 -04001030 }
1031
Tejun Heod491b272007-04-17 23:44:07 +09001032 /* alloc and init host */
Tejun Heo1626aeb2007-05-04 12:43:58 +02001033 host = ata_host_alloc_pinfo(dev, ppi, 2);
Tejun Heod491b272007-04-17 23:44:07 +09001034 if (!host) {
1035 dev_printk(KERN_ERR, &pdev->dev,
1036 "failed to allocate ATA host\n");
1037 rc = -ENOMEM;
1038 goto err_out;
1039 }
1040
Alandc3c3372007-01-02 11:58:34 +00001041 if (!legacy_mode) {
Tejun Heo1626aeb2007-05-04 12:43:58 +02001042 rc = ata_pci_init_native_host(host);
Tejun Heo0f834de2007-04-17 23:44:07 +09001043 if (rc)
1044 goto err_out;
Alandc3c3372007-01-02 11:58:34 +00001045 } else {
Tejun Heo0f834de2007-04-17 23:44:07 +09001046 int was_busy = 0;
1047
Tejun Heo1626aeb2007-05-04 12:43:58 +02001048 rc = ata_init_legacy_host(host, &was_busy);
Tejun Heo0f834de2007-04-17 23:44:07 +09001049 if (was_busy)
Jeff Garzik8cdfb292007-03-09 10:54:42 -05001050 pcim_pin_device(pdev);
Tejun Heo0f834de2007-04-17 23:44:07 +09001051 if (rc)
1052 goto err_out;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001053
Tejun Heo0f834de2007-04-17 23:44:07 +09001054 /* request respective PCI regions, may fail */
1055 rc = pci_request_region(pdev, 1, DRV_NAME);
1056 rc = pci_request_region(pdev, 3, DRV_NAME);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001057 }
1058
Tejun Heod491b272007-04-17 23:44:07 +09001059 /* init BMDMA, may fail */
1060 ata_pci_init_bmdma(host);
1061 pci_set_master(pdev);
1062
1063 /* start host and request IRQ */
1064 rc = ata_host_start(host);
1065 if (rc)
1066 goto err_out;
1067
1068 if (!legacy_mode)
Tejun Heo1626aeb2007-05-04 12:43:58 +02001069 rc = devm_request_irq(dev, pdev->irq, pi->port_ops->irq_handler,
Tejun Heod491b272007-04-17 23:44:07 +09001070 IRQF_SHARED, DRV_NAME, host);
1071 else {
Tejun Heo0f834de2007-04-17 23:44:07 +09001072 irq_handler_t handler[2] = { host->ops->irq_handler,
1073 host->ops->irq_handler };
1074 unsigned int irq_flags[2] = { IRQF_SHARED, IRQF_SHARED };
1075 void *dev_id[2] = { host, host };
1076
Tejun Heo0f834de2007-04-17 23:44:07 +09001077 rc = ata_request_legacy_irqs(host, handler, irq_flags, dev_id);
Jeff Garzikc791c302006-09-28 03:40:11 -04001078 }
Tejun Heod491b272007-04-17 23:44:07 +09001079 if (rc)
1080 goto err_out;
1081
1082 /* register */
Tejun Heo1626aeb2007-05-04 12:43:58 +02001083 rc = ata_host_register(host, pi->sht);
Tejun Heod491b272007-04-17 23:44:07 +09001084 if (rc)
1085 goto err_out;
1086
Tejun Heof0d36ef2007-01-20 16:00:28 +09001087 devres_remove_group(dev, NULL);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001088 return 0;
1089
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001090err_out:
Tejun Heof0d36ef2007-01-20 16:00:28 +09001091 devres_release_group(dev, NULL);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001092 return rc;
1093}
1094
Alan Coxd33d44f2006-03-21 15:59:57 +00001095/**
1096 * ata_pci_clear_simplex - attempt to kick device out of simplex
1097 * @pdev: PCI device
1098 *
1099 * Some PCI ATA devices report simplex mode but in fact can be told to
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001100 * enter non simplex mode. This implements the neccessary logic to
Alan Coxd33d44f2006-03-21 15:59:57 +00001101 * perform the task on such devices. Calling it on other devices will
1102 * have -undefined- behaviour.
1103 */
1104
1105int ata_pci_clear_simplex(struct pci_dev *pdev)
1106{
1107 unsigned long bmdma = pci_resource_start(pdev, 4);
1108 u8 simplex;
1109
1110 if (bmdma == 0)
1111 return -ENOENT;
1112
1113 simplex = inb(bmdma + 0x02);
1114 outb(simplex & 0x60, bmdma + 0x02);
1115 simplex = inb(bmdma + 0x02);
1116 if (simplex & 0x80)
1117 return -EOPNOTSUPP;
1118 return 0;
1119}
1120
Alan Coxa76b62c2007-03-09 09:34:07 -05001121unsigned long ata_pci_default_filter(struct ata_device *adev, unsigned long xfer_mask)
Alan Coxd33d44f2006-03-21 15:59:57 +00001122{
1123 /* Filter out DMA modes if the device has been configured by
1124 the BIOS as PIO only */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001125
Alan Coxa76b62c2007-03-09 09:34:07 -05001126 if (adev->ap->ioaddr.bmdma_addr == 0)
Alan Coxd33d44f2006-03-21 15:59:57 +00001127 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
1128 return xfer_mask;
1129}
1130
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001131#endif /* CONFIG_PCI */
1132