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