1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 #include <sys/cdefs.h>
22 #include "opt_wlan.h"
23
24 #include <sys/param.h>
25 #include <sys/lock.h>
26 #include <sys/mutex.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 #include <sys/taskqueue.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 #include <sys/linker.h>
37
38 #include <net/if.h>
39 #include <net/ethernet.h>
40 #include <net/if_media.h>
41
42 #include <net80211/ieee80211_var.h>
43 #include <net80211/ieee80211_radiotap.h>
44 #include <net80211/ieee80211_ratectl.h>
45
46 #include <dev/rtwn/if_rtwnreg.h>
47 #include <dev/rtwn/if_rtwnvar.h>
48
49 #include <dev/rtwn/if_rtwn_debug.h>
50 #include <dev/rtwn/if_rtwn_ridx.h>
51 #include <dev/rtwn/if_rtwn_rx.h>
52 #include <dev/rtwn/if_rtwn_task.h>
53 #include <dev/rtwn/if_rtwn_tx.h>
54
55 #include <dev/rtwn/rtl8192c/r92c.h>
56 #include <dev/rtwn/rtl8192c/r92c_reg.h>
57 #include <dev/rtwn/rtl8192c/r92c_var.h>
58 #include <dev/rtwn/rtl8192c/r92c_fw_cmd.h>
59 #include <dev/rtwn/rtl8192c/r92c_tx_desc.h>
60
61 #ifndef RTWN_WITHOUT_UCODE
62 static int
r92c_fw_cmd(struct rtwn_softc * sc,uint8_t id,const void * buf,int len)63 r92c_fw_cmd(struct rtwn_softc *sc, uint8_t id, const void *buf, int len)
64 {
65 struct r92c_fw_cmd cmd;
66 int ntries, error;
67
68 KASSERT(len <= sizeof(cmd.msg),
69 ("%s: firmware command too long (%d > %zu)\n",
70 __func__, len, sizeof(cmd.msg)));
71
72 if (!(sc->sc_flags & RTWN_FW_LOADED)) {
73 RTWN_DPRINTF(sc, RTWN_DEBUG_FIRMWARE, "%s: firmware "
74 "was not loaded; command (id %u) will be discarded\n",
75 __func__, id);
76 return (0);
77 }
78
79 /* Wait for current FW box to be empty. */
80 for (ntries = 0; ntries < 100; ntries++) {
81 if (!(rtwn_read_1(sc, R92C_HMETFR) & (1 << sc->fwcur)))
82 break;
83 rtwn_delay(sc, 2000);
84 }
85 if (ntries == 100) {
86 device_printf(sc->sc_dev,
87 "could not send firmware command\n");
88 return (ETIMEDOUT);
89 }
90 memset(&cmd, 0, sizeof(cmd));
91 cmd.id = id;
92 if (len > 3) {
93 /* Ext command: [id : byte2 : byte3 : byte4 : byte0 : byte1] */
94 cmd.id |= R92C_CMD_FLAG_EXT;
95 memcpy(cmd.msg, (const uint8_t *)buf + 2, len - 2);
96 memcpy(cmd.msg + 3, buf, 2);
97 } else
98 memcpy(cmd.msg, buf, len);
99
100 /* Write the first word last since that will trigger the FW. */
101 if (len > 3) {
102 error = rtwn_write_2(sc, R92C_HMEBOX_EXT(sc->fwcur),
103 *(uint16_t *)((uint8_t *)&cmd + 4));
104 if (error != 0)
105 return (error);
106 }
107 error = rtwn_write_4(sc, R92C_HMEBOX(sc->fwcur),
108 *(uint32_t *)&cmd);
109 if (error != 0)
110 return (error);
111
112 sc->fwcur = (sc->fwcur + 1) % R92C_H2C_NBOX;
113
114 return (0);
115 }
116
117 void
r92c_fw_reset(struct rtwn_softc * sc,int reason)118 r92c_fw_reset(struct rtwn_softc *sc, int reason)
119 {
120 int ntries;
121
122 if (reason == RTWN_FW_RESET_CHECKSUM)
123 return;
124
125 /* Tell 8051 to reset itself. */
126 rtwn_write_1(sc, R92C_HMETFR + 3, 0x20);
127
128 /* Wait until 8051 resets by itself. */
129 for (ntries = 0; ntries < 100; ntries++) {
130 if ((rtwn_read_2(sc, R92C_SYS_FUNC_EN) &
131 R92C_SYS_FUNC_EN_CPUEN) == 0)
132 return;
133 rtwn_delay(sc, 50);
134 }
135 /* Force 8051 reset. */
136 rtwn_setbits_1_shift(sc, R92C_SYS_FUNC_EN,
137 R92C_SYS_FUNC_EN_CPUEN, 0, 1);
138 }
139
140 void
r92c_fw_download_enable(struct rtwn_softc * sc,int enable)141 r92c_fw_download_enable(struct rtwn_softc *sc, int enable)
142 {
143 if (enable) {
144 /* 8051 enable. */
145 rtwn_setbits_1_shift(sc, R92C_SYS_FUNC_EN, 0,
146 R92C_SYS_FUNC_EN_CPUEN, 1);
147 /* MCU firmware download enable. */
148 rtwn_setbits_1(sc, R92C_MCUFWDL, 0, R92C_MCUFWDL_EN);
149 /* 8051 reset. */
150 rtwn_setbits_1_shift(sc, R92C_MCUFWDL, R92C_MCUFWDL_ROM_DLEN,
151 0, 2);
152 } else {
153 /* MCU download disable. */
154 rtwn_setbits_1(sc, R92C_MCUFWDL, R92C_MCUFWDL_EN, 0);
155 /* Reserved for f/w extension. */
156 rtwn_write_1(sc, R92C_MCUFWDL + 1, 0);
157 }
158 }
159 #endif
160
161 /*
162 * Initialize firmware rate adaptation.
163 */
164 #ifndef RTWN_WITHOUT_UCODE
165 static int
r92c_send_ra_cmd(struct rtwn_softc * sc,int macid,uint32_t rates,int maxrate)166 r92c_send_ra_cmd(struct rtwn_softc *sc, int macid, uint32_t rates,
167 int maxrate)
168 {
169 struct r92c_fw_cmd_macid_cfg cmd;
170 uint8_t mode;
171 int error = 0;
172
173 /* XXX should be called directly from iv_newstate() for MACID_BC */
174 /* XXX joinbss, not send_ra_cmd() */
175 #ifdef RTWN_TODO
176 /* NB: group addressed frames are done at 11bg rates for now */
177 if (ic->ic_curmode == IEEE80211_MODE_11B)
178 mode = R92C_RAID_11B;
179 else
180 mode = R92C_RAID_11BG;
181 /* XXX misleading 'mode' value here for unicast frames */
182 RTWN_DPRINTF(sc, RTWN_DEBUG_RA,
183 "%s: mode 0x%x, rates 0x%08x, basicrates 0x%08x\n", __func__,
184 mode, rates, basicrates);
185
186 /* Set rates mask for group addressed frames. */
187 cmd.macid = RTWN_MACID_BC | R92C_CMD_MACID_VALID;
188 cmd.mask = htole32(mode << 28 | basicrates);
189 error = rtwn_fw_cmd(sc, R92C_CMD_MACID_CONFIG, &cmd, sizeof(cmd));
190 if (error != 0) {
191 device_printf(sc->sc_dev,
192 "could not set RA mask for broadcast station\n");
193 return (error);
194 }
195 #endif
196
197 /* Set rates mask for unicast frames. */
198 if (maxrate >= RTWN_RIDX_HT_MCS(0))
199 mode = R92C_RAID_11GN;
200 else if (maxrate >= RTWN_RIDX_OFDM6)
201 mode = R92C_RAID_11BG;
202 else
203 mode = R92C_RAID_11B;
204 cmd.macid = macid | R92C_CMD_MACID_VALID;
205 cmd.mask = htole32(mode << 28 | rates);
206 error = r92c_fw_cmd(sc, R92C_CMD_MACID_CONFIG, &cmd, sizeof(cmd));
207 if (error != 0) {
208 device_printf(sc->sc_dev,
209 "%s: could not set RA mask for %d station\n",
210 __func__, macid);
211 return (error);
212 }
213
214 return (0);
215 }
216 #endif
217
218 static void
r92c_init_ra(struct rtwn_softc * sc,int macid)219 r92c_init_ra(struct rtwn_softc *sc, int macid)
220 {
221 struct ieee80211_htrateset *rs_ht;
222 struct ieee80211_node *ni;
223 uint32_t rates;
224 int maxrate;
225
226 RTWN_NT_LOCK(sc);
227 if (sc->node_list[macid] == NULL) {
228 RTWN_DPRINTF(sc, RTWN_DEBUG_RA, "%s: macid %d, ni is NULL\n",
229 __func__, macid);
230 RTWN_NT_UNLOCK(sc);
231 return;
232 }
233
234 ni = ieee80211_ref_node(sc->node_list[macid]);
235 if (ni->ni_flags & IEEE80211_NODE_HT)
236 rs_ht = &ni->ni_htrates;
237 else
238 rs_ht = NULL;
239 /* XXX MACID_BC */
240 rtwn_get_rates(sc, &ni->ni_rates, rs_ht, &rates, &maxrate, 0);
241 RTWN_NT_UNLOCK(sc);
242
243 #ifndef RTWN_WITHOUT_UCODE
244 if (sc->sc_ratectl == RTWN_RATECTL_FW) {
245 r92c_send_ra_cmd(sc, macid, rates, maxrate);
246 }
247 #endif
248
249 rtwn_write_1(sc, R92C_INIDATA_RATE_SEL(macid), maxrate);
250
251 ieee80211_free_node(ni);
252 }
253
254 void
r92c_joinbss_rpt(struct rtwn_softc * sc,int macid)255 r92c_joinbss_rpt(struct rtwn_softc *sc, int macid)
256 {
257 #ifndef RTWN_WITHOUT_UCODE
258 struct r92c_softc *rs = sc->sc_priv;
259 struct ieee80211vap *vap;
260 struct r92c_fw_cmd_joinbss_rpt cmd;
261
262 if (sc->vaps[0] == NULL) /* XXX fix */
263 goto end;
264
265 vap = &sc->vaps[0]->vap;
266 if ((vap->iv_state == IEEE80211_S_RUN) ^
267 !(rs->rs_flags & R92C_FLAG_ASSOCIATED))
268 goto end;
269
270 if (rs->rs_flags & R92C_FLAG_ASSOCIATED) {
271 cmd.mstatus = R92C_MSTATUS_DISASSOC;
272 rs->rs_flags &= ~R92C_FLAG_ASSOCIATED;
273 } else {
274 cmd.mstatus = R92C_MSTATUS_ASSOC;
275 rs->rs_flags |= R92C_FLAG_ASSOCIATED;
276 }
277
278 if (r92c_fw_cmd(sc, R92C_CMD_JOINBSS_RPT, &cmd, sizeof(cmd)) != 0) {
279 device_printf(sc->sc_dev, "%s: cannot change media status!\n",
280 __func__);
281 }
282
283 end:
284 #endif
285
286 /* TODO: init rates for RTWN_MACID_BC. */
287 if (macid & RTWN_MACID_VALID)
288 r92c_init_ra(sc, macid & ~RTWN_MACID_VALID);
289 }
290
291 #ifndef RTWN_WITHOUT_UCODE
292 int
r92c_set_rsvd_page(struct rtwn_softc * sc,int probe_resp,int null,int qos_null)293 r92c_set_rsvd_page(struct rtwn_softc *sc, int probe_resp, int null,
294 int qos_null)
295 {
296 struct r92c_fw_cmd_rsvdpage rsvd;
297
298 rsvd.probe_resp = probe_resp;
299 rsvd.ps_poll = 0;
300 rsvd.null_data = null;
301
302 return (r92c_fw_cmd(sc, R92C_CMD_RSVD_PAGE, &rsvd, sizeof(rsvd)));
303 }
304
305 int
r92c_set_pwrmode(struct rtwn_softc * sc,struct ieee80211vap * vap,int off)306 r92c_set_pwrmode(struct rtwn_softc *sc, struct ieee80211vap *vap,
307 int off)
308 {
309 struct r92c_fw_cmd_pwrmode mode;
310 int error;
311
312 /* XXX dm_RF_saving */
313
314 if (off && vap->iv_state == IEEE80211_S_RUN &&
315 (vap->iv_flags & IEEE80211_F_PMGTON))
316 mode.mode = R92C_PWRMODE_MIN;
317 else
318 mode.mode = R92C_PWRMODE_CAM;
319 mode.smart_ps = R92C_PWRMODE_SMARTPS_NULLDATA;
320 mode.bcn_pass = 1; /* XXX */
321 error = r92c_fw_cmd(sc, R92C_CMD_SET_PWRMODE, &mode, sizeof(mode));
322 if (error != 0) {
323 device_printf(sc->sc_dev,
324 "%s: CMD_SET_PWRMODE was not sent, error %d\n",
325 __func__, error);
326 }
327
328 return (error);
329 }
330
331 void
r92c_set_rssi(struct rtwn_softc * sc)332 r92c_set_rssi(struct rtwn_softc *sc)
333 {
334 struct ieee80211_node *ni;
335 struct rtwn_node *rn;
336 struct r92c_fw_cmd_rssi cmd;
337 int i;
338
339 cmd.reserved = 0;
340
341 RTWN_NT_LOCK(sc);
342 for (i = 0; i < sc->macid_limit; i++) {
343 /* XXX optimize? */
344 ni = sc->node_list[i];
345 if (ni == NULL)
346 continue;
347
348 rn = RTWN_NODE(ni);
349 cmd.macid = i;
350 cmd.pwdb = rn->avg_pwdb;
351 RTWN_DPRINTF(sc, RTWN_DEBUG_RSSI,
352 "%s: sending RSSI command (macid %d, rssi %d)\n",
353 __func__, i, rn->avg_pwdb);
354
355 RTWN_NT_UNLOCK(sc);
356 r92c_fw_cmd(sc, R92C_CMD_RSSI_SETTING, &cmd, sizeof(cmd));
357 RTWN_NT_LOCK(sc);
358 }
359 RTWN_NT_UNLOCK(sc);
360 }
361
362 static void
r92c_ratectl_tx_complete(struct rtwn_softc * sc,uint8_t * buf,int len)363 r92c_ratectl_tx_complete(struct rtwn_softc *sc, uint8_t *buf, int len)
364 {
365 struct ieee80211_ratectl_tx_status txs;
366 struct r92c_c2h_tx_rpt *rpt;
367 struct ieee80211_node *ni;
368 uint8_t macid;
369 int ntries;
370
371 if (sc->sc_ratectl != RTWN_RATECTL_NET80211) {
372 /* shouldn't happen */
373 device_printf(sc->sc_dev, "%s called while ratectl = %d!\n",
374 __func__, sc->sc_ratectl);
375 return;
376 }
377
378 rpt = (struct r92c_c2h_tx_rpt *)buf;
379 if (len != sizeof(*rpt)) {
380 device_printf(sc->sc_dev,
381 "%s: wrong report size (%d, must be %zu)\n",
382 __func__, len, sizeof(*rpt));
383 return;
384 }
385
386 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
387 "%s: ccx report dump: 0: %02X, 1: %02X, queue time: "
388 "low %02X, high %02X, 4: %02X, 5: %02X, 6: %02X, 7: %02X\n",
389 __func__, rpt->rptb0, rpt->rptb1, rpt->queue_time_low,
390 rpt->queue_time_high, rpt->rptb4, rpt->rptb5, rpt->rptb6,
391 rpt->rptb7);
392
393 macid = MS(rpt->rptb5, R92C_RPTB5_MACID);
394 if (macid > sc->macid_limit) {
395 device_printf(sc->sc_dev,
396 "macid %u is too big; increase MACID_MAX limit\n",
397 macid);
398 return;
399 }
400
401 ntries = MS(rpt->rptb0, R92C_RPTB0_RETRY_CNT);
402
403 RTWN_NT_LOCK(sc);
404 ni = sc->node_list[macid];
405 if (ni != NULL) {
406 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: frame for macid %u was"
407 "%s sent (%d retries)\n", __func__, macid,
408 (rpt->rptb7 & R92C_RPTB7_PKT_OK) ? "" : " not",
409 ntries);
410
411 txs.flags = IEEE80211_RATECTL_STATUS_LONG_RETRY;
412 txs.long_retries = ntries;
413 if (rpt->rptb7 & R92C_RPTB7_PKT_OK)
414 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
415 else if (rpt->rptb6 & R92C_RPTB6_RETRY_OVER)
416 txs.status = IEEE80211_RATECTL_TX_FAIL_LONG; /* XXX */
417 else if (rpt->rptb6 & R92C_RPTB6_LIFE_EXPIRE)
418 txs.status = IEEE80211_RATECTL_TX_FAIL_EXPIRED;
419 else
420 txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
421 ieee80211_ratectl_tx_complete(ni, &txs);
422 } else {
423 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: macid %u, ni is NULL\n",
424 __func__, macid);
425 }
426 RTWN_NT_UNLOCK(sc);
427
428 #ifdef IEEE80211_SUPPORT_SUPERG
429 if (sc->sc_tx_n_active > 0 && --sc->sc_tx_n_active <= 1)
430 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
431 #endif
432 }
433
434 static void
r92c_handle_c2h_task(struct rtwn_softc * sc,union sec_param * data)435 r92c_handle_c2h_task(struct rtwn_softc *sc, union sec_param *data)
436 {
437 const uint16_t off = R92C_C2H_EVT_MSG + sizeof(struct r92c_c2h_evt);
438 struct r92c_softc *rs = sc->sc_priv;
439 uint16_t buf[R92C_C2H_MSG_MAX_LEN / 2 + 1];
440 uint8_t id, len, status;
441 int i;
442
443 /* Do not reschedule the task if device is not running. */
444 if (!(sc->sc_flags & RTWN_RUNNING))
445 return;
446
447 /* Read current status. */
448 status = rtwn_read_1(sc, R92C_C2H_EVT_CLEAR);
449 if (status == R92C_C2H_EVT_HOST_CLOSE)
450 goto end; /* nothing to do */
451 else if (status == R92C_C2H_EVT_FW_CLOSE) {
452 len = rtwn_read_1(sc, R92C_C2H_EVT_MSG);
453 id = MS(len, R92C_C2H_EVTB0_ID);
454 len = MS(len, R92C_C2H_EVTB0_LEN);
455
456 memset(buf, 0, sizeof(buf));
457 /* Try to optimize event reads. */
458 for (i = 0; i < len; i += 2)
459 buf[i / 2] = rtwn_read_2(sc, off + i);
460 KASSERT(i < sizeof(buf), ("%s: buffer overrun (%d >= %zu)!",
461 __func__, i, sizeof(buf)));
462
463 switch (id) {
464 case R92C_C2H_EVT_TX_REPORT:
465 r92c_ratectl_tx_complete(sc, (uint8_t *)buf, len);
466 break;
467 default:
468 device_printf(sc->sc_dev,
469 "%s: C2H report %u (len %u) was not handled\n",
470 __func__, id, len);
471 break;
472 }
473 }
474
475 /* Prepare for next event. */
476 rtwn_write_1(sc, R92C_C2H_EVT_CLEAR, R92C_C2H_EVT_HOST_CLOSE);
477
478 end:
479 /* Adjust timeout for next call. */
480 if (rs->rs_c2h_pending != 0) {
481 rs->rs_c2h_pending = 0;
482 rs->rs_c2h_paused = 0;
483 } else
484 rs->rs_c2h_paused++;
485
486 if (rs->rs_c2h_paused > R92C_TX_PAUSED_THRESHOLD)
487 rs->rs_c2h_timeout = hz;
488 else
489 rs->rs_c2h_timeout = MAX(hz / 100, 1);
490
491 /* Reschedule the task. */
492 callout_reset(&rs->rs_c2h_report, rs->rs_c2h_timeout,
493 r92c_handle_c2h_report, sc);
494 }
495
496 void
r92c_handle_c2h_report(void * arg)497 r92c_handle_c2h_report(void *arg)
498 {
499 struct rtwn_softc *sc = arg;
500
501 rtwn_cmd_sleepable(sc, NULL, 0, r92c_handle_c2h_task);
502 }
503
504 #endif /* RTWN_WITHOUT_UCODE */
505