This source file includes following definitions.
- pcn_csr_read
- pcn_csr_write
- pcn_bcr_read
- pcn_bcr_write
- pcn_lookup_variant
- pcn_match
- pcn_attach
- pcn_shutdown
- pcn_start
- pcn_watchdog
- pcn_ioctl
- pcn_intr
- pcn_spnd
- pcn_txintr
- pcn_rxintr
- pcn_tick
- pcn_reset
- pcn_init
- pcn_rxdrain
- pcn_stop
- pcn_add_rxbuf
- pcn_set_filter
- pcn_79c970_mediainit
- pcn_79c970_mediastatus
- pcn_79c970_mediachange
- pcn_79c971_mediainit
- pcn_79c971_mediastatus
- pcn_79c971_mediachange
- pcn_mii_readreg
- pcn_mii_writereg
- pcn_mii_statchg
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 #if 0
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: if_pcn.c,v 1.26 2005/05/07 09:15:44 is Exp $");
71 #endif
72
73 #include "bpfilter.h"
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/timeout.h>
78 #include <sys/mbuf.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
81 #include <sys/socket.h>
82 #include <sys/ioctl.h>
83 #include <sys/errno.h>
84 #include <sys/device.h>
85 #include <sys/queue.h>
86
87 #include <net/if.h>
88 #include <net/if_dl.h>
89
90 #ifdef INET
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip.h>
95 #include <netinet/if_ether.h>
96 #endif
97
98 #include <net/if_media.h>
99
100 #if NBPFILTER > 0
101 #include <net/bpf.h>
102 #endif
103
104 #include <machine/bus.h>
105 #include <machine/intr.h>
106 #include <machine/endian.h>
107
108 #include <dev/mii/mii.h>
109 #include <dev/mii/miivar.h>
110
111 #include <dev/ic/am79900reg.h>
112 #include <dev/ic/lancereg.h>
113
114 #include <dev/pci/pcireg.h>
115 #include <dev/pci/pcivar.h>
116 #include <dev/pci/pcidevs.h>
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 #define PCN_PCI_CBIO (PCI_MAPREG_START + 0x00)
132 #define PCN_PCI_CBMEM (PCI_MAPREG_START + 0x04)
133
134
135
136
137
138 #define PCN16_APROM 0x00
139 #define PCN16_RDP 0x10
140 #define PCN16_RAP 0x12
141 #define PCN16_RESET 0x14
142 #define PCN16_BDP 0x16
143
144
145
146
147
148 #define PCN32_APROM 0x00
149 #define PCN32_RDP 0x10
150 #define PCN32_RAP 0x14
151 #define PCN32_RESET 0x18
152 #define PCN32_BDP 0x1c
153
154
155
156
157
158
159
160
161
162
163
164
165 #define PCN_NTXSEGS 16
166
167 #define PCN_TXQUEUELEN 128
168 #define PCN_TXQUEUELEN_MASK (PCN_TXQUEUELEN - 1)
169 #define PCN_NTXDESC 512
170 #define PCN_NTXDESC_MASK (PCN_NTXDESC - 1)
171 #define PCN_NEXTTX(x) (((x) + 1) & PCN_NTXDESC_MASK)
172 #define PCN_NEXTTXS(x) (((x) + 1) & PCN_TXQUEUELEN_MASK)
173
174
175 #define PCN_TXINTR_MASK 7
176
177
178
179
180
181 #define PCN_NRXDESC 128
182 #define PCN_NRXDESC_MASK (PCN_NRXDESC - 1)
183 #define PCN_NEXTRX(x) (((x) + 1) & PCN_NRXDESC_MASK)
184
185
186
187
188
189
190 struct pcn_control_data {
191
192 struct letmd pcd_txdescs[PCN_NTXDESC];
193
194
195 struct lermd pcd_rxdescs[PCN_NRXDESC];
196
197
198 struct leinit pcd_initblock;
199 };
200
201 #define PCN_CDOFF(x) offsetof(struct pcn_control_data, x)
202 #define PCN_CDTXOFF(x) PCN_CDOFF(pcd_txdescs[(x)])
203 #define PCN_CDRXOFF(x) PCN_CDOFF(pcd_rxdescs[(x)])
204 #define PCN_CDINITOFF PCN_CDOFF(pcd_initblock)
205
206
207
208
209 struct pcn_txsoft {
210 struct mbuf *txs_mbuf;
211 bus_dmamap_t txs_dmamap;
212 int txs_firstdesc;
213 int txs_lastdesc;
214 };
215
216
217
218
219 struct pcn_rxsoft {
220 struct mbuf *rxs_mbuf;
221 bus_dmamap_t rxs_dmamap;
222 };
223
224
225
226
227 static const char * const pcn_79c970_rcvfw[] = {
228 "16 bytes",
229 "64 bytes",
230 "128 bytes",
231 NULL,
232 };
233
234 static const char * const pcn_79c971_rcvfw[] = {
235 "16 bytes",
236 "64 bytes",
237 "112 bytes",
238 NULL,
239 };
240
241
242
243
244 static const char * const pcn_79c970_xmtsp[] = {
245 "8 bytes",
246 "64 bytes",
247 "128 bytes",
248 "248 bytes",
249 };
250
251 static const char * const pcn_79c971_xmtsp[] = {
252 "20 bytes",
253 "64 bytes",
254 "128 bytes",
255 "248 bytes",
256 };
257
258 static const char * const pcn_79c971_xmtsp_sram[] = {
259 "44 bytes",
260 "64 bytes",
261 "128 bytes",
262 "store-and-forward",
263 };
264
265
266
267
268 static const char * const pcn_79c970_xmtfw[] = {
269 "16 bytes",
270 "64 bytes",
271 "128 bytes",
272 NULL,
273 };
274
275 static const char * const pcn_79c971_xmtfw[] = {
276 "16 bytes",
277 "64 bytes",
278 "108 bytes",
279 NULL,
280 };
281
282
283
284
285 struct pcn_softc {
286 struct device sc_dev;
287 bus_space_tag_t sc_st;
288 bus_space_handle_t sc_sh;
289 bus_dma_tag_t sc_dmat;
290 struct arpcom sc_arpcom;
291 void *sc_sdhook;
292
293
294 const struct pcn_variant *sc_variant;
295
296 void *sc_ih;
297
298 struct mii_data sc_mii;
299
300 struct timeout sc_tick_timeout;
301
302 bus_dmamap_t sc_cddmamap;
303 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
304
305
306 struct pcn_txsoft sc_txsoft[PCN_TXQUEUELEN];
307 struct pcn_rxsoft sc_rxsoft[PCN_NRXDESC];
308
309
310 struct pcn_control_data *sc_control_data;
311 #define sc_txdescs sc_control_data->pcd_txdescs
312 #define sc_rxdescs sc_control_data->pcd_rxdescs
313 #define sc_initblock sc_control_data->pcd_initblock
314
315 const char * const *sc_rcvfw_desc;
316 int sc_rcvfw;
317
318 const char * const *sc_xmtsp_desc;
319 int sc_xmtsp;
320
321 const char * const *sc_xmtfw_desc;
322 int sc_xmtfw;
323
324 int sc_flags;
325 int sc_swstyle;
326
327 int sc_txfree;
328 int sc_txnext;
329
330 int sc_txsfree;
331 int sc_txsnext;
332 int sc_txsdirty;
333
334 int sc_rxptr;
335
336 uint32_t sc_csr5;
337 uint32_t sc_mode;
338 };
339
340
341 #define PCN_F_HAS_MII 0x0001
342
343 #define PCN_CDTXADDR(sc, x) ((sc)->sc_cddma + PCN_CDTXOFF((x)))
344 #define PCN_CDRXADDR(sc, x) ((sc)->sc_cddma + PCN_CDRXOFF((x)))
345 #define PCN_CDINITADDR(sc) ((sc)->sc_cddma + PCN_CDINITOFF)
346
347 #define PCN_CDTXSYNC(sc, x, n, ops) \
348 do { \
349 int __x, __n; \
350 \
351 __x = (x); \
352 __n = (n); \
353 \
354 \
355 if ((__x + __n) > PCN_NTXDESC) { \
356 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
357 PCN_CDTXOFF(__x), sizeof(struct letmd) * \
358 (PCN_NTXDESC - __x), (ops)); \
359 __n -= (PCN_NTXDESC - __x); \
360 __x = 0; \
361 } \
362 \
363 \
364 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
365 PCN_CDTXOFF(__x), sizeof(struct letmd) * __n, (ops)); \
366 } while (0)
367
368 #define PCN_CDRXSYNC(sc, x, ops) \
369 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
370 PCN_CDRXOFF((x)), sizeof(struct lermd), (ops))
371
372 #define PCN_CDINITSYNC(sc, ops) \
373 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
374 PCN_CDINITOFF, sizeof(struct leinit), (ops))
375
376 #define PCN_INIT_RXDESC(sc, x) \
377 do { \
378 struct pcn_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)]; \
379 struct lermd *__rmd = &(sc)->sc_rxdescs[(x)]; \
380 struct mbuf *__m = __rxs->rxs_mbuf; \
381 \
382
383
384
385
386 \
387 __m->m_data = __m->m_ext.ext_buf + 2; \
388 \
389 if ((sc)->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3) { \
390 __rmd->rmd2 = \
391 htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr + 2); \
392 __rmd->rmd0 = 0; \
393 } else { \
394 __rmd->rmd2 = 0; \
395 __rmd->rmd0 = \
396 htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr + 2); \
397 } \
398 __rmd->rmd1 = htole32(LE_R1_OWN|LE_R1_ONES| \
399 (LE_BCNT(MCLBYTES - 2) & LE_R1_BCNT_MASK)); \
400 PCN_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);\
401 } while(0)
402
403 void pcn_start(struct ifnet *);
404 void pcn_watchdog(struct ifnet *);
405 int pcn_ioctl(struct ifnet *, u_long, caddr_t);
406 int pcn_init(struct ifnet *);
407 void pcn_stop(struct ifnet *, int);
408
409 void pcn_shutdown(void *);
410
411 void pcn_reset(struct pcn_softc *);
412 void pcn_rxdrain(struct pcn_softc *);
413 int pcn_add_rxbuf(struct pcn_softc *, int);
414 void pcn_tick(void *);
415
416 void pcn_spnd(struct pcn_softc *);
417
418 void pcn_set_filter(struct pcn_softc *);
419
420 int pcn_intr(void *);
421 void pcn_txintr(struct pcn_softc *);
422 int pcn_rxintr(struct pcn_softc *);
423
424 int pcn_mii_readreg(struct device *, int, int);
425 void pcn_mii_writereg(struct device *, int, int, int);
426 void pcn_mii_statchg(struct device *);
427
428 void pcn_79c970_mediainit(struct pcn_softc *);
429 int pcn_79c970_mediachange(struct ifnet *);
430 void pcn_79c970_mediastatus(struct ifnet *, struct ifmediareq *);
431
432 void pcn_79c971_mediainit(struct pcn_softc *);
433 int pcn_79c971_mediachange(struct ifnet *);
434 void pcn_79c971_mediastatus(struct ifnet *, struct ifmediareq *);
435
436
437
438
439
440 static const struct pcn_variant {
441 const char *pcv_desc;
442 void (*pcv_mediainit)(struct pcn_softc *);
443 uint16_t pcv_chipid;
444 } pcn_variants[] = {
445 { "Am79c970",
446 pcn_79c970_mediainit,
447 PARTID_Am79c970 },
448
449 { "Am79c970A",
450 pcn_79c970_mediainit,
451 PARTID_Am79c970A },
452
453 { "Am79c971",
454 pcn_79c971_mediainit,
455 PARTID_Am79c971 },
456
457 { "Am79c972",
458 pcn_79c971_mediainit,
459 PARTID_Am79c972 },
460
461 { "Am79c973",
462 pcn_79c971_mediainit,
463 PARTID_Am79c973 },
464
465 { "Am79c975",
466 pcn_79c971_mediainit,
467 PARTID_Am79c975 },
468
469 { "Am79c976",
470 pcn_79c971_mediainit,
471 PARTID_Am79c976 },
472
473 { "Am79c978",
474 pcn_79c971_mediainit,
475 PARTID_Am79c978 },
476
477 { "Unknown",
478 pcn_79c971_mediainit,
479 0 },
480 };
481
482 int pcn_copy_small = 0;
483
484 int pcn_match(struct device *, void *, void *);
485 void pcn_attach(struct device *, struct device *, void *);
486
487 struct cfattach pcn_ca = {
488 sizeof(struct pcn_softc), pcn_match, pcn_attach,
489 };
490
491 const struct pci_matchid pcn_devices[] = {
492 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_PCNET_PCI },
493 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_PCHOME_PCI }
494 };
495
496 struct cfdriver pcn_cd = {
497 0, "pcn", DV_IFNET
498 };
499
500
501
502
503
504 static __inline uint32_t
505 pcn_csr_read(struct pcn_softc *sc, int reg)
506 {
507
508 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg);
509 return (bus_space_read_4(sc->sc_st, sc->sc_sh, PCN32_RDP));
510 }
511
512 static __inline void
513 pcn_csr_write(struct pcn_softc *sc, int reg, uint32_t val)
514 {
515
516 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg);
517 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RDP, val);
518 }
519
520 static __inline uint32_t
521 pcn_bcr_read(struct pcn_softc *sc, int reg)
522 {
523
524 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg);
525 return (bus_space_read_4(sc->sc_st, sc->sc_sh, PCN32_BDP));
526 }
527
528 static __inline void
529 pcn_bcr_write(struct pcn_softc *sc, int reg, uint32_t val)
530 {
531
532 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RAP, reg);
533 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_BDP, val);
534 }
535
536 static const struct pcn_variant *
537 pcn_lookup_variant(uint16_t chipid)
538 {
539 const struct pcn_variant *pcv;
540
541 for (pcv = pcn_variants; pcv->pcv_chipid != 0; pcv++) {
542 if (chipid == pcv->pcv_chipid)
543 return (pcv);
544 }
545
546
547
548
549
550 return (pcv);
551 }
552
553 int
554 pcn_match(struct device *parent, void *match, void *aux)
555 {
556 struct pci_attach_args *pa = aux;
557
558
559
560
561
562
563
564 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TRIDENT &&
565 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TRIDENT_4DWAVE_DX &&
566 PCI_CLASS(pa->pa_class) == PCI_CLASS_NETWORK)
567 return(1);
568
569 return (pci_matchbyid((struct pci_attach_args *)aux, pcn_devices,
570 sizeof(pcn_devices)/sizeof(pcn_devices[0])));
571 }
572
573 void
574 pcn_attach(struct device *parent, struct device *self, void *aux)
575 {
576 struct pcn_softc *sc = (struct pcn_softc *) self;
577 struct pci_attach_args *pa = aux;
578 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
579 pci_chipset_tag_t pc = pa->pa_pc;
580 pci_intr_handle_t ih;
581 const char *intrstr = NULL;
582 bus_space_tag_t iot, memt;
583 bus_space_handle_t ioh, memh;
584 bus_dma_segment_t seg;
585 int ioh_valid, memh_valid;
586 int i, rseg, error;
587 pcireg_t pmode;
588 uint32_t chipid, reg;
589 uint8_t enaddr[ETHER_ADDR_LEN];
590 int pmreg;
591
592 timeout_set(&sc->sc_tick_timeout, pcn_tick, sc);
593
594
595
596
597 ioh_valid = (pci_mapreg_map(pa, PCN_PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
598 &iot, &ioh, NULL, NULL, 0) == 0);
599 memh_valid = (pci_mapreg_map(pa, PCN_PCI_CBMEM,
600 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
601 &memt, &memh, NULL, NULL, 0) == 0);
602
603 if (memh_valid) {
604 sc->sc_st = memt;
605 sc->sc_sh = memh;
606 } else if (ioh_valid) {
607 sc->sc_st = iot;
608 sc->sc_sh = ioh;
609 } else {
610 printf(": unable to map device registers\n");
611 return;
612 }
613
614 sc->sc_dmat = pa->pa_dmat;
615
616
617 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
618 pmode = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR) &
619 PCI_PMCSR_STATE_MASK;
620 if (pmode == PCI_PMCSR_STATE_D3) {
621
622
623
624
625 printf(": unable to wake from power state D3\n");
626 return;
627 }
628 if (pmode != PCI_PMCSR_STATE_D0) {
629 printf(": waking up from power date D%d",
630 pmode);
631 pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
632 PCI_PMCSR_STATE_D0);
633 }
634 }
635
636
637
638
639
640 pcn_reset(sc);
641
642 #if !defined(PCN_NO_PROM)
643
644
645
646
647 for (i = 0; i < ETHER_ADDR_LEN; i++)
648 enaddr[i] = bus_space_read_1(sc->sc_st, sc->sc_sh,
649 PCN32_APROM + i);
650 #else
651
652
653
654
655
656
657 for (i=0; i < 3; i++) {
658 uint32_t val;
659 val = pcn_csr_read(sc, LE_CSR12 + i);
660 enaddr[2*i] = val & 0x0ff;
661 enaddr[2*i+1] = (val >> 8) & 0x0ff;
662 }
663 #endif
664
665
666
667
668
669
670 chipid = pcn_csr_read(sc, LE_CSR88);
671 sc->sc_variant = pcn_lookup_variant(CHIPID_PARTID(chipid));
672
673
674
675
676 if (pci_intr_map(pa, &ih)) {
677 printf(": unable to map interrupt\n");
678 return;
679 }
680 intrstr = pci_intr_string(pc, ih);
681 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, pcn_intr, sc,
682 self->dv_xname);
683 if (sc->sc_ih == NULL) {
684 printf(": unable to establish interrupt");
685 if (intrstr != NULL)
686 printf(" at %s", intrstr);
687 printf("\n");
688 return;
689 }
690
691
692
693
694
695 if ((error = bus_dmamem_alloc(sc->sc_dmat,
696 sizeof(struct pcn_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
697 0)) != 0) {
698 printf(": unable to allocate control data, error = %d\n",
699 error);
700 return;
701 }
702
703 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
704 sizeof(struct pcn_control_data), (caddr_t *)&sc->sc_control_data,
705 BUS_DMA_COHERENT)) != 0) {
706 printf(": unable to map control data, error = %d\n",
707 error);
708 goto fail_1;
709 }
710
711 if ((error = bus_dmamap_create(sc->sc_dmat,
712 sizeof(struct pcn_control_data), 1,
713 sizeof(struct pcn_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
714 printf(": unable to create control data DMA map, "
715 "error = %d\n", error);
716 goto fail_2;
717 }
718
719 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
720 sc->sc_control_data, sizeof(struct pcn_control_data), NULL,
721 0)) != 0) {
722 printf(": unable to load control data DMA map, error = %d\n",
723 error);
724 goto fail_3;
725 }
726
727
728 for (i = 0; i < PCN_TXQUEUELEN; i++) {
729 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
730 PCN_NTXSEGS, MCLBYTES, 0, 0,
731 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
732 printf(": unable to create tx DMA map %d, "
733 "error = %d\n", i, error);
734 goto fail_4;
735 }
736 }
737
738
739 for (i = 0; i < PCN_NRXDESC; i++) {
740 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
741 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
742 printf(": unable to create rx DMA map %d, "
743 "error = %d\n", i, error);
744 goto fail_5;
745 }
746 sc->sc_rxsoft[i].rxs_mbuf = NULL;
747 }
748
749 printf(", %s, rev %d: %s, address %s\n", sc->sc_variant->pcv_desc,
750 CHIPID_VER(chipid), intrstr, ether_sprintf(enaddr));
751
752
753 (*sc->sc_variant->pcv_mediainit)(sc);
754
755
756
757
758 switch (sc->sc_variant->pcv_chipid) {
759 case PARTID_Am79c970:
760 case PARTID_Am79c970A:
761 sc->sc_rcvfw_desc = pcn_79c970_rcvfw;
762 sc->sc_xmtsp_desc = pcn_79c970_xmtsp;
763 sc->sc_xmtfw_desc = pcn_79c970_xmtfw;
764 break;
765
766 default:
767 sc->sc_rcvfw_desc = pcn_79c971_rcvfw;
768
769
770
771
772
773
774
775
776
777
778 reg = pcn_bcr_read(sc, LE_BCR25) & 0x00ff;
779 if (reg != 0)
780 sc->sc_xmtsp_desc = pcn_79c971_xmtsp_sram;
781 else
782 sc->sc_xmtsp_desc = pcn_79c971_xmtsp;
783 sc->sc_xmtfw_desc = pcn_79c971_xmtfw;
784 break;
785 }
786
787
788
789
790
791
792
793 sc->sc_rcvfw = 1;
794 sc->sc_xmtsp = 1;
795 sc->sc_xmtfw = 0;
796
797 ifp = &sc->sc_arpcom.ac_if;
798 bcopy(enaddr, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
799 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
800 ifp->if_softc = sc;
801 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
802 ifp->if_ioctl = pcn_ioctl;
803 ifp->if_start = pcn_start;
804 ifp->if_watchdog = pcn_watchdog;
805 IFQ_SET_MAXLEN(&ifp->if_snd, PCN_NTXDESC -1);
806 IFQ_SET_READY(&ifp->if_snd);
807
808
809 if_attach(ifp);
810 ether_ifattach(ifp);
811
812
813 sc->sc_sdhook = shutdownhook_establish(pcn_shutdown, sc);
814 if (sc->sc_sdhook == NULL)
815 printf("%s: WARNING: unable to establish shutdown hook\n",
816 sc->sc_dev.dv_xname);
817 return;
818
819
820
821
822
823 fail_5:
824 for (i = 0; i < PCN_NRXDESC; i++) {
825 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
826 bus_dmamap_destroy(sc->sc_dmat,
827 sc->sc_rxsoft[i].rxs_dmamap);
828 }
829 fail_4:
830 for (i = 0; i < PCN_TXQUEUELEN; i++) {
831 if (sc->sc_txsoft[i].txs_dmamap != NULL)
832 bus_dmamap_destroy(sc->sc_dmat,
833 sc->sc_txsoft[i].txs_dmamap);
834 }
835 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
836 fail_3:
837 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
838 fail_2:
839 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
840 sizeof(struct pcn_control_data));
841 fail_1:
842 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
843 }
844
845
846
847
848
849
850 void
851 pcn_shutdown(void *arg)
852 {
853 struct pcn_softc *sc = arg;
854
855 pcn_stop(&sc->sc_arpcom.ac_if, 1);
856 pcn_reset(sc);
857 }
858
859
860
861
862
863
864 void
865 pcn_start(struct ifnet *ifp)
866 {
867 struct pcn_softc *sc = ifp->if_softc;
868 struct mbuf *m0, *m;
869 struct pcn_txsoft *txs;
870 bus_dmamap_t dmamap;
871 int error, nexttx, lasttx = -1, ofree, seg;
872
873 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
874 return;
875
876
877
878
879
880 ofree = sc->sc_txfree;
881
882
883
884
885
886
887 for (;;) {
888
889 IFQ_POLL(&ifp->if_snd, m0);
890 if (m0 == NULL)
891 break;
892 m = NULL;
893
894
895 if (sc->sc_txsfree == 0)
896 break;
897
898 txs = &sc->sc_txsoft[sc->sc_txsnext];
899 dmamap = txs->txs_dmamap;
900
901
902
903
904
905
906
907 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
908 BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
909 MGETHDR(m, M_DONTWAIT, MT_DATA);
910 if (m == NULL)
911 break;
912 if (m0->m_pkthdr.len > MHLEN) {
913 MCLGET(m, M_DONTWAIT);
914 if ((m->m_flags & M_EXT) == 0) {
915 m_freem(m);
916 break;
917 }
918 }
919 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
920 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
921 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
922 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
923 if (error)
924 break;
925 }
926
927
928
929
930
931
932
933 if (dmamap->dm_nsegs > (sc->sc_txfree - 1)) {
934
935
936
937
938
939
940
941
942
943
944 ifp->if_flags |= IFF_OACTIVE;
945 bus_dmamap_unload(sc->sc_dmat, dmamap);
946 if (m != NULL)
947 m_freem(m);
948 break;
949 }
950
951 IFQ_DEQUEUE(&ifp->if_snd, m0);
952 if (m != NULL) {
953 m_freem(m0);
954 m0 = m;
955 }
956
957
958
959
960
961
962 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
963 BUS_DMASYNC_PREWRITE);
964
965
966
967
968 if (sc->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3) {
969 for (nexttx = sc->sc_txnext, seg = 0;
970 seg < dmamap->dm_nsegs;
971 seg++, nexttx = PCN_NEXTTX(nexttx)) {
972
973
974
975
976
977
978 sc->sc_txdescs[nexttx].tmd0 = 0;
979 sc->sc_txdescs[nexttx].tmd2 =
980 htole32(dmamap->dm_segs[seg].ds_addr);
981 sc->sc_txdescs[nexttx].tmd1 =
982 htole32(LE_T1_ONES |
983 (nexttx == sc->sc_txnext ? 0 : LE_T1_OWN) |
984 (LE_BCNT(dmamap->dm_segs[seg].ds_len) &
985 LE_T1_BCNT_MASK));
986 lasttx = nexttx;
987 }
988 } else {
989 for (nexttx = sc->sc_txnext, seg = 0;
990 seg < dmamap->dm_nsegs;
991 seg++, nexttx = PCN_NEXTTX(nexttx)) {
992
993
994
995
996
997
998 sc->sc_txdescs[nexttx].tmd0 =
999 htole32(dmamap->dm_segs[seg].ds_addr);
1000 sc->sc_txdescs[nexttx].tmd2 = 0;
1001 sc->sc_txdescs[nexttx].tmd1 =
1002 htole32(LE_T1_ONES |
1003 (nexttx == sc->sc_txnext ? 0 : LE_T1_OWN) |
1004 (LE_BCNT(dmamap->dm_segs[seg].ds_len) &
1005 LE_T1_BCNT_MASK));
1006 lasttx = nexttx;
1007 }
1008 }
1009
1010 KASSERT(lasttx != -1);
1011
1012 if ((sc->sc_txsnext & PCN_TXINTR_MASK) == 0)
1013 sc->sc_txdescs[lasttx].tmd1 |= htole32(LE_T1_LTINT);
1014
1015
1016 sc->sc_txdescs[lasttx].tmd1 |= htole32(LE_T1_ENP);
1017 sc->sc_txdescs[sc->sc_txnext].tmd1 |=
1018 htole32(LE_T1_OWN|LE_T1_STP);
1019
1020
1021 PCN_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
1022 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1023
1024
1025 pcn_csr_write(sc, LE_CSR0, LE_C0_INEA|LE_C0_TDMD);
1026
1027
1028
1029
1030
1031
1032 txs->txs_mbuf = m0;
1033 txs->txs_firstdesc = sc->sc_txnext;
1034 txs->txs_lastdesc = lasttx;
1035
1036
1037 sc->sc_txfree -= dmamap->dm_nsegs;
1038 sc->sc_txnext = nexttx;
1039
1040 sc->sc_txsfree--;
1041 sc->sc_txsnext = PCN_NEXTTXS(sc->sc_txsnext);
1042
1043 #if NBPFILTER > 0
1044
1045 if (ifp->if_bpf)
1046 bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
1047 #endif
1048 }
1049
1050 if (sc->sc_txsfree == 0 || sc->sc_txfree == 0) {
1051
1052 ifp->if_flags |= IFF_OACTIVE;
1053 }
1054
1055 if (sc->sc_txfree != ofree) {
1056
1057 ifp->if_timer = 5;
1058 }
1059 }
1060
1061
1062
1063
1064
1065
1066 void
1067 pcn_watchdog(struct ifnet *ifp)
1068 {
1069 struct pcn_softc *sc = ifp->if_softc;
1070
1071
1072
1073
1074
1075 pcn_txintr(sc);
1076
1077 if (sc->sc_txfree != PCN_NTXDESC) {
1078 printf("%s: device timeout (txfree %d txsfree %d)\n",
1079 sc->sc_dev.dv_xname, sc->sc_txfree, sc->sc_txsfree);
1080 ifp->if_oerrors++;
1081
1082
1083 (void) pcn_init(ifp);
1084 }
1085
1086
1087 pcn_start(ifp);
1088 }
1089
1090
1091
1092
1093
1094
1095 int
1096 pcn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1097 {
1098 struct pcn_softc *sc = ifp->if_softc;
1099 struct ifreq *ifr = (struct ifreq *) data;
1100 struct ifaddr *ifa = (struct ifaddr *)data;
1101 int s, error = 0;
1102
1103 s = splnet();
1104
1105 if ((error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data)) > 0) {
1106
1107 pcn_start(ifp);
1108
1109 splx(s);
1110 return (error);
1111 }
1112
1113 switch (cmd) {
1114 case SIOCSIFADDR:
1115 ifp->if_flags |= IFF_UP;
1116
1117 switch (ifa->ifa_addr->sa_family) {
1118 #ifdef INET
1119 case AF_INET:
1120 pcn_init(ifp);
1121 arp_ifinit(&sc->sc_arpcom, ifa);
1122 break;
1123 #endif
1124 default:
1125 pcn_init(ifp);
1126 break;
1127 }
1128 break;
1129
1130 case SIOCSIFMTU:
1131 if (ifr->ifr_mtu > ETHERMTU || ifr->ifr_mtu < ETHERMIN)
1132 error = EINVAL;
1133 else if (ifp->if_mtu != ifr->ifr_mtu)
1134 ifp->if_mtu = ifr->ifr_mtu;
1135 break;
1136
1137 case SIOCSIFFLAGS:
1138
1139
1140
1141
1142
1143
1144 if (ifp->if_flags & IFF_UP)
1145 pcn_init(ifp);
1146 else if (ifp->if_flags & IFF_RUNNING)
1147 pcn_stop(ifp, 1);
1148 break;
1149
1150 case SIOCADDMULTI:
1151 case SIOCDELMULTI:
1152 error = (cmd == SIOCADDMULTI) ?
1153 ether_addmulti(ifr, &sc->sc_arpcom) :
1154 ether_delmulti(ifr, &sc->sc_arpcom);
1155
1156 if (error == ENETRESET) {
1157
1158
1159
1160
1161 if (ifp->if_flags & IFF_RUNNING)
1162 error = pcn_init(ifp);
1163 else
1164 error = 0;
1165 }
1166 break;
1167
1168 case SIOCSIFMEDIA:
1169 case SIOCGIFMEDIA:
1170 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1171 break;
1172
1173 default:
1174 error = ENOTTY;
1175 }
1176
1177
1178 pcn_start(ifp);
1179
1180 splx(s);
1181 return (error);
1182 }
1183
1184
1185
1186
1187
1188
1189 int
1190 pcn_intr(void *arg)
1191 {
1192 struct pcn_softc *sc = arg;
1193 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1194 uint32_t csr0;
1195 int wantinit, handled = 0;
1196
1197 for (wantinit = 0; wantinit == 0;) {
1198 csr0 = pcn_csr_read(sc, LE_CSR0);
1199 if ((csr0 & LE_C0_INTR) == 0)
1200 break;
1201
1202
1203 pcn_csr_write(sc, LE_CSR0, csr0 &
1204 (LE_C0_INEA|LE_C0_BABL|LE_C0_MISS|LE_C0_MERR|LE_C0_RINT|
1205 LE_C0_TINT|LE_C0_IDON));
1206
1207 handled = 1;
1208
1209 if (csr0 & LE_C0_RINT)
1210 wantinit = pcn_rxintr(sc);
1211
1212 if (csr0 & LE_C0_TINT)
1213 pcn_txintr(sc);
1214
1215 if (csr0 & LE_C0_ERR) {
1216 if (csr0 & LE_C0_BABL)
1217 ifp->if_oerrors++;
1218 if (csr0 & LE_C0_MISS)
1219 ifp->if_ierrors++;
1220 if (csr0 & LE_C0_MERR) {
1221 printf("%s: memory error\n",
1222 sc->sc_dev.dv_xname);
1223 wantinit = 1;
1224 break;
1225 }
1226 }
1227
1228 if ((csr0 & LE_C0_RXON) == 0) {
1229 printf("%s: receiver disabled\n",
1230 sc->sc_dev.dv_xname);
1231 ifp->if_ierrors++;
1232 wantinit = 1;
1233 }
1234
1235 if ((csr0 & LE_C0_TXON) == 0) {
1236 printf("%s: transmitter disabled\n",
1237 sc->sc_dev.dv_xname);
1238 ifp->if_oerrors++;
1239 wantinit = 1;
1240 }
1241 }
1242
1243 if (handled) {
1244 if (wantinit)
1245 pcn_init(ifp);
1246
1247
1248 pcn_start(ifp);
1249 }
1250
1251 return (handled);
1252 }
1253
1254
1255
1256
1257
1258
1259 void
1260 pcn_spnd(struct pcn_softc *sc)
1261 {
1262 int i;
1263
1264 pcn_csr_write(sc, LE_CSR5, sc->sc_csr5 | LE_C5_SPND);
1265
1266 for (i = 0; i < 10000; i++) {
1267 if (pcn_csr_read(sc, LE_CSR5) & LE_C5_SPND)
1268 return;
1269 delay(5);
1270 }
1271
1272 printf("%s: WARNING: chip failed to enter suspended state\n",
1273 sc->sc_dev.dv_xname);
1274 }
1275
1276
1277
1278
1279
1280
1281 void
1282 pcn_txintr(struct pcn_softc *sc)
1283 {
1284 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1285 struct pcn_txsoft *txs;
1286 uint32_t tmd1, tmd2, tmd;
1287 int i, j;
1288
1289 ifp->if_flags &= ~IFF_OACTIVE;
1290
1291
1292
1293
1294
1295 for (i = sc->sc_txsdirty; sc->sc_txsfree != PCN_TXQUEUELEN;
1296 i = PCN_NEXTTXS(i), sc->sc_txsfree++) {
1297 txs = &sc->sc_txsoft[i];
1298
1299 PCN_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
1300 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1301
1302 tmd1 = letoh32(sc->sc_txdescs[txs->txs_lastdesc].tmd1);
1303 if (tmd1 & LE_T1_OWN)
1304 break;
1305
1306
1307
1308
1309
1310
1311 for (j = txs->txs_firstdesc;; j = PCN_NEXTTX(j)) {
1312 tmd = letoh32(sc->sc_txdescs[j].tmd1);
1313 if (tmd & LE_T1_ERR) {
1314 ifp->if_oerrors++;
1315 if (sc->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3)
1316 tmd2 = letoh32(sc->sc_txdescs[j].tmd0);
1317 else
1318 tmd2 = letoh32(sc->sc_txdescs[j].tmd2);
1319 if (tmd2 & LE_T2_UFLO) {
1320 if (sc->sc_xmtsp < LE_C80_XMTSP_MAX) {
1321 sc->sc_xmtsp++;
1322 printf("%s: transmit "
1323 "underrun; new threshold: "
1324 "%s\n",
1325 sc->sc_dev.dv_xname,
1326 sc->sc_xmtsp_desc[
1327 sc->sc_xmtsp]);
1328 pcn_spnd(sc);
1329 pcn_csr_write(sc, LE_CSR80,
1330 LE_C80_RCVFW(sc->sc_rcvfw) |
1331 LE_C80_XMTSP(sc->sc_xmtsp) |
1332 LE_C80_XMTFW(sc->sc_xmtfw));
1333 pcn_csr_write(sc, LE_CSR5,
1334 sc->sc_csr5);
1335 } else {
1336 printf("%s: transmit "
1337 "underrun\n",
1338 sc->sc_dev.dv_xname);
1339 }
1340 } else if (tmd2 & LE_T2_BUFF) {
1341 printf("%s: transmit buffer error\n",
1342 sc->sc_dev.dv_xname);
1343 }
1344 if (tmd2 & LE_T2_LCOL)
1345 ifp->if_collisions++;
1346 if (tmd2 & LE_T2_RTRY)
1347 ifp->if_collisions += 16;
1348 goto next_packet;
1349 }
1350 if (j == txs->txs_lastdesc)
1351 break;
1352 }
1353 if (tmd1 & LE_T1_ONE)
1354 ifp->if_collisions++;
1355 else if (tmd & LE_T1_MORE) {
1356
1357 ifp->if_collisions += 2;
1358 }
1359 ifp->if_opackets++;
1360 next_packet:
1361 sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
1362 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1363 0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1364 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1365 m_freem(txs->txs_mbuf);
1366 txs->txs_mbuf = NULL;
1367 }
1368
1369
1370 sc->sc_txsdirty = i;
1371
1372
1373
1374
1375
1376 if (sc->sc_txsfree == PCN_TXQUEUELEN)
1377 ifp->if_timer = 0;
1378 }
1379
1380
1381
1382
1383
1384
1385 int
1386 pcn_rxintr(struct pcn_softc *sc)
1387 {
1388 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1389 struct pcn_rxsoft *rxs;
1390 struct mbuf *m;
1391 uint32_t rmd1;
1392 int i, len;
1393
1394 for (i = sc->sc_rxptr;; i = PCN_NEXTRX(i)) {
1395 rxs = &sc->sc_rxsoft[i];
1396
1397 PCN_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1398
1399 rmd1 = letoh32(sc->sc_rxdescs[i].rmd1);
1400
1401 if (rmd1 & LE_R1_OWN)
1402 break;
1403
1404
1405
1406
1407
1408
1409
1410 if (__predict_false((rmd1 & (LE_R1_STP|LE_R1_ENP|LE_R1_ERR)) !=
1411 (LE_R1_STP|LE_R1_ENP))) {
1412
1413 if ((rmd1 & (LE_R1_STP|LE_R1_ENP)) !=
1414 (LE_R1_STP|LE_R1_ENP)) {
1415 printf("%s: packet spilled into next buffer\n",
1416 sc->sc_dev.dv_xname);
1417 return (1);
1418 }
1419
1420
1421
1422
1423
1424 if (rmd1 & LE_R1_ERR) {
1425 ifp->if_ierrors++;
1426
1427
1428
1429
1430
1431
1432 if (rmd1 & LE_R1_OFLO)
1433 printf("%s: overflow error\n",
1434 sc->sc_dev.dv_xname);
1435 else {
1436 #define PRINTIT(x, str) \
1437 if (rmd1 & (x)) \
1438 printf("%s: %s\n", \
1439 sc->sc_dev.dv_xname, str);
1440 PRINTIT(LE_R1_FRAM, "framing error");
1441 PRINTIT(LE_R1_CRC, "CRC error");
1442 PRINTIT(LE_R1_BUFF, "buffer error");
1443 }
1444 #undef PRINTIT
1445 PCN_INIT_RXDESC(sc, i);
1446 continue;
1447 }
1448 }
1449
1450 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1451 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1452
1453
1454
1455
1456 if (sc->sc_swstyle == LE_B20_SSTYLE_PCNETPCI3)
1457 len = letoh32(sc->sc_rxdescs[i].rmd0) & LE_R1_BCNT_MASK;
1458 else
1459 len = letoh32(sc->sc_rxdescs[i].rmd2) & LE_R1_BCNT_MASK;
1460
1461
1462
1463
1464
1465 len -= ETHER_CRC_LEN;
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478 if (pcn_copy_small != 0 && len <= (MHLEN - 2)) {
1479 MGETHDR(m, M_DONTWAIT, MT_DATA);
1480 if (m == NULL)
1481 goto dropit;
1482 m->m_data += 2;
1483 memcpy(mtod(m, caddr_t),
1484 mtod(rxs->rxs_mbuf, caddr_t), len);
1485 PCN_INIT_RXDESC(sc, i);
1486 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1487 rxs->rxs_dmamap->dm_mapsize,
1488 BUS_DMASYNC_PREREAD);
1489 } else {
1490 m = rxs->rxs_mbuf;
1491 if (pcn_add_rxbuf(sc, i) != 0) {
1492 dropit:
1493 ifp->if_ierrors++;
1494 PCN_INIT_RXDESC(sc, i);
1495 bus_dmamap_sync(sc->sc_dmat,
1496 rxs->rxs_dmamap, 0,
1497 rxs->rxs_dmamap->dm_mapsize,
1498 BUS_DMASYNC_PREREAD);
1499 continue;
1500 }
1501 }
1502
1503 m->m_pkthdr.rcvif = ifp;
1504 m->m_pkthdr.len = m->m_len = len;
1505
1506 #if NBPFILTER > 0
1507
1508 if (ifp->if_bpf)
1509 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
1510 #endif
1511
1512
1513 ether_input_mbuf(ifp, m);
1514 ifp->if_ipackets++;
1515 }
1516
1517
1518 sc->sc_rxptr = i;
1519 return (0);
1520 }
1521
1522
1523
1524
1525
1526
1527 void
1528 pcn_tick(void *arg)
1529 {
1530 struct pcn_softc *sc = arg;
1531 int s;
1532
1533 s = splnet();
1534 mii_tick(&sc->sc_mii);
1535 splx(s);
1536
1537 timeout_add(&sc->sc_tick_timeout, hz);
1538 }
1539
1540
1541
1542
1543
1544
1545 void
1546 pcn_reset(struct pcn_softc *sc)
1547 {
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559 (void) bus_space_read_2(sc->sc_st, sc->sc_sh, PCN16_RESET);
1560 (void) bus_space_read_4(sc->sc_st, sc->sc_sh, PCN32_RESET);
1561
1562
1563 delay(1000);
1564
1565
1566
1567
1568
1569
1570 bus_space_write_4(sc->sc_st, sc->sc_sh, PCN32_RDP, 0);
1571 }
1572
1573
1574
1575
1576
1577
1578 int
1579 pcn_init(struct ifnet *ifp)
1580 {
1581 struct pcn_softc *sc = ifp->if_softc;
1582 struct pcn_rxsoft *rxs;
1583 uint8_t *enaddr = LLADDR(ifp->if_sadl);
1584 int i, error = 0;
1585 uint32_t reg;
1586
1587
1588 pcn_stop(ifp, 0);
1589
1590
1591 pcn_reset(sc);
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602 if (sc->sc_variant->pcv_chipid == PARTID_Am79c970)
1603 sc->sc_swstyle = LE_B20_SSTYLE_PCNETPCI2;
1604 else
1605 sc->sc_swstyle = LE_B20_SSTYLE_PCNETPCI3;
1606 pcn_bcr_write(sc, LE_BCR20, sc->sc_swstyle);
1607
1608
1609 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1610 PCN_CDTXSYNC(sc, 0, PCN_NTXDESC,
1611 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1612 sc->sc_txfree = PCN_NTXDESC;
1613 sc->sc_txnext = 0;
1614
1615
1616 for (i = 0; i < PCN_TXQUEUELEN; i++)
1617 sc->sc_txsoft[i].txs_mbuf = NULL;
1618 sc->sc_txsfree = PCN_TXQUEUELEN;
1619 sc->sc_txsnext = 0;
1620 sc->sc_txsdirty = 0;
1621
1622
1623
1624
1625
1626 for (i = 0; i < PCN_NRXDESC; i++) {
1627 rxs = &sc->sc_rxsoft[i];
1628 if (rxs->rxs_mbuf == NULL) {
1629 if ((error = pcn_add_rxbuf(sc, i)) != 0) {
1630 printf("%s: unable to allocate or map rx "
1631 "buffer %d, error = %d\n",
1632 sc->sc_dev.dv_xname, i, error);
1633
1634
1635
1636
1637 pcn_rxdrain(sc);
1638 goto out;
1639 }
1640 } else
1641 PCN_INIT_RXDESC(sc, i);
1642 }
1643 sc->sc_rxptr = 0;
1644
1645
1646 sc->sc_mode = 0;
1647 if (ifp->if_flags & IFF_PROMISC)
1648 sc->sc_mode |= LE_C15_PROM;
1649 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1650 sc->sc_mode |= LE_C15_DRCVBC;
1651
1652
1653
1654
1655
1656
1657 if (sc->sc_flags & PCN_F_HAS_MII) {
1658 pcn_bcr_write(sc, LE_BCR2,
1659 pcn_bcr_read(sc, LE_BCR2) & ~LE_B2_ASEL);
1660 sc->sc_mode |= LE_C15_PORTSEL(PORTSEL_MII);
1661
1662
1663
1664
1665
1666 pcn_bcr_write(sc, LE_BCR32,
1667 pcn_bcr_read(sc, LE_BCR32) | LE_B32_DANAS);
1668 }
1669
1670
1671
1672
1673
1674
1675 sc->sc_initblock.init_rdra = htole32(PCN_CDRXADDR(sc, 0));
1676 sc->sc_initblock.init_tdra = htole32(PCN_CDTXADDR(sc, 0));
1677 sc->sc_initblock.init_mode = htole32(sc->sc_mode |
1678 ((ffs(PCN_NTXDESC) - 1) << 28) |
1679 ((ffs(PCN_NRXDESC) - 1) << 20));
1680
1681
1682 sc->sc_initblock.init_padr[0] = htole32(enaddr[0] |
1683 (enaddr[1] << 8) | (enaddr[2] << 16) | (enaddr[3] << 24));
1684 sc->sc_initblock.init_padr[1] = htole32(enaddr[4] |
1685 (enaddr[5] << 8));
1686
1687
1688 pcn_set_filter(sc);
1689
1690
1691 pcn_csr_write(sc, LE_CSR3, LE_C3_MISSM|LE_C3_IDONM|LE_C3_DXSUFLO);
1692
1693
1694 pcn_csr_write(sc, LE_CSR4, LE_C4_DMAPLUS|LE_C4_APAD_XMT|
1695 LE_C4_MFCOM|LE_C4_RCVCCOM|LE_C4_TXSTRTM);
1696
1697
1698 sc->sc_csr5 = LE_C5_LTINTEN|LE_C5_SINTE;
1699 pcn_csr_write(sc, LE_CSR5, sc->sc_csr5);
1700
1701
1702
1703
1704
1705
1706 switch (sc->sc_variant->pcv_chipid) {
1707 case PARTID_Am79c970:
1708 case PARTID_Am79c970A:
1709
1710 break;
1711
1712 default:
1713 pcn_csr_write(sc, LE_CSR7, LE_C7_FASTSPNDE);
1714 break;
1715 }
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728 reg = pcn_bcr_read(sc, LE_BCR18);
1729 switch (sc->sc_variant->pcv_chipid) {
1730 case PARTID_Am79c970:
1731 break;
1732
1733 case PARTID_Am79c970A:
1734 reg |= LE_B18_BREADE|LE_B18_BWRITE;
1735 break;
1736
1737 default:
1738 reg |= LE_B18_BREADE|LE_B18_BWRITE|LE_B18_NOUFLO;
1739 break;
1740 }
1741 pcn_bcr_write(sc, LE_BCR18, reg);
1742
1743
1744
1745
1746 pcn_csr_write(sc, LE_CSR80, LE_C80_RCVFW(sc->sc_rcvfw) |
1747 LE_C80_XMTSP(sc->sc_xmtsp) | LE_C80_XMTFW(sc->sc_xmtfw));
1748
1749
1750
1751
1752
1753 PCN_CDINITSYNC(sc, BUS_DMASYNC_PREWRITE);
1754 pcn_csr_write(sc, LE_CSR1, PCN_CDINITADDR(sc) & 0xffff);
1755 pcn_csr_write(sc, LE_CSR2, (PCN_CDINITADDR(sc) >> 16) & 0xffff);
1756 pcn_csr_write(sc, LE_CSR0, LE_C0_INIT);
1757 delay(100);
1758 for (i = 0; i < 10000; i++) {
1759 if (pcn_csr_read(sc, LE_CSR0) & LE_C0_IDON)
1760 break;
1761 delay(10);
1762 }
1763 PCN_CDINITSYNC(sc, BUS_DMASYNC_POSTWRITE);
1764 if (i == 10000) {
1765 printf("%s: timeout processing init block\n",
1766 sc->sc_dev.dv_xname);
1767 error = EIO;
1768 goto out;
1769 }
1770
1771
1772 (void) (*sc->sc_mii.mii_media.ifm_change)(ifp);
1773
1774
1775 pcn_csr_write(sc, LE_CSR0, LE_C0_INEA|LE_C0_STRT|LE_C0_IDON);
1776
1777 if (sc->sc_flags & PCN_F_HAS_MII) {
1778
1779 timeout_add(&sc->sc_tick_timeout, hz);
1780 }
1781
1782
1783 ifp->if_flags |= IFF_RUNNING;
1784 ifp->if_flags &= ~IFF_OACTIVE;
1785
1786 out:
1787 if (error)
1788 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1789 return (error);
1790 }
1791
1792
1793
1794
1795
1796
1797 void
1798 pcn_rxdrain(struct pcn_softc *sc)
1799 {
1800 struct pcn_rxsoft *rxs;
1801 int i;
1802
1803 for (i = 0; i < PCN_NRXDESC; i++) {
1804 rxs = &sc->sc_rxsoft[i];
1805 if (rxs->rxs_mbuf != NULL) {
1806 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1807 m_freem(rxs->rxs_mbuf);
1808 rxs->rxs_mbuf = NULL;
1809 }
1810 }
1811 }
1812
1813
1814
1815
1816
1817
1818 void
1819 pcn_stop(struct ifnet *ifp, int disable)
1820 {
1821 struct pcn_softc *sc = ifp->if_softc;
1822 struct pcn_txsoft *txs;
1823 int i;
1824
1825 if (sc->sc_flags & PCN_F_HAS_MII) {
1826
1827 timeout_del(&sc->sc_tick_timeout);
1828
1829
1830 mii_down(&sc->sc_mii);
1831 }
1832
1833
1834 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1835 ifp->if_timer = 0;
1836
1837
1838 pcn_csr_write(sc, LE_CSR0, LE_C0_STOP);
1839
1840
1841 for (i = 0; i < PCN_TXQUEUELEN; i++) {
1842 txs = &sc->sc_txsoft[i];
1843 if (txs->txs_mbuf != NULL) {
1844 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1845 m_freem(txs->txs_mbuf);
1846 txs->txs_mbuf = NULL;
1847 }
1848 }
1849
1850 if (disable)
1851 pcn_rxdrain(sc);
1852 }
1853
1854
1855
1856
1857
1858
1859 int
1860 pcn_add_rxbuf(struct pcn_softc *sc, int idx)
1861 {
1862 struct pcn_rxsoft *rxs = &sc->sc_rxsoft[idx];
1863 struct mbuf *m;
1864 int error;
1865
1866 MGETHDR(m, M_DONTWAIT, MT_DATA);
1867 if (m == NULL)
1868 return (ENOBUFS);
1869
1870 MCLGET(m, M_DONTWAIT);
1871 if ((m->m_flags & M_EXT) == 0) {
1872 m_freem(m);
1873 return (ENOBUFS);
1874 }
1875
1876 if (rxs->rxs_mbuf != NULL)
1877 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1878
1879 rxs->rxs_mbuf = m;
1880
1881 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
1882 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1883 BUS_DMA_READ|BUS_DMA_NOWAIT);
1884 if (error) {
1885 printf("%s: can't load rx DMA map %d, error = %d\n",
1886 sc->sc_dev.dv_xname, idx, error);
1887 panic("pcn_add_rxbuf");
1888 }
1889
1890 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1891 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1892
1893 PCN_INIT_RXDESC(sc, idx);
1894
1895 return (0);
1896 }
1897
1898
1899
1900
1901
1902
1903 void
1904 pcn_set_filter(struct pcn_softc *sc)
1905 {
1906 struct arpcom *ac = &sc->sc_arpcom;
1907 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1908 struct ether_multi *enm;
1909 struct ether_multistep step;
1910 uint32_t crc;
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC)
1921 goto allmulti;
1922
1923 sc->sc_initblock.init_ladrf[0] =
1924 sc->sc_initblock.init_ladrf[1] =
1925 sc->sc_initblock.init_ladrf[2] =
1926 sc->sc_initblock.init_ladrf[3] = 0;
1927
1928 ETHER_FIRST_MULTI(step, ac, enm);
1929 while (enm != NULL) {
1930 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1931
1932
1933
1934
1935
1936
1937
1938
1939 goto allmulti;
1940 }
1941
1942 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1943
1944
1945 crc >>= 26;
1946
1947
1948 sc->sc_initblock.init_ladrf[crc >> 4] |=
1949 htole16(1 << (crc & 0xf));
1950
1951 ETHER_NEXT_MULTI(step, enm);
1952 }
1953
1954 ifp->if_flags &= ~IFF_ALLMULTI;
1955 return;
1956
1957 allmulti:
1958 ifp->if_flags |= IFF_ALLMULTI;
1959 sc->sc_initblock.init_ladrf[0] =
1960 sc->sc_initblock.init_ladrf[1] =
1961 sc->sc_initblock.init_ladrf[2] =
1962 sc->sc_initblock.init_ladrf[3] = 0xffff;
1963 }
1964
1965
1966
1967
1968
1969
1970 void
1971 pcn_79c970_mediainit(struct pcn_softc *sc)
1972 {
1973 ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, pcn_79c970_mediachange,
1974 pcn_79c970_mediastatus);
1975
1976 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5,
1977 PORTSEL_AUI, NULL);
1978 if (sc->sc_variant->pcv_chipid == PARTID_Am79c970A)
1979 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5|IFM_FDX,
1980 PORTSEL_AUI, NULL);
1981
1982 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T,
1983 PORTSEL_10T, NULL);
1984 if (sc->sc_variant->pcv_chipid == PARTID_Am79c970A)
1985 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T|IFM_FDX,
1986 PORTSEL_10T, NULL);
1987
1988 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO,
1989 0, NULL);
1990 if (sc->sc_variant->pcv_chipid == PARTID_Am79c970A)
1991 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO|IFM_FDX,
1992 0, NULL);
1993
1994 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
1995 }
1996
1997
1998
1999
2000
2001
2002 void
2003 pcn_79c970_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2004 {
2005 struct pcn_softc *sc = ifp->if_softc;
2006
2007
2008
2009
2010
2011
2012 ifmr->ifm_active = sc->sc_mii.mii_media.ifm_media;
2013 }
2014
2015
2016
2017
2018
2019
2020 int
2021 pcn_79c970_mediachange(struct ifnet *ifp)
2022 {
2023 struct pcn_softc *sc = ifp->if_softc;
2024 uint32_t reg;
2025
2026 if (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_media) == IFM_AUTO) {
2027
2028
2029
2030 reg = pcn_bcr_read(sc, LE_BCR2);
2031 reg |= LE_B2_ASEL;
2032 pcn_bcr_write(sc, LE_BCR2, reg);
2033 } else {
2034
2035
2036
2037 reg = pcn_bcr_read(sc, LE_BCR2);
2038 reg &= ~LE_B2_ASEL;
2039 pcn_bcr_write(sc, LE_BCR2, reg);
2040
2041 reg = pcn_csr_read(sc, LE_CSR15);
2042 reg = (reg & ~LE_C15_PORTSEL(PORTSEL_MASK)) |
2043 LE_C15_PORTSEL(sc->sc_mii.mii_media.ifm_cur->ifm_data);
2044 pcn_csr_write(sc, LE_CSR15, reg);
2045 }
2046
2047 if ((sc->sc_mii.mii_media.ifm_media & IFM_FDX) != 0) {
2048 reg = LE_B9_FDEN;
2049 if (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_media) == IFM_10_5)
2050 reg |= LE_B9_AUIFD;
2051 pcn_bcr_write(sc, LE_BCR9, reg);
2052 } else
2053 pcn_bcr_write(sc, LE_BCR9, 0);
2054
2055 return (0);
2056 }
2057
2058
2059
2060
2061
2062
2063 void
2064 pcn_79c971_mediainit(struct pcn_softc *sc)
2065 {
2066 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
2067
2068
2069 sc->sc_flags |= PCN_F_HAS_MII;
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083 sc->sc_mii.mii_ifp = ifp;
2084 sc->sc_mii.mii_readreg = pcn_mii_readreg;
2085 sc->sc_mii.mii_writereg = pcn_mii_writereg;
2086 sc->sc_mii.mii_statchg = pcn_mii_statchg;
2087 ifmedia_init(&sc->sc_mii.mii_media, 0, pcn_79c971_mediachange,
2088 pcn_79c971_mediastatus);
2089
2090 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
2091 MII_OFFSET_ANY, 0);
2092 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
2093 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
2094 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
2095 } else
2096 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
2097 }
2098
2099
2100
2101
2102
2103
2104 void
2105 pcn_79c971_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2106 {
2107 struct pcn_softc *sc = ifp->if_softc;
2108
2109 mii_pollstat(&sc->sc_mii);
2110 ifmr->ifm_status = sc->sc_mii.mii_media_status;
2111 ifmr->ifm_active = sc->sc_mii.mii_media_active;
2112 }
2113
2114
2115
2116
2117
2118
2119 int
2120 pcn_79c971_mediachange(struct ifnet *ifp)
2121 {
2122 struct pcn_softc *sc = ifp->if_softc;
2123
2124 if (ifp->if_flags & IFF_UP)
2125 mii_mediachg(&sc->sc_mii);
2126 return (0);
2127 }
2128
2129
2130
2131
2132
2133
2134 int
2135 pcn_mii_readreg(struct device *self, int phy, int reg)
2136 {
2137 struct pcn_softc *sc = (void *) self;
2138 uint32_t rv;
2139
2140 pcn_bcr_write(sc, LE_BCR33, reg | (phy << PHYAD_SHIFT));
2141 rv = pcn_bcr_read(sc, LE_BCR34) & LE_B34_MIIMD;
2142 if (rv == 0xffff)
2143 return (0);
2144
2145 return (rv);
2146 }
2147
2148
2149
2150
2151
2152
2153 void
2154 pcn_mii_writereg(struct device *self, int phy, int reg, int val)
2155 {
2156 struct pcn_softc *sc = (void *) self;
2157
2158 pcn_bcr_write(sc, LE_BCR33, reg | (phy << PHYAD_SHIFT));
2159 pcn_bcr_write(sc, LE_BCR34, val);
2160 }
2161
2162
2163
2164
2165
2166
2167 void
2168 pcn_mii_statchg(struct device *self)
2169 {
2170 struct pcn_softc *sc = (void *) self;
2171
2172 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
2173 pcn_bcr_write(sc, LE_BCR9, LE_B9_FDEN);
2174 else
2175 pcn_bcr_write(sc, LE_BCR9, 0);
2176 }