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>
32 static bool wl1251_tx_double_buffer_busy(struct wl1251
*wl
, u32 data_out_count
)
34 int used
, data_in_count
;
36 data_in_count
= wl
->data_in_count
;
38 if (data_in_count
< data_out_count
)
39 /* data_in_count has wrapped */
40 data_in_count
+= TX_STATUS_DATA_OUT_COUNT_MASK
+ 1;
42 used
= data_in_count
- data_out_count
;
45 WARN_ON(used
> DP_TX_PACKET_RING_CHUNK_NUM
);
47 if (used
>= DP_TX_PACKET_RING_CHUNK_NUM
)
53 static int wl1251_tx_path_status(struct wl1251
*wl
)
55 u32 status
, addr
, data_out_count
;
58 addr
= wl
->data_path
->tx_control_addr
;
59 status
= wl1251_mem_read32(wl
, addr
);
60 data_out_count
= status
& TX_STATUS_DATA_OUT_COUNT_MASK
;
61 busy
= wl1251_tx_double_buffer_busy(wl
, data_out_count
);
69 static int wl1251_tx_id(struct wl1251
*wl
, struct sk_buff
*skb
)
73 for (i
= 0; i
< FW_TX_CMPLT_BLOCK_SIZE
; i
++)
74 if (wl
->tx_frames
[i
] == NULL
) {
75 wl
->tx_frames
[i
] = skb
;
82 static void wl1251_tx_control(struct tx_double_buffer_desc
*tx_hdr
,
83 struct ieee80211_tx_info
*control
, u16 fc
)
85 *(u16
*)&tx_hdr
->control
= 0;
87 tx_hdr
->control
.rate_policy
= 0;
90 tx_hdr
->control
.packet_type
= 0;
92 if (control
->flags
& IEEE80211_TX_CTL_NO_ACK
)
93 tx_hdr
->control
.ack_policy
= 1;
95 tx_hdr
->control
.tx_complete
= 1;
97 if ((fc
& IEEE80211_FTYPE_DATA
) &&
98 ((fc
& IEEE80211_STYPE_QOS_DATA
) ||
99 (fc
& IEEE80211_STYPE_QOS_NULLFUNC
)))
100 tx_hdr
->control
.qos
= 1;
103 /* RSN + MIC = 8 + 8 = 16 bytes (worst case - AES). */
104 #define MAX_MSDU_SECURITY_LENGTH 16
105 #define MAX_MPDU_SECURITY_LENGTH 16
106 #define WLAN_QOS_HDR_LEN 26
107 #define MAX_MPDU_HEADER_AND_SECURITY (MAX_MPDU_SECURITY_LENGTH + \
109 #define HW_BLOCK_SIZE 252
110 static void wl1251_tx_frag_block_num(struct tx_double_buffer_desc
*tx_hdr
)
112 u16 payload_len
, frag_threshold
, mem_blocks
;
113 u16 num_mpdus
, mem_blocks_per_frag
;
115 frag_threshold
= IEEE80211_MAX_FRAG_THRESHOLD
;
116 tx_hdr
->frag_threshold
= cpu_to_le16(frag_threshold
);
118 payload_len
= le16_to_cpu(tx_hdr
->length
) + MAX_MSDU_SECURITY_LENGTH
;
120 if (payload_len
> frag_threshold
) {
121 mem_blocks_per_frag
=
122 ((frag_threshold
+ MAX_MPDU_HEADER_AND_SECURITY
) /
124 num_mpdus
= payload_len
/ frag_threshold
;
125 mem_blocks
= num_mpdus
* mem_blocks_per_frag
;
126 payload_len
-= num_mpdus
* frag_threshold
;
130 mem_blocks_per_frag
= 0;
135 mem_blocks
+= (payload_len
/ HW_BLOCK_SIZE
) + 1;
138 mem_blocks
+= min(num_mpdus
, mem_blocks_per_frag
);
140 tx_hdr
->num_mem_blocks
= mem_blocks
;
143 static int wl1251_tx_fill_hdr(struct wl1251
*wl
, struct sk_buff
*skb
,
144 struct ieee80211_tx_info
*control
)
146 struct tx_double_buffer_desc
*tx_hdr
;
147 struct ieee80211_rate
*rate
;
154 id
= wl1251_tx_id(wl
, skb
);
158 fc
= *(u16
*)skb
->data
;
159 tx_hdr
= (struct tx_double_buffer_desc
*) skb_push(skb
,
162 tx_hdr
->length
= cpu_to_le16(skb
->len
- sizeof(*tx_hdr
));
163 rate
= ieee80211_get_tx_rate(wl
->hw
, control
);
164 tx_hdr
->rate
= cpu_to_le16(rate
->hw_value
);
165 tx_hdr
->expiry_time
= cpu_to_le32(1 << 16);
168 tx_hdr
->xmit_queue
= wl1251_tx_get_queue(skb_get_queue_mapping(skb
));
170 wl1251_tx_control(tx_hdr
, control
, fc
);
171 wl1251_tx_frag_block_num(tx_hdr
);
176 /* We copy the packet to the target */
177 static int wl1251_tx_send_packet(struct wl1251
*wl
, struct sk_buff
*skb
,
178 struct ieee80211_tx_info
*control
)
180 struct tx_double_buffer_desc
*tx_hdr
;
187 tx_hdr
= (struct tx_double_buffer_desc
*) skb
->data
;
189 if (control
->control
.hw_key
&&
190 control
->control
.hw_key
->cipher
== WLAN_CIPHER_SUITE_TKIP
) {
196 fc
= *(__le16
*)(skb
->data
+ sizeof(*tx_hdr
));
197 length
= le16_to_cpu(tx_hdr
->length
) + WL1251_TKIP_IV_SPACE
;
198 tx_hdr
->length
= cpu_to_le16(length
);
200 hdrlen
= ieee80211_hdrlen(fc
);
202 pos
= skb_push(skb
, WL1251_TKIP_IV_SPACE
);
203 memmove(pos
, pos
+ WL1251_TKIP_IV_SPACE
,
204 sizeof(*tx_hdr
) + hdrlen
);
207 /* Revisit. This is a workaround for getting non-aligned packets.
208 This happens at least with EAPOL packets from the user space.
209 Our DMA requires packets to be aligned on a 4-byte boundary.
211 if (unlikely((long)skb
->data
& 0x03)) {
212 int offset
= (4 - (long)skb
->data
) & 0x03;
213 wl1251_debug(DEBUG_TX
, "skb offset %d", offset
);
215 /* check whether the current skb can be used */
216 if (!skb_cloned(skb
) && (skb_tailroom(skb
) >= offset
)) {
217 unsigned char *src
= skb
->data
;
219 /* align the buffer on a 4-byte boundary */
220 skb_reserve(skb
, offset
);
221 memmove(skb
->data
, src
, skb
->len
);
222 tx_hdr
= (struct tx_double_buffer_desc
*) skb
->data
;
224 wl1251_info("No handler, fixme!");
229 /* Our skb->data at this point includes the HW header */
230 len
= WL1251_TX_ALIGN(skb
->len
);
232 if (wl
->data_in_count
& 0x1)
233 addr
= wl
->data_path
->tx_packet_ring_addr
+
234 wl
->data_path
->tx_packet_ring_chunk_size
;
236 addr
= wl
->data_path
->tx_packet_ring_addr
;
238 wl1251_mem_write(wl
, addr
, skb
->data
, len
);
240 wl1251_debug(DEBUG_TX
, "tx id %u skb 0x%p payload %u rate 0x%x "
241 "queue %d", tx_hdr
->id
, skb
, tx_hdr
->length
,
242 tx_hdr
->rate
, tx_hdr
->xmit_queue
);
247 static void wl1251_tx_trigger(struct wl1251
*wl
)
251 if (wl
->data_in_count
& 0x1) {
252 addr
= ACX_REG_INTERRUPT_TRIG_H
;
253 data
= INTR_TRIG_TX_PROC1
;
255 addr
= ACX_REG_INTERRUPT_TRIG
;
256 data
= INTR_TRIG_TX_PROC0
;
259 wl1251_reg_write32(wl
, addr
, data
);
261 /* Bumping data in */
262 wl
->data_in_count
= (wl
->data_in_count
+ 1) &
263 TX_STATUS_DATA_OUT_COUNT_MASK
;
266 /* caller must hold wl->mutex */
267 static int wl1251_tx_frame(struct wl1251
*wl
, struct sk_buff
*skb
)
269 struct ieee80211_tx_info
*info
;
273 info
= IEEE80211_SKB_CB(skb
);
275 if (info
->control
.hw_key
) {
276 idx
= info
->control
.hw_key
->hw_key_idx
;
277 if (unlikely(wl
->default_key
!= idx
)) {
278 ret
= wl1251_acx_default_key(wl
, idx
);
284 ret
= wl1251_tx_path_status(wl
);
288 ret
= wl1251_tx_fill_hdr(wl
, skb
, info
);
292 ret
= wl1251_tx_send_packet(wl
, skb
, info
);
296 wl1251_tx_trigger(wl
);
301 void wl1251_tx_work(struct work_struct
*work
)
303 struct wl1251
*wl
= container_of(work
, struct wl1251
, tx_work
);
305 bool woken_up
= false;
308 mutex_lock(&wl
->mutex
);
310 if (unlikely(wl
->state
== WL1251_STATE_OFF
))
313 while ((skb
= skb_dequeue(&wl
->tx_queue
))) {
315 ret
= wl1251_ps_elp_wakeup(wl
);
321 ret
= wl1251_tx_frame(wl
, skb
);
323 skb_queue_head(&wl
->tx_queue
, skb
);
325 } else if (ret
< 0) {
333 wl1251_ps_elp_sleep(wl
);
335 mutex_unlock(&wl
->mutex
);
338 static const char *wl1251_tx_parse_status(u8 status
)
340 /* 8 bit status field, one character per bit plus null */
344 memset(buf
, 0, sizeof(buf
));
346 if (status
& TX_DMA_ERROR
)
348 if (status
& TX_DISABLED
)
350 if (status
& TX_RETRY_EXCEEDED
)
352 if (status
& TX_TIMEOUT
)
354 if (status
& TX_KEY_NOT_FOUND
)
356 if (status
& TX_ENCRYPT_FAIL
)
358 if (status
& TX_UNAVAILABLE_PRIORITY
)
361 /* bit 0 is unused apparently */
366 static void wl1251_tx_packet_cb(struct wl1251
*wl
,
367 struct tx_result
*result
)
369 struct ieee80211_tx_info
*info
;
374 skb
= wl
->tx_frames
[result
->id
];
376 wl1251_error("SKB for packet %d is NULL", result
->id
);
380 info
= IEEE80211_SKB_CB(skb
);
382 if (!(info
->flags
& IEEE80211_TX_CTL_NO_ACK
) &&
383 (result
->status
== TX_SUCCESS
))
384 info
->flags
|= IEEE80211_TX_STAT_ACK
;
386 info
->status
.rates
[0].count
= result
->ack_failures
+ 1;
387 wl
->stats
.retry_count
+= result
->ack_failures
;
390 * We have to remove our private TX header before pushing
391 * the skb back to mac80211.
393 frame
= skb_pull(skb
, sizeof(struct tx_double_buffer_desc
));
394 if (info
->control
.hw_key
&&
395 info
->control
.hw_key
->cipher
== WLAN_CIPHER_SUITE_TKIP
) {
396 hdrlen
= ieee80211_get_hdrlen_from_skb(skb
);
397 memmove(frame
+ WL1251_TKIP_IV_SPACE
, frame
, hdrlen
);
398 skb_pull(skb
, WL1251_TKIP_IV_SPACE
);
401 wl1251_debug(DEBUG_TX
, "tx status id %u skb 0x%p failures %u rate 0x%x"
403 result
->id
, skb
, result
->ack_failures
, result
->rate
,
404 result
->status
, wl1251_tx_parse_status(result
->status
));
407 ieee80211_tx_status(wl
->hw
, skb
);
409 wl
->tx_frames
[result
->id
] = NULL
;
411 if (wl
->tx_queue_stopped
) {
412 wl1251_debug(DEBUG_TX
, "cb: queue was stopped");
414 skb
= skb_dequeue(&wl
->tx_queue
);
416 /* The skb can be NULL because tx_work might have been
417 scheduled before the queue was stopped making the
421 ret
= wl1251_tx_frame(wl
, skb
);
423 /* firmware buffer is still full */
424 wl1251_debug(DEBUG_TX
, "cb: fw buffer "
426 skb_queue_head(&wl
->tx_queue
, skb
);
428 } else if (ret
< 0) {
434 wl1251_debug(DEBUG_TX
, "cb: waking queues");
435 ieee80211_wake_queues(wl
->hw
);
436 wl
->tx_queue_stopped
= false;
440 /* Called upon reception of a TX complete interrupt */
441 void wl1251_tx_complete(struct wl1251
*wl
)
443 int i
, result_index
, num_complete
= 0;
444 struct tx_result result
[FW_TX_CMPLT_BLOCK_SIZE
], *result_ptr
;
447 if (unlikely(wl
->state
!= WL1251_STATE_ON
))
450 /* First we read the result */
451 wl1251_mem_read(wl
, wl
->data_path
->tx_complete_addr
,
452 result
, sizeof(result
));
454 result_index
= wl
->next_tx_complete
;
456 for (i
= 0; i
< ARRAY_SIZE(result
); i
++) {
457 result_ptr
= &result
[result_index
];
459 if (result_ptr
->done_1
== 1 &&
460 result_ptr
->done_2
== 1) {
461 wl1251_tx_packet_cb(wl
, result_ptr
);
463 result_ptr
->done_1
= 0;
464 result_ptr
->done_2
= 0;
466 result_index
= (result_index
+ 1) &
467 (FW_TX_CMPLT_BLOCK_SIZE
- 1);
474 if (wl
->tx_queue_stopped
476 skb_queue_len(&wl
->tx_queue
) <= WL1251_TX_QUEUE_LOW_WATERMARK
){
478 /* firmware buffer has space, restart queues */
479 wl1251_debug(DEBUG_TX
, "tx_complete: waking queues");
480 spin_lock_irqsave(&wl
->wl_lock
, flags
);
481 ieee80211_wake_queues(wl
->hw
);
482 wl
->tx_queue_stopped
= false;
483 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
484 ieee80211_queue_work(wl
->hw
, &wl
->tx_work
);
488 /* Every completed frame needs to be acknowledged */
491 * If we've wrapped, we have to clear
492 * the results in 2 steps.
494 if (result_index
> wl
->next_tx_complete
) {
495 /* Only 1 write is needed */
497 wl
->data_path
->tx_complete_addr
+
498 (wl
->next_tx_complete
*
499 sizeof(struct tx_result
)),
500 &result
[wl
->next_tx_complete
],
502 sizeof(struct tx_result
));
505 } else if (result_index
< wl
->next_tx_complete
) {
506 /* 2 writes are needed */
508 wl
->data_path
->tx_complete_addr
+
509 (wl
->next_tx_complete
*
510 sizeof(struct tx_result
)),
511 &result
[wl
->next_tx_complete
],
512 (FW_TX_CMPLT_BLOCK_SIZE
-
513 wl
->next_tx_complete
) *
514 sizeof(struct tx_result
));
517 wl
->data_path
->tx_complete_addr
,
520 FW_TX_CMPLT_BLOCK_SIZE
+
521 wl
->next_tx_complete
) *
522 sizeof(struct tx_result
));
525 /* We have to write the whole array */
527 wl
->data_path
->tx_complete_addr
,
529 FW_TX_CMPLT_BLOCK_SIZE
*
530 sizeof(struct tx_result
));
535 wl
->next_tx_complete
= result_index
;
538 /* caller must hold wl->mutex */
539 void wl1251_tx_flush(struct wl1251
*wl
)
543 struct ieee80211_tx_info
*info
;
546 /* control->flags = 0; FIXME */
548 while ((skb
= skb_dequeue(&wl
->tx_queue
))) {
549 info
= IEEE80211_SKB_CB(skb
);
551 wl1251_debug(DEBUG_TX
, "flushing skb 0x%p", skb
);
553 if (!(info
->flags
& IEEE80211_TX_CTL_REQ_TX_STATUS
))
556 ieee80211_tx_status(wl
->hw
, skb
);
559 for (i
= 0; i
< FW_TX_CMPLT_BLOCK_SIZE
; i
++)
560 if (wl
->tx_frames
[i
] != NULL
) {
561 skb
= wl
->tx_frames
[i
];
562 info
= IEEE80211_SKB_CB(skb
);
564 if (!(info
->flags
& IEEE80211_TX_CTL_REQ_TX_STATUS
))
567 ieee80211_tx_status(wl
->hw
, skb
);
568 wl
->tx_frames
[i
] = NULL
;