iwl-4965-tx.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/sched.h>
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-sta.h"
  36. #include "iwl-io.h"
  37. #include "iwl-helpers.h"
  38. #include "iwl-4965-hw.h"
  39. #include "iwl-4965.h"
  40. /*
  41. * mac80211 queues, ACs, hardware queues, FIFOs.
  42. *
  43. * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
  44. *
  45. * Mac80211 uses the following numbers, which we get as from it
  46. * by way of skb_get_queue_mapping(skb):
  47. *
  48. * VO 0
  49. * VI 1
  50. * BE 2
  51. * BK 3
  52. *
  53. *
  54. * Regular (not A-MPDU) frames are put into hardware queues corresponding
  55. * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
  56. * own queue per aggregation session (RA/TID combination), such queues are
  57. * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
  58. * order to map frames to the right queue, we also need an AC->hw queue
  59. * mapping. This is implemented here.
  60. *
  61. * Due to the way hw queues are set up (by the hw specific modules like
  62. * iwl-4965.c), the AC->hw queue mapping is the identity
  63. * mapping.
  64. */
  65. static const u8 tid_to_ac[] = {
  66. IEEE80211_AC_BE,
  67. IEEE80211_AC_BK,
  68. IEEE80211_AC_BK,
  69. IEEE80211_AC_BE,
  70. IEEE80211_AC_VI,
  71. IEEE80211_AC_VI,
  72. IEEE80211_AC_VO,
  73. IEEE80211_AC_VO
  74. };
  75. static inline int il4965_get_ac_from_tid(u16 tid)
  76. {
  77. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  78. return tid_to_ac[tid];
  79. /* no support for TIDs 8-15 yet */
  80. return -EINVAL;
  81. }
  82. static inline int
  83. il4965_get_fifo_from_tid(struct il_rxon_context *ctx, u16 tid)
  84. {
  85. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  86. return ctx->ac_to_fifo[tid_to_ac[tid]];
  87. /* no support for TIDs 8-15 yet */
  88. return -EINVAL;
  89. }
  90. /*
  91. * handle build REPLY_TX command notification.
  92. */
  93. static void il4965_tx_cmd_build_basic(struct il_priv *il,
  94. struct sk_buff *skb,
  95. struct il_tx_cmd *tx_cmd,
  96. struct ieee80211_tx_info *info,
  97. struct ieee80211_hdr *hdr,
  98. u8 std_id)
  99. {
  100. __le16 fc = hdr->frame_control;
  101. __le32 tx_flags = tx_cmd->tx_flags;
  102. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  103. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  104. tx_flags |= TX_CMD_FLG_ACK_MSK;
  105. if (ieee80211_is_mgmt(fc))
  106. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  107. if (ieee80211_is_probe_resp(fc) &&
  108. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  109. tx_flags |= TX_CMD_FLG_TSF_MSK;
  110. } else {
  111. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  112. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  113. }
  114. if (ieee80211_is_back_req(fc))
  115. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  116. tx_cmd->sta_id = std_id;
  117. if (ieee80211_has_morefrags(fc))
  118. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  119. if (ieee80211_is_data_qos(fc)) {
  120. u8 *qc = ieee80211_get_qos_ctl(hdr);
  121. tx_cmd->tid_tspec = qc[0] & 0xf;
  122. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  123. } else {
  124. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  125. }
  126. il_tx_cmd_protection(il, info, fc, &tx_flags);
  127. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  128. if (ieee80211_is_mgmt(fc)) {
  129. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  130. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  131. else
  132. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  133. } else {
  134. tx_cmd->timeout.pm_frame_timeout = 0;
  135. }
  136. tx_cmd->driver_txop = 0;
  137. tx_cmd->tx_flags = tx_flags;
  138. tx_cmd->next_frame_len = 0;
  139. }
  140. #define RTS_DFAULT_RETRY_LIMIT 60
  141. static void il4965_tx_cmd_build_rate(struct il_priv *il,
  142. struct il_tx_cmd *tx_cmd,
  143. struct ieee80211_tx_info *info,
  144. __le16 fc)
  145. {
  146. u32 rate_flags;
  147. int rate_idx;
  148. u8 rts_retry_limit;
  149. u8 data_retry_limit;
  150. u8 rate_plcp;
  151. /* Set retry limit on DATA packets and Probe Responses*/
  152. if (ieee80211_is_probe_resp(fc))
  153. data_retry_limit = 3;
  154. else
  155. data_retry_limit = IL4965_DEFAULT_TX_RETRY;
  156. tx_cmd->data_retry_limit = data_retry_limit;
  157. /* Set retry limit on RTS packets */
  158. rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
  159. if (data_retry_limit < rts_retry_limit)
  160. rts_retry_limit = data_retry_limit;
  161. tx_cmd->rts_retry_limit = rts_retry_limit;
  162. /* DATA packets will use the uCode station table for rate/antenna
  163. * selection */
  164. if (ieee80211_is_data(fc)) {
  165. tx_cmd->initial_rate_index = 0;
  166. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  167. return;
  168. }
  169. /**
  170. * If the current TX rate stored in mac80211 has the MCS bit set, it's
  171. * not really a TX rate. Thus, we use the lowest supported rate for
  172. * this band. Also use the lowest supported rate if the stored rate
  173. * index is invalid.
  174. */
  175. rate_idx = info->control.rates[0].idx;
  176. if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) ||
  177. rate_idx < 0 || rate_idx > IL_RATE_COUNT_LEGACY)
  178. rate_idx = rate_lowest_index(&il->bands[info->band],
  179. info->control.sta);
  180. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  181. if (info->band == IEEE80211_BAND_5GHZ)
  182. rate_idx += IL_FIRST_OFDM_RATE;
  183. /* Get PLCP rate for tx_cmd->rate_n_flags */
  184. rate_plcp = il_rates[rate_idx].plcp;
  185. /* Zero out flags for this packet */
  186. rate_flags = 0;
  187. /* Set CCK flag as needed */
  188. if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
  189. rate_flags |= RATE_MCS_CCK_MSK;
  190. /* Set up antennas */
  191. il->mgmt_tx_ant = il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
  192. il->hw_params.valid_tx_ant);
  193. rate_flags |= il4965_ant_idx_to_flags(il->mgmt_tx_ant);
  194. /* Set the rate in the TX cmd */
  195. tx_cmd->rate_n_flags = il4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
  196. }
  197. static void il4965_tx_cmd_build_hwcrypto(struct il_priv *il,
  198. struct ieee80211_tx_info *info,
  199. struct il_tx_cmd *tx_cmd,
  200. struct sk_buff *skb_frag,
  201. int sta_id)
  202. {
  203. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  204. switch (keyconf->cipher) {
  205. case WLAN_CIPHER_SUITE_CCMP:
  206. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  207. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  208. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  209. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  210. D_TX("tx_cmd with AES hwcrypto\n");
  211. break;
  212. case WLAN_CIPHER_SUITE_TKIP:
  213. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  214. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  215. D_TX("tx_cmd with tkip hwcrypto\n");
  216. break;
  217. case WLAN_CIPHER_SUITE_WEP104:
  218. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  219. /* fall through */
  220. case WLAN_CIPHER_SUITE_WEP40:
  221. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  222. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  223. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  224. D_TX("Configuring packet for WEP encryption "
  225. "with key %d\n", keyconf->keyidx);
  226. break;
  227. default:
  228. IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
  229. break;
  230. }
  231. }
  232. /*
  233. * start REPLY_TX command process
  234. */
  235. int il4965_tx_skb(struct il_priv *il, struct sk_buff *skb)
  236. {
  237. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  238. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  239. struct ieee80211_sta *sta = info->control.sta;
  240. struct il_station_priv *sta_priv = NULL;
  241. struct il_tx_queue *txq;
  242. struct il_queue *q;
  243. struct il_device_cmd *out_cmd;
  244. struct il_cmd_meta *out_meta;
  245. struct il_tx_cmd *tx_cmd;
  246. struct il_rxon_context *ctx = &il->ctx;
  247. int txq_id;
  248. dma_addr_t phys_addr;
  249. dma_addr_t txcmd_phys;
  250. dma_addr_t scratch_phys;
  251. u16 len, firstlen, secondlen;
  252. u16 seq_number = 0;
  253. __le16 fc;
  254. u8 hdr_len;
  255. u8 sta_id;
  256. u8 wait_write_ptr = 0;
  257. u8 tid = 0;
  258. u8 *qc = NULL;
  259. unsigned long flags;
  260. bool is_agg = false;
  261. if (info->control.vif)
  262. ctx = il_rxon_ctx_from_vif(info->control.vif);
  263. spin_lock_irqsave(&il->lock, flags);
  264. if (il_is_rfkill(il)) {
  265. D_DROP("Dropping - RF KILL\n");
  266. goto drop_unlock;
  267. }
  268. fc = hdr->frame_control;
  269. #ifdef CONFIG_IWLEGACY_DEBUG
  270. if (ieee80211_is_auth(fc))
  271. D_TX("Sending AUTH frame\n");
  272. else if (ieee80211_is_assoc_req(fc))
  273. D_TX("Sending ASSOC frame\n");
  274. else if (ieee80211_is_reassoc_req(fc))
  275. D_TX("Sending REASSOC frame\n");
  276. #endif
  277. hdr_len = ieee80211_hdrlen(fc);
  278. /* For management frames use broadcast id to do not break aggregation */
  279. if (!ieee80211_is_data(fc))
  280. sta_id = ctx->bcast_sta_id;
  281. else {
  282. /* Find index into station table for destination station */
  283. sta_id = il_sta_id_or_broadcast(il, ctx, info->control.sta);
  284. if (sta_id == IL_INVALID_STATION) {
  285. D_DROP("Dropping - INVALID STATION: %pM\n",
  286. hdr->addr1);
  287. goto drop_unlock;
  288. }
  289. }
  290. D_TX("station Id %d\n", sta_id);
  291. if (sta)
  292. sta_priv = (void *)sta->drv_priv;
  293. if (sta_priv && sta_priv->asleep &&
  294. (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)) {
  295. /*
  296. * This sends an asynchronous command to the device,
  297. * but we can rely on it being processed before the
  298. * next frame is processed -- and the next frame to
  299. * this station is the one that will consume this
  300. * counter.
  301. * For now set the counter to just 1 since we do not
  302. * support uAPSD yet.
  303. */
  304. il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
  305. }
  306. /*
  307. * Send this frame after DTIM -- there's a special queue
  308. * reserved for this for contexts that support AP mode.
  309. */
  310. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  311. txq_id = ctx->mcast_queue;
  312. /*
  313. * The microcode will clear the more data
  314. * bit in the last frame it transmits.
  315. */
  316. hdr->frame_control |=
  317. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  318. } else
  319. txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
  320. /* irqs already disabled/saved above when locking il->lock */
  321. spin_lock(&il->sta_lock);
  322. if (ieee80211_is_data_qos(fc)) {
  323. qc = ieee80211_get_qos_ctl(hdr);
  324. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  325. if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
  326. spin_unlock(&il->sta_lock);
  327. goto drop_unlock;
  328. }
  329. seq_number = il->stations[sta_id].tid[tid].seq_number;
  330. seq_number &= IEEE80211_SCTL_SEQ;
  331. hdr->seq_ctrl = hdr->seq_ctrl &
  332. cpu_to_le16(IEEE80211_SCTL_FRAG);
  333. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  334. seq_number += 0x10;
  335. /* aggregation is on for this <sta,tid> */
  336. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  337. il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
  338. txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
  339. is_agg = true;
  340. }
  341. }
  342. txq = &il->txq[txq_id];
  343. q = &txq->q;
  344. if (unlikely(il_queue_space(q) < q->high_mark)) {
  345. spin_unlock(&il->sta_lock);
  346. goto drop_unlock;
  347. }
  348. if (ieee80211_is_data_qos(fc)) {
  349. il->stations[sta_id].tid[tid].tfds_in_queue++;
  350. if (!ieee80211_has_morefrags(fc))
  351. il->stations[sta_id].tid[tid].seq_number = seq_number;
  352. }
  353. spin_unlock(&il->sta_lock);
  354. /* Set up driver data for this TFD */
  355. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct il_tx_info));
  356. txq->txb[q->write_ptr].skb = skb;
  357. txq->txb[q->write_ptr].ctx = ctx;
  358. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  359. out_cmd = txq->cmd[q->write_ptr];
  360. out_meta = &txq->meta[q->write_ptr];
  361. tx_cmd = &out_cmd->cmd.tx;
  362. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  363. memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
  364. /*
  365. * Set up the Tx-command (not MAC!) header.
  366. * Store the chosen Tx queue and TFD index within the sequence field;
  367. * after Tx, uCode's Tx response will return this value so driver can
  368. * locate the frame within the tx queue and do post-tx processing.
  369. */
  370. out_cmd->hdr.cmd = REPLY_TX;
  371. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  372. INDEX_TO_SEQ(q->write_ptr)));
  373. /* Copy MAC header from skb into command buffer */
  374. memcpy(tx_cmd->hdr, hdr, hdr_len);
  375. /* Total # bytes to be transmitted */
  376. len = (u16)skb->len;
  377. tx_cmd->len = cpu_to_le16(len);
  378. if (info->control.hw_key)
  379. il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
  380. /* TODO need this for burst mode later on */
  381. il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
  382. il_dbg_log_tx_data_frame(il, len, hdr);
  383. il4965_tx_cmd_build_rate(il, tx_cmd, info, fc);
  384. il_update_stats(il, true, fc, len);
  385. /*
  386. * Use the first empty entry in this queue's command buffer array
  387. * to contain the Tx command and MAC header concatenated together
  388. * (payload data will be in another buffer).
  389. * Size of this varies, due to varying MAC header length.
  390. * If end is not dword aligned, we'll have 2 extra bytes at the end
  391. * of the MAC header (device reads on dword boundaries).
  392. * We'll tell device about this padding later.
  393. */
  394. len = sizeof(struct il_tx_cmd) +
  395. sizeof(struct il_cmd_header) + hdr_len;
  396. firstlen = (len + 3) & ~3;
  397. /* Tell NIC about any 2-byte padding after MAC header */
  398. if (firstlen != len)
  399. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  400. /* Physical address of this Tx command's header (not MAC header!),
  401. * within command buffer array. */
  402. txcmd_phys = pci_map_single(il->pci_dev,
  403. &out_cmd->hdr, firstlen,
  404. PCI_DMA_BIDIRECTIONAL);
  405. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  406. dma_unmap_len_set(out_meta, len, firstlen);
  407. /* Add buffer containing Tx command and MAC(!) header to TFD's
  408. * first entry */
  409. il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq,
  410. txcmd_phys, firstlen, 1, 0);
  411. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  412. txq->need_update = 1;
  413. } else {
  414. wait_write_ptr = 1;
  415. txq->need_update = 0;
  416. }
  417. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  418. * if any (802.11 null frames have no payload). */
  419. secondlen = skb->len - hdr_len;
  420. if (secondlen > 0) {
  421. phys_addr = pci_map_single(il->pci_dev, skb->data + hdr_len,
  422. secondlen, PCI_DMA_TODEVICE);
  423. il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq,
  424. phys_addr, secondlen,
  425. 0, 0);
  426. }
  427. scratch_phys = txcmd_phys + sizeof(struct il_cmd_header) +
  428. offsetof(struct il_tx_cmd, scratch);
  429. /* take back ownership of DMA buffer to enable update */
  430. pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys,
  431. firstlen, PCI_DMA_BIDIRECTIONAL);
  432. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  433. tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
  434. D_TX("sequence nr = 0X%x\n",
  435. le16_to_cpu(out_cmd->hdr.sequence));
  436. D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  437. il_print_hex_dump(il, IL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  438. il_print_hex_dump(il, IL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  439. /* Set up entry for this TFD in Tx byte-count array */
  440. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  441. il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq,
  442. le16_to_cpu(tx_cmd->len));
  443. pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys,
  444. firstlen, PCI_DMA_BIDIRECTIONAL);
  445. /* Tell device the write index *just past* this latest filled TFD */
  446. q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
  447. il_txq_update_write_ptr(il, txq);
  448. spin_unlock_irqrestore(&il->lock, flags);
  449. /*
  450. * At this point the frame is "transmitted" successfully
  451. * and we will get a TX status notification eventually,
  452. * regardless of the value of ret. "ret" only indicates
  453. * whether or not we should update the write pointer.
  454. */
  455. /*
  456. * Avoid atomic ops if it isn't an associated client.
  457. * Also, if this is a packet for aggregation, don't
  458. * increase the counter because the ucode will stop
  459. * aggregation queues when their respective station
  460. * goes to sleep.
  461. */
  462. if (sta_priv && sta_priv->client && !is_agg)
  463. atomic_inc(&sta_priv->pending_frames);
  464. if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
  465. if (wait_write_ptr) {
  466. spin_lock_irqsave(&il->lock, flags);
  467. txq->need_update = 1;
  468. il_txq_update_write_ptr(il, txq);
  469. spin_unlock_irqrestore(&il->lock, flags);
  470. } else {
  471. il_stop_queue(il, txq);
  472. }
  473. }
  474. return 0;
  475. drop_unlock:
  476. spin_unlock_irqrestore(&il->lock, flags);
  477. return -1;
  478. }
  479. static inline int il4965_alloc_dma_ptr(struct il_priv *il,
  480. struct il_dma_ptr *ptr, size_t size)
  481. {
  482. ptr->addr = dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma,
  483. GFP_KERNEL);
  484. if (!ptr->addr)
  485. return -ENOMEM;
  486. ptr->size = size;
  487. return 0;
  488. }
  489. static inline void il4965_free_dma_ptr(struct il_priv *il,
  490. struct il_dma_ptr *ptr)
  491. {
  492. if (unlikely(!ptr->addr))
  493. return;
  494. dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
  495. memset(ptr, 0, sizeof(*ptr));
  496. }
  497. /**
  498. * il4965_hw_txq_ctx_free - Free TXQ Context
  499. *
  500. * Destroy all TX DMA queues and structures
  501. */
  502. void il4965_hw_txq_ctx_free(struct il_priv *il)
  503. {
  504. int txq_id;
  505. /* Tx queues */
  506. if (il->txq) {
  507. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  508. if (txq_id == il->cmd_queue)
  509. il_cmd_queue_free(il);
  510. else
  511. il_tx_queue_free(il, txq_id);
  512. }
  513. il4965_free_dma_ptr(il, &il->kw);
  514. il4965_free_dma_ptr(il, &il->scd_bc_tbls);
  515. /* free tx queue structure */
  516. il_txq_mem(il);
  517. }
  518. /**
  519. * il4965_txq_ctx_alloc - allocate TX queue context
  520. * Allocate all Tx DMA structures and initialize them
  521. *
  522. * @param il
  523. * @return error code
  524. */
  525. int il4965_txq_ctx_alloc(struct il_priv *il)
  526. {
  527. int ret;
  528. int txq_id, slots_num;
  529. unsigned long flags;
  530. /* Free all tx/cmd queues and keep-warm buffer */
  531. il4965_hw_txq_ctx_free(il);
  532. ret = il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
  533. il->hw_params.scd_bc_tbls_size);
  534. if (ret) {
  535. IL_ERR("Scheduler BC Table allocation failed\n");
  536. goto error_bc_tbls;
  537. }
  538. /* Alloc keep-warm buffer */
  539. ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
  540. if (ret) {
  541. IL_ERR("Keep Warm allocation failed\n");
  542. goto error_kw;
  543. }
  544. /* allocate tx queue structure */
  545. ret = il_alloc_txq_mem(il);
  546. if (ret)
  547. goto error;
  548. spin_lock_irqsave(&il->lock, flags);
  549. /* Turn off all Tx DMA fifos */
  550. il4965_txq_set_sched(il, 0);
  551. /* Tell NIC where to find the "keep warm" buffer */
  552. il_wr(il, FH_KW_MEM_ADDR_REG, il->kw.dma >> 4);
  553. spin_unlock_irqrestore(&il->lock, flags);
  554. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  555. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
  556. slots_num = (txq_id == il->cmd_queue) ?
  557. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  558. ret = il_tx_queue_init(il,
  559. &il->txq[txq_id], slots_num,
  560. txq_id);
  561. if (ret) {
  562. IL_ERR("Tx %d queue init failed\n", txq_id);
  563. goto error;
  564. }
  565. }
  566. return ret;
  567. error:
  568. il4965_hw_txq_ctx_free(il);
  569. il4965_free_dma_ptr(il, &il->kw);
  570. error_kw:
  571. il4965_free_dma_ptr(il, &il->scd_bc_tbls);
  572. error_bc_tbls:
  573. return ret;
  574. }
  575. void il4965_txq_ctx_reset(struct il_priv *il)
  576. {
  577. int txq_id, slots_num;
  578. unsigned long flags;
  579. spin_lock_irqsave(&il->lock, flags);
  580. /* Turn off all Tx DMA fifos */
  581. il4965_txq_set_sched(il, 0);
  582. /* Tell NIC where to find the "keep warm" buffer */
  583. il_wr(il, FH_KW_MEM_ADDR_REG, il->kw.dma >> 4);
  584. spin_unlock_irqrestore(&il->lock, flags);
  585. /* Alloc and init all Tx queues, including the command queue (#4) */
  586. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
  587. slots_num = txq_id == il->cmd_queue ?
  588. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  589. il_tx_queue_reset(il, &il->txq[txq_id],
  590. slots_num, txq_id);
  591. }
  592. }
  593. /**
  594. * il4965_txq_ctx_stop - Stop all Tx DMA channels
  595. */
  596. void il4965_txq_ctx_stop(struct il_priv *il)
  597. {
  598. int ch, txq_id;
  599. unsigned long flags;
  600. /* Turn off all Tx DMA fifos */
  601. spin_lock_irqsave(&il->lock, flags);
  602. il4965_txq_set_sched(il, 0);
  603. /* Stop each Tx DMA channel, and wait for it to be idle */
  604. for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
  605. il_wr(il,
  606. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  607. if (il_poll_bit(il, FH_TSSR_TX_STATUS_REG,
  608. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  609. 1000))
  610. IL_ERR("Failing on timeout while stopping"
  611. " DMA channel %d [0x%08x]", ch,
  612. il_rd(il,
  613. FH_TSSR_TX_STATUS_REG));
  614. }
  615. spin_unlock_irqrestore(&il->lock, flags);
  616. if (!il->txq)
  617. return;
  618. /* Unmap DMA from host system and free skb's */
  619. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  620. if (txq_id == il->cmd_queue)
  621. il_cmd_queue_unmap(il);
  622. else
  623. il_tx_queue_unmap(il, txq_id);
  624. }
  625. /*
  626. * Find first available (lowest unused) Tx Queue, mark it "active".
  627. * Called only when finding queue for aggregation.
  628. * Should never return anything < 7, because they should already
  629. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  630. */
  631. static int il4965_txq_ctx_activate_free(struct il_priv *il)
  632. {
  633. int txq_id;
  634. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  635. if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
  636. return txq_id;
  637. return -1;
  638. }
  639. /**
  640. * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
  641. */
  642. static void il4965_tx_queue_stop_scheduler(struct il_priv *il,
  643. u16 txq_id)
  644. {
  645. /* Simply stop the queue, but don't change any configuration;
  646. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  647. il_wr_prph(il,
  648. IL49_SCD_QUEUE_STATUS_BITS(txq_id),
  649. (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  650. (1 << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  651. }
  652. /**
  653. * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
  654. */
  655. static int il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid,
  656. u16 txq_id)
  657. {
  658. u32 tbl_dw_addr;
  659. u32 tbl_dw;
  660. u16 scd_q2ratid;
  661. scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  662. tbl_dw_addr = il->scd_base_addr +
  663. IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  664. tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
  665. if (txq_id & 0x1)
  666. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  667. else
  668. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  669. il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
  670. return 0;
  671. }
  672. /**
  673. * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
  674. *
  675. * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
  676. * i.e. it must be one of the higher queues used for aggregation
  677. */
  678. static int il4965_txq_agg_enable(struct il_priv *il, int txq_id,
  679. int tx_fifo, int sta_id, int tid, u16 ssn_idx)
  680. {
  681. unsigned long flags;
  682. u16 ra_tid;
  683. int ret;
  684. if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
  685. (IL49_FIRST_AMPDU_QUEUE +
  686. il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  687. IL_WARN(
  688. "queue number out of range: %d, must be %d to %d\n",
  689. txq_id, IL49_FIRST_AMPDU_QUEUE,
  690. IL49_FIRST_AMPDU_QUEUE +
  691. il->cfg->base_params->num_of_ampdu_queues - 1);
  692. return -EINVAL;
  693. }
  694. ra_tid = BUILD_RAxTID(sta_id, tid);
  695. /* Modify device's station table to Tx this TID */
  696. ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
  697. if (ret)
  698. return ret;
  699. spin_lock_irqsave(&il->lock, flags);
  700. /* Stop this Tx queue before configuring it */
  701. il4965_tx_queue_stop_scheduler(il, txq_id);
  702. /* Map receiver-address / traffic-ID to this queue */
  703. il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
  704. /* Set this queue as a chain-building queue */
  705. il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  706. /* Place first TFD at index corresponding to start sequence number.
  707. * Assumes that ssn_idx is valid (!= 0xFFF) */
  708. il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  709. il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  710. il4965_set_wr_ptrs(il, txq_id, ssn_idx);
  711. /* Set up Tx window size and frame limit for this queue */
  712. il_write_targ_mem(il,
  713. il->scd_base_addr + IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
  714. (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  715. IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  716. il_write_targ_mem(il, il->scd_base_addr +
  717. IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  718. (SCD_FRAME_LIMIT << IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
  719. & IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  720. il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  721. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  722. il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
  723. spin_unlock_irqrestore(&il->lock, flags);
  724. return 0;
  725. }
  726. int il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
  727. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  728. {
  729. int sta_id;
  730. int tx_fifo;
  731. int txq_id;
  732. int ret;
  733. unsigned long flags;
  734. struct il_tid_data *tid_data;
  735. tx_fifo = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
  736. if (unlikely(tx_fifo < 0))
  737. return tx_fifo;
  738. IL_WARN("%s on ra = %pM tid = %d\n",
  739. __func__, sta->addr, tid);
  740. sta_id = il_sta_id(sta);
  741. if (sta_id == IL_INVALID_STATION) {
  742. IL_ERR("Start AGG on invalid station\n");
  743. return -ENXIO;
  744. }
  745. if (unlikely(tid >= MAX_TID_COUNT))
  746. return -EINVAL;
  747. if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
  748. IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
  749. return -ENXIO;
  750. }
  751. txq_id = il4965_txq_ctx_activate_free(il);
  752. if (txq_id == -1) {
  753. IL_ERR("No free aggregation queue available\n");
  754. return -ENXIO;
  755. }
  756. spin_lock_irqsave(&il->sta_lock, flags);
  757. tid_data = &il->stations[sta_id].tid[tid];
  758. *ssn = SEQ_TO_SN(tid_data->seq_number);
  759. tid_data->agg.txq_id = txq_id;
  760. il_set_swq_id(&il->txq[txq_id],
  761. il4965_get_ac_from_tid(tid), txq_id);
  762. spin_unlock_irqrestore(&il->sta_lock, flags);
  763. ret = il4965_txq_agg_enable(il, txq_id, tx_fifo,
  764. sta_id, tid, *ssn);
  765. if (ret)
  766. return ret;
  767. spin_lock_irqsave(&il->sta_lock, flags);
  768. tid_data = &il->stations[sta_id].tid[tid];
  769. if (tid_data->tfds_in_queue == 0) {
  770. D_HT("HW queue is empty\n");
  771. tid_data->agg.state = IL_AGG_ON;
  772. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  773. } else {
  774. D_HT(
  775. "HW queue is NOT empty: %d packets in HW queue\n",
  776. tid_data->tfds_in_queue);
  777. tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
  778. }
  779. spin_unlock_irqrestore(&il->sta_lock, flags);
  780. return ret;
  781. }
  782. /**
  783. * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
  784. * il->lock must be held by the caller
  785. */
  786. static int il4965_txq_agg_disable(struct il_priv *il, u16 txq_id,
  787. u16 ssn_idx, u8 tx_fifo)
  788. {
  789. if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
  790. (IL49_FIRST_AMPDU_QUEUE +
  791. il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  792. IL_WARN(
  793. "queue number out of range: %d, must be %d to %d\n",
  794. txq_id, IL49_FIRST_AMPDU_QUEUE,
  795. IL49_FIRST_AMPDU_QUEUE +
  796. il->cfg->base_params->num_of_ampdu_queues - 1);
  797. return -EINVAL;
  798. }
  799. il4965_tx_queue_stop_scheduler(il, txq_id);
  800. il_clear_bits_prph(il,
  801. IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  802. il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  803. il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  804. /* supposes that ssn_idx is valid (!= 0xFFF) */
  805. il4965_set_wr_ptrs(il, txq_id, ssn_idx);
  806. il_clear_bits_prph(il,
  807. IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  808. il_txq_ctx_deactivate(il, txq_id);
  809. il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
  810. return 0;
  811. }
  812. int il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
  813. struct ieee80211_sta *sta, u16 tid)
  814. {
  815. int tx_fifo_id, txq_id, sta_id, ssn;
  816. struct il_tid_data *tid_data;
  817. int write_ptr, read_ptr;
  818. unsigned long flags;
  819. tx_fifo_id = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
  820. if (unlikely(tx_fifo_id < 0))
  821. return tx_fifo_id;
  822. sta_id = il_sta_id(sta);
  823. if (sta_id == IL_INVALID_STATION) {
  824. IL_ERR("Invalid station for AGG tid %d\n", tid);
  825. return -ENXIO;
  826. }
  827. spin_lock_irqsave(&il->sta_lock, flags);
  828. tid_data = &il->stations[sta_id].tid[tid];
  829. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  830. txq_id = tid_data->agg.txq_id;
  831. switch (il->stations[sta_id].tid[tid].agg.state) {
  832. case IL_EMPTYING_HW_QUEUE_ADDBA:
  833. /*
  834. * This can happen if the peer stops aggregation
  835. * again before we've had a chance to drain the
  836. * queue we selected previously, i.e. before the
  837. * session was really started completely.
  838. */
  839. D_HT("AGG stop before setup done\n");
  840. goto turn_off;
  841. case IL_AGG_ON:
  842. break;
  843. default:
  844. IL_WARN("Stopping AGG while state not ON or starting\n");
  845. }
  846. write_ptr = il->txq[txq_id].q.write_ptr;
  847. read_ptr = il->txq[txq_id].q.read_ptr;
  848. /* The queue is not empty */
  849. if (write_ptr != read_ptr) {
  850. D_HT("Stopping a non empty AGG HW QUEUE\n");
  851. il->stations[sta_id].tid[tid].agg.state =
  852. IL_EMPTYING_HW_QUEUE_DELBA;
  853. spin_unlock_irqrestore(&il->sta_lock, flags);
  854. return 0;
  855. }
  856. D_HT("HW queue is empty\n");
  857. turn_off:
  858. il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
  859. /* do not restore/save irqs */
  860. spin_unlock(&il->sta_lock);
  861. spin_lock(&il->lock);
  862. /*
  863. * the only reason this call can fail is queue number out of range,
  864. * which can happen if uCode is reloaded and all the station
  865. * information are lost. if it is outside the range, there is no need
  866. * to deactivate the uCode queue, just return "success" to allow
  867. * mac80211 to clean up it own data.
  868. */
  869. il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
  870. spin_unlock_irqrestore(&il->lock, flags);
  871. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  872. return 0;
  873. }
  874. int il4965_txq_check_empty(struct il_priv *il,
  875. int sta_id, u8 tid, int txq_id)
  876. {
  877. struct il_queue *q = &il->txq[txq_id].q;
  878. u8 *addr = il->stations[sta_id].sta.sta.addr;
  879. struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
  880. struct il_rxon_context *ctx;
  881. ctx = &il->ctx;
  882. lockdep_assert_held(&il->sta_lock);
  883. switch (il->stations[sta_id].tid[tid].agg.state) {
  884. case IL_EMPTYING_HW_QUEUE_DELBA:
  885. /* We are reclaiming the last packet of the */
  886. /* aggregated HW queue */
  887. if (txq_id == tid_data->agg.txq_id &&
  888. q->read_ptr == q->write_ptr) {
  889. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  890. int tx_fifo = il4965_get_fifo_from_tid(ctx, tid);
  891. D_HT(
  892. "HW queue empty: continue DELBA flow\n");
  893. il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
  894. tid_data->agg.state = IL_AGG_OFF;
  895. ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  896. }
  897. break;
  898. case IL_EMPTYING_HW_QUEUE_ADDBA:
  899. /* We are reclaiming the last packet of the queue */
  900. if (tid_data->tfds_in_queue == 0) {
  901. D_HT(
  902. "HW queue empty: continue ADDBA flow\n");
  903. tid_data->agg.state = IL_AGG_ON;
  904. ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  905. }
  906. break;
  907. }
  908. return 0;
  909. }
  910. static void il4965_non_agg_tx_status(struct il_priv *il,
  911. struct il_rxon_context *ctx,
  912. const u8 *addr1)
  913. {
  914. struct ieee80211_sta *sta;
  915. struct il_station_priv *sta_priv;
  916. rcu_read_lock();
  917. sta = ieee80211_find_sta(ctx->vif, addr1);
  918. if (sta) {
  919. sta_priv = (void *)sta->drv_priv;
  920. /* avoid atomic ops if this isn't a client */
  921. if (sta_priv->client &&
  922. atomic_dec_return(&sta_priv->pending_frames) == 0)
  923. ieee80211_sta_block_awake(il->hw, sta, false);
  924. }
  925. rcu_read_unlock();
  926. }
  927. static void
  928. il4965_tx_status(struct il_priv *il, struct il_tx_info *tx_info,
  929. bool is_agg)
  930. {
  931. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx_info->skb->data;
  932. if (!is_agg)
  933. il4965_non_agg_tx_status(il, tx_info->ctx, hdr->addr1);
  934. ieee80211_tx_status_irqsafe(il->hw, tx_info->skb);
  935. }
  936. int il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int index)
  937. {
  938. struct il_tx_queue *txq = &il->txq[txq_id];
  939. struct il_queue *q = &txq->q;
  940. struct il_tx_info *tx_info;
  941. int nfreed = 0;
  942. struct ieee80211_hdr *hdr;
  943. if (index >= q->n_bd || il_queue_used(q, index) == 0) {
  944. IL_ERR("Read index for DMA queue txq id (%d), index %d, "
  945. "is out of range [0-%d] %d %d.\n", txq_id,
  946. index, q->n_bd, q->write_ptr, q->read_ptr);
  947. return 0;
  948. }
  949. for (index = il_queue_inc_wrap(index, q->n_bd);
  950. q->read_ptr != index;
  951. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  952. tx_info = &txq->txb[txq->q.read_ptr];
  953. if (WARN_ON_ONCE(tx_info->skb == NULL))
  954. continue;
  955. hdr = (struct ieee80211_hdr *)tx_info->skb->data;
  956. if (ieee80211_is_data_qos(hdr->frame_control))
  957. nfreed++;
  958. il4965_tx_status(il, tx_info,
  959. txq_id >= IL4965_FIRST_AMPDU_QUEUE);
  960. tx_info->skb = NULL;
  961. il->cfg->ops->lib->txq_free_tfd(il, txq);
  962. }
  963. return nfreed;
  964. }
  965. /**
  966. * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
  967. *
  968. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  969. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  970. */
  971. static int il4965_tx_status_reply_compressed_ba(struct il_priv *il,
  972. struct il_ht_agg *agg,
  973. struct il_compressed_ba_resp *ba_resp)
  974. {
  975. int i, sh, ack;
  976. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  977. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  978. int successes = 0;
  979. struct ieee80211_tx_info *info;
  980. u64 bitmap, sent_bitmap;
  981. if (unlikely(!agg->wait_for_ba)) {
  982. if (unlikely(ba_resp->bitmap))
  983. IL_ERR("Received BA when not expected\n");
  984. return -EINVAL;
  985. }
  986. /* Mark that the expected block-ack response arrived */
  987. agg->wait_for_ba = 0;
  988. D_TX_REPLY("BA %d %d\n", agg->start_idx,
  989. ba_resp->seq_ctl);
  990. /* Calculate shift to align block-ack bits with our Tx window bits */
  991. sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl >> 4);
  992. if (sh < 0) /* tbw something is wrong with indices */
  993. sh += 0x100;
  994. if (agg->frame_count > (64 - sh)) {
  995. D_TX_REPLY("more frames than bitmap size");
  996. return -1;
  997. }
  998. /* don't use 64-bit values for now */
  999. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  1000. /* check for success or failure according to the
  1001. * transmitted bitmap and block-ack bitmap */
  1002. sent_bitmap = bitmap & agg->bitmap;
  1003. /* For each frame attempted in aggregation,
  1004. * update driver's record of tx frame's status. */
  1005. i = 0;
  1006. while (sent_bitmap) {
  1007. ack = sent_bitmap & 1ULL;
  1008. successes += ack;
  1009. D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
  1010. ack ? "ACK" : "NACK", i,
  1011. (agg->start_idx + i) & 0xff,
  1012. agg->start_idx + i);
  1013. sent_bitmap >>= 1;
  1014. ++i;
  1015. }
  1016. D_TX_REPLY("Bitmap %llx\n",
  1017. (unsigned long long)bitmap);
  1018. info = IEEE80211_SKB_CB(il->txq[scd_flow].txb[agg->start_idx].skb);
  1019. memset(&info->status, 0, sizeof(info->status));
  1020. info->flags |= IEEE80211_TX_STAT_ACK;
  1021. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1022. info->status.ampdu_ack_len = successes;
  1023. info->status.ampdu_len = agg->frame_count;
  1024. il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
  1025. return 0;
  1026. }
  1027. /**
  1028. * translate ucode response to mac80211 tx status control values
  1029. */
  1030. void il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
  1031. struct ieee80211_tx_info *info)
  1032. {
  1033. struct ieee80211_tx_rate *r = &info->control.rates[0];
  1034. info->antenna_sel_tx =
  1035. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  1036. if (rate_n_flags & RATE_MCS_HT_MSK)
  1037. r->flags |= IEEE80211_TX_RC_MCS;
  1038. if (rate_n_flags & RATE_MCS_GF_MSK)
  1039. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  1040. if (rate_n_flags & RATE_MCS_HT40_MSK)
  1041. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1042. if (rate_n_flags & RATE_MCS_DUP_MSK)
  1043. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  1044. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1045. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  1046. r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  1047. }
  1048. /**
  1049. * il4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  1050. *
  1051. * Handles block-acknowledge notification from device, which reports success
  1052. * of frames sent via aggregation.
  1053. */
  1054. void il4965_rx_reply_compressed_ba(struct il_priv *il,
  1055. struct il_rx_buf *rxb)
  1056. {
  1057. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1058. struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  1059. struct il_tx_queue *txq = NULL;
  1060. struct il_ht_agg *agg;
  1061. int index;
  1062. int sta_id;
  1063. int tid;
  1064. unsigned long flags;
  1065. /* "flow" corresponds to Tx queue */
  1066. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1067. /* "ssn" is start of block-ack Tx window, corresponds to index
  1068. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1069. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  1070. if (scd_flow >= il->hw_params.max_txq_num) {
  1071. IL_ERR(
  1072. "BUG_ON scd_flow is bigger than number of queues\n");
  1073. return;
  1074. }
  1075. txq = &il->txq[scd_flow];
  1076. sta_id = ba_resp->sta_id;
  1077. tid = ba_resp->tid;
  1078. agg = &il->stations[sta_id].tid[tid].agg;
  1079. if (unlikely(agg->txq_id != scd_flow)) {
  1080. /*
  1081. * FIXME: this is a uCode bug which need to be addressed,
  1082. * log the information and return for now!
  1083. * since it is possible happen very often and in order
  1084. * not to fill the syslog, don't enable the logging by default
  1085. */
  1086. D_TX_REPLY(
  1087. "BA scd_flow %d does not match txq_id %d\n",
  1088. scd_flow, agg->txq_id);
  1089. return;
  1090. }
  1091. /* Find index just before block-ack window */
  1092. index = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  1093. spin_lock_irqsave(&il->sta_lock, flags);
  1094. D_TX_REPLY("REPLY_COMPRESSED_BA [%d] Received from %pM, "
  1095. "sta_id = %d\n",
  1096. agg->wait_for_ba,
  1097. (u8 *) &ba_resp->sta_addr_lo32,
  1098. ba_resp->sta_id);
  1099. D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx,"
  1100. "scd_flow = "
  1101. "%d, scd_ssn = %d\n",
  1102. ba_resp->tid,
  1103. ba_resp->seq_ctl,
  1104. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  1105. ba_resp->scd_flow,
  1106. ba_resp->scd_ssn);
  1107. D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n",
  1108. agg->start_idx,
  1109. (unsigned long long)agg->bitmap);
  1110. /* Update driver's record of ACK vs. not for each frame in window */
  1111. il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
  1112. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  1113. * block-ack window (we assume that they've been successfully
  1114. * transmitted ... if not, it's too late anyway). */
  1115. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  1116. /* calculate mac80211 ampdu sw queue to wake */
  1117. int freed = il4965_tx_queue_reclaim(il, scd_flow, index);
  1118. il4965_free_tfds_in_queue(il, sta_id, tid, freed);
  1119. if (il_queue_space(&txq->q) > txq->q.low_mark &&
  1120. il->mac80211_registered &&
  1121. agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
  1122. il_wake_queue(il, txq);
  1123. il4965_txq_check_empty(il, sta_id, tid, scd_flow);
  1124. }
  1125. spin_unlock_irqrestore(&il->sta_lock, flags);
  1126. }
  1127. #ifdef CONFIG_IWLEGACY_DEBUG
  1128. const char *il4965_get_tx_fail_reason(u32 status)
  1129. {
  1130. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  1131. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  1132. switch (status & TX_STATUS_MSK) {
  1133. case TX_STATUS_SUCCESS:
  1134. return "SUCCESS";
  1135. TX_STATUS_POSTPONE(DELAY);
  1136. TX_STATUS_POSTPONE(FEW_BYTES);
  1137. TX_STATUS_POSTPONE(QUIET_PERIOD);
  1138. TX_STATUS_POSTPONE(CALC_TTAK);
  1139. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  1140. TX_STATUS_FAIL(SHORT_LIMIT);
  1141. TX_STATUS_FAIL(LONG_LIMIT);
  1142. TX_STATUS_FAIL(FIFO_UNDERRUN);
  1143. TX_STATUS_FAIL(DRAIN_FLOW);
  1144. TX_STATUS_FAIL(RFKILL_FLUSH);
  1145. TX_STATUS_FAIL(LIFE_EXPIRE);
  1146. TX_STATUS_FAIL(DEST_PS);
  1147. TX_STATUS_FAIL(HOST_ABORTED);
  1148. TX_STATUS_FAIL(BT_RETRY);
  1149. TX_STATUS_FAIL(STA_INVALID);
  1150. TX_STATUS_FAIL(FRAG_DROPPED);
  1151. TX_STATUS_FAIL(TID_DISABLE);
  1152. TX_STATUS_FAIL(FIFO_FLUSHED);
  1153. TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
  1154. TX_STATUS_FAIL(PASSIVE_NO_RX);
  1155. TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
  1156. }
  1157. return "UNKNOWN";
  1158. #undef TX_STATUS_FAIL
  1159. #undef TX_STATUS_POSTPONE
  1160. }
  1161. #endif /* CONFIG_IWLEGACY_DEBUG */