iwl-tx.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. /**
  38. * iwl_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  39. *
  40. * Does NOT advance any TFD circular buffer read/write indexes
  41. * Does NOT free the TFD itself (which is within circular buffer)
  42. */
  43. int iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  44. {
  45. struct iwl_tfd_frame *bd_tmp = (struct iwl_tfd_frame *)&txq->bd[0];
  46. struct iwl_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
  47. struct pci_dev *dev = priv->pci_dev;
  48. int i;
  49. int counter = 0;
  50. int index, is_odd;
  51. /* Host command buffers stay mapped in memory, nothing to clean */
  52. if (txq->q.id == IWL_CMD_QUEUE_NUM)
  53. return 0;
  54. /* Sanity check on number of chunks */
  55. counter = IWL_GET_BITS(*bd, num_tbs);
  56. if (counter > MAX_NUM_OF_TBS) {
  57. IWL_ERROR("Too many chunks: %i\n", counter);
  58. /* @todo issue fatal error, it is quite serious situation */
  59. return 0;
  60. }
  61. /* Unmap chunks, if any.
  62. * TFD info for odd chunks is different format than for even chunks. */
  63. for (i = 0; i < counter; i++) {
  64. index = i / 2;
  65. is_odd = i & 0x1;
  66. if (is_odd)
  67. pci_unmap_single(
  68. dev,
  69. IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
  70. (IWL_GET_BITS(bd->pa[index],
  71. tb2_addr_hi20) << 16),
  72. IWL_GET_BITS(bd->pa[index], tb2_len),
  73. PCI_DMA_TODEVICE);
  74. else if (i > 0)
  75. pci_unmap_single(dev,
  76. le32_to_cpu(bd->pa[index].tb1_addr),
  77. IWL_GET_BITS(bd->pa[index], tb1_len),
  78. PCI_DMA_TODEVICE);
  79. /* Free SKB, if any, for this chunk */
  80. if (txq->txb[txq->q.read_ptr].skb[i]) {
  81. struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[i];
  82. dev_kfree_skb(skb);
  83. txq->txb[txq->q.read_ptr].skb[i] = NULL;
  84. }
  85. }
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(iwl_hw_txq_free_tfd);
  89. int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
  90. dma_addr_t addr, u16 len)
  91. {
  92. int index, is_odd;
  93. struct iwl_tfd_frame *tfd = ptr;
  94. u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
  95. /* Each TFD can point to a maximum 20 Tx buffers */
  96. if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
  97. IWL_ERROR("Error can not send more than %d chunks\n",
  98. MAX_NUM_OF_TBS);
  99. return -EINVAL;
  100. }
  101. index = num_tbs / 2;
  102. is_odd = num_tbs & 0x1;
  103. if (!is_odd) {
  104. tfd->pa[index].tb1_addr = cpu_to_le32(addr);
  105. IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
  106. iwl_get_dma_hi_address(addr));
  107. IWL_SET_BITS(tfd->pa[index], tb1_len, len);
  108. } else {
  109. IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
  110. (u32) (addr & 0xffff));
  111. IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
  112. IWL_SET_BITS(tfd->pa[index], tb2_len, len);
  113. }
  114. IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(iwl_hw_txq_attach_buf_to_tfd);
  118. /**
  119. * iwl_txq_update_write_ptr - Send new write index to hardware
  120. */
  121. int iwl_txq_update_write_ptr(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  122. {
  123. u32 reg = 0;
  124. int ret = 0;
  125. int txq_id = txq->q.id;
  126. if (txq->need_update == 0)
  127. return ret;
  128. /* if we're trying to save power */
  129. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  130. /* wake up nic if it's powered down ...
  131. * uCode will wake up, and interrupt us again, so next
  132. * time we'll skip this part. */
  133. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  134. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  135. IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
  136. iwl_set_bit(priv, CSR_GP_CNTRL,
  137. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  138. return ret;
  139. }
  140. /* restore this queue's parameters in nic hardware. */
  141. ret = iwl_grab_nic_access(priv);
  142. if (ret)
  143. return ret;
  144. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  145. txq->q.write_ptr | (txq_id << 8));
  146. iwl_release_nic_access(priv);
  147. /* else not in power-save mode, uCode will never sleep when we're
  148. * trying to tx (during RFKILL, we're not trying to tx). */
  149. } else
  150. iwl_write32(priv, HBUS_TARG_WRPTR,
  151. txq->q.write_ptr | (txq_id << 8));
  152. txq->need_update = 0;
  153. return ret;
  154. }
  155. EXPORT_SYMBOL(iwl_txq_update_write_ptr);
  156. /**
  157. * iwl_tx_queue_free - Deallocate DMA queue.
  158. * @txq: Transmit queue to deallocate.
  159. *
  160. * Empty queue by removing and destroying all BD's.
  161. * Free all buffers.
  162. * 0-fill, but do not free "txq" descriptor structure.
  163. */
  164. static void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  165. {
  166. struct iwl_queue *q = &txq->q;
  167. struct pci_dev *dev = priv->pci_dev;
  168. int len;
  169. if (q->n_bd == 0)
  170. return;
  171. /* first, empty all BD's */
  172. for (; q->write_ptr != q->read_ptr;
  173. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
  174. iwl_hw_txq_free_tfd(priv, txq);
  175. len = sizeof(struct iwl_cmd) * q->n_window;
  176. if (q->id == IWL_CMD_QUEUE_NUM)
  177. len += IWL_MAX_SCAN_SIZE;
  178. /* De-alloc array of command/tx buffers */
  179. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  180. /* De-alloc circular buffer of TFDs */
  181. if (txq->q.n_bd)
  182. pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
  183. txq->q.n_bd, txq->bd, txq->q.dma_addr);
  184. /* De-alloc array of per-TFD driver data */
  185. kfree(txq->txb);
  186. txq->txb = NULL;
  187. /* 0-fill queue descriptor structure */
  188. memset(txq, 0, sizeof(*txq));
  189. }
  190. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  191. * DMA services
  192. *
  193. * Theory of operation
  194. *
  195. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  196. * of buffer descriptors, each of which points to one or more data buffers for
  197. * the device to read from or fill. Driver and device exchange status of each
  198. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  199. * entries in each circular buffer, to protect against confusing empty and full
  200. * queue states.
  201. *
  202. * The device reads or writes the data in the queues via the device's several
  203. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  204. *
  205. * For Tx queue, there are low mark and high mark limits. If, after queuing
  206. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  207. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  208. * Tx queue resumed.
  209. *
  210. * See more detailed info in iwl-4965-hw.h.
  211. ***************************************************/
  212. int iwl_queue_space(const struct iwl_queue *q)
  213. {
  214. int s = q->read_ptr - q->write_ptr;
  215. if (q->read_ptr > q->write_ptr)
  216. s -= q->n_bd;
  217. if (s <= 0)
  218. s += q->n_window;
  219. /* keep some reserve to not confuse empty and full situations */
  220. s -= 2;
  221. if (s < 0)
  222. s = 0;
  223. return s;
  224. }
  225. EXPORT_SYMBOL(iwl_queue_space);
  226. /**
  227. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  228. */
  229. static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
  230. int count, int slots_num, u32 id)
  231. {
  232. q->n_bd = count;
  233. q->n_window = slots_num;
  234. q->id = id;
  235. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  236. * and iwl_queue_dec_wrap are broken. */
  237. BUG_ON(!is_power_of_2(count));
  238. /* slots_num must be power-of-two size, otherwise
  239. * get_cmd_index is broken. */
  240. BUG_ON(!is_power_of_2(slots_num));
  241. q->low_mark = q->n_window / 4;
  242. if (q->low_mark < 4)
  243. q->low_mark = 4;
  244. q->high_mark = q->n_window / 8;
  245. if (q->high_mark < 2)
  246. q->high_mark = 2;
  247. q->write_ptr = q->read_ptr = 0;
  248. return 0;
  249. }
  250. /**
  251. * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  252. */
  253. static int iwl_tx_queue_alloc(struct iwl_priv *priv,
  254. struct iwl_tx_queue *txq, u32 id)
  255. {
  256. struct pci_dev *dev = priv->pci_dev;
  257. /* Driver private data, only for Tx (not command) queues,
  258. * not shared with device. */
  259. if (id != IWL_CMD_QUEUE_NUM) {
  260. txq->txb = kmalloc(sizeof(txq->txb[0]) *
  261. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  262. if (!txq->txb) {
  263. IWL_ERROR("kmalloc for auxiliary BD "
  264. "structures failed\n");
  265. goto error;
  266. }
  267. } else
  268. txq->txb = NULL;
  269. /* Circular buffer of transmit frame descriptors (TFDs),
  270. * shared with device */
  271. txq->bd = pci_alloc_consistent(dev,
  272. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
  273. &txq->q.dma_addr);
  274. if (!txq->bd) {
  275. IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
  276. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
  277. goto error;
  278. }
  279. txq->q.id = id;
  280. return 0;
  281. error:
  282. kfree(txq->txb);
  283. txq->txb = NULL;
  284. return -ENOMEM;
  285. }
  286. /*
  287. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  288. * given Tx queue, and enable the DMA channel used for that queue.
  289. *
  290. * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  291. * channels supported in hardware.
  292. */
  293. static int iwl_hw_tx_queue_init(struct iwl_priv *priv,
  294. struct iwl_tx_queue *txq)
  295. {
  296. int rc;
  297. unsigned long flags;
  298. int txq_id = txq->q.id;
  299. spin_lock_irqsave(&priv->lock, flags);
  300. rc = iwl_grab_nic_access(priv);
  301. if (rc) {
  302. spin_unlock_irqrestore(&priv->lock, flags);
  303. return rc;
  304. }
  305. /* Circular buffer (TFD queue in DRAM) physical base address */
  306. iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  307. txq->q.dma_addr >> 8);
  308. /* Enable DMA channel, using same id as for TFD queue */
  309. iwl_write_direct32(
  310. priv, FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
  311. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  312. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
  313. iwl_release_nic_access(priv);
  314. spin_unlock_irqrestore(&priv->lock, flags);
  315. return 0;
  316. }
  317. /**
  318. * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
  319. */
  320. static int iwl_tx_queue_init(struct iwl_priv *priv,
  321. struct iwl_tx_queue *txq,
  322. int slots_num, u32 txq_id)
  323. {
  324. struct pci_dev *dev = priv->pci_dev;
  325. int len;
  326. int rc = 0;
  327. /*
  328. * Alloc buffer array for commands (Tx or other types of commands).
  329. * For the command queue (#4), allocate command space + one big
  330. * command for scan, since scan command is very huge; the system will
  331. * not have two scans at the same time, so only one is needed.
  332. * For normal Tx queues (all other queues), no super-size command
  333. * space is needed.
  334. */
  335. len = sizeof(struct iwl_cmd) * slots_num;
  336. if (txq_id == IWL_CMD_QUEUE_NUM)
  337. len += IWL_MAX_SCAN_SIZE;
  338. txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
  339. if (!txq->cmd)
  340. return -ENOMEM;
  341. /* Alloc driver data array and TFD circular buffer */
  342. rc = iwl_tx_queue_alloc(priv, txq, txq_id);
  343. if (rc) {
  344. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  345. return -ENOMEM;
  346. }
  347. txq->need_update = 0;
  348. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  349. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  350. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  351. /* Initialize queue's high/low-water marks, and head/tail indexes */
  352. iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  353. /* Tell device where to find queue */
  354. iwl_hw_tx_queue_init(priv, txq);
  355. return 0;
  356. }
  357. /**
  358. * iwl_hw_txq_ctx_free - Free TXQ Context
  359. *
  360. * Destroy all TX DMA queues and structures
  361. */
  362. void iwl_hw_txq_ctx_free(struct iwl_priv *priv)
  363. {
  364. int txq_id;
  365. /* Tx queues */
  366. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  367. iwl_tx_queue_free(priv, &priv->txq[txq_id]);
  368. /* Keep-warm buffer */
  369. iwl_kw_free(priv);
  370. }
  371. EXPORT_SYMBOL(iwl_hw_txq_ctx_free);
  372. /**
  373. * iwl_txq_ctx_reset - Reset TX queue context
  374. * Destroys all DMA structures and initialise them again
  375. *
  376. * @param priv
  377. * @return error code
  378. */
  379. int iwl_txq_ctx_reset(struct iwl_priv *priv)
  380. {
  381. int ret = 0;
  382. int txq_id, slots_num;
  383. unsigned long flags;
  384. iwl_kw_free(priv);
  385. /* Free all tx/cmd queues and keep-warm buffer */
  386. iwl_hw_txq_ctx_free(priv);
  387. /* Alloc keep-warm buffer */
  388. ret = iwl_kw_alloc(priv);
  389. if (ret) {
  390. IWL_ERROR("Keep Warm allocation failed");
  391. goto error_kw;
  392. }
  393. spin_lock_irqsave(&priv->lock, flags);
  394. ret = iwl_grab_nic_access(priv);
  395. if (unlikely(ret)) {
  396. spin_unlock_irqrestore(&priv->lock, flags);
  397. goto error_reset;
  398. }
  399. /* Turn off all Tx DMA fifos */
  400. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  401. iwl_release_nic_access(priv);
  402. spin_unlock_irqrestore(&priv->lock, flags);
  403. /* Tell nic where to find the keep-warm buffer */
  404. ret = iwl_kw_init(priv);
  405. if (ret) {
  406. IWL_ERROR("kw_init failed\n");
  407. goto error_reset;
  408. }
  409. /* Alloc and init all Tx queues, including the command queue (#4) */
  410. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  411. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  412. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  413. ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  414. txq_id);
  415. if (ret) {
  416. IWL_ERROR("Tx %d queue init failed\n", txq_id);
  417. goto error;
  418. }
  419. }
  420. return ret;
  421. error:
  422. iwl_hw_txq_ctx_free(priv);
  423. error_reset:
  424. iwl_kw_free(priv);
  425. error_kw:
  426. return ret;
  427. }
  428. /**
  429. * iwl_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
  430. */
  431. void iwl_txq_ctx_stop(struct iwl_priv *priv)
  432. {
  433. int txq_id;
  434. unsigned long flags;
  435. /* Turn off all Tx DMA fifos */
  436. spin_lock_irqsave(&priv->lock, flags);
  437. if (iwl_grab_nic_access(priv)) {
  438. spin_unlock_irqrestore(&priv->lock, flags);
  439. return;
  440. }
  441. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  442. /* Stop each Tx DMA channel, and wait for it to be idle */
  443. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  444. iwl_write_direct32(priv,
  445. FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
  446. iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  447. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
  448. (txq_id), 200);
  449. }
  450. iwl_release_nic_access(priv);
  451. spin_unlock_irqrestore(&priv->lock, flags);
  452. /* Deallocate memory for all Tx queues */
  453. iwl_hw_txq_ctx_free(priv);
  454. }
  455. EXPORT_SYMBOL(iwl_txq_ctx_stop);
  456. /*
  457. * handle build REPLY_TX command notification.
  458. */
  459. static void iwl_tx_cmd_build_basic(struct iwl_priv *priv,
  460. struct iwl_tx_cmd *tx_cmd,
  461. struct ieee80211_tx_info *info,
  462. struct ieee80211_hdr *hdr,
  463. int is_unicast, u8 std_id)
  464. {
  465. u16 fc = le16_to_cpu(hdr->frame_control);
  466. __le32 tx_flags = tx_cmd->tx_flags;
  467. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  468. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  469. tx_flags |= TX_CMD_FLG_ACK_MSK;
  470. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
  471. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  472. if (ieee80211_is_probe_response(fc) &&
  473. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  474. tx_flags |= TX_CMD_FLG_TSF_MSK;
  475. } else {
  476. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  477. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  478. }
  479. if (ieee80211_is_back_request(fc))
  480. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  481. tx_cmd->sta_id = std_id;
  482. if (ieee80211_get_morefrag(hdr))
  483. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  484. if (ieee80211_is_qos_data(fc)) {
  485. u8 *qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
  486. tx_cmd->tid_tspec = qc[0] & 0xf;
  487. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  488. } else {
  489. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  490. }
  491. if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
  492. tx_flags |= TX_CMD_FLG_RTS_MSK;
  493. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  494. } else if (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT) {
  495. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  496. tx_flags |= TX_CMD_FLG_CTS_MSK;
  497. }
  498. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  499. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  500. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  501. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
  502. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
  503. (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
  504. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  505. else
  506. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  507. } else {
  508. tx_cmd->timeout.pm_frame_timeout = 0;
  509. }
  510. tx_cmd->driver_txop = 0;
  511. tx_cmd->tx_flags = tx_flags;
  512. tx_cmd->next_frame_len = 0;
  513. }
  514. #define RTS_HCCA_RETRY_LIMIT 3
  515. #define RTS_DFAULT_RETRY_LIMIT 60
  516. static void iwl_tx_cmd_build_rate(struct iwl_priv *priv,
  517. struct iwl_tx_cmd *tx_cmd,
  518. struct ieee80211_tx_info *info,
  519. u16 fc, int sta_id,
  520. int is_hcca)
  521. {
  522. u8 rts_retry_limit = 0;
  523. u8 data_retry_limit = 0;
  524. u8 rate_plcp;
  525. u16 rate_flags = 0;
  526. int rate_idx;
  527. rate_idx = min(ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xffff,
  528. IWL_RATE_COUNT - 1);
  529. rate_plcp = iwl_rates[rate_idx].plcp;
  530. rts_retry_limit = (is_hcca) ?
  531. RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
  532. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  533. rate_flags |= RATE_MCS_CCK_MSK;
  534. if (ieee80211_is_probe_response(fc)) {
  535. data_retry_limit = 3;
  536. if (data_retry_limit < rts_retry_limit)
  537. rts_retry_limit = data_retry_limit;
  538. } else
  539. data_retry_limit = IWL_DEFAULT_TX_RETRY;
  540. if (priv->data_retry_limit != -1)
  541. data_retry_limit = priv->data_retry_limit;
  542. if (ieee80211_is_data(fc)) {
  543. tx_cmd->initial_rate_index = 0;
  544. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  545. } else {
  546. switch (fc & IEEE80211_FCTL_STYPE) {
  547. case IEEE80211_STYPE_AUTH:
  548. case IEEE80211_STYPE_DEAUTH:
  549. case IEEE80211_STYPE_ASSOC_REQ:
  550. case IEEE80211_STYPE_REASSOC_REQ:
  551. if (tx_cmd->tx_flags & TX_CMD_FLG_RTS_MSK) {
  552. tx_cmd->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  553. tx_cmd->tx_flags |= TX_CMD_FLG_CTS_MSK;
  554. }
  555. break;
  556. default:
  557. break;
  558. }
  559. /* Alternate between antenna A and B for successive frames */
  560. if (priv->use_ant_b_for_management_frame) {
  561. priv->use_ant_b_for_management_frame = 0;
  562. rate_flags |= RATE_MCS_ANT_B_MSK;
  563. } else {
  564. priv->use_ant_b_for_management_frame = 1;
  565. rate_flags |= RATE_MCS_ANT_A_MSK;
  566. }
  567. }
  568. tx_cmd->rts_retry_limit = rts_retry_limit;
  569. tx_cmd->data_retry_limit = data_retry_limit;
  570. tx_cmd->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
  571. }
  572. static void iwl_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  573. struct ieee80211_tx_info *info,
  574. struct iwl_tx_cmd *tx_cmd,
  575. struct sk_buff *skb_frag,
  576. int sta_id)
  577. {
  578. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  579. switch (keyconf->alg) {
  580. case ALG_CCMP:
  581. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  582. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  583. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  584. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  585. IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
  586. break;
  587. case ALG_TKIP:
  588. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  589. ieee80211_get_tkip_key(keyconf, skb_frag,
  590. IEEE80211_TKIP_P2_KEY, tx_cmd->key);
  591. IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
  592. break;
  593. case ALG_WEP:
  594. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  595. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  596. if (keyconf->keylen == WEP_KEY_LEN_128)
  597. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  598. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  599. IWL_DEBUG_TX("Configuring packet for WEP encryption "
  600. "with key %d\n", keyconf->keyidx);
  601. break;
  602. default:
  603. printk(KERN_ERR "Unknown encode alg %d\n", keyconf->alg);
  604. break;
  605. }
  606. }
  607. static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  608. {
  609. /* 0 - mgmt, 1 - cnt, 2 - data */
  610. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  611. priv->tx_stats[idx].cnt++;
  612. priv->tx_stats[idx].bytes += len;
  613. }
  614. /*
  615. * start REPLY_TX command process
  616. */
  617. int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  618. {
  619. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  620. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  621. struct iwl_tfd_frame *tfd;
  622. u32 *control_flags;
  623. int txq_id = skb_get_queue_mapping(skb);
  624. struct iwl_tx_queue *txq = NULL;
  625. struct iwl_queue *q = NULL;
  626. dma_addr_t phys_addr;
  627. dma_addr_t txcmd_phys;
  628. dma_addr_t scratch_phys;
  629. struct iwl_cmd *out_cmd = NULL;
  630. struct iwl_tx_cmd *tx_cmd;
  631. u16 len, idx, len_org;
  632. u16 seq_number = 0;
  633. u8 id, hdr_len, unicast;
  634. u8 sta_id;
  635. u16 fc;
  636. u8 wait_write_ptr = 0;
  637. u8 tid = 0;
  638. u8 *qc = NULL;
  639. unsigned long flags;
  640. int ret;
  641. spin_lock_irqsave(&priv->lock, flags);
  642. if (iwl_is_rfkill(priv)) {
  643. IWL_DEBUG_DROP("Dropping - RF KILL\n");
  644. goto drop_unlock;
  645. }
  646. if (!priv->vif) {
  647. IWL_DEBUG_DROP("Dropping - !priv->vif\n");
  648. goto drop_unlock;
  649. }
  650. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) ==
  651. IWL_INVALID_RATE) {
  652. IWL_ERROR("ERROR: No TX rate available.\n");
  653. goto drop_unlock;
  654. }
  655. unicast = !is_multicast_ether_addr(hdr->addr1);
  656. id = 0;
  657. fc = le16_to_cpu(hdr->frame_control);
  658. #ifdef CONFIG_IWLWIFI_DEBUG
  659. if (ieee80211_is_auth(fc))
  660. IWL_DEBUG_TX("Sending AUTH frame\n");
  661. else if (ieee80211_is_assoc_request(fc))
  662. IWL_DEBUG_TX("Sending ASSOC frame\n");
  663. else if (ieee80211_is_reassoc_request(fc))
  664. IWL_DEBUG_TX("Sending REASSOC frame\n");
  665. #endif
  666. /* drop all data frame if we are not associated */
  667. if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  668. (!iwl_is_associated(priv) ||
  669. ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
  670. !priv->assoc_station_added)) {
  671. IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
  672. goto drop_unlock;
  673. }
  674. spin_unlock_irqrestore(&priv->lock, flags);
  675. hdr_len = ieee80211_get_hdrlen(fc);
  676. /* Find (or create) index into station table for destination station */
  677. sta_id = iwl_get_sta_id(priv, hdr);
  678. if (sta_id == IWL_INVALID_STATION) {
  679. DECLARE_MAC_BUF(mac);
  680. IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
  681. print_mac(mac, hdr->addr1));
  682. goto drop;
  683. }
  684. IWL_DEBUG_TX("station Id %d\n", sta_id);
  685. if (ieee80211_is_qos_data(fc)) {
  686. qc = ieee80211_get_qos_ctrl(hdr, hdr_len);
  687. tid = qc[0] & 0xf;
  688. seq_number = priv->stations[sta_id].tid[tid].seq_number &
  689. IEEE80211_SCTL_SEQ;
  690. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  691. (hdr->seq_ctrl &
  692. __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
  693. seq_number += 0x10;
  694. #ifdef CONFIG_IWL4965_HT
  695. /* aggregation is on for this <sta,tid> */
  696. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  697. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  698. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  699. #endif /* CONFIG_IWL4965_HT */
  700. }
  701. /* Descriptor for chosen Tx queue */
  702. txq = &priv->txq[txq_id];
  703. q = &txq->q;
  704. spin_lock_irqsave(&priv->lock, flags);
  705. /* Set up first empty TFD within this queue's circular TFD buffer */
  706. tfd = &txq->bd[q->write_ptr];
  707. memset(tfd, 0, sizeof(*tfd));
  708. control_flags = (u32 *) tfd;
  709. idx = get_cmd_index(q, q->write_ptr, 0);
  710. /* Set up driver data for this TFD */
  711. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  712. txq->txb[q->write_ptr].skb[0] = skb;
  713. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  714. out_cmd = &txq->cmd[idx];
  715. tx_cmd = &out_cmd->cmd.tx;
  716. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  717. memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
  718. /*
  719. * Set up the Tx-command (not MAC!) header.
  720. * Store the chosen Tx queue and TFD index within the sequence field;
  721. * after Tx, uCode's Tx response will return this value so driver can
  722. * locate the frame within the tx queue and do post-tx processing.
  723. */
  724. out_cmd->hdr.cmd = REPLY_TX;
  725. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  726. INDEX_TO_SEQ(q->write_ptr)));
  727. /* Copy MAC header from skb into command buffer */
  728. memcpy(tx_cmd->hdr, hdr, hdr_len);
  729. /*
  730. * Use the first empty entry in this queue's command buffer array
  731. * to contain the Tx command and MAC header concatenated together
  732. * (payload data will be in another buffer).
  733. * Size of this varies, due to varying MAC header length.
  734. * If end is not dword aligned, we'll have 2 extra bytes at the end
  735. * of the MAC header (device reads on dword boundaries).
  736. * We'll tell device about this padding later.
  737. */
  738. len = sizeof(struct iwl_tx_cmd) +
  739. sizeof(struct iwl_cmd_header) + hdr_len;
  740. len_org = len;
  741. len = (len + 3) & ~3;
  742. if (len_org != len)
  743. len_org = 1;
  744. else
  745. len_org = 0;
  746. /* Physical address of this Tx command's header (not MAC header!),
  747. * within command buffer array. */
  748. txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
  749. offsetof(struct iwl_cmd, hdr);
  750. /* Add buffer containing Tx command and MAC(!) header to TFD's
  751. * first entry */
  752. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
  753. if (!(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT))
  754. iwl_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
  755. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  756. * if any (802.11 null frames have no payload). */
  757. len = skb->len - hdr_len;
  758. if (len) {
  759. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  760. len, PCI_DMA_TODEVICE);
  761. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
  762. }
  763. /* Tell NIC about any 2-byte padding after MAC header */
  764. if (len_org)
  765. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  766. /* Total # bytes to be transmitted */
  767. len = (u16)skb->len;
  768. tx_cmd->len = cpu_to_le16(len);
  769. /* TODO need this for burst mode later on */
  770. iwl_tx_cmd_build_basic(priv, tx_cmd, info, hdr, unicast, sta_id);
  771. /* set is_hcca to 0; it probably will never be implemented */
  772. iwl_tx_cmd_build_rate(priv, tx_cmd, info, fc, sta_id, 0);
  773. iwl_update_tx_stats(priv, fc, len);
  774. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  775. offsetof(struct iwl_tx_cmd, scratch);
  776. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  777. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
  778. if (!ieee80211_get_morefrag(hdr)) {
  779. txq->need_update = 1;
  780. if (qc)
  781. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  782. } else {
  783. wait_write_ptr = 1;
  784. txq->need_update = 0;
  785. }
  786. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  787. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  788. /* Set up entry for this TFD in Tx byte-count array */
  789. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
  790. /* Tell device the write index *just past* this latest filled TFD */
  791. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  792. ret = iwl_txq_update_write_ptr(priv, txq);
  793. spin_unlock_irqrestore(&priv->lock, flags);
  794. if (ret)
  795. return ret;
  796. if ((iwl_queue_space(q) < q->high_mark)
  797. && priv->mac80211_registered) {
  798. if (wait_write_ptr) {
  799. spin_lock_irqsave(&priv->lock, flags);
  800. txq->need_update = 1;
  801. iwl_txq_update_write_ptr(priv, txq);
  802. spin_unlock_irqrestore(&priv->lock, flags);
  803. }
  804. ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
  805. }
  806. return 0;
  807. drop_unlock:
  808. spin_unlock_irqrestore(&priv->lock, flags);
  809. drop:
  810. return -1;
  811. }
  812. EXPORT_SYMBOL(iwl_tx_skb);
  813. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  814. /**
  815. * iwl_enqueue_hcmd - enqueue a uCode command
  816. * @priv: device private data point
  817. * @cmd: a point to the ucode command structure
  818. *
  819. * The function returns < 0 values to indicate the operation is
  820. * failed. On success, it turns the index (> 0) of command in the
  821. * command queue.
  822. */
  823. int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  824. {
  825. struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  826. struct iwl_queue *q = &txq->q;
  827. struct iwl_tfd_frame *tfd;
  828. u32 *control_flags;
  829. struct iwl_cmd *out_cmd;
  830. u32 idx;
  831. u16 fix_size;
  832. dma_addr_t phys_addr;
  833. int ret;
  834. unsigned long flags;
  835. cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
  836. fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  837. /* If any of the command structures end up being larger than
  838. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  839. * we will need to increase the size of the TFD entries */
  840. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  841. !(cmd->meta.flags & CMD_SIZE_HUGE));
  842. if (iwl_is_rfkill(priv)) {
  843. IWL_DEBUG_INFO("Not sending command - RF KILL");
  844. return -EIO;
  845. }
  846. if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
  847. IWL_ERROR("No space for Tx\n");
  848. return -ENOSPC;
  849. }
  850. spin_lock_irqsave(&priv->hcmd_lock, flags);
  851. tfd = &txq->bd[q->write_ptr];
  852. memset(tfd, 0, sizeof(*tfd));
  853. control_flags = (u32 *) tfd;
  854. idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
  855. out_cmd = &txq->cmd[idx];
  856. out_cmd->hdr.cmd = cmd->id;
  857. memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
  858. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  859. /* At this point, the out_cmd now has all of the incoming cmd
  860. * information */
  861. out_cmd->hdr.flags = 0;
  862. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
  863. INDEX_TO_SEQ(q->write_ptr));
  864. if (out_cmd->meta.flags & CMD_SIZE_HUGE)
  865. out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
  866. phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
  867. offsetof(struct iwl_cmd, hdr);
  868. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
  869. IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
  870. "%d bytes at %d[%d]:%d\n",
  871. get_cmd_string(out_cmd->hdr.cmd),
  872. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  873. fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  874. txq->need_update = 1;
  875. /* Set up entry in queue's byte count circular buffer */
  876. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
  877. /* Increment and update queue's write index */
  878. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  879. ret = iwl_txq_update_write_ptr(priv, txq);
  880. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  881. return ret ? ret : idx;
  882. }