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

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