1 /*- 2 * Copyright (c) 1997-2001 Granch, Ltd. All rights reserved. 3 * Author: Denis I.Timofeev <timofeev@granch.ru> 4 * 5 * Redistributon 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 NEIGENCE 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 28 #include <sys/cdefs.h> 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/socket.h> 32 #include <sys/bus.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <machine/bus.h> 36 #include <machine/resource.h> 37 #include <sys/rman.h> 38 #include <sys/malloc.h> 39 40 #include <net/if.h> 41 #include <net/ethernet.h> 42 #include <net/if_arp.h> 43 44 #include <dev/pci/pcivar.h> 45 #include <dev/pci/pcireg.h> 46 47 #include <dev/sbni/if_sbnireg.h> 48 #include <dev/sbni/if_sbnivar.h> 49 50 static int sbni_pci_probe(device_t); 51 static int sbni_pci_attach(device_t); 52 static int sbni_pci_detach(device_t); 53 54 static device_method_t sbni_pci_methods[] = { 55 /* Device interface */ 56 DEVMETHOD(device_probe, sbni_pci_probe), 57 DEVMETHOD(device_attach, sbni_pci_attach), 58 DEVMETHOD(device_detach, sbni_pci_detach), 59 { 0, 0 } 60 }; 61 62 static driver_t sbni_pci_driver = { 63 "sbni", 64 sbni_pci_methods, 65 sizeof(struct sbni_softc) 66 }; 67 68 DRIVER_MODULE(sbni, pci, sbni_pci_driver, 0, 0); 69 MODULE_DEPEND(sbni, pci, 1, 1, 1); 70 71 static int 72 sbni_pci_probe(device_t dev) 73 { 74 struct sbni_softc *sc; 75 76 if (pci_get_vendor(dev) != SBNI_PCI_VENDOR || 77 pci_get_device(dev) != SBNI_PCI_DEVICE) 78 return (ENXIO); 79 80 sc = device_get_softc(dev); 81 if (pci_get_subdevice(dev) == 2) { 82 sc->slave_sc = malloc(sizeof(struct sbni_softc), 83 M_DEVBUF, M_NOWAIT | M_ZERO); 84 if (!sc->slave_sc) 85 return (ENOMEM); 86 device_set_desc(dev, "Granch SBNI12/PCI Dual adapter"); 87 } else 88 device_set_desc(dev, "Granch SBNI12/PCI adapter"); 89 90 sc->io_rid = PCIR_BAR(0); 91 sc->io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 92 &sc->io_rid, RF_ACTIVE); 93 if (!sc->io_res) { 94 device_printf(dev, "cannot allocate io ports!\n"); 95 if (sc->slave_sc) 96 free(sc->slave_sc, M_DEVBUF); 97 return (ENOENT); 98 } 99 100 if (sc->slave_sc) { 101 sc->slave_sc->io_res = sc->io_res; 102 sc->slave_sc->io_off = 4; 103 } 104 if (sbni_probe(sc) != 0) { 105 sbni_release_resources(sc); 106 if (sc->slave_sc) 107 free(sc->slave_sc, M_DEVBUF); 108 return (ENXIO); 109 } 110 111 return (0); 112 } 113 114 static int 115 sbni_pci_attach(device_t dev) 116 { 117 struct sbni_softc *sc; 118 struct sbni_flags flags; 119 int error; 120 121 sc = device_get_softc(dev); 122 sc->dev = dev; 123 124 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 125 RF_SHAREABLE); 126 127 if (sc->irq_res == NULL) { 128 device_printf(dev, "cannot claim irq!\n"); 129 error = ENOENT; 130 goto attach_failed; 131 } 132 133 memset(&flags, 0, sizeof(flags)); 134 135 error = sbni_attach(sc, device_get_unit(dev) * 2, flags); 136 if (error) { 137 device_printf(dev, "cannot initialize driver\n"); 138 goto attach_failed; 139 } 140 if (sc->slave_sc) { 141 error = sbni_attach(sc->slave_sc, device_get_unit(dev) * 2 + 1, 142 flags); 143 if (error) { 144 device_printf(dev, "cannot initialize slave\n"); 145 sbni_detach(sc); 146 goto attach_failed; 147 } 148 } 149 150 if (sc->irq_res) { 151 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | 152 INTR_MPSAFE, NULL, sbni_intr, sc, &sc->irq_handle); 153 if (error) { 154 device_printf(dev, "bus_setup_intr\n"); 155 sbni_detach(sc); 156 if (sc->slave_sc) 157 sbni_detach(sc); 158 goto attach_failed; 159 } 160 } 161 return (0); 162 163 attach_failed: 164 sbni_release_resources(sc); 165 if (sc->slave_sc) 166 free(sc->slave_sc, M_DEVBUF); 167 return (error); 168 } 169 170 static int 171 sbni_pci_detach(device_t dev) 172 { 173 struct sbni_softc *sc; 174 175 sc = device_get_softc(dev); 176 sbni_detach(sc); 177 if (sc->slave_sc) 178 sbni_detach(sc); 179 180 sbni_release_resources(sc); 181 if (sc->slave_sc) 182 free(sc->slave_sc, M_DEVBUF); 183 return (0); 184 } 185