tx.c 27 KB

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