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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
|
/* $OpenBSD: if_nx.c,v 1.26 2007/04/30 21:22:56 reyk Exp $ */
/*
* Copyright (c) 2007 Reyk Floeter <reyk@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Driver for the NetXen NX2031/NX2035 10Gb and Gigabit Ethernet chipsets,
* see http://www.netxen.com/.
*
* This driver was made possible because NetXen Inc. provided hardware
* and documentation. Thanks!
*/
#include "bpfilter.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/malloc.h>
#include <sys/timeout.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <machine/intr.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_ether.h>
#endif
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/if_nxreg.h>
#ifdef NX_DEBUG
#define NXDBG_FLASH (1<<0) /* debug flash access through ROMUSB */
#define NXDBG_WAIT (1<<1) /* poll registers */
#define NXDBG_ALL 0xffff /* enable all debugging messages */
int nx_debug = 0;
#define DPRINTF(_lvl, _arg...) do { \
if (nx_debug & (_lvl)) \
printf(_arg); \
} while (0)
#define DPRINTREG(_lvl, _reg) do { \
if (nx_debug & (_lvl)) \
printf("%s: 0x%08x: %08x\n", \
#_reg, _reg, nxb_read(sc, _reg)); \
} while (0)
#else
#define DPRINTREG(_lvl, _reg)
#define DPRINTF(_lvl, arg...)
#endif
#ifdef notyet
/*
* The NetXen firmware and bootloader is about 800k big, don't even try
* to load the alternative version from disk with small kernels used by
* the install media. The driver can still try to use the primary firmware
* and bootloader found in the controller's flash memory.
*/
#ifndef SMALL_KERNEL
#define NXB_LOADFIRMWARE
#endif
#endif
struct nx_softc;
struct nxb_port {
u_int8_t nxp_id;
u_int8_t nxp_mode;
u_int8_t nxp_lladdr[ETHER_ADDR_LEN];
struct nx_softc *nxp_nx;
};
struct nxb_softc {
struct device sc_dev;
pci_chipset_tag_t sc_pc;
pcitag_t sc_tag;
u_int sc_function;
bus_dma_tag_t sc_dmat;
bus_space_tag_t sc_memt;
bus_space_handle_t sc_memh;
bus_size_t sc_mems;
bus_space_tag_t sc_dbmemt;
bus_space_handle_t sc_dbmemh;
bus_size_t sc_dbmems;
pci_intr_handle_t sc_ih;
u_int sc_flags;
#define NXFLAG_FWINVALID (1<<0) /* update firmware from disk */
struct nxb_info sc_nxbinfo; /* Information from flash */
struct nxb_imageinfo sc_nxbimage; /* Image info from flash */
int sc_window; /* SW memory window */
int sc_state; /* Firmware state */
u_int32_t sc_fwmajor; /* Load image major rev */
u_int32_t sc_fwminor; /* Load image minor rev */
u_int32_t sc_fwbuild; /* Load image build rev */
u_int32_t sc_nrxbuf;
u_int32_t sc_ntxbuf;
volatile u_int sc_txpending;
struct nxb_port sc_nxp[NX_MAX_PORTS]; /* The nx ports */
int sc_nports;
};
struct nx_softc {
struct device nx_dev;
struct arpcom nx_ac;
struct mii_data nx_mii;
struct nxb_softc *nx_sc; /* The nxb board */
struct nxb_port *nx_port; /* Port information */
void *nx_ih;
struct timeout nx_tick;
};
int nxb_match(struct device *, void *, void *);
void nxb_attach(struct device *, struct device *, void *);
int nxb_query(struct nxb_softc *sc);
int nxb_booted(struct nxb_softc *sc);
void nxb_mountroot(void *);
int nxb_loadfirmware(struct nxb_softc *, struct nxb_firmware_header *,
u_int8_t **, size_t *);
int nxb_reloadfirmware(struct nxb_softc *, struct nxb_firmware_header *,
u_int8_t **, size_t *);
void nxb_reset(struct nxb_softc *);
u_int32_t nxb_read(struct nxb_softc *, bus_size_t);
void nxb_write(struct nxb_softc *, bus_size_t, u_int32_t);
void nxb_set_window(struct nxb_softc *, int);
int nxb_wait(struct nxb_softc *, bus_size_t, u_int32_t, u_int32_t,
int, u_int);
int nxb_read_rom(struct nxb_softc *, u_int32_t, u_int32_t *);
int nx_match(struct device *, void *, void *);
void nx_attach(struct device *, struct device *, void *);
int nx_print(void *, const char *);
int nx_media_change(struct ifnet *);
void nx_media_status(struct ifnet *, struct ifmediareq *);
void nx_link_state(struct nx_softc *);
void nx_init(struct ifnet *);
void nx_start(struct ifnet *);
void nx_stop(struct ifnet *);
void nx_watchdog(struct ifnet *);
int nx_ioctl(struct ifnet *, u_long, caddr_t);
void nx_iff(struct nx_softc *);
void nx_tick(void *);
int nx_intr(void *);
struct cfdriver nxb_cd = {
0, "nxb", DV_DULL
};
struct cfattach nxb_ca = {
sizeof(struct nxb_softc), nxb_match, nxb_attach
};
struct cfdriver nx_cd = {
0, "nx", DV_IFNET
};
struct cfattach nx_ca = {
sizeof(struct nx_softc), nx_match, nx_attach
};
const struct pci_matchid nxb_devices[] = {
{ PCI_VENDOR_NETXEN, PCI_PRODUCT_NETXEN_NXB_10GXxR },
{ PCI_VENDOR_NETXEN, PCI_PRODUCT_NETXEN_NXB_10GCX4 },
{ PCI_VENDOR_NETXEN, PCI_PRODUCT_NETXEN_NXB_4GCU },
{ PCI_VENDOR_NETXEN, PCI_PRODUCT_NETXEN_NXB_IMEZ },
{ PCI_VENDOR_NETXEN, PCI_PRODUCT_NETXEN_NXB_HMEZ },
{ PCI_VENDOR_NETXEN, PCI_PRODUCT_NETXEN_NXB_IMEZ_2 },
{ PCI_VENDOR_NETXEN, PCI_PRODUCT_NETXEN_NXB_HMEZ_2 }
};
const struct nxb_board {
enum nxb_board_types brd_type;
u_int brd_mode;
u_int brd_nports;
} nxb_boards[] = {
{ NXB_BOARDTYPE_P2SB35_4G, NXNIU_MODE_GBE, 4 },
{ NXB_BOARDTYPE_P2SB31_10G, NXNIU_MODE_XGE, 1 },
{ NXB_BOARDTYPE_P2SB31_2G, NXNIU_MODE_GBE, 2 },
{ NXB_BOARDTYPE_P2SB31_10GIMEZ, NXNIU_MODE_XGE, 2 },
{ NXB_BOARDTYPE_P2SB31_10GHMEZ, NXNIU_MODE_XGE, 2 },
{ NXB_BOARDTYPE_P2SB31_10GCX4, NXNIU_MODE_XGE, 1 }
};
extern int ifqmaxlen;
/*
* Routines handling the physical ''nxb'' board
*/
int
nxb_match(struct device *parent, void *match, void *aux)
{
return (pci_matchbyid((struct pci_attach_args *)aux,
nxb_devices, sizeof(nxb_devices) / sizeof(nxb_devices[0])));
}
void
nxb_attach(struct device *parent, struct device *self, void *aux)
{
struct nxb_softc *sc = (struct nxb_softc *)self;
struct pci_attach_args *pa = aux;
pcireg_t memtype;
const char *intrstr;
bus_size_t pcisize;
paddr_t pciaddr;
int i;
sc->sc_pc = pa->pa_pc;
sc->sc_tag = pa->pa_tag;
sc->sc_dmat = pa->pa_dmat;
sc->sc_function = pa->pa_function;
sc->sc_window = -1;
/*
* The NetXen NICs can have different PCI memory layouts which
* need some special handling in the driver. Support is limited
* to 32bit 128MB memory for now (the chipset uses a configurable
* window to access the complete memory range).
*/
memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, NXBAR0);
switch (memtype) {
case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
break;
case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
default:
printf(": invalid memory type: 0x%x\n", memtype);
return;
}
if (pci_mapreg_info(sc->sc_pc, sc->sc_tag, NXBAR0,
memtype, &pciaddr, &pcisize, NULL)) {
printf(": failed to get pci info\n");
return;
}
switch (pcisize) {
case NXPCIMEM_SIZE_128MB:
break;
case NXPCIMEM_SIZE_32MB:
default:
printf(": invalid memory size: %ld\n", pcisize);
return;
}
/* Finally map the PCI memory space */
if (pci_mapreg_map(pa, NXBAR0, memtype, 0, &sc->sc_memt,
&sc->sc_memh, NULL, &sc->sc_mems, 0) != 0) {
printf(": unable to map register memory\n");
return;
}
if (pci_mapreg_map(pa, NXBAR4, memtype, 0, &sc->sc_dbmemt,
&sc->sc_dbmemh, NULL, &sc->sc_dbmems, 0) != 0) {
printf(": unable to map doorbell memory\n");
goto unmap1;
}
/* Get the board information and initialize the h/w */
if (nxb_query(sc) != 0)
goto unmap;
/* Map the interrupt, the handlers will be attached later */
if (pci_intr_map(pa, &sc->sc_ih) != 0) {
printf(": unable to map interrupt\n");
goto unmap;
}
intrstr = pci_intr_string(pa->pa_pc, sc->sc_ih);
printf(": %s\n", intrstr);
for (i = 0; i < sc->sc_nports; i++)
config_found(&sc->sc_dev, &sc->sc_nxp[i], nx_print);
mountroothook_establish(nxb_mountroot, sc);
return;
unmap:
bus_space_unmap(sc->sc_dbmemt, sc->sc_dbmemh, sc->sc_dbmems);
sc->sc_dbmems = 0;
unmap1:
bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
sc->sc_mems = 0;
}
int
nxb_query(struct nxb_softc *sc)
{
struct nxb_info *ni = &sc->sc_nxbinfo;
struct nxb_userinfo *nu;
u_int32_t *data, addr;
u_int8_t *ptr;
const struct nxb_board *board = NULL;
u_int i, j, len;
nxb_set_window(sc, 1);
/*
* Get the board information from flash memory
*/
addr = NXFLASHMAP_INFO;
len = sizeof(*ni) / sizeof(u_int32_t);
data = (u_int32_t *)ni;
for (i = 0; i < len; i++) {
if (nxb_read_rom(sc, addr, data) != 0) {
printf(": failed to get board info from flash\n");
return (-1);
}
addr += sizeof(u_int32_t);
data++;
}
#ifdef NX_DEBUG
#define _NXBINFO(_e) do { \
if (nx_debug & NXDBG_FLASH) \
printf("%s: %s: 0x%08x (%u)\n", \
sc->sc_dev.dv_xname, #_e, ni->_e, ni->_e); \
} while (0)
_NXBINFO(ni_hdrver);
_NXBINFO(ni_board_mfg);
_NXBINFO(ni_board_type);
_NXBINFO(ni_board_num);
_NXBINFO(ni_chip_id);
_NXBINFO(ni_chip_minor);
_NXBINFO(ni_chip_major);
_NXBINFO(ni_chip_pkg);
_NXBINFO(ni_chip_lot);
_NXBINFO(ni_port_mask);
_NXBINFO(ni_peg_mask);
_NXBINFO(ni_icache);
_NXBINFO(ni_dcache);
_NXBINFO(ni_casper);
_NXBINFO(ni_lladdr0_low);
_NXBINFO(ni_lladdr1_low);
_NXBINFO(ni_lladdr2_low);
_NXBINFO(ni_lladdr3_low);
_NXBINFO(ni_mnsync_mode);
_NXBINFO(ni_mnsync_shift_cclk);
_NXBINFO(ni_mnsync_shift_mclk);
_NXBINFO(ni_mnwb_enable);
_NXBINFO(ni_mnfreq_crystal);
_NXBINFO(ni_mnfreq_speed);
_NXBINFO(ni_mnorg);
_NXBINFO(ni_mndepth);
_NXBINFO(ni_mnranks0);
_NXBINFO(ni_mnranks1);
_NXBINFO(ni_mnrd_latency0);
_NXBINFO(ni_mnrd_latency1);
_NXBINFO(ni_mnrd_latency2);
_NXBINFO(ni_mnrd_latency3);
_NXBINFO(ni_mnrd_latency4);
_NXBINFO(ni_mnrd_latency5);
_NXBINFO(ni_mnrd_latency6);
_NXBINFO(ni_mnrd_latency7);
_NXBINFO(ni_mnrd_latency8);
_NXBINFO(ni_mndll[0]);
_NXBINFO(ni_mnddr_mode);
_NXBINFO(ni_mnddr_extmode);
_NXBINFO(ni_mntiming0);
_NXBINFO(ni_mntiming1);
_NXBINFO(ni_mntiming2);
_NXBINFO(ni_snsync_mode);
_NXBINFO(ni_snpt_mode);
_NXBINFO(ni_snecc_enable);
_NXBINFO(ni_snwb_enable);
_NXBINFO(ni_snfreq_crystal);
_NXBINFO(ni_snfreq_speed);
_NXBINFO(ni_snorg);
_NXBINFO(ni_sndepth);
_NXBINFO(ni_sndll);
_NXBINFO(ni_snrd_latency);
_NXBINFO(ni_lladdr0_high);
_NXBINFO(ni_lladdr1_high);
_NXBINFO(ni_lladdr2_high);
_NXBINFO(ni_lladdr3_high);
_NXBINFO(ni_magic);
_NXBINFO(ni_mnrd_imm);
_NXBINFO(ni_mndll_override);
#undef _NXBINFO
#endif /* NX_DEBUG */
/* Validate the board information from flash */
if (ni->ni_hdrver != NXB_VERSION) {
printf(": unsupported flash info header version %u\n",
ni->ni_hdrver);
return (-1);
}
if (ni->ni_magic != NXB_MAGIC) {
printf(": flash info magic value mismatch\n");
return (-1);
}
/* Lookup the board */
for (i = 0; i < (sizeof(nxb_boards) / sizeof(nxb_boards[0])); i++) {
if (ni->ni_board_type == nxb_boards[i].brd_type) {
board = &nxb_boards[i];
break;
}
}
if (board == NULL) {
printf(": unsupported board type %u\n", ni->ni_board_type);
return (-1);
}
/* Configure the ports */
sc->sc_nports = board->brd_nports;
for (i = 0; i < sc->sc_nports; i++) {
sc->sc_nxp[i].nxp_id = i;
sc->sc_nxp[i].nxp_mode = board->brd_mode;
}
/*
* Get the user information from flash memory
*/
if ((nu = (struct nxb_userinfo *)
malloc(sizeof(*nu), M_TEMP, M_NOWAIT)) == NULL) {
printf(": failed to allocate user info\n");
return (-1);
}
addr = NXFLASHMAP_USER;
len = sizeof(*nu) / sizeof(u_int32_t);
data = (u_int32_t *)nu;
for (i = 0; i < len; i++) {
if (nxb_read_rom(sc, addr, data) != 0) {
printf(": failed to get user info from flash\n");
free(nu, M_TEMP);
return (-1);
}
addr += sizeof(u_int32_t);
data++;
}
/* Copy the MAC addresses */
for (i = 0; i < sc->sc_nports; i++) {
ptr = (u_int8_t *)
&nu->nu_lladdr[i * NXB_MAX_PORT_LLADDRS];
/* MAC address bytes are stored in a swapped order */
for (j = 0; j < ETHER_ADDR_LEN; j++)
sc->sc_nxp[i].nxp_lladdr[j] =
ptr[ETHER_ADDR_LEN - (j + 1)];
}
/* Make sure that the serial number is a NUL-terminated string */
nu->nu_serial_num[31] = '\0';
/* Copy flash image information */
bcopy(&nu->nu_image, &sc->sc_nxbimage, sizeof(sc->sc_nxbimage));
#ifdef NX_DEBUG
#define _NXBUSER(_e) do { \
if (nx_debug & NXDBG_FLASH) \
printf("%s: %s: 0x%08x (%u)\n", \
sc->sc_dev.dv_xname, #_e, nu->_e, nu->_e); \
} while (0)
_NXBUSER(nu_image.nim_bootld_ver);
_NXBUSER(nu_image.nim_bootld_size);
_NXBUSER(nu_image.nim_image_ver);
_NXBUSER(nu_image.nim_image_size);
_NXBUSER(nu_primary);
_NXBUSER(nu_secondary);
_NXBUSER(nu_subsys_id);
DPRINTF(NXDBG_FLASH, "%s: nu_serial_num: %s\n",
sc->sc_dev.dv_xname, nu->nu_serial_num);
_NXBUSER(nu_bios_ver);
#undef _NXBUSER
#endif
free(nu, M_TEMP);
/*
* Initialize and bootstrap the device
*/
nxb_write(sc, NXSW_CMD_PRODUCER_OFF, 0);
nxb_write(sc, NXSW_CMD_CONSUMER_OFF, 0);
nxb_write(sc, NXSW_CMD_ADDR_LO, 0);
/*
* bootstrap the firmware, the status will be polled in the
* mountroot hook.
*/
nxb_write(sc, NXROMUSB_GLB_PEGTUNE, NXROMUSB_GLB_PEGTUNE_DONE);
sc->sc_state = NX_S_BOOTING;
return (0);
}
int
nxb_booted(struct nxb_softc *sc)
{
if (nxb_wait(sc, NXSW_CMDPEG_STATE,
NXSW_CMDPEG_INIT_DONE, NXSW_CMDPEG_STATE_M, 1, 2000000) != 0) {
printf("%s: bootstrap failed, code 0x%x\n",
sc->sc_dev.dv_xname, nxb_read(sc, NXSW_CMDPEG_STATE));
return (-1);
}
return (0);
}
void
nxb_mountroot(void *arg)
{
struct nxb_softc *sc = (struct nxb_softc *)arg;
assert(sc->sc_state == NX_S_BOOTING);
/*
* Poll the status of the running firmware.
*/
if (nxb_booted(sc) != 0)
return;
/*
* Get and validate the loaded firmware version
*/
sc->sc_fwmajor = nxb_read(sc, NXSW_FW_VERSION_MAJOR);
sc->sc_fwminor = nxb_read(sc, NXSW_FW_VERSION_MINOR);
sc->sc_fwbuild = nxb_read(sc, NXSW_FW_VERSION_BUILD);
printf("%s: firmware %u.%u.%u", sc->sc_dev.dv_xname,
sc->sc_fwmajor, sc->sc_fwminor, sc->sc_fwbuild);
if (sc->sc_fwmajor != NX_FIRMWARE_MAJOR ||
sc->sc_fwminor != NX_FIRMWARE_MINOR) {
printf(", requires %u.%u.xx (%u.%u.%u)\n",
NX_FIRMWARE_MAJOR, NX_FIRMWARE_MINOR,
NX_FIRMWARE_MAJOR, NX_FIRMWARE_MINOR,
NX_FIRMWARE_BUILD);
sc->sc_flags |= NXFLAG_FWINVALID;
nxb_reset(sc);
return;
}
printf("\n");
/* Firmware is ready for operation, allow interrupts etc. */
sc->sc_state = NX_S_READY;
}
int
nxb_loadfirmware(struct nxb_softc *sc, struct nxb_firmware_header *fh,
u_int8_t **fw, size_t *fwlen)
{
#ifdef NXB_LOADFIRMWARE
u_int8_t *mem;
size_t memlen;
/*
* Load a supported bootloader and firmware image from disk
*/
if (loadfirmware("nxb", &mem, &memlen) != 0)
return (-1);
if ((memlen) < sizeof(*fh))
goto fail;
bcopy(mem, fh, sizeof(*fh));
if (ntohl(fh->fw_hdrver) != NX_FIRMWARE_HDRVER)
goto fail;
*fw = mem;
*fwlen = memlen;
return (0);
fail:
free(mem, M_DEVBUF);
#endif
return (-1);
}
int
nxb_reloadfirmware(struct nxb_softc *sc, struct nxb_firmware_header *fh,
u_int8_t **fw, size_t *fwlen)
{
u_int8_t *mem;
size_t memlen;
u_int32_t addr, *data;
u_int i;
/*
* Load the images from flash, setup a fake firmware header
*/
memlen = sc->sc_nxbimage.nim_bootld_size + sizeof(*fh);
mem = (u_int8_t *)malloc(memlen, M_DEVBUF, M_NOWAIT);
if (mem == NULL)
return (-1);
fh->fw_hdrver = htonl(NX_FIRMWARE_HDRVER);
fh->fw_image_ver = htonl(sc->sc_nxbimage.nim_image_ver);
fh->fw_image_size = 0; /* Reload firmware image from flash */
fh->fw_bootld_ver = htonl(sc->sc_nxbimage.nim_bootld_ver);
fh->fw_bootld_size = htonl(sc->sc_nxbimage.nim_bootld_size);
bcopy(fh, mem, sizeof(*fh));
addr = NXFLASHMAP_BOOTLOADER;
data = (u_int32_t *)(mem + sizeof(*fh));
for (i = 0; i < (sc->sc_nxbimage.nim_bootld_size / 4); i++) {
if (nxb_read_rom(sc, addr, data) != 0)
goto fail;
addr += sizeof(u_int32_t);
data++;
}
*fw = mem;
*fwlen = memlen;
return (0);
fail:
free(mem, M_DEVBUF);
return (-1);
}
void
nxb_reset(struct nxb_softc *sc)
{
struct nxb_firmware_header fh;
u_int8_t *fw = NULL;
size_t fwlen = 0;
int bootsz, imagesz;
u_int i;
u_int32_t *data, addr, val;
bus_size_t reg;
bzero(&fh, sizeof(fh));
if (sc->sc_flags & NXFLAG_FWINVALID) {
if (nxb_loadfirmware(sc, &fh, &fw, &fwlen) != 0) {
printf("%s: failed to load firmware from disk\n",
sc->sc_dev.dv_xname);
goto fail;
}
} else {
if (nxb_reloadfirmware(sc, &fh, &fw, &fwlen) != 0) {
printf("%s: failed to reload firmware from flash\n",
sc->sc_dev.dv_xname);
goto fail;
}
}
/*
* Validate the information found in the extra header
*/
val = ntohl(fh.fw_image_ver);
sc->sc_fwmajor = (val & NXB_IMAGE_MAJOR_M) >> NXB_IMAGE_MAJOR_S;
sc->sc_fwminor = (val & NXB_IMAGE_MINOR_M) >> NXB_IMAGE_MINOR_S;
sc->sc_fwbuild = (val & NXB_IMAGE_BUILD_M) >> NXB_IMAGE_BUILD_S;
if (sc->sc_flags & NXFLAG_FWINVALID)
printf("%s: using firmware %u.%u.%u\n", sc->sc_dev.dv_xname,
sc->sc_fwmajor, sc->sc_fwminor, sc->sc_fwbuild);
if (sc->sc_fwmajor != NX_FIRMWARE_MAJOR ||
sc->sc_fwminor != NX_FIRMWARE_MINOR) {
printf("%s: unsupported firmware version\n",
sc->sc_dev.dv_xname);
goto fail;
}
bootsz = ntohl(fh.fw_bootld_size);
imagesz = ntohl(fh.fw_image_size);
if ((imagesz + bootsz) != (fwlen - sizeof(fh)) ||
(imagesz % 4) || (bootsz % 4)) {
printf("%s: invalid firmware image\n", sc->sc_dev.dv_xname);
goto fail;
}
/*
* Initialize the SW registers
*/
addr = NXFLASHMAP_CRBINIT_0;
if (nxb_read_rom(sc, addr, &val) != 0)
goto fail1;
/* XXX */
DPRINTF(NXDBG_FLASH, "%s(%s): ncrb 0x%08x\n",
sc->sc_dev.dv_xname, __func__, val);
/*
* Load the images into RAM
*/
/* Reset casper boot chip */
nxb_write(sc, NXROMUSB_GLB_CAS_RESET, NXROMUSB_GLB_CAS_RESET_ENABLE);
reg = NXFLASHMAP_BOOTLOADER;
data = (u_int32_t *)(fw + sizeof(fh));
for (i = 0; i < (bootsz / 4); i++) {
nxb_write(sc, reg, *data);
addr += sizeof(u_int32_t);
data++;
}
if (imagesz) {
reg = NXFLASHMAP_FIRMWARE_0;
for (i = 0; i < (imagesz / 4); i++) {
nxb_write(sc, reg, *data);
addr += sizeof(u_int32_t);
data++;
}
/* tell the bootloader to load the firmware image from RAM */
nxb_write(sc, NXSW_BOOTLD_CONFIG, NXSW_BOOTLD_CONFIG_RAM);
}
/* Power on the clocks and unreset the casper boot chip */
nxb_write(sc, NXROMUSB_GLB_CHIPCLKCONTROL,
NXROMUSB_GLB_CHIPCLKCONTROL_ON);
nxb_write(sc, NXROMUSB_GLB_CAS_RESET, NXROMUSB_GLB_CAS_RESET_DISABLE);
/*
* bootstrap the newly loaded firmware and wait for completion
*/
nxb_write(sc, NXROMUSB_GLB_PEGTUNE, NXROMUSB_GLB_PEGTUNE_DONE);
if (nxb_booted(sc) != 0)
goto fail;
/* Firmware is ready for operation, allow interrupts etc. */
sc->sc_state = NX_S_READY;
goto done;
fail1:
printf("%s: failed to reset firmware\n", sc->sc_dev.dv_xname);
fail:
sc->sc_state = NX_S_FAIL;
done:
if (fw != NULL)
free(fw, M_DEVBUF);
}
u_int32_t
nxb_read(struct nxb_softc *sc, bus_size_t reg)
{
bus_space_barrier(sc->sc_memt, sc->sc_memh, reg, 4,
BUS_SPACE_BARRIER_READ);
return (bus_space_read_4(sc->sc_memt, sc->sc_memh, reg));
}
void
nxb_write(struct nxb_softc *sc, bus_size_t reg, u_int32_t val)
{
bus_space_write_4(sc->sc_memt, sc->sc_memh, reg, val);
bus_space_barrier(sc->sc_memt, sc->sc_memh, reg, 4,
BUS_SPACE_BARRIER_WRITE);
}
void
nxb_set_window(struct nxb_softc *sc, int window)
{
u_int32_t val;
if (sc->sc_window == window)
return;
assert(window == 0 || window == 1);
val = nxb_read(sc, NXCRB_WINDOW(sc->sc_function));
if (window)
val |= NXCRB_WINDOW_1;
else
val &= ~NXCRB_WINDOW_1;
nxb_write(sc, NXCRB_WINDOW(sc->sc_function), val);
sc->sc_window = window;
}
int
nxb_wait(struct nxb_softc *sc, bus_size_t reg, u_int32_t val,
u_int32_t mask, int is_set, u_int timeout)
{
u_int i;
u_int32_t data;
for (i = timeout; i > 0; i--) {
data = nxb_read(sc, reg) & mask;
if (is_set) {
if (data == val)
goto done;
} else {
if (data != val)
goto done;
}
delay(1);
}
return (-1);
done:
DPRINTF(NXDBG_WAIT, "%s(%s) "
"reg 0x%08x completed after %d/%d iterations\n",
sc->sc_dev.dv_xname, __func__, reg, i, timeout);
return (0);
}
int
nxb_read_rom(struct nxb_softc *sc, u_int32_t addr, u_int32_t *val)
{
u_int32_t data;
int ret = 0;
/* Must be called from window 1 */
assert(sc->sc_window == 1);
/*
* Need to set a lock and the lock ID to access the flash
*/
ret = nxb_wait(sc, NXSEM_FLASH_LOCK,
NXSEM_FLASH_LOCKED, NXSEM_FLASH_LOCK_M, 1, 10000);
if (ret != 0) {
DPRINTF(NXDBG_FLASH, "%s(%s): ROM lock timeout\n",
sc->sc_dev.dv_xname, __func__);
return (-1);
}
nxb_write(sc, NXSW_ROM_LOCK_ID, NXSW_ROM_LOCK_DRV);
/*
* Setup ROM data transfer
*/
/* Set the ROM address */
nxb_write(sc, NXROMUSB_ROM_ADDR, addr);
/* The delay is needed to prevent bursting on the chipset */
nxb_write(sc, NXROMUSB_ROM_ABYTE_CNT, 3);
delay(100);
nxb_write(sc, NXROMUSB_ROM_DUMMY_BYTE_CNT, 0);
/* Set opcode and wait for completion */
nxb_write(sc, NXROMUSB_ROM_OPCODE, NXROMUSB_ROM_OPCODE_READ);
ret = nxb_wait(sc, NXROMUSB_GLB_STATUS,
NXROMUSB_GLB_STATUS_DONE, NXROMUSB_GLB_STATUS_DONE, 1, 100);
if (ret != 0) {
DPRINTF(NXDBG_FLASH, "%s(%s): ROM operation timeout\n",
sc->sc_dev.dv_xname, __func__);
goto unlock;
}
/* Reset counters */
nxb_write(sc, NXROMUSB_ROM_ABYTE_CNT, 0);
delay(100);
nxb_write(sc, NXROMUSB_ROM_DUMMY_BYTE_CNT, 0);
/* Finally get the value */
data = nxb_read(sc, NXROMUSB_ROM_RDATA);
/* Flash data is stored in little endian */
*val = letoh32(data);
unlock:
/*
* Release the lock
*/
(void)nxb_read(sc, NXSEM_FLASH_UNLOCK);
return (ret);
}
/*
* Routines handling the virtual ''nx'' ports
*/
int
nx_match(struct device *parent, void *match, void *aux)
{
struct nxb_port *nxp = (struct nxb_port *)aux;
if (nxp->nxp_id >= NX_MAX_PORTS)
return (0);
switch (nxp->nxp_mode) {
case NXNIU_MODE_XGE:
case NXNIU_MODE_GBE:
return (1);
case NXNIU_MODE_FC:
/* FibreChannel mode is not documented and not supported */
return (0);
}
return (0);
}
void
nx_attach(struct device *parent, struct device *self, void *aux)
{
struct nxb_softc *sc = (struct nxb_softc *)parent;
struct nx_softc *nx = (struct nx_softc *)self;
struct nxb_port *nxp = (struct nxb_port *)aux;
struct ifnet *ifp;
nx->nx_sc = sc;
nx->nx_port = nxp;
nxp->nxp_nx = nx;
nx->nx_ih = pci_intr_establish(sc->sc_pc, sc->sc_ih, IPL_NET,
nx_intr, nx, nx->nx_dev.dv_xname);
if (nx->nx_ih == NULL) {
printf(": unable to establish interrupt\n");
return;
}
bcopy(nxp->nxp_lladdr, nx->nx_ac.ac_enaddr, ETHER_ADDR_LEN);
printf(", address %s\n", ether_sprintf(nx->nx_ac.ac_enaddr));
ifp = &nx->nx_ac.ac_if;
ifp->if_softc = nx;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_ioctl = nx_ioctl;
ifp->if_start = nx_start;
ifp->if_watchdog = nx_watchdog;
ifp->if_hardmtu = NX_JUMBO_MTU;
strlcpy(ifp->if_xname, nx->nx_dev.dv_xname, IFNAMSIZ);
IFQ_SET_MAXLEN(&ifp->if_snd, sc->sc_ntxbuf - 1);
IFQ_SET_READY(&ifp->if_snd);
ifp->if_capabilities = IFCAP_VLAN_MTU;
#if 0
ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
ifp->if_capabilities |= IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 |
IFCAP_CSUM_UDPv4;
#endif
ifmedia_init(&nx->nx_mii.mii_media, 0,
nx_media_change, nx_media_status);
ifmedia_add(&nx->nx_mii.mii_media, IFM_ETHER | IFM_AUTO, 0, NULL);
ifmedia_set(&nx->nx_mii.mii_media, IFM_ETHER | IFM_AUTO);
if_attach(ifp);
ether_ifattach(ifp);
timeout_set(&nx->nx_tick, nx_tick, sc);
return;
}
int
nx_print(void *aux, const char *parentname)
{
struct nxb_port *nxp = (struct nxb_port *)aux;
if (parentname)
printf("nx port %u at %s",
nxp->nxp_id, parentname);
else
printf(" port %u", nxp->nxp_id);
return (UNCONF);
}
int
nx_media_change(struct ifnet *ifp)
{
struct nx_softc *nx = (struct nx_softc *)ifp->if_softc;
struct nxb_port *nxp = nx->nx_port;
switch (nxp->nxp_mode) {
case NXNIU_MODE_XGE:
/* XXX */
break;
case NXNIU_MODE_GBE:
mii_mediachg(&nx->nx_mii);
break;
}
return (0);
}
void
nx_media_status(struct ifnet *ifp, struct ifmediareq *imr)
{
struct nx_softc *nx = (struct nx_softc *)ifp->if_softc;
struct nxb_port *nxp = nx->nx_port;
switch (nxp->nxp_mode) {
case NXNIU_MODE_XGE:
imr->ifm_active = IFM_ETHER | IFM_AUTO;
imr->ifm_status = IFM_AVALID;
nx_link_state(nx);
if (LINK_STATE_IS_UP(ifp->if_link_state) &&
ifp->if_flags & IFF_UP)
imr->ifm_status |= IFM_ACTIVE;
break;
case NXNIU_MODE_GBE:
mii_pollstat(&nx->nx_mii);
imr->ifm_active = nx->nx_mii.mii_media_active;
imr->ifm_status = nx->nx_mii.mii_media_status;
mii_mediachg(&nx->nx_mii);
break;
}
}
void
nx_link_state(struct nx_softc *nx)
{
struct nxb_port *nxp = nx->nx_port;
struct ifnet *ifp = &nx->nx_ac.ac_if;
u_int32_t status = 0;
int link_state = LINK_STATE_DOWN;
switch (nxp->nxp_mode) {
case NXNIU_MODE_XGE:
/* XXX */
// status = nx_read(sc, NX_XG_STATE);
if (status & NXSW_XG_LINK_UP)
link_state = LINK_STATE_FULL_DUPLEX;
if (ifp->if_link_state != link_state) {
ifp->if_link_state = link_state;
if_link_state_change(ifp);
}
break;
case NXNIU_MODE_GBE:
mii_tick(&nx->nx_mii);
break;
}
}
void
nx_watchdog(struct ifnet *ifp)
{
return;
}
int
nx_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
{
struct nx_softc *nx = (struct nx_softc *)ifp->if_softc;
struct ifaddr *ifa;
struct ifreq *ifr;
int s, error = 0;
s = splnet();
if ((error = ether_ioctl(ifp, &nx->nx_ac, cmd, data)) > 0) {
splx(s);
return (error);
}
switch (cmd) {
case SIOCSIFADDR:
ifa = (struct ifaddr *)data;
ifp->if_flags |= IFF_UP;
#ifdef INET
if (ifa->ifa_addr->sa_family == AF_INET)
arp_ifinit(&nx->nx_ac, ifa);
#endif
/* FALLTHROUGH */
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_UP) {
if (ifp->if_flags & IFF_RUNNING)
nx_iff(nx);
else
nx_init(ifp);
} else {
if (ifp->if_flags & IFF_RUNNING)
nx_stop(ifp);
}
break;
case SIOCSIFMTU:
if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ifp->if_hardmtu)
error = EINVAL;
else if (ifp->if_mtu != ifr->ifr_mtu)
ifp->if_mtu = ifr->ifr_mtu;
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
ifr = (struct ifreq *)data;
error = (cmd == SIOCADDMULTI) ?
ether_addmulti(ifr, &nx->nx_ac) :
ether_delmulti(ifr, &nx->nx_ac);
if (error == ENETRESET) {
if (ifp->if_flags & IFF_RUNNING)
nx_iff(nx);
error = 0;
}
break;
case SIOCGIFMEDIA:
case SIOCSIFMEDIA:
error = ifmedia_ioctl(ifp, ifr, &nx->nx_mii.mii_media, cmd);
break;
default:
error = ENOTTY;
}
if (error == ENETRESET) {
if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
(IFF_UP | IFF_RUNNING))
nx_init(ifp);
error = 0;
}
splx(s);
return (error);
}
void
nx_init(struct ifnet *ifp)
{
return;
}
void
nx_start(struct ifnet *ifp)
{
return;
}
void
nx_stop(struct ifnet *ifp)
{
return;
}
void
nx_iff(struct nx_softc *nx)
{
return;
}
void
nx_tick(void *arg)
{
struct nx_softc *nx = (struct nx_softc *)arg;
nx_link_state(nx);
timeout_add(&nx->nx_tick, hz);
}
int
nx_intr(void *arg)
{
struct nx_softc *nx = (struct nx_softc *)arg;
struct nxb_softc *sc = nx->nx_sc;
if (sc->sc_state != NX_S_READY)
return (0);
return (0);
}
|