blob: c59ad76c84b1ebe2e3e28b7a08166ffb5f0313fd [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>
Tejun Heo624d5c52008-03-25 22:16:41 +090038#include <linux/highmem.h>
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050039
40#include "libata.h"
41
Tejun Heo624d5c52008-03-25 22:16:41 +090042const struct ata_port_operations ata_sff_port_ops = {
43 .inherits = &ata_base_port_ops,
44
Tejun Heo9363c382008-04-07 22:47:16 +090045 .qc_prep = ata_sff_qc_prep,
46 .qc_issue = ata_sff_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +090047 .qc_fill_rtf = ata_sff_qc_fill_rtf,
Tejun Heo624d5c52008-03-25 22:16:41 +090048
Tejun Heo9363c382008-04-07 22:47:16 +090049 .freeze = ata_sff_freeze,
50 .thaw = ata_sff_thaw,
Tejun Heo0aa11132008-04-07 22:47:18 +090051 .prereset = ata_sff_prereset,
Tejun Heo9363c382008-04-07 22:47:16 +090052 .softreset = ata_sff_softreset,
Tejun Heo57c9efd2008-04-07 22:47:19 +090053 .hardreset = sata_sff_hardreset,
Tejun Heo203c75b2008-04-07 22:47:18 +090054 .postreset = ata_sff_postreset,
Tejun Heo9363c382008-04-07 22:47:16 +090055 .error_handler = ata_sff_error_handler,
56 .post_internal_cmd = ata_sff_post_internal_cmd,
Tejun Heo624d5c52008-03-25 22:16:41 +090057
Tejun Heo5682ed32008-04-07 22:47:16 +090058 .sff_dev_select = ata_sff_dev_select,
59 .sff_check_status = ata_sff_check_status,
60 .sff_tf_load = ata_sff_tf_load,
61 .sff_tf_read = ata_sff_tf_read,
62 .sff_exec_command = ata_sff_exec_command,
63 .sff_data_xfer = ata_sff_data_xfer,
64 .sff_irq_on = ata_sff_irq_on,
Tejun Heo288623a2008-04-07 22:47:17 +090065 .sff_irq_clear = ata_sff_irq_clear,
Tejun Heo624d5c52008-03-25 22:16:41 +090066
67 .port_start = ata_sff_port_start,
68};
Alan Cox0fe40ff2009-01-05 14:16:13 +000069EXPORT_SYMBOL_GPL(ata_sff_port_ops);
Tejun Heo624d5c52008-03-25 22:16:41 +090070
71const struct ata_port_operations ata_bmdma_port_ops = {
72 .inherits = &ata_sff_port_ops,
73
Tejun Heo9363c382008-04-07 22:47:16 +090074 .mode_filter = ata_bmdma_mode_filter,
Tejun Heo624d5c52008-03-25 22:16:41 +090075
76 .bmdma_setup = ata_bmdma_setup,
77 .bmdma_start = ata_bmdma_start,
78 .bmdma_stop = ata_bmdma_stop,
79 .bmdma_status = ata_bmdma_status,
Tejun Heo624d5c52008-03-25 22:16:41 +090080};
Alan Cox0fe40ff2009-01-05 14:16:13 +000081EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
Tejun Heo624d5c52008-03-25 22:16:41 +090082
Alan Cox871af122009-01-05 14:16:39 +000083const struct ata_port_operations ata_bmdma32_port_ops = {
84 .inherits = &ata_bmdma_port_ops,
85
86 .sff_data_xfer = ata_sff_data_xfer32,
87};
88EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
89
Tejun Heo624d5c52008-03-25 22:16:41 +090090/**
91 * ata_fill_sg - Fill PCI IDE PRD table
92 * @qc: Metadata associated with taskfile to be transferred
93 *
94 * Fill PCI IDE PRD (scatter-gather) table with segments
95 * associated with the current disk command.
96 *
97 * LOCKING:
98 * spin_lock_irqsave(host lock)
99 *
100 */
101static void ata_fill_sg(struct ata_queued_cmd *qc)
102{
103 struct ata_port *ap = qc->ap;
104 struct scatterlist *sg;
105 unsigned int si, pi;
106
107 pi = 0;
108 for_each_sg(qc->sg, sg, qc->n_elem, si) {
109 u32 addr, offset;
110 u32 sg_len, len;
111
112 /* determine if physical DMA addr spans 64K boundary.
113 * Note h/w doesn't support 64-bit, so we unconditionally
114 * truncate dma_addr_t to u32.
115 */
116 addr = (u32) sg_dma_address(sg);
117 sg_len = sg_dma_len(sg);
118
119 while (sg_len) {
120 offset = addr & 0xffff;
121 len = sg_len;
122 if ((offset + sg_len) > 0x10000)
123 len = 0x10000 - offset;
124
125 ap->prd[pi].addr = cpu_to_le32(addr);
126 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
127 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
128
129 pi++;
130 sg_len -= len;
131 addr += len;
132 }
133 }
134
135 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
136}
137
138/**
139 * ata_fill_sg_dumb - Fill PCI IDE PRD table
140 * @qc: Metadata associated with taskfile to be transferred
141 *
142 * Fill PCI IDE PRD (scatter-gather) table with segments
143 * associated with the current disk command. Perform the fill
144 * so that we avoid writing any length 64K records for
145 * controllers that don't follow the spec.
146 *
147 * LOCKING:
148 * spin_lock_irqsave(host lock)
149 *
150 */
151static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
152{
153 struct ata_port *ap = qc->ap;
154 struct scatterlist *sg;
155 unsigned int si, pi;
156
157 pi = 0;
158 for_each_sg(qc->sg, sg, qc->n_elem, si) {
159 u32 addr, offset;
160 u32 sg_len, len, blen;
161
162 /* determine if physical DMA addr spans 64K boundary.
163 * Note h/w doesn't support 64-bit, so we unconditionally
164 * truncate dma_addr_t to u32.
165 */
166 addr = (u32) sg_dma_address(sg);
167 sg_len = sg_dma_len(sg);
168
169 while (sg_len) {
170 offset = addr & 0xffff;
171 len = sg_len;
172 if ((offset + sg_len) > 0x10000)
173 len = 0x10000 - offset;
174
175 blen = len & 0xffff;
176 ap->prd[pi].addr = cpu_to_le32(addr);
177 if (blen == 0) {
Alan Cox0fe40ff2009-01-05 14:16:13 +0000178 /* Some PATA chipsets like the CS5530 can't
179 cope with 0x0000 meaning 64K as the spec
180 says */
Tejun Heo624d5c52008-03-25 22:16:41 +0900181 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
182 blen = 0x8000;
183 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
184 }
185 ap->prd[pi].flags_len = cpu_to_le32(blen);
186 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
187
188 pi++;
189 sg_len -= len;
190 addr += len;
191 }
192 }
193
194 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
195}
196
197/**
Tejun Heo9363c382008-04-07 22:47:16 +0900198 * ata_sff_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900199 * @qc: Metadata associated with taskfile to be prepared
200 *
201 * Prepare ATA taskfile for submission.
202 *
203 * LOCKING:
204 * spin_lock_irqsave(host lock)
205 */
Tejun Heo9363c382008-04-07 22:47:16 +0900206void ata_sff_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900207{
208 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
209 return;
210
211 ata_fill_sg(qc);
212}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000213EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900214
215/**
Tejun Heo9363c382008-04-07 22:47:16 +0900216 * ata_sff_dumb_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900217 * @qc: Metadata associated with taskfile to be prepared
218 *
219 * Prepare ATA taskfile for submission.
220 *
221 * LOCKING:
222 * spin_lock_irqsave(host lock)
223 */
Tejun Heo9363c382008-04-07 22:47:16 +0900224void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900225{
226 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
227 return;
228
229 ata_fill_sg_dumb(qc);
230}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000231EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900232
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500233/**
Tejun Heo9363c382008-04-07 22:47:16 +0900234 * ata_sff_check_status - Read device status reg & clear interrupt
Tejun Heo272f7882008-03-25 22:16:40 +0900235 * @ap: port where the device is
236 *
237 * Reads ATA taskfile status register for currently-selected device
238 * and return its value. This also clears pending interrupts
239 * from this device
240 *
241 * LOCKING:
242 * Inherited from caller.
243 */
Tejun Heo9363c382008-04-07 22:47:16 +0900244u8 ata_sff_check_status(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900245{
246 return ioread8(ap->ioaddr.status_addr);
247}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000248EXPORT_SYMBOL_GPL(ata_sff_check_status);
Tejun Heo272f7882008-03-25 22:16:40 +0900249
250/**
Tejun Heo9363c382008-04-07 22:47:16 +0900251 * ata_sff_altstatus - Read device alternate status reg
Tejun Heo272f7882008-03-25 22:16:40 +0900252 * @ap: port where the device is
253 *
254 * Reads ATA taskfile alternate status register for
255 * currently-selected device and return its value.
256 *
257 * Note: may NOT be used as the check_altstatus() entry in
258 * ata_port_operations.
259 *
260 * LOCKING:
261 * Inherited from caller.
262 */
Alan Coxa57c1ba2008-05-29 22:10:58 +0100263static u8 ata_sff_altstatus(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900264{
Tejun Heo5682ed32008-04-07 22:47:16 +0900265 if (ap->ops->sff_check_altstatus)
266 return ap->ops->sff_check_altstatus(ap);
Tejun Heo272f7882008-03-25 22:16:40 +0900267
268 return ioread8(ap->ioaddr.altstatus_addr);
269}
270
271/**
Alan Coxa57c1ba2008-05-29 22:10:58 +0100272 * ata_sff_irq_status - Check if the device is busy
273 * @ap: port where the device is
274 *
275 * Determine if the port is currently busy. Uses altstatus
276 * if available in order to avoid clearing shared IRQ status
277 * when finding an IRQ source. Non ctl capable devices don't
278 * share interrupt lines fortunately for us.
279 *
280 * LOCKING:
281 * Inherited from caller.
282 */
283static u8 ata_sff_irq_status(struct ata_port *ap)
284{
285 u8 status;
286
287 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
288 status = ata_sff_altstatus(ap);
289 /* Not us: We are busy */
290 if (status & ATA_BUSY)
Alan Cox0fe40ff2009-01-05 14:16:13 +0000291 return status;
Alan Coxa57c1ba2008-05-29 22:10:58 +0100292 }
293 /* Clear INTRQ latch */
Hugh Dickins6311c902008-06-05 14:44:39 +0100294 status = ap->ops->sff_check_status(ap);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100295 return status;
296}
297
298/**
299 * ata_sff_sync - Flush writes
300 * @ap: Port to wait for.
301 *
302 * CAUTION:
303 * If we have an mmio device with no ctl and no altstatus
304 * method this will fail. No such devices are known to exist.
305 *
306 * LOCKING:
307 * Inherited from caller.
308 */
309
310static void ata_sff_sync(struct ata_port *ap)
311{
312 if (ap->ops->sff_check_altstatus)
313 ap->ops->sff_check_altstatus(ap);
314 else if (ap->ioaddr.altstatus_addr)
315 ioread8(ap->ioaddr.altstatus_addr);
316}
317
318/**
319 * ata_sff_pause - Flush writes and wait 400nS
320 * @ap: Port to pause for.
321 *
322 * CAUTION:
323 * If we have an mmio device with no ctl and no altstatus
324 * method this will fail. No such devices are known to exist.
325 *
326 * LOCKING:
327 * Inherited from caller.
328 */
329
330void ata_sff_pause(struct ata_port *ap)
331{
332 ata_sff_sync(ap);
333 ndelay(400);
334}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000335EXPORT_SYMBOL_GPL(ata_sff_pause);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100336
337/**
338 * ata_sff_dma_pause - Pause before commencing DMA
339 * @ap: Port to pause for.
340 *
341 * Perform I/O fencing and ensure sufficient cycle delays occur
342 * for the HDMA1:0 transition
343 */
Alan Cox0fe40ff2009-01-05 14:16:13 +0000344
Alan Coxa57c1ba2008-05-29 22:10:58 +0100345void ata_sff_dma_pause(struct ata_port *ap)
346{
347 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
348 /* An altstatus read will cause the needed delay without
349 messing up the IRQ status */
350 ata_sff_altstatus(ap);
351 return;
352 }
353 /* There are no DMA controllers without ctl. BUG here to ensure
354 we never violate the HDMA1:0 transition timing and risk
355 corruption. */
356 BUG();
357}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000358EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100359
360/**
Tejun Heo9363c382008-04-07 22:47:16 +0900361 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
Tejun Heo624d5c52008-03-25 22:16:41 +0900362 * @ap: port containing status register to be polled
Tejun Heo341c2c92008-05-20 02:17:51 +0900363 * @tmout_pat: impatience timeout in msecs
364 * @tmout: overall timeout in msecs
Tejun Heo624d5c52008-03-25 22:16:41 +0900365 *
366 * Sleep until ATA Status register bit BSY clears,
367 * or a timeout occurs.
368 *
369 * LOCKING:
370 * Kernel thread context (may sleep).
371 *
372 * RETURNS:
373 * 0 on success, -errno otherwise.
374 */
Tejun Heo9363c382008-04-07 22:47:16 +0900375int ata_sff_busy_sleep(struct ata_port *ap,
376 unsigned long tmout_pat, unsigned long tmout)
Tejun Heo624d5c52008-03-25 22:16:41 +0900377{
378 unsigned long timer_start, timeout;
379 u8 status;
380
Tejun Heo9363c382008-04-07 22:47:16 +0900381 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
Tejun Heo624d5c52008-03-25 22:16:41 +0900382 timer_start = jiffies;
Tejun Heo341c2c92008-05-20 02:17:51 +0900383 timeout = ata_deadline(timer_start, tmout_pat);
Tejun Heo624d5c52008-03-25 22:16:41 +0900384 while (status != 0xff && (status & ATA_BUSY) &&
385 time_before(jiffies, timeout)) {
386 msleep(50);
Tejun Heo9363c382008-04-07 22:47:16 +0900387 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
Tejun Heo624d5c52008-03-25 22:16:41 +0900388 }
389
390 if (status != 0xff && (status & ATA_BUSY))
391 ata_port_printk(ap, KERN_WARNING,
392 "port is slow to respond, please be patient "
393 "(Status 0x%x)\n", status);
394
Tejun Heo341c2c92008-05-20 02:17:51 +0900395 timeout = ata_deadline(timer_start, tmout);
Tejun Heo624d5c52008-03-25 22:16:41 +0900396 while (status != 0xff && (status & ATA_BUSY) &&
397 time_before(jiffies, timeout)) {
398 msleep(50);
Tejun Heo5682ed32008-04-07 22:47:16 +0900399 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900400 }
401
402 if (status == 0xff)
403 return -ENODEV;
404
405 if (status & ATA_BUSY) {
406 ata_port_printk(ap, KERN_ERR, "port failed to respond "
407 "(%lu secs, Status 0x%x)\n",
Tejun Heo341c2c92008-05-20 02:17:51 +0900408 DIV_ROUND_UP(tmout, 1000), status);
Tejun Heo624d5c52008-03-25 22:16:41 +0900409 return -EBUSY;
410 }
411
412 return 0;
413}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000414EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900415
Tejun Heoaa2731a2008-04-07 22:47:19 +0900416static int ata_sff_check_ready(struct ata_link *link)
417{
418 u8 status = link->ap->ops->sff_check_status(link->ap);
419
Tejun Heo78ab88f2008-05-01 23:41:41 +0900420 return ata_check_ready(status);
Tejun Heoaa2731a2008-04-07 22:47:19 +0900421}
422
Tejun Heo624d5c52008-03-25 22:16:41 +0900423/**
Tejun Heo9363c382008-04-07 22:47:16 +0900424 * ata_sff_wait_ready - sleep until BSY clears, or timeout
Tejun Heo705e76b2008-04-07 22:47:19 +0900425 * @link: SFF link to wait ready status for
Tejun Heo624d5c52008-03-25 22:16:41 +0900426 * @deadline: deadline jiffies for the operation
427 *
428 * Sleep until ATA Status register bit BSY clears, or timeout
429 * occurs.
430 *
431 * LOCKING:
432 * Kernel thread context (may sleep).
433 *
434 * RETURNS:
435 * 0 on success, -errno otherwise.
436 */
Tejun Heo705e76b2008-04-07 22:47:19 +0900437int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +0900438{
Tejun Heoaa2731a2008-04-07 22:47:19 +0900439 return ata_wait_ready(link, deadline, ata_sff_check_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900440}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000441EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900442
443/**
Tejun Heo9363c382008-04-07 22:47:16 +0900444 * ata_sff_dev_select - Select device 0/1 on ATA bus
Tejun Heo624d5c52008-03-25 22:16:41 +0900445 * @ap: ATA channel to manipulate
446 * @device: ATA device (numbered from zero) to select
447 *
448 * Use the method defined in the ATA specification to
449 * make either device 0, or device 1, active on the
450 * ATA channel. Works with both PIO and MMIO.
451 *
452 * May be used as the dev_select() entry in ata_port_operations.
453 *
454 * LOCKING:
455 * caller.
456 */
Tejun Heo9363c382008-04-07 22:47:16 +0900457void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
Tejun Heo624d5c52008-03-25 22:16:41 +0900458{
459 u8 tmp;
460
461 if (device == 0)
462 tmp = ATA_DEVICE_OBS;
463 else
464 tmp = ATA_DEVICE_OBS | ATA_DEV1;
465
466 iowrite8(tmp, ap->ioaddr.device_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900467 ata_sff_pause(ap); /* needed; also flushes, for mmio */
Tejun Heo624d5c52008-03-25 22:16:41 +0900468}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000469EXPORT_SYMBOL_GPL(ata_sff_dev_select);
Tejun Heo624d5c52008-03-25 22:16:41 +0900470
471/**
472 * ata_dev_select - Select device 0/1 on ATA bus
473 * @ap: ATA channel to manipulate
474 * @device: ATA device (numbered from zero) to select
475 * @wait: non-zero to wait for Status register BSY bit to clear
476 * @can_sleep: non-zero if context allows sleeping
477 *
478 * Use the method defined in the ATA specification to
479 * make either device 0, or device 1, active on the
480 * ATA channel.
481 *
Tejun Heo9363c382008-04-07 22:47:16 +0900482 * This is a high-level version of ata_sff_dev_select(), which
483 * additionally provides the services of inserting the proper
484 * pauses and status polling, where needed.
Tejun Heo624d5c52008-03-25 22:16:41 +0900485 *
486 * LOCKING:
487 * caller.
488 */
489void ata_dev_select(struct ata_port *ap, unsigned int device,
490 unsigned int wait, unsigned int can_sleep)
491{
492 if (ata_msg_probe(ap))
493 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
494 "device %u, wait %u\n", device, wait);
495
496 if (wait)
497 ata_wait_idle(ap);
498
Tejun Heo5682ed32008-04-07 22:47:16 +0900499 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +0900500
501 if (wait) {
502 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
503 msleep(150);
504 ata_wait_idle(ap);
505 }
506}
507
508/**
Tejun Heo9363c382008-04-07 22:47:16 +0900509 * ata_sff_irq_on - Enable interrupts on a port.
Tejun Heo90088bb2006-10-09 11:10:26 +0900510 * @ap: Port on which interrupts are enabled.
511 *
512 * Enable interrupts on a legacy IDE device using MMIO or PIO,
513 * wait for idle, clear any pending interrupts.
514 *
515 * LOCKING:
516 * Inherited from caller.
517 */
Tejun Heo9363c382008-04-07 22:47:16 +0900518u8 ata_sff_irq_on(struct ata_port *ap)
Tejun Heo90088bb2006-10-09 11:10:26 +0900519{
520 struct ata_ioports *ioaddr = &ap->ioaddr;
521 u8 tmp;
522
523 ap->ctl &= ~ATA_NIEN;
524 ap->last_ctl = ap->ctl;
525
Tejun Heof659f0e42008-03-06 13:12:54 +0900526 if (ioaddr->ctl_addr)
527 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo90088bb2006-10-09 11:10:26 +0900528 tmp = ata_wait_idle(ap);
529
Tejun Heo5682ed32008-04-07 22:47:16 +0900530 ap->ops->sff_irq_clear(ap);
Tejun Heo90088bb2006-10-09 11:10:26 +0900531
532 return tmp;
533}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000534EXPORT_SYMBOL_GPL(ata_sff_irq_on);
Tejun Heo90088bb2006-10-09 11:10:26 +0900535
536/**
Tejun Heo9363c382008-04-07 22:47:16 +0900537 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
Tejun Heo272f7882008-03-25 22:16:40 +0900538 * @ap: Port associated with this ATA transaction.
539 *
540 * Clear interrupt and error flags in DMA status register.
541 *
542 * May be used as the irq_clear() entry in ata_port_operations.
543 *
544 * LOCKING:
545 * spin_lock_irqsave(host lock)
546 */
Tejun Heo9363c382008-04-07 22:47:16 +0900547void ata_sff_irq_clear(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900548{
549 void __iomem *mmio = ap->ioaddr.bmdma_addr;
550
551 if (!mmio)
552 return;
553
554 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
555}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000556EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
Tejun Heo272f7882008-03-25 22:16:40 +0900557
558/**
Tejun Heo9363c382008-04-07 22:47:16 +0900559 * ata_sff_tf_load - send taskfile registers to host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500560 * @ap: Port to which output is sent
561 * @tf: ATA taskfile register set
562 *
563 * Outputs ATA taskfile to standard ATA host controller.
564 *
565 * LOCKING:
566 * Inherited from caller.
567 */
Tejun Heo9363c382008-04-07 22:47:16 +0900568void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500569{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900570 struct ata_ioports *ioaddr = &ap->ioaddr;
571 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
572
573 if (tf->ctl != ap->last_ctl) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900574 if (ioaddr->ctl_addr)
575 iowrite8(tf->ctl, ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900576 ap->last_ctl = tf->ctl;
577 ata_wait_idle(ap);
578 }
579
580 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900581 WARN_ON(!ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900582 iowrite8(tf->hob_feature, ioaddr->feature_addr);
583 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
584 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
585 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
586 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
587 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
588 tf->hob_feature,
589 tf->hob_nsect,
590 tf->hob_lbal,
591 tf->hob_lbam,
592 tf->hob_lbah);
593 }
594
595 if (is_addr) {
596 iowrite8(tf->feature, ioaddr->feature_addr);
597 iowrite8(tf->nsect, ioaddr->nsect_addr);
598 iowrite8(tf->lbal, ioaddr->lbal_addr);
599 iowrite8(tf->lbam, ioaddr->lbam_addr);
600 iowrite8(tf->lbah, ioaddr->lbah_addr);
601 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
602 tf->feature,
603 tf->nsect,
604 tf->lbal,
605 tf->lbam,
606 tf->lbah);
607 }
608
609 if (tf->flags & ATA_TFLAG_DEVICE) {
610 iowrite8(tf->device, ioaddr->device_addr);
611 VPRINTK("device 0x%X\n", tf->device);
612 }
613
614 ata_wait_idle(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500615}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000616EXPORT_SYMBOL_GPL(ata_sff_tf_load);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500617
618/**
Tejun Heo9363c382008-04-07 22:47:16 +0900619 * ata_sff_tf_read - input device's ATA taskfile shadow registers
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500620 * @ap: Port from which input is read
621 * @tf: ATA taskfile register set for storing input
622 *
623 * Reads ATA taskfile registers for currently-selected device
Alan Cox76548ed2007-11-19 14:34:56 +0000624 * into @tf. Assumes the device has a fully SFF compliant task file
625 * layout and behaviour. If you device does not (eg has a different
626 * status method) then you will need to provide a replacement tf_read
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500627 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500628 * LOCKING:
629 * Inherited from caller.
630 */
Tejun Heo9363c382008-04-07 22:47:16 +0900631void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500632{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900633 struct ata_ioports *ioaddr = &ap->ioaddr;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500634
Tejun Heo9363c382008-04-07 22:47:16 +0900635 tf->command = ata_sff_check_status(ap);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900636 tf->feature = ioread8(ioaddr->error_addr);
637 tf->nsect = ioread8(ioaddr->nsect_addr);
638 tf->lbal = ioread8(ioaddr->lbal_addr);
639 tf->lbam = ioread8(ioaddr->lbam_addr);
640 tf->lbah = ioread8(ioaddr->lbah_addr);
641 tf->device = ioread8(ioaddr->device_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500642
Tejun Heo0d5ff562007-02-01 15:06:36 +0900643 if (tf->flags & ATA_TFLAG_LBA48) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900644 if (likely(ioaddr->ctl_addr)) {
645 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
646 tf->hob_feature = ioread8(ioaddr->error_addr);
647 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
648 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
649 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
650 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
651 iowrite8(tf->ctl, ioaddr->ctl_addr);
652 ap->last_ctl = tf->ctl;
653 } else
654 WARN_ON(1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900655 }
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500656}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000657EXPORT_SYMBOL_GPL(ata_sff_tf_read);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500658
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500659/**
Tejun Heo9363c382008-04-07 22:47:16 +0900660 * ata_sff_exec_command - issue ATA command to host controller
Tejun Heo272f7882008-03-25 22:16:40 +0900661 * @ap: port to which command is being issued
662 * @tf: ATA taskfile register set
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500663 *
Tejun Heo272f7882008-03-25 22:16:40 +0900664 * Issues ATA command, with proper synchronization with interrupt
665 * handler / other threads.
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500666 *
667 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400668 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500669 */
Tejun Heo9363c382008-04-07 22:47:16 +0900670void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500671{
Tejun Heo272f7882008-03-25 22:16:40 +0900672 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500673
Tejun Heo272f7882008-03-25 22:16:40 +0900674 iowrite8(tf->command, ap->ioaddr.command_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900675 ata_sff_pause(ap);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500676}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000677EXPORT_SYMBOL_GPL(ata_sff_exec_command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500678
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900679/**
Tejun Heo624d5c52008-03-25 22:16:41 +0900680 * ata_tf_to_host - issue ATA taskfile to host controller
681 * @ap: port to which command is being issued
682 * @tf: ATA taskfile register set
683 *
684 * Issues ATA taskfile register set to ATA host controller,
685 * with proper synchronization with interrupt handler and
686 * other threads.
687 *
688 * LOCKING:
689 * spin_lock_irqsave(host lock)
690 */
691static inline void ata_tf_to_host(struct ata_port *ap,
692 const struct ata_taskfile *tf)
693{
Tejun Heo5682ed32008-04-07 22:47:16 +0900694 ap->ops->sff_tf_load(ap, tf);
695 ap->ops->sff_exec_command(ap, tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900696}
697
698/**
Tejun Heo9363c382008-04-07 22:47:16 +0900699 * ata_sff_data_xfer - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900700 * @dev: device to target
701 * @buf: data buffer
702 * @buflen: buffer length
703 * @rw: read/write
704 *
705 * Transfer data from/to the device data register by PIO.
706 *
707 * LOCKING:
708 * Inherited from caller.
709 *
710 * RETURNS:
711 * Bytes consumed.
712 */
Tejun Heo9363c382008-04-07 22:47:16 +0900713unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
714 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900715{
716 struct ata_port *ap = dev->link->ap;
717 void __iomem *data_addr = ap->ioaddr.data_addr;
718 unsigned int words = buflen >> 1;
719
720 /* Transfer multiple of 2 bytes */
721 if (rw == READ)
722 ioread16_rep(data_addr, buf, words);
723 else
724 iowrite16_rep(data_addr, buf, words);
725
726 /* Transfer trailing 1 byte, if any. */
727 if (unlikely(buflen & 0x01)) {
728 __le16 align_buf[1] = { 0 };
729 unsigned char *trailing_buf = buf + buflen - 1;
730
731 if (rw == READ) {
732 align_buf[0] = cpu_to_le16(ioread16(data_addr));
733 memcpy(trailing_buf, align_buf, 1);
734 } else {
735 memcpy(align_buf, trailing_buf, 1);
736 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
737 }
738 words++;
739 }
740
741 return words << 1;
742}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000743EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
Tejun Heo624d5c52008-03-25 22:16:41 +0900744
745/**
Alan Cox871af122009-01-05 14:16:39 +0000746 * ata_sff_data_xfer32 - Transfer data by PIO
747 * @dev: device to target
748 * @buf: data buffer
749 * @buflen: buffer length
750 * @rw: read/write
751 *
752 * Transfer data from/to the device data register by PIO using 32bit
753 * I/O operations.
754 *
755 * LOCKING:
756 * Inherited from caller.
757 *
758 * RETURNS:
759 * Bytes consumed.
760 */
761
762unsigned int ata_sff_data_xfer32(struct ata_device *dev, unsigned char *buf,
763 unsigned int buflen, int rw)
764{
765 struct ata_port *ap = dev->link->ap;
766 void __iomem *data_addr = ap->ioaddr.data_addr;
767 unsigned int words = buflen >> 2;
768 int slop = buflen & 3;
769
770 /* Transfer multiple of 4 bytes */
771 if (rw == READ)
772 ioread32_rep(data_addr, buf, words);
773 else
774 iowrite32_rep(data_addr, buf, words);
775
776 if (unlikely(slop)) {
777 __le32 pad;
778 if (rw == READ) {
779 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
780 memcpy(buf + buflen - slop, &pad, slop);
781 } else {
782 memcpy(&pad, buf + buflen - slop, slop);
783 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
784 }
785 words++;
786 }
787 return words << 2;
788}
789EXPORT_SYMBOL_GPL(ata_sff_data_xfer32);
790
791/**
Tejun Heo9363c382008-04-07 22:47:16 +0900792 * ata_sff_data_xfer_noirq - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900793 * @dev: device to target
794 * @buf: data buffer
795 * @buflen: buffer length
796 * @rw: read/write
797 *
798 * Transfer data from/to the device data register by PIO. Do the
799 * transfer with interrupts disabled.
800 *
801 * LOCKING:
802 * Inherited from caller.
803 *
804 * RETURNS:
805 * Bytes consumed.
806 */
Tejun Heo9363c382008-04-07 22:47:16 +0900807unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
808 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900809{
810 unsigned long flags;
811 unsigned int consumed;
812
813 local_irq_save(flags);
Tejun Heo9363c382008-04-07 22:47:16 +0900814 consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900815 local_irq_restore(flags);
816
817 return consumed;
818}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000819EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
Tejun Heo624d5c52008-03-25 22:16:41 +0900820
821/**
822 * ata_pio_sector - Transfer a sector of data.
823 * @qc: Command on going
824 *
825 * Transfer qc->sect_size bytes of data from/to the ATA device.
826 *
827 * LOCKING:
828 * Inherited from caller.
829 */
830static void ata_pio_sector(struct ata_queued_cmd *qc)
831{
832 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
833 struct ata_port *ap = qc->ap;
834 struct page *page;
835 unsigned int offset;
836 unsigned char *buf;
837
838 if (qc->curbytes == qc->nbytes - qc->sect_size)
839 ap->hsm_task_state = HSM_ST_LAST;
840
841 page = sg_page(qc->cursg);
842 offset = qc->cursg->offset + qc->cursg_ofs;
843
844 /* get the current page and offset */
845 page = nth_page(page, (offset >> PAGE_SHIFT));
846 offset %= PAGE_SIZE;
847
848 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
849
850 if (PageHighMem(page)) {
851 unsigned long flags;
852
853 /* FIXME: use a bounce buffer */
854 local_irq_save(flags);
855 buf = kmap_atomic(page, KM_IRQ0);
856
857 /* do the actual data transfer */
Tejun Heo5682ed32008-04-07 22:47:16 +0900858 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
859 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900860
861 kunmap_atomic(buf, KM_IRQ0);
862 local_irq_restore(flags);
863 } else {
864 buf = page_address(page);
Tejun Heo5682ed32008-04-07 22:47:16 +0900865 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
866 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900867 }
868
869 qc->curbytes += qc->sect_size;
870 qc->cursg_ofs += qc->sect_size;
871
872 if (qc->cursg_ofs == qc->cursg->length) {
873 qc->cursg = sg_next(qc->cursg);
874 qc->cursg_ofs = 0;
875 }
876}
877
878/**
879 * ata_pio_sectors - Transfer one or many sectors.
880 * @qc: Command on going
881 *
882 * Transfer one or many sectors of data from/to the
883 * ATA device for the DRQ request.
884 *
885 * LOCKING:
886 * Inherited from caller.
887 */
888static void ata_pio_sectors(struct ata_queued_cmd *qc)
889{
890 if (is_multi_taskfile(&qc->tf)) {
891 /* READ/WRITE MULTIPLE */
892 unsigned int nsect;
893
894 WARN_ON(qc->dev->multi_count == 0);
895
896 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
897 qc->dev->multi_count);
898 while (nsect--)
899 ata_pio_sector(qc);
900 } else
901 ata_pio_sector(qc);
902
Alan Coxa57c1ba2008-05-29 22:10:58 +0100903 ata_sff_sync(qc->ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900904}
905
906/**
907 * atapi_send_cdb - Write CDB bytes to hardware
908 * @ap: Port to which ATAPI device is attached.
909 * @qc: Taskfile currently active
910 *
911 * When device has indicated its readiness to accept
912 * a CDB, this function is called. Send the CDB.
913 *
914 * LOCKING:
915 * caller.
916 */
917static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
918{
919 /* send SCSI cdb */
920 DPRINTK("send cdb\n");
921 WARN_ON(qc->dev->cdb_len < 12);
922
Tejun Heo5682ed32008-04-07 22:47:16 +0900923 ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100924 ata_sff_sync(ap);
925 /* FIXME: If the CDB is for DMA do we need to do the transition delay
926 or is bmdma_start guaranteed to do it ? */
Tejun Heo624d5c52008-03-25 22:16:41 +0900927 switch (qc->tf.protocol) {
928 case ATAPI_PROT_PIO:
929 ap->hsm_task_state = HSM_ST;
930 break;
931 case ATAPI_PROT_NODATA:
932 ap->hsm_task_state = HSM_ST_LAST;
933 break;
934 case ATAPI_PROT_DMA:
935 ap->hsm_task_state = HSM_ST_LAST;
936 /* initiate bmdma */
937 ap->ops->bmdma_start(qc);
938 break;
939 }
940}
941
942/**
943 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
944 * @qc: Command on going
945 * @bytes: number of bytes
946 *
947 * Transfer Transfer data from/to the ATAPI device.
948 *
949 * LOCKING:
950 * Inherited from caller.
951 *
952 */
953static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
954{
955 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
956 struct ata_port *ap = qc->ap;
957 struct ata_device *dev = qc->dev;
958 struct ata_eh_info *ehi = &dev->link->eh_info;
959 struct scatterlist *sg;
960 struct page *page;
961 unsigned char *buf;
962 unsigned int offset, count, consumed;
963
964next_sg:
965 sg = qc->cursg;
966 if (unlikely(!sg)) {
967 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
968 "buf=%u cur=%u bytes=%u",
969 qc->nbytes, qc->curbytes, bytes);
970 return -1;
971 }
972
973 page = sg_page(sg);
974 offset = sg->offset + qc->cursg_ofs;
975
976 /* get the current page and offset */
977 page = nth_page(page, (offset >> PAGE_SHIFT));
978 offset %= PAGE_SIZE;
979
980 /* don't overrun current sg */
981 count = min(sg->length - qc->cursg_ofs, bytes);
982
983 /* don't cross page boundaries */
984 count = min(count, (unsigned int)PAGE_SIZE - offset);
985
986 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
987
988 if (PageHighMem(page)) {
989 unsigned long flags;
990
991 /* FIXME: use bounce buffer */
992 local_irq_save(flags);
993 buf = kmap_atomic(page, KM_IRQ0);
994
995 /* do the actual data transfer */
Alan Cox0fe40ff2009-01-05 14:16:13 +0000996 consumed = ap->ops->sff_data_xfer(dev, buf + offset,
997 count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900998
999 kunmap_atomic(buf, KM_IRQ0);
1000 local_irq_restore(flags);
1001 } else {
1002 buf = page_address(page);
Alan Cox0fe40ff2009-01-05 14:16:13 +00001003 consumed = ap->ops->sff_data_xfer(dev, buf + offset,
1004 count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +09001005 }
1006
1007 bytes -= min(bytes, consumed);
1008 qc->curbytes += count;
1009 qc->cursg_ofs += count;
1010
1011 if (qc->cursg_ofs == sg->length) {
1012 qc->cursg = sg_next(qc->cursg);
1013 qc->cursg_ofs = 0;
1014 }
1015
1016 /* consumed can be larger than count only for the last transfer */
1017 WARN_ON(qc->cursg && count != consumed);
1018
1019 if (bytes)
1020 goto next_sg;
1021 return 0;
1022}
1023
1024/**
1025 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
1026 * @qc: Command on going
1027 *
1028 * Transfer Transfer data from/to the ATAPI device.
1029 *
1030 * LOCKING:
1031 * Inherited from caller.
1032 */
1033static void atapi_pio_bytes(struct ata_queued_cmd *qc)
1034{
1035 struct ata_port *ap = qc->ap;
1036 struct ata_device *dev = qc->dev;
1037 struct ata_eh_info *ehi = &dev->link->eh_info;
1038 unsigned int ireason, bc_lo, bc_hi, bytes;
1039 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
1040
1041 /* Abuse qc->result_tf for temp storage of intermediate TF
1042 * here to save some kernel stack usage.
1043 * For normal completion, qc->result_tf is not relevant. For
1044 * error, qc->result_tf is later overwritten by ata_qc_complete().
1045 * So, the correctness of qc->result_tf is not affected.
1046 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001047 ap->ops->sff_tf_read(ap, &qc->result_tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001048 ireason = qc->result_tf.nsect;
1049 bc_lo = qc->result_tf.lbam;
1050 bc_hi = qc->result_tf.lbah;
1051 bytes = (bc_hi << 8) | bc_lo;
1052
1053 /* shall be cleared to zero, indicating xfer of data */
1054 if (unlikely(ireason & (1 << 0)))
1055 goto atapi_check;
1056
1057 /* make sure transfer direction matches expected */
1058 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
1059 if (unlikely(do_write != i_write))
1060 goto atapi_check;
1061
1062 if (unlikely(!bytes))
1063 goto atapi_check;
1064
1065 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
1066
1067 if (unlikely(__atapi_pio_bytes(qc, bytes)))
1068 goto err_out;
Alan Coxa57c1ba2008-05-29 22:10:58 +01001069 ata_sff_sync(ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +09001070
1071 return;
1072
1073 atapi_check:
1074 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
1075 ireason, bytes);
1076 err_out:
1077 qc->err_mask |= AC_ERR_HSM;
1078 ap->hsm_task_state = HSM_ST_ERR;
1079}
1080
1081/**
1082 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
1083 * @ap: the target ata_port
1084 * @qc: qc on going
1085 *
1086 * RETURNS:
1087 * 1 if ok in workqueue, 0 otherwise.
1088 */
Alan Cox0fe40ff2009-01-05 14:16:13 +00001089static inline int ata_hsm_ok_in_wq(struct ata_port *ap,
1090 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001091{
1092 if (qc->tf.flags & ATA_TFLAG_POLLING)
1093 return 1;
1094
1095 if (ap->hsm_task_state == HSM_ST_FIRST) {
1096 if (qc->tf.protocol == ATA_PROT_PIO &&
Alan Cox0fe40ff2009-01-05 14:16:13 +00001097 (qc->tf.flags & ATA_TFLAG_WRITE))
Tejun Heo624d5c52008-03-25 22:16:41 +09001098 return 1;
1099
1100 if (ata_is_atapi(qc->tf.protocol) &&
Alan Cox0fe40ff2009-01-05 14:16:13 +00001101 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Tejun Heo624d5c52008-03-25 22:16:41 +09001102 return 1;
1103 }
1104
1105 return 0;
1106}
1107
1108/**
1109 * ata_hsm_qc_complete - finish a qc running on standard HSM
1110 * @qc: Command to complete
1111 * @in_wq: 1 if called from workqueue, 0 otherwise
1112 *
1113 * Finish @qc which is running on standard HSM.
1114 *
1115 * LOCKING:
1116 * If @in_wq is zero, spin_lock_irqsave(host lock).
1117 * Otherwise, none on entry and grabs host lock.
1118 */
1119static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
1120{
1121 struct ata_port *ap = qc->ap;
1122 unsigned long flags;
1123
1124 if (ap->ops->error_handler) {
1125 if (in_wq) {
1126 spin_lock_irqsave(ap->lock, flags);
1127
1128 /* EH might have kicked in while host lock is
1129 * released.
1130 */
1131 qc = ata_qc_from_tag(ap, qc->tag);
1132 if (qc) {
1133 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001134 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001135 ata_qc_complete(qc);
1136 } else
1137 ata_port_freeze(ap);
1138 }
1139
1140 spin_unlock_irqrestore(ap->lock, flags);
1141 } else {
1142 if (likely(!(qc->err_mask & AC_ERR_HSM)))
1143 ata_qc_complete(qc);
1144 else
1145 ata_port_freeze(ap);
1146 }
1147 } else {
1148 if (in_wq) {
1149 spin_lock_irqsave(ap->lock, flags);
Tejun Heo5682ed32008-04-07 22:47:16 +09001150 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001151 ata_qc_complete(qc);
1152 spin_unlock_irqrestore(ap->lock, flags);
1153 } else
1154 ata_qc_complete(qc);
1155 }
1156}
1157
1158/**
Tejun Heo9363c382008-04-07 22:47:16 +09001159 * ata_sff_hsm_move - move the HSM to the next state.
Tejun Heo624d5c52008-03-25 22:16:41 +09001160 * @ap: the target ata_port
1161 * @qc: qc on going
1162 * @status: current device status
1163 * @in_wq: 1 if called from workqueue, 0 otherwise
1164 *
1165 * RETURNS:
1166 * 1 when poll next status needed, 0 otherwise.
1167 */
Tejun Heo9363c382008-04-07 22:47:16 +09001168int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
1169 u8 status, int in_wq)
Tejun Heo624d5c52008-03-25 22:16:41 +09001170{
Tejun Heoa836d3e2008-06-28 01:39:43 +09001171 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo624d5c52008-03-25 22:16:41 +09001172 unsigned long flags = 0;
1173 int poll_next;
1174
1175 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
1176
Tejun Heo9363c382008-04-07 22:47:16 +09001177 /* Make sure ata_sff_qc_issue() does not throw things
Tejun Heo624d5c52008-03-25 22:16:41 +09001178 * like DMA polling into the workqueue. Notice that
1179 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1180 */
1181 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
1182
1183fsm_start:
1184 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1185 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
1186
1187 switch (ap->hsm_task_state) {
1188 case HSM_ST_FIRST:
1189 /* Send first data block or PACKET CDB */
1190
1191 /* If polling, we will stay in the work queue after
1192 * sending the data. Otherwise, interrupt handler
1193 * takes over after sending the data.
1194 */
1195 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
1196
1197 /* check device status */
1198 if (unlikely((status & ATA_DRQ) == 0)) {
1199 /* handle BSY=0, DRQ=0 as error */
1200 if (likely(status & (ATA_ERR | ATA_DF)))
1201 /* device stops HSM for abort/error */
1202 qc->err_mask |= AC_ERR_DEV;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001203 else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001204 /* HSM violation. Let EH handle this */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001205 ata_ehi_push_desc(ehi,
1206 "ST_FIRST: !(DRQ|ERR|DF)");
Tejun Heo624d5c52008-03-25 22:16:41 +09001207 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001208 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001209
1210 ap->hsm_task_state = HSM_ST_ERR;
1211 goto fsm_start;
1212 }
1213
1214 /* Device should not ask for data transfer (DRQ=1)
1215 * when it finds something wrong.
1216 * We ignore DRQ here and stop the HSM by
1217 * changing hsm_task_state to HSM_ST_ERR and
1218 * let the EH abort the command or reset the device.
1219 */
1220 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1221 /* Some ATAPI tape drives forget to clear the ERR bit
1222 * when doing the next command (mostly request sense).
1223 * We ignore ERR here to workaround and proceed sending
1224 * the CDB.
1225 */
1226 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001227 ata_ehi_push_desc(ehi, "ST_FIRST: "
1228 "DRQ=1 with device error, "
1229 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001230 qc->err_mask |= AC_ERR_HSM;
1231 ap->hsm_task_state = HSM_ST_ERR;
1232 goto fsm_start;
1233 }
1234 }
1235
1236 /* Send the CDB (atapi) or the first data block (ata pio out).
1237 * During the state transition, interrupt handler shouldn't
1238 * be invoked before the data transfer is complete and
1239 * hsm_task_state is changed. Hence, the following locking.
1240 */
1241 if (in_wq)
1242 spin_lock_irqsave(ap->lock, flags);
1243
1244 if (qc->tf.protocol == ATA_PROT_PIO) {
1245 /* PIO data out protocol.
1246 * send first data block.
1247 */
1248
1249 /* ata_pio_sectors() might change the state
1250 * to HSM_ST_LAST. so, the state is changed here
1251 * before ata_pio_sectors().
1252 */
1253 ap->hsm_task_state = HSM_ST;
1254 ata_pio_sectors(qc);
1255 } else
1256 /* send CDB */
1257 atapi_send_cdb(ap, qc);
1258
1259 if (in_wq)
1260 spin_unlock_irqrestore(ap->lock, flags);
1261
1262 /* if polling, ata_pio_task() handles the rest.
1263 * otherwise, interrupt handler takes over from here.
1264 */
1265 break;
1266
1267 case HSM_ST:
1268 /* complete command or read/write the data register */
1269 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1270 /* ATAPI PIO protocol */
1271 if ((status & ATA_DRQ) == 0) {
1272 /* No more data to transfer or device error.
1273 * Device error will be tagged in HSM_ST_LAST.
1274 */
1275 ap->hsm_task_state = HSM_ST_LAST;
1276 goto fsm_start;
1277 }
1278
1279 /* Device should not ask for data transfer (DRQ=1)
1280 * when it finds something wrong.
1281 * We ignore DRQ here and stop the HSM by
1282 * changing hsm_task_state to HSM_ST_ERR and
1283 * let the EH abort the command or reset the device.
1284 */
1285 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001286 ata_ehi_push_desc(ehi, "ST-ATAPI: "
1287 "DRQ=1 with device error, "
1288 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001289 qc->err_mask |= AC_ERR_HSM;
1290 ap->hsm_task_state = HSM_ST_ERR;
1291 goto fsm_start;
1292 }
1293
1294 atapi_pio_bytes(qc);
1295
1296 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1297 /* bad ireason reported by device */
1298 goto fsm_start;
1299
1300 } else {
1301 /* ATA PIO protocol */
1302 if (unlikely((status & ATA_DRQ) == 0)) {
1303 /* handle BSY=0, DRQ=0 as error */
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001304 if (likely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo624d5c52008-03-25 22:16:41 +09001305 /* device stops HSM for abort/error */
1306 qc->err_mask |= AC_ERR_DEV;
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001307
1308 /* If diagnostic failed and this is
1309 * IDENTIFY, it's likely a phantom
1310 * device. Mark hint.
1311 */
1312 if (qc->dev->horkage &
1313 ATA_HORKAGE_DIAGNOSTIC)
1314 qc->err_mask |=
1315 AC_ERR_NODEV_HINT;
1316 } else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001317 /* HSM violation. Let EH handle this.
1318 * Phantom devices also trigger this
1319 * condition. Mark hint.
1320 */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001321 ata_ehi_push_desc(ehi, "ST-ATA: "
1322 "DRQ=1 with device error, "
1323 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001324 qc->err_mask |= AC_ERR_HSM |
1325 AC_ERR_NODEV_HINT;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001326 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001327
1328 ap->hsm_task_state = HSM_ST_ERR;
1329 goto fsm_start;
1330 }
1331
1332 /* For PIO reads, some devices may ask for
1333 * data transfer (DRQ=1) alone with ERR=1.
1334 * We respect DRQ here and transfer one
1335 * block of junk data before changing the
1336 * hsm_task_state to HSM_ST_ERR.
1337 *
1338 * For PIO writes, ERR=1 DRQ=1 doesn't make
1339 * sense since the data block has been
1340 * transferred to the device.
1341 */
1342 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1343 /* data might be corrputed */
1344 qc->err_mask |= AC_ERR_DEV;
1345
1346 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1347 ata_pio_sectors(qc);
1348 status = ata_wait_idle(ap);
1349 }
1350
Tejun Heoa836d3e2008-06-28 01:39:43 +09001351 if (status & (ATA_BUSY | ATA_DRQ)) {
1352 ata_ehi_push_desc(ehi, "ST-ATA: "
1353 "BUSY|DRQ persists on ERR|DF, "
1354 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001355 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001356 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001357
1358 /* ata_pio_sectors() might change the
1359 * state to HSM_ST_LAST. so, the state
1360 * is changed after ata_pio_sectors().
1361 */
1362 ap->hsm_task_state = HSM_ST_ERR;
1363 goto fsm_start;
1364 }
1365
1366 ata_pio_sectors(qc);
1367
1368 if (ap->hsm_task_state == HSM_ST_LAST &&
1369 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1370 /* all data read */
1371 status = ata_wait_idle(ap);
1372 goto fsm_start;
1373 }
1374 }
1375
1376 poll_next = 1;
1377 break;
1378
1379 case HSM_ST_LAST:
1380 if (unlikely(!ata_ok(status))) {
1381 qc->err_mask |= __ac_err_mask(status);
1382 ap->hsm_task_state = HSM_ST_ERR;
1383 goto fsm_start;
1384 }
1385
1386 /* no more data to transfer */
1387 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1388 ap->print_id, qc->dev->devno, status);
1389
Tejun Heo411cb3862008-04-23 20:48:36 +09001390 WARN_ON(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
Tejun Heo624d5c52008-03-25 22:16:41 +09001391
1392 ap->hsm_task_state = HSM_ST_IDLE;
1393
1394 /* complete taskfile transaction */
1395 ata_hsm_qc_complete(qc, in_wq);
1396
1397 poll_next = 0;
1398 break;
1399
1400 case HSM_ST_ERR:
Tejun Heo624d5c52008-03-25 22:16:41 +09001401 ap->hsm_task_state = HSM_ST_IDLE;
1402
1403 /* complete taskfile transaction */
1404 ata_hsm_qc_complete(qc, in_wq);
1405
1406 poll_next = 0;
1407 break;
1408 default:
1409 poll_next = 0;
1410 BUG();
1411 }
1412
1413 return poll_next;
1414}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001415EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
Tejun Heo624d5c52008-03-25 22:16:41 +09001416
1417void ata_pio_task(struct work_struct *work)
1418{
1419 struct ata_port *ap =
1420 container_of(work, struct ata_port, port_task.work);
1421 struct ata_queued_cmd *qc = ap->port_task_data;
1422 u8 status;
1423 int poll_next;
1424
1425fsm_start:
1426 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
1427
1428 /*
1429 * This is purely heuristic. This is a fast path.
1430 * Sometimes when we enter, BSY will be cleared in
1431 * a chk-status or two. If not, the drive is probably seeking
1432 * or something. Snooze for a couple msecs, then
1433 * chk-status again. If still busy, queue delayed work.
1434 */
Tejun Heo9363c382008-04-07 22:47:16 +09001435 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
Tejun Heo624d5c52008-03-25 22:16:41 +09001436 if (status & ATA_BUSY) {
1437 msleep(2);
Tejun Heo9363c382008-04-07 22:47:16 +09001438 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
Tejun Heo624d5c52008-03-25 22:16:41 +09001439 if (status & ATA_BUSY) {
1440 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
1441 return;
1442 }
1443 }
1444
1445 /* move the HSM */
Tejun Heo9363c382008-04-07 22:47:16 +09001446 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001447
1448 /* another command or interrupt handler
1449 * may be running at this point.
1450 */
1451 if (poll_next)
1452 goto fsm_start;
1453}
1454
1455/**
Tejun Heo9363c382008-04-07 22:47:16 +09001456 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
Tejun Heo624d5c52008-03-25 22:16:41 +09001457 * @qc: command to issue to device
1458 *
1459 * Using various libata functions and hooks, this function
1460 * starts an ATA command. ATA commands are grouped into
1461 * classes called "protocols", and issuing each type of protocol
1462 * is slightly different.
1463 *
1464 * May be used as the qc_issue() entry in ata_port_operations.
1465 *
1466 * LOCKING:
1467 * spin_lock_irqsave(host lock)
1468 *
1469 * RETURNS:
1470 * Zero on success, AC_ERR_* mask on failure
1471 */
Tejun Heo9363c382008-04-07 22:47:16 +09001472unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001473{
1474 struct ata_port *ap = qc->ap;
1475
1476 /* Use polling pio if the LLD doesn't handle
1477 * interrupt driven pio and atapi CDB interrupt.
1478 */
1479 if (ap->flags & ATA_FLAG_PIO_POLLING) {
1480 switch (qc->tf.protocol) {
1481 case ATA_PROT_PIO:
1482 case ATA_PROT_NODATA:
1483 case ATAPI_PROT_PIO:
1484 case ATAPI_PROT_NODATA:
1485 qc->tf.flags |= ATA_TFLAG_POLLING;
1486 break;
1487 case ATAPI_PROT_DMA:
1488 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
1489 /* see ata_dma_blacklisted() */
1490 BUG();
1491 break;
1492 default:
1493 break;
1494 }
1495 }
1496
1497 /* select the device */
1498 ata_dev_select(ap, qc->dev->devno, 1, 0);
1499
1500 /* start the command */
1501 switch (qc->tf.protocol) {
1502 case ATA_PROT_NODATA:
1503 if (qc->tf.flags & ATA_TFLAG_POLLING)
1504 ata_qc_set_polling(qc);
1505
1506 ata_tf_to_host(ap, &qc->tf);
1507 ap->hsm_task_state = HSM_ST_LAST;
1508
1509 if (qc->tf.flags & ATA_TFLAG_POLLING)
1510 ata_pio_queue_task(ap, qc, 0);
1511
1512 break;
1513
1514 case ATA_PROT_DMA:
1515 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
1516
Tejun Heo5682ed32008-04-07 22:47:16 +09001517 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001518 ap->ops->bmdma_setup(qc); /* set up bmdma */
1519 ap->ops->bmdma_start(qc); /* initiate bmdma */
1520 ap->hsm_task_state = HSM_ST_LAST;
1521 break;
1522
1523 case ATA_PROT_PIO:
1524 if (qc->tf.flags & ATA_TFLAG_POLLING)
1525 ata_qc_set_polling(qc);
1526
1527 ata_tf_to_host(ap, &qc->tf);
1528
1529 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1530 /* PIO data out protocol */
1531 ap->hsm_task_state = HSM_ST_FIRST;
1532 ata_pio_queue_task(ap, qc, 0);
1533
1534 /* always send first data block using
1535 * the ata_pio_task() codepath.
1536 */
1537 } else {
1538 /* PIO data in protocol */
1539 ap->hsm_task_state = HSM_ST;
1540
1541 if (qc->tf.flags & ATA_TFLAG_POLLING)
1542 ata_pio_queue_task(ap, qc, 0);
1543
1544 /* if polling, ata_pio_task() handles the rest.
1545 * otherwise, interrupt handler takes over from here.
1546 */
1547 }
1548
1549 break;
1550
1551 case ATAPI_PROT_PIO:
1552 case ATAPI_PROT_NODATA:
1553 if (qc->tf.flags & ATA_TFLAG_POLLING)
1554 ata_qc_set_polling(qc);
1555
1556 ata_tf_to_host(ap, &qc->tf);
1557
1558 ap->hsm_task_state = HSM_ST_FIRST;
1559
1560 /* send cdb by polling if no cdb interrupt */
1561 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1562 (qc->tf.flags & ATA_TFLAG_POLLING))
1563 ata_pio_queue_task(ap, qc, 0);
1564 break;
1565
1566 case ATAPI_PROT_DMA:
1567 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
1568
Tejun Heo5682ed32008-04-07 22:47:16 +09001569 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001570 ap->ops->bmdma_setup(qc); /* set up bmdma */
1571 ap->hsm_task_state = HSM_ST_FIRST;
1572
1573 /* send cdb by polling if no cdb interrupt */
1574 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1575 ata_pio_queue_task(ap, qc, 0);
1576 break;
1577
1578 default:
1579 WARN_ON(1);
1580 return AC_ERR_SYSTEM;
1581 }
1582
1583 return 0;
1584}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001585EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
Tejun Heo624d5c52008-03-25 22:16:41 +09001586
1587/**
Tejun Heo22183bf2008-04-07 22:47:20 +09001588 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1589 * @qc: qc to fill result TF for
1590 *
1591 * @qc is finished and result TF needs to be filled. Fill it
1592 * using ->sff_tf_read.
1593 *
1594 * LOCKING:
1595 * spin_lock_irqsave(host lock)
1596 *
1597 * RETURNS:
1598 * true indicating that result TF is successfully filled.
1599 */
1600bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
1601{
1602 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
1603 return true;
1604}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001605EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
Tejun Heo22183bf2008-04-07 22:47:20 +09001606
1607/**
Tejun Heo9363c382008-04-07 22:47:16 +09001608 * ata_sff_host_intr - Handle host interrupt for given (port, task)
Tejun Heo624d5c52008-03-25 22:16:41 +09001609 * @ap: Port on which interrupt arrived (possibly...)
1610 * @qc: Taskfile currently active in engine
1611 *
1612 * Handle host interrupt for given queued command. Currently,
1613 * only DMA interrupts are handled. All other commands are
1614 * handled via polling with interrupts disabled (nIEN bit).
1615 *
1616 * LOCKING:
1617 * spin_lock_irqsave(host lock)
1618 *
1619 * RETURNS:
1620 * One if interrupt was handled, zero if not (shared irq).
1621 */
Tejun Heo9363c382008-04-07 22:47:16 +09001622inline unsigned int ata_sff_host_intr(struct ata_port *ap,
1623 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001624{
1625 struct ata_eh_info *ehi = &ap->link.eh_info;
1626 u8 status, host_stat = 0;
1627
1628 VPRINTK("ata%u: protocol %d task_state %d\n",
1629 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
1630
1631 /* Check whether we are expecting interrupt in this state */
1632 switch (ap->hsm_task_state) {
1633 case HSM_ST_FIRST:
1634 /* Some pre-ATAPI-4 devices assert INTRQ
1635 * at this state when ready to receive CDB.
1636 */
1637
1638 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1639 * The flag was turned on only for atapi devices. No
1640 * need to check ata_is_atapi(qc->tf.protocol) again.
1641 */
1642 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1643 goto idle_irq;
1644 break;
1645 case HSM_ST_LAST:
1646 if (qc->tf.protocol == ATA_PROT_DMA ||
1647 qc->tf.protocol == ATAPI_PROT_DMA) {
1648 /* check status of DMA engine */
1649 host_stat = ap->ops->bmdma_status(ap);
1650 VPRINTK("ata%u: host_stat 0x%X\n",
1651 ap->print_id, host_stat);
1652
1653 /* if it's not our irq... */
1654 if (!(host_stat & ATA_DMA_INTR))
1655 goto idle_irq;
1656
1657 /* before we do anything else, clear DMA-Start bit */
1658 ap->ops->bmdma_stop(qc);
1659
1660 if (unlikely(host_stat & ATA_DMA_ERR)) {
1661 /* error when transfering data to/from memory */
1662 qc->err_mask |= AC_ERR_HOST_BUS;
1663 ap->hsm_task_state = HSM_ST_ERR;
1664 }
1665 }
1666 break;
1667 case HSM_ST:
1668 break;
1669 default:
1670 goto idle_irq;
1671 }
1672
Tejun Heo624d5c52008-03-25 22:16:41 +09001673
Alan Coxa57c1ba2008-05-29 22:10:58 +01001674 /* check main status, clearing INTRQ if needed */
1675 status = ata_sff_irq_status(ap);
1676 if (status & ATA_BUSY)
Tejun Heo624d5c52008-03-25 22:16:41 +09001677 goto idle_irq;
1678
1679 /* ack bmdma irq events */
Tejun Heo5682ed32008-04-07 22:47:16 +09001680 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001681
Tejun Heo9363c382008-04-07 22:47:16 +09001682 ata_sff_hsm_move(ap, qc, status, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001683
1684 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
1685 qc->tf.protocol == ATAPI_PROT_DMA))
1686 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
1687
1688 return 1; /* irq handled */
1689
1690idle_irq:
1691 ap->stats.idle_irq++;
1692
1693#ifdef ATA_IRQ_TRAP
1694 if ((ap->stats.idle_irq % 1000) == 0) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001695 ap->ops->sff_check_status(ap);
1696 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001697 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
1698 return 1;
1699 }
1700#endif
1701 return 0; /* irq not handled */
1702}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001703EXPORT_SYMBOL_GPL(ata_sff_host_intr);
Tejun Heo624d5c52008-03-25 22:16:41 +09001704
1705/**
Tejun Heo9363c382008-04-07 22:47:16 +09001706 * ata_sff_interrupt - Default ATA host interrupt handler
Tejun Heo624d5c52008-03-25 22:16:41 +09001707 * @irq: irq line (unused)
1708 * @dev_instance: pointer to our ata_host information structure
1709 *
1710 * Default interrupt handler for PCI IDE devices. Calls
Tejun Heo9363c382008-04-07 22:47:16 +09001711 * ata_sff_host_intr() for each port that is not disabled.
Tejun Heo624d5c52008-03-25 22:16:41 +09001712 *
1713 * LOCKING:
1714 * Obtains host lock during operation.
1715 *
1716 * RETURNS:
1717 * IRQ_NONE or IRQ_HANDLED.
1718 */
Tejun Heo9363c382008-04-07 22:47:16 +09001719irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
Tejun Heo624d5c52008-03-25 22:16:41 +09001720{
1721 struct ata_host *host = dev_instance;
1722 unsigned int i;
1723 unsigned int handled = 0;
1724 unsigned long flags;
1725
1726 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1727 spin_lock_irqsave(&host->lock, flags);
1728
1729 for (i = 0; i < host->n_ports; i++) {
1730 struct ata_port *ap;
1731
1732 ap = host->ports[i];
1733 if (ap &&
1734 !(ap->flags & ATA_FLAG_DISABLED)) {
1735 struct ata_queued_cmd *qc;
1736
1737 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1738 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
1739 (qc->flags & ATA_QCFLAG_ACTIVE))
Tejun Heo9363c382008-04-07 22:47:16 +09001740 handled |= ata_sff_host_intr(ap, qc);
Tejun Heo624d5c52008-03-25 22:16:41 +09001741 }
1742 }
1743
1744 spin_unlock_irqrestore(&host->lock, flags);
1745
1746 return IRQ_RETVAL(handled);
1747}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001748EXPORT_SYMBOL_GPL(ata_sff_interrupt);
Tejun Heo624d5c52008-03-25 22:16:41 +09001749
1750/**
Tejun Heo9363c382008-04-07 22:47:16 +09001751 * ata_sff_freeze - Freeze SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001752 * @ap: port to freeze
1753 *
1754 * Freeze BMDMA controller port.
1755 *
1756 * LOCKING:
1757 * Inherited from caller.
1758 */
Tejun Heo9363c382008-04-07 22:47:16 +09001759void ata_sff_freeze(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001760{
1761 struct ata_ioports *ioaddr = &ap->ioaddr;
1762
1763 ap->ctl |= ATA_NIEN;
1764 ap->last_ctl = ap->ctl;
1765
Tejun Heof659f0e42008-03-06 13:12:54 +09001766 if (ioaddr->ctl_addr)
1767 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001768
1769 /* Under certain circumstances, some controllers raise IRQ on
1770 * ATA_NIEN manipulation. Also, many controllers fail to mask
1771 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1772 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001773 ap->ops->sff_check_status(ap);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001774
Tejun Heo5682ed32008-04-07 22:47:16 +09001775 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001776}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001777EXPORT_SYMBOL_GPL(ata_sff_freeze);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001778
1779/**
Tejun Heo9363c382008-04-07 22:47:16 +09001780 * ata_sff_thaw - Thaw SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001781 * @ap: port to thaw
1782 *
Tejun Heo9363c382008-04-07 22:47:16 +09001783 * Thaw SFF controller port.
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001784 *
1785 * LOCKING:
1786 * Inherited from caller.
1787 */
Tejun Heo9363c382008-04-07 22:47:16 +09001788void ata_sff_thaw(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001789{
1790 /* clear & re-enable interrupts */
Tejun Heo5682ed32008-04-07 22:47:16 +09001791 ap->ops->sff_check_status(ap);
1792 ap->ops->sff_irq_clear(ap);
1793 ap->ops->sff_irq_on(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001794}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001795EXPORT_SYMBOL_GPL(ata_sff_thaw);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001796
1797/**
Tejun Heo0aa11132008-04-07 22:47:18 +09001798 * ata_sff_prereset - prepare SFF link for reset
1799 * @link: SFF link to be reset
1800 * @deadline: deadline jiffies for the operation
1801 *
1802 * SFF link @link is about to be reset. Initialize it. It first
1803 * calls ata_std_prereset() and wait for !BSY if the port is
1804 * being softreset.
1805 *
1806 * LOCKING:
1807 * Kernel thread context (may sleep)
1808 *
1809 * RETURNS:
1810 * 0 on success, -errno otherwise.
1811 */
1812int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
1813{
Tejun Heo0aa11132008-04-07 22:47:18 +09001814 struct ata_eh_context *ehc = &link->eh_context;
1815 int rc;
1816
1817 rc = ata_std_prereset(link, deadline);
1818 if (rc)
1819 return rc;
1820
1821 /* if we're about to do hardreset, nothing more to do */
1822 if (ehc->i.action & ATA_EH_HARDRESET)
1823 return 0;
1824
1825 /* wait for !BSY if we don't know that no device is attached */
1826 if (!ata_link_offline(link)) {
Tejun Heo705e76b2008-04-07 22:47:19 +09001827 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo0aa11132008-04-07 22:47:18 +09001828 if (rc && rc != -ENODEV) {
1829 ata_link_printk(link, KERN_WARNING, "device not ready "
1830 "(errno=%d), forcing hardreset\n", rc);
1831 ehc->i.action |= ATA_EH_HARDRESET;
1832 }
1833 }
1834
1835 return 0;
1836}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001837EXPORT_SYMBOL_GPL(ata_sff_prereset);
Tejun Heo0aa11132008-04-07 22:47:18 +09001838
1839/**
Tejun Heo624d5c52008-03-25 22:16:41 +09001840 * ata_devchk - PATA device presence detection
1841 * @ap: ATA channel to examine
1842 * @device: Device to examine (starting at zero)
1843 *
1844 * This technique was originally described in
1845 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1846 * later found its way into the ATA/ATAPI spec.
1847 *
1848 * Write a pattern to the ATA shadow registers,
1849 * and if a device is present, it will respond by
1850 * correctly storing and echoing back the
1851 * ATA shadow register contents.
1852 *
1853 * LOCKING:
1854 * caller.
1855 */
1856static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1857{
1858 struct ata_ioports *ioaddr = &ap->ioaddr;
1859 u8 nsect, lbal;
1860
Tejun Heo5682ed32008-04-07 22:47:16 +09001861 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +09001862
1863 iowrite8(0x55, ioaddr->nsect_addr);
1864 iowrite8(0xaa, ioaddr->lbal_addr);
1865
1866 iowrite8(0xaa, ioaddr->nsect_addr);
1867 iowrite8(0x55, ioaddr->lbal_addr);
1868
1869 iowrite8(0x55, ioaddr->nsect_addr);
1870 iowrite8(0xaa, ioaddr->lbal_addr);
1871
1872 nsect = ioread8(ioaddr->nsect_addr);
1873 lbal = ioread8(ioaddr->lbal_addr);
1874
1875 if ((nsect == 0x55) && (lbal == 0xaa))
1876 return 1; /* we found a device */
1877
1878 return 0; /* nothing found */
1879}
1880
1881/**
Tejun Heo9363c382008-04-07 22:47:16 +09001882 * ata_sff_dev_classify - Parse returned ATA device signature
Tejun Heo624d5c52008-03-25 22:16:41 +09001883 * @dev: ATA device to classify (starting at zero)
1884 * @present: device seems present
1885 * @r_err: Value of error register on completion
1886 *
1887 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1888 * an ATA/ATAPI-defined set of values is placed in the ATA
1889 * shadow registers, indicating the results of device detection
1890 * and diagnostics.
1891 *
1892 * Select the ATA device, and read the values from the ATA shadow
1893 * registers. Then parse according to the Error register value,
1894 * and the spec-defined values examined by ata_dev_classify().
1895 *
1896 * LOCKING:
1897 * caller.
1898 *
1899 * RETURNS:
1900 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1901 */
Tejun Heo9363c382008-04-07 22:47:16 +09001902unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
Tejun Heo624d5c52008-03-25 22:16:41 +09001903 u8 *r_err)
1904{
1905 struct ata_port *ap = dev->link->ap;
1906 struct ata_taskfile tf;
1907 unsigned int class;
1908 u8 err;
1909
Tejun Heo5682ed32008-04-07 22:47:16 +09001910 ap->ops->sff_dev_select(ap, dev->devno);
Tejun Heo624d5c52008-03-25 22:16:41 +09001911
1912 memset(&tf, 0, sizeof(tf));
1913
Tejun Heo5682ed32008-04-07 22:47:16 +09001914 ap->ops->sff_tf_read(ap, &tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001915 err = tf.feature;
1916 if (r_err)
1917 *r_err = err;
1918
1919 /* see if device passed diags: continue and warn later */
1920 if (err == 0)
1921 /* diagnostic fail : do nothing _YET_ */
1922 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1923 else if (err == 1)
1924 /* do nothing */ ;
1925 else if ((dev->devno == 0) && (err == 0x81))
1926 /* do nothing */ ;
1927 else
1928 return ATA_DEV_NONE;
1929
1930 /* determine if device is ATA or ATAPI */
1931 class = ata_dev_classify(&tf);
1932
1933 if (class == ATA_DEV_UNKNOWN) {
1934 /* If the device failed diagnostic, it's likely to
1935 * have reported incorrect device signature too.
1936 * Assume ATA device if the device seems present but
1937 * device signature is invalid with diagnostic
1938 * failure.
1939 */
1940 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1941 class = ATA_DEV_ATA;
1942 else
1943 class = ATA_DEV_NONE;
Tejun Heo5682ed32008-04-07 22:47:16 +09001944 } else if ((class == ATA_DEV_ATA) &&
1945 (ap->ops->sff_check_status(ap) == 0))
Tejun Heo624d5c52008-03-25 22:16:41 +09001946 class = ATA_DEV_NONE;
1947
1948 return class;
1949}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001950EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
Tejun Heo624d5c52008-03-25 22:16:41 +09001951
Tejun Heo705e76b2008-04-07 22:47:19 +09001952/**
1953 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1954 * @link: SFF link which is just reset
1955 * @devmask: mask of present devices
1956 * @deadline: deadline jiffies for the operation
1957 *
1958 * Wait devices attached to SFF @link to become ready after
1959 * reset. It contains preceding 150ms wait to avoid accessing TF
1960 * status register too early.
1961 *
1962 * LOCKING:
1963 * Kernel thread context (may sleep).
1964 *
1965 * RETURNS:
1966 * 0 on success, -ENODEV if some or all of devices in @devmask
1967 * don't seem to exist. -errno on other errors.
1968 */
1969int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
1970 unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +09001971{
Tejun Heo705e76b2008-04-07 22:47:19 +09001972 struct ata_port *ap = link->ap;
Tejun Heo624d5c52008-03-25 22:16:41 +09001973 struct ata_ioports *ioaddr = &ap->ioaddr;
1974 unsigned int dev0 = devmask & (1 << 0);
1975 unsigned int dev1 = devmask & (1 << 1);
1976 int rc, ret = 0;
1977
Tejun Heo341c2c92008-05-20 02:17:51 +09001978 msleep(ATA_WAIT_AFTER_RESET);
Tejun Heo705e76b2008-04-07 22:47:19 +09001979
1980 /* always check readiness of the master device */
1981 rc = ata_sff_wait_ready(link, deadline);
1982 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
1983 * and TF status is 0xff, bail out on it too.
Tejun Heo624d5c52008-03-25 22:16:41 +09001984 */
Tejun Heo705e76b2008-04-07 22:47:19 +09001985 if (rc)
1986 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09001987
1988 /* if device 1 was found in ata_devchk, wait for register
1989 * access briefly, then wait for BSY to clear.
1990 */
1991 if (dev1) {
1992 int i;
1993
Tejun Heo5682ed32008-04-07 22:47:16 +09001994 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001995
1996 /* Wait for register access. Some ATAPI devices fail
1997 * to set nsect/lbal after reset, so don't waste too
1998 * much time on it. We're gonna wait for !BSY anyway.
1999 */
2000 for (i = 0; i < 2; i++) {
2001 u8 nsect, lbal;
2002
2003 nsect = ioread8(ioaddr->nsect_addr);
2004 lbal = ioread8(ioaddr->lbal_addr);
2005 if ((nsect == 1) && (lbal == 1))
2006 break;
2007 msleep(50); /* give drive a breather */
2008 }
2009
Tejun Heo705e76b2008-04-07 22:47:19 +09002010 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09002011 if (rc) {
2012 if (rc != -ENODEV)
2013 return rc;
2014 ret = rc;
2015 }
2016 }
2017
2018 /* is all this really necessary? */
Tejun Heo5682ed32008-04-07 22:47:16 +09002019 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002020 if (dev1)
Tejun Heo5682ed32008-04-07 22:47:16 +09002021 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002022 if (dev0)
Tejun Heo5682ed32008-04-07 22:47:16 +09002023 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002024
2025 return ret;
2026}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002027EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002028
Tejun Heo624d5c52008-03-25 22:16:41 +09002029static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
2030 unsigned long deadline)
2031{
2032 struct ata_ioports *ioaddr = &ap->ioaddr;
2033
2034 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
2035
2036 /* software reset. causes dev0 to be selected */
2037 iowrite8(ap->ctl, ioaddr->ctl_addr);
2038 udelay(20); /* FIXME: flush */
2039 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2040 udelay(20); /* FIXME: flush */
2041 iowrite8(ap->ctl, ioaddr->ctl_addr);
2042
Tejun Heo705e76b2008-04-07 22:47:19 +09002043 /* wait the port to become ready */
2044 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09002045}
2046
2047/**
Tejun Heo9363c382008-04-07 22:47:16 +09002048 * ata_sff_softreset - reset host port via ATA SRST
Tejun Heo624d5c52008-03-25 22:16:41 +09002049 * @link: ATA link to reset
2050 * @classes: resulting classes of attached devices
2051 * @deadline: deadline jiffies for the operation
2052 *
2053 * Reset host port using ATA SRST.
2054 *
2055 * LOCKING:
2056 * Kernel thread context (may sleep)
2057 *
2058 * RETURNS:
2059 * 0 on success, -errno otherwise.
2060 */
Tejun Heo9363c382008-04-07 22:47:16 +09002061int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heo624d5c52008-03-25 22:16:41 +09002062 unsigned long deadline)
2063{
2064 struct ata_port *ap = link->ap;
2065 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2066 unsigned int devmask = 0;
2067 int rc;
2068 u8 err;
2069
2070 DPRINTK("ENTER\n");
2071
Tejun Heo624d5c52008-03-25 22:16:41 +09002072 /* determine if device 0/1 are present */
2073 if (ata_devchk(ap, 0))
2074 devmask |= (1 << 0);
2075 if (slave_possible && ata_devchk(ap, 1))
2076 devmask |= (1 << 1);
2077
2078 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002079 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002080
2081 /* issue bus reset */
2082 DPRINTK("about to softreset, devmask=%x\n", devmask);
2083 rc = ata_bus_softreset(ap, devmask, deadline);
2084 /* if link is occupied, -ENODEV too is an error */
2085 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
2086 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
2087 return rc;
2088 }
2089
2090 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo9363c382008-04-07 22:47:16 +09002091 classes[0] = ata_sff_dev_classify(&link->device[0],
Tejun Heo624d5c52008-03-25 22:16:41 +09002092 devmask & (1 << 0), &err);
2093 if (slave_possible && err != 0x81)
Tejun Heo9363c382008-04-07 22:47:16 +09002094 classes[1] = ata_sff_dev_classify(&link->device[1],
Tejun Heo624d5c52008-03-25 22:16:41 +09002095 devmask & (1 << 1), &err);
2096
Tejun Heo624d5c52008-03-25 22:16:41 +09002097 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2098 return 0;
2099}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002100EXPORT_SYMBOL_GPL(ata_sff_softreset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002101
2102/**
Tejun Heo9363c382008-04-07 22:47:16 +09002103 * sata_sff_hardreset - reset host port via SATA phy reset
Tejun Heo624d5c52008-03-25 22:16:41 +09002104 * @link: link to reset
2105 * @class: resulting class of attached device
2106 * @deadline: deadline jiffies for the operation
2107 *
2108 * SATA phy-reset host port using DET bits of SControl register,
2109 * wait for !BSY and classify the attached device.
2110 *
2111 * LOCKING:
2112 * Kernel thread context (may sleep)
2113 *
2114 * RETURNS:
2115 * 0 on success, -errno otherwise.
2116 */
Tejun Heo9363c382008-04-07 22:47:16 +09002117int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heo624d5c52008-03-25 22:16:41 +09002118 unsigned long deadline)
2119{
Tejun Heo9dadd452008-04-07 22:47:19 +09002120 struct ata_eh_context *ehc = &link->eh_context;
2121 const unsigned long *timing = sata_ehc_deb_timing(ehc);
2122 bool online;
Tejun Heo624d5c52008-03-25 22:16:41 +09002123 int rc;
2124
Tejun Heo9dadd452008-04-07 22:47:19 +09002125 rc = sata_link_hardreset(link, timing, deadline, &online,
2126 ata_sff_check_ready);
Tejun Heo9dadd452008-04-07 22:47:19 +09002127 if (online)
2128 *class = ata_sff_dev_classify(link->device, 1, NULL);
Tejun Heo624d5c52008-03-25 22:16:41 +09002129
2130 DPRINTK("EXIT, class=%u\n", *class);
Tejun Heo9dadd452008-04-07 22:47:19 +09002131 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09002132}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002133EXPORT_SYMBOL_GPL(sata_sff_hardreset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002134
2135/**
Tejun Heo203c75b2008-04-07 22:47:18 +09002136 * ata_sff_postreset - SFF postreset callback
2137 * @link: the target SFF ata_link
2138 * @classes: classes of attached devices
2139 *
2140 * This function is invoked after a successful reset. It first
2141 * calls ata_std_postreset() and performs SFF specific postreset
2142 * processing.
2143 *
2144 * LOCKING:
2145 * Kernel thread context (may sleep)
2146 */
2147void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
2148{
2149 struct ata_port *ap = link->ap;
2150
2151 ata_std_postreset(link, classes);
2152
2153 /* is double-select really necessary? */
2154 if (classes[0] != ATA_DEV_NONE)
2155 ap->ops->sff_dev_select(ap, 1);
2156 if (classes[1] != ATA_DEV_NONE)
2157 ap->ops->sff_dev_select(ap, 0);
2158
2159 /* bail out if no device is present */
2160 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2161 DPRINTK("EXIT, no device\n");
2162 return;
2163 }
2164
2165 /* set up device control */
2166 if (ap->ioaddr.ctl_addr)
2167 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
2168}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002169EXPORT_SYMBOL_GPL(ata_sff_postreset);
Tejun Heo203c75b2008-04-07 22:47:18 +09002170
2171/**
Tejun Heo9363c382008-04-07 22:47:16 +09002172 * ata_sff_error_handler - Stock error handler for BMDMA controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002173 * @ap: port to handle error for
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002174 *
Tejun Heo9363c382008-04-07 22:47:16 +09002175 * Stock error handler for SFF controller. It can handle both
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002176 * PATA and SATA controllers. Many controllers should be able to
2177 * use this EH as-is or with some added handling before and
2178 * after.
2179 *
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002180 * LOCKING:
2181 * Kernel thread context (may sleep)
2182 */
Tejun Heo9363c382008-04-07 22:47:16 +09002183void ata_sff_error_handler(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002184{
Tejun Heoa1efdab2008-03-25 12:22:50 +09002185 ata_reset_fn_t softreset = ap->ops->softreset;
2186 ata_reset_fn_t hardreset = ap->ops->hardreset;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002187 struct ata_queued_cmd *qc;
2188 unsigned long flags;
2189 int thaw = 0;
2190
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002191 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002192 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2193 qc = NULL;
2194
2195 /* reset PIO HSM and stop DMA engine */
Jeff Garzikba6a1302006-06-22 23:46:10 -04002196 spin_lock_irqsave(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002197
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002198 ap->hsm_task_state = HSM_ST_IDLE;
2199
Tejun Heoed82f962008-03-25 21:34:39 +09002200 if (ap->ioaddr.bmdma_addr &&
2201 qc && (qc->tf.protocol == ATA_PROT_DMA ||
Tejun Heo0dc36882007-12-18 16:34:43 -05002202 qc->tf.protocol == ATAPI_PROT_DMA)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002203 u8 host_stat;
2204
Robert Hancockfbbb2622006-10-27 19:08:41 -07002205 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002206
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002207 /* BMDMA controllers indicate host bus error by
2208 * setting DMA_ERR bit and timing out. As it wasn't
2209 * really a timeout event, adjust error mask and
2210 * cancel frozen state.
2211 */
Alan18d90de2007-01-24 11:42:38 +00002212 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002213 qc->err_mask = AC_ERR_HOST_BUS;
2214 thaw = 1;
2215 }
2216
2217 ap->ops->bmdma_stop(qc);
2218 }
2219
Alan Coxa57c1ba2008-05-29 22:10:58 +01002220 ata_sff_sync(ap); /* FIXME: We don't need this */
Tejun Heo5682ed32008-04-07 22:47:16 +09002221 ap->ops->sff_check_status(ap);
2222 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002223
Jeff Garzikba6a1302006-06-22 23:46:10 -04002224 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002225
2226 if (thaw)
2227 ata_eh_thaw_port(ap);
2228
2229 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002230
Tejun Heo57c9efd2008-04-07 22:47:19 +09002231 /* Ignore ata_sff_softreset if ctl isn't accessible and
2232 * built-in hardresets if SCR access isn't available.
Tejun Heoa1efdab2008-03-25 12:22:50 +09002233 */
Tejun Heo9363c382008-04-07 22:47:16 +09002234 if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
Tejun Heoa1efdab2008-03-25 12:22:50 +09002235 softreset = NULL;
Tejun Heo57c9efd2008-04-07 22:47:19 +09002236 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09002237 hardreset = NULL;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002238
Tejun Heoa1efdab2008-03-25 12:22:50 +09002239 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2240 ap->ops->postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002241}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002242EXPORT_SYMBOL_GPL(ata_sff_error_handler);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002243
2244/**
Tejun Heo9363c382008-04-07 22:47:16 +09002245 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002246 * @qc: internal command to clean up
2247 *
2248 * LOCKING:
2249 * Kernel thread context (may sleep)
2250 */
Tejun Heo9363c382008-04-07 22:47:16 +09002251void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002252{
Tejun Heo570106d2008-10-20 13:10:21 +09002253 struct ata_port *ap = qc->ap;
2254 unsigned long flags;
2255
2256 spin_lock_irqsave(ap->lock, flags);
2257
2258 ap->hsm_task_state = HSM_ST_IDLE;
2259
2260 if (ap->ioaddr.bmdma_addr)
Alan61dd08c2007-01-25 15:09:05 +00002261 ata_bmdma_stop(qc);
Tejun Heo570106d2008-10-20 13:10:21 +09002262
2263 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002264}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002265EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002266
Alan Coxd92e74d2007-06-07 16:19:15 +01002267/**
2268 * ata_sff_port_start - Set port up for dma.
2269 * @ap: Port to initialize
2270 *
2271 * Called just after data structures for each port are
2272 * initialized. Allocates space for PRD table if the device
2273 * is DMA capable SFF.
2274 *
2275 * May be used as the port_start() entry in ata_port_operations.
2276 *
2277 * LOCKING:
2278 * Inherited from caller.
2279 */
Alan Coxd92e74d2007-06-07 16:19:15 +01002280int ata_sff_port_start(struct ata_port *ap)
2281{
2282 if (ap->ioaddr.bmdma_addr)
2283 return ata_port_start(ap);
2284 return 0;
2285}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002286EXPORT_SYMBOL_GPL(ata_sff_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01002287
Tejun Heo272f7882008-03-25 22:16:40 +09002288/**
Tejun Heo9363c382008-04-07 22:47:16 +09002289 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
Tejun Heo624d5c52008-03-25 22:16:41 +09002290 * @ioaddr: IO address structure to be initialized
2291 *
2292 * Utility function which initializes data_addr, error_addr,
2293 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2294 * device_addr, status_addr, and command_addr to standard offsets
2295 * relative to cmd_addr.
2296 *
2297 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2298 */
Tejun Heo9363c382008-04-07 22:47:16 +09002299void ata_sff_std_ports(struct ata_ioports *ioaddr)
Tejun Heo624d5c52008-03-25 22:16:41 +09002300{
2301 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2302 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2303 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2304 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2305 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2306 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2307 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2308 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2309 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2310 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2311}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002312EXPORT_SYMBOL_GPL(ata_sff_std_ports);
Tejun Heo624d5c52008-03-25 22:16:41 +09002313
Tejun Heo9363c382008-04-07 22:47:16 +09002314unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
2315 unsigned long xfer_mask)
Tejun Heo071ce342008-03-25 22:16:42 +09002316{
2317 /* Filter out DMA modes if the device has been configured by
2318 the BIOS as PIO only */
2319
2320 if (adev->link->ap->ioaddr.bmdma_addr == NULL)
2321 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2322 return xfer_mask;
2323}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002324EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
Tejun Heo071ce342008-03-25 22:16:42 +09002325
Tejun Heo624d5c52008-03-25 22:16:41 +09002326/**
Tejun Heo272f7882008-03-25 22:16:40 +09002327 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2328 * @qc: Info associated with this ATA transaction.
2329 *
2330 * LOCKING:
2331 * spin_lock_irqsave(host lock)
2332 */
2333void ata_bmdma_setup(struct ata_queued_cmd *qc)
2334{
2335 struct ata_port *ap = qc->ap;
2336 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2337 u8 dmactl;
2338
2339 /* load PRD table addr. */
2340 mb(); /* make sure PRD table writes are visible to controller */
2341 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2342
2343 /* specify data direction, triple-check start bit is clear */
2344 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2345 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2346 if (!rw)
2347 dmactl |= ATA_DMA_WR;
2348 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2349
2350 /* issue r/w command */
Tejun Heo5682ed32008-04-07 22:47:16 +09002351 ap->ops->sff_exec_command(ap, &qc->tf);
Tejun Heo272f7882008-03-25 22:16:40 +09002352}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002353EXPORT_SYMBOL_GPL(ata_bmdma_setup);
Tejun Heo272f7882008-03-25 22:16:40 +09002354
2355/**
2356 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2357 * @qc: Info associated with this ATA transaction.
2358 *
2359 * LOCKING:
2360 * spin_lock_irqsave(host lock)
2361 */
2362void ata_bmdma_start(struct ata_queued_cmd *qc)
2363{
2364 struct ata_port *ap = qc->ap;
2365 u8 dmactl;
2366
2367 /* start host DMA transaction */
2368 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2369 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2370
2371 /* Strictly, one may wish to issue an ioread8() here, to
2372 * flush the mmio write. However, control also passes
2373 * to the hardware at this point, and it will interrupt
2374 * us when we are to resume control. So, in effect,
2375 * we don't care when the mmio write flushes.
2376 * Further, a read of the DMA status register _immediately_
2377 * following the write may not be what certain flaky hardware
2378 * is expected, so I think it is best to not add a readb()
2379 * without first all the MMIO ATA cards/mobos.
2380 * Or maybe I'm just being paranoid.
2381 *
2382 * FIXME: The posting of this write means I/O starts are
2383 * unneccessarily delayed for MMIO
2384 */
2385}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002386EXPORT_SYMBOL_GPL(ata_bmdma_start);
Tejun Heo272f7882008-03-25 22:16:40 +09002387
2388/**
2389 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2390 * @qc: Command we are ending DMA for
2391 *
2392 * Clears the ATA_DMA_START flag in the dma control register
2393 *
2394 * May be used as the bmdma_stop() entry in ata_port_operations.
2395 *
2396 * LOCKING:
2397 * spin_lock_irqsave(host lock)
2398 */
2399void ata_bmdma_stop(struct ata_queued_cmd *qc)
2400{
2401 struct ata_port *ap = qc->ap;
2402 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2403
2404 /* clear start/stop bit */
2405 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2406 mmio + ATA_DMA_CMD);
2407
2408 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
Alan Coxa57c1ba2008-05-29 22:10:58 +01002409 ata_sff_dma_pause(ap);
Tejun Heo272f7882008-03-25 22:16:40 +09002410}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002411EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo272f7882008-03-25 22:16:40 +09002412
2413/**
2414 * ata_bmdma_status - Read PCI IDE BMDMA status
2415 * @ap: Port associated with this ATA transaction.
2416 *
2417 * Read and return BMDMA status register.
2418 *
2419 * May be used as the bmdma_status() entry in ata_port_operations.
2420 *
2421 * LOCKING:
2422 * spin_lock_irqsave(host lock)
2423 */
2424u8 ata_bmdma_status(struct ata_port *ap)
2425{
2426 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2427}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002428EXPORT_SYMBOL_GPL(ata_bmdma_status);
Tejun Heo272f7882008-03-25 22:16:40 +09002429
2430/**
Tejun Heo624d5c52008-03-25 22:16:41 +09002431 * ata_bus_reset - reset host port and associated ATA channel
2432 * @ap: port to reset
2433 *
2434 * This is typically the first time we actually start issuing
2435 * commands to the ATA channel. We wait for BSY to clear, then
2436 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2437 * result. Determine what devices, if any, are on the channel
2438 * by looking at the device 0/1 error register. Look at the signature
2439 * stored in each device's taskfile registers, to determine if
2440 * the device is ATA or ATAPI.
2441 *
2442 * LOCKING:
2443 * PCI/etc. bus probe sem.
2444 * Obtains host lock.
2445 *
2446 * SIDE EFFECTS:
2447 * Sets ATA_FLAG_DISABLED if bus reset fails.
2448 *
2449 * DEPRECATED:
2450 * This function is only for drivers which still use old EH and
2451 * will be removed soon.
Tejun Heo272f7882008-03-25 22:16:40 +09002452 */
Tejun Heo624d5c52008-03-25 22:16:41 +09002453void ata_bus_reset(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +09002454{
Tejun Heo624d5c52008-03-25 22:16:41 +09002455 struct ata_device *device = ap->link.device;
2456 struct ata_ioports *ioaddr = &ap->ioaddr;
2457 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2458 u8 err;
2459 unsigned int dev0, dev1 = 0, devmask = 0;
2460 int rc;
2461
2462 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
2463
2464 /* determine if device 0/1 are present */
2465 if (ap->flags & ATA_FLAG_SATA_RESET)
2466 dev0 = 1;
2467 else {
2468 dev0 = ata_devchk(ap, 0);
2469 if (slave_possible)
2470 dev1 = ata_devchk(ap, 1);
2471 }
2472
2473 if (dev0)
2474 devmask |= (1 << 0);
2475 if (dev1)
2476 devmask |= (1 << 1);
2477
2478 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002479 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002480
2481 /* issue bus reset */
2482 if (ap->flags & ATA_FLAG_SRST) {
Tejun Heo341c2c92008-05-20 02:17:51 +09002483 rc = ata_bus_softreset(ap, devmask,
2484 ata_deadline(jiffies, 40000));
Tejun Heo624d5c52008-03-25 22:16:41 +09002485 if (rc && rc != -ENODEV)
2486 goto err_out;
2487 }
2488
2489 /*
2490 * determine by signature whether we have ATA or ATAPI devices
2491 */
Tejun Heo9363c382008-04-07 22:47:16 +09002492 device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002493 if ((slave_possible) && (err != 0x81))
Tejun Heo9363c382008-04-07 22:47:16 +09002494 device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002495
2496 /* is double-select really necessary? */
2497 if (device[1].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002498 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002499 if (device[0].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002500 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002501
2502 /* if no devices were detected, disable this port */
2503 if ((device[0].class == ATA_DEV_NONE) &&
2504 (device[1].class == ATA_DEV_NONE))
2505 goto err_out;
2506
2507 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2508 /* set up device control for ATA_FLAG_SATA_RESET */
2509 iowrite8(ap->ctl, ioaddr->ctl_addr);
2510 }
2511
2512 DPRINTK("EXIT\n");
2513 return;
2514
2515err_out:
2516 ata_port_printk(ap, KERN_ERR, "disabling port\n");
2517 ata_port_disable(ap);
2518
2519 DPRINTK("EXIT\n");
Tejun Heo272f7882008-03-25 22:16:40 +09002520}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002521EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo272f7882008-03-25 22:16:40 +09002522
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002523#ifdef CONFIG_PCI
Alan4112e162007-01-08 12:10:05 +00002524
Tejun Heo272f7882008-03-25 22:16:40 +09002525/**
Tejun Heo9363c382008-04-07 22:47:16 +09002526 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
Tejun Heo272f7882008-03-25 22:16:40 +09002527 * @pdev: PCI device
2528 *
2529 * Some PCI ATA devices report simplex mode but in fact can be told to
2530 * enter non simplex mode. This implements the necessary logic to
2531 * perform the task on such devices. Calling it on other devices will
2532 * have -undefined- behaviour.
2533 */
Tejun Heo9363c382008-04-07 22:47:16 +09002534int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
Alan4112e162007-01-08 12:10:05 +00002535{
Tejun Heo272f7882008-03-25 22:16:40 +09002536 unsigned long bmdma = pci_resource_start(pdev, 4);
2537 u8 simplex;
Jeff Garzika84471f2007-02-26 05:51:33 -05002538
Tejun Heo272f7882008-03-25 22:16:40 +09002539 if (bmdma == 0)
2540 return -ENOENT;
2541
2542 simplex = inb(bmdma + 0x02);
2543 outb(simplex & 0x60, bmdma + 0x02);
2544 simplex = inb(bmdma + 0x02);
2545 if (simplex & 0x80)
2546 return -EOPNOTSUPP;
2547 return 0;
2548}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002549EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
Tejun Heo272f7882008-03-25 22:16:40 +09002550
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002551/**
Tejun Heo9363c382008-04-07 22:47:16 +09002552 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
Tejun Heo0f834de2007-04-17 23:44:07 +09002553 * @host: target ATA host
2554 *
2555 * Acquire PCI BMDMA resources and initialize @host accordingly.
2556 *
2557 * LOCKING:
2558 * Inherited from calling layer (may sleep).
2559 *
2560 * RETURNS:
2561 * 0 on success, -errno otherwise.
2562 */
Tejun Heo9363c382008-04-07 22:47:16 +09002563int ata_pci_bmdma_init(struct ata_host *host)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002564{
Tejun Heo0f834de2007-04-17 23:44:07 +09002565 struct device *gdev = host->dev;
2566 struct pci_dev *pdev = to_pci_dev(gdev);
2567 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002568
Alan Cox6fdc99a2007-07-26 18:41:30 +01002569 /* No BAR4 allocation: No DMA */
2570 if (pci_resource_start(pdev, 4) == 0)
2571 return 0;
2572
Tejun Heo0f834de2007-04-17 23:44:07 +09002573 /* TODO: If we get no DMA mask we should fall back to PIO */
2574 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
2575 if (rc)
2576 return rc;
2577 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
2578 if (rc)
2579 return rc;
2580
2581 /* request and iomap DMA region */
Tejun Heo35a10a82008-01-04 18:42:21 +09002582 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
Tejun Heo0f834de2007-04-17 23:44:07 +09002583 if (rc) {
2584 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
2585 return -ENOMEM;
2586 }
2587 host->iomap = pcim_iomap_table(pdev);
2588
Tejun Heo1626aeb2007-05-04 12:43:58 +02002589 for (i = 0; i < 2; i++) {
Tejun Heo0f834de2007-04-17 23:44:07 +09002590 struct ata_port *ap = host->ports[i];
Tejun Heo0f834de2007-04-17 23:44:07 +09002591 void __iomem *bmdma = host->iomap[4] + 8 * i;
2592
2593 if (ata_port_is_dummy(ap))
2594 continue;
2595
Tejun Heo21b0ad42007-04-17 23:44:07 +09002596 ap->ioaddr.bmdma_addr = bmdma;
Tejun Heo0f834de2007-04-17 23:44:07 +09002597 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
2598 (ioread8(bmdma + 2) & 0x80))
2599 host->flags |= ATA_HOST_SIMPLEX;
Tejun Heocbcdd872007-08-18 13:14:55 +09002600
2601 ata_port_desc(ap, "bmdma 0x%llx",
Alan Cox0fe40ff2009-01-05 14:16:13 +00002602 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
Tejun Heo0d5ff562007-02-01 15:06:36 +09002603 }
2604
Tejun Heo0f834de2007-04-17 23:44:07 +09002605 return 0;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002606}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002607EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002608
Tejun Heo272f7882008-03-25 22:16:40 +09002609static int ata_resources_present(struct pci_dev *pdev, int port)
2610{
2611 int i;
2612
2613 /* Check the PCI resources for this channel are enabled */
2614 port = port * 2;
Alan Cox0fe40ff2009-01-05 14:16:13 +00002615 for (i = 0; i < 2; i++) {
Tejun Heo272f7882008-03-25 22:16:40 +09002616 if (pci_resource_start(pdev, port + i) == 0 ||
2617 pci_resource_len(pdev, port + i) == 0)
2618 return 0;
2619 }
2620 return 1;
2621}
2622
Tejun Heod491b272007-04-17 23:44:07 +09002623/**
Tejun Heo9363c382008-04-07 22:47:16 +09002624 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
Tejun Heod491b272007-04-17 23:44:07 +09002625 * @host: target ATA host
Tejun Heod491b272007-04-17 23:44:07 +09002626 *
Tejun Heo1626aeb2007-05-04 12:43:58 +02002627 * Acquire native PCI ATA resources for @host and initialize the
2628 * first two ports of @host accordingly. Ports marked dummy are
2629 * skipped and allocation failure makes the port dummy.
Tejun Heod491b272007-04-17 23:44:07 +09002630 *
Tejun Heod583bc12007-07-04 18:02:07 +09002631 * Note that native PCI resources are valid even for legacy hosts
2632 * as we fix up pdev resources array early in boot, so this
2633 * function can be used for both native and legacy SFF hosts.
2634 *
Tejun Heod491b272007-04-17 23:44:07 +09002635 * LOCKING:
2636 * Inherited from calling layer (may sleep).
2637 *
2638 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +02002639 * 0 if at least one port is initialized, -ENODEV if no port is
2640 * available.
Tejun Heod491b272007-04-17 23:44:07 +09002641 */
Tejun Heo9363c382008-04-07 22:47:16 +09002642int ata_pci_sff_init_host(struct ata_host *host)
Tejun Heod491b272007-04-17 23:44:07 +09002643{
2644 struct device *gdev = host->dev;
2645 struct pci_dev *pdev = to_pci_dev(gdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002646 unsigned int mask = 0;
Tejun Heod491b272007-04-17 23:44:07 +09002647 int i, rc;
2648
Tejun Heod491b272007-04-17 23:44:07 +09002649 /* request, iomap BARs and init port addresses accordingly */
2650 for (i = 0; i < 2; i++) {
2651 struct ata_port *ap = host->ports[i];
2652 int base = i * 2;
2653 void __iomem * const *iomap;
2654
Tejun Heo1626aeb2007-05-04 12:43:58 +02002655 if (ata_port_is_dummy(ap))
Tejun Heod491b272007-04-17 23:44:07 +09002656 continue;
2657
Tejun Heo1626aeb2007-05-04 12:43:58 +02002658 /* Discard disabled ports. Some controllers show
2659 * their unused channels this way. Disabled ports are
2660 * made dummy.
2661 */
2662 if (!ata_resources_present(pdev, i)) {
2663 ap->ops = &ata_dummy_port_ops;
2664 continue;
2665 }
2666
Tejun Heo35a10a82008-01-04 18:42:21 +09002667 rc = pcim_iomap_regions(pdev, 0x3 << base,
2668 dev_driver_string(gdev));
Tejun Heod491b272007-04-17 23:44:07 +09002669 if (rc) {
Tejun Heo1626aeb2007-05-04 12:43:58 +02002670 dev_printk(KERN_WARNING, gdev,
2671 "failed to request/iomap BARs for port %d "
2672 "(errno=%d)\n", i, rc);
Tejun Heod491b272007-04-17 23:44:07 +09002673 if (rc == -EBUSY)
2674 pcim_pin_device(pdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002675 ap->ops = &ata_dummy_port_ops;
2676 continue;
Tejun Heod491b272007-04-17 23:44:07 +09002677 }
2678 host->iomap = iomap = pcim_iomap_table(pdev);
2679
2680 ap->ioaddr.cmd_addr = iomap[base];
2681 ap->ioaddr.altstatus_addr =
2682 ap->ioaddr.ctl_addr = (void __iomem *)
2683 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
Tejun Heo9363c382008-04-07 22:47:16 +09002684 ata_sff_std_ports(&ap->ioaddr);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002685
Tejun Heocbcdd872007-08-18 13:14:55 +09002686 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2687 (unsigned long long)pci_resource_start(pdev, base),
2688 (unsigned long long)pci_resource_start(pdev, base + 1));
2689
Tejun Heo1626aeb2007-05-04 12:43:58 +02002690 mask |= 1 << i;
2691 }
2692
2693 if (!mask) {
2694 dev_printk(KERN_ERR, gdev, "no available native port\n");
2695 return -ENODEV;
Tejun Heod491b272007-04-17 23:44:07 +09002696 }
2697
2698 return 0;
2699}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002700EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
Tejun Heod491b272007-04-17 23:44:07 +09002701
Tejun Heo21b0ad42007-04-17 23:44:07 +09002702/**
Tejun Heo9363c382008-04-07 22:47:16 +09002703 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
Tejun Heo21b0ad42007-04-17 23:44:07 +09002704 * @pdev: target PCI device
Tejun Heo1626aeb2007-05-04 12:43:58 +02002705 * @ppi: array of port_info, must be enough for two ports
Tejun Heo21b0ad42007-04-17 23:44:07 +09002706 * @r_host: out argument for the initialized ATA host
2707 *
2708 * Helper to allocate ATA host for @pdev, acquire all native PCI
2709 * resources and initialize it accordingly in one go.
2710 *
2711 * LOCKING:
2712 * Inherited from calling layer (may sleep).
2713 *
2714 * RETURNS:
2715 * 0 on success, -errno otherwise.
2716 */
Tejun Heo9363c382008-04-07 22:47:16 +09002717int ata_pci_sff_prepare_host(struct pci_dev *pdev,
Alan Cox0fe40ff2009-01-05 14:16:13 +00002718 const struct ata_port_info * const *ppi,
Tejun Heod583bc12007-07-04 18:02:07 +09002719 struct ata_host **r_host)
Tejun Heo21b0ad42007-04-17 23:44:07 +09002720{
2721 struct ata_host *host;
Tejun Heo21b0ad42007-04-17 23:44:07 +09002722 int rc;
2723
2724 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2725 return -ENOMEM;
2726
2727 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2728 if (!host) {
2729 dev_printk(KERN_ERR, &pdev->dev,
2730 "failed to allocate ATA host\n");
2731 rc = -ENOMEM;
2732 goto err_out;
2733 }
2734
Tejun Heo9363c382008-04-07 22:47:16 +09002735 rc = ata_pci_sff_init_host(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002736 if (rc)
2737 goto err_out;
2738
2739 /* init DMA related stuff */
Tejun Heo9363c382008-04-07 22:47:16 +09002740 rc = ata_pci_bmdma_init(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002741 if (rc)
2742 goto err_bmdma;
2743
2744 devres_remove_group(&pdev->dev, NULL);
2745 *r_host = host;
2746 return 0;
2747
Alan Cox0fe40ff2009-01-05 14:16:13 +00002748err_bmdma:
Tejun Heo21b0ad42007-04-17 23:44:07 +09002749 /* This is necessary because PCI and iomap resources are
2750 * merged and releasing the top group won't release the
2751 * acquired resources if some of those have been acquired
2752 * before entering this function.
2753 */
2754 pcim_iounmap_regions(pdev, 0xf);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002755err_out:
Tejun Heo21b0ad42007-04-17 23:44:07 +09002756 devres_release_group(&pdev->dev, NULL);
2757 return rc;
2758}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002759EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002760
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002761/**
Tejun Heo9363c382008-04-07 22:47:16 +09002762 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002763 * @host: target SFF ATA host
2764 * @irq_handler: irq_handler used when requesting IRQ(s)
2765 * @sht: scsi_host_template to use when registering the host
2766 *
2767 * This is the counterpart of ata_host_activate() for SFF ATA
2768 * hosts. This separate helper is necessary because SFF hosts
2769 * use two separate interrupts in legacy mode.
2770 *
2771 * LOCKING:
2772 * Inherited from calling layer (may sleep).
2773 *
2774 * RETURNS:
2775 * 0 on success, -errno otherwise.
2776 */
Tejun Heo9363c382008-04-07 22:47:16 +09002777int ata_pci_sff_activate_host(struct ata_host *host,
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002778 irq_handler_t irq_handler,
2779 struct scsi_host_template *sht)
2780{
2781 struct device *dev = host->dev;
2782 struct pci_dev *pdev = to_pci_dev(dev);
2783 const char *drv_name = dev_driver_string(host->dev);
2784 int legacy_mode = 0, rc;
2785
2786 rc = ata_host_start(host);
2787 if (rc)
2788 return rc;
2789
2790 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
2791 u8 tmp8, mask;
2792
2793 /* TODO: What if one channel is in native mode ... */
2794 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
2795 mask = (1 << 2) | (1 << 0);
2796 if ((tmp8 & mask) != mask)
2797 legacy_mode = 1;
2798#if defined(CONFIG_NO_ATA_LEGACY)
2799 /* Some platforms with PCI limits cannot address compat
2800 port space. In that case we punt if their firmware has
2801 left a device in compatibility mode */
2802 if (legacy_mode) {
2803 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
2804 return -EOPNOTSUPP;
2805 }
2806#endif
2807 }
2808
2809 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2810 return -ENOMEM;
2811
2812 if (!legacy_mode && pdev->irq) {
2813 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2814 IRQF_SHARED, drv_name, host);
2815 if (rc)
2816 goto out;
2817
2818 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
2819 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
2820 } else if (legacy_mode) {
2821 if (!ata_port_is_dummy(host->ports[0])) {
2822 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2823 irq_handler, IRQF_SHARED,
2824 drv_name, host);
2825 if (rc)
2826 goto out;
2827
2828 ata_port_desc(host->ports[0], "irq %d",
2829 ATA_PRIMARY_IRQ(pdev));
2830 }
2831
2832 if (!ata_port_is_dummy(host->ports[1])) {
2833 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2834 irq_handler, IRQF_SHARED,
2835 drv_name, host);
2836 if (rc)
2837 goto out;
2838
2839 ata_port_desc(host->ports[1], "irq %d",
2840 ATA_SECONDARY_IRQ(pdev));
2841 }
2842 }
2843
2844 rc = ata_host_register(host, sht);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002845out:
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002846 if (rc == 0)
2847 devres_remove_group(dev, NULL);
2848 else
2849 devres_release_group(dev, NULL);
2850
2851 return rc;
2852}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002853EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002854
2855/**
Tejun Heo9363c382008-04-07 22:47:16 +09002856 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002857 * @pdev: Controller to be initialized
Tejun Heo1626aeb2007-05-04 12:43:58 +02002858 * @ppi: array of port_info, must be enough for two ports
Tejun Heo1bd5b712008-03-25 12:22:49 +09002859 * @sht: scsi_host_template to use when registering the host
Tejun Heo887125e2008-03-25 12:22:49 +09002860 * @host_priv: host private_data
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002861 *
2862 * This is a helper function which can be called from a driver's
2863 * xxx_init_one() probe function if the hardware uses traditional
2864 * IDE taskfile registers.
2865 *
2866 * This function calls pci_enable_device(), reserves its register
2867 * regions, sets the dma mask, enables bus master mode, and calls
2868 * ata_device_add()
2869 *
Alan Cox2ec7df02006-08-10 16:59:10 +09002870 * ASSUMPTION:
2871 * Nobody makes a single channel controller that appears solely as
2872 * the secondary legacy port on PCI.
2873 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002874 * LOCKING:
2875 * Inherited from PCI layer (may sleep).
2876 *
2877 * RETURNS:
2878 * Zero on success, negative on errno-based value on error.
2879 */
Tejun Heo9363c382008-04-07 22:47:16 +09002880int ata_pci_sff_init_one(struct pci_dev *pdev,
Alan Cox0fe40ff2009-01-05 14:16:13 +00002881 const struct ata_port_info * const *ppi,
Tejun Heo9363c382008-04-07 22:47:16 +09002882 struct scsi_host_template *sht, void *host_priv)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002883{
Tejun Heof0d36ef2007-01-20 16:00:28 +09002884 struct device *dev = &pdev->dev;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002885 const struct ata_port_info *pi = NULL;
Tejun Heo0f834de2007-04-17 23:44:07 +09002886 struct ata_host *host = NULL;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002887 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002888
2889 DPRINTK("ENTER\n");
2890
Tejun Heo1626aeb2007-05-04 12:43:58 +02002891 /* look up the first valid port_info */
2892 for (i = 0; i < 2 && ppi[i]; i++) {
2893 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
2894 pi = ppi[i];
2895 break;
2896 }
2897 }
2898
2899 if (!pi) {
2900 dev_printk(KERN_ERR, &pdev->dev,
2901 "no valid port_info specified\n");
2902 return -EINVAL;
2903 }
2904
Tejun Heof0d36ef2007-01-20 16:00:28 +09002905 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2906 return -ENOMEM;
2907
Tejun Heof0d36ef2007-01-20 16:00:28 +09002908 rc = pcim_enable_device(pdev);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002909 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002910 goto out;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002911
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002912 /* prepare and activate SFF host */
Tejun Heo9363c382008-04-07 22:47:16 +09002913 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
Tejun Heod583bc12007-07-04 18:02:07 +09002914 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002915 goto out;
Tejun Heo887125e2008-03-25 12:22:49 +09002916 host->private_data = host_priv;
Tejun Heod491b272007-04-17 23:44:07 +09002917
Tejun Heod491b272007-04-17 23:44:07 +09002918 pci_set_master(pdev);
Tejun Heo9363c382008-04-07 22:47:16 +09002919 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002920out:
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002921 if (rc == 0)
2922 devres_remove_group(&pdev->dev, NULL);
2923 else
2924 devres_release_group(&pdev->dev, NULL);
Tejun Heod491b272007-04-17 23:44:07 +09002925
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002926 return rc;
2927}
Tejun Heo9363c382008-04-07 22:47:16 +09002928EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002929
Tejun Heo624d5c52008-03-25 22:16:41 +09002930#endif /* CONFIG_PCI */
Alan Cox0fe40ff2009-01-05 14:16:13 +00002931