iwl-tx.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 <net/mac80211.h>
  30. #include "iwl-eeprom.h"
  31. #include "iwl-dev.h"
  32. #include "iwl-core.h"
  33. #include "iwl-sta.h"
  34. #include "iwl-io.h"
  35. #include "iwl-helpers.h"
  36. /**
  37. * iwl_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  38. *
  39. * Does NOT advance any TFD circular buffer read/write indexes
  40. * Does NOT free the TFD itself (which is within circular buffer)
  41. */
  42. int iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  43. {
  44. struct iwl_tfd_frame *bd_tmp = (struct iwl_tfd_frame *)&txq->bd[0];
  45. struct iwl_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
  46. struct pci_dev *dev = priv->pci_dev;
  47. int i;
  48. int counter = 0;
  49. int index, is_odd;
  50. /* Host command buffers stay mapped in memory, nothing to clean */
  51. if (txq->q.id == IWL_CMD_QUEUE_NUM)
  52. return 0;
  53. /* Sanity check on number of chunks */
  54. counter = IWL_GET_BITS(*bd, num_tbs);
  55. if (counter > MAX_NUM_OF_TBS) {
  56. IWL_ERROR("Too many chunks: %i\n", counter);
  57. /* @todo issue fatal error, it is quite serious situation */
  58. return 0;
  59. }
  60. /* Unmap chunks, if any.
  61. * TFD info for odd chunks is different format than for even chunks. */
  62. for (i = 0; i < counter; i++) {
  63. index = i / 2;
  64. is_odd = i & 0x1;
  65. if (is_odd)
  66. pci_unmap_single(
  67. dev,
  68. IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
  69. (IWL_GET_BITS(bd->pa[index],
  70. tb2_addr_hi20) << 16),
  71. IWL_GET_BITS(bd->pa[index], tb2_len),
  72. PCI_DMA_TODEVICE);
  73. else if (i > 0)
  74. pci_unmap_single(dev,
  75. le32_to_cpu(bd->pa[index].tb1_addr),
  76. IWL_GET_BITS(bd->pa[index], tb1_len),
  77. PCI_DMA_TODEVICE);
  78. /* Free SKB, if any, for this chunk */
  79. if (txq->txb[txq->q.read_ptr].skb[i]) {
  80. struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[i];
  81. dev_kfree_skb(skb);
  82. txq->txb[txq->q.read_ptr].skb[i] = NULL;
  83. }
  84. }
  85. return 0;
  86. }
  87. EXPORT_SYMBOL(iwl_hw_txq_free_tfd);
  88. /**
  89. * iwl_tx_queue_free - Deallocate DMA queue.
  90. * @txq: Transmit queue to deallocate.
  91. *
  92. * Empty queue by removing and destroying all BD's.
  93. * Free all buffers.
  94. * 0-fill, but do not free "txq" descriptor structure.
  95. */
  96. static void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  97. {
  98. struct iwl4965_queue *q = &txq->q;
  99. struct pci_dev *dev = priv->pci_dev;
  100. int len;
  101. if (q->n_bd == 0)
  102. return;
  103. /* first, empty all BD's */
  104. for (; q->write_ptr != q->read_ptr;
  105. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
  106. iwl_hw_txq_free_tfd(priv, txq);
  107. len = sizeof(struct iwl_cmd) * q->n_window;
  108. if (q->id == IWL_CMD_QUEUE_NUM)
  109. len += IWL_MAX_SCAN_SIZE;
  110. /* De-alloc array of command/tx buffers */
  111. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  112. /* De-alloc circular buffer of TFDs */
  113. if (txq->q.n_bd)
  114. pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
  115. txq->q.n_bd, txq->bd, txq->q.dma_addr);
  116. /* De-alloc array of per-TFD driver data */
  117. kfree(txq->txb);
  118. txq->txb = NULL;
  119. /* 0-fill queue descriptor structure */
  120. memset(txq, 0, sizeof(*txq));
  121. }
  122. /**
  123. * iwl_hw_txq_ctx_free - Free TXQ Context
  124. *
  125. * Destroy all TX DMA queues and structures
  126. */
  127. void iwl_hw_txq_ctx_free(struct iwl_priv *priv)
  128. {
  129. int txq_id;
  130. /* Tx queues */
  131. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  132. iwl_tx_queue_free(priv, &priv->txq[txq_id]);
  133. /* Keep-warm buffer */
  134. iwl_kw_free(priv);
  135. }
  136. EXPORT_SYMBOL(iwl_hw_txq_ctx_free);
  137. /**
  138. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  139. */
  140. static int iwl_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
  141. int count, int slots_num, u32 id)
  142. {
  143. q->n_bd = count;
  144. q->n_window = slots_num;
  145. q->id = id;
  146. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  147. * and iwl_queue_dec_wrap are broken. */
  148. BUG_ON(!is_power_of_2(count));
  149. /* slots_num must be power-of-two size, otherwise
  150. * get_cmd_index is broken. */
  151. BUG_ON(!is_power_of_2(slots_num));
  152. q->low_mark = q->n_window / 4;
  153. if (q->low_mark < 4)
  154. q->low_mark = 4;
  155. q->high_mark = q->n_window / 8;
  156. if (q->high_mark < 2)
  157. q->high_mark = 2;
  158. q->write_ptr = q->read_ptr = 0;
  159. return 0;
  160. }
  161. /**
  162. * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  163. */
  164. static int iwl_tx_queue_alloc(struct iwl_priv *priv,
  165. struct iwl_tx_queue *txq, u32 id)
  166. {
  167. struct pci_dev *dev = priv->pci_dev;
  168. /* Driver private data, only for Tx (not command) queues,
  169. * not shared with device. */
  170. if (id != IWL_CMD_QUEUE_NUM) {
  171. txq->txb = kmalloc(sizeof(txq->txb[0]) *
  172. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  173. if (!txq->txb) {
  174. IWL_ERROR("kmalloc for auxiliary BD "
  175. "structures failed\n");
  176. goto error;
  177. }
  178. } else
  179. txq->txb = NULL;
  180. /* Circular buffer of transmit frame descriptors (TFDs),
  181. * shared with device */
  182. txq->bd = pci_alloc_consistent(dev,
  183. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
  184. &txq->q.dma_addr);
  185. if (!txq->bd) {
  186. IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
  187. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
  188. goto error;
  189. }
  190. txq->q.id = id;
  191. return 0;
  192. error:
  193. kfree(txq->txb);
  194. txq->txb = NULL;
  195. return -ENOMEM;
  196. }
  197. /*
  198. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  199. * given Tx queue, and enable the DMA channel used for that queue.
  200. *
  201. * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  202. * channels supported in hardware.
  203. */
  204. static int iwl_hw_tx_queue_init(struct iwl_priv *priv,
  205. struct iwl_tx_queue *txq)
  206. {
  207. int rc;
  208. unsigned long flags;
  209. int txq_id = txq->q.id;
  210. spin_lock_irqsave(&priv->lock, flags);
  211. rc = iwl_grab_nic_access(priv);
  212. if (rc) {
  213. spin_unlock_irqrestore(&priv->lock, flags);
  214. return rc;
  215. }
  216. /* Circular buffer (TFD queue in DRAM) physical base address */
  217. iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  218. txq->q.dma_addr >> 8);
  219. /* Enable DMA channel, using same id as for TFD queue */
  220. iwl_write_direct32(
  221. priv, FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
  222. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  223. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
  224. iwl_release_nic_access(priv);
  225. spin_unlock_irqrestore(&priv->lock, flags);
  226. return 0;
  227. }
  228. /**
  229. * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
  230. */
  231. static int iwl_tx_queue_init(struct iwl_priv *priv,
  232. struct iwl_tx_queue *txq,
  233. int slots_num, u32 txq_id)
  234. {
  235. struct pci_dev *dev = priv->pci_dev;
  236. int len;
  237. int rc = 0;
  238. /*
  239. * Alloc buffer array for commands (Tx or other types of commands).
  240. * For the command queue (#4), allocate command space + one big
  241. * command for scan, since scan command is very huge; the system will
  242. * not have two scans at the same time, so only one is needed.
  243. * For normal Tx queues (all other queues), no super-size command
  244. * space is needed.
  245. */
  246. len = sizeof(struct iwl_cmd) * slots_num;
  247. if (txq_id == IWL_CMD_QUEUE_NUM)
  248. len += IWL_MAX_SCAN_SIZE;
  249. txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
  250. if (!txq->cmd)
  251. return -ENOMEM;
  252. /* Alloc driver data array and TFD circular buffer */
  253. rc = iwl_tx_queue_alloc(priv, txq, txq_id);
  254. if (rc) {
  255. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  256. return -ENOMEM;
  257. }
  258. txq->need_update = 0;
  259. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  260. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  261. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  262. /* Initialize queue's high/low-water marks, and head/tail indexes */
  263. iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  264. /* Tell device where to find queue */
  265. iwl_hw_tx_queue_init(priv, txq);
  266. return 0;
  267. }
  268. /**
  269. * iwl_txq_ctx_reset - Reset TX queue context
  270. * Destroys all DMA structures and initialise them again
  271. *
  272. * @param priv
  273. * @return error code
  274. */
  275. int iwl_txq_ctx_reset(struct iwl_priv *priv)
  276. {
  277. int ret = 0;
  278. int txq_id, slots_num;
  279. iwl_kw_free(priv);
  280. /* Free all tx/cmd queues and keep-warm buffer */
  281. iwl_hw_txq_ctx_free(priv);
  282. /* Alloc keep-warm buffer */
  283. ret = iwl_kw_alloc(priv);
  284. if (ret) {
  285. IWL_ERROR("Keep Warm allocation failed");
  286. goto error_kw;
  287. }
  288. /* Turn off all Tx DMA fifos */
  289. ret = priv->cfg->ops->lib->disable_tx_fifo(priv);
  290. if (unlikely(ret))
  291. goto error_reset;
  292. /* Tell nic where to find the keep-warm buffer */
  293. ret = iwl_kw_init(priv);
  294. if (ret) {
  295. IWL_ERROR("kw_init failed\n");
  296. goto error_reset;
  297. }
  298. /* Alloc and init all (default 16) Tx queues,
  299. * including the command queue (#4) */
  300. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  301. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  302. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  303. ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  304. txq_id);
  305. if (ret) {
  306. IWL_ERROR("Tx %d queue init failed\n", txq_id);
  307. goto error;
  308. }
  309. }
  310. return ret;
  311. error:
  312. iwl_hw_txq_ctx_free(priv);
  313. error_reset:
  314. iwl_kw_free(priv);
  315. error_kw:
  316. return ret;
  317. }