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/iobuf.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 const void *input, uint16_t input_length,
45 void *output, uint16_t output_max_length,
46 uint16_t *output_length ) {
48 uint16_t _output_length;
50 if ( input != NULL ) {
51 /* Write input length */
52 outw ( input_length, pnic->ioaddr + PNIC_REG_LEN );
53 /* Write input data */
54 outsb ( pnic->ioaddr + PNIC_REG_DATA, input, input_length );
57 outw ( command, pnic->ioaddr + PNIC_REG_CMD );
59 status = inw ( pnic->ioaddr + PNIC_REG_STAT );
60 /* Retrieve output length */
61 _output_length = inw ( pnic->ioaddr + PNIC_REG_LEN );
62 if ( output_length == NULL ) {
63 if ( _output_length != output_max_length ) {
64 printf ( "pnic_command %#hx: wrong data length "
65 "returned (expected %d, got %d)\n", command,
66 output_max_length, _output_length );
69 *output_length = _output_length;
71 if ( output != NULL ) {
72 if ( _output_length > output_max_length ) {
73 printf ( "pnic_command %#hx: output buffer too small "
74 "(have %d, need %d)\n", command,
75 output_max_length, _output_length );
76 _output_length = output_max_length;
78 /* Retrieve output data */
79 insb ( pnic->ioaddr + PNIC_REG_DATA, output, _output_length );
84 static uint16_t pnic_command ( struct pnic *pnic, uint16_t command,
85 const void *input, uint16_t input_length,
86 void *output, uint16_t output_max_length,
87 uint16_t *output_length ) {
88 uint16_t status = pnic_command_quiet ( pnic, command,
90 output, output_max_length,
92 if ( status == PNIC_STATUS_OK ) return status;
93 printf ( "PNIC command %#hx (len %#hx) failed with status %#hx\n",
94 command, input_length, status );
98 /* Check API version matches that of NIC */
99 static int pnic_api_check ( uint16_t api_version ) {
100 if ( api_version != PNIC_API_VERSION ) {
101 printf ( "Warning: API version mismatch! "
102 "(NIC's is %d.%d, ours is %d.%d)\n",
103 api_version >> 8, api_version & 0xff,
104 PNIC_API_VERSION >> 8, PNIC_API_VERSION & 0xff );
106 if ( api_version < PNIC_API_VERSION ) {
107 printf ( "** You may need to update your copy of Bochs **\n" );
109 return ( api_version == PNIC_API_VERSION );
112 /**************************************************************************
113 POLL - Wait for a frame
114 ***************************************************************************/
115 static void pnic_poll ( struct net_device *netdev, unsigned int rx_quota ) {
116 struct pnic *pnic = netdev->priv;
117 struct io_buffer *iobuf;
121 /* Fetch all available packets */
123 if ( pnic_command ( pnic, PNIC_CMD_RECV_QLEN, NULL, 0,
124 &qlen, sizeof ( qlen ), NULL )
129 iobuf = alloc_iob ( ETH_FRAME_LEN );
131 printf ( "could not allocate buffer\n" );
134 if ( pnic_command ( pnic, PNIC_CMD_RECV, NULL, 0,
135 iobuf->data, ETH_FRAME_LEN, &length )
136 != PNIC_STATUS_OK ) {
140 iob_put ( iobuf, length );
141 netdev_rx ( netdev, iobuf );
146 /**************************************************************************
147 TRANSMIT - Transmit a frame
148 ***************************************************************************/
149 static int pnic_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) {
150 struct pnic *pnic = netdev->priv;
153 iob_pad ( iobuf, ETH_ZLEN );
156 pnic_command ( pnic, PNIC_CMD_XMIT, iobuf->data, iob_len ( iobuf ),
159 netdev_tx_complete ( netdev, iobuf );
163 /**************************************************************************
164 IRQ - Handle card interrupt status
165 ***************************************************************************/
167 static void pnic_irq ( struct net_device *netdev, irq_action_t action ) {
168 struct pnic *pnic = netdev->priv;
174 enabled = ( action == ENABLE ? 1 : 0 );
175 pnic_command ( pnic, PNIC_CMD_MASK_IRQ,
176 &enabled, sizeof ( enabled ), NULL, 0, NULL );
179 pnic_command ( pnic, PNIC_CMD_FORCE_IRQ,
180 NULL, 0, NULL, 0, NULL );
186 /**************************************************************************
187 OPEN - Open network device
188 ***************************************************************************/
189 static int pnic_open ( struct net_device *netdev ) {
190 struct pnic *pnic = netdev->priv;
191 static const uint8_t enable = 1;
193 /* Enable interrupts */
194 pnic_command ( pnic, PNIC_CMD_MASK_IRQ, &enable,
195 sizeof ( enable ), NULL, 0, NULL );
200 /**************************************************************************
201 CLOSE - Close network device
202 ***************************************************************************/
203 static void pnic_close ( struct net_device *netdev ) {
204 struct pnic *pnic = netdev->priv;
205 static const uint8_t disable = 0;
207 /* Disable interrupts */
208 pnic_command ( pnic, PNIC_CMD_MASK_IRQ, &disable,
209 sizeof ( disable ), NULL, 0, NULL );
212 /**************************************************************************
213 DISABLE - Turn off ethernet interface
214 ***************************************************************************/
215 static void pnic_remove ( struct pci_device *pci ) {
216 struct net_device *netdev = pci_get_drvdata ( pci );
217 struct pnic *pnic = netdev->priv;
219 unregister_netdev ( netdev );
220 pnic_command ( pnic, PNIC_CMD_RESET, NULL, 0, NULL, 0, NULL );
221 netdev_put ( netdev );
224 /**************************************************************************
225 PROBE - Look for an adapter, this routine's visible to the outside
226 ***************************************************************************/
227 static int pnic_probe ( struct pci_device *pci,
228 const struct pci_device_id *id __unused ) {
229 struct net_device *netdev;
231 uint16_t api_version;
235 /* Allocate net device */
236 netdev = alloc_etherdev ( sizeof ( *pnic ) );
240 pci_set_drvdata ( pci, netdev );
241 netdev->dev = &pci->dev;
242 pnic->ioaddr = pci->ioaddr;
244 /* Fix up PCI device */
245 adjust_pci_device ( pci );
247 /* API version check */
248 status = pnic_command_quiet ( pnic, PNIC_CMD_API_VER, NULL, 0,
250 sizeof ( api_version ), NULL );
251 if ( status != PNIC_STATUS_OK ) {
252 printf ( "PNIC failed installation check, code %#hx\n",
257 pnic_api_check ( api_version );
259 /* Get MAC address */
260 status = pnic_command ( pnic, PNIC_CMD_READ_MAC, NULL, 0,
261 netdev->ll_addr, ETH_ALEN, NULL );
263 /* Point to NIC specific routines */
264 netdev->open = pnic_open;
265 netdev->close = pnic_close;
266 netdev->poll = pnic_poll;
267 netdev->transmit = pnic_transmit;
269 /* Register network device */
270 if ( ( rc = register_netdev ( netdev ) ) != 0 )
276 /* Free net device */
277 netdev_put ( netdev );
281 static struct pci_device_id pnic_nics[] = {
282 /* genrules.pl doesn't let us use macros for PCI IDs...*/
283 PCI_ROM ( 0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor" ),
286 struct pci_driver pnic_driver __pci_driver = {
288 .id_count = ( sizeof ( pnic_nics ) / sizeof ( pnic_nics[0] ) ),
290 .remove = pnic_remove,