1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005, Devicescape Software, Inc.
4 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#ifndef IEEE80211_RATE_H
12#define IEEE80211_RATE_H
13
14#include <linux/netdevice.h>
15#include <linux/skbuff.h>
16#include <linux/types.h>
17#include <net/mac80211.h>
18#include "ieee80211_i.h"
19#include "sta_info.h"
20#include "driver-ops.h"
21
22struct rate_control_ref {
23	struct ieee80211_local *local;
24	const struct rate_control_ops *ops;
25	void *priv;
26};
27
28void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
29			   struct sta_info *sta,
30			   struct ieee80211_tx_rate_control *txrc);
31
32static inline void rate_control_tx_status(struct ieee80211_local *local,
33					  struct ieee80211_supported_band *sband,
34					  struct sta_info *sta,
35					  struct sk_buff *skb)
36{
37	struct rate_control_ref *ref = local->rate_ctrl;
38	struct ieee80211_sta *ista = &sta->sta;
39	void *priv_sta = sta->rate_ctrl_priv;
40	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
41
42	if (!ref || !test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
43		return;
44
45	if (ref->ops->tx_status)
46		ref->ops->tx_status(ref->priv, sband, ista, priv_sta, skb);
47	else
48		ref->ops->tx_status_noskb(ref->priv, sband, ista, priv_sta, info);
49}
50
51static inline void
52rate_control_tx_status_noskb(struct ieee80211_local *local,
53			     struct ieee80211_supported_band *sband,
54			     struct sta_info *sta,
55			     struct ieee80211_tx_info *info)
56{
57	struct rate_control_ref *ref = local->rate_ctrl;
58	struct ieee80211_sta *ista = &sta->sta;
59	void *priv_sta = sta->rate_ctrl_priv;
60
61	if (!ref || !test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
62		return;
63
64	if (WARN_ON_ONCE(!ref->ops->tx_status_noskb))
65		return;
66
67	ref->ops->tx_status_noskb(ref->priv, sband, ista, priv_sta, info);
68}
69
70static inline void rate_control_rate_init(struct sta_info *sta)
71{
72	struct ieee80211_local *local = sta->sdata->local;
73	struct rate_control_ref *ref = sta->rate_ctrl;
74	struct ieee80211_sta *ista = &sta->sta;
75	void *priv_sta = sta->rate_ctrl_priv;
76	struct ieee80211_supported_band *sband;
77	struct ieee80211_chanctx_conf *chanctx_conf;
78
79	ieee80211_sta_set_rx_nss(sta);
80
81	if (!ref)
82		return;
83
84	rcu_read_lock();
85
86	chanctx_conf = rcu_dereference(sta->sdata->vif.chanctx_conf);
87	if (WARN_ON(!chanctx_conf)) {
88		rcu_read_unlock();
89		return;
90	}
91
92	sband = local->hw.wiphy->bands[chanctx_conf->def.chan->band];
93
94	ref->ops->rate_init(ref->priv, sband, &chanctx_conf->def, ista,
95			    priv_sta);
96	rcu_read_unlock();
97	set_sta_flag(sta, WLAN_STA_RATE_CONTROL);
98}
99
100static inline void rate_control_rate_update(struct ieee80211_local *local,
101				    struct ieee80211_supported_band *sband,
102				    struct sta_info *sta, u32 changed)
103{
104	struct rate_control_ref *ref = local->rate_ctrl;
105	struct ieee80211_sta *ista = &sta->sta;
106	void *priv_sta = sta->rate_ctrl_priv;
107	struct ieee80211_chanctx_conf *chanctx_conf;
108
109	if (ref && ref->ops->rate_update) {
110		rcu_read_lock();
111
112		chanctx_conf = rcu_dereference(sta->sdata->vif.chanctx_conf);
113		if (WARN_ON(!chanctx_conf)) {
114			rcu_read_unlock();
115			return;
116		}
117
118		ref->ops->rate_update(ref->priv, sband, &chanctx_conf->def,
119				      ista, priv_sta, changed);
120		rcu_read_unlock();
121	}
122	drv_sta_rc_update(local, sta->sdata, &sta->sta, changed);
123}
124
125static inline void *rate_control_alloc_sta(struct rate_control_ref *ref,
126					   struct ieee80211_sta *sta,
127					   gfp_t gfp)
128{
129	return ref->ops->alloc_sta(ref->priv, sta, gfp);
130}
131
132static inline void rate_control_free_sta(struct sta_info *sta)
133{
134	struct rate_control_ref *ref = sta->rate_ctrl;
135	struct ieee80211_sta *ista = &sta->sta;
136	void *priv_sta = sta->rate_ctrl_priv;
137
138	ref->ops->free_sta(ref->priv, ista, priv_sta);
139}
140
141static inline void rate_control_add_sta_debugfs(struct sta_info *sta)
142{
143#ifdef CONFIG_MAC80211_DEBUGFS
144	struct rate_control_ref *ref = sta->rate_ctrl;
145	if (ref && sta->debugfs.dir && ref->ops->add_sta_debugfs)
146		ref->ops->add_sta_debugfs(ref->priv, sta->rate_ctrl_priv,
147					  sta->debugfs.dir);
148#endif
149}
150
151static inline void rate_control_remove_sta_debugfs(struct sta_info *sta)
152{
153#ifdef CONFIG_MAC80211_DEBUGFS
154	struct rate_control_ref *ref = sta->rate_ctrl;
155	if (ref && ref->ops->remove_sta_debugfs)
156		ref->ops->remove_sta_debugfs(ref->priv, sta->rate_ctrl_priv);
157#endif
158}
159
160/* Get a reference to the rate control algorithm. If `name' is NULL, get the
161 * first available algorithm. */
162int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
163				 const char *name);
164void rate_control_deinitialize(struct ieee80211_local *local);
165
166
167/* Rate control algorithms */
168#ifdef CONFIG_MAC80211_RC_MINSTREL
169int rc80211_minstrel_init(void);
170void rc80211_minstrel_exit(void);
171#else
172static inline int rc80211_minstrel_init(void)
173{
174	return 0;
175}
176static inline void rc80211_minstrel_exit(void)
177{
178}
179#endif
180
181#ifdef CONFIG_MAC80211_RC_MINSTREL_HT
182int rc80211_minstrel_ht_init(void);
183void rc80211_minstrel_ht_exit(void);
184#else
185static inline int rc80211_minstrel_ht_init(void)
186{
187	return 0;
188}
189static inline void rc80211_minstrel_ht_exit(void)
190{
191}
192#endif
193
194
195#endif /* IEEE80211_RATE_H */
196