iwl-tx.c 43 KB

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