sgec.c revision 1.41 1 1.41 dholland /* $NetBSD: sgec.c,v 1.41 2015/08/30 04:02:06 dholland Exp $ */
2 1.1 ragge /*
3 1.1 ragge * Copyright (c) 1999 Ludd, University of Lule}, Sweden. All rights reserved.
4 1.1 ragge *
5 1.1 ragge * Redistribution and use in source and binary forms, with or without
6 1.1 ragge * modification, are permitted provided that the following conditions
7 1.1 ragge * are met:
8 1.1 ragge * 1. Redistributions of source code must retain the above copyright
9 1.1 ragge * notice, this list of conditions and the following disclaimer.
10 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 ragge * notice, this list of conditions and the following disclaimer in the
12 1.1 ragge * documentation and/or other materials provided with the distribution.
13 1.1 ragge * 3. All advertising materials mentioning features or use of this software
14 1.1 ragge * must display the following acknowledgement:
15 1.26 perry * This product includes software developed at Ludd, University of
16 1.1 ragge * Lule}, Sweden and its contributors.
17 1.1 ragge * 4. The name of the author may not be used to endorse or promote products
18 1.1 ragge * derived from this software without specific prior written permission
19 1.1 ragge *
20 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 ragge * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 ragge * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 ragge * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 ragge * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 ragge * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 ragge * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 ragge * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 ragge * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 ragge * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 ragge */
31 1.1 ragge
32 1.1 ragge /*
33 1.1 ragge * Driver for the SGEC (Second Generation Ethernet Controller), sitting
34 1.26 perry * on for example the VAX 4000/300 (KA670).
35 1.1 ragge *
36 1.1 ragge * The SGEC looks like a mixture of the DEQNA and the TULIP. Fun toy.
37 1.1 ragge *
38 1.1 ragge * Even though the chip is capable to use virtual addresses (read the
39 1.1 ragge * System Page Table directly) this driver doesn't do so, and there
40 1.1 ragge * is no benefit in doing it either in NetBSD of today.
41 1.1 ragge *
42 1.1 ragge * Things that is still to do:
43 1.1 ragge * Collect statistics.
44 1.1 ragge * Use imperfect filtering when many multicast addresses.
45 1.1 ragge */
46 1.18 lukem
47 1.18 lukem #include <sys/cdefs.h>
48 1.41 dholland __KERNEL_RCSID(0, "$NetBSD: sgec.c,v 1.41 2015/08/30 04:02:06 dholland Exp $");
49 1.1 ragge
50 1.1 ragge #include "opt_inet.h"
51 1.1 ragge
52 1.1 ragge #include <sys/param.h>
53 1.1 ragge #include <sys/mbuf.h>
54 1.1 ragge #include <sys/socket.h>
55 1.1 ragge #include <sys/device.h>
56 1.1 ragge #include <sys/systm.h>
57 1.1 ragge #include <sys/sockio.h>
58 1.1 ragge
59 1.1 ragge #include <net/if.h>
60 1.1 ragge #include <net/if_ether.h>
61 1.1 ragge #include <net/if_dl.h>
62 1.1 ragge
63 1.1 ragge #include <netinet/in.h>
64 1.1 ragge #include <netinet/if_inarp.h>
65 1.1 ragge
66 1.1 ragge #include <net/bpf.h>
67 1.1 ragge #include <net/bpfdesc.h>
68 1.1 ragge
69 1.34 ad #include <sys/bus.h>
70 1.1 ragge
71 1.1 ragge #include <dev/ic/sgecreg.h>
72 1.1 ragge #include <dev/ic/sgecvar.h>
73 1.1 ragge
74 1.25 perry static void zeinit(struct ze_softc *);
75 1.25 perry static void zestart(struct ifnet *);
76 1.28 christos static int zeioctl(struct ifnet *, u_long, void *);
77 1.25 perry static int ze_add_rxbuf(struct ze_softc *, int);
78 1.25 perry static void ze_setup(struct ze_softc *);
79 1.25 perry static void zetimeout(struct ifnet *);
80 1.35 matt static bool zereset(struct ze_softc *);
81 1.1 ragge
82 1.1 ragge #define ZE_WCSR(csr, val) \
83 1.1 ragge bus_space_write_4(sc->sc_iot, sc->sc_ioh, csr, val)
84 1.1 ragge #define ZE_RCSR(csr) \
85 1.1 ragge bus_space_read_4(sc->sc_iot, sc->sc_ioh, csr)
86 1.1 ragge
87 1.1 ragge /*
88 1.1 ragge * Interface exists: make available by filling in network interface
89 1.1 ragge * record. System will initialize the interface when it is ready
90 1.1 ragge * to accept packets.
91 1.1 ragge */
92 1.1 ragge void
93 1.35 matt sgec_attach(struct ze_softc *sc)
94 1.1 ragge {
95 1.35 matt struct ifnet *ifp = &sc->sc_if;
96 1.35 matt struct ze_tdes *tp;
97 1.35 matt struct ze_rdes *rp;
98 1.1 ragge bus_dma_segment_t seg;
99 1.1 ragge int i, rseg, error;
100 1.1 ragge
101 1.1 ragge /*
102 1.1 ragge * Allocate DMA safe memory for descriptors and setup memory.
103 1.1 ragge */
104 1.35 matt error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct ze_cdata),
105 1.35 matt PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
106 1.35 matt if (error) {
107 1.35 matt aprint_error(": unable to allocate control data, error = %d\n",
108 1.1 ragge error);
109 1.1 ragge goto fail_0;
110 1.1 ragge }
111 1.1 ragge
112 1.35 matt error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, sizeof(struct ze_cdata),
113 1.35 matt (void **)&sc->sc_zedata, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
114 1.35 matt if (error) {
115 1.35 matt aprint_error(
116 1.35 matt ": unable to map control data, error = %d\n", error);
117 1.1 ragge goto fail_1;
118 1.1 ragge }
119 1.1 ragge
120 1.35 matt error = bus_dmamap_create(sc->sc_dmat, sizeof(struct ze_cdata), 1,
121 1.35 matt sizeof(struct ze_cdata), 0, BUS_DMA_NOWAIT, &sc->sc_cmap);
122 1.35 matt if (error) {
123 1.35 matt aprint_error(
124 1.35 matt ": unable to create control data DMA map, error = %d\n",
125 1.1 ragge error);
126 1.1 ragge goto fail_2;
127 1.1 ragge }
128 1.1 ragge
129 1.35 matt error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmap, sc->sc_zedata,
130 1.35 matt sizeof(struct ze_cdata), NULL, BUS_DMA_NOWAIT);
131 1.35 matt if (error) {
132 1.35 matt aprint_error(
133 1.35 matt ": unable to load control data DMA map, error = %d\n",
134 1.1 ragge error);
135 1.1 ragge goto fail_3;
136 1.1 ragge }
137 1.1 ragge
138 1.1 ragge /*
139 1.1 ragge * Zero the newly allocated memory.
140 1.1 ragge */
141 1.16 thorpej memset(sc->sc_zedata, 0, sizeof(struct ze_cdata));
142 1.35 matt
143 1.1 ragge /*
144 1.1 ragge * Create the transmit descriptor DMA maps.
145 1.1 ragge */
146 1.35 matt for (i = 0; error == 0 && i < TXDESCS; i++) {
147 1.35 matt error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
148 1.29 matt TXDESCS - 1, MCLBYTES, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
149 1.35 matt &sc->sc_xmtmap[i]);
150 1.35 matt }
151 1.35 matt if (error) {
152 1.35 matt aprint_error(": unable to create tx DMA map %d, error = %d\n",
153 1.35 matt i, error);
154 1.35 matt goto fail_4;
155 1.1 ragge }
156 1.1 ragge
157 1.1 ragge /*
158 1.1 ragge * Create receive buffer DMA maps.
159 1.1 ragge */
160 1.35 matt for (i = 0; error == 0 && i < RXDESCS; i++) {
161 1.35 matt error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
162 1.35 matt MCLBYTES, 0, BUS_DMA_NOWAIT, &sc->sc_rcvmap[i]);
163 1.35 matt }
164 1.35 matt if (error) {
165 1.35 matt aprint_error(": unable to create rx DMA map %d, error = %d\n",
166 1.35 matt i, error);
167 1.35 matt goto fail_5;
168 1.1 ragge }
169 1.35 matt
170 1.1 ragge /*
171 1.1 ragge * Pre-allocate the receive buffers.
172 1.1 ragge */
173 1.35 matt for (i = 0; error == 0 && i < RXDESCS; i++) {
174 1.35 matt error = ze_add_rxbuf(sc, i);
175 1.35 matt }
176 1.35 matt
177 1.35 matt if (error) {
178 1.35 matt aprint_error(
179 1.35 matt ": unable to allocate or map rx buffer %d, error = %d\n",
180 1.35 matt i, error);
181 1.35 matt goto fail_6;
182 1.1 ragge }
183 1.5 matt
184 1.5 matt /* For vmstat -i
185 1.5 matt */
186 1.6 matt evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
187 1.35 matt device_xname(sc->sc_dev), "intr");
188 1.30 matt evcnt_attach_dynamic(&sc->sc_rxintrcnt, EVCNT_TYPE_INTR,
189 1.35 matt &sc->sc_intrcnt, device_xname(sc->sc_dev), "rx intr");
190 1.30 matt evcnt_attach_dynamic(&sc->sc_txintrcnt, EVCNT_TYPE_INTR,
191 1.35 matt &sc->sc_intrcnt, device_xname(sc->sc_dev), "tx intr");
192 1.30 matt evcnt_attach_dynamic(&sc->sc_txdraincnt, EVCNT_TYPE_INTR,
193 1.35 matt &sc->sc_intrcnt, device_xname(sc->sc_dev), "tx drain");
194 1.30 matt evcnt_attach_dynamic(&sc->sc_nobufintrcnt, EVCNT_TYPE_INTR,
195 1.35 matt &sc->sc_intrcnt, device_xname(sc->sc_dev), "nobuf intr");
196 1.30 matt evcnt_attach_dynamic(&sc->sc_nointrcnt, EVCNT_TYPE_INTR,
197 1.35 matt &sc->sc_intrcnt, device_xname(sc->sc_dev), "no intr");
198 1.1 ragge
199 1.1 ragge /*
200 1.1 ragge * Create ring loops of the buffer chains.
201 1.1 ragge * This is only done once.
202 1.1 ragge */
203 1.1 ragge sc->sc_pzedata = (struct ze_cdata *)sc->sc_cmap->dm_segs[0].ds_addr;
204 1.1 ragge
205 1.1 ragge rp = sc->sc_zedata->zc_recv;
206 1.1 ragge rp[RXDESCS].ze_framelen = ZE_FRAMELEN_OW;
207 1.1 ragge rp[RXDESCS].ze_rdes1 = ZE_RDES1_CA;
208 1.1 ragge rp[RXDESCS].ze_bufaddr = (char *)sc->sc_pzedata->zc_recv;
209 1.1 ragge
210 1.1 ragge tp = sc->sc_zedata->zc_xmit;
211 1.1 ragge tp[TXDESCS].ze_tdr = ZE_TDR_OW;
212 1.1 ragge tp[TXDESCS].ze_tdes1 = ZE_TDES1_CA;
213 1.1 ragge tp[TXDESCS].ze_bufaddr = (char *)sc->sc_pzedata->zc_xmit;
214 1.1 ragge
215 1.1 ragge if (zereset(sc))
216 1.1 ragge return;
217 1.1 ragge
218 1.35 matt strcpy(ifp->if_xname, device_xname(sc->sc_dev));
219 1.1 ragge ifp->if_softc = sc;
220 1.1 ragge ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
221 1.1 ragge ifp->if_start = zestart;
222 1.1 ragge ifp->if_ioctl = zeioctl;
223 1.1 ragge ifp->if_watchdog = zetimeout;
224 1.11 thorpej IFQ_SET_READY(&ifp->if_snd);
225 1.1 ragge
226 1.1 ragge /*
227 1.1 ragge * Attach the interface.
228 1.1 ragge */
229 1.1 ragge if_attach(ifp);
230 1.1 ragge ether_ifattach(ifp, sc->sc_enaddr);
231 1.1 ragge
232 1.35 matt aprint_normal("\n");
233 1.35 matt aprint_normal_dev(sc->sc_dev, "hardware address %s\n",
234 1.1 ragge ether_sprintf(sc->sc_enaddr));
235 1.1 ragge return;
236 1.1 ragge
237 1.1 ragge /*
238 1.1 ragge * Free any resources we've allocated during the failed attach
239 1.1 ragge * attempt. Do this in reverse order and fall through.
240 1.1 ragge */
241 1.1 ragge fail_6:
242 1.1 ragge for (i = 0; i < RXDESCS; i++) {
243 1.1 ragge if (sc->sc_rxmbuf[i] != NULL) {
244 1.40 martin bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
245 1.1 ragge m_freem(sc->sc_rxmbuf[i]);
246 1.1 ragge }
247 1.1 ragge }
248 1.1 ragge fail_5:
249 1.40 martin for (i = 0; i < TXDESCS; i++) {
250 1.1 ragge if (sc->sc_xmtmap[i] != NULL)
251 1.1 ragge bus_dmamap_destroy(sc->sc_dmat, sc->sc_xmtmap[i]);
252 1.1 ragge }
253 1.1 ragge fail_4:
254 1.40 martin for (i = 0; i < RXDESCS; i++) {
255 1.1 ragge if (sc->sc_rcvmap[i] != NULL)
256 1.1 ragge bus_dmamap_destroy(sc->sc_dmat, sc->sc_rcvmap[i]);
257 1.1 ragge }
258 1.1 ragge bus_dmamap_unload(sc->sc_dmat, sc->sc_cmap);
259 1.1 ragge fail_3:
260 1.1 ragge bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
261 1.1 ragge fail_2:
262 1.28 christos bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_zedata,
263 1.1 ragge sizeof(struct ze_cdata));
264 1.1 ragge fail_1:
265 1.1 ragge bus_dmamem_free(sc->sc_dmat, &seg, rseg);
266 1.1 ragge fail_0:
267 1.1 ragge return;
268 1.1 ragge }
269 1.1 ragge
270 1.1 ragge /*
271 1.1 ragge * Initialization of interface.
272 1.1 ragge */
273 1.1 ragge void
274 1.35 matt zeinit(struct ze_softc *sc)
275 1.1 ragge {
276 1.35 matt struct ifnet *ifp = &sc->sc_if;
277 1.1 ragge struct ze_cdata *zc = sc->sc_zedata;
278 1.1 ragge int i;
279 1.1 ragge
280 1.1 ragge /*
281 1.1 ragge * Reset the interface.
282 1.1 ragge */
283 1.1 ragge if (zereset(sc))
284 1.1 ragge return;
285 1.1 ragge
286 1.30 matt sc->sc_nexttx = sc->sc_inq = sc->sc_lastack = sc->sc_txcnt = 0;
287 1.1 ragge /*
288 1.1 ragge * Release and init transmit descriptors.
289 1.1 ragge */
290 1.1 ragge for (i = 0; i < TXDESCS; i++) {
291 1.29 matt if (sc->sc_xmtmap[i]->dm_nsegs > 0)
292 1.29 matt bus_dmamap_unload(sc->sc_dmat, sc->sc_xmtmap[i]);
293 1.1 ragge if (sc->sc_txmbuf[i]) {
294 1.1 ragge m_freem(sc->sc_txmbuf[i]);
295 1.1 ragge sc->sc_txmbuf[i] = 0;
296 1.1 ragge }
297 1.1 ragge zc->zc_xmit[i].ze_tdr = 0; /* Clear valid bit */
298 1.1 ragge }
299 1.1 ragge
300 1.1 ragge
301 1.1 ragge /*
302 1.1 ragge * Init receive descriptors.
303 1.1 ragge */
304 1.1 ragge for (i = 0; i < RXDESCS; i++)
305 1.1 ragge zc->zc_recv[i].ze_framelen = ZE_FRAMELEN_OW;
306 1.1 ragge sc->sc_nextrx = 0;
307 1.1 ragge
308 1.1 ragge ZE_WCSR(ZE_CSR6, ZE_NICSR6_IE|ZE_NICSR6_BL_8|ZE_NICSR6_ST|
309 1.1 ragge ZE_NICSR6_SR|ZE_NICSR6_DC);
310 1.1 ragge
311 1.1 ragge ifp->if_flags |= IFF_RUNNING;
312 1.1 ragge ifp->if_flags &= ~IFF_OACTIVE;
313 1.1 ragge
314 1.1 ragge /*
315 1.1 ragge * Send a setup frame.
316 1.1 ragge * This will start the transmit machinery as well.
317 1.1 ragge */
318 1.1 ragge ze_setup(sc);
319 1.1 ragge
320 1.1 ragge }
321 1.1 ragge
322 1.1 ragge /*
323 1.1 ragge * Start output on interface.
324 1.1 ragge */
325 1.1 ragge void
326 1.35 matt zestart(struct ifnet *ifp)
327 1.1 ragge {
328 1.1 ragge struct ze_softc *sc = ifp->if_softc;
329 1.1 ragge struct ze_cdata *zc = sc->sc_zedata;
330 1.1 ragge paddr_t buffer;
331 1.29 matt struct mbuf *m;
332 1.30 matt int nexttx, starttx;
333 1.30 matt int len, i, totlen, error;
334 1.4 matt int old_inq = sc->sc_inq;
335 1.41 dholland uint16_t orword, tdr = 0;
336 1.29 matt bus_dmamap_t map;
337 1.1 ragge
338 1.1 ragge while (sc->sc_inq < (TXDESCS - 1)) {
339 1.1 ragge
340 1.1 ragge if (sc->sc_setup) {
341 1.1 ragge ze_setup(sc);
342 1.1 ragge continue;
343 1.1 ragge }
344 1.29 matt nexttx = sc->sc_nexttx;
345 1.11 thorpej IFQ_POLL(&sc->sc_if.if_snd, m);
346 1.1 ragge if (m == 0)
347 1.1 ragge goto out;
348 1.1 ragge /*
349 1.1 ragge * Count number of mbufs in chain.
350 1.1 ragge * Always do DMA directly from mbufs, therefore the transmit
351 1.1 ragge * ring is really big.
352 1.1 ragge */
353 1.29 matt map = sc->sc_xmtmap[nexttx];
354 1.29 matt error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
355 1.29 matt BUS_DMA_WRITE);
356 1.29 matt if (error) {
357 1.35 matt aprint_error_dev(sc->sc_dev,
358 1.35 matt "zestart: load_mbuf failed: %d", error);
359 1.29 matt goto out;
360 1.29 matt }
361 1.29 matt
362 1.29 matt if (map->dm_nsegs >= TXDESCS)
363 1.1 ragge panic("zestart"); /* XXX */
364 1.1 ragge
365 1.29 matt if ((map->dm_nsegs + sc->sc_inq) >= (TXDESCS - 1)) {
366 1.29 matt bus_dmamap_unload(sc->sc_dmat, map);
367 1.1 ragge ifp->if_flags |= IFF_OACTIVE;
368 1.1 ragge goto out;
369 1.1 ragge }
370 1.26 perry
371 1.1 ragge /*
372 1.1 ragge * m now points to a mbuf chain that can be loaded.
373 1.1 ragge * Loop around and set it.
374 1.1 ragge */
375 1.1 ragge totlen = 0;
376 1.29 matt orword = ZE_TDES1_FS;
377 1.30 matt starttx = nexttx;
378 1.29 matt for (i = 0; i < map->dm_nsegs; i++) {
379 1.29 matt buffer = map->dm_segs[i].ds_addr;
380 1.29 matt len = map->dm_segs[i].ds_len;
381 1.29 matt
382 1.30 matt KASSERT(len > 0);
383 1.1 ragge
384 1.1 ragge totlen += len;
385 1.1 ragge /* Word alignment calc */
386 1.1 ragge if (totlen == m->m_pkthdr.len) {
387 1.30 matt sc->sc_txcnt += map->dm_nsegs;
388 1.30 matt if (sc->sc_txcnt >= TXDESCS * 3 / 4) {
389 1.30 matt orword |= ZE_TDES1_IC;
390 1.30 matt sc->sc_txcnt = 0;
391 1.30 matt }
392 1.30 matt orword |= ZE_TDES1_LS;
393 1.29 matt sc->sc_txmbuf[nexttx] = m;
394 1.1 ragge }
395 1.29 matt zc->zc_xmit[nexttx].ze_bufsize = len;
396 1.29 matt zc->zc_xmit[nexttx].ze_bufaddr = (char *)buffer;
397 1.29 matt zc->zc_xmit[nexttx].ze_tdes1 = orword;
398 1.30 matt zc->zc_xmit[nexttx].ze_tdr = tdr;
399 1.29 matt
400 1.29 matt if (++nexttx == TXDESCS)
401 1.29 matt nexttx = 0;
402 1.29 matt orword = 0;
403 1.30 matt tdr = ZE_TDR_OW;
404 1.1 ragge }
405 1.29 matt
406 1.29 matt sc->sc_inq += map->dm_nsegs;
407 1.29 matt
408 1.11 thorpej IFQ_DEQUEUE(&ifp->if_snd, m);
409 1.1 ragge #ifdef DIAGNOSTIC
410 1.1 ragge if (totlen != m->m_pkthdr.len)
411 1.1 ragge panic("zestart: len fault");
412 1.1 ragge #endif
413 1.30 matt /*
414 1.30 matt * Turn ownership of the packet over to the device.
415 1.30 matt */
416 1.30 matt zc->zc_xmit[starttx].ze_tdr = ZE_TDR_OW;
417 1.1 ragge
418 1.1 ragge /*
419 1.1 ragge * Kick off the transmit logic, if it is stopped.
420 1.1 ragge */
421 1.1 ragge if ((ZE_RCSR(ZE_CSR5) & ZE_NICSR5_TS) != ZE_NICSR5_TS_RUN)
422 1.1 ragge ZE_WCSR(ZE_CSR1, -1);
423 1.29 matt sc->sc_nexttx = nexttx;
424 1.1 ragge }
425 1.1 ragge if (sc->sc_inq == (TXDESCS - 1))
426 1.1 ragge ifp->if_flags |= IFF_OACTIVE;
427 1.1 ragge
428 1.4 matt out: if (old_inq < sc->sc_inq)
429 1.1 ragge ifp->if_timer = 5; /* If transmit logic dies */
430 1.1 ragge }
431 1.1 ragge
432 1.1 ragge int
433 1.35 matt sgec_intr(struct ze_softc *sc)
434 1.1 ragge {
435 1.1 ragge struct ze_cdata *zc = sc->sc_zedata;
436 1.1 ragge struct ifnet *ifp = &sc->sc_if;
437 1.1 ragge struct mbuf *m;
438 1.1 ragge int csr, len;
439 1.1 ragge
440 1.1 ragge csr = ZE_RCSR(ZE_CSR5);
441 1.30 matt if ((csr & ZE_NICSR5_IS) == 0) { /* Wasn't we */
442 1.30 matt sc->sc_nointrcnt.ev_count++;
443 1.1 ragge return 0;
444 1.30 matt }
445 1.1 ragge ZE_WCSR(ZE_CSR5, csr);
446 1.1 ragge
447 1.30 matt if (csr & ZE_NICSR5_RU)
448 1.30 matt sc->sc_nobufintrcnt.ev_count++;
449 1.30 matt
450 1.24 thorpej if (csr & ZE_NICSR5_RI) {
451 1.30 matt sc->sc_rxintrcnt.ev_count++;
452 1.1 ragge while ((zc->zc_recv[sc->sc_nextrx].ze_framelen &
453 1.1 ragge ZE_FRAMELEN_OW) == 0) {
454 1.1 ragge
455 1.3 matt ifp->if_ipackets++;
456 1.1 ragge m = sc->sc_rxmbuf[sc->sc_nextrx];
457 1.1 ragge len = zc->zc_recv[sc->sc_nextrx].ze_framelen;
458 1.1 ragge ze_add_rxbuf(sc, sc->sc_nextrx);
459 1.1 ragge if (++sc->sc_nextrx == RXDESCS)
460 1.1 ragge sc->sc_nextrx = 0;
461 1.24 thorpej if (len < ETHER_MIN_LEN) {
462 1.24 thorpej ifp->if_ierrors++;
463 1.24 thorpej m_freem(m);
464 1.24 thorpej } else {
465 1.24 thorpej m->m_pkthdr.rcvif = ifp;
466 1.24 thorpej m->m_pkthdr.len = m->m_len =
467 1.24 thorpej len - ETHER_CRC_LEN;
468 1.38 joerg bpf_mtap(ifp, m);
469 1.24 thorpej (*ifp->if_input)(ifp, m);
470 1.24 thorpej }
471 1.1 ragge }
472 1.24 thorpej }
473 1.1 ragge
474 1.30 matt if (csr & ZE_NICSR5_TI)
475 1.30 matt sc->sc_txintrcnt.ev_count++;
476 1.30 matt if (sc->sc_lastack != sc->sc_nexttx) {
477 1.30 matt int lastack;
478 1.30 matt for (lastack = sc->sc_lastack; lastack != sc->sc_nexttx; ) {
479 1.29 matt bus_dmamap_t map;
480 1.29 matt int nlastack;
481 1.1 ragge
482 1.30 matt if ((zc->zc_xmit[lastack].ze_tdr & ZE_TDR_OW) != 0)
483 1.1 ragge break;
484 1.1 ragge
485 1.29 matt if ((zc->zc_xmit[lastack].ze_tdes1 & ZE_TDES1_DT) ==
486 1.29 matt ZE_TDES1_DT_SETUP) {
487 1.29 matt if (++lastack == TXDESCS)
488 1.29 matt lastack = 0;
489 1.29 matt sc->sc_inq--;
490 1.1 ragge continue;
491 1.1 ragge }
492 1.29 matt
493 1.29 matt KASSERT(zc->zc_xmit[lastack].ze_tdes1 & ZE_TDES1_FS);
494 1.29 matt map = sc->sc_xmtmap[lastack];
495 1.29 matt KASSERT(map->dm_nsegs > 0);
496 1.29 matt nlastack = (lastack + map->dm_nsegs - 1) % TXDESCS;
497 1.29 matt if (zc->zc_xmit[nlastack].ze_tdr & ZE_TDR_OW)
498 1.29 matt break;
499 1.29 matt lastack = nlastack;
500 1.30 matt if (sc->sc_txcnt > map->dm_nsegs)
501 1.30 matt sc->sc_txcnt -= map->dm_nsegs;
502 1.30 matt else
503 1.30 matt sc->sc_txcnt = 0;
504 1.29 matt sc->sc_inq -= map->dm_nsegs;
505 1.29 matt KASSERT(zc->zc_xmit[lastack].ze_tdes1 & ZE_TDES1_LS);
506 1.29 matt ifp->if_opackets++;
507 1.29 matt bus_dmamap_unload(sc->sc_dmat, map);
508 1.29 matt KASSERT(sc->sc_txmbuf[lastack]);
509 1.38 joerg bpf_mtap(ifp, sc->sc_txmbuf[lastack]);
510 1.29 matt m_freem(sc->sc_txmbuf[lastack]);
511 1.29 matt sc->sc_txmbuf[lastack] = 0;
512 1.29 matt if (++lastack == TXDESCS)
513 1.29 matt lastack = 0;
514 1.1 ragge }
515 1.30 matt if (lastack != sc->sc_lastack) {
516 1.30 matt sc->sc_txdraincnt.ev_count++;
517 1.30 matt sc->sc_lastack = lastack;
518 1.30 matt if (sc->sc_inq == 0)
519 1.30 matt ifp->if_timer = 0;
520 1.30 matt ifp->if_flags &= ~IFF_OACTIVE;
521 1.30 matt zestart(ifp); /* Put in more in queue */
522 1.30 matt }
523 1.1 ragge }
524 1.1 ragge return 1;
525 1.1 ragge }
526 1.1 ragge
527 1.1 ragge /*
528 1.1 ragge * Process an ioctl request.
529 1.1 ragge */
530 1.1 ragge int
531 1.35 matt zeioctl(struct ifnet *ifp, u_long cmd, void *data)
532 1.1 ragge {
533 1.1 ragge struct ze_softc *sc = ifp->if_softc;
534 1.35 matt struct ifaddr *ifa = data;
535 1.1 ragge int s = splnet(), error = 0;
536 1.1 ragge
537 1.1 ragge switch (cmd) {
538 1.1 ragge
539 1.36 dyoung case SIOCINITIFADDR:
540 1.1 ragge ifp->if_flags |= IFF_UP;
541 1.1 ragge switch(ifa->ifa_addr->sa_family) {
542 1.1 ragge #ifdef INET
543 1.1 ragge case AF_INET:
544 1.1 ragge zeinit(sc);
545 1.1 ragge arp_ifinit(ifp, ifa);
546 1.1 ragge break;
547 1.1 ragge #endif
548 1.1 ragge }
549 1.1 ragge break;
550 1.1 ragge
551 1.1 ragge case SIOCSIFFLAGS:
552 1.36 dyoung if ((error = ifioctl_common(ifp, cmd, data)) != 0)
553 1.36 dyoung break;
554 1.36 dyoung /* XXX re-use ether_ioctl() */
555 1.36 dyoung switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
556 1.36 dyoung case IFF_RUNNING:
557 1.1 ragge /*
558 1.1 ragge * If interface is marked down and it is running,
559 1.1 ragge * stop it. (by disabling receive mechanism).
560 1.1 ragge */
561 1.1 ragge ZE_WCSR(ZE_CSR6, ZE_RCSR(ZE_CSR6) &
562 1.1 ragge ~(ZE_NICSR6_ST|ZE_NICSR6_SR));
563 1.1 ragge ifp->if_flags &= ~IFF_RUNNING;
564 1.36 dyoung break;
565 1.36 dyoung case IFF_UP:
566 1.1 ragge /*
567 1.1 ragge * If interface it marked up and it is stopped, then
568 1.1 ragge * start it.
569 1.1 ragge */
570 1.1 ragge zeinit(sc);
571 1.36 dyoung break;
572 1.36 dyoung case IFF_UP|IFF_RUNNING:
573 1.1 ragge /*
574 1.1 ragge * Send a new setup packet to match any new changes.
575 1.1 ragge * (Like IFF_PROMISC etc)
576 1.1 ragge */
577 1.1 ragge ze_setup(sc);
578 1.36 dyoung break;
579 1.36 dyoung case 0:
580 1.36 dyoung break;
581 1.1 ragge }
582 1.1 ragge break;
583 1.1 ragge
584 1.1 ragge case SIOCADDMULTI:
585 1.1 ragge case SIOCDELMULTI:
586 1.1 ragge /*
587 1.1 ragge * Update our multicast list.
588 1.1 ragge */
589 1.32 dyoung if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
590 1.1 ragge /*
591 1.1 ragge * Multicast list has changed; set the hardware filter
592 1.1 ragge * accordingly.
593 1.1 ragge */
594 1.23 thorpej if (ifp->if_flags & IFF_RUNNING)
595 1.23 thorpej ze_setup(sc);
596 1.1 ragge error = 0;
597 1.1 ragge }
598 1.1 ragge break;
599 1.1 ragge
600 1.1 ragge default:
601 1.36 dyoung error = ether_ioctl(ifp, cmd, data);
602 1.1 ragge
603 1.1 ragge }
604 1.1 ragge splx(s);
605 1.1 ragge return (error);
606 1.1 ragge }
607 1.1 ragge
608 1.1 ragge /*
609 1.1 ragge * Add a receive buffer to the indicated descriptor.
610 1.1 ragge */
611 1.1 ragge int
612 1.35 matt ze_add_rxbuf(struct ze_softc *sc, int i)
613 1.1 ragge {
614 1.1 ragge struct mbuf *m;
615 1.1 ragge struct ze_rdes *rp;
616 1.1 ragge int error;
617 1.1 ragge
618 1.1 ragge MGETHDR(m, M_DONTWAIT, MT_DATA);
619 1.1 ragge if (m == NULL)
620 1.1 ragge return (ENOBUFS);
621 1.1 ragge
622 1.22 matt MCLAIM(m, &sc->sc_ec.ec_rx_mowner);
623 1.1 ragge MCLGET(m, M_DONTWAIT);
624 1.1 ragge if ((m->m_flags & M_EXT) == 0) {
625 1.1 ragge m_freem(m);
626 1.1 ragge return (ENOBUFS);
627 1.1 ragge }
628 1.1 ragge
629 1.1 ragge if (sc->sc_rxmbuf[i] != NULL)
630 1.1 ragge bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
631 1.1 ragge
632 1.1 ragge error = bus_dmamap_load(sc->sc_dmat, sc->sc_rcvmap[i],
633 1.17 thorpej m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
634 1.17 thorpej BUS_DMA_READ|BUS_DMA_NOWAIT);
635 1.1 ragge if (error)
636 1.19 provos panic("%s: can't load rx DMA map %d, error = %d",
637 1.35 matt device_xname(sc->sc_dev), i, error);
638 1.1 ragge sc->sc_rxmbuf[i] = m;
639 1.1 ragge
640 1.1 ragge bus_dmamap_sync(sc->sc_dmat, sc->sc_rcvmap[i], 0,
641 1.1 ragge sc->sc_rcvmap[i]->dm_mapsize, BUS_DMASYNC_PREREAD);
642 1.1 ragge
643 1.1 ragge /*
644 1.1 ragge * We know that the mbuf cluster is page aligned. Also, be sure
645 1.1 ragge * that the IP header will be longword aligned.
646 1.1 ragge */
647 1.1 ragge m->m_data += 2;
648 1.1 ragge rp = &sc->sc_zedata->zc_recv[i];
649 1.1 ragge rp->ze_bufsize = (m->m_ext.ext_size - 2);
650 1.1 ragge rp->ze_bufaddr = (char *)sc->sc_rcvmap[i]->dm_segs[0].ds_addr + 2;
651 1.1 ragge rp->ze_framelen = ZE_FRAMELEN_OW;
652 1.1 ragge
653 1.1 ragge return (0);
654 1.1 ragge }
655 1.1 ragge
656 1.1 ragge /*
657 1.1 ragge * Create a setup packet and put in queue for sending.
658 1.1 ragge */
659 1.1 ragge void
660 1.35 matt ze_setup(struct ze_softc *sc)
661 1.1 ragge {
662 1.1 ragge struct ether_multi *enm;
663 1.1 ragge struct ether_multistep step;
664 1.1 ragge struct ze_cdata *zc = sc->sc_zedata;
665 1.1 ragge struct ifnet *ifp = &sc->sc_if;
666 1.31 dyoung const u_int8_t *enaddr = CLLADDR(ifp->if_sadl);
667 1.13 ragge int j, idx, reg;
668 1.1 ragge
669 1.1 ragge if (sc->sc_inq == (TXDESCS - 1)) {
670 1.1 ragge sc->sc_setup = 1;
671 1.1 ragge return;
672 1.1 ragge }
673 1.1 ragge sc->sc_setup = 0;
674 1.1 ragge /*
675 1.1 ragge * Init the setup packet with valid info.
676 1.1 ragge */
677 1.1 ragge memset(zc->zc_setup, 0xff, sizeof(zc->zc_setup)); /* Broadcast */
678 1.15 thorpej memcpy(zc->zc_setup, enaddr, ETHER_ADDR_LEN);
679 1.1 ragge
680 1.1 ragge /*
681 1.26 perry * Multicast handling. The SGEC can handle up to 16 direct
682 1.1 ragge * ethernet addresses.
683 1.1 ragge */
684 1.1 ragge j = 16;
685 1.1 ragge ifp->if_flags &= ~IFF_ALLMULTI;
686 1.1 ragge ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
687 1.1 ragge while (enm != NULL) {
688 1.14 thorpej if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6)) {
689 1.1 ragge ifp->if_flags |= IFF_ALLMULTI;
690 1.1 ragge break;
691 1.1 ragge }
692 1.15 thorpej memcpy(&zc->zc_setup[j], enm->enm_addrlo, ETHER_ADDR_LEN);
693 1.1 ragge j += 8;
694 1.1 ragge ETHER_NEXT_MULTI(step, enm);
695 1.1 ragge if ((enm != NULL)&& (j == 128)) {
696 1.1 ragge ifp->if_flags |= IFF_ALLMULTI;
697 1.1 ragge break;
698 1.1 ragge }
699 1.1 ragge }
700 1.7 thorpej
701 1.7 thorpej /*
702 1.7 thorpej * ALLMULTI implies PROMISC in this driver.
703 1.7 thorpej */
704 1.7 thorpej if (ifp->if_flags & IFF_ALLMULTI)
705 1.7 thorpej ifp->if_flags |= IFF_PROMISC;
706 1.7 thorpej else if (ifp->if_pcount == 0)
707 1.7 thorpej ifp->if_flags &= ~IFF_PROMISC;
708 1.1 ragge
709 1.1 ragge /*
710 1.1 ragge * Fiddle with the receive logic.
711 1.1 ragge */
712 1.1 ragge reg = ZE_RCSR(ZE_CSR6);
713 1.1 ragge DELAY(10);
714 1.1 ragge ZE_WCSR(ZE_CSR6, reg & ~ZE_NICSR6_SR); /* Stop rx */
715 1.1 ragge reg &= ~ZE_NICSR6_AF;
716 1.1 ragge if (ifp->if_flags & IFF_PROMISC)
717 1.1 ragge reg |= ZE_NICSR6_AF_PROM;
718 1.1 ragge else if (ifp->if_flags & IFF_ALLMULTI)
719 1.1 ragge reg |= ZE_NICSR6_AF_ALLM;
720 1.1 ragge DELAY(10);
721 1.1 ragge ZE_WCSR(ZE_CSR6, reg);
722 1.1 ragge /*
723 1.1 ragge * Only send a setup packet if needed.
724 1.1 ragge */
725 1.1 ragge if ((ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI)) == 0) {
726 1.1 ragge idx = sc->sc_nexttx;
727 1.1 ragge zc->zc_xmit[idx].ze_tdes1 = ZE_TDES1_DT_SETUP;
728 1.1 ragge zc->zc_xmit[idx].ze_bufsize = 128;
729 1.1 ragge zc->zc_xmit[idx].ze_bufaddr = sc->sc_pzedata->zc_setup;
730 1.1 ragge zc->zc_xmit[idx].ze_tdr = ZE_TDR_OW;
731 1.1 ragge
732 1.1 ragge if ((ZE_RCSR(ZE_CSR5) & ZE_NICSR5_TS) != ZE_NICSR5_TS_RUN)
733 1.1 ragge ZE_WCSR(ZE_CSR1, -1);
734 1.1 ragge
735 1.1 ragge sc->sc_inq++;
736 1.1 ragge if (++sc->sc_nexttx == TXDESCS)
737 1.1 ragge sc->sc_nexttx = 0;
738 1.1 ragge }
739 1.1 ragge }
740 1.1 ragge
741 1.1 ragge /*
742 1.1 ragge * Check for dead transmit logic.
743 1.1 ragge */
744 1.1 ragge void
745 1.35 matt zetimeout(struct ifnet *ifp)
746 1.1 ragge {
747 1.1 ragge struct ze_softc *sc = ifp->if_softc;
748 1.1 ragge
749 1.1 ragge if (sc->sc_inq == 0)
750 1.1 ragge return;
751 1.1 ragge
752 1.35 matt aprint_error_dev(sc->sc_dev, "xmit logic died, resetting...\n");
753 1.1 ragge /*
754 1.1 ragge * Do a reset of interface, to get it going again.
755 1.1 ragge * Will it work by just restart the transmit logic?
756 1.1 ragge */
757 1.1 ragge zeinit(sc);
758 1.1 ragge }
759 1.1 ragge
760 1.1 ragge /*
761 1.1 ragge * Reset chip:
762 1.1 ragge * Set/reset the reset flag.
763 1.1 ragge * Write interrupt vector.
764 1.1 ragge * Write ring buffer addresses.
765 1.1 ragge * Write SBR.
766 1.1 ragge */
767 1.35 matt bool
768 1.35 matt zereset(struct ze_softc *sc)
769 1.1 ragge {
770 1.13 ragge int reg, i;
771 1.1 ragge
772 1.1 ragge ZE_WCSR(ZE_CSR6, ZE_NICSR6_RE);
773 1.1 ragge DELAY(50000);
774 1.1 ragge if (ZE_RCSR(ZE_CSR6) & ZE_NICSR5_SF) {
775 1.35 matt aprint_error_dev(sc->sc_dev, "selftest failed\n");
776 1.35 matt return true;
777 1.1 ragge }
778 1.1 ragge
779 1.1 ragge /*
780 1.1 ragge * Get the vector that were set at match time, and remember it.
781 1.1 ragge * WHICH VECTOR TO USE? Take one unused. XXX
782 1.1 ragge * Funny way to set vector described in the programmers manual.
783 1.1 ragge */
784 1.1 ragge reg = ZE_NICSR0_IPL14 | sc->sc_intvec | 0x1fff0003; /* SYNC/ASYNC??? */
785 1.1 ragge i = 10;
786 1.1 ragge do {
787 1.1 ragge if (i-- == 0) {
788 1.35 matt aprint_error_dev(sc->sc_dev,
789 1.35 matt "failing SGEC CSR0 init\n");
790 1.35 matt return true;
791 1.1 ragge }
792 1.1 ragge ZE_WCSR(ZE_CSR0, reg);
793 1.1 ragge } while (ZE_RCSR(ZE_CSR0) != reg);
794 1.1 ragge
795 1.1 ragge ZE_WCSR(ZE_CSR3, (vaddr_t)sc->sc_pzedata->zc_recv);
796 1.1 ragge ZE_WCSR(ZE_CSR4, (vaddr_t)sc->sc_pzedata->zc_xmit);
797 1.35 matt return false;
798 1.1 ragge }
799