blob: 6223ec042c801c43828bbf2809b2d26d04e3a301 [file] [log] [blame]
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001/*
Dave Jonesf3a03b02007-07-16 11:23:03 -04002 * libata-sff.c - helper library for PCI IDE BMDMA
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003 *
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 Heof659f0e42008-03-06 13:12:54 +090059 if (ioaddr->ctl_addr)
60 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo90088bb2006-10-09 11:10:26 +090061 tmp = ata_wait_idle(ap);
62
63 ap->ops->irq_clear(ap);
64
65 return tmp;
66}
67
68/**
Tejun Heo0d5ff562007-02-01 15:06:36 +090069 * ata_tf_load - send taskfile registers to host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050070 * @ap: Port to which output is sent
71 * @tf: ATA taskfile register set
72 *
73 * Outputs ATA taskfile to standard ATA host controller.
74 *
75 * LOCKING:
76 * Inherited from caller.
77 */
78
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050079void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
80{
Tejun Heo0d5ff562007-02-01 15:06:36 +090081 struct ata_ioports *ioaddr = &ap->ioaddr;
82 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
83
84 if (tf->ctl != ap->last_ctl) {
Tejun Heof659f0e42008-03-06 13:12:54 +090085 if (ioaddr->ctl_addr)
86 iowrite8(tf->ctl, ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +090087 ap->last_ctl = tf->ctl;
88 ata_wait_idle(ap);
89 }
90
91 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
Tejun Heof659f0e42008-03-06 13:12:54 +090092 WARN_ON(!ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +090093 iowrite8(tf->hob_feature, ioaddr->feature_addr);
94 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
95 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
96 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
97 iowrite8(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);
104 }
105
106 if (is_addr) {
107 iowrite8(tf->feature, ioaddr->feature_addr);
108 iowrite8(tf->nsect, ioaddr->nsect_addr);
109 iowrite8(tf->lbal, ioaddr->lbal_addr);
110 iowrite8(tf->lbam, ioaddr->lbam_addr);
111 iowrite8(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);
118 }
119
120 if (tf->flags & ATA_TFLAG_DEVICE) {
121 iowrite8(tf->device, ioaddr->device_addr);
122 VPRINTK("device 0x%X\n", tf->device);
123 }
124
125 ata_wait_idle(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500126}
127
128/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500129 * ata_exec_command - issue ATA command to host controller
130 * @ap: port to which command is being issued
131 * @tf: ATA taskfile register set
132 *
Tejun Heo0d5ff562007-02-01 15:06:36 +0900133 * Issues ATA command, with proper synchronization with interrupt
134 * handler / other threads.
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500135 *
136 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400137 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500138 */
139void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
140{
Tejun Heo44877b42007-02-21 01:06:51 +0900141 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900142
143 iowrite8(tf->command, ap->ioaddr.command_addr);
144 ata_pause(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500145}
146
147/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500148 * ata_tf_read - input device's ATA taskfile shadow registers
149 * @ap: Port from which input is read
150 * @tf: ATA taskfile register set for storing input
151 *
152 * Reads ATA taskfile registers for currently-selected device
Alan Cox76548ed2007-11-19 14:34:56 +0000153 * into @tf. Assumes the device has a fully SFF compliant task file
154 * layout and behaviour. If you device does not (eg has a different
155 * status method) then you will need to provide a replacement tf_read
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500156 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500157 * LOCKING:
158 * Inherited from caller.
159 */
160void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
161{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900162 struct ata_ioports *ioaddr = &ap->ioaddr;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500163
Alan Cox76548ed2007-11-19 14:34:56 +0000164 tf->command = ata_check_status(ap);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900165 tf->feature = ioread8(ioaddr->error_addr);
166 tf->nsect = ioread8(ioaddr->nsect_addr);
167 tf->lbal = ioread8(ioaddr->lbal_addr);
168 tf->lbam = ioread8(ioaddr->lbam_addr);
169 tf->lbah = ioread8(ioaddr->lbah_addr);
170 tf->device = ioread8(ioaddr->device_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500171
Tejun Heo0d5ff562007-02-01 15:06:36 +0900172 if (tf->flags & ATA_TFLAG_LBA48) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900173 if (likely(ioaddr->ctl_addr)) {
174 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
175 tf->hob_feature = ioread8(ioaddr->error_addr);
176 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
177 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
178 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
179 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
180 iowrite8(tf->ctl, ioaddr->ctl_addr);
181 ap->last_ctl = tf->ctl;
182 } else
183 WARN_ON(1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900184 }
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500185}
186
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500187/**
188 * ata_check_status - Read device status reg & clear interrupt
189 * @ap: port where the device is
190 *
191 * Reads ATA taskfile status register for currently-selected device
192 * and return its value. This also clears pending interrupts
193 * from this device
194 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500195 * LOCKING:
196 * Inherited from caller.
197 */
198u8 ata_check_status(struct ata_port *ap)
199{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900200 return ioread8(ap->ioaddr.status_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500201}
202
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500203/**
204 * ata_altstatus - Read device alternate status reg
205 * @ap: port where the device is
206 *
207 * Reads ATA taskfile alternate status register for
208 * currently-selected device and return its value.
209 *
210 * Note: may NOT be used as the check_altstatus() entry in
211 * ata_port_operations.
212 *
213 * LOCKING:
214 * Inherited from caller.
215 */
216u8 ata_altstatus(struct ata_port *ap)
217{
218 if (ap->ops->check_altstatus)
219 return ap->ops->check_altstatus(ap);
220
Tejun Heo0d5ff562007-02-01 15:06:36 +0900221 return ioread8(ap->ioaddr.altstatus_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500222}
223
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500224/**
Tejun Heo0d5ff562007-02-01 15:06:36 +0900225 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500226 * @qc: Info associated with this ATA transaction.
227 *
228 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400229 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500230 */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900231void ata_bmdma_setup(struct ata_queued_cmd *qc)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500232{
233 struct ata_port *ap = qc->ap;
234 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
235 u8 dmactl;
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500236
237 /* load PRD table addr. */
238 mb(); /* make sure PRD table writes are visible to controller */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900239 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500240
241 /* specify data direction, triple-check start bit is clear */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900242 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500243 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
244 if (!rw)
245 dmactl |= ATA_DMA_WR;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900246 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500247
248 /* issue r/w command */
249 ap->ops->exec_command(ap, &qc->tf);
250}
251
252/**
Tejun Heo0d5ff562007-02-01 15:06:36 +0900253 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500254 * @qc: Info associated with this ATA transaction.
255 *
256 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400257 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500258 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400259void ata_bmdma_start(struct ata_queued_cmd *qc)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500260{
261 struct ata_port *ap = qc->ap;
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500262 u8 dmactl;
263
264 /* start host DMA transaction */
Tejun Heo0d5ff562007-02-01 15:06:36 +0900265 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
266 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500267
Alan Coxe1cc9de2007-09-20 15:03:07 +0100268 /* Strictly, one may wish to issue an ioread8() here, to
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500269 * flush the mmio write. However, control also passes
270 * to the hardware at this point, and it will interrupt
271 * us when we are to resume control. So, in effect,
272 * we don't care when the mmio write flushes.
273 * Further, a read of the DMA status register _immediately_
274 * following the write may not be what certain flaky hardware
275 * is expected, so I think it is best to not add a readb()
276 * without first all the MMIO ATA cards/mobos.
277 * Or maybe I'm just being paranoid.
Alan Coxe1cc9de2007-09-20 15:03:07 +0100278 *
279 * FIXME: The posting of this write means I/O starts are
280 * unneccessarily delayed for MMIO
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500281 */
282}
283
284/**
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500285 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
286 * @ap: Port associated with this ATA transaction.
287 *
288 * Clear interrupt and error flags in DMA status register.
289 *
290 * May be used as the irq_clear() entry in ata_port_operations.
291 *
292 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400293 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500294 */
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500295void ata_bmdma_irq_clear(struct ata_port *ap)
296{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900297 void __iomem *mmio = ap->ioaddr.bmdma_addr;
298
299 if (!mmio)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500300 return;
301
Tejun Heo0d5ff562007-02-01 15:06:36 +0900302 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500303}
304
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500305/**
Tejun Heo358f9a72008-03-25 12:22:47 +0900306 * ata_noop_irq_clear - Noop placeholder for irq_clear
307 * @ap: Port associated with this ATA transaction.
308 */
309void ata_noop_irq_clear(struct ata_port *ap)
310{
311}
312
313/**
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500314 * ata_bmdma_status - Read PCI IDE BMDMA status
315 * @ap: Port associated with this ATA transaction.
316 *
317 * Read and return BMDMA status register.
318 *
319 * May be used as the bmdma_status() entry in ata_port_operations.
320 *
321 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400322 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500323 */
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500324u8 ata_bmdma_status(struct ata_port *ap)
325{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900326 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500327}
328
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500329/**
330 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
331 * @qc: Command we are ending DMA for
332 *
333 * Clears the ATA_DMA_START flag in the dma control register
334 *
335 * May be used as the bmdma_stop() entry in ata_port_operations.
336 *
337 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400338 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500339 */
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500340void ata_bmdma_stop(struct ata_queued_cmd *qc)
341{
342 struct ata_port *ap = qc->ap;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900343 void __iomem *mmio = ap->ioaddr.bmdma_addr;
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500344
Tejun Heo0d5ff562007-02-01 15:06:36 +0900345 /* clear start/stop bit */
346 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
347 mmio + ATA_DMA_CMD);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500348
349 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
350 ata_altstatus(ap); /* dummy read */
351}
352
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900353/**
354 * ata_bmdma_freeze - Freeze BMDMA controller port
355 * @ap: port to freeze
356 *
357 * Freeze BMDMA controller port.
358 *
359 * LOCKING:
360 * Inherited from caller.
361 */
362void ata_bmdma_freeze(struct ata_port *ap)
363{
364 struct ata_ioports *ioaddr = &ap->ioaddr;
365
366 ap->ctl |= ATA_NIEN;
367 ap->last_ctl = ap->ctl;
368
Tejun Heof659f0e42008-03-06 13:12:54 +0900369 if (ioaddr->ctl_addr)
370 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +0900371
372 /* Under certain circumstances, some controllers raise IRQ on
373 * ATA_NIEN manipulation. Also, many controllers fail to mask
374 * previously pending IRQ on ATA_NIEN assertion. Clear it.
375 */
376 ata_chk_status(ap);
377
378 ap->ops->irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900379}
380
381/**
382 * ata_bmdma_thaw - Thaw BMDMA controller port
383 * @ap: port to thaw
384 *
385 * Thaw BMDMA controller port.
386 *
387 * LOCKING:
388 * Inherited from caller.
389 */
390void ata_bmdma_thaw(struct ata_port *ap)
391{
392 /* clear & re-enable interrupts */
393 ata_chk_status(ap);
394 ap->ops->irq_clear(ap);
Akira Iguchi83625002007-01-26 16:27:32 +0900395 ap->ops->irq_on(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900396}
397
398/**
399 * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller
400 * @ap: port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +0900401 * @prereset: prereset method (can be NULL)
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900402 * @softreset: softreset method (can be NULL)
403 * @hardreset: hardreset method (can be NULL)
404 * @postreset: postreset method (can be NULL)
405 *
406 * Handle error for ATA BMDMA controller. It can handle both
407 * PATA and SATA controllers. Many controllers should be able to
408 * use this EH as-is or with some added handling before and
409 * after.
410 *
411 * This function is intended to be used for constructing
412 * ->error_handler callback by low level drivers.
413 *
414 * LOCKING:
415 * Kernel thread context (may sleep)
416 */
Tejun Heof5914a42006-05-31 18:27:48 +0900417void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
418 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
419 ata_postreset_fn_t postreset)
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900420{
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900421 struct ata_queued_cmd *qc;
422 unsigned long flags;
423 int thaw = 0;
424
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900425 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900426 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
427 qc = NULL;
428
429 /* reset PIO HSM and stop DMA engine */
Jeff Garzikba6a1302006-06-22 23:46:10 -0400430 spin_lock_irqsave(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900431
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900432 ap->hsm_task_state = HSM_ST_IDLE;
433
434 if (qc && (qc->tf.protocol == ATA_PROT_DMA ||
Tejun Heo0dc36882007-12-18 16:34:43 -0500435 qc->tf.protocol == ATAPI_PROT_DMA)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900436 u8 host_stat;
437
Robert Hancockfbbb2622006-10-27 19:08:41 -0700438 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900439
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900440 /* BMDMA controllers indicate host bus error by
441 * setting DMA_ERR bit and timing out. As it wasn't
442 * really a timeout event, adjust error mask and
443 * cancel frozen state.
444 */
Alan18d90de2007-01-24 11:42:38 +0000445 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900446 qc->err_mask = AC_ERR_HOST_BUS;
447 thaw = 1;
448 }
449
450 ap->ops->bmdma_stop(qc);
451 }
452
453 ata_altstatus(ap);
454 ata_chk_status(ap);
455 ap->ops->irq_clear(ap);
456
Jeff Garzikba6a1302006-06-22 23:46:10 -0400457 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900458
459 if (thaw)
460 ata_eh_thaw_port(ap);
461
462 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heof5914a42006-05-31 18:27:48 +0900463 ata_do_eh(ap, prereset, softreset, hardreset, postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900464}
465
466/**
467 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
468 * @ap: port to handle error for
469 *
470 * Stock error handler for BMDMA controller.
471 *
472 * LOCKING:
473 * Kernel thread context (may sleep)
474 */
475void ata_bmdma_error_handler(struct ata_port *ap)
476{
Tejun Heof659f0e42008-03-06 13:12:54 +0900477 ata_reset_fn_t softreset = NULL, hardreset = NULL;
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900478
Tejun Heof659f0e42008-03-06 13:12:54 +0900479 if (ap->ioaddr.ctl_addr)
480 softreset = ata_std_softreset;
Tejun Heo936fd732007-08-06 18:36:23 +0900481 if (sata_scr_valid(&ap->link))
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900482 hardreset = sata_std_hardreset;
483
Tejun Heof659f0e42008-03-06 13:12:54 +0900484 ata_bmdma_drive_eh(ap, ata_std_prereset, softreset, hardreset,
Tejun Heof5914a42006-05-31 18:27:48 +0900485 ata_std_postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900486}
487
488/**
489 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for
490 * BMDMA controller
491 * @qc: internal command to clean up
492 *
493 * LOCKING:
494 * Kernel thread context (may sleep)
495 */
496void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
497{
Alan61dd08c2007-01-25 15:09:05 +0000498 if (qc->ap->ioaddr.bmdma_addr)
499 ata_bmdma_stop(qc);
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900500}
501
Alan Coxd92e74d2007-06-07 16:19:15 +0100502/**
503 * ata_sff_port_start - Set port up for dma.
504 * @ap: Port to initialize
505 *
506 * Called just after data structures for each port are
507 * initialized. Allocates space for PRD table if the device
508 * is DMA capable SFF.
509 *
510 * May be used as the port_start() entry in ata_port_operations.
511 *
512 * LOCKING:
513 * Inherited from caller.
514 */
515
516int ata_sff_port_start(struct ata_port *ap)
517{
518 if (ap->ioaddr.bmdma_addr)
519 return ata_port_start(ap);
520 return 0;
521}
522
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500523#ifdef CONFIG_PCI
Alan4112e162007-01-08 12:10:05 +0000524
525static int ata_resources_present(struct pci_dev *pdev, int port)
526{
527 int i;
Jeff Garzika84471f2007-02-26 05:51:33 -0500528
Alan4112e162007-01-08 12:10:05 +0000529 /* Check the PCI resources for this channel are enabled */
530 port = port * 2;
531 for (i = 0; i < 2; i ++) {
532 if (pci_resource_start(pdev, port + i) == 0 ||
Tejun Heo55a6ade2007-03-09 19:43:35 +0900533 pci_resource_len(pdev, port + i) == 0)
534 return 0;
Alan4112e162007-01-08 12:10:05 +0000535 }
536 return 1;
537}
Jeff Garzika84471f2007-02-26 05:51:33 -0500538
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500539/**
Tejun Heo0f834de2007-04-17 23:44:07 +0900540 * ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host
541 * @host: target ATA host
542 *
543 * Acquire PCI BMDMA resources and initialize @host accordingly.
544 *
545 * LOCKING:
546 * Inherited from calling layer (may sleep).
547 *
548 * RETURNS:
549 * 0 on success, -errno otherwise.
550 */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200551int ata_pci_init_bmdma(struct ata_host *host)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500552{
Tejun Heo0f834de2007-04-17 23:44:07 +0900553 struct device *gdev = host->dev;
554 struct pci_dev *pdev = to_pci_dev(gdev);
555 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500556
Alan Cox6fdc99a2007-07-26 18:41:30 +0100557 /* No BAR4 allocation: No DMA */
558 if (pci_resource_start(pdev, 4) == 0)
559 return 0;
560
Tejun Heo0f834de2007-04-17 23:44:07 +0900561 /* TODO: If we get no DMA mask we should fall back to PIO */
562 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
563 if (rc)
564 return rc;
565 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
566 if (rc)
567 return rc;
568
569 /* request and iomap DMA region */
Tejun Heo35a10a82008-01-04 18:42:21 +0900570 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
Tejun Heo0f834de2007-04-17 23:44:07 +0900571 if (rc) {
572 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
573 return -ENOMEM;
574 }
575 host->iomap = pcim_iomap_table(pdev);
576
Tejun Heo1626aeb2007-05-04 12:43:58 +0200577 for (i = 0; i < 2; i++) {
Tejun Heo0f834de2007-04-17 23:44:07 +0900578 struct ata_port *ap = host->ports[i];
Tejun Heo0f834de2007-04-17 23:44:07 +0900579 void __iomem *bmdma = host->iomap[4] + 8 * i;
580
581 if (ata_port_is_dummy(ap))
582 continue;
583
Tejun Heo21b0ad42007-04-17 23:44:07 +0900584 ap->ioaddr.bmdma_addr = bmdma;
Tejun Heo0f834de2007-04-17 23:44:07 +0900585 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
586 (ioread8(bmdma + 2) & 0x80))
587 host->flags |= ATA_HOST_SIMPLEX;
Tejun Heocbcdd872007-08-18 13:14:55 +0900588
589 ata_port_desc(ap, "bmdma 0x%llx",
590 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900591 }
592
Tejun Heo0f834de2007-04-17 23:44:07 +0900593 return 0;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500594}
595
Tejun Heod491b272007-04-17 23:44:07 +0900596/**
Tejun Heod583bc12007-07-04 18:02:07 +0900597 * ata_pci_init_sff_host - acquire native PCI ATA resources and init host
Tejun Heod491b272007-04-17 23:44:07 +0900598 * @host: target ATA host
Tejun Heod491b272007-04-17 23:44:07 +0900599 *
Tejun Heo1626aeb2007-05-04 12:43:58 +0200600 * Acquire native PCI ATA resources for @host and initialize the
601 * first two ports of @host accordingly. Ports marked dummy are
602 * skipped and allocation failure makes the port dummy.
Tejun Heod491b272007-04-17 23:44:07 +0900603 *
Tejun Heod583bc12007-07-04 18:02:07 +0900604 * Note that native PCI resources are valid even for legacy hosts
605 * as we fix up pdev resources array early in boot, so this
606 * function can be used for both native and legacy SFF hosts.
607 *
Tejun Heod491b272007-04-17 23:44:07 +0900608 * LOCKING:
609 * Inherited from calling layer (may sleep).
610 *
611 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +0200612 * 0 if at least one port is initialized, -ENODEV if no port is
613 * available.
Tejun Heod491b272007-04-17 23:44:07 +0900614 */
Tejun Heod583bc12007-07-04 18:02:07 +0900615int ata_pci_init_sff_host(struct ata_host *host)
Tejun Heod491b272007-04-17 23:44:07 +0900616{
617 struct device *gdev = host->dev;
618 struct pci_dev *pdev = to_pci_dev(gdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +0200619 unsigned int mask = 0;
Tejun Heod491b272007-04-17 23:44:07 +0900620 int i, rc;
621
Tejun Heod491b272007-04-17 23:44:07 +0900622 /* request, iomap BARs and init port addresses accordingly */
623 for (i = 0; i < 2; i++) {
624 struct ata_port *ap = host->ports[i];
625 int base = i * 2;
626 void __iomem * const *iomap;
627
Tejun Heo1626aeb2007-05-04 12:43:58 +0200628 if (ata_port_is_dummy(ap))
Tejun Heod491b272007-04-17 23:44:07 +0900629 continue;
630
Tejun Heo1626aeb2007-05-04 12:43:58 +0200631 /* Discard disabled ports. Some controllers show
632 * their unused channels this way. Disabled ports are
633 * made dummy.
634 */
635 if (!ata_resources_present(pdev, i)) {
636 ap->ops = &ata_dummy_port_ops;
637 continue;
638 }
639
Tejun Heo35a10a82008-01-04 18:42:21 +0900640 rc = pcim_iomap_regions(pdev, 0x3 << base,
641 dev_driver_string(gdev));
Tejun Heod491b272007-04-17 23:44:07 +0900642 if (rc) {
Tejun Heo1626aeb2007-05-04 12:43:58 +0200643 dev_printk(KERN_WARNING, gdev,
644 "failed to request/iomap BARs for port %d "
645 "(errno=%d)\n", i, rc);
Tejun Heod491b272007-04-17 23:44:07 +0900646 if (rc == -EBUSY)
647 pcim_pin_device(pdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +0200648 ap->ops = &ata_dummy_port_ops;
649 continue;
Tejun Heod491b272007-04-17 23:44:07 +0900650 }
651 host->iomap = iomap = pcim_iomap_table(pdev);
652
653 ap->ioaddr.cmd_addr = iomap[base];
654 ap->ioaddr.altstatus_addr =
655 ap->ioaddr.ctl_addr = (void __iomem *)
656 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
657 ata_std_ports(&ap->ioaddr);
Tejun Heo1626aeb2007-05-04 12:43:58 +0200658
Tejun Heocbcdd872007-08-18 13:14:55 +0900659 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
660 (unsigned long long)pci_resource_start(pdev, base),
661 (unsigned long long)pci_resource_start(pdev, base + 1));
662
Tejun Heo1626aeb2007-05-04 12:43:58 +0200663 mask |= 1 << i;
664 }
665
666 if (!mask) {
667 dev_printk(KERN_ERR, gdev, "no available native port\n");
668 return -ENODEV;
Tejun Heod491b272007-04-17 23:44:07 +0900669 }
670
671 return 0;
672}
673
Tejun Heo21b0ad42007-04-17 23:44:07 +0900674/**
Tejun Heod583bc12007-07-04 18:02:07 +0900675 * ata_pci_prepare_sff_host - helper to prepare native PCI ATA host
Tejun Heo21b0ad42007-04-17 23:44:07 +0900676 * @pdev: target PCI device
Tejun Heo1626aeb2007-05-04 12:43:58 +0200677 * @ppi: array of port_info, must be enough for two ports
Tejun Heo21b0ad42007-04-17 23:44:07 +0900678 * @r_host: out argument for the initialized ATA host
679 *
680 * Helper to allocate ATA host for @pdev, acquire all native PCI
681 * resources and initialize it accordingly in one go.
682 *
683 * LOCKING:
684 * Inherited from calling layer (may sleep).
685 *
686 * RETURNS:
687 * 0 on success, -errno otherwise.
688 */
Tejun Heod583bc12007-07-04 18:02:07 +0900689int ata_pci_prepare_sff_host(struct pci_dev *pdev,
690 const struct ata_port_info * const * ppi,
691 struct ata_host **r_host)
Tejun Heo21b0ad42007-04-17 23:44:07 +0900692{
693 struct ata_host *host;
Tejun Heo21b0ad42007-04-17 23:44:07 +0900694 int rc;
695
696 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
697 return -ENOMEM;
698
699 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
700 if (!host) {
701 dev_printk(KERN_ERR, &pdev->dev,
702 "failed to allocate ATA host\n");
703 rc = -ENOMEM;
704 goto err_out;
705 }
706
Tejun Heod583bc12007-07-04 18:02:07 +0900707 rc = ata_pci_init_sff_host(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +0900708 if (rc)
709 goto err_out;
710
711 /* init DMA related stuff */
712 rc = ata_pci_init_bmdma(host);
713 if (rc)
714 goto err_bmdma;
715
716 devres_remove_group(&pdev->dev, NULL);
717 *r_host = host;
718 return 0;
719
720 err_bmdma:
721 /* This is necessary because PCI and iomap resources are
722 * merged and releasing the top group won't release the
723 * acquired resources if some of those have been acquired
724 * before entering this function.
725 */
726 pcim_iounmap_regions(pdev, 0xf);
727 err_out:
728 devres_release_group(&pdev->dev, NULL);
729 return rc;
730}
731
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500732/**
Tejun Heo4e6b79f2008-01-18 18:36:28 +0900733 * ata_pci_activate_sff_host - start SFF host, request IRQ and register it
734 * @host: target SFF ATA host
735 * @irq_handler: irq_handler used when requesting IRQ(s)
736 * @sht: scsi_host_template to use when registering the host
737 *
738 * This is the counterpart of ata_host_activate() for SFF ATA
739 * hosts. This separate helper is necessary because SFF hosts
740 * use two separate interrupts in legacy mode.
741 *
742 * LOCKING:
743 * Inherited from calling layer (may sleep).
744 *
745 * RETURNS:
746 * 0 on success, -errno otherwise.
747 */
748int ata_pci_activate_sff_host(struct ata_host *host,
749 irq_handler_t irq_handler,
750 struct scsi_host_template *sht)
751{
752 struct device *dev = host->dev;
753 struct pci_dev *pdev = to_pci_dev(dev);
754 const char *drv_name = dev_driver_string(host->dev);
755 int legacy_mode = 0, rc;
756
757 rc = ata_host_start(host);
758 if (rc)
759 return rc;
760
761 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
762 u8 tmp8, mask;
763
764 /* TODO: What if one channel is in native mode ... */
765 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
766 mask = (1 << 2) | (1 << 0);
767 if ((tmp8 & mask) != mask)
768 legacy_mode = 1;
769#if defined(CONFIG_NO_ATA_LEGACY)
770 /* Some platforms with PCI limits cannot address compat
771 port space. In that case we punt if their firmware has
772 left a device in compatibility mode */
773 if (legacy_mode) {
774 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
775 return -EOPNOTSUPP;
776 }
777#endif
778 }
779
780 if (!devres_open_group(dev, NULL, GFP_KERNEL))
781 return -ENOMEM;
782
783 if (!legacy_mode && pdev->irq) {
784 rc = devm_request_irq(dev, pdev->irq, irq_handler,
785 IRQF_SHARED, drv_name, host);
786 if (rc)
787 goto out;
788
789 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
790 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
791 } else if (legacy_mode) {
792 if (!ata_port_is_dummy(host->ports[0])) {
793 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
794 irq_handler, IRQF_SHARED,
795 drv_name, host);
796 if (rc)
797 goto out;
798
799 ata_port_desc(host->ports[0], "irq %d",
800 ATA_PRIMARY_IRQ(pdev));
801 }
802
803 if (!ata_port_is_dummy(host->ports[1])) {
804 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
805 irq_handler, IRQF_SHARED,
806 drv_name, host);
807 if (rc)
808 goto out;
809
810 ata_port_desc(host->ports[1], "irq %d",
811 ATA_SECONDARY_IRQ(pdev));
812 }
813 }
814
815 rc = ata_host_register(host, sht);
816 out:
817 if (rc == 0)
818 devres_remove_group(dev, NULL);
819 else
820 devres_release_group(dev, NULL);
821
822 return rc;
823}
824
825/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500826 * ata_pci_init_one - Initialize/register PCI IDE host controller
827 * @pdev: Controller to be initialized
Tejun Heo1626aeb2007-05-04 12:43:58 +0200828 * @ppi: array of port_info, must be enough for two ports
Tejun Heo1bd5b712008-03-25 12:22:49 +0900829 * @sht: scsi_host_template to use when registering the host
Tejun Heo887125e2008-03-25 12:22:49 +0900830 * @host_priv: host private_data
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500831 *
832 * This is a helper function which can be called from a driver's
833 * xxx_init_one() probe function if the hardware uses traditional
834 * IDE taskfile registers.
835 *
836 * This function calls pci_enable_device(), reserves its register
837 * regions, sets the dma mask, enables bus master mode, and calls
838 * ata_device_add()
839 *
Alan Cox2ec7df02006-08-10 16:59:10 +0900840 * ASSUMPTION:
841 * Nobody makes a single channel controller that appears solely as
842 * the secondary legacy port on PCI.
843 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500844 * LOCKING:
845 * Inherited from PCI layer (may sleep).
846 *
847 * RETURNS:
848 * Zero on success, negative on errno-based value on error.
849 */
Tejun Heo1626aeb2007-05-04 12:43:58 +0200850int ata_pci_init_one(struct pci_dev *pdev,
Tejun Heo1bd5b712008-03-25 12:22:49 +0900851 const struct ata_port_info * const * ppi,
Tejun Heo887125e2008-03-25 12:22:49 +0900852 struct scsi_host_template *sht, void *host_priv)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500853{
Tejun Heof0d36ef2007-01-20 16:00:28 +0900854 struct device *dev = &pdev->dev;
Tejun Heo1626aeb2007-05-04 12:43:58 +0200855 const struct ata_port_info *pi = NULL;
Tejun Heo0f834de2007-04-17 23:44:07 +0900856 struct ata_host *host = NULL;
Tejun Heo1626aeb2007-05-04 12:43:58 +0200857 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500858
859 DPRINTK("ENTER\n");
860
Tejun Heo1626aeb2007-05-04 12:43:58 +0200861 /* look up the first valid port_info */
862 for (i = 0; i < 2 && ppi[i]; i++) {
863 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
864 pi = ppi[i];
865 break;
866 }
867 }
868
869 if (!pi) {
870 dev_printk(KERN_ERR, &pdev->dev,
871 "no valid port_info specified\n");
872 return -EINVAL;
873 }
874
Tejun Heof0d36ef2007-01-20 16:00:28 +0900875 if (!devres_open_group(dev, NULL, GFP_KERNEL))
876 return -ENOMEM;
877
Tejun Heof0d36ef2007-01-20 16:00:28 +0900878 rc = pcim_enable_device(pdev);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500879 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +0900880 goto out;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500881
Tejun Heo4e6b79f2008-01-18 18:36:28 +0900882 /* prepare and activate SFF host */
Tejun Heod583bc12007-07-04 18:02:07 +0900883 rc = ata_pci_prepare_sff_host(pdev, ppi, &host);
884 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +0900885 goto out;
Tejun Heo887125e2008-03-25 12:22:49 +0900886 host->private_data = host_priv;
Tejun Heod491b272007-04-17 23:44:07 +0900887
Tejun Heod491b272007-04-17 23:44:07 +0900888 pci_set_master(pdev);
Tejun Heo1bd5b712008-03-25 12:22:49 +0900889 rc = ata_pci_activate_sff_host(host, ata_interrupt, sht);
Tejun Heo4e6b79f2008-01-18 18:36:28 +0900890 out:
891 if (rc == 0)
892 devres_remove_group(&pdev->dev, NULL);
893 else
894 devres_release_group(&pdev->dev, NULL);
Tejun Heod491b272007-04-17 23:44:07 +0900895
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500896 return rc;
897}
898
Alan Coxd33d44f2006-03-21 15:59:57 +0000899/**
900 * ata_pci_clear_simplex - attempt to kick device out of simplex
901 * @pdev: PCI device
902 *
903 * Some PCI ATA devices report simplex mode but in fact can be told to
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200904 * enter non simplex mode. This implements the necessary logic to
Alan Coxd33d44f2006-03-21 15:59:57 +0000905 * perform the task on such devices. Calling it on other devices will
906 * have -undefined- behaviour.
907 */
908
909int ata_pci_clear_simplex(struct pci_dev *pdev)
910{
911 unsigned long bmdma = pci_resource_start(pdev, 4);
912 u8 simplex;
913
914 if (bmdma == 0)
915 return -ENOENT;
916
917 simplex = inb(bmdma + 0x02);
918 outb(simplex & 0x60, bmdma + 0x02);
919 simplex = inb(bmdma + 0x02);
920 if (simplex & 0x80)
921 return -EOPNOTSUPP;
922 return 0;
923}
924
Alan Coxa76b62c2007-03-09 09:34:07 -0500925unsigned long ata_pci_default_filter(struct ata_device *adev, unsigned long xfer_mask)
Alan Coxd33d44f2006-03-21 15:59:57 +0000926{
927 /* Filter out DMA modes if the device has been configured by
928 the BIOS as PIO only */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500929
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700930 if (adev->link->ap->ioaddr.bmdma_addr == NULL)
Alan Coxd33d44f2006-03-21 15:59:57 +0000931 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
932 return xfer_mask;
933}
934
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500935#endif /* CONFIG_PCI */
936