tx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 <linux/etherdevice.h>
  26. #include "wl12xx.h"
  27. #include "io.h"
  28. #include "reg.h"
  29. #include "ps.h"
  30. #include "tx.h"
  31. static int wl1271_set_default_wep_key(struct wl1271 *wl, u8 id)
  32. {
  33. int ret;
  34. bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
  35. if (is_ap)
  36. ret = wl1271_cmd_set_ap_default_wep_key(wl, id);
  37. else
  38. ret = wl1271_cmd_set_sta_default_wep_key(wl, id);
  39. if (ret < 0)
  40. return ret;
  41. wl1271_debug(DEBUG_CRYPT, "default wep key idx: %d", (int)id);
  42. return 0;
  43. }
  44. static int wl1271_alloc_tx_id(struct wl1271 *wl, struct sk_buff *skb)
  45. {
  46. int id;
  47. id = find_first_zero_bit(wl->tx_frames_map, ACX_TX_DESCRIPTORS);
  48. if (id >= ACX_TX_DESCRIPTORS)
  49. return -EBUSY;
  50. __set_bit(id, wl->tx_frames_map);
  51. wl->tx_frames[id] = skb;
  52. wl->tx_frames_cnt++;
  53. return id;
  54. }
  55. static void wl1271_free_tx_id(struct wl1271 *wl, int id)
  56. {
  57. if (__test_and_clear_bit(id, wl->tx_frames_map)) {
  58. wl->tx_frames[id] = NULL;
  59. wl->tx_frames_cnt--;
  60. }
  61. }
  62. static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra,
  63. u32 buf_offset)
  64. {
  65. struct wl1271_tx_hw_descr *desc;
  66. u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
  67. u32 total_blocks;
  68. int id, ret = -EBUSY;
  69. if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
  70. return -EAGAIN;
  71. /* allocate free identifier for the packet */
  72. id = wl1271_alloc_tx_id(wl, skb);
  73. if (id < 0)
  74. return id;
  75. /* approximate the number of blocks required for this packet
  76. in the firmware */
  77. total_blocks = total_len + TX_HW_BLOCK_SIZE - 1;
  78. total_blocks = total_blocks / TX_HW_BLOCK_SIZE + TX_HW_BLOCK_SPARE;
  79. if (total_blocks <= wl->tx_blocks_available) {
  80. desc = (struct wl1271_tx_hw_descr *)skb_push(
  81. skb, total_len - skb->len);
  82. desc->extra_mem_blocks = TX_HW_BLOCK_SPARE;
  83. desc->total_mem_blocks = total_blocks;
  84. desc->id = id;
  85. wl->tx_blocks_available -= total_blocks;
  86. ret = 0;
  87. wl1271_debug(DEBUG_TX,
  88. "tx_allocate: size: %d, blocks: %d, id: %d",
  89. total_len, total_blocks, id);
  90. } else {
  91. wl1271_free_tx_id(wl, id);
  92. }
  93. return ret;
  94. }
  95. static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
  96. u32 extra, struct ieee80211_tx_info *control)
  97. {
  98. struct timespec ts;
  99. struct wl1271_tx_hw_descr *desc;
  100. int pad, ac, rate_idx;
  101. s64 hosttime;
  102. u16 tx_attr;
  103. desc = (struct wl1271_tx_hw_descr *) skb->data;
  104. /* relocate space for security header */
  105. if (extra) {
  106. void *framestart = skb->data + sizeof(*desc);
  107. u16 fc = *(u16 *)(framestart + extra);
  108. int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
  109. memmove(framestart, framestart + extra, hdrlen);
  110. }
  111. /* configure packet life time */
  112. getnstimeofday(&ts);
  113. hosttime = (timespec_to_ns(&ts) >> 10);
  114. desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
  115. if (wl->bss_type != BSS_TYPE_AP_BSS)
  116. desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
  117. else
  118. desc->life_time = cpu_to_le16(TX_HW_AP_MODE_PKT_LIFETIME_TU);
  119. /* configure the tx attributes */
  120. tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
  121. /* queue (we use same identifiers for tid's and ac's */
  122. ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  123. desc->tid = ac;
  124. if (wl->bss_type != BSS_TYPE_AP_BSS) {
  125. desc->aid = TX_HW_DEFAULT_AID;
  126. /* if the packets are destined for AP (have a STA entry)
  127. send them with AP rate policies, otherwise use default
  128. basic rates */
  129. if (control->control.sta)
  130. rate_idx = ACX_TX_AP_FULL_RATE;
  131. else
  132. rate_idx = ACX_TX_BASIC_RATE;
  133. } else {
  134. if (control->control.sta) {
  135. struct wl1271_station *wl_sta;
  136. wl_sta = (struct wl1271_station *)
  137. control->control.sta->drv_priv;
  138. desc->hlid = wl_sta->hlid;
  139. rate_idx = ac;
  140. } else {
  141. struct ieee80211_hdr *hdr;
  142. hdr = (struct ieee80211_hdr *)
  143. (skb->data + sizeof(*desc));
  144. if (ieee80211_is_mgmt(hdr->frame_control)) {
  145. desc->hlid = WL1271_AP_GLOBAL_HLID;
  146. rate_idx = ACX_TX_AP_MODE_MGMT_RATE;
  147. } else {
  148. desc->hlid = WL1271_AP_BROADCAST_HLID;
  149. rate_idx = ACX_TX_AP_MODE_BCST_RATE;
  150. }
  151. }
  152. }
  153. tx_attr |= rate_idx << TX_HW_ATTR_OFST_RATE_POLICY;
  154. desc->reserved = 0;
  155. /* align the length (and store in terms of words) */
  156. pad = WL1271_TX_ALIGN(skb->len);
  157. desc->length = cpu_to_le16(pad >> 2);
  158. /* calculate number of padding bytes */
  159. pad = pad - skb->len;
  160. tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
  161. desc->tx_attr = cpu_to_le16(tx_attr);
  162. wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d hlid: %d "
  163. "tx_attr: 0x%x len: %d life: %d mem: %d", pad, desc->hlid,
  164. le16_to_cpu(desc->tx_attr), le16_to_cpu(desc->length),
  165. le16_to_cpu(desc->life_time), desc->total_mem_blocks);
  166. }
  167. /* caller must hold wl->mutex */
  168. static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct sk_buff *skb,
  169. u32 buf_offset)
  170. {
  171. struct ieee80211_tx_info *info;
  172. u32 extra = 0;
  173. int ret = 0;
  174. u32 total_len;
  175. if (!skb)
  176. return -EINVAL;
  177. info = IEEE80211_SKB_CB(skb);
  178. if (info->control.hw_key &&
  179. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
  180. extra = WL1271_TKIP_IV_SPACE;
  181. if (info->control.hw_key) {
  182. bool is_wep;
  183. u8 idx = info->control.hw_key->hw_key_idx;
  184. u32 cipher = info->control.hw_key->cipher;
  185. is_wep = (cipher == WLAN_CIPHER_SUITE_WEP40) ||
  186. (cipher == WLAN_CIPHER_SUITE_WEP104);
  187. if (unlikely(is_wep && wl->default_key != idx)) {
  188. ret = wl1271_set_default_wep_key(wl, idx);
  189. if (ret < 0)
  190. return ret;
  191. wl->default_key = idx;
  192. }
  193. }
  194. ret = wl1271_tx_allocate(wl, skb, extra, buf_offset);
  195. if (ret < 0)
  196. return ret;
  197. wl1271_tx_fill_hdr(wl, skb, extra, info);
  198. /*
  199. * The length of each packet is stored in terms of words. Thus, we must
  200. * pad the skb data to make sure its length is aligned.
  201. * The number of padding bytes is computed and set in wl1271_tx_fill_hdr
  202. */
  203. total_len = WL1271_TX_ALIGN(skb->len);
  204. memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
  205. memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
  206. return total_len;
  207. }
  208. u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
  209. {
  210. struct ieee80211_supported_band *band;
  211. u32 enabled_rates = 0;
  212. int bit;
  213. band = wl->hw->wiphy->bands[wl->band];
  214. for (bit = 0; bit < band->n_bitrates; bit++) {
  215. if (rate_set & 0x1)
  216. enabled_rates |= band->bitrates[bit].hw_value;
  217. rate_set >>= 1;
  218. }
  219. #ifdef CONFIG_WL12XX_HT
  220. /* MCS rates indication are on bits 16 - 23 */
  221. rate_set >>= HW_HT_RATES_OFFSET - band->n_bitrates;
  222. for (bit = 0; bit < 8; bit++) {
  223. if (rate_set & 0x1)
  224. enabled_rates |= (CONF_HW_BIT_RATE_MCS_0 << bit);
  225. rate_set >>= 1;
  226. }
  227. #endif
  228. return enabled_rates;
  229. }
  230. static void handle_tx_low_watermark(struct wl1271 *wl)
  231. {
  232. unsigned long flags;
  233. if (test_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags) &&
  234. wl->tx_queue_count <= WL1271_TX_QUEUE_LOW_WATERMARK) {
  235. /* firmware buffer has space, restart queues */
  236. spin_lock_irqsave(&wl->wl_lock, flags);
  237. ieee80211_wake_queues(wl->hw);
  238. clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
  239. spin_unlock_irqrestore(&wl->wl_lock, flags);
  240. }
  241. }
  242. static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
  243. {
  244. struct sk_buff *skb = NULL;
  245. unsigned long flags;
  246. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VO]);
  247. if (skb)
  248. goto out;
  249. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VI]);
  250. if (skb)
  251. goto out;
  252. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BE]);
  253. if (skb)
  254. goto out;
  255. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BK]);
  256. out:
  257. if (skb) {
  258. spin_lock_irqsave(&wl->wl_lock, flags);
  259. wl->tx_queue_count--;
  260. spin_unlock_irqrestore(&wl->wl_lock, flags);
  261. }
  262. return skb;
  263. }
  264. static void wl1271_skb_queue_head(struct wl1271 *wl, struct sk_buff *skb)
  265. {
  266. unsigned long flags;
  267. int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  268. skb_queue_head(&wl->tx_queue[q], skb);
  269. spin_lock_irqsave(&wl->wl_lock, flags);
  270. wl->tx_queue_count++;
  271. spin_unlock_irqrestore(&wl->wl_lock, flags);
  272. }
  273. void wl1271_tx_work_locked(struct wl1271 *wl)
  274. {
  275. struct sk_buff *skb;
  276. bool woken_up = false;
  277. u32 buf_offset = 0;
  278. bool sent_packets = false;
  279. int ret;
  280. if (unlikely(wl->state == WL1271_STATE_OFF))
  281. goto out;
  282. while ((skb = wl1271_skb_dequeue(wl))) {
  283. if (!woken_up) {
  284. ret = wl1271_ps_elp_wakeup(wl, false);
  285. if (ret < 0)
  286. goto out_ack;
  287. woken_up = true;
  288. }
  289. ret = wl1271_prepare_tx_frame(wl, skb, buf_offset);
  290. if (ret == -EAGAIN) {
  291. /*
  292. * Aggregation buffer is full.
  293. * Flush buffer and try again.
  294. */
  295. wl1271_skb_queue_head(wl, skb);
  296. wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
  297. buf_offset, true);
  298. sent_packets = true;
  299. buf_offset = 0;
  300. continue;
  301. } else if (ret == -EBUSY) {
  302. /*
  303. * Firmware buffer is full.
  304. * Queue back last skb, and stop aggregating.
  305. */
  306. wl1271_skb_queue_head(wl, skb);
  307. /* No work left, avoid scheduling redundant tx work */
  308. set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
  309. goto out_ack;
  310. } else if (ret < 0) {
  311. dev_kfree_skb(skb);
  312. goto out_ack;
  313. }
  314. buf_offset += ret;
  315. wl->tx_packets_count++;
  316. }
  317. out_ack:
  318. if (buf_offset) {
  319. wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
  320. buf_offset, true);
  321. sent_packets = true;
  322. }
  323. if (sent_packets) {
  324. /* interrupt the firmware with the new packets */
  325. wl1271_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
  326. handle_tx_low_watermark(wl);
  327. }
  328. out:
  329. if (woken_up)
  330. wl1271_ps_elp_sleep(wl);
  331. }
  332. void wl1271_tx_work(struct work_struct *work)
  333. {
  334. struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
  335. mutex_lock(&wl->mutex);
  336. wl1271_tx_work_locked(wl);
  337. mutex_unlock(&wl->mutex);
  338. }
  339. static void wl1271_tx_complete_packet(struct wl1271 *wl,
  340. struct wl1271_tx_hw_res_descr *result)
  341. {
  342. struct ieee80211_tx_info *info;
  343. struct sk_buff *skb;
  344. int id = result->id;
  345. int rate = -1;
  346. u8 retries = 0;
  347. /* check for id legality */
  348. if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
  349. wl1271_warning("TX result illegal id: %d", id);
  350. return;
  351. }
  352. skb = wl->tx_frames[id];
  353. info = IEEE80211_SKB_CB(skb);
  354. /* update the TX status info */
  355. if (result->status == TX_SUCCESS) {
  356. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  357. info->flags |= IEEE80211_TX_STAT_ACK;
  358. rate = wl1271_rate_to_idx(result->rate_class_index, wl->band);
  359. retries = result->ack_failures;
  360. } else if (result->status == TX_RETRY_EXCEEDED) {
  361. wl->stats.excessive_retries++;
  362. retries = result->ack_failures;
  363. }
  364. info->status.rates[0].idx = rate;
  365. info->status.rates[0].count = retries;
  366. info->status.rates[0].flags = 0;
  367. info->status.ack_signal = -1;
  368. wl->stats.retry_count += result->ack_failures;
  369. /* update security sequence number */
  370. wl->tx_security_seq += (result->lsb_security_sequence_number -
  371. wl->tx_security_last_seq);
  372. wl->tx_security_last_seq = result->lsb_security_sequence_number;
  373. /* remove private header from packet */
  374. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  375. /* remove TKIP header space if present */
  376. if (info->control.hw_key &&
  377. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  378. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  379. memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
  380. skb_pull(skb, WL1271_TKIP_IV_SPACE);
  381. }
  382. wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  383. " status 0x%x",
  384. result->id, skb, result->ack_failures,
  385. result->rate_class_index, result->status);
  386. /* return the packet to the stack */
  387. ieee80211_tx_status(wl->hw, skb);
  388. wl1271_free_tx_id(wl, result->id);
  389. }
  390. /* Called upon reception of a TX complete interrupt */
  391. void wl1271_tx_complete(struct wl1271 *wl)
  392. {
  393. struct wl1271_acx_mem_map *memmap =
  394. (struct wl1271_acx_mem_map *)wl->target_mem_map;
  395. u32 count, fw_counter;
  396. u32 i;
  397. /* read the tx results from the chipset */
  398. wl1271_read(wl, le32_to_cpu(memmap->tx_result),
  399. wl->tx_res_if, sizeof(*wl->tx_res_if), false);
  400. fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
  401. /* write host counter to chipset (to ack) */
  402. wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
  403. offsetof(struct wl1271_tx_hw_res_if,
  404. tx_result_host_counter), fw_counter);
  405. count = fw_counter - wl->tx_results_count;
  406. wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
  407. /* verify that the result buffer is not getting overrun */
  408. if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
  409. wl1271_warning("TX result overflow from chipset: %d", count);
  410. /* process the results */
  411. for (i = 0; i < count; i++) {
  412. struct wl1271_tx_hw_res_descr *result;
  413. u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
  414. /* process the packet */
  415. result = &(wl->tx_res_if->tx_results_queue[offset]);
  416. wl1271_tx_complete_packet(wl, result);
  417. wl->tx_results_count++;
  418. }
  419. }
  420. /* caller must hold wl->mutex */
  421. void wl1271_tx_reset(struct wl1271 *wl)
  422. {
  423. int i;
  424. struct sk_buff *skb;
  425. /* TX failure */
  426. for (i = 0; i < NUM_TX_QUEUES; i++) {
  427. while ((skb = skb_dequeue(&wl->tx_queue[i]))) {
  428. wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
  429. ieee80211_tx_status(wl->hw, skb);
  430. }
  431. }
  432. wl->tx_queue_count = 0;
  433. /*
  434. * Make sure the driver is at a consistent state, in case this
  435. * function is called from a context other than interface removal.
  436. */
  437. handle_tx_low_watermark(wl);
  438. for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
  439. if (wl->tx_frames[i] != NULL) {
  440. skb = wl->tx_frames[i];
  441. wl1271_free_tx_id(wl, i);
  442. wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
  443. ieee80211_tx_status(wl->hw, skb);
  444. }
  445. }
  446. #define WL1271_TX_FLUSH_TIMEOUT 500000
  447. /* caller must *NOT* hold wl->mutex */
  448. void wl1271_tx_flush(struct wl1271 *wl)
  449. {
  450. unsigned long timeout;
  451. timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
  452. while (!time_after(jiffies, timeout)) {
  453. mutex_lock(&wl->mutex);
  454. wl1271_debug(DEBUG_TX, "flushing tx buffer: %d",
  455. wl->tx_frames_cnt);
  456. if ((wl->tx_frames_cnt == 0) && (wl->tx_queue_count == 0)) {
  457. mutex_unlock(&wl->mutex);
  458. return;
  459. }
  460. mutex_unlock(&wl->mutex);
  461. msleep(1);
  462. }
  463. wl1271_warning("Unable to flush all TX buffers, timed out.");
  464. }
  465. u32 wl1271_tx_min_rate_get(struct wl1271 *wl)
  466. {
  467. int i;
  468. u32 rate = 0;
  469. if (!wl->basic_rate_set) {
  470. WARN_ON(1);
  471. wl->basic_rate_set = wl->conf.tx.basic_rate;
  472. }
  473. for (i = 0; !rate; i++) {
  474. if ((wl->basic_rate_set >> i) & 0x1)
  475. rate = 1 << i;
  476. }
  477. return rate;
  478. }