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
) {
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 DBG ( "could not allocate buffer\n" );
132 netdev_rx_err ( netdev
, NULL
, -ENOMEM
);
135 if ( pnic_command ( pnic
, PNIC_CMD_RECV
, NULL
, 0,
136 iobuf
->data
, ETH_FRAME_LEN
, &length
)
137 != PNIC_STATUS_OK
) {
138 netdev_rx_err ( netdev
, iobuf
, -EIO
);
141 iob_put ( iobuf
, length
);
142 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 OPEN - Open network device
165 ***************************************************************************/
166 static int pnic_open ( struct net_device
*netdev __unused
) {
171 /**************************************************************************
172 CLOSE - Close network device
173 ***************************************************************************/
174 static void pnic_close ( struct net_device
*netdev __unused
) {
178 /**************************************************************************
179 IRQ - Enable/disable interrupts
180 ***************************************************************************/
181 static void pnic_irq ( struct net_device
*netdev
, int enable
) {
182 struct pnic
*pnic
= netdev
->priv
;
183 uint8_t mask
= ( enable
? 1 : 0 );
185 pnic_command ( pnic
, PNIC_CMD_MASK_IRQ
, &mask
, sizeof ( mask
),
189 /**************************************************************************
191 ***************************************************************************/
192 static struct net_device_operations pnic_operations
= {
195 .transmit
= pnic_transmit
,
200 /**************************************************************************
201 DISABLE - Turn off ethernet interface
202 ***************************************************************************/
203 static void pnic_remove ( struct pci_device
*pci
) {
204 struct net_device
*netdev
= pci_get_drvdata ( pci
);
205 struct pnic
*pnic
= netdev
->priv
;
207 unregister_netdev ( netdev
);
208 pnic_command ( pnic
, PNIC_CMD_RESET
, NULL
, 0, NULL
, 0, NULL
);
209 netdev_nullify ( netdev
);
210 netdev_put ( netdev
);
213 /**************************************************************************
214 PROBE - Look for an adapter, this routine's visible to the outside
215 ***************************************************************************/
216 static int pnic_probe ( struct pci_device
*pci
,
217 const struct pci_device_id
*id __unused
) {
218 struct net_device
*netdev
;
220 uint16_t api_version
;
224 /* Allocate net device */
225 netdev
= alloc_etherdev ( sizeof ( *pnic
) );
228 netdev_init ( netdev
, &pnic_operations
);
230 pci_set_drvdata ( pci
, netdev
);
231 netdev
->dev
= &pci
->dev
;
232 pnic
->ioaddr
= pci
->ioaddr
;
234 /* Fix up PCI device */
235 adjust_pci_device ( pci
);
237 /* API version check */
238 status
= pnic_command_quiet ( pnic
, PNIC_CMD_API_VER
, NULL
, 0,
240 sizeof ( api_version
), NULL
);
241 if ( status
!= PNIC_STATUS_OK
) {
242 printf ( "PNIC failed installation check, code %#hx\n",
247 pnic_api_check ( api_version
);
249 /* Get MAC address */
250 status
= pnic_command ( pnic
, PNIC_CMD_READ_MAC
, NULL
, 0,
251 netdev
->ll_addr
, ETH_ALEN
, NULL
);
253 /* Register network device */
254 if ( ( rc
= register_netdev ( netdev
) ) != 0 )
260 /* Free net device */
261 netdev_nullify ( netdev
);
262 netdev_put ( netdev
);
266 static struct pci_device_id pnic_nics
[] = {
267 /* genrules.pl doesn't let us use macros for PCI IDs...*/
268 PCI_ROM ( 0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor" ),
271 struct pci_driver pnic_driver __pci_driver
= {
273 .id_count
= ( sizeof ( pnic_nics
) / sizeof ( pnic_nics
[0] ) ),
275 .remove
= pnic_remove
,