iwl-tx.c 49 KB

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