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