iwl-tx.c 38 KB

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