Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / netif / cs / if_cs.c
1 /*
2  * Copyright (c) 1997,1998 Maxim Bolotin and Oleg Sharoiko.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28
29 /*
30  * $FreeBSD: src/sys/dev/cs/if_cs.c,v 1.19.2.1 2001/01/25 20:13:48 imp Exp $
31  * $DragonFly: src/sys/dev/netif/cs/if_cs.c,v 1.2 2003/06/17 04:28:23 dillon Exp $
32  *
33  * Device driver for Crystal Semiconductor CS8920 based ethernet
34  *   adapters. By Maxim Bolotin and Oleg Sharoiko, 27-April-1997
35  */
36
37 /*
38 #define  CS_DEBUG 
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50
51 #include <sys/module.h>
52 #include <sys/bus.h>
53 #include <machine/bus.h>
54 #include <sys/rman.h>
55 #include <machine/resource.h>
56 #include <machine/clock.h>
57
58 #include <net/if.h>
59 #include <net/if_arp.h>
60 #include <net/if_media.h>
61 #include <net/ethernet.h>
62 #include <net/bpf.h>
63
64 #include <dev/cs/if_csreg.h>
65 #include <dev/cs/if_csvar.h>
66
67 #ifdef  CS_USE_64K_DMA
68 #define CS_DMA_BUFFER_SIZE 65536
69 #else
70 #define CS_DMA_BUFFER_SIZE 16384
71 #endif
72
73 static int      cs_recv_delay = 570;
74 SYSCTL_INT(_machdep, OID_AUTO, cs_recv_delay, CTLFLAG_RW, &cs_recv_delay, 0, "");
75
76 static void     cs_init         (void *);
77 static int      cs_ioctl        (struct ifnet *, u_long, caddr_t);
78 static void     cs_start        (struct ifnet *);
79 static void     cs_stop         (struct cs_softc *);
80 static void     cs_reset        (struct cs_softc *);
81 static void     cs_watchdog     (struct ifnet *);
82
83 static int      cs_mediachange  (struct ifnet *);
84 static void     cs_mediastatus  (struct ifnet *, struct ifmediareq *);
85 static int      cs_mediaset     (struct cs_softc *, int);
86
87 static void     cs_write_mbufs(struct cs_softc*, struct mbuf*);
88 static void     cs_xmit_buf(struct cs_softc*);
89 static int      cs_get_packet(struct cs_softc*);
90 static void     cs_setmode(struct cs_softc*);
91
92 static int      get_eeprom_data(struct cs_softc *sc, int, int, int *);
93 static int      get_eeprom_cksum(int, int, int *);
94 static int      wait_eeprom_ready( struct cs_softc *);
95 static void     control_dc_dc( struct cs_softc *, int );
96 static int      send_test_pkt( struct cs_softc * );
97 static int      enable_tp(struct cs_softc *);
98 static int      enable_aui(struct cs_softc *);
99 static int      enable_bnc(struct cs_softc *);
100 static int      cs_duplex_auto(struct cs_softc *);
101
102 devclass_t cs_devclass;
103
104 static int
105 get_eeprom_data( struct cs_softc *sc, int off, int len, int *buffer)
106 {
107         int i;
108
109 #ifdef CS_DEBUG
110         printf(CS_NAME":EEPROM data from %x for %x:\n", off,len);
111 #endif
112
113         for (i=0;i<len;i++) {
114                 if (wait_eeprom_ready(sc) < 0) return -1;
115                 /* Send command to EEPROM to read */
116                 cs_writereg(sc->nic_addr, PP_EECMD, (off+i)|EEPROM_READ_CMD );
117                 if (wait_eeprom_ready(sc)<0)
118                         return -1;
119                 buffer[i] = cs_readreg (sc->nic_addr, PP_EEData);
120
121 #ifdef CS_DEBUG
122                 printf("%02x %02x ",(unsigned char)buffer[i],
123                                         (unsigned char)buffer[i+1]);
124 #endif
125         }
126
127 #ifdef CS_DEBUG
128         printf("\n");
129 #endif
130
131         return 0;
132 }
133
134 static int
135 get_eeprom_cksum(int off, int len, int *buffer)
136 {
137         int i,cksum=0;
138
139         for (i=0;i<len;i++)
140                 cksum+=buffer[i];
141         cksum &= 0xffff;
142         if (cksum==0)
143                 return 0;
144         return -1;
145 }
146
147 static int
148 wait_eeprom_ready(struct cs_softc *sc)
149 {
150         DELAY ( 30000 );        /* XXX should we do some checks here ? */
151         return 0;
152 }
153
154 static void
155 control_dc_dc(struct cs_softc *sc, int on_not_off)
156 {
157         unsigned int self_control = HCB1_ENBL;
158
159         if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
160                 self_control |= HCB1;
161         else
162                 self_control &= ~HCB1;
163         cs_writereg( sc->nic_addr, PP_SelfCTL, self_control );
164
165         DELAY( 500000 );
166 }
167
168
169 static int
170 cs_duplex_auto(struct cs_softc *sc)
171 {
172         int i, error=0, unit=sc->arpcom.ac_if.if_unit;
173         
174         cs_writereg(sc->nic_addr, PP_AutoNegCTL,
175                     RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE );
176         for (i=0; cs_readreg(sc->nic_addr,PP_AutoNegST)&AUTO_NEG_BUSY; i++) {
177                 if (i > 40000) {
178                         printf(CS_NAME"%1d: full/half duplex "
179                                "auto negotiation timeout\n", unit);
180                         error = ETIMEDOUT;
181                         break;
182                 }
183                 DELAY(1000);
184         }
185         DELAY( 1000000 );
186         return error;
187 }
188
189 static int
190 enable_tp(struct cs_softc *sc)
191 {
192         int unit = sc->arpcom.ac_if.if_unit;
193
194         cs_writereg(sc->nic_addr, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
195         control_dc_dc(sc, 0);
196         DELAY( 150000 );
197
198         if ((cs_readreg(sc->nic_addr, PP_LineST) & LINK_OK)==0) {
199                 printf(CS_NAME"%1d: failed to enable TP\n", unit);
200                 return EINVAL;
201         }
202
203         return 0;
204 }
205
206 /*
207  * XXX This was rewritten from Linux driver without any tests.
208  */             
209 static int
210 send_test_pkt(struct cs_softc *sc)
211 {
212         char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
213                                 0, 46,  /* A 46 in network order */
214                                 0, 0,   /* DSAP=0 & SSAP=0 fields */
215                                 0xf3, 0 /* Control (Test Req + P bit set) */ };
216         int i;
217         u_char ether_address_backup[ETHER_ADDR_LEN];
218
219         for (i = 0; i < ETHER_ADDR_LEN; i++) {
220                 ether_address_backup[i] = sc->arpcom.ac_enaddr[i];
221         }
222
223         cs_writereg(sc->nic_addr, PP_LineCTL,
224                 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_TX_ON );
225         bcopy(test_packet,
226                         sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
227         bcopy(test_packet+ETHER_ADDR_LEN,
228                         sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
229         outw(sc->nic_addr + TX_CMD_PORT, sc->send_cmd);
230         outw(sc->nic_addr + TX_LEN_PORT, sizeof(test_packet));
231
232         /* Wait for chip to allocate memory */
233         DELAY(50000);
234         if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
235                 for (i = 0; i < ETHER_ADDR_LEN; i++) {
236                         sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
237                 }
238                 return 0;
239         }
240
241         outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet));
242
243         DELAY(30000);
244
245         if ((cs_readreg(sc->nic_addr,PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
246                 for (i = 0; i < ETHER_ADDR_LEN; i++) {
247                         sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
248                 }
249                 return 1;
250         }
251         for (i = 0; i < ETHER_ADDR_LEN; i++) {
252                 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
253         }
254         return 0;
255 }
256
257 /*
258  * XXX This was rewritten from Linux driver without any tests.
259  */
260 static int
261 enable_aui(struct cs_softc *sc)
262 {
263         int unit = sc->arpcom.ac_if.if_unit;
264
265         control_dc_dc(sc, 0);
266         cs_writereg(sc->nic_addr, PP_LineCTL,
267                 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
268
269         if (!send_test_pkt(sc)) {
270                 printf(CS_NAME"%1d failed to enable AUI\n", unit);
271                 return EINVAL;
272         }
273         return 0;
274 }
275
276 /*
277  * XXX This was rewritten from Linux driver without any tests.
278  */             
279 static int
280 enable_bnc(struct cs_softc *sc)
281 {
282         int unit = sc->arpcom.ac_if.if_unit;
283
284         control_dc_dc(sc, 1);
285         cs_writereg(sc->nic_addr, PP_LineCTL,
286                 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
287
288         if (!send_test_pkt(sc)) {
289                 printf(CS_NAME"%1d failed to enable BNC\n", unit);
290                 return EINVAL;
291         }
292         return 0;
293 }
294
295 int
296 cs_cs89x0_probe(device_t dev)
297 {
298         int i;
299         int iobase;
300         int error;
301
302         u_long irq, junk;
303
304         struct cs_softc *sc = device_get_softc(dev);
305
306         unsigned rev_type = 0;
307         char chip_revision;
308         int eeprom_buff[CHKSUM_LEN];
309         int chip_type, pp_isaint, pp_isadma;
310
311         error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
312         if (error)
313                 return (error);
314
315         iobase=rman_get_start(sc->port_res);
316
317         if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG) {
318                 /* Chip not detected. Let's try to reset it */
319                 if (bootverbose)
320                         device_printf(dev, "trying to reset the chip.\n");
321                 outw(iobase+ADD_PORT, PP_SelfCTL);
322                 i = inw(iobase+DATA_PORT);
323                 outw(iobase+ADD_PORT, PP_SelfCTL);
324                 outw(iobase+DATA_PORT, i | POWER_ON_RESET);
325                 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG)
326                         return (ENXIO);
327         }
328
329         outw(iobase+ADD_PORT, PP_ChipID);
330         if (inw(iobase+DATA_PORT) != CHIP_EISA_ID_SIG)
331                 return (ENXIO);
332
333         rev_type = cs_readreg(iobase, PRODUCT_ID_ADD);
334         chip_type = rev_type & ~REVISON_BITS;
335         chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
336
337         sc->nic_addr = iobase;
338         sc->chip_type = chip_type;
339
340         if(chip_type==CS8900) {
341                 pp_isaint = PP_CS8900_ISAINT;
342                 pp_isadma = PP_CS8900_ISADMA;
343                 sc->send_cmd = TX_CS8900_AFTER_ALL;
344         } else {
345                 pp_isaint = PP_CS8920_ISAINT;
346                 pp_isadma = PP_CS8920_ISADMA;
347                 sc->send_cmd = TX_CS8920_AFTER_ALL;
348         }
349
350         /*
351          * Clear some fields so that fail of EEPROM will left them clean
352          */
353         sc->auto_neg_cnf = 0;
354         sc->adapter_cnf  = 0;
355         sc->isa_config   = 0;
356         
357         /*
358          * If no interrupt specified (or "?"), use what the board tells us.
359          */
360         error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
361
362         /*
363          * Get data from EEPROM
364          */
365         if((cs_readreg(iobase, PP_SelfST) & EEPROM_PRESENT) == 0) {
366                 device_printf(dev, "No EEPROM, assuming defaults.\n");
367         } else {
368                 if (get_eeprom_data(sc,START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
369                         device_printf(dev, "EEPROM read failed, "
370                                 "assuming defaults.\n");
371                 } else {
372                         if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN, eeprom_buff)<0) {
373                                 device_printf(dev, "EEPROM cheksum bad, "
374                                         "assuming defaults.\n");
375                         } else {
376                                 sc->auto_neg_cnf =
377                                         eeprom_buff[AUTO_NEG_CNF_OFFSET/2];
378                                 sc->adapter_cnf =
379                                         eeprom_buff[ADAPTER_CNF_OFFSET/2];
380                                 sc->isa_config =
381                                         eeprom_buff[ISA_CNF_OFFSET/2];
382
383                                 for (i=0; i<ETHER_ADDR_LEN/2; i++) {
384                                         sc->arpcom.ac_enaddr[i*2]=
385                                                 eeprom_buff[i];
386                                         sc->arpcom.ac_enaddr[i*2+1]=
387                                                 eeprom_buff[i] >> 8;
388                                 }
389
390                                 /*
391                                  * If no interrupt specified (or "?"),
392                                  * use what the board tells us.
393                                  */
394                                 if (error) {
395                                         irq = sc->isa_config & INT_NO_MASK;
396                                         if (chip_type==CS8900) {
397                                                 switch(irq) {
398                                                  case 0:
399                                                         irq=10;
400                                                         error=0;
401                                                         break;
402                                                  case 1:
403                                                         irq=11;
404                                                         error=0;
405                                                         break;
406                                                  case 2:
407                                                         irq=12;
408                                                         error=0;
409                                                         break;
410                                                  case 3:
411                                                         irq=5;
412                                                         error=0;
413                                                         break;
414                                                  default:
415                                                         device_printf(dev, "invalid irq in EEPROM.\n");
416                                                         error=EINVAL;
417                                                 }
418                                         } else {
419                                                 if (irq>CS8920_NO_INTS) {
420                                                         device_printf(dev, "invalid irq in EEPROM.\n");
421                                                         error=EINVAL;
422                                                 } else {
423                                                         error=0;
424                                                 }
425                                         }
426
427                                         if (!error)
428                                                 bus_set_resource(dev, SYS_RES_IRQ, 0,
429                                                                 irq, 1);
430                                 }
431                         }
432                 }
433         }
434
435         if (!error) {
436                 if (chip_type == CS8900) {
437                         switch(irq) {
438                                 case  5:
439                                         irq = 3;
440                                         break;
441                                 case 10:
442                                         irq = 0;
443                                         break;
444                                 case 11:
445                                         irq = 1;
446                                         break;
447                                 case 12:
448                                         irq = 2;
449                                         break;
450                                 default:
451                                         error=EINVAL;
452                         }
453                 } else {
454                         if (irq > CS8920_NO_INTS) {
455                                 error = EINVAL;
456                         }
457                 }
458         }
459
460         if (!error) {
461                 cs_writereg(iobase, pp_isaint, irq);
462         } else {
463                 device_printf(dev, "Unknown or invalid irq\n");
464                 return (ENXIO);
465         }
466         
467         /*
468          * Temporary disabled
469          *
470         if (drq>0)
471                 cs_writereg(iobase, pp_isadma, drq);
472         else {
473                 printf( CS_NAME"%1d: incorrect drq\n", unit );
474                 return 0;
475         }
476         */
477
478         if (bootverbose)
479                  device_printf(dev, "CS89%c0%s rev %c media%s%s%s\n",
480                         chip_type==CS8900 ? '0' : '2',
481                         chip_type==CS8920M ? "M" : "",
482                         chip_revision,
483                         (sc->adapter_cnf & A_CNF_10B_T) ? " TP"  : "",
484                         (sc->adapter_cnf & A_CNF_AUI)   ? " AUI" : "",
485                         (sc->adapter_cnf & A_CNF_10B_2) ? " BNC" : "");
486
487         if ((sc->adapter_cnf & A_CNF_EXTND_10B_2) &&
488             (sc->adapter_cnf & A_CNF_LOW_RX_SQUELCH))
489                 sc->line_ctl = LOW_RX_SQUELCH;
490         else
491                 sc->line_ctl = 0;
492
493         
494         return 0;
495 }
496
497 /*
498  * Allocate a port resource with the given resource id.
499  */
500 int cs_alloc_port(device_t dev, int rid, int size)
501 {
502         struct cs_softc *sc = device_get_softc(dev);
503         struct resource *res;
504
505         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
506                                  0ul, ~0ul, size, RF_ACTIVE);
507         if (res) {
508                 sc->port_rid = rid;
509                 sc->port_res = res;
510                 sc->port_used = size;
511                 return (0);
512         } else {
513                 return (ENOENT);
514         }
515 }
516
517 /*
518  * Allocate a memory resource with the given resource id.
519  */
520 int cs_alloc_memory(device_t dev, int rid, int size)
521 {
522         struct cs_softc *sc = device_get_softc(dev);
523         struct resource *res;
524
525         res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
526                                  0ul, ~0ul, size, RF_ACTIVE);
527         if (res) {
528                 sc->mem_rid = rid;
529                 sc->mem_res = res;
530                 sc->mem_used = size;
531                 return (0);
532         } else {
533                 return (ENOENT);
534         }
535 }
536
537 /*
538  * Allocate an irq resource with the given resource id.
539  */
540 int cs_alloc_irq(device_t dev, int rid, int flags)
541 {
542         struct cs_softc *sc = device_get_softc(dev);
543         struct resource *res;
544
545         res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
546                                  0ul, ~0ul, 1, (RF_ACTIVE | flags));
547         if (res) {
548                 sc->irq_rid = rid;
549                 sc->irq_res = res;
550                 return (0);
551         } else {
552                 return (ENOENT);
553         }
554 }
555
556 /*
557  * Release all resources
558  */
559 void cs_release_resources(device_t dev)
560 {
561         struct cs_softc *sc = device_get_softc(dev);
562
563         if (sc->port_res) {
564                 bus_release_resource(dev, SYS_RES_IOPORT,
565                                      sc->port_rid, sc->port_res);
566                 sc->port_res = 0;
567         }
568         if (sc->mem_res) {
569                 bus_release_resource(dev, SYS_RES_MEMORY,
570                                      sc->mem_rid, sc->mem_res);
571                 sc->mem_res = 0;
572         }
573         if (sc->irq_res) {
574                 bus_release_resource(dev, SYS_RES_IRQ,
575                                      sc->irq_rid, sc->irq_res);
576                 sc->irq_res = 0;
577         }
578 }
579
580 /*
581  * Install the interface into kernel networking data structures
582  */
583 int
584 cs_attach(struct cs_softc *sc, int unit, int flags)
585 {
586         int media=0;
587         struct ifnet *ifp = &(sc->arpcom.ac_if);
588
589         cs_stop( sc );
590
591         if (!ifp->if_name) {
592                 ifp->if_softc=sc;
593                 ifp->if_unit=unit;
594                 ifp->if_name="cs";
595                 ifp->if_output=ether_output;
596                 ifp->if_start=cs_start;
597                 ifp->if_ioctl=cs_ioctl;
598                 ifp->if_watchdog=cs_watchdog;
599                 ifp->if_init=cs_init;
600                 ifp->if_snd.ifq_maxlen= IFQ_MAXLEN;
601                 /*
602                  *  MIB DATA
603                  */
604                 /*
605                 ifp->if_linkmib=&sc->mibdata;
606                 ifp->if_linkmiblen=sizeof sc->mibdata;
607                 */
608
609                 ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST );
610
611                 /*
612                  * this code still in progress (DMA support)
613                  *
614
615                 sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_NOWAIT);
616                 if (sc->recv_ring == NULL) {
617                         log(LOG_ERR,CS_NAME
618                         "%d: Couldn't allocate memory for NIC\n", unit);
619                         return(0);
620                 }
621                 if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
622                     < (128*1024-CS_DMA_BUFFER_SIZE))
623                     sc->recv_ring+=16*1024;
624
625                 */
626
627                 sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_NOWAIT);
628                 if (sc->buffer == NULL) {
629                         printf(CS_NAME"%d: Couldn't allocate memory for NIC\n",
630                                unit);
631                         return(0);
632                 }
633
634                 /*
635                  * Initialize the media structures.
636                  */
637                 ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
638
639                 if (sc->adapter_cnf & A_CNF_10B_T) {
640                         ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
641                         if (sc->chip_type != CS8900) {
642                                 ifmedia_add(&sc->media,
643                                         IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
644                                 ifmedia_add(&sc->media,
645                                         IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
646                         }
647                 } 
648
649                 if (sc->adapter_cnf & A_CNF_10B_2)
650                         ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
651
652                 if (sc->adapter_cnf & A_CNF_AUI)
653                         ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
654
655                 if (sc->adapter_cnf & A_CNF_MEDIA)
656                         ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
657
658                 /* Set default media from EEPROM */
659                 switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
660                 case A_CNF_MEDIA_AUTO:  media = IFM_ETHER|IFM_AUTO; break;
661                 case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
662                 case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
663                 case A_CNF_MEDIA_AUI:   media = IFM_ETHER|IFM_10_5; break;
664                 default: printf(CS_NAME"%d: adapter has no media\n", unit);
665                 }
666                 ifmedia_set(&sc->media, media);
667                 cs_mediaset(sc, media);
668
669                 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
670         }
671
672         if (bootverbose)
673                 printf(CS_NAME"%d: ethernet address %6D\n",
674                        ifp->if_unit, sc->arpcom.ac_enaddr, ":");
675
676         return (0);
677 }
678
679 /*
680  * Initialize the board
681  */
682 static void
683 cs_init(void *xsc)
684 {
685         struct cs_softc *sc=(struct cs_softc *)xsc;
686         struct ifnet *ifp = &sc->arpcom.ac_if;
687         int i, s, rx_cfg;
688
689         /* address not known */
690         if (TAILQ_EMPTY(&ifp->if_addrhead)) /* unlikely? XXX */
691                 return;
692
693         /*
694          * reset whatchdog timer
695          */
696         ifp->if_timer=0;
697         sc->buf_len = 0;
698         
699         s=splimp();
700
701         /*
702          * Hardware initialization of cs
703          */
704
705         /* Enable receiver and transmitter */
706         cs_writereg(sc->nic_addr, PP_LineCTL,
707                 cs_readreg( sc->nic_addr, PP_LineCTL ) |
708                 SERIAL_RX_ON | SERIAL_TX_ON);
709
710         /* Configure the receiver mode */
711         cs_setmode(sc);
712
713         /*
714          * This defines what type of frames will cause interrupts
715          * Bad frames should generate interrupts so that the driver
716          * could track statistics of discarded packets
717          */
718         rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
719                  RX_EXTRA_DATA_ENBL;
720         if (sc->isa_config & STREAM_TRANSFER)
721                 rx_cfg |= RX_STREAM_ENBL;
722         cs_writereg(sc->nic_addr, PP_RxCFG, rx_cfg);
723
724         cs_writereg(sc->nic_addr, PP_TxCFG, TX_LOST_CRS_ENBL |
725                     TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
726                     TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
727
728         cs_writereg(sc->nic_addr, PP_BufCFG, READY_FOR_TX_ENBL |
729                     RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
730                     TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
731
732         /* Write MAC address into IA filter */
733         for (i=0; i<ETHER_ADDR_LEN/2; i++)
734                 cs_writereg(sc->nic_addr, PP_IA+i*2,
735                             sc->arpcom.ac_enaddr[i*2] |
736                             (sc->arpcom.ac_enaddr[i*2+1] << 8) );
737
738         /*
739          * Now enable everything
740          */
741 /*
742 #ifdef  CS_USE_64K_DMA
743         cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
744         #else
745
746         cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
747 #endif
748 */
749         cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
750         
751         /*
752          * Set running and clear output active flags
753          */
754         sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
755         sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
756
757         /*
758          * Start sending process
759          */
760         cs_start(ifp);
761
762         (void) splx(s);
763 }
764
765 /*
766  * Get the packet from the board and send it to the upper layer
767  * via ether_input().
768  */
769 static int
770 cs_get_packet(struct cs_softc *sc)
771 {
772         struct ifnet *ifp = &(sc->arpcom.ac_if);
773         int iobase = sc->nic_addr, status, length;
774         struct ether_header *eh;
775         struct mbuf *m;
776
777 #ifdef CS_DEBUG
778         int i;
779 #endif
780
781         status = inw(iobase + RX_FRAME_PORT);
782         length = inw(iobase + RX_FRAME_PORT);
783
784 #ifdef CS_DEBUG
785         printf(CS_NAME"%1d: rcvd: stat %x, len %d\n",
786                 ifp->if_unit, status, length);
787 #endif
788
789         if (!(status & RX_OK)) {
790 #ifdef CS_DEBUG
791                 printf(CS_NAME"%1d: bad pkt stat %x\n", ifp->if_unit, status);
792 #endif
793                 ifp->if_ierrors++;
794                 return -1;
795         }
796
797         MGETHDR(m, M_DONTWAIT, MT_DATA);
798         if (m==NULL)
799                 return -1;
800
801         if (length > MHLEN) {
802                 MCLGET(m, M_DONTWAIT);
803                 if (!(m->m_flags & M_EXT)) {
804                         m_freem(m);
805                         return -1;
806                 }
807         }
808
809         /* Initialize packet's header info */
810         m->m_pkthdr.rcvif = ifp;
811         m->m_pkthdr.len = length;
812         m->m_len = length;
813
814         /* Get the data */
815         insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
816
817         eh = mtod(m, struct ether_header *);
818
819 #ifdef CS_DEBUG
820         for (i=0;i<length;i++)
821              printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
822         printf( "\n" );
823 #endif
824
825         if (status & (RX_IA | RX_BROADCAST) || 
826             (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
827                 m->m_pkthdr.len -= sizeof(struct ether_header);
828                 m->m_len -= sizeof(struct ether_header);
829                 m->m_data += sizeof(struct ether_header);
830
831                 /* Feed the packet to the upper layer */
832                 ether_input(ifp, eh, m);
833
834                 ifp->if_ipackets++;
835
836                 if (length==ETHER_MAX_LEN-ETHER_CRC_LEN)
837                         DELAY( cs_recv_delay );
838         } else {
839                 m_freem(m);
840         }
841
842         return 0;
843 }
844
845 /*
846  * Handle interrupts
847  */
848 void
849 csintr(void *arg)
850 {
851         struct cs_softc *sc = (struct cs_softc*) arg;
852         struct ifnet *ifp = &(sc->arpcom.ac_if);
853         int status;
854
855 #ifdef CS_DEBUG
856         int unit = ifp->if_unit;
857         printf(CS_NAME"%1d: Interrupt.\n", unit);
858 #endif
859
860         while ((status=cs_readword(sc->nic_addr, ISQ_PORT))) {
861
862 #ifdef CS_DEBUG
863                 printf( CS_NAME"%1d:from ISQ: %04x\n", unit, status );
864 #endif
865
866                 switch (status & ISQ_EVENT_MASK) {
867                 case ISQ_RECEIVER_EVENT:
868                         cs_get_packet(sc);
869                         break;
870
871                 case ISQ_TRANSMITTER_EVENT:
872                         if (status & TX_OK)
873                                 ifp->if_opackets++;
874                         else
875                                 ifp->if_oerrors++;
876                         ifp->if_flags &= ~IFF_OACTIVE;
877                         ifp->if_timer = 0;
878                         break;
879
880                 case ISQ_BUFFER_EVENT:
881                         if (status & READY_FOR_TX) {
882                                 ifp->if_flags &= ~IFF_OACTIVE;
883                                 ifp->if_timer = 0;
884                         }
885
886                         if (status & TX_UNDERRUN) {
887                                 ifp->if_flags &= ~IFF_OACTIVE;
888                                 ifp->if_timer = 0;
889                                 ifp->if_oerrors++;
890                         }
891                         break;
892
893                 case ISQ_RX_MISS_EVENT:
894                         ifp->if_ierrors+=(status>>6);
895                         break;
896
897                 case ISQ_TX_COL_EVENT:
898                         ifp->if_collisions+=(status>>6);
899                         break;
900                 }
901         }
902
903         if (!(ifp->if_flags & IFF_OACTIVE)) {
904                 cs_start(ifp);
905         }
906 }
907
908 /*
909  * Save the data in buffer
910  */
911
912 static void
913 cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
914 {
915         int len;
916         struct mbuf *mp;
917         unsigned char *data, *buf;
918
919         for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
920                 len = mp->m_len;
921
922                 /*
923                  * Ignore empty parts
924                  */
925                 if (!len)
926                 continue;
927
928                 /*
929                  * Find actual data address
930                  */
931                 data = mtod(mp, caddr_t);
932
933                 bcopy((caddr_t) data, (caddr_t) buf, len);
934                 buf += len;
935                 sc->buf_len += len;
936         }
937 }
938
939
940 static void
941 cs_xmit_buf( struct cs_softc *sc )
942 {
943         outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
944         sc->buf_len = 0;
945 }
946
947 static void
948 cs_start(struct ifnet *ifp)
949 {
950         int s, length;
951         struct mbuf *m, *mp;
952         struct cs_softc *sc = ifp->if_softc;
953
954         s = splimp();
955
956         for (;;) {
957                 if (sc->buf_len)
958                         length = sc->buf_len;
959                 else {
960                         IF_DEQUEUE( &ifp->if_snd, m );
961
962                         if (m==NULL) {
963                                 (void) splx(s);
964                                 return;
965                         }
966
967                         for (length=0, mp=m; mp != NULL; mp=mp->m_next)
968                                 length += mp->m_len;
969
970                         /* Skip zero-length packets */
971                         if (length == 0) {
972                                 m_freem(m);
973                                 continue;
974                         }
975
976                         cs_write_mbufs(sc, m);
977
978                         if (ifp->if_bpf) {
979                                 bpf_mtap(ifp, m);
980                         }
981
982                         m_freem(m);
983                 }
984
985                 /*
986                  * Issue a SEND command
987                  */
988                 outw(sc->nic_addr+TX_CMD_PORT, sc->send_cmd);
989                 outw(sc->nic_addr+TX_LEN_PORT, length );
990
991                 /*
992                  * If there's no free space in the buffer then leave
993                  * this packet for the next time: indicate output active
994                  * and return.
995                  */
996                 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
997                         ifp->if_timer = sc->buf_len;
998                         (void) splx(s);
999                         ifp->if_flags |= IFF_OACTIVE;
1000                         return;
1001                 }
1002
1003                 cs_xmit_buf(sc);
1004
1005                 /*
1006                  * Set the watchdog timer in case we never hear
1007                  * from board again. (I don't know about correct
1008                  * value for this timeout)
1009                  */
1010                 ifp->if_timer = length;
1011
1012                 (void) splx(s);
1013                 ifp->if_flags |= IFF_OACTIVE;
1014                 return;
1015         }
1016 }
1017
1018 /*
1019  * Stop everything on the interface
1020  */
1021 static void
1022 cs_stop(struct cs_softc *sc)
1023 {
1024         int s = splimp();
1025
1026         cs_writereg(sc->nic_addr, PP_RxCFG, 0);
1027         cs_writereg(sc->nic_addr, PP_TxCFG, 0);
1028         cs_writereg(sc->nic_addr, PP_BufCFG, 0);
1029         cs_writereg(sc->nic_addr, PP_BusCTL, 0);
1030
1031         sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1032         sc->arpcom.ac_if.if_timer = 0;
1033
1034         (void) splx(s);
1035 }
1036
1037 /*
1038  * Reset the interface
1039  */
1040 static void
1041 cs_reset(struct cs_softc *sc)
1042 {
1043         cs_stop(sc);
1044         cs_init(sc);
1045 }
1046
1047 static void
1048 cs_setmode(struct cs_softc *sc)
1049 {
1050         struct ifnet *ifp = &(sc->arpcom.ac_if);
1051         int rx_ctl;
1052
1053         /* Stop the receiver while changing filters */
1054         cs_writereg(sc->nic_addr, PP_LineCTL, 
1055                         cs_readreg(sc->nic_addr, PP_LineCTL) & ~SERIAL_RX_ON);
1056
1057         if (ifp->if_flags & IFF_PROMISC) {
1058                 /* Turn on promiscuous mode. */
1059                 rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1060         } else {
1061                 if (ifp->if_flags & IFF_MULTICAST) {
1062                         /* Allow receiving frames with multicast addresses */
1063                         rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1064                                  RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1065                         /*
1066                          * Here the reconfiguration of chip's multicast
1067                          * filters should be done but I've no idea about
1068                          * hash transformation in this chip. If you can
1069                          * add this code or describe me the transformation
1070                          * I'd be very glad.
1071                          */
1072                 } else {
1073                         /*
1074                          * Receive only good frames addressed for us and
1075                          * good broadcasts.
1076                          */
1077                         rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1078                                  RX_OK_ACCEPT;
1079                 }
1080         }
1081
1082         /* Set up the filter */
1083         cs_writereg(sc->nic_addr, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1084
1085         /* Turn on receiver */
1086         cs_writereg(sc->nic_addr, PP_LineCTL,
1087                         cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_RX_ON);
1088 }
1089
1090 static int
1091 cs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
1092 {
1093         struct cs_softc *sc=ifp->if_softc;
1094         struct ifreq *ifr = (struct ifreq *)data;
1095         int s,error=0;
1096
1097 #ifdef CS_DEBUG
1098         printf(CS_NAME"%d: ioctl(%lx)\n",sc->arpcom.ac_if.if_unit,command);
1099 #endif
1100
1101         s=splimp();
1102
1103         switch (command) {
1104         case SIOCSIFADDR:
1105         case SIOCGIFADDR:
1106         case SIOCSIFMTU:
1107                 ether_ioctl(ifp, command, data);
1108                 break;
1109
1110         case SIOCSIFFLAGS:
1111                 /*
1112                  * Switch interface state between "running" and
1113                  * "stopped", reflecting the UP flag.
1114                  */
1115                 if (sc->arpcom.ac_if.if_flags & IFF_UP) {
1116                         if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
1117                                 cs_init(sc);
1118                         }
1119                 } else {
1120                         if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
1121                                 cs_stop(sc);
1122                         }
1123                 }
1124                 /*
1125                  * Promiscuous and/or multicast flags may have changed,
1126                  * so reprogram the multicast filter and/or receive mode.
1127                  *
1128                  * See note about multicasts in cs_setmode
1129                  */
1130                 cs_setmode(sc);
1131                 break;
1132
1133         case SIOCADDMULTI:
1134         case SIOCDELMULTI:
1135             /*
1136              * Multicast list has changed; set the hardware filter
1137              * accordingly.
1138              *
1139              * See note about multicasts in cs_setmode
1140              */
1141             cs_setmode(sc);
1142             error = 0;
1143             break;
1144
1145         case SIOCSIFMEDIA:
1146         case SIOCGIFMEDIA:
1147                 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1148                 break;
1149
1150         default:
1151                 error = EINVAL;
1152         }
1153
1154         (void) splx(s);
1155         return error;
1156 }
1157
1158 /*
1159  * Device timeout/watchdog routine. Entered if the device neglects to
1160  * generate an interrupt after a transmit has been started on it.
1161  */
1162 static void
1163 cs_watchdog(struct ifnet *ifp)
1164 {
1165         struct cs_softc *sc = ifp->if_softc;
1166
1167         ifp->if_oerrors++;
1168         log(LOG_ERR, CS_NAME"%d: device timeout\n", ifp->if_unit);
1169
1170         /* Reset the interface */
1171         if (ifp->if_flags & IFF_UP)
1172                 cs_reset(sc);
1173         else
1174                 cs_stop(sc);
1175 }
1176
1177 static int
1178 cs_mediachange(struct ifnet *ifp)
1179 {
1180         struct cs_softc *sc = ifp->if_softc;
1181         struct ifmedia *ifm = &sc->media;
1182
1183         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1184                 return EINVAL;
1185
1186         return cs_mediaset(sc, ifm->ifm_media);
1187 }
1188
1189 static void
1190 cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1191 {
1192         int line_status;
1193         struct cs_softc *sc = ifp->if_softc;
1194
1195         ifmr->ifm_active = IFM_ETHER;
1196         line_status = cs_readreg(sc->nic_addr, PP_LineST);
1197         if (line_status & TENBASET_ON) {
1198                 ifmr->ifm_active |= IFM_10_T;
1199                 if (sc->chip_type != CS8900) {
1200                         if (cs_readreg(sc->nic_addr, PP_AutoNegST) & FDX_ACTIVE)
1201                                 ifmr->ifm_active |= IFM_FDX;
1202                         if (cs_readreg(sc->nic_addr, PP_AutoNegST) & HDX_ACTIVE)
1203                                 ifmr->ifm_active |= IFM_HDX;
1204                 }
1205                 ifmr->ifm_status = IFM_AVALID;
1206                 if (line_status & LINK_OK)
1207                         ifmr->ifm_status |= IFM_ACTIVE;
1208         } else {
1209                 if (line_status & AUI_ON) {
1210                         cs_writereg(sc->nic_addr, PP_SelfCTL,
1211                                     cs_readreg(sc->nic_addr, PP_SelfCTL) |
1212                                     HCB1_ENBL);
1213                         if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1214                             (cs_readreg(sc->nic_addr, PP_SelfCTL)&HCB1))
1215                                 ifmr->ifm_active |= IFM_10_2;
1216                         else
1217                                 ifmr->ifm_active |= IFM_10_5;
1218                 }
1219         }
1220 }
1221
1222 static int
1223 cs_mediaset(struct cs_softc *sc, int media)
1224 {
1225         int error;
1226
1227         /* Stop the receiver & transmitter */
1228         cs_writereg(sc->nic_addr, PP_LineCTL,
1229                     cs_readreg(sc->nic_addr, PP_LineCTL) &
1230                     ~(SERIAL_RX_ON | SERIAL_TX_ON));
1231
1232 #ifdef CS_DEBUG
1233         printf(CS_NAME"%d: cs_setmedia(%x)\n",sc->arpcom.ac_if.if_unit,media);
1234 #endif
1235
1236         switch (IFM_SUBTYPE(media)) {
1237         default:
1238         case IFM_AUTO:
1239                 if ((error=enable_tp(sc))==0)
1240                         error = cs_duplex_auto(sc);
1241                 else if ((error=enable_bnc(sc)) != 0)
1242                         error = enable_aui(sc);
1243                 break;
1244         case IFM_10_T:
1245                 if ((error=enable_tp(sc)) != 0)
1246                         break;
1247                 if (media & IFM_FDX)
1248                         cs_duplex_full(sc);
1249                 else if (media & IFM_HDX)
1250                         cs_duplex_half(sc);
1251                 else
1252                         error = cs_duplex_auto(sc);
1253                 break;
1254         case IFM_10_2:
1255                 error = enable_bnc(sc);
1256                 break;
1257         case IFM_10_5:
1258                 error = enable_aui(sc);
1259                 break;
1260         }
1261
1262         /*
1263          * Turn the transmitter & receiver back on
1264          */
1265         cs_writereg(sc->nic_addr, PP_LineCTL,
1266                     cs_readreg( sc->nic_addr, PP_LineCTL ) |
1267                     SERIAL_RX_ON | SERIAL_TX_ON); 
1268
1269         return error;
1270 }