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.
15 /* to get some global routines like printf */
16 #include "etherboot.h"
17 /* to get the interface to the body of the program */
19 /* to get the PCI support functions, if this is a PCI NIC */
25 /* Function prototypes */
26 static int pnic_api_check ( uint16_t api_version );
28 /* NIC specific static variables go here */
29 uint8_t tx_buffer[ETH_FRAME_LEN] __shared;
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 nic *nic, 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, nic->ioaddr + PNIC_REG_LEN );
54 /* Write input data */
55 for ( i = 0; i < input_length; i++ ) {
56 outb( ((char*)input)[i], nic->ioaddr + PNIC_REG_DATA );
60 outw ( command, nic->ioaddr + PNIC_REG_CMD );
62 status = inw ( nic->ioaddr + PNIC_REG_STAT );
63 /* Retrieve output length */
64 _output_length = inw ( nic->ioaddr + PNIC_REG_LEN );
65 if ( output_length == NULL ) {
66 if ( _output_length != output_max_length ) {
67 printf ( "pnic_command %#hx: wrong data length "
68 "returned (expected %d, got %d)\n", command,
69 output_max_length, _output_length );
72 *output_length = _output_length;
74 if ( output != NULL ) {
75 if ( _output_length > output_max_length ) {
76 printf ( "pnic_command %#hx: output buffer too small "
77 "(have %d, need %d)\n", command,
78 output_max_length, _output_length );
79 _output_length = output_max_length;
81 /* Retrieve output data */
82 for ( i = 0; i < _output_length; i++ ) {
84 inb ( nic->ioaddr + PNIC_REG_DATA );
90 static uint16_t pnic_command ( struct nic *nic, uint16_t command,
91 void *input, uint16_t input_length,
92 void *output, uint16_t output_max_length,
93 uint16_t *output_length ) {
94 uint16_t status = pnic_command_quiet ( nic, command,
96 output, output_max_length,
98 if ( status == PNIC_STATUS_OK ) return status;
99 printf ( "PNIC command %#hx (len %#hx) failed with status %#hx\n",
100 command, input_length, status );
104 /* Check API version matches that of NIC */
105 static int pnic_api_check ( uint16_t api_version ) {
106 if ( api_version != PNIC_API_VERSION ) {
107 printf ( "Warning: API version mismatch! "
108 "(NIC's is %d.%d, ours is %d.%d)\n",
109 api_version >> 8, api_version & 0xff,
110 PNIC_API_VERSION >> 8, PNIC_API_VERSION & 0xff );
112 if ( api_version < PNIC_API_VERSION ) {
113 printf ( "*** You may need to update your copy of Bochs ***\n" );
115 return ( api_version == PNIC_API_VERSION );
118 /**************************************************************************
119 CONNECT - connect adapter to the network
120 ***************************************************************************/
121 static int pnic_connect ( struct nic *nic __unused ) {
126 /**************************************************************************
127 POLL - Wait for a frame
128 ***************************************************************************/
129 static int pnic_poll ( struct nic *nic, int retrieve ) {
133 /* Check receive queue length to see if there's anything to
134 * get. Necessary since once we've called PNIC_CMD_RECV we
135 * have to read out the packet, otherwise it's lost forever.
137 if ( pnic_command ( nic, PNIC_CMD_RECV_QLEN, NULL, 0,
138 &qlen, sizeof(qlen), NULL )
139 != PNIC_STATUS_OK ) return ( 0 );
140 if ( qlen == 0 ) return ( 0 );
142 /* There is a packet ready. Return 1 if we're only checking. */
143 if ( ! retrieve ) return ( 1 );
145 /* Retrieve the packet */
146 if ( pnic_command ( nic, PNIC_CMD_RECV, NULL, 0,
147 nic->packet, ETH_FRAME_LEN, &length )
148 != PNIC_STATUS_OK ) return ( 0 );
149 nic->packetlen = length;
153 /**************************************************************************
154 TRANSMIT - Transmit a frame
155 ***************************************************************************/
156 static void pnic_transmit ( struct nic *nic, const char *dest,
157 unsigned int type, unsigned int size,
159 unsigned int nstype = htons ( type );
161 if ( ( ETH_HLEN + size ) >= ETH_FRAME_LEN ) {
162 printf ( "pnic_transmit: packet too large\n" );
166 /* Assemble packet */
167 memcpy ( tx_buffer, dest, ETH_ALEN );
168 memcpy ( tx_buffer + ETH_ALEN, nic->node_addr, ETH_ALEN );
169 memcpy ( tx_buffer + 2 * ETH_ALEN, &nstype, 2 );
170 memcpy ( tx_buffer + ETH_HLEN, data, size );
172 pnic_command ( nic, PNIC_CMD_XMIT, tx_buffer, ETH_HLEN + size,
176 /**************************************************************************
177 DISABLE - Turn off ethernet interface
178 ***************************************************************************/
179 static void pnic_disable ( struct nic *nic, struct pci_device *pci __unused ) {
181 pnic_command ( nic, PNIC_CMD_RESET, NULL, 0, NULL, 0, NULL );
184 /**************************************************************************
185 IRQ - Handle card interrupt status
186 ***************************************************************************/
187 static void pnic_irq ( struct nic *nic, irq_action_t action ) {
193 enabled = ( action == ENABLE ? 1 : 0 );
194 pnic_command ( nic, PNIC_CMD_MASK_IRQ,
195 &enabled, sizeof(enabled), NULL, 0, NULL );
198 pnic_command ( nic, PNIC_CMD_FORCE_IRQ,
199 NULL, 0, NULL, 0, NULL );
204 /**************************************************************************
206 ***************************************************************************/
207 static struct nic_operations pnic_operations = {
208 .connect = pnic_connect,
210 .transmit = pnic_transmit,
214 /**************************************************************************
215 PROBE - Look for an adapter, this routine's visible to the outside
216 ***************************************************************************/
217 static int pnic_probe ( struct nic *nic, struct pci_device *pci ) {
218 uint16_t api_version;
221 /* Retrieve relevant information about PCI device */
222 pci_fill_nic ( nic, pci );
224 /* API version check */
225 status = pnic_command_quiet( nic, PNIC_CMD_API_VER, NULL, 0,
227 sizeof(api_version), NULL );
228 if ( status != PNIC_STATUS_OK ) {
229 printf ( "PNIC failed installation check, code %#hx\n",
233 pnic_api_check(api_version);
235 /* Get MAC address */
236 status = pnic_command ( nic, PNIC_CMD_READ_MAC, NULL, 0,
237 nic->node_addr, ETH_ALEN, NULL );
239 /* point to NIC specific routines */
240 nic->nic_op = &pnic_operations;
244 static struct pci_id pnic_nics[] = {
245 /* genrules.pl doesn't let us use macros for PCI IDs...*/
246 PCI_ROM ( 0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor" ),
249 PCI_DRIVER ( pnic_driver, pnic_nics, PCI_NO_CLASS );
251 DRIVER ( "PNIC", nic_driver, pci_driver, pnic_driver,
252 pnic_probe, pnic_disable );