blob: 44a3bd4b0f43c5258d04db670f62b2ae4ec6a828 [file] [log] [blame]
Christian Lamparter0a5fb842009-06-23 10:39:12 -05001/*
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);
Joe Perchesc96c31e2010-07-26 14:39:58 -070041 wiphy_debug(priv->hw->wiphy, "/ --- tx queue dump (%d entries) ---\n",
42 skb_queue_len(&priv->tx_queue));
Christian Lamparter0a5fb842009-06-23 10:39:12 -050043
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;
Joe Perchesc96c31e2010-07-26 14:39:58 -070051 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);
Christian Lamparter0a5fb842009-06-23 10:39:12 -050059
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);
Joe Perchesc96c31e2010-07-26 14:39:58 -070065 wiphy_debug(priv->hw->wiphy,
66 "\\ --- [free: %d], largest free block: %d ---\n",
67 free, largest_hole);
Christian Lamparter0a5fb842009-06-23 10:39:12 -050068 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
Christian Lamparter0a5fb842009-06-23 10:39:12 -050092 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
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500113 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;
Christian Lamparter46df10a2009-07-16 20:03:47 +0200139 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
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500144 __skb_queue_after(&priv->tx_queue, target_skb, skb);
145 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500146 return 0;
147}
148
149static void p54_tx_pending(struct p54_common *priv)
150{
151 struct sk_buff *skb;
152 int ret;
153
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500154 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{
Kalle Valo97e93fc2010-02-07 10:21:46 +0200188 struct p54_tx_queue_stats *queue;
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500189 unsigned long flags;
190
Darren Jenkins088ea182010-02-17 23:40:15 +1100191 if (WARN_ON(p54_queue >= P54_QUEUE_NUM))
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500192 return -EINVAL;
193
194 queue = &priv->tx_stats[p54_queue];
195
196 spin_lock_irqsave(&priv->tx_stats_lock, flags);
Christian Lamparter2ffa5fe2009-07-06 15:18:13 +0200197 if (unlikely(queue->len >= queue->limit && IS_QOS_QUEUE(p54_queue))) {
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500198 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{
Christian Lamparter12f49a72009-07-16 20:03:17 +0200217 if (IS_DATA_FRAME(skb)) {
Christian Lamparter2ffa5fe2009-07-06 15:18:13 +0200218 unsigned long flags;
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500219
Christian Lamparter2ffa5fe2009-07-06 15:18:13 +0200220 spin_lock_irqsave(&priv->tx_stats_lock, flags);
Christian Lamparter46df10a2009-07-16 20:03:47 +0200221 priv->tx_stats[GET_HW_QUEUE(skb)].len--;
Christian Lamparter2ffa5fe2009-07-06 15:18:13 +0200222 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
Christian Lamparter46df10a2009-07-16 20:03:47 +0200223
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 }
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500231 }
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{
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500270 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{
Christian Lamparter45f7e312010-08-24 23:29:05 +0200276 if (priv->rxhw != 5) {
Christian Lamparter7a047f42011-02-12 22:32:49 +0100277 return ((rssi * priv->cur_rssi->mul) / 64 +
278 priv->cur_rssi->add) / 4;
Christian Lamparter45f7e312010-08-24 23:29:05 +0200279 } else {
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500280 /*
281 * TODO: find the correct formula
282 */
Christian Lamparter45f7e312010-08-24 23:29:05 +0200283 return rssi / 2 - 110;
284 }
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500285}
286
Christian Lampartere0f114e2009-07-07 19:08:07 +0200287/*
288 * Even if the firmware is capable of dealing with incoming traffic,
289 * while dozing, we have to prepared in case mac80211 uses PS-POLL
290 * to retrieve outstanding frames from our AP.
291 * (see comment in net/mac80211/mlme.c @ line 1993)
292 */
293static void p54_pspoll_workaround(struct p54_common *priv, struct sk_buff *skb)
294{
295 struct ieee80211_hdr *hdr = (void *) skb->data;
296 struct ieee80211_tim_ie *tim_ie;
297 u8 *tim;
298 u8 tim_len;
299 bool new_psm;
300
301 /* only beacons have a TIM IE */
302 if (!ieee80211_is_beacon(hdr->frame_control))
303 return;
304
305 if (!priv->aid)
306 return;
307
308 /* only consider beacons from the associated BSSID */
309 if (compare_ether_addr(hdr->addr3, priv->bssid))
310 return;
311
312 tim = p54_find_ie(skb, WLAN_EID_TIM);
313 if (!tim)
314 return;
315
316 tim_len = tim[1];
317 tim_ie = (struct ieee80211_tim_ie *) &tim[2];
318
319 new_psm = ieee80211_check_tim(tim_ie, tim_len, priv->aid);
320 if (new_psm != priv->powersave_override) {
321 priv->powersave_override = new_psm;
322 p54_set_ps(priv);
323 }
324}
325
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500326static int p54_rx_data(struct p54_common *priv, struct sk_buff *skb)
327{
328 struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
329 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
330 u16 freq = le16_to_cpu(hdr->freq);
331 size_t header_len = sizeof(*hdr);
332 u32 tsf32;
333 u8 rate = hdr->rate & 0xf;
334
335 /*
336 * If the device is in a unspecified state we have to
337 * ignore all data frames. Else we could end up with a
338 * nasty crash.
339 */
340 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
341 return 0;
342
343 if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD)))
344 return 0;
345
346 if (hdr->decrypt_status == P54_DECRYPT_OK)
347 rx_status->flag |= RX_FLAG_DECRYPTED;
348 if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
349 (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
350 rx_status->flag |= RX_FLAG_MMIC_ERROR;
351
352 rx_status->signal = p54_rssi_to_dbm(priv, hdr->rssi);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500353 if (hdr->rate & 0x10)
354 rx_status->flag |= RX_FLAG_SHORTPRE;
355 if (priv->hw->conf.channel->band == IEEE80211_BAND_5GHZ)
356 rx_status->rate_idx = (rate < 4) ? 0 : rate - 4;
357 else
358 rx_status->rate_idx = rate;
359
360 rx_status->freq = freq;
361 rx_status->band = priv->hw->conf.channel->band;
362 rx_status->antenna = hdr->antenna;
363
364 tsf32 = le32_to_cpu(hdr->tsf32);
365 if (tsf32 < priv->tsf_low32)
366 priv->tsf_high32++;
367 rx_status->mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
368 priv->tsf_low32 = tsf32;
369
Johannes Berg6ebacbb2011-02-23 15:06:08 +0100370 rx_status->flag |= RX_FLAG_MACTIME_MPDU;
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500371
372 if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
373 header_len += hdr->align[0];
374
375 skb_pull(skb, header_len);
376 skb_trim(skb, le16_to_cpu(hdr->len));
Christian Lampartere0f114e2009-07-07 19:08:07 +0200377 if (unlikely(priv->hw->conf.flags & IEEE80211_CONF_PS))
378 p54_pspoll_workaround(priv, skb);
379
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500380 ieee80211_rx_irqsafe(priv->hw, skb);
381
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400382 ieee80211_queue_delayed_work(priv->hw, &priv->work,
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500383 msecs_to_jiffies(P54_STATISTICS_UPDATE));
384
385 return -1;
386}
387
388static void p54_rx_frame_sent(struct p54_common *priv, struct sk_buff *skb)
389{
390 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
391 struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
392 struct ieee80211_tx_info *info;
393 struct p54_hdr *entry_hdr;
394 struct p54_tx_data *entry_data;
395 struct sk_buff *entry;
396 unsigned int pad = 0, frame_len;
397 int count, idx;
398
399 entry = p54_find_and_unlink_skb(priv, hdr->req_id);
400 if (unlikely(!entry))
401 return ;
402
403 frame_len = entry->len;
404 info = IEEE80211_SKB_CB(entry);
405 entry_hdr = (struct p54_hdr *) entry->data;
406 entry_data = (struct p54_tx_data *) entry_hdr->data;
407 priv->stats.dot11ACKFailureCount += payload->tries - 1;
408
409 /*
410 * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are
411 * generated by the driver. Therefore tx_status is bogus
412 * and we don't want to confuse the mac80211 stack.
413 */
414 if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500415 dev_kfree_skb_any(entry);
416 return ;
417 }
418
419 /*
420 * Clear manually, ieee80211_tx_info_clear_status would
421 * clear the counts too and we need them.
422 */
423 memset(&info->status.ampdu_ack_len, 0,
424 sizeof(struct ieee80211_tx_info) -
425 offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
426 BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
427 status.ampdu_ack_len) != 23);
428
429 if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
430 pad = entry_data->align[0];
431
432 /* walk through the rates array and adjust the counts */
433 count = payload->tries;
434 for (idx = 0; idx < 4; idx++) {
435 if (count >= info->status.rates[idx].count) {
436 count -= info->status.rates[idx].count;
437 } else if (count > 0) {
438 info->status.rates[idx].count = count;
439 count = 0;
440 } else {
441 info->status.rates[idx].idx = -1;
442 info->status.rates[idx].count = 0;
443 }
444 }
445
446 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
Christian Lamparterf880c202010-08-24 22:54:05 +0200447 !(payload->status & P54_TX_FAILED))
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500448 info->flags |= IEEE80211_TX_STAT_ACK;
449 if (payload->status & P54_TX_PSM_CANCELLED)
450 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
451 info->status.ack_signal = p54_rssi_to_dbm(priv,
452 (int)payload->ack_rssi);
453
454 /* Undo all changes to the frame. */
455 switch (entry_data->key_type) {
456 case P54_CRYPTO_TKIPMICHAEL: {
457 u8 *iv = (u8 *)(entry_data->align + pad +
458 entry_data->crypt_offset);
459
460 /* Restore the original TKIP IV. */
461 iv[2] = iv[0];
462 iv[0] = iv[1];
463 iv[1] = (iv[0] | 0x20) & 0x7f; /* WEPSeed - 8.3.2.2 */
464
465 frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */
466 break;
467 }
468 case P54_CRYPTO_AESCCMP:
469 frame_len -= 8; /* remove CCMP_MIC */
470 break;
471 case P54_CRYPTO_WEP:
472 frame_len -= 4; /* remove WEP_ICV */
473 break;
474 }
475
476 skb_trim(entry, frame_len);
477 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
478 ieee80211_tx_status_irqsafe(priv->hw, entry);
479}
480
481static void p54_rx_eeprom_readback(struct p54_common *priv,
482 struct sk_buff *skb)
483{
484 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
485 struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
486 struct sk_buff *tmp;
487
488 if (!priv->eeprom)
489 return ;
490
491 if (priv->fw_var >= 0x509) {
492 memcpy(priv->eeprom, eeprom->v2.data,
493 le16_to_cpu(eeprom->v2.len));
494 } else {
495 memcpy(priv->eeprom, eeprom->v1.data,
496 le16_to_cpu(eeprom->v1.len));
497 }
498
499 priv->eeprom = NULL;
500 tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500501 dev_kfree_skb_any(tmp);
502 complete(&priv->eeprom_comp);
503}
504
505static void p54_rx_stats(struct p54_common *priv, struct sk_buff *skb)
506{
507 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
508 struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
509 struct sk_buff *tmp;
Christian Lamparter0d781562011-08-20 01:53:59 +0200510 struct ieee80211_channel *chan;
511 unsigned int i, rssi, tx, cca, dtime, dtotal, dcca, dtx, drssi, unit;
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500512 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
Christian Lamparter0d781562011-08-20 01:53:59 +0200528 /*
529 * STSW450X LMAC API page 26 - 3.8 Statistics
530 * "The exact measurement period can be derived from the
531 * timestamp member".
532 */
533 dtime = tsf32 - priv->survey_raw.timestamp;
534
535 /*
536 * STSW450X LMAC API page 26 - 3.8.1 Noise histogram
537 * The LMAC samples RSSI, CCA and transmit state at regular
538 * periods (typically 8 times per 1k [as in 1024] usec).
539 */
540 cca = le32_to_cpu(stats->sample_cca);
541 tx = le32_to_cpu(stats->sample_tx);
542 rssi = 0;
543 for (i = 0; i < ARRAY_SIZE(stats->sample_noise); i++)
544 rssi += le32_to_cpu(stats->sample_noise[i]);
545
546 dcca = cca - priv->survey_raw.cached_cca;
547 drssi = rssi - priv->survey_raw.cached_rssi;
548 dtx = tx - priv->survey_raw.cached_tx;
549 dtotal = dcca + drssi + dtx;
550
551 /*
552 * update statistics when more than a second is over since the
553 * last call, or when a update is badly needed.
554 */
555 if (dtotal && (priv->update_stats || dtime >= USEC_PER_SEC) &&
556 dtime >= dtotal) {
557 priv->survey_raw.timestamp = tsf32;
558 priv->update_stats = false;
559 unit = dtime / dtotal;
560
561 if (dcca) {
562 priv->survey_raw.cca += dcca * unit;
563 priv->survey_raw.cached_cca = cca;
564 }
565 if (dtx) {
566 priv->survey_raw.tx += dtx * unit;
567 priv->survey_raw.cached_tx = tx;
568 }
569 if (drssi) {
570 priv->survey_raw.rssi += drssi * unit;
571 priv->survey_raw.cached_rssi = rssi;
572 }
573
574 /* 1024 usec / 8 times = 128 usec / time */
575 if (!(priv->phy_ps || priv->phy_idle))
576 priv->survey_raw.active += dtotal * unit;
577 else
578 priv->survey_raw.active += (dcca + dtx) * unit;
579 }
580
581 chan = priv->curchan;
582 if (chan) {
583 struct survey_info *survey = &priv->survey[chan->hw_value];
584 survey->noise = clamp_t(s8, priv->noise, -128, 127);
585 survey->channel_time = priv->survey_raw.active / 1024;
586 survey->channel_time_tx = priv->survey_raw.tx / 1024;
587 survey->channel_time_busy = priv->survey_raw.cca / 1024 +
588 survey->channel_time_tx;
589 }
590
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500591 tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500592 dev_kfree_skb_any(tmp);
Christian Lamparter0d781562011-08-20 01:53:59 +0200593 complete(&priv->stat_comp);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500594}
595
596static void p54_rx_trap(struct p54_common *priv, struct sk_buff *skb)
597{
598 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
599 struct p54_trap *trap = (struct p54_trap *) hdr->data;
600 u16 event = le16_to_cpu(trap->event);
601 u16 freq = le16_to_cpu(trap->frequency);
602
603 switch (event) {
604 case P54_TRAP_BEACON_TX:
605 break;
606 case P54_TRAP_RADAR:
Joe Perches5db55842010-08-11 19:11:19 -0700607 wiphy_info(priv->hw->wiphy, "radar (freq:%d MHz)\n", freq);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500608 break;
609 case P54_TRAP_NO_BEACON:
610 if (priv->vif)
611 ieee80211_beacon_loss(priv->vif);
612 break;
613 case P54_TRAP_SCAN:
614 break;
615 case P54_TRAP_TBTT:
616 break;
617 case P54_TRAP_TIMER:
618 break;
Christian Lamparter6208f8b2009-08-07 19:39:05 +0200619 case P54_TRAP_FAA_RADIO_OFF:
620 wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
621 break;
622 case P54_TRAP_FAA_RADIO_ON:
623 wiphy_rfkill_set_hw_state(priv->hw->wiphy, false);
624 break;
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500625 default:
Joe Perchesc96c31e2010-07-26 14:39:58 -0700626 wiphy_info(priv->hw->wiphy, "received event:%x freq:%d\n",
627 event, freq);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500628 break;
629 }
630}
631
632static int p54_rx_control(struct p54_common *priv, struct sk_buff *skb)
633{
634 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
635
636 switch (le16_to_cpu(hdr->type)) {
637 case P54_CONTROL_TYPE_TXDONE:
638 p54_rx_frame_sent(priv, skb);
639 break;
640 case P54_CONTROL_TYPE_TRAP:
641 p54_rx_trap(priv, skb);
642 break;
643 case P54_CONTROL_TYPE_BBP:
644 break;
645 case P54_CONTROL_TYPE_STAT_READBACK:
646 p54_rx_stats(priv, skb);
647 break;
648 case P54_CONTROL_TYPE_EEPROM_READBACK:
649 p54_rx_eeprom_readback(priv, skb);
650 break;
651 default:
Joe Perchesc96c31e2010-07-26 14:39:58 -0700652 wiphy_debug(priv->hw->wiphy,
653 "not handling 0x%02x type control frame\n",
654 le16_to_cpu(hdr->type));
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500655 break;
656 }
657 return 0;
658}
659
660/* returns zero if skb can be reused */
661int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
662{
663 struct p54_common *priv = dev->priv;
664 u16 type = le16_to_cpu(*((__le16 *)skb->data));
665
666 if (type & P54_HDR_FLAG_CONTROL)
667 return p54_rx_control(priv, skb);
668 else
669 return p54_rx_data(priv, skb);
670}
671EXPORT_SYMBOL_GPL(p54_rx);
672
673static void p54_tx_80211_header(struct p54_common *priv, struct sk_buff *skb,
674 struct ieee80211_tx_info *info, u8 *queue,
675 u32 *extra_len, u16 *flags, u16 *aid,
676 bool *burst_possible)
677{
678 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
679
680 if (ieee80211_is_data_qos(hdr->frame_control))
681 *burst_possible = true;
682 else
683 *burst_possible = false;
684
Christian Lamparter3b5c5822011-01-06 23:47:52 +0100685 if (!(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500686 *flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
687
Johannes Berg3fa52052009-07-24 13:23:09 +0200688 if (info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE)
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500689 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
690
Christian Lamparter90d6f922009-08-20 20:22:01 +0200691 if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
692 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
693
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500694 *queue = skb_get_queue_mapping(skb) + P54_QUEUE_DATA;
695
696 switch (priv->mode) {
697 case NL80211_IFTYPE_MONITOR:
698 /*
699 * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
700 * every frame in promiscuous/monitor mode.
701 * see STSW45x0C LMAC API - page 12.
702 */
703 *aid = 0;
704 *flags |= P54_HDR_FLAG_DATA_OUT_PROMISC;
705 break;
706 case NL80211_IFTYPE_STATION:
707 *aid = 1;
708 break;
709 case NL80211_IFTYPE_AP:
710 case NL80211_IFTYPE_ADHOC:
711 case NL80211_IFTYPE_MESH_POINT:
712 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
713 *aid = 0;
714 *queue = P54_QUEUE_CAB;
715 return;
716 }
717
718 if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
719 if (ieee80211_is_probe_resp(hdr->frame_control)) {
720 *aid = 0;
721 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
722 P54_HDR_FLAG_DATA_OUT_NOCANCEL;
723 return;
724 } else if (ieee80211_is_beacon(hdr->frame_control)) {
725 *aid = 0;
726
727 if (info->flags & IEEE80211_TX_CTL_INJECTED) {
728 /*
729 * Injecting beacons on top of a AP is
730 * not a good idea... nevertheless,
731 * it should be doable.
732 */
733
734 return;
735 }
736
737 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
738 *queue = P54_QUEUE_BEACON;
739 *extra_len = IEEE80211_MAX_TIM_LEN;
740 return;
741 }
742 }
743
744 if (info->control.sta)
745 *aid = info->control.sta->aid;
746 break;
747 }
748}
749
Johannes Berg97359d12010-08-10 09:46:38 +0200750static u8 p54_convert_algo(u32 cipher)
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500751{
Johannes Berg97359d12010-08-10 09:46:38 +0200752 switch (cipher) {
753 case WLAN_CIPHER_SUITE_WEP40:
754 case WLAN_CIPHER_SUITE_WEP104:
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500755 return P54_CRYPTO_WEP;
Johannes Berg97359d12010-08-10 09:46:38 +0200756 case WLAN_CIPHER_SUITE_TKIP:
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500757 return P54_CRYPTO_TKIPMICHAEL;
Johannes Berg97359d12010-08-10 09:46:38 +0200758 case WLAN_CIPHER_SUITE_CCMP:
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500759 return P54_CRYPTO_AESCCMP;
760 default:
761 return 0;
762 }
763}
764
Johannes Berg7bb45682011-02-24 14:42:06 +0100765void p54_tx_80211(struct ieee80211_hw *dev, struct sk_buff *skb)
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500766{
767 struct p54_common *priv = dev->priv;
768 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
769 struct p54_tx_info *p54info;
770 struct p54_hdr *hdr;
771 struct p54_tx_data *txhdr;
Jason Contia6756da2011-04-07 21:09:57 +0200772 unsigned int padding, len, extra_len = 0;
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500773 int i, j, ridx;
774 u16 hdr_flags = 0, aid = 0;
775 u8 rate, queue = 0, crypt_offset = 0;
776 u8 cts_rate = 0x20;
777 u8 rc_flags;
778 u8 calculated_tries[4];
779 u8 nrates = 0, nremaining = 8;
780 bool burst_allowed = false;
781
782 p54_tx_80211_header(priv, skb, info, &queue, &extra_len,
783 &hdr_flags, &aid, &burst_allowed);
784
785 if (p54_tx_qos_accounting_alloc(priv, skb, queue)) {
Johannes Berg7bb45682011-02-24 14:42:06 +0100786 dev_kfree_skb_any(skb);
787 return;
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500788 }
789
790 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
791 len = skb->len;
792
793 if (info->control.hw_key) {
794 crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
Johannes Berg97359d12010-08-10 09:46:38 +0200795 if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500796 u8 *iv = (u8 *)(skb->data + crypt_offset);
797 /*
798 * The firmware excepts that the IV has to have
799 * this special format
800 */
801 iv[1] = iv[0];
802 iv[0] = iv[2];
803 iv[2] = 0;
804 }
805 }
806
807 txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
808 hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
809
810 if (padding)
811 hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
812 hdr->type = cpu_to_le16(aid);
813 hdr->rts_tries = info->control.rates[0].count;
814
815 /*
816 * we register the rates in perfect order, and
817 * RTS/CTS won't happen on 5 GHz
818 */
819 cts_rate = info->control.rts_cts_rate_idx;
820
821 memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
822
823 /* see how many rates got used */
824 for (i = 0; i < dev->max_rates; i++) {
825 if (info->control.rates[i].idx < 0)
826 break;
827 nrates++;
828 }
829
830 /* limit tries to 8/nrates per rate */
831 for (i = 0; i < nrates; i++) {
832 /*
833 * The magic expression here is equivalent to 8/nrates for
834 * all values that matter, but avoids division and jumps.
835 * Note that nrates can only take the values 1 through 4.
836 */
837 calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
838 info->control.rates[i].count);
839 nremaining -= calculated_tries[i];
840 }
841
842 /* if there are tries left, distribute from back to front */
843 for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
844 int tmp = info->control.rates[i].count - calculated_tries[i];
845
846 if (tmp <= 0)
847 continue;
848 /* RC requested more tries at this rate */
849
850 tmp = min_t(int, tmp, nremaining);
851 calculated_tries[i] += tmp;
852 nremaining -= tmp;
853 }
854
855 ridx = 0;
856 for (i = 0; i < nrates && ridx < 8; i++) {
857 /* we register the rates in perfect order */
858 rate = info->control.rates[i].idx;
859 if (info->band == IEEE80211_BAND_5GHZ)
860 rate += 4;
861
862 /* store the count we actually calculated for TX status */
863 info->control.rates[i].count = calculated_tries[i];
864
865 rc_flags = info->control.rates[i].flags;
866 if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
867 rate |= 0x10;
868 cts_rate |= 0x10;
869 }
870 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
871 burst_allowed = false;
872 rate |= 0x40;
873 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
874 rate |= 0x20;
875 burst_allowed = false;
876 }
877 for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
878 txhdr->rateset[ridx] = rate;
879 ridx++;
880 }
881 }
882
883 if (burst_allowed)
884 hdr_flags |= P54_HDR_FLAG_DATA_OUT_BURST;
885
886 /* TODO: enable bursting */
887 hdr->flags = cpu_to_le16(hdr_flags);
888 hdr->tries = ridx;
889 txhdr->rts_rate_idx = 0;
890 if (info->control.hw_key) {
Johannes Berg97359d12010-08-10 09:46:38 +0200891 txhdr->key_type = p54_convert_algo(info->control.hw_key->cipher);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500892 txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
893 memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
Johannes Berg97359d12010-08-10 09:46:38 +0200894 if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500895 /* reserve space for the MIC key */
896 len += 8;
897 memcpy(skb_put(skb, 8), &(info->control.hw_key->key
898 [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
899 }
900 /* reserve some space for ICV */
901 len += info->control.hw_key->icv_len;
902 memset(skb_put(skb, info->control.hw_key->icv_len), 0,
903 info->control.hw_key->icv_len);
904 } else {
905 txhdr->key_type = 0;
906 txhdr->key_len = 0;
907 }
908 txhdr->crypt_offset = crypt_offset;
909 txhdr->hw_queue = queue;
910 txhdr->backlog = priv->tx_stats[queue].len - 1;
911 memset(txhdr->durations, 0, sizeof(txhdr->durations));
912 txhdr->tx_antenna = ((info->antenna_sel_tx == 0) ?
913 2 : info->antenna_sel_tx - 1) & priv->tx_diversity_mask;
914 if (priv->rxhw == 5) {
915 txhdr->longbow.cts_rate = cts_rate;
916 txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
917 } else {
918 txhdr->normal.output_power = priv->output_power;
919 txhdr->normal.cts_rate = cts_rate;
920 }
921 if (padding)
922 txhdr->align[0] = padding;
923
924 hdr->len = cpu_to_le16(len);
925 /* modifies skb->cb and with it info, so must be last! */
926 p54info = (void *) info->rate_driver_data;
927 p54info->extra_len = extra_len;
928
929 p54_tx(priv, skb);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500930}