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