hd64570.c revision 1.5 1 /* $NetBSD: hd64570.c,v 1.5 1999/03/16 21:29:23 erh Exp $ */
2
3 /*
4 * Copyright (c) 1998 Vixie Enterprises
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Vixie Enterprises nor the names
17 * of its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY VIXIE ENTERPRISES AND
21 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL VIXIE ENTERPRISES OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * This software has been written for Vixie Enterprises by Michael Graff
35 * <explorer (at) flame.org>. To learn more about Vixie Enterprises, see
36 * ``http://www.vix.com''.
37 */
38
39 /*
40 * TODO:
41 *
42 * o teach the receive logic about errors, and about long frames that
43 * span more than one input buffer. (Right now, receive/transmit is
44 * limited to one descriptor's buffer space, which is MTU + 4 bytes.
45 * This is currently 1504, which is large enough to hold the HDLC
46 * header and the packet itself. Packets which are too long are
47 * silently dropped on transmit and silently dropped on receive.
48 * o write code to handle the msci interrupts, needed only for CD
49 * and CTS changes.
50 * o consider switching back to a "queue tx with DMA active" model which
51 * should help sustain outgoing traffic
52 * o through clever use of bus_dma*() functions, it should be possible
53 * to map the mbuf's data area directly into a descriptor transmit
54 * buffer, removing the need to allocate extra memory. If, however,
55 * we run out of descriptors for this, we will need to then allocate
56 * one large mbuf, copy the fragmented chain into it, and put it onto
57 * a single descriptor.
58 * o use bus_dmamap_sync() with the right offset and lengths, rather
59 * than cheating and always sync'ing the whole region.
60 */
61
62 #include "bpfilter.h"
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/device.h>
67 #include <sys/mbuf.h>
68 #include <sys/socket.h>
69 #include <sys/sockio.h>
70 #include <sys/kernel.h>
71
72 #include <net/if.h>
73 #include <net/if_types.h>
74 #include <net/netisr.h>
75
76 #include <netinet/in.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/in_var.h>
79 #include <netinet/ip.h>
80
81 #if NBPFILTER > 0
82 #include <net/bpf.h>
83 #endif
84
85 #include <machine/cpu.h>
86 #include <machine/bus.h>
87 #include <machine/intr.h>
88
89 #include <dev/pci/pcivar.h>
90 #include <dev/pci/pcireg.h>
91 #include <dev/pci/pcidevs.h>
92
93 #include <dev/ic/hd64570reg.h>
94 #include <dev/ic/hd64570var.h>
95
96 #define SCA_DEBUG_RX 0x0001
97 #define SCA_DEBUG_TX 0x0002
98 #define SCA_DEBUG_CISCO 0x0004
99 #define SCA_DEBUG_DMA 0x0008
100 #define SCA_DEBUG_RXPKT 0x0010
101 #define SCA_DEBUG_TXPKT 0x0020
102 #define SCA_DEBUG_INTR 0x0040
103
104 #if 0
105 #define SCA_DEBUG_LEVEL ( SCA_DEBUG_TX )
106 #else
107 #define SCA_DEBUG_LEVEL 0
108 #endif
109
110 u_int32_t sca_debug = SCA_DEBUG_LEVEL;
111
112 #if SCA_DEBUG_LEVEL > 0
113 #define SCA_DPRINTF(l, x) do { \
114 if ((l) & sca_debug) \
115 printf x;\
116 } while (0)
117 #else
118 #define SCA_DPRINTF(l, x)
119 #endif
120
121 #define SCA_MTU 1500 /* hard coded */
122
123 /*
124 * buffers per tx and rx channels, per port, and the size of each.
125 * Don't use these constants directly, as they are really only hints.
126 * Use the calculated values stored in struct sca_softc instead.
127 *
128 * Each must be at least 2, receive would be better at around 20 or so.
129 *
130 * XXX Due to a damned near impossible to track down bug, transmit buffers
131 * MUST be 2, no more, no less.
132 */
133 #ifndef SCA_NtxBUFS
134 #define SCA_NtxBUFS 2
135 #endif
136 #ifndef SCA_NrxBUFS
137 #define SCA_NrxBUFS 20
138 #endif
139 #ifndef SCA_BSIZE
140 #define SCA_BSIZE (SCA_MTU + 4) /* room for HDLC as well */
141 #endif
142
143 #if 0
144 #define SCA_USE_FASTQ /* use a split queue, one for fast traffic */
145 #endif
146
147 static inline void sca_write_1(struct sca_softc *, u_int, u_int8_t);
148 static inline void sca_write_2(struct sca_softc *, u_int, u_int16_t);
149 static inline u_int8_t sca_read_1(struct sca_softc *, u_int);
150 static inline u_int16_t sca_read_2(struct sca_softc *, u_int);
151
152 static inline void msci_write_1(sca_port_t *, u_int, u_int8_t);
153 static inline u_int8_t msci_read_1(sca_port_t *, u_int);
154
155 static inline void dmac_write_1(sca_port_t *, u_int, u_int8_t);
156 static inline void dmac_write_2(sca_port_t *, u_int, u_int16_t);
157 static inline u_int8_t dmac_read_1(sca_port_t *, u_int);
158 static inline u_int16_t dmac_read_2(sca_port_t *, u_int);
159
160 static int sca_alloc_dma(struct sca_softc *);
161 static void sca_setup_dma_memory(struct sca_softc *);
162 static void sca_msci_init(struct sca_softc *, sca_port_t *);
163 static void sca_dmac_init(struct sca_softc *, sca_port_t *);
164 static void sca_dmac_rxinit(sca_port_t *);
165
166 static int sca_dmac_intr(sca_port_t *, u_int8_t);
167 static int sca_msci_intr(struct sca_softc *, u_int8_t);
168
169 static void sca_get_packets(sca_port_t *);
170 static void sca_frame_process(sca_port_t *, sca_desc_t *, u_int8_t *);
171 static int sca_frame_avail(sca_port_t *, int *);
172 static void sca_frame_skip(sca_port_t *, int);
173
174 static void sca_port_starttx(sca_port_t *);
175
176 static void sca_port_up(sca_port_t *);
177 static void sca_port_down(sca_port_t *);
178
179 static int sca_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
180 struct rtentry *));
181 static int sca_ioctl __P((struct ifnet *, u_long, caddr_t));
182 static void sca_start __P((struct ifnet *));
183 static void sca_watchdog __P((struct ifnet *));
184
185 static struct mbuf *sca_mbuf_alloc(caddr_t, u_int);
186
187 #if SCA_DEBUG_LEVEL > 0
188 static void sca_frame_print(sca_port_t *, sca_desc_t *, u_int8_t *);
189 #endif
190
191 static inline void
192 sca_write_1(struct sca_softc *sc, u_int reg, u_int8_t val)
193 {
194 bus_space_write_1(sc->sc_iot, sc->sc_ioh, SCADDR(reg), val);
195 }
196
197 static inline void
198 sca_write_2(struct sca_softc *sc, u_int reg, u_int16_t val)
199 {
200 bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCADDR(reg), val);
201 }
202
203 static inline u_int8_t
204 sca_read_1(struct sca_softc *sc, u_int reg)
205 {
206 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, SCADDR(reg));
207 }
208
209 static inline u_int16_t
210 sca_read_2(struct sca_softc *sc, u_int reg)
211 {
212 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCADDR(reg));
213 }
214
215 static inline void
216 msci_write_1(sca_port_t *scp, u_int reg, u_int8_t val)
217 {
218 sca_write_1(scp->sca, scp->msci_off + reg, val);
219 }
220
221 static inline u_int8_t
222 msci_read_1(sca_port_t *scp, u_int reg)
223 {
224 return sca_read_1(scp->sca, scp->msci_off + reg);
225 }
226
227 static inline void
228 dmac_write_1(sca_port_t *scp, u_int reg, u_int8_t val)
229 {
230 sca_write_1(scp->sca, scp->dmac_off + reg, val);
231 }
232
233 static inline void
234 dmac_write_2(sca_port_t *scp, u_int reg, u_int16_t val)
235 {
236 sca_write_2(scp->sca, scp->dmac_off + reg, val);
237 }
238
239 static inline u_int8_t
240 dmac_read_1(sca_port_t *scp, u_int reg)
241 {
242 return sca_read_1(scp->sca, scp->dmac_off + reg);
243 }
244
245 static inline u_int16_t
246 dmac_read_2(sca_port_t *scp, u_int reg)
247 {
248 return sca_read_2(scp->sca, scp->dmac_off + reg);
249 }
250
251 int
252 sca_init(struct sca_softc *sc, u_int nports)
253 {
254 /*
255 * Do a little sanity check: check number of ports.
256 */
257 if (nports < 1 || nports > 2)
258 return 1;
259
260 /*
261 * remember the details
262 */
263 sc->sc_numports = nports;
264
265 /*
266 * allocate the memory and chop it into bits.
267 */
268 if (sca_alloc_dma(sc) != 0)
269 return 1;
270 sca_setup_dma_memory(sc);
271
272 /*
273 * disable DMA and MSCI interrupts
274 */
275 sca_write_1(sc, SCA_DMER, 0);
276 sca_write_1(sc, SCA_IER0, 0);
277 sca_write_1(sc, SCA_IER1, 0);
278 sca_write_1(sc, SCA_IER2, 0);
279
280 /*
281 * configure interrupt system
282 */
283 sca_write_1(sc, SCA_ITCR, 0); /* use ivr, no int ack */
284 sca_write_1(sc, SCA_IVR, 0x40);
285 sca_write_1(sc, SCA_IMVR, 0x40);
286
287 /*
288 * set wait control register to zero wait states
289 */
290 sca_write_1(sc, SCA_PABR0, 0);
291 sca_write_1(sc, SCA_PABR1, 0);
292 sca_write_1(sc, SCA_WCRL, 0);
293 sca_write_1(sc, SCA_WCRM, 0);
294 sca_write_1(sc, SCA_WCRH, 0);
295
296 /*
297 * disable DMA and reset status
298 */
299 sca_write_1(sc, SCA_PCR, SCA_PCR_PR2);
300
301 /*
302 * disable transmit DMA for all channels
303 */
304 sca_write_1(sc, SCA_DSR0 + SCA_DMAC_OFF_0, 0);
305 sca_write_1(sc, SCA_DCR0 + SCA_DMAC_OFF_0, SCA_DCR_ABRT);
306 sca_write_1(sc, SCA_DSR1 + SCA_DMAC_OFF_0, 0);
307 sca_write_1(sc, SCA_DCR1 + SCA_DMAC_OFF_0, SCA_DCR_ABRT);
308 sca_write_1(sc, SCA_DSR0 + SCA_DMAC_OFF_1, 0);
309 sca_write_1(sc, SCA_DCR0 + SCA_DMAC_OFF_1, SCA_DCR_ABRT);
310 sca_write_1(sc, SCA_DSR1 + SCA_DMAC_OFF_1, 0);
311 sca_write_1(sc, SCA_DCR1 + SCA_DMAC_OFF_1, SCA_DCR_ABRT);
312
313 /*
314 * enable DMA based on channel enable flags for each channel
315 */
316 sca_write_1(sc, SCA_DMER, SCA_DMER_EN);
317
318 /*
319 * Should check to see if the chip is responding, but for now
320 * assume it is.
321 */
322 return 0;
323 }
324
325 /*
326 * initialize the port and attach it to the networking layer
327 */
328 void
329 sca_port_attach(struct sca_softc *sc, u_int port)
330 {
331 sca_port_t *scp = &sc->sc_ports[port];
332 struct ifnet *ifp;
333 static u_int ntwo_unit = 0;
334
335 scp->sca = sc; /* point back to the parent */
336
337 scp->sp_port = port;
338
339 if (port == 0) {
340 scp->msci_off = SCA_MSCI_OFF_0;
341 scp->dmac_off = SCA_DMAC_OFF_0;
342 if(sc->parent != NULL)
343 ntwo_unit=sc->parent->dv_unit * 2 + 0;
344 else
345 ntwo_unit = 0; /* XXX */
346 } else {
347 scp->msci_off = SCA_MSCI_OFF_1;
348 scp->dmac_off = SCA_DMAC_OFF_1;
349 if(sc->parent != NULL)
350 ntwo_unit=sc->parent->dv_unit * 2 + 1;
351 else
352 ntwo_unit = 1; /* XXX */
353 }
354
355 sca_msci_init(sc, scp);
356 sca_dmac_init(sc, scp);
357
358 /*
359 * attach to the network layer
360 */
361 ifp = &scp->sp_if;
362 sprintf(ifp->if_xname, "ntwo%d", ntwo_unit);
363 ifp->if_softc = scp;
364 ifp->if_mtu = SCA_MTU;
365 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
366 ifp->if_type = IFT_OTHER; /* Should be HDLC, but... */
367 ifp->if_hdrlen = HDLC_HDRLEN;
368 ifp->if_ioctl = sca_ioctl;
369 ifp->if_output = sca_output;
370 ifp->if_watchdog = sca_watchdog;
371 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
372 scp->linkq.ifq_maxlen = 5; /* if we exceed this we are hosed already */
373 #ifdef SCA_USE_FASTQ
374 scp->fastq.ifq_maxlen = IFQ_MAXLEN;
375 #endif
376 if_attach(ifp);
377
378 #if NBPFILTER > 0
379 bpfattach(&scp->sp_bpf, ifp, DLT_HDLC, HDLC_HDRLEN);
380 #endif
381
382 if (sc->parent == NULL)
383 printf("%s: port %d\n", ifp->if_xname, port);
384 else
385 printf("%s at %s port %d\n",
386 ifp->if_xname, sc->parent->dv_xname, port);
387
388 /*
389 * reset the last seen times on the cisco keepalive protocol
390 */
391 scp->cka_lasttx = time.tv_usec;
392 scp->cka_lastrx = 0;
393 }
394
395 /*
396 * initialize the port's MSCI
397 */
398 static void
399 sca_msci_init(struct sca_softc *sc, sca_port_t *scp)
400 {
401 msci_write_1(scp, SCA_CMD0, SCA_CMD_RESET);
402 msci_write_1(scp, SCA_MD00,
403 ( SCA_MD0_CRC_1
404 | SCA_MD0_CRC_CCITT
405 | SCA_MD0_CRC_ENABLE
406 | SCA_MD0_MODE_HDLC));
407 msci_write_1(scp, SCA_MD10, SCA_MD1_NOADDRCHK);
408 msci_write_1(scp, SCA_MD20,
409 (SCA_MD2_DUPLEX | SCA_MD2_NRZ));
410
411 /*
412 * reset the port (and lower RTS)
413 */
414 msci_write_1(scp, SCA_CMD0, SCA_CMD_RXRESET);
415 msci_write_1(scp, SCA_CTL0,
416 (SCA_CTL_IDLPAT | SCA_CTL_UDRNC | SCA_CTL_RTS));
417 msci_write_1(scp, SCA_CMD0, SCA_CMD_TXRESET);
418
419 /*
420 * select the RX clock as the TX clock, and set for external
421 * clock source.
422 */
423 msci_write_1(scp, SCA_RXS0, 0);
424 msci_write_1(scp, SCA_TXS0, 0);
425
426 /*
427 * XXX don't pay attention to CTS or CD changes right now. I can't
428 * simulate one, and the transmitter will try to transmit even if
429 * CD isn't there anyway, so nothing bad SHOULD happen.
430 */
431 msci_write_1(scp, SCA_IE00, 0);
432 msci_write_1(scp, SCA_IE10, 0); /* 0x0c == CD and CTS changes only */
433 msci_write_1(scp, SCA_IE20, 0);
434 msci_write_1(scp, SCA_FIE0, 0);
435
436 msci_write_1(scp, SCA_SA00, 0);
437 msci_write_1(scp, SCA_SA10, 0);
438
439 msci_write_1(scp, SCA_IDL0, 0x7e);
440
441 msci_write_1(scp, SCA_RRC0, 0x0e);
442 msci_write_1(scp, SCA_TRC00, 0x10);
443 msci_write_1(scp, SCA_TRC10, 0x1f);
444 }
445
446 /*
447 * Take the memory for the port and construct two circular linked lists of
448 * descriptors (one tx, one rx) and set the pointers in these descriptors
449 * to point to the buffer space for this port.
450 */
451 static void
452 sca_dmac_init(struct sca_softc *sc, sca_port_t *scp)
453 {
454 sca_desc_t *desc;
455 u_int32_t desc_p;
456 u_int32_t buf_p;
457 int i;
458
459 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmam,
460 0, sc->sc_allocsize, BUS_DMASYNC_PREWRITE);
461
462 desc = scp->txdesc;
463 desc_p = scp->txdesc_p;
464 buf_p = scp->txbuf_p;
465 scp->txcur = 0;
466 scp->txinuse = 0;
467
468 for (i = 0 ; i < SCA_NtxBUFS ; i++) {
469 /*
470 * desc_p points to the physcial address of the NEXT desc
471 */
472 desc_p += sizeof(sca_desc_t);
473
474 desc->cp = desc_p & 0x0000ffff;
475 desc->bp = buf_p & 0x0000ffff;
476 desc->bpb = (buf_p & 0x00ff0000) >> 16;
477 desc->len = SCA_BSIZE;
478 desc->stat = 0;
479
480 desc++; /* point to the next descriptor */
481 buf_p += SCA_BSIZE;
482 }
483
484 /*
485 * "heal" the circular list by making the last entry point to the
486 * first.
487 */
488 desc--;
489 desc->cp = scp->txdesc_p & 0x0000ffff;
490
491 /*
492 * Now, initialize the transmit DMA logic
493 *
494 * CPB == chain pointer base address
495 */
496 dmac_write_1(scp, SCA_DSR1, 0);
497 dmac_write_1(scp, SCA_DCR1, SCA_DCR_ABRT);
498 dmac_write_1(scp, SCA_DMR1, SCA_DMR_TMOD | SCA_DMR_NF);
499 dmac_write_1(scp, SCA_DIR1,
500 (SCA_DIR_EOT | SCA_DIR_BOF | SCA_DIR_COF));
501 dmac_write_1(scp, SCA_CPB1,
502 (u_int8_t)((scp->txdesc_p & 0x00ff0000) >> 16));
503
504 /*
505 * now, do the same thing for receive descriptors
506 */
507 desc = scp->rxdesc;
508 desc_p = scp->rxdesc_p;
509 buf_p = scp->rxbuf_p;
510 scp->rxstart = 0;
511 scp->rxend = SCA_NrxBUFS - 1;
512
513 for (i = 0 ; i < SCA_NrxBUFS ; i++) {
514 /*
515 * desc_p points to the physcial address of the NEXT desc
516 */
517 desc_p += sizeof(sca_desc_t);
518
519 desc->cp = desc_p & 0x0000ffff;
520 desc->bp = buf_p & 0x0000ffff;
521 desc->bpb = (buf_p & 0x00ff0000) >> 16;
522 desc->len = SCA_BSIZE;
523 desc->stat = 0x00;
524
525 desc++; /* point to the next descriptor */
526 buf_p += SCA_BSIZE;
527 }
528
529 /*
530 * "heal" the circular list by making the last entry point to the
531 * first.
532 */
533 desc--;
534 desc->cp = scp->rxdesc_p & 0x0000ffff;
535
536 sca_dmac_rxinit(scp);
537
538 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmam,
539 0, sc->sc_allocsize, BUS_DMASYNC_POSTWRITE);
540 }
541
542 /*
543 * reset and reinitialize the receive DMA logic
544 */
545 static void
546 sca_dmac_rxinit(sca_port_t *scp)
547 {
548 /*
549 * ... and the receive DMA logic ...
550 */
551 dmac_write_1(scp, SCA_DSR0, 0); /* disable DMA */
552 dmac_write_1(scp, SCA_DCR0, SCA_DCR_ABRT);
553
554 dmac_write_1(scp, SCA_DMR0, SCA_DMR_TMOD | SCA_DMR_NF);
555 dmac_write_2(scp, SCA_BFLL0, SCA_BSIZE);
556
557 /*
558 * CPB == chain pointer base
559 * CDA == current descriptor address
560 * EDA == error descriptor address (overwrite position)
561 */
562 dmac_write_1(scp, SCA_CPB0,
563 (u_int8_t)((scp->rxdesc_p & 0x00ff0000) >> 16));
564 dmac_write_2(scp, SCA_CDAL0,
565 (u_int16_t)(scp->rxdesc_p & 0xffff));
566 dmac_write_2(scp, SCA_EDAL0,
567 (u_int16_t)(scp->rxdesc_p
568 + sizeof(sca_desc_t) * SCA_NrxBUFS));
569
570 /*
571 * enable receiver DMA
572 */
573 dmac_write_1(scp, SCA_DIR0,
574 (SCA_DIR_EOT | SCA_DIR_EOM | SCA_DIR_BOF | SCA_DIR_COF));
575 dmac_write_1(scp, SCA_DSR0, SCA_DSR_DE);
576 }
577
578 static int
579 sca_alloc_dma(struct sca_softc *sc)
580 {
581 u_int allocsize;
582 int err;
583 int rsegs;
584 u_int bpp;
585
586 SCA_DPRINTF(SCA_DEBUG_DMA,
587 ("sizeof sca_desc_t: %d bytes\n", sizeof (sca_desc_t)));
588
589 bpp = sc->sc_numports * (SCA_NtxBUFS + SCA_NrxBUFS);
590
591 allocsize = bpp * (SCA_BSIZE + sizeof (sca_desc_t));
592
593 /*
594 * sanity checks:
595 *
596 * Check the total size of the data buffers, and so on. The total
597 * DMAable space needs to fit within a single 16M region, and the
598 * descriptors need to fit within a 64K region.
599 */
600 if (allocsize > 16 * 1024 * 1024)
601 return 1;
602 if (bpp * sizeof (sca_desc_t) > 64 * 1024)
603 return 1;
604
605 sc->sc_allocsize = allocsize;
606
607 /*
608 * Allocate one huge chunk of memory.
609 */
610 if (bus_dmamem_alloc(sc->sc_dmat,
611 allocsize,
612 SCA_DMA_ALIGNMENT,
613 SCA_DMA_BOUNDRY,
614 &sc->sc_seg, 1, &rsegs, BUS_DMA_NOWAIT) != 0) {
615 printf("Could not allocate DMA memory\n");
616 return 1;
617 }
618 SCA_DPRINTF(SCA_DEBUG_DMA,
619 ("DMA memory allocated: %d bytes\n", allocsize));
620
621 if (bus_dmamem_map(sc->sc_dmat, &sc->sc_seg, 1, allocsize,
622 &sc->sc_dma_addr, BUS_DMA_NOWAIT) != 0) {
623 printf("Could not map DMA memory into kernel space\n");
624 return 1;
625 }
626 SCA_DPRINTF(SCA_DEBUG_DMA, ("DMA memory mapped\n"));
627
628 if (bus_dmamap_create(sc->sc_dmat, allocsize, 2,
629 allocsize, SCA_DMA_BOUNDRY,
630 BUS_DMA_NOWAIT, &sc->sc_dmam) != 0) {
631 printf("Could not create DMA map\n");
632 return 1;
633 }
634 SCA_DPRINTF(SCA_DEBUG_DMA, ("DMA map created\n"));
635
636 err = bus_dmamap_load(sc->sc_dmat, sc->sc_dmam, sc->sc_dma_addr,
637 allocsize, NULL, BUS_DMA_NOWAIT);
638 if (err != 0) {
639 printf("Could not load DMA segment: %d\n", err);
640 return 1;
641 }
642 SCA_DPRINTF(SCA_DEBUG_DMA, ("DMA map loaded\n"));
643
644 return 0;
645 }
646
647 /*
648 * Take the memory allocated with sca_alloc_dma() and divide it among the
649 * two ports.
650 */
651 static void
652 sca_setup_dma_memory(struct sca_softc *sc)
653 {
654 sca_port_t *scp0, *scp1;
655 u_int8_t *vaddr0;
656 u_int32_t paddr0;
657 u_long addroff;
658
659 /*
660 * remember the physical address to 24 bits only, since the upper
661 * 8 bits is programed into the device at a different layer.
662 */
663 paddr0 = (sc->sc_dmam->dm_segs[0].ds_addr & 0x00ffffff);
664 vaddr0 = sc->sc_dma_addr;
665
666 /*
667 * if we have only one port it gets the full range. If we have
668 * two we need to do a little magic to divide things up.
669 *
670 * The descriptors will all end up in the front of the area, while
671 * the remainder of the buffer is used for transmit and receive
672 * data.
673 *
674 * -------------------- start of memory
675 * tx desc port 0
676 * rx desc port 0
677 * tx desc port 1
678 * rx desc port 1
679 * tx buffer port 0
680 * rx buffer port 0
681 * tx buffer port 1
682 * rx buffer port 1
683 * -------------------- end of memory
684 */
685 scp0 = &sc->sc_ports[0];
686 scp1 = &sc->sc_ports[1];
687
688 scp0->txdesc_p = paddr0;
689 scp0->txdesc = (sca_desc_t *)vaddr0;
690 addroff = sizeof(sca_desc_t) * SCA_NtxBUFS;
691
692 /*
693 * point to the range following the tx descriptors, and
694 * set the rx descriptors there.
695 */
696 scp0->rxdesc_p = paddr0 + addroff;
697 scp0->rxdesc = (sca_desc_t *)(vaddr0 + addroff);
698 addroff += sizeof(sca_desc_t) * SCA_NrxBUFS;
699
700 if (sc->sc_numports == 2) {
701 scp1->txdesc_p = paddr0 + addroff;
702 scp1->txdesc = (sca_desc_t *)(vaddr0 + addroff);
703 addroff += sizeof(sca_desc_t) * SCA_NtxBUFS;
704
705 scp1->rxdesc_p = paddr0 + addroff;
706 scp1->rxdesc = (sca_desc_t *)(vaddr0 + addroff);
707 addroff += sizeof(sca_desc_t) * SCA_NrxBUFS;
708 }
709
710 /*
711 * point to the memory following the descriptors, and set the
712 * transmit buffer there.
713 */
714 scp0->txbuf_p = paddr0 + addroff;
715 scp0->txbuf = vaddr0 + addroff;
716 addroff += SCA_BSIZE * SCA_NtxBUFS;
717
718 /*
719 * lastly, skip over the transmit buffer and set up pointers into
720 * the receive buffer.
721 */
722 scp0->rxbuf_p = paddr0 + addroff;
723 scp0->rxbuf = vaddr0 + addroff;
724 addroff += SCA_BSIZE * SCA_NrxBUFS;
725
726 if (sc->sc_numports == 2) {
727 scp1->txbuf_p = paddr0 + addroff;
728 scp1->txbuf = vaddr0 + addroff;
729 addroff += SCA_BSIZE * SCA_NtxBUFS;
730
731 scp1->rxbuf_p = paddr0 + addroff;
732 scp1->rxbuf = vaddr0 + addroff;
733 addroff += SCA_BSIZE * SCA_NrxBUFS;
734 }
735
736 /*
737 * as a consistancy check, addroff should be equal to the allocation
738 * size.
739 */
740 if (sc->sc_allocsize != addroff)
741 printf("ERROR: sc_allocsize != addroff: %lu != %lu\n",
742 sc->sc_allocsize, addroff);
743 }
744
745 /*
746 * Queue the packet for our start routine to transmit
747 */
748 static int
749 sca_output(ifp, m, dst, rt0)
750 struct ifnet *ifp;
751 struct mbuf *m;
752 struct sockaddr *dst;
753 struct rtentry *rt0;
754 {
755 int error;
756 int s;
757 u_int16_t protocol;
758 hdlc_header_t *hdlc;
759 struct ifqueue *ifq;
760 #ifdef SCA_USE_FASTQ
761 struct ip *ip;
762 sca_port_t *scp = ifp->if_softc;
763 int highpri;
764 #endif
765
766 error = 0;
767 ifp->if_lastchange = time;
768
769 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
770 error = ENETDOWN;
771 goto bad;
772 }
773
774 #ifdef SCA_USE_FASTQ
775 highpri = 0;
776 #endif
777
778 /*
779 * determine address family, and priority for this packet
780 */
781 switch (dst->sa_family) {
782 case AF_INET:
783 protocol = HDLC_PROTOCOL_IP;
784
785 #ifdef SCA_USE_FASTQ
786 ip = mtod(m, struct ip *);
787 if ((ip->ip_tos & IPTOS_LOWDELAY) == IPTOS_LOWDELAY)
788 highpri = 1;
789 #endif
790 break;
791
792 default:
793 printf("%s: address family %d unsupported\n",
794 ifp->if_xname, dst->sa_family);
795 error = EAFNOSUPPORT;
796 goto bad;
797 }
798
799 if (M_LEADINGSPACE(m) < HDLC_HDRLEN) {
800 m = m_prepend(m, HDLC_HDRLEN, M_DONTWAIT);
801 if (m == NULL) {
802 error = ENOBUFS;
803 goto bad;
804 }
805 m->m_len = 0;
806 } else {
807 m->m_data -= HDLC_HDRLEN;
808 }
809
810 hdlc = mtod(m, hdlc_header_t *);
811 if ((m->m_flags & (M_BCAST | M_MCAST)) != 0)
812 hdlc->addr = CISCO_MULTICAST;
813 else
814 hdlc->addr = CISCO_UNICAST;
815 hdlc->control = 0;
816 hdlc->protocol = htons(protocol);
817 m->m_len += HDLC_HDRLEN;
818
819 /*
820 * queue the packet. If interactive, use the fast queue.
821 */
822 s = splnet();
823 #ifdef SCA_USE_FASTQ
824 ifq = (highpri == 1 ? &scp->fastq : &ifp->if_snd);
825 #else
826 ifq = &ifp->if_snd;
827 #endif
828 if (IF_QFULL(ifq)) {
829 IF_DROP(ifq);
830 ifp->if_oerrors++;
831 ifp->if_collisions++;
832 error = ENOBUFS;
833 splx(s);
834 goto bad;
835 }
836 ifp->if_obytes += m->m_pkthdr.len;
837 IF_ENQUEUE(ifq, m);
838
839 ifp->if_lastchange = time;
840
841 if (m->m_flags & M_MCAST)
842 ifp->if_omcasts++;
843
844 sca_start(ifp);
845 splx(s);
846
847 return (error);
848
849 bad:
850 if (m)
851 m_freem(m);
852 return (error);
853 }
854
855 static int
856 sca_ioctl(ifp, cmd, addr)
857 struct ifnet *ifp;
858 u_long cmd;
859 caddr_t addr;
860 {
861 struct ifreq *ifr;
862 struct ifaddr *ifa;
863 int error;
864 int s;
865
866 s = splnet();
867
868 ifr = (struct ifreq *)addr;
869 ifa = (struct ifaddr *)addr;
870 error = 0;
871
872 switch (cmd) {
873 case SIOCSIFADDR:
874 if (ifa->ifa_addr->sa_family == AF_INET)
875 sca_port_up(ifp->if_softc);
876 else
877 error = EAFNOSUPPORT;
878 break;
879
880 case SIOCSIFDSTADDR:
881 if (ifa->ifa_addr->sa_family != AF_INET)
882 error = EAFNOSUPPORT;
883 break;
884
885 case SIOCADDMULTI:
886 case SIOCDELMULTI:
887 if (ifr == 0) {
888 error = EAFNOSUPPORT; /* XXX */
889 break;
890 }
891 switch (ifr->ifr_addr.sa_family) {
892
893 #ifdef INET
894 case AF_INET:
895 break;
896 #endif
897
898 default:
899 error = EAFNOSUPPORT;
900 break;
901 }
902 break;
903
904 case SIOCSIFFLAGS:
905 if (ifr->ifr_flags & IFF_UP)
906 sca_port_up(ifp->if_softc);
907 else
908 sca_port_down(ifp->if_softc);
909
910 break;
911
912 default:
913 error = EINVAL;
914 }
915
916 splx(s);
917 return error;
918 }
919
920 /*
921 * start packet transmission on the interface
922 *
923 * MUST BE CALLED AT splnet()
924 */
925 static void
926 sca_start(ifp)
927 struct ifnet *ifp;
928 {
929 sca_port_t *scp = ifp->if_softc;
930 struct sca_softc *sc = scp->sca;
931 struct mbuf *m, *mb_head;
932 sca_desc_t *desc;
933 u_int8_t *buf;
934 u_int32_t buf_p;
935 int trigger_xmit;
936
937 /*
938 * can't queue when we are full or transmitter is busy
939 */
940 if ((scp->txinuse >= (SCA_NtxBUFS - 1))
941 || ((ifp->if_flags & IFF_OACTIVE) == IFF_OACTIVE))
942 return;
943
944 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmam,
945 0, sc->sc_allocsize,
946 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
947
948 trigger_xmit = 0;
949
950 txloop:
951 IF_DEQUEUE(&scp->linkq, mb_head);
952 if (mb_head == NULL)
953 #ifdef SCA_USE_FASTQ
954 IF_DEQUEUE(&scp->fastq, mb_head);
955 if (mb_head == NULL)
956 #endif
957 IF_DEQUEUE(&ifp->if_snd, mb_head);
958 if (mb_head == NULL)
959 goto start_xmit;
960
961 desc = &scp->txdesc[scp->txcur];
962 if (scp->txinuse != 0) {
963 /* Kill EOT interrupts on the previous packet. */
964 desc->stat &= ~SCA_DESC_EOT;
965 desc = &scp->txdesc[scp->txcur+1];
966 }
967 buf = scp->txbuf + SCA_BSIZE * scp->txcur;
968 buf_p = scp->txbuf_p + SCA_BSIZE * scp->txcur;
969
970 desc->bp = (u_int16_t)(buf_p & 0x0000ffff);
971 desc->bpb = (u_int8_t)((buf_p & 0x00ff0000) >> 16);
972 desc->stat = SCA_DESC_EOT | SCA_DESC_EOM; /* end of frame and xfer */
973 desc->len = 0;
974
975 /*
976 * Run through the chain, copying data into the descriptor as we
977 * go. If it won't fit in one transmission block, drop the packet.
978 * No, this isn't nice, but most of the time it _will_ fit.
979 */
980 for (m = mb_head ; m != NULL ; m = m->m_next) {
981 if (m->m_len != 0) {
982 desc->len += m->m_len;
983 if (desc->len > SCA_BSIZE) {
984 m_freem(mb_head);
985 goto txloop;
986 }
987 bcopy(mtod(m, u_int8_t *), buf, m->m_len);
988 buf += m->m_len;
989 }
990 }
991
992 ifp->if_opackets++;
993
994 #if NBPFILTER > 0
995 /*
996 * Pass packet to bpf if there is a listener.
997 */
998 if (scp->sp_bpf)
999 bpf_mtap(scp->sp_bpf, mb_head);
1000 #endif
1001
1002 m_freem(mb_head);
1003
1004 if (scp->txinuse != 0) {
1005 scp->txcur++;
1006 if (scp->txcur == SCA_NtxBUFS)
1007 scp->txcur = 0;
1008 }
1009 scp->txinuse++;
1010 trigger_xmit = 1;
1011
1012 SCA_DPRINTF(SCA_DEBUG_TX,
1013 ("TX: inuse %d index %d\n", scp->txinuse, scp->txcur));
1014
1015 if (scp->txinuse < (SCA_NtxBUFS - 1))
1016 goto txloop;
1017
1018 start_xmit:
1019 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmam,
1020 0, sc->sc_allocsize,
1021 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1022
1023 if (trigger_xmit != 0)
1024 sca_port_starttx(scp);
1025 }
1026
1027 static void
1028 sca_watchdog(ifp)
1029 struct ifnet *ifp;
1030 {
1031 }
1032
1033 int
1034 sca_hardintr(struct sca_softc *sc)
1035 {
1036 u_int8_t isr0, isr1, isr2;
1037 int ret;
1038
1039 ret = 0; /* non-zero means we processed at least one interrupt */
1040
1041 while (1) {
1042 /*
1043 * read SCA interrupts
1044 */
1045 isr0 = sca_read_1(sc, SCA_ISR0);
1046 isr1 = sca_read_1(sc, SCA_ISR1);
1047 isr2 = sca_read_1(sc, SCA_ISR2);
1048
1049 if (isr0 == 0 && isr1 == 0 && isr2 == 0)
1050 break;
1051
1052 SCA_DPRINTF(SCA_DEBUG_INTR,
1053 ("isr0 = %02x, isr1 = %02x, isr2 = %02x\n",
1054 isr0, isr1, isr2));
1055
1056 /*
1057 * check DMA interrupt
1058 */
1059 if (isr1 & 0x0f)
1060 ret += sca_dmac_intr(&sc->sc_ports[0],
1061 isr1 & 0x0f);
1062 if (isr1 & 0xf0)
1063 ret += sca_dmac_intr(&sc->sc_ports[1],
1064 (isr1 & 0xf0) >> 4);
1065
1066 if (isr0)
1067 ret += sca_msci_intr(sc, isr0);
1068
1069 #if 0 /* We don't GET timer interrupts, we have them disabled (msci IE20) */
1070 if (isr2)
1071 ret += sca_timer_intr(sc, isr2);
1072 #endif
1073 }
1074
1075 return (ret);
1076 }
1077
1078 static int
1079 sca_dmac_intr(sca_port_t *scp, u_int8_t isr)
1080 {
1081 u_int8_t dsr;
1082 int ret;
1083
1084 ret = 0;
1085
1086 /*
1087 * Check transmit channel
1088 */
1089 if (isr & 0x0c) {
1090 SCA_DPRINTF(SCA_DEBUG_INTR,
1091 ("TX INTERRUPT port %d\n", scp->sp_port));
1092
1093 dsr = 1;
1094 while (dsr != 0) {
1095 ret++;
1096 /*
1097 * reset interrupt
1098 */
1099 dsr = dmac_read_1(scp, SCA_DSR1);
1100 dmac_write_1(scp, SCA_DSR1,
1101 dsr | SCA_DSR_DEWD);
1102
1103 /*
1104 * filter out the bits we don't care about
1105 */
1106 dsr &= ( SCA_DSR_COF | SCA_DSR_BOF | SCA_DSR_EOT);
1107 if (dsr == 0)
1108 break;
1109
1110 /*
1111 * check for counter overflow
1112 */
1113 if (dsr & SCA_DSR_COF) {
1114 printf("%s: TXDMA counter overflow\n",
1115 scp->sp_if.if_xname);
1116
1117 scp->sp_if.if_flags &= ~IFF_OACTIVE;
1118 scp->txcur = 0;
1119 scp->txinuse = 0;
1120 }
1121
1122 /*
1123 * check for buffer overflow
1124 */
1125 if (dsr & SCA_DSR_BOF) {
1126 printf("%s: TXDMA buffer overflow, cda 0x%04x, eda 0x%04x, cpb 0x%02x\n",
1127 scp->sp_if.if_xname,
1128 dmac_read_2(scp, SCA_CDAL1),
1129 dmac_read_2(scp, SCA_EDAL1),
1130 dmac_read_1(scp, SCA_CPB1));
1131
1132 /*
1133 * Yikes. Arrange for a full
1134 * transmitter restart.
1135 */
1136 scp->sp_if.if_flags &= ~IFF_OACTIVE;
1137 scp->txcur = 0;
1138 scp->txinuse = 0;
1139 }
1140
1141 /*
1142 * check for end of transfer, which is not
1143 * an error. It means that all data queued
1144 * was transmitted, and we mark ourself as
1145 * not in use and stop the watchdog timer.
1146 */
1147 if (dsr & SCA_DSR_EOT) {
1148 SCA_DPRINTF(SCA_DEBUG_TX,
1149 ("Transmit completed.\n"));
1150
1151 scp->sp_if.if_flags &= ~IFF_OACTIVE;
1152 scp->txcur = 0;
1153 scp->txinuse = 0;
1154
1155 /*
1156 * check for more packets
1157 */
1158 sca_start(&scp->sp_if);
1159 }
1160 }
1161 }
1162 /*
1163 * receive channel check
1164 */
1165 if (isr & 0x03) {
1166 SCA_DPRINTF(SCA_DEBUG_INTR,
1167 ("RX INTERRUPT port %d\n", mch));
1168
1169 dsr = 1;
1170 while (dsr != 0) {
1171 ret++;
1172
1173 dsr = dmac_read_1(scp, SCA_DSR0);
1174 dmac_write_1(scp, SCA_DSR0, dsr | SCA_DSR_DEWD);
1175
1176 /*
1177 * filter out the bits we don't care about
1178 */
1179 dsr &= (SCA_DSR_EOM | SCA_DSR_COF
1180 | SCA_DSR_BOF | SCA_DSR_EOT);
1181 if (dsr == 0)
1182 break;
1183
1184 /*
1185 * End of frame
1186 */
1187 if (dsr & SCA_DSR_EOM) {
1188 SCA_DPRINTF(SCA_DEBUG_RX, ("Got a frame!\n"));
1189
1190 sca_get_packets(scp);
1191 }
1192
1193 /*
1194 * check for counter overflow
1195 */
1196 if (dsr & SCA_DSR_COF) {
1197 printf("%s: RXDMA counter overflow\n",
1198 scp->sp_if.if_xname);
1199
1200 sca_dmac_rxinit(scp);
1201 }
1202
1203 /*
1204 * check for end of transfer, which means we
1205 * ran out of descriptors to receive into.
1206 * This means the line is much faster than
1207 * we can handle.
1208 */
1209 if (dsr & (SCA_DSR_BOF | SCA_DSR_EOT)) {
1210 printf("%s: RXDMA buffer overflow\n",
1211 scp->sp_if.if_xname);
1212
1213 sca_dmac_rxinit(scp);
1214 }
1215 }
1216 }
1217
1218 return ret;
1219 }
1220
1221 static int
1222 sca_msci_intr(struct sca_softc *sc, u_int8_t isr)
1223 {
1224 printf("Got msci interrupt XXX\n");
1225
1226 return 0;
1227 }
1228
1229 static void
1230 sca_get_packets(sca_port_t *scp)
1231 {
1232 int descidx;
1233 sca_desc_t *desc;
1234 u_int8_t *buf;
1235
1236 bus_dmamap_sync(scp->sca->sc_dmat, scp->sca->sc_dmam,
1237 0, scp->sca->sc_allocsize,
1238 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1239
1240 /*
1241 * Loop while there are packets to receive. After each is processed,
1242 * call sca_frame_skip() to update the DMA registers to the new
1243 * state.
1244 */
1245 while (sca_frame_avail(scp, &descidx)) {
1246 desc = &scp->rxdesc[descidx];
1247 buf = scp->rxbuf + SCA_BSIZE * descidx;
1248
1249 sca_frame_process(scp, desc, buf);
1250 #if SCA_DEBUG_LEVEL > 0
1251 if (sca_debug & SCA_DEBUG_RXPKT)
1252 sca_frame_print(scp, desc, buf);
1253 #endif
1254 sca_frame_skip(scp, descidx);
1255 }
1256
1257 bus_dmamap_sync(scp->sca->sc_dmat, scp->sca->sc_dmam,
1258 0, scp->sca->sc_allocsize,
1259 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1260 }
1261
1262 /*
1263 * Starting with the first descriptor we wanted to read into, up to but
1264 * not including the current SCA read descriptor, look for a packet.
1265 */
1266 static int
1267 sca_frame_avail(sca_port_t *scp, int *descindx)
1268 {
1269 u_int16_t cda;
1270 int cdaidx;
1271 u_int32_t desc_p; /* physical address (lower 16 bits) */
1272 sca_desc_t *desc;
1273 u_int8_t rxstat;
1274
1275 /*
1276 * Read the current descriptor from the SCA.
1277 */
1278 cda = dmac_read_2(scp, SCA_CDAL0);
1279
1280 /*
1281 * calculate the index of the current descriptor
1282 */
1283 desc_p = cda - (u_int16_t)(scp->rxdesc_p & 0x0000ffff);
1284 cdaidx = desc_p / sizeof(sca_desc_t);
1285
1286 if (cdaidx >= SCA_NrxBUFS)
1287 return 0;
1288
1289 for (;;) {
1290 /*
1291 * if the SCA is reading into the first descriptor, we somehow
1292 * got this interrupt incorrectly. Just return that there are
1293 * no packets ready.
1294 */
1295 if (cdaidx == scp->rxstart)
1296 return 0;
1297
1298 /*
1299 * We might have a valid descriptor. Set up a pointer
1300 * to the kva address for it so we can more easily examine
1301 * the contents.
1302 */
1303 desc = &scp->rxdesc[scp->rxstart];
1304
1305 rxstat = desc->stat;
1306
1307 /*
1308 * check for errors
1309 */
1310 if (rxstat & SCA_DESC_ERRORS)
1311 goto nextpkt;
1312
1313 /*
1314 * full packet? Good.
1315 */
1316 if (rxstat & SCA_DESC_EOM) {
1317 *descindx = scp->rxstart;
1318 return 1;
1319 }
1320
1321 /*
1322 * increment the rxstart address, since this frame is
1323 * somehow damaged. Skip over it in later calls.
1324 * XXX This breaks multidescriptor receives, so each
1325 * frame HAS to fit within one descriptor's buffer
1326 * space now...
1327 */
1328 nextpkt:
1329 scp->rxstart++;
1330 if (scp->rxstart == SCA_NrxBUFS)
1331 scp->rxstart = 0;
1332 }
1333
1334 return 0;
1335 }
1336
1337 /*
1338 * Pass the packet up to the kernel if it is a packet we want to pay
1339 * attention to.
1340 *
1341 * MUST BE CALLED AT splnet()
1342 */
1343 static void
1344 sca_frame_process(sca_port_t *scp, sca_desc_t *desc, u_int8_t *p)
1345 {
1346 hdlc_header_t *hdlc;
1347 cisco_pkt_t *cisco, *ncisco;
1348 u_int16_t len;
1349 struct mbuf *m;
1350 u_int8_t *nbuf;
1351 u_int32_t t = (time.tv_sec - boottime.tv_sec) * 1000;
1352 struct ifqueue *ifq;
1353
1354 len = desc->len;
1355
1356 /*
1357 * skip packets that are too short
1358 */
1359 if (len < sizeof(hdlc_header_t))
1360 return;
1361
1362 #if NBPFILTER > 0
1363 if (scp->sp_bpf)
1364 bpf_tap(scp->sp_bpf, p, len);
1365 #endif
1366
1367 /*
1368 * read and then strip off the HDLC information
1369 */
1370 hdlc = (hdlc_header_t *)p;
1371
1372 scp->sp_if.if_ipackets++;
1373 scp->sp_if.if_lastchange = time;
1374
1375 switch (ntohs(hdlc->protocol)) {
1376 case HDLC_PROTOCOL_IP:
1377 SCA_DPRINTF(SCA_DEBUG_RX, ("Received IP packet\n"));
1378
1379 m = sca_mbuf_alloc(p, len);
1380 if (m == NULL) {
1381 scp->sp_if.if_iqdrops++;
1382 return;
1383 }
1384 m->m_pkthdr.rcvif = &scp->sp_if;
1385
1386 if (IF_QFULL(&ipintrq)) {
1387 IF_DROP(&ipintrq);
1388 scp->sp_if.if_ierrors++;
1389 scp->sp_if.if_iqdrops++;
1390 m_freem(m);
1391 } else {
1392 /*
1393 * strip off the HDLC header and hand off to IP stack
1394 */
1395 m->m_pkthdr.len -= HDLC_HDRLEN;
1396 m->m_data += HDLC_HDRLEN;
1397 m->m_len -= HDLC_HDRLEN;
1398 IF_ENQUEUE(&ipintrq, m);
1399 schednetisr(NETISR_IP);
1400 }
1401
1402 break;
1403
1404 case CISCO_KEEPALIVE:
1405 SCA_DPRINTF(SCA_DEBUG_CISCO,
1406 ("Received CISCO keepalive packet\n"));
1407
1408 if (len < CISCO_PKT_LEN) {
1409 SCA_DPRINTF(SCA_DEBUG_CISCO,
1410 ("short CISCO packet %d, wanted %d\n",
1411 len, CISCO_PKT_LEN));
1412 return;
1413 }
1414
1415 /*
1416 * allocate an mbuf and copy the important bits of data
1417 * into it.
1418 */
1419 m = sca_mbuf_alloc(p, HDLC_HDRLEN + CISCO_PKT_LEN);
1420 if (m == NULL)
1421 return;
1422
1423 nbuf = mtod(m, u_int8_t *);
1424 ncisco = (cisco_pkt_t *)(nbuf + HDLC_HDRLEN);
1425 m->m_pkthdr.rcvif = &scp->sp_if;
1426
1427 cisco = (cisco_pkt_t *)(p + HDLC_HDRLEN);
1428
1429 switch (ntohl(cisco->type)) {
1430 case CISCO_ADDR_REQ:
1431 printf("Got CISCO addr_req, ignoring\n");
1432 m_freem(m);
1433 break;
1434
1435 case CISCO_ADDR_REPLY:
1436 printf("Got CISCO addr_reply, ignoring\n");
1437 m_freem(m);
1438 break;
1439
1440 case CISCO_KEEPALIVE_REQ:
1441 SCA_DPRINTF(SCA_DEBUG_CISCO,
1442 ("Received KA, mseq %d,"
1443 " yseq %d, rel 0x%04x, t0"
1444 " %04x, t1 %04x\n",
1445 ntohl(cisco->par1), ntohl(cisco->par2),
1446 ntohs(cisco->rel), ntohs(cisco->time0),
1447 ntohs(cisco->time1)));
1448
1449 scp->cka_lastrx = ntohl(cisco->par1);
1450 scp->cka_lasttx++;
1451
1452 /*
1453 * schedule the transmit right here.
1454 */
1455 ncisco->par2 = cisco->par1;
1456 ncisco->par1 = htonl(scp->cka_lasttx);
1457 ncisco->time0 = htons((u_int16_t)(t >> 16));
1458 ncisco->time1 = htons((u_int16_t)(t & 0x0000ffff));
1459
1460 ifq = &scp->linkq;
1461 if (IF_QFULL(ifq)) {
1462 IF_DROP(ifq);
1463 m_freem(m);
1464 return;
1465 }
1466 IF_ENQUEUE(ifq, m);
1467
1468 sca_start(&scp->sp_if);
1469
1470 break;
1471
1472 default:
1473 m_freem(m);
1474 SCA_DPRINTF(SCA_DEBUG_CISCO,
1475 ("Unknown CISCO keepalive protocol 0x%04x\n",
1476 ntohl(cisco->type)));
1477 return;
1478 }
1479
1480 break;
1481
1482 default:
1483 SCA_DPRINTF(SCA_DEBUG_RX,
1484 ("Unknown/unexpected ethertype 0x%04x\n",
1485 ntohs(hdlc->protocol)));
1486 }
1487 }
1488
1489 #if SCA_DEBUG_LEVEL > 0
1490 /*
1491 * do a hex dump of the packet received into descriptor "desc" with
1492 * data buffer "p"
1493 */
1494 static void
1495 sca_frame_print(sca_port_t *scp, sca_desc_t *desc, u_int8_t *p)
1496 {
1497 int i;
1498 int nothing_yet = 1;
1499
1500 printf("descriptor va %p: cp 0x%x bpb 0x%0x bp 0x%0x stat 0x%0x len %d\n",
1501 desc, desc->cp, desc->bpb, desc->bp, desc->stat, desc->len);
1502
1503 for (i = 0 ; i < desc->len ; i++) {
1504 if (nothing_yet == 1 && *p == 0) {
1505 p++;
1506 continue;
1507 }
1508 nothing_yet = 0;
1509 if (i % 16 == 0)
1510 printf("\n");
1511 printf("%02x ", *p++);
1512 }
1513
1514 if (i % 16 != 1)
1515 printf("\n");
1516 }
1517 #endif
1518
1519 /*
1520 * skip all frames before the descriptor index "indx" -- we do this by
1521 * moving the rxstart pointer to the index following this one, and
1522 * setting the end descriptor to this index.
1523 */
1524 static void
1525 sca_frame_skip(sca_port_t *scp, int indx)
1526 {
1527 u_int32_t desc_p;
1528
1529 scp->rxstart++;
1530 if (scp->rxstart == SCA_NrxBUFS)
1531 scp->rxstart = 0;
1532
1533 desc_p = scp->rxdesc_p * sizeof(sca_desc_t) * indx;
1534 dmac_write_2(scp, SCA_EDAL0,
1535 (u_int16_t)(desc_p & 0x0000ffff));
1536 }
1537
1538 /*
1539 * set a port to the "up" state
1540 */
1541 static void
1542 sca_port_up(sca_port_t *scp)
1543 {
1544 struct sca_softc *sc = scp->sca;
1545
1546 /*
1547 * reset things
1548 */
1549 #if 0
1550 msci_write_1(scp, SCA_CMD0, SCA_CMD_TXRESET);
1551 msci_write_1(scp, SCA_CMD0, SCA_CMD_RXRESET);
1552 #endif
1553 /*
1554 * clear in-use flag
1555 */
1556 scp->sp_if.if_flags &= ~IFF_OACTIVE;
1557
1558 /*
1559 * raise DTR
1560 */
1561 sc->dtr_callback(sc->dtr_aux, scp->sp_port, 1);
1562
1563 /*
1564 * raise RTS
1565 */
1566 msci_write_1(scp, SCA_CTL0,
1567 msci_read_1(scp, SCA_CTL0) & ~SCA_CTL_RTS);
1568
1569 /*
1570 * enable interrupts
1571 */
1572 if (scp->sp_port == 0) {
1573 sca_write_1(sc, SCA_IER0, sca_read_1(sc, SCA_IER0) | 0x0f);
1574 sca_write_1(sc, SCA_IER1, sca_read_1(sc, SCA_IER1) | 0x0f);
1575 } else {
1576 sca_write_1(sc, SCA_IER0, sca_read_1(sc, SCA_IER0) | 0xf0);
1577 sca_write_1(sc, SCA_IER1, sca_read_1(sc, SCA_IER1) | 0xf0);
1578 }
1579
1580 /*
1581 * enable transmit and receive
1582 */
1583 msci_write_1(scp, SCA_CMD0, SCA_CMD_TXENABLE);
1584 msci_write_1(scp, SCA_CMD0, SCA_CMD_RXENABLE);
1585
1586 /*
1587 * reset internal state
1588 */
1589 scp->txinuse = 0;
1590 scp->txcur = 0;
1591 scp->cka_lasttx = time.tv_usec;
1592 scp->cka_lastrx = 0;
1593 }
1594
1595 /*
1596 * set a port to the "down" state
1597 */
1598 static void
1599 sca_port_down(sca_port_t *scp)
1600 {
1601 struct sca_softc *sc = scp->sca;
1602
1603 /*
1604 * lower DTR
1605 */
1606 sc->dtr_callback(sc->dtr_aux, scp->sp_port, 0);
1607
1608 /*
1609 * lower RTS
1610 */
1611 msci_write_1(scp, SCA_CTL0,
1612 msci_read_1(scp, SCA_CTL0) | SCA_CTL_RTS);
1613
1614 /*
1615 * disable interrupts
1616 */
1617 if (scp->sp_port == 0) {
1618 sca_write_1(sc, SCA_IER0, sca_read_1(sc, SCA_IER0) & 0xf0);
1619 sca_write_1(sc, SCA_IER1, sca_read_1(sc, SCA_IER1) & 0xf0);
1620 } else {
1621 sca_write_1(sc, SCA_IER0, sca_read_1(sc, SCA_IER0) & 0x0f);
1622 sca_write_1(sc, SCA_IER1, sca_read_1(sc, SCA_IER1) & 0x0f);
1623 }
1624
1625 /*
1626 * disable transmit and receive
1627 */
1628 msci_write_1(scp, SCA_CMD0, SCA_CMD_RXDISABLE);
1629 msci_write_1(scp, SCA_CMD0, SCA_CMD_TXDISABLE);
1630
1631 /*
1632 * no, we're not in use anymore
1633 */
1634 scp->sp_if.if_flags &= ~IFF_OACTIVE;
1635 }
1636
1637 /*
1638 * disable all DMA and interrupts for all ports at once.
1639 */
1640 void
1641 sca_shutdown(struct sca_softc *sca)
1642 {
1643 /*
1644 * disable DMA and interrupts
1645 */
1646 sca_write_1(sca, SCA_DMER, 0);
1647 sca_write_1(sca, SCA_IER0, 0);
1648 sca_write_1(sca, SCA_IER1, 0);
1649 }
1650
1651 /*
1652 * If there are packets to transmit, start the transmit DMA logic.
1653 */
1654 static void
1655 sca_port_starttx(sca_port_t *scp)
1656 {
1657 struct sca_softc *sc;
1658 u_int32_t startdesc_p, enddesc_p;
1659 int enddesc;
1660
1661 sc = scp->sca;
1662
1663 if (((scp->sp_if.if_flags & IFF_OACTIVE) == IFF_OACTIVE)
1664 || scp->txinuse == 0)
1665 return;
1666 scp->sp_if.if_flags |= IFF_OACTIVE;
1667
1668 /*
1669 * We have something to do, since we have at least one packet
1670 * waiting, and we are not already marked as active.
1671 */
1672 enddesc = scp->txcur;
1673 enddesc++;
1674 if (enddesc == SCA_NtxBUFS)
1675 enddesc = 0;
1676
1677 startdesc_p = scp->txdesc_p;
1678 enddesc_p = scp->txdesc_p + sizeof(sca_desc_t) * enddesc;
1679
1680 dmac_write_2(scp, SCA_EDAL1, (u_int16_t)(enddesc_p & 0x0000ffff));
1681 dmac_write_2(scp, SCA_CDAL1,
1682 (u_int16_t)(startdesc_p & 0x0000ffff));
1683
1684 /*
1685 * enable the DMA
1686 */
1687 dmac_write_1(scp, SCA_DSR1, SCA_DSR_DE);
1688 }
1689
1690 /*
1691 * allocate an mbuf at least long enough to hold "len" bytes.
1692 * If "p" is non-NULL, copy "len" bytes from it into the new mbuf,
1693 * otherwise let the caller handle copying the data in.
1694 */
1695 static struct mbuf *
1696 sca_mbuf_alloc(caddr_t p, u_int len)
1697 {
1698 struct mbuf *m;
1699
1700 /*
1701 * allocate an mbuf and copy the important bits of data
1702 * into it. If the packet won't fit in the header,
1703 * allocate a cluster for it and store it there.
1704 */
1705 MGETHDR(m, M_DONTWAIT, MT_DATA);
1706 if (m == NULL)
1707 return NULL;
1708 if (len > MHLEN) {
1709 if (len > MCLBYTES) {
1710 m_freem(m);
1711 return NULL;
1712 }
1713 MCLGET(m, M_DONTWAIT);
1714 if ((m->m_flags & M_EXT) == 0) {
1715 m_freem(m);
1716 return NULL;
1717 }
1718 }
1719 if (p != NULL)
1720 bcopy(p, mtod(m, caddr_t), len);
1721 m->m_len = len;
1722 m->m_pkthdr.len = len;
1723
1724 return (m);
1725 }
1726