if_kse.c revision 1.13 1 /* $NetBSD: if_kse.c,v 1.13 2008/03/11 23:58:06 dyoung Exp $ */
2
3 /*
4 * Copyright (c) 2006 Tohru Nishimura
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Tohru Nishimura.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: if_kse.c,v 1.13 2008/03/11 23:58:06 dyoung Exp $");
34
35 #include "bpfilter.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/callout.h>
40 #include <sys/mbuf.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/ioctl.h>
44 #include <sys/errno.h>
45 #include <sys/device.h>
46 #include <sys/queue.h>
47
48 #include <machine/endian.h>
49 #include <sys/bus.h>
50 #include <sys/intr.h>
51
52 #include <net/if.h>
53 #include <net/if_media.h>
54 #include <net/if_dl.h>
55 #include <net/if_ether.h>
56
57 #if NBPFILTER > 0
58 #include <net/bpf.h>
59 #endif
60
61 #include <dev/pci/pcivar.h>
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcidevs.h>
64
65 #define CSR_READ_4(sc, off) \
66 bus_space_read_4(sc->sc_st, sc->sc_sh, off)
67 #define CSR_WRITE_4(sc, off, val) \
68 bus_space_write_4(sc->sc_st, sc->sc_sh, off, val)
69 #define CSR_READ_2(sc, off) \
70 bus_space_read_2(sc->sc_st, sc->sc_sh, off)
71 #define CSR_WRITE_2(sc, off, val) \
72 bus_space_write_2(sc->sc_st, sc->sc_sh, off, val)
73
74 #define MDTXC 0x000 /* DMA transmit control */
75 #define MDRXC 0x004 /* DMA receive control */
76 #define MDTSC 0x008 /* DMA transmit start */
77 #define MDRSC 0x00c /* DMA receive start */
78 #define TDLB 0x010 /* transmit descriptor list base */
79 #define RDLB 0x014 /* receive descriptor list base */
80 #define MTR0 0x020 /* multicast table 31:0 */
81 #define MTR1 0x024 /* multicast table 63:32 */
82 #define INTEN 0x028 /* interrupt enable */
83 #define INTST 0x02c /* interrupt status */
84 #define MARL 0x200 /* MAC address low */
85 #define MARM 0x202 /* MAC address middle */
86 #define MARH 0x204 /* MAC address high */
87 #define GRR 0x216 /* global reset */
88 #define CIDR 0x400 /* chip ID and enable */
89 #define CGCR 0x40a /* chip global control */
90 #define IACR 0x4a0 /* indirect access control */
91 #define IADR1 0x4a2 /* indirect access data 66:63 */
92 #define IADR2 0x4a4 /* indirect access data 47:32 */
93 #define IADR3 0x4a6 /* indirect access data 63:48 */
94 #define IADR4 0x4a8 /* indirect access data 15:0 */
95 #define IADR5 0x4aa /* indirect access data 31:16 */
96 #define P1CR4 0x512 /* port 1 control 4 */
97 #define P1SR 0x514 /* port 1 status */
98 #define P2CR4 0x532 /* port 2 control 4 */
99 #define P2SR 0x534 /* port 2 status */
100
101 #define TXC_BS_MSK 0x3f000000 /* burst size */
102 #define TXC_BS_SFT (24) /* 1,2,4,8,16,32 or 0 for unlimited */
103 #define TXC_UCG (1U<<18) /* generate UDP checksum */
104 #define TXC_TCG (1U<<17) /* generate TCP checksum */
105 #define TXC_ICG (1U<<16) /* generate IP checksum */
106 #define TXC_FCE (1U<<9) /* enable flowcontrol */
107 #define TXC_EP (1U<<2) /* enable automatic padding */
108 #define TXC_AC (1U<<1) /* add CRC to frame */
109 #define TXC_TEN (1) /* enable DMA to run */
110
111 #define RXC_BS_MSK 0x3f000000 /* burst size */
112 #define RXC_BS_SFT (24) /* 1,2,4,8,16,32 or 0 for unlimited */
113 #define RXC_IHAE (1U<<19) /* IP header alignment enable */
114 #define RXC_UCC (1U<<18) /* run UDP checksum */
115 #define RXC_TCC (1U<<17) /* run TDP checksum */
116 #define RXC_ICC (1U<<16) /* run IP checksum */
117 #define RXC_FCE (1U<<9) /* enable flowcontrol */
118 #define RXC_RB (1U<<6) /* receive broadcast frame */
119 #define RXC_RM (1U<<5) /* receive multicast frame */
120 #define RXC_RU (1U<<4) /* receive unicast frame */
121 #define RXC_RE (1U<<3) /* accept error frame */
122 #define RXC_RA (1U<<2) /* receive all frame */
123 #define RXC_MHTE (1U<<1) /* use multicast hash table */
124 #define RXC_REN (1) /* enable DMA to run */
125
126 #define INT_DMLCS (1U<<31) /* link status change */
127 #define INT_DMTS (1U<<30) /* sending desc. has posted Tx done */
128 #define INT_DMRS (1U<<29) /* frame was received */
129 #define INT_DMRBUS (1U<<27) /* Rx descriptor pool is full */
130
131 #define T0_OWN (1U<<31) /* desc is ready to Tx */
132
133 #define R0_OWN (1U<<31) /* desc is empty */
134 #define R0_FS (1U<<30) /* first segment of frame */
135 #define R0_LS (1U<<29) /* last segment of frame */
136 #define R0_IPE (1U<<28) /* IP checksum error */
137 #define R0_TCPE (1U<<27) /* TCP checksum error */
138 #define R0_UDPE (1U<<26) /* UDP checksum error */
139 #define R0_ES (1U<<25) /* error summary */
140 #define R0_MF (1U<<24) /* multicast frame */
141 #define R0_SPN 0x00300000 /* 21:20 switch port 1/2 */
142 #define R0_ALIGN 0x00300000 /* 21:20 (KSZ8692P) Rx align amount */
143 #define R0_RE (1U<<19) /* MII reported error */
144 #define R0_TL (1U<<18) /* frame too long, beyond 1518 */
145 #define R0_RF (1U<<17) /* damaged runt frame */
146 #define R0_CE (1U<<16) /* CRC error */
147 #define R0_FT (1U<<15) /* frame type */
148 #define R0_FL_MASK 0x7ff /* frame length 10:0 */
149
150 #define T1_IC (1U<<31) /* post interrupt on complete */
151 #define T1_FS (1U<<30) /* first segment of frame */
152 #define T1_LS (1U<<29) /* last segment of frame */
153 #define T1_IPCKG (1U<<28) /* generate IP checksum */
154 #define T1_TCPCKG (1U<<27) /* generate TCP checksum */
155 #define T1_UDPCKG (1U<<26) /* generate UDP checksum */
156 #define T1_TER (1U<<25) /* end of ring */
157 #define T1_SPN 0x00300000 /* 21:20 switch port 1/2 */
158 #define T1_TBS_MASK 0x7ff /* segment size 10:0 */
159
160 #define R1_RER (1U<<25) /* end of ring */
161 #define R1_RBS_MASK 0x7fc /* segment size 10:0 */
162
163 #define KSE_NTXSEGS 16
164 #define KSE_TXQUEUELEN 64
165 #define KSE_TXQUEUELEN_MASK (KSE_TXQUEUELEN - 1)
166 #define KSE_TXQUEUE_GC (KSE_TXQUEUELEN / 4)
167 #define KSE_NTXDESC 256
168 #define KSE_NTXDESC_MASK (KSE_NTXDESC - 1)
169 #define KSE_NEXTTX(x) (((x) + 1) & KSE_NTXDESC_MASK)
170 #define KSE_NEXTTXS(x) (((x) + 1) & KSE_TXQUEUELEN_MASK)
171
172 #define KSE_NRXDESC 64
173 #define KSE_NRXDESC_MASK (KSE_NRXDESC - 1)
174 #define KSE_NEXTRX(x) (((x) + 1) & KSE_NRXDESC_MASK)
175
176 struct tdes {
177 uint32_t t0, t1, t2, t3;
178 };
179
180 struct rdes {
181 uint32_t r0, r1, r2, r3;
182 };
183
184 struct kse_control_data {
185 struct tdes kcd_txdescs[KSE_NTXDESC];
186 struct rdes kcd_rxdescs[KSE_NRXDESC];
187 };
188 #define KSE_CDOFF(x) offsetof(struct kse_control_data, x)
189 #define KSE_CDTXOFF(x) KSE_CDOFF(kcd_txdescs[(x)])
190 #define KSE_CDRXOFF(x) KSE_CDOFF(kcd_rxdescs[(x)])
191
192 struct kse_txsoft {
193 struct mbuf *txs_mbuf; /* head of our mbuf chain */
194 bus_dmamap_t txs_dmamap; /* our DMA map */
195 int txs_firstdesc; /* first descriptor in packet */
196 int txs_lastdesc; /* last descriptor in packet */
197 int txs_ndesc; /* # of descriptors used */
198 };
199
200 struct kse_rxsoft {
201 struct mbuf *rxs_mbuf; /* head of our mbuf chain */
202 bus_dmamap_t rxs_dmamap; /* our DMA map */
203 };
204
205 struct kse_softc {
206 struct device sc_dev; /* generic device information */
207 bus_space_tag_t sc_st; /* bus space tag */
208 bus_space_handle_t sc_sh; /* bus space handle */
209 bus_dma_tag_t sc_dmat; /* bus DMA tag */
210 struct ethercom sc_ethercom; /* Ethernet common data */
211 void *sc_ih; /* interrupt cookie */
212
213 struct ifmedia sc_media; /* ifmedia information */
214 int sc_media_status; /* PHY */
215 int sc_media_active; /* PHY */
216 callout_t sc_callout; /* MII tick callout */
217 callout_t sc_stat_ch; /* statistics counter callout */
218
219 bus_dmamap_t sc_cddmamap; /* control data DMA map */
220 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
221
222 struct kse_control_data *sc_control_data;
223 #define sc_txdescs sc_control_data->kcd_txdescs
224 #define sc_rxdescs sc_control_data->kcd_rxdescs
225
226 struct kse_txsoft sc_txsoft[KSE_TXQUEUELEN];
227 struct kse_rxsoft sc_rxsoft[KSE_NRXDESC];
228 int sc_txfree; /* number of free Tx descriptors */
229 int sc_txnext; /* next ready Tx descriptor */
230 int sc_txsfree; /* number of free Tx jobs */
231 int sc_txsnext; /* next ready Tx job */
232 int sc_txsdirty; /* dirty Tx jobs */
233 int sc_rxptr; /* next ready Rx descriptor/descsoft */
234
235 uint32_t sc_txc, sc_rxc;
236 uint32_t sc_t1csum;
237 int sc_mcsum;
238 uint32_t sc_inten;
239
240 uint32_t sc_chip;
241 uint8_t sc_altmac[16][ETHER_ADDR_LEN];
242 uint16_t sc_vlan[16];
243
244 #ifdef KSE_EVENT_COUNTERS
245 struct ksext {
246 char evcntname[3][8];
247 struct evcnt pev[3][34];
248 } sc_ext; /* switch statistics */
249 #endif
250 };
251
252 #define KSE_CDTXADDR(sc, x) ((sc)->sc_cddma + KSE_CDTXOFF((x)))
253 #define KSE_CDRXADDR(sc, x) ((sc)->sc_cddma + KSE_CDRXOFF((x)))
254
255 #define KSE_CDTXSYNC(sc, x, n, ops) \
256 do { \
257 int __x, __n; \
258 \
259 __x = (x); \
260 __n = (n); \
261 \
262 /* If it will wrap around, sync to the end of the ring. */ \
263 if ((__x + __n) > KSE_NTXDESC) { \
264 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
265 KSE_CDTXOFF(__x), sizeof(struct tdes) * \
266 (KSE_NTXDESC - __x), (ops)); \
267 __n -= (KSE_NTXDESC - __x); \
268 __x = 0; \
269 } \
270 \
271 /* Now sync whatever is left. */ \
272 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
273 KSE_CDTXOFF(__x), sizeof(struct tdes) * __n, (ops)); \
274 } while (/*CONSTCOND*/0)
275
276 #define KSE_CDRXSYNC(sc, x, ops) \
277 do { \
278 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
279 KSE_CDRXOFF((x)), sizeof(struct rdes), (ops)); \
280 } while (/*CONSTCOND*/0)
281
282 #define KSE_INIT_RXDESC(sc, x) \
283 do { \
284 struct kse_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)]; \
285 struct rdes *__rxd = &(sc)->sc_rxdescs[(x)]; \
286 struct mbuf *__m = __rxs->rxs_mbuf; \
287 \
288 __m->m_data = __m->m_ext.ext_buf; \
289 __rxd->r2 = __rxs->rxs_dmamap->dm_segs[0].ds_addr; \
290 __rxd->r1 = R1_RBS_MASK /* __m->m_ext.ext_size */; \
291 __rxd->r0 = R0_OWN; \
292 KSE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
293 } while (/*CONSTCOND*/0)
294
295 u_int kse_burstsize = 8; /* DMA burst length tuning knob */
296
297 #ifdef KSEDIAGNOSTIC
298 u_int kse_monitor_rxintr; /* fragmented UDP csum HW bug hook */
299 #endif
300
301 static int kse_match(struct device *, struct cfdata *, void *);
302 static void kse_attach(struct device *, struct device *, void *);
303
304 CFATTACH_DECL(kse, sizeof(struct kse_softc),
305 kse_match, kse_attach, NULL, NULL);
306
307 static int kse_ioctl(struct ifnet *, u_long, void *);
308 static void kse_start(struct ifnet *);
309 static void kse_watchdog(struct ifnet *);
310 static int kse_init(struct ifnet *);
311 static void kse_stop(struct ifnet *, int);
312 static void kse_reset(struct kse_softc *);
313 static void kse_set_filter(struct kse_softc *);
314 static int add_rxbuf(struct kse_softc *, int);
315 static void rxdrain(struct kse_softc *);
316 static int kse_intr(void *);
317 static void rxintr(struct kse_softc *);
318 static void txreap(struct kse_softc *);
319 static void lnkchg(struct kse_softc *);
320 static int ifmedia_upd(struct ifnet *);
321 static void ifmedia_sts(struct ifnet *, struct ifmediareq *);
322 static void phy_tick(void *);
323 static int ifmedia2_upd(struct ifnet *);
324 static void ifmedia2_sts(struct ifnet *, struct ifmediareq *);
325 #ifdef KSE_EVENT_COUNTERS
326 static void stat_tick(void *);
327 static void zerostats(struct kse_softc *);
328 #endif
329
330 static int
331 kse_match(struct device *parent, struct cfdata *match, void *aux)
332 {
333 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
334
335 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_MICREL &&
336 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_MICREL_KSZ8842 ||
337 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_MICREL_KSZ8841) &&
338 PCI_CLASS(pa->pa_class) == PCI_CLASS_NETWORK)
339 return 1;
340
341 return 0;
342 }
343
344 static void
345 kse_attach(struct device *parent, struct device *self, void *aux)
346 {
347 struct kse_softc *sc = (struct kse_softc *)self;
348 struct pci_attach_args *pa = aux;
349 pci_chipset_tag_t pc = pa->pa_pc;
350 pci_intr_handle_t ih;
351 const char *intrstr;
352 struct ifnet *ifp;
353 struct ifmedia *ifm;
354 uint8_t enaddr[ETHER_ADDR_LEN];
355 bus_dma_segment_t seg;
356 int i, p, error, nseg;
357 pcireg_t pmode;
358 int pmreg;
359
360 if (pci_mapreg_map(pa, 0x10,
361 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
362 0, &sc->sc_st, &sc->sc_sh, NULL, NULL) != 0) {
363 printf(": unable to map device registers\n");
364 return;
365 }
366
367 sc->sc_dmat = pa->pa_dmat;
368
369 /* Make sure bus mastering is enabled. */
370 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
371 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
372 PCI_COMMAND_MASTER_ENABLE);
373
374 /* Get it out of power save mode, if needed. */
375 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
376 pmode = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR) &
377 PCI_PMCSR_STATE_MASK;
378 if (pmode == PCI_PMCSR_STATE_D3) {
379 /*
380 * The card has lost all configuration data in
381 * this state, so punt.
382 */
383 printf("%s: unable to wake from power state D3\n",
384 sc->sc_dev.dv_xname);
385 return;
386 }
387 if (pmode != PCI_PMCSR_STATE_D0) {
388 printf("%s: waking up from power date D%d\n",
389 sc->sc_dev.dv_xname, pmode);
390 pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
391 PCI_PMCSR_STATE_D0);
392 }
393 }
394
395 sc->sc_chip = PCI_PRODUCT(pa->pa_id);
396 printf(": Micrel KSZ%04x Ethernet (rev. 0x%02x)\n",
397 sc->sc_chip, PCI_REVISION(pa->pa_class));
398
399 /*
400 * Read the Ethernet address from the EEPROM.
401 */
402 i = CSR_READ_2(sc, MARL);
403 enaddr[5] = i; enaddr[4] = i >> 8;
404 i = CSR_READ_2(sc, MARM);
405 enaddr[3] = i; enaddr[2] = i >> 8;
406 i = CSR_READ_2(sc, MARH);
407 enaddr[1] = i; enaddr[0] = i >> 8;
408 printf("%s: Ethernet address: %s\n",
409 sc->sc_dev.dv_xname, ether_sprintf(enaddr));
410
411 /*
412 * Enable chip function.
413 */
414 CSR_WRITE_2(sc, CIDR, 1);
415
416 /*
417 * Map and establish our interrupt.
418 */
419 if (pci_intr_map(pa, &ih)) {
420 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
421 return;
422 }
423 intrstr = pci_intr_string(pc, ih);
424 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, kse_intr, sc);
425 if (sc->sc_ih == NULL) {
426 printf("%s: unable to establish interrupt",
427 sc->sc_dev.dv_xname);
428 if (intrstr != NULL)
429 printf(" at %s", intrstr);
430 printf("\n");
431 return;
432 }
433 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
434
435 /*
436 * Allocate the control data structures, and create and load the
437 * DMA map for it.
438 */
439 error = bus_dmamem_alloc(sc->sc_dmat,
440 sizeof(struct kse_control_data), PAGE_SIZE, 0, &seg, 1, &nseg, 0);
441 if (error != 0) {
442 printf("%s: unable to allocate control data, error = %d\n",
443 sc->sc_dev.dv_xname, error);
444 goto fail_0;
445 }
446 error = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
447 sizeof(struct kse_control_data), (void **)&sc->sc_control_data,
448 BUS_DMA_COHERENT);
449 if (error != 0) {
450 printf("%s: unable to map control data, error = %d\n",
451 sc->sc_dev.dv_xname, error);
452 goto fail_1;
453 }
454 error = bus_dmamap_create(sc->sc_dmat,
455 sizeof(struct kse_control_data), 1,
456 sizeof(struct kse_control_data), 0, 0, &sc->sc_cddmamap);
457 if (error != 0) {
458 printf("%s: unable to create control data DMA map, "
459 "error = %d\n", sc->sc_dev.dv_xname, error);
460 goto fail_2;
461 }
462 error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
463 sc->sc_control_data, sizeof(struct kse_control_data), NULL, 0);
464 if (error != 0) {
465 printf("%s: unable to load control data DMA map, error = %d\n",
466 sc->sc_dev.dv_xname, error);
467 goto fail_3;
468 }
469 for (i = 0; i < KSE_TXQUEUELEN; i++) {
470 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
471 KSE_NTXSEGS, MCLBYTES, 0, 0,
472 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
473 printf("%s: unable to create tx DMA map %d, "
474 "error = %d\n", sc->sc_dev.dv_xname, i, error);
475 goto fail_4;
476 }
477 }
478 for (i = 0; i < KSE_NRXDESC; i++) {
479 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
480 1, MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
481 printf("%s: unable to create rx DMA map %d, "
482 "error = %d\n", sc->sc_dev.dv_xname, i, error);
483 goto fail_5;
484 }
485 sc->sc_rxsoft[i].rxs_mbuf = NULL;
486 }
487
488 callout_init(&sc->sc_callout, 0);
489 callout_init(&sc->sc_stat_ch, 0);
490
491 ifm = &sc->sc_media;
492 if (sc->sc_chip == 0x8841) {
493 ifmedia_init(ifm, 0, ifmedia_upd, ifmedia_sts);
494 ifmedia_add(ifm, IFM_ETHER|IFM_10_T, 0, NULL);
495 ifmedia_add(ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
496 ifmedia_add(ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
497 ifmedia_add(ifm, IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
498 ifmedia_add(ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
499 ifmedia_set(ifm, IFM_ETHER|IFM_AUTO);
500 }
501 else {
502 ifmedia_init(ifm, 0, ifmedia2_upd, ifmedia2_sts);
503 ifmedia_add(ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
504 ifmedia_set(ifm, IFM_ETHER|IFM_AUTO);
505 }
506
507 printf("%s: 10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto\n",
508 sc->sc_dev.dv_xname);
509
510 ifp = &sc->sc_ethercom.ec_if;
511 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
512 ifp->if_softc = sc;
513 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
514 ifp->if_ioctl = kse_ioctl;
515 ifp->if_start = kse_start;
516 ifp->if_watchdog = kse_watchdog;
517 ifp->if_init = kse_init;
518 ifp->if_stop = kse_stop;
519 IFQ_SET_READY(&ifp->if_snd);
520
521 /*
522 * KSZ8842 can handle 802.1Q VLAN-sized frames,
523 * can do IPv4, TCPv4, and UDPv4 checksums in hardware.
524 */
525 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
526 ifp->if_capabilities |=
527 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
528 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
529 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
530
531 if_attach(ifp);
532 ether_ifattach(ifp, enaddr);
533
534 p = (sc->sc_chip == 0x8842) ? 3 : 1;
535 #ifdef KSE_EVENT_COUNTERS
536 for (i = 0; i < p; i++) {
537 struct ksext *ee = &sc->sc_ext;
538 sprintf(ee->evcntname[i], "%s.%d", sc->sc_dev.dv_xname, i+1);
539 evcnt_attach_dynamic(&ee->pev[i][0], EVCNT_TYPE_MISC,
540 NULL, ee->evcntname[i], "RxLoPriotyByte");
541 evcnt_attach_dynamic(&ee->pev[i][1], EVCNT_TYPE_MISC,
542 NULL, ee->evcntname[i], "RxHiPriotyByte");
543 evcnt_attach_dynamic(&ee->pev[i][2], EVCNT_TYPE_MISC,
544 NULL, ee->evcntname[i], "RxUndersizePkt");
545 evcnt_attach_dynamic(&ee->pev[i][3], EVCNT_TYPE_MISC,
546 NULL, ee->evcntname[i], "RxFragments");
547 evcnt_attach_dynamic(&ee->pev[i][4], EVCNT_TYPE_MISC,
548 NULL, ee->evcntname[i], "RxOversize");
549 evcnt_attach_dynamic(&ee->pev[i][5], EVCNT_TYPE_MISC,
550 NULL, ee->evcntname[i], "RxJabbers");
551 evcnt_attach_dynamic(&ee->pev[i][6], EVCNT_TYPE_MISC,
552 NULL, ee->evcntname[i], "RxSymbolError");
553 evcnt_attach_dynamic(&ee->pev[i][7], EVCNT_TYPE_MISC,
554 NULL, ee->evcntname[i], "RxCRCError");
555 evcnt_attach_dynamic(&ee->pev[i][8], EVCNT_TYPE_MISC,
556 NULL, ee->evcntname[i], "RxAlignmentError");
557 evcnt_attach_dynamic(&ee->pev[i][9], EVCNT_TYPE_MISC,
558 NULL, ee->evcntname[i], "RxControl8808Pkts");
559 evcnt_attach_dynamic(&ee->pev[i][10], EVCNT_TYPE_MISC,
560 NULL, ee->evcntname[i], "RxPausePkts");
561 evcnt_attach_dynamic(&ee->pev[i][11], EVCNT_TYPE_MISC,
562 NULL, ee->evcntname[i], "RxBroadcast");
563 evcnt_attach_dynamic(&ee->pev[i][12], EVCNT_TYPE_MISC,
564 NULL, ee->evcntname[i], "RxMulticast");
565 evcnt_attach_dynamic(&ee->pev[i][13], EVCNT_TYPE_MISC,
566 NULL, ee->evcntname[i], "RxUnicast");
567 evcnt_attach_dynamic(&ee->pev[i][14], EVCNT_TYPE_MISC,
568 NULL, ee->evcntname[i], "Rx64Octets");
569 evcnt_attach_dynamic(&ee->pev[i][15], EVCNT_TYPE_MISC,
570 NULL, ee->evcntname[i], "Rx65To127Octets");
571 evcnt_attach_dynamic(&ee->pev[i][16], EVCNT_TYPE_MISC,
572 NULL, ee->evcntname[i], "Rx128To255Octets");
573 evcnt_attach_dynamic(&ee->pev[i][17], EVCNT_TYPE_MISC,
574 NULL, ee->evcntname[i], "Rx255To511Octets");
575 evcnt_attach_dynamic(&ee->pev[i][18], EVCNT_TYPE_MISC,
576 NULL, ee->evcntname[i], "Rx512To1023Octets");
577 evcnt_attach_dynamic(&ee->pev[i][19], EVCNT_TYPE_MISC,
578 NULL, ee->evcntname[i], "Rx1024To1522Octets");
579 evcnt_attach_dynamic(&ee->pev[i][20], EVCNT_TYPE_MISC,
580 NULL, ee->evcntname[i], "TxLoPriotyByte");
581 evcnt_attach_dynamic(&ee->pev[i][21], EVCNT_TYPE_MISC,
582 NULL, ee->evcntname[i], "TxHiPriotyByte");
583 evcnt_attach_dynamic(&ee->pev[i][22], EVCNT_TYPE_MISC,
584 NULL, ee->evcntname[i], "TxLateCollision");
585 evcnt_attach_dynamic(&ee->pev[i][23], EVCNT_TYPE_MISC,
586 NULL, ee->evcntname[i], "TxPausePkts");
587 evcnt_attach_dynamic(&ee->pev[i][24], EVCNT_TYPE_MISC,
588 NULL, ee->evcntname[i], "TxBroadcastPkts");
589 evcnt_attach_dynamic(&ee->pev[i][25], EVCNT_TYPE_MISC,
590 NULL, ee->evcntname[i], "TxMulticastPkts");
591 evcnt_attach_dynamic(&ee->pev[i][26], EVCNT_TYPE_MISC,
592 NULL, ee->evcntname[i], "TxUnicastPkts");
593 evcnt_attach_dynamic(&ee->pev[i][27], EVCNT_TYPE_MISC,
594 NULL, ee->evcntname[i], "TxDeferred");
595 evcnt_attach_dynamic(&ee->pev[i][28], EVCNT_TYPE_MISC,
596 NULL, ee->evcntname[i], "TxTotalCollision");
597 evcnt_attach_dynamic(&ee->pev[i][29], EVCNT_TYPE_MISC,
598 NULL, ee->evcntname[i], "TxExcessiveCollision");
599 evcnt_attach_dynamic(&ee->pev[i][30], EVCNT_TYPE_MISC,
600 NULL, ee->evcntname[i], "TxSingleCollision");
601 evcnt_attach_dynamic(&ee->pev[i][31], EVCNT_TYPE_MISC,
602 NULL, ee->evcntname[i], "TxMultipleCollision");
603 evcnt_attach_dynamic(&ee->pev[i][32], EVCNT_TYPE_MISC,
604 NULL, ee->evcntname[i], "TxDropPkts");
605 evcnt_attach_dynamic(&ee->pev[i][33], EVCNT_TYPE_MISC,
606 NULL, ee->evcntname[i], "RxDropPkts");
607 }
608 #endif
609 return;
610
611 fail_5:
612 for (i = 0; i < KSE_NRXDESC; i++) {
613 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
614 bus_dmamap_destroy(sc->sc_dmat,
615 sc->sc_rxsoft[i].rxs_dmamap);
616 }
617 fail_4:
618 for (i = 0; i < KSE_TXQUEUELEN; i++) {
619 if (sc->sc_txsoft[i].txs_dmamap != NULL)
620 bus_dmamap_destroy(sc->sc_dmat,
621 sc->sc_txsoft[i].txs_dmamap);
622 }
623 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
624 fail_3:
625 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
626 fail_2:
627 bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
628 sizeof(struct kse_control_data));
629 fail_1:
630 bus_dmamem_free(sc->sc_dmat, &seg, nseg);
631 fail_0:
632 return;
633 }
634
635 static int
636 kse_ioctl(struct ifnet *ifp, u_long cmd, void *data)
637 {
638 struct kse_softc *sc = ifp->if_softc;
639 struct ifreq *ifr = (struct ifreq *)data;
640 int s, error;
641
642 s = splnet();
643
644 switch (cmd) {
645 case SIOCSIFMEDIA:
646 case SIOCGIFMEDIA:
647 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
648 break;
649
650 default:
651 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
652 break;
653
654 error = 0;
655
656 if (cmd == SIOCSIFCAP)
657 error = (*ifp->if_init)(ifp);
658 if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
659 ;
660 else if (ifp->if_flags & IFF_RUNNING) {
661 /*
662 * Multicast list has changed; set the hardware filter
663 * accordingly.
664 */
665 kse_set_filter(sc);
666 }
667 break;
668 }
669
670 kse_start(ifp);
671
672 splx(s);
673 return error;
674 }
675
676 static int
677 kse_init(struct ifnet *ifp)
678 {
679 struct kse_softc *sc = ifp->if_softc;
680 uint32_t paddr;
681 int i, error = 0;
682
683 /* cancel pending I/O */
684 kse_stop(ifp, 0);
685
686 /* reset all registers but PCI configuration */
687 kse_reset(sc);
688
689 /* craft Tx descriptor ring */
690 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
691 for (i = 0, paddr = KSE_CDTXADDR(sc, 1); i < KSE_NTXDESC - 1; i++) {
692 sc->sc_txdescs[i].t3 = paddr;
693 paddr += sizeof(struct tdes);
694 }
695 sc->sc_txdescs[KSE_NTXDESC - 1].t3 = KSE_CDTXADDR(sc, 0);
696 KSE_CDTXSYNC(sc, 0, KSE_NTXDESC,
697 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
698 sc->sc_txfree = KSE_NTXDESC;
699 sc->sc_txnext = 0;
700
701 for (i = 0; i < KSE_TXQUEUELEN; i++)
702 sc->sc_txsoft[i].txs_mbuf = NULL;
703 sc->sc_txsfree = KSE_TXQUEUELEN;
704 sc->sc_txsnext = 0;
705 sc->sc_txsdirty = 0;
706
707 /* craft Rx descriptor ring */
708 memset(sc->sc_rxdescs, 0, sizeof(sc->sc_rxdescs));
709 for (i = 0, paddr = KSE_CDRXADDR(sc, 1); i < KSE_NRXDESC - 1; i++) {
710 sc->sc_rxdescs[i].r3 = paddr;
711 paddr += sizeof(struct rdes);
712 }
713 sc->sc_rxdescs[KSE_NRXDESC - 1].r3 = KSE_CDRXADDR(sc, 0);
714 for (i = 0; i < KSE_NRXDESC; i++) {
715 if (sc->sc_rxsoft[i].rxs_mbuf == NULL) {
716 if ((error = add_rxbuf(sc, i)) != 0) {
717 printf("%s: unable to allocate or map rx "
718 "buffer %d, error = %d\n",
719 sc->sc_dev.dv_xname, i, error);
720 rxdrain(sc);
721 goto out;
722 }
723 }
724 else
725 KSE_INIT_RXDESC(sc, i);
726 }
727 sc->sc_rxptr = 0;
728
729 /* hand Tx/Rx rings to HW */
730 CSR_WRITE_4(sc, TDLB, KSE_CDTXADDR(sc, 0));
731 CSR_WRITE_4(sc, RDLB, KSE_CDRXADDR(sc, 0));
732
733 sc->sc_txc = TXC_TEN | TXC_EP | TXC_AC | TXC_FCE;
734 sc->sc_rxc = RXC_REN | RXC_RU | RXC_FCE;
735 if (ifp->if_flags & IFF_PROMISC)
736 sc->sc_rxc |= RXC_RA;
737 if (ifp->if_flags & IFF_BROADCAST)
738 sc->sc_rxc |= RXC_RB;
739 sc->sc_t1csum = sc->sc_mcsum = 0;
740 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
741 sc->sc_rxc |= RXC_ICC;
742 sc->sc_mcsum |= M_CSUM_IPv4;
743 }
744 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
745 sc->sc_txc |= TXC_ICG;
746 sc->sc_t1csum |= T1_IPCKG;
747 }
748 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
749 sc->sc_rxc |= RXC_TCC;
750 sc->sc_mcsum |= M_CSUM_TCPv4;
751 }
752 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
753 sc->sc_txc |= TXC_TCG;
754 sc->sc_t1csum |= T1_TCPCKG;
755 }
756 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
757 sc->sc_rxc |= RXC_UCC;
758 sc->sc_mcsum |= M_CSUM_UDPv4;
759 }
760 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
761 sc->sc_txc |= TXC_UCG;
762 sc->sc_t1csum |= T1_UDPCKG;
763 }
764 sc->sc_txc |= (kse_burstsize << TXC_BS_SFT);
765 sc->sc_rxc |= (kse_burstsize << RXC_BS_SFT);
766
767 /* build multicast hash filter if necessary */
768 kse_set_filter(sc);
769
770 /* set current media */
771 (void)ifmedia_upd(ifp);
772
773 /* enable transmitter and receiver */
774 CSR_WRITE_4(sc, MDTXC, sc->sc_txc);
775 CSR_WRITE_4(sc, MDRXC, sc->sc_rxc);
776 CSR_WRITE_4(sc, MDRSC, 1);
777
778 /* enable interrupts */
779 sc->sc_inten = INT_DMTS|INT_DMRS|INT_DMRBUS;
780 if (sc->sc_chip == 0x8841)
781 sc->sc_inten |= INT_DMLCS;
782 CSR_WRITE_4(sc, INTST, ~0);
783 CSR_WRITE_4(sc, INTEN, sc->sc_inten);
784
785 ifp->if_flags |= IFF_RUNNING;
786 ifp->if_flags &= ~IFF_OACTIVE;
787
788 if (sc->sc_chip == 0x8841) {
789 /* start one second timer */
790 callout_reset(&sc->sc_callout, hz, phy_tick, sc);
791 }
792 #ifdef KSE_EVENT_COUNTERS
793 /* start statistics gather 1 minute timer */
794 zerostats(sc);
795 callout_reset(&sc->sc_stat_ch, hz * 60, stat_tick, sc);
796 #endif
797
798 out:
799 if (error) {
800 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
801 ifp->if_timer = 0;
802 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
803 }
804 return error;
805 }
806
807 static void
808 kse_stop(struct ifnet *ifp, int disable)
809 {
810 struct kse_softc *sc = ifp->if_softc;
811 struct kse_txsoft *txs;
812 int i;
813
814 if (sc->sc_chip == 0x8841)
815 callout_stop(&sc->sc_callout);
816 callout_stop(&sc->sc_stat_ch);
817
818 sc->sc_txc &= ~TXC_TEN;
819 sc->sc_rxc &= ~RXC_REN;
820 CSR_WRITE_4(sc, MDTXC, sc->sc_txc);
821 CSR_WRITE_4(sc, MDRXC, sc->sc_rxc);
822
823 for (i = 0; i < KSE_TXQUEUELEN; i++) {
824 txs = &sc->sc_txsoft[i];
825 if (txs->txs_mbuf != NULL) {
826 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
827 m_freem(txs->txs_mbuf);
828 txs->txs_mbuf = NULL;
829 }
830 }
831
832 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
833 ifp->if_timer = 0;
834
835 if (disable)
836 rxdrain(sc);
837 }
838
839 static void
840 kse_reset(struct kse_softc *sc)
841 {
842
843 CSR_WRITE_2(sc, GRR, 1);
844 delay(1000); /* PDF does not mention the delay amount */
845 CSR_WRITE_2(sc, GRR, 0);
846
847 CSR_WRITE_2(sc, CIDR, 1);
848 }
849
850 static void
851 kse_watchdog(struct ifnet *ifp)
852 {
853 struct kse_softc *sc = ifp->if_softc;
854
855 /*
856 * Since we're not interrupting every packet, sweep
857 * up before we report an error.
858 */
859 txreap(sc);
860
861 if (sc->sc_txfree != KSE_NTXDESC) {
862 printf("%s: device timeout (txfree %d txsfree %d txnext %d)\n",
863 sc->sc_dev.dv_xname, sc->sc_txfree, sc->sc_txsfree,
864 sc->sc_txnext);
865 ifp->if_oerrors++;
866
867 /* Reset the interface. */
868 kse_init(ifp);
869 }
870 else if (ifp->if_flags & IFF_DEBUG)
871 printf("%s: recovered from device timeout\n",
872 sc->sc_dev.dv_xname);
873
874 /* Try to get more packets going. */
875 kse_start(ifp);
876 }
877
878 static void
879 kse_start(struct ifnet *ifp)
880 {
881 struct kse_softc *sc = ifp->if_softc;
882 struct mbuf *m0, *m;
883 struct kse_txsoft *txs;
884 bus_dmamap_t dmamap;
885 int error, nexttx, lasttx, ofree, seg;
886 uint32_t tdes0;
887
888 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
889 return;
890
891 /*
892 * Remember the previous number of free descriptors.
893 */
894 ofree = sc->sc_txfree;
895
896 /*
897 * Loop through the send queue, setting up transmit descriptors
898 * until we drain the queue, or use up all available transmit
899 * descriptors.
900 */
901 for (;;) {
902 IFQ_POLL(&ifp->if_snd, m0);
903 if (m0 == NULL)
904 break;
905
906 if (sc->sc_txsfree < KSE_TXQUEUE_GC) {
907 txreap(sc);
908 if (sc->sc_txsfree == 0)
909 break;
910 }
911 txs = &sc->sc_txsoft[sc->sc_txsnext];
912 dmamap = txs->txs_dmamap;
913
914 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
915 BUS_DMA_WRITE|BUS_DMA_NOWAIT);
916 if (error) {
917 if (error == EFBIG) {
918 printf("%s: Tx packet consumes too many "
919 "DMA segments, dropping...\n",
920 sc->sc_dev.dv_xname);
921 IFQ_DEQUEUE(&ifp->if_snd, m0);
922 m_freem(m0);
923 continue;
924 }
925 /* Short on resources, just stop for now. */
926 break;
927 }
928
929 if (dmamap->dm_nsegs > sc->sc_txfree) {
930 /*
931 * Not enough free descriptors to transmit this
932 * packet. We haven't committed anything yet,
933 * so just unload the DMA map, put the packet
934 * back on the queue, and punt. Notify the upper
935 * layer that there are not more slots left.
936 */
937 ifp->if_flags |= IFF_OACTIVE;
938 bus_dmamap_unload(sc->sc_dmat, dmamap);
939 break;
940 }
941
942 IFQ_DEQUEUE(&ifp->if_snd, m0);
943
944 /*
945 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
946 */
947
948 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
949 BUS_DMASYNC_PREWRITE);
950
951 lasttx = -1; tdes0 = 0;
952 for (nexttx = sc->sc_txnext, seg = 0;
953 seg < dmamap->dm_nsegs;
954 seg++, nexttx = KSE_NEXTTX(nexttx)) {
955 struct tdes *tdes = &sc->sc_txdescs[nexttx];
956 /*
957 * If this is the first descriptor we're
958 * enqueueing, don't set the OWN bit just
959 * yet. That could cause a race condition.
960 * We'll do it below.
961 */
962 tdes->t2 = dmamap->dm_segs[seg].ds_addr;
963 tdes->t1 = sc->sc_t1csum
964 | (dmamap->dm_segs[seg].ds_len & T1_TBS_MASK);
965 tdes->t0 = tdes0;
966 tdes0 |= T0_OWN;
967 lasttx = nexttx;
968 }
969
970 /*
971 * Outgoing NFS mbuf must be unloaded when Tx completed.
972 * Without T1_IC NFS mbuf is left unack'ed for excessive
973 * time and NFS stops to proceed until kse_watchdog()
974 * calls txreap() to reclaim the unack'ed mbuf.
975 * It's painful to traverse every mbuf chain to determine
976 * whether someone is waiting for Tx completion.
977 */
978 m = m0;
979 do {
980 if ((m->m_flags & M_EXT) && m->m_ext.ext_free) {
981 sc->sc_txdescs[lasttx].t1 |= T1_IC;
982 break;
983 }
984 } while ((m = m->m_next) != NULL);
985
986 /* write last T0_OWN bit of the 1st segment */
987 sc->sc_txdescs[lasttx].t1 |= T1_LS;
988 sc->sc_txdescs[sc->sc_txnext].t1 |= T1_FS;
989 sc->sc_txdescs[sc->sc_txnext].t0 = T0_OWN;
990 KSE_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
991 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
992
993 /* tell DMA start transmit */
994 CSR_WRITE_4(sc, MDTSC, 1);
995
996 txs->txs_mbuf = m0;
997 txs->txs_firstdesc = sc->sc_txnext;
998 txs->txs_lastdesc = lasttx;
999 txs->txs_ndesc = dmamap->dm_nsegs;
1000
1001 sc->sc_txfree -= txs->txs_ndesc;
1002 sc->sc_txnext = nexttx;
1003 sc->sc_txsfree--;
1004 sc->sc_txsnext = KSE_NEXTTXS(sc->sc_txsnext);
1005 #if NBPFILTER > 0
1006 /*
1007 * Pass the packet to any BPF listeners.
1008 */
1009 if (ifp->if_bpf)
1010 bpf_mtap(ifp->if_bpf, m0);
1011 #endif /* NBPFILTER > 0 */
1012 }
1013
1014 if (sc->sc_txsfree == 0 || sc->sc_txfree == 0) {
1015 /* No more slots left; notify upper layer. */
1016 ifp->if_flags |= IFF_OACTIVE;
1017 }
1018 if (sc->sc_txfree != ofree) {
1019 /* Set a watchdog timer in case the chip flakes out. */
1020 ifp->if_timer = 5;
1021 }
1022 }
1023
1024 static void
1025 kse_set_filter(struct kse_softc *sc)
1026 {
1027 struct ether_multistep step;
1028 struct ether_multi *enm;
1029 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1030 uint32_t h, hashes[2];
1031
1032 sc->sc_rxc &= ~(RXC_MHTE | RXC_RM);
1033 ifp->if_flags &= ~IFF_ALLMULTI;
1034 if (ifp->if_flags & IFF_PROMISC)
1035 return;
1036
1037 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1038 if (enm == NULL)
1039 return;
1040 hashes[0] = hashes[1] = 0;
1041 do {
1042 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1043 /*
1044 * We must listen to a range of multicast addresses.
1045 * For now, just accept all multicasts, rather than
1046 * trying to set only those filter bits needed to match
1047 * the range. (At this time, the only use of address
1048 * ranges is for IP multicast routing, for which the
1049 * range is big enough to require all bits set.)
1050 */
1051 goto allmulti;
1052 }
1053 h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
1054 hashes[h >> 5] |= 1 << (h & 0x1f);
1055 ETHER_NEXT_MULTI(step, enm);
1056 } while (enm != NULL);
1057 sc->sc_rxc |= RXC_MHTE;
1058 CSR_WRITE_4(sc, MTR0, hashes[0]);
1059 CSR_WRITE_4(sc, MTR1, hashes[1]);
1060 return;
1061 allmulti:
1062 sc->sc_rxc |= RXC_RM;
1063 ifp->if_flags |= IFF_ALLMULTI;
1064 }
1065
1066 static int
1067 add_rxbuf(struct kse_softc *sc, int idx)
1068 {
1069 struct kse_rxsoft *rxs = &sc->sc_rxsoft[idx];
1070 struct mbuf *m;
1071 int error;
1072
1073 MGETHDR(m, M_DONTWAIT, MT_DATA);
1074 if (m == NULL)
1075 return ENOBUFS;
1076
1077 MCLGET(m, M_DONTWAIT);
1078 if ((m->m_flags & M_EXT) == 0) {
1079 m_freem(m);
1080 return ENOBUFS;
1081 }
1082
1083 if (rxs->rxs_mbuf != NULL)
1084 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1085
1086 rxs->rxs_mbuf = m;
1087
1088 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
1089 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1090 if (error) {
1091 printf("%s: can't load rx DMA map %d, error = %d\n",
1092 sc->sc_dev.dv_xname, idx, error);
1093 panic("kse_add_rxbuf");
1094 }
1095
1096 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1097 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1098
1099 KSE_INIT_RXDESC(sc, idx);
1100
1101 return 0;
1102 }
1103
1104 static void
1105 rxdrain(struct kse_softc *sc)
1106 {
1107 struct kse_rxsoft *rxs;
1108 int i;
1109
1110 for (i = 0; i < KSE_NRXDESC; i++) {
1111 rxs = &sc->sc_rxsoft[i];
1112 if (rxs->rxs_mbuf != NULL) {
1113 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1114 m_freem(rxs->rxs_mbuf);
1115 rxs->rxs_mbuf = NULL;
1116 }
1117 }
1118 }
1119
1120 static int
1121 kse_intr(void *arg)
1122 {
1123 struct kse_softc *sc = arg;
1124 uint32_t isr;
1125
1126 if ((isr = CSR_READ_4(sc, INTST)) == 0)
1127 return 0;
1128
1129 if (isr & INT_DMRS)
1130 rxintr(sc);
1131 if (isr & INT_DMTS)
1132 txreap(sc);
1133 if (isr & INT_DMLCS)
1134 lnkchg(sc);
1135 if (isr & INT_DMRBUS)
1136 printf("%s: Rx descriptor full\n", sc->sc_dev.dv_xname);
1137
1138 CSR_WRITE_4(sc, INTST, isr);
1139 return 1;
1140 }
1141
1142 static void
1143 rxintr(struct kse_softc *sc)
1144 {
1145 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1146 struct kse_rxsoft *rxs;
1147 struct mbuf *m;
1148 uint32_t rxstat;
1149 int i, len;
1150
1151 for (i = sc->sc_rxptr; /*CONSTCOND*/ 1; i = KSE_NEXTRX(i)) {
1152 rxs = &sc->sc_rxsoft[i];
1153
1154 KSE_CDRXSYNC(sc, i,
1155 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1156
1157 rxstat = sc->sc_rxdescs[i].r0;
1158
1159 if (rxstat & R0_OWN) /* desc is left empty */
1160 break;
1161
1162 /* R0_FS|R0_LS must have been marked for this desc */
1163
1164 if (rxstat & R0_ES) {
1165 ifp->if_ierrors++;
1166 #define PRINTERR(bit, str) \
1167 if (rxstat & (bit)) \
1168 printf("%s: receive error: %s\n", \
1169 sc->sc_dev.dv_xname, str)
1170 PRINTERR(R0_TL, "frame too long");
1171 PRINTERR(R0_RF, "runt frame");
1172 PRINTERR(R0_CE, "bad FCS");
1173 #undef PRINTERR
1174 KSE_INIT_RXDESC(sc, i);
1175 continue;
1176 }
1177
1178 /* HW errata; frame might be too small or too large */
1179
1180 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1181 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1182
1183 len = rxstat & R0_FL_MASK;
1184 len -= ETHER_CRC_LEN; /* trim CRC off */
1185 m = rxs->rxs_mbuf;
1186
1187 if (add_rxbuf(sc, i) != 0) {
1188 ifp->if_ierrors++;
1189 KSE_INIT_RXDESC(sc, i);
1190 bus_dmamap_sync(sc->sc_dmat,
1191 rxs->rxs_dmamap, 0,
1192 rxs->rxs_dmamap->dm_mapsize,
1193 BUS_DMASYNC_PREREAD);
1194 continue;
1195 }
1196
1197 ifp->if_ipackets++;
1198 m->m_pkthdr.rcvif = ifp;
1199 m->m_pkthdr.len = m->m_len = len;
1200
1201 if (sc->sc_mcsum) {
1202 m->m_pkthdr.csum_flags |= sc->sc_mcsum;
1203 if (rxstat & R0_IPE)
1204 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1205 if (rxstat & (R0_TCPE | R0_UDPE))
1206 m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
1207 }
1208 #if NBPFILTER > 0
1209 if (ifp->if_bpf)
1210 bpf_mtap(ifp->if_bpf, m);
1211 #endif /* NBPFILTER > 0 */
1212 (*ifp->if_input)(ifp, m);
1213 #ifdef KSEDIAGNOSTIC
1214 if (kse_monitor_rxintr > 0) {
1215 printf("m stat %x data %p len %d\n",
1216 rxstat, m->m_data, m->m_len);
1217 }
1218 #endif
1219 }
1220 sc->sc_rxptr = i;
1221 }
1222
1223 static void
1224 txreap(struct kse_softc *sc)
1225 {
1226 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1227 struct kse_txsoft *txs;
1228 uint32_t txstat;
1229 int i;
1230
1231 ifp->if_flags &= ~IFF_OACTIVE;
1232
1233 for (i = sc->sc_txsdirty; sc->sc_txsfree != KSE_TXQUEUELEN;
1234 i = KSE_NEXTTXS(i), sc->sc_txsfree++) {
1235 txs = &sc->sc_txsoft[i];
1236
1237 KSE_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndesc,
1238 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1239
1240 txstat = sc->sc_txdescs[txs->txs_lastdesc].t0;
1241
1242 if (txstat & T0_OWN) /* desc is still in use */
1243 break;
1244
1245 /* there is no way to tell transmission status per frame */
1246
1247 ifp->if_opackets++;
1248
1249 sc->sc_txfree += txs->txs_ndesc;
1250 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1251 0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1252 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1253 m_freem(txs->txs_mbuf);
1254 txs->txs_mbuf = NULL;
1255 }
1256 sc->sc_txsdirty = i;
1257 if (sc->sc_txsfree == KSE_TXQUEUELEN)
1258 ifp->if_timer = 0;
1259 }
1260
1261 static void
1262 lnkchg(struct kse_softc *sc)
1263 {
1264 struct ifmediareq ifmr;
1265
1266 #if 0 /* rambling link status */
1267 printf("%s: link %s\n", sc->sc_dev.dv_xname,
1268 (CSR_READ_2(sc, P1SR) & (1U << 5)) ? "up" : "down");
1269 #endif
1270 ifmedia_sts(&sc->sc_ethercom.ec_if, &ifmr);
1271 }
1272
1273 static int
1274 ifmedia_upd(struct ifnet *ifp)
1275 {
1276 struct kse_softc *sc = ifp->if_softc;
1277 struct ifmedia *ifm = &sc->sc_media;
1278 uint16_t ctl;
1279
1280 ctl = 0;
1281 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
1282 ctl |= (1U << 13); /* restart AN */
1283 ctl |= (1U << 7); /* enable AN */
1284 ctl |= (1U << 4); /* advertise flow control pause */
1285 ctl |= (1U << 3) | (1U << 2) | (1U << 1) | (1U << 0);
1286 }
1287 else {
1288 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX)
1289 ctl |= (1U << 6);
1290 if (ifm->ifm_media & IFM_FDX)
1291 ctl |= (1U << 5);
1292 }
1293 CSR_WRITE_2(sc, P1CR4, ctl);
1294
1295 sc->sc_media_active = IFM_NONE;
1296 sc->sc_media_status = IFM_AVALID;
1297
1298 return 0;
1299 }
1300
1301 static void
1302 ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1303 {
1304 struct kse_softc *sc = ifp->if_softc;
1305 struct ifmedia *ifm = &sc->sc_media;
1306 uint16_t ctl, sts, result;
1307
1308 ifmr->ifm_status = IFM_AVALID;
1309 ifmr->ifm_active = IFM_ETHER;
1310
1311 ctl = CSR_READ_2(sc, P1CR4);
1312 sts = CSR_READ_2(sc, P1SR);
1313 if ((sts & (1U << 5)) == 0) {
1314 ifmr->ifm_active |= IFM_NONE;
1315 goto out; /* link is down */
1316 }
1317 ifmr->ifm_status |= IFM_ACTIVE;
1318 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
1319 if ((sts & (1U << 6)) == 0) {
1320 ifmr->ifm_active |= IFM_NONE;
1321 goto out; /* negotiation in progress */
1322 }
1323 result = ctl & sts & 017;
1324 if (result & (1U << 3))
1325 ifmr->ifm_active |= IFM_100_TX|IFM_FDX;
1326 else if (result & (1U << 2))
1327 ifmr->ifm_active |= IFM_100_TX;
1328 else if (result & (1U << 1))
1329 ifmr->ifm_active |= IFM_10_T|IFM_FDX;
1330 else if (result & (1U << 0))
1331 ifmr->ifm_active |= IFM_10_T;
1332 else
1333 ifmr->ifm_active |= IFM_NONE;
1334 if (ctl & (1U << 4))
1335 ifmr->ifm_active |= IFM_FLOW | IFM_ETH_RXPAUSE;
1336 if (sts & (1U << 4))
1337 ifmr->ifm_active |= IFM_FLOW | IFM_ETH_TXPAUSE;
1338 }
1339 else {
1340 ifmr->ifm_active |= (sts & (1U << 10)) ? IFM_100_TX : IFM_10_T;
1341 if (sts & (1U << 9))
1342 ifmr->ifm_active |= IFM_FDX;
1343 if (sts & (1U << 12))
1344 ifmr->ifm_active |= IFM_FLOW | IFM_ETH_RXPAUSE;
1345 if (sts & (1U << 11))
1346 ifmr->ifm_active |= IFM_FLOW | IFM_ETH_TXPAUSE;
1347 }
1348
1349 out:
1350 sc->sc_media_status = ifmr->ifm_status;
1351 sc->sc_media_active = ifmr->ifm_active;
1352 }
1353
1354 static void
1355 phy_tick(void *arg)
1356 {
1357 struct kse_softc *sc = arg;
1358 struct ifmediareq ifmr;
1359 int s;
1360
1361 s = splnet();
1362 ifmedia_sts(&sc->sc_ethercom.ec_if, &ifmr);
1363 splx(s);
1364
1365 callout_reset(&sc->sc_callout, hz, phy_tick, sc);
1366 }
1367
1368 static int
1369 ifmedia2_upd(struct ifnet *ifp)
1370 {
1371 struct kse_softc *sc = ifp->if_softc;
1372
1373 sc->sc_media_status = IFM_AVALID;
1374 sc->sc_media_active = IFM_NONE;
1375 return 0;
1376 }
1377
1378 static void
1379 ifmedia2_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1380 {
1381 struct kse_softc *sc = ifp->if_softc;
1382 int p1sts, p2sts;
1383
1384 ifmr->ifm_status = IFM_AVALID;
1385 ifmr->ifm_active = IFM_ETHER;
1386 p1sts = CSR_READ_2(sc, P1SR);
1387 p2sts = CSR_READ_2(sc, P2SR);
1388 if (((p1sts | p2sts) & (1U << 5)) == 0)
1389 ifmr->ifm_active |= IFM_NONE;
1390 else {
1391 ifmr->ifm_status |= IFM_ACTIVE;
1392 ifmr->ifm_active |= IFM_100_TX|IFM_FDX;
1393 ifmr->ifm_active |= IFM_FLOW|IFM_ETH_RXPAUSE|IFM_ETH_TXPAUSE;
1394 }
1395 sc->sc_media_status = ifmr->ifm_status;
1396 sc->sc_media_active = ifmr->ifm_active;
1397 }
1398
1399 #ifdef KSE_EVENT_COUNTERS
1400 static void
1401 stat_tick(arg)
1402 void *arg;
1403 {
1404 struct kse_softc *sc = arg;
1405 struct ksext *ee = &sc->sc_ext;
1406 int nport, p, i, val;
1407
1408 nport = (sc->sc_chip == 0x8842) ? 3 : 1;
1409 for (p = 0; p < nport; p++) {
1410 for (i = 0; i < 32; i++) {
1411 val = 0x1c00 | (p * 0x20 + i);
1412 CSR_WRITE_2(sc, IACR, val);
1413 do {
1414 val = CSR_READ_2(sc, IADR5) << 16;
1415 } while ((val & (1U << 30)) == 0);
1416 if (val & (1U << 31)) {
1417 (void)CSR_READ_2(sc, IADR4);
1418 val = 0x3fffffff; /* has made overflow */
1419 }
1420 else {
1421 val &= 0x3fff0000; /* 29:16 */
1422 val |= CSR_READ_2(sc, IADR4); /* 15:0 */
1423 }
1424 ee->pev[p][i].ev_count += val; /* i (0-31) */
1425 }
1426 CSR_WRITE_2(sc, IACR, 0x1c00 + 0x100 + p);
1427 ee->pev[p][32].ev_count = CSR_READ_2(sc, IADR4); /* 32 */
1428 CSR_WRITE_2(sc, IACR, 0x1c00 + 0x100 + p * 3 + 1);
1429 ee->pev[p][33].ev_count = CSR_READ_2(sc, IADR4); /* 33 */
1430 }
1431 callout_reset(&sc->sc_stat_ch, hz * 60, stat_tick, arg);
1432 }
1433
1434 static void
1435 zerostats(struct kse_softc *sc)
1436 {
1437 struct ksext *ee = &sc->sc_ext;
1438 int nport, p, i, val;
1439
1440 /* make sure all the HW counters get zero */
1441 nport = (sc->sc_chip == 0x8842) ? 3 : 1;
1442 for (p = 0; p < nport; p++) {
1443 for (i = 0; i < 31; i++) {
1444 val = 0x1c00 | (p * 0x20 + i);
1445 CSR_WRITE_2(sc, IACR, val);
1446 do {
1447 val = CSR_READ_2(sc, IADR5) << 16;
1448 } while ((val & (1U << 30)) == 0);
1449 (void)CSR_READ_2(sc, IADR4);
1450 ee->pev[p][i].ev_count = 0;
1451 }
1452 }
1453 }
1454 #endif
1455