Repository navigation
Expand file tree
/
Copy pathfxp.c
More file actions
2206 lines (1893 loc) · 51.5 KB
/
Copy pathfxp.c
File metadata and controls
2206 lines (1893 loc) · 51.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
10BC8
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* fxp.c
*
* This file contains an ethernet device driver for Intel 82557, 82558,
* 82559, 82550, and 82562 fast ethernet controllers.
*
* Created: Nov 2004 by Philip Homburg <philip@f-mnx.phicoh.com>
*/
#include <minix/drivers.h>
#include <minix/netdriver.h>
#include <stdlib.h>
#include <net/hton.h>
#include <net/gen/ether.h>
#include <net/gen/eth_io.h>
#include <machine/pci.h>
#include <minix/ds.h>
#include <minix/endpoint.h>
#include <minix/timers.h>
#define debug 0
#define RAND_UPDATE /**/
#define printW() ((void)0)
#include "assert.h"
#include "fxp.h"
#include "mii.h"
/* Number of receive buffers */
#define N_RX_BUF 40
/* Number of transmit buffers */
#define N_TX_BUF 4
/* I/O vectors are handled IOVEC_NR entries at a time. */
#define IOVEC_NR 16
/* Configuration */
#define FXP_ENVVAR "FXPETH"
typedef int irq_hook_t;
/* ignore interrupt for the moment */
#define interrupt(x) do { } while(0)
static union tmpbuf
{
char pad[4096];
struct cbl_conf cc;
struct ias ias;
} *tmpbufp;
typedef struct fxp
{
port_t fxp_base_port;
int fxp_mode;
int fxp_got_int;
int fxp_send_int;
int fxp_flags;
int fxp_client;
int fxp_features; /* Needed? */
int fxp_irq;
int fxp_type; /* What kind of hardware */
int fxp_ms_regs; /* Master/slave registers */
int fxp_ee_addrlen; /* #EEPROM address bits */
int fxp_tx_alive;
int fxp_need_reset;
/* Rx */
vir_bytes fxp_read_s;
int fxp_rx_nbuf;
int fxp_rx_bufsize;
struct rfd *fxp_rx_buf;
phys_bytes fxp_rx_busaddr;
int fxp_rx_head;
int fxp_rx_need_restart;
int fxp_need_conf; /* Re-configure after draining send
* queue
*/
/* Tx */
int fxp_tx_nbuf;
int fxp_tx_bufsize;
struct tx *fxp_tx_buf;
phys_bytes fxp_tx_busaddr;
int fxp_tx_idle;
int fxp_tx_head;
int fxp_tx_tail;
int fxp_tx_threshold;
/* Link status */
int fxp_report_link;
int fxp_link_up;
int fxp_mii_busy;
u16_t fxp_mii_scr;
/* PCI related */
int fxp_seen; /* TRUE iff device available */
/* 'large' items */
irq_hook_t fxp_hook;
ether_addr_t fxp_address;
message fxp_rx_mess;
message fxp_tx_mess;
struct sc fxp_stat;
u8_t fxp_conf_bytes[CC_BYTES_NR];
char fxp_name[sizeof("fxp#n")];
iovec_t fxp_iovec[IOVEC_NR];
iovec_s_t fxp_iovec_s[IOVEC_NR];
}
fxp_t;
/* fxp_mode */
#define FM_DISABLED 0x0
#define FM_ENABLED 0x1
/* fxp_flags */
#define FF_EMPTY 0x000
#define FF_PACK_SENT 0x001
#define FF_PACK_RECV 0x002
#define FF_SEND_AVAIL 0x004
#define FF_READING 0x010
#define FF_PROMISC 0x040
#define FF_MULTI 0x080
#define FF_BROAD 0x100
#define FF_ENABLED 0x200
/* fxp_features */
#define FFE_NONE 0x0
/* fxp_type */
#define FT_UNKNOWN 0x0
#define FT_82557 0x1
#define FT_82558A 0x2
#define FT_82559 0x4
#define FT_82801 0x8
static int fxp_instance;
static fxp_t *fxp_state;
static minix_timer_t fxp_watchdog;
static u32_t system_hz;
#define fxp_inb(port, offset) (do_inb((port) + (offset)))
#define fxp_inl(port, offset) (do_inl((port) + (offset)))
#define fxp_outb(port, offset, value) (do_outb((port) + (offset), (value)))
#define fxp_outl(port, offset, value) (do_outl((port) + (offset), (value)))
static void fxp_init(message *mp);
static void fxp_pci_conf(void);
static int fxp_probe(fxp_t *fp, int skip);
static void fxp_conf_hw(fxp_t *fp);
static void fxp_init_hw(fxp_t *fp);
static void fxp_init_buf(fxp_t *fp);
static void fxp_reset_hw(fxp_t *fp);
static void fxp_confaddr(fxp_t *fp);
static void fxp_rec_mode(fxp_t *fp);
static void fxp_writev_s(const message *mp, int from_int);
static void fxp_readv_s(message *mp, int from_int);
static void fxp_do_conf(fxp_t *fp);
static void fxp_cu_ptr_cmd(fxp_t *fp, int cmd, phys_bytes bus_addr, int
check_idle);
static void fxp_ru_ptr_cmd(fxp_t *fp, int cmd, phys_bytes bus_addr, int
check_idle);
static void fxp_restart_ru(fxp_t *fp);
static void fxp_getstat_s(message *mp);
static void fxp_handler(fxp_t *fp);
static void fxp_check_ints(fxp_t *fp);
static void fxp_watchdog_f(minix_timer_t *tp);
static int fxp_link_changed(fxp_t *fp);
static void fxp_report_link(fxp_t *fp);
static void reply(fxp_t *fp);
static void mess_reply(message *req, message *reply);
static u16_t eeprom_read(fxp_t *fp, int reg);
static void eeprom_addrsize(fxp_t *fp);
static u16_t mii_read(fxp_t *fp, int reg);
static u8_t do_inb(port_t port);
static u32_t do_inl(port_t port);
static void do_outb(port_t port, u8_t v);
static void do_outl(port_t port, u32_t v);
static void tell_dev(vir_bytes start, size_t size, int pci_bus, int
pci_dev, int pci_func);
static void handle_hw_intr(void)
{
int r;
fxp_t *fp;
fp= fxp_state;
if (fp->fxp_mode != FM_ENABLED)
return;
fxp_handler(fp);
r= sys_irqenable(&fp->fxp_hook);
if (r != OK) {
panic("unable enable interrupts: %d", r);
}
if (!fp->fxp_got_int)
return;
fp->fxp_got_int= 0;
assert(fp->fxp_flags & FF_ENABLED);
fxp_check_ints(fp);
}
/* SEF functions and variables. */
static void sef_local_startup(void);
static int sef_cb_init_fresh(int type, sef_init_info_t *info);
static void sef_cb_signal_handler(int signo);
/*===========================================================================*
* main *
*===========================================================================*/
int main(int argc, char *argv[])
{
message m;
int ipc_status;
int r;
/* SEF local startup. */
env_setargs(argc, argv);
sef_local_startup();
while (TRUE)
{
if ((r= netdriver_receive(ANY, &m, &ipc_status)) != OK)
panic("netdriver_receive failed: %d", r);
if (is_ipc_notify(ipc_status)) {
switch (_ENDPOINT_P(m.m_source)) {
case HARDWARE:
handle_hw_intr();
break;
case CLOCK:
expire_timers(m.m_notify.timestamp);
break;
default:
panic(" illegal notify from: %d", m.m_source);
}
/* get new message */
continue;
}
switch (m.m_type)
{
case DL_WRITEV_S: fxp_writev_s(&m, FALSE); break;
case DL_READV_S: fxp_readv_s(&m, FALSE); break;
case DL_CONF: fxp_init(&m); break;
case DL_GETSTAT_S: fxp_getstat_s(&m); break;
default:
panic(" illegal message: %d", m.m_type);
}
}
}
/*===========================================================================*
* sef_local_startup *
*===========================================================================*/
static void sef_local_startup()
{
/* Register init callbacks. */
sef_setcb_init_fresh(sef_cb_init_fresh);
sef_setcb_init_lu(sef_cb_init_fresh);
sef_setcb_init_restart(sef_cb_init_fresh);
/* Register live update callbacks. */
sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready);
sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_workfree);
/* Register signal callbacks. */
sef_setcb_signal_handler(sef_cb_signal_handler);
/* Let SEF perform startup. */
sef_startup();
}
/*===========================================================================*
* sef_cb_init_fresh *
*===========================================================================*/
static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
{
/* Initialize the fxp driver. */
long v;
int r;
vir_bytes ft;
system_hz = sys_hz();
v = 0;
(void) env_parse("instance", "d", 0, &v, 0, 255);
fxp_instance = (int) v;
ft = sizeof(*fxp_state);
if(!(fxp_state = alloc_contig(ft, 0, NULL)))
panic("couldn't allocate table: %d", ENOMEM);
memset(fxp_state, 0, ft);
if((r=tsc_calibrate()) != OK)
panic("tsc_calibrate failed: %d", r);
/* Announce we are up! */
netdriver_announce();
return(OK);
}
/*===========================================================================*
* sef_cb_signal_handler *
*===========================================================================*/
static void sef_cb_signal_handler(int signo)
{
port_t port;
fxp_t *fp;
/* Only check for termination signal, ignore anything else. */
if (signo != SIGTERM) return;
fp= fxp_state;
if (fp->fxp_mode == FM_ENABLED && (fp->fxp_flags & FF_ENABLED)) {
port= fp->fxp_base_port;
/* Reset device */
if (debug)
printf("%s: resetting device\n", fp->fxp_name);
fxp_outl(port, CSR_PORT, CP_CMD_SOFT_RESET);
}
exit(0);
}
/*===========================================================================*
* fxp_init *
*===========================================================================*/
static void fxp_init(mp)
message *mp;
{
static int first_time= 1;
fxp_t *fp;
message reply_mess;
if (first_time)
{
first_time= 0;
fxp_pci_conf(); /* Configure PCI devices. */
init_timer(&fxp_watchdog);
set_timer(&fxp_watchdog, system_hz, fxp_watchdog_f, 0);
}
fp= fxp_state;
if (fp->fxp_mode == FM_DISABLED)
{
/* This is the default, try to (re)locate the device. */
fxp_conf_hw(fp);
if (fp->fxp_mode == FM_DISABLED)
{
/* Probe failed, or the device is configured off. */
reply_mess.m_type= DL_CONF_REPLY;
reply_mess.m_netdrv_net_dl_conf.stat= ENXIO;
mess_reply(mp, &reply_mess);
return;
}
if (fp->fxp_mode == FM_ENABLED)
fxp_init_hw(fp);
fxp_report_link(fp);
}
assert(fp->fxp_mode == FM_ENABLED);
assert(fp->fxp_flags & FF_ENABLED);
fp->fxp_flags &= ~(FF_PROMISC | FF_MULTI | FF_BROAD);
if (mp->m_net_netdrv_dl_conf.mode & DL_PROMISC_REQ)
fp->fxp_flags |= FF_PROMISC;
if (mp->m_net_netdrv_dl_conf.mode & DL_MULTI_REQ)
fp->fxp_flags |= FF_MULTI;
if (mp->m_net_netdrv_dl_conf.mode & DL_BROAD_REQ)
fp->fxp_flags |= FF_BROAD;
fxp_rec_mode(fp);
reply_mess.m_type = DL_CONF_REPLY;
reply_mess.m_netdrv_net_dl_conf.stat = OK;
memcpy(reply_mess.m_netdrv_net_dl_conf.hw_addr,
fp->fxp_address.ea_addr,
sizeof(reply_mess.m_netdrv_net_dl_conf.hw_addr));
mes
10BC8
s_reply(mp, &reply_mess);
}
/*===========================================================================*
* fxp_pci_conf *
*===========================================================================*/
static void fxp_pci_conf()
{
fxp_t *fp;
fp= fxp_state;
strlcpy(fp->fxp_name, "fxp#0", sizeof(fp->fxp_name));
fp->fxp_name[4] += fxp_instance;
fp->fxp_seen= FALSE;
fp->fxp_features= FFE_NONE;
pci_init();
if (fxp_probe(fp, fxp_instance))
fp->fxp_seen= TRUE;
}
/*===========================================================================*
* fxp_probe *
*===========================================================================*/
static int fxp_probe(fxp_t *fp, int skip)
{
int r, devind;
u16_t vid, did;
u32_t bar;
u8_t ilr, rev;
char *str;
#if VERBOSE
char *dname;
#endif
r= pci_first_dev(&devind, &vid, &did);
if (r == 0)
return FALSE;
while (skip--)
{
r= pci_next_dev(&devind, &vid, &did);
if (!r)
return FALSE;
}
#if VERBOSE
dname= pci_dev_name(vid, did);
if (!dname)
dname= "unknown device";
printf("%s: %s (%04x/%04x) at %s\n",
fp->fxp_name, dname, vid, did, pci_slot_name(devind));
#endif
pci_reserve(devind);
bar= pci_attr_r32(devind, PCI_BAR_2) & 0xffffffe0;
if (bar < 0x400) {
panic("fxp_probe: base address is not properly configured");
}
fp->fxp_base_port= bar;
ilr= pci_attr_r8(devind, PCI_ILR);
fp->fxp_irq= ilr;
if (debug)
{
printf("%s: using I/O address 0x%lx, IRQ %d\n",
fp->fxp_name, (unsigned long)bar, ilr);
}
rev= pci_attr_r8(devind, PCI_REV);
str= NULL;
fp->fxp_type= FT_UNKNOWN;
switch(rev)
{
case FXP_REV_82557A: str= "82557A"; /* 0x01 */
fp->fxp_type= FT_82557;
break;
case FXP_REV_82557B: str= "82557B"; break; /* 0x02 */
case FXP_REV_82557C: str= "82557C"; break; /* 0x03 */
case FXP_REV_82558A: str= "82558A"; /* 0x04 */
fp->fxp_type= FT_82558A;
break;
case FXP_REV_82558B: str= "82558B"; /* 0x05 */
fp->fxp_type= FT_82559;
break;
case FXP_REV_82559A: str= "82559A"; break; /* 0x06 */
case FXP_REV_82559B: str= "82559B"; break; /* 0x07 */
case FXP_REV_82559C: str= "82559C"; /* 0x08 */
fp->fxp_type= FT_82559;
break;
case FXP_REV_82559ERA: str= "82559ER-A"; /* 0x09 */
fp->fxp_type= FT_82559;
break;
case FXP_REV_82550_1: str= "82550(1)"; /* 0x0C */
fp->fxp_type= FT_82559;
break;
case FXP_REV_82550_2: str= "82550(2)"; /* 0x0D */
fp->fxp_type= FT_82559;
break;
case FXP_REV_82550_3: str= "82550(3)"; /* 0x0E */
fp->fxp_type= FT_82559;
break;
case FXP_REV_82551_1: str= "82551(1)"; /* 0x0F */
fp->fxp_type= FT_82559;
break;
case FXP_REV_82551_2: str= "82551(2)"; /* 0x10 */
fp->fxp_type= FT_82559;
break;
case FXP_REV_82801CAM: str= "82801CAM"; /* 0x42 */
fp->fxp_type= FT_82801;
break;
case FXP_REV_82801DB: str= "82801DB"; /* 0x81 */
fp->fxp_type= FT_82801;
break;
case FXP_REV_82550_4: str= "82550(4)"; /* 0x83 */
fp->fxp_type= FT_82559;
break;
}
#if VERBOSE
if (str)
printf("%s: device revision: %s\n", fp->fxp_name, str);
else
printf("%s: unknown revision: 0x%x\n", fp->fxp_name, rev);
#endif
if (fp->fxp_type == FT_UNKNOWN)
{
printf("fxp_probe: device is not supported by this driver\n");
return FALSE;
}
return TRUE;
}
/*===========================================================================*
* fxp_conf_hw *
*===========================================================================*/
static void fxp_conf_hw(fxp_t *fp)
{
#if VERBOSE
int i;
#endif
fp->fxp_mode= FM_DISABLED; /* Superfluous */
if (!fp->fxp_seen)
return;
/* PCI device is present */
fp->fxp_mode= FM_ENABLED;
fp->fxp_flags= FF_EMPTY;
fp->fxp_got_int= 0;
fp->fxp_send_int= 0;
fp->fxp_ee_addrlen= 0; /* Unknown */
fp->fxp_need_reset= 0;
fp->fxp_report_link= 0;
fp->fxp_link_up= -1; /* Unknown */
fp->fxp_mii_busy= 0;
fp->fxp_read_s= 0;
fp->fxp_rx_need_restart= 0;
fp->fxp_need_conf= 0;
fp->fxp_tx_head= 0;
fp->fxp_tx_tail= 0;
fp->fxp_tx_alive= 0;
fp->fxp_tx_threshold= TXTT_MIN;
/* Try to come up with a sensible configuration for the current
* device. Unfortunately every device is different, defaults are
* not always zero, and some fields are re-used with a completely
* different interpretation. We start out with a sensible default
* for all devices and then add device specific changes.
*/
fp->fxp_conf_bytes[0]= CC_BYTES_NR;
fp->fxp_conf_bytes[1]= CTL_DEFAULT | CRL_DEFAULT;
fp->fxp_conf_bytes[2]= CAI_DEFAULT;
fp->fxp_conf_bytes[3]= 0;
fp->fxp_conf_bytes[4]= 0;
fp->fxp_conf_bytes[5]= 0;
fp->fxp_conf_bytes[6]= CCB6_ESC | CCB6_ETCB | CCB6_RES;
fp->fxp_conf_bytes[7]= CUR_1;
fp->fxp_conf_bytes[8]= CCB8_503_MII;
fp->fxp_conf_bytes[9]= 0;
fp->fxp_conf_bytes[10]= CLB_NORMAL | CPAL_DEFAULT | CCB10_NSAI |
CCB10_RES1;
fp->fxp_conf_bytes[11]= 0;
fp->fxp_conf_bytes[12]= CIS_DEFAULT;
fp->fxp_conf_bytes[13]= CCB13_DEFAULT;
fp->fxp_conf_bytes[14]= CCB14_DEFAULT;
fp->fxp_conf_bytes[15]= CCB15_RES1 | CCB15_RES2;
fp->fxp_conf_bytes[16]= CCB16_DEFAULT;
fp->fxp_conf_bytes[17]= CCB17_DEFAULT;
fp->fxp_conf_bytes[18]= CCB18_RES1 | CCB18_PFCT | CCB18_PE;
fp->fxp_conf_bytes[19]= CCB19_FDPE;
fp->fxp_conf_bytes[20]= CCB20_PFCL | CCB20_RES1;
fp->fxp_conf_bytes[21]= CCB21_RES21;
#if VERBOSE
for (i= 0; i<CC_BYTES_NR; i++)
printf("%d: %0x, ", i, fp->fxp_conf_bytes[i]);
printf("\n");
#endif
switch(fp->fxp_type)
{
case FT_82557:
break;
case FT_82558A:
case FT_82559:
case FT_82801:
fp->fxp_conf_bytes[18] |= CCB18_LROK;
if (fp->fxp_type == FT_82801)
{
fp->fxp_conf_bytes[6] = 0xba; /* ctrl 1 */
fp->fxp_conf_bytes[15] = 0x48; /* promiscuous */
fp->fxp_conf_bytes[21] = 0x05; /* mc_all */
}
break;
default:
panic("fxp_conf_hw: bad device type: %d", fp->fxp_type);
}
/* Assume an 82555 (compatible) PHY. The should be changed for
* 82557 NICs with different PHYs
*/
fp->fxp_ms_regs = 0; /* No master/slave registers. */
#if VERBOSE
for (i= 0; i<CC_BYTES_NR; i++)
printf("%d: %0x, ", i, fp->fxp_conf_bytes[i]);
printf("\n");
#endif
}
/*===========================================================================*
* fxp_init_hw *
*===========================================================================*/
static void fxp_init_hw(fp)
fxp_t *fp;
{
int i, r, isr;
port_t port;
phys_bytes bus_addr;
port= fp->fxp_base_port;
fxp_init_buf(fp);
fp->fxp_flags = FF_EMPTY;
fp->fxp_flags |= FF_ENABLED;
/* Set the interrupt handler and policy. Do not automatically
* reenable interrupts. Return the IRQ line number on interrupts.
*/
fp->fxp_hook = fp->fxp_irq;
r= sys_irqsetpolicy(fp->fxp_irq, 0, &fp->fxp_hook);
if (r != OK)
panic("sys_irqsetpolicy failed: %d", r);
fxp_reset_hw(fp);
r= sys_irqenable(&fp->fxp_hook);
if (r != OK)
panic("sys_irqenable failed: %d", r);
/* Reset PHY? */
fxp_do_conf(fp);
/* Set pointer to statistical counters */
r= sys_umap(SELF, VM_D, (vir_bytes)&fp->fxp_stat, sizeof(fp->fxp_sta
10BC8
t),
&bus_addr);
if (r != OK)
panic("sys_umap failed: %d", r);
fxp_cu_ptr_cmd(fp, SC_CU_LOAD_DCA, bus_addr, TRUE /* check idle */);
/* Ack previous interrupts */
isr= fxp_inb(port, SCB_INT_STAT);
fxp_outb(port, SCB_INT_STAT, isr);
/* Enable interrupts */
fxp_outb(port, SCB_INT_MASK, 0);
fxp_ru_ptr_cmd(fp, SC_RU_START, fp->fxp_rx_busaddr,
TRUE /* check idle */);
fxp_confaddr(fp);
if (debug)
{
printf("%s: Ethernet address ", fp->fxp_name);
for (i= 0; i < 6; i++)
{
printf("%x%c", fp->fxp_address.ea_addr[i],
i < 5 ? ':' : '\n');
}
}
}
/*===========================================================================*
* fxp_init_buf *
*===========================================================================*/
static void fxp_init_buf(fp)
fxp_t *fp;
{
size_t rx_totbufsize, tx_totbufsize, tot_bufsize, alloc_bufsize;
char *alloc_buf;
phys_bytes buf, bus_addr;
int i, r;
struct rfd *rfdp;
struct tx *txp;
phys_bytes ph;
fp->fxp_rx_nbuf= N_RX_BUF;
rx_totbufsize= fp->fxp_rx_nbuf * sizeof(struct rfd);
fp->fxp_rx_bufsize= rx_totbufsize;
fp->fxp_tx_nbuf= N_TX_BUF;
tx_totbufsize= fp->fxp_tx_nbuf * sizeof(struct tx);
fp->fxp_tx_bufsize= tx_totbufsize;
tot_bufsize= sizeof(*tmpbufp) + tx_totbufsize + rx_totbufsize;
if (tot_bufsize % 4096)
tot_bufsize += 4096 - (tot_bufsize % 4096);
alloc_bufsize= tot_bufsize;
alloc_buf= alloc_contig(alloc_bufsize, AC_ALIGN4K, &ph);
if (alloc_buf == NULL) {
panic("fxp_init_buf: unable to alloc_contig size: %d", alloc_bufsize);
}
buf= (phys_bytes)alloc_buf;
tell_dev((vir_bytes)buf, tot_bufsize, 0, 0, 0);
tmpbufp= (union tmpbuf *)buf;
fp->fxp_rx_buf= (struct rfd *)&tmpbufp[1];
r= sys_umap(SELF, VM_D, (vir_bytes)fp->fxp_rx_buf, rx_totbufsize,
&bus_addr);
if (r != OK)
panic("sys_umap failed: %d", r);
fp->fxp_rx_busaddr= bus_addr;
#if 0
printf("fxp_init_buf: got phys 0x%x for vir 0x%x\n",
fp->fxp_rx_busaddr, fp->fxp_rx_buf);
#endif
for (i= 0, rfdp= fp->fxp_rx_buf; i<fp->fxp_rx_nbuf; i++, rfdp++)
{
rfdp->rfd_status= 0;
rfdp->rfd_command= 0;
if (i != fp->fxp_rx_nbuf-1)
{
r= sys_umap(SELF, VM_D, (vir_bytes)&rfdp[1],
sizeof(rfdp[1]), &bus_addr);
if (r != OK)
panic("sys_umap failed: %d", r);
rfdp->rfd_linkaddr= bus_addr;
}
else
{
rfdp->rfd_linkaddr= fp->fxp_rx_busaddr;
rfdp->rfd_command |= RFDC_EL;
}
rfdp->rfd_reserved= 0;
rfdp->rfd_res= 0;
rfdp->rfd_size= sizeof(rfdp->rfd_buf);
}
fp->fxp_rx_head= 0;
fp->fxp_tx_buf= (struct tx *)((char *)fp->fxp_rx_buf+rx_totbufsize);
r= sys_umap(SELF, VM_D, (vir_bytes)fp->fxp_tx_buf,
(phys_bytes)tx_totbufsize, &fp->fxp_tx_busaddr);
if (r != OK)
panic("sys_umap failed: %d", r);
for (i= 0, txp= fp->fxp_tx_buf; i<fp->fxp_tx_nbuf; i++, txp++)
{
txp->tx_status= 0;
txp->tx_command= TXC_EL | CBL_NOP; /* Just in case */
if (i != fp->fxp_tx_nbuf-1)
{
r= sys_umap(SELF, VM_D, (vir_bytes)&txp[1],
(phys_bytes)sizeof(txp[1]), &bus_addr);
if (r != OK)
panic("sys_umap failed: %d", r);
txp->tx_linkaddr= bus_addr;
}
else
{
txp->tx_linkaddr= fp->fxp_tx_busaddr;
}
txp->tx_tbda= TX_TBDA_NIL;
txp->tx_size= 0;
txp->tx_tthresh= fp->fxp_tx_threshold;
txp->tx_ntbd= 0;
}
fp->fxp_tx_idle= 1;
}
/*===========================================================================*
* fxp_reset_hw *
*===========================================================================*/
static void fxp_reset_hw(fp)
fxp_t *fp;
{
/* Inline the function in init? */
port_t port;
port= fp->fxp_base_port;
/* Reset device */
fxp_outl(port, CSR_PORT, CP_CMD_SOFT_RESET);
tickdelay(micros_to_ticks(CSR_PORT_RESET_DELAY));
/* Disable interrupts */
fxp_outb(port, SCB_INT_MASK, SIM_M);
/* Set CU base to zero */
fxp_cu_ptr_cmd(fp, SC_CU_LOAD_BASE, 0, TRUE /* check idle */);
/* Set RU base to zero */
fxp_ru_ptr_cmd(fp, SC_RU_LOAD_BASE, 0, TRUE /* check idle */);
}
/*===========================================================================*
* fxp_confaddr *
*===========================================================================*/
static void fxp_confaddr(fxp_t *fp)
{
static char eakey[]= FXP_ENVVAR "#_EA";
static char eafmt[]= "x:x:x:x:x:x";
int i, r;
phys_bytes bus_addr;
long v;
/* User defined ethernet address? */
eakey[sizeof(FXP_ENVVAR)-1]= '0' + fxp_instance;
for (i= 0; i < 6; i++)
{
if (env_parse(eakey, eafmt, i, &v, 0x00L, 0xFFL) != EP_SET)
break;
fp->fxp_address.ea_addr[i]= v;
}
if (i != 0 && i != 6) env_panic(eakey); /* It's all or nothing */
if (i == 0)
{
/* Get ethernet address from EEPROM */
for (i= 0; i<3; i++)
{
v= eeprom_read(fp, i);
fp->fxp_address.ea_addr[i*2]= (v & 0xff);
fp->fxp_address.ea_addr[i*2+1]= ((v >> 8) & 0xff);
}
}
/* Tell NIC about ethernet address */
tmpbufp->ias.ias_status= 0;
tmpbufp->ias.ias_command= CBL_C_EL | CBL_AIS;
tmpbufp->ias.ias_linkaddr= 0;
memcpy(tmpbufp->ias.ias_ethaddr, fp->fxp_address.ea_addr,
sizeof(tmpbufp->ias.ias_ethaddr));
r= sys_umap(SELF, VM_D, (vir_bytes)&tmpbufp->ias,
(phys_bytes)sizeof(tmpbufp->ias), &bus_addr);
if (r != OK)
panic("sys_umap failed: %d", r);
fxp_cu_ptr_cmd(fp, SC_CU_START, bus_addr, TRUE /* check idle */);
/* Wait for CU command to complete */
SPIN_UNTIL(tmpbufp->ias.ias_status & CBL_F_C, 1000);
if (!(tmpbufp->ias.ias_status & CBL_F_C))
panic("fxp_confaddr: CU command failed to complete");
if (!(tmpbufp->ias.ias_status & CBL_F_OK))
panic("fxp_confaddr: CU command failed");
#if VERBOSE
printf("%s: hardware ethernet address: ", fp->fxp_name);
for (i= 0; i<6; i++)
{
printf("%02x%s", fp->fxp_address.ea_addr[i],
i < 5 ? ":" : "");
}
printf("\n");
#endif
}
/*===========================================================================*
* fxp_rec_mode *
*===========================================================================*/
static void fxp_rec_mode(fp)
fxp_t *fp;
{
fp->fxp_conf_bytes[0]= CC_BYTES_NR; /* Just to be sure */
fp->fxp_conf_bytes[15] &= ~(CCB15_BD|CCB15_PM);
fp->fxp_conf_bytes[21] &= ~CCB21_MA;
if (fp->fxp_flags & FF_PROMISC)
fp->fxp_conf_bytes[15] |= CCB15_PM;
if (fp->fxp_flags & FF_MULTI)
fp->fxp_conf_bytes[21] |= CCB21_MA;
if (!(fp->fxp_flags & (FF_BROAD|FF_MULTI|FF_PROMISC)))
fp->fxp_conf_bytes[15] |= CCB15_BD;
/* Queue request if not idle */
if (fp->fxp_tx_idle)
{
fxp_do_conf(fp);
}
else
{
printf("fxp_rec_mode: setting fxp_need_conf\n");
fp->fxp_need_conf= TRUE;
}
}
/*===========================================================================*
* fxp_writev_s *
*===========================================================================*/
static void fxp_writev_s(const message *mp, int from_int)
{
endpoint_t iov_endpt;
cp_grant_id_t iov_grant;
vir_bytes iov_offset;
int i, j, n, o, r, s, count, size, prev_head;
int fxp_tx_nbuf, fxp_tx_head;
u16_t tx_command;
fxp_t *fp;
iovec_s_t *iovp;
struct tx *txp, *prev_txp;
fp= fxp_state;
count = mp->m_net_netdrv_dl_writev_s.count;
fp->fxp_client= mp->m_source;
assert(fp->fxp_mode == FM_ENABLED);
assert(fp->fxp_flags & FF_ENABLED);
if (from_int)
{
assert(fp->fxp_flags & FF_SEND_AVAIL);
fp->fxp_flags &= ~FF_SEND_AVAIL;
fp->fxp_tx_alive= TRUE;
}
if (fp->fxp_tx_idle)
{
txp= fp->fxp_tx_buf;
fxp_tx_head= 0; /* lint */
prev_txp= NULL; /* lint */
}
else
{
fxp_tx_nbuf= fp->fxp_tx_nbuf;
prev_head= fp->fxp_tx_head;
fxp_tx_head= prev_head+1;
if (fxp_tx_head == fxp_tx_nbuf)
fxp_tx_head= 0;
assert(fxp_tx_head < fxp_tx_nbuf);
if (fxp_tx_head == fp->fxp_tx_tail)
{
/* Send queue is full */
assert(!(fp->fxp_flags & FF_SEND_AVAIL));
fp->fxp_flags |= FF_SEND_AVAIL;
goto suspend;
}
prev_txp= &fp->fxp_tx_buf[prev_head];
txp= &fp->fxp_tx_buf[fxp_tx_head];
}
assert(!(fp->fxp_flags & FF_SEND_AVAIL));
assert(!(fp->fxp_flags & FF_PACK_SENT));
iov_endpt= mp->m_source;
iov_grant= mp->m_net_netdrv_dl_writev_s.grant;
size= 0;
o= 0;
iov_offset= 0;
for (i= 0; i<count; i += IOVEC_NR,
iov_offset += IOVEC_NR * sizeof(fp->fxp_iovec_s[0]))
{
n= IOVEC_NR;
if (i+n > count)
n= count-i;
r= sys_safecopyfrom(iov_endpt, iov_grant, iov_offset,
(vir_bytes)fp->fxp_iovec_s,
n * sizeof(fp->fxp_iovec_s[0]));
if (r != OK)
panic("fxp_writev: sys_safecopyfrom failed: %d", r);