126e46883SJohn Baldwin /*-
226e46883SJohn Baldwin * Copyright (c) 1997-2001 Granch, Ltd. All rights reserved.
326e46883SJohn Baldwin * Author: Denis I.Timofeev <timofeev@granch.ru>
426e46883SJohn Baldwin *
526e46883SJohn Baldwin * Redistributon and use in source and binary forms, with or without
626e46883SJohn Baldwin * modification, are permitted provided that the following conditions
726e46883SJohn Baldwin * are met:
826e46883SJohn Baldwin * 1. Redistributions of source code must retain the above copyright
926e46883SJohn Baldwin * notice unmodified, this list of conditions, and the following
1026e46883SJohn Baldwin * disclaimer.
1126e46883SJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright
1226e46883SJohn Baldwin * notice, this list of conditions and the following disclaimer in the
1326e46883SJohn Baldwin * documentation and/or other materials provided with the distribution.
1426e46883SJohn Baldwin *
1526e46883SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1626e46883SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1726e46883SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1826e46883SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1926e46883SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2026e46883SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2126e46883SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2226e46883SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2326e46883SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEIGENCE OR OTHERWISE) ARISING IN ANY WAY
2426e46883SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2526e46883SJohn Baldwin * SUCH DAMAGE.
2626e46883SJohn Baldwin */
2726e46883SJohn Baldwin
2826e46883SJohn Baldwin #include <sys/param.h>
2926e46883SJohn Baldwin #include <sys/systm.h>
3026e46883SJohn Baldwin #include <sys/socket.h>
3126e46883SJohn Baldwin #include <sys/bus.h>
3226e46883SJohn Baldwin #include <sys/kernel.h>
3326e46883SJohn Baldwin #include <sys/module.h>
3426e46883SJohn Baldwin #include <machine/bus.h>
3526e46883SJohn Baldwin #include <machine/resource.h>
3626e46883SJohn Baldwin #include <sys/rman.h>
3726e46883SJohn Baldwin #include <sys/malloc.h>
3826e46883SJohn Baldwin
3926e46883SJohn Baldwin #include <net/if.h>
4026e46883SJohn Baldwin #include <net/ethernet.h>
4126e46883SJohn Baldwin #include <net/if_arp.h>
4226e46883SJohn Baldwin
4326e46883SJohn Baldwin #include <dev/pci/pcivar.h>
4426e46883SJohn Baldwin #include <dev/pci/pcireg.h>
4526e46883SJohn Baldwin
4626e46883SJohn Baldwin #include <dev/sbni/if_sbnireg.h>
4726e46883SJohn Baldwin #include <dev/sbni/if_sbnivar.h>
4826e46883SJohn Baldwin
4926e46883SJohn Baldwin static int sbni_pci_probe(device_t);
5026e46883SJohn Baldwin static int sbni_pci_attach(device_t);
5126e46883SJohn Baldwin static int sbni_pci_detach(device_t);
5226e46883SJohn Baldwin
5326e46883SJohn Baldwin static device_method_t sbni_pci_methods[] = {
5426e46883SJohn Baldwin /* Device interface */
5526e46883SJohn Baldwin DEVMETHOD(device_probe, sbni_pci_probe),
5626e46883SJohn Baldwin DEVMETHOD(device_attach, sbni_pci_attach),
5726e46883SJohn Baldwin DEVMETHOD(device_detach, sbni_pci_detach),
5826e46883SJohn Baldwin { 0, 0 }
5926e46883SJohn Baldwin };
6026e46883SJohn Baldwin
6126e46883SJohn Baldwin static driver_t sbni_pci_driver = {
6226e46883SJohn Baldwin "sbni",
6326e46883SJohn Baldwin sbni_pci_methods,
6426e46883SJohn Baldwin sizeof(struct sbni_softc)
6526e46883SJohn Baldwin };
6626e46883SJohn Baldwin
675d9f60a3SJohn Baldwin DRIVER_MODULE(sbni, pci, sbni_pci_driver, 0, 0);
6826e46883SJohn Baldwin MODULE_DEPEND(sbni, pci, 1, 1, 1);
6926e46883SJohn Baldwin
7026e46883SJohn Baldwin static int
sbni_pci_probe(device_t dev)7126e46883SJohn Baldwin sbni_pci_probe(device_t dev)
7226e46883SJohn Baldwin {
7326e46883SJohn Baldwin struct sbni_softc *sc;
7426e46883SJohn Baldwin
7526e46883SJohn Baldwin if (pci_get_vendor(dev) != SBNI_PCI_VENDOR ||
7626e46883SJohn Baldwin pci_get_device(dev) != SBNI_PCI_DEVICE)
7726e46883SJohn Baldwin return (ENXIO);
7826e46883SJohn Baldwin
7926e46883SJohn Baldwin sc = device_get_softc(dev);
8026e46883SJohn Baldwin if (pci_get_subdevice(dev) == 2) {
8126e46883SJohn Baldwin sc->slave_sc = malloc(sizeof(struct sbni_softc),
8226e46883SJohn Baldwin M_DEVBUF, M_NOWAIT | M_ZERO);
8326e46883SJohn Baldwin if (!sc->slave_sc)
8426e46883SJohn Baldwin return (ENOMEM);
8526e46883SJohn Baldwin device_set_desc(dev, "Granch SBNI12/PCI Dual adapter");
8626e46883SJohn Baldwin } else
8726e46883SJohn Baldwin device_set_desc(dev, "Granch SBNI12/PCI adapter");
8826e46883SJohn Baldwin
8926e46883SJohn Baldwin sc->io_rid = PCIR_BAR(0);
906c3e542bSJustin Hibbits sc->io_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
916c3e542bSJustin Hibbits &sc->io_rid, RF_ACTIVE);
9226e46883SJohn Baldwin if (!sc->io_res) {
9326e46883SJohn Baldwin device_printf(dev, "cannot allocate io ports!\n");
9426e46883SJohn Baldwin if (sc->slave_sc)
9526e46883SJohn Baldwin free(sc->slave_sc, M_DEVBUF);
9626e46883SJohn Baldwin return (ENOENT);
9726e46883SJohn Baldwin }
9826e46883SJohn Baldwin
9926e46883SJohn Baldwin if (sc->slave_sc) {
10026e46883SJohn Baldwin sc->slave_sc->io_res = sc->io_res;
10126e46883SJohn Baldwin sc->slave_sc->io_off = 4;
10226e46883SJohn Baldwin }
10326e46883SJohn Baldwin if (sbni_probe(sc) != 0) {
10426e46883SJohn Baldwin sbni_release_resources(sc);
10526e46883SJohn Baldwin if (sc->slave_sc)
10626e46883SJohn Baldwin free(sc->slave_sc, M_DEVBUF);
10726e46883SJohn Baldwin return (ENXIO);
10826e46883SJohn Baldwin }
10926e46883SJohn Baldwin
11026e46883SJohn Baldwin return (0);
11126e46883SJohn Baldwin }
11226e46883SJohn Baldwin
11326e46883SJohn Baldwin static int
sbni_pci_attach(device_t dev)11426e46883SJohn Baldwin sbni_pci_attach(device_t dev)
11526e46883SJohn Baldwin {
11626e46883SJohn Baldwin struct sbni_softc *sc;
11726e46883SJohn Baldwin struct sbni_flags flags;
11826e46883SJohn Baldwin int error;
11926e46883SJohn Baldwin
12026e46883SJohn Baldwin sc = device_get_softc(dev);
12126e46883SJohn Baldwin sc->dev = dev;
12226e46883SJohn Baldwin
12326e46883SJohn Baldwin sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
12426e46883SJohn Baldwin RF_SHAREABLE);
12526e46883SJohn Baldwin
12626e46883SJohn Baldwin if (sc->irq_res == NULL) {
12726e46883SJohn Baldwin device_printf(dev, "cannot claim irq!\n");
12826e46883SJohn Baldwin error = ENOENT;
12926e46883SJohn Baldwin goto attach_failed;
13026e46883SJohn Baldwin }
13126e46883SJohn Baldwin
1329c11e6afSJohn Baldwin memset(&flags, 0, sizeof(flags));
13326e46883SJohn Baldwin
134*aa386085SZhenlei Huang sbni_attach(sc, device_get_unit(dev) * 2, flags);
135*aa386085SZhenlei Huang if (sc->slave_sc)
136*aa386085SZhenlei Huang sbni_attach(sc->slave_sc, device_get_unit(dev) * 2 + 1, flags);
13726e46883SJohn Baldwin
13826e46883SJohn Baldwin if (sc->irq_res) {
13926e46883SJohn Baldwin error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET |
14026e46883SJohn Baldwin INTR_MPSAFE, NULL, sbni_intr, sc, &sc->irq_handle);
14126e46883SJohn Baldwin if (error) {
14226e46883SJohn Baldwin device_printf(dev, "bus_setup_intr\n");
14326e46883SJohn Baldwin sbni_detach(sc);
14426e46883SJohn Baldwin if (sc->slave_sc)
14526e46883SJohn Baldwin sbni_detach(sc);
14626e46883SJohn Baldwin goto attach_failed;
14726e46883SJohn Baldwin }
14826e46883SJohn Baldwin }
14926e46883SJohn Baldwin return (0);
15026e46883SJohn Baldwin
15126e46883SJohn Baldwin attach_failed:
15226e46883SJohn Baldwin sbni_release_resources(sc);
15326e46883SJohn Baldwin if (sc->slave_sc)
15426e46883SJohn Baldwin free(sc->slave_sc, M_DEVBUF);
15526e46883SJohn Baldwin return (error);
15626e46883SJohn Baldwin }
15726e46883SJohn Baldwin
15826e46883SJohn Baldwin static int
sbni_pci_detach(device_t dev)15926e46883SJohn Baldwin sbni_pci_detach(device_t dev)
16026e46883SJohn Baldwin {
16126e46883SJohn Baldwin struct sbni_softc *sc;
16226e46883SJohn Baldwin
16326e46883SJohn Baldwin sc = device_get_softc(dev);
16426e46883SJohn Baldwin sbni_detach(sc);
16526e46883SJohn Baldwin if (sc->slave_sc)
16626e46883SJohn Baldwin sbni_detach(sc);
16726e46883SJohn Baldwin
16826e46883SJohn Baldwin sbni_release_resources(sc);
16926e46883SJohn Baldwin if (sc->slave_sc)
17026e46883SJohn Baldwin free(sc->slave_sc, M_DEVBUF);
17126e46883SJohn Baldwin return (0);
17226e46883SJohn Baldwin }
173