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