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