Merge branch 'upstream'
[sfrench/cifs-2.6.git] / include / linux / libata.h
1 /*
2  *  Copyright 2003-2005 Red Hat, Inc.  All rights reserved.
3  *  Copyright 2003-2005 Jeff Garzik
4  *
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; see the file COPYING.  If not, write to
18  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  *
21  *  libata documentation is available via 'make {ps|pdf}docs',
22  *  as Documentation/DocBook/libata.*
23  *
24  */
25
26 #ifndef __LINUX_LIBATA_H__
27 #define __LINUX_LIBATA_H__
28
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/pci.h>
32 #include <linux/dma-mapping.h>
33 #include <asm/io.h>
34 #include <linux/ata.h>
35 #include <linux/workqueue.h>
36
37 /*
38  * compile-time options
39  */
40 #undef ATA_DEBUG                /* debugging output */
41 #undef ATA_VERBOSE_DEBUG        /* yet more debugging output */
42 #undef ATA_IRQ_TRAP             /* define to ack screaming irqs */
43 #undef ATA_NDEBUG               /* define to disable quick runtime checks */
44 #undef ATA_ENABLE_PATA          /* define to enable PATA support in some
45                                  * low-level drivers */
46 #undef ATAPI_ENABLE_DMADIR      /* enables ATAPI DMADIR bridge support */
47
48
49 /* note: prints function name for you */
50 #ifdef ATA_DEBUG
51 #define DPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ## args)
52 #ifdef ATA_VERBOSE_DEBUG
53 #define VPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ## args)
54 #else
55 #define VPRINTK(fmt, args...)
56 #endif  /* ATA_VERBOSE_DEBUG */
57 #else
58 #define DPRINTK(fmt, args...)
59 #define VPRINTK(fmt, args...)
60 #endif  /* ATA_DEBUG */
61
62 #define BPRINTK(fmt, args...) if (ap->flags & ATA_FLAG_DEBUGMSG) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ## args)
63
64 #ifdef ATA_NDEBUG
65 #define assert(expr)
66 #else
67 #define assert(expr) \
68         if(unlikely(!(expr))) {                                   \
69         printk(KERN_ERR "Assertion failed! %s,%s,%s,line=%d\n", \
70         #expr,__FILE__,__FUNCTION__,__LINE__);          \
71         }
72 #endif
73
74 /* defines only for the constants which don't work well as enums */
75 #define ATA_TAG_POISON          0xfafbfcfdU
76
77 /* move to PCI layer? */
78 static inline struct device *pci_dev_to_dev(struct pci_dev *pdev)
79 {
80         return &pdev->dev;
81 }
82
83 enum {
84         /* various global constants */
85         LIBATA_MAX_PRD          = ATA_MAX_PRD / 2,
86         ATA_MAX_PORTS           = 8,
87         ATA_DEF_QUEUE           = 1,
88         ATA_MAX_QUEUE           = 1,
89         ATA_MAX_SECTORS         = 200,  /* FIXME */
90         ATA_MAX_BUS             = 2,
91         ATA_DEF_BUSY_WAIT       = 10000,
92         ATA_SHORT_PAUSE         = (HZ >> 6) + 1,
93
94         ATA_SHT_EMULATED        = 1,
95         ATA_SHT_CMD_PER_LUN     = 1,
96         ATA_SHT_THIS_ID         = -1,
97         ATA_SHT_USE_CLUSTERING  = 1,
98
99         /* struct ata_device stuff */
100         ATA_DFLAG_LBA48         = (1 << 0), /* device supports LBA48 */
101         ATA_DFLAG_PIO           = (1 << 1), /* device currently in PIO mode */
102         ATA_DFLAG_LOCK_SECTORS  = (1 << 2), /* don't adjust max_sectors */
103         ATA_DFLAG_LBA           = (1 << 3), /* device supports LBA */
104         ATA_DFLAG_CDB_INTR      = (1 << 4), /* device asserts INTRQ when ready for CDB */
105
106         ATA_DEV_UNKNOWN         = 0,    /* unknown device */
107         ATA_DEV_ATA             = 1,    /* ATA device */
108         ATA_DEV_ATA_UNSUP       = 2,    /* ATA device (unsupported) */
109         ATA_DEV_ATAPI           = 3,    /* ATAPI device */
110         ATA_DEV_ATAPI_UNSUP     = 4,    /* ATAPI device (unsupported) */
111         ATA_DEV_NONE            = 5,    /* no device */
112
113         /* struct ata_port flags */
114         ATA_FLAG_SLAVE_POSS     = (1 << 1), /* host supports slave dev */
115                                             /* (doesn't imply presence) */
116         ATA_FLAG_PORT_DISABLED  = (1 << 2), /* port is disabled, ignore it */
117         ATA_FLAG_SATA           = (1 << 3),
118         ATA_FLAG_NO_LEGACY      = (1 << 4), /* no legacy mode check */
119         ATA_FLAG_SRST           = (1 << 5), /* use ATA SRST, not E.D.D. */
120         ATA_FLAG_MMIO           = (1 << 6), /* use MMIO, not PIO */
121         ATA_FLAG_SATA_RESET     = (1 << 7), /* use COMRESET */
122         ATA_FLAG_PIO_DMA        = (1 << 8), /* PIO cmds via DMA */
123         ATA_FLAG_PIO_POLLING    = (1 << 9), /* use polling PIO if LLD
124                                              * doesn't handle PIO interrupts */
125         ATA_FLAG_DEBUGMSG       = (1 << 10),
126         ATA_FLAG_NO_ATAPI       = (1 << 11), /* No ATAPI support */
127
128         ATA_QCFLAG_ACTIVE       = (1 << 1), /* cmd not yet ack'd to scsi lyer */
129         ATA_QCFLAG_SG           = (1 << 3), /* have s/g table? */
130         ATA_QCFLAG_SINGLE       = (1 << 4), /* no s/g, just a single buffer */
131         ATA_QCFLAG_DMAMAP       = ATA_QCFLAG_SG | ATA_QCFLAG_SINGLE,
132
133         /* various lengths of time */
134         ATA_TMOUT_EDD           = 5 * HZ,       /* hueristic */
135         ATA_TMOUT_PIO           = 30 * HZ,
136         ATA_TMOUT_BOOT          = 30 * HZ,      /* hueristic */
137         ATA_TMOUT_BOOT_QUICK    = 7 * HZ,       /* hueristic */
138         ATA_TMOUT_DATAOUT       = 30 * HZ,
139         ATA_TMOUT_DATAOUT_QUICK = 5 * HZ,
140         ATA_TMOUT_CDB           = 30 * HZ,
141         ATA_TMOUT_CDB_QUICK     = 5 * HZ,
142         ATA_TMOUT_INTERNAL      = 30 * HZ,
143         ATA_TMOUT_INTERNAL_QUICK = 5 * HZ,
144
145         /* ATA bus states */
146         BUS_UNKNOWN             = 0,
147         BUS_DMA                 = 1,
148         BUS_IDLE                = 2,
149         BUS_NOINTR              = 3,
150         BUS_NODATA              = 4,
151         BUS_TIMER               = 5,
152         BUS_PIO                 = 6,
153         BUS_EDD                 = 7,
154         BUS_IDENTIFY            = 8,
155         BUS_PACKET              = 9,
156
157         /* SATA port states */
158         PORT_UNKNOWN            = 0,
159         PORT_ENABLED            = 1,
160         PORT_DISABLED           = 2,
161
162         /* encoding various smaller bitmaps into a single
163          * unsigned long bitmap
164          */
165         ATA_SHIFT_UDMA          = 0,
166         ATA_SHIFT_MWDMA         = 8,
167         ATA_SHIFT_PIO           = 11,
168
169         /* size of buffer to pad xfers ending on unaligned boundaries */
170         ATA_DMA_PAD_SZ          = 4,
171         ATA_DMA_PAD_BUF_SZ      = ATA_DMA_PAD_SZ * ATA_MAX_QUEUE,
172         
173         /* Masks for port functions */
174         ATA_PORT_PRIMARY        = (1 << 0),
175         ATA_PORT_SECONDARY      = (1 << 1),
176 };
177
178 enum hsm_task_states {
179         HSM_ST_UNKNOWN,         /* state unknown */
180         HSM_ST_IDLE,            /* no command on going */
181         HSM_ST_POLL,            /* same as HSM_ST, waits longer */
182         HSM_ST_TMOUT,           /* timeout */
183         HSM_ST,                 /* (waiting the device to) transfer data */
184         HSM_ST_LAST,            /* (waiting the device to) complete command */
185         HSM_ST_LAST_POLL,       /* same as HSM_ST_LAST, waits longer */
186         HSM_ST_ERR,             /* error */
187         HSM_ST_FIRST,           /* (waiting the device to)
188                                    write CDB or first data block */
189 };
190
191 enum ata_completion_errors {
192         AC_ERR_OTHER            = (1 << 0),
193         AC_ERR_DEV              = (1 << 1),
194         AC_ERR_ATA_BUS          = (1 << 2),
195         AC_ERR_HOST_BUS         = (1 << 3),
196 };
197
198 /* forward declarations */
199 struct scsi_device;
200 struct ata_port_operations;
201 struct ata_port;
202 struct ata_queued_cmd;
203
204 /* typedefs */
205 typedef int (*ata_qc_cb_t) (struct ata_queued_cmd *qc);
206
207 struct ata_ioports {
208         unsigned long           cmd_addr;
209         unsigned long           data_addr;
210         unsigned long           error_addr;
211         unsigned long           feature_addr;
212         unsigned long           nsect_addr;
213         unsigned long           lbal_addr;
214         unsigned long           lbam_addr;
215         unsigned long           lbah_addr;
216         unsigned long           device_addr;
217         unsigned long           status_addr;
218         unsigned long           command_addr;
219         unsigned long           altstatus_addr;
220         unsigned long           ctl_addr;
221         unsigned long           bmdma_addr;
222         unsigned long           scr_addr;
223 };
224
225 struct ata_probe_ent {
226         struct list_head        node;
227         struct device           *dev;
228         const struct ata_port_operations *port_ops;
229         struct scsi_host_template *sht;
230         struct ata_ioports      port[ATA_MAX_PORTS];
231         unsigned int            n_ports;
232         unsigned int            hard_port_no;
233         unsigned int            pio_mask;
234         unsigned int            mwdma_mask;
235         unsigned int            udma_mask;
236         unsigned int            legacy_mode;
237         unsigned long           irq;
238         unsigned int            irq_flags;
239         unsigned long           host_flags;
240         void __iomem            *mmio_base;
241         void                    *private_data;
242 };
243
244 struct ata_host_set {
245         spinlock_t              lock;
246         struct device           *dev;
247         unsigned long           irq;
248         void __iomem            *mmio_base;
249         unsigned int            n_ports;
250         void                    *private_data;
251         const struct ata_port_operations *ops;
252         struct ata_port *       ports[0];
253 };
254
255 struct ata_queued_cmd {
256         struct ata_port         *ap;
257         struct ata_device       *dev;
258
259         struct scsi_cmnd        *scsicmd;
260         void                    (*scsidone)(struct scsi_cmnd *);
261
262         struct ata_taskfile     tf;
263         u8                      cdb[ATAPI_CDB_LEN];
264
265         unsigned long           flags;          /* ATA_QCFLAG_xxx */
266         unsigned int            tag;
267         unsigned int            n_elem;
268         unsigned int            orig_n_elem;
269
270         int                     dma_dir;
271
272         unsigned int            pad_len;
273
274         unsigned int            nsect;
275         unsigned int            cursect;
276
277         unsigned int            nbytes;
278         unsigned int            curbytes;
279
280         unsigned int            cursg;
281         unsigned int            cursg_ofs;
282
283         struct scatterlist      sgent;
284         struct scatterlist      pad_sgent;
285         void                    *buf_virt;
286
287         /* DO NOT iterate over __sg manually, use ata_for_each_sg() */
288         struct scatterlist      *__sg;
289
290         unsigned int            err_mask;
291
292         ata_qc_cb_t             complete_fn;
293
294         void                    *private_data;
295 };
296
297 struct ata_host_stats {
298         unsigned long           unhandled_irq;
299         unsigned long           idle_irq;
300         unsigned long           rw_reqbuf;
301 };
302
303 struct ata_device {
304         u64                     n_sectors;      /* size of device, if ATA */
305         unsigned long           flags;          /* ATA_DFLAG_xxx */
306         unsigned int            class;          /* ATA_DEV_xxx */
307         unsigned int            devno;          /* 0 or 1 */
308         u16                     id[ATA_ID_WORDS]; /* IDENTIFY xxx DEVICE data */
309         u8                      pio_mode;
310         u8                      dma_mode;
311         u8                      xfer_mode;
312         unsigned int            xfer_shift;     /* ATA_SHIFT_xxx */
313
314         unsigned int            multi_count;    /* sectors count for
315                                                    READ/WRITE MULTIPLE */
316
317         /* for CHS addressing */
318         u16                     cylinders;      /* Number of cylinders */
319         u16                     heads;          /* Number of heads */
320         u16                     sectors;        /* Number of sectors per track */
321 };
322
323 struct ata_port {
324         struct Scsi_Host        *host;  /* our co-allocated scsi host */
325         const struct ata_port_operations *ops;
326         unsigned long           flags;  /* ATA_FLAG_xxx */
327         unsigned int            id;     /* unique id req'd by scsi midlyr */
328         unsigned int            port_no; /* unique port #; from zero */
329         unsigned int            hard_port_no;   /* hardware port #; from zero */
330
331         struct ata_prd          *prd;    /* our SG list */
332         dma_addr_t              prd_dma; /* and its DMA mapping */
333
334         void                    *pad;   /* array of DMA pad buffers */
335         dma_addr_t              pad_dma;
336
337         struct ata_ioports      ioaddr; /* ATA cmd/ctl/dma register blocks */
338
339         u8                      ctl;    /* cache of ATA control register */
340         u8                      last_ctl;       /* Cache last written value */
341         unsigned int            pio_mask;
342         unsigned int            mwdma_mask;
343         unsigned int            udma_mask;
344         unsigned int            cbl;    /* cable type; ATA_CBL_xxx */
345         unsigned int            cdb_len;
346
347         struct ata_device       device[ATA_MAX_DEVICES];
348
349         struct ata_queued_cmd   qcmd[ATA_MAX_QUEUE];
350         unsigned long           qactive;
351         unsigned int            active_tag;
352
353         struct ata_host_stats   stats;
354         struct ata_host_set     *host_set;
355
356         struct work_struct      pio_task;
357         unsigned int            hsm_task_state;
358         unsigned long           pio_task_timeout;
359
360         void                    *private_data;
361 };
362
363 struct ata_port_operations {
364         void (*port_disable) (struct ata_port *);
365
366         void (*dev_config) (struct ata_port *, struct ata_device *);
367
368         void (*set_piomode) (struct ata_port *, struct ata_device *);
369         void (*set_dmamode) (struct ata_port *, struct ata_device *);
370
371         void (*tf_load) (struct ata_port *ap, const struct ata_taskfile *tf);
372         void (*tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
373
374         void (*exec_command)(struct ata_port *ap, const struct ata_taskfile *tf);
375         u8   (*check_status)(struct ata_port *ap);
376         u8   (*check_altstatus)(struct ata_port *ap);
377         void (*dev_select)(struct ata_port *ap, unsigned int device);
378
379         void (*phy_reset) (struct ata_port *ap);
380         void (*post_set_mode) (struct ata_port *ap);
381
382         int (*check_atapi_dma) (struct ata_queued_cmd *qc);
383
384         void (*bmdma_setup) (struct ata_queued_cmd *qc);
385         void (*bmdma_start) (struct ata_queued_cmd *qc);
386
387         void (*qc_prep) (struct ata_queued_cmd *qc);
388         int (*qc_issue) (struct ata_queued_cmd *qc);
389
390         void (*eng_timeout) (struct ata_port *ap);
391
392         irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
393         void (*irq_clear) (struct ata_port *);
394
395         u32 (*scr_read) (struct ata_port *ap, unsigned int sc_reg);
396         void (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
397                            u32 val);
398
399         int (*port_start) (struct ata_port *ap);
400         void (*port_stop) (struct ata_port *ap);
401
402         void (*host_stop) (struct ata_host_set *host_set);
403
404         void (*bmdma_stop) (struct ata_queued_cmd *qc);
405         u8   (*bmdma_status) (struct ata_port *ap);
406 };
407
408 struct ata_port_info {
409         struct scsi_host_template       *sht;
410         unsigned long           host_flags;
411         unsigned long           pio_mask;
412         unsigned long           mwdma_mask;
413         unsigned long           udma_mask;
414         const struct ata_port_operations *port_ops;
415         void                    *private_data;
416 };
417
418 struct ata_timing {
419         unsigned short mode;            /* ATA mode */
420         unsigned short setup;           /* t1 */
421         unsigned short act8b;           /* t2 for 8-bit I/O */
422         unsigned short rec8b;           /* t2i for 8-bit I/O */
423         unsigned short cyc8b;           /* t0 for 8-bit I/O */
424         unsigned short active;          /* t2 or tD */
425         unsigned short recover;         /* t2i or tK */
426         unsigned short cycle;           /* t0 */
427         unsigned short udma;            /* t2CYCTYP/2 */
428 };
429
430 #define FIT(v,vmin,vmax)        max_t(short,min_t(short,v,vmax),vmin)
431
432 extern void ata_port_probe(struct ata_port *);
433 extern void __sata_phy_reset(struct ata_port *ap);
434 extern void sata_phy_reset(struct ata_port *ap);
435 extern void ata_bus_reset(struct ata_port *ap);
436 extern void ata_port_disable(struct ata_port *);
437 extern void ata_std_ports(struct ata_ioports *ioaddr);
438 #ifdef CONFIG_PCI
439 extern int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
440                              unsigned int n_ports);
441 extern void ata_pci_remove_one (struct pci_dev *pdev);
442 #endif /* CONFIG_PCI */
443 extern int ata_device_add(const struct ata_probe_ent *ent);
444 extern void ata_host_set_remove(struct ata_host_set *host_set);
445 extern int ata_scsi_detect(struct scsi_host_template *sht);
446 extern int ata_scsi_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
447 extern int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *));
448 extern int ata_scsi_error(struct Scsi_Host *host);
449 extern int ata_scsi_release(struct Scsi_Host *host);
450 extern unsigned int ata_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc);
451 extern int ata_ratelimit(void);
452
453 /*
454  * Default driver ops implementations
455  */
456 extern void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf);
457 extern void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
458 extern void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp);
459 extern void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf);
460 extern void ata_noop_dev_select (struct ata_port *ap, unsigned int device);
461 extern void ata_std_dev_select (struct ata_port *ap, unsigned int device);
462 extern u8 ata_check_status(struct ata_port *ap);
463 extern u8 ata_altstatus(struct ata_port *ap);
464 extern void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf);
465 extern int ata_port_start (struct ata_port *ap);
466 extern void ata_port_stop (struct ata_port *ap);
467 extern void ata_host_stop (struct ata_host_set *host_set);
468 extern irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
469 extern void ata_qc_prep(struct ata_queued_cmd *qc);
470 extern int ata_qc_issue_prot(struct ata_queued_cmd *qc);
471 extern void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf,
472                 unsigned int buflen);
473 extern void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
474                  unsigned int n_elem);
475 extern unsigned int ata_dev_classify(const struct ata_taskfile *tf);
476 extern void ata_dev_id_string(const u16 *id, unsigned char *s,
477                               unsigned int ofs, unsigned int len);
478 extern void ata_dev_config(struct ata_port *ap, unsigned int i);
479 extern void ata_bmdma_setup (struct ata_queued_cmd *qc);
480 extern void ata_bmdma_start (struct ata_queued_cmd *qc);
481 extern void ata_bmdma_stop(struct ata_queued_cmd *qc);
482 extern u8   ata_bmdma_status(struct ata_port *ap);
483 extern void ata_bmdma_irq_clear(struct ata_port *ap);
484 extern void ata_qc_complete(struct ata_queued_cmd *qc);
485 extern void ata_eng_timeout(struct ata_port *ap);
486 extern void ata_scsi_simulate(u16 *id, struct scsi_cmnd *cmd,
487                               void (*done)(struct scsi_cmnd *));
488 extern int ata_std_bios_param(struct scsi_device *sdev,
489                               struct block_device *bdev,
490                               sector_t capacity, int geom[]);
491 extern int ata_scsi_slave_config(struct scsi_device *sdev);
492
493 /*
494  * Timing helpers
495  */
496 extern int ata_timing_compute(struct ata_device *, unsigned short,
497                               struct ata_timing *, int, int);
498 extern void ata_timing_merge(const struct ata_timing *,
499                              const struct ata_timing *, struct ata_timing *,
500                              unsigned int);
501
502 enum {
503         ATA_TIMING_SETUP        = (1 << 0),
504         ATA_TIMING_ACT8B        = (1 << 1),
505         ATA_TIMING_REC8B        = (1 << 2),
506         ATA_TIMING_CYC8B        = (1 << 3),
507         ATA_TIMING_8BIT         = ATA_TIMING_ACT8B | ATA_TIMING_REC8B |
508                                   ATA_TIMING_CYC8B,
509         ATA_TIMING_ACTIVE       = (1 << 4),
510         ATA_TIMING_RECOVER      = (1 << 5),
511         ATA_TIMING_CYCLE        = (1 << 6),
512         ATA_TIMING_UDMA         = (1 << 7),
513         ATA_TIMING_ALL          = ATA_TIMING_SETUP | ATA_TIMING_ACT8B |
514                                   ATA_TIMING_REC8B | ATA_TIMING_CYC8B |
515                                   ATA_TIMING_ACTIVE | ATA_TIMING_RECOVER |
516                                   ATA_TIMING_CYCLE | ATA_TIMING_UDMA,
517 };
518
519
520 #ifdef CONFIG_PCI
521 struct pci_bits {
522         unsigned int            reg;    /* PCI config register to read */
523         unsigned int            width;  /* 1 (8 bit), 2 (16 bit), 4 (32 bit) */
524         unsigned long           mask;
525         unsigned long           val;
526 };
527
528 extern void ata_pci_host_stop (struct ata_host_set *host_set);
529 extern struct ata_probe_ent *
530 ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int portmask);
531 extern int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits);
532
533 #endif /* CONFIG_PCI */
534
535
536 static inline int
537 ata_sg_is_last(struct scatterlist *sg, struct ata_queued_cmd *qc)
538 {
539         if (sg == &qc->pad_sgent)
540                 return 1;
541         if (qc->pad_len)
542                 return 0;
543         if (((sg - qc->__sg) + 1) == qc->n_elem)
544                 return 1;
545         return 0;
546 }
547
548 static inline struct scatterlist *
549 ata_qc_next_sg(struct scatterlist *sg, struct ata_queued_cmd *qc)
550 {
551         if (sg == &qc->pad_sgent)
552                 return NULL;
553         if (++sg - qc->__sg < qc->n_elem)
554                 return sg;
555         return qc->pad_len ? &qc->pad_sgent : NULL;
556 }
557
558 #define ata_for_each_sg(sg, qc) \
559         for (sg = qc->__sg; sg; sg = ata_qc_next_sg(sg, qc))
560
561 static inline unsigned int ata_tag_valid(unsigned int tag)
562 {
563         return (tag < ATA_MAX_QUEUE) ? 1 : 0;
564 }
565
566 static inline unsigned int ata_dev_present(const struct ata_device *dev)
567 {
568         return ((dev->class == ATA_DEV_ATA) ||
569                 (dev->class == ATA_DEV_ATAPI));
570 }
571
572 static inline u8 ata_chk_status(struct ata_port *ap)
573 {
574         return ap->ops->check_status(ap);
575 }
576
577
578 /**
579  *      ata_pause - Flush writes and pause 400 nanoseconds.
580  *      @ap: Port to wait for.
581  *
582  *      LOCKING:
583  *      Inherited from caller.
584  */
585
586 static inline void ata_pause(struct ata_port *ap)
587 {
588         ata_altstatus(ap);
589         ndelay(400);
590 }
591
592
593 /**
594  *      ata_busy_wait - Wait for a port status register
595  *      @ap: Port to wait for.
596  *
597  *      Waits up to max*10 microseconds for the selected bits in the port's
598  *      status register to be cleared.
599  *      Returns final value of status register.
600  *
601  *      LOCKING:
602  *      Inherited from caller.
603  */
604
605 static inline u8 ata_busy_wait(struct ata_port *ap, unsigned int bits,
606                                unsigned int max)
607 {
608         u8 status;
609
610         do {
611                 udelay(10);
612                 status = ata_chk_status(ap);
613                 max--;
614         } while ((status & bits) && (max > 0));
615
616         return status;
617 }
618
619
620 /**
621  *      ata_wait_idle - Wait for a port to be idle.
622  *      @ap: Port to wait for.
623  *
624  *      Waits up to 10ms for port's BUSY and DRQ signals to clear.
625  *      Returns final value of status register.
626  *
627  *      LOCKING:
628  *      Inherited from caller.
629  */
630
631 static inline u8 ata_wait_idle(struct ata_port *ap)
632 {
633         u8 status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
634
635         if (status & (ATA_BUSY | ATA_DRQ)) {
636                 unsigned long l = ap->ioaddr.status_addr;
637                 printk(KERN_WARNING
638                        "ATA: abnormal status 0x%X on port 0x%lX\n",
639                        status, l);
640         }
641
642         return status;
643 }
644
645 static inline void ata_qc_set_polling(struct ata_queued_cmd *qc)
646 {
647         qc->tf.ctl |= ATA_NIEN;
648 }
649
650 static inline struct ata_queued_cmd *ata_qc_from_tag (struct ata_port *ap,
651                                                       unsigned int tag)
652 {
653         if (likely(ata_tag_valid(tag)))
654                 return &ap->qcmd[tag];
655         return NULL;
656 }
657
658 static inline void ata_tf_init(struct ata_port *ap, struct ata_taskfile *tf, unsigned int device)
659 {
660         memset(tf, 0, sizeof(*tf));
661
662         tf->ctl = ap->ctl;
663         if (device == 0)
664                 tf->device = ATA_DEVICE_OBS;
665         else
666                 tf->device = ATA_DEVICE_OBS | ATA_DEV1;
667 }
668
669 static inline void ata_qc_reinit(struct ata_queued_cmd *qc)
670 {
671         qc->__sg = NULL;
672         qc->flags = 0;
673         qc->cursect = qc->cursg = qc->cursg_ofs = 0;
674         qc->nsect = 0;
675         qc->nbytes = qc->curbytes = 0;
676         qc->err_mask = 0;
677
678         ata_tf_init(qc->ap, &qc->tf, qc->dev->devno);
679 }
680
681
682 /**
683  *      ata_irq_on - Enable interrupts on a port.
684  *      @ap: Port on which interrupts are enabled.
685  *
686  *      Enable interrupts on a legacy IDE device using MMIO or PIO,
687  *      wait for idle, clear any pending interrupts.
688  *
689  *      LOCKING:
690  *      Inherited from caller.
691  */
692
693 static inline u8 ata_irq_on(struct ata_port *ap)
694 {
695         struct ata_ioports *ioaddr = &ap->ioaddr;
696         u8 tmp;
697
698         ap->ctl &= ~ATA_NIEN;
699         ap->last_ctl = ap->ctl;
700
701         if (ap->flags & ATA_FLAG_MMIO)
702                 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
703         else
704                 outb(ap->ctl, ioaddr->ctl_addr);
705         tmp = ata_wait_idle(ap);
706
707         ap->ops->irq_clear(ap);
708
709         return tmp;
710 }
711
712
713 /**
714  *      ata_irq_ack - Acknowledge a device interrupt.
715  *      @ap: Port on which interrupts are enabled.
716  *
717  *      Wait up to 10 ms for legacy IDE device to become idle (BUSY
718  *      or BUSY+DRQ clear).  Obtain dma status and port status from
719  *      device.  Clear the interrupt.  Return port status.
720  *
721  *      LOCKING:
722  */
723
724 static inline u8 ata_irq_ack(struct ata_port *ap, unsigned int chk_drq)
725 {
726         unsigned int bits = chk_drq ? ATA_BUSY | ATA_DRQ : ATA_BUSY;
727         u8 host_stat, post_stat, status;
728
729         status = ata_busy_wait(ap, bits, 1000);
730         if (status & bits)
731                 DPRINTK("abnormal status 0x%X\n", status);
732
733         /* get controller status; clear intr, err bits */
734         if (ap->flags & ATA_FLAG_MMIO) {
735                 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
736                 host_stat = readb(mmio + ATA_DMA_STATUS);
737                 writeb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
738                        mmio + ATA_DMA_STATUS);
739
740                 post_stat = readb(mmio + ATA_DMA_STATUS);
741         } else {
742                 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
743                 outb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
744                      ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
745
746                 post_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
747         }
748
749         VPRINTK("irq ack: host_stat 0x%X, new host_stat 0x%X, drv_stat 0x%X\n",
750                 host_stat, post_stat, status);
751
752         return status;
753 }
754
755 static inline u32 scr_read(struct ata_port *ap, unsigned int reg)
756 {
757         return ap->ops->scr_read(ap, reg);
758 }
759
760 static inline void scr_write(struct ata_port *ap, unsigned int reg, u32 val)
761 {
762         ap->ops->scr_write(ap, reg, val);
763 }
764
765 static inline void scr_write_flush(struct ata_port *ap, unsigned int reg,
766                                    u32 val)
767 {
768         ap->ops->scr_write(ap, reg, val);
769         (void) ap->ops->scr_read(ap, reg);
770 }
771
772 static inline unsigned int sata_dev_present(struct ata_port *ap)
773 {
774         return ((scr_read(ap, SCR_STATUS) & 0xf) == 0x3) ? 1 : 0;
775 }
776
777 static inline int ata_try_flush_cache(const struct ata_device *dev)
778 {
779         return ata_id_wcache_enabled(dev->id) ||
780                ata_id_has_flush(dev->id) ||
781                ata_id_has_flush_ext(dev->id);
782 }
783
784 static inline unsigned int ac_err_mask(u8 status)
785 {
786         if (status & ATA_BUSY)
787                 return AC_ERR_ATA_BUS;
788         if (status & (ATA_ERR | ATA_DF))
789                 return AC_ERR_DEV;
790         return 0;
791 }
792
793 static inline unsigned int __ac_err_mask(u8 status)
794 {
795         unsigned int mask = ac_err_mask(status);
796         if (mask == 0)
797                 return AC_ERR_OTHER;
798         return mask;
799 }
800
801 static inline int ata_pad_alloc(struct ata_port *ap, struct device *dev)
802 {
803         ap->pad_dma = 0;
804         ap->pad = dma_alloc_coherent(dev, ATA_DMA_PAD_BUF_SZ,
805                                      &ap->pad_dma, GFP_KERNEL);
806         return (ap->pad == NULL) ? -ENOMEM : 0;
807 }
808
809 static inline void ata_pad_free(struct ata_port *ap, struct device *dev)
810 {
811         dma_free_coherent(dev, ATA_DMA_PAD_BUF_SZ, ap->pad, ap->pad_dma);
812 }
813
814 #endif /* __LINUX_LIBATA_H__ */