iwl-agn-tx.c 43 KB

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