1
2
3
4
5
6
7
8
9
10
11#include "qemu/osdep.h"
12#include "qemu/timer.h"
13#include "net/net.h"
14#include "net/eth.h"
15#include "sysemu/sysemu.h"
16#include "hw/irq.h"
17#include "hw/qdev-properties.h"
18#include "migration/vmstate.h"
19#include "qemu/module.h"
20#include "trace.h"
21#include "i82596.h"
22#include <zlib.h>
23
24#if defined(ENABLE_DEBUG)
25#define DBG(x) x
26#else
27#define DBG(x) do { } while (0)
28#endif
29
30#define USE_TIMER 0
31
32#define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
33
34#define PKT_BUF_SZ 1536
35#define MAX_MC_CNT 64
36
37#define ISCP_BUSY 0x0001
38
39#define I596_NULL ((uint32_t)0xffffffff)
40
41#define SCB_STATUS_CX 0x8000
42#define SCB_STATUS_FR 0x4000
43#define SCB_STATUS_CNA 0x2000
44#define SCB_STATUS_RNR 0x1000
45
46#define SCB_COMMAND_ACK_MASK \
47 (SCB_STATUS_CX | SCB_STATUS_FR | SCB_STATUS_CNA | SCB_STATUS_RNR)
48
49#define CU_IDLE 0
50#define CU_SUSPENDED 1
51#define CU_ACTIVE 2
52
53#define RX_IDLE 0
54#define RX_SUSPENDED 1
55#define RX_READY 4
56
57#define CMD_EOL 0x8000
58#define CMD_SUSP 0x4000
59#define CMD_INTR 0x2000
60
61#define CMD_FLEX 0x0008
62
63enum commands {
64 CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
65 CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7
66};
67
68#define STAT_C 0x8000
69#define STAT_B 0x4000
70#define STAT_OK 0x2000
71#define STAT_A 0x1000
72
73#define I596_EOF 0x8000
74#define SIZE_MASK 0x3fff
75
76#define ETHER_TYPE_LEN 2
77#define VLAN_TCI_LEN 2
78#define VLAN_HLEN (ETHER_TYPE_LEN + VLAN_TCI_LEN)
79
80
81#define I596_PREFETCH (s->config[0] & 0x80)
82#define I596_PROMISC (s->config[8] & 0x01)
83#define I596_BC_DISABLE (s->config[8] & 0x02)
84#define I596_NOCRC_INS (s->config[8] & 0x08)
85#define I596_CRCINM (s->config[11] & 0x04)
86#define I596_MC_ALL (s->config[11] & 0x20)
87#define I596_MULTIIA (s->config[13] & 0x40)
88
89
90static uint8_t get_byte(uint32_t addr)
91{
92 return ldub_phys(&address_space_memory, addr);
93}
94
95static void set_byte(uint32_t addr, uint8_t c)
96{
97 return stb_phys(&address_space_memory, addr, c);
98}
99
100static uint16_t get_uint16(uint32_t addr)
101{
102 return lduw_be_phys(&address_space_memory, addr);
103}
104
105static void set_uint16(uint32_t addr, uint16_t w)
106{
107 return stw_be_phys(&address_space_memory, addr, w);
108}
109
110static uint32_t get_uint32(uint32_t addr)
111{
112 uint32_t lo = lduw_be_phys(&address_space_memory, addr);
113 uint32_t hi = lduw_be_phys(&address_space_memory, addr + 2);
114 return (hi << 16) | lo;
115}
116
117static void set_uint32(uint32_t addr, uint32_t val)
118{
119 set_uint16(addr, (uint16_t) val);
120 set_uint16(addr + 2, val >> 16);
121}
122
123
124struct qemu_ether_header {
125 uint8_t ether_dhost[6];
126 uint8_t ether_shost[6];
127 uint16_t ether_type;
128};
129
130#define PRINT_PKTHDR(txt, BUF) do { \
131 struct qemu_ether_header *hdr = (void *)(BUF); \
132 printf(txt ": packet dhost=" MAC_FMT ", shost=" MAC_FMT ", type=0x%04x\n",\
133 MAC_ARG(hdr->ether_dhost), MAC_ARG(hdr->ether_shost), \
134 be16_to_cpu(hdr->ether_type)); \
135} while (0)
136
137static void i82596_transmit(I82596State *s, uint32_t addr)
138{
139 uint32_t tdb_p;
140
141
142 tdb_p = get_uint32(addr + 8);
143 while (tdb_p != I596_NULL) {
144 uint16_t size, len;
145 uint32_t tba;
146
147 size = get_uint16(tdb_p);
148 len = size & SIZE_MASK;
149 tba = get_uint32(tdb_p + 8);
150 trace_i82596_transmit(len, tba);
151
152 if (s->nic && len) {
153 assert(len <= sizeof(s->tx_buffer));
154 address_space_read(&address_space_memory, tba,
155 MEMTXATTRS_UNSPECIFIED, s->tx_buffer, len);
156 DBG(PRINT_PKTHDR("Send", &s->tx_buffer));
157 DBG(printf("Sending %d bytes\n", len));
158 qemu_send_packet(qemu_get_queue(s->nic), s->tx_buffer, len);
159 }
160
161
162 if (size & I596_EOF) {
163 break;
164 }
165
166
167 tdb_p = get_uint32(tdb_p + 4);
168 }
169}
170
171static void set_individual_address(I82596State *s, uint32_t addr)
172{
173 NetClientState *nc;
174 uint8_t *m;
175
176 nc = qemu_get_queue(s->nic);
177 m = s->conf.macaddr.a;
178 address_space_read(&address_space_memory, addr + 8,
179 MEMTXATTRS_UNSPECIFIED, m, ETH_ALEN);
180 qemu_format_nic_info_str(nc, m);
181 trace_i82596_new_mac(nc->info_str);
182}
183
184static void set_multicast_list(I82596State *s, uint32_t addr)
185{
186 uint16_t mc_count, i;
187
188 memset(&s->mult[0], 0, sizeof(s->mult));
189 mc_count = get_uint16(addr + 8) / ETH_ALEN;
190 addr += 10;
191 if (mc_count > MAX_MC_CNT) {
192 mc_count = MAX_MC_CNT;
193 }
194 for (i = 0; i < mc_count; i++) {
195 uint8_t multicast_addr[ETH_ALEN];
196 address_space_read(&address_space_memory, addr + i * ETH_ALEN,
197 MEMTXATTRS_UNSPECIFIED, multicast_addr, ETH_ALEN);
198 DBG(printf("Add multicast entry " MAC_FMT "\n",
199 MAC_ARG(multicast_addr)));
200 unsigned mcast_idx = (net_crc32(multicast_addr, ETH_ALEN) &
201 BITS(7, 2)) >> 2;
202 assert(mcast_idx < 8 * sizeof(s->mult));
203 s->mult[mcast_idx >> 3] |= (1 << (mcast_idx & 7));
204 }
205 trace_i82596_set_multicast(mc_count);
206}
207
208void i82596_set_link_status(NetClientState *nc)
209{
210 I82596State *d = qemu_get_nic_opaque(nc);
211
212 d->lnkst = nc->link_down ? 0 : 0x8000;
213}
214
215static void update_scb_status(I82596State *s)
216{
217 s->scb_status = (s->scb_status & 0xf000)
218 | (s->cu_status << 8) | (s->rx_status << 4);
219 set_uint16(s->scb, s->scb_status);
220}
221
222
223static void i82596_s_reset(I82596State *s)
224{
225 trace_i82596_s_reset(s);
226 s->scp = 0;
227 s->scb_status = 0;
228 s->cu_status = CU_IDLE;
229 s->rx_status = RX_SUSPENDED;
230 s->cmd_p = I596_NULL;
231 s->lnkst = 0x8000;
232 s->ca = s->ca_active = 0;
233 s->send_irq = 0;
234}
235
236
237static void command_loop(I82596State *s)
238{
239 uint16_t cmd;
240 uint16_t status;
241 uint8_t byte_cnt;
242
243 DBG(printf("STARTING COMMAND LOOP cmd_p=%08x\n", s->cmd_p));
244
245 while (s->cmd_p != I596_NULL) {
246
247 status = STAT_B;
248 set_uint16(s->cmd_p, status);
249 status = STAT_C | STAT_OK;
250
251 cmd = get_uint16(s->cmd_p + 2);
252 DBG(printf("Running command %04x at %08x\n", cmd, s->cmd_p));
253
254 switch (cmd & 0x07) {
255 case CmdNOp:
256 break;
257 case CmdSASetup:
258 set_individual_address(s, s->cmd_p);
259 break;
260 case CmdConfigure:
261 byte_cnt = get_byte(s->cmd_p + 8) & 0x0f;
262 byte_cnt = MAX(byte_cnt, 4);
263 byte_cnt = MIN(byte_cnt, sizeof(s->config));
264
265 address_space_read(&address_space_memory, s->cmd_p + 8,
266 MEMTXATTRS_UNSPECIFIED, s->config, byte_cnt);
267
268 s->config[2] &= 0x82;
269 s->config[2] |= 0x40;
270 s->config[7] &= 0xf7;
271 assert(I596_NOCRC_INS == 0);
272 s->config[10] = MAX(s->config[10], 5);
273 s->config[12] &= 0x40;
274 s->config[13] |= 0x3f;
275 break;
276 case CmdTDR:
277
278 set_uint32(s->cmd_p + 8, s->lnkst);
279 break;
280 case CmdTx:
281 i82596_transmit(s, s->cmd_p);
282 break;
283 case CmdMulticastList:
284 set_multicast_list(s, s->cmd_p);
285 break;
286 case CmdDump:
287 case CmdDiagnose:
288 printf("FIXME Command %d !!\n", cmd & 7);
289 assert(0);
290 }
291
292
293 set_uint16(s->cmd_p, status);
294
295 s->cmd_p = get_uint32(s->cmd_p + 4);
296 DBG(printf("NEXT addr would be %08x\n", s->cmd_p));
297 if (s->cmd_p == 0) {
298 s->cmd_p = I596_NULL;
299 }
300
301
302 if (cmd & CMD_EOL) {
303 s->cmd_p = I596_NULL;
304 }
305
306 if (cmd & CMD_SUSP) {
307 s->cu_status = CU_SUSPENDED;
308 printf("FIXME SUSPEND !!\n");
309 }
310
311 if (cmd & CMD_INTR) {
312 s->scb_status |= SCB_STATUS_CX;
313 } else {
314 s->scb_status &= ~SCB_STATUS_CX;
315 }
316 update_scb_status(s);
317
318
319 if (cmd & CMD_INTR) {
320 s->send_irq = 1;
321 }
322
323 if (s->cu_status != CU_ACTIVE) {
324 break;
325 }
326 }
327 DBG(printf("FINISHED COMMAND LOOP\n"));
328 qemu_flush_queued_packets(qemu_get_queue(s->nic));
329}
330
331static void i82596_flush_queue_timer(void *opaque)
332{
333 I82596State *s = opaque;
334 if (0) {
335 timer_del(s->flush_queue_timer);
336 qemu_flush_queued_packets(qemu_get_queue(s->nic));
337 timer_mod(s->flush_queue_timer,
338 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000);
339 }
340}
341
342static void examine_scb(I82596State *s)
343{
344 uint16_t command, cuc, ruc;
345
346
347 command = get_uint16(s->scb + 2);
348 cuc = (command >> 8) & 0x7;
349 ruc = (command >> 4) & 0x7;
350 DBG(printf("MAIN COMMAND %04x cuc %02x ruc %02x\n", command, cuc, ruc));
351
352 set_uint16(s->scb + 2, 0);
353
354 s->scb_status &= ~(command & SCB_COMMAND_ACK_MASK);
355
356 switch (cuc) {
357 case 0:
358 break;
359 case 1:
360 s->cu_status = CU_ACTIVE;
361 break;
362 case 4:
363 s->cu_status = CU_SUSPENDED;
364 s->scb_status |= SCB_STATUS_CNA;
365 break;
366 default:
367 printf("WARNING: Unknown CUC %d!\n", cuc);
368 }
369
370 switch (ruc) {
371 case 0:
372 break;
373 case 1:
374 case 2:
375 s->rx_status = RX_IDLE;
376 if (USE_TIMER) {
377 timer_mod(s->flush_queue_timer, qemu_clock_get_ms(
378 QEMU_CLOCK_VIRTUAL) + 1000);
379 }
380 break;
381 case 3:
382 case 4:
383 s->rx_status = RX_SUSPENDED;
384 s->scb_status |= SCB_STATUS_RNR;
385 break;
386 default:
387 printf("WARNING: Unknown RUC %d!\n", ruc);
388 }
389
390 if (command & 0x80) {
391 i82596_s_reset(s);
392 }
393
394
395 if (s->cu_status != CU_SUSPENDED) {
396 if (s->cmd_p == I596_NULL) {
397 s->cmd_p = get_uint32(s->scb + 4);
398 }
399 }
400
401
402 update_scb_status(s);
403
404 command_loop(s);
405}
406
407static void signal_ca(I82596State *s)
408{
409 uint32_t iscp = 0;
410
411
412 if (s->scp) {
413
414 s->sysbus = get_byte(s->scp + 3);
415 DBG(printf("SYSBUS = %08x\n", s->sysbus));
416 if (((s->sysbus >> 1) & 0x03) != 2) {
417 printf("WARNING: NO LINEAR MODE !!\n");
418 }
419 if ((s->sysbus >> 7)) {
420 printf("WARNING: 32BIT LINMODE IN B-STEPPING NOT SUPPORTED !!\n");
421 }
422 iscp = get_uint32(s->scp + 8);
423 s->scb = get_uint32(iscp + 4);
424 set_byte(iscp + 1, 0);
425 s->scp = 0;
426 }
427
428 s->ca++;
429 if (!s->ca_active) {
430 s->ca_active = 1;
431 while (s->ca) {
432 examine_scb(s);
433 s->ca--;
434 }
435 s->ca_active = 0;
436 }
437
438 if (s->send_irq) {
439 s->send_irq = 0;
440 qemu_set_irq(s->irq, 1);
441 }
442}
443
444void i82596_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
445{
446 I82596State *s = opaque;
447
448 switch (addr) {
449 case PORT_RESET:
450 i82596_s_reset(s);
451 break;
452 case PORT_ALTSCP:
453 s->scp = val;
454 break;
455 case PORT_CA:
456 signal_ca(s);
457 break;
458 }
459}
460
461uint32_t i82596_ioport_readw(void *opaque, uint32_t addr)
462{
463 return -1;
464}
465
466void i82596_h_reset(void *opaque)
467{
468 I82596State *s = opaque;
469
470 i82596_s_reset(s);
471}
472
473bool i82596_can_receive(NetClientState *nc)
474{
475 I82596State *s = qemu_get_nic_opaque(nc);
476
477 if (s->rx_status == RX_SUSPENDED) {
478 return false;
479 }
480
481 if (!s->lnkst) {
482 return false;
483 }
484
485 if (USE_TIMER && !timer_pending(s->flush_queue_timer)) {
486 return true;
487 }
488
489 return true;
490}
491
492#define MIN_BUF_SIZE 60
493
494ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
495{
496 I82596State *s = qemu_get_nic_opaque(nc);
497 uint32_t rfd_p;
498 uint32_t rbd;
499 uint16_t is_broadcast = 0;
500 size_t len = sz;
501 size_t bufsz = sz;
502 uint32_t crc;
503 uint8_t *crc_ptr;
504 uint8_t buf1[MIN_BUF_SIZE + VLAN_HLEN];
505 static const uint8_t broadcast_macaddr[6] = {
506 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
507
508 DBG(printf("i82596_receive() start\n"));
509
510 if (USE_TIMER && timer_pending(s->flush_queue_timer)) {
511 return 0;
512 }
513
514
515 if (s->rx_status == RX_SUSPENDED) {
516 trace_i82596_receive_analysis(">>> Receiving suspended");
517 return -1;
518 }
519
520 if (!s->lnkst) {
521 trace_i82596_receive_analysis(">>> Link down");
522 return -1;
523 }
524
525
526 if (sz < s->config[10]) {
527 printf("Received frame too small, %zu vs. %u bytes\n",
528 sz, s->config[10]);
529 return -1;
530 }
531
532 DBG(printf("Received %lu bytes\n", sz));
533
534 if (I596_PROMISC) {
535
536
537 trace_i82596_receive_analysis(
538 ">>> packet received in promiscuous mode");
539
540 } else {
541
542 if (!memcmp(buf, broadcast_macaddr, 6)) {
543
544 if (I596_BC_DISABLE) {
545 trace_i82596_receive_analysis(">>> broadcast packet rejected");
546
547 return len;
548 }
549
550 trace_i82596_receive_analysis(">>> broadcast packet received");
551 is_broadcast = 1;
552
553 } else if (buf[0] & 0x01) {
554
555 if (!I596_MC_ALL) {
556 trace_i82596_receive_analysis(">>> multicast packet rejected");
557
558 return len;
559 }
560
561 int mcast_idx = (net_crc32(buf, ETH_ALEN) & BITS(7, 2)) >> 2;
562 assert(mcast_idx < 8 * sizeof(s->mult));
563
564 if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7)))) {
565 trace_i82596_receive_analysis(">>> multicast address mismatch");
566
567 return len;
568 }
569
570 trace_i82596_receive_analysis(">>> multicast packet received");
571 is_broadcast = 1;
572
573 } else if (!memcmp(s->conf.macaddr.a, buf, 6)) {
574
575
576 trace_i82596_receive_analysis(
577 ">>> physical address matching packet received");
578
579 } else {
580
581 trace_i82596_receive_analysis(">>> unknown packet");
582
583 return len;
584 }
585 }
586
587
588 if (len < MIN_BUF_SIZE + VLAN_HLEN) {
589 memcpy(buf1, buf, len);
590 memset(buf1 + len, 0, MIN_BUF_SIZE + VLAN_HLEN - len);
591 buf = buf1;
592 if (len < MIN_BUF_SIZE) {
593 len = MIN_BUF_SIZE;
594 }
595 bufsz = len;
596 }
597
598
599 len += 4;
600 crc = cpu_to_be32(crc32(~0, buf, sz));
601 crc_ptr = (uint8_t *) &crc;
602
603 rfd_p = get_uint32(s->scb + 8);
604 assert(rfd_p && rfd_p != I596_NULL);
605
606
607 rbd = get_uint32(rfd_p + 8);
608 assert(rbd && rbd != I596_NULL);
609
610 trace_i82596_receive_packet(len);
611
612
613 while (len) {
614 uint16_t command, status;
615 uint32_t next_rfd;
616
617 command = get_uint16(rfd_p + 2);
618 assert(command & CMD_FLEX);
619
620 rbd = get_uint32(rfd_p + 8);
621 assert(get_uint16(rfd_p + 14) == 0);
622
623
624
625 while (len) {
626 uint16_t buffer_size, num;
627 uint32_t rba;
628 size_t bufcount, crccount;
629
630
631 buffer_size = get_uint16(rbd + 12);
632
633 assert(buffer_size != 0);
634
635 num = buffer_size & SIZE_MASK;
636 if (num > len) {
637 num = len;
638 }
639 rba = get_uint32(rbd + 8);
640
641
642
643
644
645 if ((len - num) >= 4) {
646
647 bufcount = num;
648 } else {
649
650 bufcount = len - 4;
651 }
652 crccount = num - bufcount;
653
654 if (bufcount > 0) {
655
656 assert(bufsz >= bufcount);
657 bufsz -= bufcount;
658 address_space_write(&address_space_memory, rba,
659 MEMTXATTRS_UNSPECIFIED, buf, bufcount);
660 rba += bufcount;
661 buf += bufcount;
662 len -= bufcount;
663 }
664
665
666 if (crccount > 0) {
667 address_space_write(&address_space_memory, rba,
668 MEMTXATTRS_UNSPECIFIED, crc_ptr, crccount);
669 rba += crccount;
670 crc_ptr += crccount;
671 len -= crccount;
672 }
673
674 num |= 0x4000;
675 if (len == 0) {
676 num |= I596_EOF;
677 }
678 set_uint16(rbd + 0, num);
679
680
681 rbd = get_uint32(rbd + 4);
682
683
684 if (buffer_size & I596_EOF)
685 break;
686 }
687
688
689 next_rfd = get_uint32(rfd_p + 4);
690 set_uint32(next_rfd + 8, rbd);
691
692 status = STAT_C | STAT_OK | is_broadcast;
693 set_uint16(rfd_p, status);
694
695 if (command & CMD_SUSP) {
696 s->rx_status = RX_SUSPENDED;
697 s->scb_status |= SCB_STATUS_RNR;
698 break;
699 }
700 if (command & CMD_EOL)
701 break;
702
703 assert(len == 0);
704 }
705
706 assert(len == 0);
707
708 s->scb_status |= SCB_STATUS_FR;
709 update_scb_status(s);
710
711
712 qemu_set_irq(s->irq, 1);
713
714
715 if (0) {
716 DBG(printf("Checking:\n"));
717 rfd_p = get_uint32(s->scb + 8);
718 DBG(printf("Next Receive: rfd is %08x\n", rfd_p));
719 rfd_p = get_uint32(rfd_p + 4);
720 DBG(printf("Next Receive: rfd is %08x\n", rfd_p));
721
722 rbd = get_uint32(rfd_p + 8);
723 DBG(printf("Next Receive: rbd is %08x\n", rbd));
724 }
725
726 return sz;
727}
728
729
730const VMStateDescription vmstate_i82596 = {
731 .name = "i82596",
732 .version_id = 1,
733 .minimum_version_id = 1,
734 .fields = (VMStateField[]) {
735 VMSTATE_UINT16(lnkst, I82596State),
736 VMSTATE_TIMER_PTR(flush_queue_timer, I82596State),
737 VMSTATE_END_OF_LIST()
738 }
739};
740
741void i82596_common_init(DeviceState *dev, I82596State *s, NetClientInfo *info)
742{
743 if (s->conf.macaddr.a[0] == 0) {
744 qemu_macaddr_default_if_unset(&s->conf.macaddr);
745 }
746 s->nic = qemu_new_nic(info, &s->conf, object_get_typename(OBJECT(dev)),
747 dev->id, s);
748 qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
749
750 if (USE_TIMER) {
751 s->flush_queue_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
752 i82596_flush_queue_timer, s);
753 }
754 s->lnkst = 0x8000;
755}
756