]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/ata/libata-sff.c
libata-sff: fix incorrect EH message
[net-next-2.6.git] / drivers / ata / libata-sff.c
CommitLineData
1fdffbce 1/*
f3a03b09 2 * libata-sff.c - helper library for PCI IDE BMDMA
1fdffbce
JG
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2006 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
33 */
34
1fdffbce
JG
35#include <linux/kernel.h>
36#include <linux/pci.h>
37#include <linux/libata.h>
624d5c51 38#include <linux/highmem.h>
1fdffbce
JG
39
40#include "libata.h"
41
624d5c51
TH
42const struct ata_port_operations ata_sff_port_ops = {
43 .inherits = &ata_base_port_ops,
44
9363c382
TH
45 .qc_prep = ata_sff_qc_prep,
46 .qc_issue = ata_sff_qc_issue,
4c9bf4e7 47 .qc_fill_rtf = ata_sff_qc_fill_rtf,
9363c382
TH
48
49 .freeze = ata_sff_freeze,
50 .thaw = ata_sff_thaw,
0aa1113d 51 .prereset = ata_sff_prereset,
9363c382 52 .softreset = ata_sff_softreset,
57c9efdf 53 .hardreset = sata_sff_hardreset,
203c75b8 54 .postreset = ata_sff_postreset,
9363c382
TH
55 .error_handler = ata_sff_error_handler,
56 .post_internal_cmd = ata_sff_post_internal_cmd,
57
5682ed33
TH
58 .sff_dev_select = ata_sff_dev_select,
59 .sff_check_status = ata_sff_check_status,
60 .sff_tf_load = ata_sff_tf_load,
61 .sff_tf_read = ata_sff_tf_read,
62 .sff_exec_command = ata_sff_exec_command,
63 .sff_data_xfer = ata_sff_data_xfer,
64 .sff_irq_on = ata_sff_irq_on,
288623a0 65 .sff_irq_clear = ata_sff_irq_clear,
624d5c51
TH
66
67 .port_start = ata_sff_port_start,
68};
0fe40ff8 69EXPORT_SYMBOL_GPL(ata_sff_port_ops);
624d5c51
TH
70
71const struct ata_port_operations ata_bmdma_port_ops = {
72 .inherits = &ata_sff_port_ops,
73
9363c382 74 .mode_filter = ata_bmdma_mode_filter,
624d5c51
TH
75
76 .bmdma_setup = ata_bmdma_setup,
77 .bmdma_start = ata_bmdma_start,
78 .bmdma_stop = ata_bmdma_stop,
79 .bmdma_status = ata_bmdma_status,
624d5c51 80};
0fe40ff8 81EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
624d5c51 82
871af121
AC
83const struct ata_port_operations ata_bmdma32_port_ops = {
84 .inherits = &ata_bmdma_port_ops,
85
86 .sff_data_xfer = ata_sff_data_xfer32,
87};
88EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
89
624d5c51
TH
90/**
91 * ata_fill_sg - Fill PCI IDE PRD table
92 * @qc: Metadata associated with taskfile to be transferred
93 *
94 * Fill PCI IDE PRD (scatter-gather) table with segments
95 * associated with the current disk command.
96 *
97 * LOCKING:
98 * spin_lock_irqsave(host lock)
99 *
100 */
101static void ata_fill_sg(struct ata_queued_cmd *qc)
102{
103 struct ata_port *ap = qc->ap;
104 struct scatterlist *sg;
105 unsigned int si, pi;
106
107 pi = 0;
108 for_each_sg(qc->sg, sg, qc->n_elem, si) {
109 u32 addr, offset;
110 u32 sg_len, len;
111
112 /* determine if physical DMA addr spans 64K boundary.
113 * Note h/w doesn't support 64-bit, so we unconditionally
114 * truncate dma_addr_t to u32.
115 */
116 addr = (u32) sg_dma_address(sg);
117 sg_len = sg_dma_len(sg);
118
119 while (sg_len) {
120 offset = addr & 0xffff;
121 len = sg_len;
122 if ((offset + sg_len) > 0x10000)
123 len = 0x10000 - offset;
124
125 ap->prd[pi].addr = cpu_to_le32(addr);
126 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
127 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
128
129 pi++;
130 sg_len -= len;
131 addr += len;
132 }
133 }
134
135 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
136}
137
138/**
139 * ata_fill_sg_dumb - Fill PCI IDE PRD table
140 * @qc: Metadata associated with taskfile to be transferred
141 *
142 * Fill PCI IDE PRD (scatter-gather) table with segments
143 * associated with the current disk command. Perform the fill
144 * so that we avoid writing any length 64K records for
145 * controllers that don't follow the spec.
146 *
147 * LOCKING:
148 * spin_lock_irqsave(host lock)
149 *
150 */
151static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
152{
153 struct ata_port *ap = qc->ap;
154 struct scatterlist *sg;
155 unsigned int si, pi;
156
157 pi = 0;
158 for_each_sg(qc->sg, sg, qc->n_elem, si) {
159 u32 addr, offset;
160 u32 sg_len, len, blen;
161
162 /* determine if physical DMA addr spans 64K boundary.
163 * Note h/w doesn't support 64-bit, so we unconditionally
164 * truncate dma_addr_t to u32.
165 */
166 addr = (u32) sg_dma_address(sg);
167 sg_len = sg_dma_len(sg);
168
169 while (sg_len) {
170 offset = addr & 0xffff;
171 len = sg_len;
172 if ((offset + sg_len) > 0x10000)
173 len = 0x10000 - offset;
174
175 blen = len & 0xffff;
176 ap->prd[pi].addr = cpu_to_le32(addr);
177 if (blen == 0) {
0fe40ff8
AC
178 /* Some PATA chipsets like the CS5530 can't
179 cope with 0x0000 meaning 64K as the spec
180 says */
624d5c51
TH
181 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
182 blen = 0x8000;
183 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
184 }
185 ap->prd[pi].flags_len = cpu_to_le32(blen);
186 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
187
188 pi++;
189 sg_len -= len;
190 addr += len;
191 }
192 }
193
194 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
195}
196
197/**
9363c382 198 * ata_sff_qc_prep - Prepare taskfile for submission
624d5c51
TH
199 * @qc: Metadata associated with taskfile to be prepared
200 *
201 * Prepare ATA taskfile for submission.
202 *
203 * LOCKING:
204 * spin_lock_irqsave(host lock)
205 */
9363c382 206void ata_sff_qc_prep(struct ata_queued_cmd *qc)
624d5c51
TH
207{
208 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
209 return;
210
211 ata_fill_sg(qc);
212}
0fe40ff8 213EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
624d5c51
TH
214
215/**
9363c382 216 * ata_sff_dumb_qc_prep - Prepare taskfile for submission
624d5c51
TH
217 * @qc: Metadata associated with taskfile to be prepared
218 *
219 * Prepare ATA taskfile for submission.
220 *
221 * LOCKING:
222 * spin_lock_irqsave(host lock)
223 */
9363c382 224void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
624d5c51
TH
225{
226 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
227 return;
228
229 ata_fill_sg_dumb(qc);
230}
0fe40ff8 231EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
624d5c51 232
272f7884 233/**
9363c382 234 * ata_sff_check_status - Read device status reg & clear interrupt
272f7884
TH
235 * @ap: port where the device is
236 *
237 * Reads ATA taskfile status register for currently-selected device
238 * and return its value. This also clears pending interrupts
239 * from this device
240 *
241 * LOCKING:
242 * Inherited from caller.
243 */
9363c382 244u8 ata_sff_check_status(struct ata_port *ap)
272f7884
TH
245{
246 return ioread8(ap->ioaddr.status_addr);
247}
0fe40ff8 248EXPORT_SYMBOL_GPL(ata_sff_check_status);
272f7884
TH
249
250/**
9363c382 251 * ata_sff_altstatus - Read device alternate status reg
272f7884
TH
252 * @ap: port where the device is
253 *
254 * Reads ATA taskfile alternate status register for
255 * currently-selected device and return its value.
256 *
257 * Note: may NOT be used as the check_altstatus() entry in
258 * ata_port_operations.
259 *
260 * LOCKING:
261 * Inherited from caller.
262 */
a57c1bad 263static u8 ata_sff_altstatus(struct ata_port *ap)
624d5c51 264{
5682ed33
TH
265 if (ap->ops->sff_check_altstatus)
266 return ap->ops->sff_check_altstatus(ap);
624d5c51
TH
267
268 return ioread8(ap->ioaddr.altstatus_addr);
269}
270
a57c1bad
AC
271/**
272 * ata_sff_irq_status - Check if the device is busy
273 * @ap: port where the device is
274 *
275 * Determine if the port is currently busy. Uses altstatus
276 * if available in order to avoid clearing shared IRQ status
277 * when finding an IRQ source. Non ctl capable devices don't
278 * share interrupt lines fortunately for us.
279 *
280 * LOCKING:
281 * Inherited from caller.
282 */
283static u8 ata_sff_irq_status(struct ata_port *ap)
284{
285 u8 status;
286
287 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
288 status = ata_sff_altstatus(ap);
289 /* Not us: We are busy */
290 if (status & ATA_BUSY)
0fe40ff8 291 return status;
a57c1bad
AC
292 }
293 /* Clear INTRQ latch */
6311c90a 294 status = ap->ops->sff_check_status(ap);
a57c1bad
AC
295 return status;
296}
297
298/**
299 * ata_sff_sync - Flush writes
300 * @ap: Port to wait for.
301 *
302 * CAUTION:
303 * If we have an mmio device with no ctl and no altstatus
304 * method this will fail. No such devices are known to exist.
305 *
306 * LOCKING:
307 * Inherited from caller.
308 */
309
310static void ata_sff_sync(struct ata_port *ap)
311{
312 if (ap->ops->sff_check_altstatus)
313 ap->ops->sff_check_altstatus(ap);
314 else if (ap->ioaddr.altstatus_addr)
315 ioread8(ap->ioaddr.altstatus_addr);
316}
317
318/**
319 * ata_sff_pause - Flush writes and wait 400nS
320 * @ap: Port to pause for.
321 *
322 * CAUTION:
323 * If we have an mmio device with no ctl and no altstatus
324 * method this will fail. No such devices are known to exist.
325 *
326 * LOCKING:
327 * Inherited from caller.
328 */
329
330void ata_sff_pause(struct ata_port *ap)
331{
332 ata_sff_sync(ap);
333 ndelay(400);
334}
0fe40ff8 335EXPORT_SYMBOL_GPL(ata_sff_pause);
a57c1bad
AC
336
337/**
338 * ata_sff_dma_pause - Pause before commencing DMA
339 * @ap: Port to pause for.
340 *
341 * Perform I/O fencing and ensure sufficient cycle delays occur
342 * for the HDMA1:0 transition
343 */
0fe40ff8 344
a57c1bad
AC
345void ata_sff_dma_pause(struct ata_port *ap)
346{
347 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
348 /* An altstatus read will cause the needed delay without
349 messing up the IRQ status */
350 ata_sff_altstatus(ap);
351 return;
352 }
353 /* There are no DMA controllers without ctl. BUG here to ensure
354 we never violate the HDMA1:0 transition timing and risk
355 corruption. */
356 BUG();
357}
0fe40ff8 358EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
a57c1bad 359
624d5c51 360/**
9363c382 361 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
624d5c51 362 * @ap: port containing status register to be polled
341c2c95
TH
363 * @tmout_pat: impatience timeout in msecs
364 * @tmout: overall timeout in msecs
624d5c51
TH
365 *
366 * Sleep until ATA Status register bit BSY clears,
367 * or a timeout occurs.
368 *
369 * LOCKING:
370 * Kernel thread context (may sleep).
371 *
372 * RETURNS:
373 * 0 on success, -errno otherwise.
374 */
9363c382
TH
375int ata_sff_busy_sleep(struct ata_port *ap,
376 unsigned long tmout_pat, unsigned long tmout)
624d5c51
TH
377{
378 unsigned long timer_start, timeout;
379 u8 status;
380
9363c382 381 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
624d5c51 382 timer_start = jiffies;
341c2c95 383 timeout = ata_deadline(timer_start, tmout_pat);
624d5c51
TH
384 while (status != 0xff && (status & ATA_BUSY) &&
385 time_before(jiffies, timeout)) {
386 msleep(50);
9363c382 387 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
624d5c51
TH
388 }
389
390 if (status != 0xff && (status & ATA_BUSY))
391 ata_port_printk(ap, KERN_WARNING,
392 "port is slow to respond, please be patient "
393 "(Status 0x%x)\n", status);
394
341c2c95 395 timeout = ata_deadline(timer_start, tmout);
624d5c51
TH
396 while (status != 0xff && (status & ATA_BUSY) &&
397 time_before(jiffies, timeout)) {
398 msleep(50);
5682ed33 399 status = ap->ops->sff_check_status(ap);
624d5c51
TH
400 }
401
402 if (status == 0xff)
403 return -ENODEV;
404
405 if (status & ATA_BUSY) {
406 ata_port_printk(ap, KERN_ERR, "port failed to respond "
407 "(%lu secs, Status 0x%x)\n",
341c2c95 408 DIV_ROUND_UP(tmout, 1000), status);
624d5c51
TH
409 return -EBUSY;
410 }
411
412 return 0;
413}
0fe40ff8 414EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
624d5c51 415
aa2731ad
TH
416static int ata_sff_check_ready(struct ata_link *link)
417{
418 u8 status = link->ap->ops->sff_check_status(link->ap);
419
78ab88f0 420 return ata_check_ready(status);
aa2731ad
TH
421}
422
624d5c51 423/**
9363c382 424 * ata_sff_wait_ready - sleep until BSY clears, or timeout
705e76be 425 * @link: SFF link to wait ready status for
624d5c51
TH
426 * @deadline: deadline jiffies for the operation
427 *
428 * Sleep until ATA Status register bit BSY clears, or timeout
429 * occurs.
430 *
431 * LOCKING:
432 * Kernel thread context (may sleep).
433 *
434 * RETURNS:
435 * 0 on success, -errno otherwise.
436 */
705e76be 437int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
624d5c51 438{
aa2731ad 439 return ata_wait_ready(link, deadline, ata_sff_check_ready);
624d5c51 440}
0fe40ff8 441EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
624d5c51
TH
442
443/**
9363c382 444 * ata_sff_dev_select - Select device 0/1 on ATA bus
624d5c51
TH
445 * @ap: ATA channel to manipulate
446 * @device: ATA device (numbered from zero) to select
447 *
448 * Use the method defined in the ATA specification to
449 * make either device 0, or device 1, active on the
450 * ATA channel. Works with both PIO and MMIO.
451 *
452 * May be used as the dev_select() entry in ata_port_operations.
453 *
454 * LOCKING:
455 * caller.
456 */
9363c382 457void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
624d5c51
TH
458{
459 u8 tmp;
460
461 if (device == 0)
462 tmp = ATA_DEVICE_OBS;
463 else
464 tmp = ATA_DEVICE_OBS | ATA_DEV1;
465
466 iowrite8(tmp, ap->ioaddr.device_addr);
9363c382 467 ata_sff_pause(ap); /* needed; also flushes, for mmio */
624d5c51 468}
0fe40ff8 469EXPORT_SYMBOL_GPL(ata_sff_dev_select);
624d5c51
TH
470
471/**
472 * ata_dev_select - Select device 0/1 on ATA bus
473 * @ap: ATA channel to manipulate
474 * @device: ATA device (numbered from zero) to select
475 * @wait: non-zero to wait for Status register BSY bit to clear
476 * @can_sleep: non-zero if context allows sleeping
477 *
478 * Use the method defined in the ATA specification to
479 * make either device 0, or device 1, active on the
480 * ATA channel.
481 *
9363c382
TH
482 * This is a high-level version of ata_sff_dev_select(), which
483 * additionally provides the services of inserting the proper
484 * pauses and status polling, where needed.
624d5c51
TH
485 *
486 * LOCKING:
487 * caller.
488 */
489void ata_dev_select(struct ata_port *ap, unsigned int device,
490 unsigned int wait, unsigned int can_sleep)
491{
492 if (ata_msg_probe(ap))
493 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
494 "device %u, wait %u\n", device, wait);
495
496 if (wait)
497 ata_wait_idle(ap);
498
5682ed33 499 ap->ops->sff_dev_select(ap, device);
624d5c51
TH
500
501 if (wait) {
502 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
503 msleep(150);
504 ata_wait_idle(ap);
505 }
506}
507
508/**
9363c382 509 * ata_sff_irq_on - Enable interrupts on a port.
624d5c51
TH
510 * @ap: Port on which interrupts are enabled.
511 *
512 * Enable interrupts on a legacy IDE device using MMIO or PIO,
513 * wait for idle, clear any pending interrupts.
514 *
515 * LOCKING:
516 * Inherited from caller.
517 */
9363c382 518u8 ata_sff_irq_on(struct ata_port *ap)
624d5c51
TH
519{
520 struct ata_ioports *ioaddr = &ap->ioaddr;
521 u8 tmp;
522
523 ap->ctl &= ~ATA_NIEN;
524 ap->last_ctl = ap->ctl;
525
526 if (ioaddr->ctl_addr)
527 iowrite8(ap->ctl, ioaddr->ctl_addr);
528 tmp = ata_wait_idle(ap);
529
5682ed33 530 ap->ops->sff_irq_clear(ap);
624d5c51
TH
531
532 return tmp;
533}
0fe40ff8 534EXPORT_SYMBOL_GPL(ata_sff_irq_on);
624d5c51
TH
535
536/**
9363c382 537 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
624d5c51
TH
538 * @ap: Port associated with this ATA transaction.
539 *
540 * Clear interrupt and error flags in DMA status register.
541 *
542 * May be used as the irq_clear() entry in ata_port_operations.
543 *
544 * LOCKING:
545 * spin_lock_irqsave(host lock)
546 */
9363c382 547void ata_sff_irq_clear(struct ata_port *ap)
624d5c51
TH
548{
549 void __iomem *mmio = ap->ioaddr.bmdma_addr;
550
551 if (!mmio)
552 return;
553
554 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
555}
0fe40ff8 556EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
624d5c51
TH
557
558/**
9363c382 559 * ata_sff_tf_load - send taskfile registers to host controller
624d5c51
TH
560 * @ap: Port to which output is sent
561 * @tf: ATA taskfile register set
562 *
563 * Outputs ATA taskfile to standard ATA host controller.
564 *
565 * LOCKING:
566 * Inherited from caller.
567 */
9363c382 568void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
624d5c51
TH
569{
570 struct ata_ioports *ioaddr = &ap->ioaddr;
571 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
572
573 if (tf->ctl != ap->last_ctl) {
574 if (ioaddr->ctl_addr)
575 iowrite8(tf->ctl, ioaddr->ctl_addr);
576 ap->last_ctl = tf->ctl;
577 ata_wait_idle(ap);
578 }
579
580 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
efcb3cf7 581 WARN_ON_ONCE(!ioaddr->ctl_addr);
624d5c51
TH
582 iowrite8(tf->hob_feature, ioaddr->feature_addr);
583 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
584 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
585 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
586 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
587 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
588 tf->hob_feature,
589 tf->hob_nsect,
590 tf->hob_lbal,
591 tf->hob_lbam,
592 tf->hob_lbah);
593 }
594
595 if (is_addr) {
596 iowrite8(tf->feature, ioaddr->feature_addr);
597 iowrite8(tf->nsect, ioaddr->nsect_addr);
598 iowrite8(tf->lbal, ioaddr->lbal_addr);
599 iowrite8(tf->lbam, ioaddr->lbam_addr);
600 iowrite8(tf->lbah, ioaddr->lbah_addr);
601 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
602 tf->feature,
603 tf->nsect,
604 tf->lbal,
605 tf->lbam,
606 tf->lbah);
607 }
608
609 if (tf->flags & ATA_TFLAG_DEVICE) {
610 iowrite8(tf->device, ioaddr->device_addr);
611 VPRINTK("device 0x%X\n", tf->device);
612 }
613
614 ata_wait_idle(ap);
615}
0fe40ff8 616EXPORT_SYMBOL_GPL(ata_sff_tf_load);
624d5c51
TH
617
618/**
9363c382 619 * ata_sff_tf_read - input device's ATA taskfile shadow registers
624d5c51
TH
620 * @ap: Port from which input is read
621 * @tf: ATA taskfile register set for storing input
622 *
623 * Reads ATA taskfile registers for currently-selected device
624 * into @tf. Assumes the device has a fully SFF compliant task file
625 * layout and behaviour. If you device does not (eg has a different
626 * status method) then you will need to provide a replacement tf_read
627 *
628 * LOCKING:
629 * Inherited from caller.
630 */
9363c382 631void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
624d5c51
TH
632{
633 struct ata_ioports *ioaddr = &ap->ioaddr;
634
9363c382 635 tf->command = ata_sff_check_status(ap);
624d5c51
TH
636 tf->feature = ioread8(ioaddr->error_addr);
637 tf->nsect = ioread8(ioaddr->nsect_addr);
638 tf->lbal = ioread8(ioaddr->lbal_addr);
639 tf->lbam = ioread8(ioaddr->lbam_addr);
640 tf->lbah = ioread8(ioaddr->lbah_addr);
641 tf->device = ioread8(ioaddr->device_addr);
642
643 if (tf->flags & ATA_TFLAG_LBA48) {
644 if (likely(ioaddr->ctl_addr)) {
645 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
646 tf->hob_feature = ioread8(ioaddr->error_addr);
647 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
648 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
649 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
650 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
651 iowrite8(tf->ctl, ioaddr->ctl_addr);
652 ap->last_ctl = tf->ctl;
653 } else
efcb3cf7 654 WARN_ON_ONCE(1);
624d5c51
TH
655 }
656}
0fe40ff8 657EXPORT_SYMBOL_GPL(ata_sff_tf_read);
624d5c51
TH
658
659/**
9363c382 660 * ata_sff_exec_command - issue ATA command to host controller
624d5c51
TH
661 * @ap: port to which command is being issued
662 * @tf: ATA taskfile register set
663 *
664 * Issues ATA command, with proper synchronization with interrupt
665 * handler / other threads.
666 *
667 * LOCKING:
668 * spin_lock_irqsave(host lock)
669 */
9363c382 670void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
624d5c51
TH
671{
672 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
673
674 iowrite8(tf->command, ap->ioaddr.command_addr);
9363c382 675 ata_sff_pause(ap);
624d5c51 676}
0fe40ff8 677EXPORT_SYMBOL_GPL(ata_sff_exec_command);
624d5c51
TH
678
679/**
680 * ata_tf_to_host - issue ATA taskfile to host controller
681 * @ap: port to which command is being issued
682 * @tf: ATA taskfile register set
683 *
684 * Issues ATA taskfile register set to ATA host controller,
685 * with proper synchronization with interrupt handler and
686 * other threads.
687 *
688 * LOCKING:
689 * spin_lock_irqsave(host lock)
690 */
691static inline void ata_tf_to_host(struct ata_port *ap,
692 const struct ata_taskfile *tf)
693{
5682ed33
TH
694 ap->ops->sff_tf_load(ap, tf);
695 ap->ops->sff_exec_command(ap, tf);
624d5c51
TH
696}
697
698/**
9363c382 699 * ata_sff_data_xfer - Transfer data by PIO
624d5c51
TH
700 * @dev: device to target
701 * @buf: data buffer
702 * @buflen: buffer length
703 * @rw: read/write
704 *
705 * Transfer data from/to the device data register by PIO.
706 *
707 * LOCKING:
708 * Inherited from caller.
709 *
710 * RETURNS:
711 * Bytes consumed.
712 */
9363c382
TH
713unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
714 unsigned int buflen, int rw)
624d5c51
TH
715{
716 struct ata_port *ap = dev->link->ap;
717 void __iomem *data_addr = ap->ioaddr.data_addr;
718 unsigned int words = buflen >> 1;
719
720 /* Transfer multiple of 2 bytes */
721 if (rw == READ)
722 ioread16_rep(data_addr, buf, words);
723 else
724 iowrite16_rep(data_addr, buf, words);
725
726 /* Transfer trailing 1 byte, if any. */
727 if (unlikely(buflen & 0x01)) {
728 __le16 align_buf[1] = { 0 };
729 unsigned char *trailing_buf = buf + buflen - 1;
730
731 if (rw == READ) {
732 align_buf[0] = cpu_to_le16(ioread16(data_addr));
733 memcpy(trailing_buf, align_buf, 1);
734 } else {
735 memcpy(align_buf, trailing_buf, 1);
736 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
737 }
738 words++;
739 }
740
741 return words << 1;
742}
0fe40ff8 743EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
624d5c51 744
871af121
AC
745/**
746 * ata_sff_data_xfer32 - Transfer data by PIO
747 * @dev: device to target
748 * @buf: data buffer
749 * @buflen: buffer length
750 * @rw: read/write
751 *
752 * Transfer data from/to the device data register by PIO using 32bit
753 * I/O operations.
754 *
755 * LOCKING:
756 * Inherited from caller.
757 *
758 * RETURNS:
759 * Bytes consumed.
760 */
761
762unsigned int ata_sff_data_xfer32(struct ata_device *dev, unsigned char *buf,
763 unsigned int buflen, int rw)
764{
765 struct ata_port *ap = dev->link->ap;
766 void __iomem *data_addr = ap->ioaddr.data_addr;
767 unsigned int words = buflen >> 2;
768 int slop = buflen & 3;
769
770 /* Transfer multiple of 4 bytes */
771 if (rw == READ)
772 ioread32_rep(data_addr, buf, words);
773 else
774 iowrite32_rep(data_addr, buf, words);
775
776 if (unlikely(slop)) {
777 __le32 pad;
778 if (rw == READ) {
779 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
780 memcpy(buf + buflen - slop, &pad, slop);
781 } else {
782 memcpy(&pad, buf + buflen - slop, slop);
783 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
784 }
785 words++;
786 }
787 return words << 2;
788}
789EXPORT_SYMBOL_GPL(ata_sff_data_xfer32);
790
624d5c51 791/**
9363c382 792 * ata_sff_data_xfer_noirq - Transfer data by PIO
624d5c51
TH
793 * @dev: device to target
794 * @buf: data buffer
795 * @buflen: buffer length
796 * @rw: read/write
797 *
798 * Transfer data from/to the device data register by PIO. Do the
799 * transfer with interrupts disabled.
800 *
801 * LOCKING:
802 * Inherited from caller.
803 *
804 * RETURNS:
805 * Bytes consumed.
806 */
9363c382
TH
807unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
808 unsigned int buflen, int rw)
624d5c51
TH
809{
810 unsigned long flags;
811 unsigned int consumed;
812
813 local_irq_save(flags);
9363c382 814 consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
624d5c51
TH
815 local_irq_restore(flags);
816
817 return consumed;
818}
0fe40ff8 819EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
624d5c51
TH
820
821/**
822 * ata_pio_sector - Transfer a sector of data.
823 * @qc: Command on going
824 *
825 * Transfer qc->sect_size bytes of data from/to the ATA device.
826 *
827 * LOCKING:
828 * Inherited from caller.
829 */
830static void ata_pio_sector(struct ata_queued_cmd *qc)
831{
832 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
833 struct ata_port *ap = qc->ap;
834 struct page *page;
835 unsigned int offset;
836 unsigned char *buf;
837
838 if (qc->curbytes == qc->nbytes - qc->sect_size)
839 ap->hsm_task_state = HSM_ST_LAST;
840
841 page = sg_page(qc->cursg);
842 offset = qc->cursg->offset + qc->cursg_ofs;
843
844 /* get the current page and offset */
845 page = nth_page(page, (offset >> PAGE_SHIFT));
846 offset %= PAGE_SIZE;
847
848 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
849
850 if (PageHighMem(page)) {
851 unsigned long flags;
852
853 /* FIXME: use a bounce buffer */
854 local_irq_save(flags);
855 buf = kmap_atomic(page, KM_IRQ0);
856
857 /* do the actual data transfer */
5682ed33
TH
858 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
859 do_write);
624d5c51
TH
860
861 kunmap_atomic(buf, KM_IRQ0);
862 local_irq_restore(flags);
863 } else {
864 buf = page_address(page);
5682ed33
TH
865 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
866 do_write);
624d5c51
TH
867 }
868
869 qc->curbytes += qc->sect_size;
870 qc->cursg_ofs += qc->sect_size;
871
872 if (qc->cursg_ofs == qc->cursg->length) {
873 qc->cursg = sg_next(qc->cursg);
874 qc->cursg_ofs = 0;
875 }
876}
877
878/**
879 * ata_pio_sectors - Transfer one or many sectors.
880 * @qc: Command on going
881 *
882 * Transfer one or many sectors of data from/to the
883 * ATA device for the DRQ request.
884 *
885 * LOCKING:
886 * Inherited from caller.
887 */
888static void ata_pio_sectors(struct ata_queued_cmd *qc)
889{
890 if (is_multi_taskfile(&qc->tf)) {
891 /* READ/WRITE MULTIPLE */
892 unsigned int nsect;
893
efcb3cf7 894 WARN_ON_ONCE(qc->dev->multi_count == 0);
624d5c51
TH
895
896 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
897 qc->dev->multi_count);
898 while (nsect--)
899 ata_pio_sector(qc);
900 } else
901 ata_pio_sector(qc);
902
a57c1bad 903 ata_sff_sync(qc->ap); /* flush */
624d5c51
TH
904}
905
906/**
907 * atapi_send_cdb - Write CDB bytes to hardware
908 * @ap: Port to which ATAPI device is attached.
909 * @qc: Taskfile currently active
910 *
911 * When device has indicated its readiness to accept
912 * a CDB, this function is called. Send the CDB.
913 *
914 * LOCKING:
915 * caller.
916 */
917static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
918{
919 /* send SCSI cdb */
920 DPRINTK("send cdb\n");
efcb3cf7 921 WARN_ON_ONCE(qc->dev->cdb_len < 12);
624d5c51 922
5682ed33 923 ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
a57c1bad
AC
924 ata_sff_sync(ap);
925 /* FIXME: If the CDB is for DMA do we need to do the transition delay
926 or is bmdma_start guaranteed to do it ? */
624d5c51
TH
927 switch (qc->tf.protocol) {
928 case ATAPI_PROT_PIO:
929 ap->hsm_task_state = HSM_ST;
930 break;
931 case ATAPI_PROT_NODATA:
932 ap->hsm_task_state = HSM_ST_LAST;
933 break;
934 case ATAPI_PROT_DMA:
935 ap->hsm_task_state = HSM_ST_LAST;
936 /* initiate bmdma */
937 ap->ops->bmdma_start(qc);
938 break;
939 }
940}
941
942/**
943 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
944 * @qc: Command on going
945 * @bytes: number of bytes
946 *
947 * Transfer Transfer data from/to the ATAPI device.
948 *
949 * LOCKING:
950 * Inherited from caller.
951 *
952 */
953static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
954{
955 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
956 struct ata_port *ap = qc->ap;
957 struct ata_device *dev = qc->dev;
958 struct ata_eh_info *ehi = &dev->link->eh_info;
959 struct scatterlist *sg;
960 struct page *page;
961 unsigned char *buf;
962 unsigned int offset, count, consumed;
963
964next_sg:
965 sg = qc->cursg;
966 if (unlikely(!sg)) {
967 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
968 "buf=%u cur=%u bytes=%u",
969 qc->nbytes, qc->curbytes, bytes);
970 return -1;
971 }
972
973 page = sg_page(sg);
974 offset = sg->offset + qc->cursg_ofs;
975
976 /* get the current page and offset */
977 page = nth_page(page, (offset >> PAGE_SHIFT));
978 offset %= PAGE_SIZE;
979
980 /* don't overrun current sg */
981 count = min(sg->length - qc->cursg_ofs, bytes);
982
983 /* don't cross page boundaries */
984 count = min(count, (unsigned int)PAGE_SIZE - offset);
985
986 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
987
988 if (PageHighMem(page)) {
989 unsigned long flags;
990
991 /* FIXME: use bounce buffer */
992 local_irq_save(flags);
993 buf = kmap_atomic(page, KM_IRQ0);
994
995 /* do the actual data transfer */
0fe40ff8
AC
996 consumed = ap->ops->sff_data_xfer(dev, buf + offset,
997 count, rw);
624d5c51
TH
998
999 kunmap_atomic(buf, KM_IRQ0);
1000 local_irq_restore(flags);
1001 } else {
1002 buf = page_address(page);
0fe40ff8
AC
1003 consumed = ap->ops->sff_data_xfer(dev, buf + offset,
1004 count, rw);
624d5c51
TH
1005 }
1006
1007 bytes -= min(bytes, consumed);
1008 qc->curbytes += count;
1009 qc->cursg_ofs += count;
1010
1011 if (qc->cursg_ofs == sg->length) {
1012 qc->cursg = sg_next(qc->cursg);
1013 qc->cursg_ofs = 0;
1014 }
1015
a0f79f7a
CB
1016 /*
1017 * There used to be a WARN_ON_ONCE(qc->cursg && count != consumed);
1018 * Unfortunately __atapi_pio_bytes doesn't know enough to do the WARN
1019 * check correctly as it doesn't know if it is the last request being
1020 * made. Somebody should implement a proper sanity check.
1021 */
624d5c51
TH
1022 if (bytes)
1023 goto next_sg;
1024 return 0;
1025}
1026
1027/**
1028 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
1029 * @qc: Command on going
1030 *
1031 * Transfer Transfer data from/to the ATAPI device.
1032 *
1033 * LOCKING:
1034 * Inherited from caller.
1035 */
1036static void atapi_pio_bytes(struct ata_queued_cmd *qc)
1037{
1038 struct ata_port *ap = qc->ap;
1039 struct ata_device *dev = qc->dev;
1040 struct ata_eh_info *ehi = &dev->link->eh_info;
1041 unsigned int ireason, bc_lo, bc_hi, bytes;
1042 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
1043
1044 /* Abuse qc->result_tf for temp storage of intermediate TF
1045 * here to save some kernel stack usage.
1046 * For normal completion, qc->result_tf is not relevant. For
1047 * error, qc->result_tf is later overwritten by ata_qc_complete().
1048 * So, the correctness of qc->result_tf is not affected.
1049 */
5682ed33 1050 ap->ops->sff_tf_read(ap, &qc->result_tf);
624d5c51
TH
1051 ireason = qc->result_tf.nsect;
1052 bc_lo = qc->result_tf.lbam;
1053 bc_hi = qc->result_tf.lbah;
1054 bytes = (bc_hi << 8) | bc_lo;
1055
1056 /* shall be cleared to zero, indicating xfer of data */
1057 if (unlikely(ireason & (1 << 0)))
1058 goto atapi_check;
1059
1060 /* make sure transfer direction matches expected */
1061 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
1062 if (unlikely(do_write != i_write))
1063 goto atapi_check;
1064
1065 if (unlikely(!bytes))
1066 goto atapi_check;
1067
1068 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
1069
1070 if (unlikely(__atapi_pio_bytes(qc, bytes)))
1071 goto err_out;
a57c1bad 1072 ata_sff_sync(ap); /* flush */
624d5c51
TH
1073
1074 return;
1075
1076 atapi_check:
1077 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
1078 ireason, bytes);
1079 err_out:
1080 qc->err_mask |= AC_ERR_HSM;
1081 ap->hsm_task_state = HSM_ST_ERR;
1082}
1083
1084/**
1085 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
1086 * @ap: the target ata_port
1087 * @qc: qc on going
1088 *
1089 * RETURNS:
1090 * 1 if ok in workqueue, 0 otherwise.
1091 */
0fe40ff8
AC
1092static inline int ata_hsm_ok_in_wq(struct ata_port *ap,
1093 struct ata_queued_cmd *qc)
624d5c51
TH
1094{
1095 if (qc->tf.flags & ATA_TFLAG_POLLING)
1096 return 1;
1097
1098 if (ap->hsm_task_state == HSM_ST_FIRST) {
1099 if (qc->tf.protocol == ATA_PROT_PIO &&
0fe40ff8 1100 (qc->tf.flags & ATA_TFLAG_WRITE))
624d5c51
TH
1101 return 1;
1102
1103 if (ata_is_atapi(qc->tf.protocol) &&
0fe40ff8 1104 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
624d5c51
TH
1105 return 1;
1106 }
1107
1108 return 0;
1109}
1110
1111/**
1112 * ata_hsm_qc_complete - finish a qc running on standard HSM
1113 * @qc: Command to complete
1114 * @in_wq: 1 if called from workqueue, 0 otherwise
1115 *
1116 * Finish @qc which is running on standard HSM.
1117 *
1118 * LOCKING:
1119 * If @in_wq is zero, spin_lock_irqsave(host lock).
1120 * Otherwise, none on entry and grabs host lock.
1121 */
1122static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
1123{
1124 struct ata_port *ap = qc->ap;
1125 unsigned long flags;
1126
1127 if (ap->ops->error_handler) {
1128 if (in_wq) {
1129 spin_lock_irqsave(ap->lock, flags);
1130
1131 /* EH might have kicked in while host lock is
1132 * released.
1133 */
1134 qc = ata_qc_from_tag(ap, qc->tag);
1135 if (qc) {
1136 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
5682ed33 1137 ap->ops->sff_irq_on(ap);
624d5c51
TH
1138 ata_qc_complete(qc);
1139 } else
1140 ata_port_freeze(ap);
1141 }
1142
1143 spin_unlock_irqrestore(ap->lock, flags);
1144 } else {
1145 if (likely(!(qc->err_mask & AC_ERR_HSM)))
1146 ata_qc_complete(qc);
1147 else
1148 ata_port_freeze(ap);
1149 }
1150 } else {
1151 if (in_wq) {
1152 spin_lock_irqsave(ap->lock, flags);
5682ed33 1153 ap->ops->sff_irq_on(ap);
624d5c51
TH
1154 ata_qc_complete(qc);
1155 spin_unlock_irqrestore(ap->lock, flags);
1156 } else
1157 ata_qc_complete(qc);
1158 }
1159}
1160
1161/**
9363c382 1162 * ata_sff_hsm_move - move the HSM to the next state.
624d5c51
TH
1163 * @ap: the target ata_port
1164 * @qc: qc on going
1165 * @status: current device status
1166 * @in_wq: 1 if called from workqueue, 0 otherwise
1167 *
1168 * RETURNS:
1169 * 1 when poll next status needed, 0 otherwise.
1170 */
9363c382
TH
1171int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
1172 u8 status, int in_wq)
624d5c51 1173{
a836d3e8 1174 struct ata_eh_info *ehi = &ap->link.eh_info;
624d5c51
TH
1175 unsigned long flags = 0;
1176 int poll_next;
1177
efcb3cf7 1178 WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
624d5c51 1179
9363c382 1180 /* Make sure ata_sff_qc_issue() does not throw things
624d5c51
TH
1181 * like DMA polling into the workqueue. Notice that
1182 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1183 */
efcb3cf7 1184 WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc));
624d5c51
TH
1185
1186fsm_start:
1187 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1188 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
1189
1190 switch (ap->hsm_task_state) {
1191 case HSM_ST_FIRST:
1192 /* Send first data block or PACKET CDB */
1193
1194 /* If polling, we will stay in the work queue after
1195 * sending the data. Otherwise, interrupt handler
1196 * takes over after sending the data.
1197 */
1198 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
1199
1200 /* check device status */
1201 if (unlikely((status & ATA_DRQ) == 0)) {
1202 /* handle BSY=0, DRQ=0 as error */
1203 if (likely(status & (ATA_ERR | ATA_DF)))
1204 /* device stops HSM for abort/error */
1205 qc->err_mask |= AC_ERR_DEV;
a836d3e8 1206 else {
624d5c51 1207 /* HSM violation. Let EH handle this */
a836d3e8
TH
1208 ata_ehi_push_desc(ehi,
1209 "ST_FIRST: !(DRQ|ERR|DF)");
624d5c51 1210 qc->err_mask |= AC_ERR_HSM;
a836d3e8 1211 }
624d5c51
TH
1212
1213 ap->hsm_task_state = HSM_ST_ERR;
1214 goto fsm_start;
1215 }
1216
1217 /* Device should not ask for data transfer (DRQ=1)
1218 * when it finds something wrong.
1219 * We ignore DRQ here and stop the HSM by
1220 * changing hsm_task_state to HSM_ST_ERR and
1221 * let the EH abort the command or reset the device.
1222 */
1223 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1224 /* Some ATAPI tape drives forget to clear the ERR bit
1225 * when doing the next command (mostly request sense).
1226 * We ignore ERR here to workaround and proceed sending
1227 * the CDB.
1228 */
1229 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
a836d3e8
TH
1230 ata_ehi_push_desc(ehi, "ST_FIRST: "
1231 "DRQ=1 with device error, "
1232 "dev_stat 0x%X", status);
624d5c51
TH
1233 qc->err_mask |= AC_ERR_HSM;
1234 ap->hsm_task_state = HSM_ST_ERR;
1235 goto fsm_start;
1236 }
1237 }
1238
1239 /* Send the CDB (atapi) or the first data block (ata pio out).
1240 * During the state transition, interrupt handler shouldn't
1241 * be invoked before the data transfer is complete and
1242 * hsm_task_state is changed. Hence, the following locking.
1243 */
1244 if (in_wq)
1245 spin_lock_irqsave(ap->lock, flags);
1246
1247 if (qc->tf.protocol == ATA_PROT_PIO) {
1248 /* PIO data out protocol.
1249 * send first data block.
1250 */
1251
1252 /* ata_pio_sectors() might change the state
1253 * to HSM_ST_LAST. so, the state is changed here
1254 * before ata_pio_sectors().
1255 */
1256 ap->hsm_task_state = HSM_ST;
1257 ata_pio_sectors(qc);
1258 } else
1259 /* send CDB */
1260 atapi_send_cdb(ap, qc);
1261
1262 if (in_wq)
1263 spin_unlock_irqrestore(ap->lock, flags);
1264
1265 /* if polling, ata_pio_task() handles the rest.
1266 * otherwise, interrupt handler takes over from here.
1267 */
1268 break;
1269
1270 case HSM_ST:
1271 /* complete command or read/write the data register */
1272 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1273 /* ATAPI PIO protocol */
1274 if ((status & ATA_DRQ) == 0) {
1275 /* No more data to transfer or device error.
1276 * Device error will be tagged in HSM_ST_LAST.
1277 */
1278 ap->hsm_task_state = HSM_ST_LAST;
1279 goto fsm_start;
1280 }
1281
1282 /* Device should not ask for data transfer (DRQ=1)
1283 * when it finds something wrong.
1284 * We ignore DRQ here and stop the HSM by
1285 * changing hsm_task_state to HSM_ST_ERR and
1286 * let the EH abort the command or reset the device.
1287 */
1288 if (unlikely(status & (ATA_ERR | ATA_DF))) {
a836d3e8
TH
1289 ata_ehi_push_desc(ehi, "ST-ATAPI: "
1290 "DRQ=1 with device error, "
1291 "dev_stat 0x%X", status);
624d5c51
TH
1292 qc->err_mask |= AC_ERR_HSM;
1293 ap->hsm_task_state = HSM_ST_ERR;
1294 goto fsm_start;
1295 }
1296
1297 atapi_pio_bytes(qc);
1298
1299 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1300 /* bad ireason reported by device */
1301 goto fsm_start;
1302
1303 } else {
1304 /* ATA PIO protocol */
1305 if (unlikely((status & ATA_DRQ) == 0)) {
1306 /* handle BSY=0, DRQ=0 as error */
6a6b97d3 1307 if (likely(status & (ATA_ERR | ATA_DF))) {
624d5c51
TH
1308 /* device stops HSM for abort/error */
1309 qc->err_mask |= AC_ERR_DEV;
6a6b97d3
TH
1310
1311 /* If diagnostic failed and this is
1312 * IDENTIFY, it's likely a phantom
1313 * device. Mark hint.
1314 */
1315 if (qc->dev->horkage &
1316 ATA_HORKAGE_DIAGNOSTIC)
1317 qc->err_mask |=
1318 AC_ERR_NODEV_HINT;
1319 } else {
624d5c51
TH
1320 /* HSM violation. Let EH handle this.
1321 * Phantom devices also trigger this
1322 * condition. Mark hint.
1323 */
a836d3e8 1324 ata_ehi_push_desc(ehi, "ST-ATA: "
80ee6f54 1325 "DRQ=0 without device error, "
a836d3e8 1326 "dev_stat 0x%X", status);
624d5c51
TH
1327 qc->err_mask |= AC_ERR_HSM |
1328 AC_ERR_NODEV_HINT;
a836d3e8 1329 }
624d5c51
TH
1330
1331 ap->hsm_task_state = HSM_ST_ERR;
1332 goto fsm_start;
1333 }
1334
1335 /* For PIO reads, some devices may ask for
1336 * data transfer (DRQ=1) alone with ERR=1.
1337 * We respect DRQ here and transfer one
1338 * block of junk data before changing the
1339 * hsm_task_state to HSM_ST_ERR.
1340 *
1341 * For PIO writes, ERR=1 DRQ=1 doesn't make
1342 * sense since the data block has been
1343 * transferred to the device.
1344 */
1345 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1346 /* data might be corrputed */
1347 qc->err_mask |= AC_ERR_DEV;
1348
1349 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1350 ata_pio_sectors(qc);
1351 status = ata_wait_idle(ap);
1352 }
1353
a836d3e8
TH
1354 if (status & (ATA_BUSY | ATA_DRQ)) {
1355 ata_ehi_push_desc(ehi, "ST-ATA: "
1356 "BUSY|DRQ persists on ERR|DF, "
1357 "dev_stat 0x%X", status);
624d5c51 1358 qc->err_mask |= AC_ERR_HSM;
a836d3e8 1359 }
624d5c51
TH
1360
1361 /* ata_pio_sectors() might change the
1362 * state to HSM_ST_LAST. so, the state
1363 * is changed after ata_pio_sectors().
1364 */
1365 ap->hsm_task_state = HSM_ST_ERR;
1366 goto fsm_start;
1367 }
1368
1369 ata_pio_sectors(qc);
1370
1371 if (ap->hsm_task_state == HSM_ST_LAST &&
1372 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1373 /* all data read */
1374 status = ata_wait_idle(ap);
1375 goto fsm_start;
1376 }
1377 }
1378
1379 poll_next = 1;
1380 break;
1381
1382 case HSM_ST_LAST:
1383 if (unlikely(!ata_ok(status))) {
1384 qc->err_mask |= __ac_err_mask(status);
1385 ap->hsm_task_state = HSM_ST_ERR;
1386 goto fsm_start;
1387 }
1388
1389 /* no more data to transfer */
1390 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1391 ap->print_id, qc->dev->devno, status);
1392
efcb3cf7 1393 WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
624d5c51
TH
1394
1395 ap->hsm_task_state = HSM_ST_IDLE;
1396
1397 /* complete taskfile transaction */
1398 ata_hsm_qc_complete(qc, in_wq);
1399
1400 poll_next = 0;
1401 break;
1402
1403 case HSM_ST_ERR:
624d5c51
TH
1404 ap->hsm_task_state = HSM_ST_IDLE;
1405
1406 /* complete taskfile transaction */
1407 ata_hsm_qc_complete(qc, in_wq);
1408
1409 poll_next = 0;
1410 break;
1411 default:
1412 poll_next = 0;
1413 BUG();
1414 }
1415
1416 return poll_next;
1417}
0fe40ff8 1418EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
624d5c51
TH
1419
1420void ata_pio_task(struct work_struct *work)
1421{
1422 struct ata_port *ap =
1423 container_of(work, struct ata_port, port_task.work);
1424 struct ata_queued_cmd *qc = ap->port_task_data;
1425 u8 status;
1426 int poll_next;
1427
1428fsm_start:
efcb3cf7 1429 WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE);
624d5c51
TH
1430
1431 /*
1432 * This is purely heuristic. This is a fast path.
1433 * Sometimes when we enter, BSY will be cleared in
1434 * a chk-status or two. If not, the drive is probably seeking
1435 * or something. Snooze for a couple msecs, then
1436 * chk-status again. If still busy, queue delayed work.
1437 */
9363c382 1438 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
624d5c51
TH
1439 if (status & ATA_BUSY) {
1440 msleep(2);
9363c382 1441 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
624d5c51
TH
1442 if (status & ATA_BUSY) {
1443 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
1444 return;
1445 }
1446 }
1447
1448 /* move the HSM */
9363c382 1449 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
624d5c51
TH
1450
1451 /* another command or interrupt handler
1452 * may be running at this point.
1453 */
1454 if (poll_next)
1455 goto fsm_start;
1456}
1457
1458/**
9363c382 1459 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
624d5c51
TH
1460 * @qc: command to issue to device
1461 *
1462 * Using various libata functions and hooks, this function
1463 * starts an ATA command. ATA commands are grouped into
1464 * classes called "protocols", and issuing each type of protocol
1465 * is slightly different.
1466 *
1467 * May be used as the qc_issue() entry in ata_port_operations.
1468 *
1469 * LOCKING:
1470 * spin_lock_irqsave(host lock)
1471 *
1472 * RETURNS:
1473 * Zero on success, AC_ERR_* mask on failure
1474 */
9363c382 1475unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
624d5c51
TH
1476{
1477 struct ata_port *ap = qc->ap;
1478
1479 /* Use polling pio if the LLD doesn't handle
1480 * interrupt driven pio and atapi CDB interrupt.
1481 */
1482 if (ap->flags & ATA_FLAG_PIO_POLLING) {
1483 switch (qc->tf.protocol) {
1484 case ATA_PROT_PIO:
1485 case ATA_PROT_NODATA:
1486 case ATAPI_PROT_PIO:
1487 case ATAPI_PROT_NODATA:
1488 qc->tf.flags |= ATA_TFLAG_POLLING;
1489 break;
1490 case ATAPI_PROT_DMA:
1491 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
1492 /* see ata_dma_blacklisted() */
1493 BUG();
1494 break;
1495 default:
1496 break;
1497 }
1498 }
1499
1500 /* select the device */
1501 ata_dev_select(ap, qc->dev->devno, 1, 0);
1502
1503 /* start the command */
1504 switch (qc->tf.protocol) {
1505 case ATA_PROT_NODATA:
1506 if (qc->tf.flags & ATA_TFLAG_POLLING)
1507 ata_qc_set_polling(qc);
1508
1509 ata_tf_to_host(ap, &qc->tf);
1510 ap->hsm_task_state = HSM_ST_LAST;
1511
1512 if (qc->tf.flags & ATA_TFLAG_POLLING)
1513 ata_pio_queue_task(ap, qc, 0);
1514
1515 break;
1516
1517 case ATA_PROT_DMA:
efcb3cf7 1518 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
624d5c51 1519
5682ed33 1520 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
624d5c51
TH
1521 ap->ops->bmdma_setup(qc); /* set up bmdma */
1522 ap->ops->bmdma_start(qc); /* initiate bmdma */
1523 ap->hsm_task_state = HSM_ST_LAST;
1524 break;
1525
1526 case ATA_PROT_PIO:
1527 if (qc->tf.flags & ATA_TFLAG_POLLING)
1528 ata_qc_set_polling(qc);
1529
1530 ata_tf_to_host(ap, &qc->tf);
1531
1532 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1533 /* PIO data out protocol */
1534 ap->hsm_task_state = HSM_ST_FIRST;
1535 ata_pio_queue_task(ap, qc, 0);
1536
1537 /* always send first data block using
1538 * the ata_pio_task() codepath.
1539 */
1540 } else {
1541 /* PIO data in protocol */
1542 ap->hsm_task_state = HSM_ST;
1543
1544 if (qc->tf.flags & ATA_TFLAG_POLLING)
1545 ata_pio_queue_task(ap, qc, 0);
1546
1547 /* if polling, ata_pio_task() handles the rest.
1548 * otherwise, interrupt handler takes over from here.
1549 */
1550 }
1551
1552 break;
1553
1554 case ATAPI_PROT_PIO:
1555 case ATAPI_PROT_NODATA:
1556 if (qc->tf.flags & ATA_TFLAG_POLLING)
1557 ata_qc_set_polling(qc);
1558
1559 ata_tf_to_host(ap, &qc->tf);
1560
1561 ap->hsm_task_state = HSM_ST_FIRST;
1562
1563 /* send cdb by polling if no cdb interrupt */
1564 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1565 (qc->tf.flags & ATA_TFLAG_POLLING))
1566 ata_pio_queue_task(ap, qc, 0);
1567 break;
1568
1569 case ATAPI_PROT_DMA:
efcb3cf7 1570 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
624d5c51 1571
5682ed33 1572 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
624d5c51
TH
1573 ap->ops->bmdma_setup(qc); /* set up bmdma */
1574 ap->hsm_task_state = HSM_ST_FIRST;
1575
1576 /* send cdb by polling if no cdb interrupt */
1577 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1578 ata_pio_queue_task(ap, qc, 0);
1579 break;
1580
1581 default:
efcb3cf7 1582 WARN_ON_ONCE(1);
624d5c51
TH
1583 return AC_ERR_SYSTEM;
1584 }
1585
1586 return 0;
1587}
0fe40ff8 1588EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
624d5c51 1589
22183bf5
TH
1590/**
1591 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1592 * @qc: qc to fill result TF for
1593 *
1594 * @qc is finished and result TF needs to be filled. Fill it
1595 * using ->sff_tf_read.
1596 *
1597 * LOCKING:
1598 * spin_lock_irqsave(host lock)
1599 *
1600 * RETURNS:
1601 * true indicating that result TF is successfully filled.
1602 */
1603bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
1604{
1605 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
1606 return true;
1607}
0fe40ff8 1608EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
22183bf5 1609
624d5c51 1610/**
9363c382 1611 * ata_sff_host_intr - Handle host interrupt for given (port, task)
624d5c51
TH
1612 * @ap: Port on which interrupt arrived (possibly...)
1613 * @qc: Taskfile currently active in engine
1614 *
1615 * Handle host interrupt for given queued command. Currently,
1616 * only DMA interrupts are handled. All other commands are
1617 * handled via polling with interrupts disabled (nIEN bit).
1618 *
1619 * LOCKING:
1620 * spin_lock_irqsave(host lock)
1621 *
1622 * RETURNS:
1623 * One if interrupt was handled, zero if not (shared irq).
1624 */
9363c382
TH
1625inline unsigned int ata_sff_host_intr(struct ata_port *ap,
1626 struct ata_queued_cmd *qc)
624d5c51
TH
1627{
1628 struct ata_eh_info *ehi = &ap->link.eh_info;
1629 u8 status, host_stat = 0;
1630
1631 VPRINTK("ata%u: protocol %d task_state %d\n",
1632 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
1633
1634 /* Check whether we are expecting interrupt in this state */
1635 switch (ap->hsm_task_state) {
1636 case HSM_ST_FIRST:
1637 /* Some pre-ATAPI-4 devices assert INTRQ
1638 * at this state when ready to receive CDB.
1639 */
1640
1641 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1642 * The flag was turned on only for atapi devices. No
1643 * need to check ata_is_atapi(qc->tf.protocol) again.
1644 */
1645 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1646 goto idle_irq;
1647 break;
1648 case HSM_ST_LAST:
1649 if (qc->tf.protocol == ATA_PROT_DMA ||
1650 qc->tf.protocol == ATAPI_PROT_DMA) {
1651 /* check status of DMA engine */
1652 host_stat = ap->ops->bmdma_status(ap);
1653 VPRINTK("ata%u: host_stat 0x%X\n",
1654 ap->print_id, host_stat);
1655
1656 /* if it's not our irq... */
1657 if (!(host_stat & ATA_DMA_INTR))
1658 goto idle_irq;
1659
1660 /* before we do anything else, clear DMA-Start bit */
1661 ap->ops->bmdma_stop(qc);
1662
1663 if (unlikely(host_stat & ATA_DMA_ERR)) {
1664 /* error when transfering data to/from memory */
1665 qc->err_mask |= AC_ERR_HOST_BUS;
1666 ap->hsm_task_state = HSM_ST_ERR;
1667 }
1668 }
1669 break;
1670 case HSM_ST:
1671 break;
1672 default:
1673 goto idle_irq;
1674 }
1675
624d5c51 1676
a57c1bad
AC
1677 /* check main status, clearing INTRQ if needed */
1678 status = ata_sff_irq_status(ap);
1679 if (status & ATA_BUSY)
624d5c51
TH
1680 goto idle_irq;
1681
1682 /* ack bmdma irq events */
5682ed33 1683 ap->ops->sff_irq_clear(ap);
624d5c51 1684
9363c382 1685 ata_sff_hsm_move(ap, qc, status, 0);
624d5c51
TH
1686
1687 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
1688 qc->tf.protocol == ATAPI_PROT_DMA))
1689 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
1690
1691 return 1; /* irq handled */
1692
1693idle_irq:
1694 ap->stats.idle_irq++;
1695
1696#ifdef ATA_IRQ_TRAP
1697 if ((ap->stats.idle_irq % 1000) == 0) {
5682ed33
TH
1698 ap->ops->sff_check_status(ap);
1699 ap->ops->sff_irq_clear(ap);
624d5c51
TH
1700 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
1701 return 1;
1702 }
1703#endif
1704 return 0; /* irq not handled */
1705}
0fe40ff8 1706EXPORT_SYMBOL_GPL(ata_sff_host_intr);
624d5c51
TH
1707
1708/**
9363c382 1709 * ata_sff_interrupt - Default ATA host interrupt handler
624d5c51
TH
1710 * @irq: irq line (unused)
1711 * @dev_instance: pointer to our ata_host information structure
1712 *
1713 * Default interrupt handler for PCI IDE devices. Calls
9363c382 1714 * ata_sff_host_intr() for each port that is not disabled.
624d5c51
TH
1715 *
1716 * LOCKING:
1717 * Obtains host lock during operation.
1718 *
1719 * RETURNS:
1720 * IRQ_NONE or IRQ_HANDLED.
1721 */
9363c382 1722irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
624d5c51
TH
1723{
1724 struct ata_host *host = dev_instance;
1725 unsigned int i;
1726 unsigned int handled = 0;
1727 unsigned long flags;
1728
1729 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1730 spin_lock_irqsave(&host->lock, flags);
1731
1732 for (i = 0; i < host->n_ports; i++) {
1733 struct ata_port *ap;
1734
1735 ap = host->ports[i];
1736 if (ap &&
1737 !(ap->flags & ATA_FLAG_DISABLED)) {
1738 struct ata_queued_cmd *qc;
1739
1740 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1741 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
1742 (qc->flags & ATA_QCFLAG_ACTIVE))
9363c382 1743 handled |= ata_sff_host_intr(ap, qc);
624d5c51
TH
1744 }
1745 }
1746
1747 spin_unlock_irqrestore(&host->lock, flags);
1748
1749 return IRQ_RETVAL(handled);
1750}
0fe40ff8 1751EXPORT_SYMBOL_GPL(ata_sff_interrupt);
624d5c51
TH
1752
1753/**
9363c382 1754 * ata_sff_freeze - Freeze SFF controller port
624d5c51
TH
1755 * @ap: port to freeze
1756 *
1757 * Freeze BMDMA controller port.
1758 *
1759 * LOCKING:
1760 * Inherited from caller.
1761 */
9363c382 1762void ata_sff_freeze(struct ata_port *ap)
624d5c51
TH
1763{
1764 struct ata_ioports *ioaddr = &ap->ioaddr;
1765
1766 ap->ctl |= ATA_NIEN;
1767 ap->last_ctl = ap->ctl;
1768
1769 if (ioaddr->ctl_addr)
1770 iowrite8(ap->ctl, ioaddr->ctl_addr);
1771
1772 /* Under certain circumstances, some controllers raise IRQ on
1773 * ATA_NIEN manipulation. Also, many controllers fail to mask
1774 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1775 */
5682ed33 1776 ap->ops->sff_check_status(ap);
624d5c51 1777
5682ed33 1778 ap->ops->sff_irq_clear(ap);
624d5c51 1779}
0fe40ff8 1780EXPORT_SYMBOL_GPL(ata_sff_freeze);
624d5c51
TH
1781
1782/**
9363c382 1783 * ata_sff_thaw - Thaw SFF controller port
624d5c51
TH
1784 * @ap: port to thaw
1785 *
9363c382 1786 * Thaw SFF controller port.
624d5c51
TH
1787 *
1788 * LOCKING:
1789 * Inherited from caller.
1790 */
9363c382 1791void ata_sff_thaw(struct ata_port *ap)
272f7884 1792{
624d5c51 1793 /* clear & re-enable interrupts */
5682ed33
TH
1794 ap->ops->sff_check_status(ap);
1795 ap->ops->sff_irq_clear(ap);
1796 ap->ops->sff_irq_on(ap);
272f7884 1797}
0fe40ff8 1798EXPORT_SYMBOL_GPL(ata_sff_thaw);
272f7884 1799
0aa1113d
TH
1800/**
1801 * ata_sff_prereset - prepare SFF link for reset
1802 * @link: SFF link to be reset
1803 * @deadline: deadline jiffies for the operation
1804 *
1805 * SFF link @link is about to be reset. Initialize it. It first
1806 * calls ata_std_prereset() and wait for !BSY if the port is
1807 * being softreset.
1808 *
1809 * LOCKING:
1810 * Kernel thread context (may sleep)
1811 *
1812 * RETURNS:
1813 * 0 on success, -errno otherwise.
1814 */
1815int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
1816{
0aa1113d
TH
1817 struct ata_eh_context *ehc = &link->eh_context;
1818 int rc;
1819
1820 rc = ata_std_prereset(link, deadline);
1821 if (rc)
1822 return rc;
1823
1824 /* if we're about to do hardreset, nothing more to do */
1825 if (ehc->i.action & ATA_EH_HARDRESET)
1826 return 0;
1827
1828 /* wait for !BSY if we don't know that no device is attached */
1829 if (!ata_link_offline(link)) {
705e76be 1830 rc = ata_sff_wait_ready(link, deadline);
0aa1113d
TH
1831 if (rc && rc != -ENODEV) {
1832 ata_link_printk(link, KERN_WARNING, "device not ready "
1833 "(errno=%d), forcing hardreset\n", rc);
1834 ehc->i.action |= ATA_EH_HARDRESET;
1835 }
1836 }
1837
1838 return 0;
1839}
0fe40ff8 1840EXPORT_SYMBOL_GPL(ata_sff_prereset);
0aa1113d 1841
90088bb4 1842/**
624d5c51
TH
1843 * ata_devchk - PATA device presence detection
1844 * @ap: ATA channel to examine
1845 * @device: Device to examine (starting at zero)
90088bb4 1846 *
624d5c51
TH
1847 * This technique was originally described in
1848 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1849 * later found its way into the ATA/ATAPI spec.
1850 *
1851 * Write a pattern to the ATA shadow registers,
1852 * and if a device is present, it will respond by
1853 * correctly storing and echoing back the
1854 * ATA shadow register contents.
90088bb4
TH
1855 *
1856 * LOCKING:
624d5c51 1857 * caller.
90088bb4 1858 */
624d5c51 1859static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
90088bb4
TH
1860{
1861 struct ata_ioports *ioaddr = &ap->ioaddr;
624d5c51 1862 u8 nsect, lbal;
90088bb4 1863
5682ed33 1864 ap->ops->sff_dev_select(ap, device);
90088bb4 1865
624d5c51
TH
1866 iowrite8(0x55, ioaddr->nsect_addr);
1867 iowrite8(0xaa, ioaddr->lbal_addr);
90088bb4 1868
624d5c51
TH
1869 iowrite8(0xaa, ioaddr->nsect_addr);
1870 iowrite8(0x55, ioaddr->lbal_addr);
90088bb4 1871
624d5c51
TH
1872 iowrite8(0x55, ioaddr->nsect_addr);
1873 iowrite8(0xaa, ioaddr->lbal_addr);
1874
1875 nsect = ioread8(ioaddr->nsect_addr);
1876 lbal = ioread8(ioaddr->lbal_addr);
1877
1878 if ((nsect == 0x55) && (lbal == 0xaa))
1879 return 1; /* we found a device */
1880
1881 return 0; /* nothing found */
90088bb4
TH
1882}
1883
272f7884 1884/**
9363c382 1885 * ata_sff_dev_classify - Parse returned ATA device signature
624d5c51
TH
1886 * @dev: ATA device to classify (starting at zero)
1887 * @present: device seems present
1888 * @r_err: Value of error register on completion
272f7884 1889 *
624d5c51
TH
1890 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1891 * an ATA/ATAPI-defined set of values is placed in the ATA
1892 * shadow registers, indicating the results of device detection
1893 * and diagnostics.
272f7884 1894 *
624d5c51
TH
1895 * Select the ATA device, and read the values from the ATA shadow
1896 * registers. Then parse according to the Error register value,
1897 * and the spec-defined values examined by ata_dev_classify().
272f7884
TH
1898 *
1899 * LOCKING:
624d5c51
TH
1900 * caller.
1901 *
1902 * RETURNS:
1903 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
272f7884 1904 */
9363c382 1905unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
624d5c51 1906 u8 *r_err)
272f7884 1907{
624d5c51
TH
1908 struct ata_port *ap = dev->link->ap;
1909 struct ata_taskfile tf;
1910 unsigned int class;
1911 u8 err;
1912
5682ed33 1913 ap->ops->sff_dev_select(ap, dev->devno);
624d5c51
TH
1914
1915 memset(&tf, 0, sizeof(tf));
1916
5682ed33 1917 ap->ops->sff_tf_read(ap, &tf);
624d5c51
TH
1918 err = tf.feature;
1919 if (r_err)
1920 *r_err = err;
1921
1922 /* see if device passed diags: continue and warn later */
1923 if (err == 0)
1924 /* diagnostic fail : do nothing _YET_ */
1925 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1926 else if (err == 1)
1927 /* do nothing */ ;
1928 else if ((dev->devno == 0) && (err == 0x81))
1929 /* do nothing */ ;
1930 else
1931 return ATA_DEV_NONE;
272f7884 1932
624d5c51
TH
1933 /* determine if device is ATA or ATAPI */
1934 class = ata_dev_classify(&tf);
272f7884 1935
624d5c51
TH
1936 if (class == ATA_DEV_UNKNOWN) {
1937 /* If the device failed diagnostic, it's likely to
1938 * have reported incorrect device signature too.
1939 * Assume ATA device if the device seems present but
1940 * device signature is invalid with diagnostic
1941 * failure.
1942 */
1943 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1944 class = ATA_DEV_ATA;
1945 else
1946 class = ATA_DEV_NONE;
5682ed33
TH
1947 } else if ((class == ATA_DEV_ATA) &&
1948 (ap->ops->sff_check_status(ap) == 0))
624d5c51
TH
1949 class = ATA_DEV_NONE;
1950
1951 return class;
272f7884 1952}
0fe40ff8 1953EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
272f7884 1954
705e76be
TH
1955/**
1956 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1957 * @link: SFF link which is just reset
1958 * @devmask: mask of present devices
1959 * @deadline: deadline jiffies for the operation
1960 *
1961 * Wait devices attached to SFF @link to become ready after
1962 * reset. It contains preceding 150ms wait to avoid accessing TF
1963 * status register too early.
1964 *
1965 * LOCKING:
1966 * Kernel thread context (may sleep).
1967 *
1968 * RETURNS:
1969 * 0 on success, -ENODEV if some or all of devices in @devmask
1970 * don't seem to exist. -errno on other errors.
1971 */
1972int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
1973 unsigned long deadline)
1fdffbce 1974{
705e76be 1975 struct ata_port *ap = link->ap;
1fdffbce 1976 struct ata_ioports *ioaddr = &ap->ioaddr;
624d5c51
TH
1977 unsigned int dev0 = devmask & (1 << 0);
1978 unsigned int dev1 = devmask & (1 << 1);
1979 int rc, ret = 0;
1fdffbce 1980
341c2c95 1981 msleep(ATA_WAIT_AFTER_RESET);
705e76be
TH
1982
1983 /* always check readiness of the master device */
1984 rc = ata_sff_wait_ready(link, deadline);
1985 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
1986 * and TF status is 0xff, bail out on it too.
624d5c51 1987 */
705e76be
TH
1988 if (rc)
1989 return rc;
1fdffbce 1990
624d5c51
TH
1991 /* if device 1 was found in ata_devchk, wait for register
1992 * access briefly, then wait for BSY to clear.
1993 */
1994 if (dev1) {
1995 int i;
1fdffbce 1996
5682ed33 1997 ap->ops->sff_dev_select(ap, 1);
1fdffbce 1998
624d5c51
TH
1999 /* Wait for register access. Some ATAPI devices fail
2000 * to set nsect/lbal after reset, so don't waste too
2001 * much time on it. We're gonna wait for !BSY anyway.
2002 */
2003 for (i = 0; i < 2; i++) {
2004 u8 nsect, lbal;
2005
2006 nsect = ioread8(ioaddr->nsect_addr);
2007 lbal = ioread8(ioaddr->lbal_addr);
2008 if ((nsect == 1) && (lbal == 1))
2009 break;
2010 msleep(50); /* give drive a breather */
2011 }
2012
705e76be 2013 rc = ata_sff_wait_ready(link, deadline);
624d5c51
TH
2014 if (rc) {
2015 if (rc != -ENODEV)
2016 return rc;
2017 ret = rc;
2018 }
1fdffbce
JG
2019 }
2020
624d5c51 2021 /* is all this really necessary? */
5682ed33 2022 ap->ops->sff_dev_select(ap, 0);
624d5c51 2023 if (dev1)
5682ed33 2024 ap->ops->sff_dev_select(ap, 1);
624d5c51 2025 if (dev0)
5682ed33 2026 ap->ops->sff_dev_select(ap, 0);
624d5c51
TH
2027
2028 return ret;
1fdffbce 2029}
0fe40ff8 2030EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
1fdffbce 2031
624d5c51
TH
2032static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
2033 unsigned long deadline)
2cc432ee 2034{
624d5c51 2035 struct ata_ioports *ioaddr = &ap->ioaddr;
2cc432ee 2036
624d5c51
TH
2037 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
2038
2039 /* software reset. causes dev0 to be selected */
2040 iowrite8(ap->ctl, ioaddr->ctl_addr);
2041 udelay(20); /* FIXME: flush */
2042 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2043 udelay(20); /* FIXME: flush */
2044 iowrite8(ap->ctl, ioaddr->ctl_addr);
2045
705e76be
TH
2046 /* wait the port to become ready */
2047 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
2cc432ee
JG
2048}
2049
6d97dbd7 2050/**
9363c382 2051 * ata_sff_softreset - reset host port via ATA SRST
624d5c51
TH
2052 * @link: ATA link to reset
2053 * @classes: resulting classes of attached devices
2054 * @deadline: deadline jiffies for the operation
6d97dbd7 2055 *
624d5c51 2056 * Reset host port using ATA SRST.
6d97dbd7
TH
2057 *
2058 * LOCKING:
624d5c51
TH
2059 * Kernel thread context (may sleep)
2060 *
2061 * RETURNS:
2062 * 0 on success, -errno otherwise.
6d97dbd7 2063 */
9363c382 2064int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
624d5c51 2065 unsigned long deadline)
6d97dbd7 2066{
624d5c51
TH
2067 struct ata_port *ap = link->ap;
2068 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2069 unsigned int devmask = 0;
2070 int rc;
2071 u8 err;
6d97dbd7 2072
624d5c51 2073 DPRINTK("ENTER\n");
6d97dbd7 2074
624d5c51
TH
2075 /* determine if device 0/1 are present */
2076 if (ata_devchk(ap, 0))
2077 devmask |= (1 << 0);
2078 if (slave_possible && ata_devchk(ap, 1))
2079 devmask |= (1 << 1);
2080
2081 /* select device 0 again */
5682ed33 2082 ap->ops->sff_dev_select(ap, 0);
624d5c51
TH
2083
2084 /* issue bus reset */
2085 DPRINTK("about to softreset, devmask=%x\n", devmask);
2086 rc = ata_bus_softreset(ap, devmask, deadline);
2087 /* if link is occupied, -ENODEV too is an error */
2088 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
2089 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
2090 return rc;
2091 }
0f0a3ad3 2092
624d5c51 2093 /* determine by signature whether we have ATA or ATAPI devices */
9363c382 2094 classes[0] = ata_sff_dev_classify(&link->device[0],
624d5c51
TH
2095 devmask & (1 << 0), &err);
2096 if (slave_possible && err != 0x81)
9363c382 2097 classes[1] = ata_sff_dev_classify(&link->device[1],
624d5c51
TH
2098 devmask & (1 << 1), &err);
2099
624d5c51
TH
2100 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2101 return 0;
6d97dbd7 2102}
0fe40ff8 2103EXPORT_SYMBOL_GPL(ata_sff_softreset);
6d97dbd7
TH
2104
2105/**
9363c382 2106 * sata_sff_hardreset - reset host port via SATA phy reset
624d5c51
TH
2107 * @link: link to reset
2108 * @class: resulting class of attached device
2109 * @deadline: deadline jiffies for the operation
6d97dbd7 2110 *
624d5c51
TH
2111 * SATA phy-reset host port using DET bits of SControl register,
2112 * wait for !BSY and classify the attached device.
6d97dbd7
TH
2113 *
2114 * LOCKING:
624d5c51
TH
2115 * Kernel thread context (may sleep)
2116 *
2117 * RETURNS:
2118 * 0 on success, -errno otherwise.
6d97dbd7 2119 */
9363c382 2120int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
624d5c51 2121 unsigned long deadline)
6d97dbd7 2122{
9dadd45b
TH
2123 struct ata_eh_context *ehc = &link->eh_context;
2124 const unsigned long *timing = sata_ehc_deb_timing(ehc);
2125 bool online;
624d5c51
TH
2126 int rc;
2127
9dadd45b
TH
2128 rc = sata_link_hardreset(link, timing, deadline, &online,
2129 ata_sff_check_ready);
9dadd45b
TH
2130 if (online)
2131 *class = ata_sff_dev_classify(link->device, 1, NULL);
624d5c51
TH
2132
2133 DPRINTK("EXIT, class=%u\n", *class);
9dadd45b 2134 return rc;
6d97dbd7 2135}
0fe40ff8 2136EXPORT_SYMBOL_GPL(sata_sff_hardreset);
6d97dbd7 2137
203c75b8
TH
2138/**
2139 * ata_sff_postreset - SFF postreset callback
2140 * @link: the target SFF ata_link
2141 * @classes: classes of attached devices
2142 *
2143 * This function is invoked after a successful reset. It first
2144 * calls ata_std_postreset() and performs SFF specific postreset
2145 * processing.
2146 *
2147 * LOCKING:
2148 * Kernel thread context (may sleep)
2149 */
2150void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
2151{
2152 struct ata_port *ap = link->ap;
2153
2154 ata_std_postreset(link, classes);
2155
2156 /* is double-select really necessary? */
2157 if (classes[0] != ATA_DEV_NONE)
2158 ap->ops->sff_dev_select(ap, 1);
2159 if (classes[1] != ATA_DEV_NONE)
2160 ap->ops->sff_dev_select(ap, 0);
2161
2162 /* bail out if no device is present */
2163 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2164 DPRINTK("EXIT, no device\n");
2165 return;
2166 }
2167
2168 /* set up device control */
2169 if (ap->ioaddr.ctl_addr)
2170 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
2171}
0fe40ff8 2172EXPORT_SYMBOL_GPL(ata_sff_postreset);
203c75b8 2173
6d97dbd7 2174/**
9363c382 2175 * ata_sff_error_handler - Stock error handler for BMDMA controller
6d97dbd7 2176 * @ap: port to handle error for
6d97dbd7 2177 *
9363c382 2178 * Stock error handler for SFF controller. It can handle both
6d97dbd7
TH
2179 * PATA and SATA controllers. Many controllers should be able to
2180 * use this EH as-is or with some added handling before and
2181 * after.
2182 *
6d97dbd7
TH
2183 * LOCKING:
2184 * Kernel thread context (may sleep)
2185 */
9363c382 2186void ata_sff_error_handler(struct ata_port *ap)
6d97dbd7 2187{
a1efdaba
TH
2188 ata_reset_fn_t softreset = ap->ops->softreset;
2189 ata_reset_fn_t hardreset = ap->ops->hardreset;
6d97dbd7
TH
2190 struct ata_queued_cmd *qc;
2191 unsigned long flags;
2192 int thaw = 0;
2193
9af5c9c9 2194 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
6d97dbd7
TH
2195 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2196 qc = NULL;
2197
2198 /* reset PIO HSM and stop DMA engine */
ba6a1308 2199 spin_lock_irqsave(ap->lock, flags);
6d97dbd7 2200
6d97dbd7
TH
2201 ap->hsm_task_state = HSM_ST_IDLE;
2202
ed82f964
TH
2203 if (ap->ioaddr.bmdma_addr &&
2204 qc && (qc->tf.protocol == ATA_PROT_DMA ||
0dc36888 2205 qc->tf.protocol == ATAPI_PROT_DMA)) {
6d97dbd7
TH
2206 u8 host_stat;
2207
fbbb262d 2208 host_stat = ap->ops->bmdma_status(ap);
6d97dbd7 2209
6d97dbd7
TH
2210 /* BMDMA controllers indicate host bus error by
2211 * setting DMA_ERR bit and timing out. As it wasn't
2212 * really a timeout event, adjust error mask and
2213 * cancel frozen state.
2214 */
18d90deb 2215 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
6d97dbd7
TH
2216 qc->err_mask = AC_ERR_HOST_BUS;
2217 thaw = 1;
2218 }
2219
2220 ap->ops->bmdma_stop(qc);
2221 }
2222
a57c1bad 2223 ata_sff_sync(ap); /* FIXME: We don't need this */
5682ed33
TH
2224 ap->ops->sff_check_status(ap);
2225 ap->ops->sff_irq_clear(ap);
6d97dbd7 2226
ba6a1308 2227 spin_unlock_irqrestore(ap->lock, flags);
6d97dbd7
TH
2228
2229 if (thaw)
2230 ata_eh_thaw_port(ap);
2231
2232 /* PIO and DMA engines have been stopped, perform recovery */
6d97dbd7 2233
57c9efdf
TH
2234 /* Ignore ata_sff_softreset if ctl isn't accessible and
2235 * built-in hardresets if SCR access isn't available.
a1efdaba 2236 */
9363c382 2237 if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
a1efdaba 2238 softreset = NULL;
57c9efdf 2239 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
a1efdaba 2240 hardreset = NULL;
6d97dbd7 2241
a1efdaba
TH
2242 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2243 ap->ops->postreset);
6d97dbd7 2244}
0fe40ff8 2245EXPORT_SYMBOL_GPL(ata_sff_error_handler);
6d97dbd7
TH
2246
2247/**
9363c382 2248 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
6d97dbd7
TH
2249 * @qc: internal command to clean up
2250 *
2251 * LOCKING:
2252 * Kernel thread context (may sleep)
2253 */
9363c382 2254void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
6d97dbd7 2255{
570106df
TH
2256 struct ata_port *ap = qc->ap;
2257 unsigned long flags;
2258
2259 spin_lock_irqsave(ap->lock, flags);
2260
2261 ap->hsm_task_state = HSM_ST_IDLE;
2262
2263 if (ap->ioaddr.bmdma_addr)
61dd08c6 2264 ata_bmdma_stop(qc);
570106df
TH
2265
2266 spin_unlock_irqrestore(ap->lock, flags);
6d97dbd7 2267}
0fe40ff8 2268EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
6d97dbd7 2269
d92e74d3
AC
2270/**
2271 * ata_sff_port_start - Set port up for dma.
2272 * @ap: Port to initialize
2273 *
2274 * Called just after data structures for each port are
2275 * initialized. Allocates space for PRD table if the device
2276 * is DMA capable SFF.
2277 *
2278 * May be used as the port_start() entry in ata_port_operations.
2279 *
2280 * LOCKING:
2281 * Inherited from caller.
2282 */
d92e74d3
AC
2283int ata_sff_port_start(struct ata_port *ap)
2284{
2285 if (ap->ioaddr.bmdma_addr)
2286 return ata_port_start(ap);
2287 return 0;
2288}
0fe40ff8 2289EXPORT_SYMBOL_GPL(ata_sff_port_start);
d92e74d3 2290
624d5c51 2291/**
9363c382 2292 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
624d5c51
TH
2293 * @ioaddr: IO address structure to be initialized
2294 *
2295 * Utility function which initializes data_addr, error_addr,
2296 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2297 * device_addr, status_addr, and command_addr to standard offsets
2298 * relative to cmd_addr.
2299 *
2300 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2301 */
9363c382 2302void ata_sff_std_ports(struct ata_ioports *ioaddr)
624d5c51
TH
2303{
2304 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2305 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2306 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2307 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2308 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2309 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2310 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2311 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2312 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2313 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2314}
0fe40ff8 2315EXPORT_SYMBOL_GPL(ata_sff_std_ports);
624d5c51 2316
9363c382
TH
2317unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
2318 unsigned long xfer_mask)
071ce34d
TH
2319{
2320 /* Filter out DMA modes if the device has been configured by
2321 the BIOS as PIO only */
2322
2323 if (adev->link->ap->ioaddr.bmdma_addr == NULL)
2324 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2325 return xfer_mask;
2326}
0fe40ff8 2327EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
071ce34d 2328
272f7884
TH
2329/**
2330 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2331 * @qc: Info associated with this ATA transaction.
2332 *
2333 * LOCKING:
2334 * spin_lock_irqsave(host lock)
2335 */
2336void ata_bmdma_setup(struct ata_queued_cmd *qc)
2337{
2338 struct ata_port *ap = qc->ap;
2339 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2340 u8 dmactl;
2341
2342 /* load PRD table addr. */
2343 mb(); /* make sure PRD table writes are visible to controller */
2344 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2345
2346 /* specify data direction, triple-check start bit is clear */
2347 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2348 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2349 if (!rw)
2350 dmactl |= ATA_DMA_WR;
2351 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2352
2353 /* issue r/w command */
5682ed33 2354 ap->ops->sff_exec_command(ap, &qc->tf);
272f7884 2355}
0fe40ff8 2356EXPORT_SYMBOL_GPL(ata_bmdma_setup);
272f7884
TH
2357
2358/**
2359 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2360 * @qc: Info associated with this ATA transaction.
2361 *
2362 * LOCKING:
2363 * spin_lock_irqsave(host lock)
2364 */
2365void ata_bmdma_start(struct ata_queued_cmd *qc)
2366{
2367 struct ata_port *ap = qc->ap;
2368 u8 dmactl;
2369
2370 /* start host DMA transaction */
2371 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2372 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2373
2374 /* Strictly, one may wish to issue an ioread8() here, to
2375 * flush the mmio write. However, control also passes
2376 * to the hardware at this point, and it will interrupt
2377 * us when we are to resume control. So, in effect,
2378 * we don't care when the mmio write flushes.
2379 * Further, a read of the DMA status register _immediately_
2380 * following the write may not be what certain flaky hardware
2381 * is expected, so I think it is best to not add a readb()
2382 * without first all the MMIO ATA cards/mobos.
2383 * Or maybe I'm just being paranoid.
2384 *
2385 * FIXME: The posting of this write means I/O starts are
2386 * unneccessarily delayed for MMIO
2387 */
2388}
0fe40ff8 2389EXPORT_SYMBOL_GPL(ata_bmdma_start);
272f7884
TH
2390
2391/**
2392 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2393 * @qc: Command we are ending DMA for
2394 *
2395 * Clears the ATA_DMA_START flag in the dma control register
2396 *
2397 * May be used as the bmdma_stop() entry in ata_port_operations.
2398 *
2399 * LOCKING:
2400 * spin_lock_irqsave(host lock)
2401 */
2402void ata_bmdma_stop(struct ata_queued_cmd *qc)
2403{
2404 struct ata_port *ap = qc->ap;
2405 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2406
2407 /* clear start/stop bit */
2408 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2409 mmio + ATA_DMA_CMD);
2410
2411 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
a57c1bad 2412 ata_sff_dma_pause(ap);
272f7884 2413}
0fe40ff8 2414EXPORT_SYMBOL_GPL(ata_bmdma_stop);
272f7884
TH
2415
2416/**
2417 * ata_bmdma_status - Read PCI IDE BMDMA status
2418 * @ap: Port associated with this ATA transaction.
2419 *
2420 * Read and return BMDMA status register.
2421 *
2422 * May be used as the bmdma_status() entry in ata_port_operations.
2423 *
2424 * LOCKING:
2425 * spin_lock_irqsave(host lock)
2426 */
2427u8 ata_bmdma_status(struct ata_port *ap)
2428{
2429 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2430}
0fe40ff8 2431EXPORT_SYMBOL_GPL(ata_bmdma_status);
272f7884
TH
2432
2433/**
624d5c51
TH
2434 * ata_bus_reset - reset host port and associated ATA channel
2435 * @ap: port to reset
2436 *
2437 * This is typically the first time we actually start issuing
2438 * commands to the ATA channel. We wait for BSY to clear, then
2439 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2440 * result. Determine what devices, if any, are on the channel
2441 * by looking at the device 0/1 error register. Look at the signature
2442 * stored in each device's taskfile registers, to determine if
2443 * the device is ATA or ATAPI.
2444 *
2445 * LOCKING:
2446 * PCI/etc. bus probe sem.
2447 * Obtains host lock.
2448 *
2449 * SIDE EFFECTS:
2450 * Sets ATA_FLAG_DISABLED if bus reset fails.
2451 *
2452 * DEPRECATED:
2453 * This function is only for drivers which still use old EH and
2454 * will be removed soon.
272f7884 2455 */
624d5c51 2456void ata_bus_reset(struct ata_port *ap)
272f7884 2457{
624d5c51
TH
2458 struct ata_device *device = ap->link.device;
2459 struct ata_ioports *ioaddr = &ap->ioaddr;
2460 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2461 u8 err;
2462 unsigned int dev0, dev1 = 0, devmask = 0;
2463 int rc;
2464
2465 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
2466
2467 /* determine if device 0/1 are present */
2468 if (ap->flags & ATA_FLAG_SATA_RESET)
2469 dev0 = 1;
2470 else {
2471 dev0 = ata_devchk(ap, 0);
2472 if (slave_possible)
2473 dev1 = ata_devchk(ap, 1);
2474 }
2475
2476 if (dev0)
2477 devmask |= (1 << 0);
2478 if (dev1)
2479 devmask |= (1 << 1);
2480
2481 /* select device 0 again */
5682ed33 2482 ap->ops->sff_dev_select(ap, 0);
624d5c51
TH
2483
2484 /* issue bus reset */
2485 if (ap->flags & ATA_FLAG_SRST) {
341c2c95
TH
2486 rc = ata_bus_softreset(ap, devmask,
2487 ata_deadline(jiffies, 40000));
624d5c51
TH
2488 if (rc && rc != -ENODEV)
2489 goto err_out;
2490 }
2491
2492 /*
2493 * determine by signature whether we have ATA or ATAPI devices
2494 */
9363c382 2495 device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
624d5c51 2496 if ((slave_possible) && (err != 0x81))
9363c382 2497 device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
624d5c51
TH
2498
2499 /* is double-select really necessary? */
2500 if (device[1].class != ATA_DEV_NONE)
5682ed33 2501 ap->ops->sff_dev_select(ap, 1);
624d5c51 2502 if (device[0].class != ATA_DEV_NONE)
5682ed33 2503 ap->ops->sff_dev_select(ap, 0);
624d5c51
TH
2504
2505 /* if no devices were detected, disable this port */
2506 if ((device[0].class == ATA_DEV_NONE) &&
2507 (device[1].class == ATA_DEV_NONE))
2508 goto err_out;
2509
2510 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2511 /* set up device control for ATA_FLAG_SATA_RESET */
2512 iowrite8(ap->ctl, ioaddr->ctl_addr);
2513 }
2514
2515 DPRINTK("EXIT\n");
2516 return;
2517
2518err_out:
2519 ata_port_printk(ap, KERN_ERR, "disabling port\n");
2520 ata_port_disable(ap);
2521
2522 DPRINTK("EXIT\n");
272f7884 2523}
0fe40ff8 2524EXPORT_SYMBOL_GPL(ata_bus_reset);
272f7884 2525
1fdffbce 2526#ifdef CONFIG_PCI
4112e16a 2527
272f7884 2528/**
9363c382 2529 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
272f7884
TH
2530 * @pdev: PCI device
2531 *
2532 * Some PCI ATA devices report simplex mode but in fact can be told to
2533 * enter non simplex mode. This implements the necessary logic to
2534 * perform the task on such devices. Calling it on other devices will
2535 * have -undefined- behaviour.
2536 */
9363c382 2537int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
4112e16a 2538{
272f7884
TH
2539 unsigned long bmdma = pci_resource_start(pdev, 4);
2540 u8 simplex;
a84471fe 2541
272f7884
TH
2542 if (bmdma == 0)
2543 return -ENOENT;
2544
2545 simplex = inb(bmdma + 0x02);
2546 outb(simplex & 0x60, bmdma + 0x02);
2547 simplex = inb(bmdma + 0x02);
2548 if (simplex & 0x80)
2549 return -EOPNOTSUPP;
2550 return 0;
2551}
0fe40ff8 2552EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
272f7884 2553
0f834de3 2554/**
9363c382 2555 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
0f834de3
TH
2556 * @host: target ATA host
2557 *
2558 * Acquire PCI BMDMA resources and initialize @host accordingly.
2559 *
2560 * LOCKING:
2561 * Inherited from calling layer (may sleep).
2562 *
2563 * RETURNS:
2564 * 0 on success, -errno otherwise.
2565 */
9363c382 2566int ata_pci_bmdma_init(struct ata_host *host)
1fdffbce 2567{
0f834de3
TH
2568 struct device *gdev = host->dev;
2569 struct pci_dev *pdev = to_pci_dev(gdev);
2570 int i, rc;
0d5ff566 2571
6fdc99a2
AC
2572 /* No BAR4 allocation: No DMA */
2573 if (pci_resource_start(pdev, 4) == 0)
2574 return 0;
2575
0f834de3
TH
2576 /* TODO: If we get no DMA mask we should fall back to PIO */
2577 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
2578 if (rc)
2579 return rc;
2580 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
2581 if (rc)
2582 return rc;
2583
2584 /* request and iomap DMA region */
35a10a80 2585 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
0f834de3
TH
2586 if (rc) {
2587 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
2588 return -ENOMEM;
0d5ff566 2589 }
0f834de3 2590 host->iomap = pcim_iomap_table(pdev);
0d5ff566 2591
1626aeb8 2592 for (i = 0; i < 2; i++) {
0f834de3 2593 struct ata_port *ap = host->ports[i];
0f834de3
TH
2594 void __iomem *bmdma = host->iomap[4] + 8 * i;
2595
2596 if (ata_port_is_dummy(ap))
2597 continue;
2598
21b0ad4f 2599 ap->ioaddr.bmdma_addr = bmdma;
0f834de3
TH
2600 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
2601 (ioread8(bmdma + 2) & 0x80))
2602 host->flags |= ATA_HOST_SIMPLEX;
cbcdd875
TH
2603
2604 ata_port_desc(ap, "bmdma 0x%llx",
0fe40ff8 2605 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
0d5ff566
TH
2606 }
2607
0f834de3
TH
2608 return 0;
2609}
0fe40ff8 2610EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
2ec7df04 2611
272f7884
TH
2612static int ata_resources_present(struct pci_dev *pdev, int port)
2613{
2614 int i;
2615
2616 /* Check the PCI resources for this channel are enabled */
2617 port = port * 2;
0fe40ff8 2618 for (i = 0; i < 2; i++) {
272f7884
TH
2619 if (pci_resource_start(pdev, port + i) == 0 ||
2620 pci_resource_len(pdev, port + i) == 0)
2621 return 0;
2622 }
2623 return 1;
2624}
2625
d491b27b 2626/**
9363c382 2627 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
d491b27b 2628 * @host: target ATA host
d491b27b 2629 *
1626aeb8
TH
2630 * Acquire native PCI ATA resources for @host and initialize the
2631 * first two ports of @host accordingly. Ports marked dummy are
2632 * skipped and allocation failure makes the port dummy.
d491b27b 2633 *
d583bc18
TH
2634 * Note that native PCI resources are valid even for legacy hosts
2635 * as we fix up pdev resources array early in boot, so this
2636 * function can be used for both native and legacy SFF hosts.
2637 *
d491b27b
TH
2638 * LOCKING:
2639 * Inherited from calling layer (may sleep).
2640 *
2641 * RETURNS:
1626aeb8
TH
2642 * 0 if at least one port is initialized, -ENODEV if no port is
2643 * available.
d491b27b 2644 */
9363c382 2645int ata_pci_sff_init_host(struct ata_host *host)
d491b27b
TH
2646{
2647 struct device *gdev = host->dev;
2648 struct pci_dev *pdev = to_pci_dev(gdev);
1626aeb8 2649 unsigned int mask = 0;
d491b27b
TH
2650 int i, rc;
2651
d491b27b
TH
2652 /* request, iomap BARs and init port addresses accordingly */
2653 for (i = 0; i < 2; i++) {
2654 struct ata_port *ap = host->ports[i];
2655 int base = i * 2;
2656 void __iomem * const *iomap;
2657
1626aeb8
TH
2658 if (ata_port_is_dummy(ap))
2659 continue;
2660
2661 /* Discard disabled ports. Some controllers show
2662 * their unused channels this way. Disabled ports are
2663 * made dummy.
2664 */
2665 if (!ata_resources_present(pdev, i)) {
2666 ap->ops = &ata_dummy_port_ops;
d491b27b 2667 continue;
1626aeb8 2668 }
d491b27b 2669
35a10a80
TH
2670 rc = pcim_iomap_regions(pdev, 0x3 << base,
2671 dev_driver_string(gdev));
d491b27b 2672 if (rc) {
1626aeb8
TH
2673 dev_printk(KERN_WARNING, gdev,
2674 "failed to request/iomap BARs for port %d "
2675 "(errno=%d)\n", i, rc);
d491b27b
TH
2676 if (rc == -EBUSY)
2677 pcim_pin_device(pdev);
1626aeb8
TH
2678 ap->ops = &ata_dummy_port_ops;
2679 continue;
d491b27b
TH
2680 }
2681 host->iomap = iomap = pcim_iomap_table(pdev);
2682
2683 ap->ioaddr.cmd_addr = iomap[base];
2684 ap->ioaddr.altstatus_addr =
2685 ap->ioaddr.ctl_addr = (void __iomem *)
2686 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
9363c382 2687 ata_sff_std_ports(&ap->ioaddr);
1626aeb8 2688
cbcdd875
TH
2689 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2690 (unsigned long long)pci_resource_start(pdev, base),
2691 (unsigned long long)pci_resource_start(pdev, base + 1));
2692
1626aeb8
TH
2693 mask |= 1 << i;
2694 }
2695
2696 if (!mask) {
2697 dev_printk(KERN_ERR, gdev, "no available native port\n");
2698 return -ENODEV;
d491b27b
TH
2699 }
2700
2701 return 0;
2702}
0fe40ff8 2703EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
d491b27b 2704
21b0ad4f 2705/**
9363c382 2706 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
21b0ad4f 2707 * @pdev: target PCI device
1626aeb8 2708 * @ppi: array of port_info, must be enough for two ports
21b0ad4f
TH
2709 * @r_host: out argument for the initialized ATA host
2710 *
2711 * Helper to allocate ATA host for @pdev, acquire all native PCI
2712 * resources and initialize it accordingly in one go.
2713 *
2714 * LOCKING:
2715 * Inherited from calling layer (may sleep).
2716 *
2717 * RETURNS:
2718 * 0 on success, -errno otherwise.
2719 */
9363c382 2720int ata_pci_sff_prepare_host(struct pci_dev *pdev,
0fe40ff8 2721 const struct ata_port_info * const *ppi,
d583bc18 2722 struct ata_host **r_host)
21b0ad4f
TH
2723{
2724 struct ata_host *host;
21b0ad4f
TH
2725 int rc;
2726
2727 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2728 return -ENOMEM;
2729
2730 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2731 if (!host) {
2732 dev_printk(KERN_ERR, &pdev->dev,
2733 "failed to allocate ATA host\n");
2734 rc = -ENOMEM;
2735 goto err_out;
2736 }
2737
9363c382 2738 rc = ata_pci_sff_init_host(host);
21b0ad4f
TH
2739 if (rc)
2740 goto err_out;
2741
2742 /* init DMA related stuff */
9363c382 2743 rc = ata_pci_bmdma_init(host);
21b0ad4f
TH
2744 if (rc)
2745 goto err_bmdma;
2746
2747 devres_remove_group(&pdev->dev, NULL);
2748 *r_host = host;
2749 return 0;
2750
0fe40ff8 2751err_bmdma:
21b0ad4f
TH
2752 /* This is necessary because PCI and iomap resources are
2753 * merged and releasing the top group won't release the
2754 * acquired resources if some of those have been acquired
2755 * before entering this function.
2756 */
2757 pcim_iounmap_regions(pdev, 0xf);
0fe40ff8 2758err_out:
21b0ad4f
TH
2759 devres_release_group(&pdev->dev, NULL);
2760 return rc;
2761}
0fe40ff8 2762EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
21b0ad4f 2763
4e6b79fa 2764/**
9363c382 2765 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
4e6b79fa
TH
2766 * @host: target SFF ATA host
2767 * @irq_handler: irq_handler used when requesting IRQ(s)
2768 * @sht: scsi_host_template to use when registering the host
2769 *
2770 * This is the counterpart of ata_host_activate() for SFF ATA
2771 * hosts. This separate helper is necessary because SFF hosts
2772 * use two separate interrupts in legacy mode.
2773 *
2774 * LOCKING:
2775 * Inherited from calling layer (may sleep).
2776 *
2777 * RETURNS:
2778 * 0 on success, -errno otherwise.
2779 */
9363c382 2780int ata_pci_sff_activate_host(struct ata_host *host,
4e6b79fa
TH
2781 irq_handler_t irq_handler,
2782 struct scsi_host_template *sht)
2783{
2784 struct device *dev = host->dev;
2785 struct pci_dev *pdev = to_pci_dev(dev);
2786 const char *drv_name = dev_driver_string(host->dev);
2787 int legacy_mode = 0, rc;
2788
2789 rc = ata_host_start(host);
2790 if (rc)
2791 return rc;
2792
2793 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
2794 u8 tmp8, mask;
2795
2796 /* TODO: What if one channel is in native mode ... */
2797 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
2798 mask = (1 << 2) | (1 << 0);
2799 if ((tmp8 & mask) != mask)
2800 legacy_mode = 1;
2801#if defined(CONFIG_NO_ATA_LEGACY)
2802 /* Some platforms with PCI limits cannot address compat
2803 port space. In that case we punt if their firmware has
2804 left a device in compatibility mode */
2805 if (legacy_mode) {
2806 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
2807 return -EOPNOTSUPP;
2808 }
2809#endif
2810 }
2811
2812 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2813 return -ENOMEM;
2814
2815 if (!legacy_mode && pdev->irq) {
2816 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2817 IRQF_SHARED, drv_name, host);
2818 if (rc)
2819 goto out;
2820
2821 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
2822 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
2823 } else if (legacy_mode) {
2824 if (!ata_port_is_dummy(host->ports[0])) {
2825 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2826 irq_handler, IRQF_SHARED,
2827 drv_name, host);
2828 if (rc)
2829 goto out;
2830
2831 ata_port_desc(host->ports[0], "irq %d",
2832 ATA_PRIMARY_IRQ(pdev));
2833 }
2834
2835 if (!ata_port_is_dummy(host->ports[1])) {
2836 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2837 irq_handler, IRQF_SHARED,
2838 drv_name, host);
2839 if (rc)
2840 goto out;
2841
2842 ata_port_desc(host->ports[1], "irq %d",
2843 ATA_SECONDARY_IRQ(pdev));
2844 }
2845 }
2846
2847 rc = ata_host_register(host, sht);
0fe40ff8 2848out:
4e6b79fa
TH
2849 if (rc == 0)
2850 devres_remove_group(dev, NULL);
2851 else
2852 devres_release_group(dev, NULL);
2853
2854 return rc;
2855}
0fe40ff8 2856EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
4e6b79fa 2857
1fdffbce 2858/**
9363c382 2859 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
1fdffbce 2860 * @pdev: Controller to be initialized
1626aeb8 2861 * @ppi: array of port_info, must be enough for two ports
1bd5b715 2862 * @sht: scsi_host_template to use when registering the host
887125e3 2863 * @host_priv: host private_data
1fdffbce
JG
2864 *
2865 * This is a helper function which can be called from a driver's
2866 * xxx_init_one() probe function if the hardware uses traditional
2867 * IDE taskfile registers.
2868 *
2869 * This function calls pci_enable_device(), reserves its register
2870 * regions, sets the dma mask, enables bus master mode, and calls
2871 * ata_device_add()
2872 *
2ec7df04
AC
2873 * ASSUMPTION:
2874 * Nobody makes a single channel controller that appears solely as
2875 * the secondary legacy port on PCI.
2876 *
1fdffbce
JG
2877 * LOCKING:
2878 * Inherited from PCI layer (may sleep).
2879 *
2880 * RETURNS:
2881 * Zero on success, negative on errno-based value on error.
2882 */
9363c382 2883int ata_pci_sff_init_one(struct pci_dev *pdev,
0fe40ff8 2884 const struct ata_port_info * const *ppi,
9363c382 2885 struct scsi_host_template *sht, void *host_priv)
1fdffbce 2886{
f0d36efd 2887 struct device *dev = &pdev->dev;
1626aeb8 2888 const struct ata_port_info *pi = NULL;
0f834de3 2889 struct ata_host *host = NULL;
1626aeb8 2890 int i, rc;
1fdffbce
JG
2891
2892 DPRINTK("ENTER\n");
2893
1626aeb8
TH
2894 /* look up the first valid port_info */
2895 for (i = 0; i < 2 && ppi[i]; i++) {
2896 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
2897 pi = ppi[i];
2898 break;
2899 }
2900 }
f0d36efd 2901
1626aeb8
TH
2902 if (!pi) {
2903 dev_printk(KERN_ERR, &pdev->dev,
2904 "no valid port_info specified\n");
2905 return -EINVAL;
2906 }
c791c306 2907
1626aeb8
TH
2908 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2909 return -ENOMEM;
1fdffbce 2910
f0d36efd 2911 rc = pcim_enable_device(pdev);
1fdffbce 2912 if (rc)
4e6b79fa 2913 goto out;
1fdffbce 2914
4e6b79fa 2915 /* prepare and activate SFF host */
9363c382 2916 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
d583bc18 2917 if (rc)
4e6b79fa 2918 goto out;
887125e3 2919 host->private_data = host_priv;
d491b27b 2920
d491b27b 2921 pci_set_master(pdev);
9363c382 2922 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
0fe40ff8 2923out:
4e6b79fa
TH
2924 if (rc == 0)
2925 devres_remove_group(&pdev->dev, NULL);
2926 else
2927 devres_release_group(&pdev->dev, NULL);
d491b27b 2928
1fdffbce
JG
2929 return rc;
2930}
0fe40ff8 2931EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
1fdffbce
JG
2932
2933#endif /* CONFIG_PCI */
2934