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