1 /**************************************************************************
2 Etherboot - BOOTP/TFTP Bootstrap Program
3 Bochs Pseudo NIC driver for Etherboot
4 ***************************************************************************/
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2, or (at
10 * your option) any later version.
12 * See pnic_api.h for an explanation of the Bochs Pseudo NIC.
20 #include <gpxe/if_ether.h>
21 #include <gpxe/ethernet.h>
22 #include <gpxe/pkbuff.h>
23 #include <gpxe/netdevice.h>
28 unsigned short ioaddr;
32 * Utility functions: issue a PNIC command, retrieve result. Use
33 * pnic_command_quiet if you don't want failure codes to be
34 * automatically printed. Returns the PNIC status code.
36 * Set output_length to NULL only if you expect to receive exactly
37 * output_max_length bytes, otherwise it'll complain that you didn't
38 * get enough data (on the assumption that if you not interested in
39 * discovering the output length then you're expecting a fixed amount
43 static uint16_t pnic_command_quiet ( struct pnic *pnic, uint16_t command,
44 void *input, uint16_t input_length,
45 void *output, uint16_t output_max_length,
46 uint16_t *output_length ) {
49 uint16_t _output_length;
51 if ( input != NULL ) {
52 /* Write input length */
53 outw ( input_length, pnic->ioaddr + PNIC_REG_LEN );
54 /* Write input data */
55 for ( i = 0; i < input_length; i++ ) {
56 outb( ((char*)input)[i],
57 pnic->ioaddr + PNIC_REG_DATA );
61 outw ( command, pnic->ioaddr + PNIC_REG_CMD );
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 );
73 *output_length = _output_length;
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;
82 /* Retrieve output data */
83 for ( i = 0; i < _output_length; i++ ) {
85 inb ( pnic->ioaddr + PNIC_REG_DATA );
91 static uint16_t pnic_command ( struct pnic *pnic, uint16_t command,
92 void *input, uint16_t input_length,
93 void *output, uint16_t output_max_length,
94 uint16_t *output_length ) {
95 uint16_t status = pnic_command_quiet ( pnic, command,
97 output, output_max_length,
99 if ( status == PNIC_STATUS_OK ) return status;
100 printf ( "PNIC command %#hx (len %#hx) failed with status %#hx\n",
101 command, input_length, status );
105 /* Check API version matches that of NIC */
106 static int pnic_api_check ( uint16_t api_version ) {
107 if ( api_version != PNIC_API_VERSION ) {
108 printf ( "Warning: API version mismatch! "
109 "(NIC's is %d.%d, ours is %d.%d)\n",
110 api_version >> 8, api_version & 0xff,
111 PNIC_API_VERSION >> 8, PNIC_API_VERSION & 0xff );
113 if ( api_version < PNIC_API_VERSION ) {
114 printf ( "** You may need to update your copy of Bochs **\n" );
116 return ( api_version == PNIC_API_VERSION );
119 /**************************************************************************
120 POLL - Wait for a frame
121 ***************************************************************************/
122 static void pnic_poll ( struct net_device *netdev ) {
123 struct pnic *pnic = netdev->priv;
128 /* Fetch all available packets */
130 if ( pnic_command ( pnic, PNIC_CMD_RECV_QLEN, NULL, 0,
131 &qlen, sizeof ( qlen ), NULL )
136 pkb = alloc_pkb ( ETH_FRAME_LEN );
139 if ( pnic_command ( pnic, PNIC_CMD_RECV, NULL, 0,
140 pkb->data, ETH_FRAME_LEN, &length )
141 != PNIC_STATUS_OK ) {
145 pkb_put ( pkb, length );
146 netdev_rx ( netdev, pkb );
150 /**************************************************************************
151 TRANSMIT - Transmit a frame
152 ***************************************************************************/
153 static int pnic_transmit ( struct net_device *netdev, struct pk_buff *pkb ) {
154 struct pnic *pnic = netdev->priv;
156 pnic_command ( pnic, PNIC_CMD_XMIT, pkb, pkb_len ( pkb ),
162 /**************************************************************************
163 IRQ - Handle card interrupt status
164 ***************************************************************************/
166 static void pnic_irq ( struct net_device *netdev, irq_action_t action ) {
167 struct pnic *pnic = netdev->priv;
173 enabled = ( action == ENABLE ? 1 : 0 );
174 pnic_command ( pnic, PNIC_CMD_MASK_IRQ,
175 &enabled, sizeof ( enabled ), NULL, 0, NULL );
178 pnic_command ( pnic, PNIC_CMD_FORCE_IRQ,
179 NULL, 0, NULL, 0, NULL );
185 /**************************************************************************
186 DISABLE - Turn off ethernet interface
187 ***************************************************************************/
188 static void pnic_remove ( struct pci_device *pci ) {
189 struct net_device *netdev = pci_get_drvdata ( pci );
190 struct pnic *pnic = netdev->priv;
192 unregister_netdev ( netdev );
193 pnic_command ( pnic, PNIC_CMD_RESET, NULL, 0, NULL, 0, NULL );
194 free_netdev ( netdev );
197 /**************************************************************************
198 PROBE - Look for an adapter, this routine's visible to the outside
199 ***************************************************************************/
200 static int pnic_probe ( struct pci_device *pci ) {
201 struct net_device *netdev;
203 uint16_t api_version;
207 /* Allocate net device */
208 netdev = alloc_etherdev ( sizeof ( *pnic ) );
214 pci_set_drvdata ( pci, netdev );
215 pnic->ioaddr = pci->ioaddr;
217 /* API version check */
218 status = pnic_command_quiet ( pnic, PNIC_CMD_API_VER, NULL, 0,
220 sizeof ( api_version ), NULL );
221 if ( status != PNIC_STATUS_OK ) {
222 printf ( "PNIC failed installation check, code %#hx\n",
227 pnic_api_check ( api_version );
229 /* Get MAC address */
230 status = pnic_command ( pnic, PNIC_CMD_READ_MAC, NULL, 0,
231 netdev->ll_addr, ETH_ALEN, NULL );
233 /* Point to NIC specific routines */
234 netdev->poll = pnic_poll;
235 netdev->transmit = pnic_transmit;
237 /* Register network device */
238 if ( ( rc = register_netdev ( netdev ) ) != 0 )
244 /* Free net device */
245 free_netdev ( netdev );
249 static struct pci_id pnic_nics[] = {
250 /* genrules.pl doesn't let us use macros for PCI IDs...*/
251 PCI_ROM ( 0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor" ),
254 static struct pci_driver pnic_driver = {
256 .id_count = ( sizeof ( pnic_nics ) / sizeof ( pnic_nics[0] ) ),
257 .class = PCI_NO_CLASS,
258 // .probe = pnic_probe,
259 // .remove = pnic_remove,
262 // PCI_DRIVER ( pnic_driver );
265 static int pnic_hack_probe ( void *dummy, struct pci_device *pci ) {
266 return ( pnic_probe ( pci ) == 0 );
269 static void pnic_hack_disable ( void *dummy, struct pci_device *pci ) {
274 extern struct type_driver test_driver;
276 DRIVER ( "PNIC", test_driver, pci_driver, pnic_driver,
277 pnic_hack_probe, pnic_hack_disable );