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

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