Remove obsolete files (INSTALL, RELNOTES)
[gpxe.git] / src / drivers / net / pnic.c
blobb431ec52a7e3c41e8860ae00b48123f9e2568e53
1 /**************************************************************************
2 Etherboot - BOOTP/TFTP Bootstrap Program
3 Bochs Pseudo NIC driver for Etherboot
4 ***************************************************************************/
6 /*
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.
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <io.h>
18 #include <errno.h>
19 #include <gpxe/pci.h>
20 #include <gpxe/if_ether.h>
21 #include <gpxe/ethernet.h>
22 #include <gpxe/iobuf.h>
23 #include <gpxe/netdevice.h>
25 #include "pnic_api.h"
27 struct pnic {
28 unsigned short ioaddr;
31 /*
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
40 * of data).
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 ) {
47 uint16_t status;
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 );
56 /* Write command */
57 outw ( command, pnic->ioaddr + PNIC_REG_CMD );
58 /* Retrieve status */
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 );
68 } else {
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 );
81 return status;
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,
89 input, input_length,
90 output, output_max_length,
91 output_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 );
95 return 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;
118 uint16_t length;
119 uint16_t qlen;
121 /* Fetch all available packets */
122 while ( 1 ) {
123 if ( pnic_command ( pnic, PNIC_CMD_RECV_QLEN, NULL, 0,
124 &qlen, sizeof ( qlen ), NULL )
125 != PNIC_STATUS_OK )
126 return;
127 if ( qlen == 0 )
128 return;
129 iobuf = alloc_iob ( ETH_FRAME_LEN );
130 if ( ! iobuf ) {
131 DBG ( "could not allocate buffer\n" );
132 netdev_rx_err ( netdev, NULL, -ENOMEM );
133 return;
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 );
139 return;
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;
152 /* Pad the packet */
153 iob_pad ( iobuf, ETH_ZLEN );
155 /* Send packet */
156 pnic_command ( pnic, PNIC_CMD_XMIT, iobuf->data, iob_len ( iobuf ),
157 NULL, 0, NULL );
159 netdev_tx_complete ( netdev, iobuf );
160 return 0;
163 /**************************************************************************
164 OPEN - Open network device
165 ***************************************************************************/
166 static int pnic_open ( struct net_device *netdev __unused ) {
167 /* Nothing to do */
168 return 0;
171 /**************************************************************************
172 CLOSE - Close network device
173 ***************************************************************************/
174 static void pnic_close ( struct net_device *netdev __unused ) {
175 /* Nothing to do */
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 ),
186 NULL, 0, NULL );
189 /**************************************************************************
190 OPERATIONS TABLE
191 ***************************************************************************/
192 static struct net_device_operations pnic_operations = {
193 .open = pnic_open,
194 .close = pnic_close,
195 .transmit = pnic_transmit,
196 .poll = pnic_poll,
197 .irq = pnic_irq,
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;
219 struct pnic *pnic;
220 uint16_t api_version;
221 uint16_t status;
222 int rc;
224 /* Allocate net device */
225 netdev = alloc_etherdev ( sizeof ( *pnic ) );
226 if ( ! netdev )
227 return -ENOMEM;
228 netdev_init ( netdev, &pnic_operations );
229 pnic = netdev->priv;
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,
239 &api_version,
240 sizeof ( api_version ), NULL );
241 if ( status != PNIC_STATUS_OK ) {
242 printf ( "PNIC failed installation check, code %#hx\n",
243 status );
244 rc = -EIO;
245 goto err;
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 )
255 goto err;
257 return 0;
259 err:
260 /* Free net device */
261 netdev_nullify ( netdev );
262 netdev_put ( netdev );
263 return rc;
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 = {
272 .ids = pnic_nics,
273 .id_count = ( sizeof ( pnic_nics ) / sizeof ( pnic_nics[0] ) ),
274 .probe = pnic_probe,
275 .remove = pnic_remove,