sd.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Storage Device.
  11. */
  12. typedef struct SDev SDev;
  13. typedef struct SDifc SDifc;
  14. typedef struct SDio SDio;
  15. typedef struct SDpart SDpart;
  16. typedef struct SDperm SDperm;
  17. typedef struct SDreq SDreq;
  18. typedef struct SDunit SDunit;
  19. struct SDperm {
  20. char* name;
  21. char* user;
  22. uint32_t perm;
  23. };
  24. struct SDpart {
  25. uint64_t start;
  26. uint64_t end;
  27. SDperm SDperm;
  28. int valid;
  29. uint32_t vers;
  30. };
  31. struct SDunit {
  32. SDev* dev;
  33. int subno;
  34. unsigned char inquiry[255]; /* format follows SCSI spec */
  35. unsigned char sense[18]; /* format follows SCSI spec */
  36. SDperm SDperm;
  37. QLock ctl;
  38. uint64_t sectors;
  39. uint32_t secsize;
  40. SDpart* part; /* nil or array of size npart */
  41. int npart;
  42. uint32_t vers;
  43. SDperm ctlperm;
  44. QLock raw; /* raw read or write in progress */
  45. uint32_t rawinuse; /* really just a test-and-set */
  46. int state;
  47. SDreq* req;
  48. SDperm rawperm;
  49. };
  50. /*
  51. * Each controller is represented by a SDev.
  52. */
  53. struct SDev {
  54. Ref r; /* Number of callers using device */
  55. SDifc* ifc; /* pnp/legacy */
  56. void* ctlr;
  57. int idno;
  58. char name[8];
  59. SDev* next;
  60. QLock ql; /* enable/disable */
  61. int enabled;
  62. int nunit; /* Number of units */
  63. QLock unitlock; /* `Loading' of units */
  64. int* unitflg; /* Unit flags */
  65. SDunit**unit;
  66. };
  67. struct SDifc {
  68. char* name;
  69. SDev* (*pnp)(void);
  70. SDev* (*legacy)(int, int);
  71. int (*enable)(SDev*);
  72. int (*disable)(SDev*);
  73. int (*verify)(SDunit*);
  74. int (*online)(SDunit*);
  75. int (*rio)(SDreq*);
  76. int (*rctl)(SDunit*, char*, int);
  77. int (*wctl)(SDunit*, Cmdbuf*);
  78. int32_t (*bio)(SDunit*, int, int, void*, int32_t, uint64_t);
  79. SDev* (*probe)(DevConf*);
  80. void (*clear)(SDev*);
  81. char* (*rtopctl)(SDev*, char*, char*);
  82. int (*wtopctl)(SDev*, Cmdbuf*);
  83. };
  84. struct SDreq {
  85. SDunit* unit;
  86. int lun;
  87. int write;
  88. unsigned char cmd[16];
  89. int clen;
  90. void* data;
  91. int dlen;
  92. int flags;
  93. int status;
  94. int32_t rlen;
  95. unsigned char sense[256];
  96. };
  97. enum {
  98. SDnosense = 0x00000001,
  99. SDvalidsense = 0x00010000,
  100. SDinq0periphqual= 0xe0,
  101. SDinq0periphtype= 0x1f,
  102. SDinq1removable = 0x80,
  103. /* periphtype values */
  104. SDperdisk = 0, /* Direct access (disk) */
  105. SDpertape = 1, /* Sequential eg, tape */
  106. SDperpr = 2, /* Printer */
  107. SDperworm = 4, /* Worm */
  108. SDpercd = 5, /* CD-ROM */
  109. SDpermo = 7, /* rewriteable MO */
  110. SDperjuke = 8, /* medium-changer */
  111. };
  112. enum {
  113. SDretry = -5, /* internal to controllers */
  114. SDmalloc = -4,
  115. SDeio = -3,
  116. SDtimeout = -2,
  117. SDnostatus = -1,
  118. SDok = 0,
  119. SDcheck = 0x02, /* check condition */
  120. SDbusy = 0x08, /* busy */
  121. SDmaxio = 2048*1024,
  122. SDnpart = 16,
  123. };
  124. /*
  125. * Allow the default #defines for sdmalloc & sdfree to be overridden by
  126. * system-specific versions. This can be used to avoid extra copying
  127. * by making sure sd buffers are cache-aligned (some ARM systems) or
  128. * page-aligned (xen) for DMA.
  129. */
  130. #ifndef sdmalloc
  131. #define sdmalloc(n) malloc(n)
  132. #define sdfree(p) free(p)
  133. #endif
  134. /*
  135. * mmc/sd/sdio host controller interface
  136. */
  137. struct SDio {
  138. char *name;
  139. int (*init)(void);
  140. void (*enable)(void);
  141. int (*inquiry)(char*, int);
  142. int (*cmd)(uint32_t, uint32_t, uint32_t*);
  143. void (*iosetup)(int, void*, int, int);
  144. void (*io)(int, unsigned char*, int);
  145. };
  146. extern SDio sdio;
  147. /* devsd.c */
  148. extern void sdadddevs(SDev*);
  149. extern void sdaddconf(SDunit*);
  150. extern void sdaddallconfs(void (*f)(SDunit*));
  151. extern void sdaddpart(SDunit*, char*, uint64_t, uint64_t);
  152. extern int sdsetsense(SDreq*, int, int, int, int);
  153. extern int sdmodesense(SDreq*, unsigned char*, void*, int);
  154. extern int sdfakescsi(SDreq*, void*, int);
  155. /* sdscsi.c */
  156. extern int scsiverify(SDunit*);
  157. extern int scsionline(SDunit*);
  158. extern int32_t scsibio(SDunit*, int, int, void*, int32_t, uint64_t);
  159. extern SDev* scsiid(SDev*, SDifc*);
  160. /*
  161. * hardware info about a device
  162. */
  163. typedef struct {
  164. uint32_t port;
  165. int size;
  166. } Devport;
  167. struct DevConf
  168. {
  169. uint32_t intnum; /* interrupt number */
  170. char *type; /* card type, malloced */
  171. int nports; /* Number of ports */
  172. Devport *ports; /* The ports themselves */
  173. };