• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 	Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
4 	Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
5 	<http://rt2x00.serialmonkey.com>
6 
7  */
8 
9 /*
10 	Module: rt2x00lib
11 	Abstract: rt2x00 generic device routines.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/log2.h>
18 #include <linux/of.h>
19 #include <linux/of_net.h>
20 
21 #include "rt2x00.h"
22 #include "rt2x00lib.h"
23 
24 /*
25  * Utility functions.
26  */
rt2x00lib_get_bssidx(struct rt2x00_dev * rt2x00dev,struct ieee80211_vif * vif)27 u32 rt2x00lib_get_bssidx(struct rt2x00_dev *rt2x00dev,
28 			 struct ieee80211_vif *vif)
29 {
30 	/*
31 	 * When in STA mode, bssidx is always 0 otherwise local_address[5]
32 	 * contains the bss number, see BSS_ID_MASK comments for details.
33 	 */
34 	if (rt2x00dev->intf_sta_count)
35 		return 0;
36 	return vif->addr[5] & (rt2x00dev->ops->max_ap_intf - 1);
37 }
38 EXPORT_SYMBOL_GPL(rt2x00lib_get_bssidx);
39 
40 /*
41  * Radio control handlers.
42  */
rt2x00lib_enable_radio(struct rt2x00_dev * rt2x00dev)43 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
44 {
45 	int status;
46 
47 	/*
48 	 * Don't enable the radio twice.
49 	 * And check if the hardware button has been disabled.
50 	 */
51 	if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
52 		return 0;
53 
54 	/*
55 	 * Initialize all data queues.
56 	 */
57 	rt2x00queue_init_queues(rt2x00dev);
58 
59 	/*
60 	 * Enable radio.
61 	 */
62 	status =
63 	    rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
64 	if (status)
65 		return status;
66 
67 	rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
68 
69 	rt2x00leds_led_radio(rt2x00dev, true);
70 	rt2x00led_led_activity(rt2x00dev, true);
71 
72 	set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
73 
74 	/*
75 	 * Enable queues.
76 	 */
77 	rt2x00queue_start_queues(rt2x00dev);
78 	rt2x00link_start_tuner(rt2x00dev);
79 
80 	/*
81 	 * Start watchdog monitoring.
82 	 */
83 	rt2x00link_start_watchdog(rt2x00dev);
84 
85 	return 0;
86 }
87 
rt2x00lib_disable_radio(struct rt2x00_dev * rt2x00dev)88 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
89 {
90 	if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
91 		return;
92 
93 	/*
94 	 * Stop watchdog monitoring.
95 	 */
96 	rt2x00link_stop_watchdog(rt2x00dev);
97 
98 	/*
99 	 * Stop all queues
100 	 */
101 	rt2x00link_stop_tuner(rt2x00dev);
102 	rt2x00queue_stop_queues(rt2x00dev);
103 	rt2x00queue_flush_queues(rt2x00dev, true);
104 	rt2x00queue_stop_queue(rt2x00dev->bcn);
105 
106 	/*
107 	 * Disable radio.
108 	 */
109 	rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
110 	rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
111 	rt2x00led_led_activity(rt2x00dev, false);
112 	rt2x00leds_led_radio(rt2x00dev, false);
113 }
114 
rt2x00lib_intf_scheduled_iter(void * data,u8 * mac,struct ieee80211_vif * vif)115 static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
116 					  struct ieee80211_vif *vif)
117 {
118 	struct rt2x00_dev *rt2x00dev = data;
119 	struct rt2x00_intf *intf = vif_to_intf(vif);
120 
121 	/*
122 	 * It is possible the radio was disabled while the work had been
123 	 * scheduled. If that happens we should return here immediately,
124 	 * note that in the spinlock protected area above the delayed_flags
125 	 * have been cleared correctly.
126 	 */
127 	if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
128 		return;
129 
130 	if (test_and_clear_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags)) {
131 		mutex_lock(&intf->beacon_skb_mutex);
132 		rt2x00queue_update_beacon(rt2x00dev, vif);
133 		mutex_unlock(&intf->beacon_skb_mutex);
134 	}
135 }
136 
rt2x00lib_intf_scheduled(struct work_struct * work)137 static void rt2x00lib_intf_scheduled(struct work_struct *work)
138 {
139 	struct rt2x00_dev *rt2x00dev =
140 	    container_of(work, struct rt2x00_dev, intf_work);
141 
142 	/*
143 	 * Iterate over each interface and perform the
144 	 * requested configurations.
145 	 */
146 	ieee80211_iterate_active_interfaces(rt2x00dev->hw,
147 					    IEEE80211_IFACE_ITER_RESUME_ALL,
148 					    rt2x00lib_intf_scheduled_iter,
149 					    rt2x00dev);
150 }
151 
rt2x00lib_autowakeup(struct work_struct * work)152 static void rt2x00lib_autowakeup(struct work_struct *work)
153 {
154 	struct rt2x00_dev *rt2x00dev =
155 	    container_of(work, struct rt2x00_dev, autowakeup_work.work);
156 
157 	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
158 		return;
159 
160 	if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
161 		rt2x00_err(rt2x00dev, "Device failed to wakeup\n");
162 	clear_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
163 }
164 
165 /*
166  * Interrupt context handlers.
167  */
rt2x00lib_bc_buffer_iter(void * data,u8 * mac,struct ieee80211_vif * vif)168 static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
169 				     struct ieee80211_vif *vif)
170 {
171 	struct ieee80211_tx_control control = {};
172 	struct rt2x00_dev *rt2x00dev = data;
173 	struct sk_buff *skb;
174 
175 	/*
176 	 * Only AP mode interfaces do broad- and multicast buffering
177 	 */
178 	if (vif->type != NL80211_IFTYPE_AP)
179 		return;
180 
181 	/*
182 	 * Send out buffered broad- and multicast frames
183 	 */
184 	skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
185 	while (skb) {
186 		rt2x00mac_tx(rt2x00dev->hw, &control, skb);
187 		skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
188 	}
189 }
190 
rt2x00lib_beaconupdate_iter(void * data,u8 * mac,struct ieee80211_vif * vif)191 static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
192 					struct ieee80211_vif *vif)
193 {
194 	struct rt2x00_dev *rt2x00dev = data;
195 
196 	if (vif->type != NL80211_IFTYPE_AP &&
197 	    vif->type != NL80211_IFTYPE_ADHOC &&
198 	    vif->type != NL80211_IFTYPE_MESH_POINT &&
199 	    vif->type != NL80211_IFTYPE_WDS)
200 		return;
201 
202 	/*
203 	 * Update the beacon without locking. This is safe on PCI devices
204 	 * as they only update the beacon periodically here. This should
205 	 * never be called for USB devices.
206 	 */
207 	WARN_ON(rt2x00_is_usb(rt2x00dev));
208 	rt2x00queue_update_beacon(rt2x00dev, vif);
209 }
210 
rt2x00lib_beacondone(struct rt2x00_dev * rt2x00dev)211 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
212 {
213 	if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
214 		return;
215 
216 	/* send buffered bc/mc frames out for every bssid */
217 	ieee80211_iterate_active_interfaces_atomic(
218 		rt2x00dev->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
219 		rt2x00lib_bc_buffer_iter, rt2x00dev);
220 	/*
221 	 * Devices with pre tbtt interrupt don't need to update the beacon
222 	 * here as they will fetch the next beacon directly prior to
223 	 * transmission.
224 	 */
225 	if (rt2x00_has_cap_pre_tbtt_interrupt(rt2x00dev))
226 		return;
227 
228 	/* fetch next beacon */
229 	ieee80211_iterate_active_interfaces_atomic(
230 		rt2x00dev->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
231 		rt2x00lib_beaconupdate_iter, rt2x00dev);
232 }
233 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
234 
rt2x00lib_pretbtt(struct rt2x00_dev * rt2x00dev)235 void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
236 {
237 	if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
238 		return;
239 
240 	/* fetch next beacon */
241 	ieee80211_iterate_active_interfaces_atomic(
242 		rt2x00dev->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
243 		rt2x00lib_beaconupdate_iter, rt2x00dev);
244 }
245 EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
246 
rt2x00lib_dmastart(struct queue_entry * entry)247 void rt2x00lib_dmastart(struct queue_entry *entry)
248 {
249 	set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
250 	rt2x00queue_index_inc(entry, Q_INDEX);
251 }
252 EXPORT_SYMBOL_GPL(rt2x00lib_dmastart);
253 
rt2x00lib_dmadone(struct queue_entry * entry)254 void rt2x00lib_dmadone(struct queue_entry *entry)
255 {
256 	set_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags);
257 	clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
258 	rt2x00queue_index_inc(entry, Q_INDEX_DMA_DONE);
259 }
260 EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
261 
rt2x00lib_txdone_bar_status(struct queue_entry * entry)262 static inline int rt2x00lib_txdone_bar_status(struct queue_entry *entry)
263 {
264 	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
265 	struct ieee80211_bar *bar = (void *) entry->skb->data;
266 	struct rt2x00_bar_list_entry *bar_entry;
267 	int ret;
268 
269 	if (likely(!ieee80211_is_back_req(bar->frame_control)))
270 		return 0;
271 
272 	/*
273 	 * Unlike all other frames, the status report for BARs does
274 	 * not directly come from the hardware as it is incapable of
275 	 * matching a BA to a previously send BAR. The hardware will
276 	 * report all BARs as if they weren't acked at all.
277 	 *
278 	 * Instead the RX-path will scan for incoming BAs and set the
279 	 * block_acked flag if it sees one that was likely caused by
280 	 * a BAR from us.
281 	 *
282 	 * Remove remaining BARs here and return their status for
283 	 * TX done processing.
284 	 */
285 	ret = 0;
286 	rcu_read_lock();
287 	list_for_each_entry_rcu(bar_entry, &rt2x00dev->bar_list, list) {
288 		if (bar_entry->entry != entry)
289 			continue;
290 
291 		spin_lock_bh(&rt2x00dev->bar_list_lock);
292 		/* Return whether this BAR was blockacked or not */
293 		ret = bar_entry->block_acked;
294 		/* Remove the BAR from our checklist */
295 		list_del_rcu(&bar_entry->list);
296 		spin_unlock_bh(&rt2x00dev->bar_list_lock);
297 		kfree_rcu(bar_entry, head);
298 
299 		break;
300 	}
301 	rcu_read_unlock();
302 
303 	return ret;
304 }
305 
rt2x00lib_fill_tx_status(struct rt2x00_dev * rt2x00dev,struct ieee80211_tx_info * tx_info,struct skb_frame_desc * skbdesc,struct txdone_entry_desc * txdesc,bool success)306 static void rt2x00lib_fill_tx_status(struct rt2x00_dev *rt2x00dev,
307 				     struct ieee80211_tx_info *tx_info,
308 				     struct skb_frame_desc *skbdesc,
309 				     struct txdone_entry_desc *txdesc,
310 				     bool success)
311 {
312 	u8 rate_idx, rate_flags, retry_rates;
313 	int i;
314 
315 	rate_idx = skbdesc->tx_rate_idx;
316 	rate_flags = skbdesc->tx_rate_flags;
317 	retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
318 	    (txdesc->retry + 1) : 1;
319 
320 	/*
321 	 * Initialize TX status
322 	 */
323 	memset(&tx_info->status, 0, sizeof(tx_info->status));
324 	tx_info->status.ack_signal = 0;
325 
326 	/*
327 	 * Frame was send with retries, hardware tried
328 	 * different rates to send out the frame, at each
329 	 * retry it lowered the rate 1 step except when the
330 	 * lowest rate was used.
331 	 */
332 	for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
333 		tx_info->status.rates[i].idx = rate_idx - i;
334 		tx_info->status.rates[i].flags = rate_flags;
335 
336 		if (rate_idx - i == 0) {
337 			/*
338 			 * The lowest rate (index 0) was used until the
339 			 * number of max retries was reached.
340 			 */
341 			tx_info->status.rates[i].count = retry_rates - i;
342 			i++;
343 			break;
344 		}
345 		tx_info->status.rates[i].count = 1;
346 	}
347 	if (i < (IEEE80211_TX_MAX_RATES - 1))
348 		tx_info->status.rates[i].idx = -1; /* terminate */
349 
350 	if (test_bit(TXDONE_NO_ACK_REQ, &txdesc->flags))
351 		tx_info->flags |= IEEE80211_TX_CTL_NO_ACK;
352 
353 	if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
354 		if (success)
355 			tx_info->flags |= IEEE80211_TX_STAT_ACK;
356 		else
357 			rt2x00dev->low_level_stats.dot11ACKFailureCount++;
358 	}
359 
360 	/*
361 	 * Every single frame has it's own tx status, hence report
362 	 * every frame as ampdu of size 1.
363 	 *
364 	 * TODO: if we can find out how many frames were aggregated
365 	 * by the hw we could provide the real ampdu_len to mac80211
366 	 * which would allow the rc algorithm to better decide on
367 	 * which rates are suitable.
368 	 */
369 	if (test_bit(TXDONE_AMPDU, &txdesc->flags) ||
370 	    tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
371 		tx_info->flags |= IEEE80211_TX_STAT_AMPDU |
372 				  IEEE80211_TX_CTL_AMPDU;
373 		tx_info->status.ampdu_len = 1;
374 		tx_info->status.ampdu_ack_len = success ? 1 : 0;
375 	}
376 
377 	if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
378 		if (success)
379 			rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
380 		else
381 			rt2x00dev->low_level_stats.dot11RTSFailureCount++;
382 	}
383 }
384 
rt2x00lib_clear_entry(struct rt2x00_dev * rt2x00dev,struct queue_entry * entry)385 static void rt2x00lib_clear_entry(struct rt2x00_dev *rt2x00dev,
386 				  struct queue_entry *entry)
387 {
388 	/*
389 	 * Make this entry available for reuse.
390 	 */
391 	entry->skb = NULL;
392 	entry->flags = 0;
393 
394 	rt2x00dev->ops->lib->clear_entry(entry);
395 
396 	rt2x00queue_index_inc(entry, Q_INDEX_DONE);
397 
398 	/*
399 	 * If the data queue was below the threshold before the txdone
400 	 * handler we must make sure the packet queue in the mac80211 stack
401 	 * is reenabled when the txdone handler has finished. This has to be
402 	 * serialized with rt2x00mac_tx(), otherwise we can wake up queue
403 	 * before it was stopped.
404 	 */
405 	spin_lock_bh(&entry->queue->tx_lock);
406 	if (!rt2x00queue_threshold(entry->queue))
407 		rt2x00queue_unpause_queue(entry->queue);
408 	spin_unlock_bh(&entry->queue->tx_lock);
409 }
410 
rt2x00lib_txdone_nomatch(struct queue_entry * entry,struct txdone_entry_desc * txdesc)411 void rt2x00lib_txdone_nomatch(struct queue_entry *entry,
412 			      struct txdone_entry_desc *txdesc)
413 {
414 	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
415 	struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
416 	struct ieee80211_tx_info txinfo = {};
417 	bool success;
418 
419 	/*
420 	 * Unmap the skb.
421 	 */
422 	rt2x00queue_unmap_skb(entry);
423 
424 	/*
425 	 * Signal that the TX descriptor is no longer in the skb.
426 	 */
427 	skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
428 
429 	/*
430 	 * Send frame to debugfs immediately, after this call is completed
431 	 * we are going to overwrite the skb->cb array.
432 	 */
433 	rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry);
434 
435 	/*
436 	 * Determine if the frame has been successfully transmitted and
437 	 * remove BARs from our check list while checking for their
438 	 * TX status.
439 	 */
440 	success =
441 	    rt2x00lib_txdone_bar_status(entry) ||
442 	    test_bit(TXDONE_SUCCESS, &txdesc->flags);
443 
444 	if (!test_bit(TXDONE_UNKNOWN, &txdesc->flags)) {
445 		/*
446 		 * Update TX statistics.
447 		 */
448 		rt2x00dev->link.qual.tx_success += success;
449 		rt2x00dev->link.qual.tx_failed += !success;
450 
451 		rt2x00lib_fill_tx_status(rt2x00dev, &txinfo, skbdesc, txdesc,
452 					 success);
453 		ieee80211_tx_status_noskb(rt2x00dev->hw, skbdesc->sta, &txinfo);
454 	}
455 
456 	dev_kfree_skb_any(entry->skb);
457 	rt2x00lib_clear_entry(rt2x00dev, entry);
458 }
459 EXPORT_SYMBOL_GPL(rt2x00lib_txdone_nomatch);
460 
rt2x00lib_txdone(struct queue_entry * entry,struct txdone_entry_desc * txdesc)461 void rt2x00lib_txdone(struct queue_entry *entry,
462 		      struct txdone_entry_desc *txdesc)
463 {
464 	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
465 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
466 	struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
467 	u8 skbdesc_flags = skbdesc->flags;
468 	unsigned int header_length;
469 	bool success;
470 
471 	/*
472 	 * Unmap the skb.
473 	 */
474 	rt2x00queue_unmap_skb(entry);
475 
476 	/*
477 	 * Remove the extra tx headroom from the skb.
478 	 */
479 	skb_pull(entry->skb, rt2x00dev->extra_tx_headroom);
480 
481 	/*
482 	 * Signal that the TX descriptor is no longer in the skb.
483 	 */
484 	skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
485 
486 	/*
487 	 * Determine the length of 802.11 header.
488 	 */
489 	header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
490 
491 	/*
492 	 * Remove L2 padding which was added during
493 	 */
494 	if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_L2PAD))
495 		rt2x00queue_remove_l2pad(entry->skb, header_length);
496 
497 	/*
498 	 * If the IV/EIV data was stripped from the frame before it was
499 	 * passed to the hardware, we should now reinsert it again because
500 	 * mac80211 will expect the same data to be present it the
501 	 * frame as it was passed to us.
502 	 */
503 	if (rt2x00_has_cap_hw_crypto(rt2x00dev))
504 		rt2x00crypto_tx_insert_iv(entry->skb, header_length);
505 
506 	/*
507 	 * Send frame to debugfs immediately, after this call is completed
508 	 * we are going to overwrite the skb->cb array.
509 	 */
510 	rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry);
511 
512 	/*
513 	 * Determine if the frame has been successfully transmitted and
514 	 * remove BARs from our check list while checking for their
515 	 * TX status.
516 	 */
517 	success =
518 	    rt2x00lib_txdone_bar_status(entry) ||
519 	    test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
520 	    test_bit(TXDONE_UNKNOWN, &txdesc->flags);
521 
522 	/*
523 	 * Update TX statistics.
524 	 */
525 	rt2x00dev->link.qual.tx_success += success;
526 	rt2x00dev->link.qual.tx_failed += !success;
527 
528 	rt2x00lib_fill_tx_status(rt2x00dev, tx_info, skbdesc, txdesc, success);
529 
530 	/*
531 	 * Only send the status report to mac80211 when it's a frame
532 	 * that originated in mac80211. If this was a extra frame coming
533 	 * through a mac80211 library call (RTS/CTS) then we should not
534 	 * send the status report back.
535 	 */
536 	if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
537 		if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TASKLET_CONTEXT))
538 			ieee80211_tx_status(rt2x00dev->hw, entry->skb);
539 		else
540 			ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
541 	} else {
542 		dev_kfree_skb_any(entry->skb);
543 	}
544 
545 	rt2x00lib_clear_entry(rt2x00dev, entry);
546 }
547 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
548 
rt2x00lib_txdone_noinfo(struct queue_entry * entry,u32 status)549 void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
550 {
551 	struct txdone_entry_desc txdesc;
552 
553 	txdesc.flags = 0;
554 	__set_bit(status, &txdesc.flags);
555 	txdesc.retry = 0;
556 
557 	rt2x00lib_txdone(entry, &txdesc);
558 }
559 EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
560 
rt2x00lib_find_ie(u8 * data,unsigned int len,u8 ie)561 static u8 *rt2x00lib_find_ie(u8 *data, unsigned int len, u8 ie)
562 {
563 	struct ieee80211_mgmt *mgmt = (void *)data;
564 	u8 *pos, *end;
565 
566 	pos = (u8 *)mgmt->u.beacon.variable;
567 	end = data + len;
568 	while (pos < end) {
569 		if (pos + 2 + pos[1] > end)
570 			return NULL;
571 
572 		if (pos[0] == ie)
573 			return pos;
574 
575 		pos += 2 + pos[1];
576 	}
577 
578 	return NULL;
579 }
580 
rt2x00lib_sleep(struct work_struct * work)581 static void rt2x00lib_sleep(struct work_struct *work)
582 {
583 	struct rt2x00_dev *rt2x00dev =
584 	    container_of(work, struct rt2x00_dev, sleep_work);
585 
586 	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
587 		return;
588 
589 	/*
590 	 * Check again is powersaving is enabled, to prevent races from delayed
591 	 * work execution.
592 	 */
593 	if (!test_bit(CONFIG_POWERSAVING, &rt2x00dev->flags))
594 		rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf,
595 				 IEEE80211_CONF_CHANGE_PS);
596 }
597 
rt2x00lib_rxdone_check_ba(struct rt2x00_dev * rt2x00dev,struct sk_buff * skb,struct rxdone_entry_desc * rxdesc)598 static void rt2x00lib_rxdone_check_ba(struct rt2x00_dev *rt2x00dev,
599 				      struct sk_buff *skb,
600 				      struct rxdone_entry_desc *rxdesc)
601 {
602 	struct rt2x00_bar_list_entry *entry;
603 	struct ieee80211_bar *ba = (void *)skb->data;
604 
605 	if (likely(!ieee80211_is_back(ba->frame_control)))
606 		return;
607 
608 	if (rxdesc->size < sizeof(*ba) + FCS_LEN)
609 		return;
610 
611 	rcu_read_lock();
612 	list_for_each_entry_rcu(entry, &rt2x00dev->bar_list, list) {
613 
614 		if (ba->start_seq_num != entry->start_seq_num)
615 			continue;
616 
617 #define TID_CHECK(a, b) (						\
618 	((a) & cpu_to_le16(IEEE80211_BAR_CTRL_TID_INFO_MASK)) ==	\
619 	((b) & cpu_to_le16(IEEE80211_BAR_CTRL_TID_INFO_MASK)))		\
620 
621 		if (!TID_CHECK(ba->control, entry->control))
622 			continue;
623 
624 #undef TID_CHECK
625 
626 		if (!ether_addr_equal_64bits(ba->ra, entry->ta))
627 			continue;
628 
629 		if (!ether_addr_equal_64bits(ba->ta, entry->ra))
630 			continue;
631 
632 		/* Mark BAR since we received the according BA */
633 		spin_lock_bh(&rt2x00dev->bar_list_lock);
634 		entry->block_acked = 1;
635 		spin_unlock_bh(&rt2x00dev->bar_list_lock);
636 		break;
637 	}
638 	rcu_read_unlock();
639 
640 }
641 
rt2x00lib_rxdone_check_ps(struct rt2x00_dev * rt2x00dev,struct sk_buff * skb,struct rxdone_entry_desc * rxdesc)642 static void rt2x00lib_rxdone_check_ps(struct rt2x00_dev *rt2x00dev,
643 				      struct sk_buff *skb,
644 				      struct rxdone_entry_desc *rxdesc)
645 {
646 	struct ieee80211_hdr *hdr = (void *) skb->data;
647 	struct ieee80211_tim_ie *tim_ie;
648 	u8 *tim;
649 	u8 tim_len;
650 	bool cam;
651 
652 	/* If this is not a beacon, or if mac80211 has no powersaving
653 	 * configured, or if the device is already in powersaving mode
654 	 * we can exit now. */
655 	if (likely(!ieee80211_is_beacon(hdr->frame_control) ||
656 		   !(rt2x00dev->hw->conf.flags & IEEE80211_CONF_PS)))
657 		return;
658 
659 	/* min. beacon length + FCS_LEN */
660 	if (skb->len <= 40 + FCS_LEN)
661 		return;
662 
663 	/* and only beacons from the associated BSSID, please */
664 	if (!(rxdesc->dev_flags & RXDONE_MY_BSS) ||
665 	    !rt2x00dev->aid)
666 		return;
667 
668 	rt2x00dev->last_beacon = jiffies;
669 
670 	tim = rt2x00lib_find_ie(skb->data, skb->len - FCS_LEN, WLAN_EID_TIM);
671 	if (!tim)
672 		return;
673 
674 	if (tim[1] < sizeof(*tim_ie))
675 		return;
676 
677 	tim_len = tim[1];
678 	tim_ie = (struct ieee80211_tim_ie *) &tim[2];
679 
680 	/* Check whenever the PHY can be turned off again. */
681 
682 	/* 1. What about buffered unicast traffic for our AID? */
683 	cam = ieee80211_check_tim(tim_ie, tim_len, rt2x00dev->aid);
684 
685 	/* 2. Maybe the AP wants to send multicast/broadcast data? */
686 	cam |= (tim_ie->bitmap_ctrl & 0x01);
687 
688 	if (!cam && !test_bit(CONFIG_POWERSAVING, &rt2x00dev->flags))
689 		queue_work(rt2x00dev->workqueue, &rt2x00dev->sleep_work);
690 }
691 
rt2x00lib_rxdone_read_signal(struct rt2x00_dev * rt2x00dev,struct rxdone_entry_desc * rxdesc)692 static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
693 					struct rxdone_entry_desc *rxdesc)
694 {
695 	struct ieee80211_supported_band *sband;
696 	const struct rt2x00_rate *rate;
697 	unsigned int i;
698 	int signal = rxdesc->signal;
699 	int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
700 
701 	switch (rxdesc->rate_mode) {
702 	case RATE_MODE_CCK:
703 	case RATE_MODE_OFDM:
704 		/*
705 		 * For non-HT rates the MCS value needs to contain the
706 		 * actually used rate modulation (CCK or OFDM).
707 		 */
708 		if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
709 			signal = RATE_MCS(rxdesc->rate_mode, signal);
710 
711 		sband = &rt2x00dev->bands[rt2x00dev->curr_band];
712 		for (i = 0; i < sband->n_bitrates; i++) {
713 			rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
714 			if (((type == RXDONE_SIGNAL_PLCP) &&
715 			     (rate->plcp == signal)) ||
716 			    ((type == RXDONE_SIGNAL_BITRATE) &&
717 			      (rate->bitrate == signal)) ||
718 			    ((type == RXDONE_SIGNAL_MCS) &&
719 			      (rate->mcs == signal))) {
720 				return i;
721 			}
722 		}
723 		break;
724 	case RATE_MODE_HT_MIX:
725 	case RATE_MODE_HT_GREENFIELD:
726 		if (signal >= 0 && signal <= 76)
727 			return signal;
728 		break;
729 	default:
730 		break;
731 	}
732 
733 	rt2x00_warn(rt2x00dev, "Frame received with unrecognized signal, mode=0x%.4x, signal=0x%.4x, type=%d\n",
734 		    rxdesc->rate_mode, signal, type);
735 	return 0;
736 }
737 
rt2x00lib_rxdone(struct queue_entry * entry,gfp_t gfp)738 void rt2x00lib_rxdone(struct queue_entry *entry, gfp_t gfp)
739 {
740 	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
741 	struct rxdone_entry_desc rxdesc;
742 	struct sk_buff *skb;
743 	struct ieee80211_rx_status *rx_status;
744 	unsigned int header_length;
745 	int rate_idx;
746 
747 	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
748 	    !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
749 		goto submit_entry;
750 
751 	if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
752 		goto submit_entry;
753 
754 	/*
755 	 * Allocate a new sk_buffer. If no new buffer available, drop the
756 	 * received frame and reuse the existing buffer.
757 	 */
758 	skb = rt2x00queue_alloc_rxskb(entry, gfp);
759 	if (!skb)
760 		goto submit_entry;
761 
762 	/*
763 	 * Unmap the skb.
764 	 */
765 	rt2x00queue_unmap_skb(entry);
766 
767 	/*
768 	 * Extract the RXD details.
769 	 */
770 	memset(&rxdesc, 0, sizeof(rxdesc));
771 	rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
772 
773 	/*
774 	 * Check for valid size in case we get corrupted descriptor from
775 	 * hardware.
776 	 */
777 	if (unlikely(rxdesc.size == 0 ||
778 		     rxdesc.size > entry->queue->data_size)) {
779 		rt2x00_err(rt2x00dev, "Wrong frame size %d max %d\n",
780 			   rxdesc.size, entry->queue->data_size);
781 		dev_kfree_skb(entry->skb);
782 		goto renew_skb;
783 	}
784 
785 	/*
786 	 * The data behind the ieee80211 header must be
787 	 * aligned on a 4 byte boundary.
788 	 */
789 	header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
790 
791 	/*
792 	 * Hardware might have stripped the IV/EIV/ICV data,
793 	 * in that case it is possible that the data was
794 	 * provided separately (through hardware descriptor)
795 	 * in which case we should reinsert the data into the frame.
796 	 */
797 	if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
798 	    (rxdesc.flags & RX_FLAG_IV_STRIPPED))
799 		rt2x00crypto_rx_insert_iv(entry->skb, header_length,
800 					  &rxdesc);
801 	else if (header_length &&
802 		 (rxdesc.size > header_length) &&
803 		 (rxdesc.dev_flags & RXDONE_L2PAD))
804 		rt2x00queue_remove_l2pad(entry->skb, header_length);
805 
806 	/* Trim buffer to correct size */
807 	skb_trim(entry->skb, rxdesc.size);
808 
809 	/*
810 	 * Translate the signal to the correct bitrate index.
811 	 */
812 	rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
813 	if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
814 	    rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
815 		rxdesc.encoding = RX_ENC_HT;
816 
817 	/*
818 	 * Check if this is a beacon, and more frames have been
819 	 * buffered while we were in powersaving mode.
820 	 */
821 	rt2x00lib_rxdone_check_ps(rt2x00dev, entry->skb, &rxdesc);
822 
823 	/*
824 	 * Check for incoming BlockAcks to match to the BlockAckReqs
825 	 * we've send out.
826 	 */
827 	rt2x00lib_rxdone_check_ba(rt2x00dev, entry->skb, &rxdesc);
828 
829 	/*
830 	 * Update extra components
831 	 */
832 	rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
833 	rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
834 	rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry);
835 
836 	/*
837 	 * Initialize RX status information, and send frame
838 	 * to mac80211.
839 	 */
840 	rx_status = IEEE80211_SKB_RXCB(entry->skb);
841 
842 	/* Ensure that all fields of rx_status are initialized
843 	 * properly. The skb->cb array was used for driver
844 	 * specific informations, so rx_status might contain
845 	 * garbage.
846 	 */
847 	memset(rx_status, 0, sizeof(*rx_status));
848 
849 	rx_status->mactime = rxdesc.timestamp;
850 	rx_status->band = rt2x00dev->curr_band;
851 	rx_status->freq = rt2x00dev->curr_freq;
852 	rx_status->rate_idx = rate_idx;
853 	rx_status->signal = rxdesc.rssi;
854 	rx_status->flag = rxdesc.flags;
855 	rx_status->enc_flags = rxdesc.enc_flags;
856 	rx_status->encoding = rxdesc.encoding;
857 	rx_status->bw = rxdesc.bw;
858 	rx_status->antenna = rt2x00dev->link.ant.active.rx;
859 
860 	ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
861 
862 renew_skb:
863 	/*
864 	 * Replace the skb with the freshly allocated one.
865 	 */
866 	entry->skb = skb;
867 
868 submit_entry:
869 	entry->flags = 0;
870 	rt2x00queue_index_inc(entry, Q_INDEX_DONE);
871 	if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
872 	    test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
873 		rt2x00dev->ops->lib->clear_entry(entry);
874 }
875 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
876 
877 /*
878  * Driver initialization handlers.
879  */
880 const struct rt2x00_rate rt2x00_supported_rates[12] = {
881 	{
882 		.flags = DEV_RATE_CCK,
883 		.bitrate = 10,
884 		.ratemask = BIT(0),
885 		.plcp = 0x00,
886 		.mcs = RATE_MCS(RATE_MODE_CCK, 0),
887 	},
888 	{
889 		.flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
890 		.bitrate = 20,
891 		.ratemask = BIT(1),
892 		.plcp = 0x01,
893 		.mcs = RATE_MCS(RATE_MODE_CCK, 1),
894 	},
895 	{
896 		.flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
897 		.bitrate = 55,
898 		.ratemask = BIT(2),
899 		.plcp = 0x02,
900 		.mcs = RATE_MCS(RATE_MODE_CCK, 2),
901 	},
902 	{
903 		.flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
904 		.bitrate = 110,
905 		.ratemask = BIT(3),
906 		.plcp = 0x03,
907 		.mcs = RATE_MCS(RATE_MODE_CCK, 3),
908 	},
909 	{
910 		.flags = DEV_RATE_OFDM,
911 		.bitrate = 60,
912 		.ratemask = BIT(4),
913 		.plcp = 0x0b,
914 		.mcs = RATE_MCS(RATE_MODE_OFDM, 0),
915 	},
916 	{
917 		.flags = DEV_RATE_OFDM,
918 		.bitrate = 90,
919 		.ratemask = BIT(5),
920 		.plcp = 0x0f,
921 		.mcs = RATE_MCS(RATE_MODE_OFDM, 1),
922 	},
923 	{
924 		.flags = DEV_RATE_OFDM,
925 		.bitrate = 120,
926 		.ratemask = BIT(6),
927 		.plcp = 0x0a,
928 		.mcs = RATE_MCS(RATE_MODE_OFDM, 2),
929 	},
930 	{
931 		.flags = DEV_RATE_OFDM,
932 		.bitrate = 180,
933 		.ratemask = BIT(7),
934 		.plcp = 0x0e,
935 		.mcs = RATE_MCS(RATE_MODE_OFDM, 3),
936 	},
937 	{
938 		.flags = DEV_RATE_OFDM,
939 		.bitrate = 240,
940 		.ratemask = BIT(8),
941 		.plcp = 0x09,
942 		.mcs = RATE_MCS(RATE_MODE_OFDM, 4),
943 	},
944 	{
945 		.flags = DEV_RATE_OFDM,
946 		.bitrate = 360,
947 		.ratemask = BIT(9),
948 		.plcp = 0x0d,
949 		.mcs = RATE_MCS(RATE_MODE_OFDM, 5),
950 	},
951 	{
952 		.flags = DEV_RATE_OFDM,
953 		.bitrate = 480,
954 		.ratemask = BIT(10),
955 		.plcp = 0x08,
956 		.mcs = RATE_MCS(RATE_MODE_OFDM, 6),
957 	},
958 	{
959 		.flags = DEV_RATE_OFDM,
960 		.bitrate = 540,
961 		.ratemask = BIT(11),
962 		.plcp = 0x0c,
963 		.mcs = RATE_MCS(RATE_MODE_OFDM, 7),
964 	},
965 };
966 
rt2x00lib_channel(struct ieee80211_channel * entry,const int channel,const int tx_power,const int value)967 static void rt2x00lib_channel(struct ieee80211_channel *entry,
968 			      const int channel, const int tx_power,
969 			      const int value)
970 {
971 	/* XXX: this assumption about the band is wrong for 802.11j */
972 	entry->band = channel <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
973 	entry->center_freq = ieee80211_channel_to_frequency(channel,
974 							    entry->band);
975 	entry->hw_value = value;
976 	entry->max_power = tx_power;
977 	entry->max_antenna_gain = 0xff;
978 }
979 
rt2x00lib_rate(struct ieee80211_rate * entry,const u16 index,const struct rt2x00_rate * rate)980 static void rt2x00lib_rate(struct ieee80211_rate *entry,
981 			   const u16 index, const struct rt2x00_rate *rate)
982 {
983 	entry->flags = 0;
984 	entry->bitrate = rate->bitrate;
985 	entry->hw_value = index;
986 	entry->hw_value_short = index;
987 
988 	if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
989 		entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
990 }
991 
rt2x00lib_set_mac_address(struct rt2x00_dev * rt2x00dev,u8 * eeprom_mac_addr)992 void rt2x00lib_set_mac_address(struct rt2x00_dev *rt2x00dev, u8 *eeprom_mac_addr)
993 {
994 	const char *mac_addr;
995 
996 	mac_addr = of_get_mac_address(rt2x00dev->dev->of_node);
997 	if (!IS_ERR(mac_addr))
998 		ether_addr_copy(eeprom_mac_addr, mac_addr);
999 
1000 	if (!is_valid_ether_addr(eeprom_mac_addr)) {
1001 		eth_random_addr(eeprom_mac_addr);
1002 		rt2x00_eeprom_dbg(rt2x00dev, "MAC: %pM\n", eeprom_mac_addr);
1003 	}
1004 }
1005 EXPORT_SYMBOL_GPL(rt2x00lib_set_mac_address);
1006 
rt2x00lib_probe_hw_modes(struct rt2x00_dev * rt2x00dev,struct hw_mode_spec * spec)1007 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
1008 				    struct hw_mode_spec *spec)
1009 {
1010 	struct ieee80211_hw *hw = rt2x00dev->hw;
1011 	struct ieee80211_channel *channels;
1012 	struct ieee80211_rate *rates;
1013 	unsigned int num_rates;
1014 	unsigned int i;
1015 
1016 	num_rates = 0;
1017 	if (spec->supported_rates & SUPPORT_RATE_CCK)
1018 		num_rates += 4;
1019 	if (spec->supported_rates & SUPPORT_RATE_OFDM)
1020 		num_rates += 8;
1021 
1022 	channels = kcalloc(spec->num_channels, sizeof(*channels), GFP_KERNEL);
1023 	if (!channels)
1024 		return -ENOMEM;
1025 
1026 	rates = kcalloc(num_rates, sizeof(*rates), GFP_KERNEL);
1027 	if (!rates)
1028 		goto exit_free_channels;
1029 
1030 	/*
1031 	 * Initialize Rate list.
1032 	 */
1033 	for (i = 0; i < num_rates; i++)
1034 		rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
1035 
1036 	/*
1037 	 * Initialize Channel list.
1038 	 */
1039 	for (i = 0; i < spec->num_channels; i++) {
1040 		rt2x00lib_channel(&channels[i],
1041 				  spec->channels[i].channel,
1042 				  spec->channels_info[i].max_power, i);
1043 	}
1044 
1045 	/*
1046 	 * Intitialize 802.11b, 802.11g
1047 	 * Rates: CCK, OFDM.
1048 	 * Channels: 2.4 GHz
1049 	 */
1050 	if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
1051 		rt2x00dev->bands[NL80211_BAND_2GHZ].n_channels = 14;
1052 		rt2x00dev->bands[NL80211_BAND_2GHZ].n_bitrates = num_rates;
1053 		rt2x00dev->bands[NL80211_BAND_2GHZ].channels = channels;
1054 		rt2x00dev->bands[NL80211_BAND_2GHZ].bitrates = rates;
1055 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
1056 		    &rt2x00dev->bands[NL80211_BAND_2GHZ];
1057 		memcpy(&rt2x00dev->bands[NL80211_BAND_2GHZ].ht_cap,
1058 		       &spec->ht, sizeof(spec->ht));
1059 	}
1060 
1061 	/*
1062 	 * Intitialize 802.11a
1063 	 * Rates: OFDM.
1064 	 * Channels: OFDM, UNII, HiperLAN2.
1065 	 */
1066 	if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
1067 		rt2x00dev->bands[NL80211_BAND_5GHZ].n_channels =
1068 		    spec->num_channels - 14;
1069 		rt2x00dev->bands[NL80211_BAND_5GHZ].n_bitrates =
1070 		    num_rates - 4;
1071 		rt2x00dev->bands[NL80211_BAND_5GHZ].channels = &channels[14];
1072 		rt2x00dev->bands[NL80211_BAND_5GHZ].bitrates = &rates[4];
1073 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
1074 		    &rt2x00dev->bands[NL80211_BAND_5GHZ];
1075 		memcpy(&rt2x00dev->bands[NL80211_BAND_5GHZ].ht_cap,
1076 		       &spec->ht, sizeof(spec->ht));
1077 	}
1078 
1079 	return 0;
1080 
1081  exit_free_channels:
1082 	kfree(channels);
1083 	rt2x00_err(rt2x00dev, "Allocation ieee80211 modes failed\n");
1084 	return -ENOMEM;
1085 }
1086 
rt2x00lib_remove_hw(struct rt2x00_dev * rt2x00dev)1087 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
1088 {
1089 	if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
1090 		ieee80211_unregister_hw(rt2x00dev->hw);
1091 
1092 	if (likely(rt2x00dev->hw->wiphy->bands[NL80211_BAND_2GHZ])) {
1093 		kfree(rt2x00dev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels);
1094 		kfree(rt2x00dev->hw->wiphy->bands[NL80211_BAND_2GHZ]->bitrates);
1095 		rt2x00dev->hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
1096 		rt2x00dev->hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
1097 	}
1098 
1099 	kfree(rt2x00dev->spec.channels_info);
1100 }
1101 
rt2x00lib_probe_hw(struct rt2x00_dev * rt2x00dev)1102 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
1103 {
1104 	struct hw_mode_spec *spec = &rt2x00dev->spec;
1105 	int status;
1106 
1107 	if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
1108 		return 0;
1109 
1110 	/*
1111 	 * Initialize HW modes.
1112 	 */
1113 	status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
1114 	if (status)
1115 		return status;
1116 
1117 	/*
1118 	 * Initialize HW fields.
1119 	 */
1120 	rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
1121 
1122 	/*
1123 	 * Initialize extra TX headroom required.
1124 	 */
1125 	rt2x00dev->hw->extra_tx_headroom =
1126 		max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
1127 		      rt2x00dev->extra_tx_headroom);
1128 
1129 	/*
1130 	 * Take TX headroom required for alignment into account.
1131 	 */
1132 	if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_L2PAD))
1133 		rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
1134 	else if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DMA))
1135 		rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
1136 
1137 	/*
1138 	 * Tell mac80211 about the size of our private STA structure.
1139 	 */
1140 	rt2x00dev->hw->sta_data_size = sizeof(struct rt2x00_sta);
1141 
1142 	/*
1143 	 * Allocate tx status FIFO for driver use.
1144 	 */
1145 	if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TXSTATUS_FIFO)) {
1146 		/*
1147 		 * Allocate the txstatus fifo. In the worst case the tx
1148 		 * status fifo has to hold the tx status of all entries
1149 		 * in all tx queues. Hence, calculate the kfifo size as
1150 		 * tx_queues * entry_num and round up to the nearest
1151 		 * power of 2.
1152 		 */
1153 		int kfifo_size =
1154 			roundup_pow_of_two(rt2x00dev->ops->tx_queues *
1155 					   rt2x00dev->tx->limit *
1156 					   sizeof(u32));
1157 
1158 		status = kfifo_alloc(&rt2x00dev->txstatus_fifo, kfifo_size,
1159 				     GFP_KERNEL);
1160 		if (status)
1161 			return status;
1162 	}
1163 
1164 	/*
1165 	 * Initialize tasklets if used by the driver. Tasklets are
1166 	 * disabled until the interrupts are turned on. The driver
1167 	 * has to handle that.
1168 	 */
1169 #define RT2X00_TASKLET_INIT(taskletname) \
1170 	if (rt2x00dev->ops->lib->taskletname) { \
1171 		tasklet_setup(&rt2x00dev->taskletname, \
1172 			     rt2x00dev->ops->lib->taskletname); \
1173 	}
1174 
1175 	RT2X00_TASKLET_INIT(txstatus_tasklet);
1176 	RT2X00_TASKLET_INIT(pretbtt_tasklet);
1177 	RT2X00_TASKLET_INIT(tbtt_tasklet);
1178 	RT2X00_TASKLET_INIT(rxdone_tasklet);
1179 	RT2X00_TASKLET_INIT(autowake_tasklet);
1180 
1181 #undef RT2X00_TASKLET_INIT
1182 
1183 	/*
1184 	 * Register HW.
1185 	 */
1186 	status = ieee80211_register_hw(rt2x00dev->hw);
1187 	if (status)
1188 		return status;
1189 
1190 	set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
1191 
1192 	return 0;
1193 }
1194 
1195 /*
1196  * Initialization/uninitialization handlers.
1197  */
rt2x00lib_uninitialize(struct rt2x00_dev * rt2x00dev)1198 static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
1199 {
1200 	if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
1201 		return;
1202 
1203 	/*
1204 	 * Stop rfkill polling.
1205 	 */
1206 	if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DELAYED_RFKILL))
1207 		rt2x00rfkill_unregister(rt2x00dev);
1208 
1209 	/*
1210 	 * Allow the HW to uninitialize.
1211 	 */
1212 	rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1213 
1214 	/*
1215 	 * Free allocated queue entries.
1216 	 */
1217 	rt2x00queue_uninitialize(rt2x00dev);
1218 }
1219 
rt2x00lib_initialize(struct rt2x00_dev * rt2x00dev)1220 static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
1221 {
1222 	int status;
1223 
1224 	if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
1225 		return 0;
1226 
1227 	/*
1228 	 * Allocate all queue entries.
1229 	 */
1230 	status = rt2x00queue_initialize(rt2x00dev);
1231 	if (status)
1232 		return status;
1233 
1234 	/*
1235 	 * Initialize the device.
1236 	 */
1237 	status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1238 	if (status) {
1239 		rt2x00queue_uninitialize(rt2x00dev);
1240 		return status;
1241 	}
1242 
1243 	set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
1244 
1245 	/*
1246 	 * Start rfkill polling.
1247 	 */
1248 	if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DELAYED_RFKILL))
1249 		rt2x00rfkill_register(rt2x00dev);
1250 
1251 	return 0;
1252 }
1253 
rt2x00lib_start(struct rt2x00_dev * rt2x00dev)1254 int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
1255 {
1256 	int retval = 0;
1257 
1258 	/*
1259 	 * If this is the first interface which is added,
1260 	 * we should load the firmware now.
1261 	 */
1262 	retval = rt2x00lib_load_firmware(rt2x00dev);
1263 	if (retval)
1264 		goto out;
1265 
1266 	/*
1267 	 * Initialize the device.
1268 	 */
1269 	retval = rt2x00lib_initialize(rt2x00dev);
1270 	if (retval)
1271 		goto out;
1272 
1273 	rt2x00dev->intf_ap_count = 0;
1274 	rt2x00dev->intf_sta_count = 0;
1275 	rt2x00dev->intf_associated = 0;
1276 	rt2x00dev->intf_beaconing = 0;
1277 
1278 	/* Enable the radio */
1279 	retval = rt2x00lib_enable_radio(rt2x00dev);
1280 	if (retval)
1281 		goto out;
1282 
1283 	set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
1284 
1285 out:
1286 	return retval;
1287 }
1288 
rt2x00lib_stop(struct rt2x00_dev * rt2x00dev)1289 void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
1290 {
1291 	if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
1292 		return;
1293 
1294 	/*
1295 	 * Perhaps we can add something smarter here,
1296 	 * but for now just disabling the radio should do.
1297 	 */
1298 	rt2x00lib_disable_radio(rt2x00dev);
1299 
1300 	rt2x00dev->intf_ap_count = 0;
1301 	rt2x00dev->intf_sta_count = 0;
1302 	rt2x00dev->intf_associated = 0;
1303 	rt2x00dev->intf_beaconing = 0;
1304 }
1305 
rt2x00lib_set_if_combinations(struct rt2x00_dev * rt2x00dev)1306 static inline void rt2x00lib_set_if_combinations(struct rt2x00_dev *rt2x00dev)
1307 {
1308 	struct ieee80211_iface_limit *if_limit;
1309 	struct ieee80211_iface_combination *if_combination;
1310 
1311 	if (rt2x00dev->ops->max_ap_intf < 2)
1312 		return;
1313 
1314 	/*
1315 	 * Build up AP interface limits structure.
1316 	 */
1317 	if_limit = &rt2x00dev->if_limits_ap;
1318 	if_limit->max = rt2x00dev->ops->max_ap_intf;
1319 	if_limit->types = BIT(NL80211_IFTYPE_AP);
1320 #ifdef CONFIG_MAC80211_MESH
1321 	if_limit->types |= BIT(NL80211_IFTYPE_MESH_POINT);
1322 #endif
1323 
1324 	/*
1325 	 * Build up AP interface combinations structure.
1326 	 */
1327 	if_combination = &rt2x00dev->if_combinations[IF_COMB_AP];
1328 	if_combination->limits = if_limit;
1329 	if_combination->n_limits = 1;
1330 	if_combination->max_interfaces = if_limit->max;
1331 	if_combination->num_different_channels = 1;
1332 
1333 	/*
1334 	 * Finally, specify the possible combinations to mac80211.
1335 	 */
1336 	rt2x00dev->hw->wiphy->iface_combinations = rt2x00dev->if_combinations;
1337 	rt2x00dev->hw->wiphy->n_iface_combinations = 1;
1338 }
1339 
rt2x00dev_extra_tx_headroom(struct rt2x00_dev * rt2x00dev)1340 static unsigned int rt2x00dev_extra_tx_headroom(struct rt2x00_dev *rt2x00dev)
1341 {
1342 	if (WARN_ON(!rt2x00dev->tx))
1343 		return 0;
1344 
1345 	if (rt2x00_is_usb(rt2x00dev))
1346 		return rt2x00dev->tx[0].winfo_size + rt2x00dev->tx[0].desc_size;
1347 
1348 	return rt2x00dev->tx[0].winfo_size;
1349 }
1350 
1351 /*
1352  * driver allocation handlers.
1353  */
rt2x00lib_probe_dev(struct rt2x00_dev * rt2x00dev)1354 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1355 {
1356 	int retval = -ENOMEM;
1357 
1358 	/*
1359 	 * Set possible interface combinations.
1360 	 */
1361 	rt2x00lib_set_if_combinations(rt2x00dev);
1362 
1363 	/*
1364 	 * Allocate the driver data memory, if necessary.
1365 	 */
1366 	if (rt2x00dev->ops->drv_data_size > 0) {
1367 		rt2x00dev->drv_data = kzalloc(rt2x00dev->ops->drv_data_size,
1368 			                      GFP_KERNEL);
1369 		if (!rt2x00dev->drv_data) {
1370 			retval = -ENOMEM;
1371 			goto exit;
1372 		}
1373 	}
1374 
1375 	spin_lock_init(&rt2x00dev->irqmask_lock);
1376 	mutex_init(&rt2x00dev->csr_mutex);
1377 	mutex_init(&rt2x00dev->conf_mutex);
1378 	INIT_LIST_HEAD(&rt2x00dev->bar_list);
1379 	spin_lock_init(&rt2x00dev->bar_list_lock);
1380 	hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC,
1381 		     HRTIMER_MODE_REL);
1382 
1383 	set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1384 
1385 	/*
1386 	 * Make room for rt2x00_intf inside the per-interface
1387 	 * structure ieee80211_vif.
1388 	 */
1389 	rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
1390 
1391 	/*
1392 	 * rt2x00 devices can only use the last n bits of the MAC address
1393 	 * for virtual interfaces.
1394 	 */
1395 	rt2x00dev->hw->wiphy->addr_mask[ETH_ALEN - 1] =
1396 		(rt2x00dev->ops->max_ap_intf - 1);
1397 
1398 	/*
1399 	 * Initialize work.
1400 	 */
1401 	rt2x00dev->workqueue =
1402 	    alloc_ordered_workqueue("%s", 0, wiphy_name(rt2x00dev->hw->wiphy));
1403 	if (!rt2x00dev->workqueue) {
1404 		retval = -ENOMEM;
1405 		goto exit;
1406 	}
1407 
1408 	INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
1409 	INIT_DELAYED_WORK(&rt2x00dev->autowakeup_work, rt2x00lib_autowakeup);
1410 	INIT_WORK(&rt2x00dev->sleep_work, rt2x00lib_sleep);
1411 
1412 	/*
1413 	 * Let the driver probe the device to detect the capabilities.
1414 	 */
1415 	retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1416 	if (retval) {
1417 		rt2x00_err(rt2x00dev, "Failed to allocate device\n");
1418 		goto exit;
1419 	}
1420 
1421 	/*
1422 	 * Allocate queue array.
1423 	 */
1424 	retval = rt2x00queue_allocate(rt2x00dev);
1425 	if (retval)
1426 		goto exit;
1427 
1428 	/* Cache TX headroom value */
1429 	rt2x00dev->extra_tx_headroom = rt2x00dev_extra_tx_headroom(rt2x00dev);
1430 
1431 	/*
1432 	 * Determine which operating modes are supported, all modes
1433 	 * which require beaconing, depend on the availability of
1434 	 * beacon entries.
1435 	 */
1436 	rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1437 	if (rt2x00dev->bcn->limit > 0)
1438 		rt2x00dev->hw->wiphy->interface_modes |=
1439 		    BIT(NL80211_IFTYPE_ADHOC) |
1440 #ifdef CONFIG_MAC80211_MESH
1441 		    BIT(NL80211_IFTYPE_MESH_POINT) |
1442 #endif
1443 #ifdef CONFIG_WIRELESS_WDS
1444 		    BIT(NL80211_IFTYPE_WDS) |
1445 #endif
1446 		    BIT(NL80211_IFTYPE_AP);
1447 
1448 	rt2x00dev->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
1449 
1450 	wiphy_ext_feature_set(rt2x00dev->hw->wiphy,
1451 			      NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1452 
1453 	/*
1454 	 * Initialize ieee80211 structure.
1455 	 */
1456 	retval = rt2x00lib_probe_hw(rt2x00dev);
1457 	if (retval) {
1458 		rt2x00_err(rt2x00dev, "Failed to initialize hw\n");
1459 		goto exit;
1460 	}
1461 
1462 	/*
1463 	 * Register extra components.
1464 	 */
1465 	rt2x00link_register(rt2x00dev);
1466 	rt2x00leds_register(rt2x00dev);
1467 	rt2x00debug_register(rt2x00dev);
1468 
1469 	/*
1470 	 * Start rfkill polling.
1471 	 */
1472 	if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DELAYED_RFKILL))
1473 		rt2x00rfkill_register(rt2x00dev);
1474 
1475 	return 0;
1476 
1477 exit:
1478 	rt2x00lib_remove_dev(rt2x00dev);
1479 
1480 	return retval;
1481 }
1482 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1483 
rt2x00lib_remove_dev(struct rt2x00_dev * rt2x00dev)1484 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1485 {
1486 	clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1487 
1488 	/*
1489 	 * Stop rfkill polling.
1490 	 */
1491 	if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DELAYED_RFKILL))
1492 		rt2x00rfkill_unregister(rt2x00dev);
1493 
1494 	/*
1495 	 * Disable radio.
1496 	 */
1497 	rt2x00lib_disable_radio(rt2x00dev);
1498 
1499 	/*
1500 	 * Stop all work.
1501 	 */
1502 	cancel_work_sync(&rt2x00dev->intf_work);
1503 	cancel_delayed_work_sync(&rt2x00dev->autowakeup_work);
1504 	cancel_work_sync(&rt2x00dev->sleep_work);
1505 
1506 	hrtimer_cancel(&rt2x00dev->txstatus_timer);
1507 
1508 	/*
1509 	 * Kill the tx status tasklet.
1510 	 */
1511 	tasklet_kill(&rt2x00dev->txstatus_tasklet);
1512 	tasklet_kill(&rt2x00dev->pretbtt_tasklet);
1513 	tasklet_kill(&rt2x00dev->tbtt_tasklet);
1514 	tasklet_kill(&rt2x00dev->rxdone_tasklet);
1515 	tasklet_kill(&rt2x00dev->autowake_tasklet);
1516 
1517 	/*
1518 	 * Uninitialize device.
1519 	 */
1520 	rt2x00lib_uninitialize(rt2x00dev);
1521 
1522 	if (rt2x00dev->workqueue)
1523 		destroy_workqueue(rt2x00dev->workqueue);
1524 
1525 	/*
1526 	 * Free the tx status fifo.
1527 	 */
1528 	kfifo_free(&rt2x00dev->txstatus_fifo);
1529 
1530 	/*
1531 	 * Free extra components
1532 	 */
1533 	rt2x00debug_deregister(rt2x00dev);
1534 	rt2x00leds_unregister(rt2x00dev);
1535 
1536 	/*
1537 	 * Free ieee80211_hw memory.
1538 	 */
1539 	rt2x00lib_remove_hw(rt2x00dev);
1540 
1541 	/*
1542 	 * Free firmware image.
1543 	 */
1544 	rt2x00lib_free_firmware(rt2x00dev);
1545 
1546 	/*
1547 	 * Free queue structures.
1548 	 */
1549 	rt2x00queue_free(rt2x00dev);
1550 
1551 	/*
1552 	 * Free the driver data.
1553 	 */
1554 	kfree(rt2x00dev->drv_data);
1555 }
1556 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1557 
1558 /*
1559  * Device state handlers
1560  */
rt2x00lib_suspend(struct rt2x00_dev * rt2x00dev)1561 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev)
1562 {
1563 	rt2x00_dbg(rt2x00dev, "Going to sleep\n");
1564 
1565 	/*
1566 	 * Prevent mac80211 from accessing driver while suspended.
1567 	 */
1568 	if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1569 		return 0;
1570 
1571 	/*
1572 	 * Cleanup as much as possible.
1573 	 */
1574 	rt2x00lib_uninitialize(rt2x00dev);
1575 
1576 	/*
1577 	 * Suspend/disable extra components.
1578 	 */
1579 	rt2x00leds_suspend(rt2x00dev);
1580 	rt2x00debug_deregister(rt2x00dev);
1581 
1582 	/*
1583 	 * Set device mode to sleep for power management,
1584 	 * on some hardware this call seems to consistently fail.
1585 	 * From the specifications it is hard to tell why it fails,
1586 	 * and if this is a "bad thing".
1587 	 * Overall it is safe to just ignore the failure and
1588 	 * continue suspending. The only downside is that the
1589 	 * device will not be in optimal power save mode, but with
1590 	 * the radio and the other components already disabled the
1591 	 * device is as good as disabled.
1592 	 */
1593 	if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
1594 		rt2x00_warn(rt2x00dev, "Device failed to enter sleep state, continue suspending\n");
1595 
1596 	return 0;
1597 }
1598 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1599 
rt2x00lib_resume(struct rt2x00_dev * rt2x00dev)1600 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1601 {
1602 	rt2x00_dbg(rt2x00dev, "Waking up\n");
1603 
1604 	/*
1605 	 * Restore/enable extra components.
1606 	 */
1607 	rt2x00debug_register(rt2x00dev);
1608 	rt2x00leds_resume(rt2x00dev);
1609 
1610 	/*
1611 	 * We are ready again to receive requests from mac80211.
1612 	 */
1613 	set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1614 
1615 	return 0;
1616 }
1617 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1618 
1619 /*
1620  * rt2x00lib module information.
1621  */
1622 MODULE_AUTHOR(DRV_PROJECT);
1623 MODULE_VERSION(DRV_VERSION);
1624 MODULE_DESCRIPTION("rt2x00 library");
1625 MODULE_LICENSE("GPL");
1626