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.

pnic.c 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**************************************************************************
  2. Etherboot - BOOTP/TFTP Bootstrap Program
  3. Bochs Pseudo NIC driver for Etherboot
  4. ***************************************************************************/
  5. /*
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. *
  21. * See pnic_api.h for an explanation of the Bochs Pseudo NIC.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER );
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <ipxe/io.h>
  27. #include <errno.h>
  28. #include <ipxe/pci.h>
  29. #include <ipxe/if_ether.h>
  30. #include <ipxe/ethernet.h>
  31. #include <ipxe/iobuf.h>
  32. #include <ipxe/netdevice.h>
  33. #include "pnic_api.h"
  34. struct pnic {
  35. unsigned short ioaddr;
  36. };
  37. /*
  38. * Utility functions: issue a PNIC command, retrieve result. Use
  39. * pnic_command_quiet if you don't want failure codes to be
  40. * automatically printed. Returns the PNIC status code.
  41. *
  42. * Set output_length to NULL only if you expect to receive exactly
  43. * output_max_length bytes, otherwise it'll complain that you didn't
  44. * get enough data (on the assumption that if you not interested in
  45. * discovering the output length then you're expecting a fixed amount
  46. * of data).
  47. */
  48. static uint16_t pnic_command_quiet ( struct pnic *pnic, uint16_t command,
  49. const void *input, uint16_t input_length,
  50. void *output, uint16_t output_max_length,
  51. uint16_t *output_length ) {
  52. uint16_t status;
  53. uint16_t _output_length;
  54. if ( input != NULL ) {
  55. /* Write input length */
  56. outw ( input_length, pnic->ioaddr + PNIC_REG_LEN );
  57. /* Write input data */
  58. outsb ( pnic->ioaddr + PNIC_REG_DATA, input, input_length );
  59. }
  60. /* Write command */
  61. outw ( command, pnic->ioaddr + PNIC_REG_CMD );
  62. /* Retrieve status */
  63. status = inw ( pnic->ioaddr + PNIC_REG_STAT );
  64. /* Retrieve output length */
  65. _output_length = inw ( pnic->ioaddr + PNIC_REG_LEN );
  66. if ( output_length == NULL ) {
  67. if ( _output_length != output_max_length ) {
  68. printf ( "pnic_command %#hx: wrong data length "
  69. "returned (expected %d, got %d)\n", command,
  70. output_max_length, _output_length );
  71. }
  72. } else {
  73. *output_length = _output_length;
  74. }
  75. if ( output != NULL ) {
  76. if ( _output_length > output_max_length ) {
  77. printf ( "pnic_command %#hx: output buffer too small "
  78. "(have %d, need %d)\n", command,
  79. output_max_length, _output_length );
  80. _output_length = output_max_length;
  81. }
  82. /* Retrieve output data */
  83. insb ( pnic->ioaddr + PNIC_REG_DATA, output, _output_length );
  84. }
  85. return status;
  86. }
  87. static uint16_t pnic_command ( struct pnic *pnic, uint16_t command,
  88. const void *input, uint16_t input_length,
  89. void *output, uint16_t output_max_length,
  90. uint16_t *output_length ) {
  91. uint16_t status = pnic_command_quiet ( pnic, command,
  92. input, input_length,
  93. output, output_max_length,
  94. output_length );
  95. if ( status == PNIC_STATUS_OK ) return status;
  96. printf ( "PNIC command %#hx (len %#hx) failed with status %#hx\n",
  97. command, input_length, status );
  98. return status;
  99. }
  100. /* Check API version matches that of NIC */
  101. static int pnic_api_check ( uint16_t api_version ) {
  102. if ( api_version != PNIC_API_VERSION ) {
  103. printf ( "Warning: API version mismatch! "
  104. "(NIC's is %d.%d, ours is %d.%d)\n",
  105. api_version >> 8, api_version & 0xff,
  106. PNIC_API_VERSION >> 8, PNIC_API_VERSION & 0xff );
  107. }
  108. if ( api_version < PNIC_API_VERSION ) {
  109. printf ( "** You may need to update your copy of Bochs **\n" );
  110. }
  111. return ( api_version == PNIC_API_VERSION );
  112. }
  113. /**************************************************************************
  114. POLL - Wait for a frame
  115. ***************************************************************************/
  116. static void pnic_poll ( struct net_device *netdev ) {
  117. struct pnic *pnic = netdev->priv;
  118. struct io_buffer *iobuf;
  119. uint16_t length;
  120. uint16_t qlen;
  121. /* Fetch all available packets */
  122. while ( 1 ) {
  123. if ( pnic_command ( pnic, PNIC_CMD_RECV_QLEN, NULL, 0,
  124. &qlen, sizeof ( qlen ), NULL )
  125. != PNIC_STATUS_OK )
  126. return;
  127. if ( qlen == 0 )
  128. return;
  129. iobuf = alloc_iob ( ETH_FRAME_LEN );
  130. if ( ! iobuf ) {
  131. DBG ( "could not allocate buffer\n" );
  132. netdev_rx_err ( netdev, NULL, -ENOMEM );
  133. return;
  134. }
  135. if ( pnic_command ( pnic, PNIC_CMD_RECV, NULL, 0,
  136. iobuf->data, ETH_FRAME_LEN, &length )
  137. != PNIC_STATUS_OK ) {
  138. netdev_rx_err ( netdev, iobuf, -EIO );
  139. return;
  140. }
  141. iob_put ( iobuf, length );
  142. netdev_rx ( netdev, iobuf );
  143. }
  144. }
  145. /**************************************************************************
  146. TRANSMIT - Transmit a frame
  147. ***************************************************************************/
  148. static int pnic_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) {
  149. struct pnic *pnic = netdev->priv;
  150. /* Pad the packet */
  151. iob_pad ( iobuf, ETH_ZLEN );
  152. /* Send packet */
  153. pnic_command ( pnic, PNIC_CMD_XMIT, iobuf->data, iob_len ( iobuf ),
  154. NULL, 0, NULL );
  155. netdev_tx_complete ( netdev, iobuf );
  156. return 0;
  157. }
  158. /**************************************************************************
  159. OPEN - Open network device
  160. ***************************************************************************/
  161. static int pnic_open ( struct net_device *netdev __unused ) {
  162. /* Nothing to do */
  163. return 0;
  164. }
  165. /**************************************************************************
  166. CLOSE - Close network device
  167. ***************************************************************************/
  168. static void pnic_close ( struct net_device *netdev __unused ) {
  169. /* Nothing to do */
  170. }
  171. /**************************************************************************
  172. IRQ - Enable/disable interrupts
  173. ***************************************************************************/
  174. static void pnic_irq ( struct net_device *netdev, int enable ) {
  175. struct pnic *pnic = netdev->priv;
  176. uint8_t mask = ( enable ? 1 : 0 );
  177. pnic_command ( pnic, PNIC_CMD_MASK_IRQ, &mask, sizeof ( mask ),
  178. NULL, 0, NULL );
  179. }
  180. /**************************************************************************
  181. OPERATIONS TABLE
  182. ***************************************************************************/
  183. static struct net_device_operations pnic_operations = {
  184. .open = pnic_open,
  185. .close = pnic_close,
  186. .transmit = pnic_transmit,
  187. .poll = pnic_poll,
  188. .irq = pnic_irq,
  189. };
  190. /**************************************************************************
  191. DISABLE - Turn off ethernet interface
  192. ***************************************************************************/
  193. static void pnic_remove ( struct pci_device *pci ) {
  194. struct net_device *netdev = pci_get_drvdata ( pci );
  195. struct pnic *pnic = netdev->priv;
  196. unregister_netdev ( netdev );
  197. pnic_command ( pnic, PNIC_CMD_RESET, NULL, 0, NULL, 0, NULL );
  198. netdev_nullify ( netdev );
  199. netdev_put ( netdev );
  200. }
  201. /**************************************************************************
  202. PROBE - Look for an adapter, this routine's visible to the outside
  203. ***************************************************************************/
  204. static int pnic_probe ( struct pci_device *pci ) {
  205. struct net_device *netdev;
  206. struct pnic *pnic;
  207. uint16_t api_version;
  208. uint16_t status;
  209. int rc;
  210. /* Allocate net device */
  211. netdev = alloc_etherdev ( sizeof ( *pnic ) );
  212. if ( ! netdev )
  213. return -ENOMEM;
  214. netdev_init ( netdev, &pnic_operations );
  215. pnic = netdev->priv;
  216. pci_set_drvdata ( pci, netdev );
  217. netdev->dev = &pci->dev;
  218. pnic->ioaddr = pci->ioaddr;
  219. /* Fix up PCI device */
  220. adjust_pci_device ( pci );
  221. /* API version check */
  222. status = pnic_command_quiet ( pnic, PNIC_CMD_API_VER, NULL, 0,
  223. &api_version,
  224. sizeof ( api_version ), NULL );
  225. if ( status != PNIC_STATUS_OK ) {
  226. printf ( "PNIC failed installation check, code %#hx\n",
  227. status );
  228. rc = -EIO;
  229. goto err;
  230. }
  231. pnic_api_check ( api_version );
  232. /* Get MAC address */
  233. status = pnic_command ( pnic, PNIC_CMD_READ_MAC, NULL, 0,
  234. netdev->hw_addr, ETH_ALEN, NULL );
  235. /* Register network device */
  236. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  237. goto err;
  238. /* Mark as link up; PNIC has no concept of link state */
  239. netdev_link_up ( netdev );
  240. return 0;
  241. err:
  242. /* Free net device */
  243. netdev_nullify ( netdev );
  244. netdev_put ( netdev );
  245. return rc;
  246. }
  247. static struct pci_device_id pnic_nics[] = {
  248. /* genrules.pl doesn't let us use macros for PCI IDs...*/
  249. PCI_ROM ( 0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor", 0 ),
  250. };
  251. struct pci_driver pnic_driver __pci_driver = {
  252. .ids = pnic_nics,
  253. .id_count = ( sizeof ( pnic_nics ) / sizeof ( pnic_nics[0] ) ),
  254. .probe = pnic_probe,
  255. .remove = pnic_remove,
  256. };