tx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 "wl12xx.h"
  26. #include "io.h"
  27. #include "reg.h"
  28. #include "ps.h"
  29. #include "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_WL12XX_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. wl->tx_queue_count <= 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. static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
  196. {
  197. struct sk_buff *skb = NULL;
  198. unsigned long flags;
  199. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VO]);
  200. if (skb)
  201. goto out;
  202. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VI]);
  203. if (skb)
  204. goto out;
  205. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BE]);
  206. if (skb)
  207. goto out;
  208. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BK]);
  209. out:
  210. if (skb) {
  211. spin_lock_irqsave(&wl->wl_lock, flags);
  212. wl->tx_queue_count--;
  213. spin_unlock_irqrestore(&wl->wl_lock, flags);
  214. }
  215. return skb;
  216. }
  217. static void wl1271_skb_queue_head(struct wl1271 *wl, struct sk_buff *skb)
  218. {
  219. unsigned long flags;
  220. int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  221. skb_queue_head(&wl->tx_queue[q], skb);
  222. spin_lock_irqsave(&wl->wl_lock, flags);
  223. wl->tx_queue_count++;
  224. spin_unlock_irqrestore(&wl->wl_lock, flags);
  225. }
  226. void wl1271_tx_work_locked(struct wl1271 *wl)
  227. {
  228. struct sk_buff *skb;
  229. bool woken_up = false;
  230. u32 sta_rates = 0;
  231. u32 buf_offset = 0;
  232. bool sent_packets = false;
  233. int ret;
  234. /* check if the rates supported by the AP have changed */
  235. if (unlikely(test_and_clear_bit(WL1271_FLAG_STA_RATES_CHANGED,
  236. &wl->flags))) {
  237. unsigned long flags;
  238. spin_lock_irqsave(&wl->wl_lock, flags);
  239. sta_rates = wl->sta_rate_set;
  240. spin_unlock_irqrestore(&wl->wl_lock, flags);
  241. }
  242. if (unlikely(wl->state == WL1271_STATE_OFF))
  243. goto out;
  244. /* if rates have changed, re-configure the rate policy */
  245. if (unlikely(sta_rates)) {
  246. ret = wl1271_ps_elp_wakeup(wl, false);
  247. if (ret < 0)
  248. goto out;
  249. woken_up = true;
  250. wl->rate_set = wl1271_tx_enabled_rates_get(wl, sta_rates);
  251. wl1271_acx_rate_policies(wl);
  252. }
  253. while ((skb = wl1271_skb_dequeue(wl))) {
  254. if (!woken_up) {
  255. ret = wl1271_ps_elp_wakeup(wl, false);
  256. if (ret < 0)
  257. goto out_ack;
  258. woken_up = true;
  259. }
  260. ret = wl1271_prepare_tx_frame(wl, skb, buf_offset);
  261. if (ret == -EAGAIN) {
  262. /*
  263. * Aggregation buffer is full.
  264. * Flush buffer and try again.
  265. */
  266. wl1271_skb_queue_head(wl, skb);
  267. wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
  268. buf_offset, true);
  269. sent_packets = true;
  270. buf_offset = 0;
  271. continue;
  272. } else if (ret == -EBUSY) {
  273. /*
  274. * Firmware buffer is full.
  275. * Queue back last skb, and stop aggregating.
  276. */
  277. wl1271_skb_queue_head(wl, skb);
  278. /* No work left, avoid scheduling redundant tx work */
  279. set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
  280. goto out_ack;
  281. } else if (ret < 0) {
  282. dev_kfree_skb(skb);
  283. goto out_ack;
  284. }
  285. buf_offset += ret;
  286. wl->tx_packets_count++;
  287. }
  288. out_ack:
  289. if (buf_offset) {
  290. wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
  291. buf_offset, true);
  292. sent_packets = true;
  293. }
  294. if (sent_packets) {
  295. /* interrupt the firmware with the new packets */
  296. wl1271_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
  297. handle_tx_low_watermark(wl);
  298. }
  299. out:
  300. if (woken_up)
  301. wl1271_ps_elp_sleep(wl);
  302. }
  303. void wl1271_tx_work(struct work_struct *work)
  304. {
  305. struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
  306. mutex_lock(&wl->mutex);
  307. wl1271_tx_work_locked(wl);
  308. mutex_unlock(&wl->mutex);
  309. }
  310. static void wl1271_tx_complete_packet(struct wl1271 *wl,
  311. struct wl1271_tx_hw_res_descr *result)
  312. {
  313. struct ieee80211_tx_info *info;
  314. struct sk_buff *skb;
  315. int id = result->id;
  316. int rate = -1;
  317. u8 retries = 0;
  318. /* check for id legality */
  319. if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
  320. wl1271_warning("TX result illegal id: %d", id);
  321. return;
  322. }
  323. skb = wl->tx_frames[id];
  324. info = IEEE80211_SKB_CB(skb);
  325. /* update the TX status info */
  326. if (result->status == TX_SUCCESS) {
  327. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  328. info->flags |= IEEE80211_TX_STAT_ACK;
  329. rate = wl1271_rate_to_idx(result->rate_class_index, wl->band);
  330. retries = result->ack_failures;
  331. } else if (result->status == TX_RETRY_EXCEEDED) {
  332. wl->stats.excessive_retries++;
  333. retries = result->ack_failures;
  334. }
  335. info->status.rates[0].idx = rate;
  336. info->status.rates[0].count = retries;
  337. info->status.rates[0].flags = 0;
  338. info->status.ack_signal = -1;
  339. wl->stats.retry_count += result->ack_failures;
  340. /* update security sequence number */
  341. wl->tx_security_seq += (result->lsb_security_sequence_number -
  342. wl->tx_security_last_seq);
  343. wl->tx_security_last_seq = result->lsb_security_sequence_number;
  344. /* remove private header from packet */
  345. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  346. /* remove TKIP header space if present */
  347. if (info->control.hw_key &&
  348. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  349. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  350. memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
  351. skb_pull(skb, WL1271_TKIP_IV_SPACE);
  352. }
  353. wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  354. " status 0x%x",
  355. result->id, skb, result->ack_failures,
  356. result->rate_class_index, result->status);
  357. /* return the packet to the stack */
  358. ieee80211_tx_status(wl->hw, skb);
  359. wl1271_free_tx_id(wl, result->id);
  360. }
  361. /* Called upon reception of a TX complete interrupt */
  362. void wl1271_tx_complete(struct wl1271 *wl)
  363. {
  364. struct wl1271_acx_mem_map *memmap =
  365. (struct wl1271_acx_mem_map *)wl->target_mem_map;
  366. u32 count, fw_counter;
  367. u32 i;
  368. /* read the tx results from the chipset */
  369. wl1271_read(wl, le32_to_cpu(memmap->tx_result),
  370. wl->tx_res_if, sizeof(*wl->tx_res_if), false);
  371. fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
  372. /* write host counter to chipset (to ack) */
  373. wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
  374. offsetof(struct wl1271_tx_hw_res_if,
  375. tx_result_host_counter), fw_counter);
  376. count = fw_counter - wl->tx_results_count;
  377. wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
  378. /* verify that the result buffer is not getting overrun */
  379. if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
  380. wl1271_warning("TX result overflow from chipset: %d", count);
  381. /* process the results */
  382. for (i = 0; i < count; i++) {
  383. struct wl1271_tx_hw_res_descr *result;
  384. u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
  385. /* process the packet */
  386. result = &(wl->tx_res_if->tx_results_queue[offset]);
  387. wl1271_tx_complete_packet(wl, result);
  388. wl->tx_results_count++;
  389. }
  390. }
  391. /* caller must hold wl->mutex */
  392. void wl1271_tx_reset(struct wl1271 *wl)
  393. {
  394. int i;
  395. struct sk_buff *skb;
  396. /* TX failure */
  397. for (i = 0; i < NUM_TX_QUEUES; i++) {
  398. while ((skb = skb_dequeue(&wl->tx_queue[i]))) {
  399. wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
  400. ieee80211_tx_status(wl->hw, skb);
  401. }
  402. }
  403. wl->tx_queue_count = 0;
  404. /*
  405. * Make sure the driver is at a consistent state, in case this
  406. * function is called from a context other than interface removal.
  407. */
  408. handle_tx_low_watermark(wl);
  409. for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
  410. if (wl->tx_frames[i] != NULL) {
  411. skb = wl->tx_frames[i];
  412. wl1271_free_tx_id(wl, i);
  413. wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
  414. ieee80211_tx_status(wl->hw, skb);
  415. }
  416. }
  417. #define WL1271_TX_FLUSH_TIMEOUT 500000
  418. /* caller must *NOT* hold wl->mutex */
  419. void wl1271_tx_flush(struct wl1271 *wl)
  420. {
  421. unsigned long timeout;
  422. timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
  423. while (!time_after(jiffies, timeout)) {
  424. mutex_lock(&wl->mutex);
  425. wl1271_debug(DEBUG_TX, "flushing tx buffer: %d",
  426. wl->tx_frames_cnt);
  427. if ((wl->tx_frames_cnt == 0) && (wl->tx_queue_count == 0)) {
  428. mutex_unlock(&wl->mutex);
  429. return;
  430. }
  431. mutex_unlock(&wl->mutex);
  432. msleep(1);
  433. }
  434. wl1271_warning("Unable to flush all TX buffers, timed out.");
  435. }