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