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