iwl-tx.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  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/etherdevice.h>
  30. #include <linux/sched.h>
  31. #include <net/mac80211.h>
  32. #include "iwl-eeprom.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. /*
  39. * mac80211 queues, ACs, hardware queues, FIFOs.
  40. *
  41. * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
  42. *
  43. * Mac80211 uses the following numbers, which we get as from it
  44. * by way of skb_get_queue_mapping(skb):
  45. *
  46. * VO 0
  47. * VI 1
  48. * BE 2
  49. * BK 3
  50. *
  51. *
  52. * Regular (not A-MPDU) frames are put into hardware queues corresponding
  53. * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
  54. * own queue per aggregation session (RA/TID combination), such queues are
  55. * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
  56. * order to map frames to the right queue, we also need an AC->hw queue
  57. * mapping. This is implemented here.
  58. *
  59. * Due to the way hw queues are set up (by the hw specific modules like
  60. * iwl-4965.c, iwl-5000.c etc.), the AC->hw queue mapping is the identity
  61. * mapping.
  62. */
  63. static const u8 tid_to_ac[] = {
  64. /* this matches the mac80211 numbers */
  65. 2, 3, 3, 2, 1, 1, 0, 0
  66. };
  67. static const u8 ac_to_fifo[] = {
  68. IWL_TX_FIFO_VO,
  69. IWL_TX_FIFO_VI,
  70. IWL_TX_FIFO_BE,
  71. IWL_TX_FIFO_BK,
  72. };
  73. static inline int get_fifo_from_ac(u8 ac)
  74. {
  75. return ac_to_fifo[ac];
  76. }
  77. static inline int get_queue_from_ac(u16 ac)
  78. {
  79. return ac;
  80. }
  81. static inline int get_fifo_from_tid(u16 tid)
  82. {
  83. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  84. return get_fifo_from_ac(tid_to_ac[tid]);
  85. /* no support for TIDs 8-15 yet */
  86. return -EINVAL;
  87. }
  88. static inline int iwl_alloc_dma_ptr(struct iwl_priv *priv,
  89. struct iwl_dma_ptr *ptr, size_t size)
  90. {
  91. ptr->addr = dma_alloc_coherent(&priv->pci_dev->dev, size, &ptr->dma,
  92. GFP_KERNEL);
  93. if (!ptr->addr)
  94. return -ENOMEM;
  95. ptr->size = size;
  96. return 0;
  97. }
  98. static inline void iwl_free_dma_ptr(struct iwl_priv *priv,
  99. struct iwl_dma_ptr *ptr)
  100. {
  101. if (unlikely(!ptr->addr))
  102. return;
  103. dma_free_coherent(&priv->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
  104. memset(ptr, 0, sizeof(*ptr));
  105. }
  106. /**
  107. * iwl_txq_update_write_ptr - Send new write index to hardware
  108. */
  109. void iwl_txq_update_write_ptr(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  110. {
  111. u32 reg = 0;
  112. int txq_id = txq->q.id;
  113. if (txq->need_update == 0)
  114. return;
  115. /* if we're trying to save power */
  116. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  117. /* wake up nic if it's powered down ...
  118. * uCode will wake up, and interrupt us again, so next
  119. * time we'll skip this part. */
  120. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  121. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  122. IWL_DEBUG_INFO(priv, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
  123. txq_id, reg);
  124. iwl_set_bit(priv, CSR_GP_CNTRL,
  125. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  126. return;
  127. }
  128. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  129. txq->q.write_ptr | (txq_id << 8));
  130. /* else not in power-save mode, uCode will never sleep when we're
  131. * trying to tx (during RFKILL, we're not trying to tx). */
  132. } else
  133. iwl_write32(priv, HBUS_TARG_WRPTR,
  134. txq->q.write_ptr | (txq_id << 8));
  135. txq->need_update = 0;
  136. }
  137. EXPORT_SYMBOL(iwl_txq_update_write_ptr);
  138. void iwl_free_tfds_in_queue(struct iwl_priv *priv,
  139. int sta_id, int tid, int freed)
  140. {
  141. if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  142. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  143. else {
  144. IWL_ERR(priv, "free more than tfds_in_queue (%u:%d)\n",
  145. priv->stations[sta_id].tid[tid].tfds_in_queue,
  146. freed);
  147. priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
  148. }
  149. }
  150. EXPORT_SYMBOL(iwl_free_tfds_in_queue);
  151. /**
  152. * iwl_tx_queue_free - Deallocate DMA queue.
  153. * @txq: Transmit queue to deallocate.
  154. *
  155. * Empty queue by removing and destroying all BD's.
  156. * Free all buffers.
  157. * 0-fill, but do not free "txq" descriptor structure.
  158. */
  159. void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
  160. {
  161. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  162. struct iwl_queue *q = &txq->q;
  163. struct device *dev = &priv->pci_dev->dev;
  164. int i;
  165. if (q->n_bd == 0)
  166. return;
  167. /* first, empty all BD's */
  168. for (; q->write_ptr != q->read_ptr;
  169. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
  170. priv->cfg->ops->lib->txq_free_tfd(priv, txq);
  171. /* De-alloc array of command/tx buffers */
  172. for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
  173. kfree(txq->cmd[i]);
  174. /* De-alloc circular buffer of TFDs */
  175. if (txq->q.n_bd)
  176. dma_free_coherent(dev, priv->hw_params.tfd_size *
  177. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  178. /* De-alloc array of per-TFD driver data */
  179. kfree(txq->txb);
  180. txq->txb = NULL;
  181. /* deallocate arrays */
  182. kfree(txq->cmd);
  183. kfree(txq->meta);
  184. txq->cmd = NULL;
  185. txq->meta = NULL;
  186. /* 0-fill queue descriptor structure */
  187. memset(txq, 0, sizeof(*txq));
  188. }
  189. EXPORT_SYMBOL(iwl_tx_queue_free);
  190. /**
  191. * iwl_cmd_queue_free - Deallocate DMA queue.
  192. * @txq: Transmit queue to deallocate.
  193. *
  194. * Empty queue by removing and destroying all BD's.
  195. * Free all buffers.
  196. * 0-fill, but do not free "txq" descriptor structure.
  197. */
  198. void iwl_cmd_queue_free(struct iwl_priv *priv)
  199. {
  200. struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  201. struct iwl_queue *q = &txq->q;
  202. struct device *dev = &priv->pci_dev->dev;
  203. int i;
  204. if (q->n_bd == 0)
  205. return;
  206. /* De-alloc array of command/tx buffers */
  207. for (i = 0; i <= TFD_CMD_SLOTS; i++)
  208. kfree(txq->cmd[i]);
  209. /* De-alloc circular buffer of TFDs */
  210. if (txq->q.n_bd)
  211. dma_free_coherent(dev, priv->hw_params.tfd_size * txq->q.n_bd,
  212. txq->tfds, txq->q.dma_addr);
  213. /* deallocate arrays */
  214. kfree(txq->cmd);
  215. kfree(txq->meta);
  216. txq->cmd = NULL;
  217. txq->meta = NULL;
  218. /* 0-fill queue descriptor structure */
  219. memset(txq, 0, sizeof(*txq));
  220. }
  221. EXPORT_SYMBOL(iwl_cmd_queue_free);
  222. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  223. * DMA services
  224. *
  225. * Theory of operation
  226. *
  227. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  228. * of buffer descriptors, each of which points to one or more data buffers for
  229. * the device to read from or fill. Driver and device exchange status of each
  230. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  231. * entries in each circular buffer, to protect against confusing empty and full
  232. * queue states.
  233. *
  234. * The device reads or writes the data in the queues via the device's several
  235. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  236. *
  237. * For Tx queue, there are low mark and high mark limits. If, after queuing
  238. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  239. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  240. * Tx queue resumed.
  241. *
  242. * See more detailed info in iwl-4965-hw.h.
  243. ***************************************************/
  244. int iwl_queue_space(const struct iwl_queue *q)
  245. {
  246. int s = q->read_ptr - q->write_ptr;
  247. if (q->read_ptr > q->write_ptr)
  248. s -= q->n_bd;
  249. if (s <= 0)
  250. s += q->n_window;
  251. /* keep some reserve to not confuse empty and full situations */
  252. s -= 2;
  253. if (s < 0)
  254. s = 0;
  255. return s;
  256. }
  257. EXPORT_SYMBOL(iwl_queue_space);
  258. /**
  259. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  260. */
  261. static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
  262. int count, int slots_num, u32 id)
  263. {
  264. q->n_bd = count;
  265. q->n_window = slots_num;
  266. q->id = id;
  267. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  268. * and iwl_queue_dec_wrap are broken. */
  269. BUG_ON(!is_power_of_2(count));
  270. /* slots_num must be power-of-two size, otherwise
  271. * get_cmd_index is broken. */
  272. BUG_ON(!is_power_of_2(slots_num));
  273. q->low_mark = q->n_window / 4;
  274. if (q->low_mark < 4)
  275. q->low_mark = 4;
  276. q->high_mark = q->n_window / 8;
  277. if (q->high_mark < 2)
  278. q->high_mark = 2;
  279. q->write_ptr = q->read_ptr = 0;
  280. q->last_read_ptr = 0;
  281. q->repeat_same_read_ptr = 0;
  282. return 0;
  283. }
  284. /**
  285. * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  286. */
  287. static int iwl_tx_queue_alloc(struct iwl_priv *priv,
  288. struct iwl_tx_queue *txq, u32 id)
  289. {
  290. struct device *dev = &priv->pci_dev->dev;
  291. size_t tfd_sz = priv->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
  292. /* Driver private data, only for Tx (not command) queues,
  293. * not shared with device. */
  294. if (id != IWL_CMD_QUEUE_NUM) {
  295. txq->txb = kmalloc(sizeof(txq->txb[0]) *
  296. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  297. if (!txq->txb) {
  298. IWL_ERR(priv, "kmalloc for auxiliary BD "
  299. "structures failed\n");
  300. goto error;
  301. }
  302. } else {
  303. txq->txb = NULL;
  304. }
  305. /* Circular buffer of transmit frame descriptors (TFDs),
  306. * shared with device */
  307. txq->tfds = dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr,
  308. GFP_KERNEL);
  309. if (!txq->tfds) {
  310. IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n", tfd_sz);
  311. goto error;
  312. }
  313. txq->q.id = id;
  314. return 0;
  315. error:
  316. kfree(txq->txb);
  317. txq->txb = NULL;
  318. return -ENOMEM;
  319. }
  320. /**
  321. * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
  322. */
  323. int iwl_tx_queue_init(struct iwl_priv *priv, struct iwl_tx_queue *txq,
  324. int slots_num, u32 txq_id)
  325. {
  326. int i, len;
  327. int ret;
  328. int actual_slots = slots_num;
  329. /*
  330. * Alloc buffer array for commands (Tx or other types of commands).
  331. * For the command queue (#4), allocate command space + one big
  332. * command for scan, since scan command is very huge; the system will
  333. * not have two scans at the same time, so only one is needed.
  334. * For normal Tx queues (all other queues), no super-size command
  335. * space is needed.
  336. */
  337. if (txq_id == IWL_CMD_QUEUE_NUM)
  338. actual_slots++;
  339. txq->meta = kzalloc(sizeof(struct iwl_cmd_meta) * actual_slots,
  340. GFP_KERNEL);
  341. txq->cmd = kzalloc(sizeof(struct iwl_device_cmd *) * actual_slots,
  342. GFP_KERNEL);
  343. if (!txq->meta || !txq->cmd)
  344. goto out_free_arrays;
  345. len = sizeof(struct iwl_device_cmd);
  346. for (i = 0; i < actual_slots; i++) {
  347. /* only happens for cmd queue */
  348. if (i == slots_num)
  349. len = IWL_MAX_CMD_SIZE;
  350. txq->cmd[i] = kmalloc(len, GFP_KERNEL);
  351. if (!txq->cmd[i])
  352. goto err;
  353. }
  354. /* Alloc driver data array and TFD circular buffer */
  355. ret = iwl_tx_queue_alloc(priv, txq, txq_id);
  356. if (ret)
  357. goto err;
  358. txq->need_update = 0;
  359. /*
  360. * Aggregation TX queues will get their ID when aggregation begins;
  361. * they overwrite the setting done here. The command FIFO doesn't
  362. * need an swq_id so don't set one to catch errors, all others can
  363. * be set up to the identity mapping.
  364. */
  365. if (txq_id != IWL_CMD_QUEUE_NUM)
  366. txq->swq_id = txq_id;
  367. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  368. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  369. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  370. /* Initialize queue's high/low-water marks, and head/tail indexes */
  371. iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  372. /* Tell device where to find queue */
  373. priv->cfg->ops->lib->txq_init(priv, txq);
  374. return 0;
  375. err:
  376. for (i = 0; i < actual_slots; i++)
  377. kfree(txq->cmd[i]);
  378. out_free_arrays:
  379. kfree(txq->meta);
  380. kfree(txq->cmd);
  381. return -ENOMEM;
  382. }
  383. EXPORT_SYMBOL(iwl_tx_queue_init);
  384. /**
  385. * iwl_hw_txq_ctx_free - Free TXQ Context
  386. *
  387. * Destroy all TX DMA queues and structures
  388. */
  389. void iwl_hw_txq_ctx_free(struct iwl_priv *priv)
  390. {
  391. int txq_id;
  392. /* Tx queues */
  393. if (priv->txq) {
  394. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num;
  395. txq_id++)
  396. if (txq_id == IWL_CMD_QUEUE_NUM)
  397. iwl_cmd_queue_free(priv);
  398. else
  399. iwl_tx_queue_free(priv, txq_id);
  400. }
  401. iwl_free_dma_ptr(priv, &priv->kw);
  402. iwl_free_dma_ptr(priv, &priv->scd_bc_tbls);
  403. /* free tx queue structure */
  404. iwl_free_txq_mem(priv);
  405. }
  406. EXPORT_SYMBOL(iwl_hw_txq_ctx_free);
  407. /**
  408. * iwl_txq_ctx_reset - Reset TX queue context
  409. * Destroys all DMA structures and initialize them again
  410. *
  411. * @param priv
  412. * @return error code
  413. */
  414. int iwl_txq_ctx_reset(struct iwl_priv *priv)
  415. {
  416. int ret = 0;
  417. int txq_id, slots_num;
  418. unsigned long flags;
  419. /* Free all tx/cmd queues and keep-warm buffer */
  420. iwl_hw_txq_ctx_free(priv);
  421. ret = iwl_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
  422. priv->hw_params.scd_bc_tbls_size);
  423. if (ret) {
  424. IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
  425. goto error_bc_tbls;
  426. }
  427. /* Alloc keep-warm buffer */
  428. ret = iwl_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
  429. if (ret) {
  430. IWL_ERR(priv, "Keep Warm allocation failed\n");
  431. goto error_kw;
  432. }
  433. /* allocate tx queue structure */
  434. ret = iwl_alloc_txq_mem(priv);
  435. if (ret)
  436. goto error;
  437. spin_lock_irqsave(&priv->lock, flags);
  438. /* Turn off all Tx DMA fifos */
  439. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  440. /* Tell NIC where to find the "keep warm" buffer */
  441. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  442. spin_unlock_irqrestore(&priv->lock, flags);
  443. /* Alloc and init all Tx queues, including the command queue (#4) */
  444. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  445. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  446. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  447. ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  448. txq_id);
  449. if (ret) {
  450. IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
  451. goto error;
  452. }
  453. }
  454. return ret;
  455. error:
  456. iwl_hw_txq_ctx_free(priv);
  457. iwl_free_dma_ptr(priv, &priv->kw);
  458. error_kw:
  459. iwl_free_dma_ptr(priv, &priv->scd_bc_tbls);
  460. error_bc_tbls:
  461. return ret;
  462. }
  463. /**
  464. * iwl_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
  465. */
  466. void iwl_txq_ctx_stop(struct iwl_priv *priv)
  467. {
  468. int ch;
  469. unsigned long flags;
  470. /* Turn off all Tx DMA fifos */
  471. spin_lock_irqsave(&priv->lock, flags);
  472. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  473. /* Stop each Tx DMA channel, and wait for it to be idle */
  474. for (ch = 0; ch < priv->hw_params.dma_chnl_num; ch++) {
  475. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  476. iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  477. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  478. 1000);
  479. }
  480. spin_unlock_irqrestore(&priv->lock, flags);
  481. /* Deallocate memory for all Tx queues */
  482. iwl_hw_txq_ctx_free(priv);
  483. }
  484. EXPORT_SYMBOL(iwl_txq_ctx_stop);
  485. /*
  486. * handle build REPLY_TX command notification.
  487. */
  488. static void iwl_tx_cmd_build_basic(struct iwl_priv *priv,
  489. struct iwl_tx_cmd *tx_cmd,
  490. struct ieee80211_tx_info *info,
  491. struct ieee80211_hdr *hdr,
  492. u8 std_id)
  493. {
  494. __le16 fc = hdr->frame_control;
  495. __le32 tx_flags = tx_cmd->tx_flags;
  496. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  497. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  498. tx_flags |= TX_CMD_FLG_ACK_MSK;
  499. if (ieee80211_is_mgmt(fc))
  500. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  501. if (ieee80211_is_probe_resp(fc) &&
  502. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  503. tx_flags |= TX_CMD_FLG_TSF_MSK;
  504. } else {
  505. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  506. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  507. }
  508. if (ieee80211_is_back_req(fc))
  509. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  510. tx_cmd->sta_id = std_id;
  511. if (ieee80211_has_morefrags(fc))
  512. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  513. if (ieee80211_is_data_qos(fc)) {
  514. u8 *qc = ieee80211_get_qos_ctl(hdr);
  515. tx_cmd->tid_tspec = qc[0] & 0xf;
  516. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  517. } else {
  518. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  519. }
  520. priv->cfg->ops->utils->rts_tx_cmd_flag(info, &tx_flags);
  521. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  522. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  523. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  524. if (ieee80211_is_mgmt(fc)) {
  525. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  526. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  527. else
  528. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  529. } else {
  530. tx_cmd->timeout.pm_frame_timeout = 0;
  531. }
  532. tx_cmd->driver_txop = 0;
  533. tx_cmd->tx_flags = tx_flags;
  534. tx_cmd->next_frame_len = 0;
  535. }
  536. #define RTS_DFAULT_RETRY_LIMIT 60
  537. static void iwl_tx_cmd_build_rate(struct iwl_priv *priv,
  538. struct iwl_tx_cmd *tx_cmd,
  539. struct ieee80211_tx_info *info,
  540. __le16 fc)
  541. {
  542. u32 rate_flags;
  543. int rate_idx;
  544. u8 rts_retry_limit;
  545. u8 data_retry_limit;
  546. u8 rate_plcp;
  547. /* Set retry limit on DATA packets and Probe Responses*/
  548. if (ieee80211_is_probe_resp(fc))
  549. data_retry_limit = 3;
  550. else
  551. data_retry_limit = IWL_DEFAULT_TX_RETRY;
  552. tx_cmd->data_retry_limit = data_retry_limit;
  553. /* Set retry limit on RTS packets */
  554. rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
  555. if (data_retry_limit < rts_retry_limit)
  556. rts_retry_limit = data_retry_limit;
  557. tx_cmd->rts_retry_limit = rts_retry_limit;
  558. /* DATA packets will use the uCode station table for rate/antenna
  559. * selection */
  560. if (ieee80211_is_data(fc)) {
  561. tx_cmd->initial_rate_index = 0;
  562. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  563. return;
  564. }
  565. /**
  566. * If the current TX rate stored in mac80211 has the MCS bit set, it's
  567. * not really a TX rate. Thus, we use the lowest supported rate for
  568. * this band. Also use the lowest supported rate if the stored rate
  569. * index is invalid.
  570. */
  571. rate_idx = info->control.rates[0].idx;
  572. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS ||
  573. (rate_idx < 0) || (rate_idx > IWL_RATE_COUNT_LEGACY))
  574. rate_idx = rate_lowest_index(&priv->bands[info->band],
  575. info->control.sta);
  576. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  577. if (info->band == IEEE80211_BAND_5GHZ)
  578. rate_idx += IWL_FIRST_OFDM_RATE;
  579. /* Get PLCP rate for tx_cmd->rate_n_flags */
  580. rate_plcp = iwl_rates[rate_idx].plcp;
  581. /* Zero out flags for this packet */
  582. rate_flags = 0;
  583. /* Set CCK flag as needed */
  584. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  585. rate_flags |= RATE_MCS_CCK_MSK;
  586. /* Set up RTS and CTS flags for certain packets */
  587. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  588. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  589. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  590. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  591. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  592. if (tx_cmd->tx_flags & TX_CMD_FLG_RTS_MSK) {
  593. tx_cmd->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  594. tx_cmd->tx_flags |= TX_CMD_FLG_CTS_MSK;
  595. }
  596. break;
  597. default:
  598. break;
  599. }
  600. /* Set up antennas */
  601. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant);
  602. rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
  603. /* Set the rate in the TX cmd */
  604. tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
  605. }
  606. static void iwl_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  607. struct ieee80211_tx_info *info,
  608. struct iwl_tx_cmd *tx_cmd,
  609. struct sk_buff *skb_frag,
  610. int sta_id)
  611. {
  612. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  613. switch (keyconf->alg) {
  614. case ALG_CCMP:
  615. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  616. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  617. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  618. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  619. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  620. break;
  621. case ALG_TKIP:
  622. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  623. ieee80211_get_tkip_key(keyconf, skb_frag,
  624. IEEE80211_TKIP_P2_KEY, tx_cmd->key);
  625. IWL_DEBUG_TX(priv, "tx_cmd with tkip hwcrypto\n");
  626. break;
  627. case ALG_WEP:
  628. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  629. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  630. if (keyconf->keylen == WEP_KEY_LEN_128)
  631. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  632. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  633. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  634. "with key %d\n", keyconf->keyidx);
  635. break;
  636. default:
  637. IWL_ERR(priv, "Unknown encode alg %d\n", keyconf->alg);
  638. break;
  639. }
  640. }
  641. /*
  642. * start REPLY_TX command process
  643. */
  644. int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  645. {
  646. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  647. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  648. struct ieee80211_sta *sta = info->control.sta;
  649. struct iwl_station_priv *sta_priv = NULL;
  650. struct iwl_tx_queue *txq;
  651. struct iwl_queue *q;
  652. struct iwl_device_cmd *out_cmd;
  653. struct iwl_cmd_meta *out_meta;
  654. struct iwl_tx_cmd *tx_cmd;
  655. int swq_id, txq_id;
  656. dma_addr_t phys_addr;
  657. dma_addr_t txcmd_phys;
  658. dma_addr_t scratch_phys;
  659. u16 len, len_org, firstlen, secondlen;
  660. u16 seq_number = 0;
  661. __le16 fc;
  662. u8 hdr_len;
  663. u8 sta_id;
  664. u8 wait_write_ptr = 0;
  665. u8 tid = 0;
  666. u8 *qc = NULL;
  667. unsigned long flags;
  668. spin_lock_irqsave(&priv->lock, flags);
  669. if (iwl_is_rfkill(priv)) {
  670. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  671. goto drop_unlock;
  672. }
  673. fc = hdr->frame_control;
  674. #ifdef CONFIG_IWLWIFI_DEBUG
  675. if (ieee80211_is_auth(fc))
  676. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  677. else if (ieee80211_is_assoc_req(fc))
  678. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  679. else if (ieee80211_is_reassoc_req(fc))
  680. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  681. #endif
  682. hdr_len = ieee80211_hdrlen(fc);
  683. /* Find (or create) index into station table for destination station */
  684. if (info->flags & IEEE80211_TX_CTL_INJECTED)
  685. sta_id = priv->hw_params.bcast_sta_id;
  686. else
  687. sta_id = iwl_get_sta_id(priv, hdr);
  688. if (sta_id == IWL_INVALID_STATION) {
  689. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  690. hdr->addr1);
  691. goto drop_unlock;
  692. }
  693. IWL_DEBUG_TX(priv, "station Id %d\n", sta_id);
  694. if (sta)
  695. sta_priv = (void *)sta->drv_priv;
  696. if (sta_priv && sta_id != priv->hw_params.bcast_sta_id &&
  697. sta_priv->asleep) {
  698. WARN_ON(!(info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE));
  699. /*
  700. * This sends an asynchronous command to the device,
  701. * but we can rely on it being processed before the
  702. * next frame is processed -- and the next frame to
  703. * this station is the one that will consume this
  704. * counter.
  705. * For now set the counter to just 1 since we do not
  706. * support uAPSD yet.
  707. */
  708. iwl_sta_modify_sleep_tx_count(priv, sta_id, 1);
  709. }
  710. txq_id = get_queue_from_ac(skb_get_queue_mapping(skb));
  711. if (ieee80211_is_data_qos(fc)) {
  712. qc = ieee80211_get_qos_ctl(hdr);
  713. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  714. if (unlikely(tid >= MAX_TID_COUNT))
  715. goto drop_unlock;
  716. seq_number = priv->stations[sta_id].tid[tid].seq_number;
  717. seq_number &= IEEE80211_SCTL_SEQ;
  718. hdr->seq_ctrl = hdr->seq_ctrl &
  719. cpu_to_le16(IEEE80211_SCTL_FRAG);
  720. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  721. seq_number += 0x10;
  722. /* aggregation is on for this <sta,tid> */
  723. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  724. priv->stations[sta_id].tid[tid].agg.state == IWL_AGG_ON) {
  725. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  726. }
  727. }
  728. txq = &priv->txq[txq_id];
  729. swq_id = txq->swq_id;
  730. q = &txq->q;
  731. if (unlikely(iwl_queue_space(q) < q->high_mark))
  732. goto drop_unlock;
  733. if (ieee80211_is_data_qos(fc))
  734. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  735. /* Set up driver data for this TFD */
  736. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  737. txq->txb[q->write_ptr].skb[0] = skb;
  738. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  739. out_cmd = txq->cmd[q->write_ptr];
  740. out_meta = &txq->meta[q->write_ptr];
  741. tx_cmd = &out_cmd->cmd.tx;
  742. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  743. memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
  744. /*
  745. * Set up the Tx-command (not MAC!) header.
  746. * Store the chosen Tx queue and TFD index within the sequence field;
  747. * after Tx, uCode's Tx response will return this value so driver can
  748. * locate the frame within the tx queue and do post-tx processing.
  749. */
  750. out_cmd->hdr.cmd = REPLY_TX;
  751. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  752. INDEX_TO_SEQ(q->write_ptr)));
  753. /* Copy MAC header from skb into command buffer */
  754. memcpy(tx_cmd->hdr, hdr, hdr_len);
  755. /* Total # bytes to be transmitted */
  756. len = (u16)skb->len;
  757. tx_cmd->len = cpu_to_le16(len);
  758. if (info->control.hw_key)
  759. iwl_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
  760. /* TODO need this for burst mode later on */
  761. iwl_tx_cmd_build_basic(priv, tx_cmd, info, hdr, sta_id);
  762. iwl_dbg_log_tx_data_frame(priv, len, hdr);
  763. iwl_tx_cmd_build_rate(priv, tx_cmd, info, fc);
  764. iwl_update_stats(priv, true, fc, len);
  765. /*
  766. * Use the first empty entry in this queue's command buffer array
  767. * to contain the Tx command and MAC header concatenated together
  768. * (payload data will be in another buffer).
  769. * Size of this varies, due to varying MAC header length.
  770. * If end is not dword aligned, we'll have 2 extra bytes at the end
  771. * of the MAC header (device reads on dword boundaries).
  772. * We'll tell device about this padding later.
  773. */
  774. len = sizeof(struct iwl_tx_cmd) +
  775. sizeof(struct iwl_cmd_header) + hdr_len;
  776. len_org = len;
  777. firstlen = len = (len + 3) & ~3;
  778. if (len_org != len)
  779. len_org = 1;
  780. else
  781. len_org = 0;
  782. /* Tell NIC about any 2-byte padding after MAC header */
  783. if (len_org)
  784. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  785. /* Physical address of this Tx command's header (not MAC header!),
  786. * within command buffer array. */
  787. txcmd_phys = pci_map_single(priv->pci_dev,
  788. &out_cmd->hdr, len,
  789. PCI_DMA_BIDIRECTIONAL);
  790. pci_unmap_addr_set(out_meta, mapping, txcmd_phys);
  791. pci_unmap_len_set(out_meta, len, len);
  792. /* Add buffer containing Tx command and MAC(!) header to TFD's
  793. * first entry */
  794. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  795. txcmd_phys, len, 1, 0);
  796. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  797. txq->need_update = 1;
  798. if (qc)
  799. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  800. } else {
  801. wait_write_ptr = 1;
  802. txq->need_update = 0;
  803. }
  804. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  805. * if any (802.11 null frames have no payload). */
  806. secondlen = len = skb->len - hdr_len;
  807. if (len) {
  808. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  809. len, PCI_DMA_TODEVICE);
  810. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  811. phys_addr, len,
  812. 0, 0);
  813. }
  814. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  815. offsetof(struct iwl_tx_cmd, scratch);
  816. len = sizeof(struct iwl_tx_cmd) +
  817. sizeof(struct iwl_cmd_header) + hdr_len;
  818. /* take back ownership of DMA buffer to enable update */
  819. pci_dma_sync_single_for_cpu(priv->pci_dev, txcmd_phys,
  820. len, PCI_DMA_BIDIRECTIONAL);
  821. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  822. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  823. IWL_DEBUG_TX(priv, "sequence nr = 0X%x \n",
  824. le16_to_cpu(out_cmd->hdr.sequence));
  825. IWL_DEBUG_TX(priv, "tx_flags = 0X%x \n", le32_to_cpu(tx_cmd->tx_flags));
  826. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  827. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  828. /* Set up entry for this TFD in Tx byte-count array */
  829. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  830. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq,
  831. le16_to_cpu(tx_cmd->len));
  832. pci_dma_sync_single_for_device(priv->pci_dev, txcmd_phys,
  833. len, PCI_DMA_BIDIRECTIONAL);
  834. trace_iwlwifi_dev_tx(priv,
  835. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  836. sizeof(struct iwl_tfd),
  837. &out_cmd->hdr, firstlen,
  838. skb->data + hdr_len, secondlen);
  839. /* Tell device the write index *just past* this latest filled TFD */
  840. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  841. iwl_txq_update_write_ptr(priv, txq);
  842. spin_unlock_irqrestore(&priv->lock, flags);
  843. /*
  844. * At this point the frame is "transmitted" successfully
  845. * and we will get a TX status notification eventually,
  846. * regardless of the value of ret. "ret" only indicates
  847. * whether or not we should update the write pointer.
  848. */
  849. /* avoid atomic ops if it isn't an associated client */
  850. if (sta_priv && sta_priv->client)
  851. atomic_inc(&sta_priv->pending_frames);
  852. if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
  853. if (wait_write_ptr) {
  854. spin_lock_irqsave(&priv->lock, flags);
  855. txq->need_update = 1;
  856. iwl_txq_update_write_ptr(priv, txq);
  857. spin_unlock_irqrestore(&priv->lock, flags);
  858. } else {
  859. iwl_stop_queue(priv, txq->swq_id);
  860. }
  861. }
  862. return 0;
  863. drop_unlock:
  864. spin_unlock_irqrestore(&priv->lock, flags);
  865. return -1;
  866. }
  867. EXPORT_SYMBOL(iwl_tx_skb);
  868. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  869. /**
  870. * iwl_enqueue_hcmd - enqueue a uCode command
  871. * @priv: device private data point
  872. * @cmd: a point to the ucode command structure
  873. *
  874. * The function returns < 0 values to indicate the operation is
  875. * failed. On success, it turns the index (> 0) of command in the
  876. * command queue.
  877. */
  878. int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  879. {
  880. struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  881. struct iwl_queue *q = &txq->q;
  882. struct iwl_device_cmd *out_cmd;
  883. struct iwl_cmd_meta *out_meta;
  884. dma_addr_t phys_addr;
  885. unsigned long flags;
  886. int len;
  887. u32 idx;
  888. u16 fix_size;
  889. cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
  890. fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  891. /* If any of the command structures end up being larger than
  892. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  893. * we will need to increase the size of the TFD entries
  894. * Also, check to see if command buffer should not exceed the size
  895. * of device_cmd and max_cmd_size. */
  896. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  897. !(cmd->flags & CMD_SIZE_HUGE));
  898. BUG_ON(fix_size > IWL_MAX_CMD_SIZE);
  899. if (iwl_is_rfkill(priv) || iwl_is_ctkill(priv)) {
  900. IWL_WARN(priv, "Not sending command - %s KILL\n",
  901. iwl_is_rfkill(priv) ? "RF" : "CT");
  902. return -EIO;
  903. }
  904. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  905. IWL_ERR(priv, "No space in command queue\n");
  906. if (iwl_within_ct_kill_margin(priv))
  907. iwl_tt_enter_ct_kill(priv);
  908. else {
  909. IWL_ERR(priv, "Restarting adapter due to queue full\n");
  910. queue_work(priv->workqueue, &priv->restart);
  911. }
  912. return -ENOSPC;
  913. }
  914. spin_lock_irqsave(&priv->hcmd_lock, flags);
  915. idx = get_cmd_index(q, q->write_ptr, cmd->flags & CMD_SIZE_HUGE);
  916. out_cmd = txq->cmd[idx];
  917. out_meta = &txq->meta[idx];
  918. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  919. out_meta->flags = cmd->flags;
  920. if (cmd->flags & CMD_WANT_SKB)
  921. out_meta->source = cmd;
  922. if (cmd->flags & CMD_ASYNC)
  923. out_meta->callback = cmd->callback;
  924. out_cmd->hdr.cmd = cmd->id;
  925. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  926. /* At this point, the out_cmd now has all of the incoming cmd
  927. * information */
  928. out_cmd->hdr.flags = 0;
  929. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
  930. INDEX_TO_SEQ(q->write_ptr));
  931. if (cmd->flags & CMD_SIZE_HUGE)
  932. out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
  933. len = sizeof(struct iwl_device_cmd);
  934. if (idx == TFD_CMD_SLOTS)
  935. len = IWL_MAX_CMD_SIZE;
  936. #ifdef CONFIG_IWLWIFI_DEBUG
  937. switch (out_cmd->hdr.cmd) {
  938. case REPLY_TX_LINK_QUALITY_CMD:
  939. case SENSITIVITY_CMD:
  940. IWL_DEBUG_HC_DUMP(priv, "Sending command %s (#%x), seq: 0x%04X, "
  941. "%d bytes at %d[%d]:%d\n",
  942. get_cmd_string(out_cmd->hdr.cmd),
  943. out_cmd->hdr.cmd,
  944. le16_to_cpu(out_cmd->hdr.sequence), fix_size,
  945. q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  946. break;
  947. default:
  948. IWL_DEBUG_HC(priv, "Sending command %s (#%x), seq: 0x%04X, "
  949. "%d bytes at %d[%d]:%d\n",
  950. get_cmd_string(out_cmd->hdr.cmd),
  951. out_cmd->hdr.cmd,
  952. le16_to_cpu(out_cmd->hdr.sequence), fix_size,
  953. q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  954. }
  955. #endif
  956. txq->need_update = 1;
  957. if (priv->cfg->ops->lib->txq_update_byte_cnt_tbl)
  958. /* Set up entry in queue's byte count circular buffer */
  959. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
  960. phys_addr = pci_map_single(priv->pci_dev, &out_cmd->hdr,
  961. fix_size, PCI_DMA_BIDIRECTIONAL);
  962. pci_unmap_addr_set(out_meta, mapping, phys_addr);
  963. pci_unmap_len_set(out_meta, len, fix_size);
  964. trace_iwlwifi_dev_hcmd(priv, &out_cmd->hdr, fix_size, cmd->flags);
  965. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  966. phys_addr, fix_size, 1,
  967. U32_PAD(cmd->len));
  968. /* Increment and update queue's write index */
  969. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  970. iwl_txq_update_write_ptr(priv, txq);
  971. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  972. return idx;
  973. }
  974. static void iwl_tx_status(struct iwl_priv *priv, struct sk_buff *skb)
  975. {
  976. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  977. struct ieee80211_sta *sta;
  978. struct iwl_station_priv *sta_priv;
  979. sta = ieee80211_find_sta(priv->vif, hdr->addr1);
  980. if (sta) {
  981. sta_priv = (void *)sta->drv_priv;
  982. /* avoid atomic ops if this isn't a client */
  983. if (sta_priv->client &&
  984. atomic_dec_return(&sta_priv->pending_frames) == 0)
  985. ieee80211_sta_block_awake(priv->hw, sta, false);
  986. }
  987. ieee80211_tx_status_irqsafe(priv->hw, skb);
  988. }
  989. int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  990. {
  991. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  992. struct iwl_queue *q = &txq->q;
  993. struct iwl_tx_info *tx_info;
  994. int nfreed = 0;
  995. struct ieee80211_hdr *hdr;
  996. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  997. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  998. "is out of range [0-%d] %d %d.\n", txq_id,
  999. index, q->n_bd, q->write_ptr, q->read_ptr);
  1000. return 0;
  1001. }
  1002. for (index = iwl_queue_inc_wrap(index, q->n_bd);
  1003. q->read_ptr != index;
  1004. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  1005. tx_info = &txq->txb[txq->q.read_ptr];
  1006. iwl_tx_status(priv, tx_info->skb[0]);
  1007. hdr = (struct ieee80211_hdr *)tx_info->skb[0]->data;
  1008. if (hdr && ieee80211_is_data_qos(hdr->frame_control))
  1009. nfreed++;
  1010. tx_info->skb[0] = NULL;
  1011. if (priv->cfg->ops->lib->txq_inval_byte_cnt_tbl)
  1012. priv->cfg->ops->lib->txq_inval_byte_cnt_tbl(priv, txq);
  1013. priv->cfg->ops->lib->txq_free_tfd(priv, txq);
  1014. }
  1015. return nfreed;
  1016. }
  1017. EXPORT_SYMBOL(iwl_tx_queue_reclaim);
  1018. /**
  1019. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  1020. *
  1021. * When FW advances 'R' index, all entries between old and new 'R' index
  1022. * need to be reclaimed. As result, some free space forms. If there is
  1023. * enough free space (> low mark), wake the stack that feeds us.
  1024. */
  1025. static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id,
  1026. int idx, int cmd_idx)
  1027. {
  1028. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  1029. struct iwl_queue *q = &txq->q;
  1030. int nfreed = 0;
  1031. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  1032. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  1033. "is out of range [0-%d] %d %d.\n", txq_id,
  1034. idx, q->n_bd, q->write_ptr, q->read_ptr);
  1035. return;
  1036. }
  1037. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  1038. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  1039. if (nfreed++ > 0) {
  1040. IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", idx,
  1041. q->write_ptr, q->read_ptr);
  1042. queue_work(priv->workqueue, &priv->restart);
  1043. }
  1044. }
  1045. }
  1046. /**
  1047. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  1048. * @rxb: Rx buffer to reclaim
  1049. *
  1050. * If an Rx buffer has an async callback associated with it the callback
  1051. * will be executed. The attached skb (if present) will only be freed
  1052. * if the callback returns 1
  1053. */
  1054. void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  1055. {
  1056. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1057. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1058. int txq_id = SEQ_TO_QUEUE(sequence);
  1059. int index = SEQ_TO_INDEX(sequence);
  1060. int cmd_index;
  1061. bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
  1062. struct iwl_device_cmd *cmd;
  1063. struct iwl_cmd_meta *meta;
  1064. /* If a Tx command is being handled and it isn't in the actual
  1065. * command queue then there a command routing bug has been introduced
  1066. * in the queue management code. */
  1067. if (WARN(txq_id != IWL_CMD_QUEUE_NUM,
  1068. "wrong command queue %d, sequence 0x%X readp=%d writep=%d\n",
  1069. txq_id, sequence,
  1070. priv->txq[IWL_CMD_QUEUE_NUM].q.read_ptr,
  1071. priv->txq[IWL_CMD_QUEUE_NUM].q.write_ptr)) {
  1072. iwl_print_hex_error(priv, pkt, 32);
  1073. return;
  1074. }
  1075. cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
  1076. cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
  1077. meta = &priv->txq[IWL_CMD_QUEUE_NUM].meta[cmd_index];
  1078. pci_unmap_single(priv->pci_dev,
  1079. pci_unmap_addr(meta, mapping),
  1080. pci_unmap_len(meta, len),
  1081. PCI_DMA_BIDIRECTIONAL);
  1082. /* Input error checking is done when commands are added to queue. */
  1083. if (meta->flags & CMD_WANT_SKB) {
  1084. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  1085. rxb->page = NULL;
  1086. } else if (meta->callback)
  1087. meta->callback(priv, cmd, pkt);
  1088. iwl_hcmd_queue_reclaim(priv, txq_id, index, cmd_index);
  1089. if (!(meta->flags & CMD_ASYNC)) {
  1090. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  1091. IWL_DEBUG_INFO(priv, "Clearing HCMD_ACTIVE for command %s \n",
  1092. get_cmd_string(cmd->hdr.cmd));
  1093. wake_up_interruptible(&priv->wait_command_queue);
  1094. }
  1095. }
  1096. EXPORT_SYMBOL(iwl_tx_cmd_complete);
  1097. /*
  1098. * Find first available (lowest unused) Tx Queue, mark it "active".
  1099. * Called only when finding queue for aggregation.
  1100. * Should never return anything < 7, because they should already
  1101. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  1102. */
  1103. static int iwl_txq_ctx_activate_free(struct iwl_priv *priv)
  1104. {
  1105. int txq_id;
  1106. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  1107. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  1108. return txq_id;
  1109. return -1;
  1110. }
  1111. int iwl_tx_agg_start(struct iwl_priv *priv, const u8 *ra, u16 tid, u16 *ssn)
  1112. {
  1113. int sta_id;
  1114. int tx_fifo;
  1115. int txq_id;
  1116. int ret;
  1117. unsigned long flags;
  1118. struct iwl_tid_data *tid_data;
  1119. tx_fifo = get_fifo_from_tid(tid);
  1120. if (unlikely(tx_fifo < 0))
  1121. return tx_fifo;
  1122. IWL_WARN(priv, "%s on ra = %pM tid = %d\n",
  1123. __func__, ra, tid);
  1124. sta_id = iwl_find_station(priv, ra);
  1125. if (sta_id == IWL_INVALID_STATION) {
  1126. IWL_ERR(priv, "Start AGG on invalid station\n");
  1127. return -ENXIO;
  1128. }
  1129. if (unlikely(tid >= MAX_TID_COUNT))
  1130. return -EINVAL;
  1131. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
  1132. IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
  1133. return -ENXIO;
  1134. }
  1135. txq_id = iwl_txq_ctx_activate_free(priv);
  1136. if (txq_id == -1) {
  1137. IWL_ERR(priv, "No free aggregation queue available\n");
  1138. return -ENXIO;
  1139. }
  1140. spin_lock_irqsave(&priv->sta_lock, flags);
  1141. tid_data = &priv->stations[sta_id].tid[tid];
  1142. *ssn = SEQ_TO_SN(tid_data->seq_number);
  1143. tid_data->agg.txq_id = txq_id;
  1144. priv->txq[txq_id].swq_id = iwl_virtual_agg_queue_num(tx_fifo, txq_id);
  1145. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1146. ret = priv->cfg->ops->lib->txq_agg_enable(priv, txq_id, tx_fifo,
  1147. sta_id, tid, *ssn);
  1148. if (ret)
  1149. return ret;
  1150. if (tid_data->tfds_in_queue == 0) {
  1151. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  1152. tid_data->agg.state = IWL_AGG_ON;
  1153. ieee80211_start_tx_ba_cb_irqsafe(priv->vif, ra, tid);
  1154. } else {
  1155. IWL_DEBUG_HT(priv, "HW queue is NOT empty: %d packets in HW queue\n",
  1156. tid_data->tfds_in_queue);
  1157. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  1158. }
  1159. return ret;
  1160. }
  1161. EXPORT_SYMBOL(iwl_tx_agg_start);
  1162. int iwl_tx_agg_stop(struct iwl_priv *priv , const u8 *ra, u16 tid)
  1163. {
  1164. int tx_fifo_id, txq_id, sta_id, ssn = -1;
  1165. struct iwl_tid_data *tid_data;
  1166. int write_ptr, read_ptr;
  1167. unsigned long flags;
  1168. if (!ra) {
  1169. IWL_ERR(priv, "ra = NULL\n");
  1170. return -EINVAL;
  1171. }
  1172. tx_fifo_id = get_fifo_from_tid(tid);
  1173. if (unlikely(tx_fifo_id < 0))
  1174. return tx_fifo_id;
  1175. sta_id = iwl_find_station(priv, ra);
  1176. if (sta_id == IWL_INVALID_STATION) {
  1177. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  1178. return -ENXIO;
  1179. }
  1180. if (priv->stations[sta_id].tid[tid].agg.state ==
  1181. IWL_EMPTYING_HW_QUEUE_ADDBA) {
  1182. IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
  1183. ieee80211_stop_tx_ba_cb_irqsafe(priv->vif, ra, tid);
  1184. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  1185. return 0;
  1186. }
  1187. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
  1188. IWL_WARN(priv, "Stopping AGG while state not ON or starting\n");
  1189. tid_data = &priv->stations[sta_id].tid[tid];
  1190. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  1191. txq_id = tid_data->agg.txq_id;
  1192. write_ptr = priv->txq[txq_id].q.write_ptr;
  1193. read_ptr = priv->txq[txq_id].q.read_ptr;
  1194. /* The queue is not empty */
  1195. if (write_ptr != read_ptr) {
  1196. IWL_DEBUG_HT(priv, "Stopping a non empty AGG HW QUEUE\n");
  1197. priv->stations[sta_id].tid[tid].agg.state =
  1198. IWL_EMPTYING_HW_QUEUE_DELBA;
  1199. return 0;
  1200. }
  1201. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  1202. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  1203. spin_lock_irqsave(&priv->lock, flags);
  1204. /*
  1205. * the only reason this call can fail is queue number out of range,
  1206. * which can happen if uCode is reloaded and all the station
  1207. * information are lost. if it is outside the range, there is no need
  1208. * to deactivate the uCode queue, just return "success" to allow
  1209. * mac80211 to clean up it own data.
  1210. */
  1211. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id, ssn,
  1212. tx_fifo_id);
  1213. spin_unlock_irqrestore(&priv->lock, flags);
  1214. ieee80211_stop_tx_ba_cb_irqsafe(priv->vif, ra, tid);
  1215. return 0;
  1216. }
  1217. EXPORT_SYMBOL(iwl_tx_agg_stop);
  1218. int iwl_txq_check_empty(struct iwl_priv *priv, int sta_id, u8 tid, int txq_id)
  1219. {
  1220. struct iwl_queue *q = &priv->txq[txq_id].q;
  1221. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  1222. struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
  1223. switch (priv->stations[sta_id].tid[tid].agg.state) {
  1224. case IWL_EMPTYING_HW_QUEUE_DELBA:
  1225. /* We are reclaiming the last packet of the */
  1226. /* aggregated HW queue */
  1227. if ((txq_id == tid_data->agg.txq_id) &&
  1228. (q->read_ptr == q->write_ptr)) {
  1229. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  1230. int tx_fifo = get_fifo_from_tid(tid);
  1231. IWL_DEBUG_HT(priv, "HW queue empty: continue DELBA flow\n");
  1232. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id,
  1233. ssn, tx_fifo);
  1234. tid_data->agg.state = IWL_AGG_OFF;
  1235. ieee80211_stop_tx_ba_cb_irqsafe(priv->vif, addr, tid);
  1236. }
  1237. break;
  1238. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  1239. /* We are reclaiming the last packet of the queue */
  1240. if (tid_data->tfds_in_queue == 0) {
  1241. IWL_DEBUG_HT(priv, "HW queue empty: continue ADDBA flow\n");
  1242. tid_data->agg.state = IWL_AGG_ON;
  1243. ieee80211_start_tx_ba_cb_irqsafe(priv->vif, addr, tid);
  1244. }
  1245. break;
  1246. }
  1247. return 0;
  1248. }
  1249. EXPORT_SYMBOL(iwl_txq_check_empty);
  1250. /**
  1251. * iwl_tx_status_reply_compressed_ba - Update tx status from block-ack
  1252. *
  1253. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  1254. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  1255. */
  1256. static int iwl_tx_status_reply_compressed_ba(struct iwl_priv *priv,
  1257. struct iwl_ht_agg *agg,
  1258. struct iwl_compressed_ba_resp *ba_resp)
  1259. {
  1260. int i, sh, ack;
  1261. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  1262. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1263. u64 bitmap;
  1264. int successes = 0;
  1265. struct ieee80211_tx_info *info;
  1266. if (unlikely(!agg->wait_for_ba)) {
  1267. IWL_ERR(priv, "Received BA when not expected\n");
  1268. return -EINVAL;
  1269. }
  1270. /* Mark that the expected block-ack response arrived */
  1271. agg->wait_for_ba = 0;
  1272. IWL_DEBUG_TX_REPLY(priv, "BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
  1273. /* Calculate shift to align block-ack bits with our Tx window bits */
  1274. sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl >> 4);
  1275. if (sh < 0) /* tbw something is wrong with indices */
  1276. sh += 0x100;
  1277. /* don't use 64-bit values for now */
  1278. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  1279. if (agg->frame_count > (64 - sh)) {
  1280. IWL_DEBUG_TX_REPLY(priv, "more frames than bitmap size");
  1281. return -1;
  1282. }
  1283. /* check for success or failure according to the
  1284. * transmitted bitmap and block-ack bitmap */
  1285. bitmap &= agg->bitmap;
  1286. /* For each frame attempted in aggregation,
  1287. * update driver's record of tx frame's status. */
  1288. for (i = 0; i < agg->frame_count ; i++) {
  1289. ack = bitmap & (1ULL << i);
  1290. successes += !!ack;
  1291. IWL_DEBUG_TX_REPLY(priv, "%s ON i=%d idx=%d raw=%d\n",
  1292. ack ? "ACK" : "NACK", i, (agg->start_idx + i) & 0xff,
  1293. agg->start_idx + i);
  1294. }
  1295. info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb[0]);
  1296. memset(&info->status, 0, sizeof(info->status));
  1297. info->flags |= IEEE80211_TX_STAT_ACK;
  1298. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1299. info->status.ampdu_ack_map = successes;
  1300. info->status.ampdu_ack_len = agg->frame_count;
  1301. iwl_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
  1302. IWL_DEBUG_TX_REPLY(priv, "Bitmap %llx\n", (unsigned long long)bitmap);
  1303. return 0;
  1304. }
  1305. /**
  1306. * iwl_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  1307. *
  1308. * Handles block-acknowledge notification from device, which reports success
  1309. * of frames sent via aggregation.
  1310. */
  1311. void iwl_rx_reply_compressed_ba(struct iwl_priv *priv,
  1312. struct iwl_rx_mem_buffer *rxb)
  1313. {
  1314. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1315. struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  1316. struct iwl_tx_queue *txq = NULL;
  1317. struct iwl_ht_agg *agg;
  1318. int index;
  1319. int sta_id;
  1320. int tid;
  1321. /* "flow" corresponds to Tx queue */
  1322. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1323. /* "ssn" is start of block-ack Tx window, corresponds to index
  1324. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1325. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  1326. if (scd_flow >= priv->hw_params.max_txq_num) {
  1327. IWL_ERR(priv,
  1328. "BUG_ON scd_flow is bigger than number of queues\n");
  1329. return;
  1330. }
  1331. txq = &priv->txq[scd_flow];
  1332. sta_id = ba_resp->sta_id;
  1333. tid = ba_resp->tid;
  1334. agg = &priv->stations[sta_id].tid[tid].agg;
  1335. /* Find index just before block-ack window */
  1336. index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  1337. /* TODO: Need to get this copy more safely - now good for debug */
  1338. IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
  1339. "sta_id = %d\n",
  1340. agg->wait_for_ba,
  1341. (u8 *) &ba_resp->sta_addr_lo32,
  1342. ba_resp->sta_id);
  1343. IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
  1344. "%d, scd_ssn = %d\n",
  1345. ba_resp->tid,
  1346. ba_resp->seq_ctl,
  1347. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  1348. ba_resp->scd_flow,
  1349. ba_resp->scd_ssn);
  1350. IWL_DEBUG_TX_REPLY(priv, "DAT start_idx = %d, bitmap = 0x%llx \n",
  1351. agg->start_idx,
  1352. (unsigned long long)agg->bitmap);
  1353. /* Update driver's record of ACK vs. not for each frame in window */
  1354. iwl_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  1355. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  1356. * block-ack window (we assume that they've been successfully
  1357. * transmitted ... if not, it's too late anyway). */
  1358. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  1359. /* calculate mac80211 ampdu sw queue to wake */
  1360. int freed = iwl_tx_queue_reclaim(priv, scd_flow, index);
  1361. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  1362. if ((iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  1363. priv->mac80211_registered &&
  1364. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
  1365. iwl_wake_queue(priv, txq->swq_id);
  1366. iwl_txq_check_empty(priv, sta_id, tid, scd_flow);
  1367. }
  1368. }
  1369. EXPORT_SYMBOL(iwl_rx_reply_compressed_ba);
  1370. #ifdef CONFIG_IWLWIFI_DEBUG
  1371. #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
  1372. const char *iwl_get_tx_fail_reason(u32 status)
  1373. {
  1374. switch (status & TX_STATUS_MSK) {
  1375. case TX_STATUS_SUCCESS:
  1376. return "SUCCESS";
  1377. TX_STATUS_ENTRY(SHORT_LIMIT);
  1378. TX_STATUS_ENTRY(LONG_LIMIT);
  1379. TX_STATUS_ENTRY(FIFO_UNDERRUN);
  1380. TX_STATUS_ENTRY(MGMNT_ABORT);
  1381. TX_STATUS_ENTRY(NEXT_FRAG);
  1382. TX_STATUS_ENTRY(LIFE_EXPIRE);
  1383. TX_STATUS_ENTRY(DEST_PS);
  1384. TX_STATUS_ENTRY(ABORTED);
  1385. TX_STATUS_ENTRY(BT_RETRY);
  1386. TX_STATUS_ENTRY(STA_INVALID);
  1387. TX_STATUS_ENTRY(FRAG_DROPPED);
  1388. TX_STATUS_ENTRY(TID_DISABLE);
  1389. TX_STATUS_ENTRY(FRAME_FLUSHED);
  1390. TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
  1391. TX_STATUS_ENTRY(TX_LOCKED);
  1392. TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
  1393. }
  1394. return "UNKNOWN";
  1395. }
  1396. EXPORT_SYMBOL(iwl_get_tx_fail_reason);
  1397. #endif /* CONFIG_IWLWIFI_DEBUG */