Merge from vendor branch ATHEROS:
[dragonfly.git] / sys / dev / netif / ed / if_ed_pci.c
1 /*
2  *
3  * Copyright (c) 1996 Stefan Esser <se@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Absolutely no warranty of function or purpose is made by the author
16  *    Stefan Esser.
17  * 4. Modifications may be freely made to this file if the above conditions
18  *    are met.
19  *
20  * $FreeBSD: src/sys/dev/ed/if_ed_pci.c,v 1.34 2003/10/31 18:31:58 brooks Exp $
21  * $DragonFly: src/sys/dev/netif/ed/if_ed_pci.c,v 1.11 2005/12/31 14:07:59 sephe Exp $
22  */
23
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/socket.h>
27 #include <sys/kernel.h>
28
29 #include <sys/module.h>
30 #include <sys/bus.h>
31
32 #include <machine/bus.h>
33 #include <sys/rman.h>
34 #include <machine/resource.h>
35
36 #include <net/if.h>
37 #include <net/if_arp.h>
38 #include <net/if_mib.h>
39
40 #include <bus/pci/pcireg.h>
41 #include <bus/pci/pcivar.h>
42
43 #include "if_edvar.h"
44
45 static int ed_pci_detach(device_t dev);
46
47 static struct _pcsid
48 {
49         u_int32_t       type;
50         const char      *desc;
51 } pci_ids[] =
52 {
53         { 0x802910ec,   "NE2000 PCI Ethernet (RealTek 8029)"    },
54         { 0x50004a14,   "NE2000 PCI Ethernet (NetVin 5000)"     },
55         { 0x09401050,   "NE2000 PCI Ethernet (ProLAN)"          },
56         { 0x140111f6,   "NE2000 PCI Ethernet (Compex)"          },
57         { 0x30008e2e,   "NE2000 PCI Ethernet (KTI)"             },
58         { 0x19808c4a,   "NE2000 PCI Ethernet (Winbond W89C940)" },
59         { 0x0e3410bd,   "NE2000 PCI Ethernet (Surecom NE-34)"   },
60         { 0x09261106,   "NE2000 PCI Ethernet (VIA VT86C926)"    },
61         { 0x00000000,   NULL                                    }
62 };
63
64 static int      ed_pci_probe    (device_t);
65 static int      ed_pci_attach   (device_t);
66
67 static int
68 ed_pci_probe (device_t dev)
69 {
70         u_int32_t       type = pci_get_devid(dev);
71         struct _pcsid   *ep =pci_ids;
72
73         while (ep->type && ep->type != type)
74                 ++ep;
75         if (ep->desc) {
76                 device_set_desc(dev, ep->desc);
77                 return 0;
78         } else {
79                 return ENXIO;
80         }
81 }
82
83 static int
84 ed_pci_attach(device_t dev)
85 {
86         struct  ed_softc *sc = device_get_softc(dev);
87         int     flags = 0;
88         int     error;
89
90         error = ed_probe_Novell(dev, PCIR_MAPS, flags);
91         if (error)
92                 return (error);
93
94         error = ed_alloc_irq(dev, 0, RF_SHAREABLE);
95         if (error) {
96                 ed_release_resources(dev);
97                 return (error);
98         }
99
100         error = ed_attach(dev);
101         if (error == 0) {
102                 error = bus_setup_intr(dev, sc->irq_res, INTR_NETSAFE,
103                                        edintr, sc, &sc->irq_handle, 
104                                        sc->arpcom.ac_if.if_serializer);
105                 if (error)
106                         ed_pci_detach(dev);
107         } else {
108                 ed_release_resources(dev);
109         }
110         return (error);
111 }
112
113 static int
114 ed_pci_detach(device_t dev)
115 {
116         struct ed_softc *sc = device_get_softc(dev);
117         struct ifnet *ifp = &sc->arpcom.ac_if;
118
119         lwkt_serialize_enter(ifp->if_serializer);
120
121         if (sc->gone) {
122                 device_printf(dev, "already unloaded\n");
123                 lwkt_serialize_exit(ifp->if_serializer);
124                 return (0);
125         }
126         ed_stop(sc);
127         ifp->if_flags &= ~IFF_RUNNING;
128         sc->gone = 1;
129         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
130
131         lwkt_serialize_exit(ifp->if_serializer);
132
133         ether_ifdetach(ifp);
134         ed_release_resources(dev);
135         return (0);
136 }
137
138 static device_method_t ed_pci_methods[] = {
139         /* Device interface */
140         DEVMETHOD(device_probe,         ed_pci_probe),
141         DEVMETHOD(device_attach,        ed_pci_attach),
142         DEVMETHOD(device_attach,        ed_pci_detach),
143
144         { 0, 0 }
145 };
146
147 static driver_t ed_pci_driver = {
148         "ed",
149         ed_pci_methods,
150         sizeof(struct ed_softc),
151 };
152
153 static devclass_t ed_devclass;
154
155 DRIVER_MODULE(ed, pci, ed_pci_driver, ed_devclass, 0, 0);
156 MODULE_DEPEND(ed, pci, 1, 1, 1);