libata.h 25 KB

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