blob: 9a10cb055ac2e554c6025cc35bd5c40677df3cce [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,
Alan Cox3d47aa82009-03-24 10:23:19 +000055 .drain_fifo = ata_sff_drain_fifo,
Tejun Heo9363c382008-04-07 22:47:16 +090056 .error_handler = ata_sff_error_handler,
57 .post_internal_cmd = ata_sff_post_internal_cmd,
Tejun Heo624d5c52008-03-25 22:16:41 +090058
Tejun Heo5682ed32008-04-07 22:47:16 +090059 .sff_dev_select = ata_sff_dev_select,
60 .sff_check_status = ata_sff_check_status,
61 .sff_tf_load = ata_sff_tf_load,
62 .sff_tf_read = ata_sff_tf_read,
63 .sff_exec_command = ata_sff_exec_command,
64 .sff_data_xfer = ata_sff_data_xfer,
65 .sff_irq_on = ata_sff_irq_on,
Tejun Heo288623a2008-04-07 22:47:17 +090066 .sff_irq_clear = ata_sff_irq_clear,
Tejun Heo624d5c52008-03-25 22:16:41 +090067
68 .port_start = ata_sff_port_start,
69};
Alan Cox0fe40ff2009-01-05 14:16:13 +000070EXPORT_SYMBOL_GPL(ata_sff_port_ops);
Tejun Heo624d5c52008-03-25 22:16:41 +090071
72const struct ata_port_operations ata_bmdma_port_ops = {
73 .inherits = &ata_sff_port_ops,
74
Tejun Heo9363c382008-04-07 22:47:16 +090075 .mode_filter = ata_bmdma_mode_filter,
Tejun Heo624d5c52008-03-25 22:16:41 +090076
77 .bmdma_setup = ata_bmdma_setup,
78 .bmdma_start = ata_bmdma_start,
79 .bmdma_stop = ata_bmdma_stop,
80 .bmdma_status = ata_bmdma_status,
Tejun Heo624d5c52008-03-25 22:16:41 +090081};
Alan Cox0fe40ff2009-01-05 14:16:13 +000082EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
Tejun Heo624d5c52008-03-25 22:16:41 +090083
Alan Cox871af122009-01-05 14:16:39 +000084const struct ata_port_operations ata_bmdma32_port_ops = {
85 .inherits = &ata_bmdma_port_ops,
86
87 .sff_data_xfer = ata_sff_data_xfer32,
88};
89EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
90
Tejun Heo624d5c52008-03-25 22:16:41 +090091/**
92 * ata_fill_sg - Fill PCI IDE PRD table
93 * @qc: Metadata associated with taskfile to be transferred
94 *
95 * Fill PCI IDE PRD (scatter-gather) table with segments
96 * associated with the current disk command.
97 *
98 * LOCKING:
99 * spin_lock_irqsave(host lock)
100 *
101 */
102static void ata_fill_sg(struct ata_queued_cmd *qc)
103{
104 struct ata_port *ap = qc->ap;
105 struct scatterlist *sg;
106 unsigned int si, pi;
107
108 pi = 0;
109 for_each_sg(qc->sg, sg, qc->n_elem, si) {
110 u32 addr, offset;
111 u32 sg_len, len;
112
113 /* determine if physical DMA addr spans 64K boundary.
114 * Note h/w doesn't support 64-bit, so we unconditionally
115 * truncate dma_addr_t to u32.
116 */
117 addr = (u32) sg_dma_address(sg);
118 sg_len = sg_dma_len(sg);
119
120 while (sg_len) {
121 offset = addr & 0xffff;
122 len = sg_len;
123 if ((offset + sg_len) > 0x10000)
124 len = 0x10000 - offset;
125
126 ap->prd[pi].addr = cpu_to_le32(addr);
127 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
128 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
129
130 pi++;
131 sg_len -= len;
132 addr += len;
133 }
134 }
135
136 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
137}
138
139/**
140 * ata_fill_sg_dumb - Fill PCI IDE PRD table
141 * @qc: Metadata associated with taskfile to be transferred
142 *
143 * Fill PCI IDE PRD (scatter-gather) table with segments
144 * associated with the current disk command. Perform the fill
145 * so that we avoid writing any length 64K records for
146 * controllers that don't follow the spec.
147 *
148 * LOCKING:
149 * spin_lock_irqsave(host lock)
150 *
151 */
152static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
153{
154 struct ata_port *ap = qc->ap;
155 struct scatterlist *sg;
156 unsigned int si, pi;
157
158 pi = 0;
159 for_each_sg(qc->sg, sg, qc->n_elem, si) {
160 u32 addr, offset;
161 u32 sg_len, len, blen;
162
163 /* determine if physical DMA addr spans 64K boundary.
164 * Note h/w doesn't support 64-bit, so we unconditionally
165 * truncate dma_addr_t to u32.
166 */
167 addr = (u32) sg_dma_address(sg);
168 sg_len = sg_dma_len(sg);
169
170 while (sg_len) {
171 offset = addr & 0xffff;
172 len = sg_len;
173 if ((offset + sg_len) > 0x10000)
174 len = 0x10000 - offset;
175
176 blen = len & 0xffff;
177 ap->prd[pi].addr = cpu_to_le32(addr);
178 if (blen == 0) {
Alan Cox0fe40ff2009-01-05 14:16:13 +0000179 /* Some PATA chipsets like the CS5530 can't
180 cope with 0x0000 meaning 64K as the spec
181 says */
Tejun Heo624d5c52008-03-25 22:16:41 +0900182 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
183 blen = 0x8000;
184 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
185 }
186 ap->prd[pi].flags_len = cpu_to_le32(blen);
187 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
188
189 pi++;
190 sg_len -= len;
191 addr += len;
192 }
193 }
194
195 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
196}
197
198/**
Tejun Heo9363c382008-04-07 22:47:16 +0900199 * ata_sff_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900200 * @qc: Metadata associated with taskfile to be prepared
201 *
202 * Prepare ATA taskfile for submission.
203 *
204 * LOCKING:
205 * spin_lock_irqsave(host lock)
206 */
Tejun Heo9363c382008-04-07 22:47:16 +0900207void ata_sff_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900208{
209 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
210 return;
211
212 ata_fill_sg(qc);
213}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000214EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900215
216/**
Tejun Heo9363c382008-04-07 22:47:16 +0900217 * ata_sff_dumb_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900218 * @qc: Metadata associated with taskfile to be prepared
219 *
220 * Prepare ATA taskfile for submission.
221 *
222 * LOCKING:
223 * spin_lock_irqsave(host lock)
224 */
Tejun Heo9363c382008-04-07 22:47:16 +0900225void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900226{
227 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
228 return;
229
230 ata_fill_sg_dumb(qc);
231}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000232EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900233
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500234/**
Tejun Heo9363c382008-04-07 22:47:16 +0900235 * ata_sff_check_status - Read device status reg & clear interrupt
Tejun Heo272f7882008-03-25 22:16:40 +0900236 * @ap: port where the device is
237 *
238 * Reads ATA taskfile status register for currently-selected device
239 * and return its value. This also clears pending interrupts
240 * from this device
241 *
242 * LOCKING:
243 * Inherited from caller.
244 */
Tejun Heo9363c382008-04-07 22:47:16 +0900245u8 ata_sff_check_status(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900246{
247 return ioread8(ap->ioaddr.status_addr);
248}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000249EXPORT_SYMBOL_GPL(ata_sff_check_status);
Tejun Heo272f7882008-03-25 22:16:40 +0900250
251/**
Tejun Heo9363c382008-04-07 22:47:16 +0900252 * ata_sff_altstatus - Read device alternate status reg
Tejun Heo272f7882008-03-25 22:16:40 +0900253 * @ap: port where the device is
254 *
255 * Reads ATA taskfile alternate status register for
256 * currently-selected device and return its value.
257 *
258 * Note: may NOT be used as the check_altstatus() entry in
259 * ata_port_operations.
260 *
261 * LOCKING:
262 * Inherited from caller.
263 */
Alan Coxa57c1ba2008-05-29 22:10:58 +0100264static u8 ata_sff_altstatus(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900265{
Tejun Heo5682ed32008-04-07 22:47:16 +0900266 if (ap->ops->sff_check_altstatus)
267 return ap->ops->sff_check_altstatus(ap);
Tejun Heo272f7882008-03-25 22:16:40 +0900268
269 return ioread8(ap->ioaddr.altstatus_addr);
270}
271
272/**
Alan Coxa57c1ba2008-05-29 22:10:58 +0100273 * ata_sff_irq_status - Check if the device is busy
274 * @ap: port where the device is
275 *
276 * Determine if the port is currently busy. Uses altstatus
277 * if available in order to avoid clearing shared IRQ status
278 * when finding an IRQ source. Non ctl capable devices don't
279 * share interrupt lines fortunately for us.
280 *
281 * LOCKING:
282 * Inherited from caller.
283 */
284static u8 ata_sff_irq_status(struct ata_port *ap)
285{
286 u8 status;
287
288 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
289 status = ata_sff_altstatus(ap);
290 /* Not us: We are busy */
291 if (status & ATA_BUSY)
Alan Cox0fe40ff2009-01-05 14:16:13 +0000292 return status;
Alan Coxa57c1ba2008-05-29 22:10:58 +0100293 }
294 /* Clear INTRQ latch */
Hugh Dickins6311c902008-06-05 14:44:39 +0100295 status = ap->ops->sff_check_status(ap);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100296 return status;
297}
298
299/**
300 * ata_sff_sync - Flush writes
301 * @ap: Port to wait for.
302 *
303 * CAUTION:
304 * If we have an mmio device with no ctl and no altstatus
305 * method this will fail. No such devices are known to exist.
306 *
307 * LOCKING:
308 * Inherited from caller.
309 */
310
311static void ata_sff_sync(struct ata_port *ap)
312{
313 if (ap->ops->sff_check_altstatus)
314 ap->ops->sff_check_altstatus(ap);
315 else if (ap->ioaddr.altstatus_addr)
316 ioread8(ap->ioaddr.altstatus_addr);
317}
318
319/**
320 * ata_sff_pause - Flush writes and wait 400nS
321 * @ap: Port to pause for.
322 *
323 * CAUTION:
324 * If we have an mmio device with no ctl and no altstatus
325 * method this will fail. No such devices are known to exist.
326 *
327 * LOCKING:
328 * Inherited from caller.
329 */
330
331void ata_sff_pause(struct ata_port *ap)
332{
333 ata_sff_sync(ap);
334 ndelay(400);
335}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000336EXPORT_SYMBOL_GPL(ata_sff_pause);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100337
338/**
339 * ata_sff_dma_pause - Pause before commencing DMA
340 * @ap: Port to pause for.
341 *
342 * Perform I/O fencing and ensure sufficient cycle delays occur
343 * for the HDMA1:0 transition
344 */
Alan Cox0fe40ff2009-01-05 14:16:13 +0000345
Alan Coxa57c1ba2008-05-29 22:10:58 +0100346void ata_sff_dma_pause(struct ata_port *ap)
347{
348 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
349 /* An altstatus read will cause the needed delay without
350 messing up the IRQ status */
351 ata_sff_altstatus(ap);
352 return;
353 }
354 /* There are no DMA controllers without ctl. BUG here to ensure
355 we never violate the HDMA1:0 transition timing and risk
356 corruption. */
357 BUG();
358}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000359EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100360
361/**
Tejun Heo9363c382008-04-07 22:47:16 +0900362 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
Tejun Heo624d5c52008-03-25 22:16:41 +0900363 * @ap: port containing status register to be polled
Tejun Heo341c2c92008-05-20 02:17:51 +0900364 * @tmout_pat: impatience timeout in msecs
365 * @tmout: overall timeout in msecs
Tejun Heo624d5c52008-03-25 22:16:41 +0900366 *
367 * Sleep until ATA Status register bit BSY clears,
368 * or a timeout occurs.
369 *
370 * LOCKING:
371 * Kernel thread context (may sleep).
372 *
373 * RETURNS:
374 * 0 on success, -errno otherwise.
375 */
Tejun Heo9363c382008-04-07 22:47:16 +0900376int ata_sff_busy_sleep(struct ata_port *ap,
377 unsigned long tmout_pat, unsigned long tmout)
Tejun Heo624d5c52008-03-25 22:16:41 +0900378{
379 unsigned long timer_start, timeout;
380 u8 status;
381
Tejun Heo9363c382008-04-07 22:47:16 +0900382 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
Tejun Heo624d5c52008-03-25 22:16:41 +0900383 timer_start = jiffies;
Tejun Heo341c2c92008-05-20 02:17:51 +0900384 timeout = ata_deadline(timer_start, tmout_pat);
Tejun Heo624d5c52008-03-25 22:16:41 +0900385 while (status != 0xff && (status & ATA_BUSY) &&
386 time_before(jiffies, timeout)) {
387 msleep(50);
Tejun Heo9363c382008-04-07 22:47:16 +0900388 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
Tejun Heo624d5c52008-03-25 22:16:41 +0900389 }
390
391 if (status != 0xff && (status & ATA_BUSY))
392 ata_port_printk(ap, KERN_WARNING,
393 "port is slow to respond, please be patient "
394 "(Status 0x%x)\n", status);
395
Tejun Heo341c2c92008-05-20 02:17:51 +0900396 timeout = ata_deadline(timer_start, tmout);
Tejun Heo624d5c52008-03-25 22:16:41 +0900397 while (status != 0xff && (status & ATA_BUSY) &&
398 time_before(jiffies, timeout)) {
399 msleep(50);
Tejun Heo5682ed32008-04-07 22:47:16 +0900400 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900401 }
402
403 if (status == 0xff)
404 return -ENODEV;
405
406 if (status & ATA_BUSY) {
407 ata_port_printk(ap, KERN_ERR, "port failed to respond "
408 "(%lu secs, Status 0x%x)\n",
Tejun Heo341c2c92008-05-20 02:17:51 +0900409 DIV_ROUND_UP(tmout, 1000), status);
Tejun Heo624d5c52008-03-25 22:16:41 +0900410 return -EBUSY;
411 }
412
413 return 0;
414}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000415EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900416
Tejun Heoaa2731a2008-04-07 22:47:19 +0900417static int ata_sff_check_ready(struct ata_link *link)
418{
419 u8 status = link->ap->ops->sff_check_status(link->ap);
420
Tejun Heo78ab88f2008-05-01 23:41:41 +0900421 return ata_check_ready(status);
Tejun Heoaa2731a2008-04-07 22:47:19 +0900422}
423
Tejun Heo624d5c52008-03-25 22:16:41 +0900424/**
Tejun Heo9363c382008-04-07 22:47:16 +0900425 * ata_sff_wait_ready - sleep until BSY clears, or timeout
Tejun Heo705e76b2008-04-07 22:47:19 +0900426 * @link: SFF link to wait ready status for
Tejun Heo624d5c52008-03-25 22:16:41 +0900427 * @deadline: deadline jiffies for the operation
428 *
429 * Sleep until ATA Status register bit BSY clears, or timeout
430 * occurs.
431 *
432 * LOCKING:
433 * Kernel thread context (may sleep).
434 *
435 * RETURNS:
436 * 0 on success, -errno otherwise.
437 */
Tejun Heo705e76b2008-04-07 22:47:19 +0900438int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +0900439{
Tejun Heoaa2731a2008-04-07 22:47:19 +0900440 return ata_wait_ready(link, deadline, ata_sff_check_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900441}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000442EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900443
444/**
Tejun Heo9363c382008-04-07 22:47:16 +0900445 * ata_sff_dev_select - Select device 0/1 on ATA bus
Tejun Heo624d5c52008-03-25 22:16:41 +0900446 * @ap: ATA channel to manipulate
447 * @device: ATA device (numbered from zero) to select
448 *
449 * Use the method defined in the ATA specification to
450 * make either device 0, or device 1, active on the
451 * ATA channel. Works with both PIO and MMIO.
452 *
453 * May be used as the dev_select() entry in ata_port_operations.
454 *
455 * LOCKING:
456 * caller.
457 */
Tejun Heo9363c382008-04-07 22:47:16 +0900458void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
Tejun Heo624d5c52008-03-25 22:16:41 +0900459{
460 u8 tmp;
461
462 if (device == 0)
463 tmp = ATA_DEVICE_OBS;
464 else
465 tmp = ATA_DEVICE_OBS | ATA_DEV1;
466
467 iowrite8(tmp, ap->ioaddr.device_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900468 ata_sff_pause(ap); /* needed; also flushes, for mmio */
Tejun Heo624d5c52008-03-25 22:16:41 +0900469}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000470EXPORT_SYMBOL_GPL(ata_sff_dev_select);
Tejun Heo624d5c52008-03-25 22:16:41 +0900471
472/**
473 * ata_dev_select - Select device 0/1 on ATA bus
474 * @ap: ATA channel to manipulate
475 * @device: ATA device (numbered from zero) to select
476 * @wait: non-zero to wait for Status register BSY bit to clear
477 * @can_sleep: non-zero if context allows sleeping
478 *
479 * Use the method defined in the ATA specification to
480 * make either device 0, or device 1, active on the
481 * ATA channel.
482 *
Tejun Heo9363c382008-04-07 22:47:16 +0900483 * This is a high-level version of ata_sff_dev_select(), which
484 * additionally provides the services of inserting the proper
485 * pauses and status polling, where needed.
Tejun Heo624d5c52008-03-25 22:16:41 +0900486 *
487 * LOCKING:
488 * caller.
489 */
490void ata_dev_select(struct ata_port *ap, unsigned int device,
491 unsigned int wait, unsigned int can_sleep)
492{
493 if (ata_msg_probe(ap))
494 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
495 "device %u, wait %u\n", device, wait);
496
497 if (wait)
498 ata_wait_idle(ap);
499
Tejun Heo5682ed32008-04-07 22:47:16 +0900500 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +0900501
502 if (wait) {
503 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
504 msleep(150);
505 ata_wait_idle(ap);
506 }
507}
508
509/**
Tejun Heo9363c382008-04-07 22:47:16 +0900510 * ata_sff_irq_on - Enable interrupts on a port.
Tejun Heo90088bb2006-10-09 11:10:26 +0900511 * @ap: Port on which interrupts are enabled.
512 *
513 * Enable interrupts on a legacy IDE device using MMIO or PIO,
514 * wait for idle, clear any pending interrupts.
515 *
516 * LOCKING:
517 * Inherited from caller.
518 */
Tejun Heo9363c382008-04-07 22:47:16 +0900519u8 ata_sff_irq_on(struct ata_port *ap)
Tejun Heo90088bb2006-10-09 11:10:26 +0900520{
521 struct ata_ioports *ioaddr = &ap->ioaddr;
522 u8 tmp;
523
524 ap->ctl &= ~ATA_NIEN;
525 ap->last_ctl = ap->ctl;
526
Tejun Heof659f0e42008-03-06 13:12:54 +0900527 if (ioaddr->ctl_addr)
528 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo90088bb2006-10-09 11:10:26 +0900529 tmp = ata_wait_idle(ap);
530
Tejun Heo5682ed32008-04-07 22:47:16 +0900531 ap->ops->sff_irq_clear(ap);
Tejun Heo90088bb2006-10-09 11:10:26 +0900532
533 return tmp;
534}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000535EXPORT_SYMBOL_GPL(ata_sff_irq_on);
Tejun Heo90088bb2006-10-09 11:10:26 +0900536
537/**
Tejun Heo9363c382008-04-07 22:47:16 +0900538 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
Tejun Heo272f7882008-03-25 22:16:40 +0900539 * @ap: Port associated with this ATA transaction.
540 *
541 * Clear interrupt and error flags in DMA status register.
542 *
543 * May be used as the irq_clear() entry in ata_port_operations.
544 *
545 * LOCKING:
546 * spin_lock_irqsave(host lock)
547 */
Tejun Heo9363c382008-04-07 22:47:16 +0900548void ata_sff_irq_clear(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900549{
550 void __iomem *mmio = ap->ioaddr.bmdma_addr;
551
552 if (!mmio)
553 return;
554
555 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
556}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000557EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
Tejun Heo272f7882008-03-25 22:16:40 +0900558
559/**
Tejun Heo9363c382008-04-07 22:47:16 +0900560 * ata_sff_tf_load - send taskfile registers to host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500561 * @ap: Port to which output is sent
562 * @tf: ATA taskfile register set
563 *
564 * Outputs ATA taskfile to standard ATA host controller.
565 *
566 * LOCKING:
567 * Inherited from caller.
568 */
Tejun Heo9363c382008-04-07 22:47:16 +0900569void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500570{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900571 struct ata_ioports *ioaddr = &ap->ioaddr;
572 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
573
574 if (tf->ctl != ap->last_ctl) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900575 if (ioaddr->ctl_addr)
576 iowrite8(tf->ctl, ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900577 ap->last_ctl = tf->ctl;
578 ata_wait_idle(ap);
579 }
580
581 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900582 WARN_ON_ONCE(!ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900583 iowrite8(tf->hob_feature, ioaddr->feature_addr);
584 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
585 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
586 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
587 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
588 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
589 tf->hob_feature,
590 tf->hob_nsect,
591 tf->hob_lbal,
592 tf->hob_lbam,
593 tf->hob_lbah);
594 }
595
596 if (is_addr) {
597 iowrite8(tf->feature, ioaddr->feature_addr);
598 iowrite8(tf->nsect, ioaddr->nsect_addr);
599 iowrite8(tf->lbal, ioaddr->lbal_addr);
600 iowrite8(tf->lbam, ioaddr->lbam_addr);
601 iowrite8(tf->lbah, ioaddr->lbah_addr);
602 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
603 tf->feature,
604 tf->nsect,
605 tf->lbal,
606 tf->lbam,
607 tf->lbah);
608 }
609
610 if (tf->flags & ATA_TFLAG_DEVICE) {
611 iowrite8(tf->device, ioaddr->device_addr);
612 VPRINTK("device 0x%X\n", tf->device);
613 }
614
615 ata_wait_idle(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500616}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000617EXPORT_SYMBOL_GPL(ata_sff_tf_load);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500618
619/**
Tejun Heo9363c382008-04-07 22:47:16 +0900620 * ata_sff_tf_read - input device's ATA taskfile shadow registers
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500621 * @ap: Port from which input is read
622 * @tf: ATA taskfile register set for storing input
623 *
624 * Reads ATA taskfile registers for currently-selected device
Alan Cox76548ed2007-11-19 14:34:56 +0000625 * into @tf. Assumes the device has a fully SFF compliant task file
626 * layout and behaviour. If you device does not (eg has a different
627 * status method) then you will need to provide a replacement tf_read
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500628 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500629 * LOCKING:
630 * Inherited from caller.
631 */
Tejun Heo9363c382008-04-07 22:47:16 +0900632void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500633{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900634 struct ata_ioports *ioaddr = &ap->ioaddr;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500635
Tejun Heo9363c382008-04-07 22:47:16 +0900636 tf->command = ata_sff_check_status(ap);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900637 tf->feature = ioread8(ioaddr->error_addr);
638 tf->nsect = ioread8(ioaddr->nsect_addr);
639 tf->lbal = ioread8(ioaddr->lbal_addr);
640 tf->lbam = ioread8(ioaddr->lbam_addr);
641 tf->lbah = ioread8(ioaddr->lbah_addr);
642 tf->device = ioread8(ioaddr->device_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500643
Tejun Heo0d5ff562007-02-01 15:06:36 +0900644 if (tf->flags & ATA_TFLAG_LBA48) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900645 if (likely(ioaddr->ctl_addr)) {
646 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
647 tf->hob_feature = ioread8(ioaddr->error_addr);
648 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
649 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
650 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
651 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
652 iowrite8(tf->ctl, ioaddr->ctl_addr);
653 ap->last_ctl = tf->ctl;
654 } else
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900655 WARN_ON_ONCE(1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900656 }
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500657}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000658EXPORT_SYMBOL_GPL(ata_sff_tf_read);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500659
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500660/**
Tejun Heo9363c382008-04-07 22:47:16 +0900661 * ata_sff_exec_command - issue ATA command to host controller
Tejun Heo272f7882008-03-25 22:16:40 +0900662 * @ap: port to which command is being issued
663 * @tf: ATA taskfile register set
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500664 *
Tejun Heo272f7882008-03-25 22:16:40 +0900665 * Issues ATA command, with proper synchronization with interrupt
666 * handler / other threads.
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500667 *
668 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400669 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500670 */
Tejun Heo9363c382008-04-07 22:47:16 +0900671void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500672{
Tejun Heo272f7882008-03-25 22:16:40 +0900673 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500674
Tejun Heo272f7882008-03-25 22:16:40 +0900675 iowrite8(tf->command, ap->ioaddr.command_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900676 ata_sff_pause(ap);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500677}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000678EXPORT_SYMBOL_GPL(ata_sff_exec_command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500679
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900680/**
Tejun Heo624d5c52008-03-25 22:16:41 +0900681 * ata_tf_to_host - issue ATA taskfile to host controller
682 * @ap: port to which command is being issued
683 * @tf: ATA taskfile register set
684 *
685 * Issues ATA taskfile register set to ATA host controller,
686 * with proper synchronization with interrupt handler and
687 * other threads.
688 *
689 * LOCKING:
690 * spin_lock_irqsave(host lock)
691 */
692static inline void ata_tf_to_host(struct ata_port *ap,
693 const struct ata_taskfile *tf)
694{
Tejun Heo5682ed32008-04-07 22:47:16 +0900695 ap->ops->sff_tf_load(ap, tf);
696 ap->ops->sff_exec_command(ap, tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900697}
698
699/**
Tejun Heo9363c382008-04-07 22:47:16 +0900700 * ata_sff_data_xfer - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900701 * @dev: device to target
702 * @buf: data buffer
703 * @buflen: buffer length
704 * @rw: read/write
705 *
706 * Transfer data from/to the device data register by PIO.
707 *
708 * LOCKING:
709 * Inherited from caller.
710 *
711 * RETURNS:
712 * Bytes consumed.
713 */
Tejun Heo9363c382008-04-07 22:47:16 +0900714unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
715 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900716{
717 struct ata_port *ap = dev->link->ap;
718 void __iomem *data_addr = ap->ioaddr.data_addr;
719 unsigned int words = buflen >> 1;
720
721 /* Transfer multiple of 2 bytes */
722 if (rw == READ)
723 ioread16_rep(data_addr, buf, words);
724 else
725 iowrite16_rep(data_addr, buf, words);
726
727 /* Transfer trailing 1 byte, if any. */
728 if (unlikely(buflen & 0x01)) {
729 __le16 align_buf[1] = { 0 };
730 unsigned char *trailing_buf = buf + buflen - 1;
731
732 if (rw == READ) {
733 align_buf[0] = cpu_to_le16(ioread16(data_addr));
734 memcpy(trailing_buf, align_buf, 1);
735 } else {
736 memcpy(align_buf, trailing_buf, 1);
737 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
738 }
739 words++;
740 }
741
742 return words << 1;
743}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000744EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
Tejun Heo624d5c52008-03-25 22:16:41 +0900745
746/**
Alan Cox871af122009-01-05 14:16:39 +0000747 * ata_sff_data_xfer32 - Transfer data by PIO
748 * @dev: device to target
749 * @buf: data buffer
750 * @buflen: buffer length
751 * @rw: read/write
752 *
753 * Transfer data from/to the device data register by PIO using 32bit
754 * I/O operations.
755 *
756 * LOCKING:
757 * Inherited from caller.
758 *
759 * RETURNS:
760 * Bytes consumed.
761 */
762
763unsigned int ata_sff_data_xfer32(struct ata_device *dev, unsigned char *buf,
764 unsigned int buflen, int rw)
765{
766 struct ata_port *ap = dev->link->ap;
767 void __iomem *data_addr = ap->ioaddr.data_addr;
768 unsigned int words = buflen >> 2;
769 int slop = buflen & 3;
770
771 /* Transfer multiple of 4 bytes */
772 if (rw == READ)
773 ioread32_rep(data_addr, buf, words);
774 else
775 iowrite32_rep(data_addr, buf, words);
776
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400777 /* Transfer trailing bytes, if any */
Alan Cox871af122009-01-05 14:16:39 +0000778 if (unlikely(slop)) {
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400779 unsigned char pad[4];
780
781 /* Point buf to the tail of buffer */
782 buf += buflen - slop;
783
784 /*
785 * Use io*_rep() accessors here as well to avoid pointlessly
786 * swapping bytes to and fro on the big endian machines...
787 */
Alan Cox871af122009-01-05 14:16:39 +0000788 if (rw == READ) {
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400789 if (slop < 3)
790 ioread16_rep(data_addr, pad, 1);
791 else
792 ioread32_rep(data_addr, pad, 1);
793 memcpy(buf, pad, slop);
Alan Cox871af122009-01-05 14:16:39 +0000794 } else {
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400795 memcpy(pad, buf, slop);
796 if (slop < 3)
797 iowrite16_rep(data_addr, pad, 1);
798 else
799 iowrite32_rep(data_addr, pad, 1);
Alan Cox871af122009-01-05 14:16:39 +0000800 }
Alan Cox871af122009-01-05 14:16:39 +0000801 }
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400802 return (buflen + 1) & ~1;
Alan Cox871af122009-01-05 14:16:39 +0000803}
804EXPORT_SYMBOL_GPL(ata_sff_data_xfer32);
805
806/**
Tejun Heo9363c382008-04-07 22:47:16 +0900807 * ata_sff_data_xfer_noirq - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900808 * @dev: device to target
809 * @buf: data buffer
810 * @buflen: buffer length
811 * @rw: read/write
812 *
813 * Transfer data from/to the device data register by PIO. Do the
814 * transfer with interrupts disabled.
815 *
816 * LOCKING:
817 * Inherited from caller.
818 *
819 * RETURNS:
820 * Bytes consumed.
821 */
Tejun Heo9363c382008-04-07 22:47:16 +0900822unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
823 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900824{
825 unsigned long flags;
826 unsigned int consumed;
827
828 local_irq_save(flags);
Tejun Heo9363c382008-04-07 22:47:16 +0900829 consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900830 local_irq_restore(flags);
831
832 return consumed;
833}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000834EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
Tejun Heo624d5c52008-03-25 22:16:41 +0900835
836/**
837 * ata_pio_sector - Transfer a sector of data.
838 * @qc: Command on going
839 *
840 * Transfer qc->sect_size bytes of data from/to the ATA device.
841 *
842 * LOCKING:
843 * Inherited from caller.
844 */
845static void ata_pio_sector(struct ata_queued_cmd *qc)
846{
847 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
848 struct ata_port *ap = qc->ap;
849 struct page *page;
850 unsigned int offset;
851 unsigned char *buf;
852
853 if (qc->curbytes == qc->nbytes - qc->sect_size)
854 ap->hsm_task_state = HSM_ST_LAST;
855
856 page = sg_page(qc->cursg);
857 offset = qc->cursg->offset + qc->cursg_ofs;
858
859 /* get the current page and offset */
860 page = nth_page(page, (offset >> PAGE_SHIFT));
861 offset %= PAGE_SIZE;
862
863 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
864
865 if (PageHighMem(page)) {
866 unsigned long flags;
867
868 /* FIXME: use a bounce buffer */
869 local_irq_save(flags);
870 buf = kmap_atomic(page, KM_IRQ0);
871
872 /* do the actual data transfer */
Tejun Heo5682ed32008-04-07 22:47:16 +0900873 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
874 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900875
876 kunmap_atomic(buf, KM_IRQ0);
877 local_irq_restore(flags);
878 } else {
879 buf = page_address(page);
Tejun Heo5682ed32008-04-07 22:47:16 +0900880 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
881 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900882 }
883
884 qc->curbytes += qc->sect_size;
885 qc->cursg_ofs += qc->sect_size;
886
887 if (qc->cursg_ofs == qc->cursg->length) {
888 qc->cursg = sg_next(qc->cursg);
889 qc->cursg_ofs = 0;
890 }
891}
892
893/**
894 * ata_pio_sectors - Transfer one or many sectors.
895 * @qc: Command on going
896 *
897 * Transfer one or many sectors of data from/to the
898 * ATA device for the DRQ request.
899 *
900 * LOCKING:
901 * Inherited from caller.
902 */
903static void ata_pio_sectors(struct ata_queued_cmd *qc)
904{
905 if (is_multi_taskfile(&qc->tf)) {
906 /* READ/WRITE MULTIPLE */
907 unsigned int nsect;
908
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900909 WARN_ON_ONCE(qc->dev->multi_count == 0);
Tejun Heo624d5c52008-03-25 22:16:41 +0900910
911 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
912 qc->dev->multi_count);
913 while (nsect--)
914 ata_pio_sector(qc);
915 } else
916 ata_pio_sector(qc);
917
Alan Coxa57c1ba2008-05-29 22:10:58 +0100918 ata_sff_sync(qc->ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900919}
920
921/**
922 * atapi_send_cdb - Write CDB bytes to hardware
923 * @ap: Port to which ATAPI device is attached.
924 * @qc: Taskfile currently active
925 *
926 * When device has indicated its readiness to accept
927 * a CDB, this function is called. Send the CDB.
928 *
929 * LOCKING:
930 * caller.
931 */
932static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
933{
934 /* send SCSI cdb */
935 DPRINTK("send cdb\n");
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900936 WARN_ON_ONCE(qc->dev->cdb_len < 12);
Tejun Heo624d5c52008-03-25 22:16:41 +0900937
Tejun Heo5682ed32008-04-07 22:47:16 +0900938 ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100939 ata_sff_sync(ap);
940 /* FIXME: If the CDB is for DMA do we need to do the transition delay
941 or is bmdma_start guaranteed to do it ? */
Tejun Heo624d5c52008-03-25 22:16:41 +0900942 switch (qc->tf.protocol) {
943 case ATAPI_PROT_PIO:
944 ap->hsm_task_state = HSM_ST;
945 break;
946 case ATAPI_PROT_NODATA:
947 ap->hsm_task_state = HSM_ST_LAST;
948 break;
949 case ATAPI_PROT_DMA:
950 ap->hsm_task_state = HSM_ST_LAST;
951 /* initiate bmdma */
952 ap->ops->bmdma_start(qc);
953 break;
954 }
955}
956
957/**
958 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
959 * @qc: Command on going
960 * @bytes: number of bytes
961 *
962 * Transfer Transfer data from/to the ATAPI device.
963 *
964 * LOCKING:
965 * Inherited from caller.
966 *
967 */
968static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
969{
970 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
971 struct ata_port *ap = qc->ap;
972 struct ata_device *dev = qc->dev;
973 struct ata_eh_info *ehi = &dev->link->eh_info;
974 struct scatterlist *sg;
975 struct page *page;
976 unsigned char *buf;
977 unsigned int offset, count, consumed;
978
979next_sg:
980 sg = qc->cursg;
981 if (unlikely(!sg)) {
982 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
983 "buf=%u cur=%u bytes=%u",
984 qc->nbytes, qc->curbytes, bytes);
985 return -1;
986 }
987
988 page = sg_page(sg);
989 offset = sg->offset + qc->cursg_ofs;
990
991 /* get the current page and offset */
992 page = nth_page(page, (offset >> PAGE_SHIFT));
993 offset %= PAGE_SIZE;
994
995 /* don't overrun current sg */
996 count = min(sg->length - qc->cursg_ofs, bytes);
997
998 /* don't cross page boundaries */
999 count = min(count, (unsigned int)PAGE_SIZE - offset);
1000
1001 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
1002
1003 if (PageHighMem(page)) {
1004 unsigned long flags;
1005
1006 /* FIXME: use bounce buffer */
1007 local_irq_save(flags);
1008 buf = kmap_atomic(page, KM_IRQ0);
1009
1010 /* do the actual data transfer */
Alan Cox0fe40ff2009-01-05 14:16:13 +00001011 consumed = ap->ops->sff_data_xfer(dev, buf + offset,
1012 count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +09001013
1014 kunmap_atomic(buf, KM_IRQ0);
1015 local_irq_restore(flags);
1016 } else {
1017 buf = page_address(page);
Alan Cox0fe40ff2009-01-05 14:16:13 +00001018 consumed = ap->ops->sff_data_xfer(dev, buf + offset,
1019 count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +09001020 }
1021
1022 bytes -= min(bytes, consumed);
1023 qc->curbytes += count;
1024 qc->cursg_ofs += count;
1025
1026 if (qc->cursg_ofs == sg->length) {
1027 qc->cursg = sg_next(qc->cursg);
1028 qc->cursg_ofs = 0;
1029 }
1030
Christian Borntraegera0f79f72009-01-13 10:38:36 +01001031 /*
1032 * There used to be a WARN_ON_ONCE(qc->cursg && count != consumed);
1033 * Unfortunately __atapi_pio_bytes doesn't know enough to do the WARN
1034 * check correctly as it doesn't know if it is the last request being
1035 * made. Somebody should implement a proper sanity check.
1036 */
Tejun Heo624d5c52008-03-25 22:16:41 +09001037 if (bytes)
1038 goto next_sg;
1039 return 0;
1040}
1041
1042/**
1043 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
1044 * @qc: Command on going
1045 *
1046 * Transfer Transfer data from/to the ATAPI device.
1047 *
1048 * LOCKING:
1049 * Inherited from caller.
1050 */
1051static void atapi_pio_bytes(struct ata_queued_cmd *qc)
1052{
1053 struct ata_port *ap = qc->ap;
1054 struct ata_device *dev = qc->dev;
1055 struct ata_eh_info *ehi = &dev->link->eh_info;
1056 unsigned int ireason, bc_lo, bc_hi, bytes;
1057 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
1058
1059 /* Abuse qc->result_tf for temp storage of intermediate TF
1060 * here to save some kernel stack usage.
1061 * For normal completion, qc->result_tf is not relevant. For
1062 * error, qc->result_tf is later overwritten by ata_qc_complete().
1063 * So, the correctness of qc->result_tf is not affected.
1064 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001065 ap->ops->sff_tf_read(ap, &qc->result_tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001066 ireason = qc->result_tf.nsect;
1067 bc_lo = qc->result_tf.lbam;
1068 bc_hi = qc->result_tf.lbah;
1069 bytes = (bc_hi << 8) | bc_lo;
1070
1071 /* shall be cleared to zero, indicating xfer of data */
1072 if (unlikely(ireason & (1 << 0)))
1073 goto atapi_check;
1074
1075 /* make sure transfer direction matches expected */
1076 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
1077 if (unlikely(do_write != i_write))
1078 goto atapi_check;
1079
1080 if (unlikely(!bytes))
1081 goto atapi_check;
1082
1083 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
1084
1085 if (unlikely(__atapi_pio_bytes(qc, bytes)))
1086 goto err_out;
Alan Coxa57c1ba2008-05-29 22:10:58 +01001087 ata_sff_sync(ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +09001088
1089 return;
1090
1091 atapi_check:
1092 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
1093 ireason, bytes);
1094 err_out:
1095 qc->err_mask |= AC_ERR_HSM;
1096 ap->hsm_task_state = HSM_ST_ERR;
1097}
1098
1099/**
1100 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
1101 * @ap: the target ata_port
1102 * @qc: qc on going
1103 *
1104 * RETURNS:
1105 * 1 if ok in workqueue, 0 otherwise.
1106 */
Alan Cox0fe40ff2009-01-05 14:16:13 +00001107static inline int ata_hsm_ok_in_wq(struct ata_port *ap,
1108 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001109{
1110 if (qc->tf.flags & ATA_TFLAG_POLLING)
1111 return 1;
1112
1113 if (ap->hsm_task_state == HSM_ST_FIRST) {
1114 if (qc->tf.protocol == ATA_PROT_PIO &&
Alan Cox0fe40ff2009-01-05 14:16:13 +00001115 (qc->tf.flags & ATA_TFLAG_WRITE))
Tejun Heo624d5c52008-03-25 22:16:41 +09001116 return 1;
1117
1118 if (ata_is_atapi(qc->tf.protocol) &&
Alan Cox0fe40ff2009-01-05 14:16:13 +00001119 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Tejun Heo624d5c52008-03-25 22:16:41 +09001120 return 1;
1121 }
1122
1123 return 0;
1124}
1125
1126/**
1127 * ata_hsm_qc_complete - finish a qc running on standard HSM
1128 * @qc: Command to complete
1129 * @in_wq: 1 if called from workqueue, 0 otherwise
1130 *
1131 * Finish @qc which is running on standard HSM.
1132 *
1133 * LOCKING:
1134 * If @in_wq is zero, spin_lock_irqsave(host lock).
1135 * Otherwise, none on entry and grabs host lock.
1136 */
1137static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
1138{
1139 struct ata_port *ap = qc->ap;
1140 unsigned long flags;
1141
1142 if (ap->ops->error_handler) {
1143 if (in_wq) {
1144 spin_lock_irqsave(ap->lock, flags);
1145
1146 /* EH might have kicked in while host lock is
1147 * released.
1148 */
1149 qc = ata_qc_from_tag(ap, qc->tag);
1150 if (qc) {
1151 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001152 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001153 ata_qc_complete(qc);
1154 } else
1155 ata_port_freeze(ap);
1156 }
1157
1158 spin_unlock_irqrestore(ap->lock, flags);
1159 } else {
1160 if (likely(!(qc->err_mask & AC_ERR_HSM)))
1161 ata_qc_complete(qc);
1162 else
1163 ata_port_freeze(ap);
1164 }
1165 } else {
1166 if (in_wq) {
1167 spin_lock_irqsave(ap->lock, flags);
Tejun Heo5682ed32008-04-07 22:47:16 +09001168 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001169 ata_qc_complete(qc);
1170 spin_unlock_irqrestore(ap->lock, flags);
1171 } else
1172 ata_qc_complete(qc);
1173 }
1174}
1175
1176/**
Tejun Heo9363c382008-04-07 22:47:16 +09001177 * ata_sff_hsm_move - move the HSM to the next state.
Tejun Heo624d5c52008-03-25 22:16:41 +09001178 * @ap: the target ata_port
1179 * @qc: qc on going
1180 * @status: current device status
1181 * @in_wq: 1 if called from workqueue, 0 otherwise
1182 *
1183 * RETURNS:
1184 * 1 when poll next status needed, 0 otherwise.
1185 */
Tejun Heo9363c382008-04-07 22:47:16 +09001186int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
1187 u8 status, int in_wq)
Tejun Heo624d5c52008-03-25 22:16:41 +09001188{
Tejun Heoa836d3e2008-06-28 01:39:43 +09001189 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo624d5c52008-03-25 22:16:41 +09001190 unsigned long flags = 0;
1191 int poll_next;
1192
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001193 WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001194
Tejun Heo9363c382008-04-07 22:47:16 +09001195 /* Make sure ata_sff_qc_issue() does not throw things
Tejun Heo624d5c52008-03-25 22:16:41 +09001196 * like DMA polling into the workqueue. Notice that
1197 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1198 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001199 WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc));
Tejun Heo624d5c52008-03-25 22:16:41 +09001200
1201fsm_start:
1202 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1203 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
1204
1205 switch (ap->hsm_task_state) {
1206 case HSM_ST_FIRST:
1207 /* Send first data block or PACKET CDB */
1208
1209 /* If polling, we will stay in the work queue after
1210 * sending the data. Otherwise, interrupt handler
1211 * takes over after sending the data.
1212 */
1213 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
1214
1215 /* check device status */
1216 if (unlikely((status & ATA_DRQ) == 0)) {
1217 /* handle BSY=0, DRQ=0 as error */
1218 if (likely(status & (ATA_ERR | ATA_DF)))
1219 /* device stops HSM for abort/error */
1220 qc->err_mask |= AC_ERR_DEV;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001221 else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001222 /* HSM violation. Let EH handle this */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001223 ata_ehi_push_desc(ehi,
1224 "ST_FIRST: !(DRQ|ERR|DF)");
Tejun Heo624d5c52008-03-25 22:16:41 +09001225 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001226 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001227
1228 ap->hsm_task_state = HSM_ST_ERR;
1229 goto fsm_start;
1230 }
1231
1232 /* Device should not ask for data transfer (DRQ=1)
1233 * when it finds something wrong.
1234 * We ignore DRQ here and stop the HSM by
1235 * changing hsm_task_state to HSM_ST_ERR and
1236 * let the EH abort the command or reset the device.
1237 */
1238 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1239 /* Some ATAPI tape drives forget to clear the ERR bit
1240 * when doing the next command (mostly request sense).
1241 * We ignore ERR here to workaround and proceed sending
1242 * the CDB.
1243 */
1244 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001245 ata_ehi_push_desc(ehi, "ST_FIRST: "
1246 "DRQ=1 with device error, "
1247 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001248 qc->err_mask |= AC_ERR_HSM;
1249 ap->hsm_task_state = HSM_ST_ERR;
1250 goto fsm_start;
1251 }
1252 }
1253
1254 /* Send the CDB (atapi) or the first data block (ata pio out).
1255 * During the state transition, interrupt handler shouldn't
1256 * be invoked before the data transfer is complete and
1257 * hsm_task_state is changed. Hence, the following locking.
1258 */
1259 if (in_wq)
1260 spin_lock_irqsave(ap->lock, flags);
1261
1262 if (qc->tf.protocol == ATA_PROT_PIO) {
1263 /* PIO data out protocol.
1264 * send first data block.
1265 */
1266
1267 /* ata_pio_sectors() might change the state
1268 * to HSM_ST_LAST. so, the state is changed here
1269 * before ata_pio_sectors().
1270 */
1271 ap->hsm_task_state = HSM_ST;
1272 ata_pio_sectors(qc);
1273 } else
1274 /* send CDB */
1275 atapi_send_cdb(ap, qc);
1276
1277 if (in_wq)
1278 spin_unlock_irqrestore(ap->lock, flags);
1279
1280 /* if polling, ata_pio_task() handles the rest.
1281 * otherwise, interrupt handler takes over from here.
1282 */
1283 break;
1284
1285 case HSM_ST:
1286 /* complete command or read/write the data register */
1287 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1288 /* ATAPI PIO protocol */
1289 if ((status & ATA_DRQ) == 0) {
1290 /* No more data to transfer or device error.
1291 * Device error will be tagged in HSM_ST_LAST.
1292 */
1293 ap->hsm_task_state = HSM_ST_LAST;
1294 goto fsm_start;
1295 }
1296
1297 /* Device should not ask for data transfer (DRQ=1)
1298 * when it finds something wrong.
1299 * We ignore DRQ here and stop the HSM by
1300 * changing hsm_task_state to HSM_ST_ERR and
1301 * let the EH abort the command or reset the device.
1302 */
1303 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001304 ata_ehi_push_desc(ehi, "ST-ATAPI: "
1305 "DRQ=1 with device error, "
1306 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001307 qc->err_mask |= AC_ERR_HSM;
1308 ap->hsm_task_state = HSM_ST_ERR;
1309 goto fsm_start;
1310 }
1311
1312 atapi_pio_bytes(qc);
1313
1314 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1315 /* bad ireason reported by device */
1316 goto fsm_start;
1317
1318 } else {
1319 /* ATA PIO protocol */
1320 if (unlikely((status & ATA_DRQ) == 0)) {
1321 /* handle BSY=0, DRQ=0 as error */
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001322 if (likely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo624d5c52008-03-25 22:16:41 +09001323 /* device stops HSM for abort/error */
1324 qc->err_mask |= AC_ERR_DEV;
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001325
1326 /* If diagnostic failed and this is
1327 * IDENTIFY, it's likely a phantom
1328 * device. Mark hint.
1329 */
1330 if (qc->dev->horkage &
1331 ATA_HORKAGE_DIAGNOSTIC)
1332 qc->err_mask |=
1333 AC_ERR_NODEV_HINT;
1334 } else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001335 /* HSM violation. Let EH handle this.
1336 * Phantom devices also trigger this
1337 * condition. Mark hint.
1338 */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001339 ata_ehi_push_desc(ehi, "ST-ATA: "
Tejun Heo80ee6f52009-01-23 14:12:59 +09001340 "DRQ=0 without device error, "
Tejun Heoa836d3e2008-06-28 01:39:43 +09001341 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001342 qc->err_mask |= AC_ERR_HSM |
1343 AC_ERR_NODEV_HINT;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001344 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001345
1346 ap->hsm_task_state = HSM_ST_ERR;
1347 goto fsm_start;
1348 }
1349
1350 /* For PIO reads, some devices may ask for
1351 * data transfer (DRQ=1) alone with ERR=1.
1352 * We respect DRQ here and transfer one
1353 * block of junk data before changing the
1354 * hsm_task_state to HSM_ST_ERR.
1355 *
1356 * For PIO writes, ERR=1 DRQ=1 doesn't make
1357 * sense since the data block has been
1358 * transferred to the device.
1359 */
1360 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1361 /* data might be corrputed */
1362 qc->err_mask |= AC_ERR_DEV;
1363
1364 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1365 ata_pio_sectors(qc);
1366 status = ata_wait_idle(ap);
1367 }
1368
Tejun Heoa836d3e2008-06-28 01:39:43 +09001369 if (status & (ATA_BUSY | ATA_DRQ)) {
1370 ata_ehi_push_desc(ehi, "ST-ATA: "
1371 "BUSY|DRQ persists on ERR|DF, "
1372 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001373 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001374 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001375
Tejun Heob9199302009-01-25 10:26:00 +09001376 /* There are oddball controllers with
1377 * status register stuck at 0x7f and
1378 * lbal/m/h at zero which makes it
1379 * pass all other presence detection
1380 * mechanisms we have. Set NODEV_HINT
1381 * for it. Kernel bz#7241.
1382 */
1383 if (status == 0x7f)
1384 qc->err_mask |= AC_ERR_NODEV_HINT;
1385
Tejun Heo624d5c52008-03-25 22:16:41 +09001386 /* ata_pio_sectors() might change the
1387 * state to HSM_ST_LAST. so, the state
1388 * is changed after ata_pio_sectors().
1389 */
1390 ap->hsm_task_state = HSM_ST_ERR;
1391 goto fsm_start;
1392 }
1393
1394 ata_pio_sectors(qc);
1395
1396 if (ap->hsm_task_state == HSM_ST_LAST &&
1397 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1398 /* all data read */
1399 status = ata_wait_idle(ap);
1400 goto fsm_start;
1401 }
1402 }
1403
1404 poll_next = 1;
1405 break;
1406
1407 case HSM_ST_LAST:
1408 if (unlikely(!ata_ok(status))) {
1409 qc->err_mask |= __ac_err_mask(status);
1410 ap->hsm_task_state = HSM_ST_ERR;
1411 goto fsm_start;
1412 }
1413
1414 /* no more data to transfer */
1415 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1416 ap->print_id, qc->dev->devno, status);
1417
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001418 WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
Tejun Heo624d5c52008-03-25 22:16:41 +09001419
1420 ap->hsm_task_state = HSM_ST_IDLE;
1421
1422 /* complete taskfile transaction */
1423 ata_hsm_qc_complete(qc, in_wq);
1424
1425 poll_next = 0;
1426 break;
1427
1428 case HSM_ST_ERR:
Tejun Heo624d5c52008-03-25 22:16:41 +09001429 ap->hsm_task_state = HSM_ST_IDLE;
1430
1431 /* complete taskfile transaction */
1432 ata_hsm_qc_complete(qc, in_wq);
1433
1434 poll_next = 0;
1435 break;
1436 default:
1437 poll_next = 0;
1438 BUG();
1439 }
1440
1441 return poll_next;
1442}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001443EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
Tejun Heo624d5c52008-03-25 22:16:41 +09001444
1445void ata_pio_task(struct work_struct *work)
1446{
1447 struct ata_port *ap =
1448 container_of(work, struct ata_port, port_task.work);
1449 struct ata_queued_cmd *qc = ap->port_task_data;
1450 u8 status;
1451 int poll_next;
1452
1453fsm_start:
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001454 WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE);
Tejun Heo624d5c52008-03-25 22:16:41 +09001455
1456 /*
1457 * This is purely heuristic. This is a fast path.
1458 * Sometimes when we enter, BSY will be cleared in
1459 * a chk-status or two. If not, the drive is probably seeking
1460 * or something. Snooze for a couple msecs, then
1461 * chk-status again. If still busy, queue delayed work.
1462 */
Tejun Heo9363c382008-04-07 22:47:16 +09001463 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
Tejun Heo624d5c52008-03-25 22:16:41 +09001464 if (status & ATA_BUSY) {
1465 msleep(2);
Tejun Heo9363c382008-04-07 22:47:16 +09001466 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
Tejun Heo624d5c52008-03-25 22:16:41 +09001467 if (status & ATA_BUSY) {
1468 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
1469 return;
1470 }
1471 }
1472
1473 /* move the HSM */
Tejun Heo9363c382008-04-07 22:47:16 +09001474 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001475
1476 /* another command or interrupt handler
1477 * may be running at this point.
1478 */
1479 if (poll_next)
1480 goto fsm_start;
1481}
1482
1483/**
Tejun Heo9363c382008-04-07 22:47:16 +09001484 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
Tejun Heo624d5c52008-03-25 22:16:41 +09001485 * @qc: command to issue to device
1486 *
1487 * Using various libata functions and hooks, this function
1488 * starts an ATA command. ATA commands are grouped into
1489 * classes called "protocols", and issuing each type of protocol
1490 * is slightly different.
1491 *
1492 * May be used as the qc_issue() entry in ata_port_operations.
1493 *
1494 * LOCKING:
1495 * spin_lock_irqsave(host lock)
1496 *
1497 * RETURNS:
1498 * Zero on success, AC_ERR_* mask on failure
1499 */
Tejun Heo9363c382008-04-07 22:47:16 +09001500unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001501{
1502 struct ata_port *ap = qc->ap;
1503
1504 /* Use polling pio if the LLD doesn't handle
1505 * interrupt driven pio and atapi CDB interrupt.
1506 */
1507 if (ap->flags & ATA_FLAG_PIO_POLLING) {
1508 switch (qc->tf.protocol) {
1509 case ATA_PROT_PIO:
1510 case ATA_PROT_NODATA:
1511 case ATAPI_PROT_PIO:
1512 case ATAPI_PROT_NODATA:
1513 qc->tf.flags |= ATA_TFLAG_POLLING;
1514 break;
1515 case ATAPI_PROT_DMA:
1516 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
1517 /* see ata_dma_blacklisted() */
1518 BUG();
1519 break;
1520 default:
1521 break;
1522 }
1523 }
1524
1525 /* select the device */
1526 ata_dev_select(ap, qc->dev->devno, 1, 0);
1527
1528 /* start the command */
1529 switch (qc->tf.protocol) {
1530 case ATA_PROT_NODATA:
1531 if (qc->tf.flags & ATA_TFLAG_POLLING)
1532 ata_qc_set_polling(qc);
1533
1534 ata_tf_to_host(ap, &qc->tf);
1535 ap->hsm_task_state = HSM_ST_LAST;
1536
1537 if (qc->tf.flags & ATA_TFLAG_POLLING)
1538 ata_pio_queue_task(ap, qc, 0);
1539
1540 break;
1541
1542 case ATA_PROT_DMA:
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001543 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
Tejun Heo624d5c52008-03-25 22:16:41 +09001544
Tejun Heo5682ed32008-04-07 22:47:16 +09001545 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001546 ap->ops->bmdma_setup(qc); /* set up bmdma */
1547 ap->ops->bmdma_start(qc); /* initiate bmdma */
1548 ap->hsm_task_state = HSM_ST_LAST;
1549 break;
1550
1551 case ATA_PROT_PIO:
1552 if (qc->tf.flags & ATA_TFLAG_POLLING)
1553 ata_qc_set_polling(qc);
1554
1555 ata_tf_to_host(ap, &qc->tf);
1556
1557 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1558 /* PIO data out protocol */
1559 ap->hsm_task_state = HSM_ST_FIRST;
1560 ata_pio_queue_task(ap, qc, 0);
1561
1562 /* always send first data block using
1563 * the ata_pio_task() codepath.
1564 */
1565 } else {
1566 /* PIO data in protocol */
1567 ap->hsm_task_state = HSM_ST;
1568
1569 if (qc->tf.flags & ATA_TFLAG_POLLING)
1570 ata_pio_queue_task(ap, qc, 0);
1571
1572 /* if polling, ata_pio_task() handles the rest.
1573 * otherwise, interrupt handler takes over from here.
1574 */
1575 }
1576
1577 break;
1578
1579 case ATAPI_PROT_PIO:
1580 case ATAPI_PROT_NODATA:
1581 if (qc->tf.flags & ATA_TFLAG_POLLING)
1582 ata_qc_set_polling(qc);
1583
1584 ata_tf_to_host(ap, &qc->tf);
1585
1586 ap->hsm_task_state = HSM_ST_FIRST;
1587
1588 /* send cdb by polling if no cdb interrupt */
1589 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1590 (qc->tf.flags & ATA_TFLAG_POLLING))
1591 ata_pio_queue_task(ap, qc, 0);
1592 break;
1593
1594 case ATAPI_PROT_DMA:
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001595 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
Tejun Heo624d5c52008-03-25 22:16:41 +09001596
Tejun Heo5682ed32008-04-07 22:47:16 +09001597 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001598 ap->ops->bmdma_setup(qc); /* set up bmdma */
1599 ap->hsm_task_state = HSM_ST_FIRST;
1600
1601 /* send cdb by polling if no cdb interrupt */
1602 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1603 ata_pio_queue_task(ap, qc, 0);
1604 break;
1605
1606 default:
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001607 WARN_ON_ONCE(1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001608 return AC_ERR_SYSTEM;
1609 }
1610
1611 return 0;
1612}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001613EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
Tejun Heo624d5c52008-03-25 22:16:41 +09001614
1615/**
Tejun Heo22183bf2008-04-07 22:47:20 +09001616 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1617 * @qc: qc to fill result TF for
1618 *
1619 * @qc is finished and result TF needs to be filled. Fill it
1620 * using ->sff_tf_read.
1621 *
1622 * LOCKING:
1623 * spin_lock_irqsave(host lock)
1624 *
1625 * RETURNS:
1626 * true indicating that result TF is successfully filled.
1627 */
1628bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
1629{
1630 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
1631 return true;
1632}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001633EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
Tejun Heo22183bf2008-04-07 22:47:20 +09001634
1635/**
Tejun Heo9363c382008-04-07 22:47:16 +09001636 * ata_sff_host_intr - Handle host interrupt for given (port, task)
Tejun Heo624d5c52008-03-25 22:16:41 +09001637 * @ap: Port on which interrupt arrived (possibly...)
1638 * @qc: Taskfile currently active in engine
1639 *
1640 * Handle host interrupt for given queued command. Currently,
1641 * only DMA interrupts are handled. All other commands are
1642 * handled via polling with interrupts disabled (nIEN bit).
1643 *
1644 * LOCKING:
1645 * spin_lock_irqsave(host lock)
1646 *
1647 * RETURNS:
1648 * One if interrupt was handled, zero if not (shared irq).
1649 */
Tejun Heo9363c382008-04-07 22:47:16 +09001650inline unsigned int ata_sff_host_intr(struct ata_port *ap,
1651 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001652{
1653 struct ata_eh_info *ehi = &ap->link.eh_info;
1654 u8 status, host_stat = 0;
1655
1656 VPRINTK("ata%u: protocol %d task_state %d\n",
1657 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
1658
1659 /* Check whether we are expecting interrupt in this state */
1660 switch (ap->hsm_task_state) {
1661 case HSM_ST_FIRST:
1662 /* Some pre-ATAPI-4 devices assert INTRQ
1663 * at this state when ready to receive CDB.
1664 */
1665
1666 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1667 * The flag was turned on only for atapi devices. No
1668 * need to check ata_is_atapi(qc->tf.protocol) again.
1669 */
1670 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1671 goto idle_irq;
1672 break;
1673 case HSM_ST_LAST:
1674 if (qc->tf.protocol == ATA_PROT_DMA ||
1675 qc->tf.protocol == ATAPI_PROT_DMA) {
1676 /* check status of DMA engine */
1677 host_stat = ap->ops->bmdma_status(ap);
1678 VPRINTK("ata%u: host_stat 0x%X\n",
1679 ap->print_id, host_stat);
1680
1681 /* if it's not our irq... */
1682 if (!(host_stat & ATA_DMA_INTR))
1683 goto idle_irq;
1684
1685 /* before we do anything else, clear DMA-Start bit */
1686 ap->ops->bmdma_stop(qc);
1687
1688 if (unlikely(host_stat & ATA_DMA_ERR)) {
1689 /* error when transfering data to/from memory */
1690 qc->err_mask |= AC_ERR_HOST_BUS;
1691 ap->hsm_task_state = HSM_ST_ERR;
1692 }
1693 }
1694 break;
1695 case HSM_ST:
1696 break;
1697 default:
1698 goto idle_irq;
1699 }
1700
Tejun Heo624d5c52008-03-25 22:16:41 +09001701
Alan Coxa57c1ba2008-05-29 22:10:58 +01001702 /* check main status, clearing INTRQ if needed */
1703 status = ata_sff_irq_status(ap);
1704 if (status & ATA_BUSY)
Tejun Heo624d5c52008-03-25 22:16:41 +09001705 goto idle_irq;
1706
1707 /* ack bmdma irq events */
Tejun Heo5682ed32008-04-07 22:47:16 +09001708 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001709
Tejun Heo9363c382008-04-07 22:47:16 +09001710 ata_sff_hsm_move(ap, qc, status, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001711
1712 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
1713 qc->tf.protocol == ATAPI_PROT_DMA))
1714 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
1715
1716 return 1; /* irq handled */
1717
1718idle_irq:
1719 ap->stats.idle_irq++;
1720
1721#ifdef ATA_IRQ_TRAP
1722 if ((ap->stats.idle_irq % 1000) == 0) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001723 ap->ops->sff_check_status(ap);
1724 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001725 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
1726 return 1;
1727 }
1728#endif
1729 return 0; /* irq not handled */
1730}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001731EXPORT_SYMBOL_GPL(ata_sff_host_intr);
Tejun Heo624d5c52008-03-25 22:16:41 +09001732
1733/**
Tejun Heo9363c382008-04-07 22:47:16 +09001734 * ata_sff_interrupt - Default ATA host interrupt handler
Tejun Heo624d5c52008-03-25 22:16:41 +09001735 * @irq: irq line (unused)
1736 * @dev_instance: pointer to our ata_host information structure
1737 *
1738 * Default interrupt handler for PCI IDE devices. Calls
Tejun Heo9363c382008-04-07 22:47:16 +09001739 * ata_sff_host_intr() for each port that is not disabled.
Tejun Heo624d5c52008-03-25 22:16:41 +09001740 *
1741 * LOCKING:
1742 * Obtains host lock during operation.
1743 *
1744 * RETURNS:
1745 * IRQ_NONE or IRQ_HANDLED.
1746 */
Tejun Heo9363c382008-04-07 22:47:16 +09001747irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
Tejun Heo624d5c52008-03-25 22:16:41 +09001748{
1749 struct ata_host *host = dev_instance;
1750 unsigned int i;
1751 unsigned int handled = 0;
1752 unsigned long flags;
1753
1754 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1755 spin_lock_irqsave(&host->lock, flags);
1756
1757 for (i = 0; i < host->n_ports; i++) {
1758 struct ata_port *ap;
1759
1760 ap = host->ports[i];
1761 if (ap &&
1762 !(ap->flags & ATA_FLAG_DISABLED)) {
1763 struct ata_queued_cmd *qc;
1764
1765 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1766 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
1767 (qc->flags & ATA_QCFLAG_ACTIVE))
Tejun Heo9363c382008-04-07 22:47:16 +09001768 handled |= ata_sff_host_intr(ap, qc);
Tejun Heo624d5c52008-03-25 22:16:41 +09001769 }
1770 }
1771
1772 spin_unlock_irqrestore(&host->lock, flags);
1773
1774 return IRQ_RETVAL(handled);
1775}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001776EXPORT_SYMBOL_GPL(ata_sff_interrupt);
Tejun Heo624d5c52008-03-25 22:16:41 +09001777
1778/**
Tejun Heo9363c382008-04-07 22:47:16 +09001779 * ata_sff_freeze - Freeze SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001780 * @ap: port to freeze
1781 *
1782 * Freeze BMDMA controller port.
1783 *
1784 * LOCKING:
1785 * Inherited from caller.
1786 */
Tejun Heo9363c382008-04-07 22:47:16 +09001787void ata_sff_freeze(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001788{
1789 struct ata_ioports *ioaddr = &ap->ioaddr;
1790
1791 ap->ctl |= ATA_NIEN;
1792 ap->last_ctl = ap->ctl;
1793
Tejun Heof659f0e42008-03-06 13:12:54 +09001794 if (ioaddr->ctl_addr)
1795 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001796
1797 /* Under certain circumstances, some controllers raise IRQ on
1798 * ATA_NIEN manipulation. Also, many controllers fail to mask
1799 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1800 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001801 ap->ops->sff_check_status(ap);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001802
Tejun Heo5682ed32008-04-07 22:47:16 +09001803 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001804}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001805EXPORT_SYMBOL_GPL(ata_sff_freeze);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001806
1807/**
Tejun Heo9363c382008-04-07 22:47:16 +09001808 * ata_sff_thaw - Thaw SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001809 * @ap: port to thaw
1810 *
Tejun Heo9363c382008-04-07 22:47:16 +09001811 * Thaw SFF controller port.
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001812 *
1813 * LOCKING:
1814 * Inherited from caller.
1815 */
Tejun Heo9363c382008-04-07 22:47:16 +09001816void ata_sff_thaw(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001817{
1818 /* clear & re-enable interrupts */
Tejun Heo5682ed32008-04-07 22:47:16 +09001819 ap->ops->sff_check_status(ap);
1820 ap->ops->sff_irq_clear(ap);
1821 ap->ops->sff_irq_on(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001822}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001823EXPORT_SYMBOL_GPL(ata_sff_thaw);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001824
1825/**
Tejun Heo0aa11132008-04-07 22:47:18 +09001826 * ata_sff_prereset - prepare SFF link for reset
1827 * @link: SFF link to be reset
1828 * @deadline: deadline jiffies for the operation
1829 *
1830 * SFF link @link is about to be reset. Initialize it. It first
1831 * calls ata_std_prereset() and wait for !BSY if the port is
1832 * being softreset.
1833 *
1834 * LOCKING:
1835 * Kernel thread context (may sleep)
1836 *
1837 * RETURNS:
1838 * 0 on success, -errno otherwise.
1839 */
1840int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
1841{
Tejun Heo0aa11132008-04-07 22:47:18 +09001842 struct ata_eh_context *ehc = &link->eh_context;
1843 int rc;
1844
1845 rc = ata_std_prereset(link, deadline);
1846 if (rc)
1847 return rc;
1848
1849 /* if we're about to do hardreset, nothing more to do */
1850 if (ehc->i.action & ATA_EH_HARDRESET)
1851 return 0;
1852
1853 /* wait for !BSY if we don't know that no device is attached */
1854 if (!ata_link_offline(link)) {
Tejun Heo705e76b2008-04-07 22:47:19 +09001855 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo0aa11132008-04-07 22:47:18 +09001856 if (rc && rc != -ENODEV) {
1857 ata_link_printk(link, KERN_WARNING, "device not ready "
1858 "(errno=%d), forcing hardreset\n", rc);
1859 ehc->i.action |= ATA_EH_HARDRESET;
1860 }
1861 }
1862
1863 return 0;
1864}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001865EXPORT_SYMBOL_GPL(ata_sff_prereset);
Tejun Heo0aa11132008-04-07 22:47:18 +09001866
1867/**
Tejun Heo624d5c52008-03-25 22:16:41 +09001868 * ata_devchk - PATA device presence detection
1869 * @ap: ATA channel to examine
1870 * @device: Device to examine (starting at zero)
1871 *
1872 * This technique was originally described in
1873 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1874 * later found its way into the ATA/ATAPI spec.
1875 *
1876 * Write a pattern to the ATA shadow registers,
1877 * and if a device is present, it will respond by
1878 * correctly storing and echoing back the
1879 * ATA shadow register contents.
1880 *
1881 * LOCKING:
1882 * caller.
1883 */
1884static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1885{
1886 struct ata_ioports *ioaddr = &ap->ioaddr;
1887 u8 nsect, lbal;
1888
Tejun Heo5682ed32008-04-07 22:47:16 +09001889 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +09001890
1891 iowrite8(0x55, ioaddr->nsect_addr);
1892 iowrite8(0xaa, ioaddr->lbal_addr);
1893
1894 iowrite8(0xaa, ioaddr->nsect_addr);
1895 iowrite8(0x55, ioaddr->lbal_addr);
1896
1897 iowrite8(0x55, ioaddr->nsect_addr);
1898 iowrite8(0xaa, ioaddr->lbal_addr);
1899
1900 nsect = ioread8(ioaddr->nsect_addr);
1901 lbal = ioread8(ioaddr->lbal_addr);
1902
1903 if ((nsect == 0x55) && (lbal == 0xaa))
1904 return 1; /* we found a device */
1905
1906 return 0; /* nothing found */
1907}
1908
1909/**
Tejun Heo9363c382008-04-07 22:47:16 +09001910 * ata_sff_dev_classify - Parse returned ATA device signature
Tejun Heo624d5c52008-03-25 22:16:41 +09001911 * @dev: ATA device to classify (starting at zero)
1912 * @present: device seems present
1913 * @r_err: Value of error register on completion
1914 *
1915 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1916 * an ATA/ATAPI-defined set of values is placed in the ATA
1917 * shadow registers, indicating the results of device detection
1918 * and diagnostics.
1919 *
1920 * Select the ATA device, and read the values from the ATA shadow
1921 * registers. Then parse according to the Error register value,
1922 * and the spec-defined values examined by ata_dev_classify().
1923 *
1924 * LOCKING:
1925 * caller.
1926 *
1927 * RETURNS:
1928 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1929 */
Tejun Heo9363c382008-04-07 22:47:16 +09001930unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
Tejun Heo624d5c52008-03-25 22:16:41 +09001931 u8 *r_err)
1932{
1933 struct ata_port *ap = dev->link->ap;
1934 struct ata_taskfile tf;
1935 unsigned int class;
1936 u8 err;
1937
Tejun Heo5682ed32008-04-07 22:47:16 +09001938 ap->ops->sff_dev_select(ap, dev->devno);
Tejun Heo624d5c52008-03-25 22:16:41 +09001939
1940 memset(&tf, 0, sizeof(tf));
1941
Tejun Heo5682ed32008-04-07 22:47:16 +09001942 ap->ops->sff_tf_read(ap, &tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001943 err = tf.feature;
1944 if (r_err)
1945 *r_err = err;
1946
1947 /* see if device passed diags: continue and warn later */
1948 if (err == 0)
1949 /* diagnostic fail : do nothing _YET_ */
1950 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1951 else if (err == 1)
1952 /* do nothing */ ;
1953 else if ((dev->devno == 0) && (err == 0x81))
1954 /* do nothing */ ;
1955 else
1956 return ATA_DEV_NONE;
1957
1958 /* determine if device is ATA or ATAPI */
1959 class = ata_dev_classify(&tf);
1960
1961 if (class == ATA_DEV_UNKNOWN) {
1962 /* If the device failed diagnostic, it's likely to
1963 * have reported incorrect device signature too.
1964 * Assume ATA device if the device seems present but
1965 * device signature is invalid with diagnostic
1966 * failure.
1967 */
1968 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1969 class = ATA_DEV_ATA;
1970 else
1971 class = ATA_DEV_NONE;
Tejun Heo5682ed32008-04-07 22:47:16 +09001972 } else if ((class == ATA_DEV_ATA) &&
1973 (ap->ops->sff_check_status(ap) == 0))
Tejun Heo624d5c52008-03-25 22:16:41 +09001974 class = ATA_DEV_NONE;
1975
1976 return class;
1977}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001978EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
Tejun Heo624d5c52008-03-25 22:16:41 +09001979
Tejun Heo705e76b2008-04-07 22:47:19 +09001980/**
1981 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1982 * @link: SFF link which is just reset
1983 * @devmask: mask of present devices
1984 * @deadline: deadline jiffies for the operation
1985 *
1986 * Wait devices attached to SFF @link to become ready after
1987 * reset. It contains preceding 150ms wait to avoid accessing TF
1988 * status register too early.
1989 *
1990 * LOCKING:
1991 * Kernel thread context (may sleep).
1992 *
1993 * RETURNS:
1994 * 0 on success, -ENODEV if some or all of devices in @devmask
1995 * don't seem to exist. -errno on other errors.
1996 */
1997int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
1998 unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +09001999{
Tejun Heo705e76b2008-04-07 22:47:19 +09002000 struct ata_port *ap = link->ap;
Tejun Heo624d5c52008-03-25 22:16:41 +09002001 struct ata_ioports *ioaddr = &ap->ioaddr;
2002 unsigned int dev0 = devmask & (1 << 0);
2003 unsigned int dev1 = devmask & (1 << 1);
2004 int rc, ret = 0;
2005
Tejun Heo341c2c92008-05-20 02:17:51 +09002006 msleep(ATA_WAIT_AFTER_RESET);
Tejun Heo705e76b2008-04-07 22:47:19 +09002007
2008 /* always check readiness of the master device */
2009 rc = ata_sff_wait_ready(link, deadline);
2010 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
2011 * and TF status is 0xff, bail out on it too.
Tejun Heo624d5c52008-03-25 22:16:41 +09002012 */
Tejun Heo705e76b2008-04-07 22:47:19 +09002013 if (rc)
2014 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09002015
2016 /* if device 1 was found in ata_devchk, wait for register
2017 * access briefly, then wait for BSY to clear.
2018 */
2019 if (dev1) {
2020 int i;
2021
Tejun Heo5682ed32008-04-07 22:47:16 +09002022 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002023
2024 /* Wait for register access. Some ATAPI devices fail
2025 * to set nsect/lbal after reset, so don't waste too
2026 * much time on it. We're gonna wait for !BSY anyway.
2027 */
2028 for (i = 0; i < 2; i++) {
2029 u8 nsect, lbal;
2030
2031 nsect = ioread8(ioaddr->nsect_addr);
2032 lbal = ioread8(ioaddr->lbal_addr);
2033 if ((nsect == 1) && (lbal == 1))
2034 break;
2035 msleep(50); /* give drive a breather */
2036 }
2037
Tejun Heo705e76b2008-04-07 22:47:19 +09002038 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09002039 if (rc) {
2040 if (rc != -ENODEV)
2041 return rc;
2042 ret = rc;
2043 }
2044 }
2045
2046 /* is all this really necessary? */
Tejun Heo5682ed32008-04-07 22:47:16 +09002047 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002048 if (dev1)
Tejun Heo5682ed32008-04-07 22:47:16 +09002049 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002050 if (dev0)
Tejun Heo5682ed32008-04-07 22:47:16 +09002051 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002052
2053 return ret;
2054}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002055EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002056
Tejun Heo624d5c52008-03-25 22:16:41 +09002057static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
2058 unsigned long deadline)
2059{
2060 struct ata_ioports *ioaddr = &ap->ioaddr;
2061
2062 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
2063
2064 /* software reset. causes dev0 to be selected */
2065 iowrite8(ap->ctl, ioaddr->ctl_addr);
2066 udelay(20); /* FIXME: flush */
2067 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2068 udelay(20); /* FIXME: flush */
2069 iowrite8(ap->ctl, ioaddr->ctl_addr);
Stuart MENEFYe3e43852009-03-10 11:38:13 +00002070 ap->last_ctl = ap->ctl;
Tejun Heo624d5c52008-03-25 22:16:41 +09002071
Tejun Heo705e76b2008-04-07 22:47:19 +09002072 /* wait the port to become ready */
2073 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09002074}
2075
2076/**
Tejun Heo9363c382008-04-07 22:47:16 +09002077 * ata_sff_softreset - reset host port via ATA SRST
Tejun Heo624d5c52008-03-25 22:16:41 +09002078 * @link: ATA link to reset
2079 * @classes: resulting classes of attached devices
2080 * @deadline: deadline jiffies for the operation
2081 *
2082 * Reset host port using ATA SRST.
2083 *
2084 * LOCKING:
2085 * Kernel thread context (may sleep)
2086 *
2087 * RETURNS:
2088 * 0 on success, -errno otherwise.
2089 */
Tejun Heo9363c382008-04-07 22:47:16 +09002090int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heo624d5c52008-03-25 22:16:41 +09002091 unsigned long deadline)
2092{
2093 struct ata_port *ap = link->ap;
2094 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2095 unsigned int devmask = 0;
2096 int rc;
2097 u8 err;
2098
2099 DPRINTK("ENTER\n");
2100
Tejun Heo624d5c52008-03-25 22:16:41 +09002101 /* determine if device 0/1 are present */
2102 if (ata_devchk(ap, 0))
2103 devmask |= (1 << 0);
2104 if (slave_possible && ata_devchk(ap, 1))
2105 devmask |= (1 << 1);
2106
2107 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002108 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002109
2110 /* issue bus reset */
2111 DPRINTK("about to softreset, devmask=%x\n", devmask);
2112 rc = ata_bus_softreset(ap, devmask, deadline);
2113 /* if link is occupied, -ENODEV too is an error */
2114 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
2115 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
2116 return rc;
2117 }
2118
2119 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo9363c382008-04-07 22:47:16 +09002120 classes[0] = ata_sff_dev_classify(&link->device[0],
Tejun Heo624d5c52008-03-25 22:16:41 +09002121 devmask & (1 << 0), &err);
2122 if (slave_possible && err != 0x81)
Tejun Heo9363c382008-04-07 22:47:16 +09002123 classes[1] = ata_sff_dev_classify(&link->device[1],
Tejun Heo624d5c52008-03-25 22:16:41 +09002124 devmask & (1 << 1), &err);
2125
Tejun Heo624d5c52008-03-25 22:16:41 +09002126 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2127 return 0;
2128}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002129EXPORT_SYMBOL_GPL(ata_sff_softreset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002130
2131/**
Tejun Heo9363c382008-04-07 22:47:16 +09002132 * sata_sff_hardreset - reset host port via SATA phy reset
Tejun Heo624d5c52008-03-25 22:16:41 +09002133 * @link: link to reset
2134 * @class: resulting class of attached device
2135 * @deadline: deadline jiffies for the operation
2136 *
2137 * SATA phy-reset host port using DET bits of SControl register,
2138 * wait for !BSY and classify the attached device.
2139 *
2140 * LOCKING:
2141 * Kernel thread context (may sleep)
2142 *
2143 * RETURNS:
2144 * 0 on success, -errno otherwise.
2145 */
Tejun Heo9363c382008-04-07 22:47:16 +09002146int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heo624d5c52008-03-25 22:16:41 +09002147 unsigned long deadline)
2148{
Tejun Heo9dadd452008-04-07 22:47:19 +09002149 struct ata_eh_context *ehc = &link->eh_context;
2150 const unsigned long *timing = sata_ehc_deb_timing(ehc);
2151 bool online;
Tejun Heo624d5c52008-03-25 22:16:41 +09002152 int rc;
2153
Tejun Heo9dadd452008-04-07 22:47:19 +09002154 rc = sata_link_hardreset(link, timing, deadline, &online,
2155 ata_sff_check_ready);
Tejun Heo9dadd452008-04-07 22:47:19 +09002156 if (online)
2157 *class = ata_sff_dev_classify(link->device, 1, NULL);
Tejun Heo624d5c52008-03-25 22:16:41 +09002158
2159 DPRINTK("EXIT, class=%u\n", *class);
Tejun Heo9dadd452008-04-07 22:47:19 +09002160 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09002161}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002162EXPORT_SYMBOL_GPL(sata_sff_hardreset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002163
2164/**
Tejun Heo203c75b2008-04-07 22:47:18 +09002165 * ata_sff_postreset - SFF postreset callback
2166 * @link: the target SFF ata_link
2167 * @classes: classes of attached devices
2168 *
2169 * This function is invoked after a successful reset. It first
2170 * calls ata_std_postreset() and performs SFF specific postreset
2171 * processing.
2172 *
2173 * LOCKING:
2174 * Kernel thread context (may sleep)
2175 */
2176void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
2177{
2178 struct ata_port *ap = link->ap;
2179
2180 ata_std_postreset(link, classes);
2181
2182 /* is double-select really necessary? */
2183 if (classes[0] != ATA_DEV_NONE)
2184 ap->ops->sff_dev_select(ap, 1);
2185 if (classes[1] != ATA_DEV_NONE)
2186 ap->ops->sff_dev_select(ap, 0);
2187
2188 /* bail out if no device is present */
2189 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2190 DPRINTK("EXIT, no device\n");
2191 return;
2192 }
2193
2194 /* set up device control */
Stuart MENEFYe3e43852009-03-10 11:38:13 +00002195 if (ap->ioaddr.ctl_addr) {
Tejun Heo203c75b2008-04-07 22:47:18 +09002196 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Stuart MENEFYe3e43852009-03-10 11:38:13 +00002197 ap->last_ctl = ap->ctl;
2198 }
Tejun Heo203c75b2008-04-07 22:47:18 +09002199}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002200EXPORT_SYMBOL_GPL(ata_sff_postreset);
Tejun Heo203c75b2008-04-07 22:47:18 +09002201
2202/**
Alan Cox3d47aa82009-03-24 10:23:19 +00002203 * ata_sff_drain_fifo - Stock FIFO drain logic for SFF controllers
2204 * @qc: command
2205 *
2206 * Drain the FIFO and device of any stuck data following a command
2207 * failing to complete. In some cases this is neccessary before a
2208 * reset will recover the device.
2209 *
2210 */
2211
2212void ata_sff_drain_fifo(struct ata_queued_cmd *qc)
2213{
2214 int count;
2215 struct ata_port *ap;
2216
2217 /* We only need to flush incoming data when a command was running */
2218 if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
2219 return;
2220
2221 ap = qc->ap;
2222 /* Drain up to 64K of data before we give up this recovery method */
2223 for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
2224 && count < 32768; count++)
2225 ioread16(ap->ioaddr.data_addr);
2226
2227 /* Can become DEBUG later */
2228 if (count)
2229 ata_port_printk(ap, KERN_DEBUG,
2230 "drained %d bytes to clear DRQ.\n", count);
2231
2232}
2233EXPORT_SYMBOL_GPL(ata_sff_drain_fifo);
2234
2235/**
Tejun Heo9363c382008-04-07 22:47:16 +09002236 * ata_sff_error_handler - Stock error handler for BMDMA controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002237 * @ap: port to handle error for
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002238 *
Tejun Heo9363c382008-04-07 22:47:16 +09002239 * Stock error handler for SFF controller. It can handle both
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002240 * PATA and SATA controllers. Many controllers should be able to
2241 * use this EH as-is or with some added handling before and
2242 * after.
2243 *
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002244 * LOCKING:
2245 * Kernel thread context (may sleep)
2246 */
Tejun Heo9363c382008-04-07 22:47:16 +09002247void ata_sff_error_handler(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002248{
Tejun Heoa1efdab2008-03-25 12:22:50 +09002249 ata_reset_fn_t softreset = ap->ops->softreset;
2250 ata_reset_fn_t hardreset = ap->ops->hardreset;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002251 struct ata_queued_cmd *qc;
2252 unsigned long flags;
2253 int thaw = 0;
2254
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002255 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002256 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2257 qc = NULL;
2258
2259 /* reset PIO HSM and stop DMA engine */
Jeff Garzikba6a1302006-06-22 23:46:10 -04002260 spin_lock_irqsave(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002261
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002262 ap->hsm_task_state = HSM_ST_IDLE;
2263
Tejun Heoed82f962008-03-25 21:34:39 +09002264 if (ap->ioaddr.bmdma_addr &&
2265 qc && (qc->tf.protocol == ATA_PROT_DMA ||
Tejun Heo0dc36882007-12-18 16:34:43 -05002266 qc->tf.protocol == ATAPI_PROT_DMA)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002267 u8 host_stat;
2268
Robert Hancockfbbb2622006-10-27 19:08:41 -07002269 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002270
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002271 /* BMDMA controllers indicate host bus error by
2272 * setting DMA_ERR bit and timing out. As it wasn't
2273 * really a timeout event, adjust error mask and
2274 * cancel frozen state.
2275 */
Alan Cox3d47aa82009-03-24 10:23:19 +00002276 if (qc->err_mask == AC_ERR_TIMEOUT
2277 && (host_stat & ATA_DMA_ERR)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002278 qc->err_mask = AC_ERR_HOST_BUS;
2279 thaw = 1;
2280 }
2281
2282 ap->ops->bmdma_stop(qc);
2283 }
2284
Alan Coxa57c1ba2008-05-29 22:10:58 +01002285 ata_sff_sync(ap); /* FIXME: We don't need this */
Tejun Heo5682ed32008-04-07 22:47:16 +09002286 ap->ops->sff_check_status(ap);
2287 ap->ops->sff_irq_clear(ap);
Alan Cox3d47aa82009-03-24 10:23:19 +00002288 /* We *MUST* do FIFO draining before we issue a reset as several
2289 * devices helpfully clear their internal state and will lock solid
2290 * if we touch the data port post reset. Pass qc in case anyone wants
2291 * to do different PIO/DMA recovery or has per command fixups
2292 */
2293 if (ap->ops->drain_fifo)
2294 ap->ops->drain_fifo(qc);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002295
Jeff Garzikba6a1302006-06-22 23:46:10 -04002296 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002297
2298 if (thaw)
2299 ata_eh_thaw_port(ap);
2300
2301 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002302
Tejun Heo57c9efd2008-04-07 22:47:19 +09002303 /* Ignore ata_sff_softreset if ctl isn't accessible and
2304 * built-in hardresets if SCR access isn't available.
Tejun Heoa1efdab2008-03-25 12:22:50 +09002305 */
Tejun Heo9363c382008-04-07 22:47:16 +09002306 if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
Tejun Heoa1efdab2008-03-25 12:22:50 +09002307 softreset = NULL;
Tejun Heo57c9efd2008-04-07 22:47:19 +09002308 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09002309 hardreset = NULL;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002310
Tejun Heoa1efdab2008-03-25 12:22:50 +09002311 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2312 ap->ops->postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002313}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002314EXPORT_SYMBOL_GPL(ata_sff_error_handler);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002315
2316/**
Tejun Heo9363c382008-04-07 22:47:16 +09002317 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002318 * @qc: internal command to clean up
2319 *
2320 * LOCKING:
2321 * Kernel thread context (may sleep)
2322 */
Tejun Heo9363c382008-04-07 22:47:16 +09002323void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002324{
Tejun Heo570106d2008-10-20 13:10:21 +09002325 struct ata_port *ap = qc->ap;
2326 unsigned long flags;
2327
2328 spin_lock_irqsave(ap->lock, flags);
2329
2330 ap->hsm_task_state = HSM_ST_IDLE;
2331
2332 if (ap->ioaddr.bmdma_addr)
Alan61dd08c2007-01-25 15:09:05 +00002333 ata_bmdma_stop(qc);
Tejun Heo570106d2008-10-20 13:10:21 +09002334
2335 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002336}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002337EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002338
Alan Coxd92e74d2007-06-07 16:19:15 +01002339/**
2340 * ata_sff_port_start - Set port up for dma.
2341 * @ap: Port to initialize
2342 *
2343 * Called just after data structures for each port are
2344 * initialized. Allocates space for PRD table if the device
2345 * is DMA capable SFF.
2346 *
2347 * May be used as the port_start() entry in ata_port_operations.
2348 *
2349 * LOCKING:
2350 * Inherited from caller.
2351 */
Alan Coxd92e74d2007-06-07 16:19:15 +01002352int ata_sff_port_start(struct ata_port *ap)
2353{
2354 if (ap->ioaddr.bmdma_addr)
2355 return ata_port_start(ap);
2356 return 0;
2357}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002358EXPORT_SYMBOL_GPL(ata_sff_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01002359
Tejun Heo272f7882008-03-25 22:16:40 +09002360/**
Tejun Heo9363c382008-04-07 22:47:16 +09002361 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
Tejun Heo624d5c52008-03-25 22:16:41 +09002362 * @ioaddr: IO address structure to be initialized
2363 *
2364 * Utility function which initializes data_addr, error_addr,
2365 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2366 * device_addr, status_addr, and command_addr to standard offsets
2367 * relative to cmd_addr.
2368 *
2369 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2370 */
Tejun Heo9363c382008-04-07 22:47:16 +09002371void ata_sff_std_ports(struct ata_ioports *ioaddr)
Tejun Heo624d5c52008-03-25 22:16:41 +09002372{
2373 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2374 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2375 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2376 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2377 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2378 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2379 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2380 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2381 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2382 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2383}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002384EXPORT_SYMBOL_GPL(ata_sff_std_ports);
Tejun Heo624d5c52008-03-25 22:16:41 +09002385
Tejun Heo9363c382008-04-07 22:47:16 +09002386unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
2387 unsigned long xfer_mask)
Tejun Heo071ce342008-03-25 22:16:42 +09002388{
2389 /* Filter out DMA modes if the device has been configured by
2390 the BIOS as PIO only */
2391
2392 if (adev->link->ap->ioaddr.bmdma_addr == NULL)
2393 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2394 return xfer_mask;
2395}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002396EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
Tejun Heo071ce342008-03-25 22:16:42 +09002397
Tejun Heo624d5c52008-03-25 22:16:41 +09002398/**
Tejun Heo272f7882008-03-25 22:16:40 +09002399 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2400 * @qc: Info associated with this ATA transaction.
2401 *
2402 * LOCKING:
2403 * spin_lock_irqsave(host lock)
2404 */
2405void ata_bmdma_setup(struct ata_queued_cmd *qc)
2406{
2407 struct ata_port *ap = qc->ap;
2408 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2409 u8 dmactl;
2410
2411 /* load PRD table addr. */
2412 mb(); /* make sure PRD table writes are visible to controller */
2413 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2414
2415 /* specify data direction, triple-check start bit is clear */
2416 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2417 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2418 if (!rw)
2419 dmactl |= ATA_DMA_WR;
2420 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2421
2422 /* issue r/w command */
Tejun Heo5682ed32008-04-07 22:47:16 +09002423 ap->ops->sff_exec_command(ap, &qc->tf);
Tejun Heo272f7882008-03-25 22:16:40 +09002424}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002425EXPORT_SYMBOL_GPL(ata_bmdma_setup);
Tejun Heo272f7882008-03-25 22:16:40 +09002426
2427/**
2428 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2429 * @qc: Info associated with this ATA transaction.
2430 *
2431 * LOCKING:
2432 * spin_lock_irqsave(host lock)
2433 */
2434void ata_bmdma_start(struct ata_queued_cmd *qc)
2435{
2436 struct ata_port *ap = qc->ap;
2437 u8 dmactl;
2438
2439 /* start host DMA transaction */
2440 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2441 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2442
2443 /* Strictly, one may wish to issue an ioread8() here, to
2444 * flush the mmio write. However, control also passes
2445 * to the hardware at this point, and it will interrupt
2446 * us when we are to resume control. So, in effect,
2447 * we don't care when the mmio write flushes.
2448 * Further, a read of the DMA status register _immediately_
2449 * following the write may not be what certain flaky hardware
2450 * is expected, so I think it is best to not add a readb()
2451 * without first all the MMIO ATA cards/mobos.
2452 * Or maybe I'm just being paranoid.
2453 *
2454 * FIXME: The posting of this write means I/O starts are
2455 * unneccessarily delayed for MMIO
2456 */
2457}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002458EXPORT_SYMBOL_GPL(ata_bmdma_start);
Tejun Heo272f7882008-03-25 22:16:40 +09002459
2460/**
2461 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2462 * @qc: Command we are ending DMA for
2463 *
2464 * Clears the ATA_DMA_START flag in the dma control register
2465 *
2466 * May be used as the bmdma_stop() entry in ata_port_operations.
2467 *
2468 * LOCKING:
2469 * spin_lock_irqsave(host lock)
2470 */
2471void ata_bmdma_stop(struct ata_queued_cmd *qc)
2472{
2473 struct ata_port *ap = qc->ap;
2474 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2475
2476 /* clear start/stop bit */
2477 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2478 mmio + ATA_DMA_CMD);
2479
2480 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
Alan Coxa57c1ba2008-05-29 22:10:58 +01002481 ata_sff_dma_pause(ap);
Tejun Heo272f7882008-03-25 22:16:40 +09002482}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002483EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo272f7882008-03-25 22:16:40 +09002484
2485/**
2486 * ata_bmdma_status - Read PCI IDE BMDMA status
2487 * @ap: Port associated with this ATA transaction.
2488 *
2489 * Read and return BMDMA status register.
2490 *
2491 * May be used as the bmdma_status() entry in ata_port_operations.
2492 *
2493 * LOCKING:
2494 * spin_lock_irqsave(host lock)
2495 */
2496u8 ata_bmdma_status(struct ata_port *ap)
2497{
2498 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2499}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002500EXPORT_SYMBOL_GPL(ata_bmdma_status);
Tejun Heo272f7882008-03-25 22:16:40 +09002501
2502/**
Tejun Heo624d5c52008-03-25 22:16:41 +09002503 * ata_bus_reset - reset host port and associated ATA channel
2504 * @ap: port to reset
2505 *
2506 * This is typically the first time we actually start issuing
2507 * commands to the ATA channel. We wait for BSY to clear, then
2508 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2509 * result. Determine what devices, if any, are on the channel
2510 * by looking at the device 0/1 error register. Look at the signature
2511 * stored in each device's taskfile registers, to determine if
2512 * the device is ATA or ATAPI.
2513 *
2514 * LOCKING:
2515 * PCI/etc. bus probe sem.
2516 * Obtains host lock.
2517 *
2518 * SIDE EFFECTS:
2519 * Sets ATA_FLAG_DISABLED if bus reset fails.
2520 *
2521 * DEPRECATED:
2522 * This function is only for drivers which still use old EH and
2523 * will be removed soon.
Tejun Heo272f7882008-03-25 22:16:40 +09002524 */
Tejun Heo624d5c52008-03-25 22:16:41 +09002525void ata_bus_reset(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +09002526{
Tejun Heo624d5c52008-03-25 22:16:41 +09002527 struct ata_device *device = ap->link.device;
2528 struct ata_ioports *ioaddr = &ap->ioaddr;
2529 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2530 u8 err;
2531 unsigned int dev0, dev1 = 0, devmask = 0;
2532 int rc;
2533
2534 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
2535
2536 /* determine if device 0/1 are present */
2537 if (ap->flags & ATA_FLAG_SATA_RESET)
2538 dev0 = 1;
2539 else {
2540 dev0 = ata_devchk(ap, 0);
2541 if (slave_possible)
2542 dev1 = ata_devchk(ap, 1);
2543 }
2544
2545 if (dev0)
2546 devmask |= (1 << 0);
2547 if (dev1)
2548 devmask |= (1 << 1);
2549
2550 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002551 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002552
2553 /* issue bus reset */
2554 if (ap->flags & ATA_FLAG_SRST) {
Tejun Heo341c2c92008-05-20 02:17:51 +09002555 rc = ata_bus_softreset(ap, devmask,
2556 ata_deadline(jiffies, 40000));
Tejun Heo624d5c52008-03-25 22:16:41 +09002557 if (rc && rc != -ENODEV)
2558 goto err_out;
2559 }
2560
2561 /*
2562 * determine by signature whether we have ATA or ATAPI devices
2563 */
Tejun Heo9363c382008-04-07 22:47:16 +09002564 device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002565 if ((slave_possible) && (err != 0x81))
Tejun Heo9363c382008-04-07 22:47:16 +09002566 device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002567
2568 /* is double-select really necessary? */
2569 if (device[1].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002570 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002571 if (device[0].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002572 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002573
2574 /* if no devices were detected, disable this port */
2575 if ((device[0].class == ATA_DEV_NONE) &&
2576 (device[1].class == ATA_DEV_NONE))
2577 goto err_out;
2578
2579 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2580 /* set up device control for ATA_FLAG_SATA_RESET */
2581 iowrite8(ap->ctl, ioaddr->ctl_addr);
Stuart MENEFYe3e43852009-03-10 11:38:13 +00002582 ap->last_ctl = ap->ctl;
Tejun Heo624d5c52008-03-25 22:16:41 +09002583 }
2584
2585 DPRINTK("EXIT\n");
2586 return;
2587
2588err_out:
2589 ata_port_printk(ap, KERN_ERR, "disabling port\n");
2590 ata_port_disable(ap);
2591
2592 DPRINTK("EXIT\n");
Tejun Heo272f7882008-03-25 22:16:40 +09002593}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002594EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo272f7882008-03-25 22:16:40 +09002595
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002596#ifdef CONFIG_PCI
Alan4112e162007-01-08 12:10:05 +00002597
Tejun Heo272f7882008-03-25 22:16:40 +09002598/**
Tejun Heo9363c382008-04-07 22:47:16 +09002599 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
Tejun Heo272f7882008-03-25 22:16:40 +09002600 * @pdev: PCI device
2601 *
2602 * Some PCI ATA devices report simplex mode but in fact can be told to
2603 * enter non simplex mode. This implements the necessary logic to
2604 * perform the task on such devices. Calling it on other devices will
2605 * have -undefined- behaviour.
2606 */
Tejun Heo9363c382008-04-07 22:47:16 +09002607int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
Alan4112e162007-01-08 12:10:05 +00002608{
Tejun Heo272f7882008-03-25 22:16:40 +09002609 unsigned long bmdma = pci_resource_start(pdev, 4);
2610 u8 simplex;
Jeff Garzika84471f2007-02-26 05:51:33 -05002611
Tejun Heo272f7882008-03-25 22:16:40 +09002612 if (bmdma == 0)
2613 return -ENOENT;
2614
2615 simplex = inb(bmdma + 0x02);
2616 outb(simplex & 0x60, bmdma + 0x02);
2617 simplex = inb(bmdma + 0x02);
2618 if (simplex & 0x80)
2619 return -EOPNOTSUPP;
2620 return 0;
2621}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002622EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
Tejun Heo272f7882008-03-25 22:16:40 +09002623
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002624/**
Tejun Heo9363c382008-04-07 22:47:16 +09002625 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
Tejun Heo0f834de2007-04-17 23:44:07 +09002626 * @host: target ATA host
2627 *
2628 * Acquire PCI BMDMA resources and initialize @host accordingly.
2629 *
2630 * LOCKING:
2631 * Inherited from calling layer (may sleep).
2632 *
2633 * RETURNS:
2634 * 0 on success, -errno otherwise.
2635 */
Tejun Heo9363c382008-04-07 22:47:16 +09002636int ata_pci_bmdma_init(struct ata_host *host)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002637{
Tejun Heo0f834de2007-04-17 23:44:07 +09002638 struct device *gdev = host->dev;
2639 struct pci_dev *pdev = to_pci_dev(gdev);
2640 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002641
Alan Cox6fdc99a2007-07-26 18:41:30 +01002642 /* No BAR4 allocation: No DMA */
2643 if (pci_resource_start(pdev, 4) == 0)
2644 return 0;
2645
Tejun Heo0f834de2007-04-17 23:44:07 +09002646 /* TODO: If we get no DMA mask we should fall back to PIO */
2647 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
2648 if (rc)
2649 return rc;
2650 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
2651 if (rc)
2652 return rc;
2653
2654 /* request and iomap DMA region */
Tejun Heo35a10a82008-01-04 18:42:21 +09002655 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
Tejun Heo0f834de2007-04-17 23:44:07 +09002656 if (rc) {
2657 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
2658 return -ENOMEM;
2659 }
2660 host->iomap = pcim_iomap_table(pdev);
2661
Tejun Heo1626aeb2007-05-04 12:43:58 +02002662 for (i = 0; i < 2; i++) {
Tejun Heo0f834de2007-04-17 23:44:07 +09002663 struct ata_port *ap = host->ports[i];
Tejun Heo0f834de2007-04-17 23:44:07 +09002664 void __iomem *bmdma = host->iomap[4] + 8 * i;
2665
2666 if (ata_port_is_dummy(ap))
2667 continue;
2668
Tejun Heo21b0ad42007-04-17 23:44:07 +09002669 ap->ioaddr.bmdma_addr = bmdma;
Tejun Heo0f834de2007-04-17 23:44:07 +09002670 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
2671 (ioread8(bmdma + 2) & 0x80))
2672 host->flags |= ATA_HOST_SIMPLEX;
Tejun Heocbcdd872007-08-18 13:14:55 +09002673
2674 ata_port_desc(ap, "bmdma 0x%llx",
Alan Cox0fe40ff2009-01-05 14:16:13 +00002675 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
Tejun Heo0d5ff562007-02-01 15:06:36 +09002676 }
2677
Tejun Heo0f834de2007-04-17 23:44:07 +09002678 return 0;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002679}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002680EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002681
Tejun Heo272f7882008-03-25 22:16:40 +09002682static int ata_resources_present(struct pci_dev *pdev, int port)
2683{
2684 int i;
2685
2686 /* Check the PCI resources for this channel are enabled */
2687 port = port * 2;
Alan Cox0fe40ff2009-01-05 14:16:13 +00002688 for (i = 0; i < 2; i++) {
Tejun Heo272f7882008-03-25 22:16:40 +09002689 if (pci_resource_start(pdev, port + i) == 0 ||
2690 pci_resource_len(pdev, port + i) == 0)
2691 return 0;
2692 }
2693 return 1;
2694}
2695
Tejun Heod491b272007-04-17 23:44:07 +09002696/**
Tejun Heo9363c382008-04-07 22:47:16 +09002697 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
Tejun Heod491b272007-04-17 23:44:07 +09002698 * @host: target ATA host
Tejun Heod491b272007-04-17 23:44:07 +09002699 *
Tejun Heo1626aeb2007-05-04 12:43:58 +02002700 * Acquire native PCI ATA resources for @host and initialize the
2701 * first two ports of @host accordingly. Ports marked dummy are
2702 * skipped and allocation failure makes the port dummy.
Tejun Heod491b272007-04-17 23:44:07 +09002703 *
Tejun Heod583bc12007-07-04 18:02:07 +09002704 * Note that native PCI resources are valid even for legacy hosts
2705 * as we fix up pdev resources array early in boot, so this
2706 * function can be used for both native and legacy SFF hosts.
2707 *
Tejun Heod491b272007-04-17 23:44:07 +09002708 * LOCKING:
2709 * Inherited from calling layer (may sleep).
2710 *
2711 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +02002712 * 0 if at least one port is initialized, -ENODEV if no port is
2713 * available.
Tejun Heod491b272007-04-17 23:44:07 +09002714 */
Tejun Heo9363c382008-04-07 22:47:16 +09002715int ata_pci_sff_init_host(struct ata_host *host)
Tejun Heod491b272007-04-17 23:44:07 +09002716{
2717 struct device *gdev = host->dev;
2718 struct pci_dev *pdev = to_pci_dev(gdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002719 unsigned int mask = 0;
Tejun Heod491b272007-04-17 23:44:07 +09002720 int i, rc;
2721
Tejun Heod491b272007-04-17 23:44:07 +09002722 /* request, iomap BARs and init port addresses accordingly */
2723 for (i = 0; i < 2; i++) {
2724 struct ata_port *ap = host->ports[i];
2725 int base = i * 2;
2726 void __iomem * const *iomap;
2727
Tejun Heo1626aeb2007-05-04 12:43:58 +02002728 if (ata_port_is_dummy(ap))
Tejun Heod491b272007-04-17 23:44:07 +09002729 continue;
2730
Tejun Heo1626aeb2007-05-04 12:43:58 +02002731 /* Discard disabled ports. Some controllers show
2732 * their unused channels this way. Disabled ports are
2733 * made dummy.
2734 */
2735 if (!ata_resources_present(pdev, i)) {
2736 ap->ops = &ata_dummy_port_ops;
2737 continue;
2738 }
2739
Tejun Heo35a10a82008-01-04 18:42:21 +09002740 rc = pcim_iomap_regions(pdev, 0x3 << base,
2741 dev_driver_string(gdev));
Tejun Heod491b272007-04-17 23:44:07 +09002742 if (rc) {
Tejun Heo1626aeb2007-05-04 12:43:58 +02002743 dev_printk(KERN_WARNING, gdev,
2744 "failed to request/iomap BARs for port %d "
2745 "(errno=%d)\n", i, rc);
Tejun Heod491b272007-04-17 23:44:07 +09002746 if (rc == -EBUSY)
2747 pcim_pin_device(pdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002748 ap->ops = &ata_dummy_port_ops;
2749 continue;
Tejun Heod491b272007-04-17 23:44:07 +09002750 }
2751 host->iomap = iomap = pcim_iomap_table(pdev);
2752
2753 ap->ioaddr.cmd_addr = iomap[base];
2754 ap->ioaddr.altstatus_addr =
2755 ap->ioaddr.ctl_addr = (void __iomem *)
2756 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
Tejun Heo9363c382008-04-07 22:47:16 +09002757 ata_sff_std_ports(&ap->ioaddr);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002758
Tejun Heocbcdd872007-08-18 13:14:55 +09002759 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2760 (unsigned long long)pci_resource_start(pdev, base),
2761 (unsigned long long)pci_resource_start(pdev, base + 1));
2762
Tejun Heo1626aeb2007-05-04 12:43:58 +02002763 mask |= 1 << i;
2764 }
2765
2766 if (!mask) {
2767 dev_printk(KERN_ERR, gdev, "no available native port\n");
2768 return -ENODEV;
Tejun Heod491b272007-04-17 23:44:07 +09002769 }
2770
2771 return 0;
2772}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002773EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
Tejun Heod491b272007-04-17 23:44:07 +09002774
Tejun Heo21b0ad42007-04-17 23:44:07 +09002775/**
Tejun Heo9363c382008-04-07 22:47:16 +09002776 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
Tejun Heo21b0ad42007-04-17 23:44:07 +09002777 * @pdev: target PCI device
Tejun Heo1626aeb2007-05-04 12:43:58 +02002778 * @ppi: array of port_info, must be enough for two ports
Tejun Heo21b0ad42007-04-17 23:44:07 +09002779 * @r_host: out argument for the initialized ATA host
2780 *
2781 * Helper to allocate ATA host for @pdev, acquire all native PCI
2782 * resources and initialize it accordingly in one go.
2783 *
2784 * LOCKING:
2785 * Inherited from calling layer (may sleep).
2786 *
2787 * RETURNS:
2788 * 0 on success, -errno otherwise.
2789 */
Tejun Heo9363c382008-04-07 22:47:16 +09002790int ata_pci_sff_prepare_host(struct pci_dev *pdev,
Alan Cox0fe40ff2009-01-05 14:16:13 +00002791 const struct ata_port_info * const *ppi,
Tejun Heod583bc12007-07-04 18:02:07 +09002792 struct ata_host **r_host)
Tejun Heo21b0ad42007-04-17 23:44:07 +09002793{
2794 struct ata_host *host;
Tejun Heo21b0ad42007-04-17 23:44:07 +09002795 int rc;
2796
2797 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2798 return -ENOMEM;
2799
2800 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2801 if (!host) {
2802 dev_printk(KERN_ERR, &pdev->dev,
2803 "failed to allocate ATA host\n");
2804 rc = -ENOMEM;
2805 goto err_out;
2806 }
2807
Tejun Heo9363c382008-04-07 22:47:16 +09002808 rc = ata_pci_sff_init_host(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002809 if (rc)
2810 goto err_out;
2811
2812 /* init DMA related stuff */
Tejun Heo9363c382008-04-07 22:47:16 +09002813 rc = ata_pci_bmdma_init(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002814 if (rc)
2815 goto err_bmdma;
2816
2817 devres_remove_group(&pdev->dev, NULL);
2818 *r_host = host;
2819 return 0;
2820
Alan Cox0fe40ff2009-01-05 14:16:13 +00002821err_bmdma:
Tejun Heo21b0ad42007-04-17 23:44:07 +09002822 /* This is necessary because PCI and iomap resources are
2823 * merged and releasing the top group won't release the
2824 * acquired resources if some of those have been acquired
2825 * before entering this function.
2826 */
2827 pcim_iounmap_regions(pdev, 0xf);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002828err_out:
Tejun Heo21b0ad42007-04-17 23:44:07 +09002829 devres_release_group(&pdev->dev, NULL);
2830 return rc;
2831}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002832EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002833
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002834/**
Tejun Heo9363c382008-04-07 22:47:16 +09002835 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002836 * @host: target SFF ATA host
2837 * @irq_handler: irq_handler used when requesting IRQ(s)
2838 * @sht: scsi_host_template to use when registering the host
2839 *
2840 * This is the counterpart of ata_host_activate() for SFF ATA
2841 * hosts. This separate helper is necessary because SFF hosts
2842 * use two separate interrupts in legacy mode.
2843 *
2844 * LOCKING:
2845 * Inherited from calling layer (may sleep).
2846 *
2847 * RETURNS:
2848 * 0 on success, -errno otherwise.
2849 */
Tejun Heo9363c382008-04-07 22:47:16 +09002850int ata_pci_sff_activate_host(struct ata_host *host,
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002851 irq_handler_t irq_handler,
2852 struct scsi_host_template *sht)
2853{
2854 struct device *dev = host->dev;
2855 struct pci_dev *pdev = to_pci_dev(dev);
2856 const char *drv_name = dev_driver_string(host->dev);
2857 int legacy_mode = 0, rc;
2858
2859 rc = ata_host_start(host);
2860 if (rc)
2861 return rc;
2862
2863 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
2864 u8 tmp8, mask;
2865
2866 /* TODO: What if one channel is in native mode ... */
2867 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
2868 mask = (1 << 2) | (1 << 0);
2869 if ((tmp8 & mask) != mask)
2870 legacy_mode = 1;
2871#if defined(CONFIG_NO_ATA_LEGACY)
2872 /* Some platforms with PCI limits cannot address compat
2873 port space. In that case we punt if their firmware has
2874 left a device in compatibility mode */
2875 if (legacy_mode) {
2876 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
2877 return -EOPNOTSUPP;
2878 }
2879#endif
2880 }
2881
2882 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2883 return -ENOMEM;
2884
2885 if (!legacy_mode && pdev->irq) {
2886 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2887 IRQF_SHARED, drv_name, host);
2888 if (rc)
2889 goto out;
2890
2891 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
2892 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
2893 } else if (legacy_mode) {
2894 if (!ata_port_is_dummy(host->ports[0])) {
2895 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2896 irq_handler, IRQF_SHARED,
2897 drv_name, host);
2898 if (rc)
2899 goto out;
2900
2901 ata_port_desc(host->ports[0], "irq %d",
2902 ATA_PRIMARY_IRQ(pdev));
2903 }
2904
2905 if (!ata_port_is_dummy(host->ports[1])) {
2906 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2907 irq_handler, IRQF_SHARED,
2908 drv_name, host);
2909 if (rc)
2910 goto out;
2911
2912 ata_port_desc(host->ports[1], "irq %d",
2913 ATA_SECONDARY_IRQ(pdev));
2914 }
2915 }
2916
2917 rc = ata_host_register(host, sht);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002918out:
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002919 if (rc == 0)
2920 devres_remove_group(dev, NULL);
2921 else
2922 devres_release_group(dev, NULL);
2923
2924 return rc;
2925}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002926EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002927
2928/**
Tejun Heo9363c382008-04-07 22:47:16 +09002929 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002930 * @pdev: Controller to be initialized
Tejun Heo1626aeb2007-05-04 12:43:58 +02002931 * @ppi: array of port_info, must be enough for two ports
Tejun Heo1bd5b712008-03-25 12:22:49 +09002932 * @sht: scsi_host_template to use when registering the host
Tejun Heo887125e2008-03-25 12:22:49 +09002933 * @host_priv: host private_data
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002934 *
2935 * This is a helper function which can be called from a driver's
2936 * xxx_init_one() probe function if the hardware uses traditional
2937 * IDE taskfile registers.
2938 *
2939 * This function calls pci_enable_device(), reserves its register
2940 * regions, sets the dma mask, enables bus master mode, and calls
2941 * ata_device_add()
2942 *
Alan Cox2ec7df02006-08-10 16:59:10 +09002943 * ASSUMPTION:
2944 * Nobody makes a single channel controller that appears solely as
2945 * the secondary legacy port on PCI.
2946 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002947 * LOCKING:
2948 * Inherited from PCI layer (may sleep).
2949 *
2950 * RETURNS:
2951 * Zero on success, negative on errno-based value on error.
2952 */
Tejun Heo9363c382008-04-07 22:47:16 +09002953int ata_pci_sff_init_one(struct pci_dev *pdev,
Alan Cox0fe40ff2009-01-05 14:16:13 +00002954 const struct ata_port_info * const *ppi,
Tejun Heo9363c382008-04-07 22:47:16 +09002955 struct scsi_host_template *sht, void *host_priv)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002956{
Tejun Heof0d36ef2007-01-20 16:00:28 +09002957 struct device *dev = &pdev->dev;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002958 const struct ata_port_info *pi = NULL;
Tejun Heo0f834de2007-04-17 23:44:07 +09002959 struct ata_host *host = NULL;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002960 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002961
2962 DPRINTK("ENTER\n");
2963
Tejun Heo1626aeb2007-05-04 12:43:58 +02002964 /* look up the first valid port_info */
2965 for (i = 0; i < 2 && ppi[i]; i++) {
2966 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
2967 pi = ppi[i];
2968 break;
2969 }
2970 }
2971
2972 if (!pi) {
2973 dev_printk(KERN_ERR, &pdev->dev,
2974 "no valid port_info specified\n");
2975 return -EINVAL;
2976 }
2977
Tejun Heof0d36ef2007-01-20 16:00:28 +09002978 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2979 return -ENOMEM;
2980
Tejun Heof0d36ef2007-01-20 16:00:28 +09002981 rc = pcim_enable_device(pdev);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002982 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002983 goto out;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002984
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002985 /* prepare and activate SFF host */
Tejun Heo9363c382008-04-07 22:47:16 +09002986 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
Tejun Heod583bc12007-07-04 18:02:07 +09002987 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002988 goto out;
Tejun Heo887125e2008-03-25 12:22:49 +09002989 host->private_data = host_priv;
Tejun Heod491b272007-04-17 23:44:07 +09002990
Tejun Heod491b272007-04-17 23:44:07 +09002991 pci_set_master(pdev);
Tejun Heo9363c382008-04-07 22:47:16 +09002992 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002993out:
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002994 if (rc == 0)
2995 devres_remove_group(&pdev->dev, NULL);
2996 else
2997 devres_release_group(&pdev->dev, NULL);
Tejun Heod491b272007-04-17 23:44:07 +09002998
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002999 return rc;
3000}
Tejun Heo9363c382008-04-07 22:47:16 +09003001EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
Alan Cox0fe40ff2009-01-05 14:16:13 +00003002
Tejun Heo624d5c52008-03-25 22:16:41 +09003003#endif /* CONFIG_PCI */