libata.h 22 KB

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