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

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