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