iwl-rx.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 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. * James P. Ketrenos <ipw2100-admin@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. u32 reg = 0;
  126. int ret = 0;
  127. unsigned long flags;
  128. spin_lock_irqsave(&q->lock, flags);
  129. if (q->need_update == 0)
  130. goto exit_unlock;
  131. /* If power-saving is in use, make sure device is awake */
  132. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  133. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  134. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  135. iwl_set_bit(priv, CSR_GP_CNTRL,
  136. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  137. goto exit_unlock;
  138. }
  139. ret = iwl_grab_nic_access(priv);
  140. if (ret)
  141. goto exit_unlock;
  142. /* Device expects a multiple of 8 */
  143. iwl_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
  144. q->write & ~0x7);
  145. iwl_release_nic_access(priv);
  146. /* Else device is assumed to be awake */
  147. } else
  148. /* Device expects a multiple of 8 */
  149. iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
  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->dma_addr);
  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 ((write != (rxq->write & ~0x7))
  204. || (abs(rxq->write - rxq->read) > 7)) {
  205. spin_lock_irqsave(&rxq->lock, flags);
  206. rxq->need_update = 1;
  207. spin_unlock_irqrestore(&rxq->lock, flags);
  208. ret = iwl_rx_queue_update_write_ptr(priv, rxq);
  209. }
  210. return ret;
  211. }
  212. EXPORT_SYMBOL(iwl_rx_queue_restock);
  213. /**
  214. * iwl_rx_replenish - Move all used packet from rx_used to rx_free
  215. *
  216. * When moving to rx_free an SKB is allocated for the slot.
  217. *
  218. * Also restock the Rx queue via iwl_rx_queue_restock.
  219. * This is called as a scheduled work item (except for during initialization)
  220. */
  221. void iwl_rx_allocate(struct iwl_priv *priv)
  222. {
  223. struct iwl_rx_queue *rxq = &priv->rxq;
  224. struct list_head *element;
  225. struct iwl_rx_mem_buffer *rxb;
  226. unsigned long flags;
  227. spin_lock_irqsave(&rxq->lock, flags);
  228. while (!list_empty(&rxq->rx_used)) {
  229. element = rxq->rx_used.next;
  230. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  231. /* Alloc a new receive buffer */
  232. rxb->skb = alloc_skb(priv->hw_params.rx_buf_size,
  233. __GFP_NOWARN | GFP_ATOMIC);
  234. if (!rxb->skb) {
  235. if (net_ratelimit())
  236. printk(KERN_CRIT DRV_NAME
  237. ": Can not allocate SKB buffers\n");
  238. /* We don't reschedule replenish work here -- we will
  239. * call the restock method and if it still needs
  240. * more buffers it will schedule replenish */
  241. break;
  242. }
  243. priv->alloc_rxb_skb++;
  244. list_del(element);
  245. /* Get physical address of RB/SKB */
  246. rxb->dma_addr =
  247. pci_map_single(priv->pci_dev, rxb->skb->data,
  248. priv->hw_params.rx_buf_size, PCI_DMA_FROMDEVICE);
  249. list_add_tail(&rxb->list, &rxq->rx_free);
  250. rxq->free_count++;
  251. }
  252. spin_unlock_irqrestore(&rxq->lock, flags);
  253. }
  254. EXPORT_SYMBOL(iwl_rx_allocate);
  255. void iwl_rx_replenish(struct iwl_priv *priv)
  256. {
  257. unsigned long flags;
  258. iwl_rx_allocate(priv);
  259. spin_lock_irqsave(&priv->lock, flags);
  260. iwl_rx_queue_restock(priv);
  261. spin_unlock_irqrestore(&priv->lock, flags);
  262. }
  263. EXPORT_SYMBOL(iwl_rx_replenish);
  264. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  265. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  266. * This free routine walks the list of POOL entries and if SKB is set to
  267. * non NULL it is unmapped and freed
  268. */
  269. void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  270. {
  271. int i;
  272. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  273. if (rxq->pool[i].skb != NULL) {
  274. pci_unmap_single(priv->pci_dev,
  275. rxq->pool[i].dma_addr,
  276. priv->hw_params.rx_buf_size,
  277. PCI_DMA_FROMDEVICE);
  278. dev_kfree_skb(rxq->pool[i].skb);
  279. }
  280. }
  281. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  282. rxq->dma_addr);
  283. rxq->bd = NULL;
  284. }
  285. EXPORT_SYMBOL(iwl_rx_queue_free);
  286. int iwl_rx_queue_alloc(struct iwl_priv *priv)
  287. {
  288. struct iwl_rx_queue *rxq = &priv->rxq;
  289. struct pci_dev *dev = priv->pci_dev;
  290. int i;
  291. spin_lock_init(&rxq->lock);
  292. INIT_LIST_HEAD(&rxq->rx_free);
  293. INIT_LIST_HEAD(&rxq->rx_used);
  294. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  295. rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
  296. if (!rxq->bd)
  297. return -ENOMEM;
  298. /* Fill the rx_used queue with _all_ of the Rx buffers */
  299. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  300. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  301. /* Set us so that we have processed and used all buffers, but have
  302. * not restocked the Rx queue with fresh buffers */
  303. rxq->read = rxq->write = 0;
  304. rxq->free_count = 0;
  305. rxq->need_update = 0;
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(iwl_rx_queue_alloc);
  309. void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  310. {
  311. unsigned long flags;
  312. int i;
  313. spin_lock_irqsave(&rxq->lock, flags);
  314. INIT_LIST_HEAD(&rxq->rx_free);
  315. INIT_LIST_HEAD(&rxq->rx_used);
  316. /* Fill the rx_used queue with _all_ of the Rx buffers */
  317. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  318. /* In the reset function, these buffers may have been allocated
  319. * to an SKB, so we need to unmap and free potential storage */
  320. if (rxq->pool[i].skb != NULL) {
  321. pci_unmap_single(priv->pci_dev,
  322. rxq->pool[i].dma_addr,
  323. priv->hw_params.rx_buf_size,
  324. PCI_DMA_FROMDEVICE);
  325. priv->alloc_rxb_skb--;
  326. dev_kfree_skb(rxq->pool[i].skb);
  327. rxq->pool[i].skb = NULL;
  328. }
  329. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  330. }
  331. /* Set us so that we have processed and used all buffers, but have
  332. * not restocked the Rx queue with fresh buffers */
  333. rxq->read = rxq->write = 0;
  334. rxq->free_count = 0;
  335. spin_unlock_irqrestore(&rxq->lock, flags);
  336. }
  337. EXPORT_SYMBOL(iwl_rx_queue_reset);
  338. int iwl_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  339. {
  340. int ret;
  341. unsigned long flags;
  342. unsigned int rb_size;
  343. spin_lock_irqsave(&priv->lock, flags);
  344. ret = iwl_grab_nic_access(priv);
  345. if (ret) {
  346. spin_unlock_irqrestore(&priv->lock, flags);
  347. return ret;
  348. }
  349. if (priv->cfg->mod_params->amsdu_size_8K)
  350. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  351. else
  352. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  353. /* Stop Rx DMA */
  354. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  355. /* Reset driver's Rx queue write index */
  356. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  357. /* Tell device where to find RBD circular buffer in DRAM */
  358. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  359. rxq->dma_addr >> 8);
  360. /* Tell device where in DRAM to update its Rx status */
  361. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  362. (priv->shared_phys + priv->rb_closed_offset) >> 4);
  363. /* Enable Rx DMA, enable host interrupt, Rx buffer size 4k, 256 RBDs */
  364. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  365. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  366. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  367. rb_size |
  368. /* 0x10 << 4 | */
  369. (RX_QUEUE_SIZE_LOG <<
  370. FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
  371. /*
  372. * iwl_write32(priv,CSR_INT_COAL_REG,0);
  373. */
  374. iwl_release_nic_access(priv);
  375. spin_unlock_irqrestore(&priv->lock, flags);
  376. return 0;
  377. }
  378. int iwl_rxq_stop(struct iwl_priv *priv)
  379. {
  380. int ret;
  381. unsigned long flags;
  382. spin_lock_irqsave(&priv->lock, flags);
  383. ret = iwl_grab_nic_access(priv);
  384. if (unlikely(ret)) {
  385. spin_unlock_irqrestore(&priv->lock, flags);
  386. return ret;
  387. }
  388. /* stop Rx DMA */
  389. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  390. ret = iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  391. (1 << 24), 1000);
  392. if (ret < 0)
  393. IWL_ERROR("Can't stop Rx DMA.\n");
  394. iwl_release_nic_access(priv);
  395. spin_unlock_irqrestore(&priv->lock, flags);
  396. return 0;
  397. }
  398. EXPORT_SYMBOL(iwl_rxq_stop);
  399. void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
  400. struct iwl_rx_mem_buffer *rxb)
  401. {
  402. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  403. struct iwl4965_missed_beacon_notif *missed_beacon;
  404. missed_beacon = &pkt->u.missed_beacon;
  405. if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
  406. IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  407. le32_to_cpu(missed_beacon->consequtive_missed_beacons),
  408. le32_to_cpu(missed_beacon->total_missed_becons),
  409. le32_to_cpu(missed_beacon->num_recvd_beacons),
  410. le32_to_cpu(missed_beacon->num_expected_beacons));
  411. if (!test_bit(STATUS_SCANNING, &priv->status))
  412. iwl_init_sensitivity(priv);
  413. }
  414. }
  415. EXPORT_SYMBOL(iwl_rx_missed_beacon_notif);
  416. int iwl_rx_agg_start(struct iwl_priv *priv, const u8 *addr, int tid, u16 ssn)
  417. {
  418. unsigned long flags;
  419. int sta_id;
  420. sta_id = iwl_find_station(priv, addr);
  421. if (sta_id == IWL_INVALID_STATION)
  422. return -ENXIO;
  423. spin_lock_irqsave(&priv->sta_lock, flags);
  424. priv->stations[sta_id].sta.station_flags_msk = 0;
  425. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  426. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  427. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  428. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  429. spin_unlock_irqrestore(&priv->sta_lock, flags);
  430. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  431. CMD_ASYNC);
  432. }
  433. EXPORT_SYMBOL(iwl_rx_agg_start);
  434. int iwl_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
  435. {
  436. unsigned long flags;
  437. int sta_id;
  438. sta_id = iwl_find_station(priv, addr);
  439. if (sta_id == IWL_INVALID_STATION)
  440. return -ENXIO;
  441. spin_lock_irqsave(&priv->sta_lock, flags);
  442. priv->stations[sta_id].sta.station_flags_msk = 0;
  443. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  444. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  445. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  446. spin_unlock_irqrestore(&priv->sta_lock, flags);
  447. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  448. CMD_ASYNC);
  449. }
  450. EXPORT_SYMBOL(iwl_rx_agg_stop);
  451. /* Calculate noise level, based on measurements during network silence just
  452. * before arriving beacon. This measurement can be done only if we know
  453. * exactly when to expect beacons, therefore only when we're associated. */
  454. static void iwl_rx_calc_noise(struct iwl_priv *priv)
  455. {
  456. struct statistics_rx_non_phy *rx_info
  457. = &(priv->statistics.rx.general);
  458. int num_active_rx = 0;
  459. int total_silence = 0;
  460. int bcn_silence_a =
  461. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  462. int bcn_silence_b =
  463. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  464. int bcn_silence_c =
  465. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  466. if (bcn_silence_a) {
  467. total_silence += bcn_silence_a;
  468. num_active_rx++;
  469. }
  470. if (bcn_silence_b) {
  471. total_silence += bcn_silence_b;
  472. num_active_rx++;
  473. }
  474. if (bcn_silence_c) {
  475. total_silence += bcn_silence_c;
  476. num_active_rx++;
  477. }
  478. /* Average among active antennas */
  479. if (num_active_rx)
  480. priv->last_rx_noise = (total_silence / num_active_rx) - 107;
  481. else
  482. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  483. IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
  484. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  485. priv->last_rx_noise);
  486. }
  487. #define REG_RECALIB_PERIOD (60)
  488. void iwl_rx_statistics(struct iwl_priv *priv,
  489. struct iwl_rx_mem_buffer *rxb)
  490. {
  491. int change;
  492. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  493. IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
  494. (int)sizeof(priv->statistics), pkt->len);
  495. change = ((priv->statistics.general.temperature !=
  496. pkt->u.stats.general.temperature) ||
  497. ((priv->statistics.flag &
  498. STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
  499. (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
  500. memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
  501. set_bit(STATUS_STATISTICS, &priv->status);
  502. /* Reschedule the statistics timer to occur in
  503. * REG_RECALIB_PERIOD seconds to ensure we get a
  504. * thermal update even if the uCode doesn't give
  505. * us one */
  506. mod_timer(&priv->statistics_periodic, jiffies +
  507. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  508. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  509. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  510. iwl_rx_calc_noise(priv);
  511. queue_work(priv->workqueue, &priv->run_time_calib_work);
  512. }
  513. iwl_leds_background(priv);
  514. if (priv->cfg->ops->lib->temperature && change)
  515. priv->cfg->ops->lib->temperature(priv);
  516. }
  517. EXPORT_SYMBOL(iwl_rx_statistics);
  518. #define PERFECT_RSSI (-20) /* dBm */
  519. #define WORST_RSSI (-95) /* dBm */
  520. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  521. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  522. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  523. * about formulas used below. */
  524. static int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
  525. {
  526. int sig_qual;
  527. int degradation = PERFECT_RSSI - rssi_dbm;
  528. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  529. * as indicator; formula is (signal dbm - noise dbm).
  530. * SNR at or above 40 is a great signal (100%).
  531. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  532. * Weakest usable signal is usually 10 - 15 dB SNR. */
  533. if (noise_dbm) {
  534. if (rssi_dbm - noise_dbm >= 40)
  535. return 100;
  536. else if (rssi_dbm < noise_dbm)
  537. return 0;
  538. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  539. /* Else use just the signal level.
  540. * This formula is a least squares fit of data points collected and
  541. * compared with a reference system that had a percentage (%) display
  542. * for signal quality. */
  543. } else
  544. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  545. (15 * RSSI_RANGE + 62 * degradation)) /
  546. (RSSI_RANGE * RSSI_RANGE);
  547. if (sig_qual > 100)
  548. sig_qual = 100;
  549. else if (sig_qual < 1)
  550. sig_qual = 0;
  551. return sig_qual;
  552. }
  553. #ifdef CONFIG_IWLWIFI_DEBUG
  554. /**
  555. * iwl_dbg_report_frame - dump frame to syslog during debug sessions
  556. *
  557. * You may hack this function to show different aspects of received frames,
  558. * including selective frame dumps.
  559. * group100 parameter selects whether to show 1 out of 100 good frames.
  560. *
  561. * TODO: This was originally written for 3945, need to audit for
  562. * proper operation with 4965.
  563. */
  564. static void iwl_dbg_report_frame(struct iwl_priv *priv,
  565. struct iwl_rx_packet *pkt,
  566. struct ieee80211_hdr *header, int group100)
  567. {
  568. u32 to_us;
  569. u32 print_summary = 0;
  570. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  571. u32 hundred = 0;
  572. u32 dataframe = 0;
  573. __le16 fc;
  574. u16 seq_ctl;
  575. u16 channel;
  576. u16 phy_flags;
  577. int rate_sym;
  578. u16 length;
  579. u16 status;
  580. u16 bcn_tmr;
  581. u32 tsf_low;
  582. u64 tsf;
  583. u8 rssi;
  584. u8 agc;
  585. u16 sig_avg;
  586. u16 noise_diff;
  587. struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
  588. struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
  589. struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
  590. u8 *data = IWL_RX_DATA(pkt);
  591. if (likely(!(priv->debug_level & IWL_DL_RX)))
  592. return;
  593. /* MAC header */
  594. fc = header->frame_control;
  595. seq_ctl = le16_to_cpu(header->seq_ctrl);
  596. /* metadata */
  597. channel = le16_to_cpu(rx_hdr->channel);
  598. phy_flags = le16_to_cpu(rx_hdr->phy_flags);
  599. rate_sym = rx_hdr->rate;
  600. length = le16_to_cpu(rx_hdr->len);
  601. /* end-of-frame status and timestamp */
  602. status = le32_to_cpu(rx_end->status);
  603. bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
  604. tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
  605. tsf = le64_to_cpu(rx_end->timestamp);
  606. /* signal statistics */
  607. rssi = rx_stats->rssi;
  608. agc = rx_stats->agc;
  609. sig_avg = le16_to_cpu(rx_stats->sig_avg);
  610. noise_diff = le16_to_cpu(rx_stats->noise_diff);
  611. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  612. /* if data frame is to us and all is good,
  613. * (optionally) print summary for only 1 out of every 100 */
  614. if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
  615. cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  616. dataframe = 1;
  617. if (!group100)
  618. print_summary = 1; /* print each frame */
  619. else if (priv->framecnt_to_us < 100) {
  620. priv->framecnt_to_us++;
  621. print_summary = 0;
  622. } else {
  623. priv->framecnt_to_us = 0;
  624. print_summary = 1;
  625. hundred = 1;
  626. }
  627. } else {
  628. /* print summary for all other frames */
  629. print_summary = 1;
  630. }
  631. if (print_summary) {
  632. char *title;
  633. int rate_idx;
  634. u32 bitrate;
  635. if (hundred)
  636. title = "100Frames";
  637. else if (ieee80211_has_retry(fc))
  638. title = "Retry";
  639. else if (ieee80211_is_assoc_resp(fc))
  640. title = "AscRsp";
  641. else if (ieee80211_is_reassoc_resp(fc))
  642. title = "RasRsp";
  643. else if (ieee80211_is_probe_resp(fc)) {
  644. title = "PrbRsp";
  645. print_dump = 1; /* dump frame contents */
  646. } else if (ieee80211_is_beacon(fc)) {
  647. title = "Beacon";
  648. print_dump = 1; /* dump frame contents */
  649. } else if (ieee80211_is_atim(fc))
  650. title = "ATIM";
  651. else if (ieee80211_is_auth(fc))
  652. title = "Auth";
  653. else if (ieee80211_is_deauth(fc))
  654. title = "DeAuth";
  655. else if (ieee80211_is_disassoc(fc))
  656. title = "DisAssoc";
  657. else
  658. title = "Frame";
  659. rate_idx = iwl_hwrate_to_plcp_idx(rate_sym);
  660. if (unlikely(rate_idx == -1))
  661. bitrate = 0;
  662. else
  663. bitrate = iwl_rates[rate_idx].ieee / 2;
  664. /* print frame summary.
  665. * MAC addresses show just the last byte (for brevity),
  666. * but you can hack it to show more, if you'd like to. */
  667. if (dataframe)
  668. IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
  669. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  670. title, le16_to_cpu(fc), header->addr1[5],
  671. length, rssi, channel, bitrate);
  672. else {
  673. /* src/dst addresses assume managed mode */
  674. IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
  675. "src=0x%02x, rssi=%u, tim=%lu usec, "
  676. "phy=0x%02x, chnl=%d\n",
  677. title, le16_to_cpu(fc), header->addr1[5],
  678. header->addr3[5], rssi,
  679. tsf_low - priv->scan_start_tsf,
  680. phy_flags, channel);
  681. }
  682. }
  683. if (print_dump)
  684. iwl_print_hex_dump(priv, IWL_DL_RX, data, length);
  685. }
  686. #else
  687. static inline void iwl_dbg_report_frame(struct iwl_priv *priv,
  688. struct iwl_rx_packet *pkt,
  689. struct ieee80211_hdr *header,
  690. int group100)
  691. {
  692. }
  693. #endif
  694. static void iwl_add_radiotap(struct iwl_priv *priv,
  695. struct sk_buff *skb,
  696. struct iwl4965_rx_phy_res *rx_start,
  697. struct ieee80211_rx_status *stats,
  698. u32 ampdu_status)
  699. {
  700. s8 signal = stats->signal;
  701. s8 noise = 0;
  702. int rate = stats->rate_idx;
  703. u64 tsf = stats->mactime;
  704. __le16 antenna;
  705. __le16 phy_flags_hw = rx_start->phy_flags;
  706. struct iwl4965_rt_rx_hdr {
  707. struct ieee80211_radiotap_header rt_hdr;
  708. __le64 rt_tsf; /* TSF */
  709. u8 rt_flags; /* radiotap packet flags */
  710. u8 rt_rate; /* rate in 500kb/s */
  711. __le16 rt_channelMHz; /* channel in MHz */
  712. __le16 rt_chbitmask; /* channel bitfield */
  713. s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
  714. s8 rt_dbmnoise;
  715. u8 rt_antenna; /* antenna number */
  716. } __attribute__ ((packed)) *iwl4965_rt;
  717. /* TODO: We won't have enough headroom for HT frames. Fix it later. */
  718. if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
  719. if (net_ratelimit())
  720. printk(KERN_ERR "not enough headroom [%d] for "
  721. "radiotap head [%zd]\n",
  722. skb_headroom(skb), sizeof(*iwl4965_rt));
  723. return;
  724. }
  725. /* put radiotap header in front of 802.11 header and data */
  726. iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
  727. /* initialise radiotap header */
  728. iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  729. iwl4965_rt->rt_hdr.it_pad = 0;
  730. /* total header + data */
  731. put_unaligned_le16(sizeof(*iwl4965_rt), &iwl4965_rt->rt_hdr.it_len);
  732. /* Indicate all the fields we add to the radiotap header */
  733. put_unaligned_le32((1 << IEEE80211_RADIOTAP_TSFT) |
  734. (1 << IEEE80211_RADIOTAP_FLAGS) |
  735. (1 << IEEE80211_RADIOTAP_RATE) |
  736. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  737. (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
  738. (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
  739. (1 << IEEE80211_RADIOTAP_ANTENNA),
  740. &(iwl4965_rt->rt_hdr.it_present));
  741. /* Zero the flags, we'll add to them as we go */
  742. iwl4965_rt->rt_flags = 0;
  743. put_unaligned_le64(tsf, &iwl4965_rt->rt_tsf);
  744. iwl4965_rt->rt_dbmsignal = signal;
  745. iwl4965_rt->rt_dbmnoise = noise;
  746. /* Convert the channel frequency and set the flags */
  747. put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
  748. if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
  749. put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ,
  750. &iwl4965_rt->rt_chbitmask);
  751. else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
  752. put_unaligned_le16(IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ,
  753. &iwl4965_rt->rt_chbitmask);
  754. else /* 802.11g */
  755. put_unaligned_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ,
  756. &iwl4965_rt->rt_chbitmask);
  757. if (rate == -1)
  758. iwl4965_rt->rt_rate = 0;
  759. else
  760. iwl4965_rt->rt_rate = iwl_rates[rate].ieee;
  761. /*
  762. * "antenna number"
  763. *
  764. * It seems that the antenna field in the phy flags value
  765. * is actually a bitfield. This is undefined by radiotap,
  766. * it wants an actual antenna number but I always get "7"
  767. * for most legacy frames I receive indicating that the
  768. * same frame was received on all three RX chains.
  769. *
  770. * I think this field should be removed in favour of a
  771. * new 802.11n radiotap field "RX chains" that is defined
  772. * as a bitmask.
  773. */
  774. antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
  775. iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
  776. /* set the preamble flag if appropriate */
  777. if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  778. iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
  779. stats->flag |= RX_FLAG_RADIOTAP;
  780. }
  781. static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  782. {
  783. /* 0 - mgmt, 1 - cnt, 2 - data */
  784. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  785. priv->rx_stats[idx].cnt++;
  786. priv->rx_stats[idx].bytes += len;
  787. }
  788. /*
  789. * returns non-zero if packet should be dropped
  790. */
  791. static int iwl_set_decrypted_flag(struct iwl_priv *priv,
  792. struct ieee80211_hdr *hdr,
  793. u32 decrypt_res,
  794. struct ieee80211_rx_status *stats)
  795. {
  796. u16 fc = le16_to_cpu(hdr->frame_control);
  797. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  798. return 0;
  799. if (!(fc & IEEE80211_FCTL_PROTECTED))
  800. return 0;
  801. IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
  802. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  803. case RX_RES_STATUS_SEC_TYPE_TKIP:
  804. /* The uCode has got a bad phase 1 Key, pushes the packet.
  805. * Decryption will be done in SW. */
  806. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  807. RX_RES_STATUS_BAD_KEY_TTAK)
  808. break;
  809. case RX_RES_STATUS_SEC_TYPE_WEP:
  810. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  811. RX_RES_STATUS_BAD_ICV_MIC) {
  812. /* bad ICV, the packet is destroyed since the
  813. * decryption is inplace, drop it */
  814. IWL_DEBUG_RX("Packet destroyed\n");
  815. return -1;
  816. }
  817. case RX_RES_STATUS_SEC_TYPE_CCMP:
  818. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  819. RX_RES_STATUS_DECRYPT_OK) {
  820. IWL_DEBUG_RX("hw decrypt successfully!!!\n");
  821. stats->flag |= RX_FLAG_DECRYPTED;
  822. }
  823. break;
  824. default:
  825. break;
  826. }
  827. return 0;
  828. }
  829. static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  830. {
  831. u32 decrypt_out = 0;
  832. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  833. RX_RES_STATUS_STATION_FOUND)
  834. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  835. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  836. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  837. /* packet was not encrypted */
  838. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  839. RX_RES_STATUS_SEC_TYPE_NONE)
  840. return decrypt_out;
  841. /* packet was encrypted with unknown alg */
  842. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  843. RX_RES_STATUS_SEC_TYPE_ERR)
  844. return decrypt_out;
  845. /* decryption was not done in HW */
  846. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  847. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  848. return decrypt_out;
  849. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  850. case RX_RES_STATUS_SEC_TYPE_CCMP:
  851. /* alg is CCM: check MIC only */
  852. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  853. /* Bad MIC */
  854. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  855. else
  856. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  857. break;
  858. case RX_RES_STATUS_SEC_TYPE_TKIP:
  859. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  860. /* Bad TTAK */
  861. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  862. break;
  863. }
  864. /* fall through if TTAK OK */
  865. default:
  866. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  867. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  868. else
  869. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  870. break;
  871. };
  872. IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
  873. decrypt_in, decrypt_out);
  874. return decrypt_out;
  875. }
  876. static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
  877. int include_phy,
  878. struct iwl_rx_mem_buffer *rxb,
  879. struct ieee80211_rx_status *stats)
  880. {
  881. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  882. struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
  883. (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
  884. struct ieee80211_hdr *hdr;
  885. u16 len;
  886. __le32 *rx_end;
  887. unsigned int skblen;
  888. u32 ampdu_status;
  889. u32 ampdu_status_legacy;
  890. if (!include_phy && priv->last_phy_res[0])
  891. rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
  892. if (!rx_start) {
  893. IWL_ERROR("MPDU frame without a PHY data\n");
  894. return;
  895. }
  896. if (include_phy) {
  897. hdr = (struct ieee80211_hdr *)((u8 *) &rx_start[1] +
  898. rx_start->cfg_phy_cnt);
  899. len = le16_to_cpu(rx_start->byte_count);
  900. rx_end = (__le32 *) ((u8 *) &pkt->u.raw[0] +
  901. sizeof(struct iwl4965_rx_phy_res) +
  902. rx_start->cfg_phy_cnt + len);
  903. } else {
  904. struct iwl4965_rx_mpdu_res_start *amsdu =
  905. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  906. hdr = (struct ieee80211_hdr *)(pkt->u.raw +
  907. sizeof(struct iwl4965_rx_mpdu_res_start));
  908. len = le16_to_cpu(amsdu->byte_count);
  909. rx_start->byte_count = amsdu->byte_count;
  910. rx_end = (__le32 *) (((u8 *) hdr) + len);
  911. }
  912. ampdu_status = le32_to_cpu(*rx_end);
  913. skblen = ((u8 *) rx_end - (u8 *) &pkt->u.raw[0]) + sizeof(u32);
  914. if (!include_phy) {
  915. /* New status scheme, need to translate */
  916. ampdu_status_legacy = ampdu_status;
  917. ampdu_status = iwl_translate_rx_status(priv, ampdu_status);
  918. }
  919. /* start from MAC */
  920. skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
  921. skb_put(rxb->skb, len); /* end where data ends */
  922. /* We only process data packets if the interface is open */
  923. if (unlikely(!priv->is_open)) {
  924. IWL_DEBUG_DROP_LIMIT
  925. ("Dropping packet while interface is not open.\n");
  926. return;
  927. }
  928. hdr = (struct ieee80211_hdr *)rxb->skb->data;
  929. /* in case of HW accelerated crypto and bad decryption, drop */
  930. if (!priv->hw_params.sw_crypto &&
  931. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  932. return;
  933. if (priv->add_radiotap)
  934. iwl_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
  935. iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
  936. ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
  937. priv->alloc_rxb_skb--;
  938. rxb->skb = NULL;
  939. }
  940. /* Calc max signal level (dBm) among 3 possible receivers */
  941. static int iwl_calc_rssi(struct iwl_priv *priv,
  942. struct iwl4965_rx_phy_res *rx_resp)
  943. {
  944. /* data from PHY/DSP regarding signal strength, etc.,
  945. * contents are always there, not configurable by host. */
  946. struct iwl4965_rx_non_cfg_phy *ncphy =
  947. (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
  948. u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
  949. >> IWL_AGC_DB_POS;
  950. u32 valid_antennae =
  951. (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
  952. >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
  953. u8 max_rssi = 0;
  954. u32 i;
  955. /* Find max rssi among 3 possible receivers.
  956. * These values are measured by the digital signal processor (DSP).
  957. * They should stay fairly constant even as the signal strength varies,
  958. * if the radio's automatic gain control (AGC) is working right.
  959. * AGC value (see below) will provide the "interesting" info. */
  960. for (i = 0; i < 3; i++)
  961. if (valid_antennae & (1 << i))
  962. max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
  963. IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  964. ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
  965. max_rssi, agc);
  966. /* dBm = max_rssi dB - agc dB - constant.
  967. * Higher AGC (higher radio gain) means lower signal. */
  968. return max_rssi - agc - IWL_RSSI_OFFSET;
  969. }
  970. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  971. {
  972. unsigned long flags;
  973. spin_lock_irqsave(&priv->sta_lock, flags);
  974. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  975. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  976. priv->stations[sta_id].sta.sta.modify_mask = 0;
  977. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  978. spin_unlock_irqrestore(&priv->sta_lock, flags);
  979. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  980. }
  981. static void iwl_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
  982. {
  983. /* FIXME: need locking over ps_status ??? */
  984. u8 sta_id = iwl_find_station(priv, addr);
  985. if (sta_id != IWL_INVALID_STATION) {
  986. u8 sta_awake = priv->stations[sta_id].
  987. ps_status == STA_PS_STATUS_WAKE;
  988. if (sta_awake && ps_bit)
  989. priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
  990. else if (!sta_awake && !ps_bit) {
  991. iwl_sta_modify_ps_wake(priv, sta_id);
  992. priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
  993. }
  994. }
  995. }
  996. /* This is necessary only for a number of statistics, see the caller. */
  997. static int iwl_is_network_packet(struct iwl_priv *priv,
  998. struct ieee80211_hdr *header)
  999. {
  1000. /* Filter incoming packets to determine if they are targeted toward
  1001. * this network, discarding packets coming from ourselves */
  1002. switch (priv->iw_mode) {
  1003. case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
  1004. /* packets to our IBSS update information */
  1005. return !compare_ether_addr(header->addr3, priv->bssid);
  1006. case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
  1007. /* packets to our IBSS update information */
  1008. return !compare_ether_addr(header->addr2, priv->bssid);
  1009. default:
  1010. return 1;
  1011. }
  1012. }
  1013. /* Called for REPLY_RX (legacy ABG frames), or
  1014. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  1015. void iwl_rx_reply_rx(struct iwl_priv *priv,
  1016. struct iwl_rx_mem_buffer *rxb)
  1017. {
  1018. struct ieee80211_hdr *header;
  1019. struct ieee80211_rx_status rx_status;
  1020. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1021. /* Use phy data (Rx signal strength, etc.) contained within
  1022. * this rx packet for legacy frames,
  1023. * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
  1024. int include_phy = (pkt->hdr.cmd == REPLY_RX);
  1025. struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
  1026. (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
  1027. (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
  1028. __le32 *rx_end;
  1029. unsigned int len = 0;
  1030. u16 fc;
  1031. u8 network_packet;
  1032. rx_status.mactime = le64_to_cpu(rx_start->timestamp);
  1033. rx_status.freq =
  1034. ieee80211_channel_to_frequency(le16_to_cpu(rx_start->channel));
  1035. rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  1036. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  1037. rx_status.rate_idx =
  1038. iwl_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
  1039. if (rx_status.band == IEEE80211_BAND_5GHZ)
  1040. rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
  1041. rx_status.antenna = 0;
  1042. rx_status.flag = 0;
  1043. rx_status.flag |= RX_FLAG_TSFT;
  1044. if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
  1045. IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
  1046. rx_start->cfg_phy_cnt);
  1047. return;
  1048. }
  1049. if (!include_phy) {
  1050. if (priv->last_phy_res[0])
  1051. rx_start = (struct iwl4965_rx_phy_res *)
  1052. &priv->last_phy_res[1];
  1053. else
  1054. rx_start = NULL;
  1055. }
  1056. if (!rx_start) {
  1057. IWL_ERROR("MPDU frame without a PHY data\n");
  1058. return;
  1059. }
  1060. if (include_phy) {
  1061. header = (struct ieee80211_hdr *)((u8 *) &rx_start[1]
  1062. + rx_start->cfg_phy_cnt);
  1063. len = le16_to_cpu(rx_start->byte_count);
  1064. rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
  1065. sizeof(struct iwl4965_rx_phy_res) + len);
  1066. } else {
  1067. struct iwl4965_rx_mpdu_res_start *amsdu =
  1068. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  1069. header = (void *)(pkt->u.raw +
  1070. sizeof(struct iwl4965_rx_mpdu_res_start));
  1071. len = le16_to_cpu(amsdu->byte_count);
  1072. rx_end = (__le32 *) (pkt->u.raw +
  1073. sizeof(struct iwl4965_rx_mpdu_res_start) + len);
  1074. }
  1075. if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
  1076. !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  1077. IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
  1078. le32_to_cpu(*rx_end));
  1079. return;
  1080. }
  1081. priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
  1082. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  1083. rx_status.signal = iwl_calc_rssi(priv, rx_start);
  1084. /* Meaningful noise values are available only from beacon statistics,
  1085. * which are gathered only when associated, and indicate noise
  1086. * only for the associated network channel ...
  1087. * Ignore these noise values while scanning (other channels) */
  1088. if (iwl_is_associated(priv) &&
  1089. !test_bit(STATUS_SCANNING, &priv->status)) {
  1090. rx_status.noise = priv->last_rx_noise;
  1091. rx_status.qual = iwl_calc_sig_qual(rx_status.signal,
  1092. rx_status.noise);
  1093. } else {
  1094. rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  1095. rx_status.qual = iwl_calc_sig_qual(rx_status.signal, 0);
  1096. }
  1097. /* Reset beacon noise level if not associated. */
  1098. if (!iwl_is_associated(priv))
  1099. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  1100. /* Set "1" to report good data frames in groups of 100 */
  1101. /* FIXME: need to optimze the call: */
  1102. iwl_dbg_report_frame(priv, pkt, header, 1);
  1103. IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
  1104. rx_status.signal, rx_status.noise, rx_status.signal,
  1105. (unsigned long long)rx_status.mactime);
  1106. /* Take shortcut when only in monitor mode */
  1107. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
  1108. iwl_pass_packet_to_mac80211(priv, include_phy,
  1109. rxb, &rx_status);
  1110. return;
  1111. }
  1112. network_packet = iwl_is_network_packet(priv, header);
  1113. if (network_packet) {
  1114. priv->last_rx_rssi = rx_status.signal;
  1115. priv->last_beacon_time = priv->ucode_beacon_time;
  1116. priv->last_tsf = le64_to_cpu(rx_start->timestamp);
  1117. }
  1118. fc = le16_to_cpu(header->frame_control);
  1119. switch (fc & IEEE80211_FCTL_FTYPE) {
  1120. case IEEE80211_FTYPE_MGMT:
  1121. case IEEE80211_FTYPE_DATA:
  1122. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  1123. iwl_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  1124. header->addr2);
  1125. /* fall through */
  1126. default:
  1127. iwl_pass_packet_to_mac80211(priv, include_phy, rxb,
  1128. &rx_status);
  1129. break;
  1130. }
  1131. }
  1132. EXPORT_SYMBOL(iwl_rx_reply_rx);
  1133. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  1134. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  1135. void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
  1136. struct iwl_rx_mem_buffer *rxb)
  1137. {
  1138. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1139. priv->last_phy_res[0] = 1;
  1140. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  1141. sizeof(struct iwl4965_rx_phy_res));
  1142. }
  1143. EXPORT_SYMBOL(iwl_rx_reply_rx_phy);