/testbed/atapi/atavar.h

http://rtems-atapi.googlecode.com/ · C Header · 466 lines · 275 code · 65 blank · 126 comment · 0 complexity · 3fd4c5ac4f8b26da3058b243219d33f6 MD5 · raw file

  1. /* $NetBSD: atavar.h,v 1.80 2009/10/19 18:41:12 bouyer Exp $ */
  2. /*
  3. * Copyright (c) 1998, 2001 Manuel Bouyer.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef _DEV_ATA_ATAVAR_H_
  26. #define _DEV_ATA_ATAVAR_H_
  27. #include <sys/lock.h>
  28. #include <rtems/bsd/sys/queue.h>
  29. #include "ataconf.h"
  30. /* XXX For scsipi_adapter and scsipi_channel. */
  31. #include "scsipi/scsipi_all.h"
  32. #include "scsipi/atapiconf.h"
  33. /*
  34. * Max number of drives per channel.
  35. */
  36. #define ATA_MAXDRIVES 2
  37. /*
  38. * Description of a command to be handled by an ATA controller. These
  39. * commands are queued in a list.
  40. */
  41. struct ata_xfer {
  42. volatile u_int c_flags; /* command state flags */
  43. /* Channel and drive that are to process the request. */
  44. struct ata_channel *c_chp;
  45. int c_drive;
  46. void *c_cmd; /* private request structure pointer */
  47. void *c_databuf; /* pointer to data buffer */
  48. int c_bcount; /* byte count left */
  49. int c_skip; /* bytes already transferred */
  50. int c_dscpoll; /* counter for dsc polling (ATAPI) */
  51. int c_lenoff; /* offset to c_bcount (ATAPI) */
  52. /* Link on the command queue. */
  53. TAILQ_ENTRY(ata_xfer) c_xferchain;
  54. /* Low-level protocol handlers. */
  55. void (*c_start)(struct ata_channel *, struct ata_xfer *);
  56. int (*c_intr)(struct ata_channel *, struct ata_xfer *, int);
  57. void (*c_kill_xfer)(struct ata_channel *, struct ata_xfer *, int);
  58. };
  59. /* flags in c_flags */
  60. #define C_ATAPI 0x0001 /* xfer is ATAPI request */
  61. #define C_TIMEOU 0x0002 /* xfer processing timed out */
  62. #define C_POLL 0x0004 /* command is polled */
  63. #define C_DMA 0x0008 /* command uses DMA */
  64. #define C_WAIT 0x0010 /* can use tsleep */
  65. #define C_WAITACT 0x0020 /* wakeup when active */
  66. #define C_FREE 0x0040 /* call ata_free_xfer() asap */
  67. #define C_PIOBM 0x0080 /* command uses busmastering PIO */
  68. /* reasons for c_kill_xfer() */
  69. #define KILL_GONE 1 /* device is gone */
  70. #define KILL_RESET 2 /* xfer was reset */
  71. /* Per-channel queue of ata_xfers. May be shared by multiple channels. */
  72. struct ata_queue {
  73. TAILQ_HEAD(, ata_xfer) queue_xfer; /* queue of pending commands */
  74. int queue_freeze; /* freeze count for the queue */
  75. struct ata_xfer *active_xfer; /* active command */
  76. int queue_flags; /* flags for this queue */
  77. #define QF_IDLE_WAIT 0x01 /* someone is wants the controller idle */
  78. };
  79. /* ATA bus instance state information. */
  80. struct atabus_softc {
  81. device_t sc_dev;
  82. struct ata_channel *sc_chan;
  83. int sc_flags;
  84. #define ATABUSCF_OPEN 0x01
  85. };
  86. /*
  87. * A queue of atabus instances, used to ensure the same bus probe order
  88. * for a given hardware configuration at each boot.
  89. */
  90. struct atabus_initq {
  91. TAILQ_ENTRY(atabus_initq) atabus_initq;
  92. struct atabus_softc *atabus_sc;
  93. };
  94. //ifdef _KERNEL
  95. TAILQ_HEAD(atabus_initq_head, atabus_initq);
  96. extern struct atabus_initq_head atabus_initq_head;
  97. extern struct simplelock atabus_interlock;
  98. //#endif /* _KERNEL */
  99. /* High-level functions and structures used by both ATA and ATAPI devices */
  100. struct ataparams;
  101. /* Datas common to drives and controller drivers */
  102. struct ata_drive_datas {
  103. u_int8_t drive; /* drive number */
  104. int8_t ata_vers; /* ATA version supported */
  105. u_int16_t drive_flags; /* bitmask for drives present/absent and cap */
  106. #define DRIVE_ATA 0x0001
  107. #define DRIVE_ATAPI 0x0002
  108. #define DRIVE_OLD 0x0004
  109. #define DRIVE (DRIVE_ATA|DRIVE_ATAPI|DRIVE_OLD)
  110. #define DRIVE_CAP32 0x0008
  111. #define DRIVE_DMA 0x0010
  112. #define DRIVE_UDMA 0x0020
  113. #define DRIVE_MODE 0x0040 /* the drive reported its mode */
  114. #define DRIVE_RESET 0x0080 /* reset the drive state at next xfer */
  115. #define DRIVE_WAITDRAIN 0x0100 /* device is waiting for the queue to drain */
  116. #define DRIVE_ATAPIST 0x0200 /* device is an ATAPI tape drive */
  117. #define DRIVE_NOSTREAM 0x0400 /* no stream methods on this drive */
  118. /*
  119. * Current setting of drive's PIO, DMA and UDMA modes.
  120. * Is initialised by the disks drivers at attach time, and may be
  121. * changed later by the controller's code if needed
  122. */
  123. u_int8_t PIO_mode; /* Current setting of drive's PIO mode */
  124. #if NATA_DMA
  125. u_int8_t DMA_mode; /* Current setting of drive's DMA mode */
  126. #if NATA_UDMA
  127. u_int8_t UDMA_mode; /* Current setting of drive's UDMA mode */
  128. #endif
  129. #endif
  130. /* Supported modes for this drive */
  131. u_int8_t PIO_cap; /* supported drive's PIO mode */
  132. #if NATA_DMA
  133. u_int8_t DMA_cap; /* supported drive's DMA mode */
  134. #if NATA_UDMA
  135. u_int8_t UDMA_cap; /* supported drive's UDMA mode */
  136. #endif
  137. #endif
  138. /*
  139. * Drive state.
  140. * This is reset to 0 after a channel reset.
  141. */
  142. u_int8_t state;
  143. #define RESET 0
  144. #define READY 1
  145. #if NATA_DMA
  146. /* numbers of xfers and DMA errs. Used by ata_dmaerr() */
  147. u_int8_t n_dmaerrs;
  148. u_int32_t n_xfers;
  149. /* Downgrade after NERRS_MAX errors in at most NXFER xfers */
  150. #define NERRS_MAX 4
  151. #define NXFER 4000
  152. #endif
  153. /* Callbacks into the drive's driver. */
  154. void (*drv_done)(void *); /* transfer is done */
  155. device_t drv_softc; /* ATA drives softc, if any */
  156. void *chnl_softc; /* channel softc */
  157. };
  158. /* User config flags that force (or disable) the use of a mode */
  159. #define ATA_CONFIG_PIO_MODES 0x0007
  160. #define ATA_CONFIG_PIO_SET 0x0008
  161. #define ATA_CONFIG_PIO_OFF 0
  162. #define ATA_CONFIG_DMA_MODES 0x0070
  163. #define ATA_CONFIG_DMA_SET 0x0080
  164. #define ATA_CONFIG_DMA_DISABLE 0x0070
  165. #define ATA_CONFIG_DMA_OFF 4
  166. #define ATA_CONFIG_UDMA_MODES 0x0700
  167. #define ATA_CONFIG_UDMA_SET 0x0800
  168. #define ATA_CONFIG_UDMA_DISABLE 0x0700
  169. #define ATA_CONFIG_UDMA_OFF 8
  170. /*
  171. * Parameters/state needed by the controller to perform an ATA bio.
  172. */
  173. struct ata_bio {
  174. volatile u_int16_t flags;/* cmd flags */
  175. #define ATA_NOSLEEP 0x0001 /* Can't sleep */
  176. #define ATA_POLL 0x0002 /* poll for completion */
  177. #define ATA_ITSDONE 0x0004 /* the transfer is as done as it gets */
  178. #define ATA_SINGLE 0x0008 /* transfer must be done in singlesector mode */
  179. #define ATA_LBA 0x0010 /* transfer uses LBA addressing */
  180. #define ATA_READ 0x0020 /* transfer is a read (otherwise a write) */
  181. #define ATA_CORR 0x0040 /* transfer had a corrected error */
  182. #define ATA_LBA48 0x0080 /* transfer uses 48-bit LBA addressing */
  183. int multi; /* # of blocks to transfer in multi-mode */
  184. struct disklabel *lp; /* pointer to drive's label info */
  185. daddr_t blkno; /* block addr */
  186. daddr_t blkdone;/* number of blks transferred */
  187. daddr_t nblks; /* number of block currently transferring */
  188. int nbytes; /* number of bytes currently transferring */
  189. long bcount; /* total number of bytes */
  190. char *databuf;/* data buffer address */
  191. volatile int error;
  192. #define NOERROR 0 /* There was no error (r_error invalid) */
  193. #define ERROR 1 /* check r_error */
  194. #define ERR_DF 2 /* Drive fault */
  195. #define ERR_DMA 3 /* DMA error */
  196. #define TIMEOUT 4 /* device timed out */
  197. #define ERR_NODEV 5 /* device has been gone */
  198. #define ERR_RESET 6 /* command was terminated by channel reset */
  199. u_int8_t r_error;/* copy of error register */
  200. daddr_t badsect[127];/* 126 plus trailing -1 marker */
  201. };
  202. /*
  203. * ATA/ATAPI commands description
  204. *
  205. * This structure defines the interface between the ATA/ATAPI device driver
  206. * and the controller for short commands. It contains the command's parameter,
  207. * the len of data's to read/write (if any), and a function to call upon
  208. * completion.
  209. * If no sleep is allowed, the driver can poll for command completion.
  210. * Once the command completed, if the error registed is valid, the flag
  211. * AT_ERROR is set and the error register value is copied to r_error .
  212. * A separate interface is needed for read/write or ATAPI packet commands
  213. * (which need multiple interrupts per commands).
  214. */
  215. struct ata_command {
  216. u_int8_t r_command; /* Parameters to upload to registers */
  217. u_int8_t r_head;
  218. u_int16_t r_cyl;
  219. u_int8_t r_sector;
  220. u_int8_t r_count;
  221. u_int8_t r_features;
  222. u_int8_t r_st_bmask; /* status register mask to wait for before
  223. command */
  224. u_int8_t r_st_pmask; /* status register mask to wait for after
  225. command */
  226. u_int8_t r_error; /* error register after command done */
  227. volatile u_int16_t flags;
  228. #define AT_READ 0x0001 /* There is data to read */
  229. #define AT_WRITE 0x0002 /* There is data to write (excl. with AT_READ) */
  230. #define AT_WAIT 0x0008 /* wait in controller code for command completion */
  231. #define AT_POLL 0x0010 /* poll for command completion (no interrupts) */
  232. #define AT_DONE 0x0020 /* command is done */
  233. #define AT_XFDONE 0x0040 /* data xfer is done */
  234. #define AT_ERROR 0x0080 /* command is done with error */
  235. #define AT_TIMEOU 0x0100 /* command timed out */
  236. #define AT_DF 0x0200 /* Drive fault */
  237. #define AT_RESET 0x0400 /* command terminated by channel reset */
  238. #define AT_GONE 0x0800 /* command terminated because device is gone */
  239. #define AT_READREG 0x1000 /* Read registers on completion */
  240. int timeout; /* timeout (in ms) */
  241. void *data; /* Data buffer address */
  242. int bcount; /* number of bytes to transfer */
  243. void (*callback)(void *); /* command to call once command completed */
  244. void *callback_arg; /* argument passed to *callback() */
  245. };
  246. /*
  247. * ata_bustype. The first field must be compatible with scsipi_bustype,
  248. * as it's used for autoconfig by both ata and atapi drivers.
  249. */
  250. struct ata_bustype {
  251. int bustype_type; /* symbolic name of type */
  252. int (*ata_bio)(struct ata_drive_datas *, struct ata_bio *);
  253. void (*ata_reset_drive)(struct ata_drive_datas *, int);
  254. void (*ata_reset_channel)(struct ata_channel *, int);
  255. /* extra flags for ata_reset_*(), in addition to AT_* */
  256. #define AT_RST_EMERG 0x10000 /* emergency - e.g. for a dump */
  257. #define AT_RST_NOCMD 0x20000 /* XXX has to go - temporary until we have tagged queuing */
  258. int (*ata_exec_command)(struct ata_drive_datas *,
  259. struct ata_command *);
  260. #define ATACMD_COMPLETE 0x01
  261. #define ATACMD_QUEUED 0x02
  262. #define ATACMD_TRY_AGAIN 0x03
  263. int (*ata_get_params)(struct ata_drive_datas *, u_int8_t,
  264. struct ataparams *);
  265. int (*ata_addref)(struct ata_drive_datas *);
  266. void (*ata_delref)(struct ata_drive_datas *);
  267. void (*ata_killpending)(struct ata_drive_datas *);
  268. };
  269. /* bustype_type */ /* XXX XXX XXX */
  270. /* #define SCSIPI_BUSTYPE_SCSI 0 */
  271. /* #define SCSIPI_BUSTYPE_ATAPI 1 */
  272. #define SCSIPI_BUSTYPE_ATA 2
  273. /*
  274. * Describe an ATA device. Has to be compatible with scsipi_channel, so
  275. * start with a pointer to ata_bustype.
  276. */
  277. struct ata_device {
  278. const struct ata_bustype *adev_bustype;
  279. int adev_channel;
  280. int adev_openings;
  281. struct ata_drive_datas *adev_drv_data;
  282. };
  283. /*
  284. * Per-channel data
  285. */
  286. struct ata_channel {
  287. struct callout ch_callout; /* callout handle */
  288. int ch_channel; /* location */
  289. struct atac_softc *ch_atac; /* ATA controller softc */
  290. /* Our state */
  291. volatile int ch_flags;
  292. #define ATACH_SHUTDOWN 0x02 /* channel is shutting down */
  293. #define ATACH_IRQ_WAIT 0x10 /* controller is waiting for irq */
  294. #define ATACH_DMA_WAIT 0x20 /* controller is waiting for DMA */
  295. #define ATACH_PIOBM_WAIT 0x40 /* controller is waiting for busmastering PIO */
  296. #define ATACH_DISABLED 0x80 /* channel is disabled */
  297. #define ATACH_TH_RUN 0x100 /* the kernel thread is working */
  298. #define ATACH_TH_RESET 0x200 /* someone ask the thread to reset */
  299. u_int8_t ch_status; /* copy of status register */
  300. u_int8_t ch_error; /* copy of error register */
  301. /* for the reset callback */
  302. int ch_reset_flags;
  303. /* per-drive info */
  304. int ch_ndrive;
  305. struct ata_drive_datas ch_drive[ATA_MAXDRIVES];
  306. device_t atabus; /* self */
  307. /* ATAPI children */
  308. device_t atapibus;
  309. struct scsipi_channel ch_atapi_channel;
  310. /* ATA children */
  311. device_t ata_drives[ATA_MAXDRIVES];
  312. /*
  313. * Channel queues. May be the same for all channels, if hw
  314. * channels are not independent.
  315. */
  316. struct ata_queue *ch_queue;
  317. /* The channel kernel thread */
  318. struct lwp *ch_thread;
  319. };
  320. /*
  321. * ATA controller softc.
  322. *
  323. * This contains a bunch of generic info that all ATA controllers need
  324. * to have.
  325. *
  326. * XXX There is still some lingering wdc-centricity here.
  327. */
  328. struct atac_softc {
  329. device_t atac_dev; /* generic device info */
  330. int atac_cap; /* controller capabilities */
  331. #define ATAC_CAP_DATA16 0x0001 /* can do 16-bit data access */
  332. #define ATAC_CAP_DATA32 0x0002 /* can do 32-bit data access */
  333. #define ATAC_CAP_DMA 0x0008 /* can do ATA DMA modes */
  334. #define ATAC_CAP_UDMA 0x0010 /* can do ATA Ultra DMA modes */
  335. #define ATAC_CAP_PIOBM 0x0020 /* can do busmastering PIO transfer */
  336. #define ATAC_CAP_ATA_NOSTREAM 0x0040 /* don't use stream funcs on ATA */
  337. #define ATAC_CAP_ATAPI_NOSTREAM 0x0080 /* don't use stream funcs on ATAPI */
  338. #define ATAC_CAP_NOIRQ 0x1000 /* controller never interrupts */
  339. #define ATAC_CAP_RAID 0x4000 /* controller "supports" RAID */
  340. uint8_t atac_pio_cap; /* highest PIO mode supported */
  341. #if NATA_DMA
  342. uint8_t atac_dma_cap; /* highest DMA mode supported */
  343. #if NATA_UDMA
  344. uint8_t atac_udma_cap; /* highest UDMA mode supported */
  345. #endif
  346. #endif
  347. /* Array of pointers to channel-specific data. */
  348. struct ata_channel **atac_channels;
  349. int atac_nchannels;
  350. const struct ata_bustype *atac_bustype_ata;
  351. /*
  352. * Glue between ATA and SCSIPI for the benefit of ATAPI.
  353. *
  354. * Note: The reference count here is used for both ATA and ATAPI
  355. * devices.
  356. */
  357. struct atapi_adapter atac_atapi_adapter;
  358. void (*atac_atapibus_attach)(struct atabus_softc *);
  359. /* Driver callback to probe for drives. */
  360. void (*atac_probe)(struct ata_channel *);
  361. /* Optional callbacks to lock/unlock hardware. */
  362. int (*atac_claim_hw)(struct ata_channel *, int);
  363. void (*atac_free_hw)(struct ata_channel *);
  364. /*
  365. * Optional callbacks to set drive mode. Required for anything
  366. * but basic PIO operation.
  367. */
  368. void (*atac_set_modes)(struct ata_channel *);
  369. };
  370. #ifdef _KERNEL
  371. void ata_channel_attach(struct ata_channel *);
  372. int atabusprint(void *aux, const char *);
  373. int ataprint(void *aux, const char *);
  374. struct ataparams;
  375. int ata_get_params(struct ata_drive_datas *, u_int8_t, struct ataparams *);
  376. int ata_set_mode(struct ata_drive_datas *, u_int8_t, u_int8_t);
  377. /* return code for these cmds */
  378. #define CMD_OK 0
  379. #define CMD_ERR 1
  380. #define CMD_AGAIN 2
  381. struct ata_xfer *ata_get_xfer(int);
  382. void ata_free_xfer(struct ata_channel *, struct ata_xfer *);
  383. #define ATAXF_CANSLEEP 0x00
  384. #define ATAXF_NOSLEEP 0x01
  385. void ata_exec_xfer(struct ata_channel *, struct ata_xfer *);
  386. void ata_kill_pending(struct ata_drive_datas *);
  387. void ata_reset_channel(struct ata_channel *, int);
  388. int ata_addref(struct ata_channel *);
  389. void ata_delref(struct ata_channel *);
  390. void atastart(struct ata_channel *);
  391. void ata_print_modes(struct ata_channel *);
  392. #if NATA_DMA
  393. int ata_downgrade_mode(struct ata_drive_datas *, int);
  394. #endif
  395. void ata_probe_caps(struct ata_drive_datas *);
  396. #if NATA_DMA
  397. void ata_dmaerr(struct ata_drive_datas *, int);
  398. #endif
  399. void ata_queue_idle(struct ata_queue *);
  400. #endif /* _KERNEL */
  401. #endif /* _DEV_ATA_ATAVAR_H_ */