libata.h 18 KB

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