e28fd5b9453d25442c3ee9635acb5af58c3138a6
[qemu] / hw / eepro100.c
1 /*
2  * QEMU i8255x (PRO100) emulation
3  *
4  * Copyright (c) 2006-2007 Stefan Weil
5  *
6  * Portions of the code are copies from grub / etherboot eepro100.c
7  * and linux e100.c.
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * Tested features (i82559):
23  *      PXE boot (i386) no valid link
24  *      Linux networking (i386) ok
25  *
26  * Untested:
27  *      non-i386 platforms
28  *      Windows networking
29  *
30  * References:
31  *
32  * Intel 8255x 10/100 Mbps Ethernet Controller Family
33  * Open Source Software Developer Manual
34  */
35
36 #if defined(TARGET_I386)
37 # warning "PXE boot still not working!"
38 #endif
39
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"
46
47 /* Common declarations for all PCI devices. */
48
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))
55
56 #define KiB 1024
57
58 /* debug EEPRO100 card */
59 //~ #define DEBUG_EEPRO100
60
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
66
67 /* Set flags to 0 to disable debug output. */
68 #define MDI     0
69
70 #define TRACE(flag, command) ((flag) ? (command) : (void)0)
71
72 #define missing(text)       assert(!"feature is missing in this emulation: " text)
73
74 #define MAX_ETH_FRAME_SIZE 1514
75
76 /* This driver supports several different devices which are declared here. */
77 #define i82551          0x82551
78 #define i82557B         0x82557b
79 #define i82557C         0x82557c
80 #define i82558B         0x82558b
81 #define i82559C         0x82559c
82 #define i82559ER        0x82559e
83 #define i82562          0x82562
84
85 #define EEPROM_SIZE     64
86
87 #define PCI_MEM_SIZE            (4 * KiB)
88 #define PCI_IO_SIZE             64
89 #define PCI_FLASH_SIZE          (128 * KiB)
90
91 #define BIT(n) (1 << (n))
92 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
93
94 /* The SCB accepts the following controls for the Tx and Rx units: */
95 #define  CU_NOP         0x0000  /* No operation. */
96 #define  CU_START       0x0010  /* CU start. */
97 #define  CU_RESUME      0x0020  /* CU resume. */
98 #define  CU_STATSADDR   0x0040  /* Load dump counters address. */
99 #define  CU_SHOWSTATS   0x0050  /* Dump statistical counters. */
100 #define  CU_CMD_BASE    0x0060  /* Load CU base address. */
101 #define  CU_DUMPSTATS   0x0070  /* Dump and reset statistical counters. */
102 #define  CU_SRESUME     0x00a0  /* CU static resume. */
103
104 #define  RU_NOP         0x0000
105 #define  RX_START       0x0001
106 #define  RX_RESUME      0x0002
107 #define  RX_ABORT       0x0004
108 #define  RX_ADDR_LOAD   0x0006
109 #define  RX_RESUMENR    0x0007
110 #define INT_MASK        0x0100
111 #define DRVR_INT        0x0200  /* Driver generated interrupt. */
112
113 /* Offsets to the various registers.
114    All accesses need not be longword aligned. */
115 enum speedo_offsets {
116     SCBStatus = 0,
117     SCBAck = 1,
118     SCBCmd = 2,                 /* Rx/Command Unit command and status. */
119     SCBIntmask = 3,
120     SCBPointer = 4,             /* General purpose pointer. */
121     SCBPort = 8,                /* Misc. commands and operands.  */
122     SCBflash = 12, SCBeeprom = 14,      /* EEPROM and flash memory control. */
123     SCBCtrlMDI = 16,            /* MDI interface control. */
124     SCBEarlyRx = 20,            /* Early receive byte count. */
125     SCBFlow = 24,
126 };
127
128 /* A speedo3 transmit buffer descriptor with two buffers... */
129 typedef struct {
130     uint16_t status;
131     uint16_t command;
132     uint32_t link;              /* void * */
133     uint32_t tx_desc_addr;      /* transmit buffer decsriptor array address. */
134     uint16_t tcb_bytes;         /* transmit command block byte count (in lower 14 bits */
135     uint8_t tx_threshold;       /* transmit threshold */
136     uint8_t tbd_count;          /* TBD number */
137     //~ /* This constitutes two "TBD" entries: hdr and data */
138     //~ uint32_t tx_buf_addr0;  /* void *, header of frame to be transmitted.  */
139     //~ int32_t  tx_buf_size0;  /* Length of Tx hdr. */
140     //~ uint32_t tx_buf_addr1;  /* void *, data to be transmitted.  */
141     //~ int32_t  tx_buf_size1;  /* Length of Tx data. */
142 } eepro100_tx_t;
143
144 /* Receive frame descriptor. */
145 typedef struct {
146     int16_t status;
147     uint16_t command;
148     uint32_t link;              /* struct RxFD * */
149     uint32_t rx_buf_addr;       /* void * */
150     uint16_t count;
151     uint16_t size;
152     char packet[MAX_ETH_FRAME_SIZE + 4];
153 } eepro100_rx_t;
154
155 typedef struct {
156     uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions,
157         tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
158         tx_multiple_collisions, tx_total_collisions;
159     uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors,
160         rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
161         rx_short_frame_errors;
162     uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
163     uint16_t xmt_tco_frames, rcv_tco_frames;
164     uint32_t complete;
165 } eepro100_stats_t;
166
167 typedef enum {
168     cu_idle = 0,
169     cu_suspended = 1,
170     cu_active = 2,
171     cu_lpq_active = 2,
172     cu_hqp_active = 3
173 } cu_state_t;
174
175 typedef enum {
176     ru_idle = 0,
177     ru_suspended = 1,
178     ru_no_resources = 2,
179     ru_ready = 4
180 } ru_state_t;
181
182 typedef struct {
183     PCIDevice dev;
184 #if 1
185     uint8_t cmd;
186     uint32_t start;
187     uint32_t stop;
188     uint8_t boundary;
189     uint8_t tsr;
190     uint8_t tpsr;
191     uint16_t tcnt;
192     uint16_t rcnt;
193     uint32_t rsar;
194     uint8_t rsr;
195     uint8_t rxcr;
196     uint8_t isr;
197     uint8_t dcfg;
198     uint8_t imr;
199     uint8_t phys[6];            /* mac address */
200     uint8_t curpag;
201     uint8_t mult[8];            /* multicast mask array */
202     int mmio_index;
203     VLANClientState *vc;
204 #endif
205     uint8_t scb_stat;           /* SCB stat/ack byte */
206     uint8_t int_stat;           /* PCI interrupt status */
207     uint32_t region[3];         /* PCI region addresses */
208     uint8_t macaddr[6];
209     uint32_t statcounter[19];
210     uint16_t mdimem[32];
211     eeprom_t *eeprom;
212     uint32_t device;            /* device variant */
213     uint32_t pointer;
214     /* (cu_base + cu_offset) address the next command block in the command block list. */
215     uint32_t cu_base;           /* CU base address */
216     uint32_t cu_offset;         /* CU address offset */
217     /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */
218     uint32_t ru_base;           /* RU base address */
219     uint32_t ru_offset;         /* RU address offset */
220     uint32_t statsaddr;         /* pointer to eepro100_stats_t */
221     eepro100_stats_t statistics;        /* statistical counters */
222 #if 0
223     uint16_t status;
224 #endif
225
226     /* Configuration bytes. */
227     uint8_t configuration[22];
228
229     /* Data in mem is always in the byte order of the controller (le). */
230     uint8_t mem[PCI_MEM_SIZE];
231 } EEPRO100State;
232
233 /* Default values for MDI (PHY) registers */
234 static const uint16_t eepro100_mdi_default[] = {
235     /* MDI Registers 0 - 6, 7 */
236     0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000,
237     /* MDI Registers 8 - 15 */
238     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
239     /* MDI Registers 16 - 31 */
240     0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
241     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
242 };
243
244 /* Readonly mask for MDI (PHY) registers */
245 static const uint16_t eepro100_mdi_mask[] = {
246     0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
247     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
248     0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
249     0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
250 };
251
252 #define POLYNOMIAL 0x04c11db6
253
254 /* From FreeBSD */
255 /* XXX: optimize */
256 static int compute_mcast_idx(const uint8_t * ep)
257 {
258     uint32_t crc;
259     int carry, i, j;
260     uint8_t b;
261
262     crc = 0xffffffff;
263     for (i = 0; i < 6; i++) {
264         b = *ep++;
265         for (j = 0; j < 8; j++) {
266             carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
267             crc <<= 1;
268             b >>= 1;
269             if (carry)
270                 crc = ((crc ^ POLYNOMIAL) | carry);
271         }
272     }
273     return (crc >> 26);
274 }
275
276 #if defined(DEBUG_EEPRO100)
277 static const char *nic_dump(const uint8_t * buf, unsigned size)
278 {
279     static char dump[3 * 16 + 1];
280     char *p = &dump[0];
281     if (size > 16)
282         size = 16;
283     while (size-- > 0) {
284         p += sprintf(p, " %02x", *buf++);
285     }
286     return dump;
287 }
288 #endif                          /* DEBUG_EEPRO100 */
289
290 enum scb_stat_ack {
291     stat_ack_not_ours = 0x00,
292     stat_ack_sw_gen = 0x04,
293     stat_ack_rnr = 0x10,
294     stat_ack_cu_idle = 0x20,
295     stat_ack_frame_rx = 0x40,
296     stat_ack_cu_cmd_done = 0x80,
297     stat_ack_not_present = 0xFF,
298     stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
299     stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
300 };
301
302 static void disable_interrupt(EEPRO100State * s)
303 {
304     if (s->int_stat) {
305         logout("interrupt disabled\n");
306         qemu_irq_lower(s->dev.irq[0]);
307         s->int_stat = 0;
308     }
309 }
310
311 static void enable_interrupt(EEPRO100State * s)
312 {
313     if (!s->int_stat) {
314         logout("interrupt enabled\n");
315         qemu_irq_raise(s->dev.irq[0]);
316         s->int_stat = 1;
317     }
318 }
319
320 static void eepro100_acknowledge(EEPRO100State * s)
321 {
322     s->scb_stat &= ~s->mem[SCBAck];
323     s->mem[SCBAck] = s->scb_stat;
324     if (s->scb_stat == 0) {
325         disable_interrupt(s);
326     }
327 }
328
329 static void eepro100_interrupt(EEPRO100State * s, uint8_t stat)
330 {
331     uint8_t mask = ~s->mem[SCBIntmask];
332     s->mem[SCBAck] |= stat;
333     stat = s->scb_stat = s->mem[SCBAck];
334     stat &= (mask | 0x0f);
335     //~ stat &= (~s->mem[SCBIntmask] | 0x0xf);
336     if (stat && (mask & 0x01)) {
337         /* SCB mask and SCB Bit M do not disable interrupt. */
338         enable_interrupt(s);
339     } else if (s->int_stat) {
340         disable_interrupt(s);
341     }
342 }
343
344 static void eepro100_cx_interrupt(EEPRO100State * s)
345 {
346     /* CU completed action command. */
347     /* Transmit not ok (82557 only, not in emulation). */
348     eepro100_interrupt(s, 0x80);
349 }
350
351 static void eepro100_cna_interrupt(EEPRO100State * s)
352 {
353     /* CU left the active state. */
354     eepro100_interrupt(s, 0x20);
355 }
356
357 static void eepro100_fr_interrupt(EEPRO100State * s)
358 {
359     /* RU received a complete frame. */
360     eepro100_interrupt(s, 0x40);
361 }
362
363 #if 0
364 static void eepro100_rnr_interrupt(EEPRO100State * s)
365 {
366     /* RU is not ready. */
367     eepro100_interrupt(s, 0x10);
368 }
369 #endif
370
371 static void eepro100_mdi_interrupt(EEPRO100State * s)
372 {
373     /* MDI completed read or write cycle. */
374     eepro100_interrupt(s, 0x08);
375 }
376
377 static void eepro100_swi_interrupt(EEPRO100State * s)
378 {
379     /* Software has requested an interrupt. */
380     eepro100_interrupt(s, 0x04);
381 }
382
383 #if 0
384 static void eepro100_fcp_interrupt(EEPRO100State * s)
385 {
386     /* Flow control pause interrupt (82558 and later). */
387     eepro100_interrupt(s, 0x01);
388 }
389 #endif
390
391 static void pci_reset(EEPRO100State * s)
392 {
393     uint32_t device = s->device;
394     uint8_t *pci_conf = s->dev.config;
395
396     logout("%p\n", s);
397
398     /* PCI Vendor ID */
399     pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
400     /* PCI Device ID depends on device and is set below. */
401     /* PCI Command */
402     PCI_CONFIG_16(PCI_COMMAND, 0x0000);
403     /* PCI Status */
404     PCI_CONFIG_16(PCI_STATUS, 0x2800);
405     /* PCI Revision ID */
406     PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
407     /* PCI Class Code */
408     PCI_CONFIG_8(0x09, 0x00);
409     pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
410     /* PCI Cache Line Size */
411     /* check cache line size!!! */
412     //~ PCI_CONFIG_8(0x0c, 0x00);
413     /* PCI Latency Timer */
414     PCI_CONFIG_8(0x0d, 0x20);   // latency timer = 32 clocks
415     /* PCI Header Type */
416     /* BIST (built-in self test) */
417 #if defined(TARGET_I386)
418 // !!! workaround for buggy bios
419 //~ #define PCI_ADDRESS_SPACE_MEM_PREFETCH 0
420 #endif
421 #if 0
422     /* PCI Base Address Registers */
423     /* CSR Memory Mapped Base Address */
424     PCI_CONFIG_32(PCI_BASE_ADDRESS_0,
425                   PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_MEM_PREFETCH);
426     /* CSR I/O Mapped Base Address */
427     PCI_CONFIG_32(PCI_BASE_ADDRESS_1, PCI_ADDRESS_SPACE_IO);
428 #if 0
429     /* Flash Memory Mapped Base Address */
430     PCI_CONFIG_32(PCI_BASE_ADDRESS_2, 0xfffe0000 | PCI_ADDRESS_SPACE_MEM);
431 #endif
432 #endif
433     /* Expansion ROM Base Address (depends on boot disable!!!) */
434     PCI_CONFIG_32(0x30, 0x00000000);
435     /* Capability Pointer */
436     PCI_CONFIG_8(0x34, 0xdc);
437     /* Interrupt Pin */
438     PCI_CONFIG_8(0x3d, 1);      // interrupt pin 0
439     /* Minimum Grant */
440     PCI_CONFIG_8(0x3e, 0x08);
441     /* Maximum Latency */
442     PCI_CONFIG_8(0x3f, 0x18);
443     /* Power Management Capabilities / Next Item Pointer / Capability ID */
444     PCI_CONFIG_32(0xdc, 0x7e210001);
445
446     switch (device) {
447     case i82551:
448         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
449         PCI_CONFIG_8(PCI_REVISION_ID, 0x0f);
450         break;
451     case i82557B:
452         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
453         PCI_CONFIG_8(PCI_REVISION_ID, 0x02);
454         break;
455     case i82557C:
456         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
457         PCI_CONFIG_8(PCI_REVISION_ID, 0x03);
458         break;
459     case i82558B:
460         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
461         PCI_CONFIG_16(PCI_STATUS, 0x2810);
462         PCI_CONFIG_8(PCI_REVISION_ID, 0x05);
463         break;
464     case i82559C:
465         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82557);
466         PCI_CONFIG_16(PCI_STATUS, 0x2810);
467         //~ PCI_CONFIG_8(PCI_REVISION_ID, 0x08);
468         break;
469     case i82559ER:
470         pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82551IT);
471         PCI_CONFIG_16(PCI_STATUS, 0x2810);
472         PCI_CONFIG_8(PCI_REVISION_ID, 0x09);
473         break;
474     //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1029);
475     //~ PCI_CONFIG_16(PCI_DEVICE_ID, 0x1030);       /* 82559 InBusiness 10/100 */
476     default:
477         logout("Device %X is undefined!\n", device);
478     }
479
480     if (device == i82557C || device == i82558B || device == i82559C) {
481         logout("Get device id and revision from EEPROM!!!\n");
482     }
483 }
484
485 static void nic_selective_reset(EEPRO100State * s)
486 {
487     size_t i;
488     uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom);
489     //~ eeprom93xx_reset(s->eeprom);
490     memcpy(eeprom_contents, s->macaddr, 6);
491     eeprom_contents[0xa] = 0x4000;
492     uint16_t sum = 0;
493     for (i = 0; i < EEPROM_SIZE - 1; i++) {
494         sum += eeprom_contents[i];
495     }
496     eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum;
497
498     memset(s->mem, 0, sizeof(s->mem));
499     uint32_t val = BIT(21);
500     memcpy(&s->mem[SCBCtrlMDI], &val, sizeof(val));
501
502     assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default));
503     memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem));
504 }
505
506 static void nic_reset(void *opaque)
507 {
508     EEPRO100State *s = opaque;
509     logout("%p\n", s);
510     static int first;
511     if (!first) {
512         first = 1;
513     }
514     nic_selective_reset(s);
515 }
516
517 #if defined(DEBUG_EEPRO100)
518 static const char *reg[PCI_IO_SIZE / 4] = {
519     "Command/Status",
520     "General Pointer",
521     "Port",
522     "EEPROM/Flash Control",
523     "MDI Control",
524     "Receive DMA Byte Count",
525     "Flow control register",
526     "General Status/Control"
527 };
528
529 static char *regname(uint32_t addr)
530 {
531     static char buf[16];
532     if (addr < PCI_IO_SIZE) {
533         const char *r = reg[addr / 4];
534         if (r != 0) {
535             sprintf(buf, "%s+%u", r, addr % 4);
536         } else {
537             sprintf(buf, "0x%02x", addr);
538         }
539     } else {
540         sprintf(buf, "??? 0x%08x", addr);
541     }
542     return buf;
543 }
544 #endif                          /* DEBUG_EEPRO100 */
545
546 #if 0
547 static uint16_t eepro100_read_status(EEPRO100State * s)
548 {
549     uint16_t val = s->status;
550     logout("val=0x%04x\n", val);
551     return val;
552 }
553
554 static void eepro100_write_status(EEPRO100State * s, uint16_t val)
555 {
556     logout("val=0x%04x\n", val);
557     s->status = val;
558 }
559 #endif
560
561 /*****************************************************************************
562  *
563  * Command emulation.
564  *
565  ****************************************************************************/
566
567 #if 0
568 static uint16_t eepro100_read_command(EEPRO100State * s)
569 {
570     uint16_t val = 0xffff;
571     //~ logout("val=0x%04x\n", val);
572     return val;
573 }
574 #endif
575
576 static bool device_supports_eTxCB(EEPRO100State * s)
577 {
578     return (s->device != i82557B && s->device != i82557C);
579 }
580
581 /* Commands that can be put in a command list entry. */
582 enum commands {
583     CmdNOp = 0,
584     CmdIASetup = 1,
585     CmdConfigure = 2,
586     CmdMulticastList = 3,
587     CmdTx = 4,
588     CmdTDR = 5,                 /* load microcode */
589     CmdDump = 6,
590     CmdDiagnose = 7,
591
592     /* And some extra flags: */
593     CmdSuspend = 0x4000,        /* Suspend after completion. */
594     CmdIntr = 0x2000,           /* Interrupt after completion. */
595     CmdTxFlex = 0x0008,         /* Use "Flexible mode" for CmdTx command. */
596 };
597
598 static cu_state_t get_cu_state(EEPRO100State * s)
599 {
600     return ((s->mem[SCBStatus] >> 6) & 0x03);
601 }
602
603 static void set_cu_state(EEPRO100State * s, cu_state_t state)
604 {
605     s->mem[SCBStatus] = (s->mem[SCBStatus] & 0x3f) + (state << 6);
606 }
607
608 static ru_state_t get_ru_state(EEPRO100State * s)
609 {
610     return ((s->mem[SCBStatus] >> 2) & 0x0f);
611 }
612
613 static void set_ru_state(EEPRO100State * s, ru_state_t state)
614 {
615     s->mem[SCBStatus] = (s->mem[SCBStatus] & 0xc3) + (state << 2);
616 }
617
618 static void dump_statistics(EEPRO100State * s)
619 {
620     /* Dump statistical data. Most data is never changed by the emulation
621      * and always 0, so we first just copy the whole block and then those
622      * values which really matter.
623      * Number of data should check configuration!!!
624      */
625     cpu_physical_memory_write(s->statsaddr, (uint8_t *) & s->statistics, 64);
626     stl_phys(s->statsaddr + 0, s->statistics.tx_good_frames);
627     stl_phys(s->statsaddr + 36, s->statistics.rx_good_frames);
628     stl_phys(s->statsaddr + 48, s->statistics.rx_resource_errors);
629     stl_phys(s->statsaddr + 60, s->statistics.rx_short_frame_errors);
630     //~ stw_phys(s->statsaddr + 76, s->statistics.xmt_tco_frames);
631     //~ stw_phys(s->statsaddr + 78, s->statistics.rcv_tco_frames);
632     //~ missing("CU dump statistical counters");
633 }
634
635 static void eepro100_cu_command(EEPRO100State * s, uint8_t val)
636 {
637     eepro100_tx_t tx;
638     uint32_t cb_address;
639     switch (val) {
640     case CU_NOP:
641         /* No operation. */
642         break;
643     case CU_START:
644         if (get_cu_state(s) != cu_idle) {
645             /* Intel documentation says that CU must be idle for the CU
646              * start command. Intel driver for Linux also starts the CU
647              * from suspended state. */
648             logout("CU state is %u, should be %u\n", get_cu_state(s), cu_idle);
649             //~ assert(!"wrong CU state");
650         }
651         set_cu_state(s, cu_active);
652         s->cu_offset = s->pointer;
653       next_command:
654         cb_address = s->cu_base + s->cu_offset;
655         cpu_physical_memory_read(cb_address, (uint8_t *) & tx, sizeof(tx));
656         uint16_t status = le16_to_cpu(tx.status);
657         uint16_t command = le16_to_cpu(tx.command);
658         logout
659             ("val=0x%02x (cu start), status=0x%04x, command=0x%04x, link=0x%08x\n",
660              val, status, command, tx.link);
661         bool bit_el = ((command & 0x8000) != 0);
662         bool bit_s = ((command & 0x4000) != 0);
663         bool bit_i = ((command & 0x2000) != 0);
664         bool bit_nc = ((command & 0x0010) != 0);
665         //~ bool bit_sf = ((command & 0x0008) != 0);
666         uint16_t cmd = command & 0x0007;
667         s->cu_offset = le32_to_cpu(tx.link);
668         switch (cmd) {
669         case CmdNOp:
670             /* Do nothing. */
671             break;
672         case CmdIASetup:
673             cpu_physical_memory_read(cb_address + 8, &s->macaddr[0], 6);
674             logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
675             break;
676         case CmdConfigure:
677             cpu_physical_memory_read(cb_address + 8, &s->configuration[0],
678                                      sizeof(s->configuration));
679             logout("configuration: %s\n", nic_dump(&s->configuration[0], 16));
680             break;
681         case CmdMulticastList:
682             //~ missing("multicast list");
683             break;
684         case CmdTx:
685             (void)0;
686             uint32_t tbd_array = le32_to_cpu(tx.tx_desc_addr);
687             uint16_t tcb_bytes = (le16_to_cpu(tx.tcb_bytes) & 0x3fff);
688             logout
689                 ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n",
690                  tbd_array, tcb_bytes, tx.tbd_count);
691             assert(!bit_nc);
692             //~ assert(!bit_sf);
693             assert(tcb_bytes <= 2600);
694             /* Next assertion fails for local configuration. */
695             //~ assert((tcb_bytes > 0) || (tbd_array != 0xffffffff));
696             if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) {
697                 logout
698                     ("illegal values of TBD array address and TCB byte count!\n");
699             }
700             // sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes
701             uint8_t buf[2600];
702             uint16_t size = 0;
703             uint32_t tbd_address = cb_address + 0x10;
704             assert(tcb_bytes <= sizeof(buf));
705             while (size < tcb_bytes) {
706                 uint32_t tx_buffer_address = ldl_phys(tbd_address);
707                 uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
708                 //~ uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
709                 tbd_address += 8;
710                 logout
711                     ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n",
712                      tx_buffer_address, tx_buffer_size);
713                 tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
714                 cpu_physical_memory_read(tx_buffer_address, &buf[size],
715                                          tx_buffer_size);
716                 size += tx_buffer_size;
717             }
718             if (tbd_array == 0xffffffff) {
719                 /* Simplified mode. Was already handled by code above. */
720             } else {
721                 /* Flexible mode. */
722                 uint8_t tbd_count = 0;
723                 if (device_supports_eTxCB(s) && !(s->configuration[6] & BIT(4))) {
724                     /* Extended Flexible TCB. */
725                     assert(tcb_bytes == 0);
726                     for (; tbd_count < 2; tbd_count++) {
727                         uint32_t tx_buffer_address = ldl_phys(tbd_address);
728                         uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
729                         uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
730                         tbd_address += 8;
731                         logout
732                             ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n",
733                              tx_buffer_address, tx_buffer_size);
734                         tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
735                         cpu_physical_memory_read(tx_buffer_address, &buf[size],
736                                                  tx_buffer_size);
737                         size += tx_buffer_size;
738                         if (tx_buffer_el & 1) {
739                             break;
740                         }
741                     }
742                 }
743                 tbd_address = tbd_array;
744                 for (; tbd_count < tx.tbd_count; tbd_count++) {
745                     uint32_t tx_buffer_address = ldl_phys(tbd_address);
746                     uint16_t tx_buffer_size = lduw_phys(tbd_address + 4);
747                     uint16_t tx_buffer_el = lduw_phys(tbd_address + 6);
748                     tbd_address += 8;
749                     logout
750                         ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n",
751                          tx_buffer_address, tx_buffer_size);
752                     tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size);
753                     cpu_physical_memory_read(tx_buffer_address, &buf[size],
754                                              tx_buffer_size);
755                     size += tx_buffer_size;
756                     if (tx_buffer_el & 1) {
757                         break;
758                     }
759                 }
760             }
761             qemu_send_packet(s->vc, buf, size);
762             s->statistics.tx_good_frames++;
763             /* Transmit with bad status would raise an CX/TNO interrupt.
764              * (82557 only). Emulation never has bad status. */
765             //~ eepro100_cx_interrupt(s);
766             break;
767         case CmdTDR:
768             logout("load microcode\n");
769             /* Starting with offset 8, the command contains
770              * 64 dwords microcode which we just ignore here. */
771             break;
772         default:
773             missing("undefined command");
774         }
775         /* Write new status (success). */
776         stw_phys(cb_address, status | 0x8000 | 0x2000);
777         if (bit_i) {
778             /* CU completed action. */
779             eepro100_cx_interrupt(s);
780         }
781         if (bit_el) {
782             /* CU becomes idle. */
783             set_cu_state(s, cu_idle);
784             eepro100_cna_interrupt(s);
785         } else if (bit_s) {
786             /* CU becomes suspended. */
787             set_cu_state(s, cu_suspended);
788             eepro100_cna_interrupt(s);
789         } else {
790             /* More entries in list. */
791             logout("CU list with at least one more entry\n");
792             goto next_command;
793         }
794         logout("CU list empty\n");
795         /* List is empty. Now CU is idle or suspended. */
796         break;
797     case CU_RESUME:
798         if (get_cu_state(s) != cu_suspended) {
799             logout("bad CU resume from CU state %u\n", get_cu_state(s));
800             /* Workaround for bad Linux eepro100 driver which resumes
801              * from idle state. */
802             //~ missing("cu resume");
803             set_cu_state(s, cu_suspended);
804         }
805         if (get_cu_state(s) == cu_suspended) {
806             logout("CU resuming\n");
807             set_cu_state(s, cu_active);
808             goto next_command;
809         }
810         break;
811     case CU_STATSADDR:
812         /* Load dump counters address. */
813         s->statsaddr = s->pointer;
814         logout("val=0x%02x (status address)\n", val);
815         break;
816     case CU_SHOWSTATS:
817         /* Dump statistical counters. */
818         dump_statistics(s);
819         break;
820     case CU_CMD_BASE:
821         /* Load CU base. */
822         logout("val=0x%02x (CU base address)\n", val);
823         s->cu_base = s->pointer;
824         break;
825     case CU_DUMPSTATS:
826         /* Dump and reset statistical counters. */
827         dump_statistics(s);
828         memset(&s->statistics, 0, sizeof(s->statistics));
829         break;
830     case CU_SRESUME:
831         /* CU static resume. */
832         missing("CU static resume");
833         break;
834     default:
835         missing("Undefined CU command");
836     }
837 }
838
839 static void eepro100_ru_command(EEPRO100State * s, uint8_t val)
840 {
841     switch (val) {
842     case RU_NOP:
843         /* No operation. */
844         break;
845     case RX_START:
846         /* RU start. */
847         if (get_ru_state(s) != ru_idle) {
848             logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle);
849             //~ assert(!"wrong RU state");
850         }
851         set_ru_state(s, ru_ready);
852         s->ru_offset = s->pointer;
853         logout("val=0x%02x (rx start)\n", val);
854         break;
855     case RX_RESUME:
856         /* Restart RU. */
857         if (get_ru_state(s) != ru_suspended) {
858             logout("RU state is %u, should be %u\n", get_ru_state(s),
859                    ru_suspended);
860             //~ assert(!"wrong RU state");
861         }
862         set_ru_state(s, ru_ready);
863         break;
864     case RX_ADDR_LOAD:
865         /* Load RU base. */
866         logout("val=0x%02x (RU base address)\n", val);
867         s->ru_base = s->pointer;
868         break;
869     default:
870         logout("val=0x%02x (undefined RU command)\n", val);
871         missing("Undefined SU command");
872     }
873 }
874
875 static void eepro100_write_command(EEPRO100State * s, uint8_t val)
876 {
877     eepro100_ru_command(s, val & 0x0f);
878     eepro100_cu_command(s, val & 0xf0);
879     if ((val) == 0) {
880         logout("val=0x%02x\n", val);
881     }
882     /* Clear command byte after command was accepted. */
883     s->mem[SCBCmd] = 0;
884 }
885
886 /*****************************************************************************
887  *
888  * EEPROM emulation.
889  *
890  ****************************************************************************/
891
892 #define EEPROM_CS       0x02
893 #define EEPROM_SK       0x01
894 #define EEPROM_DI       0x04
895 #define EEPROM_DO       0x08
896
897 static uint16_t eepro100_read_eeprom(EEPRO100State * s)
898 {
899     uint16_t val;
900     memcpy(&val, &s->mem[SCBeeprom], sizeof(val));
901     if (eeprom93xx_read(s->eeprom)) {
902         val |= EEPROM_DO;
903     } else {
904         val &= ~EEPROM_DO;
905     }
906     return val;
907 }
908
909 static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val)
910 {
911     logout("write val=0x%02x\n", val);
912
913     /* mask unwriteable bits */
914     //~ val = SET_MASKED(val, 0x31, eeprom->value);
915
916     int eecs = ((val & EEPROM_CS) != 0);
917     int eesk = ((val & EEPROM_SK) != 0);
918     int eedi = ((val & EEPROM_DI) != 0);
919     eeprom93xx_write(eeprom, eecs, eesk, eedi);
920 }
921
922 static void eepro100_write_pointer(EEPRO100State * s, uint32_t val)
923 {
924     s->pointer = le32_to_cpu(val);
925     logout("val=0x%08x\n", val);
926 }
927
928 /*****************************************************************************
929  *
930  * MDI emulation.
931  *
932  ****************************************************************************/
933
934 #if defined(DEBUG_EEPRO100)
935 static const char *mdi_op_name[] = {
936     "opcode 0",
937     "write",
938     "read",
939     "opcode 3"
940 };
941
942 static const char *mdi_reg_name[] = {
943     "Control",
944     "Status",
945     "PHY Identification (Word 1)",
946     "PHY Identification (Word 2)",
947     "Auto-Negotiation Advertisement",
948     "Auto-Negotiation Link Partner Ability",
949     "Auto-Negotiation Expansion"
950 };
951 #endif                          /* DEBUG_EEPRO100 */
952
953 static uint32_t eepro100_read_mdi(EEPRO100State * s)
954 {
955     uint32_t val;
956     memcpy(&val, &s->mem[0x10], sizeof(val));
957
958 #ifdef DEBUG_EEPRO100
959     uint8_t raiseint = (val & BIT(29)) >> 29;
960     uint8_t opcode = (val & BITS(27, 26)) >> 26;
961     uint8_t phy = (val & BITS(25, 21)) >> 21;
962     uint8_t reg = (val & BITS(20, 16)) >> 16;
963     uint16_t data = (val & BITS(15, 0));
964 #endif
965     /* Emulation takes no time to finish MDI transaction. */
966     val |= BIT(28);
967     TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
968                       val, raiseint, mdi_op_name[opcode], phy,
969                       mdi_reg_name[reg], data));
970     return val;
971 }
972
973 //~ #define BITS(val, upper, lower) (val & ???)
974 static void eepro100_write_mdi(EEPRO100State * s, uint32_t val)
975 {
976     uint8_t raiseint = (val & BIT(29)) >> 29;
977     uint8_t opcode = (val & BITS(27, 26)) >> 26;
978     uint8_t phy = (val & BITS(25, 21)) >> 21;
979     uint8_t reg = (val & BITS(20, 16)) >> 16;
980     uint16_t data = (val & BITS(15, 0));
981     if (phy != 1) {
982         /* Unsupported PHY address. */
983         //~ logout("phy must be 1 but is %u\n", phy);
984         data = 0;
985     } else if (opcode != 1 && opcode != 2) {
986         /* Unsupported opcode. */
987         logout("opcode must be 1 or 2 but is %u\n", opcode);
988         data = 0;
989     } else if (reg > 6) {
990         /* Unsupported register. */
991         logout("register must be 0...6 but is %u\n", reg);
992         data = 0;
993     } else {
994         TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n",
995                           val, raiseint, mdi_op_name[opcode], phy,
996                           mdi_reg_name[reg], data));
997         if (opcode == 1) {
998             /* MDI write */
999             switch (reg) {
1000             case 0:            /* Control Register */
1001                 if (data & 0x8000) {
1002                     /* Reset status and control registers to default. */
1003                     s->mdimem[0] = eepro100_mdi_default[0];
1004                     s->mdimem[1] = eepro100_mdi_default[1];
1005                     data = s->mdimem[reg];
1006                 } else {
1007                     /* Restart Auto Configuration = Normal Operation */
1008                     data &= ~0x0200;
1009                 }
1010                 break;
1011             case 1:            /* Status Register */
1012                 missing("not writable");
1013                 data = s->mdimem[reg];
1014                 break;
1015             case 2:            /* PHY Identification Register (Word 1) */
1016             case 3:            /* PHY Identification Register (Word 2) */
1017                 missing("not implemented");
1018                 break;
1019             case 4:            /* Auto-Negotiation Advertisement Register */
1020             case 5:            /* Auto-Negotiation Link Partner Ability Register */
1021                 break;
1022             case 6:            /* Auto-Negotiation Expansion Register */
1023             default:
1024                 missing("not implemented");
1025             }
1026             s->mdimem[reg] = data;
1027         } else if (opcode == 2) {
1028             /* MDI read */
1029             switch (reg) {
1030             case 0:            /* Control Register */
1031                 if (data & 0x8000) {
1032                     /* Reset status and control registers to default. */
1033                     s->mdimem[0] = eepro100_mdi_default[0];
1034                     s->mdimem[1] = eepro100_mdi_default[1];
1035                 }
1036                 break;
1037             case 1:            /* Status Register */
1038                 s->mdimem[reg] |= 0x0020;
1039                 break;
1040             case 2:            /* PHY Identification Register (Word 1) */
1041             case 3:            /* PHY Identification Register (Word 2) */
1042             case 4:            /* Auto-Negotiation Advertisement Register */
1043                 break;
1044             case 5:            /* Auto-Negotiation Link Partner Ability Register */
1045                 s->mdimem[reg] = 0x41fe;
1046                 break;
1047             case 6:            /* Auto-Negotiation Expansion Register */
1048                 s->mdimem[reg] = 0x0001;
1049                 break;
1050             }
1051             data = s->mdimem[reg];
1052         }
1053         /* Emulation takes no time to finish MDI transaction.
1054          * Set MDI bit in SCB status register. */
1055         s->mem[SCBAck] |= 0x08;
1056         val |= BIT(28);
1057         if (raiseint) {
1058             eepro100_mdi_interrupt(s);
1059         }
1060     }
1061     val = (val & 0xffff0000) + data;
1062     memcpy(&s->mem[0x10], &val, sizeof(val));
1063 }
1064
1065 /*****************************************************************************
1066  *
1067  * Port emulation.
1068  *
1069  ****************************************************************************/
1070
1071 #define PORT_SOFTWARE_RESET     0
1072 #define PORT_SELFTEST           1
1073 #define PORT_SELECTIVE_RESET    2
1074 #define PORT_DUMP               3
1075 #define PORT_SELECTION_MASK     3
1076
1077 typedef struct {
1078     uint32_t st_sign;           /* Self Test Signature */
1079     uint32_t st_result;         /* Self Test Results */
1080 } eepro100_selftest_t;
1081
1082 static uint32_t eepro100_read_port(EEPRO100State * s)
1083 {
1084     return 0;
1085 }
1086
1087 static void eepro100_write_port(EEPRO100State * s, uint32_t val)
1088 {
1089     val = le32_to_cpu(val);
1090     uint32_t address = (val & ~PORT_SELECTION_MASK);
1091     uint8_t selection = (val & PORT_SELECTION_MASK);
1092     switch (selection) {
1093     case PORT_SOFTWARE_RESET:
1094         nic_reset(s);
1095         break;
1096     case PORT_SELFTEST:
1097         logout("selftest address=0x%08x\n", address);
1098         eepro100_selftest_t data;
1099         cpu_physical_memory_read(address, (uint8_t *) & data, sizeof(data));
1100         data.st_sign = 0xffffffff;
1101         data.st_result = 0;
1102         cpu_physical_memory_write(address, (uint8_t *) & data, sizeof(data));
1103         break;
1104     case PORT_SELECTIVE_RESET:
1105         logout("selective reset, selftest address=0x%08x\n", address);
1106         nic_selective_reset(s);
1107         break;
1108     default:
1109         logout("val=0x%08x\n", val);
1110         missing("unknown port selection");
1111     }
1112 }
1113
1114 /*****************************************************************************
1115  *
1116  * General hardware emulation.
1117  *
1118  ****************************************************************************/
1119
1120 static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr)
1121 {
1122     uint8_t val;
1123     if (addr <= sizeof(s->mem) - sizeof(val)) {
1124         memcpy(&val, &s->mem[addr], sizeof(val));
1125     }
1126
1127     switch (addr) {
1128     case SCBStatus:
1129         //~ val = eepro100_read_status(s);
1130         logout("addr=%s val=0x%02x\n", regname(addr), val);
1131         break;
1132     case SCBAck:
1133         //~ val = eepro100_read_status(s);
1134         logout("addr=%s val=0x%02x\n", regname(addr), val);
1135         break;
1136     case SCBCmd:
1137         logout("addr=%s val=0x%02x\n", regname(addr), val);
1138         //~ val = eepro100_read_command(s);
1139         break;
1140     case SCBIntmask:
1141         logout("addr=%s val=0x%02x\n", regname(addr), val);
1142         break;
1143     case SCBPort + 3:
1144         logout("addr=%s val=0x%02x\n", regname(addr), val);
1145         break;
1146     case SCBeeprom:
1147         val = eepro100_read_eeprom(s);
1148         break;
1149     case 0x1b:                 /* PMDR (power management driver register) */
1150         val = 0;
1151         logout("addr=%s val=0x%02x\n", regname(addr), val);
1152         break;
1153     case 0x1d:                 /* general status register */
1154         /* 100 Mbps full duplex, valid link */
1155         val = 0x07;
1156         logout("addr=General Status val=%02x\n", val);
1157         break;
1158     default:
1159         logout("addr=%s val=0x%02x\n", regname(addr), val);
1160         missing("unknown byte read");
1161     }
1162     return val;
1163 }
1164
1165 static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr)
1166 {
1167     uint16_t val;
1168     if (addr <= sizeof(s->mem) - sizeof(val)) {
1169         memcpy(&val, &s->mem[addr], sizeof(val));
1170     }
1171
1172     logout("addr=%s val=0x%04x\n", regname(addr), val);
1173
1174     switch (addr) {
1175     case SCBStatus:
1176         //~ val = eepro100_read_status(s);
1177         break;
1178     case SCBeeprom:
1179         val = eepro100_read_eeprom(s);
1180         break;
1181     default:
1182         logout("addr=%s val=0x%04x\n", regname(addr), val);
1183         missing("unknown word read");
1184     }
1185     return val;
1186 }
1187
1188 static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr)
1189 {
1190     uint32_t val;
1191     if (addr <= sizeof(s->mem) - sizeof(val)) {
1192         memcpy(&val, &s->mem[addr], sizeof(val));
1193     }
1194
1195     switch (addr) {
1196     case SCBStatus:
1197         //~ val = eepro100_read_status(s);
1198         logout("addr=%s val=0x%08x\n", regname(addr), val);
1199         break;
1200     case SCBPointer:
1201         //~ val = eepro100_read_pointer(s);
1202         logout("addr=%s val=0x%08x\n", regname(addr), val);
1203         break;
1204     case SCBPort:
1205         val = eepro100_read_port(s);
1206         logout("addr=%s val=0x%08x\n", regname(addr), val);
1207         break;
1208     case SCBCtrlMDI:
1209         val = eepro100_read_mdi(s);
1210         break;
1211     default:
1212         logout("addr=%s val=0x%08x\n", regname(addr), val);
1213         missing("unknown longword read");
1214     }
1215     return val;
1216 }
1217
1218 static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val)
1219 {
1220     if (addr <= sizeof(s->mem) - sizeof(val)) {
1221         memcpy(&s->mem[addr], &val, sizeof(val));
1222     }
1223
1224     logout("addr=%s val=0x%02x\n", regname(addr), val);
1225
1226     switch (addr) {
1227     case SCBStatus:
1228         //~ eepro100_write_status(s, val);
1229         break;
1230     case SCBAck:
1231         eepro100_acknowledge(s);
1232         break;
1233     case SCBCmd:
1234         eepro100_write_command(s, val);
1235         break;
1236     case SCBIntmask:
1237         if (val & BIT(1)) {
1238             eepro100_swi_interrupt(s);
1239         }
1240         eepro100_interrupt(s, 0);
1241         break;
1242     case SCBPort + 3:
1243     case SCBFlow:
1244     case SCBFlow + 1:
1245     case SCBFlow + 2:
1246     case SCBFlow + 3:
1247         logout("addr=%s val=0x%02x\n", regname(addr), val);
1248         break;
1249     case SCBeeprom:
1250         eepro100_write_eeprom(s->eeprom, val);
1251         break;
1252     default:
1253         logout("addr=%s val=0x%02x\n", regname(addr), val);
1254         missing("unknown byte write");
1255     }
1256 }
1257
1258 static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val)
1259 {
1260     if (addr <= sizeof(s->mem) - sizeof(val)) {
1261         memcpy(&s->mem[addr], &val, sizeof(val));
1262     }
1263
1264     logout("addr=%s val=0x%04x\n", regname(addr), val);
1265
1266     switch (addr) {
1267     case SCBStatus:
1268         //~ eepro100_write_status(s, val);
1269         eepro100_acknowledge(s);
1270         break;
1271     case SCBCmd:
1272         eepro100_write_command(s, val);
1273         eepro100_write1(s, SCBIntmask, val >> 8);
1274         break;
1275     case SCBeeprom:
1276         eepro100_write_eeprom(s->eeprom, val);
1277         break;
1278     default:
1279         logout("addr=%s val=0x%04x\n", regname(addr), val);
1280         missing("unknown word write");
1281     }
1282 }
1283
1284 static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val)
1285 {
1286     if (addr <= sizeof(s->mem) - sizeof(val)) {
1287         memcpy(&s->mem[addr], &val, sizeof(val));
1288     }
1289
1290     switch (addr) {
1291     case SCBPointer:
1292         eepro100_write_pointer(s, val);
1293         break;
1294     case SCBPort:
1295         logout("addr=%s val=0x%08x\n", regname(addr), val);
1296         eepro100_write_port(s, val);
1297         break;
1298     case SCBCtrlMDI:
1299         eepro100_write_mdi(s, val);
1300         break;
1301     default:
1302         logout("addr=%s val=0x%08x\n", regname(addr), val);
1303         missing("unknown longword write");
1304     }
1305 }
1306
1307 static uint32_t ioport_read1(void *opaque, uint32_t addr)
1308 {
1309     EEPRO100State *s = opaque;
1310     //~ logout("addr=%s\n", regname(addr));
1311     return eepro100_read1(s, addr - s->region[1]);
1312 }
1313
1314 static uint32_t ioport_read2(void *opaque, uint32_t addr)
1315 {
1316     EEPRO100State *s = opaque;
1317     return eepro100_read2(s, addr - s->region[1]);
1318 }
1319
1320 static uint32_t ioport_read4(void *opaque, uint32_t addr)
1321 {
1322     EEPRO100State *s = opaque;
1323     return eepro100_read4(s, addr - s->region[1]);
1324 }
1325
1326 static void ioport_write1(void *opaque, uint32_t addr, uint32_t val)
1327 {
1328     EEPRO100State *s = opaque;
1329     //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1330     eepro100_write1(s, addr - s->region[1], val);
1331 }
1332
1333 static void ioport_write2(void *opaque, uint32_t addr, uint32_t val)
1334 {
1335     EEPRO100State *s = opaque;
1336     eepro100_write2(s, addr - s->region[1], val);
1337 }
1338
1339 static void ioport_write4(void *opaque, uint32_t addr, uint32_t val)
1340 {
1341     EEPRO100State *s = opaque;
1342     eepro100_write4(s, addr - s->region[1], val);
1343 }
1344
1345 /***********************************************************/
1346 /* PCI EEPRO100 definitions */
1347
1348 static void pci_map(PCIDevice * pci_dev, int region_num,
1349                     uint32_t addr, uint32_t size, int type)
1350 {
1351     EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1352
1353     logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1354            region_num, addr, size, type);
1355
1356     assert(region_num == 1);
1357     register_ioport_write(addr, size, 1, ioport_write1, s);
1358     register_ioport_read(addr, size, 1, ioport_read1, s);
1359     register_ioport_write(addr, size, 2, ioport_write2, s);
1360     register_ioport_read(addr, size, 2, ioport_read2, s);
1361     register_ioport_write(addr, size, 4, ioport_write4, s);
1362     register_ioport_read(addr, size, 4, ioport_read4, s);
1363
1364     s->region[region_num] = addr;
1365 }
1366
1367 static void pci_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
1368 {
1369     EEPRO100State *s = opaque;
1370     //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1371     eepro100_write1(s, addr, val);
1372 }
1373
1374 static void pci_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
1375 {
1376     EEPRO100State *s = opaque;
1377     //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1378     eepro100_write2(s, addr, val);
1379 }
1380
1381 static void pci_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
1382 {
1383     EEPRO100State *s = opaque;
1384     //~ logout("addr=%s val=0x%02x\n", regname(addr), val);
1385     eepro100_write4(s, addr, val);
1386 }
1387
1388 static uint32_t pci_mmio_readb(void *opaque, target_phys_addr_t addr)
1389 {
1390     EEPRO100State *s = opaque;
1391     //~ logout("addr=%s\n", regname(addr));
1392     return eepro100_read1(s, addr);
1393 }
1394
1395 static uint32_t pci_mmio_readw(void *opaque, target_phys_addr_t addr)
1396 {
1397     EEPRO100State *s = opaque;
1398     //~ logout("addr=%s\n", regname(addr));
1399     return eepro100_read2(s, addr);
1400 }
1401
1402 static uint32_t pci_mmio_readl(void *opaque, target_phys_addr_t addr)
1403 {
1404     EEPRO100State *s = opaque;
1405     //~ logout("addr=%s\n", regname(addr));
1406     return eepro100_read4(s, addr);
1407 }
1408
1409 static CPUWriteMemoryFunc * const pci_mmio_write[] = {
1410     pci_mmio_writeb,
1411     pci_mmio_writew,
1412     pci_mmio_writel
1413 };
1414
1415 static CPUReadMemoryFunc * const pci_mmio_read[] = {
1416     pci_mmio_readb,
1417     pci_mmio_readw,
1418     pci_mmio_readl
1419 };
1420
1421 static void pci_mmio_map(PCIDevice * pci_dev, int region_num,
1422                          uint32_t addr, uint32_t size, int type)
1423 {
1424     EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1425
1426     logout("region %d, addr=0x%08x, size=0x%08x, type=%d\n",
1427            region_num, addr, size, type);
1428
1429     if (region_num == 0) {
1430         /* Map control / status registers. */
1431         cpu_register_physical_memory(addr, size, s->mmio_index);
1432         s->region[region_num] = addr;
1433     }
1434 }
1435
1436 static int nic_can_receive(VLANClientState *vc)
1437 {
1438     EEPRO100State *s = vc->opaque;
1439     logout("%p\n", s);
1440     return get_ru_state(s) == ru_ready;
1441     //~ return !eepro100_buffer_full(s);
1442 }
1443
1444 static ssize_t nic_receive(VLANClientState *vc, const uint8_t * buf, size_t size)
1445 {
1446     /* TODO:
1447      * - Magic packets should set bit 30 in power management driver register.
1448      * - Interesting packets should set bit 29 in power management driver register.
1449      */
1450     EEPRO100State *s = vc->opaque;
1451     uint16_t rfd_status = 0xa000;
1452     static const uint8_t broadcast_macaddr[6] =
1453         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1454
1455     /* TODO: check multiple IA bit. */
1456     assert(!(s->configuration[20] & BIT(6)));
1457
1458     if (s->configuration[8] & 0x80) {
1459         /* CSMA is disabled. */
1460         logout("%p received while CSMA is disabled\n", s);
1461         return -1;
1462     } else if (size < 64 && (s->configuration[7] & 1)) {
1463         /* Short frame and configuration byte 7/0 (discard short receive) set:
1464          * Short frame is discarded */
1465         logout("%p received short frame (%d byte)\n", s, size);
1466         s->statistics.rx_short_frame_errors++;
1467         //~ return -1;
1468     } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & 8)) {
1469         /* Long frame and configuration byte 18/3 (long receive ok) not set:
1470          * Long frames are discarded. */
1471         logout("%p received long frame (%d byte), ignored\n", s, size);
1472         return -1;
1473     } else if (memcmp(buf, s->macaddr, 6) == 0) {       // !!!
1474         /* Frame matches individual address. */
1475         /* TODO: check configuration byte 15/4 (ignore U/L). */
1476         logout("%p received frame for me, len=%d\n", s, size);
1477     } else if (memcmp(buf, broadcast_macaddr, 6) == 0) {
1478         /* Broadcast frame. */
1479         logout("%p received broadcast, len=%d\n", s, size);
1480         rfd_status |= 0x0002;
1481     } else if (buf[0] & 0x01) { // !!!
1482         /* Multicast frame. */
1483         logout("%p received multicast, len=%d\n", s, size);
1484         /* TODO: check multicast all bit. */
1485         assert(!(s->configuration[21] & BIT(3)));
1486         int mcast_idx = compute_mcast_idx(buf);
1487         if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7)))) {
1488             return size;
1489         }
1490         rfd_status |= 0x0002;
1491     } else if (s->configuration[15] & 1) {
1492         /* Promiscuous: receive all. */
1493         logout("%p received frame in promiscuous mode, len=%d\n", s, size);
1494         rfd_status |= 0x0004;
1495     } else {
1496         logout("%p received frame, ignored, len=%d,%s\n", s, size,
1497                nic_dump(buf, size));
1498         return size;
1499     }
1500
1501     if (get_ru_state(s) != ru_ready) {
1502         /* No ressources available. */
1503         logout("no ressources, state=%u\n", get_ru_state(s));
1504         s->statistics.rx_resource_errors++;
1505         //~ assert(!"no ressources");
1506         return -1;
1507     }
1508     //~ !!!
1509 //~ $3 = {status = 0x0, command = 0xc000, link = 0x2d220, rx_buf_addr = 0x207dc, count = 0x0, size = 0x5f8, packet = {0x0 <repeats 1518 times>}}
1510     eepro100_rx_t rx;
1511     cpu_physical_memory_read(s->ru_base + s->ru_offset, (uint8_t *) & rx,
1512                              offsetof(eepro100_rx_t, packet));
1513     uint16_t rfd_command = le16_to_cpu(rx.command);
1514     uint16_t rfd_size = le16_to_cpu(rx.size);
1515     assert(size <= rfd_size);
1516     if (size < 64) {
1517         rfd_status |= 0x0080;
1518     }
1519     logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n", rfd_command,
1520            rx.link, rx.rx_buf_addr, rfd_size);
1521     stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, status),
1522              rfd_status);
1523     stw_phys(s->ru_base + s->ru_offset + offsetof(eepro100_rx_t, count), size);
1524     /* Early receive interrupt not supported. */
1525     //~ eepro100_er_interrupt(s);
1526     /* Receive CRC Transfer not supported. */
1527     assert(!(s->configuration[18] & 4));
1528     /* TODO: check stripping enable bit. */
1529     //~ assert(!(s->configuration[17] & 1));
1530     cpu_physical_memory_write(s->ru_base + s->ru_offset +
1531                               offsetof(eepro100_rx_t, packet), buf, size);
1532     s->statistics.rx_good_frames++;
1533     eepro100_fr_interrupt(s);
1534     s->ru_offset = le32_to_cpu(rx.link);
1535     if (rfd_command & 0x8000) {
1536         /* EL bit is set, so this was the last frame. */
1537         assert(0);
1538     }
1539     if (rfd_command & 0x4000) {
1540         /* S bit is set. */
1541         set_ru_state(s, ru_suspended);
1542     }
1543     return size;
1544 }
1545
1546 static int nic_load(QEMUFile * f, void *opaque, int version_id)
1547 {
1548     EEPRO100State *s = opaque;
1549     int i;
1550     int ret;
1551
1552     if (version_id > 3)
1553         return -EINVAL;
1554
1555     if (version_id >= 3) {
1556         ret = pci_device_load(&s->dev, f);
1557         if (ret < 0)
1558             return ret;
1559     }
1560
1561     if (version_id >= 2) {
1562         qemu_get_8s(f, &s->rxcr);
1563     } else {
1564         s->rxcr = 0x0c;
1565     }
1566
1567     qemu_get_8s(f, &s->cmd);
1568     qemu_get_be32s(f, &s->start);
1569     qemu_get_be32s(f, &s->stop);
1570     qemu_get_8s(f, &s->boundary);
1571     qemu_get_8s(f, &s->tsr);
1572     qemu_get_8s(f, &s->tpsr);
1573     qemu_get_be16s(f, &s->tcnt);
1574     qemu_get_be16s(f, &s->rcnt);
1575     qemu_get_be32s(f, &s->rsar);
1576     qemu_get_8s(f, &s->rsr);
1577     qemu_get_8s(f, &s->isr);
1578     qemu_get_8s(f, &s->dcfg);
1579     qemu_get_8s(f, &s->imr);
1580     qemu_get_buffer(f, s->phys, 6);
1581     qemu_get_8s(f, &s->curpag);
1582     qemu_get_buffer(f, s->mult, 8);
1583     qemu_get_buffer(f, s->mem, sizeof(s->mem));
1584
1585     /* Restore all members of struct between scv_stat and mem */
1586     qemu_get_8s(f, &s->scb_stat);
1587     qemu_get_8s(f, &s->int_stat);
1588     for (i = 0; i < 3; i++)
1589         qemu_get_be32s(f, &s->region[i]);
1590     qemu_get_buffer(f, s->macaddr, 6);
1591     for (i = 0; i < 19; i++)
1592         qemu_get_be32s(f, &s->statcounter[i]);
1593     for (i = 0; i < 32; i++)
1594         qemu_get_be16s(f, &s->mdimem[i]);
1595     /* The eeprom should be saved and restored by its own routines */
1596     qemu_get_be32s(f, &s->device);
1597     qemu_get_be32s(f, &s->pointer);
1598     qemu_get_be32s(f, &s->cu_base);
1599     qemu_get_be32s(f, &s->cu_offset);
1600     qemu_get_be32s(f, &s->ru_base);
1601     qemu_get_be32s(f, &s->ru_offset);
1602     qemu_get_be32s(f, &s->statsaddr);
1603     /* Restore epro100_stats_t statistics */
1604     qemu_get_be32s(f, &s->statistics.tx_good_frames);
1605     qemu_get_be32s(f, &s->statistics.tx_max_collisions);
1606     qemu_get_be32s(f, &s->statistics.tx_late_collisions);
1607     qemu_get_be32s(f, &s->statistics.tx_underruns);
1608     qemu_get_be32s(f, &s->statistics.tx_lost_crs);
1609     qemu_get_be32s(f, &s->statistics.tx_deferred);
1610     qemu_get_be32s(f, &s->statistics.tx_single_collisions);
1611     qemu_get_be32s(f, &s->statistics.tx_multiple_collisions);
1612     qemu_get_be32s(f, &s->statistics.tx_total_collisions);
1613     qemu_get_be32s(f, &s->statistics.rx_good_frames);
1614     qemu_get_be32s(f, &s->statistics.rx_crc_errors);
1615     qemu_get_be32s(f, &s->statistics.rx_alignment_errors);
1616     qemu_get_be32s(f, &s->statistics.rx_resource_errors);
1617     qemu_get_be32s(f, &s->statistics.rx_overrun_errors);
1618     qemu_get_be32s(f, &s->statistics.rx_cdt_errors);
1619     qemu_get_be32s(f, &s->statistics.rx_short_frame_errors);
1620     qemu_get_be32s(f, &s->statistics.fc_xmt_pause);
1621     qemu_get_be32s(f, &s->statistics.fc_rcv_pause);
1622     qemu_get_be32s(f, &s->statistics.fc_rcv_unsupported);
1623     qemu_get_be16s(f, &s->statistics.xmt_tco_frames);
1624     qemu_get_be16s(f, &s->statistics.rcv_tco_frames);
1625     qemu_get_be32s(f, &s->statistics.complete);
1626 #if 0
1627     qemu_get_be16s(f, &s->status);
1628 #endif
1629
1630     /* Configuration bytes. */
1631     qemu_get_buffer(f, s->configuration, sizeof(s->configuration));
1632
1633     return 0;
1634 }
1635
1636 static void nic_save(QEMUFile * f, void *opaque)
1637 {
1638     EEPRO100State *s = opaque;
1639     int i;
1640
1641     pci_device_save(&s->dev, f);
1642
1643     qemu_put_8s(f, &s->rxcr);
1644
1645     qemu_put_8s(f, &s->cmd);
1646     qemu_put_be32s(f, &s->start);
1647     qemu_put_be32s(f, &s->stop);
1648     qemu_put_8s(f, &s->boundary);
1649     qemu_put_8s(f, &s->tsr);
1650     qemu_put_8s(f, &s->tpsr);
1651     qemu_put_be16s(f, &s->tcnt);
1652     qemu_put_be16s(f, &s->rcnt);
1653     qemu_put_be32s(f, &s->rsar);
1654     qemu_put_8s(f, &s->rsr);
1655     qemu_put_8s(f, &s->isr);
1656     qemu_put_8s(f, &s->dcfg);
1657     qemu_put_8s(f, &s->imr);
1658     qemu_put_buffer(f, s->phys, 6);
1659     qemu_put_8s(f, &s->curpag);
1660     qemu_put_buffer(f, s->mult, 8);
1661     qemu_put_buffer(f, s->mem, sizeof(s->mem));
1662
1663     /* Save all members of struct between scv_stat and mem */
1664     qemu_put_8s(f, &s->scb_stat);
1665     qemu_put_8s(f, &s->int_stat);
1666     for (i = 0; i < 3; i++)
1667         qemu_put_be32s(f, &s->region[i]);
1668     qemu_put_buffer(f, s->macaddr, 6);
1669     for (i = 0; i < 19; i++)
1670         qemu_put_be32s(f, &s->statcounter[i]);
1671     for (i = 0; i < 32; i++)
1672         qemu_put_be16s(f, &s->mdimem[i]);
1673     /* The eeprom should be saved and restored by its own routines */
1674     qemu_put_be32s(f, &s->device);
1675     qemu_put_be32s(f, &s->pointer);
1676     qemu_put_be32s(f, &s->cu_base);
1677     qemu_put_be32s(f, &s->cu_offset);
1678     qemu_put_be32s(f, &s->ru_base);
1679     qemu_put_be32s(f, &s->ru_offset);
1680     qemu_put_be32s(f, &s->statsaddr);
1681     /* Save epro100_stats_t statistics */
1682     qemu_put_be32s(f, &s->statistics.tx_good_frames);
1683     qemu_put_be32s(f, &s->statistics.tx_max_collisions);
1684     qemu_put_be32s(f, &s->statistics.tx_late_collisions);
1685     qemu_put_be32s(f, &s->statistics.tx_underruns);
1686     qemu_put_be32s(f, &s->statistics.tx_lost_crs);
1687     qemu_put_be32s(f, &s->statistics.tx_deferred);
1688     qemu_put_be32s(f, &s->statistics.tx_single_collisions);
1689     qemu_put_be32s(f, &s->statistics.tx_multiple_collisions);
1690     qemu_put_be32s(f, &s->statistics.tx_total_collisions);
1691     qemu_put_be32s(f, &s->statistics.rx_good_frames);
1692     qemu_put_be32s(f, &s->statistics.rx_crc_errors);
1693     qemu_put_be32s(f, &s->statistics.rx_alignment_errors);
1694     qemu_put_be32s(f, &s->statistics.rx_resource_errors);
1695     qemu_put_be32s(f, &s->statistics.rx_overrun_errors);
1696     qemu_put_be32s(f, &s->statistics.rx_cdt_errors);
1697     qemu_put_be32s(f, &s->statistics.rx_short_frame_errors);
1698     qemu_put_be32s(f, &s->statistics.fc_xmt_pause);
1699     qemu_put_be32s(f, &s->statistics.fc_rcv_pause);
1700     qemu_put_be32s(f, &s->statistics.fc_rcv_unsupported);
1701     qemu_put_be16s(f, &s->statistics.xmt_tco_frames);
1702     qemu_put_be16s(f, &s->statistics.rcv_tco_frames);
1703     qemu_put_be32s(f, &s->statistics.complete);
1704 #if 0
1705     qemu_put_be16s(f, &s->status);
1706 #endif
1707
1708     /* Configuration bytes. */
1709     qemu_put_buffer(f, s->configuration, sizeof(s->configuration));
1710 }
1711
1712 static void nic_cleanup(VLANClientState *vc)
1713 {
1714     EEPRO100State *s = vc->opaque;
1715
1716     unregister_savevm(vc->model, s);
1717
1718     eeprom93xx_free(s->eeprom);
1719 }
1720
1721 static int pci_nic_uninit(PCIDevice *dev)
1722 {
1723     EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, dev);
1724
1725     cpu_unregister_io_memory(s->mmio_index);
1726
1727     return 0;
1728 }
1729
1730 static int nic_init(PCIDevice *pci_dev, uint32_t device)
1731 {
1732     EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev);
1733
1734     logout("\n");
1735
1736     s->dev.unregister = pci_nic_uninit;
1737
1738     s->device = device;
1739
1740     pci_reset(s);
1741
1742     /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM,
1743      * i82559 and later support 64 or 256 word EEPROM. */
1744     s->eeprom = eeprom93xx_new(EEPROM_SIZE);
1745
1746     /* Handler for memory-mapped I/O */
1747     s->mmio_index =
1748         cpu_register_io_memory(pci_mmio_read, pci_mmio_write, s);
1749
1750     pci_register_bar(&s->dev, 0, PCI_MEM_SIZE,
1751                            PCI_ADDRESS_SPACE_MEM |
1752                            PCI_ADDRESS_SPACE_MEM_PREFETCH, pci_mmio_map);
1753     pci_register_bar(&s->dev, 1, PCI_IO_SIZE, PCI_ADDRESS_SPACE_IO,
1754                            pci_map);
1755     pci_register_bar(&s->dev, 2, PCI_FLASH_SIZE, PCI_ADDRESS_SPACE_MEM,
1756                            pci_mmio_map);
1757
1758     qdev_get_macaddr(&s->dev.qdev, s->macaddr);
1759     logout("macaddr: %s\n", nic_dump(&s->macaddr[0], 6));
1760     assert(s->region[1] == 0);
1761
1762     nic_reset(s);
1763
1764     s->vc = qdev_get_vlan_client(&s->dev.qdev,
1765                                  nic_can_receive, nic_receive, NULL,
1766                                  nic_cleanup, s);
1767
1768     qemu_format_nic_info_str(s->vc, s->macaddr);
1769
1770     qemu_register_reset(nic_reset, s);
1771
1772     register_savevm(s->vc->model, -1, 3, nic_save, nic_load, s);
1773     return 0;
1774 }
1775
1776 static int pci_i82551_init(PCIDevice *dev)
1777 {
1778     return nic_init(dev, i82551);
1779 }
1780
1781 static int pci_i82557b_init(PCIDevice *dev)
1782 {
1783     return nic_init(dev, i82557B);
1784 }
1785
1786 static int pci_i82559er_init(PCIDevice *dev)
1787 {
1788     return nic_init(dev, i82559ER);
1789 }
1790
1791 static PCIDeviceInfo eepro100_info[] = {
1792     {
1793         .qdev.name = "i82551",
1794         .qdev.size = sizeof(EEPRO100State),
1795         .init      = pci_i82551_init,
1796     },{
1797         .qdev.name = "i82557b",
1798         .qdev.size = sizeof(EEPRO100State),
1799         .init      = pci_i82557b_init,
1800     },{
1801         .qdev.name = "i82559er",
1802         .qdev.size = sizeof(EEPRO100State),
1803         .init      = pci_i82559er_init,
1804     },{
1805         /* end of list */
1806     }
1807 };
1808
1809 static void eepro100_register_devices(void)
1810 {
1811     pci_qdev_register_many(eepro100_info);
1812 }
1813
1814 device_init(eepro100_register_devices)