1 /************************************************************************** 2 Etherboot - BOOTP/TFTP Bootstrap Program 3 Bochs Pseudo NIC driver for Etherboot 4 ***************************************************************************/ 5 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. 11 * 12 * See pnic_api.h for an explanation of the Bochs Pseudo NIC. 13 */ 14 15 /* to get some global routines like printf */ 16 #include "etherboot.h" 17 /* to get the interface to the body of the program */ 18 #include "nic.h" 19 /* to get the PCI support functions, if this is a PCI NIC */ 20 #include "pci.h" 21 22 /* PNIC API */ 23 #include "pnic_api.h" 24 25 /* Private data structure */ 26 typedef struct { 27 uint16_t api_version; 28 } pnic_priv_data_t; 29 30 /* Function prototypes */ 31 static int pnic_api_check ( uint16_t api_version ); 32 33 /* NIC specific static variables go here */ 34 static uint8_t tx_buffer[ETH_FRAME_LEN]; 35 36 /* 37 * Utility functions: issue a PNIC command, retrieve result. Use 38 * pnic_command_quiet if you don't want failure codes to be 39 * automatically printed. Returns the PNIC status code. 40 * 41 * Set output_length to NULL only if you expect to receive exactly 42 * output_max_length bytes, otherwise it'll complain that you didn't 43 * get enough data (on the assumption that if you not interested in 44 * discovering the output length then you're expecting a fixed amount 45 * of data). 46 */ 47 48 static uint16_t pnic_command_quiet ( struct nic *nic, uint16_t command, 49 void *input, uint16_t input_length, 50 void *output, uint16_t output_max_length, 51 uint16_t *output_length ) { 52 int i; 53 uint16_t status; 54 uint16_t _output_length; 55 56 if ( input != NULL ) { 57 /* Write input length */ 58 outw ( input_length, nic->ioaddr + PNIC_REG_LEN ); 59 /* Write input data */ 60 for ( i = 0; i < input_length; i++ ) { 61 outb( ((char*)input)[i], nic->ioaddr + PNIC_REG_DATA ); 62 } 63 } 64 /* Write command */ 65 outw ( command, nic->ioaddr + PNIC_REG_CMD ); 66 /* Retrieve status */ 67 status = inw ( nic->ioaddr + PNIC_REG_STAT ); 68 /* Retrieve output length */ 69 _output_length = inw ( nic->ioaddr + PNIC_REG_LEN ); 70 if ( output_length == NULL ) { 71 if ( _output_length != output_max_length ) { 72 printf ( "pnic_command %#hx: wrong data length " 73 "returned (expected %d, got %d)\n", command, 74 output_max_length, _output_length ); 75 } 76 } else { 77 *output_length = _output_length; 78 } 79 if ( output != NULL ) { 80 if ( _output_length > output_max_length ) { 81 printf ( "pnic_command %#hx: output buffer too small " 82 "(have %d, need %d)\n", command, 83 output_max_length, _output_length ); 84 _output_length = output_max_length; 85 } 86 /* Retrieve output data */ 87 for ( i = 0; i < _output_length; i++ ) { 88 ((char*)output)[i] = 89 inb ( nic->ioaddr + PNIC_REG_DATA ); 90 } 91 } 92 return status; 93 } 94 95 static uint16_t pnic_command ( struct nic *nic, uint16_t command, 96 void *input, uint16_t input_length, 97 void *output, uint16_t output_max_length, 98 uint16_t *output_length ) { 99 pnic_priv_data_t *priv = (pnic_priv_data_t*)nic->priv_data; 100 uint16_t status = pnic_command_quiet ( nic, command, 101 input, input_length, 102 output, output_max_length, 103 output_length ); 104 if ( status == PNIC_STATUS_OK ) return status; 105 printf ( "PNIC command %#hx (len %#hx) failed with status %#hx\n", 106 command, input_length, status ); 107 if ( priv->api_version ) pnic_api_check(priv->api_version); 108 return status; 109 } 110 111 /* Check API version matches that of NIC */ 112 static int pnic_api_check ( uint16_t api_version ) { 113 if ( api_version != PNIC_API_VERSION ) { 114 printf ( "Warning: API version mismatch! " 115 "(NIC's is %d.%d, ours is %d.%d)\n", 116 api_version >> 8, api_version & 0xff, 117 PNIC_API_VERSION >> 8, PNIC_API_VERSION & 0xff ); 118 } 119 if ( api_version < PNIC_API_VERSION ) { 120 printf ( "*** You may need to update your copy of Bochs ***\n" ); 121 } 122 return ( api_version == PNIC_API_VERSION ); 123 } 124 125 /************************************************************************** 126 POLL - Wait for a frame 127 ***************************************************************************/ 128 static int pnic_poll(struct nic *nic, int retrieve) 129 { 130 uint16_t length; 131 uint16_t qlen; 132 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. 136 */ 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 ); 141 142 /* There is a packet ready. Return 1 if we're only checking. */ 143 if ( ! retrieve ) return ( 1 ); 144 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; 150 return ( 1 ); 151 } 152 153 /************************************************************************** 154 TRANSMIT - Transmit a frame 155 ***************************************************************************/ 156 static void pnic_transmit( 157 struct nic *nic, 158 const char *dest, /* Destination */ 159 unsigned int type, /* Type */ 160 unsigned int size, /* size */ 161 const char *data) /* Packet */ 162 { 163 unsigned int nstype = htons ( type ); 164 165 if ( ( ETH_HLEN + size ) >= ETH_FRAME_LEN ) { 166 printf ( "pnic_transmit: packet too large\n" ); 167 return; 168 } 169 170 /* Assemble packet */ 171 memcpy ( tx_buffer, dest, ETH_ALEN ); 172 memcpy ( tx_buffer + ETH_ALEN, nic->node_addr, ETH_ALEN ); 173 memcpy ( tx_buffer + 2 * ETH_ALEN, &nstype, 2 ); 174 memcpy ( tx_buffer + ETH_HLEN, data, size ); 175 176 pnic_command ( nic, PNIC_CMD_XMIT, tx_buffer, ETH_HLEN + size, 177 NULL, 0, NULL ); 178 } 179 180 /************************************************************************** 181 DISABLE - Turn off ethernet interface 182 ***************************************************************************/ 183 static void pnic_disable(struct dev *dev) 184 { 185 struct nic *nic = (struct nic *)dev; 186 pnic_command ( nic, PNIC_CMD_RESET, NULL, 0, NULL, 0, NULL ); 187 } 188 189 /************************************************************************** 190 IRQ - Handle card interrupt status 191 ***************************************************************************/ 192 static void pnic_irq ( struct nic *nic, irq_action_t action ) 193 { 194 uint8_t enabled; 195 196 switch ( action ) { 197 case DISABLE : 198 case ENABLE : 199 enabled = ( action == ENABLE ? 1 : 0 ); 200 pnic_command ( nic, PNIC_CMD_MASK_IRQ, 201 &enabled, sizeof(enabled), NULL, 0, NULL ); 202 break; 203 case FORCE : 204 pnic_command ( nic, PNIC_CMD_FORCE_IRQ, 205 NULL, 0, NULL, 0, NULL ); 206 break; 207 } 208 } 209 210 /************************************************************************** 211 PROBE - Look for an adapter, this routine's visible to the outside 212 ***************************************************************************/ 213 214 static int pnic_probe(struct dev *dev, struct pci_device *pci) 215 { 216 struct nic *nic = (struct nic *)dev; 217 static pnic_priv_data_t priv; 218 uint16_t status; 219 220 printf(" - "); 221 222 /* Clear private data structure and chain it in */ 223 memset ( &priv, 0, sizeof(priv) ); 224 nic->priv_data = &priv; 225 226 /* Mask the bit that says "this is an io addr" */ 227 nic->ioaddr = pci->ioaddr & ~3; 228 nic->irqno = pci->irq; 229 /* Not sure what this does, but the rtl8139 driver does it */ 230 adjust_pci_device(pci); 231 232 status = pnic_command_quiet( nic, PNIC_CMD_API_VER, NULL, 0, 233 &priv.api_version, 234 sizeof(priv.api_version), NULL ); 235 if ( status != PNIC_STATUS_OK ) { 236 printf ( "PNIC failed installation check, code %#hx\n", 237 status ); 238 return 0; 239 } 240 pnic_api_check(priv.api_version); 241 status = pnic_command ( nic, PNIC_CMD_READ_MAC, NULL, 0, 242 nic->node_addr, ETH_ALEN, NULL ); 243 printf ( "Detected Bochs Pseudo NIC MAC %! (API v%d.%d) at %#hx\n", 244 nic->node_addr, priv.api_version>>8, priv.api_version&0xff, 245 nic->ioaddr ); 246 247 /* point to NIC specific routines */ 248 dev->disable = pnic_disable; 249 nic->poll = pnic_poll; 250 nic->transmit = pnic_transmit; 251 nic->irq = pnic_irq; 252 return 1; 253 } 254 255 static struct pci_id pnic_nics[] = { 256 /* genrules.pl doesn't let us use macros for PCI IDs...*/ 257 PCI_ROM(0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor"), 258 }; 259 260 struct pci_driver pnic_driver = { 261 .type = NIC_DRIVER, 262 .name = "PNIC", 263 .probe = pnic_probe, 264 .ids = pnic_nics, 265 .id_count = sizeof(pnic_nics)/sizeof(pnic_nics[0]), 266 .class = 0, 267 }; 268