Merge from vendor branch READLINE:
[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.10 2004/07/23 07:16:25 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(dev, SYS_RES_IRQ, &rid,
234                                  0ul, ~0ul, 1, RF_ACTIVE);
235         if (res) {
236                 sc->irq_rid = rid;
237                 sc->irq_res = res;
238                 return (0);
239         } else {
240                 return (ENOENT);
241         }
242 }
243
244 /*
245  * Release all resources
246  */
247 void
248 cm_release_resources(dev)
249         device_t dev;
250 {
251         struct cm_softc *sc = device_get_softc(dev);
252
253         if (sc->port_res) {
254                 bus_deactivate_resource(dev, SYS_RES_IOPORT,
255                                      sc->port_rid, sc->port_res);
256                 bus_release_resource(dev, SYS_RES_IOPORT,
257                                      sc->port_rid, sc->port_res);
258                 sc->port_res = 0;
259         }
260         if (sc->mem_res) {
261                 bus_deactivate_resource(dev, SYS_RES_MEMORY,
262                                      sc->mem_rid, sc->mem_res);
263                 bus_release_resource(dev, SYS_RES_MEMORY,
264                                      sc->mem_rid, sc->mem_res);
265                 sc->mem_res = 0;
266         }
267         if (sc->irq_res) {
268                 bus_deactivate_resource(dev, SYS_RES_IRQ,
269                                      sc->irq_rid, sc->irq_res);
270                 bus_release_resource(dev, SYS_RES_IRQ,
271                                      sc->irq_rid, sc->irq_res);
272                 sc->irq_res = 0;
273         }
274 }
275
276 int
277 cm_attach(dev)
278         device_t dev;
279 {
280         struct cm_softc *sc = device_get_softc(dev);
281         struct ifnet *ifp = &sc->sc_arccom.ac_if;
282         int s;
283         u_int8_t linkaddress;
284
285         s = splhigh();
286
287         /*
288          * read the arcnet address from the board
289          */
290
291         GETREG(CMRESET);
292         do {
293                 DELAY(200);
294         } while (!(GETREG(CMSTAT) & CM_POR));
295
296         linkaddress = GETMEM(CMMACOFF);
297
298         /* clear the int mask... */
299
300         sc->sc_intmask = 0;
301         PUTREG(CMSTAT, 0);
302
303         PUTREG(CMCMD, CM_CONF(CONF_LONG));
304         PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
305         sc->sc_recontime = sc->sc_reconcount = 0;
306
307         /* and reenable kernel int level */
308         splx(s);
309
310         /*
311          * set interface to stopped condition (reset)
312          */
313         cm_stop(sc);
314
315         ifp->if_softc = sc;
316         if_initname(ifp, "cm", device_get_unit(dev));
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;
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 defined(__DragonFly__) || __FreeBSD_version < 500000
335         callout_init(&sc->sc_recon_ch);
336 #else
337         callout_init(&sc->sc_recon_ch, 0);
338 #endif
339
340         printf("%s: link addr 0x%02x (%d)\n",
341                ifp->if_xname, linkaddress, linkaddress);
342         return 0;
343 }
344
345 /*
346  * Initialize device
347  *
348  */
349 void
350 cm_init(xsc)
351         void *xsc;
352 {
353         struct cm_softc *sc = (struct cm_softc *)xsc;
354         struct ifnet *ifp;
355         int s;
356
357         ifp = &sc->sc_arccom.ac_if;
358
359         if ((ifp->if_flags & IFF_RUNNING) == 0) {
360                 s = splimp();
361                 ifp->if_flags |= IFF_RUNNING;
362                 cm_reset(sc);
363                 cm_start(ifp);
364                 splx(s);
365         }
366 }
367
368 /*
369  * Reset the interface...
370  *
371  * this assumes that it is called inside a critical section...
372  *
373  */
374 void
375 cm_reset(sc)
376         struct cm_softc *sc;
377 {
378         struct ifnet *ifp;
379         int linkaddress;
380
381         ifp = &sc->sc_arccom.ac_if;
382
383 #ifdef CM_DEBUG
384         printf("%s: reset\n", ifp->if_xname);
385 #endif
386         /* stop and restart hardware */
387
388         GETREG(CMRESET);
389         do {
390                 DELAY(200);
391         } while (!(GETREG(CMSTAT) & CM_POR));
392
393         linkaddress = GETMEM(CMMACOFF);
394
395 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
396         printf("%s: reset: card reset, link addr = 0x%02x (%d)\n",
397             ifp->if_xname, linkaddress, linkaddress);
398 #endif
399
400         /* tell the routing level about the (possibly changed) link address */
401         arc_storelladdr(ifp, linkaddress);
402         arc_frag_init(ifp);
403
404         /* POR is NMI, but we need it below: */
405         sc->sc_intmask = CM_RECON|CM_POR;
406         PUTREG(CMSTAT, sc->sc_intmask);
407         PUTREG(CMCMD, CM_CONF(CONF_LONG));
408
409 #ifdef CM_DEBUG
410         printf("%s: reset: chip configured, status=0x%02x\n",
411             ifp->if_xname, GETREG(CMSTAT));
412 #endif
413         PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
414
415 #ifdef CM_DEBUG
416         printf("%s: reset: bits cleared, status=0x%02x\n",
417             ifp->if_xname, GETREG(CMSTAT));
418 #endif
419
420         sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
421
422         /* start receiver */
423
424         sc->sc_intmask  |= CM_RI;
425         sc->sc_rx_fillcount = 0;
426         sc->sc_rx_act = 2;
427
428         PUTREG(CMCMD, CM_RXBC(2));
429         PUTREG(CMSTAT, sc->sc_intmask);
430
431 #ifdef CM_DEBUG
432         printf("%s: reset: started receiver, status=0x%02x\n",
433             ifp->if_xname, GETREG(CMSTAT));
434 #endif
435
436         /* and init transmitter status */
437         sc->sc_tx_act = 0;
438         sc->sc_tx_fillcount = 0;
439
440         ifp->if_flags |= IFF_RUNNING;
441         ifp->if_flags &= ~IFF_OACTIVE;
442
443         cm_start(ifp);
444 }
445
446 /*
447  * Take interface offline
448  */
449 void
450 cm_stop(sc)
451         struct cm_softc *sc;
452 {
453         /* Stop the interrupts */
454         PUTREG(CMSTAT, 0);
455
456         /* Stop the interface */
457         GETREG(CMRESET);
458
459         /* Stop watchdog timer */
460         sc->sc_arccom.ac_if.if_timer = 0;
461 }
462
463 /*
464  * Start output on interface. Get another datagram to send
465  * off the interface queue, and copy it to the
466  * interface becore starting the output
467  *
468  * this assumes that it is called inside a critical section...
469  * XXX hm... does it still?
470  *
471  */
472 void
473 cm_start(ifp)
474         struct ifnet *ifp;
475 {
476         struct cm_softc *sc = ifp->if_softc;
477         struct mbuf *m,*mp;
478
479         int cm_ram_ptr;
480         int len, tlen, offset, s, buffer;
481 #ifdef CMTIMINGS
482         u_long copystart, lencopy, perbyte;
483 #endif
484
485 #if defined(CM_DEBUG) && (CM_DEBUG > 3)
486         printf("%s: start(%p)\n", ifp->if_xname, ifp);
487 #endif
488
489         if ((ifp->if_flags & IFF_RUNNING) == 0)
490                 return;
491
492         s = splimp();
493
494         if (sc->sc_tx_fillcount >= 2) {
495                 splx(s);
496                 return;
497         }
498
499         m = arc_frag_next(ifp);
500         buffer = sc->sc_tx_act ^ 1;
501
502         splx(s);
503
504         if (m == 0)
505                 return;
506
507 #ifdef CM_DEBUG
508         if (m->m_len < ARC_HDRLEN)
509                 m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
510         printf("%s: start: filling %d from %d to %d type %d\n",
511             ifp->if_xname, buffer, mtod(m, u_char *)[0],
512             mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
513 #else
514         if (m->m_len < 2)
515                 m = m_pullup(m, 2);
516 #endif
517         cm_ram_ptr = buffer * 512;
518
519         if (m == 0)
520                 return;
521
522         /* write the addresses to RAM and throw them away */
523
524         /*
525          * Hardware does this: Yet Another Microsecond Saved.
526          * (btw, timing code says usually 2 microseconds)
527          * PUTMEM(cm_ram_ptr + 0, mtod(m, u_char *)[0]);
528          */
529
530         PUTMEM(cm_ram_ptr + 1, mtod(m, u_char *)[1]);
531         m_adj(m, 2);
532
533         /* get total length left at this point */
534         tlen = m->m_pkthdr.len;
535         if (tlen < ARC_MIN_FORBID_LEN) {
536                 offset = 256 - tlen;
537                 PUTMEM(cm_ram_ptr + 2, offset);
538         } else {
539                 PUTMEM(cm_ram_ptr + 2, 0);
540                 if (tlen <= ARC_MAX_FORBID_LEN)
541                         offset = 255;           /* !!! */
542                 else {
543                         if (tlen > ARC_MAX_LEN)
544                                 tlen = ARC_MAX_LEN;
545                         offset = 512 - tlen;
546                 }
547                 PUTMEM(cm_ram_ptr + 3, offset);
548
549         }
550         cm_ram_ptr += offset;
551
552         /* lets loop through the mbuf chain */
553
554         for (mp = m; mp; mp = mp->m_next) {
555                 if ((len = mp->m_len)) {                /* YAMS */
556                         bus_space_write_region_1(
557                             rman_get_bustag(sc->mem_res),
558                             rman_get_bushandle(sc->mem_res),
559                             cm_ram_ptr, mtod(mp, caddr_t), len);
560
561                         cm_ram_ptr += len;
562                 }
563         }
564
565         sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
566         sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
567
568         /* actually transmit the packet */
569         s = splimp();
570
571         if (++sc->sc_tx_fillcount > 1) {
572                 /*
573                  * We are filled up to the rim. No more bufs for the moment,
574                  * please.
575                  */
576                 ifp->if_flags |= IFF_OACTIVE;
577         } else {
578 #ifdef CM_DEBUG
579                 printf("%s: start: starting transmitter on buffer %d\n",
580                     ifp->if_xname, buffer);
581 #endif
582                 /* Transmitter was off, start it */
583                 sc->sc_tx_act = buffer;
584
585                 /*
586                  * We still can accept another buf, so don't:
587                  * ifp->if_flags |= IFF_OACTIVE;
588                  */
589                 sc->sc_intmask |= CM_TA;
590                 PUTREG(CMCMD, CM_TX(buffer));
591                 PUTREG(CMSTAT, sc->sc_intmask);
592
593                 sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
594         }
595         splx(s);
596         m_freem(m);
597
598         /*
599          * After 10 times reading the docs, I realized
600          * that in the case the receiver NAKs the buffer request,
601          * the hardware retries till shutdown.
602          * This is integrated now in the code above.
603          */
604
605         return;
606 }
607
608 /*
609  * Arcnet interface receiver soft interrupt:
610  * get the stuff out of any filled buffer we find.
611  */
612 void
613 cm_srint(vsc)
614         void *vsc;
615 {
616         struct cm_softc *sc = (struct cm_softc *)vsc;
617         int buffer, len, offset, s, type;
618         int cm_ram_ptr;
619         struct mbuf *m;
620         struct arc_header *ah;
621         struct ifnet *ifp;
622
623         ifp = &sc->sc_arccom.ac_if;
624
625         s = splimp();
626         buffer = sc->sc_rx_act ^ 1;
627         splx(s);
628
629         /* Allocate header mbuf */
630         MGETHDR(m, MB_DONTWAIT, MT_DATA);
631
632         if (m == 0) {
633                 /*
634                  * in case s.th. goes wrong with mem, drop it
635                  * to make sure the receiver can be started again
636                  * count it as input error (we dont have any other
637                  * detectable)
638                  */
639                 ifp->if_ierrors++;
640                 goto cleanup;
641         }
642
643         m->m_pkthdr.rcvif = ifp;
644
645         /*
646          * Align so that IP packet will be longword aligned. Here we
647          * assume that m_data of new packet is longword aligned.
648          * When implementing PHDS, we might have to change it to 2,
649          * (2*sizeof(ulong) - CM_HDRNEWLEN)), packet type dependent.
650          */
651
652         cm_ram_ptr = buffer * 512;
653         offset = GETMEM(cm_ram_ptr + 2);
654         if (offset)
655                 len = 256 - offset;
656         else {
657                 offset = GETMEM(cm_ram_ptr + 3);
658                 len = 512 - offset;
659         }
660
661         /*
662          * first +2 bytes for align fixup below
663          * second +2 bytes are for src/dst addresses
664          */
665         if ((len + 2 + 2) > MHLEN) {
666                 /* attach an mbuf cluster */
667                 MCLGET(m, MB_DONTWAIT);
668
669                 /* Insist on getting a cluster */
670                 if ((m->m_flags & M_EXT) == 0) {
671                         ifp->if_ierrors++;
672                         goto cleanup;
673                 }
674         }
675
676         if (m == 0) {
677                 ifp->if_ierrors++;
678                 goto cleanup;
679         }
680
681         type = GETMEM(cm_ram_ptr + offset);
682         m->m_data += 1 + arc_isphds(type);
683         /* mbuf filled with ARCnet addresses */
684         m->m_pkthdr.len = m->m_len = len + 2;
685
686         ah = mtod(m, struct arc_header *);
687         ah->arc_shost = GETMEM(cm_ram_ptr + 0);
688         ah->arc_dhost = GETMEM(cm_ram_ptr + 1);
689
690         bus_space_read_region_1(
691             rman_get_bustag(sc->mem_res), rman_get_bushandle(sc->mem_res),
692             cm_ram_ptr + offset, mtod(m, u_char *) + 2, len);
693
694         (*ifp->if_input)(ifp, m);
695
696         m = NULL;
697         ifp->if_ipackets++;
698
699 cleanup:
700
701         if (m != NULL)
702                 m_freem(m);
703
704         /* mark buffer as invalid by source id 0 */
705         PUTMEM(buffer << 9, 0);
706         s = splimp();
707
708         if (--sc->sc_rx_fillcount == 2 - 1) {
709
710                 /* was off, restart it on buffer just emptied */
711                 sc->sc_rx_act = buffer;
712                 sc->sc_intmask |= CM_RI;
713
714                 /* this also clears the RI flag interupt: */
715                 PUTREG(CMCMD, CM_RXBC(buffer));
716                 PUTREG(CMSTAT, sc->sc_intmask);
717
718 #ifdef CM_DEBUG
719                 printf("%s: srint: restarted rx on buf %d\n",
720                     ifp->if_xname, buffer);
721 #endif
722         }
723         splx(s);
724 }
725
726 __inline static void
727 cm_tint(sc, isr)
728         struct cm_softc *sc;
729         int isr;
730 {
731         struct ifnet *ifp;
732
733         int buffer;
734 #ifdef CMTIMINGS
735         int clknow;
736 #endif
737
738         ifp = &(sc->sc_arccom.ac_if);
739         buffer = sc->sc_tx_act;
740
741         /*
742          * retransmit code:
743          * Normal situtations first for fast path:
744          * If acknowledgement received ok or broadcast, we're ok.
745          * else if
746          */
747
748         if (isr & CM_TMA || sc->sc_broadcast[buffer])
749                 sc->sc_arccom.ac_if.if_opackets++;
750 #ifdef CMRETRANSMIT
751         else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
752             && --sc->sc_retransmits[buffer] > 0) {
753                 /* retransmit same buffer */
754                 PUTREG(CMCMD, CM_TX(buffer));
755                 return;
756         }
757 #endif
758         else
759                 ifp->if_oerrors++;
760
761
762         /* We know we can accept another buffer at this point. */
763         ifp->if_flags &= ~IFF_OACTIVE;
764
765         if (--sc->sc_tx_fillcount > 0) {
766
767                 /*
768                  * start tx on other buffer.
769                  * This also clears the int flag
770                  */
771                 buffer ^= 1;
772                 sc->sc_tx_act = buffer;
773
774                 /*
775                  * already given:
776                  * sc->sc_intmask |= CM_TA;
777                  * PUTREG(CMSTAT, sc->sc_intmask);
778                  */
779                 PUTREG(CMCMD, CM_TX(buffer));
780                 /* init watchdog timer */
781                 ifp->if_timer = ARCTIMEOUT;
782
783 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
784                 printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
785                     ifp->if_xname, buffer, GETREG(CMSTAT));
786 #endif
787         } else {
788                 /* have to disable TX interrupt */
789                 sc->sc_intmask &= ~CM_TA;
790                 PUTREG(CMSTAT, sc->sc_intmask);
791                 /* ... and watchdog timer */
792                 ifp->if_timer = 0;
793
794 #ifdef CM_DEBUG
795                 printf("%s: tint: no more buffers to send, status 0x%02x\n",
796                     ifp->if_xname, GETREG(CMSTAT));
797 #endif
798         }
799
800         /* XXXX TODO */
801 #ifdef CMSOFTCOPY
802         /* schedule soft int to fill a new buffer for us */
803         softintr_schedule(sc->sc_txcookie);
804 #else
805         /* call it directly */
806         cm_start(ifp);
807 #endif
808 }
809
810 /*
811  * Our interrupt routine
812  */
813 void
814 cmintr(arg)
815         void *arg;
816 {
817         struct cm_softc *sc = arg;
818         struct ifnet *ifp = &sc->sc_arccom.ac_if;
819
820         u_char isr, maskedisr;
821         int buffer;
822         u_long newsec;
823
824         isr = GETREG(CMSTAT);
825         maskedisr = isr & sc->sc_intmask;
826         if (!maskedisr)
827                 return;
828         do {
829
830 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
831                 printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
832                     ifp->if_xname, isr, sc->sc_intmask);
833 #endif
834
835                 if (maskedisr & CM_POR) {
836                         /*
837                          * XXX We should never see this. Don't bother to store
838                          * the address.
839                          * sc->sc_arccom.ac_anaddr = GETMEM(CMMACOFF);
840                          */
841                         PUTREG(CMCMD, CM_CLR(CLR_POR));
842                         log(LOG_WARNING,
843                             "%s: intr: got spurious power on reset int\n",
844                             ifp->if_xname);
845                 }
846
847                 if (maskedisr & CM_RECON) {
848                         /*
849                          * we dont need to:
850                          * PUTREG(CMCMD, CM_CONF(CONF_LONG));
851                          */
852                         PUTREG(CMCMD, CM_CLR(CLR_RECONFIG));
853                         sc->sc_arccom.ac_if.if_collisions++;
854
855                         /*
856                          * If less than 2 seconds per reconfig:
857                          *      If ARC_EXCESSIVE_RECONFIGS
858                          *      since last burst, complain and set treshold for
859                          *      warnings to ARC_EXCESSIVE_RECONS_REWARN.
860                          *
861                          * This allows for, e.g., new stations on the cable, or
862                          * cable switching as long as it is over after
863                          * (normally) 16 seconds.
864                          *
865                          * XXX TODO: check timeout bits in status word and
866                          * double time if necessary.
867                          */
868
869                         callout_stop(&sc->sc_recon_ch);
870                         newsec = time_second;
871                         if ((newsec - sc->sc_recontime <= 2) &&
872                             (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
873                                 log(LOG_WARNING,
874                                     "%s: excessive token losses, "
875                                     "cable problem?\n",
876                                     ifp->if_xname);
877                         }
878                         sc->sc_recontime = newsec;
879                         callout_reset(&sc->sc_recon_ch, 15 * hz,
880                             cm_reconwatch, (void *)sc);
881                 }
882
883                 if (maskedisr & CM_RI) {
884 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
885                         printf("%s: intr: hard rint, act %d\n",
886                             ifp->if_xname, sc->sc_rx_act);
887 #endif
888
889                         buffer = sc->sc_rx_act;
890                         /* look if buffer is marked invalid: */
891                         if (GETMEM(buffer * 512) == 0) {
892                                 /*
893                                  * invalid marked buffer (or illegally
894                                  * configured sender)
895                                  */
896                                 log(LOG_WARNING,
897                                     "%s: spurious RX interupt or sender 0 "
898                                     " (ignored)\n", ifp->if_xname);
899                                 /*
900                                  * restart receiver on same buffer.
901                                  * XXX maybe better reset interface?
902                                  */
903                                 PUTREG(CMCMD, CM_RXBC(buffer));
904                         } else {
905                                 if (++sc->sc_rx_fillcount > 1) {
906                                         sc->sc_intmask &= ~CM_RI;
907                                         PUTREG(CMSTAT, sc->sc_intmask);
908                                 } else {
909                                         buffer ^= 1;
910                                         sc->sc_rx_act = buffer;
911
912                                         /*
913                                          * Start receiver on other receive
914                                          * buffer. This also clears the RI
915                                          * interupt flag.
916                                          */
917                                         PUTREG(CMCMD, CM_RXBC(buffer));
918                                         /* in RX intr, so mask is ok for RX */
919
920 #ifdef CM_DEBUG
921                                         printf("%s: strt rx for buf %d, "
922                                             "stat 0x%02x\n",
923                                             ifp->if_xname,
924                                             sc->sc_rx_act, GETREG(CMSTAT));
925 #endif
926                                 }
927
928 #ifdef CMSOFTCOPY
929                                 /*
930                                  * this one starts a soft int to copy out
931                                  * of the hw
932                                  */
933                                 softintr_schedule(sc->sc_rxcookie);
934 #else
935                                 /* this one does the copy here */
936                                 cm_srint(sc);
937 #endif
938                         }
939                 }
940                 if (maskedisr & CM_TA) {
941                         cm_tint(sc, isr);
942                 }
943                 isr = GETREG(CMSTAT);
944                 maskedisr = isr & sc->sc_intmask;
945         } while (maskedisr);
946 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
947         printf("%s: intr (exit): status 0x%02x, intmask 0x%02x\n",
948             ifp->if_xname, isr, sc->sc_intmask);
949 #endif
950 }
951
952 void
953 cm_reconwatch(arg)
954         void *arg;
955 {
956         struct cm_softc *sc = arg;
957         struct ifnet *ifp = &sc->sc_arccom.ac_if;
958
959         if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
960                 sc->sc_reconcount = 0;
961                 log(LOG_WARNING, "%s: token valid again.\n",
962                     ifp->if_xname);
963         }
964         sc->sc_reconcount = 0;
965 }
966
967
968 /*
969  * Process an ioctl request.
970  * This code needs some work - it looks pretty ugly.
971  */
972 int
973 cm_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
974 {
975         struct cm_softc *sc;
976         struct ifaddr *ifa;
977         struct ifreq *ifr;
978         int s, error;
979
980         error = 0;
981         sc = ifp->if_softc;
982         ifa = (struct ifaddr *)data;
983         ifr = (struct ifreq *)data;
984         s = splimp();
985
986 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
987         printf("%s: ioctl() called, cmd = 0x%lx\n",
988             ifp->if_xname, 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 an 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 }