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