xref: /linux/drivers/net/wireless/intersil/p54/main.c (revision 9b50d9c06c275419ac36de8b5a5dd1ed6b522770)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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).
14  */
15 
16 #include <linux/slab.h>
17 #include <linux/firmware.h>
18 #include <linux/etherdevice.h>
19 #include <linux/module.h>
20 
21 #include <net/mac80211.h>
22 
23 #include "p54.h"
24 #include "lmac.h"
25 
26 static bool modparam_nohwcrypt;
27 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, 0444);
28 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
29 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
30 MODULE_DESCRIPTION("Softmac Prism54 common code");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS("prism54common");
33 
34 static 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 
50 static 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;
55 
56 	switch (notify_cmd) {
57 	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 
66 static 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 
74 u8 *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 
96 static 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 	 */
103 	u8 *tim;
104 	u8 dtim_len;
105 	u8 dtim_period;
106 	u8 *next;
107 
108 	tim = p54_find_ie(skb, WLAN_EID_TIM);
109 	if (!tim)
110 		return 0;
111 
112 	dtim_len = tim[1];
113 	dtim_period = tim[3];
114 	next = tim + 2 + dtim_len;
115 
116 	if (dtim_len < 3)
117 		return -EINVAL;
118 
119 	memmove(tim, next, skb_tail_pointer(skb) - next);
120 	tim = skb_tail_pointer(skb) - (dtim_len + 2);
121 
122 	/* 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;
128 
129 	if (dtim_len > 3)
130 		skb_trim(skb, skb->len - (dtim_len - 3));
131 
132 	return 0;
133 }
134 
135 static int p54_beacon_update(struct p54_common *priv,
136 			struct ieee80211_vif *vif)
137 {
138 	struct ieee80211_tx_control control = { };
139 	struct sk_buff *beacon;
140 	int ret;
141 
142 	beacon = ieee80211_beacon_get(priv->hw, vif, 0);
143 	if (!beacon)
144 		return -ENOMEM;
145 	ret = p54_beacon_format_ie_tim(beacon);
146 	if (ret) {
147 		dev_kfree_skb_any(beacon);
148 		return ret;
149 	}
150 
151 	/*
152 	 * During operation, the firmware takes care of beaconing.
153 	 * The driver only needs to upload a new beacon template, once
154 	 * the template was changed by the stack or userspace.
155 	 *
156 	 * LMAC API 3.2.2 also specifies that the driver does not need
157 	 * to cancel the old beacon template by hand, instead the firmware
158 	 * will release the previous one through the feedback mechanism.
159 	 */
160 	p54_tx_80211(priv->hw, &control, beacon);
161 	priv->tsf_high32 = 0;
162 	priv->tsf_low32 = 0;
163 
164 	return 0;
165 }
166 
167 static int p54_start(struct ieee80211_hw *dev)
168 {
169 	struct p54_common *priv = dev->priv;
170 	int err;
171 
172 	mutex_lock(&priv->conf_mutex);
173 	err = priv->open(dev);
174 	if (err)
175 		goto out;
176 	P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
177 	P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
178 	P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
179 	P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
180 	err = p54_set_edcf(priv);
181 	if (err)
182 		goto out;
183 
184 	eth_broadcast_addr(priv->bssid);
185 	priv->mode = NL80211_IFTYPE_MONITOR;
186 	err = p54_setup_mac(priv);
187 	if (err) {
188 		priv->mode = NL80211_IFTYPE_UNSPECIFIED;
189 		goto out;
190 	}
191 
192 	ieee80211_queue_delayed_work(dev, &priv->work, 0);
193 
194 	priv->softled_state = 0;
195 	err = p54_set_leds(priv);
196 
197 out:
198 	mutex_unlock(&priv->conf_mutex);
199 	return err;
200 }
201 
202 static void p54_stop(struct ieee80211_hw *dev, bool suspend)
203 {
204 	struct p54_common *priv = dev->priv;
205 	int i;
206 
207 	priv->mode = NL80211_IFTYPE_UNSPECIFIED;
208 	priv->softled_state = 0;
209 	cancel_delayed_work_sync(&priv->work);
210 	mutex_lock(&priv->conf_mutex);
211 	p54_set_leds(priv);
212 	priv->stop(dev);
213 	skb_queue_purge(&priv->tx_pending);
214 	skb_queue_purge(&priv->tx_queue);
215 	for (i = 0; i < P54_QUEUE_NUM; i++) {
216 		priv->tx_stats[i].count = 0;
217 		priv->tx_stats[i].len = 0;
218 	}
219 
220 	priv->beacon_req_id = cpu_to_le32(0);
221 	priv->tsf_high32 = priv->tsf_low32 = 0;
222 	mutex_unlock(&priv->conf_mutex);
223 }
224 
225 static int p54_add_interface(struct ieee80211_hw *dev,
226 			     struct ieee80211_vif *vif)
227 {
228 	struct p54_common *priv = dev->priv;
229 	int err;
230 
231 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
232 
233 	mutex_lock(&priv->conf_mutex);
234 	if (priv->mode != NL80211_IFTYPE_MONITOR) {
235 		mutex_unlock(&priv->conf_mutex);
236 		return -EOPNOTSUPP;
237 	}
238 
239 	priv->vif = vif;
240 
241 	switch (vif->type) {
242 	case NL80211_IFTYPE_STATION:
243 	case NL80211_IFTYPE_ADHOC:
244 	case NL80211_IFTYPE_AP:
245 	case NL80211_IFTYPE_MESH_POINT:
246 		priv->mode = vif->type;
247 		break;
248 	default:
249 		mutex_unlock(&priv->conf_mutex);
250 		return -EOPNOTSUPP;
251 	}
252 
253 	memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
254 	err = p54_setup_mac(priv);
255 	mutex_unlock(&priv->conf_mutex);
256 	return err;
257 }
258 
259 static void p54_remove_interface(struct ieee80211_hw *dev,
260 				 struct ieee80211_vif *vif)
261 {
262 	struct p54_common *priv = dev->priv;
263 
264 	mutex_lock(&priv->conf_mutex);
265 	priv->vif = NULL;
266 
267 	/*
268 	 * LMAC API 3.2.2 states that any active beacon template must be
269 	 * canceled by the driver before attempting a mode transition.
270 	 */
271 	if (le32_to_cpu(priv->beacon_req_id) != 0) {
272 		p54_tx_cancel(priv, priv->beacon_req_id);
273 		wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
274 	}
275 	priv->mode = NL80211_IFTYPE_MONITOR;
276 	eth_zero_addr(priv->mac_addr);
277 	eth_zero_addr(priv->bssid);
278 	p54_setup_mac(priv);
279 	mutex_unlock(&priv->conf_mutex);
280 }
281 
282 static int p54_wait_for_stats(struct ieee80211_hw *dev)
283 {
284 	struct p54_common *priv = dev->priv;
285 	int ret;
286 
287 	priv->update_stats = true;
288 	ret = p54_fetch_statistics(priv);
289 	if (ret)
290 		return ret;
291 
292 	ret = wait_for_completion_interruptible_timeout(&priv->stat_comp, HZ);
293 	if (ret == 0)
294 		return -ETIMEDOUT;
295 
296 	return 0;
297 }
298 
299 static void p54_reset_stats(struct p54_common *priv)
300 {
301 	struct ieee80211_channel *chan = priv->curchan;
302 
303 	if (chan) {
304 		struct survey_info *info = &priv->survey[chan->hw_value];
305 
306 		/* only reset channel statistics, don't touch .filled, etc. */
307 		info->time = 0;
308 		info->time_busy = 0;
309 		info->time_tx = 0;
310 	}
311 
312 	priv->update_stats = true;
313 	priv->survey_raw.active = 0;
314 	priv->survey_raw.cca = 0;
315 	priv->survey_raw.tx = 0;
316 }
317 
318 static int p54_config(struct ieee80211_hw *dev, int radio_idx, u32 changed)
319 {
320 	int ret = 0;
321 	struct p54_common *priv = dev->priv;
322 	struct ieee80211_conf *conf = &dev->conf;
323 
324 	mutex_lock(&priv->conf_mutex);
325 	if (changed & IEEE80211_CONF_CHANGE_POWER)
326 		priv->output_power = conf->power_level << 2;
327 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
328 		struct ieee80211_channel *oldchan;
329 		WARN_ON(p54_wait_for_stats(dev));
330 		oldchan = priv->curchan;
331 		priv->curchan = NULL;
332 		ret = p54_scan(priv, P54_SCAN_EXIT, 0);
333 		if (ret) {
334 			priv->curchan = oldchan;
335 			goto out;
336 		}
337 		/*
338 		 * TODO: Use the LM_SCAN_TRAP to determine the current
339 		 * operating channel.
340 		 */
341 		priv->curchan = priv->hw->conf.chandef.chan;
342 		p54_reset_stats(priv);
343 		WARN_ON(p54_fetch_statistics(priv));
344 	}
345 	if (changed & IEEE80211_CONF_CHANGE_PS) {
346 		WARN_ON(p54_wait_for_stats(dev));
347 		ret = p54_set_ps(priv);
348 		if (ret)
349 			goto out;
350 		WARN_ON(p54_wait_for_stats(dev));
351 	}
352 	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
353 		WARN_ON(p54_wait_for_stats(dev));
354 		ret = p54_setup_mac(priv);
355 		if (ret)
356 			goto out;
357 		WARN_ON(p54_wait_for_stats(dev));
358 	}
359 
360 out:
361 	mutex_unlock(&priv->conf_mutex);
362 	return ret;
363 }
364 
365 static u64 p54_prepare_multicast(struct ieee80211_hw *dev,
366 				 struct netdev_hw_addr_list *mc_list)
367 {
368 	struct p54_common *priv = dev->priv;
369 	struct netdev_hw_addr *ha;
370 	int i;
371 
372 	BUILD_BUG_ON(ARRAY_SIZE(priv->mc_maclist) !=
373 		ARRAY_SIZE(((struct p54_group_address_table *)NULL)->mac_list));
374 	/*
375 	 * The first entry is reserved for the global broadcast MAC.
376 	 * Otherwise the firmware will drop it and ARP will no longer work.
377 	 */
378 	i = 1;
379 	priv->mc_maclist_num = netdev_hw_addr_list_count(mc_list) + i;
380 	netdev_hw_addr_list_for_each(ha, mc_list) {
381 		memcpy(&priv->mc_maclist[i], ha->addr, ETH_ALEN);
382 		i++;
383 		if (i >= ARRAY_SIZE(priv->mc_maclist))
384 			break;
385 	}
386 
387 	return 1; /* update */
388 }
389 
390 static void p54_configure_filter(struct ieee80211_hw *dev,
391 				 unsigned int changed_flags,
392 				 unsigned int *total_flags,
393 				 u64 multicast)
394 {
395 	struct p54_common *priv = dev->priv;
396 
397 	*total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS;
398 
399 	priv->filter_flags = *total_flags;
400 
401 	if (changed_flags & FIF_OTHER_BSS)
402 		p54_setup_mac(priv);
403 
404 	if (changed_flags & FIF_ALLMULTI || multicast)
405 		p54_set_groupfilter(priv);
406 }
407 
408 static int p54_conf_tx(struct ieee80211_hw *dev,
409 		       struct ieee80211_vif *vif,
410 		       unsigned int link_id, u16 queue,
411 		       const struct ieee80211_tx_queue_params *params)
412 {
413 	struct p54_common *priv = dev->priv;
414 	int ret;
415 
416 	mutex_lock(&priv->conf_mutex);
417 	P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
418 		      params->cw_min, params->cw_max, params->txop);
419 	ret = p54_set_edcf(priv);
420 	mutex_unlock(&priv->conf_mutex);
421 	return ret;
422 }
423 
424 static 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 
438 	mutex_lock(&priv->conf_mutex);
439 	WARN_ON_ONCE(p54_fetch_statistics(priv));
440 	mutex_unlock(&priv->conf_mutex);
441 }
442 
443 static 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 
452 static void p54_bss_info_changed(struct ieee80211_hw *dev,
453 				 struct ieee80211_vif *vif,
454 				 struct ieee80211_bss_conf *info,
455 				 u64 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) {
477 		if (dev->conf.chandef.chan->band == NL80211_BAND_5GHZ)
478 			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 (vif->cfg.assoc) {
487 			priv->aid = vif->cfg.aid;
488 			priv->wakeup_timer = info->beacon_int *
489 					     info->dtim_period * 5;
490 			p54_setup_mac(priv);
491 		} else {
492 			priv->wakeup_timer = 500;
493 			priv->aid = 0;
494 		}
495 	}
496 
497 	mutex_unlock(&priv->conf_mutex);
498 }
499 
500 static 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 
512 	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 
523 	mutex_lock(&priv->conf_mutex);
524 	if (cmd == SET_KEY) {
525 		switch (key->cipher) {
526 		case WLAN_CIPHER_SUITE_TKIP:
527 			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;
535 		case WLAN_CIPHER_SUITE_WEP40:
536 		case WLAN_CIPHER_SUITE_WEP104:
537 			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;
544 		case WLAN_CIPHER_SUITE_CCMP:
545 			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 			/*
561 			 * The device supports the chosen algorithm, but the
562 			 * 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 		}
573 
574 		key->flags |= IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
575 	} 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 
601 out_unlock:
602 	mutex_unlock(&priv->conf_mutex);
603 	return ret;
604 }
605 
606 static int p54_get_survey(struct ieee80211_hw *dev, int idx,
607 				struct survey_info *survey)
608 {
609 	struct p54_common *priv = dev->priv;
610 	struct ieee80211_channel *chan;
611 	int err, tries;
612 	bool in_use = false;
613 
614 	if (idx >= priv->chan_num)
615 		return -ENOENT;
616 
617 #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;
626 
627 			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. */
634 			if  (survey->time != 0) {
635 				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 				 * 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
651 }
652 
653 static 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 
669 static void p54_flush(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
670 		      u32 queues, bool drop)
671 {
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 
696 static void p54_set_coverage_class(struct ieee80211_hw *dev,
697 				   int radio_idx,
698 				   s16 coverage_class)
699 {
700 	struct p54_common *priv = dev->priv;
701 
702 	mutex_lock(&priv->conf_mutex);
703 	/* support all coverage class values as in 802.11-2007 Table 7-27 */
704 	priv->coverage_class = clamp_t(u8, coverage_class, 0, 31);
705 	p54_set_edcf(priv);
706 	mutex_unlock(&priv->conf_mutex);
707 }
708 
709 static const struct ieee80211_ops p54_ops = {
710 	.add_chanctx = ieee80211_emulate_add_chanctx,
711 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
712 	.change_chanctx = ieee80211_emulate_change_chanctx,
713 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
714 	.tx			= p54_tx_80211,
715 	.wake_tx_queue		= ieee80211_handle_wake_tx_queue,
716 	.start			= p54_start,
717 	.stop			= p54_stop,
718 	.add_interface		= p54_add_interface,
719 	.remove_interface	= p54_remove_interface,
720 	.set_tim		= p54_set_tim,
721 	.sta_notify		= p54_sta_notify,
722 	.sta_add		= p54_sta_add_remove,
723 	.sta_remove		= p54_sta_add_remove,
724 	.set_key		= p54_set_key,
725 	.config			= p54_config,
726 	.flush			= p54_flush,
727 	.bss_info_changed	= p54_bss_info_changed,
728 	.prepare_multicast	= p54_prepare_multicast,
729 	.configure_filter	= p54_configure_filter,
730 	.conf_tx		= p54_conf_tx,
731 	.get_stats		= p54_get_stats,
732 	.get_survey		= p54_get_survey,
733 	.set_coverage_class	= p54_set_coverage_class,
734 };
735 
736 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
737 {
738 	struct ieee80211_hw *dev;
739 	struct p54_common *priv;
740 
741 	dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
742 	if (!dev)
743 		return NULL;
744 
745 	priv = dev->priv;
746 	priv->hw = dev;
747 	priv->mode = NL80211_IFTYPE_UNSPECIFIED;
748 	priv->basic_rate_mask = 0x15f;
749 	spin_lock_init(&priv->tx_stats_lock);
750 	skb_queue_head_init(&priv->tx_queue);
751 	skb_queue_head_init(&priv->tx_pending);
752 	ieee80211_hw_set(dev, REPORTS_TX_ACK_STATUS);
753 	ieee80211_hw_set(dev, MFP_CAPABLE);
754 	ieee80211_hw_set(dev, PS_NULLFUNC_STACK);
755 	ieee80211_hw_set(dev, SUPPORTS_PS);
756 	ieee80211_hw_set(dev, RX_INCLUDES_FCS);
757 	ieee80211_hw_set(dev, SIGNAL_DBM);
758 
759 	dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
760 				      BIT(NL80211_IFTYPE_ADHOC) |
761 				      BIT(NL80211_IFTYPE_AP) |
762 				      BIT(NL80211_IFTYPE_MESH_POINT);
763 
764 	priv->beacon_req_id = cpu_to_le32(0);
765 	priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
766 	priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
767 	priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
768 	priv->tx_stats[P54_QUEUE_CAB].limit = 3;
769 	priv->tx_stats[P54_QUEUE_DATA].limit = 5;
770 	dev->queues = 1;
771 	priv->noise = -94;
772 	/*
773 	 * We support at most 8 tries no matter which rate they're at,
774 	 * we cannot support max_rates * max_rate_tries as we set it
775 	 * here, but setting it correctly to 4/2 or so would limit us
776 	 * artificially if the RC algorithm wants just two rates, so
777 	 * let's say 4/7, we'll redistribute it at TX time, see the
778 	 * comments there.
779 	 */
780 	dev->max_rates = 4;
781 	dev->max_rate_tries = 7;
782 	dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
783 				 sizeof(struct p54_tx_data);
784 
785 	/*
786 	 * For now, disable PS by default because it affects
787 	 * link stability significantly.
788 	 */
789 	dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
790 
791 	mutex_init(&priv->conf_mutex);
792 	mutex_init(&priv->eeprom_mutex);
793 	init_completion(&priv->stat_comp);
794 	init_completion(&priv->eeprom_comp);
795 	init_completion(&priv->beacon_comp);
796 	INIT_DELAYED_WORK(&priv->work, p54_work);
797 
798 	eth_broadcast_addr(priv->mc_maclist[0]);
799 	priv->curchan = NULL;
800 	p54_reset_stats(priv);
801 	return dev;
802 }
803 EXPORT_SYMBOL_GPL(p54_init_common);
804 
805 int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
806 {
807 	struct p54_common __maybe_unused *priv = dev->priv;
808 	int err;
809 
810 	err = ieee80211_register_hw(dev);
811 	if (err) {
812 		dev_err(pdev, "Cannot register device (%d).\n", err);
813 		return err;
814 	}
815 	priv->registered = true;
816 
817 #ifdef CONFIG_P54_LEDS
818 	err = p54_init_leds(priv);
819 	if (err) {
820 		p54_unregister_common(dev);
821 		return err;
822 	}
823 #endif /* CONFIG_P54_LEDS */
824 
825 	dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
826 	return 0;
827 }
828 EXPORT_SYMBOL_GPL(p54_register_common);
829 
830 void p54_free_common(struct ieee80211_hw *dev)
831 {
832 	struct p54_common *priv = dev->priv;
833 	unsigned int i;
834 
835 	for (i = 0; i < NUM_NL80211_BANDS; i++)
836 		kfree(priv->band_table[i]);
837 
838 	kfree(priv->iq_autocal);
839 	kfree(priv->output_limit);
840 	kfree(priv->curve_data);
841 	kfree(priv->rssi_db);
842 	bitmap_free(priv->used_rxkeys);
843 	kfree(priv->survey);
844 	priv->iq_autocal = NULL;
845 	priv->output_limit = NULL;
846 	priv->curve_data = NULL;
847 	priv->rssi_db = NULL;
848 	priv->used_rxkeys = NULL;
849 	priv->survey = NULL;
850 	ieee80211_free_hw(dev);
851 }
852 EXPORT_SYMBOL_GPL(p54_free_common);
853 
854 void p54_unregister_common(struct ieee80211_hw *dev)
855 {
856 	struct p54_common *priv = dev->priv;
857 
858 	if (priv->registered) {
859 		priv->registered = false;
860 #ifdef CONFIG_P54_LEDS
861 		p54_unregister_leds(priv);
862 #endif /* CONFIG_P54_LEDS */
863 		ieee80211_unregister_hw(dev);
864 	}
865 
866 	mutex_destroy(&priv->conf_mutex);
867 	mutex_destroy(&priv->eeprom_mutex);
868 }
869 EXPORT_SYMBOL_GPL(p54_unregister_common);
870