if: Move if_cpuid into ifaltq; prepare multiple TX queues support
[dragonfly.git] / sys / dev / netif / ex / if_ex_pccard.c
1 /*-
2  * Copyright (c) 2000 Mitsuru IWASAKI
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, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/sys/dev/ex/if_ex_pccard.c,v 1.2.2.1 2001/03/05 05:33:20 imp Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/interrupt.h>
33 #include <sys/socket.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/rman.h>
37 #include <sys/serialize.h>
38
39 #include <net/if.h>
40 #include <net/if_arp.h>
41 #include <net/if_media.h> 
42 #include <net/ifq_var.h>
43
44 #include "if_exreg.h"
45 #include "if_exvar.h"
46
47 #include <bus/pccard/pccardvar.h>
48 #include <bus/pccard/pccarddevs.h>
49
50 static const struct pccard_product ex_pccard_products[] = {
51         PCMCIA_CARD(OLICOM, OC2220, 0),
52         { NULL }
53 };
54
55 /* Bus Front End Functions */
56 static int      ex_pccard_match         (device_t);
57 static int      ex_pccard_probe         (device_t);
58 static int      ex_pccard_attach        (device_t);
59 static int      ex_pccard_detach        (device_t);
60
61 static device_method_t ex_pccard_methods[] = {
62         /* Device interface */
63         DEVMETHOD(device_probe,         pccard_compat_probe),
64         DEVMETHOD(device_attach,        pccard_compat_attach),
65         DEVMETHOD(device_detach,        ex_pccard_detach),
66
67         /* Card interface */
68         DEVMETHOD(card_compat_match,    ex_pccard_match),
69         DEVMETHOD(card_compat_probe,    ex_pccard_probe),
70         DEVMETHOD(card_compat_attach,   ex_pccard_attach),
71
72         { 0, 0 }
73 };
74
75 static driver_t ex_pccard_driver = {
76         "ex",
77         ex_pccard_methods,
78         sizeof(struct ex_softc),
79 };
80
81 extern devclass_t ex_devclass;
82
83 DRIVER_MODULE(if_ex, pccard, ex_pccard_driver, ex_devclass, NULL, NULL);
84
85 static int
86 ex_pccard_match(device_t dev)
87 {
88         const struct pccard_product *pp;
89
90         if ((pp = pccard_product_lookup(dev, ex_pccard_products,
91             sizeof(ex_pccard_products[0]), NULL)) != NULL) {
92                 if (pp->pp_name != NULL)
93                         device_set_desc(dev, pp->pp_name);
94                 return 0;
95         }
96         return EIO;
97 }
98
99 static int
100 ex_pccard_probe(device_t dev)
101 {
102         u_int           iobase;
103         u_int           irq;
104
105         iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0);
106         if (!iobase) {
107                 kprintf("ex: no iobase?\n");
108                 return(ENXIO);
109         }
110
111         if (bootverbose)
112                 kprintf("ex: ex_pccard_probe() found card at 0x%03x\n", iobase);
113
114         irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0);
115
116         if (irq == 0) {
117                 kprintf("ex: invalid IRQ.\n");
118                 return(ENXIO);
119         }
120
121         return(0);
122 }
123
124 static int
125 ex_pccard_attach(device_t dev)
126 {
127         struct ex_softc *       sc = device_get_softc(dev);
128         struct ifnet *          ifp = &sc->arpcom.ac_if;
129         int                     error = 0;
130         int                     i;
131         uint8_t                 sum;
132         const uint8_t   *       ether_addr;
133
134         sc->dev = dev;
135         sc->ioport_rid = 0;
136         sc->irq_rid = 0;
137
138         if ((error = ex_alloc_resources(dev)) != 0) {
139                 device_printf(dev, "ex_alloc_resources() failed!\n");
140                 goto bad;
141         }
142
143         /*
144          * Fill in several fields of the softc structure:
145          *      - I/O base address.
146          *      - Hardware Ethernet address.
147          *      - IRQ number.
148          */
149         sc->iobase = rman_get_start(sc->ioport);
150         sc->irq_no = rman_get_start(sc->irq);
151
152         ether_addr = pccard_get_ether(dev);
153         for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
154                 sum |= ether_addr[i];
155         if (sum)
156                 bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
157
158         if ((error = ex_attach(dev)) != 0) {
159                 device_printf(dev, "ex_attach() failed!\n");
160                 goto bad;
161         }
162
163         error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
164                                 ex_intr, (void *)sc, &sc->ih, 
165                                 ifp->if_serializer);
166         if (error) {
167                 device_printf(dev, "bus_setup_intr() failed!\n");
168                 goto bad;
169         }
170
171         ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->irq));
172
173         return(0);
174 bad:
175         ex_release_resources(dev);
176         return (error);
177 }
178
179 static int
180 ex_pccard_detach(device_t dev)
181 {
182         struct ex_softc         *sc = device_get_softc(dev);
183         struct ifnet            *ifp = &sc->arpcom.ac_if;
184
185         lwkt_serialize_enter(ifp->if_serializer);
186         ex_stop(sc);
187         ifp->if_flags &= ~IFF_RUNNING;
188         lwkt_serialize_exit(ifp->if_serializer);
189
190         ether_ifdetach(ifp);
191         ex_release_resources(dev);
192         return (0);
193 }