wl1271_tx.c 13 KB

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