tx.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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 "wlcore.h"
  27. #include "debug.h"
  28. #include "io.h"
  29. #include "ps.h"
  30. #include "tx.h"
  31. #include "event.h"
  32. #include "hw_ops.h"
  33. /*
  34. * TODO: this is here just for now, it must be removed when the data
  35. * operations are in place.
  36. */
  37. #include "../wl12xx/reg.h"
  38. static int wl1271_set_default_wep_key(struct wl1271 *wl,
  39. struct wl12xx_vif *wlvif, u8 id)
  40. {
  41. int ret;
  42. bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
  43. if (is_ap)
  44. ret = wl12xx_cmd_set_default_wep_key(wl, id,
  45. wlvif->ap.bcast_hlid);
  46. else
  47. ret = wl12xx_cmd_set_default_wep_key(wl, id, wlvif->sta.hlid);
  48. if (ret < 0)
  49. return ret;
  50. wl1271_debug(DEBUG_CRYPT, "default wep key idx: %d", (int)id);
  51. return 0;
  52. }
  53. static int wl1271_alloc_tx_id(struct wl1271 *wl, struct sk_buff *skb)
  54. {
  55. int id;
  56. id = find_first_zero_bit(wl->tx_frames_map, wl->num_tx_desc);
  57. if (id >= wl->num_tx_desc)
  58. return -EBUSY;
  59. __set_bit(id, wl->tx_frames_map);
  60. wl->tx_frames[id] = skb;
  61. wl->tx_frames_cnt++;
  62. return id;
  63. }
  64. void wl1271_free_tx_id(struct wl1271 *wl, int id)
  65. {
  66. if (__test_and_clear_bit(id, wl->tx_frames_map)) {
  67. if (unlikely(wl->tx_frames_cnt == wl->num_tx_desc))
  68. clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
  69. wl->tx_frames[id] = NULL;
  70. wl->tx_frames_cnt--;
  71. }
  72. }
  73. EXPORT_SYMBOL(wl1271_free_tx_id);
  74. static void wl1271_tx_ap_update_inconnection_sta(struct wl1271 *wl,
  75. struct sk_buff *skb)
  76. {
  77. struct ieee80211_hdr *hdr;
  78. /*
  79. * add the station to the known list before transmitting the
  80. * authentication response. this way it won't get de-authed by FW
  81. * when transmitting too soon.
  82. */
  83. hdr = (struct ieee80211_hdr *)(skb->data +
  84. sizeof(struct wl1271_tx_hw_descr));
  85. if (ieee80211_is_auth(hdr->frame_control))
  86. wl1271_acx_set_inconnection_sta(wl, hdr->addr1);
  87. }
  88. static void wl1271_tx_regulate_link(struct wl1271 *wl,
  89. struct wl12xx_vif *wlvif,
  90. u8 hlid)
  91. {
  92. bool fw_ps, single_sta;
  93. u8 tx_pkts;
  94. if (WARN_ON(!test_bit(hlid, wlvif->links_map)))
  95. return;
  96. fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
  97. tx_pkts = wl->links[hlid].allocated_pkts;
  98. single_sta = (wl->active_sta_count == 1);
  99. /*
  100. * if in FW PS and there is enough data in FW we can put the link
  101. * into high-level PS and clean out its TX queues.
  102. * Make an exception if this is the only connected station. In this
  103. * case FW-memory congestion is not a problem.
  104. */
  105. if (!single_sta && fw_ps && tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
  106. wl12xx_ps_link_start(wl, wlvif, hlid, true);
  107. }
  108. bool wl12xx_is_dummy_packet(struct wl1271 *wl, struct sk_buff *skb)
  109. {
  110. return wl->dummy_packet == skb;
  111. }
  112. EXPORT_SYMBOL(wl12xx_is_dummy_packet);
  113. u8 wl12xx_tx_get_hlid_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  114. struct sk_buff *skb)
  115. {
  116. struct ieee80211_tx_info *control = IEEE80211_SKB_CB(skb);
  117. if (control->control.sta) {
  118. struct wl1271_station *wl_sta;
  119. wl_sta = (struct wl1271_station *)
  120. control->control.sta->drv_priv;
  121. return wl_sta->hlid;
  122. } else {
  123. struct ieee80211_hdr *hdr;
  124. if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
  125. return wl->system_hlid;
  126. hdr = (struct ieee80211_hdr *)skb->data;
  127. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
  128. return wlvif->ap.bcast_hlid;
  129. else
  130. return wlvif->ap.global_hlid;
  131. }
  132. }
  133. u8 wl12xx_tx_get_hlid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  134. struct sk_buff *skb)
  135. {
  136. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  137. if (!wlvif || wl12xx_is_dummy_packet(wl, skb))
  138. return wl->system_hlid;
  139. if (wlvif->bss_type == BSS_TYPE_AP_BSS)
  140. return wl12xx_tx_get_hlid_ap(wl, wlvif, skb);
  141. if ((test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
  142. test_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags)) &&
  143. !ieee80211_is_auth(hdr->frame_control) &&
  144. !ieee80211_is_assoc_req(hdr->frame_control))
  145. return wlvif->sta.hlid;
  146. else
  147. return wlvif->dev_hlid;
  148. }
  149. unsigned int wlcore_calc_packet_alignment(struct wl1271 *wl,
  150. unsigned int packet_length)
  151. {
  152. if ((wl->quirks & WLCORE_QUIRK_TX_PAD_LAST_FRAME) ||
  153. !(wl->quirks & WLCORE_QUIRK_TX_BLOCKSIZE_ALIGN))
  154. return ALIGN(packet_length, WL1271_TX_ALIGN_TO);
  155. else
  156. return ALIGN(packet_length, WL12XX_BUS_BLOCK_SIZE);
  157. }
  158. EXPORT_SYMBOL(wlcore_calc_packet_alignment);
  159. static int wl1271_tx_allocate(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  160. struct sk_buff *skb, u32 extra, u32 buf_offset,
  161. u8 hlid, bool is_gem)
  162. {
  163. struct wl1271_tx_hw_descr *desc;
  164. u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
  165. u32 total_blocks;
  166. int id, ret = -EBUSY, ac;
  167. u32 spare_blocks;
  168. if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
  169. return -EAGAIN;
  170. spare_blocks = wlcore_hw_get_spare_blocks(wl, is_gem);
  171. /* allocate free identifier for the packet */
  172. id = wl1271_alloc_tx_id(wl, skb);
  173. if (id < 0)
  174. return id;
  175. total_blocks = wlcore_hw_calc_tx_blocks(wl, total_len, spare_blocks);
  176. if (total_blocks <= wl->tx_blocks_available) {
  177. desc = (struct wl1271_tx_hw_descr *)skb_push(
  178. skb, total_len - skb->len);
  179. wlcore_hw_set_tx_desc_blocks(wl, desc, total_blocks,
  180. spare_blocks);
  181. desc->id = id;
  182. wl->tx_blocks_available -= total_blocks;
  183. wl->tx_allocated_blocks += total_blocks;
  184. /* If the FW was empty before, arm the Tx watchdog */
  185. if (wl->tx_allocated_blocks == total_blocks)
  186. wl12xx_rearm_tx_watchdog_locked(wl);
  187. ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  188. wl->tx_allocated_pkts[ac]++;
  189. if (!wl12xx_is_dummy_packet(wl, skb) && wlvif &&
  190. wlvif->bss_type == BSS_TYPE_AP_BSS &&
  191. test_bit(hlid, wlvif->ap.sta_hlid_map))
  192. wl->links[hlid].allocated_pkts++;
  193. ret = 0;
  194. wl1271_debug(DEBUG_TX,
  195. "tx_allocate: size: %d, blocks: %d, id: %d",
  196. total_len, total_blocks, id);
  197. } else {
  198. wl1271_free_tx_id(wl, id);
  199. }
  200. return ret;
  201. }
  202. static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  203. struct sk_buff *skb, u32 extra,
  204. struct ieee80211_tx_info *control, u8 hlid)
  205. {
  206. struct timespec ts;
  207. struct wl1271_tx_hw_descr *desc;
  208. int ac, rate_idx;
  209. s64 hosttime;
  210. u16 tx_attr = 0;
  211. __le16 frame_control;
  212. struct ieee80211_hdr *hdr;
  213. u8 *frame_start;
  214. bool is_dummy;
  215. desc = (struct wl1271_tx_hw_descr *) skb->data;
  216. frame_start = (u8 *)(desc + 1);
  217. hdr = (struct ieee80211_hdr *)(frame_start + extra);
  218. frame_control = hdr->frame_control;
  219. /* relocate space for security header */
  220. if (extra) {
  221. int hdrlen = ieee80211_hdrlen(frame_control);
  222. memmove(frame_start, hdr, hdrlen);
  223. skb_set_network_header(skb, skb_network_offset(skb) + extra);
  224. }
  225. /* configure packet life time */
  226. getnstimeofday(&ts);
  227. hosttime = (timespec_to_ns(&ts) >> 10);
  228. desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
  229. is_dummy = wl12xx_is_dummy_packet(wl, skb);
  230. if (is_dummy || !wlvif || wlvif->bss_type != BSS_TYPE_AP_BSS)
  231. desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
  232. else
  233. desc->life_time = cpu_to_le16(TX_HW_AP_MODE_PKT_LIFETIME_TU);
  234. /* queue */
  235. ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  236. desc->tid = skb->priority;
  237. if (is_dummy) {
  238. /*
  239. * FW expects the dummy packet to have an invalid session id -
  240. * any session id that is different than the one set in the join
  241. */
  242. tx_attr = (SESSION_COUNTER_INVALID <<
  243. TX_HW_ATTR_OFST_SESSION_COUNTER) &
  244. TX_HW_ATTR_SESSION_COUNTER;
  245. tx_attr |= TX_HW_ATTR_TX_DUMMY_REQ;
  246. } else if (wlvif) {
  247. /* configure the tx attributes */
  248. tx_attr = wlvif->session_counter <<
  249. TX_HW_ATTR_OFST_SESSION_COUNTER;
  250. }
  251. desc->hlid = hlid;
  252. if (is_dummy || !wlvif)
  253. rate_idx = 0;
  254. else if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
  255. /*
  256. * if the packets are destined for AP (have a STA entry)
  257. * send them with AP rate policies (EAPOLs are an exception),
  258. * otherwise use default basic rates
  259. */
  260. if (control->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
  261. rate_idx = wlvif->sta.p2p_rate_idx;
  262. else if (skb->protocol == cpu_to_be16(ETH_P_PAE))
  263. rate_idx = wlvif->sta.basic_rate_idx;
  264. else if (control->control.sta)
  265. rate_idx = wlvif->sta.ap_rate_idx;
  266. else
  267. rate_idx = wlvif->sta.basic_rate_idx;
  268. } else {
  269. if (hlid == wlvif->ap.global_hlid)
  270. rate_idx = wlvif->ap.mgmt_rate_idx;
  271. else if (hlid == wlvif->ap.bcast_hlid)
  272. rate_idx = wlvif->ap.bcast_rate_idx;
  273. else
  274. rate_idx = wlvif->ap.ucast_rate_idx[ac];
  275. }
  276. tx_attr |= rate_idx << TX_HW_ATTR_OFST_RATE_POLICY;
  277. /* for WEP shared auth - no fw encryption is needed */
  278. if (ieee80211_is_auth(frame_control) &&
  279. ieee80211_has_protected(frame_control))
  280. tx_attr |= TX_HW_ATTR_HOST_ENCRYPT;
  281. desc->tx_attr = cpu_to_le16(tx_attr);
  282. wlcore_hw_set_tx_desc_csum(wl, desc, skb);
  283. wlcore_hw_set_tx_desc_data_len(wl, desc, skb);
  284. }
  285. /* caller must hold wl->mutex */
  286. static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  287. struct sk_buff *skb, u32 buf_offset)
  288. {
  289. struct ieee80211_tx_info *info;
  290. u32 extra = 0;
  291. int ret = 0;
  292. u32 total_len;
  293. u8 hlid;
  294. bool is_dummy;
  295. bool is_gem = false;
  296. if (!skb)
  297. return -EINVAL;
  298. info = IEEE80211_SKB_CB(skb);
  299. /* TODO: handle dummy packets on multi-vifs */
  300. is_dummy = wl12xx_is_dummy_packet(wl, skb);
  301. if ((wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE) &&
  302. info->control.hw_key &&
  303. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
  304. extra = WL1271_EXTRA_SPACE_TKIP;
  305. if (info->control.hw_key) {
  306. bool is_wep;
  307. u8 idx = info->control.hw_key->hw_key_idx;
  308. u32 cipher = info->control.hw_key->cipher;
  309. is_wep = (cipher == WLAN_CIPHER_SUITE_WEP40) ||
  310. (cipher == WLAN_CIPHER_SUITE_WEP104);
  311. if (unlikely(is_wep && wlvif->default_key != idx)) {
  312. ret = wl1271_set_default_wep_key(wl, wlvif, idx);
  313. if (ret < 0)
  314. return ret;
  315. wlvif->default_key = idx;
  316. }
  317. is_gem = (cipher == WL1271_CIPHER_SUITE_GEM);
  318. }
  319. hlid = wl12xx_tx_get_hlid(wl, wlvif, skb);
  320. if (hlid == WL12XX_INVALID_LINK_ID) {
  321. wl1271_error("invalid hlid. dropping skb 0x%p", skb);
  322. return -EINVAL;
  323. }
  324. ret = wl1271_tx_allocate(wl, wlvif, skb, extra, buf_offset, hlid,
  325. is_gem);
  326. if (ret < 0)
  327. return ret;
  328. wl1271_tx_fill_hdr(wl, wlvif, skb, extra, info, hlid);
  329. if (!is_dummy && wlvif && wlvif->bss_type == BSS_TYPE_AP_BSS) {
  330. wl1271_tx_ap_update_inconnection_sta(wl, skb);
  331. wl1271_tx_regulate_link(wl, wlvif, hlid);
  332. }
  333. /*
  334. * The length of each packet is stored in terms of
  335. * words. Thus, we must pad the skb data to make sure its
  336. * length is aligned. The number of padding bytes is computed
  337. * and set in wl1271_tx_fill_hdr.
  338. * In special cases, we want to align to a specific block size
  339. * (eg. for wl128x with SDIO we align to 256).
  340. */
  341. total_len = wlcore_calc_packet_alignment(wl, skb->len);
  342. memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
  343. memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
  344. /* Revert side effects in the dummy packet skb, so it can be reused */
  345. if (is_dummy)
  346. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  347. return total_len;
  348. }
  349. u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set,
  350. enum ieee80211_band rate_band)
  351. {
  352. struct ieee80211_supported_band *band;
  353. u32 enabled_rates = 0;
  354. int bit;
  355. band = wl->hw->wiphy->bands[rate_band];
  356. for (bit = 0; bit < band->n_bitrates; bit++) {
  357. if (rate_set & 0x1)
  358. enabled_rates |= band->bitrates[bit].hw_value;
  359. rate_set >>= 1;
  360. }
  361. /* MCS rates indication are on bits 16 - 31 */
  362. rate_set >>= HW_HT_RATES_OFFSET - band->n_bitrates;
  363. for (bit = 0; bit < 16; bit++) {
  364. if (rate_set & 0x1)
  365. enabled_rates |= (CONF_HW_BIT_RATE_MCS_0 << bit);
  366. rate_set >>= 1;
  367. }
  368. return enabled_rates;
  369. }
  370. void wl1271_handle_tx_low_watermark(struct wl1271 *wl)
  371. {
  372. int i;
  373. for (i = 0; i < NUM_TX_QUEUES; i++) {
  374. if (wlcore_is_queue_stopped_by_reason(wl, i,
  375. WLCORE_QUEUE_STOP_REASON_WATERMARK) &&
  376. wl->tx_queue_count[i] <= WL1271_TX_QUEUE_LOW_WATERMARK) {
  377. /* firmware buffer has space, restart queues */
  378. wlcore_wake_queue(wl, i,
  379. WLCORE_QUEUE_STOP_REASON_WATERMARK);
  380. }
  381. }
  382. }
  383. static struct sk_buff_head *wl1271_select_queue(struct wl1271 *wl,
  384. struct sk_buff_head *queues)
  385. {
  386. int i, q = -1, ac;
  387. u32 min_pkts = 0xffffffff;
  388. /*
  389. * Find a non-empty ac where:
  390. * 1. There are packets to transmit
  391. * 2. The FW has the least allocated blocks
  392. *
  393. * We prioritize the ACs according to VO>VI>BE>BK
  394. */
  395. for (i = 0; i < NUM_TX_QUEUES; i++) {
  396. ac = wl1271_tx_get_queue(i);
  397. if (!skb_queue_empty(&queues[ac]) &&
  398. (wl->tx_allocated_pkts[ac] < min_pkts)) {
  399. q = ac;
  400. min_pkts = wl->tx_allocated_pkts[q];
  401. }
  402. }
  403. if (q == -1)
  404. return NULL;
  405. return &queues[q];
  406. }
  407. static struct sk_buff *wl12xx_lnk_skb_dequeue(struct wl1271 *wl,
  408. struct wl1271_link *lnk)
  409. {
  410. struct sk_buff *skb;
  411. unsigned long flags;
  412. struct sk_buff_head *queue;
  413. queue = wl1271_select_queue(wl, lnk->tx_queue);
  414. if (!queue)
  415. return NULL;
  416. skb = skb_dequeue(queue);
  417. if (skb) {
  418. int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  419. spin_lock_irqsave(&wl->wl_lock, flags);
  420. WARN_ON_ONCE(wl->tx_queue_count[q] <= 0);
  421. wl->tx_queue_count[q]--;
  422. spin_unlock_irqrestore(&wl->wl_lock, flags);
  423. }
  424. return skb;
  425. }
  426. static struct sk_buff *wl12xx_vif_skb_dequeue(struct wl1271 *wl,
  427. struct wl12xx_vif *wlvif)
  428. {
  429. struct sk_buff *skb = NULL;
  430. int i, h, start_hlid;
  431. /* start from the link after the last one */
  432. start_hlid = (wlvif->last_tx_hlid + 1) % WL12XX_MAX_LINKS;
  433. /* dequeue according to AC, round robin on each link */
  434. for (i = 0; i < WL12XX_MAX_LINKS; i++) {
  435. h = (start_hlid + i) % WL12XX_MAX_LINKS;
  436. /* only consider connected stations */
  437. if (!test_bit(h, wlvif->links_map))
  438. continue;
  439. skb = wl12xx_lnk_skb_dequeue(wl, &wl->links[h]);
  440. if (!skb)
  441. continue;
  442. wlvif->last_tx_hlid = h;
  443. break;
  444. }
  445. if (!skb)
  446. wlvif->last_tx_hlid = 0;
  447. return skb;
  448. }
  449. static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
  450. {
  451. unsigned long flags;
  452. struct wl12xx_vif *wlvif = wl->last_wlvif;
  453. struct sk_buff *skb = NULL;
  454. /* continue from last wlvif (round robin) */
  455. if (wlvif) {
  456. wl12xx_for_each_wlvif_continue(wl, wlvif) {
  457. skb = wl12xx_vif_skb_dequeue(wl, wlvif);
  458. if (skb) {
  459. wl->last_wlvif = wlvif;
  460. break;
  461. }
  462. }
  463. }
  464. /* dequeue from the system HLID before the restarting wlvif list */
  465. if (!skb)
  466. skb = wl12xx_lnk_skb_dequeue(wl, &wl->links[wl->system_hlid]);
  467. /* do a new pass over the wlvif list */
  468. if (!skb) {
  469. wl12xx_for_each_wlvif(wl, wlvif) {
  470. skb = wl12xx_vif_skb_dequeue(wl, wlvif);
  471. if (skb) {
  472. wl->last_wlvif = wlvif;
  473. break;
  474. }
  475. /*
  476. * No need to continue after last_wlvif. The previous
  477. * pass should have found it.
  478. */
  479. if (wlvif == wl->last_wlvif)
  480. break;
  481. }
  482. }
  483. if (!skb &&
  484. test_and_clear_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags)) {
  485. int q;
  486. skb = wl->dummy_packet;
  487. q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  488. spin_lock_irqsave(&wl->wl_lock, flags);
  489. WARN_ON_ONCE(wl->tx_queue_count[q] <= 0);
  490. wl->tx_queue_count[q]--;
  491. spin_unlock_irqrestore(&wl->wl_lock, flags);
  492. }
  493. return skb;
  494. }
  495. static void wl1271_skb_queue_head(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  496. struct sk_buff *skb)
  497. {
  498. unsigned long flags;
  499. int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
  500. if (wl12xx_is_dummy_packet(wl, skb)) {
  501. set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
  502. } else {
  503. u8 hlid = wl12xx_tx_get_hlid(wl, wlvif, skb);
  504. skb_queue_head(&wl->links[hlid].tx_queue[q], skb);
  505. /* make sure we dequeue the same packet next time */
  506. wlvif->last_tx_hlid = (hlid + WL12XX_MAX_LINKS - 1) %
  507. WL12XX_MAX_LINKS;
  508. }
  509. spin_lock_irqsave(&wl->wl_lock, flags);
  510. wl->tx_queue_count[q]++;
  511. spin_unlock_irqrestore(&wl->wl_lock, flags);
  512. }
  513. static bool wl1271_tx_is_data_present(struct sk_buff *skb)
  514. {
  515. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data);
  516. return ieee80211_is_data_present(hdr->frame_control);
  517. }
  518. void wl12xx_rearm_rx_streaming(struct wl1271 *wl, unsigned long *active_hlids)
  519. {
  520. struct wl12xx_vif *wlvif;
  521. u32 timeout;
  522. u8 hlid;
  523. if (!wl->conf.rx_streaming.interval)
  524. return;
  525. if (!wl->conf.rx_streaming.always &&
  526. !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags))
  527. return;
  528. timeout = wl->conf.rx_streaming.duration;
  529. wl12xx_for_each_wlvif_sta(wl, wlvif) {
  530. bool found = false;
  531. for_each_set_bit(hlid, active_hlids, WL12XX_MAX_LINKS) {
  532. if (test_bit(hlid, wlvif->links_map)) {
  533. found = true;
  534. break;
  535. }
  536. }
  537. if (!found)
  538. continue;
  539. /* enable rx streaming */
  540. if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
  541. ieee80211_queue_work(wl->hw,
  542. &wlvif->rx_streaming_enable_work);
  543. mod_timer(&wlvif->rx_streaming_timer,
  544. jiffies + msecs_to_jiffies(timeout));
  545. }
  546. }
  547. void wl1271_tx_work_locked(struct wl1271 *wl)
  548. {
  549. struct wl12xx_vif *wlvif;
  550. struct sk_buff *skb;
  551. struct wl1271_tx_hw_descr *desc;
  552. u32 buf_offset = 0, last_len = 0;
  553. bool sent_packets = false;
  554. unsigned long active_hlids[BITS_TO_LONGS(WL12XX_MAX_LINKS)] = {0};
  555. int ret;
  556. if (unlikely(wl->state == WL1271_STATE_OFF))
  557. return;
  558. while ((skb = wl1271_skb_dequeue(wl))) {
  559. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  560. bool has_data = false;
  561. wlvif = NULL;
  562. if (!wl12xx_is_dummy_packet(wl, skb) && info->control.vif)
  563. wlvif = wl12xx_vif_to_data(info->control.vif);
  564. has_data = wlvif && wl1271_tx_is_data_present(skb);
  565. ret = wl1271_prepare_tx_frame(wl, wlvif, skb, buf_offset);
  566. if (ret == -EAGAIN) {
  567. /*
  568. * Aggregation buffer is full.
  569. * Flush buffer and try again.
  570. */
  571. wl1271_skb_queue_head(wl, wlvif, skb);
  572. buf_offset = wlcore_hw_pre_pkt_send(wl, buf_offset,
  573. last_len);
  574. wlcore_write_data(wl, REG_SLV_MEM_DATA, wl->aggr_buf,
  575. buf_offset, true);
  576. sent_packets = true;
  577. buf_offset = 0;
  578. continue;
  579. } else if (ret == -EBUSY) {
  580. /*
  581. * Firmware buffer is full.
  582. * Queue back last skb, and stop aggregating.
  583. */
  584. wl1271_skb_queue_head(wl, wlvif, skb);
  585. /* No work left, avoid scheduling redundant tx work */
  586. set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
  587. goto out_ack;
  588. } else if (ret < 0) {
  589. if (wl12xx_is_dummy_packet(wl, skb))
  590. /*
  591. * fw still expects dummy packet,
  592. * so re-enqueue it
  593. */
  594. wl1271_skb_queue_head(wl, wlvif, skb);
  595. else
  596. ieee80211_free_txskb(wl->hw, skb);
  597. goto out_ack;
  598. }
  599. last_len = ret;
  600. buf_offset += last_len;
  601. wl->tx_packets_count++;
  602. if (has_data) {
  603. desc = (struct wl1271_tx_hw_descr *) skb->data;
  604. __set_bit(desc->hlid, active_hlids);
  605. }
  606. }
  607. out_ack:
  608. if (buf_offset) {
  609. buf_offset = wlcore_hw_pre_pkt_send(wl, buf_offset, last_len);
  610. wlcore_write_data(wl, REG_SLV_MEM_DATA, wl->aggr_buf,
  611. buf_offset, true);
  612. sent_packets = true;
  613. }
  614. if (sent_packets) {
  615. /*
  616. * Interrupt the firmware with the new packets. This is only
  617. * required for older hardware revisions
  618. */
  619. if (wl->quirks & WLCORE_QUIRK_END_OF_TRANSACTION)
  620. wl1271_write32(wl, WL12XX_HOST_WR_ACCESS,
  621. wl->tx_packets_count);
  622. wl1271_handle_tx_low_watermark(wl);
  623. }
  624. wl12xx_rearm_rx_streaming(wl, active_hlids);
  625. }
  626. void wl1271_tx_work(struct work_struct *work)
  627. {
  628. struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
  629. int ret;
  630. mutex_lock(&wl->mutex);
  631. ret = wl1271_ps_elp_wakeup(wl);
  632. if (ret < 0)
  633. goto out;
  634. wl1271_tx_work_locked(wl);
  635. wl1271_ps_elp_sleep(wl);
  636. out:
  637. mutex_unlock(&wl->mutex);
  638. }
  639. static u8 wl1271_tx_get_rate_flags(u8 rate_class_index)
  640. {
  641. u8 flags = 0;
  642. /*
  643. * TODO: use wl12xx constants when this code is moved to wl12xx, as
  644. * only it uses Tx-completion.
  645. */
  646. if (rate_class_index <= 8)
  647. flags |= IEEE80211_TX_RC_MCS;
  648. /*
  649. * TODO: use wl12xx constants when this code is moved to wl12xx, as
  650. * only it uses Tx-completion.
  651. */
  652. if (rate_class_index == 0)
  653. flags |= IEEE80211_TX_RC_SHORT_GI;
  654. return flags;
  655. }
  656. static void wl1271_tx_complete_packet(struct wl1271 *wl,
  657. struct wl1271_tx_hw_res_descr *result)
  658. {
  659. struct ieee80211_tx_info *info;
  660. struct ieee80211_vif *vif;
  661. struct wl12xx_vif *wlvif;
  662. struct sk_buff *skb;
  663. int id = result->id;
  664. int rate = -1;
  665. u8 rate_flags = 0;
  666. u8 retries = 0;
  667. /* check for id legality */
  668. if (unlikely(id >= wl->num_tx_desc || wl->tx_frames[id] == NULL)) {
  669. wl1271_warning("TX result illegal id: %d", id);
  670. return;
  671. }
  672. skb = wl->tx_frames[id];
  673. info = IEEE80211_SKB_CB(skb);
  674. if (wl12xx_is_dummy_packet(wl, skb)) {
  675. wl1271_free_tx_id(wl, id);
  676. return;
  677. }
  678. /* info->control is valid as long as we don't update info->status */
  679. vif = info->control.vif;
  680. wlvif = wl12xx_vif_to_data(vif);
  681. /* update the TX status info */
  682. if (result->status == TX_SUCCESS) {
  683. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  684. info->flags |= IEEE80211_TX_STAT_ACK;
  685. rate = wlcore_rate_to_idx(wl, result->rate_class_index,
  686. wlvif->band);
  687. rate_flags = wl1271_tx_get_rate_flags(result->rate_class_index);
  688. retries = result->ack_failures;
  689. } else if (result->status == TX_RETRY_EXCEEDED) {
  690. wl->stats.excessive_retries++;
  691. retries = result->ack_failures;
  692. }
  693. info->status.rates[0].idx = rate;
  694. info->status.rates[0].count = retries;
  695. info->status.rates[0].flags = rate_flags;
  696. info->status.ack_signal = -1;
  697. wl->stats.retry_count += result->ack_failures;
  698. /*
  699. * update sequence number only when relevant, i.e. only in
  700. * sessions of TKIP, AES and GEM (not in open or WEP sessions)
  701. */
  702. if (info->control.hw_key &&
  703. (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP ||
  704. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_CCMP ||
  705. info->control.hw_key->cipher == WL1271_CIPHER_SUITE_GEM)) {
  706. u8 fw_lsb = result->tx_security_sequence_number_lsb;
  707. u8 cur_lsb = wlvif->tx_security_last_seq_lsb;
  708. /*
  709. * update security sequence number, taking care of potential
  710. * wrap-around
  711. */
  712. wlvif->tx_security_seq += (fw_lsb - cur_lsb) & 0xff;
  713. wlvif->tx_security_last_seq_lsb = fw_lsb;
  714. }
  715. /* remove private header from packet */
  716. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  717. /* remove TKIP header space if present */
  718. if ((wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE) &&
  719. info->control.hw_key &&
  720. info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  721. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  722. memmove(skb->data + WL1271_EXTRA_SPACE_TKIP, skb->data,
  723. hdrlen);
  724. skb_pull(skb, WL1271_EXTRA_SPACE_TKIP);
  725. }
  726. wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
  727. " status 0x%x",
  728. result->id, skb, result->ack_failures,
  729. result->rate_class_index, result->status);
  730. /* return the packet to the stack */
  731. skb_queue_tail(&wl->deferred_tx_queue, skb);
  732. queue_work(wl->freezable_wq, &wl->netstack_work);
  733. wl1271_free_tx_id(wl, result->id);
  734. }
  735. /* Called upon reception of a TX complete interrupt */
  736. int wlcore_tx_complete(struct wl1271 *wl)
  737. {
  738. struct wl1271_acx_mem_map *memmap =
  739. (struct wl1271_acx_mem_map *)wl->target_mem_map;
  740. u32 count, fw_counter;
  741. u32 i;
  742. int ret;
  743. /* read the tx results from the chipset */
  744. ret = wlcore_read(wl, le32_to_cpu(memmap->tx_result),
  745. wl->tx_res_if, sizeof(*wl->tx_res_if), false);
  746. if (ret < 0)
  747. goto out;
  748. fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
  749. /* write host counter to chipset (to ack) */
  750. wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
  751. offsetof(struct wl1271_tx_hw_res_if,
  752. tx_result_host_counter), fw_counter);
  753. count = fw_counter - wl->tx_results_count;
  754. wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
  755. /* verify that the result buffer is not getting overrun */
  756. if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
  757. wl1271_warning("TX result overflow from chipset: %d", count);
  758. /* process the results */
  759. for (i = 0; i < count; i++) {
  760. struct wl1271_tx_hw_res_descr *result;
  761. u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
  762. /* process the packet */
  763. result = &(wl->tx_res_if->tx_results_queue[offset]);
  764. wl1271_tx_complete_packet(wl, result);
  765. wl->tx_results_count++;
  766. }
  767. out:
  768. return ret;
  769. }
  770. EXPORT_SYMBOL(wlcore_tx_complete);
  771. void wl1271_tx_reset_link_queues(struct wl1271 *wl, u8 hlid)
  772. {
  773. struct sk_buff *skb;
  774. int i;
  775. unsigned long flags;
  776. struct ieee80211_tx_info *info;
  777. int total[NUM_TX_QUEUES];
  778. for (i = 0; i < NUM_TX_QUEUES; i++) {
  779. total[i] = 0;
  780. while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
  781. wl1271_debug(DEBUG_TX, "link freeing skb 0x%p", skb);
  782. if (!wl12xx_is_dummy_packet(wl, skb)) {
  783. info = IEEE80211_SKB_CB(skb);
  784. info->status.rates[0].idx = -1;
  785. info->status.rates[0].count = 0;
  786. ieee80211_tx_status_ni(wl->hw, skb);
  787. }
  788. total[i]++;
  789. }
  790. }
  791. spin_lock_irqsave(&wl->wl_lock, flags);
  792. for (i = 0; i < NUM_TX_QUEUES; i++)
  793. wl->tx_queue_count[i] -= total[i];
  794. spin_unlock_irqrestore(&wl->wl_lock, flags);
  795. wl1271_handle_tx_low_watermark(wl);
  796. }
  797. /* caller must hold wl->mutex and TX must be stopped */
  798. void wl12xx_tx_reset_wlvif(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  799. {
  800. int i;
  801. /* TX failure */
  802. for_each_set_bit(i, wlvif->links_map, WL12XX_MAX_LINKS) {
  803. if (wlvif->bss_type == BSS_TYPE_AP_BSS)
  804. wl1271_free_sta(wl, wlvif, i);
  805. else
  806. wlvif->sta.ba_rx_bitmap = 0;
  807. wl->links[i].allocated_pkts = 0;
  808. wl->links[i].prev_freed_pkts = 0;
  809. }
  810. wlvif->last_tx_hlid = 0;
  811. }
  812. /* caller must hold wl->mutex and TX must be stopped */
  813. void wl12xx_tx_reset(struct wl1271 *wl)
  814. {
  815. int i;
  816. struct sk_buff *skb;
  817. struct ieee80211_tx_info *info;
  818. /* only reset the queues if something bad happened */
  819. if (WARN_ON_ONCE(wl1271_tx_total_queue_count(wl) != 0)) {
  820. for (i = 0; i < WL12XX_MAX_LINKS; i++)
  821. wl1271_tx_reset_link_queues(wl, i);
  822. for (i = 0; i < NUM_TX_QUEUES; i++)
  823. wl->tx_queue_count[i] = 0;
  824. }
  825. /*
  826. * Make sure the driver is at a consistent state, in case this
  827. * function is called from a context other than interface removal.
  828. * This call will always wake the TX queues.
  829. */
  830. wl1271_handle_tx_low_watermark(wl);
  831. for (i = 0; i < wl->num_tx_desc; i++) {
  832. if (wl->tx_frames[i] == NULL)
  833. continue;
  834. skb = wl->tx_frames[i];
  835. wl1271_free_tx_id(wl, i);
  836. wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
  837. if (!wl12xx_is_dummy_packet(wl, skb)) {
  838. /*
  839. * Remove private headers before passing the skb to
  840. * mac80211
  841. */
  842. info = IEEE80211_SKB_CB(skb);
  843. skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
  844. if ((wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE) &&
  845. info->control.hw_key &&
  846. info->control.hw_key->cipher ==
  847. WLAN_CIPHER_SUITE_TKIP) {
  848. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  849. memmove(skb->data + WL1271_EXTRA_SPACE_TKIP,
  850. skb->data, hdrlen);
  851. skb_pull(skb, WL1271_EXTRA_SPACE_TKIP);
  852. }
  853. info->status.rates[0].idx = -1;
  854. info->status.rates[0].count = 0;
  855. ieee80211_tx_status_ni(wl->hw, skb);
  856. }
  857. }
  858. }
  859. #define WL1271_TX_FLUSH_TIMEOUT 500000
  860. /* caller must *NOT* hold wl->mutex */
  861. void wl1271_tx_flush(struct wl1271 *wl)
  862. {
  863. unsigned long timeout;
  864. int i;
  865. timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
  866. /* only one flush should be in progress, for consistent queue state */
  867. mutex_lock(&wl->flush_mutex);
  868. wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_FLUSH);
  869. while (!time_after(jiffies, timeout)) {
  870. mutex_lock(&wl->mutex);
  871. wl1271_debug(DEBUG_TX, "flushing tx buffer: %d %d",
  872. wl->tx_frames_cnt,
  873. wl1271_tx_total_queue_count(wl));
  874. if ((wl->tx_frames_cnt == 0) &&
  875. (wl1271_tx_total_queue_count(wl) == 0)) {
  876. mutex_unlock(&wl->mutex);
  877. goto out;
  878. }
  879. mutex_unlock(&wl->mutex);
  880. msleep(1);
  881. }
  882. wl1271_warning("Unable to flush all TX buffers, timed out.");
  883. /* forcibly flush all Tx buffers on our queues */
  884. mutex_lock(&wl->mutex);
  885. for (i = 0; i < WL12XX_MAX_LINKS; i++)
  886. wl1271_tx_reset_link_queues(wl, i);
  887. mutex_unlock(&wl->mutex);
  888. out:
  889. wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_FLUSH);
  890. mutex_unlock(&wl->flush_mutex);
  891. }
  892. EXPORT_SYMBOL_GPL(wl1271_tx_flush);
  893. u32 wl1271_tx_min_rate_get(struct wl1271 *wl, u32 rate_set)
  894. {
  895. if (WARN_ON(!rate_set))
  896. return 0;
  897. return BIT(__ffs(rate_set));
  898. }
  899. void wlcore_stop_queue_locked(struct wl1271 *wl, u8 queue,
  900. enum wlcore_queue_stop_reason reason)
  901. {
  902. bool stopped = !!wl->queue_stop_reasons[queue];
  903. /* queue should not be stopped for this reason */
  904. WARN_ON(test_and_set_bit(reason, &wl->queue_stop_reasons[queue]));
  905. if (stopped)
  906. return;
  907. ieee80211_stop_queue(wl->hw, wl1271_tx_get_mac80211_queue(queue));
  908. }
  909. void wlcore_stop_queue(struct wl1271 *wl, u8 queue,
  910. enum wlcore_queue_stop_reason reason)
  911. {
  912. unsigned long flags;
  913. spin_lock_irqsave(&wl->wl_lock, flags);
  914. wlcore_stop_queue_locked(wl, queue, reason);
  915. spin_unlock_irqrestore(&wl->wl_lock, flags);
  916. }
  917. void wlcore_wake_queue(struct wl1271 *wl, u8 queue,
  918. enum wlcore_queue_stop_reason reason)
  919. {
  920. unsigned long flags;
  921. spin_lock_irqsave(&wl->wl_lock, flags);
  922. /* queue should not be clear for this reason */
  923. WARN_ON(!test_and_clear_bit(reason, &wl->queue_stop_reasons[queue]));
  924. if (wl->queue_stop_reasons[queue])
  925. goto out;
  926. ieee80211_wake_queue(wl->hw, wl1271_tx_get_mac80211_queue(queue));
  927. out:
  928. spin_unlock_irqrestore(&wl->wl_lock, flags);
  929. }
  930. void wlcore_stop_queues(struct wl1271 *wl,
  931. enum wlcore_queue_stop_reason reason)
  932. {
  933. int i;
  934. for (i = 0; i < NUM_TX_QUEUES; i++)
  935. wlcore_stop_queue(wl, i, reason);
  936. }
  937. EXPORT_SYMBOL_GPL(wlcore_stop_queues);
  938. void wlcore_wake_queues(struct wl1271 *wl,
  939. enum wlcore_queue_stop_reason reason)
  940. {
  941. int i;
  942. for (i = 0; i < NUM_TX_QUEUES; i++)
  943. wlcore_wake_queue(wl, i, reason);
  944. }
  945. EXPORT_SYMBOL_GPL(wlcore_wake_queues);
  946. void wlcore_reset_stopped_queues(struct wl1271 *wl)
  947. {
  948. int i;
  949. unsigned long flags;
  950. spin_lock_irqsave(&wl->wl_lock, flags);
  951. for (i = 0; i < NUM_TX_QUEUES; i++) {
  952. if (!wl->queue_stop_reasons[i])
  953. continue;
  954. wl->queue_stop_reasons[i] = 0;
  955. ieee80211_wake_queue(wl->hw,
  956. wl1271_tx_get_mac80211_queue(i));
  957. }
  958. spin_unlock_irqrestore(&wl->wl_lock, flags);
  959. }
  960. bool wlcore_is_queue_stopped_by_reason(struct wl1271 *wl, u8 queue,
  961. enum wlcore_queue_stop_reason reason)
  962. {
  963. return test_bit(reason, &wl->queue_stop_reasons[queue]);
  964. }
  965. bool wlcore_is_queue_stopped(struct wl1271 *wl, u8 queue)
  966. {
  967. return !!wl->queue_stop_reasons[queue];
  968. }