Merge from vendor branch CVS:
[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.11 2005/02/11 22:25:56 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         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
322         ifp->if_timer = 0;
323         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
324
325         arc_ifattach(ifp, linkaddress);
326
327 #ifdef CMSOFTCOPY
328         sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, cm_srint, sc);
329         sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
330                 (void (*) (void *))cm_start, ifp);
331 #endif
332
333 #if defined(__DragonFly__) || __FreeBSD_version < 500000
334         callout_init(&sc->sc_recon_ch);
335 #else
336         callout_init(&sc->sc_recon_ch, 0);
337 #endif
338
339         printf("%s: link addr 0x%02x (%d)\n",
340                ifp->if_xname, 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         printf("%s: reset\n", ifp->if_xname);
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         printf("%s: reset: card reset, link addr = 0x%02x (%d)\n",
396             ifp->if_xname, 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         printf("%s: reset: chip configured, status=0x%02x\n",
410             ifp->if_xname, GETREG(CMSTAT));
411 #endif
412         PUTREG(CMCMD, CM_CLR(CLR_POR|CLR_RECONFIG));
413
414 #ifdef CM_DEBUG
415         printf("%s: reset: bits cleared, status=0x%02x\n",
416             ifp->if_xname, 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         printf("%s: reset: started receiver, status=0x%02x\n",
432             ifp->if_xname, 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         printf("%s: start(%p)\n", ifp->if_xname, 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         printf("%s: start: filling %d from %d to %d type %d\n",
510             ifp->if_xname, 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                 printf("%s: start: starting transmitter on buffer %d\n",
579                     ifp->if_xname, 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, MB_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, MB_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         (*ifp->if_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                 printf("%s: srint: restarted rx on buf %d\n",
719                     ifp->if_xname, buffer);
720 #endif
721         }
722         splx(s);
723 }
724
725 __inline static void
726 cm_tint(sc, isr)
727         struct cm_softc *sc;
728         int isr;
729 {
730         struct ifnet *ifp;
731
732         int buffer;
733 #ifdef CMTIMINGS
734         int clknow;
735 #endif
736
737         ifp = &(sc->sc_arccom.ac_if);
738         buffer = sc->sc_tx_act;
739
740         /*
741          * retransmit code:
742          * Normal situtations first for fast path:
743          * If acknowledgement received ok or broadcast, we're ok.
744          * else if
745          */
746
747         if (isr & CM_TMA || sc->sc_broadcast[buffer])
748                 sc->sc_arccom.ac_if.if_opackets++;
749 #ifdef CMRETRANSMIT
750         else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
751             && --sc->sc_retransmits[buffer] > 0) {
752                 /* retransmit same buffer */
753                 PUTREG(CMCMD, CM_TX(buffer));
754                 return;
755         }
756 #endif
757         else
758                 ifp->if_oerrors++;
759
760
761         /* We know we can accept another buffer at this point. */
762         ifp->if_flags &= ~IFF_OACTIVE;
763
764         if (--sc->sc_tx_fillcount > 0) {
765
766                 /*
767                  * start tx on other buffer.
768                  * This also clears the int flag
769                  */
770                 buffer ^= 1;
771                 sc->sc_tx_act = buffer;
772
773                 /*
774                  * already given:
775                  * sc->sc_intmask |= CM_TA;
776                  * PUTREG(CMSTAT, sc->sc_intmask);
777                  */
778                 PUTREG(CMCMD, CM_TX(buffer));
779                 /* init watchdog timer */
780                 ifp->if_timer = ARCTIMEOUT;
781
782 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
783                 printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
784                     ifp->if_xname, 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                 printf("%s: tint: no more buffers to send, status 0x%02x\n",
795                     ifp->if_xname, 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                 printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
831                     ifp->if_xname, 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                         printf("%s: intr: hard rint, act %d\n",
885                             ifp->if_xname, 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                                         printf("%s: strt rx for buf %d, "
921                                             "stat 0x%02x\n",
922                                             ifp->if_xname,
923                                             sc->sc_rx_act, GETREG(CMSTAT));
924 #endif
925                                 }
926
927 #ifdef CMSOFTCOPY
928                                 /*
929                                  * this one starts a soft int to copy out
930                                  * of the hw
931                                  */
932                                 softintr_schedule(sc->sc_rxcookie);
933 #else
934                                 /* this one does the copy here */
935                                 cm_srint(sc);
936 #endif
937                         }
938                 }
939                 if (maskedisr & CM_TA) {
940                         cm_tint(sc, isr);
941                 }
942                 isr = GETREG(CMSTAT);
943                 maskedisr = isr & sc->sc_intmask;
944         } while (maskedisr);
945 #if defined(CM_DEBUG) && (CM_DEBUG > 1)
946         printf("%s: intr (exit): status 0x%02x, intmask 0x%02x\n",
947             ifp->if_xname, isr, sc->sc_intmask);
948 #endif
949 }
950
951 void
952 cm_reconwatch(arg)
953         void *arg;
954 {
955         struct cm_softc *sc = arg;
956         struct ifnet *ifp = &sc->sc_arccom.ac_if;
957
958         if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
959                 sc->sc_reconcount = 0;
960                 log(LOG_WARNING, "%s: token valid again.\n",
961                     ifp->if_xname);
962         }
963         sc->sc_reconcount = 0;
964 }
965
966
967 /*
968  * Process an ioctl request.
969  * This code needs some work - it looks pretty ugly.
970  */
971 int
972 cm_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
973 {
974         struct cm_softc *sc;
975         struct ifaddr *ifa;
976         struct ifreq *ifr;
977         int s, error;
978
979         error = 0;
980         sc = ifp->if_softc;
981         ifa = (struct ifaddr *)data;
982         ifr = (struct ifreq *)data;
983         s = splimp();
984
985 #if defined(CM_DEBUG) && (CM_DEBUG > 2)
986         printf("%s: ioctl() called, cmd = 0x%lx\n",
987             ifp->if_xname, command);
988 #endif
989
990         switch (command) {
991         case SIOCSIFADDR:
992         case SIOCGIFADDR:
993         case SIOCADDMULTI:
994         case SIOCDELMULTI:
995         case SIOCSIFMTU:
996                 error = arc_ioctl(ifp, command, data);
997                 break;
998
999         case SIOCSIFFLAGS:
1000                 if ((ifp->if_flags & IFF_UP) == 0 &&
1001                     (ifp->if_flags & IFF_RUNNING) != 0) {
1002                         /*
1003                          * If interface is marked down and it is running,
1004                          * then stop it.
1005                          */
1006                         cm_stop(sc);
1007                         ifp->if_flags &= ~IFF_RUNNING;
1008                 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1009                            (ifp->if_flags & IFF_RUNNING) == 0) {
1010                         /*
1011                          * If interface is marked up and it is stopped, then
1012                          * start it.
1013                          */
1014                         cm_init(sc);
1015                 }
1016                 break;
1017
1018         default:
1019                 error = EINVAL;
1020                 break;
1021         }
1022
1023         splx(s);
1024         return (error);
1025 }
1026
1027 /*
1028  * watchdog routine for transmitter.
1029  *
1030  * We need this, because else a receiver whose hardware is alive, but whose
1031  * software has not enabled the Receiver, would make our hardware wait forever
1032  * Discovered this after 20 times reading the docs.
1033  *
1034  * Only thing we do is disable transmitter. We'll get an transmit timeout,
1035  * and the int handler will have to decide not to retransmit (in case
1036  * retransmission is implemented).
1037  *
1038  * This one assumes being called inside splimp()
1039  */
1040
1041 void
1042 cm_watchdog(ifp)
1043         struct ifnet *ifp;
1044 {
1045         struct cm_softc *sc = ifp->if_softc;
1046
1047         PUTREG(CMCMD, CM_TXDIS);
1048         return;
1049 }