blob: 4560458a6d6079fa17d15c0a77d39e5d79b23395 [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"
28#include "wl1271_spi.h"
29#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;
90 int pad;
91
92 desc = (struct wl1271_tx_hw_descr *) skb->data;
93
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +030094 /* relocate space for security header */
95 if (extra) {
96 void *framestart = skb->data + sizeof(*desc);
97 u16 fc = *(u16 *)(framestart + extra);
98 int hdrlen = ieee80211_hdrlen(fc);
99 memmove(framestart, framestart + extra, hdrlen);
100 }
101
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300102 /* configure packet life time */
103 desc->start_time = jiffies_to_usecs(jiffies) - wl->time_offset;
104 desc->life_time = TX_HW_MGMT_PKT_LIFETIME_TU;
105
106 /* configure the tx attributes */
107 desc->tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
108 /* FIXME: do we know the packet priority? can we identify mgmt
109 packets, and use max prio for them at least? */
110 desc->tid = 0;
111 desc->aid = TX_HW_DEFAULT_AID;
112 desc->reserved = 0;
113
114 /* align the length (and store in terms of words) */
115 pad = WL1271_TX_ALIGN(skb->len);
116 desc->length = pad >> 2;
117
118 /* calculate number of padding bytes */
119 pad = pad - skb->len;
120 desc->tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
121
122 wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d", pad);
123 return 0;
124}
125
126static int wl1271_tx_send_packet(struct wl1271 *wl, struct sk_buff *skb,
127 struct ieee80211_tx_info *control)
128{
129
130 struct wl1271_tx_hw_descr *desc;
131 int len;
132
133 /* FIXME: This is a workaround for getting non-aligned packets.
134 This happens at least with EAPOL packets from the user space.
135 Our DMA requires packets to be aligned on a 4-byte boundary.
136 */
137 if (unlikely((long)skb->data & 0x03)) {
138 int offset = (4 - (long)skb->data) & 0x03;
139 wl1271_debug(DEBUG_TX, "skb offset %d", offset);
140
141 /* check whether the current skb can be used */
142 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
143 unsigned char *src = skb->data;
144
145 /* align the buffer on a 4-byte boundary */
146 skb_reserve(skb, offset);
147 memmove(skb->data, src, skb->len);
148 } else {
149 wl1271_info("No handler, fixme!");
150 return -EINVAL;
151 }
152 }
153
154 len = WL1271_TX_ALIGN(skb->len);
155
156 /* perform a fixed address block write with the packet */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300157 wl1271_spi_write(wl, WL1271_SLV_MEM_DATA, skb->data, len, true);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300158
159 /* write packet new counter into the write access register */
160 wl->tx_packets_count++;
Juuso Oikarinen74621412009-10-12 15:08:54 +0300161 wl1271_spi_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300162
163 desc = (struct wl1271_tx_hw_descr *) skb->data;
164 wl1271_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u (%u words)",
165 desc->id, skb, len, desc->length);
166
167 return 0;
168}
169
170/* caller must hold wl->mutex */
171static int wl1271_tx_frame(struct wl1271 *wl, struct sk_buff *skb)
172{
173 struct ieee80211_tx_info *info;
174 u32 extra = 0;
175 int ret = 0;
176 u8 idx;
177
178 if (!skb)
179 return -EINVAL;
180
181 info = IEEE80211_SKB_CB(skb);
182
183 if (info->control.hw_key &&
184 info->control.hw_key->alg == ALG_TKIP)
185 extra = WL1271_TKIP_IV_SPACE;
186
187 if (info->control.hw_key) {
188 idx = info->control.hw_key->hw_key_idx;
189
190 /* FIXME: do we have to do this if we're not using WEP? */
191 if (unlikely(wl->default_key != idx)) {
192 ret = wl1271_cmd_set_default_wep_key(wl, idx);
193 if (ret < 0)
194 return ret;
195 }
196 }
197
198 ret = wl1271_tx_allocate(wl, skb, extra);
199 if (ret < 0)
200 return ret;
201
202 ret = wl1271_tx_fill_hdr(wl, skb, extra, info);
203 if (ret < 0)
204 return ret;
205
206 ret = wl1271_tx_send_packet(wl, skb, info);
207 if (ret < 0)
208 return ret;
209
210 return ret;
211}
212
213void wl1271_tx_work(struct work_struct *work)
214{
215 struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
216 struct sk_buff *skb;
217 bool woken_up = false;
218 int ret;
219
220 mutex_lock(&wl->mutex);
221
222 if (unlikely(wl->state == WL1271_STATE_OFF))
223 goto out;
224
225 while ((skb = skb_dequeue(&wl->tx_queue))) {
226 if (!woken_up) {
227 ret = wl1271_ps_elp_wakeup(wl, false);
228 if (ret < 0)
229 goto out;
230 woken_up = true;
231 }
232
233 ret = wl1271_tx_frame(wl, skb);
234 if (ret == -EBUSY) {
235 /* firmware buffer is full, stop queues */
236 wl1271_debug(DEBUG_TX, "tx_work: fw buffer full, "
237 "stop queues");
238 ieee80211_stop_queues(wl->hw);
239 wl->tx_queue_stopped = true;
240 skb_queue_head(&wl->tx_queue, skb);
241 goto out;
242 } else if (ret < 0) {
243 dev_kfree_skb(skb);
244 goto out;
245 } else if (wl->tx_queue_stopped) {
246 /* firmware buffer has space, restart queues */
247 wl1271_debug(DEBUG_TX,
248 "complete_packet: waking queues");
249 ieee80211_wake_queues(wl->hw);
250 wl->tx_queue_stopped = false;
251 }
252 }
253
254out:
255 if (woken_up)
256 wl1271_ps_elp_sleep(wl);
257
258 mutex_unlock(&wl->mutex);
259}
260
261static void wl1271_tx_complete_packet(struct wl1271 *wl,
262 struct wl1271_tx_hw_res_descr *result)
263{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300264 struct ieee80211_tx_info *info;
265 struct sk_buff *skb;
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300266 u16 seq;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300267 int id = result->id;
268
269 /* check for id legality */
Juuso Oikarinenbe7078c2009-10-08 21:56:26 +0300270 if (id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300271 wl1271_warning("TX result illegal id: %d", id);
272 return;
273 }
274
275 skb = wl->tx_frames[id];
276 info = IEEE80211_SKB_CB(skb);
277
278 /* update packet status */
279 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
280 if (result->status == TX_SUCCESS)
281 info->flags |= IEEE80211_TX_STAT_ACK;
282 if (result->status & TX_RETRY_EXCEEDED) {
283 /* FIXME */
284 /* info->status.excessive_retries = 1; */
285 wl->stats.excessive_retries++;
286 }
287 }
288
289 /* FIXME */
290 /* info->status.retry_count = result->ack_failures; */
291 wl->stats.retry_count += result->ack_failures;
292
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300293 /* update security sequence number */
294 seq = wl->tx_security_seq_16 +
295 (result->lsb_security_sequence_number -
296 wl->tx_security_last_seq);
297 wl->tx_security_last_seq = result->lsb_security_sequence_number;
298
299 if (seq < wl->tx_security_seq_16)
300 wl->tx_security_seq_32++;
301 wl->tx_security_seq_16 = seq;
302
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300303 /* remove private header from packet */
304 skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
305
306 /* remove TKIP header space if present */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300307 if (info->control.hw_key &&
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300308 info->control.hw_key->alg == ALG_TKIP) {
309 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
310 memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
311 skb_pull(skb, WL1271_TKIP_IV_SPACE);
312 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300313
314 wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
315 " status 0x%x",
316 result->id, skb, result->ack_failures,
317 result->rate_class_index, result->status);
318
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300319 /* return the packet to the stack */
320 ieee80211_tx_status(wl->hw, skb);
321 wl->tx_frames[result->id] = NULL;
322}
323
324/* Called upon reception of a TX complete interrupt */
325void wl1271_tx_complete(struct wl1271 *wl, u32 count)
326{
327 struct wl1271_acx_mem_map *memmap =
328 (struct wl1271_acx_mem_map *)wl->target_mem_map;
329 u32 i;
330
331 wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
332
333 /* read the tx results from the chipset */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300334 wl1271_spi_read(wl, memmap->tx_result,
335 wl->tx_res_if, sizeof(*wl->tx_res_if), false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300336
337 /* verify that the result buffer is not getting overrun */
338 if (count > TX_HW_RESULT_QUEUE_LEN) {
339 wl1271_warning("TX result overflow from chipset: %d", count);
340 count = TX_HW_RESULT_QUEUE_LEN;
341 }
342
343 /* process the results */
344 for (i = 0; i < count; i++) {
345 struct wl1271_tx_hw_res_descr *result;
346 u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
347
348 /* process the packet */
349 result = &(wl->tx_res_if->tx_results_queue[offset]);
350 wl1271_tx_complete_packet(wl, result);
351
352 wl->tx_results_count++;
353 }
354
355 /* write host counter to chipset (to ack) */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300356 wl1271_spi_write32(wl, memmap->tx_result +
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300357 offsetof(struct wl1271_tx_hw_res_if,
358 tx_result_host_counter),
359 wl->tx_res_if->tx_result_fw_counter);
360}
361
362/* caller must hold wl->mutex */
363void wl1271_tx_flush(struct wl1271 *wl)
364{
365 int i;
366 struct sk_buff *skb;
367 struct ieee80211_tx_info *info;
368
369 /* TX failure */
370/* control->flags = 0; FIXME */
371
372 while ((skb = skb_dequeue(&wl->tx_queue))) {
373 info = IEEE80211_SKB_CB(skb);
374
375 wl1271_debug(DEBUG_TX, "flushing skb 0x%p", skb);
376
377 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
378 continue;
379
380 ieee80211_tx_status(wl->hw, skb);
381 }
382
Juuso Oikarinenbe7078c2009-10-08 21:56:26 +0300383 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300384 if (wl->tx_frames[i] != NULL) {
385 skb = wl->tx_frames[i];
386 info = IEEE80211_SKB_CB(skb);
387
388 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
389 continue;
390
391 ieee80211_tx_status(wl->hw, skb);
392 wl->tx_frames[i] = NULL;
393 }
394}