kvm/ppc/booke64: Disable e6500 support
[pandora-kernel.git] / net / mac80211 / mesh_ps.c
1 /*
2  * Copyright 2012-2013, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de>
3  * Copyright 2012-2013, cozybit Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include "mesh.h"
11 #include "wme.h"
12
13
14 /* mesh PS management */
15
16 /**
17  * mps_qos_null_get - create pre-addressed QoS Null frame for mesh powersave
18  */
19 static struct sk_buff *mps_qos_null_get(struct sta_info *sta)
20 {
21         struct ieee80211_sub_if_data *sdata = sta->sdata;
22         struct ieee80211_local *local = sdata->local;
23         struct ieee80211_hdr *nullfunc; /* use 4addr header */
24         struct sk_buff *skb;
25         int size = sizeof(*nullfunc);
26         __le16 fc;
27
28         skb = dev_alloc_skb(local->hw.extra_tx_headroom + size + 2);
29         if (!skb)
30                 return NULL;
31         skb_reserve(skb, local->hw.extra_tx_headroom);
32
33         nullfunc = (struct ieee80211_hdr *) skb_put(skb, size);
34         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_QOS_NULLFUNC);
35         ieee80211_fill_mesh_addresses(nullfunc, &fc, sta->sta.addr,
36                                       sdata->vif.addr);
37         nullfunc->frame_control = fc;
38         nullfunc->duration_id = 0;
39         /* no address resolution for this frame -> set addr 1 immediately */
40         memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
41         memset(skb_put(skb, 2), 0, 2); /* append QoS control field */
42         ieee80211_mps_set_frame_flags(sdata, sta, nullfunc);
43
44         return skb;
45 }
46
47 /**
48  * mps_qos_null_tx - send a QoS Null to indicate link-specific power mode
49  */
50 static void mps_qos_null_tx(struct sta_info *sta)
51 {
52         struct sk_buff *skb;
53
54         skb = mps_qos_null_get(sta);
55         if (!skb)
56                 return;
57
58         mps_dbg(sta->sdata, "announcing peer-specific power mode to %pM\n",
59                 sta->sta.addr);
60
61         /* don't unintentionally start a MPSP */
62         if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
63                 u8 *qc = ieee80211_get_qos_ctl((void *) skb->data);
64
65                 qc[0] |= IEEE80211_QOS_CTL_EOSP;
66         }
67
68         ieee80211_tx_skb(sta->sdata, skb);
69 }
70
71 /**
72  * ieee80211_mps_local_status_update - track status of local link-specific PMs
73  *
74  * @sdata: local mesh subif
75  *
76  * sets the non-peer power mode and triggers the driver PS (re-)configuration
77  * Return BSS_CHANGED_BEACON if a beacon update is necessary.
78  */
79 u32 ieee80211_mps_local_status_update(struct ieee80211_sub_if_data *sdata)
80 {
81         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
82         struct sta_info *sta;
83         bool peering = false;
84         int light_sleep_cnt = 0;
85         int deep_sleep_cnt = 0;
86         u32 changed = 0;
87         enum nl80211_mesh_power_mode nonpeer_pm;
88
89         rcu_read_lock();
90         list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
91                 if (sdata != sta->sdata)
92                         continue;
93
94                 switch (sta->plink_state) {
95                 case NL80211_PLINK_OPN_SNT:
96                 case NL80211_PLINK_OPN_RCVD:
97                 case NL80211_PLINK_CNF_RCVD:
98                         peering = true;
99                         break;
100                 case NL80211_PLINK_ESTAB:
101                         if (sta->local_pm == NL80211_MESH_POWER_LIGHT_SLEEP)
102                                 light_sleep_cnt++;
103                         else if (sta->local_pm == NL80211_MESH_POWER_DEEP_SLEEP)
104                                 deep_sleep_cnt++;
105                         break;
106                 default:
107                         break;
108                 }
109         }
110         rcu_read_unlock();
111
112         /*
113          * Set non-peer mode to active during peering/scanning/authentication
114          * (see IEEE802.11-2012 13.14.8.3). The non-peer mesh power mode is
115          * deep sleep if the local STA is in light or deep sleep towards at
116          * least one mesh peer (see 13.14.3.1). Otherwise, set it to the
117          * user-configured default value.
118          */
119         if (peering) {
120                 mps_dbg(sdata, "setting non-peer PM to active for peering\n");
121                 nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
122         } else if (light_sleep_cnt || deep_sleep_cnt) {
123                 mps_dbg(sdata, "setting non-peer PM to deep sleep\n");
124                 nonpeer_pm = NL80211_MESH_POWER_DEEP_SLEEP;
125         } else {
126                 mps_dbg(sdata, "setting non-peer PM to user value\n");
127                 nonpeer_pm = ifmsh->mshcfg.power_mode;
128         }
129
130         /* need update if sleep counts move between 0 and non-zero */
131         if (ifmsh->nonpeer_pm != nonpeer_pm ||
132             !ifmsh->ps_peers_light_sleep != !light_sleep_cnt ||
133             !ifmsh->ps_peers_deep_sleep != !deep_sleep_cnt)
134                 changed = BSS_CHANGED_BEACON;
135
136         ifmsh->nonpeer_pm = nonpeer_pm;
137         ifmsh->ps_peers_light_sleep = light_sleep_cnt;
138         ifmsh->ps_peers_deep_sleep = deep_sleep_cnt;
139
140         return changed;
141 }
142
143 /**
144  * ieee80211_mps_set_sta_local_pm - set local PM towards a mesh STA
145  *
146  * @sta: mesh STA
147  * @pm: the power mode to set
148  * Return BSS_CHANGED_BEACON if a beacon update is in order.
149  */
150 u32 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
151                                    enum nl80211_mesh_power_mode pm)
152 {
153         struct ieee80211_sub_if_data *sdata = sta->sdata;
154
155         mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
156                 pm, sta->sta.addr);
157
158         sta->local_pm = pm;
159
160         /*
161          * announce peer-specific power mode transition
162          * (see IEEE802.11-2012 13.14.3.2 and 13.14.3.3)
163          */
164         if (sta->plink_state == NL80211_PLINK_ESTAB)
165                 mps_qos_null_tx(sta);
166
167         return ieee80211_mps_local_status_update(sdata);
168 }
169
170 /**
171  * ieee80211_mps_set_frame_flags - set mesh PS flags in FC (and QoS Control)
172  *
173  * @sdata: local mesh subif
174  * @sta: mesh STA
175  * @hdr: 802.11 frame header
176  *
177  * see IEEE802.11-2012 8.2.4.1.7 and 8.2.4.5.11
178  *
179  * NOTE: sta must be given when an individually-addressed QoS frame header
180  * is handled, for group-addressed and management frames it is not used
181  */
182 void ieee80211_mps_set_frame_flags(struct ieee80211_sub_if_data *sdata,
183                                    struct sta_info *sta,
184                                    struct ieee80211_hdr *hdr)
185 {
186         enum nl80211_mesh_power_mode pm;
187         u8 *qc;
188
189         if (WARN_ON(is_unicast_ether_addr(hdr->addr1) &&
190                     ieee80211_is_data_qos(hdr->frame_control) &&
191                     !sta))
192                 return;
193
194         if (is_unicast_ether_addr(hdr->addr1) &&
195             ieee80211_is_data_qos(hdr->frame_control) &&
196             sta->plink_state == NL80211_PLINK_ESTAB)
197                 pm = sta->local_pm;
198         else
199                 pm = sdata->u.mesh.nonpeer_pm;
200
201         if (pm == NL80211_MESH_POWER_ACTIVE)
202                 hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_PM);
203         else
204                 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
205
206         if (!ieee80211_is_data_qos(hdr->frame_control))
207                 return;
208
209         qc = ieee80211_get_qos_ctl(hdr);
210
211         if ((is_unicast_ether_addr(hdr->addr1) &&
212              pm == NL80211_MESH_POWER_DEEP_SLEEP) ||
213             (is_multicast_ether_addr(hdr->addr1) &&
214              sdata->u.mesh.ps_peers_deep_sleep > 0))
215                 qc[1] |= (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
216         else
217                 qc[1] &= ~(IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8);
218 }
219
220 /**
221  * ieee80211_mps_sta_status_update - update buffering status of neighbor STA
222  *
223  * @sta: mesh STA
224  *
225  * called after change of peering status or non-peer/peer-specific power mode
226  */
227 void ieee80211_mps_sta_status_update(struct sta_info *sta)
228 {
229         enum nl80211_mesh_power_mode pm;
230         bool do_buffer;
231
232         /*
233          * use peer-specific power mode if peering is established and the
234          * peer's power mode is known
235          */
236         if (sta->plink_state == NL80211_PLINK_ESTAB &&
237             sta->peer_pm != NL80211_MESH_POWER_UNKNOWN)
238                 pm = sta->peer_pm;
239         else
240                 pm = sta->nonpeer_pm;
241
242         do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
243
244         /* Don't let the same PS state be set twice */
245         if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer)
246                 return;
247
248         if (do_buffer) {
249                 set_sta_flag(sta, WLAN_STA_PS_STA);
250                 atomic_inc(&sta->sdata->u.mesh.ps.num_sta_ps);
251                 mps_dbg(sta->sdata, "start PS buffering frames towards %pM\n",
252                         sta->sta.addr);
253         } else {
254                 ieee80211_sta_ps_deliver_wakeup(sta);
255         }
256
257         /* clear the MPSP flags for non-peers or active STA */
258         if (sta->plink_state != NL80211_PLINK_ESTAB) {
259                 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
260                 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
261         } else if (!do_buffer) {
262                 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
263         }
264 }
265
266 static void mps_set_sta_peer_pm(struct sta_info *sta,
267                                 struct ieee80211_hdr *hdr)
268 {
269         enum nl80211_mesh_power_mode pm;
270         u8 *qc = ieee80211_get_qos_ctl(hdr);
271
272         /*
273          * Test Power Management field of frame control (PW) and
274          * mesh power save level subfield of QoS control field (PSL)
275          *
276          * | PM | PSL| Mesh PM |
277          * +----+----+---------+
278          * | 0  |Rsrv|  Active |
279          * | 1  | 0  |  Light  |
280          * | 1  | 1  |  Deep   |
281          */
282         if (ieee80211_has_pm(hdr->frame_control)) {
283                 if (qc[1] & (IEEE80211_QOS_CTL_MESH_PS_LEVEL >> 8))
284                         pm = NL80211_MESH_POWER_DEEP_SLEEP;
285                 else
286                         pm = NL80211_MESH_POWER_LIGHT_SLEEP;
287         } else {
288                 pm = NL80211_MESH_POWER_ACTIVE;
289         }
290
291         if (sta->peer_pm == pm)
292                 return;
293
294         mps_dbg(sta->sdata, "STA %pM enters mode %d\n",
295                 sta->sta.addr, pm);
296
297         sta->peer_pm = pm;
298
299         ieee80211_mps_sta_status_update(sta);
300 }
301
302 static void mps_set_sta_nonpeer_pm(struct sta_info *sta,
303                                    struct ieee80211_hdr *hdr)
304 {
305         enum nl80211_mesh_power_mode pm;
306
307         if (ieee80211_has_pm(hdr->frame_control))
308                 pm = NL80211_MESH_POWER_DEEP_SLEEP;
309         else
310                 pm = NL80211_MESH_POWER_ACTIVE;
311
312         if (sta->nonpeer_pm == pm)
313                 return;
314
315         mps_dbg(sta->sdata, "STA %pM sets non-peer mode to %d\n",
316                 sta->sta.addr, pm);
317
318         sta->nonpeer_pm = pm;
319
320         ieee80211_mps_sta_status_update(sta);
321 }
322
323 /**
324  * ieee80211_mps_rx_h_sta_process - frame receive handler for mesh powersave
325  *
326  * @sta: STA info that transmitted the frame
327  * @hdr: IEEE 802.11 (QoS) Header
328  */
329 void ieee80211_mps_rx_h_sta_process(struct sta_info *sta,
330                                     struct ieee80211_hdr *hdr)
331 {
332         if (is_unicast_ether_addr(hdr->addr1) &&
333             ieee80211_is_data_qos(hdr->frame_control)) {
334                 /*
335                  * individually addressed QoS Data/Null frames contain
336                  * peer link-specific PS mode towards the local STA
337                  */
338                 mps_set_sta_peer_pm(sta, hdr);
339
340                 /* check for mesh Peer Service Period trigger frames */
341                 ieee80211_mpsp_trigger_process(ieee80211_get_qos_ctl(hdr),
342                                                sta, false, false);
343         } else {
344                 /*
345                  * can only determine non-peer PS mode
346                  * (see IEEE802.11-2012 8.2.4.1.7)
347                  */
348                 mps_set_sta_nonpeer_pm(sta, hdr);
349         }
350 }
351
352
353 /* mesh PS frame release */
354
355 static void mpsp_trigger_send(struct sta_info *sta, bool rspi, bool eosp)
356 {
357         struct ieee80211_sub_if_data *sdata = sta->sdata;
358         struct sk_buff *skb;
359         struct ieee80211_hdr *nullfunc;
360         struct ieee80211_tx_info *info;
361         u8 *qc;
362
363         skb = mps_qos_null_get(sta);
364         if (!skb)
365                 return;
366
367         nullfunc = (struct ieee80211_hdr *) skb->data;
368         if (!eosp)
369                 nullfunc->frame_control |=
370                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
371         /*
372          * | RSPI | EOSP |  MPSP triggering   |
373          * +------+------+--------------------+
374          * |  0   |  0   | local STA is owner |
375          * |  0   |  1   | no MPSP (MPSP end) |
376          * |  1   |  0   | both STA are owner |
377          * |  1   |  1   | peer STA is owner  | see IEEE802.11-2012 13.14.9.2
378          */
379         qc = ieee80211_get_qos_ctl(nullfunc);
380         if (rspi)
381                 qc[1] |= (IEEE80211_QOS_CTL_RSPI >> 8);
382         if (eosp)
383                 qc[0] |= IEEE80211_QOS_CTL_EOSP;
384
385         info = IEEE80211_SKB_CB(skb);
386
387         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
388                        IEEE80211_TX_CTL_REQ_TX_STATUS;
389
390         mps_dbg(sdata, "sending MPSP trigger%s%s to %pM\n",
391                 rspi ? " RSPI" : "", eosp ? " EOSP" : "", sta->sta.addr);
392
393         ieee80211_tx_skb(sdata, skb);
394 }
395
396 /**
397  * mpsp_qos_null_append - append QoS Null frame to MPSP skb queue if needed
398  *
399  * To properly end a mesh MPSP the last transmitted frame has to set the EOSP
400  * flag in the QoS Control field. In case the current tailing frame is not a
401  * QoS Data frame, append a QoS Null to carry the flag.
402  */
403 static void mpsp_qos_null_append(struct sta_info *sta,
404                                  struct sk_buff_head *frames)
405 {
406         struct ieee80211_sub_if_data *sdata = sta->sdata;
407         struct sk_buff *new_skb, *skb = skb_peek_tail(frames);
408         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
409         struct ieee80211_tx_info *info;
410
411         if (ieee80211_is_data_qos(hdr->frame_control))
412                 return;
413
414         new_skb = mps_qos_null_get(sta);
415         if (!new_skb)
416                 return;
417
418         mps_dbg(sdata, "appending QoS Null in MPSP towards %pM\n",
419                 sta->sta.addr);
420         /*
421          * This frame has to be transmitted last. Assign lowest priority to
422          * make sure it cannot pass other frames when releasing multiple ACs.
423          */
424         new_skb->priority = 1;
425         skb_set_queue_mapping(new_skb, IEEE80211_AC_BK);
426         ieee80211_set_qos_hdr(sdata, new_skb);
427
428         info = IEEE80211_SKB_CB(new_skb);
429         info->control.vif = &sdata->vif;
430         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
431
432         __skb_queue_tail(frames, new_skb);
433 }
434
435 /**
436  * mps_frame_deliver - transmit frames during mesh powersave
437  *
438  * @sta: STA info to transmit to
439  * @n_frames: number of frames to transmit. -1 for all
440  */
441 static void mps_frame_deliver(struct sta_info *sta, int n_frames)
442 {
443         struct ieee80211_sub_if_data *sdata = sta->sdata;
444         struct ieee80211_local *local = sdata->local;
445         int ac;
446         struct sk_buff_head frames;
447         struct sk_buff *skb;
448         bool more_data = false;
449
450         skb_queue_head_init(&frames);
451
452         /* collect frame(s) from buffers */
453         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
454                 while (n_frames != 0) {
455                         skb = skb_dequeue(&sta->tx_filtered[ac]);
456                         if (!skb) {
457                                 skb = skb_dequeue(
458                                         &sta->ps_tx_buf[ac]);
459                                 if (skb)
460                                         local->total_ps_buffered--;
461                         }
462                         if (!skb)
463                                 break;
464                         n_frames--;
465                         __skb_queue_tail(&frames, skb);
466                 }
467
468                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
469                     !skb_queue_empty(&sta->ps_tx_buf[ac]))
470                         more_data = true;
471         }
472
473         /* nothing to send? -> EOSP */
474         if (skb_queue_empty(&frames)) {
475                 mpsp_trigger_send(sta, false, true);
476                 return;
477         }
478
479         /* in a MPSP make sure the last skb is a QoS Data frame */
480         if (test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
481                 mpsp_qos_null_append(sta, &frames);
482
483         mps_dbg(sta->sdata, "sending %d frames to PS STA %pM\n",
484                 skb_queue_len(&frames), sta->sta.addr);
485
486         /* prepare collected frames for transmission */
487         skb_queue_walk(&frames, skb) {
488                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
489                 struct ieee80211_hdr *hdr = (void *) skb->data;
490
491                 /*
492                  * Tell TX path to send this frame even though the
493                  * STA may still remain is PS mode after this frame
494                  * exchange.
495                  */
496                 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
497
498                 if (more_data || !skb_queue_is_last(&frames, skb))
499                         hdr->frame_control |=
500                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
501                 else
502                         hdr->frame_control &=
503                                 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
504
505                 if (skb_queue_is_last(&frames, skb) &&
506                     ieee80211_is_data_qos(hdr->frame_control)) {
507                         u8 *qoshdr = ieee80211_get_qos_ctl(hdr);
508
509                         /* MPSP trigger frame ends service period */
510                         *qoshdr |= IEEE80211_QOS_CTL_EOSP;
511                         info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
512                 }
513         }
514
515         ieee80211_add_pending_skbs(local, &frames);
516         sta_info_recalc_tim(sta);
517 }
518
519 /**
520  * ieee80211_mpsp_trigger_process - track status of mesh Peer Service Periods
521  *
522  * @qc: QoS Control field
523  * @sta: peer to start a MPSP with
524  * @tx: frame was transmitted by the local STA
525  * @acked: frame has been transmitted successfully
526  *
527  * NOTE: active mode STA may only serve as MPSP owner
528  */
529 void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
530                                     bool tx, bool acked)
531 {
532         u8 rspi = qc[1] & (IEEE80211_QOS_CTL_RSPI >> 8);
533         u8 eosp = qc[0] & IEEE80211_QOS_CTL_EOSP;
534
535         if (tx) {
536                 if (rspi && acked)
537                         set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
538
539                 if (eosp)
540                         clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
541                 else if (acked &&
542                          test_sta_flag(sta, WLAN_STA_PS_STA) &&
543                          !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
544                         mps_frame_deliver(sta, -1);
545         } else {
546                 if (eosp)
547                         clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
548                 else if (sta->local_pm != NL80211_MESH_POWER_ACTIVE)
549                         set_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
550
551                 if (rspi && !test_and_set_sta_flag(sta, WLAN_STA_MPSP_OWNER))
552                         mps_frame_deliver(sta, -1);
553         }
554 }
555
556 /**
557  * ieee80211_mps_frame_release - release buffered frames in response to beacon
558  *
559  * @sta: mesh STA
560  * @elems: beacon IEs
561  *
562  * For peers if we have individually-addressed frames buffered or the peer
563  * indicates buffered frames, send a corresponding MPSP trigger frame. Since
564  * we do not evaluate the awake window duration, QoS Nulls are used as MPSP
565  * trigger frames. If the neighbour STA is not a peer, only send single frames.
566  */
567 void ieee80211_mps_frame_release(struct sta_info *sta,
568                                  struct ieee802_11_elems *elems)
569 {
570         int ac, buffer_local = 0;
571         bool has_buffered = false;
572
573         /* TIM map only for LLID <= IEEE80211_MAX_AID */
574         if (sta->plink_state == NL80211_PLINK_ESTAB)
575                 has_buffered = ieee80211_check_tim(elems->tim, elems->tim_len,
576                                 le16_to_cpu(sta->llid) % IEEE80211_MAX_AID);
577
578         if (has_buffered)
579                 mps_dbg(sta->sdata, "%pM indicates buffered frames\n",
580                         sta->sta.addr);
581
582         /* only transmit to PS STA with announced, non-zero awake window */
583         if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
584             (!elems->awake_window || !le16_to_cpu(*elems->awake_window)))
585                 return;
586
587         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
588                 buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) +
589                                 skb_queue_len(&sta->tx_filtered[ac]);
590
591         if (!has_buffered && !buffer_local)
592                 return;
593
594         if (sta->plink_state == NL80211_PLINK_ESTAB)
595                 mpsp_trigger_send(sta, has_buffered, !buffer_local);
596         else
597                 mps_frame_deliver(sta, 1);
598 }