iwl-tx.c 42 KB

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