iwl-rx.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2010 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 <asm/unaligned.h>
  32. #include "iwl-eeprom.h"
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-sta.h"
  36. #include "iwl-io.h"
  37. #include "iwl-calib.h"
  38. #include "iwl-helpers.h"
  39. /************************** RX-FUNCTIONS ****************************/
  40. /*
  41. * Rx theory of operation
  42. *
  43. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  44. * each of which point to Receive Buffers to be filled by the NIC. These get
  45. * used not only for Rx frames, but for any command response or notification
  46. * from the NIC. The driver and NIC manage the Rx buffers by means
  47. * of indexes into the circular buffer.
  48. *
  49. * Rx Queue Indexes
  50. * The host/firmware share two index registers for managing the Rx buffers.
  51. *
  52. * The READ index maps to the first position that the firmware may be writing
  53. * to -- the driver can read up to (but not including) this position and get
  54. * good data.
  55. * The READ index is managed by the firmware once the card is enabled.
  56. *
  57. * The WRITE index maps to the last position the driver has read from -- the
  58. * position preceding WRITE is the last slot the firmware can place a packet.
  59. *
  60. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  61. * WRITE = READ.
  62. *
  63. * During initialization, the host sets up the READ queue position to the first
  64. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  65. *
  66. * When the firmware places a packet in a buffer, it will advance the READ index
  67. * and fire the RX interrupt. The driver can then query the READ index and
  68. * process as many packets as possible, moving the WRITE index forward as it
  69. * resets the Rx queue buffers with new memory.
  70. *
  71. * The management in the driver is as follows:
  72. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  73. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  74. * to replenish the iwl->rxq->rx_free.
  75. * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
  76. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  77. * 'processed' and 'read' driver indexes as well)
  78. * + A received packet is processed and handed to the kernel network stack,
  79. * detached from the iwl->rxq. The driver 'processed' index is updated.
  80. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  81. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  82. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  83. * were enough free buffers and RX_STALLED is set it is cleared.
  84. *
  85. *
  86. * Driver sequence:
  87. *
  88. * iwl_rx_queue_alloc() Allocates rx_free
  89. * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
  90. * iwl_rx_queue_restock
  91. * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
  92. * queue, updates firmware pointers, and updates
  93. * the WRITE index. If insufficient rx_free buffers
  94. * are available, schedules iwl_rx_replenish
  95. *
  96. * -- enable interrupts --
  97. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  98. * READ INDEX, detaching the SKB from the pool.
  99. * Moves the packet buffer from queue to rx_used.
  100. * Calls iwl_rx_queue_restock to refill any empty
  101. * slots.
  102. * ...
  103. *
  104. */
  105. /**
  106. * iwl_rx_queue_space - Return number of free slots available in queue.
  107. */
  108. int iwl_rx_queue_space(const struct iwl_rx_queue *q)
  109. {
  110. int s = q->read - q->write;
  111. if (s <= 0)
  112. s += RX_QUEUE_SIZE;
  113. /* keep some buffer to not confuse full and empty queue */
  114. s -= 2;
  115. if (s < 0)
  116. s = 0;
  117. return s;
  118. }
  119. EXPORT_SYMBOL(iwl_rx_queue_space);
  120. /**
  121. * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  122. */
  123. void iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
  124. {
  125. unsigned long flags;
  126. u32 rx_wrt_ptr_reg = priv->hw_params.rx_wrt_ptr_reg;
  127. u32 reg;
  128. spin_lock_irqsave(&q->lock, flags);
  129. if (q->need_update == 0)
  130. goto exit_unlock;
  131. /* If power-saving is in use, make sure device is awake */
  132. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  133. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  134. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  135. IWL_DEBUG_INFO(priv, "Rx queue requesting wakeup, GP1 = 0x%x\n",
  136. reg);
  137. iwl_set_bit(priv, CSR_GP_CNTRL,
  138. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  139. goto exit_unlock;
  140. }
  141. q->write_actual = (q->write & ~0x7);
  142. iwl_write_direct32(priv, rx_wrt_ptr_reg, q->write_actual);
  143. /* Else device is assumed to be awake */
  144. } else {
  145. /* Device expects a multiple of 8 */
  146. q->write_actual = (q->write & ~0x7);
  147. iwl_write_direct32(priv, rx_wrt_ptr_reg, q->write_actual);
  148. }
  149. q->need_update = 0;
  150. exit_unlock:
  151. spin_unlock_irqrestore(&q->lock, flags);
  152. }
  153. EXPORT_SYMBOL(iwl_rx_queue_update_write_ptr);
  154. /**
  155. * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  156. */
  157. static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
  158. dma_addr_t dma_addr)
  159. {
  160. return cpu_to_le32((u32)(dma_addr >> 8));
  161. }
  162. /**
  163. * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
  164. *
  165. * If there are slots in the RX queue that need to be restocked,
  166. * and we have free pre-allocated buffers, fill the ranks as much
  167. * as we can, pulling from rx_free.
  168. *
  169. * This moves the 'write' index forward to catch up with 'processed', and
  170. * also updates the memory address in the firmware to reference the new
  171. * target buffer.
  172. */
  173. void iwl_rx_queue_restock(struct iwl_priv *priv)
  174. {
  175. struct iwl_rx_queue *rxq = &priv->rxq;
  176. struct list_head *element;
  177. struct iwl_rx_mem_buffer *rxb;
  178. unsigned long flags;
  179. int write;
  180. spin_lock_irqsave(&rxq->lock, flags);
  181. write = rxq->write & ~0x7;
  182. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  183. /* Get next free Rx buffer, remove from free list */
  184. element = rxq->rx_free.next;
  185. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  186. list_del(element);
  187. /* Point to Rx buffer via next RBD in circular buffer */
  188. rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->page_dma);
  189. rxq->queue[rxq->write] = rxb;
  190. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  191. rxq->free_count--;
  192. }
  193. spin_unlock_irqrestore(&rxq->lock, flags);
  194. /* If the pre-allocated buffer pool is dropping low, schedule to
  195. * refill it */
  196. if (rxq->free_count <= RX_LOW_WATERMARK)
  197. queue_work(priv->workqueue, &priv->rx_replenish);
  198. /* If we've added more space for the firmware to place data, tell it.
  199. * Increment device's write pointer in multiples of 8. */
  200. if (rxq->write_actual != (rxq->write & ~0x7)) {
  201. spin_lock_irqsave(&rxq->lock, flags);
  202. rxq->need_update = 1;
  203. spin_unlock_irqrestore(&rxq->lock, flags);
  204. iwl_rx_queue_update_write_ptr(priv, rxq);
  205. }
  206. }
  207. EXPORT_SYMBOL(iwl_rx_queue_restock);
  208. /**
  209. * iwl_rx_replenish - Move all used packet from rx_used to rx_free
  210. *
  211. * When moving to rx_free an SKB is allocated for the slot.
  212. *
  213. * Also restock the Rx queue via iwl_rx_queue_restock.
  214. * This is called as a scheduled work item (except for during initialization)
  215. */
  216. void iwl_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  217. {
  218. struct iwl_rx_queue *rxq = &priv->rxq;
  219. struct list_head *element;
  220. struct iwl_rx_mem_buffer *rxb;
  221. struct page *page;
  222. unsigned long flags;
  223. gfp_t gfp_mask = priority;
  224. while (1) {
  225. spin_lock_irqsave(&rxq->lock, flags);
  226. if (list_empty(&rxq->rx_used)) {
  227. spin_unlock_irqrestore(&rxq->lock, flags);
  228. return;
  229. }
  230. spin_unlock_irqrestore(&rxq->lock, flags);
  231. if (rxq->free_count > RX_LOW_WATERMARK)
  232. gfp_mask |= __GFP_NOWARN;
  233. if (priv->hw_params.rx_page_order > 0)
  234. gfp_mask |= __GFP_COMP;
  235. /* Alloc a new receive buffer */
  236. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  237. if (!page) {
  238. if (net_ratelimit())
  239. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  240. "order: %d\n",
  241. priv->hw_params.rx_page_order);
  242. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  243. net_ratelimit())
  244. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  245. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  246. rxq->free_count);
  247. /* We don't reschedule replenish work here -- we will
  248. * call the restock method and if it still needs
  249. * more buffers it will schedule replenish */
  250. return;
  251. }
  252. spin_lock_irqsave(&rxq->lock, flags);
  253. if (list_empty(&rxq->rx_used)) {
  254. spin_unlock_irqrestore(&rxq->lock, flags);
  255. __free_pages(page, priv->hw_params.rx_page_order);
  256. return;
  257. }
  258. element = rxq->rx_used.next;
  259. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  260. list_del(element);
  261. spin_unlock_irqrestore(&rxq->lock, flags);
  262. rxb->page = page;
  263. /* Get physical address of the RB */
  264. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  265. PAGE_SIZE << priv->hw_params.rx_page_order,
  266. PCI_DMA_FROMDEVICE);
  267. /* dma address must be no more than 36 bits */
  268. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  269. /* and also 256 byte aligned! */
  270. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  271. spin_lock_irqsave(&rxq->lock, flags);
  272. list_add_tail(&rxb->list, &rxq->rx_free);
  273. rxq->free_count++;
  274. priv->alloc_rxb_page++;
  275. spin_unlock_irqrestore(&rxq->lock, flags);
  276. }
  277. }
  278. void iwl_rx_replenish(struct iwl_priv *priv)
  279. {
  280. unsigned long flags;
  281. iwl_rx_allocate(priv, GFP_KERNEL);
  282. spin_lock_irqsave(&priv->lock, flags);
  283. iwl_rx_queue_restock(priv);
  284. spin_unlock_irqrestore(&priv->lock, flags);
  285. }
  286. EXPORT_SYMBOL(iwl_rx_replenish);
  287. void iwl_rx_replenish_now(struct iwl_priv *priv)
  288. {
  289. iwl_rx_allocate(priv, GFP_ATOMIC);
  290. iwl_rx_queue_restock(priv);
  291. }
  292. EXPORT_SYMBOL(iwl_rx_replenish_now);
  293. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  294. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  295. * This free routine walks the list of POOL entries and if SKB is set to
  296. * non NULL it is unmapped and freed
  297. */
  298. void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  299. {
  300. int i;
  301. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  302. if (rxq->pool[i].page != NULL) {
  303. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  304. PAGE_SIZE << priv->hw_params.rx_page_order,
  305. PCI_DMA_FROMDEVICE);
  306. __iwl_free_pages(priv, rxq->pool[i].page);
  307. rxq->pool[i].page = NULL;
  308. }
  309. }
  310. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  311. rxq->dma_addr);
  312. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  313. rxq->rb_stts, rxq->rb_stts_dma);
  314. rxq->bd = NULL;
  315. rxq->rb_stts = NULL;
  316. }
  317. EXPORT_SYMBOL(iwl_rx_queue_free);
  318. int iwl_rx_queue_alloc(struct iwl_priv *priv)
  319. {
  320. struct iwl_rx_queue *rxq = &priv->rxq;
  321. struct device *dev = &priv->pci_dev->dev;
  322. int i;
  323. spin_lock_init(&rxq->lock);
  324. INIT_LIST_HEAD(&rxq->rx_free);
  325. INIT_LIST_HEAD(&rxq->rx_used);
  326. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  327. rxq->bd = dma_alloc_coherent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr,
  328. GFP_KERNEL);
  329. if (!rxq->bd)
  330. goto err_bd;
  331. rxq->rb_stts = dma_alloc_coherent(dev, sizeof(struct iwl_rb_status),
  332. &rxq->rb_stts_dma, GFP_KERNEL);
  333. if (!rxq->rb_stts)
  334. goto err_rb;
  335. /* Fill the rx_used queue with _all_ of the Rx buffers */
  336. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  337. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  338. /* Set us so that we have processed and used all buffers, but have
  339. * not restocked the Rx queue with fresh buffers */
  340. rxq->read = rxq->write = 0;
  341. rxq->write_actual = 0;
  342. rxq->free_count = 0;
  343. rxq->need_update = 0;
  344. return 0;
  345. err_rb:
  346. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  347. rxq->dma_addr);
  348. err_bd:
  349. return -ENOMEM;
  350. }
  351. EXPORT_SYMBOL(iwl_rx_queue_alloc);
  352. void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  353. {
  354. unsigned long flags;
  355. int i;
  356. spin_lock_irqsave(&rxq->lock, flags);
  357. INIT_LIST_HEAD(&rxq->rx_free);
  358. INIT_LIST_HEAD(&rxq->rx_used);
  359. /* Fill the rx_used queue with _all_ of the Rx buffers */
  360. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  361. /* In the reset function, these buffers may have been allocated
  362. * to an SKB, so we need to unmap and free potential storage */
  363. if (rxq->pool[i].page != NULL) {
  364. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  365. PAGE_SIZE << priv->hw_params.rx_page_order,
  366. PCI_DMA_FROMDEVICE);
  367. __iwl_free_pages(priv, rxq->pool[i].page);
  368. rxq->pool[i].page = NULL;
  369. }
  370. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  371. }
  372. /* Set us so that we have processed and used all buffers, but have
  373. * not restocked the Rx queue with fresh buffers */
  374. rxq->read = rxq->write = 0;
  375. rxq->write_actual = 0;
  376. rxq->free_count = 0;
  377. spin_unlock_irqrestore(&rxq->lock, flags);
  378. }
  379. int iwl_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  380. {
  381. u32 rb_size;
  382. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  383. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  384. if (!priv->cfg->use_isr_legacy)
  385. rb_timeout = RX_RB_TIMEOUT;
  386. if (priv->cfg->mod_params->amsdu_size_8K)
  387. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  388. else
  389. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  390. /* Stop Rx DMA */
  391. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  392. /* Reset driver's Rx queue write index */
  393. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  394. /* Tell device where to find RBD circular buffer in DRAM */
  395. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  396. (u32)(rxq->dma_addr >> 8));
  397. /* Tell device where in DRAM to update its Rx status */
  398. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  399. rxq->rb_stts_dma >> 4);
  400. /* Enable Rx DMA
  401. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  402. * the credit mechanism in 5000 HW RX FIFO
  403. * Direct rx interrupts to hosts
  404. * Rx buffer size 4 or 8k
  405. * RB timeout 0x10
  406. * 256 RBDs
  407. */
  408. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  409. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  410. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  411. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  412. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  413. rb_size|
  414. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  415. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  416. /* Set interrupt coalescing timer to default (2048 usecs) */
  417. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  418. return 0;
  419. }
  420. int iwl_rxq_stop(struct iwl_priv *priv)
  421. {
  422. /* stop Rx DMA */
  423. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  424. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  425. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  426. return 0;
  427. }
  428. EXPORT_SYMBOL(iwl_rxq_stop);
  429. void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
  430. struct iwl_rx_mem_buffer *rxb)
  431. {
  432. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  433. struct iwl_missed_beacon_notif *missed_beacon;
  434. missed_beacon = &pkt->u.missed_beacon;
  435. if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
  436. priv->missed_beacon_threshold) {
  437. IWL_DEBUG_CALIB(priv, "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  438. le32_to_cpu(missed_beacon->consecutive_missed_beacons),
  439. le32_to_cpu(missed_beacon->total_missed_becons),
  440. le32_to_cpu(missed_beacon->num_recvd_beacons),
  441. le32_to_cpu(missed_beacon->num_expected_beacons));
  442. if (!test_bit(STATUS_SCANNING, &priv->status))
  443. iwl_init_sensitivity(priv);
  444. }
  445. }
  446. EXPORT_SYMBOL(iwl_rx_missed_beacon_notif);
  447. void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
  448. struct iwl_rx_mem_buffer *rxb)
  449. {
  450. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  451. struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
  452. if (!report->state) {
  453. IWL_DEBUG_11H(priv,
  454. "Spectrum Measure Notification: Start\n");
  455. return;
  456. }
  457. memcpy(&priv->measure_report, report, sizeof(*report));
  458. priv->measurement_status |= MEASUREMENT_READY;
  459. }
  460. EXPORT_SYMBOL(iwl_rx_spectrum_measure_notif);
  461. /* Calculate noise level, based on measurements during network silence just
  462. * before arriving beacon. This measurement can be done only if we know
  463. * exactly when to expect beacons, therefore only when we're associated. */
  464. static void iwl_rx_calc_noise(struct iwl_priv *priv)
  465. {
  466. struct statistics_rx_non_phy *rx_info
  467. = &(priv->statistics.rx.general);
  468. int num_active_rx = 0;
  469. int total_silence = 0;
  470. int bcn_silence_a =
  471. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  472. int bcn_silence_b =
  473. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  474. int bcn_silence_c =
  475. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  476. if (bcn_silence_a) {
  477. total_silence += bcn_silence_a;
  478. num_active_rx++;
  479. }
  480. if (bcn_silence_b) {
  481. total_silence += bcn_silence_b;
  482. num_active_rx++;
  483. }
  484. if (bcn_silence_c) {
  485. total_silence += bcn_silence_c;
  486. num_active_rx++;
  487. }
  488. /* Average among active antennas */
  489. if (num_active_rx)
  490. priv->last_rx_noise = (total_silence / num_active_rx) - 107;
  491. else
  492. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  493. IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
  494. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  495. priv->last_rx_noise);
  496. }
  497. #ifdef CONFIG_IWLWIFI_DEBUG
  498. /*
  499. * based on the assumption of all statistics counter are in DWORD
  500. * FIXME: This function is for debugging, do not deal with
  501. * the case of counters roll-over.
  502. */
  503. static void iwl_accumulative_statistics(struct iwl_priv *priv,
  504. __le32 *stats)
  505. {
  506. int i;
  507. __le32 *prev_stats;
  508. u32 *accum_stats;
  509. u32 *delta, *max_delta;
  510. prev_stats = (__le32 *)&priv->statistics;
  511. accum_stats = (u32 *)&priv->accum_statistics;
  512. delta = (u32 *)&priv->delta_statistics;
  513. max_delta = (u32 *)&priv->max_delta;
  514. for (i = sizeof(__le32); i < sizeof(struct iwl_notif_statistics);
  515. i += sizeof(__le32), stats++, prev_stats++, delta++,
  516. max_delta++, accum_stats++) {
  517. if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
  518. *delta = (le32_to_cpu(*stats) -
  519. le32_to_cpu(*prev_stats));
  520. *accum_stats += *delta;
  521. if (*delta > *max_delta)
  522. *max_delta = *delta;
  523. }
  524. }
  525. /* reset accumulative statistics for "no-counter" type statistics */
  526. priv->accum_statistics.general.temperature =
  527. priv->statistics.general.temperature;
  528. priv->accum_statistics.general.temperature_m =
  529. priv->statistics.general.temperature_m;
  530. priv->accum_statistics.general.ttl_timestamp =
  531. priv->statistics.general.ttl_timestamp;
  532. priv->accum_statistics.tx.tx_power.ant_a =
  533. priv->statistics.tx.tx_power.ant_a;
  534. priv->accum_statistics.tx.tx_power.ant_b =
  535. priv->statistics.tx.tx_power.ant_b;
  536. priv->accum_statistics.tx.tx_power.ant_c =
  537. priv->statistics.tx.tx_power.ant_c;
  538. }
  539. #endif
  540. #define REG_RECALIB_PERIOD (60)
  541. /* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
  542. #define ACK_CNT_RATIO (50)
  543. #define BA_TIMEOUT_CNT (5)
  544. #define BA_TIMEOUT_MAX (16)
  545. /**
  546. * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
  547. *
  548. * When the ACK count ratio is 0 and aggregated BA timeout retries exceeding
  549. * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
  550. * operation state.
  551. */
  552. bool iwl_good_ack_health(struct iwl_priv *priv,
  553. struct iwl_rx_packet *pkt)
  554. {
  555. bool rc = true;
  556. int actual_ack_cnt_delta, expected_ack_cnt_delta;
  557. int ba_timeout_delta;
  558. actual_ack_cnt_delta =
  559. le32_to_cpu(pkt->u.stats.tx.actual_ack_cnt) -
  560. le32_to_cpu(priv->statistics.tx.actual_ack_cnt);
  561. expected_ack_cnt_delta =
  562. le32_to_cpu(pkt->u.stats.tx.expected_ack_cnt) -
  563. le32_to_cpu(priv->statistics.tx.expected_ack_cnt);
  564. ba_timeout_delta =
  565. le32_to_cpu(pkt->u.stats.tx.agg.ba_timeout) -
  566. le32_to_cpu(priv->statistics.tx.agg.ba_timeout);
  567. if ((priv->_agn.agg_tids_count > 0) &&
  568. (expected_ack_cnt_delta > 0) &&
  569. (((actual_ack_cnt_delta * 100) / expected_ack_cnt_delta)
  570. < ACK_CNT_RATIO) &&
  571. (ba_timeout_delta > BA_TIMEOUT_CNT)) {
  572. IWL_DEBUG_RADIO(priv, "actual_ack_cnt delta = %d,"
  573. " expected_ack_cnt = %d\n",
  574. actual_ack_cnt_delta, expected_ack_cnt_delta);
  575. #ifdef CONFIG_IWLWIFI_DEBUG
  576. IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta = %d\n",
  577. priv->delta_statistics.tx.rx_detected_cnt);
  578. IWL_DEBUG_RADIO(priv,
  579. "ack_or_ba_timeout_collision delta = %d\n",
  580. priv->delta_statistics.tx.
  581. ack_or_ba_timeout_collision);
  582. #endif
  583. IWL_DEBUG_RADIO(priv, "agg ba_timeout delta = %d\n",
  584. ba_timeout_delta);
  585. if (!actual_ack_cnt_delta &&
  586. (ba_timeout_delta >= BA_TIMEOUT_MAX))
  587. rc = false;
  588. }
  589. return rc;
  590. }
  591. EXPORT_SYMBOL(iwl_good_ack_health);
  592. /**
  593. * iwl_good_plcp_health - checks for plcp error.
  594. *
  595. * When the plcp error is exceeding the thresholds, reset the radio
  596. * to improve the throughput.
  597. */
  598. bool iwl_good_plcp_health(struct iwl_priv *priv,
  599. struct iwl_rx_packet *pkt)
  600. {
  601. bool rc = true;
  602. int combined_plcp_delta;
  603. unsigned int plcp_msec;
  604. unsigned long plcp_received_jiffies;
  605. /*
  606. * check for plcp_err and trigger radio reset if it exceeds
  607. * the plcp error threshold plcp_delta.
  608. */
  609. plcp_received_jiffies = jiffies;
  610. plcp_msec = jiffies_to_msecs((long) plcp_received_jiffies -
  611. (long) priv->plcp_jiffies);
  612. priv->plcp_jiffies = plcp_received_jiffies;
  613. /*
  614. * check to make sure plcp_msec is not 0 to prevent division
  615. * by zero.
  616. */
  617. if (plcp_msec) {
  618. combined_plcp_delta =
  619. (le32_to_cpu(pkt->u.stats.rx.ofdm.plcp_err) -
  620. le32_to_cpu(priv->statistics.rx.ofdm.plcp_err)) +
  621. (le32_to_cpu(pkt->u.stats.rx.ofdm_ht.plcp_err) -
  622. le32_to_cpu(priv->statistics.rx.ofdm_ht.plcp_err));
  623. if ((combined_plcp_delta > 0) &&
  624. ((combined_plcp_delta * 100) / plcp_msec) >
  625. priv->cfg->plcp_delta_threshold) {
  626. /*
  627. * if plcp_err exceed the threshold,
  628. * the following data is printed in csv format:
  629. * Text: plcp_err exceeded %d,
  630. * Received ofdm.plcp_err,
  631. * Current ofdm.plcp_err,
  632. * Received ofdm_ht.plcp_err,
  633. * Current ofdm_ht.plcp_err,
  634. * combined_plcp_delta,
  635. * plcp_msec
  636. */
  637. IWL_DEBUG_RADIO(priv, "plcp_err exceeded %u, "
  638. "%u, %u, %u, %u, %d, %u mSecs\n",
  639. priv->cfg->plcp_delta_threshold,
  640. le32_to_cpu(pkt->u.stats.rx.ofdm.plcp_err),
  641. le32_to_cpu(priv->statistics.rx.ofdm.plcp_err),
  642. le32_to_cpu(pkt->u.stats.rx.ofdm_ht.plcp_err),
  643. le32_to_cpu(
  644. priv->statistics.rx.ofdm_ht.plcp_err),
  645. combined_plcp_delta, plcp_msec);
  646. rc = false;
  647. }
  648. }
  649. return rc;
  650. }
  651. EXPORT_SYMBOL(iwl_good_plcp_health);
  652. static void iwl_recover_from_statistics(struct iwl_priv *priv,
  653. struct iwl_rx_packet *pkt)
  654. {
  655. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  656. return;
  657. if (iwl_is_associated(priv)) {
  658. if (priv->cfg->ops->lib->check_ack_health) {
  659. if (!priv->cfg->ops->lib->check_ack_health(
  660. priv, pkt)) {
  661. /*
  662. * low ack count detected
  663. * restart Firmware
  664. */
  665. IWL_ERR(priv, "low ack count detected, "
  666. "restart firmware\n");
  667. iwl_force_reset(priv, IWL_FW_RESET);
  668. }
  669. } else if (priv->cfg->ops->lib->check_plcp_health) {
  670. if (!priv->cfg->ops->lib->check_plcp_health(
  671. priv, pkt)) {
  672. /*
  673. * high plcp error detected
  674. * reset Radio
  675. */
  676. iwl_force_reset(priv, IWL_RF_RESET);
  677. }
  678. }
  679. }
  680. }
  681. void iwl_rx_statistics(struct iwl_priv *priv,
  682. struct iwl_rx_mem_buffer *rxb)
  683. {
  684. int change;
  685. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  686. IWL_DEBUG_RX(priv, "Statistics notification received (%d vs %d).\n",
  687. (int)sizeof(priv->statistics),
  688. le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK);
  689. change = ((priv->statistics.general.temperature !=
  690. pkt->u.stats.general.temperature) ||
  691. ((priv->statistics.flag &
  692. STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
  693. (pkt->u.stats.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
  694. #ifdef CONFIG_IWLWIFI_DEBUG
  695. iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats);
  696. #endif
  697. iwl_recover_from_statistics(priv, pkt);
  698. memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
  699. set_bit(STATUS_STATISTICS, &priv->status);
  700. /* Reschedule the statistics timer to occur in
  701. * REG_RECALIB_PERIOD seconds to ensure we get a
  702. * thermal update even if the uCode doesn't give
  703. * us one */
  704. mod_timer(&priv->statistics_periodic, jiffies +
  705. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  706. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  707. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  708. iwl_rx_calc_noise(priv);
  709. queue_work(priv->workqueue, &priv->run_time_calib_work);
  710. }
  711. if (priv->cfg->ops->lib->temp_ops.temperature && change)
  712. priv->cfg->ops->lib->temp_ops.temperature(priv);
  713. }
  714. EXPORT_SYMBOL(iwl_rx_statistics);
  715. void iwl_reply_statistics(struct iwl_priv *priv,
  716. struct iwl_rx_mem_buffer *rxb)
  717. {
  718. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  719. if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
  720. #ifdef CONFIG_IWLWIFI_DEBUG
  721. memset(&priv->accum_statistics, 0,
  722. sizeof(struct iwl_notif_statistics));
  723. memset(&priv->delta_statistics, 0,
  724. sizeof(struct iwl_notif_statistics));
  725. memset(&priv->max_delta, 0,
  726. sizeof(struct iwl_notif_statistics));
  727. #endif
  728. IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
  729. }
  730. iwl_rx_statistics(priv, rxb);
  731. }
  732. EXPORT_SYMBOL(iwl_reply_statistics);
  733. /* Calc max signal level (dBm) among 3 possible receivers */
  734. static inline int iwl_calc_rssi(struct iwl_priv *priv,
  735. struct iwl_rx_phy_res *rx_resp)
  736. {
  737. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  738. }
  739. #ifdef CONFIG_IWLWIFI_DEBUG
  740. /**
  741. * iwl_dbg_report_frame - dump frame to syslog during debug sessions
  742. *
  743. * You may hack this function to show different aspects of received frames,
  744. * including selective frame dumps.
  745. * group100 parameter selects whether to show 1 out of 100 good data frames.
  746. * All beacon and probe response frames are printed.
  747. */
  748. static void iwl_dbg_report_frame(struct iwl_priv *priv,
  749. struct iwl_rx_phy_res *phy_res, u16 length,
  750. struct ieee80211_hdr *header, int group100)
  751. {
  752. u32 to_us;
  753. u32 print_summary = 0;
  754. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  755. u32 hundred = 0;
  756. u32 dataframe = 0;
  757. __le16 fc;
  758. u16 seq_ctl;
  759. u16 channel;
  760. u16 phy_flags;
  761. u32 rate_n_flags;
  762. u32 tsf_low;
  763. int rssi;
  764. if (likely(!(iwl_get_debug_level(priv) & IWL_DL_RX)))
  765. return;
  766. /* MAC header */
  767. fc = header->frame_control;
  768. seq_ctl = le16_to_cpu(header->seq_ctrl);
  769. /* metadata */
  770. channel = le16_to_cpu(phy_res->channel);
  771. phy_flags = le16_to_cpu(phy_res->phy_flags);
  772. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  773. /* signal statistics */
  774. rssi = iwl_calc_rssi(priv, phy_res);
  775. tsf_low = le64_to_cpu(phy_res->timestamp) & 0x0ffffffff;
  776. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  777. /* if data frame is to us and all is good,
  778. * (optionally) print summary for only 1 out of every 100 */
  779. if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
  780. cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  781. dataframe = 1;
  782. if (!group100)
  783. print_summary = 1; /* print each frame */
  784. else if (priv->framecnt_to_us < 100) {
  785. priv->framecnt_to_us++;
  786. print_summary = 0;
  787. } else {
  788. priv->framecnt_to_us = 0;
  789. print_summary = 1;
  790. hundred = 1;
  791. }
  792. } else {
  793. /* print summary for all other frames */
  794. print_summary = 1;
  795. }
  796. if (print_summary) {
  797. char *title;
  798. int rate_idx;
  799. u32 bitrate;
  800. if (hundred)
  801. title = "100Frames";
  802. else if (ieee80211_has_retry(fc))
  803. title = "Retry";
  804. else if (ieee80211_is_assoc_resp(fc))
  805. title = "AscRsp";
  806. else if (ieee80211_is_reassoc_resp(fc))
  807. title = "RasRsp";
  808. else if (ieee80211_is_probe_resp(fc)) {
  809. title = "PrbRsp";
  810. print_dump = 1; /* dump frame contents */
  811. } else if (ieee80211_is_beacon(fc)) {
  812. title = "Beacon";
  813. print_dump = 1; /* dump frame contents */
  814. } else if (ieee80211_is_atim(fc))
  815. title = "ATIM";
  816. else if (ieee80211_is_auth(fc))
  817. title = "Auth";
  818. else if (ieee80211_is_deauth(fc))
  819. title = "DeAuth";
  820. else if (ieee80211_is_disassoc(fc))
  821. title = "DisAssoc";
  822. else
  823. title = "Frame";
  824. rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
  825. if (unlikely((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT))) {
  826. bitrate = 0;
  827. WARN_ON_ONCE(1);
  828. } else {
  829. bitrate = iwl_rates[rate_idx].ieee / 2;
  830. }
  831. /* print frame summary.
  832. * MAC addresses show just the last byte (for brevity),
  833. * but you can hack it to show more, if you'd like to. */
  834. if (dataframe)
  835. IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
  836. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  837. title, le16_to_cpu(fc), header->addr1[5],
  838. length, rssi, channel, bitrate);
  839. else {
  840. /* src/dst addresses assume managed mode */
  841. IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, src=0x%02x, "
  842. "len=%u, rssi=%d, tim=%lu usec, "
  843. "phy=0x%02x, chnl=%d\n",
  844. title, le16_to_cpu(fc), header->addr1[5],
  845. header->addr3[5], length, rssi,
  846. tsf_low - priv->scan_start_tsf,
  847. phy_flags, channel);
  848. }
  849. }
  850. if (print_dump)
  851. iwl_print_hex_dump(priv, IWL_DL_RX, header, length);
  852. }
  853. #endif
  854. /*
  855. * returns non-zero if packet should be dropped
  856. */
  857. int iwl_set_decrypted_flag(struct iwl_priv *priv,
  858. struct ieee80211_hdr *hdr,
  859. u32 decrypt_res,
  860. struct ieee80211_rx_status *stats)
  861. {
  862. u16 fc = le16_to_cpu(hdr->frame_control);
  863. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  864. return 0;
  865. if (!(fc & IEEE80211_FCTL_PROTECTED))
  866. return 0;
  867. IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
  868. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  869. case RX_RES_STATUS_SEC_TYPE_TKIP:
  870. /* The uCode has got a bad phase 1 Key, pushes the packet.
  871. * Decryption will be done in SW. */
  872. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  873. RX_RES_STATUS_BAD_KEY_TTAK)
  874. break;
  875. case RX_RES_STATUS_SEC_TYPE_WEP:
  876. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  877. RX_RES_STATUS_BAD_ICV_MIC) {
  878. /* bad ICV, the packet is destroyed since the
  879. * decryption is inplace, drop it */
  880. IWL_DEBUG_RX(priv, "Packet destroyed\n");
  881. return -1;
  882. }
  883. case RX_RES_STATUS_SEC_TYPE_CCMP:
  884. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  885. RX_RES_STATUS_DECRYPT_OK) {
  886. IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
  887. stats->flag |= RX_FLAG_DECRYPTED;
  888. }
  889. break;
  890. default:
  891. break;
  892. }
  893. return 0;
  894. }
  895. EXPORT_SYMBOL(iwl_set_decrypted_flag);
  896. static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  897. {
  898. u32 decrypt_out = 0;
  899. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  900. RX_RES_STATUS_STATION_FOUND)
  901. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  902. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  903. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  904. /* packet was not encrypted */
  905. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  906. RX_RES_STATUS_SEC_TYPE_NONE)
  907. return decrypt_out;
  908. /* packet was encrypted with unknown alg */
  909. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  910. RX_RES_STATUS_SEC_TYPE_ERR)
  911. return decrypt_out;
  912. /* decryption was not done in HW */
  913. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  914. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  915. return decrypt_out;
  916. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  917. case RX_RES_STATUS_SEC_TYPE_CCMP:
  918. /* alg is CCM: check MIC only */
  919. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  920. /* Bad MIC */
  921. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  922. else
  923. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  924. break;
  925. case RX_RES_STATUS_SEC_TYPE_TKIP:
  926. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  927. /* Bad TTAK */
  928. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  929. break;
  930. }
  931. /* fall through if TTAK OK */
  932. default:
  933. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  934. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  935. else
  936. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  937. break;
  938. };
  939. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  940. decrypt_in, decrypt_out);
  941. return decrypt_out;
  942. }
  943. static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
  944. struct ieee80211_hdr *hdr,
  945. u16 len,
  946. u32 ampdu_status,
  947. struct iwl_rx_mem_buffer *rxb,
  948. struct ieee80211_rx_status *stats)
  949. {
  950. struct sk_buff *skb;
  951. int ret = 0;
  952. __le16 fc = hdr->frame_control;
  953. /* We only process data packets if the interface is open */
  954. if (unlikely(!priv->is_open)) {
  955. IWL_DEBUG_DROP_LIMIT(priv,
  956. "Dropping packet while interface is not open.\n");
  957. return;
  958. }
  959. /* In case of HW accelerated crypto and bad decryption, drop */
  960. if (!priv->cfg->mod_params->sw_crypto &&
  961. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  962. return;
  963. skb = alloc_skb(IWL_LINK_HDR_MAX * 2, GFP_ATOMIC);
  964. if (!skb) {
  965. IWL_ERR(priv, "alloc_skb failed\n");
  966. return;
  967. }
  968. skb_reserve(skb, IWL_LINK_HDR_MAX);
  969. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
  970. /* mac80211 currently doesn't support paged SKB. Convert it to
  971. * linear SKB for management frame and data frame requires
  972. * software decryption or software defragementation. */
  973. if (ieee80211_is_mgmt(fc) ||
  974. ieee80211_has_protected(fc) ||
  975. ieee80211_has_morefrags(fc) ||
  976. le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG ||
  977. (ieee80211_is_data_qos(fc) &&
  978. *ieee80211_get_qos_ctl(hdr) &
  979. IEEE80211_QOS_CONTROL_A_MSDU_PRESENT))
  980. ret = skb_linearize(skb);
  981. else
  982. ret = __pskb_pull_tail(skb, min_t(u16, IWL_LINK_HDR_MAX, len)) ?
  983. 0 : -ENOMEM;
  984. if (ret) {
  985. kfree_skb(skb);
  986. goto out;
  987. }
  988. /*
  989. * XXX: We cannot touch the page and its virtual memory (hdr) after
  990. * here. It might have already been freed by the above skb change.
  991. */
  992. iwl_update_stats(priv, false, fc, len);
  993. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  994. ieee80211_rx(priv->hw, skb);
  995. out:
  996. priv->alloc_rxb_page--;
  997. rxb->page = NULL;
  998. }
  999. /* Called for REPLY_RX (legacy ABG frames), or
  1000. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  1001. void iwl_rx_reply_rx(struct iwl_priv *priv,
  1002. struct iwl_rx_mem_buffer *rxb)
  1003. {
  1004. struct ieee80211_hdr *header;
  1005. struct ieee80211_rx_status rx_status;
  1006. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1007. struct iwl_rx_phy_res *phy_res;
  1008. __le32 rx_pkt_status;
  1009. struct iwl4965_rx_mpdu_res_start *amsdu;
  1010. u32 len;
  1011. u32 ampdu_status;
  1012. u32 rate_n_flags;
  1013. /**
  1014. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  1015. * REPLY_RX: physical layer info is in this buffer
  1016. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  1017. * command and cached in priv->last_phy_res
  1018. *
  1019. * Here we set up local variables depending on which command is
  1020. * received.
  1021. */
  1022. if (pkt->hdr.cmd == REPLY_RX) {
  1023. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  1024. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  1025. + phy_res->cfg_phy_cnt);
  1026. len = le16_to_cpu(phy_res->byte_count);
  1027. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  1028. phy_res->cfg_phy_cnt + len);
  1029. ampdu_status = le32_to_cpu(rx_pkt_status);
  1030. } else {
  1031. if (!priv->last_phy_res[0]) {
  1032. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  1033. return;
  1034. }
  1035. phy_res = (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  1036. amsdu = (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  1037. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  1038. len = le16_to_cpu(amsdu->byte_count);
  1039. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  1040. ampdu_status = iwl_translate_rx_status(priv,
  1041. le32_to_cpu(rx_pkt_status));
  1042. }
  1043. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  1044. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  1045. phy_res->cfg_phy_cnt);
  1046. return;
  1047. }
  1048. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  1049. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  1050. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  1051. le32_to_cpu(rx_pkt_status));
  1052. return;
  1053. }
  1054. /* This will be used in several places later */
  1055. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  1056. /* rx_status carries information about the packet to mac80211 */
  1057. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  1058. rx_status.freq =
  1059. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
  1060. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  1061. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  1062. rx_status.rate_idx =
  1063. iwl_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  1064. rx_status.flag = 0;
  1065. /* TSF isn't reliable. In order to allow smooth user experience,
  1066. * this W/A doesn't propagate it to the mac80211 */
  1067. /*rx_status.flag |= RX_FLAG_TSFT;*/
  1068. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  1069. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  1070. rx_status.signal = iwl_calc_rssi(priv, phy_res);
  1071. /* Meaningful noise values are available only from beacon statistics,
  1072. * which are gathered only when associated, and indicate noise
  1073. * only for the associated network channel ...
  1074. * Ignore these noise values while scanning (other channels) */
  1075. if (iwl_is_associated(priv) &&
  1076. !test_bit(STATUS_SCANNING, &priv->status)) {
  1077. rx_status.noise = priv->last_rx_noise;
  1078. } else {
  1079. rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  1080. }
  1081. /* Reset beacon noise level if not associated. */
  1082. if (!iwl_is_associated(priv))
  1083. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  1084. #ifdef CONFIG_IWLWIFI_DEBUG
  1085. /* Set "1" to report good data frames in groups of 100 */
  1086. if (unlikely(iwl_get_debug_level(priv) & IWL_DL_RX))
  1087. iwl_dbg_report_frame(priv, phy_res, len, header, 1);
  1088. #endif
  1089. iwl_dbg_log_rx_data_frame(priv, len, header);
  1090. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, noise %d, TSF %llu\n",
  1091. rx_status.signal, rx_status.noise,
  1092. (unsigned long long)rx_status.mactime);
  1093. /*
  1094. * "antenna number"
  1095. *
  1096. * It seems that the antenna field in the phy flags value
  1097. * is actually a bit field. This is undefined by radiotap,
  1098. * it wants an actual antenna number but I always get "7"
  1099. * for most legacy frames I receive indicating that the
  1100. * same frame was received on all three RX chains.
  1101. *
  1102. * I think this field should be removed in favor of a
  1103. * new 802.11n radiotap field "RX chains" that is defined
  1104. * as a bitmask.
  1105. */
  1106. rx_status.antenna =
  1107. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  1108. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  1109. /* set the preamble flag if appropriate */
  1110. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  1111. rx_status.flag |= RX_FLAG_SHORTPRE;
  1112. /* Set up the HT phy flags */
  1113. if (rate_n_flags & RATE_MCS_HT_MSK)
  1114. rx_status.flag |= RX_FLAG_HT;
  1115. if (rate_n_flags & RATE_MCS_HT40_MSK)
  1116. rx_status.flag |= RX_FLAG_40MHZ;
  1117. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1118. rx_status.flag |= RX_FLAG_SHORT_GI;
  1119. iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  1120. rxb, &rx_status);
  1121. }
  1122. EXPORT_SYMBOL(iwl_rx_reply_rx);
  1123. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  1124. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  1125. void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
  1126. struct iwl_rx_mem_buffer *rxb)
  1127. {
  1128. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1129. priv->last_phy_res[0] = 1;
  1130. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  1131. sizeof(struct iwl_rx_phy_res));
  1132. }
  1133. EXPORT_SYMBOL(iwl_rx_reply_rx_phy);