xref: /freebsd/sys/dev/rtwn/if_rtwn.c (revision 1acd7ad907c388d72ce6bf1da9e4473ccfd16a01)
1 /*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6  * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 /*
25  * Driver for Realtek RTL8188CE-VAU/RTL8188CUS/RTL8188EU/RTL8188RU/RTL8192CU/RTL8812AU/RTL8821AU.
26  */
27 #include "opt_wlan.h"
28 
29 #include <sys/param.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/mbuf.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/endian.h>
42 #include <sys/linker.h>
43 #include <sys/firmware.h>
44 #include <sys/kdb.h>
45 
46 #include <net/bpf.h>
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_arp.h>
50 #include <net/ethernet.h>
51 #include <net/if_dl.h>
52 #include <net/if_media.h>
53 #include <net/if_types.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in_var.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/ip.h>
60 
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_regdomain.h>
63 #include <net80211/ieee80211_radiotap.h>
64 #include <net80211/ieee80211_ratectl.h>
65 
66 #include <dev/rtwn/if_rtwnreg.h>
67 #include <dev/rtwn/if_rtwnvar.h>
68 
69 #include <dev/rtwn/if_rtwn_beacon.h>
70 #include <dev/rtwn/if_rtwn_calib.h>
71 #include <dev/rtwn/if_rtwn_cam.h>
72 #include <dev/rtwn/if_rtwn_debug.h>
73 #include <dev/rtwn/if_rtwn_efuse.h>
74 #include <dev/rtwn/if_rtwn_fw.h>
75 #include <dev/rtwn/if_rtwn_ridx.h>
76 #include <dev/rtwn/if_rtwn_rx.h>
77 #include <dev/rtwn/if_rtwn_task.h>
78 #include <dev/rtwn/if_rtwn_tx.h>
79 
80 #include <dev/rtwn/rtl8192c/r92c_reg.h>
81 
82 
83 static void		rtwn_radiotap_attach(struct rtwn_softc *);
84 static void		rtwn_vap_decrement_counters(struct rtwn_softc *,
85 			    enum ieee80211_opmode, int);
86 static void		rtwn_set_ic_opmode(struct rtwn_softc *);
87 static struct ieee80211vap *rtwn_vap_create(struct ieee80211com *,
88 			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
89 			    int, const uint8_t [IEEE80211_ADDR_LEN],
90 			    const uint8_t [IEEE80211_ADDR_LEN]);
91 static void		rtwn_vap_delete(struct ieee80211vap *);
92 static int		rtwn_read_chipid(struct rtwn_softc *);
93 static int		rtwn_ioctl_reset(struct ieee80211vap *, u_long);
94 #ifndef RTWN_WITHOUT_UCODE
95 static void		rtwn_set_media_status(struct rtwn_softc *,
96 			    union sec_param *);
97 static int		rtwn_tx_fwpkt_check(struct rtwn_softc *,
98 			    struct ieee80211vap *);
99 static int		rtwn_construct_nulldata(struct rtwn_softc *,
100 			    struct ieee80211vap *, uint8_t *, int);
101 static int		rtwn_push_nulldata(struct rtwn_softc *,
102 			    struct ieee80211vap *);
103 static void		rtwn_pwrmode_init(void *);
104 static void		rtwn_set_pwrmode_cb(struct rtwn_softc *,
105 			    union sec_param *);
106 #endif
107 static void		rtwn_tsf_sync_adhoc(void *);
108 static void		rtwn_tsf_sync_adhoc_task(void *, int);
109 static void		rtwn_tsf_sync_enable(struct rtwn_softc *,
110 			    struct ieee80211vap *);
111 static void		rtwn_set_ack_preamble(struct rtwn_softc *);
112 static void		rtwn_set_mode(struct rtwn_softc *, uint8_t, int);
113 static int		rtwn_monitor_newstate(struct ieee80211vap *,
114 			    enum ieee80211_state, int);
115 static int		rtwn_newstate(struct ieee80211vap *,
116 			    enum ieee80211_state, int);
117 static void		rtwn_calc_basicrates(struct rtwn_softc *);
118 static int		rtwn_run(struct rtwn_softc *,
119 			    struct ieee80211vap *);
120 #ifndef D4054
121 static void		rtwn_watchdog(void *);
122 #endif
123 static void		rtwn_parent(struct ieee80211com *);
124 static int		rtwn_llt_write(struct rtwn_softc *, uint32_t,
125 			    uint32_t);
126 static int		rtwn_llt_init(struct rtwn_softc *);
127 static int		rtwn_dma_init(struct rtwn_softc *);
128 static int		rtwn_mac_init(struct rtwn_softc *);
129 static void		rtwn_mrr_init(struct rtwn_softc *);
130 static void		rtwn_scan_start(struct ieee80211com *);
131 static void		rtwn_scan_curchan(struct ieee80211_scan_state *,
132 			    unsigned long);
133 static void		rtwn_scan_end(struct ieee80211com *);
134 static void		rtwn_getradiocaps(struct ieee80211com *, int, int *,
135 			    struct ieee80211_channel[]);
136 static void		rtwn_update_chw(struct ieee80211com *);
137 static void		rtwn_set_channel(struct ieee80211com *);
138 static int		rtwn_wme_update(struct ieee80211com *);
139 static void		rtwn_update_slot(struct ieee80211com *);
140 static void		rtwn_update_slot_cb(struct rtwn_softc *,
141 			    union sec_param *);
142 static void		rtwn_update_aifs(struct rtwn_softc *, uint8_t);
143 static void		rtwn_update_promisc(struct ieee80211com *);
144 static void		rtwn_update_mcast(struct ieee80211com *);
145 static int		rtwn_set_bssid(struct rtwn_softc *,
146 			    const uint8_t *, int);
147 static int		rtwn_set_macaddr(struct rtwn_softc *,
148 			    const uint8_t *, int);
149 static struct ieee80211_node *rtwn_node_alloc(struct ieee80211vap *,
150 			    const uint8_t mac[IEEE80211_ADDR_LEN]);
151 static void		rtwn_newassoc(struct ieee80211_node *, int);
152 static void		rtwn_node_free(struct ieee80211_node *);
153 static void		rtwn_init_beacon_reg(struct rtwn_softc *);
154 static int		rtwn_init(struct rtwn_softc *);
155 static void		rtwn_stop(struct rtwn_softc *);
156 
157 MALLOC_DEFINE(M_RTWN_PRIV, "rtwn_priv", "rtwn driver private state");
158 
159 static const uint8_t rtwn_chan_2ghz[] =
160 	{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
161 
162 static const uint16_t wme2reg[] =
163 	{ R92C_EDCA_BE_PARAM, R92C_EDCA_BK_PARAM,
164 	  R92C_EDCA_VI_PARAM, R92C_EDCA_VO_PARAM };
165 
166 int
167 rtwn_attach(struct rtwn_softc *sc)
168 {
169 	struct ieee80211com *ic = &sc->sc_ic;
170 	int error;
171 
172 	sc->cur_bcnq_id = RTWN_VAP_ID_INVALID;
173 
174 	RTWN_NT_LOCK_INIT(sc);
175 	rtwn_cmdq_init(sc);
176 #ifndef D4054
177 	callout_init_mtx(&sc->sc_watchdog_to, &sc->sc_mtx, 0);
178 #endif
179 	callout_init(&sc->sc_calib_to, 0);
180 	callout_init(&sc->sc_pwrmode_init, 0);
181 	mbufq_init(&sc->sc_snd, ifqmaxlen);
182 
183 	RTWN_LOCK(sc);
184 	error = rtwn_read_chipid(sc);
185 	RTWN_UNLOCK(sc);
186 	if (error != 0) {
187 		device_printf(sc->sc_dev, "unsupported test chip\n");
188 		goto detach;
189 	}
190 
191 	error = rtwn_read_rom(sc);
192 	if (error != 0) {
193 		device_printf(sc->sc_dev, "%s: cannot read rom, error %d\n",
194 		    __func__, error);
195 		goto detach;
196 	}
197 
198 	if (sc->macid_limit > RTWN_MACID_LIMIT) {
199 		device_printf(sc->sc_dev,
200 		    "macid limit will be reduced from %d to %d\n",
201 		    sc->macid_limit, RTWN_MACID_LIMIT);
202 		sc->macid_limit = RTWN_MACID_LIMIT;
203 	}
204 	if (sc->cam_entry_limit > RTWN_CAM_ENTRY_LIMIT) {
205 		device_printf(sc->sc_dev,
206 		    "cam entry limit will be reduced from %d to %d\n",
207 		    sc->cam_entry_limit, RTWN_CAM_ENTRY_LIMIT);
208 		sc->cam_entry_limit = RTWN_CAM_ENTRY_LIMIT;
209 	}
210 	if (sc->txdesc_len > RTWN_TX_DESC_SIZE) {
211 		device_printf(sc->sc_dev,
212 		    "adjust size for Tx descriptor (current %d, needed %d)\n",
213 		    RTWN_TX_DESC_SIZE, sc->txdesc_len);
214 		goto detach;
215 	}
216 
217 	device_printf(sc->sc_dev, "MAC/BB %s, RF 6052 %dT%dR\n",
218 	    sc->name, sc->ntxchains, sc->nrxchains);
219 
220 	ic->ic_softc = sc;
221 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
222 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
223 
224 	/* set device capabilities */
225 	ic->ic_caps =
226 		  IEEE80211_C_STA		/* station mode */
227 		| IEEE80211_C_MONITOR		/* monitor mode */
228 		| IEEE80211_C_IBSS		/* adhoc mode */
229 		| IEEE80211_C_HOSTAP		/* hostap mode */
230 #if 0	/* TODO: HRPWM register setup */
231 #ifndef RTWN_WITHOUT_UCODE
232 		| IEEE80211_C_PMGT		/* Station-side power mgmt */
233 #endif
234 #endif
235 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
236 		| IEEE80211_C_SHSLOT		/* short slot time supported */
237 #if 0
238 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
239 #endif
240 		| IEEE80211_C_WPA		/* 802.11i */
241 		| IEEE80211_C_WME		/* 802.11e */
242 		| IEEE80211_C_SWAMSDUTX		/* Do software A-MSDU TX */
243 		| IEEE80211_C_FF		/* Atheros fast-frames */
244 		;
245 
246 	if (sc->sc_hwcrypto != RTWN_CRYPTO_SW) {
247 		ic->ic_cryptocaps =
248 		    IEEE80211_CRYPTO_WEP |
249 		    IEEE80211_CRYPTO_TKIP |
250 		    IEEE80211_CRYPTO_AES_CCM;
251 	}
252 
253 	ic->ic_htcaps =
254 	      IEEE80211_HTCAP_SHORTGI20		/* short GI in 20MHz */
255 	    | IEEE80211_HTCAP_MAXAMSDU_3839	/* max A-MSDU length */
256 	    | IEEE80211_HTCAP_SMPS_OFF		/* SM PS mode disabled */
257 	    /* s/w capabilities */
258 	    | IEEE80211_HTC_HT			/* HT operation */
259 	    | IEEE80211_HTC_AMPDU		/* A-MPDU tx */
260 	    | IEEE80211_HTC_AMSDU		/* A-MSDU tx */
261 	    ;
262 
263 	if (sc->sc_ht40) {
264 		ic->ic_htcaps |=
265 		      IEEE80211_HTCAP_CHWIDTH40	/* 40 MHz channel width */
266 		    | IEEE80211_HTCAP_SHORTGI40	/* short GI in 40MHz */
267 		    ;
268 	}
269 
270 	ic->ic_txstream = sc->ntxchains;
271 	ic->ic_rxstream = sc->nrxchains;
272 
273 	/* Enable TX watchdog */
274 #ifdef D4054
275 	ic->ic_flags_ext |= IEEE80211_FEXT_WATCHDOG;
276 #endif
277 
278 	/* Adjust capabilities. */
279 	rtwn_adj_devcaps(sc);
280 
281 	rtwn_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
282 	    ic->ic_channels);
283 
284 	/* XXX TODO: setup regdomain if R92C_CHANNEL_PLAN_BY_HW bit is set. */
285 
286 	ieee80211_ifattach(ic);
287 	ic->ic_raw_xmit = rtwn_raw_xmit;
288 	ic->ic_scan_start = rtwn_scan_start;
289 	sc->sc_scan_curchan = ic->ic_scan_curchan;
290 	ic->ic_scan_curchan = rtwn_scan_curchan;
291 	ic->ic_scan_end = rtwn_scan_end;
292 	ic->ic_getradiocaps = rtwn_getradiocaps;
293 	ic->ic_update_chw = rtwn_update_chw;
294 	ic->ic_set_channel = rtwn_set_channel;
295 	ic->ic_transmit = rtwn_transmit;
296 	ic->ic_parent = rtwn_parent;
297 	ic->ic_vap_create = rtwn_vap_create;
298 	ic->ic_vap_delete = rtwn_vap_delete;
299 	ic->ic_wme.wme_update = rtwn_wme_update;
300 	ic->ic_updateslot = rtwn_update_slot;
301 	ic->ic_update_promisc = rtwn_update_promisc;
302 	ic->ic_update_mcast = rtwn_update_mcast;
303 	ic->ic_node_alloc = rtwn_node_alloc;
304 	ic->ic_newassoc = rtwn_newassoc;
305 	sc->sc_node_free = ic->ic_node_free;
306 	ic->ic_node_free = rtwn_node_free;
307 
308 	rtwn_postattach(sc);
309 	rtwn_radiotap_attach(sc);
310 
311 	if (bootverbose)
312 		ieee80211_announce(ic);
313 
314 	return (0);
315 
316 detach:
317 	return (ENXIO);			/* failure */
318 }
319 
320 static void
321 rtwn_radiotap_attach(struct rtwn_softc *sc)
322 {
323 	struct rtwn_rx_radiotap_header *rxtap = &sc->sc_rxtap;
324 	struct rtwn_tx_radiotap_header *txtap = &sc->sc_txtap;
325 
326 	ieee80211_radiotap_attach(&sc->sc_ic,
327 	    &txtap->wt_ihdr, sizeof(*txtap), RTWN_TX_RADIOTAP_PRESENT,
328 	    &rxtap->wr_ihdr, sizeof(*rxtap), RTWN_RX_RADIOTAP_PRESENT);
329 }
330 
331 void
332 rtwn_sysctlattach(struct rtwn_softc *sc)
333 {
334 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
335 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
336 
337 #if 1
338 	sc->sc_ht40 = 0;
339 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
340 	    "ht40", CTLFLAG_RDTUN, &sc->sc_ht40,
341 	    sc->sc_ht40, "Enable 40 MHz mode support");
342 #endif
343 
344 #ifdef RTWN_DEBUG
345 	SYSCTL_ADD_U32(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
346 	    "debug", CTLFLAG_RWTUN, &sc->sc_debug, sc->sc_debug,
347 	    "Control debugging printfs");
348 #endif
349 
350 	sc->sc_hwcrypto = RTWN_CRYPTO_PAIR;
351 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
352 	    "hwcrypto", CTLFLAG_RDTUN, &sc->sc_hwcrypto,
353 	    sc->sc_hwcrypto, "Enable h/w crypto: "
354 	    "0 - disable, 1 - pairwise keys, 2 - all keys");
355 	if (sc->sc_hwcrypto >= RTWN_CRYPTO_MAX)
356 		sc->sc_hwcrypto = RTWN_CRYPTO_FULL;
357 
358 	sc->sc_ratectl_sysctl = RTWN_RATECTL_NET80211;
359 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
360 	    "ratectl", CTLFLAG_RDTUN, &sc->sc_ratectl_sysctl,
361 	    sc->sc_ratectl_sysctl, "Select rate control mechanism: "
362 	    "0 - disabled, 1 - via net80211, 2 - via firmware");
363 	if (sc->sc_ratectl_sysctl >= RTWN_RATECTL_MAX)
364 		sc->sc_ratectl_sysctl = RTWN_RATECTL_FW;
365 
366 	sc->sc_ratectl = sc->sc_ratectl_sysctl;
367 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
368 	    "ratectl_selected", CTLFLAG_RD, &sc->sc_ratectl,
369 	    sc->sc_ratectl,
370 	    "Currently selected rate control mechanism (by the driver)");
371 }
372 
373 void
374 rtwn_detach(struct rtwn_softc *sc)
375 {
376 	struct ieee80211com *ic = &sc->sc_ic;
377 
378 	if (ic->ic_softc == sc) {
379 		/* Stop command queue. */
380 		RTWN_CMDQ_LOCK(sc);
381 		sc->sc_detached = 1;
382 		RTWN_CMDQ_UNLOCK(sc);
383 
384 		ieee80211_draintask(ic, &sc->cmdq_task);
385 		ieee80211_ifdetach(ic);
386 	}
387 
388 	rtwn_cmdq_destroy(sc);
389 	if (RTWN_NT_LOCK_INITIALIZED(sc))
390 		RTWN_NT_LOCK_DESTROY(sc);
391 }
392 
393 void
394 rtwn_suspend(struct rtwn_softc *sc)
395 {
396 	struct ieee80211com *ic = &sc->sc_ic;
397 
398 	ieee80211_suspend_all(ic);
399 }
400 
401 void
402 rtwn_resume(struct rtwn_softc *sc)
403 {
404 	struct ieee80211com *ic = &sc->sc_ic;
405 
406 	ieee80211_resume_all(ic);
407 }
408 
409 static void
410 rtwn_vap_decrement_counters(struct rtwn_softc *sc,
411     enum ieee80211_opmode opmode, int id)
412 {
413 
414 	RTWN_ASSERT_LOCKED(sc);
415 
416 	if (id != RTWN_VAP_ID_INVALID) {
417 		KASSERT(id == 0 || id == 1, ("wrong vap id %d!\n", id));
418 		KASSERT(sc->vaps[id] != NULL, ("vap pointer is NULL\n"));
419 		sc->vaps[id] = NULL;
420 	}
421 
422 	switch (opmode) {
423 	case IEEE80211_M_HOSTAP:
424 		sc->ap_vaps--;
425 		/* FALLTHROUGH */
426 	case IEEE80211_M_IBSS:
427 		sc->bcn_vaps--;
428 		/* FALLTHROUGH */
429 	case IEEE80211_M_STA:
430 		sc->nvaps--;
431 		break;
432 	case IEEE80211_M_MONITOR:
433 		sc->mon_vaps--;
434 		break;
435 	default:
436 		KASSERT(0, ("wrong opmode %d\n", opmode));
437 		break;
438 	}
439 
440 	KASSERT(sc->vaps_running >= 0 && sc->monvaps_running >= 0,
441 	    ("number of running vaps is negative (vaps %d, monvaps %d)\n",
442 	    sc->vaps_running, sc->monvaps_running));
443 	KASSERT(sc->vaps_running - sc->monvaps_running <= RTWN_PORT_COUNT,
444 	    ("number of running vaps is too big (vaps %d, monvaps %d)\n",
445 	    sc->vaps_running, sc->monvaps_running));
446 
447 	KASSERT(sc->nvaps >= 0 && sc->nvaps <= RTWN_PORT_COUNT,
448 	    ("wrong value %d for nvaps\n", sc->nvaps));
449 	KASSERT(sc->mon_vaps >= 0, ("mon_vaps is negative (%d)\n",
450 	    sc->mon_vaps));
451 	KASSERT(sc->bcn_vaps >= 0 && ((RTWN_CHIP_HAS_BCNQ1(sc) &&
452 	    sc->bcn_vaps <= RTWN_PORT_COUNT) || sc->bcn_vaps <= 1),
453 	    ("bcn_vaps value %d is wrong\n", sc->bcn_vaps));
454 	KASSERT(sc->ap_vaps >= 0 && ((RTWN_CHIP_HAS_BCNQ1(sc) &&
455 	    sc->ap_vaps <= RTWN_PORT_COUNT) || sc->ap_vaps <= 1),
456 	    ("ap_vaps value %d is wrong\n", sc->ap_vaps));
457 }
458 
459 static void
460 rtwn_set_ic_opmode(struct rtwn_softc *sc)
461 {
462 	struct ieee80211com *ic = &sc->sc_ic;
463 
464 	RTWN_ASSERT_LOCKED(sc);
465 
466 	/* for ieee80211_reset_erp() */
467 	if (sc->bcn_vaps - sc->ap_vaps > 0)
468 		ic->ic_opmode = IEEE80211_M_IBSS;
469 	else if (sc->ap_vaps > 0)
470 		ic->ic_opmode = IEEE80211_M_HOSTAP;
471 	else if (sc->nvaps > 0)
472 		ic->ic_opmode = IEEE80211_M_STA;
473 	else
474 		ic->ic_opmode = IEEE80211_M_MONITOR;
475 }
476 
477 static struct ieee80211vap *
478 rtwn_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
479     enum ieee80211_opmode opmode, int flags,
480     const uint8_t bssid[IEEE80211_ADDR_LEN],
481     const uint8_t mac[IEEE80211_ADDR_LEN])
482 {
483 	struct rtwn_softc *sc = ic->ic_softc;
484 	struct rtwn_vap *uvp;
485 	struct ieee80211vap *vap;
486 	int id = RTWN_VAP_ID_INVALID;
487 
488 	RTWN_LOCK(sc);
489 	KASSERT(sc->nvaps <= RTWN_PORT_COUNT,
490 	    ("nvaps overflow (%d > %d)\n", sc->nvaps, RTWN_PORT_COUNT));
491 	KASSERT(sc->ap_vaps <= RTWN_PORT_COUNT,
492 	    ("ap_vaps overflow (%d > %d)\n", sc->ap_vaps, RTWN_PORT_COUNT));
493 	KASSERT(sc->bcn_vaps <= RTWN_PORT_COUNT,
494 	    ("bcn_vaps overflow (%d > %d)\n", sc->bcn_vaps, RTWN_PORT_COUNT));
495 
496 	if (opmode != IEEE80211_M_MONITOR) {
497 		switch (sc->nvaps) {
498 		case 0:
499 			id = 0;
500 			break;
501 		case 1:
502 			if (sc->vaps[1] == NULL)
503 				id = 1;
504 			else if (sc->vaps[0] == NULL)
505 				id = 0;
506 			KASSERT(id != RTWN_VAP_ID_INVALID,
507 			    ("no free ports left\n"));
508 			break;
509 		case 2:
510 		default:
511 			goto fail;
512 		}
513 
514 		if (opmode == IEEE80211_M_IBSS ||
515 		    opmode == IEEE80211_M_HOSTAP) {
516 			if ((sc->bcn_vaps == 1 && !RTWN_CHIP_HAS_BCNQ1(sc)) ||
517 			    sc->bcn_vaps == RTWN_PORT_COUNT)
518 				goto fail;
519 		}
520 	}
521 
522 	switch (opmode) {
523 	case IEEE80211_M_HOSTAP:
524 		sc->ap_vaps++;
525 		/* FALLTHROUGH */
526 	case IEEE80211_M_IBSS:
527 		sc->bcn_vaps++;
528 		/* FALLTHROUGH */
529 	case IEEE80211_M_STA:
530 		sc->nvaps++;
531 		break;
532 	case IEEE80211_M_MONITOR:
533 		sc->mon_vaps++;
534 		break;
535 	default:
536 		KASSERT(0, ("unknown opmode %d\n", opmode));
537 		goto fail;
538 	}
539 	RTWN_UNLOCK(sc);
540 
541 	uvp = malloc(sizeof(struct rtwn_vap), M_80211_VAP, M_WAITOK | M_ZERO);
542 	uvp->id = id;
543 	if (id != RTWN_VAP_ID_INVALID) {
544 		RTWN_LOCK(sc);
545 		sc->vaps[id] = uvp;
546 		RTWN_UNLOCK(sc);
547 	}
548 	vap = &uvp->vap;
549 	/* enable s/w bmiss handling for sta mode */
550 
551 	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
552 	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
553 		/* out of memory */
554 		free(uvp, M_80211_VAP);
555 
556 		RTWN_LOCK(sc);
557 		rtwn_vap_decrement_counters(sc, opmode, id);
558 		RTWN_UNLOCK(sc);
559 
560 		return (NULL);
561 	}
562 
563 	rtwn_beacon_init(sc, &uvp->bcn_desc.txd[0], uvp->id);
564 	rtwn_vap_preattach(sc, vap);
565 
566 	/* override state transition machine */
567 	uvp->newstate = vap->iv_newstate;
568 	if (opmode == IEEE80211_M_MONITOR)
569 		vap->iv_newstate = rtwn_monitor_newstate;
570 	else
571 		vap->iv_newstate = rtwn_newstate;
572 	vap->iv_update_beacon = rtwn_update_beacon;
573 	vap->iv_reset = rtwn_ioctl_reset;
574 	vap->iv_key_alloc = rtwn_key_alloc;
575 	vap->iv_key_set = rtwn_key_set;
576 	vap->iv_key_delete = rtwn_key_delete;
577 	vap->iv_max_aid = sc->macid_limit;
578 
579 	/* 802.11n parameters */
580 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_16;
581 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
582 
583 	if (opmode == IEEE80211_M_IBSS) {
584 		uvp->recv_mgmt = vap->iv_recv_mgmt;
585 		vap->iv_recv_mgmt = rtwn_adhoc_recv_mgmt;
586 		TASK_INIT(&uvp->tsf_sync_adhoc_task, 0,
587 		    rtwn_tsf_sync_adhoc_task, vap);
588 		callout_init(&uvp->tsf_sync_adhoc, 0);
589 	}
590 
591 	/*
592 	 * NB: driver can select net80211 RA even when user requests
593 	 * another mechanism.
594 	 */
595 	ieee80211_ratectl_init(vap);
596 
597 	/* complete setup */
598 	ieee80211_vap_attach(vap, ieee80211_media_change,
599 	    ieee80211_media_status, mac);
600 
601 	RTWN_LOCK(sc);
602 	rtwn_set_ic_opmode(sc);
603 	if (sc->sc_flags & RTWN_RUNNING) {
604 		if (uvp->id != RTWN_VAP_ID_INVALID)
605 			rtwn_set_macaddr(sc, vap->iv_myaddr, uvp->id);
606 
607 		rtwn_rxfilter_update(sc);
608 	}
609 	RTWN_UNLOCK(sc);
610 
611 	return (vap);
612 
613 fail:
614 	RTWN_UNLOCK(sc);
615 	return (NULL);
616 }
617 
618 static void
619 rtwn_vap_delete(struct ieee80211vap *vap)
620 {
621 	struct ieee80211com *ic = vap->iv_ic;
622 	struct rtwn_softc *sc = ic->ic_softc;
623 	struct rtwn_vap *uvp = RTWN_VAP(vap);
624 
625 	/* Put vap into INIT state + stop device if needed. */
626 	ieee80211_stop(vap);
627 	ieee80211_draintask(ic, &vap->iv_nstate_task);
628 	ieee80211_draintask(ic, &ic->ic_parent_task);
629 
630 	RTWN_LOCK(sc);
631 	if (uvp->bcn_mbuf != NULL)
632 		m_freem(uvp->bcn_mbuf);
633 	/* Cancel any unfinished Tx. */
634 	rtwn_reset_lists(sc, vap);
635 	rtwn_vap_decrement_counters(sc, vap->iv_opmode, uvp->id);
636 	rtwn_set_ic_opmode(sc);
637 	if (sc->sc_flags & RTWN_RUNNING)
638 		rtwn_rxfilter_update(sc);
639 	RTWN_UNLOCK(sc);
640 
641 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
642 		ieee80211_draintask(ic, &uvp->tsf_sync_adhoc_task);
643 		callout_drain(&uvp->tsf_sync_adhoc);
644 	}
645 
646 	ieee80211_ratectl_deinit(vap);
647 	ieee80211_vap_detach(vap);
648 	free(uvp, M_80211_VAP);
649 }
650 
651 static int
652 rtwn_read_chipid(struct rtwn_softc *sc)
653 {
654 	uint32_t reg;
655 
656 	reg = rtwn_read_4(sc, R92C_SYS_CFG);
657 	if (reg & R92C_SYS_CFG_TRP_VAUX_EN)	/* test chip */
658 		return (EOPNOTSUPP);
659 
660 	rtwn_read_chipid_vendor(sc, reg);
661 
662 	return (0);
663 }
664 
665 static int
666 rtwn_ioctl_reset(struct ieee80211vap *vap, u_long cmd)
667 {
668 	int error;
669 
670 	switch (cmd) {
671 #ifndef RTWN_WITHOUT_UCODE
672 	case IEEE80211_IOC_POWERSAVE:
673 	case IEEE80211_IOC_POWERSAVESLEEP:
674 	{
675 		struct rtwn_softc *sc = vap->iv_ic->ic_softc;
676 		struct rtwn_vap *uvp = RTWN_VAP(vap);
677 
678 		if (vap->iv_opmode == IEEE80211_M_STA && uvp->id == 0) {
679 			RTWN_LOCK(sc);
680 			if (sc->sc_flags & RTWN_RUNNING)
681 				error = rtwn_set_pwrmode(sc, vap, 1);
682 			else
683 				error = 0;
684 			RTWN_UNLOCK(sc);
685 			if (error != 0)
686 				error = ENETRESET;
687 		} else
688 			error = EOPNOTSUPP;
689 		break;
690 	}
691 #endif
692 	case IEEE80211_IOC_SHORTGI:
693 	case IEEE80211_IOC_RTSTHRESHOLD:
694 	case IEEE80211_IOC_PROTMODE:
695 	case IEEE80211_IOC_HTPROTMODE:
696 		error = 0;
697 		break;
698 	default:
699 		error = ENETRESET;
700 		break;
701 	}
702 
703 	return (error);
704 }
705 
706 #ifndef RTWN_WITHOUT_UCODE
707 static void
708 rtwn_set_media_status(struct rtwn_softc *sc, union sec_param *data)
709 {
710 	sc->sc_set_media_status(sc, data->macid);
711 }
712 
713 static int
714 rtwn_tx_fwpkt_check(struct rtwn_softc *sc, struct ieee80211vap *vap)
715 {
716 	int ntries, error;
717 
718 	for (ntries = 0; ntries < 5; ntries++) {
719 		error = rtwn_push_nulldata(sc, vap);
720 		if (error == 0)
721 			break;
722 	}
723 	if (ntries == 5) {
724 		device_printf(sc->sc_dev,
725 		    "%s: cannot push f/w frames into chip, error %d!\n",
726 		    __func__, error);
727 		return (error);
728 	}
729 
730 	return (0);
731 }
732 
733 static int
734 rtwn_construct_nulldata(struct rtwn_softc *sc, struct ieee80211vap *vap,
735     uint8_t *ptr, int qos)
736 {
737 	struct rtwn_vap *uvp = RTWN_VAP(vap);
738 	struct ieee80211com *ic = &sc->sc_ic;
739 	struct rtwn_tx_desc_common *txd;
740 	struct ieee80211_frame *wh;
741 	int pktlen;
742 
743 	/* XXX obtain from net80211 */
744 	wh = (struct ieee80211_frame *)(ptr + sc->txdesc_len);
745 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
746 	wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
747 	IEEE80211_ADDR_COPY(wh->i_addr1, vap->iv_bss->ni_bssid);
748 	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
749 	IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_macaddr);
750 
751 	txd = (struct rtwn_tx_desc_common *)ptr;
752 	txd->offset = sc->txdesc_len;
753 	pktlen = sc->txdesc_len;
754 	if (qos) {
755 		struct ieee80211_qosframe *qwh;
756 		const int tid = WME_AC_TO_TID(WME_AC_BE);
757 
758 		qwh = (struct ieee80211_qosframe *)wh;
759 		qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS_NULL;
760 		qwh->i_qos[0] = tid & IEEE80211_QOS_TID;
761 
762 		txd->pktlen = htole16(sizeof(struct ieee80211_qosframe));
763 		pktlen += sizeof(struct ieee80211_qosframe);
764 	} else {
765 		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA;
766 
767 		txd->pktlen = htole16(sizeof(struct ieee80211_frame));
768 		pktlen += sizeof(struct ieee80211_frame);
769 	}
770 
771 	rtwn_fill_tx_desc_null(sc, ptr,
772 	    ic->ic_curmode == IEEE80211_MODE_11B, qos, uvp->id);
773 
774 	return (pktlen);
775 }
776 
777 static int
778 rtwn_push_nulldata(struct rtwn_softc *sc, struct ieee80211vap *vap)
779 {
780 	struct rtwn_vap *uvp = RTWN_VAP(vap);
781 	struct ieee80211com *ic = vap->iv_ic;
782 	struct ieee80211_channel *c = ic->ic_curchan;
783 	struct mbuf *m;
784 	uint8_t *ptr;
785 	int required_size, bcn_size, null_size, null_data, error;
786 
787 	if (!(sc->sc_flags & RTWN_FW_LOADED))
788 		return (0);	/* requires firmware */
789 
790 	KASSERT(sc->page_size > 0, ("page size was not set!\n"));
791 
792 	/* Leave some space for beacon (multi-vap) */
793 	bcn_size = roundup(RTWN_BCN_MAX_SIZE, sc->page_size);
794 	/* 1 page for Null Data + 1 page for Qos Null Data frames. */
795 	required_size = bcn_size + sc->page_size * 2;
796 
797 	m = m_get2(required_size, M_NOWAIT, MT_DATA, M_PKTHDR);
798 	if (m == NULL)
799 		return (ENOMEM);
800 
801 	/* Setup beacon descriptor. */
802 	rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0],
803 	    IEEE80211_IS_CHAN_5GHZ(c));
804 
805 	ptr = mtod(m, uint8_t *);
806 	memset(ptr, 0, required_size - sc->txdesc_len);
807 
808 	/* Construct Null Data frame. */
809 	ptr += bcn_size - sc->txdesc_len;
810 	null_size = rtwn_construct_nulldata(sc, vap, ptr, 0);
811 	KASSERT(null_size < sc->page_size,
812 	    ("recalculate size for Null Data frame\n"));
813 
814 	/* Construct Qos Null Data frame. */
815 	ptr += roundup(null_size, sc->page_size);
816 	null_size = rtwn_construct_nulldata(sc, vap, ptr, 1);
817 	KASSERT(null_size < sc->page_size,
818 	    ("recalculate size for Qos Null Data frame\n"));
819 
820 	/* Do not try to detect a beacon here. */
821 	rtwn_setbits_1_shift(sc, R92C_CR, 0, R92C_CR_ENSWBCN, 1);
822 	rtwn_setbits_1_shift(sc, R92C_FWHW_TXQ_CTRL,
823 	    R92C_FWHW_TXQ_CTRL_REAL_BEACON, 0, 2);
824 
825 	if (uvp->bcn_mbuf != NULL)
826 		m_freem(uvp->bcn_mbuf);
827 
828 	m->m_pkthdr.len = m->m_len = required_size - sc->txdesc_len;
829 	uvp->bcn_mbuf = m;
830 
831 	error = rtwn_tx_beacon_check(sc, uvp);
832 	if (error != 0) {
833 		RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
834 		    "%s: frame was not recognized!\n", __func__);
835 		goto fail;
836 	}
837 
838 	/* Setup addresses in firmware. */
839 	null_data = howmany(bcn_size, sc->page_size);
840 	error = rtwn_set_rsvd_page(sc, 0, null_data, null_data + 1);
841 	if (error != 0) {
842 		device_printf(sc->sc_dev,
843 		    "%s: CMD_RSVD_PAGE was not sent, error %d\n",
844 		    __func__, error);
845 		goto fail;
846 	}
847 
848 fail:
849 	/* Re-enable beacon detection. */
850 	rtwn_setbits_1_shift(sc, R92C_FWHW_TXQ_CTRL,
851 	    0, R92C_FWHW_TXQ_CTRL_REAL_BEACON, 2);
852 	rtwn_setbits_1_shift(sc, R92C_CR, R92C_CR_ENSWBCN, 0, 1);
853 
854 	/* Restore beacon (if present). */
855 	if (sc->bcn_vaps > 0 && sc->vaps[!uvp->id] != NULL) {
856 		struct rtwn_vap *uvp2 = sc->vaps[!uvp->id];
857 
858 		if (uvp2->curr_mode != R92C_MSR_NOLINK)
859 			error = rtwn_tx_beacon_check(sc, uvp2);
860 	}
861 
862 	return (error);
863 }
864 
865 static void
866 rtwn_pwrmode_init(void *arg)
867 {
868 	struct rtwn_softc *sc = arg;
869 
870 	rtwn_cmd_sleepable(sc, NULL, 0, rtwn_set_pwrmode_cb);
871 }
872 
873 static void
874 rtwn_set_pwrmode_cb(struct rtwn_softc *sc, union sec_param *data)
875 {
876 	struct ieee80211vap *vap = &sc->vaps[0]->vap;
877 
878 	if (vap != NULL)
879 		rtwn_set_pwrmode(sc, vap, 1);
880 }
881 #endif
882 
883 static void
884 rtwn_tsf_sync_adhoc(void *arg)
885 {
886 	struct ieee80211vap *vap = arg;
887 	struct ieee80211com *ic = vap->iv_ic;
888 	struct rtwn_vap *uvp = RTWN_VAP(vap);
889 
890 	if (uvp->curr_mode != R92C_MSR_NOLINK) {
891 		/* Do it in process context. */
892 		ieee80211_runtask(ic, &uvp->tsf_sync_adhoc_task);
893 	}
894 }
895 
896 /*
897  * Workaround for TSF synchronization:
898  * when BSSID filter in IBSS mode is not set
899  * (and TSF synchronization is enabled), then any beacon may update it.
900  * This routine synchronizes it when BSSID matching is enabled (IBSS merge
901  * is not possible during this period).
902  *
903  * NOTE: there is no race with rtwn_newstate(), since it uses the same
904  * taskqueue.
905  */
906 static void
907 rtwn_tsf_sync_adhoc_task(void *arg, int pending)
908 {
909 	struct ieee80211vap *vap = arg;
910 	struct rtwn_vap *uvp = RTWN_VAP(vap);
911 	struct rtwn_softc *sc = vap->iv_ic->ic_softc;
912 	struct ieee80211_node *ni;
913 
914 	RTWN_LOCK(sc);
915 	ni = ieee80211_ref_node(vap->iv_bss);
916 
917 	/* Accept beacons with the same BSSID. */
918 	rtwn_set_rx_bssid_all(sc, 0);
919 
920 	/* Deny RCR updates. */
921 	sc->sc_flags |= RTWN_RCR_LOCKED;
922 
923 	/* Enable synchronization. */
924 	rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
925 	    R92C_BCN_CTRL_DIS_TSF_UDT0, 0);
926 
927 	/* Synchronize. */
928 	rtwn_delay(sc, ni->ni_intval * 5 * 1000);
929 
930 	/* Disable synchronization. */
931 	rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
932 	    0, R92C_BCN_CTRL_DIS_TSF_UDT0);
933 
934 	/* Accept all beacons. */
935 	sc->sc_flags &= ~RTWN_RCR_LOCKED;
936 	rtwn_set_rx_bssid_all(sc, 1);
937 
938 	/* Schedule next TSF synchronization. */
939 	callout_reset(&uvp->tsf_sync_adhoc, 60*hz, rtwn_tsf_sync_adhoc, vap);
940 
941 	ieee80211_free_node(ni);
942 	RTWN_UNLOCK(sc);
943 }
944 
945 static void
946 rtwn_tsf_sync_enable(struct rtwn_softc *sc, struct ieee80211vap *vap)
947 {
948 	struct ieee80211com *ic = &sc->sc_ic;
949 	struct rtwn_vap *uvp = RTWN_VAP(vap);
950 
951 	/* Reset TSF. */
952 	rtwn_write_1(sc, R92C_DUAL_TSF_RST, R92C_DUAL_TSF_RESET(uvp->id));
953 
954 	switch (vap->iv_opmode) {
955 	case IEEE80211_M_STA:
956 		/* Enable TSF synchronization. */
957 		rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
958 		    R92C_BCN_CTRL_DIS_TSF_UDT0, 0);
959 		break;
960 	case IEEE80211_M_IBSS:
961 		ieee80211_runtask(ic, &uvp->tsf_sync_adhoc_task);
962 		/* FALLTHROUGH */
963 	case IEEE80211_M_HOSTAP:
964 		/* Enable beaconing. */
965 		rtwn_beacon_enable(sc, uvp->id, 1);
966 		break;
967 	default:
968 		device_printf(sc->sc_dev, "undefined opmode %d\n",
969 		    vap->iv_opmode);
970 		return;
971 	}
972 }
973 
974 static void
975 rtwn_set_ack_preamble(struct rtwn_softc *sc)
976 {
977 	struct ieee80211com *ic = &sc->sc_ic;
978 	uint32_t reg;
979 
980 	reg = rtwn_read_4(sc, R92C_WMAC_TRXPTCL_CTL);
981 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
982 		reg |= R92C_WMAC_TRXPTCL_SHPRE;
983 	else
984 		reg &= ~R92C_WMAC_TRXPTCL_SHPRE;
985 	rtwn_write_4(sc, R92C_WMAC_TRXPTCL_CTL, reg);
986 }
987 
988 static void
989 rtwn_set_mode(struct rtwn_softc *sc, uint8_t mode, int id)
990 {
991 
992 	rtwn_setbits_1(sc, R92C_MSR, R92C_MSR_MASK << id * 2, mode << id * 2);
993 	if (sc->vaps[id] != NULL)
994 		sc->vaps[id]->curr_mode = mode;
995 }
996 
997 static int
998 rtwn_monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate,
999     int arg)
1000 {
1001 	struct ieee80211com *ic = vap->iv_ic;
1002 	struct rtwn_softc *sc = ic->ic_softc;
1003 	struct rtwn_vap *uvp = RTWN_VAP(vap);
1004 
1005 	RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s -> %s\n",
1006 	    ieee80211_state_name[vap->iv_state],
1007 	    ieee80211_state_name[nstate]);
1008 
1009 	if (vap->iv_state != nstate) {
1010 		IEEE80211_UNLOCK(ic);
1011 		RTWN_LOCK(sc);
1012 
1013 		switch (nstate) {
1014 		case IEEE80211_S_INIT:
1015 			sc->vaps_running--;
1016 			sc->monvaps_running--;
1017 
1018 			if (sc->vaps_running == 0) {
1019 				/* Turn link LED off. */
1020 				rtwn_set_led(sc, RTWN_LED_LINK, 0);
1021 			}
1022 			break;
1023 		case IEEE80211_S_RUN:
1024 			sc->vaps_running++;
1025 			sc->monvaps_running++;
1026 
1027 			if (sc->vaps_running == 1) {
1028 				/* Turn link LED on. */
1029 				rtwn_set_led(sc, RTWN_LED_LINK, 1);
1030 			}
1031 			break;
1032 		default:
1033 			/* NOTREACHED */
1034 			break;
1035 		}
1036 
1037 		RTWN_UNLOCK(sc);
1038 		IEEE80211_LOCK(ic);
1039 	}
1040 
1041 	return (uvp->newstate(vap, nstate, arg));
1042 }
1043 
1044 static int
1045 rtwn_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1046 {
1047 	struct rtwn_vap *uvp = RTWN_VAP(vap);
1048 	struct ieee80211com *ic = vap->iv_ic;
1049 	struct rtwn_softc *sc = ic->ic_softc;
1050 	enum ieee80211_state ostate;
1051 	int error, early_newstate;
1052 
1053 	ostate = vap->iv_state;
1054 	RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s -> %s\n",
1055 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
1056 
1057 	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC &&
1058 	    ostate == IEEE80211_S_INIT && nstate == IEEE80211_S_RUN) {
1059 		/* need to call iv_newstate() firstly */
1060 		error = uvp->newstate(vap, nstate, arg);
1061 		if (error != 0)
1062 			return (error);
1063 
1064 		early_newstate = 1;
1065 	} else
1066 		early_newstate = 0;
1067 
1068 	IEEE80211_UNLOCK(ic);
1069 	RTWN_LOCK(sc);
1070 	if (ostate == IEEE80211_S_RUN) {
1071 		sc->vaps_running--;
1072 
1073 		/* Set media status to 'No Link'. */
1074 		rtwn_set_mode(sc, R92C_MSR_NOLINK, uvp->id);
1075 
1076 		if (vap->iv_opmode == IEEE80211_M_IBSS) {
1077 			/* Stop periodical TSF synchronization. */
1078 			callout_stop(&uvp->tsf_sync_adhoc);
1079 		}
1080 
1081 		/* Disable TSF synchronization / beaconing. */
1082 		rtwn_beacon_enable(sc, uvp->id, 0);
1083 		rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
1084 		    0, R92C_BCN_CTRL_DIS_TSF_UDT0);
1085 
1086 		/* NB: monitor mode vaps are using port 0. */
1087 		if (uvp->id != 0 || sc->monvaps_running == 0) {
1088 			/* Reset TSF. */
1089 			rtwn_write_1(sc, R92C_DUAL_TSF_RST,
1090 			    R92C_DUAL_TSF_RESET(uvp->id));
1091 		}
1092 
1093 #ifndef RTWN_WITHOUT_UCODE
1094 		if ((ic->ic_caps & IEEE80211_C_PMGT) != 0 && uvp->id == 0) {
1095 			/* Disable power management. */
1096 			callout_stop(&sc->sc_pwrmode_init);
1097 			rtwn_set_pwrmode(sc, vap, 0);
1098 		}
1099 #endif
1100 		if (sc->vaps_running - sc->monvaps_running > 0) {
1101 			/* Recalculate basic rates bitmap. */
1102 			rtwn_calc_basicrates(sc);
1103 		}
1104 
1105 		if (sc->vaps_running == sc->monvaps_running) {
1106 			/* Stop calibration. */
1107 			callout_stop(&sc->sc_calib_to);
1108 
1109 			/* Stop Rx of data frames. */
1110 			rtwn_write_2(sc, R92C_RXFLTMAP2, 0);
1111 
1112 			/* Reset EDCA parameters. */
1113 			rtwn_write_4(sc, R92C_EDCA_VO_PARAM, 0x002f3217);
1114 			rtwn_write_4(sc, R92C_EDCA_VI_PARAM, 0x005e4317);
1115 			rtwn_write_4(sc, R92C_EDCA_BE_PARAM, 0x00105320);
1116 			rtwn_write_4(sc, R92C_EDCA_BK_PARAM, 0x0000a444);
1117 
1118 			if (sc->vaps_running == 0) {
1119 				/* Turn link LED off. */
1120 				rtwn_set_led(sc, RTWN_LED_LINK, 0);
1121 			}
1122 		}
1123 	}
1124 
1125 	error = 0;
1126 	switch (nstate) {
1127 	case IEEE80211_S_SCAN:
1128 		/* Pause AC Tx queues. */
1129 		if (sc->vaps_running == 0)
1130 			rtwn_setbits_1(sc, R92C_TXPAUSE, 0, R92C_TX_QUEUE_AC);
1131 		break;
1132 	case IEEE80211_S_RUN:
1133 		error = rtwn_run(sc, vap);
1134 		if (error != 0) {
1135 			device_printf(sc->sc_dev,
1136 			    "%s: could not move to RUN state\n", __func__);
1137 			break;
1138 		}
1139 
1140 		sc->vaps_running++;
1141 		break;
1142 	default:
1143 		break;
1144 	}
1145 
1146 	RTWN_UNLOCK(sc);
1147 	IEEE80211_LOCK(ic);
1148 	if (error != 0)
1149 		return (error);
1150 
1151 	return (early_newstate ? 0 : uvp->newstate(vap, nstate, arg));
1152 }
1153 
1154 static void
1155 rtwn_calc_basicrates(struct rtwn_softc *sc)
1156 {
1157 	struct ieee80211com *ic = &sc->sc_ic;
1158 	uint32_t basicrates;
1159 	int i;
1160 
1161 	RTWN_ASSERT_LOCKED(sc);
1162 
1163 	if (ic->ic_flags & IEEE80211_F_SCAN)
1164 		return;		/* will be done by rtwn_scan_end(). */
1165 
1166 	basicrates = 0;
1167 	for (i = 0; i < nitems(sc->vaps); i++) {
1168 		struct rtwn_vap *rvp;
1169 		struct ieee80211vap *vap;
1170 		struct ieee80211_node *ni;
1171 		uint32_t rates;
1172 
1173 		rvp = sc->vaps[i];
1174 		if (rvp == NULL || rvp->curr_mode == R92C_MSR_NOLINK)
1175 			continue;
1176 
1177 		vap = &rvp->vap;
1178 		if (vap->iv_bss == NULL)
1179 			continue;
1180 
1181 		ni = ieee80211_ref_node(vap->iv_bss);
1182 		rtwn_get_rates(sc, &ni->ni_rates, NULL, &rates, NULL, 1);
1183 		basicrates |= rates;
1184 		ieee80211_free_node(ni);
1185 	}
1186 
1187 	if (basicrates == 0)
1188 		return;
1189 
1190 	/* XXX initial RTS rate? */
1191 	rtwn_set_basicrates(sc, basicrates);
1192 }
1193 
1194 static int
1195 rtwn_run(struct rtwn_softc *sc, struct ieee80211vap *vap)
1196 {
1197 	struct ieee80211com *ic = vap->iv_ic;
1198 	struct rtwn_vap *uvp = RTWN_VAP(vap);
1199 	struct ieee80211_node *ni;
1200 	uint8_t mode;
1201 	int error;
1202 
1203 	RTWN_ASSERT_LOCKED(sc);
1204 
1205 	error = 0;
1206 	ni = ieee80211_ref_node(vap->iv_bss);
1207 
1208 	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
1209 	    ni->ni_chan == IEEE80211_CHAN_ANYC) {
1210 		error = EINVAL;
1211 		goto fail;
1212 	}
1213 
1214 	switch (vap->iv_opmode) {
1215 	case IEEE80211_M_STA:
1216 		mode = R92C_MSR_INFRA;
1217 		break;
1218 	case IEEE80211_M_IBSS:
1219 		mode = R92C_MSR_ADHOC;
1220 		break;
1221 	case IEEE80211_M_HOSTAP:
1222 		mode = R92C_MSR_AP;
1223 		break;
1224 	default:
1225 		KASSERT(0, ("undefined opmode %d\n", vap->iv_opmode));
1226 		error = EINVAL;
1227 		goto fail;
1228 	}
1229 
1230 	/* Set media status to 'Associated'. */
1231 	rtwn_set_mode(sc, mode, uvp->id);
1232 
1233 	/* Set AssocID. */
1234 	/* XXX multi-vap? */
1235 	rtwn_write_2(sc, R92C_BCN_PSR_RPT,
1236 	    0xc000 | IEEE80211_NODE_AID(ni));
1237 
1238 	/* Set BSSID. */
1239 	rtwn_set_bssid(sc, ni->ni_bssid, uvp->id);
1240 
1241 	/* Set beacon interval. */
1242 	rtwn_write_2(sc, R92C_BCN_INTERVAL(uvp->id), ni->ni_intval);
1243 
1244 	if (sc->vaps_running == sc->monvaps_running) {
1245 		/* Enable Rx of data frames. */
1246 		rtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff);
1247 
1248 		/* Flush all AC queues. */
1249 		rtwn_write_1(sc, R92C_TXPAUSE, 0);
1250 	}
1251 
1252 #ifndef RTWN_WITHOUT_UCODE
1253 	/* Upload (QoS) Null Data frame to firmware. */
1254 	/* Note: do this for port 0 only. */
1255 	if ((ic->ic_caps & IEEE80211_C_PMGT) != 0 &&
1256 	    vap->iv_opmode == IEEE80211_M_STA && uvp->id == 0) {
1257 		error = rtwn_tx_fwpkt_check(sc, vap);
1258 		if (error != 0)
1259 			goto fail;
1260 
1261 		/* Setup power management. */
1262 		/*
1263 		 * NB: it will be enabled immediately - delay it,
1264 		 * so 4-Way handshake will not be interrupted.
1265 		 */
1266 		callout_reset(&sc->sc_pwrmode_init, 5*hz,
1267 		    rtwn_pwrmode_init, sc);
1268 	}
1269 #endif
1270 
1271 	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1272 	    vap->iv_opmode == IEEE80211_M_IBSS) {
1273 		error = rtwn_setup_beacon(sc, ni);
1274 		if (error != 0) {
1275 			device_printf(sc->sc_dev,
1276 			    "unable to push beacon into the chip, "
1277 			    "error %d\n", error);
1278 			goto fail;
1279 		}
1280 	}
1281 
1282 	/* Set ACK preamble type. */
1283 	rtwn_set_ack_preamble(sc);
1284 
1285 	/* Enable TSF synchronization. */
1286 	rtwn_tsf_sync_enable(sc, vap);
1287 
1288 	/* Set basic rates mask. */
1289 	rtwn_calc_basicrates(sc);
1290 
1291 #ifdef RTWN_TODO
1292 	rtwn_write_1(sc, R92C_SIFS_CCK + 1, 10);
1293 	rtwn_write_1(sc, R92C_SIFS_OFDM + 1, 10);
1294 	rtwn_write_1(sc, R92C_SPEC_SIFS + 1, 10);
1295 	rtwn_write_1(sc, R92C_MAC_SPEC_SIFS + 1, 10);
1296 	rtwn_write_1(sc, R92C_R2T_SIFS + 1, 10);
1297 	rtwn_write_1(sc, R92C_T2T_SIFS + 1, 10);
1298 #endif
1299 
1300 	if (sc->vaps_running == sc->monvaps_running) {
1301 		/* Reset temperature calibration state machine. */
1302 		sc->sc_flags &= ~RTWN_TEMP_MEASURED;
1303 		sc->thcal_temp = sc->thermal_meter;
1304 
1305 		/* Start periodic calibration. */
1306 		callout_reset(&sc->sc_calib_to, 2*hz, rtwn_calib_to,
1307 		    sc);
1308 
1309 		if (sc->vaps_running == 0) {
1310 			/* Turn link LED on. */
1311 			rtwn_set_led(sc, RTWN_LED_LINK, 1);
1312 		}
1313 	}
1314 
1315 fail:
1316 	ieee80211_free_node(ni);
1317 
1318 	return (error);
1319 }
1320 
1321 #ifndef D4054
1322 static void
1323 rtwn_watchdog(void *arg)
1324 {
1325 	struct rtwn_softc *sc = arg;
1326 	struct ieee80211com *ic = &sc->sc_ic;
1327 
1328 	RTWN_ASSERT_LOCKED(sc);
1329 
1330 	KASSERT(sc->sc_flags & RTWN_RUNNING, ("not running"));
1331 
1332 	if (sc->sc_tx_timer != 0 && --sc->sc_tx_timer == 0) {
1333 		ic_printf(ic, "device timeout\n");
1334 		ieee80211_restart_all(ic);
1335 		return;
1336 	}
1337 	callout_reset(&sc->sc_watchdog_to, hz, rtwn_watchdog, sc);
1338 }
1339 #endif
1340 
1341 static void
1342 rtwn_parent(struct ieee80211com *ic)
1343 {
1344 	struct rtwn_softc *sc = ic->ic_softc;
1345 	struct ieee80211vap *vap;
1346 
1347 	if (ic->ic_nrunning > 0) {
1348 		if (rtwn_init(sc) != 0) {
1349 			IEEE80211_LOCK(ic);
1350 			TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1351 				ieee80211_stop_locked(vap);
1352 			IEEE80211_UNLOCK(ic);
1353 		} else
1354 			ieee80211_start_all(ic);
1355 	} else
1356 		rtwn_stop(sc);
1357 }
1358 
1359 
1360 static int
1361 rtwn_llt_write(struct rtwn_softc *sc, uint32_t addr, uint32_t data)
1362 {
1363 	int ntries, error;
1364 
1365 	error = rtwn_write_4(sc, R92C_LLT_INIT,
1366 	    SM(R92C_LLT_INIT_OP, R92C_LLT_INIT_OP_WRITE) |
1367 	    SM(R92C_LLT_INIT_ADDR, addr) |
1368 	    SM(R92C_LLT_INIT_DATA, data));
1369 	if (error != 0)
1370 		return (error);
1371 	/* Wait for write operation to complete. */
1372 	for (ntries = 0; ntries < 20; ntries++) {
1373 		if (MS(rtwn_read_4(sc, R92C_LLT_INIT), R92C_LLT_INIT_OP) ==
1374 		    R92C_LLT_INIT_OP_NO_ACTIVE)
1375 			return (0);
1376 		rtwn_delay(sc, 10);
1377 	}
1378 	return (ETIMEDOUT);
1379 }
1380 
1381 static int
1382 rtwn_llt_init(struct rtwn_softc *sc)
1383 {
1384 	int i, error;
1385 
1386 	/* Reserve pages [0; page_count]. */
1387 	for (i = 0; i < sc->page_count; i++) {
1388 		if ((error = rtwn_llt_write(sc, i, i + 1)) != 0)
1389 			return (error);
1390 	}
1391 	/* NB: 0xff indicates end-of-list. */
1392 	if ((error = rtwn_llt_write(sc, i, 0xff)) != 0)
1393 		return (error);
1394 	/*
1395 	 * Use pages [page_count + 1; pktbuf_count - 1]
1396 	 * as ring buffer.
1397 	 */
1398 	for (++i; i < sc->pktbuf_count - 1; i++) {
1399 		if ((error = rtwn_llt_write(sc, i, i + 1)) != 0)
1400 			return (error);
1401 	}
1402 	/* Make the last page point to the beginning of the ring buffer. */
1403 	error = rtwn_llt_write(sc, i, sc->page_count + 1);
1404 	return (error);
1405 }
1406 
1407 static int
1408 rtwn_dma_init(struct rtwn_softc *sc)
1409 {
1410 #define RTWN_CHK(res) do {	\
1411 	if (res != 0)		\
1412 		return (EIO);	\
1413 } while(0)
1414 	uint16_t reg;
1415 	uint8_t tx_boundary;
1416 	int error;
1417 
1418 	/* Initialize LLT table. */
1419 	error = rtwn_llt_init(sc);
1420 	if (error != 0)
1421 		return (error);
1422 
1423 	/* Set the number of pages for each queue. */
1424 	RTWN_DPRINTF(sc, RTWN_DEBUG_RESET,
1425 	    "%s: pages per queue: high %d, normal %d, low %d, public %d\n",
1426 	    __func__, sc->nhqpages, sc->nnqpages, sc->nlqpages,
1427 	    sc->npubqpages);
1428 
1429 	RTWN_CHK(rtwn_write_1(sc, R92C_RQPN_NPQ, sc->nnqpages));
1430 	RTWN_CHK(rtwn_write_4(sc, R92C_RQPN,
1431 	    /* Set number of pages for public queue. */
1432 	    SM(R92C_RQPN_PUBQ, sc->npubqpages) |
1433 	    /* Set number of pages for high priority queue. */
1434 	    SM(R92C_RQPN_HPQ, sc->nhqpages) |
1435 	    /* Set number of pages for low priority queue. */
1436 	    SM(R92C_RQPN_LPQ, sc->nlqpages) |
1437 	    /* Load values. */
1438 	    R92C_RQPN_LD));
1439 
1440 	/* Initialize TX buffer boundary. */
1441 	KASSERT(sc->page_count < 255 && sc->page_count > 0,
1442 	    ("page_count is %d\n", sc->page_count));
1443 	tx_boundary = sc->page_count + 1;
1444 	RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, tx_boundary));
1445 	RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, tx_boundary));
1446 	RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD, tx_boundary));
1447 	RTWN_CHK(rtwn_write_1(sc, R92C_TRXFF_BNDY, tx_boundary));
1448 	RTWN_CHK(rtwn_write_1(sc, R92C_TDECTRL + 1, tx_boundary));
1449 
1450 	error = rtwn_init_bcnq1_boundary(sc);
1451 	if (error != 0)
1452 		return (error);
1453 
1454 	/* Set queue to USB pipe mapping. */
1455 	/* Note: PCIe devices are using some magic number here. */
1456 	reg = rtwn_get_qmap(sc);
1457 	RTWN_CHK(rtwn_setbits_2(sc, R92C_TRXDMA_CTRL,
1458 	    R92C_TRXDMA_CTRL_QMAP_M, reg));
1459 
1460 	/* Configure Tx/Rx DMA (PCIe). */
1461 	rtwn_set_desc_addr(sc);
1462 
1463 	/* Set Tx/Rx transfer page boundary. */
1464 	RTWN_CHK(rtwn_write_2(sc, R92C_TRXFF_BNDY + 2,
1465 	    sc->rx_dma_size - 1));
1466 
1467 	/* Set Tx/Rx transfer page size. */
1468 	rtwn_set_page_size(sc);
1469 
1470 	return (0);
1471 }
1472 
1473 static int
1474 rtwn_mac_init(struct rtwn_softc *sc)
1475 {
1476 	int i, error;
1477 
1478 	/* Write MAC initialization values. */
1479 	for (i = 0; i < sc->mac_size; i++) {
1480 		error = rtwn_write_1(sc, sc->mac_prog[i].reg,
1481 		    sc->mac_prog[i].val);
1482 		if (error != 0)
1483 			return (error);
1484 	}
1485 
1486 	return (0);
1487 }
1488 
1489 static void
1490 rtwn_mrr_init(struct rtwn_softc *sc)
1491 {
1492 	int i;
1493 
1494 	/* Drop rate index by 1 per retry. */
1495 	for (i = 0; i < R92C_DARFRC_SIZE; i++) {
1496 		rtwn_write_1(sc, R92C_DARFRC + i, i + 1);
1497 		rtwn_write_1(sc, R92C_RARFRC + i, i + 1);
1498 	}
1499 }
1500 
1501 static void
1502 rtwn_scan_start(struct ieee80211com *ic)
1503 {
1504 	struct rtwn_softc *sc = ic->ic_softc;
1505 
1506 	RTWN_LOCK(sc);
1507 	/* Pause beaconing. */
1508 	rtwn_setbits_1(sc, R92C_TXPAUSE, 0, R92C_TX_QUEUE_BCN);
1509 	/* Receive beacons / probe responses from any BSSID. */
1510 	if (sc->bcn_vaps == 0)
1511 		rtwn_set_rx_bssid_all(sc, 1);
1512 	RTWN_UNLOCK(sc);
1513 }
1514 
1515 static void
1516 rtwn_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
1517 {
1518 	struct rtwn_softc *sc = ss->ss_ic->ic_softc;
1519 
1520 	/* Make link LED blink during scan. */
1521 	RTWN_LOCK(sc);
1522 	rtwn_set_led(sc, RTWN_LED_LINK, !sc->ledlink);
1523 	RTWN_UNLOCK(sc);
1524 
1525 	sc->sc_scan_curchan(ss, maxdwell);
1526 }
1527 
1528 static void
1529 rtwn_scan_end(struct ieee80211com *ic)
1530 {
1531 	struct rtwn_softc *sc = ic->ic_softc;
1532 
1533 	RTWN_LOCK(sc);
1534 	/* Restore limitations. */
1535 	if (ic->ic_promisc == 0 && sc->bcn_vaps == 0)
1536 		rtwn_set_rx_bssid_all(sc, 0);
1537 
1538 	/* Restore LED state. */
1539 	rtwn_set_led(sc, RTWN_LED_LINK, (sc->vaps_running != 0));
1540 
1541 	/* Restore basic rates mask. */
1542 	rtwn_calc_basicrates(sc);
1543 
1544 	/* Resume beaconing. */
1545 	rtwn_setbits_1(sc, R92C_TXPAUSE, R92C_TX_QUEUE_BCN, 0);
1546 	RTWN_UNLOCK(sc);
1547 }
1548 
1549 static void
1550 rtwn_getradiocaps(struct ieee80211com *ic,
1551     int maxchans, int *nchans, struct ieee80211_channel chans[])
1552 {
1553 	struct rtwn_softc *sc = ic->ic_softc;
1554 	uint8_t bands[IEEE80211_MODE_BYTES];
1555 	int i;
1556 
1557 	memset(bands, 0, sizeof(bands));
1558 	setbit(bands, IEEE80211_MODE_11B);
1559 	setbit(bands, IEEE80211_MODE_11G);
1560 	setbit(bands, IEEE80211_MODE_11NG);
1561 	ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
1562 	    rtwn_chan_2ghz, nitems(rtwn_chan_2ghz), bands,
1563 	    !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40));
1564 
1565 	/* XXX workaround add_channel_list() limitations */
1566 	setbit(bands, IEEE80211_MODE_11A);
1567 	setbit(bands, IEEE80211_MODE_11NA);
1568 	for (i = 0; i < nitems(sc->chan_num_5ghz); i++) {
1569 		if (sc->chan_num_5ghz[i] == 0)
1570 			continue;
1571 
1572 		ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
1573 		    sc->chan_list_5ghz[i], sc->chan_num_5ghz[i], bands,
1574 		    !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40));
1575 	}
1576 }
1577 
1578 static void
1579 rtwn_update_chw(struct ieee80211com *ic)
1580 {
1581 }
1582 
1583 static void
1584 rtwn_set_channel(struct ieee80211com *ic)
1585 {
1586 	struct rtwn_softc *sc = ic->ic_softc;
1587 	struct ieee80211_channel *c = ic->ic_curchan;
1588 
1589 	RTWN_LOCK(sc);
1590 	rtwn_set_chan(sc, c);
1591 	sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
1592 	sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
1593 	sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
1594 	sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
1595 	RTWN_UNLOCK(sc);
1596 }
1597 
1598 static int
1599 rtwn_wme_update(struct ieee80211com *ic)
1600 {
1601 	struct ieee80211_channel *c = ic->ic_curchan;
1602 	struct rtwn_softc *sc = ic->ic_softc;
1603 	struct wmeParams *wmep = sc->cap_wmeParams;
1604 	uint8_t aifs, acm, slottime;
1605 	int ac;
1606 
1607 	/* Prevent possible races. */
1608 	IEEE80211_LOCK(ic);	/* XXX */
1609 	RTWN_LOCK(sc);
1610 	memcpy(wmep, ic->ic_wme.wme_chanParams.cap_wmeParams,
1611 	    sizeof(sc->cap_wmeParams));
1612 	RTWN_UNLOCK(sc);
1613 	IEEE80211_UNLOCK(ic);
1614 
1615 	acm = 0;
1616 	slottime = IEEE80211_GET_SLOTTIME(ic);
1617 
1618 	RTWN_LOCK(sc);
1619 	for (ac = WME_AC_BE; ac < WME_NUM_AC; ac++) {
1620 		/* AIFS[AC] = AIFSN[AC] * aSlotTime + aSIFSTime. */
1621 		aifs = wmep[ac].wmep_aifsn * slottime +
1622 		    (IEEE80211_IS_CHAN_5GHZ(c) ?
1623 			IEEE80211_DUR_OFDM_SIFS : IEEE80211_DUR_SIFS);
1624 		rtwn_write_4(sc, wme2reg[ac],
1625 		    SM(R92C_EDCA_PARAM_TXOP, wmep[ac].wmep_txopLimit) |
1626 		    SM(R92C_EDCA_PARAM_ECWMIN, wmep[ac].wmep_logcwmin) |
1627 		    SM(R92C_EDCA_PARAM_ECWMAX, wmep[ac].wmep_logcwmax) |
1628 		    SM(R92C_EDCA_PARAM_AIFS, aifs));
1629 		if (ac != WME_AC_BE)
1630 			acm |= wmep[ac].wmep_acm << ac;
1631 	}
1632 
1633 	if (acm != 0)
1634 		acm |= R92C_ACMHWCTRL_EN;
1635 	rtwn_setbits_1(sc, R92C_ACMHWCTRL, R92C_ACMHWCTRL_ACM_MASK, acm);
1636 	RTWN_UNLOCK(sc);
1637 
1638 	return 0;
1639 }
1640 
1641 static void
1642 rtwn_update_slot(struct ieee80211com *ic)
1643 {
1644 	rtwn_cmd_sleepable(ic->ic_softc, NULL, 0, rtwn_update_slot_cb);
1645 }
1646 
1647 static void
1648 rtwn_update_slot_cb(struct rtwn_softc *sc, union sec_param *data)
1649 {
1650 	struct ieee80211com *ic = &sc->sc_ic;
1651 	uint8_t slottime;
1652 
1653 	slottime = IEEE80211_GET_SLOTTIME(ic);
1654 
1655 	RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s: setting slot time to %uus\n",
1656 	    __func__, slottime);
1657 
1658 	rtwn_write_1(sc, R92C_SLOT, slottime);
1659 	rtwn_update_aifs(sc, slottime);
1660 }
1661 
1662 static void
1663 rtwn_update_aifs(struct rtwn_softc *sc, uint8_t slottime)
1664 {
1665 	struct ieee80211_channel *c = sc->sc_ic.ic_curchan;
1666 	const struct wmeParams *wmep = sc->cap_wmeParams;
1667 	uint8_t aifs, ac;
1668 
1669 	for (ac = WME_AC_BE; ac < WME_NUM_AC; ac++) {
1670 		/* AIFS[AC] = AIFSN[AC] * aSlotTime + aSIFSTime. */
1671 		aifs = wmep[ac].wmep_aifsn * slottime +
1672 		    (IEEE80211_IS_CHAN_5GHZ(c) ?
1673 			IEEE80211_DUR_OFDM_SIFS : IEEE80211_DUR_SIFS);
1674 		rtwn_write_1(sc, wme2reg[ac], aifs);
1675 	}
1676 }
1677 
1678 static void
1679 rtwn_update_promisc(struct ieee80211com *ic)
1680 {
1681 	struct rtwn_softc *sc = ic->ic_softc;
1682 
1683 	RTWN_LOCK(sc);
1684 	if (sc->sc_flags & RTWN_RUNNING)
1685 		rtwn_set_promisc(sc);
1686 	RTWN_UNLOCK(sc);
1687 }
1688 
1689 static void
1690 rtwn_update_mcast(struct ieee80211com *ic)
1691 {
1692 	struct rtwn_softc *sc = ic->ic_softc;
1693 
1694 	RTWN_LOCK(sc);
1695 	if (sc->sc_flags & RTWN_RUNNING)
1696 		rtwn_set_multi(sc);
1697 	RTWN_UNLOCK(sc);
1698 }
1699 
1700 static int
1701 rtwn_set_bssid(struct rtwn_softc *sc, const uint8_t *bssid, int id)
1702 {
1703 	int error;
1704 
1705 	error = rtwn_write_4(sc, R92C_BSSID(id), le32dec(&bssid[0]));
1706 	if (error != 0)
1707 		return (error);
1708 	error = rtwn_write_2(sc, R92C_BSSID(id) + 4, le16dec(&bssid[4]));
1709 
1710 	return (error);
1711 }
1712 
1713 static int
1714 rtwn_set_macaddr(struct rtwn_softc *sc, const uint8_t *addr, int id)
1715 {
1716 	int error;
1717 
1718 	error = rtwn_write_4(sc, R92C_MACID(id), le32dec(&addr[0]));
1719 	if (error != 0)
1720 		return (error);
1721 	error = rtwn_write_2(sc, R92C_MACID(id) + 4, le16dec(&addr[4]));
1722 
1723 	return (error);
1724 }
1725 
1726 static struct ieee80211_node *
1727 rtwn_node_alloc(struct ieee80211vap *vap,
1728     const uint8_t mac[IEEE80211_ADDR_LEN])
1729 {
1730 	struct rtwn_node *un;
1731 
1732 	un = malloc(sizeof (struct rtwn_node), M_80211_NODE,
1733 	    M_NOWAIT | M_ZERO);
1734 
1735 	if (un == NULL)
1736 		return NULL;
1737 
1738 	un->id = RTWN_MACID_UNDEFINED;
1739 	un->avg_pwdb = -1;
1740 
1741 	return &un->ni;
1742 }
1743 
1744 static void
1745 rtwn_newassoc(struct ieee80211_node *ni, int isnew)
1746 {
1747 	struct rtwn_softc *sc = ni->ni_ic->ic_softc;
1748 	struct rtwn_node *un = RTWN_NODE(ni);
1749 	int id;
1750 
1751 	if (!isnew)
1752 		return;
1753 
1754 	RTWN_NT_LOCK(sc);
1755 	for (id = 0; id <= sc->macid_limit; id++) {
1756 		if (id != RTWN_MACID_BC && sc->node_list[id] == NULL) {
1757 			un->id = id;
1758 			sc->node_list[id] = ni;
1759 			break;
1760 		}
1761 	}
1762 	RTWN_NT_UNLOCK(sc);
1763 
1764 	if (id > sc->macid_limit) {
1765 		device_printf(sc->sc_dev, "%s: node table is full\n",
1766 		    __func__);
1767 		return;
1768 	}
1769 
1770 #ifndef RTWN_WITHOUT_UCODE
1771 	/* Notify firmware. */
1772 	id |= RTWN_MACID_VALID;
1773 	rtwn_cmd_sleepable(sc, &id, sizeof(id), rtwn_set_media_status);
1774 #endif
1775 }
1776 
1777 static void
1778 rtwn_node_free(struct ieee80211_node *ni)
1779 {
1780 	struct rtwn_softc *sc = ni->ni_ic->ic_softc;
1781 	struct rtwn_node *un = RTWN_NODE(ni);
1782 
1783 	RTWN_NT_LOCK(sc);
1784 	if (un->id != RTWN_MACID_UNDEFINED) {
1785 		sc->node_list[un->id] = NULL;
1786 #ifndef RTWN_WITHOUT_UCODE
1787 		rtwn_cmd_sleepable(sc, &un->id, sizeof(un->id),
1788 		    rtwn_set_media_status);
1789 #endif
1790 	}
1791 	RTWN_NT_UNLOCK(sc);
1792 
1793 	sc->sc_node_free(ni);
1794 }
1795 
1796 static void
1797 rtwn_init_beacon_reg(struct rtwn_softc *sc)
1798 {
1799 	rtwn_write_1(sc, R92C_BCN_CTRL(0), R92C_BCN_CTRL_DIS_TSF_UDT0);
1800 	rtwn_write_1(sc, R92C_BCN_CTRL(1), R92C_BCN_CTRL_DIS_TSF_UDT0);
1801 	rtwn_write_2(sc, R92C_TBTT_PROHIBIT, 0x6404);
1802 	rtwn_write_1(sc, R92C_DRVERLYINT, 0x05);
1803 	rtwn_write_1(sc, R92C_BCNDMATIM, 0x02);
1804 	rtwn_write_2(sc, R92C_BCNTCFG, 0x660f);
1805 }
1806 
1807 static int
1808 rtwn_init(struct rtwn_softc *sc)
1809 {
1810 	struct ieee80211com *ic = &sc->sc_ic;
1811 	int i, error;
1812 
1813 	RTWN_LOCK(sc);
1814 	if (sc->sc_flags & RTWN_RUNNING) {
1815 		RTWN_UNLOCK(sc);
1816 		return (0);
1817 	}
1818 	sc->sc_flags |= RTWN_STARTED;
1819 
1820 	/* Power on adapter. */
1821 	error = rtwn_power_on(sc);
1822 	if (error != 0)
1823 		goto fail;
1824 
1825 #ifndef RTWN_WITHOUT_UCODE
1826 	/* Load 8051 microcode. */
1827 	error = rtwn_load_firmware(sc);
1828 	if (error == 0)
1829 		sc->sc_flags |= RTWN_FW_LOADED;
1830 
1831 	/* Init firmware commands ring. */
1832 	sc->fwcur = 0;
1833 #endif
1834 
1835 	/* Initialize MAC block. */
1836 	error = rtwn_mac_init(sc);
1837 	if (error != 0) {
1838 		device_printf(sc->sc_dev,
1839 		    "%s: error while initializing MAC block\n", __func__);
1840 		goto fail;
1841 	}
1842 
1843 	/* Initialize DMA. */
1844 	error = rtwn_dma_init(sc);
1845 	if (error != 0)
1846 		goto fail;
1847 
1848 	/* Drop incorrect TX (USB). */
1849 	rtwn_drop_incorrect_tx(sc);
1850 
1851 	/* Set info size in Rx descriptors (in 64-bit words). */
1852 	rtwn_write_1(sc, R92C_RX_DRVINFO_SZ, R92C_RX_DRVINFO_SZ_DEF);
1853 
1854 	/* Init interrupts. */
1855 	rtwn_init_intr(sc);
1856 
1857 	for (i = 0; i < nitems(sc->vaps); i++) {
1858 		struct rtwn_vap *uvp = sc->vaps[i];
1859 
1860 		/* Set initial network type. */
1861 		rtwn_set_mode(sc, R92C_MSR_NOLINK, i);
1862 
1863 		if (uvp == NULL)
1864 			continue;
1865 
1866 		/* Set MAC address. */
1867 		error = rtwn_set_macaddr(sc, uvp->vap.iv_myaddr, uvp->id);
1868 		if (error != 0)
1869 			goto fail;
1870 	}
1871 
1872 	/* Initialize Rx filter. */
1873 	rtwn_rxfilter_init(sc);
1874 
1875 	/* Set short/long retry limits. */
1876 	rtwn_write_2(sc, R92C_RL,
1877 	    SM(R92C_RL_SRL, 0x30) | SM(R92C_RL_LRL, 0x30));
1878 
1879 	/* Initialize EDCA parameters. */
1880 	rtwn_init_edca(sc);
1881 
1882 	rtwn_setbits_1(sc, R92C_FWHW_TXQ_CTRL, 0,
1883 	    R92C_FWHW_TXQ_CTRL_AMPDU_RTY_NEW);
1884 	/* Set ACK timeout. */
1885 	rtwn_write_1(sc, R92C_ACKTO, sc->ackto);
1886 
1887 	/* Setup aggregation. */
1888 	/* Tx aggregation. */
1889 	rtwn_init_tx_agg(sc);
1890 	rtwn_init_rx_agg(sc);
1891 
1892 	/* Initialize beacon parameters. */
1893 	rtwn_init_beacon_reg(sc);
1894 
1895 	/* Init A-MPDU parameters. */
1896 	rtwn_init_ampdu(sc);
1897 
1898 	/* Init MACTXEN / MACRXEN after setting RxFF boundary. */
1899 	rtwn_setbits_1(sc, R92C_CR, 0, R92C_CR_MACTXEN | R92C_CR_MACRXEN);
1900 
1901 	/* Initialize BB/RF blocks. */
1902 	rtwn_init_bb(sc);
1903 	rtwn_init_rf(sc);
1904 
1905 	/* Initialize wireless band. */
1906 	rtwn_set_chan(sc, ic->ic_curchan);
1907 
1908 	/* Clear per-station keys table. */
1909 	rtwn_init_cam(sc);
1910 
1911 	/* Enable decryption / encryption. */
1912 	rtwn_init_seccfg(sc);
1913 
1914 	/* Install static keys (if any). */
1915 	for (i = 0; i < nitems(sc->vaps); i++) {
1916 		if (sc->vaps[i] != NULL) {
1917 			error = rtwn_init_static_keys(sc, sc->vaps[i]);
1918 			if (error != 0)
1919 				goto fail;
1920 		}
1921 	}
1922 
1923 	/* Initialize antenna selection. */
1924 	rtwn_init_antsel(sc);
1925 
1926 	/* Enable hardware sequence numbering. */
1927 	rtwn_write_1(sc, R92C_HWSEQ_CTRL, R92C_TX_QUEUE_ALL);
1928 
1929 	/* Disable BAR. */
1930 	rtwn_write_4(sc, R92C_BAR_MODE_CTRL, 0x0201ffff);
1931 
1932 	/* NAV limit. */
1933 	rtwn_write_1(sc, R92C_NAV_UPPER, 0);
1934 
1935 	/* Initialize GPIO setting. */
1936 	rtwn_setbits_1(sc, R92C_GPIO_MUXCFG, R92C_GPIO_MUXCFG_ENBT, 0);
1937 
1938 	/* Initialize MRR. */
1939 	rtwn_mrr_init(sc);
1940 
1941 	/* Device-specific post initialization. */
1942 	rtwn_post_init(sc);
1943 
1944 	rtwn_start_xfers(sc);
1945 
1946 #ifndef D4054
1947 	callout_reset(&sc->sc_watchdog_to, hz, rtwn_watchdog, sc);
1948 #endif
1949 
1950 	sc->sc_flags |= RTWN_RUNNING;
1951 fail:
1952 	RTWN_UNLOCK(sc);
1953 
1954 	return (error);
1955 }
1956 
1957 static void
1958 rtwn_stop(struct rtwn_softc *sc)
1959 {
1960 
1961 	RTWN_LOCK(sc);
1962 	if (!(sc->sc_flags & RTWN_STARTED)) {
1963 		RTWN_UNLOCK(sc);
1964 		return;
1965 	}
1966 
1967 #ifndef D4054
1968 	callout_stop(&sc->sc_watchdog_to);
1969 	sc->sc_tx_timer = 0;
1970 #endif
1971 	sc->sc_flags &= ~(RTWN_STARTED | RTWN_RUNNING | RTWN_FW_LOADED);
1972 	sc->sc_flags &= ~RTWN_TEMP_MEASURED;
1973 	sc->fwver = 0;
1974 	sc->thcal_temp = 0;
1975 	sc->cur_bcnq_id = RTWN_VAP_ID_INVALID;
1976 
1977 #ifdef D4054
1978 	ieee80211_tx_watchdog_stop(&sc->sc_ic);
1979 #endif
1980 
1981 	rtwn_abort_xfers(sc);
1982 	rtwn_drain_mbufq(sc);
1983 	rtwn_power_off(sc);
1984 	rtwn_reset_lists(sc, NULL);
1985 	RTWN_UNLOCK(sc);
1986 }
1987 
1988 MODULE_VERSION(rtwn, 2);
1989 MODULE_DEPEND(rtwn, wlan, 1, 1, 1);
1990 #ifndef RTWN_WITHOUT_UCODE
1991 MODULE_DEPEND(rtwn, firmware, 1, 1, 1);
1992 #endif
1993