iwl-tx.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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_hw_txq_ctx_free - Free TXQ Context
  228. *
  229. * Destroy all TX DMA queues and structures
  230. */
  231. void iwl_hw_txq_ctx_free(struct iwl_priv *priv)
  232. {
  233. int txq_id;
  234. /* Tx queues */
  235. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  236. iwl_tx_queue_free(priv, &priv->txq[txq_id]);
  237. /* Keep-warm buffer */
  238. iwl_kw_free(priv);
  239. }
  240. EXPORT_SYMBOL(iwl_hw_txq_ctx_free);
  241. /**
  242. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  243. */
  244. static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
  245. int count, int slots_num, u32 id)
  246. {
  247. q->n_bd = count;
  248. q->n_window = slots_num;
  249. q->id = id;
  250. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  251. * and iwl_queue_dec_wrap are broken. */
  252. BUG_ON(!is_power_of_2(count));
  253. /* slots_num must be power-of-two size, otherwise
  254. * get_cmd_index is broken. */
  255. BUG_ON(!is_power_of_2(slots_num));
  256. q->low_mark = q->n_window / 4;
  257. if (q->low_mark < 4)
  258. q->low_mark = 4;
  259. q->high_mark = q->n_window / 8;
  260. if (q->high_mark < 2)
  261. q->high_mark = 2;
  262. q->write_ptr = q->read_ptr = 0;
  263. return 0;
  264. }
  265. /**
  266. * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  267. */
  268. static int iwl_tx_queue_alloc(struct iwl_priv *priv,
  269. struct iwl_tx_queue *txq, u32 id)
  270. {
  271. struct pci_dev *dev = priv->pci_dev;
  272. /* Driver private data, only for Tx (not command) queues,
  273. * not shared with device. */
  274. if (id != IWL_CMD_QUEUE_NUM) {
  275. txq->txb = kmalloc(sizeof(txq->txb[0]) *
  276. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  277. if (!txq->txb) {
  278. IWL_ERROR("kmalloc for auxiliary BD "
  279. "structures failed\n");
  280. goto error;
  281. }
  282. } else
  283. txq->txb = NULL;
  284. /* Circular buffer of transmit frame descriptors (TFDs),
  285. * shared with device */
  286. txq->bd = pci_alloc_consistent(dev,
  287. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
  288. &txq->q.dma_addr);
  289. if (!txq->bd) {
  290. IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
  291. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
  292. goto error;
  293. }
  294. txq->q.id = id;
  295. return 0;
  296. error:
  297. kfree(txq->txb);
  298. txq->txb = NULL;
  299. return -ENOMEM;
  300. }
  301. /*
  302. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  303. * given Tx queue, and enable the DMA channel used for that queue.
  304. *
  305. * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  306. * channels supported in hardware.
  307. */
  308. static int iwl_hw_tx_queue_init(struct iwl_priv *priv,
  309. struct iwl_tx_queue *txq)
  310. {
  311. int rc;
  312. unsigned long flags;
  313. int txq_id = txq->q.id;
  314. spin_lock_irqsave(&priv->lock, flags);
  315. rc = iwl_grab_nic_access(priv);
  316. if (rc) {
  317. spin_unlock_irqrestore(&priv->lock, flags);
  318. return rc;
  319. }
  320. /* Circular buffer (TFD queue in DRAM) physical base address */
  321. iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  322. txq->q.dma_addr >> 8);
  323. /* Enable DMA channel, using same id as for TFD queue */
  324. iwl_write_direct32(
  325. priv, FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
  326. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  327. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
  328. iwl_release_nic_access(priv);
  329. spin_unlock_irqrestore(&priv->lock, flags);
  330. return 0;
  331. }
  332. /**
  333. * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
  334. */
  335. static int iwl_tx_queue_init(struct iwl_priv *priv,
  336. struct iwl_tx_queue *txq,
  337. int slots_num, u32 txq_id)
  338. {
  339. struct pci_dev *dev = priv->pci_dev;
  340. int len;
  341. int rc = 0;
  342. /*
  343. * Alloc buffer array for commands (Tx or other types of commands).
  344. * For the command queue (#4), allocate command space + one big
  345. * command for scan, since scan command is very huge; the system will
  346. * not have two scans at the same time, so only one is needed.
  347. * For normal Tx queues (all other queues), no super-size command
  348. * space is needed.
  349. */
  350. len = sizeof(struct iwl_cmd) * slots_num;
  351. if (txq_id == IWL_CMD_QUEUE_NUM)
  352. len += IWL_MAX_SCAN_SIZE;
  353. txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
  354. if (!txq->cmd)
  355. return -ENOMEM;
  356. /* Alloc driver data array and TFD circular buffer */
  357. rc = iwl_tx_queue_alloc(priv, txq, txq_id);
  358. if (rc) {
  359. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  360. return -ENOMEM;
  361. }
  362. txq->need_update = 0;
  363. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  364. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  365. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  366. /* Initialize queue's high/low-water marks, and head/tail indexes */
  367. iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  368. /* Tell device where to find queue */
  369. iwl_hw_tx_queue_init(priv, txq);
  370. return 0;
  371. }
  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. iwl_kw_free(priv);
  384. /* Free all tx/cmd queues and keep-warm buffer */
  385. iwl_hw_txq_ctx_free(priv);
  386. /* Alloc keep-warm buffer */
  387. ret = iwl_kw_alloc(priv);
  388. if (ret) {
  389. IWL_ERROR("Keep Warm allocation failed");
  390. goto error_kw;
  391. }
  392. /* Turn off all Tx DMA fifos */
  393. ret = priv->cfg->ops->lib->disable_tx_fifo(priv);
  394. if (unlikely(ret))
  395. goto error_reset;
  396. /* Tell nic where to find the keep-warm buffer */
  397. ret = iwl_kw_init(priv);
  398. if (ret) {
  399. IWL_ERROR("kw_init failed\n");
  400. goto error_reset;
  401. }
  402. /* Alloc and init all (default 16) Tx queues,
  403. * including the command queue (#4) */
  404. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  405. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  406. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  407. ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  408. txq_id);
  409. if (ret) {
  410. IWL_ERROR("Tx %d queue init failed\n", txq_id);
  411. goto error;
  412. }
  413. }
  414. return ret;
  415. error:
  416. iwl_hw_txq_ctx_free(priv);
  417. error_reset:
  418. iwl_kw_free(priv);
  419. error_kw:
  420. return ret;
  421. }
  422. /*
  423. * handle build REPLY_TX command notification.
  424. */
  425. static void iwl_tx_cmd_build_basic(struct iwl_priv *priv,
  426. struct iwl_tx_cmd *tx_cmd,
  427. struct ieee80211_tx_control *ctrl,
  428. struct ieee80211_hdr *hdr,
  429. int is_unicast, u8 std_id)
  430. {
  431. u16 fc = le16_to_cpu(hdr->frame_control);
  432. __le32 tx_flags = tx_cmd->tx_flags;
  433. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  434. if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
  435. tx_flags |= TX_CMD_FLG_ACK_MSK;
  436. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
  437. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  438. if (ieee80211_is_probe_response(fc) &&
  439. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  440. tx_flags |= TX_CMD_FLG_TSF_MSK;
  441. } else {
  442. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  443. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  444. }
  445. if (ieee80211_is_back_request(fc))
  446. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  447. tx_cmd->sta_id = std_id;
  448. if (ieee80211_get_morefrag(hdr))
  449. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  450. if (ieee80211_is_qos_data(fc)) {
  451. u8 *qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
  452. tx_cmd->tid_tspec = qc[0] & 0xf;
  453. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  454. } else {
  455. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  456. }
  457. if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  458. tx_flags |= TX_CMD_FLG_RTS_MSK;
  459. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  460. } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
  461. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  462. tx_flags |= TX_CMD_FLG_CTS_MSK;
  463. }
  464. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  465. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  466. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  467. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
  468. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
  469. (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
  470. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  471. else
  472. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  473. } else {
  474. tx_cmd->timeout.pm_frame_timeout = 0;
  475. }
  476. tx_cmd->driver_txop = 0;
  477. tx_cmd->tx_flags = tx_flags;
  478. tx_cmd->next_frame_len = 0;
  479. }
  480. #define RTS_HCCA_RETRY_LIMIT 3
  481. #define RTS_DFAULT_RETRY_LIMIT 60
  482. static void iwl_tx_cmd_build_rate(struct iwl_priv *priv,
  483. struct iwl_tx_cmd *tx_cmd,
  484. struct ieee80211_tx_control *ctrl,
  485. u16 fc, int sta_id,
  486. int is_hcca)
  487. {
  488. u8 rts_retry_limit = 0;
  489. u8 data_retry_limit = 0;
  490. u8 rate_plcp;
  491. u16 rate_flags = 0;
  492. int rate_idx;
  493. rate_idx = min(ieee80211_get_tx_rate(priv->hw, ctrl)->hw_value & 0xffff,
  494. IWL_RATE_COUNT - 1);
  495. rate_plcp = iwl_rates[rate_idx].plcp;
  496. rts_retry_limit = (is_hcca) ?
  497. RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
  498. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  499. rate_flags |= RATE_MCS_CCK_MSK;
  500. if (ieee80211_is_probe_response(fc)) {
  501. data_retry_limit = 3;
  502. if (data_retry_limit < rts_retry_limit)
  503. rts_retry_limit = data_retry_limit;
  504. } else
  505. data_retry_limit = IWL_DEFAULT_TX_RETRY;
  506. if (priv->data_retry_limit != -1)
  507. data_retry_limit = priv->data_retry_limit;
  508. if (ieee80211_is_data(fc)) {
  509. tx_cmd->initial_rate_index = 0;
  510. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  511. } else {
  512. switch (fc & IEEE80211_FCTL_STYPE) {
  513. case IEEE80211_STYPE_AUTH:
  514. case IEEE80211_STYPE_DEAUTH:
  515. case IEEE80211_STYPE_ASSOC_REQ:
  516. case IEEE80211_STYPE_REASSOC_REQ:
  517. if (tx_cmd->tx_flags & TX_CMD_FLG_RTS_MSK) {
  518. tx_cmd->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  519. tx_cmd->tx_flags |= TX_CMD_FLG_CTS_MSK;
  520. }
  521. break;
  522. default:
  523. break;
  524. }
  525. /* Alternate between antenna A and B for successive frames */
  526. if (priv->use_ant_b_for_management_frame) {
  527. priv->use_ant_b_for_management_frame = 0;
  528. rate_flags |= RATE_MCS_ANT_B_MSK;
  529. } else {
  530. priv->use_ant_b_for_management_frame = 1;
  531. rate_flags |= RATE_MCS_ANT_A_MSK;
  532. }
  533. }
  534. tx_cmd->rts_retry_limit = rts_retry_limit;
  535. tx_cmd->data_retry_limit = data_retry_limit;
  536. tx_cmd->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
  537. }
  538. static void iwl_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  539. struct ieee80211_tx_control *ctl,
  540. struct iwl_tx_cmd *tx_cmd,
  541. struct sk_buff *skb_frag,
  542. int sta_id)
  543. {
  544. struct ieee80211_key_conf *keyconf = ctl->hw_key;
  545. switch (keyconf->alg) {
  546. case ALG_CCMP:
  547. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  548. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  549. if (ctl->flags & IEEE80211_TXCTL_AMPDU)
  550. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  551. IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
  552. break;
  553. case ALG_TKIP:
  554. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  555. ieee80211_get_tkip_key(keyconf, skb_frag,
  556. IEEE80211_TKIP_P2_KEY, tx_cmd->key);
  557. IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
  558. break;
  559. case ALG_WEP:
  560. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  561. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  562. if (keyconf->keylen == WEP_KEY_LEN_128)
  563. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  564. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  565. IWL_DEBUG_TX("Configuring packet for WEP encryption "
  566. "with key %d\n", keyconf->keyidx);
  567. break;
  568. default:
  569. printk(KERN_ERR "Unknown encode alg %d\n", keyconf->alg);
  570. break;
  571. }
  572. }
  573. static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  574. {
  575. /* 0 - mgmt, 1 - cnt, 2 - data */
  576. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  577. priv->tx_stats[idx].cnt++;
  578. priv->tx_stats[idx].bytes += len;
  579. }
  580. /*
  581. * start REPLY_TX command process
  582. */
  583. int iwl_tx_skb(struct iwl_priv *priv,
  584. struct sk_buff *skb, struct ieee80211_tx_control *ctl)
  585. {
  586. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  587. struct iwl_tfd_frame *tfd;
  588. u32 *control_flags;
  589. int txq_id = ctl->queue;
  590. struct iwl_tx_queue *txq = NULL;
  591. struct iwl_queue *q = NULL;
  592. dma_addr_t phys_addr;
  593. dma_addr_t txcmd_phys;
  594. dma_addr_t scratch_phys;
  595. struct iwl_cmd *out_cmd = NULL;
  596. struct iwl_tx_cmd *tx_cmd;
  597. u16 len, idx, len_org;
  598. u16 seq_number = 0;
  599. u8 id, hdr_len, unicast;
  600. u8 sta_id;
  601. u16 fc;
  602. u8 wait_write_ptr = 0;
  603. u8 tid = 0;
  604. u8 *qc = NULL;
  605. unsigned long flags;
  606. int ret;
  607. spin_lock_irqsave(&priv->lock, flags);
  608. if (iwl_is_rfkill(priv)) {
  609. IWL_DEBUG_DROP("Dropping - RF KILL\n");
  610. goto drop_unlock;
  611. }
  612. if (!priv->vif) {
  613. IWL_DEBUG_DROP("Dropping - !priv->vif\n");
  614. goto drop_unlock;
  615. }
  616. if ((ieee80211_get_tx_rate(priv->hw, ctl)->hw_value & 0xFF) ==
  617. IWL_INVALID_RATE) {
  618. IWL_ERROR("ERROR: No TX rate available.\n");
  619. goto drop_unlock;
  620. }
  621. unicast = !is_multicast_ether_addr(hdr->addr1);
  622. id = 0;
  623. fc = le16_to_cpu(hdr->frame_control);
  624. #ifdef CONFIG_IWLWIFI_DEBUG
  625. if (ieee80211_is_auth(fc))
  626. IWL_DEBUG_TX("Sending AUTH frame\n");
  627. else if (ieee80211_is_assoc_request(fc))
  628. IWL_DEBUG_TX("Sending ASSOC frame\n");
  629. else if (ieee80211_is_reassoc_request(fc))
  630. IWL_DEBUG_TX("Sending REASSOC frame\n");
  631. #endif
  632. /* drop all data frame if we are not associated */
  633. if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  634. (!iwl_is_associated(priv) ||
  635. ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
  636. !priv->assoc_station_added)) {
  637. IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
  638. goto drop_unlock;
  639. }
  640. spin_unlock_irqrestore(&priv->lock, flags);
  641. hdr_len = ieee80211_get_hdrlen(fc);
  642. /* Find (or create) index into station table for destination station */
  643. sta_id = iwl_get_sta_id(priv, hdr);
  644. if (sta_id == IWL_INVALID_STATION) {
  645. DECLARE_MAC_BUF(mac);
  646. IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
  647. print_mac(mac, hdr->addr1));
  648. goto drop;
  649. }
  650. IWL_DEBUG_TX("station Id %d\n", sta_id);
  651. if (ieee80211_is_qos_data(fc)) {
  652. qc = ieee80211_get_qos_ctrl(hdr, hdr_len);
  653. tid = qc[0] & 0xf;
  654. seq_number = priv->stations[sta_id].tid[tid].seq_number &
  655. IEEE80211_SCTL_SEQ;
  656. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  657. (hdr->seq_ctrl &
  658. __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
  659. seq_number += 0x10;
  660. #ifdef CONFIG_IWL4965_HT
  661. /* aggregation is on for this <sta,tid> */
  662. if (ctl->flags & IEEE80211_TXCTL_AMPDU)
  663. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  664. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  665. #endif /* CONFIG_IWL4965_HT */
  666. }
  667. /* Descriptor for chosen Tx queue */
  668. txq = &priv->txq[txq_id];
  669. q = &txq->q;
  670. spin_lock_irqsave(&priv->lock, flags);
  671. /* Set up first empty TFD within this queue's circular TFD buffer */
  672. tfd = &txq->bd[q->write_ptr];
  673. memset(tfd, 0, sizeof(*tfd));
  674. control_flags = (u32 *) tfd;
  675. idx = get_cmd_index(q, q->write_ptr, 0);
  676. /* Set up driver data for this TFD */
  677. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  678. txq->txb[q->write_ptr].skb[0] = skb;
  679. memcpy(&(txq->txb[q->write_ptr].status.control),
  680. ctl, sizeof(struct ieee80211_tx_control));
  681. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  682. out_cmd = &txq->cmd[idx];
  683. tx_cmd = &out_cmd->cmd.tx;
  684. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  685. memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
  686. /*
  687. * Set up the Tx-command (not MAC!) header.
  688. * Store the chosen Tx queue and TFD index within the sequence field;
  689. * after Tx, uCode's Tx response will return this value so driver can
  690. * locate the frame within the tx queue and do post-tx processing.
  691. */
  692. out_cmd->hdr.cmd = REPLY_TX;
  693. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  694. INDEX_TO_SEQ(q->write_ptr)));
  695. /* Copy MAC header from skb into command buffer */
  696. memcpy(tx_cmd->hdr, hdr, hdr_len);
  697. /*
  698. * Use the first empty entry in this queue's command buffer array
  699. * to contain the Tx command and MAC header concatenated together
  700. * (payload data will be in another buffer).
  701. * Size of this varies, due to varying MAC header length.
  702. * If end is not dword aligned, we'll have 2 extra bytes at the end
  703. * of the MAC header (device reads on dword boundaries).
  704. * We'll tell device about this padding later.
  705. */
  706. len = sizeof(struct iwl_tx_cmd) +
  707. sizeof(struct iwl_cmd_header) + hdr_len;
  708. len_org = len;
  709. len = (len + 3) & ~3;
  710. if (len_org != len)
  711. len_org = 1;
  712. else
  713. len_org = 0;
  714. /* Physical address of this Tx command's header (not MAC header!),
  715. * within command buffer array. */
  716. txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
  717. offsetof(struct iwl_cmd, hdr);
  718. /* Add buffer containing Tx command and MAC(!) header to TFD's
  719. * first entry */
  720. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
  721. if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
  722. iwl_tx_cmd_build_hwcrypto(priv, ctl, tx_cmd, skb, sta_id);
  723. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  724. * if any (802.11 null frames have no payload). */
  725. len = skb->len - hdr_len;
  726. if (len) {
  727. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  728. len, PCI_DMA_TODEVICE);
  729. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
  730. }
  731. /* Tell NIC about any 2-byte padding after MAC header */
  732. if (len_org)
  733. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  734. /* Total # bytes to be transmitted */
  735. len = (u16)skb->len;
  736. tx_cmd->len = cpu_to_le16(len);
  737. /* TODO need this for burst mode later on */
  738. iwl_tx_cmd_build_basic(priv, tx_cmd, ctl, hdr, unicast, sta_id);
  739. /* set is_hcca to 0; it probably will never be implemented */
  740. iwl_tx_cmd_build_rate(priv, tx_cmd, ctl, fc, sta_id, 0);
  741. iwl_update_tx_stats(priv, fc, len);
  742. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  743. offsetof(struct iwl_tx_cmd, scratch);
  744. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  745. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
  746. if (!ieee80211_get_morefrag(hdr)) {
  747. txq->need_update = 1;
  748. if (qc)
  749. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  750. } else {
  751. wait_write_ptr = 1;
  752. txq->need_update = 0;
  753. }
  754. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  755. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  756. /* Set up entry for this TFD in Tx byte-count array */
  757. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
  758. /* Tell device the write index *just past* this latest filled TFD */
  759. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  760. ret = iwl_txq_update_write_ptr(priv, txq);
  761. spin_unlock_irqrestore(&priv->lock, flags);
  762. if (ret)
  763. return ret;
  764. if ((iwl_queue_space(q) < q->high_mark)
  765. && priv->mac80211_registered) {
  766. if (wait_write_ptr) {
  767. spin_lock_irqsave(&priv->lock, flags);
  768. txq->need_update = 1;
  769. iwl_txq_update_write_ptr(priv, txq);
  770. spin_unlock_irqrestore(&priv->lock, flags);
  771. }
  772. ieee80211_stop_queue(priv->hw, ctl->queue);
  773. }
  774. return 0;
  775. drop_unlock:
  776. spin_unlock_irqrestore(&priv->lock, flags);
  777. drop:
  778. return -1;
  779. }
  780. EXPORT_SYMBOL(iwl_tx_skb);
  781. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  782. /**
  783. * iwl_enqueue_hcmd - enqueue a uCode command
  784. * @priv: device private data point
  785. * @cmd: a point to the ucode command structure
  786. *
  787. * The function returns < 0 values to indicate the operation is
  788. * failed. On success, it turns the index (> 0) of command in the
  789. * command queue.
  790. */
  791. int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  792. {
  793. struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  794. struct iwl_queue *q = &txq->q;
  795. struct iwl_tfd_frame *tfd;
  796. u32 *control_flags;
  797. struct iwl_cmd *out_cmd;
  798. u32 idx;
  799. u16 fix_size;
  800. dma_addr_t phys_addr;
  801. int ret;
  802. unsigned long flags;
  803. cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
  804. fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  805. /* If any of the command structures end up being larger than
  806. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  807. * we will need to increase the size of the TFD entries */
  808. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  809. !(cmd->meta.flags & CMD_SIZE_HUGE));
  810. if (iwl_is_rfkill(priv)) {
  811. IWL_DEBUG_INFO("Not sending command - RF KILL");
  812. return -EIO;
  813. }
  814. if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
  815. IWL_ERROR("No space for Tx\n");
  816. return -ENOSPC;
  817. }
  818. spin_lock_irqsave(&priv->hcmd_lock, flags);
  819. tfd = &txq->bd[q->write_ptr];
  820. memset(tfd, 0, sizeof(*tfd));
  821. control_flags = (u32 *) tfd;
  822. idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
  823. out_cmd = &txq->cmd[idx];
  824. out_cmd->hdr.cmd = cmd->id;
  825. memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
  826. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  827. /* At this point, the out_cmd now has all of the incoming cmd
  828. * information */
  829. out_cmd->hdr.flags = 0;
  830. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
  831. INDEX_TO_SEQ(q->write_ptr));
  832. if (out_cmd->meta.flags & CMD_SIZE_HUGE)
  833. out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
  834. phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
  835. offsetof(struct iwl_cmd, hdr);
  836. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
  837. IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
  838. "%d bytes at %d[%d]:%d\n",
  839. get_cmd_string(out_cmd->hdr.cmd),
  840. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  841. fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  842. txq->need_update = 1;
  843. /* Set up entry in queue's byte count circular buffer */
  844. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
  845. /* Increment and update queue's write index */
  846. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  847. ret = iwl_txq_update_write_ptr(priv, txq);
  848. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  849. return ret ? ret : idx;
  850. }