iwl-rx.c 37 KB

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