• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/net/wireless/p54/
1/*
2 * Common code for mac80211 Prism54 drivers
3 *
4 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
5 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 *
8 * Based on:
9 * - the islsm (softmac prism54) driver, which is:
10 *   Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
11 * - stlc45xx driver
12 *   Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/init.h>
20#include <linux/firmware.h>
21#include <linux/etherdevice.h>
22
23#include <net/mac80211.h>
24
25#include "p54.h"
26#include "lmac.h"
27
28#ifdef P54_MM_DEBUG
29static void p54_dump_tx_queue(struct p54_common *priv)
30{
31	unsigned long flags;
32	struct ieee80211_tx_info *info;
33	struct p54_tx_info *range;
34	struct sk_buff *skb;
35	struct p54_hdr *hdr;
36	unsigned int i = 0;
37	u32 prev_addr;
38	u32 largest_hole = 0, free;
39
40	spin_lock_irqsave(&priv->tx_queue.lock, flags);
41	wiphy_debug(priv->hw->wiphy, "/ --- tx queue dump (%d entries) ---\n",
42		    skb_queue_len(&priv->tx_queue));
43
44	prev_addr = priv->rx_start;
45	skb_queue_walk(&priv->tx_queue, skb) {
46		info = IEEE80211_SKB_CB(skb);
47		range = (void *) info->rate_driver_data;
48		hdr = (void *) skb->data;
49
50		free = range->start_addr - prev_addr;
51		wiphy_debug(priv->hw->wiphy,
52			    "| [%02d] => [skb:%p skb_len:0x%04x "
53			    "hdr:{flags:%02x len:%04x req_id:%04x type:%02x} "
54			    "mem:{start:%04x end:%04x, free:%d}]\n",
55			    i++, skb, skb->len,
56			    le16_to_cpu(hdr->flags), le16_to_cpu(hdr->len),
57			    le32_to_cpu(hdr->req_id), le16_to_cpu(hdr->type),
58			    range->start_addr, range->end_addr, free);
59
60		prev_addr = range->end_addr;
61		largest_hole = max(largest_hole, free);
62	}
63	free = priv->rx_end - prev_addr;
64	largest_hole = max(largest_hole, free);
65	wiphy_debug(priv->hw->wiphy,
66		    "\\ --- [free: %d], largest free block: %d ---\n",
67		    free, largest_hole);
68	spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
69}
70#endif /* P54_MM_DEBUG */
71
72/*
73 * So, the firmware is somewhat stupid and doesn't know what places in its
74 * memory incoming data should go to. By poking around in the firmware, we
75 * can find some unused memory to upload our packets to. However, data that we
76 * want the card to TX needs to stay intact until the card has told us that
77 * it is done with it. This function finds empty places we can upload to and
78 * marks allocated areas as reserved if necessary. p54_find_and_unlink_skb or
79 * p54_free_skb frees allocated areas.
80 */
81static int p54_assign_address(struct p54_common *priv, struct sk_buff *skb)
82{
83	struct sk_buff *entry, *target_skb = NULL;
84	struct ieee80211_tx_info *info;
85	struct p54_tx_info *range;
86	struct p54_hdr *data = (void *) skb->data;
87	unsigned long flags;
88	u32 last_addr = priv->rx_start;
89	u32 target_addr = priv->rx_start;
90	u16 len = priv->headroom + skb->len + priv->tailroom + 3;
91
92	info = IEEE80211_SKB_CB(skb);
93	range = (void *) info->rate_driver_data;
94	len = (range->extra_len + len) & ~0x3;
95
96	spin_lock_irqsave(&priv->tx_queue.lock, flags);
97	if (unlikely(skb_queue_len(&priv->tx_queue) == 32)) {
98		/*
99		 * The tx_queue is now really full.
100		 *
101		 * TODO: check if the device has crashed and reset it.
102		 */
103		spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
104		return -EBUSY;
105	}
106
107	skb_queue_walk(&priv->tx_queue, entry) {
108		u32 hole_size;
109		info = IEEE80211_SKB_CB(entry);
110		range = (void *) info->rate_driver_data;
111		hole_size = range->start_addr - last_addr;
112
113		if (!target_skb && hole_size >= len) {
114			target_skb = entry->prev;
115			hole_size -= len;
116			target_addr = last_addr;
117			break;
118		}
119		last_addr = range->end_addr;
120	}
121	if (unlikely(!target_skb)) {
122		if (priv->rx_end - last_addr >= len) {
123			target_skb = priv->tx_queue.prev;
124			if (!skb_queue_empty(&priv->tx_queue)) {
125				info = IEEE80211_SKB_CB(target_skb);
126				range = (void *)info->rate_driver_data;
127				target_addr = range->end_addr;
128			}
129		} else {
130			spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
131			return -ENOSPC;
132		}
133	}
134
135	info = IEEE80211_SKB_CB(skb);
136	range = (void *) info->rate_driver_data;
137	range->start_addr = target_addr;
138	range->end_addr = target_addr + len;
139	data->req_id = cpu_to_le32(target_addr + priv->headroom);
140	if (IS_DATA_FRAME(skb) &&
141	    unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON))
142		priv->beacon_req_id = data->req_id;
143
144	__skb_queue_after(&priv->tx_queue, target_skb, skb);
145	spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
146	return 0;
147}
148
149static void p54_tx_pending(struct p54_common *priv)
150{
151	struct sk_buff *skb;
152	int ret;
153
154	skb = skb_dequeue(&priv->tx_pending);
155	if (unlikely(!skb))
156		return ;
157
158	ret = p54_assign_address(priv, skb);
159	if (unlikely(ret))
160		skb_queue_head(&priv->tx_pending, skb);
161	else
162		priv->tx(priv->hw, skb);
163}
164
165static void p54_wake_queues(struct p54_common *priv)
166{
167	unsigned long flags;
168	unsigned int i;
169
170	if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
171		return ;
172
173	p54_tx_pending(priv);
174
175	spin_lock_irqsave(&priv->tx_stats_lock, flags);
176	for (i = 0; i < priv->hw->queues; i++) {
177		if (priv->tx_stats[i + P54_QUEUE_DATA].len <
178		    priv->tx_stats[i + P54_QUEUE_DATA].limit)
179			ieee80211_wake_queue(priv->hw, i);
180	}
181	spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
182}
183
184static int p54_tx_qos_accounting_alloc(struct p54_common *priv,
185				       struct sk_buff *skb,
186				       const u16 p54_queue)
187{
188	struct p54_tx_queue_stats *queue;
189	unsigned long flags;
190
191	if (WARN_ON(p54_queue >= P54_QUEUE_NUM))
192		return -EINVAL;
193
194	queue = &priv->tx_stats[p54_queue];
195
196	spin_lock_irqsave(&priv->tx_stats_lock, flags);
197	if (unlikely(queue->len >= queue->limit && IS_QOS_QUEUE(p54_queue))) {
198		spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
199		return -ENOSPC;
200	}
201
202	queue->len++;
203	queue->count++;
204
205	if (unlikely(queue->len == queue->limit && IS_QOS_QUEUE(p54_queue))) {
206		u16 ac_queue = p54_queue - P54_QUEUE_DATA;
207		ieee80211_stop_queue(priv->hw, ac_queue);
208	}
209
210	spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
211	return 0;
212}
213
214static void p54_tx_qos_accounting_free(struct p54_common *priv,
215				       struct sk_buff *skb)
216{
217	if (IS_DATA_FRAME(skb)) {
218		unsigned long flags;
219
220		spin_lock_irqsave(&priv->tx_stats_lock, flags);
221		priv->tx_stats[GET_HW_QUEUE(skb)].len--;
222		spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
223
224		if (unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON)) {
225			if (priv->beacon_req_id == GET_REQ_ID(skb)) {
226				/* this is the  active beacon set anymore */
227				priv->beacon_req_id = 0;
228			}
229			complete(&priv->beacon_comp);
230		}
231	}
232	p54_wake_queues(priv);
233}
234
235void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
236{
237	struct p54_common *priv = dev->priv;
238	if (unlikely(!skb))
239		return ;
240
241	skb_unlink(skb, &priv->tx_queue);
242	p54_tx_qos_accounting_free(priv, skb);
243	dev_kfree_skb_any(skb);
244}
245EXPORT_SYMBOL_GPL(p54_free_skb);
246
247static struct sk_buff *p54_find_and_unlink_skb(struct p54_common *priv,
248					       const __le32 req_id)
249{
250	struct sk_buff *entry;
251	unsigned long flags;
252
253	spin_lock_irqsave(&priv->tx_queue.lock, flags);
254	skb_queue_walk(&priv->tx_queue, entry) {
255		struct p54_hdr *hdr = (struct p54_hdr *) entry->data;
256
257		if (hdr->req_id == req_id) {
258			__skb_unlink(entry, &priv->tx_queue);
259			spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
260			p54_tx_qos_accounting_free(priv, entry);
261			return entry;
262		}
263	}
264	spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
265	return NULL;
266}
267
268void p54_tx(struct p54_common *priv, struct sk_buff *skb)
269{
270	skb_queue_tail(&priv->tx_pending, skb);
271	p54_tx_pending(priv);
272}
273
274static int p54_rssi_to_dbm(struct p54_common *priv, int rssi)
275{
276	int band = priv->hw->conf.channel->band;
277
278	if (priv->rxhw != 5)
279		return ((rssi * priv->rssical_db[band].mul) / 64 +
280			 priv->rssical_db[band].add) / 4;
281	else
282		/*
283		 * TODO: find the correct formula
284		 */
285		return ((rssi * priv->rssical_db[band].mul) / 64 +
286			 priv->rssical_db[band].add) / 4;
287}
288
289/*
290 * Even if the firmware is capable of dealing with incoming traffic,
291 * while dozing, we have to prepared in case mac80211 uses PS-POLL
292 * to retrieve outstanding frames from our AP.
293 * (see comment in net/mac80211/mlme.c @ line 1993)
294 */
295static void p54_pspoll_workaround(struct p54_common *priv, struct sk_buff *skb)
296{
297	struct ieee80211_hdr *hdr = (void *) skb->data;
298	struct ieee80211_tim_ie *tim_ie;
299	u8 *tim;
300	u8 tim_len;
301	bool new_psm;
302
303	/* only beacons have a TIM IE */
304	if (!ieee80211_is_beacon(hdr->frame_control))
305		return;
306
307	if (!priv->aid)
308		return;
309
310	/* only consider beacons from the associated BSSID */
311	if (compare_ether_addr(hdr->addr3, priv->bssid))
312		return;
313
314	tim = p54_find_ie(skb, WLAN_EID_TIM);
315	if (!tim)
316		return;
317
318	tim_len = tim[1];
319	tim_ie = (struct ieee80211_tim_ie *) &tim[2];
320
321	new_psm = ieee80211_check_tim(tim_ie, tim_len, priv->aid);
322	if (new_psm != priv->powersave_override) {
323		priv->powersave_override = new_psm;
324		p54_set_ps(priv);
325	}
326}
327
328static int p54_rx_data(struct p54_common *priv, struct sk_buff *skb)
329{
330	struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
331	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
332	u16 freq = le16_to_cpu(hdr->freq);
333	size_t header_len = sizeof(*hdr);
334	u32 tsf32;
335	u8 rate = hdr->rate & 0xf;
336
337	/*
338	 * If the device is in a unspecified state we have to
339	 * ignore all data frames. Else we could end up with a
340	 * nasty crash.
341	 */
342	if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
343		return 0;
344
345	if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD)))
346		return 0;
347
348	if (hdr->decrypt_status == P54_DECRYPT_OK)
349		rx_status->flag |= RX_FLAG_DECRYPTED;
350	if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
351	    (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
352		rx_status->flag |= RX_FLAG_MMIC_ERROR;
353
354	rx_status->signal = p54_rssi_to_dbm(priv, hdr->rssi);
355	if (hdr->rate & 0x10)
356		rx_status->flag |= RX_FLAG_SHORTPRE;
357	if (priv->hw->conf.channel->band == IEEE80211_BAND_5GHZ)
358		rx_status->rate_idx = (rate < 4) ? 0 : rate - 4;
359	else
360		rx_status->rate_idx = rate;
361
362	rx_status->freq = freq;
363	rx_status->band =  priv->hw->conf.channel->band;
364	rx_status->antenna = hdr->antenna;
365
366	tsf32 = le32_to_cpu(hdr->tsf32);
367	if (tsf32 < priv->tsf_low32)
368		priv->tsf_high32++;
369	rx_status->mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
370	priv->tsf_low32 = tsf32;
371
372	rx_status->flag |= RX_FLAG_TSFT;
373
374	if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
375		header_len += hdr->align[0];
376
377	skb_pull(skb, header_len);
378	skb_trim(skb, le16_to_cpu(hdr->len));
379	if (unlikely(priv->hw->conf.flags & IEEE80211_CONF_PS))
380		p54_pspoll_workaround(priv, skb);
381
382	ieee80211_rx_irqsafe(priv->hw, skb);
383
384	ieee80211_queue_delayed_work(priv->hw, &priv->work,
385			   msecs_to_jiffies(P54_STATISTICS_UPDATE));
386
387	return -1;
388}
389
390static void p54_rx_frame_sent(struct p54_common *priv, struct sk_buff *skb)
391{
392	struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
393	struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
394	struct ieee80211_tx_info *info;
395	struct p54_hdr *entry_hdr;
396	struct p54_tx_data *entry_data;
397	struct sk_buff *entry;
398	unsigned int pad = 0, frame_len;
399	int count, idx;
400
401	entry = p54_find_and_unlink_skb(priv, hdr->req_id);
402	if (unlikely(!entry))
403		return ;
404
405	frame_len = entry->len;
406	info = IEEE80211_SKB_CB(entry);
407	entry_hdr = (struct p54_hdr *) entry->data;
408	entry_data = (struct p54_tx_data *) entry_hdr->data;
409	priv->stats.dot11ACKFailureCount += payload->tries - 1;
410
411	/*
412	 * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are
413	 * generated by the driver. Therefore tx_status is bogus
414	 * and we don't want to confuse the mac80211 stack.
415	 */
416	if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {
417		dev_kfree_skb_any(entry);
418		return ;
419	}
420
421	/*
422	 * Clear manually, ieee80211_tx_info_clear_status would
423	 * clear the counts too and we need them.
424	 */
425	memset(&info->status.ampdu_ack_len, 0,
426	       sizeof(struct ieee80211_tx_info) -
427	       offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
428	BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
429			      status.ampdu_ack_len) != 23);
430
431	if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
432		pad = entry_data->align[0];
433
434	/* walk through the rates array and adjust the counts */
435	count = payload->tries;
436	for (idx = 0; idx < 4; idx++) {
437		if (count >= info->status.rates[idx].count) {
438			count -= info->status.rates[idx].count;
439		} else if (count > 0) {
440			info->status.rates[idx].count = count;
441			count = 0;
442		} else {
443			info->status.rates[idx].idx = -1;
444			info->status.rates[idx].count = 0;
445		}
446	}
447
448	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
449	     !(payload->status & P54_TX_FAILED))
450		info->flags |= IEEE80211_TX_STAT_ACK;
451	if (payload->status & P54_TX_PSM_CANCELLED)
452		info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
453	info->status.ack_signal = p54_rssi_to_dbm(priv,
454						  (int)payload->ack_rssi);
455
456	/* Undo all changes to the frame. */
457	switch (entry_data->key_type) {
458	case P54_CRYPTO_TKIPMICHAEL: {
459		u8 *iv = (u8 *)(entry_data->align + pad +
460				entry_data->crypt_offset);
461
462		/* Restore the original TKIP IV. */
463		iv[2] = iv[0];
464		iv[0] = iv[1];
465		iv[1] = (iv[0] | 0x20) & 0x7f;	/* WEPSeed - 8.3.2.2 */
466
467		frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */
468		break;
469		}
470	case P54_CRYPTO_AESCCMP:
471		frame_len -= 8; /* remove CCMP_MIC */
472		break;
473	case P54_CRYPTO_WEP:
474		frame_len -= 4; /* remove WEP_ICV */
475		break;
476	}
477
478	skb_trim(entry, frame_len);
479	skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
480	ieee80211_tx_status_irqsafe(priv->hw, entry);
481}
482
483static void p54_rx_eeprom_readback(struct p54_common *priv,
484				   struct sk_buff *skb)
485{
486	struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
487	struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
488	struct sk_buff *tmp;
489
490	if (!priv->eeprom)
491		return ;
492
493	if (priv->fw_var >= 0x509) {
494		memcpy(priv->eeprom, eeprom->v2.data,
495		       le16_to_cpu(eeprom->v2.len));
496	} else {
497		memcpy(priv->eeprom, eeprom->v1.data,
498		       le16_to_cpu(eeprom->v1.len));
499	}
500
501	priv->eeprom = NULL;
502	tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
503	dev_kfree_skb_any(tmp);
504	complete(&priv->eeprom_comp);
505}
506
507static void p54_rx_stats(struct p54_common *priv, struct sk_buff *skb)
508{
509	struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
510	struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
511	struct sk_buff *tmp;
512	u32 tsf32;
513
514	if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
515		return ;
516
517	tsf32 = le32_to_cpu(stats->tsf32);
518	if (tsf32 < priv->tsf_low32)
519		priv->tsf_high32++;
520	priv->tsf_low32 = tsf32;
521
522	priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
523	priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
524	priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
525
526	priv->noise = p54_rssi_to_dbm(priv, le32_to_cpu(stats->noise));
527
528	tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
529	dev_kfree_skb_any(tmp);
530}
531
532static void p54_rx_trap(struct p54_common *priv, struct sk_buff *skb)
533{
534	struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
535	struct p54_trap *trap = (struct p54_trap *) hdr->data;
536	u16 event = le16_to_cpu(trap->event);
537	u16 freq = le16_to_cpu(trap->frequency);
538
539	switch (event) {
540	case P54_TRAP_BEACON_TX:
541		break;
542	case P54_TRAP_RADAR:
543		wiphy_info(priv->hw->wiphy, "radar (freq:%d MHz)\n", freq);
544		break;
545	case P54_TRAP_NO_BEACON:
546		if (priv->vif)
547			ieee80211_beacon_loss(priv->vif);
548		break;
549	case P54_TRAP_SCAN:
550		break;
551	case P54_TRAP_TBTT:
552		break;
553	case P54_TRAP_TIMER:
554		break;
555	case P54_TRAP_FAA_RADIO_OFF:
556		wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
557		break;
558	case P54_TRAP_FAA_RADIO_ON:
559		wiphy_rfkill_set_hw_state(priv->hw->wiphy, false);
560		break;
561	default:
562		wiphy_info(priv->hw->wiphy, "received event:%x freq:%d\n",
563			   event, freq);
564		break;
565	}
566}
567
568static int p54_rx_control(struct p54_common *priv, struct sk_buff *skb)
569{
570	struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
571
572	switch (le16_to_cpu(hdr->type)) {
573	case P54_CONTROL_TYPE_TXDONE:
574		p54_rx_frame_sent(priv, skb);
575		break;
576	case P54_CONTROL_TYPE_TRAP:
577		p54_rx_trap(priv, skb);
578		break;
579	case P54_CONTROL_TYPE_BBP:
580		break;
581	case P54_CONTROL_TYPE_STAT_READBACK:
582		p54_rx_stats(priv, skb);
583		break;
584	case P54_CONTROL_TYPE_EEPROM_READBACK:
585		p54_rx_eeprom_readback(priv, skb);
586		break;
587	default:
588		wiphy_debug(priv->hw->wiphy,
589			    "not handling 0x%02x type control frame\n",
590			    le16_to_cpu(hdr->type));
591		break;
592	}
593	return 0;
594}
595
596/* returns zero if skb can be reused */
597int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
598{
599	struct p54_common *priv = dev->priv;
600	u16 type = le16_to_cpu(*((__le16 *)skb->data));
601
602	if (type & P54_HDR_FLAG_CONTROL)
603		return p54_rx_control(priv, skb);
604	else
605		return p54_rx_data(priv, skb);
606}
607EXPORT_SYMBOL_GPL(p54_rx);
608
609static void p54_tx_80211_header(struct p54_common *priv, struct sk_buff *skb,
610				struct ieee80211_tx_info *info, u8 *queue,
611				u32 *extra_len, u16 *flags, u16 *aid,
612				bool *burst_possible)
613{
614	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
615
616	if (ieee80211_is_data_qos(hdr->frame_control))
617		*burst_possible = true;
618	else
619		*burst_possible = false;
620
621	if (!(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
622		*flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
623
624	if (info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE)
625		*flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
626
627	if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
628		*flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
629
630	*queue = skb_get_queue_mapping(skb) + P54_QUEUE_DATA;
631
632	switch (priv->mode) {
633	case NL80211_IFTYPE_MONITOR:
634		/*
635		 * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
636		 * every frame in promiscuous/monitor mode.
637		 * see STSW45x0C LMAC API - page 12.
638		 */
639		*aid = 0;
640		*flags |= P54_HDR_FLAG_DATA_OUT_PROMISC;
641		break;
642	case NL80211_IFTYPE_STATION:
643		*aid = 1;
644		break;
645	case NL80211_IFTYPE_AP:
646	case NL80211_IFTYPE_ADHOC:
647	case NL80211_IFTYPE_MESH_POINT:
648		if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
649			*aid = 0;
650			*queue = P54_QUEUE_CAB;
651			return;
652		}
653
654		if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
655			if (ieee80211_is_probe_resp(hdr->frame_control)) {
656				*aid = 0;
657				*flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
658					  P54_HDR_FLAG_DATA_OUT_NOCANCEL;
659				return;
660			} else if (ieee80211_is_beacon(hdr->frame_control)) {
661				*aid = 0;
662
663				if (info->flags & IEEE80211_TX_CTL_INJECTED) {
664					/*
665					 * Injecting beacons on top of a AP is
666					 * not a good idea... nevertheless,
667					 * it should be doable.
668					 */
669
670					return;
671				}
672
673				*flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
674				*queue = P54_QUEUE_BEACON;
675				*extra_len = IEEE80211_MAX_TIM_LEN;
676				return;
677			}
678		}
679
680		if (info->control.sta)
681			*aid = info->control.sta->aid;
682		break;
683	}
684}
685
686static u8 p54_convert_algo(enum ieee80211_key_alg alg)
687{
688	switch (alg) {
689	case ALG_WEP:
690		return P54_CRYPTO_WEP;
691	case ALG_TKIP:
692		return P54_CRYPTO_TKIPMICHAEL;
693	case ALG_CCMP:
694		return P54_CRYPTO_AESCCMP;
695	default:
696		return 0;
697	}
698}
699
700int p54_tx_80211(struct ieee80211_hw *dev, struct sk_buff *skb)
701{
702	struct p54_common *priv = dev->priv;
703	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
704	struct p54_tx_info *p54info;
705	struct p54_hdr *hdr;
706	struct p54_tx_data *txhdr;
707	unsigned int padding, len, extra_len;
708	int i, j, ridx;
709	u16 hdr_flags = 0, aid = 0;
710	u8 rate, queue = 0, crypt_offset = 0;
711	u8 cts_rate = 0x20;
712	u8 rc_flags;
713	u8 calculated_tries[4];
714	u8 nrates = 0, nremaining = 8;
715	bool burst_allowed = false;
716
717	p54_tx_80211_header(priv, skb, info, &queue, &extra_len,
718			    &hdr_flags, &aid, &burst_allowed);
719
720	if (p54_tx_qos_accounting_alloc(priv, skb, queue)) {
721		if (!IS_QOS_QUEUE(queue)) {
722			dev_kfree_skb_any(skb);
723			return NETDEV_TX_OK;
724		} else {
725			return NETDEV_TX_BUSY;
726		}
727	}
728
729	padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
730	len = skb->len;
731
732	if (info->control.hw_key) {
733		crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
734		if (info->control.hw_key->alg == ALG_TKIP) {
735			u8 *iv = (u8 *)(skb->data + crypt_offset);
736			/*
737			 * The firmware excepts that the IV has to have
738			 * this special format
739			 */
740			iv[1] = iv[0];
741			iv[0] = iv[2];
742			iv[2] = 0;
743		}
744	}
745
746	txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
747	hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
748
749	if (padding)
750		hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
751	hdr->type = cpu_to_le16(aid);
752	hdr->rts_tries = info->control.rates[0].count;
753
754	/*
755	 * we register the rates in perfect order, and
756	 * RTS/CTS won't happen on 5 GHz
757	 */
758	cts_rate = info->control.rts_cts_rate_idx;
759
760	memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
761
762	/* see how many rates got used */
763	for (i = 0; i < dev->max_rates; i++) {
764		if (info->control.rates[i].idx < 0)
765			break;
766		nrates++;
767	}
768
769	/* limit tries to 8/nrates per rate */
770	for (i = 0; i < nrates; i++) {
771		/*
772		 * The magic expression here is equivalent to 8/nrates for
773		 * all values that matter, but avoids division and jumps.
774		 * Note that nrates can only take the values 1 through 4.
775		 */
776		calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
777						 info->control.rates[i].count);
778		nremaining -= calculated_tries[i];
779	}
780
781	/* if there are tries left, distribute from back to front */
782	for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
783		int tmp = info->control.rates[i].count - calculated_tries[i];
784
785		if (tmp <= 0)
786			continue;
787		/* RC requested more tries at this rate */
788
789		tmp = min_t(int, tmp, nremaining);
790		calculated_tries[i] += tmp;
791		nremaining -= tmp;
792	}
793
794	ridx = 0;
795	for (i = 0; i < nrates && ridx < 8; i++) {
796		/* we register the rates in perfect order */
797		rate = info->control.rates[i].idx;
798		if (info->band == IEEE80211_BAND_5GHZ)
799			rate += 4;
800
801		/* store the count we actually calculated for TX status */
802		info->control.rates[i].count = calculated_tries[i];
803
804		rc_flags = info->control.rates[i].flags;
805		if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
806			rate |= 0x10;
807			cts_rate |= 0x10;
808		}
809		if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
810			burst_allowed = false;
811			rate |= 0x40;
812		} else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
813			rate |= 0x20;
814			burst_allowed = false;
815		}
816		for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
817			txhdr->rateset[ridx] = rate;
818			ridx++;
819		}
820	}
821
822	if (burst_allowed)
823		hdr_flags |= P54_HDR_FLAG_DATA_OUT_BURST;
824
825	/* TODO: enable bursting */
826	hdr->flags = cpu_to_le16(hdr_flags);
827	hdr->tries = ridx;
828	txhdr->rts_rate_idx = 0;
829	if (info->control.hw_key) {
830		txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
831		txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
832		memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
833		if (info->control.hw_key->alg == ALG_TKIP) {
834			/* reserve space for the MIC key */
835			len += 8;
836			memcpy(skb_put(skb, 8), &(info->control.hw_key->key
837				[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
838		}
839		/* reserve some space for ICV */
840		len += info->control.hw_key->icv_len;
841		memset(skb_put(skb, info->control.hw_key->icv_len), 0,
842		       info->control.hw_key->icv_len);
843	} else {
844		txhdr->key_type = 0;
845		txhdr->key_len = 0;
846	}
847	txhdr->crypt_offset = crypt_offset;
848	txhdr->hw_queue = queue;
849	txhdr->backlog = priv->tx_stats[queue].len - 1;
850	memset(txhdr->durations, 0, sizeof(txhdr->durations));
851	txhdr->tx_antenna = ((info->antenna_sel_tx == 0) ?
852		2 : info->antenna_sel_tx - 1) & priv->tx_diversity_mask;
853	if (priv->rxhw == 5) {
854		txhdr->longbow.cts_rate = cts_rate;
855		txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
856	} else {
857		txhdr->normal.output_power = priv->output_power;
858		txhdr->normal.cts_rate = cts_rate;
859	}
860	if (padding)
861		txhdr->align[0] = padding;
862
863	hdr->len = cpu_to_le16(len);
864	/* modifies skb->cb and with it info, so must be last! */
865	p54info = (void *) info->rate_driver_data;
866	p54info->extra_len = extra_len;
867
868	p54_tx(priv, skb);
869	return NETDEV_TX_OK;
870}
871