wl1271_tx.c 10 KB

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