iwl-tx.c 43 KB

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