libata.h 27 KB

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