tx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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_update_filters(struct wl1271 *wl,
  63. struct sk_buff *skb)
  64. {
  65. struct ieee80211_hdr *hdr;
  66. hdr = (struct ieee80211_hdr *)(skb->data +
  67. sizeof(struct wl1271_tx_hw_descr));
  68. /*
  69. * stop bssid-based filtering before transmitting authentication
  70. * requests. this way the hw will never drop authentication
  71. * responses coming from BSSIDs it isn't familiar with (e.g. on
  72. * roaming)
  73. */
  74. if (!ieee80211_is_auth(hdr->frame_control))
  75. return 0;
  76. wl1271_configure_filters(wl, FIF_OTHER_BSS);
  77. return wl1271_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
  78. }
  79. static void wl1271_tx_ap_update_inconnection_sta(struct wl1271 *wl,
  80. struct sk_buff *skb)
  81. {
  82. struct ieee80211_hdr *hdr;
  83. /*
  84. * add the station to the known list before transmitting the
  85. * authentication response. this way it won't get de-authed by FW
  86. * when transmitting too soon.
  87. */
  88. hdr = (struct ieee80211_hdr *)(skb->data +
  89. sizeof(struct wl1271_tx_hw_descr));
  90. if (ieee80211_is_auth(hdr->frame_control))
  91. wl1271_acx_set_inconnection_sta(wl, hdr->addr1);
  92. }
  93. static void wl1271_tx_regulate_link(struct wl1271 *wl, u8 hlid)
  94. {
  95. bool fw_ps;
  96. u8 tx_blks;
  97. /* only regulate station links */
  98. if (hlid < WL1271_AP_STA_HLID_START)
  99. return;
  100. fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
  101. tx_blks = wl->links[hlid].allocated_blks;
  102. /*
  103. * if in FW PS and there is enough data in FW we can put the link
  104. * into high-level PS and clean out its TX queues.
  105. */
  106. if (fw_ps && tx_blks >= WL1271_PS_STA_MAX_BLOCKS)
  107. wl1271_ps_link_start(wl, hlid, true);
  108. }
  109. u8 wl1271_tx_get_hlid(struct sk_buff *skb)
  110. {
  111. struct ieee80211_tx_info *control = IEEE80211_SKB_CB(skb);
  112. if (control->control.sta) {
  113. struct wl1271_station *wl_sta;
  114. wl_sta = (struct wl1271_station *)
  115. control->control.sta->drv_priv;
  116. return wl_sta->hlid;
  117. } else {
  118. struct ieee80211_hdr *hdr;
  119. hdr = (struct ieee80211_hdr *)skb->data;
  120. if (ieee80211_is_mgmt(hdr->frame_control))
  121. return WL1271_AP_GLOBAL_HLID;
  122. else
  123. return WL1271_AP_BROADCAST_HLID;
  124. }
  125. }
  126. static unsigned int wl12xx_calc_packet_alignment(struct wl1271 *wl,
  127. unsigned int packet_length)
  128. {
  129. if (wl->quirks & WL12XX_QUIRK_BLOCKSIZE_ALIGNMENT)
  130. return ALIGN(packet_length, WL12XX_BUS_BLOCK_SIZE);
  131. else
  132. return ALIGN(packet_length, WL1271_TX_ALIGN_TO);
  133. }
  134. static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra,
  135. u32 buf_offset, u8 hlid)
  136. {
  137. struct wl1271_tx_hw_descr *desc;
  138. u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
  139. u32 len;
  140. u32 total_blocks;
  141. int id, ret = -EBUSY;
  142. u32 spare_blocks;
  143. if (unlikely(wl->quirks & WL12XX_QUIRK_USE_2_SPARE_BLOCKS))
  144. spare_blocks = 2;
  145. else
  146. spare_blocks = 1;
  147. if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
  148. return -EAGAIN;
  149. /* allocate free identifier for the packet */
  150. id = wl1271_alloc_tx_id(wl, skb);
  151. if (id < 0)
  152. return id;
  153. /* approximate the number of blocks required for this packet
  154. in the firmware */
  155. len = wl12xx_calc_packet_alignment(wl, total_len);
  156. total_blocks = (len + TX_HW_BLOCK_SIZE - 1) / TX_HW_BLOCK_SIZE +
  157. spare_blocks;
  158. if (total_blocks <= wl->tx_blocks_available) {
  159. desc = (struct wl1271_tx_hw_descr *)skb_push(
  160. skb, total_len - skb->len);
  161. /* HW descriptor fields change between wl127x and wl128x */
  162. if (wl->chip.id == CHIP_ID_1283_PG20) {
  163. desc->wl128x_mem.total_mem_blocks = total_blocks;
  164. } else {
  165. desc->wl127x_mem.extra_blocks = spare_blocks;
  166. desc->wl127x_mem.total_mem_blocks = total_blocks;
  167. }
  168. desc->id = id;
  169. wl->tx_blocks_available -= total_blocks;
  170. wl->tx_allocated_blocks += total_blocks;
  171. if (wl->bss_type == BSS_TYPE_AP_BSS)
  172. wl->links[hlid].allocated_blks += total_blocks;
  173. ret = 0;
  174. wl1271_debug(DEBUG_TX,
  175. "tx_allocate: size: %d, blocks: %d, id: %d",
  176. total_len, total_blocks, id);
  177. } else {
  178. wl1271_free_tx_id(wl, id);
  179. }
  180. return ret;
  181. }
  182. static bool wl12xx_is_dummy_packet(struct wl1271 *wl, struct sk_buff *skb)
  183. {
  184. return wl->dummy_packet == skb;
  185. }
  186. static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
  187. u32 extra, struct ieee80211_tx_info *control,
  188. u8 hlid)
  189. {
  190. struct timespec ts;
  191. struct wl1271_tx_hw_descr *desc;
  192. int aligned_len, ac, rate_idx;
  193. s64 hosttime;
  194. u16 tx_attr;
  195. desc = (struct wl1271_tx_hw_descr *) skb->data;
  196. /* relocate space for security header */
  197. if (extra) {
  198. void *framestart = skb->data + sizeof(*desc);
  199. u16 fc = *(u16 *)(framestart + extra);
  200. int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
  201. memmove(framestart, framestart + extra, hdrlen);
  202. }
  203. /* configure packet life time */
  204. getnstimeofday(&ts);
  205. hosttime = (timespec_to_ns(&ts) >> 10);
  206. desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
  207. if (wl->bss_type != BSS_TYPE_AP_BSS)
  208. desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
  209. else
  210. desc->life_time = cpu_to_le16(TX_HW_AP_MODE_PKT_LIFETIME_TU);
  211. /* queue */
  212. ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  213. desc->tid = skb->priority;
  214. if (wl12xx_is_dummy_packet(wl, skb)) {
  215. /*
  216. * FW expects the dummy packet to have an invalid session id -
  217. * any session id that is different than the one set in the join
  218. */
  219. tx_attr = ((~wl->session_counter) <<
  220. TX_HW_ATTR_OFST_SESSION_COUNTER) &
  221. TX_HW_ATTR_SESSION_COUNTER;
  222. tx_attr |= TX_HW_ATTR_TX_DUMMY_REQ;
  223. } else {
  224. /* configure the tx attributes */
  225. tx_attr =
  226. wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
  227. }
  228. if (wl->bss_type != BSS_TYPE_AP_BSS) {
  229. desc->aid = hlid;
  230. /* if the packets are destined for AP (have a STA entry)
  231. send them with AP rate policies, otherwise use default
  232. basic rates */
  233. if (control->control.sta)
  234. rate_idx = ACX_TX_AP_FULL_RATE;
  235. else
  236. rate_idx = ACX_TX_BASIC_RATE;
  237. } else {
  238. desc->hlid = hlid;
  239. switch (hlid) {
  240. case WL1271_AP_GLOBAL_HLID:
  241. rate_idx = ACX_TX_AP_MODE_MGMT_RATE;
  242. break;
  243. case WL1271_AP_BROADCAST_HLID:
  244. rate_idx = ACX_TX_AP_MODE_BCST_RATE;
  245. break;
  246. default:
  247. rate_idx = ac;
  248. break;
  249. }
  250. }
  251. tx_attr |= rate_idx << TX_HW_ATTR_OFST_RATE_POLICY;
  252. desc->reserved = 0;
  253. aligned_len = wl12xx_calc_packet_alignment(wl, skb->len);
  254. if (wl->chip.id == CHIP_ID_1283_PG20) {
  255. desc->wl128x_mem.extra_bytes = aligned_len - skb->len;
  256. desc->length = cpu_to_le16(aligned_len >> 2);
  257. wl1271_debug(DEBUG_TX, "tx_fill_hdr: hlid: %d "
  258. "tx_attr: 0x%x len: %d life: %d mem: %d",
  259. desc->hlid, tx_attr,
  260. le16_to_cpu(desc->length),
  261. le16_to_cpu(desc->life_time),
  262. desc->wl128x_mem.total_mem_blocks);
  263. } else {
  264. int pad;
  265. /* Store the aligned length in terms of words */
  266. desc->length = cpu_to_le16(aligned_len >> 2);
  267. /* calculate number of padding bytes */
  268. pad = aligned_len - skb->len;
  269. tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
  270. wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d hlid: %d "
  271. "tx_attr: 0x%x len: %d life: %d mem: %d", pad,
  272. desc->hlid, tx_attr,
  273. le16_to_cpu(desc->length),
  274. le16_to_cpu(desc->life_time),
  275. desc->wl127x_mem.total_mem_blocks);
  276. }
  277. desc->tx_attr = cpu_to_le16(tx_attr);
  278. }
  279. /* caller must hold wl->mutex */
  280. static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct sk_buff *skb,
  281. u32 buf_offset)
  282. {
  283. struct ieee80211_tx_info *info;
  284. u32 extra = 0;
  285. int ret = 0;
  286. u32 total_len;
  287. u8 hlid;
  288. if (!skb)
  289. return -EINVAL;
  290. info = IEEE80211_SKB_CB(skb);
  291. if (info->control.hw_key &&
  292. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
  293. extra = WL1271_TKIP_IV_SPACE;
  294. if (info->control.hw_key) {
  295. bool is_wep;
  296. u8 idx = info->control.hw_key->hw_key_idx;
  297. u32 cipher = info->control.hw_key->cipher;
  298. is_wep = (cipher == WLAN_CIPHER_SUITE_WEP40) ||
  299. (cipher == WLAN_CIPHER_SUITE_WEP104);
  300. if (unlikely(is_wep && wl->default_key != idx)) {
  301. ret = wl1271_set_default_wep_key(wl, idx);
  302. if (ret < 0)
  303. return ret;
  304. wl->default_key = idx;
  305. }
  306. }
  307. if (wl->bss_type == BSS_TYPE_AP_BSS)
  308. hlid = wl1271_tx_get_hlid(skb);
  309. else
  310. hlid = TX_HW_DEFAULT_AID;
  311. ret = wl1271_tx_allocate(wl, skb, extra, buf_offset, hlid);
  312. if (ret < 0)
  313. return ret;
  314. if (wl->bss_type == BSS_TYPE_AP_BSS) {
  315. wl1271_tx_ap_update_inconnection_sta(wl, skb);
  316. wl1271_tx_regulate_link(wl, hlid);
  317. } else {
  318. wl1271_tx_update_filters(wl, skb);
  319. }
  320. wl1271_tx_fill_hdr(wl, skb, extra, info, hlid);
  321. /*
  322. * The length of each packet is stored in terms of
  323. * words. Thus, we must pad the skb data to make sure its
  324. * length is aligned. The number of padding bytes is computed
  325. * and set in wl1271_tx_fill_hdr.
  326. * In special cases, we want to align to a specific block size
  327. * (eg. for wl128x with SDIO we align to 256).
  328. */
  329. total_len = wl12xx_calc_packet_alignment(wl, skb->len);
  330. memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
  331. memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
  332. /* Revert side effects in the dummy packet skb, so it can be reused */
  333. if (wl12xx_is_dummy_packet(wl, skb))
  334. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  335. return total_len;
  336. }
  337. u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
  338. {
  339. struct ieee80211_supported_band *band;
  340. u32 enabled_rates = 0;
  341. int bit;
  342. band = wl->hw->wiphy->bands[wl->band];
  343. for (bit = 0; bit < band->n_bitrates; bit++) {
  344. if (rate_set & 0x1)
  345. enabled_rates |= band->bitrates[bit].hw_value;
  346. rate_set >>= 1;
  347. }
  348. #ifdef CONFIG_WL12XX_HT
  349. /* MCS rates indication are on bits 16 - 23 */
  350. rate_set >>= HW_HT_RATES_OFFSET - band->n_bitrates;
  351. for (bit = 0; bit < 8; bit++) {
  352. if (rate_set & 0x1)
  353. enabled_rates |= (CONF_HW_BIT_RATE_MCS_0 << bit);
  354. rate_set >>= 1;
  355. }
  356. #endif
  357. return enabled_rates;
  358. }
  359. void wl1271_handle_tx_low_watermark(struct wl1271 *wl)
  360. {
  361. unsigned long flags;
  362. if (test_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags) &&
  363. wl->tx_queue_count <= WL1271_TX_QUEUE_LOW_WATERMARK) {
  364. /* firmware buffer has space, restart queues */
  365. spin_lock_irqsave(&wl->wl_lock, flags);
  366. ieee80211_wake_queues(wl->hw);
  367. clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
  368. spin_unlock_irqrestore(&wl->wl_lock, flags);
  369. }
  370. }
  371. static struct sk_buff *wl1271_sta_skb_dequeue(struct wl1271 *wl)
  372. {
  373. struct sk_buff *skb = NULL;
  374. unsigned long flags;
  375. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VO]);
  376. if (skb)
  377. goto out;
  378. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VI]);
  379. if (skb)
  380. goto out;
  381. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BE]);
  382. if (skb)
  383. goto out;
  384. skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BK]);
  385. out:
  386. if (skb) {
  387. spin_lock_irqsave(&wl->wl_lock, flags);
  388. wl->tx_queue_count--;
  389. spin_unlock_irqrestore(&wl->wl_lock, flags);
  390. }
  391. return skb;
  392. }
  393. static struct sk_buff *wl1271_ap_skb_dequeue(struct wl1271 *wl)
  394. {
  395. struct sk_buff *skb = NULL;
  396. unsigned long flags;
  397. int i, h, start_hlid;
  398. /* start from the link after the last one */
  399. start_hlid = (wl->last_tx_hlid + 1) % AP_MAX_LINKS;
  400. /* dequeue according to AC, round robin on each link */
  401. for (i = 0; i < AP_MAX_LINKS; i++) {
  402. h = (start_hlid + i) % AP_MAX_LINKS;
  403. skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_VO]);
  404. if (skb)
  405. goto out;
  406. skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_VI]);
  407. if (skb)
  408. goto out;
  409. skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_BE]);
  410. if (skb)
  411. goto out;
  412. skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_BK]);
  413. if (skb)
  414. goto out;
  415. }
  416. out:
  417. if (skb) {
  418. wl->last_tx_hlid = h;
  419. spin_lock_irqsave(&wl->wl_lock, flags);
  420. wl->tx_queue_count--;
  421. spin_unlock_irqrestore(&wl->wl_lock, flags);
  422. } else {
  423. wl->last_tx_hlid = 0;
  424. }
  425. return skb;
  426. }
  427. static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
  428. {
  429. unsigned long flags;
  430. struct sk_buff *skb = NULL;
  431. if (wl->bss_type == BSS_TYPE_AP_BSS)
  432. skb = wl1271_ap_skb_dequeue(wl);
  433. else
  434. skb = wl1271_sta_skb_dequeue(wl);
  435. if (!skb &&
  436. test_and_clear_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags)) {
  437. skb = wl->dummy_packet;
  438. spin_lock_irqsave(&wl->wl_lock, flags);
  439. wl->tx_queue_count--;
  440. spin_unlock_irqrestore(&wl->wl_lock, flags);
  441. }
  442. return skb;
  443. }
  444. static void wl1271_skb_queue_head(struct wl1271 *wl, struct sk_buff *skb)
  445. {
  446. unsigned long flags;
  447. int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  448. if (wl12xx_is_dummy_packet(wl, skb)) {
  449. set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
  450. } else if (wl->bss_type == BSS_TYPE_AP_BSS) {
  451. u8 hlid = wl1271_tx_get_hlid(skb);
  452. skb_queue_head(&wl->links[hlid].tx_queue[q], skb);
  453. /* make sure we dequeue the same packet next time */
  454. wl->last_tx_hlid = (hlid + AP_MAX_LINKS - 1) % AP_MAX_LINKS;
  455. } else {
  456. skb_queue_head(&wl->tx_queue[q], skb);
  457. }
  458. spin_lock_irqsave(&wl->wl_lock, flags);
  459. wl->tx_queue_count++;
  460. spin_unlock_irqrestore(&wl->wl_lock, flags);
  461. }
  462. void wl1271_tx_work_locked(struct wl1271 *wl)
  463. {
  464. struct sk_buff *skb;
  465. u32 buf_offset = 0;
  466. bool sent_packets = false;
  467. int ret;
  468. if (unlikely(wl->state == WL1271_STATE_OFF))
  469. return;
  470. while ((skb = wl1271_skb_dequeue(wl))) {
  471. ret = wl1271_prepare_tx_frame(wl, skb, buf_offset);
  472. if (ret == -EAGAIN) {
  473. /*
  474. * Aggregation buffer is full.
  475. * Flush buffer and try again.
  476. */
  477. wl1271_skb_queue_head(wl, skb);
  478. wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
  479. buf_offset, true);
  480. sent_packets = true;
  481. buf_offset = 0;
  482. continue;
  483. } else if (ret == -EBUSY) {
  484. /*
  485. * Firmware buffer is full.
  486. * Queue back last skb, and stop aggregating.
  487. */
  488. wl1271_skb_queue_head(wl, skb);
  489. /* No work left, avoid scheduling redundant tx work */
  490. set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
  491. goto out_ack;
  492. } else if (ret < 0) {
  493. dev_kfree_skb(skb);
  494. goto out_ack;
  495. }
  496. buf_offset += ret;
  497. wl->tx_packets_count++;
  498. }
  499. out_ack:
  500. if (buf_offset) {
  501. wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
  502. buf_offset, true);
  503. sent_packets = true;
  504. }
  505. if (sent_packets) {
  506. /*
  507. * Interrupt the firmware with the new packets. This is only
  508. * required for older hardware revisions
  509. */
  510. if (wl->quirks & WL12XX_QUIRK_END_OF_TRANSACTION)
  511. wl1271_write32(wl, WL1271_HOST_WR_ACCESS,
  512. wl->tx_packets_count);
  513. wl1271_handle_tx_low_watermark(wl);
  514. }
  515. }
  516. void wl1271_tx_work(struct work_struct *work)
  517. {
  518. struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
  519. int ret;
  520. mutex_lock(&wl->mutex);
  521. ret = wl1271_ps_elp_wakeup(wl);
  522. if (ret < 0)
  523. goto out;
  524. wl1271_tx_work_locked(wl);
  525. wl1271_ps_elp_wakeup(wl);
  526. out:
  527. mutex_unlock(&wl->mutex);
  528. }
  529. static void wl1271_tx_complete_packet(struct wl1271 *wl,
  530. struct wl1271_tx_hw_res_descr *result)
  531. {
  532. struct ieee80211_tx_info *info;
  533. struct sk_buff *skb;
  534. int id = result->id;
  535. int rate = -1;
  536. u8 retries = 0;
  537. /* check for id legality */
  538. if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
  539. wl1271_warning("TX result illegal id: %d", id);
  540. return;
  541. }
  542. skb = wl->tx_frames[id];
  543. info = IEEE80211_SKB_CB(skb);
  544. if (wl12xx_is_dummy_packet(wl, skb)) {
  545. wl1271_free_tx_id(wl, id);
  546. return;
  547. }
  548. /* update the TX status info */
  549. if (result->status == TX_SUCCESS) {
  550. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  551. info->flags |= IEEE80211_TX_STAT_ACK;
  552. rate = wl1271_rate_to_idx(result->rate_class_index, wl->band);
  553. retries = result->ack_failures;
  554. } else if (result->status == TX_RETRY_EXCEEDED) {
  555. wl->stats.excessive_retries++;
  556. retries = result->ack_failures;
  557. }
  558. info->status.rates[0].idx = rate;
  559. info->status.rates[0].count = retries;
  560. info->status.rates[0].flags = 0;
  561. info->status.ack_signal = -1;
  562. wl->stats.retry_count += result->ack_failures;
  563. /* update security sequence number */
  564. wl->tx_security_seq += (result->lsb_security_sequence_number -
  565. wl->tx_security_last_seq);
  566. wl->tx_security_last_seq = result->lsb_security_sequence_number;
  567. /* remove private header from packet */
  568. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  569. /* remove TKIP header space if present */
  570. if (info->control.hw_key &&
  571. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  572. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  573. memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
  574. skb_pull(skb, WL1271_TKIP_IV_SPACE);
  575. }
  576. wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  577. " status 0x%x",
  578. result->id, skb, result->ack_failures,
  579. result->rate_class_index, result->status);
  580. /* return the packet to the stack */
  581. skb_queue_tail(&wl->deferred_tx_queue, skb);
  582. ieee80211_queue_work(wl->hw, &wl->netstack_work);
  583. wl1271_free_tx_id(wl, result->id);
  584. }
  585. /* Called upon reception of a TX complete interrupt */
  586. void wl1271_tx_complete(struct wl1271 *wl)
  587. {
  588. struct wl1271_acx_mem_map *memmap =
  589. (struct wl1271_acx_mem_map *)wl->target_mem_map;
  590. u32 count, fw_counter;
  591. u32 i;
  592. /* read the tx results from the chipset */
  593. wl1271_read(wl, le32_to_cpu(memmap->tx_result),
  594. wl->tx_res_if, sizeof(*wl->tx_res_if), false);
  595. fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
  596. /* write host counter to chipset (to ack) */
  597. wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
  598. offsetof(struct wl1271_tx_hw_res_if,
  599. tx_result_host_counter), fw_counter);
  600. count = fw_counter - wl->tx_results_count;
  601. wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
  602. /* verify that the result buffer is not getting overrun */
  603. if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
  604. wl1271_warning("TX result overflow from chipset: %d", count);
  605. /* process the results */
  606. for (i = 0; i < count; i++) {
  607. struct wl1271_tx_hw_res_descr *result;
  608. u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
  609. /* process the packet */
  610. result = &(wl->tx_res_if->tx_results_queue[offset]);
  611. wl1271_tx_complete_packet(wl, result);
  612. wl->tx_results_count++;
  613. }
  614. }
  615. void wl1271_tx_reset_link_queues(struct wl1271 *wl, u8 hlid)
  616. {
  617. struct sk_buff *skb;
  618. int i, total = 0;
  619. unsigned long flags;
  620. struct ieee80211_tx_info *info;
  621. for (i = 0; i < NUM_TX_QUEUES; i++) {
  622. while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
  623. wl1271_debug(DEBUG_TX, "link freeing skb 0x%p", skb);
  624. info = IEEE80211_SKB_CB(skb);
  625. info->status.rates[0].idx = -1;
  626. info->status.rates[0].count = 0;
  627. ieee80211_tx_status(wl->hw, skb);
  628. total++;
  629. }
  630. }
  631. spin_lock_irqsave(&wl->wl_lock, flags);
  632. wl->tx_queue_count -= total;
  633. spin_unlock_irqrestore(&wl->wl_lock, flags);
  634. wl1271_handle_tx_low_watermark(wl);
  635. }
  636. /* caller must hold wl->mutex */
  637. void wl1271_tx_reset(struct wl1271 *wl)
  638. {
  639. int i;
  640. struct sk_buff *skb;
  641. struct ieee80211_tx_info *info;
  642. /* TX failure */
  643. if (wl->bss_type == BSS_TYPE_AP_BSS) {
  644. for (i = 0; i < AP_MAX_LINKS; i++) {
  645. wl1271_tx_reset_link_queues(wl, i);
  646. wl->links[i].allocated_blks = 0;
  647. wl->links[i].prev_freed_blks = 0;
  648. }
  649. wl->last_tx_hlid = 0;
  650. } else {
  651. for (i = 0; i < NUM_TX_QUEUES; i++) {
  652. while ((skb = skb_dequeue(&wl->tx_queue[i]))) {
  653. wl1271_debug(DEBUG_TX, "freeing skb 0x%p",
  654. skb);
  655. if (!wl12xx_is_dummy_packet(wl, skb)) {
  656. info = IEEE80211_SKB_CB(skb);
  657. info->status.rates[0].idx = -1;
  658. info->status.rates[0].count = 0;
  659. ieee80211_tx_status(wl->hw, skb);
  660. }
  661. }
  662. }
  663. }
  664. wl->tx_queue_count = 0;
  665. /*
  666. * Make sure the driver is at a consistent state, in case this
  667. * function is called from a context other than interface removal.
  668. */
  669. wl1271_handle_tx_low_watermark(wl);
  670. for (i = 0; i < ACX_TX_DESCRIPTORS; i++) {
  671. if (wl->tx_frames[i] == NULL)
  672. continue;
  673. skb = wl->tx_frames[i];
  674. wl1271_free_tx_id(wl, i);
  675. wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
  676. if (!wl12xx_is_dummy_packet(wl, skb)) {
  677. /*
  678. * Remove private headers before passing the skb to
  679. * mac80211
  680. */
  681. info = IEEE80211_SKB_CB(skb);
  682. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  683. if (info->control.hw_key &&
  684. info->control.hw_key->cipher ==
  685. WLAN_CIPHER_SUITE_TKIP) {
  686. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  687. memmove(skb->data + WL1271_TKIP_IV_SPACE,
  688. skb->data, hdrlen);
  689. skb_pull(skb, WL1271_TKIP_IV_SPACE);
  690. }
  691. info->status.rates[0].idx = -1;
  692. info->status.rates[0].count = 0;
  693. ieee80211_tx_status(wl->hw, skb);
  694. }
  695. }
  696. }
  697. #define WL1271_TX_FLUSH_TIMEOUT 500000
  698. /* caller must *NOT* hold wl->mutex */
  699. void wl1271_tx_flush(struct wl1271 *wl)
  700. {
  701. unsigned long timeout;
  702. timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
  703. while (!time_after(jiffies, timeout)) {
  704. mutex_lock(&wl->mutex);
  705. wl1271_debug(DEBUG_TX, "flushing tx buffer: %d %d",
  706. wl->tx_frames_cnt, wl->tx_queue_count);
  707. if ((wl->tx_frames_cnt == 0) && (wl->tx_queue_count == 0)) {
  708. mutex_unlock(&wl->mutex);
  709. return;
  710. }
  711. mutex_unlock(&wl->mutex);
  712. msleep(1);
  713. }
  714. wl1271_warning("Unable to flush all TX buffers, timed out.");
  715. }
  716. u32 wl1271_tx_min_rate_get(struct wl1271 *wl)
  717. {
  718. int i;
  719. u32 rate = 0;
  720. if (!wl->basic_rate_set) {
  721. WARN_ON(1);
  722. wl->basic_rate_set = wl->conf.tx.basic_rate;
  723. }
  724. for (i = 0; !rate; i++) {
  725. if ((wl->basic_rate_set >> i) & 0x1)
  726. rate = 1 << i;
  727. }
  728. return rate;
  729. }