ixp425_if_npe.c revision 1.54 1 1.54 riastrad /* $NetBSD: ixp425_if_npe.c,v 1.54 2024/06/29 12:11:10 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.54 riastrad __KERNEL_RCSID(0, "$NetBSD: ixp425_if_npe.c,v 1.54 2024/06/29 12:11:10 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.53 skrll #include <sys/kmem.h>
56 1.1 scw #include <sys/mbuf.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.53 skrll dma->buf = kmem_zalloc(nbuf * sizeof(struct npebuf), KM_SLEEP);
472 1.1 scw dma->buf_phys = dma->buf_map->dm_segs[0].ds_addr;
473 1.1 scw for (i = 0; i < dma->nbuf; i++) {
474 1.1 scw struct npebuf *npe = &dma->buf[i];
475 1.1 scw struct npehwbuf *hw = &dma->hwbuf[i];
476 1.1 scw
477 1.39 msaitoh /* Calculate offset to shared area */
478 1.1 scw npe->ix_neaddr = dma->buf_phys +
479 1.1 scw ((uintptr_t)hw - (uintptr_t)dma->hwbuf);
480 1.1 scw KASSERT((npe->ix_neaddr & 0x1f) == 0);
481 1.10 msaitoh error = bus_dmamap_create(sc->sc_dt, MCLBYTES, maxseg,
482 1.1 scw MCLBYTES, 0, 0, &npe->ix_map);
483 1.1 scw if (error != 0) {
484 1.23 matt aprint_error_dev(sc->sc_dev,
485 1.23 matt "unable to %s for %s buffer %u, error %u\n",
486 1.23 matt "create dmamap", dma->name, i, error);
487 1.1 scw /* XXXSCW: Free up maps... */
488 1.1 scw return error;
489 1.1 scw }
490 1.1 scw npe->ix_hw = hw;
491 1.1 scw }
492 1.1 scw bus_dmamap_sync(sc->sc_dt, dma->buf_map, 0, dma->buf_map->dm_mapsize,
493 1.1 scw BUS_DMASYNC_PREWRITE);
494 1.1 scw return 0;
495 1.1 scw }
496 1.1 scw
497 1.1 scw #if 0
498 1.1 scw static void
499 1.1 scw npe_dma_destroy(struct npe_softc *sc, struct npedma *dma)
500 1.1 scw {
501 1.1 scw int i;
502 1.1 scw
503 1.1 scw /* XXXSCW: Clean this up */
504 1.1 scw
505 1.1 scw if (dma->hwbuf != NULL) {
506 1.1 scw for (i = 0; i < dma->nbuf; i++) {
507 1.1 scw struct npebuf *npe = &dma->buf[i];
508 1.1 scw bus_dmamap_destroy(sc->sc_dt, npe->ix_map);
509 1.1 scw }
510 1.1 scw bus_dmamap_unload(sc->sc_dt, dma->buf_map);
511 1.3 christos bus_dmamem_free(sc->sc_dt, (void *)dma->hwbuf, dma->buf_map);
512 1.1 scw bus_dmamap_destroy(sc->sc_dt, dma->buf_map);
513 1.1 scw }
514 1.1 scw if (dma->buf != NULL)
515 1.53 skrll kmem_free(dma->buf, dma->nbuf * sizeof(struct npebuf));
516 1.1 scw memset(dma, 0, sizeof(*dma));
517 1.1 scw }
518 1.1 scw #endif
519 1.1 scw
520 1.1 scw static int
521 1.1 scw npe_activate(struct npe_softc *sc)
522 1.1 scw {
523 1.1 scw bus_dma_segment_t seg;
524 1.1 scw int unit = sc->sc_unit;
525 1.1 scw int error, i, rseg;
526 1.3 christos void *statbuf;
527 1.1 scw
528 1.1 scw /* load NPE firmware and start it running */
529 1.1 scw error = ixpnpe_init(sc->sc_npe, "npe_fw", npeconfig[unit].imageid);
530 1.1 scw if (error != 0)
531 1.1 scw return error;
532 1.1 scw
533 1.1 scw if (bus_space_map(sc->sc_iot, npeconfig[unit].regbase,
534 1.1 scw npeconfig[unit].regsize, 0, &sc->sc_ioh)) {
535 1.23 matt aprint_error_dev(sc->sc_dev, "Cannot map registers 0x%x:0x%x\n",
536 1.23 matt npeconfig[unit].regbase, npeconfig[unit].regsize);
537 1.1 scw return ENOMEM;
538 1.1 scw }
539 1.1 scw
540 1.1 scw if (npeconfig[unit].miibase != npeconfig[unit].regbase) {
541 1.1 scw /*
542 1.1 scw * The PHY's are only accessible from one MAC (it appears)
543 1.1 scw * so for other MAC's setup an additional mapping for
544 1.1 scw * frobbing the PHY registers.
545 1.1 scw */
546 1.1 scw if (bus_space_map(sc->sc_iot, npeconfig[unit].miibase,
547 1.1 scw npeconfig[unit].miisize, 0, &sc->sc_miih)) {
548 1.23 matt aprint_error_dev(sc->sc_dev,
549 1.23 matt "Cannot map MII registers 0x%x:0x%x\n",
550 1.23 matt npeconfig[unit].miibase, npeconfig[unit].miisize);
551 1.1 scw return ENOMEM;
552 1.1 scw }
553 1.1 scw } else
554 1.1 scw sc->sc_miih = sc->sc_ioh;
555 1.1 scw error = npe_dma_setup(sc, &sc->txdma, "tx", NPE_TXBUF, NPE_MAXSEG);
556 1.1 scw if (error != 0)
557 1.1 scw return error;
558 1.1 scw error = npe_dma_setup(sc, &sc->rxdma, "rx", NPE_RXBUF, 1);
559 1.1 scw if (error != 0)
560 1.1 scw return error;
561 1.1 scw
562 1.1 scw /* setup statistics block */
563 1.1 scw error = bus_dmamem_alloc(sc->sc_dt, sizeof(struct npestats),
564 1.1 scw sizeof(uint32_t), 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
565 1.1 scw if (error) {
566 1.23 matt aprint_error_dev(sc->sc_dev,
567 1.23 matt "unable to %s for %s, error %u\n",
568 1.23 matt "allocate memory", "stats block", error);
569 1.1 scw return error;
570 1.1 scw }
571 1.1 scw
572 1.1 scw error = bus_dmamem_map(sc->sc_dt, &seg, 1, sizeof(struct npestats),
573 1.1 scw &statbuf, BUS_DMA_NOWAIT);
574 1.1 scw if (error) {
575 1.23 matt aprint_error_dev(sc->sc_dev,
576 1.23 matt "unable to %s for %s, error %u\n",
577 1.23 matt "map memory", "stats block", error);
578 1.1 scw return error;
579 1.1 scw }
580 1.1 scw sc->sc_stats = (void *)statbuf;
581 1.1 scw
582 1.1 scw error = bus_dmamap_create(sc->sc_dt, sizeof(struct npestats), 1,
583 1.1 scw sizeof(struct npestats), 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
584 1.1 scw &sc->sc_stats_map);
585 1.1 scw if (error) {
586 1.23 matt aprint_error_dev(sc->sc_dev,
587 1.23 matt "unable to %s for %s, error %u\n",
588 1.23 matt "create map", "stats block", error);
589 1.1 scw return error;
590 1.1 scw }
591 1.1 scw
592 1.29 maxv error = bus_dmamap_load(sc->sc_dt, sc->sc_stats_map, sc->sc_stats,
593 1.29 maxv sizeof(struct npestats), NULL, BUS_DMA_NOWAIT);
594 1.29 maxv if (error) {
595 1.23 matt aprint_error_dev(sc->sc_dev,
596 1.23 matt "unable to %s for %s, error %u\n",
597 1.23 matt "load map", "stats block", error);
598 1.1 scw return error;
599 1.1 scw }
600 1.1 scw sc->sc_stats_phys = sc->sc_stats_map->dm_segs[0].ds_addr;
601 1.1 scw
602 1.1 scw /* XXX disable half-bridge LEARNING+FILTERING feature */
603 1.1 scw
604 1.1 scw /*
605 1.1 scw * Setup h/w rx/tx queues. There are four q's:
606 1.1 scw * rx inbound q of rx'd frames
607 1.1 scw * rx_free pool of ixpbuf's for receiving frames
608 1.1 scw * tx outbound q of frames to send
609 1.1 scw * tx_done q of tx frames that have been processed
610 1.1 scw *
611 1.1 scw * The NPE handles the actual tx/rx process and the q manager
612 1.1 scw * handles the queues. The driver just writes entries to the
613 1.1 scw * q manager mailbox's and gets callbacks when there are rx'd
614 1.1 scw * frames to process or tx'd frames to reap. These callbacks
615 1.1 scw * are controlled by the q configurations; e.g. we get a
616 1.1 scw * callback when tx_done has 2 or more frames to process and
617 1.48 andvar * when the rx q has at least one frame. These settings can
618 1.1 scw * changed at the time the q is configured.
619 1.1 scw */
620 1.1 scw sc->rx_qid = npeconfig[unit].rx_qid;
621 1.1 scw ixpqmgr_qconfig(sc->rx_qid, NPE_RXBUF, 0, 1,
622 1.1 scw IX_QMGR_Q_SOURCE_ID_NOT_E, npe_rxdone, sc);
623 1.1 scw sc->rx_freeqid = npeconfig[unit].rx_freeqid;
624 1.1 scw ixpqmgr_qconfig(sc->rx_freeqid, NPE_RXBUF, 0, NPE_RXBUF/2, 0, NULL, sc);
625 1.1 scw /* tell the NPE to direct all traffic to rx_qid */
626 1.1 scw #if 0
627 1.1 scw for (i = 0; i < 8; i++)
628 1.1 scw #else
629 1.23 matt printf("%s: remember to fix rx q setup\n", device_xname(sc->sc_dev));
630 1.1 scw for (i = 0; i < 4; i++)
631 1.1 scw #endif
632 1.1 scw npe_setrxqosentry(sc, i, 0, sc->rx_qid);
633 1.1 scw
634 1.1 scw sc->tx_qid = npeconfig[unit].tx_qid;
635 1.1 scw sc->tx_doneqid = npeconfig[unit].tx_doneqid;
636 1.1 scw ixpqmgr_qconfig(sc->tx_qid, NPE_TXBUF, 0, NPE_TXBUF, 0, NULL, sc);
637 1.1 scw if (tx_doneqid == -1) {
638 1.1 scw ixpqmgr_qconfig(sc->tx_doneqid, NPE_TXBUF, 0, 2,
639 1.1 scw IX_QMGR_Q_SOURCE_ID_NOT_E, npe_txdone, sc);
640 1.1 scw tx_doneqid = sc->tx_doneqid;
641 1.1 scw }
642 1.1 scw
643 1.1 scw KASSERT(npes[npeconfig[unit].npeid] == NULL);
644 1.1 scw npes[npeconfig[unit].npeid] = sc;
645 1.1 scw
646 1.1 scw return 0;
647 1.1 scw }
648 1.1 scw
649 1.1 scw #if 0
650 1.1 scw static void
651 1.1 scw npe_deactivate(struct npe_softc *sc);
652 1.1 scw {
653 1.1 scw int unit = sc->sc_unit;
654 1.1 scw
655 1.1 scw npes[npeconfig[unit].npeid] = NULL;
656 1.1 scw
657 1.1 scw /* XXX disable q's */
658 1.1 scw if (sc->sc_npe != NULL)
659 1.1 scw ixpnpe_stop(sc->sc_npe);
660 1.1 scw if (sc->sc_stats != NULL) {
661 1.1 scw bus_dmamap_unload(sc->sc_stats_tag, sc->sc_stats_map);
662 1.1 scw bus_dmamem_free(sc->sc_stats_tag, sc->sc_stats,
663 1.1 scw sc->sc_stats_map);
664 1.1 scw bus_dmamap_destroy(sc->sc_stats_tag, sc->sc_stats_map);
665 1.1 scw }
666 1.1 scw if (sc->sc_stats_tag != NULL)
667 1.1 scw bus_dma_tag_destroy(sc->sc_stats_tag);
668 1.1 scw npe_dma_destroy(sc, &sc->txdma);
669 1.1 scw npe_dma_destroy(sc, &sc->rxdma);
670 1.1 scw bus_generic_detach(sc->sc_dev);
671 1.46 thorpej XXX ifmedia_fini somewhere
672 1.1 scw if (sc->sc_mii)
673 1.1 scw device_delete_child(sc->sc_dev, sc->sc_mii);
674 1.1 scw #if 0
675 1.1 scw /* XXX sc_ioh and sc_miih */
676 1.1 scw if (sc->mem_res)
677 1.1 scw bus_release_resource(dev, SYS_RES_IOPORT,
678 1.1 scw rman_get_rid(sc->mem_res), sc->mem_res);
679 1.1 scw sc->mem_res = 0;
680 1.1 scw #endif
681 1.1 scw }
682 1.1 scw #endif
683 1.1 scw
684 1.1 scw static void
685 1.1 scw npe_addstats(struct npe_softc *sc)
686 1.1 scw {
687 1.1 scw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
688 1.1 scw struct npestats *ns = sc->sc_stats;
689 1.1 scw
690 1.45 thorpej net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
691 1.54 riastrad if_statadd_ref(ifp, nsr, if_oerrors,
692 1.1 scw be32toh(ns->dot3StatsInternalMacTransmitErrors)
693 1.1 scw + be32toh(ns->dot3StatsCarrierSenseErrors)
694 1.1 scw + be32toh(ns->TxVLANIdFilterDiscards)
695 1.45 thorpej );
696 1.54 riastrad if_statadd_ref(ifp, nsr, if_ierrors,
697 1.45 thorpej be32toh(ns->dot3StatsFCSErrors)
698 1.1 scw + be32toh(ns->dot3StatsInternalMacReceiveErrors)
699 1.1 scw + be32toh(ns->RxOverrunDiscards)
700 1.1 scw + be32toh(ns->RxUnderflowEntryDiscards)
701 1.45 thorpej );
702 1.54 riastrad if_statadd_ref(ifp, nsr, if_collisions,
703 1.1 scw be32toh(ns->dot3StatsSingleCollisionFrames)
704 1.1 scw + be32toh(ns->dot3StatsMultipleCollisionFrames)
705 1.45 thorpej );
706 1.45 thorpej IF_STAT_PUTREF(ifp);
707 1.1 scw }
708 1.1 scw
709 1.1 scw static void
710 1.1 scw npe_tick(void *xsc)
711 1.1 scw {
712 1.1 scw #define ACK (NPE_RESETSTATS << NPE_MAC_MSGID_SHL)
713 1.1 scw struct npe_softc *sc = xsc;
714 1.1 scw uint32_t msg[2];
715 1.1 scw
716 1.1 scw /*
717 1.1 scw * NB: to avoid sleeping with the softc lock held we
718 1.1 scw * split the NPE msg processing into two parts. The
719 1.1 scw * request for statistics is sent w/o waiting for a
720 1.1 scw * reply and then on the next tick we retrieve the
721 1.1 scw * results. This works because npe_tick is the only
722 1.1 scw * code that talks via the mailbox's (except at setup).
723 1.1 scw * This likely can be handled better.
724 1.1 scw */
725 1.1 scw if (ixpnpe_recvmsg(sc->sc_npe, msg) == 0 && msg[0] == ACK) {
726 1.1 scw bus_dmamap_sync(sc->sc_dt, sc->sc_stats_map, 0,
727 1.1 scw sizeof(struct npestats), BUS_DMASYNC_POSTREAD);
728 1.1 scw npe_addstats(sc);
729 1.1 scw }
730 1.1 scw npe_updatestats(sc);
731 1.1 scw mii_tick(&sc->sc_mii);
732 1.1 scw
733 1.39 msaitoh /* Schedule next poll */
734 1.1 scw callout_reset(&sc->sc_tick_ch, hz, npe_tick, sc);
735 1.1 scw #undef ACK
736 1.1 scw }
737 1.1 scw
738 1.1 scw static void
739 1.6 matt npe_setmac(struct npe_softc *sc, const u_char *eaddr)
740 1.1 scw {
741 1.11 msaitoh
742 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_1, eaddr[0]);
743 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_2, eaddr[1]);
744 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_3, eaddr[2]);
745 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_4, eaddr[3]);
746 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_5, eaddr[4]);
747 1.1 scw WR4(sc, NPE_MAC_UNI_ADDR_6, eaddr[5]);
748 1.1 scw }
749 1.1 scw
750 1.1 scw static void
751 1.11 msaitoh npe_getmac(struct npe_softc *sc)
752 1.1 scw {
753 1.11 msaitoh uint8_t *eaddr = sc->sc_enaddr;
754 1.11 msaitoh
755 1.11 msaitoh if (npe_getmac_md != NULL) {
756 1.23 matt (*npe_getmac_md)(device_unit(sc->sc_dev), eaddr);
757 1.11 msaitoh } else {
758 1.11 msaitoh /*
759 1.11 msaitoh * Some system's unicast address appears to be loaded from
760 1.11 msaitoh * EEPROM on reset
761 1.11 msaitoh */
762 1.11 msaitoh eaddr[0] = RD4(sc, NPE_MAC_UNI_ADDR_1) & 0xff;
763 1.11 msaitoh eaddr[1] = RD4(sc, NPE_MAC_UNI_ADDR_2) & 0xff;
764 1.11 msaitoh eaddr[2] = RD4(sc, NPE_MAC_UNI_ADDR_3) & 0xff;
765 1.11 msaitoh eaddr[3] = RD4(sc, NPE_MAC_UNI_ADDR_4) & 0xff;
766 1.11 msaitoh eaddr[4] = RD4(sc, NPE_MAC_UNI_ADDR_5) & 0xff;
767 1.11 msaitoh eaddr[5] = RD4(sc, NPE_MAC_UNI_ADDR_6) & 0xff;
768 1.11 msaitoh }
769 1.1 scw }
770 1.1 scw
771 1.1 scw struct txdone {
772 1.1 scw struct npebuf *head;
773 1.1 scw struct npebuf **tail;
774 1.1 scw int count;
775 1.1 scw };
776 1.1 scw
777 1.1 scw static __inline void
778 1.1 scw npe_txdone_finish(struct npe_softc *sc, const struct txdone *td)
779 1.1 scw {
780 1.1 scw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
781 1.1 scw
782 1.1 scw *td->tail = sc->tx_free;
783 1.1 scw sc->tx_free = td->head;
784 1.1 scw /*
785 1.1 scw * We're no longer busy, so clear the busy flag and call the
786 1.1 scw * start routine to xmit more packets.
787 1.1 scw */
788 1.45 thorpej if_statadd(ifp, if_opackets, td->count);
789 1.1 scw ifp->if_timer = 0;
790 1.33 nonaka if_schedule_deferred_start(ifp);
791 1.1 scw }
792 1.1 scw
793 1.1 scw /*
794 1.1 scw * Q manager callback on tx done queue. Reap mbufs
795 1.1 scw * and return tx buffers to the free list. Finally
796 1.1 scw * restart output. Note the microcode has only one
797 1.1 scw * txdone q wired into it so we must use the NPE ID
798 1.1 scw * returned with each npehwbuf to decide where to
799 1.1 scw * send buffers.
800 1.1 scw */
801 1.1 scw static void
802 1.1 scw npe_txdone(int qid, void *arg)
803 1.1 scw {
804 1.1 scw #define P2V(a, dma) \
805 1.1 scw &(dma)->buf[((a) - (dma)->buf_phys) / sizeof(struct npehwbuf)]
806 1.1 scw struct npe_softc *sc;
807 1.1 scw struct npebuf *npe;
808 1.1 scw struct txdone *td, q[NPE_MAX];
809 1.1 scw uint32_t entry;
810 1.1 scw
811 1.1 scw /* XXX no NPE-A support */
812 1.1 scw q[NPE_B].tail = &q[NPE_B].head; q[NPE_B].count = 0;
813 1.1 scw q[NPE_C].tail = &q[NPE_C].head; q[NPE_C].count = 0;
814 1.1 scw /* XXX max # at a time? */
815 1.1 scw while (ixpqmgr_qread(qid, &entry) == 0) {
816 1.1 scw sc = npes[NPE_QM_Q_NPE(entry)];
817 1.1 scw DPRINTF(sc, "%s: entry 0x%x NPE %u port %u\n",
818 1.1 scw __func__, entry, NPE_QM_Q_NPE(entry), NPE_QM_Q_PORT(entry));
819 1.22 tls rnd_add_uint32(&sc->rnd_source, entry);
820 1.1 scw
821 1.1 scw npe = P2V(NPE_QM_Q_ADDR(entry), &sc->txdma);
822 1.1 scw m_freem(npe->ix_m);
823 1.1 scw npe->ix_m = NULL;
824 1.1 scw
825 1.1 scw td = &q[NPE_QM_Q_NPE(entry)];
826 1.1 scw *td->tail = npe;
827 1.1 scw td->tail = &npe->ix_next;
828 1.1 scw td->count++;
829 1.1 scw }
830 1.1 scw
831 1.1 scw if (q[NPE_B].count)
832 1.1 scw npe_txdone_finish(npes[NPE_B], &q[NPE_B]);
833 1.1 scw if (q[NPE_C].count)
834 1.1 scw npe_txdone_finish(npes[NPE_C], &q[NPE_C]);
835 1.1 scw #undef P2V
836 1.1 scw }
837 1.1 scw
838 1.1 scw static __inline struct mbuf *
839 1.1 scw npe_getcl(void)
840 1.1 scw {
841 1.1 scw struct mbuf *m;
842 1.1 scw
843 1.1 scw MGETHDR(m, M_DONTWAIT, MT_DATA);
844 1.1 scw if (m != NULL) {
845 1.1 scw MCLGET(m, M_DONTWAIT);
846 1.1 scw if ((m->m_flags & M_EXT) == 0) {
847 1.1 scw m_freem(m);
848 1.1 scw m = NULL;
849 1.1 scw }
850 1.1 scw }
851 1.39 msaitoh return m;
852 1.1 scw }
853 1.1 scw
854 1.1 scw static int
855 1.1 scw npe_rxbuf_init(struct npe_softc *sc, struct npebuf *npe, struct mbuf *m)
856 1.1 scw {
857 1.1 scw struct npehwbuf *hw;
858 1.1 scw int error;
859 1.1 scw
860 1.1 scw if (m == NULL) {
861 1.1 scw m = npe_getcl();
862 1.1 scw if (m == NULL)
863 1.1 scw return ENOBUFS;
864 1.1 scw }
865 1.11 msaitoh KASSERT(m->m_ext.ext_size >= (NPE_FRAME_SIZE_DEFAULT + ETHER_ALIGN));
866 1.11 msaitoh m->m_pkthdr.len = m->m_len = NPE_FRAME_SIZE_DEFAULT;
867 1.1 scw /* backload payload and align ip hdr */
868 1.11 msaitoh m->m_data = m->m_ext.ext_buf + (m->m_ext.ext_size
869 1.11 msaitoh - (NPE_FRAME_SIZE_DEFAULT + ETHER_ALIGN));
870 1.1 scw error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map, m,
871 1.40 msaitoh BUS_DMA_READ | BUS_DMA_NOWAIT);
872 1.1 scw if (error != 0) {
873 1.1 scw m_freem(m);
874 1.1 scw return error;
875 1.1 scw }
876 1.1 scw hw = npe->ix_hw;
877 1.1 scw hw->ix_ne[0].data = htobe32(npe->ix_map->dm_segs[0].ds_addr);
878 1.1 scw /* NB: NPE requires length be a multiple of 64 */
879 1.1 scw /* NB: buffer length is shifted in word */
880 1.1 scw hw->ix_ne[0].len = htobe32(npe->ix_map->dm_segs[0].ds_len << 16);
881 1.1 scw hw->ix_ne[0].next = 0;
882 1.1 scw npe->ix_m = m;
883 1.1 scw /* Flush the memory in the mbuf */
884 1.1 scw bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0, npe->ix_map->dm_mapsize,
885 1.1 scw BUS_DMASYNC_PREREAD);
886 1.1 scw return 0;
887 1.1 scw }
888 1.1 scw
889 1.1 scw /*
890 1.1 scw * RX q processing for a specific NPE. Claim entries
891 1.1 scw * from the hardware queue and pass the frames up the
892 1.1 scw * stack. Pass the rx buffers to the free list.
893 1.1 scw */
894 1.1 scw static void
895 1.1 scw npe_rxdone(int qid, void *arg)
896 1.1 scw {
897 1.1 scw #define P2V(a, dma) \
898 1.1 scw &(dma)->buf[((a) - (dma)->buf_phys) / sizeof(struct npehwbuf)]
899 1.1 scw struct npe_softc *sc = arg;
900 1.1 scw struct npedma *dma = &sc->rxdma;
901 1.1 scw uint32_t entry;
902 1.1 scw
903 1.1 scw while (ixpqmgr_qread(qid, &entry) == 0) {
904 1.1 scw struct npebuf *npe = P2V(NPE_QM_Q_ADDR(entry), dma);
905 1.1 scw struct mbuf *m;
906 1.1 scw
907 1.1 scw DPRINTF(sc, "%s: entry 0x%x neaddr 0x%x ne_len 0x%x\n",
908 1.1 scw __func__, entry, npe->ix_neaddr, npe->ix_hw->ix_ne[0].len);
909 1.22 tls rnd_add_uint32(&sc->rnd_source, entry);
910 1.1 scw /*
911 1.1 scw * Allocate a new mbuf to replenish the rx buffer.
912 1.1 scw * If doing so fails we drop the rx'd frame so we
913 1.1 scw * can reuse the previous mbuf. When we're able to
914 1.1 scw * allocate a new mbuf dispatch the mbuf w/ rx'd
915 1.1 scw * data up the stack and replace it with the newly
916 1.1 scw * allocated one.
917 1.1 scw */
918 1.1 scw m = npe_getcl();
919 1.1 scw if (m != NULL) {
920 1.1 scw struct mbuf *mrx = npe->ix_m;
921 1.1 scw struct npehwbuf *hw = npe->ix_hw;
922 1.1 scw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
923 1.1 scw
924 1.1 scw /* Flush mbuf memory for rx'd data */
925 1.1 scw bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0,
926 1.1 scw npe->ix_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
927 1.1 scw
928 1.1 scw /* XXX flush hw buffer; works now 'cuz coherent */
929 1.1 scw /* set m_len etc. per rx frame size */
930 1.1 scw mrx->m_len = be32toh(hw->ix_ne[0].len) & 0xffff;
931 1.1 scw mrx->m_pkthdr.len = mrx->m_len;
932 1.31 ozaki m_set_rcvif(mrx, ifp);
933 1.11 msaitoh /* Don't add M_HASFCS. See below */
934 1.11 msaitoh
935 1.11 msaitoh #if 1
936 1.11 msaitoh if (mrx->m_pkthdr.len < sizeof(struct ether_header)) {
937 1.11 msaitoh log(LOG_INFO, "%s: too short frame (len=%d)\n",
938 1.39 msaitoh device_xname(sc->sc_dev),
939 1.39 msaitoh mrx->m_pkthdr.len);
940 1.11 msaitoh /* Back out "newly allocated" mbuf. */
941 1.11 msaitoh m_freem(m);
942 1.45 thorpej if_statinc(ifp, if_ierrors);
943 1.11 msaitoh goto fail;
944 1.11 msaitoh }
945 1.11 msaitoh if ((ifp->if_flags & IFF_PROMISC) == 0) {
946 1.11 msaitoh struct ether_header *eh;
947 1.11 msaitoh
948 1.11 msaitoh /*
949 1.11 msaitoh * Workaround for "Non-Intel XScale Technology
950 1.11 msaitoh * Eratta" No. 29. AA:BB:CC:DD:EE:xF's packet
951 1.11 msaitoh * matches the filter (both unicast and
952 1.11 msaitoh * multicast).
953 1.11 msaitoh */
954 1.11 msaitoh eh = mtod(mrx, struct ether_header *);
955 1.11 msaitoh if (ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
956 1.39 msaitoh /* Unicast */
957 1.11 msaitoh
958 1.11 msaitoh if (sc->sc_enaddr[5] != eh->ether_dhost[5]) {
959 1.39 msaitoh /* Discard it */
960 1.11 msaitoh #if 0
961 1.11 msaitoh printf("discard it\n");
962 1.11 msaitoh #endif
963 1.11 msaitoh /*
964 1.11 msaitoh * Back out "newly allocated"
965 1.11 msaitoh * mbuf.
966 1.11 msaitoh */
967 1.11 msaitoh m_freem(m);
968 1.11 msaitoh goto fail;
969 1.11 msaitoh }
970 1.11 msaitoh } else if (memcmp(eh->ether_dhost,
971 1.11 msaitoh etherbroadcastaddr, 6) == 0) {
972 1.11 msaitoh /* Always accept broadcast packet*/
973 1.11 msaitoh } else {
974 1.11 msaitoh struct ethercom *ec = &sc->sc_ethercom;
975 1.11 msaitoh struct ether_multi *enm;
976 1.11 msaitoh struct ether_multistep step;
977 1.11 msaitoh int match = 0;
978 1.11 msaitoh
979 1.39 msaitoh /* Multicast */
980 1.11 msaitoh
981 1.41 msaitoh ETHER_LOCK(ec);
982 1.11 msaitoh ETHER_FIRST_MULTI(step, ec, enm);
983 1.11 msaitoh while (enm != NULL) {
984 1.11 msaitoh uint64_t lowint, highint, dest;
985 1.11 msaitoh
986 1.11 msaitoh lowint = MAC2UINT64(enm->enm_addrlo);
987 1.11 msaitoh highint = MAC2UINT64(enm->enm_addrhi);
988 1.11 msaitoh dest = MAC2UINT64(eh->ether_dhost);
989 1.11 msaitoh #if 0
990 1.11 msaitoh printf("%llx\n", lowint);
991 1.11 msaitoh printf("%llx\n", dest);
992 1.11 msaitoh printf("%llx\n", highint);
993 1.11 msaitoh #endif
994 1.11 msaitoh if ((lowint <= dest) && (dest <= highint)) {
995 1.11 msaitoh match = 1;
996 1.11 msaitoh break;
997 1.11 msaitoh }
998 1.11 msaitoh ETHER_NEXT_MULTI(step, enm);
999 1.11 msaitoh }
1000 1.41 msaitoh ETHER_UNLOCK(ec);
1001 1.41 msaitoh
1002 1.11 msaitoh if (match == 0) {
1003 1.39 msaitoh /* Discard it */
1004 1.11 msaitoh #if 0
1005 1.11 msaitoh printf("discard it(M)\n");
1006 1.11 msaitoh #endif
1007 1.11 msaitoh /*
1008 1.11 msaitoh * Back out "newly allocated"
1009 1.11 msaitoh * mbuf.
1010 1.11 msaitoh */
1011 1.11 msaitoh m_freem(m);
1012 1.11 msaitoh goto fail;
1013 1.11 msaitoh }
1014 1.11 msaitoh }
1015 1.11 msaitoh }
1016 1.11 msaitoh if (mrx->m_pkthdr.len > NPE_FRAME_SIZE_DEFAULT) {
1017 1.11 msaitoh log(LOG_INFO, "%s: oversized frame (len=%d)\n",
1018 1.23 matt device_xname(sc->sc_dev), mrx->m_pkthdr.len);
1019 1.11 msaitoh /* Back out "newly allocated" mbuf. */
1020 1.11 msaitoh m_freem(m);
1021 1.45 thorpej if_statinc(ifp, if_ierrors);
1022 1.11 msaitoh goto fail;
1023 1.11 msaitoh }
1024 1.11 msaitoh #endif
1025 1.11 msaitoh
1026 1.11 msaitoh /*
1027 1.11 msaitoh * Trim FCS!
1028 1.11 msaitoh * NPE always adds the FCS by this driver's setting,
1029 1.11 msaitoh * so we always trim it here and not add M_HASFCS.
1030 1.11 msaitoh */
1031 1.11 msaitoh m_adj(mrx, -ETHER_CRC_LEN);
1032 1.1 scw
1033 1.10 msaitoh /*
1034 1.10 msaitoh * Tap off here if there is a bpf listener.
1035 1.10 msaitoh */
1036 1.32 ozaki
1037 1.30 ozaki if_percpuq_enqueue(ifp->if_percpuq, mrx);
1038 1.1 scw } else {
1039 1.11 msaitoh fail:
1040 1.1 scw /* discard frame and re-use mbuf */
1041 1.1 scw m = npe->ix_m;
1042 1.1 scw }
1043 1.1 scw if (npe_rxbuf_init(sc, npe, m) == 0) {
1044 1.1 scw /* return npe buf to rx free list */
1045 1.1 scw ixpqmgr_qwrite(sc->rx_freeqid, npe->ix_neaddr);
1046 1.1 scw } else {
1047 1.1 scw /* XXX should not happen */
1048 1.1 scw }
1049 1.1 scw }
1050 1.1 scw #undef P2V
1051 1.1 scw }
1052 1.1 scw
1053 1.1 scw static void
1054 1.1 scw npe_startxmit(struct npe_softc *sc)
1055 1.1 scw {
1056 1.1 scw struct npedma *dma = &sc->txdma;
1057 1.1 scw int i;
1058 1.1 scw
1059 1.1 scw sc->tx_free = NULL;
1060 1.1 scw for (i = 0; i < dma->nbuf; i++) {
1061 1.1 scw struct npebuf *npe = &dma->buf[i];
1062 1.1 scw if (npe->ix_m != NULL) {
1063 1.1 scw /* NB: should not happen */
1064 1.1 scw printf("%s: %s: free mbuf at entry %u\n",
1065 1.23 matt device_xname(sc->sc_dev), __func__, i);
1066 1.1 scw m_freem(npe->ix_m);
1067 1.1 scw }
1068 1.1 scw npe->ix_m = NULL;
1069 1.1 scw npe->ix_next = sc->tx_free;
1070 1.1 scw sc->tx_free = npe;
1071 1.1 scw }
1072 1.1 scw }
1073 1.1 scw
1074 1.1 scw static void
1075 1.1 scw npe_startrecv(struct npe_softc *sc)
1076 1.1 scw {
1077 1.1 scw struct npedma *dma = &sc->rxdma;
1078 1.1 scw struct npebuf *npe;
1079 1.1 scw int i;
1080 1.1 scw
1081 1.1 scw for (i = 0; i < dma->nbuf; i++) {
1082 1.1 scw npe = &dma->buf[i];
1083 1.1 scw npe_rxbuf_init(sc, npe, npe->ix_m);
1084 1.39 msaitoh /* Set npe buf on rx free list */
1085 1.1 scw ixpqmgr_qwrite(sc->rx_freeqid, npe->ix_neaddr);
1086 1.1 scw }
1087 1.1 scw }
1088 1.1 scw
1089 1.1 scw static void
1090 1.13 msaitoh npeinit_macreg(struct npe_softc *sc)
1091 1.1 scw {
1092 1.1 scw
1093 1.1 scw /*
1094 1.1 scw * Reset MAC core.
1095 1.1 scw */
1096 1.1 scw WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_RESET);
1097 1.1 scw DELAY(NPE_MAC_RESET_DELAY);
1098 1.39 msaitoh /* Configure MAC to generate MDC clock */
1099 1.1 scw WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_MDC_EN);
1100 1.1 scw
1101 1.44 msaitoh /* Disable transmitter and receiver in the MAC */
1102 1.39 msaitoh WR4(sc, NPE_MAC_RX_CNTRL1,
1103 1.1 scw RD4(sc, NPE_MAC_RX_CNTRL1) &~ NPE_RX_CNTRL1_RX_EN);
1104 1.39 msaitoh WR4(sc, NPE_MAC_TX_CNTRL1,
1105 1.1 scw RD4(sc, NPE_MAC_TX_CNTRL1) &~ NPE_TX_CNTRL1_TX_EN);
1106 1.1 scw
1107 1.1 scw /*
1108 1.1 scw * Set the MAC core registers.
1109 1.1 scw */
1110 1.1 scw WR4(sc, NPE_MAC_INT_CLK_THRESH, 0x1); /* clock ratio: for ipx4xx */
1111 1.1 scw WR4(sc, NPE_MAC_TX_CNTRL2, 0xf); /* max retries */
1112 1.1 scw WR4(sc, NPE_MAC_RANDOM_SEED, 0x8); /* LFSR back-off seed */
1113 1.39 msaitoh /* Thresholds determined by NPE firmware FS */
1114 1.1 scw WR4(sc, NPE_MAC_THRESH_P_EMPTY, 0x12);
1115 1.1 scw WR4(sc, NPE_MAC_THRESH_P_FULL, 0x30);
1116 1.12 msaitoh WR4(sc, NPE_MAC_BUF_SIZE_TX, NPE_MAC_BUF_SIZE_TX_DEFAULT);
1117 1.12 msaitoh /* tx fifo threshold (bytes) */
1118 1.1 scw WR4(sc, NPE_MAC_TX_DEFER, 0x15); /* for single deferral */
1119 1.1 scw WR4(sc, NPE_MAC_RX_DEFER, 0x16); /* deferral on inter-frame gap*/
1120 1.1 scw WR4(sc, NPE_MAC_TX_TWO_DEFER_1, 0x8); /* for 2-part deferral */
1121 1.1 scw WR4(sc, NPE_MAC_TX_TWO_DEFER_2, 0x7); /* for 2-part deferral */
1122 1.12 msaitoh WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT);
1123 1.12 msaitoh /* assumes MII mode */
1124 1.1 scw WR4(sc, NPE_MAC_TX_CNTRL1,
1125 1.1 scw NPE_TX_CNTRL1_RETRY /* retry failed xmits */
1126 1.1 scw | NPE_TX_CNTRL1_FCS_EN /* append FCS */
1127 1.1 scw | NPE_TX_CNTRL1_2DEFER /* 2-part deferal */
1128 1.1 scw | NPE_TX_CNTRL1_PAD_EN); /* pad runt frames */
1129 1.1 scw /* XXX pad strip? */
1130 1.1 scw WR4(sc, NPE_MAC_RX_CNTRL1,
1131 1.1 scw NPE_RX_CNTRL1_CRC_EN /* include CRC/FCS */
1132 1.1 scw | NPE_RX_CNTRL1_PAUSE_EN); /* ena pause frame handling */
1133 1.1 scw WR4(sc, NPE_MAC_RX_CNTRL2, 0);
1134 1.13 msaitoh }
1135 1.1 scw
1136 1.16 msaitoh static void
1137 1.16 msaitoh npeinit_resetcb(void *xsc)
1138 1.16 msaitoh {
1139 1.16 msaitoh struct npe_softc *sc = xsc;
1140 1.16 msaitoh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1141 1.16 msaitoh uint32_t msg[2];
1142 1.16 msaitoh
1143 1.45 thorpej if_statinc(ifp, if_oerrors);
1144 1.16 msaitoh npeinit_locked(sc);
1145 1.16 msaitoh
1146 1.16 msaitoh msg[0] = NPE_NOTIFYMACRECOVERYDONE << NPE_MAC_MSGID_SHL
1147 1.16 msaitoh | (npeconfig[sc->sc_unit].macport << NPE_MAC_PORTID_SHL);
1148 1.16 msaitoh msg[1] = 0;
1149 1.16 msaitoh ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1150 1.16 msaitoh }
1151 1.16 msaitoh
1152 1.13 msaitoh /*
1153 1.13 msaitoh * Reset and initialize the chip
1154 1.13 msaitoh */
1155 1.13 msaitoh static void
1156 1.13 msaitoh npeinit_locked(void *xsc)
1157 1.13 msaitoh {
1158 1.13 msaitoh struct npe_softc *sc = xsc;
1159 1.13 msaitoh struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1160 1.13 msaitoh
1161 1.13 msaitoh /* Cancel any pending I/O. */
1162 1.13 msaitoh npestop(ifp, 0);
1163 1.13 msaitoh
1164 1.13 msaitoh /* Reset the chip to a known state. */
1165 1.13 msaitoh npeinit_macreg(sc);
1166 1.6 matt npe_setmac(sc, CLLADDR(ifp->if_sadl));
1167 1.15 msaitoh ether_mediachange(ifp);
1168 1.1 scw npe_setmcast(sc);
1169 1.1 scw
1170 1.1 scw npe_startxmit(sc);
1171 1.1 scw npe_startrecv(sc);
1172 1.1 scw
1173 1.1 scw ifp->if_flags |= IFF_RUNNING;
1174 1.1 scw ifp->if_timer = 0; /* just in case */
1175 1.1 scw
1176 1.44 msaitoh /* Enable transmitter and receiver in the MAC */
1177 1.39 msaitoh WR4(sc, NPE_MAC_RX_CNTRL1,
1178 1.1 scw RD4(sc, NPE_MAC_RX_CNTRL1) | NPE_RX_CNTRL1_RX_EN);
1179 1.39 msaitoh WR4(sc, NPE_MAC_TX_CNTRL1,
1180 1.1 scw RD4(sc, NPE_MAC_TX_CNTRL1) | NPE_TX_CNTRL1_TX_EN);
1181 1.1 scw
1182 1.1 scw callout_reset(&sc->sc_tick_ch, hz, npe_tick, sc);
1183 1.1 scw }
1184 1.1 scw
1185 1.1 scw static int
1186 1.1 scw npeinit(struct ifnet *ifp)
1187 1.1 scw {
1188 1.1 scw struct npe_softc *sc = ifp->if_softc;
1189 1.1 scw int s;
1190 1.1 scw
1191 1.1 scw s = splnet();
1192 1.1 scw npeinit_locked(sc);
1193 1.1 scw splx(s);
1194 1.1 scw
1195 1.39 msaitoh return 0;
1196 1.1 scw }
1197 1.1 scw
1198 1.1 scw /*
1199 1.1 scw * Defragment an mbuf chain, returning at most maxfrags separate
1200 1.1 scw * mbufs+clusters. If this is not possible NULL is returned and
1201 1.27 snj * the original mbuf chain is left in its present (potentially
1202 1.1 scw * modified) state. We use two techniques: collapsing consecutive
1203 1.1 scw * mbufs and replacing consecutive mbufs by a cluster.
1204 1.1 scw */
1205 1.1 scw static __inline struct mbuf *
1206 1.1 scw npe_defrag(struct mbuf *m0)
1207 1.1 scw {
1208 1.1 scw struct mbuf *m;
1209 1.1 scw
1210 1.1 scw MGETHDR(m, M_DONTWAIT, MT_DATA);
1211 1.1 scw if (m == NULL)
1212 1.39 msaitoh return NULL;
1213 1.35 maxv m_copy_pkthdr(m, m0);
1214 1.1 scw
1215 1.1 scw if ((m->m_len = m0->m_pkthdr.len) > MHLEN) {
1216 1.1 scw MCLGET(m, M_DONTWAIT);
1217 1.1 scw if ((m->m_flags & M_EXT) == 0) {
1218 1.1 scw m_freem(m);
1219 1.39 msaitoh return NULL;
1220 1.1 scw }
1221 1.1 scw }
1222 1.1 scw
1223 1.3 christos m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
1224 1.1 scw m_freem(m0);
1225 1.1 scw
1226 1.39 msaitoh return m;
1227 1.1 scw }
1228 1.1 scw
1229 1.1 scw /*
1230 1.1 scw * Dequeue packets and place on the h/w transmit queue.
1231 1.1 scw */
1232 1.1 scw static void
1233 1.1 scw npestart(struct ifnet *ifp)
1234 1.1 scw {
1235 1.1 scw struct npe_softc *sc = ifp->if_softc;
1236 1.1 scw struct npebuf *npe;
1237 1.1 scw struct npehwbuf *hw;
1238 1.1 scw struct mbuf *m, *n;
1239 1.1 scw bus_dma_segment_t *segs;
1240 1.1 scw int nseg, len, error, i;
1241 1.1 scw uint32_t next;
1242 1.1 scw
1243 1.52 thorpej if ((ifp->if_flags & IFF_RUNNING) == 0)
1244 1.1 scw return;
1245 1.1 scw
1246 1.1 scw while (sc->tx_free != NULL) {
1247 1.1 scw IFQ_DEQUEUE(&ifp->if_snd, m);
1248 1.13 msaitoh if (m == NULL)
1249 1.13 msaitoh break;
1250 1.1 scw npe = sc->tx_free;
1251 1.1 scw error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map, m,
1252 1.40 msaitoh BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1253 1.1 scw if (error == EFBIG) {
1254 1.1 scw n = npe_defrag(m);
1255 1.1 scw if (n == NULL) {
1256 1.1 scw printf("%s: %s: too many fragments\n",
1257 1.23 matt device_xname(sc->sc_dev), __func__);
1258 1.1 scw m_freem(m);
1259 1.1 scw return; /* XXX? */
1260 1.1 scw }
1261 1.1 scw m = n;
1262 1.1 scw error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map,
1263 1.40 msaitoh m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1264 1.1 scw }
1265 1.1 scw if (error != 0) {
1266 1.1 scw printf("%s: %s: error %u\n",
1267 1.23 matt device_xname(sc->sc_dev), __func__, error);
1268 1.1 scw m_freem(m);
1269 1.1 scw return; /* XXX? */
1270 1.1 scw }
1271 1.1 scw sc->tx_free = npe->ix_next;
1272 1.1 scw
1273 1.1 scw /*
1274 1.1 scw * Tap off here if there is a bpf listener.
1275 1.1 scw */
1276 1.34 msaitoh bpf_mtap(ifp, m, BPF_D_OUT);
1277 1.1 scw
1278 1.1 scw bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0,
1279 1.1 scw npe->ix_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
1280 1.1 scw
1281 1.1 scw npe->ix_m = m;
1282 1.1 scw hw = npe->ix_hw;
1283 1.1 scw len = m->m_pkthdr.len;
1284 1.1 scw nseg = npe->ix_map->dm_nsegs;
1285 1.1 scw segs = npe->ix_map->dm_segs;
1286 1.1 scw next = npe->ix_neaddr + sizeof(hw->ix_ne[0]);
1287 1.1 scw for (i = 0; i < nseg; i++) {
1288 1.1 scw hw->ix_ne[i].data = htobe32(segs[i].ds_addr);
1289 1.1 scw hw->ix_ne[i].len = htobe32((segs[i].ds_len<<16) | len);
1290 1.1 scw hw->ix_ne[i].next = htobe32(next);
1291 1.1 scw
1292 1.1 scw len = 0; /* zero for segments > 1 */
1293 1.1 scw next += sizeof(hw->ix_ne[0]);
1294 1.1 scw }
1295 1.1 scw hw->ix_ne[i-1].next = 0; /* zero last in chain */
1296 1.1 scw /* XXX flush descriptor instead of using uncached memory */
1297 1.1 scw
1298 1.1 scw DPRINTF(sc, "%s: qwrite(%u, 0x%x) ne_data %x ne_len 0x%x\n",
1299 1.1 scw __func__, sc->tx_qid, npe->ix_neaddr,
1300 1.1 scw hw->ix_ne[0].data, hw->ix_ne[0].len);
1301 1.1 scw /* stick it on the tx q */
1302 1.1 scw /* XXX add vlan priority */
1303 1.1 scw ixpqmgr_qwrite(sc->tx_qid, npe->ix_neaddr);
1304 1.1 scw
1305 1.1 scw ifp->if_timer = 5;
1306 1.1 scw }
1307 1.1 scw }
1308 1.1 scw
1309 1.1 scw static void
1310 1.1 scw npe_stopxmit(struct npe_softc *sc)
1311 1.1 scw {
1312 1.1 scw struct npedma *dma = &sc->txdma;
1313 1.1 scw int i;
1314 1.1 scw
1315 1.1 scw /* XXX qmgr */
1316 1.1 scw for (i = 0; i < dma->nbuf; i++) {
1317 1.1 scw struct npebuf *npe = &dma->buf[i];
1318 1.1 scw
1319 1.1 scw if (npe->ix_m != NULL) {
1320 1.1 scw bus_dmamap_unload(sc->sc_dt, npe->ix_map);
1321 1.1 scw m_freem(npe->ix_m);
1322 1.1 scw npe->ix_m = NULL;
1323 1.1 scw }
1324 1.1 scw }
1325 1.1 scw }
1326 1.1 scw
1327 1.1 scw static void
1328 1.1 scw npe_stoprecv(struct npe_softc *sc)
1329 1.1 scw {
1330 1.1 scw struct npedma *dma = &sc->rxdma;
1331 1.1 scw int i;
1332 1.1 scw
1333 1.1 scw /* XXX qmgr */
1334 1.1 scw for (i = 0; i < dma->nbuf; i++) {
1335 1.1 scw struct npebuf *npe = &dma->buf[i];
1336 1.1 scw
1337 1.1 scw if (npe->ix_m != NULL) {
1338 1.1 scw bus_dmamap_unload(sc->sc_dt, npe->ix_map);
1339 1.1 scw m_freem(npe->ix_m);
1340 1.1 scw npe->ix_m = NULL;
1341 1.1 scw }
1342 1.1 scw }
1343 1.1 scw }
1344 1.1 scw
1345 1.1 scw /*
1346 1.1 scw * Turn off interrupts, and stop the nic.
1347 1.1 scw */
1348 1.1 scw void
1349 1.1 scw npestop(struct ifnet *ifp, int disable)
1350 1.1 scw {
1351 1.1 scw struct npe_softc *sc = ifp->if_softc;
1352 1.1 scw
1353 1.44 msaitoh /* Disable transmitter and receiver in the MAC */
1354 1.39 msaitoh WR4(sc, NPE_MAC_RX_CNTRL1,
1355 1.1 scw RD4(sc, NPE_MAC_RX_CNTRL1) &~ NPE_RX_CNTRL1_RX_EN);
1356 1.39 msaitoh WR4(sc, NPE_MAC_TX_CNTRL1,
1357 1.1 scw RD4(sc, NPE_MAC_TX_CNTRL1) &~ NPE_TX_CNTRL1_TX_EN);
1358 1.1 scw
1359 1.1 scw callout_stop(&sc->sc_tick_ch);
1360 1.1 scw
1361 1.1 scw npe_stopxmit(sc);
1362 1.1 scw npe_stoprecv(sc);
1363 1.1 scw /* XXX go into loopback & drain q's? */
1364 1.1 scw /* XXX but beware of disabling tx above */
1365 1.1 scw
1366 1.1 scw /*
1367 1.1 scw * The MAC core rx/tx disable may leave the MAC hardware in an
1368 1.39 msaitoh * unpredictable state. A hw reset is executed before resetting
1369 1.1 scw * all the MAC parameters to a known value.
1370 1.1 scw */
1371 1.1 scw WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_RESET);
1372 1.1 scw DELAY(NPE_MAC_RESET_DELAY);
1373 1.1 scw WR4(sc, NPE_MAC_INT_CLK_THRESH, NPE_MAC_INT_CLK_THRESH_DEFAULT);
1374 1.1 scw WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_MDC_EN);
1375 1.13 msaitoh
1376 1.13 msaitoh ifp->if_timer = 0;
1377 1.52 thorpej ifp->if_flags &= ~IFF_RUNNING;
1378 1.1 scw }
1379 1.1 scw
1380 1.1 scw void
1381 1.1 scw npewatchdog(struct ifnet *ifp)
1382 1.1 scw {
1383 1.1 scw struct npe_softc *sc = ifp->if_softc;
1384 1.1 scw int s;
1385 1.1 scw
1386 1.23 matt aprint_error_dev(sc->sc_dev, "device timeout\n");
1387 1.1 scw s = splnet();
1388 1.45 thorpej if_statinc(ifp, if_oerrors);
1389 1.1 scw npeinit_locked(sc);
1390 1.1 scw splx(s);
1391 1.1 scw }
1392 1.1 scw
1393 1.1 scw static int
1394 1.3 christos npeioctl(struct ifnet *ifp, u_long cmd, void *data)
1395 1.1 scw {
1396 1.1 scw struct npe_softc *sc = ifp->if_softc;
1397 1.13 msaitoh struct ifreq *ifr = (struct ifreq *) data;
1398 1.1 scw int s, error = 0;
1399 1.1 scw
1400 1.1 scw s = splnet();
1401 1.1 scw
1402 1.13 msaitoh switch (cmd) {
1403 1.13 msaitoh case SIOCSIFMEDIA:
1404 1.13 msaitoh #if 0 /* not yet */
1405 1.13 msaitoh /* Flow control requires full-duplex mode. */
1406 1.13 msaitoh if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
1407 1.13 msaitoh (ifr->ifr_media & IFM_FDX) == 0)
1408 1.13 msaitoh ifr->ifr_media &= ~IFM_ETH_FMASK;
1409 1.13 msaitoh if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
1410 1.13 msaitoh if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
1411 1.13 msaitoh /* We can do both TXPAUSE and RXPAUSE. */
1412 1.13 msaitoh ifr->ifr_media |=
1413 1.13 msaitoh IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
1414 1.13 msaitoh }
1415 1.13 msaitoh sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
1416 1.13 msaitoh }
1417 1.13 msaitoh #endif
1418 1.13 msaitoh error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1419 1.13 msaitoh break;
1420 1.13 msaitoh case SIOCSIFFLAGS:
1421 1.40 msaitoh if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_RUNNING) {
1422 1.13 msaitoh /*
1423 1.13 msaitoh * If interface is marked down and it is running,
1424 1.13 msaitoh * then stop and disable it.
1425 1.13 msaitoh */
1426 1.49 riastrad if_stop(ifp, 1);
1427 1.40 msaitoh } else if ((ifp->if_flags & (IFF_UP |IFF_RUNNING)) == IFF_UP) {
1428 1.13 msaitoh /*
1429 1.13 msaitoh * If interface is marked up and it is stopped, then
1430 1.13 msaitoh * start it.
1431 1.13 msaitoh */
1432 1.50 riastrad error = if_init(ifp);
1433 1.13 msaitoh } else if ((ifp->if_flags & IFF_UP) != 0) {
1434 1.42 msaitoh u_short diff;
1435 1.13 msaitoh
1436 1.13 msaitoh /* Up (AND RUNNING). */
1437 1.13 msaitoh
1438 1.13 msaitoh diff = (ifp->if_flags ^ sc->sc_if_flags)
1439 1.40 msaitoh & (IFF_PROMISC | IFF_ALLMULTI);
1440 1.40 msaitoh if ((diff & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
1441 1.13 msaitoh /*
1442 1.51 andvar * If the difference between last flag and
1443 1.13 msaitoh * new flag only IFF_PROMISC or IFF_ALLMULTI,
1444 1.13 msaitoh * set multicast filter only (don't reset to
1445 1.13 msaitoh * prevent link down).
1446 1.13 msaitoh */
1447 1.13 msaitoh npe_setmcast(sc);
1448 1.13 msaitoh } else {
1449 1.13 msaitoh /*
1450 1.13 msaitoh * Reset the interface to pick up changes in
1451 1.13 msaitoh * any other flags that affect the hardware
1452 1.13 msaitoh * state.
1453 1.13 msaitoh */
1454 1.50 riastrad error = if_init(ifp);
1455 1.13 msaitoh }
1456 1.13 msaitoh }
1457 1.13 msaitoh sc->sc_if_flags = ifp->if_flags;
1458 1.13 msaitoh break;
1459 1.13 msaitoh default:
1460 1.13 msaitoh error = ether_ioctl(ifp, cmd, data);
1461 1.13 msaitoh if (error == ENETRESET) {
1462 1.13 msaitoh /*
1463 1.13 msaitoh * Multicast list has changed; set the hardware filter
1464 1.13 msaitoh * accordingly.
1465 1.13 msaitoh */
1466 1.13 msaitoh npe_setmcast(sc);
1467 1.13 msaitoh error = 0;
1468 1.1 scw }
1469 1.1 scw }
1470 1.1 scw
1471 1.1 scw npestart(ifp);
1472 1.1 scw
1473 1.1 scw splx(s);
1474 1.1 scw return error;
1475 1.1 scw }
1476 1.1 scw
1477 1.1 scw /*
1478 1.1 scw * Setup a traffic class -> rx queue mapping.
1479 1.1 scw */
1480 1.1 scw static int
1481 1.1 scw npe_setrxqosentry(struct npe_softc *sc, int classix, int trafclass, int qid)
1482 1.1 scw {
1483 1.1 scw int npeid = npeconfig[sc->sc_unit].npeid;
1484 1.1 scw uint32_t msg[2];
1485 1.1 scw
1486 1.15 msaitoh msg[0] = (NPE_SETRXQOSENTRY << NPE_MAC_MSGID_SHL) | (npeid << 20)
1487 1.15 msaitoh | classix;
1488 1.1 scw msg[1] = (trafclass << 24) | (1 << 23) | (qid << 16) | (qid << 4);
1489 1.1 scw return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1490 1.1 scw }
1491 1.1 scw
1492 1.1 scw /*
1493 1.1 scw * Update and reset the statistics in the NPE.
1494 1.1 scw */
1495 1.1 scw static int
1496 1.1 scw npe_updatestats(struct npe_softc *sc)
1497 1.1 scw {
1498 1.1 scw uint32_t msg[2];
1499 1.1 scw
1500 1.1 scw msg[0] = NPE_RESETSTATS << NPE_MAC_MSGID_SHL;
1501 1.1 scw msg[1] = sc->sc_stats_phys; /* physical address of stat block */
1502 1.1 scw return ixpnpe_sendmsg(sc->sc_npe, msg); /* NB: no recv */
1503 1.1 scw }
1504 1.1 scw
1505 1.1 scw #if 0
1506 1.1 scw /*
1507 1.1 scw * Get the current statistics block.
1508 1.1 scw */
1509 1.1 scw static int
1510 1.1 scw npe_getstats(struct npe_softc *sc)
1511 1.1 scw {
1512 1.1 scw uint32_t msg[2];
1513 1.1 scw
1514 1.1 scw msg[0] = NPE_GETSTATS << NPE_MAC_MSGID_SHL;
1515 1.1 scw msg[1] = sc->sc_stats_phys; /* physical address of stat block */
1516 1.1 scw return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1517 1.1 scw }
1518 1.1 scw
1519 1.1 scw /*
1520 1.1 scw * Query the image id of the loaded firmware.
1521 1.1 scw */
1522 1.1 scw static uint32_t
1523 1.1 scw npe_getimageid(struct npe_softc *sc)
1524 1.1 scw {
1525 1.1 scw uint32_t msg[2];
1526 1.1 scw
1527 1.1 scw msg[0] = NPE_GETSTATUS << NPE_MAC_MSGID_SHL;
1528 1.1 scw msg[1] = 0;
1529 1.1 scw return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg) == 0 ? msg[1] : 0;
1530 1.1 scw }
1531 1.1 scw
1532 1.1 scw /*
1533 1.1 scw * Enable/disable loopback.
1534 1.1 scw */
1535 1.1 scw static int
1536 1.1 scw npe_setloopback(struct npe_softc *sc, int ena)
1537 1.1 scw {
1538 1.1 scw uint32_t msg[2];
1539 1.1 scw
1540 1.1 scw msg[0] = (NPE_SETLOOPBACK << NPE_MAC_MSGID_SHL) | (ena != 0);
1541 1.1 scw msg[1] = 0;
1542 1.1 scw return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
1543 1.1 scw }
1544 1.1 scw #endif
1545 1.1 scw
1546 1.1 scw /*
1547 1.1 scw * MII bus support routines.
1548 1.1 scw *
1549 1.1 scw * NB: ixp425 has one PHY per NPE
1550 1.1 scw */
1551 1.1 scw static uint32_t
1552 1.1 scw npe_mii_mdio_read(struct npe_softc *sc, int reg)
1553 1.1 scw {
1554 1.1 scw #define MII_RD4(sc, reg) bus_space_read_4(sc->sc_iot, sc->sc_miih, reg)
1555 1.1 scw uint32_t v;
1556 1.1 scw
1557 1.1 scw /* NB: registers are known to be sequential */
1558 1.1 scw v = (MII_RD4(sc, reg+0) & 0xff) << 0;
1559 1.1 scw v |= (MII_RD4(sc, reg+4) & 0xff) << 8;
1560 1.1 scw v |= (MII_RD4(sc, reg+8) & 0xff) << 16;
1561 1.1 scw v |= (MII_RD4(sc, reg+12) & 0xff) << 24;
1562 1.1 scw return v;
1563 1.1 scw #undef MII_RD4
1564 1.1 scw }
1565 1.1 scw
1566 1.1 scw static void
1567 1.1 scw npe_mii_mdio_write(struct npe_softc *sc, int reg, uint32_t cmd)
1568 1.1 scw {
1569 1.1 scw #define MII_WR4(sc, reg, v) \
1570 1.1 scw bus_space_write_4(sc->sc_iot, sc->sc_miih, reg, v)
1571 1.1 scw
1572 1.1 scw /* NB: registers are known to be sequential */
1573 1.1 scw MII_WR4(sc, reg+0, cmd & 0xff);
1574 1.1 scw MII_WR4(sc, reg+4, (cmd >> 8) & 0xff);
1575 1.1 scw MII_WR4(sc, reg+8, (cmd >> 16) & 0xff);
1576 1.1 scw MII_WR4(sc, reg+12, (cmd >> 24) & 0xff);
1577 1.1 scw #undef MII_WR4
1578 1.1 scw }
1579 1.1 scw
1580 1.1 scw static int
1581 1.1 scw npe_mii_mdio_wait(struct npe_softc *sc)
1582 1.1 scw {
1583 1.1 scw #define MAXTRIES 100 /* XXX */
1584 1.1 scw uint32_t v;
1585 1.1 scw int i;
1586 1.1 scw
1587 1.1 scw for (i = 0; i < MAXTRIES; i++) {
1588 1.1 scw v = npe_mii_mdio_read(sc, NPE_MAC_MDIO_CMD);
1589 1.1 scw if ((v & NPE_MII_GO) == 0)
1590 1.36 msaitoh return 0;
1591 1.1 scw }
1592 1.36 msaitoh return ETIMEDOUT;
1593 1.1 scw #undef MAXTRIES
1594 1.1 scw }
1595 1.1 scw
1596 1.1 scw static int
1597 1.36 msaitoh npe_miibus_readreg(device_t self, int phy, int reg, uint16_t *val)
1598 1.1 scw {
1599 1.23 matt struct npe_softc *sc = device_private(self);
1600 1.1 scw uint32_t v;
1601 1.1 scw
1602 1.1 scw if (sc->sc_phy > IXPNPECF_PHY_DEFAULT && phy != sc->sc_phy)
1603 1.36 msaitoh return -1;
1604 1.1 scw v = (phy << NPE_MII_ADDR_SHL) | (reg << NPE_MII_REG_SHL)
1605 1.1 scw | NPE_MII_GO;
1606 1.1 scw npe_mii_mdio_write(sc, NPE_MAC_MDIO_CMD, v);
1607 1.36 msaitoh if (npe_mii_mdio_wait(sc) == 0)
1608 1.1 scw v = npe_mii_mdio_read(sc, NPE_MAC_MDIO_STS);
1609 1.1 scw else
1610 1.1 scw v = 0xffff | NPE_MII_READ_FAIL;
1611 1.36 msaitoh
1612 1.36 msaitoh if ((v & NPE_MII_READ_FAIL) != 0)
1613 1.36 msaitoh return -1;
1614 1.36 msaitoh
1615 1.36 msaitoh *val = v & 0xffff;
1616 1.36 msaitoh return 0;
1617 1.1 scw #undef MAXTRIES
1618 1.1 scw }
1619 1.1 scw
1620 1.36 msaitoh static int
1621 1.36 msaitoh npe_miibus_writereg(device_t self, int phy, int reg, uint16_t val)
1622 1.1 scw {
1623 1.23 matt struct npe_softc *sc = device_private(self);
1624 1.1 scw uint32_t v;
1625 1.1 scw
1626 1.1 scw if (sc->sc_phy > IXPNPECF_PHY_DEFAULT && phy != sc->sc_phy)
1627 1.36 msaitoh return -1;
1628 1.1 scw v = (phy << NPE_MII_ADDR_SHL) | (reg << NPE_MII_REG_SHL)
1629 1.36 msaitoh | val | NPE_MII_WRITE
1630 1.1 scw | NPE_MII_GO;
1631 1.1 scw npe_mii_mdio_write(sc, NPE_MAC_MDIO_CMD, v);
1632 1.36 msaitoh
1633 1.36 msaitoh return npe_mii_mdio_wait(sc);
1634 1.1 scw }
1635 1.1 scw
1636 1.1 scw static void
1637 1.23 matt npe_miibus_statchg(struct ifnet *ifp)
1638 1.1 scw {
1639 1.23 matt struct npe_softc *sc = ifp->if_softc;
1640 1.1 scw uint32_t tx1, rx1;
1641 1.16 msaitoh uint32_t randoff;
1642 1.1 scw
1643 1.39 msaitoh /* Sync MAC duplex state */
1644 1.1 scw tx1 = RD4(sc, NPE_MAC_TX_CNTRL1);
1645 1.1 scw rx1 = RD4(sc, NPE_MAC_RX_CNTRL1);
1646 1.1 scw if (sc->sc_mii.mii_media_active & IFM_FDX) {
1647 1.16 msaitoh WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT);
1648 1.1 scw tx1 &= ~NPE_TX_CNTRL1_DUPLEX;
1649 1.1 scw rx1 |= NPE_RX_CNTRL1_PAUSE_EN;
1650 1.1 scw } else {
1651 1.16 msaitoh struct timeval now;
1652 1.16 msaitoh getmicrotime(&now);
1653 1.16 msaitoh randoff = (RD4(sc, NPE_MAC_UNI_ADDR_6) ^ now.tv_usec)
1654 1.16 msaitoh & 0x7f;
1655 1.16 msaitoh WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT
1656 1.16 msaitoh + randoff);
1657 1.1 scw tx1 |= NPE_TX_CNTRL1_DUPLEX;
1658 1.1 scw rx1 &= ~NPE_RX_CNTRL1_PAUSE_EN;
1659 1.1 scw }
1660 1.1 scw WR4(sc, NPE_MAC_RX_CNTRL1, rx1);
1661 1.1 scw WR4(sc, NPE_MAC_TX_CNTRL1, tx1);
1662 1.1 scw }
1663