You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dev.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #ifndef DEV_H
  2. #define DEV_H
  3. #include "stdint.h"
  4. #include "string.h"
  5. #include "buffer.h"
  6. #include "dhcp.h" /* for dhcp_dev_id */
  7. #include <gpxe/tables.h>
  8. #include <assert.h>
  9. /*
  10. * Forward declarations
  11. *
  12. */
  13. struct type_dev;
  14. struct type_driver;
  15. struct bus_driver;
  16. struct bus_dev;
  17. struct device_driver;
  18. /*
  19. * When looking at the following data structures, mentally substitute
  20. * "<bus>_" in place of "bus_" and everything will become clear.
  21. * "struct bus_location" becomes "struct <bus>_location", which means
  22. * "the location of a device on a <bus> bus", where <bus> is a
  23. * particular type of bus such as "pci" or "isapnp".
  24. *
  25. */
  26. /*
  27. * A physical device location on a bus.
  28. *
  29. */
  30. #define BUS_LOC_SIZE 8
  31. struct bus_loc {
  32. char bytes[BUS_LOC_SIZE];
  33. };
  34. /*
  35. * A structure fully describing a physical device on a bus.
  36. *
  37. */
  38. #define BUS_DEV_SIZE 32
  39. struct bus_dev {
  40. char bytes[BUS_DEV_SIZE];
  41. };
  42. /*
  43. * Individual buses will have different sizes for their <bus>_location
  44. * and <bus>_device structures. We need to be able to allocate static
  45. * storage that's large enough to contain these structures for any
  46. * bus type that's being used in the current binary.
  47. *
  48. * We can't just create a union of all the various types, because some
  49. * may be architecture-dependent (and some are even embedded in
  50. * specific drivers, e.g. 3c509), so this would quickly get messy.
  51. *
  52. * We could use the magic of common symbols. Each bus could declare a
  53. * common symbol with the name "_bus_dev" of the correct size; this
  54. * is easily done using code like
  55. * struct pci_device _bus_dev;
  56. * The linker would then use the largest size of the "_bus_dev" symbol
  57. * in any included object, thus giving us a single _bus_dev symbol of
  58. * *exactly* the required size. However, there's no way to extract
  59. * the size of this symbol, either directly as a linker symbol
  60. * ("_bus_dev_size = SIZEOF(_bus_dev)"; the linker language just
  61. * doesn't provide this construct) or via any linker trickery I can
  62. * think of (such as creating a special common symbol section just for
  63. * this symbol then using SIZE(section) to read the size of the
  64. * section; ld recognises only a single common symbol section called
  65. * "COMMON").
  66. *
  67. * Since there's no way to get the size of the symbol, this
  68. * effectively limits us to just one instance of the symbol. This is
  69. * all very well for the simple case of "just boot from any single
  70. * device you can", but becomes limiting when you want to do things
  71. * like introducing PCMCIA buses (which must instantiate other devices
  72. * such as PCMCIA controllers).
  73. *
  74. * So, we declare the maximum sizes of these constructions to be
  75. * compile-time constants. Each individual bus driver should define
  76. * its own struct <bus>_location and struct <bus>_device however it
  77. * likes, and can freely cast pointers from struct bus_loc to
  78. * struct <bus>_location (and similarly for bus_dev). To guard
  79. * against bounding errors, each bus driver *MUST* use the macros
  80. * BUS_LOC_CHECK() and BUS_DEV_CHECK(), as in:
  81. *
  82. * BUS_LOC_CHECK ( struct pci_location );
  83. * BUS_DEV_CHECK ( struct pci_device );
  84. *
  85. * These macros will generate a link-time error if the size of the
  86. * <bus> structure exceeds the declared maximum size.
  87. *
  88. * The macros will generate no binary object code, but must be placed
  89. * inside a function (in order to generate syntactically valid C).
  90. * The easiest wy to do this is to place them in the
  91. * <bus>_next_location() function.
  92. *
  93. * If anyone can think of a better way of doing this that avoids *ALL*
  94. * of the problems described above, please implement it!
  95. *
  96. */
  97. #define BUS_LOC_CHECK(datatype) \
  98. linker_assert( ( sizeof (datatype) <= sizeof (struct bus_loc) ), \
  99. __BUS_LOC_SIZE_is_too_small__see_dev_h )
  100. #define BUS_DEV_CHECK(datatype) \
  101. linker_assert( ( sizeof (datatype) <= sizeof (struct bus_dev) ), \
  102. __BUS_DEV_SIZE_is_too_small__see_dev_h )
  103. /*
  104. * Bus-level operations.
  105. *
  106. * int next_location ( struct bus_loc * bus_loc )
  107. *
  108. * Increment bus_loc to point to the next possible device on
  109. * the bus (e.g. the next PCI busdevfn, or the next ISAPnP CSN).
  110. * If there are no more valid locations, return 0 and leave
  111. * struct bus_loc zeroed, otherwise return true.
  112. *
  113. * int fill_device ( struct bus_dev *bus_dev,
  114. * struct bus_loc *bus_loc )
  115. *
  116. * Fill out a bus_dev structure with the parameters for the
  117. * device at bus_loc. (For example, fill in the PCI vendor
  118. * and device IDs). Return true if there is a device physically
  119. * present at this location, otherwise 0.
  120. *
  121. * int check_driver ( struct bus_dev *bus_dev,
  122. * struct device_driver *device_driver )
  123. *
  124. * Test whether or not the specified driver is capable of driving
  125. * the specified device by, for example, comparing the device's
  126. * PCI IDs against the list of PCI IDs claimed by the driver.
  127. *
  128. * char * describe ( struct bus_dev *bus_dev )
  129. *
  130. * Return a text string describing the bus device bus_dev
  131. * (e.g. "PCI 00:01.2")
  132. *
  133. * char * name ( struct bus_dev *bus_dev )
  134. *
  135. * Return a text string describing the bus device bus_dev
  136. * (e.g. "dfe538")
  137. *
  138. */
  139. struct bus_driver {
  140. const char *name;
  141. int ( *next_location ) ( struct bus_loc *bus_loc );
  142. int ( *fill_device ) ( struct bus_dev *bus_dev,
  143. struct bus_loc *bus_loc );
  144. int ( *check_driver ) ( struct bus_dev *bus_dev,
  145. struct device_driver *device_driver );
  146. char * ( *describe_device ) ( struct bus_dev *bus_dev );
  147. const char * ( *name_device ) ( struct bus_dev *bus_dev );
  148. };
  149. #define __bus_driver __table ( bus_driver, 01 )
  150. /*
  151. * A structure fully describing the bus-independent parts of a
  152. * particular type (e.g. nic or disk) of device.
  153. *
  154. * Unlike struct bus_dev, e can limit ourselves to having no more than
  155. * one instance of this data structure. We therefore place an
  156. * instance in each type driver file (e.g. nic.c), and simply use a
  157. * pointer to the struct type_dev in the struct dev.
  158. *
  159. */
  160. struct type_dev;
  161. /*
  162. * A type driver (e.g. nic, disk)
  163. *
  164. */
  165. struct type_driver {
  166. char *name;
  167. struct type_dev *type_dev; /* single instance */
  168. char * ( * describe_device ) ( struct type_dev *type_dev );
  169. int ( * configure ) ( struct type_dev *type_dev );
  170. int ( * load ) ( struct type_dev *type_dev, struct buffer *buffer );
  171. };
  172. #define __type_driver __table ( type_driver, 01 )
  173. /*
  174. * A driver for a device.
  175. *
  176. */
  177. struct device_driver {
  178. const char *name;
  179. struct type_driver *type_driver;
  180. struct bus_driver *bus_driver;
  181. struct bus_driver_info *bus_driver_info;
  182. int ( * probe ) ( struct type_dev *type_dev,
  183. struct bus_dev *bus_dev );
  184. void ( * disable ) ( struct type_dev *type_dev,
  185. struct bus_dev *bus_dev );
  186. };
  187. #define __device_driver __table ( device_driver, 01 )
  188. #if 0
  189. #define DRIVER(_name,_type_driver,_bus_driver,_bus_info, \
  190. _probe,_disable) \
  191. struct device_driver device_ ## _bus_info __device_driver = { \
  192. .name = _name, \
  193. .type_driver = &_type_driver, \
  194. .bus_driver = &_bus_driver, \
  195. .bus_driver_info = ( struct bus_driver_info * ) &_bus_info, \
  196. .probe = ( int (*) () ) _probe, \
  197. .disable = ( void (*) () ) _disable, \
  198. };
  199. #endif
  200. #define DRIVER(a,b,c,d,e,f)
  201. /*
  202. * A bootable device, comprising a physical device on a bus, a driver
  203. * for that device, and a type device
  204. *
  205. */
  206. struct dev {
  207. struct bus_driver *bus_driver;
  208. struct bus_loc bus_loc;
  209. struct bus_dev bus_dev;
  210. struct device_driver *device_driver;
  211. struct type_driver *type_driver;
  212. struct type_dev *type_dev;
  213. };
  214. /* The current boot device */
  215. extern struct dev dev;
  216. /*
  217. * Functions in dev.c
  218. *
  219. */
  220. extern void print_drivers ( void );
  221. extern int find_any ( struct bus_driver **bus_driver, struct bus_loc *bus_loc,
  222. struct bus_dev *bus_dev, signed int skip );
  223. extern int find_by_device ( struct device_driver **device_driver,
  224. struct bus_driver *bus_driver,
  225. struct bus_dev *bus_dev,
  226. signed int skip );
  227. extern int find_by_driver ( struct bus_loc *bus_loc, struct bus_dev *bus_dev,
  228. struct device_driver *device_driver,
  229. signed int skip );
  230. extern int find_any_with_driver ( struct dev *dev, signed int skip );
  231. /*
  232. * Functions inlined to save space
  233. *
  234. */
  235. /* Probe a device */
  236. static inline int probe ( struct dev *dev ) {
  237. return dev->device_driver->probe ( dev->type_dev, &dev->bus_dev );
  238. }
  239. /* Disable a device */
  240. static inline void disable ( struct dev *dev ) {
  241. dev->device_driver->disable ( dev->type_dev, &dev->bus_dev );
  242. }
  243. /* Set the default boot device */
  244. static inline void select_device ( struct dev *dev,
  245. struct bus_driver *bus_driver,
  246. struct bus_loc *bus_loc ) {
  247. dev->bus_driver = bus_driver;
  248. memcpy ( &dev->bus_loc, bus_loc, sizeof ( dev->bus_loc ) );
  249. }
  250. /* Configure a device */
  251. static inline int configure ( struct dev *dev ) {
  252. return dev->type_driver->configure ( dev->type_dev );
  253. }
  254. /* Boot from a device */
  255. static inline int load ( struct dev *dev, struct buffer *buffer ) {
  256. return dev->type_driver->load ( dev->type_dev, buffer );
  257. }
  258. #endif /* DEV_H */