tx.c 15 KB

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