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