aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/drivers/net/wireless/morsemicro/mm81x/rc.c
blob: 28dd293df966c560cd817bc9d15d14d428b5890a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2017-2026 Morse Micro
 */
#include <linux/slab.h>
#include <linux/timer.h>
#include "core.h"
#include "mac.h"
#include "bus.h"
#include "rc.h"

#define MM81X_RC_BW_TO_MMRC_BW(X)    \
	(((X) == 1) ? MMRC_BW_1MHZ : \
	 ((X) == 2) ? MMRC_BW_2MHZ : \
	 ((X) == 4) ? MMRC_BW_4MHZ : \
	 ((X) == 8) ? MMRC_BW_8MHZ : \
		      MMRC_BW_2MHZ)

static void mm81x_rc_work(struct work_struct *work)
{
	struct mm81x_rc *mrc = container_of(work, struct mm81x_rc, work);
	struct list_head *pos;

	spin_lock_bh(&mrc->lock);

	list_for_each(pos, &mrc->stas) {
		struct mm81x_rc_sta *mrc_sta =
			container_of(pos, struct mm81x_rc_sta, list);
		unsigned long now = jiffies;

		mrc_sta->last_update = now;

		mmrc_update(mrc_sta->tb);
	}

	spin_unlock_bh(&mrc->lock);

	mod_timer(&mrc->timer, jiffies + msecs_to_jiffies(100));
}

static void mm81x_rc_timer(struct timer_list *t)
{
	struct mm81x_rc *mrc = timer_container_of(mrc, t, timer);
	struct mm81x *mors = mrc->mors;

	queue_work(mors->net_wq, &mors->mrc.work);
}

void mm81x_rc_init(struct mm81x *mors)
{
	INIT_LIST_HEAD(&mors->mrc.stas);
	spin_lock_init(&mors->mrc.lock);

	INIT_WORK(&mors->mrc.work, mm81x_rc_work);
	timer_setup(&mors->mrc.timer, mm81x_rc_timer, 0);

	mors->mrc.mors = mors;
	mod_timer(&mors->mrc.timer, jiffies + msecs_to_jiffies(100));
}

void mm81x_rc_deinit(struct mm81x *mors)
{
	timer_shutdown_sync(&mors->mrc.timer);
	cancel_work_sync(&mors->mrc.work);
}

static void mm81x_rc_sta_config_guard_per_bw(struct ieee80211_sta *sta,
					     struct mmrc_sta_capabilities *caps)
{
	caps->guard = BIT(MMRC_GUARD_LONG);

	if (caps->bandwidth & BIT(MMRC_BW_1MHZ)) {
		caps->sgi_per_bw |= SGI_PER_BW(MMRC_BW_1MHZ);
		caps->guard |= BIT(MMRC_GUARD_SHORT);
	}

	if (caps->bandwidth & BIT(MMRC_BW_2MHZ)) {
		caps->sgi_per_bw |= SGI_PER_BW(MMRC_BW_2MHZ);
		caps->guard |= BIT(MMRC_GUARD_SHORT);
	}

	if (caps->bandwidth & BIT(MMRC_BW_4MHZ)) {
		caps->sgi_per_bw |= SGI_PER_BW(MMRC_BW_4MHZ);
		caps->guard |= BIT(MMRC_GUARD_SHORT);
	}

	if (caps->bandwidth & BIT(MMRC_BW_8MHZ)) {
		caps->sgi_per_bw |= SGI_PER_BW(MMRC_BW_8MHZ);
		caps->guard |= BIT(MMRC_GUARD_SHORT);
	}
}

static void mm81x_rc_sta_add_s1g_sta_caps(struct mm81x *mors,
					  struct mmrc_sta_capabilities *caps,
					  struct ieee80211_sta_s1g_cap *s1g_cap)
{
	int nss_idx = 0;
	u8 rx_mcs = s1g_cap->nss_mcs[0] & 0x3; /* 1SS */
	u8 tx_mcs = (s1g_cap->nss_mcs[2] >> 1) & 0x3; /* 1SS */
	u8 mcs = min(rx_mcs, tx_mcs);

	switch (mcs) {
	case IEEE80211_VHT_MCS_SUPPORT_0_9: /* VHT 9 ->  S1G 9 */
		caps->rates |= BIT(MMRC_MCS9) | BIT(MMRC_MCS8);
		fallthrough;
	case IEEE80211_VHT_MCS_SUPPORT_0_8: /* VHT 8 -> S1G 7 */
		caps->rates |= BIT(MMRC_MCS7) | BIT(MMRC_MCS6) |
			       BIT(MMRC_MCS5) | BIT(MMRC_MCS4) | BIT(MMRC_MCS3);
		fallthrough;
	case IEEE80211_VHT_MCS_SUPPORT_0_7: /* VHT 7 -> S1G 2 */
		caps->rates |= BIT(MMRC_MCS2) | BIT(MMRC_MCS1) |
			       BIT(MMRC_MCS0) | BIT(MMRC_MCS10);
		caps->spatial_streams |= (BIT(nss_idx) & 0x0F);
		break;

	default:
		dev_warn(mors->dev, "Invalid MCS encoding 0x%02x for stream %d",
			 mcs, nss_idx);
	}
}

int mm81x_rc_sta_add(struct mm81x *mors, struct ieee80211_vif *vif,
		     struct ieee80211_sta *sta)
{
	struct ieee80211_sta_s1g_cap *s1g_cap = &sta->deflink.s1g_cap;
	struct mm81x_sta *msta = (struct mm81x_sta *)sta->drv_priv;
	struct mmrc_sta_capabilities caps;
	int oper_bw_mhz = cfg80211_chandef_get_width(&mors->chandef);
	size_t table_mem_size;
	struct mmrc_table *tb;

	memset(&caps, 0, sizeof(caps));

	mm81x_rc_sta_add_s1g_sta_caps(mors, &caps, s1g_cap);

	/* Configure STA for support up to 8MHZ */
	while (oper_bw_mhz > 0) {
		caps.bandwidth |= BIT(MM81X_RC_BW_TO_MMRC_BW(oper_bw_mhz));
		oper_bw_mhz >>= 1;
	}

	/* Configure STA for short and long guard */
	mm81x_rc_sta_config_guard_per_bw(sta, &caps);

	/* Set max rates */
	if (mors->hw->max_rates > 0 &&
	    mors->hw->max_rates < IEEE80211_TX_MAX_RATES)
		caps.max_rates = mors->hw->max_rates;
	else
		caps.max_rates = IEEE80211_TX_MAX_RATES;

	/* Set max reties */
	if (mors->hw->max_rate_tries >= MMRC_MIN_CHAIN_ATTEMPTS &&
	    mors->hw->max_rate_tries < MMRC_MAX_CHAIN_ATTEMPTS)
		caps.max_retries = mors->hw->max_rate_tries;
	else
		caps.max_retries = MMRC_MAX_CHAIN_ATTEMPTS;

	WARN_ON(msta->rc.tb);
	table_mem_size = mmrc_memory_required_for_caps(&caps);
	tb = kzalloc(table_mem_size, GFP_KERNEL);
	if (!tb)
		return -ENOMEM;

	/* Initialise the STA rate control table */
	mmrc_sta_init(tb, &caps, msta->avg_rssi);

	spin_lock_bh(&mors->mrc.lock);
	kfree(msta->rc.tb);
	msta->rc.tb = tb;
	list_add(&msta->rc.list, &mors->mrc.stas);
	msta->rc.last_update = jiffies;
	spin_unlock_bh(&mors->mrc.lock);

	return 0;
}

void mm81x_rc_sta_remove(struct mm81x *mors, struct ieee80211_sta *sta)
{
	struct mm81x_sta *msta = (struct mm81x_sta *)sta->drv_priv;

	spin_lock_bh(&mors->mrc.lock);
	if (msta->rc.tb) {
		list_del_init(&msta->rc.list);
		kfree(msta->rc.tb);
		msta->rc.tb = NULL;
	}
	spin_unlock_bh(&mors->mrc.lock);
}

static void mm81x_rc_sta_fill_basic_rates(struct mm81x_skb_tx_info *tx_info,
					  struct ieee80211_tx_info *info,
					  int tx_bw)
{
	int i;
	enum dot11_bandwidth bw_idx = mm81x_ratecode_bw_mhz_to_bw_index(tx_bw);
	enum mm81x_rate_preamble pream = MM81X_RATE_PREAMBLE_S1G_SHORT;

	mm81x_ratecode_mcs_index_set(&tx_info->rates[0].mm81x_ratecode, 0);
	mm81x_ratecode_nss_index_set(&tx_info->rates[0].mm81x_ratecode,
				     NSS_TO_NSS_IDX(1));
	mm81x_ratecode_bw_index_set(&tx_info->rates[0].mm81x_ratecode, bw_idx);
	if (bw_idx == DOT11_BANDWIDTH_1MHZ)
		pream = MM81X_RATE_PREAMBLE_S1G_1M;
	mm81x_ratecode_preamble_set(&tx_info->rates[0].mm81x_ratecode, pream);
	tx_info->rates[0].count = 4;

	for (i = 1; i < IEEE80211_TX_MAX_RATES; i++)
		tx_info->rates[i].count = 0;

	info->control.rates[0].idx = 0;
	info->control.rates[0].count = tx_info->rates[0].count;
	info->control.rates[0].flags = 0;
	info->control.rates[1].idx = -1;
}

static int mm81x_rc_sta_get_rates(struct mm81x *mors, struct mm81x_sta *msta,
				  struct mmrc_rate_table *rates, size_t size)
{
	int ret = -ENOENT;
	struct list_head *pos;

	spin_lock_bh(&mors->mrc.lock);
	list_for_each(pos, &mors->mrc.stas) {
		struct mm81x_rc_sta *mrc_sta =
			list_entry(pos, struct mm81x_rc_sta, list);

		if (&msta->rc == mrc_sta) {
			ret = 0;
			mmrc_get_rates(msta->rc.tb, rates, size);
			break;
		}
	}
	spin_unlock_bh(&mors->mrc.lock);

	return ret;
}

static bool mm81x_rc_use_basic_rates(struct ieee80211_sta *sta,
				     struct sk_buff *skb,
				     struct ieee80211_hdr *hdr)
{
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);

	if (!sta)
		return true;

	if (ieee80211_is_qos_nullfunc(hdr->frame_control) ||
	    ieee80211_is_nullfunc(hdr->frame_control))
		return true;

	if (!ieee80211_is_data_qos(hdr->frame_control))
		return true;

	/* Use basic rates for EAPOL exchanges or when instructed */
	if (unlikely((skb->protocol == cpu_to_be16(ETH_P_PAE) ||
		      info->flags & IEEE80211_TX_CTL_USE_MINRATE)))
		return true;

	return false;
}

void mm81x_rc_sta_fill_tx_rates(struct mm81x *mors,
				struct mm81x_skb_tx_info *tx_info,
				struct sk_buff *skb, struct ieee80211_sta *sta,
				int tx_bw, bool rts_allowed)
{
	int ret, i;
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
	struct mm81x_sta *msta;
	struct mmrc_rate_table rates;
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);

	BUILD_BUG_ON((MMRC_BW_1MHZ != (enum mmrc_bw)DOT11_BANDWIDTH_1MHZ ||
		      MMRC_BW_2MHZ != (enum mmrc_bw)DOT11_BANDWIDTH_2MHZ ||
		      MMRC_BW_4MHZ != (enum mmrc_bw)DOT11_BANDWIDTH_4MHZ ||
		      MMRC_BW_16MHZ != (enum mmrc_bw)DOT11_BANDWIDTH_16MHZ));

	memset(&info->control.rates, 0, sizeof(info->control.rates));
	memset(&info->status.rates, 0, sizeof(info->status.rates));
	mm81x_rc_sta_fill_basic_rates(tx_info, info, tx_bw);

	/* Use basic rates for non data packets */
	if (mm81x_rc_use_basic_rates(sta, skb, hdr))
		return;

	msta = (struct mm81x_sta *)sta->drv_priv;
	if (!msta)
		return;

	ret = mm81x_rc_sta_get_rates(mors, msta, &rates, skb->len);
	if (ret != 0)
		return;

	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
		info->control.rates[i].flags = 0;
		if (rates.rates[i].rate != MMRC_MCS_UNUSED) {
			u8 mcs = rates.rates[i].rate;
			u8 nss_index = rates.rates[i].ss;
			enum dot11_bandwidth bw_idx =
				(enum dot11_bandwidth)rates.rates[i].bw;
			enum mm81x_rate_preamble pream =
				MM81X_RATE_PREAMBLE_S1G_SHORT;

			mm81x_ratecode_bw_index_set(
				&tx_info->rates[i].mm81x_ratecode, bw_idx);
			mm81x_ratecode_mcs_index_set(
				&tx_info->rates[i].mm81x_ratecode, mcs);
			mm81x_ratecode_nss_index_set(
				&tx_info->rates[i].mm81x_ratecode, nss_index);
			if (bw_idx == DOT11_BANDWIDTH_1MHZ)
				pream = MM81X_RATE_PREAMBLE_S1G_1M;
			mm81x_ratecode_preamble_set(
				&tx_info->rates[i].mm81x_ratecode, pream);
			tx_info->rates[i].count = rates.rates[i].attempts;

			if (rts_allowed &&
			    (rates.rates[i].flags & BIT(MMRC_FLAGS_CTS_RTS))) {
				mm81x_ratecode_enable_rts(
					&tx_info->rates[i].mm81x_ratecode);
				info->control.rates[i].flags |=
					IEEE80211_TX_RC_USE_RTS_CTS;
			}

			if (rates.rates[i].guard == MMRC_GUARD_SHORT) {
				mm81x_ratecode_enable_sgi(
					&tx_info->rates[i].mm81x_ratecode);
				info->control.rates[i].flags |=
					IEEE80211_TX_RC_SHORT_GI;
			}

			/* Update skb tx_info */
			info->control.rates[i].idx = rates.rates[i].rate;
			info->control.rates[i].count = rates.rates[i].attempts;
		} else {
			info->control.rates[i].idx = -1;
			info->control.rates[i].count = 0;
			tx_info->rates[i].count = 0;
		}
	}
}

static void mm81x_rc_sta_set_rates(struct mm81x *mors, struct mm81x_sta *msta,
				   struct mmrc_rate_table *rates, int attempts,
				   bool was_aggregated)
{
	struct list_head *pos;

	spin_lock_bh(&mors->mrc.lock);
	list_for_each(pos, &mors->mrc.stas) {
		struct mm81x_rc_sta *mrc_sta =
			list_entry(pos, struct mm81x_rc_sta, list);

		if (&msta->rc == mrc_sta) {
			mmrc_feedback(msta->rc.tb, rates, attempts,
				      was_aggregated);
			break;
		}
	}
	spin_unlock_bh(&mors->mrc.lock);
}

void mm81x_rc_sta_feedback_rates(struct mm81x *mors, struct sk_buff *skb,
				 struct ieee80211_sta *sta,
				 struct mm81x_skb_tx_status *tx_sts,
				 int attempts)
{
	int i;
	u32 tx_airtime = 0;
	struct mmrc_rate_table rates;
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
	struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
	struct ieee80211_tx_rate *r = &txi->status.rates[0];
	int count = min_t(int, MM81X_SKB_MAX_RATES, IEEE80211_TX_MAX_RATES);
	struct mm81x_sta *msta = msta = (struct mm81x_sta *)sta->drv_priv;

	/* Don't update rate info if basic rates were used */
	if (mm81x_rc_use_basic_rates(sta, skb, hdr))
		goto exit;

	if (attempts <= 0)
		/* Did we really send the packet? */
		goto exit;

	for (i = 0; i < count; i++) {
		rates.rates[i].rate = mm81x_ratecode_mcs_index_get(
			tx_sts->rates[i].mm81x_ratecode);
		rates.rates[i].ss = mm81x_ratecode_nss_index_get(
			tx_sts->rates[i].mm81x_ratecode);
		rates.rates[i].guard =
			mm81x_ratecode_sgi_get(tx_sts->rates[i].mm81x_ratecode);
		rates.rates[i].bw = mm81x_ratecode_bw_index_get(
			tx_sts->rates[i].mm81x_ratecode);
		rates.rates[i].flags =
			mm81x_ratecode_rts_get(tx_sts->rates[i].mm81x_ratecode);
		rates.rates[i].attempts = tx_sts->rates[i].count;

		tx_airtime +=
			mmrc_calculate_rate_tx_time(&rates.rates[i], skb->len);
	}

	if (msta) {
		/*
		 * Save the rate information. This will be used to update
		 * station's tx rate stats
		 */
		msta->last_sta_tx_rate.bw = rates.rates[0].bw;
		msta->last_sta_tx_rate.rate = rates.rates[0].rate;
		msta->last_sta_tx_rate.ss = rates.rates[0].ss;
		msta->last_sta_tx_rate.guard = rates.rates[0].guard;
	}

	mm81x_rc_sta_set_rates(mors, msta, &rates, attempts,
			       !!(le32_to_cpu(tx_sts->flags) &
				  MM81X_TX_STATUS_WAS_AGGREGATED));

	ieee80211_sta_register_airtime(sta, tx_sts->tid, tx_airtime, 0);

exit:
	ieee80211_tx_info_clear_status(txi);

	if (!(le32_to_cpu(tx_sts->flags) & MM81X_TX_STATUS_FLAGS_NO_ACK) &&
	    !(txi->flags & IEEE80211_TX_CTL_NO_ACK))
		txi->flags |= IEEE80211_TX_STAT_ACK;

	if (le32_to_cpu(tx_sts->flags) & MM81X_TX_STATUS_FLAGS_PS_FILTERED) {
		txi->flags |= IEEE80211_TX_STAT_TX_FILTERED;

		/*
		 * Clear TX CTL AMPDU flag so that this frame gets rescheduled
		 * in ieee80211_handle_filtered_frame(). This flag will get set
		 * again by mac80211's tx path on rescheduling.
		 */
		txi->flags &= ~IEEE80211_TX_CTL_AMPDU;
		if (msta) {
			if (!msta->tx_ps_filter_en)
				dev_dbg(mors->dev, "TX ps filter set sta[%pM]",
					msta->addr);
			msta->tx_ps_filter_en = true;
		}
	}

	for (i = 0; i < count; i++) {
		if (tx_sts->rates[i].count > 0) {
			r[i].count = tx_sts->rates[i].count;
			r[i].flags |= IEEE80211_TX_RC_MCS;
		} else {
			r[i].idx = -1;
		}
	}

	/* single packet per A-MPDU (for now) */
	if (txi->flags & IEEE80211_TX_CTL_AMPDU) {
		txi->flags |= IEEE80211_TX_STAT_AMPDU;
		txi->status.ampdu_len = 1;
		txi->status.ampdu_ack_len =
			txi->flags & IEEE80211_TX_STAT_ACK ? 1 : 0;
	}

	/*
	 * Inform mac80211 that the SP (elicited by a PS-Poll or u-APSD) is
	 * over
	 */
	if (sta && (txi->flags & IEEE80211_TX_STATUS_EOSP)) {
		txi->flags &= ~IEEE80211_TX_STATUS_EOSP;
		ieee80211_sta_eosp(sta);
	}
}

void mm81x_rc_sta_state_check(struct mm81x *mors, struct ieee80211_vif *vif,
			      struct ieee80211_sta *sta,
			      enum ieee80211_sta_state old_state,
			      enum ieee80211_sta_state new_state)
{
	struct mm81x_sta *msta = (struct mm81x_sta *)sta->drv_priv;

	/* Add to Morse RC STA list */
	if (old_state < new_state && new_state == IEEE80211_STA_ASSOC) {
		/* Newly associated, add to RC */
		mm81x_rc_sta_add(mors, vif, sta);
	} else if (old_state > new_state && (old_state == IEEE80211_STA_ASSOC ||
					     old_state == IEEE80211_STA_AUTH)) {
		/* Lost or failed association; remove from list */
		mm81x_rc_sta_remove(mors, sta);
	} else if (old_state < new_state && old_state == IEEE80211_STA_NONE &&
		   msta->rc.list.prev) {
		/*
		 * Special case for driver warning issue causing a sta to be
		 * left on the list
		 */
		dev_dbg(mors->dev, "Remove stale sta from rc list");
		mm81x_rc_sta_remove(mors, sta);
	}
}