iwl-tx.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. u16 fc = le16_to_cpu(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 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
  490. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  491. if (ieee80211_is_probe_response(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_request(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_get_morefrag(hdr))
  502. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  503. if (ieee80211_is_qos_data(fc)) {
  504. u8 *qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
  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. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  511. tx_flags |= TX_CMD_FLG_RTS_MSK;
  512. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  513. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  514. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  515. tx_flags |= TX_CMD_FLG_CTS_MSK;
  516. }
  517. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  518. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  519. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  520. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
  521. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
  522. (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
  523. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  524. else
  525. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  526. } else {
  527. tx_cmd->timeout.pm_frame_timeout = 0;
  528. }
  529. tx_cmd->driver_txop = 0;
  530. tx_cmd->tx_flags = tx_flags;
  531. tx_cmd->next_frame_len = 0;
  532. }
  533. #define RTS_HCCA_RETRY_LIMIT 3
  534. #define RTS_DFAULT_RETRY_LIMIT 60
  535. static void iwl_tx_cmd_build_rate(struct iwl_priv *priv,
  536. struct iwl_tx_cmd *tx_cmd,
  537. struct ieee80211_tx_info *info,
  538. u16 fc, int sta_id,
  539. int is_hcca)
  540. {
  541. u8 rts_retry_limit = 0;
  542. u8 data_retry_limit = 0;
  543. u8 rate_plcp;
  544. u16 rate_flags = 0;
  545. int rate_idx;
  546. rate_idx = min(ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xffff,
  547. IWL_RATE_COUNT - 1);
  548. rate_plcp = iwl_rates[rate_idx].plcp;
  549. rts_retry_limit = (is_hcca) ?
  550. RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
  551. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  552. rate_flags |= RATE_MCS_CCK_MSK;
  553. if (ieee80211_is_probe_response(fc)) {
  554. data_retry_limit = 3;
  555. if (data_retry_limit < rts_retry_limit)
  556. rts_retry_limit = data_retry_limit;
  557. } else
  558. data_retry_limit = IWL_DEFAULT_TX_RETRY;
  559. if (priv->data_retry_limit != -1)
  560. data_retry_limit = priv->data_retry_limit;
  561. if (ieee80211_is_data(fc)) {
  562. tx_cmd->initial_rate_index = 0;
  563. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  564. } else {
  565. switch (fc & IEEE80211_FCTL_STYPE) {
  566. case IEEE80211_STYPE_AUTH:
  567. case IEEE80211_STYPE_DEAUTH:
  568. case IEEE80211_STYPE_ASSOC_REQ:
  569. case IEEE80211_STYPE_REASSOC_REQ:
  570. if (tx_cmd->tx_flags & TX_CMD_FLG_RTS_MSK) {
  571. tx_cmd->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  572. tx_cmd->tx_flags |= TX_CMD_FLG_CTS_MSK;
  573. }
  574. break;
  575. default:
  576. break;
  577. }
  578. /* Alternate between antenna A and B for successive frames */
  579. if (priv->use_ant_b_for_management_frame) {
  580. priv->use_ant_b_for_management_frame = 0;
  581. rate_flags |= RATE_MCS_ANT_B_MSK;
  582. } else {
  583. priv->use_ant_b_for_management_frame = 1;
  584. rate_flags |= RATE_MCS_ANT_A_MSK;
  585. }
  586. }
  587. tx_cmd->rts_retry_limit = rts_retry_limit;
  588. tx_cmd->data_retry_limit = data_retry_limit;
  589. tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
  590. }
  591. static void iwl_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  592. struct ieee80211_tx_info *info,
  593. struct iwl_tx_cmd *tx_cmd,
  594. struct sk_buff *skb_frag,
  595. int sta_id)
  596. {
  597. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  598. switch (keyconf->alg) {
  599. case ALG_CCMP:
  600. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  601. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  602. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  603. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  604. IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
  605. break;
  606. case ALG_TKIP:
  607. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  608. ieee80211_get_tkip_key(keyconf, skb_frag,
  609. IEEE80211_TKIP_P2_KEY, tx_cmd->key);
  610. IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
  611. break;
  612. case ALG_WEP:
  613. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  614. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  615. if (keyconf->keylen == WEP_KEY_LEN_128)
  616. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  617. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  618. IWL_DEBUG_TX("Configuring packet for WEP encryption "
  619. "with key %d\n", keyconf->keyidx);
  620. break;
  621. default:
  622. printk(KERN_ERR "Unknown encode alg %d\n", keyconf->alg);
  623. break;
  624. }
  625. }
  626. static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  627. {
  628. /* 0 - mgmt, 1 - cnt, 2 - data */
  629. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  630. priv->tx_stats[idx].cnt++;
  631. priv->tx_stats[idx].bytes += len;
  632. }
  633. /*
  634. * start REPLY_TX command process
  635. */
  636. int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  637. {
  638. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  639. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  640. struct iwl_tfd_frame *tfd;
  641. u32 *control_flags;
  642. int txq_id = skb_get_queue_mapping(skb);
  643. struct iwl_tx_queue *txq = NULL;
  644. struct iwl_queue *q = NULL;
  645. dma_addr_t phys_addr;
  646. dma_addr_t txcmd_phys;
  647. dma_addr_t scratch_phys;
  648. struct iwl_cmd *out_cmd = NULL;
  649. struct iwl_tx_cmd *tx_cmd;
  650. u16 len, idx, len_org;
  651. u16 seq_number = 0;
  652. u8 id, hdr_len, unicast;
  653. u8 sta_id;
  654. u16 fc;
  655. u8 wait_write_ptr = 0;
  656. u8 tid = 0;
  657. u8 *qc = NULL;
  658. unsigned long flags;
  659. int ret;
  660. spin_lock_irqsave(&priv->lock, flags);
  661. if (iwl_is_rfkill(priv)) {
  662. IWL_DEBUG_DROP("Dropping - RF KILL\n");
  663. goto drop_unlock;
  664. }
  665. if (!priv->vif) {
  666. IWL_DEBUG_DROP("Dropping - !priv->vif\n");
  667. goto drop_unlock;
  668. }
  669. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) ==
  670. IWL_INVALID_RATE) {
  671. IWL_ERROR("ERROR: No TX rate available.\n");
  672. goto drop_unlock;
  673. }
  674. unicast = !is_multicast_ether_addr(hdr->addr1);
  675. id = 0;
  676. fc = le16_to_cpu(hdr->frame_control);
  677. #ifdef CONFIG_IWLWIFI_DEBUG
  678. if (ieee80211_is_auth(fc))
  679. IWL_DEBUG_TX("Sending AUTH frame\n");
  680. else if (ieee80211_is_assoc_request(fc))
  681. IWL_DEBUG_TX("Sending ASSOC frame\n");
  682. else if (ieee80211_is_reassoc_request(fc))
  683. IWL_DEBUG_TX("Sending REASSOC frame\n");
  684. #endif
  685. /* drop all data frame if we are not associated */
  686. if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  687. (!iwl_is_associated(priv) ||
  688. ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
  689. !priv->assoc_station_added)) {
  690. IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
  691. goto drop_unlock;
  692. }
  693. spin_unlock_irqrestore(&priv->lock, flags);
  694. hdr_len = ieee80211_get_hdrlen(fc);
  695. /* Find (or create) index into station table for destination station */
  696. sta_id = iwl_get_sta_id(priv, hdr);
  697. if (sta_id == IWL_INVALID_STATION) {
  698. DECLARE_MAC_BUF(mac);
  699. IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
  700. print_mac(mac, hdr->addr1));
  701. goto drop;
  702. }
  703. IWL_DEBUG_TX("station Id %d\n", sta_id);
  704. if (ieee80211_is_qos_data(fc)) {
  705. qc = ieee80211_get_qos_ctrl(hdr, hdr_len);
  706. tid = qc[0] & 0xf;
  707. seq_number = priv->stations[sta_id].tid[tid].seq_number &
  708. IEEE80211_SCTL_SEQ;
  709. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  710. (hdr->seq_ctrl &
  711. __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
  712. seq_number += 0x10;
  713. /* aggregation is on for this <sta,tid> */
  714. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  715. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  716. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  717. }
  718. /* Descriptor for chosen Tx queue */
  719. txq = &priv->txq[txq_id];
  720. q = &txq->q;
  721. spin_lock_irqsave(&priv->lock, flags);
  722. /* Set up first empty TFD within this queue's circular TFD buffer */
  723. tfd = &txq->bd[q->write_ptr];
  724. memset(tfd, 0, sizeof(*tfd));
  725. control_flags = (u32 *) tfd;
  726. idx = get_cmd_index(q, q->write_ptr, 0);
  727. /* Set up driver data for this TFD */
  728. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  729. txq->txb[q->write_ptr].skb[0] = skb;
  730. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  731. out_cmd = &txq->cmd[idx];
  732. tx_cmd = &out_cmd->cmd.tx;
  733. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  734. memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
  735. /*
  736. * Set up the Tx-command (not MAC!) header.
  737. * Store the chosen Tx queue and TFD index within the sequence field;
  738. * after Tx, uCode's Tx response will return this value so driver can
  739. * locate the frame within the tx queue and do post-tx processing.
  740. */
  741. out_cmd->hdr.cmd = REPLY_TX;
  742. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  743. INDEX_TO_SEQ(q->write_ptr)));
  744. /* Copy MAC header from skb into command buffer */
  745. memcpy(tx_cmd->hdr, hdr, hdr_len);
  746. /*
  747. * Use the first empty entry in this queue's command buffer array
  748. * to contain the Tx command and MAC header concatenated together
  749. * (payload data will be in another buffer).
  750. * Size of this varies, due to varying MAC header length.
  751. * If end is not dword aligned, we'll have 2 extra bytes at the end
  752. * of the MAC header (device reads on dword boundaries).
  753. * We'll tell device about this padding later.
  754. */
  755. len = sizeof(struct iwl_tx_cmd) +
  756. sizeof(struct iwl_cmd_header) + hdr_len;
  757. len_org = len;
  758. len = (len + 3) & ~3;
  759. if (len_org != len)
  760. len_org = 1;
  761. else
  762. len_org = 0;
  763. /* Physical address of this Tx command's header (not MAC header!),
  764. * within command buffer array. */
  765. txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
  766. offsetof(struct iwl_cmd, hdr);
  767. /* Add buffer containing Tx command and MAC(!) header to TFD's
  768. * first entry */
  769. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
  770. if (!(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT))
  771. iwl_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
  772. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  773. * if any (802.11 null frames have no payload). */
  774. len = skb->len - hdr_len;
  775. if (len) {
  776. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  777. len, PCI_DMA_TODEVICE);
  778. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
  779. }
  780. /* Tell NIC about any 2-byte padding after MAC header */
  781. if (len_org)
  782. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  783. /* Total # bytes to be transmitted */
  784. len = (u16)skb->len;
  785. tx_cmd->len = cpu_to_le16(len);
  786. /* TODO need this for burst mode later on */
  787. iwl_tx_cmd_build_basic(priv, tx_cmd, info, hdr, unicast, sta_id);
  788. /* set is_hcca to 0; it probably will never be implemented */
  789. iwl_tx_cmd_build_rate(priv, tx_cmd, info, fc, sta_id, 0);
  790. iwl_update_tx_stats(priv, fc, len);
  791. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  792. offsetof(struct iwl_tx_cmd, scratch);
  793. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  794. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
  795. if (!ieee80211_get_morefrag(hdr)) {
  796. txq->need_update = 1;
  797. if (qc)
  798. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  799. } else {
  800. wait_write_ptr = 1;
  801. txq->need_update = 0;
  802. }
  803. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  804. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  805. /* Set up entry for this TFD in Tx byte-count array */
  806. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
  807. /* Tell device the write index *just past* this latest filled TFD */
  808. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  809. ret = iwl_txq_update_write_ptr(priv, txq);
  810. spin_unlock_irqrestore(&priv->lock, flags);
  811. if (ret)
  812. return ret;
  813. if ((iwl_queue_space(q) < q->high_mark)
  814. && priv->mac80211_registered) {
  815. if (wait_write_ptr) {
  816. spin_lock_irqsave(&priv->lock, flags);
  817. txq->need_update = 1;
  818. iwl_txq_update_write_ptr(priv, txq);
  819. spin_unlock_irqrestore(&priv->lock, flags);
  820. }
  821. ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
  822. }
  823. return 0;
  824. drop_unlock:
  825. spin_unlock_irqrestore(&priv->lock, flags);
  826. drop:
  827. return -1;
  828. }
  829. EXPORT_SYMBOL(iwl_tx_skb);
  830. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  831. /**
  832. * iwl_enqueue_hcmd - enqueue a uCode command
  833. * @priv: device private data point
  834. * @cmd: a point to the ucode command structure
  835. *
  836. * The function returns < 0 values to indicate the operation is
  837. * failed. On success, it turns the index (> 0) of command in the
  838. * command queue.
  839. */
  840. int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  841. {
  842. struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  843. struct iwl_queue *q = &txq->q;
  844. struct iwl_tfd_frame *tfd;
  845. u32 *control_flags;
  846. struct iwl_cmd *out_cmd;
  847. u32 idx;
  848. u16 fix_size;
  849. dma_addr_t phys_addr;
  850. int ret;
  851. unsigned long flags;
  852. cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
  853. fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  854. /* If any of the command structures end up being larger than
  855. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  856. * we will need to increase the size of the TFD entries */
  857. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  858. !(cmd->meta.flags & CMD_SIZE_HUGE));
  859. if (iwl_is_rfkill(priv)) {
  860. IWL_DEBUG_INFO("Not sending command - RF KILL");
  861. return -EIO;
  862. }
  863. if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
  864. IWL_ERROR("No space for Tx\n");
  865. return -ENOSPC;
  866. }
  867. spin_lock_irqsave(&priv->hcmd_lock, flags);
  868. tfd = &txq->bd[q->write_ptr];
  869. memset(tfd, 0, sizeof(*tfd));
  870. control_flags = (u32 *) tfd;
  871. idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
  872. out_cmd = &txq->cmd[idx];
  873. out_cmd->hdr.cmd = cmd->id;
  874. memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
  875. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  876. /* At this point, the out_cmd now has all of the incoming cmd
  877. * information */
  878. out_cmd->hdr.flags = 0;
  879. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
  880. INDEX_TO_SEQ(q->write_ptr));
  881. if (out_cmd->meta.flags & CMD_SIZE_HUGE)
  882. out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
  883. phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
  884. offsetof(struct iwl_cmd, hdr);
  885. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
  886. IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
  887. "%d bytes at %d[%d]:%d\n",
  888. get_cmd_string(out_cmd->hdr.cmd),
  889. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  890. fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  891. txq->need_update = 1;
  892. /* Set up entry in queue's byte count circular buffer */
  893. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
  894. /* Increment and update queue's write index */
  895. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  896. ret = iwl_txq_update_write_ptr(priv, txq);
  897. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  898. return ret ? ret : idx;
  899. }
  900. int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  901. {
  902. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  903. struct iwl_queue *q = &txq->q;
  904. struct iwl_tx_info *tx_info;
  905. int nfreed = 0;
  906. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  907. IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
  908. "is out of range [0-%d] %d %d.\n", txq_id,
  909. index, q->n_bd, q->write_ptr, q->read_ptr);
  910. return 0;
  911. }
  912. for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
  913. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  914. tx_info = &txq->txb[txq->q.read_ptr];
  915. ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb[0]);
  916. tx_info->skb[0] = NULL;
  917. if (priv->cfg->ops->lib->txq_inval_byte_cnt_tbl)
  918. priv->cfg->ops->lib->txq_inval_byte_cnt_tbl(priv, txq);
  919. iwl_hw_txq_free_tfd(priv, txq);
  920. nfreed++;
  921. }
  922. return nfreed;
  923. }
  924. EXPORT_SYMBOL(iwl_tx_queue_reclaim);
  925. /**
  926. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  927. *
  928. * When FW advances 'R' index, all entries between old and new 'R' index
  929. * need to be reclaimed. As result, some free space forms. If there is
  930. * enough free space (> low mark), wake the stack that feeds us.
  931. */
  932. static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  933. {
  934. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  935. struct iwl_queue *q = &txq->q;
  936. int nfreed = 0;
  937. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  938. IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
  939. "is out of range [0-%d] %d %d.\n", txq_id,
  940. index, q->n_bd, q->write_ptr, q->read_ptr);
  941. return;
  942. }
  943. for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
  944. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  945. if (nfreed > 1) {
  946. IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
  947. q->write_ptr, q->read_ptr);
  948. queue_work(priv->workqueue, &priv->restart);
  949. }
  950. nfreed++;
  951. }
  952. }
  953. /**
  954. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  955. * @rxb: Rx buffer to reclaim
  956. *
  957. * If an Rx buffer has an async callback associated with it the callback
  958. * will be executed. The attached skb (if present) will only be freed
  959. * if the callback returns 1
  960. */
  961. void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  962. {
  963. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  964. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  965. int txq_id = SEQ_TO_QUEUE(sequence);
  966. int index = SEQ_TO_INDEX(sequence);
  967. int huge = sequence & SEQ_HUGE_FRAME;
  968. int cmd_index;
  969. struct iwl_cmd *cmd;
  970. /* If a Tx command is being handled and it isn't in the actual
  971. * command queue then there a command routing bug has been introduced
  972. * in the queue management code. */
  973. if (txq_id != IWL_CMD_QUEUE_NUM)
  974. IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
  975. txq_id, pkt->hdr.cmd);
  976. BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
  977. cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
  978. cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
  979. /* Input error checking is done when commands are added to queue. */
  980. if (cmd->meta.flags & CMD_WANT_SKB) {
  981. cmd->meta.source->u.skb = rxb->skb;
  982. rxb->skb = NULL;
  983. } else if (cmd->meta.u.callback &&
  984. !cmd->meta.u.callback(priv, cmd, rxb->skb))
  985. rxb->skb = NULL;
  986. iwl_hcmd_queue_reclaim(priv, txq_id, index);
  987. if (!(cmd->meta.flags & CMD_ASYNC)) {
  988. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  989. wake_up_interruptible(&priv->wait_command_queue);
  990. }
  991. }
  992. EXPORT_SYMBOL(iwl_tx_cmd_complete);
  993. /*
  994. * Find first available (lowest unused) Tx Queue, mark it "active".
  995. * Called only when finding queue for aggregation.
  996. * Should never return anything < 7, because they should already
  997. * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
  998. */
  999. static int iwl_txq_ctx_activate_free(struct iwl_priv *priv)
  1000. {
  1001. int txq_id;
  1002. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  1003. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  1004. return txq_id;
  1005. return -1;
  1006. }
  1007. int iwl_tx_agg_start(struct iwl_priv *priv, const u8 *ra, u16 tid, u16 *ssn)
  1008. {
  1009. int sta_id;
  1010. int tx_fifo;
  1011. int txq_id;
  1012. int ret;
  1013. unsigned long flags;
  1014. struct iwl_tid_data *tid_data;
  1015. DECLARE_MAC_BUF(mac);
  1016. if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
  1017. tx_fifo = default_tid_to_tx_fifo[tid];
  1018. else
  1019. return -EINVAL;
  1020. IWL_WARNING("%s on ra = %s tid = %d\n",
  1021. __func__, print_mac(mac, ra), tid);
  1022. sta_id = iwl_find_station(priv, ra);
  1023. if (sta_id == IWL_INVALID_STATION)
  1024. return -ENXIO;
  1025. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
  1026. IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
  1027. return -ENXIO;
  1028. }
  1029. txq_id = iwl_txq_ctx_activate_free(priv);
  1030. if (txq_id == -1)
  1031. return -ENXIO;
  1032. spin_lock_irqsave(&priv->sta_lock, flags);
  1033. tid_data = &priv->stations[sta_id].tid[tid];
  1034. *ssn = SEQ_TO_SN(tid_data->seq_number);
  1035. tid_data->agg.txq_id = txq_id;
  1036. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1037. ret = priv->cfg->ops->lib->txq_agg_enable(priv, txq_id, tx_fifo,
  1038. sta_id, tid, *ssn);
  1039. if (ret)
  1040. return ret;
  1041. if (tid_data->tfds_in_queue == 0) {
  1042. printk(KERN_ERR "HW queue is empty\n");
  1043. tid_data->agg.state = IWL_AGG_ON;
  1044. ieee80211_start_tx_ba_cb_irqsafe(priv->hw, ra, tid);
  1045. } else {
  1046. IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
  1047. tid_data->tfds_in_queue);
  1048. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  1049. }
  1050. return ret;
  1051. }
  1052. EXPORT_SYMBOL(iwl_tx_agg_start);
  1053. int iwl_tx_agg_stop(struct iwl_priv *priv , const u8 *ra, u16 tid)
  1054. {
  1055. int tx_fifo_id, txq_id, sta_id, ssn = -1;
  1056. struct iwl_tid_data *tid_data;
  1057. int ret, write_ptr, read_ptr;
  1058. unsigned long flags;
  1059. DECLARE_MAC_BUF(mac);
  1060. if (!ra) {
  1061. IWL_ERROR("ra = NULL\n");
  1062. return -EINVAL;
  1063. }
  1064. if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
  1065. tx_fifo_id = default_tid_to_tx_fifo[tid];
  1066. else
  1067. return -EINVAL;
  1068. sta_id = iwl_find_station(priv, ra);
  1069. if (sta_id == IWL_INVALID_STATION)
  1070. return -ENXIO;
  1071. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
  1072. IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
  1073. tid_data = &priv->stations[sta_id].tid[tid];
  1074. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  1075. txq_id = tid_data->agg.txq_id;
  1076. write_ptr = priv->txq[txq_id].q.write_ptr;
  1077. read_ptr = priv->txq[txq_id].q.read_ptr;
  1078. /* The queue is not empty */
  1079. if (write_ptr != read_ptr) {
  1080. IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
  1081. priv->stations[sta_id].tid[tid].agg.state =
  1082. IWL_EMPTYING_HW_QUEUE_DELBA;
  1083. return 0;
  1084. }
  1085. IWL_DEBUG_HT("HW queue is empty\n");
  1086. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  1087. spin_lock_irqsave(&priv->lock, flags);
  1088. ret = priv->cfg->ops->lib->txq_agg_disable(priv, txq_id, ssn,
  1089. tx_fifo_id);
  1090. spin_unlock_irqrestore(&priv->lock, flags);
  1091. if (ret)
  1092. return ret;
  1093. ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
  1094. return 0;
  1095. }
  1096. EXPORT_SYMBOL(iwl_tx_agg_stop);
  1097. int iwl_txq_check_empty(struct iwl_priv *priv, int sta_id, u8 tid, int txq_id)
  1098. {
  1099. struct iwl_queue *q = &priv->txq[txq_id].q;
  1100. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  1101. struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
  1102. switch (priv->stations[sta_id].tid[tid].agg.state) {
  1103. case IWL_EMPTYING_HW_QUEUE_DELBA:
  1104. /* We are reclaiming the last packet of the */
  1105. /* aggregated HW queue */
  1106. if (txq_id == tid_data->agg.txq_id &&
  1107. q->read_ptr == q->write_ptr) {
  1108. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  1109. int tx_fifo = default_tid_to_tx_fifo[tid];
  1110. IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
  1111. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id,
  1112. ssn, tx_fifo);
  1113. tid_data->agg.state = IWL_AGG_OFF;
  1114. ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
  1115. }
  1116. break;
  1117. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  1118. /* We are reclaiming the last packet of the queue */
  1119. if (tid_data->tfds_in_queue == 0) {
  1120. IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
  1121. tid_data->agg.state = IWL_AGG_ON;
  1122. ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
  1123. }
  1124. break;
  1125. }
  1126. return 0;
  1127. }
  1128. EXPORT_SYMBOL(iwl_txq_check_empty);
  1129. #ifdef CONFIG_IWLWIF_DEBUG
  1130. #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
  1131. const char *iwl_get_tx_fail_reason(u32 status)
  1132. {
  1133. switch (status & TX_STATUS_MSK) {
  1134. case TX_STATUS_SUCCESS:
  1135. return "SUCCESS";
  1136. TX_STATUS_ENTRY(SHORT_LIMIT);
  1137. TX_STATUS_ENTRY(LONG_LIMIT);
  1138. TX_STATUS_ENTRY(FIFO_UNDERRUN);
  1139. TX_STATUS_ENTRY(MGMNT_ABORT);
  1140. TX_STATUS_ENTRY(NEXT_FRAG);
  1141. TX_STATUS_ENTRY(LIFE_EXPIRE);
  1142. TX_STATUS_ENTRY(DEST_PS);
  1143. TX_STATUS_ENTRY(ABORTED);
  1144. TX_STATUS_ENTRY(BT_RETRY);
  1145. TX_STATUS_ENTRY(STA_INVALID);
  1146. TX_STATUS_ENTRY(FRAG_DROPPED);
  1147. TX_STATUS_ENTRY(TID_DISABLE);
  1148. TX_STATUS_ENTRY(FRAME_FLUSHED);
  1149. TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
  1150. TX_STATUS_ENTRY(TX_LOCKED);
  1151. TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
  1152. }
  1153. return "UNKNOWN";
  1154. }
  1155. EXPORT_SYMBOL(iwl_get_tx_fail_reason);
  1156. #endif /* CONFIG_IWLWIFI_DEBUG */