iwl-tx.c 44 KB

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