-
Notifications
You must be signed in to change notification settings - Fork 5
/
if_alx.c
1636 lines (1351 loc) · 37.9 KB
/
if_alx.c
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
/*-
* Copyright (c) 2012 Qualcomm Atheros, Inc.
* Copyright (c) 2013, Mark Johnston <[email protected]>
*
* Permission to use, copy, modify, and/or 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.
*/
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bitstring.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/queue.h>
#include <sys/rman.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/taskqueue.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <net/if_var.h>
#include <net/if_vlan_var.h>
#include <netinet/in.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <machine/bus.h>
#include "compat.h"
#include "alx_hw.h"
#include "if_alxreg.h"
#include "if_alxvar.h"
MODULE_DEPEND(alx, pci, 1, 1, 1);
MODULE_DEPEND(alx, ether, 1, 1, 1);
static struct alx_dev {
uint16_t alx_vendorid;
uint16_t alx_deviceid;
const char *alx_name;
} alx_devs[] = {
{ ALX_VENDOR_ID, ALX_DEV_ID_AR8161,
"Qualcomm Atheros AR8161 Gigabit Ethernet" },
{ ALX_VENDOR_ID, ALX_DEV_ID_AR8162,
"Qualcomm Atheros AR8162 Fast Ethernet" },
{ ALX_VENDOR_ID, ALX_DEV_ID_AR8171,
"Qualcomm Atheros AR8171 Gigabit Ethernet" },
{ ALX_VENDOR_ID, ALX_DEV_ID_AR8172,
"Qualcomm Atheros AR8172 Fast Ethernet" },
{ ALX_VENDOR_ID, ALX_DEV_ID_KI2201,
"Qualcomm Atheros Killer E2201 Gigabit Ethernet"
},
};
static int alx_attach(device_t);
static int alx_detach(device_t);
static int alx_probe(device_t);
static int alx_resume(device_t);
static int alx_shutdown(device_t);
static void alx_stop(struct alx_softc *);
static int alx_suspend(device_t);
static int alx_ioctl(struct ifnet *, u_long, caddr_t);
static void alx_init(void *);
static void alx_init_locked(struct alx_softc *);
static int alx_media_change(struct ifnet *);
static void alx_media_status(struct ifnet *, struct ifmediareq *);
static void alx_start(struct ifnet *);
static void alx_start_locked(struct ifnet *);
static int alx_alloc_intr(struct alx_softc *);
static void alx_free_intr(struct alx_softc *);
static void alx_link_task(void *, int);
static void alx_int_task(void *, int);
static void alx_intr_disable(struct alx_softc *);
static void alx_intr_enable(struct alx_softc *);
static int alx_intr_legacy(void *);
static int alx_intr_msi(void *);
static void alx_reset(struct alx_softc *sc);
static void alx_update_link(struct alx_softc *);
static int alx_dma_alloc(struct alx_softc *);
#if 0
static void alx_dma_free(struct alx_softc *);
#endif
static void alx_dmamap_cb(void *, bus_dma_segment_t *, int, int);
static void alx_init_rx_ring(struct alx_softc *);
static void alx_init_tx_ring(struct alx_softc *);
static int alx_newbuf(struct alx_softc *, int);
static void alx_rxintr(struct alx_softc *);
static void alx_txintr(struct alx_softc *);
static int alx_xmit(struct alx_softc *, struct mbuf **);
static device_method_t alx_methods[] = {
DEVMETHOD(device_probe, alx_probe),
DEVMETHOD(device_attach, alx_attach),
DEVMETHOD(device_detach, alx_detach),
DEVMETHOD(device_shutdown, alx_shutdown),
DEVMETHOD(device_suspend, alx_suspend),
DEVMETHOD(device_resume, alx_resume),
DEVMETHOD_END
};
static driver_t alx_driver = {
"alx",
alx_methods,
sizeof(struct alx_softc)
};
static devclass_t alx_devclass;
DRIVER_MODULE(alx, pci, alx_driver, alx_devclass, 0, 0);
static SYSCTL_NODE(_hw, OID_AUTO, alx, CTLFLAG_RD, 0, "alx driver settings");
static int alx_enable_msix = 0;
TUNABLE_INT("hw.alx.enable_msix", &alx_enable_msix);
SYSCTL_INT(_hw_alx, OID_AUTO, enable_msix, CTLFLAG_RDTUN, &alx_enable_msix,
0, "Enable MSI-X interrupts");
static int alx_enable_msi = 0;
TUNABLE_INT("hw.alx.enable_msi", &alx_enable_msi);
SYSCTL_INT(_hw_alx, OID_AUTO, enable_msi, CTLFLAG_RDTUN, &alx_enable_msi,
0, "Enable MSI interrupts");
static void
alx_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
{
if (error != 0)
return;
*(bus_addr_t *)arg = segs[0].ds_addr;
}
/*
* XXX:
* - multiple queues
* - does the chipset's DMA engine support more than one segment?
*/
static int
alx_dma_alloc(struct alx_softc *sc)
{
device_t dev;
struct alx_hw *hw;
struct alx_buffer *buf;
int error, i;
dev = sc->alx_dev;
hw = &sc->hw;
/* Create parent tag. */
error = bus_dma_tag_create(
bus_get_dma_tag(sc->alx_dev), /* parent */
1, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1, /* nsegments */
BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
0, /* flags */
NULL, NULL, /* lockfunc, lockfuncarg */
&sc->alx_parent_tag);
if (error != 0) {
device_printf(dev, "could not create parent DMA tag\n");
return (error);
}
/* Create the DMA tag for the transmit packet descriptor ring. */
/* XXX assuming 1 queue at the moment. */
error = bus_dma_tag_create(
sc->alx_parent_tag, /* parent */
8, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
sc->tx_ringsz * sizeof(struct tpd_desc), /* maxsize */
1, /* nsegments */
sc->tx_ringsz * sizeof(struct tpd_desc), /* maxsegsize */
0, /* flags */
NULL, NULL, /* lockfunc, lockfuncarg */
&sc->alx_tx_tag);
if (error != 0) {
device_printf(dev, "could not create TX descriptor ring tag\n");
return (error);
}
/* Allocate DMA memory for the transmit packet descriptor ring. */
error = bus_dmamem_alloc(sc->alx_tx_tag,
(void **)&sc->alx_tx_queue.tpd_hdr,
BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
&sc->alx_tx_dmamap);
if (error != 0) {
device_printf(dev,
"could not allocate DMA'able memory for TX ring\n");
/* XXX cleanup */
return (error);
}
/* Do the actual DMA mapping of the transmit packet descriptor ring. */
error = bus_dmamap_load(sc->alx_tx_tag, sc->alx_tx_dmamap,
sc->alx_tx_queue.tpd_hdr, sc->tx_ringsz * sizeof(struct tpd_desc),
alx_dmamap_cb, &sc->alx_tx_queue.tpd_dma, 0);
if (error != 0) {
device_printf(dev, "could not load DMA map for TX ring\n");
/* XXX cleanup */
return (error);
}
/* Create the DMA tag for the receive ready descriptor ring. */
/* XXX assuming 1 queue at the moment. */
error = bus_dma_tag_create(
sc->alx_parent_tag, /* parent */
8, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
sc->rx_ringsz * sizeof(struct rrd_desc), /* maxsize */
1, /* nsegments */
sc->rx_ringsz * sizeof(struct rrd_desc), /* maxsegsize */
0, /* flags */
NULL, NULL, /* lockfunc, lockfuncarg */
&sc->alx_rr_tag);
if (error != 0) {
device_printf(dev, "could not create RX descriptor ring tag\n");
return (error);
}
error = bus_dmamem_alloc(sc->alx_rr_tag,
(void **)&sc->alx_rx_queue.rrd_hdr,
BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
&sc->alx_rr_dmamap);
if (error != 0) {
device_printf(dev,
"could not allocate DMA'able memory for RX ring\n");
/* XXX cleanup */
return (error);
}
error = bus_dmamap_load(sc->alx_rr_tag, sc->alx_rr_dmamap,
sc->alx_rx_queue.rrd_hdr, sc->rx_ringsz * sizeof(struct rrd_desc),
alx_dmamap_cb, &sc->alx_rx_queue.rrd_dma, 0);
if (error != 0) {
device_printf(dev,
"could not load DMA map for RX ready ring\n");
/* XXX cleanup */
return (error);
}
/* Create the DMA tag for the receive free descriptor ring. */
/* XXX assuming 1 queue at the moment. */
error = bus_dma_tag_create(
sc->alx_parent_tag, /* parent */
8, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
sc->rx_ringsz * sizeof(struct rfd_desc), /* maxsize */
1, /* nsegments */
sc->rx_ringsz * sizeof(struct rfd_desc), /* maxsegsize */
0, /* flags */
NULL, NULL, /* lockfunc, lockfuncarg */
&sc->alx_rx_tag);
if (error != 0) {
device_printf(dev,
"could not create RX descriptor ring DMA tag\n");
/* XXX cleanup */
return (error);
}
error = bus_dmamem_alloc(sc->alx_rx_tag,
(void **)&sc->alx_rx_queue.rfd_hdr,
BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
&sc->alx_rx_dmamap);
if (error != 0) {
device_printf(dev,
"could not allocate DMA'able memory for RX ring\n");
/* XXX cleanup */
return (error);
}
error = bus_dmamap_load(sc->alx_rx_tag, sc->alx_rx_dmamap,
sc->alx_rx_queue.rfd_hdr, sc->rx_ringsz * sizeof(struct rfd_desc),
alx_dmamap_cb, &sc->alx_rx_queue.rfd_dma, 0);
if (error != 0) {
device_printf(dev, "could not load DMA map for RX free ring\n");
/* XXX cleanup */
return (error);
}
/* Create the DMA tag for the transmit buffers. */
/* XXX where do maxsize, nsegments, maxsegsize come from? */
error = bus_dma_tag_create(
sc->alx_parent_tag, /* parent */
8, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
PAGE_SIZE, /* maxsize XXX */
32, /* nsegments XXX */
PAGE_SIZE, /* maxsegsize */
0, /* flags */
NULL, NULL, /* lockfunc, lockarg */
&sc->alx_tx_buf_tag);
if (error != 0) {
device_printf(dev, "could not create TX buffer DMA tag\n");
/* XXX cleanup */
return (error);
}
/* Allocate space for the TX buffer ring. */
sc->alx_tx_queue.bf_info = malloc(
sc->tx_ringsz * sizeof(struct alx_buffer), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (sc->alx_tx_queue.bf_info == NULL) {
device_printf(dev,
"could not allocate memory for TX buffer ring\n");
/* XXX cleanup */
return (error);
}
/* Create DMA maps for the TX buffers. */
buf = sc->alx_tx_queue.bf_info;
for (i = 0; i < sc->tx_ringsz; i++, buf++) {
error = bus_dmamap_create(sc->alx_tx_buf_tag, 0, &buf->dmamap);
if (error != 0) {
device_printf(dev, "could not create TX DMA map\n");
/* XXX cleanup */
return (error);
}
}
/* Create the DMA tag for the receive buffers. */
error = bus_dma_tag_create(
sc->alx_parent_tag, /* parent */
8, 0, /* alignment, boundary */
BUS_SPACE_MAXADDR, /* lowaddr */
BUS_SPACE_MAXADDR, /* highaddr */
NULL, NULL, /* filter, filterarg */
MCLBYTES, /* maxsize */
1, /* nsegments */
MCLBYTES, /* maxsegsize */
0, /* flags */
NULL, NULL, /* lockfunc, lockarg */
&sc->alx_rx_buf_tag);
if (error != 0) {
device_printf(dev, "could not create RX buffer DMA tag\n");
/* XXX cleanup */
return (error);
}
/* Allocate space for the RX buffer ring. */
sc->alx_rx_queue.bf_info = malloc(
sc->rx_ringsz * sizeof(struct alx_buffer), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (sc->alx_rx_queue.bf_info == NULL) {
device_printf(dev,
"could not allocate memory for RX buffer ring\n");
/* XXX cleanup */
return (error);
}
/* Create DMA maps for the RX buffers. */
buf = sc->alx_rx_queue.bf_info;
for (i = 0; i < sc->rx_ringsz; i++, buf++) {
error = bus_dmamap_create(sc->alx_rx_buf_tag, 0, &buf->dmamap);
if (error != 0) {
device_printf(dev, "could not create RX DMA map\n");
/* XXX cleanup */
return (error);
}
}
return (error);
}
#if 0
static void __unused
alx_dma_free(struct alx_softc *sc)
{
}
#endif
static void
alx_intr_enable(struct alx_softc *sc)
{
struct alx_hw *hw;
hw = &sc->hw;
/* level-1 interrupt switch */
ALX_MEM_W32(hw, ALX_ISR, 0);
ALX_MEM_W32(hw, ALX_IMR, hw->imask);
ALX_MEM_FLUSH(hw);
#ifdef __notyet
int i;
/* enable all individual MSIX IRQs */
if (ALX_FLAG(sc, USING_MSIX))
for (i = 0; i < adpt->nr_vec; i++)
alx_mask_msix(hw, i, false);
#endif
}
static void
alx_intr_disable(struct alx_softc *sc)
{
struct alx_hw *hw = &sc->hw;
ALX_MEM_W32(hw, ALX_ISR, ALX_ISR_DIS);
ALX_MEM_W32(hw, ALX_IMR, 0);
ALX_MEM_FLUSH(hw);
#ifdef __notyet
int i;
if (ALX_FLAG(sc, USING_MSIX))
for (i = 0; i < sc->nr_vec; i++)
alx_mask_msix(hw, i, true);
#endif
}
static int
alx_identify_hw(struct alx_softc *sc)
{
device_t dev = sc->alx_dev;
struct alx_hw *hw = &sc->hw;
int rev;
hw->device_id = pci_get_device(dev);
hw->subdev_id = pci_get_subdevice(dev);
hw->subven_id = pci_get_subvendor(dev);
hw->revision = pci_get_revid(dev);
rev = ALX_REVID(hw);
switch (ALX_DID(hw)) {
case ALX_DEV_ID_AR8161:
case ALX_DEV_ID_AR8162:
case ALX_DEV_ID_AR8171:
case ALX_DEV_ID_AR8172:
case ALX_DEV_ID_KI2201:
if (rev > ALX_REV_C0)
break;
ALX_CAP_SET(hw, L0S);
ALX_CAP_SET(hw, L1);
ALX_CAP_SET(hw, MTQ);
ALX_CAP_SET(hw, RSS);
ALX_CAP_SET(hw, MSIX);
ALX_CAP_SET(hw, SWOI);
hw->max_dma_chnl = rev >= ALX_REV_B0 ? 4 : 2;
if (rev < ALX_REV_C0) {
hw->ptrn_ofs = 0x600;
hw->max_ptrns = 8;
} else {
hw->ptrn_ofs = 0x14000;
hw->max_ptrns = 16;
}
break;
default:
return (EINVAL);
}
if (ALX_DID(hw) & 1)
ALX_CAP_SET(hw, GIGA);
return (0);
}
static const u8 def_rss_key[40] = {
0xE2, 0x91, 0xD7, 0x3D, 0x18, 0x05, 0xEC, 0x6C,
0x2A, 0x94, 0xB3, 0x0D, 0xA5, 0x4F, 0x2B, 0xEC,
0xEA, 0x49, 0xAF, 0x7C, 0xE2, 0x14, 0xAD, 0x3D,
0xB8, 0x55, 0xAA, 0xBE, 0x6A, 0x3E, 0x67, 0xEA,
0x14, 0x36, 0x4D, 0x17, 0x3B, 0xED, 0x20, 0x0D,
};
/* alx_init_adapter -
* initialize general software structure (struct alx_adapter).
* fields are inited based on PCI device information.
*/
static int
alx_init_sw(struct alx_softc *sc)
{
device_t dev = sc->alx_dev;
struct alx_hw *hw = &sc->hw;
int i, err;
err = alx_identify_hw(sc);
if (err) {
device_printf(dev, "unrecognized chip, aborting\n");
return (err);
}
/* assign patch flag for specific platforms */
alx_patch_assign(hw);
memcpy(hw->rss_key, def_rss_key, sizeof(def_rss_key));
hw->rss_idt_size = 128;
hw->rss_hash_type = ALX_RSS_HASH_TYPE_ALL;
hw->smb_timer = 400;
sc->tx_ringsz = 256;
sc->rx_ringsz = 512;
hw->sleep_ctrl = ALX_SLEEP_WOL_MAGIC | ALX_SLEEP_WOL_PHY;
hw->imt = 200;
hw->imask = ALX_ISR_MISC;
hw->dma_chnl = hw->max_dma_chnl;
hw->ith_tpd = sc->tx_ringsz / 3;
hw->link_up = false;
hw->link_duplex = 0;
hw->link_speed = 0;
hw->adv_cfg = ADVERTISED_Autoneg | ADVERTISED_10baseT_Half |
ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Full |
ADVERTISED_100baseT_Half | ADVERTISED_1000baseT_Full;
hw->flowctrl = ALX_FC_ANEG | ALX_FC_RX | ALX_FC_TX;
hw->wrr_ctrl = ALX_WRR_PRI_RESTRICT_NONE;
for (i = 0; i < ARRAY_SIZE(hw->wrr); i++)
hw->wrr[i] = 4;
hw->rx_ctrl = ALX_MAC_CTRL_WOLSPED_SWEN | ALX_MAC_CTRL_MHASH_ALG_HI5B |
ALX_MAC_CTRL_BRD_EN | ALX_MAC_CTRL_PCRCE | ALX_MAC_CTRL_CRCE |
ALX_MAC_CTRL_RXFC_EN | ALX_MAC_CTRL_TXFC_EN |
FIELDX(ALX_MAC_CTRL_PRMBLEN, 7);
hw->is_fpga = false;
sc->irq_sem = 0;
return err;
}
static void
alx_init_rx_ring(struct alx_softc *sc)
{
struct alx_hw *hw;
int i, error;
ALX_LOCK_ASSERT(sc);
hw = &sc->hw;
sc->alx_rx_queue.pidx = 0;
sc->alx_rx_queue.p_reg = ALX_RFD_PIDX;
sc->alx_rx_queue.cidx = 0;
sc->alx_rx_queue.rrd_cidx = 0;
sc->alx_rx_queue.c_reg = ALX_RFD_CIDX;
sc->alx_rx_queue.qidx = 0;
sc->alx_rx_queue.count = sc->rx_ringsz;
hw->imask |= ALX_ISR_RX_Q0;
/* XXX the rings are all supposed to come from the same 4GB block. */
ALX_MEM_W32(hw, ALX_RX_BASE_ADDR_HI, sc->alx_rx_queue.rfd_dma >> 32);
ALX_MEM_W32(hw, ALX_RRD_ADDR_LO, sc->alx_rx_queue.rrd_dma);
ALX_MEM_W32(hw, ALX_RFD_ADDR_LO, sc->alx_rx_queue.rfd_dma);
ALX_MEM_W32(hw, ALX_RRD_RING_SZ, sc->rx_ringsz);
ALX_MEM_W32(hw, ALX_RFD_RING_SZ, sc->rx_ringsz);
ALX_MEM_W32(hw, ALX_RFD_BUF_SZ, sc->rxbuf_size);
/* XXX multiple queues. */
for (i = 0; i < sc->rx_ringsz; i++) {
error = alx_newbuf(sc, i);
if (error != 0) {
/* XXX this needs to be handled better. */
device_printf(sc->alx_dev, "failed to refresh mbufs\n");
break;
}
}
ALX_MEM_W16(hw, ALX_RFD_PIDX, sc->rx_ringsz - 1);
bus_dmamap_sync(sc->alx_rx_tag, sc->alx_rx_dmamap,
BUS_DMASYNC_PREWRITE);
}
static void
alx_init_tx_ring(struct alx_softc *sc)
{
struct alx_hw *hw;
struct alx_buffer *tx_buf;
int i;
ALX_LOCK_ASSERT(sc);
hw = &sc->hw;
sc->alx_tx_queue.pidx = 0;
sc->alx_tx_queue.p_reg = ALX_TPD_PRI0_PIDX;
sc->alx_tx_queue.cidx = 0;
sc->alx_tx_queue.c_reg = ALX_TPD_PRI0_CIDX;
sc->alx_tx_queue.qidx = 0;
sc->alx_tx_queue.count = sc->tx_ringsz;
hw->imask |= ALX_ISR_TX_Q0;
for (i = 0; i < sc->tx_ringsz; i++) {
tx_buf = &sc->alx_tx_queue.bf_info[i];
tx_buf->m = NULL;
}
/* XXX multiple queues */
ALX_MEM_W32(hw, ALX_TX_BASE_ADDR_HI, sc->alx_tx_queue.tpd_dma >> 32);
ALX_MEM_W32(hw, ALX_TPD_PRI0_ADDR_LO, sc->alx_tx_queue.tpd_dma);
ALX_MEM_W32(hw, ALX_TPD_RING_SZ, sc->tx_ringsz);
}
static void
alx_rxintr(struct alx_softc *sc)
{
struct mbuf *m;
struct ifnet *ifp;
struct alx_buffer *rx_buf;
struct rrd_desc *rrd;
int rrd_cidx, rfd_cidx, rrd_pidx, count;
ALX_LOCK_ASSERT(sc);
bus_dmamap_sync(sc->alx_rr_tag, sc->alx_rr_dmamap,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
bus_dmamap_sync(sc->alx_rx_tag, sc->alx_rx_dmamap,
BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
ifp = sc->alx_ifp;
count = 0;
rrd_cidx = sc->alx_rx_queue.cidx;
#if 0
printf("consuming packets starting at %d\n", rrd_cidx);
#endif
while (1) {
rrd = &sc->alx_rx_queue.rrd_hdr[rrd_cidx];
if ((rrd->word3 & (1 << RRD_UPDATED_SHIFT)) == 0)
break;
rrd->word3 &= ~(1 << RRD_UPDATED_SHIFT);
/* Get the index of the corresponding RFD. */
rfd_cidx = FIELD_GETX(rrd->word0, RRD_SI);
if (rfd_cidx != rrd_cidx ||
FIELD_GETX(rrd->word0, RRD_NOR) != 1) {
device_printf(sc->alx_dev,
"RX consumer index mismatch: %d vs. %d, and %d\n",
rrd_cidx, rfd_cidx,
FIELD_GETX(rrd->word0, RRD_NOR));
/* XXX reset the chip? */
return;
}
rx_buf = &sc->alx_rx_queue.bf_info[rfd_cidx];
m = rx_buf->m;
rx_buf->m = NULL;
m->m_flags |= M_PKTHDR;
m->m_len = FIELD_GETX(rrd->word3, RRD_PKTLEN) - ETHER_CRC_LEN;
m->m_pkthdr.len = m->m_len;
m->m_pkthdr.rcvif = ifp;
#if 0
printf("read a %d-byte packet\n", m->m_len);
#endif
/* Pass the packet up the stack. */
ALX_UNLOCK(sc);
(*ifp->if_input)(ifp, m);
ALX_LOCK(sc);
count++;
if (++rrd_cidx == sc->rx_ringsz)
rrd_cidx = 0;
}
#if 0
printf("consumed %d packets\n", count);
#endif
if (count > 0) {
sc->alx_rx_queue.cidx = rrd_cidx;
/* Refresh mbufs. */
rrd_pidx = sc->alx_rx_queue.pidx;
while (rrd_pidx != rrd_cidx) {
#if 0
printf("refreshing mbuf at %d\n", rrd_pidx);
#endif
if (alx_newbuf(sc, rrd_pidx) != 0)
break;
if (++rrd_pidx == sc->rx_ringsz)
rrd_pidx = 0;
}
sc->alx_rx_queue.pidx = rrd_pidx;
ALX_MEM_W16(&sc->hw, ALX_RFD_PIDX, rrd_pidx);
/* Sync receive descriptors. */
bus_dmamap_sync(sc->alx_rr_tag, sc->alx_rr_dmamap,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
bus_dmamap_sync(sc->alx_rx_tag, sc->alx_rx_dmamap,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
}
}
static void
alx_txintr(struct alx_softc *sc)
{
struct ifnet *ifp;
struct alx_buffer *tx_buf;
int tpd_cidx, tpd_hw_cidx;
ALX_LOCK_ASSERT(sc);
ifp = sc->alx_ifp;
tpd_cidx = sc->alx_tx_queue.cidx;
ALX_MEM_R16(&sc->hw, ALX_TPD_PRI0_CIDX, &tpd_hw_cidx);
#if 0
printf("in txintr cidx is %d, hw_cidx is %d\n", tpd_cidx, tpd_hw_cidx);
#endif
while (tpd_cidx != tpd_hw_cidx) {
tx_buf = &sc->alx_tx_queue.bf_info[tpd_cidx];
if (tx_buf->m == NULL) {
if (++tpd_cidx == sc->tx_ringsz)
tpd_cidx = 0;
continue;
}
ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
/* Tear down the DMA mapping for the used mbuf. */
bus_dmamap_sync(sc->alx_tx_buf_tag, tx_buf->dmamap,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->alx_tx_buf_tag, tx_buf->dmamap);
m_freem(tx_buf->m);
tx_buf->m = NULL;
if (++tpd_cidx == sc->tx_ringsz)
tpd_cidx = 0;
}
sc->alx_tx_queue.cidx = tpd_cidx;
}
static int
alx_newbuf(struct alx_softc *sc, int index)
{
struct mbuf *m;
bus_dma_segment_t seg;
struct alx_buffer *rx_buf;
struct rfd_desc *rfd;
int nsegs;
m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
if (m == NULL)
return (ENOBUFS);
m->m_len = m->m_pkthdr.len = MCLBYTES;
rx_buf = &sc->alx_rx_queue.bf_info[index];
if (bus_dmamap_load_mbuf_sg(sc->alx_rx_buf_tag, rx_buf->dmamap, m, &seg,
&nsegs, 0) != 0) {
m_freem(m);
return (ENOBUFS);
}
rx_buf->m = m;
bus_dmamap_sync(sc->alx_rx_buf_tag, rx_buf->dmamap,
BUS_DMASYNC_PREREAD);
rfd = &sc->alx_rx_queue.rfd_hdr[index];
rfd->addr = htole64(seg.ds_addr);
return (0);
}
static int
alx_xmit(struct alx_softc *sc, struct mbuf **m_head)
{
struct mbuf *m;
bus_dma_segment_t segs[32];
bus_dmamap_t txmap;
struct tpd_desc *td = NULL;
struct alx_buffer *tx_buf, *tx_buf_mapped;
int desci, error, nsegs, i;
uint16_t cidx;
ALX_LOCK_ASSERT(sc);
M_ASSERTPKTHDR(*m_head);
ALX_MEM_R16(&sc->hw, sc->alx_tx_queue.c_reg, &cidx);
desci = sc->alx_tx_queue.pidx;
tx_buf = tx_buf_mapped = &sc->alx_tx_queue.bf_info[desci];
txmap = tx_buf->dmamap;
error = bus_dmamap_load_mbuf_sg(sc->alx_tx_buf_tag, txmap, *m_head,
segs, &nsegs, 0);
if (error == EFBIG) {
m = m_collapse(*m_head, M_NOWAIT, 32 /* XXX magic */);
if (m == NULL) {
/* XXX increment counter? */
m_freem(*m_head);
*m_head = NULL;
return (ENOBUFS);
}
*m_head = m;
/*
* Try to create the mapping again now that some of the
* fragments have been coalesced.
*/
error = bus_dmamap_load_mbuf_sg(sc->alx_tx_buf_tag, txmap,
*m_head, segs, &nsegs, 0);
if (error != 0) {
m_freem(*m_head);
*m_head = NULL;
return (error);
}
} else if (error != 0)
/* XXX increment counter? */
return (error);
if (nsegs == 0) {
m_freem(*m_head);
*m_head = NULL;
/* XXX increment counter? */
return (EIO);
}
/* Make sure we have enough descriptors available. */
/* XXX what's up with the - 2? It's in em(4) and age(4). */
/* XXX count isn't ever modified. */
if (nsegs > sc->alx_tx_queue.count - 2) {
/* XXX increment counter? */
bus_dmamap_unload(sc->alx_tx_tag, txmap);
return (ENOBUFS);
}
for (i = 0; i < nsegs; i++, desci = ALX_TX_INC(desci, sc)) {
td = &sc->alx_tx_queue.tpd_hdr[desci];
td->addr = htole64(segs[i].ds_addr);
td->len = htole32(segs[i].ds_len);
td->flags = 0;
}
/* This is the last descriptor for this packet. */
td->flags |= 1 << TPD_EOP_SHIFT;
/* Update the producer index. */
sc->alx_tx_queue.pidx = desci;
/* Save the mbuf pointer so that we can unmap it later. */
tx_buf->m = *m_head;
/*
* Swap the maps between the first and last descriptors so that the last
* descriptor gets the real map. The first descriptor will end up with
* an unused map.
*/
tx_buf_mapped->dmamap = tx_buf->dmamap;
tx_buf->dmamap = txmap;
bus_dmamap_sync(sc->alx_tx_buf_tag, txmap, BUS_DMASYNC_PREWRITE);
/* Let the hardware know that we're all set. */
bus_dmamap_sync(sc->alx_tx_tag, sc->alx_tx_dmamap,
BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
ALX_MEM_W16(&sc->hw, sc->alx_tx_queue.p_reg, desci);
return (0);
}
static void
alx_stop(struct alx_softc *sc)
{
struct ifnet *ifp;
struct alx_hw *hw;
struct alx_buffer *tx_buf, *rx_buf;
int i, error;
ALX_LOCK_ASSERT(sc);
hw = &sc->hw;
ifp = sc->alx_ifp;
ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
alx_intr_disable(sc);
error = alx_stop_mac(hw);
if (error != 0)
device_printf(sc->alx_dev, "error stopping MAC\n");
/* XXX what else? */
for (i = 0; i < sc->tx_ringsz; i++) {
tx_buf = &sc->alx_tx_queue.bf_info[i];
if (tx_buf->m != NULL) {
bus_dmamap_sync(sc->alx_tx_buf_tag, tx_buf->dmamap,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->alx_tx_buf_tag, tx_buf->dmamap);
m_freem(tx_buf->m);
tx_buf->m = NULL;
}
}
for (i = 0; i < sc->rx_ringsz; i++) {
rx_buf = &sc->alx_rx_queue.bf_info[i];
if (rx_buf->m != NULL) {
bus_dmamap_sync(sc->alx_rx_buf_tag, rx_buf->dmamap,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->alx_rx_buf_tag, rx_buf->dmamap);
m_freem(rx_buf->m);
rx_buf->m = NULL;
}
}
}
static void
alx_reset(struct alx_softc *sc)
{
device_t dev;
struct alx_hw *hw;
bool phy_cfged;
dev = sc->alx_dev;
hw = &sc->hw;
alx_reset_pcie(hw);
phy_cfged = alx_phy_configed(hw);
if (!phy_cfged)
alx_reset_phy(hw, !hw->hib_patch);
if (alx_reset_mac(hw))
device_printf(dev, "failed to reset MAC\n");
/* XXX what else? */
}
static void
alx_update_link(struct alx_softc *sc)
{
struct alx_hw *hw;
bool link_up, prev_link_up;
int error;
uint16_t speed, prev_speed;
ALX_LOCK_ASSERT(sc);
hw = &sc->hw;
error = alx_get_phy_link(hw, &link_up, &speed);
if (error != 0)
return;
if ((!link_up && !hw->link_up) ||
(sc->alx_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
return;
prev_speed = hw->link_speed + hw->link_duplex;
prev_link_up = hw->link_up;
hw->link_up = link_up;
if (link_up) {
if (prev_link_up && prev_speed == speed)
return;
hw->link_duplex = speed % 10;
hw->link_speed = speed - hw->link_duplex;
alx_post_phy_link(hw, hw->link_speed, ALX_CAP(hw, AZ));
alx_enable_aspm(hw, ALX_CAP(hw, L0S), ALX_CAP(hw, L1));
alx_start_mac(hw);
if_link_state_change(sc->alx_ifp, LINK_STATE_UP);
} else {
hw->link_duplex = 0;
hw->link_speed = 0;