1 /* $OpenBSD: if_rsu.c,v 1.17 2013/04/15 09:23:01 mglocker Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 /*
19 * Driver for Realtek RTL8188SU/RTL8191SU/RTL8192SU.
20 *
21 * TODO:
22 * o tx a-mpdu
23 * o hostap / ibss / mesh
24 * o power-save operation
25 */
26
27 #include "opt_wlan.h"
28
29 #include <sys/param.h>
30 #include <sys/endian.h>
31 #include <sys/sockio.h>
32 #include <sys/malloc.h>
33 #include <sys/mbuf.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/bus.h>
39 #include <sys/firmware.h>
40 #include <sys/module.h>
41
42 #include <net/bpf.h>
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_arp.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/if_types.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/in_var.h>
53 #include <netinet/if_ether.h>
54 #include <netinet/ip.h>
55
56 #include <net80211/ieee80211_var.h>
57 #include <net80211/ieee80211_regdomain.h>
58 #include <net80211/ieee80211_radiotap.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include "usbdevs.h"
63
64 #include <dev/rtwn/if_rtwn_ridx.h> /* XXX */
65 #include <dev/usb/wlan/if_rsureg.h>
66
67 #define RSU_RATE_IS_CCK RTWN_RATE_IS_CCK
68
69 #ifdef USB_DEBUG
70 static int rsu_debug = 0;
71 SYSCTL_NODE(_hw_usb, OID_AUTO, rsu, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
72 "USB rsu");
73 SYSCTL_INT(_hw_usb_rsu, OID_AUTO, debug, CTLFLAG_RWTUN, &rsu_debug, 0,
74 "Debug level");
75 #define RSU_DPRINTF(_sc, _flg, ...) \
76 do \
77 if (((_flg) == (RSU_DEBUG_ANY)) || (rsu_debug & (_flg))) \
78 device_printf((_sc)->sc_dev, __VA_ARGS__); \
79 while (0)
80 #else
81 #define RSU_DPRINTF(_sc, _flg, ...)
82 #endif
83
84 static int rsu_enable_11n = 1;
85 TUNABLE_INT("hw.usb.rsu.enable_11n", &rsu_enable_11n);
86
87 #define RSU_DEBUG_ANY 0xffffffff
88 #define RSU_DEBUG_TX 0x00000001
89 #define RSU_DEBUG_RX 0x00000002
90 #define RSU_DEBUG_RESET 0x00000004
91 #define RSU_DEBUG_CALIB 0x00000008
92 #define RSU_DEBUG_STATE 0x00000010
93 #define RSU_DEBUG_SCAN 0x00000020
94 #define RSU_DEBUG_FWCMD 0x00000040
95 #define RSU_DEBUG_TXDONE 0x00000080
96 #define RSU_DEBUG_FW 0x00000100
97 #define RSU_DEBUG_FWDBG 0x00000200
98 #define RSU_DEBUG_AMPDU 0x00000400
99 #define RSU_DEBUG_KEY 0x00000800
100 #define RSU_DEBUG_USB 0x00001000
101
102 static const STRUCT_USB_HOST_ID rsu_devs[] = {
103 #define RSU_HT_NOT_SUPPORTED 0
104 #define RSU_HT_SUPPORTED 1
105 #define RSU_DEV_HT(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
106 RSU_HT_SUPPORTED) }
107 #define RSU_DEV(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
108 RSU_HT_NOT_SUPPORTED) }
109 RSU_DEV(ASUS, RTL8192SU),
110 RSU_DEV(AZUREWAVE, RTL8192SU_4),
111 RSU_DEV(SITECOMEU, WLA1000),
112 RSU_DEV_HT(ACCTON, RTL8192SU),
113 RSU_DEV_HT(ASUS, USBN10),
114 RSU_DEV_HT(AZUREWAVE, RTL8192SU_1),
115 RSU_DEV_HT(AZUREWAVE, RTL8192SU_2),
116 RSU_DEV_HT(AZUREWAVE, RTL8192SU_3),
117 RSU_DEV_HT(AZUREWAVE, RTL8192SU_5),
118 RSU_DEV_HT(BELKIN, RTL8192SU_1),
119 RSU_DEV_HT(BELKIN, RTL8192SU_2),
120 RSU_DEV_HT(BELKIN, RTL8192SU_3),
121 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_1),
122 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_2),
123 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_3),
124 RSU_DEV_HT(COREGA, RTL8192SU),
125 RSU_DEV_HT(DLINK2, DWA131A1),
126 RSU_DEV_HT(DLINK2, RTL8192SU_1),
127 RSU_DEV_HT(DLINK2, RTL8192SU_2),
128 RSU_DEV_HT(EDIMAX, RTL8192SU_1),
129 RSU_DEV_HT(EDIMAX, RTL8192SU_2),
130 RSU_DEV_HT(EDIMAX, EW7622UMN),
131 RSU_DEV_HT(GUILLEMOT, HWGUN54),
132 RSU_DEV_HT(GUILLEMOT, HWNUM300),
133 RSU_DEV_HT(HAWKING, RTL8192SU_1),
134 RSU_DEV_HT(HAWKING, RTL8192SU_2),
135 RSU_DEV_HT(PLANEX2, GWUSNANO),
136 RSU_DEV_HT(REALTEK, RTL8171),
137 RSU_DEV_HT(REALTEK, RTL8172),
138 RSU_DEV_HT(REALTEK, RTL8173),
139 RSU_DEV_HT(REALTEK, RTL8174),
140 RSU_DEV_HT(REALTEK, RTL8192SU),
141 RSU_DEV_HT(REALTEK, RTL8712),
142 RSU_DEV_HT(REALTEK, RTL8713),
143 RSU_DEV_HT(SENAO, RTL8192SU_1),
144 RSU_DEV_HT(SENAO, RTL8192SU_2),
145 RSU_DEV_HT(SITECOMEU, WL349V1),
146 RSU_DEV_HT(SITECOMEU, WL353),
147 RSU_DEV_HT(SITECOMEU, RTL8188S),
148 RSU_DEV_HT(SWEEX2, LW154),
149 RSU_DEV_HT(TRENDNET, TEW646UBH),
150 #undef RSU_DEV_HT
151 #undef RSU_DEV
152 };
153
154 static device_probe_t rsu_match;
155 static device_attach_t rsu_attach;
156 static device_detach_t rsu_detach;
157 static usb_callback_t rsu_bulk_tx_callback_be_bk;
158 static usb_callback_t rsu_bulk_tx_callback_vi_vo;
159 static usb_callback_t rsu_bulk_tx_callback_h2c;
160 static usb_callback_t rsu_bulk_rx_callback;
161 static usb_error_t rsu_do_request(struct rsu_softc *,
162 struct usb_device_request *, void *);
163 static struct ieee80211vap *
164 rsu_vap_create(struct ieee80211com *, const char name[IFNAMSIZ],
165 int, enum ieee80211_opmode, int,
166 const uint8_t bssid[IEEE80211_ADDR_LEN],
167 const uint8_t mac[IEEE80211_ADDR_LEN]);
168 static void rsu_vap_delete(struct ieee80211vap *);
169 static void rsu_scan_start(struct ieee80211com *);
170 static void rsu_scan_end(struct ieee80211com *);
171 static void rsu_getradiocaps(struct ieee80211com *, int, int *,
172 struct ieee80211_channel[]);
173 static void rsu_set_channel(struct ieee80211com *);
174 static void rsu_scan_curchan(struct ieee80211_scan_state *, unsigned long);
175 static void rsu_scan_mindwell(struct ieee80211_scan_state *);
176 static void rsu_update_promisc(struct ieee80211com *);
177 static uint8_t rsu_get_multi_pos(const uint8_t[]);
178 static void rsu_set_multi(struct rsu_softc *);
179 static void rsu_update_mcast(struct ieee80211com *);
180 static int rsu_alloc_rx_list(struct rsu_softc *);
181 static void rsu_free_rx_list(struct rsu_softc *);
182 static int rsu_alloc_tx_list(struct rsu_softc *);
183 static void rsu_free_tx_list(struct rsu_softc *);
184 static void rsu_free_list(struct rsu_softc *, struct rsu_data [], int);
185 static struct rsu_data *_rsu_getbuf(struct rsu_softc *);
186 static struct rsu_data *rsu_getbuf(struct rsu_softc *);
187 static void rsu_freebuf(struct rsu_softc *, struct rsu_data *);
188 static int rsu_write_region_1(struct rsu_softc *, uint16_t, uint8_t *,
189 int);
190 static void rsu_write_1(struct rsu_softc *, uint16_t, uint8_t);
191 static void rsu_write_2(struct rsu_softc *, uint16_t, uint16_t);
192 static void rsu_write_4(struct rsu_softc *, uint16_t, uint32_t);
193 static int rsu_read_region_1(struct rsu_softc *, uint16_t, uint8_t *,
194 int);
195 static uint8_t rsu_read_1(struct rsu_softc *, uint16_t);
196 static uint16_t rsu_read_2(struct rsu_softc *, uint16_t);
197 static uint32_t rsu_read_4(struct rsu_softc *, uint16_t);
198 static int rsu_fw_iocmd(struct rsu_softc *, uint32_t);
199 static uint8_t rsu_efuse_read_1(struct rsu_softc *, uint16_t);
200 static int rsu_read_rom(struct rsu_softc *);
201 static int rsu_fw_cmd(struct rsu_softc *, uint8_t, void *, int);
202 static void rsu_calib_task(void *, int);
203 static void rsu_tx_task(void *, int);
204 static void rsu_set_led(struct rsu_softc *, int);
205 static int rsu_monitor_newstate(struct ieee80211vap *,
206 enum ieee80211_state, int);
207 static int rsu_newstate(struct ieee80211vap *, enum ieee80211_state, int);
208 static int rsu_key_alloc(struct ieee80211vap *, struct ieee80211_key *,
209 ieee80211_keyix *, ieee80211_keyix *);
210 static int rsu_process_key(struct ieee80211vap *,
211 const struct ieee80211_key *, int);
212 static int rsu_key_set(struct ieee80211vap *,
213 const struct ieee80211_key *);
214 static int rsu_key_delete(struct ieee80211vap *,
215 const struct ieee80211_key *);
216 static int rsu_cam_read(struct rsu_softc *, uint8_t, uint32_t *);
217 static void rsu_cam_write(struct rsu_softc *, uint8_t, uint32_t);
218 static int rsu_key_check(struct rsu_softc *, ieee80211_keyix, int);
219 static uint8_t rsu_crypto_mode(struct rsu_softc *, u_int, int);
220 static int rsu_set_key_group(struct rsu_softc *,
221 const struct ieee80211_key *);
222 static int rsu_set_key_pair(struct rsu_softc *,
223 const struct ieee80211_key *);
224 static int rsu_reinit_static_keys(struct rsu_softc *);
225 static int rsu_delete_key(struct rsu_softc *sc, ieee80211_keyix);
226 static void rsu_delete_key_pair_cb(void *, int);
227 static int rsu_site_survey(struct rsu_softc *,
228 struct ieee80211_scan_ssid *);
229 static int rsu_join_bss(struct rsu_softc *, struct ieee80211_node *);
230 static int rsu_disconnect(struct rsu_softc *);
231 static int rsu_hwrssi_to_rssi(struct rsu_softc *, int hw_rssi);
232 static void rsu_event_survey(struct rsu_softc *, uint8_t *, int);
233 static void rsu_event_join_bss(struct rsu_softc *, uint8_t *, int);
234 static void rsu_rx_event(struct rsu_softc *, uint8_t, uint8_t *, int);
235 static void rsu_rx_multi_event(struct rsu_softc *, uint8_t *, int);
236 static int8_t rsu_get_rssi(struct rsu_softc *, int, void *);
237 static struct mbuf * rsu_rx_copy_to_mbuf(struct rsu_softc *,
238 struct r92s_rx_stat *, int);
239 static uint32_t rsu_get_tsf_low(struct rsu_softc *);
240 static uint32_t rsu_get_tsf_high(struct rsu_softc *);
241 static struct ieee80211_node * rsu_rx_frame(struct rsu_softc *, struct mbuf *);
242 static struct mbuf * rsu_rx_multi_frame(struct rsu_softc *, uint8_t *, int);
243 static struct mbuf *
244 rsu_rxeof(struct usb_xfer *, struct rsu_data *);
245 static void rsu_txeof(struct usb_xfer *, struct rsu_data *);
246 static int rsu_raw_xmit(struct ieee80211_node *, struct mbuf *,
247 const struct ieee80211_bpf_params *);
248 static void rsu_rxfilter_init(struct rsu_softc *);
249 static void rsu_rxfilter_set(struct rsu_softc *, uint32_t, uint32_t);
250 static void rsu_rxfilter_refresh(struct rsu_softc *);
251 static int rsu_init(struct rsu_softc *);
252 static int rsu_tx_start(struct rsu_softc *, struct ieee80211_node *,
253 struct mbuf *, struct rsu_data *);
254 static int rsu_transmit(struct ieee80211com *, struct mbuf *);
255 static void rsu_start(struct rsu_softc *);
256 static void _rsu_start(struct rsu_softc *);
257 static int rsu_ioctl_net(struct ieee80211com *, u_long, void *);
258 static void rsu_parent(struct ieee80211com *);
259 static void rsu_stop(struct rsu_softc *);
260 static void rsu_ms_delay(struct rsu_softc *, int);
261
262 static device_method_t rsu_methods[] = {
263 DEVMETHOD(device_probe, rsu_match),
264 DEVMETHOD(device_attach, rsu_attach),
265 DEVMETHOD(device_detach, rsu_detach),
266
267 DEVMETHOD_END
268 };
269
270 static driver_t rsu_driver = {
271 .name = "rsu",
272 .methods = rsu_methods,
273 .size = sizeof(struct rsu_softc)
274 };
275
276 DRIVER_MODULE(rsu, uhub, rsu_driver, NULL, NULL);
277 MODULE_DEPEND(rsu, wlan, 1, 1, 1);
278 MODULE_DEPEND(rsu, usb, 1, 1, 1);
279 MODULE_DEPEND(rsu, firmware, 1, 1, 1);
280 MODULE_VERSION(rsu, 1);
281 USB_PNP_HOST_INFO(rsu_devs);
282
283 static uint8_t rsu_wme_ac_xfer_map[4] = {
284 [WME_AC_BE] = RSU_BULK_TX_BE_BK,
285 [WME_AC_BK] = RSU_BULK_TX_BE_BK,
286 [WME_AC_VI] = RSU_BULK_TX_VI_VO,
287 [WME_AC_VO] = RSU_BULK_TX_VI_VO,
288 };
289
290 /* XXX hard-coded */
291 #define RSU_H2C_ENDPOINT 3
292
293 static const struct usb_config rsu_config[RSU_N_TRANSFER] = {
294 [RSU_BULK_RX] = {
295 .type = UE_BULK,
296 .endpoint = UE_ADDR_ANY,
297 .direction = UE_DIR_IN,
298 .bufsize = RSU_RXBUFSZ,
299 .flags = {
300 .pipe_bof = 1,
301 .short_xfer_ok = 1
302 },
303 .callback = rsu_bulk_rx_callback
304 },
305 [RSU_BULK_TX_BE_BK] = {
306 .type = UE_BULK,
307 .endpoint = 0x06,
308 .direction = UE_DIR_OUT,
309 .bufsize = RSU_TXBUFSZ,
310 .flags = {
311 .ext_buffer = 1,
312 .pipe_bof = 1,
313 .force_short_xfer = 1
314 },
315 .callback = rsu_bulk_tx_callback_be_bk,
316 .timeout = RSU_TX_TIMEOUT
317 },
318 [RSU_BULK_TX_VI_VO] = {
319 .type = UE_BULK,
320 .endpoint = 0x04,
321 .direction = UE_DIR_OUT,
322 .bufsize = RSU_TXBUFSZ,
323 .flags = {
324 .ext_buffer = 1,
325 .pipe_bof = 1,
326 .force_short_xfer = 1
327 },
328 .callback = rsu_bulk_tx_callback_vi_vo,
329 .timeout = RSU_TX_TIMEOUT
330 },
331 [RSU_BULK_TX_H2C] = {
332 .type = UE_BULK,
333 .endpoint = 0x0d,
334 .direction = UE_DIR_OUT,
335 .bufsize = RSU_TXBUFSZ,
336 .flags = {
337 .ext_buffer = 1,
338 .pipe_bof = 1,
339 .short_xfer_ok = 1
340 },
341 .callback = rsu_bulk_tx_callback_h2c,
342 .timeout = RSU_TX_TIMEOUT
343 },
344 };
345
346 static int
rsu_match(device_t self)347 rsu_match(device_t self)
348 {
349 struct usb_attach_arg *uaa = device_get_ivars(self);
350
351 if (uaa->usb_mode != USB_MODE_HOST ||
352 uaa->info.bIfaceIndex != 0 ||
353 uaa->info.bConfigIndex != 0)
354 return (ENXIO);
355
356 return (usbd_lookup_id_by_uaa(rsu_devs, sizeof(rsu_devs), uaa));
357 }
358
359 static int
rsu_send_mgmt(struct ieee80211_node * ni,int type,int arg)360 rsu_send_mgmt(struct ieee80211_node *ni, int type, int arg)
361 {
362
363 return (ENOTSUP);
364 }
365
366 static void
rsu_update_chw(struct ieee80211com * ic)367 rsu_update_chw(struct ieee80211com *ic)
368 {
369
370 }
371
372 /*
373 * notification from net80211 that it'd like to do A-MPDU on the given TID.
374 */
375 static int
rsu_ampdu_enable(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)376 rsu_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
377 {
378 struct rsu_softc *sc = ni->ni_ic->ic_softc;
379 struct r92s_add_ba_req req;
380
381 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: called, tid=%d\n",
382 __func__, tap->txa_tid);
383
384 /* Don't enable if it's requested or running */
385 if (IEEE80211_AMPDU_REQUESTED(tap))
386 return (0);
387 if (IEEE80211_AMPDU_RUNNING(tap))
388 return (0);
389
390 /* We've decided to send addba; so send it */
391 req.tid = htole32(tap->txa_tid);
392
393 /* Attempt net80211 state */
394 if (ieee80211_ampdu_tx_request_ext(ni, tap->txa_tid) != 1)
395 return (0);
396
397 /* Send the firmware command */
398 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU,
399 "%s: establishing AMPDU TX for TID %d\n",
400 __func__,
401 tap->txa_tid);
402
403 RSU_LOCK(sc);
404 if (rsu_fw_cmd(sc, R92S_CMD_ADDBA_REQ, &req, sizeof(req)) != 0) {
405 RSU_UNLOCK(sc);
406 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: AMPDU TX cmd failure\n",
407 __func__);
408 /* Mark failure */
409 ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 0);
410 /* Return 0, we've been driving this ourselves */
411 return (0);
412 }
413 RSU_UNLOCK(sc);
414
415 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: AMPDU TX cmd success\n",
416 __func__);
417
418 /* Mark success; we don't get any further notifications */
419 ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 1);
420
421 /* Return 0, we've been driving this ourselves */
422 return (0);
423 }
424
425 static int
rsu_wme_update(struct ieee80211com * ic)426 rsu_wme_update(struct ieee80211com *ic)
427 {
428
429 /* Firmware handles this; not our problem */
430 return (0);
431 }
432
433 static int
rsu_attach(device_t self)434 rsu_attach(device_t self)
435 {
436 struct usb_attach_arg *uaa = device_get_ivars(self);
437 struct rsu_softc *sc = device_get_softc(self);
438 struct ieee80211com *ic = &sc->sc_ic;
439 int error;
440 uint8_t iface_index;
441 struct usb_interface *iface;
442 const char *rft;
443
444 device_set_usb_desc(self);
445 sc->sc_udev = uaa->device;
446 sc->sc_dev = self;
447 sc->sc_rx_checksum_enable = 1;
448 if (rsu_enable_11n)
449 sc->sc_ht = !! (USB_GET_DRIVER_INFO(uaa) & RSU_HT_SUPPORTED);
450
451 /* Get number of endpoints */
452 iface = usbd_get_iface(sc->sc_udev, 0);
453 sc->sc_nendpoints = iface->idesc->bNumEndpoints;
454
455 /* Endpoints are hard-coded for now, so enforce 4-endpoint only */
456 if (sc->sc_nendpoints != 4) {
457 device_printf(sc->sc_dev,
458 "the driver currently only supports 4-endpoint devices\n");
459 return (ENXIO);
460 }
461
462 mtx_init(&sc->sc_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
463 MTX_DEF);
464 RSU_DELKEY_BMAP_LOCK_INIT(sc);
465 TIMEOUT_TASK_INIT(taskqueue_thread, &sc->calib_task, 0,
466 rsu_calib_task, sc);
467 TASK_INIT(&sc->del_key_task, 0, rsu_delete_key_pair_cb, sc);
468 TASK_INIT(&sc->tx_task, 0, rsu_tx_task, sc);
469 mbufq_init(&sc->sc_snd, ifqmaxlen);
470
471 /* Allocate Tx/Rx buffers. */
472 error = rsu_alloc_rx_list(sc);
473 if (error != 0) {
474 device_printf(sc->sc_dev, "could not allocate Rx buffers\n");
475 goto fail_usb;
476 }
477
478 error = rsu_alloc_tx_list(sc);
479 if (error != 0) {
480 device_printf(sc->sc_dev, "could not allocate Tx buffers\n");
481 rsu_free_rx_list(sc);
482 goto fail_usb;
483 }
484
485 iface_index = 0;
486 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
487 rsu_config, RSU_N_TRANSFER, sc, &sc->sc_mtx);
488 if (error) {
489 device_printf(sc->sc_dev,
490 "could not allocate USB transfers, err=%s\n",
491 usbd_errstr(error));
492 goto fail_usb;
493 }
494 RSU_LOCK(sc);
495 /* Read chip revision. */
496 sc->cut = MS(rsu_read_4(sc, R92S_PMC_FSM), R92S_PMC_FSM_CUT);
497 if (sc->cut != 3)
498 sc->cut = (sc->cut >> 1) + 1;
499 error = rsu_read_rom(sc);
500 RSU_UNLOCK(sc);
501 if (error != 0) {
502 device_printf(self, "could not read ROM\n");
503 goto fail_rom;
504 }
505
506 /* Figure out TX/RX streams */
507 switch (sc->rom[84]) {
508 case 0x0:
509 sc->sc_rftype = RTL8712_RFCONFIG_1T1R;
510 sc->sc_nrxstream = 1;
511 sc->sc_ntxstream = 1;
512 rft = "1T1R";
513 break;
514 case 0x1:
515 sc->sc_rftype = RTL8712_RFCONFIG_1T2R;
516 sc->sc_nrxstream = 2;
517 sc->sc_ntxstream = 1;
518 rft = "1T2R";
519 break;
520 case 0x2:
521 sc->sc_rftype = RTL8712_RFCONFIG_2T2R;
522 sc->sc_nrxstream = 2;
523 sc->sc_ntxstream = 2;
524 rft = "2T2R";
525 break;
526 case 0x3: /* "green" NIC */
527 sc->sc_rftype = RTL8712_RFCONFIG_1T2R;
528 sc->sc_nrxstream = 2;
529 sc->sc_ntxstream = 1;
530 rft = "1T2R ('green')";
531 break;
532 default:
533 device_printf(sc->sc_dev,
534 "%s: unknown board type (rfconfig=0x%02x)\n",
535 __func__,
536 sc->rom[84]);
537 goto fail_rom;
538 }
539
540 IEEE80211_ADDR_COPY(ic->ic_macaddr, &sc->rom[0x12]);
541 device_printf(self, "MAC/BB RTL8712 cut %d %s\n", sc->cut, rft);
542
543 ic->ic_softc = sc;
544 ic->ic_name = device_get_nameunit(self);
545 ic->ic_phytype = IEEE80211_T_OFDM; /* Not only, but not used. */
546 ic->ic_opmode = IEEE80211_M_STA; /* Default to BSS mode. */
547
548 /* Set device capabilities. */
549 ic->ic_caps =
550 IEEE80211_C_STA | /* station mode */
551 IEEE80211_C_MONITOR | /* monitor mode supported */
552 #if 0
553 IEEE80211_C_BGSCAN | /* Background scan. */
554 #endif
555 IEEE80211_C_SHPREAMBLE | /* Short preamble supported. */
556 IEEE80211_C_WME | /* WME/QoS */
557 IEEE80211_C_SHSLOT | /* Short slot time supported. */
558 IEEE80211_C_WPA; /* WPA/RSN. */
559
560 ic->ic_cryptocaps =
561 IEEE80211_CRYPTO_WEP |
562 IEEE80211_CRYPTO_TKIP |
563 IEEE80211_CRYPTO_AES_CCM;
564
565 /* Check if HT support is present. */
566 if (sc->sc_ht) {
567 device_printf(sc->sc_dev, "%s: enabling 11n\n", __func__);
568
569 /* Enable basic HT */
570 ic->ic_htcaps = IEEE80211_HTC_HT |
571 IEEE80211_HTC_AMPDU |
572 IEEE80211_HTC_AMSDU |
573 IEEE80211_HTCAP_MAXAMSDU_3839 |
574 IEEE80211_HTCAP_SMPS_OFF;
575 ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40;
576
577 /* set number of spatial streams */
578 ic->ic_txstream = sc->sc_ntxstream;
579 ic->ic_rxstream = sc->sc_nrxstream;
580 }
581 ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
582 ic->ic_flags_ext |= IEEE80211_FEXT_SEQNO_OFFLOAD;
583
584 rsu_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
585 ic->ic_channels);
586
587 ieee80211_ifattach(ic);
588 ic->ic_raw_xmit = rsu_raw_xmit;
589 ic->ic_scan_start = rsu_scan_start;
590 ic->ic_scan_end = rsu_scan_end;
591 ic->ic_getradiocaps = rsu_getradiocaps;
592 ic->ic_set_channel = rsu_set_channel;
593 ic->ic_scan_curchan = rsu_scan_curchan;
594 ic->ic_scan_mindwell = rsu_scan_mindwell;
595 ic->ic_vap_create = rsu_vap_create;
596 ic->ic_vap_delete = rsu_vap_delete;
597 ic->ic_update_promisc = rsu_update_promisc;
598 ic->ic_update_mcast = rsu_update_mcast;
599 ic->ic_ioctl = rsu_ioctl_net;
600 ic->ic_parent = rsu_parent;
601 ic->ic_transmit = rsu_transmit;
602 ic->ic_send_mgmt = rsu_send_mgmt;
603 ic->ic_update_chw = rsu_update_chw;
604 ic->ic_ampdu_enable = rsu_ampdu_enable;
605 ic->ic_wme.wme_update = rsu_wme_update;
606
607 ieee80211_radiotap_attach(ic, &sc->sc_txtap.wt_ihdr,
608 sizeof(sc->sc_txtap), RSU_TX_RADIOTAP_PRESENT,
609 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
610 RSU_RX_RADIOTAP_PRESENT);
611
612 if (bootverbose)
613 ieee80211_announce(ic);
614
615 return (0);
616
617 fail_rom:
618 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
619 fail_usb:
620 mtx_destroy(&sc->sc_mtx);
621 return (ENXIO);
622 }
623
624 static int
rsu_detach(device_t self)625 rsu_detach(device_t self)
626 {
627 struct rsu_softc *sc = device_get_softc(self);
628 struct ieee80211com *ic = &sc->sc_ic;
629
630 rsu_stop(sc);
631
632 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
633
634 /*
635 * Free buffers /before/ we detach from net80211, else node
636 * references to destroyed vaps will lead to a panic.
637 */
638 /* Free Tx/Rx buffers. */
639 RSU_LOCK(sc);
640 rsu_free_tx_list(sc);
641 rsu_free_rx_list(sc);
642 RSU_UNLOCK(sc);
643
644 /* Frames are freed; detach from net80211 */
645 ieee80211_ifdetach(ic);
646
647 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
648 taskqueue_drain(taskqueue_thread, &sc->del_key_task);
649 taskqueue_drain(taskqueue_thread, &sc->tx_task);
650
651 RSU_DELKEY_BMAP_LOCK_DESTROY(sc);
652 mtx_destroy(&sc->sc_mtx);
653
654 return (0);
655 }
656
657 static usb_error_t
rsu_do_request(struct rsu_softc * sc,struct usb_device_request * req,void * data)658 rsu_do_request(struct rsu_softc *sc, struct usb_device_request *req,
659 void *data)
660 {
661 usb_error_t err;
662 int ntries = 10;
663
664 RSU_ASSERT_LOCKED(sc);
665
666 while (ntries--) {
667 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
668 req, data, 0, NULL, 250 /* ms */);
669 if (err == 0 || err == USB_ERR_NOT_CONFIGURED)
670 break;
671 RSU_DPRINTF(sc, RSU_DEBUG_USB,
672 "Control request failed, %s (retries left: %d)\n",
673 usbd_errstr(err), ntries);
674 rsu_ms_delay(sc, 10);
675 }
676
677 return (err);
678 }
679
680 static struct ieee80211vap *
rsu_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])681 rsu_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
682 enum ieee80211_opmode opmode, int flags,
683 const uint8_t bssid[IEEE80211_ADDR_LEN],
684 const uint8_t mac[IEEE80211_ADDR_LEN])
685 {
686 struct rsu_softc *sc = ic->ic_softc;
687 struct rsu_vap *uvp;
688 struct ieee80211vap *vap;
689 if_t ifp;
690
691 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
692 return (NULL);
693
694 uvp = malloc(sizeof(struct rsu_vap), M_80211_VAP, M_WAITOK | M_ZERO);
695 vap = &uvp->vap;
696
697 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
698 flags, bssid) != 0) {
699 /* out of memory */
700 free(uvp, M_80211_VAP);
701 return (NULL);
702 }
703
704 ifp = vap->iv_ifp;
705 if_setcapabilities(ifp, IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
706 RSU_LOCK(sc);
707 if (sc->sc_rx_checksum_enable)
708 if_setcapenablebit(ifp, IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6, 0);
709 RSU_UNLOCK(sc);
710
711 /* override state transition machine */
712 uvp->newstate = vap->iv_newstate;
713 if (opmode == IEEE80211_M_MONITOR)
714 vap->iv_newstate = rsu_monitor_newstate;
715 else
716 vap->iv_newstate = rsu_newstate;
717 vap->iv_key_alloc = rsu_key_alloc;
718 vap->iv_key_set = rsu_key_set;
719 vap->iv_key_delete = rsu_key_delete;
720
721 /* Limits from the r92su driver */
722 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_16;
723 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K;
724
725 /* complete setup */
726 ieee80211_vap_attach(vap, ieee80211_media_change,
727 ieee80211_media_status, mac);
728 ic->ic_opmode = opmode;
729
730 return (vap);
731 }
732
733 static void
rsu_vap_delete(struct ieee80211vap * vap)734 rsu_vap_delete(struct ieee80211vap *vap)
735 {
736 struct rsu_vap *uvp = RSU_VAP(vap);
737
738 ieee80211_vap_detach(vap);
739 free(uvp, M_80211_VAP);
740 }
741
742 static void
rsu_scan_start(struct ieee80211com * ic)743 rsu_scan_start(struct ieee80211com *ic)
744 {
745 struct rsu_softc *sc = ic->ic_softc;
746 struct ieee80211_scan_state *ss = ic->ic_scan;
747 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
748 int error;
749
750 /* Scanning is done by the firmware. */
751 RSU_LOCK(sc);
752 sc->sc_active_scan = !!(ss->ss_flags & IEEE80211_SCAN_ACTIVE);
753 /* XXX TODO: force awake if in network-sleep? */
754 error = rsu_site_survey(sc, ss->ss_nssid > 0 ? &ss->ss_ssid[0] : NULL);
755 RSU_UNLOCK(sc);
756 if (error != 0) {
757 device_printf(sc->sc_dev,
758 "could not send site survey command\n");
759 ieee80211_cancel_scan(vap);
760 }
761 }
762
763 static void
rsu_scan_end(struct ieee80211com * ic)764 rsu_scan_end(struct ieee80211com *ic)
765 {
766 /* Nothing to do here. */
767 }
768
769 static void
rsu_getradiocaps(struct ieee80211com * ic,int maxchans,int * nchans,struct ieee80211_channel chans[])770 rsu_getradiocaps(struct ieee80211com *ic,
771 int maxchans, int *nchans, struct ieee80211_channel chans[])
772 {
773 struct rsu_softc *sc = ic->ic_softc;
774 uint8_t bands[IEEE80211_MODE_BYTES];
775
776 /* Set supported .11b and .11g rates. */
777 memset(bands, 0, sizeof(bands));
778 setbit(bands, IEEE80211_MODE_11B);
779 setbit(bands, IEEE80211_MODE_11G);
780 if (sc->sc_ht)
781 setbit(bands, IEEE80211_MODE_11NG);
782 ieee80211_add_channels_default_2ghz(chans, maxchans, nchans,
783 bands, (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) ?
784 NET80211_CBW_FLAG_HT40 : 0);
785 }
786
787 static void
rsu_set_channel(struct ieee80211com * ic)788 rsu_set_channel(struct ieee80211com *ic)
789 {
790 struct rsu_softc *sc = ic->ic_softc;
791
792 /*
793 * Only need to set the channel in Monitor mode. AP scanning and auth
794 * are already taken care of by their respective firmware commands.
795 */
796 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
797 struct r92s_set_channel cmd;
798 int error;
799
800 cmd.channel = IEEE80211_CHAN2IEEE(ic->ic_curchan);
801
802 RSU_LOCK(sc);
803 error = rsu_fw_cmd(sc, R92S_CMD_SET_CHANNEL, &cmd,
804 sizeof(cmd));
805 if (error != 0) {
806 device_printf(sc->sc_dev,
807 "%s: error %d setting channel\n", __func__,
808 error);
809 }
810 RSU_UNLOCK(sc);
811 }
812 }
813
814 static void
rsu_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)815 rsu_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
816 {
817 /* Scan is done in rsu_scan_start(). */
818 }
819
820 /**
821 * Called by the net80211 framework to indicate
822 * the minimum dwell time has been met, terminate the scan.
823 * We don't actually terminate the scan as the firmware will notify
824 * us when it's finished and we have no way to interrupt it.
825 */
826 static void
rsu_scan_mindwell(struct ieee80211_scan_state * ss)827 rsu_scan_mindwell(struct ieee80211_scan_state *ss)
828 {
829 /* NB: don't try to abort scan; wait for firmware to finish */
830 }
831
832 static void
rsu_update_promisc(struct ieee80211com * ic)833 rsu_update_promisc(struct ieee80211com *ic)
834 {
835 struct rsu_softc *sc = ic->ic_softc;
836
837 RSU_LOCK(sc);
838 if (sc->sc_running)
839 rsu_rxfilter_refresh(sc);
840 RSU_UNLOCK(sc);
841 }
842
843 /*
844 * The same as rtwn_get_multi_pos() / rtwn_set_multi().
845 */
846 static uint8_t
rsu_get_multi_pos(const uint8_t maddr[])847 rsu_get_multi_pos(const uint8_t maddr[])
848 {
849 uint64_t mask = 0x00004d101df481b4;
850 uint8_t pos = 0x27; /* initial value */
851 int i, j;
852
853 for (i = 0; i < IEEE80211_ADDR_LEN; i++)
854 for (j = (i == 0) ? 1 : 0; j < 8; j++)
855 if ((maddr[i] >> j) & 1)
856 pos ^= (mask >> (i * 8 + j - 1));
857
858 pos &= 0x3f;
859
860 return (pos);
861 }
862
863 static u_int
rsu_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)864 rsu_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
865 {
866 uint32_t *mfilt = arg;
867 uint8_t pos;
868
869 pos = rsu_get_multi_pos(LLADDR(sdl));
870 mfilt[pos / 32] |= (1 << (pos % 32));
871
872 return (1);
873 }
874
875 static void
rsu_set_multi(struct rsu_softc * sc)876 rsu_set_multi(struct rsu_softc *sc)
877 {
878 struct ieee80211com *ic = &sc->sc_ic;
879 uint32_t mfilt[2];
880
881 RSU_ASSERT_LOCKED(sc);
882
883 /* general structure was copied from ath(4). */
884 if (ic->ic_allmulti == 0) {
885 struct ieee80211vap *vap;
886
887 /*
888 * Merge multicast addresses to form the hardware filter.
889 */
890 mfilt[0] = mfilt[1] = 0;
891 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
892 if_foreach_llmaddr(vap->iv_ifp, rsu_hash_maddr, &mfilt);
893 } else
894 mfilt[0] = mfilt[1] = ~0;
895
896 rsu_write_4(sc, R92S_MAR + 0, mfilt[0]);
897 rsu_write_4(sc, R92S_MAR + 4, mfilt[1]);
898
899 RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: MC filter %08x:%08x\n",
900 __func__, mfilt[0], mfilt[1]);
901 }
902
903 static void
rsu_update_mcast(struct ieee80211com * ic)904 rsu_update_mcast(struct ieee80211com *ic)
905 {
906 struct rsu_softc *sc = ic->ic_softc;
907
908 RSU_LOCK(sc);
909 if (sc->sc_running)
910 rsu_set_multi(sc);
911 RSU_UNLOCK(sc);
912 }
913
914 static int
rsu_alloc_list(struct rsu_softc * sc,struct rsu_data data[],int ndata,int maxsz)915 rsu_alloc_list(struct rsu_softc *sc, struct rsu_data data[],
916 int ndata, int maxsz)
917 {
918 int i, error;
919
920 for (i = 0; i < ndata; i++) {
921 struct rsu_data *dp = &data[i];
922 dp->sc = sc;
923 dp->m = NULL;
924 dp->buf = malloc(maxsz, M_USBDEV, M_NOWAIT);
925 if (dp->buf == NULL) {
926 device_printf(sc->sc_dev,
927 "could not allocate buffer\n");
928 error = ENOMEM;
929 goto fail;
930 }
931 dp->ni = NULL;
932 }
933
934 return (0);
935 fail:
936 rsu_free_list(sc, data, ndata);
937 return (error);
938 }
939
940 static int
rsu_alloc_rx_list(struct rsu_softc * sc)941 rsu_alloc_rx_list(struct rsu_softc *sc)
942 {
943 int error, i;
944
945 error = rsu_alloc_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT,
946 RSU_RXBUFSZ);
947 if (error != 0)
948 return (error);
949
950 STAILQ_INIT(&sc->sc_rx_active);
951 STAILQ_INIT(&sc->sc_rx_inactive);
952
953 for (i = 0; i < RSU_RX_LIST_COUNT; i++)
954 STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], next);
955
956 return (0);
957 }
958
959 static int
rsu_alloc_tx_list(struct rsu_softc * sc)960 rsu_alloc_tx_list(struct rsu_softc *sc)
961 {
962 int error, i;
963
964 error = rsu_alloc_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT,
965 RSU_TXBUFSZ);
966 if (error != 0)
967 return (error);
968
969 STAILQ_INIT(&sc->sc_tx_inactive);
970
971 for (i = 0; i != RSU_N_TRANSFER; i++) {
972 STAILQ_INIT(&sc->sc_tx_active[i]);
973 STAILQ_INIT(&sc->sc_tx_pending[i]);
974 }
975
976 for (i = 0; i < RSU_TX_LIST_COUNT; i++) {
977 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i], next);
978 }
979
980 return (0);
981 }
982
983 static void
rsu_free_tx_list(struct rsu_softc * sc)984 rsu_free_tx_list(struct rsu_softc *sc)
985 {
986 int i;
987
988 /* prevent further allocations from TX list(s) */
989 STAILQ_INIT(&sc->sc_tx_inactive);
990
991 for (i = 0; i != RSU_N_TRANSFER; i++) {
992 STAILQ_INIT(&sc->sc_tx_active[i]);
993 STAILQ_INIT(&sc->sc_tx_pending[i]);
994 }
995
996 rsu_free_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT);
997 }
998
999 static void
rsu_free_rx_list(struct rsu_softc * sc)1000 rsu_free_rx_list(struct rsu_softc *sc)
1001 {
1002 /* prevent further allocations from RX list(s) */
1003 STAILQ_INIT(&sc->sc_rx_inactive);
1004 STAILQ_INIT(&sc->sc_rx_active);
1005
1006 rsu_free_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT);
1007 }
1008
1009 static void
rsu_free_list(struct rsu_softc * sc,struct rsu_data data[],int ndata)1010 rsu_free_list(struct rsu_softc *sc, struct rsu_data data[], int ndata)
1011 {
1012 int i;
1013
1014 for (i = 0; i < ndata; i++) {
1015 struct rsu_data *dp = &data[i];
1016
1017 if (dp->buf != NULL) {
1018 free(dp->buf, M_USBDEV);
1019 dp->buf = NULL;
1020 }
1021 if (dp->ni != NULL) {
1022 ieee80211_free_node(dp->ni);
1023 dp->ni = NULL;
1024 }
1025 }
1026 }
1027
1028 static struct rsu_data *
_rsu_getbuf(struct rsu_softc * sc)1029 _rsu_getbuf(struct rsu_softc *sc)
1030 {
1031 struct rsu_data *bf;
1032
1033 bf = STAILQ_FIRST(&sc->sc_tx_inactive);
1034 if (bf != NULL)
1035 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
1036 else
1037 bf = NULL;
1038 return (bf);
1039 }
1040
1041 static struct rsu_data *
rsu_getbuf(struct rsu_softc * sc)1042 rsu_getbuf(struct rsu_softc *sc)
1043 {
1044 struct rsu_data *bf;
1045
1046 RSU_ASSERT_LOCKED(sc);
1047
1048 bf = _rsu_getbuf(sc);
1049 if (bf == NULL) {
1050 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: no buffers\n", __func__);
1051 }
1052 return (bf);
1053 }
1054
1055 static void
rsu_freebuf(struct rsu_softc * sc,struct rsu_data * bf)1056 rsu_freebuf(struct rsu_softc *sc, struct rsu_data *bf)
1057 {
1058
1059 RSU_ASSERT_LOCKED(sc);
1060 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, bf, next);
1061 }
1062
1063 static int
rsu_write_region_1(struct rsu_softc * sc,uint16_t addr,uint8_t * buf,int len)1064 rsu_write_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
1065 int len)
1066 {
1067 usb_device_request_t req;
1068
1069 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1070 req.bRequest = R92S_REQ_REGS;
1071 USETW(req.wValue, addr);
1072 USETW(req.wIndex, 0);
1073 USETW(req.wLength, len);
1074
1075 return (rsu_do_request(sc, &req, buf));
1076 }
1077
1078 static void
rsu_write_1(struct rsu_softc * sc,uint16_t addr,uint8_t val)1079 rsu_write_1(struct rsu_softc *sc, uint16_t addr, uint8_t val)
1080 {
1081 rsu_write_region_1(sc, addr, &val, 1);
1082 }
1083
1084 static void
rsu_write_2(struct rsu_softc * sc,uint16_t addr,uint16_t val)1085 rsu_write_2(struct rsu_softc *sc, uint16_t addr, uint16_t val)
1086 {
1087 val = htole16(val);
1088 rsu_write_region_1(sc, addr, (uint8_t *)&val, 2);
1089 }
1090
1091 static void
rsu_write_4(struct rsu_softc * sc,uint16_t addr,uint32_t val)1092 rsu_write_4(struct rsu_softc *sc, uint16_t addr, uint32_t val)
1093 {
1094 val = htole32(val);
1095 rsu_write_region_1(sc, addr, (uint8_t *)&val, 4);
1096 }
1097
1098 static int
rsu_read_region_1(struct rsu_softc * sc,uint16_t addr,uint8_t * buf,int len)1099 rsu_read_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
1100 int len)
1101 {
1102 usb_device_request_t req;
1103
1104 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1105 req.bRequest = R92S_REQ_REGS;
1106 USETW(req.wValue, addr);
1107 USETW(req.wIndex, 0);
1108 USETW(req.wLength, len);
1109
1110 return (rsu_do_request(sc, &req, buf));
1111 }
1112
1113 static uint8_t
rsu_read_1(struct rsu_softc * sc,uint16_t addr)1114 rsu_read_1(struct rsu_softc *sc, uint16_t addr)
1115 {
1116 uint8_t val;
1117
1118 if (rsu_read_region_1(sc, addr, &val, 1) != 0)
1119 return (0xff);
1120 return (val);
1121 }
1122
1123 static uint16_t
rsu_read_2(struct rsu_softc * sc,uint16_t addr)1124 rsu_read_2(struct rsu_softc *sc, uint16_t addr)
1125 {
1126 uint16_t val;
1127
1128 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0)
1129 return (0xffff);
1130 return (le16toh(val));
1131 }
1132
1133 static uint32_t
rsu_read_4(struct rsu_softc * sc,uint16_t addr)1134 rsu_read_4(struct rsu_softc *sc, uint16_t addr)
1135 {
1136 uint32_t val;
1137
1138 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0)
1139 return (0xffffffff);
1140 return (le32toh(val));
1141 }
1142
1143 static int
rsu_fw_iocmd(struct rsu_softc * sc,uint32_t iocmd)1144 rsu_fw_iocmd(struct rsu_softc *sc, uint32_t iocmd)
1145 {
1146 int ntries;
1147
1148 rsu_write_4(sc, R92S_IOCMD_CTRL, iocmd);
1149 rsu_ms_delay(sc, 1);
1150 for (ntries = 0; ntries < 50; ntries++) {
1151 if (rsu_read_4(sc, R92S_IOCMD_CTRL) == 0)
1152 return (0);
1153 rsu_ms_delay(sc, 1);
1154 }
1155 return (ETIMEDOUT);
1156 }
1157
1158 static uint8_t
rsu_efuse_read_1(struct rsu_softc * sc,uint16_t addr)1159 rsu_efuse_read_1(struct rsu_softc *sc, uint16_t addr)
1160 {
1161 uint32_t reg;
1162 int ntries;
1163
1164 reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
1165 reg = RW(reg, R92S_EFUSE_CTRL_ADDR, addr);
1166 reg &= ~R92S_EFUSE_CTRL_VALID;
1167 rsu_write_4(sc, R92S_EFUSE_CTRL, reg);
1168 /* Wait for read operation to complete. */
1169 for (ntries = 0; ntries < 100; ntries++) {
1170 reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
1171 if (reg & R92S_EFUSE_CTRL_VALID)
1172 return (MS(reg, R92S_EFUSE_CTRL_DATA));
1173 rsu_ms_delay(sc, 1);
1174 }
1175 device_printf(sc->sc_dev,
1176 "could not read efuse byte at address 0x%x\n", addr);
1177 return (0xff);
1178 }
1179
1180 static int
rsu_read_rom(struct rsu_softc * sc)1181 rsu_read_rom(struct rsu_softc *sc)
1182 {
1183 uint8_t *rom = sc->rom;
1184 uint16_t addr = 0;
1185 uint32_t reg;
1186 uint8_t off, msk;
1187 int i;
1188
1189 /* Make sure that ROM type is eFuse and that autoload succeeded. */
1190 reg = rsu_read_1(sc, R92S_EE_9346CR);
1191 if ((reg & (R92S_9356SEL | R92S_EEPROM_EN)) != R92S_EEPROM_EN)
1192 return (EIO);
1193
1194 /* Turn on 2.5V to prevent eFuse leakage. */
1195 reg = rsu_read_1(sc, R92S_EFUSE_TEST + 3);
1196 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg | 0x80);
1197 rsu_ms_delay(sc, 1);
1198 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg & ~0x80);
1199
1200 /* Read full ROM image. */
1201 memset(&sc->rom, 0xff, sizeof(sc->rom));
1202 while (addr < 512) {
1203 reg = rsu_efuse_read_1(sc, addr);
1204 if (reg == 0xff)
1205 break;
1206 addr++;
1207 off = reg >> 4;
1208 msk = reg & 0xf;
1209 for (i = 0; i < 4; i++) {
1210 if (msk & (1 << i))
1211 continue;
1212 rom[off * 8 + i * 2 + 0] =
1213 rsu_efuse_read_1(sc, addr);
1214 addr++;
1215 rom[off * 8 + i * 2 + 1] =
1216 rsu_efuse_read_1(sc, addr);
1217 addr++;
1218 }
1219 }
1220 #ifdef USB_DEBUG
1221 if (rsu_debug & RSU_DEBUG_RESET) {
1222 /* Dump ROM content. */
1223 printf("\n");
1224 for (i = 0; i < sizeof(sc->rom); i++)
1225 printf("%02x:", rom[i]);
1226 printf("\n");
1227 }
1228 #endif
1229 return (0);
1230 }
1231
1232 static int
rsu_fw_cmd(struct rsu_softc * sc,uint8_t code,void * buf,int len)1233 rsu_fw_cmd(struct rsu_softc *sc, uint8_t code, void *buf, int len)
1234 {
1235 const uint8_t which = RSU_H2C_ENDPOINT;
1236 struct rsu_data *data;
1237 struct r92s_tx_desc *txd;
1238 struct r92s_fw_cmd_hdr *cmd;
1239 int cmdsz;
1240 int xferlen;
1241
1242 RSU_ASSERT_LOCKED(sc);
1243
1244 data = rsu_getbuf(sc);
1245 if (data == NULL)
1246 return (ENOMEM);
1247
1248 /* Blank the entire payload, just to be safe */
1249 memset(data->buf, '\0', RSU_TXBUFSZ);
1250
1251 /* Round-up command length to a multiple of 8 bytes. */
1252 /* XXX TODO: is this required? */
1253 cmdsz = (len + 7) & ~7;
1254
1255 xferlen = sizeof(*txd) + sizeof(*cmd) + cmdsz;
1256 KASSERT(xferlen <= RSU_TXBUFSZ, ("%s: invalid length", __func__));
1257 memset(data->buf, 0, xferlen);
1258
1259 /* Setup Tx descriptor. */
1260 txd = (struct r92s_tx_desc *)data->buf;
1261 txd->txdw0 = htole32(
1262 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
1263 SM(R92S_TXDW0_PKTLEN, sizeof(*cmd) + cmdsz) |
1264 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
1265 txd->txdw1 = htole32(SM(R92S_TXDW1_QSEL, R92S_TXDW1_QSEL_H2C));
1266
1267 /* Setup command header. */
1268 cmd = (struct r92s_fw_cmd_hdr *)&txd[1];
1269 cmd->len = htole16(cmdsz);
1270 cmd->code = code;
1271 cmd->seq = sc->cmd_seq;
1272 sc->cmd_seq = (sc->cmd_seq + 1) & 0x7f;
1273
1274 /* Copy command payload. */
1275 memcpy(&cmd[1], buf, len);
1276
1277 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FWCMD,
1278 "%s: Tx cmd code=0x%x len=0x%x\n",
1279 __func__, code, cmdsz);
1280 data->buflen = xferlen;
1281 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
1282 usbd_transfer_start(sc->sc_xfer[which]);
1283
1284 return (0);
1285 }
1286
1287 /* ARGSUSED */
1288 static void
rsu_calib_task(void * arg,int pending __unused)1289 rsu_calib_task(void *arg, int pending __unused)
1290 {
1291 struct rsu_softc *sc = arg;
1292 #ifdef notyet
1293 uint32_t reg;
1294 #endif
1295
1296 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: running calibration task\n",
1297 __func__);
1298
1299 RSU_LOCK(sc);
1300 #ifdef notyet
1301 /* Read WPS PBC status. */
1302 rsu_write_1(sc, R92S_MAC_PINMUX_CTRL,
1303 R92S_GPIOMUX_EN | SM(R92S_GPIOSEL_GPIO, R92S_GPIOSEL_GPIO_JTAG));
1304 rsu_write_1(sc, R92S_GPIO_IO_SEL,
1305 rsu_read_1(sc, R92S_GPIO_IO_SEL) & ~R92S_GPIO_WPS);
1306 reg = rsu_read_1(sc, R92S_GPIO_CTRL);
1307 if (reg != 0xff && (reg & R92S_GPIO_WPS))
1308 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "WPS PBC is pushed\n");
1309 #endif
1310 /* Read current signal level. */
1311 if (rsu_fw_iocmd(sc, 0xf4000001) == 0) {
1312 sc->sc_currssi = rsu_read_4(sc, R92S_IOCMD_DATA);
1313 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: RSSI=%d (%d)\n",
1314 __func__, sc->sc_currssi,
1315 rsu_hwrssi_to_rssi(sc, sc->sc_currssi));
1316 }
1317 if (sc->sc_calibrating)
1318 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz);
1319 RSU_UNLOCK(sc);
1320 }
1321
1322 static void
rsu_tx_task(void * arg,int pending __unused)1323 rsu_tx_task(void *arg, int pending __unused)
1324 {
1325 struct rsu_softc *sc = arg;
1326
1327 RSU_LOCK(sc);
1328 _rsu_start(sc);
1329 RSU_UNLOCK(sc);
1330 }
1331
1332 #define RSU_PWR_UNKNOWN 0x0
1333 #define RSU_PWR_ACTIVE 0x1
1334 #define RSU_PWR_OFF 0x2
1335 #define RSU_PWR_SLEEP 0x3
1336
1337 /*
1338 * Set the current power state.
1339 *
1340 * The rtlwifi code doesn't do this so aggressively; it
1341 * waits for an idle period after association with
1342 * no traffic before doing this.
1343 *
1344 * For now - it's on in all states except RUN, and
1345 * in RUN it'll transition to allow sleep.
1346 */
1347
1348 struct r92s_pwr_cmd {
1349 uint8_t mode;
1350 uint8_t smart_ps;
1351 uint8_t bcn_pass_time;
1352 };
1353
1354 static int
rsu_set_fw_power_state(struct rsu_softc * sc,int state)1355 rsu_set_fw_power_state(struct rsu_softc *sc, int state)
1356 {
1357 struct r92s_set_pwr_mode cmd;
1358 //struct r92s_pwr_cmd cmd;
1359 int error;
1360
1361 RSU_ASSERT_LOCKED(sc);
1362
1363 /* only change state if required */
1364 if (sc->sc_curpwrstate == state)
1365 return (0);
1366
1367 memset(&cmd, 0, sizeof(cmd));
1368
1369 switch (state) {
1370 case RSU_PWR_ACTIVE:
1371 /* Force the hardware awake */
1372 rsu_write_1(sc, R92S_USB_HRPWM,
1373 R92S_USB_HRPWM_PS_ST_ACTIVE | R92S_USB_HRPWM_PS_ALL_ON);
1374 cmd.mode = R92S_PS_MODE_ACTIVE;
1375 break;
1376 case RSU_PWR_SLEEP:
1377 cmd.mode = R92S_PS_MODE_DTIM; /* XXX configurable? */
1378 cmd.smart_ps = 1; /* XXX 2 if doing p2p */
1379 cmd.bcn_pass_time = 5; /* in 100mS usb.c, linux/rtlwifi */
1380 break;
1381 case RSU_PWR_OFF:
1382 cmd.mode = R92S_PS_MODE_RADIOOFF;
1383 break;
1384 default:
1385 device_printf(sc->sc_dev, "%s: unknown ps mode (%d)\n",
1386 __func__,
1387 state);
1388 return (ENXIO);
1389 }
1390
1391 RSU_DPRINTF(sc, RSU_DEBUG_RESET,
1392 "%s: setting ps mode to %d (mode %d)\n",
1393 __func__, state, cmd.mode);
1394 error = rsu_fw_cmd(sc, R92S_CMD_SET_PWR_MODE, &cmd, sizeof(cmd));
1395 if (error == 0)
1396 sc->sc_curpwrstate = state;
1397
1398 return (error);
1399 }
1400
1401 static void
rsu_set_led(struct rsu_softc * sc,int on)1402 rsu_set_led(struct rsu_softc *sc, int on)
1403 {
1404 rsu_write_1(sc, R92S_LEDCFG,
1405 (rsu_read_1(sc, R92S_LEDCFG) & 0xf0) | (!on << 3));
1406 }
1407
1408 static int
rsu_monitor_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1409 rsu_monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate,
1410 int arg)
1411 {
1412 struct ieee80211com *ic = vap->iv_ic;
1413 struct rsu_softc *sc = ic->ic_softc;
1414 struct rsu_vap *uvp = RSU_VAP(vap);
1415
1416 if (vap->iv_state != nstate) {
1417 IEEE80211_UNLOCK(ic);
1418 RSU_LOCK(sc);
1419
1420 switch (nstate) {
1421 case IEEE80211_S_INIT:
1422 sc->sc_vap_is_running = 0;
1423 rsu_set_led(sc, 0);
1424 break;
1425 case IEEE80211_S_RUN:
1426 sc->sc_vap_is_running = 1;
1427 rsu_set_led(sc, 1);
1428 break;
1429 default:
1430 /* NOTREACHED */
1431 break;
1432 }
1433 rsu_rxfilter_refresh(sc);
1434
1435 RSU_UNLOCK(sc);
1436 IEEE80211_LOCK(ic);
1437 }
1438
1439 return (uvp->newstate(vap, nstate, arg));
1440 }
1441
1442 static int
rsu_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1443 rsu_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1444 {
1445 struct rsu_vap *uvp = RSU_VAP(vap);
1446 struct ieee80211com *ic = vap->iv_ic;
1447 struct rsu_softc *sc = ic->ic_softc;
1448 struct ieee80211_node *ni;
1449 struct ieee80211_rateset *rs;
1450 enum ieee80211_state ostate;
1451 int error, startcal = 0;
1452
1453 ostate = vap->iv_state;
1454 RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: %s -> %s\n",
1455 __func__,
1456 ieee80211_state_name[ostate],
1457 ieee80211_state_name[nstate]);
1458
1459 IEEE80211_UNLOCK(ic);
1460 if (ostate == IEEE80211_S_RUN) {
1461 RSU_LOCK(sc);
1462 /* Stop calibration. */
1463 sc->sc_calibrating = 0;
1464
1465 /* Pause Tx for AC queues. */
1466 rsu_write_1(sc, R92S_TXPAUSE, R92S_TXPAUSE_AC);
1467 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1468
1469 RSU_UNLOCK(sc);
1470 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
1471 taskqueue_drain(taskqueue_thread, &sc->tx_task);
1472 RSU_LOCK(sc);
1473 /* Disassociate from our current BSS. */
1474 rsu_disconnect(sc);
1475 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1476
1477 /* Refresh Rx filter (may be modified by firmware). */
1478 sc->sc_vap_is_running = 0;
1479 rsu_rxfilter_refresh(sc);
1480
1481 /* Reinstall static keys. */
1482 if (sc->sc_running)
1483 rsu_reinit_static_keys(sc);
1484 } else
1485 RSU_LOCK(sc);
1486 switch (nstate) {
1487 case IEEE80211_S_INIT:
1488 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE);
1489 break;
1490 case IEEE80211_S_AUTH:
1491 ni = ieee80211_ref_node(vap->iv_bss);
1492 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE);
1493 error = rsu_join_bss(sc, ni);
1494 ieee80211_free_node(ni);
1495 if (error != 0) {
1496 device_printf(sc->sc_dev,
1497 "could not send join command\n");
1498 }
1499 break;
1500 case IEEE80211_S_RUN:
1501 /* Flush all AC queues. */
1502 rsu_write_1(sc, R92S_TXPAUSE, 0);
1503
1504 ni = ieee80211_ref_node(vap->iv_bss);
1505 rs = &ni->ni_rates;
1506 /* Indicate highest supported rate. */
1507 ieee80211_node_set_txrate_dot11rate(ni,
1508 rs->rs_rates[rs->rs_nrates - 1]);
1509 (void) rsu_set_fw_power_state(sc, RSU_PWR_SLEEP);
1510 ieee80211_free_node(ni);
1511 startcal = 1;
1512 break;
1513 default:
1514 break;
1515 }
1516 if (startcal != 0) {
1517 sc->sc_calibrating = 1;
1518 /* Start periodic calibration. */
1519 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task,
1520 hz);
1521 }
1522 RSU_UNLOCK(sc);
1523 IEEE80211_LOCK(ic);
1524 return (uvp->newstate(vap, nstate, arg));
1525 }
1526
1527 static int
rsu_key_alloc(struct ieee80211vap * vap,struct ieee80211_key * k,ieee80211_keyix * keyix,ieee80211_keyix * rxkeyix)1528 rsu_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
1529 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
1530 {
1531 struct rsu_softc *sc = vap->iv_ic->ic_softc;
1532 int is_checked = 0;
1533
1534 if (ieee80211_is_key_global(vap, k)) {
1535 *keyix = ieee80211_crypto_get_key_wepidx(vap, k);
1536 } else {
1537 /* Note: assumes this is a pairwise key */
1538 if (vap->iv_opmode != IEEE80211_M_STA) {
1539 *keyix = 0;
1540 /* TODO: obtain keyix from node id */
1541 is_checked = 1;
1542 k->wk_flags |= IEEE80211_KEY_SWCRYPT;
1543 } else
1544 /*
1545 * TODO: should allocate these from the CAM space;
1546 * skipping over the fixed slots and _BC / _BSS.
1547 */
1548 *keyix = R92S_MACID_BSS;
1549 }
1550
1551 if (!is_checked) {
1552 RSU_LOCK(sc);
1553 if (isset(sc->keys_bmap, *keyix)) {
1554 device_printf(sc->sc_dev,
1555 "%s: key slot %d is already used!\n",
1556 __func__, *keyix);
1557 RSU_UNLOCK(sc);
1558 return (0);
1559 }
1560 setbit(sc->keys_bmap, *keyix);
1561 RSU_UNLOCK(sc);
1562 }
1563
1564 *rxkeyix = *keyix;
1565
1566 return (1);
1567 }
1568
1569 static int
rsu_process_key(struct ieee80211vap * vap,const struct ieee80211_key * k,int set)1570 rsu_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k,
1571 int set)
1572 {
1573 struct rsu_softc *sc = vap->iv_ic->ic_softc;
1574 int ret;
1575
1576 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
1577 /* Not for us. */
1578 return (1);
1579 }
1580
1581 /* Handle group keys. */
1582 if (ieee80211_is_key_global(vap, k)) {
1583 KASSERT(k->wk_keyix < nitems(sc->group_keys),
1584 ("keyix %u > %zu\n", k->wk_keyix, nitems(sc->group_keys)));
1585
1586 RSU_LOCK(sc);
1587 sc->group_keys[k->wk_keyix] = (set ? k : NULL);
1588 if (!sc->sc_running) {
1589 /* Static keys will be set during device startup. */
1590 RSU_UNLOCK(sc);
1591 return (1);
1592 }
1593
1594 if (set)
1595 ret = rsu_set_key_group(sc, k);
1596 else
1597 ret = rsu_delete_key(sc, k->wk_keyix);
1598 RSU_UNLOCK(sc);
1599
1600 return (!ret);
1601 }
1602
1603 if (set) {
1604 /* wait for pending key removal */
1605 taskqueue_drain(taskqueue_thread, &sc->del_key_task);
1606
1607 RSU_LOCK(sc);
1608 ret = rsu_set_key_pair(sc, k);
1609 RSU_UNLOCK(sc);
1610 } else {
1611 RSU_DELKEY_BMAP_LOCK(sc);
1612 setbit(sc->free_keys_bmap, k->wk_keyix);
1613 RSU_DELKEY_BMAP_UNLOCK(sc);
1614
1615 /* workaround ieee80211_node_delucastkey() locking */
1616 taskqueue_enqueue(taskqueue_thread, &sc->del_key_task);
1617 ret = 0; /* fake success */
1618 }
1619
1620 return (!ret);
1621 }
1622
1623 static int
rsu_key_set(struct ieee80211vap * vap,const struct ieee80211_key * k)1624 rsu_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
1625 {
1626 return (rsu_process_key(vap, k, 1));
1627 }
1628
1629 static int
rsu_key_delete(struct ieee80211vap * vap,const struct ieee80211_key * k)1630 rsu_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
1631 {
1632 return (rsu_process_key(vap, k, 0));
1633 }
1634
1635 static int
rsu_cam_read(struct rsu_softc * sc,uint8_t addr,uint32_t * val)1636 rsu_cam_read(struct rsu_softc *sc, uint8_t addr, uint32_t *val)
1637 {
1638 int ntries;
1639
1640 rsu_write_4(sc, R92S_CAMCMD,
1641 R92S_CAMCMD_POLLING | SM(R92S_CAMCMD_ADDR, addr));
1642 for (ntries = 0; ntries < 10; ntries++) {
1643 if (!(rsu_read_4(sc, R92S_CAMCMD) & R92S_CAMCMD_POLLING))
1644 break;
1645
1646 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(1));
1647 }
1648 if (ntries == 10) {
1649 device_printf(sc->sc_dev,
1650 "%s: cannot read CAM entry at address %02X\n",
1651 __func__, addr);
1652 return (ETIMEDOUT);
1653 }
1654
1655 *val = rsu_read_4(sc, R92S_CAMREAD);
1656
1657 return (0);
1658 }
1659
1660 static void
rsu_cam_write(struct rsu_softc * sc,uint8_t addr,uint32_t data)1661 rsu_cam_write(struct rsu_softc *sc, uint8_t addr, uint32_t data)
1662 {
1663
1664 rsu_write_4(sc, R92S_CAMWRITE, data);
1665 rsu_write_4(sc, R92S_CAMCMD,
1666 R92S_CAMCMD_POLLING | R92S_CAMCMD_WRITE |
1667 SM(R92S_CAMCMD_ADDR, addr));
1668 }
1669
1670 static int
rsu_key_check(struct rsu_softc * sc,ieee80211_keyix keyix,int is_valid)1671 rsu_key_check(struct rsu_softc *sc, ieee80211_keyix keyix, int is_valid)
1672 {
1673 uint32_t val;
1674 int error, ntries;
1675
1676 for (ntries = 0; ntries < 20; ntries++) {
1677 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(1));
1678
1679 error = rsu_cam_read(sc, R92S_CAM_CTL0(keyix), &val);
1680 if (error != 0) {
1681 device_printf(sc->sc_dev,
1682 "%s: cannot check key status!\n", __func__);
1683 return (error);
1684 }
1685 if (((val & R92S_CAM_VALID) == 0) ^ is_valid)
1686 break;
1687 }
1688 if (ntries == 20) {
1689 device_printf(sc->sc_dev,
1690 "%s: key %d is %s marked as valid, rejecting request\n",
1691 __func__, keyix, is_valid ? "not" : "still");
1692 return (EIO);
1693 }
1694
1695 return (0);
1696 }
1697
1698 /*
1699 * Map net80211 cipher to RTL8712 security mode.
1700 */
1701 static uint8_t
rsu_crypto_mode(struct rsu_softc * sc,u_int cipher,int keylen)1702 rsu_crypto_mode(struct rsu_softc *sc, u_int cipher, int keylen)
1703 {
1704 switch (cipher) {
1705 case IEEE80211_CIPHER_WEP:
1706 return keylen < 8 ? R92S_KEY_ALGO_WEP40 : R92S_KEY_ALGO_WEP104;
1707 case IEEE80211_CIPHER_TKIP:
1708 return R92S_KEY_ALGO_TKIP;
1709 case IEEE80211_CIPHER_AES_CCM:
1710 return R92S_KEY_ALGO_AES;
1711 default:
1712 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1713 return R92S_KEY_ALGO_INVALID;
1714 }
1715 }
1716
1717 static int
rsu_set_key_group(struct rsu_softc * sc,const struct ieee80211_key * k)1718 rsu_set_key_group(struct rsu_softc *sc, const struct ieee80211_key *k)
1719 {
1720 struct r92s_fw_cmd_set_key key;
1721 uint8_t algo;
1722 int error;
1723
1724 RSU_ASSERT_LOCKED(sc);
1725
1726 /* Map net80211 cipher to HW crypto algorithm. */
1727 algo = rsu_crypto_mode(sc, k->wk_cipher->ic_cipher,
1728 ieee80211_crypto_get_key_len(k));
1729 if (algo == R92S_KEY_ALGO_INVALID)
1730 return (EINVAL);
1731
1732 memset(&key, 0, sizeof(key));
1733 key.algo = algo;
1734 key.cam_id = k->wk_keyix;
1735 key.grpkey = (k->wk_flags & IEEE80211_KEY_GROUP) != 0;
1736 memcpy(key.key, ieee80211_crypto_get_key_data(k),
1737 MIN(ieee80211_crypto_get_key_len(k), sizeof(key.key)));
1738
1739 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD,
1740 "%s: keyix %u, group %u, algo %u/%u, flags %04X, len %u, "
1741 "macaddr %s\n", __func__, key.cam_id, key.grpkey,
1742 k->wk_cipher->ic_cipher, key.algo, k->wk_flags,
1743 ieee80211_crypto_get_key_len(k), ether_sprintf(k->wk_macaddr));
1744
1745 error = rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
1746 if (error != 0) {
1747 device_printf(sc->sc_dev,
1748 "%s: cannot send firmware command, error %d\n",
1749 __func__, error);
1750 return (error);
1751 }
1752
1753 return (rsu_key_check(sc, k->wk_keyix, 1));
1754 }
1755
1756 static int
rsu_set_key_pair(struct rsu_softc * sc,const struct ieee80211_key * k)1757 rsu_set_key_pair(struct rsu_softc *sc, const struct ieee80211_key *k)
1758 {
1759 struct r92s_fw_cmd_set_key_mac key;
1760 uint8_t algo;
1761 int error;
1762
1763 RSU_ASSERT_LOCKED(sc);
1764
1765 if (!sc->sc_running)
1766 return (ESHUTDOWN);
1767
1768 /* Map net80211 cipher to HW crypto algorithm. */
1769 algo = rsu_crypto_mode(sc, k->wk_cipher->ic_cipher,
1770 ieee80211_crypto_get_key_len(k));
1771 if (algo == R92S_KEY_ALGO_INVALID)
1772 return (EINVAL);
1773
1774 memset(&key, 0, sizeof(key));
1775 key.algo = algo;
1776 memcpy(key.macaddr, k->wk_macaddr, sizeof(key.macaddr));
1777 memcpy(key.key, ieee80211_crypto_get_key_data(k),
1778 MIN(ieee80211_crypto_get_key_len(k), sizeof(key.key)));
1779
1780 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD,
1781 "%s: keyix %u, algo %u/%u, flags %04X, len %u, macaddr %s\n",
1782 __func__, k->wk_keyix, k->wk_cipher->ic_cipher, key.algo,
1783 k->wk_flags, ieee80211_crypto_get_key_len(k),
1784 ether_sprintf(key.macaddr));
1785
1786 error = rsu_fw_cmd(sc, R92S_CMD_SET_STA_KEY, &key, sizeof(key));
1787 if (error != 0) {
1788 device_printf(sc->sc_dev,
1789 "%s: cannot send firmware command, error %d\n",
1790 __func__, error);
1791 return (error);
1792 }
1793
1794 return (rsu_key_check(sc, k->wk_keyix, 1));
1795 }
1796
1797 static int
rsu_reinit_static_keys(struct rsu_softc * sc)1798 rsu_reinit_static_keys(struct rsu_softc *sc)
1799 {
1800 int i, error;
1801
1802 for (i = 0; i < nitems(sc->group_keys); i++) {
1803 if (sc->group_keys[i] != NULL) {
1804 error = rsu_set_key_group(sc, sc->group_keys[i]);
1805 if (error != 0) {
1806 device_printf(sc->sc_dev,
1807 "%s: failed to set static key %d, "
1808 "error %d\n", __func__, i, error);
1809 return (error);
1810 }
1811 }
1812 }
1813
1814 return (0);
1815 }
1816
1817 static int
rsu_delete_key(struct rsu_softc * sc,ieee80211_keyix keyix)1818 rsu_delete_key(struct rsu_softc *sc, ieee80211_keyix keyix)
1819 {
1820 struct r92s_fw_cmd_set_key key;
1821 uint32_t val;
1822 int error;
1823
1824 RSU_ASSERT_LOCKED(sc);
1825
1826 if (!sc->sc_running)
1827 return (0);
1828
1829 /* check if it was automatically removed by firmware */
1830 error = rsu_cam_read(sc, R92S_CAM_CTL0(keyix), &val);
1831 if (error == 0 && (val & R92S_CAM_VALID) == 0) {
1832 RSU_DPRINTF(sc, RSU_DEBUG_KEY,
1833 "%s: key %u does not exist\n", __func__, keyix);
1834 clrbit(sc->keys_bmap, keyix);
1835 return (0);
1836 }
1837
1838 memset(&key, 0, sizeof(key));
1839 key.cam_id = keyix;
1840
1841 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD,
1842 "%s: removing key %u\n", __func__, key.cam_id);
1843
1844 error = rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
1845 if (error != 0) {
1846 device_printf(sc->sc_dev,
1847 "%s: cannot send firmware command, error %d\n",
1848 __func__, error);
1849 goto finish;
1850 }
1851
1852 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(5));
1853
1854 /*
1855 * Clear 'valid' bit manually (cannot be done via firmware command).
1856 * Used for key check + when firmware command cannot be sent.
1857 */
1858 finish:
1859 rsu_cam_write(sc, R92S_CAM_CTL0(keyix), 0);
1860
1861 clrbit(sc->keys_bmap, keyix);
1862
1863 return (rsu_key_check(sc, keyix, 0));
1864 }
1865
1866 static void
rsu_delete_key_pair_cb(void * arg,int pending __unused)1867 rsu_delete_key_pair_cb(void *arg, int pending __unused)
1868 {
1869 struct rsu_softc *sc = arg;
1870 int i;
1871
1872 RSU_DELKEY_BMAP_LOCK(sc);
1873 for (i = IEEE80211_WEP_NKID; i < R92S_CAM_ENTRY_LIMIT; i++) {
1874 if (isset(sc->free_keys_bmap, i)) {
1875 RSU_DELKEY_BMAP_UNLOCK(sc);
1876
1877 RSU_LOCK(sc);
1878 RSU_DPRINTF(sc, RSU_DEBUG_KEY,
1879 "%s: calling rsu_delete_key() with keyix = %d\n",
1880 __func__, i);
1881 (void) rsu_delete_key(sc, i);
1882 RSU_UNLOCK(sc);
1883
1884 RSU_DELKEY_BMAP_LOCK(sc);
1885 clrbit(sc->free_keys_bmap, i);
1886
1887 /* bmap can be changed */
1888 i = IEEE80211_WEP_NKID - 1;
1889 continue;
1890 }
1891 }
1892 RSU_DELKEY_BMAP_UNLOCK(sc);
1893 }
1894
1895 static int
rsu_site_survey(struct rsu_softc * sc,struct ieee80211_scan_ssid * ssid)1896 rsu_site_survey(struct rsu_softc *sc, struct ieee80211_scan_ssid *ssid)
1897 {
1898 struct r92s_fw_cmd_sitesurvey cmd;
1899
1900 RSU_ASSERT_LOCKED(sc);
1901
1902 memset(&cmd, 0, sizeof(cmd));
1903 /* TODO: passive channels? */
1904 if (sc->sc_active_scan)
1905 cmd.active = htole32(1);
1906 cmd.limit = htole32(48);
1907
1908 if (ssid != NULL) {
1909 sc->sc_extra_scan = 1;
1910 cmd.ssidlen = htole32(ssid->len);
1911 memcpy(cmd.ssid, ssid->ssid, ssid->len);
1912 }
1913 #ifdef USB_DEBUG
1914 if (rsu_debug & (RSU_DEBUG_SCAN | RSU_DEBUG_FWCMD)) {
1915 device_printf(sc->sc_dev,
1916 "sending site survey command, active %d",
1917 le32toh(cmd.active));
1918 if (ssid != NULL) {
1919 printf(", ssid: ");
1920 ieee80211_print_essid(cmd.ssid, le32toh(cmd.ssidlen));
1921 }
1922 printf("\n");
1923 }
1924 #endif
1925 return (rsu_fw_cmd(sc, R92S_CMD_SITE_SURVEY, &cmd, sizeof(cmd)));
1926 }
1927
1928 static int
rsu_join_bss(struct rsu_softc * sc,struct ieee80211_node * ni)1929 rsu_join_bss(struct rsu_softc *sc, struct ieee80211_node *ni)
1930 {
1931 struct ieee80211com *ic = &sc->sc_ic;
1932 struct ieee80211vap *vap = ni->ni_vap;
1933 struct ndis_wlan_bssid_ex *bss;
1934 struct ndis_802_11_fixed_ies *fixed;
1935 struct r92s_fw_cmd_auth auth;
1936 uint8_t buf[sizeof(*bss) + 128] __aligned(4);
1937 uint8_t *frm;
1938 uint8_t opmode;
1939 int error;
1940
1941 RSU_ASSERT_LOCKED(sc);
1942
1943 /* Let the FW decide the opmode based on the capinfo field. */
1944 opmode = NDIS802_11AUTOUNKNOWN;
1945 RSU_DPRINTF(sc, RSU_DEBUG_RESET,
1946 "%s: setting operating mode to %d\n",
1947 __func__, opmode);
1948 error = rsu_fw_cmd(sc, R92S_CMD_SET_OPMODE, &opmode, sizeof(opmode));
1949 if (error != 0)
1950 return (error);
1951
1952 memset(&auth, 0, sizeof(auth));
1953 if (vap->iv_flags & IEEE80211_F_WPA) {
1954 auth.mode = R92S_AUTHMODE_WPA;
1955 auth.dot1x = (ni->ni_authmode == IEEE80211_AUTH_8021X);
1956 } else
1957 auth.mode = R92S_AUTHMODE_OPEN;
1958 RSU_DPRINTF(sc, RSU_DEBUG_RESET,
1959 "%s: setting auth mode to %d\n",
1960 __func__, auth.mode);
1961 error = rsu_fw_cmd(sc, R92S_CMD_SET_AUTH, &auth, sizeof(auth));
1962 if (error != 0)
1963 return (error);
1964
1965 memset(buf, 0, sizeof(buf));
1966 bss = (struct ndis_wlan_bssid_ex *)buf;
1967 IEEE80211_ADDR_COPY(bss->macaddr, ni->ni_bssid);
1968 bss->ssid.ssidlen = htole32(ni->ni_esslen);
1969 memcpy(bss->ssid.ssid, ni->ni_essid, ni->ni_esslen);
1970 if (vap->iv_flags & (IEEE80211_F_PRIVACY | IEEE80211_F_WPA))
1971 bss->privacy = htole32(1);
1972 bss->rssi = htole32(ni->ni_avgrssi);
1973 if (ic->ic_curmode == IEEE80211_MODE_11B)
1974 bss->networktype = htole32(NDIS802_11DS);
1975 else
1976 bss->networktype = htole32(NDIS802_11OFDM24);
1977 bss->config.len = htole32(sizeof(bss->config));
1978 bss->config.bintval = htole32(ni->ni_intval);
1979 bss->config.dsconfig = htole32(ieee80211_chan2ieee(ic, ni->ni_chan));
1980 bss->inframode = htole32(NDIS802_11INFRASTRUCTURE);
1981 /* XXX verify how this is supposed to look! */
1982 memcpy(bss->supprates, ni->ni_rates.rs_rates,
1983 ni->ni_rates.rs_nrates);
1984 /* Write the fixed fields of the beacon frame. */
1985 fixed = (struct ndis_802_11_fixed_ies *)&bss[1];
1986 memcpy(&fixed->tstamp, ni->ni_tstamp.data, 8);
1987 fixed->bintval = htole16(ni->ni_intval);
1988 fixed->capabilities = htole16(ni->ni_capinfo);
1989 /* Write IEs to be included in the association request. */
1990 frm = (uint8_t *)&fixed[1];
1991 frm = ieee80211_add_rsn(frm, vap);
1992 frm = ieee80211_add_wpa(frm, vap);
1993 frm = ieee80211_add_qos(frm, ni);
1994 if ((ic->ic_flags & IEEE80211_F_WME) &&
1995 (ni->ni_ies.wme_ie != NULL))
1996 frm = ieee80211_add_wme_info(frm, &ic->ic_wme, ni);
1997 if (ni->ni_flags & IEEE80211_NODE_HT) {
1998 frm = ieee80211_add_htcap(frm, ni);
1999 frm = ieee80211_add_htinfo(frm, ni);
2000 }
2001 bss->ieslen = htole32(frm - (uint8_t *)fixed);
2002 bss->len = htole32(((frm - buf) + 3) & ~3);
2003 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_FWCMD,
2004 "%s: sending join bss command to %s chan %d\n",
2005 __func__,
2006 ether_sprintf(bss->macaddr), le32toh(bss->config.dsconfig));
2007 return (rsu_fw_cmd(sc, R92S_CMD_JOIN_BSS, buf, sizeof(buf)));
2008 }
2009
2010 static int
rsu_disconnect(struct rsu_softc * sc)2011 rsu_disconnect(struct rsu_softc *sc)
2012 {
2013 uint32_t zero = 0; /* :-) */
2014
2015 /* Disassociate from our current BSS. */
2016 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
2017 "%s: sending disconnect command\n", __func__);
2018 return (rsu_fw_cmd(sc, R92S_CMD_DISCONNECT, &zero, sizeof(zero)));
2019 }
2020
2021 /*
2022 * Map the hardware provided RSSI value to a signal level.
2023 * For the most part it's just something we divide by and cap
2024 * so it doesn't overflow the representation by net80211.
2025 */
2026 static int
rsu_hwrssi_to_rssi(struct rsu_softc * sc,int hw_rssi)2027 rsu_hwrssi_to_rssi(struct rsu_softc *sc, int hw_rssi)
2028 {
2029 int v;
2030
2031 if (hw_rssi == 0)
2032 return (0);
2033 v = hw_rssi >> 4;
2034 if (v > 80)
2035 v = 80;
2036 return (v);
2037 }
2038
2039 CTASSERT(MCLBYTES > sizeof(struct ieee80211_frame));
2040
2041 static void
rsu_event_survey(struct rsu_softc * sc,uint8_t * buf,int len)2042 rsu_event_survey(struct rsu_softc *sc, uint8_t *buf, int len)
2043 {
2044 struct ieee80211com *ic = &sc->sc_ic;
2045 struct ieee80211_frame *wh;
2046 struct ndis_wlan_bssid_ex *bss;
2047 struct ieee80211_rx_stats rxs;
2048 struct mbuf *m;
2049 uint32_t ieslen;
2050 uint32_t pktlen;
2051
2052 if (__predict_false(len < sizeof(*bss)))
2053 return;
2054 bss = (struct ndis_wlan_bssid_ex *)buf;
2055 ieslen = le32toh(bss->ieslen);
2056 /* range check length of information element */
2057 if (__predict_false(ieslen > (uint32_t)(len - sizeof(*bss))))
2058 return;
2059
2060 RSU_DPRINTF(sc, RSU_DEBUG_SCAN,
2061 "%s: found BSS %s: len=%d chan=%d inframode=%d "
2062 "networktype=%d privacy=%d, RSSI=%d\n",
2063 __func__,
2064 ether_sprintf(bss->macaddr), ieslen,
2065 le32toh(bss->config.dsconfig), le32toh(bss->inframode),
2066 le32toh(bss->networktype), le32toh(bss->privacy),
2067 le32toh(bss->rssi));
2068
2069 /* Build a fake beacon frame to let net80211 do all the parsing. */
2070 /* XXX TODO: just call the new scan API methods! */
2071 if (__predict_false(ieslen > (size_t)(MCLBYTES - sizeof(*wh))))
2072 return;
2073 pktlen = sizeof(*wh) + ieslen;
2074 m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR);
2075 if (__predict_false(m == NULL))
2076 return;
2077 wh = mtod(m, struct ieee80211_frame *);
2078 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2079 IEEE80211_FC0_SUBTYPE_BEACON;
2080 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2081 USETW(wh->i_dur, 0);
2082 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
2083 IEEE80211_ADDR_COPY(wh->i_addr2, bss->macaddr);
2084 IEEE80211_ADDR_COPY(wh->i_addr3, bss->macaddr);
2085 *(uint16_t *)wh->i_seq = 0;
2086 memcpy(&wh[1], (uint8_t *)&bss[1], ieslen);
2087
2088 /* Finalize mbuf. */
2089 m->m_pkthdr.len = m->m_len = pktlen;
2090
2091 /* Set channel flags for input path */
2092 bzero(&rxs, sizeof(rxs));
2093 rxs.r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ;
2094 rxs.r_flags |= IEEE80211_R_BAND;
2095 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI;
2096 rxs.c_ieee = le32toh(bss->config.dsconfig);
2097 rxs.c_freq = ieee80211_ieee2mhz(rxs.c_ieee, IEEE80211_CHAN_2GHZ);
2098 rxs.c_band = IEEE80211_CHAN_2GHZ;
2099 /* This is a number from 0..100; so let's just divide it down a bit */
2100 rxs.c_rssi = le32toh(bss->rssi) / 2;
2101 rxs.c_nf = -96;
2102 if (ieee80211_add_rx_params(m, &rxs) == 0)
2103 return;
2104
2105 /* XXX avoid a LOR */
2106 RSU_UNLOCK(sc);
2107 ieee80211_input_mimo_all(ic, m);
2108 RSU_LOCK(sc);
2109 }
2110
2111 static void
rsu_event_join_bss(struct rsu_softc * sc,uint8_t * buf,int len)2112 rsu_event_join_bss(struct rsu_softc *sc, uint8_t *buf, int len)
2113 {
2114 struct ieee80211com *ic = &sc->sc_ic;
2115 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2116 struct ieee80211_node *ni = vap->iv_bss;
2117 struct r92s_event_join_bss *rsp;
2118 uint32_t tmp;
2119 int res;
2120
2121 if (__predict_false(len < sizeof(*rsp)))
2122 return;
2123 rsp = (struct r92s_event_join_bss *)buf;
2124 res = (int)le32toh(rsp->join_res);
2125
2126 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
2127 "%s: Rx join BSS event len=%d res=%d\n",
2128 __func__, len, res);
2129
2130 /*
2131 * XXX Don't do this; there's likely a better way to tell
2132 * the caller we failed.
2133 */
2134 if (res <= 0) {
2135 RSU_UNLOCK(sc);
2136 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
2137 RSU_LOCK(sc);
2138 return;
2139 }
2140
2141 tmp = le32toh(rsp->associd);
2142 if (tmp >= vap->iv_max_aid) {
2143 RSU_DPRINTF(sc, RSU_DEBUG_ANY, "Assoc ID overflow\n");
2144 tmp = 1;
2145 }
2146 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
2147 "%s: associated with %s associd=%d\n",
2148 __func__, ether_sprintf(rsp->bss.macaddr), tmp);
2149 /* XXX is this required? What's the top two bits for again? */
2150 ni->ni_associd = tmp | 0xc000;
2151
2152 /* Refresh Rx filter (was changed by firmware). */
2153 sc->sc_vap_is_running = 1;
2154 rsu_rxfilter_refresh(sc);
2155
2156 RSU_UNLOCK(sc);
2157 ieee80211_new_state(vap, IEEE80211_S_RUN,
2158 IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2159 RSU_LOCK(sc);
2160 }
2161
2162 static void
rsu_event_addba_req_report(struct rsu_softc * sc,uint8_t * buf,int len)2163 rsu_event_addba_req_report(struct rsu_softc *sc, uint8_t *buf, int len)
2164 {
2165 struct ieee80211com *ic = &sc->sc_ic;
2166 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2167 struct r92s_add_ba_event *ba = (void *) buf;
2168 struct ieee80211_node *ni;
2169
2170 if (len < sizeof(*ba)) {
2171 device_printf(sc->sc_dev, "%s: short read (%d)\n", __func__, len);
2172 return;
2173 }
2174
2175 if (vap == NULL)
2176 return;
2177
2178 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: mac=%s, tid=%d, ssn=%d\n",
2179 __func__,
2180 ether_sprintf(ba->mac_addr),
2181 (int) ba->tid,
2182 (int) le16toh(ba->ssn) >> 4);
2183
2184 /* XXX do node lookup; this is STA specific */
2185
2186 ni = ieee80211_ref_node(vap->iv_bss);
2187 ieee80211_ampdu_rx_start_ext(ni, ba->tid, le16toh(ba->ssn) >> 4, 32);
2188 ieee80211_free_node(ni);
2189 }
2190
2191 static void
rsu_rx_event(struct rsu_softc * sc,uint8_t code,uint8_t * buf,int len)2192 rsu_rx_event(struct rsu_softc *sc, uint8_t code, uint8_t *buf, int len)
2193 {
2194 struct ieee80211com *ic = &sc->sc_ic;
2195 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2196
2197 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD,
2198 "%s: Rx event code=%d len=%d\n", __func__, code, len);
2199 switch (code) {
2200 case R92S_EVT_SURVEY:
2201 rsu_event_survey(sc, buf, len);
2202 break;
2203 case R92S_EVT_SURVEY_DONE:
2204 RSU_DPRINTF(sc, RSU_DEBUG_SCAN,
2205 "%s: %s scan done, found %d BSS\n",
2206 __func__, sc->sc_extra_scan ? "direct" : "broadcast",
2207 le32toh(*(uint32_t *)buf));
2208 if (sc->sc_extra_scan == 1) {
2209 /* Send broadcast probe request. */
2210 sc->sc_extra_scan = 0;
2211 if (vap != NULL && rsu_site_survey(sc, NULL) != 0) {
2212 RSU_UNLOCK(sc);
2213 ieee80211_cancel_scan(vap);
2214 RSU_LOCK(sc);
2215 }
2216 break;
2217 }
2218 if (vap != NULL) {
2219 RSU_UNLOCK(sc);
2220 ieee80211_scan_done(vap);
2221 RSU_LOCK(sc);
2222 }
2223 break;
2224 case R92S_EVT_JOIN_BSS:
2225 if (vap->iv_state == IEEE80211_S_AUTH)
2226 rsu_event_join_bss(sc, buf, len);
2227 break;
2228
2229 /* TODO: what about R92S_EVT_ADD_STA? and decoding macid? */
2230 /* It likely is required for IBSS/AP mode */
2231
2232 /* TODO: should I be doing this transition in AP mode? */
2233 case R92S_EVT_DEL_STA:
2234 RSU_DPRINTF(sc, RSU_DEBUG_FWCMD | RSU_DEBUG_STATE,
2235 "%s: disassociated from %s\n", __func__,
2236 ether_sprintf(buf));
2237 if (vap->iv_state == IEEE80211_S_RUN &&
2238 IEEE80211_ADDR_EQ(vap->iv_bss->ni_bssid, buf)) {
2239 RSU_UNLOCK(sc);
2240 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
2241 RSU_LOCK(sc);
2242 }
2243 break;
2244 case R92S_EVT_WPS_PBC:
2245 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD,
2246 "%s: WPS PBC pushed.\n", __func__);
2247 break;
2248 case R92S_EVT_FWDBG:
2249 buf[60] = '\0';
2250 /* TODO: some are \n terminated, some aren't, sigh */
2251 RSU_DPRINTF(sc, RSU_DEBUG_FWDBG, "FWDBG: %s\n", (char *)buf);
2252 break;
2253 case R92S_EVT_ADDBA_REQ_REPORT:
2254 rsu_event_addba_req_report(sc, buf, len);
2255 break;
2256 default:
2257 device_printf(sc->sc_dev, "%s: unhandled code (%d)\n", __func__, code);
2258 break;
2259 }
2260 }
2261
2262 static void
rsu_rx_multi_event(struct rsu_softc * sc,uint8_t * buf,int len)2263 rsu_rx_multi_event(struct rsu_softc *sc, uint8_t *buf, int len)
2264 {
2265 struct r92s_fw_cmd_hdr *cmd;
2266 int cmdsz;
2267
2268 RSU_DPRINTF(sc, RSU_DEBUG_RX, "%s: Rx events len=%d\n", __func__, len);
2269
2270 /* Skip Rx status. */
2271 buf += sizeof(struct r92s_rx_stat);
2272 len -= sizeof(struct r92s_rx_stat);
2273
2274 /* Process all events. */
2275 for (;;) {
2276 /* Check that command header fits. */
2277 if (__predict_false(len < sizeof(*cmd)))
2278 break;
2279 cmd = (struct r92s_fw_cmd_hdr *)buf;
2280 /* Check that command payload fits. */
2281 cmdsz = le16toh(cmd->len);
2282 if (__predict_false(len < sizeof(*cmd) + cmdsz))
2283 break;
2284
2285 /* Process firmware event. */
2286 rsu_rx_event(sc, cmd->code, (uint8_t *)&cmd[1], cmdsz);
2287
2288 if (!(cmd->seq & R92S_FW_CMD_MORE))
2289 break;
2290 buf += sizeof(*cmd) + cmdsz;
2291 len -= sizeof(*cmd) + cmdsz;
2292 }
2293 }
2294
2295 static int8_t
rsu_get_rssi(struct rsu_softc * sc,int rate,void * physt)2296 rsu_get_rssi(struct rsu_softc *sc, int rate, void *physt)
2297 {
2298 static const int8_t cckoff[] = { 14, -2, -20, -40 };
2299 struct r92s_rx_phystat *phy;
2300 struct r92s_rx_cck *cck;
2301 uint8_t rpt;
2302 int8_t rssi;
2303
2304 if (rate <= 3) {
2305 cck = (struct r92s_rx_cck *)physt;
2306 rpt = (cck->agc_rpt >> 6) & 0x3;
2307 rssi = cck->agc_rpt & 0x3e;
2308 rssi = cckoff[rpt] - rssi;
2309 } else { /* OFDM/HT. */
2310 phy = (struct r92s_rx_phystat *)physt;
2311 rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 106;
2312 }
2313 return (rssi);
2314 }
2315
2316 static struct mbuf *
rsu_rx_copy_to_mbuf(struct rsu_softc * sc,struct r92s_rx_stat * stat,int totlen)2317 rsu_rx_copy_to_mbuf(struct rsu_softc *sc, struct r92s_rx_stat *stat,
2318 int totlen)
2319 {
2320 struct ieee80211com *ic = &sc->sc_ic;
2321 struct mbuf *m;
2322 uint32_t rxdw0;
2323 int pktlen;
2324
2325 rxdw0 = le32toh(stat->rxdw0);
2326 if (__predict_false(rxdw0 & (R92S_RXDW0_CRCERR | R92S_RXDW0_ICVERR))) {
2327 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2328 "%s: RX flags error (%s)\n", __func__,
2329 rxdw0 & R92S_RXDW0_CRCERR ? "CRC" : "ICV");
2330 goto fail;
2331 }
2332
2333 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN);
2334 if (__predict_false(pktlen < sizeof (struct ieee80211_frame_ack))) {
2335 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2336 "%s: frame is too short: %d\n", __func__, pktlen);
2337 goto fail;
2338 }
2339
2340 m = m_get2(totlen, M_NOWAIT, MT_DATA, M_PKTHDR);
2341 if (__predict_false(m == NULL)) {
2342 device_printf(sc->sc_dev,
2343 "%s: could not allocate RX mbuf, totlen %d\n",
2344 __func__, totlen);
2345 goto fail;
2346 }
2347
2348 /* Finalize mbuf. */
2349 memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen);
2350 m->m_pkthdr.len = m->m_len = totlen;
2351
2352 return (m);
2353 fail:
2354 counter_u64_add(ic->ic_ierrors, 1);
2355 return (NULL);
2356 }
2357
2358 static uint32_t
rsu_get_tsf_low(struct rsu_softc * sc)2359 rsu_get_tsf_low(struct rsu_softc *sc)
2360 {
2361 return (rsu_read_4(sc, R92S_TSFTR));
2362 }
2363
2364 static uint32_t
rsu_get_tsf_high(struct rsu_softc * sc)2365 rsu_get_tsf_high(struct rsu_softc *sc)
2366 {
2367 return (rsu_read_4(sc, R92S_TSFTR + 4));
2368 }
2369
2370 static struct ieee80211_node *
rsu_rx_frame(struct rsu_softc * sc,struct mbuf * m)2371 rsu_rx_frame(struct rsu_softc *sc, struct mbuf *m)
2372 {
2373 struct ieee80211com *ic = &sc->sc_ic;
2374 struct ieee80211_frame_min *wh;
2375 struct ieee80211_rx_stats rxs;
2376 struct r92s_rx_stat *stat;
2377 uint32_t rxdw0, rxdw3;
2378 uint8_t cipher, rate;
2379 int infosz;
2380 int rssi;
2381
2382 stat = mtod(m, struct r92s_rx_stat *);
2383 rxdw0 = le32toh(stat->rxdw0);
2384 rxdw3 = le32toh(stat->rxdw3);
2385
2386 rate = MS(rxdw3, R92S_RXDW3_RATE);
2387 cipher = MS(rxdw0, R92S_RXDW0_CIPHER);
2388 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
2389
2390 /* Get RSSI from PHY status descriptor if present. */
2391 if (infosz != 0 && (rxdw0 & R92S_RXDW0_PHYST))
2392 rssi = rsu_get_rssi(sc, rate, &stat[1]);
2393 else {
2394 /* Cheat and get the last calibrated RSSI */
2395 rssi = rsu_hwrssi_to_rssi(sc, sc->sc_currssi);
2396 }
2397
2398 /* Hardware does Rx TCP checksum offload. */
2399 /*
2400 * This flag can be set for some other
2401 * (e.g., EAPOL) frame types, so don't rely on it.
2402 */
2403 if (rxdw3 & R92S_RXDW3_TCPCHKVALID) {
2404 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2405 "%s: TCP/IP checksums: %schecked / %schecked\n",
2406 __func__,
2407 (rxdw3 & R92S_RXDW3_TCPCHKRPT) ? "" : "not ",
2408 (rxdw3 & R92S_RXDW3_IPCHKRPT) ? "" : "not ");
2409
2410 /*
2411 * 'IP header checksum valid' bit will not be set if
2412 * the frame was not checked / has incorrect checksum /
2413 * does not have checksum (IPv6).
2414 *
2415 * NB: if DF bit is not set then frame will not be checked.
2416 */
2417 if (rxdw3 & R92S_RXDW3_IPCHKRPT) {
2418 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
2419 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2420 }
2421
2422 /*
2423 * This is independent of the above check.
2424 */
2425 if (rxdw3 & R92S_RXDW3_TCPCHKRPT) {
2426 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
2427 m->m_pkthdr.csum_flags |= CSUM_PSEUDO_HDR;
2428 m->m_pkthdr.csum_data = 0xffff;
2429 }
2430 }
2431
2432 /* RX flags */
2433
2434 /* Set channel flags for input path */
2435 bzero(&rxs, sizeof(rxs));
2436
2437 /* normal RSSI */
2438 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI;
2439 rxs.c_rssi = rssi;
2440 rxs.c_nf = -96;
2441
2442 /* Rate */
2443 if (rate < 12) {
2444 rxs.c_rate = ridx2rate[rate];
2445 if (RSU_RATE_IS_CCK(rate))
2446 rxs.c_pktflags |= IEEE80211_RX_F_CCK;
2447 else
2448 rxs.c_pktflags |= IEEE80211_RX_F_OFDM;
2449 } else {
2450 rxs.c_rate = IEEE80211_RATE_MCS | (rate - 12);
2451 rxs.c_pktflags |= IEEE80211_RX_F_HT;
2452 }
2453
2454 if (ieee80211_radiotap_active(ic)) {
2455 struct rsu_rx_radiotap_header *tap = &sc->sc_rxtap;
2456
2457 /* Map HW rate index to 802.11 rate. */
2458 tap->wr_flags = 0; /* TODO */
2459 tap->wr_tsft = rsu_get_tsf_high(sc);
2460 if (le32toh(stat->tsf_low) > rsu_get_tsf_low(sc))
2461 tap->wr_tsft--;
2462 tap->wr_tsft = (uint64_t)htole32(tap->wr_tsft) << 32;
2463 tap->wr_tsft += stat->tsf_low;
2464
2465 tap->wr_rate = rxs.c_rate;
2466 tap->wr_dbm_antsignal = rssi;
2467 };
2468
2469 (void) ieee80211_add_rx_params(m, &rxs);
2470
2471 /* Drop descriptor. */
2472 m_adj(m, sizeof(*stat) + infosz);
2473 wh = mtod(m, struct ieee80211_frame_min *);
2474 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
2475 cipher != R92S_KEY_ALGO_NONE) {
2476 m->m_flags |= M_WEP;
2477 }
2478
2479 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2480 "%s: Rx frame len %d, rate %d, infosz %d\n",
2481 __func__, m->m_len, rate, infosz);
2482
2483 if (m->m_len >= sizeof(*wh))
2484 return (ieee80211_find_rxnode(ic, wh));
2485
2486 return (NULL);
2487 }
2488
2489 static struct mbuf *
rsu_rx_multi_frame(struct rsu_softc * sc,uint8_t * buf,int len)2490 rsu_rx_multi_frame(struct rsu_softc *sc, uint8_t *buf, int len)
2491 {
2492 struct r92s_rx_stat *stat;
2493 uint32_t rxdw0;
2494 int totlen, pktlen, infosz, npkts;
2495 struct mbuf *m, *m0 = NULL, *prevm = NULL;
2496
2497 /*
2498 * don't pass packets to the ieee80211 framework if the driver isn't
2499 * RUNNING.
2500 */
2501 if (!sc->sc_running)
2502 return (NULL);
2503
2504 /* Get the number of encapsulated frames. */
2505 stat = (struct r92s_rx_stat *)buf;
2506 npkts = MS(le32toh(stat->rxdw2), R92S_RXDW2_PKTCNT);
2507 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2508 "%s: Rx %d frames in one chunk\n", __func__, npkts);
2509
2510 /* Process all of them. */
2511 while (npkts-- > 0) {
2512 if (__predict_false(len < sizeof(*stat)))
2513 break;
2514 stat = (struct r92s_rx_stat *)buf;
2515 rxdw0 = le32toh(stat->rxdw0);
2516
2517 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN);
2518 if (__predict_false(pktlen == 0))
2519 break;
2520
2521 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
2522
2523 /* Make sure everything fits in xfer. */
2524 totlen = sizeof(*stat) + infosz + pktlen;
2525 if (__predict_false(totlen > len))
2526 break;
2527
2528 /* Process 802.11 frame. */
2529 m = rsu_rx_copy_to_mbuf(sc, stat, totlen);
2530 if (m0 == NULL)
2531 m0 = m;
2532 if (prevm == NULL)
2533 prevm = m;
2534 else {
2535 prevm->m_next = m;
2536 prevm = m;
2537 }
2538 /* Next chunk is 128-byte aligned. */
2539 totlen = (totlen + 127) & ~127;
2540 buf += totlen;
2541 len -= totlen;
2542 }
2543
2544 return (m0);
2545 }
2546
2547 static struct mbuf *
rsu_rxeof(struct usb_xfer * xfer,struct rsu_data * data)2548 rsu_rxeof(struct usb_xfer *xfer, struct rsu_data *data)
2549 {
2550 struct rsu_softc *sc = data->sc;
2551 struct ieee80211com *ic = &sc->sc_ic;
2552 struct r92s_rx_stat *stat;
2553 int len;
2554
2555 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
2556
2557 if (__predict_false(len < sizeof(*stat))) {
2558 RSU_DPRINTF(sc, RSU_DEBUG_RX, "xfer too short %d\n", len);
2559 counter_u64_add(ic->ic_ierrors, 1);
2560 return (NULL);
2561 }
2562 /* Determine if it is a firmware C2H event or an 802.11 frame. */
2563 stat = (struct r92s_rx_stat *)data->buf;
2564 if ((le32toh(stat->rxdw1) & 0x1ff) == 0x1ff) {
2565 rsu_rx_multi_event(sc, data->buf, len);
2566 /* No packets to process. */
2567 return (NULL);
2568 } else
2569 return (rsu_rx_multi_frame(sc, data->buf, len));
2570 }
2571
2572 static void
rsu_bulk_rx_callback(struct usb_xfer * xfer,usb_error_t error)2573 rsu_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2574 {
2575 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2576 struct ieee80211com *ic = &sc->sc_ic;
2577 struct ieee80211_node *ni;
2578 struct mbuf *m = NULL, *next;
2579 struct rsu_data *data;
2580
2581 RSU_ASSERT_LOCKED(sc);
2582
2583 switch (USB_GET_STATE(xfer)) {
2584 case USB_ST_TRANSFERRED:
2585 data = STAILQ_FIRST(&sc->sc_rx_active);
2586 if (data == NULL)
2587 goto tr_setup;
2588 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2589 m = rsu_rxeof(xfer, data);
2590 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2591 /* FALLTHROUGH */
2592 case USB_ST_SETUP:
2593 tr_setup:
2594 data = STAILQ_FIRST(&sc->sc_rx_inactive);
2595 if (data == NULL) {
2596 KASSERT(m == NULL, ("mbuf isn't NULL"));
2597 return;
2598 }
2599 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2600 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2601 usbd_xfer_set_frame_data(xfer, 0, data->buf,
2602 usbd_xfer_max_len(xfer));
2603 usbd_transfer_submit(xfer);
2604 /*
2605 * To avoid LOR we should unlock our private mutex here to call
2606 * ieee80211_input() because here is at the end of a USB
2607 * callback and safe to unlock.
2608 */
2609 while (m != NULL) {
2610 next = m->m_next;
2611 m->m_next = NULL;
2612
2613 ni = rsu_rx_frame(sc, m);
2614 RSU_UNLOCK(sc);
2615
2616 if (ni != NULL) {
2617 if (ni->ni_flags & IEEE80211_NODE_HT)
2618 m->m_flags |= M_AMPDU;
2619 (void)ieee80211_input_mimo(ni, m);
2620 ieee80211_free_node(ni);
2621 } else
2622 (void)ieee80211_input_mimo_all(ic, m);
2623
2624 RSU_LOCK(sc);
2625 m = next;
2626 }
2627 break;
2628 default:
2629 /* needs it to the inactive queue due to a error. */
2630 data = STAILQ_FIRST(&sc->sc_rx_active);
2631 if (data != NULL) {
2632 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2633 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2634 }
2635 if (error != USB_ERR_CANCELLED) {
2636 usbd_xfer_set_stall(xfer);
2637 counter_u64_add(ic->ic_ierrors, 1);
2638 goto tr_setup;
2639 }
2640 break;
2641 }
2642
2643 }
2644
2645 static void
rsu_txeof(struct usb_xfer * xfer,struct rsu_data * data)2646 rsu_txeof(struct usb_xfer *xfer, struct rsu_data *data)
2647 {
2648 #ifdef USB_DEBUG
2649 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2650 #endif
2651
2652 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: called; data=%p\n",
2653 __func__,
2654 data);
2655
2656 if (data->m) {
2657 /* XXX status? */
2658 ieee80211_tx_complete(data->ni, data->m, 0);
2659 data->m = NULL;
2660 data->ni = NULL;
2661 }
2662 }
2663
2664 static void
rsu_bulk_tx_callback_sub(struct usb_xfer * xfer,usb_error_t error,uint8_t which)2665 rsu_bulk_tx_callback_sub(struct usb_xfer *xfer, usb_error_t error,
2666 uint8_t which)
2667 {
2668 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2669 struct ieee80211com *ic = &sc->sc_ic;
2670 struct rsu_data *data;
2671
2672 RSU_ASSERT_LOCKED(sc);
2673
2674 switch (USB_GET_STATE(xfer)) {
2675 case USB_ST_TRANSFERRED:
2676 data = STAILQ_FIRST(&sc->sc_tx_active[which]);
2677 if (data == NULL)
2678 goto tr_setup;
2679 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: transfer done %p\n",
2680 __func__, data);
2681 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
2682 rsu_txeof(xfer, data);
2683 rsu_freebuf(sc, data);
2684 /* FALLTHROUGH */
2685 case USB_ST_SETUP:
2686 tr_setup:
2687 data = STAILQ_FIRST(&sc->sc_tx_pending[which]);
2688 if (data == NULL) {
2689 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE,
2690 "%s: empty pending queue sc %p\n", __func__, sc);
2691 return;
2692 }
2693 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending[which], next);
2694 STAILQ_INSERT_TAIL(&sc->sc_tx_active[which], data, next);
2695 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2696 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE,
2697 "%s: submitting transfer %p\n",
2698 __func__,
2699 data);
2700 usbd_transfer_submit(xfer);
2701 break;
2702 default:
2703 data = STAILQ_FIRST(&sc->sc_tx_active[which]);
2704 if (data != NULL) {
2705 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
2706 rsu_txeof(xfer, data);
2707 rsu_freebuf(sc, data);
2708 }
2709 counter_u64_add(ic->ic_oerrors, 1);
2710
2711 if (error != USB_ERR_CANCELLED) {
2712 usbd_xfer_set_stall(xfer);
2713 goto tr_setup;
2714 }
2715 break;
2716 }
2717
2718 /*
2719 * XXX TODO: if the queue is low, flush out FF TX frames.
2720 * Remember to unlock the driver for now; net80211 doesn't
2721 * defer it for us.
2722 */
2723 }
2724
2725 static void
rsu_bulk_tx_callback_be_bk(struct usb_xfer * xfer,usb_error_t error)2726 rsu_bulk_tx_callback_be_bk(struct usb_xfer *xfer, usb_error_t error)
2727 {
2728 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2729
2730 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_BE_BK);
2731
2732 /* This kicks the TX taskqueue */
2733 rsu_start(sc);
2734 }
2735
2736 static void
rsu_bulk_tx_callback_vi_vo(struct usb_xfer * xfer,usb_error_t error)2737 rsu_bulk_tx_callback_vi_vo(struct usb_xfer *xfer, usb_error_t error)
2738 {
2739 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2740
2741 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_VI_VO);
2742
2743 /* This kicks the TX taskqueue */
2744 rsu_start(sc);
2745 }
2746
2747 static void
rsu_bulk_tx_callback_h2c(struct usb_xfer * xfer,usb_error_t error)2748 rsu_bulk_tx_callback_h2c(struct usb_xfer *xfer, usb_error_t error)
2749 {
2750 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2751
2752 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_H2C);
2753
2754 /* This kicks the TX taskqueue */
2755 rsu_start(sc);
2756 }
2757
2758 /*
2759 * Transmit the given frame.
2760 *
2761 * This doesn't free the node or mbuf upon failure.
2762 */
2763 static int
rsu_tx_start(struct rsu_softc * sc,struct ieee80211_node * ni,struct mbuf * m0,struct rsu_data * data)2764 rsu_tx_start(struct rsu_softc *sc, struct ieee80211_node *ni,
2765 struct mbuf *m0, struct rsu_data *data)
2766 {
2767 const struct ieee80211_txparam *tp = ni->ni_txparms;
2768 struct ieee80211vap *vap = ni->ni_vap;
2769 struct ieee80211_frame *wh;
2770 struct ieee80211_key *k = NULL;
2771 struct r92s_tx_desc *txd;
2772 uint8_t rate, ridx, type, cipher, qos;
2773 int prio = 0;
2774 uint8_t which;
2775 int hasqos;
2776 int ismcast;
2777 int xferlen;
2778 int qid;
2779
2780 RSU_ASSERT_LOCKED(sc);
2781
2782 wh = mtod(m0, struct ieee80211_frame *);
2783 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2784 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2785
2786 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: data=%p, m=%p\n",
2787 __func__, data, m0);
2788
2789 /* Choose a TX rate index. */
2790 if (type == IEEE80211_FC0_TYPE_MGT ||
2791 type == IEEE80211_FC0_TYPE_CTL ||
2792 (m0->m_flags & M_EAPOL) != 0)
2793 rate = tp->mgmtrate;
2794 else if (ismcast)
2795 rate = tp->mcastrate;
2796 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2797 rate = tp->ucastrate;
2798 else
2799 rate = 0;
2800
2801 if (rate != 0)
2802 ridx = rate2ridx(rate);
2803
2804 /* Assign sequence number, A-MPDU or otherwise */
2805 ieee80211_output_seqno_assign(ni, -1, m0);
2806
2807 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2808 k = ieee80211_crypto_encap(ni, m0);
2809 if (k == NULL) {
2810 device_printf(sc->sc_dev,
2811 "ieee80211_crypto_encap returns NULL.\n");
2812 /* XXX we don't expect the fragmented frames */
2813 return (ENOBUFS);
2814 }
2815 wh = mtod(m0, struct ieee80211_frame *);
2816 }
2817 /* If we have QoS then use it */
2818 /* XXX TODO: mbuf WME/PRI versus TID? */
2819 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2820 /* Has QoS */
2821 prio = M_WME_GETAC(m0);
2822 which = rsu_wme_ac_xfer_map[prio];
2823 hasqos = 1;
2824 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
2825 } else {
2826 /* Non-QoS TID */
2827 /* XXX TODO: tid=0 for non-qos TID? */
2828 which = rsu_wme_ac_xfer_map[WME_AC_BE];
2829 hasqos = 0;
2830 prio = 0;
2831 qos = 0;
2832 }
2833
2834 qid = rsu_ac2qid[prio];
2835 #if 0
2836 switch (type) {
2837 case IEEE80211_FC0_TYPE_CTL:
2838 case IEEE80211_FC0_TYPE_MGT:
2839 which = rsu_wme_ac_xfer_map[WME_AC_VO];
2840 break;
2841 default:
2842 which = rsu_wme_ac_xfer_map[M_WME_GETAC(m0)];
2843 break;
2844 }
2845 hasqos = 0;
2846 #endif
2847
2848 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: pri=%d, which=%d, hasqos=%d\n",
2849 __func__,
2850 prio,
2851 which,
2852 hasqos);
2853
2854 /* Fill Tx descriptor. */
2855 txd = (struct r92s_tx_desc *)data->buf;
2856 memset(txd, 0, sizeof(*txd));
2857
2858 txd->txdw0 |= htole32(
2859 SM(R92S_TXDW0_PKTLEN, m0->m_pkthdr.len) |
2860 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
2861 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
2862
2863 /* TODO: correct macid here? It should be in the node */
2864 txd->txdw1 |= htole32(
2865 SM(R92S_TXDW1_MACID, R92S_MACID_BSS) | SM(R92S_TXDW1_QSEL, qid));
2866
2867 if (!hasqos)
2868 txd->txdw1 |= htole32(R92S_TXDW1_NONQOS);
2869 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWENCRYPT)) {
2870 switch (k->wk_cipher->ic_cipher) {
2871 case IEEE80211_CIPHER_WEP:
2872 cipher = R92S_TXDW1_CIPHER_WEP;
2873 break;
2874 case IEEE80211_CIPHER_TKIP:
2875 cipher = R92S_TXDW1_CIPHER_TKIP;
2876 break;
2877 case IEEE80211_CIPHER_AES_CCM:
2878 cipher = R92S_TXDW1_CIPHER_AES;
2879 break;
2880 default:
2881 cipher = R92S_TXDW1_CIPHER_NONE;
2882 }
2883 txd->txdw1 |= htole32(
2884 SM(R92S_TXDW1_CIPHER, cipher) |
2885 SM(R92S_TXDW1_KEYIDX, k->wk_keyix));
2886 }
2887
2888 /*
2889 * Note: no need to set TXDW2_AGGEN/TXDW2_BK to mark
2890 * A-MPDU and non-AMPDU candidates; the firmware will
2891 * handle this for us.
2892 */
2893
2894 if (ismcast)
2895 txd->txdw2 |= htole32(R92S_TXDW2_BMCAST);
2896
2897 if (!ismcast && (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
2898 IEEE80211_QOS_ACKPOLICY_NOACK)) {
2899 txd->txdw2 |= htole32(R92S_TXDW2_RTY_LMT_ENA);
2900 txd->txdw2 |= htole32(SM(R92S_TXDW2_RTY_LMT, tp->maxretry));
2901 }
2902
2903 /* Force mgmt / mcast / ucast rate if needed. */
2904 if (rate != 0) {
2905 /* Data rate fallback limit (max). */
2906 txd->txdw5 |= htole32(SM(R92S_TXDW5_DATARATE_FB_LMT, 0x1f));
2907 txd->txdw5 |= htole32(SM(R92S_TXDW5_DATARATE, ridx));
2908 txd->txdw4 |= htole32(R92S_TXDW4_DRVRATE);
2909 }
2910
2911 /*
2912 * Pass in prio here, NOT the sequence number.
2913 *
2914 * The hardware is in theory incrementing sequence numbers
2915 * for us, but I haven't yet figured out exactly when/how
2916 * it's supposed to work.
2917 */
2918 txd->txdw3 |= htole32(SM(R92S_TXDW3_SEQ, prio));
2919
2920 if (ieee80211_radiotap_active_vap(vap)) {
2921 struct rsu_tx_radiotap_header *tap = &sc->sc_txtap;
2922
2923 tap->wt_flags = 0;
2924 ieee80211_radiotap_tx(vap, m0);
2925 }
2926
2927 xferlen = sizeof(*txd) + m0->m_pkthdr.len;
2928 KASSERT(xferlen <= RSU_TXBUFSZ, ("%s: invalid length", __func__));
2929 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&txd[1]);
2930
2931 data->buflen = xferlen;
2932 data->ni = ni;
2933 data->m = m0;
2934 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
2935
2936 /* start transfer, if any */
2937 usbd_transfer_start(sc->sc_xfer[which]);
2938 return (0);
2939 }
2940
2941 static int
rsu_transmit(struct ieee80211com * ic,struct mbuf * m)2942 rsu_transmit(struct ieee80211com *ic, struct mbuf *m)
2943 {
2944 struct rsu_softc *sc = ic->ic_softc;
2945 int error;
2946
2947 RSU_LOCK(sc);
2948 if (!sc->sc_running) {
2949 RSU_UNLOCK(sc);
2950 return (ENXIO);
2951 }
2952
2953 /*
2954 * XXX TODO: ensure that we treat 'm' as a list of frames
2955 * to transmit!
2956 */
2957 error = mbufq_enqueue(&sc->sc_snd, m);
2958 if (error) {
2959 RSU_DPRINTF(sc, RSU_DEBUG_TX,
2960 "%s: mbufq_enable: failed (%d)\n",
2961 __func__,
2962 error);
2963 RSU_UNLOCK(sc);
2964 return (error);
2965 }
2966 RSU_UNLOCK(sc);
2967
2968 /* This kicks the TX taskqueue */
2969 rsu_start(sc);
2970
2971 return (0);
2972 }
2973
2974 static void
rsu_drain_mbufq(struct rsu_softc * sc)2975 rsu_drain_mbufq(struct rsu_softc *sc)
2976 {
2977 struct mbuf *m;
2978 struct ieee80211_node *ni;
2979
2980 RSU_ASSERT_LOCKED(sc);
2981 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2982 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2983 m->m_pkthdr.rcvif = NULL;
2984 ieee80211_free_node(ni);
2985 m_freem(m);
2986 }
2987 }
2988
2989 static void
_rsu_start(struct rsu_softc * sc)2990 _rsu_start(struct rsu_softc *sc)
2991 {
2992 struct ieee80211_node *ni;
2993 struct rsu_data *bf;
2994 struct mbuf *m;
2995
2996 RSU_ASSERT_LOCKED(sc);
2997
2998 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2999 bf = rsu_getbuf(sc);
3000 if (bf == NULL) {
3001 RSU_DPRINTF(sc, RSU_DEBUG_TX,
3002 "%s: failed to get buffer\n", __func__);
3003 mbufq_prepend(&sc->sc_snd, m);
3004 break;
3005 }
3006
3007 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3008 m->m_pkthdr.rcvif = NULL;
3009
3010 if (rsu_tx_start(sc, ni, m, bf) != 0) {
3011 RSU_DPRINTF(sc, RSU_DEBUG_TX,
3012 "%s: failed to transmit\n", __func__);
3013 if_inc_counter(ni->ni_vap->iv_ifp,
3014 IFCOUNTER_OERRORS, 1);
3015 rsu_freebuf(sc, bf);
3016 ieee80211_free_node(ni);
3017 m_freem(m);
3018 break;
3019 }
3020 }
3021 }
3022
3023 static void
rsu_start(struct rsu_softc * sc)3024 rsu_start(struct rsu_softc *sc)
3025 {
3026
3027 taskqueue_enqueue(taskqueue_thread, &sc->tx_task);
3028 }
3029
3030 static int
rsu_ioctl_net(struct ieee80211com * ic,u_long cmd,void * data)3031 rsu_ioctl_net(struct ieee80211com *ic, u_long cmd, void *data)
3032 {
3033 struct rsu_softc *sc = ic->ic_softc;
3034 struct ifreq *ifr = (struct ifreq *)data;
3035 int error;
3036
3037 error = 0;
3038 switch (cmd) {
3039 case SIOCSIFCAP:
3040 {
3041 struct ieee80211vap *vap;
3042 int rxmask;
3043
3044 rxmask = ifr->ifr_reqcap & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
3045
3046 RSU_LOCK(sc);
3047 /* Both RXCSUM bits must be set (or unset). */
3048 if (sc->sc_rx_checksum_enable &&
3049 rxmask != (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
3050 rxmask = 0;
3051 sc->sc_rx_checksum_enable = 0;
3052 rsu_rxfilter_set(sc, R92S_RCR_TCP_OFFLD_EN, 0);
3053 } else if (!sc->sc_rx_checksum_enable && rxmask != 0) {
3054 rxmask = IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
3055 sc->sc_rx_checksum_enable = 1;
3056 rsu_rxfilter_set(sc, 0, R92S_RCR_TCP_OFFLD_EN);
3057 } else {
3058 /* Nothing to do. */
3059 RSU_UNLOCK(sc);
3060 break;
3061 }
3062 RSU_UNLOCK(sc);
3063
3064 IEEE80211_LOCK(ic); /* XXX */
3065 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
3066 if_t ifp = vap->iv_ifp;
3067
3068 if_setcapenablebit(ifp, 0,
3069 IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
3070 if_setcapenablebit(ifp, rxmask, 0);
3071 }
3072 IEEE80211_UNLOCK(ic);
3073 break;
3074 }
3075 default:
3076 error = ENOTTY; /* for net80211 */
3077 break;
3078 }
3079
3080 return (error);
3081 }
3082
3083 static void
rsu_parent(struct ieee80211com * ic)3084 rsu_parent(struct ieee80211com *ic)
3085 {
3086 struct rsu_softc *sc = ic->ic_softc;
3087
3088 if (ic->ic_nrunning > 0) {
3089 if (rsu_init(sc) == 0)
3090 ieee80211_start_all(ic);
3091 else {
3092 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3093 if (vap != NULL)
3094 ieee80211_stop(vap);
3095 }
3096 } else
3097 rsu_stop(sc);
3098 }
3099
3100 /*
3101 * Power on sequence for A-cut adapters.
3102 */
3103 static void
rsu_power_on_acut(struct rsu_softc * sc)3104 rsu_power_on_acut(struct rsu_softc *sc)
3105 {
3106 uint32_t reg;
3107
3108 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
3109 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
3110
3111 /* Enable AFE macro block's bandgap and Mbias. */
3112 rsu_write_1(sc, R92S_AFE_MISC,
3113 rsu_read_1(sc, R92S_AFE_MISC) |
3114 R92S_AFE_MISC_BGEN | R92S_AFE_MISC_MBEN);
3115 /* Enable LDOA15 block. */
3116 rsu_write_1(sc, R92S_LDOA15_CTRL,
3117 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
3118
3119 rsu_write_1(sc, R92S_SPS1_CTRL,
3120 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_LDEN);
3121 rsu_ms_delay(sc, 2000);
3122 /* Enable switch regulator block. */
3123 rsu_write_1(sc, R92S_SPS1_CTRL,
3124 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_SWEN);
3125
3126 rsu_write_4(sc, R92S_SPS1_CTRL, 0x00a7b267);
3127
3128 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3129 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
3130
3131 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3132 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
3133
3134 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3135 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x90);
3136
3137 /* Enable AFE clock. */
3138 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
3139 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
3140 /* Enable AFE PLL macro block. */
3141 rsu_write_1(sc, R92S_AFE_PLL_CTRL,
3142 rsu_read_1(sc, R92S_AFE_PLL_CTRL) | 0x11);
3143 /* Attach AFE PLL to MACTOP/BB. */
3144 rsu_write_1(sc, R92S_SYS_ISO_CTRL,
3145 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
3146
3147 /* Switch to 40MHz clock instead of 80MHz. */
3148 rsu_write_2(sc, R92S_SYS_CLKR,
3149 rsu_read_2(sc, R92S_SYS_CLKR) & ~R92S_SYS_CLKSEL);
3150
3151 /* Enable MAC clock. */
3152 rsu_write_2(sc, R92S_SYS_CLKR,
3153 rsu_read_2(sc, R92S_SYS_CLKR) |
3154 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
3155
3156 rsu_write_1(sc, R92S_PMC_FSM, 0x02);
3157
3158 /* Enable digital core and IOREG R/W. */
3159 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3160 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
3161
3162 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3163 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
3164
3165 /* Switch the control path to firmware. */
3166 reg = rsu_read_2(sc, R92S_SYS_CLKR);
3167 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
3168 rsu_write_2(sc, R92S_SYS_CLKR, reg);
3169
3170 rsu_write_2(sc, R92S_CR, 0x37fc);
3171
3172 /* Fix USB RX FIFO issue. */
3173 rsu_write_1(sc, 0xfe5c,
3174 rsu_read_1(sc, 0xfe5c) | 0x80);
3175 rsu_write_1(sc, 0x00ab,
3176 rsu_read_1(sc, 0x00ab) | 0xc0);
3177
3178 rsu_write_1(sc, R92S_SYS_CLKR,
3179 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
3180 }
3181
3182 /*
3183 * Power on sequence for B-cut and C-cut adapters.
3184 */
3185 static void
rsu_power_on_bcut(struct rsu_softc * sc)3186 rsu_power_on_bcut(struct rsu_softc *sc)
3187 {
3188 uint32_t reg;
3189 int ntries;
3190
3191 /* Prevent eFuse leakage. */
3192 rsu_write_1(sc, 0x37, 0xb0);
3193 rsu_ms_delay(sc, 10);
3194 rsu_write_1(sc, 0x37, 0x30);
3195
3196 /* Switch the control path to hardware. */
3197 reg = rsu_read_2(sc, R92S_SYS_CLKR);
3198 if (reg & R92S_FWHW_SEL) {
3199 rsu_write_2(sc, R92S_SYS_CLKR,
3200 reg & ~(R92S_SWHW_SEL | R92S_FWHW_SEL));
3201 }
3202 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3203 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) & ~0x8c);
3204 rsu_ms_delay(sc, 1);
3205
3206 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
3207 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
3208
3209 reg = rsu_read_1(sc, R92S_AFE_MISC);
3210 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN);
3211 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN |
3212 R92S_AFE_MISC_MBEN | R92S_AFE_MISC_I32_EN);
3213
3214 /* Enable PLL. */
3215 rsu_write_1(sc, R92S_LDOA15_CTRL,
3216 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
3217
3218 rsu_write_1(sc, R92S_LDOV12D_CTRL,
3219 rsu_read_1(sc, R92S_LDOV12D_CTRL) | R92S_LDV12_EN);
3220
3221 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3222 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
3223
3224 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3225 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
3226
3227 /* Support 64KB IMEM. */
3228 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3229 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x97);
3230
3231 /* Enable AFE clock. */
3232 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
3233 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
3234 /* Enable AFE PLL macro block. */
3235 reg = rsu_read_1(sc, R92S_AFE_PLL_CTRL);
3236 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
3237 rsu_ms_delay(sc, 1);
3238 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x51);
3239 rsu_ms_delay(sc, 1);
3240 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
3241 rsu_ms_delay(sc, 1);
3242
3243 /* Attach AFE PLL to MACTOP/BB. */
3244 rsu_write_1(sc, R92S_SYS_ISO_CTRL,
3245 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
3246
3247 /* Switch to 40MHz clock. */
3248 rsu_write_1(sc, R92S_SYS_CLKR, 0x00);
3249 /* Disable CPU clock and 80MHz SSC. */
3250 rsu_write_1(sc, R92S_SYS_CLKR,
3251 rsu_read_1(sc, R92S_SYS_CLKR) | 0xa0);
3252 /* Enable MAC clock. */
3253 rsu_write_2(sc, R92S_SYS_CLKR,
3254 rsu_read_2(sc, R92S_SYS_CLKR) |
3255 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
3256
3257 rsu_write_1(sc, R92S_PMC_FSM, 0x02);
3258
3259 /* Enable digital core and IOREG R/W. */
3260 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3261 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
3262
3263 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3264 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
3265
3266 /* Switch the control path to firmware. */
3267 reg = rsu_read_2(sc, R92S_SYS_CLKR);
3268 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
3269 rsu_write_2(sc, R92S_SYS_CLKR, reg);
3270
3271 rsu_write_2(sc, R92S_CR, 0x37fc);
3272
3273 /* Fix USB RX FIFO issue. */
3274 rsu_write_1(sc, 0xfe5c,
3275 rsu_read_1(sc, 0xfe5c) | 0x80);
3276
3277 rsu_write_1(sc, R92S_SYS_CLKR,
3278 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
3279
3280 rsu_write_1(sc, 0xfe1c, 0x80);
3281
3282 /* Make sure TxDMA is ready to download firmware. */
3283 for (ntries = 0; ntries < 20; ntries++) {
3284 reg = rsu_read_1(sc, R92S_TCR);
3285 if ((reg & (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT)) ==
3286 (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT))
3287 break;
3288 rsu_ms_delay(sc, 1);
3289 }
3290 if (ntries == 20) {
3291 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_TX,
3292 "%s: TxDMA is not ready\n",
3293 __func__);
3294 /* Reset TxDMA. */
3295 reg = rsu_read_1(sc, R92S_CR);
3296 rsu_write_1(sc, R92S_CR, reg & ~R92S_CR_TXDMA_EN);
3297 rsu_ms_delay(sc, 1);
3298 rsu_write_1(sc, R92S_CR, reg | R92S_CR_TXDMA_EN);
3299 }
3300 }
3301
3302 static void
rsu_power_off(struct rsu_softc * sc)3303 rsu_power_off(struct rsu_softc *sc)
3304 {
3305 /* Turn RF off. */
3306 rsu_write_1(sc, R92S_RF_CTRL, 0x00);
3307 rsu_ms_delay(sc, 5);
3308
3309 /* Turn MAC off. */
3310 /* Switch control path. */
3311 rsu_write_1(sc, R92S_SYS_CLKR + 1, 0x38);
3312 /* Reset MACTOP. */
3313 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x70);
3314 rsu_write_1(sc, R92S_PMC_FSM, 0x06);
3315 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 0, 0xf9);
3316 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 0xe8);
3317
3318 /* Disable AFE PLL. */
3319 rsu_write_1(sc, R92S_AFE_PLL_CTRL, 0x00);
3320 /* Disable A15V. */
3321 rsu_write_1(sc, R92S_LDOA15_CTRL, 0x54);
3322 /* Disable eFuse 1.2V. */
3323 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x50);
3324 rsu_write_1(sc, R92S_LDOV12D_CTRL, 0x24);
3325 /* Enable AFE macro block's bandgap and Mbias. */
3326 rsu_write_1(sc, R92S_AFE_MISC, 0x30);
3327 /* Disable 1.6V LDO. */
3328 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x56);
3329 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x43);
3330
3331 /* Firmware - tell it to switch things off */
3332 (void) rsu_set_fw_power_state(sc, RSU_PWR_OFF);
3333 }
3334
3335 static int
rsu_fw_loadsection(struct rsu_softc * sc,const uint8_t * buf,int len)3336 rsu_fw_loadsection(struct rsu_softc *sc, const uint8_t *buf, int len)
3337 {
3338 const uint8_t which = rsu_wme_ac_xfer_map[WME_AC_VO];
3339 struct rsu_data *data;
3340 struct r92s_tx_desc *txd;
3341 int mlen;
3342
3343 while (len > 0) {
3344 data = rsu_getbuf(sc);
3345 if (data == NULL)
3346 return (ENOMEM);
3347 txd = (struct r92s_tx_desc *)data->buf;
3348 memset(txd, 0, sizeof(*txd));
3349 if (len <= RSU_TXBUFSZ - sizeof(*txd)) {
3350 /* Last chunk. */
3351 txd->txdw0 |= htole32(R92S_TXDW0_LINIP);
3352 mlen = len;
3353 } else
3354 mlen = RSU_TXBUFSZ - sizeof(*txd);
3355 txd->txdw0 |= htole32(SM(R92S_TXDW0_PKTLEN, mlen));
3356 memcpy(&txd[1], buf, mlen);
3357 data->buflen = sizeof(*txd) + mlen;
3358 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FW | RSU_DEBUG_RESET,
3359 "%s: starting transfer %p\n",
3360 __func__, data);
3361 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
3362 buf += mlen;
3363 len -= mlen;
3364 }
3365 usbd_transfer_start(sc->sc_xfer[which]);
3366 return (0);
3367 }
3368
3369 CTASSERT(sizeof(size_t) >= sizeof(uint32_t));
3370
3371 static int
rsu_load_firmware(struct rsu_softc * sc)3372 rsu_load_firmware(struct rsu_softc *sc)
3373 {
3374 const struct r92s_fw_hdr *hdr;
3375 struct r92s_fw_priv dmem;
3376 struct ieee80211com *ic = &sc->sc_ic;
3377 const uint8_t *imem, *emem;
3378 uint32_t imemsz, ememsz;
3379 const struct firmware *fw;
3380 size_t size;
3381 uint32_t reg;
3382 int ntries, error;
3383
3384 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY) {
3385 RSU_DPRINTF(sc, RSU_DEBUG_ANY,
3386 "%s: Firmware already loaded\n",
3387 __func__);
3388 return (0);
3389 }
3390
3391 RSU_UNLOCK(sc);
3392 /* Read firmware image from the filesystem. */
3393 if ((fw = firmware_get("rsu-rtl8712fw")) == NULL) {
3394 device_printf(sc->sc_dev,
3395 "%s: failed load firmware of file rsu-rtl8712fw\n",
3396 __func__);
3397 RSU_LOCK(sc);
3398 return (ENXIO);
3399 }
3400 RSU_LOCK(sc);
3401 size = fw->datasize;
3402 if (size < sizeof(*hdr)) {
3403 device_printf(sc->sc_dev, "firmware too short\n");
3404 error = EINVAL;
3405 goto fail;
3406 }
3407 hdr = (const struct r92s_fw_hdr *)fw->data;
3408 if (hdr->signature != htole16(0x8712) &&
3409 hdr->signature != htole16(0x8192)) {
3410 device_printf(sc->sc_dev,
3411 "invalid firmware signature 0x%x\n",
3412 le16toh(hdr->signature));
3413 error = EINVAL;
3414 goto fail;
3415 }
3416 RSU_DPRINTF(sc, RSU_DEBUG_FW, "FW V%d %02x-%02x %02x:%02x\n",
3417 le16toh(hdr->version), hdr->month, hdr->day, hdr->hour,
3418 hdr->minute);
3419
3420 /* Make sure that driver and firmware are in sync. */
3421 if (hdr->privsz != htole32(sizeof(dmem))) {
3422 device_printf(sc->sc_dev, "unsupported firmware image\n");
3423 error = EINVAL;
3424 goto fail;
3425 }
3426 /* Get FW sections sizes. */
3427 imemsz = le32toh(hdr->imemsz);
3428 ememsz = le32toh(hdr->sramsz);
3429 /* Check that all FW sections fit in image. */
3430 if (imemsz > (size_t)(size - sizeof(*hdr)) ||
3431 ememsz > (size_t)(size - sizeof(*hdr) - imemsz)) {
3432 device_printf(sc->sc_dev, "firmware too short\n");
3433 error = EINVAL;
3434 goto fail;
3435 }
3436 imem = (const uint8_t *)&hdr[1];
3437 emem = imem + imemsz;
3438
3439 /* Load IMEM section. */
3440 error = rsu_fw_loadsection(sc, imem, imemsz);
3441 if (error != 0) {
3442 device_printf(sc->sc_dev,
3443 "could not load firmware section %s\n", "IMEM");
3444 goto fail;
3445 }
3446 /* Wait for load to complete. */
3447 for (ntries = 0; ntries != 50; ntries++) {
3448 rsu_ms_delay(sc, 10);
3449 reg = rsu_read_1(sc, R92S_TCR);
3450 if (reg & R92S_TCR_IMEM_CODE_DONE)
3451 break;
3452 }
3453 if (ntries == 50) {
3454 device_printf(sc->sc_dev, "timeout waiting for IMEM transfer\n");
3455 error = ETIMEDOUT;
3456 goto fail;
3457 }
3458 /* Load EMEM section. */
3459 error = rsu_fw_loadsection(sc, emem, ememsz);
3460 if (error != 0) {
3461 device_printf(sc->sc_dev,
3462 "could not load firmware section %s\n", "EMEM");
3463 goto fail;
3464 }
3465 /* Wait for load to complete. */
3466 for (ntries = 0; ntries != 50; ntries++) {
3467 rsu_ms_delay(sc, 10);
3468 reg = rsu_read_2(sc, R92S_TCR);
3469 if (reg & R92S_TCR_EMEM_CODE_DONE)
3470 break;
3471 }
3472 if (ntries == 50) {
3473 device_printf(sc->sc_dev, "timeout waiting for EMEM transfer\n");
3474 error = ETIMEDOUT;
3475 goto fail;
3476 }
3477 /* Enable CPU. */
3478 rsu_write_1(sc, R92S_SYS_CLKR,
3479 rsu_read_1(sc, R92S_SYS_CLKR) | R92S_SYS_CPU_CLKSEL);
3480 if (!(rsu_read_1(sc, R92S_SYS_CLKR) & R92S_SYS_CPU_CLKSEL)) {
3481 device_printf(sc->sc_dev, "could not enable system clock\n");
3482 error = EIO;
3483 goto fail;
3484 }
3485 rsu_write_2(sc, R92S_SYS_FUNC_EN,
3486 rsu_read_2(sc, R92S_SYS_FUNC_EN) | R92S_FEN_CPUEN);
3487 if (!(rsu_read_2(sc, R92S_SYS_FUNC_EN) & R92S_FEN_CPUEN)) {
3488 device_printf(sc->sc_dev,
3489 "could not enable microcontroller\n");
3490 error = EIO;
3491 goto fail;
3492 }
3493 /* Wait for CPU to initialize. */
3494 for (ntries = 0; ntries < 100; ntries++) {
3495 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_IMEM_RDY)
3496 break;
3497 rsu_ms_delay(sc, 1);
3498 }
3499 if (ntries == 100) {
3500 device_printf(sc->sc_dev,
3501 "timeout waiting for microcontroller\n");
3502 error = ETIMEDOUT;
3503 goto fail;
3504 }
3505
3506 /* Update DMEM section before loading. */
3507 memset(&dmem, 0, sizeof(dmem));
3508 dmem.hci_sel = R92S_HCI_SEL_USB | R92S_HCI_SEL_8172;
3509 dmem.nendpoints = sc->sc_nendpoints;
3510 dmem.chip_version = sc->cut;
3511 dmem.rf_config = sc->sc_rftype;
3512 dmem.vcs_type = R92S_VCS_TYPE_AUTO;
3513 dmem.vcs_mode = R92S_VCS_MODE_RTS_CTS;
3514 dmem.turbo_mode = 0;
3515 dmem.bw40_en = !! (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40);
3516 /* net80211 handles AMSDUs just fine */
3517 dmem.amsdu2ampdu_en = 0;
3518 dmem.ampdu_en = !! (sc->sc_ht);
3519 dmem.agg_offload = !! (sc->sc_ht);
3520 dmem.qos_en = 1;
3521 dmem.ps_offload = 1;
3522 dmem.lowpower_mode = 1; /* XXX TODO: configurable? */
3523 /* Load DMEM section. */
3524 error = rsu_fw_loadsection(sc, (uint8_t *)&dmem, sizeof(dmem));
3525 if (error != 0) {
3526 device_printf(sc->sc_dev,
3527 "could not load firmware section %s\n", "DMEM");
3528 goto fail;
3529 }
3530 /* Wait for load to complete. */
3531 for (ntries = 0; ntries < 100; ntries++) {
3532 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_DMEM_CODE_DONE)
3533 break;
3534 rsu_ms_delay(sc, 1);
3535 }
3536 if (ntries == 100) {
3537 device_printf(sc->sc_dev, "timeout waiting for %s transfer\n",
3538 "DMEM");
3539 error = ETIMEDOUT;
3540 goto fail;
3541 }
3542 /* Wait for firmware readiness. */
3543 for (ntries = 0; ntries < 60; ntries++) {
3544 if (!(rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY))
3545 break;
3546 rsu_ms_delay(sc, 1);
3547 }
3548 if (ntries == 60) {
3549 device_printf(sc->sc_dev,
3550 "timeout waiting for firmware readiness\n");
3551 error = ETIMEDOUT;
3552 goto fail;
3553 }
3554 fail:
3555 firmware_put(fw, FIRMWARE_UNLOAD);
3556 return (error);
3557 }
3558
3559 static int
rsu_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)3560 rsu_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3561 const struct ieee80211_bpf_params *params)
3562 {
3563 struct ieee80211com *ic = ni->ni_ic;
3564 struct rsu_softc *sc = ic->ic_softc;
3565 struct rsu_data *bf;
3566
3567 /* prevent management frames from being sent if we're not ready */
3568 if (!sc->sc_running) {
3569 m_freem(m);
3570 return (ENETDOWN);
3571 }
3572 RSU_LOCK(sc);
3573 bf = rsu_getbuf(sc);
3574 if (bf == NULL) {
3575 m_freem(m);
3576 RSU_UNLOCK(sc);
3577 return (ENOBUFS);
3578 }
3579 if (rsu_tx_start(sc, ni, m, bf) != 0) {
3580 m_freem(m);
3581 rsu_freebuf(sc, bf);
3582 RSU_UNLOCK(sc);
3583 return (EIO);
3584 }
3585 RSU_UNLOCK(sc);
3586
3587 return (0);
3588 }
3589
3590 static void
rsu_rxfilter_init(struct rsu_softc * sc)3591 rsu_rxfilter_init(struct rsu_softc *sc)
3592 {
3593 uint32_t reg;
3594
3595 RSU_ASSERT_LOCKED(sc);
3596
3597 /* Setup multicast filter. */
3598 rsu_set_multi(sc);
3599
3600 /* Adjust Rx filter. */
3601 reg = rsu_read_4(sc, R92S_RCR);
3602 reg &= ~R92S_RCR_AICV;
3603 reg |= R92S_RCR_APP_PHYSTS;
3604 if (sc->sc_rx_checksum_enable)
3605 reg |= R92S_RCR_TCP_OFFLD_EN;
3606 rsu_write_4(sc, R92S_RCR, reg);
3607
3608 /* Update dynamic Rx filter parts. */
3609 rsu_rxfilter_refresh(sc);
3610 }
3611
3612 static void
rsu_rxfilter_set(struct rsu_softc * sc,uint32_t clear,uint32_t set)3613 rsu_rxfilter_set(struct rsu_softc *sc, uint32_t clear, uint32_t set)
3614 {
3615 /* NB: firmware can touch this register too. */
3616 rsu_write_4(sc, R92S_RCR,
3617 (rsu_read_4(sc, R92S_RCR) & ~clear) | set);
3618 }
3619
3620 static void
rsu_rxfilter_refresh(struct rsu_softc * sc)3621 rsu_rxfilter_refresh(struct rsu_softc *sc)
3622 {
3623 struct ieee80211com *ic = &sc->sc_ic;
3624 uint32_t mask_all, mask_min;
3625
3626 RSU_ASSERT_LOCKED(sc);
3627
3628 /* NB: RCR_AMF / RXFLTMAP_MGT are used by firmware. */
3629 mask_all = R92S_RCR_ACF | R92S_RCR_AAP;
3630 mask_min = R92S_RCR_APM;
3631 if (sc->sc_vap_is_running)
3632 mask_min |= R92S_RCR_CBSSID;
3633 else
3634 mask_all |= R92S_RCR_ADF;
3635
3636 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
3637 uint16_t rxfltmap;
3638 if (sc->sc_vap_is_running)
3639 rxfltmap = 0;
3640 else
3641 rxfltmap = R92S_RXFLTMAP_MGT_DEF;
3642 rsu_write_2(sc, R92S_RXFLTMAP_MGT, rxfltmap);
3643 }
3644
3645 if (ic->ic_promisc == 0 && ic->ic_opmode != IEEE80211_M_MONITOR)
3646 rsu_rxfilter_set(sc, mask_all, mask_min);
3647 else
3648 rsu_rxfilter_set(sc, mask_min, mask_all);
3649 }
3650
3651 static int
rsu_init(struct rsu_softc * sc)3652 rsu_init(struct rsu_softc *sc)
3653 {
3654 struct ieee80211com *ic = &sc->sc_ic;
3655 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3656 uint8_t macaddr[IEEE80211_ADDR_LEN];
3657 int error;
3658 int i;
3659
3660 RSU_LOCK(sc);
3661
3662 if (sc->sc_running) {
3663 RSU_UNLOCK(sc);
3664 return (0);
3665 }
3666
3667 /* Ensure the mbuf queue is drained */
3668 rsu_drain_mbufq(sc);
3669
3670 /* Reset power management state. */
3671 rsu_write_1(sc, R92S_USB_HRPWM, 0);
3672
3673 /* Power on adapter. */
3674 if (sc->cut == 1)
3675 rsu_power_on_acut(sc);
3676 else
3677 rsu_power_on_bcut(sc);
3678
3679 /* Load firmware. */
3680 error = rsu_load_firmware(sc);
3681 if (error != 0)
3682 goto fail;
3683
3684 rsu_write_4(sc, R92S_CR,
3685 rsu_read_4(sc, R92S_CR) & ~0xff000000);
3686
3687 /* Use 128 bytes pages. */
3688 rsu_write_1(sc, 0x00b5,
3689 rsu_read_1(sc, 0x00b5) | 0x01);
3690 /* Enable USB Rx aggregation. */
3691 rsu_write_1(sc, 0x00bd,
3692 rsu_read_1(sc, 0x00bd) | 0x80);
3693 /* Set USB Rx aggregation threshold. */
3694 rsu_write_1(sc, 0x00d9, 0x01);
3695 /* Set USB Rx aggregation timeout (1.7ms/4). */
3696 rsu_write_1(sc, 0xfe5b, 0x04);
3697 /* Fix USB Rx FIFO issue. */
3698 rsu_write_1(sc, 0xfe5c,
3699 rsu_read_1(sc, 0xfe5c) | 0x80);
3700
3701 /* Set MAC address. */
3702 IEEE80211_ADDR_COPY(macaddr, vap ? vap->iv_myaddr : ic->ic_macaddr);
3703 rsu_write_region_1(sc, R92S_MACID, macaddr, IEEE80211_ADDR_LEN);
3704
3705 /* It really takes 1.5 seconds for the firmware to boot: */
3706 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(2000));
3707
3708 RSU_DPRINTF(sc, RSU_DEBUG_RESET, "%s: setting MAC address to %s\n",
3709 __func__,
3710 ether_sprintf(macaddr));
3711 error = rsu_fw_cmd(sc, R92S_CMD_SET_MAC_ADDRESS, macaddr,
3712 IEEE80211_ADDR_LEN);
3713 if (error != 0) {
3714 device_printf(sc->sc_dev, "could not set MAC address\n");
3715 goto fail;
3716 }
3717
3718 /* Initialize Rx filter. */
3719 rsu_rxfilter_init(sc);
3720
3721 /* Set PS mode fully active */
3722 error = rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE);
3723 if (error != 0) {
3724 device_printf(sc->sc_dev, "could not set PS mode\n");
3725 goto fail;
3726 }
3727
3728 /* Install static keys (if any). */
3729 error = rsu_reinit_static_keys(sc);
3730 if (error != 0)
3731 goto fail;
3732
3733 sc->sc_extra_scan = 0;
3734 usbd_transfer_start(sc->sc_xfer[RSU_BULK_RX]);
3735
3736 /* We're ready to go. */
3737 sc->sc_running = 1;
3738 RSU_UNLOCK(sc);
3739
3740 return (0);
3741 fail:
3742 /* Need to stop all failed transfers, if any */
3743 for (i = 0; i != RSU_N_TRANSFER; i++)
3744 usbd_transfer_stop(sc->sc_xfer[i]);
3745 RSU_UNLOCK(sc);
3746
3747 return (error);
3748 }
3749
3750 static void
rsu_stop(struct rsu_softc * sc)3751 rsu_stop(struct rsu_softc *sc)
3752 {
3753 int i;
3754
3755 RSU_LOCK(sc);
3756 if (!sc->sc_running) {
3757 RSU_UNLOCK(sc);
3758 return;
3759 }
3760
3761 sc->sc_running = 0;
3762 sc->sc_vap_is_running = 0;
3763 sc->sc_calibrating = 0;
3764 taskqueue_cancel_timeout(taskqueue_thread, &sc->calib_task, NULL);
3765 taskqueue_cancel(taskqueue_thread, &sc->tx_task, NULL);
3766
3767 /* Power off adapter. */
3768 rsu_power_off(sc);
3769
3770 /*
3771 * CAM is not accessible after shutdown;
3772 * all entries are marked (by firmware?) as invalid.
3773 */
3774 memset(sc->free_keys_bmap, 0, sizeof(sc->free_keys_bmap));
3775 memset(sc->keys_bmap, 0, sizeof(sc->keys_bmap));
3776
3777 for (i = 0; i < RSU_N_TRANSFER; i++)
3778 usbd_transfer_stop(sc->sc_xfer[i]);
3779
3780 /* Ensure the mbuf queue is drained */
3781 rsu_drain_mbufq(sc);
3782 RSU_UNLOCK(sc);
3783 }
3784
3785 /*
3786 * Note: usb_pause_mtx() actually releases the mutex before calling pause(),
3787 * which breaks any kind of driver serialisation.
3788 */
3789 static void
rsu_ms_delay(struct rsu_softc * sc,int ms)3790 rsu_ms_delay(struct rsu_softc *sc, int ms)
3791 {
3792
3793 //usb_pause_mtx(&sc->sc_mtx, hz / 1000);
3794 DELAY(ms * 1000);
3795 }
3796