iwl-agn-tx.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2010 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-agn-hw.h"
  39. #include "iwl-agn.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, iwl-5000.c etc.), 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 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 get_fifo_from_tid(struct iwl_rxon_context *ctx, u16 tid)
  83. {
  84. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  85. return ctx->ac_to_fifo[tid_to_ac[tid]];
  86. /* no support for TIDs 8-15 yet */
  87. return -EINVAL;
  88. }
  89. /**
  90. * iwlagn_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  91. */
  92. void iwlagn_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
  93. struct iwl_tx_queue *txq,
  94. u16 byte_cnt)
  95. {
  96. struct iwlagn_scd_bc_tbl *scd_bc_tbl = priv->scd_bc_tbls.addr;
  97. int write_ptr = txq->q.write_ptr;
  98. int txq_id = txq->q.id;
  99. u8 sec_ctl = 0;
  100. u8 sta_id = 0;
  101. u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  102. __le16 bc_ent;
  103. WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
  104. if (txq_id != priv->cmd_queue) {
  105. sta_id = txq->cmd[txq->q.write_ptr]->cmd.tx.sta_id;
  106. sec_ctl = txq->cmd[txq->q.write_ptr]->cmd.tx.sec_ctl;
  107. switch (sec_ctl & TX_CMD_SEC_MSK) {
  108. case TX_CMD_SEC_CCM:
  109. len += CCMP_MIC_LEN;
  110. break;
  111. case TX_CMD_SEC_TKIP:
  112. len += TKIP_ICV_LEN;
  113. break;
  114. case TX_CMD_SEC_WEP:
  115. len += WEP_IV_LEN + WEP_ICV_LEN;
  116. break;
  117. }
  118. }
  119. bc_ent = cpu_to_le16((len & 0xFFF) | (sta_id << 12));
  120. scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
  121. if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
  122. scd_bc_tbl[txq_id].
  123. tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
  124. }
  125. void iwlagn_txq_inval_byte_cnt_tbl(struct iwl_priv *priv,
  126. struct iwl_tx_queue *txq)
  127. {
  128. struct iwlagn_scd_bc_tbl *scd_bc_tbl = priv->scd_bc_tbls.addr;
  129. int txq_id = txq->q.id;
  130. int read_ptr = txq->q.read_ptr;
  131. u8 sta_id = 0;
  132. __le16 bc_ent;
  133. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  134. if (txq_id != priv->cmd_queue)
  135. sta_id = txq->cmd[read_ptr]->cmd.tx.sta_id;
  136. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  137. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  138. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  139. scd_bc_tbl[txq_id].
  140. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  141. }
  142. static int iwlagn_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
  143. u16 txq_id)
  144. {
  145. u32 tbl_dw_addr;
  146. u32 tbl_dw;
  147. u16 scd_q2ratid;
  148. scd_q2ratid = ra_tid & IWL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  149. tbl_dw_addr = priv->scd_base_addr +
  150. IWLAGN_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  151. tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
  152. if (txq_id & 0x1)
  153. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  154. else
  155. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  156. iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
  157. return 0;
  158. }
  159. static void iwlagn_tx_queue_stop_scheduler(struct iwl_priv *priv, u16 txq_id)
  160. {
  161. /* Simply stop the queue, but don't change any configuration;
  162. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  163. iwl_write_prph(priv,
  164. IWLAGN_SCD_QUEUE_STATUS_BITS(txq_id),
  165. (0 << IWLAGN_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  166. (1 << IWLAGN_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  167. }
  168. void iwlagn_set_wr_ptrs(struct iwl_priv *priv,
  169. int txq_id, u32 index)
  170. {
  171. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  172. (index & 0xff) | (txq_id << 8));
  173. iwl_write_prph(priv, IWLAGN_SCD_QUEUE_RDPTR(txq_id), index);
  174. }
  175. void iwlagn_tx_queue_set_status(struct iwl_priv *priv,
  176. struct iwl_tx_queue *txq,
  177. int tx_fifo_id, int scd_retry)
  178. {
  179. int txq_id = txq->q.id;
  180. int active = test_bit(txq_id, &priv->txq_ctx_active_msk) ? 1 : 0;
  181. iwl_write_prph(priv, IWLAGN_SCD_QUEUE_STATUS_BITS(txq_id),
  182. (active << IWLAGN_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  183. (tx_fifo_id << IWLAGN_SCD_QUEUE_STTS_REG_POS_TXF) |
  184. (1 << IWLAGN_SCD_QUEUE_STTS_REG_POS_WSL) |
  185. IWLAGN_SCD_QUEUE_STTS_REG_MSK);
  186. txq->sched_retry = scd_retry;
  187. IWL_DEBUG_INFO(priv, "%s %s Queue %d on FIFO %d\n",
  188. active ? "Activate" : "Deactivate",
  189. scd_retry ? "BA" : "AC/CMD", txq_id, tx_fifo_id);
  190. }
  191. int iwlagn_txq_agg_enable(struct iwl_priv *priv, int txq_id,
  192. int tx_fifo, int sta_id, int tid, u16 ssn_idx)
  193. {
  194. unsigned long flags;
  195. u16 ra_tid;
  196. int ret;
  197. if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) ||
  198. (IWLAGN_FIRST_AMPDU_QUEUE +
  199. priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  200. IWL_WARN(priv,
  201. "queue number out of range: %d, must be %d to %d\n",
  202. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  203. IWLAGN_FIRST_AMPDU_QUEUE +
  204. priv->cfg->base_params->num_of_ampdu_queues - 1);
  205. return -EINVAL;
  206. }
  207. ra_tid = BUILD_RAxTID(sta_id, tid);
  208. /* Modify device's station table to Tx this TID */
  209. ret = iwl_sta_tx_modify_enable_tid(priv, sta_id, tid);
  210. if (ret)
  211. return ret;
  212. spin_lock_irqsave(&priv->lock, flags);
  213. /* Stop this Tx queue before configuring it */
  214. iwlagn_tx_queue_stop_scheduler(priv, txq_id);
  215. /* Map receiver-address / traffic-ID to this queue */
  216. iwlagn_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
  217. /* Set this queue as a chain-building queue */
  218. iwl_set_bits_prph(priv, IWLAGN_SCD_QUEUECHAIN_SEL, (1<<txq_id));
  219. /* enable aggregations for the queue */
  220. iwl_set_bits_prph(priv, IWLAGN_SCD_AGGR_SEL, (1<<txq_id));
  221. /* Place first TFD at index corresponding to start sequence number.
  222. * Assumes that ssn_idx is valid (!= 0xFFF) */
  223. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  224. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  225. iwlagn_set_wr_ptrs(priv, txq_id, ssn_idx);
  226. /* Set up Tx window size and frame limit for this queue */
  227. iwl_write_targ_mem(priv, priv->scd_base_addr +
  228. IWLAGN_SCD_CONTEXT_QUEUE_OFFSET(txq_id) +
  229. sizeof(u32),
  230. ((SCD_WIN_SIZE <<
  231. IWLAGN_SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  232. IWLAGN_SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  233. ((SCD_FRAME_LIMIT <<
  234. IWLAGN_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  235. IWLAGN_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  236. iwl_set_bits_prph(priv, IWLAGN_SCD_INTERRUPT_MASK, (1 << txq_id));
  237. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  238. iwlagn_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
  239. spin_unlock_irqrestore(&priv->lock, flags);
  240. return 0;
  241. }
  242. int iwlagn_txq_agg_disable(struct iwl_priv *priv, u16 txq_id,
  243. u16 ssn_idx, u8 tx_fifo)
  244. {
  245. if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) ||
  246. (IWLAGN_FIRST_AMPDU_QUEUE +
  247. priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  248. IWL_ERR(priv,
  249. "queue number out of range: %d, must be %d to %d\n",
  250. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  251. IWLAGN_FIRST_AMPDU_QUEUE +
  252. priv->cfg->base_params->num_of_ampdu_queues - 1);
  253. return -EINVAL;
  254. }
  255. iwlagn_tx_queue_stop_scheduler(priv, txq_id);
  256. iwl_clear_bits_prph(priv, IWLAGN_SCD_AGGR_SEL, (1 << txq_id));
  257. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  258. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  259. /* supposes that ssn_idx is valid (!= 0xFFF) */
  260. iwlagn_set_wr_ptrs(priv, txq_id, ssn_idx);
  261. iwl_clear_bits_prph(priv, IWLAGN_SCD_INTERRUPT_MASK, (1 << txq_id));
  262. iwl_txq_ctx_deactivate(priv, txq_id);
  263. iwlagn_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
  264. return 0;
  265. }
  266. /*
  267. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  268. * must be called under priv->lock and mac access
  269. */
  270. void iwlagn_txq_set_sched(struct iwl_priv *priv, u32 mask)
  271. {
  272. iwl_write_prph(priv, IWLAGN_SCD_TXFACT, mask);
  273. }
  274. /*
  275. * handle build REPLY_TX command notification.
  276. */
  277. static void iwlagn_tx_cmd_build_basic(struct iwl_priv *priv,
  278. struct sk_buff *skb,
  279. struct iwl_tx_cmd *tx_cmd,
  280. struct ieee80211_tx_info *info,
  281. struct ieee80211_hdr *hdr,
  282. u8 std_id)
  283. {
  284. __le16 fc = hdr->frame_control;
  285. __le32 tx_flags = tx_cmd->tx_flags;
  286. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  287. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  288. tx_flags |= TX_CMD_FLG_ACK_MSK;
  289. if (ieee80211_is_mgmt(fc))
  290. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  291. if (ieee80211_is_probe_resp(fc) &&
  292. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  293. tx_flags |= TX_CMD_FLG_TSF_MSK;
  294. } else {
  295. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  296. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  297. }
  298. if (ieee80211_is_back_req(fc))
  299. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  300. else if (info->band == IEEE80211_BAND_2GHZ &&
  301. priv->cfg->bt_params &&
  302. priv->cfg->bt_params->advanced_bt_coexist &&
  303. (ieee80211_is_auth(fc) || ieee80211_is_assoc_req(fc) ||
  304. ieee80211_is_reassoc_req(fc) ||
  305. skb->protocol == cpu_to_be16(ETH_P_PAE)))
  306. tx_flags |= TX_CMD_FLG_IGNORE_BT;
  307. tx_cmd->sta_id = std_id;
  308. if (ieee80211_has_morefrags(fc))
  309. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  310. if (ieee80211_is_data_qos(fc)) {
  311. u8 *qc = ieee80211_get_qos_ctl(hdr);
  312. tx_cmd->tid_tspec = qc[0] & 0xf;
  313. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  314. } else {
  315. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  316. }
  317. priv->cfg->ops->utils->tx_cmd_protection(priv, info, fc, &tx_flags);
  318. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  319. if (ieee80211_is_mgmt(fc)) {
  320. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  321. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  322. else
  323. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  324. } else {
  325. tx_cmd->timeout.pm_frame_timeout = 0;
  326. }
  327. tx_cmd->driver_txop = 0;
  328. tx_cmd->tx_flags = tx_flags;
  329. tx_cmd->next_frame_len = 0;
  330. }
  331. #define RTS_DFAULT_RETRY_LIMIT 60
  332. static void iwlagn_tx_cmd_build_rate(struct iwl_priv *priv,
  333. struct iwl_tx_cmd *tx_cmd,
  334. struct ieee80211_tx_info *info,
  335. __le16 fc)
  336. {
  337. u32 rate_flags;
  338. int rate_idx;
  339. u8 rts_retry_limit;
  340. u8 data_retry_limit;
  341. u8 rate_plcp;
  342. /* Set retry limit on DATA packets and Probe Responses*/
  343. if (ieee80211_is_probe_resp(fc))
  344. data_retry_limit = 3;
  345. else
  346. data_retry_limit = IWLAGN_DEFAULT_TX_RETRY;
  347. tx_cmd->data_retry_limit = data_retry_limit;
  348. /* Set retry limit on RTS packets */
  349. rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
  350. if (data_retry_limit < rts_retry_limit)
  351. rts_retry_limit = data_retry_limit;
  352. tx_cmd->rts_retry_limit = rts_retry_limit;
  353. /* DATA packets will use the uCode station table for rate/antenna
  354. * selection */
  355. if (ieee80211_is_data(fc)) {
  356. tx_cmd->initial_rate_index = 0;
  357. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  358. return;
  359. }
  360. /**
  361. * If the current TX rate stored in mac80211 has the MCS bit set, it's
  362. * not really a TX rate. Thus, we use the lowest supported rate for
  363. * this band. Also use the lowest supported rate if the stored rate
  364. * index is invalid.
  365. */
  366. rate_idx = info->control.rates[0].idx;
  367. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS ||
  368. (rate_idx < 0) || (rate_idx > IWL_RATE_COUNT_LEGACY))
  369. rate_idx = rate_lowest_index(&priv->bands[info->band],
  370. info->control.sta);
  371. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  372. if (info->band == IEEE80211_BAND_5GHZ)
  373. rate_idx += IWL_FIRST_OFDM_RATE;
  374. /* Get PLCP rate for tx_cmd->rate_n_flags */
  375. rate_plcp = iwl_rates[rate_idx].plcp;
  376. /* Zero out flags for this packet */
  377. rate_flags = 0;
  378. /* Set CCK flag as needed */
  379. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  380. rate_flags |= RATE_MCS_CCK_MSK;
  381. /* Set up antennas */
  382. if (priv->cfg->bt_params &&
  383. priv->cfg->bt_params->advanced_bt_coexist &&
  384. priv->bt_full_concurrent) {
  385. /* operated as 1x1 in full concurrency mode */
  386. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  387. first_antenna(priv->hw_params.valid_tx_ant));
  388. } else
  389. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  390. priv->hw_params.valid_tx_ant);
  391. rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
  392. /* Set the rate in the TX cmd */
  393. tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
  394. }
  395. static void iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  396. struct ieee80211_tx_info *info,
  397. struct iwl_tx_cmd *tx_cmd,
  398. struct sk_buff *skb_frag,
  399. int sta_id)
  400. {
  401. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  402. switch (keyconf->cipher) {
  403. case WLAN_CIPHER_SUITE_CCMP:
  404. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  405. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  406. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  407. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  408. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  409. break;
  410. case WLAN_CIPHER_SUITE_TKIP:
  411. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  412. ieee80211_get_tkip_key(keyconf, skb_frag,
  413. IEEE80211_TKIP_P2_KEY, tx_cmd->key);
  414. IWL_DEBUG_TX(priv, "tx_cmd with tkip hwcrypto\n");
  415. break;
  416. case WLAN_CIPHER_SUITE_WEP104:
  417. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  418. /* fall through */
  419. case WLAN_CIPHER_SUITE_WEP40:
  420. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  421. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  422. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  423. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  424. "with key %d\n", keyconf->keyidx);
  425. break;
  426. default:
  427. IWL_ERR(priv, "Unknown encode cipher %x\n", keyconf->cipher);
  428. break;
  429. }
  430. }
  431. /*
  432. * start REPLY_TX command process
  433. */
  434. int iwlagn_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  435. {
  436. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  437. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  438. struct ieee80211_sta *sta = info->control.sta;
  439. struct iwl_station_priv *sta_priv = NULL;
  440. struct iwl_tx_queue *txq;
  441. struct iwl_queue *q;
  442. struct iwl_device_cmd *out_cmd;
  443. struct iwl_cmd_meta *out_meta;
  444. struct iwl_tx_cmd *tx_cmd;
  445. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  446. int txq_id;
  447. dma_addr_t phys_addr;
  448. dma_addr_t txcmd_phys;
  449. dma_addr_t scratch_phys;
  450. u16 len, firstlen, secondlen;
  451. u16 seq_number = 0;
  452. __le16 fc;
  453. u8 hdr_len;
  454. u8 sta_id;
  455. u8 wait_write_ptr = 0;
  456. u8 tid = 0;
  457. u8 *qc = NULL;
  458. unsigned long flags;
  459. bool is_agg = false;
  460. /*
  461. * If the frame needs to go out off-channel, then
  462. * we'll have put the PAN context to that channel,
  463. * so make the frame go out there.
  464. */
  465. if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)
  466. ctx = &priv->contexts[IWL_RXON_CTX_PAN];
  467. else if (info->control.vif)
  468. ctx = iwl_rxon_ctx_from_vif(info->control.vif);
  469. spin_lock_irqsave(&priv->lock, flags);
  470. if (iwl_is_rfkill(priv)) {
  471. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  472. goto drop_unlock;
  473. }
  474. fc = hdr->frame_control;
  475. #ifdef CONFIG_IWLWIFI_DEBUG
  476. if (ieee80211_is_auth(fc))
  477. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  478. else if (ieee80211_is_assoc_req(fc))
  479. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  480. else if (ieee80211_is_reassoc_req(fc))
  481. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  482. #endif
  483. hdr_len = ieee80211_hdrlen(fc);
  484. /* Find index into station table for destination station */
  485. sta_id = iwl_sta_id_or_broadcast(priv, ctx, info->control.sta);
  486. if (sta_id == IWL_INVALID_STATION) {
  487. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  488. hdr->addr1);
  489. goto drop_unlock;
  490. }
  491. IWL_DEBUG_TX(priv, "station Id %d\n", sta_id);
  492. if (sta)
  493. sta_priv = (void *)sta->drv_priv;
  494. if (sta_priv && sta_priv->asleep &&
  495. (info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE)) {
  496. /*
  497. * This sends an asynchronous command to the device,
  498. * but we can rely on it being processed before the
  499. * next frame is processed -- and the next frame to
  500. * this station is the one that will consume this
  501. * counter.
  502. * For now set the counter to just 1 since we do not
  503. * support uAPSD yet.
  504. */
  505. iwl_sta_modify_sleep_tx_count(priv, sta_id, 1);
  506. }
  507. /*
  508. * Send this frame after DTIM -- there's a special queue
  509. * reserved for this for contexts that support AP mode.
  510. */
  511. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  512. txq_id = ctx->mcast_queue;
  513. /*
  514. * The microcode will clear the more data
  515. * bit in the last frame it transmits.
  516. */
  517. hdr->frame_control |=
  518. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  519. } else
  520. txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
  521. /* irqs already disabled/saved above when locking priv->lock */
  522. spin_lock(&priv->sta_lock);
  523. if (ieee80211_is_data_qos(fc)) {
  524. qc = ieee80211_get_qos_ctl(hdr);
  525. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  526. if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
  527. spin_unlock(&priv->sta_lock);
  528. goto drop_unlock;
  529. }
  530. seq_number = priv->stations[sta_id].tid[tid].seq_number;
  531. seq_number &= IEEE80211_SCTL_SEQ;
  532. hdr->seq_ctrl = hdr->seq_ctrl &
  533. cpu_to_le16(IEEE80211_SCTL_FRAG);
  534. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  535. seq_number += 0x10;
  536. /* aggregation is on for this <sta,tid> */
  537. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  538. priv->stations[sta_id].tid[tid].agg.state == IWL_AGG_ON) {
  539. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  540. is_agg = true;
  541. }
  542. }
  543. txq = &priv->txq[txq_id];
  544. q = &txq->q;
  545. if (unlikely(iwl_queue_space(q) < q->high_mark)) {
  546. spin_unlock(&priv->sta_lock);
  547. goto drop_unlock;
  548. }
  549. if (ieee80211_is_data_qos(fc)) {
  550. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  551. if (!ieee80211_has_morefrags(fc))
  552. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  553. }
  554. spin_unlock(&priv->sta_lock);
  555. /* Set up driver data for this TFD */
  556. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  557. txq->txb[q->write_ptr].skb = skb;
  558. txq->txb[q->write_ptr].ctx = ctx;
  559. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  560. out_cmd = txq->cmd[q->write_ptr];
  561. out_meta = &txq->meta[q->write_ptr];
  562. tx_cmd = &out_cmd->cmd.tx;
  563. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  564. memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
  565. /*
  566. * Set up the Tx-command (not MAC!) header.
  567. * Store the chosen Tx queue and TFD index within the sequence field;
  568. * after Tx, uCode's Tx response will return this value so driver can
  569. * locate the frame within the tx queue and do post-tx processing.
  570. */
  571. out_cmd->hdr.cmd = REPLY_TX;
  572. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  573. INDEX_TO_SEQ(q->write_ptr)));
  574. /* Copy MAC header from skb into command buffer */
  575. memcpy(tx_cmd->hdr, hdr, hdr_len);
  576. /* Total # bytes to be transmitted */
  577. len = (u16)skb->len;
  578. tx_cmd->len = cpu_to_le16(len);
  579. if (info->control.hw_key)
  580. iwlagn_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
  581. /* TODO need this for burst mode later on */
  582. iwlagn_tx_cmd_build_basic(priv, skb, tx_cmd, info, hdr, sta_id);
  583. iwl_dbg_log_tx_data_frame(priv, len, hdr);
  584. iwlagn_tx_cmd_build_rate(priv, tx_cmd, info, fc);
  585. iwl_update_stats(priv, true, fc, len);
  586. /*
  587. * Use the first empty entry in this queue's command buffer array
  588. * to contain the Tx command and MAC header concatenated together
  589. * (payload data will be in another buffer).
  590. * Size of this varies, due to varying MAC header length.
  591. * If end is not dword aligned, we'll have 2 extra bytes at the end
  592. * of the MAC header (device reads on dword boundaries).
  593. * We'll tell device about this padding later.
  594. */
  595. len = sizeof(struct iwl_tx_cmd) +
  596. sizeof(struct iwl_cmd_header) + hdr_len;
  597. firstlen = (len + 3) & ~3;
  598. /* Tell NIC about any 2-byte padding after MAC header */
  599. if (firstlen != len)
  600. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  601. /* Physical address of this Tx command's header (not MAC header!),
  602. * within command buffer array. */
  603. txcmd_phys = pci_map_single(priv->pci_dev,
  604. &out_cmd->hdr, firstlen,
  605. PCI_DMA_BIDIRECTIONAL);
  606. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  607. dma_unmap_len_set(out_meta, len, firstlen);
  608. /* Add buffer containing Tx command and MAC(!) header to TFD's
  609. * first entry */
  610. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  611. txcmd_phys, firstlen, 1, 0);
  612. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  613. txq->need_update = 1;
  614. } else {
  615. wait_write_ptr = 1;
  616. txq->need_update = 0;
  617. }
  618. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  619. * if any (802.11 null frames have no payload). */
  620. secondlen = skb->len - hdr_len;
  621. if (secondlen > 0) {
  622. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  623. secondlen, PCI_DMA_TODEVICE);
  624. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  625. phys_addr, secondlen,
  626. 0, 0);
  627. }
  628. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  629. offsetof(struct iwl_tx_cmd, scratch);
  630. /* take back ownership of DMA buffer to enable update */
  631. pci_dma_sync_single_for_cpu(priv->pci_dev, txcmd_phys,
  632. firstlen, PCI_DMA_BIDIRECTIONAL);
  633. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  634. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  635. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  636. le16_to_cpu(out_cmd->hdr.sequence));
  637. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  638. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  639. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  640. /* Set up entry for this TFD in Tx byte-count array */
  641. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  642. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq,
  643. le16_to_cpu(tx_cmd->len));
  644. pci_dma_sync_single_for_device(priv->pci_dev, txcmd_phys,
  645. firstlen, PCI_DMA_BIDIRECTIONAL);
  646. trace_iwlwifi_dev_tx(priv,
  647. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  648. sizeof(struct iwl_tfd),
  649. &out_cmd->hdr, firstlen,
  650. skb->data + hdr_len, secondlen);
  651. /* Tell device the write index *just past* this latest filled TFD */
  652. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  653. iwl_txq_update_write_ptr(priv, txq);
  654. spin_unlock_irqrestore(&priv->lock, flags);
  655. /*
  656. * At this point the frame is "transmitted" successfully
  657. * and we will get a TX status notification eventually,
  658. * regardless of the value of ret. "ret" only indicates
  659. * whether or not we should update the write pointer.
  660. */
  661. /*
  662. * Avoid atomic ops if it isn't an associated client.
  663. * Also, if this is a packet for aggregation, don't
  664. * increase the counter because the ucode will stop
  665. * aggregation queues when their respective station
  666. * goes to sleep.
  667. */
  668. if (sta_priv && sta_priv->client && !is_agg)
  669. atomic_inc(&sta_priv->pending_frames);
  670. if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
  671. if (wait_write_ptr) {
  672. spin_lock_irqsave(&priv->lock, flags);
  673. txq->need_update = 1;
  674. iwl_txq_update_write_ptr(priv, txq);
  675. spin_unlock_irqrestore(&priv->lock, flags);
  676. } else {
  677. iwl_stop_queue(priv, txq);
  678. }
  679. }
  680. return 0;
  681. drop_unlock:
  682. spin_unlock_irqrestore(&priv->lock, flags);
  683. return -1;
  684. }
  685. static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv,
  686. struct iwl_dma_ptr *ptr, size_t size)
  687. {
  688. ptr->addr = dma_alloc_coherent(&priv->pci_dev->dev, size, &ptr->dma,
  689. GFP_KERNEL);
  690. if (!ptr->addr)
  691. return -ENOMEM;
  692. ptr->size = size;
  693. return 0;
  694. }
  695. static inline void iwlagn_free_dma_ptr(struct iwl_priv *priv,
  696. struct iwl_dma_ptr *ptr)
  697. {
  698. if (unlikely(!ptr->addr))
  699. return;
  700. dma_free_coherent(&priv->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
  701. memset(ptr, 0, sizeof(*ptr));
  702. }
  703. /**
  704. * iwlagn_hw_txq_ctx_free - Free TXQ Context
  705. *
  706. * Destroy all TX DMA queues and structures
  707. */
  708. void iwlagn_hw_txq_ctx_free(struct iwl_priv *priv)
  709. {
  710. int txq_id;
  711. /* Tx queues */
  712. if (priv->txq) {
  713. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  714. if (txq_id == priv->cmd_queue)
  715. iwl_cmd_queue_free(priv);
  716. else
  717. iwl_tx_queue_free(priv, txq_id);
  718. }
  719. iwlagn_free_dma_ptr(priv, &priv->kw);
  720. iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
  721. /* free tx queue structure */
  722. iwl_free_txq_mem(priv);
  723. }
  724. /**
  725. * iwlagn_txq_ctx_alloc - allocate TX queue context
  726. * Allocate all Tx DMA structures and initialize them
  727. *
  728. * @param priv
  729. * @return error code
  730. */
  731. int iwlagn_txq_ctx_alloc(struct iwl_priv *priv)
  732. {
  733. int ret;
  734. int txq_id, slots_num;
  735. unsigned long flags;
  736. /* Free all tx/cmd queues and keep-warm buffer */
  737. iwlagn_hw_txq_ctx_free(priv);
  738. ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
  739. priv->hw_params.scd_bc_tbls_size);
  740. if (ret) {
  741. IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
  742. goto error_bc_tbls;
  743. }
  744. /* Alloc keep-warm buffer */
  745. ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
  746. if (ret) {
  747. IWL_ERR(priv, "Keep Warm allocation failed\n");
  748. goto error_kw;
  749. }
  750. /* allocate tx queue structure */
  751. ret = iwl_alloc_txq_mem(priv);
  752. if (ret)
  753. goto error;
  754. spin_lock_irqsave(&priv->lock, flags);
  755. /* Turn off all Tx DMA fifos */
  756. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  757. /* Tell NIC where to find the "keep warm" buffer */
  758. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  759. spin_unlock_irqrestore(&priv->lock, flags);
  760. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  761. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  762. slots_num = (txq_id == priv->cmd_queue) ?
  763. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  764. ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  765. txq_id);
  766. if (ret) {
  767. IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
  768. goto error;
  769. }
  770. }
  771. return ret;
  772. error:
  773. iwlagn_hw_txq_ctx_free(priv);
  774. iwlagn_free_dma_ptr(priv, &priv->kw);
  775. error_kw:
  776. iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
  777. error_bc_tbls:
  778. return ret;
  779. }
  780. void iwlagn_txq_ctx_reset(struct iwl_priv *priv)
  781. {
  782. int txq_id, slots_num;
  783. unsigned long flags;
  784. spin_lock_irqsave(&priv->lock, flags);
  785. /* Turn off all Tx DMA fifos */
  786. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  787. /* Tell NIC where to find the "keep warm" buffer */
  788. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  789. spin_unlock_irqrestore(&priv->lock, flags);
  790. /* Alloc and init all Tx queues, including the command queue (#4) */
  791. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  792. slots_num = txq_id == priv->cmd_queue ?
  793. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  794. iwl_tx_queue_reset(priv, &priv->txq[txq_id], slots_num, txq_id);
  795. }
  796. }
  797. /**
  798. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  799. */
  800. void iwlagn_txq_ctx_stop(struct iwl_priv *priv)
  801. {
  802. int ch, txq_id;
  803. unsigned long flags;
  804. /* Turn off all Tx DMA fifos */
  805. spin_lock_irqsave(&priv->lock, flags);
  806. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  807. /* Stop each Tx DMA channel, and wait for it to be idle */
  808. for (ch = 0; ch < priv->hw_params.dma_chnl_num; ch++) {
  809. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  810. if (iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  811. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  812. 1000))
  813. IWL_ERR(priv, "Failing on timeout while stopping"
  814. " DMA channel %d [0x%08x]", ch,
  815. iwl_read_direct32(priv, FH_TSSR_TX_STATUS_REG));
  816. }
  817. spin_unlock_irqrestore(&priv->lock, flags);
  818. if (!priv->txq)
  819. return;
  820. /* Unmap DMA from host system and free skb's */
  821. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  822. if (txq_id == priv->cmd_queue)
  823. iwl_cmd_queue_unmap(priv);
  824. else
  825. iwl_tx_queue_unmap(priv, txq_id);
  826. }
  827. /*
  828. * Find first available (lowest unused) Tx Queue, mark it "active".
  829. * Called only when finding queue for aggregation.
  830. * Should never return anything < 7, because they should already
  831. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  832. */
  833. static int iwlagn_txq_ctx_activate_free(struct iwl_priv *priv)
  834. {
  835. int txq_id;
  836. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  837. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  838. return txq_id;
  839. return -1;
  840. }
  841. int iwlagn_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
  842. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  843. {
  844. int sta_id;
  845. int tx_fifo;
  846. int txq_id;
  847. int ret;
  848. unsigned long flags;
  849. struct iwl_tid_data *tid_data;
  850. tx_fifo = get_fifo_from_tid(iwl_rxon_ctx_from_vif(vif), tid);
  851. if (unlikely(tx_fifo < 0))
  852. return tx_fifo;
  853. IWL_WARN(priv, "%s on ra = %pM tid = %d\n",
  854. __func__, sta->addr, tid);
  855. sta_id = iwl_sta_id(sta);
  856. if (sta_id == IWL_INVALID_STATION) {
  857. IWL_ERR(priv, "Start AGG on invalid station\n");
  858. return -ENXIO;
  859. }
  860. if (unlikely(tid >= MAX_TID_COUNT))
  861. return -EINVAL;
  862. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
  863. IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
  864. return -ENXIO;
  865. }
  866. txq_id = iwlagn_txq_ctx_activate_free(priv);
  867. if (txq_id == -1) {
  868. IWL_ERR(priv, "No free aggregation queue available\n");
  869. return -ENXIO;
  870. }
  871. spin_lock_irqsave(&priv->sta_lock, flags);
  872. tid_data = &priv->stations[sta_id].tid[tid];
  873. *ssn = SEQ_TO_SN(tid_data->seq_number);
  874. tid_data->agg.txq_id = txq_id;
  875. iwl_set_swq_id(&priv->txq[txq_id], get_ac_from_tid(tid), txq_id);
  876. spin_unlock_irqrestore(&priv->sta_lock, flags);
  877. ret = priv->cfg->ops->lib->txq_agg_enable(priv, txq_id, tx_fifo,
  878. sta_id, tid, *ssn);
  879. if (ret)
  880. return ret;
  881. spin_lock_irqsave(&priv->sta_lock, flags);
  882. tid_data = &priv->stations[sta_id].tid[tid];
  883. if (tid_data->tfds_in_queue == 0) {
  884. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  885. tid_data->agg.state = IWL_AGG_ON;
  886. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  887. } else {
  888. IWL_DEBUG_HT(priv, "HW queue is NOT empty: %d packets in HW queue\n",
  889. tid_data->tfds_in_queue);
  890. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  891. }
  892. spin_unlock_irqrestore(&priv->sta_lock, flags);
  893. return ret;
  894. }
  895. int iwlagn_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
  896. struct ieee80211_sta *sta, u16 tid)
  897. {
  898. int tx_fifo_id, txq_id, sta_id, ssn;
  899. struct iwl_tid_data *tid_data;
  900. int write_ptr, read_ptr;
  901. unsigned long flags;
  902. tx_fifo_id = get_fifo_from_tid(iwl_rxon_ctx_from_vif(vif), tid);
  903. if (unlikely(tx_fifo_id < 0))
  904. return tx_fifo_id;
  905. sta_id = iwl_sta_id(sta);
  906. if (sta_id == IWL_INVALID_STATION) {
  907. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  908. return -ENXIO;
  909. }
  910. spin_lock_irqsave(&priv->sta_lock, flags);
  911. tid_data = &priv->stations[sta_id].tid[tid];
  912. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  913. txq_id = tid_data->agg.txq_id;
  914. switch (priv->stations[sta_id].tid[tid].agg.state) {
  915. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  916. /*
  917. * This can happen if the peer stops aggregation
  918. * again before we've had a chance to drain the
  919. * queue we selected previously, i.e. before the
  920. * session was really started completely.
  921. */
  922. IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
  923. goto turn_off;
  924. case IWL_AGG_ON:
  925. break;
  926. default:
  927. IWL_WARN(priv, "Stopping AGG while state not ON or starting\n");
  928. }
  929. write_ptr = priv->txq[txq_id].q.write_ptr;
  930. read_ptr = priv->txq[txq_id].q.read_ptr;
  931. /* The queue is not empty */
  932. if (write_ptr != read_ptr) {
  933. IWL_DEBUG_HT(priv, "Stopping a non empty AGG HW QUEUE\n");
  934. priv->stations[sta_id].tid[tid].agg.state =
  935. IWL_EMPTYING_HW_QUEUE_DELBA;
  936. spin_unlock_irqrestore(&priv->sta_lock, flags);
  937. return 0;
  938. }
  939. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  940. turn_off:
  941. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  942. /* do not restore/save irqs */
  943. spin_unlock(&priv->sta_lock);
  944. spin_lock(&priv->lock);
  945. /*
  946. * the only reason this call can fail is queue number out of range,
  947. * which can happen if uCode is reloaded and all the station
  948. * information are lost. if it is outside the range, there is no need
  949. * to deactivate the uCode queue, just return "success" to allow
  950. * mac80211 to clean up it own data.
  951. */
  952. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id, ssn,
  953. tx_fifo_id);
  954. spin_unlock_irqrestore(&priv->lock, flags);
  955. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  956. return 0;
  957. }
  958. int iwlagn_txq_check_empty(struct iwl_priv *priv,
  959. int sta_id, u8 tid, int txq_id)
  960. {
  961. struct iwl_queue *q = &priv->txq[txq_id].q;
  962. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  963. struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
  964. struct iwl_rxon_context *ctx;
  965. ctx = &priv->contexts[priv->stations[sta_id].ctxid];
  966. lockdep_assert_held(&priv->sta_lock);
  967. switch (priv->stations[sta_id].tid[tid].agg.state) {
  968. case IWL_EMPTYING_HW_QUEUE_DELBA:
  969. /* We are reclaiming the last packet of the */
  970. /* aggregated HW queue */
  971. if ((txq_id == tid_data->agg.txq_id) &&
  972. (q->read_ptr == q->write_ptr)) {
  973. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  974. int tx_fifo = get_fifo_from_tid(ctx, tid);
  975. IWL_DEBUG_HT(priv, "HW queue empty: continue DELBA flow\n");
  976. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id,
  977. ssn, tx_fifo);
  978. tid_data->agg.state = IWL_AGG_OFF;
  979. ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  980. }
  981. break;
  982. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  983. /* We are reclaiming the last packet of the queue */
  984. if (tid_data->tfds_in_queue == 0) {
  985. IWL_DEBUG_HT(priv, "HW queue empty: continue ADDBA flow\n");
  986. tid_data->agg.state = IWL_AGG_ON;
  987. ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  988. }
  989. break;
  990. }
  991. return 0;
  992. }
  993. static void iwlagn_non_agg_tx_status(struct iwl_priv *priv,
  994. struct iwl_rxon_context *ctx,
  995. const u8 *addr1)
  996. {
  997. struct ieee80211_sta *sta;
  998. struct iwl_station_priv *sta_priv;
  999. rcu_read_lock();
  1000. sta = ieee80211_find_sta(ctx->vif, addr1);
  1001. if (sta) {
  1002. sta_priv = (void *)sta->drv_priv;
  1003. /* avoid atomic ops if this isn't a client */
  1004. if (sta_priv->client &&
  1005. atomic_dec_return(&sta_priv->pending_frames) == 0)
  1006. ieee80211_sta_block_awake(priv->hw, sta, false);
  1007. }
  1008. rcu_read_unlock();
  1009. }
  1010. static void iwlagn_tx_status(struct iwl_priv *priv, struct iwl_tx_info *tx_info,
  1011. bool is_agg)
  1012. {
  1013. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx_info->skb->data;
  1014. if (!is_agg)
  1015. iwlagn_non_agg_tx_status(priv, tx_info->ctx, hdr->addr1);
  1016. ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb);
  1017. }
  1018. int iwlagn_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  1019. {
  1020. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  1021. struct iwl_queue *q = &txq->q;
  1022. struct iwl_tx_info *tx_info;
  1023. int nfreed = 0;
  1024. struct ieee80211_hdr *hdr;
  1025. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  1026. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  1027. "is out of range [0-%d] %d %d.\n", txq_id,
  1028. index, q->n_bd, q->write_ptr, q->read_ptr);
  1029. return 0;
  1030. }
  1031. for (index = iwl_queue_inc_wrap(index, q->n_bd);
  1032. q->read_ptr != index;
  1033. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  1034. tx_info = &txq->txb[txq->q.read_ptr];
  1035. iwlagn_tx_status(priv, tx_info,
  1036. txq_id >= IWLAGN_FIRST_AMPDU_QUEUE);
  1037. hdr = (struct ieee80211_hdr *)tx_info->skb->data;
  1038. if (hdr && ieee80211_is_data_qos(hdr->frame_control))
  1039. nfreed++;
  1040. tx_info->skb = NULL;
  1041. if (priv->cfg->ops->lib->txq_inval_byte_cnt_tbl)
  1042. priv->cfg->ops->lib->txq_inval_byte_cnt_tbl(priv, txq);
  1043. priv->cfg->ops->lib->txq_free_tfd(priv, txq);
  1044. }
  1045. return nfreed;
  1046. }
  1047. /**
  1048. * iwlagn_tx_status_reply_compressed_ba - Update tx status from block-ack
  1049. *
  1050. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  1051. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  1052. */
  1053. static int iwlagn_tx_status_reply_compressed_ba(struct iwl_priv *priv,
  1054. struct iwl_ht_agg *agg,
  1055. struct iwl_compressed_ba_resp *ba_resp)
  1056. {
  1057. int i, sh, ack;
  1058. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  1059. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1060. int successes = 0;
  1061. struct ieee80211_tx_info *info;
  1062. if (unlikely(!agg->wait_for_ba)) {
  1063. if (unlikely(ba_resp->bitmap))
  1064. IWL_ERR(priv, "Received BA when not expected\n");
  1065. return -EINVAL;
  1066. }
  1067. /* Mark that the expected block-ack response arrived */
  1068. agg->wait_for_ba = 0;
  1069. IWL_DEBUG_TX_REPLY(priv, "BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
  1070. /* Calculate shift to align block-ack bits with our Tx window bits */
  1071. sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl >> 4);
  1072. if (sh < 0) /* tbw something is wrong with indices */
  1073. sh += 0x100;
  1074. if (agg->frame_count > (64 - sh)) {
  1075. IWL_DEBUG_TX_REPLY(priv, "more frames than bitmap size");
  1076. return -1;
  1077. }
  1078. if (!priv->cfg->base_params->no_agg_framecnt_info && ba_resp->txed) {
  1079. /*
  1080. * sent and ack information provided by uCode
  1081. * use it instead of figure out ourself
  1082. */
  1083. if (ba_resp->txed_2_done > ba_resp->txed) {
  1084. IWL_DEBUG_TX_REPLY(priv,
  1085. "bogus sent(%d) and ack(%d) count\n",
  1086. ba_resp->txed, ba_resp->txed_2_done);
  1087. /*
  1088. * set txed_2_done = txed,
  1089. * so it won't impact rate scale
  1090. */
  1091. ba_resp->txed = ba_resp->txed_2_done;
  1092. }
  1093. IWL_DEBUG_HT(priv, "agg frames sent:%d, acked:%d\n",
  1094. ba_resp->txed, ba_resp->txed_2_done);
  1095. } else {
  1096. u64 bitmap, sent_bitmap;
  1097. /* don't use 64-bit values for now */
  1098. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  1099. /* check for success or failure according to the
  1100. * transmitted bitmap and block-ack bitmap */
  1101. sent_bitmap = bitmap & agg->bitmap;
  1102. /* For each frame attempted in aggregation,
  1103. * update driver's record of tx frame's status. */
  1104. i = 0;
  1105. while (sent_bitmap) {
  1106. ack = sent_bitmap & 1ULL;
  1107. successes += ack;
  1108. IWL_DEBUG_TX_REPLY(priv, "%s ON i=%d idx=%d raw=%d\n",
  1109. ack ? "ACK" : "NACK", i,
  1110. (agg->start_idx + i) & 0xff,
  1111. agg->start_idx + i);
  1112. sent_bitmap >>= 1;
  1113. ++i;
  1114. }
  1115. IWL_DEBUG_TX_REPLY(priv, "Bitmap %llx\n",
  1116. (unsigned long long)bitmap);
  1117. }
  1118. info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb);
  1119. memset(&info->status, 0, sizeof(info->status));
  1120. info->flags |= IEEE80211_TX_STAT_ACK;
  1121. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1122. if (!priv->cfg->base_params->no_agg_framecnt_info && ba_resp->txed) {
  1123. info->status.ampdu_ack_len = ba_resp->txed_2_done;
  1124. info->status.ampdu_len = ba_resp->txed;
  1125. } else {
  1126. info->status.ampdu_ack_len = successes;
  1127. info->status.ampdu_len = agg->frame_count;
  1128. }
  1129. iwlagn_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
  1130. return 0;
  1131. }
  1132. /**
  1133. * translate ucode response to mac80211 tx status control values
  1134. */
  1135. void iwlagn_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
  1136. struct ieee80211_tx_info *info)
  1137. {
  1138. struct ieee80211_tx_rate *r = &info->control.rates[0];
  1139. info->antenna_sel_tx =
  1140. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  1141. if (rate_n_flags & RATE_MCS_HT_MSK)
  1142. r->flags |= IEEE80211_TX_RC_MCS;
  1143. if (rate_n_flags & RATE_MCS_GF_MSK)
  1144. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  1145. if (rate_n_flags & RATE_MCS_HT40_MSK)
  1146. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1147. if (rate_n_flags & RATE_MCS_DUP_MSK)
  1148. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  1149. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1150. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  1151. r->idx = iwlagn_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  1152. }
  1153. /**
  1154. * iwlagn_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  1155. *
  1156. * Handles block-acknowledge notification from device, which reports success
  1157. * of frames sent via aggregation.
  1158. */
  1159. void iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv,
  1160. struct iwl_rx_mem_buffer *rxb)
  1161. {
  1162. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1163. struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  1164. struct iwl_tx_queue *txq = NULL;
  1165. struct iwl_ht_agg *agg;
  1166. int index;
  1167. int sta_id;
  1168. int tid;
  1169. unsigned long flags;
  1170. /* "flow" corresponds to Tx queue */
  1171. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1172. /* "ssn" is start of block-ack Tx window, corresponds to index
  1173. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1174. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  1175. if (scd_flow >= priv->hw_params.max_txq_num) {
  1176. IWL_ERR(priv,
  1177. "BUG_ON scd_flow is bigger than number of queues\n");
  1178. return;
  1179. }
  1180. txq = &priv->txq[scd_flow];
  1181. sta_id = ba_resp->sta_id;
  1182. tid = ba_resp->tid;
  1183. agg = &priv->stations[sta_id].tid[tid].agg;
  1184. if (unlikely(agg->txq_id != scd_flow)) {
  1185. /*
  1186. * FIXME: this is a uCode bug which need to be addressed,
  1187. * log the information and return for now!
  1188. * since it is possible happen very often and in order
  1189. * not to fill the syslog, don't enable the logging by default
  1190. */
  1191. IWL_DEBUG_TX_REPLY(priv,
  1192. "BA scd_flow %d does not match txq_id %d\n",
  1193. scd_flow, agg->txq_id);
  1194. return;
  1195. }
  1196. /* Find index just before block-ack window */
  1197. index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  1198. spin_lock_irqsave(&priv->sta_lock, flags);
  1199. IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
  1200. "sta_id = %d\n",
  1201. agg->wait_for_ba,
  1202. (u8 *) &ba_resp->sta_addr_lo32,
  1203. ba_resp->sta_id);
  1204. IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
  1205. "%d, scd_ssn = %d\n",
  1206. ba_resp->tid,
  1207. ba_resp->seq_ctl,
  1208. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  1209. ba_resp->scd_flow,
  1210. ba_resp->scd_ssn);
  1211. IWL_DEBUG_TX_REPLY(priv, "DAT start_idx = %d, bitmap = 0x%llx\n",
  1212. agg->start_idx,
  1213. (unsigned long long)agg->bitmap);
  1214. /* Update driver's record of ACK vs. not for each frame in window */
  1215. iwlagn_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  1216. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  1217. * block-ack window (we assume that they've been successfully
  1218. * transmitted ... if not, it's too late anyway). */
  1219. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  1220. /* calculate mac80211 ampdu sw queue to wake */
  1221. int freed = iwlagn_tx_queue_reclaim(priv, scd_flow, index);
  1222. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  1223. if ((iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  1224. priv->mac80211_registered &&
  1225. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
  1226. iwl_wake_queue(priv, txq);
  1227. iwlagn_txq_check_empty(priv, sta_id, tid, scd_flow);
  1228. }
  1229. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1230. }
  1231. #ifdef CONFIG_IWLWIFI_DEBUG
  1232. const char *iwl_get_tx_fail_reason(u32 status)
  1233. {
  1234. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  1235. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  1236. switch (status & TX_STATUS_MSK) {
  1237. case TX_STATUS_SUCCESS:
  1238. return "SUCCESS";
  1239. TX_STATUS_POSTPONE(DELAY);
  1240. TX_STATUS_POSTPONE(FEW_BYTES);
  1241. TX_STATUS_POSTPONE(BT_PRIO);
  1242. TX_STATUS_POSTPONE(QUIET_PERIOD);
  1243. TX_STATUS_POSTPONE(CALC_TTAK);
  1244. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  1245. TX_STATUS_FAIL(SHORT_LIMIT);
  1246. TX_STATUS_FAIL(LONG_LIMIT);
  1247. TX_STATUS_FAIL(FIFO_UNDERRUN);
  1248. TX_STATUS_FAIL(DRAIN_FLOW);
  1249. TX_STATUS_FAIL(RFKILL_FLUSH);
  1250. TX_STATUS_FAIL(LIFE_EXPIRE);
  1251. TX_STATUS_FAIL(DEST_PS);
  1252. TX_STATUS_FAIL(HOST_ABORTED);
  1253. TX_STATUS_FAIL(BT_RETRY);
  1254. TX_STATUS_FAIL(STA_INVALID);
  1255. TX_STATUS_FAIL(FRAG_DROPPED);
  1256. TX_STATUS_FAIL(TID_DISABLE);
  1257. TX_STATUS_FAIL(FIFO_FLUSHED);
  1258. TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
  1259. TX_STATUS_FAIL(PASSIVE_NO_RX);
  1260. TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
  1261. }
  1262. return "UNKNOWN";
  1263. #undef TX_STATUS_FAIL
  1264. #undef TX_STATUS_POSTPONE
  1265. }
  1266. #endif /* CONFIG_IWLWIFI_DEBUG */