]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cm/smc90cx6.c
This commit was generated by cvs2svn to compensate for changes in r141098,
[FreeBSD/FreeBSD.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 #if __FreeBSD_version < 500000
66 #include <machine/clock.h>
67 #endif
68
69 #include <net/if.h>
70 #include <net/if_dl.h>
71 #include <net/if_types.h>
72 #include <net/if_arc.h>
73
74 #include <dev/cm/smc90cx6reg.h>
75 #include <dev/cm/smc90cx6var.h>
76
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);
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, device_get_name(dev), device_get_unit(dev));
316         ifp->if_output = arc_output;
317         ifp->if_start = cm_start;
318         ifp->if_ioctl = cm_ioctl;
319         ifp->if_watchdog  = cm_watchdog;
320         ifp->if_init = cm_init;
321         /* XXX IFQ_SET_READY(&ifp->if_snd); */
322         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
323         ifp->if_timer = 0;
324         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NEEDSGIANT;
325
326         arc_ifattach(ifp, linkaddress);
327
328 #ifdef CMSOFTCOPY
329         sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
330         sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
331                 (void (*)(void *))cm_start, ifp);
332 #endif
333
334 #if __FreeBSD_version < 500000
335         callout_init(&sc->sc_recon_ch);
336 #else
337         callout_init(&sc->sc_recon_ch, 0);
338 #endif
339
340         if_printf(ifp, "link addr 0x%02x (%d)\n", linkaddress, linkaddress);
341         return 0;
342 }
343
344 /*
345  * Initialize device
346  *
347  */
348 void
349 cm_init(xsc)
350         void *xsc;
351 {
352         struct cm_softc *sc = (struct cm_softc *)xsc;
353         struct ifnet *ifp;
354         int s;
355
356         ifp = &sc->sc_arccom.ac_if;
357
358         if ((ifp->if_flags & IFF_RUNNING) == 0) {
359                 s = splimp();
360                 ifp->if_flags |= IFF_RUNNING;
361                 cm_reset(sc);
362                 cm_start(ifp);
363                 splx(s);
364         }
365 }
366
367 /*
368  * Reset the interface...
369  *
370  * this assumes that it is called inside a critical section...
371  *
372  */
373 void
374 cm_reset(sc)
375         struct cm_softc *sc;
376 {
377         struct ifnet *ifp;
378         int linkaddress;
379
380         ifp = &sc->sc_arccom.ac_if;
381
382 #ifdef CM_DEBUG
383         if_printf(ifp, "reset\n");
384 #endif
385         /* stop and restart hardware */
386
387         GETREG(CMRESET);
388         do {
389                 DELAY(200);
390         } while (!(GETREG(CMSTAT) & CM_POR));
391
392         linkaddress = GETMEM(CMMACOFF);
393
394 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
395         if_printf(ifp, "reset: card reset, link addr = 0x%02x (%d)\n",
396             linkaddress, linkaddress);
397 #endif
398
399         /* tell the routing level about the (possibly changed) link address */
400         arc_storelladdr(ifp, linkaddress);
401         arc_frag_init(ifp);
402
403         /* POR is NMI, but we need it below: */
404         sc->sc_intmask = CM_RECON|CM_POR;
405         PUTREG(CMSTAT, sc->sc_intmask);
406         PUTREG(CMCMD, CM_CONF(CONF_LONG));
407
408 #ifdef CM_DEBUG
409         if_printf(ifp, "reset: chip configured, status=0x%02x\n",
410             GETREG(CMSTAT));
411 #endif
412         PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
413
414 #ifdef CM_DEBUG
415         if_printf(ifp, "reset: bits cleared, status=0x%02x\n",
416              GETREG(CMSTAT));
417 #endif
418
419         sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
420
421         /* start receiver */
422
423         sc->sc_intmask  |= CM_RI;
424         sc->sc_rx_fillcount = 0;
425         sc->sc_rx_act = 2;
426
427         PUTREG(CMCMD, CM_RXBC(2));
428         PUTREG(CMSTAT, sc->sc_intmask);
429
430 #ifdef CM_DEBUG
431         if_printf(ifp, "reset: started receiver, status=0x%02x\n",
432             GETREG(CMSTAT));
433 #endif
434
435         /* and init transmitter status */
436         sc->sc_tx_act = 0;
437         sc->sc_tx_fillcount = 0;
438
439         ifp->if_flags |= IFF_RUNNING;
440         ifp->if_flags &= ~IFF_OACTIVE;
441
442         cm_start(ifp);
443 }
444
445 /*
446  * Take interface offline
447  */
448 void
449 cm_stop(sc)
450         struct cm_softc *sc;
451 {
452         /* Stop the interrupts */
453         PUTREG(CMSTAT, 0);
454
455         /* Stop the interface */
456         GETREG(CMRESET);
457
458         /* Stop watchdog timer */
459         sc->sc_arccom.ac_if.if_timer = 0;
460 }
461
462 /*
463  * Start output on interface. Get another datagram to send
464  * off the interface queue, and copy it to the
465  * interface becore starting the output
466  *
467  * this assumes that it is called inside a critical section...
468  * XXX hm... does it still?
469  *
470  */
471 void
472 cm_start(ifp)
473         struct ifnet *ifp;
474 {
475         struct cm_softc *sc = ifp->if_softc;
476         struct mbuf *m,*mp;
477
478         int cm_ram_ptr;
479         int len, tlen, offset, s, buffer;
480 #ifdef CMTIMINGS
481         u_long copystart, lencopy, perbyte;
482 #endif
483
484 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
485         if_printf(ifp, "start(%p)\n", ifp);
486 #endif
487
488         if ((ifp->if_flags & IFF_RUNNING) == 0)
489                 return;
490
491         s = splimp();
492
493         if (sc->sc_tx_fillcount >= 2) {
494                 splx(s);
495                 return;
496         }
497
498         m = arc_frag_next(ifp);
499         buffer = sc->sc_tx_act ^ 1;
500
501         splx(s);
502
503         if (m == 0)
504                 return;
505
506 #ifdef CM_DEBUG
507         if (m->m_len < ARC_HDRLEN)
508                 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
509         if_printf(ifp, "start: filling %d from %d to %d type %d\n",
510             buffer, mtod(m, u_char *)[0],
511             mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
512 #else
513         if (m->m_len < 2)
514                 m = m_pullup(m, 2);
515 #endif
516         cm_ram_ptr = buffer * 512;
517
518         if (m == 0)
519                 return;
520
521         /* write the addresses to RAM and throw them away */
522
523         /*
524          * Hardware does this: Yet Another Microsecond Saved.
525          * (btw, timing code says usually 2 microseconds)
526          * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
527          */
528
529         PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
530         m_adj(m, 2);
531
532         /* get total length left at this point */
533         tlen = m->m_pkthdr.len;
534         if (tlen < ARC_MIN_FORBID_LEN) {
535                 offset = 256 - tlen;
536                 PUTMEM(cm_ram_ptr + 2, offset);
537         } else {
538                 PUTMEM(cm_ram_ptr + 2, 0);
539                 if (tlen <= ARC_MAX_FORBID_LEN)
540                         offset = 255;           /* !!! */
541                 else {
542                         if (tlen > ARC_MAX_LEN)
543                                 tlen = ARC_MAX_LEN;
544                         offset = 512 - tlen;
545                 }
546                 PUTMEM(cm_ram_ptr + 3, offset);
547
548         }
549         cm_ram_ptr += offset;
550
551         /* lets loop through the mbuf chain */
552
553         for (mp = m; mp; mp = mp->m_next) {
554                 if ((len = mp->m_len)) {                /* YAMS */
555                         bus_space_write_region_1(
556                             rman_get_bustag(sc->mem_res),
557                             rman_get_bushandle(sc->mem_res),
558                             cm_ram_ptr, mtod(mp, caddr_t), len);
559
560                         cm_ram_ptr += len;
561                 }
562         }
563
564         sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
565         sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
566
567         /* actually transmit the packet */
568         s = splimp();
569
570         if (++sc->sc_tx_fillcount > 1) {
571                 /*
572                  * We are filled up to the rim. No more bufs for the moment,
573                  * please.
574                  */
575                 ifp->if_flags |= IFF_OACTIVE;
576         } else {
577 #ifdef CM_DEBUG
578                 if_printf(ifp, "start: starting transmitter on buffer %d\n",
579                     buffer);
580 #endif
581                 /* Transmitter was off, start it */
582                 sc->sc_tx_act = buffer;
583
584                 /*
585                  * We still can accept another buf, so don't:
586                  * ifp->if_flags |= IFF_OACTIVE;
587                  */
588                 sc->sc_intmask |= CM_TA;
589                 PUTREG(CMCMD, CM_TX(buffer));
590                 PUTREG(CMSTAT, sc->sc_intmask);
591
592                 sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
593         }
594         splx(s);
595         m_freem(m);
596
597         /*
598          * After 10 times reading the docs, I realized
599          * that in the case the receiver NAKs the buffer request,
600          * the hardware retries till shutdown.
601          * This is integrated now in the code above.
602          */
603
604         return;
605 }
606
607 /*
608  * Arcnet interface receiver soft interrupt:
609  * get the stuff out of any filled buffer we find.
610  */
611 void
612 cm_srint(vsc)
613         void *vsc;
614 {
615         struct cm_softc *sc = (struct cm_softc *)vsc;
616         int buffer, len, offset, s, type;
617         int cm_ram_ptr;
618         struct mbuf *m;
619         struct arc_header *ah;
620         struct ifnet *ifp;
621
622         ifp = &sc->sc_arccom.ac_if;
623
624         s = splimp();
625         buffer = sc->sc_rx_act ^ 1;
626         splx(s);
627
628         /* Allocate header mbuf */
629         MGETHDR(m, M_DONTWAIT, MT_DATA);
630
631         if (m == 0) {
632                 /*
633                  * in case s.th. goes wrong with mem, drop it
634                  * to make sure the receiver can be started again
635                  * count it as input error (we dont have any other
636                  * detectable)
637                  */
638                 ifp->if_ierrors++;
639                 goto cleanup;
640         }
641
642         m->m_pkthdr.rcvif = ifp;
643
644         /*
645          * Align so that IP packet will be longword aligned. Here we
646          * assume that m_data of new packet is longword aligned.
647          * When implementing PHDS, we might have to change it to 2,
648          * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
649          */
650
651         cm_ram_ptr = buffer * 512;
652         offset = GETMEM(cm_ram_ptr + 2);
653         if (offset)
654                 len = 256 - offset;
655         else {
656                 offset = GETMEM(cm_ram_ptr + 3);
657                 len = 512 - offset;
658         }
659
660         /*
661          * first +2 bytes for align fixup below
662          * second +2 bytes are for src/dst addresses
663          */
664         if ((len + 2 + 2) > MHLEN) {
665                 /* attach an mbuf cluster */
666                 MCLGET(m, M_DONTWAIT);
667
668                 /* Insist on getting a cluster */
669                 if ((m->m_flags & M_EXT) == 0) {
670                         ifp->if_ierrors++;
671                         goto cleanup;
672                 }
673         }
674
675         if (m == 0) {
676                 ifp->if_ierrors++;
677                 goto cleanup;
678         }
679
680         type = GETMEM(cm_ram_ptr + offset);
681         m->m_data += 1 + arc_isphds(type);
682         /* mbuf filled with ARCnet addresses */
683         m->m_pkthdr.len = m->m_len = len + 2;
684
685         ah = mtod(m, struct arc_header *);
686         ah->arc_shost = GETMEM(cm_ram_ptr + 0);
687         ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
688
689         bus_space_read_region_1(
690             rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
691             cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
692
693         arc_input(ifp, m);
694
695         m = NULL;
696         ifp->if_ipackets++;
697
698 cleanup:
699
700         if (m != NULL)
701                 m_freem(m);
702
703         /* mark buffer as invalid by source id 0 */
704         PUTMEM(buffer << 9, 0);
705         s = splimp();
706
707         if (--sc->sc_rx_fillcount == 2 - 1) {
708
709                 /* was off, restart it on buffer just emptied */
710                 sc->sc_rx_act = buffer;
711                 sc->sc_intmask |= CM_RI;
712
713                 /* this also clears the RI flag interupt: */
714                 PUTREG(CMCMD, CM_RXBC(buffer));
715                 PUTREG(CMSTAT, sc->sc_intmask);
716
717 #ifdef CM_DEBUG
718                 if_printf(ifp, "srint: restarted rx on buf %d\n", 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                 if_printf(ifp,
783                     "tint: starting tx on buffer %d, status 0x%02x\n",
784                     buffer, GETREG(CMSTAT));
785 #endif
786         } else {
787                 /* have to disable TX interrupt */
788                 sc->sc_intmask &= ~CM_TA;
789                 PUTREG(CMSTAT, sc->sc_intmask);
790                 /* ... and watchdog timer */
791                 ifp->if_timer = 0;
792
793 #ifdef CM_DEBUG
794                 if_printf(ifp, "tint: no more buffers to send, status 0x%02x\n",
795                     GETREG(CMSTAT));
796 #endif
797         }
798
799         /* XXXX TODO */
800 #ifdef CMSOFTCOPY
801         /* schedule soft int to fill a new buffer for us */
802         softintr_schedule(sc->sc_txcookie);
803 #else
804         /* call it directly */
805         cm_start(ifp);
806 #endif
807 }
808
809 /*
810  * Our interrupt routine
811  */
812 void
813 cmintr(arg)
814         void *arg;
815 {
816         struct cm_softc *sc = arg;
817         struct ifnet *ifp = &sc->sc_arccom.ac_if;
818
819         u_char isr, maskedisr;
820         int buffer;
821         u_long newsec;
822
823         isr = GETREG(CMSTAT);
824         maskedisr = isr & sc->sc_intmask;
825         if (!maskedisr)
826                 return;
827         do {
828
829 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
830                 if_printf(ifp, "intr: status 0x%02x, intmask 0x%02x\n",
831                     isr, sc->sc_intmask);
832 #endif
833
834                 if (maskedisr & CM_POR) {
835                         /*
836                          * XXX We should never see this. Don't bother to store
837                          * the address.
838                          * sc->sc_arccom.ac_anaddr = GETMEM(CMMACOFF);
839                          */
840                         PUTREG(CMCMD, CM_CLR(CLR_POR));
841                         log(LOG_WARNING,
842                             "%s: intr: got spurious power on reset int\n",
843                             ifp->if_xname);
844                 }
845
846                 if (maskedisr & CM_RECON) {
847                         /*
848                          * we dont need to:
849                          * PUTREG(CMCMD, CM_CONF(CONF_LONG));
850                          */
851                         PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
852                         sc->sc_arccom.ac_if.if_collisions++;
853
854                         /*
855                          * If less than 2 seconds per reconfig:
856                          *      If ARC_EXCESSIVE_RECONFIGS
857                          *      since last burst, complain and set treshold for
858                          *      warnings to ARC_EXCESSIVE_RECONS_REWARN.
859                          *
860                          * This allows for, e.g., new stations on the cable, or
861                          * cable switching as long as it is over after
862                          * (normally) 16 seconds.
863                          *
864                          * XXX TODO: check timeout bits in status word and
865                          * double time if necessary.
866                          */
867
868                         callout_stop(&sc->sc_recon_ch);
869                         newsec = time_second;
870                         if ((newsec - sc->sc_recontime <= 2) &&
871                             (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
872                                 log(LOG_WARNING,
873                                     "%s: excessive token losses, "
874                                     "cable problem?\n",
875                                     ifp->if_xname);
876                         }
877                         sc->sc_recontime = newsec;
878                         callout_reset(&sc->sc_recon_ch, 15 * hz,
879                             cm_reconwatch, (void *)sc);
880                 }
881
882                 if (maskedisr & CM_RI) {
883 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
884                         if_printf(ifp, "intr: hard rint, act %d\n",
885                             sc->sc_rx_act);
886 #endif
887
888                         buffer = sc->sc_rx_act;
889                         /* look if buffer is marked invalid: */
890                         if (GETMEM(buffer * 512) == 0) {
891                                 /*
892                                  * invalid marked buffer (or illegally
893                                  * configured sender)
894                                  */
895                                 log(LOG_WARNING,
896                                     "%s: spurious RX interupt or sender 0 "
897                                     " (ignored)\n", ifp->if_xname);
898                                 /*
899                                  * restart receiver on same buffer.
900                                  * XXX maybe better reset interface?
901                                  */
902                                 PUTREG(CMCMD, CM_RXBC(buffer));
903                         } else {
904                                 if (++sc->sc_rx_fillcount > 1) {
905                                         sc->sc_intmask &= ~CM_RI;
906                                         PUTREG(CMSTAT, sc->sc_intmask);
907                                 } else {
908                                         buffer ^= 1;
909                                         sc->sc_rx_act = buffer;
910
911                                         /*
912                                          * Start receiver on other receive
913                                          * buffer. This also clears the RI
914                                          * interupt flag.
915                                          */
916                                         PUTREG(CMCMD, CM_RXBC(buffer));
917                                         /* in RX intr, so mask is ok for RX */
918
919 #ifdef CM_DEBUG
920                                         if_printf(ifp, "strt rx for buf %d, "
921                                             "stat 0x%02x\n",
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         if_printf(ifp, "intr (exit): status 0x%02x, intmask 0x%02x\n",
946             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(ifp, command, data)
972         struct ifnet *ifp;
973         u_long command;
974         caddr_t data;
975 {
976         struct cm_softc *sc;
977         struct ifaddr *ifa;
978         struct ifreq *ifr;
979         int s, error;
980
981         error = 0;
982         sc = ifp->if_softc;
983         ifa = (struct ifaddr *)data;
984         ifr = (struct ifreq *)data;
985         s = splimp();
986
987 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
988         if_printf(ifp, "ioctl() called, cmd = 0x%lx\n", command);
989 #endif
990
991         switch (command) {
992         case SIOCSIFADDR:
993         case SIOCGIFADDR:
994         case SIOCADDMULTI:
995         case SIOCDELMULTI:
996         case SIOCSIFMTU:
997                 error = arc_ioctl(ifp, command, data);
998                 break;
999
1000         case SIOCSIFFLAGS:
1001                 if ((ifp->if_flags & IFF_UP) == 0 &&
1002                     (ifp->if_flags & IFF_RUNNING) != 0) {
1003                         /*
1004                          * If interface is marked down and it is running,
1005                          * then stop it.
1006                          */
1007                         cm_stop(sc);
1008                         ifp->if_flags &= ~IFF_RUNNING;
1009                 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1010                            (ifp->if_flags & IFF_RUNNING) == 0) {
1011                         /*
1012                          * If interface is marked up and it is stopped, then
1013                          * start it.
1014                          */
1015                         cm_init(sc);
1016                 }
1017                 break;
1018
1019         default:
1020                 error = EINVAL;
1021                 break;
1022         }
1023
1024         splx(s);
1025         return (error);
1026 }
1027
1028 /*
1029  * watchdog routine for transmitter.
1030  *
1031  * We need this, because else a receiver whose hardware is alive, but whose
1032  * software has not enabled the Receiver, would make our hardware wait forever
1033  * Discovered this after 20 times reading the docs.
1034  *
1035  * Only thing we do is disable transmitter. We'll get a transmit timeout,
1036  * and the int handler will have to decide not to retransmit (in case
1037  * retransmission is implemented).
1038  *
1039  * This one assumes being called inside splimp()
1040  */
1041
1042 void
1043 cm_watchdog(ifp)
1044         struct ifnet *ifp;
1045 {
1046         struct cm_softc *sc = ifp->if_softc;
1047
1048         PUTREG(CMCMD, CM_TXDIS);
1049         return;
1050 }