iwl-tx.c 44 KB

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