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