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.

interface.c 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (C) 2007 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 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 <string.h>
  25. #include <ipxe/interface.h>
  26. /** @file
  27. *
  28. * Object interfaces
  29. *
  30. */
  31. /*****************************************************************************
  32. *
  33. * The null interface
  34. *
  35. */
  36. /**
  37. * Close null interface
  38. *
  39. * @v intf Null interface
  40. * @v rc Reason for close
  41. */
  42. static void null_intf_close ( struct interface *intf __unused,
  43. int rc __unused ) {
  44. /* Do nothing. In particular, do not call intf_restart(),
  45. * since that would result in an infinite loop.
  46. */
  47. }
  48. /** Null interface operations */
  49. static struct interface_operation null_intf_op[] = {
  50. INTF_OP ( intf_close, struct interface *, null_intf_close ),
  51. };
  52. /** Null interface descriptor */
  53. struct interface_descriptor null_intf_desc =
  54. INTF_DESC_PURE ( null_intf_op );
  55. /** The null interface */
  56. struct interface null_intf = INTF_INIT ( null_intf_desc );
  57. /*****************************************************************************
  58. *
  59. * Object interface plumbing
  60. *
  61. */
  62. /**
  63. * Plug an object interface into a new destination object interface
  64. *
  65. * @v intf Object interface
  66. * @v dest New destination object interface
  67. *
  68. * The reference to the existing destination interface is dropped, a
  69. * reference to the new destination interface is obtained, and the
  70. * interface is updated to point to the new destination interface.
  71. *
  72. * Note that there is no "unplug" call; instead you must plug the
  73. * interface into a null interface.
  74. */
  75. void intf_plug ( struct interface *intf, struct interface *dest ) {
  76. DBGC ( INTF_COL ( intf ),
  77. "INTF " INTF_INTF_FMT " replug to " INTF_FMT "\n",
  78. INTF_INTF_DBG ( intf, intf->dest ), INTF_DBG ( dest ) );
  79. intf_get ( dest );
  80. intf_put ( intf->dest );
  81. intf->dest = dest;
  82. }
  83. /**
  84. * Plug two object interfaces together
  85. *
  86. * @v a Object interface A
  87. * @v b Object interface B
  88. *
  89. * Plugs interface A into interface B, and interface B into interface
  90. * A. (The basic plug() function is unidirectional; this function is
  91. * merely a shorthand for two calls to plug(), hence the name.)
  92. */
  93. void intf_plug_plug ( struct interface *a, struct interface *b ) {
  94. intf_plug ( a, b );
  95. intf_plug ( b, a );
  96. }
  97. /**
  98. * Unplug an object interface
  99. *
  100. * @v intf Object interface
  101. */
  102. void intf_unplug ( struct interface *intf ) {
  103. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " unplug\n",
  104. INTF_INTF_DBG ( intf, intf->dest ) );
  105. intf_put ( intf->dest );
  106. intf->dest = &null_intf;
  107. }
  108. /**
  109. * Ignore all further operations on an object interface
  110. *
  111. * @v intf Object interface
  112. */
  113. void intf_nullify ( struct interface *intf ) {
  114. intf->desc = &null_intf_desc;
  115. }
  116. /**
  117. * Increment reference count on an object interface
  118. *
  119. * @v intf Object interface
  120. * @ret intf Object interface
  121. */
  122. struct interface * intf_get ( struct interface *intf ) {
  123. ref_get ( intf->refcnt );
  124. return intf;
  125. }
  126. /**
  127. * Decrement reference count on an object interface
  128. *
  129. * @v intf Object interface
  130. */
  131. void intf_put ( struct interface *intf ) {
  132. ref_put ( intf->refcnt );
  133. }
  134. /**
  135. * Get pointer to object containing object interface
  136. *
  137. * @v intf Object interface
  138. * @ret object Containing object
  139. */
  140. void * intf_object ( struct interface *intf ) {
  141. return ( ( ( void * ) intf ) - intf->desc->offset );
  142. }
  143. /**
  144. * Get pass-through interface
  145. *
  146. * @v intf Object interface
  147. * @ret passthru Pass-through interface, or NULL
  148. */
  149. static struct interface * intf_get_passthru ( struct interface *intf ) {
  150. struct interface_descriptor *desc = intf->desc;
  151. if ( desc->passthru_offset ) {
  152. return ( ( ( void * ) intf ) + desc->passthru_offset );
  153. } else {
  154. return NULL;
  155. }
  156. }
  157. /**
  158. * Get object interface destination and operation method (without pass-through)
  159. *
  160. * @v intf Object interface
  161. * @v type Operation type
  162. * @ret dest Destination interface
  163. * @ret func Implementing method, or NULL
  164. */
  165. void * intf_get_dest_op_no_passthru_untyped ( struct interface *intf,
  166. void *type,
  167. struct interface **dest ) {
  168. struct interface_descriptor *desc;
  169. struct interface_operation *op;
  170. unsigned int i;
  171. *dest = intf_get ( intf->dest );
  172. desc = (*dest)->desc;
  173. for ( i = desc->num_op, op = desc->op ; i ; i--, op++ ) {
  174. if ( op->type == type )
  175. return op->func;
  176. }
  177. return NULL;
  178. }
  179. /**
  180. * Get object interface destination and operation method
  181. *
  182. * @v intf Object interface
  183. * @v type Operation type
  184. * @ret dest Destination interface
  185. * @ret func Implementing method, or NULL
  186. */
  187. void * intf_get_dest_op_untyped ( struct interface *intf, void *type,
  188. struct interface **dest ) {
  189. void *func;
  190. while ( 1 ) {
  191. /* Search for an implementing method provided by the
  192. * current destination interface.
  193. */
  194. func = intf_get_dest_op_no_passthru_untyped( intf, type, dest );
  195. if ( func )
  196. return func;
  197. /* Pass through to the underlying interface, if applicable */
  198. if ( ! ( intf = intf_get_passthru ( *dest ) ) )
  199. return NULL;
  200. intf_put ( *dest );
  201. }
  202. }
  203. /*****************************************************************************
  204. *
  205. * Generic interface operations
  206. *
  207. */
  208. /**
  209. * Close an object interface
  210. *
  211. * @v intf Object interface
  212. * @v rc Reason for close
  213. *
  214. * Note that this function merely informs the destination object that
  215. * the interface is about to be closed; it doesn't actually disconnect
  216. * the interface. In most cases, you probably want to use
  217. * intf_shutdown() or intf_restart() instead.
  218. */
  219. void intf_close ( struct interface *intf, int rc ) {
  220. struct interface *dest;
  221. intf_close_TYPE ( void * ) *op =
  222. intf_get_dest_op ( intf, intf_close, &dest );
  223. void *object = intf_object ( dest );
  224. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " close (%s)\n",
  225. INTF_INTF_DBG ( intf, dest ), strerror ( rc ) );
  226. if ( op ) {
  227. op ( object, rc );
  228. } else {
  229. /* Default is to restart the interface */
  230. intf_restart ( dest, rc );
  231. }
  232. intf_put ( dest );
  233. }
  234. /**
  235. * Shut down an object interface
  236. *
  237. * @v intf Object interface
  238. * @v rc Reason for close
  239. *
  240. * Blocks further operations from being received via the interface,
  241. * executes a close operation on the destination interface, and
  242. * unplugs the interface.
  243. */
  244. void intf_shutdown ( struct interface *intf, int rc ) {
  245. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " shutting down (%s)\n",
  246. INTF_DBG ( intf ), strerror ( rc ) );
  247. /* Block further operations */
  248. intf_nullify ( intf );
  249. /* Notify destination of close */
  250. intf_close ( intf, rc );
  251. /* Unplug interface */
  252. intf_unplug ( intf );
  253. }
  254. /**
  255. * Shut down and restart an object interface
  256. *
  257. * @v intf Object interface
  258. * @v rc Reason for close
  259. *
  260. * Shuts down the interface, then unblocks operations that were
  261. * blocked during shutdown.
  262. */
  263. void intf_restart ( struct interface *intf, int rc ) {
  264. /* Shut down the interface */
  265. intf_shutdown ( intf, rc );
  266. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " restarting\n",
  267. INTF_DBG ( intf ) );
  268. /* Restore the interface descriptor. Must be done after
  269. * shutdown (rather than inhibiting intf_shutdown() from
  270. * nullifying the descriptor) in order to avoid a potential
  271. * infinite loop as the intf_close() operations on each side
  272. * of the link call each other recursively.
  273. */
  274. intf_reinit ( intf );
  275. }
  276. /**
  277. * Poke an object interface
  278. *
  279. * @v intf Object interface
  280. * @v type Operation type
  281. *
  282. * This is a helper function to implement methods which take no
  283. * parameters and return nothing.
  284. */
  285. void intf_poke ( struct interface *intf,
  286. void ( type ) ( struct interface *intf ) ) {
  287. struct interface *dest;
  288. intf_poke_TYPE ( void * ) *op =
  289. intf_get_dest_op_untyped ( intf, type, &dest );
  290. void *object = intf_object ( dest );
  291. if ( op ) {
  292. op ( object );
  293. } else {
  294. /* Default is to do nothing */
  295. }
  296. intf_put ( dest );
  297. }