2 * This file is part of wl1251
4 * Copyright (c) 1998-2007 Texas Instruments Incorporated
5 * Copyright (C) 2008 Nokia Corporation
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 #include <linux/kernel.h>
24 #include <linux/module.h>
33 static bool wl1251_tx_double_buffer_busy(struct wl1251 *wl, u32 data_out_count)
35 int used, data_in_count;
37 data_in_count = wl->data_in_count;
39 if (data_in_count < data_out_count)
40 /* data_in_count has wrapped */
41 data_in_count += TX_STATUS_DATA_OUT_COUNT_MASK + 1;
43 used = data_in_count - data_out_count;
46 WARN_ON(used > DP_TX_PACKET_RING_CHUNK_NUM);
48 if (used >= DP_TX_PACKET_RING_CHUNK_NUM)
54 int wl1251_tx_path_status(struct wl1251 *wl)
56 u32 status, addr, data_out_count;
59 addr = wl->data_path->tx_control_addr;
60 status = wl1251_mem_read32(wl, addr);
61 data_out_count = status & TX_STATUS_DATA_OUT_COUNT_MASK;
62 busy = wl1251_tx_double_buffer_busy(wl, data_out_count);
70 static int wl1251_tx_id(struct wl1251 *wl, struct sk_buff *skb)
74 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
75 if (wl->tx_frames[i] == NULL) {
76 wl->tx_frames[i] = skb;
83 static void wl1251_tx_control(struct tx_double_buffer_desc *tx_hdr,
84 struct ieee80211_tx_info *control, u16 fc)
86 *(u16 *)&tx_hdr->control = 0;
88 tx_hdr->control.rate_policy = 0;
91 tx_hdr->control.packet_type = 0;
93 if (control->flags & IEEE80211_TX_CTL_NO_ACK)
94 tx_hdr->control.ack_policy = 1;
96 tx_hdr->control.tx_complete = 1;
98 if ((fc & IEEE80211_FTYPE_DATA) &&
99 ((fc & IEEE80211_STYPE_QOS_DATA) ||
100 (fc & IEEE80211_STYPE_QOS_NULLFUNC)))
101 tx_hdr->control.qos = 1;
104 /* RSN + MIC = 8 + 8 = 16 bytes (worst case - AES). */
105 #define MAX_MSDU_SECURITY_LENGTH 16
106 #define MAX_MPDU_SECURITY_LENGTH 16
107 #define WLAN_QOS_HDR_LEN 26
108 #define MAX_MPDU_HEADER_AND_SECURITY (MAX_MPDU_SECURITY_LENGTH + \
110 #define HW_BLOCK_SIZE 252
111 static void wl1251_tx_frag_block_num(struct tx_double_buffer_desc *tx_hdr)
113 u16 payload_len, frag_threshold, mem_blocks;
114 u16 num_mpdus, mem_blocks_per_frag;
116 frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
117 tx_hdr->frag_threshold = cpu_to_le16(frag_threshold);
119 payload_len = le16_to_cpu(tx_hdr->length) + MAX_MSDU_SECURITY_LENGTH;
121 if (payload_len > frag_threshold) {
122 mem_blocks_per_frag =
123 ((frag_threshold + MAX_MPDU_HEADER_AND_SECURITY) /
125 num_mpdus = payload_len / frag_threshold;
126 mem_blocks = num_mpdus * mem_blocks_per_frag;
127 payload_len -= num_mpdus * frag_threshold;
131 mem_blocks_per_frag = 0;
136 mem_blocks += (payload_len / HW_BLOCK_SIZE) + 1;
139 mem_blocks += min(num_mpdus, mem_blocks_per_frag);
141 tx_hdr->num_mem_blocks = mem_blocks;
144 static int wl1251_tx_fill_hdr(struct wl1251 *wl, struct sk_buff *skb,
145 struct ieee80211_tx_info *control)
147 struct tx_double_buffer_desc *tx_hdr;
148 struct ieee80211_rate *rate;
155 id = wl1251_tx_id(wl, skb);
159 fc = *(u16 *)skb->data;
160 tx_hdr = (struct tx_double_buffer_desc *) skb_push(skb,
163 tx_hdr->length = cpu_to_le16(skb->len - sizeof(*tx_hdr));
164 rate = ieee80211_get_tx_rate(wl->hw, control);
165 tx_hdr->rate = cpu_to_le16(rate->hw_value);
166 tx_hdr->expiry_time = cpu_to_le32(1 << 16);
169 tx_hdr->xmit_queue = wl1251_tx_get_queue(skb_get_queue_mapping(skb));
171 wl1251_tx_control(tx_hdr, control, fc);
172 wl1251_tx_frag_block_num(tx_hdr);
177 /* We copy the packet to the target */
178 static int wl1251_tx_send_packet(struct wl1251 *wl, struct sk_buff *skb,
179 struct ieee80211_tx_info *control)
181 struct tx_double_buffer_desc *tx_hdr;
188 tx_hdr = (struct tx_double_buffer_desc *) skb->data;
190 if (control->control.hw_key &&
191 control->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
197 fc = *(__le16 *)(skb->data + sizeof(*tx_hdr));
198 length = le16_to_cpu(tx_hdr->length) + WL1251_TKIP_IV_SPACE;
199 tx_hdr->length = cpu_to_le16(length);
201 hdrlen = ieee80211_hdrlen(fc);
203 pos = skb_push(skb, WL1251_TKIP_IV_SPACE);
204 memmove(pos, pos + WL1251_TKIP_IV_SPACE,
205 sizeof(*tx_hdr) + hdrlen);
208 /* Revisit. This is a workaround for getting non-aligned packets.
209 This happens at least with EAPOL packets from the user space.
210 Our DMA requires packets to be aligned on a 4-byte boundary.
212 if (unlikely((long)skb->data & 0x03)) {
213 int offset = (4 - (long)skb->data) & 0x03;
214 wl1251_debug(DEBUG_TX, "skb offset %d", offset);
216 /* check whether the current skb can be used */
217 if (skb_cloned(skb) || (skb_tailroom(skb) < offset)) {
218 struct sk_buff *newskb = skb_copy_expand(skb, 0, 3,
221 if (unlikely(newskb == NULL)) {
222 wl1251_error("Can't allocate skb!");
226 tx_hdr = (struct tx_double_buffer_desc *) newskb->data;
228 dev_kfree_skb_any(skb);
229 wl->tx_frames[tx_hdr->id] = skb = newskb;
231 offset = (4 - (long)skb->data) & 0x03;
232 wl1251_debug(DEBUG_TX, "new skb offset %d", offset);
235 /* align the buffer on a 4-byte boundary */
237 unsigned char *src = skb->data;
238 skb_reserve(skb, offset);
239 memmove(skb->data, src, skb->len);
240 tx_hdr = (struct tx_double_buffer_desc *) skb->data;
244 /* Our skb->data at this point includes the HW header */
245 len = WL1251_TX_ALIGN(skb->len);
247 if (wl->data_in_count & 0x1)
248 addr = wl->data_path->tx_packet_ring_addr +
249 wl->data_path->tx_packet_ring_chunk_size;
251 addr = wl->data_path->tx_packet_ring_addr;
253 wl1251_mem_write(wl, addr, skb->data, len);
255 wl1251_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u rate 0x%x "
256 "queue %d", tx_hdr->id, skb, tx_hdr->length,
257 tx_hdr->rate, tx_hdr->xmit_queue);
262 static void wl1251_tx_trigger(struct wl1251 *wl)
266 if (wl->data_in_count & 0x1) {
267 addr = ACX_REG_INTERRUPT_TRIG_H;
268 data = INTR_TRIG_TX_PROC1;
270 addr = ACX_REG_INTERRUPT_TRIG;
271 data = INTR_TRIG_TX_PROC0;
274 wl1251_reg_write32(wl, addr, data);
276 /* Bumping data in */
277 wl->data_in_count = (wl->data_in_count + 1) &
278 TX_STATUS_DATA_OUT_COUNT_MASK;
281 static void enable_tx_for_packet_injection(struct wl1251 *wl)
285 ret = wl1251_cmd_join(wl, BSS_TYPE_STA_BSS, wl->channel,
286 wl->beacon_int, wl->dtim_period);
288 wl1251_warning("join failed");
292 ret = wl1251_event_wait(wl, JOIN_EVENT_COMPLETE_ID, 100);
294 wl1251_warning("join timeout");
301 /* caller must hold wl->mutex */
302 int wl1251_tx_frame(struct wl1251 *wl, struct sk_buff *skb)
304 struct ieee80211_tx_info *info;
308 info = IEEE80211_SKB_CB(skb);
310 if (info->control.hw_key) {
311 if (unlikely(wl->monitor_present))
314 idx = info->control.hw_key->hw_key_idx;
315 if (unlikely(wl->default_key != idx)) {
316 ret = wl1251_acx_default_key(wl, idx);
322 /* Enable tx path in monitor mode for packet injection */
323 if ((wl->vif == NULL) && !wl->joined)
324 enable_tx_for_packet_injection(wl);
326 ret = wl1251_tx_path_status(wl);
330 ret = wl1251_tx_fill_hdr(wl, skb, info);
334 ret = wl1251_tx_send_packet(wl, skb, info);
338 wl1251_tx_trigger(wl);
343 void wl1251_tx_work(struct work_struct *work)
345 struct wl1251 *wl = container_of(work, struct wl1251, tx_work);
347 bool woken_up = false;
350 mutex_lock(&wl->mutex);
352 if (unlikely(wl->state == WL1251_STATE_OFF))
355 while ((skb = skb_dequeue(&wl->tx_queue))) {
357 ret = wl1251_ps_elp_wakeup(wl);
363 ret = wl1251_tx_frame(wl, skb);
365 skb_queue_head(&wl->tx_queue, skb);
367 } else if (ret < 0) {
375 wl1251_ps_elp_sleep(wl);
377 mutex_unlock(&wl->mutex);
380 static const char *wl1251_tx_parse_status(u8 status)
382 /* 8 bit status field, one character per bit plus null */
386 memset(buf, 0, sizeof(buf));
388 if (status & TX_DMA_ERROR)
390 if (status & TX_DISABLED)
392 if (status & TX_RETRY_EXCEEDED)
394 if (status & TX_TIMEOUT)
396 if (status & TX_KEY_NOT_FOUND)
398 if (status & TX_ENCRYPT_FAIL)
400 if (status & TX_UNAVAILABLE_PRIORITY)
403 /* bit 0 is unused apparently */
408 static void wl1251_tx_packet_cb(struct wl1251 *wl,
409 struct tx_result *result)
411 struct ieee80211_tx_info *info;
416 skb = wl->tx_frames[result->id];
418 wl1251_error("SKB for packet %d is NULL", result->id);
422 info = IEEE80211_SKB_CB(skb);
424 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
425 (result->status == TX_SUCCESS))
426 info->flags |= IEEE80211_TX_STAT_ACK;
428 info->status.rates[0].count = result->ack_failures + 1;
429 wl->stats.retry_count += result->ack_failures;
432 * We have to remove our private TX header before pushing
433 * the skb back to mac80211.
435 frame = skb_pull(skb, sizeof(struct tx_double_buffer_desc));
436 if (info->control.hw_key &&
437 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
438 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
439 memmove(frame + WL1251_TKIP_IV_SPACE, frame, hdrlen);
440 skb_pull(skb, WL1251_TKIP_IV_SPACE);
443 wl1251_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
445 result->id, skb, result->ack_failures, result->rate,
446 result->status, wl1251_tx_parse_status(result->status));
449 ieee80211_tx_status(wl->hw, skb);
451 wl->tx_frames[result->id] = NULL;
454 /* Called upon reception of a TX complete interrupt */
455 void wl1251_tx_complete(struct wl1251 *wl)
457 int i, result_index, num_complete = 0, queue_len;
458 struct tx_result result[FW_TX_CMPLT_BLOCK_SIZE], *result_ptr;
461 if (unlikely(wl->state != WL1251_STATE_ON))
464 /* First we read the result */
465 wl1251_mem_read(wl, wl->data_path->tx_complete_addr,
466 result, sizeof(result));
468 result_index = wl->next_tx_complete;
470 for (i = 0; i < ARRAY_SIZE(result); i++) {
471 result_ptr = &result[result_index];
473 if (result_ptr->done_1 == 1 &&
474 result_ptr->done_2 == 1) {
475 wl1251_tx_packet_cb(wl, result_ptr);
477 result_ptr->done_1 = 0;
478 result_ptr->done_2 = 0;
480 result_index = (result_index + 1) &
481 (FW_TX_CMPLT_BLOCK_SIZE - 1);
488 queue_len = skb_queue_len(&wl->tx_queue);
490 if ((num_complete > 0) && (queue_len > 0)) {
491 /* firmware buffer has space, reschedule tx_work */
492 wl1251_debug(DEBUG_TX, "tx_complete: reschedule tx_work");
493 ieee80211_queue_work(wl->hw, &wl->tx_work);
496 if (wl->tx_queue_stopped &&
497 queue_len <= WL1251_TX_QUEUE_LOW_WATERMARK) {
498 /* tx_queue has space, restart queues */
499 wl1251_debug(DEBUG_TX, "tx_complete: waking queues");
500 spin_lock_irqsave(&wl->wl_lock, flags);
501 ieee80211_wake_queues(wl->hw);
502 wl->tx_queue_stopped = false;
503 spin_unlock_irqrestore(&wl->wl_lock, flags);
506 /* Every completed frame needs to be acknowledged */
509 * If we've wrapped, we have to clear
510 * the results in 2 steps.
512 if (result_index > wl->next_tx_complete) {
513 /* Only 1 write is needed */
515 wl->data_path->tx_complete_addr +
516 (wl->next_tx_complete *
517 sizeof(struct tx_result)),
518 &result[wl->next_tx_complete],
520 sizeof(struct tx_result));
523 } else if (result_index < wl->next_tx_complete) {
524 /* 2 writes are needed */
526 wl->data_path->tx_complete_addr +
527 (wl->next_tx_complete *
528 sizeof(struct tx_result)),
529 &result[wl->next_tx_complete],
530 (FW_TX_CMPLT_BLOCK_SIZE -
531 wl->next_tx_complete) *
532 sizeof(struct tx_result));
535 wl->data_path->tx_complete_addr,
538 FW_TX_CMPLT_BLOCK_SIZE +
539 wl->next_tx_complete) *
540 sizeof(struct tx_result));
543 /* We have to write the whole array */
545 wl->data_path->tx_complete_addr,
547 FW_TX_CMPLT_BLOCK_SIZE *
548 sizeof(struct tx_result));
553 wl->next_tx_complete = result_index;
554 wl->last_io_jiffies = jiffies;
557 /* caller must hold wl->mutex */
558 void wl1251_tx_flush(struct wl1251 *wl)
562 struct ieee80211_tx_info *info;
565 /* control->flags = 0; FIXME */
567 while ((skb = skb_dequeue(&wl->tx_queue))) {
568 info = IEEE80211_SKB_CB(skb);
570 wl1251_debug(DEBUG_TX, "flushing skb 0x%p", skb);
572 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
575 ieee80211_tx_status(wl->hw, skb);
578 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
579 if (wl->tx_frames[i] != NULL) {
580 skb = wl->tx_frames[i];
581 info = IEEE80211_SKB_CB(skb);
583 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
586 ieee80211_tx_status(wl->hw, skb);
587 wl->tx_frames[i] = NULL;