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