iwl-4965-tx.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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 iwl4965_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. iwl4965_get_fifo_from_tid(struct iwl_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 iwl4965_tx_cmd_build_basic(struct iwl_priv *priv,
  94. struct sk_buff *skb,
  95. struct iwl_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. iwl_legacy_tx_cmd_protection(priv, 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 iwl4965_tx_cmd_build_rate(struct iwl_priv *priv,
  142. struct iwl_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 = IWL4965_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 > IWL_RATE_COUNT_LEGACY))
  178. rate_idx = rate_lowest_index(&priv->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 += IWL_FIRST_OFDM_RATE;
  183. /* Get PLCP rate for tx_cmd->rate_n_flags */
  184. rate_plcp = iwl_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 >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  189. rate_flags |= RATE_MCS_CCK_MSK;
  190. /* Set up antennas */
  191. priv->mgmt_tx_ant = iwl4965_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  192. priv->hw_params.valid_tx_ant);
  193. rate_flags |= iwl4965_ant_idx_to_flags(priv->mgmt_tx_ant);
  194. /* Set the rate in the TX cmd */
  195. tx_cmd->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
  196. }
  197. static void iwl4965_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  198. struct ieee80211_tx_info *info,
  199. struct iwl_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. IWL_DEBUG_TX(priv, "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_key(keyconf, skb_frag,
  215. IEEE80211_TKIP_P2_KEY, tx_cmd->key);
  216. IWL_DEBUG_TX(priv, "tx_cmd with tkip hwcrypto\n");
  217. break;
  218. case WLAN_CIPHER_SUITE_WEP104:
  219. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  220. /* fall through */
  221. case WLAN_CIPHER_SUITE_WEP40:
  222. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  223. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  224. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  225. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  226. "with key %d\n", keyconf->keyidx);
  227. break;
  228. default:
  229. IWL_ERR(priv, "Unknown encode cipher %x\n", keyconf->cipher);
  230. break;
  231. }
  232. }
  233. /*
  234. * start REPLY_TX command process
  235. */
  236. int iwl4965_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  237. {
  238. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  239. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  240. struct ieee80211_sta *sta = info->control.sta;
  241. struct iwl_station_priv *sta_priv = NULL;
  242. struct iwl_tx_queue *txq;
  243. struct iwl_queue *q;
  244. struct iwl_device_cmd *out_cmd;
  245. struct iwl_cmd_meta *out_meta;
  246. struct iwl_tx_cmd *tx_cmd;
  247. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  248. int txq_id;
  249. dma_addr_t phys_addr;
  250. dma_addr_t txcmd_phys;
  251. dma_addr_t scratch_phys;
  252. u16 len, firstlen, secondlen;
  253. u16 seq_number = 0;
  254. __le16 fc;
  255. u8 hdr_len;
  256. u8 sta_id;
  257. u8 wait_write_ptr = 0;
  258. u8 tid = 0;
  259. u8 *qc = NULL;
  260. unsigned long flags;
  261. bool is_agg = false;
  262. if (info->control.vif)
  263. ctx = iwl_legacy_rxon_ctx_from_vif(info->control.vif);
  264. spin_lock_irqsave(&priv->lock, flags);
  265. if (iwl_legacy_is_rfkill(priv)) {
  266. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  267. goto drop_unlock;
  268. }
  269. fc = hdr->frame_control;
  270. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  271. if (ieee80211_is_auth(fc))
  272. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  273. else if (ieee80211_is_assoc_req(fc))
  274. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  275. else if (ieee80211_is_reassoc_req(fc))
  276. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  277. #endif
  278. hdr_len = ieee80211_hdrlen(fc);
  279. /* Find index into station table for destination station */
  280. sta_id = iwl_legacy_sta_id_or_broadcast(priv, ctx, info->control.sta);
  281. if (sta_id == IWL_INVALID_STATION) {
  282. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  283. hdr->addr1);
  284. goto drop_unlock;
  285. }
  286. IWL_DEBUG_TX(priv, "station Id %d\n", sta_id);
  287. if (sta)
  288. sta_priv = (void *)sta->drv_priv;
  289. if (sta_priv && sta_priv->asleep &&
  290. (info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE)) {
  291. /*
  292. * This sends an asynchronous command to the device,
  293. * but we can rely on it being processed before the
  294. * next frame is processed -- and the next frame to
  295. * this station is the one that will consume this
  296. * counter.
  297. * For now set the counter to just 1 since we do not
  298. * support uAPSD yet.
  299. */
  300. iwl4965_sta_modify_sleep_tx_count(priv, sta_id, 1);
  301. }
  302. /*
  303. * Send this frame after DTIM -- there's a special queue
  304. * reserved for this for contexts that support AP mode.
  305. */
  306. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  307. txq_id = ctx->mcast_queue;
  308. /*
  309. * The microcode will clear the more data
  310. * bit in the last frame it transmits.
  311. */
  312. hdr->frame_control |=
  313. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  314. } else
  315. txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
  316. /* irqs already disabled/saved above when locking priv->lock */
  317. spin_lock(&priv->sta_lock);
  318. if (ieee80211_is_data_qos(fc)) {
  319. qc = ieee80211_get_qos_ctl(hdr);
  320. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  321. if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
  322. spin_unlock(&priv->sta_lock);
  323. goto drop_unlock;
  324. }
  325. seq_number = priv->stations[sta_id].tid[tid].seq_number;
  326. seq_number &= IEEE80211_SCTL_SEQ;
  327. hdr->seq_ctrl = hdr->seq_ctrl &
  328. cpu_to_le16(IEEE80211_SCTL_FRAG);
  329. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  330. seq_number += 0x10;
  331. /* aggregation is on for this <sta,tid> */
  332. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  333. priv->stations[sta_id].tid[tid].agg.state == IWL_AGG_ON) {
  334. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  335. is_agg = true;
  336. }
  337. }
  338. txq = &priv->txq[txq_id];
  339. q = &txq->q;
  340. if (unlikely(iwl_legacy_queue_space(q) < q->high_mark)) {
  341. spin_unlock(&priv->sta_lock);
  342. goto drop_unlock;
  343. }
  344. if (ieee80211_is_data_qos(fc)) {
  345. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  346. if (!ieee80211_has_morefrags(fc))
  347. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  348. }
  349. spin_unlock(&priv->sta_lock);
  350. /* Set up driver data for this TFD */
  351. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  352. txq->txb[q->write_ptr].skb = skb;
  353. txq->txb[q->write_ptr].ctx = ctx;
  354. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  355. out_cmd = txq->cmd[q->write_ptr];
  356. out_meta = &txq->meta[q->write_ptr];
  357. tx_cmd = &out_cmd->cmd.tx;
  358. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  359. memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
  360. /*
  361. * Set up the Tx-command (not MAC!) header.
  362. * Store the chosen Tx queue and TFD index within the sequence field;
  363. * after Tx, uCode's Tx response will return this value so driver can
  364. * locate the frame within the tx queue and do post-tx processing.
  365. */
  366. out_cmd->hdr.cmd = REPLY_TX;
  367. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  368. INDEX_TO_SEQ(q->write_ptr)));
  369. /* Copy MAC header from skb into command buffer */
  370. memcpy(tx_cmd->hdr, hdr, hdr_len);
  371. /* Total # bytes to be transmitted */
  372. len = (u16)skb->len;
  373. tx_cmd->len = cpu_to_le16(len);
  374. if (info->control.hw_key)
  375. iwl4965_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
  376. /* TODO need this for burst mode later on */
  377. iwl4965_tx_cmd_build_basic(priv, skb, tx_cmd, info, hdr, sta_id);
  378. iwl_legacy_dbg_log_tx_data_frame(priv, len, hdr);
  379. iwl4965_tx_cmd_build_rate(priv, tx_cmd, info, fc);
  380. iwl_legacy_update_stats(priv, true, fc, len);
  381. /*
  382. * Use the first empty entry in this queue's command buffer array
  383. * to contain the Tx command and MAC header concatenated together
  384. * (payload data will be in another buffer).
  385. * Size of this varies, due to varying MAC header length.
  386. * If end is not dword aligned, we'll have 2 extra bytes at the end
  387. * of the MAC header (device reads on dword boundaries).
  388. * We'll tell device about this padding later.
  389. */
  390. len = sizeof(struct iwl_tx_cmd) +
  391. sizeof(struct iwl_cmd_header) + hdr_len;
  392. firstlen = (len + 3) & ~3;
  393. /* Tell NIC about any 2-byte padding after MAC header */
  394. if (firstlen != len)
  395. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  396. /* Physical address of this Tx command's header (not MAC header!),
  397. * within command buffer array. */
  398. txcmd_phys = pci_map_single(priv->pci_dev,
  399. &out_cmd->hdr, firstlen,
  400. PCI_DMA_BIDIRECTIONAL);
  401. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  402. dma_unmap_len_set(out_meta, len, firstlen);
  403. /* Add buffer containing Tx command and MAC(!) header to TFD's
  404. * first entry */
  405. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  406. txcmd_phys, firstlen, 1, 0);
  407. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  408. txq->need_update = 1;
  409. } else {
  410. wait_write_ptr = 1;
  411. txq->need_update = 0;
  412. }
  413. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  414. * if any (802.11 null frames have no payload). */
  415. secondlen = skb->len - hdr_len;
  416. if (secondlen > 0) {
  417. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  418. secondlen, PCI_DMA_TODEVICE);
  419. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  420. phys_addr, secondlen,
  421. 0, 0);
  422. }
  423. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  424. offsetof(struct iwl_tx_cmd, scratch);
  425. /* take back ownership of DMA buffer to enable update */
  426. pci_dma_sync_single_for_cpu(priv->pci_dev, txcmd_phys,
  427. firstlen, PCI_DMA_BIDIRECTIONAL);
  428. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  429. tx_cmd->dram_msb_ptr = iwl_legacy_get_dma_hi_addr(scratch_phys);
  430. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  431. le16_to_cpu(out_cmd->hdr.sequence));
  432. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  433. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  434. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  435. /* Set up entry for this TFD in Tx byte-count array */
  436. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  437. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq,
  438. le16_to_cpu(tx_cmd->len));
  439. pci_dma_sync_single_for_device(priv->pci_dev, txcmd_phys,
  440. firstlen, PCI_DMA_BIDIRECTIONAL);
  441. trace_iwlwifi_legacy_dev_tx(priv,
  442. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  443. sizeof(struct iwl_tfd),
  444. &out_cmd->hdr, firstlen,
  445. skb->data + hdr_len, secondlen);
  446. /* Tell device the write index *just past* this latest filled TFD */
  447. q->write_ptr = iwl_legacy_queue_inc_wrap(q->write_ptr, q->n_bd);
  448. iwl_legacy_txq_update_write_ptr(priv, txq);
  449. spin_unlock_irqrestore(&priv->lock, flags);
  450. /*
  451. * At this point the frame is "transmitted" successfully
  452. * and we will get a TX status notification eventually,
  453. * regardless of the value of ret. "ret" only indicates
  454. * whether or not we should update the write pointer.
  455. */
  456. /*
  457. * Avoid atomic ops if it isn't an associated client.
  458. * Also, if this is a packet for aggregation, don't
  459. * increase the counter because the ucode will stop
  460. * aggregation queues when their respective station
  461. * goes to sleep.
  462. */
  463. if (sta_priv && sta_priv->client && !is_agg)
  464. atomic_inc(&sta_priv->pending_frames);
  465. if ((iwl_legacy_queue_space(q) < q->high_mark) &&
  466. priv->mac80211_registered) {
  467. if (wait_write_ptr) {
  468. spin_lock_irqsave(&priv->lock, flags);
  469. txq->need_update = 1;
  470. iwl_legacy_txq_update_write_ptr(priv, txq);
  471. spin_unlock_irqrestore(&priv->lock, flags);
  472. } else {
  473. iwl_legacy_stop_queue(priv, txq);
  474. }
  475. }
  476. return 0;
  477. drop_unlock:
  478. spin_unlock_irqrestore(&priv->lock, flags);
  479. return -1;
  480. }
  481. static inline int iwl4965_alloc_dma_ptr(struct iwl_priv *priv,
  482. struct iwl_dma_ptr *ptr, size_t size)
  483. {
  484. ptr->addr = dma_alloc_coherent(&priv->pci_dev->dev, size, &ptr->dma,
  485. GFP_KERNEL);
  486. if (!ptr->addr)
  487. return -ENOMEM;
  488. ptr->size = size;
  489. return 0;
  490. }
  491. static inline void iwl4965_free_dma_ptr(struct iwl_priv *priv,
  492. struct iwl_dma_ptr *ptr)
  493. {
  494. if (unlikely(!ptr->addr))
  495. return;
  496. dma_free_coherent(&priv->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
  497. memset(ptr, 0, sizeof(*ptr));
  498. }
  499. /**
  500. * iwl4965_hw_txq_ctx_free - Free TXQ Context
  501. *
  502. * Destroy all TX DMA queues and structures
  503. */
  504. void iwl4965_hw_txq_ctx_free(struct iwl_priv *priv)
  505. {
  506. int txq_id;
  507. /* Tx queues */
  508. if (priv->txq) {
  509. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  510. if (txq_id == priv->cmd_queue)
  511. iwl_legacy_cmd_queue_free(priv);
  512. else
  513. iwl_legacy_tx_queue_free(priv, txq_id);
  514. }
  515. iwl4965_free_dma_ptr(priv, &priv->kw);
  516. iwl4965_free_dma_ptr(priv, &priv->scd_bc_tbls);
  517. /* free tx queue structure */
  518. iwl_legacy_txq_mem(priv);
  519. }
  520. /**
  521. * iwl4965_txq_ctx_alloc - allocate TX queue context
  522. * Allocate all Tx DMA structures and initialize them
  523. *
  524. * @param priv
  525. * @return error code
  526. */
  527. int iwl4965_txq_ctx_alloc(struct iwl_priv *priv)
  528. {
  529. int ret;
  530. int txq_id, slots_num;
  531. unsigned long flags;
  532. /* Free all tx/cmd queues and keep-warm buffer */
  533. iwl4965_hw_txq_ctx_free(priv);
  534. ret = iwl4965_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
  535. priv->hw_params.scd_bc_tbls_size);
  536. if (ret) {
  537. IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
  538. goto error_bc_tbls;
  539. }
  540. /* Alloc keep-warm buffer */
  541. ret = iwl4965_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
  542. if (ret) {
  543. IWL_ERR(priv, "Keep Warm allocation failed\n");
  544. goto error_kw;
  545. }
  546. /* allocate tx queue structure */
  547. ret = iwl_legacy_alloc_txq_mem(priv);
  548. if (ret)
  549. goto error;
  550. spin_lock_irqsave(&priv->lock, flags);
  551. /* Turn off all Tx DMA fifos */
  552. iwl4965_txq_set_sched(priv, 0);
  553. /* Tell NIC where to find the "keep warm" buffer */
  554. iwl_legacy_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  555. spin_unlock_irqrestore(&priv->lock, flags);
  556. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  557. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  558. slots_num = (txq_id == priv->cmd_queue) ?
  559. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  560. ret = iwl_legacy_tx_queue_init(priv,
  561. &priv->txq[txq_id], slots_num,
  562. txq_id);
  563. if (ret) {
  564. IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
  565. goto error;
  566. }
  567. }
  568. return ret;
  569. error:
  570. iwl4965_hw_txq_ctx_free(priv);
  571. iwl4965_free_dma_ptr(priv, &priv->kw);
  572. error_kw:
  573. iwl4965_free_dma_ptr(priv, &priv->scd_bc_tbls);
  574. error_bc_tbls:
  575. return ret;
  576. }
  577. void iwl4965_txq_ctx_reset(struct iwl_priv *priv)
  578. {
  579. int txq_id, slots_num;
  580. unsigned long flags;
  581. spin_lock_irqsave(&priv->lock, flags);
  582. /* Turn off all Tx DMA fifos */
  583. iwl4965_txq_set_sched(priv, 0);
  584. /* Tell NIC where to find the "keep warm" buffer */
  585. iwl_legacy_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  586. spin_unlock_irqrestore(&priv->lock, flags);
  587. /* Alloc and init all Tx queues, including the command queue (#4) */
  588. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  589. slots_num = txq_id == priv->cmd_queue ?
  590. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  591. iwl_legacy_tx_queue_reset(priv, &priv->txq[txq_id],
  592. slots_num, txq_id);
  593. }
  594. }
  595. /**
  596. * iwl4965_txq_ctx_stop - Stop all Tx DMA channels
  597. */
  598. void iwl4965_txq_ctx_stop(struct iwl_priv *priv)
  599. {
  600. int ch;
  601. unsigned long flags;
  602. /* Turn off all Tx DMA fifos */
  603. spin_lock_irqsave(&priv->lock, flags);
  604. iwl4965_txq_set_sched(priv, 0);
  605. /* Stop each Tx DMA channel, and wait for it to be idle */
  606. for (ch = 0; ch < priv->hw_params.dma_chnl_num; ch++) {
  607. iwl_legacy_write_direct32(priv,
  608. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  609. if (iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  610. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  611. 1000))
  612. IWL_ERR(priv, "Failing on timeout while stopping"
  613. " DMA channel %d [0x%08x]", ch,
  614. iwl_legacy_read_direct32(priv,
  615. FH_TSSR_TX_STATUS_REG));
  616. }
  617. spin_unlock_irqrestore(&priv->lock, flags);
  618. }
  619. /*
  620. * Find first available (lowest unused) Tx Queue, mark it "active".
  621. * Called only when finding queue for aggregation.
  622. * Should never return anything < 7, because they should already
  623. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  624. */
  625. static int iwl4965_txq_ctx_activate_free(struct iwl_priv *priv)
  626. {
  627. int txq_id;
  628. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  629. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  630. return txq_id;
  631. return -1;
  632. }
  633. /**
  634. * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
  635. */
  636. static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
  637. u16 txq_id)
  638. {
  639. /* Simply stop the queue, but don't change any configuration;
  640. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  641. iwl_legacy_write_prph(priv,
  642. IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
  643. (0 << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  644. (1 << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  645. }
  646. /**
  647. * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
  648. */
  649. static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
  650. u16 txq_id)
  651. {
  652. u32 tbl_dw_addr;
  653. u32 tbl_dw;
  654. u16 scd_q2ratid;
  655. scd_q2ratid = ra_tid & IWL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  656. tbl_dw_addr = priv->scd_base_addr +
  657. IWL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  658. tbl_dw = iwl_legacy_read_targ_mem(priv, tbl_dw_addr);
  659. if (txq_id & 0x1)
  660. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  661. else
  662. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  663. iwl_legacy_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
  664. return 0;
  665. }
  666. /**
  667. * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
  668. *
  669. * NOTE: txq_id must be greater than IWL49_FIRST_AMPDU_QUEUE,
  670. * i.e. it must be one of the higher queues used for aggregation
  671. */
  672. static int iwl4965_txq_agg_enable(struct iwl_priv *priv, int txq_id,
  673. int tx_fifo, int sta_id, int tid, u16 ssn_idx)
  674. {
  675. unsigned long flags;
  676. u16 ra_tid;
  677. int ret;
  678. if ((IWL49_FIRST_AMPDU_QUEUE > txq_id) ||
  679. (IWL49_FIRST_AMPDU_QUEUE +
  680. priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  681. IWL_WARN(priv,
  682. "queue number out of range: %d, must be %d to %d\n",
  683. txq_id, IWL49_FIRST_AMPDU_QUEUE,
  684. IWL49_FIRST_AMPDU_QUEUE +
  685. priv->cfg->base_params->num_of_ampdu_queues - 1);
  686. return -EINVAL;
  687. }
  688. ra_tid = BUILD_RAxTID(sta_id, tid);
  689. /* Modify device's station table to Tx this TID */
  690. ret = iwl4965_sta_tx_modify_enable_tid(priv, sta_id, tid);
  691. if (ret)
  692. return ret;
  693. spin_lock_irqsave(&priv->lock, flags);
  694. /* Stop this Tx queue before configuring it */
  695. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  696. /* Map receiver-address / traffic-ID to this queue */
  697. iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
  698. /* Set this queue as a chain-building queue */
  699. iwl_legacy_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  700. /* Place first TFD at index corresponding to start sequence number.
  701. * Assumes that ssn_idx is valid (!= 0xFFF) */
  702. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  703. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  704. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  705. /* Set up Tx window size and frame limit for this queue */
  706. iwl_legacy_write_targ_mem(priv,
  707. priv->scd_base_addr + IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
  708. (SCD_WIN_SIZE << IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  709. IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  710. iwl_legacy_write_targ_mem(priv, priv->scd_base_addr +
  711. IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  712. (SCD_FRAME_LIMIT << IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
  713. & IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  714. iwl_legacy_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  715. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  716. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
  717. spin_unlock_irqrestore(&priv->lock, flags);
  718. return 0;
  719. }
  720. int iwl4965_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
  721. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  722. {
  723. int sta_id;
  724. int tx_fifo;
  725. int txq_id;
  726. int ret;
  727. unsigned long flags;
  728. struct iwl_tid_data *tid_data;
  729. tx_fifo = iwl4965_get_fifo_from_tid(iwl_legacy_rxon_ctx_from_vif(vif), tid);
  730. if (unlikely(tx_fifo < 0))
  731. return tx_fifo;
  732. IWL_WARN(priv, "%s on ra = %pM tid = %d\n",
  733. __func__, sta->addr, tid);
  734. sta_id = iwl_legacy_sta_id(sta);
  735. if (sta_id == IWL_INVALID_STATION) {
  736. IWL_ERR(priv, "Start AGG on invalid station\n");
  737. return -ENXIO;
  738. }
  739. if (unlikely(tid >= MAX_TID_COUNT))
  740. return -EINVAL;
  741. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
  742. IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
  743. return -ENXIO;
  744. }
  745. txq_id = iwl4965_txq_ctx_activate_free(priv);
  746. if (txq_id == -1) {
  747. IWL_ERR(priv, "No free aggregation queue available\n");
  748. return -ENXIO;
  749. }
  750. spin_lock_irqsave(&priv->sta_lock, flags);
  751. tid_data = &priv->stations[sta_id].tid[tid];
  752. *ssn = SEQ_TO_SN(tid_data->seq_number);
  753. tid_data->agg.txq_id = txq_id;
  754. iwl_legacy_set_swq_id(&priv->txq[txq_id],
  755. iwl4965_get_ac_from_tid(tid), txq_id);
  756. spin_unlock_irqrestore(&priv->sta_lock, flags);
  757. ret = iwl4965_txq_agg_enable(priv, txq_id, tx_fifo,
  758. sta_id, tid, *ssn);
  759. if (ret)
  760. return ret;
  761. spin_lock_irqsave(&priv->sta_lock, flags);
  762. tid_data = &priv->stations[sta_id].tid[tid];
  763. if (tid_data->tfds_in_queue == 0) {
  764. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  765. tid_data->agg.state = IWL_AGG_ON;
  766. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  767. } else {
  768. IWL_DEBUG_HT(priv,
  769. "HW queue is NOT empty: %d packets in HW queue\n",
  770. tid_data->tfds_in_queue);
  771. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  772. }
  773. spin_unlock_irqrestore(&priv->sta_lock, flags);
  774. return ret;
  775. }
  776. /**
  777. * txq_id must be greater than IWL49_FIRST_AMPDU_QUEUE
  778. * priv->lock must be held by the caller
  779. */
  780. static int iwl4965_txq_agg_disable(struct iwl_priv *priv, u16 txq_id,
  781. u16 ssn_idx, u8 tx_fifo)
  782. {
  783. if ((IWL49_FIRST_AMPDU_QUEUE > txq_id) ||
  784. (IWL49_FIRST_AMPDU_QUEUE +
  785. priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  786. IWL_WARN(priv,
  787. "queue number out of range: %d, must be %d to %d\n",
  788. txq_id, IWL49_FIRST_AMPDU_QUEUE,
  789. IWL49_FIRST_AMPDU_QUEUE +
  790. priv->cfg->base_params->num_of_ampdu_queues - 1);
  791. return -EINVAL;
  792. }
  793. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  794. iwl_legacy_clear_bits_prph(priv,
  795. IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  796. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  797. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  798. /* supposes that ssn_idx is valid (!= 0xFFF) */
  799. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  800. iwl_legacy_clear_bits_prph(priv,
  801. IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  802. iwl_txq_ctx_deactivate(priv, txq_id);
  803. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
  804. return 0;
  805. }
  806. int iwl4965_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
  807. struct ieee80211_sta *sta, u16 tid)
  808. {
  809. int tx_fifo_id, txq_id, sta_id, ssn;
  810. struct iwl_tid_data *tid_data;
  811. int write_ptr, read_ptr;
  812. unsigned long flags;
  813. tx_fifo_id = iwl4965_get_fifo_from_tid(iwl_legacy_rxon_ctx_from_vif(vif), tid);
  814. if (unlikely(tx_fifo_id < 0))
  815. return tx_fifo_id;
  816. sta_id = iwl_legacy_sta_id(sta);
  817. if (sta_id == IWL_INVALID_STATION) {
  818. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  819. return -ENXIO;
  820. }
  821. spin_lock_irqsave(&priv->sta_lock, flags);
  822. tid_data = &priv->stations[sta_id].tid[tid];
  823. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  824. txq_id = tid_data->agg.txq_id;
  825. switch (priv->stations[sta_id].tid[tid].agg.state) {
  826. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  827. /*
  828. * This can happen if the peer stops aggregation
  829. * again before we've had a chance to drain the
  830. * queue we selected previously, i.e. before the
  831. * session was really started completely.
  832. */
  833. IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
  834. goto turn_off;
  835. case IWL_AGG_ON:
  836. break;
  837. default:
  838. IWL_WARN(priv, "Stopping AGG while state not ON or starting\n");
  839. }
  840. write_ptr = priv->txq[txq_id].q.write_ptr;
  841. read_ptr = priv->txq[txq_id].q.read_ptr;
  842. /* The queue is not empty */
  843. if (write_ptr != read_ptr) {
  844. IWL_DEBUG_HT(priv, "Stopping a non empty AGG HW QUEUE\n");
  845. priv->stations[sta_id].tid[tid].agg.state =
  846. IWL_EMPTYING_HW_QUEUE_DELBA;
  847. spin_unlock_irqrestore(&priv->sta_lock, flags);
  848. return 0;
  849. }
  850. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  851. turn_off:
  852. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  853. /* do not restore/save irqs */
  854. spin_unlock(&priv->sta_lock);
  855. spin_lock(&priv->lock);
  856. /*
  857. * the only reason this call can fail is queue number out of range,
  858. * which can happen if uCode is reloaded and all the station
  859. * information are lost. if it is outside the range, there is no need
  860. * to deactivate the uCode queue, just return "success" to allow
  861. * mac80211 to clean up it own data.
  862. */
  863. iwl4965_txq_agg_disable(priv, txq_id, ssn, tx_fifo_id);
  864. spin_unlock_irqrestore(&priv->lock, flags);
  865. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  866. return 0;
  867. }
  868. int iwl4965_txq_check_empty(struct iwl_priv *priv,
  869. int sta_id, u8 tid, int txq_id)
  870. {
  871. struct iwl_queue *q = &priv->txq[txq_id].q;
  872. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  873. struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
  874. struct iwl_rxon_context *ctx;
  875. ctx = &priv->contexts[priv->stations[sta_id].ctxid];
  876. lockdep_assert_held(&priv->sta_lock);
  877. switch (priv->stations[sta_id].tid[tid].agg.state) {
  878. case IWL_EMPTYING_HW_QUEUE_DELBA:
  879. /* We are reclaiming the last packet of the */
  880. /* aggregated HW queue */
  881. if ((txq_id == tid_data->agg.txq_id) &&
  882. (q->read_ptr == q->write_ptr)) {
  883. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  884. int tx_fifo = iwl4965_get_fifo_from_tid(ctx, tid);
  885. IWL_DEBUG_HT(priv,
  886. "HW queue empty: continue DELBA flow\n");
  887. iwl4965_txq_agg_disable(priv, txq_id, ssn, tx_fifo);
  888. tid_data->agg.state = IWL_AGG_OFF;
  889. ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  890. }
  891. break;
  892. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  893. /* We are reclaiming the last packet of the queue */
  894. if (tid_data->tfds_in_queue == 0) {
  895. IWL_DEBUG_HT(priv,
  896. "HW queue empty: continue ADDBA flow\n");
  897. tid_data->agg.state = IWL_AGG_ON;
  898. ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  899. }
  900. break;
  901. }
  902. return 0;
  903. }
  904. static void iwl4965_non_agg_tx_status(struct iwl_priv *priv,
  905. struct iwl_rxon_context *ctx,
  906. const u8 *addr1)
  907. {
  908. struct ieee80211_sta *sta;
  909. struct iwl_station_priv *sta_priv;
  910. rcu_read_lock();
  911. sta = ieee80211_find_sta(ctx->vif, addr1);
  912. if (sta) {
  913. sta_priv = (void *)sta->drv_priv;
  914. /* avoid atomic ops if this isn't a client */
  915. if (sta_priv->client &&
  916. atomic_dec_return(&sta_priv->pending_frames) == 0)
  917. ieee80211_sta_block_awake(priv->hw, sta, false);
  918. }
  919. rcu_read_unlock();
  920. }
  921. static void
  922. iwl4965_tx_status(struct iwl_priv *priv, struct iwl_tx_info *tx_info,
  923. bool is_agg)
  924. {
  925. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx_info->skb->data;
  926. if (!is_agg)
  927. iwl4965_non_agg_tx_status(priv, tx_info->ctx, hdr->addr1);
  928. ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb);
  929. }
  930. int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  931. {
  932. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  933. struct iwl_queue *q = &txq->q;
  934. struct iwl_tx_info *tx_info;
  935. int nfreed = 0;
  936. struct ieee80211_hdr *hdr;
  937. if ((index >= q->n_bd) || (iwl_legacy_queue_used(q, index) == 0)) {
  938. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  939. "is out of range [0-%d] %d %d.\n", txq_id,
  940. index, q->n_bd, q->write_ptr, q->read_ptr);
  941. return 0;
  942. }
  943. for (index = iwl_legacy_queue_inc_wrap(index, q->n_bd);
  944. q->read_ptr != index;
  945. q->read_ptr = iwl_legacy_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  946. tx_info = &txq->txb[txq->q.read_ptr];
  947. iwl4965_tx_status(priv, tx_info,
  948. txq_id >= IWL4965_FIRST_AMPDU_QUEUE);
  949. hdr = (struct ieee80211_hdr *)tx_info->skb->data;
  950. if (hdr && ieee80211_is_data_qos(hdr->frame_control))
  951. nfreed++;
  952. tx_info->skb = NULL;
  953. priv->cfg->ops->lib->txq_free_tfd(priv, txq);
  954. }
  955. return nfreed;
  956. }
  957. /**
  958. * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
  959. *
  960. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  961. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  962. */
  963. static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
  964. struct iwl_ht_agg *agg,
  965. struct iwl_compressed_ba_resp *ba_resp)
  966. {
  967. int i, sh, ack;
  968. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  969. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  970. int successes = 0;
  971. struct ieee80211_tx_info *info;
  972. u64 bitmap, sent_bitmap;
  973. if (unlikely(!agg->wait_for_ba)) {
  974. if (unlikely(ba_resp->bitmap))
  975. IWL_ERR(priv, "Received BA when not expected\n");
  976. return -EINVAL;
  977. }
  978. /* Mark that the expected block-ack response arrived */
  979. agg->wait_for_ba = 0;
  980. IWL_DEBUG_TX_REPLY(priv, "BA %d %d\n", agg->start_idx,
  981. ba_resp->seq_ctl);
  982. /* Calculate shift to align block-ack bits with our Tx window bits */
  983. sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl >> 4);
  984. if (sh < 0) /* tbw something is wrong with indices */
  985. sh += 0x100;
  986. if (agg->frame_count > (64 - sh)) {
  987. IWL_DEBUG_TX_REPLY(priv, "more frames than bitmap size");
  988. return -1;
  989. }
  990. /* don't use 64-bit values for now */
  991. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  992. /* check for success or failure according to the
  993. * transmitted bitmap and block-ack bitmap */
  994. sent_bitmap = bitmap & agg->bitmap;
  995. /* For each frame attempted in aggregation,
  996. * update driver's record of tx frame's status. */
  997. i = 0;
  998. while (sent_bitmap) {
  999. ack = sent_bitmap & 1ULL;
  1000. successes += ack;
  1001. IWL_DEBUG_TX_REPLY(priv, "%s ON i=%d idx=%d raw=%d\n",
  1002. ack ? "ACK" : "NACK", i,
  1003. (agg->start_idx + i) & 0xff,
  1004. agg->start_idx + i);
  1005. sent_bitmap >>= 1;
  1006. ++i;
  1007. }
  1008. IWL_DEBUG_TX_REPLY(priv, "Bitmap %llx\n",
  1009. (unsigned long long)bitmap);
  1010. info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb);
  1011. memset(&info->status, 0, sizeof(info->status));
  1012. info->flags |= IEEE80211_TX_STAT_ACK;
  1013. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1014. info->status.ampdu_ack_len = successes;
  1015. info->status.ampdu_len = agg->frame_count;
  1016. iwl4965_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
  1017. return 0;
  1018. }
  1019. /**
  1020. * translate ucode response to mac80211 tx status control values
  1021. */
  1022. void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
  1023. struct ieee80211_tx_info *info)
  1024. {
  1025. struct ieee80211_tx_rate *r = &info->control.rates[0];
  1026. info->antenna_sel_tx =
  1027. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  1028. if (rate_n_flags & RATE_MCS_HT_MSK)
  1029. r->flags |= IEEE80211_TX_RC_MCS;
  1030. if (rate_n_flags & RATE_MCS_GF_MSK)
  1031. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  1032. if (rate_n_flags & RATE_MCS_HT40_MSK)
  1033. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1034. if (rate_n_flags & RATE_MCS_DUP_MSK)
  1035. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  1036. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1037. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  1038. r->idx = iwl4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  1039. }
  1040. /**
  1041. * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  1042. *
  1043. * Handles block-acknowledge notification from device, which reports success
  1044. * of frames sent via aggregation.
  1045. */
  1046. void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
  1047. struct iwl_rx_mem_buffer *rxb)
  1048. {
  1049. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1050. struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  1051. struct iwl_tx_queue *txq = NULL;
  1052. struct iwl_ht_agg *agg;
  1053. int index;
  1054. int sta_id;
  1055. int tid;
  1056. unsigned long flags;
  1057. /* "flow" corresponds to Tx queue */
  1058. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1059. /* "ssn" is start of block-ack Tx window, corresponds to index
  1060. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1061. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  1062. if (scd_flow >= priv->hw_params.max_txq_num) {
  1063. IWL_ERR(priv,
  1064. "BUG_ON scd_flow is bigger than number of queues\n");
  1065. return;
  1066. }
  1067. txq = &priv->txq[scd_flow];
  1068. sta_id = ba_resp->sta_id;
  1069. tid = ba_resp->tid;
  1070. agg = &priv->stations[sta_id].tid[tid].agg;
  1071. if (unlikely(agg->txq_id != scd_flow)) {
  1072. /*
  1073. * FIXME: this is a uCode bug which need to be addressed,
  1074. * log the information and return for now!
  1075. * since it is possible happen very often and in order
  1076. * not to fill the syslog, don't enable the logging by default
  1077. */
  1078. IWL_DEBUG_TX_REPLY(priv,
  1079. "BA scd_flow %d does not match txq_id %d\n",
  1080. scd_flow, agg->txq_id);
  1081. return;
  1082. }
  1083. /* Find index just before block-ack window */
  1084. index = iwl_legacy_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  1085. spin_lock_irqsave(&priv->sta_lock, flags);
  1086. IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
  1087. "sta_id = %d\n",
  1088. agg->wait_for_ba,
  1089. (u8 *) &ba_resp->sta_addr_lo32,
  1090. ba_resp->sta_id);
  1091. IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx,"
  1092. "scd_flow = "
  1093. "%d, scd_ssn = %d\n",
  1094. ba_resp->tid,
  1095. ba_resp->seq_ctl,
  1096. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  1097. ba_resp->scd_flow,
  1098. ba_resp->scd_ssn);
  1099. IWL_DEBUG_TX_REPLY(priv, "DAT start_idx = %d, bitmap = 0x%llx\n",
  1100. agg->start_idx,
  1101. (unsigned long long)agg->bitmap);
  1102. /* Update driver's record of ACK vs. not for each frame in window */
  1103. iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  1104. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  1105. * block-ack window (we assume that they've been successfully
  1106. * transmitted ... if not, it's too late anyway). */
  1107. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  1108. /* calculate mac80211 ampdu sw queue to wake */
  1109. int freed = iwl4965_tx_queue_reclaim(priv, scd_flow, index);
  1110. iwl4965_free_tfds_in_queue(priv, sta_id, tid, freed);
  1111. if ((iwl_legacy_queue_space(&txq->q) > txq->q.low_mark) &&
  1112. priv->mac80211_registered &&
  1113. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
  1114. iwl_legacy_wake_queue(priv, txq);
  1115. iwl4965_txq_check_empty(priv, sta_id, tid, scd_flow);
  1116. }
  1117. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1118. }
  1119. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1120. const char *iwl4965_get_tx_fail_reason(u32 status)
  1121. {
  1122. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  1123. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  1124. switch (status & TX_STATUS_MSK) {
  1125. case TX_STATUS_SUCCESS:
  1126. return "SUCCESS";
  1127. TX_STATUS_POSTPONE(DELAY);
  1128. TX_STATUS_POSTPONE(FEW_BYTES);
  1129. TX_STATUS_POSTPONE(QUIET_PERIOD);
  1130. TX_STATUS_POSTPONE(CALC_TTAK);
  1131. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  1132. TX_STATUS_FAIL(SHORT_LIMIT);
  1133. TX_STATUS_FAIL(LONG_LIMIT);
  1134. TX_STATUS_FAIL(FIFO_UNDERRUN);
  1135. TX_STATUS_FAIL(DRAIN_FLOW);
  1136. TX_STATUS_FAIL(RFKILL_FLUSH);
  1137. TX_STATUS_FAIL(LIFE_EXPIRE);
  1138. TX_STATUS_FAIL(DEST_PS);
  1139. TX_STATUS_FAIL(HOST_ABORTED);
  1140. TX_STATUS_FAIL(BT_RETRY);
  1141. TX_STATUS_FAIL(STA_INVALID);
  1142. TX_STATUS_FAIL(FRAG_DROPPED);
  1143. TX_STATUS_FAIL(TID_DISABLE);
  1144. TX_STATUS_FAIL(FIFO_FLUSHED);
  1145. TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
  1146. TX_STATUS_FAIL(PASSIVE_NO_RX);
  1147. TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
  1148. }
  1149. return "UNKNOWN";
  1150. #undef TX_STATUS_FAIL
  1151. #undef TX_STATUS_POSTPONE
  1152. }
  1153. #endif /* CONFIG_IWLWIFI_LEGACY_DEBUG */