wl1271_tx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  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. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include "wl1271.h"
  26. #include "wl1271_io.h"
  27. #include "wl1271_reg.h"
  28. #include "wl1271_ps.h"
  29. #include "wl1271_tx.h"
  30. static int wl1271_tx_id(struct wl1271 *wl, struct sk_buff *skb)
  31. {
  32. int i;
  33. for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
  34. if (wl->tx_frames[i] == NULL) {
  35. wl->tx_frames[i] = skb;
  36. wl->tx_frames_cnt++;
  37. return i;
  38. }
  39. return -EBUSY;
  40. }
  41. static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra,
  42. u32 buf_offset)
  43. {
  44. struct wl1271_tx_hw_descr *desc;
  45. u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
  46. u32 total_blocks;
  47. int id, ret = -EBUSY;
  48. if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
  49. return -EBUSY;
  50. /* allocate free identifier for the packet */
  51. id = wl1271_tx_id(wl, skb);
  52. if (id < 0)
  53. return id;
  54. /* approximate the number of blocks required for this packet
  55. in the firmware */
  56. total_blocks = total_len + TX_HW_BLOCK_SIZE - 1;
  57. total_blocks = total_blocks / TX_HW_BLOCK_SIZE + TX_HW_BLOCK_SPARE;
  58. if (total_blocks <= wl->tx_blocks_available) {
  59. desc = (struct wl1271_tx_hw_descr *)skb_push(
  60. skb, total_len - skb->len);
  61. desc->extra_mem_blocks = TX_HW_BLOCK_SPARE;
  62. desc->total_mem_blocks = total_blocks;
  63. desc->id = id;
  64. wl->tx_blocks_available -= total_blocks;
  65. ret = 0;
  66. wl1271_debug(DEBUG_TX,
  67. "tx_allocate: size: %d, blocks: %d, id: %d",
  68. total_len, total_blocks, id);
  69. } else {
  70. wl->tx_frames[id] = NULL;
  71. wl->tx_frames_cnt--;
  72. }
  73. return ret;
  74. }
  75. static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
  76. u32 extra, struct ieee80211_tx_info *control)
  77. {
  78. struct timespec ts;
  79. struct wl1271_tx_hw_descr *desc;
  80. int pad, ac;
  81. s64 hosttime;
  82. u16 tx_attr;
  83. desc = (struct wl1271_tx_hw_descr *) skb->data;
  84. /* relocate space for security header */
  85. if (extra) {
  86. void *framestart = skb->data + sizeof(*desc);
  87. u16 fc = *(u16 *)(framestart + extra);
  88. int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
  89. memmove(framestart, framestart + extra, hdrlen);
  90. }
  91. /* configure packet life time */
  92. getnstimeofday(&ts);
  93. hosttime = (timespec_to_ns(&ts) >> 10);
  94. desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
  95. desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
  96. /* configure the tx attributes */
  97. tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
  98. /* queue (we use same identifiers for tid's and ac's */
  99. ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  100. desc->tid = ac;
  101. desc->aid = TX_HW_DEFAULT_AID;
  102. desc->reserved = 0;
  103. /* align the length (and store in terms of words) */
  104. pad = WL1271_TX_ALIGN(skb->len);
  105. desc->length = cpu_to_le16(pad >> 2);
  106. /* calculate number of padding bytes */
  107. pad = pad - skb->len;
  108. tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
  109. /* if the packets are destined for AP (have a STA entry) send them
  110. with AP rate policies, otherwise use default basic rates */
  111. if (control->control.sta)
  112. tx_attr |= ACX_TX_AP_FULL_RATE << TX_HW_ATTR_OFST_RATE_POLICY;
  113. desc->tx_attr = cpu_to_le16(tx_attr);
  114. wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d", pad);
  115. }
  116. /* caller must hold wl->mutex */
  117. static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct sk_buff *skb,
  118. u32 buf_offset)
  119. {
  120. struct ieee80211_tx_info *info;
  121. u32 extra = 0;
  122. int ret = 0;
  123. u8 idx;
  124. u32 total_len;
  125. if (!skb)
  126. return -EINVAL;
  127. info = IEEE80211_SKB_CB(skb);
  128. if (info->control.hw_key &&
  129. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
  130. extra = WL1271_TKIP_IV_SPACE;
  131. if (info->control.hw_key) {
  132. idx = info->control.hw_key->hw_key_idx;
  133. /* FIXME: do we have to do this if we're not using WEP? */
  134. if (unlikely(wl->default_key != idx)) {
  135. ret = wl1271_cmd_set_default_wep_key(wl, idx);
  136. if (ret < 0)
  137. return ret;
  138. wl->default_key = idx;
  139. }
  140. }
  141. ret = wl1271_tx_allocate(wl, skb, extra, buf_offset);
  142. if (ret < 0)
  143. return ret;
  144. wl1271_tx_fill_hdr(wl, skb, extra, info);
  145. /*
  146. * The length of each packet is stored in terms of words. Thus, we must
  147. * pad the skb data to make sure its length is aligned.
  148. * The number of padding bytes is computed and set in wl1271_tx_fill_hdr
  149. */
  150. total_len = WL1271_TX_ALIGN(skb->len);
  151. memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
  152. memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
  153. return total_len;
  154. }
  155. u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
  156. {
  157. struct ieee80211_supported_band *band;
  158. u32 enabled_rates = 0;
  159. int bit;
  160. band = wl->hw->wiphy->bands[wl->band];
  161. for (bit = 0; bit < band->n_bitrates; bit++) {
  162. if (rate_set & 0x1)
  163. enabled_rates |= band->bitrates[bit].hw_value;
  164. rate_set >>= 1;
  165. }
  166. return enabled_rates;
  167. }
  168. void wl1271_tx_work(struct work_struct *work)
  169. {
  170. struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
  171. struct sk_buff *skb;
  172. bool woken_up = false;
  173. u32 sta_rates = 0;
  174. u32 buf_offset;
  175. int ret;
  176. /* check if the rates supported by the AP have changed */
  177. if (unlikely(test_and_clear_bit(WL1271_FLAG_STA_RATES_CHANGED,
  178. &wl->flags))) {
  179. unsigned long flags;
  180. spin_lock_irqsave(&wl->wl_lock, flags);
  181. sta_rates = wl->sta_rate_set;
  182. spin_unlock_irqrestore(&wl->wl_lock, flags);
  183. }
  184. mutex_lock(&wl->mutex);
  185. if (unlikely(wl->state == WL1271_STATE_OFF))
  186. goto out;
  187. /* if rates have changed, re-configure the rate policy */
  188. if (unlikely(sta_rates)) {
  189. wl->rate_set = wl1271_tx_enabled_rates_get(wl, sta_rates);
  190. wl1271_acx_rate_policies(wl);
  191. }
  192. /* Prepare the transfer buffer, by aggregating all
  193. * available packets */
  194. buf_offset = 0;
  195. while ((skb = skb_dequeue(&wl->tx_queue))) {
  196. if (!woken_up) {
  197. ret = wl1271_ps_elp_wakeup(wl, false);
  198. if (ret < 0)
  199. goto out_ack;
  200. woken_up = true;
  201. }
  202. ret = wl1271_prepare_tx_frame(wl, skb, buf_offset);
  203. if (ret == -EBUSY) {
  204. /*
  205. * Either the firmware buffer is full, or the
  206. * aggregation buffer is.
  207. * Queue back last skb, and stop aggregating.
  208. */
  209. skb_queue_head(&wl->tx_queue, skb);
  210. goto out_ack;
  211. } else if (ret < 0) {
  212. dev_kfree_skb(skb);
  213. goto out_ack;
  214. }
  215. buf_offset += ret;
  216. wl->tx_packets_count++;
  217. }
  218. out_ack:
  219. if (buf_offset) {
  220. wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
  221. buf_offset, true);
  222. /* interrupt the firmware with the new packets */
  223. wl1271_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
  224. }
  225. out:
  226. if (woken_up)
  227. wl1271_ps_elp_sleep(wl);
  228. mutex_unlock(&wl->mutex);
  229. }
  230. static void wl1271_tx_complete_packet(struct wl1271 *wl,
  231. struct wl1271_tx_hw_res_descr *result)
  232. {
  233. struct ieee80211_tx_info *info;
  234. struct sk_buff *skb;
  235. int id = result->id;
  236. int rate = -1;
  237. u8 retries = 0;
  238. /* check for id legality */
  239. if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
  240. wl1271_warning("TX result illegal id: %d", id);
  241. return;
  242. }
  243. skb = wl->tx_frames[id];
  244. info = IEEE80211_SKB_CB(skb);
  245. /* update the TX status info */
  246. if (result->status == TX_SUCCESS) {
  247. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  248. info->flags |= IEEE80211_TX_STAT_ACK;
  249. rate = wl1271_rate_to_idx(wl, result->rate_class_index);
  250. retries = result->ack_failures;
  251. } else if (result->status == TX_RETRY_EXCEEDED) {
  252. wl->stats.excessive_retries++;
  253. retries = result->ack_failures;
  254. }
  255. info->status.rates[0].idx = rate;
  256. info->status.rates[0].count = retries;
  257. info->status.rates[0].flags = 0;
  258. info->status.ack_signal = -1;
  259. wl->stats.retry_count += result->ack_failures;
  260. /* update security sequence number */
  261. wl->tx_security_seq += (result->lsb_security_sequence_number -
  262. wl->tx_security_last_seq);
  263. wl->tx_security_last_seq = result->lsb_security_sequence_number;
  264. /* remove private header from packet */
  265. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  266. /* remove TKIP header space if present */
  267. if (info->control.hw_key &&
  268. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  269. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  270. memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
  271. skb_pull(skb, WL1271_TKIP_IV_SPACE);
  272. }
  273. wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  274. " status 0x%x",
  275. result->id, skb, result->ack_failures,
  276. result->rate_class_index, result->status);
  277. /* return the packet to the stack */
  278. ieee80211_tx_status(wl->hw, skb);
  279. wl->tx_frames[result->id] = NULL;
  280. wl->tx_frames_cnt--;
  281. }
  282. /* Called upon reception of a TX complete interrupt */
  283. void wl1271_tx_complete(struct wl1271 *wl)
  284. {
  285. struct wl1271_acx_mem_map *memmap =
  286. (struct wl1271_acx_mem_map *)wl->target_mem_map;
  287. u32 count, fw_counter;
  288. u32 i;
  289. /* read the tx results from the chipset */
  290. wl1271_read(wl, le32_to_cpu(memmap->tx_result),
  291. wl->tx_res_if, sizeof(*wl->tx_res_if), false);
  292. fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
  293. /* write host counter to chipset (to ack) */
  294. wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
  295. offsetof(struct wl1271_tx_hw_res_if,
  296. tx_result_host_counter), fw_counter);
  297. count = fw_counter - wl->tx_results_count;
  298. wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
  299. /* verify that the result buffer is not getting overrun */
  300. if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
  301. wl1271_warning("TX result overflow from chipset: %d", count);
  302. /* process the results */
  303. for (i = 0; i < count; i++) {
  304. struct wl1271_tx_hw_res_descr *result;
  305. u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
  306. /* process the packet */
  307. result = &(wl->tx_res_if->tx_results_queue[offset]);
  308. wl1271_tx_complete_packet(wl, result);
  309. wl->tx_results_count++;
  310. }
  311. if (test_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags) &&
  312. skb_queue_len(&wl->tx_queue) <= WL1271_TX_QUEUE_LOW_WATERMARK) {
  313. unsigned long flags;
  314. /* firmware buffer has space, restart queues */
  315. wl1271_debug(DEBUG_TX, "tx_complete: waking queues");
  316. spin_lock_irqsave(&wl->wl_lock, flags);
  317. ieee80211_wake_queues(wl->hw);
  318. clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
  319. spin_unlock_irqrestore(&wl->wl_lock, flags);
  320. ieee80211_queue_work(wl->hw, &wl->tx_work);
  321. }
  322. }
  323. /* caller must hold wl->mutex */
  324. void wl1271_tx_reset(struct wl1271 *wl)
  325. {
  326. int i;
  327. struct sk_buff *skb;
  328. /* TX failure */
  329. while ((skb = skb_dequeue(&wl->tx_queue))) {
  330. wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
  331. ieee80211_tx_status(wl->hw, skb);
  332. }
  333. for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
  334. if (wl->tx_frames[i] != NULL) {
  335. skb = wl->tx_frames[i];
  336. wl->tx_frames[i] = NULL;
  337. wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
  338. ieee80211_tx_status(wl->hw, skb);
  339. }
  340. wl->tx_frames_cnt = 0;
  341. }
  342. #define WL1271_TX_FLUSH_TIMEOUT 500000
  343. /* caller must *NOT* hold wl->mutex */
  344. void wl1271_tx_flush(struct wl1271 *wl)
  345. {
  346. unsigned long timeout;
  347. timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
  348. while (!time_after(jiffies, timeout)) {
  349. mutex_lock(&wl->mutex);
  350. wl1271_debug(DEBUG_TX, "flushing tx buffer: %d",
  351. wl->tx_frames_cnt);
  352. if ((wl->tx_frames_cnt == 0) &&
  353. skb_queue_empty(&wl->tx_queue)) {
  354. mutex_unlock(&wl->mutex);
  355. return;
  356. }
  357. mutex_unlock(&wl->mutex);
  358. msleep(1);
  359. }
  360. wl1271_warning("Unable to flush all TX buffers, timed out.");
  361. }