iwl-rx.c 35 KB

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