]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/rtwn/if_rtwn.c
MFV r309299:
[FreeBSD/FreeBSD.git] / sys / dev / rtwn / if_rtwn.c
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         /* Cancel any unfinished Tx. */
632         rtwn_reset_lists(sc, vap);
633         if (uvp->bcn_mbuf != NULL)
634                 m_freem(uvp->bcn_mbuf);
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                 rtwn_beacon_unload(sc, uvp->id);
827                 m_freem(uvp->bcn_mbuf);
828         }
829
830         m->m_pkthdr.len = m->m_len = required_size - sc->txdesc_len;
831         uvp->bcn_mbuf = m;
832
833         error = rtwn_tx_beacon_check(sc, uvp);
834         if (error != 0) {
835                 RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
836                     "%s: frame was not recognized!\n", __func__);
837                 goto fail;
838         }
839
840         /* Setup addresses in firmware. */
841         null_data = howmany(bcn_size, sc->page_size);
842         error = rtwn_set_rsvd_page(sc, 0, null_data, null_data + 1);
843         if (error != 0) {
844                 device_printf(sc->sc_dev,
845                     "%s: CMD_RSVD_PAGE was not sent, error %d\n",
846                     __func__, error);
847                 goto fail;
848         }
849
850 fail:
851         /* Re-enable beacon detection. */
852         rtwn_setbits_1_shift(sc, R92C_FWHW_TXQ_CTRL,
853             0, R92C_FWHW_TXQ_CTRL_REAL_BEACON, 2);
854         rtwn_setbits_1_shift(sc, R92C_CR, R92C_CR_ENSWBCN, 0, 1);
855
856         /* Restore beacon (if present). */
857         if (sc->bcn_vaps > 0 && sc->vaps[!uvp->id] != NULL) {
858                 struct rtwn_vap *uvp2 = sc->vaps[!uvp->id];
859
860                 if (uvp2->curr_mode != R92C_MSR_NOLINK)
861                         error = rtwn_tx_beacon_check(sc, uvp2);
862         }
863
864         return (error);
865 }
866
867 static void
868 rtwn_pwrmode_init(void *arg)
869 {
870         struct rtwn_softc *sc = arg;
871
872         rtwn_cmd_sleepable(sc, NULL, 0, rtwn_set_pwrmode_cb);
873 }
874
875 static void
876 rtwn_set_pwrmode_cb(struct rtwn_softc *sc, union sec_param *data)
877 {
878         struct ieee80211vap *vap = &sc->vaps[0]->vap;
879
880         if (vap != NULL)
881                 rtwn_set_pwrmode(sc, vap, 1);
882 }
883 #endif
884
885 static void
886 rtwn_tsf_sync_adhoc(void *arg)
887 {
888         struct ieee80211vap *vap = arg;
889         struct ieee80211com *ic = vap->iv_ic;
890         struct rtwn_vap *uvp = RTWN_VAP(vap);
891
892         if (uvp->curr_mode != R92C_MSR_NOLINK) {
893                 /* Do it in process context. */
894                 ieee80211_runtask(ic, &uvp->tsf_sync_adhoc_task);
895         }
896 }
897
898 /*
899  * Workaround for TSF synchronization:
900  * when BSSID filter in IBSS mode is not set
901  * (and TSF synchronization is enabled), then any beacon may update it.
902  * This routine synchronizes it when BSSID matching is enabled (IBSS merge
903  * is not possible during this period).
904  *
905  * NOTE: there is no race with rtwn_newstate(), since it uses the same
906  * taskqueue.
907  */
908 static void
909 rtwn_tsf_sync_adhoc_task(void *arg, int pending)
910 {
911         struct ieee80211vap *vap = arg;
912         struct rtwn_vap *uvp = RTWN_VAP(vap);
913         struct rtwn_softc *sc = vap->iv_ic->ic_softc;
914         struct ieee80211_node *ni;
915
916         RTWN_LOCK(sc);
917         ni = ieee80211_ref_node(vap->iv_bss);
918
919         /* Accept beacons with the same BSSID. */
920         rtwn_set_rx_bssid_all(sc, 0);
921
922         /* Deny RCR updates. */
923         sc->sc_flags |= RTWN_RCR_LOCKED;
924
925         /* Enable synchronization. */
926         rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
927             R92C_BCN_CTRL_DIS_TSF_UDT0, 0);
928
929         /* Synchronize. */
930         rtwn_delay(sc, ni->ni_intval * 5 * 1000);
931
932         /* Disable synchronization. */
933         rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
934             0, R92C_BCN_CTRL_DIS_TSF_UDT0);
935
936         /* Accept all beacons. */
937         sc->sc_flags &= ~RTWN_RCR_LOCKED;
938         rtwn_set_rx_bssid_all(sc, 1);
939
940         /* Schedule next TSF synchronization. */
941         callout_reset(&uvp->tsf_sync_adhoc, 60*hz, rtwn_tsf_sync_adhoc, vap);
942
943         ieee80211_free_node(ni);
944         RTWN_UNLOCK(sc);
945 }
946
947 static void
948 rtwn_tsf_sync_enable(struct rtwn_softc *sc, struct ieee80211vap *vap)
949 {
950         struct ieee80211com *ic = &sc->sc_ic;
951         struct rtwn_vap *uvp = RTWN_VAP(vap);
952
953         /* Reset TSF. */
954         rtwn_write_1(sc, R92C_DUAL_TSF_RST, R92C_DUAL_TSF_RESET(uvp->id));
955
956         switch (vap->iv_opmode) {
957         case IEEE80211_M_STA:
958                 /* Enable TSF synchronization. */
959                 rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
960                     R92C_BCN_CTRL_DIS_TSF_UDT0, 0);
961                 break;
962         case IEEE80211_M_IBSS:
963                 ieee80211_runtask(ic, &uvp->tsf_sync_adhoc_task);
964                 /* FALLTHROUGH */
965         case IEEE80211_M_HOSTAP:
966                 /* Enable beaconing. */
967                 rtwn_beacon_enable(sc, uvp->id, 1);
968                 break;
969         default:
970                 device_printf(sc->sc_dev, "undefined opmode %d\n",
971                     vap->iv_opmode);
972                 return;
973         }
974 }
975
976 static void
977 rtwn_set_ack_preamble(struct rtwn_softc *sc)
978 {
979         struct ieee80211com *ic = &sc->sc_ic;
980         uint32_t reg;
981
982         reg = rtwn_read_4(sc, R92C_WMAC_TRXPTCL_CTL);
983         if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
984                 reg |= R92C_WMAC_TRXPTCL_SHPRE;
985         else
986                 reg &= ~R92C_WMAC_TRXPTCL_SHPRE;
987         rtwn_write_4(sc, R92C_WMAC_TRXPTCL_CTL, reg);
988 }
989
990 static void
991 rtwn_set_mode(struct rtwn_softc *sc, uint8_t mode, int id)
992 {
993
994         rtwn_setbits_1(sc, R92C_MSR, R92C_MSR_MASK << id * 2, mode << id * 2);
995         if (sc->vaps[id] != NULL)
996                 sc->vaps[id]->curr_mode = mode;
997 }
998
999 static int
1000 rtwn_monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate,
1001     int arg)
1002 {
1003         struct ieee80211com *ic = vap->iv_ic;
1004         struct rtwn_softc *sc = ic->ic_softc;
1005         struct rtwn_vap *uvp = RTWN_VAP(vap);
1006
1007         RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s -> %s\n",
1008             ieee80211_state_name[vap->iv_state],
1009             ieee80211_state_name[nstate]);
1010
1011         if (vap->iv_state != nstate) {
1012                 IEEE80211_UNLOCK(ic);
1013                 RTWN_LOCK(sc);
1014
1015                 switch (nstate) {
1016                 case IEEE80211_S_INIT:
1017                         sc->vaps_running--;
1018                         sc->monvaps_running--;
1019
1020                         if (sc->vaps_running == 0) {
1021                                 /* Turn link LED off. */
1022                                 rtwn_set_led(sc, RTWN_LED_LINK, 0);
1023                         }
1024                         break;
1025                 case IEEE80211_S_RUN:
1026                         sc->vaps_running++;
1027                         sc->monvaps_running++;
1028
1029                         if (sc->vaps_running == 1) {
1030                                 /* Turn link LED on. */
1031                                 rtwn_set_led(sc, RTWN_LED_LINK, 1);
1032                         }
1033                         break;
1034                 default:
1035                         /* NOTREACHED */
1036                         break;
1037                 }
1038
1039                 RTWN_UNLOCK(sc);
1040                 IEEE80211_LOCK(ic);
1041         }
1042
1043         return (uvp->newstate(vap, nstate, arg));
1044 }
1045
1046 static int
1047 rtwn_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1048 {
1049         struct rtwn_vap *uvp = RTWN_VAP(vap);
1050         struct ieee80211com *ic = vap->iv_ic;
1051         struct rtwn_softc *sc = ic->ic_softc;
1052         enum ieee80211_state ostate;
1053         int error, early_newstate;
1054
1055         ostate = vap->iv_state;
1056         RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s -> %s\n",
1057             ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
1058
1059         if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC &&
1060             ostate == IEEE80211_S_INIT && nstate == IEEE80211_S_RUN) {
1061                 /* need to call iv_newstate() firstly */
1062                 error = uvp->newstate(vap, nstate, arg);
1063                 if (error != 0)
1064                         return (error);
1065
1066                 early_newstate = 1;
1067         } else
1068                 early_newstate = 0;
1069
1070         IEEE80211_UNLOCK(ic);
1071         RTWN_LOCK(sc);
1072         if (ostate == IEEE80211_S_RUN) {
1073                 sc->vaps_running--;
1074
1075                 /* Set media status to 'No Link'. */
1076                 rtwn_set_mode(sc, R92C_MSR_NOLINK, uvp->id);
1077
1078                 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1079                         /* Stop periodical TSF synchronization. */
1080                         callout_stop(&uvp->tsf_sync_adhoc);
1081                 }
1082
1083                 /* Disable TSF synchronization / beaconing. */
1084                 rtwn_beacon_enable(sc, uvp->id, 0);
1085                 rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
1086                     0, R92C_BCN_CTRL_DIS_TSF_UDT0);
1087
1088                 /* NB: monitor mode vaps are using port 0. */
1089                 if (uvp->id != 0 || sc->monvaps_running == 0) {
1090                         /* Reset TSF. */
1091                         rtwn_write_1(sc, R92C_DUAL_TSF_RST,
1092                             R92C_DUAL_TSF_RESET(uvp->id));
1093                 }
1094
1095 #ifndef RTWN_WITHOUT_UCODE
1096                 if ((ic->ic_caps & IEEE80211_C_PMGT) != 0 && uvp->id == 0) {
1097                         /* Disable power management. */
1098                         callout_stop(&sc->sc_pwrmode_init);
1099                         rtwn_set_pwrmode(sc, vap, 0);
1100                 }
1101 #endif
1102                 if (sc->vaps_running - sc->monvaps_running > 0) {
1103                         /* Recalculate basic rates bitmap. */
1104                         rtwn_calc_basicrates(sc);
1105                 }
1106
1107                 if (sc->vaps_running == sc->monvaps_running) {
1108                         /* Stop calibration. */
1109                         callout_stop(&sc->sc_calib_to);
1110
1111                         /* Stop Rx of data frames. */
1112                         rtwn_write_2(sc, R92C_RXFLTMAP2, 0);
1113
1114                         /* Reset EDCA parameters. */
1115                         rtwn_write_4(sc, R92C_EDCA_VO_PARAM, 0x002f3217);
1116                         rtwn_write_4(sc, R92C_EDCA_VI_PARAM, 0x005e4317);
1117                         rtwn_write_4(sc, R92C_EDCA_BE_PARAM, 0x00105320);
1118                         rtwn_write_4(sc, R92C_EDCA_BK_PARAM, 0x0000a444);
1119
1120                         if (sc->vaps_running == 0) {
1121                                 /* Turn link LED off. */
1122                                 rtwn_set_led(sc, RTWN_LED_LINK, 0);
1123                         }
1124                 }
1125         }
1126
1127         error = 0;
1128         switch (nstate) {
1129         case IEEE80211_S_SCAN:
1130                 /* Pause AC Tx queues. */
1131                 if (sc->vaps_running == 0)
1132                         rtwn_setbits_1(sc, R92C_TXPAUSE, 0, R92C_TX_QUEUE_AC);
1133                 break;
1134         case IEEE80211_S_RUN:
1135                 error = rtwn_run(sc, vap);
1136                 if (error != 0) {
1137                         device_printf(sc->sc_dev,
1138                             "%s: could not move to RUN state\n", __func__);
1139                         break;
1140                 }
1141
1142                 sc->vaps_running++;
1143                 break;
1144         default:
1145                 break;
1146         }
1147
1148         RTWN_UNLOCK(sc);
1149         IEEE80211_LOCK(ic);
1150         if (error != 0)
1151                 return (error);
1152
1153         return (early_newstate ? 0 : uvp->newstate(vap, nstate, arg));
1154 }
1155
1156 static void
1157 rtwn_calc_basicrates(struct rtwn_softc *sc)
1158 {
1159         struct ieee80211com *ic = &sc->sc_ic;
1160         uint32_t basicrates;
1161         int i;
1162
1163         RTWN_ASSERT_LOCKED(sc);
1164
1165         if (ic->ic_flags & IEEE80211_F_SCAN)
1166                 return;         /* will be done by rtwn_scan_end(). */
1167
1168         basicrates = 0;
1169         for (i = 0; i < nitems(sc->vaps); i++) {
1170                 struct rtwn_vap *rvp;
1171                 struct ieee80211vap *vap;
1172                 struct ieee80211_node *ni;
1173                 uint32_t rates;
1174
1175                 rvp = sc->vaps[i];
1176                 if (rvp == NULL || rvp->curr_mode == R92C_MSR_NOLINK)
1177                         continue;
1178
1179                 vap = &rvp->vap;
1180                 if (vap->iv_bss == NULL)
1181                         continue;
1182
1183                 ni = ieee80211_ref_node(vap->iv_bss);
1184                 rtwn_get_rates(sc, &ni->ni_rates, NULL, &rates, NULL, 1);
1185                 basicrates |= rates;
1186                 ieee80211_free_node(ni);
1187         }
1188
1189         if (basicrates == 0)
1190                 return;
1191
1192         /* XXX initial RTS rate? */
1193         rtwn_set_basicrates(sc, basicrates);
1194 }
1195
1196 static int
1197 rtwn_run(struct rtwn_softc *sc, struct ieee80211vap *vap)
1198 {
1199         struct ieee80211com *ic = vap->iv_ic;
1200         struct rtwn_vap *uvp = RTWN_VAP(vap);
1201         struct ieee80211_node *ni;
1202         uint8_t mode;
1203         int error;
1204
1205         RTWN_ASSERT_LOCKED(sc);
1206
1207         error = 0;
1208         ni = ieee80211_ref_node(vap->iv_bss);
1209
1210         if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
1211             ni->ni_chan == IEEE80211_CHAN_ANYC) {
1212                 error = EINVAL;
1213                 goto fail;
1214         }
1215
1216         switch (vap->iv_opmode) {
1217         case IEEE80211_M_STA:
1218                 mode = R92C_MSR_INFRA;
1219                 break;
1220         case IEEE80211_M_IBSS:
1221                 mode = R92C_MSR_ADHOC;
1222                 break;
1223         case IEEE80211_M_HOSTAP:
1224                 mode = R92C_MSR_AP;
1225                 break;
1226         default:
1227                 KASSERT(0, ("undefined opmode %d\n", vap->iv_opmode));
1228                 error = EINVAL;
1229                 goto fail;
1230         }
1231
1232         /* Set media status to 'Associated'. */
1233         rtwn_set_mode(sc, mode, uvp->id);
1234
1235         /* Set AssocID. */
1236         /* XXX multi-vap? */
1237         rtwn_write_2(sc, R92C_BCN_PSR_RPT,
1238             0xc000 | IEEE80211_NODE_AID(ni));
1239
1240         /* Set BSSID. */
1241         rtwn_set_bssid(sc, ni->ni_bssid, uvp->id);
1242
1243         /* Set beacon interval. */
1244         rtwn_write_2(sc, R92C_BCN_INTERVAL(uvp->id), ni->ni_intval);
1245
1246         if (sc->vaps_running == sc->monvaps_running) {
1247                 /* Enable Rx of data frames. */
1248                 rtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff);
1249
1250                 /* Flush all AC queues. */
1251                 rtwn_write_1(sc, R92C_TXPAUSE, 0);
1252         }
1253
1254 #ifndef RTWN_WITHOUT_UCODE
1255         /* Upload (QoS) Null Data frame to firmware. */
1256         /* Note: do this for port 0 only. */
1257         if ((ic->ic_caps & IEEE80211_C_PMGT) != 0 &&
1258             vap->iv_opmode == IEEE80211_M_STA && uvp->id == 0) {
1259                 error = rtwn_tx_fwpkt_check(sc, vap);
1260                 if (error != 0)
1261                         goto fail;
1262
1263                 /* Setup power management. */
1264                 /*
1265                  * NB: it will be enabled immediately - delay it,
1266                  * so 4-Way handshake will not be interrupted.
1267                  */
1268                 callout_reset(&sc->sc_pwrmode_init, 5*hz,
1269                     rtwn_pwrmode_init, sc);
1270         }
1271 #endif
1272
1273         /* Enable TSF synchronization. */
1274         rtwn_tsf_sync_enable(sc, vap);
1275
1276         if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1277             vap->iv_opmode == IEEE80211_M_IBSS) {
1278                 error = rtwn_setup_beacon(sc, ni);
1279                 if (error != 0) {
1280                         device_printf(sc->sc_dev,
1281                             "unable to push beacon into the chip, "
1282                             "error %d\n", error);
1283                         goto fail;
1284                 }
1285         }
1286
1287         /* Set ACK preamble type. */
1288         rtwn_set_ack_preamble(sc);
1289
1290         /* Set basic rates mask. */
1291         rtwn_calc_basicrates(sc);
1292
1293 #ifdef RTWN_TODO
1294         rtwn_write_1(sc, R92C_SIFS_CCK + 1, 10);
1295         rtwn_write_1(sc, R92C_SIFS_OFDM + 1, 10);
1296         rtwn_write_1(sc, R92C_SPEC_SIFS + 1, 10);
1297         rtwn_write_1(sc, R92C_MAC_SPEC_SIFS + 1, 10);
1298         rtwn_write_1(sc, R92C_R2T_SIFS + 1, 10);
1299         rtwn_write_1(sc, R92C_T2T_SIFS + 1, 10);
1300 #endif
1301
1302         if (sc->vaps_running == sc->monvaps_running) {
1303                 /* Reset temperature calibration state machine. */
1304                 sc->sc_flags &= ~RTWN_TEMP_MEASURED;
1305                 sc->thcal_temp = sc->thermal_meter;
1306
1307                 /* Start periodic calibration. */
1308                 callout_reset(&sc->sc_calib_to, 2*hz, rtwn_calib_to,
1309                     sc);
1310
1311                 if (sc->vaps_running == 0) {
1312                         /* Turn link LED on. */
1313                         rtwn_set_led(sc, RTWN_LED_LINK, 1);
1314                 }
1315         }
1316
1317 fail:
1318         ieee80211_free_node(ni);
1319
1320         return (error);
1321 }
1322
1323 #ifndef D4054
1324 static void
1325 rtwn_watchdog(void *arg)
1326 {
1327         struct rtwn_softc *sc = arg;
1328         struct ieee80211com *ic = &sc->sc_ic;
1329
1330         RTWN_ASSERT_LOCKED(sc);
1331
1332         KASSERT(sc->sc_flags & RTWN_RUNNING, ("not running"));
1333
1334         if (sc->sc_tx_timer != 0 && --sc->sc_tx_timer == 0) {
1335                 ic_printf(ic, "device timeout\n");
1336                 ieee80211_restart_all(ic);
1337                 return;
1338         }
1339         callout_reset(&sc->sc_watchdog_to, hz, rtwn_watchdog, sc);
1340 }
1341 #endif
1342
1343 static void
1344 rtwn_parent(struct ieee80211com *ic)
1345 {
1346         struct rtwn_softc *sc = ic->ic_softc;
1347         struct ieee80211vap *vap;
1348
1349         if (ic->ic_nrunning > 0) {
1350                 if (rtwn_init(sc) != 0) {
1351                         IEEE80211_LOCK(ic);
1352                         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1353                                 ieee80211_stop_locked(vap);
1354                         IEEE80211_UNLOCK(ic);
1355                 } else
1356                         ieee80211_start_all(ic);
1357         } else
1358                 rtwn_stop(sc);
1359 }
1360
1361
1362 static int
1363 rtwn_llt_write(struct rtwn_softc *sc, uint32_t addr, uint32_t data)
1364 {
1365         int ntries, error;
1366
1367         error = rtwn_write_4(sc, R92C_LLT_INIT,
1368             SM(R92C_LLT_INIT_OP, R92C_LLT_INIT_OP_WRITE) |
1369             SM(R92C_LLT_INIT_ADDR, addr) |
1370             SM(R92C_LLT_INIT_DATA, data));
1371         if (error != 0)
1372                 return (error);
1373         /* Wait for write operation to complete. */
1374         for (ntries = 0; ntries < 20; ntries++) {
1375                 if (MS(rtwn_read_4(sc, R92C_LLT_INIT), R92C_LLT_INIT_OP) ==
1376                     R92C_LLT_INIT_OP_NO_ACTIVE)
1377                         return (0);
1378                 rtwn_delay(sc, 10);
1379         }
1380         return (ETIMEDOUT);
1381 }
1382
1383 static int
1384 rtwn_llt_init(struct rtwn_softc *sc)
1385 {
1386         int i, error;
1387
1388         /* Reserve pages [0; page_count]. */
1389         for (i = 0; i < sc->page_count; i++) {
1390                 if ((error = rtwn_llt_write(sc, i, i + 1)) != 0)
1391                         return (error);
1392         }
1393         /* NB: 0xff indicates end-of-list. */
1394         if ((error = rtwn_llt_write(sc, i, 0xff)) != 0)
1395                 return (error);
1396         /*
1397          * Use pages [page_count + 1; pktbuf_count - 1]
1398          * as ring buffer.
1399          */
1400         for (++i; i < sc->pktbuf_count - 1; i++) {
1401                 if ((error = rtwn_llt_write(sc, i, i + 1)) != 0)
1402                         return (error);
1403         }
1404         /* Make the last page point to the beginning of the ring buffer. */
1405         error = rtwn_llt_write(sc, i, sc->page_count + 1);
1406         return (error);
1407 }
1408
1409 static int
1410 rtwn_dma_init(struct rtwn_softc *sc)
1411 {
1412 #define RTWN_CHK(res) do {      \
1413         if (res != 0)           \
1414                 return (EIO);   \
1415 } while(0)
1416         uint16_t reg;
1417         uint8_t tx_boundary;
1418         int error;
1419
1420         /* Initialize LLT table. */
1421         error = rtwn_llt_init(sc);
1422         if (error != 0)
1423                 return (error);
1424
1425         /* Set the number of pages for each queue. */
1426         RTWN_DPRINTF(sc, RTWN_DEBUG_RESET,
1427             "%s: pages per queue: high %d, normal %d, low %d, public %d\n",
1428             __func__, sc->nhqpages, sc->nnqpages, sc->nlqpages,
1429             sc->npubqpages);
1430
1431         RTWN_CHK(rtwn_write_1(sc, R92C_RQPN_NPQ, sc->nnqpages));
1432         RTWN_CHK(rtwn_write_4(sc, R92C_RQPN,
1433             /* Set number of pages for public queue. */
1434             SM(R92C_RQPN_PUBQ, sc->npubqpages) |
1435             /* Set number of pages for high priority queue. */
1436             SM(R92C_RQPN_HPQ, sc->nhqpages) |
1437             /* Set number of pages for low priority queue. */
1438             SM(R92C_RQPN_LPQ, sc->nlqpages) |
1439             /* Load values. */
1440             R92C_RQPN_LD));
1441
1442         /* Initialize TX buffer boundary. */
1443         KASSERT(sc->page_count < 255 && sc->page_count > 0,
1444             ("page_count is %d\n", sc->page_count));
1445         tx_boundary = sc->page_count + 1;
1446         RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, tx_boundary));
1447         RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, tx_boundary));
1448         RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD, tx_boundary));
1449         RTWN_CHK(rtwn_write_1(sc, R92C_TRXFF_BNDY, tx_boundary));
1450         RTWN_CHK(rtwn_write_1(sc, R92C_TDECTRL + 1, tx_boundary));
1451
1452         error = rtwn_init_bcnq1_boundary(sc);
1453         if (error != 0)
1454                 return (error);
1455
1456         /* Set queue to USB pipe mapping. */
1457         /* Note: PCIe devices are using some magic number here. */
1458         reg = rtwn_get_qmap(sc);
1459         RTWN_CHK(rtwn_setbits_2(sc, R92C_TRXDMA_CTRL,
1460             R92C_TRXDMA_CTRL_QMAP_M, reg));
1461
1462         /* Configure Tx/Rx DMA (PCIe). */
1463         rtwn_set_desc_addr(sc);
1464
1465         /* Set Tx/Rx transfer page boundary. */
1466         RTWN_CHK(rtwn_write_2(sc, R92C_TRXFF_BNDY + 2,
1467             sc->rx_dma_size - 1));
1468
1469         /* Set Tx/Rx transfer page size. */
1470         rtwn_set_page_size(sc);
1471
1472         return (0);
1473 }
1474
1475 static int
1476 rtwn_mac_init(struct rtwn_softc *sc)
1477 {
1478         int i, error;
1479
1480         /* Write MAC initialization values. */
1481         for (i = 0; i < sc->mac_size; i++) {
1482                 error = rtwn_write_1(sc, sc->mac_prog[i].reg,
1483                     sc->mac_prog[i].val);
1484                 if (error != 0)
1485                         return (error);
1486         }
1487
1488         return (0);
1489 }
1490
1491 static void
1492 rtwn_mrr_init(struct rtwn_softc *sc)
1493 {
1494         int i;
1495
1496         /* Drop rate index by 1 per retry. */
1497         for (i = 0; i < R92C_DARFRC_SIZE; i++) {
1498                 rtwn_write_1(sc, R92C_DARFRC + i, i + 1);
1499                 rtwn_write_1(sc, R92C_RARFRC + i, i + 1);
1500         }
1501 }
1502
1503 static void
1504 rtwn_scan_start(struct ieee80211com *ic)
1505 {
1506         struct rtwn_softc *sc = ic->ic_softc;
1507
1508         RTWN_LOCK(sc);
1509         /* Pause beaconing. */
1510         rtwn_setbits_1(sc, R92C_TXPAUSE, 0, R92C_TX_QUEUE_BCN);
1511         /* Receive beacons / probe responses from any BSSID. */
1512         if (sc->bcn_vaps == 0)
1513                 rtwn_set_rx_bssid_all(sc, 1);
1514         RTWN_UNLOCK(sc);
1515 }
1516
1517 static void
1518 rtwn_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
1519 {
1520         struct rtwn_softc *sc = ss->ss_ic->ic_softc;
1521
1522         /* Make link LED blink during scan. */
1523         RTWN_LOCK(sc);
1524         rtwn_set_led(sc, RTWN_LED_LINK, !sc->ledlink);
1525         RTWN_UNLOCK(sc);
1526
1527         sc->sc_scan_curchan(ss, maxdwell);
1528 }
1529
1530 static void
1531 rtwn_scan_end(struct ieee80211com *ic)
1532 {
1533         struct rtwn_softc *sc = ic->ic_softc;
1534
1535         RTWN_LOCK(sc);
1536         /* Restore limitations. */
1537         if (ic->ic_promisc == 0 && sc->bcn_vaps == 0)
1538                 rtwn_set_rx_bssid_all(sc, 0);
1539
1540         /* Restore LED state. */
1541         rtwn_set_led(sc, RTWN_LED_LINK, (sc->vaps_running != 0));
1542
1543         /* Restore basic rates mask. */
1544         rtwn_calc_basicrates(sc);
1545
1546         /* Resume beaconing. */
1547         rtwn_setbits_1(sc, R92C_TXPAUSE, R92C_TX_QUEUE_BCN, 0);
1548         RTWN_UNLOCK(sc);
1549 }
1550
1551 static void
1552 rtwn_getradiocaps(struct ieee80211com *ic,
1553     int maxchans, int *nchans, struct ieee80211_channel chans[])
1554 {
1555         struct rtwn_softc *sc = ic->ic_softc;
1556         uint8_t bands[IEEE80211_MODE_BYTES];
1557         int i;
1558
1559         memset(bands, 0, sizeof(bands));
1560         setbit(bands, IEEE80211_MODE_11B);
1561         setbit(bands, IEEE80211_MODE_11G);
1562         setbit(bands, IEEE80211_MODE_11NG);
1563         ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
1564             rtwn_chan_2ghz, nitems(rtwn_chan_2ghz), bands,
1565             !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40));
1566
1567         /* XXX workaround add_channel_list() limitations */
1568         setbit(bands, IEEE80211_MODE_11A);
1569         setbit(bands, IEEE80211_MODE_11NA);
1570         for (i = 0; i < nitems(sc->chan_num_5ghz); i++) {
1571                 if (sc->chan_num_5ghz[i] == 0)
1572                         continue;
1573
1574                 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
1575                     sc->chan_list_5ghz[i], sc->chan_num_5ghz[i], bands,
1576                     !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40));
1577         }
1578 }
1579
1580 static void
1581 rtwn_update_chw(struct ieee80211com *ic)
1582 {
1583 }
1584
1585 static void
1586 rtwn_set_channel(struct ieee80211com *ic)
1587 {
1588         struct rtwn_softc *sc = ic->ic_softc;
1589         struct ieee80211_channel *c = ic->ic_curchan;
1590
1591         RTWN_LOCK(sc);
1592         rtwn_set_chan(sc, c);
1593         sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
1594         sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
1595         sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
1596         sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
1597         RTWN_UNLOCK(sc);
1598 }
1599
1600 static int
1601 rtwn_wme_update(struct ieee80211com *ic)
1602 {
1603         struct ieee80211_channel *c = ic->ic_curchan;
1604         struct rtwn_softc *sc = ic->ic_softc;
1605         struct wmeParams *wmep = sc->cap_wmeParams;
1606         uint8_t aifs, acm, slottime;
1607         int ac;
1608
1609         /* Prevent possible races. */
1610         IEEE80211_LOCK(ic);     /* XXX */
1611         RTWN_LOCK(sc);
1612         memcpy(wmep, ic->ic_wme.wme_chanParams.cap_wmeParams,
1613             sizeof(sc->cap_wmeParams));
1614         RTWN_UNLOCK(sc);
1615         IEEE80211_UNLOCK(ic);
1616
1617         acm = 0;
1618         slottime = IEEE80211_GET_SLOTTIME(ic);
1619
1620         RTWN_LOCK(sc);
1621         for (ac = WME_AC_BE; ac < WME_NUM_AC; ac++) {
1622                 /* AIFS[AC] = AIFSN[AC] * aSlotTime + aSIFSTime. */
1623                 aifs = wmep[ac].wmep_aifsn * slottime +
1624                     (IEEE80211_IS_CHAN_5GHZ(c) ?
1625                         IEEE80211_DUR_OFDM_SIFS : IEEE80211_DUR_SIFS);
1626                 rtwn_write_4(sc, wme2reg[ac],
1627                     SM(R92C_EDCA_PARAM_TXOP, wmep[ac].wmep_txopLimit) |
1628                     SM(R92C_EDCA_PARAM_ECWMIN, wmep[ac].wmep_logcwmin) |
1629                     SM(R92C_EDCA_PARAM_ECWMAX, wmep[ac].wmep_logcwmax) |
1630                     SM(R92C_EDCA_PARAM_AIFS, aifs));
1631                 if (ac != WME_AC_BE)
1632                         acm |= wmep[ac].wmep_acm << ac;
1633         }
1634
1635         if (acm != 0)
1636                 acm |= R92C_ACMHWCTRL_EN;
1637         rtwn_setbits_1(sc, R92C_ACMHWCTRL, R92C_ACMHWCTRL_ACM_MASK, acm);
1638         RTWN_UNLOCK(sc);
1639
1640         return 0;
1641 }
1642
1643 static void
1644 rtwn_update_slot(struct ieee80211com *ic)
1645 {
1646         rtwn_cmd_sleepable(ic->ic_softc, NULL, 0, rtwn_update_slot_cb);
1647 }
1648
1649 static void
1650 rtwn_update_slot_cb(struct rtwn_softc *sc, union sec_param *data)
1651 {
1652         struct ieee80211com *ic = &sc->sc_ic;
1653         uint8_t slottime;
1654
1655         slottime = IEEE80211_GET_SLOTTIME(ic);
1656
1657         RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s: setting slot time to %uus\n",
1658             __func__, slottime);
1659
1660         rtwn_write_1(sc, R92C_SLOT, slottime);
1661         rtwn_update_aifs(sc, slottime);
1662 }
1663
1664 static void
1665 rtwn_update_aifs(struct rtwn_softc *sc, uint8_t slottime)
1666 {
1667         struct ieee80211_channel *c = sc->sc_ic.ic_curchan;
1668         const struct wmeParams *wmep = sc->cap_wmeParams;
1669         uint8_t aifs, ac;
1670
1671         for (ac = WME_AC_BE; ac < WME_NUM_AC; ac++) {
1672                 /* AIFS[AC] = AIFSN[AC] * aSlotTime + aSIFSTime. */
1673                 aifs = wmep[ac].wmep_aifsn * slottime +
1674                     (IEEE80211_IS_CHAN_5GHZ(c) ?
1675                         IEEE80211_DUR_OFDM_SIFS : IEEE80211_DUR_SIFS);
1676                 rtwn_write_1(sc, wme2reg[ac], aifs);
1677         }
1678 }
1679
1680 static void
1681 rtwn_update_promisc(struct ieee80211com *ic)
1682 {
1683         struct rtwn_softc *sc = ic->ic_softc;
1684
1685         RTWN_LOCK(sc);
1686         if (sc->sc_flags & RTWN_RUNNING)
1687                 rtwn_set_promisc(sc);
1688         RTWN_UNLOCK(sc);
1689 }
1690
1691 static void
1692 rtwn_update_mcast(struct ieee80211com *ic)
1693 {
1694         struct rtwn_softc *sc = ic->ic_softc;
1695
1696         RTWN_LOCK(sc);
1697         if (sc->sc_flags & RTWN_RUNNING)
1698                 rtwn_set_multi(sc);
1699         RTWN_UNLOCK(sc);
1700 }
1701
1702 static int
1703 rtwn_set_bssid(struct rtwn_softc *sc, const uint8_t *bssid, int id)
1704 {
1705         int error;
1706
1707         error = rtwn_write_4(sc, R92C_BSSID(id), le32dec(&bssid[0]));
1708         if (error != 0)
1709                 return (error);
1710         error = rtwn_write_2(sc, R92C_BSSID(id) + 4, le16dec(&bssid[4]));
1711
1712         return (error);
1713 }
1714
1715 static int
1716 rtwn_set_macaddr(struct rtwn_softc *sc, const uint8_t *addr, int id)
1717 {
1718         int error;
1719
1720         error = rtwn_write_4(sc, R92C_MACID(id), le32dec(&addr[0]));
1721         if (error != 0)
1722                 return (error);
1723         error = rtwn_write_2(sc, R92C_MACID(id) + 4, le16dec(&addr[4]));
1724
1725         return (error);
1726 }
1727
1728 static struct ieee80211_node *
1729 rtwn_node_alloc(struct ieee80211vap *vap,
1730     const uint8_t mac[IEEE80211_ADDR_LEN])
1731 {
1732         struct rtwn_node *un;
1733
1734         un = malloc(sizeof (struct rtwn_node), M_80211_NODE,
1735             M_NOWAIT | M_ZERO);
1736
1737         if (un == NULL)
1738                 return NULL;
1739
1740         un->id = RTWN_MACID_UNDEFINED;
1741         un->avg_pwdb = -1;
1742
1743         return &un->ni;
1744 }
1745
1746 static void
1747 rtwn_newassoc(struct ieee80211_node *ni, int isnew)
1748 {
1749         struct rtwn_softc *sc = ni->ni_ic->ic_softc;
1750         struct rtwn_node *un = RTWN_NODE(ni);
1751         int id;
1752
1753         if (!isnew)
1754                 return;
1755
1756         RTWN_NT_LOCK(sc);
1757         for (id = 0; id <= sc->macid_limit; id++) {
1758                 if (id != RTWN_MACID_BC && sc->node_list[id] == NULL) {
1759                         un->id = id;
1760                         sc->node_list[id] = ni;
1761                         break;
1762                 }
1763         }
1764         RTWN_NT_UNLOCK(sc);
1765
1766         if (id > sc->macid_limit) {
1767                 device_printf(sc->sc_dev, "%s: node table is full\n",
1768                     __func__);
1769                 return;
1770         }
1771
1772 #ifndef RTWN_WITHOUT_UCODE
1773         /* Notify firmware. */
1774         id |= RTWN_MACID_VALID;
1775         rtwn_cmd_sleepable(sc, &id, sizeof(id), rtwn_set_media_status);
1776 #endif
1777 }
1778
1779 static void
1780 rtwn_node_free(struct ieee80211_node *ni)
1781 {
1782         struct rtwn_softc *sc = ni->ni_ic->ic_softc;
1783         struct rtwn_node *un = RTWN_NODE(ni);
1784
1785         RTWN_NT_LOCK(sc);
1786         if (un->id != RTWN_MACID_UNDEFINED) {
1787                 sc->node_list[un->id] = NULL;
1788 #ifndef RTWN_WITHOUT_UCODE
1789                 rtwn_cmd_sleepable(sc, &un->id, sizeof(un->id),
1790                     rtwn_set_media_status);
1791 #endif
1792         }
1793         RTWN_NT_UNLOCK(sc);
1794
1795         sc->sc_node_free(ni);
1796 }
1797
1798 static void
1799 rtwn_init_beacon_reg(struct rtwn_softc *sc)
1800 {
1801         rtwn_write_1(sc, R92C_BCN_CTRL(0), R92C_BCN_CTRL_DIS_TSF_UDT0);
1802         rtwn_write_1(sc, R92C_BCN_CTRL(1), R92C_BCN_CTRL_DIS_TSF_UDT0);
1803         rtwn_write_2(sc, R92C_TBTT_PROHIBIT, 0x6404);
1804         rtwn_write_1(sc, R92C_DRVERLYINT, 0x05);
1805         rtwn_write_1(sc, R92C_BCNDMATIM, 0x02);
1806         rtwn_write_2(sc, R92C_BCNTCFG, 0x660f);
1807 }
1808
1809 static int
1810 rtwn_init(struct rtwn_softc *sc)
1811 {
1812         struct ieee80211com *ic = &sc->sc_ic;
1813         int i, error;
1814
1815         RTWN_LOCK(sc);
1816         if (sc->sc_flags & RTWN_RUNNING) {
1817                 RTWN_UNLOCK(sc);
1818                 return (0);
1819         }
1820         sc->sc_flags |= RTWN_STARTED;
1821
1822         /* Power on adapter. */
1823         error = rtwn_power_on(sc);
1824         if (error != 0)
1825                 goto fail;
1826
1827 #ifndef RTWN_WITHOUT_UCODE
1828         /* Load 8051 microcode. */
1829         error = rtwn_load_firmware(sc);
1830         if (error == 0)
1831                 sc->sc_flags |= RTWN_FW_LOADED;
1832
1833         /* Init firmware commands ring. */
1834         sc->fwcur = 0;
1835 #endif
1836
1837         /* Initialize MAC block. */
1838         error = rtwn_mac_init(sc);
1839         if (error != 0) {
1840                 device_printf(sc->sc_dev,
1841                     "%s: error while initializing MAC block\n", __func__);
1842                 goto fail;
1843         }
1844
1845         /* Initialize DMA. */
1846         error = rtwn_dma_init(sc);
1847         if (error != 0)
1848                 goto fail;
1849
1850         /* Drop incorrect TX (USB). */
1851         rtwn_drop_incorrect_tx(sc);
1852
1853         /* Set info size in Rx descriptors (in 64-bit words). */
1854         rtwn_write_1(sc, R92C_RX_DRVINFO_SZ, R92C_RX_DRVINFO_SZ_DEF);
1855
1856         /* Init interrupts. */
1857         rtwn_init_intr(sc);
1858
1859         for (i = 0; i < nitems(sc->vaps); i++) {
1860                 struct rtwn_vap *uvp = sc->vaps[i];
1861
1862                 /* Set initial network type. */
1863                 rtwn_set_mode(sc, R92C_MSR_NOLINK, i);
1864
1865                 if (uvp == NULL)
1866                         continue;
1867
1868                 /* Set MAC address. */
1869                 error = rtwn_set_macaddr(sc, uvp->vap.iv_myaddr, uvp->id);
1870                 if (error != 0)
1871                         goto fail;
1872         }
1873
1874         /* Initialize Rx filter. */
1875         rtwn_rxfilter_init(sc);
1876
1877         /* Set short/long retry limits. */
1878         rtwn_write_2(sc, R92C_RL,
1879             SM(R92C_RL_SRL, 0x30) | SM(R92C_RL_LRL, 0x30));
1880
1881         /* Initialize EDCA parameters. */
1882         rtwn_init_edca(sc);
1883
1884         rtwn_setbits_1(sc, R92C_FWHW_TXQ_CTRL, 0,
1885             R92C_FWHW_TXQ_CTRL_AMPDU_RTY_NEW);
1886         /* Set ACK timeout. */
1887         rtwn_write_1(sc, R92C_ACKTO, sc->ackto);
1888
1889         /* Setup aggregation. */
1890         /* Tx aggregation. */
1891         rtwn_init_tx_agg(sc);
1892         rtwn_init_rx_agg(sc);
1893
1894         /* Initialize beacon parameters. */
1895         rtwn_init_beacon_reg(sc);
1896
1897         /* Init A-MPDU parameters. */
1898         rtwn_init_ampdu(sc);
1899
1900         /* Init MACTXEN / MACRXEN after setting RxFF boundary. */
1901         rtwn_setbits_1(sc, R92C_CR, 0, R92C_CR_MACTXEN | R92C_CR_MACRXEN);
1902
1903         /* Initialize BB/RF blocks. */
1904         rtwn_init_bb(sc);
1905         rtwn_init_rf(sc);
1906
1907         /* Initialize wireless band. */
1908         rtwn_set_chan(sc, ic->ic_curchan);
1909
1910         /* Clear per-station keys table. */
1911         rtwn_init_cam(sc);
1912
1913         /* Enable decryption / encryption. */
1914         rtwn_init_seccfg(sc);
1915
1916         /* Install static keys (if any). */
1917         for (i = 0; i < nitems(sc->vaps); i++) {
1918                 if (sc->vaps[i] != NULL) {
1919                         error = rtwn_init_static_keys(sc, sc->vaps[i]);
1920                         if (error != 0)
1921                                 goto fail;
1922                 }
1923         }
1924
1925         /* Initialize antenna selection. */
1926         rtwn_init_antsel(sc);
1927
1928         /* Enable hardware sequence numbering. */
1929         rtwn_write_1(sc, R92C_HWSEQ_CTRL, R92C_TX_QUEUE_ALL);
1930
1931         /* Disable BAR. */
1932         rtwn_write_4(sc, R92C_BAR_MODE_CTRL, 0x0201ffff);
1933
1934         /* NAV limit. */
1935         rtwn_write_1(sc, R92C_NAV_UPPER, 0);
1936
1937         /* Initialize GPIO setting. */
1938         rtwn_setbits_1(sc, R92C_GPIO_MUXCFG, R92C_GPIO_MUXCFG_ENBT, 0);
1939
1940         /* Initialize MRR. */
1941         rtwn_mrr_init(sc);
1942
1943         /* Device-specific post initialization. */
1944         rtwn_post_init(sc);
1945
1946         rtwn_start_xfers(sc);
1947
1948 #ifndef D4054
1949         callout_reset(&sc->sc_watchdog_to, hz, rtwn_watchdog, sc);
1950 #endif
1951
1952         sc->sc_flags |= RTWN_RUNNING;
1953 fail:
1954         RTWN_UNLOCK(sc);
1955
1956         return (error);
1957 }
1958
1959 static void
1960 rtwn_stop(struct rtwn_softc *sc)
1961 {
1962
1963         RTWN_LOCK(sc);
1964         if (!(sc->sc_flags & RTWN_STARTED)) {
1965                 RTWN_UNLOCK(sc);
1966                 return;
1967         }
1968
1969 #ifndef D4054
1970         callout_stop(&sc->sc_watchdog_to);
1971         sc->sc_tx_timer = 0;
1972 #endif
1973         sc->sc_flags &= ~(RTWN_STARTED | RTWN_RUNNING | RTWN_FW_LOADED);
1974         sc->sc_flags &= ~RTWN_TEMP_MEASURED;
1975         sc->fwver = 0;
1976         sc->thcal_temp = 0;
1977         sc->cur_bcnq_id = RTWN_VAP_ID_INVALID;
1978
1979 #ifdef D4054
1980         ieee80211_tx_watchdog_stop(&sc->sc_ic);
1981 #endif
1982
1983         rtwn_abort_xfers(sc);
1984         rtwn_drain_mbufq(sc);
1985         rtwn_power_off(sc);
1986         rtwn_reset_lists(sc, NULL);
1987         RTWN_UNLOCK(sc);
1988 }
1989
1990 MODULE_VERSION(rtwn, 2);
1991 MODULE_DEPEND(rtwn, wlan, 1, 1, 1);
1992 #ifndef RTWN_WITHOUT_UCODE
1993 MODULE_DEPEND(rtwn, firmware, 1, 1, 1);
1994 #endif