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