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