if_sq.c revision 1.19 1 /* $NetBSD: if_sq.c,v 1.19 2003/10/30 23:05:56 matt Exp $ */
2
3 /*
4 * Copyright (c) 2001 Rafal K. Boni
5 * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * Portions of this code are derived from software contributed to The
9 * NetBSD Foundation by Jason R. Thorpe of the Numerical Aerospace
10 * Simulation Facility, NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: if_sq.c,v 1.19 2003/10/30 23:05:56 matt Exp $");
37
38 #include "bpfilter.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 #include <sys/callout.h>
44 #include <sys/mbuf.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51
52 #include <uvm/uvm_extern.h>
53
54 #include <machine/endian.h>
55
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
59 #include <net/if_ether.h>
60
61 #if NBPFILTER > 0
62 #include <net/bpf.h>
63 #endif
64
65 #include <machine/bus.h>
66 #include <machine/intr.h>
67
68 #include <dev/ic/seeq8003reg.h>
69
70 #include <sgimips/hpc/sqvar.h>
71 #include <sgimips/hpc/hpcvar.h>
72 #include <sgimips/hpc/hpcreg.h>
73
74 #include <dev/arcbios/arcbios.h>
75 #include <dev/arcbios/arcbiosvar.h>
76
77 #define static
78
79 /*
80 * Short TODO list:
81 * (1) Do counters for bad-RX packets.
82 * (2) Allow multi-segment transmits, instead of copying to a single,
83 * contiguous mbuf.
84 * (3) Verify sq_stop() turns off enough stuff; I was still getting
85 * seeq interrupts after sq_stop().
86 * (4) Fix up printfs in driver (most should only fire ifdef SQ_DEBUG
87 * or something similar.
88 * (5) Implement EDLC modes: especially packet auto-pad and simplex
89 * mode.
90 * (6) Should the driver filter out its own transmissions in non-EDLC
91 * mode?
92 * (7) Multicast support -- multicast filter, address management, ...
93 * (8) Deal with RB0 (recv buffer overflow) on reception. Will need
94 * to figure out if RB0 is read-only as stated in one spot in the
95 * HPC spec or read-write (ie, is the 'write a one to clear it')
96 * the correct thing?
97 */
98
99 static int sq_match(struct device *, struct cfdata *, void *);
100 static void sq_attach(struct device *, struct device *, void *);
101 static int sq_init(struct ifnet *);
102 static void sq_start(struct ifnet *);
103 static void sq_stop(struct ifnet *, int);
104 static void sq_watchdog(struct ifnet *);
105 static int sq_ioctl(struct ifnet *, u_long, caddr_t);
106
107 static void sq_set_filter(struct sq_softc *);
108 static int sq_intr(void *);
109 static int sq_rxintr(struct sq_softc *);
110 static int sq_txintr(struct sq_softc *);
111 static void sq_reset(struct sq_softc *);
112 static int sq_add_rxbuf(struct sq_softc *, int);
113 static void sq_dump_buffer(u_int32_t addr, u_int32_t len);
114
115 static void enaddr_aton(const char*, u_int8_t*);
116
117 /* Actions */
118 #define SQ_RESET 1
119 #define SQ_ADD_TO_DMA 2
120 #define SQ_START_DMA 3
121 #define SQ_DONE_DMA 4
122 #define SQ_RESTART_DMA 5
123 #define SQ_TXINTR_ENTER 6
124 #define SQ_TXINTR_EXIT 7
125 #define SQ_TXINTR_BUSY 8
126
127 struct sq_action_trace {
128 int action;
129 int bufno;
130 int status;
131 int freebuf;
132 };
133
134 #define SQ_TRACEBUF_SIZE 100
135 int sq_trace_idx = 0;
136 struct sq_action_trace sq_trace[SQ_TRACEBUF_SIZE];
137
138 void sq_trace_dump(struct sq_softc* sc);
139
140 #define SQ_TRACE(act, buf, stat, free) do { \
141 sq_trace[sq_trace_idx].action = (act); \
142 sq_trace[sq_trace_idx].bufno = (buf); \
143 sq_trace[sq_trace_idx].status = (stat); \
144 sq_trace[sq_trace_idx].freebuf = (free); \
145 if (++sq_trace_idx == SQ_TRACEBUF_SIZE) { \
146 memset(&sq_trace, 0, sizeof(sq_trace)); \
147 sq_trace_idx = 0; \
148 } \
149 } while (0)
150
151 CFATTACH_DECL(sq, sizeof(struct sq_softc),
152 sq_match, sq_attach, NULL, NULL);
153
154 #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
155
156 static int
157 sq_match(struct device *parent, struct cfdata *cf, void *aux)
158 {
159 struct hpc_attach_args *ha = aux;
160
161 if (strcmp(ha->ha_name, cf->cf_name) == 0)
162 return (1);
163
164 return (0);
165 }
166
167 static void
168 sq_attach(struct device *parent, struct device *self, void *aux)
169 {
170 int i, err;
171 char* macaddr;
172 struct sq_softc *sc = (void *)self;
173 struct hpc_attach_args *haa = aux;
174 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
175
176 sc->sc_hpct = haa->ha_st;
177 if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
178 haa->ha_dmaoff,
179 HPC_ENET_REGS_SIZE,
180 &sc->sc_hpch)) != 0) {
181 printf(": unable to map HPC DMA registers, error = %d\n", err);
182 goto fail_0;
183 }
184
185 sc->sc_regt = haa->ha_st;
186 if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
187 haa->ha_devoff,
188 HPC_ENET_DEVREGS_SIZE,
189 &sc->sc_regh)) != 0) {
190 printf(": unable to map Seeq registers, error = %d\n", err);
191 goto fail_0;
192 }
193
194 sc->sc_dmat = haa->ha_dmat;
195
196 if ((err = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct sq_control),
197 PAGE_SIZE, PAGE_SIZE, &sc->sc_cdseg,
198 1, &sc->sc_ncdseg, BUS_DMA_NOWAIT)) != 0) {
199 printf(": unable to allocate control data, error = %d\n", err);
200 goto fail_0;
201 }
202
203 if ((err = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_ncdseg,
204 sizeof(struct sq_control),
205 (caddr_t *)&sc->sc_control,
206 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
207 printf(": unable to map control data, error = %d\n", err);
208 goto fail_1;
209 }
210
211 if ((err = bus_dmamap_create(sc->sc_dmat, sizeof(struct sq_control),
212 1, sizeof(struct sq_control), PAGE_SIZE,
213 BUS_DMA_NOWAIT, &sc->sc_cdmap)) != 0) {
214 printf(": unable to create DMA map for control data, error "
215 "= %d\n", err);
216 goto fail_2;
217 }
218
219 if ((err = bus_dmamap_load(sc->sc_dmat, sc->sc_cdmap, sc->sc_control,
220 sizeof(struct sq_control),
221 NULL, BUS_DMA_NOWAIT)) != 0) {
222 printf(": unable to load DMA map for control data, error "
223 "= %d\n", err);
224 goto fail_3;
225 }
226
227 memset(sc->sc_control, 0, sizeof(struct sq_control));
228
229 /* Create transmit buffer DMA maps */
230 for (i = 0; i < SQ_NTXDESC; i++) {
231 if ((err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
232 0, BUS_DMA_NOWAIT,
233 &sc->sc_txmap[i])) != 0) {
234 printf(": unable to create tx DMA map %d, error = %d\n",
235 i, err);
236 goto fail_4;
237 }
238 }
239
240 /* Create transmit buffer DMA maps */
241 for (i = 0; i < SQ_NRXDESC; i++) {
242 if ((err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
243 0, BUS_DMA_NOWAIT,
244 &sc->sc_rxmap[i])) != 0) {
245 printf(": unable to create rx DMA map %d, error = %d\n",
246 i, err);
247 goto fail_5;
248 }
249 }
250
251 /* Pre-allocate the receive buffers. */
252 for (i = 0; i < SQ_NRXDESC; i++) {
253 if ((err = sq_add_rxbuf(sc, i)) != 0) {
254 printf(": unable to allocate or map rx buffer %d\n,"
255 " error = %d\n", i, err);
256 goto fail_6;
257 }
258 }
259
260 if ((macaddr = ARCBIOS->GetEnvironmentVariable("eaddr")) == NULL) {
261 printf(": unable to get MAC address!\n");
262 goto fail_6;
263 }
264
265 evcnt_attach_dynamic(&sc->sq_intrcnt, EVCNT_TYPE_INTR, NULL,
266 self->dv_xname, "intr");
267
268 if ((cpu_intr_establish(haa->ha_irq, IPL_NET, sq_intr, sc)) == NULL) {
269 printf(": unable to establish interrupt!\n");
270 goto fail_6;
271 }
272
273 /* Reset the chip to a known state. */
274 sq_reset(sc);
275
276 /*
277 * Determine if we're an 8003 or 80c03 by setting the first
278 * MAC address register to non-zero, and then reading it back.
279 * If it's zero, we have an 80c03, because we will have read
280 * the TxCollLSB register.
281 */
282 bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCOLLS0, 0xa5);
283 if (bus_space_read_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCOLLS0) == 0)
284 sc->sc_type = SQ_TYPE_80C03;
285 else
286 sc->sc_type = SQ_TYPE_8003;
287 bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCOLLS0, 0x00);
288
289 printf(": SGI Seeq %s\n",
290 sc->sc_type == SQ_TYPE_80C03 ? "80c03" : "8003");
291
292 enaddr_aton(macaddr, sc->sc_enaddr);
293
294 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
295 ether_sprintf(sc->sc_enaddr));
296
297 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
298 ifp->if_softc = sc;
299 ifp->if_mtu = ETHERMTU;
300 ifp->if_init = sq_init;
301 ifp->if_stop = sq_stop;
302 ifp->if_start = sq_start;
303 ifp->if_ioctl = sq_ioctl;
304 ifp->if_watchdog = sq_watchdog;
305 ifp->if_flags = IFF_BROADCAST | IFF_NOTRAILERS | IFF_MULTICAST;
306 IFQ_SET_READY(&ifp->if_snd);
307
308 if_attach(ifp);
309 ether_ifattach(ifp, sc->sc_enaddr);
310
311 memset(&sq_trace, 0, sizeof(sq_trace));
312 /* Done! */
313 return;
314
315 /*
316 * Free any resources we've allocated during the failed attach
317 * attempt. Do this in reverse order and fall through.
318 */
319 fail_6:
320 for (i = 0; i < SQ_NRXDESC; i++) {
321 if (sc->sc_rxmbuf[i] != NULL) {
322 bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmap[i]);
323 m_freem(sc->sc_rxmbuf[i]);
324 }
325 }
326 fail_5:
327 for (i = 0; i < SQ_NRXDESC; i++) {
328 if (sc->sc_rxmap[i] != NULL)
329 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxmap[i]);
330 }
331 fail_4:
332 for (i = 0; i < SQ_NTXDESC; i++) {
333 if (sc->sc_txmap[i] != NULL)
334 bus_dmamap_destroy(sc->sc_dmat, sc->sc_txmap[i]);
335 }
336 bus_dmamap_unload(sc->sc_dmat, sc->sc_cdmap);
337 fail_3:
338 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cdmap);
339 fail_2:
340 bus_dmamem_unmap(sc->sc_dmat, (caddr_t) sc->sc_control,
341 sizeof(struct sq_control));
342 fail_1:
343 bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_ncdseg);
344 fail_0:
345 return;
346 }
347
348 /* Set up data to get the interface up and running. */
349 int
350 sq_init(struct ifnet *ifp)
351 {
352 int i;
353 u_int32_t reg;
354 struct sq_softc *sc = ifp->if_softc;
355
356 /* Cancel any in-progress I/O */
357 sq_stop(ifp, 0);
358
359 sc->sc_nextrx = 0;
360
361 sc->sc_nfreetx = SQ_NTXDESC;
362 sc->sc_nexttx = sc->sc_prevtx = 0;
363
364 SQ_TRACE(SQ_RESET, 0, 0, sc->sc_nfreetx);
365
366 /* Set into 8003 mode, bank 0 to program ethernet address */
367 bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCMD, TXCMD_BANK0);
368
369 /* Now write the address */
370 for (i = 0; i < ETHER_ADDR_LEN; i++)
371 bus_space_write_1(sc->sc_regt, sc->sc_regh, i,
372 sc->sc_enaddr[i]);
373
374 sc->sc_rxcmd = RXCMD_IE_CRC |
375 RXCMD_IE_DRIB |
376 RXCMD_IE_SHORT |
377 RXCMD_IE_END |
378 RXCMD_IE_GOOD;
379
380 /*
381 * Set the receive filter -- this will add some bits to the
382 * prototype RXCMD register. Do this before setting the
383 * transmit config register, since we might need to switch
384 * banks.
385 */
386 sq_set_filter(sc);
387
388 /* Set up Seeq transmit command register */
389 bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCMD,
390 TXCMD_IE_UFLOW |
391 TXCMD_IE_COLL |
392 TXCMD_IE_16COLL |
393 TXCMD_IE_GOOD);
394
395 /* Now write the receive command register. */
396 bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_RXCMD, sc->sc_rxcmd);
397
398 /* Set up HPC ethernet DMA config */
399 reg = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_DMACFG);
400 bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_DMACFG,
401 reg | ENETR_DMACFG_FIX_RXDC |
402 ENETR_DMACFG_FIX_INTR |
403 ENETR_DMACFG_FIX_EOP);
404
405 /* Pass the start of the receive ring to the HPC */
406 bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_NDBP,
407 SQ_CDRXADDR(sc, 0));
408
409 /* And turn on the HPC ethernet receive channel */
410 bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_CTL,
411 ENETR_CTL_ACTIVE);
412
413 ifp->if_flags |= IFF_RUNNING;
414 ifp->if_flags &= ~IFF_OACTIVE;
415
416 return 0;
417 }
418
419 static void
420 sq_set_filter(struct sq_softc *sc)
421 {
422 struct ethercom *ec = &sc->sc_ethercom;
423 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
424 struct ether_multi *enm;
425 struct ether_multistep step;
426
427 /*
428 * Check for promiscuous mode. Also implies
429 * all-multicast.
430 */
431 if (ifp->if_flags & IFF_PROMISC) {
432 sc->sc_rxcmd |= RXCMD_REC_ALL;
433 ifp->if_flags |= IFF_ALLMULTI;
434 return;
435 }
436
437 /*
438 * The 8003 has no hash table. If we have any multicast
439 * addresses on the list, enable reception of all multicast
440 * frames.
441 *
442 * XXX The 80c03 has a hash table. We should use it.
443 */
444
445 ETHER_FIRST_MULTI(step, ec, enm);
446
447 if (enm == NULL) {
448 sc->sc_rxcmd &= ~RXCMD_REC_MASK;
449 sc->sc_rxcmd |= RXCMD_REC_BROAD;
450
451 ifp->if_flags &= ~IFF_ALLMULTI;
452 return;
453 }
454
455 sc->sc_rxcmd |= RXCMD_REC_MULTI;
456 ifp->if_flags |= IFF_ALLMULTI;
457 }
458
459 int
460 sq_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
461 {
462 int s, error = 0;
463
464 s = splnet();
465
466 error = ether_ioctl(ifp, cmd, data);
467 if (error == ENETRESET) {
468 /*
469 * Multicast list has changed; set the hardware filter
470 * accordingly.
471 */
472 error = sq_init(ifp);
473 }
474
475 splx(s);
476 return (error);
477 }
478
479 void
480 sq_start(struct ifnet *ifp)
481 {
482 struct sq_softc *sc = ifp->if_softc;
483 u_int32_t status;
484 struct mbuf *m0, *m;
485 bus_dmamap_t dmamap;
486 int err, totlen, nexttx, firsttx, lasttx = -1, ofree, seg;
487
488 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
489 return;
490
491 /*
492 * Remember the previous number of free descriptors and
493 * the first descriptor we'll use.
494 */
495 ofree = sc->sc_nfreetx;
496 firsttx = sc->sc_nexttx;
497
498 /*
499 * Loop through the send queue, setting up transmit descriptors
500 * until we drain the queue, or use up all available transmit
501 * descriptors.
502 */
503 while (sc->sc_nfreetx != 0) {
504 /*
505 * Grab a packet off the queue.
506 */
507 IFQ_POLL(&ifp->if_snd, m0);
508 if (m0 == NULL)
509 break;
510 m = NULL;
511
512 dmamap = sc->sc_txmap[sc->sc_nexttx];
513
514 /*
515 * Load the DMA map. If this fails, the packet either
516 * didn't fit in the alloted number of segments, or we were
517 * short on resources. In this case, we'll copy and try
518 * again.
519 * Also copy it if we need to pad, so that we are sure there
520 * is room for the pad buffer.
521 * XXX the right way of doing this is to use a static buffer
522 * for padding and adding it to the transmit descriptor (see
523 * sys/dev/pci/if_tl.c for example). We can't do this here yet
524 * because we can't send packets with more than one fragment.
525 */
526 if (m0->m_pkthdr.len < ETHER_PAD_LEN ||
527 bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
528 BUS_DMA_NOWAIT) != 0) {
529 MGETHDR(m, M_DONTWAIT, MT_DATA);
530 if (m == NULL) {
531 printf("%s: unable to allocate Tx mbuf\n",
532 sc->sc_dev.dv_xname);
533 break;
534 }
535 if (m0->m_pkthdr.len > MHLEN) {
536 MCLGET(m, M_DONTWAIT);
537 if ((m->m_flags & M_EXT) == 0) {
538 printf("%s: unable to allocate Tx "
539 "cluster\n", sc->sc_dev.dv_xname);
540 m_freem(m);
541 break;
542 }
543 }
544
545 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
546 if (m0->m_pkthdr.len < ETHER_PAD_LEN) {
547 memset(mtod(m, char *) + m0->m_pkthdr.len, 0,
548 ETHER_PAD_LEN - m0->m_pkthdr.len);
549 m->m_pkthdr.len = m->m_len = ETHER_PAD_LEN;
550 } else
551 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
552
553 if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
554 m, BUS_DMA_NOWAIT)) != 0) {
555 printf("%s: unable to load Tx buffer, "
556 "error = %d\n", sc->sc_dev.dv_xname, err);
557 break;
558 }
559 }
560
561 /*
562 * Ensure we have enough descriptors free to describe
563 * the packet.
564 */
565 if (dmamap->dm_nsegs > sc->sc_nfreetx) {
566 /*
567 * Not enough free descriptors to transmit this
568 * packet. We haven't committed to anything yet,
569 * so just unload the DMA map, put the packet
570 * back on the queue, and punt. Notify the upper
571 * layer that there are no more slots left.
572 *
573 * XXX We could allocate an mbuf and copy, but
574 * XXX it is worth it?
575 */
576 ifp->if_flags |= IFF_OACTIVE;
577 bus_dmamap_unload(sc->sc_dmat, dmamap);
578 if (m != NULL)
579 m_freem(m);
580 break;
581 }
582
583 IFQ_DEQUEUE(&ifp->if_snd, m0);
584 #if NBPFILTER > 0
585 /*
586 * Pass the packet to any BPF listeners.
587 */
588 if (ifp->if_bpf)
589 bpf_mtap(ifp->if_bpf, m0);
590 #endif /* NBPFILTER > 0 */
591 if (m != NULL) {
592 m_freem(m0);
593 m0 = m;
594 }
595
596 /*
597 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
598 */
599
600 /* Sync the DMA map. */
601 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
602 BUS_DMASYNC_PREWRITE);
603
604 /*
605 * Initialize the transmit descriptors.
606 */
607 for (nexttx = sc->sc_nexttx, seg = 0, totlen = 0;
608 seg < dmamap->dm_nsegs;
609 seg++, nexttx = SQ_NEXTTX(nexttx)) {
610 sc->sc_txdesc[nexttx].hdd_bufptr =
611 dmamap->dm_segs[seg].ds_addr;
612 sc->sc_txdesc[nexttx].hdd_ctl =
613 dmamap->dm_segs[seg].ds_len;
614 sc->sc_txdesc[nexttx].hdd_descptr=
615 SQ_CDTXADDR(sc, SQ_NEXTTX(nexttx));
616 lasttx = nexttx;
617 totlen += dmamap->dm_segs[seg].ds_len;
618 }
619
620 /* Last descriptor gets end-of-packet */
621 KASSERT(lasttx != -1);
622 sc->sc_txdesc[lasttx].hdd_ctl |= HDD_CTL_EOPACKET;
623
624 #if 0
625 printf("%s: transmit %d-%d, len %d\n", sc->sc_dev.dv_xname,
626 sc->sc_nexttx, lasttx,
627 totlen);
628 #endif
629
630 if (ifp->if_flags & IFF_DEBUG) {
631 printf(" transmit chain:\n");
632 for (seg = sc->sc_nexttx;; seg = SQ_NEXTTX(seg)) {
633 printf(" descriptor %d:\n", seg);
634 printf(" hdd_bufptr: 0x%08x\n",
635 sc->sc_txdesc[seg].hdd_bufptr);
636 printf(" hdd_ctl: 0x%08x\n",
637 sc->sc_txdesc[seg].hdd_ctl);
638 printf(" hdd_descptr: 0x%08x\n",
639 sc->sc_txdesc[seg].hdd_descptr);
640
641 if (seg == lasttx)
642 break;
643 }
644 }
645
646 /* Sync the descriptors we're using. */
647 SQ_CDTXSYNC(sc, sc->sc_nexttx, dmamap->dm_nsegs,
648 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
649
650 /* Store a pointer to the packet so we can free it later */
651 sc->sc_txmbuf[sc->sc_nexttx] = m0;
652
653 /* Advance the tx pointer. */
654 sc->sc_nfreetx -= dmamap->dm_nsegs;
655 sc->sc_nexttx = nexttx;
656
657 }
658
659 /* All transmit descriptors used up, let upper layers know */
660 if (sc->sc_nfreetx == 0)
661 ifp->if_flags |= IFF_OACTIVE;
662
663 if (sc->sc_nfreetx != ofree) {
664 #if 0
665 printf("%s: %d packets enqueued, first %d, INTR on %d\n",
666 sc->sc_dev.dv_xname, lasttx - firsttx + 1,
667 firsttx, lasttx);
668 #endif
669
670 /*
671 * Cause a transmit interrupt to happen on the
672 * last packet we enqueued, mark it as the last
673 * descriptor.
674 */
675 KASSERT(lasttx != -1);
676 sc->sc_txdesc[lasttx].hdd_ctl |= (HDD_CTL_INTR |
677 HDD_CTL_EOCHAIN);
678 SQ_CDTXSYNC(sc, lasttx, 1,
679 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
680
681 /*
682 * There is a potential race condition here if the HPC
683 * DMA channel is active and we try and either update
684 * the 'next descriptor' pointer in the HPC PIO space
685 * or the 'next descriptor' pointer in a previous desc-
686 * riptor.
687 *
688 * To avoid this, if the channel is active, we rely on
689 * the transmit interrupt routine noticing that there
690 * are more packets to send and restarting the HPC DMA
691 * engine, rather than mucking with the DMA state here.
692 */
693 status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
694 HPC_ENETX_CTL);
695
696 if ((status & ENETX_CTL_ACTIVE) != 0) {
697 SQ_TRACE(SQ_ADD_TO_DMA, firsttx, status,
698 sc->sc_nfreetx);
699 sc->sc_txdesc[SQ_PREVTX(firsttx)].hdd_ctl &=
700 ~HDD_CTL_EOCHAIN;
701 SQ_CDTXSYNC(sc, SQ_PREVTX(firsttx), 1,
702 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
703 } else {
704 SQ_TRACE(SQ_START_DMA, firsttx, status, sc->sc_nfreetx);
705
706 bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
707 HPC_ENETX_NDBP, SQ_CDTXADDR(sc, firsttx));
708
709 /* Kick DMA channel into life */
710 bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
711 HPC_ENETX_CTL, ENETX_CTL_ACTIVE);
712 }
713
714 /* Set a watchdog timer in case the chip flakes out. */
715 ifp->if_timer = 5;
716 }
717 }
718
719 void
720 sq_stop(struct ifnet *ifp, int disable)
721 {
722 int i;
723 struct sq_softc *sc = ifp->if_softc;
724
725 for (i =0; i < SQ_NTXDESC; i++) {
726 if (sc->sc_txmbuf[i] != NULL) {
727 bus_dmamap_unload(sc->sc_dmat, sc->sc_txmap[i]);
728 m_freem(sc->sc_txmbuf[i]);
729 sc->sc_txmbuf[i] = NULL;
730 }
731 }
732
733 /* Clear Seeq transmit/receive command registers */
734 bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_TXCMD, 0);
735 bus_space_write_1(sc->sc_regt, sc->sc_regh, SEEQ_RXCMD, 0);
736
737 sq_reset(sc);
738
739 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
740 ifp->if_timer = 0;
741 }
742
743 /* Device timeout/watchdog routine. */
744 void
745 sq_watchdog(struct ifnet *ifp)
746 {
747 u_int32_t status;
748 struct sq_softc *sc = ifp->if_softc;
749
750 status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETX_CTL);
751 log(LOG_ERR, "%s: device timeout (prev %d, next %d, free %d, "
752 "status %08x)\n", sc->sc_dev.dv_xname, sc->sc_prevtx,
753 sc->sc_nexttx, sc->sc_nfreetx, status);
754
755 sq_trace_dump(sc);
756
757 memset(&sq_trace, 0, sizeof(sq_trace));
758 sq_trace_idx = 0;
759
760 ++ifp->if_oerrors;
761
762 sq_init(ifp);
763 }
764
765 void sq_trace_dump(struct sq_softc* sc)
766 {
767 int i;
768
769 for(i = 0; i < sq_trace_idx; i++) {
770 printf("%s: [%d] action %d, buf %d, free %d, status %08x\n",
771 sc->sc_dev.dv_xname, i, sq_trace[i].action,
772 sq_trace[i].bufno, sq_trace[i].freebuf,
773 sq_trace[i].status);
774 }
775 }
776
777 static int
778 sq_intr(void * arg)
779 {
780 struct sq_softc *sc = arg;
781 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
782 int handled = 0;
783 u_int32_t stat;
784
785 stat = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_RESET);
786
787 if ((stat & 2) == 0) {
788 printf("%s: Unexpected interrupt!\n", sc->sc_dev.dv_xname);
789 return 0;
790 }
791
792 bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_RESET, 2);
793
794 /*
795 * If the interface isn't running, the interrupt couldn't
796 * possibly have come from us.
797 */
798 if ((ifp->if_flags & IFF_RUNNING) == 0)
799 return 0;
800
801 sc->sq_intrcnt.ev_count++;
802
803 /* Always check for received packets */
804 if (sq_rxintr(sc) != 0)
805 handled++;
806
807 /* Only handle transmit interrupts if we actually sent something */
808 if (sc->sc_nfreetx < SQ_NTXDESC) {
809 sq_txintr(sc);
810 handled++;
811 }
812
813 #if NRND > 0
814 if (handled)
815 rnd_add_uint32(&sc->rnd_source, stat);
816 #endif
817 return (handled);
818 }
819
820 static int
821 sq_rxintr(struct sq_softc *sc)
822 {
823 int count = 0;
824 struct mbuf* m;
825 int i, framelen;
826 u_int8_t pktstat;
827 u_int32_t status;
828 int new_end, orig_end;
829 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
830
831 for(i = sc->sc_nextrx;; i = SQ_NEXTRX(i)) {
832 SQ_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
833
834 /* If this is a CPU-owned buffer, we're at the end of the list */
835 if (sc->sc_rxdesc[i].hdd_ctl & HDD_CTL_OWN) {
836 #if 0
837 u_int32_t reg;
838
839 reg = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
840 HPC_ENETR_CTL);
841 printf("%s: rxintr: done at %d (ctl %08x)\n",
842 sc->sc_dev.dv_xname, i, reg);
843 #endif
844 break;
845 }
846
847 count++;
848
849 m = sc->sc_rxmbuf[i];
850 framelen = m->m_ext.ext_size -
851 HDD_CTL_BYTECNT(sc->sc_rxdesc[i].hdd_ctl) - 3;
852
853 /* Now sync the actual packet data */
854 bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[i], 0,
855 sc->sc_rxmap[i]->dm_mapsize, BUS_DMASYNC_POSTREAD);
856
857 pktstat = *((u_int8_t*)m->m_data + framelen + 2);
858
859 if ((pktstat & RXSTAT_GOOD) == 0) {
860 ifp->if_ierrors++;
861
862 if (pktstat & RXSTAT_OFLOW)
863 printf("%s: receive FIFO overflow\n",
864 sc->sc_dev.dv_xname);
865
866 bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[i], 0,
867 sc->sc_rxmap[i]->dm_mapsize,
868 BUS_DMASYNC_PREREAD);
869 SQ_INIT_RXDESC(sc, i);
870 continue;
871 }
872
873 if (sq_add_rxbuf(sc, i) != 0) {
874 ifp->if_ierrors++;
875 bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[i], 0,
876 sc->sc_rxmap[i]->dm_mapsize,
877 BUS_DMASYNC_PREREAD);
878 SQ_INIT_RXDESC(sc, i);
879 continue;
880 }
881
882
883 m->m_data += 2;
884 m->m_pkthdr.rcvif = ifp;
885 m->m_pkthdr.len = m->m_len = framelen;
886
887 ifp->if_ipackets++;
888
889 #if 0
890 printf("%s: sq_rxintr: buf %d len %d\n", sc->sc_dev.dv_xname,
891 i, framelen);
892 #endif
893
894 #if NBPFILTER > 0
895 if (ifp->if_bpf)
896 bpf_mtap(ifp->if_bpf, m);
897 #endif
898 (*ifp->if_input)(ifp, m);
899 }
900
901
902 /* If anything happened, move ring start/end pointers to new spot */
903 if (i != sc->sc_nextrx) {
904 new_end = SQ_PREVRX(i);
905 sc->sc_rxdesc[new_end].hdd_ctl |= HDD_CTL_EOCHAIN;
906 SQ_CDRXSYNC(sc, new_end, BUS_DMASYNC_PREREAD |
907 BUS_DMASYNC_PREWRITE);
908
909 orig_end = SQ_PREVRX(sc->sc_nextrx);
910 sc->sc_rxdesc[orig_end].hdd_ctl &= ~HDD_CTL_EOCHAIN;
911 SQ_CDRXSYNC(sc, orig_end, BUS_DMASYNC_PREREAD |
912 BUS_DMASYNC_PREWRITE);
913
914 sc->sc_nextrx = i;
915 }
916
917 status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_CTL);
918
919 /* If receive channel is stopped, restart it... */
920 if ((status & ENETR_CTL_ACTIVE) == 0) {
921 /* Pass the start of the receive ring to the HPC */
922 bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
923 HPC_ENETR_NDBP, SQ_CDRXADDR(sc, sc->sc_nextrx));
924
925 /* And turn on the HPC ethernet receive channel */
926 bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_CTL,
927 ENETR_CTL_ACTIVE);
928 }
929
930 return count;
931 }
932
933 static int
934 sq_txintr(struct sq_softc *sc)
935 {
936 int i;
937 u_int32_t status;
938 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
939
940 status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETX_CTL);
941
942 SQ_TRACE(SQ_TXINTR_ENTER, sc->sc_prevtx, status, sc->sc_nfreetx);
943
944 if ((status & (ENETX_CTL_ACTIVE | TXSTAT_GOOD)) == 0) {
945 if (status & TXSTAT_COLL)
946 ifp->if_collisions++;
947
948 if (status & TXSTAT_UFLOW) {
949 printf("%s: transmit underflow\n", sc->sc_dev.dv_xname);
950 ifp->if_oerrors++;
951 }
952
953 if (status & TXSTAT_16COLL) {
954 printf("%s: max collisions reached\n", sc->sc_dev.dv_xname);
955 ifp->if_oerrors++;
956 ifp->if_collisions += 16;
957 }
958 }
959
960 i = sc->sc_prevtx;
961 while (sc->sc_nfreetx < SQ_NTXDESC) {
962 /*
963 * Check status first so we don't end up with a case of
964 * the buffer not being finished while the DMA channel
965 * has gone idle.
966 */
967 status = bus_space_read_4(sc->sc_hpct, sc->sc_hpch,
968 HPC_ENETX_CTL);
969
970 SQ_CDTXSYNC(sc, i, sc->sc_txmap[i]->dm_nsegs,
971 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
972
973 /* If not yet transmitted, try and start DMA engine again */
974 if ((sc->sc_txdesc[i].hdd_ctl & HDD_CTL_XMITDONE) == 0) {
975 if ((status & ENETX_CTL_ACTIVE) == 0) {
976 SQ_TRACE(SQ_RESTART_DMA, i, status,
977 sc->sc_nfreetx);
978
979 bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
980 HPC_ENETX_NDBP, SQ_CDTXADDR(sc, i));
981
982 /* Kick DMA channel into life */
983 bus_space_write_4(sc->sc_hpct, sc->sc_hpch,
984 HPC_ENETX_CTL, ENETX_CTL_ACTIVE);
985
986 /*
987 * Set a watchdog timer in case the chip
988 * flakes out.
989 */
990 ifp->if_timer = 5;
991 } else {
992 SQ_TRACE(SQ_TXINTR_BUSY, i, status,
993 sc->sc_nfreetx);
994 }
995 break;
996 }
997
998 /* Sync the packet data, unload DMA map, free mbuf */
999 bus_dmamap_sync(sc->sc_dmat, sc->sc_txmap[i], 0,
1000 sc->sc_txmap[i]->dm_mapsize,
1001 BUS_DMASYNC_POSTWRITE);
1002 bus_dmamap_unload(sc->sc_dmat, sc->sc_txmap[i]);
1003 m_freem(sc->sc_txmbuf[i]);
1004 sc->sc_txmbuf[i] = NULL;
1005
1006 ifp->if_opackets++;
1007 sc->sc_nfreetx++;
1008
1009 SQ_TRACE(SQ_DONE_DMA, i, status, sc->sc_nfreetx);
1010 i = SQ_NEXTTX(i);
1011 }
1012
1013 /* prevtx now points to next xmit packet not yet finished */
1014 sc->sc_prevtx = i;
1015
1016 /* If we have buffers free, let upper layers know */
1017 if (sc->sc_nfreetx > 0)
1018 ifp->if_flags &= ~IFF_OACTIVE;
1019
1020 /* If all packets have left the coop, cancel watchdog */
1021 if (sc->sc_nfreetx == SQ_NTXDESC)
1022 ifp->if_timer = 0;
1023
1024 SQ_TRACE(SQ_TXINTR_EXIT, sc->sc_prevtx, status, sc->sc_nfreetx);
1025 sq_start(ifp);
1026
1027 return 1;
1028 }
1029
1030
1031 void
1032 sq_reset(struct sq_softc *sc)
1033 {
1034 /* Stop HPC dma channels */
1035 bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_CTL, 0);
1036 bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETX_CTL, 0);
1037
1038 bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_RESET, 3);
1039 delay(20);
1040 bus_space_write_4(sc->sc_hpct, sc->sc_hpch, HPC_ENETR_RESET, 0);
1041 }
1042
1043 /* sq_add_rxbuf: Add a receive buffer to the indicated descriptor. */
1044 int
1045 sq_add_rxbuf(struct sq_softc *sc, int idx)
1046 {
1047 int err;
1048 struct mbuf *m;
1049
1050 MGETHDR(m, M_DONTWAIT, MT_DATA);
1051 if (m == NULL)
1052 return (ENOBUFS);
1053
1054 MCLGET(m, M_DONTWAIT);
1055 if ((m->m_flags & M_EXT) == 0) {
1056 m_freem(m);
1057 return (ENOBUFS);
1058 }
1059
1060 if (sc->sc_rxmbuf[idx] != NULL)
1061 bus_dmamap_unload(sc->sc_dmat, sc->sc_rxmap[idx]);
1062
1063 sc->sc_rxmbuf[idx] = m;
1064
1065 if ((err = bus_dmamap_load(sc->sc_dmat, sc->sc_rxmap[idx],
1066 m->m_ext.ext_buf, m->m_ext.ext_size,
1067 NULL, BUS_DMA_NOWAIT)) != 0) {
1068 printf("%s: can't load rx DMA map %d, error = %d\n",
1069 sc->sc_dev.dv_xname, idx, err);
1070 panic("sq_add_rxbuf"); /* XXX */
1071 }
1072
1073 bus_dmamap_sync(sc->sc_dmat, sc->sc_rxmap[idx], 0,
1074 sc->sc_rxmap[idx]->dm_mapsize, BUS_DMASYNC_PREREAD);
1075
1076 SQ_INIT_RXDESC(sc, idx);
1077
1078 return 0;
1079 }
1080
1081 void
1082 sq_dump_buffer(u_int32_t addr, u_int32_t len)
1083 {
1084 u_int i;
1085 u_char* physaddr = (char*) MIPS_PHYS_TO_KSEG1((caddr_t)addr);
1086
1087 if (len == 0)
1088 return;
1089
1090 printf("%p: ", physaddr);
1091
1092 for(i = 0; i < len; i++) {
1093 printf("%02x ", *(physaddr + i) & 0xff);
1094 if ((i % 16) == 15 && i != len - 1)
1095 printf("\n%p: ", physaddr + i);
1096 }
1097
1098 printf("\n");
1099 }
1100
1101
1102 void
1103 enaddr_aton(const char* str, u_int8_t* eaddr)
1104 {
1105 int i;
1106 char c;
1107
1108 for(i = 0; i < ETHER_ADDR_LEN; i++) {
1109 if (*str == ':')
1110 str++;
1111
1112 c = *str++;
1113 if (isdigit(c)) {
1114 eaddr[i] = (c - '0');
1115 } else if (isxdigit(c)) {
1116 eaddr[i] = (toupper(c) + 10 - 'A');
1117 }
1118
1119 c = *str++;
1120 if (isdigit(c)) {
1121 eaddr[i] = (eaddr[i] << 4) | (c - '0');
1122 } else if (isxdigit(c)) {
1123 eaddr[i] = (eaddr[i] << 4) | (toupper(c) + 10 - 'A');
1124 }
1125 }
1126 }
1127