libata.h 28 KB

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