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