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