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