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