libata.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. #ifndef __LINUX_LIBATA_H__
  26. #define __LINUX_LIBATA_H__
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/pci.h>
  30. #include <asm/io.h>
  31. #include <linux/ata.h>
  32. #include <linux/workqueue.h>
  33. /*
  34. * compile-time options
  35. */
  36. #undef ATA_DEBUG /* debugging output */
  37. #undef ATA_VERBOSE_DEBUG /* yet more debugging output */
  38. #undef ATA_IRQ_TRAP /* define to ack screaming irqs */
  39. #undef ATA_NDEBUG /* define to disable quick runtime checks */
  40. #undef ATA_ENABLE_PATA /* define to enable PATA support in some
  41. * low-level drivers */
  42. #undef ATAPI_ENABLE_DMADIR /* enables ATAPI DMADIR bridge support */
  43. /* note: prints function name for you */
  44. #ifdef ATA_DEBUG
  45. #define DPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ## args)
  46. #ifdef ATA_VERBOSE_DEBUG
  47. #define VPRINTK(fmt, args...) printk(KERN_ERR "%s: " fmt, __FUNCTION__, ## args)
  48. #else
  49. #define VPRINTK(fmt, args...)
  50. #endif /* ATA_VERBOSE_DEBUG */
  51. #else
  52. #define DPRINTK(fmt, args...)
  53. #define VPRINTK(fmt, args...)
  54. #endif /* ATA_DEBUG */
  55. #ifdef ATA_NDEBUG
  56. #define assert(expr)
  57. #else
  58. #define assert(expr) \
  59. if(unlikely(!(expr))) { \
  60. printk(KERN_ERR "Assertion failed! %s,%s,%s,line=%d\n", \
  61. #expr,__FILE__,__FUNCTION__,__LINE__); \
  62. }
  63. #endif
  64. /* defines only for the constants which don't work well as enums */
  65. #define ATA_TAG_POISON 0xfafbfcfdU
  66. /* move to PCI layer? */
  67. static inline struct device *pci_dev_to_dev(struct pci_dev *pdev)
  68. {
  69. return &pdev->dev;
  70. }
  71. enum {
  72. /* various global constants */
  73. LIBATA_MAX_PRD = ATA_MAX_PRD / 2,
  74. ATA_MAX_PORTS = 8,
  75. ATA_DEF_QUEUE = 1,
  76. ATA_MAX_QUEUE = 1,
  77. ATA_MAX_SECTORS = 200, /* FIXME */
  78. ATA_MAX_BUS = 2,
  79. ATA_DEF_BUSY_WAIT = 10000,
  80. ATA_SHORT_PAUSE = (HZ >> 6) + 1,
  81. ATA_SHT_EMULATED = 1,
  82. ATA_SHT_CMD_PER_LUN = 1,
  83. ATA_SHT_THIS_ID = -1,
  84. ATA_SHT_USE_CLUSTERING = 1,
  85. /* struct ata_device stuff */
  86. ATA_DFLAG_LBA48 = (1 << 0), /* device supports LBA48 */
  87. ATA_DFLAG_PIO = (1 << 1), /* device currently in PIO mode */
  88. ATA_DFLAG_LOCK_SECTORS = (1 << 2), /* don't adjust max_sectors */
  89. ATA_DFLAG_LBA = (1 << 3), /* device supports LBA */
  90. ATA_DFLAG_CDB_INTR = (1 << 4), /* device asserts INTRQ when ready for CDB */
  91. ATA_DEV_UNKNOWN = 0, /* unknown device */
  92. ATA_DEV_ATA = 1, /* ATA device */
  93. ATA_DEV_ATA_UNSUP = 2, /* ATA device (unsupported) */
  94. ATA_DEV_ATAPI = 3, /* ATAPI device */
  95. ATA_DEV_ATAPI_UNSUP = 4, /* ATAPI device (unsupported) */
  96. ATA_DEV_NONE = 5, /* no device */
  97. /* struct ata_port flags */
  98. ATA_FLAG_SLAVE_POSS = (1 << 1), /* host supports slave dev */
  99. /* (doesn't imply presence) */
  100. ATA_FLAG_PORT_DISABLED = (1 << 2), /* port is disabled, ignore it */
  101. ATA_FLAG_SATA = (1 << 3),
  102. ATA_FLAG_NO_LEGACY = (1 << 4), /* no legacy mode check */
  103. ATA_FLAG_SRST = (1 << 5), /* use ATA SRST, not E.D.D. */
  104. ATA_FLAG_MMIO = (1 << 6), /* use MMIO, not PIO */
  105. ATA_FLAG_SATA_RESET = (1 << 7), /* use COMRESET */
  106. ATA_FLAG_PIO_DMA = (1 << 8), /* PIO cmds via DMA */
  107. ATA_FLAG_PIO_POLLING = (1 << 9), /* use polling PIO if LLD
  108. * doesn't handle PIO interrupts */
  109. ATA_QCFLAG_ACTIVE = (1 << 1), /* cmd not yet ack'd to scsi lyer */
  110. ATA_QCFLAG_SG = (1 << 3), /* have s/g table? */
  111. ATA_QCFLAG_SINGLE = (1 << 4), /* no s/g, just a single buffer */
  112. ATA_QCFLAG_DMAMAP = ATA_QCFLAG_SG | ATA_QCFLAG_SINGLE,
  113. /* various lengths of time */
  114. ATA_TMOUT_EDD = 5 * HZ, /* hueristic */
  115. ATA_TMOUT_PIO = 30 * HZ,
  116. ATA_TMOUT_BOOT = 30 * HZ, /* hueristic */
  117. ATA_TMOUT_BOOT_QUICK = 7 * HZ, /* hueristic */
  118. ATA_TMOUT_DATAOUT = 30 * HZ,
  119. ATA_TMOUT_DATAOUT_QUICK = 5 * HZ,
  120. /* ATA bus states */
  121. BUS_UNKNOWN = 0,
  122. BUS_DMA = 1,
  123. BUS_IDLE = 2,
  124. BUS_NOINTR = 3,
  125. BUS_NODATA = 4,
  126. BUS_TIMER = 5,
  127. BUS_PIO = 6,
  128. BUS_EDD = 7,
  129. BUS_IDENTIFY = 8,
  130. BUS_PACKET = 9,
  131. /* SATA port states */
  132. PORT_UNKNOWN = 0,
  133. PORT_ENABLED = 1,
  134. PORT_DISABLED = 2,
  135. /* encoding various smaller bitmaps into a single
  136. * unsigned long bitmap
  137. */
  138. ATA_SHIFT_UDMA = 0,
  139. ATA_SHIFT_MWDMA = 8,
  140. ATA_SHIFT_PIO = 11,
  141. /* Masks for port functions */
  142. ATA_PORT_PRIMARY = (1 << 0),
  143. ATA_PORT_SECONDARY = (1 << 1),
  144. };
  145. enum hsm_task_states {
  146. HSM_ST_UNKNOWN, /* state unknown */
  147. HSM_ST_IDLE, /* no command on going */
  148. HSM_ST_POLL, /* same as HSM_ST, waits longer */
  149. HSM_ST_TMOUT, /* timeout */
  150. HSM_ST, /* (waiting the device to) transfer data */
  151. HSM_ST_LAST, /* (waiting the device to) complete command */
  152. HSM_ST_LAST_POLL, /* same as HSM_ST_LAST, waits longer */
  153. HSM_ST_ERR, /* error */
  154. HSM_ST_FIRST, /* (waiting the device to)
  155. write CDB or first data block */
  156. };
  157. enum ata_completion_errors {
  158. AC_ERR_OTHER = (1 << 0),
  159. AC_ERR_DEV = (1 << 1),
  160. AC_ERR_ATA_BUS = (1 << 2),
  161. AC_ERR_HOST_BUS = (1 << 3),
  162. };
  163. /* forward declarations */
  164. struct scsi_device;
  165. struct ata_port_operations;
  166. struct ata_port;
  167. struct ata_queued_cmd;
  168. /* typedefs */
  169. typedef int (*ata_qc_cb_t) (struct ata_queued_cmd *qc, unsigned int err_mask);
  170. struct ata_ioports {
  171. unsigned long cmd_addr;
  172. unsigned long data_addr;
  173. unsigned long error_addr;
  174. unsigned long feature_addr;
  175. unsigned long nsect_addr;
  176. unsigned long lbal_addr;
  177. unsigned long lbam_addr;
  178. unsigned long lbah_addr;
  179. unsigned long device_addr;
  180. unsigned long status_addr;
  181. unsigned long command_addr;
  182. unsigned long altstatus_addr;
  183. unsigned long ctl_addr;
  184. unsigned long bmdma_addr;
  185. unsigned long scr_addr;
  186. };
  187. struct ata_probe_ent {
  188. struct list_head node;
  189. struct device *dev;
  190. const struct ata_port_operations *port_ops;
  191. Scsi_Host_Template *sht;
  192. struct ata_ioports port[ATA_MAX_PORTS];
  193. unsigned int n_ports;
  194. unsigned int hard_port_no;
  195. unsigned int pio_mask;
  196. unsigned int mwdma_mask;
  197. unsigned int udma_mask;
  198. unsigned int legacy_mode;
  199. unsigned long irq;
  200. unsigned int irq_flags;
  201. unsigned long host_flags;
  202. void __iomem *mmio_base;
  203. void *private_data;
  204. };
  205. struct ata_host_set {
  206. spinlock_t lock;
  207. struct device *dev;
  208. unsigned long irq;
  209. void __iomem *mmio_base;
  210. unsigned int n_ports;
  211. void *private_data;
  212. const struct ata_port_operations *ops;
  213. struct ata_port * ports[0];
  214. };
  215. struct ata_queued_cmd {
  216. struct ata_port *ap;
  217. struct ata_device *dev;
  218. struct scsi_cmnd *scsicmd;
  219. void (*scsidone)(struct scsi_cmnd *);
  220. struct ata_taskfile tf;
  221. u8 cdb[ATAPI_CDB_LEN];
  222. unsigned long flags; /* ATA_QCFLAG_xxx */
  223. unsigned int tag;
  224. unsigned int n_elem;
  225. int dma_dir;
  226. unsigned int nsect;
  227. unsigned int cursect;
  228. unsigned int nbytes;
  229. unsigned int curbytes;
  230. unsigned int cursg;
  231. unsigned int cursg_ofs;
  232. struct scatterlist sgent;
  233. void *buf_virt;
  234. struct scatterlist *sg;
  235. ata_qc_cb_t complete_fn;
  236. struct completion *waiting;
  237. void *private_data;
  238. };
  239. struct ata_host_stats {
  240. unsigned long unhandled_irq;
  241. unsigned long idle_irq;
  242. unsigned long rw_reqbuf;
  243. };
  244. struct ata_device {
  245. u64 n_sectors; /* size of device, if ATA */
  246. unsigned long flags; /* ATA_DFLAG_xxx */
  247. unsigned int class; /* ATA_DEV_xxx */
  248. unsigned int devno; /* 0 or 1 */
  249. u16 id[ATA_ID_WORDS]; /* IDENTIFY xxx DEVICE data */
  250. u8 pio_mode;
  251. u8 dma_mode;
  252. u8 xfer_mode;
  253. unsigned int xfer_shift; /* ATA_SHIFT_xxx */
  254. unsigned int multi_count; /* sectors count for
  255. READ/WRITE MULTIPLE */
  256. /* for CHS addressing */
  257. u16 cylinders; /* Number of cylinders */
  258. u16 heads; /* Number of heads */
  259. u16 sectors; /* Number of sectors per track */
  260. };
  261. struct ata_port {
  262. struct Scsi_Host *host; /* our co-allocated scsi host */
  263. const struct ata_port_operations *ops;
  264. unsigned long flags; /* ATA_FLAG_xxx */
  265. unsigned int id; /* unique id req'd by scsi midlyr */
  266. unsigned int port_no; /* unique port #; from zero */
  267. unsigned int hard_port_no; /* hardware port #; from zero */
  268. struct ata_prd *prd; /* our SG list */
  269. dma_addr_t prd_dma; /* and its DMA mapping */
  270. struct ata_ioports ioaddr; /* ATA cmd/ctl/dma register blocks */
  271. u8 ctl; /* cache of ATA control register */
  272. u8 last_ctl; /* Cache last written value */
  273. unsigned int bus_state;
  274. unsigned int port_state;
  275. unsigned int pio_mask;
  276. unsigned int mwdma_mask;
  277. unsigned int udma_mask;
  278. unsigned int cbl; /* cable type; ATA_CBL_xxx */
  279. unsigned int cdb_len;
  280. struct ata_device device[ATA_MAX_DEVICES];
  281. struct ata_queued_cmd qcmd[ATA_MAX_QUEUE];
  282. unsigned long qactive;
  283. unsigned int active_tag;
  284. struct ata_host_stats stats;
  285. struct ata_host_set *host_set;
  286. struct work_struct dataout_task;
  287. struct work_struct pio_task;
  288. unsigned int hsm_task_state;
  289. unsigned long pio_task_timeout;
  290. void *private_data;
  291. };
  292. struct ata_port_operations {
  293. void (*port_disable) (struct ata_port *);
  294. void (*dev_config) (struct ata_port *, struct ata_device *);
  295. void (*set_piomode) (struct ata_port *, struct ata_device *);
  296. void (*set_dmamode) (struct ata_port *, struct ata_device *);
  297. void (*tf_load) (struct ata_port *ap, const struct ata_taskfile *tf);
  298. void (*tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
  299. void (*exec_command)(struct ata_port *ap, const struct ata_taskfile *tf);
  300. u8 (*check_status)(struct ata_port *ap);
  301. u8 (*check_altstatus)(struct ata_port *ap);
  302. void (*dev_select)(struct ata_port *ap, unsigned int device);
  303. void (*phy_reset) (struct ata_port *ap);
  304. void (*post_set_mode) (struct ata_port *ap);
  305. int (*check_atapi_dma) (struct ata_queued_cmd *qc);
  306. void (*bmdma_setup) (struct ata_queued_cmd *qc);
  307. void (*bmdma_start) (struct ata_queued_cmd *qc);
  308. void (*qc_prep) (struct ata_queued_cmd *qc);
  309. int (*qc_issue) (struct ata_queued_cmd *qc);
  310. void (*eng_timeout) (struct ata_port *ap);
  311. irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
  312. void (*irq_clear) (struct ata_port *);
  313. u32 (*scr_read) (struct ata_port *ap, unsigned int sc_reg);
  314. void (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
  315. u32 val);
  316. int (*port_start) (struct ata_port *ap);
  317. void (*port_stop) (struct ata_port *ap);
  318. void (*host_stop) (struct ata_host_set *host_set);
  319. void (*bmdma_stop) (struct ata_queued_cmd *qc);
  320. u8 (*bmdma_status) (struct ata_port *ap);
  321. };
  322. struct ata_port_info {
  323. Scsi_Host_Template *sht;
  324. unsigned long host_flags;
  325. unsigned long pio_mask;
  326. unsigned long mwdma_mask;
  327. unsigned long udma_mask;
  328. const struct ata_port_operations *port_ops;
  329. };
  330. struct ata_timing {
  331. unsigned short mode; /* ATA mode */
  332. unsigned short setup; /* t1 */
  333. unsigned short act8b; /* t2 for 8-bit I/O */
  334. unsigned short rec8b; /* t2i for 8-bit I/O */
  335. unsigned short cyc8b; /* t0 for 8-bit I/O */
  336. unsigned short active; /* t2 or tD */
  337. unsigned short recover; /* t2i or tK */
  338. unsigned short cycle; /* t0 */
  339. unsigned short udma; /* t2CYCTYP/2 */
  340. };
  341. #define FIT(v,vmin,vmax) max_t(short,min_t(short,v,vmax),vmin)
  342. extern void ata_port_probe(struct ata_port *);
  343. extern void __sata_phy_reset(struct ata_port *ap);
  344. extern void sata_phy_reset(struct ata_port *ap);
  345. extern void ata_bus_reset(struct ata_port *ap);
  346. extern void ata_port_disable(struct ata_port *);
  347. extern void ata_std_ports(struct ata_ioports *ioaddr);
  348. #ifdef CONFIG_PCI
  349. extern int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
  350. unsigned int n_ports);
  351. extern void ata_pci_remove_one (struct pci_dev *pdev);
  352. #endif /* CONFIG_PCI */
  353. extern int ata_device_add(const struct ata_probe_ent *ent);
  354. extern void ata_host_set_remove(struct ata_host_set *host_set);
  355. extern int ata_scsi_detect(Scsi_Host_Template *sht);
  356. extern int ata_scsi_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
  357. extern int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *));
  358. extern int ata_scsi_error(struct Scsi_Host *host);
  359. extern int ata_scsi_release(struct Scsi_Host *host);
  360. extern unsigned int ata_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc);
  361. extern int ata_ratelimit(void);
  362. /*
  363. * Default driver ops implementations
  364. */
  365. extern void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf);
  366. extern void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  367. extern void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp);
  368. extern void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf);
  369. extern void ata_noop_dev_select (struct ata_port *ap, unsigned int device);
  370. extern void ata_std_dev_select (struct ata_port *ap, unsigned int device);
  371. extern u8 ata_check_status(struct ata_port *ap);
  372. extern u8 ata_altstatus(struct ata_port *ap);
  373. extern void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf);
  374. extern int ata_port_start (struct ata_port *ap);
  375. extern void ata_port_stop (struct ata_port *ap);
  376. extern void ata_host_stop (struct ata_host_set *host_set);
  377. extern irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
  378. extern void ata_qc_prep(struct ata_queued_cmd *qc);
  379. extern int ata_qc_issue_prot(struct ata_queued_cmd *qc);
  380. extern void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf,
  381. unsigned int buflen);
  382. extern void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
  383. unsigned int n_elem);
  384. extern unsigned int ata_dev_classify(const struct ata_taskfile *tf);
  385. extern void ata_dev_id_string(const u16 *id, unsigned char *s,
  386. unsigned int ofs, unsigned int len);
  387. extern void ata_dev_config(struct ata_port *ap, unsigned int i);
  388. extern void ata_bmdma_setup (struct ata_queued_cmd *qc);
  389. extern void ata_bmdma_start (struct ata_queued_cmd *qc);
  390. extern void ata_bmdma_stop(struct ata_queued_cmd *qc);
  391. extern u8 ata_bmdma_status(struct ata_port *ap);
  392. extern void ata_bmdma_irq_clear(struct ata_port *ap);
  393. extern void ata_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask);
  394. extern void ata_eng_timeout(struct ata_port *ap);
  395. extern void ata_scsi_simulate(u16 *id, struct scsi_cmnd *cmd,
  396. void (*done)(struct scsi_cmnd *));
  397. extern int ata_std_bios_param(struct scsi_device *sdev,
  398. struct block_device *bdev,
  399. sector_t capacity, int geom[]);
  400. extern int ata_scsi_slave_config(struct scsi_device *sdev);
  401. /*
  402. * Timing helpers
  403. */
  404. extern int ata_timing_compute(struct ata_device *, unsigned short,
  405. struct ata_timing *, int, int);
  406. extern void ata_timing_merge(const struct ata_timing *,
  407. const struct ata_timing *, struct ata_timing *,
  408. unsigned int);
  409. enum {
  410. ATA_TIMING_SETUP = (1 << 0),
  411. ATA_TIMING_ACT8B = (1 << 1),
  412. ATA_TIMING_REC8B = (1 << 2),
  413. ATA_TIMING_CYC8B = (1 << 3),
  414. ATA_TIMING_8BIT = ATA_TIMING_ACT8B | ATA_TIMING_REC8B |
  415. ATA_TIMING_CYC8B,
  416. ATA_TIMING_ACTIVE = (1 << 4),
  417. ATA_TIMING_RECOVER = (1 << 5),
  418. ATA_TIMING_CYCLE = (1 << 6),
  419. ATA_TIMING_UDMA = (1 << 7),
  420. ATA_TIMING_ALL = ATA_TIMING_SETUP | ATA_TIMING_ACT8B |
  421. ATA_TIMING_REC8B | ATA_TIMING_CYC8B |
  422. ATA_TIMING_ACTIVE | ATA_TIMING_RECOVER |
  423. ATA_TIMING_CYCLE | ATA_TIMING_UDMA,
  424. };
  425. #ifdef CONFIG_PCI
  426. struct pci_bits {
  427. unsigned int reg; /* PCI config register to read */
  428. unsigned int width; /* 1 (8 bit), 2 (16 bit), 4 (32 bit) */
  429. unsigned long mask;
  430. unsigned long val;
  431. };
  432. extern void ata_pci_host_stop (struct ata_host_set *host_set);
  433. extern struct ata_probe_ent *
  434. ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int portmask);
  435. extern int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits);
  436. #endif /* CONFIG_PCI */
  437. static inline unsigned int ata_tag_valid(unsigned int tag)
  438. {
  439. return (tag < ATA_MAX_QUEUE) ? 1 : 0;
  440. }
  441. static inline unsigned int ata_dev_present(const struct ata_device *dev)
  442. {
  443. return ((dev->class == ATA_DEV_ATA) ||
  444. (dev->class == ATA_DEV_ATAPI));
  445. }
  446. static inline u8 ata_chk_status(struct ata_port *ap)
  447. {
  448. return ap->ops->check_status(ap);
  449. }
  450. /**
  451. * ata_pause - Flush writes and pause 400 nanoseconds.
  452. * @ap: Port to wait for.
  453. *
  454. * LOCKING:
  455. * Inherited from caller.
  456. */
  457. static inline void ata_pause(struct ata_port *ap)
  458. {
  459. ata_altstatus(ap);
  460. ndelay(400);
  461. }
  462. /**
  463. * ata_busy_wait - Wait for a port status register
  464. * @ap: Port to wait for.
  465. *
  466. * Waits up to max*10 microseconds for the selected bits in the port's
  467. * status register to be cleared.
  468. * Returns final value of status register.
  469. *
  470. * LOCKING:
  471. * Inherited from caller.
  472. */
  473. static inline u8 ata_busy_wait(struct ata_port *ap, unsigned int bits,
  474. unsigned int max)
  475. {
  476. u8 status;
  477. do {
  478. udelay(10);
  479. status = ata_chk_status(ap);
  480. max--;
  481. } while ((status & bits) && (max > 0));
  482. return status;
  483. }
  484. /**
  485. * ata_wait_idle - Wait for a port to be idle.
  486. * @ap: Port to wait for.
  487. *
  488. * Waits up to 10ms for port's BUSY and DRQ signals to clear.
  489. * Returns final value of status register.
  490. *
  491. * LOCKING:
  492. * Inherited from caller.
  493. */
  494. static inline u8 ata_wait_idle(struct ata_port *ap)
  495. {
  496. u8 status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  497. if (status & (ATA_BUSY | ATA_DRQ)) {
  498. unsigned long l = ap->ioaddr.status_addr;
  499. printk(KERN_WARNING
  500. "ATA: abnormal status 0x%X on port 0x%lX\n",
  501. status, l);
  502. }
  503. return status;
  504. }
  505. static inline void ata_qc_set_polling(struct ata_queued_cmd *qc)
  506. {
  507. qc->tf.ctl |= ATA_NIEN;
  508. }
  509. static inline struct ata_queued_cmd *ata_qc_from_tag (struct ata_port *ap,
  510. unsigned int tag)
  511. {
  512. if (likely(ata_tag_valid(tag)))
  513. return &ap->qcmd[tag];
  514. return NULL;
  515. }
  516. static inline void ata_tf_init(struct ata_port *ap, struct ata_taskfile *tf, unsigned int device)
  517. {
  518. memset(tf, 0, sizeof(*tf));
  519. tf->ctl = ap->ctl;
  520. if (device == 0)
  521. tf->device = ATA_DEVICE_OBS;
  522. else
  523. tf->device = ATA_DEVICE_OBS | ATA_DEV1;
  524. }
  525. /**
  526. * ata_irq_on - Enable interrupts on a port.
  527. * @ap: Port on which interrupts are enabled.
  528. *
  529. * Enable interrupts on a legacy IDE device using MMIO or PIO,
  530. * wait for idle, clear any pending interrupts.
  531. *
  532. * LOCKING:
  533. * Inherited from caller.
  534. */
  535. static inline u8 ata_irq_on(struct ata_port *ap)
  536. {
  537. struct ata_ioports *ioaddr = &ap->ioaddr;
  538. u8 tmp;
  539. ap->ctl &= ~ATA_NIEN;
  540. ap->last_ctl = ap->ctl;
  541. if (ap->flags & ATA_FLAG_MMIO)
  542. writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
  543. else
  544. outb(ap->ctl, ioaddr->ctl_addr);
  545. tmp = ata_wait_idle(ap);
  546. ap->ops->irq_clear(ap);
  547. return tmp;
  548. }
  549. /**
  550. * ata_irq_ack - Acknowledge a device interrupt.
  551. * @ap: Port on which interrupts are enabled.
  552. *
  553. * Wait up to 10 ms for legacy IDE device to become idle (BUSY
  554. * or BUSY+DRQ clear). Obtain dma status and port status from
  555. * device. Clear the interrupt. Return port status.
  556. *
  557. * LOCKING:
  558. */
  559. static inline u8 ata_irq_ack(struct ata_port *ap, unsigned int chk_drq)
  560. {
  561. unsigned int bits = chk_drq ? ATA_BUSY | ATA_DRQ : ATA_BUSY;
  562. u8 host_stat, post_stat, status;
  563. status = ata_busy_wait(ap, bits, 1000);
  564. if (status & bits)
  565. DPRINTK("abnormal status 0x%X\n", status);
  566. /* get controller status; clear intr, err bits */
  567. if (ap->flags & ATA_FLAG_MMIO) {
  568. void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
  569. host_stat = readb(mmio + ATA_DMA_STATUS);
  570. writeb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
  571. mmio + ATA_DMA_STATUS);
  572. post_stat = readb(mmio + ATA_DMA_STATUS);
  573. } else {
  574. host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  575. outb(host_stat | ATA_DMA_INTR | ATA_DMA_ERR,
  576. ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  577. post_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  578. }
  579. VPRINTK("irq ack: host_stat 0x%X, new host_stat 0x%X, drv_stat 0x%X\n",
  580. host_stat, post_stat, status);
  581. return status;
  582. }
  583. static inline u32 scr_read(struct ata_port *ap, unsigned int reg)
  584. {
  585. return ap->ops->scr_read(ap, reg);
  586. }
  587. static inline void scr_write(struct ata_port *ap, unsigned int reg, u32 val)
  588. {
  589. ap->ops->scr_write(ap, reg, val);
  590. }
  591. static inline void scr_write_flush(struct ata_port *ap, unsigned int reg,
  592. u32 val)
  593. {
  594. ap->ops->scr_write(ap, reg, val);
  595. (void) ap->ops->scr_read(ap, reg);
  596. }
  597. static inline unsigned int sata_dev_present(struct ata_port *ap)
  598. {
  599. return ((scr_read(ap, SCR_STATUS) & 0xf) == 0x3) ? 1 : 0;
  600. }
  601. static inline int ata_try_flush_cache(const struct ata_device *dev)
  602. {
  603. return ata_id_wcache_enabled(dev->id) ||
  604. ata_id_has_flush(dev->id) ||
  605. ata_id_has_flush_ext(dev->id);
  606. }
  607. static inline unsigned int ac_err_mask(u8 status)
  608. {
  609. if (status & ATA_BUSY)
  610. return AC_ERR_ATA_BUS;
  611. if (status & (ATA_ERR | ATA_DF))
  612. return AC_ERR_DEV;
  613. return 0;
  614. }
  615. static inline unsigned int __ac_err_mask(u8 status)
  616. {
  617. unsigned int mask = ac_err_mask(status);
  618. if (mask == 0)
  619. return AC_ERR_OTHER;
  620. return mask;
  621. }
  622. #endif /* __LINUX_LIBATA_H__ */