iwl-4965-tx.c 40 KB

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