blob: ca2676f79bbb50f8fe8379d86407d09849450b3e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -05002/*
3 * mac80211 glue code for mac80211 Prism54 drivers
4 *
5 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
6 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
7 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
8 *
9 * Based on:
10 * - the islsm (softmac prism54) driver, which is:
11 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
12 * - stlc45xx driver
13 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050014 */
15
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050017#include <linux/firmware.h>
18#include <linux/etherdevice.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040019#include <linux/module.h>
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050020
21#include <net/mac80211.h>
22
23#include "p54.h"
24#include "lmac.h"
25
Rusty Russelleb939922011-12-19 14:08:01 +000026static bool modparam_nohwcrypt;
Joe Perches2ef00c52018-03-23 15:54:37 -070027module_param_named(nohwcrypt, modparam_nohwcrypt, bool, 0444);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050028MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
29MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
30MODULE_DESCRIPTION("Softmac Prism54 common code");
31MODULE_LICENSE("GPL");
32MODULE_ALIAS("prism54common");
33
Johannes Berg17f6f052010-02-19 19:06:54 +010034static int p54_sta_add_remove(struct ieee80211_hw *hw,
35 struct ieee80211_vif *vif,
36 struct ieee80211_sta *sta)
37{
38 struct p54_common *priv = hw->priv;
39
40 /*
41 * Notify the firmware that we don't want or we don't
42 * need to buffer frames for this station anymore.
43 */
44
45 p54_sta_unlock(priv, sta->addr);
46
47 return 0;
48}
49
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050050static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
51 enum sta_notify_cmd notify_cmd,
52 struct ieee80211_sta *sta)
53{
54 struct p54_common *priv = dev->priv;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050055
Johannes Berg17f6f052010-02-19 19:06:54 +010056 switch (notify_cmd) {
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050057 case STA_NOTIFY_AWAKE:
58 /* update the firmware's filter table */
59 p54_sta_unlock(priv, sta->addr);
60 break;
61 default:
62 break;
63 }
64}
65
66static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
67 bool set)
68{
69 struct p54_common *priv = dev->priv;
70
71 return p54_update_beacon_tim(priv, sta->aid, set);
72}
73
Christian Lampartere0f114e2009-07-07 19:08:07 +020074u8 *p54_find_ie(struct sk_buff *skb, u8 ie)
75{
76 struct ieee80211_mgmt *mgmt = (void *)skb->data;
77 u8 *pos, *end;
78
79 if (skb->len <= sizeof(mgmt))
80 return NULL;
81
82 pos = (u8 *)mgmt->u.beacon.variable;
83 end = skb->data + skb->len;
84 while (pos < end) {
85 if (pos + 2 + pos[1] > end)
86 return NULL;
87
88 if (pos[0] == ie)
89 return pos;
90
91 pos += 2 + pos[1];
92 }
93 return NULL;
94}
95
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050096static int p54_beacon_format_ie_tim(struct sk_buff *skb)
97{
98 /*
99 * the good excuse for this mess is ... the firmware.
100 * The dummy TIM MUST be at the end of the beacon frame,
101 * because it'll be overwritten!
102 */
Christian Lampartere0f114e2009-07-07 19:08:07 +0200103 u8 *tim;
104 u8 dtim_len;
105 u8 dtim_period;
106 u8 *next;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500107
Christian Lampartere0f114e2009-07-07 19:08:07 +0200108 tim = p54_find_ie(skb, WLAN_EID_TIM);
109 if (!tim)
110 return 0;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500111
Christian Lampartere0f114e2009-07-07 19:08:07 +0200112 dtim_len = tim[1];
113 dtim_period = tim[3];
114 next = tim + 2 + dtim_len;
115
116 if (dtim_len < 3)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500117 return -EINVAL;
118
Christian Lampartere0f114e2009-07-07 19:08:07 +0200119 memmove(tim, next, skb_tail_pointer(skb) - next);
120 tim = skb_tail_pointer(skb) - (dtim_len + 2);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500121
Christian Lampartere0f114e2009-07-07 19:08:07 +0200122 /* add the dummy at the end */
123 tim[0] = WLAN_EID_TIM;
124 tim[1] = 3;
125 tim[2] = 0;
126 tim[3] = dtim_period;
127 tim[4] = 0;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500128
Christian Lampartere0f114e2009-07-07 19:08:07 +0200129 if (dtim_len > 3)
130 skb_trim(skb, skb->len - (dtim_len - 3));
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500131
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500132 return 0;
133}
134
135static int p54_beacon_update(struct p54_common *priv,
136 struct ieee80211_vif *vif)
137{
Christian Lamparter390fd9d2012-09-08 01:28:42 +0200138 struct ieee80211_tx_control control = { };
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500139 struct sk_buff *beacon;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500140 int ret;
141
142 beacon = ieee80211_beacon_get(priv->hw, vif);
143 if (!beacon)
144 return -ENOMEM;
145 ret = p54_beacon_format_ie_tim(beacon);
146 if (ret)
147 return ret;
148
Christian Lamparter46df10a2009-07-16 20:03:47 +0200149 /*
150 * During operation, the firmware takes care of beaconing.
151 * The driver only needs to upload a new beacon template, once
152 * the template was changed by the stack or userspace.
153 *
154 * LMAC API 3.2.2 also specifies that the driver does not need
155 * to cancel the old beacon template by hand, instead the firmware
156 * will release the previous one through the feedback mechanism.
157 */
Christian Lamparter390fd9d2012-09-08 01:28:42 +0200158 p54_tx_80211(priv->hw, &control, beacon);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500159 priv->tsf_high32 = 0;
160 priv->tsf_low32 = 0;
161
162 return 0;
163}
164
165static int p54_start(struct ieee80211_hw *dev)
166{
167 struct p54_common *priv = dev->priv;
168 int err;
169
170 mutex_lock(&priv->conf_mutex);
171 err = priv->open(dev);
172 if (err)
173 goto out;
174 P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
175 P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
176 P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
177 P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
178 err = p54_set_edcf(priv);
179 if (err)
180 goto out;
181
Joe Perches93803b32015-03-02 19:54:49 -0800182 eth_broadcast_addr(priv->bssid);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500183 priv->mode = NL80211_IFTYPE_MONITOR;
184 err = p54_setup_mac(priv);
185 if (err) {
186 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
187 goto out;
188 }
189
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400190 ieee80211_queue_delayed_work(dev, &priv->work, 0);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500191
192 priv->softled_state = 0;
193 err = p54_set_leds(priv);
194
195out:
196 mutex_unlock(&priv->conf_mutex);
197 return err;
198}
199
200static void p54_stop(struct ieee80211_hw *dev)
201{
202 struct p54_common *priv = dev->priv;
203 int i;
204
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500205 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
206 priv->softled_state = 0;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500207 cancel_delayed_work_sync(&priv->work);
Christian Lamparter0d781562011-08-20 01:53:59 +0200208 mutex_lock(&priv->conf_mutex);
209 p54_set_leds(priv);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500210 priv->stop(dev);
211 skb_queue_purge(&priv->tx_pending);
212 skb_queue_purge(&priv->tx_queue);
213 for (i = 0; i < P54_QUEUE_NUM; i++) {
214 priv->tx_stats[i].count = 0;
215 priv->tx_stats[i].len = 0;
216 }
217
218 priv->beacon_req_id = cpu_to_le32(0);
219 priv->tsf_high32 = priv->tsf_low32 = 0;
220 mutex_unlock(&priv->conf_mutex);
221}
222
223static int p54_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100224 struct ieee80211_vif *vif)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500225{
226 struct p54_common *priv = dev->priv;
Christian Lamparter972a3132012-03-03 21:24:43 +0100227 int err;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500228
Johannes Bergc1288b12012-01-19 09:29:57 +0100229 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
230
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500231 mutex_lock(&priv->conf_mutex);
232 if (priv->mode != NL80211_IFTYPE_MONITOR) {
233 mutex_unlock(&priv->conf_mutex);
234 return -EOPNOTSUPP;
235 }
236
Johannes Berg1ed32e42009-12-23 13:15:45 +0100237 priv->vif = vif;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500238
Johannes Berg1ed32e42009-12-23 13:15:45 +0100239 switch (vif->type) {
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500240 case NL80211_IFTYPE_STATION:
241 case NL80211_IFTYPE_ADHOC:
242 case NL80211_IFTYPE_AP:
243 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ed32e42009-12-23 13:15:45 +0100244 priv->mode = vif->type;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500245 break;
246 default:
247 mutex_unlock(&priv->conf_mutex);
248 return -EOPNOTSUPP;
249 }
250
Johannes Berg1ed32e42009-12-23 13:15:45 +0100251 memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
Christian Lamparter972a3132012-03-03 21:24:43 +0100252 err = p54_setup_mac(priv);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500253 mutex_unlock(&priv->conf_mutex);
Christian Lamparter972a3132012-03-03 21:24:43 +0100254 return err;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500255}
256
257static void p54_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100258 struct ieee80211_vif *vif)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500259{
260 struct p54_common *priv = dev->priv;
261
262 mutex_lock(&priv->conf_mutex);
263 priv->vif = NULL;
Christian Lamparter46df10a2009-07-16 20:03:47 +0200264
265 /*
266 * LMAC API 3.2.2 states that any active beacon template must be
267 * canceled by the driver before attempting a mode transition.
268 */
269 if (le32_to_cpu(priv->beacon_req_id) != 0) {
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500270 p54_tx_cancel(priv, priv->beacon_req_id);
Christian Lamparter46df10a2009-07-16 20:03:47 +0200271 wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500272 }
273 priv->mode = NL80211_IFTYPE_MONITOR;
Joe Perches93803b32015-03-02 19:54:49 -0800274 eth_zero_addr(priv->mac_addr);
275 eth_zero_addr(priv->bssid);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500276 p54_setup_mac(priv);
277 mutex_unlock(&priv->conf_mutex);
278}
279
Christian Lamparter0d781562011-08-20 01:53:59 +0200280static int p54_wait_for_stats(struct ieee80211_hw *dev)
281{
282 struct p54_common *priv = dev->priv;
283 int ret;
284
285 priv->update_stats = true;
286 ret = p54_fetch_statistics(priv);
287 if (ret)
288 return ret;
289
290 ret = wait_for_completion_interruptible_timeout(&priv->stat_comp, HZ);
291 if (ret == 0)
292 return -ETIMEDOUT;
293
294 return 0;
295}
296
297static void p54_reset_stats(struct p54_common *priv)
298{
299 struct ieee80211_channel *chan = priv->curchan;
300
301 if (chan) {
302 struct survey_info *info = &priv->survey[chan->hw_value];
303
304 /* only reset channel statistics, don't touch .filled, etc. */
Johannes Berg4ed20be2014-11-14 16:35:34 +0100305 info->time = 0;
306 info->time_busy = 0;
307 info->time_tx = 0;
Christian Lamparter0d781562011-08-20 01:53:59 +0200308 }
309
310 priv->update_stats = true;
311 priv->survey_raw.active = 0;
312 priv->survey_raw.cca = 0;
313 priv->survey_raw.tx = 0;
314}
315
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500316static int p54_config(struct ieee80211_hw *dev, u32 changed)
317{
318 int ret = 0;
319 struct p54_common *priv = dev->priv;
320 struct ieee80211_conf *conf = &dev->conf;
321
322 mutex_lock(&priv->conf_mutex);
323 if (changed & IEEE80211_CONF_CHANGE_POWER)
324 priv->output_power = conf->power_level << 2;
325 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Christian Lamparter0d781562011-08-20 01:53:59 +0200326 struct ieee80211_channel *oldchan;
327 WARN_ON(p54_wait_for_stats(dev));
328 oldchan = priv->curchan;
329 priv->curchan = NULL;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500330 ret = p54_scan(priv, P54_SCAN_EXIT, 0);
Christian Lamparter0d781562011-08-20 01:53:59 +0200331 if (ret) {
332 priv->curchan = oldchan;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500333 goto out;
Christian Lamparter0d781562011-08-20 01:53:59 +0200334 }
335 /*
336 * TODO: Use the LM_SCAN_TRAP to determine the current
337 * operating channel.
338 */
Karl Beldan675a0b02013-03-25 16:26:57 +0100339 priv->curchan = priv->hw->conf.chandef.chan;
Christian Lamparter0d781562011-08-20 01:53:59 +0200340 p54_reset_stats(priv);
341 WARN_ON(p54_fetch_statistics(priv));
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500342 }
343 if (changed & IEEE80211_CONF_CHANGE_PS) {
Christian Lamparter0d781562011-08-20 01:53:59 +0200344 WARN_ON(p54_wait_for_stats(dev));
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500345 ret = p54_set_ps(priv);
346 if (ret)
347 goto out;
Christian Lamparter0d781562011-08-20 01:53:59 +0200348 WARN_ON(p54_wait_for_stats(dev));
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500349 }
Christian Lamparter6208f8b2009-08-07 19:39:05 +0200350 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
Christian Lamparter0d781562011-08-20 01:53:59 +0200351 WARN_ON(p54_wait_for_stats(dev));
Christian Lamparter6208f8b2009-08-07 19:39:05 +0200352 ret = p54_setup_mac(priv);
353 if (ret)
354 goto out;
Christian Lamparter0d781562011-08-20 01:53:59 +0200355 WARN_ON(p54_wait_for_stats(dev));
Christian Lamparter6208f8b2009-08-07 19:39:05 +0200356 }
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500357
358out:
359 mutex_unlock(&priv->conf_mutex);
360 return ret;
361}
362
Christian Lamparterbe8d98e2011-04-24 17:22:59 +0200363static u64 p54_prepare_multicast(struct ieee80211_hw *dev,
364 struct netdev_hw_addr_list *mc_list)
365{
366 struct p54_common *priv = dev->priv;
367 struct netdev_hw_addr *ha;
368 int i;
369
370 BUILD_BUG_ON(ARRAY_SIZE(priv->mc_maclist) !=
371 ARRAY_SIZE(((struct p54_group_address_table *)NULL)->mac_list));
372 /*
373 * The first entry is reserved for the global broadcast MAC.
374 * Otherwise the firmware will drop it and ARP will no longer work.
375 */
376 i = 1;
377 priv->mc_maclist_num = netdev_hw_addr_list_count(mc_list) + i;
378 netdev_hw_addr_list_for_each(ha, mc_list) {
379 memcpy(&priv->mc_maclist[i], ha->addr, ETH_ALEN);
380 i++;
381 if (i >= ARRAY_SIZE(priv->mc_maclist))
382 break;
383 }
384
385 return 1; /* update */
386}
387
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500388static void p54_configure_filter(struct ieee80211_hw *dev,
389 unsigned int changed_flags,
390 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200391 u64 multicast)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500392{
393 struct p54_common *priv = dev->priv;
394
Johannes Bergdf140462015-04-22 14:40:58 +0200395 *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500396
397 priv->filter_flags = *total_flags;
398
Johannes Bergdf140462015-04-22 14:40:58 +0200399 if (changed_flags & FIF_OTHER_BSS)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500400 p54_setup_mac(priv);
Christian Lamparterbe8d98e2011-04-24 17:22:59 +0200401
402 if (changed_flags & FIF_ALLMULTI || multicast)
403 p54_set_groupfilter(priv);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500404}
405
Eliad Peller8a3a3c82011-10-02 10:15:52 +0200406static int p54_conf_tx(struct ieee80211_hw *dev,
407 struct ieee80211_vif *vif, u16 queue,
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500408 const struct ieee80211_tx_queue_params *params)
409{
410 struct p54_common *priv = dev->priv;
411 int ret;
412
413 mutex_lock(&priv->conf_mutex);
Christian Lamparter718126a2009-08-07 19:38:51 +0200414 if (queue < dev->queues) {
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500415 P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
416 params->cw_min, params->cw_max, params->txop);
417 ret = p54_set_edcf(priv);
418 } else
419 ret = -EINVAL;
420 mutex_unlock(&priv->conf_mutex);
421 return ret;
422}
423
424static void p54_work(struct work_struct *work)
425{
426 struct p54_common *priv = container_of(work, struct p54_common,
427 work.work);
428
429 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
430 return ;
431
432 /*
433 * TODO: walk through tx_queue and do the following tasks
434 * 1. initiate bursts.
435 * 2. cancel stuck frames / reset the device if necessary.
436 */
437
Christian Lamparter0d781562011-08-20 01:53:59 +0200438 mutex_lock(&priv->conf_mutex);
439 WARN_ON_ONCE(p54_fetch_statistics(priv));
440 mutex_unlock(&priv->conf_mutex);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500441}
442
443static int p54_get_stats(struct ieee80211_hw *dev,
444 struct ieee80211_low_level_stats *stats)
445{
446 struct p54_common *priv = dev->priv;
447
448 memcpy(stats, &priv->stats, sizeof(*stats));
449 return 0;
450}
451
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500452static void p54_bss_info_changed(struct ieee80211_hw *dev,
453 struct ieee80211_vif *vif,
454 struct ieee80211_bss_conf *info,
455 u32 changed)
456{
457 struct p54_common *priv = dev->priv;
458
459 mutex_lock(&priv->conf_mutex);
460 if (changed & BSS_CHANGED_BSSID) {
461 memcpy(priv->bssid, info->bssid, ETH_ALEN);
462 p54_setup_mac(priv);
463 }
464
465 if (changed & BSS_CHANGED_BEACON) {
466 p54_scan(priv, P54_SCAN_EXIT, 0);
467 p54_setup_mac(priv);
468 p54_beacon_update(priv, vif);
469 p54_set_edcf(priv);
470 }
471
472 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
473 priv->use_short_slot = info->use_short_slot;
474 p54_set_edcf(priv);
475 }
476 if (changed & BSS_CHANGED_BASIC_RATES) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200477 if (dev->conf.chandef.chan->band == NL80211_BAND_5GHZ)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500478 priv->basic_rate_mask = (info->basic_rates << 4);
479 else
480 priv->basic_rate_mask = info->basic_rates;
481 p54_setup_mac(priv);
482 if (priv->fw_var >= 0x500)
483 p54_scan(priv, P54_SCAN_EXIT, 0);
484 }
485 if (changed & BSS_CHANGED_ASSOC) {
486 if (info->assoc) {
487 priv->aid = info->aid;
488 priv->wakeup_timer = info->beacon_int *
489 info->dtim_period * 5;
490 p54_setup_mac(priv);
Christian Lampartere0f114e2009-07-07 19:08:07 +0200491 } else {
492 priv->wakeup_timer = 500;
493 priv->aid = 0;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500494 }
495 }
496
497 mutex_unlock(&priv->conf_mutex);
498}
499
500static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
501 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
502 struct ieee80211_key_conf *key)
503{
504 struct p54_common *priv = dev->priv;
505 int slot, ret = 0;
506 u8 algo = 0;
507 u8 *addr = NULL;
508
509 if (modparam_nohwcrypt)
510 return -EOPNOTSUPP;
511
Christian Lamparter960334a2012-09-08 01:48:19 +0200512 if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT) {
513 /*
514 * Unfortunately most/all firmwares are trying to decrypt
515 * incoming management frames if a suitable key can be found.
516 * However, in doing so the data in these frames gets
517 * corrupted. So, we can't have firmware supported crypto
518 * offload in this case.
519 */
520 return -EOPNOTSUPP;
521 }
522
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500523 mutex_lock(&priv->conf_mutex);
524 if (cmd == SET_KEY) {
Johannes Berg97359d12010-08-10 09:46:38 +0200525 switch (key->cipher) {
526 case WLAN_CIPHER_SUITE_TKIP:
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500527 if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
528 BR_DESC_PRIV_CAP_TKIP))) {
529 ret = -EOPNOTSUPP;
530 goto out_unlock;
531 }
532 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
533 algo = P54_CRYPTO_TKIPMICHAEL;
534 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200535 case WLAN_CIPHER_SUITE_WEP40:
536 case WLAN_CIPHER_SUITE_WEP104:
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500537 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
538 ret = -EOPNOTSUPP;
539 goto out_unlock;
540 }
541 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
542 algo = P54_CRYPTO_WEP;
543 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200544 case WLAN_CIPHER_SUITE_CCMP:
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500545 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
546 ret = -EOPNOTSUPP;
547 goto out_unlock;
548 }
549 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
550 algo = P54_CRYPTO_AESCCMP;
551 break;
552 default:
553 ret = -EOPNOTSUPP;
554 goto out_unlock;
555 }
556 slot = bitmap_find_free_region(priv->used_rxkeys,
557 priv->rx_keycache_size, 0);
558
559 if (slot < 0) {
560 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300561 * The device supports the chosen algorithm, but the
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500562 * firmware does not provide enough key slots to store
563 * all of them.
564 * But encryption offload for outgoing frames is always
565 * possible, so we just pretend that the upload was
566 * successful and do the decryption in software.
567 */
568
569 /* mark the key as invalid. */
570 key->hw_key_idx = 0xff;
571 goto out_unlock;
572 }
Ido Yarivdb128472015-01-06 08:39:02 -0500573
574 key->flags |= IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500575 } else {
576 slot = key->hw_key_idx;
577
578 if (slot == 0xff) {
579 /* This key was not uploaded into the rx key cache. */
580
581 goto out_unlock;
582 }
583
584 bitmap_release_region(priv->used_rxkeys, slot, 0);
585 algo = 0;
586 }
587
588 if (sta)
589 addr = sta->addr;
590
591 ret = p54_upload_key(priv, algo, slot, key->keyidx,
592 key->keylen, addr, key->key);
593 if (ret) {
594 bitmap_release_region(priv->used_rxkeys, slot, 0);
595 ret = -EOPNOTSUPP;
596 goto out_unlock;
597 }
598
599 key->hw_key_idx = slot;
600
601out_unlock:
602 mutex_unlock(&priv->conf_mutex);
603 return ret;
604}
605
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400606static int p54_get_survey(struct ieee80211_hw *dev, int idx,
607 struct survey_info *survey)
608{
609 struct p54_common *priv = dev->priv;
Christian Lamparter0d781562011-08-20 01:53:59 +0200610 struct ieee80211_channel *chan;
611 int err, tries;
612 bool in_use = false;
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400613
Christian Lamparter0d781562011-08-20 01:53:59 +0200614 if (idx >= priv->chan_num)
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400615 return -ENOENT;
616
Christian Lamparter0d781562011-08-20 01:53:59 +0200617#define MAX_TRIES 1
618 for (tries = 0; tries < MAX_TRIES; tries++) {
619 chan = priv->curchan;
620 if (chan && chan->hw_value == idx) {
621 mutex_lock(&priv->conf_mutex);
622 err = p54_wait_for_stats(dev);
623 mutex_unlock(&priv->conf_mutex);
624 if (err)
625 return err;
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400626
Christian Lamparter0d781562011-08-20 01:53:59 +0200627 in_use = true;
628 }
629
630 memcpy(survey, &priv->survey[idx], sizeof(*survey));
631
632 if (in_use) {
633 /* test if the reported statistics are valid. */
Johannes Berg4ed20be2014-11-14 16:35:34 +0100634 if (survey->time != 0) {
Christian Lamparter0d781562011-08-20 01:53:59 +0200635 survey->filled |= SURVEY_INFO_IN_USE;
636 } else {
637 /*
638 * hw/fw has not accumulated enough sample sets.
639 * Wait for 100ms, this ought to be enough to
640 * to get at least one non-null set of channel
641 * usage statistics.
642 */
643 msleep(100);
644 continue;
645 }
646 }
647 return 0;
648 }
649 return -ETIMEDOUT;
650#undef MAX_TRIES
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400651}
652
Christian Lamparter0d4171e2011-02-16 19:43:06 +0100653static unsigned int p54_flush_count(struct p54_common *priv)
654{
655 unsigned int total = 0, i;
656
657 BUILD_BUG_ON(P54_QUEUE_NUM > ARRAY_SIZE(priv->tx_stats));
658
659 /*
660 * Because the firmware has the sole control over any frames
661 * in the P54_QUEUE_BEACON or P54_QUEUE_SCAN queues, they
662 * don't really count as pending or active.
663 */
664 for (i = P54_QUEUE_MGMT; i < P54_QUEUE_NUM; i++)
665 total += priv->tx_stats[i].len;
666 return total;
667}
668
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +0200669static void p54_flush(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
670 u32 queues, bool drop)
Christian Lamparter0d4171e2011-02-16 19:43:06 +0100671{
672 struct p54_common *priv = dev->priv;
673 unsigned int total, i;
674
675 /*
676 * Currently, it wouldn't really matter if we wait for one second
677 * or 15 minutes. But once someone gets around and completes the
678 * TODOs [ancel stuck frames / reset device] in p54_work, it will
679 * suddenly make sense to wait that long.
680 */
681 i = P54_STATISTICS_UPDATE * 2 / 20;
682
683 /*
684 * In this case no locking is required because as we speak the
685 * queues have already been stopped and no new frames can sneak
686 * up from behind.
687 */
688 while ((total = p54_flush_count(priv) && i--)) {
689 /* waste time */
690 msleep(20);
691 }
692
693 WARN(total, "tx flush timeout, unresponsive firmware");
694}
695
Lorenzo Bianconia4bcaf52014-09-04 23:57:41 +0200696static void p54_set_coverage_class(struct ieee80211_hw *dev,
697 s16 coverage_class)
Christian Lamparter3083e832011-02-24 14:12:20 +0100698{
699 struct p54_common *priv = dev->priv;
700
701 mutex_lock(&priv->conf_mutex);
702 /* support all coverage class values as in 802.11-2007 Table 7-27 */
703 priv->coverage_class = clamp_t(u8, coverage_class, 0, 31);
704 p54_set_edcf(priv);
705 mutex_unlock(&priv->conf_mutex);
706}
707
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500708static const struct ieee80211_ops p54_ops = {
709 .tx = p54_tx_80211,
710 .start = p54_start,
711 .stop = p54_stop,
712 .add_interface = p54_add_interface,
713 .remove_interface = p54_remove_interface,
714 .set_tim = p54_set_tim,
715 .sta_notify = p54_sta_notify,
Johannes Berg17f6f052010-02-19 19:06:54 +0100716 .sta_add = p54_sta_add_remove,
717 .sta_remove = p54_sta_add_remove,
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500718 .set_key = p54_set_key,
719 .config = p54_config,
Christian Lamparter0d4171e2011-02-16 19:43:06 +0100720 .flush = p54_flush,
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500721 .bss_info_changed = p54_bss_info_changed,
Christian Lamparterbe8d98e2011-04-24 17:22:59 +0200722 .prepare_multicast = p54_prepare_multicast,
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500723 .configure_filter = p54_configure_filter,
724 .conf_tx = p54_conf_tx,
725 .get_stats = p54_get_stats,
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400726 .get_survey = p54_get_survey,
Christian Lamparter3083e832011-02-24 14:12:20 +0100727 .set_coverage_class = p54_set_coverage_class,
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500728};
729
730struct ieee80211_hw *p54_init_common(size_t priv_data_len)
731{
732 struct ieee80211_hw *dev;
733 struct p54_common *priv;
734
735 dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
736 if (!dev)
737 return NULL;
738
739 priv = dev->priv;
740 priv->hw = dev;
741 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
742 priv->basic_rate_mask = 0x15f;
743 spin_lock_init(&priv->tx_stats_lock);
744 skb_queue_head_init(&priv->tx_queue);
745 skb_queue_head_init(&priv->tx_pending);
Johannes Berg30686bf2015-06-02 21:39:54 +0200746 ieee80211_hw_set(dev, REPORTS_TX_ACK_STATUS);
747 ieee80211_hw_set(dev, MFP_CAPABLE);
748 ieee80211_hw_set(dev, PS_NULLFUNC_STACK);
749 ieee80211_hw_set(dev, SUPPORTS_PS);
750 ieee80211_hw_set(dev, RX_INCLUDES_FCS);
751 ieee80211_hw_set(dev, SIGNAL_DBM);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500752
753 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
754 BIT(NL80211_IFTYPE_ADHOC) |
755 BIT(NL80211_IFTYPE_AP) |
756 BIT(NL80211_IFTYPE_MESH_POINT);
757
Christian Lamparter46df10a2009-07-16 20:03:47 +0200758 priv->beacon_req_id = cpu_to_le32(0);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500759 priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
760 priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
761 priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
762 priv->tx_stats[P54_QUEUE_CAB].limit = 3;
763 priv->tx_stats[P54_QUEUE_DATA].limit = 5;
764 dev->queues = 1;
765 priv->noise = -94;
766 /*
767 * We support at most 8 tries no matter which rate they're at,
768 * we cannot support max_rates * max_rate_tries as we set it
769 * here, but setting it correctly to 4/2 or so would limit us
770 * artificially if the RC algorithm wants just two rates, so
771 * let's say 4/7, we'll redistribute it at TX time, see the
772 * comments there.
773 */
774 dev->max_rates = 4;
775 dev->max_rate_tries = 7;
776 dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
777 sizeof(struct p54_tx_data);
778
Christian Lamparterc46aaba2009-08-14 13:23:05 +0200779 /*
780 * For now, disable PS by default because it affects
781 * link stability significantly.
782 */
Johannes Berg5be83de2009-11-19 00:56:28 +0100783 dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
Christian Lamparterc46aaba2009-08-14 13:23:05 +0200784
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500785 mutex_init(&priv->conf_mutex);
786 mutex_init(&priv->eeprom_mutex);
Christian Lamparter0d781562011-08-20 01:53:59 +0200787 init_completion(&priv->stat_comp);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500788 init_completion(&priv->eeprom_comp);
Christian Lamparter46df10a2009-07-16 20:03:47 +0200789 init_completion(&priv->beacon_comp);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500790 INIT_DELAYED_WORK(&priv->work, p54_work);
791
Joe Perches93803b32015-03-02 19:54:49 -0800792 eth_broadcast_addr(priv->mc_maclist[0]);
Christian Lamparter0d781562011-08-20 01:53:59 +0200793 priv->curchan = NULL;
794 p54_reset_stats(priv);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500795 return dev;
796}
797EXPORT_SYMBOL_GPL(p54_init_common);
798
799int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
800{
Larry Fingerfe0b7c62011-02-15 09:37:06 -0600801 struct p54_common __maybe_unused *priv = dev->priv;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500802 int err;
803
804 err = ieee80211_register_hw(dev);
805 if (err) {
806 dev_err(pdev, "Cannot register device (%d).\n", err);
807 return err;
808 }
Christian Lampartera9b93612012-03-17 14:10:02 +0100809 priv->registered = true;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500810
811#ifdef CONFIG_P54_LEDS
812 err = p54_init_leds(priv);
Christian Lampartera9b93612012-03-17 14:10:02 +0100813 if (err) {
814 p54_unregister_common(dev);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500815 return err;
Christian Lampartera9b93612012-03-17 14:10:02 +0100816 }
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500817#endif /* CONFIG_P54_LEDS */
818
819 dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
820 return 0;
821}
822EXPORT_SYMBOL_GPL(p54_register_common);
823
824void p54_free_common(struct ieee80211_hw *dev)
825{
826 struct p54_common *priv = dev->priv;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200827 unsigned int i;
828
Johannes Berg57fbcce2016-04-12 15:56:15 +0200829 for (i = 0; i < NUM_NL80211_BANDS; i++)
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200830 kfree(priv->band_table[i]);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500831
832 kfree(priv->iq_autocal);
833 kfree(priv->output_limit);
834 kfree(priv->curve_data);
Christian Lamparter7a047f42011-02-12 22:32:49 +0100835 kfree(priv->rssi_db);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500836 kfree(priv->used_rxkeys);
Christian Lamparter0d781562011-08-20 01:53:59 +0200837 kfree(priv->survey);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500838 priv->iq_autocal = NULL;
839 priv->output_limit = NULL;
840 priv->curve_data = NULL;
Christian Lamparter7a047f42011-02-12 22:32:49 +0100841 priv->rssi_db = NULL;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500842 priv->used_rxkeys = NULL;
Christian Lamparter0d781562011-08-20 01:53:59 +0200843 priv->survey = NULL;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500844 ieee80211_free_hw(dev);
845}
846EXPORT_SYMBOL_GPL(p54_free_common);
847
848void p54_unregister_common(struct ieee80211_hw *dev)
849{
850 struct p54_common *priv = dev->priv;
851
Christian Lampartera9b93612012-03-17 14:10:02 +0100852 if (priv->registered) {
853 priv->registered = false;
Andrey Konovalovfc097852017-09-26 17:11:33 +0200854#ifdef CONFIG_P54_LEDS
855 p54_unregister_leds(priv);
856#endif /* CONFIG_P54_LEDS */
Christian Lampartera9b93612012-03-17 14:10:02 +0100857 ieee80211_unregister_hw(dev);
858 }
859
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500860 mutex_destroy(&priv->conf_mutex);
861 mutex_destroy(&priv->eeprom_mutex);
862}
863EXPORT_SYMBOL_GPL(p54_unregister_common);