/src/qemu/hw/net/eepro100.c
Line | Count | Source |
1 | | /* |
2 | | * QEMU i8255x (PRO100) emulation |
3 | | * |
4 | | * Copyright (C) 2006-2011 Stefan Weil |
5 | | * |
6 | | * Portions of the code are copies from grub / etherboot eepro100.c |
7 | | * and linux e100.c. |
8 | | * |
9 | | * SPDX-License-Identifier: GPL-2.0-or-later |
10 | | * |
11 | | * This program is free software: you can redistribute it and/or modify |
12 | | * it under the terms of the GNU General Public License as published by |
13 | | * the Free Software Foundation, either version 2 of the License, or |
14 | | * (at your option) any later version. |
15 | | * |
16 | | * This program is distributed in the hope that it will be useful, |
17 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | | * GNU General Public License for more details. |
20 | | * |
21 | | * You should have received a copy of the GNU General Public License |
22 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
23 | | * |
24 | | * Tested features (i82559): |
25 | | * PXE boot (i386 guest, i386 / mips / mipsel / ppc host) ok |
26 | | * Linux networking (i386) ok |
27 | | * |
28 | | * Untested: |
29 | | * Windows networking |
30 | | * |
31 | | * References: |
32 | | * |
33 | | * Intel 8255x 10/100 Mbps Ethernet Controller Family |
34 | | * Open Source Software Developer Manual |
35 | | * |
36 | | * TODO: |
37 | | * * PHY emulation should be separated from nic emulation. |
38 | | * Most nic emulations could share the same phy code. |
39 | | * * i82550 is untested. It is programmed like the i82559. |
40 | | * * i82562 is untested. It is programmed like the i82559. |
41 | | * * Power management (i82558 and later) is not implemented. |
42 | | * * Wake-on-LAN is not implemented. |
43 | | */ |
44 | | |
45 | | #include "qemu/osdep.h" |
46 | | #include "qemu/units.h" |
47 | | #include "hw/pci/pci_device.h" |
48 | | #include "hw/core/qdev-properties.h" |
49 | | #include "migration/vmstate.h" |
50 | | #include "net/net.h" |
51 | | #include "net/eth.h" |
52 | | #include "hw/nvram/eeprom93xx.h" |
53 | | #include "system/system.h" |
54 | | #include "system/dma.h" |
55 | | #include "system/reset.h" |
56 | | #include "qemu/bitops.h" |
57 | | #include "qemu/module.h" |
58 | | #include "qapi/error.h" |
59 | | |
60 | | /* QEMU sends frames smaller than 60 bytes to ethernet nics. |
61 | | * Such frames are rejected by real nics and their emulations. |
62 | | * To avoid this behaviour, other nic emulations pad received |
63 | | * frames. The following definition enables this padding for |
64 | | * eepro100, too. We keep the define around in case it might |
65 | | * become useful the future if the core networking is ever |
66 | | * changed to pad short packets itself. */ |
67 | | #define CONFIG_PAD_RECEIVED_FRAMES |
68 | | |
69 | | /* Debug EEPRO100 card. */ |
70 | | #if 0 |
71 | | # define DEBUG_EEPRO100 |
72 | | #endif |
73 | | |
74 | | #ifdef DEBUG_EEPRO100 |
75 | | #define logout(fmt, ...) fprintf(stderr, "EE100\t%-24s" fmt, __func__, ## __VA_ARGS__) |
76 | | #else |
77 | 0 | #define logout(fmt, ...) ((void)0) |
78 | | #endif |
79 | | |
80 | | /* Set flags to 0 to disable debug output. */ |
81 | | #define INT 1 /* interrupt related actions */ |
82 | | #define MDI 1 /* mdi related actions */ |
83 | | #define OTHER 1 |
84 | | #define RXTX 1 |
85 | | #define EEPROM 1 /* eeprom related actions */ |
86 | | |
87 | 0 | #define TRACE(flag, command) ((flag) ? (command) : (void)0) |
88 | | |
89 | 0 | #define missing(text) fprintf(stderr, "eepro100: feature is missing in this emulation: " text "\n") |
90 | | |
91 | 0 | #define MAX_ETH_FRAME_SIZE 1514 |
92 | | |
93 | | /* This driver supports several different devices which are declared here. */ |
94 | 0 | #define i82550 0x82550 |
95 | 0 | #define i82551 0x82551 |
96 | 0 | #define i82557A 0x82557a |
97 | 0 | #define i82557B 0x82557b |
98 | 0 | #define i82557C 0x82557c |
99 | 0 | #define i82558A 0x82558a |
100 | 0 | #define i82558B 0x82558b |
101 | 0 | #define i82559A 0x82559a |
102 | 0 | #define i82559B 0x82559b |
103 | 0 | #define i82559C 0x82559c |
104 | 0 | #define i82559ER 0x82559e |
105 | 0 | #define i82562 0x82562 |
106 | 0 | #define i82801 0x82801 |
107 | | |
108 | | /* Use 64 word EEPROM. TODO: could be a runtime option. */ |
109 | 0 | #define EEPROM_SIZE 64 |
110 | | |
111 | 0 | #define PCI_MEM_SIZE (4 * KiB) |
112 | 0 | #define PCI_IO_SIZE 64 |
113 | 0 | #define PCI_FLASH_SIZE (128 * KiB) |
114 | | |
115 | 0 | #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m) |
116 | | |
117 | | /* The SCB accepts the following controls for the Tx and Rx units: */ |
118 | 0 | #define CU_NOP 0x0000 /* No operation. */ |
119 | 0 | #define CU_START 0x0010 /* CU start. */ |
120 | 0 | #define CU_RESUME 0x0020 /* CU resume. */ |
121 | 0 | #define CU_STATSADDR 0x0040 /* Load dump counters address. */ |
122 | 0 | #define CU_SHOWSTATS 0x0050 /* Dump statistical counters. */ |
123 | 0 | #define CU_CMD_BASE 0x0060 /* Load CU base address. */ |
124 | 0 | #define CU_DUMPSTATS 0x0070 /* Dump and reset statistical counters. */ |
125 | 0 | #define CU_SRESUME 0x00a0 /* CU static resume. */ |
126 | | |
127 | 0 | #define RU_NOP 0x0000 |
128 | 0 | #define RX_START 0x0001 |
129 | 0 | #define RX_RESUME 0x0002 |
130 | 0 | #define RU_ABORT 0x0004 |
131 | 0 | #define RX_ADDR_LOAD 0x0006 |
132 | | #define RX_RESUMENR 0x0007 |
133 | | #define INT_MASK 0x0100 |
134 | | #define DRVR_INT 0x0200 /* Driver generated interrupt. */ |
135 | | |
136 | | typedef struct { |
137 | | const char *name; |
138 | | const char *desc; |
139 | | uint16_t device_id; |
140 | | uint8_t revision; |
141 | | uint16_t subsystem_vendor_id; |
142 | | uint16_t subsystem_id; |
143 | | |
144 | | uint32_t device; |
145 | | uint8_t stats_size; |
146 | | bool has_extended_tcb_support; |
147 | | bool power_management; |
148 | | } E100PCIDeviceInfo; |
149 | | |
150 | | /* Offsets to the various registers. |
151 | | All accesses need not be longword aligned. */ |
152 | | typedef enum { |
153 | | SCBStatus = 0, /* Status Word. */ |
154 | | SCBAck = 1, |
155 | | SCBCmd = 2, /* Rx/Command Unit command and status. */ |
156 | | SCBIntmask = 3, |
157 | | SCBPointer = 4, /* General purpose pointer. */ |
158 | | SCBPort = 8, /* Misc. commands and operands. */ |
159 | | SCBflash = 12, /* Flash memory control. */ |
160 | | SCBeeprom = 14, /* EEPROM control. */ |
161 | | SCBCtrlMDI = 16, /* MDI interface control. */ |
162 | | SCBEarlyRx = 20, /* Early receive byte count. */ |
163 | | SCBFlow = 24, /* Flow Control. */ |
164 | | SCBpmdr = 27, /* Power Management Driver. */ |
165 | | SCBgctrl = 28, /* General Control. */ |
166 | | SCBgstat = 29, /* General Status. */ |
167 | | } E100RegisterOffset; |
168 | | |
169 | | /* A speedo3 transmit buffer descriptor with two buffers... */ |
170 | | typedef struct { |
171 | | uint16_t status; |
172 | | uint16_t command; |
173 | | uint32_t link; /* void * */ |
174 | | uint32_t tbd_array_addr; /* transmit buffer descriptor array address. */ |
175 | | uint16_t tcb_bytes; /* transmit command block byte count (in lower 14 bits */ |
176 | | uint8_t tx_threshold; /* transmit threshold */ |
177 | | uint8_t tbd_count; /* TBD number */ |
178 | | #if 0 |
179 | | /* This constitutes two "TBD" entries: hdr and data */ |
180 | | uint32_t tx_buf_addr0; /* void *, header of frame to be transmitted. */ |
181 | | int32_t tx_buf_size0; /* Length of Tx hdr. */ |
182 | | uint32_t tx_buf_addr1; /* void *, data to be transmitted. */ |
183 | | int32_t tx_buf_size1; /* Length of Tx data. */ |
184 | | #endif |
185 | | } eepro100_tx_t; |
186 | | |
187 | | /* Receive frame descriptor. */ |
188 | | typedef struct { |
189 | | int16_t status; |
190 | | uint16_t command; |
191 | | uint32_t link; /* struct RxFD * */ |
192 | | uint32_t rx_buf_addr; /* void * */ |
193 | | uint16_t count; |
194 | | uint16_t size; |
195 | | /* Ethernet frame data follows. */ |
196 | | } eepro100_rx_t; |
197 | | |
198 | | typedef enum { |
199 | | COMMAND_EL = BIT(15), |
200 | | COMMAND_S = BIT(14), |
201 | | COMMAND_I = BIT(13), |
202 | | COMMAND_NC = BIT(4), |
203 | | COMMAND_SF = BIT(3), |
204 | | COMMAND_CMD = BITS(2, 0), |
205 | | } scb_command_bit; |
206 | | |
207 | | typedef enum { |
208 | | STATUS_C = BIT(15), |
209 | | STATUS_OK = BIT(13), |
210 | | } scb_status_bit; |
211 | | |
212 | | typedef struct { |
213 | | uint32_t tx_good_frames, tx_max_collisions, tx_late_collisions, |
214 | | tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions, |
215 | | tx_multiple_collisions, tx_total_collisions; |
216 | | uint32_t rx_good_frames, rx_crc_errors, rx_alignment_errors, |
217 | | rx_resource_errors, rx_overrun_errors, rx_cdt_errors, |
218 | | rx_short_frame_errors; |
219 | | uint32_t fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported; |
220 | | uint16_t xmt_tco_frames, rcv_tco_frames; |
221 | | /* TODO: i82559 has six reserved statistics but a total of 24 dwords. */ |
222 | | uint32_t reserved[4]; |
223 | | } eepro100_stats_t; |
224 | | |
225 | | typedef enum { |
226 | | cu_idle = 0, |
227 | | cu_suspended = 1, |
228 | | cu_active = 2, |
229 | | cu_lpq_active = 2, |
230 | | cu_hqp_active = 3 |
231 | | } cu_state_t; |
232 | | |
233 | | typedef enum { |
234 | | ru_idle = 0, |
235 | | ru_suspended = 1, |
236 | | ru_no_resources = 2, |
237 | | ru_ready = 4 |
238 | | } ru_state_t; |
239 | | |
240 | | typedef struct { |
241 | | PCIDevice dev; |
242 | | /* Hash register (multicast mask array, multiple individual addresses). */ |
243 | | uint8_t mult[8]; |
244 | | MemoryRegion mmio_bar; |
245 | | MemoryRegion io_bar; |
246 | | MemoryRegion flash_bar; |
247 | | NICState *nic; |
248 | | NICConf conf; |
249 | | uint8_t scb_stat; /* SCB stat/ack byte */ |
250 | | uint8_t int_stat; /* PCI interrupt status */ |
251 | | /* region must not be saved by nic_save. */ |
252 | | uint16_t mdimem[32]; |
253 | | eeprom_t *eeprom; |
254 | | uint32_t device; /* device variant */ |
255 | | /* (cu_base + cu_offset) address the next command block in the command block list. */ |
256 | | uint32_t cu_base; /* CU base address */ |
257 | | uint32_t cu_offset; /* CU address offset */ |
258 | | /* (ru_base + ru_offset) address the RFD in the Receive Frame Area. */ |
259 | | uint32_t ru_base; /* RU base address */ |
260 | | uint32_t ru_offset; /* RU address offset */ |
261 | | uint32_t statsaddr; /* pointer to eepro100_stats_t */ |
262 | | |
263 | | /* Temporary status information (no need to save these values), |
264 | | * used while processing CU commands. */ |
265 | | eepro100_tx_t tx; /* transmit buffer descriptor */ |
266 | | uint32_t cb_address; /* = cu_base + cu_offset */ |
267 | | |
268 | | /* Statistical counters. Also used for wake-up packet (i82559). */ |
269 | | eepro100_stats_t statistics; |
270 | | |
271 | | /* Data in mem is always in the byte order of the controller (le). |
272 | | * It must be dword aligned to allow direct access to 32 bit values. */ |
273 | | uint8_t mem[PCI_MEM_SIZE] __attribute__((aligned(8))); |
274 | | |
275 | | /* Configuration bytes. */ |
276 | | uint8_t configuration[22]; |
277 | | |
278 | | /* vmstate for each particular nic */ |
279 | | VMStateDescription *vmstate; |
280 | | |
281 | | /* Quasi static device properties (no need to save them). */ |
282 | | uint16_t stats_size; |
283 | | bool has_extended_tcb_support; |
284 | | } EEPRO100State; |
285 | | |
286 | | /* Word indices in EEPROM. */ |
287 | | typedef enum { |
288 | | EEPROM_CNFG_MDIX = 0x03, |
289 | | EEPROM_ID = 0x05, |
290 | | EEPROM_PHY_ID = 0x06, |
291 | | EEPROM_VENDOR_ID = 0x0c, |
292 | | EEPROM_CONFIG_ASF = 0x0d, |
293 | | EEPROM_DEVICE_ID = 0x23, |
294 | | EEPROM_SMBUS_ADDR = 0x90, |
295 | | } EEPROMOffset; |
296 | | |
297 | | /* Bit values for EEPROM ID word. */ |
298 | | typedef enum { |
299 | | EEPROM_ID_MDM = BIT(0), /* Modem */ |
300 | | EEPROM_ID_STB = BIT(1), /* Standby Enable */ |
301 | | EEPROM_ID_WMR = BIT(2), /* ??? */ |
302 | | EEPROM_ID_WOL = BIT(5), /* Wake on LAN */ |
303 | | EEPROM_ID_DPD = BIT(6), /* Deep Power Down */ |
304 | | EEPROM_ID_ALT = BIT(7), /* */ |
305 | | /* BITS(10, 8) device revision */ |
306 | | EEPROM_ID_BD = BIT(11), /* boot disable */ |
307 | | EEPROM_ID_ID = BIT(13), /* id bit */ |
308 | | /* BITS(15, 14) signature */ |
309 | | EEPROM_ID_VALID = BIT(14), /* signature for valid eeprom */ |
310 | | } eeprom_id_bit; |
311 | | |
312 | | /* Default values for MDI (PHY) registers */ |
313 | | static const uint16_t eepro100_mdi_default[] = { |
314 | | /* MDI Registers 0 - 6, 7 */ |
315 | | 0x3000, 0x780d, 0x02a8, 0x0154, 0x05e1, 0x0000, 0x0000, 0x0000, |
316 | | /* MDI Registers 8 - 15 */ |
317 | | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, |
318 | | /* MDI Registers 16 - 31 */ |
319 | | 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, |
320 | | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, |
321 | | }; |
322 | | |
323 | | /* Readonly mask for MDI (PHY) registers */ |
324 | | static const uint16_t eepro100_mdi_mask[] = { |
325 | | 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000, |
326 | | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, |
327 | | 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, |
328 | | 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, |
329 | | }; |
330 | | |
331 | | static E100PCIDeviceInfo *eepro100_get_class(EEPRO100State *s); |
332 | | |
333 | | /* Read a 16 bit control/status (CSR) register. */ |
334 | | static uint16_t e100_read_reg2(EEPRO100State *s, E100RegisterOffset addr) |
335 | 0 | { |
336 | 0 | assert(!((uintptr_t)&s->mem[addr] & 1)); |
337 | 0 | return lduw_le_p(&s->mem[addr]); |
338 | 0 | } |
339 | | |
340 | | /* Read a 32 bit control/status (CSR) register. */ |
341 | | static uint32_t e100_read_reg4(EEPRO100State *s, E100RegisterOffset addr) |
342 | 0 | { |
343 | 0 | assert(!((uintptr_t)&s->mem[addr] & 3)); |
344 | 0 | return ldl_le_p(&s->mem[addr]); |
345 | 0 | } |
346 | | |
347 | | /* Write a 16 bit control/status (CSR) register. */ |
348 | | static void e100_write_reg2(EEPRO100State *s, E100RegisterOffset addr, |
349 | | uint16_t val) |
350 | 0 | { |
351 | 0 | assert(!((uintptr_t)&s->mem[addr] & 1)); |
352 | 0 | stw_le_p(&s->mem[addr], val); |
353 | 0 | } |
354 | | |
355 | | /* Read a 32 bit control/status (CSR) register. */ |
356 | | static void e100_write_reg4(EEPRO100State *s, E100RegisterOffset addr, |
357 | | uint32_t val) |
358 | 0 | { |
359 | 0 | assert(!((uintptr_t)&s->mem[addr] & 3)); |
360 | 0 | stl_le_p(&s->mem[addr], val); |
361 | 0 | } |
362 | | |
363 | | #if defined(DEBUG_EEPRO100) |
364 | | static const char *nic_dump(const uint8_t * buf, unsigned size) |
365 | | { |
366 | | static char dump[3 * 16 + 1]; |
367 | | char *p = &dump[0]; |
368 | | if (size > 16) { |
369 | | size = 16; |
370 | | } |
371 | | while (size-- > 0) { |
372 | | p += sprintf(p, " %02x", *buf++); |
373 | | } |
374 | | return dump; |
375 | | } |
376 | | #endif /* DEBUG_EEPRO100 */ |
377 | | |
378 | | enum scb_stat_ack { |
379 | | stat_ack_not_ours = 0x00, |
380 | | stat_ack_sw_gen = 0x04, |
381 | | stat_ack_rnr = 0x10, |
382 | | stat_ack_cu_idle = 0x20, |
383 | | stat_ack_frame_rx = 0x40, |
384 | | stat_ack_cu_cmd_done = 0x80, |
385 | | stat_ack_not_present = 0xFF, |
386 | | stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx), |
387 | | stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done), |
388 | | }; |
389 | | |
390 | | static void disable_interrupt(EEPRO100State * s) |
391 | 0 | { |
392 | 0 | if (s->int_stat) { |
393 | 0 | TRACE(INT, logout("interrupt disabled\n")); |
394 | 0 | pci_irq_deassert(&s->dev); |
395 | 0 | s->int_stat = 0; |
396 | 0 | } |
397 | 0 | } |
398 | | |
399 | | static void enable_interrupt(EEPRO100State * s) |
400 | 0 | { |
401 | 0 | if (!s->int_stat) { |
402 | 0 | TRACE(INT, logout("interrupt enabled\n")); |
403 | 0 | pci_irq_assert(&s->dev); |
404 | 0 | s->int_stat = 1; |
405 | 0 | } |
406 | 0 | } |
407 | | |
408 | | static void eepro100_acknowledge(EEPRO100State * s) |
409 | 0 | { |
410 | 0 | s->scb_stat &= ~s->mem[SCBAck]; |
411 | 0 | s->mem[SCBAck] = s->scb_stat; |
412 | 0 | if (s->scb_stat == 0) { |
413 | 0 | disable_interrupt(s); |
414 | 0 | } |
415 | 0 | } |
416 | | |
417 | | static void eepro100_interrupt(EEPRO100State * s, uint8_t status) |
418 | 0 | { |
419 | 0 | uint8_t mask = ~s->mem[SCBIntmask]; |
420 | 0 | s->mem[SCBAck] |= status; |
421 | 0 | status = s->scb_stat = s->mem[SCBAck]; |
422 | 0 | status &= (mask | 0x0f); |
423 | | #if 0 |
424 | | status &= (~s->mem[SCBIntmask] | 0x0xf); |
425 | | #endif |
426 | 0 | if (status && (mask & 0x01)) { |
427 | | /* SCB mask and SCB Bit M do not disable interrupt. */ |
428 | 0 | enable_interrupt(s); |
429 | 0 | } else if (s->int_stat) { |
430 | 0 | disable_interrupt(s); |
431 | 0 | } |
432 | 0 | } |
433 | | |
434 | | static void eepro100_cx_interrupt(EEPRO100State * s) |
435 | 0 | { |
436 | | /* CU completed action command. */ |
437 | | /* Transmit not ok (82557 only, not in emulation). */ |
438 | 0 | eepro100_interrupt(s, 0x80); |
439 | 0 | } |
440 | | |
441 | | static void eepro100_cna_interrupt(EEPRO100State * s) |
442 | 0 | { |
443 | | /* CU left the active state. */ |
444 | 0 | eepro100_interrupt(s, 0x20); |
445 | 0 | } |
446 | | |
447 | | static void eepro100_fr_interrupt(EEPRO100State * s) |
448 | 0 | { |
449 | | /* RU received a complete frame. */ |
450 | 0 | eepro100_interrupt(s, 0x40); |
451 | 0 | } |
452 | | |
453 | | static void eepro100_rnr_interrupt(EEPRO100State * s) |
454 | 0 | { |
455 | | /* RU is not ready. */ |
456 | 0 | eepro100_interrupt(s, 0x10); |
457 | 0 | } |
458 | | |
459 | | static void eepro100_mdi_interrupt(EEPRO100State * s) |
460 | 0 | { |
461 | | /* MDI completed read or write cycle. */ |
462 | 0 | eepro100_interrupt(s, 0x08); |
463 | 0 | } |
464 | | |
465 | | static void eepro100_swi_interrupt(EEPRO100State * s) |
466 | 0 | { |
467 | | /* Software has requested an interrupt. */ |
468 | 0 | eepro100_interrupt(s, 0x04); |
469 | 0 | } |
470 | | |
471 | | #if 0 |
472 | | static void eepro100_fcp_interrupt(EEPRO100State * s) |
473 | | { |
474 | | /* Flow control pause interrupt (82558 and later). */ |
475 | | eepro100_interrupt(s, 0x01); |
476 | | } |
477 | | #endif |
478 | | |
479 | | static void e100_pci_reset(EEPRO100State *s, Error **errp) |
480 | 0 | { |
481 | 0 | E100PCIDeviceInfo *info = eepro100_get_class(s); |
482 | 0 | uint32_t device = s->device; |
483 | 0 | uint8_t *pci_conf = s->dev.config; |
484 | |
|
485 | 0 | TRACE(OTHER, logout("%p\n", s)); |
486 | | |
487 | | /* PCI Status */ |
488 | 0 | pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM | |
489 | 0 | PCI_STATUS_FAST_BACK); |
490 | | /* PCI Latency Timer */ |
491 | 0 | pci_set_byte(pci_conf + PCI_LATENCY_TIMER, 0x20); /* latency timer = 32 clocks */ |
492 | | /* Capability Pointer is set by PCI framework. */ |
493 | | /* Interrupt Line */ |
494 | | /* Interrupt Pin */ |
495 | 0 | pci_set_byte(pci_conf + PCI_INTERRUPT_PIN, 1); /* interrupt pin A */ |
496 | | /* Minimum Grant */ |
497 | 0 | pci_set_byte(pci_conf + PCI_MIN_GNT, 0x08); |
498 | | /* Maximum Latency */ |
499 | 0 | pci_set_byte(pci_conf + PCI_MAX_LAT, 0x18); |
500 | |
|
501 | 0 | s->stats_size = info->stats_size; |
502 | 0 | s->has_extended_tcb_support = info->has_extended_tcb_support; |
503 | |
|
504 | 0 | switch (device) { |
505 | 0 | case i82550: |
506 | 0 | case i82551: |
507 | 0 | case i82557A: |
508 | 0 | case i82557B: |
509 | 0 | case i82557C: |
510 | 0 | case i82558A: |
511 | 0 | case i82558B: |
512 | 0 | case i82559A: |
513 | 0 | case i82559B: |
514 | 0 | case i82559ER: |
515 | 0 | case i82562: |
516 | 0 | case i82801: |
517 | 0 | case i82559C: |
518 | 0 | break; |
519 | 0 | default: |
520 | 0 | logout("Device %X is undefined!\n", device); |
521 | 0 | } |
522 | | |
523 | | /* Standard TxCB. */ |
524 | 0 | s->configuration[6] |= BIT(4); |
525 | | |
526 | | /* Standard statistical counters. */ |
527 | 0 | s->configuration[6] |= BIT(5); |
528 | |
|
529 | 0 | if (s->stats_size == 80) { |
530 | | /* TODO: check TCO Statistical Counters bit. Documentation not clear. */ |
531 | 0 | if (s->configuration[6] & BIT(2)) { |
532 | | /* TCO statistical counters. */ |
533 | 0 | assert(s->configuration[6] & BIT(5)); |
534 | 0 | } else { |
535 | 0 | if (s->configuration[6] & BIT(5)) { |
536 | | /* No extended statistical counters, i82557 compatible. */ |
537 | 0 | s->stats_size = 64; |
538 | 0 | } else { |
539 | | /* i82558 compatible. */ |
540 | 0 | s->stats_size = 76; |
541 | 0 | } |
542 | 0 | } |
543 | 0 | } else { |
544 | 0 | if (s->configuration[6] & BIT(5)) { |
545 | | /* No extended statistical counters. */ |
546 | 0 | s->stats_size = 64; |
547 | 0 | } |
548 | 0 | } |
549 | 0 | assert(s->stats_size > 0 && s->stats_size <= sizeof(s->statistics)); |
550 | |
|
551 | 0 | if (info->power_management) { |
552 | | /* Power Management Capabilities */ |
553 | 0 | int cfg_offset = 0xdc; |
554 | 0 | int r = pci_pm_init(&s->dev, cfg_offset, errp); |
555 | 0 | if (r < 0) { |
556 | 0 | return; |
557 | 0 | } |
558 | | |
559 | 0 | pci_set_word(pci_conf + cfg_offset + PCI_PM_PMC, 0x7e21); |
560 | | #if 0 /* TODO: replace dummy code for power management emulation. */ |
561 | | /* TODO: Power Management Control / Status. */ |
562 | | pci_set_word(pci_conf + cfg_offset + PCI_PM_CTRL, 0x0000); |
563 | | /* TODO: Ethernet Power Consumption Registers (i82559 and later). */ |
564 | | pci_set_byte(pci_conf + cfg_offset + PCI_PM_PPB_EXTENSIONS, 0x0000); |
565 | | #endif |
566 | 0 | } |
567 | | |
568 | 0 | #if EEPROM_SIZE > 0 |
569 | 0 | if (device == i82557C || device == i82558B || device == i82559C) { |
570 | | /* |
571 | | TODO: get vendor id from EEPROM for i82557C or later. |
572 | | TODO: get device id from EEPROM for i82557C or later. |
573 | | TODO: status bit 4 can be disabled by EEPROM for i82558, i82559. |
574 | | TODO: header type is determined by EEPROM for i82559. |
575 | | TODO: get subsystem id from EEPROM for i82557C or later. |
576 | | TODO: get subsystem vendor id from EEPROM for i82557C or later. |
577 | | TODO: exp. rom baddr depends on a bit in EEPROM for i82558 or later. |
578 | | TODO: capability pointer depends on EEPROM for i82558. |
579 | | */ |
580 | 0 | logout("Get device id and revision from EEPROM!!!\n"); |
581 | 0 | } |
582 | 0 | #endif /* EEPROM_SIZE > 0 */ |
583 | 0 | } |
584 | | |
585 | | static void nic_selective_reset(EEPRO100State * s) |
586 | 0 | { |
587 | 0 | size_t i; |
588 | 0 | uint16_t *eeprom_contents = eeprom93xx_data(s->eeprom); |
589 | | #if 0 |
590 | | eeprom93xx_reset(s->eeprom); |
591 | | #endif |
592 | 0 | memcpy(eeprom_contents, s->conf.macaddr.a, 6); |
593 | 0 | eeprom_contents[EEPROM_ID] = EEPROM_ID_VALID; |
594 | 0 | if (s->device == i82557B || s->device == i82557C) |
595 | 0 | eeprom_contents[5] = 0x0100; |
596 | 0 | eeprom_contents[EEPROM_PHY_ID] = 1; |
597 | 0 | uint16_t sum = 0; |
598 | 0 | for (i = 0; i < EEPROM_SIZE - 1; i++) { |
599 | 0 | sum += eeprom_contents[i]; |
600 | 0 | } |
601 | 0 | eeprom_contents[EEPROM_SIZE - 1] = 0xbaba - sum; |
602 | 0 | TRACE(EEPROM, logout("checksum=0x%04x\n", eeprom_contents[EEPROM_SIZE - 1])); |
603 | |
|
604 | 0 | memset(s->mem, 0, sizeof(s->mem)); |
605 | 0 | e100_write_reg4(s, SCBCtrlMDI, BIT(21)); |
606 | |
|
607 | 0 | assert(sizeof(s->mdimem) == sizeof(eepro100_mdi_default)); |
608 | 0 | memcpy(&s->mdimem[0], &eepro100_mdi_default[0], sizeof(s->mdimem)); |
609 | 0 | } |
610 | | |
611 | | static void nic_reset(void *opaque) |
612 | 0 | { |
613 | 0 | EEPRO100State *s = opaque; |
614 | 0 | TRACE(OTHER, logout("%p\n", s)); |
615 | | /* TODO: Clearing of hash register for selective reset, too? */ |
616 | 0 | memset(&s->mult[0], 0, sizeof(s->mult)); |
617 | 0 | nic_selective_reset(s); |
618 | 0 | } |
619 | | |
620 | | #if defined(DEBUG_EEPRO100) |
621 | | static const char * const e100_reg[PCI_IO_SIZE / 4] = { |
622 | | "Command/Status", |
623 | | "General Pointer", |
624 | | "Port", |
625 | | "EEPROM/Flash Control", |
626 | | "MDI Control", |
627 | | "Receive DMA Byte Count", |
628 | | "Flow Control", |
629 | | "General Status/Control" |
630 | | }; |
631 | | |
632 | | static char *regname(uint32_t addr) |
633 | | { |
634 | | static char buf[32]; |
635 | | if (addr < PCI_IO_SIZE) { |
636 | | const char *r = e100_reg[addr / 4]; |
637 | | if (r != 0) { |
638 | | snprintf(buf, sizeof(buf), "%s+%u", r, addr % 4); |
639 | | } else { |
640 | | snprintf(buf, sizeof(buf), "0x%02x", addr); |
641 | | } |
642 | | } else { |
643 | | snprintf(buf, sizeof(buf), "??? 0x%08x", addr); |
644 | | } |
645 | | return buf; |
646 | | } |
647 | | #endif /* DEBUG_EEPRO100 */ |
648 | | |
649 | | /***************************************************************************** |
650 | | * |
651 | | * Command emulation. |
652 | | * |
653 | | ****************************************************************************/ |
654 | | |
655 | | #if 0 |
656 | | static uint16_t eepro100_read_command(EEPRO100State * s) |
657 | | { |
658 | | uint16_t val = 0xffff; |
659 | | TRACE(OTHER, logout("val=0x%04x\n", val)); |
660 | | return val; |
661 | | } |
662 | | #endif |
663 | | |
664 | | /* Commands that can be put in a command list entry. */ |
665 | | enum commands { |
666 | | CmdNOp = 0, |
667 | | CmdIASetup = 1, |
668 | | CmdConfigure = 2, |
669 | | CmdMulticastList = 3, |
670 | | CmdTx = 4, |
671 | | CmdTDR = 5, /* load microcode */ |
672 | | CmdDump = 6, |
673 | | CmdDiagnose = 7, |
674 | | |
675 | | /* And some extra flags: */ |
676 | | CmdSuspend = 0x4000, /* Suspend after completion. */ |
677 | | CmdIntr = 0x2000, /* Interrupt after completion. */ |
678 | | CmdTxFlex = 0x0008, /* Use "Flexible mode" for CmdTx command. */ |
679 | | }; |
680 | | |
681 | | static cu_state_t get_cu_state(EEPRO100State * s) |
682 | 0 | { |
683 | 0 | return ((s->mem[SCBStatus] & BITS(7, 6)) >> 6); |
684 | 0 | } |
685 | | |
686 | | static void set_cu_state(EEPRO100State * s, cu_state_t state) |
687 | 0 | { |
688 | 0 | s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(7, 6)) + (state << 6); |
689 | 0 | } |
690 | | |
691 | | static ru_state_t get_ru_state(EEPRO100State * s) |
692 | 0 | { |
693 | 0 | return ((s->mem[SCBStatus] & BITS(5, 2)) >> 2); |
694 | 0 | } |
695 | | |
696 | | static void set_ru_state(EEPRO100State * s, ru_state_t state) |
697 | 0 | { |
698 | 0 | s->mem[SCBStatus] = (s->mem[SCBStatus] & ~BITS(5, 2)) + (state << 2); |
699 | 0 | } |
700 | | |
701 | | static void dump_statistics(EEPRO100State * s) |
702 | 0 | { |
703 | 0 | const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED; |
704 | | |
705 | | /* Dump statistical data. Most data is never changed by the emulation |
706 | | * and always 0, so we first just copy the whole block and then those |
707 | | * values which really matter. |
708 | | * Number of data should check configuration!!! |
709 | | */ |
710 | 0 | pci_dma_write(&s->dev, s->statsaddr, &s->statistics, s->stats_size); |
711 | 0 | stl_le_pci_dma(&s->dev, s->statsaddr + 0, |
712 | 0 | s->statistics.tx_good_frames, attrs); |
713 | 0 | stl_le_pci_dma(&s->dev, s->statsaddr + 36, |
714 | 0 | s->statistics.rx_good_frames, attrs); |
715 | 0 | stl_le_pci_dma(&s->dev, s->statsaddr + 48, |
716 | 0 | s->statistics.rx_resource_errors, attrs); |
717 | 0 | stl_le_pci_dma(&s->dev, s->statsaddr + 60, |
718 | 0 | s->statistics.rx_short_frame_errors, attrs); |
719 | | #if 0 |
720 | | stw_le_pci_dma(&s->dev, s->statsaddr + 76, |
721 | | s->statistics.xmt_tco_frames, attrs); |
722 | | stw_le_pci_dma(&s->dev, s->statsaddr + 78, |
723 | | s->statistics.rcv_tco_frames, attrs); |
724 | | missing("CU dump statistical counters"); |
725 | | #endif |
726 | 0 | } |
727 | | |
728 | | static void read_cb(EEPRO100State *s) |
729 | 0 | { |
730 | 0 | pci_dma_read(&s->dev, s->cb_address, &s->tx, sizeof(s->tx)); |
731 | 0 | s->tx.status = le16_to_cpu(s->tx.status); |
732 | 0 | s->tx.command = le16_to_cpu(s->tx.command); |
733 | 0 | s->tx.link = le32_to_cpu(s->tx.link); |
734 | 0 | s->tx.tbd_array_addr = le32_to_cpu(s->tx.tbd_array_addr); |
735 | 0 | s->tx.tcb_bytes = le16_to_cpu(s->tx.tcb_bytes); |
736 | 0 | } |
737 | | |
738 | | static void tx_command(EEPRO100State *s) |
739 | 0 | { |
740 | 0 | const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED; |
741 | 0 | uint32_t tbd_array = s->tx.tbd_array_addr; |
742 | 0 | uint16_t tcb_bytes = s->tx.tcb_bytes & 0x3fff; |
743 | | /* Sends larger than MAX_ETH_FRAME_SIZE are allowed, up to 2600 bytes. */ |
744 | 0 | uint8_t buf[2600]; |
745 | 0 | uint16_t size = 0; |
746 | 0 | uint32_t tbd_address = s->cb_address + 0x10; |
747 | 0 | TRACE(RXTX, logout |
748 | 0 | ("transmit, TBD array address 0x%08x, TCB byte count 0x%04x, TBD count %u\n", |
749 | 0 | tbd_array, tcb_bytes, s->tx.tbd_count)); |
750 | |
|
751 | 0 | if (tcb_bytes > 2600) { |
752 | 0 | logout("TCB byte count too large, using 2600\n"); |
753 | 0 | tcb_bytes = 2600; |
754 | 0 | } |
755 | 0 | if (!((tcb_bytes > 0) || (tbd_array != 0xffffffff))) { |
756 | 0 | logout |
757 | 0 | ("illegal values of TBD array address and TCB byte count!\n"); |
758 | 0 | } |
759 | 0 | assert(tcb_bytes <= sizeof(buf)); |
760 | 0 | while (size < tcb_bytes) { |
761 | 0 | TRACE(RXTX, logout |
762 | 0 | ("TBD (simplified mode): buffer address 0x%08x, size 0x%04x\n", |
763 | 0 | tbd_address, tcb_bytes)); |
764 | 0 | pci_dma_read(&s->dev, tbd_address, &buf[size], tcb_bytes); |
765 | 0 | size += tcb_bytes; |
766 | 0 | } |
767 | 0 | if (tbd_array == 0xffffffff) { |
768 | | /* Simplified mode. Was already handled by code above. */ |
769 | 0 | } else { |
770 | | /* Flexible mode. */ |
771 | 0 | uint8_t tbd_count = 0; |
772 | 0 | uint32_t tx_buffer_address; |
773 | 0 | uint16_t tx_buffer_size; |
774 | 0 | uint16_t tx_buffer_el; |
775 | |
|
776 | 0 | if (s->has_extended_tcb_support && !(s->configuration[6] & BIT(4))) { |
777 | | /* Extended Flexible TCB. */ |
778 | 0 | for (; tbd_count < 2; tbd_count++) { |
779 | 0 | ldl_le_pci_dma(&s->dev, tbd_address, &tx_buffer_address, attrs); |
780 | 0 | lduw_le_pci_dma(&s->dev, tbd_address + 4, &tx_buffer_size, attrs); |
781 | 0 | lduw_le_pci_dma(&s->dev, tbd_address + 6, &tx_buffer_el, attrs); |
782 | 0 | tbd_address += 8; |
783 | 0 | TRACE(RXTX, logout |
784 | 0 | ("TBD (extended flexible mode): buffer address 0x%08x, size 0x%04x\n", |
785 | 0 | tx_buffer_address, tx_buffer_size)); |
786 | 0 | tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size); |
787 | 0 | pci_dma_read(&s->dev, tx_buffer_address, |
788 | 0 | &buf[size], tx_buffer_size); |
789 | 0 | size += tx_buffer_size; |
790 | 0 | if (tx_buffer_el & 1) { |
791 | 0 | break; |
792 | 0 | } |
793 | 0 | } |
794 | 0 | } |
795 | 0 | tbd_address = tbd_array; |
796 | 0 | for (; tbd_count < s->tx.tbd_count; tbd_count++) { |
797 | 0 | ldl_le_pci_dma(&s->dev, tbd_address, &tx_buffer_address, attrs); |
798 | 0 | lduw_le_pci_dma(&s->dev, tbd_address + 4, &tx_buffer_size, attrs); |
799 | 0 | lduw_le_pci_dma(&s->dev, tbd_address + 6, &tx_buffer_el, attrs); |
800 | 0 | tbd_address += 8; |
801 | 0 | TRACE(RXTX, logout |
802 | 0 | ("TBD (flexible mode): buffer address 0x%08x, size 0x%04x\n", |
803 | 0 | tx_buffer_address, tx_buffer_size)); |
804 | 0 | tx_buffer_size = MIN(tx_buffer_size, sizeof(buf) - size); |
805 | 0 | pci_dma_read(&s->dev, tx_buffer_address, |
806 | 0 | &buf[size], tx_buffer_size); |
807 | 0 | size += tx_buffer_size; |
808 | 0 | if (tx_buffer_el & 1) { |
809 | 0 | break; |
810 | 0 | } |
811 | 0 | } |
812 | 0 | } |
813 | 0 | TRACE(RXTX, logout("%p sending frame, len=%d,%s\n", s, size, nic_dump(buf, size))); |
814 | 0 | qemu_send_packet(qemu_get_queue(s->nic), buf, size); |
815 | 0 | s->statistics.tx_good_frames++; |
816 | | /* Transmit with bad status would raise an CX/TNO interrupt. |
817 | | * (82557 only). Emulation never has bad status. */ |
818 | | #if 0 |
819 | | eepro100_cx_interrupt(s); |
820 | | #endif |
821 | 0 | } |
822 | | |
823 | | static void set_multicast_list(EEPRO100State *s) |
824 | 0 | { |
825 | 0 | uint16_t multicast_count = s->tx.tbd_array_addr & BITS(13, 0); |
826 | 0 | uint16_t i; |
827 | 0 | memset(&s->mult[0], 0, sizeof(s->mult)); |
828 | 0 | TRACE(OTHER, logout("multicast list, multicast count = %u\n", multicast_count)); |
829 | 0 | for (i = 0; i < multicast_count; i += 6) { |
830 | 0 | uint8_t multicast_addr[6]; |
831 | 0 | pci_dma_read(&s->dev, s->cb_address + 10 + i, multicast_addr, 6); |
832 | 0 | TRACE(OTHER, logout("multicast entry %s\n", nic_dump(multicast_addr, 6))); |
833 | 0 | unsigned mcast_idx = (net_crc32(multicast_addr, ETH_ALEN) & |
834 | 0 | BITS(7, 2)) >> 2; |
835 | 0 | assert(mcast_idx < 64); |
836 | 0 | s->mult[mcast_idx >> 3] |= (1 << (mcast_idx & 7)); |
837 | 0 | } |
838 | 0 | } |
839 | | |
840 | | static void action_command(EEPRO100State *s) |
841 | 0 | { |
842 | 0 | const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED; |
843 | | /* The loop below won't stop if it gets special handcrafted data. |
844 | | Therefore we limit the number of iterations. */ |
845 | 0 | unsigned max_loop_count = 16; |
846 | |
|
847 | 0 | for (;;) { |
848 | 0 | bool bit_el; |
849 | 0 | bool bit_s; |
850 | 0 | bool bit_i; |
851 | 0 | bool bit_nc; |
852 | 0 | uint16_t ok_status = STATUS_OK; |
853 | 0 | s->cb_address = s->cu_base + s->cu_offset; |
854 | 0 | read_cb(s); |
855 | 0 | bit_el = ((s->tx.command & COMMAND_EL) != 0); |
856 | 0 | bit_s = ((s->tx.command & COMMAND_S) != 0); |
857 | 0 | bit_i = ((s->tx.command & COMMAND_I) != 0); |
858 | 0 | bit_nc = ((s->tx.command & COMMAND_NC) != 0); |
859 | | #if 0 |
860 | | bool bit_sf = ((s->tx.command & COMMAND_SF) != 0); |
861 | | #endif |
862 | |
|
863 | 0 | if (max_loop_count-- == 0) { |
864 | | /* Prevent an endless loop. */ |
865 | 0 | logout("loop in %s:%u\n", __FILE__, __LINE__); |
866 | 0 | break; |
867 | 0 | } |
868 | | |
869 | 0 | s->cu_offset = s->tx.link; |
870 | 0 | TRACE(OTHER, |
871 | 0 | logout("val=(cu start), status=0x%04x, command=0x%04x, link=0x%08x\n", |
872 | 0 | s->tx.status, s->tx.command, s->tx.link)); |
873 | 0 | switch (s->tx.command & COMMAND_CMD) { |
874 | 0 | case CmdNOp: |
875 | | /* Do nothing. */ |
876 | 0 | break; |
877 | 0 | case CmdIASetup: |
878 | 0 | pci_dma_read(&s->dev, s->cb_address + 8, &s->conf.macaddr.a[0], 6); |
879 | 0 | TRACE(OTHER, logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6))); |
880 | 0 | break; |
881 | 0 | case CmdConfigure: |
882 | 0 | pci_dma_read(&s->dev, s->cb_address + 8, |
883 | 0 | &s->configuration[0], sizeof(s->configuration)); |
884 | 0 | TRACE(OTHER, logout("configuration: %s\n", |
885 | 0 | nic_dump(&s->configuration[0], 16))); |
886 | 0 | TRACE(OTHER, logout("configuration: %s\n", |
887 | 0 | nic_dump(&s->configuration[16], |
888 | 0 | ARRAY_SIZE(s->configuration) - 16))); |
889 | 0 | if (s->configuration[20] & BIT(6)) { |
890 | 0 | TRACE(OTHER, logout("Multiple IA bit\n")); |
891 | 0 | } |
892 | 0 | break; |
893 | 0 | case CmdMulticastList: |
894 | 0 | set_multicast_list(s); |
895 | 0 | break; |
896 | 0 | case CmdTx: |
897 | 0 | if (bit_nc) { |
898 | 0 | missing("CmdTx: NC = 0"); |
899 | 0 | ok_status = 0; |
900 | 0 | break; |
901 | 0 | } |
902 | 0 | tx_command(s); |
903 | 0 | break; |
904 | 0 | case CmdTDR: |
905 | 0 | TRACE(OTHER, logout("load microcode\n")); |
906 | | /* Starting with offset 8, the command contains |
907 | | * 64 dwords microcode which we just ignore here. */ |
908 | 0 | break; |
909 | 0 | case CmdDiagnose: |
910 | 0 | TRACE(OTHER, logout("diagnose\n")); |
911 | | /* Make sure error flag is not set. */ |
912 | 0 | s->tx.status = 0; |
913 | 0 | break; |
914 | 0 | default: |
915 | 0 | missing("undefined command"); |
916 | 0 | ok_status = 0; |
917 | 0 | break; |
918 | 0 | } |
919 | | /* Write new status. */ |
920 | 0 | stw_le_pci_dma(&s->dev, s->cb_address, |
921 | 0 | s->tx.status | ok_status | STATUS_C, attrs); |
922 | 0 | if (bit_i) { |
923 | | /* CU completed action. */ |
924 | 0 | eepro100_cx_interrupt(s); |
925 | 0 | } |
926 | 0 | if (bit_el) { |
927 | | /* CU becomes idle. Terminate command loop. */ |
928 | 0 | set_cu_state(s, cu_idle); |
929 | 0 | eepro100_cna_interrupt(s); |
930 | 0 | break; |
931 | 0 | } else if (bit_s) { |
932 | | /* CU becomes suspended. Terminate command loop. */ |
933 | 0 | set_cu_state(s, cu_suspended); |
934 | 0 | eepro100_cna_interrupt(s); |
935 | 0 | break; |
936 | 0 | } else { |
937 | | /* More entries in list. */ |
938 | 0 | TRACE(OTHER, logout("CU list with at least one more entry\n")); |
939 | 0 | } |
940 | 0 | } |
941 | 0 | TRACE(OTHER, logout("CU list empty\n")); |
942 | | /* List is empty. Now CU is idle or suspended. */ |
943 | 0 | } |
944 | | |
945 | | static void eepro100_cu_command(EEPRO100State * s, uint8_t val) |
946 | 0 | { |
947 | 0 | const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED; |
948 | 0 | cu_state_t cu_state; |
949 | 0 | switch (val) { |
950 | 0 | case CU_NOP: |
951 | | /* No operation. */ |
952 | 0 | break; |
953 | 0 | case CU_START: |
954 | 0 | cu_state = get_cu_state(s); |
955 | 0 | if (cu_state != cu_idle && cu_state != cu_suspended) { |
956 | | /* Intel documentation says that CU must be idle or suspended |
957 | | * for the CU start command. */ |
958 | 0 | logout("unexpected CU state is %u\n", cu_state); |
959 | 0 | } |
960 | 0 | set_cu_state(s, cu_active); |
961 | 0 | s->cu_offset = e100_read_reg4(s, SCBPointer); |
962 | 0 | action_command(s); |
963 | 0 | break; |
964 | 0 | case CU_RESUME: |
965 | 0 | if (get_cu_state(s) != cu_suspended) { |
966 | 0 | logout("bad CU resume from CU state %u\n", get_cu_state(s)); |
967 | | /* Workaround for bad Linux eepro100 driver which resumes |
968 | | * from idle state. */ |
969 | | #if 0 |
970 | | missing("cu resume"); |
971 | | #endif |
972 | 0 | set_cu_state(s, cu_suspended); |
973 | 0 | } |
974 | 0 | if (get_cu_state(s) == cu_suspended) { |
975 | 0 | TRACE(OTHER, logout("CU resuming\n")); |
976 | 0 | set_cu_state(s, cu_active); |
977 | 0 | action_command(s); |
978 | 0 | } |
979 | 0 | break; |
980 | 0 | case CU_STATSADDR: |
981 | | /* Load dump counters address. */ |
982 | 0 | s->statsaddr = e100_read_reg4(s, SCBPointer); |
983 | 0 | TRACE(OTHER, logout("val=0x%02x (dump counters address)\n", val)); |
984 | 0 | if (s->statsaddr & 3) { |
985 | | /* Memory must be Dword aligned. */ |
986 | 0 | logout("unaligned dump counters address\n"); |
987 | | /* Handling of misaligned addresses is undefined. |
988 | | * Here we align the address by ignoring the lower bits. */ |
989 | | /* TODO: Test unaligned dump counter address on real hardware. */ |
990 | 0 | s->statsaddr &= ~3; |
991 | 0 | } |
992 | 0 | break; |
993 | 0 | case CU_SHOWSTATS: |
994 | | /* Dump statistical counters. */ |
995 | 0 | TRACE(OTHER, logout("val=0x%02x (dump stats)\n", val)); |
996 | 0 | dump_statistics(s); |
997 | 0 | stl_le_pci_dma(&s->dev, s->statsaddr + s->stats_size, 0xa005, attrs); |
998 | 0 | break; |
999 | 0 | case CU_CMD_BASE: |
1000 | | /* Load CU base. */ |
1001 | 0 | TRACE(OTHER, logout("val=0x%02x (CU base address)\n", val)); |
1002 | 0 | s->cu_base = e100_read_reg4(s, SCBPointer); |
1003 | 0 | break; |
1004 | 0 | case CU_DUMPSTATS: |
1005 | | /* Dump and reset statistical counters. */ |
1006 | 0 | TRACE(OTHER, logout("val=0x%02x (dump stats and reset)\n", val)); |
1007 | 0 | dump_statistics(s); |
1008 | 0 | stl_le_pci_dma(&s->dev, s->statsaddr + s->stats_size, 0xa007, attrs); |
1009 | 0 | memset(&s->statistics, 0, sizeof(s->statistics)); |
1010 | 0 | break; |
1011 | 0 | case CU_SRESUME: |
1012 | | /* CU static resume. */ |
1013 | 0 | missing("CU static resume"); |
1014 | 0 | break; |
1015 | 0 | default: |
1016 | 0 | missing("Undefined CU command"); |
1017 | 0 | } |
1018 | 0 | } |
1019 | | |
1020 | | static void eepro100_ru_command(EEPRO100State * s, uint8_t val) |
1021 | 0 | { |
1022 | 0 | switch (val) { |
1023 | 0 | case RU_NOP: |
1024 | | /* No operation. */ |
1025 | 0 | break; |
1026 | 0 | case RX_START: |
1027 | | /* RU start. */ |
1028 | 0 | if (get_ru_state(s) != ru_idle) { |
1029 | 0 | logout("RU state is %u, should be %u\n", get_ru_state(s), ru_idle); |
1030 | | #if 0 |
1031 | | assert(!"wrong RU state"); |
1032 | | #endif |
1033 | 0 | } |
1034 | 0 | set_ru_state(s, ru_ready); |
1035 | 0 | s->ru_offset = e100_read_reg4(s, SCBPointer); |
1036 | 0 | qemu_flush_queued_packets(qemu_get_queue(s->nic)); |
1037 | 0 | TRACE(OTHER, logout("val=0x%02x (rx start)\n", val)); |
1038 | 0 | break; |
1039 | 0 | case RX_RESUME: |
1040 | | /* Restart RU. */ |
1041 | 0 | if (get_ru_state(s) != ru_suspended) { |
1042 | 0 | logout("RU state is %u, should be %u\n", get_ru_state(s), |
1043 | 0 | ru_suspended); |
1044 | | #if 0 |
1045 | | assert(!"wrong RU state"); |
1046 | | #endif |
1047 | 0 | } |
1048 | 0 | set_ru_state(s, ru_ready); |
1049 | 0 | break; |
1050 | 0 | case RU_ABORT: |
1051 | | /* RU abort. */ |
1052 | 0 | if (get_ru_state(s) == ru_ready) { |
1053 | 0 | eepro100_rnr_interrupt(s); |
1054 | 0 | } |
1055 | 0 | set_ru_state(s, ru_idle); |
1056 | 0 | break; |
1057 | 0 | case RX_ADDR_LOAD: |
1058 | | /* Load RU base. */ |
1059 | 0 | TRACE(OTHER, logout("val=0x%02x (RU base address)\n", val)); |
1060 | 0 | s->ru_base = e100_read_reg4(s, SCBPointer); |
1061 | 0 | break; |
1062 | 0 | default: |
1063 | 0 | logout("val=0x%02x (undefined RU command)\n", val); |
1064 | 0 | missing("Undefined SU command"); |
1065 | 0 | } |
1066 | 0 | } |
1067 | | |
1068 | | static void eepro100_write_command(EEPRO100State * s, uint8_t val) |
1069 | 0 | { |
1070 | 0 | eepro100_ru_command(s, val & 0x0f); |
1071 | 0 | eepro100_cu_command(s, val & 0xf0); |
1072 | 0 | if ((val) == 0) { |
1073 | 0 | TRACE(OTHER, logout("val=0x%02x\n", val)); |
1074 | 0 | } |
1075 | | /* Clear command byte after command was accepted. */ |
1076 | 0 | s->mem[SCBCmd] = 0; |
1077 | 0 | } |
1078 | | |
1079 | | /***************************************************************************** |
1080 | | * |
1081 | | * EEPROM emulation. |
1082 | | * |
1083 | | ****************************************************************************/ |
1084 | | |
1085 | 0 | #define EEPROM_CS 0x02 |
1086 | 0 | #define EEPROM_SK 0x01 |
1087 | 0 | #define EEPROM_DI 0x04 |
1088 | 0 | #define EEPROM_DO 0x08 |
1089 | | |
1090 | | static uint16_t eepro100_read_eeprom(EEPRO100State * s) |
1091 | 0 | { |
1092 | 0 | uint16_t val = e100_read_reg2(s, SCBeeprom); |
1093 | 0 | if (eeprom93xx_read(s->eeprom)) { |
1094 | 0 | val |= EEPROM_DO; |
1095 | 0 | } else { |
1096 | 0 | val &= ~EEPROM_DO; |
1097 | 0 | } |
1098 | 0 | TRACE(EEPROM, logout("val=0x%04x\n", val)); |
1099 | 0 | return val; |
1100 | 0 | } |
1101 | | |
1102 | | static void eepro100_write_eeprom(eeprom_t * eeprom, uint8_t val) |
1103 | 0 | { |
1104 | 0 | TRACE(EEPROM, logout("val=0x%02x\n", val)); |
1105 | | |
1106 | | /* mask unwritable bits */ |
1107 | | #if 0 |
1108 | | val = SET_MASKED(val, 0x31, eeprom->value); |
1109 | | #endif |
1110 | |
|
1111 | 0 | int eecs = ((val & EEPROM_CS) != 0); |
1112 | 0 | int eesk = ((val & EEPROM_SK) != 0); |
1113 | 0 | int eedi = ((val & EEPROM_DI) != 0); |
1114 | 0 | eeprom93xx_write(eeprom, eecs, eesk, eedi); |
1115 | 0 | } |
1116 | | |
1117 | | /***************************************************************************** |
1118 | | * |
1119 | | * MDI emulation. |
1120 | | * |
1121 | | ****************************************************************************/ |
1122 | | |
1123 | | #if defined(DEBUG_EEPRO100) |
1124 | | static const char * const mdi_op_name[] = { |
1125 | | "opcode 0", |
1126 | | "write", |
1127 | | "read", |
1128 | | "opcode 3" |
1129 | | }; |
1130 | | |
1131 | | static const char * const mdi_reg_name[] = { |
1132 | | "Control", |
1133 | | "Status", |
1134 | | "PHY Identification (Word 1)", |
1135 | | "PHY Identification (Word 2)", |
1136 | | "Auto-Negotiation Advertisement", |
1137 | | "Auto-Negotiation Link Partner Ability", |
1138 | | "Auto-Negotiation Expansion" |
1139 | | }; |
1140 | | |
1141 | | static const char *reg2name(uint8_t reg) |
1142 | | { |
1143 | | static char buffer[10]; |
1144 | | const char *p = buffer; |
1145 | | if (reg < ARRAY_SIZE(mdi_reg_name)) { |
1146 | | p = mdi_reg_name[reg]; |
1147 | | } else { |
1148 | | snprintf(buffer, sizeof(buffer), "reg=0x%02x", reg); |
1149 | | } |
1150 | | return p; |
1151 | | } |
1152 | | #endif /* DEBUG_EEPRO100 */ |
1153 | | |
1154 | | static uint32_t eepro100_read_mdi(EEPRO100State * s) |
1155 | 0 | { |
1156 | 0 | uint32_t val = e100_read_reg4(s, SCBCtrlMDI); |
1157 | |
|
1158 | | #ifdef DEBUG_EEPRO100 |
1159 | | uint8_t raiseint = (val & BIT(29)) >> 29; |
1160 | | uint8_t opcode = (val & BITS(27, 26)) >> 26; |
1161 | | uint8_t phy = (val & BITS(25, 21)) >> 21; |
1162 | | uint8_t reg = (val & BITS(20, 16)) >> 16; |
1163 | | uint16_t data = (val & BITS(15, 0)); |
1164 | | #endif |
1165 | | /* Emulation takes no time to finish MDI transaction. */ |
1166 | 0 | val |= BIT(28); |
1167 | 0 | TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n", |
1168 | 0 | val, raiseint, mdi_op_name[opcode], phy, |
1169 | 0 | reg2name(reg), data)); |
1170 | 0 | return val; |
1171 | 0 | } |
1172 | | |
1173 | | static void eepro100_write_mdi(EEPRO100State *s) |
1174 | 0 | { |
1175 | 0 | uint32_t val = e100_read_reg4(s, SCBCtrlMDI); |
1176 | 0 | uint8_t raiseint = (val & BIT(29)) >> 29; |
1177 | 0 | uint8_t opcode = (val & BITS(27, 26)) >> 26; |
1178 | 0 | uint8_t phy = (val & BITS(25, 21)) >> 21; |
1179 | 0 | uint8_t reg = (val & BITS(20, 16)) >> 16; |
1180 | 0 | uint16_t data = (val & BITS(15, 0)); |
1181 | 0 | TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n", |
1182 | 0 | val, raiseint, mdi_op_name[opcode], phy, reg2name(reg), data)); |
1183 | 0 | if (phy != 1) { |
1184 | | /* Unsupported PHY address. */ |
1185 | | #if 0 |
1186 | | logout("phy must be 1 but is %u\n", phy); |
1187 | | #endif |
1188 | 0 | data = 0; |
1189 | 0 | } else if (opcode != 1 && opcode != 2) { |
1190 | | /* Unsupported opcode. */ |
1191 | 0 | logout("opcode must be 1 or 2 but is %u\n", opcode); |
1192 | 0 | data = 0; |
1193 | 0 | } else if (reg > 6) { |
1194 | | /* Unsupported register. */ |
1195 | 0 | logout("register must be 0...6 but is %u\n", reg); |
1196 | 0 | data = 0; |
1197 | 0 | } else { |
1198 | 0 | TRACE(MDI, logout("val=0x%08x (int=%u, %s, phy=%u, %s, data=0x%04x\n", |
1199 | 0 | val, raiseint, mdi_op_name[opcode], phy, |
1200 | 0 | reg2name(reg), data)); |
1201 | 0 | if (opcode == 1) { |
1202 | | /* MDI write */ |
1203 | 0 | switch (reg) { |
1204 | 0 | case 0: /* Control Register */ |
1205 | 0 | if (data & 0x8000) { |
1206 | | /* Reset status and control registers to default. */ |
1207 | 0 | s->mdimem[0] = eepro100_mdi_default[0]; |
1208 | 0 | s->mdimem[1] = eepro100_mdi_default[1]; |
1209 | 0 | data = s->mdimem[reg]; |
1210 | 0 | } else { |
1211 | | /* Restart Auto Configuration = Normal Operation */ |
1212 | 0 | data &= ~0x0200; |
1213 | 0 | } |
1214 | 0 | break; |
1215 | 0 | case 1: /* Status Register */ |
1216 | 0 | missing("not writable"); |
1217 | 0 | break; |
1218 | 0 | case 2: /* PHY Identification Register (Word 1) */ |
1219 | 0 | case 3: /* PHY Identification Register (Word 2) */ |
1220 | 0 | missing("not implemented"); |
1221 | 0 | break; |
1222 | 0 | case 4: /* Auto-Negotiation Advertisement Register */ |
1223 | 0 | case 5: /* Auto-Negotiation Link Partner Ability Register */ |
1224 | 0 | break; |
1225 | 0 | case 6: /* Auto-Negotiation Expansion Register */ |
1226 | 0 | default: |
1227 | 0 | missing("not implemented"); |
1228 | 0 | } |
1229 | 0 | s->mdimem[reg] &= eepro100_mdi_mask[reg]; |
1230 | 0 | s->mdimem[reg] |= data & ~eepro100_mdi_mask[reg]; |
1231 | 0 | } else if (opcode == 2) { |
1232 | | /* MDI read */ |
1233 | 0 | switch (reg) { |
1234 | 0 | case 0: /* Control Register */ |
1235 | 0 | if (data & 0x8000) { |
1236 | | /* Reset status and control registers to default. */ |
1237 | 0 | s->mdimem[0] = eepro100_mdi_default[0]; |
1238 | 0 | s->mdimem[1] = eepro100_mdi_default[1]; |
1239 | 0 | } |
1240 | 0 | break; |
1241 | 0 | case 1: /* Status Register */ |
1242 | 0 | s->mdimem[reg] |= 0x0020; |
1243 | 0 | break; |
1244 | 0 | case 2: /* PHY Identification Register (Word 1) */ |
1245 | 0 | case 3: /* PHY Identification Register (Word 2) */ |
1246 | 0 | case 4: /* Auto-Negotiation Advertisement Register */ |
1247 | 0 | break; |
1248 | 0 | case 5: /* Auto-Negotiation Link Partner Ability Register */ |
1249 | 0 | s->mdimem[reg] = 0x41fe; |
1250 | 0 | break; |
1251 | 0 | case 6: /* Auto-Negotiation Expansion Register */ |
1252 | 0 | s->mdimem[reg] = 0x0001; |
1253 | 0 | break; |
1254 | 0 | } |
1255 | 0 | data = s->mdimem[reg]; |
1256 | 0 | } |
1257 | | /* Emulation takes no time to finish MDI transaction. |
1258 | | * Set MDI bit in SCB status register. */ |
1259 | 0 | s->mem[SCBAck] |= 0x08; |
1260 | 0 | val |= BIT(28); |
1261 | 0 | if (raiseint) { |
1262 | 0 | eepro100_mdi_interrupt(s); |
1263 | 0 | } |
1264 | 0 | } |
1265 | 0 | val = (val & 0xffff0000) + data; |
1266 | 0 | e100_write_reg4(s, SCBCtrlMDI, val); |
1267 | 0 | } |
1268 | | |
1269 | | /***************************************************************************** |
1270 | | * |
1271 | | * Port emulation. |
1272 | | * |
1273 | | ****************************************************************************/ |
1274 | | |
1275 | 0 | #define PORT_SOFTWARE_RESET 0 |
1276 | 0 | #define PORT_SELFTEST 1 |
1277 | 0 | #define PORT_SELECTIVE_RESET 2 |
1278 | | #define PORT_DUMP 3 |
1279 | 0 | #define PORT_SELECTION_MASK 3 |
1280 | | |
1281 | | typedef struct { |
1282 | | uint32_t st_sign; /* Self Test Signature */ |
1283 | | uint32_t st_result; /* Self Test Results */ |
1284 | | } eepro100_selftest_t; |
1285 | | |
1286 | | static uint32_t eepro100_read_port(EEPRO100State * s) |
1287 | 0 | { |
1288 | 0 | return 0; |
1289 | 0 | } |
1290 | | |
1291 | | static void eepro100_write_port(EEPRO100State *s) |
1292 | 0 | { |
1293 | 0 | uint32_t val = e100_read_reg4(s, SCBPort); |
1294 | 0 | uint32_t address = (val & ~PORT_SELECTION_MASK); |
1295 | 0 | uint8_t selection = (val & PORT_SELECTION_MASK); |
1296 | 0 | switch (selection) { |
1297 | 0 | case PORT_SOFTWARE_RESET: |
1298 | 0 | nic_reset(s); |
1299 | 0 | break; |
1300 | 0 | case PORT_SELFTEST: |
1301 | 0 | TRACE(OTHER, logout("selftest address=0x%08x\n", address)); |
1302 | 0 | eepro100_selftest_t data; |
1303 | 0 | pci_dma_read(&s->dev, address, (uint8_t *) &data, sizeof(data)); |
1304 | 0 | data.st_sign = 0xffffffff; |
1305 | 0 | data.st_result = 0; |
1306 | 0 | pci_dma_write(&s->dev, address, (uint8_t *) &data, sizeof(data)); |
1307 | 0 | break; |
1308 | 0 | case PORT_SELECTIVE_RESET: |
1309 | 0 | TRACE(OTHER, logout("selective reset, selftest address=0x%08x\n", address)); |
1310 | 0 | nic_selective_reset(s); |
1311 | 0 | break; |
1312 | 0 | default: |
1313 | 0 | logout("val=0x%08x\n", val); |
1314 | 0 | missing("unknown port selection"); |
1315 | 0 | } |
1316 | 0 | } |
1317 | | |
1318 | | /***************************************************************************** |
1319 | | * |
1320 | | * General hardware emulation. |
1321 | | * |
1322 | | ****************************************************************************/ |
1323 | | |
1324 | | static uint8_t eepro100_read1(EEPRO100State * s, uint32_t addr) |
1325 | 0 | { |
1326 | 0 | uint8_t val = 0; |
1327 | 0 | if (addr <= sizeof(s->mem) - sizeof(val)) { |
1328 | 0 | val = s->mem[addr]; |
1329 | 0 | } |
1330 | |
|
1331 | 0 | switch (addr) { |
1332 | 0 | case SCBStatus: |
1333 | 0 | case SCBAck: |
1334 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1335 | 0 | break; |
1336 | 0 | case SCBCmd: |
1337 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1338 | | #if 0 |
1339 | | val = eepro100_read_command(s); |
1340 | | #endif |
1341 | 0 | break; |
1342 | 0 | case SCBIntmask: |
1343 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1344 | 0 | break; |
1345 | 0 | case SCBPort + 3: |
1346 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1347 | 0 | break; |
1348 | 0 | case SCBeeprom: |
1349 | 0 | val = eepro100_read_eeprom(s); |
1350 | 0 | break; |
1351 | 0 | case SCBCtrlMDI: |
1352 | 0 | case SCBCtrlMDI + 1: |
1353 | 0 | case SCBCtrlMDI + 2: |
1354 | 0 | case SCBCtrlMDI + 3: |
1355 | 0 | val = (uint8_t)(eepro100_read_mdi(s) >> (8 * (addr & 3))); |
1356 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1357 | 0 | break; |
1358 | 0 | case SCBpmdr: /* Power Management Driver Register */ |
1359 | 0 | val = 0; |
1360 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1361 | 0 | break; |
1362 | 0 | case SCBgctrl: /* General Control Register */ |
1363 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1364 | 0 | break; |
1365 | 0 | case SCBgstat: /* General Status Register */ |
1366 | | /* 100 Mbps full duplex, valid link */ |
1367 | 0 | val = 0x07; |
1368 | 0 | TRACE(OTHER, logout("addr=General Status val=%02x\n", val)); |
1369 | 0 | break; |
1370 | 0 | default: |
1371 | 0 | logout("addr=%s val=0x%02x\n", regname(addr), val); |
1372 | 0 | missing("unknown byte read"); |
1373 | 0 | } |
1374 | 0 | return val; |
1375 | 0 | } |
1376 | | |
1377 | | static uint16_t eepro100_read2(EEPRO100State * s, uint32_t addr) |
1378 | 0 | { |
1379 | 0 | uint16_t val = 0; |
1380 | 0 | if (addr <= sizeof(s->mem) - sizeof(val)) { |
1381 | 0 | val = e100_read_reg2(s, addr); |
1382 | 0 | } |
1383 | |
|
1384 | 0 | switch (addr) { |
1385 | 0 | case SCBStatus: |
1386 | 0 | case SCBCmd: |
1387 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1388 | 0 | break; |
1389 | 0 | case SCBeeprom: |
1390 | 0 | val = eepro100_read_eeprom(s); |
1391 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1392 | 0 | break; |
1393 | 0 | case SCBCtrlMDI: |
1394 | 0 | case SCBCtrlMDI + 2: |
1395 | 0 | val = (uint16_t)(eepro100_read_mdi(s) >> (8 * (addr & 3))); |
1396 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1397 | 0 | break; |
1398 | 0 | default: |
1399 | 0 | logout("addr=%s val=0x%04x\n", regname(addr), val); |
1400 | 0 | missing("unknown word read"); |
1401 | 0 | } |
1402 | 0 | return val; |
1403 | 0 | } |
1404 | | |
1405 | | static uint32_t eepro100_read4(EEPRO100State * s, uint32_t addr) |
1406 | 0 | { |
1407 | 0 | uint32_t val = 0; |
1408 | 0 | if (addr <= sizeof(s->mem) - sizeof(val)) { |
1409 | 0 | val = e100_read_reg4(s, addr); |
1410 | 0 | } |
1411 | |
|
1412 | 0 | switch (addr) { |
1413 | 0 | case SCBStatus: |
1414 | 0 | TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val)); |
1415 | 0 | break; |
1416 | 0 | case SCBPointer: |
1417 | 0 | TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val)); |
1418 | 0 | break; |
1419 | 0 | case SCBPort: |
1420 | 0 | val = eepro100_read_port(s); |
1421 | 0 | TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val)); |
1422 | 0 | break; |
1423 | 0 | case SCBflash: |
1424 | 0 | val = eepro100_read_eeprom(s); |
1425 | 0 | TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val)); |
1426 | 0 | break; |
1427 | 0 | case SCBCtrlMDI: |
1428 | 0 | val = eepro100_read_mdi(s); |
1429 | 0 | break; |
1430 | 0 | default: |
1431 | 0 | logout("addr=%s val=0x%08x\n", regname(addr), val); |
1432 | 0 | missing("unknown longword read"); |
1433 | 0 | } |
1434 | 0 | return val; |
1435 | 0 | } |
1436 | | |
1437 | | static void eepro100_write1(EEPRO100State * s, uint32_t addr, uint8_t val) |
1438 | 0 | { |
1439 | | /* SCBStatus is readonly. */ |
1440 | 0 | if (addr > SCBStatus && addr <= sizeof(s->mem) - sizeof(val)) { |
1441 | 0 | s->mem[addr] = val; |
1442 | 0 | } |
1443 | |
|
1444 | 0 | switch (addr) { |
1445 | 0 | case SCBStatus: |
1446 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1447 | 0 | break; |
1448 | 0 | case SCBAck: |
1449 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1450 | 0 | eepro100_acknowledge(s); |
1451 | 0 | break; |
1452 | 0 | case SCBCmd: |
1453 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1454 | 0 | eepro100_write_command(s, val); |
1455 | 0 | break; |
1456 | 0 | case SCBIntmask: |
1457 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1458 | 0 | if (val & BIT(1)) { |
1459 | 0 | eepro100_swi_interrupt(s); |
1460 | 0 | } |
1461 | 0 | eepro100_interrupt(s, 0); |
1462 | 0 | break; |
1463 | 0 | case SCBPointer: |
1464 | 0 | case SCBPointer + 1: |
1465 | 0 | case SCBPointer + 2: |
1466 | 0 | case SCBPointer + 3: |
1467 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1468 | 0 | break; |
1469 | 0 | case SCBPort: |
1470 | 0 | case SCBPort + 1: |
1471 | 0 | case SCBPort + 2: |
1472 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1473 | 0 | break; |
1474 | 0 | case SCBPort + 3: |
1475 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1476 | 0 | eepro100_write_port(s); |
1477 | 0 | break; |
1478 | 0 | case SCBFlow: /* does not exist on 82557 */ |
1479 | 0 | case SCBFlow + 1: |
1480 | 0 | case SCBFlow + 2: |
1481 | 0 | case SCBpmdr: /* does not exist on 82557 */ |
1482 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1483 | 0 | break; |
1484 | 0 | case SCBeeprom: |
1485 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1486 | 0 | eepro100_write_eeprom(s->eeprom, val); |
1487 | 0 | break; |
1488 | 0 | case SCBCtrlMDI: |
1489 | 0 | case SCBCtrlMDI + 1: |
1490 | 0 | case SCBCtrlMDI + 2: |
1491 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1492 | 0 | break; |
1493 | 0 | case SCBCtrlMDI + 3: |
1494 | 0 | TRACE(OTHER, logout("addr=%s val=0x%02x\n", regname(addr), val)); |
1495 | 0 | eepro100_write_mdi(s); |
1496 | 0 | break; |
1497 | 0 | default: |
1498 | 0 | logout("addr=%s val=0x%02x\n", regname(addr), val); |
1499 | 0 | missing("unknown byte write"); |
1500 | 0 | } |
1501 | 0 | } |
1502 | | |
1503 | | static void eepro100_write2(EEPRO100State * s, uint32_t addr, uint16_t val) |
1504 | 0 | { |
1505 | | /* SCBStatus is readonly. */ |
1506 | 0 | if (addr > SCBStatus && addr <= sizeof(s->mem) - sizeof(val)) { |
1507 | 0 | e100_write_reg2(s, addr, val); |
1508 | 0 | } |
1509 | |
|
1510 | 0 | switch (addr) { |
1511 | 0 | case SCBStatus: |
1512 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1513 | 0 | s->mem[SCBAck] = (val >> 8); |
1514 | 0 | eepro100_acknowledge(s); |
1515 | 0 | break; |
1516 | 0 | case SCBCmd: |
1517 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1518 | 0 | eepro100_write_command(s, val); |
1519 | 0 | eepro100_write1(s, SCBIntmask, val >> 8); |
1520 | 0 | break; |
1521 | 0 | case SCBPointer: |
1522 | 0 | case SCBPointer + 2: |
1523 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1524 | 0 | break; |
1525 | 0 | case SCBPort: |
1526 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1527 | 0 | break; |
1528 | 0 | case SCBPort + 2: |
1529 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1530 | 0 | eepro100_write_port(s); |
1531 | 0 | break; |
1532 | 0 | case SCBeeprom: |
1533 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1534 | 0 | eepro100_write_eeprom(s->eeprom, val); |
1535 | 0 | break; |
1536 | 0 | case SCBCtrlMDI: |
1537 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1538 | 0 | break; |
1539 | 0 | case SCBCtrlMDI + 2: |
1540 | 0 | TRACE(OTHER, logout("addr=%s val=0x%04x\n", regname(addr), val)); |
1541 | 0 | eepro100_write_mdi(s); |
1542 | 0 | break; |
1543 | 0 | default: |
1544 | 0 | logout("addr=%s val=0x%04x\n", regname(addr), val); |
1545 | 0 | missing("unknown word write"); |
1546 | 0 | } |
1547 | 0 | } |
1548 | | |
1549 | | static void eepro100_write4(EEPRO100State * s, uint32_t addr, uint32_t val) |
1550 | 0 | { |
1551 | 0 | if (addr <= sizeof(s->mem) - sizeof(val)) { |
1552 | 0 | e100_write_reg4(s, addr, val); |
1553 | 0 | } |
1554 | |
|
1555 | 0 | switch (addr) { |
1556 | 0 | case SCBPointer: |
1557 | 0 | TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val)); |
1558 | 0 | break; |
1559 | 0 | case SCBPort: |
1560 | 0 | TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val)); |
1561 | 0 | eepro100_write_port(s); |
1562 | 0 | break; |
1563 | 0 | case SCBflash: |
1564 | 0 | TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val)); |
1565 | 0 | val = val >> 16; |
1566 | 0 | eepro100_write_eeprom(s->eeprom, val); |
1567 | 0 | break; |
1568 | 0 | case SCBCtrlMDI: |
1569 | 0 | TRACE(OTHER, logout("addr=%s val=0x%08x\n", regname(addr), val)); |
1570 | 0 | eepro100_write_mdi(s); |
1571 | 0 | break; |
1572 | 0 | default: |
1573 | 0 | logout("addr=%s val=0x%08x\n", regname(addr), val); |
1574 | 0 | missing("unknown longword write"); |
1575 | 0 | } |
1576 | 0 | } |
1577 | | |
1578 | | static uint64_t eepro100_read(void *opaque, hwaddr addr, |
1579 | | unsigned size) |
1580 | 0 | { |
1581 | 0 | EEPRO100State *s = opaque; |
1582 | |
|
1583 | 0 | switch (size) { |
1584 | 0 | case 1: return eepro100_read1(s, addr); |
1585 | 0 | case 2: return eepro100_read2(s, addr); |
1586 | 0 | case 4: return eepro100_read4(s, addr); |
1587 | 0 | default: abort(); |
1588 | 0 | } |
1589 | 0 | } |
1590 | | |
1591 | | static void eepro100_write(void *opaque, hwaddr addr, |
1592 | | uint64_t data, unsigned size) |
1593 | 0 | { |
1594 | 0 | EEPRO100State *s = opaque; |
1595 | |
|
1596 | 0 | switch (size) { |
1597 | 0 | case 1: |
1598 | 0 | eepro100_write1(s, addr, data); |
1599 | 0 | break; |
1600 | 0 | case 2: |
1601 | 0 | eepro100_write2(s, addr, data); |
1602 | 0 | break; |
1603 | 0 | case 4: |
1604 | 0 | eepro100_write4(s, addr, data); |
1605 | 0 | break; |
1606 | 0 | default: |
1607 | 0 | abort(); |
1608 | 0 | } |
1609 | 0 | } |
1610 | | |
1611 | | static const MemoryRegionOps eepro100_ops = { |
1612 | | .read = eepro100_read, |
1613 | | .write = eepro100_write, |
1614 | | .endianness = DEVICE_LITTLE_ENDIAN, |
1615 | | }; |
1616 | | |
1617 | | static ssize_t nic_receive(NetClientState *nc, const uint8_t * buf, size_t size) |
1618 | 0 | { |
1619 | | /* TODO: |
1620 | | * - Magic packets should set bit 30 in power management driver register. |
1621 | | * - Interesting packets should set bit 29 in power management driver register. |
1622 | | */ |
1623 | 0 | const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED; |
1624 | 0 | EEPRO100State *s = qemu_get_nic_opaque(nc); |
1625 | 0 | uint16_t rfd_status = 0xa000; |
1626 | 0 | #if defined(CONFIG_PAD_RECEIVED_FRAMES) |
1627 | 0 | uint8_t min_buf[60]; |
1628 | 0 | #endif |
1629 | 0 | static const uint8_t broadcast_macaddr[6] = |
1630 | 0 | { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
1631 | |
|
1632 | 0 | #if defined(CONFIG_PAD_RECEIVED_FRAMES) |
1633 | | /* Pad to minimum Ethernet frame length */ |
1634 | 0 | if (size < sizeof(min_buf)) { |
1635 | 0 | memcpy(min_buf, buf, size); |
1636 | 0 | memset(&min_buf[size], 0, sizeof(min_buf) - size); |
1637 | 0 | buf = min_buf; |
1638 | 0 | size = sizeof(min_buf); |
1639 | 0 | } |
1640 | 0 | #endif |
1641 | |
|
1642 | 0 | if (s->configuration[8] & 0x80) { |
1643 | | /* CSMA is disabled. */ |
1644 | 0 | logout("%p received while CSMA is disabled\n", s); |
1645 | 0 | return -1; |
1646 | | #if !defined(CONFIG_PAD_RECEIVED_FRAMES) |
1647 | | } else if (size < 64 && (s->configuration[7] & BIT(0))) { |
1648 | | /* Short frame and configuration byte 7/0 (discard short receive) set: |
1649 | | * Short frame is discarded */ |
1650 | | logout("%p received short frame (%zu byte)\n", s, size); |
1651 | | s->statistics.rx_short_frame_errors++; |
1652 | | return -1; |
1653 | | #endif |
1654 | 0 | } else if ((size > MAX_ETH_FRAME_SIZE + 4) && !(s->configuration[18] & BIT(3))) { |
1655 | | /* Long frame and configuration byte 18/3 (long receive ok) not set: |
1656 | | * Long frames are discarded. */ |
1657 | 0 | logout("%p received long frame (%zu byte), ignored\n", s, size); |
1658 | 0 | return -1; |
1659 | 0 | } else if (memcmp(buf, s->conf.macaddr.a, 6) == 0) { /* !!! */ |
1660 | | /* Frame matches individual address. */ |
1661 | | /* TODO: check configuration byte 15/4 (ignore U/L). */ |
1662 | 0 | TRACE(RXTX, logout("%p received frame for me, len=%zu\n", s, size)); |
1663 | 0 | } else if (memcmp(buf, broadcast_macaddr, 6) == 0) { |
1664 | | /* Broadcast frame. */ |
1665 | 0 | TRACE(RXTX, logout("%p received broadcast, len=%zu\n", s, size)); |
1666 | 0 | rfd_status |= 0x0002; |
1667 | 0 | } else if (buf[0] & 0x01) { |
1668 | | /* Multicast frame. */ |
1669 | 0 | TRACE(RXTX, logout("%p received multicast, len=%zu,%s\n", s, size, nic_dump(buf, size))); |
1670 | 0 | if (s->configuration[21] & BIT(3)) { |
1671 | | /* Multicast all bit is set, receive all multicast frames. */ |
1672 | 0 | } else { |
1673 | 0 | unsigned mcast_idx = (net_crc32(buf, ETH_ALEN) & BITS(7, 2)) >> 2; |
1674 | 0 | assert(mcast_idx < 64); |
1675 | 0 | if (s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))) { |
1676 | | /* Multicast frame is allowed in hash table. */ |
1677 | 0 | } else if (s->configuration[15] & BIT(0)) { |
1678 | | /* Promiscuous: receive all. */ |
1679 | 0 | rfd_status |= 0x0004; |
1680 | 0 | } else { |
1681 | 0 | TRACE(RXTX, logout("%p multicast ignored\n", s)); |
1682 | 0 | return -1; |
1683 | 0 | } |
1684 | 0 | } |
1685 | | /* TODO: Next not for promiscuous mode? */ |
1686 | 0 | rfd_status |= 0x0002; |
1687 | 0 | } else if (s->configuration[15] & BIT(0)) { |
1688 | | /* Promiscuous: receive all. */ |
1689 | 0 | TRACE(RXTX, logout("%p received frame in promiscuous mode, len=%zu\n", s, size)); |
1690 | 0 | rfd_status |= 0x0004; |
1691 | 0 | } else if (s->configuration[20] & BIT(6)) { |
1692 | | /* Multiple IA bit set. */ |
1693 | 0 | unsigned mcast_idx = net_crc32(buf, ETH_ALEN) >> 26; |
1694 | 0 | assert(mcast_idx < 64); |
1695 | 0 | if (s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))) { |
1696 | 0 | TRACE(RXTX, logout("%p accepted, multiple IA bit set\n", s)); |
1697 | 0 | } else { |
1698 | 0 | TRACE(RXTX, logout("%p frame ignored, multiple IA bit set\n", s)); |
1699 | 0 | return -1; |
1700 | 0 | } |
1701 | 0 | } else { |
1702 | 0 | TRACE(RXTX, logout("%p received frame, ignored, len=%zu,%s\n", s, size, |
1703 | 0 | nic_dump(buf, size))); |
1704 | 0 | return size; |
1705 | 0 | } |
1706 | | |
1707 | 0 | if (get_ru_state(s) != ru_ready) { |
1708 | | /* No resources available. */ |
1709 | 0 | logout("no resources, state=%u\n", get_ru_state(s)); |
1710 | | /* TODO: RNR interrupt only at first failed frame? */ |
1711 | 0 | eepro100_rnr_interrupt(s); |
1712 | 0 | s->statistics.rx_resource_errors++; |
1713 | | #if 0 |
1714 | | assert(!"no resources"); |
1715 | | #endif |
1716 | 0 | return -1; |
1717 | 0 | } |
1718 | | /* !!! */ |
1719 | 0 | eepro100_rx_t rx; |
1720 | 0 | pci_dma_read(&s->dev, s->ru_base + s->ru_offset, |
1721 | 0 | &rx, sizeof(eepro100_rx_t)); |
1722 | 0 | uint16_t rfd_command = le16_to_cpu(rx.command); |
1723 | 0 | uint16_t rfd_size = le16_to_cpu(rx.size); |
1724 | |
|
1725 | 0 | if (size > rfd_size) { |
1726 | 0 | logout("Receive buffer (%" PRId16 " bytes) too small for data " |
1727 | 0 | "(%zu bytes); data truncated\n", rfd_size, size); |
1728 | 0 | size = rfd_size; |
1729 | 0 | } |
1730 | | #if !defined(CONFIG_PAD_RECEIVED_FRAMES) |
1731 | | if (size < 64) { |
1732 | | rfd_status |= 0x0080; |
1733 | | } |
1734 | | #endif |
1735 | 0 | TRACE(OTHER, logout("command 0x%04x, link 0x%08x, addr 0x%08x, size %u\n", |
1736 | 0 | rfd_command, rx.link, rx.rx_buf_addr, rfd_size)); |
1737 | 0 | stw_le_pci_dma(&s->dev, s->ru_base + s->ru_offset + |
1738 | 0 | offsetof(eepro100_rx_t, status), rfd_status, attrs); |
1739 | 0 | stw_le_pci_dma(&s->dev, s->ru_base + s->ru_offset + |
1740 | 0 | offsetof(eepro100_rx_t, count), size, attrs); |
1741 | | /* Early receive interrupt not supported. */ |
1742 | | #if 0 |
1743 | | eepro100_er_interrupt(s); |
1744 | | #endif |
1745 | | /* Receive CRC Transfer not supported. */ |
1746 | 0 | if (s->configuration[18] & BIT(2)) { |
1747 | 0 | missing("Receive CRC Transfer"); |
1748 | 0 | return -1; |
1749 | 0 | } |
1750 | | /* TODO: check stripping enable bit. */ |
1751 | | #if 0 |
1752 | | assert(!(s->configuration[17] & BIT(0))); |
1753 | | #endif |
1754 | 0 | pci_dma_write(&s->dev, s->ru_base + s->ru_offset + |
1755 | 0 | sizeof(eepro100_rx_t), buf, size); |
1756 | 0 | s->statistics.rx_good_frames++; |
1757 | 0 | eepro100_fr_interrupt(s); |
1758 | 0 | s->ru_offset = le32_to_cpu(rx.link); |
1759 | 0 | if (rfd_command & COMMAND_EL) { |
1760 | | /* EL bit is set, so this was the last frame. */ |
1761 | 0 | logout("receive: Running out of frames\n"); |
1762 | 0 | set_ru_state(s, ru_no_resources); |
1763 | 0 | eepro100_rnr_interrupt(s); |
1764 | 0 | } |
1765 | 0 | if (rfd_command & COMMAND_S) { |
1766 | | /* S bit is set. */ |
1767 | 0 | set_ru_state(s, ru_suspended); |
1768 | 0 | } |
1769 | 0 | return size; |
1770 | 0 | } |
1771 | | |
1772 | | static const VMStateDescription vmstate_eepro100 = { |
1773 | | .version_id = 3, |
1774 | | .minimum_version_id = 2, |
1775 | | .fields = (const VMStateField[]) { |
1776 | | VMSTATE_PCI_DEVICE(dev, EEPRO100State), |
1777 | | VMSTATE_UNUSED(32), |
1778 | | VMSTATE_BUFFER(mult, EEPRO100State), |
1779 | | VMSTATE_BUFFER(mem, EEPRO100State), |
1780 | | /* Save all members of struct between scb_stat and mem. */ |
1781 | | VMSTATE_UINT8(scb_stat, EEPRO100State), |
1782 | | VMSTATE_UINT8(int_stat, EEPRO100State), |
1783 | | VMSTATE_UNUSED(3*4), |
1784 | | VMSTATE_MACADDR(conf.macaddr, EEPRO100State), |
1785 | | VMSTATE_UNUSED(19*4), |
1786 | | VMSTATE_UINT16_ARRAY(mdimem, EEPRO100State, 32), |
1787 | | /* The eeprom should be saved and restored by its own routines. */ |
1788 | | VMSTATE_UINT32(device, EEPRO100State), |
1789 | | /* TODO check device. */ |
1790 | | VMSTATE_UINT32(cu_base, EEPRO100State), |
1791 | | VMSTATE_UINT32(cu_offset, EEPRO100State), |
1792 | | VMSTATE_UINT32(ru_base, EEPRO100State), |
1793 | | VMSTATE_UINT32(ru_offset, EEPRO100State), |
1794 | | VMSTATE_UINT32(statsaddr, EEPRO100State), |
1795 | | /* Save eepro100_stats_t statistics. */ |
1796 | | VMSTATE_UINT32(statistics.tx_good_frames, EEPRO100State), |
1797 | | VMSTATE_UINT32(statistics.tx_max_collisions, EEPRO100State), |
1798 | | VMSTATE_UINT32(statistics.tx_late_collisions, EEPRO100State), |
1799 | | VMSTATE_UINT32(statistics.tx_underruns, EEPRO100State), |
1800 | | VMSTATE_UINT32(statistics.tx_lost_crs, EEPRO100State), |
1801 | | VMSTATE_UINT32(statistics.tx_deferred, EEPRO100State), |
1802 | | VMSTATE_UINT32(statistics.tx_single_collisions, EEPRO100State), |
1803 | | VMSTATE_UINT32(statistics.tx_multiple_collisions, EEPRO100State), |
1804 | | VMSTATE_UINT32(statistics.tx_total_collisions, EEPRO100State), |
1805 | | VMSTATE_UINT32(statistics.rx_good_frames, EEPRO100State), |
1806 | | VMSTATE_UINT32(statistics.rx_crc_errors, EEPRO100State), |
1807 | | VMSTATE_UINT32(statistics.rx_alignment_errors, EEPRO100State), |
1808 | | VMSTATE_UINT32(statistics.rx_resource_errors, EEPRO100State), |
1809 | | VMSTATE_UINT32(statistics.rx_overrun_errors, EEPRO100State), |
1810 | | VMSTATE_UINT32(statistics.rx_cdt_errors, EEPRO100State), |
1811 | | VMSTATE_UINT32(statistics.rx_short_frame_errors, EEPRO100State), |
1812 | | VMSTATE_UINT32(statistics.fc_xmt_pause, EEPRO100State), |
1813 | | VMSTATE_UINT32(statistics.fc_rcv_pause, EEPRO100State), |
1814 | | VMSTATE_UINT32(statistics.fc_rcv_unsupported, EEPRO100State), |
1815 | | VMSTATE_UINT16(statistics.xmt_tco_frames, EEPRO100State), |
1816 | | VMSTATE_UINT16(statistics.rcv_tco_frames, EEPRO100State), |
1817 | | /* Configuration bytes. */ |
1818 | | VMSTATE_BUFFER(configuration, EEPRO100State), |
1819 | | VMSTATE_END_OF_LIST() |
1820 | | } |
1821 | | }; |
1822 | | |
1823 | | static void pci_nic_uninit(PCIDevice *pci_dev) |
1824 | 0 | { |
1825 | 0 | EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev); |
1826 | |
|
1827 | 0 | vmstate_unregister(VMSTATE_IF(&pci_dev->qdev), s->vmstate, s); |
1828 | 0 | g_free(s->vmstate); |
1829 | 0 | eeprom93xx_free(&pci_dev->qdev, s->eeprom); |
1830 | 0 | qemu_del_nic(s->nic); |
1831 | 0 | } |
1832 | | |
1833 | | static NetClientInfo net_eepro100_info = { |
1834 | | .type = NET_CLIENT_DRIVER_NIC, |
1835 | | .size = sizeof(NICState), |
1836 | | .receive = nic_receive, |
1837 | | }; |
1838 | | |
1839 | | static void e100_nic_realize(PCIDevice *pci_dev, Error **errp) |
1840 | 0 | { |
1841 | 0 | EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, pci_dev); |
1842 | 0 | E100PCIDeviceInfo *info = eepro100_get_class(s); |
1843 | 0 | Error *local_err = NULL; |
1844 | |
|
1845 | 0 | TRACE(OTHER, logout("\n")); |
1846 | |
|
1847 | 0 | s->device = info->device; |
1848 | |
|
1849 | 0 | e100_pci_reset(s, &local_err); |
1850 | 0 | if (local_err) { |
1851 | 0 | error_propagate(errp, local_err); |
1852 | 0 | return; |
1853 | 0 | } |
1854 | | |
1855 | | /* Add 64 * 2 EEPROM. i82557 and i82558 support a 64 word EEPROM, |
1856 | | * i82559 and later support 64 or 256 word EEPROM. */ |
1857 | 0 | s->eeprom = eeprom93xx_new(&pci_dev->qdev, EEPROM_SIZE); |
1858 | | |
1859 | | /* Handler for memory-mapped I/O */ |
1860 | 0 | memory_region_init_io(&s->mmio_bar, OBJECT(s), &eepro100_ops, s, |
1861 | 0 | "eepro100-mmio", PCI_MEM_SIZE); |
1862 | 0 | pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->mmio_bar); |
1863 | 0 | memory_region_init_io(&s->io_bar, OBJECT(s), &eepro100_ops, s, |
1864 | 0 | "eepro100-io", PCI_IO_SIZE); |
1865 | 0 | pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar); |
1866 | | /* FIXME: flash aliases to mmio?! */ |
1867 | 0 | memory_region_init_io(&s->flash_bar, OBJECT(s), &eepro100_ops, s, |
1868 | 0 | "eepro100-flash", PCI_FLASH_SIZE); |
1869 | 0 | pci_register_bar(&s->dev, 2, 0, &s->flash_bar); |
1870 | |
|
1871 | 0 | qemu_macaddr_default_if_unset(&s->conf.macaddr); |
1872 | 0 | logout("macaddr: %s\n", nic_dump(&s->conf.macaddr.a[0], 6)); |
1873 | |
|
1874 | 0 | nic_reset(s); |
1875 | |
|
1876 | 0 | s->nic = qemu_new_nic(&net_eepro100_info, &s->conf, |
1877 | 0 | object_get_typename(OBJECT(pci_dev)), |
1878 | 0 | pci_dev->qdev.id, |
1879 | 0 | &pci_dev->qdev.mem_reentrancy_guard, s); |
1880 | |
|
1881 | 0 | qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a); |
1882 | 0 | TRACE(OTHER, logout("%s\n", qemu_get_queue(s->nic)->info_str)); |
1883 | |
|
1884 | 0 | qemu_register_reset(nic_reset, s); |
1885 | |
|
1886 | 0 | s->vmstate = g_memdup(&vmstate_eepro100, sizeof(vmstate_eepro100)); |
1887 | 0 | s->vmstate->name = qemu_get_queue(s->nic)->model; |
1888 | 0 | vmstate_register_any(VMSTATE_IF(&pci_dev->qdev), s->vmstate, s); |
1889 | 0 | } |
1890 | | |
1891 | | static void eepro100_instance_init(Object *obj) |
1892 | 793 | { |
1893 | 793 | EEPRO100State *s = DO_UPCAST(EEPRO100State, dev, PCI_DEVICE(obj)); |
1894 | 793 | device_add_bootindex_property(obj, &s->conf.bootindex, |
1895 | 793 | "bootindex", "/ethernet-phy@0", |
1896 | 793 | DEVICE(s)); |
1897 | 793 | } |
1898 | | |
1899 | | static E100PCIDeviceInfo e100_devices[] = { |
1900 | | { |
1901 | | .name = "i82550", |
1902 | | .desc = "Intel i82550 Ethernet", |
1903 | | .device = i82550, |
1904 | | /* TODO: check device id. */ |
1905 | | .device_id = PCI_DEVICE_ID_INTEL_82551IT, |
1906 | | /* Revision ID: 0x0c, 0x0d, 0x0e. */ |
1907 | | .revision = 0x0e, |
1908 | | /* TODO: check size of statistical counters. */ |
1909 | | .stats_size = 80, |
1910 | | /* TODO: check extended tcb support. */ |
1911 | | .has_extended_tcb_support = true, |
1912 | | .power_management = true, |
1913 | | },{ |
1914 | | .name = "i82551", |
1915 | | .desc = "Intel i82551 Ethernet", |
1916 | | .device = i82551, |
1917 | | .device_id = PCI_DEVICE_ID_INTEL_82551IT, |
1918 | | /* Revision ID: 0x0f, 0x10. */ |
1919 | | .revision = 0x0f, |
1920 | | /* TODO: check size of statistical counters. */ |
1921 | | .stats_size = 80, |
1922 | | .has_extended_tcb_support = true, |
1923 | | .power_management = true, |
1924 | | },{ |
1925 | | .name = "i82557a", |
1926 | | .desc = "Intel i82557A Ethernet", |
1927 | | .device = i82557A, |
1928 | | .device_id = PCI_DEVICE_ID_INTEL_82557, |
1929 | | .revision = 0x01, |
1930 | | .power_management = false, |
1931 | | },{ |
1932 | | .name = "i82557b", |
1933 | | .desc = "Intel i82557B Ethernet", |
1934 | | .device = i82557B, |
1935 | | .device_id = PCI_DEVICE_ID_INTEL_82557, |
1936 | | .revision = 0x02, |
1937 | | .power_management = false, |
1938 | | },{ |
1939 | | .name = "i82557c", |
1940 | | .desc = "Intel i82557C Ethernet", |
1941 | | .device = i82557C, |
1942 | | .device_id = PCI_DEVICE_ID_INTEL_82557, |
1943 | | .revision = 0x03, |
1944 | | .power_management = false, |
1945 | | },{ |
1946 | | .name = "i82558a", |
1947 | | .desc = "Intel i82558A Ethernet", |
1948 | | .device = i82558A, |
1949 | | .device_id = PCI_DEVICE_ID_INTEL_82557, |
1950 | | .revision = 0x04, |
1951 | | .stats_size = 76, |
1952 | | .has_extended_tcb_support = true, |
1953 | | .power_management = true, |
1954 | | },{ |
1955 | | .name = "i82558b", |
1956 | | .desc = "Intel i82558B Ethernet", |
1957 | | .device = i82558B, |
1958 | | .device_id = PCI_DEVICE_ID_INTEL_82557, |
1959 | | .revision = 0x05, |
1960 | | .stats_size = 76, |
1961 | | .has_extended_tcb_support = true, |
1962 | | .power_management = true, |
1963 | | },{ |
1964 | | .name = "i82559a", |
1965 | | .desc = "Intel i82559A Ethernet", |
1966 | | .device = i82559A, |
1967 | | .device_id = PCI_DEVICE_ID_INTEL_82557, |
1968 | | .revision = 0x06, |
1969 | | .stats_size = 80, |
1970 | | .has_extended_tcb_support = true, |
1971 | | .power_management = true, |
1972 | | },{ |
1973 | | .name = "i82559b", |
1974 | | .desc = "Intel i82559B Ethernet", |
1975 | | .device = i82559B, |
1976 | | .device_id = PCI_DEVICE_ID_INTEL_82557, |
1977 | | .revision = 0x07, |
1978 | | .stats_size = 80, |
1979 | | .has_extended_tcb_support = true, |
1980 | | .power_management = true, |
1981 | | },{ |
1982 | | .name = "i82559c", |
1983 | | .desc = "Intel i82559C Ethernet", |
1984 | | .device = i82559C, |
1985 | | .device_id = PCI_DEVICE_ID_INTEL_82557, |
1986 | | #if 0 |
1987 | | .revision = 0x08, |
1988 | | #endif |
1989 | | /* TODO: Windows wants revision id 0x0c. */ |
1990 | | .revision = 0x0c, |
1991 | | #if EEPROM_SIZE > 0 |
1992 | | .subsystem_vendor_id = PCI_VENDOR_ID_INTEL, |
1993 | | .subsystem_id = 0x0040, |
1994 | | #endif |
1995 | | .stats_size = 80, |
1996 | | .has_extended_tcb_support = true, |
1997 | | .power_management = true, |
1998 | | },{ |
1999 | | .name = "i82559er", |
2000 | | .desc = "Intel i82559ER Ethernet", |
2001 | | .device = i82559ER, |
2002 | | .device_id = PCI_DEVICE_ID_INTEL_82551IT, |
2003 | | .revision = 0x09, |
2004 | | .stats_size = 80, |
2005 | | .has_extended_tcb_support = true, |
2006 | | .power_management = true, |
2007 | | },{ |
2008 | | .name = "i82562", |
2009 | | .desc = "Intel i82562 Ethernet", |
2010 | | .device = i82562, |
2011 | | /* TODO: check device id. */ |
2012 | | .device_id = PCI_DEVICE_ID_INTEL_82551IT, |
2013 | | /* TODO: wrong revision id. */ |
2014 | | .revision = 0x0e, |
2015 | | .stats_size = 80, |
2016 | | .has_extended_tcb_support = true, |
2017 | | .power_management = true, |
2018 | | },{ |
2019 | | /* Toshiba Tecra 8200. */ |
2020 | | .name = "i82801", |
2021 | | .desc = "Intel i82801 Ethernet", |
2022 | | .device = i82801, |
2023 | | .device_id = 0x2449, |
2024 | | .revision = 0x03, |
2025 | | .stats_size = 80, |
2026 | | .has_extended_tcb_support = true, |
2027 | | .power_management = true, |
2028 | | } |
2029 | | }; |
2030 | | |
2031 | | static E100PCIDeviceInfo *eepro100_get_class_by_name(const char *typename) |
2032 | 793 | { |
2033 | 793 | E100PCIDeviceInfo *info = NULL; |
2034 | 793 | int i; |
2035 | | |
2036 | | /* This is admittedly awkward but also temporary. QOM allows for |
2037 | | * parameterized typing and for subclassing both of which would suitable |
2038 | | * handle what's going on here. But class_data is already being used as |
2039 | | * a stop-gap hack to allow incremental qdev conversion so we cannot use it |
2040 | | * right now. Once we merge the final QOM series, we can come back here and |
2041 | | * do this in a much more elegant fashion. |
2042 | | */ |
2043 | 5.55k | for (i = 0; i < ARRAY_SIZE(e100_devices); i++) { |
2044 | 5.55k | if (strcmp(e100_devices[i].name, typename) == 0) { |
2045 | 793 | info = &e100_devices[i]; |
2046 | 793 | break; |
2047 | 793 | } |
2048 | 5.55k | } |
2049 | 793 | assert(info != NULL); |
2050 | | |
2051 | 793 | return info; |
2052 | 793 | } |
2053 | | |
2054 | | static E100PCIDeviceInfo *eepro100_get_class(EEPRO100State *s) |
2055 | 0 | { |
2056 | 0 | return eepro100_get_class_by_name(object_get_typename(OBJECT(s))); |
2057 | 0 | } |
2058 | | |
2059 | | static const Property e100_properties[] = { |
2060 | | DEFINE_NIC_PROPERTIES(EEPRO100State, conf), |
2061 | | }; |
2062 | | |
2063 | | static void eepro100_class_init(ObjectClass *klass, const void *data) |
2064 | 793 | { |
2065 | 793 | DeviceClass *dc = DEVICE_CLASS(klass); |
2066 | 793 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
2067 | 793 | E100PCIDeviceInfo *info; |
2068 | | |
2069 | 793 | info = eepro100_get_class_by_name(object_class_get_name(klass)); |
2070 | | |
2071 | 793 | set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); |
2072 | 793 | device_class_set_props(dc, e100_properties); |
2073 | 793 | dc->desc = info->desc; |
2074 | 793 | k->vendor_id = PCI_VENDOR_ID_INTEL; |
2075 | 793 | k->class_id = PCI_CLASS_NETWORK_ETHERNET; |
2076 | 793 | k->romfile = "pxe-eepro100.rom"; |
2077 | 793 | k->realize = e100_nic_realize; |
2078 | 793 | k->exit = pci_nic_uninit; |
2079 | 793 | k->device_id = info->device_id; |
2080 | 793 | k->revision = info->revision; |
2081 | 793 | k->subsystem_vendor_id = info->subsystem_vendor_id; |
2082 | 793 | k->subsystem_id = info->subsystem_id; |
2083 | 793 | } |
2084 | | |
2085 | | static void eepro100_register_types(void) |
2086 | 61 | { |
2087 | 61 | size_t i; |
2088 | 854 | for (i = 0; i < ARRAY_SIZE(e100_devices); i++) { |
2089 | 793 | TypeInfo type_info = {}; |
2090 | 793 | E100PCIDeviceInfo *info = &e100_devices[i]; |
2091 | | |
2092 | 793 | type_info.name = info->name; |
2093 | 793 | type_info.parent = TYPE_PCI_DEVICE; |
2094 | 793 | type_info.class_init = eepro100_class_init; |
2095 | 793 | type_info.instance_size = sizeof(EEPRO100State); |
2096 | 793 | type_info.instance_init = eepro100_instance_init; |
2097 | 793 | type_info.interfaces = (const InterfaceInfo[]) { |
2098 | 793 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, |
2099 | 793 | { }, |
2100 | 793 | }; |
2101 | | |
2102 | 793 | type_register_static(&type_info); |
2103 | 793 | } |
2104 | 61 | } |
2105 | | |
2106 | | type_init(eepro100_register_types) |