iwl-tx.c 44 KB

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