kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / netif / cm / smc90cx6.c
1 /*      $NetBSD: smc90cx6.c,v 1.38 2001/07/07 15:57:53 thorpej Exp $ */
2 /*      $FreeBSD: src/sys/dev/cm/smc90cx6.c,v 1.1.2.3 2003/02/05 18:42:14 fjoe Exp $ */
3 /*      $DragonFly: src/sys/dev/netif/cm/Attic/smc90cx6.c,v 1.3 2003/08/07 21:17:00 dillon Exp $ */
4
5 /*-
6  * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Ignatios Souvatzis.
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. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 /*
42  * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
43  * compatibility mode) boards
44  */
45
46 /* #define CMSOFTCOPY */
47 #define CMRETRANSMIT /**/
48 /* #define CM_DEBUG */
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/sockio.h>
53 #include <sys/mbuf.h>
54 #include <sys/module.h>
55 #include <sys/kernel.h>
56 #include <sys/socket.h>
57 #include <sys/syslog.h>
58 #include <sys/bus.h>
59
60 #include <machine/bus.h>
61 #include <sys/rman.h>
62 #include <machine/resource.h>
63
64 #if __FreeBSD_version < 500000
65 #include <machine/clock.h>
66 #endif
67
68 #include <net/if.h>
69 #include <net/if_dl.h>
70 #include <net/if_types.h>
71 #include <net/if_arc.h>
72
73 #include "smc90cx6reg.h"
74 #include "smc90cx6var.h"
75
76 MODULE_DEPEND(if_cm, arcnet, 1, 1, 1);
77
78 /* these should be elsewhere */
79
80 #define ARC_MIN_LEN 1
81 #define ARC_MIN_FORBID_LEN 254
82 #define ARC_MAX_FORBID_LEN 256
83 #define ARC_MAX_LEN 508
84 #define ARC_ADDR_LEN 1
85
86 /* for watchdog timer. This should be more than enough. */
87 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
88
89 /* short notation */
90
91 #define GETREG(off)                                                     \
92         bus_space_read_1(rman_get_bustag((sc)->port_res),               \
93                          rman_get_bushandle((sc)->port_res),            \
94                          (off))
95 #define PUTREG(off, value)                                              \
96         bus_space_write_1(rman_get_bustag((sc)->port_res),              \
97                           rman_get_bushandle((sc)->port_res),           \
98                           (off), (value))
99 #define GETMEM(off)                                                     \
100         bus_space_read_1(rman_get_bustag((sc)->mem_res),                \
101                          rman_get_bushandle((sc)->mem_res),             \
102                          (off))
103 #define PUTMEM(off, value)                                              \
104         bus_space_write_1(rman_get_bustag((sc)->mem_res),               \
105                           rman_get_bushandle((sc)->mem_res),            \
106                           (off), (value))
107
108 devclass_t cm_devclass;
109
110 /*
111  * This currently uses 2 bufs for tx, 2 for rx
112  *
113  * New rx protocol:
114  *
115  * rx has a fillcount variable. If fillcount > (NRXBUF-1),
116  * rx can be switched off from rx hard int.
117  * Else rx is restarted on the other receiver.
118  * rx soft int counts down. if it is == (NRXBUF-1), it restarts
119  * the receiver.
120  * To ensure packet ordering (we need that for 1201 later), we have a counter
121  * which is incremented modulo 256 on each receive and a per buffer
122  * variable, which is set to the counter on filling. The soft int can
123  * compare both values to determine the older packet.
124  *
125  * Transmit direction:
126  *
127  * cm_start checks tx_fillcount
128  * case 2: return
129  *
130  * else fill tx_act ^ 1 && inc tx_fillcount
131  *
132  * check tx_fillcount again.
133  * case 2: set IFF_OACTIVE to stop arc_output from filling us.
134  * case 1: start tx
135  *
136  * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
137  * case 1: start tx on tx_act ^ 1, softcall cm_start
138  * case 0: softcall cm_start
139  *
140  * #define fill(i) get mbuf && copy mbuf to chip(i)
141  */
142
143 void    cm_init __P((void *));
144 void    cm_reset __P((struct cm_softc *));
145 void    cm_start __P((struct ifnet *));
146 int     cm_ioctl __P((struct ifnet *, unsigned long, caddr_t));
147 void    cm_watchdog __P((struct ifnet *));
148 void    cm_srint __P((void *vsc));
149 static  void cm_tint __P((struct cm_softc *, int));
150 void    cm_reconwatch(void *);
151
152 int
153 cm_probe(dev)
154         device_t dev;
155 {
156         int error;
157         struct cm_softc *sc = device_get_softc(dev);
158
159         error = cm_alloc_port(dev, 0, CM_IO_PORTS);
160         if (error)
161                 return error;
162
163         if (GETREG(CMSTAT) == 0xff)
164                 return ENXIO;
165
166         error = cm_alloc_memory(dev, 0, 0x800);
167         if (error)
168                 return error;
169
170         return 0;
171 }
172
173 /*
174  * Allocate a port resource with the given resource id.
175  */
176 int
177 cm_alloc_port(dev, rid, size)
178         device_t dev;
179         int rid;
180         int size;
181 {
182         struct cm_softc *sc = device_get_softc(dev);
183         struct resource *res;
184
185         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
186                                  0ul, ~0ul, size, RF_ACTIVE);
187         if (res) {
188                 sc->port_rid = rid;
189                 sc->port_res = res;
190                 sc->port_used = size;
191                 return (0);
192         } else {
193                 return (ENOENT);
194         }
195 }
196
197 /*
198  * Allocate a memory resource with the given resource id.
199  */
200 int
201 cm_alloc_memory(dev, rid, size)
202         device_t dev;
203         int rid;
204         int size;
205 {
206         struct cm_softc *sc = device_get_softc(dev);
207         struct resource *res;
208
209         res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
210                                  0ul, ~0ul, size, RF_ACTIVE);
211         if (res) {
212                 sc->mem_rid = rid;
213                 sc->mem_res = res;
214                 sc->mem_used = size;
215                 return (0);
216         } else {
217                 return (ENOENT);
218         }
219 }
220
221 /*
222  * Allocate an irq resource with the given resource id.
223  */
224 int
225 cm_alloc_irq(dev, rid)
226         device_t dev;
227         int rid;
228 {
229         struct cm_softc *sc = device_get_softc(dev);
230         struct resource *res;
231
232         res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
233                                  0ul, ~0ul, 1, RF_ACTIVE);
234         if (res) {
235                 sc->irq_rid = rid;
236                 sc->irq_res = res;
237                 return (0);
238         } else {
239                 return (ENOENT);
240         }
241 }
242
243 /*
244  * Release all resources
245  */
246 void
247 cm_release_resources(dev)
248         device_t dev;
249 {
250         struct cm_softc *sc = device_get_softc(dev);
251
252         if (sc->port_res) {
253                 bus_deactivate_resource(dev, SYS_RES_IOPORT,
254                                      sc->port_rid, sc->port_res);
255                 bus_release_resource(dev, SYS_RES_IOPORT,
256                                      sc->port_rid, sc->port_res);
257                 sc->port_res = 0;
258         }
259         if (sc->mem_res) {
260                 bus_deactivate_resource(dev, SYS_RES_MEMORY,
261                                      sc->mem_rid, sc->mem_res);
262                 bus_release_resource(dev, SYS_RES_MEMORY,
263                                      sc->mem_rid, sc->mem_res);
264                 sc->mem_res = 0;
265         }
266         if (sc->irq_res) {
267                 bus_deactivate_resource(dev, SYS_RES_IRQ,
268                                      sc->irq_rid, sc->irq_res);
269                 bus_release_resource(dev, SYS_RES_IRQ,
270                                      sc->irq_rid, sc->irq_res);
271                 sc->irq_res = 0;
272         }
273 }
274
275 int
276 cm_attach(sc, unit)
277         struct cm_softc *sc;
278         int unit;
279 {
280         struct ifnet *ifp = &sc->sc_arccom.ac_if;
281         int s;
282         u_int8_t linkaddress;
283
284         s = splhigh();
285
286         /*
287          * read the arcnet address from the board
288          */
289
290         GETREG(CMRESET);
291         do {
292                 DELAY(200);
293         } while (!(GETREG(CMSTAT) & CM_POR));
294
295         linkaddress = GETMEM(CMMACOFF);
296
297         /* clear the int mask... */
298
299         sc->sc_intmask = 0;
300         PUTREG(CMSTAT, 0);
301
302         PUTREG(CMCMD, CM_CONF(CONF_LONG));
303         PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
304         sc->sc_recontime = sc->sc_reconcount = 0;
305
306         /* and reenable kernel int level */
307         splx(s);
308
309         /*
310          * set interface to stopped condition (reset)
311          */
312         cm_stop(sc);
313
314         if (!ifp->if_name) {
315                 ifp->if_softc = sc;
316                 ifp->if_unit = unit;
317                 ifp->if_name = "cm";
318                 ifp->if_output = arc_output;
319                 ifp->if_start = cm_start;
320                 ifp->if_ioctl = cm_ioctl;
321                 ifp->if_watchdog  = cm_watchdog;
322                 ifp->if_init = cm_init;
323                 /* XXX IFQ_SET_READY(&ifp->if_snd); */
324                 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
325                 ifp->if_timer = 0;
326                 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
327
328                 arc_ifattach(ifp, linkaddress);
329
330 #ifdef CMSOFTCOPY
331                 sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
332                 sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
333                         (void (*) __P((void *)))cm_start, ifp);
334 #endif
335
336 #if __FreeBSD_version < 500000
337                 callout_init(&sc->sc_recon_ch);
338 #else
339                 callout_init(&sc->sc_recon_ch, 0);
340 #endif
341         }
342
343         printf("%s%d: link addr 0x%02x (%d)\n",
344                ifp->if_name, ifp->if_unit, linkaddress, linkaddress);
345         return 0;
346 }
347
348 /*
349  * Initialize device
350  *
351  */
352 void
353 cm_init(xsc)
354         void *xsc;
355 {
356         struct cm_softc *sc = (struct cm_softc *)xsc;
357         struct ifnet *ifp;
358         int s;
359
360         ifp = &sc->sc_arccom.ac_if;
361
362         if ((ifp->if_flags & IFF_RUNNING) == 0) {
363                 s = splimp();
364                 ifp->if_flags |= IFF_RUNNING;
365                 cm_reset(sc);
366                 cm_start(ifp);
367                 splx(s);
368         }
369 }
370
371 /*
372  * Reset the interface...
373  *
374  * this assumes that it is called inside a critical section...
375  *
376  */
377 void
378 cm_reset(sc)
379         struct cm_softc *sc;
380 {
381         struct ifnet *ifp;
382         int linkaddress;
383
384         ifp = &sc->sc_arccom.ac_if;
385
386 #ifdef CM_DEBUG
387         printf("%s%d: reset\n", ifp->if_name, ifp->if_unit);
388 #endif
389         /* stop and restart hardware */
390
391         GETREG(CMRESET);
392         do {
393                 DELAY(200);
394         } while (!(GETREG(CMSTAT) & CM_POR));
395
396         linkaddress = GETMEM(CMMACOFF);
397
398 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
399         printf("%s%d: reset: card reset, link addr = 0x%02x (%d)\n",
400             ifp->if_name, ifp->if_unit, linkaddress, linkaddress);
401 #endif
402
403         /* tell the routing level about the (possibly changed) link address */
404         arc_storelladdr(ifp, linkaddress);
405         arc_frag_init(ifp);
406
407         /* POR is NMI, but we need it below: */
408         sc->sc_intmask = CM_RECON|CM_POR;
409         PUTREG(CMSTAT, sc->sc_intmask);
410         PUTREG(CMCMD, CM_CONF(CONF_LONG));
411
412 #ifdef CM_DEBUG
413         printf("%s%d: reset: chip configured, status=0x%02x\n",
414             ifp->if_name, ifp->if_unit, GETREG(CMSTAT));
415 #endif
416         PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
417
418 #ifdef CM_DEBUG
419         printf("%s%d: reset: bits cleared, status=0x%02x\n",
420             ifp->if_name, ifp->if_unit, GETREG(CMSTAT));
421 #endif
422
423         sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
424
425         /* start receiver */
426
427         sc->sc_intmask  |= CM_RI;
428         sc->sc_rx_fillcount = 0;
429         sc->sc_rx_act = 2;
430
431         PUTREG(CMCMD, CM_RXBC(2));
432         PUTREG(CMSTAT, sc->sc_intmask);
433
434 #ifdef CM_DEBUG
435         printf("%s%d: reset: started receiver, status=0x%02x\n",
436             ifp->if_name, ifp->if_unit, GETREG(CMSTAT));
437 #endif
438
439         /* and init transmitter status */
440         sc->sc_tx_act = 0;
441         sc->sc_tx_fillcount = 0;
442
443         ifp->if_flags |= IFF_RUNNING;
444         ifp->if_flags &= ~IFF_OACTIVE;
445
446         cm_start(ifp);
447 }
448
449 /*
450  * Take interface offline
451  */
452 void
453 cm_stop(sc)
454         struct cm_softc *sc;
455 {
456         /* Stop the interrupts */
457         PUTREG(CMSTAT, 0);
458
459         /* Stop the interface */
460         GETREG(CMRESET);
461
462         /* Stop watchdog timer */
463         sc->sc_arccom.ac_if.if_timer = 0;
464 }
465
466 /*
467  * Start output on interface. Get another datagram to send
468  * off the interface queue, and copy it to the
469  * interface becore starting the output
470  *
471  * this assumes that it is called inside a critical section...
472  * XXX hm... does it still?
473  *
474  */
475 void
476 cm_start(ifp)
477         struct ifnet *ifp;
478 {
479         struct cm_softc *sc = ifp->if_softc;
480         struct mbuf *m,*mp;
481
482         int cm_ram_ptr;
483         int len, tlen, offset, s, buffer;
484 #ifdef CMTIMINGS
485         u_long copystart, lencopy, perbyte;
486 #endif
487
488 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
489         printf("%s%d: start(%p)\n", ifp->if_name, ifp->if_unit, ifp);
490 #endif
491
492         if ((ifp->if_flags & IFF_RUNNING) == 0)
493                 return;
494
495         s = splimp();
496
497         if (sc->sc_tx_fillcount >= 2) {
498                 splx(s);
499                 return;
500         }
501
502         m = arc_frag_next(ifp);
503         buffer = sc->sc_tx_act ^ 1;
504
505         splx(s);
506
507         if (m == 0)
508                 return;
509
510 #ifdef CM_DEBUG
511         if (m->m_len < ARC_HDRLEN)
512                 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
513         printf("%s%d: start: filling %d from %d to %d type %d\n",
514             ifp->if_name, ifp->if_unit, buffer, mtod(m, u_char *)[0],
515             mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
516 #else
517         if (m->m_len < 2)
518                 m = m_pullup(m, 2);
519 #endif
520         cm_ram_ptr = buffer * 512;
521
522         if (m == 0)
523                 return;
524
525         /* write the addresses to RAM and throw them away */
526
527         /*
528          * Hardware does this: Yet Another Microsecond Saved.
529          * (btw, timing code says usually 2 microseconds)
530          * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
531          */
532
533         PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
534         m_adj(m, 2);
535
536         /* get total length left at this point */
537         tlen = m->m_pkthdr.len;
538         if (tlen < ARC_MIN_FORBID_LEN) {
539                 offset = 256 - tlen;
540                 PUTMEM(cm_ram_ptr + 2, offset);
541         } else {
542                 PUTMEM(cm_ram_ptr + 2, 0);
543                 if (tlen <= ARC_MAX_FORBID_LEN)
544                         offset = 255;           /* !!! */
545                 else {
546                         if (tlen > ARC_MAX_LEN)
547                                 tlen = ARC_MAX_LEN;
548                         offset = 512 - tlen;
549                 }
550                 PUTMEM(cm_ram_ptr + 3, offset);
551
552         }
553         cm_ram_ptr += offset;
554
555         /* lets loop through the mbuf chain */
556
557         for (mp = m; mp; mp = mp->m_next) {
558                 if ((len = mp->m_len)) {                /* YAMS */
559                         bus_space_write_region_1(
560                             rman_get_bustag(sc->mem_res),
561                             rman_get_bushandle(sc->mem_res),
562                             cm_ram_ptr, mtod(mp, caddr_t), len);
563
564                         cm_ram_ptr += len;
565                 }
566         }
567
568         sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
569         sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
570
571         /* actually transmit the packet */
572         s = splimp();
573
574         if (++sc->sc_tx_fillcount > 1) {
575                 /*
576                  * We are filled up to the rim. No more bufs for the moment,
577                  * please.
578                  */
579                 ifp->if_flags |= IFF_OACTIVE;
580         } else {
581 #ifdef CM_DEBUG
582                 printf("%s%d: start: starting transmitter on buffer %d\n",
583                     ifp->if_name, ifp->if_unit, buffer);
584 #endif
585                 /* Transmitter was off, start it */
586                 sc->sc_tx_act = buffer;
587
588                 /*
589                  * We still can accept another buf, so don't:
590                  * ifp->if_flags |= IFF_OACTIVE;
591                  */
592                 sc->sc_intmask |= CM_TA;
593                 PUTREG(CMCMD, CM_TX(buffer));
594                 PUTREG(CMSTAT, sc->sc_intmask);
595
596                 sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
597         }
598         splx(s);
599         m_freem(m);
600
601         /*
602          * After 10 times reading the docs, I realized
603          * that in the case the receiver NAKs the buffer request,
604          * the hardware retries till shutdown.
605          * This is integrated now in the code above.
606          */
607
608         return;
609 }
610
611 /*
612  * Arcnet interface receiver soft interrupt:
613  * get the stuff out of any filled buffer we find.
614  */
615 void
616 cm_srint(vsc)
617         void *vsc;
618 {
619         struct cm_softc *sc = (struct cm_softc *)vsc;
620         int buffer, len, offset, s, type;
621         int cm_ram_ptr;
622         struct mbuf *m;
623         struct arc_header *ah;
624         struct ifnet *ifp;
625
626         ifp = &sc->sc_arccom.ac_if;
627
628         s = splimp();
629         buffer = sc->sc_rx_act ^ 1;
630         splx(s);
631
632         /* Allocate header mbuf */
633         MGETHDR(m, M_DONTWAIT, MT_DATA);
634
635         if (m == 0) {
636                 /*
637                  * in case s.th. goes wrong with mem, drop it
638                  * to make sure the receiver can be started again
639                  * count it as input error (we dont have any other
640                  * detectable)
641                  */
642                 ifp->if_ierrors++;
643                 goto cleanup;
644         }
645
646         m->m_pkthdr.rcvif = ifp;
647
648         /*
649          * Align so that IP packet will be longword aligned. Here we
650          * assume that m_data of new packet is longword aligned.
651          * When implementing PHDS, we might have to change it to 2,
652          * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
653          */
654
655         cm_ram_ptr = buffer * 512;
656         offset = GETMEM(cm_ram_ptr + 2);
657         if (offset)
658                 len = 256 - offset;
659         else {
660                 offset = GETMEM(cm_ram_ptr + 3);
661                 len = 512 - offset;
662         }
663
664         /*
665          * first +2 bytes for align fixup below
666          * second +2 bytes are for src/dst addresses
667          */
668         if ((len + 2 + 2) > MHLEN) {
669                 /* attach an mbuf cluster */
670                 MCLGET(m, M_DONTWAIT);
671
672                 /* Insist on getting a cluster */
673                 if ((m->m_flags & M_EXT) == 0) {
674                         ifp->if_ierrors++;
675                         goto cleanup;
676                 }
677         }
678
679         if (m == 0) {
680                 ifp->if_ierrors++;
681                 goto cleanup;
682         }
683
684         type = GETMEM(cm_ram_ptr + offset);
685         m->m_data += 1 + arc_isphds(type);
686         /* mbuf filled with ARCnet addresses */
687         m->m_pkthdr.len = m->m_len = len + 2;
688
689         ah = mtod(m, struct arc_header *);
690         ah->arc_shost = GETMEM(cm_ram_ptr + 0);
691         ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
692
693         bus_space_read_region_1(
694             rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
695             cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
696
697         arc_input(ifp, m);
698
699         m = NULL;
700         ifp->if_ipackets++;
701
702 cleanup:
703
704         if (m != NULL)
705                 m_freem(m);
706
707         /* mark buffer as invalid by source id 0 */
708         PUTMEM(buffer << 9, 0);
709         s = splimp();
710
711         if (--sc->sc_rx_fillcount == 2 - 1) {
712
713                 /* was off, restart it on buffer just emptied */
714                 sc->sc_rx_act = buffer;
715                 sc->sc_intmask |= CM_RI;
716
717                 /* this also clears the RI flag interupt: */
718                 PUTREG(CMCMD, CM_RXBC(buffer));
719                 PUTREG(CMSTAT, sc->sc_intmask);
720
721 #ifdef CM_DEBUG
722                 printf("%s%d: srint: restarted rx on buf %d\n",
723                     ifp->if_name, ifp->if_unit, buffer);
724 #endif
725         }
726         splx(s);
727 }
728
729 __inline static void
730 cm_tint(sc, isr)
731         struct cm_softc *sc;
732         int isr;
733 {
734         struct ifnet *ifp;
735
736         int buffer;
737 #ifdef CMTIMINGS
738         int clknow;
739 #endif
740
741         ifp = &(sc->sc_arccom.ac_if);
742         buffer = sc->sc_tx_act;
743
744         /*
745          * retransmit code:
746          * Normal situtations first for fast path:
747          * If acknowledgement received ok or broadcast, we're ok.
748          * else if
749          */
750
751         if (isr & CM_TMA || sc->sc_broadcast[buffer])
752                 sc->sc_arccom.ac_if.if_opackets++;
753 #ifdef CMRETRANSMIT
754         else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
755             && --sc->sc_retransmits[buffer] > 0) {
756                 /* retransmit same buffer */
757                 PUTREG(CMCMD, CM_TX(buffer));
758                 return;
759         }
760 #endif
761         else
762                 ifp->if_oerrors++;
763
764
765         /* We know we can accept another buffer at this point. */
766         ifp->if_flags &= ~IFF_OACTIVE;
767
768         if (--sc->sc_tx_fillcount > 0) {
769
770                 /*
771                  * start tx on other buffer.
772                  * This also clears the int flag
773                  */
774                 buffer ^= 1;
775                 sc->sc_tx_act = buffer;
776
777                 /*
778                  * already given:
779                  * sc->sc_intmask |= CM_TA;
780                  * PUTREG(CMSTAT, sc->sc_intmask);
781                  */
782                 PUTREG(CMCMD, CM_TX(buffer));
783                 /* init watchdog timer */
784                 ifp->if_timer = ARCTIMEOUT;
785
786 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
787                 printf("%s%d: tint: starting tx on buffer %d, status 0x%02x\n",
788                     ifp->if_name, ifp->if_unit, buffer, GETREG(CMSTAT));
789 #endif
790         } else {
791                 /* have to disable TX interrupt */
792                 sc->sc_intmask &= ~CM_TA;
793                 PUTREG(CMSTAT, sc->sc_intmask);
794                 /* ... and watchdog timer */
795                 ifp->if_timer = 0;
796
797 #ifdef CM_DEBUG
798                 printf("%s%d: tint: no more buffers to send, status 0x%02x\n",
799                     ifp->if_name, ifp->if_unit, GETREG(CMSTAT));
800 #endif
801         }
802
803         /* XXXX TODO */
804 #ifdef CMSOFTCOPY
805         /* schedule soft int to fill a new buffer for us */
806         softintr_schedule(sc->sc_txcookie);
807 #else
808         /* call it directly */
809         cm_start(ifp);
810 #endif
811 }
812
813 /*
814  * Our interrupt routine
815  */
816 void
817 cmintr(arg)
818         void *arg;
819 {
820         struct cm_softc *sc = arg;
821         struct ifnet *ifp = &sc->sc_arccom.ac_if;
822
823         u_char isr, maskedisr;
824         int buffer;
825         u_long newsec;
826
827         isr = GETREG(CMSTAT);
828         maskedisr = isr & sc->sc_intmask;
829         if (!maskedisr)
830                 return;
831         do {
832
833 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
834                 printf("%s%d: intr: status 0x%02x, intmask 0x%02x\n",
835                     ifp->if_name, ifp->if_unit, isr, sc->sc_intmask);
836 #endif
837
838                 if (maskedisr & CM_POR) {
839                         /*
840                          * XXX We should never see this. Don't bother to store
841                          * the address.
842                          * sc->sc_arccom.ac_anaddr = GETMEM(CMMACOFF);
843                          */
844                         PUTREG(CMCMD, CM_CLR(CLR_POR));
845                         log(LOG_WARNING,
846                             "%s%d: intr: got spurious power on reset int\n",
847                             ifp->if_name, ifp->if_unit);
848                 }
849
850                 if (maskedisr & CM_RECON) {
851                         /*
852                          * we dont need to:
853                          * PUTREG(CMCMD, CM_CONF(CONF_LONG));
854                          */
855                         PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
856                         sc->sc_arccom.ac_if.if_collisions++;
857
858                         /*
859                          * If less than 2 seconds per reconfig:
860                          *      If ARC_EXCESSIVE_RECONFIGS
861                          *      since last burst, complain and set treshold for
862                          *      warnings to ARC_EXCESSIVE_RECONS_REWARN.
863                          *
864                          * This allows for, e.g., new stations on the cable, or
865                          * cable switching as long as it is over after
866                          * (normally) 16 seconds.
867                          *
868                          * XXX TODO: check timeout bits in status word and
869                          * double time if necessary.
870                          */
871
872                         callout_stop(&sc->sc_recon_ch);
873                         newsec = time_second;
874                         if ((newsec - sc->sc_recontime <= 2) &&
875                             (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
876                                 log(LOG_WARNING,
877                                     "%s%d: excessive token losses, "
878                                     "cable problem?\n",
879                                     ifp->if_name, ifp->if_unit);
880                         }
881                         sc->sc_recontime = newsec;
882                         callout_reset(&sc->sc_recon_ch, 15 * hz,
883                             cm_reconwatch, (void *)sc);
884                 }
885
886                 if (maskedisr & CM_RI) {
887 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
888                         printf("%s%d: intr: hard rint, act %d\n",
889                             ifp->if_name, ifp->if_unit, sc->sc_rx_act);
890 #endif
891
892                         buffer = sc->sc_rx_act;
893                         /* look if buffer is marked invalid: */
894                         if (GETMEM(buffer * 512) == 0) {
895                                 /*
896                                  * invalid marked buffer (or illegally
897                                  * configured sender)
898                                  */
899                                 log(LOG_WARNING,
900                                     "%s%d: spurious RX interupt or sender 0 "
901                                     " (ignored)\n", ifp->if_name, ifp->if_unit);
902                                 /*
903                                  * restart receiver on same buffer.
904                                  * XXX maybe better reset interface?
905                                  */
906                                 PUTREG(CMCMD, CM_RXBC(buffer));
907                         } else {
908                                 if (++sc->sc_rx_fillcount > 1) {
909                                         sc->sc_intmask &= ~CM_RI;
910                                         PUTREG(CMSTAT, sc->sc_intmask);
911                                 } else {
912                                         buffer ^= 1;
913                                         sc->sc_rx_act = buffer;
914
915                                         /*
916                                          * Start receiver on other receive
917                                          * buffer. This also clears the RI
918                                          * interupt flag.
919                                          */
920                                         PUTREG(CMCMD, CM_RXBC(buffer));
921                                         /* in RX intr, so mask is ok for RX */
922
923 #ifdef CM_DEBUG
924                                         printf("%s%d: strt rx for buf %d, "
925                                             "stat 0x%02x\n",
926                                             ifp->if_name, ifp->if_unit,
927                                             sc->sc_rx_act, GETREG(CMSTAT));
928 #endif
929                                 }
930
931 #ifdef CMSOFTCOPY
932                                 /*
933                                  * this one starts a soft int to copy out
934                                  * of the hw
935                                  */
936                                 softintr_schedule(sc->sc_rxcookie);
937 #else
938                                 /* this one does the copy here */
939                                 cm_srint(sc);
940 #endif
941                         }
942                 }
943                 if (maskedisr & CM_TA) {
944                         cm_tint(sc, isr);
945                 }
946                 isr = GETREG(CMSTAT);
947                 maskedisr = isr & sc->sc_intmask;
948         } while (maskedisr);
949 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
950         printf("%s%d: intr (exit): status 0x%02x, intmask 0x%02x\n",
951             ifp->if_name, ifp->if_unit, isr, sc->sc_intmask);
952 #endif
953 }
954
955 void
956 cm_reconwatch(arg)
957         void *arg;
958 {
959         struct cm_softc *sc = arg;
960         struct ifnet *ifp = &sc->sc_arccom.ac_if;
961
962         if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
963                 sc->sc_reconcount = 0;
964                 log(LOG_WARNING, "%s%d: token valid again.\n",
965                     ifp->if_name, ifp->if_unit);
966         }
967         sc->sc_reconcount = 0;
968 }
969
970
971 /*
972  * Process an ioctl request.
973  * This code needs some work - it looks pretty ugly.
974  */
975 int
976 cm_ioctl(ifp, command, data)
977         struct ifnet *ifp;
978         u_long command;
979         caddr_t data;
980 {
981         struct cm_softc *sc;
982         struct ifaddr *ifa;
983         struct ifreq *ifr;
984         int s, error;
985
986         error = 0;
987         sc = ifp->if_softc;
988         ifa = (struct ifaddr *)data;
989         ifr = (struct ifreq *)data;
990         s = splimp();
991
992 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
993         printf("%s%d: ioctl() called, cmd = 0x%lx\n",
994             ifp->if_name, ifp->if_unit, command);
995 #endif
996
997         switch (command) {
998         case SIOCSIFADDR:
999         case SIOCGIFADDR:
1000         case SIOCADDMULTI:
1001         case SIOCDELMULTI:
1002         case SIOCSIFMTU:
1003                 error = arc_ioctl(ifp, command, data);
1004                 break;
1005
1006         case SIOCSIFFLAGS:
1007                 if ((ifp->if_flags & IFF_UP) == 0 &&
1008                     (ifp->if_flags & IFF_RUNNING) != 0) {
1009                         /*
1010                          * If interface is marked down and it is running,
1011                          * then stop it.
1012                          */
1013                         cm_stop(sc);
1014                         ifp->if_flags &= ~IFF_RUNNING;
1015                 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1016                            (ifp->if_flags & IFF_RUNNING) == 0) {
1017                         /*
1018                          * If interface is marked up and it is stopped, then
1019                          * start it.
1020                          */
1021                         cm_init(sc);
1022                 }
1023                 break;
1024
1025         default:
1026                 error = EINVAL;
1027                 break;
1028         }
1029
1030         splx(s);
1031         return (error);
1032 }
1033
1034 /*
1035  * watchdog routine for transmitter.
1036  *
1037  * We need this, because else a receiver whose hardware is alive, but whose
1038  * software has not enabled the Receiver, would make our hardware wait forever
1039  * Discovered this after 20 times reading the docs.
1040  *
1041  * Only thing we do is disable transmitter. We'll get an transmit timeout,
1042  * and the int handler will have to decide not to retransmit (in case
1043  * retransmission is implemented).
1044  *
1045  * This one assumes being called inside splimp()
1046  */
1047
1048 void
1049 cm_watchdog(ifp)
1050         struct ifnet *ifp;
1051 {
1052         struct cm_softc *sc = ifp->if_softc;
1053
1054         PUTREG(CMCMD, CM_TXDIS);
1055         return;
1056 }