blob: bb18415d3d6344214948b56f30491a2c2bea87be [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
Alan Coxc96f1732009-03-24 10:23:46 +000068 .lost_interrupt = ata_sff_lost_interrupt,
69
Tejun Heo624d5c52008-03-25 22:16:41 +090070 .port_start = ata_sff_port_start,
71};
Alan Cox0fe40ff2009-01-05 14:16:13 +000072EXPORT_SYMBOL_GPL(ata_sff_port_ops);
Tejun Heo624d5c52008-03-25 22:16:41 +090073
74const struct ata_port_operations ata_bmdma_port_ops = {
75 .inherits = &ata_sff_port_ops,
76
Tejun Heo9363c382008-04-07 22:47:16 +090077 .mode_filter = ata_bmdma_mode_filter,
Tejun Heo624d5c52008-03-25 22:16:41 +090078
79 .bmdma_setup = ata_bmdma_setup,
80 .bmdma_start = ata_bmdma_start,
81 .bmdma_stop = ata_bmdma_stop,
82 .bmdma_status = ata_bmdma_status,
Tejun Heo624d5c52008-03-25 22:16:41 +090083};
Alan Cox0fe40ff2009-01-05 14:16:13 +000084EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
Tejun Heo624d5c52008-03-25 22:16:41 +090085
Alan Cox871af122009-01-05 14:16:39 +000086const struct ata_port_operations ata_bmdma32_port_ops = {
87 .inherits = &ata_bmdma_port_ops,
88
89 .sff_data_xfer = ata_sff_data_xfer32,
Alan Coxe3cf95d2009-04-09 17:31:17 +010090 .port_start = ata_sff_port_start32,
Alan Cox871af122009-01-05 14:16:39 +000091};
92EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
93
Tejun Heo624d5c52008-03-25 22:16:41 +090094/**
95 * ata_fill_sg - Fill PCI IDE PRD table
96 * @qc: Metadata associated with taskfile to be transferred
97 *
98 * Fill PCI IDE PRD (scatter-gather) table with segments
99 * associated with the current disk command.
100 *
101 * LOCKING:
102 * spin_lock_irqsave(host lock)
103 *
104 */
105static void ata_fill_sg(struct ata_queued_cmd *qc)
106{
107 struct ata_port *ap = qc->ap;
108 struct scatterlist *sg;
109 unsigned int si, pi;
110
111 pi = 0;
112 for_each_sg(qc->sg, sg, qc->n_elem, si) {
113 u32 addr, offset;
114 u32 sg_len, len;
115
116 /* determine if physical DMA addr spans 64K boundary.
117 * Note h/w doesn't support 64-bit, so we unconditionally
118 * truncate dma_addr_t to u32.
119 */
120 addr = (u32) sg_dma_address(sg);
121 sg_len = sg_dma_len(sg);
122
123 while (sg_len) {
124 offset = addr & 0xffff;
125 len = sg_len;
126 if ((offset + sg_len) > 0x10000)
127 len = 0x10000 - offset;
128
129 ap->prd[pi].addr = cpu_to_le32(addr);
130 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
131 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
132
133 pi++;
134 sg_len -= len;
135 addr += len;
136 }
137 }
138
139 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
140}
141
142/**
143 * ata_fill_sg_dumb - Fill PCI IDE PRD table
144 * @qc: Metadata associated with taskfile to be transferred
145 *
146 * Fill PCI IDE PRD (scatter-gather) table with segments
147 * associated with the current disk command. Perform the fill
148 * so that we avoid writing any length 64K records for
149 * controllers that don't follow the spec.
150 *
151 * LOCKING:
152 * spin_lock_irqsave(host lock)
153 *
154 */
155static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
156{
157 struct ata_port *ap = qc->ap;
158 struct scatterlist *sg;
159 unsigned int si, pi;
160
161 pi = 0;
162 for_each_sg(qc->sg, sg, qc->n_elem, si) {
163 u32 addr, offset;
164 u32 sg_len, len, blen;
165
166 /* determine if physical DMA addr spans 64K boundary.
167 * Note h/w doesn't support 64-bit, so we unconditionally
168 * truncate dma_addr_t to u32.
169 */
170 addr = (u32) sg_dma_address(sg);
171 sg_len = sg_dma_len(sg);
172
173 while (sg_len) {
174 offset = addr & 0xffff;
175 len = sg_len;
176 if ((offset + sg_len) > 0x10000)
177 len = 0x10000 - offset;
178
179 blen = len & 0xffff;
180 ap->prd[pi].addr = cpu_to_le32(addr);
181 if (blen == 0) {
Alan Cox0fe40ff2009-01-05 14:16:13 +0000182 /* Some PATA chipsets like the CS5530 can't
183 cope with 0x0000 meaning 64K as the spec
184 says */
Tejun Heo624d5c52008-03-25 22:16:41 +0900185 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
186 blen = 0x8000;
187 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
188 }
189 ap->prd[pi].flags_len = cpu_to_le32(blen);
190 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
191
192 pi++;
193 sg_len -= len;
194 addr += len;
195 }
196 }
197
198 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
199}
200
201/**
Tejun Heo9363c382008-04-07 22:47:16 +0900202 * ata_sff_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900203 * @qc: Metadata associated with taskfile to be prepared
204 *
205 * Prepare ATA taskfile for submission.
206 *
207 * LOCKING:
208 * spin_lock_irqsave(host lock)
209 */
Tejun Heo9363c382008-04-07 22:47:16 +0900210void ata_sff_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900211{
212 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
213 return;
214
215 ata_fill_sg(qc);
216}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000217EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900218
219/**
Tejun Heo9363c382008-04-07 22:47:16 +0900220 * ata_sff_dumb_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900221 * @qc: Metadata associated with taskfile to be prepared
222 *
223 * Prepare ATA taskfile for submission.
224 *
225 * LOCKING:
226 * spin_lock_irqsave(host lock)
227 */
Tejun Heo9363c382008-04-07 22:47:16 +0900228void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900229{
230 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
231 return;
232
233 ata_fill_sg_dumb(qc);
234}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000235EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900236
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500237/**
Tejun Heo9363c382008-04-07 22:47:16 +0900238 * ata_sff_check_status - Read device status reg & clear interrupt
Tejun Heo272f7882008-03-25 22:16:40 +0900239 * @ap: port where the device is
240 *
241 * Reads ATA taskfile status register for currently-selected device
242 * and return its value. This also clears pending interrupts
243 * from this device
244 *
245 * LOCKING:
246 * Inherited from caller.
247 */
Tejun Heo9363c382008-04-07 22:47:16 +0900248u8 ata_sff_check_status(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900249{
250 return ioread8(ap->ioaddr.status_addr);
251}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000252EXPORT_SYMBOL_GPL(ata_sff_check_status);
Tejun Heo272f7882008-03-25 22:16:40 +0900253
254/**
Tejun Heo9363c382008-04-07 22:47:16 +0900255 * ata_sff_altstatus - Read device alternate status reg
Tejun Heo272f7882008-03-25 22:16:40 +0900256 * @ap: port where the device is
257 *
258 * Reads ATA taskfile alternate status register for
259 * currently-selected device and return its value.
260 *
261 * Note: may NOT be used as the check_altstatus() entry in
262 * ata_port_operations.
263 *
264 * LOCKING:
265 * Inherited from caller.
266 */
Alan Coxa57c1ba2008-05-29 22:10:58 +0100267static u8 ata_sff_altstatus(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900268{
Tejun Heo5682ed32008-04-07 22:47:16 +0900269 if (ap->ops->sff_check_altstatus)
270 return ap->ops->sff_check_altstatus(ap);
Tejun Heo272f7882008-03-25 22:16:40 +0900271
272 return ioread8(ap->ioaddr.altstatus_addr);
273}
274
275/**
Alan Coxa57c1ba2008-05-29 22:10:58 +0100276 * ata_sff_irq_status - Check if the device is busy
277 * @ap: port where the device is
278 *
279 * Determine if the port is currently busy. Uses altstatus
280 * if available in order to avoid clearing shared IRQ status
281 * when finding an IRQ source. Non ctl capable devices don't
282 * share interrupt lines fortunately for us.
283 *
284 * LOCKING:
285 * Inherited from caller.
286 */
287static u8 ata_sff_irq_status(struct ata_port *ap)
288{
289 u8 status;
290
291 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
292 status = ata_sff_altstatus(ap);
293 /* Not us: We are busy */
294 if (status & ATA_BUSY)
Alan Cox0fe40ff2009-01-05 14:16:13 +0000295 return status;
Alan Coxa57c1ba2008-05-29 22:10:58 +0100296 }
297 /* Clear INTRQ latch */
Hugh Dickins6311c902008-06-05 14:44:39 +0100298 status = ap->ops->sff_check_status(ap);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100299 return status;
300}
301
302/**
303 * ata_sff_sync - Flush writes
304 * @ap: Port to wait for.
305 *
306 * CAUTION:
307 * If we have an mmio device with no ctl and no altstatus
308 * method this will fail. No such devices are known to exist.
309 *
310 * LOCKING:
311 * Inherited from caller.
312 */
313
314static void ata_sff_sync(struct ata_port *ap)
315{
316 if (ap->ops->sff_check_altstatus)
317 ap->ops->sff_check_altstatus(ap);
318 else if (ap->ioaddr.altstatus_addr)
319 ioread8(ap->ioaddr.altstatus_addr);
320}
321
322/**
323 * ata_sff_pause - Flush writes and wait 400nS
324 * @ap: Port to pause for.
325 *
326 * CAUTION:
327 * If we have an mmio device with no ctl and no altstatus
328 * method this will fail. No such devices are known to exist.
329 *
330 * LOCKING:
331 * Inherited from caller.
332 */
333
334void ata_sff_pause(struct ata_port *ap)
335{
336 ata_sff_sync(ap);
337 ndelay(400);
338}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000339EXPORT_SYMBOL_GPL(ata_sff_pause);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100340
341/**
342 * ata_sff_dma_pause - Pause before commencing DMA
343 * @ap: Port to pause for.
344 *
345 * Perform I/O fencing and ensure sufficient cycle delays occur
346 * for the HDMA1:0 transition
347 */
Alan Cox0fe40ff2009-01-05 14:16:13 +0000348
Alan Coxa57c1ba2008-05-29 22:10:58 +0100349void ata_sff_dma_pause(struct ata_port *ap)
350{
351 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
352 /* An altstatus read will cause the needed delay without
353 messing up the IRQ status */
354 ata_sff_altstatus(ap);
355 return;
356 }
357 /* There are no DMA controllers without ctl. BUG here to ensure
358 we never violate the HDMA1:0 transition timing and risk
359 corruption. */
360 BUG();
361}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000362EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100363
364/**
Tejun Heo9363c382008-04-07 22:47:16 +0900365 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
Tejun Heo624d5c52008-03-25 22:16:41 +0900366 * @ap: port containing status register to be polled
Tejun Heo341c2c92008-05-20 02:17:51 +0900367 * @tmout_pat: impatience timeout in msecs
368 * @tmout: overall timeout in msecs
Tejun Heo624d5c52008-03-25 22:16:41 +0900369 *
370 * Sleep until ATA Status register bit BSY clears,
371 * or a timeout occurs.
372 *
373 * LOCKING:
374 * Kernel thread context (may sleep).
375 *
376 * RETURNS:
377 * 0 on success, -errno otherwise.
378 */
Tejun Heo9363c382008-04-07 22:47:16 +0900379int ata_sff_busy_sleep(struct ata_port *ap,
380 unsigned long tmout_pat, unsigned long tmout)
Tejun Heo624d5c52008-03-25 22:16:41 +0900381{
382 unsigned long timer_start, timeout;
383 u8 status;
384
Tejun Heo9363c382008-04-07 22:47:16 +0900385 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
Tejun Heo624d5c52008-03-25 22:16:41 +0900386 timer_start = jiffies;
Tejun Heo341c2c92008-05-20 02:17:51 +0900387 timeout = ata_deadline(timer_start, tmout_pat);
Tejun Heo624d5c52008-03-25 22:16:41 +0900388 while (status != 0xff && (status & ATA_BUSY) &&
389 time_before(jiffies, timeout)) {
390 msleep(50);
Tejun Heo9363c382008-04-07 22:47:16 +0900391 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
Tejun Heo624d5c52008-03-25 22:16:41 +0900392 }
393
394 if (status != 0xff && (status & ATA_BUSY))
395 ata_port_printk(ap, KERN_WARNING,
396 "port is slow to respond, please be patient "
397 "(Status 0x%x)\n", status);
398
Tejun Heo341c2c92008-05-20 02:17:51 +0900399 timeout = ata_deadline(timer_start, tmout);
Tejun Heo624d5c52008-03-25 22:16:41 +0900400 while (status != 0xff && (status & ATA_BUSY) &&
401 time_before(jiffies, timeout)) {
402 msleep(50);
Tejun Heo5682ed32008-04-07 22:47:16 +0900403 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900404 }
405
406 if (status == 0xff)
407 return -ENODEV;
408
409 if (status & ATA_BUSY) {
410 ata_port_printk(ap, KERN_ERR, "port failed to respond "
411 "(%lu secs, Status 0x%x)\n",
Tejun Heo341c2c92008-05-20 02:17:51 +0900412 DIV_ROUND_UP(tmout, 1000), status);
Tejun Heo624d5c52008-03-25 22:16:41 +0900413 return -EBUSY;
414 }
415
416 return 0;
417}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000418EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
Tejun Heo624d5c52008-03-25 22:16:41 +0900419
Tejun Heoaa2731a2008-04-07 22:47:19 +0900420static int ata_sff_check_ready(struct ata_link *link)
421{
422 u8 status = link->ap->ops->sff_check_status(link->ap);
423
Tejun Heo78ab88f2008-05-01 23:41:41 +0900424 return ata_check_ready(status);
Tejun Heoaa2731a2008-04-07 22:47:19 +0900425}
426
Tejun Heo624d5c52008-03-25 22:16:41 +0900427/**
Tejun Heo9363c382008-04-07 22:47:16 +0900428 * ata_sff_wait_ready - sleep until BSY clears, or timeout
Tejun Heo705e76b2008-04-07 22:47:19 +0900429 * @link: SFF link to wait ready status for
Tejun Heo624d5c52008-03-25 22:16:41 +0900430 * @deadline: deadline jiffies for the operation
431 *
432 * Sleep until ATA Status register bit BSY clears, or timeout
433 * occurs.
434 *
435 * LOCKING:
436 * Kernel thread context (may sleep).
437 *
438 * RETURNS:
439 * 0 on success, -errno otherwise.
440 */
Tejun Heo705e76b2008-04-07 22:47:19 +0900441int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +0900442{
Tejun Heoaa2731a2008-04-07 22:47:19 +0900443 return ata_wait_ready(link, deadline, ata_sff_check_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900444}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000445EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900446
447/**
Tejun Heo9363c382008-04-07 22:47:16 +0900448 * ata_sff_dev_select - Select device 0/1 on ATA bus
Tejun Heo624d5c52008-03-25 22:16:41 +0900449 * @ap: ATA channel to manipulate
450 * @device: ATA device (numbered from zero) to select
451 *
452 * Use the method defined in the ATA specification to
453 * make either device 0, or device 1, active on the
454 * ATA channel. Works with both PIO and MMIO.
455 *
456 * May be used as the dev_select() entry in ata_port_operations.
457 *
458 * LOCKING:
459 * caller.
460 */
Tejun Heo9363c382008-04-07 22:47:16 +0900461void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
Tejun Heo624d5c52008-03-25 22:16:41 +0900462{
463 u8 tmp;
464
465 if (device == 0)
466 tmp = ATA_DEVICE_OBS;
467 else
468 tmp = ATA_DEVICE_OBS | ATA_DEV1;
469
470 iowrite8(tmp, ap->ioaddr.device_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900471 ata_sff_pause(ap); /* needed; also flushes, for mmio */
Tejun Heo624d5c52008-03-25 22:16:41 +0900472}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000473EXPORT_SYMBOL_GPL(ata_sff_dev_select);
Tejun Heo624d5c52008-03-25 22:16:41 +0900474
475/**
476 * ata_dev_select - Select device 0/1 on ATA bus
477 * @ap: ATA channel to manipulate
478 * @device: ATA device (numbered from zero) to select
479 * @wait: non-zero to wait for Status register BSY bit to clear
480 * @can_sleep: non-zero if context allows sleeping
481 *
482 * Use the method defined in the ATA specification to
483 * make either device 0, or device 1, active on the
484 * ATA channel.
485 *
Tejun Heo9363c382008-04-07 22:47:16 +0900486 * This is a high-level version of ata_sff_dev_select(), which
487 * additionally provides the services of inserting the proper
488 * pauses and status polling, where needed.
Tejun Heo624d5c52008-03-25 22:16:41 +0900489 *
490 * LOCKING:
491 * caller.
492 */
493void ata_dev_select(struct ata_port *ap, unsigned int device,
494 unsigned int wait, unsigned int can_sleep)
495{
496 if (ata_msg_probe(ap))
497 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
498 "device %u, wait %u\n", device, wait);
499
500 if (wait)
501 ata_wait_idle(ap);
502
Tejun Heo5682ed32008-04-07 22:47:16 +0900503 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +0900504
505 if (wait) {
506 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
507 msleep(150);
508 ata_wait_idle(ap);
509 }
510}
511
512/**
Tejun Heo9363c382008-04-07 22:47:16 +0900513 * ata_sff_irq_on - Enable interrupts on a port.
Tejun Heo90088bb2006-10-09 11:10:26 +0900514 * @ap: Port on which interrupts are enabled.
515 *
516 * Enable interrupts on a legacy IDE device using MMIO or PIO,
517 * wait for idle, clear any pending interrupts.
518 *
519 * LOCKING:
520 * Inherited from caller.
521 */
Tejun Heo9363c382008-04-07 22:47:16 +0900522u8 ata_sff_irq_on(struct ata_port *ap)
Tejun Heo90088bb2006-10-09 11:10:26 +0900523{
524 struct ata_ioports *ioaddr = &ap->ioaddr;
525 u8 tmp;
526
527 ap->ctl &= ~ATA_NIEN;
528 ap->last_ctl = ap->ctl;
529
Tejun Heof659f0e42008-03-06 13:12:54 +0900530 if (ioaddr->ctl_addr)
531 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo90088bb2006-10-09 11:10:26 +0900532 tmp = ata_wait_idle(ap);
533
Tejun Heo5682ed32008-04-07 22:47:16 +0900534 ap->ops->sff_irq_clear(ap);
Tejun Heo90088bb2006-10-09 11:10:26 +0900535
536 return tmp;
537}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000538EXPORT_SYMBOL_GPL(ata_sff_irq_on);
Tejun Heo90088bb2006-10-09 11:10:26 +0900539
540/**
Tejun Heo9363c382008-04-07 22:47:16 +0900541 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
Tejun Heo272f7882008-03-25 22:16:40 +0900542 * @ap: Port associated with this ATA transaction.
543 *
544 * Clear interrupt and error flags in DMA status register.
545 *
546 * May be used as the irq_clear() entry in ata_port_operations.
547 *
548 * LOCKING:
549 * spin_lock_irqsave(host lock)
550 */
Tejun Heo9363c382008-04-07 22:47:16 +0900551void ata_sff_irq_clear(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900552{
553 void __iomem *mmio = ap->ioaddr.bmdma_addr;
554
555 if (!mmio)
556 return;
557
558 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
559}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000560EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
Tejun Heo272f7882008-03-25 22:16:40 +0900561
562/**
Tejun Heo9363c382008-04-07 22:47:16 +0900563 * ata_sff_tf_load - send taskfile registers to host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500564 * @ap: Port to which output is sent
565 * @tf: ATA taskfile register set
566 *
567 * Outputs ATA taskfile to standard ATA host controller.
568 *
569 * LOCKING:
570 * Inherited from caller.
571 */
Tejun Heo9363c382008-04-07 22:47:16 +0900572void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500573{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900574 struct ata_ioports *ioaddr = &ap->ioaddr;
575 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
576
577 if (tf->ctl != ap->last_ctl) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900578 if (ioaddr->ctl_addr)
579 iowrite8(tf->ctl, ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900580 ap->last_ctl = tf->ctl;
581 ata_wait_idle(ap);
582 }
583
584 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900585 WARN_ON_ONCE(!ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900586 iowrite8(tf->hob_feature, ioaddr->feature_addr);
587 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
588 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
589 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
590 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
591 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
592 tf->hob_feature,
593 tf->hob_nsect,
594 tf->hob_lbal,
595 tf->hob_lbam,
596 tf->hob_lbah);
597 }
598
599 if (is_addr) {
600 iowrite8(tf->feature, ioaddr->feature_addr);
601 iowrite8(tf->nsect, ioaddr->nsect_addr);
602 iowrite8(tf->lbal, ioaddr->lbal_addr);
603 iowrite8(tf->lbam, ioaddr->lbam_addr);
604 iowrite8(tf->lbah, ioaddr->lbah_addr);
605 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
606 tf->feature,
607 tf->nsect,
608 tf->lbal,
609 tf->lbam,
610 tf->lbah);
611 }
612
613 if (tf->flags & ATA_TFLAG_DEVICE) {
614 iowrite8(tf->device, ioaddr->device_addr);
615 VPRINTK("device 0x%X\n", tf->device);
616 }
617
618 ata_wait_idle(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500619}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000620EXPORT_SYMBOL_GPL(ata_sff_tf_load);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500621
622/**
Tejun Heo9363c382008-04-07 22:47:16 +0900623 * ata_sff_tf_read - input device's ATA taskfile shadow registers
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500624 * @ap: Port from which input is read
625 * @tf: ATA taskfile register set for storing input
626 *
627 * Reads ATA taskfile registers for currently-selected device
Alan Cox76548ed2007-11-19 14:34:56 +0000628 * into @tf. Assumes the device has a fully SFF compliant task file
629 * layout and behaviour. If you device does not (eg has a different
630 * status method) then you will need to provide a replacement tf_read
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500631 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500632 * LOCKING:
633 * Inherited from caller.
634 */
Tejun Heo9363c382008-04-07 22:47:16 +0900635void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500636{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900637 struct ata_ioports *ioaddr = &ap->ioaddr;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500638
Tejun Heo9363c382008-04-07 22:47:16 +0900639 tf->command = ata_sff_check_status(ap);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900640 tf->feature = ioread8(ioaddr->error_addr);
641 tf->nsect = ioread8(ioaddr->nsect_addr);
642 tf->lbal = ioread8(ioaddr->lbal_addr);
643 tf->lbam = ioread8(ioaddr->lbam_addr);
644 tf->lbah = ioread8(ioaddr->lbah_addr);
645 tf->device = ioread8(ioaddr->device_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500646
Tejun Heo0d5ff562007-02-01 15:06:36 +0900647 if (tf->flags & ATA_TFLAG_LBA48) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900648 if (likely(ioaddr->ctl_addr)) {
649 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
650 tf->hob_feature = ioread8(ioaddr->error_addr);
651 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
652 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
653 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
654 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
655 iowrite8(tf->ctl, ioaddr->ctl_addr);
656 ap->last_ctl = tf->ctl;
657 } else
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900658 WARN_ON_ONCE(1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900659 }
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500660}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000661EXPORT_SYMBOL_GPL(ata_sff_tf_read);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500662
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500663/**
Tejun Heo9363c382008-04-07 22:47:16 +0900664 * ata_sff_exec_command - issue ATA command to host controller
Tejun Heo272f7882008-03-25 22:16:40 +0900665 * @ap: port to which command is being issued
666 * @tf: ATA taskfile register set
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500667 *
Tejun Heo272f7882008-03-25 22:16:40 +0900668 * Issues ATA command, with proper synchronization with interrupt
669 * handler / other threads.
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500670 *
671 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400672 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500673 */
Tejun Heo9363c382008-04-07 22:47:16 +0900674void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500675{
Tejun Heo272f7882008-03-25 22:16:40 +0900676 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500677
Tejun Heo272f7882008-03-25 22:16:40 +0900678 iowrite8(tf->command, ap->ioaddr.command_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900679 ata_sff_pause(ap);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500680}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000681EXPORT_SYMBOL_GPL(ata_sff_exec_command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500682
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900683/**
Tejun Heo624d5c52008-03-25 22:16:41 +0900684 * ata_tf_to_host - issue ATA taskfile to host controller
685 * @ap: port to which command is being issued
686 * @tf: ATA taskfile register set
687 *
688 * Issues ATA taskfile register set to ATA host controller,
689 * with proper synchronization with interrupt handler and
690 * other threads.
691 *
692 * LOCKING:
693 * spin_lock_irqsave(host lock)
694 */
695static inline void ata_tf_to_host(struct ata_port *ap,
696 const struct ata_taskfile *tf)
697{
Tejun Heo5682ed32008-04-07 22:47:16 +0900698 ap->ops->sff_tf_load(ap, tf);
699 ap->ops->sff_exec_command(ap, tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900700}
701
702/**
Tejun Heo9363c382008-04-07 22:47:16 +0900703 * ata_sff_data_xfer - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900704 * @dev: device to target
705 * @buf: data buffer
706 * @buflen: buffer length
707 * @rw: read/write
708 *
709 * Transfer data from/to the device data register by PIO.
710 *
711 * LOCKING:
712 * Inherited from caller.
713 *
714 * RETURNS:
715 * Bytes consumed.
716 */
Tejun Heo9363c382008-04-07 22:47:16 +0900717unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
718 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900719{
720 struct ata_port *ap = dev->link->ap;
721 void __iomem *data_addr = ap->ioaddr.data_addr;
722 unsigned int words = buflen >> 1;
723
724 /* Transfer multiple of 2 bytes */
725 if (rw == READ)
726 ioread16_rep(data_addr, buf, words);
727 else
728 iowrite16_rep(data_addr, buf, words);
729
730 /* Transfer trailing 1 byte, if any. */
731 if (unlikely(buflen & 0x01)) {
732 __le16 align_buf[1] = { 0 };
733 unsigned char *trailing_buf = buf + buflen - 1;
734
735 if (rw == READ) {
736 align_buf[0] = cpu_to_le16(ioread16(data_addr));
737 memcpy(trailing_buf, align_buf, 1);
738 } else {
739 memcpy(align_buf, trailing_buf, 1);
740 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
741 }
742 words++;
743 }
744
745 return words << 1;
746}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000747EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
Tejun Heo624d5c52008-03-25 22:16:41 +0900748
749/**
Alan Cox871af122009-01-05 14:16:39 +0000750 * ata_sff_data_xfer32 - Transfer data by PIO
751 * @dev: device to target
752 * @buf: data buffer
753 * @buflen: buffer length
754 * @rw: read/write
755 *
756 * Transfer data from/to the device data register by PIO using 32bit
757 * I/O operations.
758 *
759 * LOCKING:
760 * Inherited from caller.
761 *
762 * RETURNS:
763 * Bytes consumed.
764 */
765
766unsigned int ata_sff_data_xfer32(struct ata_device *dev, unsigned char *buf,
767 unsigned int buflen, int rw)
768{
769 struct ata_port *ap = dev->link->ap;
770 void __iomem *data_addr = ap->ioaddr.data_addr;
771 unsigned int words = buflen >> 2;
772 int slop = buflen & 3;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100773
774 if (!(ap->pflags & ATA_PFLAG_PIO32))
775 return ata_sff_data_xfer(dev, buf, buflen, rw);
Alan Cox871af122009-01-05 14:16:39 +0000776
777 /* Transfer multiple of 4 bytes */
778 if (rw == READ)
779 ioread32_rep(data_addr, buf, words);
780 else
781 iowrite32_rep(data_addr, buf, words);
782
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400783 /* Transfer trailing bytes, if any */
Alan Cox871af122009-01-05 14:16:39 +0000784 if (unlikely(slop)) {
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400785 unsigned char pad[4];
786
787 /* Point buf to the tail of buffer */
788 buf += buflen - slop;
789
790 /*
791 * Use io*_rep() accessors here as well to avoid pointlessly
792 * swapping bytes to and fro on the big endian machines...
793 */
Alan Cox871af122009-01-05 14:16:39 +0000794 if (rw == READ) {
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400795 if (slop < 3)
796 ioread16_rep(data_addr, pad, 1);
797 else
798 ioread32_rep(data_addr, pad, 1);
799 memcpy(buf, pad, slop);
Alan Cox871af122009-01-05 14:16:39 +0000800 } else {
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400801 memcpy(pad, buf, slop);
802 if (slop < 3)
803 iowrite16_rep(data_addr, pad, 1);
804 else
805 iowrite32_rep(data_addr, pad, 1);
Alan Cox871af122009-01-05 14:16:39 +0000806 }
Alan Cox871af122009-01-05 14:16:39 +0000807 }
Sergei Shtylyovd1b35252009-02-15 23:24:24 +0400808 return (buflen + 1) & ~1;
Alan Cox871af122009-01-05 14:16:39 +0000809}
810EXPORT_SYMBOL_GPL(ata_sff_data_xfer32);
811
812/**
Tejun Heo9363c382008-04-07 22:47:16 +0900813 * ata_sff_data_xfer_noirq - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900814 * @dev: device to target
815 * @buf: data buffer
816 * @buflen: buffer length
817 * @rw: read/write
818 *
819 * Transfer data from/to the device data register by PIO. Do the
820 * transfer with interrupts disabled.
821 *
822 * LOCKING:
823 * Inherited from caller.
824 *
825 * RETURNS:
826 * Bytes consumed.
827 */
Tejun Heo9363c382008-04-07 22:47:16 +0900828unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
829 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900830{
831 unsigned long flags;
832 unsigned int consumed;
833
834 local_irq_save(flags);
Tejun Heo9363c382008-04-07 22:47:16 +0900835 consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900836 local_irq_restore(flags);
837
838 return consumed;
839}
Alan Cox0fe40ff2009-01-05 14:16:13 +0000840EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
Tejun Heo624d5c52008-03-25 22:16:41 +0900841
842/**
843 * ata_pio_sector - Transfer a sector of data.
844 * @qc: Command on going
845 *
846 * Transfer qc->sect_size bytes of data from/to the ATA device.
847 *
848 * LOCKING:
849 * Inherited from caller.
850 */
851static void ata_pio_sector(struct ata_queued_cmd *qc)
852{
853 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
854 struct ata_port *ap = qc->ap;
855 struct page *page;
856 unsigned int offset;
857 unsigned char *buf;
858
859 if (qc->curbytes == qc->nbytes - qc->sect_size)
860 ap->hsm_task_state = HSM_ST_LAST;
861
862 page = sg_page(qc->cursg);
863 offset = qc->cursg->offset + qc->cursg_ofs;
864
865 /* get the current page and offset */
866 page = nth_page(page, (offset >> PAGE_SHIFT));
867 offset %= PAGE_SIZE;
868
869 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
870
871 if (PageHighMem(page)) {
872 unsigned long flags;
873
874 /* FIXME: use a bounce buffer */
875 local_irq_save(flags);
876 buf = kmap_atomic(page, KM_IRQ0);
877
878 /* do the actual data transfer */
Tejun Heo5682ed32008-04-07 22:47:16 +0900879 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
880 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900881
882 kunmap_atomic(buf, KM_IRQ0);
883 local_irq_restore(flags);
884 } else {
885 buf = page_address(page);
Tejun Heo5682ed32008-04-07 22:47:16 +0900886 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
887 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900888 }
889
890 qc->curbytes += qc->sect_size;
891 qc->cursg_ofs += qc->sect_size;
892
893 if (qc->cursg_ofs == qc->cursg->length) {
894 qc->cursg = sg_next(qc->cursg);
895 qc->cursg_ofs = 0;
896 }
897}
898
899/**
900 * ata_pio_sectors - Transfer one or many sectors.
901 * @qc: Command on going
902 *
903 * Transfer one or many sectors of data from/to the
904 * ATA device for the DRQ request.
905 *
906 * LOCKING:
907 * Inherited from caller.
908 */
909static void ata_pio_sectors(struct ata_queued_cmd *qc)
910{
911 if (is_multi_taskfile(&qc->tf)) {
912 /* READ/WRITE MULTIPLE */
913 unsigned int nsect;
914
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900915 WARN_ON_ONCE(qc->dev->multi_count == 0);
Tejun Heo624d5c52008-03-25 22:16:41 +0900916
917 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
918 qc->dev->multi_count);
919 while (nsect--)
920 ata_pio_sector(qc);
921 } else
922 ata_pio_sector(qc);
923
Alan Coxa57c1ba2008-05-29 22:10:58 +0100924 ata_sff_sync(qc->ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900925}
926
927/**
928 * atapi_send_cdb - Write CDB bytes to hardware
929 * @ap: Port to which ATAPI device is attached.
930 * @qc: Taskfile currently active
931 *
932 * When device has indicated its readiness to accept
933 * a CDB, this function is called. Send the CDB.
934 *
935 * LOCKING:
936 * caller.
937 */
938static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
939{
940 /* send SCSI cdb */
941 DPRINTK("send cdb\n");
Tejun Heoefcb3cf2009-01-09 19:19:14 +0900942 WARN_ON_ONCE(qc->dev->cdb_len < 12);
Tejun Heo624d5c52008-03-25 22:16:41 +0900943
Tejun Heo5682ed32008-04-07 22:47:16 +0900944 ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100945 ata_sff_sync(ap);
946 /* FIXME: If the CDB is for DMA do we need to do the transition delay
947 or is bmdma_start guaranteed to do it ? */
Tejun Heo624d5c52008-03-25 22:16:41 +0900948 switch (qc->tf.protocol) {
949 case ATAPI_PROT_PIO:
950 ap->hsm_task_state = HSM_ST;
951 break;
952 case ATAPI_PROT_NODATA:
953 ap->hsm_task_state = HSM_ST_LAST;
954 break;
955 case ATAPI_PROT_DMA:
956 ap->hsm_task_state = HSM_ST_LAST;
957 /* initiate bmdma */
958 ap->ops->bmdma_start(qc);
959 break;
960 }
961}
962
963/**
964 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
965 * @qc: Command on going
966 * @bytes: number of bytes
967 *
968 * Transfer Transfer data from/to the ATAPI device.
969 *
970 * LOCKING:
971 * Inherited from caller.
972 *
973 */
974static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
975{
976 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
977 struct ata_port *ap = qc->ap;
978 struct ata_device *dev = qc->dev;
979 struct ata_eh_info *ehi = &dev->link->eh_info;
980 struct scatterlist *sg;
981 struct page *page;
982 unsigned char *buf;
983 unsigned int offset, count, consumed;
984
985next_sg:
986 sg = qc->cursg;
987 if (unlikely(!sg)) {
988 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
989 "buf=%u cur=%u bytes=%u",
990 qc->nbytes, qc->curbytes, bytes);
991 return -1;
992 }
993
994 page = sg_page(sg);
995 offset = sg->offset + qc->cursg_ofs;
996
997 /* get the current page and offset */
998 page = nth_page(page, (offset >> PAGE_SHIFT));
999 offset %= PAGE_SIZE;
1000
1001 /* don't overrun current sg */
1002 count = min(sg->length - qc->cursg_ofs, bytes);
1003
1004 /* don't cross page boundaries */
1005 count = min(count, (unsigned int)PAGE_SIZE - offset);
1006
1007 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
1008
1009 if (PageHighMem(page)) {
1010 unsigned long flags;
1011
1012 /* FIXME: use bounce buffer */
1013 local_irq_save(flags);
1014 buf = kmap_atomic(page, KM_IRQ0);
1015
1016 /* do the actual data transfer */
Alan Cox0fe40ff2009-01-05 14:16:13 +00001017 consumed = ap->ops->sff_data_xfer(dev, buf + offset,
1018 count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +09001019
1020 kunmap_atomic(buf, KM_IRQ0);
1021 local_irq_restore(flags);
1022 } else {
1023 buf = page_address(page);
Alan Cox0fe40ff2009-01-05 14:16:13 +00001024 consumed = ap->ops->sff_data_xfer(dev, buf + offset,
1025 count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +09001026 }
1027
1028 bytes -= min(bytes, consumed);
1029 qc->curbytes += count;
1030 qc->cursg_ofs += count;
1031
1032 if (qc->cursg_ofs == sg->length) {
1033 qc->cursg = sg_next(qc->cursg);
1034 qc->cursg_ofs = 0;
1035 }
1036
Christian Borntraegera0f79f72009-01-13 10:38:36 +01001037 /*
1038 * There used to be a WARN_ON_ONCE(qc->cursg && count != consumed);
1039 * Unfortunately __atapi_pio_bytes doesn't know enough to do the WARN
1040 * check correctly as it doesn't know if it is the last request being
1041 * made. Somebody should implement a proper sanity check.
1042 */
Tejun Heo624d5c52008-03-25 22:16:41 +09001043 if (bytes)
1044 goto next_sg;
1045 return 0;
1046}
1047
1048/**
1049 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
1050 * @qc: Command on going
1051 *
1052 * Transfer Transfer data from/to the ATAPI device.
1053 *
1054 * LOCKING:
1055 * Inherited from caller.
1056 */
1057static void atapi_pio_bytes(struct ata_queued_cmd *qc)
1058{
1059 struct ata_port *ap = qc->ap;
1060 struct ata_device *dev = qc->dev;
1061 struct ata_eh_info *ehi = &dev->link->eh_info;
1062 unsigned int ireason, bc_lo, bc_hi, bytes;
1063 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
1064
1065 /* Abuse qc->result_tf for temp storage of intermediate TF
1066 * here to save some kernel stack usage.
1067 * For normal completion, qc->result_tf is not relevant. For
1068 * error, qc->result_tf is later overwritten by ata_qc_complete().
1069 * So, the correctness of qc->result_tf is not affected.
1070 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001071 ap->ops->sff_tf_read(ap, &qc->result_tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001072 ireason = qc->result_tf.nsect;
1073 bc_lo = qc->result_tf.lbam;
1074 bc_hi = qc->result_tf.lbah;
1075 bytes = (bc_hi << 8) | bc_lo;
1076
1077 /* shall be cleared to zero, indicating xfer of data */
1078 if (unlikely(ireason & (1 << 0)))
1079 goto atapi_check;
1080
1081 /* make sure transfer direction matches expected */
1082 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
1083 if (unlikely(do_write != i_write))
1084 goto atapi_check;
1085
1086 if (unlikely(!bytes))
1087 goto atapi_check;
1088
1089 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
1090
1091 if (unlikely(__atapi_pio_bytes(qc, bytes)))
1092 goto err_out;
Alan Coxa57c1ba2008-05-29 22:10:58 +01001093 ata_sff_sync(ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +09001094
1095 return;
1096
1097 atapi_check:
1098 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
1099 ireason, bytes);
1100 err_out:
1101 qc->err_mask |= AC_ERR_HSM;
1102 ap->hsm_task_state = HSM_ST_ERR;
1103}
1104
1105/**
1106 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
1107 * @ap: the target ata_port
1108 * @qc: qc on going
1109 *
1110 * RETURNS:
1111 * 1 if ok in workqueue, 0 otherwise.
1112 */
Alan Cox0fe40ff2009-01-05 14:16:13 +00001113static inline int ata_hsm_ok_in_wq(struct ata_port *ap,
1114 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001115{
1116 if (qc->tf.flags & ATA_TFLAG_POLLING)
1117 return 1;
1118
1119 if (ap->hsm_task_state == HSM_ST_FIRST) {
1120 if (qc->tf.protocol == ATA_PROT_PIO &&
Alan Cox0fe40ff2009-01-05 14:16:13 +00001121 (qc->tf.flags & ATA_TFLAG_WRITE))
Tejun Heo624d5c52008-03-25 22:16:41 +09001122 return 1;
1123
1124 if (ata_is_atapi(qc->tf.protocol) &&
Alan Cox0fe40ff2009-01-05 14:16:13 +00001125 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Tejun Heo624d5c52008-03-25 22:16:41 +09001126 return 1;
1127 }
1128
1129 return 0;
1130}
1131
1132/**
1133 * ata_hsm_qc_complete - finish a qc running on standard HSM
1134 * @qc: Command to complete
1135 * @in_wq: 1 if called from workqueue, 0 otherwise
1136 *
1137 * Finish @qc which is running on standard HSM.
1138 *
1139 * LOCKING:
1140 * If @in_wq is zero, spin_lock_irqsave(host lock).
1141 * Otherwise, none on entry and grabs host lock.
1142 */
1143static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
1144{
1145 struct ata_port *ap = qc->ap;
1146 unsigned long flags;
1147
1148 if (ap->ops->error_handler) {
1149 if (in_wq) {
1150 spin_lock_irqsave(ap->lock, flags);
1151
1152 /* EH might have kicked in while host lock is
1153 * released.
1154 */
1155 qc = ata_qc_from_tag(ap, qc->tag);
1156 if (qc) {
1157 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001158 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001159 ata_qc_complete(qc);
1160 } else
1161 ata_port_freeze(ap);
1162 }
1163
1164 spin_unlock_irqrestore(ap->lock, flags);
1165 } else {
1166 if (likely(!(qc->err_mask & AC_ERR_HSM)))
1167 ata_qc_complete(qc);
1168 else
1169 ata_port_freeze(ap);
1170 }
1171 } else {
1172 if (in_wq) {
1173 spin_lock_irqsave(ap->lock, flags);
Tejun Heo5682ed32008-04-07 22:47:16 +09001174 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001175 ata_qc_complete(qc);
1176 spin_unlock_irqrestore(ap->lock, flags);
1177 } else
1178 ata_qc_complete(qc);
1179 }
1180}
1181
1182/**
Tejun Heo9363c382008-04-07 22:47:16 +09001183 * ata_sff_hsm_move - move the HSM to the next state.
Tejun Heo624d5c52008-03-25 22:16:41 +09001184 * @ap: the target ata_port
1185 * @qc: qc on going
1186 * @status: current device status
1187 * @in_wq: 1 if called from workqueue, 0 otherwise
1188 *
1189 * RETURNS:
1190 * 1 when poll next status needed, 0 otherwise.
1191 */
Tejun Heo9363c382008-04-07 22:47:16 +09001192int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
1193 u8 status, int in_wq)
Tejun Heo624d5c52008-03-25 22:16:41 +09001194{
Tejun Heoa836d3e2008-06-28 01:39:43 +09001195 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo624d5c52008-03-25 22:16:41 +09001196 unsigned long flags = 0;
1197 int poll_next;
1198
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001199 WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001200
Tejun Heo9363c382008-04-07 22:47:16 +09001201 /* Make sure ata_sff_qc_issue() does not throw things
Tejun Heo624d5c52008-03-25 22:16:41 +09001202 * like DMA polling into the workqueue. Notice that
1203 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1204 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001205 WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc));
Tejun Heo624d5c52008-03-25 22:16:41 +09001206
1207fsm_start:
1208 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1209 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
1210
1211 switch (ap->hsm_task_state) {
1212 case HSM_ST_FIRST:
1213 /* Send first data block or PACKET CDB */
1214
1215 /* If polling, we will stay in the work queue after
1216 * sending the data. Otherwise, interrupt handler
1217 * takes over after sending the data.
1218 */
1219 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
1220
1221 /* check device status */
1222 if (unlikely((status & ATA_DRQ) == 0)) {
1223 /* handle BSY=0, DRQ=0 as error */
1224 if (likely(status & (ATA_ERR | ATA_DF)))
1225 /* device stops HSM for abort/error */
1226 qc->err_mask |= AC_ERR_DEV;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001227 else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001228 /* HSM violation. Let EH handle this */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001229 ata_ehi_push_desc(ehi,
1230 "ST_FIRST: !(DRQ|ERR|DF)");
Tejun Heo624d5c52008-03-25 22:16:41 +09001231 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001232 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001233
1234 ap->hsm_task_state = HSM_ST_ERR;
1235 goto fsm_start;
1236 }
1237
1238 /* Device should not ask for data transfer (DRQ=1)
1239 * when it finds something wrong.
1240 * We ignore DRQ here and stop the HSM by
1241 * changing hsm_task_state to HSM_ST_ERR and
1242 * let the EH abort the command or reset the device.
1243 */
1244 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1245 /* Some ATAPI tape drives forget to clear the ERR bit
1246 * when doing the next command (mostly request sense).
1247 * We ignore ERR here to workaround and proceed sending
1248 * the CDB.
1249 */
1250 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001251 ata_ehi_push_desc(ehi, "ST_FIRST: "
1252 "DRQ=1 with device error, "
1253 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001254 qc->err_mask |= AC_ERR_HSM;
1255 ap->hsm_task_state = HSM_ST_ERR;
1256 goto fsm_start;
1257 }
1258 }
1259
1260 /* Send the CDB (atapi) or the first data block (ata pio out).
1261 * During the state transition, interrupt handler shouldn't
1262 * be invoked before the data transfer is complete and
1263 * hsm_task_state is changed. Hence, the following locking.
1264 */
1265 if (in_wq)
1266 spin_lock_irqsave(ap->lock, flags);
1267
1268 if (qc->tf.protocol == ATA_PROT_PIO) {
1269 /* PIO data out protocol.
1270 * send first data block.
1271 */
1272
1273 /* ata_pio_sectors() might change the state
1274 * to HSM_ST_LAST. so, the state is changed here
1275 * before ata_pio_sectors().
1276 */
1277 ap->hsm_task_state = HSM_ST;
1278 ata_pio_sectors(qc);
1279 } else
1280 /* send CDB */
1281 atapi_send_cdb(ap, qc);
1282
1283 if (in_wq)
1284 spin_unlock_irqrestore(ap->lock, flags);
1285
1286 /* if polling, ata_pio_task() handles the rest.
1287 * otherwise, interrupt handler takes over from here.
1288 */
1289 break;
1290
1291 case HSM_ST:
1292 /* complete command or read/write the data register */
1293 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1294 /* ATAPI PIO protocol */
1295 if ((status & ATA_DRQ) == 0) {
1296 /* No more data to transfer or device error.
1297 * Device error will be tagged in HSM_ST_LAST.
1298 */
1299 ap->hsm_task_state = HSM_ST_LAST;
1300 goto fsm_start;
1301 }
1302
1303 /* Device should not ask for data transfer (DRQ=1)
1304 * when it finds something wrong.
1305 * We ignore DRQ here and stop the HSM by
1306 * changing hsm_task_state to HSM_ST_ERR and
1307 * let the EH abort the command or reset the device.
1308 */
1309 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001310 ata_ehi_push_desc(ehi, "ST-ATAPI: "
1311 "DRQ=1 with device error, "
1312 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001313 qc->err_mask |= AC_ERR_HSM;
1314 ap->hsm_task_state = HSM_ST_ERR;
1315 goto fsm_start;
1316 }
1317
1318 atapi_pio_bytes(qc);
1319
1320 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1321 /* bad ireason reported by device */
1322 goto fsm_start;
1323
1324 } else {
1325 /* ATA PIO protocol */
1326 if (unlikely((status & ATA_DRQ) == 0)) {
1327 /* handle BSY=0, DRQ=0 as error */
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001328 if (likely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo624d5c52008-03-25 22:16:41 +09001329 /* device stops HSM for abort/error */
1330 qc->err_mask |= AC_ERR_DEV;
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001331
1332 /* If diagnostic failed and this is
1333 * IDENTIFY, it's likely a phantom
1334 * device. Mark hint.
1335 */
1336 if (qc->dev->horkage &
1337 ATA_HORKAGE_DIAGNOSTIC)
1338 qc->err_mask |=
1339 AC_ERR_NODEV_HINT;
1340 } else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001341 /* HSM violation. Let EH handle this.
1342 * Phantom devices also trigger this
1343 * condition. Mark hint.
1344 */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001345 ata_ehi_push_desc(ehi, "ST-ATA: "
Tejun Heo80ee6f52009-01-23 14:12:59 +09001346 "DRQ=0 without device error, "
Tejun Heoa836d3e2008-06-28 01:39:43 +09001347 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001348 qc->err_mask |= AC_ERR_HSM |
1349 AC_ERR_NODEV_HINT;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001350 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001351
1352 ap->hsm_task_state = HSM_ST_ERR;
1353 goto fsm_start;
1354 }
1355
1356 /* For PIO reads, some devices may ask for
1357 * data transfer (DRQ=1) alone with ERR=1.
1358 * We respect DRQ here and transfer one
1359 * block of junk data before changing the
1360 * hsm_task_state to HSM_ST_ERR.
1361 *
1362 * For PIO writes, ERR=1 DRQ=1 doesn't make
1363 * sense since the data block has been
1364 * transferred to the device.
1365 */
1366 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1367 /* data might be corrputed */
1368 qc->err_mask |= AC_ERR_DEV;
1369
1370 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1371 ata_pio_sectors(qc);
1372 status = ata_wait_idle(ap);
1373 }
1374
Tejun Heoa836d3e2008-06-28 01:39:43 +09001375 if (status & (ATA_BUSY | ATA_DRQ)) {
1376 ata_ehi_push_desc(ehi, "ST-ATA: "
1377 "BUSY|DRQ persists on ERR|DF, "
1378 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001379 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001380 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001381
Tejun Heob9199302009-01-25 10:26:00 +09001382 /* There are oddball controllers with
1383 * status register stuck at 0x7f and
1384 * lbal/m/h at zero which makes it
1385 * pass all other presence detection
1386 * mechanisms we have. Set NODEV_HINT
1387 * for it. Kernel bz#7241.
1388 */
1389 if (status == 0x7f)
1390 qc->err_mask |= AC_ERR_NODEV_HINT;
1391
Tejun Heo624d5c52008-03-25 22:16:41 +09001392 /* ata_pio_sectors() might change the
1393 * state to HSM_ST_LAST. so, the state
1394 * is changed after ata_pio_sectors().
1395 */
1396 ap->hsm_task_state = HSM_ST_ERR;
1397 goto fsm_start;
1398 }
1399
1400 ata_pio_sectors(qc);
1401
1402 if (ap->hsm_task_state == HSM_ST_LAST &&
1403 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1404 /* all data read */
1405 status = ata_wait_idle(ap);
1406 goto fsm_start;
1407 }
1408 }
1409
1410 poll_next = 1;
1411 break;
1412
1413 case HSM_ST_LAST:
1414 if (unlikely(!ata_ok(status))) {
1415 qc->err_mask |= __ac_err_mask(status);
1416 ap->hsm_task_state = HSM_ST_ERR;
1417 goto fsm_start;
1418 }
1419
1420 /* no more data to transfer */
1421 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1422 ap->print_id, qc->dev->devno, status);
1423
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001424 WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
Tejun Heo624d5c52008-03-25 22:16:41 +09001425
1426 ap->hsm_task_state = HSM_ST_IDLE;
1427
1428 /* complete taskfile transaction */
1429 ata_hsm_qc_complete(qc, in_wq);
1430
1431 poll_next = 0;
1432 break;
1433
1434 case HSM_ST_ERR:
Tejun Heo624d5c52008-03-25 22:16:41 +09001435 ap->hsm_task_state = HSM_ST_IDLE;
1436
1437 /* complete taskfile transaction */
1438 ata_hsm_qc_complete(qc, in_wq);
1439
1440 poll_next = 0;
1441 break;
1442 default:
1443 poll_next = 0;
1444 BUG();
1445 }
1446
1447 return poll_next;
1448}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001449EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
Tejun Heo624d5c52008-03-25 22:16:41 +09001450
1451void ata_pio_task(struct work_struct *work)
1452{
1453 struct ata_port *ap =
1454 container_of(work, struct ata_port, port_task.work);
1455 struct ata_queued_cmd *qc = ap->port_task_data;
1456 u8 status;
1457 int poll_next;
1458
1459fsm_start:
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001460 WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE);
Tejun Heo624d5c52008-03-25 22:16:41 +09001461
1462 /*
1463 * This is purely heuristic. This is a fast path.
1464 * Sometimes when we enter, BSY will be cleared in
1465 * a chk-status or two. If not, the drive is probably seeking
1466 * or something. Snooze for a couple msecs, then
1467 * chk-status again. If still busy, queue delayed work.
1468 */
Tejun Heo9363c382008-04-07 22:47:16 +09001469 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
Tejun Heo624d5c52008-03-25 22:16:41 +09001470 if (status & ATA_BUSY) {
1471 msleep(2);
Tejun Heo9363c382008-04-07 22:47:16 +09001472 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
Tejun Heo624d5c52008-03-25 22:16:41 +09001473 if (status & ATA_BUSY) {
1474 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
1475 return;
1476 }
1477 }
1478
1479 /* move the HSM */
Tejun Heo9363c382008-04-07 22:47:16 +09001480 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001481
1482 /* another command or interrupt handler
1483 * may be running at this point.
1484 */
1485 if (poll_next)
1486 goto fsm_start;
1487}
1488
1489/**
Tejun Heo9363c382008-04-07 22:47:16 +09001490 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
Tejun Heo624d5c52008-03-25 22:16:41 +09001491 * @qc: command to issue to device
1492 *
1493 * Using various libata functions and hooks, this function
1494 * starts an ATA command. ATA commands are grouped into
1495 * classes called "protocols", and issuing each type of protocol
1496 * is slightly different.
1497 *
1498 * May be used as the qc_issue() entry in ata_port_operations.
1499 *
1500 * LOCKING:
1501 * spin_lock_irqsave(host lock)
1502 *
1503 * RETURNS:
1504 * Zero on success, AC_ERR_* mask on failure
1505 */
Tejun Heo9363c382008-04-07 22:47:16 +09001506unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001507{
1508 struct ata_port *ap = qc->ap;
1509
1510 /* Use polling pio if the LLD doesn't handle
1511 * interrupt driven pio and atapi CDB interrupt.
1512 */
1513 if (ap->flags & ATA_FLAG_PIO_POLLING) {
1514 switch (qc->tf.protocol) {
1515 case ATA_PROT_PIO:
1516 case ATA_PROT_NODATA:
1517 case ATAPI_PROT_PIO:
1518 case ATAPI_PROT_NODATA:
1519 qc->tf.flags |= ATA_TFLAG_POLLING;
1520 break;
1521 case ATAPI_PROT_DMA:
1522 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
1523 /* see ata_dma_blacklisted() */
1524 BUG();
1525 break;
1526 default:
1527 break;
1528 }
1529 }
1530
1531 /* select the device */
1532 ata_dev_select(ap, qc->dev->devno, 1, 0);
1533
1534 /* start the command */
1535 switch (qc->tf.protocol) {
1536 case ATA_PROT_NODATA:
1537 if (qc->tf.flags & ATA_TFLAG_POLLING)
1538 ata_qc_set_polling(qc);
1539
1540 ata_tf_to_host(ap, &qc->tf);
1541 ap->hsm_task_state = HSM_ST_LAST;
1542
1543 if (qc->tf.flags & ATA_TFLAG_POLLING)
1544 ata_pio_queue_task(ap, qc, 0);
1545
1546 break;
1547
1548 case ATA_PROT_DMA:
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001549 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
Tejun Heo624d5c52008-03-25 22:16:41 +09001550
Tejun Heo5682ed32008-04-07 22:47:16 +09001551 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001552 ap->ops->bmdma_setup(qc); /* set up bmdma */
1553 ap->ops->bmdma_start(qc); /* initiate bmdma */
1554 ap->hsm_task_state = HSM_ST_LAST;
1555 break;
1556
1557 case ATA_PROT_PIO:
1558 if (qc->tf.flags & ATA_TFLAG_POLLING)
1559 ata_qc_set_polling(qc);
1560
1561 ata_tf_to_host(ap, &qc->tf);
1562
1563 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1564 /* PIO data out protocol */
1565 ap->hsm_task_state = HSM_ST_FIRST;
1566 ata_pio_queue_task(ap, qc, 0);
1567
1568 /* always send first data block using
1569 * the ata_pio_task() codepath.
1570 */
1571 } else {
1572 /* PIO data in protocol */
1573 ap->hsm_task_state = HSM_ST;
1574
1575 if (qc->tf.flags & ATA_TFLAG_POLLING)
1576 ata_pio_queue_task(ap, qc, 0);
1577
1578 /* if polling, ata_pio_task() handles the rest.
1579 * otherwise, interrupt handler takes over from here.
1580 */
1581 }
1582
1583 break;
1584
1585 case ATAPI_PROT_PIO:
1586 case ATAPI_PROT_NODATA:
1587 if (qc->tf.flags & ATA_TFLAG_POLLING)
1588 ata_qc_set_polling(qc);
1589
1590 ata_tf_to_host(ap, &qc->tf);
1591
1592 ap->hsm_task_state = HSM_ST_FIRST;
1593
1594 /* send cdb by polling if no cdb interrupt */
1595 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1596 (qc->tf.flags & ATA_TFLAG_POLLING))
1597 ata_pio_queue_task(ap, qc, 0);
1598 break;
1599
1600 case ATAPI_PROT_DMA:
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001601 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
Tejun Heo624d5c52008-03-25 22:16:41 +09001602
Tejun Heo5682ed32008-04-07 22:47:16 +09001603 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001604 ap->ops->bmdma_setup(qc); /* set up bmdma */
1605 ap->hsm_task_state = HSM_ST_FIRST;
1606
1607 /* send cdb by polling if no cdb interrupt */
1608 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1609 ata_pio_queue_task(ap, qc, 0);
1610 break;
1611
1612 default:
Tejun Heoefcb3cf2009-01-09 19:19:14 +09001613 WARN_ON_ONCE(1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001614 return AC_ERR_SYSTEM;
1615 }
1616
1617 return 0;
1618}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001619EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
Tejun Heo624d5c52008-03-25 22:16:41 +09001620
1621/**
Tejun Heo22183bf2008-04-07 22:47:20 +09001622 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1623 * @qc: qc to fill result TF for
1624 *
1625 * @qc is finished and result TF needs to be filled. Fill it
1626 * using ->sff_tf_read.
1627 *
1628 * LOCKING:
1629 * spin_lock_irqsave(host lock)
1630 *
1631 * RETURNS:
1632 * true indicating that result TF is successfully filled.
1633 */
1634bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
1635{
1636 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
1637 return true;
1638}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001639EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
Tejun Heo22183bf2008-04-07 22:47:20 +09001640
1641/**
Tejun Heo9363c382008-04-07 22:47:16 +09001642 * ata_sff_host_intr - Handle host interrupt for given (port, task)
Tejun Heo624d5c52008-03-25 22:16:41 +09001643 * @ap: Port on which interrupt arrived (possibly...)
1644 * @qc: Taskfile currently active in engine
1645 *
1646 * Handle host interrupt for given queued command. Currently,
1647 * only DMA interrupts are handled. All other commands are
1648 * handled via polling with interrupts disabled (nIEN bit).
1649 *
1650 * LOCKING:
1651 * spin_lock_irqsave(host lock)
1652 *
1653 * RETURNS:
1654 * One if interrupt was handled, zero if not (shared irq).
1655 */
Alan Coxc96f1732009-03-24 10:23:46 +00001656unsigned int ata_sff_host_intr(struct ata_port *ap,
Tejun Heo9363c382008-04-07 22:47:16 +09001657 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001658{
1659 struct ata_eh_info *ehi = &ap->link.eh_info;
1660 u8 status, host_stat = 0;
1661
1662 VPRINTK("ata%u: protocol %d task_state %d\n",
1663 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
1664
1665 /* Check whether we are expecting interrupt in this state */
1666 switch (ap->hsm_task_state) {
1667 case HSM_ST_FIRST:
1668 /* Some pre-ATAPI-4 devices assert INTRQ
1669 * at this state when ready to receive CDB.
1670 */
1671
1672 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1673 * The flag was turned on only for atapi devices. No
1674 * need to check ata_is_atapi(qc->tf.protocol) again.
1675 */
1676 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1677 goto idle_irq;
1678 break;
1679 case HSM_ST_LAST:
1680 if (qc->tf.protocol == ATA_PROT_DMA ||
1681 qc->tf.protocol == ATAPI_PROT_DMA) {
1682 /* check status of DMA engine */
1683 host_stat = ap->ops->bmdma_status(ap);
1684 VPRINTK("ata%u: host_stat 0x%X\n",
1685 ap->print_id, host_stat);
1686
1687 /* if it's not our irq... */
1688 if (!(host_stat & ATA_DMA_INTR))
1689 goto idle_irq;
1690
1691 /* before we do anything else, clear DMA-Start bit */
1692 ap->ops->bmdma_stop(qc);
1693
1694 if (unlikely(host_stat & ATA_DMA_ERR)) {
1695 /* error when transfering data to/from memory */
1696 qc->err_mask |= AC_ERR_HOST_BUS;
1697 ap->hsm_task_state = HSM_ST_ERR;
1698 }
1699 }
1700 break;
1701 case HSM_ST:
1702 break;
1703 default:
1704 goto idle_irq;
1705 }
1706
Tejun Heo624d5c52008-03-25 22:16:41 +09001707
Alan Coxa57c1ba2008-05-29 22:10:58 +01001708 /* check main status, clearing INTRQ if needed */
1709 status = ata_sff_irq_status(ap);
1710 if (status & ATA_BUSY)
Tejun Heo624d5c52008-03-25 22:16:41 +09001711 goto idle_irq;
1712
1713 /* ack bmdma irq events */
Tejun Heo5682ed32008-04-07 22:47:16 +09001714 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001715
Tejun Heo9363c382008-04-07 22:47:16 +09001716 ata_sff_hsm_move(ap, qc, status, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001717
1718 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
1719 qc->tf.protocol == ATAPI_PROT_DMA))
1720 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
1721
1722 return 1; /* irq handled */
1723
1724idle_irq:
1725 ap->stats.idle_irq++;
1726
1727#ifdef ATA_IRQ_TRAP
1728 if ((ap->stats.idle_irq % 1000) == 0) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001729 ap->ops->sff_check_status(ap);
1730 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001731 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
1732 return 1;
1733 }
1734#endif
1735 return 0; /* irq not handled */
1736}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001737EXPORT_SYMBOL_GPL(ata_sff_host_intr);
Tejun Heo624d5c52008-03-25 22:16:41 +09001738
1739/**
Tejun Heo9363c382008-04-07 22:47:16 +09001740 * ata_sff_interrupt - Default ATA host interrupt handler
Tejun Heo624d5c52008-03-25 22:16:41 +09001741 * @irq: irq line (unused)
1742 * @dev_instance: pointer to our ata_host information structure
1743 *
1744 * Default interrupt handler for PCI IDE devices. Calls
Tejun Heo9363c382008-04-07 22:47:16 +09001745 * ata_sff_host_intr() for each port that is not disabled.
Tejun Heo624d5c52008-03-25 22:16:41 +09001746 *
1747 * LOCKING:
1748 * Obtains host lock during operation.
1749 *
1750 * RETURNS:
1751 * IRQ_NONE or IRQ_HANDLED.
1752 */
Tejun Heo9363c382008-04-07 22:47:16 +09001753irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
Tejun Heo624d5c52008-03-25 22:16:41 +09001754{
1755 struct ata_host *host = dev_instance;
1756 unsigned int i;
1757 unsigned int handled = 0;
1758 unsigned long flags;
1759
1760 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1761 spin_lock_irqsave(&host->lock, flags);
1762
1763 for (i = 0; i < host->n_ports; i++) {
1764 struct ata_port *ap;
1765
1766 ap = host->ports[i];
1767 if (ap &&
1768 !(ap->flags & ATA_FLAG_DISABLED)) {
1769 struct ata_queued_cmd *qc;
1770
1771 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1772 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
1773 (qc->flags & ATA_QCFLAG_ACTIVE))
Tejun Heo9363c382008-04-07 22:47:16 +09001774 handled |= ata_sff_host_intr(ap, qc);
Tejun Heo624d5c52008-03-25 22:16:41 +09001775 }
1776 }
1777
1778 spin_unlock_irqrestore(&host->lock, flags);
1779
1780 return IRQ_RETVAL(handled);
1781}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001782EXPORT_SYMBOL_GPL(ata_sff_interrupt);
Tejun Heo624d5c52008-03-25 22:16:41 +09001783
1784/**
Alan Coxc96f1732009-03-24 10:23:46 +00001785 * ata_sff_lost_interrupt - Check for an apparent lost interrupt
1786 * @ap: port that appears to have timed out
1787 *
1788 * Called from the libata error handlers when the core code suspects
1789 * an interrupt has been lost. If it has complete anything we can and
1790 * then return. Interface must support altstatus for this faster
1791 * recovery to occur.
1792 *
1793 * Locking:
1794 * Caller holds host lock
1795 */
1796
1797void ata_sff_lost_interrupt(struct ata_port *ap)
1798{
1799 u8 status;
1800 struct ata_queued_cmd *qc;
1801
1802 /* Only one outstanding command per SFF channel */
1803 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1804 /* Check we have a live one.. */
1805 if (qc == NULL || !(qc->flags & ATA_QCFLAG_ACTIVE))
1806 return;
1807 /* We cannot lose an interrupt on a polled command */
1808 if (qc->tf.flags & ATA_TFLAG_POLLING)
1809 return;
1810 /* See if the controller thinks it is still busy - if so the command
1811 isn't a lost IRQ but is still in progress */
1812 status = ata_sff_altstatus(ap);
1813 if (status & ATA_BUSY)
1814 return;
1815
1816 /* There was a command running, we are no longer busy and we have
1817 no interrupt. */
1818 ata_port_printk(ap, KERN_WARNING, "lost interrupt (Status 0x%x)\n",
1819 status);
1820 /* Run the host interrupt logic as if the interrupt had not been
1821 lost */
1822 ata_sff_host_intr(ap, qc);
1823}
1824EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt);
1825
1826/**
Tejun Heo9363c382008-04-07 22:47:16 +09001827 * ata_sff_freeze - Freeze SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001828 * @ap: port to freeze
1829 *
1830 * Freeze BMDMA controller port.
1831 *
1832 * LOCKING:
1833 * Inherited from caller.
1834 */
Tejun Heo9363c382008-04-07 22:47:16 +09001835void ata_sff_freeze(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001836{
1837 struct ata_ioports *ioaddr = &ap->ioaddr;
1838
1839 ap->ctl |= ATA_NIEN;
1840 ap->last_ctl = ap->ctl;
1841
Tejun Heof659f0e42008-03-06 13:12:54 +09001842 if (ioaddr->ctl_addr)
1843 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001844
1845 /* Under certain circumstances, some controllers raise IRQ on
1846 * ATA_NIEN manipulation. Also, many controllers fail to mask
1847 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1848 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001849 ap->ops->sff_check_status(ap);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001850
Tejun Heo5682ed32008-04-07 22:47:16 +09001851 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001852}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001853EXPORT_SYMBOL_GPL(ata_sff_freeze);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001854
1855/**
Tejun Heo9363c382008-04-07 22:47:16 +09001856 * ata_sff_thaw - Thaw SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001857 * @ap: port to thaw
1858 *
Tejun Heo9363c382008-04-07 22:47:16 +09001859 * Thaw SFF controller port.
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001860 *
1861 * LOCKING:
1862 * Inherited from caller.
1863 */
Tejun Heo9363c382008-04-07 22:47:16 +09001864void ata_sff_thaw(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001865{
1866 /* clear & re-enable interrupts */
Tejun Heo5682ed32008-04-07 22:47:16 +09001867 ap->ops->sff_check_status(ap);
1868 ap->ops->sff_irq_clear(ap);
1869 ap->ops->sff_irq_on(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001870}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001871EXPORT_SYMBOL_GPL(ata_sff_thaw);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001872
1873/**
Tejun Heo0aa11132008-04-07 22:47:18 +09001874 * ata_sff_prereset - prepare SFF link for reset
1875 * @link: SFF link to be reset
1876 * @deadline: deadline jiffies for the operation
1877 *
1878 * SFF link @link is about to be reset. Initialize it. It first
1879 * calls ata_std_prereset() and wait for !BSY if the port is
1880 * being softreset.
1881 *
1882 * LOCKING:
1883 * Kernel thread context (may sleep)
1884 *
1885 * RETURNS:
1886 * 0 on success, -errno otherwise.
1887 */
1888int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
1889{
Tejun Heo0aa11132008-04-07 22:47:18 +09001890 struct ata_eh_context *ehc = &link->eh_context;
1891 int rc;
1892
1893 rc = ata_std_prereset(link, deadline);
1894 if (rc)
1895 return rc;
1896
1897 /* if we're about to do hardreset, nothing more to do */
1898 if (ehc->i.action & ATA_EH_HARDRESET)
1899 return 0;
1900
1901 /* wait for !BSY if we don't know that no device is attached */
1902 if (!ata_link_offline(link)) {
Tejun Heo705e76b2008-04-07 22:47:19 +09001903 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo0aa11132008-04-07 22:47:18 +09001904 if (rc && rc != -ENODEV) {
1905 ata_link_printk(link, KERN_WARNING, "device not ready "
1906 "(errno=%d), forcing hardreset\n", rc);
1907 ehc->i.action |= ATA_EH_HARDRESET;
1908 }
1909 }
1910
1911 return 0;
1912}
Alan Cox0fe40ff2009-01-05 14:16:13 +00001913EXPORT_SYMBOL_GPL(ata_sff_prereset);
Tejun Heo0aa11132008-04-07 22:47:18 +09001914
1915/**
Tejun Heo624d5c52008-03-25 22:16:41 +09001916 * ata_devchk - PATA device presence detection
1917 * @ap: ATA channel to examine
1918 * @device: Device to examine (starting at zero)
1919 *
1920 * This technique was originally described in
1921 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1922 * later found its way into the ATA/ATAPI spec.
1923 *
1924 * Write a pattern to the ATA shadow registers,
1925 * and if a device is present, it will respond by
1926 * correctly storing and echoing back the
1927 * ATA shadow register contents.
1928 *
1929 * LOCKING:
1930 * caller.
1931 */
1932static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1933{
1934 struct ata_ioports *ioaddr = &ap->ioaddr;
1935 u8 nsect, lbal;
1936
Tejun Heo5682ed32008-04-07 22:47:16 +09001937 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +09001938
1939 iowrite8(0x55, ioaddr->nsect_addr);
1940 iowrite8(0xaa, ioaddr->lbal_addr);
1941
1942 iowrite8(0xaa, ioaddr->nsect_addr);
1943 iowrite8(0x55, ioaddr->lbal_addr);
1944
1945 iowrite8(0x55, ioaddr->nsect_addr);
1946 iowrite8(0xaa, ioaddr->lbal_addr);
1947
1948 nsect = ioread8(ioaddr->nsect_addr);
1949 lbal = ioread8(ioaddr->lbal_addr);
1950
1951 if ((nsect == 0x55) && (lbal == 0xaa))
1952 return 1; /* we found a device */
1953
1954 return 0; /* nothing found */
1955}
1956
1957/**
Tejun Heo9363c382008-04-07 22:47:16 +09001958 * ata_sff_dev_classify - Parse returned ATA device signature
Tejun Heo624d5c52008-03-25 22:16:41 +09001959 * @dev: ATA device to classify (starting at zero)
1960 * @present: device seems present
1961 * @r_err: Value of error register on completion
1962 *
1963 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1964 * an ATA/ATAPI-defined set of values is placed in the ATA
1965 * shadow registers, indicating the results of device detection
1966 * and diagnostics.
1967 *
1968 * Select the ATA device, and read the values from the ATA shadow
1969 * registers. Then parse according to the Error register value,
1970 * and the spec-defined values examined by ata_dev_classify().
1971 *
1972 * LOCKING:
1973 * caller.
1974 *
1975 * RETURNS:
1976 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1977 */
Tejun Heo9363c382008-04-07 22:47:16 +09001978unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
Tejun Heo624d5c52008-03-25 22:16:41 +09001979 u8 *r_err)
1980{
1981 struct ata_port *ap = dev->link->ap;
1982 struct ata_taskfile tf;
1983 unsigned int class;
1984 u8 err;
1985
Tejun Heo5682ed32008-04-07 22:47:16 +09001986 ap->ops->sff_dev_select(ap, dev->devno);
Tejun Heo624d5c52008-03-25 22:16:41 +09001987
1988 memset(&tf, 0, sizeof(tf));
1989
Tejun Heo5682ed32008-04-07 22:47:16 +09001990 ap->ops->sff_tf_read(ap, &tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001991 err = tf.feature;
1992 if (r_err)
1993 *r_err = err;
1994
1995 /* see if device passed diags: continue and warn later */
1996 if (err == 0)
1997 /* diagnostic fail : do nothing _YET_ */
1998 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1999 else if (err == 1)
2000 /* do nothing */ ;
2001 else if ((dev->devno == 0) && (err == 0x81))
2002 /* do nothing */ ;
2003 else
2004 return ATA_DEV_NONE;
2005
2006 /* determine if device is ATA or ATAPI */
2007 class = ata_dev_classify(&tf);
2008
2009 if (class == ATA_DEV_UNKNOWN) {
2010 /* If the device failed diagnostic, it's likely to
2011 * have reported incorrect device signature too.
2012 * Assume ATA device if the device seems present but
2013 * device signature is invalid with diagnostic
2014 * failure.
2015 */
2016 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
2017 class = ATA_DEV_ATA;
2018 else
2019 class = ATA_DEV_NONE;
Tejun Heo5682ed32008-04-07 22:47:16 +09002020 } else if ((class == ATA_DEV_ATA) &&
2021 (ap->ops->sff_check_status(ap) == 0))
Tejun Heo624d5c52008-03-25 22:16:41 +09002022 class = ATA_DEV_NONE;
2023
2024 return class;
2025}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002026EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
Tejun Heo624d5c52008-03-25 22:16:41 +09002027
Tejun Heo705e76b2008-04-07 22:47:19 +09002028/**
2029 * ata_sff_wait_after_reset - wait for devices to become ready after reset
2030 * @link: SFF link which is just reset
2031 * @devmask: mask of present devices
2032 * @deadline: deadline jiffies for the operation
2033 *
2034 * Wait devices attached to SFF @link to become ready after
2035 * reset. It contains preceding 150ms wait to avoid accessing TF
2036 * status register too early.
2037 *
2038 * LOCKING:
2039 * Kernel thread context (may sleep).
2040 *
2041 * RETURNS:
2042 * 0 on success, -ENODEV if some or all of devices in @devmask
2043 * don't seem to exist. -errno on other errors.
2044 */
2045int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
2046 unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +09002047{
Tejun Heo705e76b2008-04-07 22:47:19 +09002048 struct ata_port *ap = link->ap;
Tejun Heo624d5c52008-03-25 22:16:41 +09002049 struct ata_ioports *ioaddr = &ap->ioaddr;
2050 unsigned int dev0 = devmask & (1 << 0);
2051 unsigned int dev1 = devmask & (1 << 1);
2052 int rc, ret = 0;
2053
Tejun Heo341c2c92008-05-20 02:17:51 +09002054 msleep(ATA_WAIT_AFTER_RESET);
Tejun Heo705e76b2008-04-07 22:47:19 +09002055
2056 /* always check readiness of the master device */
2057 rc = ata_sff_wait_ready(link, deadline);
2058 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
2059 * and TF status is 0xff, bail out on it too.
Tejun Heo624d5c52008-03-25 22:16:41 +09002060 */
Tejun Heo705e76b2008-04-07 22:47:19 +09002061 if (rc)
2062 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09002063
2064 /* if device 1 was found in ata_devchk, wait for register
2065 * access briefly, then wait for BSY to clear.
2066 */
2067 if (dev1) {
2068 int i;
2069
Tejun Heo5682ed32008-04-07 22:47:16 +09002070 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002071
2072 /* Wait for register access. Some ATAPI devices fail
2073 * to set nsect/lbal after reset, so don't waste too
2074 * much time on it. We're gonna wait for !BSY anyway.
2075 */
2076 for (i = 0; i < 2; i++) {
2077 u8 nsect, lbal;
2078
2079 nsect = ioread8(ioaddr->nsect_addr);
2080 lbal = ioread8(ioaddr->lbal_addr);
2081 if ((nsect == 1) && (lbal == 1))
2082 break;
2083 msleep(50); /* give drive a breather */
2084 }
2085
Tejun Heo705e76b2008-04-07 22:47:19 +09002086 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09002087 if (rc) {
2088 if (rc != -ENODEV)
2089 return rc;
2090 ret = rc;
2091 }
2092 }
2093
2094 /* is all this really necessary? */
Tejun Heo5682ed32008-04-07 22:47:16 +09002095 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002096 if (dev1)
Tejun Heo5682ed32008-04-07 22:47:16 +09002097 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002098 if (dev0)
Tejun Heo5682ed32008-04-07 22:47:16 +09002099 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002100
2101 return ret;
2102}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002103EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002104
Tejun Heo624d5c52008-03-25 22:16:41 +09002105static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
2106 unsigned long deadline)
2107{
2108 struct ata_ioports *ioaddr = &ap->ioaddr;
2109
2110 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
2111
2112 /* software reset. causes dev0 to be selected */
2113 iowrite8(ap->ctl, ioaddr->ctl_addr);
2114 udelay(20); /* FIXME: flush */
2115 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2116 udelay(20); /* FIXME: flush */
2117 iowrite8(ap->ctl, ioaddr->ctl_addr);
Stuart MENEFYe3e43852009-03-10 11:38:13 +00002118 ap->last_ctl = ap->ctl;
Tejun Heo624d5c52008-03-25 22:16:41 +09002119
Tejun Heo705e76b2008-04-07 22:47:19 +09002120 /* wait the port to become ready */
2121 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09002122}
2123
2124/**
Tejun Heo9363c382008-04-07 22:47:16 +09002125 * ata_sff_softreset - reset host port via ATA SRST
Tejun Heo624d5c52008-03-25 22:16:41 +09002126 * @link: ATA link to reset
2127 * @classes: resulting classes of attached devices
2128 * @deadline: deadline jiffies for the operation
2129 *
2130 * Reset host port using ATA SRST.
2131 *
2132 * LOCKING:
2133 * Kernel thread context (may sleep)
2134 *
2135 * RETURNS:
2136 * 0 on success, -errno otherwise.
2137 */
Tejun Heo9363c382008-04-07 22:47:16 +09002138int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heo624d5c52008-03-25 22:16:41 +09002139 unsigned long deadline)
2140{
2141 struct ata_port *ap = link->ap;
2142 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2143 unsigned int devmask = 0;
2144 int rc;
2145 u8 err;
2146
2147 DPRINTK("ENTER\n");
2148
Tejun Heo624d5c52008-03-25 22:16:41 +09002149 /* determine if device 0/1 are present */
2150 if (ata_devchk(ap, 0))
2151 devmask |= (1 << 0);
2152 if (slave_possible && ata_devchk(ap, 1))
2153 devmask |= (1 << 1);
2154
2155 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002156 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002157
2158 /* issue bus reset */
2159 DPRINTK("about to softreset, devmask=%x\n", devmask);
2160 rc = ata_bus_softreset(ap, devmask, deadline);
2161 /* if link is occupied, -ENODEV too is an error */
2162 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
2163 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
2164 return rc;
2165 }
2166
2167 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo9363c382008-04-07 22:47:16 +09002168 classes[0] = ata_sff_dev_classify(&link->device[0],
Tejun Heo624d5c52008-03-25 22:16:41 +09002169 devmask & (1 << 0), &err);
2170 if (slave_possible && err != 0x81)
Tejun Heo9363c382008-04-07 22:47:16 +09002171 classes[1] = ata_sff_dev_classify(&link->device[1],
Tejun Heo624d5c52008-03-25 22:16:41 +09002172 devmask & (1 << 1), &err);
2173
Tejun Heo624d5c52008-03-25 22:16:41 +09002174 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2175 return 0;
2176}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002177EXPORT_SYMBOL_GPL(ata_sff_softreset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002178
2179/**
Tejun Heo9363c382008-04-07 22:47:16 +09002180 * sata_sff_hardreset - reset host port via SATA phy reset
Tejun Heo624d5c52008-03-25 22:16:41 +09002181 * @link: link to reset
2182 * @class: resulting class of attached device
2183 * @deadline: deadline jiffies for the operation
2184 *
2185 * SATA phy-reset host port using DET bits of SControl register,
2186 * wait for !BSY and classify the attached device.
2187 *
2188 * LOCKING:
2189 * Kernel thread context (may sleep)
2190 *
2191 * RETURNS:
2192 * 0 on success, -errno otherwise.
2193 */
Tejun Heo9363c382008-04-07 22:47:16 +09002194int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heo624d5c52008-03-25 22:16:41 +09002195 unsigned long deadline)
2196{
Tejun Heo9dadd452008-04-07 22:47:19 +09002197 struct ata_eh_context *ehc = &link->eh_context;
2198 const unsigned long *timing = sata_ehc_deb_timing(ehc);
2199 bool online;
Tejun Heo624d5c52008-03-25 22:16:41 +09002200 int rc;
2201
Tejun Heo9dadd452008-04-07 22:47:19 +09002202 rc = sata_link_hardreset(link, timing, deadline, &online,
2203 ata_sff_check_ready);
Tejun Heo9dadd452008-04-07 22:47:19 +09002204 if (online)
2205 *class = ata_sff_dev_classify(link->device, 1, NULL);
Tejun Heo624d5c52008-03-25 22:16:41 +09002206
2207 DPRINTK("EXIT, class=%u\n", *class);
Tejun Heo9dadd452008-04-07 22:47:19 +09002208 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09002209}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002210EXPORT_SYMBOL_GPL(sata_sff_hardreset);
Tejun Heo624d5c52008-03-25 22:16:41 +09002211
2212/**
Tejun Heo203c75b2008-04-07 22:47:18 +09002213 * ata_sff_postreset - SFF postreset callback
2214 * @link: the target SFF ata_link
2215 * @classes: classes of attached devices
2216 *
2217 * This function is invoked after a successful reset. It first
2218 * calls ata_std_postreset() and performs SFF specific postreset
2219 * processing.
2220 *
2221 * LOCKING:
2222 * Kernel thread context (may sleep)
2223 */
2224void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
2225{
2226 struct ata_port *ap = link->ap;
2227
2228 ata_std_postreset(link, classes);
2229
2230 /* is double-select really necessary? */
2231 if (classes[0] != ATA_DEV_NONE)
2232 ap->ops->sff_dev_select(ap, 1);
2233 if (classes[1] != ATA_DEV_NONE)
2234 ap->ops->sff_dev_select(ap, 0);
2235
2236 /* bail out if no device is present */
2237 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2238 DPRINTK("EXIT, no device\n");
2239 return;
2240 }
2241
2242 /* set up device control */
Stuart MENEFYe3e43852009-03-10 11:38:13 +00002243 if (ap->ioaddr.ctl_addr) {
Tejun Heo203c75b2008-04-07 22:47:18 +09002244 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Stuart MENEFYe3e43852009-03-10 11:38:13 +00002245 ap->last_ctl = ap->ctl;
2246 }
Tejun Heo203c75b2008-04-07 22:47:18 +09002247}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002248EXPORT_SYMBOL_GPL(ata_sff_postreset);
Tejun Heo203c75b2008-04-07 22:47:18 +09002249
2250/**
Alan Cox3d47aa82009-03-24 10:23:19 +00002251 * ata_sff_drain_fifo - Stock FIFO drain logic for SFF controllers
2252 * @qc: command
2253 *
2254 * Drain the FIFO and device of any stuck data following a command
2255 * failing to complete. In some cases this is neccessary before a
2256 * reset will recover the device.
2257 *
2258 */
2259
2260void ata_sff_drain_fifo(struct ata_queued_cmd *qc)
2261{
2262 int count;
2263 struct ata_port *ap;
2264
2265 /* We only need to flush incoming data when a command was running */
2266 if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
2267 return;
2268
2269 ap = qc->ap;
2270 /* Drain up to 64K of data before we give up this recovery method */
2271 for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
2272 && count < 32768; count++)
2273 ioread16(ap->ioaddr.data_addr);
2274
2275 /* Can become DEBUG later */
2276 if (count)
2277 ata_port_printk(ap, KERN_DEBUG,
2278 "drained %d bytes to clear DRQ.\n", count);
2279
2280}
2281EXPORT_SYMBOL_GPL(ata_sff_drain_fifo);
2282
2283/**
Tejun Heo9363c382008-04-07 22:47:16 +09002284 * ata_sff_error_handler - Stock error handler for BMDMA controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002285 * @ap: port to handle error for
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002286 *
Tejun Heo9363c382008-04-07 22:47:16 +09002287 * Stock error handler for SFF controller. It can handle both
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002288 * PATA and SATA controllers. Many controllers should be able to
2289 * use this EH as-is or with some added handling before and
2290 * after.
2291 *
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002292 * LOCKING:
2293 * Kernel thread context (may sleep)
2294 */
Tejun Heo9363c382008-04-07 22:47:16 +09002295void ata_sff_error_handler(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002296{
Tejun Heoa1efdab2008-03-25 12:22:50 +09002297 ata_reset_fn_t softreset = ap->ops->softreset;
2298 ata_reset_fn_t hardreset = ap->ops->hardreset;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002299 struct ata_queued_cmd *qc;
2300 unsigned long flags;
2301 int thaw = 0;
2302
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002303 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002304 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2305 qc = NULL;
2306
2307 /* reset PIO HSM and stop DMA engine */
Jeff Garzikba6a1302006-06-22 23:46:10 -04002308 spin_lock_irqsave(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002309
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002310 ap->hsm_task_state = HSM_ST_IDLE;
2311
Tejun Heoed82f962008-03-25 21:34:39 +09002312 if (ap->ioaddr.bmdma_addr &&
2313 qc && (qc->tf.protocol == ATA_PROT_DMA ||
Tejun Heo0dc36882007-12-18 16:34:43 -05002314 qc->tf.protocol == ATAPI_PROT_DMA)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002315 u8 host_stat;
2316
Robert Hancockfbbb2622006-10-27 19:08:41 -07002317 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002318
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002319 /* BMDMA controllers indicate host bus error by
2320 * setting DMA_ERR bit and timing out. As it wasn't
2321 * really a timeout event, adjust error mask and
2322 * cancel frozen state.
2323 */
Alan Cox3d47aa82009-03-24 10:23:19 +00002324 if (qc->err_mask == AC_ERR_TIMEOUT
2325 && (host_stat & ATA_DMA_ERR)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002326 qc->err_mask = AC_ERR_HOST_BUS;
2327 thaw = 1;
2328 }
2329
2330 ap->ops->bmdma_stop(qc);
2331 }
2332
Alan Coxa57c1ba2008-05-29 22:10:58 +01002333 ata_sff_sync(ap); /* FIXME: We don't need this */
Tejun Heo5682ed32008-04-07 22:47:16 +09002334 ap->ops->sff_check_status(ap);
2335 ap->ops->sff_irq_clear(ap);
Alan Cox3d47aa82009-03-24 10:23:19 +00002336 /* We *MUST* do FIFO draining before we issue a reset as several
2337 * devices helpfully clear their internal state and will lock solid
2338 * if we touch the data port post reset. Pass qc in case anyone wants
2339 * to do different PIO/DMA recovery or has per command fixups
2340 */
2341 if (ap->ops->drain_fifo)
2342 ap->ops->drain_fifo(qc);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002343
Jeff Garzikba6a1302006-06-22 23:46:10 -04002344 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002345
2346 if (thaw)
2347 ata_eh_thaw_port(ap);
2348
2349 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002350
Tejun Heo57c9efd2008-04-07 22:47:19 +09002351 /* Ignore ata_sff_softreset if ctl isn't accessible and
2352 * built-in hardresets if SCR access isn't available.
Tejun Heoa1efdab2008-03-25 12:22:50 +09002353 */
Tejun Heo9363c382008-04-07 22:47:16 +09002354 if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
Tejun Heoa1efdab2008-03-25 12:22:50 +09002355 softreset = NULL;
Tejun Heo57c9efd2008-04-07 22:47:19 +09002356 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09002357 hardreset = NULL;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002358
Tejun Heoa1efdab2008-03-25 12:22:50 +09002359 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2360 ap->ops->postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002361}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002362EXPORT_SYMBOL_GPL(ata_sff_error_handler);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002363
2364/**
Tejun Heo9363c382008-04-07 22:47:16 +09002365 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002366 * @qc: internal command to clean up
2367 *
2368 * LOCKING:
2369 * Kernel thread context (may sleep)
2370 */
Tejun Heo9363c382008-04-07 22:47:16 +09002371void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002372{
Tejun Heo570106d2008-10-20 13:10:21 +09002373 struct ata_port *ap = qc->ap;
2374 unsigned long flags;
2375
2376 spin_lock_irqsave(ap->lock, flags);
2377
2378 ap->hsm_task_state = HSM_ST_IDLE;
2379
2380 if (ap->ioaddr.bmdma_addr)
Alan61dd08c2007-01-25 15:09:05 +00002381 ata_bmdma_stop(qc);
Tejun Heo570106d2008-10-20 13:10:21 +09002382
2383 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002384}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002385EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002386
Alan Coxd92e74d2007-06-07 16:19:15 +01002387/**
2388 * ata_sff_port_start - Set port up for dma.
2389 * @ap: Port to initialize
2390 *
2391 * Called just after data structures for each port are
2392 * initialized. Allocates space for PRD table if the device
2393 * is DMA capable SFF.
2394 *
2395 * May be used as the port_start() entry in ata_port_operations.
2396 *
2397 * LOCKING:
2398 * Inherited from caller.
2399 */
Alan Coxd92e74d2007-06-07 16:19:15 +01002400int ata_sff_port_start(struct ata_port *ap)
2401{
2402 if (ap->ioaddr.bmdma_addr)
2403 return ata_port_start(ap);
2404 return 0;
2405}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002406EXPORT_SYMBOL_GPL(ata_sff_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01002407
Tejun Heo272f7882008-03-25 22:16:40 +09002408/**
Alan Coxe3cf95d2009-04-09 17:31:17 +01002409 * ata_sff_port_start32 - Set port up for dma.
2410 * @ap: Port to initialize
2411 *
2412 * Called just after data structures for each port are
2413 * initialized. Allocates space for PRD table if the device
2414 * is DMA capable SFF.
2415 *
2416 * May be used as the port_start() entry in ata_port_operations for
2417 * devices that are capable of 32bit PIO.
2418 *
2419 * LOCKING:
2420 * Inherited from caller.
2421 */
2422int ata_sff_port_start32(struct ata_port *ap)
2423{
2424 ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
2425 if (ap->ioaddr.bmdma_addr)
2426 return ata_port_start(ap);
2427 return 0;
2428}
2429EXPORT_SYMBOL_GPL(ata_sff_port_start32);
2430
2431/**
Tejun Heo9363c382008-04-07 22:47:16 +09002432 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
Tejun Heo624d5c52008-03-25 22:16:41 +09002433 * @ioaddr: IO address structure to be initialized
2434 *
2435 * Utility function which initializes data_addr, error_addr,
2436 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2437 * device_addr, status_addr, and command_addr to standard offsets
2438 * relative to cmd_addr.
2439 *
2440 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2441 */
Tejun Heo9363c382008-04-07 22:47:16 +09002442void ata_sff_std_ports(struct ata_ioports *ioaddr)
Tejun Heo624d5c52008-03-25 22:16:41 +09002443{
2444 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2445 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2446 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2447 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2448 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2449 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2450 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2451 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2452 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2453 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2454}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002455EXPORT_SYMBOL_GPL(ata_sff_std_ports);
Tejun Heo624d5c52008-03-25 22:16:41 +09002456
Tejun Heo9363c382008-04-07 22:47:16 +09002457unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
2458 unsigned long xfer_mask)
Tejun Heo071ce342008-03-25 22:16:42 +09002459{
2460 /* Filter out DMA modes if the device has been configured by
2461 the BIOS as PIO only */
2462
2463 if (adev->link->ap->ioaddr.bmdma_addr == NULL)
2464 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2465 return xfer_mask;
2466}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002467EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
Tejun Heo071ce342008-03-25 22:16:42 +09002468
Tejun Heo624d5c52008-03-25 22:16:41 +09002469/**
Tejun Heo272f7882008-03-25 22:16:40 +09002470 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2471 * @qc: Info associated with this ATA transaction.
2472 *
2473 * LOCKING:
2474 * spin_lock_irqsave(host lock)
2475 */
2476void ata_bmdma_setup(struct ata_queued_cmd *qc)
2477{
2478 struct ata_port *ap = qc->ap;
2479 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2480 u8 dmactl;
2481
2482 /* load PRD table addr. */
2483 mb(); /* make sure PRD table writes are visible to controller */
2484 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2485
2486 /* specify data direction, triple-check start bit is clear */
2487 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2488 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2489 if (!rw)
2490 dmactl |= ATA_DMA_WR;
2491 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2492
2493 /* issue r/w command */
Tejun Heo5682ed32008-04-07 22:47:16 +09002494 ap->ops->sff_exec_command(ap, &qc->tf);
Tejun Heo272f7882008-03-25 22:16:40 +09002495}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002496EXPORT_SYMBOL_GPL(ata_bmdma_setup);
Tejun Heo272f7882008-03-25 22:16:40 +09002497
2498/**
2499 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2500 * @qc: Info associated with this ATA transaction.
2501 *
2502 * LOCKING:
2503 * spin_lock_irqsave(host lock)
2504 */
2505void ata_bmdma_start(struct ata_queued_cmd *qc)
2506{
2507 struct ata_port *ap = qc->ap;
2508 u8 dmactl;
2509
2510 /* start host DMA transaction */
2511 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2512 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2513
2514 /* Strictly, one may wish to issue an ioread8() here, to
2515 * flush the mmio write. However, control also passes
2516 * to the hardware at this point, and it will interrupt
2517 * us when we are to resume control. So, in effect,
2518 * we don't care when the mmio write flushes.
2519 * Further, a read of the DMA status register _immediately_
2520 * following the write may not be what certain flaky hardware
2521 * is expected, so I think it is best to not add a readb()
2522 * without first all the MMIO ATA cards/mobos.
2523 * Or maybe I'm just being paranoid.
2524 *
2525 * FIXME: The posting of this write means I/O starts are
2526 * unneccessarily delayed for MMIO
2527 */
2528}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002529EXPORT_SYMBOL_GPL(ata_bmdma_start);
Tejun Heo272f7882008-03-25 22:16:40 +09002530
2531/**
2532 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2533 * @qc: Command we are ending DMA for
2534 *
2535 * Clears the ATA_DMA_START flag in the dma control register
2536 *
2537 * May be used as the bmdma_stop() entry in ata_port_operations.
2538 *
2539 * LOCKING:
2540 * spin_lock_irqsave(host lock)
2541 */
2542void ata_bmdma_stop(struct ata_queued_cmd *qc)
2543{
2544 struct ata_port *ap = qc->ap;
2545 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2546
2547 /* clear start/stop bit */
2548 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2549 mmio + ATA_DMA_CMD);
2550
2551 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
Alan Coxa57c1ba2008-05-29 22:10:58 +01002552 ata_sff_dma_pause(ap);
Tejun Heo272f7882008-03-25 22:16:40 +09002553}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002554EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo272f7882008-03-25 22:16:40 +09002555
2556/**
2557 * ata_bmdma_status - Read PCI IDE BMDMA status
2558 * @ap: Port associated with this ATA transaction.
2559 *
2560 * Read and return BMDMA status register.
2561 *
2562 * May be used as the bmdma_status() entry in ata_port_operations.
2563 *
2564 * LOCKING:
2565 * spin_lock_irqsave(host lock)
2566 */
2567u8 ata_bmdma_status(struct ata_port *ap)
2568{
2569 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2570}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002571EXPORT_SYMBOL_GPL(ata_bmdma_status);
Tejun Heo272f7882008-03-25 22:16:40 +09002572
2573/**
Tejun Heo624d5c52008-03-25 22:16:41 +09002574 * ata_bus_reset - reset host port and associated ATA channel
2575 * @ap: port to reset
2576 *
2577 * This is typically the first time we actually start issuing
2578 * commands to the ATA channel. We wait for BSY to clear, then
2579 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2580 * result. Determine what devices, if any, are on the channel
2581 * by looking at the device 0/1 error register. Look at the signature
2582 * stored in each device's taskfile registers, to determine if
2583 * the device is ATA or ATAPI.
2584 *
2585 * LOCKING:
2586 * PCI/etc. bus probe sem.
2587 * Obtains host lock.
2588 *
2589 * SIDE EFFECTS:
2590 * Sets ATA_FLAG_DISABLED if bus reset fails.
2591 *
2592 * DEPRECATED:
2593 * This function is only for drivers which still use old EH and
2594 * will be removed soon.
Tejun Heo272f7882008-03-25 22:16:40 +09002595 */
Tejun Heo624d5c52008-03-25 22:16:41 +09002596void ata_bus_reset(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +09002597{
Tejun Heo624d5c52008-03-25 22:16:41 +09002598 struct ata_device *device = ap->link.device;
2599 struct ata_ioports *ioaddr = &ap->ioaddr;
2600 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2601 u8 err;
2602 unsigned int dev0, dev1 = 0, devmask = 0;
2603 int rc;
2604
2605 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
2606
2607 /* determine if device 0/1 are present */
2608 if (ap->flags & ATA_FLAG_SATA_RESET)
2609 dev0 = 1;
2610 else {
2611 dev0 = ata_devchk(ap, 0);
2612 if (slave_possible)
2613 dev1 = ata_devchk(ap, 1);
2614 }
2615
2616 if (dev0)
2617 devmask |= (1 << 0);
2618 if (dev1)
2619 devmask |= (1 << 1);
2620
2621 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002622 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002623
2624 /* issue bus reset */
2625 if (ap->flags & ATA_FLAG_SRST) {
Tejun Heo341c2c92008-05-20 02:17:51 +09002626 rc = ata_bus_softreset(ap, devmask,
2627 ata_deadline(jiffies, 40000));
Tejun Heo624d5c52008-03-25 22:16:41 +09002628 if (rc && rc != -ENODEV)
2629 goto err_out;
2630 }
2631
2632 /*
2633 * determine by signature whether we have ATA or ATAPI devices
2634 */
Tejun Heo9363c382008-04-07 22:47:16 +09002635 device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002636 if ((slave_possible) && (err != 0x81))
Tejun Heo9363c382008-04-07 22:47:16 +09002637 device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002638
2639 /* is double-select really necessary? */
2640 if (device[1].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002641 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002642 if (device[0].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002643 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002644
2645 /* if no devices were detected, disable this port */
2646 if ((device[0].class == ATA_DEV_NONE) &&
2647 (device[1].class == ATA_DEV_NONE))
2648 goto err_out;
2649
2650 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2651 /* set up device control for ATA_FLAG_SATA_RESET */
2652 iowrite8(ap->ctl, ioaddr->ctl_addr);
Stuart MENEFYe3e43852009-03-10 11:38:13 +00002653 ap->last_ctl = ap->ctl;
Tejun Heo624d5c52008-03-25 22:16:41 +09002654 }
2655
2656 DPRINTK("EXIT\n");
2657 return;
2658
2659err_out:
2660 ata_port_printk(ap, KERN_ERR, "disabling port\n");
2661 ata_port_disable(ap);
2662
2663 DPRINTK("EXIT\n");
Tejun Heo272f7882008-03-25 22:16:40 +09002664}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002665EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo272f7882008-03-25 22:16:40 +09002666
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002667#ifdef CONFIG_PCI
Alan4112e162007-01-08 12:10:05 +00002668
Tejun Heo272f7882008-03-25 22:16:40 +09002669/**
Tejun Heo9363c382008-04-07 22:47:16 +09002670 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
Tejun Heo272f7882008-03-25 22:16:40 +09002671 * @pdev: PCI device
2672 *
2673 * Some PCI ATA devices report simplex mode but in fact can be told to
2674 * enter non simplex mode. This implements the necessary logic to
2675 * perform the task on such devices. Calling it on other devices will
2676 * have -undefined- behaviour.
2677 */
Tejun Heo9363c382008-04-07 22:47:16 +09002678int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
Alan4112e162007-01-08 12:10:05 +00002679{
Tejun Heo272f7882008-03-25 22:16:40 +09002680 unsigned long bmdma = pci_resource_start(pdev, 4);
2681 u8 simplex;
Jeff Garzika84471f2007-02-26 05:51:33 -05002682
Tejun Heo272f7882008-03-25 22:16:40 +09002683 if (bmdma == 0)
2684 return -ENOENT;
2685
2686 simplex = inb(bmdma + 0x02);
2687 outb(simplex & 0x60, bmdma + 0x02);
2688 simplex = inb(bmdma + 0x02);
2689 if (simplex & 0x80)
2690 return -EOPNOTSUPP;
2691 return 0;
2692}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002693EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
Tejun Heo272f7882008-03-25 22:16:40 +09002694
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002695/**
Tejun Heo9363c382008-04-07 22:47:16 +09002696 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
Tejun Heo0f834de2007-04-17 23:44:07 +09002697 * @host: target ATA host
2698 *
2699 * Acquire PCI BMDMA resources and initialize @host accordingly.
2700 *
2701 * LOCKING:
2702 * Inherited from calling layer (may sleep).
2703 *
2704 * RETURNS:
2705 * 0 on success, -errno otherwise.
2706 */
Tejun Heo9363c382008-04-07 22:47:16 +09002707int ata_pci_bmdma_init(struct ata_host *host)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002708{
Tejun Heo0f834de2007-04-17 23:44:07 +09002709 struct device *gdev = host->dev;
2710 struct pci_dev *pdev = to_pci_dev(gdev);
2711 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002712
Alan Cox6fdc99a2007-07-26 18:41:30 +01002713 /* No BAR4 allocation: No DMA */
2714 if (pci_resource_start(pdev, 4) == 0)
2715 return 0;
2716
Tejun Heo0f834de2007-04-17 23:44:07 +09002717 /* TODO: If we get no DMA mask we should fall back to PIO */
2718 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
2719 if (rc)
2720 return rc;
2721 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
2722 if (rc)
2723 return rc;
2724
2725 /* request and iomap DMA region */
Tejun Heo35a10a82008-01-04 18:42:21 +09002726 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
Tejun Heo0f834de2007-04-17 23:44:07 +09002727 if (rc) {
2728 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
2729 return -ENOMEM;
2730 }
2731 host->iomap = pcim_iomap_table(pdev);
2732
Tejun Heo1626aeb2007-05-04 12:43:58 +02002733 for (i = 0; i < 2; i++) {
Tejun Heo0f834de2007-04-17 23:44:07 +09002734 struct ata_port *ap = host->ports[i];
Tejun Heo0f834de2007-04-17 23:44:07 +09002735 void __iomem *bmdma = host->iomap[4] + 8 * i;
2736
2737 if (ata_port_is_dummy(ap))
2738 continue;
2739
Tejun Heo21b0ad42007-04-17 23:44:07 +09002740 ap->ioaddr.bmdma_addr = bmdma;
Tejun Heo0f834de2007-04-17 23:44:07 +09002741 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
2742 (ioread8(bmdma + 2) & 0x80))
2743 host->flags |= ATA_HOST_SIMPLEX;
Tejun Heocbcdd872007-08-18 13:14:55 +09002744
2745 ata_port_desc(ap, "bmdma 0x%llx",
Alan Cox0fe40ff2009-01-05 14:16:13 +00002746 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
Tejun Heo0d5ff562007-02-01 15:06:36 +09002747 }
2748
Tejun Heo0f834de2007-04-17 23:44:07 +09002749 return 0;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002750}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002751EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002752
Tejun Heo272f7882008-03-25 22:16:40 +09002753static int ata_resources_present(struct pci_dev *pdev, int port)
2754{
2755 int i;
2756
2757 /* Check the PCI resources for this channel are enabled */
2758 port = port * 2;
Alan Cox0fe40ff2009-01-05 14:16:13 +00002759 for (i = 0; i < 2; i++) {
Tejun Heo272f7882008-03-25 22:16:40 +09002760 if (pci_resource_start(pdev, port + i) == 0 ||
2761 pci_resource_len(pdev, port + i) == 0)
2762 return 0;
2763 }
2764 return 1;
2765}
2766
Tejun Heod491b272007-04-17 23:44:07 +09002767/**
Tejun Heo9363c382008-04-07 22:47:16 +09002768 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
Tejun Heod491b272007-04-17 23:44:07 +09002769 * @host: target ATA host
Tejun Heod491b272007-04-17 23:44:07 +09002770 *
Tejun Heo1626aeb2007-05-04 12:43:58 +02002771 * Acquire native PCI ATA resources for @host and initialize the
2772 * first two ports of @host accordingly. Ports marked dummy are
2773 * skipped and allocation failure makes the port dummy.
Tejun Heod491b272007-04-17 23:44:07 +09002774 *
Tejun Heod583bc12007-07-04 18:02:07 +09002775 * Note that native PCI resources are valid even for legacy hosts
2776 * as we fix up pdev resources array early in boot, so this
2777 * function can be used for both native and legacy SFF hosts.
2778 *
Tejun Heod491b272007-04-17 23:44:07 +09002779 * LOCKING:
2780 * Inherited from calling layer (may sleep).
2781 *
2782 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +02002783 * 0 if at least one port is initialized, -ENODEV if no port is
2784 * available.
Tejun Heod491b272007-04-17 23:44:07 +09002785 */
Tejun Heo9363c382008-04-07 22:47:16 +09002786int ata_pci_sff_init_host(struct ata_host *host)
Tejun Heod491b272007-04-17 23:44:07 +09002787{
2788 struct device *gdev = host->dev;
2789 struct pci_dev *pdev = to_pci_dev(gdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002790 unsigned int mask = 0;
Tejun Heod491b272007-04-17 23:44:07 +09002791 int i, rc;
2792
Tejun Heod491b272007-04-17 23:44:07 +09002793 /* request, iomap BARs and init port addresses accordingly */
2794 for (i = 0; i < 2; i++) {
2795 struct ata_port *ap = host->ports[i];
2796 int base = i * 2;
2797 void __iomem * const *iomap;
2798
Tejun Heo1626aeb2007-05-04 12:43:58 +02002799 if (ata_port_is_dummy(ap))
Tejun Heod491b272007-04-17 23:44:07 +09002800 continue;
2801
Tejun Heo1626aeb2007-05-04 12:43:58 +02002802 /* Discard disabled ports. Some controllers show
2803 * their unused channels this way. Disabled ports are
2804 * made dummy.
2805 */
2806 if (!ata_resources_present(pdev, i)) {
2807 ap->ops = &ata_dummy_port_ops;
2808 continue;
2809 }
2810
Tejun Heo35a10a82008-01-04 18:42:21 +09002811 rc = pcim_iomap_regions(pdev, 0x3 << base,
2812 dev_driver_string(gdev));
Tejun Heod491b272007-04-17 23:44:07 +09002813 if (rc) {
Tejun Heo1626aeb2007-05-04 12:43:58 +02002814 dev_printk(KERN_WARNING, gdev,
2815 "failed to request/iomap BARs for port %d "
2816 "(errno=%d)\n", i, rc);
Tejun Heod491b272007-04-17 23:44:07 +09002817 if (rc == -EBUSY)
2818 pcim_pin_device(pdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002819 ap->ops = &ata_dummy_port_ops;
2820 continue;
Tejun Heod491b272007-04-17 23:44:07 +09002821 }
2822 host->iomap = iomap = pcim_iomap_table(pdev);
2823
2824 ap->ioaddr.cmd_addr = iomap[base];
2825 ap->ioaddr.altstatus_addr =
2826 ap->ioaddr.ctl_addr = (void __iomem *)
2827 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
Tejun Heo9363c382008-04-07 22:47:16 +09002828 ata_sff_std_ports(&ap->ioaddr);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002829
Tejun Heocbcdd872007-08-18 13:14:55 +09002830 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2831 (unsigned long long)pci_resource_start(pdev, base),
2832 (unsigned long long)pci_resource_start(pdev, base + 1));
2833
Tejun Heo1626aeb2007-05-04 12:43:58 +02002834 mask |= 1 << i;
2835 }
2836
2837 if (!mask) {
2838 dev_printk(KERN_ERR, gdev, "no available native port\n");
2839 return -ENODEV;
Tejun Heod491b272007-04-17 23:44:07 +09002840 }
2841
2842 return 0;
2843}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002844EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
Tejun Heod491b272007-04-17 23:44:07 +09002845
Tejun Heo21b0ad42007-04-17 23:44:07 +09002846/**
Tejun Heo9363c382008-04-07 22:47:16 +09002847 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
Tejun Heo21b0ad42007-04-17 23:44:07 +09002848 * @pdev: target PCI device
Tejun Heo1626aeb2007-05-04 12:43:58 +02002849 * @ppi: array of port_info, must be enough for two ports
Tejun Heo21b0ad42007-04-17 23:44:07 +09002850 * @r_host: out argument for the initialized ATA host
2851 *
2852 * Helper to allocate ATA host for @pdev, acquire all native PCI
2853 * resources and initialize it accordingly in one go.
2854 *
2855 * LOCKING:
2856 * Inherited from calling layer (may sleep).
2857 *
2858 * RETURNS:
2859 * 0 on success, -errno otherwise.
2860 */
Tejun Heo9363c382008-04-07 22:47:16 +09002861int ata_pci_sff_prepare_host(struct pci_dev *pdev,
Alan Cox0fe40ff2009-01-05 14:16:13 +00002862 const struct ata_port_info * const *ppi,
Tejun Heod583bc12007-07-04 18:02:07 +09002863 struct ata_host **r_host)
Tejun Heo21b0ad42007-04-17 23:44:07 +09002864{
2865 struct ata_host *host;
Tejun Heo21b0ad42007-04-17 23:44:07 +09002866 int rc;
2867
2868 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2869 return -ENOMEM;
2870
2871 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2872 if (!host) {
2873 dev_printk(KERN_ERR, &pdev->dev,
2874 "failed to allocate ATA host\n");
2875 rc = -ENOMEM;
2876 goto err_out;
2877 }
2878
Tejun Heo9363c382008-04-07 22:47:16 +09002879 rc = ata_pci_sff_init_host(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002880 if (rc)
2881 goto err_out;
2882
2883 /* init DMA related stuff */
Tejun Heo9363c382008-04-07 22:47:16 +09002884 rc = ata_pci_bmdma_init(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002885 if (rc)
2886 goto err_bmdma;
2887
2888 devres_remove_group(&pdev->dev, NULL);
2889 *r_host = host;
2890 return 0;
2891
Alan Cox0fe40ff2009-01-05 14:16:13 +00002892err_bmdma:
Tejun Heo21b0ad42007-04-17 23:44:07 +09002893 /* This is necessary because PCI and iomap resources are
2894 * merged and releasing the top group won't release the
2895 * acquired resources if some of those have been acquired
2896 * before entering this function.
2897 */
2898 pcim_iounmap_regions(pdev, 0xf);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002899err_out:
Tejun Heo21b0ad42007-04-17 23:44:07 +09002900 devres_release_group(&pdev->dev, NULL);
2901 return rc;
2902}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002903EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002904
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002905/**
Tejun Heo9363c382008-04-07 22:47:16 +09002906 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002907 * @host: target SFF ATA host
2908 * @irq_handler: irq_handler used when requesting IRQ(s)
2909 * @sht: scsi_host_template to use when registering the host
2910 *
2911 * This is the counterpart of ata_host_activate() for SFF ATA
2912 * hosts. This separate helper is necessary because SFF hosts
2913 * use two separate interrupts in legacy mode.
2914 *
2915 * LOCKING:
2916 * Inherited from calling layer (may sleep).
2917 *
2918 * RETURNS:
2919 * 0 on success, -errno otherwise.
2920 */
Tejun Heo9363c382008-04-07 22:47:16 +09002921int ata_pci_sff_activate_host(struct ata_host *host,
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002922 irq_handler_t irq_handler,
2923 struct scsi_host_template *sht)
2924{
2925 struct device *dev = host->dev;
2926 struct pci_dev *pdev = to_pci_dev(dev);
2927 const char *drv_name = dev_driver_string(host->dev);
2928 int legacy_mode = 0, rc;
2929
2930 rc = ata_host_start(host);
2931 if (rc)
2932 return rc;
2933
2934 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
2935 u8 tmp8, mask;
2936
2937 /* TODO: What if one channel is in native mode ... */
2938 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
2939 mask = (1 << 2) | (1 << 0);
2940 if ((tmp8 & mask) != mask)
2941 legacy_mode = 1;
2942#if defined(CONFIG_NO_ATA_LEGACY)
2943 /* Some platforms with PCI limits cannot address compat
2944 port space. In that case we punt if their firmware has
2945 left a device in compatibility mode */
2946 if (legacy_mode) {
2947 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
2948 return -EOPNOTSUPP;
2949 }
2950#endif
2951 }
2952
2953 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2954 return -ENOMEM;
2955
2956 if (!legacy_mode && pdev->irq) {
2957 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2958 IRQF_SHARED, drv_name, host);
2959 if (rc)
2960 goto out;
2961
2962 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
2963 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
2964 } else if (legacy_mode) {
2965 if (!ata_port_is_dummy(host->ports[0])) {
2966 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2967 irq_handler, IRQF_SHARED,
2968 drv_name, host);
2969 if (rc)
2970 goto out;
2971
2972 ata_port_desc(host->ports[0], "irq %d",
2973 ATA_PRIMARY_IRQ(pdev));
2974 }
2975
2976 if (!ata_port_is_dummy(host->ports[1])) {
2977 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2978 irq_handler, IRQF_SHARED,
2979 drv_name, host);
2980 if (rc)
2981 goto out;
2982
2983 ata_port_desc(host->ports[1], "irq %d",
2984 ATA_SECONDARY_IRQ(pdev));
2985 }
2986 }
2987
2988 rc = ata_host_register(host, sht);
Alan Cox0fe40ff2009-01-05 14:16:13 +00002989out:
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002990 if (rc == 0)
2991 devres_remove_group(dev, NULL);
2992 else
2993 devres_release_group(dev, NULL);
2994
2995 return rc;
2996}
Alan Cox0fe40ff2009-01-05 14:16:13 +00002997EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002998
2999/**
Tejun Heo9363c382008-04-07 22:47:16 +09003000 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003001 * @pdev: Controller to be initialized
Tejun Heo1626aeb2007-05-04 12:43:58 +02003002 * @ppi: array of port_info, must be enough for two ports
Tejun Heo1bd5b712008-03-25 12:22:49 +09003003 * @sht: scsi_host_template to use when registering the host
Tejun Heo887125e2008-03-25 12:22:49 +09003004 * @host_priv: host private_data
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003005 *
3006 * This is a helper function which can be called from a driver's
3007 * xxx_init_one() probe function if the hardware uses traditional
3008 * IDE taskfile registers.
3009 *
3010 * This function calls pci_enable_device(), reserves its register
3011 * regions, sets the dma mask, enables bus master mode, and calls
3012 * ata_device_add()
3013 *
Alan Cox2ec7df02006-08-10 16:59:10 +09003014 * ASSUMPTION:
3015 * Nobody makes a single channel controller that appears solely as
3016 * the secondary legacy port on PCI.
3017 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003018 * LOCKING:
3019 * Inherited from PCI layer (may sleep).
3020 *
3021 * RETURNS:
3022 * Zero on success, negative on errno-based value on error.
3023 */
Tejun Heo9363c382008-04-07 22:47:16 +09003024int ata_pci_sff_init_one(struct pci_dev *pdev,
Alan Cox0fe40ff2009-01-05 14:16:13 +00003025 const struct ata_port_info * const *ppi,
Tejun Heo9363c382008-04-07 22:47:16 +09003026 struct scsi_host_template *sht, void *host_priv)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003027{
Tejun Heof0d36ef2007-01-20 16:00:28 +09003028 struct device *dev = &pdev->dev;
Tejun Heo1626aeb2007-05-04 12:43:58 +02003029 const struct ata_port_info *pi = NULL;
Tejun Heo0f834de2007-04-17 23:44:07 +09003030 struct ata_host *host = NULL;
Tejun Heo1626aeb2007-05-04 12:43:58 +02003031 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003032
3033 DPRINTK("ENTER\n");
3034
Tejun Heo1626aeb2007-05-04 12:43:58 +02003035 /* look up the first valid port_info */
3036 for (i = 0; i < 2 && ppi[i]; i++) {
3037 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
3038 pi = ppi[i];
3039 break;
3040 }
3041 }
3042
3043 if (!pi) {
3044 dev_printk(KERN_ERR, &pdev->dev,
3045 "no valid port_info specified\n");
3046 return -EINVAL;
3047 }
3048
Tejun Heof0d36ef2007-01-20 16:00:28 +09003049 if (!devres_open_group(dev, NULL, GFP_KERNEL))
3050 return -ENOMEM;
3051
Tejun Heof0d36ef2007-01-20 16:00:28 +09003052 rc = pcim_enable_device(pdev);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003053 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09003054 goto out;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003055
Tejun Heo4e6b79f2008-01-18 18:36:28 +09003056 /* prepare and activate SFF host */
Tejun Heo9363c382008-04-07 22:47:16 +09003057 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
Tejun Heod583bc12007-07-04 18:02:07 +09003058 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09003059 goto out;
Tejun Heo887125e2008-03-25 12:22:49 +09003060 host->private_data = host_priv;
Tejun Heod491b272007-04-17 23:44:07 +09003061
Tejun Heod491b272007-04-17 23:44:07 +09003062 pci_set_master(pdev);
Tejun Heo9363c382008-04-07 22:47:16 +09003063 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
Alan Cox0fe40ff2009-01-05 14:16:13 +00003064out:
Tejun Heo4e6b79f2008-01-18 18:36:28 +09003065 if (rc == 0)
3066 devres_remove_group(&pdev->dev, NULL);
3067 else
3068 devres_release_group(&pdev->dev, NULL);
Tejun Heod491b272007-04-17 23:44:07 +09003069
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003070 return rc;
3071}
Tejun Heo9363c382008-04-07 22:47:16 +09003072EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
Alan Cox0fe40ff2009-01-05 14:16:13 +00003073
Tejun Heo624d5c52008-03-25 22:16:41 +09003074#endif /* CONFIG_PCI */