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,bool shortgi)166 r92c_send_ra_cmd(struct rtwn_softc *sc, int macid, uint32_t rates,
167 int maxrate, bool shortgi)
168 {
169 struct r92c_fw_cmd_macid_cfg cmd;
170 uint8_t mode;
171 int error = 0;
172
173 /* Set rates mask for unicast frames. */
174 if (RTWN_RATE_IS_HT(maxrate))
175 mode = R92C_RAID_11BGN;
176 else if (RTWN_RATE_IS_OFDM(maxrate))
177 mode = R92C_RAID_11BG;
178 else
179 mode = R92C_RAID_11B;
180 cmd.macid = macid | R92C_CMD_MACID_VALID;
181 if (shortgi)
182 cmd.macid |= R92C_CMD_MACID_SGI;
183 cmd.mask = htole32(mode << 28 | rates);
184 error = r92c_fw_cmd(sc, R92C_CMD_MACID_CONFIG, &cmd, sizeof(cmd));
185 if (error != 0) {
186 device_printf(sc->sc_dev,
187 "%s: could not set RA mask for %d station\n",
188 __func__, macid);
189 return (error);
190 }
191
192 return (0);
193 }
194 #endif
195
196 static void
r92c_init_ra(struct rtwn_softc * sc,int macid)197 r92c_init_ra(struct rtwn_softc *sc, int macid)
198 {
199 struct ieee80211_htrateset *rs_ht;
200 struct ieee80211_node *ni;
201 uint32_t rates, htrates;
202 int maxrate;
203
204 RTWN_NT_LOCK(sc);
205 if (sc->node_list[macid] == NULL) {
206 RTWN_DPRINTF(sc, RTWN_DEBUG_RA, "%s: macid %d, ni is NULL\n",
207 __func__, macid);
208 RTWN_NT_UNLOCK(sc);
209 return;
210 }
211
212 ni = ieee80211_ref_node(sc->node_list[macid]);
213 if (ni->ni_flags & IEEE80211_NODE_HT)
214 rs_ht = &ni->ni_htrates;
215 else
216 rs_ht = NULL;
217 /*
218 * Note: this pushes the rate bitmap and maxrate into the
219 * firmware; and for this chipset 2-stream 11n support is enough.
220 */
221 rtwn_get_rates(sc, &ni->ni_rates, rs_ht, &rates, &htrates, &maxrate, 0);
222 RTWN_NT_UNLOCK(sc);
223
224 #ifndef RTWN_WITHOUT_UCODE
225 if (sc->sc_ratectl == RTWN_RATECTL_FW) {
226 uint32_t fw_rates;
227 bool shortgi;
228 /* Add HT rates after normal rates; limit to MCS0..15 */
229 fw_rates = rates |
230 ((htrates & 0xffff) << RTWN_RIDX_HT_MCS_SHIFT);
231 /* Re-calculate short-gi based on op mode */
232 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan))
233 shortgi = ieee80211_ht_check_tx_shortgi_40(ni);
234 else if (IEEE80211_IS_CHAN_HT20(ni->ni_chan))
235 shortgi = ieee80211_ht_check_tx_shortgi_20(ni);
236 else
237 shortgi = false;
238 r92c_send_ra_cmd(sc, macid, fw_rates, maxrate, shortgi);
239 }
240 #endif
241
242 /*
243 * There's no need to set this if firmware rate control is
244 * enabled - the firmware will be controlling this per MACID.
245 */
246 if (sc->sc_ratectl != RTWN_RATECTL_FW)
247 rtwn_write_1(sc, R92C_INIDATA_RATE_SEL(macid), maxrate);
248
249 ieee80211_free_node(ni);
250 }
251
252 void
r92c_joinbss_rpt(struct rtwn_softc * sc,int macid)253 r92c_joinbss_rpt(struct rtwn_softc *sc, int macid)
254 {
255 #ifndef RTWN_WITHOUT_UCODE
256 struct r92c_softc *rs = sc->sc_priv;
257 struct ieee80211vap *vap;
258 struct r92c_fw_cmd_joinbss_rpt cmd;
259
260 if (sc->vaps[0] == NULL) /* XXX fix */
261 goto end;
262
263 vap = &sc->vaps[0]->vap;
264 if ((vap->iv_state == IEEE80211_S_RUN) ^
265 !(rs->rs_flags & R92C_FLAG_ASSOCIATED))
266 goto end;
267
268 if (rs->rs_flags & R92C_FLAG_ASSOCIATED) {
269 cmd.mstatus = R92C_MSTATUS_DISASSOC;
270 rs->rs_flags &= ~R92C_FLAG_ASSOCIATED;
271 } else {
272 cmd.mstatus = R92C_MSTATUS_ASSOC;
273 rs->rs_flags |= R92C_FLAG_ASSOCIATED;
274 }
275
276 if (r92c_fw_cmd(sc, R92C_CMD_JOINBSS_RPT, &cmd, sizeof(cmd)) != 0) {
277 device_printf(sc->sc_dev, "%s: cannot change media status!\n",
278 __func__);
279 }
280
281 end:
282 #endif
283
284 if (macid & RTWN_MACID_VALID)
285 r92c_init_ra(sc, macid & ~RTWN_MACID_VALID);
286 }
287
288 #ifndef RTWN_WITHOUT_UCODE
289 int
r92c_set_rsvd_page(struct rtwn_softc * sc,int probe_resp,int null,int qos_null)290 r92c_set_rsvd_page(struct rtwn_softc *sc, int probe_resp, int null,
291 int qos_null)
292 {
293 struct r92c_fw_cmd_rsvdpage rsvd;
294
295 rsvd.probe_resp = probe_resp;
296 rsvd.ps_poll = 0;
297 rsvd.null_data = null;
298
299 return (r92c_fw_cmd(sc, R92C_CMD_RSVD_PAGE, &rsvd, sizeof(rsvd)));
300 }
301
302 int
r92c_set_pwrmode(struct rtwn_softc * sc,struct ieee80211vap * vap,int off)303 r92c_set_pwrmode(struct rtwn_softc *sc, struct ieee80211vap *vap,
304 int off)
305 {
306 struct r92c_fw_cmd_pwrmode mode;
307 int error;
308
309 /* XXX dm_RF_saving */
310
311 if (off && vap->iv_state == IEEE80211_S_RUN &&
312 (vap->iv_flags & IEEE80211_F_PMGTON))
313 mode.mode = R92C_PWRMODE_MIN;
314 else
315 mode.mode = R92C_PWRMODE_CAM;
316 mode.smart_ps = R92C_PWRMODE_SMARTPS_NULLDATA;
317 mode.bcn_pass = 1; /* XXX */
318 error = r92c_fw_cmd(sc, R92C_CMD_SET_PWRMODE, &mode, sizeof(mode));
319 if (error != 0) {
320 device_printf(sc->sc_dev,
321 "%s: CMD_SET_PWRMODE was not sent, error %d\n",
322 __func__, error);
323 }
324
325 return (error);
326 }
327
328 void
r92c_set_rssi(struct rtwn_softc * sc)329 r92c_set_rssi(struct rtwn_softc *sc)
330 {
331 struct ieee80211_node *ni;
332 struct rtwn_node *rn;
333 struct r92c_fw_cmd_rssi cmd;
334 int i;
335
336 cmd.reserved = 0;
337
338 RTWN_NT_LOCK(sc);
339 for (i = 0; i < sc->macid_limit; i++) {
340 /* XXX optimize? */
341 ni = sc->node_list[i];
342 if (ni == NULL)
343 continue;
344
345 rn = RTWN_NODE(ni);
346 cmd.macid = i;
347 cmd.pwdb = rn->avg_pwdb;
348 RTWN_DPRINTF(sc, RTWN_DEBUG_RSSI,
349 "%s: sending RSSI command (macid %d, rssi %d)\n",
350 __func__, i, rn->avg_pwdb);
351
352 RTWN_NT_UNLOCK(sc);
353 r92c_fw_cmd(sc, R92C_CMD_RSSI_SETTING, &cmd, sizeof(cmd));
354 RTWN_NT_LOCK(sc);
355 }
356 RTWN_NT_UNLOCK(sc);
357 }
358
359 static void
r92c_ratectl_tx_complete(struct rtwn_softc * sc,uint8_t * buf,int len)360 r92c_ratectl_tx_complete(struct rtwn_softc *sc, uint8_t *buf, int len)
361 {
362 struct ieee80211_ratectl_tx_status txs;
363 struct r92c_c2h_tx_rpt *rpt;
364 struct ieee80211_node *ni;
365 uint8_t macid;
366 int ntries;
367
368 if (sc->sc_ratectl != RTWN_RATECTL_NET80211) {
369 /* shouldn't happen */
370 device_printf(sc->sc_dev, "%s called while ratectl = %d!\n",
371 __func__, sc->sc_ratectl);
372 return;
373 }
374
375 rpt = (struct r92c_c2h_tx_rpt *)buf;
376 if (len != sizeof(*rpt)) {
377 device_printf(sc->sc_dev,
378 "%s: wrong report size (%d, must be %zu)\n",
379 __func__, len, sizeof(*rpt));
380 return;
381 }
382
383 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR,
384 "%s: ccx report dump: 0: %02X, 1: %02X, queue time: "
385 "low %02X, high %02X, 4: %02X, 5: %02X, 6: %02X, 7: %02X\n",
386 __func__, rpt->rptb0, rpt->rptb1, rpt->queue_time_low,
387 rpt->queue_time_high, rpt->rptb4, rpt->rptb5, rpt->rptb6,
388 rpt->rptb7);
389
390 macid = MS(rpt->rptb5, R92C_RPTB5_MACID);
391 if (macid > sc->macid_limit) {
392 device_printf(sc->sc_dev,
393 "macid %u is too big; increase MACID_MAX limit\n",
394 macid);
395 return;
396 }
397
398 ntries = MS(rpt->rptb0, R92C_RPTB0_RETRY_CNT);
399
400 RTWN_NT_LOCK(sc);
401 ni = sc->node_list[macid];
402 if (ni != NULL) {
403 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: frame for macid %u was"
404 "%s sent (%d retries)\n", __func__, macid,
405 (rpt->rptb7 & R92C_RPTB7_PKT_OK) ? "" : " not",
406 ntries);
407
408 txs.flags = IEEE80211_RATECTL_STATUS_LONG_RETRY;
409 txs.long_retries = ntries;
410 if (rpt->rptb7 & R92C_RPTB7_PKT_OK)
411 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
412 else if (rpt->rptb6 & R92C_RPTB6_RETRY_OVER)
413 txs.status = IEEE80211_RATECTL_TX_FAIL_LONG; /* XXX */
414 else if (rpt->rptb6 & R92C_RPTB6_LIFE_EXPIRE)
415 txs.status = IEEE80211_RATECTL_TX_FAIL_EXPIRED;
416 else
417 txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
418 ieee80211_ratectl_tx_complete(ni, &txs);
419 } else {
420 RTWN_DPRINTF(sc, RTWN_DEBUG_INTR, "%s: macid %u, ni is NULL\n",
421 __func__, macid);
422 }
423 RTWN_NT_UNLOCK(sc);
424
425 #ifdef IEEE80211_SUPPORT_SUPERG
426 if (sc->sc_tx_n_active > 0 && --sc->sc_tx_n_active <= 1)
427 rtwn_cmd_sleepable(sc, NULL, 0, rtwn_ff_flush_all);
428 #endif
429 }
430
431 static void
r92c_handle_c2h_task(struct rtwn_softc * sc,union sec_param * data)432 r92c_handle_c2h_task(struct rtwn_softc *sc, union sec_param *data)
433 {
434 const uint16_t off = R92C_C2H_EVT_MSG + sizeof(struct r92c_c2h_evt);
435 struct r92c_softc *rs = sc->sc_priv;
436 uint16_t buf[R92C_C2H_MSG_MAX_LEN / 2 + 1];
437 uint8_t id, len, status;
438 int i;
439
440 /* Do not reschedule the task if device is not running. */
441 if (!(sc->sc_flags & RTWN_RUNNING))
442 return;
443
444 /* Read current status. */
445 status = rtwn_read_1(sc, R92C_C2H_EVT_CLEAR);
446 if (status == R92C_C2H_EVT_HOST_CLOSE)
447 goto end; /* nothing to do */
448 else if (status == R92C_C2H_EVT_FW_CLOSE) {
449 len = rtwn_read_1(sc, R92C_C2H_EVT_MSG);
450 id = MS(len, R92C_C2H_EVTB0_ID);
451 len = MS(len, R92C_C2H_EVTB0_LEN);
452
453 memset(buf, 0, sizeof(buf));
454 /* Try to optimize event reads. */
455 for (i = 0; i < len; i += 2)
456 buf[i / 2] = rtwn_read_2(sc, off + i);
457 KASSERT(i < sizeof(buf), ("%s: buffer overrun (%d >= %zu)!",
458 __func__, i, sizeof(buf)));
459
460 switch (id) {
461 case R92C_C2H_EVT_TX_REPORT:
462 r92c_ratectl_tx_complete(sc, (uint8_t *)buf, len);
463 break;
464 default:
465 device_printf(sc->sc_dev,
466 "%s: C2H report %u (len %u) was not handled\n",
467 __func__, id, len);
468 break;
469 }
470 }
471
472 /* Prepare for next event. */
473 rtwn_write_1(sc, R92C_C2H_EVT_CLEAR, R92C_C2H_EVT_HOST_CLOSE);
474
475 end:
476 /* Adjust timeout for next call. */
477 if (rs->rs_c2h_pending != 0) {
478 rs->rs_c2h_pending = 0;
479 rs->rs_c2h_paused = 0;
480 } else
481 rs->rs_c2h_paused++;
482
483 if (rs->rs_c2h_paused > R92C_TX_PAUSED_THRESHOLD)
484 rs->rs_c2h_timeout = hz;
485 else
486 rs->rs_c2h_timeout = MAX(hz / 100, 1);
487
488 /* Reschedule the task. */
489 callout_reset(&rs->rs_c2h_report, rs->rs_c2h_timeout,
490 r92c_handle_c2h_report, sc);
491 }
492
493 void
r92c_handle_c2h_report(void * arg)494 r92c_handle_c2h_report(void *arg)
495 {
496 struct rtwn_softc *sc = arg;
497
498 rtwn_cmd_sleepable(sc, NULL, 0, r92c_handle_c2h_task);
499 }
500
501 #endif /* RTWN_WITHOUT_UCODE */
502