pq3etsec.c revision 1.52.2.1 1 /* $NetBSD: pq3etsec.c,v 1.52.2.1 2021/03/20 19:33:37 thorpej Exp $ */
2 /*-
3 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8 * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9 *
10 * This material is based upon work supported by the Defense Advanced Research
11 * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12 * Contract No. N66001-09-C-2073.
13 * Approved for Public Release, Distribution Unlimited
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: pq3etsec.c,v 1.52.2.1 2021/03/20 19:33:37 thorpej Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_inet.h"
42 #include "opt_mpc85xx.h"
43 #include "opt_multiprocessor.h"
44 #include "opt_net_mpsafe.h"
45 #endif
46
47 #include <sys/param.h>
48 #include <sys/cpu.h>
49 #include <sys/device.h>
50 #include <sys/mbuf.h>
51 #include <sys/ioctl.h>
52 #include <sys/intr.h>
53 #include <sys/bus.h>
54 #include <sys/kernel.h>
55 #include <sys/kmem.h>
56 #include <sys/proc.h>
57 #include <sys/atomic.h>
58 #include <sys/callout.h>
59 #include <sys/sysctl.h>
60
61 #include <sys/rndsource.h>
62
63 #include <net/if.h>
64 #include <net/if_dl.h>
65 #include <net/if_ether.h>
66 #include <net/if_media.h>
67 #include <net/bpf.h>
68
69 #include <dev/mii/miivar.h>
70
71 #ifdef INET
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/in_offload.h>
76 #endif /* INET */
77 #ifdef INET6
78 #include <netinet6/in6.h>
79 #include <netinet/ip6.h>
80 #endif
81 #include <netinet6/in6_offload.h>
82
83 #include <powerpc/spr.h>
84 #include <powerpc/booke/spr.h>
85 #include <powerpc/booke/cpuvar.h>
86 #include <powerpc/booke/e500var.h>
87 #include <powerpc/booke/e500reg.h>
88 #include <powerpc/booke/etsecreg.h>
89
90 #define M_HASFCB M_LINK2 /* tx packet has FCB prepended */
91
92 #define ETSEC_MAXTXMBUFS 30
93 #define ETSEC_NTXSEGS 30
94 #define ETSEC_MAXRXMBUFS 511
95 #define ETSEC_MINRXMBUFS 32
96 #define ETSEC_NRXSEGS 1
97
98 #define IFCAP_RCTRL_IPCSEN IFCAP_CSUM_IPv4_Rx
99 #define IFCAP_RCTRL_TUCSEN (IFCAP_CSUM_TCPv4_Rx \
100 | IFCAP_CSUM_UDPv4_Rx \
101 | IFCAP_CSUM_TCPv6_Rx \
102 | IFCAP_CSUM_UDPv6_Rx)
103
104 #define IFCAP_TCTRL_IPCSEN IFCAP_CSUM_IPv4_Tx
105 #define IFCAP_TCTRL_TUCSEN (IFCAP_CSUM_TCPv4_Tx \
106 | IFCAP_CSUM_UDPv4_Tx \
107 | IFCAP_CSUM_TCPv6_Tx \
108 | IFCAP_CSUM_UDPv6_Tx)
109
110 #define IFCAP_ETSEC (IFCAP_RCTRL_IPCSEN | IFCAP_RCTRL_TUCSEN \
111 | IFCAP_TCTRL_IPCSEN | IFCAP_TCTRL_TUCSEN)
112
113 #define M_CSUM_IP (M_CSUM_CIP | M_CSUM_CTU)
114 #define M_CSUM_IP6 (M_CSUM_TCPv6 | M_CSUM_UDPv6)
115 #define M_CSUM_TUP (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_TCPv6 | M_CSUM_UDPv6)
116 #define M_CSUM_UDP (M_CSUM_UDPv4 | M_CSUM_UDPv6)
117 #define M_CSUM_IP4 (M_CSUM_IPv4 | M_CSUM_UDPv4 | M_CSUM_TCPv4)
118 #define M_CSUM_CIP (M_CSUM_IPv4)
119 #define M_CSUM_CTU (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_TCPv6 | M_CSUM_UDPv6)
120
121 struct pq3etsec_txqueue {
122 bus_dmamap_t txq_descmap;
123 volatile struct txbd *txq_consumer;
124 volatile struct txbd *txq_producer;
125 volatile struct txbd *txq_first;
126 volatile struct txbd *txq_last;
127 struct ifqueue txq_mbufs;
128 struct mbuf *txq_next;
129 #ifdef ETSEC_DEBUG
130 struct mbuf *txq_lmbufs[512];
131 #endif
132 uint32_t txq_qmask;
133 uint32_t txq_free;
134 uint32_t txq_threshold;
135 uint32_t txq_lastintr;
136 bus_size_t txq_reg_tbase;
137 bus_dma_segment_t txq_descmap_seg;
138 };
139
140 struct pq3etsec_rxqueue {
141 bus_dmamap_t rxq_descmap;
142 volatile struct rxbd *rxq_consumer;
143 volatile struct rxbd *rxq_producer;
144 volatile struct rxbd *rxq_first;
145 volatile struct rxbd *rxq_last;
146 struct mbuf *rxq_mhead;
147 struct mbuf **rxq_mtail;
148 struct mbuf *rxq_mconsumer;
149 #ifdef ETSEC_DEBUG
150 struct mbuf *rxq_mbufs[512];
151 #endif
152 uint32_t rxq_qmask;
153 uint32_t rxq_inuse;
154 uint32_t rxq_threshold;
155 bus_size_t rxq_reg_rbase;
156 bus_size_t rxq_reg_rbptr;
157 bus_dma_segment_t rxq_descmap_seg;
158 };
159
160 struct pq3etsec_mapcache {
161 u_int dmc_nmaps;
162 u_int dmc_maxseg;
163 u_int dmc_maxmaps;
164 u_int dmc_maxmapsize;
165 bus_dmamap_t dmc_maps[0];
166 };
167
168 struct pq3etsec_softc {
169 device_t sc_dev;
170 device_t sc_mdio_dev;
171 struct ethercom sc_ec;
172 #define sc_if sc_ec.ec_if
173 struct mii_data sc_mii;
174 bus_space_tag_t sc_bst;
175 bus_space_handle_t sc_bsh;
176 bus_space_handle_t sc_mdio_bsh;
177 bus_dma_tag_t sc_dmat;
178 int sc_phy_addr;
179 prop_dictionary_t sc_intrmap;
180 uint32_t sc_intrmask;
181
182 uint32_t sc_soft_flags;
183 #define SOFT_RESET 0x0001
184 #define SOFT_RXINTR 0x0010
185 #define SOFT_RXBSY 0x0020
186 #define SOFT_TXINTR 0x0100
187 #define SOFT_TXERROR 0x0200
188
189 struct pq3etsec_txqueue sc_txq;
190 struct pq3etsec_rxqueue sc_rxq;
191 uint32_t sc_txerrors;
192 uint32_t sc_rxerrors;
193
194 size_t sc_rx_adjlen;
195
196 /*
197 * Copies of various ETSEC registers.
198 */
199 uint32_t sc_imask;
200 uint32_t sc_maccfg1;
201 uint32_t sc_maccfg2;
202 uint32_t sc_maxfrm;
203 uint32_t sc_ecntrl;
204 uint32_t sc_dmactrl;
205 uint32_t sc_macstnaddr1;
206 uint32_t sc_macstnaddr2;
207 uint32_t sc_tctrl;
208 uint32_t sc_rctrl;
209 uint32_t sc_gaddr[16];
210 uint64_t sc_macaddrs[15];
211
212 void *sc_tx_ih;
213 void *sc_rx_ih;
214 void *sc_error_ih;
215 void *sc_soft_ih;
216
217 kmutex_t *sc_lock;
218 kmutex_t *sc_hwlock;
219
220 struct evcnt sc_ev_tx_stall;
221 struct evcnt sc_ev_tx_intr;
222 struct evcnt sc_ev_rx_stall;
223 struct evcnt sc_ev_rx_intr;
224 struct evcnt sc_ev_error_intr;
225 struct evcnt sc_ev_soft_intr;
226 struct evcnt sc_ev_tx_pause;
227 struct evcnt sc_ev_rx_pause;
228 struct evcnt sc_ev_mii_ticks;
229
230 struct callout sc_mii_callout;
231 uint64_t sc_mii_last_tick;
232
233 struct ifqueue sc_rx_bufcache;
234 struct pq3etsec_mapcache *sc_rx_mapcache;
235 struct pq3etsec_mapcache *sc_tx_mapcache;
236
237 /* Interrupt Coalescing parameters */
238 int sc_ic_rx_time;
239 int sc_ic_rx_count;
240 int sc_ic_tx_time;
241 int sc_ic_tx_count;
242
243 krndsource_t rnd_source;
244 };
245
246 #define ETSEC_IC_RX_ENABLED(sc) \
247 ((sc)->sc_ic_rx_time != 0 && (sc)->sc_ic_rx_count != 0)
248 #define ETSEC_IC_TX_ENABLED(sc) \
249 ((sc)->sc_ic_tx_time != 0 && (sc)->sc_ic_tx_count != 0)
250
251 struct pq3mdio_softc {
252 device_t mdio_dev;
253
254 kmutex_t *mdio_lock;
255
256 bus_space_tag_t mdio_bst;
257 bus_space_handle_t mdio_bsh;
258 };
259
260 static int pq3etsec_match(device_t, cfdata_t, void *);
261 static void pq3etsec_attach(device_t, device_t, void *);
262
263 static int pq3mdio_match(device_t, cfdata_t, void *);
264 static void pq3mdio_attach(device_t, device_t, void *);
265
266 static void pq3etsec_ifstart(struct ifnet *);
267 static void pq3etsec_ifwatchdog(struct ifnet *);
268 static int pq3etsec_ifinit(struct ifnet *);
269 static void pq3etsec_ifstop(struct ifnet *, int);
270 static int pq3etsec_ifioctl(struct ifnet *, u_long, void *);
271
272 static int pq3etsec_mapcache_create(struct pq3etsec_softc *,
273 struct pq3etsec_mapcache **, size_t, size_t, size_t);
274 static void pq3etsec_mapcache_destroy(struct pq3etsec_softc *,
275 struct pq3etsec_mapcache *);
276 static bus_dmamap_t pq3etsec_mapcache_get(struct pq3etsec_softc *,
277 struct pq3etsec_mapcache *);
278 static void pq3etsec_mapcache_put(struct pq3etsec_softc *,
279 struct pq3etsec_mapcache *, bus_dmamap_t);
280
281 static int pq3etsec_txq_attach(struct pq3etsec_softc *,
282 struct pq3etsec_txqueue *, u_int);
283 static void pq3etsec_txq_purge(struct pq3etsec_softc *,
284 struct pq3etsec_txqueue *);
285 static void pq3etsec_txq_reset(struct pq3etsec_softc *,
286 struct pq3etsec_txqueue *);
287 static bool pq3etsec_txq_consume(struct pq3etsec_softc *,
288 struct pq3etsec_txqueue *);
289 static bool pq3etsec_txq_produce(struct pq3etsec_softc *,
290 struct pq3etsec_txqueue *, struct mbuf *m);
291 static bool pq3etsec_txq_active_p(struct pq3etsec_softc *,
292 struct pq3etsec_txqueue *);
293
294 static int pq3etsec_rxq_attach(struct pq3etsec_softc *,
295 struct pq3etsec_rxqueue *, u_int);
296 static bool pq3etsec_rxq_produce(struct pq3etsec_softc *,
297 struct pq3etsec_rxqueue *);
298 static void pq3etsec_rxq_purge(struct pq3etsec_softc *,
299 struct pq3etsec_rxqueue *, bool);
300 static void pq3etsec_rxq_reset(struct pq3etsec_softc *,
301 struct pq3etsec_rxqueue *);
302
303 static void pq3etsec_mc_setup(struct pq3etsec_softc *);
304
305 static void pq3etsec_mii_tick(void *);
306 static int pq3etsec_rx_intr(void *);
307 static int pq3etsec_tx_intr(void *);
308 static int pq3etsec_error_intr(void *);
309 static void pq3etsec_soft_intr(void *);
310
311 static void pq3etsec_set_ic_rx(struct pq3etsec_softc *);
312 static void pq3etsec_set_ic_tx(struct pq3etsec_softc *);
313
314 static void pq3etsec_sysctl_setup(struct sysctllog **, struct pq3etsec_softc *);
315
316 CFATTACH_DECL_NEW(pq3etsec, sizeof(struct pq3etsec_softc),
317 pq3etsec_match, pq3etsec_attach, NULL, NULL);
318
319 CFATTACH_DECL_NEW(pq3mdio_tsec, sizeof(struct pq3mdio_softc),
320 pq3mdio_match, pq3mdio_attach, NULL, NULL);
321
322 CFATTACH_DECL_NEW(pq3mdio_cpunode, sizeof(struct pq3mdio_softc),
323 pq3mdio_match, pq3mdio_attach, NULL, NULL);
324
325 static inline uint32_t
326 etsec_mdio_read(struct pq3mdio_softc *mdio, bus_size_t off)
327 {
328 return bus_space_read_4(mdio->mdio_bst, mdio->mdio_bsh, off);
329 }
330
331 static inline void
332 etsec_mdio_write(struct pq3mdio_softc *mdio, bus_size_t off, uint32_t data)
333 {
334 bus_space_write_4(mdio->mdio_bst, mdio->mdio_bsh, off, data);
335 }
336
337 static inline uint32_t
338 etsec_read(struct pq3etsec_softc *sc, bus_size_t off)
339 {
340 return bus_space_read_4(sc->sc_bst, sc->sc_bsh, off);
341 }
342
343 static int
344 pq3mdio_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
345 {
346 return strcmp(cf->cf_name, "mdio") == 0;
347 }
348
349 static int
350 pq3mdio_match(device_t parent, cfdata_t cf, void *aux)
351 {
352 const uint16_t svr = (mfspr(SPR_SVR) & ~0x80000) >> 16;
353 const bool p1025_p = (svr == (SVR_P1025v1 >> 16)
354 || svr == (SVR_P1016v1 >> 16));
355
356 if (device_is_a(parent, "cpunode")) {
357 if (!p1025_p
358 || !e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
359 return 0;
360
361 return 1;
362 }
363
364 if (device_is_a(parent, "tsec")) {
365 if (p1025_p
366 || !e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
367 return 0;
368
369 return 1;
370 }
371
372 return 0;
373 }
374
375 static void
376 pq3mdio_attach(device_t parent, device_t self, void *aux)
377 {
378 struct pq3mdio_softc * const mdio = device_private(self);
379 struct cpunode_attach_args * const cna = aux;
380 struct cpunode_locators * const cnl = &cna->cna_locs;
381
382 mdio->mdio_dev = self;
383 mdio->mdio_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
384
385 if (device_is_a(parent, "cpunode")) {
386 struct cpunode_softc * const psc = device_private(parent);
387 psc->sc_children |= cna->cna_childmask;
388
389 mdio->mdio_bst = cna->cna_memt;
390 if (bus_space_map(mdio->mdio_bst, cnl->cnl_addr,
391 cnl->cnl_size, 0, &mdio->mdio_bsh) != 0) {
392 aprint_error(": error mapping registers @ %#x\n",
393 cnl->cnl_addr);
394 return;
395 }
396 } else {
397 struct pq3etsec_softc * const sc = device_private(parent);
398
399 KASSERT(device_is_a(parent, "tsec"));
400 KASSERTMSG(cnl->cnl_addr == ETSEC1_BASE
401 || cnl->cnl_addr == ETSEC2_BASE
402 || cnl->cnl_addr == ETSEC3_BASE
403 || cnl->cnl_addr == ETSEC4_BASE,
404 "unknown tsec addr %x", cnl->cnl_addr);
405
406 mdio->mdio_bst = sc->sc_bst;
407 mdio->mdio_bsh = sc->sc_bsh;
408 }
409
410 aprint_normal("\n");
411 }
412
413 static int
414 pq3mdio_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
415 {
416 struct pq3mdio_softc * const mdio = device_private(self);
417 uint32_t miimcom = etsec_mdio_read(mdio, MIIMCOM);
418
419 mutex_enter(mdio->mdio_lock);
420
421 etsec_mdio_write(mdio, MIIMADD,
422 __SHIFTIN(phy, MIIMADD_PHY) | __SHIFTIN(reg, MIIMADD_REG));
423
424 etsec_mdio_write(mdio, MIIMCOM, 0); /* clear any past bits */
425 etsec_mdio_write(mdio, MIIMCOM, MIIMCOM_READ);
426
427 while (etsec_mdio_read(mdio, MIIMIND) != 0) {
428 delay(1);
429 }
430 *val = etsec_mdio_read(mdio, MIIMSTAT) &0xffff;
431
432 if (miimcom == MIIMCOM_SCAN)
433 etsec_mdio_write(mdio, MIIMCOM, miimcom);
434
435 #if 0
436 aprint_normal_dev(mdio->mdio_dev, "%s: phy %d reg %d: %#x\n",
437 __func__, phy, reg, data);
438 #endif
439 mutex_exit(mdio->mdio_lock);
440 return 0;
441 }
442
443 static int
444 pq3mdio_mii_writereg(device_t self, int phy, int reg, uint16_t data)
445 {
446 struct pq3mdio_softc * const mdio = device_private(self);
447 uint32_t miimcom = etsec_mdio_read(mdio, MIIMCOM);
448
449 #if 0
450 aprint_normal_dev(mdio->mdio_dev, "%s: phy %d reg %d: %#x\n",
451 __func__, phy, reg, data);
452 #endif
453
454 mutex_enter(mdio->mdio_lock);
455
456 etsec_mdio_write(mdio, MIIMADD,
457 __SHIFTIN(phy, MIIMADD_PHY) | __SHIFTIN(reg, MIIMADD_REG));
458 etsec_mdio_write(mdio, MIIMCOM, 0); /* clear any past bits */
459 etsec_mdio_write(mdio, MIIMCON, data);
460
461 int timo = 1000; /* 1ms */
462 while ((etsec_mdio_read(mdio, MIIMIND) & MIIMIND_BUSY) && --timo > 0) {
463 delay(1);
464 }
465
466 if (miimcom == MIIMCOM_SCAN)
467 etsec_mdio_write(mdio, MIIMCOM, miimcom);
468
469 mutex_exit(mdio->mdio_lock);
470
471 return 0;
472 }
473
474 static inline void
475 etsec_write(struct pq3etsec_softc *sc, bus_size_t off, uint32_t data)
476 {
477 bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, data);
478 }
479
480 static void
481 pq3etsec_mii_statchg(struct ifnet *ifp)
482 {
483 struct pq3etsec_softc * const sc = ifp->if_softc;
484 struct mii_data * const mii = &sc->sc_mii;
485
486 uint32_t maccfg1 = sc->sc_maccfg1;
487 uint32_t maccfg2 = sc->sc_maccfg2;
488 uint32_t ecntrl = sc->sc_ecntrl;
489
490 maccfg1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
491 maccfg2 &= ~(MACCFG2_IFMODE | MACCFG2_FD);
492
493 if (sc->sc_mii.mii_media_active & IFM_FDX) {
494 maccfg2 |= MACCFG2_FD;
495 }
496
497 /*
498 * Now deal with the flow control bits.
499 */
500 if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO
501 && (mii->mii_media_active & IFM_ETH_FMASK)) {
502 if (mii->mii_media_active & IFM_ETH_RXPAUSE)
503 maccfg1 |= MACCFG1_RX_FLOW;
504 if (mii->mii_media_active & IFM_ETH_TXPAUSE)
505 maccfg1 |= MACCFG1_TX_FLOW;
506 }
507
508 /*
509 * Now deal with the speed.
510 */
511 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
512 maccfg2 |= MACCFG2_IFMODE_GMII;
513 } else {
514 maccfg2 |= MACCFG2_IFMODE_MII;
515 ecntrl &= ~ECNTRL_R100M;
516 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_10_T) {
517 ecntrl |= ECNTRL_R100M;
518 }
519 }
520
521 /*
522 * If things are different, re-init things.
523 */
524 if (maccfg1 != sc->sc_maccfg1
525 || maccfg2 != sc->sc_maccfg2
526 || ecntrl != sc->sc_ecntrl) {
527 if (sc->sc_if.if_flags & IFF_RUNNING)
528 atomic_or_uint(&sc->sc_soft_flags, SOFT_RESET);
529 sc->sc_maccfg1 = maccfg1;
530 sc->sc_maccfg2 = maccfg2;
531 sc->sc_ecntrl = ecntrl;
532 }
533 }
534
535 #if 0
536 static void
537 pq3etsec_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
538 {
539 struct pq3etsec_softc * const sc = ifp->if_softc;
540
541 mii_pollstat(&sc->sc_mii);
542 ether_mediastatus(ifp, ifmr);
543 ifmr->ifm_status = sc->sc_mii.mii_media_status;
544 ifmr->ifm_active = sc->sc_mii.mii_media_active;
545 }
546
547 static int
548 pq3etsec_mediachange(struct ifnet *ifp)
549 {
550 struct pq3etsec_softc * const sc = ifp->if_softc;
551
552 if ((ifp->if_flags & IFF_UP) == 0)
553 return 0;
554
555 int rv = mii_mediachg(&sc->sc_mii);
556 return (rv == ENXIO) ? 0 : rv;
557 }
558 #endif
559
560 static int
561 pq3etsec_match(device_t parent, cfdata_t cf, void *aux)
562 {
563
564 if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
565 return 0;
566
567 return 1;
568 }
569
570 static void
571 pq3etsec_attach(device_t parent, device_t self, void *aux)
572 {
573 struct cpunode_softc * const psc = device_private(parent);
574 struct pq3etsec_softc * const sc = device_private(self);
575 struct mii_data * const mii = &sc->sc_mii;
576 struct cpunode_attach_args * const cna = aux;
577 struct cpunode_locators * const cnl = &cna->cna_locs;
578 cfdata_t cf = device_cfdata(self);
579 int error;
580
581 psc->sc_children |= cna->cna_childmask;
582 sc->sc_dev = self;
583 sc->sc_bst = cna->cna_memt;
584 sc->sc_dmat = &booke_bus_dma_tag;
585
586 /*
587 * Pull out the mdio bus and phy we are supposed to use.
588 */
589 const int mdio = cf->cf_loc[CPUNODECF_MDIO];
590 const int phy = cf->cf_loc[CPUNODECF_PHY];
591 if (mdio != CPUNODECF_MDIO_DEFAULT)
592 aprint_normal(" mdio %d", mdio);
593
594 /*
595 * See if the phy is in the config file...
596 */
597 if (phy != CPUNODECF_PHY_DEFAULT) {
598 sc->sc_phy_addr = phy;
599 } else {
600 unsigned char prop_name[20];
601 snprintf(prop_name, sizeof(prop_name), "tsec%u-phy-addr",
602 cnl->cnl_instance);
603 sc->sc_phy_addr = board_info_get_number(prop_name);
604 }
605 if (sc->sc_phy_addr != MII_PHY_ANY)
606 aprint_normal(" phy %d", sc->sc_phy_addr);
607
608 error = bus_space_map(sc->sc_bst, cnl->cnl_addr, cnl->cnl_size, 0,
609 &sc->sc_bsh);
610 if (error) {
611 aprint_error(": error mapping registers: %d\n", error);
612 return;
613 }
614
615 /*
616 * Assume firmware has aready set the mac address and fetch it
617 * before we reinit it.
618 */
619 sc->sc_macstnaddr2 = etsec_read(sc, MACSTNADDR2);
620 sc->sc_macstnaddr1 = etsec_read(sc, MACSTNADDR1);
621 sc->sc_rctrl = RCTRL_DEFAULT;
622 sc->sc_ecntrl = etsec_read(sc, ECNTRL);
623 sc->sc_maccfg1 = etsec_read(sc, MACCFG1);
624 sc->sc_maccfg2 = etsec_read(sc, MACCFG2) | MACCFG2_DEFAULT;
625
626 if (sc->sc_macstnaddr1 == 0 && sc->sc_macstnaddr2 == 0) {
627 size_t len;
628 const uint8_t *mac_addr =
629 board_info_get_data("tsec-mac-addr-base", &len);
630 KASSERT(len == ETHER_ADDR_LEN);
631 sc->sc_macstnaddr2 =
632 (mac_addr[1] << 24)
633 | (mac_addr[0] << 16);
634 sc->sc_macstnaddr1 =
635 ((mac_addr[5] + cnl->cnl_instance - 1) << 24)
636 | (mac_addr[4] << 16)
637 | (mac_addr[3] << 8)
638 | (mac_addr[2] << 0);
639 #if 0
640 aprint_error(": mac-address unknown\n");
641 return;
642 #endif
643 }
644
645 sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
646 sc->sc_hwlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
647
648 callout_init(&sc->sc_mii_callout, CALLOUT_MPSAFE);
649 callout_setfunc(&sc->sc_mii_callout, pq3etsec_mii_tick, sc);
650
651 /* Disable interrupts */
652 etsec_write(sc, IMASK, 0);
653
654 error = pq3etsec_rxq_attach(sc, &sc->sc_rxq, 0);
655 if (error) {
656 aprint_error(": failed to init rxq: %d\n", error);
657 goto fail_1;
658 }
659
660 error = pq3etsec_txq_attach(sc, &sc->sc_txq, 0);
661 if (error) {
662 aprint_error(": failed to init txq: %d\n", error);
663 goto fail_2;
664 }
665
666 error = pq3etsec_mapcache_create(sc, &sc->sc_rx_mapcache,
667 ETSEC_MAXRXMBUFS, MCLBYTES, ETSEC_NRXSEGS);
668 if (error) {
669 aprint_error(": failed to allocate rx dmamaps: %d\n", error);
670 goto fail_3;
671 }
672
673 error = pq3etsec_mapcache_create(sc, &sc->sc_tx_mapcache,
674 ETSEC_MAXTXMBUFS, MCLBYTES, ETSEC_NTXSEGS);
675 if (error) {
676 aprint_error(": failed to allocate tx dmamaps: %d\n", error);
677 goto fail_4;
678 }
679
680 sc->sc_tx_ih = intr_establish(cnl->cnl_intrs[0], IPL_VM, IST_ONCHIP,
681 pq3etsec_tx_intr, sc);
682 if (sc->sc_tx_ih == NULL) {
683 aprint_error(": failed to establish tx interrupt: %d\n",
684 cnl->cnl_intrs[0]);
685 goto fail_5;
686 }
687
688 sc->sc_rx_ih = intr_establish(cnl->cnl_intrs[1], IPL_VM, IST_ONCHIP,
689 pq3etsec_rx_intr, sc);
690 if (sc->sc_rx_ih == NULL) {
691 aprint_error(": failed to establish rx interrupt: %d\n",
692 cnl->cnl_intrs[1]);
693 goto fail_6;
694 }
695
696 sc->sc_error_ih = intr_establish(cnl->cnl_intrs[2], IPL_VM, IST_ONCHIP,
697 pq3etsec_error_intr, sc);
698 if (sc->sc_error_ih == NULL) {
699 aprint_error(": failed to establish error interrupt: %d\n",
700 cnl->cnl_intrs[2]);
701 goto fail_7;
702 }
703
704 int softint_flags = SOFTINT_NET;
705 #if !defined(MULTIPROCESSOR) || defined(NET_MPSAFE)
706 softint_flags |= SOFTINT_MPSAFE;
707 #endif /* !MULTIPROCESSOR || NET_MPSAFE */
708 sc->sc_soft_ih = softint_establish(softint_flags,
709 pq3etsec_soft_intr, sc);
710 if (sc->sc_soft_ih == NULL) {
711 aprint_error(": failed to establish soft interrupt\n");
712 goto fail_8;
713 }
714
715 /*
716 * If there was no MDIO
717 */
718 if (mdio == CPUNODECF_MDIO_DEFAULT) {
719 aprint_normal("\n");
720 cfdata_t mdio_cf = config_search(self, cna,
721 CFARG_SUBMATCH, pq3mdio_find,
722 CFARG_EOL);
723 if (mdio_cf != NULL) {
724 sc->sc_mdio_dev = config_attach(self, mdio_cf, cna, NULL);
725 }
726 } else {
727 sc->sc_mdio_dev = device_find_by_driver_unit("mdio", mdio);
728 if (sc->sc_mdio_dev == NULL) {
729 aprint_error(": failed to locate mdio device\n");
730 goto fail_9;
731 }
732 aprint_normal("\n");
733 }
734
735 etsec_write(sc, ATTR, ATTR_DEFAULT);
736 etsec_write(sc, ATTRELI, ATTRELI_DEFAULT);
737
738 /* Enable interrupt coalesing */
739 sc->sc_ic_rx_time = 768;
740 sc->sc_ic_rx_count = 16;
741 sc->sc_ic_tx_time = 768;
742 sc->sc_ic_tx_count = 16;
743 pq3etsec_set_ic_rx(sc);
744 pq3etsec_set_ic_tx(sc);
745
746 char enaddr[ETHER_ADDR_LEN] = {
747 [0] = sc->sc_macstnaddr2 >> 16,
748 [1] = sc->sc_macstnaddr2 >> 24,
749 [2] = sc->sc_macstnaddr1 >> 0,
750 [3] = sc->sc_macstnaddr1 >> 8,
751 [4] = sc->sc_macstnaddr1 >> 16,
752 [5] = sc->sc_macstnaddr1 >> 24,
753 };
754 aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
755 ether_sprintf(enaddr));
756
757 const char * const xname = device_xname(sc->sc_dev);
758 struct ethercom * const ec = &sc->sc_ec;
759 struct ifnet * const ifp = &ec->ec_if;
760
761 ec->ec_mii = mii;
762
763 mii->mii_ifp = ifp;
764 mii->mii_readreg = pq3mdio_mii_readreg;
765 mii->mii_writereg = pq3mdio_mii_writereg;
766 mii->mii_statchg = pq3etsec_mii_statchg;
767
768 ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
769
770 if (sc->sc_mdio_dev != NULL && sc->sc_phy_addr < 32) {
771 mii_attach(sc->sc_mdio_dev, mii, 0xffffffff,
772 sc->sc_phy_addr, MII_OFFSET_ANY, MIIF_DOPAUSE);
773
774 if (LIST_FIRST(&mii->mii_phys) == NULL) {
775 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE,
776 0, NULL);
777 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
778 } else {
779 callout_schedule(&sc->sc_mii_callout, hz);
780 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
781 }
782 } else {
783 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_1000_T | IFM_FDX,
784 0, NULL);
785 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_1000_T | IFM_FDX);
786 }
787
788 ec->ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING
789 | ETHERCAP_JUMBO_MTU;
790 ec->ec_capenable = ETHERCAP_VLAN_HWTAGGING;
791
792 strlcpy(ifp->if_xname, xname, IFNAMSIZ);
793 ifp->if_softc = sc;
794 ifp->if_capabilities = IFCAP_ETSEC;
795 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
796 ifp->if_ioctl = pq3etsec_ifioctl;
797 ifp->if_start = pq3etsec_ifstart;
798 ifp->if_watchdog = pq3etsec_ifwatchdog;
799 ifp->if_init = pq3etsec_ifinit;
800 ifp->if_stop = pq3etsec_ifstop;
801 IFQ_SET_READY(&ifp->if_snd);
802
803 /*
804 * Attach the interface.
805 */
806 error = if_initialize(ifp);
807 if (error != 0) {
808 aprint_error_dev(sc->sc_dev, "if_initialize failed(%d)\n",
809 error);
810 goto fail_10;
811 }
812 pq3etsec_sysctl_setup(NULL, sc);
813 if_attach(ifp);
814 if_deferred_start_init(ifp, NULL);
815 ether_ifattach(ifp, enaddr);
816
817 rnd_attach_source(&sc->rnd_source, xname, RND_TYPE_NET,
818 RND_FLAG_DEFAULT);
819
820 pq3etsec_ifstop(ifp, true);
821
822 evcnt_attach_dynamic(&sc->sc_ev_rx_stall, EVCNT_TYPE_MISC,
823 NULL, xname, "rx stall");
824 evcnt_attach_dynamic(&sc->sc_ev_tx_stall, EVCNT_TYPE_MISC,
825 NULL, xname, "tx stall");
826 evcnt_attach_dynamic(&sc->sc_ev_tx_intr, EVCNT_TYPE_INTR,
827 NULL, xname, "tx intr");
828 evcnt_attach_dynamic(&sc->sc_ev_rx_intr, EVCNT_TYPE_INTR,
829 NULL, xname, "rx intr");
830 evcnt_attach_dynamic(&sc->sc_ev_error_intr, EVCNT_TYPE_INTR,
831 NULL, xname, "error intr");
832 evcnt_attach_dynamic(&sc->sc_ev_soft_intr, EVCNT_TYPE_INTR,
833 NULL, xname, "soft intr");
834 evcnt_attach_dynamic(&sc->sc_ev_tx_pause, EVCNT_TYPE_MISC,
835 NULL, xname, "tx pause");
836 evcnt_attach_dynamic(&sc->sc_ev_rx_pause, EVCNT_TYPE_MISC,
837 NULL, xname, "rx pause");
838 evcnt_attach_dynamic(&sc->sc_ev_mii_ticks, EVCNT_TYPE_MISC,
839 NULL, xname, "mii ticks");
840 return;
841
842 fail_10:
843 ifmedia_removeall(&mii->mii_media);
844 mii_detach(mii, sc->sc_phy_addr, MII_OFFSET_ANY);
845 fail_9:
846 softint_disestablish(sc->sc_soft_ih);
847 fail_8:
848 intr_disestablish(sc->sc_error_ih);
849 fail_7:
850 intr_disestablish(sc->sc_rx_ih);
851 fail_6:
852 intr_disestablish(sc->sc_tx_ih);
853 fail_5:
854 pq3etsec_mapcache_destroy(sc, sc->sc_tx_mapcache);
855 fail_4:
856 pq3etsec_mapcache_destroy(sc, sc->sc_rx_mapcache);
857 fail_3:
858 #if 0 /* notyet */
859 pq3etsec_txq_detach(sc);
860 #endif
861 fail_2:
862 #if 0 /* notyet */
863 pq3etsec_rxq_detach(sc);
864 #endif
865 fail_1:
866 callout_destroy(&sc->sc_mii_callout);
867 mutex_obj_free(sc->sc_lock);
868 mutex_obj_free(sc->sc_hwlock);
869 bus_space_unmap(sc->sc_bst, sc->sc_bsh, cnl->cnl_size);
870 }
871
872 static uint64_t
873 pq3etsec_macaddr_create(const uint8_t *lladdr)
874 {
875 uint64_t macaddr = 0;
876
877 lladdr += ETHER_ADDR_LEN;
878 for (u_int i = ETHER_ADDR_LEN; i-- > 0; ) {
879 macaddr = (macaddr << 8) | *--lladdr;
880 }
881 return macaddr << 16;
882 }
883
884 static int
885 pq3etsec_ifinit(struct ifnet *ifp)
886 {
887 struct pq3etsec_softc * const sc = ifp->if_softc;
888 int error = 0;
889
890 sc->sc_maxfrm = uimax(ifp->if_mtu + 32, MCLBYTES);
891 if (ifp->if_mtu > ETHERMTU_JUMBO)
892 return error;
893
894 KASSERT(ifp->if_flags & IFF_UP);
895
896 /*
897 * Stop the interface (steps 1 to 4 in the Soft Reset and
898 * Reconfigurating Procedure.
899 */
900 pq3etsec_ifstop(ifp, 0);
901
902 /*
903 * If our frame size has changed (or it's our first time through)
904 * destroy the existing transmit mapcache.
905 */
906 if (sc->sc_tx_mapcache != NULL
907 && sc->sc_maxfrm != sc->sc_tx_mapcache->dmc_maxmapsize) {
908 pq3etsec_mapcache_destroy(sc, sc->sc_tx_mapcache);
909 sc->sc_tx_mapcache = NULL;
910 }
911
912 if (sc->sc_tx_mapcache == NULL) {
913 error = pq3etsec_mapcache_create(sc, &sc->sc_tx_mapcache,
914 ETSEC_MAXTXMBUFS, sc->sc_maxfrm, ETSEC_NTXSEGS);
915 if (error)
916 return error;
917 }
918
919 sc->sc_ev_mii_ticks.ev_count++;
920 mii_tick(&sc->sc_mii);
921
922 if (ifp->if_flags & IFF_PROMISC) {
923 sc->sc_rctrl |= RCTRL_PROM;
924 } else {
925 sc->sc_rctrl &= ~RCTRL_PROM;
926 }
927
928 uint32_t rctrl_prsdep = 0;
929 sc->sc_rctrl &=
930 ~(RCTRL_IPCSEN | RCTRL_TUCSEN | RCTRL_VLEX | RCTRL_PRSDEP);
931 if (VLAN_ATTACHED(&sc->sc_ec)) {
932 sc->sc_rctrl |= RCTRL_VLEX;
933 rctrl_prsdep = RCTRL_PRSDEP_L2;
934 }
935 if (ifp->if_capenable & IFCAP_RCTRL_IPCSEN) {
936 sc->sc_rctrl |= RCTRL_IPCSEN;
937 rctrl_prsdep = RCTRL_PRSDEP_L3;
938 }
939 if (ifp->if_capenable & IFCAP_RCTRL_TUCSEN) {
940 sc->sc_rctrl |= RCTRL_TUCSEN;
941 rctrl_prsdep = RCTRL_PRSDEP_L4;
942 }
943 sc->sc_rctrl |= rctrl_prsdep;
944 #if 0
945 if (sc->sc_rctrl
946 & (RCTRL_IPCSEN | RCTRL_TUCSEN | RCTRL_VLEX | RCTRL_PRSDEP))
947 aprint_normal_dev(sc->sc_dev,
948 "rctrl=%#x ipcsen=%"PRIuMAX" tucsen=%"PRIuMAX" vlex=%"PRIuMAX" prsdep=%"PRIuMAX"\n",
949 sc->sc_rctrl,
950 __SHIFTOUT(sc->sc_rctrl, RCTRL_IPCSEN),
951 __SHIFTOUT(sc->sc_rctrl, RCTRL_TUCSEN),
952 __SHIFTOUT(sc->sc_rctrl, RCTRL_VLEX),
953 __SHIFTOUT(sc->sc_rctrl, RCTRL_PRSDEP));
954 #endif
955
956 sc->sc_tctrl &= ~(TCTRL_IPCSEN | TCTRL_TUCSEN | TCTRL_VLINS);
957 if (VLAN_ATTACHED(&sc->sc_ec)) /* is this really true */
958 sc->sc_tctrl |= TCTRL_VLINS;
959 if (ifp->if_capenable & IFCAP_TCTRL_IPCSEN)
960 sc->sc_tctrl |= TCTRL_IPCSEN;
961 if (ifp->if_capenable & IFCAP_TCTRL_TUCSEN)
962 sc->sc_tctrl |= TCTRL_TUCSEN;
963 #if 0
964 if (sc->sc_tctrl & (TCTRL_IPCSEN | TCTRL_TUCSEN | TCTRL_VLINS))
965 aprint_normal_dev(sc->sc_dev,
966 "tctrl=%#x ipcsen=%"PRIuMAX" tucsen=%"PRIuMAX" vlins=%"PRIuMAX"\n",
967 sc->sc_tctrl,
968 __SHIFTOUT(sc->sc_tctrl, TCTRL_IPCSEN),
969 __SHIFTOUT(sc->sc_tctrl, TCTRL_TUCSEN),
970 __SHIFTOUT(sc->sc_tctrl, TCTRL_VLINS));
971 #endif
972
973 sc->sc_maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
974
975 const uint64_t macstnaddr =
976 pq3etsec_macaddr_create(CLLADDR(ifp->if_sadl));
977
978 sc->sc_imask = IEVENT_DPE;
979
980 /* 5. Load TDBPH, TBASEH, TBASE0-TBASE7 with new Tx BD pointers */
981 pq3etsec_rxq_reset(sc, &sc->sc_rxq);
982 pq3etsec_rxq_produce(sc, &sc->sc_rxq); /* fill with rx buffers */
983
984 /* 6. Load RDBPH, RBASEH, RBASE0-RBASE7 with new Rx BD pointers */
985 pq3etsec_txq_reset(sc, &sc->sc_txq);
986
987 /* 7. Setup other MAC registers (MACCFG2, MAXFRM, etc.) */
988 KASSERT(MACCFG2_PADCRC & sc->sc_maccfg2);
989 etsec_write(sc, MAXFRM, sc->sc_maxfrm);
990 etsec_write(sc, MACSTNADDR1, (uint32_t)(macstnaddr >> 32));
991 etsec_write(sc, MACSTNADDR2, (uint32_t)(macstnaddr >> 0));
992 etsec_write(sc, MACCFG1, sc->sc_maccfg1);
993 etsec_write(sc, MACCFG2, sc->sc_maccfg2);
994 etsec_write(sc, ECNTRL, sc->sc_ecntrl);
995
996 /* 8. Setup group address hash table (GADDR0-GADDR15) */
997 pq3etsec_mc_setup(sc);
998
999 /* 9. Setup receive frame filer table (via RQFAR, RQFCR, and RQFPR) */
1000 etsec_write(sc, MRBLR, MCLBYTES);
1001
1002 /* 10. Setup WWR, WOP, TOD bits in DMACTRL register */
1003 sc->sc_dmactrl |= DMACTRL_DEFAULT;
1004 etsec_write(sc, DMACTRL, sc->sc_dmactrl);
1005
1006 /* 11. Enable transmit queues in TQUEUE, and ensure that the transmit scheduling mode is correctly set in TCTRL. */
1007 etsec_write(sc, TQUEUE, TQUEUE_EN0);
1008 sc->sc_imask |= IEVENT_TXF | IEVENT_TXE | IEVENT_TXC;
1009
1010 etsec_write(sc, TCTRL, sc->sc_tctrl); /* for TOE stuff */
1011
1012 /* 12. Enable receive queues in RQUEUE, */
1013 etsec_write(sc, RQUEUE, RQUEUE_EN0 | RQUEUE_EX0);
1014 sc->sc_imask |= IEVENT_RXF | IEVENT_BSY | IEVENT_RXC;
1015
1016 /* and optionally set TOE functionality in RCTRL. */
1017 etsec_write(sc, RCTRL, sc->sc_rctrl);
1018 sc->sc_rx_adjlen = __SHIFTOUT(sc->sc_rctrl, RCTRL_PAL);
1019 if ((sc->sc_rctrl & RCTRL_PRSDEP) != RCTRL_PRSDEP_OFF)
1020 sc->sc_rx_adjlen += sizeof(struct rxfcb);
1021
1022 /* 13. Clear THLT and TXF bits in TSTAT register by writing 1 to them */
1023 etsec_write(sc, TSTAT, TSTAT_THLT | TSTAT_TXF);
1024
1025 /* 14. Clear QHLT and RXF bits in RSTAT register by writing 1 to them.*/
1026 etsec_write(sc, RSTAT, RSTAT_QHLT | RSTAT_RXF);
1027
1028 /* 15. Clear GRS/GTS bits in DMACTRL (do not change other bits) */
1029 sc->sc_dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
1030 etsec_write(sc, DMACTRL, sc->sc_dmactrl);
1031
1032 /* 16. Enable Tx_EN/Rx_EN in MACCFG1 register */
1033 etsec_write(sc, MACCFG1, sc->sc_maccfg1 | MACCFG1_TX_EN|MACCFG1_RX_EN);
1034 etsec_write(sc, MACCFG1, sc->sc_maccfg1 | MACCFG1_TX_EN|MACCFG1_RX_EN);
1035
1036 sc->sc_soft_flags = 0;
1037
1038 etsec_write(sc, IMASK, sc->sc_imask);
1039
1040 ifp->if_flags |= IFF_RUNNING;
1041
1042 return error;
1043 }
1044
1045 static void
1046 pq3etsec_ifstop(struct ifnet *ifp, int disable)
1047 {
1048 struct pq3etsec_softc * const sc = ifp->if_softc;
1049
1050 KASSERT(!cpu_intr_p());
1051 const uint32_t imask_gsc_mask = IEVENT_GTSC | IEVENT_GRSC;
1052 /*
1053 * Clear the GTSC and GRSC from the interrupt mask until
1054 * we are ready for them. Then clear them from IEVENT,
1055 * request the graceful shutdown, and then enable the
1056 * GTSC and GRSC bits in the mask. This should cause the
1057 * error interrupt to fire which will issue a wakeup to
1058 * allow us to resume.
1059 */
1060
1061 /*
1062 * 1. Set GRS/GTS bits in DMACTRL register
1063 */
1064 sc->sc_dmactrl |= DMACTRL_GRS | DMACTRL_GTS;
1065 etsec_write(sc, IMASK, sc->sc_imask & ~imask_gsc_mask);
1066 etsec_write(sc, IEVENT, imask_gsc_mask);
1067 etsec_write(sc, DMACTRL, sc->sc_dmactrl);
1068
1069 if (etsec_read(sc, MACCFG1) & (MACCFG1_TX_EN | MACCFG1_RX_EN)) {
1070 /*
1071 * 2. Poll GRSC/GTSC bits in IEVENT register until both are set
1072 */
1073 etsec_write(sc, IMASK, sc->sc_imask | imask_gsc_mask);
1074
1075 u_int timo = 1000;
1076 uint32_t ievent = etsec_read(sc, IEVENT);
1077 while ((ievent & imask_gsc_mask) != imask_gsc_mask) {
1078 if (--timo == 0) {
1079 aprint_error_dev(sc->sc_dev,
1080 "WARNING: "
1081 "request to stop failed (IEVENT=%#x)\n",
1082 ievent);
1083 break;
1084 }
1085 delay(10);
1086 ievent = etsec_read(sc, IEVENT);
1087 }
1088 }
1089
1090 /*
1091 * Now reset the controller.
1092 *
1093 * 3. Set SOFT_RESET bit in MACCFG1 register
1094 * 4. Clear SOFT_RESET bit in MACCFG1 register
1095 */
1096 etsec_write(sc, MACCFG1, MACCFG1_SOFT_RESET);
1097 etsec_write(sc, MACCFG1, 0);
1098 etsec_write(sc, IMASK, 0);
1099 etsec_write(sc, IEVENT, ~0);
1100 sc->sc_imask = 0;
1101 ifp->if_flags &= ~IFF_RUNNING;
1102
1103 uint32_t tbipa = etsec_read(sc, TBIPA);
1104 if (tbipa == sc->sc_phy_addr) {
1105 aprint_normal_dev(sc->sc_dev, "relocating TBI\n");
1106 etsec_write(sc, TBIPA, 0x1f);
1107 }
1108 uint32_t miimcfg = etsec_read(sc, MIIMCFG);
1109 etsec_write(sc, MIIMCFG, MIIMCFG_RESET);
1110 etsec_write(sc, MIIMCFG, miimcfg);
1111
1112 /*
1113 * Let's consume any remaing transmitted packets. And if we are
1114 * disabling the interface, purge ourselves of any untransmitted
1115 * packets. But don't consume any received packets, just drop them.
1116 * If we aren't disabling the interface, save the mbufs in the
1117 * receive queue for reuse.
1118 */
1119 pq3etsec_rxq_purge(sc, &sc->sc_rxq, disable);
1120 pq3etsec_txq_consume(sc, &sc->sc_txq);
1121 if (disable) {
1122 pq3etsec_txq_purge(sc, &sc->sc_txq);
1123 IFQ_PURGE(&ifp->if_snd);
1124 }
1125 }
1126
1127 static void
1128 pq3etsec_ifwatchdog(struct ifnet *ifp)
1129 {
1130 }
1131
1132 static void
1133 pq3etsec_mc_setup(
1134 struct pq3etsec_softc *sc)
1135 {
1136 struct ethercom * const ec = &sc->sc_ec;
1137 struct ifnet * const ifp = &sc->sc_if;
1138 struct ether_multi *enm;
1139 struct ether_multistep step;
1140 uint32_t *gaddr = sc->sc_gaddr + ((sc->sc_rctrl & RCTRL_GHTX) ? 0 : 8);
1141 const uint32_t crc_shift = 32 - ((sc->sc_rctrl & RCTRL_GHTX) ? 9 : 8);
1142
1143 memset(sc->sc_gaddr, 0, sizeof(sc->sc_gaddr));
1144 memset(sc->sc_macaddrs, 0, sizeof(sc->sc_macaddrs));
1145
1146 ifp->if_flags &= ~IFF_ALLMULTI;
1147
1148 ETHER_LOCK(ec);
1149 ETHER_FIRST_MULTI(step, ec, enm);
1150 for (u_int i = 0; enm != NULL; ) {
1151 const char *addr = enm->enm_addrlo;
1152 if (memcmp(addr, enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
1153 ifp->if_flags |= IFF_ALLMULTI;
1154 memset(gaddr, 0xff, 32 << (crc_shift & 1));
1155 memset(sc->sc_macaddrs, 0, sizeof(sc->sc_macaddrs));
1156 break;
1157 }
1158 if ((sc->sc_rctrl & RCTRL_EMEN)
1159 && i < __arraycount(sc->sc_macaddrs)) {
1160 sc->sc_macaddrs[i++] = pq3etsec_macaddr_create(addr);
1161 } else {
1162 uint32_t crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
1163 #if 0
1164 printf("%s: %s: crc=%#x: %#x: [%u,%u]=%#x\n", __func__,
1165 ether_sprintf(addr), crc,
1166 crc >> crc_shift,
1167 crc >> (crc_shift + 5),
1168 (crc >> crc_shift) & 31,
1169 1 << (((crc >> crc_shift) & 31) ^ 31));
1170 #endif
1171 /*
1172 * The documentation doesn't completely follow PowerPC
1173 * bit order. The BE crc32 (H) for 01:00:5E:00:00:01
1174 * is 0x7fa32d9b. By empirical testing, the
1175 * corresponding hash bit is word 3, bit 31 (ppc bit
1176 * order). Since 3 << 31 | 31 is 0x7f, we deduce
1177 * H[0:2] selects the register while H[3:7] selects
1178 * the bit (ppc bit order).
1179 */
1180 crc >>= crc_shift;
1181 gaddr[crc / 32] |= 1 << ((crc & 31) ^ 31);
1182 }
1183 ETHER_NEXT_MULTI(step, enm);
1184 }
1185 ETHER_UNLOCK(ec);
1186 for (u_int i = 0; i < 8; i++) {
1187 etsec_write(sc, IGADDR(i), sc->sc_gaddr[i]);
1188 etsec_write(sc, GADDR(i), sc->sc_gaddr[i+8]);
1189 #if 0
1190 if (sc->sc_gaddr[i] || sc->sc_gaddr[i+8])
1191 printf("%s: IGADDR%u(%#x)=%#x GADDR%u(%#x)=%#x\n", __func__,
1192 i, IGADDR(i), etsec_read(sc, IGADDR(i)),
1193 i, GADDR(i), etsec_read(sc, GADDR(i)));
1194 #endif
1195 }
1196 for (u_int i = 0; i < __arraycount(sc->sc_macaddrs); i++) {
1197 uint64_t macaddr = sc->sc_macaddrs[i];
1198 etsec_write(sc, MACnADDR1(i), (uint32_t)(macaddr >> 32));
1199 etsec_write(sc, MACnADDR2(i), (uint32_t)(macaddr >> 0));
1200 #if 0
1201 if (macaddr)
1202 printf("%s: MAC%02uADDR2(%08x)=%#x MAC%02uADDR2(%#x)=%08x\n", __func__,
1203 i+1, MACnADDR1(i), etsec_read(sc, MACnADDR1(i)),
1204 i+1, MACnADDR2(i), etsec_read(sc, MACnADDR2(i)));
1205 #endif
1206 }
1207 }
1208
1209 static int
1210 pq3etsec_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
1211 {
1212 struct pq3etsec_softc *sc = ifp->if_softc;
1213 struct ifreq * const ifr = data;
1214 const int s = splnet();
1215 int error;
1216
1217 switch (cmd) {
1218 case SIOCSIFMEDIA:
1219 /* Flow control requires full-duplex mode. */
1220 if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
1221 (ifr->ifr_media & IFM_FDX) == 0)
1222 ifr->ifr_media &= ~IFM_ETH_FMASK;
1223 if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
1224 if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
1225 /* We can do both TXPAUSE and RXPAUSE. */
1226 ifr->ifr_media |=
1227 IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
1228 }
1229 }
1230 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1231 break;
1232
1233 default:
1234 error = ether_ioctl(ifp, cmd, data);
1235 if (error != ENETRESET)
1236 break;
1237
1238 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
1239 error = 0;
1240 if (ifp->if_flags & IFF_RUNNING)
1241 pq3etsec_mc_setup(sc);
1242 break;
1243 }
1244 error = pq3etsec_ifinit(ifp);
1245 break;
1246 }
1247
1248 splx(s);
1249 return error;
1250 }
1251
1252 static void
1253 pq3etsec_rxq_desc_presync(
1254 struct pq3etsec_softc *sc,
1255 struct pq3etsec_rxqueue *rxq,
1256 volatile struct rxbd *rxbd,
1257 size_t count)
1258 {
1259 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_descmap,
1260 (rxbd - rxq->rxq_first) * sizeof(*rxbd), count * sizeof(*rxbd),
1261 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1262 }
1263
1264 static void
1265 pq3etsec_rxq_desc_postsync(
1266 struct pq3etsec_softc *sc,
1267 struct pq3etsec_rxqueue *rxq,
1268 volatile struct rxbd *rxbd,
1269 size_t count)
1270 {
1271 bus_dmamap_sync(sc->sc_dmat, rxq->rxq_descmap,
1272 (rxbd - rxq->rxq_first) * sizeof(*rxbd), count * sizeof(*rxbd),
1273 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1274 }
1275
1276 static void
1277 pq3etsec_txq_desc_presync(
1278 struct pq3etsec_softc *sc,
1279 struct pq3etsec_txqueue *txq,
1280 volatile struct txbd *txbd,
1281 size_t count)
1282 {
1283 bus_dmamap_sync(sc->sc_dmat, txq->txq_descmap,
1284 (txbd - txq->txq_first) * sizeof(*txbd), count * sizeof(*txbd),
1285 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1286 }
1287
1288 static void
1289 pq3etsec_txq_desc_postsync(
1290 struct pq3etsec_softc *sc,
1291 struct pq3etsec_txqueue *txq,
1292 volatile struct txbd *txbd,
1293 size_t count)
1294 {
1295 bus_dmamap_sync(sc->sc_dmat, txq->txq_descmap,
1296 (txbd - txq->txq_first) * sizeof(*txbd), count * sizeof(*txbd),
1297 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1298 }
1299
1300 static bus_dmamap_t
1301 pq3etsec_mapcache_get(
1302 struct pq3etsec_softc *sc,
1303 struct pq3etsec_mapcache *dmc)
1304 {
1305 KASSERT(dmc->dmc_nmaps > 0);
1306 KASSERT(dmc->dmc_maps[dmc->dmc_nmaps-1] != NULL);
1307 return dmc->dmc_maps[--dmc->dmc_nmaps];
1308 }
1309
1310 static void
1311 pq3etsec_mapcache_put(
1312 struct pq3etsec_softc *sc,
1313 struct pq3etsec_mapcache *dmc,
1314 bus_dmamap_t map)
1315 {
1316 KASSERT(map != NULL);
1317 KASSERT(dmc->dmc_nmaps < dmc->dmc_maxmaps);
1318 dmc->dmc_maps[dmc->dmc_nmaps++] = map;
1319 }
1320
1321 static void
1322 pq3etsec_mapcache_destroy(
1323 struct pq3etsec_softc *sc,
1324 struct pq3etsec_mapcache *dmc)
1325 {
1326 const size_t dmc_size =
1327 offsetof(struct pq3etsec_mapcache, dmc_maps[dmc->dmc_maxmaps]);
1328
1329 for (u_int i = 0; i < dmc->dmc_maxmaps; i++) {
1330 bus_dmamap_destroy(sc->sc_dmat, dmc->dmc_maps[i]);
1331 }
1332 kmem_intr_free(dmc, dmc_size);
1333 }
1334
1335 static int
1336 pq3etsec_mapcache_create(
1337 struct pq3etsec_softc *sc,
1338 struct pq3etsec_mapcache **dmc_p,
1339 size_t maxmaps,
1340 size_t maxmapsize,
1341 size_t maxseg)
1342 {
1343 const size_t dmc_size =
1344 offsetof(struct pq3etsec_mapcache, dmc_maps[maxmaps]);
1345 struct pq3etsec_mapcache * const dmc =
1346 kmem_intr_zalloc(dmc_size, KM_NOSLEEP);
1347
1348 dmc->dmc_maxmaps = maxmaps;
1349 dmc->dmc_nmaps = maxmaps;
1350 dmc->dmc_maxmapsize = maxmapsize;
1351 dmc->dmc_maxseg = maxseg;
1352
1353 for (u_int i = 0; i < maxmaps; i++) {
1354 int error = bus_dmamap_create(sc->sc_dmat, dmc->dmc_maxmapsize,
1355 dmc->dmc_maxseg, dmc->dmc_maxmapsize, 0,
1356 BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &dmc->dmc_maps[i]);
1357 if (error) {
1358 aprint_error_dev(sc->sc_dev,
1359 "failed to creat dma map cache "
1360 "entry %u of %zu: %d\n",
1361 i, maxmaps, error);
1362 while (i-- > 0) {
1363 bus_dmamap_destroy(sc->sc_dmat,
1364 dmc->dmc_maps[i]);
1365 }
1366 kmem_intr_free(dmc, dmc_size);
1367 return error;
1368 }
1369 KASSERT(dmc->dmc_maps[i] != NULL);
1370 }
1371
1372 *dmc_p = dmc;
1373
1374 return 0;
1375 }
1376
1377 #if 0
1378 static void
1379 pq3etsec_dmamem_free(
1380 bus_dma_tag_t dmat,
1381 size_t map_size,
1382 bus_dma_segment_t *seg,
1383 bus_dmamap_t map,
1384 void *kvap)
1385 {
1386 bus_dmamap_destroy(dmat, map);
1387 bus_dmamem_unmap(dmat, kvap, map_size);
1388 bus_dmamem_free(dmat, seg, 1);
1389 }
1390 #endif
1391
1392 static int
1393 pq3etsec_dmamem_alloc(
1394 bus_dma_tag_t dmat,
1395 size_t map_size,
1396 bus_dma_segment_t *seg,
1397 bus_dmamap_t *map,
1398 void **kvap)
1399 {
1400 int error;
1401 int nseg;
1402
1403 *kvap = NULL;
1404 *map = NULL;
1405
1406 error = bus_dmamem_alloc(dmat, map_size, PAGE_SIZE, 0,
1407 seg, 1, &nseg, 0);
1408 if (error)
1409 return error;
1410
1411 KASSERT(nseg == 1);
1412
1413 error = bus_dmamem_map(dmat, seg, nseg, map_size, (void **)kvap,
1414 BUS_DMA_COHERENT);
1415 if (error == 0) {
1416 error = bus_dmamap_create(dmat, map_size, 1, map_size, 0, 0,
1417 map);
1418 if (error == 0) {
1419 error = bus_dmamap_load(dmat, *map, *kvap, map_size,
1420 NULL, 0);
1421 if (error == 0)
1422 return 0;
1423 bus_dmamap_destroy(dmat, *map);
1424 *map = NULL;
1425 }
1426 bus_dmamem_unmap(dmat, *kvap, map_size);
1427 *kvap = NULL;
1428 }
1429 bus_dmamem_free(dmat, seg, nseg);
1430 return 0;
1431 }
1432
1433 static struct mbuf *
1434 pq3etsec_rx_buf_alloc(
1435 struct pq3etsec_softc *sc)
1436 {
1437 struct mbuf *m = m_gethdr(M_DONTWAIT, MT_DATA);
1438 if (m == NULL) {
1439 printf("%s:%d: %s\n", __func__, __LINE__, "m_gethdr");
1440 return NULL;
1441 }
1442 MCLGET(m, M_DONTWAIT);
1443 if ((m->m_flags & M_EXT) == 0) {
1444 printf("%s:%d: %s\n", __func__, __LINE__, "MCLGET");
1445 m_freem(m);
1446 return NULL;
1447 }
1448 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
1449
1450 bus_dmamap_t map = pq3etsec_mapcache_get(sc, sc->sc_rx_mapcache);
1451 if (map == NULL) {
1452 printf("%s:%d: %s\n", __func__, __LINE__, "map get");
1453 m_freem(m);
1454 return NULL;
1455 }
1456 M_SETCTX(m, map);
1457 m->m_len = m->m_pkthdr.len = MCLBYTES;
1458 int error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
1459 BUS_DMA_READ | BUS_DMA_NOWAIT);
1460 if (error) {
1461 aprint_error_dev(sc->sc_dev, "fail to load rx dmamap: %d\n",
1462 error);
1463 M_SETCTX(m, NULL);
1464 m_freem(m);
1465 pq3etsec_mapcache_put(sc, sc->sc_rx_mapcache, map);
1466 return NULL;
1467 }
1468 KASSERT(map->dm_mapsize == MCLBYTES);
1469 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1470 BUS_DMASYNC_PREREAD);
1471
1472 return m;
1473 }
1474
1475 static void
1476 pq3etsec_rx_map_unload(
1477 struct pq3etsec_softc *sc,
1478 struct mbuf *m)
1479 {
1480 KASSERT(m);
1481 for (; m != NULL; m = m->m_next) {
1482 bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
1483 KASSERT(map);
1484 KASSERT(map->dm_mapsize == MCLBYTES);
1485 bus_dmamap_sync(sc->sc_dmat, map, 0, m->m_len,
1486 BUS_DMASYNC_POSTREAD);
1487 bus_dmamap_unload(sc->sc_dmat, map);
1488 pq3etsec_mapcache_put(sc, sc->sc_rx_mapcache, map);
1489 M_SETCTX(m, NULL);
1490 }
1491 }
1492
1493 static bool
1494 pq3etsec_rxq_produce(
1495 struct pq3etsec_softc *sc,
1496 struct pq3etsec_rxqueue *rxq)
1497 {
1498 volatile struct rxbd *producer = rxq->rxq_producer;
1499 #if 0
1500 size_t inuse = rxq->rxq_inuse;
1501 #endif
1502 while (rxq->rxq_inuse < rxq->rxq_threshold) {
1503 struct mbuf *m;
1504 IF_DEQUEUE(&sc->sc_rx_bufcache, m);
1505 if (m == NULL) {
1506 m = pq3etsec_rx_buf_alloc(sc);
1507 if (m == NULL) {
1508 printf("%s: pq3etsec_rx_buf_alloc failed\n", __func__);
1509 break;
1510 }
1511 }
1512 bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
1513 KASSERT(map);
1514
1515 #ifdef ETSEC_DEBUG
1516 KASSERT(rxq->rxq_mbufs[producer-rxq->rxq_first] == NULL);
1517 rxq->rxq_mbufs[producer-rxq->rxq_first] = m;
1518 #endif
1519
1520 /* rxbd_len is write-only by the ETSEC */
1521 producer->rxbd_bufptr = map->dm_segs[0].ds_addr;
1522 membar_producer();
1523 producer->rxbd_flags |= RXBD_E;
1524 if (__predict_false(rxq->rxq_mhead == NULL)) {
1525 KASSERT(producer == rxq->rxq_consumer);
1526 rxq->rxq_mconsumer = m;
1527 }
1528 *rxq->rxq_mtail = m;
1529 rxq->rxq_mtail = &m->m_next;
1530 m->m_len = MCLBYTES;
1531 m->m_next = NULL;
1532 rxq->rxq_inuse++;
1533 if (++producer == rxq->rxq_last) {
1534 membar_producer();
1535 pq3etsec_rxq_desc_presync(sc, rxq, rxq->rxq_producer,
1536 rxq->rxq_last - rxq->rxq_producer);
1537 producer = rxq->rxq_producer = rxq->rxq_first;
1538 }
1539 }
1540 if (producer != rxq->rxq_producer) {
1541 membar_producer();
1542 pq3etsec_rxq_desc_presync(sc, rxq, rxq->rxq_producer,
1543 producer - rxq->rxq_producer);
1544 rxq->rxq_producer = producer;
1545 }
1546 uint32_t qhlt = etsec_read(sc, RSTAT) & RSTAT_QHLT;
1547 if (qhlt) {
1548 KASSERT(qhlt & rxq->rxq_qmask);
1549 sc->sc_ev_rx_stall.ev_count++;
1550 etsec_write(sc, RSTAT, RSTAT_QHLT & rxq->rxq_qmask);
1551 }
1552 #if 0
1553 aprint_normal_dev(sc->sc_dev,
1554 "%s: buffers inuse went from %zu to %zu\n",
1555 __func__, inuse, rxq->rxq_inuse);
1556 #endif
1557 return true;
1558 }
1559
1560 static bool
1561 pq3etsec_rx_offload(
1562 struct pq3etsec_softc *sc,
1563 struct mbuf *m,
1564 const struct rxfcb *fcb)
1565 {
1566 if (fcb->rxfcb_flags & RXFCB_VLN) {
1567 vlan_set_tag(m, fcb->rxfcb_vlctl);
1568 }
1569 if ((fcb->rxfcb_flags & RXFCB_IP) == 0
1570 || (fcb->rxfcb_flags & (RXFCB_CIP | RXFCB_CTU)) == 0)
1571 return true;
1572 int csum_flags = 0;
1573 if ((fcb->rxfcb_flags & (RXFCB_IP6 | RXFCB_CIP)) == RXFCB_CIP) {
1574 csum_flags |= M_CSUM_IPv4;
1575 if (fcb->rxfcb_flags & RXFCB_EIP)
1576 csum_flags |= M_CSUM_IPv4_BAD;
1577 }
1578 if ((fcb->rxfcb_flags & RXFCB_CTU) == RXFCB_CTU) {
1579 int ipv_flags;
1580 if (fcb->rxfcb_flags & RXFCB_IP6)
1581 ipv_flags = M_CSUM_TCPv6 | M_CSUM_UDPv6;
1582 else
1583 ipv_flags = M_CSUM_TCPv4 | M_CSUM_UDPv4;
1584 if (fcb->rxfcb_pro == IPPROTO_TCP) {
1585 csum_flags |= (M_CSUM_TCPv4 |M_CSUM_TCPv6) & ipv_flags;
1586 } else {
1587 csum_flags |= (M_CSUM_UDPv4 |M_CSUM_UDPv6) & ipv_flags;
1588 }
1589 if (fcb->rxfcb_flags & RXFCB_ETU)
1590 csum_flags |= M_CSUM_TCP_UDP_BAD;
1591 }
1592
1593 m->m_pkthdr.csum_flags = csum_flags;
1594 return true;
1595 }
1596
1597 static void
1598 pq3etsec_rx_input(
1599 struct pq3etsec_softc *sc,
1600 struct mbuf *m,
1601 uint16_t rxbd_flags)
1602 {
1603 struct ifnet * const ifp = &sc->sc_if;
1604
1605 pq3etsec_rx_map_unload(sc, m);
1606
1607 if ((sc->sc_rctrl & RCTRL_PRSDEP) != RCTRL_PRSDEP_OFF) {
1608 struct rxfcb fcb = *mtod(m, struct rxfcb *);
1609 if (!pq3etsec_rx_offload(sc, m, &fcb))
1610 return;
1611 }
1612 m_adj(m, sc->sc_rx_adjlen);
1613
1614 if (rxbd_flags & RXBD_M)
1615 m->m_flags |= M_PROMISC;
1616 if (rxbd_flags & RXBD_BC)
1617 m->m_flags |= M_BCAST;
1618 if (rxbd_flags & RXBD_MC)
1619 m->m_flags |= M_MCAST;
1620 m->m_flags |= M_HASFCS;
1621 m_set_rcvif(m, &sc->sc_if);
1622
1623 /*
1624 * Let's give it to the network subsystm to deal with.
1625 */
1626 if_percpuq_enqueue(ifp->if_percpuq, m);
1627 }
1628
1629 static void
1630 pq3etsec_rxq_consume(
1631 struct pq3etsec_softc *sc,
1632 struct pq3etsec_rxqueue *rxq)
1633 {
1634 struct ifnet * const ifp = &sc->sc_if;
1635 volatile struct rxbd *consumer = rxq->rxq_consumer;
1636 size_t rxconsumed = 0;
1637
1638 etsec_write(sc, RSTAT, RSTAT_RXF & rxq->rxq_qmask);
1639
1640 for (;;) {
1641 if (consumer == rxq->rxq_producer) {
1642 rxq->rxq_consumer = consumer;
1643 rxq->rxq_inuse -= rxconsumed;
1644 KASSERT(rxq->rxq_inuse == 0);
1645 return;
1646 }
1647 pq3etsec_rxq_desc_postsync(sc, rxq, consumer, 1);
1648 const uint16_t rxbd_flags = consumer->rxbd_flags;
1649 if (rxbd_flags & RXBD_E) {
1650 rxq->rxq_consumer = consumer;
1651 rxq->rxq_inuse -= rxconsumed;
1652 return;
1653 }
1654 KASSERT(rxq->rxq_mconsumer != NULL);
1655 #ifdef ETSEC_DEBUG
1656 KASSERT(rxq->rxq_mbufs[consumer - rxq->rxq_first] == rxq->rxq_mconsumer);
1657 #endif
1658 #if 0
1659 printf("%s: rxdb[%u]: flags=%#x len=%#x: %08x %08x %08x %08x\n",
1660 __func__,
1661 consumer - rxq->rxq_first, rxbd_flags, consumer->rxbd_len,
1662 mtod(rxq->rxq_mconsumer, int *)[0],
1663 mtod(rxq->rxq_mconsumer, int *)[1],
1664 mtod(rxq->rxq_mconsumer, int *)[2],
1665 mtod(rxq->rxq_mconsumer, int *)[3]);
1666 #endif
1667 /*
1668 * We own this packet again. Clear all flags except wrap.
1669 */
1670 rxconsumed++;
1671 consumer->rxbd_flags = rxbd_flags & (RXBD_W | RXBD_I);
1672
1673 /*
1674 * If this descriptor has the LAST bit set and no errors,
1675 * it's a valid input packet.
1676 */
1677 if ((rxbd_flags & (RXBD_L | RXBD_ERRORS)) == RXBD_L) {
1678 size_t rxbd_len = consumer->rxbd_len;
1679 struct mbuf *m = rxq->rxq_mhead;
1680 struct mbuf *m_last = rxq->rxq_mconsumer;
1681 if ((rxq->rxq_mhead = m_last->m_next) == NULL)
1682 rxq->rxq_mtail = &rxq->rxq_mhead;
1683 rxq->rxq_mconsumer = rxq->rxq_mhead;
1684 m_last->m_next = NULL;
1685 m_last->m_len = rxbd_len & (MCLBYTES - 1);
1686 m->m_pkthdr.len = rxbd_len;
1687 pq3etsec_rx_input(sc, m, rxbd_flags);
1688 } else if (rxbd_flags & RXBD_L) {
1689 KASSERT(rxbd_flags & RXBD_ERRORS);
1690 struct mbuf *m;
1691 /*
1692 * We encountered an error, take the mbufs and add
1693 * then to the rx bufcache so we can reuse them.
1694 */
1695 if_statinc(ifp, if_ierrors);
1696 for (m = rxq->rxq_mhead;
1697 m != rxq->rxq_mconsumer;
1698 m = m->m_next) {
1699 IF_ENQUEUE(&sc->sc_rx_bufcache, m);
1700 }
1701 m = rxq->rxq_mconsumer;
1702 if ((rxq->rxq_mhead = m->m_next) == NULL)
1703 rxq->rxq_mtail = &rxq->rxq_mhead;
1704 rxq->rxq_mconsumer = m->m_next;
1705 IF_ENQUEUE(&sc->sc_rx_bufcache, m);
1706 } else {
1707 rxq->rxq_mconsumer = rxq->rxq_mconsumer->m_next;
1708 }
1709 #ifdef ETSEC_DEBUG
1710 rxq->rxq_mbufs[consumer - rxq->rxq_first] = NULL;
1711 #endif
1712
1713 /*
1714 * Wrap at the last entry!
1715 */
1716 if (rxbd_flags & RXBD_W) {
1717 KASSERT(consumer + 1 == rxq->rxq_last);
1718 consumer = rxq->rxq_first;
1719 } else {
1720 consumer++;
1721 }
1722 #ifdef ETSEC_DEBUG
1723 KASSERT(rxq->rxq_mbufs[consumer - rxq->rxq_first] == rxq->rxq_mconsumer);
1724 #endif
1725 }
1726
1727 if (rxconsumed != 0)
1728 rnd_add_uint32(&sc->rnd_source, rxconsumed);
1729 }
1730
1731 static void
1732 pq3etsec_rxq_purge(
1733 struct pq3etsec_softc *sc,
1734 struct pq3etsec_rxqueue *rxq,
1735 bool discard)
1736 {
1737 struct mbuf *m;
1738
1739 if ((m = rxq->rxq_mhead) != NULL) {
1740 #ifdef ETSEC_DEBUG
1741 memset(rxq->rxq_mbufs, 0, sizeof(rxq->rxq_mbufs));
1742 #endif
1743
1744 if (discard) {
1745 pq3etsec_rx_map_unload(sc, m);
1746 m_freem(m);
1747 } else {
1748 while (m != NULL) {
1749 struct mbuf *m0 = m->m_next;
1750 m->m_next = NULL;
1751 IF_ENQUEUE(&sc->sc_rx_bufcache, m);
1752 m = m0;
1753 }
1754 }
1755 }
1756
1757 rxq->rxq_mconsumer = NULL;
1758 rxq->rxq_mhead = NULL;
1759 rxq->rxq_mtail = &rxq->rxq_mhead;
1760 rxq->rxq_inuse = 0;
1761 }
1762
1763 static void
1764 pq3etsec_rxq_reset(
1765 struct pq3etsec_softc *sc,
1766 struct pq3etsec_rxqueue *rxq)
1767 {
1768 /*
1769 * sync all the descriptors
1770 */
1771 pq3etsec_rxq_desc_postsync(sc, rxq, rxq->rxq_first,
1772 rxq->rxq_last - rxq->rxq_first);
1773
1774 /*
1775 * Make sure we own all descriptors in the ring.
1776 */
1777 volatile struct rxbd *rxbd;
1778 for (rxbd = rxq->rxq_first; rxbd < rxq->rxq_last - 1; rxbd++) {
1779 rxbd->rxbd_flags = RXBD_I;
1780 }
1781
1782 /*
1783 * Last descriptor has the wrap flag.
1784 */
1785 rxbd->rxbd_flags = RXBD_W | RXBD_I;
1786
1787 /*
1788 * Reset the producer consumer indexes.
1789 */
1790 rxq->rxq_consumer = rxq->rxq_first;
1791 rxq->rxq_producer = rxq->rxq_first;
1792 rxq->rxq_inuse = 0;
1793 if (rxq->rxq_threshold < ETSEC_MINRXMBUFS)
1794 rxq->rxq_threshold = ETSEC_MINRXMBUFS;
1795
1796 sc->sc_imask |= IEVENT_RXF | IEVENT_BSY;
1797
1798 /*
1799 * Restart the transmit at the first descriptor
1800 */
1801 etsec_write(sc, rxq->rxq_reg_rbase, rxq->rxq_descmap->dm_segs->ds_addr);
1802 }
1803
1804 static int
1805 pq3etsec_rxq_attach(
1806 struct pq3etsec_softc *sc,
1807 struct pq3etsec_rxqueue *rxq,
1808 u_int qno)
1809 {
1810 size_t map_size = PAGE_SIZE;
1811 size_t desc_count = map_size / sizeof(struct rxbd);
1812 int error;
1813 void *descs;
1814
1815 error = pq3etsec_dmamem_alloc(sc->sc_dmat, map_size,
1816 &rxq->rxq_descmap_seg, &rxq->rxq_descmap, &descs);
1817 if (error)
1818 return error;
1819
1820 memset(descs, 0, map_size);
1821 rxq->rxq_first = descs;
1822 rxq->rxq_last = rxq->rxq_first + desc_count;
1823 rxq->rxq_consumer = descs;
1824 rxq->rxq_producer = descs;
1825
1826 pq3etsec_rxq_purge(sc, rxq, true);
1827 pq3etsec_rxq_reset(sc, rxq);
1828
1829 rxq->rxq_reg_rbase = RBASEn(qno);
1830 rxq->rxq_qmask = RSTAT_QHLTn(qno) | RSTAT_RXFn(qno);
1831
1832 return 0;
1833 }
1834
1835 static bool
1836 pq3etsec_txq_active_p(
1837 struct pq3etsec_softc * const sc,
1838 struct pq3etsec_txqueue *txq)
1839 {
1840 return !IF_IS_EMPTY(&txq->txq_mbufs);
1841 }
1842
1843 static bool
1844 pq3etsec_txq_fillable_p(
1845 struct pq3etsec_softc * const sc,
1846 struct pq3etsec_txqueue *txq)
1847 {
1848 return txq->txq_free >= txq->txq_threshold;
1849 }
1850
1851 static int
1852 pq3etsec_txq_attach(
1853 struct pq3etsec_softc *sc,
1854 struct pq3etsec_txqueue *txq,
1855 u_int qno)
1856 {
1857 size_t map_size = PAGE_SIZE;
1858 size_t desc_count = map_size / sizeof(struct txbd);
1859 int error;
1860 void *descs;
1861
1862 error = pq3etsec_dmamem_alloc(sc->sc_dmat, map_size,
1863 &txq->txq_descmap_seg, &txq->txq_descmap, &descs);
1864 if (error)
1865 return error;
1866
1867 memset(descs, 0, map_size);
1868 txq->txq_first = descs;
1869 txq->txq_last = txq->txq_first + desc_count;
1870 txq->txq_consumer = descs;
1871 txq->txq_producer = descs;
1872
1873 IFQ_SET_MAXLEN(&txq->txq_mbufs, ETSEC_MAXTXMBUFS);
1874
1875 txq->txq_reg_tbase = TBASEn(qno);
1876 txq->txq_qmask = TSTAT_THLTn(qno) | TSTAT_TXFn(qno);
1877
1878 pq3etsec_txq_reset(sc, txq);
1879
1880 return 0;
1881 }
1882
1883 static int
1884 pq3etsec_txq_map_load(
1885 struct pq3etsec_softc *sc,
1886 struct pq3etsec_txqueue *txq,
1887 struct mbuf *m)
1888 {
1889 bus_dmamap_t map;
1890 int error;
1891
1892 map = M_GETCTX(m, bus_dmamap_t);
1893 if (map != NULL)
1894 return 0;
1895
1896 map = pq3etsec_mapcache_get(sc, sc->sc_tx_mapcache);
1897 if (map == NULL)
1898 return ENOMEM;
1899
1900 error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
1901 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1902 if (error)
1903 return error;
1904
1905 bus_dmamap_sync(sc->sc_dmat, map, 0, m->m_pkthdr.len,
1906 BUS_DMASYNC_PREWRITE);
1907 M_SETCTX(m, map);
1908 return 0;
1909 }
1910
1911 static void
1912 pq3etsec_txq_map_unload(
1913 struct pq3etsec_softc *sc,
1914 struct pq3etsec_txqueue *txq,
1915 struct mbuf *m)
1916 {
1917 KASSERT(m);
1918 bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
1919 bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1920 BUS_DMASYNC_POSTWRITE);
1921 bus_dmamap_unload(sc->sc_dmat, map);
1922 pq3etsec_mapcache_put(sc, sc->sc_tx_mapcache, map);
1923 }
1924
1925 static bool
1926 pq3etsec_txq_produce(
1927 struct pq3etsec_softc *sc,
1928 struct pq3etsec_txqueue *txq,
1929 struct mbuf *m)
1930 {
1931 bus_dmamap_t map = M_GETCTX(m, bus_dmamap_t);
1932
1933 if (map->dm_nsegs > txq->txq_free)
1934 return false;
1935
1936 /*
1937 * TCP Offload flag must be set in the first descriptor.
1938 */
1939 volatile struct txbd *producer = txq->txq_producer;
1940 uint16_t last_flags = TXBD_L;
1941 uint16_t first_flags = TXBD_R
1942 | ((m->m_flags & M_HASFCB) ? TXBD_TOE : 0);
1943
1944 /*
1945 * If we've produced enough descriptors without consuming any
1946 * we need to ask for an interrupt to reclaim some.
1947 */
1948 txq->txq_lastintr += map->dm_nsegs;
1949 if (ETSEC_IC_TX_ENABLED(sc)
1950 || txq->txq_lastintr >= txq->txq_threshold
1951 || txq->txq_mbufs.ifq_len + 1 == txq->txq_mbufs.ifq_maxlen) {
1952 txq->txq_lastintr = 0;
1953 last_flags |= TXBD_I;
1954 }
1955
1956 #ifdef ETSEC_DEBUG
1957 KASSERT(txq->txq_lmbufs[producer - txq->txq_first] == NULL);
1958 #endif
1959 KASSERT(producer != txq->txq_last);
1960 producer->txbd_bufptr = map->dm_segs[0].ds_addr;
1961 producer->txbd_len = map->dm_segs[0].ds_len;
1962
1963 if (map->dm_nsegs > 1) {
1964 volatile struct txbd *start = producer + 1;
1965 size_t count = map->dm_nsegs - 1;
1966 for (u_int i = 1; i < map->dm_nsegs; i++) {
1967 if (__predict_false(++producer == txq->txq_last)) {
1968 producer = txq->txq_first;
1969 if (start < txq->txq_last) {
1970 pq3etsec_txq_desc_presync(sc, txq,
1971 start, txq->txq_last - start);
1972 count -= txq->txq_last - start;
1973 }
1974 start = txq->txq_first;
1975 }
1976 #ifdef ETSEC_DEBUG
1977 KASSERT(txq->txq_lmbufs[producer - txq->txq_first] == NULL);
1978 #endif
1979 producer->txbd_bufptr = map->dm_segs[i].ds_addr;
1980 producer->txbd_len = map->dm_segs[i].ds_len;
1981 producer->txbd_flags = TXBD_R
1982 | (producer->txbd_flags & TXBD_W)
1983 | (i == map->dm_nsegs - 1 ? last_flags : 0);
1984 #if 0
1985 printf("%s: txbd[%u]=%#x/%u/%#x\n", __func__, producer - txq->txq_first,
1986 producer->txbd_flags, producer->txbd_len, producer->txbd_bufptr);
1987 #endif
1988 }
1989 pq3etsec_txq_desc_presync(sc, txq, start, count);
1990 } else {
1991 first_flags |= last_flags;
1992 }
1993
1994 membar_producer();
1995 txq->txq_producer->txbd_flags =
1996 first_flags | (txq->txq_producer->txbd_flags & TXBD_W);
1997 #if 0
1998 printf("%s: txbd[%u]=%#x/%u/%#x\n", __func__,
1999 txq->txq_producer - txq->txq_first, txq->txq_producer->txbd_flags,
2000 txq->txq_producer->txbd_len, txq->txq_producer->txbd_bufptr);
2001 #endif
2002 pq3etsec_txq_desc_presync(sc, txq, txq->txq_producer, 1);
2003
2004 /*
2005 * Reduce free count by the number of segments we consumed.
2006 */
2007 txq->txq_free -= map->dm_nsegs;
2008 KASSERT(map->dm_nsegs == 1 || txq->txq_producer != producer);
2009 KASSERT(map->dm_nsegs == 1 || (txq->txq_producer->txbd_flags & TXBD_L) == 0);
2010 KASSERT(producer->txbd_flags & TXBD_L);
2011 #ifdef ETSEC_DEBUG
2012 txq->txq_lmbufs[producer - txq->txq_first] = m;
2013 #endif
2014
2015 #if 0
2016 printf("%s: mbuf %p: produced a %u byte packet in %u segments (%u..%u)\n",
2017 __func__, m, m->m_pkthdr.len, map->dm_nsegs,
2018 txq->txq_producer - txq->txq_first, producer - txq->txq_first);
2019 #endif
2020
2021 if (++producer == txq->txq_last)
2022 txq->txq_producer = txq->txq_first;
2023 else
2024 txq->txq_producer = producer;
2025 IF_ENQUEUE(&txq->txq_mbufs, m);
2026
2027 /*
2028 * Restart the transmitter.
2029 */
2030 etsec_write(sc, TSTAT, txq->txq_qmask & TSTAT_THLT); /* W1C */
2031
2032 return true;
2033 }
2034
2035 static void
2036 pq3etsec_tx_offload(
2037 struct pq3etsec_softc *sc,
2038 struct pq3etsec_txqueue *txq,
2039 struct mbuf **mp)
2040 {
2041 struct mbuf *m = *mp;
2042 u_int csum_flags = m->m_pkthdr.csum_flags;
2043 bool have_vtag;
2044 uint16_t vtag;
2045
2046 KASSERT(m->m_flags & M_PKTHDR);
2047
2048 have_vtag = vlan_has_tag(m);
2049 vtag = (have_vtag) ? vlan_get_tag(m) : 0;
2050
2051 /*
2052 * Let see if we are doing any offload first.
2053 */
2054 if (csum_flags == 0 && !have_vtag) {
2055 m->m_flags &= ~M_HASFCB;
2056 return;
2057 }
2058
2059 uint16_t flags = 0;
2060 if (csum_flags & M_CSUM_IP) {
2061 flags |= TXFCB_IP
2062 | ((csum_flags & M_CSUM_IP6) ? TXFCB_IP6 : 0)
2063 | ((csum_flags & M_CSUM_TUP) ? TXFCB_TUP : 0)
2064 | ((csum_flags & M_CSUM_UDP) ? TXFCB_UDP : 0)
2065 | ((csum_flags & M_CSUM_CIP) ? TXFCB_CIP : 0)
2066 | ((csum_flags & M_CSUM_CTU) ? TXFCB_CTU : 0);
2067 }
2068 if (have_vtag) {
2069 flags |= TXFCB_VLN;
2070 }
2071 if (flags == 0) {
2072 m->m_flags &= ~M_HASFCB;
2073 return;
2074 }
2075
2076 struct txfcb fcb;
2077 fcb.txfcb_flags = flags;
2078 if (csum_flags & M_CSUM_IPv4)
2079 fcb.txfcb_l4os = M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
2080 else
2081 fcb.txfcb_l4os = M_CSUM_DATA_IPv6_IPHL(m->m_pkthdr.csum_data);
2082 fcb.txfcb_l3os = ETHER_HDR_LEN;
2083 fcb.txfcb_phcs = 0;
2084 fcb.txfcb_vlctl = vtag;
2085
2086 #if 0
2087 printf("%s: csum_flags=%#x: txfcb flags=%#x lsos=%u l4os=%u phcs=%u vlctl=%#x\n",
2088 __func__, csum_flags, fcb.txfcb_flags, fcb.txfcb_l3os, fcb.txfcb_l4os,
2089 fcb.txfcb_phcs, fcb.txfcb_vlctl);
2090 #endif
2091
2092 if (M_LEADINGSPACE(m) >= sizeof(fcb)) {
2093 m->m_data -= sizeof(fcb);
2094 m->m_len += sizeof(fcb);
2095 } else if (!(m->m_flags & M_EXT) && MHLEN - m->m_len >= sizeof(fcb)) {
2096 memmove(m->m_pktdat + sizeof(fcb), m->m_data, m->m_len);
2097 m->m_data = m->m_pktdat;
2098 m->m_len += sizeof(fcb);
2099 } else {
2100 struct mbuf *mn;
2101 MGET(mn, M_DONTWAIT, m->m_type);
2102 if (mn == NULL) {
2103 if (csum_flags & M_CSUM_IP4) {
2104 #ifdef INET
2105 in_undefer_cksum(m, ETHER_HDR_LEN,
2106 csum_flags & M_CSUM_IP4);
2107 #else
2108 panic("%s: impossible M_CSUM flags %#x",
2109 device_xname(sc->sc_dev), csum_flags);
2110 #endif
2111 } else if (csum_flags & M_CSUM_IP6) {
2112 #ifdef INET6
2113 in6_undefer_cksum(m, ETHER_HDR_LEN,
2114 csum_flags & M_CSUM_IP6);
2115 #else
2116 panic("%s: impossible M_CSUM flags %#x",
2117 device_xname(sc->sc_dev), csum_flags);
2118 #endif
2119 }
2120
2121 m->m_flags &= ~M_HASFCB;
2122 return;
2123 }
2124
2125 m_move_pkthdr(mn, m);
2126 mn->m_next = m;
2127 m = mn;
2128 m_align(m, sizeof(fcb));
2129 m->m_len = sizeof(fcb);
2130 *mp = m;
2131 }
2132 m->m_pkthdr.len += sizeof(fcb);
2133 m->m_flags |= M_HASFCB;
2134 *mtod(m, struct txfcb *) = fcb;
2135 return;
2136 }
2137
2138 static bool
2139 pq3etsec_txq_enqueue(
2140 struct pq3etsec_softc *sc,
2141 struct pq3etsec_txqueue *txq)
2142 {
2143 for (;;) {
2144 if (IF_QFULL(&txq->txq_mbufs))
2145 return false;
2146 struct mbuf *m = txq->txq_next;
2147 if (m == NULL) {
2148 int s = splnet();
2149 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
2150 splx(s);
2151 if (m == NULL)
2152 return true;
2153 M_SETCTX(m, NULL);
2154 pq3etsec_tx_offload(sc, txq, &m);
2155 } else {
2156 txq->txq_next = NULL;
2157 }
2158 int error = pq3etsec_txq_map_load(sc, txq, m);
2159 if (error) {
2160 aprint_error_dev(sc->sc_dev,
2161 "discarded packet due to "
2162 "dmamap load failure: %d\n", error);
2163 m_freem(m);
2164 continue;
2165 }
2166 KASSERT(txq->txq_next == NULL);
2167 if (!pq3etsec_txq_produce(sc, txq, m)) {
2168 txq->txq_next = m;
2169 return false;
2170 }
2171 KASSERT(txq->txq_next == NULL);
2172 }
2173 }
2174
2175 static bool
2176 pq3etsec_txq_consume(
2177 struct pq3etsec_softc *sc,
2178 struct pq3etsec_txqueue *txq)
2179 {
2180 struct ifnet * const ifp = &sc->sc_if;
2181 volatile struct txbd *consumer = txq->txq_consumer;
2182 size_t txfree = 0;
2183
2184 #if 0
2185 printf("%s: entry: free=%zu\n", __func__, txq->txq_free);
2186 #endif
2187 etsec_write(sc, TSTAT, TSTAT_TXF & txq->txq_qmask);
2188
2189 for (;;) {
2190 if (consumer == txq->txq_producer) {
2191 txq->txq_consumer = consumer;
2192 txq->txq_free += txfree;
2193 txq->txq_lastintr -= uimin(txq->txq_lastintr, txfree);
2194 #if 0
2195 printf("%s: empty: freed %zu descriptors going form %zu to %zu\n",
2196 __func__, txfree, txq->txq_free - txfree, txq->txq_free);
2197 #endif
2198 KASSERT(txq->txq_lastintr == 0);
2199 KASSERT(txq->txq_free == txq->txq_last - txq->txq_first - 1);
2200 return true;
2201 }
2202 pq3etsec_txq_desc_postsync(sc, txq, consumer, 1);
2203 const uint16_t txbd_flags = consumer->txbd_flags;
2204 if (txbd_flags & TXBD_R) {
2205 txq->txq_consumer = consumer;
2206 txq->txq_free += txfree;
2207 txq->txq_lastintr -= uimin(txq->txq_lastintr, txfree);
2208 #if 0
2209 printf("%s: freed %zu descriptors\n",
2210 __func__, txfree);
2211 #endif
2212 return pq3etsec_txq_fillable_p(sc, txq);
2213 }
2214
2215 /*
2216 * If this is the last descriptor in the chain, get the
2217 * mbuf, free its dmamap, and free the mbuf chain itself.
2218 */
2219 if (txbd_flags & TXBD_L) {
2220 struct mbuf *m;
2221
2222 IF_DEQUEUE(&txq->txq_mbufs, m);
2223 #ifdef ETSEC_DEBUG
2224 KASSERTMSG(
2225 m == txq->txq_lmbufs[consumer-txq->txq_first],
2226 "%s: %p [%u]: flags %#x m (%p) != %p (%p)",
2227 __func__, consumer, consumer - txq->txq_first,
2228 txbd_flags, m,
2229 &txq->txq_lmbufs[consumer-txq->txq_first],
2230 txq->txq_lmbufs[consumer-txq->txq_first]);
2231 #endif
2232 KASSERT(m);
2233 pq3etsec_txq_map_unload(sc, txq, m);
2234 #if 0
2235 printf("%s: mbuf %p: consumed a %u byte packet\n",
2236 __func__, m, m->m_pkthdr.len);
2237 #endif
2238 if (m->m_flags & M_HASFCB)
2239 m_adj(m, sizeof(struct txfcb));
2240 bpf_mtap(ifp, m, BPF_D_OUT);
2241 net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
2242 if_statinc_ref(nsr, if_opackets);
2243 if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
2244 if (m->m_flags & M_MCAST)
2245 if_statinc_ref(nsr, if_omcasts);
2246 if (txbd_flags & TXBD_ERRORS)
2247 if_statinc_ref(nsr, if_oerrors);
2248 IF_STAT_PUTREF(ifp);
2249 m_freem(m);
2250 #ifdef ETSEC_DEBUG
2251 txq->txq_lmbufs[consumer - txq->txq_first] = NULL;
2252 #endif
2253 } else {
2254 #ifdef ETSEC_DEBUG
2255 KASSERT(txq->txq_lmbufs[consumer-txq->txq_first] == NULL);
2256 #endif
2257 }
2258
2259 /*
2260 * We own this packet again. Clear all flags except wrap.
2261 */
2262 txfree++;
2263 //consumer->txbd_flags = txbd_flags & TXBD_W;
2264
2265 /*
2266 * Wrap at the last entry!
2267 */
2268 if (txbd_flags & TXBD_W) {
2269 KASSERT(consumer + 1 == txq->txq_last);
2270 consumer = txq->txq_first;
2271 } else {
2272 consumer++;
2273 KASSERT(consumer < txq->txq_last);
2274 }
2275 }
2276
2277 if (txfree != 0)
2278 rnd_add_uint32(&sc->rnd_source, txfree);
2279 }
2280
2281 static void
2282 pq3etsec_txq_purge(
2283 struct pq3etsec_softc *sc,
2284 struct pq3etsec_txqueue *txq)
2285 {
2286 struct mbuf *m;
2287 KASSERT((etsec_read(sc, MACCFG1) & MACCFG1_TX_EN) == 0);
2288
2289 for (;;) {
2290 IF_DEQUEUE(&txq->txq_mbufs, m);
2291 if (m == NULL)
2292 break;
2293 pq3etsec_txq_map_unload(sc, txq, m);
2294 m_freem(m);
2295 }
2296 if ((m = txq->txq_next) != NULL) {
2297 txq->txq_next = NULL;
2298 pq3etsec_txq_map_unload(sc, txq, m);
2299 m_freem(m);
2300 }
2301 #ifdef ETSEC_DEBUG
2302 memset(txq->txq_lmbufs, 0, sizeof(txq->txq_lmbufs));
2303 #endif
2304 }
2305
2306 static void
2307 pq3etsec_txq_reset(
2308 struct pq3etsec_softc *sc,
2309 struct pq3etsec_txqueue *txq)
2310 {
2311 /*
2312 * sync all the descriptors
2313 */
2314 pq3etsec_txq_desc_postsync(sc, txq, txq->txq_first,
2315 txq->txq_last - txq->txq_first);
2316
2317 /*
2318 * Make sure we own all descriptors in the ring.
2319 */
2320 volatile struct txbd *txbd;
2321 for (txbd = txq->txq_first; txbd < txq->txq_last - 1; txbd++) {
2322 txbd->txbd_flags = 0;
2323 }
2324
2325 /*
2326 * Last descriptor has the wrap flag.
2327 */
2328 txbd->txbd_flags = TXBD_W;
2329
2330 /*
2331 * Reset the producer consumer indexes.
2332 */
2333 txq->txq_consumer = txq->txq_first;
2334 txq->txq_producer = txq->txq_first;
2335 txq->txq_free = txq->txq_last - txq->txq_first - 1;
2336 txq->txq_threshold = txq->txq_free / 2;
2337 txq->txq_lastintr = 0;
2338
2339 /*
2340 * What do we want to get interrupted on?
2341 */
2342 sc->sc_imask |= IEVENT_TXF | IEVENT_TXE;
2343
2344 /*
2345 * Restart the transmit at the first descriptor
2346 */
2347 etsec_write(sc, txq->txq_reg_tbase, txq->txq_descmap->dm_segs->ds_addr);
2348 }
2349
2350 static void
2351 pq3etsec_ifstart(struct ifnet *ifp)
2352 {
2353 struct pq3etsec_softc * const sc = ifp->if_softc;
2354
2355 if (__predict_false((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)) {
2356 return;
2357 }
2358
2359 atomic_or_uint(&sc->sc_soft_flags, SOFT_TXINTR);
2360 softint_schedule(sc->sc_soft_ih);
2361 }
2362
2363 static void
2364 pq3etsec_tx_error(
2365 struct pq3etsec_softc * const sc)
2366 {
2367 struct pq3etsec_txqueue * const txq = &sc->sc_txq;
2368
2369 pq3etsec_txq_consume(sc, txq);
2370
2371 if (pq3etsec_txq_fillable_p(sc, txq))
2372 sc->sc_if.if_flags &= ~IFF_OACTIVE;
2373 if (sc->sc_txerrors
2374 & (IEVENT_LC | IEVENT_CRL | IEVENT_XFUN | IEVENT_BABT)) {
2375 } else if (sc->sc_txerrors & IEVENT_EBERR) {
2376 }
2377
2378 if (pq3etsec_txq_active_p(sc, txq))
2379 etsec_write(sc, TSTAT, TSTAT_THLT & txq->txq_qmask);
2380 if (!pq3etsec_txq_enqueue(sc, txq)) {
2381 sc->sc_ev_tx_stall.ev_count++;
2382 sc->sc_if.if_flags |= IFF_OACTIVE;
2383 }
2384
2385 sc->sc_txerrors = 0;
2386 }
2387
2388 int
2389 pq3etsec_tx_intr(void *arg)
2390 {
2391 struct pq3etsec_softc * const sc = arg;
2392
2393 mutex_enter(sc->sc_hwlock);
2394
2395 sc->sc_ev_tx_intr.ev_count++;
2396
2397 uint32_t ievent = etsec_read(sc, IEVENT);
2398 ievent &= IEVENT_TXF | IEVENT_TXB;
2399 etsec_write(sc, IEVENT, ievent); /* write 1 to clear */
2400
2401 #if 0
2402 aprint_normal_dev(sc->sc_dev, "%s: ievent=%#x imask=%#x\n",
2403 __func__, ievent, etsec_read(sc, IMASK));
2404 #endif
2405
2406 if (ievent == 0) {
2407 mutex_exit(sc->sc_hwlock);
2408 return 0;
2409 }
2410
2411 sc->sc_imask &= ~(IEVENT_TXF | IEVENT_TXB);
2412 atomic_or_uint(&sc->sc_soft_flags, SOFT_TXINTR);
2413 etsec_write(sc, IMASK, sc->sc_imask);
2414 softint_schedule(sc->sc_soft_ih);
2415
2416 mutex_exit(sc->sc_hwlock);
2417
2418 return 1;
2419 }
2420
2421 int
2422 pq3etsec_rx_intr(void *arg)
2423 {
2424 struct pq3etsec_softc * const sc = arg;
2425
2426 mutex_enter(sc->sc_hwlock);
2427
2428 sc->sc_ev_rx_intr.ev_count++;
2429
2430 uint32_t ievent = etsec_read(sc, IEVENT);
2431 ievent &= IEVENT_RXF | IEVENT_RXB;
2432 etsec_write(sc, IEVENT, ievent); /* write 1 to clear */
2433 if (ievent == 0) {
2434 mutex_exit(sc->sc_hwlock);
2435 return 0;
2436 }
2437
2438 #if 0
2439 aprint_normal_dev(sc->sc_dev, "%s: ievent=%#x\n", __func__, ievent);
2440 #endif
2441
2442 sc->sc_imask &= ~(IEVENT_RXF | IEVENT_RXB);
2443 atomic_or_uint(&sc->sc_soft_flags, SOFT_RXINTR);
2444 etsec_write(sc, IMASK, sc->sc_imask);
2445 softint_schedule(sc->sc_soft_ih);
2446
2447 mutex_exit(sc->sc_hwlock);
2448
2449 return 1;
2450 }
2451
2452 int
2453 pq3etsec_error_intr(void *arg)
2454 {
2455 struct pq3etsec_softc * const sc = arg;
2456
2457 mutex_enter(sc->sc_hwlock);
2458
2459 sc->sc_ev_error_intr.ev_count++;
2460
2461 for (int rv = 0, soft_flags = 0;; rv = 1) {
2462 uint32_t ievent = etsec_read(sc, IEVENT);
2463 ievent &= ~(IEVENT_RXF | IEVENT_RXB | IEVENT_TXF | IEVENT_TXB);
2464 etsec_write(sc, IEVENT, ievent); /* write 1 to clear */
2465 if (ievent == 0) {
2466 if (soft_flags) {
2467 atomic_or_uint(&sc->sc_soft_flags, soft_flags);
2468 softint_schedule(sc->sc_soft_ih);
2469 }
2470 mutex_exit(sc->sc_hwlock);
2471 return rv;
2472 }
2473 #if 0
2474 aprint_normal_dev(sc->sc_dev, "%s: ievent=%#x imask=%#x\n",
2475 __func__, ievent, etsec_read(sc, IMASK));
2476 #endif
2477
2478 if (ievent & (IEVENT_GRSC | IEVENT_GTSC)) {
2479 sc->sc_imask &= ~(IEVENT_GRSC | IEVENT_GTSC);
2480 etsec_write(sc, IMASK, sc->sc_imask);
2481 wakeup(sc);
2482 }
2483 if (ievent & (IEVENT_MMRD | IEVENT_MMWR)) {
2484 sc->sc_imask &= ~(IEVENT_MMRD | IEVENT_MMWR);
2485 etsec_write(sc, IMASK, sc->sc_imask);
2486 wakeup(&sc->sc_mii);
2487 }
2488 if (ievent & IEVENT_BSY) {
2489 soft_flags |= SOFT_RXBSY;
2490 sc->sc_imask &= ~IEVENT_BSY;
2491 etsec_write(sc, IMASK, sc->sc_imask);
2492 }
2493 if (ievent & IEVENT_TXE) {
2494 soft_flags |= SOFT_TXERROR;
2495 sc->sc_imask &= ~IEVENT_TXE;
2496 sc->sc_txerrors |= ievent;
2497 }
2498 if (ievent & IEVENT_TXC) {
2499 sc->sc_ev_tx_pause.ev_count++;
2500 }
2501 if (ievent & IEVENT_RXC) {
2502 sc->sc_ev_rx_pause.ev_count++;
2503 }
2504 if (ievent & IEVENT_DPE) {
2505 soft_flags |= SOFT_RESET;
2506 sc->sc_imask &= ~IEVENT_DPE;
2507 etsec_write(sc, IMASK, sc->sc_imask);
2508 }
2509 }
2510 }
2511
2512 void
2513 pq3etsec_soft_intr(void *arg)
2514 {
2515 struct pq3etsec_softc * const sc = arg;
2516 struct ifnet * const ifp = &sc->sc_if;
2517 uint32_t imask = 0;
2518
2519 mutex_enter(sc->sc_lock);
2520
2521 u_int soft_flags = atomic_swap_uint(&sc->sc_soft_flags, 0);
2522
2523 sc->sc_ev_soft_intr.ev_count++;
2524
2525 if (soft_flags & SOFT_RESET) {
2526 int s = splnet();
2527 pq3etsec_ifinit(ifp);
2528 splx(s);
2529 soft_flags = 0;
2530 }
2531
2532 if (soft_flags & SOFT_RXBSY) {
2533 struct pq3etsec_rxqueue * const rxq = &sc->sc_rxq;
2534 size_t threshold = 5 * rxq->rxq_threshold / 4;
2535 if (threshold >= rxq->rxq_last - rxq->rxq_first) {
2536 threshold = rxq->rxq_last - rxq->rxq_first - 1;
2537 } else {
2538 imask |= IEVENT_BSY;
2539 }
2540 aprint_normal_dev(sc->sc_dev,
2541 "increasing receive buffers from %zu to %zu\n",
2542 rxq->rxq_threshold, threshold);
2543 rxq->rxq_threshold = threshold;
2544 }
2545
2546 if ((soft_flags & SOFT_TXINTR)
2547 || pq3etsec_txq_active_p(sc, &sc->sc_txq)) {
2548 /*
2549 * Let's do what we came here for. Consume transmitted
2550 * packets off the transmit ring.
2551 */
2552 if (!pq3etsec_txq_consume(sc, &sc->sc_txq)
2553 || !pq3etsec_txq_enqueue(sc, &sc->sc_txq)) {
2554 sc->sc_ev_tx_stall.ev_count++;
2555 ifp->if_flags |= IFF_OACTIVE;
2556 } else {
2557 ifp->if_flags &= ~IFF_OACTIVE;
2558 }
2559 imask |= IEVENT_TXF;
2560 }
2561
2562 if (soft_flags & (SOFT_RXINTR | SOFT_RXBSY)) {
2563 /* Let's consume */
2564 pq3etsec_rxq_consume(sc, &sc->sc_rxq);
2565 imask |= IEVENT_RXF;
2566 }
2567
2568 if (soft_flags & SOFT_TXERROR) {
2569 pq3etsec_tx_error(sc);
2570 imask |= IEVENT_TXE;
2571 }
2572
2573 if (ifp->if_flags & IFF_RUNNING) {
2574 pq3etsec_rxq_produce(sc, &sc->sc_rxq);
2575 mutex_spin_enter(sc->sc_hwlock);
2576 sc->sc_imask |= imask;
2577 etsec_write(sc, IMASK, sc->sc_imask);
2578 mutex_spin_exit(sc->sc_hwlock);
2579 } else {
2580 KASSERT((soft_flags & SOFT_RXBSY) == 0);
2581 }
2582
2583 mutex_exit(sc->sc_lock);
2584 }
2585
2586 static void
2587 pq3etsec_mii_tick(void *arg)
2588 {
2589 struct pq3etsec_softc * const sc = arg;
2590 mutex_enter(sc->sc_lock);
2591 callout_ack(&sc->sc_mii_callout);
2592 sc->sc_ev_mii_ticks.ev_count++;
2593 #ifdef DEBUG
2594 uint64_t now = mftb();
2595 if (now - sc->sc_mii_last_tick < cpu_timebase - 5000) {
2596 aprint_debug_dev(sc->sc_dev, "%s: diff=%"PRIu64"\n",
2597 __func__, now - sc->sc_mii_last_tick);
2598 callout_stop(&sc->sc_mii_callout);
2599 }
2600 #endif
2601 mii_tick(&sc->sc_mii);
2602 int s = splnet();
2603 if (sc->sc_soft_flags & SOFT_RESET)
2604 softint_schedule(sc->sc_soft_ih);
2605 splx(s);
2606 callout_schedule(&sc->sc_mii_callout, hz);
2607 #ifdef DEBUG
2608 sc->sc_mii_last_tick = now;
2609 #endif
2610 mutex_exit(sc->sc_lock);
2611 }
2612
2613 static void
2614 pq3etsec_set_ic_rx(struct pq3etsec_softc *sc)
2615 {
2616 uint32_t reg;
2617
2618 if (ETSEC_IC_RX_ENABLED(sc)) {
2619 reg = RXIC_ICEN;
2620 reg |= RXIC_ICFT_SET(sc->sc_ic_rx_count);
2621 reg |= RXIC_ICTT_SET(sc->sc_ic_rx_time);
2622 } else {
2623 /* Disable RX interrupt coalescing */
2624 reg = 0;
2625 }
2626
2627 etsec_write(sc, RXIC, reg);
2628 }
2629
2630 static void
2631 pq3etsec_set_ic_tx(struct pq3etsec_softc *sc)
2632 {
2633 uint32_t reg;
2634
2635 if (ETSEC_IC_TX_ENABLED(sc)) {
2636 reg = TXIC_ICEN;
2637 reg |= TXIC_ICFT_SET(sc->sc_ic_tx_count);
2638 reg |= TXIC_ICTT_SET(sc->sc_ic_tx_time);
2639 } else {
2640 /* Disable TX interrupt coalescing */
2641 reg = 0;
2642 }
2643
2644 etsec_write(sc, TXIC, reg);
2645 }
2646
2647 /*
2648 * sysctl
2649 */
2650 static int
2651 pq3etsec_sysctl_ic_time_helper(SYSCTLFN_ARGS, int *valuep)
2652 {
2653 struct sysctlnode node = *rnode;
2654 struct pq3etsec_softc *sc = rnode->sysctl_data;
2655 int value = *valuep;
2656 int error;
2657
2658 node.sysctl_data = &value;
2659 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2660 if (error != 0 || newp == NULL)
2661 return error;
2662
2663 if (value < 0 || value > 65535)
2664 return EINVAL;
2665
2666 mutex_enter(sc->sc_lock);
2667 *valuep = value;
2668 if (valuep == &sc->sc_ic_rx_time)
2669 pq3etsec_set_ic_rx(sc);
2670 else
2671 pq3etsec_set_ic_tx(sc);
2672 mutex_exit(sc->sc_lock);
2673
2674 return 0;
2675 }
2676
2677 static int
2678 pq3etsec_sysctl_ic_count_helper(SYSCTLFN_ARGS, int *valuep)
2679 {
2680 struct sysctlnode node = *rnode;
2681 struct pq3etsec_softc *sc = rnode->sysctl_data;
2682 int value = *valuep;
2683 int error;
2684
2685 node.sysctl_data = &value;
2686 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2687 if (error != 0 || newp == NULL)
2688 return error;
2689
2690 if (value < 0 || value > 255)
2691 return EINVAL;
2692
2693 mutex_enter(sc->sc_lock);
2694 *valuep = value;
2695 if (valuep == &sc->sc_ic_rx_count)
2696 pq3etsec_set_ic_rx(sc);
2697 else
2698 pq3etsec_set_ic_tx(sc);
2699 mutex_exit(sc->sc_lock);
2700
2701 return 0;
2702 }
2703
2704 static int
2705 pq3etsec_sysctl_ic_rx_time_helper(SYSCTLFN_ARGS)
2706 {
2707 struct pq3etsec_softc *sc = rnode->sysctl_data;
2708
2709 return pq3etsec_sysctl_ic_time_helper(SYSCTLFN_CALL(rnode),
2710 &sc->sc_ic_rx_time);
2711 }
2712
2713 static int
2714 pq3etsec_sysctl_ic_rx_count_helper(SYSCTLFN_ARGS)
2715 {
2716 struct pq3etsec_softc *sc = rnode->sysctl_data;
2717
2718 return pq3etsec_sysctl_ic_count_helper(SYSCTLFN_CALL(rnode),
2719 &sc->sc_ic_rx_count);
2720 }
2721
2722 static int
2723 pq3etsec_sysctl_ic_tx_time_helper(SYSCTLFN_ARGS)
2724 {
2725 struct pq3etsec_softc *sc = rnode->sysctl_data;
2726
2727 return pq3etsec_sysctl_ic_time_helper(SYSCTLFN_CALL(rnode),
2728 &sc->sc_ic_tx_time);
2729 }
2730
2731 static int
2732 pq3etsec_sysctl_ic_tx_count_helper(SYSCTLFN_ARGS)
2733 {
2734 struct pq3etsec_softc *sc = rnode->sysctl_data;
2735
2736 return pq3etsec_sysctl_ic_count_helper(SYSCTLFN_CALL(rnode),
2737 &sc->sc_ic_tx_count);
2738 }
2739
2740 static void pq3etsec_sysctl_setup(struct sysctllog **clog,
2741 struct pq3etsec_softc *sc)
2742 {
2743 const struct sysctlnode *cnode, *rnode;
2744
2745 if (sysctl_createv(clog, 0, NULL, &rnode,
2746 CTLFLAG_PERMANENT,
2747 CTLTYPE_NODE, device_xname(sc->sc_dev),
2748 SYSCTL_DESCR("TSEC interface"),
2749 NULL, 0, NULL, 0,
2750 CTL_HW, CTL_CREATE, CTL_EOL) != 0)
2751 goto bad;
2752
2753 if (sysctl_createv(clog, 0, &rnode, &rnode,
2754 CTLFLAG_PERMANENT,
2755 CTLTYPE_NODE, "int_coal",
2756 SYSCTL_DESCR("Interrupts coalescing"),
2757 NULL, 0, NULL, 0,
2758 CTL_CREATE, CTL_EOL) != 0)
2759 goto bad;
2760
2761 if (sysctl_createv(clog, 0, &rnode, &cnode,
2762 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
2763 CTLTYPE_INT, "rx_time",
2764 SYSCTL_DESCR("RX time threshold (0-65535)"),
2765 pq3etsec_sysctl_ic_rx_time_helper, 0, (void *)sc, 0,
2766 CTL_CREATE, CTL_EOL) != 0)
2767 goto bad;
2768
2769 if (sysctl_createv(clog, 0, &rnode, &cnode,
2770 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
2771 CTLTYPE_INT, "rx_count",
2772 SYSCTL_DESCR("RX frame count threshold (0-255)"),
2773 pq3etsec_sysctl_ic_rx_count_helper, 0, (void *)sc, 0,
2774 CTL_CREATE, CTL_EOL) != 0)
2775 goto bad;
2776
2777 if (sysctl_createv(clog, 0, &rnode, &cnode,
2778 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
2779 CTLTYPE_INT, "tx_time",
2780 SYSCTL_DESCR("TX time threshold (0-65535)"),
2781 pq3etsec_sysctl_ic_tx_time_helper, 0, (void *)sc, 0,
2782 CTL_CREATE, CTL_EOL) != 0)
2783 goto bad;
2784
2785 if (sysctl_createv(clog, 0, &rnode, &cnode,
2786 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
2787 CTLTYPE_INT, "tx_count",
2788 SYSCTL_DESCR("TX frame count threshold (0-255)"),
2789 pq3etsec_sysctl_ic_tx_count_helper, 0, (void *)sc, 0,
2790 CTL_CREATE, CTL_EOL) != 0)
2791 goto bad;
2792
2793 return;
2794
2795 bad:
2796 aprint_error_dev(sc->sc_dev, "could not attach sysctl nodes\n");
2797 }
2798