mei: me: add cannon point device ids
[linux/fpc-iii.git] / drivers / net / wireless / rsi / rsi_91x_core.c
blobd0d2201830e8bbeaf8fd13cf2dd86d03d151f6fb
1 /**
2 * Copyright (c) 2014 Redpine Signals Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "rsi_mgmt.h"
18 #include "rsi_common.h"
19 #include "rsi_hal.h"
21 /**
22 * rsi_determine_min_weight_queue() - This function determines the queue with
23 * the min weight.
24 * @common: Pointer to the driver private structure.
26 * Return: q_num: Corresponding queue number.
28 static u8 rsi_determine_min_weight_queue(struct rsi_common *common)
30 struct wmm_qinfo *tx_qinfo = common->tx_qinfo;
31 u32 q_len = 0;
32 u8 ii = 0;
34 for (ii = 0; ii < NUM_EDCA_QUEUES; ii++) {
35 q_len = skb_queue_len(&common->tx_queue[ii]);
36 if ((tx_qinfo[ii].pkt_contended) && q_len) {
37 common->min_weight = tx_qinfo[ii].weight;
38 break;
41 return ii;
44 /**
45 * rsi_recalculate_weights() - This function recalculates the weights
46 * corresponding to each queue.
47 * @common: Pointer to the driver private structure.
49 * Return: recontend_queue bool variable
51 static bool rsi_recalculate_weights(struct rsi_common *common)
53 struct wmm_qinfo *tx_qinfo = common->tx_qinfo;
54 bool recontend_queue = false;
55 u8 ii = 0;
56 u32 q_len = 0;
58 for (ii = 0; ii < NUM_EDCA_QUEUES; ii++) {
59 q_len = skb_queue_len(&common->tx_queue[ii]);
60 /* Check for the need of contention */
61 if (q_len) {
62 if (tx_qinfo[ii].pkt_contended) {
63 tx_qinfo[ii].weight =
64 ((tx_qinfo[ii].weight > common->min_weight) ?
65 tx_qinfo[ii].weight - common->min_weight : 0);
66 } else {
67 tx_qinfo[ii].pkt_contended = 1;
68 tx_qinfo[ii].weight = tx_qinfo[ii].wme_params;
69 recontend_queue = true;
71 } else { /* No packets so no contention */
72 tx_qinfo[ii].weight = 0;
73 tx_qinfo[ii].pkt_contended = 0;
77 return recontend_queue;
80 /**
81 * rsi_get_num_pkts_dequeue() - This function determines the number of
82 * packets to be dequeued based on the number
83 * of bytes calculated using txop.
85 * @common: Pointer to the driver private structure.
86 * @q_num: the queue from which pkts have to be dequeued
88 * Return: pkt_num: Number of pkts to be dequeued.
90 static u32 rsi_get_num_pkts_dequeue(struct rsi_common *common, u8 q_num)
92 struct rsi_hw *adapter = common->priv;
93 struct sk_buff *skb;
94 u32 pkt_cnt = 0;
95 s16 txop = common->tx_qinfo[q_num].txop * 32;
96 __le16 r_txop;
97 struct ieee80211_rate rate;
98 struct ieee80211_hdr *wh;
99 struct ieee80211_vif *vif;
101 rate.bitrate = RSI_RATE_MCS0 * 5 * 10; /* Convert to Kbps */
102 if (q_num == VI_Q)
103 txop = ((txop << 5) / 80);
105 if (skb_queue_len(&common->tx_queue[q_num]))
106 skb = skb_peek(&common->tx_queue[q_num]);
107 else
108 return 0;
110 do {
111 wh = (struct ieee80211_hdr *)skb->data;
112 vif = rsi_get_vif(adapter, wh->addr2);
113 r_txop = ieee80211_generic_frame_duration(adapter->hw,
114 vif,
115 common->band,
116 skb->len, &rate);
117 txop -= le16_to_cpu(r_txop);
118 pkt_cnt += 1;
119 /*checking if pkts are still there*/
120 if (skb_queue_len(&common->tx_queue[q_num]) - pkt_cnt)
121 skb = skb->next;
122 else
123 break;
125 } while (txop > 0);
127 return pkt_cnt;
131 * rsi_core_determine_hal_queue() - This function determines the queue from
132 * which packet has to be dequeued.
133 * @common: Pointer to the driver private structure.
135 * Return: q_num: Corresponding queue number on success.
137 static u8 rsi_core_determine_hal_queue(struct rsi_common *common)
139 bool recontend_queue = false;
140 u32 q_len = 0;
141 u8 q_num = INVALID_QUEUE;
142 u8 ii = 0;
144 if (skb_queue_len(&common->tx_queue[MGMT_BEACON_Q])) {
145 q_num = MGMT_BEACON_Q;
146 return q_num;
148 if (skb_queue_len(&common->tx_queue[MGMT_SOFT_Q])) {
149 if (!common->mgmt_q_block)
150 q_num = MGMT_SOFT_Q;
151 return q_num;
154 if (common->hw_data_qs_blocked)
155 return q_num;
157 if (common->pkt_cnt != 0) {
158 --common->pkt_cnt;
159 return common->selected_qnum;
162 get_queue_num:
163 recontend_queue = false;
165 q_num = rsi_determine_min_weight_queue(common);
167 ii = q_num;
169 /* Selecting the queue with least back off */
170 for (; ii < NUM_EDCA_QUEUES; ii++) {
171 q_len = skb_queue_len(&common->tx_queue[ii]);
172 if (((common->tx_qinfo[ii].pkt_contended) &&
173 (common->tx_qinfo[ii].weight < common->min_weight)) &&
174 q_len) {
175 common->min_weight = common->tx_qinfo[ii].weight;
176 q_num = ii;
180 if (q_num < NUM_EDCA_QUEUES)
181 common->tx_qinfo[q_num].pkt_contended = 0;
183 /* Adjust the back off values for all queues again */
184 recontend_queue = rsi_recalculate_weights(common);
186 q_len = skb_queue_len(&common->tx_queue[q_num]);
187 if (!q_len) {
188 /* If any queues are freshly contended and the selected queue
189 * doesn't have any packets
190 * then get the queue number again with fresh values
192 if (recontend_queue)
193 goto get_queue_num;
195 q_num = INVALID_QUEUE;
196 return q_num;
199 common->selected_qnum = q_num;
200 q_len = skb_queue_len(&common->tx_queue[q_num]);
202 if (q_num == VO_Q || q_num == VI_Q) {
203 common->pkt_cnt = rsi_get_num_pkts_dequeue(common, q_num);
204 common->pkt_cnt -= 1;
207 return q_num;
211 * rsi_core_queue_pkt() - This functions enqueues the packet to the queue
212 * specified by the queue number.
213 * @common: Pointer to the driver private structure.
214 * @skb: Pointer to the socket buffer structure.
216 * Return: None.
218 static void rsi_core_queue_pkt(struct rsi_common *common,
219 struct sk_buff *skb)
221 u8 q_num = skb->priority;
222 if (q_num >= NUM_SOFT_QUEUES) {
223 rsi_dbg(ERR_ZONE, "%s: Invalid Queue Number: q_num = %d\n",
224 __func__, q_num);
225 dev_kfree_skb(skb);
226 return;
229 skb_queue_tail(&common->tx_queue[q_num], skb);
233 * rsi_core_dequeue_pkt() - This functions dequeues the packet from the queue
234 * specified by the queue number.
235 * @common: Pointer to the driver private structure.
236 * @q_num: Queue number.
238 * Return: Pointer to sk_buff structure.
240 static struct sk_buff *rsi_core_dequeue_pkt(struct rsi_common *common,
241 u8 q_num)
243 if (q_num >= NUM_SOFT_QUEUES) {
244 rsi_dbg(ERR_ZONE, "%s: Invalid Queue Number: q_num = %d\n",
245 __func__, q_num);
246 return NULL;
249 return skb_dequeue(&common->tx_queue[q_num]);
253 * rsi_core_qos_processor() - This function is used to determine the wmm queue
254 * based on the backoff procedure. Data packets are
255 * dequeued from the selected hal queue and sent to
256 * the below layers.
257 * @common: Pointer to the driver private structure.
259 * Return: None.
261 void rsi_core_qos_processor(struct rsi_common *common)
263 struct rsi_hw *adapter = common->priv;
264 struct sk_buff *skb;
265 unsigned long tstamp_1, tstamp_2;
266 u8 q_num;
267 int status;
269 tstamp_1 = jiffies;
270 while (1) {
271 q_num = rsi_core_determine_hal_queue(common);
272 rsi_dbg(DATA_TX_ZONE,
273 "%s: Queue number = %d\n", __func__, q_num);
275 if (q_num == INVALID_QUEUE) {
276 rsi_dbg(DATA_TX_ZONE, "%s: No More Pkt\n", __func__);
277 break;
279 if (common->hibernate_resume)
280 break;
282 mutex_lock(&common->tx_lock);
284 status = adapter->check_hw_queue_status(adapter, q_num);
285 if ((status <= 0)) {
286 mutex_unlock(&common->tx_lock);
287 break;
290 if ((q_num < MGMT_SOFT_Q) &&
291 ((skb_queue_len(&common->tx_queue[q_num])) <=
292 MIN_DATA_QUEUE_WATER_MARK)) {
293 if (ieee80211_queue_stopped(adapter->hw, WME_AC(q_num)))
294 ieee80211_wake_queue(adapter->hw,
295 WME_AC(q_num));
298 skb = rsi_core_dequeue_pkt(common, q_num);
299 if (skb == NULL) {
300 rsi_dbg(ERR_ZONE, "skb null\n");
301 mutex_unlock(&common->tx_lock);
302 break;
305 if (q_num == MGMT_SOFT_Q) {
306 status = rsi_send_mgmt_pkt(common, skb);
307 } else if (q_num == MGMT_BEACON_Q) {
308 status = rsi_send_pkt_to_bus(common, skb);
309 dev_kfree_skb(skb);
310 } else {
311 status = rsi_send_data_pkt(common, skb);
314 if (status) {
315 mutex_unlock(&common->tx_lock);
316 break;
319 common->tx_stats.total_tx_pkt_send[q_num]++;
321 tstamp_2 = jiffies;
322 mutex_unlock(&common->tx_lock);
324 if (time_after(tstamp_2, tstamp_1 + (300 * HZ) / 1000))
325 schedule();
329 struct rsi_sta *rsi_find_sta(struct rsi_common *common, u8 *mac_addr)
331 int i;
333 for (i = 0; i < common->max_stations; i++) {
334 if (!common->stations[i].sta)
335 continue;
336 if (!(memcmp(common->stations[i].sta->addr,
337 mac_addr, ETH_ALEN)))
338 return &common->stations[i];
340 return NULL;
343 struct ieee80211_vif *rsi_get_vif(struct rsi_hw *adapter, u8 *mac)
345 struct ieee80211_vif *vif;
346 int i;
348 for (i = 0; i < RSI_MAX_VIFS; i++) {
349 vif = adapter->vifs[i];
350 if (!vif)
351 continue;
352 if (!memcmp(vif->addr, mac, ETH_ALEN))
353 return vif;
355 return NULL;
359 * rsi_core_xmit() - This function transmits the packets received from mac80211
360 * @common: Pointer to the driver private structure.
361 * @skb: Pointer to the socket buffer structure.
363 * Return: None.
365 void rsi_core_xmit(struct rsi_common *common, struct sk_buff *skb)
367 struct rsi_hw *adapter = common->priv;
368 struct ieee80211_tx_info *info;
369 struct skb_info *tx_params;
370 struct ieee80211_hdr *wh = NULL;
371 struct ieee80211_vif *vif;
372 u8 q_num, tid = 0;
373 struct rsi_sta *rsta = NULL;
375 if ((!skb) || (!skb->len)) {
376 rsi_dbg(ERR_ZONE, "%s: Null skb/zero Length packet\n",
377 __func__);
378 goto xmit_fail;
380 if (common->fsm_state != FSM_MAC_INIT_DONE) {
381 rsi_dbg(ERR_ZONE, "%s: FSM state not open\n", __func__);
382 goto xmit_fail;
384 if (common->wow_flags & RSI_WOW_ENABLED) {
385 rsi_dbg(ERR_ZONE,
386 "%s: Blocking Tx_packets when WOWLAN is enabled\n",
387 __func__);
388 goto xmit_fail;
391 info = IEEE80211_SKB_CB(skb);
392 tx_params = (struct skb_info *)info->driver_data;
393 wh = (struct ieee80211_hdr *)&skb->data[0];
394 tx_params->sta_id = 0;
396 vif = rsi_get_vif(adapter, wh->addr2);
397 if (!vif)
398 goto xmit_fail;
399 tx_params->vif = vif;
400 tx_params->vap_id = ((struct vif_priv *)vif->drv_priv)->vap_id;
401 if ((ieee80211_is_mgmt(wh->frame_control)) ||
402 (ieee80211_is_ctl(wh->frame_control)) ||
403 (ieee80211_is_qos_nullfunc(wh->frame_control))) {
404 q_num = MGMT_SOFT_Q;
405 skb->priority = q_num;
406 } else {
407 if (ieee80211_is_data_qos(wh->frame_control)) {
408 tid = (skb->data[24] & IEEE80211_QOS_TID);
409 skb->priority = TID_TO_WME_AC(tid);
410 } else {
411 tid = IEEE80211_NONQOS_TID;
412 skb->priority = BE_Q;
415 q_num = skb->priority;
416 tx_params->tid = tid;
418 if (((vif->type == NL80211_IFTYPE_AP) ||
419 (vif->type == NL80211_IFTYPE_P2P_GO)) &&
420 (!is_broadcast_ether_addr(wh->addr1)) &&
421 (!is_multicast_ether_addr(wh->addr1))) {
422 rsta = rsi_find_sta(common, wh->addr1);
423 if (!rsta)
424 goto xmit_fail;
425 tx_params->sta_id = rsta->sta_id;
428 if (rsta) {
429 /* Start aggregation if not done for this tid */
430 if (!rsta->start_tx_aggr[tid]) {
431 rsta->start_tx_aggr[tid] = true;
432 ieee80211_start_tx_ba_session(rsta->sta,
433 tid, 0);
438 if ((q_num < MGMT_SOFT_Q) &&
439 ((skb_queue_len(&common->tx_queue[q_num]) + 1) >=
440 DATA_QUEUE_WATER_MARK)) {
441 rsi_dbg(ERR_ZONE, "%s: sw queue full\n", __func__);
442 if (!ieee80211_queue_stopped(adapter->hw, WME_AC(q_num)))
443 ieee80211_stop_queue(adapter->hw, WME_AC(q_num));
444 rsi_set_event(&common->tx_thread.event);
445 goto xmit_fail;
448 rsi_core_queue_pkt(common, skb);
449 rsi_dbg(DATA_TX_ZONE, "%s: ===> Scheduling TX thead <===\n", __func__);
450 rsi_set_event(&common->tx_thread.event);
452 return;
454 xmit_fail:
455 rsi_dbg(ERR_ZONE, "%s: Failed to queue packet\n", __func__);
456 /* Dropping pkt here */
457 ieee80211_free_txskb(common->priv->hw, skb);