iwl-agn-tx.c 41 KB

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