wl1271_tx.c 12 KB

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