iwl-4965-tx.c 40 KB

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