blob: 2e057b0e325706d7a6966e2cd01b038eb5afc853 [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
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"
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020028#include "wl1271_io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030029#include "wl1271_reg.h"
30#include "wl1271_ps.h"
31#include "wl1271_tx.h"
32
33static int wl1271_tx_id(struct wl1271 *wl, struct sk_buff *skb)
34{
35 int i;
Juuso Oikarinenbe7078c2009-10-08 21:56:26 +030036 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030037 if (wl->tx_frames[i] == NULL) {
38 wl->tx_frames[i] = skb;
39 return i;
40 }
41
42 return -EBUSY;
43}
44
45static 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 */
Juuso Oikarinen207347e2009-10-12 15:08:53 +030060 total_blocks = (total_len + 20) >> TX_HW_BLOCK_SHIFT_DIV;
61 excluded = (total_blocks << 2) + ((total_len + 20) & 0xff) + 34;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030062 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
86static 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;
Kalle Valoc6999d82010-02-18 13:25:41 +020090 int pad, ac;
Luciano Coelhod0f63b22009-10-15 10:33:29 +030091 u16 tx_attr;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030092
93 desc = (struct wl1271_tx_hw_descr *) skb->data;
94
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +030095 /* relocate space for security header */
96 if (extra) {
97 void *framestart = skb->data + sizeof(*desc);
98 u16 fc = *(u16 *)(framestart + extra);
Luciano Coelhod0f63b22009-10-15 10:33:29 +030099 int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300100 memmove(framestart, framestart + extra, hdrlen);
101 }
102
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300103 /* configure packet life time */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300104 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);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300107
108 /* configure the tx attributes */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300109 tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
Kalle Valoc6999d82010-02-18 13:25:41 +0200110
111 /* queue */
112 ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
113 desc->tid = wl1271_tx_ac_to_tid(ac);
114
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300115 desc->aid = TX_HW_DEFAULT_AID;
116 desc->reserved = 0;
117
118 /* align the length (and store in terms of words) */
119 pad = WL1271_TX_ALIGN(skb->len);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300120 desc->length = cpu_to_le16(pad >> 2);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300121
122 /* calculate number of padding bytes */
123 pad = pad - skb->len;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300124 tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
125
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200126 /* if the packets are destined for AP (have a STA entry) send them
127 with AP rate policies, otherwise use default basic rates */
128 if (control->control.sta)
129 tx_attr |= ACX_TX_AP_FULL_RATE << TX_HW_ATTR_OFST_RATE_POLICY;
130
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300131 desc->tx_attr = cpu_to_le16(tx_attr);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300132
133 wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d", pad);
134 return 0;
135}
136
137static int wl1271_tx_send_packet(struct wl1271 *wl, struct sk_buff *skb,
138 struct ieee80211_tx_info *control)
139{
140
141 struct wl1271_tx_hw_descr *desc;
142 int len;
143
144 /* FIXME: This is a workaround for getting non-aligned packets.
145 This happens at least with EAPOL packets from the user space.
146 Our DMA requires packets to be aligned on a 4-byte boundary.
147 */
148 if (unlikely((long)skb->data & 0x03)) {
149 int offset = (4 - (long)skb->data) & 0x03;
150 wl1271_debug(DEBUG_TX, "skb offset %d", offset);
151
152 /* check whether the current skb can be used */
153 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
154 unsigned char *src = skb->data;
155
156 /* align the buffer on a 4-byte boundary */
157 skb_reserve(skb, offset);
158 memmove(skb->data, src, skb->len);
159 } else {
160 wl1271_info("No handler, fixme!");
161 return -EINVAL;
162 }
163 }
164
165 len = WL1271_TX_ALIGN(skb->len);
166
167 /* perform a fixed address block write with the packet */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200168 wl1271_write(wl, WL1271_SLV_MEM_DATA, skb->data, len, true);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300169
170 /* write packet new counter into the write access register */
171 wl->tx_packets_count++;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300172
173 desc = (struct wl1271_tx_hw_descr *) skb->data;
174 wl1271_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u (%u words)",
175 desc->id, skb, len, desc->length);
176
177 return 0;
178}
179
180/* caller must hold wl->mutex */
181static int wl1271_tx_frame(struct wl1271 *wl, struct sk_buff *skb)
182{
183 struct ieee80211_tx_info *info;
184 u32 extra = 0;
185 int ret = 0;
186 u8 idx;
187
188 if (!skb)
189 return -EINVAL;
190
191 info = IEEE80211_SKB_CB(skb);
192
193 if (info->control.hw_key &&
194 info->control.hw_key->alg == ALG_TKIP)
195 extra = WL1271_TKIP_IV_SPACE;
196
197 if (info->control.hw_key) {
198 idx = info->control.hw_key->hw_key_idx;
199
200 /* FIXME: do we have to do this if we're not using WEP? */
201 if (unlikely(wl->default_key != idx)) {
202 ret = wl1271_cmd_set_default_wep_key(wl, idx);
203 if (ret < 0)
204 return ret;
Juuso Oikarinenee444cf2010-02-18 13:25:50 +0200205 wl->default_key = idx;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300206 }
207 }
208
209 ret = wl1271_tx_allocate(wl, skb, extra);
210 if (ret < 0)
211 return ret;
212
213 ret = wl1271_tx_fill_hdr(wl, skb, extra, info);
214 if (ret < 0)
215 return ret;
216
217 ret = wl1271_tx_send_packet(wl, skb, info);
218 if (ret < 0)
219 return ret;
220
221 return ret;
222}
223
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200224static u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
225{
226 struct ieee80211_supported_band *band;
227 u32 enabled_rates = 0;
228 int bit;
229
230 band = wl->hw->wiphy->bands[wl->band];
231 for (bit = 0; bit < band->n_bitrates; bit++) {
232 if (rate_set & 0x1)
233 enabled_rates |= band->bitrates[bit].hw_value;
234 rate_set >>= 1;
235 }
236
237 return enabled_rates;
238}
239
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300240void wl1271_tx_work(struct work_struct *work)
241{
242 struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
243 struct sk_buff *skb;
244 bool woken_up = false;
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200245 u32 sta_rates = 0;
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200246 u32 prev_tx_packets_count;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300247 int ret;
248
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200249 /* check if the rates supported by the AP have changed */
250 if (unlikely(test_and_clear_bit(WL1271_FLAG_STA_RATES_CHANGED,
251 &wl->flags))) {
252 unsigned long flags;
253 spin_lock_irqsave(&wl->wl_lock, flags);
254 sta_rates = wl->sta_rate_set;
255 spin_unlock_irqrestore(&wl->wl_lock, flags);
256 }
257
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300258 mutex_lock(&wl->mutex);
259
260 if (unlikely(wl->state == WL1271_STATE_OFF))
261 goto out;
262
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200263 prev_tx_packets_count = wl->tx_packets_count;
264
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200265 /* if rates have changed, re-configure the rate policy */
266 if (unlikely(sta_rates)) {
267 wl->rate_set = wl1271_tx_enabled_rates_get(wl, sta_rates);
268 wl1271_acx_rate_policies(wl);
269 }
270
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300271 while ((skb = skb_dequeue(&wl->tx_queue))) {
272 if (!woken_up) {
273 ret = wl1271_ps_elp_wakeup(wl, false);
274 if (ret < 0)
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200275 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300276 woken_up = true;
277 }
278
279 ret = wl1271_tx_frame(wl, skb);
280 if (ret == -EBUSY) {
281 /* firmware buffer is full, stop queues */
282 wl1271_debug(DEBUG_TX, "tx_work: fw buffer full, "
283 "stop queues");
284 ieee80211_stop_queues(wl->hw);
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200285 set_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300286 skb_queue_head(&wl->tx_queue, skb);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200287 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300288 } else if (ret < 0) {
289 dev_kfree_skb(skb);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200290 goto out_ack;
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200291 } else if (test_and_clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED,
292 &wl->flags)) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300293 /* firmware buffer has space, restart queues */
294 wl1271_debug(DEBUG_TX,
295 "complete_packet: waking queues");
296 ieee80211_wake_queues(wl->hw);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300297 }
298 }
299
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200300out_ack:
301 /* interrupt the firmware with the new packets */
302 if (prev_tx_packets_count != wl->tx_packets_count)
303 wl1271_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
304
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300305out:
306 if (woken_up)
307 wl1271_ps_elp_sleep(wl);
308
309 mutex_unlock(&wl->mutex);
310}
311
312static void wl1271_tx_complete_packet(struct wl1271 *wl,
313 struct wl1271_tx_hw_res_descr *result)
314{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300315 struct ieee80211_tx_info *info;
316 struct sk_buff *skb;
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300317 u16 seq;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300318 int id = result->id;
319
320 /* check for id legality */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200321 if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300322 wl1271_warning("TX result illegal id: %d", id);
323 return;
324 }
325
326 skb = wl->tx_frames[id];
327 info = IEEE80211_SKB_CB(skb);
328
329 /* update packet status */
330 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
331 if (result->status == TX_SUCCESS)
332 info->flags |= IEEE80211_TX_STAT_ACK;
333 if (result->status & TX_RETRY_EXCEEDED) {
334 /* FIXME */
335 /* info->status.excessive_retries = 1; */
336 wl->stats.excessive_retries++;
337 }
338 }
339
340 /* FIXME */
341 /* info->status.retry_count = result->ack_failures; */
342 wl->stats.retry_count += result->ack_failures;
343
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300344 /* update security sequence number */
345 seq = wl->tx_security_seq_16 +
346 (result->lsb_security_sequence_number -
347 wl->tx_security_last_seq);
348 wl->tx_security_last_seq = result->lsb_security_sequence_number;
349
350 if (seq < wl->tx_security_seq_16)
351 wl->tx_security_seq_32++;
352 wl->tx_security_seq_16 = seq;
353
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300354 /* remove private header from packet */
355 skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
356
357 /* remove TKIP header space if present */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300358 if (info->control.hw_key &&
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300359 info->control.hw_key->alg == ALG_TKIP) {
360 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
361 memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
362 skb_pull(skb, WL1271_TKIP_IV_SPACE);
363 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300364
365 wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
366 " status 0x%x",
367 result->id, skb, result->ack_failures,
368 result->rate_class_index, result->status);
369
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300370 /* return the packet to the stack */
371 ieee80211_tx_status(wl->hw, skb);
372 wl->tx_frames[result->id] = NULL;
373}
374
375/* Called upon reception of a TX complete interrupt */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200376void wl1271_tx_complete(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300377{
378 struct wl1271_acx_mem_map *memmap =
379 (struct wl1271_acx_mem_map *)wl->target_mem_map;
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200380 u32 count, fw_counter;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300381 u32 i;
382
383 wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
384
385 /* read the tx results from the chipset */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200386 wl1271_read(wl, le32_to_cpu(memmap->tx_result),
387 wl->tx_res_if, sizeof(*wl->tx_res_if), false);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200388 fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
389
390 /* write host counter to chipset (to ack) */
391 wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
392 offsetof(struct wl1271_tx_hw_res_if,
393 tx_result_host_counter), fw_counter);
394
395 count = fw_counter - wl->tx_results_count;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300396
397 /* verify that the result buffer is not getting overrun */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200398 if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300399 wl1271_warning("TX result overflow from chipset: %d", count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300400
401 /* process the results */
402 for (i = 0; i < count; i++) {
403 struct wl1271_tx_hw_res_descr *result;
404 u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
405
406 /* process the packet */
407 result = &(wl->tx_res_if->tx_results_queue[offset]);
408 wl1271_tx_complete_packet(wl, result);
409
410 wl->tx_results_count++;
411 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300412}
413
414/* caller must hold wl->mutex */
415void wl1271_tx_flush(struct wl1271 *wl)
416{
417 int i;
418 struct sk_buff *skb;
419 struct ieee80211_tx_info *info;
420
421 /* TX failure */
422/* control->flags = 0; FIXME */
423
424 while ((skb = skb_dequeue(&wl->tx_queue))) {
425 info = IEEE80211_SKB_CB(skb);
426
427 wl1271_debug(DEBUG_TX, "flushing skb 0x%p", skb);
428
429 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
430 continue;
431
432 ieee80211_tx_status(wl->hw, skb);
433 }
434
Juuso Oikarinenbe7078c2009-10-08 21:56:26 +0300435 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300436 if (wl->tx_frames[i] != NULL) {
437 skb = wl->tx_frames[i];
438 info = IEEE80211_SKB_CB(skb);
439
440 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
441 continue;
442
443 ieee80211_tx_status(wl->hw, skb);
444 wl->tx_frames[i] = NULL;
445 }
446}