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