pci: introduce pcibus_t to represent pci bus address/size instead of uint32_t
[qemu/ar7.git] / hw / eepro100.c
blob55e7717a81123e2bc0b9718f229cb0caee13fbb8
1 /*
2 * QEMU i8255x (PRO100) emulation
4 * Copyright (c) 2006-2007 Stefan Weil
6 * Portions of the code are copies from grub / etherboot eepro100.c
7 * and linux e100.c.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * Tested features (i82559):
23 * PXE boot (i386) no valid link
24 * Linux networking (i386) ok
26 * Untested:
27 * non-i386 platforms
28 * Windows networking
30 * References:
32 * Intel 8255x 10/100 Mbps Ethernet Controller Family
33 * Open Source Software Developer Manual
36 #if defined(TARGET_I386)
37 # warning "PXE boot still not working!"
38 #endif
40 #include <stddef.h> /* offsetof */
41 #include <stdbool.h>
42 #include "hw.h"
43 #include "pci.h"
44 #include "net.h"
45 #include "eeprom93xx.h"
47 /* Common declarations for all PCI devices. */
49 #define PCI_CONFIG_8(offset, value) \
50 (pci_conf[offset] = (value))
51 #define PCI_CONFIG_16(offset, value) \
52 (*(uint16_t *)&pci_conf[offset] = cpu_to_le16(value))
53 #define PCI_CONFIG_32(offset, value) \
54 (*(uint32_t *)&pci_conf[offset] = cpu_to_le32(value))
56 #define KiB 1024
58 /* Debug EEPRO100 card. */
59 //~ #define DEBUG_EEPRO100
61 #ifdef DEBUG_EEPRO100
62 #define logout(fmt, ...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ## __VA_ARGS__)
63 #else
64 #define logout(fmt, ...) ((void)0)
65 #endif
67 /* Set flags to 0 to disable debug output. */
68 #define INT 1 /* interrupt related actions */
69 #define MDI 1 /* mdi related actions */
70 #define OTHER 1
71 #define RXTX 1
72 #define EEPROM 1 /* eeprom related actions */
74 #define TRACE(flag, command) ((flag) ? (command) : (void)0)
76 #define missing(text) fprintf(stderr, "eepro100: feature is missing in this emulation: " text "\n")
78 #define MAX_ETH_FRAME_SIZE 1514
80 /* This driver supports several different devices which are declared here. */
81 #define i82550 0x82550
82 #define i82551 0x82551
83 #define i82557A 0x82557a
84 #define i82557B 0x82557b
85 #define i82557C 0x82557c
86 #define i82558A 0x82558a
87 #define i82558B 0x82558b
88 #define i82559A 0x82559a
89 #define i82559B 0x82559b
90 #define i82559C 0x82559c
91 #define i82559ER 0x82559e
92 #define i82562 0x82562
94 /* Use 64 word EEPROM. TODO: could be a runtime option. */
95 #define EEPROM_SIZE 64
97 #define PCI_MEM_SIZE (4 * KiB)
98 #define PCI_IO_SIZE 64
99 #define PCI_FLASH_SIZE (128 * KiB)
101 #define BIT(n) (1 << (n))
102 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
104 /* The SCB accepts the following controls for the Tx and Rx units: */
105 #define CU_NOP 0x0000 /* No operation. */
106 #define CU_START 0x0010 /* CU start. */
107 #define CU_RESUME 0x0020 /* CU resume. */
108 #define CU_STATSADDR 0x0040 /* Load dump counters address. */
109 #define CU_SHOWSTATS 0x0050 /* Dump statistical counters. */
110 #define CU_CMD_BASE 0x0060 /* Load CU base address. */
111 #define CU_DUMPSTATS 0x0070 /* Dump and reset statistical counters. */
112 #define CU_SRESUME 0x00a0 /* CU static resume. */
114 #define RU_NOP 0x0000
115 #define RX_START 0x0001
116 #define RX_RESUME 0x0002
117 #define RX_ABORT 0x0004
118 #define RX_ADDR_LOAD 0x0006
119 #define RX_RESUMENR 0x0007
120 #define INT_MASK 0x0100
121 #define DRVR_INT 0x0200 /* Driver generated interrupt. */
123 /* Offsets to the various registers.
124 All accesses need not be longword aligned. */
125 enum speedo_offsets {
126 SCBStatus = 0,
127 SCBAck = 1,
128 SCBCmd = 2, /* Rx/Command Unit command and status. */
129 SCBIntmask = 3,
130 SCBPointer = 4, /* General purpose pointer. */
131 SCBPort = 8, /* Misc. commands and operands. */
132 SCBflash = 12, SCBeeprom = 14, /* EEPROM and flash memory control. */
133 SCBCtrlMDI = 16, /* MDI interface control. */
134 SCBEarlyRx = 20, /* Early receive byte count. */
135 SCBFlow = 24,
138 /* A speedo3 transmit buffer descriptor with two buffers... */
139 typedef struct {
140 uint16_t status;
141 uint16_t command;
142 uint32_t link; /* void * */
143 uint32_t tx_desc_addr; /* transmit buffer decsriptor array address. */
144 uint16_t tcb_bytes; /* transmit command block byte count (in lower 14 bits */
145 uint8_t tx_threshold; /* transmit threshold */
146 uint8_t tbd_count; /* TBD number */
147 //~ /* This constitutes two "TBD" entries: hdr and data */
148 //~ uint32_t tx_buf_addr0; /* void *, header of frame to be transmitted. */
149 //~ int32_t tx_buf_size0; /* Length of Tx hdr. */
150 //~ uint32_t tx_buf_addr1; /* void *, data to be transmitted. */
151 //~ int32_t tx_buf_size1; /* Length of Tx data. */
152 } eepro100_tx_t;
154 /* Receive frame descriptor. */
155 typedef struct {
156 int16_t status;
157 uint16_t command;
158 uint32_t link; /* struct RxFD * */
159 uint32_t rx_buf_addr; /* void * */
160 uint16_t count;
161 uint16_t size;
162 char packet[MAX_ETH_FRAME_SIZE + 4];
163 } eepro100_rx_t;
165 typedef struct {
166 uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
167 tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
168 tx_multiple_collisions, tx_total_collisions;
169 uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
170 rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
171 rx_short_frame_errors;
172 uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
173 uint16_t xmt_tco_frames, rcv_tco_frames;
174 uint32_t complete;
175 } eepro100_stats_t;
177 typedef enum {
178 cu_idle = 0,
179 cu_suspended = 1,
180 cu_active = 2,
181 cu_lpq_active = 2,
182 cu_hqp_active = 3
183 } cu_state_t;
185 typedef enum {
186 ru_idle = 0,
187 ru_suspended = 1,
188 ru_no_resources = 2,
189 ru_ready = 4
190 } ru_state_t;
192 typedef struct {
193 PCIDevice dev;
194 uint8_t mult[8]; /* multicast mask array */
195 int mmio_index;
196 VLANClientState *vc;
197 NICConf conf;
198 uint8_t scb_stat; /* SCB stat/ack byte */
199 uint8_t int_stat; /* PCI interrupt status */
200 /* region must not be saved by nic_save. */
201 uint32_t region[3]; /* PCI region addresses */
202 uint16_t mdimem[32];
203 eeprom_t *eeprom;
204 uint32_t device; /* device variant */
205 uint32_t pointer;
206 /* (cu_base + cu_offset) address the next command block in the command block list. */
207 uint32_t cu_base; /* CU base address */
208 uint32_t cu_offset; /* CU address offset */
209 /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
210 uint32_t ru_base; /* RU base address */
211 uint32_t ru_offset; /* RU address offset */
212 uint32_t statsaddr; /* pointer to eepro100_stats_t */
213 eepro100_stats_t statistics; /* statistical counters */
214 #if 0
215 uint16_t status;
216 #endif
218 /* Configuration bytes. */
219 uint8_t configuration[22];
221 /* Data in mem is always in the byte order of the controller (le). */
222 uint8_t mem[PCI_MEM_SIZE];
223 /* vmstate for each particular nic */
224 VMStateDescription *vmstate;
225 } EEPRO100State;
227 /* Default values for MDI (PHY) registers */
228 static const uint16_t eepro100_mdi_default[] = {
229 /* MDI Registers 0 - 6, 7 */
230 0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
231 /* MDI Registers 8 - 15 */
232 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
233 /* MDI Registers 16 - 31 */
234 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
235 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
238 /* Readonly mask for MDI (PHY) registers */
239 static const uint16_t eepro100_mdi_mask[] = {
240 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
241 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
242 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
243 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
246 #define POLYNOMIAL 0x04c11db6
248 /* From FreeBSD */
249 /* XXX: optimize */
250 static int compute_mcast_idx(const uint8_t * ep)
252 uint32_t crc;
253 int carry, i, j;
254 uint8_t b;
256 crc = 0xffffffff;
257 for (i = 0; i < 6; i++) {
258 b = *ep++;
259 for (j = 0; j < 8; j++) {
260 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
261 crc <<= 1;
262 b >>= 1;
263 if (carry) {
264 crc = ((crc ^ POLYNOMIAL) | carry);
268 return (crc >> 26);
271 #if defined(DEBUG_EEPRO100)
272 static const char *nic_dump(const uint8_t * buf, unsigned size)
274 static char dump[3 * 16 + 1];
275 char *p = &dump[0];
276 if (size > 16) {
277 size = 16;
279 while (size-- > 0) {
280 p += sprintf(p, " %02x", *buf++);
282 return dump;
284 #endif /* DEBUG_EEPRO100 */
286 enum scb_stat_ack {
287 stat_ack_not_ours = 0x00,
288 stat_ack_sw_gen = 0x04,
289 stat_ack_rnr = 0x10,
290 stat_ack_cu_idle = 0x20,
291 stat_ack_frame_rx = 0x40,
292 stat_ack_cu_cmd_done = 0x80,
293 stat_ack_not_present = 0xFF,
294 stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
295 stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
298 static void disable_interrupt(EEPRO100State * s)
300 if (s->int_stat) {
301 TRACE(INT, logout("interrupt disabled\n"));
302 qemu_irq_lower(s->dev.irq[0]);
303 s->int_stat = 0;
307 static void enable_interrupt(EEPRO100State * s)
309 if (!s->int_stat) {
310 TRACE(INT, logout("interrupt enabled\n"));
311 qemu_irq_raise(s->dev.irq[0]);
312 s->int_stat = 1;
316 static void eepro100_acknowledge(EEPRO100State * s)
318 s->scb_stat &= ~s->mem[SCBAck];
319 s->mem[SCBAck] = s->scb_stat;
320 if (s->scb_stat == 0) {
321 disable_interrupt(s);
325 static void eepro100_interrupt(EEPRO100State * s, uint8_t stat)
327 uint8_t mask = ~s->mem[SCBIntmask];
328 s->mem[SCBAck] |= stat;
329 stat = s->scb_stat = s->mem[SCBAck];
330 stat &= (mask | 0x0f);
331 //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
332 if (stat && (mask & 0x01)) {
333 /* SCB mask and SCB Bit M do not disable interrupt. */
334 enable_interrupt(s);
335 } else if (s->int_stat) {
336 disable_interrupt(s);
340 static void eepro100_cx_interrupt(EEPRO100State * s)
342 /* CU completed action command. */
343 /* Transmit not ok (82557 only, not in emulation). */
344 eepro100_interrupt(s, 0x80);
347 static void eepro100_cna_interrupt(EEPRO100State * s)
349 /* CU left the active state. */
350 eepro100_interrupt(s, 0x20);
353 static void eepro100_fr_interrupt(EEPRO100State * s)
355 /* RU received a complete frame. */
356 eepro100_interrupt(s, 0x40);
359 #if 0
360 static void eepro100_rnr_interrupt(EEPRO100State * s)
362 /* RU is not ready. */
363 eepro100_interrupt(s, 0x10);
365 #endif
367 static void eepro100_mdi_interrupt(EEPRO100State * s)
369 /* MDI completed read or write cycle. */
370 eepro100_interrupt(s, 0x08);
373 static void eepro100_swi_interrupt(EEPRO100State * s)
375 /* Software has requested an interrupt. */
376 eepro100_interrupt(s, 0x04);
379 #if 0
380 static void eepro100_fcp_interrupt(EEPRO100State * s)
382 /* Flow control pause interrupt (82558 and later). */
383 eepro100_interrupt(s, 0x01);
385 #endif
387 static void pci_reset(EEPRO100State * s)
389 uint32_t device = s->device;
390 uint8_t *pci_conf = s->dev.config;
392 TRACE(OTHER, logout("%p\n", s));
394 /* PCI Vendor ID */
395 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
396 /* PCI Device ID depends on device and is set below. */
397 /* PCI Command */
398 PCI_CONFIG_16(PCI_COMMAND, 0x0000);
399 /* PCI Status */
400 PCI_CONFIG_16(PCI_STATUS, 0x2800);
401 /* PCI Revision ID */
402 PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
403 /* PCI Class Code */
404 PCI_CONFIG_8(0x09, 0x00);
405 pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
406 /* PCI Cache Line Size */
407 /* check cache line size!!! */
408 //~ PCI_CONFIG_8(0x0c, 0x00);
409 /* PCI Latency Timer */
410 PCI_CONFIG_8(0x0d, 0x20); // latency timer = 32 clocks
411 /* PCI Header Type */
412 /* BIST (built-in self test) */
413 #if defined(TARGET_I386)
414 // !!! workaround for buggy bios
415 //~ #define PCI_BASE_ADDRESS_MEM_PREFETCH 0
416 #endif
417 #if 0
418 /* PCI Base Address Registers */
419 /* CSR Memory Mapped Base Address */
420 PCI_CONFIG_32(PCI_BASE_ADDRESS_0,
421 PCI_BASE_ADDRESS_SPACE_MEMORY |
422 PCI_BASE_ADDRESS_MEM_PREFETCH);
423 /* CSR I/O Mapped Base Address */
424 PCI_CONFIG_32(PCI_BASE_ADDRESS_1, PCI_BASE_ADDRESS_SPACE_IO);
425 #if 0
426 /* Flash Memory Mapped Base Address */
427 PCI_CONFIG_32(PCI_BASE_ADDRESS_2,
428 0xfffe0000 | PCI_BASE_ADDRESS_SPACE_MEMORY);
429 #endif
430 #endif
431 /* Expansion ROM Base Address (depends on boot disable!!!) */
432 PCI_CONFIG_32(0x30, 0x00000000);
433 /* Capability Pointer */
434 PCI_CONFIG_8(0x34, 0xdc);
435 /* Interrupt Line */
436 /* Interrupt Pin */
437 PCI_CONFIG_8(0x3d, 1); // interrupt pin 0
438 /* Minimum Grant */
439 PCI_CONFIG_8(0x3e, 0x08);
440 /* Maximum Latency */
441 PCI_CONFIG_8(0x3f, 0x18);
442 /* Power Management Capabilities / Next Item Pointer / Capability ID */
443 PCI_CONFIG_32(0xdc, 0x7e210001);
445 switch (device) {
446 case i82551:
447 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
448 PCI_CONFIG_8(PCI_REVISION_ID, 0x0f);
449 break;
450 case i82557B:
451 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
452 PCI_CONFIG_8(PCI_REVISION_ID, 0x02);
453 break;
454 case i82557C:
455 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
456 PCI_CONFIG_8(PCI_REVISION_ID, 0x03);
457 break;
458 case i82558B:
459 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
460 PCI_CONFIG_16(PCI_STATUS, 0x2810);
461 PCI_CONFIG_8(PCI_REVISION_ID, 0x05);
462 break;
463 case i82559C:
464 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
465 PCI_CONFIG_16(PCI_STATUS, 0x2810);
466 //~ PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
467 break;
468 case i82559ER:
469 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
470 PCI_CONFIG_16(PCI_STATUS, 0x2810);
471 PCI_CONFIG_8(PCI_REVISION_ID, 0x09);
472 break;
473 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1029);
474 //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1030); /* 82559 InBusiness 10/100 */
475 default:
476 logout("Device %X is undefined!\n", device);
479 if (device == i82557C || device == i82558B || device == i82559C) {
480 logout("Get device id and revision from EEPROM!!!\n");
484 static void nic_selective_reset(EEPRO100State * s)
486 size_t i;
487 uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
488 //~ eeprom93xx_reset(s->eeprom);
489 memcpy(eeprom_contents, s->conf.macaddr.a, 6);
490 eeprom_contents[0xa] = 0x4000;
491 if (s->device == i82557B || s->device == i82557C)
492 eeprom_contents[5] = 0x0100;
493 uint16_t sum = 0;
494 for (i = 0; i < EEPROM_SIZE - 1; i++) {
495 sum += eeprom_contents[i];
497 eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
498 TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1]));
500 memset(s->mem, 0, sizeof(s->mem));
501 uint32_t val = BIT(21);
502 memcpy(&s->mem[SCBCtrlMDI], &val, sizeof(val));
504 assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
505 memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
508 static void nic_reset(void *opaque)
510 EEPRO100State *s = opaque;
511 TRACE(OTHER, logout("%p\n", s));
512 nic_selective_reset(s);
515 #if defined(DEBUG_EEPRO100)
516 static const char * const reg[PCI_IO_SIZE / 4] = {
517 "Command/Status",
518 "General Pointer",
519 "Port",
520 "EEPROM/Flash Control",
521 "MDI Control",
522 "Receive DMA Byte Count",
523 "Flow control",
524 "General Status/Control"
527 static char *regname(uint32_t addr)
529 static char buf[16];
530 if (addr < PCI_IO_SIZE) {
531 const char *r = reg[addr / 4];
532 if (r != 0) {
533 snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4);
534 } else {
535 snprintf(buf, sizeof(buf), "0x%02x", addr);
537 } else {
538 snprintf(buf, sizeof(buf), "??? 0x%08x", addr);
540 return buf;
542 #endif /* DEBUG_EEPRO100 */
544 #if 0
545 static uint16_t eepro100_read_status(EEPRO100State * s)
547 uint16_t val = s->status;
548 TRACE(OTHER, logout("val=0x%04x\n", val));
549 return val;
552 static void eepro100_write_status(EEPRO100State * s, uint16_t val)
554 TRACE(OTHER, logout("val=0x%04x\n", val));
555 s->status = val;
557 #endif
559 /*****************************************************************************
561 * Command emulation.
563 ****************************************************************************/
565 #if 0
566 static uint16_t eepro100_read_command(EEPRO100State * s)
568 uint16_t val = 0xffff;
569 //~ TRACE(OTHER, logout("val=0x%04x\n", val));
570 return val;
572 #endif
574 static bool device_supports_eTxCB(EEPRO100State * s)
576 return (s->device != i82557B && s->device != i82557C);
579 /* Commands that can be put in a command list entry. */
580 enum commands {
581 CmdNOp = 0,
582 CmdIASetup = 1,
583 CmdConfigure = 2,
584 CmdMulticastList = 3,
585 CmdTx = 4,
586 CmdTDR = 5, /* load microcode */
587 CmdDump = 6,
588 CmdDiagnose = 7,
590 /* And some extra flags: */
591 CmdSuspend = 0x4000, /* Suspend after completion. */
592 CmdIntr = 0x2000, /* Interrupt after completion. */
593 CmdTxFlex = 0x0008, /* Use "Flexible mode" for CmdTx command. */
596 static cu_state_t get_cu_state(EEPRO100State * s)
598 return ((s->mem[SCBStatus] >> 6) & 0x03);
601 static void set_cu_state(EEPRO100State * s, cu_state_t state)
603 s->mem[SCBStatus] = (s->mem[SCBStatus] & 0x3f) + (state << 6);
606 static ru_state_t get_ru_state(EEPRO100State * s)
608 return ((s->mem[SCBStatus] >> 2) & 0x0f);
611 static void set_ru_state(EEPRO100State * s, ru_state_t state)
613 s->mem[SCBStatus] = (s->mem[SCBStatus] & 0xc3) + (state << 2);
616 static void dump_statistics(EEPRO100State * s)
618 /* Dump statistical data. Most data is never changed by the emulation
619 * and always 0, so we first just copy the whole block and then those
620 * values which really matter.
621 * Number of data should check configuration!!!
623 cpu_physical_memory_write(s->statsaddr, (uint8_t *) & s->statistics, 64);
624 stl_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
625 stl_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
626 stl_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
627 stl_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
628 //~ stw_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
629 //~ stw_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
630 //~ missing("CU dump statistical counters");
633 static void action_command(EEPRO100State *s)
635 for (;;) {
636 uint32_t cb_address = s->cu_base + s->cu_offset;
637 eepro100_tx_t tx;
638 cpu_physical_memory_read(cb_address, (uint8_t *) & tx, sizeof(tx));
639 uint16_t status = le16_to_cpu(tx.status);
640 uint16_t command = le16_to_cpu(tx.command);
641 logout
642 ("val=0x%02x (cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
643 val, status, command, tx.link);
644 bool bit_el = ((command & 0x8000) != 0);
645 bool bit_s = ((command & 0x4000) != 0);
646 bool bit_i = ((command & 0x2000) != 0);
647 bool bit_nc = ((command & 0x0010) != 0);
648 bool success = true;
649 //~ bool bit_sf = ((command & 0x0008) != 0);
650 uint16_t cmd = command & 0x0007;
651 s->cu_offset = le32_to_cpu(tx.link);
652 switch (cmd) {
653 case CmdNOp:
654 /* Do nothing. */
655 break;
656 case CmdIASetup:
657 cpu_physical_memory_read(cb_address + 8, &s->conf.macaddr.a[0], 6);
658 TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6)));
659 break;
660 case CmdConfigure:
661 cpu_physical_memory_read(cb_address + 8, &s->configuration[0],
662 sizeof(s->configuration));
663 TRACE(OTHER, logout("configuration: %s\n", nic_dump(&s->configuration[0], 16)));
664 break;
665 case CmdMulticastList:
666 //~ missing("multicast list");
667 break;
668 case CmdTx:
669 (void)0;
670 uint32_t tbd_array = le32_to_cpu(tx.tx_desc_addr);
671 uint16_t tcb_bytes = (le16_to_cpu(tx.tcb_bytes) & 0x3fff);
672 TRACE(RXTX, logout
673 ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
674 tbd_array, tcb_bytes, tx.tbd_count));
676 if (bit_nc) {
677 missing("CmdTx: NC = 0");
678 success = false;
679 break;
681 //~ assert(!bit_sf);
682 if (tcb_bytes > 2600) {
683 logout("TCB byte count too large, using 2600\n");
684 tcb_bytes = 2600;
686 /* Next assertion fails for local configuration. */
687 //~ assert((tcb_bytes > 0) || (tbd_array != 0xffffffff));
688 if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
689 logout
690 ("illegal values of TBD array address and TCB byte count!\n");
692 // sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes
693 uint8_t buf[2600];
694 uint16_t size = 0;
695 uint32_t tbd_address = cb_address + 0x10;
696 assert(tcb_bytes <= sizeof(buf));
697 while (size < tcb_bytes) {
698 uint32_t tx_buffer_address = ldl_phys(tbd_address);
699 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
700 //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
701 tbd_address += 8;
702 TRACE(RXTX, logout
703 ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
704 tx_buffer_address, tx_buffer_size));
705 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
706 cpu_physical_memory_read(tx_buffer_address, &buf[size],
707 tx_buffer_size);
708 size += tx_buffer_size;
710 if (tbd_array == 0xffffffff) {
711 /* Simplified mode. Was already handled by code above. */
712 } else {
713 /* Flexible mode. */
714 uint8_t tbd_count = 0;
715 if (device_supports_eTxCB(s) && !(s->configuration[6] & BIT(4))) {
716 /* Extended Flexible TCB. */
717 for (; tbd_count < 2; tbd_count++) {
718 uint32_t tx_buffer_address = ldl_phys(tbd_address);
719 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
720 uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
721 tbd_address += 8;
722 TRACE(RXTX, logout
723 ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
724 tx_buffer_address, tx_buffer_size));
725 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
726 cpu_physical_memory_read(tx_buffer_address, &buf[size],
727 tx_buffer_size);
728 size += tx_buffer_size;
729 if (tx_buffer_el & 1) {
730 break;
734 tbd_address = tbd_array;
735 for (; tbd_count < tx.tbd_count; tbd_count++) {
736 uint32_t tx_buffer_address = ldl_phys(tbd_address);
737 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
738 uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
739 tbd_address += 8;
740 TRACE(RXTX, logout
741 ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
742 tx_buffer_address, tx_buffer_size));
743 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
744 cpu_physical_memory_read(tx_buffer_address, &buf[size],
745 tx_buffer_size);
746 size += tx_buffer_size;
747 if (tx_buffer_el & 1) {
748 break;
752 TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size)));
753 qemu_send_packet(s->vc, buf, size);
754 s->statistics.tx_good_frames++;
755 /* Transmit with bad status would raise an CX/TNO interrupt.
756 * (82557 only). Emulation never has bad status. */
757 //~ eepro100_cx_interrupt(s);
758 break;
759 case CmdTDR:
760 TRACE(OTHER, logout("load microcode\n"));
761 /* Starting with offset 8, the command contains
762 * 64 dwords microcode which we just ignore here. */
763 break;
764 default:
765 missing("undefined command");
766 success = false;
767 break;
769 /* Write new status. */
770 stw_phys(cb_address, status | 0x8000 | (success ? 0x2000 : 0));
771 if (bit_i) {
772 /* CU completed action. */
773 eepro100_cx_interrupt(s);
775 if (bit_el) {
776 /* CU becomes idle. Terminate command loop. */
777 set_cu_state(s, cu_idle);
778 eepro100_cna_interrupt(s);
779 break;
780 } else if (bit_s) {
781 /* CU becomes suspended. Terminate command loop. */
782 set_cu_state(s, cu_suspended);
783 eepro100_cna_interrupt(s);
784 break;
785 } else {
786 /* More entries in list. */
787 TRACE(OTHER, logout("CU list with at least one more entry\n"));
790 TRACE(OTHER, logout("CU list empty\n"));
791 /* List is empty. Now CU is idle or suspended. */
794 static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
796 switch (val) {
797 case CU_NOP:
798 /* No operation. */
799 break;
800 case CU_START:
801 if (get_cu_state(s) != cu_idle) {
802 /* Intel documentation says that CU must be idle for the CU
803 * start command. Intel driver for Linux also starts the CU
804 * from suspended state. */
805 logout("CU state is %u, should be %u\n", get_cu_state(s), cu_idle);
806 //~ assert(!"wrong CU state");
808 set_cu_state(s, cu_active);
809 s->cu_offset = s->pointer;
810 action_command(s);
811 break;
812 case CU_RESUME:
813 if (get_cu_state(s) != cu_suspended) {
814 logout("bad CU resume from CU state %u\n", get_cu_state(s));
815 /* Workaround for bad Linux eepro100 driver which resumes
816 * from idle state. */
817 //~ missing("cu resume");
818 set_cu_state(s, cu_suspended);
820 if (get_cu_state(s) == cu_suspended) {
821 TRACE(OTHER, logout("CU resuming\n"));
822 set_cu_state(s, cu_active);
823 action_command(s);
825 break;
826 case CU_STATSADDR:
827 /* Load dump counters address. */
828 s->statsaddr = s->pointer;
829 TRACE(OTHER, logout("val=0x%02x (status address)\n", val));
830 break;
831 case CU_SHOWSTATS:
832 /* Dump statistical counters. */
833 TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val));
834 dump_statistics(s);
835 break;
836 case CU_CMD_BASE:
837 /* Load CU base. */
838 TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val));
839 s->cu_base = s->pointer;
840 break;
841 case CU_DUMPSTATS:
842 /* Dump and reset statistical counters. */
843 TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val));
844 dump_statistics(s);
845 memset(&s->statistics, 0, sizeof(s->statistics));
846 break;
847 case CU_SRESUME:
848 /* CU static resume. */
849 missing("CU static resume");
850 break;
851 default:
852 missing("Undefined CU command");
856 static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
858 switch (val) {
859 case RU_NOP:
860 /* No operation. */
861 break;
862 case RX_START:
863 /* RU start. */
864 if (get_ru_state(s) != ru_idle) {
865 logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
866 //~ assert(!"wrong RU state");
868 set_ru_state(s, ru_ready);
869 s->ru_offset = s->pointer;
870 TRACE(OTHER, logout("val=0x%02x (rx start)\n", val));
871 break;
872 case RX_RESUME:
873 /* Restart RU. */
874 if (get_ru_state(s) != ru_suspended) {
875 logout("RU state is %u, should be %u\n", get_ru_state(s),
876 ru_suspended);
877 //~ assert(!"wrong RU state");
879 set_ru_state(s, ru_ready);
880 break;
881 case RX_ADDR_LOAD:
882 /* Load RU base. */
883 TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val));
884 s->ru_base = s->pointer;
885 break;
886 default:
887 logout("val=0x%02x (undefined RU command)\n", val);
888 missing("Undefined SU command");
892 static void eepro100_write_command(EEPRO100State * s, uint8_t val)
894 eepro100_ru_command(s, val & 0x0f);
895 eepro100_cu_command(s, val & 0xf0);
896 if ((val) == 0) {
897 TRACE(OTHER, logout("val=0x%02x\n", val));
899 /* Clear command byte after command was accepted. */
900 s->mem[SCBCmd] = 0;
903 /*****************************************************************************
905 * EEPROM emulation.
907 ****************************************************************************/
909 #define EEPROM_CS 0x02
910 #define EEPROM_SK 0x01
911 #define EEPROM_DI 0x04
912 #define EEPROM_DO 0x08
914 static uint16_t eepro100_read_eeprom(EEPRO100State * s)
916 uint16_t val;
917 memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
918 if (eeprom93xx_read(s->eeprom)) {
919 val |= EEPROM_DO;
920 } else {
921 val &= ~EEPROM_DO;
923 TRACE(EEPROM, logout("val=0x%04x\n", val));
924 return val;
927 static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
929 TRACE(EEPROM, logout("val=0x%02x\n", val));
931 /* mask unwriteable bits */
932 //~ val = SET_MASKED(val, 0x31, eeprom->value);
934 int eecs = ((val & EEPROM_CS) != 0);
935 int eesk = ((val & EEPROM_SK) != 0);
936 int eedi = ((val & EEPROM_DI) != 0);
937 eeprom93xx_write(eeprom, eecs, eesk, eedi);
940 static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
942 s->pointer = le32_to_cpu(val);
943 TRACE(OTHER, logout("val=0x%08x\n", val));
946 /*****************************************************************************
948 * MDI emulation.
950 ****************************************************************************/
952 #if defined(DEBUG_EEPRO100)
953 static const char * const mdi_op_name[] = {
954 "opcode 0",
955 "write",
956 "read",
957 "opcode 3"
960 static const char * const mdi_reg_name[] = {
961 "Control",
962 "Status",
963 "PHY Identification (Word 1)",
964 "PHY Identification (Word 2)",
965 "Auto-Negotiation Advertisement",
966 "Auto-Negotiation Link Partner Ability",
967 "Auto-Negotiation Expansion"
970 static const char *reg2name(uint8_t reg)
972 static char buffer[10];
973 const char *p = buffer;
974 if (reg < ARRAY_SIZE(mdi_reg_name)) {
975 p = mdi_reg_name[reg];
976 } else {
977 snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg);
979 return p;
981 #endif /* DEBUG_EEPRO100 */
983 static uint32_t eepro100_read_mdi(EEPRO100State * s)
985 uint32_t val;
986 memcpy(&val, &s->mem[0x10], sizeof(val));
988 #ifdef DEBUG_EEPRO100
989 uint8_t raiseint = (val & BIT(29)) >> 29;
990 uint8_t opcode = (val & BITS(27, 26)) >> 26;
991 uint8_t phy = (val & BITS(25, 21)) >> 21;
992 uint8_t reg = (val & BITS(20, 16)) >> 16;
993 uint16_t data = (val & BITS(15, 0));
994 #endif
995 /* Emulation takes no time to finish MDI transaction. */
996 val |= BIT(28);
997 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
998 val, raiseint, mdi_op_name[opcode], phy,
999 reg2name(reg), data));
1000 return val;
1003 static void eepro100_write_mdi(EEPRO100State * s, uint32_t val)
1005 uint8_t raiseint = (val & BIT(29)) >> 29;
1006 uint8_t opcode = (val & BITS(27, 26)) >> 26;
1007 uint8_t phy = (val & BITS(25, 21)) >> 21;
1008 uint8_t reg = (val & BITS(20, 16)) >> 16;
1009 uint16_t data = (val & BITS(15, 0));
1010 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1011 val, raiseint, mdi_op_name[opcode], phy, reg2name(reg), data));
1012 if (phy != 1) {
1013 /* Unsupported PHY address. */
1014 //~ logout("phy must be 1 but is %u\n", phy);
1015 data = 0;
1016 } else if (opcode != 1 && opcode != 2) {
1017 /* Unsupported opcode. */
1018 logout("opcode must be 1 or 2 but is %u\n", opcode);
1019 data = 0;
1020 } else if (reg > 6) {
1021 /* Unsupported register. */
1022 logout("register must be 0...6 but is %u\n", reg);
1023 data = 0;
1024 } else {
1025 TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
1026 val, raiseint, mdi_op_name[opcode], phy,
1027 reg2name(reg), data));
1028 if (opcode == 1) {
1029 /* MDI write */
1030 switch (reg) {
1031 case 0: /* Control Register */
1032 if (data & 0x8000) {
1033 /* Reset status and control registers to default. */
1034 s->mdimem[0] = eepro100_mdi_default[0];
1035 s->mdimem[1] = eepro100_mdi_default[1];
1036 data = s->mdimem[reg];
1037 } else {
1038 /* Restart Auto Configuration = Normal Operation */
1039 data &= ~0x0200;
1041 break;
1042 case 1: /* Status Register */
1043 missing("not writable");
1044 data = s->mdimem[reg];
1045 break;
1046 case 2: /* PHY Identification Register (Word 1) */
1047 case 3: /* PHY Identification Register (Word 2) */
1048 missing("not implemented");
1049 break;
1050 case 4: /* Auto-Negotiation Advertisement Register */
1051 case 5: /* Auto-Negotiation Link Partner Ability Register */
1052 break;
1053 case 6: /* Auto-Negotiation Expansion Register */
1054 default:
1055 missing("not implemented");
1057 s->mdimem[reg] = data;
1058 } else if (opcode == 2) {
1059 /* MDI read */
1060 switch (reg) {
1061 case 0: /* Control Register */
1062 if (data & 0x8000) {
1063 /* Reset status and control registers to default. */
1064 s->mdimem[0] = eepro100_mdi_default[0];
1065 s->mdimem[1] = eepro100_mdi_default[1];
1067 break;
1068 case 1: /* Status Register */
1069 s->mdimem[reg] |= 0x0020;
1070 break;
1071 case 2: /* PHY Identification Register (Word 1) */
1072 case 3: /* PHY Identification Register (Word 2) */
1073 case 4: /* Auto-Negotiation Advertisement Register */
1074 break;
1075 case 5: /* Auto-Negotiation Link Partner Ability Register */
1076 s->mdimem[reg] = 0x41fe;
1077 break;
1078 case 6: /* Auto-Negotiation Expansion Register */
1079 s->mdimem[reg] = 0x0001;
1080 break;
1082 data = s->mdimem[reg];
1084 /* Emulation takes no time to finish MDI transaction.
1085 * Set MDI bit in SCB status register. */
1086 s->mem[SCBAck] |= 0x08;
1087 val |= BIT(28);
1088 if (raiseint) {
1089 eepro100_mdi_interrupt(s);
1092 val = (val & 0xffff0000) + data;
1093 memcpy(&s->mem[0x10], &val, sizeof(val));
1096 /*****************************************************************************
1098 * Port emulation.
1100 ****************************************************************************/
1102 #define PORT_SOFTWARE_RESET 0
1103 #define PORT_SELFTEST 1
1104 #define PORT_SELECTIVE_RESET 2
1105 #define PORT_DUMP 3
1106 #define PORT_SELECTION_MASK 3
1108 typedef struct {
1109 uint32_t st_sign; /* Self Test Signature */
1110 uint32_t st_result; /* Self Test Results */
1111 } eepro100_selftest_t;
1113 static uint32_t eepro100_read_port(EEPRO100State * s)
1115 return 0;
1118 static void eepro100_write_port(EEPRO100State * s, uint32_t val)
1120 val = le32_to_cpu(val);
1121 uint32_t address = (val & ~PORT_SELECTION_MASK);
1122 uint8_t selection = (val & PORT_SELECTION_MASK);
1123 switch (selection) {
1124 case PORT_SOFTWARE_RESET:
1125 nic_reset(s);
1126 break;
1127 case PORT_SELFTEST:
1128 TRACE(OTHER, logout("selftest address=0x%08x\n", address));
1129 eepro100_selftest_t data;
1130 cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
1131 data.st_sign = 0xffffffff;
1132 data.st_result = 0;
1133 cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
1134 break;
1135 case PORT_SELECTIVE_RESET:
1136 TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address));
1137 nic_selective_reset(s);
1138 break;
1139 default:
1140 logout("val=0x%08x\n", val);
1141 missing("unknown port selection");
1145 /*****************************************************************************
1147 * General hardware emulation.
1149 ****************************************************************************/
1151 static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
1153 uint8_t val;
1154 if (addr <= sizeof(s->mem) - sizeof(val)) {
1155 memcpy(&val, &s->mem[addr], sizeof(val));
1158 switch (addr) {
1159 case SCBStatus:
1160 //~ val = eepro100_read_status(s);
1161 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1162 break;
1163 case SCBAck:
1164 //~ val = eepro100_read_status(s);
1165 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1166 break;
1167 case SCBCmd:
1168 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1169 //~ val = eepro100_read_command(s);
1170 break;
1171 case SCBIntmask:
1172 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1173 break;
1174 case SCBPort + 3:
1175 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1176 break;
1177 case SCBeeprom:
1178 val = eepro100_read_eeprom(s);
1179 break;
1180 case 0x1b: /* PMDR (power management driver register) */
1181 val = 0;
1182 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1183 break;
1184 case 0x1d: /* general status register */
1185 /* 100 Mbps full duplex, valid link */
1186 val = 0x07;
1187 TRACE(OTHER, logout("addr=General Status val=%02x\n", val));
1188 break;
1189 default:
1190 logout("addr=%s val=0x%02x\n", regname(addr), val);
1191 missing("unknown byte read");
1193 return val;
1196 static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
1198 uint16_t val;
1199 if (addr <= sizeof(s->mem) - sizeof(val)) {
1200 memcpy(&val, &s->mem[addr], sizeof(val));
1203 switch (addr) {
1204 case SCBStatus:
1205 //~ val = eepro100_read_status(s);
1206 case SCBCmd:
1207 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1208 break;
1209 case SCBeeprom:
1210 val = eepro100_read_eeprom(s);
1211 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1212 break;
1213 default:
1214 logout("addr=%s val=0x%04x\n", regname(addr), val);
1215 missing("unknown word read");
1217 return val;
1220 static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
1222 uint32_t val;
1223 if (addr <= sizeof(s->mem) - sizeof(val)) {
1224 memcpy(&val, &s->mem[addr], sizeof(val));
1227 switch (addr) {
1228 case SCBStatus:
1229 //~ val = eepro100_read_status(s);
1230 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1231 break;
1232 case SCBPointer:
1233 //~ val = eepro100_read_pointer(s);
1234 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1235 break;
1236 case SCBPort:
1237 val = eepro100_read_port(s);
1238 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1239 break;
1240 case SCBCtrlMDI:
1241 val = eepro100_read_mdi(s);
1242 break;
1243 default:
1244 logout("addr=%s val=0x%08x\n", regname(addr), val);
1245 missing("unknown longword read");
1247 return val;
1250 static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
1252 if (addr <= sizeof(s->mem) - sizeof(val)) {
1253 memcpy(&s->mem[addr], &val, sizeof(val));
1256 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1258 switch (addr) {
1259 case SCBStatus:
1260 //~ eepro100_write_status(s, val);
1261 break;
1262 case SCBAck:
1263 eepro100_acknowledge(s);
1264 break;
1265 case SCBCmd:
1266 eepro100_write_command(s, val);
1267 break;
1268 case SCBIntmask:
1269 if (val & BIT(1)) {
1270 eepro100_swi_interrupt(s);
1272 eepro100_interrupt(s, 0);
1273 break;
1274 case SCBPort + 3:
1275 case SCBFlow: /* does not exist on 82557 */
1276 case SCBFlow + 1:
1277 case SCBFlow + 2:
1278 case SCBFlow + 3:
1279 TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val));
1280 break;
1281 case SCBeeprom:
1282 eepro100_write_eeprom(s->eeprom, val);
1283 break;
1284 default:
1285 logout("addr=%s val=0x%02x\n", regname(addr), val);
1286 missing("unknown byte write");
1290 static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
1292 if (addr <= sizeof(s->mem) - sizeof(val)) {
1293 memcpy(&s->mem[addr], &val, sizeof(val));
1296 TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val));
1298 switch (addr) {
1299 case SCBStatus:
1300 //~ eepro100_write_status(s, val);
1301 eepro100_acknowledge(s);
1302 break;
1303 case SCBCmd:
1304 eepro100_write_command(s, val);
1305 eepro100_write1(s, SCBIntmask, val >> 8);
1306 break;
1307 case SCBeeprom:
1308 eepro100_write_eeprom(s->eeprom, val);
1309 break;
1310 default:
1311 logout("addr=%s val=0x%04x\n", regname(addr), val);
1312 missing("unknown word write");
1316 static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
1318 if (addr <= sizeof(s->mem) - sizeof(val)) {
1319 memcpy(&s->mem[addr], &val, sizeof(val));
1322 switch (addr) {
1323 case SCBPointer:
1324 eepro100_write_pointer(s, val);
1325 break;
1326 case SCBPort:
1327 TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val));
1328 eepro100_write_port(s, val);
1329 break;
1330 case SCBCtrlMDI:
1331 eepro100_write_mdi(s, val);
1332 break;
1333 default:
1334 logout("addr=%s val=0x%08x\n", regname(addr), val);
1335 missing("unknown longword write");
1339 /*****************************************************************************
1341 * Port mapped I/O.
1343 ****************************************************************************/
1345 static uint32_t ioport_read1(void *opaque, uint32_t addr)
1347 EEPRO100State *s = opaque;
1348 //~ logout("addr=%s\n", regname(addr));
1349 return eepro100_read1(s, addr - s->region[1]);
1352 static uint32_t ioport_read2(void *opaque, uint32_t addr)
1354 EEPRO100State *s = opaque;
1355 return eepro100_read2(s, addr - s->region[1]);
1358 static uint32_t ioport_read4(void *opaque, uint32_t addr)
1360 EEPRO100State *s = opaque;
1361 return eepro100_read4(s, addr - s->region[1]);
1364 static void ioport_write1(void *opaque, uint32_t addr, uint32_t val)
1366 EEPRO100State *s = opaque;
1367 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1368 eepro100_write1(s, addr - s->region[1], val);
1371 static void ioport_write2(void *opaque, uint32_t addr, uint32_t val)
1373 EEPRO100State *s = opaque;
1374 eepro100_write2(s, addr - s->region[1], val);
1377 static void ioport_write4(void *opaque, uint32_t addr, uint32_t val)
1379 EEPRO100State *s = opaque;
1380 eepro100_write4(s, addr - s->region[1], val);
1383 /***********************************************************/
1384 /* PCI EEPRO100 definitions */
1386 static void pci_map(PCIDevice * pci_dev, int region_num,
1387 pcibus_t addr, pcibus_t size, int type)
1389 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1391 TRACE(OTHER, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1392 region_num, addr, size, type));
1394 assert(region_num == 1);
1395 register_ioport_write(addr, size, 1, ioport_write1, s);
1396 register_ioport_read(addr, size, 1, ioport_read1, s);
1397 register_ioport_write(addr, size, 2, ioport_write2, s);
1398 register_ioport_read(addr, size, 2, ioport_read2, s);
1399 register_ioport_write(addr, size, 4, ioport_write4, s);
1400 register_ioport_read(addr, size, 4, ioport_read4, s);
1402 s->region[region_num] = addr;
1405 /*****************************************************************************
1407 * Memory mapped I/O.
1409 ****************************************************************************/
1411 static void pci_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1413 EEPRO100State *s = opaque;
1414 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1415 eepro100_write1(s, addr, val);
1418 static void pci_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1420 EEPRO100State *s = opaque;
1421 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1422 eepro100_write2(s, addr, val);
1425 static void pci_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1427 EEPRO100State *s = opaque;
1428 //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1429 eepro100_write4(s, addr, val);
1432 static uint32_t pci_mmio_readb(void *opaque, target_phys_addr_t addr)
1434 EEPRO100State *s = opaque;
1435 //~ logout("addr=%s\n", regname(addr));
1436 return eepro100_read1(s, addr);
1439 static uint32_t pci_mmio_readw(void *opaque, target_phys_addr_t addr)
1441 EEPRO100State *s = opaque;
1442 //~ logout("addr=%s\n", regname(addr));
1443 return eepro100_read2(s, addr);
1446 static uint32_t pci_mmio_readl(void *opaque, target_phys_addr_t addr)
1448 EEPRO100State *s = opaque;
1449 //~ logout("addr=%s\n", regname(addr));
1450 return eepro100_read4(s, addr);
1453 static CPUWriteMemoryFunc * const pci_mmio_write[] = {
1454 pci_mmio_writeb,
1455 pci_mmio_writew,
1456 pci_mmio_writel
1459 static CPUReadMemoryFunc * const pci_mmio_read[] = {
1460 pci_mmio_readb,
1461 pci_mmio_readw,
1462 pci_mmio_readl
1465 static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
1466 pcibus_t addr, pcibus_t size, int type)
1468 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1470 TRACE(OTHER, logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1471 region_num, addr, size, type));
1473 if (region_num == 0) {
1474 /* Map control / status registers. */
1475 cpu_register_physical_memory(addr, size, s->mmio_index);
1476 s->region[region_num] = addr;
1480 static int nic_can_receive(VLANClientState *vc)
1482 EEPRO100State *s = vc->opaque;
1483 TRACE(RXTX, logout("%p\n", s));
1484 return get_ru_state(s) == ru_ready;
1485 //~ return !eepro100_buffer_full(s);
1488 static ssize_t nic_receive(VLANClientState *vc, const uint8_t * buf, size_t size)
1490 /* TODO:
1491 * - Magic packets should set bit 30 in power management driver register.
1492 * - Interesting packets should set bit 29 in power management driver register.
1494 EEPRO100State *s = vc->opaque;
1495 uint16_t rfd_status = 0xa000;
1496 static const uint8_t broadcast_macaddr[6] =
1497 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1499 /* TODO: check multiple IA bit. */
1500 if (s->configuration[20] & BIT(6)) {
1501 missing("Multiple IA bit");
1502 return -1;
1505 if (s->configuration[8] & 0x80) {
1506 /* CSMA is disabled. */
1507 logout("%p received while CSMA is disabled\n", s);
1508 return -1;
1509 } else if (size < 64 && (s->configuration[7] & 1)) {
1510 /* Short frame and configuration byte 7/0 (discard short receive) set:
1511 * Short frame is discarded */
1512 logout("%p received short frame (%zu byte)\n", s, size);
1513 s->statistics.rx_short_frame_errors++;
1514 //~ return -1;
1515 } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & 8)) {
1516 /* Long frame and configuration byte 18/3 (long receive ok) not set:
1517 * Long frames are discarded. */
1518 logout("%p received long frame (%zu byte), ignored\n", s, size);
1519 return -1;
1520 } else if (memcmp(buf, s->conf.macaddr.a, 6) == 0) { // !!!
1521 /* Frame matches individual address. */
1522 /* TODO: check configuration byte 15/4 (ignore U/L). */
1523 TRACE(RXTX, logout("%p received frame for me, len=%zu\n", s, size));
1524 } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
1525 /* Broadcast frame. */
1526 TRACE(RXTX, logout("%p received broadcast, len=%zu\n", s, size));
1527 rfd_status |= 0x0002;
1528 } else if (buf[0] & 0x01) { // !!!
1529 /* Multicast frame. */
1530 TRACE(RXTX, logout("%p received multicast, len=%zu\n", s, size));
1531 /* TODO: check multicast all bit. */
1532 if (s->configuration[21] & BIT(3)) {
1533 missing("Multicast All bit");
1535 int mcast_idx = compute_mcast_idx(buf);
1536 if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7)))) {
1537 return size;
1539 rfd_status |= 0x0002;
1540 } else if (s->configuration[15] & 1) {
1541 /* Promiscuous: receive all. */
1542 TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%zu\n", s, size));
1543 rfd_status |= 0x0004;
1544 } else {
1545 TRACE(RXTX, logout("%p received frame, ignored, len=%zu,%s\n", s, size,
1546 nic_dump(buf, size)));
1547 return size;
1550 if (get_ru_state(s) != ru_ready) {
1551 /* No resources available. */
1552 logout("no resources, state=%u\n", get_ru_state(s));
1553 s->statistics.rx_resource_errors++;
1554 //~ assert(!"no resources");
1555 return -1;
1557 //~ !!!
1558 //~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
1559 eepro100_rx_t rx;
1560 cpu_physical_memory_read(s->ru_base + s->ru_offset, (uint8_t *) & rx,
1561 offsetof(eepro100_rx_t, packet));
1562 uint16_t rfd_command = le16_to_cpu(rx.command);
1563 uint16_t rfd_size = le16_to_cpu(rx.size);
1565 if (size > rfd_size) {
1566 logout("Receive buffer (%" PRId16 " bytes) too small for data "
1567 "(%zu bytes); data truncated\n", rfd_size, size);
1568 size = rfd_size;
1570 if (size < 64) {
1571 rfd_status |= 0x0080;
1573 TRACE(OTHER, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n",
1574 rfd_command, rx.link, rx.rx_buf_addr, rfd_size));
1575 stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, status),
1576 rfd_status);
1577 stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, count), size);
1578 /* Early receive interrupt not supported. */
1579 //~ eepro100_er_interrupt(s);
1580 /* Receive CRC Transfer not supported. */
1581 if (s->configuration[18] & 4) {
1582 missing("Receive CRC Transfer");
1583 return -1;
1585 /* TODO: check stripping enable bit. */
1586 //~ assert(!(s->configuration[17] & 1));
1587 cpu_physical_memory_write(s->ru_base + s->ru_offset +
1588 offsetof(eepro100_rx_t, packet), buf, size);
1589 s->statistics.rx_good_frames++;
1590 eepro100_fr_interrupt(s);
1591 s->ru_offset = le32_to_cpu(rx.link);
1592 if (rfd_command & 0x8000) {
1593 /* EL bit is set, so this was the last frame. */
1594 logout("receive: Running out of frames\n");
1595 set_ru_state(s, ru_suspended);
1597 if (rfd_command & 0x4000) {
1598 /* S bit is set. */
1599 set_ru_state(s, ru_suspended);
1601 return size;
1604 static const VMStateDescription vmstate_eepro100 = {
1605 .version_id = 3,
1606 .minimum_version_id = 2,
1607 .minimum_version_id_old = 2,
1608 .fields = (VMStateField []) {
1609 VMSTATE_PCI_DEVICE(dev, EEPRO100State),
1610 VMSTATE_UNUSED(32),
1611 VMSTATE_BUFFER(mult, EEPRO100State),
1612 VMSTATE_BUFFER(mem, EEPRO100State),
1613 /* Save all members of struct between scb_stat and mem. */
1614 VMSTATE_UINT8(scb_stat, EEPRO100State),
1615 VMSTATE_UINT8(int_stat, EEPRO100State),
1616 VMSTATE_UNUSED(3*4),
1617 VMSTATE_MACADDR(conf.macaddr, EEPRO100State),
1618 VMSTATE_UNUSED(19*4),
1619 VMSTATE_UINT16_ARRAY(mdimem, EEPRO100State, 32),
1620 /* The eeprom should be saved and restored by its own routines. */
1621 VMSTATE_UINT32(device, EEPRO100State),
1622 /* TODO check device. */
1623 VMSTATE_UINT32(pointer, EEPRO100State),
1624 VMSTATE_UINT32(cu_base, EEPRO100State),
1625 VMSTATE_UINT32(cu_offset, EEPRO100State),
1626 VMSTATE_UINT32(ru_base, EEPRO100State),
1627 VMSTATE_UINT32(ru_offset, EEPRO100State),
1628 VMSTATE_UINT32(statsaddr, EEPRO100State),
1629 /* Save epro100_stats_t statistics. */
1630 VMSTATE_UINT32(statistics.tx_good_frames, EEPRO100State),
1631 VMSTATE_UINT32(statistics.tx_max_collisions, EEPRO100State),
1632 VMSTATE_UINT32(statistics.tx_late_collisions, EEPRO100State),
1633 VMSTATE_UINT32(statistics.tx_underruns, EEPRO100State),
1634 VMSTATE_UINT32(statistics.tx_lost_crs, EEPRO100State),
1635 VMSTATE_UINT32(statistics.tx_deferred, EEPRO100State),
1636 VMSTATE_UINT32(statistics.tx_single_collisions, EEPRO100State),
1637 VMSTATE_UINT32(statistics.tx_multiple_collisions, EEPRO100State),
1638 VMSTATE_UINT32(statistics.tx_total_collisions, EEPRO100State),
1639 VMSTATE_UINT32(statistics.rx_good_frames, EEPRO100State),
1640 VMSTATE_UINT32(statistics.rx_crc_errors, EEPRO100State),
1641 VMSTATE_UINT32(statistics.rx_alignment_errors, EEPRO100State),
1642 VMSTATE_UINT32(statistics.rx_resource_errors, EEPRO100State),
1643 VMSTATE_UINT32(statistics.rx_overrun_errors, EEPRO100State),
1644 VMSTATE_UINT32(statistics.rx_cdt_errors, EEPRO100State),
1645 VMSTATE_UINT32(statistics.rx_short_frame_errors, EEPRO100State),
1646 VMSTATE_UINT32(statistics.fc_xmt_pause, EEPRO100State),
1647 VMSTATE_UINT32(statistics.fc_rcv_pause, EEPRO100State),
1648 VMSTATE_UINT32(statistics.fc_rcv_unsupported, EEPRO100State),
1649 VMSTATE_UINT16(statistics.xmt_tco_frames, EEPRO100State),
1650 VMSTATE_UINT16(statistics.rcv_tco_frames, EEPRO100State),
1651 VMSTATE_UINT32(statistics.complete, EEPRO100State),
1652 #if 0
1653 VMSTATE_UINT16(status, EEPRO100State),
1654 #endif
1655 /* Configuration bytes. */
1656 VMSTATE_BUFFER(configuration, EEPRO100State),
1657 VMSTATE_END_OF_LIST()
1661 static void nic_cleanup(VLANClientState *vc)
1663 EEPRO100State *s = vc->opaque;
1665 s->vc = NULL;
1668 static int pci_nic_uninit(PCIDevice *pci_dev)
1670 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1672 cpu_unregister_io_memory(s->mmio_index);
1673 vmstate_unregister(s->vmstate, s);
1674 eeprom93xx_free(s->eeprom);
1675 qemu_del_vlan_client(s->vc);
1676 return 0;
1679 static int nic_init(PCIDevice *pci_dev, uint32_t device)
1681 EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1683 TRACE(OTHER, logout("\n"));
1685 s->device = device;
1687 pci_reset(s);
1689 /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1690 * i82559 and later support 64 or 256 word EEPROM. */
1691 s->eeprom = eeprom93xx_new(EEPROM_SIZE);
1693 /* Handler for memory-mapped I/O */
1694 s->mmio_index =
1695 cpu_register_io_memory(pci_mmio_read, pci_mmio_write, s);
1697 pci_register_bar(&s->dev, 0, PCI_MEM_SIZE,
1698 PCI_BASE_ADDRESS_SPACE_MEMORY |
1699 PCI_BASE_ADDRESS_MEM_PREFETCH, pci_mmio_map);
1700 pci_register_bar(&s->dev, 1, PCI_IO_SIZE, PCI_BASE_ADDRESS_SPACE_IO,
1701 pci_map);
1702 pci_register_bar(&s->dev, 2, PCI_FLASH_SIZE, PCI_BASE_ADDRESS_SPACE_MEMORY,
1703 pci_mmio_map);
1705 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1706 logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
1707 assert(s->region[1] == 0);
1709 nic_reset(s);
1711 s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_NIC,
1712 s->conf.vlan, s->conf.peer,
1713 pci_dev->qdev.info->name, pci_dev->qdev.id,
1714 nic_can_receive, nic_receive, NULL, NULL,
1715 nic_cleanup, s);
1717 qemu_format_nic_info_str(s->vc, s->conf.macaddr.a);
1718 TRACE(OTHER, logout("%s\n", s->vc->info_str));
1720 qemu_register_reset(nic_reset, s);
1722 s->vmstate = qemu_malloc(sizeof(vmstate_eepro100));
1723 memcpy(s->vmstate, &vmstate_eepro100, sizeof(vmstate_eepro100));
1724 s->vmstate->name = s->vc->model;
1725 vmstate_register(-1, s->vmstate, s);
1726 return 0;
1729 static int pci_i82550_init(PCIDevice *pci_dev)
1731 return nic_init(pci_dev, i82550);
1734 static int pci_i82551_init(PCIDevice *pci_dev)
1736 return nic_init(pci_dev, i82551);
1739 static int pci_i82557a_init(PCIDevice *pci_dev)
1741 return nic_init(pci_dev, i82557A);
1744 static int pci_i82557b_init(PCIDevice *pci_dev)
1746 return nic_init(pci_dev, i82557B);
1749 static int pci_i82557c_init(PCIDevice *pci_dev)
1751 return nic_init(pci_dev, i82557C);
1754 static int pci_i82558a_init(PCIDevice *pci_dev)
1756 return nic_init(pci_dev, i82558A);
1759 static int pci_i82558b_init(PCIDevice *pci_dev)
1761 return nic_init(pci_dev, i82558B);
1764 static int pci_i82559a_init(PCIDevice *pci_dev)
1766 return nic_init(pci_dev, i82559A);
1769 static int pci_i82559b_init(PCIDevice *pci_dev)
1771 return nic_init(pci_dev, i82559B);
1774 static int pci_i82559c_init(PCIDevice *pci_dev)
1776 return nic_init(pci_dev, i82559C);
1779 static int pci_i82559er_init(PCIDevice *pci_dev)
1781 return nic_init(pci_dev, i82559ER);
1784 static int pci_i82562_init(PCIDevice *pci_dev)
1786 return nic_init(pci_dev, i82562);
1789 static PCIDeviceInfo eepro100_info[] = {
1791 .qdev.name = "i82550",
1792 .qdev.size = sizeof(EEPRO100State),
1793 .init = pci_i82550_init,
1794 .qdev.props = (Property[]) {
1795 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1796 DEFINE_PROP_END_OF_LIST(),
1799 .qdev.name = "i82551",
1800 .qdev.size = sizeof(EEPRO100State),
1801 .init = pci_i82551_init,
1802 .exit = pci_nic_uninit,
1803 .qdev.props = (Property[]) {
1804 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1805 DEFINE_PROP_END_OF_LIST(),
1808 .qdev.name = "i82557a",
1809 .qdev.size = sizeof(EEPRO100State),
1810 .init = pci_i82557a_init,
1811 .qdev.props = (Property[]) {
1812 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1813 DEFINE_PROP_END_OF_LIST(),
1816 .qdev.name = "i82557b",
1817 .qdev.size = sizeof(EEPRO100State),
1818 .init = pci_i82557b_init,
1819 .exit = pci_nic_uninit,
1820 .qdev.props = (Property[]) {
1821 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1822 DEFINE_PROP_END_OF_LIST(),
1825 .qdev.name = "i82557c",
1826 .qdev.size = sizeof(EEPRO100State),
1827 .init = pci_i82557c_init,
1828 .qdev.props = (Property[]) {
1829 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1830 DEFINE_PROP_END_OF_LIST(),
1833 .qdev.name = "i82558a",
1834 .qdev.size = sizeof(EEPRO100State),
1835 .init = pci_i82558a_init,
1836 .qdev.props = (Property[]) {
1837 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1838 DEFINE_PROP_END_OF_LIST(),
1841 .qdev.name = "i82558b",
1842 .qdev.size = sizeof(EEPRO100State),
1843 .init = pci_i82558b_init,
1844 .qdev.props = (Property[]) {
1845 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1846 DEFINE_PROP_END_OF_LIST(),
1849 .qdev.name = "i82559a",
1850 .qdev.size = sizeof(EEPRO100State),
1851 .init = pci_i82559a_init,
1852 .qdev.props = (Property[]) {
1853 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1854 DEFINE_PROP_END_OF_LIST(),
1857 .qdev.name = "i82559b",
1858 .qdev.size = sizeof(EEPRO100State),
1859 .init = pci_i82559b_init,
1860 .qdev.props = (Property[]) {
1861 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1862 DEFINE_PROP_END_OF_LIST(),
1865 .qdev.name = "i82559c",
1866 .qdev.size = sizeof(EEPRO100State),
1867 .init = pci_i82559c_init,
1868 .qdev.props = (Property[]) {
1869 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1870 DEFINE_PROP_END_OF_LIST(),
1873 .qdev.name = "i82559er",
1874 .qdev.size = sizeof(EEPRO100State),
1875 .init = pci_i82559er_init,
1876 .exit = pci_nic_uninit,
1877 .qdev.props = (Property[]) {
1878 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1879 DEFINE_PROP_END_OF_LIST(),
1882 .qdev.name = "i82562",
1883 .qdev.size = sizeof(EEPRO100State),
1884 .init = pci_i82562_init,
1885 .qdev.props = (Property[]) {
1886 DEFINE_NIC_PROPERTIES(EEPRO100State, conf),
1887 DEFINE_PROP_END_OF_LIST(),
1890 /* end of list */
1894 static void eepro100_register_devices(void)
1896 pci_qdev_register_many(eepro100_info);
1899 device_init(eepro100_register_devices)