summaryrefslogblamecommitdiffstats
path: root/freebsd/sys/dev/rtwn/if_rtwn.c
blob: 79868dc0ffad9cc56a62ac23deb2597deff03864 (plain) (tree)
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384





























                                                                                                 
                      

                       
                     




























































                                                                              

                                                                  
                          


























                                                                         


























































































































































































































































































































































































































































































































































































                                                                               
                                









                                  





                                                                   
                          





























































































































































































































































































































































































































































































































































































































































































                                                                               

































































































































































































                                                                              
                                 





                                                     

                                             


                                         
                                                                   




































































































































                                                                             
                                                            




                                                    
                                           

















                                                                       


                                                                       










                                                    

                                                               

























































































































































































                                                                          
                                                       
















                                               
#include <machine/rtems-bsd-kernel-space.h>

/*	$OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $	*/

/*-
 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * Driver for Realtek RTL8188CE-VAU/RTL8188CUS/RTL8188EU/RTL8188RU/RTL8192CU/RTL8812AU/RTL8821AU.
 */
#include <rtems/bsd/local/opt_wlan.h>

#include <sys/param.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/linker.h>
#include <sys/firmware.h>
#include <sys/kdb.h>

#include <net/bpf.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>

#include <net80211/ieee80211_var.h>
#include <net80211/ieee80211_regdomain.h>
#include <net80211/ieee80211_radiotap.h>
#include <net80211/ieee80211_ratectl.h>

#include <dev/rtwn/if_rtwnreg.h>
#include <dev/rtwn/if_rtwnvar.h>

#include <dev/rtwn/if_rtwn_beacon.h>
#include <dev/rtwn/if_rtwn_calib.h>
#include <dev/rtwn/if_rtwn_cam.h>
#include <dev/rtwn/if_rtwn_debug.h>
#include <dev/rtwn/if_rtwn_efuse.h>
#include <dev/rtwn/if_rtwn_fw.h>
#include <dev/rtwn/if_rtwn_ridx.h>
#include <dev/rtwn/if_rtwn_rx.h>
#include <dev/rtwn/if_rtwn_task.h>
#include <dev/rtwn/if_rtwn_tx.h>

#include <dev/rtwn/rtl8192c/r92c_reg.h>


static void		rtwn_radiotap_attach(struct rtwn_softc *);
static void		rtwn_vap_decrement_counters(struct rtwn_softc *,
			    enum ieee80211_opmode, int);
static void		rtwn_set_ic_opmode(struct rtwn_softc *);
static struct ieee80211vap *rtwn_vap_create(struct ieee80211com *,
			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
			    int, const uint8_t [IEEE80211_ADDR_LEN],
			    const uint8_t [IEEE80211_ADDR_LEN]);
static void		rtwn_vap_delete(struct ieee80211vap *);
static int		rtwn_read_chipid(struct rtwn_softc *);
static int		rtwn_ioctl_reset(struct ieee80211vap *, u_long);
static void		rtwn_set_media_status(struct rtwn_softc *,
			    union sec_param *);
#ifndef RTWN_WITHOUT_UCODE
static int		rtwn_tx_fwpkt_check(struct rtwn_softc *,
			    struct ieee80211vap *);
static int		rtwn_construct_nulldata(struct rtwn_softc *,
			    struct ieee80211vap *, uint8_t *, int);
static int		rtwn_push_nulldata(struct rtwn_softc *,
			    struct ieee80211vap *);
static void		rtwn_pwrmode_init(void *);
static void		rtwn_set_pwrmode_cb(struct rtwn_softc *,
			    union sec_param *);
#endif
static void		rtwn_tsf_sync_adhoc(void *);
static void		rtwn_tsf_sync_adhoc_task(void *, int);
static void		rtwn_tsf_sync_enable(struct rtwn_softc *,
			    struct ieee80211vap *);
static void		rtwn_set_ack_preamble(struct rtwn_softc *);
static void		rtwn_set_mode(struct rtwn_softc *, uint8_t, int);
static int		rtwn_monitor_newstate(struct ieee80211vap *,
			    enum ieee80211_state, int);
static int		rtwn_newstate(struct ieee80211vap *,
			    enum ieee80211_state, int);
static void		rtwn_calc_basicrates(struct rtwn_softc *);
static int		rtwn_run(struct rtwn_softc *,
			    struct ieee80211vap *);
#ifndef D4054
static void		rtwn_watchdog(void *);
#endif
static void		rtwn_parent(struct ieee80211com *);
static int		rtwn_dma_init(struct rtwn_softc *);
static int		rtwn_mac_init(struct rtwn_softc *);
static void		rtwn_mrr_init(struct rtwn_softc *);
static void		rtwn_scan_start(struct ieee80211com *);
static void		rtwn_scan_curchan(struct ieee80211_scan_state *,
			    unsigned long);
static void		rtwn_scan_end(struct ieee80211com *);
static void		rtwn_getradiocaps(struct ieee80211com *, int, int *,
			    struct ieee80211_channel[]);
static void		rtwn_update_chw(struct ieee80211com *);
static void		rtwn_set_channel(struct ieee80211com *);
static int		rtwn_wme_update(struct ieee80211com *);
static void		rtwn_update_slot(struct ieee80211com *);
static void		rtwn_update_slot_cb(struct rtwn_softc *,
			    union sec_param *);
static void		rtwn_update_aifs(struct rtwn_softc *, uint8_t);
static void		rtwn_update_promisc(struct ieee80211com *);
static void		rtwn_update_mcast(struct ieee80211com *);
static int		rtwn_set_bssid(struct rtwn_softc *,
			    const uint8_t *, int);
static int		rtwn_set_macaddr(struct rtwn_softc *,
			    const uint8_t *, int);
static struct ieee80211_node *rtwn_node_alloc(struct ieee80211vap *,
			    const uint8_t mac[IEEE80211_ADDR_LEN]);
static void		rtwn_newassoc(struct ieee80211_node *, int);
static void		rtwn_node_free(struct ieee80211_node *);
static void		rtwn_init_beacon_reg(struct rtwn_softc *);
static int		rtwn_init(struct rtwn_softc *);
static void		rtwn_stop(struct rtwn_softc *);

MALLOC_DEFINE(M_RTWN_PRIV, "rtwn_priv", "rtwn driver private state");

static const uint8_t rtwn_chan_2ghz[] =
	{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };

static const uint16_t wme2reg[] =
	{ R92C_EDCA_BE_PARAM, R92C_EDCA_BK_PARAM,
	  R92C_EDCA_VI_PARAM, R92C_EDCA_VO_PARAM };

int
rtwn_attach(struct rtwn_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	int error;

	sc->cur_bcnq_id = RTWN_VAP_ID_INVALID;

	RTWN_NT_LOCK_INIT(sc);
	rtwn_cmdq_init(sc);
#ifndef D4054
	callout_init_mtx(&sc->sc_watchdog_to, &sc->sc_mtx, 0);
#endif
	callout_init(&sc->sc_calib_to, 0);
	callout_init(&sc->sc_pwrmode_init, 0);
	mbufq_init(&sc->sc_snd, ifqmaxlen);

	RTWN_LOCK(sc);
	error = rtwn_read_chipid(sc);
	RTWN_UNLOCK(sc);
	if (error != 0) {
		device_printf(sc->sc_dev, "unsupported test chip\n");
		goto detach;
	}

	error = rtwn_read_rom(sc);
	if (error != 0) {
		device_printf(sc->sc_dev, "%s: cannot read rom, error %d\n",
		    __func__, error);
		goto detach;
	}

	if (sc->macid_limit > RTWN_MACID_LIMIT) {
		device_printf(sc->sc_dev,
		    "macid limit will be reduced from %d to %d\n",
		    sc->macid_limit, RTWN_MACID_LIMIT);
		sc->macid_limit = RTWN_MACID_LIMIT;
	}
	if (sc->cam_entry_limit > RTWN_CAM_ENTRY_LIMIT) {
		device_printf(sc->sc_dev,
		    "cam entry limit will be reduced from %d to %d\n",
		    sc->cam_entry_limit, RTWN_CAM_ENTRY_LIMIT);
		sc->cam_entry_limit = RTWN_CAM_ENTRY_LIMIT;
	}
	if (sc->txdesc_len > RTWN_TX_DESC_SIZE) {
		device_printf(sc->sc_dev,
		    "adjust size for Tx descriptor (current %d, needed %d)\n",
		    RTWN_TX_DESC_SIZE, sc->txdesc_len);
		goto detach;
	}

	device_printf(sc->sc_dev, "MAC/BB %s, RF 6052 %dT%dR\n",
	    sc->name, sc->ntxchains, sc->nrxchains);

	ic->ic_softc = sc;
	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */

	/* set device capabilities */
	ic->ic_caps =
		  IEEE80211_C_STA		/* station mode */
		| IEEE80211_C_MONITOR		/* monitor mode */
		| IEEE80211_C_IBSS		/* adhoc mode */
		| IEEE80211_C_HOSTAP		/* hostap mode */
#if 0	/* TODO: HRPWM register setup */
#ifndef RTWN_WITHOUT_UCODE
		| IEEE80211_C_PMGT		/* Station-side power mgmt */
#endif
#endif
		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
		| IEEE80211_C_SHSLOT		/* short slot time supported */
#if 0
		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
#endif
		| IEEE80211_C_WPA		/* 802.11i */
		| IEEE80211_C_WME		/* 802.11e */
		| IEEE80211_C_SWAMSDUTX		/* Do software A-MSDU TX */
		| IEEE80211_C_FF		/* Atheros fast-frames */
		;

	if (sc->sc_hwcrypto != RTWN_CRYPTO_SW) {
		ic->ic_cryptocaps =
		    IEEE80211_CRYPTO_WEP |
		    IEEE80211_CRYPTO_TKIP |
		    IEEE80211_CRYPTO_AES_CCM;
	}

	ic->ic_htcaps =
	      IEEE80211_HTCAP_SHORTGI20		/* short GI in 20MHz */
	    | IEEE80211_HTCAP_MAXAMSDU_3839	/* max A-MSDU length */
	    | IEEE80211_HTCAP_SMPS_OFF		/* SM PS mode disabled */
	    /* s/w capabilities */
	    | IEEE80211_HTC_HT			/* HT operation */
	    | IEEE80211_HTC_AMPDU		/* A-MPDU tx */
	    | IEEE80211_HTC_AMSDU		/* A-MSDU tx */
	    ;

	if (sc->sc_ht40) {
		ic->ic_htcaps |=
		      IEEE80211_HTCAP_CHWIDTH40	/* 40 MHz channel width */
		    | IEEE80211_HTCAP_SHORTGI40	/* short GI in 40MHz */
		    ;
	}

	ic->ic_txstream = sc->ntxchains;
	ic->ic_rxstream = sc->nrxchains;

	/* Enable TX watchdog */
#ifdef D4054
	ic->ic_flags_ext |= IEEE80211_FEXT_WATCHDOG;
#endif

	/* Adjust capabilities. */
	rtwn_adj_devcaps(sc);

	rtwn_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
	    ic->ic_channels);

	/* XXX TODO: setup regdomain if R92C_CHANNEL_PLAN_BY_HW bit is set. */

	ieee80211_ifattach(ic);
	ic->ic_raw_xmit = rtwn_raw_xmit;
	ic->ic_scan_start = rtwn_scan_start;
	sc->sc_scan_curchan = ic->ic_scan_curchan;
	ic->ic_scan_curchan = rtwn_scan_curchan;
	ic->ic_scan_end = rtwn_scan_end;
	ic->ic_getradiocaps = rtwn_getradiocaps;
	ic->ic_update_chw = rtwn_update_chw;
	ic->ic_set_channel = rtwn_set_channel;
	ic->ic_transmit = rtwn_transmit;
	ic->ic_parent = rtwn_parent;
	ic->ic_vap_create = rtwn_vap_create;
	ic->ic_vap_delete = rtwn_vap_delete;
	ic->ic_wme.wme_update = rtwn_wme_update;
	ic->ic_updateslot = rtwn_update_slot;
	ic->ic_update_promisc = rtwn_update_promisc;
	ic->ic_update_mcast = rtwn_update_mcast;
	ic->ic_node_alloc = rtwn_node_alloc;
	ic->ic_newassoc = rtwn_newassoc;
	sc->sc_node_free = ic->ic_node_free;
	ic->ic_node_free = rtwn_node_free;

	rtwn_postattach(sc);
	rtwn_radiotap_attach(sc);

	if (bootverbose)
		ieee80211_announce(ic);

	return (0);

detach:
	return (ENXIO);			/* failure */
}

static void
rtwn_radiotap_attach(struct rtwn_softc *sc)
{
	struct rtwn_rx_radiotap_header *rxtap = &sc->sc_rxtap;
	struct rtwn_tx_radiotap_header *txtap = &sc->sc_txtap;

	ieee80211_radiotap_attach(&sc->sc_ic,
	    &txtap->wt_ihdr, sizeof(*txtap), RTWN_TX_RADIOTAP_PRESENT,
	    &rxtap->wr_ihdr, sizeof(*rxtap), RTWN_RX_RADIOTAP_PRESENT);
}

void
rtwn_sysctlattach(struct rtwn_softc *sc)
{
	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);

#if 1
	sc->sc_ht40 = 0;
	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
	    "ht40", CTLFLAG_RDTUN, &sc->sc_ht40,
	    sc->sc_ht40, "Enable 40 MHz mode support");
#endif

#ifdef RTWN_DEBUG
	SYSCTL_ADD_U32(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
	    "debug", CTLFLAG_RWTUN, &sc->sc_debug, sc->sc_debug,
	    "Control debugging printfs");
#endif

	sc->sc_hwcrypto = RTWN_CRYPTO_PAIR;
	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
	    "hwcrypto", CTLFLAG_RDTUN, &sc->sc_hwcrypto,
	    sc->sc_hwcrypto, "Enable h/w crypto: "
	    "0 - disable, 1 - pairwise keys, 2 - all keys");
	if (sc->sc_hwcrypto >= RTWN_CRYPTO_MAX)
		sc->sc_hwcrypto = RTWN_CRYPTO_FULL;

	sc->sc_ratectl_sysctl = RTWN_RATECTL_NET80211;
	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
	    "ratectl", CTLFLAG_RDTUN, &sc->sc_ratectl_sysctl,
	    sc->sc_ratectl_sysctl, "Select rate control mechanism: "
	    "0 - disabled, 1 - via net80211, 2 - via firmware");
	if (sc->sc_ratectl_sysctl >= RTWN_RATECTL_MAX)
		sc->sc_ratectl_sysctl = RTWN_RATECTL_FW;

	sc->sc_ratectl = sc->sc_ratectl_sysctl;
	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
	    "ratectl_selected", CTLFLAG_RD, &sc->sc_ratectl,
	    sc->sc_ratectl,
	    "Currently selected rate control mechanism (by the driver)");
}

void
rtwn_detach(struct rtwn_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;

	if (ic->ic_softc == sc) {
		/* Stop command queue. */
		RTWN_CMDQ_LOCK(sc);
		sc->sc_detached = 1;
		RTWN_CMDQ_UNLOCK(sc);

		ieee80211_draintask(ic, &sc->cmdq_task);
		ieee80211_ifdetach(ic);
	}

	rtwn_cmdq_destroy(sc);
	if (RTWN_NT_LOCK_INITIALIZED(sc))
		RTWN_NT_LOCK_DESTROY(sc);
}

void
rtwn_suspend(struct rtwn_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;

	ieee80211_suspend_all(ic);
}

void
rtwn_resume(struct rtwn_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;

	ieee80211_resume_all(ic);
}

static void
rtwn_vap_decrement_counters(struct rtwn_softc *sc,
    enum ieee80211_opmode opmode, int id)
{

	RTWN_ASSERT_LOCKED(sc);

	if (id != RTWN_VAP_ID_INVALID) {
		KASSERT(id == 0 || id == 1, ("wrong vap id %d!\n", id));
		KASSERT(sc->vaps[id] != NULL, ("vap pointer is NULL\n"));
		sc->vaps[id] = NULL;
	}

	switch (opmode) {
	case IEEE80211_M_HOSTAP:
		sc->ap_vaps--;
		/* FALLTHROUGH */
	case IEEE80211_M_IBSS:
		sc->bcn_vaps--;
		/* FALLTHROUGH */
	case IEEE80211_M_STA:
		sc->nvaps--;
		break;
	case IEEE80211_M_MONITOR:
		sc->mon_vaps--;
		break;
	default:
		KASSERT(0, ("wrong opmode %d\n", opmode));
		break;
	}

	KASSERT(sc->vaps_running >= 0 && sc->monvaps_running >= 0,
	    ("number of running vaps is negative (vaps %d, monvaps %d)\n",
	    sc->vaps_running, sc->monvaps_running));
	KASSERT(sc->vaps_running - sc->monvaps_running <= RTWN_PORT_COUNT,
	    ("number of running vaps is too big (vaps %d, monvaps %d)\n",
	    sc->vaps_running, sc->monvaps_running));

	KASSERT(sc->nvaps >= 0 && sc->nvaps <= RTWN_PORT_COUNT,
	    ("wrong value %d for nvaps\n", sc->nvaps));
	KASSERT(sc->mon_vaps >= 0, ("mon_vaps is negative (%d)\n",
	    sc->mon_vaps));
	KASSERT(sc->bcn_vaps >= 0 && ((RTWN_CHIP_HAS_BCNQ1(sc) &&
	    sc->bcn_vaps <= RTWN_PORT_COUNT) || sc->bcn_vaps <= 1),
	    ("bcn_vaps value %d is wrong\n", sc->bcn_vaps));
	KASSERT(sc->ap_vaps >= 0 && ((RTWN_CHIP_HAS_BCNQ1(sc) &&
	    sc->ap_vaps <= RTWN_PORT_COUNT) || sc->ap_vaps <= 1),
	    ("ap_vaps value %d is wrong\n", sc->ap_vaps));
}

static void
rtwn_set_ic_opmode(struct rtwn_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;

	RTWN_ASSERT_LOCKED(sc);

	/* for ieee80211_reset_erp() */
	if (sc->bcn_vaps - sc->ap_vaps > 0)
		ic->ic_opmode = IEEE80211_M_IBSS;
	else if (sc->ap_vaps > 0)
		ic->ic_opmode = IEEE80211_M_HOSTAP;
	else if (sc->nvaps > 0)
		ic->ic_opmode = IEEE80211_M_STA;
	else
		ic->ic_opmode = IEEE80211_M_MONITOR;
}

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])
{
	struct rtwn_softc *sc = ic->ic_softc;
	struct rtwn_vap *uvp;
	struct ieee80211vap *vap;
	int id = RTWN_VAP_ID_INVALID;

	RTWN_LOCK(sc);
	KASSERT(sc->nvaps <= RTWN_PORT_COUNT,
	    ("nvaps overflow (%d > %d)\n", sc->nvaps, RTWN_PORT_COUNT));
	KASSERT(sc->ap_vaps <= RTWN_PORT_COUNT,
	    ("ap_vaps overflow (%d > %d)\n", sc->ap_vaps, RTWN_PORT_COUNT));
	KASSERT(sc->bcn_vaps <= RTWN_PORT_COUNT,
	    ("bcn_vaps overflow (%d > %d)\n", sc->bcn_vaps, RTWN_PORT_COUNT));

	if (opmode != IEEE80211_M_MONITOR) {
		switch (sc->nvaps) {
		case 0:
			id = 0;
			break;
		case 1:
			if (sc->vaps[1] == NULL)
				id = 1;
			else if (sc->vaps[0] == NULL)
				id = 0;
			KASSERT(id != RTWN_VAP_ID_INVALID,
			    ("no free ports left\n"));
			break;
		case 2:
		default:
			goto fail;
		}

		if (opmode == IEEE80211_M_IBSS ||
		    opmode == IEEE80211_M_HOSTAP) {
			if ((sc->bcn_vaps == 1 && !RTWN_CHIP_HAS_BCNQ1(sc)) ||
			    sc->bcn_vaps == RTWN_PORT_COUNT)
				goto fail;
		}
	}

	switch (opmode) {
	case IEEE80211_M_HOSTAP:
		sc->ap_vaps++;
		/* FALLTHROUGH */
	case IEEE80211_M_IBSS:
		sc->bcn_vaps++;
		/* FALLTHROUGH */
	case IEEE80211_M_STA:
		sc->nvaps++;
		break;
	case IEEE80211_M_MONITOR:
		sc->mon_vaps++;
		break;
	default:
		KASSERT(0, ("unknown opmode %d\n", opmode));
		goto fail;
	}
	RTWN_UNLOCK(sc);

	uvp = malloc(sizeof(struct rtwn_vap), M_80211_VAP, M_WAITOK | M_ZERO);
	uvp->id = id;
	if (id != RTWN_VAP_ID_INVALID) {
		RTWN_LOCK(sc);
		sc->vaps[id] = uvp;
		RTWN_UNLOCK(sc);
	}
	vap = &uvp->vap;
	/* enable s/w bmiss handling for sta mode */

	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
		/* out of memory */
		free(uvp, M_80211_VAP);

		RTWN_LOCK(sc);
		rtwn_vap_decrement_counters(sc, opmode, id);
		RTWN_UNLOCK(sc);

		return (NULL);
	}

	rtwn_beacon_init(sc, &uvp->bcn_desc.txd[0], uvp->id);
	rtwn_vap_preattach(sc, vap);

	/* override state transition machine */
	uvp->newstate = vap->iv_newstate;
	if (opmode == IEEE80211_M_MONITOR)
		vap->iv_newstate = rtwn_monitor_newstate;
	else
		vap->iv_newstate = rtwn_newstate;
	vap->iv_update_beacon = rtwn_update_beacon;
	vap->iv_reset = rtwn_ioctl_reset;
	vap->iv_key_alloc = rtwn_key_alloc;
	vap->iv_key_set = rtwn_key_set;
	vap->iv_key_delete = rtwn_key_delete;
	vap->iv_max_aid = sc->macid_limit;

	/* 802.11n parameters */
	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_16;
	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;

	TIMEOUT_TASK_INIT(taskqueue_thread, &uvp->tx_beacon_csa, 0,
	    rtwn_tx_beacon_csa, vap);
	if (opmode == IEEE80211_M_IBSS) {
		uvp->recv_mgmt = vap->iv_recv_mgmt;
		vap->iv_recv_mgmt = rtwn_adhoc_recv_mgmt;
		TASK_INIT(&uvp->tsf_sync_adhoc_task, 0,
		    rtwn_tsf_sync_adhoc_task, vap);
		callout_init(&uvp->tsf_sync_adhoc, 0);
	}

	/*
	 * NB: driver can select net80211 RA even when user requests
	 * another mechanism.
	 */
	ieee80211_ratectl_init(vap);

	/* complete setup */
	ieee80211_vap_attach(vap, ieee80211_media_change,
	    ieee80211_media_status, mac);

	RTWN_LOCK(sc);
	rtwn_set_ic_opmode(sc);
	if (sc->sc_flags & RTWN_RUNNING) {
		if (uvp->id != RTWN_VAP_ID_INVALID)
			rtwn_set_macaddr(sc, vap->iv_myaddr, uvp->id);

		rtwn_rxfilter_update(sc);
	}
	RTWN_UNLOCK(sc);

	return (vap);

fail:
	RTWN_UNLOCK(sc);
	return (NULL);
}

static void
rtwn_vap_delete(struct ieee80211vap *vap)
{
	struct ieee80211com *ic = vap->iv_ic;
	struct rtwn_softc *sc = ic->ic_softc;
	struct rtwn_vap *uvp = RTWN_VAP(vap);

	/* Put vap into INIT state + stop device if needed. */
	ieee80211_stop(vap);
	ieee80211_draintask(ic, &vap->iv_nstate_task);
	ieee80211_draintask(ic, &ic->ic_parent_task);

	RTWN_LOCK(sc);
	/* Cancel any unfinished Tx. */
	rtwn_reset_lists(sc, vap);
	if (uvp->bcn_mbuf != NULL)
		m_freem(uvp->bcn_mbuf);
	rtwn_vap_decrement_counters(sc, vap->iv_opmode, uvp->id);
	rtwn_set_ic_opmode(sc);
	if (sc->sc_flags & RTWN_RUNNING)
		rtwn_rxfilter_update(sc);
	RTWN_UNLOCK(sc);

	if (vap->iv_opmode == IEEE80211_M_IBSS) {
		ieee80211_draintask(ic, &uvp->tsf_sync_adhoc_task);
		callout_drain(&uvp->tsf_sync_adhoc);
	}

	ieee80211_ratectl_deinit(vap);
	ieee80211_vap_detach(vap);
	free(uvp, M_80211_VAP);
}

static int
rtwn_read_chipid(struct rtwn_softc *sc)
{
	uint32_t reg;

	reg = rtwn_read_4(sc, R92C_SYS_CFG);
	if (reg & R92C_SYS_CFG_TRP_VAUX_EN)	/* test chip */
		return (EOPNOTSUPP);

	rtwn_read_chipid_vendor(sc, reg);

	return (0);
}

static int
rtwn_ioctl_reset(struct ieee80211vap *vap, u_long cmd)
{
	int error;

	switch (cmd) {
#ifndef RTWN_WITHOUT_UCODE
	case IEEE80211_IOC_POWERSAVE:
	case IEEE80211_IOC_POWERSAVESLEEP:
	{
		struct rtwn_softc *sc = vap->iv_ic->ic_softc;
		struct rtwn_vap *uvp = RTWN_VAP(vap);

		if (vap->iv_opmode == IEEE80211_M_STA && uvp->id == 0) {
			RTWN_LOCK(sc);
			if (sc->sc_flags & RTWN_RUNNING)
				error = rtwn_set_pwrmode(sc, vap, 1);
			else
				error = 0;
			RTWN_UNLOCK(sc);
			if (error != 0)
				error = ENETRESET;
		} else
			error = EOPNOTSUPP;
		break;
	}
#endif
	case IEEE80211_IOC_SHORTGI:
	case IEEE80211_IOC_RTSTHRESHOLD:
	case IEEE80211_IOC_PROTMODE:
	case IEEE80211_IOC_HTPROTMODE:
	case IEEE80211_IOC_LDPC:
		error = 0;
		break;
	default:
		error = ENETRESET;
		break;
	}

	return (error);
}

static void
rtwn_set_media_status(struct rtwn_softc *sc, union sec_param *data)
{
	sc->sc_set_media_status(sc, data->macid);
}

#ifndef RTWN_WITHOUT_UCODE
static int
rtwn_tx_fwpkt_check(struct rtwn_softc *sc, struct ieee80211vap *vap)
{
	int ntries, error;

	for (ntries = 0; ntries < 5; ntries++) {
		error = rtwn_push_nulldata(sc, vap);
		if (error == 0)
			break;
	}
	if (ntries == 5) {
		device_printf(sc->sc_dev,
		    "%s: cannot push f/w frames into chip, error %d!\n",
		    __func__, error);
		return (error);
	}

	return (0);
}

static int
rtwn_construct_nulldata(struct rtwn_softc *sc, struct ieee80211vap *vap,
    uint8_t *ptr, int qos)
{
	struct rtwn_vap *uvp = RTWN_VAP(vap);
	struct ieee80211com *ic = &sc->sc_ic;
	struct rtwn_tx_desc_common *txd;
	struct ieee80211_frame *wh;
	int pktlen;

	/* XXX obtain from net80211 */
	wh = (struct ieee80211_frame *)(ptr + sc->txdesc_len);
	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
	wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
	IEEE80211_ADDR_COPY(wh->i_addr1, vap->iv_bss->ni_bssid);
	IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
	IEEE80211_ADDR_COPY(wh->i_addr3, vap->iv_bss->ni_macaddr);

	txd = (struct rtwn_tx_desc_common *)ptr;
	txd->offset = sc->txdesc_len;
	pktlen = sc->txdesc_len;
	if (qos) {
		struct ieee80211_qosframe *qwh;
		const int tid = WME_AC_TO_TID(WME_AC_BE);

		qwh = (struct ieee80211_qosframe *)wh;
		qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS_NULL;
		qwh->i_qos[0] = tid & IEEE80211_QOS_TID;

		txd->pktlen = htole16(sizeof(struct ieee80211_qosframe));
		pktlen += sizeof(struct ieee80211_qosframe);
	} else {
		wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA;

		txd->pktlen = htole16(sizeof(struct ieee80211_frame));
		pktlen += sizeof(struct ieee80211_frame);
	}

	rtwn_fill_tx_desc_null(sc, ptr,
	    ic->ic_curmode == IEEE80211_MODE_11B, qos, uvp->id);

	return (pktlen);
}

static int
rtwn_push_nulldata(struct rtwn_softc *sc, struct ieee80211vap *vap)
{
	struct rtwn_vap *uvp = RTWN_VAP(vap);
	struct ieee80211com *ic = vap->iv_ic;
	struct ieee80211_channel *c = ic->ic_curchan;
	struct mbuf *m;
	uint8_t *ptr;
	int required_size, bcn_size, null_size, null_data, error;

	if (!(sc->sc_flags & RTWN_FW_LOADED))
		return (0);	/* requires firmware */

	KASSERT(sc->page_size > 0, ("page size was not set!\n"));

	/* Leave some space for beacon (multi-vap) */
	bcn_size = roundup(RTWN_BCN_MAX_SIZE, sc->page_size);
	/* 1 page for Null Data + 1 page for Qos Null Data frames. */
	required_size = bcn_size + sc->page_size * 2;

	m = m_get2(required_size, M_NOWAIT, MT_DATA, M_PKTHDR);
	if (m == NULL)
		return (ENOMEM);

	/* Setup beacon descriptor. */
	rtwn_beacon_set_rate(sc, &uvp->bcn_desc.txd[0],
	    IEEE80211_IS_CHAN_5GHZ(c));

	ptr = mtod(m, uint8_t *);
	memset(ptr, 0, required_size - sc->txdesc_len);

	/* Construct Null Data frame. */
	ptr += bcn_size - sc->txdesc_len;
	null_size = rtwn_construct_nulldata(sc, vap, ptr, 0);
	KASSERT(null_size < sc->page_size,
	    ("recalculate size for Null Data frame\n"));

	/* Construct Qos Null Data frame. */
	ptr += roundup(null_size, sc->page_size);
	null_size = rtwn_construct_nulldata(sc, vap, ptr, 1);
	KASSERT(null_size < sc->page_size,
	    ("recalculate size for Qos Null Data frame\n"));

	/* Do not try to detect a beacon here. */
	rtwn_setbits_1_shift(sc, R92C_CR, 0, R92C_CR_ENSWBCN, 1);
	rtwn_setbits_1_shift(sc, R92C_FWHW_TXQ_CTRL,
	    R92C_FWHW_TXQ_CTRL_REAL_BEACON, 0, 2);

	if (uvp->bcn_mbuf != NULL) {
		rtwn_beacon_unload(sc, uvp->id);
		m_freem(uvp->bcn_mbuf);
	}

	m->m_pkthdr.len = m->m_len = required_size - sc->txdesc_len;
	uvp->bcn_mbuf = m;

	error = rtwn_tx_beacon_check(sc, uvp);
	if (error != 0) {
		RTWN_DPRINTF(sc, RTWN_DEBUG_BEACON,
		    "%s: frame was not recognized!\n", __func__);
		goto fail;
	}

	/* Setup addresses in firmware. */
	null_data = howmany(bcn_size, sc->page_size);
	error = rtwn_set_rsvd_page(sc, 0, null_data, null_data + 1);
	if (error != 0) {
		device_printf(sc->sc_dev,
		    "%s: CMD_RSVD_PAGE was not sent, error %d\n",
		    __func__, error);
		goto fail;
	}

fail:
	/* Re-enable beacon detection. */
	rtwn_setbits_1_shift(sc, R92C_FWHW_TXQ_CTRL,
	    0, R92C_FWHW_TXQ_CTRL_REAL_BEACON, 2);
	rtwn_setbits_1_shift(sc, R92C_CR, R92C_CR_ENSWBCN, 0, 1);

	/* Restore beacon (if present). */
	if (sc->bcn_vaps > 0 && sc->vaps[!uvp->id] != NULL) {
		struct rtwn_vap *uvp2 = sc->vaps[!uvp->id];

		if (uvp2->curr_mode != R92C_MSR_NOLINK)
			error = rtwn_tx_beacon_check(sc, uvp2);
	}

	return (error);
}

static void
rtwn_pwrmode_init(void *arg)
{
	struct rtwn_softc *sc = arg;

	rtwn_cmd_sleepable(sc, NULL, 0, rtwn_set_pwrmode_cb);
}

static void
rtwn_set_pwrmode_cb(struct rtwn_softc *sc, union sec_param *data)
{
	struct ieee80211vap *vap = &sc->vaps[0]->vap;

	if (vap != NULL)
		rtwn_set_pwrmode(sc, vap, 1);
}
#endif

static void
rtwn_tsf_sync_adhoc(void *arg)
{
	struct ieee80211vap *vap = arg;
	struct ieee80211com *ic = vap->iv_ic;
	struct rtwn_vap *uvp = RTWN_VAP(vap);

	if (uvp->curr_mode != R92C_MSR_NOLINK) {
		/* Do it in process context. */
		ieee80211_runtask(ic, &uvp->tsf_sync_adhoc_task);
	}
}

/*
 * Workaround for TSF synchronization:
 * when BSSID filter in IBSS mode is not set
 * (and TSF synchronization is enabled), then any beacon may update it.
 * This routine synchronizes it when BSSID matching is enabled (IBSS merge
 * is not possible during this period).
 *
 * NOTE: there is no race with rtwn_newstate(), since it uses the same
 * taskqueue.
 */
static void
rtwn_tsf_sync_adhoc_task(void *arg, int pending)
{
	struct ieee80211vap *vap = arg;
	struct rtwn_vap *uvp = RTWN_VAP(vap);
	struct rtwn_softc *sc = vap->iv_ic->ic_softc;
	struct ieee80211_node *ni;

	RTWN_LOCK(sc);
	ni = ieee80211_ref_node(vap->iv_bss);

	/* Accept beacons with the same BSSID. */
	rtwn_set_rx_bssid_all(sc, 0);

	/* Deny RCR updates. */
	sc->sc_flags |= RTWN_RCR_LOCKED;

	/* Enable synchronization. */
	rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
	    R92C_BCN_CTRL_DIS_TSF_UDT0, 0);

	/* Synchronize. */
	rtwn_delay(sc, ni->ni_intval * 5 * 1000);

	/* Disable synchronization. */
	rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
	    0, R92C_BCN_CTRL_DIS_TSF_UDT0);

	/* Accept all beacons. */
	sc->sc_flags &= ~RTWN_RCR_LOCKED;
	rtwn_set_rx_bssid_all(sc, 1);

	/* Schedule next TSF synchronization. */
	callout_reset(&uvp->tsf_sync_adhoc, 60*hz, rtwn_tsf_sync_adhoc, vap);

	ieee80211_free_node(ni);
	RTWN_UNLOCK(sc);
}

static void
rtwn_tsf_sync_enable(struct rtwn_softc *sc, struct ieee80211vap *vap)
{
	struct ieee80211com *ic = &sc->sc_ic;
	struct rtwn_vap *uvp = RTWN_VAP(vap);

	/* Reset TSF. */
	rtwn_write_1(sc, R92C_DUAL_TSF_RST, R92C_DUAL_TSF_RESET(uvp->id));

	switch (vap->iv_opmode) {
	case IEEE80211_M_STA:
		/* Enable TSF synchronization. */
		rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
		    R92C_BCN_CTRL_DIS_TSF_UDT0, 0);
		break;
	case IEEE80211_M_IBSS:
		ieee80211_runtask(ic, &uvp->tsf_sync_adhoc_task);
		/* FALLTHROUGH */
	case IEEE80211_M_HOSTAP:
		/* Enable beaconing. */
		rtwn_beacon_enable(sc, uvp->id, 1);
		break;
	default:
		device_printf(sc->sc_dev, "undefined opmode %d\n",
		    vap->iv_opmode);
		return;
	}
}

static void
rtwn_set_ack_preamble(struct rtwn_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	uint32_t reg;

	reg = rtwn_read_4(sc, R92C_WMAC_TRXPTCL_CTL);
	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
		reg |= R92C_WMAC_TRXPTCL_SHPRE;
	else
		reg &= ~R92C_WMAC_TRXPTCL_SHPRE;
	rtwn_write_4(sc, R92C_WMAC_TRXPTCL_CTL, reg);
}

static void
rtwn_set_mode(struct rtwn_softc *sc, uint8_t mode, int id)
{

	rtwn_setbits_1(sc, R92C_MSR, R92C_MSR_MASK << id * 2, mode << id * 2);
	if (sc->vaps[id] != NULL)
		sc->vaps[id]->curr_mode = mode;
}

static int
rtwn_monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate,
    int arg)
{
	struct ieee80211com *ic = vap->iv_ic;
	struct rtwn_softc *sc = ic->ic_softc;
	struct rtwn_vap *uvp = RTWN_VAP(vap);

	RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s -> %s\n",
	    ieee80211_state_name[vap->iv_state],
	    ieee80211_state_name[nstate]);

	if (vap->iv_state != nstate) {
		IEEE80211_UNLOCK(ic);
		RTWN_LOCK(sc);

		switch (nstate) {
		case IEEE80211_S_INIT:
			sc->vaps_running--;
			sc->monvaps_running--;

			if (sc->vaps_running == 0) {
				/* Turn link LED off. */
				rtwn_set_led(sc, RTWN_LED_LINK, 0);
			}
			break;
		case IEEE80211_S_RUN:
			sc->vaps_running++;
			sc->monvaps_running++;

			if (sc->vaps_running == 1) {
				/* Turn link LED on. */
				rtwn_set_led(sc, RTWN_LED_LINK, 1);
			}
			break;
		default:
			/* NOTREACHED */
			break;
		}

		RTWN_UNLOCK(sc);
		IEEE80211_LOCK(ic);
	}

	return (uvp->newstate(vap, nstate, arg));
}

static int
rtwn_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
{
	struct rtwn_vap *uvp = RTWN_VAP(vap);
	struct ieee80211com *ic = vap->iv_ic;
	struct rtwn_softc *sc = ic->ic_softc;
	enum ieee80211_state ostate;
	int error, early_newstate;

	ostate = vap->iv_state;
	RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s -> %s\n",
	    ieee80211_state_name[ostate], ieee80211_state_name[nstate]);

	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC &&
	    ostate == IEEE80211_S_INIT && nstate == IEEE80211_S_RUN) {
		/* need to call iv_newstate() firstly */
		error = uvp->newstate(vap, nstate, arg);
		if (error != 0)
			return (error);

		early_newstate = 1;
	} else
		early_newstate = 0;

	if (ostate == IEEE80211_S_CSA) {
		taskqueue_cancel_timeout(taskqueue_thread,
		    &uvp->tx_beacon_csa, NULL);

		/*
		 * In multi-vap case second counter may not be cleared
		 * properly.
		 */
		vap->iv_csa_count = 0;
	}
	IEEE80211_UNLOCK(ic);
	RTWN_LOCK(sc);

	if (ostate == IEEE80211_S_CSA) {
		/* Unblock all queues (multi-vap case). */
		rtwn_write_1(sc, R92C_TXPAUSE, 0);
	}

	if ((ostate == IEEE80211_S_RUN && nstate != IEEE80211_S_CSA) ||
	    ostate == IEEE80211_S_CSA) {
		sc->vaps_running--;

		/* Set media status to 'No Link'. */
		rtwn_set_mode(sc, R92C_MSR_NOLINK, uvp->id);

		if (vap->iv_opmode == IEEE80211_M_IBSS) {
			/* Stop periodical TSF synchronization. */
			callout_stop(&uvp->tsf_sync_adhoc);
		}

		/* Disable TSF synchronization / beaconing. */
		rtwn_beacon_enable(sc, uvp->id, 0);
		rtwn_setbits_1(sc, R92C_BCN_CTRL(uvp->id),
		    0, R92C_BCN_CTRL_DIS_TSF_UDT0);

		/* NB: monitor mode vaps are using port 0. */
		if (uvp->id != 0 || sc->monvaps_running == 0) {
			/* Reset TSF. */
			rtwn_write_1(sc, R92C_DUAL_TSF_RST,
			    R92C_DUAL_TSF_RESET(uvp->id));
		}

#ifndef RTWN_WITHOUT_UCODE
		if ((ic->ic_caps & IEEE80211_C_PMGT) != 0 && uvp->id == 0) {
			/* Disable power management. */
			callout_stop(&sc->sc_pwrmode_init);
			rtwn_set_pwrmode(sc, vap, 0);
		}
#endif
		if (sc->vaps_running - sc->monvaps_running > 0) {
			/* Recalculate basic rates bitmap. */
			rtwn_calc_basicrates(sc);
		}

		if (sc->vaps_running == sc->monvaps_running) {
			/* Stop calibration. */
			callout_stop(&sc->sc_calib_to);

			/* Stop Rx of data frames. */
			rtwn_write_2(sc, R92C_RXFLTMAP2, 0);

			/* Reset EDCA parameters. */
			rtwn_write_4(sc, R92C_EDCA_VO_PARAM, 0x002f3217);
			rtwn_write_4(sc, R92C_EDCA_VI_PARAM, 0x005e4317);
			rtwn_write_4(sc, R92C_EDCA_BE_PARAM, 0x00105320);
			rtwn_write_4(sc, R92C_EDCA_BK_PARAM, 0x0000a444);

			if (sc->vaps_running == 0) {
				/* Turn link LED off. */
				rtwn_set_led(sc, RTWN_LED_LINK, 0);
			}
		}
	}

	error = 0;
	switch (nstate) {
	case IEEE80211_S_SCAN:
		/* Pause AC Tx queues. */
		if (sc->vaps_running == 0)
			rtwn_setbits_1(sc, R92C_TXPAUSE, 0, R92C_TX_QUEUE_AC);
		break;
	case IEEE80211_S_RUN:
		error = rtwn_run(sc, vap);
		if (error != 0) {
			device_printf(sc->sc_dev,
			    "%s: could not move to RUN state\n", __func__);
			break;
		}

		sc->vaps_running++;
		break;
	case IEEE80211_S_CSA:
		/* Block all Tx queues (except beacon queue). */
		rtwn_setbits_1(sc, R92C_TXPAUSE, 0,
		    R92C_TX_QUEUE_AC | R92C_TX_QUEUE_MGT | R92C_TX_QUEUE_HIGH);
		break;
	default:
		break;
	}

	RTWN_UNLOCK(sc);
	IEEE80211_LOCK(ic);
	if (error != 0)
		return (error);

	return (early_newstate ? 0 : uvp->newstate(vap, nstate, arg));
}

static void
rtwn_calc_basicrates(struct rtwn_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	uint32_t basicrates;
	int i;

	RTWN_ASSERT_LOCKED(sc);

	if (ic->ic_flags & IEEE80211_F_SCAN)
		return;		/* will be done by rtwn_scan_end(). */

	basicrates = 0;
	for (i = 0; i < nitems(sc->vaps); i++) {
		struct rtwn_vap *rvp;
		struct ieee80211vap *vap;
		struct ieee80211_node *ni;
		uint32_t rates;

		rvp = sc->vaps[i];
		if (rvp == NULL || rvp->curr_mode == R92C_MSR_NOLINK)
			continue;

		vap = &rvp->vap;
		if (vap->iv_bss == NULL)
			continue;

		ni = ieee80211_ref_node(vap->iv_bss);
		rtwn_get_rates(sc, &ni->ni_rates, NULL, &rates, NULL, 1);
		basicrates |= rates;
		ieee80211_free_node(ni);
	}

	if (basicrates == 0)
		return;

	/* XXX initial RTS rate? */
	rtwn_set_basicrates(sc, basicrates);
}

static int
rtwn_run(struct rtwn_softc *sc, struct ieee80211vap *vap)
{
	struct ieee80211com *ic = vap->iv_ic;
	struct rtwn_vap *uvp = RTWN_VAP(vap);
	struct ieee80211_node *ni;
	uint8_t mode;
	int error;

	RTWN_ASSERT_LOCKED(sc);

	error = 0;
	ni = ieee80211_ref_node(vap->iv_bss);

	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
	    ni->ni_chan == IEEE80211_CHAN_ANYC) {
		error = EINVAL;
		goto fail;
	}

	switch (vap->iv_opmode) {
	case IEEE80211_M_STA:
		mode = R92C_MSR_INFRA;
		break;
	case IEEE80211_M_IBSS:
		mode = R92C_MSR_ADHOC;
		break;
	case IEEE80211_M_HOSTAP:
		mode = R92C_MSR_AP;
		break;
	default:
		KASSERT(0, ("undefined opmode %d\n", vap->iv_opmode));
		error = EINVAL;
		goto fail;
	}

	/* Set media status to 'Associated'. */
	rtwn_set_mode(sc, mode, uvp->id);

	/* Set AssocID. */
	/* XXX multi-vap? */
	rtwn_write_2(sc, R92C_BCN_PSR_RPT,
	    0xc000 | IEEE80211_NODE_AID(ni));

	/* Set BSSID. */
	rtwn_set_bssid(sc, ni->ni_bssid, uvp->id);

	/* Set beacon interval. */
	rtwn_write_2(sc, R92C_BCN_INTERVAL(uvp->id), ni->ni_intval);

	if (sc->vaps_running == sc->monvaps_running) {
		/* Enable Rx of data frames. */
		rtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff);

		/* Flush all AC queues. */
		rtwn_write_1(sc, R92C_TXPAUSE, 0);
	}

#ifndef RTWN_WITHOUT_UCODE
	/* Upload (QoS) Null Data frame to firmware. */
	/* Note: do this for port 0 only. */
	if ((ic->ic_caps & IEEE80211_C_PMGT) != 0 &&
	    vap->iv_opmode == IEEE80211_M_STA && uvp->id == 0) {
		error = rtwn_tx_fwpkt_check(sc, vap);
		if (error != 0)
			goto fail;

		/* Setup power management. */
		/*
		 * NB: it will be enabled immediately - delay it,
		 * so 4-Way handshake will not be interrupted.
		 */
		callout_reset(&sc->sc_pwrmode_init, 5*hz,
		    rtwn_pwrmode_init, sc);
	}
#endif

	/* Enable TSF synchronization. */
	rtwn_tsf_sync_enable(sc, vap);

	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
	    vap->iv_opmode == IEEE80211_M_IBSS) {
		error = rtwn_setup_beacon(sc, ni);
		if (error != 0) {
			device_printf(sc->sc_dev,
			    "unable to push beacon into the chip, "
			    "error %d\n", error);
			goto fail;
		}
	}

	/* Set ACK preamble type. */
	rtwn_set_ack_preamble(sc);

	/* Set basic rates mask. */
	rtwn_calc_basicrates(sc);

#ifdef RTWN_TODO
	rtwn_write_1(sc, R92C_SIFS_CCK + 1, 10);
	rtwn_write_1(sc, R92C_SIFS_OFDM + 1, 10);
	rtwn_write_1(sc, R92C_SPEC_SIFS + 1, 10);
	rtwn_write_1(sc, R92C_MAC_SPEC_SIFS + 1, 10);
	rtwn_write_1(sc, R92C_R2T_SIFS + 1, 10);
	rtwn_write_1(sc, R92C_T2T_SIFS + 1, 10);
#endif

	if (sc->vaps_running == sc->monvaps_running) {
		/* Reset temperature calibration state machine. */
		sc->sc_flags &= ~RTWN_TEMP_MEASURED;
		sc->thcal_temp = sc->thermal_meter;

		/* Start periodic calibration. */
		callout_reset(&sc->sc_calib_to, 2*hz, rtwn_calib_to,
		    sc);

		if (sc->vaps_running == 0) {
			/* Turn link LED on. */
			rtwn_set_led(sc, RTWN_LED_LINK, 1);
		}
	}

fail:
	ieee80211_free_node(ni);

	return (error);
}

#ifndef D4054
static void
rtwn_watchdog(void *arg)
{
	struct rtwn_softc *sc = arg;
	struct ieee80211com *ic = &sc->sc_ic;

	RTWN_ASSERT_LOCKED(sc);

	KASSERT(sc->sc_flags & RTWN_RUNNING, ("not running"));

	if (sc->sc_tx_timer != 0 && --sc->sc_tx_timer == 0) {
		ic_printf(ic, "device timeout\n");
		ieee80211_restart_all(ic);
		return;
	}
	callout_reset(&sc->sc_watchdog_to, hz, rtwn_watchdog, sc);
}
#endif

static void
rtwn_parent(struct ieee80211com *ic)
{
	struct rtwn_softc *sc = ic->ic_softc;
	struct ieee80211vap *vap;

	if (ic->ic_nrunning > 0) {
		if (rtwn_init(sc) != 0) {
			IEEE80211_LOCK(ic);
			TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
				ieee80211_stop_locked(vap);
			IEEE80211_UNLOCK(ic);
		} else
			ieee80211_start_all(ic);
	} else
		rtwn_stop(sc);
}

static int
rtwn_dma_init(struct rtwn_softc *sc)
{
#define RTWN_CHK(res) do {	\
	if (res != 0)		\
		return (EIO);	\
} while(0)
	uint16_t reg;
	uint8_t tx_boundary;
	int error;

	/* Initialize LLT table. */
	error = rtwn_llt_init(sc);
	if (error != 0)
		return (error);

	/* Set the number of pages for each queue. */
	RTWN_DPRINTF(sc, RTWN_DEBUG_RESET,
	    "%s: pages per queue: high %d, normal %d, low %d, public %d\n",
	    __func__, sc->nhqpages, sc->nnqpages, sc->nlqpages,
	    sc->npubqpages);

	RTWN_CHK(rtwn_write_1(sc, R92C_RQPN_NPQ, sc->nnqpages));
	RTWN_CHK(rtwn_write_4(sc, R92C_RQPN,
	    /* Set number of pages for public queue. */
	    SM(R92C_RQPN_PUBQ, sc->npubqpages) |
	    /* Set number of pages for high priority queue. */
	    SM(R92C_RQPN_HPQ, sc->nhqpages) |
	    /* Set number of pages for low priority queue. */
	    SM(R92C_RQPN_LPQ, sc->nlqpages) |
	    /* Load values. */
	    R92C_RQPN_LD));

	/* Initialize TX buffer boundary. */
	KASSERT(sc->page_count < 255 && sc->page_count > 0,
	    ("page_count is %d\n", sc->page_count));
	tx_boundary = sc->page_count + 1;
	RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, tx_boundary));
	RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, tx_boundary));
	RTWN_CHK(rtwn_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD, tx_boundary));
	RTWN_CHK(rtwn_write_1(sc, R92C_TRXFF_BNDY, tx_boundary));
	RTWN_CHK(rtwn_write_1(sc, R92C_TDECTRL + 1, tx_boundary));

	error = rtwn_init_bcnq1_boundary(sc);
	if (error != 0)
		return (error);

	/* Set queue to USB pipe mapping. */
	/* Note: PCIe devices are using some magic number here. */
	reg = rtwn_get_qmap(sc);
	RTWN_CHK(rtwn_setbits_2(sc, R92C_TRXDMA_CTRL,
	    R92C_TRXDMA_CTRL_QMAP_M, reg));

	/* Configure Tx/Rx DMA (PCIe). */
	rtwn_set_desc_addr(sc);

	/* Set Tx/Rx transfer page boundary. */
	RTWN_CHK(rtwn_write_2(sc, R92C_TRXFF_BNDY + 2,
	    sc->rx_dma_size - 1));

	/* Set Tx/Rx transfer page size. */
	rtwn_set_page_size(sc);

	return (0);
}

static int
rtwn_mac_init(struct rtwn_softc *sc)
{
	int i, error;

	/* Write MAC initialization values. */
	for (i = 0; i < sc->mac_size; i++) {
		error = rtwn_write_1(sc, sc->mac_prog[i].reg,
		    sc->mac_prog[i].val);
		if (error != 0)
			return (error);
	}

	return (0);
}

static void
rtwn_mrr_init(struct rtwn_softc *sc)
{
	int i;

	/* Drop rate index by 1 per retry. */
	for (i = 0; i < R92C_DARFRC_SIZE; i++) {
		rtwn_write_1(sc, R92C_DARFRC + i, i + 1);
		rtwn_write_1(sc, R92C_RARFRC + i, i + 1);
	}
}

static void
rtwn_scan_start(struct ieee80211com *ic)
{
	struct rtwn_softc *sc = ic->ic_softc;

	RTWN_LOCK(sc);
	/* Pause beaconing. */
	rtwn_setbits_1(sc, R92C_TXPAUSE, 0, R92C_TX_QUEUE_BCN);
	/* Receive beacons / probe responses from any BSSID. */
	if (sc->bcn_vaps == 0)
		rtwn_set_rx_bssid_all(sc, 1);
	RTWN_UNLOCK(sc);
}

static void
rtwn_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
{
	struct rtwn_softc *sc = ss->ss_ic->ic_softc;

	/* Make link LED blink during scan. */
	RTWN_LOCK(sc);
	rtwn_set_led(sc, RTWN_LED_LINK, !sc->ledlink);
	RTWN_UNLOCK(sc);

	sc->sc_scan_curchan(ss, maxdwell);
}

static void
rtwn_scan_end(struct ieee80211com *ic)
{
	struct rtwn_softc *sc = ic->ic_softc;

	RTWN_LOCK(sc);
	/* Restore limitations. */
	if (ic->ic_promisc == 0 && sc->bcn_vaps == 0)
		rtwn_set_rx_bssid_all(sc, 0);

	/* Restore LED state. */
	rtwn_set_led(sc, RTWN_LED_LINK, (sc->vaps_running != 0));

	/* Restore basic rates mask. */
	rtwn_calc_basicrates(sc);

	/* Resume beaconing. */
	rtwn_setbits_1(sc, R92C_TXPAUSE, R92C_TX_QUEUE_BCN, 0);
	RTWN_UNLOCK(sc);
}

static void
rtwn_getradiocaps(struct ieee80211com *ic,
    int maxchans, int *nchans, struct ieee80211_channel chans[])
{
	struct rtwn_softc *sc = ic->ic_softc;
	uint8_t bands[IEEE80211_MODE_BYTES];
	int i;

	memset(bands, 0, sizeof(bands));
	setbit(bands, IEEE80211_MODE_11B);
	setbit(bands, IEEE80211_MODE_11G);
	setbit(bands, IEEE80211_MODE_11NG);
	ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
	    rtwn_chan_2ghz, nitems(rtwn_chan_2ghz), bands,
	    !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40));

	/* XXX workaround add_channel_list() limitations */
	setbit(bands, IEEE80211_MODE_11A);
	setbit(bands, IEEE80211_MODE_11NA);
	for (i = 0; i < nitems(sc->chan_num_5ghz); i++) {
		if (sc->chan_num_5ghz[i] == 0)
			continue;

		ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
		    sc->chan_list_5ghz[i], sc->chan_num_5ghz[i], bands,
		    !!(ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40));
	}
}

static void
rtwn_update_chw(struct ieee80211com *ic)
{
}

static void
rtwn_set_channel(struct ieee80211com *ic)
{
	struct rtwn_softc *sc = ic->ic_softc;
	struct ieee80211_channel *c = ic->ic_curchan;

	RTWN_LOCK(sc);
	rtwn_set_chan(sc, c);
	sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
	sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
	sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
	sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
	RTWN_UNLOCK(sc);
}

static int
rtwn_wme_update(struct ieee80211com *ic)
{
	struct chanAccParams chp;
	struct ieee80211_channel *c = ic->ic_curchan;
	struct rtwn_softc *sc = ic->ic_softc;
	struct wmeParams *wmep = sc->cap_wmeParams;
	uint8_t aifs, acm, slottime;
	int ac;

	ieee80211_wme_ic_getparams(ic, &chp);

	/* Prevent possible races. */
	IEEE80211_LOCK(ic);	/* XXX */
	RTWN_LOCK(sc);
	memcpy(wmep, chp.cap_wmeParams, sizeof(sc->cap_wmeParams));
	RTWN_UNLOCK(sc);
	IEEE80211_UNLOCK(ic);

	acm = 0;
	slottime = IEEE80211_GET_SLOTTIME(ic);

	RTWN_LOCK(sc);
	for (ac = WME_AC_BE; ac < WME_NUM_AC; ac++) {
		/* AIFS[AC] = AIFSN[AC] * aSlotTime + aSIFSTime. */
		aifs = wmep[ac].wmep_aifsn * slottime +
		    (IEEE80211_IS_CHAN_5GHZ(c) ?
			IEEE80211_DUR_OFDM_SIFS : IEEE80211_DUR_SIFS);
		rtwn_write_4(sc, wme2reg[ac],
		    SM(R92C_EDCA_PARAM_TXOP, wmep[ac].wmep_txopLimit) |
		    SM(R92C_EDCA_PARAM_ECWMIN, wmep[ac].wmep_logcwmin) |
		    SM(R92C_EDCA_PARAM_ECWMAX, wmep[ac].wmep_logcwmax) |
		    SM(R92C_EDCA_PARAM_AIFS, aifs));
		if (ac != WME_AC_BE)
			acm |= wmep[ac].wmep_acm << ac;
	}

	if (acm != 0)
		acm |= R92C_ACMHWCTRL_EN;
	rtwn_setbits_1(sc, R92C_ACMHWCTRL, R92C_ACMHWCTRL_ACM_MASK, acm);
	RTWN_UNLOCK(sc);

	return 0;
}

static void
rtwn_update_slot(struct ieee80211com *ic)
{
	rtwn_cmd_sleepable(ic->ic_softc, NULL, 0, rtwn_update_slot_cb);
}

static void
rtwn_update_slot_cb(struct rtwn_softc *sc, union sec_param *data)
{
	struct ieee80211com *ic = &sc->sc_ic;
	uint8_t slottime;

	slottime = IEEE80211_GET_SLOTTIME(ic);

	RTWN_DPRINTF(sc, RTWN_DEBUG_STATE, "%s: setting slot time to %uus\n",
	    __func__, slottime);

	rtwn_write_1(sc, R92C_SLOT, slottime);
	rtwn_update_aifs(sc, slottime);
}

static void
rtwn_update_aifs(struct rtwn_softc *sc, uint8_t slottime)
{
	struct ieee80211_channel *c = sc->sc_ic.ic_curchan;
	const struct wmeParams *wmep = sc->cap_wmeParams;
	uint8_t aifs, ac;

	for (ac = WME_AC_BE; ac < WME_NUM_AC; ac++) {
		/* AIFS[AC] = AIFSN[AC] * aSlotTime + aSIFSTime. */
		aifs = wmep[ac].wmep_aifsn * slottime +
		    (IEEE80211_IS_CHAN_5GHZ(c) ?
			IEEE80211_DUR_OFDM_SIFS : IEEE80211_DUR_SIFS);
		rtwn_write_1(sc, wme2reg[ac], aifs);
	}
}

static void
rtwn_update_promisc(struct ieee80211com *ic)
{
	struct rtwn_softc *sc = ic->ic_softc;

	RTWN_LOCK(sc);
	if (sc->sc_flags & RTWN_RUNNING)
		rtwn_set_promisc(sc);
	RTWN_UNLOCK(sc);
}

static void
rtwn_update_mcast(struct ieee80211com *ic)
{
	struct rtwn_softc *sc = ic->ic_softc;

	RTWN_LOCK(sc);
	if (sc->sc_flags & RTWN_RUNNING)
		rtwn_set_multi(sc);
	RTWN_UNLOCK(sc);
}

static int
rtwn_set_bssid(struct rtwn_softc *sc, const uint8_t *bssid, int id)
{
	int error;

	error = rtwn_write_4(sc, R92C_BSSID(id), le32dec(&bssid[0]));
	if (error != 0)
		return (error);
	error = rtwn_write_2(sc, R92C_BSSID(id) + 4, le16dec(&bssid[4]));

	return (error);
}

static int
rtwn_set_macaddr(struct rtwn_softc *sc, const uint8_t *addr, int id)
{
	int error;

	error = rtwn_write_4(sc, R92C_MACID(id), le32dec(&addr[0]));
	if (error != 0)
		return (error);
	error = rtwn_write_2(sc, R92C_MACID(id) + 4, le16dec(&addr[4]));

	return (error);
}

static struct ieee80211_node *
rtwn_node_alloc(struct ieee80211vap *vap,
    const uint8_t mac[IEEE80211_ADDR_LEN])
{
	struct rtwn_node *un;

	un = malloc(sizeof (struct rtwn_node), M_80211_NODE,
	    M_NOWAIT | M_ZERO);

	if (un == NULL)
		return NULL;

	un->id = RTWN_MACID_UNDEFINED;
	un->avg_pwdb = -1;

	return &un->ni;
}

static void
rtwn_newassoc(struct ieee80211_node *ni, int isnew __unused)
{
	struct rtwn_softc *sc = ni->ni_ic->ic_softc;
	struct rtwn_node *un = RTWN_NODE(ni);
	int id;

	if (un->id != RTWN_MACID_UNDEFINED)
		return;

	RTWN_NT_LOCK(sc);
	for (id = 0; id <= sc->macid_limit; id++) {
		if (id != RTWN_MACID_BC && sc->node_list[id] == NULL) {
			un->id = id;
			sc->node_list[id] = ni;
			break;
		}
	}
	RTWN_NT_UNLOCK(sc);

	if (id > sc->macid_limit) {
		device_printf(sc->sc_dev, "%s: node table is full\n",
		    __func__);
		return;
	}

	/* Notify firmware. */
	id |= RTWN_MACID_VALID;
	rtwn_cmd_sleepable(sc, &id, sizeof(id), rtwn_set_media_status);
}

static void
rtwn_node_free(struct ieee80211_node *ni)
{
	struct rtwn_softc *sc = ni->ni_ic->ic_softc;
	struct rtwn_node *un = RTWN_NODE(ni);

	RTWN_NT_LOCK(sc);
	if (un->id != RTWN_MACID_UNDEFINED) {
		sc->node_list[un->id] = NULL;
		rtwn_cmd_sleepable(sc, &un->id, sizeof(un->id),
		    rtwn_set_media_status);
	}
	RTWN_NT_UNLOCK(sc);

	sc->sc_node_free(ni);
}

static void
rtwn_init_beacon_reg(struct rtwn_softc *sc)
{
	rtwn_write_1(sc, R92C_BCN_CTRL(0), R92C_BCN_CTRL_DIS_TSF_UDT0);
	rtwn_write_1(sc, R92C_BCN_CTRL(1), R92C_BCN_CTRL_DIS_TSF_UDT0);
	rtwn_write_2(sc, R92C_TBTT_PROHIBIT, 0x6404);
	rtwn_write_1(sc, R92C_DRVERLYINT, 0x05);
	rtwn_write_1(sc, R92C_BCNDMATIM, 0x02);
	rtwn_write_2(sc, R92C_BCNTCFG, 0x660f);
}

static int
rtwn_init(struct rtwn_softc *sc)
{
	struct ieee80211com *ic = &sc->sc_ic;
	int i, error;

	RTWN_LOCK(sc);
	if (sc->sc_flags & RTWN_RUNNING) {
		RTWN_UNLOCK(sc);
		return (0);
	}
	sc->sc_flags |= RTWN_STARTED;

	/* Power on adapter. */
	error = rtwn_power_on(sc);
	if (error != 0)
		goto fail;

#ifndef RTWN_WITHOUT_UCODE
	/* Load 8051 microcode. */
	error = rtwn_load_firmware(sc);
	if (error == 0)
		sc->sc_flags |= RTWN_FW_LOADED;

	/* Init firmware commands ring. */
	sc->fwcur = 0;
#endif

	/* Initialize MAC block. */
	error = rtwn_mac_init(sc);
	if (error != 0) {
		device_printf(sc->sc_dev,
		    "%s: error while initializing MAC block\n", __func__);
		goto fail;
	}

	/* Initialize DMA. */
	error = rtwn_dma_init(sc);
	if (error != 0)
		goto fail;

	/* Drop incorrect TX (USB). */
	rtwn_drop_incorrect_tx(sc);

	/* Set info size in Rx descriptors (in 64-bit words). */
	rtwn_write_1(sc, R92C_RX_DRVINFO_SZ, R92C_RX_DRVINFO_SZ_DEF);

	/* Init interrupts. */
	rtwn_init_intr(sc);

	for (i = 0; i < nitems(sc->vaps); i++) {
		struct rtwn_vap *uvp = sc->vaps[i];

		/* Set initial network type. */
		rtwn_set_mode(sc, R92C_MSR_NOLINK, i);

		if (uvp == NULL)
			continue;

		/* Set MAC address. */
		error = rtwn_set_macaddr(sc, uvp->vap.iv_myaddr, uvp->id);
		if (error != 0)
			goto fail;
	}

	/* Initialize Rx filter. */
	rtwn_rxfilter_init(sc);

	/* Set short/long retry limits. */
	rtwn_write_2(sc, R92C_RL,
	    SM(R92C_RL_SRL, 0x30) | SM(R92C_RL_LRL, 0x30));

	/* Initialize EDCA parameters. */
	rtwn_init_edca(sc);

	rtwn_setbits_1(sc, R92C_FWHW_TXQ_CTRL, 0,
	    R92C_FWHW_TXQ_CTRL_AMPDU_RTY_NEW);
	/* Set ACK timeout. */
	rtwn_write_1(sc, R92C_ACKTO, sc->ackto);

	/* Setup aggregation. */
	/* Tx aggregation. */
	rtwn_init_tx_agg(sc);
	rtwn_init_rx_agg(sc);

	/* Initialize beacon parameters. */
	rtwn_init_beacon_reg(sc);

	/* Init A-MPDU parameters. */
	rtwn_init_ampdu(sc);

	/* Init MACTXEN / MACRXEN after setting RxFF boundary. */
	rtwn_setbits_1(sc, R92C_CR, 0, R92C_CR_MACTXEN | R92C_CR_MACRXEN);

	/* Initialize BB/RF blocks. */
	rtwn_init_bb(sc);
	rtwn_init_rf(sc);

	/* Initialize wireless band. */
	rtwn_set_chan(sc, ic->ic_curchan);

	/* Clear per-station keys table. */
	rtwn_init_cam(sc);

	/* Enable decryption / encryption. */
	rtwn_init_seccfg(sc);

	/* Install static keys (if any). */
	for (i = 0; i < nitems(sc->vaps); i++) {
		if (sc->vaps[i] != NULL) {
			error = rtwn_init_static_keys(sc, sc->vaps[i]);
			if (error != 0)
				goto fail;
		}
	}

	/* Initialize antenna selection. */
	rtwn_init_antsel(sc);

	/* Enable hardware sequence numbering. */
	rtwn_write_1(sc, R92C_HWSEQ_CTRL, R92C_TX_QUEUE_ALL);

	/* Disable BAR. */
	rtwn_write_4(sc, R92C_BAR_MODE_CTRL, 0x0201ffff);

	/* NAV limit. */
	rtwn_write_1(sc, R92C_NAV_UPPER, 0);

	/* Initialize GPIO setting. */
	rtwn_setbits_1(sc, R92C_GPIO_MUXCFG, R92C_GPIO_MUXCFG_ENBT, 0);

	/* Initialize MRR. */
	rtwn_mrr_init(sc);

	/* Device-specific post initialization. */
	rtwn_post_init(sc);

	rtwn_start_xfers(sc);

#ifndef D4054
	callout_reset(&sc->sc_watchdog_to, hz, rtwn_watchdog, sc);
#endif

	sc->sc_flags |= RTWN_RUNNING;
fail:
	RTWN_UNLOCK(sc);

	return (error);
}

static void
rtwn_stop(struct rtwn_softc *sc)
{

	RTWN_LOCK(sc);
	if (!(sc->sc_flags & RTWN_STARTED)) {
		RTWN_UNLOCK(sc);
		return;
	}

#ifndef D4054
	callout_stop(&sc->sc_watchdog_to);
	sc->sc_tx_timer = 0;
#endif
	sc->sc_flags &= ~(RTWN_STARTED | RTWN_RUNNING | RTWN_FW_LOADED);
	sc->sc_flags &= ~RTWN_TEMP_MEASURED;
	sc->fwver = 0;
	sc->thcal_temp = 0;
	sc->cur_bcnq_id = RTWN_VAP_ID_INVALID;
	bzero(&sc->last_physt, sizeof(sc->last_physt));

#ifdef D4054
	ieee80211_tx_watchdog_stop(&sc->sc_ic);
#endif

	rtwn_abort_xfers(sc);
	rtwn_drain_mbufq(sc);
	rtwn_power_off(sc);
	rtwn_reset_lists(sc, NULL);
	RTWN_UNLOCK(sc);
}

MODULE_VERSION(rtwn, 2);
MODULE_DEPEND(rtwn, wlan, 1, 1, 1);
#ifndef RTWN_WITHOUT_UCODE
MODULE_DEPEND(rtwn, firmware, 1, 1, 1);
#endif