compat-wireless-2010-03-10
[pandora-wifi.git] / drivers / net / wireless / wl12xx / wl1271_tx.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26
27 #include "wl1271.h"
28 #include "wl1271_spi.h"
29 #include "wl1271_io.h"
30 #include "wl1271_reg.h"
31 #include "wl1271_ps.h"
32 #include "wl1271_tx.h"
33
34 static int wl1271_tx_id(struct wl1271 *wl, struct sk_buff *skb)
35 {
36         int i;
37         for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
38                 if (wl->tx_frames[i] == NULL) {
39                         wl->tx_frames[i] = skb;
40                         return i;
41                 }
42
43         return -EBUSY;
44 }
45
46 static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra)
47 {
48         struct wl1271_tx_hw_descr *desc;
49         u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
50         u32 total_blocks, excluded;
51         int id, ret = -EBUSY;
52
53         /* allocate free identifier for the packet */
54         id = wl1271_tx_id(wl, skb);
55         if (id < 0)
56                 return id;
57
58         /* approximate the number of blocks required for this packet
59            in the firmware */
60         /* FIXME: try to figure out what is done here and make it cleaner */
61         total_blocks = (total_len + 20) >> TX_HW_BLOCK_SHIFT_DIV;
62         excluded = (total_blocks << 2) + ((total_len + 20) & 0xff) + 34;
63         total_blocks += (excluded > 252) ? 2 : 1;
64         total_blocks += TX_HW_BLOCK_SPARE;
65
66         if (total_blocks <= wl->tx_blocks_available) {
67                 desc = (struct wl1271_tx_hw_descr *)skb_push(
68                         skb, total_len - skb->len);
69
70                 desc->extra_mem_blocks = TX_HW_BLOCK_SPARE;
71                 desc->total_mem_blocks = total_blocks;
72                 desc->id = id;
73
74                 wl->tx_blocks_available -= total_blocks;
75
76                 ret = 0;
77
78                 wl1271_debug(DEBUG_TX,
79                              "tx_allocate: size: %d, blocks: %d, id: %d",
80                              total_len, total_blocks, id);
81         } else
82                 wl->tx_frames[id] = NULL;
83
84         return ret;
85 }
86
87 static int wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
88                               u32 extra, struct ieee80211_tx_info *control)
89 {
90         struct wl1271_tx_hw_descr *desc;
91         int pad, ac;
92         u16 tx_attr;
93
94         desc = (struct wl1271_tx_hw_descr *) skb->data;
95
96         /* relocate space for security header */
97         if (extra) {
98                 void *framestart = skb->data + sizeof(*desc);
99                 u16 fc = *(u16 *)(framestart + extra);
100                 int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
101                 memmove(framestart, framestart + extra, hdrlen);
102         }
103
104         /* configure packet life time */
105         desc->start_time = cpu_to_le32(jiffies_to_usecs(jiffies) -
106                                        wl->time_offset);
107         desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
108
109         /* configure the tx attributes */
110         tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
111
112         /* queue */
113         ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
114         desc->tid = wl1271_tx_ac_to_tid(ac);
115
116         desc->aid = TX_HW_DEFAULT_AID;
117         desc->reserved = 0;
118
119         /* align the length (and store in terms of words) */
120         pad = WL1271_TX_ALIGN(skb->len);
121         desc->length = cpu_to_le16(pad >> 2);
122
123         /* calculate number of padding bytes */
124         pad = pad - skb->len;
125         tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
126
127         /* if the packets are destined for AP (have a STA entry) send them
128            with AP rate policies, otherwise use default basic rates */
129         if (control->control.sta)
130                 tx_attr |= ACX_TX_AP_FULL_RATE << TX_HW_ATTR_OFST_RATE_POLICY;
131
132         desc->tx_attr = cpu_to_le16(tx_attr);
133
134         wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d", pad);
135         return 0;
136 }
137
138 static int wl1271_tx_send_packet(struct wl1271 *wl, struct sk_buff *skb,
139                                  struct ieee80211_tx_info *control)
140 {
141
142         struct wl1271_tx_hw_descr *desc;
143         int len;
144
145         /* FIXME: This is a workaround for getting non-aligned packets.
146            This happens at least with EAPOL packets from the user space.
147            Our DMA requires packets to be aligned on a 4-byte boundary.
148         */
149         if (unlikely((long)skb->data & 0x03)) {
150                 int offset = (4 - (long)skb->data) & 0x03;
151                 wl1271_debug(DEBUG_TX, "skb offset %d", offset);
152
153                 /* check whether the current skb can be used */
154                 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
155                         unsigned char *src = skb->data;
156
157                         /* align the buffer on a 4-byte boundary */
158                         skb_reserve(skb, offset);
159                         memmove(skb->data, src, skb->len);
160                 } else {
161                         wl1271_info("No handler, fixme!");
162                         return -EINVAL;
163                 }
164         }
165
166         len = WL1271_TX_ALIGN(skb->len);
167
168         /* perform a fixed address block write with the packet */
169         wl1271_write(wl, WL1271_SLV_MEM_DATA, skb->data, len, true);
170
171         /* write packet new counter into the write access register */
172         wl->tx_packets_count++;
173         wl1271_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
174
175         desc = (struct wl1271_tx_hw_descr *) skb->data;
176         wl1271_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u (%u words)",
177                      desc->id, skb, len, desc->length);
178
179         return 0;
180 }
181
182 /* caller must hold wl->mutex */
183 static int wl1271_tx_frame(struct wl1271 *wl, struct sk_buff *skb)
184 {
185         struct ieee80211_tx_info *info;
186         u32 extra = 0;
187         int ret = 0;
188         u8 idx;
189
190         if (!skb)
191                 return -EINVAL;
192
193         info = IEEE80211_SKB_CB(skb);
194
195         if (info->control.hw_key &&
196             info->control.hw_key->alg == ALG_TKIP)
197                 extra = WL1271_TKIP_IV_SPACE;
198
199         if (info->control.hw_key) {
200                 idx = info->control.hw_key->hw_key_idx;
201
202                 /* FIXME: do we have to do this if we're not using WEP? */
203                 if (unlikely(wl->default_key != idx)) {
204                         ret = wl1271_cmd_set_default_wep_key(wl, idx);
205                         if (ret < 0)
206                                 return ret;
207                         wl->default_key = idx;
208                 }
209         }
210
211         ret = wl1271_tx_allocate(wl, skb, extra);
212         if (ret < 0)
213                 return ret;
214
215         ret = wl1271_tx_fill_hdr(wl, skb, extra, info);
216         if (ret < 0)
217                 return ret;
218
219         ret = wl1271_tx_send_packet(wl, skb, info);
220         if (ret < 0)
221                 return ret;
222
223         return ret;
224 }
225
226 static u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
227 {
228         struct ieee80211_supported_band *band;
229         u32 enabled_rates = 0;
230         int bit;
231
232         band = wl->hw->wiphy->bands[wl->band];
233         for (bit = 0; bit < band->n_bitrates; bit++) {
234                 if (rate_set & 0x1)
235                         enabled_rates |= band->bitrates[bit].hw_value;
236                 rate_set >>= 1;
237         }
238
239         return enabled_rates;
240 }
241
242 void wl1271_tx_work(struct work_struct *work)
243 {
244         struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
245         struct sk_buff *skb;
246         bool woken_up = false;
247         u32 sta_rates = 0;
248         int ret;
249
250         /* check if the rates supported by the AP have changed */
251         if (unlikely(test_and_clear_bit(WL1271_FLAG_STA_RATES_CHANGED,
252                                         &wl->flags))) {
253                 unsigned long flags;
254                 spin_lock_irqsave(&wl->wl_lock, flags);
255                 sta_rates = wl->sta_rate_set;
256                 spin_unlock_irqrestore(&wl->wl_lock, flags);
257         }
258
259         mutex_lock(&wl->mutex);
260
261         if (unlikely(wl->state == WL1271_STATE_OFF))
262                 goto out;
263
264         /* if rates have changed, re-configure the rate policy */
265         if (unlikely(sta_rates)) {
266                 wl->rate_set = wl1271_tx_enabled_rates_get(wl, sta_rates);
267                 wl1271_acx_rate_policies(wl);
268         }
269
270         while ((skb = skb_dequeue(&wl->tx_queue))) {
271                 if (!woken_up) {
272                         ret = wl1271_ps_elp_wakeup(wl, false);
273                         if (ret < 0)
274                                 goto out;
275                         woken_up = true;
276                 }
277
278                 ret = wl1271_tx_frame(wl, skb);
279                 if (ret == -EBUSY) {
280                         /* firmware buffer is full, stop queues */
281                         wl1271_debug(DEBUG_TX, "tx_work: fw buffer full, "
282                                      "stop queues");
283                         ieee80211_stop_queues(wl->hw);
284                         set_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
285                         skb_queue_head(&wl->tx_queue, skb);
286                         goto out;
287                 } else if (ret < 0) {
288                         dev_kfree_skb(skb);
289                         goto out;
290                 } else if (test_and_clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED,
291                                               &wl->flags)) {
292                         /* firmware buffer has space, restart queues */
293                         wl1271_debug(DEBUG_TX,
294                                      "complete_packet: waking queues");
295                         ieee80211_wake_queues(wl->hw);
296                 }
297         }
298
299 out:
300         if (woken_up)
301                 wl1271_ps_elp_sleep(wl);
302
303         mutex_unlock(&wl->mutex);
304 }
305
306 static void wl1271_tx_complete_packet(struct wl1271 *wl,
307                                       struct wl1271_tx_hw_res_descr *result)
308 {
309         struct ieee80211_tx_info *info;
310         struct sk_buff *skb;
311         u16 seq;
312         int id = result->id;
313
314         /* check for id legality */
315         if (id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL) {
316                 wl1271_warning("TX result illegal id: %d", id);
317                 return;
318         }
319
320         skb = wl->tx_frames[id];
321         info = IEEE80211_SKB_CB(skb);
322
323         /* update packet status */
324         if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
325                 if (result->status == TX_SUCCESS)
326                         info->flags |= IEEE80211_TX_STAT_ACK;
327                 if (result->status & TX_RETRY_EXCEEDED) {
328                         /* FIXME */
329                         /* info->status.excessive_retries = 1; */
330                         wl->stats.excessive_retries++;
331                 }
332         }
333
334         /* FIXME */
335         /* info->status.retry_count = result->ack_failures; */
336         wl->stats.retry_count += result->ack_failures;
337
338         /* update security sequence number */
339         seq = wl->tx_security_seq_16 +
340                 (result->lsb_security_sequence_number -
341                  wl->tx_security_last_seq);
342         wl->tx_security_last_seq = result->lsb_security_sequence_number;
343
344         if (seq < wl->tx_security_seq_16)
345                 wl->tx_security_seq_32++;
346         wl->tx_security_seq_16 = seq;
347
348         /* remove private header from packet */
349         skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
350
351         /* remove TKIP header space if present */
352         if (info->control.hw_key &&
353             info->control.hw_key->alg == ALG_TKIP) {
354                 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
355                 memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
356                 skb_pull(skb, WL1271_TKIP_IV_SPACE);
357         }
358
359         wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
360                      " status 0x%x",
361                      result->id, skb, result->ack_failures,
362                      result->rate_class_index, result->status);
363
364         /* return the packet to the stack */
365         ieee80211_tx_status(wl->hw, skb);
366         wl->tx_frames[result->id] = NULL;
367 }
368
369 /* Called upon reception of a TX complete interrupt */
370 void wl1271_tx_complete(struct wl1271 *wl, u32 count)
371 {
372         struct wl1271_acx_mem_map *memmap =
373                 (struct wl1271_acx_mem_map *)wl->target_mem_map;
374         u32 i;
375
376         wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
377
378         /* read the tx results from the chipset */
379         wl1271_read(wl, le32_to_cpu(memmap->tx_result),
380                     wl->tx_res_if, sizeof(*wl->tx_res_if), false);
381
382         /* verify that the result buffer is not getting overrun */
383         if (count > TX_HW_RESULT_QUEUE_LEN) {
384                 wl1271_warning("TX result overflow from chipset: %d", count);
385                 count = TX_HW_RESULT_QUEUE_LEN;
386         }
387
388         /* process the results */
389         for (i = 0; i < count; i++) {
390                 struct wl1271_tx_hw_res_descr *result;
391                 u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
392
393                 /* process the packet */
394                 result =  &(wl->tx_res_if->tx_results_queue[offset]);
395                 wl1271_tx_complete_packet(wl, result);
396
397                 wl->tx_results_count++;
398         }
399
400         /* write host counter to chipset (to ack) */
401         wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
402                        offsetof(struct wl1271_tx_hw_res_if,
403                        tx_result_host_counter),
404                        le32_to_cpu(wl->tx_res_if->tx_result_fw_counter));
405 }
406
407 /* caller must hold wl->mutex */
408 void wl1271_tx_flush(struct wl1271 *wl)
409 {
410         int i;
411         struct sk_buff *skb;
412         struct ieee80211_tx_info *info;
413
414         /* TX failure */
415 /*      control->flags = 0; FIXME */
416
417         while ((skb = skb_dequeue(&wl->tx_queue))) {
418                 info = IEEE80211_SKB_CB(skb);
419
420                 wl1271_debug(DEBUG_TX, "flushing skb 0x%p", skb);
421
422                 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
423                                 continue;
424
425                 ieee80211_tx_status(wl->hw, skb);
426         }
427
428         for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
429                 if (wl->tx_frames[i] != NULL) {
430                         skb = wl->tx_frames[i];
431                         info = IEEE80211_SKB_CB(skb);
432
433                         if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
434                                 continue;
435
436                         ieee80211_tx_status(wl->hw, skb);
437                         wl->tx_frames[i] = NULL;
438                 }
439 }