iwl-tx.c 42 KB

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