iwl-tx.c 44 KB

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