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