iwl-tx.c 43 KB

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