wl1271_tx.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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_spi.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 < FW_TX_CMPLT_BLOCK_SIZE; i++)
  34. if (wl->tx_frames[i] == NULL) {
  35. wl->tx_frames[i] = skb;
  36. return i;
  37. }
  38. return -EBUSY;
  39. }
  40. static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra)
  41. {
  42. struct wl1271_tx_hw_descr *desc;
  43. u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
  44. u32 total_blocks, excluded;
  45. int id, ret = -EBUSY;
  46. /* allocate free identifier for the packet */
  47. id = wl1271_tx_id(wl, skb);
  48. if (id < 0)
  49. return id;
  50. /* approximate the number of blocks required for this packet
  51. in the firmware */
  52. /* FIXME: try to figure out what is done here and make it cleaner */
  53. total_blocks = (skb->len) >> TX_HW_BLOCK_SHIFT_DIV;
  54. excluded = (total_blocks << 2) + (skb->len & 0xff) + 34;
  55. total_blocks += (excluded > 252) ? 2 : 1;
  56. total_blocks += TX_HW_BLOCK_SPARE;
  57. if (total_blocks <= wl->tx_blocks_available) {
  58. desc = (struct wl1271_tx_hw_descr *)skb_push(
  59. skb, total_len - skb->len);
  60. desc->extra_mem_blocks = TX_HW_BLOCK_SPARE;
  61. desc->total_mem_blocks = total_blocks;
  62. desc->id = id;
  63. wl->tx_blocks_available -= total_blocks;
  64. ret = 0;
  65. wl1271_debug(DEBUG_TX,
  66. "tx_allocate: size: %d, blocks: %d, id: %d",
  67. total_len, total_blocks, id);
  68. } else
  69. wl->tx_frames[id] = NULL;
  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 wl1271_tx_hw_descr *desc;
  76. int pad;
  77. desc = (struct wl1271_tx_hw_descr *) skb->data;
  78. /* configure packet life time */
  79. desc->start_time = jiffies_to_usecs(jiffies) - wl->time_offset;
  80. desc->life_time = TX_HW_MGMT_PKT_LIFETIME_TU;
  81. /* configure the tx attributes */
  82. desc->tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
  83. /* FIXME: do we know the packet priority? can we identify mgmt
  84. packets, and use max prio for them at least? */
  85. desc->tid = 0;
  86. desc->aid = TX_HW_DEFAULT_AID;
  87. desc->reserved = 0;
  88. /* align the length (and store in terms of words) */
  89. pad = WL1271_TX_ALIGN(skb->len);
  90. desc->length = pad >> 2;
  91. /* calculate number of padding bytes */
  92. pad = pad - skb->len;
  93. desc->tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
  94. wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d", pad);
  95. return 0;
  96. }
  97. static int wl1271_tx_send_packet(struct wl1271 *wl, struct sk_buff *skb,
  98. struct ieee80211_tx_info *control)
  99. {
  100. struct wl1271_tx_hw_descr *desc;
  101. int len;
  102. /* FIXME: This is a workaround for getting non-aligned packets.
  103. This happens at least with EAPOL packets from the user space.
  104. Our DMA requires packets to be aligned on a 4-byte boundary.
  105. */
  106. if (unlikely((long)skb->data & 0x03)) {
  107. int offset = (4 - (long)skb->data) & 0x03;
  108. wl1271_debug(DEBUG_TX, "skb offset %d", offset);
  109. /* check whether the current skb can be used */
  110. if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
  111. unsigned char *src = skb->data;
  112. /* align the buffer on a 4-byte boundary */
  113. skb_reserve(skb, offset);
  114. memmove(skb->data, src, skb->len);
  115. } else {
  116. wl1271_info("No handler, fixme!");
  117. return -EINVAL;
  118. }
  119. }
  120. len = WL1271_TX_ALIGN(skb->len);
  121. /* perform a fixed address block write with the packet */
  122. wl1271_spi_reg_write(wl, WL1271_SLV_MEM_DATA, skb->data, len, true);
  123. /* write packet new counter into the write access register */
  124. wl->tx_packets_count++;
  125. wl1271_reg_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
  126. desc = (struct wl1271_tx_hw_descr *) skb->data;
  127. wl1271_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u (%u words)",
  128. desc->id, skb, len, desc->length);
  129. return 0;
  130. }
  131. /* caller must hold wl->mutex */
  132. static int wl1271_tx_frame(struct wl1271 *wl, struct sk_buff *skb)
  133. {
  134. struct ieee80211_tx_info *info;
  135. u32 extra = 0;
  136. int ret = 0;
  137. u8 idx;
  138. if (!skb)
  139. return -EINVAL;
  140. info = IEEE80211_SKB_CB(skb);
  141. if (info->control.hw_key &&
  142. info->control.hw_key->alg == ALG_TKIP)
  143. extra = WL1271_TKIP_IV_SPACE;
  144. if (info->control.hw_key) {
  145. idx = info->control.hw_key->hw_key_idx;
  146. /* FIXME: do we have to do this if we're not using WEP? */
  147. if (unlikely(wl->default_key != idx)) {
  148. ret = wl1271_cmd_set_default_wep_key(wl, idx);
  149. if (ret < 0)
  150. return ret;
  151. }
  152. }
  153. ret = wl1271_tx_allocate(wl, skb, extra);
  154. if (ret < 0)
  155. return ret;
  156. ret = wl1271_tx_fill_hdr(wl, skb, extra, info);
  157. if (ret < 0)
  158. return ret;
  159. ret = wl1271_tx_send_packet(wl, skb, info);
  160. if (ret < 0)
  161. return ret;
  162. return ret;
  163. }
  164. void wl1271_tx_work(struct work_struct *work)
  165. {
  166. struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
  167. struct sk_buff *skb;
  168. bool woken_up = false;
  169. int ret;
  170. mutex_lock(&wl->mutex);
  171. if (unlikely(wl->state == WL1271_STATE_OFF))
  172. goto out;
  173. while ((skb = skb_dequeue(&wl->tx_queue))) {
  174. if (!woken_up) {
  175. ret = wl1271_ps_elp_wakeup(wl, false);
  176. if (ret < 0)
  177. goto out;
  178. woken_up = true;
  179. }
  180. ret = wl1271_tx_frame(wl, skb);
  181. if (ret == -EBUSY) {
  182. /* firmware buffer is full, stop queues */
  183. wl1271_debug(DEBUG_TX, "tx_work: fw buffer full, "
  184. "stop queues");
  185. ieee80211_stop_queues(wl->hw);
  186. wl->tx_queue_stopped = true;
  187. skb_queue_head(&wl->tx_queue, skb);
  188. goto out;
  189. } else if (ret < 0) {
  190. dev_kfree_skb(skb);
  191. goto out;
  192. } else if (wl->tx_queue_stopped) {
  193. /* firmware buffer has space, restart queues */
  194. wl1271_debug(DEBUG_TX,
  195. "complete_packet: waking queues");
  196. ieee80211_wake_queues(wl->hw);
  197. wl->tx_queue_stopped = false;
  198. }
  199. }
  200. out:
  201. if (woken_up)
  202. wl1271_ps_elp_sleep(wl);
  203. mutex_unlock(&wl->mutex);
  204. }
  205. static void wl1271_tx_complete_packet(struct wl1271 *wl,
  206. struct wl1271_tx_hw_res_descr *result)
  207. {
  208. struct ieee80211_tx_info *info;
  209. struct sk_buff *skb;
  210. u32 header_len;
  211. int id = result->id;
  212. /* check for id legality */
  213. if (id >= TX_HW_RESULT_QUEUE_LEN || wl->tx_frames[id] == NULL) {
  214. wl1271_warning("TX result illegal id: %d", id);
  215. return;
  216. }
  217. skb = wl->tx_frames[id];
  218. info = IEEE80211_SKB_CB(skb);
  219. /* update packet status */
  220. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  221. if (result->status == TX_SUCCESS)
  222. info->flags |= IEEE80211_TX_STAT_ACK;
  223. if (result->status & TX_RETRY_EXCEEDED) {
  224. /* FIXME */
  225. /* info->status.excessive_retries = 1; */
  226. wl->stats.excessive_retries++;
  227. }
  228. }
  229. /* FIXME */
  230. /* info->status.retry_count = result->ack_failures; */
  231. wl->stats.retry_count += result->ack_failures;
  232. /* get header len */
  233. if (info->control.hw_key &&
  234. info->control.hw_key->alg == ALG_TKIP)
  235. header_len = WL1271_TKIP_IV_SPACE +
  236. sizeof(struct wl1271_tx_hw_descr);
  237. else
  238. header_len = sizeof(struct wl1271_tx_hw_descr);
  239. wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  240. " status 0x%x",
  241. result->id, skb, result->ack_failures,
  242. result->rate_class_index, result->status);
  243. /* remove private header from packet */
  244. skb_pull(skb, header_len);
  245. /* return the packet to the stack */
  246. ieee80211_tx_status(wl->hw, skb);
  247. wl->tx_frames[result->id] = NULL;
  248. }
  249. /* Called upon reception of a TX complete interrupt */
  250. void wl1271_tx_complete(struct wl1271 *wl, u32 count)
  251. {
  252. struct wl1271_acx_mem_map *memmap =
  253. (struct wl1271_acx_mem_map *)wl->target_mem_map;
  254. u32 i;
  255. wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
  256. /* read the tx results from the chipset */
  257. wl1271_spi_mem_read(wl, memmap->tx_result,
  258. wl->tx_res_if, sizeof(*wl->tx_res_if));
  259. /* verify that the result buffer is not getting overrun */
  260. if (count > TX_HW_RESULT_QUEUE_LEN) {
  261. wl1271_warning("TX result overflow from chipset: %d", count);
  262. count = TX_HW_RESULT_QUEUE_LEN;
  263. }
  264. /* process the results */
  265. for (i = 0; i < count; i++) {
  266. struct wl1271_tx_hw_res_descr *result;
  267. u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
  268. /* process the packet */
  269. result = &(wl->tx_res_if->tx_results_queue[offset]);
  270. wl1271_tx_complete_packet(wl, result);
  271. wl->tx_results_count++;
  272. }
  273. /* write host counter to chipset (to ack) */
  274. wl1271_mem_write32(wl, memmap->tx_result +
  275. offsetof(struct wl1271_tx_hw_res_if,
  276. tx_result_host_counter),
  277. wl->tx_res_if->tx_result_fw_counter);
  278. }
  279. /* caller must hold wl->mutex */
  280. void wl1271_tx_flush(struct wl1271 *wl)
  281. {
  282. int i;
  283. struct sk_buff *skb;
  284. struct ieee80211_tx_info *info;
  285. /* TX failure */
  286. /* control->flags = 0; FIXME */
  287. while ((skb = skb_dequeue(&wl->tx_queue))) {
  288. info = IEEE80211_SKB_CB(skb);
  289. wl1271_debug(DEBUG_TX, "flushing skb 0x%p", skb);
  290. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  291. continue;
  292. ieee80211_tx_status(wl->hw, skb);
  293. }
  294. for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
  295. if (wl->tx_frames[i] != NULL) {
  296. skb = wl->tx_frames[i];
  297. info = IEEE80211_SKB_CB(skb);
  298. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  299. continue;
  300. ieee80211_tx_status(wl->hw, skb);
  301. wl->tx_frames[i] = NULL;
  302. }
  303. }