Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

pnic.c 8.6KB

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