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