xref: /dragonfly/sys/dev/netif/ath/ath/if_ath_pci.c (revision 23265324)
1 /*
2  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution 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, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  * 3. Neither the names of the above-listed copyright holders nor the names
16  *    of any contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * Alternatively, this software may be distributed under the terms of the
20  * GNU General Public License ("GPL") version 2 as published by the Free
21  * Software Foundation.
22  *
23  * NO WARRANTY
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGES.
35  *
36  * $FreeBSD: src/sys/dev/ath/if_ath_pci.c,v 1.12 2005/03/05 19:06:12 imp Exp $
37  * $DragonFly: src/sys/dev/netif/ath/ath/if_ath_pci.c,v 1.3 2007/01/08 12:15:27 swildner Exp $
38  */
39 
40 /*
41  * PCI/Cardbus front-end for the Atheros Wireless LAN controller driver.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/module.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/sysctl.h>
50 #include <sys/bus.h>
51 #include <sys/rman.h>
52 
53 #include <sys/socket.h>
54 
55 #include <net/if.h>
56 #include <net/if_media.h>
57 #include <net/if_arp.h>
58 
59 #include <netproto/802_11/ieee80211_var.h>
60 
61 #include <bus/pci/pcivar.h>
62 #include <bus/pci/pcireg.h>
63 
64 #include <dev/netif/ath/ath/if_athvar.h>
65 #include <contrib/dev/ath/ah.h>
66 
67 static int	ath_pci_probe(device_t);
68 static int	ath_pci_attach(device_t);
69 static int	ath_pci_detach(device_t);
70 static int	ath_pci_shutdown(device_t);
71 static int	ath_pci_suspend(device_t);
72 static int	ath_pci_resume(device_t);
73 
74 static void	ath_pci_setup(device_t);
75 
76 /*
77  * PCI glue.
78  */
79 
80 struct ath_pci_softc {
81 	struct ath_softc	sc_sc;
82 	struct resource		*sc_sr;		/* memory resource */
83 	int			sc_srid;
84 };
85 
86 #define	BS_BAR	0x10
87 #define	PCIR_RETRY_TIMEOUT	0x41
88 
89 static int
90 ath_pci_probe(device_t dev)
91 {
92 	const char* devname;
93 
94 	devname = ath_hal_probe(pci_get_vendor(dev), pci_get_device(dev));
95 	if (devname != NULL) {
96 		device_set_desc(dev, devname);
97 		return 0;
98 	}
99 	return ENXIO;
100 }
101 
102 static void
103 ath_pci_setup(device_t dev)
104 {
105 	/*
106 	 * Enable memory mapping and bus mastering.
107 	 */
108 	pci_enable_busmaster(dev);
109 
110 	/*
111 	 * Disable retry timeout to keep PCI Tx retries from
112 	 * interfering with C3 CPU state.
113 	 */
114 	pci_write_config(dev, PCIR_RETRY_TIMEOUT, 0, 1);
115 }
116 
117 static int
118 ath_pci_attach(device_t dev)
119 {
120 	struct ath_pci_softc *psc = device_get_softc(dev);
121 	struct ath_softc *sc = &psc->sc_sc;
122 	int error;
123 
124 	sc->sc_dev = dev;
125 
126 	ath_pci_setup(dev);
127 
128 	/*
129 	 * Setup memory-mapping of PCI registers.
130 	 */
131 	psc->sc_srid = BS_BAR;
132 	psc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->sc_srid,
133 					    RF_ACTIVE);
134 	if (psc->sc_sr == NULL) {
135 		device_printf(dev, "cannot map register space\n");
136 		return ENXIO;
137 	}
138 	sc->sc_st = rman_get_bustag(psc->sc_sr);
139 	sc->sc_sh = rman_get_bushandle(psc->sc_sr);
140 
141 	/*
142 	 * Setup DMA descriptor area.
143 	 */
144 	error = bus_dma_tag_create(NULL, 1, 0,
145 				   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
146 				   NULL, NULL, 0x3ffff/* maxsize XXX */,
147 				   ATH_MAX_SCATTER, 0x3ffff/* maxsegsize XXX */,
148 				   BUS_DMA_ALLOCNOW, &sc->sc_dmat);
149 	if (error) {
150 		device_printf(dev, "cannot allocate DMA tag\n");
151 		goto back;
152 	}
153 
154 	error = ath_attach(pci_get_device(dev), sc);
155 
156 back:
157 	if (error)
158 		ath_pci_detach(dev);
159 	return error;
160 }
161 
162 static int
163 ath_pci_detach(device_t dev)
164 {
165 	struct ath_pci_softc *psc = device_get_softc(dev);
166 	struct ath_softc *sc = &psc->sc_sc;
167 
168 	/* XXX check if device was removed */
169 	sc->sc_invalid = !bus_child_present(dev);
170 
171 	if (device_is_attached(dev))
172 		ath_detach(sc);
173 
174 	bus_generic_detach(dev);
175 
176 	if (sc->sc_dmat != NULL)
177 		bus_dma_tag_destroy(sc->sc_dmat);
178 
179 	if (psc->sc_sr != NULL) {
180 		bus_release_resource(dev, SYS_RES_MEMORY, psc->sc_srid,
181 				     psc->sc_sr);
182 	}
183 	return 0;
184 }
185 
186 static int
187 ath_pci_shutdown(device_t dev)
188 {
189 	struct ath_pci_softc *psc = device_get_softc(dev);
190 
191 	ath_shutdown(&psc->sc_sc);
192 	return (0);
193 }
194 
195 static int
196 ath_pci_suspend(device_t dev)
197 {
198 	struct ath_pci_softc *psc = device_get_softc(dev);
199 
200 	ath_suspend(&psc->sc_sc);
201 	return (0);
202 }
203 
204 static int
205 ath_pci_resume(device_t dev)
206 {
207 	struct ath_pci_softc *psc = device_get_softc(dev);
208 
209 	ath_pci_setup(dev);
210 	ath_resume(&psc->sc_sc);
211 	return (0);
212 }
213 
214 static device_method_t ath_pci_methods[] = {
215 	/* Device interface */
216 	DEVMETHOD(device_probe,		ath_pci_probe),
217 	DEVMETHOD(device_attach,	ath_pci_attach),
218 	DEVMETHOD(device_detach,	ath_pci_detach),
219 	DEVMETHOD(device_shutdown,	ath_pci_shutdown),
220 	DEVMETHOD(device_suspend,	ath_pci_suspend),
221 	DEVMETHOD(device_resume,	ath_pci_resume),
222 
223 	{ 0, 0 }
224 };
225 
226 static driver_t ath_pci_driver = {
227 	"ath",
228 	ath_pci_methods,
229 	sizeof (struct ath_pci_softc)
230 };
231 
232 static	devclass_t ath_devclass;
233 
234 DRIVER_MODULE(if_ath, pci, ath_pci_driver, ath_devclass, 0, 0);
235 DRIVER_MODULE(if_ath, cardbus, ath_pci_driver, ath_devclass, 0, 0);
236 
237 MODULE_VERSION(if_ath, 1);
238 
239 MODULE_DEPEND(if_ath, ath_hal, 1, 1, 1);	/* Atheros HAL */
240 MODULE_DEPEND(if_ath, ath_rate, 1, 1, 1);	/* rate control algorithm */
241 MODULE_DEPEND(if_ath, wlan, 1, 1, 1);		/* 802.11 media layer */
242