iwl-rx.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 <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. int 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. int ret = 0;
  129. spin_lock_irqsave(&q->lock, flags);
  130. if (q->need_update == 0)
  131. goto exit_unlock;
  132. /* If power-saving is in use, make sure device is awake */
  133. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  134. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  135. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  136. iwl_set_bit(priv, CSR_GP_CNTRL,
  137. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  138. goto exit_unlock;
  139. }
  140. q->write_actual = (q->write & ~0x7);
  141. iwl_write_direct32(priv, rx_wrt_ptr_reg, q->write_actual);
  142. /* Else device is assumed to be awake */
  143. } else {
  144. /* Device expects a multiple of 8 */
  145. q->write_actual = (q->write & ~0x7);
  146. iwl_write_direct32(priv, rx_wrt_ptr_reg, q->write_actual);
  147. }
  148. q->need_update = 0;
  149. exit_unlock:
  150. spin_unlock_irqrestore(&q->lock, flags);
  151. return ret;
  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. int 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. int ret = 0;
  181. spin_lock_irqsave(&rxq->lock, flags);
  182. write = rxq->write & ~0x7;
  183. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  184. /* Get next free Rx buffer, remove from free list */
  185. element = rxq->rx_free.next;
  186. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  187. list_del(element);
  188. /* Point to Rx buffer via next RBD in circular buffer */
  189. rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->aligned_dma_addr);
  190. rxq->queue[rxq->write] = rxb;
  191. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  192. rxq->free_count--;
  193. }
  194. spin_unlock_irqrestore(&rxq->lock, flags);
  195. /* If the pre-allocated buffer pool is dropping low, schedule to
  196. * refill it */
  197. if (rxq->free_count <= RX_LOW_WATERMARK)
  198. queue_work(priv->workqueue, &priv->rx_replenish);
  199. /* If we've added more space for the firmware to place data, tell it.
  200. * Increment device's write pointer in multiples of 8. */
  201. if (rxq->write_actual != (rxq->write & ~0x7)) {
  202. spin_lock_irqsave(&rxq->lock, flags);
  203. rxq->need_update = 1;
  204. spin_unlock_irqrestore(&rxq->lock, flags);
  205. ret = iwl_rx_queue_update_write_ptr(priv, rxq);
  206. }
  207. return ret;
  208. }
  209. EXPORT_SYMBOL(iwl_rx_queue_restock);
  210. /**
  211. * iwl_rx_replenish - Move all used packet from rx_used to rx_free
  212. *
  213. * When moving to rx_free an SKB is allocated for the slot.
  214. *
  215. * Also restock the Rx queue via iwl_rx_queue_restock.
  216. * This is called as a scheduled work item (except for during initialization)
  217. */
  218. void iwl_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  219. {
  220. struct iwl_rx_queue *rxq = &priv->rxq;
  221. struct list_head *element;
  222. struct iwl_rx_mem_buffer *rxb;
  223. struct sk_buff *skb;
  224. unsigned long flags;
  225. while (1) {
  226. spin_lock_irqsave(&rxq->lock, flags);
  227. if (list_empty(&rxq->rx_used)) {
  228. spin_unlock_irqrestore(&rxq->lock, flags);
  229. return;
  230. }
  231. spin_unlock_irqrestore(&rxq->lock, flags);
  232. if (rxq->free_count > RX_LOW_WATERMARK)
  233. priority |= __GFP_NOWARN;
  234. /* Alloc a new receive buffer */
  235. skb = alloc_skb(priv->hw_params.rx_buf_size + 256,
  236. priority);
  237. if (!skb) {
  238. if (net_ratelimit())
  239. IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
  240. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  241. net_ratelimit())
  242. IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
  243. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  244. rxq->free_count);
  245. /* We don't reschedule replenish work here -- we will
  246. * call the restock method and if it still needs
  247. * more buffers it will schedule replenish */
  248. break;
  249. }
  250. spin_lock_irqsave(&rxq->lock, flags);
  251. if (list_empty(&rxq->rx_used)) {
  252. spin_unlock_irqrestore(&rxq->lock, flags);
  253. dev_kfree_skb_any(skb);
  254. return;
  255. }
  256. element = rxq->rx_used.next;
  257. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  258. list_del(element);
  259. spin_unlock_irqrestore(&rxq->lock, flags);
  260. rxb->skb = skb;
  261. /* Get physical address of RB/SKB */
  262. rxb->real_dma_addr = pci_map_single(
  263. priv->pci_dev,
  264. rxb->skb->data,
  265. priv->hw_params.rx_buf_size + 256,
  266. PCI_DMA_FROMDEVICE);
  267. /* dma address must be no more than 36 bits */
  268. BUG_ON(rxb->real_dma_addr & ~DMA_BIT_MASK(36));
  269. /* and also 256 byte aligned! */
  270. rxb->aligned_dma_addr = ALIGN(rxb->real_dma_addr, 256);
  271. skb_reserve(rxb->skb, rxb->aligned_dma_addr - rxb->real_dma_addr);
  272. spin_lock_irqsave(&rxq->lock, flags);
  273. list_add_tail(&rxb->list, &rxq->rx_free);
  274. rxq->free_count++;
  275. priv->alloc_rxb_skb++;
  276. spin_unlock_irqrestore(&rxq->lock, flags);
  277. }
  278. }
  279. void iwl_rx_replenish(struct iwl_priv *priv)
  280. {
  281. unsigned long flags;
  282. iwl_rx_allocate(priv, GFP_KERNEL);
  283. spin_lock_irqsave(&priv->lock, flags);
  284. iwl_rx_queue_restock(priv);
  285. spin_unlock_irqrestore(&priv->lock, flags);
  286. }
  287. EXPORT_SYMBOL(iwl_rx_replenish);
  288. void iwl_rx_replenish_now(struct iwl_priv *priv)
  289. {
  290. iwl_rx_allocate(priv, GFP_ATOMIC);
  291. iwl_rx_queue_restock(priv);
  292. }
  293. EXPORT_SYMBOL(iwl_rx_replenish_now);
  294. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  295. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  296. * This free routine walks the list of POOL entries and if SKB is set to
  297. * non NULL it is unmapped and freed
  298. */
  299. void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  300. {
  301. int i;
  302. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  303. if (rxq->pool[i].skb != NULL) {
  304. pci_unmap_single(priv->pci_dev,
  305. rxq->pool[i].real_dma_addr,
  306. priv->hw_params.rx_buf_size + 256,
  307. PCI_DMA_FROMDEVICE);
  308. dev_kfree_skb(rxq->pool[i].skb);
  309. }
  310. }
  311. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  312. rxq->dma_addr);
  313. pci_free_consistent(priv->pci_dev, sizeof(struct iwl_rb_status),
  314. rxq->rb_stts, rxq->rb_stts_dma);
  315. rxq->bd = NULL;
  316. rxq->rb_stts = NULL;
  317. }
  318. EXPORT_SYMBOL(iwl_rx_queue_free);
  319. int iwl_rx_queue_alloc(struct iwl_priv *priv)
  320. {
  321. struct iwl_rx_queue *rxq = &priv->rxq;
  322. struct pci_dev *dev = priv->pci_dev;
  323. int i;
  324. spin_lock_init(&rxq->lock);
  325. INIT_LIST_HEAD(&rxq->rx_free);
  326. INIT_LIST_HEAD(&rxq->rx_used);
  327. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  328. rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
  329. if (!rxq->bd)
  330. goto err_bd;
  331. rxq->rb_stts = pci_alloc_consistent(dev, sizeof(struct iwl_rb_status),
  332. &rxq->rb_stts_dma);
  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. pci_free_consistent(priv->pci_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].skb != NULL) {
  364. pci_unmap_single(priv->pci_dev,
  365. rxq->pool[i].real_dma_addr,
  366. priv->hw_params.rx_buf_size + 256,
  367. PCI_DMA_FROMDEVICE);
  368. priv->alloc_rxb_skb--;
  369. dev_kfree_skb(rxq->pool[i].skb);
  370. rxq->pool[i].skb = NULL;
  371. }
  372. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  373. }
  374. /* Set us so that we have processed and used all buffers, but have
  375. * not restocked the Rx queue with fresh buffers */
  376. rxq->read = rxq->write = 0;
  377. rxq->write_actual = 0;
  378. rxq->free_count = 0;
  379. spin_unlock_irqrestore(&rxq->lock, flags);
  380. }
  381. int iwl_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  382. {
  383. u32 rb_size;
  384. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  385. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  386. if (!priv->cfg->use_isr_legacy)
  387. rb_timeout = RX_RB_TIMEOUT;
  388. if (priv->cfg->mod_params->amsdu_size_8K)
  389. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  390. else
  391. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  392. /* Stop Rx DMA */
  393. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  394. /* Reset driver's Rx queue write index */
  395. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  396. /* Tell device where to find RBD circular buffer in DRAM */
  397. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  398. (u32)(rxq->dma_addr >> 8));
  399. /* Tell device where in DRAM to update its Rx status */
  400. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  401. rxq->rb_stts_dma >> 4);
  402. /* Enable Rx DMA
  403. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  404. * the credit mechanism in 5000 HW RX FIFO
  405. * Direct rx interrupts to hosts
  406. * Rx buffer size 4 or 8k
  407. * RB timeout 0x10
  408. * 256 RBDs
  409. */
  410. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  411. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  412. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  413. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  414. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  415. rb_size|
  416. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  417. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  418. iwl_write32(priv, CSR_INT_COALESCING, 0x40);
  419. return 0;
  420. }
  421. int iwl_rxq_stop(struct iwl_priv *priv)
  422. {
  423. /* stop Rx DMA */
  424. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  425. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  426. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  427. return 0;
  428. }
  429. EXPORT_SYMBOL(iwl_rxq_stop);
  430. void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
  431. struct iwl_rx_mem_buffer *rxb)
  432. {
  433. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  434. struct iwl_missed_beacon_notif *missed_beacon;
  435. missed_beacon = &pkt->u.missed_beacon;
  436. if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
  437. IWL_DEBUG_CALIB(priv, "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  438. le32_to_cpu(missed_beacon->consequtive_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. /* Calculate noise level, based on measurements during network silence just
  448. * before arriving beacon. This measurement can be done only if we know
  449. * exactly when to expect beacons, therefore only when we're associated. */
  450. static void iwl_rx_calc_noise(struct iwl_priv *priv)
  451. {
  452. struct statistics_rx_non_phy *rx_info
  453. = &(priv->statistics.rx.general);
  454. int num_active_rx = 0;
  455. int total_silence = 0;
  456. int bcn_silence_a =
  457. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  458. int bcn_silence_b =
  459. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  460. int bcn_silence_c =
  461. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  462. if (bcn_silence_a) {
  463. total_silence += bcn_silence_a;
  464. num_active_rx++;
  465. }
  466. if (bcn_silence_b) {
  467. total_silence += bcn_silence_b;
  468. num_active_rx++;
  469. }
  470. if (bcn_silence_c) {
  471. total_silence += bcn_silence_c;
  472. num_active_rx++;
  473. }
  474. /* Average among active antennas */
  475. if (num_active_rx)
  476. priv->last_rx_noise = (total_silence / num_active_rx) - 107;
  477. else
  478. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  479. IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
  480. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  481. priv->last_rx_noise);
  482. }
  483. #ifdef CONFIG_IWLWIFI_DEBUG
  484. /*
  485. * based on the assumption of all statistics counter are in DWORD
  486. * FIXME: This function is for debugging, do not deal with
  487. * the case of counters roll-over.
  488. */
  489. static void iwl_accumulative_statistics(struct iwl_priv *priv,
  490. __le32 *stats)
  491. {
  492. int i;
  493. __le32 *prev_stats;
  494. u32 *accum_stats;
  495. prev_stats = (__le32 *)&priv->statistics;
  496. accum_stats = (u32 *)&priv->accum_statistics;
  497. for (i = sizeof(__le32); i < sizeof(struct iwl_notif_statistics);
  498. i += sizeof(__le32), stats++, prev_stats++, accum_stats++)
  499. if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats))
  500. *accum_stats += (le32_to_cpu(*stats) -
  501. le32_to_cpu(*prev_stats));
  502. /* reset accumulative statistics for "no-counter" type statistics */
  503. priv->accum_statistics.general.temperature =
  504. priv->statistics.general.temperature;
  505. priv->accum_statistics.general.temperature_m =
  506. priv->statistics.general.temperature_m;
  507. priv->accum_statistics.general.ttl_timestamp =
  508. priv->statistics.general.ttl_timestamp;
  509. priv->accum_statistics.tx.tx_power.ant_a =
  510. priv->statistics.tx.tx_power.ant_a;
  511. priv->accum_statistics.tx.tx_power.ant_b =
  512. priv->statistics.tx.tx_power.ant_b;
  513. priv->accum_statistics.tx.tx_power.ant_c =
  514. priv->statistics.tx.tx_power.ant_c;
  515. }
  516. #endif
  517. #define REG_RECALIB_PERIOD (60)
  518. void iwl_rx_statistics(struct iwl_priv *priv,
  519. struct iwl_rx_mem_buffer *rxb)
  520. {
  521. int change;
  522. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  523. IWL_DEBUG_RX(priv, "Statistics notification received (%d vs %d).\n",
  524. (int)sizeof(priv->statistics),
  525. le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK);
  526. change = ((priv->statistics.general.temperature !=
  527. pkt->u.stats.general.temperature) ||
  528. ((priv->statistics.flag &
  529. STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
  530. (pkt->u.stats.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
  531. #ifdef CONFIG_IWLWIFI_DEBUG
  532. iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats);
  533. #endif
  534. memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
  535. set_bit(STATUS_STATISTICS, &priv->status);
  536. /* Reschedule the statistics timer to occur in
  537. * REG_RECALIB_PERIOD seconds to ensure we get a
  538. * thermal update even if the uCode doesn't give
  539. * us one */
  540. mod_timer(&priv->statistics_periodic, jiffies +
  541. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  542. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  543. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  544. iwl_rx_calc_noise(priv);
  545. queue_work(priv->workqueue, &priv->run_time_calib_work);
  546. }
  547. iwl_leds_background(priv);
  548. if (priv->cfg->ops->lib->temp_ops.temperature && change)
  549. priv->cfg->ops->lib->temp_ops.temperature(priv);
  550. }
  551. EXPORT_SYMBOL(iwl_rx_statistics);
  552. #define PERFECT_RSSI (-20) /* dBm */
  553. #define WORST_RSSI (-95) /* dBm */
  554. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  555. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  556. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  557. * about formulas used below. */
  558. static int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
  559. {
  560. int sig_qual;
  561. int degradation = PERFECT_RSSI - rssi_dbm;
  562. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  563. * as indicator; formula is (signal dbm - noise dbm).
  564. * SNR at or above 40 is a great signal (100%).
  565. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  566. * Weakest usable signal is usually 10 - 15 dB SNR. */
  567. if (noise_dbm) {
  568. if (rssi_dbm - noise_dbm >= 40)
  569. return 100;
  570. else if (rssi_dbm < noise_dbm)
  571. return 0;
  572. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  573. /* Else use just the signal level.
  574. * This formula is a least squares fit of data points collected and
  575. * compared with a reference system that had a percentage (%) display
  576. * for signal quality. */
  577. } else
  578. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  579. (15 * RSSI_RANGE + 62 * degradation)) /
  580. (RSSI_RANGE * RSSI_RANGE);
  581. if (sig_qual > 100)
  582. sig_qual = 100;
  583. else if (sig_qual < 1)
  584. sig_qual = 0;
  585. return sig_qual;
  586. }
  587. /* Calc max signal level (dBm) among 3 possible receivers */
  588. static inline int iwl_calc_rssi(struct iwl_priv *priv,
  589. struct iwl_rx_phy_res *rx_resp)
  590. {
  591. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  592. }
  593. #ifdef CONFIG_IWLWIFI_DEBUG
  594. /**
  595. * iwl_dbg_report_frame - dump frame to syslog during debug sessions
  596. *
  597. * You may hack this function to show different aspects of received frames,
  598. * including selective frame dumps.
  599. * group100 parameter selects whether to show 1 out of 100 good data frames.
  600. * All beacon and probe response frames are printed.
  601. */
  602. static void iwl_dbg_report_frame(struct iwl_priv *priv,
  603. struct iwl_rx_phy_res *phy_res, u16 length,
  604. struct ieee80211_hdr *header, int group100)
  605. {
  606. u32 to_us;
  607. u32 print_summary = 0;
  608. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  609. u32 hundred = 0;
  610. u32 dataframe = 0;
  611. __le16 fc;
  612. u16 seq_ctl;
  613. u16 channel;
  614. u16 phy_flags;
  615. u32 rate_n_flags;
  616. u32 tsf_low;
  617. int rssi;
  618. if (likely(!(iwl_get_debug_level(priv) & IWL_DL_RX)))
  619. return;
  620. /* MAC header */
  621. fc = header->frame_control;
  622. seq_ctl = le16_to_cpu(header->seq_ctrl);
  623. /* metadata */
  624. channel = le16_to_cpu(phy_res->channel);
  625. phy_flags = le16_to_cpu(phy_res->phy_flags);
  626. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  627. /* signal statistics */
  628. rssi = iwl_calc_rssi(priv, phy_res);
  629. tsf_low = le64_to_cpu(phy_res->timestamp) & 0x0ffffffff;
  630. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  631. /* if data frame is to us and all is good,
  632. * (optionally) print summary for only 1 out of every 100 */
  633. if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
  634. cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  635. dataframe = 1;
  636. if (!group100)
  637. print_summary = 1; /* print each frame */
  638. else if (priv->framecnt_to_us < 100) {
  639. priv->framecnt_to_us++;
  640. print_summary = 0;
  641. } else {
  642. priv->framecnt_to_us = 0;
  643. print_summary = 1;
  644. hundred = 1;
  645. }
  646. } else {
  647. /* print summary for all other frames */
  648. print_summary = 1;
  649. }
  650. if (print_summary) {
  651. char *title;
  652. int rate_idx;
  653. u32 bitrate;
  654. if (hundred)
  655. title = "100Frames";
  656. else if (ieee80211_has_retry(fc))
  657. title = "Retry";
  658. else if (ieee80211_is_assoc_resp(fc))
  659. title = "AscRsp";
  660. else if (ieee80211_is_reassoc_resp(fc))
  661. title = "RasRsp";
  662. else if (ieee80211_is_probe_resp(fc)) {
  663. title = "PrbRsp";
  664. print_dump = 1; /* dump frame contents */
  665. } else if (ieee80211_is_beacon(fc)) {
  666. title = "Beacon";
  667. print_dump = 1; /* dump frame contents */
  668. } else if (ieee80211_is_atim(fc))
  669. title = "ATIM";
  670. else if (ieee80211_is_auth(fc))
  671. title = "Auth";
  672. else if (ieee80211_is_deauth(fc))
  673. title = "DeAuth";
  674. else if (ieee80211_is_disassoc(fc))
  675. title = "DisAssoc";
  676. else
  677. title = "Frame";
  678. rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
  679. if (unlikely((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT))) {
  680. bitrate = 0;
  681. WARN_ON_ONCE(1);
  682. } else {
  683. bitrate = iwl_rates[rate_idx].ieee / 2;
  684. }
  685. /* print frame summary.
  686. * MAC addresses show just the last byte (for brevity),
  687. * but you can hack it to show more, if you'd like to. */
  688. if (dataframe)
  689. IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
  690. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  691. title, le16_to_cpu(fc), header->addr1[5],
  692. length, rssi, channel, bitrate);
  693. else {
  694. /* src/dst addresses assume managed mode */
  695. IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, src=0x%02x, "
  696. "len=%u, rssi=%d, tim=%lu usec, "
  697. "phy=0x%02x, chnl=%d\n",
  698. title, le16_to_cpu(fc), header->addr1[5],
  699. header->addr3[5], length, rssi,
  700. tsf_low - priv->scan_start_tsf,
  701. phy_flags, channel);
  702. }
  703. }
  704. if (print_dump)
  705. iwl_print_hex_dump(priv, IWL_DL_RX, header, length);
  706. }
  707. #endif
  708. /*
  709. * returns non-zero if packet should be dropped
  710. */
  711. int iwl_set_decrypted_flag(struct iwl_priv *priv,
  712. struct ieee80211_hdr *hdr,
  713. u32 decrypt_res,
  714. struct ieee80211_rx_status *stats)
  715. {
  716. u16 fc = le16_to_cpu(hdr->frame_control);
  717. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  718. return 0;
  719. if (!(fc & IEEE80211_FCTL_PROTECTED))
  720. return 0;
  721. IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
  722. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  723. case RX_RES_STATUS_SEC_TYPE_TKIP:
  724. /* The uCode has got a bad phase 1 Key, pushes the packet.
  725. * Decryption will be done in SW. */
  726. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  727. RX_RES_STATUS_BAD_KEY_TTAK)
  728. break;
  729. case RX_RES_STATUS_SEC_TYPE_WEP:
  730. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  731. RX_RES_STATUS_BAD_ICV_MIC) {
  732. /* bad ICV, the packet is destroyed since the
  733. * decryption is inplace, drop it */
  734. IWL_DEBUG_RX(priv, "Packet destroyed\n");
  735. return -1;
  736. }
  737. case RX_RES_STATUS_SEC_TYPE_CCMP:
  738. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  739. RX_RES_STATUS_DECRYPT_OK) {
  740. IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
  741. stats->flag |= RX_FLAG_DECRYPTED;
  742. }
  743. break;
  744. default:
  745. break;
  746. }
  747. return 0;
  748. }
  749. EXPORT_SYMBOL(iwl_set_decrypted_flag);
  750. static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  751. {
  752. u32 decrypt_out = 0;
  753. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  754. RX_RES_STATUS_STATION_FOUND)
  755. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  756. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  757. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  758. /* packet was not encrypted */
  759. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  760. RX_RES_STATUS_SEC_TYPE_NONE)
  761. return decrypt_out;
  762. /* packet was encrypted with unknown alg */
  763. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  764. RX_RES_STATUS_SEC_TYPE_ERR)
  765. return decrypt_out;
  766. /* decryption was not done in HW */
  767. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  768. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  769. return decrypt_out;
  770. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  771. case RX_RES_STATUS_SEC_TYPE_CCMP:
  772. /* alg is CCM: check MIC only */
  773. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  774. /* Bad MIC */
  775. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  776. else
  777. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  778. break;
  779. case RX_RES_STATUS_SEC_TYPE_TKIP:
  780. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  781. /* Bad TTAK */
  782. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  783. break;
  784. }
  785. /* fall through if TTAK OK */
  786. default:
  787. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  788. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  789. else
  790. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  791. break;
  792. };
  793. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  794. decrypt_in, decrypt_out);
  795. return decrypt_out;
  796. }
  797. static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
  798. struct ieee80211_hdr *hdr,
  799. u16 len,
  800. u32 ampdu_status,
  801. struct iwl_rx_mem_buffer *rxb,
  802. struct ieee80211_rx_status *stats)
  803. {
  804. /* We only process data packets if the interface is open */
  805. if (unlikely(!priv->is_open)) {
  806. IWL_DEBUG_DROP_LIMIT(priv,
  807. "Dropping packet while interface is not open.\n");
  808. return;
  809. }
  810. /* In case of HW accelerated crypto and bad decryption, drop */
  811. if (!priv->cfg->mod_params->sw_crypto &&
  812. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  813. return;
  814. /* Resize SKB from mac header to end of packet */
  815. skb_reserve(rxb->skb, (void *)hdr - (void *)rxb->skb->data);
  816. skb_put(rxb->skb, len);
  817. iwl_update_stats(priv, false, hdr->frame_control, len);
  818. memcpy(IEEE80211_SKB_RXCB(rxb->skb), stats, sizeof(*stats));
  819. ieee80211_rx_irqsafe(priv->hw, rxb->skb);
  820. priv->alloc_rxb_skb--;
  821. rxb->skb = NULL;
  822. }
  823. /* This is necessary only for a number of statistics, see the caller. */
  824. static int iwl_is_network_packet(struct iwl_priv *priv,
  825. struct ieee80211_hdr *header)
  826. {
  827. /* Filter incoming packets to determine if they are targeted toward
  828. * this network, discarding packets coming from ourselves */
  829. switch (priv->iw_mode) {
  830. case NL80211_IFTYPE_ADHOC: /* Header: Dest. | Source | BSSID */
  831. /* packets to our IBSS update information */
  832. return !compare_ether_addr(header->addr3, priv->bssid);
  833. case NL80211_IFTYPE_STATION: /* Header: Dest. | AP{BSSID} | Source */
  834. /* packets to our IBSS update information */
  835. return !compare_ether_addr(header->addr2, priv->bssid);
  836. default:
  837. return 1;
  838. }
  839. }
  840. /* Called for REPLY_RX (legacy ABG frames), or
  841. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  842. void iwl_rx_reply_rx(struct iwl_priv *priv,
  843. struct iwl_rx_mem_buffer *rxb)
  844. {
  845. struct ieee80211_hdr *header;
  846. struct ieee80211_rx_status rx_status;
  847. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  848. struct iwl_rx_phy_res *phy_res;
  849. __le32 rx_pkt_status;
  850. struct iwl4965_rx_mpdu_res_start *amsdu;
  851. u32 len;
  852. u32 ampdu_status;
  853. u16 fc;
  854. u32 rate_n_flags;
  855. /**
  856. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  857. * REPLY_RX: physical layer info is in this buffer
  858. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  859. * command and cached in priv->last_phy_res
  860. *
  861. * Here we set up local variables depending on which command is
  862. * received.
  863. */
  864. if (pkt->hdr.cmd == REPLY_RX) {
  865. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  866. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  867. + phy_res->cfg_phy_cnt);
  868. len = le16_to_cpu(phy_res->byte_count);
  869. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  870. phy_res->cfg_phy_cnt + len);
  871. ampdu_status = le32_to_cpu(rx_pkt_status);
  872. } else {
  873. if (!priv->last_phy_res[0]) {
  874. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  875. return;
  876. }
  877. phy_res = (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  878. amsdu = (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  879. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  880. len = le16_to_cpu(amsdu->byte_count);
  881. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  882. ampdu_status = iwl_translate_rx_status(priv,
  883. le32_to_cpu(rx_pkt_status));
  884. }
  885. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  886. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  887. phy_res->cfg_phy_cnt);
  888. return;
  889. }
  890. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  891. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  892. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  893. le32_to_cpu(rx_pkt_status));
  894. return;
  895. }
  896. /* This will be used in several places later */
  897. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  898. /* rx_status carries information about the packet to mac80211 */
  899. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  900. rx_status.freq =
  901. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
  902. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  903. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  904. rx_status.rate_idx =
  905. iwl_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  906. rx_status.flag = 0;
  907. /* TSF isn't reliable. In order to allow smooth user experience,
  908. * this W/A doesn't propagate it to the mac80211 */
  909. /*rx_status.flag |= RX_FLAG_TSFT;*/
  910. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  911. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  912. rx_status.signal = iwl_calc_rssi(priv, phy_res);
  913. /* Meaningful noise values are available only from beacon statistics,
  914. * which are gathered only when associated, and indicate noise
  915. * only for the associated network channel ...
  916. * Ignore these noise values while scanning (other channels) */
  917. if (iwl_is_associated(priv) &&
  918. !test_bit(STATUS_SCANNING, &priv->status)) {
  919. rx_status.noise = priv->last_rx_noise;
  920. rx_status.qual = iwl_calc_sig_qual(rx_status.signal,
  921. rx_status.noise);
  922. } else {
  923. rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  924. rx_status.qual = iwl_calc_sig_qual(rx_status.signal, 0);
  925. }
  926. /* Reset beacon noise level if not associated. */
  927. if (!iwl_is_associated(priv))
  928. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  929. #ifdef CONFIG_IWLWIFI_DEBUG
  930. /* Set "1" to report good data frames in groups of 100 */
  931. if (unlikely(iwl_get_debug_level(priv) & IWL_DL_RX))
  932. iwl_dbg_report_frame(priv, phy_res, len, header, 1);
  933. #endif
  934. iwl_dbg_log_rx_data_frame(priv, len, header);
  935. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, noise %d, qual %d, TSF %llu\n",
  936. rx_status.signal, rx_status.noise, rx_status.qual,
  937. (unsigned long long)rx_status.mactime);
  938. /*
  939. * "antenna number"
  940. *
  941. * It seems that the antenna field in the phy flags value
  942. * is actually a bit field. This is undefined by radiotap,
  943. * it wants an actual antenna number but I always get "7"
  944. * for most legacy frames I receive indicating that the
  945. * same frame was received on all three RX chains.
  946. *
  947. * I think this field should be removed in favor of a
  948. * new 802.11n radiotap field "RX chains" that is defined
  949. * as a bitmask.
  950. */
  951. rx_status.antenna =
  952. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  953. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  954. /* set the preamble flag if appropriate */
  955. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  956. rx_status.flag |= RX_FLAG_SHORTPRE;
  957. /* Set up the HT phy flags */
  958. if (rate_n_flags & RATE_MCS_HT_MSK)
  959. rx_status.flag |= RX_FLAG_HT;
  960. if (rate_n_flags & RATE_MCS_HT40_MSK)
  961. rx_status.flag |= RX_FLAG_40MHZ;
  962. if (rate_n_flags & RATE_MCS_SGI_MSK)
  963. rx_status.flag |= RX_FLAG_SHORT_GI;
  964. if (iwl_is_network_packet(priv, header)) {
  965. priv->last_rx_rssi = rx_status.signal;
  966. priv->last_beacon_time = priv->ucode_beacon_time;
  967. priv->last_tsf = le64_to_cpu(phy_res->timestamp);
  968. }
  969. fc = le16_to_cpu(header->frame_control);
  970. switch (fc & IEEE80211_FCTL_FTYPE) {
  971. case IEEE80211_FTYPE_MGMT:
  972. case IEEE80211_FTYPE_DATA:
  973. if (priv->iw_mode == NL80211_IFTYPE_AP)
  974. iwl_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  975. header->addr2);
  976. /* fall through */
  977. default:
  978. iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  979. rxb, &rx_status);
  980. break;
  981. }
  982. }
  983. EXPORT_SYMBOL(iwl_rx_reply_rx);
  984. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  985. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  986. void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
  987. struct iwl_rx_mem_buffer *rxb)
  988. {
  989. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  990. priv->last_phy_res[0] = 1;
  991. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  992. sizeof(struct iwl_rx_phy_res));
  993. }
  994. EXPORT_SYMBOL(iwl_rx_reply_rx_phy);