if_wpi.c revision 1.6.4.8 1 1.6.4.8 yamt /* $NetBSD: if_wpi.c,v 1.6.4.8 2008/01/21 09:44:04 yamt Exp $ */
2 1.6.4.2 yamt
3 1.6.4.2 yamt /*-
4 1.6.4.4 yamt * Copyright (c) 2006, 2007
5 1.6.4.2 yamt * Damien Bergamini <damien.bergamini (at) free.fr>
6 1.6.4.2 yamt *
7 1.6.4.2 yamt * Permission to use, copy, modify, and distribute this software for any
8 1.6.4.2 yamt * purpose with or without fee is hereby granted, provided that the above
9 1.6.4.2 yamt * copyright notice and this permission notice appear in all copies.
10 1.6.4.2 yamt *
11 1.6.4.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.6.4.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.6.4.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.6.4.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.6.4.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.6.4.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.6.4.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.6.4.2 yamt */
19 1.6.4.2 yamt
20 1.6.4.2 yamt #include <sys/cdefs.h>
21 1.6.4.8 yamt __KERNEL_RCSID(0, "$NetBSD: if_wpi.c,v 1.6.4.8 2008/01/21 09:44:04 yamt Exp $");
22 1.6.4.2 yamt
23 1.6.4.2 yamt /*
24 1.6.4.2 yamt * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
25 1.6.4.2 yamt */
26 1.6.4.2 yamt
27 1.6.4.2 yamt #include "bpfilter.h"
28 1.6.4.2 yamt
29 1.6.4.2 yamt #include <sys/param.h>
30 1.6.4.2 yamt #include <sys/sockio.h>
31 1.6.4.2 yamt #include <sys/sysctl.h>
32 1.6.4.2 yamt #include <sys/mbuf.h>
33 1.6.4.2 yamt #include <sys/kernel.h>
34 1.6.4.2 yamt #include <sys/socket.h>
35 1.6.4.2 yamt #include <sys/systm.h>
36 1.6.4.2 yamt #include <sys/malloc.h>
37 1.6.4.2 yamt #include <sys/conf.h>
38 1.6.4.2 yamt #include <sys/kauth.h>
39 1.6.4.3 yamt #include <sys/callout.h>
40 1.6.4.2 yamt
41 1.6.4.5 yamt #include <sys/bus.h>
42 1.6.4.2 yamt #include <machine/endian.h>
43 1.6.4.5 yamt #include <sys/intr.h>
44 1.6.4.2 yamt
45 1.6.4.2 yamt #include <dev/pci/pcireg.h>
46 1.6.4.2 yamt #include <dev/pci/pcivar.h>
47 1.6.4.2 yamt #include <dev/pci/pcidevs.h>
48 1.6.4.2 yamt
49 1.6.4.2 yamt #if NBPFILTER > 0
50 1.6.4.2 yamt #include <net/bpf.h>
51 1.6.4.2 yamt #endif
52 1.6.4.2 yamt #include <net/if.h>
53 1.6.4.2 yamt #include <net/if_arp.h>
54 1.6.4.2 yamt #include <net/if_dl.h>
55 1.6.4.2 yamt #include <net/if_ether.h>
56 1.6.4.2 yamt #include <net/if_media.h>
57 1.6.4.2 yamt #include <net/if_types.h>
58 1.6.4.2 yamt
59 1.6.4.2 yamt #include <net80211/ieee80211_var.h>
60 1.6.4.2 yamt #include <net80211/ieee80211_amrr.h>
61 1.6.4.2 yamt #include <net80211/ieee80211_radiotap.h>
62 1.6.4.2 yamt
63 1.6.4.2 yamt #include <netinet/in.h>
64 1.6.4.2 yamt #include <netinet/in_systm.h>
65 1.6.4.2 yamt #include <netinet/in_var.h>
66 1.6.4.2 yamt #include <netinet/ip.h>
67 1.6.4.2 yamt
68 1.6.4.2 yamt #include <dev/firmload.h>
69 1.6.4.2 yamt
70 1.6.4.2 yamt #include <dev/pci/if_wpireg.h>
71 1.6.4.2 yamt #include <dev/pci/if_wpivar.h>
72 1.6.4.2 yamt
73 1.6.4.2 yamt #ifdef WPI_DEBUG
74 1.6.4.2 yamt #define DPRINTF(x) if (wpi_debug > 0) printf x
75 1.6.4.2 yamt #define DPRINTFN(n, x) if (wpi_debug >= (n)) printf x
76 1.6.4.2 yamt int wpi_debug = 1;
77 1.6.4.2 yamt #else
78 1.6.4.2 yamt #define DPRINTF(x)
79 1.6.4.2 yamt #define DPRINTFN(n, x)
80 1.6.4.2 yamt #endif
81 1.6.4.2 yamt
82 1.6.4.2 yamt /*
83 1.6.4.2 yamt * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
84 1.6.4.2 yamt */
85 1.6.4.2 yamt static const struct ieee80211_rateset wpi_rateset_11a =
86 1.6.4.2 yamt { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
87 1.6.4.2 yamt
88 1.6.4.2 yamt static const struct ieee80211_rateset wpi_rateset_11b =
89 1.6.4.2 yamt { 4, { 2, 4, 11, 22 } };
90 1.6.4.2 yamt
91 1.6.4.2 yamt static const struct ieee80211_rateset wpi_rateset_11g =
92 1.6.4.2 yamt { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
93 1.6.4.2 yamt
94 1.6.4.7 yamt static int wpi_match(device_t, struct cfdata *, void *);
95 1.6.4.7 yamt static void wpi_attach(device_t, device_t, void *);
96 1.6.4.7 yamt static int wpi_detach(device_t , int);
97 1.6.4.3 yamt static int wpi_dma_contig_alloc(bus_dma_tag_t, struct wpi_dma_info *,
98 1.6.4.2 yamt void **, bus_size_t, bus_size_t, int);
99 1.6.4.3 yamt static void wpi_dma_contig_free(struct wpi_dma_info *);
100 1.6.4.2 yamt static int wpi_alloc_shared(struct wpi_softc *);
101 1.6.4.2 yamt static void wpi_free_shared(struct wpi_softc *);
102 1.6.4.4 yamt static int wpi_alloc_fwmem(struct wpi_softc *);
103 1.6.4.4 yamt static void wpi_free_fwmem(struct wpi_softc *);
104 1.6.4.3 yamt static struct wpi_rbuf *wpi_alloc_rbuf(struct wpi_softc *);
105 1.6.4.4 yamt static void wpi_free_rbuf(struct mbuf *, void *, size_t, void *);
106 1.6.4.3 yamt static int wpi_alloc_rpool(struct wpi_softc *);
107 1.6.4.3 yamt static void wpi_free_rpool(struct wpi_softc *);
108 1.6.4.2 yamt static int wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
109 1.6.4.2 yamt static void wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
110 1.6.4.2 yamt static void wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
111 1.6.4.2 yamt static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *, int,
112 1.6.4.2 yamt int);
113 1.6.4.2 yamt static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
114 1.6.4.2 yamt static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
115 1.6.4.2 yamt static struct ieee80211_node * wpi_node_alloc(struct ieee80211_node_table *);
116 1.6.4.4 yamt static void wpi_newassoc(struct ieee80211_node *, int);
117 1.6.4.2 yamt static int wpi_media_change(struct ifnet *);
118 1.6.4.2 yamt static int wpi_newstate(struct ieee80211com *, enum ieee80211_state, int);
119 1.6.4.4 yamt static void wpi_fix_channel(struct ieee80211com *, struct mbuf *);
120 1.6.4.2 yamt static void wpi_mem_lock(struct wpi_softc *);
121 1.6.4.2 yamt static void wpi_mem_unlock(struct wpi_softc *);
122 1.6.4.2 yamt static uint32_t wpi_mem_read(struct wpi_softc *, uint16_t);
123 1.6.4.2 yamt static void wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
124 1.6.4.4 yamt static void wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
125 1.6.4.4 yamt const uint32_t *, int);
126 1.6.4.4 yamt static int wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
127 1.6.4.4 yamt static int wpi_load_microcode(struct wpi_softc *, const uint8_t *, int);
128 1.6.4.4 yamt static int wpi_load_firmware(struct wpi_softc *);
129 1.6.4.4 yamt static void wpi_calib_timeout(void *);
130 1.6.4.4 yamt static void wpi_iter_func(void *, struct ieee80211_node *);
131 1.6.4.4 yamt static void wpi_power_calibration(struct wpi_softc *, int);
132 1.6.4.2 yamt static void wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
133 1.6.4.2 yamt struct wpi_rx_data *);
134 1.6.4.2 yamt static void wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
135 1.6.4.2 yamt static void wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
136 1.6.4.2 yamt static void wpi_notif_intr(struct wpi_softc *);
137 1.6.4.2 yamt static int wpi_intr(void *);
138 1.6.4.4 yamt static void wpi_read_eeprom(struct wpi_softc *);
139 1.6.4.4 yamt static void wpi_read_eeprom_channels(struct wpi_softc *, int);
140 1.6.4.4 yamt static void wpi_read_eeprom_group(struct wpi_softc *, int);
141 1.6.4.2 yamt static uint8_t wpi_plcp_signal(int);
142 1.6.4.2 yamt static int wpi_tx_data(struct wpi_softc *, struct mbuf *,
143 1.6.4.2 yamt struct ieee80211_node *, int);
144 1.6.4.2 yamt static void wpi_start(struct ifnet *);
145 1.6.4.2 yamt static void wpi_watchdog(struct ifnet *);
146 1.6.4.4 yamt static int wpi_ioctl(struct ifnet *, u_long, void *);
147 1.6.4.2 yamt static int wpi_cmd(struct wpi_softc *, int, const void *, int, int);
148 1.6.4.2 yamt static int wpi_wme_update(struct ieee80211com *);
149 1.6.4.2 yamt static int wpi_mrr_setup(struct wpi_softc *);
150 1.6.4.2 yamt static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
151 1.6.4.2 yamt static void wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
152 1.6.4.4 yamt static int wpi_set_txpower(struct wpi_softc *,
153 1.6.4.4 yamt struct ieee80211_channel *, int);
154 1.6.4.4 yamt static int wpi_get_power_index(struct wpi_softc *,
155 1.6.4.4 yamt struct wpi_power_group *, struct ieee80211_channel *, int);
156 1.6.4.2 yamt static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
157 1.6.4.2 yamt static int wpi_auth(struct wpi_softc *);
158 1.6.4.2 yamt static int wpi_scan(struct wpi_softc *, uint16_t);
159 1.6.4.2 yamt static int wpi_config(struct wpi_softc *);
160 1.6.4.2 yamt static void wpi_stop_master(struct wpi_softc *);
161 1.6.4.2 yamt static int wpi_power_up(struct wpi_softc *);
162 1.6.4.2 yamt static int wpi_reset(struct wpi_softc *);
163 1.6.4.2 yamt static void wpi_hw_config(struct wpi_softc *);
164 1.6.4.2 yamt static int wpi_init(struct ifnet *);
165 1.6.4.2 yamt static void wpi_stop(struct ifnet *, int);
166 1.6.4.8 yamt static bool wpi_resume(device_t);
167 1.6.4.8 yamt static int wpi_getrfkill(struct wpi_softc *);
168 1.6.4.8 yamt static void wpi_sysctlattach(struct wpi_softc *);
169 1.6.4.2 yamt
170 1.6.4.7 yamt CFATTACH_DECL_NEW(wpi, sizeof (struct wpi_softc), wpi_match, wpi_attach,
171 1.6.4.2 yamt wpi_detach, NULL);
172 1.6.4.2 yamt
173 1.6.4.2 yamt static int
174 1.6.4.7 yamt wpi_match(device_t parent, struct cfdata *match __unused, void *aux)
175 1.6.4.2 yamt {
176 1.6.4.2 yamt struct pci_attach_args *pa = aux;
177 1.6.4.2 yamt
178 1.6.4.2 yamt if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
179 1.6.4.2 yamt return 0;
180 1.6.4.2 yamt
181 1.6.4.2 yamt if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_1 ||
182 1.6.4.3 yamt PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_2)
183 1.6.4.2 yamt return 1;
184 1.6.4.2 yamt
185 1.6.4.2 yamt return 0;
186 1.6.4.2 yamt }
187 1.6.4.2 yamt
188 1.6.4.2 yamt /* Base Address Register */
189 1.6.4.2 yamt #define WPI_PCI_BAR0 0x10
190 1.6.4.2 yamt
191 1.6.4.2 yamt static void
192 1.6.4.7 yamt wpi_attach(device_t parent __unused, device_t self, void *aux)
193 1.6.4.2 yamt {
194 1.6.4.7 yamt struct wpi_softc *sc = device_private(self);
195 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
196 1.6.4.2 yamt struct ifnet *ifp = &sc->sc_ec.ec_if;
197 1.6.4.2 yamt struct pci_attach_args *pa = aux;
198 1.6.4.2 yamt const char *intrstr;
199 1.6.4.2 yamt char devinfo[256];
200 1.6.4.2 yamt bus_space_tag_t memt;
201 1.6.4.2 yamt bus_space_handle_t memh;
202 1.6.4.2 yamt pci_intr_handle_t ih;
203 1.6.4.2 yamt pcireg_t data;
204 1.6.4.4 yamt int error, ac, revision;
205 1.6.4.2 yamt
206 1.6.4.7 yamt sc->sc_dev = self;
207 1.6.4.2 yamt sc->sc_pct = pa->pa_pc;
208 1.6.4.2 yamt sc->sc_pcitag = pa->pa_tag;
209 1.6.4.2 yamt
210 1.6.4.4 yamt callout_init(&sc->calib_to, 0);
211 1.6.4.7 yamt callout_setfunc(&sc->calib_to, wpi_calib_timeout, sc);
212 1.6.4.2 yamt
213 1.6.4.2 yamt pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
214 1.6.4.2 yamt revision = PCI_REVISION(pa->pa_class);
215 1.6.4.2 yamt aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
216 1.6.4.2 yamt
217 1.6.4.8 yamt pci_disable_retry(pa->pa_pc, pa->pa_tag);
218 1.6.4.2 yamt
219 1.6.4.2 yamt /* enable bus-mastering */
220 1.6.4.2 yamt data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
221 1.6.4.2 yamt data |= PCI_COMMAND_MASTER_ENABLE;
222 1.6.4.2 yamt pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
223 1.6.4.2 yamt
224 1.6.4.2 yamt /* map the register window */
225 1.6.4.2 yamt error = pci_mapreg_map(pa, WPI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
226 1.6.4.3 yamt PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz);
227 1.6.4.2 yamt if (error != 0) {
228 1.6.4.7 yamt aprint_error_dev(self, "could not map memory space\n");
229 1.6.4.2 yamt return;
230 1.6.4.2 yamt }
231 1.6.4.2 yamt
232 1.6.4.2 yamt sc->sc_st = memt;
233 1.6.4.2 yamt sc->sc_sh = memh;
234 1.6.4.2 yamt sc->sc_dmat = pa->pa_dmat;
235 1.6.4.2 yamt
236 1.6.4.2 yamt if (pci_intr_map(pa, &ih) != 0) {
237 1.6.4.7 yamt aprint_error_dev(self, "could not map interrupt\n");
238 1.6.4.2 yamt return;
239 1.6.4.2 yamt }
240 1.6.4.2 yamt
241 1.6.4.2 yamt intrstr = pci_intr_string(sc->sc_pct, ih);
242 1.6.4.2 yamt sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, wpi_intr, sc);
243 1.6.4.2 yamt if (sc->sc_ih == NULL) {
244 1.6.4.7 yamt aprint_error_dev(self, "could not establish interrupt");
245 1.6.4.2 yamt if (intrstr != NULL)
246 1.6.4.2 yamt aprint_error(" at %s", intrstr);
247 1.6.4.2 yamt aprint_error("\n");
248 1.6.4.2 yamt return;
249 1.6.4.2 yamt }
250 1.6.4.7 yamt aprint_normal_dev(self, "interrupting at %s\n", intrstr);
251 1.6.4.2 yamt
252 1.6.4.2 yamt if (wpi_reset(sc) != 0) {
253 1.6.4.7 yamt aprint_error_dev(self, "could not reset adapter\n");
254 1.6.4.2 yamt return;
255 1.6.4.2 yamt }
256 1.6.4.2 yamt
257 1.6.4.4 yamt /*
258 1.6.4.4 yamt * Allocate DMA memory for firmware transfers.
259 1.6.4.4 yamt */
260 1.6.4.7 yamt if ((error = wpi_alloc_fwmem(sc)) != 0)
261 1.6.4.4 yamt return;
262 1.6.4.4 yamt
263 1.6.4.2 yamt /*
264 1.6.4.2 yamt * Allocate shared page and Tx/Rx rings.
265 1.6.4.2 yamt */
266 1.6.4.2 yamt if ((error = wpi_alloc_shared(sc)) != 0) {
267 1.6.4.7 yamt aprint_error_dev(self, "could not allocate shared area\n");
268 1.6.4.4 yamt goto fail1;
269 1.6.4.2 yamt }
270 1.6.4.2 yamt
271 1.6.4.3 yamt if ((error = wpi_alloc_rpool(sc)) != 0) {
272 1.6.4.7 yamt aprint_error_dev(self, "could not allocate Rx buffers\n");
273 1.6.4.4 yamt goto fail2;
274 1.6.4.3 yamt }
275 1.6.4.3 yamt
276 1.6.4.2 yamt for (ac = 0; ac < 4; ac++) {
277 1.6.4.2 yamt error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac);
278 1.6.4.2 yamt if (error != 0) {
279 1.6.4.7 yamt aprint_error_dev(self, "could not allocate Tx ring %d\n", ac);
280 1.6.4.4 yamt goto fail3;
281 1.6.4.2 yamt }
282 1.6.4.2 yamt }
283 1.6.4.2 yamt
284 1.6.4.2 yamt error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
285 1.6.4.2 yamt if (error != 0) {
286 1.6.4.7 yamt aprint_error_dev(self, "could not allocate command ring\n");
287 1.6.4.3 yamt goto fail3;
288 1.6.4.2 yamt }
289 1.6.4.2 yamt
290 1.6.4.2 yamt if (wpi_alloc_rx_ring(sc, &sc->rxq) != 0) {
291 1.6.4.7 yamt aprint_error_dev(self, "could not allocate Rx ring\n");
292 1.6.4.3 yamt goto fail4;
293 1.6.4.2 yamt }
294 1.6.4.2 yamt
295 1.6.4.2 yamt ic->ic_ifp = ifp;
296 1.6.4.2 yamt ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
297 1.6.4.2 yamt ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
298 1.6.4.2 yamt ic->ic_state = IEEE80211_S_INIT;
299 1.6.4.2 yamt
300 1.6.4.2 yamt /* set device capabilities */
301 1.6.4.2 yamt ic->ic_caps =
302 1.6.4.2 yamt IEEE80211_C_IBSS | /* IBSS mode support */
303 1.6.4.2 yamt IEEE80211_C_WPA | /* 802.11i */
304 1.6.4.2 yamt IEEE80211_C_MONITOR | /* monitor mode supported */
305 1.6.4.2 yamt IEEE80211_C_TXPMGT | /* tx power management */
306 1.6.4.2 yamt IEEE80211_C_SHSLOT | /* short slot time supported */
307 1.6.4.2 yamt IEEE80211_C_SHPREAMBLE | /* short preamble supported */
308 1.6.4.2 yamt IEEE80211_C_WME; /* 802.11e */
309 1.6.4.2 yamt
310 1.6.4.4 yamt /* read supported channels and MAC address from EEPROM */
311 1.6.4.2 yamt wpi_read_eeprom(sc);
312 1.6.4.2 yamt
313 1.6.4.4 yamt /* set supported .11a, .11b, .11g rates */
314 1.6.4.3 yamt ic->ic_sup_rates[IEEE80211_MODE_11A] = wpi_rateset_11a;
315 1.6.4.2 yamt ic->ic_sup_rates[IEEE80211_MODE_11B] = wpi_rateset_11b;
316 1.6.4.2 yamt ic->ic_sup_rates[IEEE80211_MODE_11G] = wpi_rateset_11g;
317 1.6.4.2 yamt
318 1.6.4.2 yamt ic->ic_ibss_chan = &ic->ic_channels[0];
319 1.6.4.2 yamt
320 1.6.4.2 yamt ifp->if_softc = sc;
321 1.6.4.2 yamt ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
322 1.6.4.2 yamt ifp->if_init = wpi_init;
323 1.6.4.2 yamt ifp->if_stop = wpi_stop;
324 1.6.4.2 yamt ifp->if_ioctl = wpi_ioctl;
325 1.6.4.2 yamt ifp->if_start = wpi_start;
326 1.6.4.2 yamt ifp->if_watchdog = wpi_watchdog;
327 1.6.4.2 yamt IFQ_SET_READY(&ifp->if_snd);
328 1.6.4.7 yamt memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
329 1.6.4.2 yamt
330 1.6.4.2 yamt if_attach(ifp);
331 1.6.4.2 yamt ieee80211_ifattach(ic);
332 1.6.4.2 yamt /* override default methods */
333 1.6.4.2 yamt ic->ic_node_alloc = wpi_node_alloc;
334 1.6.4.2 yamt ic->ic_newassoc = wpi_newassoc;
335 1.6.4.2 yamt ic->ic_wme.wme_update = wpi_wme_update;
336 1.6.4.2 yamt
337 1.6.4.2 yamt /* override state transition machine */
338 1.6.4.2 yamt sc->sc_newstate = ic->ic_newstate;
339 1.6.4.2 yamt ic->ic_newstate = wpi_newstate;
340 1.6.4.2 yamt ieee80211_media_init(ic, wpi_media_change, ieee80211_media_status);
341 1.6.4.2 yamt
342 1.6.4.2 yamt sc->amrr.amrr_min_success_threshold = 1;
343 1.6.4.2 yamt sc->amrr.amrr_max_success_threshold = 15;
344 1.6.4.2 yamt
345 1.6.4.8 yamt wpi_sysctlattach(sc);
346 1.6.4.8 yamt
347 1.6.4.8 yamt if (!pmf_device_register(self, NULL, wpi_resume))
348 1.6.4.8 yamt aprint_error_dev(self, "couldn't establish power handler\n");
349 1.6.4.8 yamt else
350 1.6.4.8 yamt pmf_class_network_register(self, ifp);
351 1.6.4.2 yamt
352 1.6.4.2 yamt #if NBPFILTER > 0
353 1.6.4.2 yamt bpfattach2(ifp, DLT_IEEE802_11_RADIO,
354 1.6.4.2 yamt sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
355 1.6.4.2 yamt &sc->sc_drvbpf);
356 1.6.4.2 yamt
357 1.6.4.2 yamt sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
358 1.6.4.2 yamt sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
359 1.6.4.2 yamt sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT);
360 1.6.4.2 yamt
361 1.6.4.2 yamt sc->sc_txtap_len = sizeof sc->sc_txtapu;
362 1.6.4.2 yamt sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
363 1.6.4.2 yamt sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT);
364 1.6.4.2 yamt #endif
365 1.6.4.2 yamt
366 1.6.4.2 yamt ieee80211_announce(ic);
367 1.6.4.2 yamt
368 1.6.4.2 yamt return;
369 1.6.4.2 yamt
370 1.6.4.4 yamt fail4: wpi_free_tx_ring(sc, &sc->cmdq);
371 1.6.4.4 yamt fail3: while (--ac >= 0)
372 1.6.4.2 yamt wpi_free_tx_ring(sc, &sc->txq[ac]);
373 1.6.4.3 yamt wpi_free_rpool(sc);
374 1.6.4.4 yamt fail2: wpi_free_shared(sc);
375 1.6.4.4 yamt fail1: wpi_free_fwmem(sc);
376 1.6.4.2 yamt }
377 1.6.4.2 yamt
378 1.6.4.2 yamt static int
379 1.6.4.7 yamt wpi_detach(device_t self, int flags __unused)
380 1.6.4.2 yamt {
381 1.6.4.7 yamt struct wpi_softc *sc = device_private(self);
382 1.6.4.3 yamt struct ifnet *ifp = sc->sc_ic.ic_ifp;
383 1.6.4.2 yamt int ac;
384 1.6.4.2 yamt
385 1.6.4.2 yamt wpi_stop(ifp, 1);
386 1.6.4.2 yamt
387 1.6.4.2 yamt #if NBPFILTER > 0
388 1.6.4.2 yamt if (ifp != NULL)
389 1.6.4.2 yamt bpfdetach(ifp);
390 1.6.4.2 yamt #endif
391 1.6.4.2 yamt ieee80211_ifdetach(&sc->sc_ic);
392 1.6.4.2 yamt if (ifp != NULL)
393 1.6.4.2 yamt if_detach(ifp);
394 1.6.4.2 yamt
395 1.6.4.2 yamt for (ac = 0; ac < 4; ac++)
396 1.6.4.2 yamt wpi_free_tx_ring(sc, &sc->txq[ac]);
397 1.6.4.2 yamt wpi_free_tx_ring(sc, &sc->cmdq);
398 1.6.4.2 yamt wpi_free_rx_ring(sc, &sc->rxq);
399 1.6.4.3 yamt wpi_free_rpool(sc);
400 1.6.4.2 yamt wpi_free_shared(sc);
401 1.6.4.2 yamt
402 1.6.4.2 yamt if (sc->sc_ih != NULL) {
403 1.6.4.2 yamt pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
404 1.6.4.2 yamt sc->sc_ih = NULL;
405 1.6.4.2 yamt }
406 1.6.4.2 yamt
407 1.6.4.2 yamt bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
408 1.6.4.2 yamt
409 1.6.4.2 yamt return 0;
410 1.6.4.2 yamt }
411 1.6.4.2 yamt
412 1.6.4.2 yamt static int
413 1.6.4.3 yamt wpi_dma_contig_alloc(bus_dma_tag_t tag, struct wpi_dma_info *dma,
414 1.6.4.2 yamt void **kvap, bus_size_t size, bus_size_t alignment, int flags)
415 1.6.4.2 yamt {
416 1.6.4.2 yamt int nsegs, error;
417 1.6.4.2 yamt
418 1.6.4.3 yamt dma->tag = tag;
419 1.6.4.2 yamt dma->size = size;
420 1.6.4.2 yamt
421 1.6.4.3 yamt error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map);
422 1.6.4.3 yamt if (error != 0)
423 1.6.4.2 yamt goto fail;
424 1.6.4.2 yamt
425 1.6.4.3 yamt error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs,
426 1.6.4.3 yamt flags);
427 1.6.4.3 yamt if (error != 0)
428 1.6.4.2 yamt goto fail;
429 1.6.4.2 yamt
430 1.6.4.3 yamt error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags);
431 1.6.4.3 yamt if (error != 0)
432 1.6.4.2 yamt goto fail;
433 1.6.4.2 yamt
434 1.6.4.3 yamt error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags);
435 1.6.4.3 yamt if (error != 0)
436 1.6.4.2 yamt goto fail;
437 1.6.4.2 yamt
438 1.6.4.2 yamt memset(dma->vaddr, 0, size);
439 1.6.4.2 yamt
440 1.6.4.2 yamt dma->paddr = dma->map->dm_segs[0].ds_addr;
441 1.6.4.3 yamt if (kvap != NULL)
442 1.6.4.3 yamt *kvap = dma->vaddr;
443 1.6.4.2 yamt
444 1.6.4.2 yamt return 0;
445 1.6.4.2 yamt
446 1.6.4.3 yamt fail: wpi_dma_contig_free(dma);
447 1.6.4.2 yamt return error;
448 1.6.4.2 yamt }
449 1.6.4.2 yamt
450 1.6.4.2 yamt static void
451 1.6.4.3 yamt wpi_dma_contig_free(struct wpi_dma_info *dma)
452 1.6.4.2 yamt {
453 1.6.4.2 yamt if (dma->map != NULL) {
454 1.6.4.2 yamt if (dma->vaddr != NULL) {
455 1.6.4.3 yamt bus_dmamap_unload(dma->tag, dma->map);
456 1.6.4.3 yamt bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size);
457 1.6.4.3 yamt bus_dmamem_free(dma->tag, &dma->seg, 1);
458 1.6.4.2 yamt dma->vaddr = NULL;
459 1.6.4.2 yamt }
460 1.6.4.3 yamt bus_dmamap_destroy(dma->tag, dma->map);
461 1.6.4.2 yamt dma->map = NULL;
462 1.6.4.2 yamt }
463 1.6.4.2 yamt }
464 1.6.4.2 yamt
465 1.6.4.2 yamt /*
466 1.6.4.2 yamt * Allocate a shared page between host and NIC.
467 1.6.4.2 yamt */
468 1.6.4.2 yamt static int
469 1.6.4.2 yamt wpi_alloc_shared(struct wpi_softc *sc)
470 1.6.4.2 yamt {
471 1.6.4.2 yamt int error;
472 1.6.4.2 yamt /* must be aligned on a 4K-page boundary */
473 1.6.4.3 yamt error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma,
474 1.6.4.4 yamt (void **)&sc->shared, sizeof (struct wpi_shared),
475 1.6.4.4 yamt WPI_BUF_ALIGN,BUS_DMA_NOWAIT);
476 1.6.4.2 yamt if (error != 0)
477 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
478 1.6.4.7 yamt "could not allocate shared area DMA memory\n");
479 1.6.4.2 yamt
480 1.6.4.2 yamt return error;
481 1.6.4.2 yamt }
482 1.6.4.2 yamt
483 1.6.4.2 yamt static void
484 1.6.4.2 yamt wpi_free_shared(struct wpi_softc *sc)
485 1.6.4.2 yamt {
486 1.6.4.3 yamt wpi_dma_contig_free(&sc->shared_dma);
487 1.6.4.3 yamt }
488 1.6.4.3 yamt
489 1.6.4.4 yamt /*
490 1.6.4.4 yamt * Allocate DMA-safe memory for firmware transfer.
491 1.6.4.4 yamt */
492 1.6.4.4 yamt static int
493 1.6.4.4 yamt wpi_alloc_fwmem(struct wpi_softc *sc)
494 1.6.4.4 yamt {
495 1.6.4.4 yamt int error;
496 1.6.4.4 yamt /* allocate enough contiguous space to store text and data */
497 1.6.4.4 yamt error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL,
498 1.6.4.4 yamt WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 0,
499 1.6.4.4 yamt BUS_DMA_NOWAIT);
500 1.6.4.4 yamt
501 1.6.4.4 yamt if (error != 0)
502 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
503 1.6.4.7 yamt "could not allocate firmware transfer area"
504 1.6.4.7 yamt "DMA memory\n");
505 1.6.4.4 yamt return error;
506 1.6.4.4 yamt }
507 1.6.4.4 yamt
508 1.6.4.4 yamt static void
509 1.6.4.4 yamt wpi_free_fwmem(struct wpi_softc *sc)
510 1.6.4.4 yamt {
511 1.6.4.4 yamt wpi_dma_contig_free(&sc->fw_dma);
512 1.6.4.4 yamt }
513 1.6.4.4 yamt
514 1.6.4.4 yamt
515 1.6.4.3 yamt static struct wpi_rbuf *
516 1.6.4.3 yamt wpi_alloc_rbuf(struct wpi_softc *sc)
517 1.6.4.3 yamt {
518 1.6.4.3 yamt struct wpi_rbuf *rbuf;
519 1.6.4.3 yamt
520 1.6.4.3 yamt rbuf = SLIST_FIRST(&sc->rxq.freelist);
521 1.6.4.3 yamt if (rbuf == NULL)
522 1.6.4.3 yamt return NULL;
523 1.6.4.3 yamt SLIST_REMOVE_HEAD(&sc->rxq.freelist, next);
524 1.6.4.4 yamt sc->rxq.nb_free_entries --;
525 1.6.4.4 yamt
526 1.6.4.3 yamt return rbuf;
527 1.6.4.3 yamt }
528 1.6.4.3 yamt
529 1.6.4.3 yamt /*
530 1.6.4.3 yamt * This is called automatically by the network stack when the mbuf to which our
531 1.6.4.3 yamt * Rx buffer is attached is freed.
532 1.6.4.3 yamt */
533 1.6.4.3 yamt static void
534 1.6.4.4 yamt wpi_free_rbuf(struct mbuf* m, void *buf, size_t size, void *arg)
535 1.6.4.3 yamt {
536 1.6.4.3 yamt struct wpi_rbuf *rbuf = arg;
537 1.6.4.3 yamt struct wpi_softc *sc = rbuf->sc;
538 1.6.4.3 yamt
539 1.6.4.3 yamt /* put the buffer back in the free list */
540 1.6.4.4 yamt
541 1.6.4.3 yamt SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next);
542 1.6.4.4 yamt sc->rxq.nb_free_entries ++;
543 1.6.4.3 yamt
544 1.6.4.6 yamt if (__predict_true(m != NULL))
545 1.6.4.6 yamt pool_cache_put(mb_cache, m);
546 1.6.4.3 yamt }
547 1.6.4.3 yamt
548 1.6.4.3 yamt static int
549 1.6.4.3 yamt wpi_alloc_rpool(struct wpi_softc *sc)
550 1.6.4.3 yamt {
551 1.6.4.3 yamt struct wpi_rx_ring *ring = &sc->rxq;
552 1.6.4.3 yamt struct wpi_rbuf *rbuf;
553 1.6.4.3 yamt int i, error;
554 1.6.4.3 yamt
555 1.6.4.3 yamt /* allocate a big chunk of DMA'able memory.. */
556 1.6.4.3 yamt error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL,
557 1.6.4.3 yamt WPI_RBUF_COUNT * WPI_RBUF_SIZE, WPI_BUF_ALIGN, BUS_DMA_NOWAIT);
558 1.6.4.3 yamt if (error != 0) {
559 1.6.4.7 yamt aprint_normal_dev(sc->sc_dev,
560 1.6.4.7 yamt "could not allocate Rx buffers DMA memory\n");
561 1.6.4.7 yamt return error;
562 1.6.4.3 yamt }
563 1.6.4.3 yamt
564 1.6.4.3 yamt /* ..and split it into 3KB chunks */
565 1.6.4.3 yamt SLIST_INIT(&ring->freelist);
566 1.6.4.3 yamt for (i = 0; i < WPI_RBUF_COUNT; i++) {
567 1.6.4.3 yamt rbuf = &ring->rbuf[i];
568 1.6.4.3 yamt rbuf->sc = sc; /* backpointer for callbacks */
569 1.6.4.4 yamt rbuf->vaddr = (char *)ring->buf_dma.vaddr + i * WPI_RBUF_SIZE;
570 1.6.4.3 yamt rbuf->paddr = ring->buf_dma.paddr + i * WPI_RBUF_SIZE;
571 1.6.4.3 yamt
572 1.6.4.3 yamt SLIST_INSERT_HEAD(&ring->freelist, rbuf, next);
573 1.6.4.3 yamt }
574 1.6.4.4 yamt
575 1.6.4.4 yamt ring->nb_free_entries = WPI_RBUF_COUNT;
576 1.6.4.3 yamt return 0;
577 1.6.4.3 yamt }
578 1.6.4.3 yamt
579 1.6.4.3 yamt static void
580 1.6.4.3 yamt wpi_free_rpool(struct wpi_softc *sc)
581 1.6.4.3 yamt {
582 1.6.4.3 yamt wpi_dma_contig_free(&sc->rxq.buf_dma);
583 1.6.4.2 yamt }
584 1.6.4.2 yamt
585 1.6.4.2 yamt static int
586 1.6.4.2 yamt wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
587 1.6.4.2 yamt {
588 1.6.4.2 yamt struct wpi_rx_data *data;
589 1.6.4.3 yamt struct wpi_rbuf *rbuf;
590 1.6.4.2 yamt int i, error;
591 1.6.4.2 yamt
592 1.6.4.2 yamt ring->cur = 0;
593 1.6.4.2 yamt
594 1.6.4.3 yamt error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
595 1.6.4.2 yamt (void **)&ring->desc,
596 1.6.4.2 yamt WPI_RX_RING_COUNT * sizeof (struct wpi_rx_desc),
597 1.6.4.2 yamt WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
598 1.6.4.2 yamt if (error != 0) {
599 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not allocate rx ring DMA memory\n");
600 1.6.4.2 yamt goto fail;
601 1.6.4.2 yamt }
602 1.6.4.2 yamt
603 1.6.4.2 yamt /*
604 1.6.4.3 yamt * Setup Rx buffers.
605 1.6.4.2 yamt */
606 1.6.4.2 yamt for (i = 0; i < WPI_RX_RING_COUNT; i++) {
607 1.6.4.2 yamt data = &ring->data[i];
608 1.6.4.2 yamt
609 1.6.4.2 yamt MGETHDR(data->m, M_DONTWAIT, MT_DATA);
610 1.6.4.2 yamt if (data->m == NULL) {
611 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n");
612 1.6.4.2 yamt error = ENOMEM;
613 1.6.4.2 yamt goto fail;
614 1.6.4.2 yamt }
615 1.6.4.3 yamt if ((rbuf = wpi_alloc_rbuf(sc)) == NULL) {
616 1.6.4.2 yamt m_freem(data->m);
617 1.6.4.2 yamt data->m = NULL;
618 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not allocate rx cluster\n");
619 1.6.4.2 yamt error = ENOMEM;
620 1.6.4.2 yamt goto fail;
621 1.6.4.2 yamt }
622 1.6.4.3 yamt /* attach Rx buffer to mbuf */
623 1.6.4.3 yamt MEXTADD(data->m, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
624 1.6.4.3 yamt rbuf);
625 1.6.4.3 yamt data->m->m_flags |= M_EXT_RW;
626 1.6.4.2 yamt
627 1.6.4.3 yamt ring->desc[i] = htole32(rbuf->paddr);
628 1.6.4.2 yamt }
629 1.6.4.2 yamt
630 1.6.4.2 yamt return 0;
631 1.6.4.2 yamt
632 1.6.4.2 yamt fail: wpi_free_rx_ring(sc, ring);
633 1.6.4.2 yamt return error;
634 1.6.4.2 yamt }
635 1.6.4.2 yamt
636 1.6.4.2 yamt static void
637 1.6.4.2 yamt wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
638 1.6.4.2 yamt {
639 1.6.4.2 yamt int ntries;
640 1.6.4.2 yamt
641 1.6.4.2 yamt wpi_mem_lock(sc);
642 1.6.4.2 yamt
643 1.6.4.2 yamt WPI_WRITE(sc, WPI_RX_CONFIG, 0);
644 1.6.4.2 yamt for (ntries = 0; ntries < 100; ntries++) {
645 1.6.4.2 yamt if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
646 1.6.4.2 yamt break;
647 1.6.4.2 yamt DELAY(10);
648 1.6.4.2 yamt }
649 1.6.4.2 yamt #ifdef WPI_DEBUG
650 1.6.4.2 yamt if (ntries == 100 && wpi_debug > 0)
651 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "timeout resetting Rx ring\n");
652 1.6.4.2 yamt #endif
653 1.6.4.2 yamt wpi_mem_unlock(sc);
654 1.6.4.2 yamt
655 1.6.4.2 yamt ring->cur = 0;
656 1.6.4.2 yamt }
657 1.6.4.2 yamt
658 1.6.4.2 yamt static void
659 1.6.4.2 yamt wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
660 1.6.4.2 yamt {
661 1.6.4.2 yamt int i;
662 1.6.4.2 yamt
663 1.6.4.3 yamt wpi_dma_contig_free(&ring->desc_dma);
664 1.6.4.2 yamt
665 1.6.4.2 yamt for (i = 0; i < WPI_RX_RING_COUNT; i++) {
666 1.6.4.3 yamt if (ring->data[i].m != NULL)
667 1.6.4.3 yamt m_freem(ring->data[i].m);
668 1.6.4.2 yamt }
669 1.6.4.2 yamt }
670 1.6.4.2 yamt
671 1.6.4.2 yamt static int
672 1.6.4.2 yamt wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
673 1.6.4.2 yamt int qid)
674 1.6.4.2 yamt {
675 1.6.4.2 yamt struct wpi_tx_data *data;
676 1.6.4.2 yamt int i, error;
677 1.6.4.2 yamt
678 1.6.4.2 yamt ring->qid = qid;
679 1.6.4.2 yamt ring->count = count;
680 1.6.4.2 yamt ring->queued = 0;
681 1.6.4.2 yamt ring->cur = 0;
682 1.6.4.2 yamt
683 1.6.4.3 yamt error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
684 1.6.4.2 yamt (void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
685 1.6.4.2 yamt WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
686 1.6.4.2 yamt if (error != 0) {
687 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not allocate tx ring DMA memory\n");
688 1.6.4.2 yamt goto fail;
689 1.6.4.2 yamt }
690 1.6.4.2 yamt
691 1.6.4.2 yamt /* update shared page with ring's base address */
692 1.6.4.2 yamt sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
693 1.6.4.2 yamt
694 1.6.4.3 yamt error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma,
695 1.6.4.3 yamt (void **)&ring->cmd,
696 1.6.4.2 yamt count * sizeof (struct wpi_tx_cmd), 4, BUS_DMA_NOWAIT);
697 1.6.4.2 yamt if (error != 0) {
698 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not allocate tx cmd DMA memory\n");
699 1.6.4.2 yamt goto fail;
700 1.6.4.2 yamt }
701 1.6.4.2 yamt
702 1.6.4.2 yamt ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
703 1.6.4.2 yamt M_NOWAIT);
704 1.6.4.2 yamt if (ring->data == NULL) {
705 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not allocate tx data slots\n");
706 1.6.4.2 yamt goto fail;
707 1.6.4.2 yamt }
708 1.6.4.2 yamt
709 1.6.4.2 yamt memset(ring->data, 0, count * sizeof (struct wpi_tx_data));
710 1.6.4.2 yamt
711 1.6.4.2 yamt for (i = 0; i < count; i++) {
712 1.6.4.2 yamt data = &ring->data[i];
713 1.6.4.2 yamt
714 1.6.4.2 yamt error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
715 1.6.4.2 yamt WPI_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
716 1.6.4.2 yamt &data->map);
717 1.6.4.2 yamt if (error != 0) {
718 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not create tx buf DMA map\n");
719 1.6.4.2 yamt goto fail;
720 1.6.4.2 yamt }
721 1.6.4.2 yamt }
722 1.6.4.2 yamt
723 1.6.4.2 yamt return 0;
724 1.6.4.2 yamt
725 1.6.4.2 yamt fail: wpi_free_tx_ring(sc, ring);
726 1.6.4.2 yamt return error;
727 1.6.4.2 yamt }
728 1.6.4.2 yamt
729 1.6.4.2 yamt static void
730 1.6.4.2 yamt wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
731 1.6.4.2 yamt {
732 1.6.4.2 yamt struct wpi_tx_data *data;
733 1.6.4.2 yamt int i, ntries;
734 1.6.4.2 yamt
735 1.6.4.2 yamt wpi_mem_lock(sc);
736 1.6.4.2 yamt
737 1.6.4.2 yamt WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
738 1.6.4.2 yamt for (ntries = 0; ntries < 100; ntries++) {
739 1.6.4.2 yamt if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
740 1.6.4.2 yamt break;
741 1.6.4.2 yamt DELAY(10);
742 1.6.4.2 yamt }
743 1.6.4.2 yamt #ifdef WPI_DEBUG
744 1.6.4.2 yamt if (ntries == 100 && wpi_debug > 0) {
745 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "timeout resetting Tx ring %d\n",
746 1.6.4.7 yamt ring->qid);
747 1.6.4.2 yamt }
748 1.6.4.2 yamt #endif
749 1.6.4.2 yamt wpi_mem_unlock(sc);
750 1.6.4.2 yamt
751 1.6.4.2 yamt for (i = 0; i < ring->count; i++) {
752 1.6.4.2 yamt data = &ring->data[i];
753 1.6.4.2 yamt
754 1.6.4.2 yamt if (data->m != NULL) {
755 1.6.4.2 yamt bus_dmamap_unload(sc->sc_dmat, data->map);
756 1.6.4.2 yamt m_freem(data->m);
757 1.6.4.2 yamt data->m = NULL;
758 1.6.4.2 yamt }
759 1.6.4.2 yamt }
760 1.6.4.2 yamt
761 1.6.4.2 yamt ring->queued = 0;
762 1.6.4.2 yamt ring->cur = 0;
763 1.6.4.2 yamt }
764 1.6.4.2 yamt
765 1.6.4.2 yamt static void
766 1.6.4.2 yamt wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
767 1.6.4.2 yamt {
768 1.6.4.2 yamt struct wpi_tx_data *data;
769 1.6.4.2 yamt int i;
770 1.6.4.2 yamt
771 1.6.4.3 yamt wpi_dma_contig_free(&ring->desc_dma);
772 1.6.4.3 yamt wpi_dma_contig_free(&ring->cmd_dma);
773 1.6.4.2 yamt
774 1.6.4.2 yamt if (ring->data != NULL) {
775 1.6.4.2 yamt for (i = 0; i < ring->count; i++) {
776 1.6.4.2 yamt data = &ring->data[i];
777 1.6.4.2 yamt
778 1.6.4.2 yamt if (data->m != NULL) {
779 1.6.4.2 yamt bus_dmamap_unload(sc->sc_dmat, data->map);
780 1.6.4.2 yamt m_freem(data->m);
781 1.6.4.2 yamt }
782 1.6.4.2 yamt }
783 1.6.4.2 yamt free(ring->data, M_DEVBUF);
784 1.6.4.2 yamt }
785 1.6.4.2 yamt }
786 1.6.4.2 yamt
787 1.6.4.2 yamt /*ARGUSED*/
788 1.6.4.2 yamt static struct ieee80211_node *
789 1.6.4.3 yamt wpi_node_alloc(struct ieee80211_node_table *nt __unused)
790 1.6.4.2 yamt {
791 1.6.4.2 yamt struct wpi_node *wn;
792 1.6.4.2 yamt
793 1.6.4.8 yamt wn = malloc(sizeof (struct wpi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
794 1.6.4.2 yamt
795 1.6.4.2 yamt return (struct ieee80211_node *)wn;
796 1.6.4.2 yamt }
797 1.6.4.2 yamt
798 1.6.4.4 yamt static void
799 1.6.4.4 yamt wpi_newassoc(struct ieee80211_node *ni, int isnew)
800 1.6.4.4 yamt {
801 1.6.4.4 yamt struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
802 1.6.4.4 yamt int i;
803 1.6.4.4 yamt
804 1.6.4.4 yamt ieee80211_amrr_node_init(&sc->amrr, &((struct wpi_node *)ni)->amn);
805 1.6.4.4 yamt
806 1.6.4.4 yamt /* set rate to some reasonable initial value */
807 1.6.4.4 yamt for (i = ni->ni_rates.rs_nrates - 1;
808 1.6.4.4 yamt i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
809 1.6.4.4 yamt i--);
810 1.6.4.4 yamt ni->ni_txrate = i;
811 1.6.4.4 yamt }
812 1.6.4.4 yamt
813 1.6.4.2 yamt static int
814 1.6.4.2 yamt wpi_media_change(struct ifnet *ifp)
815 1.6.4.2 yamt {
816 1.6.4.2 yamt int error;
817 1.6.4.2 yamt
818 1.6.4.2 yamt error = ieee80211_media_change(ifp);
819 1.6.4.2 yamt if (error != ENETRESET)
820 1.6.4.2 yamt return error;
821 1.6.4.2 yamt
822 1.6.4.2 yamt if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
823 1.6.4.2 yamt wpi_init(ifp);
824 1.6.4.2 yamt
825 1.6.4.2 yamt return 0;
826 1.6.4.2 yamt }
827 1.6.4.2 yamt
828 1.6.4.2 yamt static int
829 1.6.4.2 yamt wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
830 1.6.4.2 yamt {
831 1.6.4.2 yamt struct ifnet *ifp = ic->ic_ifp;
832 1.6.4.2 yamt struct wpi_softc *sc = ifp->if_softc;
833 1.6.4.4 yamt struct ieee80211_node *ni;
834 1.6.4.2 yamt int error;
835 1.6.4.2 yamt
836 1.6.4.4 yamt callout_stop(&sc->calib_to);
837 1.6.4.2 yamt
838 1.6.4.2 yamt switch (nstate) {
839 1.6.4.2 yamt case IEEE80211_S_SCAN:
840 1.6.4.4 yamt
841 1.6.4.4 yamt if (sc->is_scanning)
842 1.6.4.4 yamt break;
843 1.6.4.4 yamt
844 1.6.4.4 yamt sc->is_scanning = true;
845 1.6.4.2 yamt ieee80211_node_table_reset(&ic->ic_scan);
846 1.6.4.2 yamt ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
847 1.6.4.2 yamt
848 1.6.4.2 yamt /* make the link LED blink while we're scanning */
849 1.6.4.2 yamt wpi_set_led(sc, WPI_LED_LINK, 20, 2);
850 1.6.4.2 yamt
851 1.6.4.2 yamt if ((error = wpi_scan(sc, IEEE80211_CHAN_G)) != 0) {
852 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not initiate scan\n");
853 1.6.4.2 yamt ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
854 1.6.4.2 yamt return error;
855 1.6.4.2 yamt }
856 1.6.4.2 yamt
857 1.6.4.2 yamt ic->ic_state = nstate;
858 1.6.4.2 yamt return 0;
859 1.6.4.2 yamt
860 1.6.4.3 yamt case IEEE80211_S_ASSOC:
861 1.6.4.3 yamt if (ic->ic_state != IEEE80211_S_RUN)
862 1.6.4.3 yamt break;
863 1.6.4.3 yamt /* FALLTHROUGH */
864 1.6.4.2 yamt case IEEE80211_S_AUTH:
865 1.6.4.4 yamt sc->config.associd = 0;
866 1.6.4.2 yamt sc->config.filter &= ~htole32(WPI_FILTER_BSS);
867 1.6.4.2 yamt if ((error = wpi_auth(sc)) != 0) {
868 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
869 1.6.4.7 yamt "could not send authentication request\n");
870 1.6.4.2 yamt return error;
871 1.6.4.2 yamt }
872 1.6.4.2 yamt break;
873 1.6.4.2 yamt
874 1.6.4.2 yamt case IEEE80211_S_RUN:
875 1.6.4.2 yamt if (ic->ic_opmode == IEEE80211_M_MONITOR) {
876 1.6.4.2 yamt /* link LED blinks while monitoring */
877 1.6.4.2 yamt wpi_set_led(sc, WPI_LED_LINK, 5, 5);
878 1.6.4.2 yamt break;
879 1.6.4.2 yamt }
880 1.6.4.4 yamt
881 1.6.4.4 yamt ni = ic->ic_bss;
882 1.6.4.2 yamt
883 1.6.4.2 yamt if (ic->ic_opmode != IEEE80211_M_STA) {
884 1.6.4.2 yamt (void) wpi_auth(sc); /* XXX */
885 1.6.4.4 yamt wpi_setup_beacon(sc, ni);
886 1.6.4.2 yamt }
887 1.6.4.2 yamt
888 1.6.4.4 yamt wpi_enable_tsf(sc, ni);
889 1.6.4.2 yamt
890 1.6.4.2 yamt /* update adapter's configuration */
891 1.6.4.4 yamt sc->config.associd = htole16(ni->ni_associd & ~0xc000);
892 1.6.4.2 yamt /* short preamble/slot time are negotiated when associating */
893 1.6.4.2 yamt sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
894 1.6.4.2 yamt WPI_CONFIG_SHSLOT);
895 1.6.4.2 yamt if (ic->ic_flags & IEEE80211_F_SHSLOT)
896 1.6.4.2 yamt sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
897 1.6.4.2 yamt if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
898 1.6.4.2 yamt sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
899 1.6.4.2 yamt sc->config.filter |= htole32(WPI_FILTER_BSS);
900 1.6.4.2 yamt if (ic->ic_opmode != IEEE80211_M_STA)
901 1.6.4.2 yamt sc->config.filter |= htole32(WPI_FILTER_BEACON);
902 1.6.4.2 yamt
903 1.6.4.2 yamt /* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
904 1.6.4.2 yamt
905 1.6.4.2 yamt DPRINTF(("config chan %d flags %x\n", sc->config.chan,
906 1.6.4.2 yamt sc->config.flags));
907 1.6.4.2 yamt error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
908 1.6.4.2 yamt sizeof (struct wpi_config), 1);
909 1.6.4.2 yamt if (error != 0) {
910 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not update configuration\n");
911 1.6.4.2 yamt return error;
912 1.6.4.2 yamt }
913 1.6.4.2 yamt
914 1.6.4.4 yamt /* configuration has changed, set Tx power accordingly */
915 1.6.4.4 yamt if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
916 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
917 1.6.4.4 yamt return error;
918 1.6.4.4 yamt }
919 1.6.4.4 yamt
920 1.6.4.2 yamt if (ic->ic_opmode == IEEE80211_M_STA) {
921 1.6.4.2 yamt /* fake a join to init the tx rate */
922 1.6.4.4 yamt wpi_newassoc(ni, 1);
923 1.6.4.2 yamt }
924 1.6.4.2 yamt
925 1.6.4.4 yamt /* start periodic calibration timer */
926 1.6.4.4 yamt sc->calib_cnt = 0;
927 1.6.4.7 yamt callout_schedule(&sc->calib_to, hz/2);
928 1.6.4.2 yamt
929 1.6.4.2 yamt /* link LED always on while associated */
930 1.6.4.2 yamt wpi_set_led(sc, WPI_LED_LINK, 0, 1);
931 1.6.4.2 yamt break;
932 1.6.4.2 yamt
933 1.6.4.2 yamt case IEEE80211_S_INIT:
934 1.6.4.4 yamt sc->is_scanning = false;
935 1.6.4.2 yamt break;
936 1.6.4.2 yamt }
937 1.6.4.2 yamt
938 1.6.4.2 yamt return sc->sc_newstate(ic, nstate, arg);
939 1.6.4.2 yamt }
940 1.6.4.2 yamt
941 1.6.4.2 yamt /*
942 1.6.4.4 yamt * XXX: Hack to set the current channel to the value advertised in beacons or
943 1.6.4.4 yamt * probe responses. Only used during AP detection.
944 1.6.4.4 yamt * XXX: Duplicated from if_iwi.c
945 1.6.4.4 yamt */
946 1.6.4.4 yamt static void
947 1.6.4.4 yamt wpi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
948 1.6.4.4 yamt {
949 1.6.4.4 yamt struct ieee80211_frame *wh;
950 1.6.4.4 yamt uint8_t subtype;
951 1.6.4.4 yamt uint8_t *frm, *efrm;
952 1.6.4.4 yamt
953 1.6.4.4 yamt wh = mtod(m, struct ieee80211_frame *);
954 1.6.4.4 yamt
955 1.6.4.4 yamt if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
956 1.6.4.4 yamt return;
957 1.6.4.4 yamt
958 1.6.4.4 yamt subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
959 1.6.4.4 yamt
960 1.6.4.4 yamt if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
961 1.6.4.4 yamt subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
962 1.6.4.4 yamt return;
963 1.6.4.4 yamt
964 1.6.4.4 yamt frm = (uint8_t *)(wh + 1);
965 1.6.4.4 yamt efrm = mtod(m, uint8_t *) + m->m_len;
966 1.6.4.4 yamt
967 1.6.4.4 yamt frm += 12; /* skip tstamp, bintval and capinfo fields */
968 1.6.4.4 yamt while (frm < efrm) {
969 1.6.4.4 yamt if (*frm == IEEE80211_ELEMID_DSPARMS)
970 1.6.4.4 yamt #if IEEE80211_CHAN_MAX < 255
971 1.6.4.4 yamt if (frm[2] <= IEEE80211_CHAN_MAX)
972 1.6.4.4 yamt #endif
973 1.6.4.4 yamt ic->ic_curchan = &ic->ic_channels[frm[2]];
974 1.6.4.4 yamt
975 1.6.4.4 yamt frm += frm[1] + 2;
976 1.6.4.4 yamt }
977 1.6.4.4 yamt }
978 1.6.4.4 yamt
979 1.6.4.4 yamt /*
980 1.6.4.2 yamt * Grab exclusive access to NIC memory.
981 1.6.4.2 yamt */
982 1.6.4.2 yamt static void
983 1.6.4.2 yamt wpi_mem_lock(struct wpi_softc *sc)
984 1.6.4.2 yamt {
985 1.6.4.2 yamt uint32_t tmp;
986 1.6.4.2 yamt int ntries;
987 1.6.4.2 yamt
988 1.6.4.2 yamt tmp = WPI_READ(sc, WPI_GPIO_CTL);
989 1.6.4.2 yamt WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
990 1.6.4.2 yamt
991 1.6.4.2 yamt /* spin until we actually get the lock */
992 1.6.4.2 yamt for (ntries = 0; ntries < 1000; ntries++) {
993 1.6.4.2 yamt if ((WPI_READ(sc, WPI_GPIO_CTL) &
994 1.6.4.2 yamt (WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
995 1.6.4.2 yamt break;
996 1.6.4.2 yamt DELAY(10);
997 1.6.4.2 yamt }
998 1.6.4.2 yamt if (ntries == 1000)
999 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not lock memory\n");
1000 1.6.4.2 yamt }
1001 1.6.4.2 yamt
1002 1.6.4.2 yamt /*
1003 1.6.4.2 yamt * Release lock on NIC memory.
1004 1.6.4.2 yamt */
1005 1.6.4.2 yamt static void
1006 1.6.4.2 yamt wpi_mem_unlock(struct wpi_softc *sc)
1007 1.6.4.2 yamt {
1008 1.6.4.2 yamt uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
1009 1.6.4.2 yamt WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
1010 1.6.4.2 yamt }
1011 1.6.4.2 yamt
1012 1.6.4.2 yamt static uint32_t
1013 1.6.4.2 yamt wpi_mem_read(struct wpi_softc *sc, uint16_t addr)
1014 1.6.4.2 yamt {
1015 1.6.4.2 yamt WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
1016 1.6.4.2 yamt return WPI_READ(sc, WPI_READ_MEM_DATA);
1017 1.6.4.2 yamt }
1018 1.6.4.2 yamt
1019 1.6.4.2 yamt static void
1020 1.6.4.2 yamt wpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
1021 1.6.4.2 yamt {
1022 1.6.4.2 yamt WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
1023 1.6.4.2 yamt WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
1024 1.6.4.2 yamt }
1025 1.6.4.2 yamt
1026 1.6.4.2 yamt static void
1027 1.6.4.2 yamt wpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
1028 1.6.4.4 yamt const uint32_t *data, int wlen)
1029 1.6.4.2 yamt {
1030 1.6.4.2 yamt for (; wlen > 0; wlen--, data++, addr += 4)
1031 1.6.4.2 yamt wpi_mem_write(sc, addr, *data);
1032 1.6.4.2 yamt }
1033 1.6.4.4 yamt
1034 1.6.4.2 yamt
1035 1.6.4.2 yamt /*
1036 1.6.4.4 yamt * Read `len' bytes from the EEPROM. We access the EEPROM through the MAC
1037 1.6.4.4 yamt * instead of using the traditional bit-bang method.
1038 1.6.4.2 yamt */
1039 1.6.4.4 yamt static int
1040 1.6.4.4 yamt wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len)
1041 1.6.4.2 yamt {
1042 1.6.4.4 yamt uint8_t *out = data;
1043 1.6.4.2 yamt uint32_t val;
1044 1.6.4.4 yamt int ntries;
1045 1.6.4.2 yamt
1046 1.6.4.2 yamt wpi_mem_lock(sc);
1047 1.6.4.4 yamt for (; len > 0; len -= 2, addr++) {
1048 1.6.4.4 yamt WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
1049 1.6.4.4 yamt
1050 1.6.4.4 yamt for (ntries = 0; ntries < 10; ntries++) {
1051 1.6.4.4 yamt if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) &
1052 1.6.4.4 yamt WPI_EEPROM_READY)
1053 1.6.4.4 yamt break;
1054 1.6.4.4 yamt DELAY(5);
1055 1.6.4.4 yamt }
1056 1.6.4.4 yamt if (ntries == 10) {
1057 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not read EEPROM\n");
1058 1.6.4.4 yamt return ETIMEDOUT;
1059 1.6.4.4 yamt }
1060 1.6.4.4 yamt *out++ = val >> 16;
1061 1.6.4.4 yamt if (len > 1)
1062 1.6.4.4 yamt *out++ = val >> 24;
1063 1.6.4.2 yamt }
1064 1.6.4.2 yamt wpi_mem_unlock(sc);
1065 1.6.4.2 yamt
1066 1.6.4.4 yamt return 0;
1067 1.6.4.2 yamt }
1068 1.6.4.2 yamt
1069 1.6.4.2 yamt /*
1070 1.6.4.2 yamt * The firmware boot code is small and is intended to be copied directly into
1071 1.6.4.2 yamt * the NIC internal memory.
1072 1.6.4.2 yamt */
1073 1.6.4.4 yamt int
1074 1.6.4.4 yamt wpi_load_microcode(struct wpi_softc *sc, const uint8_t *ucode, int size)
1075 1.6.4.2 yamt {
1076 1.6.4.4 yamt int ntries;
1077 1.6.4.2 yamt
1078 1.6.4.2 yamt size /= sizeof (uint32_t);
1079 1.6.4.2 yamt
1080 1.6.4.2 yamt wpi_mem_lock(sc);
1081 1.6.4.2 yamt
1082 1.6.4.2 yamt /* copy microcode image into NIC memory */
1083 1.6.4.4 yamt wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE,
1084 1.6.4.4 yamt (const uint32_t *)ucode, size);
1085 1.6.4.2 yamt
1086 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
1087 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
1088 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
1089 1.6.4.2 yamt
1090 1.6.4.2 yamt /* run microcode */
1091 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
1092 1.6.4.2 yamt
1093 1.6.4.4 yamt /* wait for transfer to complete */
1094 1.6.4.4 yamt for (ntries = 0; ntries < 1000; ntries++) {
1095 1.6.4.4 yamt if (!(wpi_mem_read(sc, WPI_MEM_UCODE_CTL) & WPI_UC_RUN))
1096 1.6.4.4 yamt break;
1097 1.6.4.4 yamt DELAY(10);
1098 1.6.4.4 yamt }
1099 1.6.4.4 yamt if (ntries == 1000) {
1100 1.6.4.4 yamt wpi_mem_unlock(sc);
1101 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
1102 1.6.4.4 yamt return ETIMEDOUT;
1103 1.6.4.4 yamt }
1104 1.6.4.4 yamt wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE);
1105 1.6.4.4 yamt
1106 1.6.4.2 yamt wpi_mem_unlock(sc);
1107 1.6.4.2 yamt
1108 1.6.4.2 yamt return 0;
1109 1.6.4.2 yamt }
1110 1.6.4.2 yamt
1111 1.6.4.2 yamt static int
1112 1.6.4.4 yamt wpi_load_firmware(struct wpi_softc *sc)
1113 1.6.4.2 yamt {
1114 1.6.4.4 yamt struct wpi_dma_info *dma = &sc->fw_dma;
1115 1.6.4.4 yamt struct wpi_firmware_hdr hdr;
1116 1.6.4.4 yamt const uint8_t *init_text, *init_data, *main_text, *main_data;
1117 1.6.4.4 yamt const uint8_t *boot_text;
1118 1.6.4.4 yamt uint32_t init_textsz, init_datasz, main_textsz, main_datasz;
1119 1.6.4.4 yamt uint32_t boot_textsz;
1120 1.6.4.4 yamt firmware_handle_t fw;
1121 1.6.4.4 yamt u_char *dfw;
1122 1.6.4.4 yamt size_t size;
1123 1.6.4.4 yamt int error;
1124 1.6.4.2 yamt
1125 1.6.4.4 yamt /* load firmware image from disk */
1126 1.6.4.4 yamt if ((error = firmware_open("if_wpi","iwlwifi-3945.ucode", &fw) != 0)) {
1127 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not read firmware file\n");
1128 1.6.4.2 yamt goto fail1;
1129 1.6.4.2 yamt }
1130 1.6.4.4 yamt
1131 1.6.4.4 yamt size = firmware_get_size(fw);
1132 1.6.4.2 yamt
1133 1.6.4.4 yamt /* extract firmware header information */
1134 1.6.4.4 yamt if (size < sizeof (struct wpi_firmware_hdr)) {
1135 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "truncated firmware header: %zu bytes\n",
1136 1.6.4.7 yamt size);
1137 1.6.4.4 yamt error = EINVAL;
1138 1.6.4.4 yamt goto fail2;
1139 1.6.4.4 yamt }
1140 1.6.4.4 yamt
1141 1.6.4.4 yamt if ((error = firmware_read(fw, 0, &hdr,
1142 1.6.4.4 yamt sizeof (struct wpi_firmware_hdr))) != 0) {
1143 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "can't get firmware header\n");
1144 1.6.4.2 yamt goto fail2;
1145 1.6.4.2 yamt }
1146 1.6.4.2 yamt
1147 1.6.4.4 yamt main_textsz = le32toh(hdr.main_textsz);
1148 1.6.4.4 yamt main_datasz = le32toh(hdr.main_datasz);
1149 1.6.4.4 yamt init_textsz = le32toh(hdr.init_textsz);
1150 1.6.4.4 yamt init_datasz = le32toh(hdr.init_datasz);
1151 1.6.4.4 yamt boot_textsz = le32toh(hdr.boot_textsz);
1152 1.6.4.4 yamt
1153 1.6.4.4 yamt /* sanity-check firmware segments sizes */
1154 1.6.4.4 yamt if (main_textsz > WPI_FW_MAIN_TEXT_MAXSZ ||
1155 1.6.4.4 yamt main_datasz > WPI_FW_MAIN_DATA_MAXSZ ||
1156 1.6.4.4 yamt init_textsz > WPI_FW_INIT_TEXT_MAXSZ ||
1157 1.6.4.4 yamt init_datasz > WPI_FW_INIT_DATA_MAXSZ ||
1158 1.6.4.4 yamt boot_textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
1159 1.6.4.4 yamt (boot_textsz & 3) != 0) {
1160 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "invalid firmware header\n");
1161 1.6.4.4 yamt error = EINVAL;
1162 1.6.4.4 yamt goto fail2;
1163 1.6.4.4 yamt }
1164 1.6.4.4 yamt
1165 1.6.4.4 yamt /* check that all firmware segments are present */
1166 1.6.4.4 yamt if (size < sizeof (struct wpi_firmware_hdr) + main_textsz +
1167 1.6.4.4 yamt main_datasz + init_textsz + init_datasz + boot_textsz) {
1168 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "firmware file too short: %zu bytes\n",
1169 1.6.4.7 yamt size);
1170 1.6.4.4 yamt error = EINVAL;
1171 1.6.4.4 yamt goto fail2;
1172 1.6.4.4 yamt }
1173 1.6.4.4 yamt
1174 1.6.4.4 yamt dfw = firmware_malloc(size);
1175 1.6.4.4 yamt if (dfw == NULL) {
1176 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "not enough memory to stock firmware\n");
1177 1.6.4.4 yamt error = ENOMEM;
1178 1.6.4.4 yamt goto fail2;
1179 1.6.4.2 yamt }
1180 1.6.4.2 yamt
1181 1.6.4.4 yamt if ((error = firmware_read(fw, 0, dfw, size)) != 0) {
1182 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "can't get firmware\n");
1183 1.6.4.4 yamt goto fail2;
1184 1.6.4.2 yamt }
1185 1.6.4.2 yamt
1186 1.6.4.4 yamt /* get pointers to firmware segments */
1187 1.6.4.4 yamt main_text = dfw + sizeof (struct wpi_firmware_hdr);
1188 1.6.4.4 yamt main_data = main_text + main_textsz;
1189 1.6.4.4 yamt init_text = main_data + main_datasz;
1190 1.6.4.4 yamt init_data = init_text + init_textsz;
1191 1.6.4.4 yamt boot_text = init_data + init_datasz;
1192 1.6.4.4 yamt
1193 1.6.4.4 yamt /* copy initialization images into pre-allocated DMA-safe memory */
1194 1.6.4.4 yamt memcpy(dma->vaddr, init_data, init_datasz);
1195 1.6.4.4 yamt memcpy((char*)dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, init_text, init_textsz);
1196 1.6.4.2 yamt
1197 1.6.4.4 yamt /* tell adapter where to find initialization images */
1198 1.6.4.4 yamt wpi_mem_lock(sc);
1199 1.6.4.4 yamt wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
1200 1.6.4.4 yamt wpi_mem_write(sc, WPI_MEM_DATA_SIZE, init_datasz);
1201 1.6.4.4 yamt wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
1202 1.6.4.4 yamt dma->paddr + WPI_FW_INIT_DATA_MAXSZ);
1203 1.6.4.4 yamt wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, init_textsz);
1204 1.6.4.4 yamt wpi_mem_unlock(sc);
1205 1.6.4.2 yamt
1206 1.6.4.4 yamt /* load firmware boot code */
1207 1.6.4.4 yamt if ((error = wpi_load_microcode(sc, boot_text, boot_textsz)) != 0) {
1208 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
1209 1.6.4.4 yamt goto fail3;
1210 1.6.4.2 yamt }
1211 1.6.4.2 yamt
1212 1.6.4.4 yamt /* now press "execute" ;-) */
1213 1.6.4.4 yamt WPI_WRITE(sc, WPI_RESET, 0);
1214 1.6.4.4 yamt
1215 1.6.4.4 yamt /* ..and wait at most one second for adapter to initialize */
1216 1.6.4.4 yamt if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
1217 1.6.4.4 yamt /* this isn't what was supposed to happen.. */
1218 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
1219 1.6.4.7 yamt "timeout waiting for adapter to initialize\n");
1220 1.6.4.4 yamt }
1221 1.6.4.4 yamt
1222 1.6.4.4 yamt /* copy runtime images into pre-allocated DMA-safe memory */
1223 1.6.4.4 yamt memcpy(dma->vaddr, main_data, main_datasz);
1224 1.6.4.4 yamt memcpy((char*)dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, main_text, main_textsz);
1225 1.6.4.4 yamt
1226 1.6.4.4 yamt /* tell adapter where to find runtime images */
1227 1.6.4.2 yamt wpi_mem_lock(sc);
1228 1.6.4.4 yamt wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
1229 1.6.4.4 yamt wpi_mem_write(sc, WPI_MEM_DATA_SIZE, main_datasz);
1230 1.6.4.4 yamt wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
1231 1.6.4.4 yamt dma->paddr + WPI_FW_MAIN_DATA_MAXSZ);
1232 1.6.4.4 yamt wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | main_textsz);
1233 1.6.4.4 yamt wpi_mem_unlock(sc);
1234 1.6.4.2 yamt
1235 1.6.4.4 yamt /* wait at most one second for second alive notification */
1236 1.6.4.4 yamt if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
1237 1.6.4.4 yamt /* this isn't what was supposed to happen.. */
1238 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
1239 1.6.4.7 yamt "timeout waiting for adapter to initialize\n");
1240 1.6.4.4 yamt }
1241 1.6.4.2 yamt
1242 1.6.4.2 yamt
1243 1.6.4.4 yamt fail3: firmware_free(dfw,size);
1244 1.6.4.4 yamt fail2: firmware_close(fw);
1245 1.6.4.4 yamt fail1: return error;
1246 1.6.4.4 yamt }
1247 1.6.4.2 yamt
1248 1.6.4.4 yamt static void
1249 1.6.4.4 yamt wpi_calib_timeout(void *arg)
1250 1.6.4.4 yamt {
1251 1.6.4.4 yamt struct wpi_softc *sc = arg;
1252 1.6.4.4 yamt struct ieee80211com *ic = &sc->sc_ic;
1253 1.6.4.4 yamt int temp, s;
1254 1.6.4.2 yamt
1255 1.6.4.4 yamt /* automatic rate control triggered every 500ms */
1256 1.6.4.4 yamt if (ic->ic_fixed_rate == -1) {
1257 1.6.4.4 yamt s = splnet();
1258 1.6.4.4 yamt if (ic->ic_opmode == IEEE80211_M_STA)
1259 1.6.4.4 yamt wpi_iter_func(sc, ic->ic_bss);
1260 1.6.4.4 yamt else
1261 1.6.4.4 yamt ieee80211_iterate_nodes(&ic->ic_sta, wpi_iter_func, sc);
1262 1.6.4.4 yamt splx(s);
1263 1.6.4.2 yamt }
1264 1.6.4.4 yamt
1265 1.6.4.4 yamt /* update sensor data */
1266 1.6.4.4 yamt temp = (int)WPI_READ(sc, WPI_TEMPERATURE);
1267 1.6.4.4 yamt
1268 1.6.4.4 yamt /* automatic power calibration every 60s */
1269 1.6.4.4 yamt if (++sc->calib_cnt >= 120) {
1270 1.6.4.4 yamt wpi_power_calibration(sc, temp);
1271 1.6.4.4 yamt sc->calib_cnt = 0;
1272 1.6.4.2 yamt }
1273 1.6.4.2 yamt
1274 1.6.4.7 yamt callout_schedule(&sc->calib_to, hz/2);
1275 1.6.4.4 yamt }
1276 1.6.4.2 yamt
1277 1.6.4.4 yamt static void
1278 1.6.4.4 yamt wpi_iter_func(void *arg, struct ieee80211_node *ni)
1279 1.6.4.4 yamt {
1280 1.6.4.4 yamt struct wpi_softc *sc = arg;
1281 1.6.4.4 yamt struct wpi_node *wn = (struct wpi_node *)ni;
1282 1.6.4.2 yamt
1283 1.6.4.4 yamt ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
1284 1.6.4.4 yamt }
1285 1.6.4.4 yamt
1286 1.6.4.4 yamt /*
1287 1.6.4.4 yamt * This function is called periodically (every 60 seconds) to adjust output
1288 1.6.4.4 yamt * power to temperature changes.
1289 1.6.4.4 yamt */
1290 1.6.4.4 yamt void
1291 1.6.4.4 yamt wpi_power_calibration(struct wpi_softc *sc, int temp)
1292 1.6.4.4 yamt {
1293 1.6.4.4 yamt /* sanity-check read value */
1294 1.6.4.4 yamt if (temp < -260 || temp > 25) {
1295 1.6.4.4 yamt /* this can't be correct, ignore */
1296 1.6.4.4 yamt DPRINTF(("out-of-range temperature reported: %d\n", temp));
1297 1.6.4.4 yamt return;
1298 1.6.4.4 yamt }
1299 1.6.4.4 yamt
1300 1.6.4.4 yamt DPRINTF(("temperature %d->%d\n", sc->temp, temp));
1301 1.6.4.4 yamt
1302 1.6.4.4 yamt /* adjust Tx power if need be */
1303 1.6.4.4 yamt if (abs(temp - sc->temp) <= 6)
1304 1.6.4.4 yamt return;
1305 1.6.4.4 yamt
1306 1.6.4.4 yamt sc->temp = temp;
1307 1.6.4.4 yamt
1308 1.6.4.4 yamt if (wpi_set_txpower(sc, sc->sc_ic.ic_bss->ni_chan, 1) != 0) {
1309 1.6.4.4 yamt /* just warn, too bad for the automatic calibration... */
1310 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not adjust Tx power\n");
1311 1.6.4.4 yamt }
1312 1.6.4.2 yamt }
1313 1.6.4.2 yamt
1314 1.6.4.2 yamt static void
1315 1.6.4.2 yamt wpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1316 1.6.4.2 yamt struct wpi_rx_data *data)
1317 1.6.4.2 yamt {
1318 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
1319 1.6.4.2 yamt struct ifnet *ifp = ic->ic_ifp;
1320 1.6.4.2 yamt struct wpi_rx_ring *ring = &sc->rxq;
1321 1.6.4.2 yamt struct wpi_rx_stat *stat;
1322 1.6.4.2 yamt struct wpi_rx_head *head;
1323 1.6.4.2 yamt struct wpi_rx_tail *tail;
1324 1.6.4.3 yamt struct wpi_rbuf *rbuf;
1325 1.6.4.2 yamt struct ieee80211_frame *wh;
1326 1.6.4.2 yamt struct ieee80211_node *ni;
1327 1.6.4.2 yamt struct mbuf *m, *mnew;
1328 1.6.4.4 yamt int data_off ;
1329 1.6.4.2 yamt
1330 1.6.4.2 yamt stat = (struct wpi_rx_stat *)(desc + 1);
1331 1.6.4.2 yamt
1332 1.6.4.2 yamt if (stat->len > WPI_STAT_MAXLEN) {
1333 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "invalid rx statistic header\n");
1334 1.6.4.2 yamt ifp->if_ierrors++;
1335 1.6.4.2 yamt return;
1336 1.6.4.2 yamt }
1337 1.6.4.2 yamt
1338 1.6.4.4 yamt head = (struct wpi_rx_head *)((char *)(stat + 1) + stat->len);
1339 1.6.4.4 yamt tail = (struct wpi_rx_tail *)((char *)(head + 1) + le16toh(head->len));
1340 1.6.4.2 yamt
1341 1.6.4.2 yamt DPRINTFN(4, ("rx intr: idx=%d len=%d stat len=%d rssi=%d rate=%x "
1342 1.6.4.4 yamt "chan=%d tstamp=%" PRId64 "\n", ring->cur, le32toh(desc->len),
1343 1.6.4.2 yamt le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
1344 1.6.4.2 yamt le64toh(tail->tstamp)));
1345 1.6.4.2 yamt
1346 1.6.4.2 yamt /*
1347 1.6.4.2 yamt * Discard Rx frames with bad CRC early (XXX we may want to pass them
1348 1.6.4.2 yamt * to radiotap in monitor mode).
1349 1.6.4.2 yamt */
1350 1.6.4.2 yamt if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1351 1.6.4.2 yamt DPRINTF(("rx tail flags error %x\n", le32toh(tail->flags)));
1352 1.6.4.2 yamt ifp->if_ierrors++;
1353 1.6.4.2 yamt return;
1354 1.6.4.2 yamt }
1355 1.6.4.2 yamt
1356 1.6.4.4 yamt /* Compute where are the useful datas */
1357 1.6.4.4 yamt data_off = (char*)(head + 1) - mtod(data->m, char*);
1358 1.6.4.4 yamt
1359 1.6.4.4 yamt /*
1360 1.6.4.4 yamt * If the number of free entry is too low
1361 1.6.4.4 yamt * just dup the data->m socket and reuse the same rbuf entry
1362 1.6.4.4 yamt */
1363 1.6.4.4 yamt if (sc->rxq.nb_free_entries <= WPI_RBUF_LOW_LIMIT) {
1364 1.6.4.4 yamt
1365 1.6.4.4 yamt /* Prepare the mbuf for the m_dup */
1366 1.6.4.4 yamt data->m->m_pkthdr.len = data->m->m_len = le16toh(head->len);
1367 1.6.4.4 yamt data->m->m_data = (char*) data->m->m_data + data_off;
1368 1.6.4.2 yamt
1369 1.6.4.4 yamt m = m_dup(data->m,0,M_COPYALL,M_DONTWAIT);
1370 1.6.4.2 yamt
1371 1.6.4.4 yamt /* Restore the m_data pointer for future use */
1372 1.6.4.4 yamt data->m->m_data = (char*) data->m->m_data - data_off;
1373 1.6.4.2 yamt
1374 1.6.4.4 yamt if (m == NULL) {
1375 1.6.4.4 yamt ifp->if_ierrors++;
1376 1.6.4.4 yamt return;
1377 1.6.4.4 yamt }
1378 1.6.4.4 yamt } else {
1379 1.6.4.2 yamt
1380 1.6.4.4 yamt MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1381 1.6.4.4 yamt if (mnew == NULL) {
1382 1.6.4.4 yamt ifp->if_ierrors++;
1383 1.6.4.4 yamt return;
1384 1.6.4.4 yamt }
1385 1.6.4.4 yamt
1386 1.6.4.4 yamt rbuf = wpi_alloc_rbuf(sc);
1387 1.6.4.4 yamt KASSERT(rbuf != NULL);
1388 1.6.4.4 yamt
1389 1.6.4.4 yamt /* attach Rx buffer to mbuf */
1390 1.6.4.4 yamt MEXTADD(mnew, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
1391 1.6.4.4 yamt rbuf);
1392 1.6.4.4 yamt mnew->m_flags |= M_EXT_RW;
1393 1.6.4.4 yamt
1394 1.6.4.4 yamt m = data->m;
1395 1.6.4.4 yamt data->m = mnew;
1396 1.6.4.2 yamt
1397 1.6.4.4 yamt /* update Rx descriptor */
1398 1.6.4.4 yamt ring->desc[ring->cur] = htole32(rbuf->paddr);
1399 1.6.4.4 yamt
1400 1.6.4.4 yamt m->m_data = (char*)m->m_data + data_off;
1401 1.6.4.4 yamt m->m_pkthdr.len = m->m_len = le16toh(head->len);
1402 1.6.4.4 yamt }
1403 1.6.4.2 yamt
1404 1.6.4.2 yamt /* finalize mbuf */
1405 1.6.4.2 yamt m->m_pkthdr.rcvif = ifp;
1406 1.6.4.2 yamt
1407 1.6.4.4 yamt if (ic->ic_state == IEEE80211_S_SCAN)
1408 1.6.4.4 yamt wpi_fix_channel(ic, m);
1409 1.6.4.4 yamt
1410 1.6.4.2 yamt #if NBPFILTER > 0
1411 1.6.4.2 yamt if (sc->sc_drvbpf != NULL) {
1412 1.6.4.2 yamt struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1413 1.6.4.2 yamt
1414 1.6.4.2 yamt tap->wr_flags = 0;
1415 1.6.4.2 yamt tap->wr_chan_freq =
1416 1.6.4.2 yamt htole16(ic->ic_channels[head->chan].ic_freq);
1417 1.6.4.2 yamt tap->wr_chan_flags =
1418 1.6.4.2 yamt htole16(ic->ic_channels[head->chan].ic_flags);
1419 1.6.4.2 yamt tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
1420 1.6.4.2 yamt tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
1421 1.6.4.2 yamt tap->wr_tsft = tail->tstamp;
1422 1.6.4.2 yamt tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
1423 1.6.4.2 yamt switch (head->rate) {
1424 1.6.4.2 yamt /* CCK rates */
1425 1.6.4.2 yamt case 10: tap->wr_rate = 2; break;
1426 1.6.4.2 yamt case 20: tap->wr_rate = 4; break;
1427 1.6.4.2 yamt case 55: tap->wr_rate = 11; break;
1428 1.6.4.2 yamt case 110: tap->wr_rate = 22; break;
1429 1.6.4.2 yamt /* OFDM rates */
1430 1.6.4.2 yamt case 0xd: tap->wr_rate = 12; break;
1431 1.6.4.2 yamt case 0xf: tap->wr_rate = 18; break;
1432 1.6.4.2 yamt case 0x5: tap->wr_rate = 24; break;
1433 1.6.4.2 yamt case 0x7: tap->wr_rate = 36; break;
1434 1.6.4.2 yamt case 0x9: tap->wr_rate = 48; break;
1435 1.6.4.2 yamt case 0xb: tap->wr_rate = 72; break;
1436 1.6.4.2 yamt case 0x1: tap->wr_rate = 96; break;
1437 1.6.4.2 yamt case 0x3: tap->wr_rate = 108; break;
1438 1.6.4.2 yamt /* unknown rate: should not happen */
1439 1.6.4.2 yamt default: tap->wr_rate = 0;
1440 1.6.4.2 yamt }
1441 1.6.4.2 yamt if (le16toh(head->flags) & 0x4)
1442 1.6.4.2 yamt tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1443 1.6.4.2 yamt
1444 1.6.4.2 yamt bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1445 1.6.4.2 yamt }
1446 1.6.4.2 yamt #endif
1447 1.6.4.2 yamt
1448 1.6.4.2 yamt /* grab a reference to the source node */
1449 1.6.4.2 yamt wh = mtod(m, struct ieee80211_frame *);
1450 1.6.4.2 yamt ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1451 1.6.4.2 yamt
1452 1.6.4.2 yamt /* send the frame to the 802.11 layer */
1453 1.6.4.2 yamt ieee80211_input(ic, m, ni, stat->rssi, 0);
1454 1.6.4.2 yamt
1455 1.6.4.2 yamt /* release node reference */
1456 1.6.4.2 yamt ieee80211_free_node(ni);
1457 1.6.4.2 yamt }
1458 1.6.4.2 yamt
1459 1.6.4.2 yamt static void
1460 1.6.4.2 yamt wpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1461 1.6.4.2 yamt {
1462 1.6.4.2 yamt struct ifnet *ifp = sc->sc_ic.ic_ifp;
1463 1.6.4.2 yamt struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1464 1.6.4.2 yamt struct wpi_tx_data *txdata = &ring->data[desc->idx];
1465 1.6.4.2 yamt struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1466 1.6.4.2 yamt struct wpi_node *wn = (struct wpi_node *)txdata->ni;
1467 1.6.4.2 yamt
1468 1.6.4.2 yamt DPRINTFN(4, ("tx done: qid=%d idx=%d retries=%d nkill=%d rate=%x "
1469 1.6.4.2 yamt "duration=%d status=%x\n", desc->qid, desc->idx, stat->ntries,
1470 1.6.4.2 yamt stat->nkill, stat->rate, le32toh(stat->duration),
1471 1.6.4.2 yamt le32toh(stat->status)));
1472 1.6.4.2 yamt
1473 1.6.4.2 yamt /*
1474 1.6.4.2 yamt * Update rate control statistics for the node.
1475 1.6.4.2 yamt * XXX we should not count mgmt frames since they're always sent at
1476 1.6.4.2 yamt * the lowest available bit-rate.
1477 1.6.4.2 yamt */
1478 1.6.4.2 yamt wn->amn.amn_txcnt++;
1479 1.6.4.2 yamt if (stat->ntries > 0) {
1480 1.6.4.2 yamt DPRINTFN(3, ("tx intr ntries %d\n", stat->ntries));
1481 1.6.4.2 yamt wn->amn.amn_retrycnt++;
1482 1.6.4.2 yamt }
1483 1.6.4.2 yamt
1484 1.6.4.2 yamt if ((le32toh(stat->status) & 0xff) != 1)
1485 1.6.4.2 yamt ifp->if_oerrors++;
1486 1.6.4.2 yamt else
1487 1.6.4.2 yamt ifp->if_opackets++;
1488 1.6.4.2 yamt
1489 1.6.4.2 yamt bus_dmamap_unload(sc->sc_dmat, txdata->map);
1490 1.6.4.2 yamt m_freem(txdata->m);
1491 1.6.4.2 yamt txdata->m = NULL;
1492 1.6.4.2 yamt ieee80211_free_node(txdata->ni);
1493 1.6.4.2 yamt txdata->ni = NULL;
1494 1.6.4.2 yamt
1495 1.6.4.2 yamt ring->queued--;
1496 1.6.4.2 yamt
1497 1.6.4.2 yamt sc->sc_tx_timer = 0;
1498 1.6.4.2 yamt ifp->if_flags &= ~IFF_OACTIVE;
1499 1.6.4.2 yamt wpi_start(ifp);
1500 1.6.4.2 yamt }
1501 1.6.4.2 yamt
1502 1.6.4.2 yamt static void
1503 1.6.4.2 yamt wpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1504 1.6.4.2 yamt {
1505 1.6.4.2 yamt struct wpi_tx_ring *ring = &sc->cmdq;
1506 1.6.4.2 yamt struct wpi_tx_data *data;
1507 1.6.4.2 yamt
1508 1.6.4.2 yamt if ((desc->qid & 7) != 4)
1509 1.6.4.2 yamt return; /* not a command ack */
1510 1.6.4.2 yamt
1511 1.6.4.2 yamt data = &ring->data[desc->idx];
1512 1.6.4.2 yamt
1513 1.6.4.2 yamt /* if the command was mapped in a mbuf, free it */
1514 1.6.4.2 yamt if (data->m != NULL) {
1515 1.6.4.2 yamt bus_dmamap_unload(sc->sc_dmat, data->map);
1516 1.6.4.2 yamt m_freem(data->m);
1517 1.6.4.2 yamt data->m = NULL;
1518 1.6.4.2 yamt }
1519 1.6.4.2 yamt
1520 1.6.4.2 yamt wakeup(&ring->cmd[desc->idx]);
1521 1.6.4.2 yamt }
1522 1.6.4.2 yamt
1523 1.6.4.2 yamt static void
1524 1.6.4.2 yamt wpi_notif_intr(struct wpi_softc *sc)
1525 1.6.4.2 yamt {
1526 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
1527 1.6.4.3 yamt struct ifnet *ifp = ic->ic_ifp;
1528 1.6.4.2 yamt struct wpi_rx_desc *desc;
1529 1.6.4.2 yamt struct wpi_rx_data *data;
1530 1.6.4.2 yamt uint32_t hw;
1531 1.6.4.2 yamt
1532 1.6.4.2 yamt hw = le32toh(sc->shared->next);
1533 1.6.4.2 yamt while (sc->rxq.cur != hw) {
1534 1.6.4.2 yamt data = &sc->rxq.data[sc->rxq.cur];
1535 1.6.4.2 yamt
1536 1.6.4.2 yamt desc = mtod(data->m, struct wpi_rx_desc *);
1537 1.6.4.2 yamt
1538 1.6.4.2 yamt DPRINTFN(4, ("rx notification qid=%x idx=%d flags=%x type=%d "
1539 1.6.4.2 yamt "len=%d\n", desc->qid, desc->idx, desc->flags,
1540 1.6.4.2 yamt desc->type, le32toh(desc->len)));
1541 1.6.4.2 yamt
1542 1.6.4.2 yamt if (!(desc->qid & 0x80)) /* reply to a command */
1543 1.6.4.2 yamt wpi_cmd_intr(sc, desc);
1544 1.6.4.2 yamt
1545 1.6.4.2 yamt switch (desc->type) {
1546 1.6.4.2 yamt case WPI_RX_DONE:
1547 1.6.4.2 yamt /* a 802.11 frame was received */
1548 1.6.4.2 yamt wpi_rx_intr(sc, desc, data);
1549 1.6.4.2 yamt break;
1550 1.6.4.2 yamt
1551 1.6.4.2 yamt case WPI_TX_DONE:
1552 1.6.4.2 yamt /* a 802.11 frame has been transmitted */
1553 1.6.4.2 yamt wpi_tx_intr(sc, desc);
1554 1.6.4.2 yamt break;
1555 1.6.4.2 yamt
1556 1.6.4.2 yamt case WPI_UC_READY:
1557 1.6.4.2 yamt {
1558 1.6.4.2 yamt struct wpi_ucode_info *uc =
1559 1.6.4.2 yamt (struct wpi_ucode_info *)(desc + 1);
1560 1.6.4.2 yamt
1561 1.6.4.2 yamt /* the microcontroller is ready */
1562 1.6.4.2 yamt DPRINTF(("microcode alive notification version %x "
1563 1.6.4.2 yamt "alive %x\n", le32toh(uc->version),
1564 1.6.4.2 yamt le32toh(uc->valid)));
1565 1.6.4.2 yamt
1566 1.6.4.2 yamt if (le32toh(uc->valid) != 1) {
1567 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
1568 1.6.4.7 yamt "microcontroller initialization failed\n");
1569 1.6.4.2 yamt }
1570 1.6.4.2 yamt break;
1571 1.6.4.2 yamt }
1572 1.6.4.2 yamt case WPI_STATE_CHANGED:
1573 1.6.4.2 yamt {
1574 1.6.4.2 yamt uint32_t *status = (uint32_t *)(desc + 1);
1575 1.6.4.2 yamt
1576 1.6.4.2 yamt /* enabled/disabled notification */
1577 1.6.4.2 yamt DPRINTF(("state changed to %x\n", le32toh(*status)));
1578 1.6.4.2 yamt
1579 1.6.4.2 yamt if (le32toh(*status) & 1) {
1580 1.6.4.2 yamt /* the radio button has to be pushed */
1581 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "Radio transmitter is off\n");
1582 1.6.4.3 yamt /* turn the interface down */
1583 1.6.4.3 yamt ifp->if_flags &= ~IFF_UP;
1584 1.6.4.3 yamt wpi_stop(ifp, 1);
1585 1.6.4.3 yamt return; /* no further processing */
1586 1.6.4.2 yamt }
1587 1.6.4.2 yamt break;
1588 1.6.4.2 yamt }
1589 1.6.4.2 yamt case WPI_START_SCAN:
1590 1.6.4.2 yamt {
1591 1.6.4.2 yamt struct wpi_start_scan *scan =
1592 1.6.4.2 yamt (struct wpi_start_scan *)(desc + 1);
1593 1.6.4.2 yamt
1594 1.6.4.2 yamt DPRINTFN(2, ("scanning channel %d status %x\n",
1595 1.6.4.2 yamt scan->chan, le32toh(scan->status)));
1596 1.6.4.2 yamt
1597 1.6.4.2 yamt /* fix current channel */
1598 1.6.4.2 yamt ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan];
1599 1.6.4.2 yamt break;
1600 1.6.4.2 yamt }
1601 1.6.4.2 yamt case WPI_STOP_SCAN:
1602 1.6.4.2 yamt {
1603 1.6.4.2 yamt struct wpi_stop_scan *scan =
1604 1.6.4.2 yamt (struct wpi_stop_scan *)(desc + 1);
1605 1.6.4.2 yamt
1606 1.6.4.2 yamt DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
1607 1.6.4.2 yamt scan->nchan, scan->status, scan->chan));
1608 1.6.4.2 yamt
1609 1.6.4.2 yamt if (scan->status == 1 && scan->chan <= 14) {
1610 1.6.4.2 yamt /*
1611 1.6.4.2 yamt * We just finished scanning 802.11g channels,
1612 1.6.4.2 yamt * start scanning 802.11a ones.
1613 1.6.4.2 yamt */
1614 1.6.4.2 yamt if (wpi_scan(sc, IEEE80211_CHAN_A) == 0)
1615 1.6.4.2 yamt break;
1616 1.6.4.2 yamt }
1617 1.6.4.4 yamt sc->is_scanning = false;
1618 1.6.4.2 yamt ieee80211_end_scan(ic);
1619 1.6.4.2 yamt break;
1620 1.6.4.2 yamt }
1621 1.6.4.2 yamt }
1622 1.6.4.2 yamt
1623 1.6.4.2 yamt sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
1624 1.6.4.2 yamt }
1625 1.6.4.2 yamt
1626 1.6.4.2 yamt /* tell the firmware what we have processed */
1627 1.6.4.2 yamt hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
1628 1.6.4.2 yamt WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
1629 1.6.4.2 yamt }
1630 1.6.4.2 yamt
1631 1.6.4.2 yamt static int
1632 1.6.4.2 yamt wpi_intr(void *arg)
1633 1.6.4.2 yamt {
1634 1.6.4.2 yamt struct wpi_softc *sc = arg;
1635 1.6.4.3 yamt struct ifnet *ifp = sc->sc_ic.ic_ifp;
1636 1.6.4.2 yamt uint32_t r;
1637 1.6.4.2 yamt
1638 1.6.4.2 yamt r = WPI_READ(sc, WPI_INTR);
1639 1.6.4.2 yamt if (r == 0 || r == 0xffffffff)
1640 1.6.4.2 yamt return 0; /* not for us */
1641 1.6.4.2 yamt
1642 1.6.4.2 yamt DPRINTFN(5, ("interrupt reg %x\n", r));
1643 1.6.4.2 yamt
1644 1.6.4.2 yamt /* disable interrupts */
1645 1.6.4.2 yamt WPI_WRITE(sc, WPI_MASK, 0);
1646 1.6.4.2 yamt /* ack interrupts */
1647 1.6.4.2 yamt WPI_WRITE(sc, WPI_INTR, r);
1648 1.6.4.2 yamt
1649 1.6.4.2 yamt if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
1650 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "fatal firmware error\n");
1651 1.6.4.2 yamt sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1652 1.6.4.3 yamt wpi_stop(sc->sc_ic.ic_ifp, 1);
1653 1.6.4.2 yamt return 1;
1654 1.6.4.2 yamt }
1655 1.6.4.2 yamt
1656 1.6.4.2 yamt if (r & WPI_RX_INTR)
1657 1.6.4.2 yamt wpi_notif_intr(sc);
1658 1.6.4.2 yamt
1659 1.6.4.2 yamt if (r & WPI_ALIVE_INTR) /* firmware initialized */
1660 1.6.4.2 yamt wakeup(sc);
1661 1.6.4.2 yamt
1662 1.6.4.2 yamt /* re-enable interrupts */
1663 1.6.4.3 yamt if (ifp->if_flags & IFF_UP)
1664 1.6.4.3 yamt WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
1665 1.6.4.2 yamt
1666 1.6.4.2 yamt return 1;
1667 1.6.4.2 yamt }
1668 1.6.4.2 yamt
1669 1.6.4.2 yamt static uint8_t
1670 1.6.4.2 yamt wpi_plcp_signal(int rate)
1671 1.6.4.2 yamt {
1672 1.6.4.2 yamt switch (rate) {
1673 1.6.4.2 yamt /* CCK rates (returned values are device-dependent) */
1674 1.6.4.2 yamt case 2: return 10;
1675 1.6.4.2 yamt case 4: return 20;
1676 1.6.4.2 yamt case 11: return 55;
1677 1.6.4.2 yamt case 22: return 110;
1678 1.6.4.2 yamt
1679 1.6.4.2 yamt /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1680 1.6.4.2 yamt /* R1-R4, (u)ral is R4-R1 */
1681 1.6.4.2 yamt case 12: return 0xd;
1682 1.6.4.2 yamt case 18: return 0xf;
1683 1.6.4.2 yamt case 24: return 0x5;
1684 1.6.4.2 yamt case 36: return 0x7;
1685 1.6.4.2 yamt case 48: return 0x9;
1686 1.6.4.2 yamt case 72: return 0xb;
1687 1.6.4.2 yamt case 96: return 0x1;
1688 1.6.4.2 yamt case 108: return 0x3;
1689 1.6.4.2 yamt
1690 1.6.4.2 yamt /* unsupported rates (should not get there) */
1691 1.6.4.2 yamt default: return 0;
1692 1.6.4.2 yamt }
1693 1.6.4.2 yamt }
1694 1.6.4.2 yamt
1695 1.6.4.2 yamt /* quickly determine if a given rate is CCK or OFDM */
1696 1.6.4.2 yamt #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1697 1.6.4.2 yamt
1698 1.6.4.2 yamt static int
1699 1.6.4.2 yamt wpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1700 1.6.4.2 yamt int ac)
1701 1.6.4.2 yamt {
1702 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
1703 1.6.4.2 yamt struct wpi_tx_ring *ring = &sc->txq[ac];
1704 1.6.4.2 yamt struct wpi_tx_desc *desc;
1705 1.6.4.2 yamt struct wpi_tx_data *data;
1706 1.6.4.2 yamt struct wpi_tx_cmd *cmd;
1707 1.6.4.2 yamt struct wpi_cmd_data *tx;
1708 1.6.4.2 yamt struct ieee80211_frame *wh;
1709 1.6.4.2 yamt struct ieee80211_key *k;
1710 1.6.4.2 yamt const struct chanAccParams *cap;
1711 1.6.4.2 yamt struct mbuf *mnew;
1712 1.6.4.2 yamt int i, error, rate, hdrlen, noack = 0;
1713 1.6.4.2 yamt
1714 1.6.4.2 yamt desc = &ring->desc[ring->cur];
1715 1.6.4.2 yamt data = &ring->data[ring->cur];
1716 1.6.4.2 yamt
1717 1.6.4.2 yamt wh = mtod(m0, struct ieee80211_frame *);
1718 1.6.4.2 yamt
1719 1.6.4.2 yamt if (IEEE80211_QOS_HAS_SEQ(wh)) {
1720 1.6.4.2 yamt cap = &ic->ic_wme.wme_chanParams;
1721 1.6.4.2 yamt noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1722 1.6.4.5 yamt }
1723 1.6.4.2 yamt
1724 1.6.4.2 yamt if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1725 1.6.4.2 yamt k = ieee80211_crypto_encap(ic, ni, m0);
1726 1.6.4.2 yamt if (k == NULL) {
1727 1.6.4.2 yamt m_freem(m0);
1728 1.6.4.2 yamt return ENOBUFS;
1729 1.6.4.2 yamt }
1730 1.6.4.2 yamt
1731 1.6.4.2 yamt /* packet header may have moved, reset our local pointer */
1732 1.6.4.2 yamt wh = mtod(m0, struct ieee80211_frame *);
1733 1.6.4.2 yamt }
1734 1.6.4.2 yamt
1735 1.6.4.5 yamt hdrlen = ieee80211_anyhdrsize(wh);
1736 1.6.4.5 yamt
1737 1.6.4.2 yamt /* pickup a rate */
1738 1.6.4.2 yamt if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1739 1.6.4.2 yamt IEEE80211_FC0_TYPE_MGT) {
1740 1.6.4.2 yamt /* mgmt frames are sent at the lowest available bit-rate */
1741 1.6.4.2 yamt rate = ni->ni_rates.rs_rates[0];
1742 1.6.4.2 yamt } else {
1743 1.6.4.2 yamt if (ic->ic_fixed_rate != -1) {
1744 1.6.4.2 yamt rate = ic->ic_sup_rates[ic->ic_curmode].
1745 1.6.4.2 yamt rs_rates[ic->ic_fixed_rate];
1746 1.6.4.2 yamt } else
1747 1.6.4.2 yamt rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1748 1.6.4.2 yamt }
1749 1.6.4.2 yamt rate &= IEEE80211_RATE_VAL;
1750 1.6.4.2 yamt
1751 1.6.4.2 yamt
1752 1.6.4.2 yamt #if NBPFILTER > 0
1753 1.6.4.2 yamt if (sc->sc_drvbpf != NULL) {
1754 1.6.4.2 yamt struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
1755 1.6.4.2 yamt
1756 1.6.4.2 yamt tap->wt_flags = 0;
1757 1.6.4.2 yamt tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
1758 1.6.4.2 yamt tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
1759 1.6.4.2 yamt tap->wt_rate = rate;
1760 1.6.4.2 yamt tap->wt_hwqueue = ac;
1761 1.6.4.2 yamt if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1762 1.6.4.2 yamt tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1763 1.6.4.2 yamt
1764 1.6.4.2 yamt bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1765 1.6.4.2 yamt }
1766 1.6.4.2 yamt #endif
1767 1.6.4.2 yamt
1768 1.6.4.2 yamt cmd = &ring->cmd[ring->cur];
1769 1.6.4.2 yamt cmd->code = WPI_CMD_TX_DATA;
1770 1.6.4.2 yamt cmd->flags = 0;
1771 1.6.4.2 yamt cmd->qid = ring->qid;
1772 1.6.4.2 yamt cmd->idx = ring->cur;
1773 1.6.4.2 yamt
1774 1.6.4.2 yamt tx = (struct wpi_cmd_data *)cmd->data;
1775 1.6.4.2 yamt tx->flags = 0;
1776 1.6.4.2 yamt
1777 1.6.4.2 yamt if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1778 1.6.4.2 yamt tx->flags |= htole32(WPI_TX_NEED_ACK);
1779 1.6.4.3 yamt } else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
1780 1.6.4.3 yamt tx->flags |= htole32(WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP);
1781 1.6.4.2 yamt
1782 1.6.4.2 yamt tx->flags |= htole32(WPI_TX_AUTO_SEQ);
1783 1.6.4.2 yamt
1784 1.6.4.3 yamt /* retrieve destination node's id */
1785 1.6.4.3 yamt tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? WPI_ID_BROADCAST :
1786 1.6.4.3 yamt WPI_ID_BSS;
1787 1.6.4.3 yamt
1788 1.6.4.2 yamt if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1789 1.6.4.2 yamt IEEE80211_FC0_TYPE_MGT) {
1790 1.6.4.2 yamt /* tell h/w to set timestamp in probe responses */
1791 1.6.4.2 yamt if ((wh->i_fc[0] &
1792 1.6.4.2 yamt (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1793 1.6.4.2 yamt (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1794 1.6.4.2 yamt tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
1795 1.6.4.2 yamt
1796 1.6.4.2 yamt if (((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1797 1.6.4.2 yamt IEEE80211_FC0_SUBTYPE_ASSOC_REQ) ||
1798 1.6.4.2 yamt ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1799 1.6.4.2 yamt IEEE80211_FC0_SUBTYPE_REASSOC_REQ))
1800 1.6.4.2 yamt tx->timeout = htole16(3);
1801 1.6.4.2 yamt else
1802 1.6.4.2 yamt tx->timeout = htole16(2);
1803 1.6.4.2 yamt } else
1804 1.6.4.2 yamt tx->timeout = htole16(0);
1805 1.6.4.2 yamt
1806 1.6.4.2 yamt tx->rate = wpi_plcp_signal(rate);
1807 1.6.4.2 yamt
1808 1.6.4.2 yamt /* be very persistant at sending frames out */
1809 1.6.4.2 yamt tx->rts_ntries = 7;
1810 1.6.4.2 yamt tx->data_ntries = 15;
1811 1.6.4.2 yamt
1812 1.6.4.2 yamt tx->ofdm_mask = 0xff;
1813 1.6.4.2 yamt tx->cck_mask = 0xf;
1814 1.6.4.4 yamt tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
1815 1.6.4.2 yamt
1816 1.6.4.2 yamt tx->len = htole16(m0->m_pkthdr.len);
1817 1.6.4.2 yamt
1818 1.6.4.2 yamt /* save and trim IEEE802.11 header */
1819 1.6.4.5 yamt memcpy((uint8_t *)(tx + 1), wh, hdrlen);
1820 1.6.4.2 yamt m_adj(m0, hdrlen);
1821 1.6.4.2 yamt
1822 1.6.4.2 yamt error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1823 1.6.4.2 yamt BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1824 1.6.4.2 yamt if (error != 0 && error != EFBIG) {
1825 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n", error);
1826 1.6.4.2 yamt m_freem(m0);
1827 1.6.4.2 yamt return error;
1828 1.6.4.2 yamt }
1829 1.6.4.2 yamt if (error != 0) {
1830 1.6.4.2 yamt /* too many fragments, linearize */
1831 1.6.4.2 yamt MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1832 1.6.4.2 yamt if (mnew == NULL) {
1833 1.6.4.2 yamt m_freem(m0);
1834 1.6.4.2 yamt return ENOMEM;
1835 1.6.4.2 yamt }
1836 1.6.4.2 yamt
1837 1.6.4.2 yamt M_COPY_PKTHDR(mnew, m0);
1838 1.6.4.2 yamt if (m0->m_pkthdr.len > MHLEN) {
1839 1.6.4.2 yamt MCLGET(mnew, M_DONTWAIT);
1840 1.6.4.2 yamt if (!(mnew->m_flags & M_EXT)) {
1841 1.6.4.2 yamt m_freem(m0);
1842 1.6.4.2 yamt m_freem(mnew);
1843 1.6.4.2 yamt return ENOMEM;
1844 1.6.4.2 yamt }
1845 1.6.4.2 yamt }
1846 1.6.4.2 yamt
1847 1.6.4.4 yamt m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
1848 1.6.4.2 yamt m_freem(m0);
1849 1.6.4.2 yamt mnew->m_len = mnew->m_pkthdr.len;
1850 1.6.4.2 yamt m0 = mnew;
1851 1.6.4.2 yamt
1852 1.6.4.2 yamt error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1853 1.6.4.2 yamt BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1854 1.6.4.2 yamt if (error != 0) {
1855 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
1856 1.6.4.7 yamt error);
1857 1.6.4.2 yamt m_freem(m0);
1858 1.6.4.2 yamt return error;
1859 1.6.4.2 yamt }
1860 1.6.4.2 yamt }
1861 1.6.4.2 yamt
1862 1.6.4.2 yamt data->m = m0;
1863 1.6.4.2 yamt data->ni = ni;
1864 1.6.4.2 yamt
1865 1.6.4.2 yamt DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
1866 1.6.4.2 yamt ring->qid, ring->cur, m0->m_pkthdr.len, data->map->dm_nsegs));
1867 1.6.4.2 yamt
1868 1.6.4.2 yamt /* first scatter/gather segment is used by the tx data command */
1869 1.6.4.2 yamt desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
1870 1.6.4.2 yamt (1 + data->map->dm_nsegs) << 24);
1871 1.6.4.2 yamt desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
1872 1.6.4.2 yamt ring->cur * sizeof (struct wpi_tx_cmd));
1873 1.6.4.5 yamt desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_data) +
1874 1.6.4.5 yamt ((hdrlen + 3) & ~3));
1875 1.6.4.2 yamt
1876 1.6.4.2 yamt for (i = 1; i <= data->map->dm_nsegs; i++) {
1877 1.6.4.2 yamt desc->segs[i].addr =
1878 1.6.4.2 yamt htole32(data->map->dm_segs[i - 1].ds_addr);
1879 1.6.4.2 yamt desc->segs[i].len =
1880 1.6.4.2 yamt htole32(data->map->dm_segs[i - 1].ds_len);
1881 1.6.4.2 yamt }
1882 1.6.4.2 yamt
1883 1.6.4.2 yamt ring->queued++;
1884 1.6.4.2 yamt
1885 1.6.4.2 yamt /* kick ring */
1886 1.6.4.2 yamt ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
1887 1.6.4.2 yamt WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
1888 1.6.4.2 yamt
1889 1.6.4.2 yamt return 0;
1890 1.6.4.2 yamt }
1891 1.6.4.2 yamt
1892 1.6.4.2 yamt static void
1893 1.6.4.2 yamt wpi_start(struct ifnet *ifp)
1894 1.6.4.2 yamt {
1895 1.6.4.2 yamt struct wpi_softc *sc = ifp->if_softc;
1896 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
1897 1.6.4.2 yamt struct ieee80211_node *ni;
1898 1.6.4.2 yamt struct ether_header *eh;
1899 1.6.4.2 yamt struct mbuf *m0;
1900 1.6.4.2 yamt int ac;
1901 1.6.4.2 yamt
1902 1.6.4.2 yamt /*
1903 1.6.4.2 yamt * net80211 may still try to send management frames even if the
1904 1.6.4.2 yamt * IFF_RUNNING flag is not set...
1905 1.6.4.2 yamt */
1906 1.6.4.2 yamt if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1907 1.6.4.2 yamt return;
1908 1.6.4.2 yamt
1909 1.6.4.2 yamt for (;;) {
1910 1.6.4.4 yamt IF_DEQUEUE(&ic->ic_mgtq, m0);
1911 1.6.4.2 yamt if (m0 != NULL) {
1912 1.6.4.2 yamt
1913 1.6.4.2 yamt ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1914 1.6.4.2 yamt m0->m_pkthdr.rcvif = NULL;
1915 1.6.4.2 yamt
1916 1.6.4.2 yamt /* management frames go into ring 0 */
1917 1.6.4.2 yamt if (sc->txq[0].queued > sc->txq[0].count - 8) {
1918 1.6.4.2 yamt ifp->if_oerrors++;
1919 1.6.4.2 yamt continue;
1920 1.6.4.2 yamt }
1921 1.6.4.2 yamt #if NBPFILTER > 0
1922 1.6.4.2 yamt if (ic->ic_rawbpf != NULL)
1923 1.6.4.2 yamt bpf_mtap(ic->ic_rawbpf, m0);
1924 1.6.4.2 yamt #endif
1925 1.6.4.2 yamt if (wpi_tx_data(sc, m0, ni, 0) != 0) {
1926 1.6.4.2 yamt ifp->if_oerrors++;
1927 1.6.4.2 yamt break;
1928 1.6.4.2 yamt }
1929 1.6.4.2 yamt } else {
1930 1.6.4.2 yamt if (ic->ic_state != IEEE80211_S_RUN)
1931 1.6.4.2 yamt break;
1932 1.6.4.3 yamt IFQ_POLL(&ifp->if_snd, m0);
1933 1.6.4.2 yamt if (m0 == NULL)
1934 1.6.4.2 yamt break;
1935 1.6.4.2 yamt
1936 1.6.4.2 yamt if (m0->m_len < sizeof (*eh) &&
1937 1.6.4.2 yamt (m0 = m_pullup(m0, sizeof (*eh))) != NULL) {
1938 1.6.4.2 yamt ifp->if_oerrors++;
1939 1.6.4.2 yamt continue;
1940 1.6.4.2 yamt }
1941 1.6.4.2 yamt eh = mtod(m0, struct ether_header *);
1942 1.6.4.2 yamt ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1943 1.6.4.2 yamt if (ni == NULL) {
1944 1.6.4.2 yamt m_freem(m0);
1945 1.6.4.2 yamt ifp->if_oerrors++;
1946 1.6.4.2 yamt continue;
1947 1.6.4.2 yamt }
1948 1.6.4.2 yamt
1949 1.6.4.2 yamt /* classify mbuf so we can find which tx ring to use */
1950 1.6.4.2 yamt if (ieee80211_classify(ic, m0, ni) != 0) {
1951 1.6.4.2 yamt m_freem(m0);
1952 1.6.4.2 yamt ieee80211_free_node(ni);
1953 1.6.4.2 yamt ifp->if_oerrors++;
1954 1.6.4.2 yamt continue;
1955 1.6.4.2 yamt }
1956 1.6.4.2 yamt
1957 1.6.4.2 yamt /* no QoS encapsulation for EAPOL frames */
1958 1.6.4.2 yamt ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1959 1.6.4.2 yamt M_WME_GETAC(m0) : WME_AC_BE;
1960 1.6.4.2 yamt
1961 1.6.4.2 yamt if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
1962 1.6.4.2 yamt /* there is no place left in this ring */
1963 1.6.4.2 yamt ifp->if_flags |= IFF_OACTIVE;
1964 1.6.4.2 yamt break;
1965 1.6.4.2 yamt }
1966 1.6.4.3 yamt IFQ_DEQUEUE(&ifp->if_snd, m0);
1967 1.6.4.2 yamt #if NBPFILTER > 0
1968 1.6.4.2 yamt if (ifp->if_bpf != NULL)
1969 1.6.4.2 yamt bpf_mtap(ifp->if_bpf, m0);
1970 1.6.4.2 yamt #endif
1971 1.6.4.2 yamt m0 = ieee80211_encap(ic, m0, ni);
1972 1.6.4.2 yamt if (m0 == NULL) {
1973 1.6.4.2 yamt ieee80211_free_node(ni);
1974 1.6.4.2 yamt ifp->if_oerrors++;
1975 1.6.4.2 yamt continue;
1976 1.6.4.2 yamt }
1977 1.6.4.2 yamt #if NBPFILTER > 0
1978 1.6.4.2 yamt if (ic->ic_rawbpf != NULL)
1979 1.6.4.2 yamt bpf_mtap(ic->ic_rawbpf, m0);
1980 1.6.4.2 yamt #endif
1981 1.6.4.2 yamt if (wpi_tx_data(sc, m0, ni, ac) != 0) {
1982 1.6.4.2 yamt ieee80211_free_node(ni);
1983 1.6.4.2 yamt ifp->if_oerrors++;
1984 1.6.4.2 yamt break;
1985 1.6.4.2 yamt }
1986 1.6.4.2 yamt }
1987 1.6.4.2 yamt
1988 1.6.4.2 yamt sc->sc_tx_timer = 5;
1989 1.6.4.2 yamt ifp->if_timer = 1;
1990 1.6.4.2 yamt }
1991 1.6.4.2 yamt }
1992 1.6.4.2 yamt
1993 1.6.4.2 yamt static void
1994 1.6.4.2 yamt wpi_watchdog(struct ifnet *ifp)
1995 1.6.4.2 yamt {
1996 1.6.4.2 yamt struct wpi_softc *sc = ifp->if_softc;
1997 1.6.4.2 yamt
1998 1.6.4.2 yamt ifp->if_timer = 0;
1999 1.6.4.2 yamt
2000 1.6.4.2 yamt if (sc->sc_tx_timer > 0) {
2001 1.6.4.2 yamt if (--sc->sc_tx_timer == 0) {
2002 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "device timeout\n");
2003 1.6.4.2 yamt ifp->if_oerrors++;
2004 1.6.4.2 yamt ifp->if_flags &= ~IFF_UP;
2005 1.6.4.2 yamt wpi_stop(ifp, 1);
2006 1.6.4.2 yamt return;
2007 1.6.4.2 yamt }
2008 1.6.4.2 yamt ifp->if_timer = 1;
2009 1.6.4.2 yamt }
2010 1.6.4.2 yamt
2011 1.6.4.2 yamt ieee80211_watchdog(&sc->sc_ic);
2012 1.6.4.2 yamt }
2013 1.6.4.2 yamt
2014 1.6.4.2 yamt static int
2015 1.6.4.4 yamt wpi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2016 1.6.4.2 yamt {
2017 1.6.4.2 yamt #define IS_RUNNING(ifp) \
2018 1.6.4.2 yamt ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
2019 1.6.4.2 yamt
2020 1.6.4.2 yamt struct wpi_softc *sc = ifp->if_softc;
2021 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
2022 1.6.4.2 yamt int s, error = 0;
2023 1.6.4.2 yamt
2024 1.6.4.2 yamt s = splnet();
2025 1.6.4.2 yamt
2026 1.6.4.2 yamt switch (cmd) {
2027 1.6.4.2 yamt case SIOCSIFFLAGS:
2028 1.6.4.2 yamt if (ifp->if_flags & IFF_UP) {
2029 1.6.4.2 yamt if (!(ifp->if_flags & IFF_RUNNING))
2030 1.6.4.2 yamt wpi_init(ifp);
2031 1.6.4.2 yamt } else {
2032 1.6.4.2 yamt if (ifp->if_flags & IFF_RUNNING)
2033 1.6.4.2 yamt wpi_stop(ifp, 1);
2034 1.6.4.2 yamt }
2035 1.6.4.2 yamt break;
2036 1.6.4.2 yamt
2037 1.6.4.2 yamt case SIOCADDMULTI:
2038 1.6.4.2 yamt case SIOCDELMULTI:
2039 1.6.4.4 yamt /* XXX no h/w multicast filter? --dyoung */
2040 1.6.4.4 yamt if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
2041 1.6.4.2 yamt /* setup multicast filter, etc */
2042 1.6.4.2 yamt error = 0;
2043 1.6.4.2 yamt }
2044 1.6.4.2 yamt break;
2045 1.6.4.2 yamt
2046 1.6.4.2 yamt default:
2047 1.6.4.3 yamt error = ieee80211_ioctl(ic, cmd, data);
2048 1.6.4.2 yamt }
2049 1.6.4.2 yamt
2050 1.6.4.2 yamt if (error == ENETRESET) {
2051 1.6.4.2 yamt if (IS_RUNNING(ifp) &&
2052 1.6.4.2 yamt (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2053 1.6.4.2 yamt wpi_init(ifp);
2054 1.6.4.2 yamt error = 0;
2055 1.6.4.2 yamt }
2056 1.6.4.2 yamt
2057 1.6.4.2 yamt splx(s);
2058 1.6.4.2 yamt return error;
2059 1.6.4.2 yamt
2060 1.6.4.2 yamt #undef IS_RUNNING
2061 1.6.4.2 yamt }
2062 1.6.4.2 yamt
2063 1.6.4.2 yamt /*
2064 1.6.4.2 yamt * Extract various information from EEPROM.
2065 1.6.4.2 yamt */
2066 1.6.4.2 yamt static void
2067 1.6.4.2 yamt wpi_read_eeprom(struct wpi_softc *sc)
2068 1.6.4.2 yamt {
2069 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
2070 1.6.4.4 yamt char domain[4];
2071 1.6.4.2 yamt int i;
2072 1.6.4.2 yamt
2073 1.6.4.4 yamt wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap, 1);
2074 1.6.4.4 yamt wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev, 2);
2075 1.6.4.4 yamt wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1);
2076 1.6.4.4 yamt
2077 1.6.4.4 yamt DPRINTF(("cap=%x rev=%x type=%x\n", sc->cap, le16toh(sc->rev),
2078 1.6.4.4 yamt sc->type));
2079 1.6.4.4 yamt
2080 1.6.4.4 yamt /* read and print regulatory domain */
2081 1.6.4.4 yamt wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, domain, 4);
2082 1.6.4.7 yamt aprint_normal_dev(sc->sc_dev, "%.4s", domain);
2083 1.6.4.4 yamt
2084 1.6.4.4 yamt /* read and print MAC address */
2085 1.6.4.4 yamt wpi_read_prom_data(sc, WPI_EEPROM_MAC, ic->ic_myaddr, 6);
2086 1.6.4.4 yamt aprint_normal(", address %s\n", ether_sprintf(ic->ic_myaddr));
2087 1.6.4.4 yamt
2088 1.6.4.4 yamt /* read the list of authorized channels */
2089 1.6.4.4 yamt for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
2090 1.6.4.4 yamt wpi_read_eeprom_channels(sc, i);
2091 1.6.4.4 yamt
2092 1.6.4.4 yamt /* read the list of power groups */
2093 1.6.4.4 yamt for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
2094 1.6.4.4 yamt wpi_read_eeprom_group(sc, i);
2095 1.6.4.4 yamt }
2096 1.6.4.4 yamt
2097 1.6.4.4 yamt static void
2098 1.6.4.4 yamt wpi_read_eeprom_channels(struct wpi_softc *sc, int n)
2099 1.6.4.4 yamt {
2100 1.6.4.4 yamt struct ieee80211com *ic = &sc->sc_ic;
2101 1.6.4.4 yamt const struct wpi_chan_band *band = &wpi_bands[n];
2102 1.6.4.4 yamt struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
2103 1.6.4.4 yamt int chan, i;
2104 1.6.4.4 yamt
2105 1.6.4.4 yamt wpi_read_prom_data(sc, band->addr, channels,
2106 1.6.4.4 yamt band->nchan * sizeof (struct wpi_eeprom_chan));
2107 1.6.4.4 yamt
2108 1.6.4.4 yamt for (i = 0; i < band->nchan; i++) {
2109 1.6.4.4 yamt if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID))
2110 1.6.4.4 yamt continue;
2111 1.6.4.4 yamt
2112 1.6.4.4 yamt chan = band->chan[i];
2113 1.6.4.4 yamt
2114 1.6.4.4 yamt if (n == 0) { /* 2GHz band */
2115 1.6.4.4 yamt ic->ic_channels[chan].ic_freq =
2116 1.6.4.4 yamt ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
2117 1.6.4.4 yamt ic->ic_channels[chan].ic_flags =
2118 1.6.4.4 yamt IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
2119 1.6.4.4 yamt IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
2120 1.6.4.4 yamt
2121 1.6.4.4 yamt } else { /* 5GHz band */
2122 1.6.4.4 yamt /*
2123 1.6.4.4 yamt * Some 3945abg adapters support channels 7, 8, 11
2124 1.6.4.4 yamt * and 12 in the 2GHz *and* 5GHz bands.
2125 1.6.4.4 yamt * Because of limitations in our net80211(9) stack,
2126 1.6.4.4 yamt * we can't support these channels in 5GHz band.
2127 1.6.4.4 yamt */
2128 1.6.4.4 yamt if (chan <= 14)
2129 1.6.4.4 yamt continue;
2130 1.6.4.4 yamt
2131 1.6.4.4 yamt ic->ic_channels[chan].ic_freq =
2132 1.6.4.4 yamt ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
2133 1.6.4.4 yamt ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
2134 1.6.4.4 yamt }
2135 1.6.4.4 yamt
2136 1.6.4.4 yamt /* is active scan allowed on this channel? */
2137 1.6.4.4 yamt if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
2138 1.6.4.4 yamt ic->ic_channels[chan].ic_flags |=
2139 1.6.4.4 yamt IEEE80211_CHAN_PASSIVE;
2140 1.6.4.4 yamt }
2141 1.6.4.4 yamt
2142 1.6.4.4 yamt /* save maximum allowed power for this channel */
2143 1.6.4.4 yamt sc->maxpwr[chan] = channels[i].maxpwr;
2144 1.6.4.4 yamt
2145 1.6.4.4 yamt DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n",
2146 1.6.4.4 yamt chan, channels[i].flags, sc->maxpwr[chan]));
2147 1.6.4.4 yamt }
2148 1.6.4.4 yamt }
2149 1.6.4.4 yamt
2150 1.6.4.4 yamt static void
2151 1.6.4.4 yamt wpi_read_eeprom_group(struct wpi_softc *sc, int n)
2152 1.6.4.4 yamt {
2153 1.6.4.4 yamt struct wpi_power_group *group = &sc->groups[n];
2154 1.6.4.4 yamt struct wpi_eeprom_group rgroup;
2155 1.6.4.4 yamt int i;
2156 1.6.4.4 yamt
2157 1.6.4.4 yamt wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup,
2158 1.6.4.4 yamt sizeof rgroup);
2159 1.6.4.4 yamt
2160 1.6.4.4 yamt /* save power group information */
2161 1.6.4.4 yamt group->chan = rgroup.chan;
2162 1.6.4.4 yamt group->maxpwr = rgroup.maxpwr;
2163 1.6.4.4 yamt /* temperature at which the samples were taken */
2164 1.6.4.4 yamt group->temp = (int16_t)le16toh(rgroup.temp);
2165 1.6.4.4 yamt
2166 1.6.4.4 yamt DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n,
2167 1.6.4.4 yamt group->chan, group->maxpwr, group->temp));
2168 1.6.4.4 yamt
2169 1.6.4.4 yamt for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
2170 1.6.4.4 yamt group->samples[i].index = rgroup.samples[i].index;
2171 1.6.4.4 yamt group->samples[i].power = rgroup.samples[i].power;
2172 1.6.4.4 yamt
2173 1.6.4.4 yamt DPRINTF(("\tsample %d: index=%d power=%d\n", i,
2174 1.6.4.4 yamt group->samples[i].index, group->samples[i].power));
2175 1.6.4.2 yamt }
2176 1.6.4.2 yamt }
2177 1.6.4.2 yamt
2178 1.6.4.2 yamt /*
2179 1.6.4.2 yamt * Send a command to the firmware.
2180 1.6.4.2 yamt */
2181 1.6.4.2 yamt static int
2182 1.6.4.2 yamt wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
2183 1.6.4.2 yamt {
2184 1.6.4.2 yamt struct wpi_tx_ring *ring = &sc->cmdq;
2185 1.6.4.2 yamt struct wpi_tx_desc *desc;
2186 1.6.4.2 yamt struct wpi_tx_cmd *cmd;
2187 1.6.4.2 yamt
2188 1.6.4.2 yamt KASSERT(size <= sizeof cmd->data);
2189 1.6.4.2 yamt
2190 1.6.4.2 yamt desc = &ring->desc[ring->cur];
2191 1.6.4.2 yamt cmd = &ring->cmd[ring->cur];
2192 1.6.4.2 yamt
2193 1.6.4.2 yamt cmd->code = code;
2194 1.6.4.2 yamt cmd->flags = 0;
2195 1.6.4.2 yamt cmd->qid = ring->qid;
2196 1.6.4.2 yamt cmd->idx = ring->cur;
2197 1.6.4.2 yamt memcpy(cmd->data, buf, size);
2198 1.6.4.2 yamt
2199 1.6.4.2 yamt desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
2200 1.6.4.2 yamt desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2201 1.6.4.2 yamt ring->cur * sizeof (struct wpi_tx_cmd));
2202 1.6.4.2 yamt desc->segs[0].len = htole32(4 + size);
2203 1.6.4.2 yamt
2204 1.6.4.2 yamt /* kick cmd ring */
2205 1.6.4.2 yamt ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2206 1.6.4.2 yamt WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2207 1.6.4.2 yamt
2208 1.6.4.2 yamt return async ? 0 : tsleep(cmd, PCATCH, "wpicmd", hz);
2209 1.6.4.2 yamt }
2210 1.6.4.2 yamt
2211 1.6.4.2 yamt static int
2212 1.6.4.2 yamt wpi_wme_update(struct ieee80211com *ic)
2213 1.6.4.2 yamt {
2214 1.6.4.2 yamt #define WPI_EXP2(v) htole16((1 << (v)) - 1)
2215 1.6.4.2 yamt #define WPI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
2216 1.6.4.2 yamt struct wpi_softc *sc = ic->ic_ifp->if_softc;
2217 1.6.4.2 yamt const struct wmeParams *wmep;
2218 1.6.4.2 yamt struct wpi_wme_setup wme;
2219 1.6.4.2 yamt int ac;
2220 1.6.4.2 yamt
2221 1.6.4.2 yamt /* don't override default WME values if WME is not actually enabled */
2222 1.6.4.2 yamt if (!(ic->ic_flags & IEEE80211_F_WME))
2223 1.6.4.2 yamt return 0;
2224 1.6.4.2 yamt
2225 1.6.4.2 yamt wme.flags = 0;
2226 1.6.4.2 yamt for (ac = 0; ac < WME_NUM_AC; ac++) {
2227 1.6.4.2 yamt wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
2228 1.6.4.2 yamt wme.ac[ac].aifsn = wmep->wmep_aifsn;
2229 1.6.4.2 yamt wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
2230 1.6.4.2 yamt wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
2231 1.6.4.2 yamt wme.ac[ac].txop = WPI_USEC(wmep->wmep_txopLimit);
2232 1.6.4.2 yamt
2233 1.6.4.2 yamt DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
2234 1.6.4.2 yamt "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
2235 1.6.4.2 yamt wme.ac[ac].cwmax, wme.ac[ac].txop));
2236 1.6.4.2 yamt }
2237 1.6.4.2 yamt
2238 1.6.4.2 yamt return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
2239 1.6.4.2 yamt #undef WPI_USEC
2240 1.6.4.2 yamt #undef WPI_EXP2
2241 1.6.4.2 yamt }
2242 1.6.4.2 yamt
2243 1.6.4.2 yamt /*
2244 1.6.4.2 yamt * Configure h/w multi-rate retries.
2245 1.6.4.2 yamt */
2246 1.6.4.2 yamt static int
2247 1.6.4.2 yamt wpi_mrr_setup(struct wpi_softc *sc)
2248 1.6.4.2 yamt {
2249 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
2250 1.6.4.2 yamt struct wpi_mrr_setup mrr;
2251 1.6.4.2 yamt int i, error;
2252 1.6.4.2 yamt
2253 1.6.4.2 yamt /* CCK rates (not used with 802.11a) */
2254 1.6.4.2 yamt for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
2255 1.6.4.2 yamt mrr.rates[i].flags = 0;
2256 1.6.4.2 yamt mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
2257 1.6.4.2 yamt /* fallback to the immediate lower CCK rate (if any) */
2258 1.6.4.2 yamt mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
2259 1.6.4.2 yamt /* try one time at this rate before falling back to "next" */
2260 1.6.4.2 yamt mrr.rates[i].ntries = 1;
2261 1.6.4.2 yamt }
2262 1.6.4.2 yamt
2263 1.6.4.2 yamt /* OFDM rates (not used with 802.11b) */
2264 1.6.4.2 yamt for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
2265 1.6.4.2 yamt mrr.rates[i].flags = 0;
2266 1.6.4.2 yamt mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
2267 1.6.4.2 yamt /* fallback to the immediate lower rate (if any) */
2268 1.6.4.2 yamt /* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
2269 1.6.4.2 yamt mrr.rates[i].next = (i == WPI_OFDM6) ?
2270 1.6.4.2 yamt ((ic->ic_curmode == IEEE80211_MODE_11A) ?
2271 1.6.4.2 yamt WPI_OFDM6 : WPI_CCK2) :
2272 1.6.4.2 yamt i - 1;
2273 1.6.4.2 yamt /* try one time at this rate before falling back to "next" */
2274 1.6.4.2 yamt mrr.rates[i].ntries = 1;
2275 1.6.4.2 yamt }
2276 1.6.4.2 yamt
2277 1.6.4.2 yamt /* setup MRR for control frames */
2278 1.6.4.2 yamt mrr.which = htole32(WPI_MRR_CTL);
2279 1.6.4.4 yamt error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2280 1.6.4.2 yamt if (error != 0) {
2281 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not setup MRR for control frames\n");
2282 1.6.4.2 yamt return error;
2283 1.6.4.2 yamt }
2284 1.6.4.2 yamt
2285 1.6.4.2 yamt /* setup MRR for data frames */
2286 1.6.4.2 yamt mrr.which = htole32(WPI_MRR_DATA);
2287 1.6.4.4 yamt error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2288 1.6.4.2 yamt if (error != 0) {
2289 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not setup MRR for data frames\n");
2290 1.6.4.2 yamt return error;
2291 1.6.4.2 yamt }
2292 1.6.4.2 yamt
2293 1.6.4.2 yamt return 0;
2294 1.6.4.2 yamt }
2295 1.6.4.2 yamt
2296 1.6.4.2 yamt static void
2297 1.6.4.2 yamt wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
2298 1.6.4.2 yamt {
2299 1.6.4.2 yamt struct wpi_cmd_led led;
2300 1.6.4.2 yamt
2301 1.6.4.2 yamt led.which = which;
2302 1.6.4.2 yamt led.unit = htole32(100000); /* on/off in unit of 100ms */
2303 1.6.4.2 yamt led.off = off;
2304 1.6.4.2 yamt led.on = on;
2305 1.6.4.2 yamt
2306 1.6.4.2 yamt (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
2307 1.6.4.2 yamt }
2308 1.6.4.2 yamt
2309 1.6.4.2 yamt static void
2310 1.6.4.2 yamt wpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
2311 1.6.4.2 yamt {
2312 1.6.4.2 yamt struct wpi_cmd_tsf tsf;
2313 1.6.4.2 yamt uint64_t val, mod;
2314 1.6.4.2 yamt
2315 1.6.4.2 yamt memset(&tsf, 0, sizeof tsf);
2316 1.6.4.2 yamt memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
2317 1.6.4.2 yamt tsf.bintval = htole16(ni->ni_intval);
2318 1.6.4.2 yamt tsf.lintval = htole16(10);
2319 1.6.4.2 yamt
2320 1.6.4.2 yamt /* compute remaining time until next beacon */
2321 1.6.4.2 yamt val = (uint64_t)ni->ni_intval * 1024; /* msecs -> usecs */
2322 1.6.4.2 yamt mod = le64toh(tsf.tstamp) % val;
2323 1.6.4.2 yamt tsf.binitval = htole32((uint32_t)(val - mod));
2324 1.6.4.2 yamt
2325 1.6.4.4 yamt DPRINTF(("TSF bintval=%u tstamp=%" PRId64 ", init=%u\n",
2326 1.6.4.2 yamt ni->ni_intval, le64toh(tsf.tstamp), (uint32_t)(val - mod)));
2327 1.6.4.2 yamt
2328 1.6.4.2 yamt if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
2329 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not enable TSF\n");
2330 1.6.4.2 yamt }
2331 1.6.4.2 yamt
2332 1.6.4.2 yamt /*
2333 1.6.4.4 yamt * Update Tx power to match what is defined for channel `c'.
2334 1.6.4.4 yamt */
2335 1.6.4.4 yamt static int
2336 1.6.4.4 yamt wpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async)
2337 1.6.4.4 yamt {
2338 1.6.4.4 yamt struct ieee80211com *ic = &sc->sc_ic;
2339 1.6.4.4 yamt struct wpi_power_group *group;
2340 1.6.4.4 yamt struct wpi_cmd_txpower txpower;
2341 1.6.4.4 yamt u_int chan;
2342 1.6.4.4 yamt int i;
2343 1.6.4.4 yamt
2344 1.6.4.4 yamt /* get channel number */
2345 1.6.4.4 yamt chan = ieee80211_chan2ieee(ic, c);
2346 1.6.4.4 yamt
2347 1.6.4.4 yamt /* find the power group to which this channel belongs */
2348 1.6.4.4 yamt if (IEEE80211_IS_CHAN_5GHZ(c)) {
2349 1.6.4.4 yamt for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
2350 1.6.4.4 yamt if (chan <= group->chan)
2351 1.6.4.4 yamt break;
2352 1.6.4.4 yamt } else
2353 1.6.4.4 yamt group = &sc->groups[0];
2354 1.6.4.4 yamt
2355 1.6.4.4 yamt memset(&txpower, 0, sizeof txpower);
2356 1.6.4.4 yamt txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1;
2357 1.6.4.4 yamt txpower.chan = htole16(chan);
2358 1.6.4.4 yamt
2359 1.6.4.4 yamt /* set Tx power for all OFDM and CCK rates */
2360 1.6.4.4 yamt for (i = 0; i <= 11 ; i++) {
2361 1.6.4.4 yamt /* retrieve Tx power for this channel/rate combination */
2362 1.6.4.4 yamt int idx = wpi_get_power_index(sc, group, c,
2363 1.6.4.4 yamt wpi_ridx_to_rate[i]);
2364 1.6.4.4 yamt
2365 1.6.4.4 yamt txpower.rates[i].plcp = wpi_ridx_to_plcp[i];
2366 1.6.4.4 yamt
2367 1.6.4.4 yamt if (IEEE80211_IS_CHAN_5GHZ(c)) {
2368 1.6.4.4 yamt txpower.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
2369 1.6.4.4 yamt txpower.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
2370 1.6.4.4 yamt } else {
2371 1.6.4.4 yamt txpower.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
2372 1.6.4.4 yamt txpower.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
2373 1.6.4.4 yamt }
2374 1.6.4.4 yamt DPRINTF(("chan %d/rate %d: power index %d\n", chan,
2375 1.6.4.4 yamt wpi_ridx_to_rate[i], idx));
2376 1.6.4.4 yamt }
2377 1.6.4.4 yamt
2378 1.6.4.4 yamt return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async);
2379 1.6.4.4 yamt }
2380 1.6.4.4 yamt
2381 1.6.4.4 yamt /*
2382 1.6.4.4 yamt * Determine Tx power index for a given channel/rate combination.
2383 1.6.4.4 yamt * This takes into account the regulatory information from EEPROM and the
2384 1.6.4.4 yamt * current temperature.
2385 1.6.4.4 yamt */
2386 1.6.4.4 yamt static int
2387 1.6.4.4 yamt wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
2388 1.6.4.4 yamt struct ieee80211_channel *c, int rate)
2389 1.6.4.4 yamt {
2390 1.6.4.4 yamt /* fixed-point arithmetic division using a n-bit fractional part */
2391 1.6.4.4 yamt #define fdivround(a, b, n) \
2392 1.6.4.4 yamt ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
2393 1.6.4.4 yamt
2394 1.6.4.4 yamt /* linear interpolation */
2395 1.6.4.4 yamt #define interpolate(x, x1, y1, x2, y2, n) \
2396 1.6.4.4 yamt ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
2397 1.6.4.4 yamt
2398 1.6.4.4 yamt struct ieee80211com *ic = &sc->sc_ic;
2399 1.6.4.4 yamt struct wpi_power_sample *sample;
2400 1.6.4.4 yamt int pwr, idx;
2401 1.6.4.4 yamt u_int chan;
2402 1.6.4.4 yamt
2403 1.6.4.4 yamt /* get channel number */
2404 1.6.4.4 yamt chan = ieee80211_chan2ieee(ic, c);
2405 1.6.4.4 yamt
2406 1.6.4.4 yamt /* default power is group's maximum power - 3dB */
2407 1.6.4.4 yamt pwr = group->maxpwr / 2;
2408 1.6.4.4 yamt
2409 1.6.4.4 yamt /* decrease power for highest OFDM rates to reduce distortion */
2410 1.6.4.4 yamt switch (rate) {
2411 1.6.4.4 yamt case 72: /* 36Mb/s */
2412 1.6.4.4 yamt pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 : 5;
2413 1.6.4.4 yamt break;
2414 1.6.4.4 yamt case 96: /* 48Mb/s */
2415 1.6.4.4 yamt pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10;
2416 1.6.4.4 yamt break;
2417 1.6.4.4 yamt case 108: /* 54Mb/s */
2418 1.6.4.4 yamt pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12;
2419 1.6.4.4 yamt break;
2420 1.6.4.4 yamt }
2421 1.6.4.4 yamt
2422 1.6.4.4 yamt /* never exceed channel's maximum allowed Tx power */
2423 1.6.4.4 yamt pwr = min(pwr, sc->maxpwr[chan]);
2424 1.6.4.4 yamt
2425 1.6.4.4 yamt /* retrieve power index into gain tables from samples */
2426 1.6.4.4 yamt for (sample = group->samples; sample < &group->samples[3]; sample++)
2427 1.6.4.4 yamt if (pwr > sample[1].power)
2428 1.6.4.4 yamt break;
2429 1.6.4.4 yamt /* fixed-point linear interpolation using a 19-bit fractional part */
2430 1.6.4.4 yamt idx = interpolate(pwr, sample[0].power, sample[0].index,
2431 1.6.4.4 yamt sample[1].power, sample[1].index, 19);
2432 1.6.4.4 yamt
2433 1.6.4.4 yamt /*
2434 1.6.4.4 yamt * Adjust power index based on current temperature:
2435 1.6.4.4 yamt * - if cooler than factory-calibrated: decrease output power
2436 1.6.4.4 yamt * - if warmer than factory-calibrated: increase output power
2437 1.6.4.4 yamt */
2438 1.6.4.4 yamt idx -= (sc->temp - group->temp) * 11 / 100;
2439 1.6.4.4 yamt
2440 1.6.4.4 yamt /* decrease power for CCK rates (-5dB) */
2441 1.6.4.4 yamt if (!WPI_RATE_IS_OFDM(rate))
2442 1.6.4.4 yamt idx += 10;
2443 1.6.4.4 yamt
2444 1.6.4.4 yamt /* keep power index in a valid range */
2445 1.6.4.4 yamt if (idx < 0)
2446 1.6.4.4 yamt return 0;
2447 1.6.4.4 yamt if (idx > WPI_MAX_PWR_INDEX)
2448 1.6.4.4 yamt return WPI_MAX_PWR_INDEX;
2449 1.6.4.4 yamt return idx;
2450 1.6.4.4 yamt
2451 1.6.4.4 yamt #undef interpolate
2452 1.6.4.4 yamt #undef fdivround
2453 1.6.4.4 yamt }
2454 1.6.4.4 yamt
2455 1.6.4.4 yamt /*
2456 1.6.4.2 yamt * Build a beacon frame that the firmware will broadcast periodically in
2457 1.6.4.2 yamt * IBSS or HostAP modes.
2458 1.6.4.2 yamt */
2459 1.6.4.2 yamt static int
2460 1.6.4.2 yamt wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
2461 1.6.4.2 yamt {
2462 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
2463 1.6.4.2 yamt struct wpi_tx_ring *ring = &sc->cmdq;
2464 1.6.4.2 yamt struct wpi_tx_desc *desc;
2465 1.6.4.2 yamt struct wpi_tx_data *data;
2466 1.6.4.2 yamt struct wpi_tx_cmd *cmd;
2467 1.6.4.2 yamt struct wpi_cmd_beacon *bcn;
2468 1.6.4.2 yamt struct ieee80211_beacon_offsets bo;
2469 1.6.4.2 yamt struct mbuf *m0;
2470 1.6.4.2 yamt int error;
2471 1.6.4.2 yamt
2472 1.6.4.2 yamt desc = &ring->desc[ring->cur];
2473 1.6.4.2 yamt data = &ring->data[ring->cur];
2474 1.6.4.2 yamt
2475 1.6.4.2 yamt m0 = ieee80211_beacon_alloc(ic, ni, &bo);
2476 1.6.4.2 yamt if (m0 == NULL) {
2477 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not allocate beacon frame\n");
2478 1.6.4.2 yamt return ENOMEM;
2479 1.6.4.2 yamt }
2480 1.6.4.2 yamt
2481 1.6.4.2 yamt cmd = &ring->cmd[ring->cur];
2482 1.6.4.2 yamt cmd->code = WPI_CMD_SET_BEACON;
2483 1.6.4.2 yamt cmd->flags = 0;
2484 1.6.4.2 yamt cmd->qid = ring->qid;
2485 1.6.4.2 yamt cmd->idx = ring->cur;
2486 1.6.4.2 yamt
2487 1.6.4.2 yamt bcn = (struct wpi_cmd_beacon *)cmd->data;
2488 1.6.4.2 yamt memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
2489 1.6.4.2 yamt bcn->id = WPI_ID_BROADCAST;
2490 1.6.4.2 yamt bcn->ofdm_mask = 0xff;
2491 1.6.4.2 yamt bcn->cck_mask = 0x0f;
2492 1.6.4.4 yamt bcn->lifetime = htole32(WPI_LIFETIME_INFINITE);
2493 1.6.4.2 yamt bcn->len = htole16(m0->m_pkthdr.len);
2494 1.6.4.2 yamt bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2495 1.6.4.2 yamt wpi_plcp_signal(12) : wpi_plcp_signal(2);
2496 1.6.4.2 yamt bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
2497 1.6.4.2 yamt
2498 1.6.4.2 yamt /* save and trim IEEE802.11 header */
2499 1.6.4.4 yamt m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh);
2500 1.6.4.2 yamt m_adj(m0, sizeof (struct ieee80211_frame));
2501 1.6.4.2 yamt
2502 1.6.4.2 yamt /* assume beacon frame is contiguous */
2503 1.6.4.2 yamt error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
2504 1.6.4.2 yamt BUS_DMA_READ | BUS_DMA_NOWAIT);
2505 1.6.4.2 yamt if (error) {
2506 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not map beacon\n");
2507 1.6.4.2 yamt m_freem(m0);
2508 1.6.4.2 yamt return error;
2509 1.6.4.2 yamt }
2510 1.6.4.2 yamt
2511 1.6.4.2 yamt data->m = m0;
2512 1.6.4.2 yamt
2513 1.6.4.2 yamt /* first scatter/gather segment is used by the beacon command */
2514 1.6.4.2 yamt desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
2515 1.6.4.2 yamt desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2516 1.6.4.2 yamt ring->cur * sizeof (struct wpi_tx_cmd));
2517 1.6.4.2 yamt desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_beacon));
2518 1.6.4.2 yamt desc->segs[1].addr = htole32(data->map->dm_segs[0].ds_addr);
2519 1.6.4.2 yamt desc->segs[1].len = htole32(data->map->dm_segs[0].ds_len);
2520 1.6.4.2 yamt
2521 1.6.4.2 yamt /* kick cmd ring */
2522 1.6.4.2 yamt ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2523 1.6.4.2 yamt WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2524 1.6.4.2 yamt
2525 1.6.4.2 yamt return 0;
2526 1.6.4.2 yamt }
2527 1.6.4.2 yamt
2528 1.6.4.2 yamt static int
2529 1.6.4.2 yamt wpi_auth(struct wpi_softc *sc)
2530 1.6.4.2 yamt {
2531 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
2532 1.6.4.2 yamt struct ieee80211_node *ni = ic->ic_bss;
2533 1.6.4.2 yamt struct wpi_node_info node;
2534 1.6.4.2 yamt int error;
2535 1.6.4.2 yamt
2536 1.6.4.2 yamt /* update adapter's configuration */
2537 1.6.4.2 yamt IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
2538 1.6.4.2 yamt sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2539 1.6.4.2 yamt sc->config.flags = htole32(WPI_CONFIG_TSF);
2540 1.6.4.2 yamt if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2541 1.6.4.2 yamt sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2542 1.6.4.2 yamt WPI_CONFIG_24GHZ);
2543 1.6.4.2 yamt }
2544 1.6.4.2 yamt switch (ic->ic_curmode) {
2545 1.6.4.2 yamt case IEEE80211_MODE_11A:
2546 1.6.4.2 yamt sc->config.cck_mask = 0;
2547 1.6.4.2 yamt sc->config.ofdm_mask = 0x15;
2548 1.6.4.2 yamt break;
2549 1.6.4.2 yamt case IEEE80211_MODE_11B:
2550 1.6.4.2 yamt sc->config.cck_mask = 0x03;
2551 1.6.4.2 yamt sc->config.ofdm_mask = 0;
2552 1.6.4.2 yamt break;
2553 1.6.4.2 yamt default: /* assume 802.11b/g */
2554 1.6.4.2 yamt sc->config.cck_mask = 0x0f;
2555 1.6.4.2 yamt sc->config.ofdm_mask = 0x15;
2556 1.6.4.2 yamt }
2557 1.6.4.2 yamt
2558 1.6.4.2 yamt DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
2559 1.6.4.2 yamt sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
2560 1.6.4.2 yamt error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2561 1.6.4.2 yamt sizeof (struct wpi_config), 1);
2562 1.6.4.2 yamt if (error != 0) {
2563 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not configure\n");
2564 1.6.4.2 yamt return error;
2565 1.6.4.2 yamt }
2566 1.6.4.2 yamt
2567 1.6.4.4 yamt /* configuration has changed, set Tx power accordingly */
2568 1.6.4.4 yamt if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
2569 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
2570 1.6.4.4 yamt return error;
2571 1.6.4.4 yamt }
2572 1.6.4.4 yamt
2573 1.6.4.2 yamt /* add default node */
2574 1.6.4.2 yamt memset(&node, 0, sizeof node);
2575 1.6.4.2 yamt IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
2576 1.6.4.2 yamt node.id = WPI_ID_BSS;
2577 1.6.4.2 yamt node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2578 1.6.4.2 yamt wpi_plcp_signal(12) : wpi_plcp_signal(2);
2579 1.6.4.4 yamt node.action = htole32(WPI_ACTION_SET_RATE);
2580 1.6.4.4 yamt node.antenna = WPI_ANTENNA_BOTH;
2581 1.6.4.2 yamt error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2582 1.6.4.2 yamt if (error != 0) {
2583 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not add BSS node\n");
2584 1.6.4.2 yamt return error;
2585 1.6.4.2 yamt }
2586 1.6.4.2 yamt
2587 1.6.4.2 yamt return 0;
2588 1.6.4.2 yamt }
2589 1.6.4.2 yamt
2590 1.6.4.2 yamt /*
2591 1.6.4.2 yamt * Send a scan request to the firmware. Since this command is huge, we map it
2592 1.6.4.2 yamt * into a mbuf instead of using the pre-allocated set of commands.
2593 1.6.4.2 yamt */
2594 1.6.4.2 yamt static int
2595 1.6.4.2 yamt wpi_scan(struct wpi_softc *sc, uint16_t flags)
2596 1.6.4.2 yamt {
2597 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
2598 1.6.4.2 yamt struct wpi_tx_ring *ring = &sc->cmdq;
2599 1.6.4.2 yamt struct wpi_tx_desc *desc;
2600 1.6.4.2 yamt struct wpi_tx_data *data;
2601 1.6.4.2 yamt struct wpi_tx_cmd *cmd;
2602 1.6.4.2 yamt struct wpi_scan_hdr *hdr;
2603 1.6.4.2 yamt struct wpi_scan_chan *chan;
2604 1.6.4.2 yamt struct ieee80211_frame *wh;
2605 1.6.4.2 yamt struct ieee80211_rateset *rs;
2606 1.6.4.2 yamt struct ieee80211_channel *c;
2607 1.6.4.2 yamt enum ieee80211_phymode mode;
2608 1.6.4.2 yamt uint8_t *frm;
2609 1.6.4.2 yamt int nrates, pktlen, error;
2610 1.6.4.2 yamt
2611 1.6.4.2 yamt desc = &ring->desc[ring->cur];
2612 1.6.4.2 yamt data = &ring->data[ring->cur];
2613 1.6.4.2 yamt
2614 1.6.4.2 yamt MGETHDR(data->m, M_DONTWAIT, MT_DATA);
2615 1.6.4.2 yamt if (data->m == NULL) {
2616 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
2617 1.6.4.7 yamt "could not allocate mbuf for scan command\n");
2618 1.6.4.2 yamt return ENOMEM;
2619 1.6.4.2 yamt }
2620 1.6.4.2 yamt
2621 1.6.4.2 yamt MCLGET(data->m, M_DONTWAIT);
2622 1.6.4.2 yamt if (!(data->m->m_flags & M_EXT)) {
2623 1.6.4.2 yamt m_freem(data->m);
2624 1.6.4.2 yamt data->m = NULL;
2625 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
2626 1.6.4.7 yamt "could not allocate mbuf for scan command\n");
2627 1.6.4.2 yamt return ENOMEM;
2628 1.6.4.2 yamt }
2629 1.6.4.2 yamt
2630 1.6.4.2 yamt cmd = mtod(data->m, struct wpi_tx_cmd *);
2631 1.6.4.2 yamt cmd->code = WPI_CMD_SCAN;
2632 1.6.4.2 yamt cmd->flags = 0;
2633 1.6.4.2 yamt cmd->qid = ring->qid;
2634 1.6.4.2 yamt cmd->idx = ring->cur;
2635 1.6.4.2 yamt
2636 1.6.4.2 yamt hdr = (struct wpi_scan_hdr *)cmd->data;
2637 1.6.4.2 yamt memset(hdr, 0, sizeof (struct wpi_scan_hdr));
2638 1.6.4.4 yamt hdr->txflags = htole32(WPI_TX_AUTO_SEQ);
2639 1.6.4.4 yamt hdr->id = WPI_ID_BROADCAST;
2640 1.6.4.4 yamt hdr->lifetime = htole32(WPI_LIFETIME_INFINITE);
2641 1.6.4.4 yamt
2642 1.6.4.2 yamt /*
2643 1.6.4.2 yamt * Move to the next channel if no packets are received within 5 msecs
2644 1.6.4.2 yamt * after sending the probe request (this helps to reduce the duration
2645 1.6.4.2 yamt * of active scans).
2646 1.6.4.2 yamt */
2647 1.6.4.2 yamt hdr->quiet = htole16(5); /* timeout in milliseconds */
2648 1.6.4.4 yamt hdr->plcp_threshold = htole16(1); /* min # of packets */
2649 1.6.4.2 yamt
2650 1.6.4.2 yamt if (flags & IEEE80211_CHAN_A) {
2651 1.6.4.4 yamt hdr->crc_threshold = htole16(1);
2652 1.6.4.2 yamt /* send probe requests at 6Mbps */
2653 1.6.4.2 yamt hdr->rate = wpi_plcp_signal(12);
2654 1.6.4.2 yamt } else {
2655 1.6.4.2 yamt hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
2656 1.6.4.2 yamt /* send probe requests at 1Mbps */
2657 1.6.4.2 yamt hdr->rate = wpi_plcp_signal(2);
2658 1.6.4.2 yamt }
2659 1.6.4.2 yamt
2660 1.6.4.4 yamt /* for directed scans, firmware inserts the essid IE itself */
2661 1.6.4.4 yamt hdr->essid[0].id = IEEE80211_ELEMID_SSID;
2662 1.6.4.4 yamt hdr->essid[0].len = ic->ic_des_esslen;
2663 1.6.4.4 yamt memcpy(hdr->essid[0].data, ic->ic_des_essid, ic->ic_des_esslen);
2664 1.6.4.2 yamt
2665 1.6.4.2 yamt /*
2666 1.6.4.2 yamt * Build a probe request frame. Most of the following code is a
2667 1.6.4.2 yamt * copy & paste of what is done in net80211.
2668 1.6.4.2 yamt */
2669 1.6.4.2 yamt wh = (struct ieee80211_frame *)(hdr + 1);
2670 1.6.4.2 yamt wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2671 1.6.4.2 yamt IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2672 1.6.4.2 yamt wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2673 1.6.4.2 yamt IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
2674 1.6.4.2 yamt IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
2675 1.6.4.2 yamt IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
2676 1.6.4.2 yamt *(u_int16_t *)&wh->i_dur[0] = 0; /* filled by h/w */
2677 1.6.4.2 yamt *(u_int16_t *)&wh->i_seq[0] = 0; /* filled by h/w */
2678 1.6.4.2 yamt
2679 1.6.4.2 yamt frm = (uint8_t *)(wh + 1);
2680 1.6.4.2 yamt
2681 1.6.4.4 yamt /* add empty essid IE (firmware generates it for directed scans) */
2682 1.6.4.2 yamt *frm++ = IEEE80211_ELEMID_SSID;
2683 1.6.4.4 yamt *frm++ = 0;
2684 1.6.4.2 yamt
2685 1.6.4.2 yamt mode = ieee80211_chan2mode(ic, ic->ic_ibss_chan);
2686 1.6.4.2 yamt rs = &ic->ic_sup_rates[mode];
2687 1.6.4.2 yamt
2688 1.6.4.2 yamt /* add supported rates IE */
2689 1.6.4.2 yamt *frm++ = IEEE80211_ELEMID_RATES;
2690 1.6.4.2 yamt nrates = rs->rs_nrates;
2691 1.6.4.2 yamt if (nrates > IEEE80211_RATE_SIZE)
2692 1.6.4.2 yamt nrates = IEEE80211_RATE_SIZE;
2693 1.6.4.2 yamt *frm++ = nrates;
2694 1.6.4.2 yamt memcpy(frm, rs->rs_rates, nrates);
2695 1.6.4.2 yamt frm += nrates;
2696 1.6.4.2 yamt
2697 1.6.4.2 yamt /* add supported xrates IE */
2698 1.6.4.2 yamt if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2699 1.6.4.2 yamt nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2700 1.6.4.2 yamt *frm++ = IEEE80211_ELEMID_XRATES;
2701 1.6.4.2 yamt *frm++ = nrates;
2702 1.6.4.2 yamt memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2703 1.6.4.2 yamt frm += nrates;
2704 1.6.4.2 yamt }
2705 1.6.4.2 yamt
2706 1.6.4.2 yamt /* setup length of probe request */
2707 1.6.4.4 yamt hdr->paylen = htole16(frm - (uint8_t *)wh);
2708 1.6.4.2 yamt
2709 1.6.4.2 yamt chan = (struct wpi_scan_chan *)frm;
2710 1.6.4.2 yamt for (c = &ic->ic_channels[1];
2711 1.6.4.2 yamt c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
2712 1.6.4.2 yamt if ((c->ic_flags & flags) != flags)
2713 1.6.4.2 yamt continue;
2714 1.6.4.2 yamt
2715 1.6.4.2 yamt chan->chan = ieee80211_chan2ieee(ic, c);
2716 1.6.4.4 yamt chan->flags = 0;
2717 1.6.4.4 yamt if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2718 1.6.4.4 yamt chan->flags |= WPI_CHAN_ACTIVE;
2719 1.6.4.4 yamt if (ic->ic_des_esslen != 0)
2720 1.6.4.4 yamt chan->flags |= WPI_CHAN_DIRECT;
2721 1.6.4.4 yamt }
2722 1.6.4.4 yamt chan->dsp_gain = 0x6e;
2723 1.6.4.2 yamt if (IEEE80211_IS_CHAN_5GHZ(c)) {
2724 1.6.4.4 yamt chan->rf_gain = 0x3b;
2725 1.6.4.2 yamt chan->active = htole16(10);
2726 1.6.4.2 yamt chan->passive = htole16(110);
2727 1.6.4.2 yamt } else {
2728 1.6.4.4 yamt chan->rf_gain = 0x28;
2729 1.6.4.2 yamt chan->active = htole16(20);
2730 1.6.4.2 yamt chan->passive = htole16(120);
2731 1.6.4.2 yamt }
2732 1.6.4.2 yamt hdr->nchan++;
2733 1.6.4.2 yamt chan++;
2734 1.6.4.2 yamt
2735 1.6.4.2 yamt frm += sizeof (struct wpi_scan_chan);
2736 1.6.4.2 yamt }
2737 1.6.4.4 yamt hdr->len = htole16(frm - (uint8_t *)hdr);
2738 1.6.4.4 yamt pktlen = frm - (uint8_t *)cmd;
2739 1.6.4.2 yamt
2740 1.6.4.2 yamt error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, pktlen,
2741 1.6.4.2 yamt NULL, BUS_DMA_NOWAIT);
2742 1.6.4.2 yamt if (error) {
2743 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not map scan command\n");
2744 1.6.4.2 yamt m_freem(data->m);
2745 1.6.4.2 yamt data->m = NULL;
2746 1.6.4.2 yamt return error;
2747 1.6.4.2 yamt }
2748 1.6.4.2 yamt
2749 1.6.4.2 yamt desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
2750 1.6.4.2 yamt desc->segs[0].addr = htole32(data->map->dm_segs[0].ds_addr);
2751 1.6.4.2 yamt desc->segs[0].len = htole32(data->map->dm_segs[0].ds_len);
2752 1.6.4.2 yamt
2753 1.6.4.2 yamt /* kick cmd ring */
2754 1.6.4.2 yamt ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2755 1.6.4.2 yamt WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2756 1.6.4.2 yamt
2757 1.6.4.2 yamt return 0; /* will be notified async. of failure/success */
2758 1.6.4.2 yamt }
2759 1.6.4.2 yamt
2760 1.6.4.2 yamt static int
2761 1.6.4.2 yamt wpi_config(struct wpi_softc *sc)
2762 1.6.4.2 yamt {
2763 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
2764 1.6.4.2 yamt struct ifnet *ifp = ic->ic_ifp;
2765 1.6.4.2 yamt struct wpi_power power;
2766 1.6.4.2 yamt struct wpi_bluetooth bluetooth;
2767 1.6.4.2 yamt struct wpi_node_info node;
2768 1.6.4.2 yamt int error;
2769 1.6.4.2 yamt
2770 1.6.4.2 yamt memset(&power, 0, sizeof power);
2771 1.6.4.4 yamt power.flags = htole32(WPI_POWER_CAM | 0x8);
2772 1.6.4.2 yamt error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
2773 1.6.4.2 yamt if (error != 0) {
2774 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not set power mode\n");
2775 1.6.4.2 yamt return error;
2776 1.6.4.2 yamt }
2777 1.6.4.2 yamt
2778 1.6.4.2 yamt /* configure bluetooth coexistence */
2779 1.6.4.2 yamt memset(&bluetooth, 0, sizeof bluetooth);
2780 1.6.4.2 yamt bluetooth.flags = 3;
2781 1.6.4.2 yamt bluetooth.lead = 0xaa;
2782 1.6.4.2 yamt bluetooth.kill = 1;
2783 1.6.4.2 yamt error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
2784 1.6.4.2 yamt 0);
2785 1.6.4.2 yamt if (error != 0) {
2786 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
2787 1.6.4.7 yamt "could not configure bluetooth coexistence\n");
2788 1.6.4.2 yamt return error;
2789 1.6.4.2 yamt }
2790 1.6.4.2 yamt
2791 1.6.4.2 yamt /* configure adapter */
2792 1.6.4.2 yamt memset(&sc->config, 0, sizeof (struct wpi_config));
2793 1.6.4.4 yamt IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2794 1.6.4.2 yamt IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr);
2795 1.6.4.2 yamt /*set default channel*/
2796 1.6.4.2 yamt sc->config.chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
2797 1.6.4.2 yamt sc->config.flags = htole32(WPI_CONFIG_TSF);
2798 1.6.4.2 yamt if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan)) {
2799 1.6.4.2 yamt sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2800 1.6.4.2 yamt WPI_CONFIG_24GHZ);
2801 1.6.4.2 yamt }
2802 1.6.4.2 yamt sc->config.filter = 0;
2803 1.6.4.2 yamt switch (ic->ic_opmode) {
2804 1.6.4.2 yamt case IEEE80211_M_STA:
2805 1.6.4.2 yamt sc->config.mode = WPI_MODE_STA;
2806 1.6.4.2 yamt sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
2807 1.6.4.2 yamt break;
2808 1.6.4.2 yamt case IEEE80211_M_IBSS:
2809 1.6.4.2 yamt case IEEE80211_M_AHDEMO:
2810 1.6.4.2 yamt sc->config.mode = WPI_MODE_IBSS;
2811 1.6.4.2 yamt break;
2812 1.6.4.2 yamt case IEEE80211_M_HOSTAP:
2813 1.6.4.2 yamt sc->config.mode = WPI_MODE_HOSTAP;
2814 1.6.4.2 yamt break;
2815 1.6.4.2 yamt case IEEE80211_M_MONITOR:
2816 1.6.4.2 yamt sc->config.mode = WPI_MODE_MONITOR;
2817 1.6.4.2 yamt sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
2818 1.6.4.2 yamt WPI_FILTER_CTL | WPI_FILTER_PROMISC);
2819 1.6.4.2 yamt break;
2820 1.6.4.2 yamt }
2821 1.6.4.2 yamt sc->config.cck_mask = 0x0f; /* not yet negotiated */
2822 1.6.4.2 yamt sc->config.ofdm_mask = 0xff; /* not yet negotiated */
2823 1.6.4.2 yamt error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2824 1.6.4.2 yamt sizeof (struct wpi_config), 0);
2825 1.6.4.2 yamt if (error != 0) {
2826 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "configure command failed\n");
2827 1.6.4.2 yamt return error;
2828 1.6.4.2 yamt }
2829 1.6.4.2 yamt
2830 1.6.4.4 yamt /* configuration has changed, set Tx power accordingly */
2831 1.6.4.4 yamt if ((error = wpi_set_txpower(sc, ic->ic_ibss_chan, 0)) != 0) {
2832 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not set Tx power\n");
2833 1.6.4.4 yamt return error;
2834 1.6.4.4 yamt }
2835 1.6.4.4 yamt
2836 1.6.4.2 yamt /* add broadcast node */
2837 1.6.4.2 yamt memset(&node, 0, sizeof node);
2838 1.6.4.2 yamt IEEE80211_ADDR_COPY(node.bssid, etherbroadcastaddr);
2839 1.6.4.2 yamt node.id = WPI_ID_BROADCAST;
2840 1.6.4.2 yamt node.rate = wpi_plcp_signal(2);
2841 1.6.4.4 yamt node.action = htole32(WPI_ACTION_SET_RATE);
2842 1.6.4.4 yamt node.antenna = WPI_ANTENNA_BOTH;
2843 1.6.4.2 yamt error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
2844 1.6.4.2 yamt if (error != 0) {
2845 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not add broadcast node\n");
2846 1.6.4.2 yamt return error;
2847 1.6.4.2 yamt }
2848 1.6.4.2 yamt
2849 1.6.4.4 yamt if ((error = wpi_mrr_setup(sc)) != 0) {
2850 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not setup MRR\n");
2851 1.6.4.4 yamt return error;
2852 1.6.4.4 yamt }
2853 1.6.4.4 yamt
2854 1.6.4.2 yamt return 0;
2855 1.6.4.2 yamt }
2856 1.6.4.2 yamt
2857 1.6.4.2 yamt static void
2858 1.6.4.2 yamt wpi_stop_master(struct wpi_softc *sc)
2859 1.6.4.2 yamt {
2860 1.6.4.2 yamt uint32_t tmp;
2861 1.6.4.2 yamt int ntries;
2862 1.6.4.2 yamt
2863 1.6.4.2 yamt tmp = WPI_READ(sc, WPI_RESET);
2864 1.6.4.2 yamt WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER);
2865 1.6.4.2 yamt
2866 1.6.4.2 yamt tmp = WPI_READ(sc, WPI_GPIO_CTL);
2867 1.6.4.2 yamt if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
2868 1.6.4.2 yamt return; /* already asleep */
2869 1.6.4.2 yamt
2870 1.6.4.2 yamt for (ntries = 0; ntries < 100; ntries++) {
2871 1.6.4.2 yamt if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
2872 1.6.4.2 yamt break;
2873 1.6.4.2 yamt DELAY(10);
2874 1.6.4.2 yamt }
2875 1.6.4.2 yamt if (ntries == 100) {
2876 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
2877 1.6.4.2 yamt }
2878 1.6.4.2 yamt }
2879 1.6.4.2 yamt
2880 1.6.4.2 yamt static int
2881 1.6.4.2 yamt wpi_power_up(struct wpi_softc *sc)
2882 1.6.4.2 yamt {
2883 1.6.4.2 yamt uint32_t tmp;
2884 1.6.4.2 yamt int ntries;
2885 1.6.4.2 yamt
2886 1.6.4.2 yamt wpi_mem_lock(sc);
2887 1.6.4.2 yamt tmp = wpi_mem_read(sc, WPI_MEM_POWER);
2888 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
2889 1.6.4.2 yamt wpi_mem_unlock(sc);
2890 1.6.4.2 yamt
2891 1.6.4.2 yamt for (ntries = 0; ntries < 5000; ntries++) {
2892 1.6.4.2 yamt if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
2893 1.6.4.2 yamt break;
2894 1.6.4.2 yamt DELAY(10);
2895 1.6.4.2 yamt }
2896 1.6.4.2 yamt if (ntries == 5000) {
2897 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "timeout waiting for NIC to power up\n");
2898 1.6.4.2 yamt return ETIMEDOUT;
2899 1.6.4.2 yamt }
2900 1.6.4.2 yamt return 0;
2901 1.6.4.2 yamt }
2902 1.6.4.2 yamt
2903 1.6.4.2 yamt static int
2904 1.6.4.2 yamt wpi_reset(struct wpi_softc *sc)
2905 1.6.4.2 yamt {
2906 1.6.4.2 yamt uint32_t tmp;
2907 1.6.4.2 yamt int ntries;
2908 1.6.4.2 yamt
2909 1.6.4.2 yamt /* clear any pending interrupts */
2910 1.6.4.2 yamt WPI_WRITE(sc, WPI_INTR, 0xffffffff);
2911 1.6.4.2 yamt
2912 1.6.4.2 yamt tmp = WPI_READ(sc, WPI_PLL_CTL);
2913 1.6.4.2 yamt WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
2914 1.6.4.2 yamt
2915 1.6.4.2 yamt tmp = WPI_READ(sc, WPI_CHICKEN);
2916 1.6.4.2 yamt WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
2917 1.6.4.2 yamt
2918 1.6.4.2 yamt tmp = WPI_READ(sc, WPI_GPIO_CTL);
2919 1.6.4.2 yamt WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
2920 1.6.4.2 yamt
2921 1.6.4.2 yamt /* wait for clock stabilization */
2922 1.6.4.2 yamt for (ntries = 0; ntries < 1000; ntries++) {
2923 1.6.4.2 yamt if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
2924 1.6.4.2 yamt break;
2925 1.6.4.2 yamt DELAY(10);
2926 1.6.4.2 yamt }
2927 1.6.4.2 yamt if (ntries == 1000) {
2928 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
2929 1.6.4.7 yamt "timeout waiting for clock stabilization\n");
2930 1.6.4.2 yamt return ETIMEDOUT;
2931 1.6.4.2 yamt }
2932 1.6.4.2 yamt
2933 1.6.4.2 yamt /* initialize EEPROM */
2934 1.6.4.2 yamt tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
2935 1.6.4.2 yamt if ((tmp & WPI_EEPROM_VERSION) == 0) {
2936 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "EEPROM not found\n");
2937 1.6.4.2 yamt return EIO;
2938 1.6.4.2 yamt }
2939 1.6.4.2 yamt WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
2940 1.6.4.2 yamt
2941 1.6.4.2 yamt return 0;
2942 1.6.4.2 yamt }
2943 1.6.4.2 yamt
2944 1.6.4.2 yamt static void
2945 1.6.4.2 yamt wpi_hw_config(struct wpi_softc *sc)
2946 1.6.4.2 yamt {
2947 1.6.4.2 yamt uint32_t rev, hw;
2948 1.6.4.2 yamt
2949 1.6.4.4 yamt /* voodoo from the reference driver */
2950 1.6.4.2 yamt hw = WPI_READ(sc, WPI_HWCONFIG);
2951 1.6.4.2 yamt
2952 1.6.4.2 yamt rev = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG);
2953 1.6.4.2 yamt rev = PCI_REVISION(rev);
2954 1.6.4.2 yamt if ((rev & 0xc0) == 0x40)
2955 1.6.4.2 yamt hw |= WPI_HW_ALM_MB;
2956 1.6.4.2 yamt else if (!(rev & 0x80))
2957 1.6.4.2 yamt hw |= WPI_HW_ALM_MM;
2958 1.6.4.2 yamt
2959 1.6.4.4 yamt if (sc->cap == 0x80)
2960 1.6.4.2 yamt hw |= WPI_HW_SKU_MRC;
2961 1.6.4.2 yamt
2962 1.6.4.2 yamt hw &= ~WPI_HW_REV_D;
2963 1.6.4.4 yamt if ((le16toh(sc->rev) & 0xf0) == 0xd0)
2964 1.6.4.2 yamt hw |= WPI_HW_REV_D;
2965 1.6.4.2 yamt
2966 1.6.4.4 yamt if (sc->type > 1)
2967 1.6.4.2 yamt hw |= WPI_HW_TYPE_B;
2968 1.6.4.2 yamt
2969 1.6.4.2 yamt DPRINTF(("setting h/w config %x\n", hw));
2970 1.6.4.2 yamt WPI_WRITE(sc, WPI_HWCONFIG, hw);
2971 1.6.4.2 yamt }
2972 1.6.4.2 yamt
2973 1.6.4.2 yamt static int
2974 1.6.4.2 yamt wpi_init(struct ifnet *ifp)
2975 1.6.4.2 yamt {
2976 1.6.4.2 yamt struct wpi_softc *sc = ifp->if_softc;
2977 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
2978 1.6.4.2 yamt uint32_t tmp;
2979 1.6.4.2 yamt int qid, ntries, error;
2980 1.6.4.2 yamt
2981 1.6.4.4 yamt wpi_stop(ifp,1);
2982 1.6.4.2 yamt (void)wpi_reset(sc);
2983 1.6.4.2 yamt
2984 1.6.4.2 yamt wpi_mem_lock(sc);
2985 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
2986 1.6.4.2 yamt DELAY(20);
2987 1.6.4.2 yamt tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
2988 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
2989 1.6.4.2 yamt wpi_mem_unlock(sc);
2990 1.6.4.2 yamt
2991 1.6.4.2 yamt (void)wpi_power_up(sc);
2992 1.6.4.2 yamt wpi_hw_config(sc);
2993 1.6.4.2 yamt
2994 1.6.4.2 yamt /* init Rx ring */
2995 1.6.4.2 yamt wpi_mem_lock(sc);
2996 1.6.4.2 yamt WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
2997 1.6.4.2 yamt WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
2998 1.6.4.2 yamt offsetof(struct wpi_shared, next));
2999 1.6.4.2 yamt WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
3000 1.6.4.2 yamt WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
3001 1.6.4.2 yamt wpi_mem_unlock(sc);
3002 1.6.4.2 yamt
3003 1.6.4.2 yamt /* init Tx rings */
3004 1.6.4.2 yamt wpi_mem_lock(sc);
3005 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */
3006 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_RA, 1); /* enable RA0 */
3007 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
3008 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
3009 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
3010 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
3011 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
3012 1.6.4.2 yamt
3013 1.6.4.2 yamt WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
3014 1.6.4.2 yamt WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
3015 1.6.4.2 yamt
3016 1.6.4.2 yamt for (qid = 0; qid < 6; qid++) {
3017 1.6.4.2 yamt WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
3018 1.6.4.2 yamt WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
3019 1.6.4.2 yamt WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
3020 1.6.4.2 yamt }
3021 1.6.4.2 yamt wpi_mem_unlock(sc);
3022 1.6.4.2 yamt
3023 1.6.4.2 yamt /* clear "radio off" and "disable command" bits (reversed logic) */
3024 1.6.4.2 yamt WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3025 1.6.4.2 yamt WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
3026 1.6.4.2 yamt
3027 1.6.4.2 yamt /* clear any pending interrupts */
3028 1.6.4.2 yamt WPI_WRITE(sc, WPI_INTR, 0xffffffff);
3029 1.6.4.2 yamt /* enable interrupts */
3030 1.6.4.2 yamt WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
3031 1.6.4.2 yamt
3032 1.6.4.4 yamt /* not sure why/if this is necessary... */
3033 1.6.4.4 yamt WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3034 1.6.4.4 yamt WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3035 1.6.4.2 yamt
3036 1.6.4.4 yamt if ((error = wpi_load_firmware(sc)) != 0) {
3037 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not load firmware\n");
3038 1.6.4.7 yamt goto fail1;
3039 1.6.4.7 yamt }
3040 1.6.4.7 yamt
3041 1.6.4.7 yamt /* Check the status of the radio switch */
3042 1.6.4.8 yamt if (wpi_getrfkill(sc)) {
3043 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "Radio is disabled by hardware switch\n");
3044 1.6.4.8 yamt error = EBUSY;
3045 1.6.4.2 yamt goto fail1;
3046 1.6.4.2 yamt }
3047 1.6.4.2 yamt
3048 1.6.4.2 yamt /* wait for thermal sensors to calibrate */
3049 1.6.4.2 yamt for (ntries = 0; ntries < 1000; ntries++) {
3050 1.6.4.4 yamt if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
3051 1.6.4.2 yamt break;
3052 1.6.4.2 yamt DELAY(10);
3053 1.6.4.2 yamt }
3054 1.6.4.2 yamt if (ntries == 1000) {
3055 1.6.4.7 yamt aprint_error_dev(sc->sc_dev,
3056 1.6.4.7 yamt "timeout waiting for thermal sensors calibration\n");
3057 1.6.4.2 yamt error = ETIMEDOUT;
3058 1.6.4.2 yamt goto fail1;
3059 1.6.4.2 yamt }
3060 1.6.4.4 yamt
3061 1.6.4.4 yamt DPRINTF(("temperature %d\n", sc->temp));
3062 1.6.4.2 yamt
3063 1.6.4.2 yamt if ((error = wpi_config(sc)) != 0) {
3064 1.6.4.7 yamt aprint_error_dev(sc->sc_dev, "could not configure device\n");
3065 1.6.4.2 yamt goto fail1;
3066 1.6.4.2 yamt }
3067 1.6.4.2 yamt
3068 1.6.4.2 yamt ifp->if_flags &= ~IFF_OACTIVE;
3069 1.6.4.2 yamt ifp->if_flags |= IFF_RUNNING;
3070 1.6.4.2 yamt
3071 1.6.4.2 yamt if (ic->ic_opmode != IEEE80211_M_MONITOR) {
3072 1.6.4.2 yamt if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
3073 1.6.4.2 yamt ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3074 1.6.4.2 yamt }
3075 1.6.4.2 yamt else
3076 1.6.4.2 yamt ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
3077 1.6.4.2 yamt
3078 1.6.4.2 yamt return 0;
3079 1.6.4.2 yamt
3080 1.6.4.2 yamt fail1: wpi_stop(ifp, 1);
3081 1.6.4.2 yamt return error;
3082 1.6.4.2 yamt }
3083 1.6.4.2 yamt
3084 1.6.4.2 yamt
3085 1.6.4.2 yamt static void
3086 1.6.4.2 yamt wpi_stop(struct ifnet *ifp, int disable)
3087 1.6.4.2 yamt {
3088 1.6.4.2 yamt struct wpi_softc *sc = ifp->if_softc;
3089 1.6.4.2 yamt struct ieee80211com *ic = &sc->sc_ic;
3090 1.6.4.2 yamt uint32_t tmp;
3091 1.6.4.2 yamt int ac;
3092 1.6.4.2 yamt
3093 1.6.4.2 yamt ifp->if_timer = sc->sc_tx_timer = 0;
3094 1.6.4.2 yamt ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3095 1.6.4.2 yamt
3096 1.6.4.2 yamt ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
3097 1.6.4.2 yamt
3098 1.6.4.2 yamt /* disable interrupts */
3099 1.6.4.2 yamt WPI_WRITE(sc, WPI_MASK, 0);
3100 1.6.4.2 yamt WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
3101 1.6.4.2 yamt WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
3102 1.6.4.2 yamt WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
3103 1.6.4.2 yamt
3104 1.6.4.2 yamt wpi_mem_lock(sc);
3105 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_MODE, 0);
3106 1.6.4.2 yamt wpi_mem_unlock(sc);
3107 1.6.4.2 yamt
3108 1.6.4.2 yamt /* reset all Tx rings */
3109 1.6.4.2 yamt for (ac = 0; ac < 4; ac++)
3110 1.6.4.2 yamt wpi_reset_tx_ring(sc, &sc->txq[ac]);
3111 1.6.4.2 yamt wpi_reset_tx_ring(sc, &sc->cmdq);
3112 1.6.4.2 yamt
3113 1.6.4.2 yamt /* reset Rx ring */
3114 1.6.4.2 yamt wpi_reset_rx_ring(sc, &sc->rxq);
3115 1.6.4.4 yamt
3116 1.6.4.2 yamt wpi_mem_lock(sc);
3117 1.6.4.2 yamt wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
3118 1.6.4.2 yamt wpi_mem_unlock(sc);
3119 1.6.4.2 yamt
3120 1.6.4.2 yamt DELAY(5);
3121 1.6.4.2 yamt
3122 1.6.4.2 yamt wpi_stop_master(sc);
3123 1.6.4.2 yamt
3124 1.6.4.2 yamt tmp = WPI_READ(sc, WPI_RESET);
3125 1.6.4.2 yamt WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
3126 1.6.4.2 yamt }
3127 1.6.4.8 yamt
3128 1.6.4.8 yamt static bool
3129 1.6.4.8 yamt wpi_resume(device_t dv)
3130 1.6.4.8 yamt {
3131 1.6.4.8 yamt struct wpi_softc *sc = device_private(dv);
3132 1.6.4.8 yamt
3133 1.6.4.8 yamt pci_disable_retry(sc->sc_pct, sc->sc_pcitag);
3134 1.6.4.8 yamt (void)wpi_reset(sc);
3135 1.6.4.8 yamt
3136 1.6.4.8 yamt return true;
3137 1.6.4.8 yamt }
3138 1.6.4.8 yamt
3139 1.6.4.8 yamt /*
3140 1.6.4.8 yamt * Return whether or not the radio is enabled in hardware
3141 1.6.4.8 yamt * (i.e. the rfkill switch is "off").
3142 1.6.4.8 yamt */
3143 1.6.4.8 yamt static int
3144 1.6.4.8 yamt wpi_getrfkill(struct wpi_softc *sc)
3145 1.6.4.8 yamt {
3146 1.6.4.8 yamt uint32_t tmp;
3147 1.6.4.8 yamt
3148 1.6.4.8 yamt wpi_mem_lock(sc);
3149 1.6.4.8 yamt tmp = wpi_mem_read(sc, WPI_MEM_RFKILL);
3150 1.6.4.8 yamt wpi_mem_unlock(sc);
3151 1.6.4.8 yamt
3152 1.6.4.8 yamt return !(tmp & 0x01);
3153 1.6.4.8 yamt }
3154 1.6.4.8 yamt
3155 1.6.4.8 yamt static int
3156 1.6.4.8 yamt wpi_sysctl_radio(SYSCTLFN_ARGS)
3157 1.6.4.8 yamt {
3158 1.6.4.8 yamt struct sysctlnode node;
3159 1.6.4.8 yamt struct wpi_softc *sc;
3160 1.6.4.8 yamt int val, error;
3161 1.6.4.8 yamt
3162 1.6.4.8 yamt node = *rnode;
3163 1.6.4.8 yamt sc = (struct wpi_softc *)node.sysctl_data;
3164 1.6.4.8 yamt
3165 1.6.4.8 yamt val = !wpi_getrfkill(sc);
3166 1.6.4.8 yamt
3167 1.6.4.8 yamt node.sysctl_data = &val;
3168 1.6.4.8 yamt error = sysctl_lookup(SYSCTLFN_CALL(&node));
3169 1.6.4.8 yamt
3170 1.6.4.8 yamt if (error || newp == NULL)
3171 1.6.4.8 yamt return error;
3172 1.6.4.8 yamt
3173 1.6.4.8 yamt return 0;
3174 1.6.4.8 yamt }
3175 1.6.4.8 yamt
3176 1.6.4.8 yamt static void
3177 1.6.4.8 yamt wpi_sysctlattach(struct wpi_softc *sc)
3178 1.6.4.8 yamt {
3179 1.6.4.8 yamt int rc;
3180 1.6.4.8 yamt const struct sysctlnode *rnode;
3181 1.6.4.8 yamt const struct sysctlnode *cnode;
3182 1.6.4.8 yamt
3183 1.6.4.8 yamt struct sysctllog **clog = &sc->sc_sysctllog;
3184 1.6.4.8 yamt
3185 1.6.4.8 yamt if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
3186 1.6.4.8 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
3187 1.6.4.8 yamt NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
3188 1.6.4.8 yamt goto err;
3189 1.6.4.8 yamt
3190 1.6.4.8 yamt if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
3191 1.6.4.8 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev),
3192 1.6.4.8 yamt SYSCTL_DESCR("wpi controls and statistics"),
3193 1.6.4.8 yamt NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
3194 1.6.4.8 yamt goto err;
3195 1.6.4.8 yamt
3196 1.6.4.8 yamt if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
3197 1.6.4.8 yamt CTLFLAG_PERMANENT, CTLTYPE_INT, "radio",
3198 1.6.4.8 yamt SYSCTL_DESCR("radio transmitter switch state (0=off, 1=on)"),
3199 1.6.4.8 yamt wpi_sysctl_radio, 0, sc, 0, CTL_CREATE, CTL_EOL)) != 0)
3200 1.6.4.8 yamt goto err;
3201 1.6.4.8 yamt
3202 1.6.4.8 yamt #ifdef WPI_DEBUG
3203 1.6.4.8 yamt /* control debugging printfs */
3204 1.6.4.8 yamt if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
3205 1.6.4.8 yamt CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
3206 1.6.4.8 yamt "debug", SYSCTL_DESCR("Enable debugging output"),
3207 1.6.4.8 yamt NULL, 0, &wpi_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
3208 1.6.4.8 yamt goto err;
3209 1.6.4.8 yamt #endif
3210 1.6.4.8 yamt
3211 1.6.4.8 yamt return;
3212 1.6.4.8 yamt err:
3213 1.6.4.8 yamt aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
3214 1.6.4.8 yamt }
3215