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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 type Device type
  188. * @v instance Device instance
  189. * @ret rc Return status code
  190. */
  191. static int xenbus_probe_device ( struct xen_hypervisor *xen,
  192. struct device *parent, const char *type,
  193. const char *instance ) {
  194. struct xen_device *xendev;
  195. size_t key_len;
  196. int rc;
  197. /* Allocate and initialise structure */
  198. key_len = ( 7 /* "device/" */ + strlen ( type ) + 1 /* "/" */ +
  199. strlen ( instance ) + 1 /* NUL */ );
  200. xendev = zalloc ( sizeof ( *xendev ) + key_len );
  201. if ( ! xendev ) {
  202. rc = -ENOMEM;
  203. goto err_alloc;
  204. }
  205. snprintf ( xendev->dev.name, sizeof ( xendev->dev.name ), "%s/%s",
  206. type, instance );
  207. xendev->dev.desc.bus_type = BUS_TYPE_XEN;
  208. INIT_LIST_HEAD ( &xendev->dev.children );
  209. list_add_tail ( &xendev->dev.siblings, &parent->children );
  210. xendev->dev.parent = parent;
  211. xendev->xen = xen;
  212. xendev->key = ( ( void * ) ( xendev + 1 ) );
  213. snprintf ( xendev->key, key_len, "device/%s/%s", type, instance );
  214. /* Read backend key */
  215. if ( ( rc = xenstore_read ( xen, &xendev->backend, xendev->key,
  216. "backend", NULL ) ) != 0 ) {
  217. DBGC ( xendev, "XENBUS %s could not read backend: %s\n",
  218. xendev->key, strerror ( rc ) );
  219. goto err_read_backend;
  220. }
  221. /* Read backend domain ID */
  222. if ( ( rc = xenstore_read_num ( xen, &xendev->backend_id, xendev->key,
  223. "backend-id", NULL ) ) != 0 ) {
  224. DBGC ( xendev, "XENBUS %s could not read backend-id: %s\n",
  225. xendev->key, strerror ( rc ) );
  226. goto err_read_backend_id;
  227. }
  228. DBGC ( xendev, "XENBUS %s backend=\"%s\" in domain %ld\n",
  229. xendev->key, xendev->backend, xendev->backend_id );
  230. /* Look for a driver */
  231. xendev->driver = xenbus_find_driver ( type );
  232. if ( ! xendev->driver ) {
  233. DBGC ( xendev, "XENBUS %s has no driver\n", xendev->key );
  234. /* Not a fatal error */
  235. rc = 0;
  236. goto err_no_driver;
  237. }
  238. xendev->dev.driver_name = xendev->driver->name;
  239. DBGC ( xendev, "XENBUS %s has driver \"%s\"\n", xendev->key,
  240. xendev->driver->name );
  241. /* Probe driver */
  242. if ( ( rc = xendev->driver->probe ( xendev ) ) != 0 ) {
  243. DBGC ( xendev, "XENBUS could not probe %s: %s\n",
  244. xendev->key, strerror ( rc ) );
  245. goto err_probe;
  246. }
  247. return 0;
  248. xendev->driver->remove ( xendev );
  249. err_probe:
  250. err_no_driver:
  251. err_read_backend_id:
  252. free ( xendev->backend );
  253. err_read_backend:
  254. list_del ( &xendev->dev.siblings );
  255. free ( xendev );
  256. err_alloc:
  257. return rc;
  258. }
  259. /**
  260. * Remove Xen device
  261. *
  262. * @v xendev Xen device
  263. */
  264. static void xenbus_remove_device ( struct xen_device *xendev ) {
  265. /* Remove device */
  266. xendev->driver->remove ( xendev );
  267. free ( xendev->backend );
  268. list_del ( &xendev->dev.siblings );
  269. free ( xendev );
  270. }
  271. /**
  272. * Probe Xen devices of a given type
  273. *
  274. * @v xen Xen hypervisor
  275. * @v parent Parent device
  276. * @v type Device type
  277. * @ret rc Return status code
  278. */
  279. static int xenbus_probe_type ( struct xen_hypervisor *xen,
  280. struct device *parent, const char *type ) {
  281. char *children;
  282. char *child;
  283. size_t len;
  284. int rc;
  285. /* Get children of this key */
  286. if ( ( rc = xenstore_directory ( xen, &children, &len, "device",
  287. type, NULL ) ) != 0 ) {
  288. DBGC ( xen, "XENBUS could not list \"%s\" devices: %s\n",
  289. type, strerror ( rc ) );
  290. goto err_directory;
  291. }
  292. /* Probe each child */
  293. for ( child = children ; child < ( children + len ) ;
  294. child += ( strlen ( child ) + 1 /* NUL */ ) ) {
  295. if ( ( rc = xenbus_probe_device ( xen, parent, type,
  296. child ) ) != 0 )
  297. goto err_probe_device;
  298. }
  299. free ( children );
  300. return 0;
  301. err_probe_device:
  302. free ( children );
  303. err_directory:
  304. return rc;
  305. }
  306. /**
  307. * Probe Xen bus
  308. *
  309. * @v xen Xen hypervisor
  310. * @v parent Parent device
  311. * @ret rc Return status code
  312. */
  313. int xenbus_probe ( struct xen_hypervisor *xen, struct device *parent ) {
  314. char *types;
  315. char *type;
  316. size_t len;
  317. int rc;
  318. /* Get children of "device" key */
  319. if ( ( rc = xenstore_directory ( xen, &types, &len, "device",
  320. NULL ) ) != 0 ) {
  321. DBGC ( xen, "XENBUS could not list device types: %s\n",
  322. strerror ( rc ) );
  323. goto err_directory;
  324. }
  325. /* Probe each child type */
  326. for ( type = types ; type < ( types + len ) ;
  327. type += ( strlen ( type ) + 1 /* NUL */ ) ) {
  328. if ( ( rc = xenbus_probe_type ( xen, parent, type ) ) != 0 )
  329. goto err_probe_type;
  330. }
  331. free ( types );
  332. return 0;
  333. xenbus_remove ( xen, parent );
  334. err_probe_type:
  335. free ( types );
  336. err_directory:
  337. return rc;
  338. }
  339. /**
  340. * Remove Xen bus
  341. *
  342. * @v xen Xen hypervisor
  343. * @v parent Parent device
  344. */
  345. void xenbus_remove ( struct xen_hypervisor *xen __unused,
  346. struct device *parent ) {
  347. struct xen_device *xendev;
  348. struct xen_device *tmp;
  349. /* Remove devices */
  350. list_for_each_entry_safe ( xendev, tmp, &parent->children,
  351. dev.siblings ) {
  352. xenbus_remove_device ( xendev );
  353. }
  354. }