tx.c 34 KB

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