Merge from vendor branch BZIP:
[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.14 2006/10/25 20:55:56 dillon Exp $
22  */
23
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/socket.h>
27 #include <sys/kernel.h>
28 #include <sys/module.h>
29 #include <sys/bus.h>
30 #include <sys/rman.h>
31
32 #include <net/if.h>
33 #include <net/if_arp.h>
34 #include <net/if_mib.h>
35
36 #include <bus/pci/pcidevs.h>
37 #include <bus/pci/pcireg.h>
38 #include <bus/pci/pcivar.h>
39
40 #include "if_edvar.h"
41
42 static int ed_pci_detach(device_t dev);
43
44 static struct ed_type {
45         uint16_t         ed_vid;
46         uint16_t         ed_did;
47         const char      *ed_name;
48 } ed_devs[] = {
49         { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8029,
50                 "NE2000 PCI Ethernet (RealTek 8029)" },
51         { PCI_VENDOR_NETVIN, PCI_PRODUCT_NETVIN_5000,
52                 "NE2000 PCI Ethernet (NetVin 5000)" },
53         { PCI_VENDOR_PROLAN, PCI_PRODUCT_PROLAN_NE2KETHER,
54                 "NE2000 PCI Ethernet (ProLAN)" },
55         { PCI_VENDOR_COMPEX, PCI_PRODUCT_COMPEX_NE2KETHER,
56                 "NE2000 PCI Ethernet (Compex)" },
57         { PCI_VENDOR_KTI, PCI_PRODUCT_KTI_NE2KETHER,
58                 "NE2000 PCI Ethernet (KTI)" },
59         { PCI_VENDOR_WINBOND, PCI_PRODUCT_WINBOND_W89C940F,
60                 "NE2000 PCI Ethernet (Winbond W89C940)" },
61         { PCI_VENDOR_SURECOM, PCI_PRODUCT_SURECOM_NE34,
62                 "NE2000 PCI Ethernet (Surecom NE-34)" },
63         { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C926,
64                 "NE2000 PCI Ethernet (VIA VT86C926)" },
65         { 0, 0, NULL }
66 };
67
68 static int      ed_pci_probe    (device_t);
69 static int      ed_pci_attach   (device_t);
70
71 static int
72 ed_pci_probe(device_t dev)
73 {
74         struct ed_type *t;
75         uint16_t product = pci_get_device(dev);
76         uint16_t vendor = pci_get_vendor(dev);
77
78         for (t = ed_devs; t->ed_name != NULL; t++) {
79                 if (vendor == t->ed_vid && product == t->ed_did) {
80                         device_set_desc(dev, t->ed_name);
81                         return 0;
82                 }
83         }
84         return ENXIO;
85 }
86
87 static int
88 ed_pci_attach(device_t dev)
89 {
90         struct  ed_softc *sc = device_get_softc(dev);
91         int     flags = 0;
92         int     error;
93
94         error = ed_probe_Novell(dev, PCIR_MAPS, flags);
95         if (error)
96                 return (error);
97
98         error = ed_alloc_irq(dev, 0, RF_SHAREABLE);
99         if (error) {
100                 ed_release_resources(dev);
101                 return (error);
102         }
103
104         error = ed_attach(dev);
105         if (error == 0) {
106                 error = bus_setup_intr(dev, sc->irq_res, INTR_NETSAFE,
107                                        edintr, sc, &sc->irq_handle, 
108                                        sc->arpcom.ac_if.if_serializer);
109                 if (error)
110                         ed_pci_detach(dev);
111         } else {
112                 ed_release_resources(dev);
113         }
114         return (error);
115 }
116
117 static int
118 ed_pci_detach(device_t dev)
119 {
120         struct ed_softc *sc = device_get_softc(dev);
121         struct ifnet *ifp = &sc->arpcom.ac_if;
122
123         lwkt_serialize_enter(ifp->if_serializer);
124
125         if (sc->gone) {
126                 device_printf(dev, "already unloaded\n");
127                 lwkt_serialize_exit(ifp->if_serializer);
128                 return (0);
129         }
130         ed_stop(sc);
131         ifp->if_flags &= ~IFF_RUNNING;
132         sc->gone = 1;
133         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
134
135         lwkt_serialize_exit(ifp->if_serializer);
136
137         ether_ifdetach(ifp);
138         ed_release_resources(dev);
139         return (0);
140 }
141
142 static device_method_t ed_pci_methods[] = {
143         /* Device interface */
144         DEVMETHOD(device_probe,         ed_pci_probe),
145         DEVMETHOD(device_attach,        ed_pci_attach),
146         DEVMETHOD(device_attach,        ed_pci_detach),
147
148         { 0, 0 }
149 };
150
151 static driver_t ed_pci_driver = {
152         "ed",
153         ed_pci_methods,
154         sizeof(struct ed_softc),
155 };
156
157 DRIVER_MODULE(ed, pci, ed_pci_driver, ed_devclass, 0, 0);
158 MODULE_DEPEND(ed, pci, 1, 1, 1);