ixp425_if_npe.c revision 1.50 1 1.50 riastrad /* $NetBSD: ixp425_if_npe.c,v 1.50 2021/12/31 14:25:22 riastradh Exp $ */
2 1.1 scw
3 1.1 scw /*-
4 1.1 scw * Copyright (c) 2006 Sam Leffler. All rights reserved.
5 1.1 scw *
6 1.1 scw * Redistribution and use in source and binary forms, with or without
7 1.1 scw * modification, are permitted provided that the following conditions
8 1.1 scw * are met:
9 1.1 scw * 1. Redistributions of source code must retain the above copyright
10 1.1 scw * notice, this list of conditions and the following disclaimer.
11 1.1 scw * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 scw * notice, this list of conditions and the following disclaimer in the
13 1.1 scw * documentation and/or other materials provided with the distribution.
14 1.1 scw *
15 1.1 scw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 scw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 scw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 scw * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 scw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 scw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 scw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 scw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 scw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 scw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 scw */
26 1.1 scw
27 1.1 scw #include <sys/cdefs.h>
28 1.1 scw #if 0
29 1.1 scw __FBSDID("$FreeBSD: src/sys/arm/xscale/ixp425/if_npe.c,v 1.1 2006/11/19 23:55:23 sam Exp $");
30 1.1 scw #endif
31 1.50 riastrad __KERNEL_RCSID(0, "$NetBSD: ixp425_if_npe.c,v 1.50 2021/12/31 14:25:22 riastradh Exp $");
32 1.1 scw
33 1.1 scw /*
34 1.1 scw * Intel XScale NPE Ethernet driver.
35 1.1 scw *
36 1.1 scw * This driver handles the two ports present on the IXP425.
37 1.1 scw * Packet processing is done by the Network Processing Engines
38 1.1 scw * (NPE's) that work together with a MAC and PHY. The MAC
39 1.1 scw * is also mapped to the XScale cpu; the PHY is accessed via
40 1.1 scw * the MAC. NPE-XScale communication happens through h/w
41 1.1 scw * queues managed by the Q Manager block.
42 1.1 scw *
43 1.1 scw * The code here replaces the ethAcc, ethMii, and ethDB classes
44 1.1 scw * in the Intel Access Library (IAL) and the OS-specific driver.
45 1.1 scw *
46 1.1 scw * XXX add vlan support
47 1.1 scw * XXX NPE-C port doesn't work yet
48 1.1 scw */
49 1.1 scw
50 1.1 scw #include <sys/param.h>
51 1.1 scw #include <sys/systm.h>
52 1.1 scw #include <sys/kernel.h>
53 1.1 scw #include <sys/device.h>
54 1.1 scw #include <sys/callout.h>
55 1.1 scw #include <sys/mbuf.h>
56 1.1 scw #include <sys/malloc.h>
57 1.1 scw #include <sys/socket.h>
58 1.1 scw #include <sys/endian.h>
59 1.1 scw #include <sys/ioctl.h>
60 1.11 msaitoh #include <sys/syslog.h>
61 1.20 dyoung #include <sys/bus.h>
62 1.39 msaitoh #include <sys/rndsource.h>
63 1.1 scw
64 1.1 scw #include <net/if.h>
65 1.1 scw #include <net/if_dl.h>
66 1.1 scw #include <net/if_media.h>
67 1.1 scw #include <net/if_ether.h>
68 1.1 scw #include <net/bpf.h>
69 1.1 scw
70 1.1 scw #include <arm/xscale/ixp425reg.h>
71 1.1 scw #include <arm/xscale/ixp425var.h>
72 1.1 scw #include <arm/xscale/ixp425_qmgr.h>
73 1.1 scw #include <arm/xscale/ixp425_npevar.h>
74 1.1 scw #include <arm/xscale/ixp425_if_npereg.h>
75 1.1 scw
76 1.1 scw #include <dev/mii/miivar.h>
77 1.1 scw
78 1.1 scw #include "locators.h"
79 1.1 scw
80 1.1 scw struct npebuf {
81 1.1 scw struct npebuf *ix_next; /* chain to next buffer */
82 1.1 scw void *ix_m; /* backpointer to mbuf */
83 1.1 scw bus_dmamap_t ix_map; /* bus dma map for associated data */
84 1.1 scw struct npehwbuf *ix_hw; /* associated h/w block */
85 1.1 scw uint32_t ix_neaddr; /* phys address of ix_hw */
86 1.1 scw };
87 1.1 scw
88 1.1 scw struct npedma {
89 1.1 scw const char* name;
90 1.1 scw int nbuf; /* # npebuf's allocated */
91 1.1 scw bus_dmamap_t m_map;
92 1.1 scw struct npehwbuf *hwbuf; /* NPE h/w buffers */
93 1.1 scw bus_dmamap_t buf_map;
94 1.1 scw bus_addr_t buf_phys; /* phys addr of buffers */
95 1.1 scw struct npebuf *buf; /* s/w buffers (1-1 w/ h/w) */
96 1.1 scw };
97 1.1 scw
98 1.1 scw struct npe_softc {
99 1.23 matt device_t sc_dev;
100 1.1 scw struct ethercom sc_ethercom;
101 1.11 msaitoh uint8_t sc_enaddr[ETHER_ADDR_LEN];
102 1.1 scw struct mii_data sc_mii;
103 1.39 msaitoh bus_space_tag_t sc_iot;
104 1.1 scw bus_dma_tag_t sc_dt;
105 1.1 scw bus_space_handle_t sc_ioh; /* MAC register window */
106 1.1 scw bus_space_handle_t sc_miih; /* MII register window */
107 1.1 scw struct ixpnpe_softc *sc_npe; /* NPE support */
108 1.1 scw int sc_unit;
109 1.1 scw int sc_phy;
110 1.1 scw struct callout sc_tick_ch; /* Tick callout */
111 1.1 scw struct npedma txdma;
112 1.1 scw struct npebuf *tx_free; /* list of free tx buffers */
113 1.1 scw struct npedma rxdma;
114 1.1 scw int rx_qid; /* rx qid */
115 1.1 scw int rx_freeqid; /* rx free buffers qid */
116 1.1 scw int tx_qid; /* tx qid */
117 1.1 scw int tx_doneqid; /* tx completed qid */
118 1.1 scw struct npestats *sc_stats;
119 1.1 scw bus_dmamap_t sc_stats_map;
120 1.1 scw bus_addr_t sc_stats_phys; /* phys addr of sc_stats */
121 1.42 msaitoh u_short sc_if_flags; /* keep last if_flags */
122 1.21 tls krndsource_t rnd_source; /* random source */
123 1.1 scw };
124 1.1 scw
125 1.1 scw /*
126 1.1 scw * Per-unit static configuration for IXP425. The tx and
127 1.1 scw * rx free Q id's are fixed by the NPE microcode. The
128 1.1 scw * rx Q id's are programmed to be separate to simplify
129 1.1 scw * multi-port processing. It may be better to handle
130 1.1 scw * all traffic through one Q (as done by the Intel drivers).
131 1.1 scw *
132 1.1 scw * Note that the PHY's are accessible only from MAC A
133 1.1 scw * on the IXP425. This and other platform-specific
134 1.1 scw * assumptions probably need to be handled through hints.
135 1.1 scw */
136 1.1 scw static const struct {
137 1.1 scw const char *desc; /* device description */
138 1.1 scw int npeid; /* NPE assignment */
139 1.16 msaitoh int macport; /* Port number of the MAC */
140 1.1 scw uint32_t imageid; /* NPE firmware image id */
141 1.1 scw uint32_t regbase;
142 1.1 scw int regsize;
143 1.1 scw uint32_t miibase;
144 1.1 scw int miisize;
145 1.1 scw uint8_t rx_qid;
146 1.1 scw uint8_t rx_freeqid;
147 1.1 scw uint8_t tx_qid;
148 1.1 scw uint8_t tx_doneqid;
149 1.1 scw } npeconfig[NPE_PORTS_MAX] = {
150 1.1 scw { .desc = "IXP NPE-B",
151 1.1 scw .npeid = NPE_B,
152 1.16 msaitoh .macport = 0x10,
153 1.1 scw .imageid = IXP425_NPE_B_IMAGEID,
154 1.1 scw .regbase = IXP425_MAC_A_HWBASE,
155 1.1 scw .regsize = IXP425_MAC_A_SIZE,
156 1.1 scw .miibase = IXP425_MAC_A_HWBASE,
157 1.1 scw .miisize = IXP425_MAC_A_SIZE,
158 1.1 scw .rx_qid = 4,
159 1.1 scw .rx_freeqid = 27,
160 1.1 scw .tx_qid = 24,
161 1.1 scw .tx_doneqid = 31
162 1.1 scw },
163 1.1 scw { .desc = "IXP NPE-C",
164 1.1 scw .npeid = NPE_C,
165 1.16 msaitoh .macport = 0x20,
166 1.1 scw .imageid = IXP425_NPE_C_IMAGEID,
167 1.1 scw .regbase = IXP425_MAC_B_HWBASE,
168 1.1 scw .regsize = IXP425_MAC_B_SIZE,
169 1.1 scw .miibase = IXP425_MAC_A_HWBASE,
170 1.1 scw .miisize = IXP425_MAC_A_SIZE,
171 1.1 scw .rx_qid = 12,
172 1.1 scw .rx_freeqid = 28,
173 1.1 scw .tx_qid = 25,
174 1.1 scw .tx_doneqid = 31
175 1.1 scw },
176 1.1 scw };
177 1.1 scw static struct npe_softc *npes[NPE_MAX]; /* NB: indexed by npeid */
178 1.1 scw
179 1.1 scw static __inline uint32_t
180 1.1 scw RD4(struct npe_softc *sc, bus_size_t off)
181 1.1 scw {
182 1.1 scw return bus_space_read_4(sc->sc_iot, sc->sc_ioh, off);
183 1.1 scw }
184 1.1 scw
185 1.1 scw static __inline void
186 1.1 scw WR4(struct npe_softc *sc, bus_size_t off, uint32_t val)
187 1.1 scw {
188 1.1 scw bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
189 1.1 scw }
190 1.1 scw
191 1.1 scw static int npe_activate(struct npe_softc *);
192 1.1 scw #if 0
193 1.1 scw static void npe_deactivate(struct npe_softc *);
194 1.1 scw #endif
195 1.40 msaitoh static void npe_setmac(struct npe_softc *, const u_char *);
196 1.40 msaitoh static void npe_getmac(struct npe_softc *);
197 1.40 msaitoh static void npe_txdone(int, void *);
198 1.1 scw static int npe_rxbuf_init(struct npe_softc *, struct npebuf *,
199 1.1 scw struct mbuf *);
200 1.40 msaitoh static void npe_rxdone(int, void *);
201 1.13 msaitoh static void npeinit_macreg(struct npe_softc *);
202 1.1 scw static int npeinit(struct ifnet *);
203 1.16 msaitoh static void npeinit_resetcb(void *);
204 1.13 msaitoh static void npeinit_locked(void *);
205 1.1 scw static void npestart(struct ifnet *);
206 1.1 scw static void npestop(struct ifnet *, int);
207 1.1 scw static void npewatchdog(struct ifnet *);
208 1.40 msaitoh static int npeioctl(struct ifnet *, u_long, void *);
209 1.1 scw
210 1.40 msaitoh static int npe_setrxqosentry(struct npe_softc *, int, int, int);
211 1.1 scw static int npe_updatestats(struct npe_softc *);
212 1.1 scw #if 0
213 1.1 scw static int npe_getstats(struct npe_softc *);
214 1.1 scw static uint32_t npe_getimageid(struct npe_softc *);
215 1.40 msaitoh static int npe_setloopback(struct npe_softc *, int);
216 1.1 scw #endif
217 1.1 scw
218 1.36 msaitoh static int npe_miibus_readreg(device_t, int, int, uint16_t *);
219 1.36 msaitoh static int npe_miibus_writereg(device_t, int, int, uint16_t);
220 1.23 matt static void npe_miibus_statchg(struct ifnet *);
221 1.1 scw
222 1.1 scw static int npe_debug;
223 1.1 scw #define DPRINTF(sc, fmt, ...) do { \
224 1.1 scw if (npe_debug) printf(fmt, __VA_ARGS__); \
225 1.1 scw } while (0)
226 1.1 scw #define DPRINTFn(n, sc, fmt, ...) do { \
227 1.1 scw if (npe_debug >= n) printf(fmt, __VA_ARGS__); \
228 1.1 scw } while (0)
229 1.1 scw
230 1.1 scw #define NPE_TXBUF 128
231 1.1 scw #define NPE_RXBUF 64
232 1.1 scw
233 1.11 msaitoh #define MAC2UINT64(addr) (((uint64_t)addr[0] << 40) \
234 1.11 msaitoh + ((uint64_t)addr[1] << 32) \
235 1.11 msaitoh + ((uint64_t)addr[2] << 24) \
236 1.11 msaitoh + ((uint64_t)addr[3] << 16) \
237 1.11 msaitoh + ((uint64_t)addr[4] << 8) \
238 1.11 msaitoh + (uint64_t)addr[5])
239 1.11 msaitoh
240 1.1 scw /* NB: all tx done processing goes through one queue */
241 1.1 scw static int tx_doneqid = -1;
242 1.1 scw
243 1.11 msaitoh void (*npe_getmac_md)(int, uint8_t *);
244 1.11 msaitoh
245 1.23 matt static int npe_match(device_t, cfdata_t, void *);
246 1.23 matt static void npe_attach(device_t, device_t, void *);
247 1.1 scw
248 1.23 matt CFATTACH_DECL_NEW(npe, sizeof(struct npe_softc),
249 1.1 scw npe_match, npe_attach, NULL, NULL);
250 1.1 scw
251 1.1 scw static int
252 1.23 matt npe_match(device_t parent, cfdata_t cf, void *arg)
253 1.1 scw {
254 1.1 scw struct ixpnpe_attach_args *na = arg;
255 1.1 scw
256 1.1 scw return (na->na_unit == NPE_B || na->na_unit == NPE_C);
257 1.1 scw }
258 1.1 scw
259 1.1 scw static void
260 1.23 matt npe_attach(device_t parent, device_t self, void *arg)
261 1.1 scw {
262 1.23 matt struct npe_softc *sc = device_private(self);
263 1.23 matt struct ixpnpe_softc *isc = device_private(parent);
264 1.1 scw struct ixpnpe_attach_args *na = arg;
265 1.1 scw struct ifnet *ifp;
266 1.40 msaitoh struct mii_data * const mii = &sc->sc_mii;
267 1.1 scw
268 1.1 scw aprint_naive("\n");
269 1.1 scw aprint_normal(": Ethernet co-processor\n");
270 1.1 scw
271 1.23 matt sc->sc_dev = self;
272 1.1 scw sc->sc_iot = na->na_iot;
273 1.1 scw sc->sc_dt = na->na_dt;
274 1.1 scw sc->sc_npe = na->na_npe;
275 1.1 scw sc->sc_unit = (na->na_unit == NPE_B) ? 0 : 1;
276 1.1 scw sc->sc_phy = na->na_phy;
277 1.1 scw
278 1.1 scw memset(&sc->sc_ethercom, 0, sizeof(sc->sc_ethercom));
279 1.40 msaitoh memset(mii, 0, sizeof(*mii));
280 1.1 scw
281 1.4 ad callout_init(&sc->sc_tick_ch, 0);
282 1.1 scw
283 1.1 scw if (npe_activate(sc)) {
284 1.23 matt aprint_error_dev(sc->sc_dev,
285 1.23 matt "Failed to activate NPE (missing microcode?)\n");
286 1.1 scw return;
287 1.1 scw }
288 1.1 scw
289 1.14 msaitoh npe_getmac(sc);
290 1.13 msaitoh npeinit_macreg(sc);
291 1.1 scw
292 1.23 matt aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
293 1.11 msaitoh ether_sprintf(sc->sc_enaddr));
294 1.1 scw
295 1.1 scw ifp = &sc->sc_ethercom.ec_if;
296 1.40 msaitoh mii->mii_ifp = ifp;
297 1.40 msaitoh mii->mii_readreg = npe_miibus_readreg;
298 1.40 msaitoh mii->mii_writereg = npe_miibus_writereg;
299 1.40 msaitoh mii->mii_statchg = npe_miibus_statchg;
300 1.40 msaitoh sc->sc_ethercom.ec_mii = mii;
301 1.15 msaitoh
302 1.40 msaitoh ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
303 1.47 thorpej ether_mediastatus);
304 1.1 scw
305 1.40 msaitoh mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
306 1.15 msaitoh MII_OFFSET_ANY, MIIF_DOPAUSE);
307 1.40 msaitoh if (LIST_FIRST(&mii->mii_phys) == NULL) {
308 1.40 msaitoh ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
309 1.40 msaitoh ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
310 1.15 msaitoh } else
311 1.40 msaitoh ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
312 1.1 scw
313 1.1 scw ifp->if_softc = sc;
314 1.23 matt strcpy(ifp->if_xname, device_xname(sc->sc_dev));
315 1.1 scw ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
316 1.1 scw ifp->if_start = npestart;
317 1.1 scw ifp->if_ioctl = npeioctl;
318 1.1 scw ifp->if_watchdog = npewatchdog;
319 1.1 scw ifp->if_init = npeinit;
320 1.1 scw ifp->if_stop = npestop;
321 1.1 scw IFQ_SET_READY(&ifp->if_snd);
322 1.1 scw
323 1.10 msaitoh /* VLAN capable */
324 1.10 msaitoh sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
325 1.10 msaitoh
326 1.1 scw if_attach(ifp);
327 1.33 nonaka if_deferred_start_init(ifp, NULL);
328 1.11 msaitoh ether_ifattach(ifp, sc->sc_enaddr);
329 1.23 matt rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
330 1.26 tls RND_TYPE_NET, RND_FLAG_DEFAULT);
331 1.16 msaitoh
332 1.16 msaitoh /* callback function to reset MAC */
333 1.16 msaitoh isc->macresetcbfunc = npeinit_resetcb;
334 1.16 msaitoh isc->macresetcbarg = sc;
335 1.1 scw }
336 1.1 scw
337 1.1 scw /*
338 1.1 scw * Compute and install the multicast filter.
339 1.1 scw */
340 1.1 scw static void
341 1.1 scw npe_setmcast(struct npe_softc *sc)
342 1.1 scw {
343 1.40 msaitoh struct ethercom *ec = &sc->sc_ethercom;
344 1.40 msaitoh struct ifnet *ifp = &ec->ec_if;
345 1.1 scw uint8_t mask[ETHER_ADDR_LEN], addr[ETHER_ADDR_LEN];
346 1.11 msaitoh uint32_t reg;
347 1.16 msaitoh uint32_t msg[2];
348 1.1 scw int i;
349 1.1 scw
350 1.11 msaitoh /* Always use filter. Is here a correct position? */
351 1.11 msaitoh reg = RD4(sc, NPE_MAC_RX_CNTRL1);
352 1.11 msaitoh WR4(sc, NPE_MAC_RX_CNTRL1, reg | NPE_RX_CNTRL1_ADDR_FLTR_EN);
353 1.11 msaitoh
354 1.1 scw if (ifp->if_flags & IFF_PROMISC) {
355 1.1 scw memset(mask, 0, ETHER_ADDR_LEN);
356 1.1 scw memset(addr, 0, ETHER_ADDR_LEN);
357 1.1 scw } else if (ifp->if_flags & IFF_ALLMULTI) {
358 1.1 scw static const uint8_t allmulti[ETHER_ADDR_LEN] =
359 1.1 scw { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
360 1.1 scw all_multi:
361 1.1 scw memcpy(mask, allmulti, ETHER_ADDR_LEN);
362 1.1 scw memcpy(addr, allmulti, ETHER_ADDR_LEN);
363 1.1 scw } else {
364 1.1 scw uint8_t clr[ETHER_ADDR_LEN], set[ETHER_ADDR_LEN];
365 1.1 scw struct ether_multistep step;
366 1.1 scw struct ether_multi *enm;
367 1.1 scw
368 1.1 scw memset(clr, 0, ETHER_ADDR_LEN);
369 1.1 scw memset(set, 0xff, ETHER_ADDR_LEN);
370 1.1 scw
371 1.41 msaitoh ETHER_LOCK(ec);
372 1.40 msaitoh ETHER_FIRST_MULTI(step, ec, enm);
373 1.1 scw while (enm != NULL) {
374 1.39 msaitoh if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
375 1.39 msaitoh ETHER_ADDR_LEN)) {
376 1.1 scw ifp->if_flags |= IFF_ALLMULTI;
377 1.41 msaitoh ETHER_UNLOCK(ec);
378 1.1 scw goto all_multi;
379 1.1 scw }
380 1.1 scw
381 1.1 scw for (i = 0; i < ETHER_ADDR_LEN; i++) {
382 1.1 scw clr[i] |= enm->enm_addrlo[i];
383 1.1 scw set[i] &= enm->enm_addrlo[i];
384 1.1 scw }
385 1.1 scw
386 1.1 scw ETHER_NEXT_MULTI(step, enm);
387 1.1 scw }
388 1.41 msaitoh ETHER_UNLOCK(ec);
389 1.1 scw
390 1.1 scw for (i = 0; i < ETHER_ADDR_LEN; i++) {
391 1.1 scw mask[i] = set[i] | ~clr[i];
392 1.1 scw addr[i] = set[i];
393 1.1 scw }
394 1.1 scw }
395 1.1 scw
396 1.1 scw /*
397 1.1 scw * Write the mask and address registers.
398 1.1 scw */
399 1.1 scw for (i = 0; i < ETHER_ADDR_LEN; i++) {
400 1.1 scw WR4(sc, NPE_MAC_ADDR_MASK(i), mask[i]);
401 1.1 scw WR4(sc, NPE_MAC_ADDR(i), addr[i]);
402 1.1 scw }
403 1.16 msaitoh
404 1.16 msaitoh msg[0] = NPE_ADDRESSFILTERCONFIG << NPE_MAC_MSGID_SHL
405 1.16 msaitoh | (npeconfig[sc->sc_unit].macport << NPE_MAC_PORTID_SHL);
406 1.16 msaitoh msg[1] = ((ifp->if_flags & IFF_PROMISC) ? 1 : 0) << 24
407 1.16 msaitoh | ((RD4(sc, NPE_MAC_UNI_ADDR_6) & 0xff) << 16)
408 1.16 msaitoh | (addr[5] << 8) | mask[5];
409 1.16 msaitoh ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
410 1.1 scw }
411 1.1 scw
412 1.1 scw static int
413 1.1 scw npe_dma_setup(struct npe_softc *sc, struct npedma *dma,
414 1.1 scw const char *name, int nbuf, int maxseg)
415 1.1 scw {
416 1.1 scw bus_dma_segment_t seg;
417 1.1 scw int rseg, error, i;
418 1.3 christos void *hwbuf;
419 1.1 scw size_t size;
420 1.1 scw
421 1.10 msaitoh memset(dma, 0, sizeof(*dma));
422 1.1 scw
423 1.1 scw dma->name = name;
424 1.1 scw dma->nbuf = nbuf;
425 1.1 scw
426 1.1 scw size = nbuf * sizeof(struct npehwbuf);
427 1.1 scw
428 1.1 scw /* XXX COHERENT for now */
429 1.1 scw error = bus_dmamem_alloc(sc->sc_dt, size, sizeof(uint32_t), 0, &seg,
430 1.1 scw 1, &rseg, BUS_DMA_NOWAIT);
431 1.1 scw if (error) {
432 1.23 matt aprint_error_dev(sc->sc_dev,
433 1.23 matt "unable to %s for %s %s buffers, error %u\n",
434 1.23 matt "allocate memory", dma->name, "h/w", error);
435 1.1 scw }
436 1.1 scw
437 1.1 scw error = bus_dmamem_map(sc->sc_dt, &seg, 1, size, &hwbuf,
438 1.1 scw BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE);
439 1.1 scw if (error) {
440 1.23 matt aprint_error_dev(sc->sc_dev,
441 1.23 matt "unable to %s for %s %s buffers, error %u\n",
442 1.23 matt "map memory", dma->name, "h/w", error);
443 1.1 scw free_dmamem:
444 1.1 scw bus_dmamem_free(sc->sc_dt, &seg, rseg);
445 1.1 scw return error;
446 1.1 scw }
447 1.1 scw dma->hwbuf = (void *)hwbuf;
448 1.1 scw
449 1.1 scw error = bus_dmamap_create(sc->sc_dt, size, 1, size, 0,
450 1.1 scw BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &dma->buf_map);
451 1.1 scw if (error) {
452 1.23 matt aprint_error_dev(sc->sc_dev,
453 1.23 matt "unable to %s for %s %s buffers, error %u\n",
454 1.23 matt "create map", dma->name, "h/w", error);
455 1.1 scw unmap_dmamem:
456 1.1 scw dma->hwbuf = NULL;
457 1.1 scw bus_dmamem_unmap(sc->sc_dt, hwbuf, size);
458 1.1 scw goto free_dmamem;
459 1.1 scw }
460 1.1 scw
461 1.1 scw error = bus_dmamap_load(sc->sc_dt, dma->buf_map, hwbuf, size, NULL,
462 1.1 scw BUS_DMA_NOWAIT);
463 1.1 scw if (error) {
464 1.23 matt aprint_error_dev(sc->sc_dev,
465 1.23 matt "unable to %s for %s %s buffers, error %u\n",
466 1.23 matt "load map", dma->name, "h/w", error);
467 1.1 scw bus_dmamap_destroy(sc->sc_dt, dma->buf_map);
468 1.1 scw goto unmap_dmamem;
469 1.1 scw }
470 1.1 scw
471 1.1 scw /* XXX M_TEMP */
472 1.39 msaitoh dma->buf = malloc(nbuf * sizeof(struct npebuf), M_TEMP,
473 1.43 chs M_WAITOK | M_ZERO);
474 1.1 scw dma->buf_phys = dma->buf_map->dm_segs[0].ds_addr;
475 1.1 scw for (i = 0; i < dma->nbuf; i++) {
476 1.1 scw struct npebuf *npe = &dma->buf[i];
477 1.1 scw struct npehwbuf *hw = &dma->hwbuf[i];
478 1.1 scw
479 1.39 msaitoh /* Calculate offset to shared area */
480 1.1 scw npe->ix_neaddr = dma->buf_phys +
481 1.1 scw ((uintptr_t)hw - (uintptr_t)dma->hwbuf);
482 1.1 scw KASSERT((npe->ix_neaddr & 0x1f) == 0);
483 1.10 msaitoh error = bus_dmamap_create(sc->sc_dt, MCLBYTES, maxseg,
484 1.1 scw MCLBYTES, 0, 0, &npe->ix_map);
485 1.1 scw if (error != 0) {
486 1.23 matt aprint_error_dev(sc->sc_dev,
487 1.23 matt "unable to %s for %s buffer %u, error %u\n",
488 1.23 matt "create dmamap", dma->name, i, error);
489 1.1 scw /* XXXSCW: Free up maps... */
490 1.1 scw return error;
491 1.1 scw }
492 1.1 scw npe->ix_hw = hw;
493 1.1 scw }
494 1.1 scw bus_dmamap_sync(sc->sc_dt, dma->buf_map, 0, dma->buf_map->dm_mapsize,
495 1.1 scw BUS_DMASYNC_PREWRITE);
496 1.1 scw return 0;
497 1.1 scw }
498 1.1 scw
499 1.1 scw #if 0
500 1.1 scw static void
501 1.1 scw npe_dma_destroy(struct npe_softc *sc, struct npedma *dma)
502 1.1 scw {
503 1.1 scw int i;
504 1.1 scw
505 1.1 scw /* XXXSCW: Clean this up */
506 1.1 scw
507 1.1 scw if (dma->hwbuf != NULL) {
508 1.1 scw for (i = 0; i < dma->nbuf; i++) {
509 1.1 scw struct npebuf *npe = &dma->buf[i];
510 1.1 scw bus_dmamap_destroy(sc->sc_dt, npe->ix_map);
511 1.1 scw }
512 1.1 scw bus_dmamap_unload(sc->sc_dt, dma->buf_map);
513 1.3 christos bus_dmamem_free(sc->sc_dt, (void *)dma->hwbuf, dma->buf_map);
514 1.1 scw bus_dmamap_destroy(sc->sc_dt, dma->buf_map);
515 1.1 scw }
516 1.1 scw if (dma->buf != NULL)
517 1.1 scw free(dma->buf, M_TEMP);
518 1.1 scw memset(dma, 0, sizeof(*dma));
519 1.1 scw }
520 1.1 scw #endif
521 1.1 scw
522 1.1 scw static int
523 1.1 scw npe_activate(struct npe_softc *sc)
524 1.1 scw {
525 1.1 scw bus_dma_segment_t seg;
526 1.1 scw int unit = sc->sc_unit;
527 1.1 scw int error, i, rseg;
528 1.3 christos void *statbuf;
529 1.1 scw
530 1.1 scw /* load NPE firmware and start it running */
531 1.1 scw error = ixpnpe_init(sc->sc_npe, "npe_fw", npeconfig[unit].imageid);
532 1.1 scw if (error != 0)
533 1.1 scw return error;
534 1.1 scw
535 1.1 scw if (bus_space_map(sc->sc_iot, npeconfig[unit].regbase,
536 1.1 scw npeconfig[unit].regsize, 0, &sc->sc_ioh)) {
537 1.23 matt aprint_error_dev(sc->sc_dev, "Cannot map registers 0x%x:0x%x\n",
538 1.23 matt npeconfig[unit].regbase, npeconfig[unit].regsize);
539 1.1 scw return ENOMEM;
540 1.1 scw }
541 1.1 scw
542 1.1 scw if (npeconfig[unit].miibase != npeconfig[unit].regbase) {
543 1.1 scw /*
544 1.1 scw * The PHY's are only accessible from one MAC (it appears)
545 1.1 scw * so for other MAC's setup an additional mapping for
546 1.1 scw * frobbing the PHY registers.
547 1.1 scw */
548 1.1 scw if (bus_space_map(sc->sc_iot, npeconfig[unit].miibase,
549 1.1 scw npeconfig[unit].miisize, 0, &sc->sc_miih)) {
550 1.23 matt aprint_error_dev(sc->sc_dev,
551 1.23 matt "Cannot map MII registers 0x%x:0x%x\n",
552 1.23 matt npeconfig[unit].miibase, npeconfig[unit].miisize);
553 1.1 scw return ENOMEM;
554 1.1 scw }
555 1.1 scw } else
556 1.1 scw sc->sc_miih = sc->sc_ioh;
557 1.1 scw error = npe_dma_setup(sc, &sc->txdma, "tx", NPE_TXBUF, NPE_MAXSEG);
558 1.1 scw if (error != 0)
559 1.1 scw return error;
560 1.1 scw error = npe_dma_setup(sc, &sc->rxdma, "rx", NPE_RXBUF, 1);
561 1.1 scw if (error != 0)
562 1.1 scw return error;
563 1.1 scw
564 1.1 scw /* setup statistics block */
565 1.1 scw error = bus_dmamem_alloc(sc->sc_dt, sizeof(struct npestats),
566 1.1 scw sizeof(uint32_t), 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
567 1.1 scw if (error) {
568 1.23 matt aprint_error_dev(sc->sc_dev,
569 1.23 matt "unable to %s for %s, error %u\n",
570 1.23 matt "allocate memory", "stats block", error);
571 1.1 scw return error;
572 1.1 scw }
573 1.1 scw
574 1.1 scw error = bus_dmamem_map(sc->sc_dt, &seg, 1, sizeof(struct npestats),
575 1.1 scw &statbuf, BUS_DMA_NOWAIT);
576 1.1 scw if (error) {
577 1.23 matt aprint_error_dev(sc->sc_dev,
578 1.23 matt "unable to %s for %s, error %u\n",
579 1.23 matt "map memory", "stats block", error);
580 1.1 scw return error;
581 1.1 scw }
582 1.1 scw sc->sc_stats = (void *)statbuf;
583 1.1 scw
584 1.1 scw error = bus_dmamap_create(sc->sc_dt, sizeof(struct npestats), 1,
585 1.1 scw sizeof(struct npestats), 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
586 1.1 scw &sc->sc_stats_map);
587 1.1 scw if (error) {
588 1.23 matt aprint_error_dev(sc->sc_dev,
589 1.23 matt "unable to %s for %s, error %u\n",
590 1.23 matt "create map", "stats block", error);
591 1.1 scw return error;
592 1.1 scw }
593 1.1 scw
594 1.29 maxv error = bus_dmamap_load(sc->sc_dt, sc->sc_stats_map, sc->sc_stats,
595 1.29 maxv sizeof(struct npestats), NULL, BUS_DMA_NOWAIT);
596 1.29 maxv if (error) {
597 1.23 matt aprint_error_dev(sc->sc_dev,
598 1.23 matt "unable to %s for %s, error %u\n",
599 1.23 matt "load map", "stats block", error);
600 1.1 scw return error;
601 1.1 scw }
602 1.1 scw sc->sc_stats_phys = sc->sc_stats_map->dm_segs[0].ds_addr;
603 1.1 scw
604 1.1 scw /* XXX disable half-bridge LEARNING+FILTERING feature */
605 1.1 scw
606 1.1 scw /*
607 1.1 scw * Setup h/w rx/tx queues. There are four q's:
608 1.1 scw * rx inbound q of rx'd frames
609 1.1 scw * rx_free pool of ixpbuf's for receiving frames
610 1.1 scw * tx outbound q of frames to send
611 1.1 scw * tx_done q of tx frames that have been processed
612 1.1 scw *
613 1.1 scw * The NPE handles the actual tx/rx process and the q manager
614 1.1 scw * handles the queues. The driver just writes entries to the
615 1.1 scw * q manager mailbox's and gets callbacks when there are rx'd
616 1.1 scw * frames to process or tx'd frames to reap. These callbacks
617 1.1 scw * are controlled by the q configurations; e.g. we get a
618 1.1 scw * callback when tx_done has 2 or more frames to process and
619 1.48 andvar * when the rx q has at least one frame. These settings can
620 1.1 scw * changed at the time the q is configured.
621 1.1 scw */
622 1.1 scw sc->rx_qid = npeconfig[unit].rx_qid;
623 1.1 scw ixpqmgr_qconfig(sc->rx_qid, NPE_RXBUF, 0, 1,
624 1.1 scw IX_QMGR_Q_SOURCE_ID_NOT_E, npe_rxdone, sc);
625 1.1 scw sc->rx_freeqid = npeconfig[unit].rx_freeqid;
626 1.1 scw ixpqmgr_qconfig(sc->rx_freeqid, NPE_RXBUF, 0, NPE_RXBUF/2, 0, NULL, sc);
627 1.1 scw /* tell the NPE to direct all traffic to rx_qid */
628 1.1 scw #if 0
629 1.1 scw for (i = 0; i < 8; i++)
630 1.1 scw #else
631 1.23 matt printf("%s: remember to fix rx q setup\n", device_xname(sc->sc_dev));
632 1.1 scw for (i = 0; i < 4; i++)
633 1.1 scw #endif
634 1.1 scw npe_setrxqosentry(sc, i, 0, sc->rx_qid);
635 1.1 scw
636 1.1 scw sc->tx_qid = npeconfig[unit].tx_qid;
637 1.1 scw sc->tx_doneqid = npeconfig[unit].tx_doneqid;
638 1.1 scw ixpqmgr_qconfig(sc->tx_qid, NPE_TXBUF, 0, NPE_TXBUF, 0, NULL, sc);
639 1.1 scw if (tx_doneqid == -1) {
640 1.1 scw ixpqmgr_qconfig(sc->tx_doneqid, NPE_TXBUF, 0, 2,
641 1.1 scw IX_QMGR_Q_SOURCE_ID_NOT_E, npe_txdone, sc);
642 1.1 scw tx_doneqid = sc->tx_doneqid;
643 1.1 scw }
644 1.1 scw
645 1.1 scw KASSERT(npes[npeconfig[unit].npeid] == NULL);
646 1.1 scw npes[npeconfig[unit].npeid] = sc;
647 1.1 scw
648 1.1 scw return 0;
649 1.1 scw }
650 1.1 scw
651 1.1 scw #if 0
652 1.1 scw static void
653 1.1 scw npe_deactivate(struct npe_softc *sc);
654 1.1 scw {
655 1.1 scw int unit = sc->sc_unit;
656 1.1 scw
657 1.1 scw npes[npeconfig[unit].npeid] = NULL;
658 1.1 scw
659 1.1 scw /* XXX disable q's */
660 1.1 scw if (sc->sc_npe != NULL)
661 1.1 scw ixpnpe_stop(sc->sc_npe);
662 1.1 scw if (sc->sc_stats != NULL) {
663 1.1 scw bus_dmamap_unload(sc->sc_stats_tag, sc->sc_stats_map);
664 1.1 scw bus_dmamem_free(sc->sc_stats_tag, sc->sc_stats,
665 1.1 scw sc->sc_stats_map);
666 1.1 scw bus_dmamap_destroy(sc->sc_stats_tag, sc->sc_stats_map);
667 1.1 scw }
668 1.1 scw if (sc->sc_stats_tag != NULL)
669 1.1 scw bus_dma_tag_destroy(sc->sc_stats_tag);
670 1.1 scw npe_dma_destroy(sc, &sc->txdma);
671 1.1 scw npe_dma_destroy(sc, &sc->rxdma);
672 1.1 scw bus_generic_detach(sc->sc_dev);
673 1.46 thorpej XXX ifmedia_fini somewhere
674 1.1 scw if (sc->sc_mii)
675 1.1 scw device_delete_child(sc->sc_dev, sc->sc_mii);
676 1.1 scw #if 0
677 1.1 scw /* XXX sc_ioh and sc_miih */
678 1.1 scw if (sc->mem_res)
679 1.1 scw bus_release_resource(dev, SYS_RES_IOPORT,
680 1.1 scw rman_get_rid(sc->mem_res), sc->mem_res);
681 1.1 scw sc->mem_res = 0;
682 1.1 scw #endif
683 1.1 scw }
684 1.1 scw #endif
685 1.1 scw
686 1.1 scw static void
687 1.1 scw npe_addstats(struct npe_softc *sc)
688 1.1 scw {
689 1.1 scw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
690 1.1 scw struct npestats *ns = sc->sc_stats;
691 1.1 scw
692 1.45 thorpej net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
693 1.45 thorpej if_statadd_ref(nsr, if_oerrors,
694 1.1 scw be32toh(ns->dot3StatsInternalMacTransmitErrors)
695 1.1 scw + be32toh(ns->dot3StatsCarrierSenseErrors)
696 1.1 scw + be32toh(ns->TxVLANIdFilterDiscards)
697 1.45 thorpej );
698 1.45 thorpej if_statadd_ref(nsr, if_ierrors,
699 1.45 thorpej be32toh(ns->dot3StatsFCSErrors)
700 1.1 scw + be32toh(ns->dot3StatsInternalMacReceiveErrors)
701 1.1 scw + be32toh(ns->RxOverrunDiscards)
702 1.1 scw + be32toh(ns->RxUnderflowEntryDiscards)
703 1.45 thorpej );
704 1.45 thorpej if_statadd_ref(nsr, if_collisions,
705 1.1 scw be32toh(ns->dot3StatsSingleCollisionFrames)
706 1.1 scw + be32toh(ns->dot3StatsMultipleCollisionFrames)
707 1.45 thorpej );
708 1.45 thorpej IF_STAT_PUTREF(ifp);
709 1.1 scw }
710 1.1 scw
711 1.1 scw static void
712 1.1 scw npe_tick(void *xsc)
713 1.1 scw {
714 1.1 scw #define ACK (NPE_RESETSTATS << NPE_MAC_MSGID_SHL)
715 1.1 scw struct npe_softc *sc = xsc;
716 1.1 scw uint32_t msg[2];
717 1.1 scw
718 1.1 scw /*
719 1.1 scw * NB: to avoid sleeping with the softc lock held we
720 1.1 scw * split the NPE msg processing into two parts. The
721 1.1 scw * request for statistics is sent w/o waiting for a
722 1.1 scw * reply and then on the next tick we retrieve the
723 1.1 scw * results. This works because npe_tick is the only
724 1.1 scw * code that talks via the mailbox's (except at setup).
725 1.1 scw * This likely can be handled better.
726 1.1 scw */
727 1.1 scw if (ixpnpe_recvmsg(sc->sc_npe, msg) == 0 && msg[0] == ACK) {
728 1.1 scw bus_dmamap_sync(sc->sc_dt, sc->sc_stats_map, 0,
729 1.1 scw sizeof(struct npestats), BUS_DMASYNC_POSTREAD);
730 1.1 scw npe_addstats(sc);
731 1.1 scw }
732 1.1 scw npe_updatestats(sc);
733 1.1 scw mii_tick(&sc->sc_mii);
734 1.1 scw
735 1.39 msaitoh /* Schedule next poll */
736 1.1 scw callout_reset(&sc->sc_tick_ch, hz, npe_tick, sc);
737 1.1 scw #undef ACK
738 1.1 scw }
739 1.1 scw
740 1.1 scw static void
741 1.6 matt npe_setmac(struct npe_softc *sc, const u_char *eaddr)
742 1.1 scw {
743 1.11 msaitoh
744 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_1, eaddr[0]);
745 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_2, eaddr[1]);
746 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_3, eaddr[2]);
747 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_4, eaddr[3]);
748 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_5, eaddr[4]);
749 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_6, eaddr[5]);
750 1.1 scw }
751 1.1 scw
752 1.1 scw static void
753 1.11 msaitoh npe_getmac(struct npe_softc *sc)
754 1.1 scw {
755 1.11 msaitoh uint8_t *eaddr = sc->sc_enaddr;
756 1.11 msaitoh
757 1.11 msaitoh if (npe_getmac_md != NULL) {
758 1.23 matt (*npe_getmac_md)(device_unit(sc->sc_dev), eaddr);
759 1.11 msaitoh } else {
760 1.11 msaitoh /*
761 1.11 msaitoh * Some system's unicast address appears to be loaded from
762 1.11 msaitoh * EEPROM on reset
763 1.11 msaitoh */
764 1.11 msaitoh eaddr[0] = RD4(sc, NPE_MAC_UNI_ADDR_1) & 0xff;
765 1.11 msaitoh eaddr[1] = RD4(sc, NPE_MAC_UNI_ADDR_2) & 0xff;
766 1.11 msaitoh eaddr[2] = RD4(sc, NPE_MAC_UNI_ADDR_3) & 0xff;
767 1.11 msaitoh eaddr[3] = RD4(sc, NPE_MAC_UNI_ADDR_4) & 0xff;
768 1.11 msaitoh eaddr[4] = RD4(sc, NPE_MAC_UNI_ADDR_5) & 0xff;
769 1.11 msaitoh eaddr[5] = RD4(sc, NPE_MAC_UNI_ADDR_6) & 0xff;
770 1.11 msaitoh }
771 1.1 scw }
772 1.1 scw
773 1.1 scw struct txdone {
774 1.1 scw struct npebuf *head;
775 1.1 scw struct npebuf **tail;
776 1.1 scw int count;
777 1.1 scw };
778 1.1 scw
779 1.1 scw static __inline void
780 1.1 scw npe_txdone_finish(struct npe_softc *sc, const struct txdone *td)
781 1.1 scw {
782 1.1 scw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
783 1.1 scw
784 1.1 scw *td->tail = sc->tx_free;
785 1.1 scw sc->tx_free = td->head;
786 1.1 scw /*
787 1.1 scw * We're no longer busy, so clear the busy flag and call the
788 1.1 scw * start routine to xmit more packets.
789 1.1 scw */
790 1.45 thorpej if_statadd(ifp, if_opackets, td->count);
791 1.1 scw ifp->if_flags &= ~IFF_OACTIVE;
792 1.1 scw ifp->if_timer = 0;
793 1.33 nonaka if_schedule_deferred_start(ifp);
794 1.1 scw }
795 1.1 scw
796 1.1 scw /*
797 1.1 scw * Q manager callback on tx done queue. Reap mbufs
798 1.1 scw * and return tx buffers to the free list. Finally
799 1.1 scw * restart output. Note the microcode has only one
800 1.1 scw * txdone q wired into it so we must use the NPE ID
801 1.1 scw * returned with each npehwbuf to decide where to
802 1.1 scw * send buffers.
803 1.1 scw */
804 1.1 scw static void
805 1.1 scw npe_txdone(int qid, void *arg)
806 1.1 scw {
807 1.1 scw #define P2V(a, dma) \
808 1.1 scw &(dma)->buf[((a) - (dma)->buf_phys) / sizeof(struct npehwbuf)]
809 1.1 scw struct npe_softc *sc;
810 1.1 scw struct npebuf *npe;
811 1.1 scw struct txdone *td, q[NPE_MAX];
812 1.1 scw uint32_t entry;
813 1.1 scw
814 1.1 scw /* XXX no NPE-A support */
815 1.1 scw q[NPE_B].tail = &q[NPE_B].head; q[NPE_B].count = 0;
816 1.1 scw q[NPE_C].tail = &q[NPE_C].head; q[NPE_C].count = 0;
817 1.1 scw /* XXX max # at a time? */
818 1.1 scw while (ixpqmgr_qread(qid, &entry) == 0) {
819 1.1 scw sc = npes[NPE_QM_Q_NPE(entry)];
820 1.1 scw DPRINTF(sc, "%s: entry 0x%x NPE %u port %u\n",
821 1.1 scw __func__, entry, NPE_QM_Q_NPE(entry), NPE_QM_Q_PORT(entry));
822 1.22 tls rnd_add_uint32(&sc->rnd_source, entry);
823 1.1 scw
824 1.1 scw npe = P2V(NPE_QM_Q_ADDR(entry), &sc->txdma);
825 1.1 scw m_freem(npe->ix_m);
826 1.1 scw npe->ix_m = NULL;
827 1.1 scw
828 1.1 scw td = &q[NPE_QM_Q_NPE(entry)];
829 1.1 scw *td->tail = npe;
830 1.1 scw td->tail = &npe->ix_next;
831 1.1 scw td->count++;
832 1.1 scw }
833 1.1 scw
834 1.1 scw if (q[NPE_B].count)
835 1.1 scw npe_txdone_finish(npes[NPE_B], &q[NPE_B]);
836 1.1 scw if (q[NPE_C].count)
837 1.1 scw npe_txdone_finish(npes[NPE_C], &q[NPE_C]);
838 1.1 scw #undef P2V
839 1.1 scw }
840 1.1 scw
841 1.1 scw static __inline struct mbuf *
842 1.1 scw npe_getcl(void)
843 1.1 scw {
844 1.1 scw struct mbuf *m;
845 1.1 scw
846 1.1 scw MGETHDR(m, M_DONTWAIT, MT_DATA);
847 1.1 scw if (m != NULL) {
848 1.1 scw MCLGET(m, M_DONTWAIT);
849 1.1 scw if ((m->m_flags & M_EXT) == 0) {
850 1.1 scw m_freem(m);
851 1.1 scw m = NULL;
852 1.1 scw }
853 1.1 scw }
854 1.39 msaitoh return m;
855 1.1 scw }
856 1.1 scw
857 1.1 scw static int
858 1.1 scw npe_rxbuf_init(struct npe_softc *sc, struct npebuf *npe, struct mbuf *m)
859 1.1 scw {
860 1.1 scw struct npehwbuf *hw;
861 1.1 scw int error;
862 1.1 scw
863 1.1 scw if (m == NULL) {
864 1.1 scw m = npe_getcl();
865 1.1 scw if (m == NULL)
866 1.1 scw return ENOBUFS;
867 1.1 scw }
868 1.11 msaitoh KASSERT(m->m_ext.ext_size >= (NPE_FRAME_SIZE_DEFAULT + ETHER_ALIGN));
869 1.11 msaitoh m->m_pkthdr.len = m->m_len = NPE_FRAME_SIZE_DEFAULT;
870 1.1 scw /* backload payload and align ip hdr */
871 1.11 msaitoh m->m_data = m->m_ext.ext_buf + (m->m_ext.ext_size
872 1.11 msaitoh - (NPE_FRAME_SIZE_DEFAULT + ETHER_ALIGN));
873 1.1 scw error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map, m,
874 1.40 msaitoh BUS_DMA_READ | BUS_DMA_NOWAIT);
875 1.1 scw if (error != 0) {
876 1.1 scw m_freem(m);
877 1.1 scw return error;
878 1.1 scw }
879 1.1 scw hw = npe->ix_hw;
880 1.1 scw hw->ix_ne[0].data = htobe32(npe->ix_map->dm_segs[0].ds_addr);
881 1.1 scw /* NB: NPE requires length be a multiple of 64 */
882 1.1 scw /* NB: buffer length is shifted in word */
883 1.1 scw hw->ix_ne[0].len = htobe32(npe->ix_map->dm_segs[0].ds_len << 16);
884 1.1 scw hw->ix_ne[0].next = 0;
885 1.1 scw npe->ix_m = m;
886 1.1 scw /* Flush the memory in the mbuf */
887 1.1 scw bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0, npe->ix_map->dm_mapsize,
888 1.1 scw BUS_DMASYNC_PREREAD);
889 1.1 scw return 0;
890 1.1 scw }
891 1.1 scw
892 1.1 scw /*
893 1.1 scw * RX q processing for a specific NPE. Claim entries
894 1.1 scw * from the hardware queue and pass the frames up the
895 1.1 scw * stack. Pass the rx buffers to the free list.
896 1.1 scw */
897 1.1 scw static void
898 1.1 scw npe_rxdone(int qid, void *arg)
899 1.1 scw {
900 1.1 scw #define P2V(a, dma) \
901 1.1 scw &(dma)->buf[((a) - (dma)->buf_phys) / sizeof(struct npehwbuf)]
902 1.1 scw struct npe_softc *sc = arg;
903 1.1 scw struct npedma *dma = &sc->rxdma;
904 1.1 scw uint32_t entry;
905 1.1 scw
906 1.1 scw while (ixpqmgr_qread(qid, &entry) == 0) {
907 1.1 scw struct npebuf *npe = P2V(NPE_QM_Q_ADDR(entry), dma);
908 1.1 scw struct mbuf *m;
909 1.1 scw
910 1.1 scw DPRINTF(sc, "%s: entry 0x%x neaddr 0x%x ne_len 0x%x\n",
911 1.1 scw __func__, entry, npe->ix_neaddr, npe->ix_hw->ix_ne[0].len);
912 1.22 tls rnd_add_uint32(&sc->rnd_source, entry);
913 1.1 scw /*
914 1.1 scw * Allocate a new mbuf to replenish the rx buffer.
915 1.1 scw * If doing so fails we drop the rx'd frame so we
916 1.1 scw * can reuse the previous mbuf. When we're able to
917 1.1 scw * allocate a new mbuf dispatch the mbuf w/ rx'd
918 1.1 scw * data up the stack and replace it with the newly
919 1.1 scw * allocated one.
920 1.1 scw */
921 1.1 scw m = npe_getcl();
922 1.1 scw if (m != NULL) {
923 1.1 scw struct mbuf *mrx = npe->ix_m;
924 1.1 scw struct npehwbuf *hw = npe->ix_hw;
925 1.1 scw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
926 1.1 scw
927 1.1 scw /* Flush mbuf memory for rx'd data */
928 1.1 scw bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0,
929 1.1 scw npe->ix_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
930 1.1 scw
931 1.1 scw /* XXX flush hw buffer; works now 'cuz coherent */
932 1.1 scw /* set m_len etc. per rx frame size */
933 1.1 scw mrx->m_len = be32toh(hw->ix_ne[0].len) & 0xffff;
934 1.1 scw mrx->m_pkthdr.len = mrx->m_len;
935 1.31 ozaki m_set_rcvif(mrx, ifp);
936 1.11 msaitoh /* Don't add M_HASFCS. See below */
937 1.11 msaitoh
938 1.11 msaitoh #if 1
939 1.11 msaitoh if (mrx->m_pkthdr.len < sizeof(struct ether_header)) {
940 1.11 msaitoh log(LOG_INFO, "%s: too short frame (len=%d)\n",
941 1.39 msaitoh device_xname(sc->sc_dev),
942 1.39 msaitoh mrx->m_pkthdr.len);
943 1.11 msaitoh /* Back out "newly allocated" mbuf. */
944 1.11 msaitoh m_freem(m);
945 1.45 thorpej if_statinc(ifp, if_ierrors);
946 1.11 msaitoh goto fail;
947 1.11 msaitoh }
948 1.11 msaitoh if ((ifp->if_flags & IFF_PROMISC) == 0) {
949 1.11 msaitoh struct ether_header *eh;
950 1.11 msaitoh
951 1.11 msaitoh /*
952 1.11 msaitoh * Workaround for "Non-Intel XScale Technology
953 1.11 msaitoh * Eratta" No. 29. AA:BB:CC:DD:EE:xF's packet
954 1.11 msaitoh * matches the filter (both unicast and
955 1.11 msaitoh * multicast).
956 1.11 msaitoh */
957 1.11 msaitoh eh = mtod(mrx, struct ether_header *);
958 1.11 msaitoh if (ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
959 1.39 msaitoh /* Unicast */
960 1.11 msaitoh
961 1.11 msaitoh if (sc->sc_enaddr[5] != eh->ether_dhost[5]) {
962 1.39 msaitoh /* Discard it */
963 1.11 msaitoh #if 0
964 1.11 msaitoh printf("discard it\n");
965 1.11 msaitoh #endif
966 1.11 msaitoh /*
967 1.11 msaitoh * Back out "newly allocated"
968 1.11 msaitoh * mbuf.
969 1.11 msaitoh */
970 1.11 msaitoh m_freem(m);
971 1.11 msaitoh goto fail;
972 1.11 msaitoh }
973 1.11 msaitoh } else if (memcmp(eh->ether_dhost,
974 1.11 msaitoh etherbroadcastaddr, 6) == 0) {
975 1.11 msaitoh /* Always accept broadcast packet*/
976 1.11 msaitoh } else {
977 1.11 msaitoh struct ethercom *ec = &sc->sc_ethercom;
978 1.11 msaitoh struct ether_multi *enm;
979 1.11 msaitoh struct ether_multistep step;
980 1.11 msaitoh int match = 0;
981 1.11 msaitoh
982 1.39 msaitoh /* Multicast */
983 1.11 msaitoh
984 1.41 msaitoh ETHER_LOCK(ec);
985 1.11 msaitoh ETHER_FIRST_MULTI(step, ec, enm);
986 1.11 msaitoh while (enm != NULL) {
987 1.11 msaitoh uint64_t lowint, highint, dest;
988 1.11 msaitoh
989 1.11 msaitoh lowint = MAC2UINT64(enm->enm_addrlo);
990 1.11 msaitoh highint = MAC2UINT64(enm->enm_addrhi);
991 1.11 msaitoh dest = MAC2UINT64(eh->ether_dhost);
992 1.11 msaitoh #if 0
993 1.11 msaitoh printf("%llx\n", lowint);
994 1.11 msaitoh printf("%llx\n", dest);
995 1.11 msaitoh printf("%llx\n", highint);
996 1.11 msaitoh #endif
997 1.11 msaitoh if ((lowint <= dest) && (dest <= highint)) {
998 1.11 msaitoh match = 1;
999 1.11 msaitoh break;
1000 1.11 msaitoh }
1001 1.11 msaitoh ETHER_NEXT_MULTI(step, enm);
1002 1.11 msaitoh }
1003 1.41 msaitoh ETHER_UNLOCK(ec);
1004 1.41 msaitoh
1005 1.11 msaitoh if (match == 0) {
1006 1.39 msaitoh /* Discard it */
1007 1.11 msaitoh #if 0
1008 1.11 msaitoh printf("discard it(M)\n");
1009 1.11 msaitoh #endif
1010 1.11 msaitoh /*
1011 1.11 msaitoh * Back out "newly allocated"
1012 1.11 msaitoh * mbuf.
1013 1.11 msaitoh */
1014 1.11 msaitoh m_freem(m);
1015 1.11 msaitoh goto fail;
1016 1.11 msaitoh }
1017 1.11 msaitoh }
1018 1.11 msaitoh }
1019 1.11 msaitoh if (mrx->m_pkthdr.len > NPE_FRAME_SIZE_DEFAULT) {
1020 1.11 msaitoh log(LOG_INFO, "%s: oversized frame (len=%d)\n",
1021 1.23 matt device_xname(sc->sc_dev), mrx->m_pkthdr.len);
1022 1.11 msaitoh /* Back out "newly allocated" mbuf. */
1023 1.11 msaitoh m_freem(m);
1024 1.45 thorpej if_statinc(ifp, if_ierrors);
1025 1.11 msaitoh goto fail;
1026 1.11 msaitoh }
1027 1.11 msaitoh #endif
1028 1.11 msaitoh
1029 1.11 msaitoh /*
1030 1.11 msaitoh * Trim FCS!
1031 1.11 msaitoh * NPE always adds the FCS by this driver's setting,
1032 1.11 msaitoh * so we always trim it here and not add M_HASFCS.
1033 1.11 msaitoh */
1034 1.11 msaitoh m_adj(mrx, -ETHER_CRC_LEN);
1035 1.1 scw
1036 1.10 msaitoh /*
1037 1.10 msaitoh * Tap off here if there is a bpf listener.
1038 1.10 msaitoh */
1039 1.32 ozaki
1040 1.30 ozaki if_percpuq_enqueue(ifp->if_percpuq, mrx);
1041 1.1 scw } else {
1042 1.11 msaitoh fail:
1043 1.1 scw /* discard frame and re-use mbuf */
1044 1.1 scw m = npe->ix_m;
1045 1.1 scw }
1046 1.1 scw if (npe_rxbuf_init(sc, npe, m) == 0) {
1047 1.1 scw /* return npe buf to rx free list */
1048 1.1 scw ixpqmgr_qwrite(sc->rx_freeqid, npe->ix_neaddr);
1049 1.1 scw } else {
1050 1.1 scw /* XXX should not happen */
1051 1.1 scw }
1052 1.1 scw }
1053 1.1 scw #undef P2V
1054 1.1 scw }
1055 1.1 scw
1056 1.1 scw static void
1057 1.1 scw npe_startxmit(struct npe_softc *sc)
1058 1.1 scw {
1059 1.1 scw struct npedma *dma = &sc->txdma;
1060 1.1 scw int i;
1061 1.1 scw
1062 1.1 scw sc->tx_free = NULL;
1063 1.1 scw for (i = 0; i < dma->nbuf; i++) {
1064 1.1 scw struct npebuf *npe = &dma->buf[i];
1065 1.1 scw if (npe->ix_m != NULL) {
1066 1.1 scw /* NB: should not happen */
1067 1.1 scw printf("%s: %s: free mbuf at entry %u\n",
1068 1.23 matt device_xname(sc->sc_dev), __func__, i);
1069 1.1 scw m_freem(npe->ix_m);
1070 1.1 scw }
1071 1.1 scw npe->ix_m = NULL;
1072 1.1 scw npe->ix_next = sc->tx_free;
1073 1.1 scw sc->tx_free = npe;
1074 1.1 scw }
1075 1.1 scw }
1076 1.1 scw
1077 1.1 scw static void
1078 1.1 scw npe_startrecv(struct npe_softc *sc)
1079 1.1 scw {
1080 1.1 scw struct npedma *dma = &sc->rxdma;
1081 1.1 scw struct npebuf *npe;
1082 1.1 scw int i;
1083 1.1 scw
1084 1.1 scw for (i = 0; i < dma->nbuf; i++) {
1085 1.1 scw npe = &dma->buf[i];
1086 1.1 scw npe_rxbuf_init(sc, npe, npe->ix_m);
1087 1.39 msaitoh /* Set npe buf on rx free list */
1088 1.1 scw ixpqmgr_qwrite(sc->rx_freeqid, npe->ix_neaddr);
1089 1.1 scw }
1090 1.1 scw }
1091 1.1 scw
1092 1.1 scw static void
1093 1.13 msaitoh npeinit_macreg(struct npe_softc *sc)
1094 1.1 scw {
1095 1.1 scw
1096 1.1 scw /*
1097 1.1 scw * Reset MAC core.
1098 1.1 scw */
1099 1.1 scw WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_RESET);
1100 1.1 scw DELAY(NPE_MAC_RESET_DELAY);
1101 1.39 msaitoh /* Configure MAC to generate MDC clock */
1102 1.1 scw WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_MDC_EN);
1103 1.1 scw
1104 1.44 msaitoh /* Disable transmitter and receiver in the MAC */
1105 1.39 msaitoh WR4(sc, NPE_MAC_RX_CNTRL1,
1106 1.1 scw RD4(sc, NPE_MAC_RX_CNTRL1) &~ NPE_RX_CNTRL1_RX_EN);
1107 1.39 msaitoh WR4(sc, NPE_MAC_TX_CNTRL1,
1108 1.1 scw RD4(sc, NPE_MAC_TX_CNTRL1) &~ NPE_TX_CNTRL1_TX_EN);
1109 1.1 scw
1110 1.1 scw /*
1111 1.1 scw * Set the MAC core registers.
1112 1.1 scw */
1113 1.1 scw WR4(sc, NPE_MAC_INT_CLK_THRESH, 0x1); /* clock ratio: for ipx4xx */
1114 1.1 scw WR4(sc, NPE_MAC_TX_CNTRL2, 0xf); /* max retries */
1115 1.1 scw WR4(sc, NPE_MAC_RANDOM_SEED, 0x8); /* LFSR back-off seed */
1116 1.39 msaitoh /* Thresholds determined by NPE firmware FS */
1117 1.1 scw WR4(sc, NPE_MAC_THRESH_P_EMPTY, 0x12);
1118 1.1 scw WR4(sc, NPE_MAC_THRESH_P_FULL, 0x30);
1119 1.12 msaitoh WR4(sc, NPE_MAC_BUF_SIZE_TX, NPE_MAC_BUF_SIZE_TX_DEFAULT);
1120 1.12 msaitoh /* tx fifo threshold (bytes) */
1121 1.1 scw WR4(sc, NPE_MAC_TX_DEFER, 0x15); /* for single deferral */
1122 1.1 scw WR4(sc, NPE_MAC_RX_DEFER, 0x16); /* deferral on inter-frame gap*/
1123 1.1 scw WR4(sc, NPE_MAC_TX_TWO_DEFER_1, 0x8); /* for 2-part deferral */
1124 1.1 scw WR4(sc, NPE_MAC_TX_TWO_DEFER_2, 0x7); /* for 2-part deferral */
1125 1.12 msaitoh WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT);
1126 1.12 msaitoh /* assumes MII mode */
1127 1.1 scw WR4(sc, NPE_MAC_TX_CNTRL1,
1128 1.1 scw NPE_TX_CNTRL1_RETRY /* retry failed xmits */
1129 1.1 scw | NPE_TX_CNTRL1_FCS_EN /* append FCS */
1130 1.1 scw | NPE_TX_CNTRL1_2DEFER /* 2-part deferal */
1131 1.1 scw | NPE_TX_CNTRL1_PAD_EN); /* pad runt frames */
1132 1.1 scw /* XXX pad strip? */
1133 1.1 scw WR4(sc, NPE_MAC_RX_CNTRL1,
1134 1.1 scw NPE_RX_CNTRL1_CRC_EN /* include CRC/FCS */
1135 1.1 scw | NPE_RX_CNTRL1_PAUSE_EN); /* ena pause frame handling */
1136 1.1 scw WR4(sc, NPE_MAC_RX_CNTRL2, 0);
1137 1.13 msaitoh }
1138 1.1 scw
1139 1.16 msaitoh static void
1140 1.16 msaitoh npeinit_resetcb(void *xsc)
1141 1.16 msaitoh {
1142 1.16 msaitoh struct npe_softc *sc = xsc;
1143 1.16 msaitoh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1144 1.16 msaitoh uint32_t msg[2];
1145 1.16 msaitoh
1146 1.45 thorpej if_statinc(ifp, if_oerrors);
1147 1.16 msaitoh npeinit_locked(sc);
1148 1.16 msaitoh
1149 1.16 msaitoh msg[0] = NPE_NOTIFYMACRECOVERYDONE << NPE_MAC_MSGID_SHL
1150 1.16 msaitoh | (npeconfig[sc->sc_unit].macport << NPE_MAC_PORTID_SHL);
1151 1.16 msaitoh msg[1] = 0;
1152 1.16 msaitoh ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1153 1.16 msaitoh }
1154 1.16 msaitoh
1155 1.13 msaitoh /*
1156 1.13 msaitoh * Reset and initialize the chip
1157 1.13 msaitoh */
1158 1.13 msaitoh static void
1159 1.13 msaitoh npeinit_locked(void *xsc)
1160 1.13 msaitoh {
1161 1.13 msaitoh struct npe_softc *sc = xsc;
1162 1.13 msaitoh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1163 1.13 msaitoh
1164 1.13 msaitoh /* Cancel any pending I/O. */
1165 1.13 msaitoh npestop(ifp, 0);
1166 1.13 msaitoh
1167 1.13 msaitoh /* Reset the chip to a known state. */
1168 1.13 msaitoh npeinit_macreg(sc);
1169 1.6 matt npe_setmac(sc, CLLADDR(ifp->if_sadl));
1170 1.15 msaitoh ether_mediachange(ifp);
1171 1.1 scw npe_setmcast(sc);
1172 1.1 scw
1173 1.1 scw npe_startxmit(sc);
1174 1.1 scw npe_startrecv(sc);
1175 1.1 scw
1176 1.1 scw ifp->if_flags |= IFF_RUNNING;
1177 1.1 scw ifp->if_flags &= ~IFF_OACTIVE;
1178 1.1 scw ifp->if_timer = 0; /* just in case */
1179 1.1 scw
1180 1.44 msaitoh /* Enable transmitter and receiver in the MAC */
1181 1.39 msaitoh WR4(sc, NPE_MAC_RX_CNTRL1,
1182 1.1 scw RD4(sc, NPE_MAC_RX_CNTRL1) | NPE_RX_CNTRL1_RX_EN);
1183 1.39 msaitoh WR4(sc, NPE_MAC_TX_CNTRL1,
1184 1.1 scw RD4(sc, NPE_MAC_TX_CNTRL1) | NPE_TX_CNTRL1_TX_EN);
1185 1.1 scw
1186 1.1 scw callout_reset(&sc->sc_tick_ch, hz, npe_tick, sc);
1187 1.1 scw }
1188 1.1 scw
1189 1.1 scw static int
1190 1.1 scw npeinit(struct ifnet *ifp)
1191 1.1 scw {
1192 1.1 scw struct npe_softc *sc = ifp->if_softc;
1193 1.1 scw int s;
1194 1.1 scw
1195 1.1 scw s = splnet();
1196 1.1 scw npeinit_locked(sc);
1197 1.1 scw splx(s);
1198 1.1 scw
1199 1.39 msaitoh return 0;
1200 1.1 scw }
1201 1.1 scw
1202 1.1 scw /*
1203 1.1 scw * Defragment an mbuf chain, returning at most maxfrags separate
1204 1.1 scw * mbufs+clusters. If this is not possible NULL is returned and
1205 1.27 snj * the original mbuf chain is left in its present (potentially
1206 1.1 scw * modified) state. We use two techniques: collapsing consecutive
1207 1.1 scw * mbufs and replacing consecutive mbufs by a cluster.
1208 1.1 scw */
1209 1.1 scw static __inline struct mbuf *
1210 1.1 scw npe_defrag(struct mbuf *m0)
1211 1.1 scw {
1212 1.1 scw struct mbuf *m;
1213 1.1 scw
1214 1.1 scw MGETHDR(m, M_DONTWAIT, MT_DATA);
1215 1.1 scw if (m == NULL)
1216 1.39 msaitoh return NULL;
1217 1.35 maxv m_copy_pkthdr(m, m0);
1218 1.1 scw
1219 1.1 scw if ((m->m_len = m0->m_pkthdr.len) > MHLEN) {
1220 1.1 scw MCLGET(m, M_DONTWAIT);
1221 1.1 scw if ((m->m_flags & M_EXT) == 0) {
1222 1.1 scw m_freem(m);
1223 1.39 msaitoh return NULL;
1224 1.1 scw }
1225 1.1 scw }
1226 1.1 scw
1227 1.3 christos m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
1228 1.1 scw m_freem(m0);
1229 1.1 scw
1230 1.39 msaitoh return m;
1231 1.1 scw }
1232 1.1 scw
1233 1.1 scw /*
1234 1.1 scw * Dequeue packets and place on the h/w transmit queue.
1235 1.1 scw */
1236 1.1 scw static void
1237 1.1 scw npestart(struct ifnet *ifp)
1238 1.1 scw {
1239 1.1 scw struct npe_softc *sc = ifp->if_softc;
1240 1.1 scw struct npebuf *npe;
1241 1.1 scw struct npehwbuf *hw;
1242 1.1 scw struct mbuf *m, *n;
1243 1.1 scw bus_dma_segment_t *segs;
1244 1.1 scw int nseg, len, error, i;
1245 1.1 scw uint32_t next;
1246 1.1 scw
1247 1.40 msaitoh if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1248 1.1 scw return;
1249 1.1 scw
1250 1.1 scw while (sc->tx_free != NULL) {
1251 1.1 scw IFQ_DEQUEUE(&ifp->if_snd, m);
1252 1.13 msaitoh if (m == NULL)
1253 1.13 msaitoh break;
1254 1.1 scw npe = sc->tx_free;
1255 1.1 scw error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map, m,
1256 1.40 msaitoh BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1257 1.1 scw if (error == EFBIG) {
1258 1.1 scw n = npe_defrag(m);
1259 1.1 scw if (n == NULL) {
1260 1.1 scw printf("%s: %s: too many fragments\n",
1261 1.23 matt device_xname(sc->sc_dev), __func__);
1262 1.1 scw m_freem(m);
1263 1.1 scw return; /* XXX? */
1264 1.1 scw }
1265 1.1 scw m = n;
1266 1.1 scw error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map,
1267 1.40 msaitoh m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1268 1.1 scw }
1269 1.1 scw if (error != 0) {
1270 1.1 scw printf("%s: %s: error %u\n",
1271 1.23 matt device_xname(sc->sc_dev), __func__, error);
1272 1.1 scw m_freem(m);
1273 1.1 scw return; /* XXX? */
1274 1.1 scw }
1275 1.1 scw sc->tx_free = npe->ix_next;
1276 1.1 scw
1277 1.1 scw /*
1278 1.1 scw * Tap off here if there is a bpf listener.
1279 1.1 scw */
1280 1.34 msaitoh bpf_mtap(ifp, m, BPF_D_OUT);
1281 1.1 scw
1282 1.1 scw bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0,
1283 1.1 scw npe->ix_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
1284 1.1 scw
1285 1.1 scw npe->ix_m = m;
1286 1.1 scw hw = npe->ix_hw;
1287 1.1 scw len = m->m_pkthdr.len;
1288 1.1 scw nseg = npe->ix_map->dm_nsegs;
1289 1.1 scw segs = npe->ix_map->dm_segs;
1290 1.1 scw next = npe->ix_neaddr + sizeof(hw->ix_ne[0]);
1291 1.1 scw for (i = 0; i < nseg; i++) {
1292 1.1 scw hw->ix_ne[i].data = htobe32(segs[i].ds_addr);
1293 1.1 scw hw->ix_ne[i].len = htobe32((segs[i].ds_len<<16) | len);
1294 1.1 scw hw->ix_ne[i].next = htobe32(next);
1295 1.1 scw
1296 1.1 scw len = 0; /* zero for segments > 1 */
1297 1.1 scw next += sizeof(hw->ix_ne[0]);
1298 1.1 scw }
1299 1.1 scw hw->ix_ne[i-1].next = 0; /* zero last in chain */
1300 1.1 scw /* XXX flush descriptor instead of using uncached memory */
1301 1.1 scw
1302 1.1 scw DPRINTF(sc, "%s: qwrite(%u, 0x%x) ne_data %x ne_len 0x%x\n",
1303 1.1 scw __func__, sc->tx_qid, npe->ix_neaddr,
1304 1.1 scw hw->ix_ne[0].data, hw->ix_ne[0].len);
1305 1.1 scw /* stick it on the tx q */
1306 1.1 scw /* XXX add vlan priority */
1307 1.1 scw ixpqmgr_qwrite(sc->tx_qid, npe->ix_neaddr);
1308 1.1 scw
1309 1.1 scw ifp->if_timer = 5;
1310 1.1 scw }
1311 1.1 scw if (sc->tx_free == NULL)
1312 1.1 scw ifp->if_flags |= IFF_OACTIVE;
1313 1.1 scw }
1314 1.1 scw
1315 1.1 scw static void
1316 1.1 scw npe_stopxmit(struct npe_softc *sc)
1317 1.1 scw {
1318 1.1 scw struct npedma *dma = &sc->txdma;
1319 1.1 scw int i;
1320 1.1 scw
1321 1.1 scw /* XXX qmgr */
1322 1.1 scw for (i = 0; i < dma->nbuf; i++) {
1323 1.1 scw struct npebuf *npe = &dma->buf[i];
1324 1.1 scw
1325 1.1 scw if (npe->ix_m != NULL) {
1326 1.1 scw bus_dmamap_unload(sc->sc_dt, npe->ix_map);
1327 1.1 scw m_freem(npe->ix_m);
1328 1.1 scw npe->ix_m = NULL;
1329 1.1 scw }
1330 1.1 scw }
1331 1.1 scw }
1332 1.1 scw
1333 1.1 scw static void
1334 1.1 scw npe_stoprecv(struct npe_softc *sc)
1335 1.1 scw {
1336 1.1 scw struct npedma *dma = &sc->rxdma;
1337 1.1 scw int i;
1338 1.1 scw
1339 1.1 scw /* XXX qmgr */
1340 1.1 scw for (i = 0; i < dma->nbuf; i++) {
1341 1.1 scw struct npebuf *npe = &dma->buf[i];
1342 1.1 scw
1343 1.1 scw if (npe->ix_m != NULL) {
1344 1.1 scw bus_dmamap_unload(sc->sc_dt, npe->ix_map);
1345 1.1 scw m_freem(npe->ix_m);
1346 1.1 scw npe->ix_m = NULL;
1347 1.1 scw }
1348 1.1 scw }
1349 1.1 scw }
1350 1.1 scw
1351 1.1 scw /*
1352 1.1 scw * Turn off interrupts, and stop the nic.
1353 1.1 scw */
1354 1.1 scw void
1355 1.1 scw npestop(struct ifnet *ifp, int disable)
1356 1.1 scw {
1357 1.1 scw struct npe_softc *sc = ifp->if_softc;
1358 1.1 scw
1359 1.44 msaitoh /* Disable transmitter and receiver in the MAC */
1360 1.39 msaitoh WR4(sc, NPE_MAC_RX_CNTRL1,
1361 1.1 scw RD4(sc, NPE_MAC_RX_CNTRL1) &~ NPE_RX_CNTRL1_RX_EN);
1362 1.39 msaitoh WR4(sc, NPE_MAC_TX_CNTRL1,
1363 1.1 scw RD4(sc, NPE_MAC_TX_CNTRL1) &~ NPE_TX_CNTRL1_TX_EN);
1364 1.1 scw
1365 1.1 scw callout_stop(&sc->sc_tick_ch);
1366 1.1 scw
1367 1.1 scw npe_stopxmit(sc);
1368 1.1 scw npe_stoprecv(sc);
1369 1.1 scw /* XXX go into loopback & drain q's? */
1370 1.1 scw /* XXX but beware of disabling tx above */
1371 1.1 scw
1372 1.1 scw /*
1373 1.1 scw * The MAC core rx/tx disable may leave the MAC hardware in an
1374 1.39 msaitoh * unpredictable state. A hw reset is executed before resetting
1375 1.1 scw * all the MAC parameters to a known value.
1376 1.1 scw */
1377 1.1 scw WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_RESET);
1378 1.1 scw DELAY(NPE_MAC_RESET_DELAY);
1379 1.1 scw WR4(sc, NPE_MAC_INT_CLK_THRESH, NPE_MAC_INT_CLK_THRESH_DEFAULT);
1380 1.1 scw WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_MDC_EN);
1381 1.13 msaitoh
1382 1.13 msaitoh ifp->if_timer = 0;
1383 1.13 msaitoh ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1384 1.1 scw }
1385 1.1 scw
1386 1.1 scw void
1387 1.1 scw npewatchdog(struct ifnet *ifp)
1388 1.1 scw {
1389 1.1 scw struct npe_softc *sc = ifp->if_softc;
1390 1.1 scw int s;
1391 1.1 scw
1392 1.23 matt aprint_error_dev(sc->sc_dev, "device timeout\n");
1393 1.1 scw s = splnet();
1394 1.45 thorpej if_statinc(ifp, if_oerrors);
1395 1.1 scw npeinit_locked(sc);
1396 1.1 scw splx(s);
1397 1.1 scw }
1398 1.1 scw
1399 1.1 scw static int
1400 1.3 christos npeioctl(struct ifnet *ifp, u_long cmd, void *data)
1401 1.1 scw {
1402 1.1 scw struct npe_softc *sc = ifp->if_softc;
1403 1.13 msaitoh struct ifreq *ifr = (struct ifreq *) data;
1404 1.1 scw int s, error = 0;
1405 1.1 scw
1406 1.1 scw s = splnet();
1407 1.1 scw
1408 1.13 msaitoh switch (cmd) {
1409 1.13 msaitoh case SIOCSIFMEDIA:
1410 1.13 msaitoh #if 0 /* not yet */
1411 1.13 msaitoh /* Flow control requires full-duplex mode. */
1412 1.13 msaitoh if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
1413 1.13 msaitoh (ifr->ifr_media & IFM_FDX) == 0)
1414 1.13 msaitoh ifr->ifr_media &= ~IFM_ETH_FMASK;
1415 1.13 msaitoh if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
1416 1.13 msaitoh if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
1417 1.13 msaitoh /* We can do both TXPAUSE and RXPAUSE. */
1418 1.13 msaitoh ifr->ifr_media |=
1419 1.13 msaitoh IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
1420 1.13 msaitoh }
1421 1.13 msaitoh sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
1422 1.13 msaitoh }
1423 1.13 msaitoh #endif
1424 1.13 msaitoh error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1425 1.13 msaitoh break;
1426 1.13 msaitoh case SIOCSIFFLAGS:
1427 1.40 msaitoh if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_RUNNING) {
1428 1.13 msaitoh /*
1429 1.13 msaitoh * If interface is marked down and it is running,
1430 1.13 msaitoh * then stop and disable it.
1431 1.13 msaitoh */
1432 1.49 riastrad if_stop(ifp, 1);
1433 1.40 msaitoh } else if ((ifp->if_flags & (IFF_UP |IFF_RUNNING)) == IFF_UP) {
1434 1.13 msaitoh /*
1435 1.13 msaitoh * If interface is marked up and it is stopped, then
1436 1.13 msaitoh * start it.
1437 1.13 msaitoh */
1438 1.50 riastrad error = if_init(ifp);
1439 1.13 msaitoh } else if ((ifp->if_flags & IFF_UP) != 0) {
1440 1.42 msaitoh u_short diff;
1441 1.13 msaitoh
1442 1.13 msaitoh /* Up (AND RUNNING). */
1443 1.13 msaitoh
1444 1.13 msaitoh diff = (ifp->if_flags ^ sc->sc_if_flags)
1445 1.40 msaitoh & (IFF_PROMISC | IFF_ALLMULTI);
1446 1.40 msaitoh if ((diff & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
1447 1.13 msaitoh /*
1448 1.13 msaitoh * If the difference bettween last flag and
1449 1.13 msaitoh * new flag only IFF_PROMISC or IFF_ALLMULTI,
1450 1.13 msaitoh * set multicast filter only (don't reset to
1451 1.13 msaitoh * prevent link down).
1452 1.13 msaitoh */
1453 1.13 msaitoh npe_setmcast(sc);
1454 1.13 msaitoh } else {
1455 1.13 msaitoh /*
1456 1.13 msaitoh * Reset the interface to pick up changes in
1457 1.13 msaitoh * any other flags that affect the hardware
1458 1.13 msaitoh * state.
1459 1.13 msaitoh */
1460 1.50 riastrad error = if_init(ifp);
1461 1.13 msaitoh }
1462 1.13 msaitoh }
1463 1.13 msaitoh sc->sc_if_flags = ifp->if_flags;
1464 1.13 msaitoh break;
1465 1.13 msaitoh default:
1466 1.13 msaitoh error = ether_ioctl(ifp, cmd, data);
1467 1.13 msaitoh if (error == ENETRESET) {
1468 1.13 msaitoh /*
1469 1.13 msaitoh * Multicast list has changed; set the hardware filter
1470 1.13 msaitoh * accordingly.
1471 1.13 msaitoh */
1472 1.13 msaitoh npe_setmcast(sc);
1473 1.13 msaitoh error = 0;
1474 1.1 scw }
1475 1.1 scw }
1476 1.1 scw
1477 1.1 scw npestart(ifp);
1478 1.1 scw
1479 1.1 scw splx(s);
1480 1.1 scw return error;
1481 1.1 scw }
1482 1.1 scw
1483 1.1 scw /*
1484 1.1 scw * Setup a traffic class -> rx queue mapping.
1485 1.1 scw */
1486 1.1 scw static int
1487 1.1 scw npe_setrxqosentry(struct npe_softc *sc, int classix, int trafclass, int qid)
1488 1.1 scw {
1489 1.1 scw int npeid = npeconfig[sc->sc_unit].npeid;
1490 1.1 scw uint32_t msg[2];
1491 1.1 scw
1492 1.15 msaitoh msg[0] = (NPE_SETRXQOSENTRY << NPE_MAC_MSGID_SHL) | (npeid << 20)
1493 1.15 msaitoh | classix;
1494 1.1 scw msg[1] = (trafclass << 24) | (1 << 23) | (qid << 16) | (qid << 4);
1495 1.1 scw return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1496 1.1 scw }
1497 1.1 scw
1498 1.1 scw /*
1499 1.1 scw * Update and reset the statistics in the NPE.
1500 1.1 scw */
1501 1.1 scw static int
1502 1.1 scw npe_updatestats(struct npe_softc *sc)
1503 1.1 scw {
1504 1.1 scw uint32_t msg[2];
1505 1.1 scw
1506 1.1 scw msg[0] = NPE_RESETSTATS << NPE_MAC_MSGID_SHL;
1507 1.1 scw msg[1] = sc->sc_stats_phys; /* physical address of stat block */
1508 1.1 scw return ixpnpe_sendmsg(sc->sc_npe, msg); /* NB: no recv */
1509 1.1 scw }
1510 1.1 scw
1511 1.1 scw #if 0
1512 1.1 scw /*
1513 1.1 scw * Get the current statistics block.
1514 1.1 scw */
1515 1.1 scw static int
1516 1.1 scw npe_getstats(struct npe_softc *sc)
1517 1.1 scw {
1518 1.1 scw uint32_t msg[2];
1519 1.1 scw
1520 1.1 scw msg[0] = NPE_GETSTATS << NPE_MAC_MSGID_SHL;
1521 1.1 scw msg[1] = sc->sc_stats_phys; /* physical address of stat block */
1522 1.1 scw return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1523 1.1 scw }
1524 1.1 scw
1525 1.1 scw /*
1526 1.1 scw * Query the image id of the loaded firmware.
1527 1.1 scw */
1528 1.1 scw static uint32_t
1529 1.1 scw npe_getimageid(struct npe_softc *sc)
1530 1.1 scw {
1531 1.1 scw uint32_t msg[2];
1532 1.1 scw
1533 1.1 scw msg[0] = NPE_GETSTATUS << NPE_MAC_MSGID_SHL;
1534 1.1 scw msg[1] = 0;
1535 1.1 scw return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg) == 0 ? msg[1] : 0;
1536 1.1 scw }
1537 1.1 scw
1538 1.1 scw /*
1539 1.1 scw * Enable/disable loopback.
1540 1.1 scw */
1541 1.1 scw static int
1542 1.1 scw npe_setloopback(struct npe_softc *sc, int ena)
1543 1.1 scw {
1544 1.1 scw uint32_t msg[2];
1545 1.1 scw
1546 1.1 scw msg[0] = (NPE_SETLOOPBACK << NPE_MAC_MSGID_SHL) | (ena != 0);
1547 1.1 scw msg[1] = 0;
1548 1.1 scw return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1549 1.1 scw }
1550 1.1 scw #endif
1551 1.1 scw
1552 1.1 scw /*
1553 1.1 scw * MII bus support routines.
1554 1.1 scw *
1555 1.1 scw * NB: ixp425 has one PHY per NPE
1556 1.1 scw */
1557 1.1 scw static uint32_t
1558 1.1 scw npe_mii_mdio_read(struct npe_softc *sc, int reg)
1559 1.1 scw {
1560 1.1 scw #define MII_RD4(sc, reg) bus_space_read_4(sc->sc_iot, sc->sc_miih, reg)
1561 1.1 scw uint32_t v;
1562 1.1 scw
1563 1.1 scw /* NB: registers are known to be sequential */
1564 1.1 scw v = (MII_RD4(sc, reg+0) & 0xff) << 0;
1565 1.1 scw v |= (MII_RD4(sc, reg+4) & 0xff) << 8;
1566 1.1 scw v |= (MII_RD4(sc, reg+8) & 0xff) << 16;
1567 1.1 scw v |= (MII_RD4(sc, reg+12) & 0xff) << 24;
1568 1.1 scw return v;
1569 1.1 scw #undef MII_RD4
1570 1.1 scw }
1571 1.1 scw
1572 1.1 scw static void
1573 1.1 scw npe_mii_mdio_write(struct npe_softc *sc, int reg, uint32_t cmd)
1574 1.1 scw {
1575 1.1 scw #define MII_WR4(sc, reg, v) \
1576 1.1 scw bus_space_write_4(sc->sc_iot, sc->sc_miih, reg, v)
1577 1.1 scw
1578 1.1 scw /* NB: registers are known to be sequential */
1579 1.1 scw MII_WR4(sc, reg+0, cmd & 0xff);
1580 1.1 scw MII_WR4(sc, reg+4, (cmd >> 8) & 0xff);
1581 1.1 scw MII_WR4(sc, reg+8, (cmd >> 16) & 0xff);
1582 1.1 scw MII_WR4(sc, reg+12, (cmd >> 24) & 0xff);
1583 1.1 scw #undef MII_WR4
1584 1.1 scw }
1585 1.1 scw
1586 1.1 scw static int
1587 1.1 scw npe_mii_mdio_wait(struct npe_softc *sc)
1588 1.1 scw {
1589 1.1 scw #define MAXTRIES 100 /* XXX */
1590 1.1 scw uint32_t v;
1591 1.1 scw int i;
1592 1.1 scw
1593 1.1 scw for (i = 0; i < MAXTRIES; i++) {
1594 1.1 scw v = npe_mii_mdio_read(sc, NPE_MAC_MDIO_CMD);
1595 1.1 scw if ((v & NPE_MII_GO) == 0)
1596 1.36 msaitoh return 0;
1597 1.1 scw }
1598 1.36 msaitoh return ETIMEDOUT;
1599 1.1 scw #undef MAXTRIES
1600 1.1 scw }
1601 1.1 scw
1602 1.1 scw static int
1603 1.36 msaitoh npe_miibus_readreg(device_t self, int phy, int reg, uint16_t *val)
1604 1.1 scw {
1605 1.23 matt struct npe_softc *sc = device_private(self);
1606 1.1 scw uint32_t v;
1607 1.1 scw
1608 1.1 scw if (sc->sc_phy > IXPNPECF_PHY_DEFAULT && phy != sc->sc_phy)
1609 1.36 msaitoh return -1;
1610 1.1 scw v = (phy << NPE_MII_ADDR_SHL) | (reg << NPE_MII_REG_SHL)
1611 1.1 scw | NPE_MII_GO;
1612 1.1 scw npe_mii_mdio_write(sc, NPE_MAC_MDIO_CMD, v);
1613 1.36 msaitoh if (npe_mii_mdio_wait(sc) == 0)
1614 1.1 scw v = npe_mii_mdio_read(sc, NPE_MAC_MDIO_STS);
1615 1.1 scw else
1616 1.1 scw v = 0xffff | NPE_MII_READ_FAIL;
1617 1.36 msaitoh
1618 1.36 msaitoh if ((v & NPE_MII_READ_FAIL) != 0)
1619 1.36 msaitoh return -1;
1620 1.36 msaitoh
1621 1.36 msaitoh *val = v & 0xffff;
1622 1.36 msaitoh return 0;
1623 1.1 scw #undef MAXTRIES
1624 1.1 scw }
1625 1.1 scw
1626 1.36 msaitoh static int
1627 1.36 msaitoh npe_miibus_writereg(device_t self, int phy, int reg, uint16_t val)
1628 1.1 scw {
1629 1.23 matt struct npe_softc *sc = device_private(self);
1630 1.1 scw uint32_t v;
1631 1.1 scw
1632 1.1 scw if (sc->sc_phy > IXPNPECF_PHY_DEFAULT && phy != sc->sc_phy)
1633 1.36 msaitoh return -1;
1634 1.1 scw v = (phy << NPE_MII_ADDR_SHL) | (reg << NPE_MII_REG_SHL)
1635 1.36 msaitoh | val | NPE_MII_WRITE
1636 1.1 scw | NPE_MII_GO;
1637 1.1 scw npe_mii_mdio_write(sc, NPE_MAC_MDIO_CMD, v);
1638 1.36 msaitoh
1639 1.36 msaitoh return npe_mii_mdio_wait(sc);
1640 1.1 scw }
1641 1.1 scw
1642 1.1 scw static void
1643 1.23 matt npe_miibus_statchg(struct ifnet *ifp)
1644 1.1 scw {
1645 1.23 matt struct npe_softc *sc = ifp->if_softc;
1646 1.1 scw uint32_t tx1, rx1;
1647 1.16 msaitoh uint32_t randoff;
1648 1.1 scw
1649 1.39 msaitoh /* Sync MAC duplex state */
1650 1.1 scw tx1 = RD4(sc, NPE_MAC_TX_CNTRL1);
1651 1.1 scw rx1 = RD4(sc, NPE_MAC_RX_CNTRL1);
1652 1.1 scw if (sc->sc_mii.mii_media_active & IFM_FDX) {
1653 1.16 msaitoh WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT);
1654 1.1 scw tx1 &= ~NPE_TX_CNTRL1_DUPLEX;
1655 1.1 scw rx1 |= NPE_RX_CNTRL1_PAUSE_EN;
1656 1.1 scw } else {
1657 1.16 msaitoh struct timeval now;
1658 1.16 msaitoh getmicrotime(&now);
1659 1.16 msaitoh randoff = (RD4(sc, NPE_MAC_UNI_ADDR_6) ^ now.tv_usec)
1660 1.16 msaitoh & 0x7f;
1661 1.16 msaitoh WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT
1662 1.16 msaitoh + randoff);
1663 1.1 scw tx1 |= NPE_TX_CNTRL1_DUPLEX;
1664 1.1 scw rx1 &= ~NPE_RX_CNTRL1_PAUSE_EN;
1665 1.1 scw }
1666 1.1 scw WR4(sc, NPE_MAC_RX_CNTRL1, rx1);
1667 1.1 scw WR4(sc, NPE_MAC_TX_CNTRL1, tx1);
1668 1.1 scw }
1669