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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /* to get some global routines like printf */
  14. #include "etherboot.h"
  15. /* to get the interface to the body of the program */
  16. #include "nic.h"
  17. /* to get the PCI support functions, if this is a PCI NIC */
  18. #include "pci.h"
  19. /* PNIC API */
  20. #include "pnic_api.h"
  21. /* Function prototypes */
  22. static int pnic_api_check ( uint16_t api_version );
  23. /* NIC specific static variables go here */
  24. static uint8_t tx_buffer[ETH_FRAME_LEN];
  25. /*
  26. * Utility functions: issue a PNIC command, retrieve result. Use
  27. * pnic_command_quiet if you don't want failure codes to be
  28. * automatically printed. Returns the PNIC status code.
  29. *
  30. * Set output_length to NULL only if you expect to receive exactly
  31. * output_max_length bytes, otherwise it'll complain that you didn't
  32. * get enough data (on the assumption that if you not interested in
  33. * discovering the output length then you're expecting a fixed amount
  34. * of data).
  35. */
  36. static uint16_t pnic_command_quiet ( struct nic *nic, uint16_t command,
  37. void *input, uint16_t input_length,
  38. void *output, uint16_t output_max_length,
  39. uint16_t *output_length ) {
  40. int i;
  41. uint16_t status;
  42. uint16_t _output_length;
  43. if ( input != NULL ) {
  44. /* Write input length */
  45. outw ( input_length, nic->ioaddr + PNIC_REG_LEN );
  46. /* Write input data */
  47. for ( i = 0; i < input_length; i++ ) {
  48. outb( ((char*)input)[i], nic->ioaddr + PNIC_REG_DATA );
  49. }
  50. }
  51. /* Write command */
  52. outw ( command, nic->ioaddr + PNIC_REG_CMD );
  53. /* Retrieve status */
  54. status = inw ( nic->ioaddr + PNIC_REG_STAT );
  55. /* Retrieve output length */
  56. _output_length = inw ( nic->ioaddr + PNIC_REG_LEN );
  57. if ( output_length == NULL ) {
  58. if ( _output_length != output_max_length ) {
  59. printf ( "pnic_command %#hx: wrong data length "
  60. "returned (expected %d, got %d)\n", command,
  61. output_max_length, _output_length );
  62. }
  63. } else {
  64. *output_length = _output_length;
  65. }
  66. if ( output != NULL ) {
  67. if ( _output_length > output_max_length ) {
  68. printf ( "pnic_command %#hx: output buffer too small "
  69. "(have %d, need %d)\n", command,
  70. output_max_length, _output_length );
  71. _output_length = output_max_length;
  72. }
  73. /* Retrieve output data */
  74. for ( i = 0; i < _output_length; i++ ) {
  75. ((char*)output)[i] =
  76. inb ( nic->ioaddr + PNIC_REG_DATA );
  77. }
  78. }
  79. return status;
  80. }
  81. static uint16_t pnic_command ( struct nic *nic, uint16_t command,
  82. void *input, uint16_t input_length,
  83. void *output, uint16_t output_max_length,
  84. uint16_t *output_length ) {
  85. uint16_t status = pnic_command_quiet ( nic, command,
  86. input, input_length,
  87. output, output_max_length,
  88. output_length );
  89. if ( status == PNIC_STATUS_OK ) return status;
  90. printf ( "PNIC command %#hx (len %#hx) failed with status %#hx\n",
  91. command, input_length, status );
  92. return status;
  93. }
  94. /* Check API version matches that of NIC */
  95. static int pnic_api_check ( uint16_t api_version ) {
  96. if ( api_version != PNIC_API_VERSION ) {
  97. printf ( "Warning: API version mismatch! "
  98. "(NIC's is %d.%d, ours is %d.%d)\n",
  99. api_version >> 8, api_version & 0xff,
  100. PNIC_API_VERSION >> 8, PNIC_API_VERSION & 0xff );
  101. }
  102. if ( api_version < PNIC_API_VERSION ) {
  103. printf ( "*** You may need to update your copy of Bochs ***\n" );
  104. }
  105. return ( api_version == PNIC_API_VERSION );
  106. }
  107. /**************************************************************************
  108. CONNECT - connect adapter to the network
  109. ***************************************************************************/
  110. static int pnic_connect ( struct nic *nic __unused ) {
  111. /* Nothing to do */
  112. return 1;
  113. }
  114. /**************************************************************************
  115. POLL - Wait for a frame
  116. ***************************************************************************/
  117. static int pnic_poll ( struct nic *nic, int retrieve ) {
  118. uint16_t length;
  119. uint16_t qlen;
  120. /* Check receive queue length to see if there's anything to
  121. * get. Necessary since once we've called PNIC_CMD_RECV we
  122. * have to read out the packet, otherwise it's lost forever.
  123. */
  124. if ( pnic_command ( nic, PNIC_CMD_RECV_QLEN, NULL, 0,
  125. &qlen, sizeof(qlen), NULL )
  126. != PNIC_STATUS_OK ) return ( 0 );
  127. if ( qlen == 0 ) return ( 0 );
  128. /* There is a packet ready. Return 1 if we're only checking. */
  129. if ( ! retrieve ) return ( 1 );
  130. /* Retrieve the packet */
  131. if ( pnic_command ( nic, PNIC_CMD_RECV, NULL, 0,
  132. nic->packet, ETH_FRAME_LEN, &length )
  133. != PNIC_STATUS_OK ) return ( 0 );
  134. nic->packetlen = length;
  135. return ( 1 );
  136. }
  137. /**************************************************************************
  138. TRANSMIT - Transmit a frame
  139. ***************************************************************************/
  140. static void pnic_transmit ( struct nic *nic, const char *dest,
  141. unsigned int type, unsigned int size,
  142. const char *data ) {
  143. unsigned int nstype = htons ( type );
  144. if ( ( ETH_HLEN + size ) >= ETH_FRAME_LEN ) {
  145. printf ( "pnic_transmit: packet too large\n" );
  146. return;
  147. }
  148. /* Assemble packet */
  149. memcpy ( tx_buffer, dest, ETH_ALEN );
  150. memcpy ( tx_buffer + ETH_ALEN, nic->node_addr, ETH_ALEN );
  151. memcpy ( tx_buffer + 2 * ETH_ALEN, &nstype, 2 );
  152. memcpy ( tx_buffer + ETH_HLEN, data, size );
  153. pnic_command ( nic, PNIC_CMD_XMIT, tx_buffer, ETH_HLEN + size,
  154. NULL, 0, NULL );
  155. }
  156. /**************************************************************************
  157. DISABLE - Turn off ethernet interface
  158. ***************************************************************************/
  159. static void pnic_disable ( struct nic *nic ) {
  160. pnic_command ( nic, PNIC_CMD_RESET, NULL, 0, NULL, 0, NULL );
  161. }
  162. /**************************************************************************
  163. IRQ - Handle card interrupt status
  164. ***************************************************************************/
  165. static void pnic_irq ( struct nic *nic, irq_action_t action ) {
  166. uint8_t enabled;
  167. switch ( action ) {
  168. case DISABLE :
  169. case ENABLE :
  170. enabled = ( action == ENABLE ? 1 : 0 );
  171. pnic_command ( nic, PNIC_CMD_MASK_IRQ,
  172. &enabled, sizeof(enabled), NULL, 0, NULL );
  173. break;
  174. case FORCE :
  175. pnic_command ( nic, PNIC_CMD_FORCE_IRQ,
  176. NULL, 0, NULL, 0, NULL );
  177. break;
  178. }
  179. }
  180. /**************************************************************************
  181. NIC operations table
  182. ***************************************************************************/
  183. static struct nic_operations pnic_operations = {
  184. .connect = pnic_connect,
  185. .poll = pnic_poll,
  186. .transmit = pnic_transmit,
  187. .irq = pnic_irq,
  188. .disable = pnic_disable,
  189. };
  190. /**************************************************************************
  191. PROBE - Look for an adapter, this routine's visible to the outside
  192. ***************************************************************************/
  193. static int pnic_probe ( struct dev *dev, struct pci_device *pci ) {
  194. struct nic *nic = nic_device ( dev );
  195. uint16_t api_version;
  196. uint16_t status;
  197. /* Retrieve relevant information about PCI device */
  198. nic->ioaddr = pci->ioaddr;
  199. nic->irqno = pci->irq;
  200. /* API version check */
  201. status = pnic_command_quiet( nic, PNIC_CMD_API_VER, NULL, 0,
  202. &api_version,
  203. sizeof(api_version), NULL );
  204. if ( status != PNIC_STATUS_OK ) {
  205. printf ( "PNIC failed installation check, code %#hx\n",
  206. status );
  207. return 0;
  208. }
  209. pnic_api_check(api_version);
  210. /* Get MAC address */
  211. status = pnic_command ( nic, PNIC_CMD_READ_MAC, NULL, 0,
  212. nic->node_addr, ETH_ALEN, NULL );
  213. /* point to NIC specific routines */
  214. nic->nic_op = &pnic_operations;
  215. return 1;
  216. }
  217. static struct pci_id pnic_nics[] = {
  218. /* genrules.pl doesn't let us use macros for PCI IDs...*/
  219. PCI_ROM ( 0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor" ),
  220. };
  221. static struct pci_driver pnic_driver =
  222. PCI_DRIVER ( "PNIC", pnic_nics, PCI_NO_CLASS );
  223. BOOT_DRIVER ( "PNIC", find_pci_boot_device, pnic_driver, pnic_probe );