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

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