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.

xenbus.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <ipxe/malloc.h>
  27. #include <ipxe/device.h>
  28. #include <ipxe/timer.h>
  29. #include <ipxe/nap.h>
  30. #include <ipxe/xen.h>
  31. #include <ipxe/xenstore.h>
  32. #include <ipxe/xenbus.h>
  33. /** @file
  34. *
  35. * Xen device bus
  36. *
  37. */
  38. /* Disambiguate the various error causes */
  39. #define ETIMEDOUT_UNKNOWN \
  40. __einfo_error ( EINFO_ETIMEDOUT_UNKNOWN )
  41. #define EINFO_ETIMEDOUT_UNKNOWN \
  42. __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateUnknown, \
  43. "Unknown" )
  44. #define ETIMEDOUT_INITIALISING \
  45. __einfo_error ( EINFO_ETIMEDOUT_INITIALISING )
  46. #define EINFO_ETIMEDOUT_INITIALISING \
  47. __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateInitialising, \
  48. "Initialising" )
  49. #define ETIMEDOUT_INITWAIT \
  50. __einfo_error ( EINFO_ETIMEDOUT_INITWAIT )
  51. #define EINFO_ETIMEDOUT_INITWAIT \
  52. __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateInitWait, \
  53. "InitWait" )
  54. #define ETIMEDOUT_INITIALISED \
  55. __einfo_error ( EINFO_ETIMEDOUT_INITIALISED )
  56. #define EINFO_ETIMEDOUT_INITIALISED \
  57. __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateInitialised, \
  58. "Initialised" )
  59. #define ETIMEDOUT_CONNECTED \
  60. __einfo_error ( EINFO_ETIMEDOUT_CONNECTED )
  61. #define EINFO_ETIMEDOUT_CONNECTED \
  62. __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateConnected, \
  63. "Connected" )
  64. #define ETIMEDOUT_CLOSING \
  65. __einfo_error ( EINFO_ETIMEDOUT_CLOSING )
  66. #define EINFO_ETIMEDOUT_CLOSING \
  67. __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateClosing, \
  68. "Closing" )
  69. #define ETIMEDOUT_CLOSED \
  70. __einfo_error ( EINFO_ETIMEDOUT_CLOSED )
  71. #define EINFO_ETIMEDOUT_CLOSED \
  72. __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateClosed, \
  73. "Closed" )
  74. #define ETIMEDOUT_RECONFIGURING \
  75. __einfo_error ( EINFO_ETIMEDOUT_RECONFIGURING )
  76. #define EINFO_ETIMEDOUT_RECONFIGURING \
  77. __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateReconfiguring, \
  78. "Reconfiguring" )
  79. #define ETIMEDOUT_RECONFIGURED \
  80. __einfo_error ( EINFO_ETIMEDOUT_RECONFIGURED )
  81. #define EINFO_ETIMEDOUT_RECONFIGURED \
  82. __einfo_uniqify ( EINFO_ETIMEDOUT, XenbusStateReconfigured, \
  83. "Reconfigured" )
  84. #define ETIMEDOUT_STATE( state ) \
  85. EUNIQ ( EINFO_ETIMEDOUT, (state), ETIMEDOUT_UNKNOWN, \
  86. ETIMEDOUT_INITIALISING, ETIMEDOUT_INITWAIT, \
  87. ETIMEDOUT_INITIALISED, ETIMEDOUT_CONNECTED, \
  88. ETIMEDOUT_CLOSING, ETIMEDOUT_CLOSED, \
  89. ETIMEDOUT_RECONFIGURING, ETIMEDOUT_RECONFIGURED )
  90. /** Maximum time to wait for backend to reach a given state, in ticks */
  91. #define XENBUS_BACKEND_TIMEOUT ( 5 * TICKS_PER_SEC )
  92. /**
  93. * Set device state
  94. *
  95. * @v xendev Xen device
  96. * @v state New state
  97. * @ret rc Return status code
  98. */
  99. int xenbus_set_state ( struct xen_device *xendev, int state ) {
  100. int rc;
  101. /* Attempt to set state */
  102. if ( ( rc = xenstore_write_num ( xendev->xen, state, xendev->key,
  103. "state", NULL ) ) != 0 ) {
  104. DBGC ( xendev, "XENBUS %s could not set state=\"%d\": %s\n",
  105. xendev->key, state, strerror ( rc ) );
  106. return rc;
  107. }
  108. return 0;
  109. }
  110. /**
  111. * Get backend state
  112. *
  113. * @v xendev Xen device
  114. * @ret state Backend state, or negative error
  115. */
  116. int xenbus_backend_state ( struct xen_device *xendev ) {
  117. unsigned long state;
  118. int rc;
  119. /* Attempt to get backend state */
  120. if ( ( rc = xenstore_read_num ( xendev->xen, &state, xendev->backend,
  121. "state", NULL ) ) != 0 ) {
  122. DBGC ( xendev, "XENBUS %s could not read %s/state: %s\n",
  123. xendev->key, xendev->backend, strerror ( rc ) );
  124. return rc;
  125. }
  126. return state;
  127. }
  128. /**
  129. * Wait for backend to reach a given state
  130. *
  131. * @v xendev Xen device
  132. * @v state Desired backend state
  133. * @ret rc Return status code
  134. */
  135. int xenbus_backend_wait ( struct xen_device *xendev, int state ) {
  136. unsigned long started = currticks();
  137. unsigned long elapsed;
  138. unsigned int attempts = 0;
  139. int current_state;
  140. int rc;
  141. /* Wait for backend to reach this state */
  142. do {
  143. /* Get current backend state */
  144. current_state = xenbus_backend_state ( xendev );
  145. if ( current_state < 0 ) {
  146. rc = current_state;
  147. return rc;
  148. }
  149. if ( current_state == state )
  150. return 0;
  151. /* Allow time for backend to react */
  152. cpu_nap();
  153. /* XenStore is a very slow interface; any fixed delay
  154. * time would be dwarfed by the XenStore access time.
  155. * We therefore use wall clock to time out this
  156. * operation.
  157. */
  158. elapsed = ( currticks() - started );
  159. attempts++;
  160. } while ( elapsed < XENBUS_BACKEND_TIMEOUT );
  161. /* Construct status code from current backend state */
  162. rc = -ETIMEDOUT_STATE ( current_state );
  163. DBGC ( xendev, "XENBUS %s timed out after %d attempts waiting for "
  164. "%s/state=\"%d\": %s\n", xendev->key, attempts, xendev->backend,
  165. state, strerror ( rc ) );
  166. return rc;
  167. }
  168. /**
  169. * Find driver for Xen device
  170. *
  171. * @v type Device type
  172. * @ret driver Driver, or NULL
  173. */
  174. static struct xen_driver * xenbus_find_driver ( const char *type ) {
  175. struct xen_driver *xendrv;
  176. for_each_table_entry ( xendrv, XEN_DRIVERS ) {
  177. if ( strcmp ( xendrv->type, type ) == 0 )
  178. return xendrv;
  179. }
  180. return NULL;
  181. }
  182. /**
  183. * Probe Xen device
  184. *
  185. * @v xen Xen hypervisor
  186. * @v parent Parent device
  187. * @v instance Device instance
  188. * @v driver Device driver
  189. * @ret rc Return status code
  190. */
  191. static int xenbus_probe_device ( struct xen_hypervisor *xen,
  192. struct device *parent, const char *instance,
  193. struct xen_driver *driver ) {
  194. const char *type = driver->type;
  195. struct xen_device *xendev;
  196. size_t key_len;
  197. int rc;
  198. /* Allocate and initialise structure */
  199. key_len = ( 7 /* "device/" */ + strlen ( type ) + 1 /* "/" */ +
  200. strlen ( instance ) + 1 /* NUL */ );
  201. xendev = zalloc ( sizeof ( *xendev ) + key_len );
  202. if ( ! xendev ) {
  203. rc = -ENOMEM;
  204. goto err_alloc;
  205. }
  206. snprintf ( xendev->dev.name, sizeof ( xendev->dev.name ), "%s/%s",
  207. type, instance );
  208. xendev->dev.desc.bus_type = BUS_TYPE_XEN;
  209. INIT_LIST_HEAD ( &xendev->dev.children );
  210. list_add_tail ( &xendev->dev.siblings, &parent->children );
  211. xendev->dev.parent = parent;
  212. xendev->xen = xen;
  213. xendev->key = ( ( void * ) ( xendev + 1 ) );
  214. snprintf ( xendev->key, key_len, "device/%s/%s", type, instance );
  215. xendev->driver = driver;
  216. xendev->dev.driver_name = driver->name;
  217. DBGC ( xendev, "XENBUS %s has driver \"%s\"\n", xendev->key,
  218. xendev->driver->name );
  219. /* Read backend key */
  220. if ( ( rc = xenstore_read ( xen, &xendev->backend, xendev->key,
  221. "backend", NULL ) ) != 0 ) {
  222. DBGC ( xendev, "XENBUS %s could not read backend: %s\n",
  223. xendev->key, strerror ( rc ) );
  224. goto err_read_backend;
  225. }
  226. /* Read backend domain ID */
  227. if ( ( rc = xenstore_read_num ( xen, &xendev->backend_id, xendev->key,
  228. "backend-id", NULL ) ) != 0 ) {
  229. DBGC ( xendev, "XENBUS %s could not read backend-id: %s\n",
  230. xendev->key, strerror ( rc ) );
  231. goto err_read_backend_id;
  232. }
  233. DBGC ( xendev, "XENBUS %s backend=\"%s\" in domain %ld\n",
  234. xendev->key, xendev->backend, xendev->backend_id );
  235. /* Probe driver */
  236. if ( ( rc = xendev->driver->probe ( xendev ) ) != 0 ) {
  237. DBGC ( xendev, "XENBUS could not probe %s: %s\n",
  238. xendev->key, strerror ( rc ) );
  239. goto err_probe;
  240. }
  241. return 0;
  242. xendev->driver->remove ( xendev );
  243. err_probe:
  244. err_read_backend_id:
  245. free ( xendev->backend );
  246. err_read_backend:
  247. list_del ( &xendev->dev.siblings );
  248. free ( xendev );
  249. err_alloc:
  250. return rc;
  251. }
  252. /**
  253. * Remove Xen device
  254. *
  255. * @v xendev Xen device
  256. */
  257. static void xenbus_remove_device ( struct xen_device *xendev ) {
  258. /* Remove device */
  259. xendev->driver->remove ( xendev );
  260. free ( xendev->backend );
  261. list_del ( &xendev->dev.siblings );
  262. free ( xendev );
  263. }
  264. /**
  265. * Probe Xen devices of a given type
  266. *
  267. * @v xen Xen hypervisor
  268. * @v parent Parent device
  269. * @v type Device type
  270. * @ret rc Return status code
  271. */
  272. static int xenbus_probe_type ( struct xen_hypervisor *xen,
  273. struct device *parent, const char *type ) {
  274. struct xen_driver *driver;
  275. char *children;
  276. char *child;
  277. size_t len;
  278. int rc;
  279. /* Look for a driver */
  280. driver = xenbus_find_driver ( type );
  281. if ( ! driver ) {
  282. DBGC ( xen, "XENBUS has no driver for \"%s\" devices\n", type );
  283. /* Not a fatal error */
  284. rc = 0;
  285. goto err_no_driver;
  286. }
  287. /* Get children of this key */
  288. if ( ( rc = xenstore_directory ( xen, &children, &len, "device",
  289. type, NULL ) ) != 0 ) {
  290. DBGC ( xen, "XENBUS could not list \"%s\" devices: %s\n",
  291. type, strerror ( rc ) );
  292. goto err_directory;
  293. }
  294. /* Probe each child */
  295. for ( child = children ; child < ( children + len ) ;
  296. child += ( strlen ( child ) + 1 /* NUL */ ) ) {
  297. if ( ( rc = xenbus_probe_device ( xen, parent, child,
  298. driver ) ) != 0 )
  299. goto err_probe_device;
  300. }
  301. free ( children );
  302. return 0;
  303. err_probe_device:
  304. free ( children );
  305. err_directory:
  306. err_no_driver:
  307. return rc;
  308. }
  309. /**
  310. * Probe Xen bus
  311. *
  312. * @v xen Xen hypervisor
  313. * @v parent Parent device
  314. * @ret rc Return status code
  315. */
  316. int xenbus_probe ( struct xen_hypervisor *xen, struct device *parent ) {
  317. char *types;
  318. char *type;
  319. size_t len;
  320. int rc;
  321. /* Get children of "device" key */
  322. if ( ( rc = xenstore_directory ( xen, &types, &len, "device",
  323. NULL ) ) != 0 ) {
  324. DBGC ( xen, "XENBUS could not list device types: %s\n",
  325. strerror ( rc ) );
  326. goto err_directory;
  327. }
  328. /* Probe each child type */
  329. for ( type = types ; type < ( types + len ) ;
  330. type += ( strlen ( type ) + 1 /* NUL */ ) ) {
  331. if ( ( rc = xenbus_probe_type ( xen, parent, type ) ) != 0 )
  332. goto err_probe_type;
  333. }
  334. free ( types );
  335. return 0;
  336. xenbus_remove ( xen, parent );
  337. err_probe_type:
  338. free ( types );
  339. err_directory:
  340. return rc;
  341. }
  342. /**
  343. * Remove Xen bus
  344. *
  345. * @v xen Xen hypervisor
  346. * @v parent Parent device
  347. */
  348. void xenbus_remove ( struct xen_hypervisor *xen __unused,
  349. struct device *parent ) {
  350. struct xen_device *xendev;
  351. struct xen_device *tmp;
  352. /* Remove devices */
  353. list_for_each_entry_safe ( xendev, tmp, &parent->children,
  354. dev.siblings ) {
  355. xenbus_remove_device ( xendev );
  356. }
  357. }