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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. intf_plug ( intf, &null_intf );
  104. }
  105. /**
  106. * Ignore all further operations on an object interface
  107. *
  108. * @v intf Object interface
  109. */
  110. void intf_nullify ( struct interface *intf ) {
  111. intf->desc = &null_intf_desc;
  112. }
  113. /**
  114. * Increment reference count on an object interface
  115. *
  116. * @v intf Object interface
  117. * @ret intf Object interface
  118. */
  119. struct interface * intf_get ( struct interface *intf ) {
  120. ref_get ( intf->refcnt );
  121. return intf;
  122. }
  123. /**
  124. * Decrement reference count on an object interface
  125. *
  126. * @v intf Object interface
  127. */
  128. void intf_put ( struct interface *intf ) {
  129. ref_put ( intf->refcnt );
  130. }
  131. /**
  132. * Get pointer to object containing object interface
  133. *
  134. * @v intf Object interface
  135. * @ret object Containing object
  136. */
  137. void * intf_object ( struct interface *intf ) {
  138. return ( ( ( void * ) intf ) - intf->desc->offset );
  139. }
  140. /**
  141. * Get pass-through interface
  142. *
  143. * @v intf Object interface
  144. * @ret passthru Pass-through interface, or NULL
  145. */
  146. static struct interface * intf_get_passthru ( struct interface *intf ) {
  147. struct interface_descriptor *desc = intf->desc;
  148. if ( desc->passthru_offset ) {
  149. return ( ( ( void * ) intf ) + desc->passthru_offset );
  150. } else {
  151. return NULL;
  152. }
  153. }
  154. /**
  155. * Get object interface destination and operation method (without pass-through)
  156. *
  157. * @v intf Object interface
  158. * @v type Operation type
  159. * @ret dest Destination interface
  160. * @ret func Implementing method, or NULL
  161. */
  162. void * intf_get_dest_op_no_passthru_untyped ( struct interface *intf,
  163. void *type,
  164. struct interface **dest ) {
  165. struct interface_descriptor *desc;
  166. struct interface_operation *op;
  167. unsigned int i;
  168. *dest = intf_get ( intf->dest );
  169. desc = (*dest)->desc;
  170. for ( i = desc->num_op, op = desc->op ; i ; i--, op++ ) {
  171. if ( op->type == type )
  172. return op->func;
  173. }
  174. return NULL;
  175. }
  176. /**
  177. * Get object interface destination and operation method
  178. *
  179. * @v intf Object interface
  180. * @v type Operation type
  181. * @ret dest Destination interface
  182. * @ret func Implementing method, or NULL
  183. */
  184. void * intf_get_dest_op_untyped ( struct interface *intf, void *type,
  185. struct interface **dest ) {
  186. void *func;
  187. while ( 1 ) {
  188. /* Search for an implementing method provided by the
  189. * current destination interface.
  190. */
  191. func = intf_get_dest_op_no_passthru_untyped( intf, type, dest );
  192. if ( func )
  193. return func;
  194. /* Pass through to the underlying interface, if applicable */
  195. if ( ! ( intf = intf_get_passthru ( *dest ) ) )
  196. return NULL;
  197. intf_put ( *dest );
  198. }
  199. }
  200. /*****************************************************************************
  201. *
  202. * Generic interface operations
  203. *
  204. */
  205. /**
  206. * Close an object interface
  207. *
  208. * @v intf Object interface
  209. * @v rc Reason for close
  210. *
  211. * Note that this function merely informs the destination object that
  212. * the interface is about to be closed; it doesn't actually disconnect
  213. * the interface. In most cases, you probably want to use
  214. * intf_shutdown() or intf_restart() instead.
  215. */
  216. void intf_close ( struct interface *intf, int rc ) {
  217. struct interface *dest;
  218. intf_close_TYPE ( void * ) *op =
  219. intf_get_dest_op ( intf, intf_close, &dest );
  220. void *object = intf_object ( dest );
  221. DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " close (%s)\n",
  222. INTF_INTF_DBG ( intf, dest ), strerror ( rc ) );
  223. if ( op ) {
  224. op ( object, rc );
  225. } else {
  226. /* Default is to restart the interface */
  227. intf_restart ( dest, rc );
  228. }
  229. intf_put ( dest );
  230. }
  231. /**
  232. * Shut down an object interface
  233. *
  234. * @v intf Object interface
  235. * @v rc Reason for close
  236. *
  237. * Blocks further operations from being received via the interface,
  238. * executes a close operation on the destination interface, and
  239. * unplugs the interface.
  240. */
  241. void intf_shutdown ( struct interface *intf, int rc ) {
  242. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " shutting down (%s)\n",
  243. INTF_DBG ( intf ), strerror ( rc ) );
  244. /* Block further operations */
  245. intf_nullify ( intf );
  246. /* Notify destination of close */
  247. intf_close ( intf, rc );
  248. /* Unplug interface */
  249. intf_unplug ( intf );
  250. }
  251. /**
  252. * Shut down and restart an object interface
  253. *
  254. * @v intf Object interface
  255. * @v rc Reason for close
  256. *
  257. * Shuts down the interface, then unblocks operations that were
  258. * blocked during shutdown.
  259. */
  260. void intf_restart ( struct interface *intf, int rc ) {
  261. struct interface_descriptor *desc = intf->desc;
  262. /* Shut down the interface */
  263. intf_shutdown ( intf, rc );
  264. DBGC ( INTF_COL ( intf ), "INTF " INTF_FMT " restarting\n",
  265. INTF_DBG ( intf ) );
  266. /* Restore the interface descriptor. Must be done after
  267. * shutdown (rather than inhibiting intf_shutdown() from
  268. * nullifying the descriptor) in order to avoid a potential
  269. * infinite loop as the intf_close() operations on each side
  270. * of the link call each other recursively.
  271. */
  272. intf->desc = desc;
  273. }