Make all network interrupt service routines MPSAFE part 1/3.
[dragonfly.git] / sys / dev / netif / cs / if_cs.c
CommitLineData
984263bc
MD
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 *
198d1293 27 * $FreeBSD: src/sys/dev/cs/if_cs.c,v 1.19.2.1 2001/01/25 20:13:48 imp Exp $
78195a76 28 * $DragonFly: src/sys/dev/netif/cs/if_cs.c,v 1.23 2005/11/28 17:13:41 dillon Exp $
984263bc
MD
29 */
30
31/*
984263bc
MD
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/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46#include <sys/kernel.h>
47#include <sys/sysctl.h>
48#include <sys/syslog.h>
96723634 49#include <sys/thread2.h>
984263bc
MD
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>
5eeabb60 59#include <net/ifq_var.h>
984263bc
MD
60#include <net/if_arp.h>
61#include <net/if_media.h>
62#include <net/ethernet.h>
63#include <net/bpf.h>
64
1f2de5d4
MD
65#include "if_csreg.h"
66#include "if_csvar.h"
984263bc
MD
67
68#ifdef CS_USE_64K_DMA
69#define CS_DMA_BUFFER_SIZE 65536
70#else
71#define CS_DMA_BUFFER_SIZE 16384
72#endif
73
74static int cs_recv_delay = 570;
75SYSCTL_INT(_machdep, OID_AUTO, cs_recv_delay, CTLFLAG_RW, &cs_recv_delay, 0, "");
76
bd4539cc
JH
77static void cs_init(void *);
78static int cs_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
79static void cs_start(struct ifnet *);
80static void cs_stop(struct cs_softc *);
81static void cs_reset(struct cs_softc *);
82static void cs_watchdog(struct ifnet *);
513da8e7 83static void csintr(void *);
984263bc 84
bd4539cc
JH
85static int cs_mediachange(struct ifnet *);
86static void cs_mediastatus(struct ifnet *, struct ifmediareq *);
87static int cs_mediaset(struct cs_softc *, int);
984263bc
MD
88
89static void cs_write_mbufs(struct cs_softc*, struct mbuf*);
90static void cs_xmit_buf(struct cs_softc*);
91static int cs_get_packet(struct cs_softc*);
92static void cs_setmode(struct cs_softc*);
93
754e3891 94static int get_eeprom_data(device_t, int, int, int *);
984263bc
MD
95static int get_eeprom_cksum(int, int, int *);
96static int wait_eeprom_ready( struct cs_softc *);
bd4539cc
JH
97static void control_dc_dc(struct cs_softc *, int );
98static int send_test_pkt(struct cs_softc * );
984263bc
MD
99static int enable_tp(struct cs_softc *);
100static int enable_aui(struct cs_softc *);
101static int enable_bnc(struct cs_softc *);
102static int cs_duplex_auto(struct cs_softc *);
103
104devclass_t cs_devclass;
105
32832096
MD
106DECLARE_DUMMY_MODULE(if_cs);
107
984263bc 108static int
754e3891 109get_eeprom_data(device_t dev, int off, int len, int *buffer)
984263bc 110{
754e3891 111 struct cs_softc *sc;
984263bc
MD
112 int i;
113
754e3891
SZ
114 sc = device_get_softc(dev);
115
984263bc 116#ifdef CS_DEBUG
754e3891 117 device_printf(dev, "EEPROM data from %x for %x:\n", off, len);
984263bc
MD
118#endif
119
120 for (i=0;i<len;i++) {
121 if (wait_eeprom_ready(sc) < 0) return -1;
122 /* Send command to EEPROM to read */
123 cs_writereg(sc->nic_addr, PP_EECMD, (off+i)|EEPROM_READ_CMD );
124 if (wait_eeprom_ready(sc)<0)
125 return -1;
126 buffer[i] = cs_readreg (sc->nic_addr, PP_EEData);
127
128#ifdef CS_DEBUG
129 printf("%02x %02x ",(unsigned char)buffer[i],
130 (unsigned char)buffer[i+1]);
131#endif
132 }
133
134#ifdef CS_DEBUG
135 printf("\n");
136#endif
137
138 return 0;
139}
140
141static int
142get_eeprom_cksum(int off, int len, int *buffer)
143{
144 int i,cksum=0;
145
146 for (i=0;i<len;i++)
147 cksum+=buffer[i];
148 cksum &= 0xffff;
149 if (cksum==0)
150 return 0;
151 return -1;
152}
153
154static int
155wait_eeprom_ready(struct cs_softc *sc)
156{
157 DELAY ( 30000 ); /* XXX should we do some checks here ? */
158 return 0;
159}
160
161static void
162control_dc_dc(struct cs_softc *sc, int on_not_off)
163{
164 unsigned int self_control = HCB1_ENBL;
165
166 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0) ^ on_not_off)
167 self_control |= HCB1;
168 else
169 self_control &= ~HCB1;
170 cs_writereg( sc->nic_addr, PP_SelfCTL, self_control );
171
172 DELAY( 500000 );
173}
174
175
176static int
177cs_duplex_auto(struct cs_softc *sc)
178{
3e4a09e7 179 int i, error=0;
984263bc
MD
180
181 cs_writereg(sc->nic_addr, PP_AutoNegCTL,
182 RE_NEG_NOW | ALLOW_FDX | AUTO_NEG_ENABLE );
183 for (i=0; cs_readreg(sc->nic_addr,PP_AutoNegST)&AUTO_NEG_BUSY; i++) {
184 if (i > 40000) {
754e3891
SZ
185 if_printf(&sc->arpcom.ac_if, "full/half duplex "
186 "auto negotiation timeout\n");
984263bc
MD
187 error = ETIMEDOUT;
188 break;
189 }
190 DELAY(1000);
191 }
192 DELAY( 1000000 );
193 return error;
194}
195
196static int
197enable_tp(struct cs_softc *sc)
198{
984263bc
MD
199 cs_writereg(sc->nic_addr, PP_LineCTL, sc->line_ctl & ~AUI_ONLY);
200 control_dc_dc(sc, 0);
201 DELAY( 150000 );
202
203 if ((cs_readreg(sc->nic_addr, PP_LineST) & LINK_OK)==0) {
754e3891 204 if_printf(&sc->arpcom.ac_if, "failed to enable TP\n");
984263bc
MD
205 return EINVAL;
206 }
207
208 return 0;
209}
210
211/*
212 * XXX This was rewritten from Linux driver without any tests.
213 */
214static int
215send_test_pkt(struct cs_softc *sc)
216{
217 char test_packet[] = { 0,0,0,0,0,0, 0,0,0,0,0,0,
218 0, 46, /* A 46 in network order */
219 0, 0, /* DSAP=0 & SSAP=0 fields */
220 0xf3, 0 /* Control (Test Req + P bit set) */ };
221 int i;
222 u_char ether_address_backup[ETHER_ADDR_LEN];
223
224 for (i = 0; i < ETHER_ADDR_LEN; i++) {
225 ether_address_backup[i] = sc->arpcom.ac_enaddr[i];
226 }
227
228 cs_writereg(sc->nic_addr, PP_LineCTL,
229 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_TX_ON );
230 bcopy(test_packet,
231 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
232 bcopy(test_packet+ETHER_ADDR_LEN,
233 sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
234 outw(sc->nic_addr + TX_CMD_PORT, sc->send_cmd);
235 outw(sc->nic_addr + TX_LEN_PORT, sizeof(test_packet));
236
237 /* Wait for chip to allocate memory */
238 DELAY(50000);
239 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
240 for (i = 0; i < ETHER_ADDR_LEN; i++) {
241 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
242 }
243 return 0;
244 }
245
246 outsw(sc->nic_addr + TX_FRAME_PORT, test_packet, sizeof(test_packet));
247
248 DELAY(30000);
249
250 if ((cs_readreg(sc->nic_addr,PP_TxEvent) & TX_SEND_OK_BITS) == TX_OK) {
251 for (i = 0; i < ETHER_ADDR_LEN; i++) {
252 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
253 }
254 return 1;
255 }
256 for (i = 0; i < ETHER_ADDR_LEN; i++) {
257 sc->arpcom.ac_enaddr[i] = ether_address_backup[i];
258 }
259 return 0;
260}
261
262/*
263 * XXX This was rewritten from Linux driver without any tests.
264 */
265static int
266enable_aui(struct cs_softc *sc)
267{
984263bc
MD
268 control_dc_dc(sc, 0);
269 cs_writereg(sc->nic_addr, PP_LineCTL,
270 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
271
272 if (!send_test_pkt(sc)) {
754e3891 273 if_printf(&sc->arpcom.ac_if, "failed to enable AUI\n");
984263bc
MD
274 return EINVAL;
275 }
276 return 0;
277}
278
279/*
280 * XXX This was rewritten from Linux driver without any tests.
281 */
282static int
283enable_bnc(struct cs_softc *sc)
284{
984263bc
MD
285 control_dc_dc(sc, 1);
286 cs_writereg(sc->nic_addr, PP_LineCTL,
287 (sc->line_ctl & ~AUTO_AUI_10BASET) | AUI_ONLY);
288
289 if (!send_test_pkt(sc)) {
754e3891 290 if_printf(&sc->arpcom.ac_if, "failed to enable BNC\n");
984263bc
MD
291 return EINVAL;
292 }
293 return 0;
294}
295
296int
297cs_cs89x0_probe(device_t dev)
298{
299 int i;
300 int iobase;
301 int error;
302
303 u_long irq, junk;
304
305 struct cs_softc *sc = device_get_softc(dev);
306
307 unsigned rev_type = 0;
308 char chip_revision;
309 int eeprom_buff[CHKSUM_LEN];
310 int chip_type, pp_isaint, pp_isadma;
311
312 error = cs_alloc_port(dev, 0, CS_89x0_IO_PORTS);
313 if (error)
314 return (error);
315
316 iobase=rman_get_start(sc->port_res);
317
318 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG) {
319 /* Chip not detected. Let's try to reset it */
320 if (bootverbose)
321 device_printf(dev, "trying to reset the chip.\n");
322 outw(iobase+ADD_PORT, PP_SelfCTL);
323 i = inw(iobase+DATA_PORT);
324 outw(iobase+ADD_PORT, PP_SelfCTL);
325 outw(iobase+DATA_PORT, i | POWER_ON_RESET);
326 if ((inw(iobase+ADD_PORT) & ADD_MASK) != ADD_SIG)
327 return (ENXIO);
328 }
329
330 outw(iobase+ADD_PORT, PP_ChipID);
331 if (inw(iobase+DATA_PORT) != CHIP_EISA_ID_SIG)
332 return (ENXIO);
333
334 rev_type = cs_readreg(iobase, PRODUCT_ID_ADD);
335 chip_type = rev_type & ~REVISON_BITS;
336 chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A';
337
338 sc->nic_addr = iobase;
339 sc->chip_type = chip_type;
340
341 if(chip_type==CS8900) {
342 pp_isaint = PP_CS8900_ISAINT;
343 pp_isadma = PP_CS8900_ISADMA;
344 sc->send_cmd = TX_CS8900_AFTER_ALL;
345 } else {
346 pp_isaint = PP_CS8920_ISAINT;
347 pp_isadma = PP_CS8920_ISADMA;
348 sc->send_cmd = TX_CS8920_AFTER_ALL;
349 }
350
351 /*
352 * Clear some fields so that fail of EEPROM will left them clean
353 */
354 sc->auto_neg_cnf = 0;
355 sc->adapter_cnf = 0;
356 sc->isa_config = 0;
357
358 /*
359 * If no interrupt specified (or "?"), use what the board tells us.
360 */
361 error = bus_get_resource(dev, SYS_RES_IRQ, 0, &irq, &junk);
362
363 /*
364 * Get data from EEPROM
365 */
366 if((cs_readreg(iobase, PP_SelfST) & EEPROM_PRESENT) == 0) {
367 device_printf(dev, "No EEPROM, assuming defaults.\n");
368 } else {
754e3891
SZ
369 if (get_eeprom_data(dev, START_EEPROM_DATA, CHKSUM_LEN,
370 eeprom_buff)<0) {
984263bc
MD
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 {
754e3891 475 device_printf(dev, "incorrect drq\n");
984263bc
MD
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 */
502int 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 */
522int 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 */
542int 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
4e6d744d
JS
547 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
548 (RF_ACTIVE | flags));
984263bc
MD
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 */
561void 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 */
585int
3e4a09e7 586cs_attach(device_t dev)
984263bc 587{
3e4a09e7 588 struct cs_softc *sc = device_get_softc(dev);
513da8e7 589 int media = 0, error;
78195a76 590 struct ifnet *ifp = &sc->arpcom.ac_if;
984263bc 591
513da8e7
SZ
592 /*
593 * Initialize the media structures.
594 */
595 ifmedia_init(&sc->media, 0, cs_mediachange, cs_mediastatus);
596
597 KASSERT(sc->port_used > 0 || sc->mem_used > 0,
598 ("%s: either I/O port or I/O memory should be used",
599 device_get_nameunit(dev)));
600 if (sc->port_used > 0)
601 cs_alloc_port(dev, sc->port_rid, sc->port_used);
602 if (sc->mem_used > 0)
603 cs_alloc_memory(dev, sc->mem_rid, sc->mem_used);
604
605 error = cs_alloc_irq(dev, sc->irq_rid, 0);
606 if (error) {
607 device_printf(dev, "Couldn't allocate irq");
608 goto bad;
609 }
610
754e3891 611 cs_stop(sc);
984263bc 612
3e4a09e7 613 ifp->if_softc=sc;
754e3891 614 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3e4a09e7
MD
615 ifp->if_start=cs_start;
616 ifp->if_ioctl=cs_ioctl;
617 ifp->if_watchdog=cs_watchdog;
618 ifp->if_init=cs_init;
5eeabb60
JS
619 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
620 ifq_set_ready(&ifp->if_snd);
513da8e7
SZ
621
622#if 0
3e4a09e7
MD
623 /*
624 * MIB DATA
625 */
3e4a09e7
MD
626 ifp->if_linkmib=&sc->mibdata;
627 ifp->if_linkmiblen=sizeof sc->mibdata;
513da8e7 628#endif
984263bc 629
3e4a09e7 630 ifp->if_flags=(IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST );
984263bc 631
513da8e7 632#if 0
3e4a09e7
MD
633 /*
634 * this code still in progress (DMA support)
513da8e7 635 */
c5541aee 636 sc->recv_ring=malloc(CS_DMA_BUFFER_SIZE<<1, M_DEVBUF, M_WAITOK);
3e4a09e7
MD
637 if ((sc->recv_ring-(sc->recv_ring & 0x1FFFF))
638 < (128*1024-CS_DMA_BUFFER_SIZE))
639 sc->recv_ring+=16*1024;
513da8e7 640#endif
984263bc 641
c5541aee 642 sc->buffer=malloc(ETHER_MAX_LEN-ETHER_CRC_LEN,M_DEVBUF,M_WAITOK);
984263bc 643
3e4a09e7
MD
644 if (sc->adapter_cnf & A_CNF_10B_T) {
645 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_T, 0, NULL);
646 if (sc->chip_type != CS8900) {
647 ifmedia_add(&sc->media,
648 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
649 ifmedia_add(&sc->media,
650 IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
984263bc 651 }
3e4a09e7 652 }
984263bc 653
3e4a09e7
MD
654 if (sc->adapter_cnf & A_CNF_10B_2)
655 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_2, 0, NULL);
984263bc 656
3e4a09e7
MD
657 if (sc->adapter_cnf & A_CNF_AUI)
658 ifmedia_add(&sc->media, IFM_ETHER|IFM_10_5, 0, NULL);
984263bc 659
3e4a09e7
MD
660 if (sc->adapter_cnf & A_CNF_MEDIA)
661 ifmedia_add(&sc->media, IFM_ETHER|IFM_AUTO, 0, NULL);
984263bc 662
3e4a09e7
MD
663 /* Set default media from EEPROM */
664 switch (sc->adapter_cnf & A_CNF_MEDIA_TYPE) {
665 case A_CNF_MEDIA_AUTO: media = IFM_ETHER|IFM_AUTO; break;
666 case A_CNF_MEDIA_10B_T: media = IFM_ETHER|IFM_10_T; break;
667 case A_CNF_MEDIA_10B_2: media = IFM_ETHER|IFM_10_2; break;
668 case A_CNF_MEDIA_AUI: media = IFM_ETHER|IFM_10_5; break;
754e3891 669 default: device_printf(dev, "adapter has no media\n");
3e4a09e7
MD
670 }
671 ifmedia_set(&sc->media, media);
672 cs_mediaset(sc, media);
984263bc 673
78195a76 674 ether_ifattach(ifp, sc->arpcom.ac_enaddr, NULL);
984263bc 675
78195a76
MD
676 error = bus_setup_intr(dev, sc->irq_res, INTR_NETSAFE,
677 csintr, sc, &sc->irq_handle,
678 ifp->if_serializer);
513da8e7
SZ
679 if (error) {
680 device_printf(dev, "Couldn't set up irq");
681 ether_ifdetach(ifp);
682 goto bad;
683 }
684
685 return 0;
686
687bad:
688 cs_detach(dev);
689 return error;
690}
691
692int
693cs_detach(device_t dev)
694{
695 struct cs_softc *sc = device_get_softc(dev);
78195a76 696 struct ifnet *ifp = &sc->arpcom.ac_if;
513da8e7 697
78195a76 698 lwkt_serialize_enter(ifp->if_serializer);
513da8e7
SZ
699
700 if (device_is_attached(dev)) {
701 cs_stop(sc);
702 ether_ifdetach(&sc->arpcom.ac_if);
703 }
513da8e7
SZ
704 if (sc->irq_handle != NULL)
705 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
706
513da8e7
SZ
707#if 0
708 /*
709 * this code still in progress (DMA support)
710 */
711 if (sc->recv_ring != NULL)
712 free(sc->recv_ring, M_DEVBUF);
713#endif
714
715 if (sc->buffer != NULL)
716 free(sc->buffer, M_DEVBUF);
513da8e7 717 cs_release_resources(dev);
513da8e7
SZ
718 ifmedia_removeall(&sc->media);
719
78195a76 720 lwkt_serialize_exit(ifp->if_serializer);
513da8e7 721 return 0;
984263bc
MD
722}
723
724/*
725 * Initialize the board
726 */
727static void
728cs_init(void *xsc)
729{
730 struct cs_softc *sc=(struct cs_softc *)xsc;
731 struct ifnet *ifp = &sc->arpcom.ac_if;
96723634
JS
732 int i, rx_cfg;
733
984263bc
MD
734 /*
735 * reset whatchdog timer
736 */
737 ifp->if_timer=0;
738 sc->buf_len = 0;
984263bc
MD
739
740 /*
741 * Hardware initialization of cs
742 */
743
744 /* Enable receiver and transmitter */
745 cs_writereg(sc->nic_addr, PP_LineCTL,
746 cs_readreg( sc->nic_addr, PP_LineCTL ) |
747 SERIAL_RX_ON | SERIAL_TX_ON);
748
749 /* Configure the receiver mode */
750 cs_setmode(sc);
751
752 /*
753 * This defines what type of frames will cause interrupts
754 * Bad frames should generate interrupts so that the driver
755 * could track statistics of discarded packets
756 */
757 rx_cfg = RX_OK_ENBL | RX_CRC_ERROR_ENBL | RX_RUNT_ENBL |
758 RX_EXTRA_DATA_ENBL;
759 if (sc->isa_config & STREAM_TRANSFER)
760 rx_cfg |= RX_STREAM_ENBL;
761 cs_writereg(sc->nic_addr, PP_RxCFG, rx_cfg);
762
763 cs_writereg(sc->nic_addr, PP_TxCFG, TX_LOST_CRS_ENBL |
764 TX_SQE_ERROR_ENBL | TX_OK_ENBL | TX_LATE_COL_ENBL |
765 TX_JBR_ENBL | TX_ANY_COL_ENBL | TX_16_COL_ENBL);
766
767 cs_writereg(sc->nic_addr, PP_BufCFG, READY_FOR_TX_ENBL |
768 RX_MISS_COUNT_OVRFLOW_ENBL | TX_COL_COUNT_OVRFLOW_ENBL |
769 TX_UNDERRUN_ENBL /*| RX_DMA_ENBL*/);
770
771 /* Write MAC address into IA filter */
772 for (i=0; i<ETHER_ADDR_LEN/2; i++)
773 cs_writereg(sc->nic_addr, PP_IA+i*2,
774 sc->arpcom.ac_enaddr[i*2] |
775 (sc->arpcom.ac_enaddr[i*2+1] << 8) );
776
777 /*
778 * Now enable everything
779 */
780/*
781#ifdef CS_USE_64K_DMA
782 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ | RX_DMA_SIZE_64K);
783 #else
784
785 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
786#endif
787*/
788 cs_writereg(sc->nic_addr, PP_BusCTL, ENABLE_IRQ);
789
790 /*
791 * Set running and clear output active flags
792 */
793 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
794 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
795
796 /*
797 * Start sending process
798 */
799 cs_start(ifp);
984263bc
MD
800}
801
802/*
803 * Get the packet from the board and send it to the upper layer
804 * via ether_input().
805 */
806static int
807cs_get_packet(struct cs_softc *sc)
808{
809 struct ifnet *ifp = &(sc->arpcom.ac_if);
810 int iobase = sc->nic_addr, status, length;
984263bc
MD
811 struct mbuf *m;
812
813#ifdef CS_DEBUG
814 int i;
815#endif
816
817 status = inw(iobase + RX_FRAME_PORT);
818 length = inw(iobase + RX_FRAME_PORT);
819
820#ifdef CS_DEBUG
754e3891 821 if_printf(ifp, "rcvd: stat %x, len %d\n", status, length);
984263bc
MD
822#endif
823
824 if (!(status & RX_OK)) {
825#ifdef CS_DEBUG
754e3891 826 if_printf(ifp, "bad pkt stat %x\n", status);
984263bc
MD
827#endif
828 ifp->if_ierrors++;
829 return -1;
830 }
831
74f1caca 832 MGETHDR(m, MB_DONTWAIT, MT_DATA);
984263bc
MD
833 if (m==NULL)
834 return -1;
835
836 if (length > MHLEN) {
74f1caca 837 MCLGET(m, MB_DONTWAIT);
984263bc
MD
838 if (!(m->m_flags & M_EXT)) {
839 m_freem(m);
840 return -1;
841 }
842 }
843
844 /* Initialize packet's header info */
845 m->m_pkthdr.rcvif = ifp;
846 m->m_pkthdr.len = length;
847 m->m_len = length;
848
849 /* Get the data */
850 insw(iobase + RX_FRAME_PORT, m->m_data, (length+1)>>1);
851
984263bc
MD
852#ifdef CS_DEBUG
853 for (i=0;i<length;i++)
854 printf(" %02x",(unsigned char)*((char *)(m->m_data+i)));
855 printf( "\n" );
856#endif
857
858 if (status & (RX_IA | RX_BROADCAST) ||
859 (ifp->if_flags & IFF_MULTICAST && status & RX_HASHED)) {
78195a76 860 ifp->if_input(ifp, m);
984263bc
MD
861
862 ifp->if_ipackets++;
863
864 if (length==ETHER_MAX_LEN-ETHER_CRC_LEN)
865 DELAY( cs_recv_delay );
866 } else {
867 m_freem(m);
868 }
869
870 return 0;
871}
872
873/*
874 * Handle interrupts
875 */
513da8e7 876static void
984263bc
MD
877csintr(void *arg)
878{
879 struct cs_softc *sc = (struct cs_softc*) arg;
880 struct ifnet *ifp = &(sc->arpcom.ac_if);
881 int status;
882
883#ifdef CS_DEBUG
754e3891 884 if_printf(ifp, "Interrupt.\n");
984263bc
MD
885#endif
886
887 while ((status=cs_readword(sc->nic_addr, ISQ_PORT))) {
888
889#ifdef CS_DEBUG
754e3891 890 if_printf(ifp, "from ISQ: %04x\n", status);
984263bc
MD
891#endif
892
893 switch (status & ISQ_EVENT_MASK) {
894 case ISQ_RECEIVER_EVENT:
895 cs_get_packet(sc);
896 break;
897
898 case ISQ_TRANSMITTER_EVENT:
899 if (status & TX_OK)
900 ifp->if_opackets++;
901 else
902 ifp->if_oerrors++;
903 ifp->if_flags &= ~IFF_OACTIVE;
904 ifp->if_timer = 0;
905 break;
906
907 case ISQ_BUFFER_EVENT:
908 if (status & READY_FOR_TX) {
909 ifp->if_flags &= ~IFF_OACTIVE;
910 ifp->if_timer = 0;
911 }
912
913 if (status & TX_UNDERRUN) {
914 ifp->if_flags &= ~IFF_OACTIVE;
915 ifp->if_timer = 0;
916 ifp->if_oerrors++;
917 }
918 break;
919
920 case ISQ_RX_MISS_EVENT:
921 ifp->if_ierrors+=(status>>6);
922 break;
923
924 case ISQ_TX_COL_EVENT:
925 ifp->if_collisions+=(status>>6);
926 break;
927 }
928 }
929
930 if (!(ifp->if_flags & IFF_OACTIVE)) {
931 cs_start(ifp);
932 }
933}
934
935/*
936 * Save the data in buffer
937 */
938
939static void
940cs_write_mbufs( struct cs_softc *sc, struct mbuf *m )
941{
942 int len;
943 struct mbuf *mp;
944 unsigned char *data, *buf;
945
946 for (mp=m, buf=sc->buffer, sc->buf_len=0; mp != NULL; mp=mp->m_next) {
947 len = mp->m_len;
948
949 /*
950 * Ignore empty parts
951 */
952 if (!len)
953 continue;
954
955 /*
956 * Find actual data address
957 */
958 data = mtod(mp, caddr_t);
959
960 bcopy((caddr_t) data, (caddr_t) buf, len);
961 buf += len;
962 sc->buf_len += len;
963 }
964}
965
966
967static void
968cs_xmit_buf( struct cs_softc *sc )
969{
970 outsw(sc->nic_addr+TX_FRAME_PORT, sc->buffer, (sc->buf_len+1)>>1);
971 sc->buf_len = 0;
972}
973
974static void
975cs_start(struct ifnet *ifp)
976{
96723634 977 int length;
984263bc
MD
978 struct mbuf *m, *mp;
979 struct cs_softc *sc = ifp->if_softc;
980
984263bc
MD
981 for (;;) {
982 if (sc->buf_len)
983 length = sc->buf_len;
984 else {
d2c71fa0 985 m = ifq_dequeue(&ifp->if_snd, NULL);
984263bc
MD
986
987 if (m==NULL) {
984263bc
MD
988 return;
989 }
990
991 for (length=0, mp=m; mp != NULL; mp=mp->m_next)
992 length += mp->m_len;
993
994 /* Skip zero-length packets */
995 if (length == 0) {
996 m_freem(m);
997 continue;
998 }
999
1000 cs_write_mbufs(sc, m);
1001
7600679e 1002 BPF_MTAP(ifp, m);
984263bc
MD
1003
1004 m_freem(m);
1005 }
1006
1007 /*
1008 * Issue a SEND command
1009 */
1010 outw(sc->nic_addr+TX_CMD_PORT, sc->send_cmd);
1011 outw(sc->nic_addr+TX_LEN_PORT, length );
1012
1013 /*
1014 * If there's no free space in the buffer then leave
1015 * this packet for the next time: indicate output active
1016 * and return.
1017 */
1018 if (!(cs_readreg(sc->nic_addr, PP_BusST) & READY_FOR_TX_NOW)) {
1019 ifp->if_timer = sc->buf_len;
984263bc
MD
1020 ifp->if_flags |= IFF_OACTIVE;
1021 return;
1022 }
1023
1024 cs_xmit_buf(sc);
1025
1026 /*
1027 * Set the watchdog timer in case we never hear
1028 * from board again. (I don't know about correct
1029 * value for this timeout)
1030 */
1031 ifp->if_timer = length;
1032
984263bc
MD
1033 ifp->if_flags |= IFF_OACTIVE;
1034 return;
1035 }
1036}
1037
1038/*
1039 * Stop everything on the interface
1040 */
1041static void
1042cs_stop(struct cs_softc *sc)
1043{
984263bc
MD
1044 cs_writereg(sc->nic_addr, PP_RxCFG, 0);
1045 cs_writereg(sc->nic_addr, PP_TxCFG, 0);
1046 cs_writereg(sc->nic_addr, PP_BufCFG, 0);
1047 cs_writereg(sc->nic_addr, PP_BusCTL, 0);
1048
1049 sc->arpcom.ac_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1050 sc->arpcom.ac_if.if_timer = 0;
984263bc
MD
1051}
1052
1053/*
1054 * Reset the interface
1055 */
1056static void
1057cs_reset(struct cs_softc *sc)
1058{
1059 cs_stop(sc);
1060 cs_init(sc);
1061}
1062
1063static void
1064cs_setmode(struct cs_softc *sc)
1065{
1066 struct ifnet *ifp = &(sc->arpcom.ac_if);
1067 int rx_ctl;
1068
1069 /* Stop the receiver while changing filters */
1070 cs_writereg(sc->nic_addr, PP_LineCTL,
1071 cs_readreg(sc->nic_addr, PP_LineCTL) & ~SERIAL_RX_ON);
1072
1073 if (ifp->if_flags & IFF_PROMISC) {
1074 /* Turn on promiscuous mode. */
1075 rx_ctl = RX_OK_ACCEPT | RX_PROM_ACCEPT;
1076 } else {
1077 if (ifp->if_flags & IFF_MULTICAST) {
1078 /* Allow receiving frames with multicast addresses */
1079 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1080 RX_OK_ACCEPT | RX_MULTCAST_ACCEPT;
1081 /*
1082 * Here the reconfiguration of chip's multicast
1083 * filters should be done but I've no idea about
1084 * hash transformation in this chip. If you can
1085 * add this code or describe me the transformation
1086 * I'd be very glad.
1087 */
1088 } else {
1089 /*
1090 * Receive only good frames addressed for us and
1091 * good broadcasts.
1092 */
1093 rx_ctl = RX_IA_ACCEPT | RX_BROADCAST_ACCEPT |
1094 RX_OK_ACCEPT;
1095 }
1096 }
1097
1098 /* Set up the filter */
1099 cs_writereg(sc->nic_addr, PP_RxCTL, RX_DEF_ACCEPT | rx_ctl);
1100
1101 /* Turn on receiver */
1102 cs_writereg(sc->nic_addr, PP_LineCTL,
1103 cs_readreg(sc->nic_addr, PP_LineCTL) | SERIAL_RX_ON);
1104}
1105
1106static int
bd4539cc
JH
1107cs_ioctl(register struct ifnet *ifp, u_long command, caddr_t data,
1108 struct ucred *cr)
984263bc
MD
1109{
1110 struct cs_softc *sc=ifp->if_softc;
1111 struct ifreq *ifr = (struct ifreq *)data;
96723634 1112 int error=0;
984263bc
MD
1113
1114#ifdef CS_DEBUG
754e3891 1115 if_printf(ifp, "ioctl(%lx)\n", command);
984263bc
MD
1116#endif
1117
984263bc 1118 switch (command) {
984263bc
MD
1119 case SIOCSIFFLAGS:
1120 /*
1121 * Switch interface state between "running" and
1122 * "stopped", reflecting the UP flag.
1123 */
1124 if (sc->arpcom.ac_if.if_flags & IFF_UP) {
1125 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)==0) {
1126 cs_init(sc);
1127 }
1128 } else {
1129 if ((sc->arpcom.ac_if.if_flags & IFF_RUNNING)!=0) {
1130 cs_stop(sc);
1131 }
1132 }
1133 /*
1134 * Promiscuous and/or multicast flags may have changed,
1135 * so reprogram the multicast filter and/or receive mode.
1136 *
1137 * See note about multicasts in cs_setmode
1138 */
1139 cs_setmode(sc);
1140 break;
1141
1142 case SIOCADDMULTI:
1143 case SIOCDELMULTI:
1144 /*
1145 * Multicast list has changed; set the hardware filter
1146 * accordingly.
1147 *
1148 * See note about multicasts in cs_setmode
1149 */
1150 cs_setmode(sc);
1151 error = 0;
1152 break;
1153
1154 case SIOCSIFMEDIA:
1155 case SIOCGIFMEDIA:
1156 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
1157 break;
4cde4dd5
JS
1158 default:
1159 error = ether_ioctl(ifp, command, data);
1160 break;
984263bc
MD
1161 }
1162
984263bc
MD
1163 return error;
1164}
1165
1166/*
1167 * Device timeout/watchdog routine. Entered if the device neglects to
1168 * generate an interrupt after a transmit has been started on it.
1169 */
1170static void
1171cs_watchdog(struct ifnet *ifp)
1172{
1173 struct cs_softc *sc = ifp->if_softc;
1174
1175 ifp->if_oerrors++;
3e4a09e7 1176 log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
984263bc
MD
1177
1178 /* Reset the interface */
1179 if (ifp->if_flags & IFF_UP)
1180 cs_reset(sc);
1181 else
1182 cs_stop(sc);
1183}
1184
1185static int
1186cs_mediachange(struct ifnet *ifp)
1187{
1188 struct cs_softc *sc = ifp->if_softc;
1189 struct ifmedia *ifm = &sc->media;
1190
1191 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1192 return EINVAL;
1193
1194 return cs_mediaset(sc, ifm->ifm_media);
1195}
1196
1197static void
1198cs_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1199{
1200 int line_status;
1201 struct cs_softc *sc = ifp->if_softc;
1202
1203 ifmr->ifm_active = IFM_ETHER;
1204 line_status = cs_readreg(sc->nic_addr, PP_LineST);
1205 if (line_status & TENBASET_ON) {
1206 ifmr->ifm_active |= IFM_10_T;
1207 if (sc->chip_type != CS8900) {
1208 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & FDX_ACTIVE)
1209 ifmr->ifm_active |= IFM_FDX;
1210 if (cs_readreg(sc->nic_addr, PP_AutoNegST) & HDX_ACTIVE)
1211 ifmr->ifm_active |= IFM_HDX;
1212 }
1213 ifmr->ifm_status = IFM_AVALID;
1214 if (line_status & LINK_OK)
1215 ifmr->ifm_status |= IFM_ACTIVE;
1216 } else {
1217 if (line_status & AUI_ON) {
1218 cs_writereg(sc->nic_addr, PP_SelfCTL,
1219 cs_readreg(sc->nic_addr, PP_SelfCTL) |
1220 HCB1_ENBL);
1221 if (((sc->adapter_cnf & A_CNF_DC_DC_POLARITY)!=0)^
1222 (cs_readreg(sc->nic_addr, PP_SelfCTL)&HCB1))
1223 ifmr->ifm_active |= IFM_10_2;
1224 else
1225 ifmr->ifm_active |= IFM_10_5;
1226 }
1227 }
1228}
1229
1230static int
1231cs_mediaset(struct cs_softc *sc, int media)
1232{
1233 int error;
1234
1235 /* Stop the receiver & transmitter */
1236 cs_writereg(sc->nic_addr, PP_LineCTL,
1237 cs_readreg(sc->nic_addr, PP_LineCTL) &
1238 ~(SERIAL_RX_ON | SERIAL_TX_ON));
1239
1240#ifdef CS_DEBUG
754e3891 1241 if_printf(&sc->arpcom.ac_if, "cs_setmedia(%x)\n", media);
984263bc
MD
1242#endif
1243
1244 switch (IFM_SUBTYPE(media)) {
1245 default:
1246 case IFM_AUTO:
1247 if ((error=enable_tp(sc))==0)
1248 error = cs_duplex_auto(sc);
1249 else if ((error=enable_bnc(sc)) != 0)
1250 error = enable_aui(sc);
1251 break;
1252 case IFM_10_T:
1253 if ((error=enable_tp(sc)) != 0)
1254 break;
1255 if (media & IFM_FDX)
1256 cs_duplex_full(sc);
1257 else if (media & IFM_HDX)
1258 cs_duplex_half(sc);
1259 else
1260 error = cs_duplex_auto(sc);
1261 break;
1262 case IFM_10_2:
1263 error = enable_bnc(sc);
1264 break;
1265 case IFM_10_5:
1266 error = enable_aui(sc);
1267 break;
1268 }
1269
1270 /*
1271 * Turn the transmitter & receiver back on
1272 */
1273 cs_writereg(sc->nic_addr, PP_LineCTL,
1274 cs_readreg( sc->nic_addr, PP_LineCTL ) |
1275 SERIAL_RX_ON | SERIAL_TX_ON);
1276
1277 return error;
1278}