wl1271_tx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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, ac;
  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. /* queue */
  93. ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  94. desc->tid = wl1271_tx_ac_to_tid(ac);
  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. /* if the packets are destined for AP (have a STA entry) send them
  104. with AP rate policies, otherwise use default basic rates */
  105. if (control->control.sta)
  106. tx_attr |= ACX_TX_AP_FULL_RATE << TX_HW_ATTR_OFST_RATE_POLICY;
  107. desc->tx_attr = cpu_to_le16(tx_attr);
  108. wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d", pad);
  109. return 0;
  110. }
  111. static int wl1271_tx_send_packet(struct wl1271 *wl, struct sk_buff *skb,
  112. struct ieee80211_tx_info *control)
  113. {
  114. struct wl1271_tx_hw_descr *desc;
  115. int len;
  116. /* FIXME: This is a workaround for getting non-aligned packets.
  117. This happens at least with EAPOL packets from the user space.
  118. Our DMA requires packets to be aligned on a 4-byte boundary.
  119. */
  120. if (unlikely((long)skb->data & 0x03)) {
  121. int offset = (4 - (long)skb->data) & 0x03;
  122. wl1271_debug(DEBUG_TX, "skb offset %d", offset);
  123. /* check whether the current skb can be used */
  124. if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
  125. unsigned char *src = skb->data;
  126. /* align the buffer on a 4-byte boundary */
  127. skb_reserve(skb, offset);
  128. memmove(skb->data, src, skb->len);
  129. } else {
  130. wl1271_info("No handler, fixme!");
  131. return -EINVAL;
  132. }
  133. }
  134. len = WL1271_TX_ALIGN(skb->len);
  135. /* perform a fixed address block write with the packet */
  136. wl1271_spi_write(wl, WL1271_SLV_MEM_DATA, skb->data, len, true);
  137. /* write packet new counter into the write access register */
  138. wl->tx_packets_count++;
  139. wl1271_spi_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
  140. desc = (struct wl1271_tx_hw_descr *) skb->data;
  141. wl1271_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u (%u words)",
  142. desc->id, skb, len, desc->length);
  143. return 0;
  144. }
  145. /* caller must hold wl->mutex */
  146. static int wl1271_tx_frame(struct wl1271 *wl, struct sk_buff *skb)
  147. {
  148. struct ieee80211_tx_info *info;
  149. u32 extra = 0;
  150. int ret = 0;
  151. u8 idx;
  152. if (!skb)
  153. return -EINVAL;
  154. info = IEEE80211_SKB_CB(skb);
  155. if (info->control.hw_key &&
  156. info->control.hw_key->alg == ALG_TKIP)
  157. extra = WL1271_TKIP_IV_SPACE;
  158. if (info->control.hw_key) {
  159. idx = info->control.hw_key->hw_key_idx;
  160. /* FIXME: do we have to do this if we're not using WEP? */
  161. if (unlikely(wl->default_key != idx)) {
  162. ret = wl1271_cmd_set_default_wep_key(wl, idx);
  163. if (ret < 0)
  164. return ret;
  165. wl->default_key = idx;
  166. }
  167. }
  168. ret = wl1271_tx_allocate(wl, skb, extra);
  169. if (ret < 0)
  170. return ret;
  171. ret = wl1271_tx_fill_hdr(wl, skb, extra, info);
  172. if (ret < 0)
  173. return ret;
  174. ret = wl1271_tx_send_packet(wl, skb, info);
  175. if (ret < 0)
  176. return ret;
  177. return ret;
  178. }
  179. static u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
  180. {
  181. struct ieee80211_supported_band *band;
  182. u32 enabled_rates = 0;
  183. int bit;
  184. band = wl->hw->wiphy->bands[wl->band];
  185. for (bit = 0; bit < band->n_bitrates; bit++) {
  186. if (rate_set & 0x1)
  187. enabled_rates |= band->bitrates[bit].hw_value;
  188. rate_set >>= 1;
  189. }
  190. return enabled_rates;
  191. }
  192. void wl1271_tx_work(struct work_struct *work)
  193. {
  194. struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
  195. struct sk_buff *skb;
  196. bool woken_up = false;
  197. u32 sta_rates = 0;
  198. int ret;
  199. /* check if the rates supported by the AP have changed */
  200. if (unlikely(test_and_clear_bit(WL1271_FLAG_STA_RATES_CHANGED,
  201. &wl->flags))) {
  202. unsigned long flags;
  203. spin_lock_irqsave(&wl->wl_lock, flags);
  204. sta_rates = wl->sta_rate_set;
  205. spin_unlock_irqrestore(&wl->wl_lock, flags);
  206. }
  207. mutex_lock(&wl->mutex);
  208. if (unlikely(wl->state == WL1271_STATE_OFF))
  209. goto out;
  210. /* if rates have changed, re-configure the rate policy */
  211. if (unlikely(sta_rates)) {
  212. wl->rate_set = wl1271_tx_enabled_rates_get(wl, sta_rates);
  213. wl1271_acx_rate_policies(wl);
  214. }
  215. while ((skb = skb_dequeue(&wl->tx_queue))) {
  216. if (!woken_up) {
  217. ret = wl1271_ps_elp_wakeup(wl, false);
  218. if (ret < 0)
  219. goto out;
  220. woken_up = true;
  221. }
  222. ret = wl1271_tx_frame(wl, skb);
  223. if (ret == -EBUSY) {
  224. /* firmware buffer is full, stop queues */
  225. wl1271_debug(DEBUG_TX, "tx_work: fw buffer full, "
  226. "stop queues");
  227. ieee80211_stop_queues(wl->hw);
  228. set_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
  229. skb_queue_head(&wl->tx_queue, skb);
  230. goto out;
  231. } else if (ret < 0) {
  232. dev_kfree_skb(skb);
  233. goto out;
  234. } else if (test_and_clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED,
  235. &wl->flags)) {
  236. /* firmware buffer has space, restart queues */
  237. wl1271_debug(DEBUG_TX,
  238. "complete_packet: waking queues");
  239. ieee80211_wake_queues(wl->hw);
  240. }
  241. }
  242. out:
  243. if (woken_up)
  244. wl1271_ps_elp_sleep(wl);
  245. mutex_unlock(&wl->mutex);
  246. }
  247. static void wl1271_tx_complete_packet(struct wl1271 *wl,
  248. struct wl1271_tx_hw_res_descr *result)
  249. {
  250. struct ieee80211_tx_info *info;
  251. struct sk_buff *skb;
  252. u16 seq;
  253. int id = result->id;
  254. /* check for id legality */
  255. if (id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL) {
  256. wl1271_warning("TX result illegal id: %d", id);
  257. return;
  258. }
  259. skb = wl->tx_frames[id];
  260. info = IEEE80211_SKB_CB(skb);
  261. /* update packet status */
  262. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  263. if (result->status == TX_SUCCESS)
  264. info->flags |= IEEE80211_TX_STAT_ACK;
  265. if (result->status & TX_RETRY_EXCEEDED) {
  266. /* FIXME */
  267. /* info->status.excessive_retries = 1; */
  268. wl->stats.excessive_retries++;
  269. }
  270. }
  271. /* FIXME */
  272. /* info->status.retry_count = result->ack_failures; */
  273. wl->stats.retry_count += result->ack_failures;
  274. /* update security sequence number */
  275. seq = wl->tx_security_seq_16 +
  276. (result->lsb_security_sequence_number -
  277. wl->tx_security_last_seq);
  278. wl->tx_security_last_seq = result->lsb_security_sequence_number;
  279. if (seq < wl->tx_security_seq_16)
  280. wl->tx_security_seq_32++;
  281. wl->tx_security_seq_16 = seq;
  282. /* remove private header from packet */
  283. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  284. /* remove TKIP header space if present */
  285. if (info->control.hw_key &&
  286. info->control.hw_key->alg == ALG_TKIP) {
  287. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  288. memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
  289. skb_pull(skb, WL1271_TKIP_IV_SPACE);
  290. }
  291. wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  292. " status 0x%x",
  293. result->id, skb, result->ack_failures,
  294. result->rate_class_index, result->status);
  295. /* return the packet to the stack */
  296. ieee80211_tx_status(wl->hw, skb);
  297. wl->tx_frames[result->id] = NULL;
  298. }
  299. /* Called upon reception of a TX complete interrupt */
  300. void wl1271_tx_complete(struct wl1271 *wl, u32 count)
  301. {
  302. struct wl1271_acx_mem_map *memmap =
  303. (struct wl1271_acx_mem_map *)wl->target_mem_map;
  304. u32 i;
  305. wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
  306. /* read the tx results from the chipset */
  307. wl1271_spi_read(wl, le32_to_cpu(memmap->tx_result),
  308. wl->tx_res_if, sizeof(*wl->tx_res_if), false);
  309. /* verify that the result buffer is not getting overrun */
  310. if (count > TX_HW_RESULT_QUEUE_LEN) {
  311. wl1271_warning("TX result overflow from chipset: %d", count);
  312. count = TX_HW_RESULT_QUEUE_LEN;
  313. }
  314. /* process the results */
  315. for (i = 0; i < count; i++) {
  316. struct wl1271_tx_hw_res_descr *result;
  317. u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
  318. /* process the packet */
  319. result = &(wl->tx_res_if->tx_results_queue[offset]);
  320. wl1271_tx_complete_packet(wl, result);
  321. wl->tx_results_count++;
  322. }
  323. /* write host counter to chipset (to ack) */
  324. wl1271_spi_write32(wl, le32_to_cpu(memmap->tx_result) +
  325. offsetof(struct wl1271_tx_hw_res_if,
  326. tx_result_host_counter),
  327. le32_to_cpu(wl->tx_res_if->tx_result_fw_counter));
  328. }
  329. /* caller must hold wl->mutex */
  330. void wl1271_tx_flush(struct wl1271 *wl)
  331. {
  332. int i;
  333. struct sk_buff *skb;
  334. struct ieee80211_tx_info *info;
  335. /* TX failure */
  336. /* control->flags = 0; FIXME */
  337. while ((skb = skb_dequeue(&wl->tx_queue))) {
  338. info = IEEE80211_SKB_CB(skb);
  339. wl1271_debug(DEBUG_TX, "flushing skb 0x%p", skb);
  340. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  341. continue;
  342. ieee80211_tx_status(wl->hw, skb);
  343. }
  344. for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
  345. if (wl->tx_frames[i] != NULL) {
  346. skb = wl->tx_frames[i];
  347. info = IEEE80211_SKB_CB(skb);
  348. if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
  349. continue;
  350. ieee80211_tx_status(wl->hw, skb);
  351. wl->tx_frames[i] = NULL;
  352. }
  353. }