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