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