iwl-agn-tx.c 43 KB

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