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