iwl-tx.c 49 KB

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