| 1 |
/* |
| 2 |
* Cisco router simlation platform. |
| 3 |
* Copyright (C) 2005,2006 Christophe Fillot. All rights reserved. |
| 4 |
* |
| 5 |
* DEC21140 FastEthernet chip emulation. |
| 6 |
* |
| 7 |
* It allows to emulate a C7200-IO-FE card with 1 port and PA-FE-TX cards. |
| 8 |
* |
| 9 |
* Many many thanks to mtve (aka "Mtv Europe") for his great work on |
| 10 |
* this stuff. |
| 11 |
* |
| 12 |
* Manuals: |
| 13 |
* |
| 14 |
* DECchip 21140 PCI fast Ethernet LAN controller Hardware reference manual |
| 15 |
* http://ftp.nluug.nl/NetBSD/misc/dec-docs/ec-qc0cb-te.ps.gz |
| 16 |
* |
| 17 |
* National DP83840 PHY |
| 18 |
* http://www.rezrov.net/docs/DP83840A.pdf |
| 19 |
* |
| 20 |
* Remark: only Big-endian mode is supported. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <time.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <assert.h> |
| 31 |
|
| 32 |
#include "crc.h" |
| 33 |
#include "utils.h" |
| 34 |
#include "cpu.h" |
| 35 |
#include "vm.h" |
| 36 |
#include "dynamips.h" |
| 37 |
#include "memory.h" |
| 38 |
#include "device.h" |
| 39 |
#include "net.h" |
| 40 |
#include "net_io.h" |
| 41 |
#include "ptask.h" |
| 42 |
#include "dev_dec21140.h" |
| 43 |
|
| 44 |
/* Debugging flags */ |
| 45 |
#define DEBUG_MII_REGS 0 |
| 46 |
#define DEBUG_CSR_REGS 0 |
| 47 |
#define DEBUG_PCI_REGS 0 |
| 48 |
#define DEBUG_TRANSMIT 0 |
| 49 |
#define DEBUG_RECEIVE 0 |
| 50 |
|
| 51 |
/* DEC21140 PCI vendor/product codes */ |
| 52 |
#define DEC21140_PCI_VENDOR_ID 0x1011 |
| 53 |
#define DEC21140_PCI_PRODUCT_ID 0x0009 |
| 54 |
|
| 55 |
/* DEC21140 PCI registers */ |
| 56 |
#define DEC21140_PCI_CFID_REG_OFFSET 0x00 |
| 57 |
#define DEC21140_PCI_CFCS_REG_OFFSET 0x04 |
| 58 |
#define DEC21140_PCI_CFRV_REG_OFFSET 0x08 |
| 59 |
#define DEC21140_PCI_CFLT_REG_OFFSET 0x0C |
| 60 |
#define DEC21140_PCI_CBIO_REG_OFFSET 0x10 |
| 61 |
#define DEC21140_PCI_CBMA_REG_OFFSET 0x14 |
| 62 |
#define DEC21140_PCI_CFIT_REG_OFFSET 0x3C |
| 63 |
#define DEC21140_PCI_CFDA_REG_OFFSET 0x40 |
| 64 |
|
| 65 |
/* Number of CSR registers */ |
| 66 |
#define DEC21140_CSR_NR 16 |
| 67 |
|
| 68 |
/* CSR5: Status Register */ |
| 69 |
#define DEC21140_CSR5_TI 0x00000001 /* TX Interrupt */ |
| 70 |
#define DEC21140_CSR5_TPS 0x00000002 /* TX Process Stopped */ |
| 71 |
#define DEC21140_CSR5_TU 0x00000004 /* TX Buffer Unavailable */ |
| 72 |
#define DEC21140_CSR5_TJT 0x00000008 /* TX Jabber Timeout */ |
| 73 |
#define DEC21140_CSR5_UNF 0x00000020 /* TX Underflow */ |
| 74 |
#define DEC21140_CSR5_RI 0x00000040 /* RX Interrupt */ |
| 75 |
#define DEC21140_CSR5_RU 0x00000080 /* RX Buffer Unavailable */ |
| 76 |
#define DEC21140_CSR5_RPS 0x00000100 /* RX Process Stopped */ |
| 77 |
#define DEC21140_CSR5_RWT 0x00000200 /* RX Watchdog Timeout */ |
| 78 |
#define DEC21140_CSR5_GTE 0x00000800 /* Gen Purpose Timer Expired */ |
| 79 |
#define DEC21140_CSR5_FBE 0x00002000 /* Fatal Bus Error */ |
| 80 |
#define DEC21140_CSR5_AIS 0x00008000 /* Abnormal Interrupt Summary */ |
| 81 |
#define DEC21140_CSR5_NIS 0x00010000 /* Normal Interrupt Summary */ |
| 82 |
|
| 83 |
#define DEC21140_NIS_BITS \ |
| 84 |
(DEC21140_CSR5_TI|DEC21140_CSR5_RI|DEC21140_CSR5_TU) |
| 85 |
|
| 86 |
#define DEC21140_AIS_BITS \ |
| 87 |
(DEC21140_CSR5_TPS|DEC21140_CSR5_TJT|DEC21140_CSR5_UNF| \ |
| 88 |
DEC21140_CSR5_RU|DEC21140_CSR5_RPS|DEC21140_CSR5_RWT| \ |
| 89 |
DEC21140_CSR5_GTE|DEC21140_CSR5_FBE) |
| 90 |
|
| 91 |
#define DEC21140_CSR5_RS_SHIFT 17 |
| 92 |
#define DEC21140_CSR5_TS_SHIFT 20 |
| 93 |
|
| 94 |
/* CSR6: Operating Mode Register */ |
| 95 |
#define DEC21140_CSR6_START_RX 0x00000002 |
| 96 |
#define DEC21140_CSR6_START_TX 0x00002000 |
| 97 |
#define DEC21140_CSR6_PROMISC 0x00000040 |
| 98 |
|
| 99 |
/* CSR9: Serial EEPROM and MII */ |
| 100 |
#define DEC21140_CSR9_RX_BIT 0x00080000 |
| 101 |
#define DEC21140_CSR9_MII_READ 0x00040000 |
| 102 |
#define DEC21140_CSR9_TX_BIT 0x00020000 |
| 103 |
#define DEC21140_CSR9_MDC_CLOCK 0x00010000 |
| 104 |
#define DEC21140_CSR9_READ 0x00004000 |
| 105 |
#define DEC21140_CSR9_WRITE 0x00002000 |
| 106 |
|
| 107 |
/* Maximum packet size */ |
| 108 |
#define DEC21140_MAX_PKT_SIZE 2048 |
| 109 |
|
| 110 |
/* Send up to 32 packets in a TX ring scan pass */ |
| 111 |
#define DEC21140_TXRING_PASS_COUNT 32 |
| 112 |
|
| 113 |
/* Setup frame size */ |
| 114 |
#define DEC21140_SETUP_FRAME_SIZE 192 |
| 115 |
|
| 116 |
/* RX descriptors */ |
| 117 |
#define DEC21140_RXDESC_OWN 0x80000000 /* Ownership */ |
| 118 |
#define DEC21140_RXDESC_LS 0x00000100 /* Last Segment */ |
| 119 |
#define DEC21140_RXDESC_FS 0x00000200 /* First Segment */ |
| 120 |
#define DEC21140_RXDESC_MF 0x00000400 /* Multicast Frame */ |
| 121 |
#define DEC21140_RXDESC_DE 0x00004000 /* Descriptor Error */ |
| 122 |
#define DEC21140_RXDESC_RCH 0x01000000 /* Sec. Addr. Chained */ |
| 123 |
#define DEC21140_RXDESC_RER 0x02000000 /* Receive End of Ring */ |
| 124 |
#define DEC21140_RXDESC_FL_SHIFT 16 |
| 125 |
#define DEC21140_RXDESC_LEN_MASK 0x7ff |
| 126 |
|
| 127 |
/* TX descriptors */ |
| 128 |
#define DEC21140_TXDESC_OWN 0x80000000 /* Ownership */ |
| 129 |
#define DEC21140_TXDESC_TCH 0x01000000 /* Sec. Addr. Chained */ |
| 130 |
#define DEC21140_TXDESC_TER 0x02000000 /* Transmit End of Ring */ |
| 131 |
#define DEC21140_TXDESC_SET 0x08000000 /* Setup frame */ |
| 132 |
#define DEC21140_TXDESC_FS 0x20000000 /* First Segment */ |
| 133 |
#define DEC21140_TXDESC_LS 0x40000000 /* Last Segment */ |
| 134 |
#define DEC21140_TXDESC_IC 0x80000000 /* IRQ on completion */ |
| 135 |
|
| 136 |
#define DEC21140_TXDESC_LEN_MASK 0x7ff |
| 137 |
|
| 138 |
/* RX Descriptor */ |
| 139 |
struct rx_desc { |
| 140 |
m_uint32_t rdes[4]; |
| 141 |
}; |
| 142 |
|
| 143 |
/* TX Descriptor */ |
| 144 |
struct tx_desc { |
| 145 |
m_uint32_t tdes[4]; |
| 146 |
}; |
| 147 |
|
| 148 |
/* DEC21140 Data */ |
| 149 |
struct dec21140_data { |
| 150 |
char *name; |
| 151 |
|
| 152 |
/* Physical addresses of current RX and TX descriptors */ |
| 153 |
m_uint32_t rx_current; |
| 154 |
m_uint32_t tx_current; |
| 155 |
|
| 156 |
/* CSR registers */ |
| 157 |
m_uint32_t csr[DEC21140_CSR_NR]; |
| 158 |
|
| 159 |
/* MII registers */ |
| 160 |
m_uint32_t mii_state; |
| 161 |
m_uint32_t mii_phy; |
| 162 |
m_uint32_t mii_reg; |
| 163 |
m_uint32_t mii_data; |
| 164 |
m_uint32_t mii_outbits; |
| 165 |
m_uint16_t mii_regs[32][32]; |
| 166 |
|
| 167 |
/* Ethernet unicast addresses */ |
| 168 |
n_eth_addr_t mac_addr[16]; |
| 169 |
u_int mac_addr_count; |
| 170 |
|
| 171 |
/* Device information */ |
| 172 |
struct vdevice *dev; |
| 173 |
|
| 174 |
/* PCI device information */ |
| 175 |
struct pci_device *pci_dev; |
| 176 |
|
| 177 |
/* Virtual machine */ |
| 178 |
vm_instance_t *vm; |
| 179 |
|
| 180 |
/* NetIO descriptor */ |
| 181 |
netio_desc_t *nio; |
| 182 |
|
| 183 |
/* TX ring scanner task id */ |
| 184 |
ptask_id_t tx_tid; |
| 185 |
}; |
| 186 |
|
| 187 |
/* Log a dec21140 message */ |
| 188 |
#define DEC21140_LOG(d,msg...) vm_log((d)->vm,(d)->name,msg) |
| 189 |
|
| 190 |
/* Check if a packet must be delivered to the emulated chip */ |
| 191 |
static inline int dec21140_handle_mac_addr(struct dec21140_data *d, |
| 192 |
m_uint8_t *pkt) |
| 193 |
{ |
| 194 |
n_eth_hdr_t *hdr = (n_eth_hdr_t *)pkt; |
| 195 |
int i; |
| 196 |
|
| 197 |
/* Accept systematically frames if we are running is promiscuous mode */ |
| 198 |
if (d->csr[6] & DEC21140_CSR6_PROMISC) |
| 199 |
return(TRUE); |
| 200 |
|
| 201 |
/* Accept systematically all multicast frames */ |
| 202 |
if (eth_addr_is_mcast(&hdr->daddr)) |
| 203 |
return(TRUE); |
| 204 |
|
| 205 |
/* Accept frames directly for us, discard others */ |
| 206 |
for(i=0;i<d->mac_addr_count;i++) |
| 207 |
if (!memcmp(&d->mac_addr[i],&hdr->daddr,N_ETH_ALEN)) |
| 208 |
return(TRUE); |
| 209 |
|
| 210 |
return(FALSE); |
| 211 |
} |
| 212 |
|
| 213 |
/* Update MAC addresses */ |
| 214 |
static void dec21140_update_mac_addr(struct dec21140_data *d, |
| 215 |
u_char *setup_frame) |
| 216 |
{ |
| 217 |
n_eth_addr_t addr; |
| 218 |
int i,nb_addr,addr_size; |
| 219 |
|
| 220 |
d->mac_addr_count = 0; |
| 221 |
|
| 222 |
addr_size = N_ETH_ALEN * 2; |
| 223 |
nb_addr = DEC21140_SETUP_FRAME_SIZE / addr_size; |
| 224 |
|
| 225 |
for(i=0;i<nb_addr;i++) { |
| 226 |
addr.eth_addr_byte[0] = setup_frame[(i * addr_size) + 0]; |
| 227 |
addr.eth_addr_byte[1] = setup_frame[(i * addr_size) + 1]; |
| 228 |
addr.eth_addr_byte[2] = setup_frame[(i * addr_size) + 4]; |
| 229 |
addr.eth_addr_byte[3] = setup_frame[(i * addr_size) + 5]; |
| 230 |
addr.eth_addr_byte[4] = setup_frame[(i * addr_size) + 8]; |
| 231 |
addr.eth_addr_byte[5] = setup_frame[(i * addr_size) + 9]; |
| 232 |
|
| 233 |
if (!eth_addr_is_mcast(&addr)) { |
| 234 |
memcpy(&d->mac_addr[d->mac_addr_count],&addr,N_ETH_ALEN); |
| 235 |
DEC21140_LOG(d,"unicast MAC address: " |
| 236 |
"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", |
| 237 |
addr.eth_addr_byte[0],addr.eth_addr_byte[1], |
| 238 |
addr.eth_addr_byte[2],addr.eth_addr_byte[3], |
| 239 |
addr.eth_addr_byte[4],addr.eth_addr_byte[5]); |
| 240 |
d->mac_addr_count++; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/* Get a PCI register name */ |
| 246 |
_maybe_used static char *pci_cfgreg_name(int reg) |
| 247 |
{ |
| 248 |
static char *name[] = { |
| 249 |
"FID", "FCS", "FRV", "FLT", "BIO", "BMA", "?", "?", |
| 250 |
"?", "?", "?", "?", "?", "?", "?", "FIT", "FDA" |
| 251 |
}; |
| 252 |
|
| 253 |
return((reg>=0) && (reg<=DEC21140_CSR_NR*4) && ((reg&3)==0) ? |
| 254 |
name[reg>>2] : "?"); |
| 255 |
} |
| 256 |
|
| 257 |
/* |
| 258 |
* read from register of DP83840A PHY |
| 259 |
*/ |
| 260 |
static m_uint16_t mii_reg_read(struct dec21140_data *d) |
| 261 |
{ |
| 262 |
#if DEBUG_MII_REGS |
| 263 |
DEC21140_LOG(d,"MII PHY read %d reg %d\n",d->mii_phy,d->mii_reg); |
| 264 |
#endif |
| 265 |
|
| 266 |
/* |
| 267 |
* if it's BASIC MODE STATUS REGISTER (BMSR) at address 0x1 |
| 268 |
* then tell them that "Link Status" is up and no troubles. |
| 269 |
*/ |
| 270 |
if (d->mii_reg == 1) { |
| 271 |
if (d->nio != NULL) |
| 272 |
return(0x04); |
| 273 |
else |
| 274 |
return(0x00); |
| 275 |
} |
| 276 |
|
| 277 |
return(d->mii_regs[d->mii_phy][d->mii_reg]); |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* write to register of DP83840A PHY |
| 282 |
*/ |
| 283 |
static void mii_reg_write(struct dec21140_data *d) |
| 284 |
{ |
| 285 |
#if DEBUG_MII_REGS |
| 286 |
DEC21140_LOG(d,"MII PHY write %d reg %d value %04x\n", |
| 287 |
d->mii_phy,d->mii_reg,d->mii_data); |
| 288 |
#endif |
| 289 |
assert(d->mii_phy < 32); |
| 290 |
assert(d->mii_reg < 32); |
| 291 |
d->mii_regs[d->mii_phy][d->mii_reg] = d->mii_data; |
| 292 |
} |
| 293 |
|
| 294 |
/* |
| 295 |
* process new bit sent by IOS to PHY. |
| 296 |
*/ |
| 297 |
static void mii_newbit(struct dec21140_data *d,int newbit) |
| 298 |
{ |
| 299 |
#if DEBUG_MII_REGS |
| 300 |
DEC21140_LOG(d,"MII state was %d\n",d->mii_state); |
| 301 |
#endif |
| 302 |
|
| 303 |
switch (d->mii_state) { |
| 304 |
case 0: /* init */ |
| 305 |
d->mii_state = newbit ? 0 : 1; |
| 306 |
d->mii_phy = 0; |
| 307 |
d->mii_reg = 0; |
| 308 |
d->mii_data = 0; |
| 309 |
break; |
| 310 |
|
| 311 |
case 1: /* already got 0 */ |
| 312 |
d->mii_state = newbit ? 2 : 0; |
| 313 |
break; |
| 314 |
|
| 315 |
case 2: /* already got attention */ |
| 316 |
d->mii_state = newbit ? 3 : 4; |
| 317 |
break; |
| 318 |
|
| 319 |
case 3: /* probably it's read */ |
| 320 |
d->mii_state = newbit ? 0 : 10; |
| 321 |
break; |
| 322 |
|
| 323 |
case 4: /* probably it's write */ |
| 324 |
d->mii_state = newbit ? 20 : 0; |
| 325 |
break; |
| 326 |
|
| 327 |
case 10: case 11: case 12: case 13: case 14: |
| 328 |
case 20: case 21: case 22: case 23: case 24: |
| 329 |
/* read or write state, read 5 bits of phy */ |
| 330 |
d->mii_phy <<= 1; |
| 331 |
d->mii_phy |= newbit; |
| 332 |
d->mii_state++; |
| 333 |
break; |
| 334 |
|
| 335 |
case 15: case 16: case 17: case 18: case 19: |
| 336 |
case 25: case 26: case 27: case 28: case 29: |
| 337 |
/* read or write state, read 5 bits of reg */ |
| 338 |
d->mii_reg <<= 1; |
| 339 |
d->mii_reg |= newbit; |
| 340 |
d->mii_state++; |
| 341 |
|
| 342 |
if (d->mii_state == 20) { |
| 343 |
/* read state, got everything */ |
| 344 |
d->mii_outbits = mii_reg_read (d) << 15; /* first bit will |
| 345 |
* be thrown away! |
| 346 |
*/ |
| 347 |
d->mii_state = 0; |
| 348 |
} |
| 349 |
|
| 350 |
break; |
| 351 |
|
| 352 |
case 30: /* write state, read first waiting bit */ |
| 353 |
d->mii_state = newbit ? 31 : 0; |
| 354 |
break; |
| 355 |
|
| 356 |
case 31: /* write state, read second waiting bit */ |
| 357 |
d->mii_state = newbit ? 0 : 32; |
| 358 |
break; |
| 359 |
|
| 360 |
case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39: |
| 361 |
case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: |
| 362 |
/* write state, read 16 bits of data */ |
| 363 |
d->mii_data <<= 1; |
| 364 |
d->mii_data |= newbit; |
| 365 |
d->mii_state++; |
| 366 |
|
| 367 |
if (d->mii_state == 48) { |
| 368 |
/* write state, got everything */ |
| 369 |
mii_reg_write (d); |
| 370 |
d->mii_state = 0; |
| 371 |
} |
| 372 |
|
| 373 |
break; |
| 374 |
default: |
| 375 |
DEC21140_LOG(d,"MII impossible state\n"); |
| 376 |
} |
| 377 |
|
| 378 |
#if DEBUG_MII_REGS |
| 379 |
DEC21140_LOG(d,"MII state now %d\n",d->mii_state); |
| 380 |
#endif |
| 381 |
} |
| 382 |
|
| 383 |
/* Update the interrupt status */ |
| 384 |
static inline void dev_dec21140_update_irq_status(struct dec21140_data *d) |
| 385 |
{ |
| 386 |
int trigger = FALSE; |
| 387 |
m_uint32_t csr5; |
| 388 |
|
| 389 |
/* Work on a temporary copy of csr5 */ |
| 390 |
csr5 = d->csr[5]; |
| 391 |
|
| 392 |
/* Compute Interrupt Summary */ |
| 393 |
csr5 &= ~(DEC21140_CSR5_AIS|DEC21140_CSR5_NIS); |
| 394 |
|
| 395 |
if (csr5 & DEC21140_NIS_BITS) { |
| 396 |
csr5 |= DEC21140_CSR5_NIS; |
| 397 |
trigger = TRUE; |
| 398 |
} |
| 399 |
|
| 400 |
if (csr5 & DEC21140_AIS_BITS) { |
| 401 |
csr5 |= DEC21140_CSR5_AIS; |
| 402 |
trigger = TRUE; |
| 403 |
} |
| 404 |
|
| 405 |
d->csr[5] = csr5; |
| 406 |
|
| 407 |
if (trigger) |
| 408 |
pci_dev_trigger_irq(d->vm,d->pci_dev); |
| 409 |
else { |
| 410 |
pci_dev_clear_irq(d->vm,d->pci_dev); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
/* |
| 415 |
* dev_dec21140_access() |
| 416 |
*/ |
| 417 |
void *dev_dec21140_access(cpu_gen_t *cpu,struct vdevice *dev, |
| 418 |
m_uint32_t offset,u_int op_size,u_int op_type, |
| 419 |
m_uint64_t *data) |
| 420 |
{ |
| 421 |
struct dec21140_data *d = dev->priv_data; |
| 422 |
u_int reg; |
| 423 |
|
| 424 |
/* which CSR register ? */ |
| 425 |
reg = offset / 8; |
| 426 |
|
| 427 |
if ((reg >= DEC21140_CSR_NR) || (offset % 8) != 0) { |
| 428 |
cpu_log(cpu,d->name,"invalid access to offset 0x%x\n",offset); |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
|
| 432 |
if (op_type == MTS_READ) { |
| 433 |
#if DEBUG_CSR_REGS |
| 434 |
cpu_log(cpu,d->name,"read CSR%u value 0x%x\n",reg,d->csr[reg]); |
| 435 |
#endif |
| 436 |
switch(reg) { |
| 437 |
case 5: |
| 438 |
/* Dynamically construct CSR5 */ |
| 439 |
*data = 0; |
| 440 |
|
| 441 |
if (d->csr[6] & DEC21140_CSR6_START_RX) |
| 442 |
*data |= 0x03 << DEC21140_CSR5_RS_SHIFT; |
| 443 |
|
| 444 |
if (d->csr[6] & DEC21140_CSR6_START_TX) |
| 445 |
*data |= 0x03 << DEC21140_CSR5_TS_SHIFT; |
| 446 |
|
| 447 |
*data |= d->csr[5]; |
| 448 |
break; |
| 449 |
|
| 450 |
case 8: |
| 451 |
/* CSR8 is cleared when read (missed frame counter) */ |
| 452 |
d->csr[reg] = 0; |
| 453 |
*data = 0; |
| 454 |
break; |
| 455 |
|
| 456 |
default: |
| 457 |
*data = d->csr[reg]; |
| 458 |
} |
| 459 |
} else { |
| 460 |
#if DEBUG_CSR_REGS |
| 461 |
cpu_log(cpu,d->name,"write CSR%u value 0x%x\n",reg,(m_uint32_t)*data); |
| 462 |
#endif |
| 463 |
switch(reg) { |
| 464 |
case 3: |
| 465 |
d->csr[reg] = *data; |
| 466 |
d->rx_current = d->csr[reg]; |
| 467 |
break; |
| 468 |
case 4: |
| 469 |
d->csr[reg] = *data; |
| 470 |
d->tx_current = d->csr[reg]; |
| 471 |
break; |
| 472 |
case 5: |
| 473 |
d->csr[reg] &= ~(*data); |
| 474 |
dev_dec21140_update_irq_status(d); |
| 475 |
break; |
| 476 |
case 9: |
| 477 |
/* |
| 478 |
* CSR9, probably they want to mess with MII PHY |
| 479 |
* The protocol to PHY is like serial over one bit. |
| 480 |
* We will ignore clock 0 of read or write. |
| 481 |
* |
| 482 |
* This whole code is needed only to tell IOS that "Link Status" |
| 483 |
* bit in BMSR register of DP83840A PHY is set. |
| 484 |
* |
| 485 |
* Also it makes "sh contr f0/0" happy. |
| 486 |
*/ |
| 487 |
d->csr[reg] = *data; |
| 488 |
|
| 489 |
if ((*data & ~DEC21140_CSR9_TX_BIT) == (DEC21140_CSR9_MII_READ| |
| 490 |
DEC21140_CSR9_READ|DEC21140_CSR9_MDC_CLOCK)) { |
| 491 |
/* |
| 492 |
* read, pop one bit from mii_outbits |
| 493 |
*/ |
| 494 |
if (d->mii_outbits & (1<<31)) |
| 495 |
d->csr[9] |= DEC21140_CSR9_RX_BIT; |
| 496 |
else |
| 497 |
d->csr[9] &= ~DEC21140_CSR9_RX_BIT; |
| 498 |
d->mii_outbits <<= 1; |
| 499 |
} else if((*data&~DEC21140_CSR9_TX_BIT) == |
| 500 |
(DEC21140_CSR9_WRITE|DEC21140_CSR9_MDC_CLOCK)) { |
| 501 |
/* |
| 502 |
* write, we've got input, do state machine |
| 503 |
*/ |
| 504 |
mii_newbit(d,(*data&DEC21140_CSR9_TX_BIT) ? 1 : 0); |
| 505 |
} |
| 506 |
break; |
| 507 |
|
| 508 |
default: |
| 509 |
d->csr[reg] = *data; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
return NULL; |
| 514 |
} |
| 515 |
|
| 516 |
/* |
| 517 |
* Get the address of the next RX descriptor. |
| 518 |
*/ |
| 519 |
static m_uint32_t rxdesc_get_next(struct dec21140_data *d,m_uint32_t rxd_addr, |
| 520 |
struct rx_desc *rxd) |
| 521 |
{ |
| 522 |
m_uint32_t nrxd_addr; |
| 523 |
|
| 524 |
/* go to the next descriptor */ |
| 525 |
if (rxd->rdes[1] & DEC21140_RXDESC_RER) |
| 526 |
nrxd_addr = d->csr[3]; |
| 527 |
else { |
| 528 |
if (rxd->rdes[1] & DEC21140_RXDESC_RCH) |
| 529 |
nrxd_addr = rxd->rdes[3]; |
| 530 |
else |
| 531 |
nrxd_addr = rxd_addr + sizeof(struct rx_desc); |
| 532 |
} |
| 533 |
|
| 534 |
return(nrxd_addr); |
| 535 |
} |
| 536 |
|
| 537 |
/* Read a RX descriptor */ |
| 538 |
static void rxdesc_read(struct dec21140_data *d,m_uint32_t rxd_addr, |
| 539 |
struct rx_desc *rxd) |
| 540 |
{ |
| 541 |
/* get the next descriptor from VM physical RAM */ |
| 542 |
physmem_copy_from_vm(d->vm,rxd,rxd_addr,sizeof(struct rx_desc)); |
| 543 |
|
| 544 |
/* byte-swapping */ |
| 545 |
rxd->rdes[0] = vmtoh32(rxd->rdes[0]); |
| 546 |
rxd->rdes[1] = vmtoh32(rxd->rdes[1]); |
| 547 |
rxd->rdes[2] = vmtoh32(rxd->rdes[2]); |
| 548 |
rxd->rdes[3] = vmtoh32(rxd->rdes[3]); |
| 549 |
} |
| 550 |
|
| 551 |
/* |
| 552 |
* Try to acquire the specified RX descriptor. Returns TRUE if we have it. |
| 553 |
* It assumes that the byte-swapping is done. |
| 554 |
*/ |
| 555 |
static inline int rxdesc_acquire(m_uint32_t rdes0) |
| 556 |
{ |
| 557 |
return(rdes0 & DEC21140_RXDESC_OWN); |
| 558 |
} |
| 559 |
|
| 560 |
/* Put a packet in buffer(s) of a descriptor */ |
| 561 |
static void rxdesc_put_pkt(struct dec21140_data *d,struct rx_desc *rxd, |
| 562 |
u_char **pkt,ssize_t *pkt_len) |
| 563 |
{ |
| 564 |
ssize_t len1,len2,cp_len; |
| 565 |
|
| 566 |
/* get rbs1 and rbs2 */ |
| 567 |
len1 = rxd->rdes[1] & DEC21140_RXDESC_LEN_MASK; |
| 568 |
len2 = (rxd->rdes[1] >> 10) & DEC21140_RXDESC_LEN_MASK; |
| 569 |
|
| 570 |
/* try with buffer #1 */ |
| 571 |
if (len1 != 0) |
| 572 |
{ |
| 573 |
/* compute the data length to copy */ |
| 574 |
cp_len = m_min(len1,*pkt_len); |
| 575 |
|
| 576 |
/* copy packet data to the VM physical RAM */ |
| 577 |
physmem_copy_to_vm(d->vm,*pkt,rxd->rdes[2],cp_len); |
| 578 |
|
| 579 |
*pkt += cp_len; |
| 580 |
*pkt_len -= cp_len; |
| 581 |
} |
| 582 |
|
| 583 |
/* try with buffer #2 */ |
| 584 |
if ((len2 != 0) && !(rxd->rdes[1] & DEC21140_RXDESC_RCH)) |
| 585 |
{ |
| 586 |
/* compute the data length to copy */ |
| 587 |
cp_len = m_min(len2,*pkt_len); |
| 588 |
|
| 589 |
/* copy packet data to the VM physical RAM */ |
| 590 |
physmem_copy_to_vm(d->vm,*pkt,rxd->rdes[3],cp_len); |
| 591 |
|
| 592 |
*pkt += cp_len; |
| 593 |
*pkt_len -= cp_len; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
/* |
| 598 |
* Put a packet in the RX ring of the DEC21140. |
| 599 |
*/ |
| 600 |
static int dev_dec21140_receive_pkt(struct dec21140_data *d, |
| 601 |
u_char *pkt,ssize_t pkt_len) |
| 602 |
{ |
| 603 |
m_uint32_t rx_start,rxdn_addr,rxdn_rdes0; |
| 604 |
struct rx_desc rxd0,rxdn,*rxdc; |
| 605 |
ssize_t tot_len = pkt_len; |
| 606 |
u_char *pkt_ptr = pkt; |
| 607 |
n_eth_hdr_t *hdr; |
| 608 |
int i; |
| 609 |
|
| 610 |
/* Truncate the packet if it is too big */ |
| 611 |
pkt_len = m_min(pkt_len,DEC21140_MAX_PKT_SIZE); |
| 612 |
|
| 613 |
/* Copy the current rxring descriptor */ |
| 614 |
rxdesc_read(d,d->rx_current,&rxd0); |
| 615 |
|
| 616 |
/* We must have the first descriptor... */ |
| 617 |
if (!rxdesc_acquire(rxd0.rdes[0])) |
| 618 |
return(FALSE); |
| 619 |
|
| 620 |
/* Remember the first RX descriptor address */ |
| 621 |
rx_start = d->rx_current; |
| 622 |
|
| 623 |
for(i=0,rxdc=&rxd0;tot_len>0;i++) |
| 624 |
{ |
| 625 |
/* Put data into the descriptor buffers */ |
| 626 |
rxdesc_put_pkt(d,rxdc,&pkt_ptr,&tot_len); |
| 627 |
|
| 628 |
/* Get address of the next descriptor */ |
| 629 |
rxdn_addr = rxdesc_get_next(d,d->rx_current,rxdc); |
| 630 |
|
| 631 |
/* We have finished if the complete packet has been stored */ |
| 632 |
if (tot_len == 0) { |
| 633 |
rxdc->rdes[0] = DEC21140_RXDESC_LS; |
| 634 |
rxdc->rdes[0] |= (pkt_len + 4) << DEC21140_RXDESC_FL_SHIFT; |
| 635 |
|
| 636 |
/* if this is a multicast frame, set the appropriate bit */ |
| 637 |
hdr = (n_eth_hdr_t *)pkt; |
| 638 |
if (eth_addr_is_mcast(&hdr->daddr)) |
| 639 |
rxdc->rdes[0] |= DEC21140_RXDESC_MF; |
| 640 |
|
| 641 |
if (i != 0) |
| 642 |
physmem_copy_u32_to_vm(d->vm,d->rx_current,rxdc->rdes[0]); |
| 643 |
|
| 644 |
d->rx_current = rxdn_addr; |
| 645 |
break; |
| 646 |
} |
| 647 |
|
| 648 |
/* Get status of the next descriptor to see if we can acquire it */ |
| 649 |
rxdn_rdes0 = physmem_copy_u32_from_vm(d->vm,rxdn_addr); |
| 650 |
|
| 651 |
if (!rxdesc_acquire(rxdn_rdes0)) |
| 652 |
rxdc->rdes[0] = DEC21140_RXDESC_LS | DEC21140_RXDESC_DE; |
| 653 |
else |
| 654 |
rxdc->rdes[0] = 0; /* ok, no special flag */ |
| 655 |
|
| 656 |
/* Update the new status (only if we are not on the first desc) */ |
| 657 |
if (i != 0) |
| 658 |
physmem_copy_u32_to_vm(d->vm,d->rx_current,rxdc->rdes[0]); |
| 659 |
|
| 660 |
/* Update the RX pointer */ |
| 661 |
d->rx_current = rxdn_addr; |
| 662 |
|
| 663 |
if (rxdc->rdes[0] != 0) |
| 664 |
break; |
| 665 |
|
| 666 |
/* Read the next descriptor from VM physical RAM */ |
| 667 |
rxdesc_read(d,rxdn_addr,&rxdn); |
| 668 |
rxdc = &rxdn; |
| 669 |
} |
| 670 |
|
| 671 |
/* Update the first RX descriptor */ |
| 672 |
rxd0.rdes[0] |= DEC21140_RXDESC_FS; |
| 673 |
physmem_copy_u32_to_vm(d->vm,rx_start,rxd0.rdes[0]); |
| 674 |
|
| 675 |
/* Indicate that we have a frame ready */ |
| 676 |
d->csr[5] |= DEC21140_CSR5_RI; |
| 677 |
dev_dec21140_update_irq_status(d); |
| 678 |
return(TRUE); |
| 679 |
} |
| 680 |
|
| 681 |
/* Handle the DEC21140 RX ring */ |
| 682 |
static int dev_dec21140_handle_rxring(netio_desc_t *nio, |
| 683 |
u_char *pkt,ssize_t pkt_len, |
| 684 |
struct dec21140_data *d) |
| 685 |
{ |
| 686 |
/* |
| 687 |
* Don't start receive if the RX ring address has not been set |
| 688 |
* and if the SR bit in CSR6 is not set yet. |
| 689 |
*/ |
| 690 |
if ((d->csr[3] == 0) || !(d->csr[6] & DEC21140_CSR6_START_RX)) |
| 691 |
return(FALSE); |
| 692 |
|
| 693 |
#if DEBUG_RECEIVE |
| 694 |
DEC21140_LOG(d,"receiving a packet of %d bytes\n",pkt_len); |
| 695 |
mem_dump(log_file,pkt,pkt_len); |
| 696 |
#endif |
| 697 |
|
| 698 |
/* |
| 699 |
* Receive only multicast/broadcast trafic + unicast traffic |
| 700 |
* for this virtual machine. |
| 701 |
*/ |
| 702 |
if (dec21140_handle_mac_addr(d,pkt)) |
| 703 |
return(dev_dec21140_receive_pkt(d,pkt,pkt_len)); |
| 704 |
|
| 705 |
return(FALSE); |
| 706 |
} |
| 707 |
|
| 708 |
/* Read a TX descriptor */ |
| 709 |
static void txdesc_read(struct dec21140_data *d,m_uint32_t txd_addr, |
| 710 |
struct tx_desc *txd) |
| 711 |
{ |
| 712 |
/* get the descriptor from VM physical RAM */ |
| 713 |
physmem_copy_from_vm(d->vm,txd,txd_addr,sizeof(struct tx_desc)); |
| 714 |
|
| 715 |
/* byte-swapping */ |
| 716 |
txd->tdes[0] = vmtoh32(txd->tdes[0]); |
| 717 |
txd->tdes[1] = vmtoh32(txd->tdes[1]); |
| 718 |
txd->tdes[2] = vmtoh32(txd->tdes[2]); |
| 719 |
txd->tdes[3] = vmtoh32(txd->tdes[3]); |
| 720 |
} |
| 721 |
|
| 722 |
/* Set the address of the next TX descriptor */ |
| 723 |
static void txdesc_set_next(struct dec21140_data *d,struct tx_desc *txd) |
| 724 |
{ |
| 725 |
if (txd->tdes[1] & DEC21140_TXDESC_TER) |
| 726 |
d->tx_current = d->csr[4]; |
| 727 |
else { |
| 728 |
if (txd->tdes[1] & DEC21140_TXDESC_TCH) |
| 729 |
d->tx_current = txd->tdes[3]; |
| 730 |
else |
| 731 |
d->tx_current += sizeof(struct tx_desc); |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
/* Handle the TX ring (single packet) */ |
| 736 |
static int dev_dec21140_handle_txring_single(struct dec21140_data *d) |
| 737 |
{ |
| 738 |
u_char pkt[DEC21140_MAX_PKT_SIZE],*pkt_ptr; |
| 739 |
u_char setup_frame[DEC21140_SETUP_FRAME_SIZE]; |
| 740 |
m_uint32_t tx_start,len1,len2,clen,tot_len; |
| 741 |
struct tx_desc txd0,ctxd,*ptxd; |
| 742 |
int done = FALSE; |
| 743 |
|
| 744 |
/* |
| 745 |
* Don't start transmit if the txring address has not been set |
| 746 |
* and if the ST bit in CSR6 is not set yet. |
| 747 |
*/ |
| 748 |
if ((d->csr[4] == 0) || (!(d->csr[6] & DEC21140_CSR6_START_TX))) |
| 749 |
return(FALSE); |
| 750 |
|
| 751 |
/* Check if the NIO can transmit */ |
| 752 |
if (!netio_can_transmit(d->nio)) |
| 753 |
return(FALSE); |
| 754 |
|
| 755 |
/* Copy the current txring descriptor */ |
| 756 |
tx_start = d->tx_current; |
| 757 |
ptxd = &txd0; |
| 758 |
txdesc_read(d,tx_start,ptxd); |
| 759 |
|
| 760 |
/* If we don't own the first descriptor, we cannot transmit */ |
| 761 |
if (!(txd0.tdes[0] & DEC21140_TXDESC_OWN)) |
| 762 |
return(FALSE); |
| 763 |
|
| 764 |
/* |
| 765 |
* Ignore setup frames (clear the own bit and skip). |
| 766 |
* We extract unicast MAC addresses to allow only appropriate traffic |
| 767 |
* to pass. |
| 768 |
*/ |
| 769 |
if (!(txd0.tdes[1] & (DEC21140_TXDESC_FS|DEC21140_TXDESC_LS))) |
| 770 |
{ |
| 771 |
len1 = ptxd->tdes[1] & DEC21140_TXDESC_LEN_MASK; |
| 772 |
len2 = (ptxd->tdes[1] >> 11) & DEC21140_TXDESC_LEN_MASK; |
| 773 |
|
| 774 |
if (txd0.tdes[1] & DEC21140_TXDESC_SET) { |
| 775 |
physmem_copy_from_vm(d->vm,setup_frame,ptxd->tdes[2], |
| 776 |
sizeof(setup_frame)); |
| 777 |
dec21140_update_mac_addr(d,setup_frame); |
| 778 |
} |
| 779 |
|
| 780 |
txdesc_set_next(d,ptxd); |
| 781 |
goto clear_txd0_own_bit; |
| 782 |
} |
| 783 |
|
| 784 |
#if DEBUG_TRANSMIT |
| 785 |
DEC21140_LOG(d,"dec21140_handle_txring: 1st desc: " |
| 786 |
"tdes[0]=0x%x, tdes[1]=0x%x, tdes[2]=0x%x, tdes[3]=0x%x\n", |
| 787 |
ptxd->tdes[0],ptxd->tdes[1],ptxd->tdes[2],ptxd->tdes[3]); |
| 788 |
#endif |
| 789 |
|
| 790 |
/* Empty packet for now */ |
| 791 |
pkt_ptr = pkt; |
| 792 |
tot_len = 0; |
| 793 |
|
| 794 |
do { |
| 795 |
#if DEBUG_TRANSMIT |
| 796 |
DEC21140_LOG(d,"dec21140_handle_txring: loop: " |
| 797 |
"tdes[0]=0x%x, tdes[1]=0x%x, tdes[2]=0x%x, tdes[3]=0x%x\n", |
| 798 |
ptxd->tdes[0],ptxd->tdes[1],ptxd->tdes[2],ptxd->tdes[3]); |
| 799 |
#endif |
| 800 |
|
| 801 |
if (!(ptxd->tdes[0] & DEC21140_TXDESC_OWN)) { |
| 802 |
DEC21140_LOG(d,"dec21140_handle_txring: descriptor not owned!\n"); |
| 803 |
return(FALSE); |
| 804 |
} |
| 805 |
|
| 806 |
len1 = ptxd->tdes[1] & DEC21140_TXDESC_LEN_MASK; |
| 807 |
len2 = (ptxd->tdes[1] >> 11) & DEC21140_TXDESC_LEN_MASK; |
| 808 |
clen = len1 + len2; |
| 809 |
|
| 810 |
/* Be sure that we have either len1 or len2 not null */ |
| 811 |
if (clen != 0) |
| 812 |
{ |
| 813 |
if (len1 != 0) |
| 814 |
physmem_copy_from_vm(d->vm,pkt_ptr,ptxd->tdes[2],len1); |
| 815 |
|
| 816 |
if ((len2 != 0) && !(ptxd->tdes[1] & DEC21140_TXDESC_TCH)) |
| 817 |
physmem_copy_from_vm(d->vm,pkt_ptr+len1,ptxd->tdes[3],len2); |
| 818 |
} |
| 819 |
|
| 820 |
pkt_ptr += clen; |
| 821 |
tot_len += clen; |
| 822 |
|
| 823 |
/* Clear the OWN bit if this is not the first descriptor */ |
| 824 |
if (!(ptxd->tdes[1] & DEC21140_TXDESC_FS)) |
| 825 |
physmem_copy_u32_to_vm(d->vm,d->tx_current,0); |
| 826 |
|
| 827 |
/* Go to the next descriptor */ |
| 828 |
txdesc_set_next(d,ptxd); |
| 829 |
|
| 830 |
/* |
| 831 |
* Copy the next txring descriptor (ignore setup frames that |
| 832 |
* have both FS and LS bit cleared). |
| 833 |
*/ |
| 834 |
if (!(ptxd->tdes[1] & (DEC21140_TXDESC_LS|DEC21140_TXDESC_SET))) { |
| 835 |
txdesc_read(d,d->tx_current,&ctxd); |
| 836 |
ptxd = &ctxd; |
| 837 |
} else |
| 838 |
done = TRUE; |
| 839 |
}while(!done); |
| 840 |
|
| 841 |
if (tot_len != 0) { |
| 842 |
#if DEBUG_TRANSMIT |
| 843 |
DEC21140_LOG(d,"sending packet of %u bytes\n",tot_len); |
| 844 |
mem_dump(log_file,pkt,tot_len); |
| 845 |
#endif |
| 846 |
/* rewrite ISL header if required */ |
| 847 |
cisco_isl_rewrite(pkt,tot_len); |
| 848 |
|
| 849 |
/* send it on wire */ |
| 850 |
netio_send(d->nio,pkt,tot_len); |
| 851 |
} |
| 852 |
|
| 853 |
clear_txd0_own_bit: |
| 854 |
/* Clear the OWN flag of the first descriptor */ |
| 855 |
physmem_copy_u32_to_vm(d->vm,tx_start,0); |
| 856 |
|
| 857 |
/* Interrupt on completion ? */ |
| 858 |
if (txd0.tdes[1] & DEC21140_TXDESC_IC) { |
| 859 |
d->csr[5] |= DEC21140_CSR5_TI; |
| 860 |
dev_dec21140_update_irq_status(d); |
| 861 |
} |
| 862 |
|
| 863 |
return(TRUE); |
| 864 |
} |
| 865 |
|
| 866 |
/* Handle the TX ring */ |
| 867 |
static int dev_dec21140_handle_txring(struct dec21140_data *d) |
| 868 |
{ |
| 869 |
int i; |
| 870 |
|
| 871 |
for(i=0;i<DEC21140_TXRING_PASS_COUNT;i++) |
| 872 |
if (!dev_dec21140_handle_txring_single(d)) |
| 873 |
break; |
| 874 |
|
| 875 |
netio_clear_bw_stat(d->nio); |
| 876 |
return(TRUE); |
| 877 |
} |
| 878 |
|
| 879 |
/* |
| 880 |
* pci_dec21140_read() |
| 881 |
* |
| 882 |
* Read a PCI register. |
| 883 |
*/ |
| 884 |
static m_uint32_t pci_dec21140_read(cpu_gen_t *cpu,struct pci_device *dev, |
| 885 |
int reg) |
| 886 |
{ |
| 887 |
struct dec21140_data *d = dev->priv_data; |
| 888 |
|
| 889 |
#if DEBUG_PCI_REGS |
| 890 |
DEC21140_LOG(d,"read C%s(%u)\n",pci_cfgreg_name(reg),reg); |
| 891 |
#endif |
| 892 |
|
| 893 |
switch (reg) { |
| 894 |
case DEC21140_PCI_CFID_REG_OFFSET: |
| 895 |
return(0x00091011); |
| 896 |
case DEC21140_PCI_CFRV_REG_OFFSET: |
| 897 |
return(0x02000011); |
| 898 |
case DEC21140_PCI_CBMA_REG_OFFSET: |
| 899 |
return(d->dev->phys_addr); |
| 900 |
default: |
| 901 |
return(0); |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
/* |
| 906 |
* pci_dec21140_write() |
| 907 |
* |
| 908 |
* Write a PCI register. |
| 909 |
*/ |
| 910 |
static void pci_dec21140_write(cpu_gen_t *cpu,struct pci_device *dev, |
| 911 |
int reg,m_uint32_t value) |
| 912 |
{ |
| 913 |
struct dec21140_data *d = dev->priv_data; |
| 914 |
|
| 915 |
#if DEBUG_PCI_REGS |
| 916 |
DEC21140_LOG(d,"write C%s(%u) value 0x%x\n",pci_cfgreg_name(reg),reg,value); |
| 917 |
#endif |
| 918 |
|
| 919 |
switch(reg) { |
| 920 |
case DEC21140_PCI_CBMA_REG_OFFSET: |
| 921 |
vm_map_device(cpu->vm,d->dev,(m_uint64_t)value); |
| 922 |
DEC21140_LOG(d,"registers are mapped at 0x%x\n",value); |
| 923 |
break; |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
/* |
| 928 |
* dev_dec21140_init() |
| 929 |
* |
| 930 |
* Generic DEC21140 initialization code. |
| 931 |
*/ |
| 932 |
struct dec21140_data *dev_dec21140_init(vm_instance_t *vm,char *name, |
| 933 |
struct pci_bus *pci_bus,int pci_device, |
| 934 |
int irq) |
| 935 |
{ |
| 936 |
struct dec21140_data *d; |
| 937 |
struct pci_device *pci_dev; |
| 938 |
struct vdevice *dev; |
| 939 |
|
| 940 |
/* Allocate the private data structure for DEC21140 */ |
| 941 |
if (!(d = malloc(sizeof(*d)))) { |
| 942 |
fprintf(stderr,"%s (DEC21140): out of memory\n",name); |
| 943 |
return NULL; |
| 944 |
} |
| 945 |
|
| 946 |
memset(d,0,sizeof(*d)); |
| 947 |
|
| 948 |
/* Add as PCI device */ |
| 949 |
pci_dev = pci_dev_add(pci_bus,name, |
| 950 |
DEC21140_PCI_VENDOR_ID,DEC21140_PCI_PRODUCT_ID, |
| 951 |
pci_device,0,irq, |
| 952 |
d,NULL,pci_dec21140_read,pci_dec21140_write); |
| 953 |
|
| 954 |
if (!pci_dev) { |
| 955 |
fprintf(stderr,"%s (DEC21140): unable to create PCI device.\n",name); |
| 956 |
goto err_pci_dev; |
| 957 |
} |
| 958 |
|
| 959 |
/* Create the device itself */ |
| 960 |
if (!(dev = dev_create(name))) { |
| 961 |
fprintf(stderr,"%s (DEC21140): unable to create device.\n",name); |
| 962 |
goto err_dev; |
| 963 |
} |
| 964 |
|
| 965 |
d->name = name; |
| 966 |
d->vm = vm; |
| 967 |
d->pci_dev = pci_dev; |
| 968 |
d->dev = dev; |
| 969 |
|
| 970 |
/* Basic register setup */ |
| 971 |
d->csr[0] = 0xfff80000; |
| 972 |
d->csr[5] = 0xfc000000; |
| 973 |
d->csr[8] = 0xfffe0000; |
| 974 |
|
| 975 |
dev->phys_addr = 0; |
| 976 |
dev->phys_len = 0x20000; |
| 977 |
dev->handler = dev_dec21140_access; |
| 978 |
dev->priv_data = d; |
| 979 |
return(d); |
| 980 |
|
| 981 |
err_dev: |
| 982 |
pci_dev_remove(pci_dev); |
| 983 |
err_pci_dev: |
| 984 |
free(d); |
| 985 |
return NULL; |
| 986 |
} |
| 987 |
|
| 988 |
/* Remove a DEC21140 device */ |
| 989 |
void dev_dec21140_remove(struct dec21140_data *d) |
| 990 |
{ |
| 991 |
if (d != NULL) { |
| 992 |
pci_dev_remove(d->pci_dev); |
| 993 |
vm_unbind_device(d->vm,d->dev); |
| 994 |
cpu_group_rebuild_mts(d->vm->cpu_group); |
| 995 |
free(d->dev); |
| 996 |
free(d); |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
/* Bind a NIO to DEC21140 device */ |
| 1001 |
int dev_dec21140_set_nio(struct dec21140_data *d,netio_desc_t *nio) |
| 1002 |
{ |
| 1003 |
/* check that a NIO is not already bound */ |
| 1004 |
if (d->nio != NULL) |
| 1005 |
return(-1); |
| 1006 |
|
| 1007 |
d->nio = nio; |
| 1008 |
d->tx_tid = ptask_add((ptask_callback)dev_dec21140_handle_txring,d,NULL); |
| 1009 |
netio_rxl_add(nio,(netio_rx_handler_t)dev_dec21140_handle_rxring,d,NULL); |
| 1010 |
return(0); |
| 1011 |
} |
| 1012 |
|
| 1013 |
/* Unbind a NIO from a DEC21140 device */ |
| 1014 |
void dev_dec21140_unset_nio(struct dec21140_data *d) |
| 1015 |
{ |
| 1016 |
if (d->nio != NULL) { |
| 1017 |
ptask_remove(d->tx_tid); |
| 1018 |
netio_rxl_remove(d->nio); |
| 1019 |
d->nio = NULL; |
| 1020 |
} |
| 1021 |
} |