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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <io.h>
  15. #include <vsprintf.h>
  16. #include <errno.h>
  17. #include <gpxe/pci.h>
  18. #include <gpxe/if_ether.h>
  19. #include <gpxe/ethernet.h>
  20. #include <gpxe/pkbuff.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. 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. 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, unsigned int rx_quota ) {
  106. struct pnic *pnic = netdev->priv;
  107. struct pk_buff *pkb;
  108. uint16_t length;
  109. uint16_t qlen;
  110. /* Fetch all available packets */
  111. while ( rx_quota ) {
  112. if ( pnic_command ( pnic, PNIC_CMD_RECV_QLEN, NULL, 0,
  113. &qlen, sizeof ( qlen ), NULL )
  114. != PNIC_STATUS_OK )
  115. break;
  116. if ( qlen == 0 )
  117. break;
  118. pkb = alloc_pkb ( ETH_FRAME_LEN );
  119. if ( ! pkb ) {
  120. printf ( "could not allocate buffer\n" );
  121. break;
  122. }
  123. if ( pnic_command ( pnic, PNIC_CMD_RECV, NULL, 0,
  124. pkb->data, ETH_FRAME_LEN, &length )
  125. != PNIC_STATUS_OK ) {
  126. free_pkb ( pkb );
  127. break;
  128. }
  129. pkb_put ( pkb, length );
  130. netdev_rx ( netdev, pkb );
  131. --rx_quota;
  132. }
  133. }
  134. /**************************************************************************
  135. TRANSMIT - Transmit a frame
  136. ***************************************************************************/
  137. static int pnic_transmit ( struct net_device *netdev, struct pk_buff *pkb ) {
  138. struct pnic *pnic = netdev->priv;
  139. int pad_len;
  140. /* Pad to minimum packet length */
  141. pad_len = ( ETH_ZLEN - pkb_len ( pkb ) );
  142. if ( pad_len > 0 )
  143. memset ( pkb_put ( pkb, pad_len ), 0, pad_len );
  144. /* Send packet */
  145. pnic_command ( pnic, PNIC_CMD_XMIT, pkb->data, pkb_len ( pkb ),
  146. NULL, 0, NULL );
  147. netdev_tx_complete ( netdev, pkb );
  148. return 0;
  149. }
  150. /**************************************************************************
  151. IRQ - Handle card interrupt status
  152. ***************************************************************************/
  153. #if 0
  154. static void pnic_irq ( struct net_device *netdev, irq_action_t action ) {
  155. struct pnic *pnic = netdev->priv;
  156. uint8_t enabled;
  157. switch ( action ) {
  158. case DISABLE :
  159. case ENABLE :
  160. enabled = ( action == ENABLE ? 1 : 0 );
  161. pnic_command ( pnic, PNIC_CMD_MASK_IRQ,
  162. &enabled, sizeof ( enabled ), NULL, 0, NULL );
  163. break;
  164. case FORCE :
  165. pnic_command ( pnic, PNIC_CMD_FORCE_IRQ,
  166. NULL, 0, NULL, 0, NULL );
  167. break;
  168. }
  169. }
  170. #endif
  171. /**************************************************************************
  172. OPEN - Open network device
  173. ***************************************************************************/
  174. static int pnic_open ( struct net_device *netdev __unused ) {
  175. return 0;
  176. }
  177. /**************************************************************************
  178. CLOSE - Close network device
  179. ***************************************************************************/
  180. static void pnic_close ( struct net_device *netdev __unused ) {
  181. /* Nothing to do */
  182. }
  183. /**************************************************************************
  184. DISABLE - Turn off ethernet interface
  185. ***************************************************************************/
  186. static void pnic_remove ( struct pci_device *pci ) {
  187. struct net_device *netdev = pci_get_drvdata ( pci );
  188. struct pnic *pnic = netdev->priv;
  189. unregister_netdev ( netdev );
  190. pnic_command ( pnic, PNIC_CMD_RESET, NULL, 0, NULL, 0, NULL );
  191. free_netdev ( netdev );
  192. }
  193. /**************************************************************************
  194. PROBE - Look for an adapter, this routine's visible to the outside
  195. ***************************************************************************/
  196. static int pnic_probe ( struct pci_device *pci,
  197. const struct pci_device_id *id __unused ) {
  198. struct net_device *netdev;
  199. struct pnic *pnic;
  200. uint16_t api_version;
  201. uint16_t status;
  202. int rc;
  203. /* Fix up PCI device */
  204. adjust_pci_device ( pci );
  205. /* Allocate net device */
  206. netdev = alloc_etherdev ( sizeof ( *pnic ) );
  207. if ( ! netdev ) {
  208. rc = -ENOMEM;
  209. goto err;
  210. }
  211. pnic = netdev->priv;
  212. pci_set_drvdata ( pci, netdev );
  213. netdev->dev = &pci->dev;
  214. pnic->ioaddr = pci->ioaddr;
  215. /* API version check */
  216. status = pnic_command_quiet ( pnic, PNIC_CMD_API_VER, NULL, 0,
  217. &api_version,
  218. sizeof ( api_version ), NULL );
  219. if ( status != PNIC_STATUS_OK ) {
  220. printf ( "PNIC failed installation check, code %#hx\n",
  221. status );
  222. rc = -EIO;
  223. goto err;
  224. }
  225. pnic_api_check ( api_version );
  226. /* Get MAC address */
  227. status = pnic_command ( pnic, PNIC_CMD_READ_MAC, NULL, 0,
  228. netdev->ll_addr, ETH_ALEN, NULL );
  229. /* Point to NIC specific routines */
  230. netdev->open = pnic_open;
  231. netdev->close = pnic_close;
  232. netdev->poll = pnic_poll;
  233. netdev->transmit = pnic_transmit;
  234. /* Register network device */
  235. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  236. goto err;
  237. return 0;
  238. err:
  239. /* Free net device */
  240. free_netdev ( netdev );
  241. return rc;
  242. }
  243. static struct pci_device_id pnic_nics[] = {
  244. /* genrules.pl doesn't let us use macros for PCI IDs...*/
  245. PCI_ROM ( 0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor" ),
  246. };
  247. struct pci_driver pnic_driver __pci_driver = {
  248. .ids = pnic_nics,
  249. .id_count = ( sizeof ( pnic_nics ) / sizeof ( pnic_nics[0] ) ),
  250. .probe = pnic_probe,
  251. .remove = pnic_remove,
  252. };