iwl-rx.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  695. {
  696. /* 0 - mgmt, 1 - cnt, 2 - data */
  697. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  698. priv->rx_stats[idx].cnt++;
  699. priv->rx_stats[idx].bytes += len;
  700. }
  701. /*
  702. * returns non-zero if packet should be dropped
  703. */
  704. static int iwl_set_decrypted_flag(struct iwl_priv *priv,
  705. struct ieee80211_hdr *hdr,
  706. u32 decrypt_res,
  707. struct ieee80211_rx_status *stats)
  708. {
  709. u16 fc = le16_to_cpu(hdr->frame_control);
  710. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  711. return 0;
  712. if (!(fc & IEEE80211_FCTL_PROTECTED))
  713. return 0;
  714. IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
  715. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  716. case RX_RES_STATUS_SEC_TYPE_TKIP:
  717. /* The uCode has got a bad phase 1 Key, pushes the packet.
  718. * Decryption will be done in SW. */
  719. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  720. RX_RES_STATUS_BAD_KEY_TTAK)
  721. break;
  722. case RX_RES_STATUS_SEC_TYPE_WEP:
  723. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  724. RX_RES_STATUS_BAD_ICV_MIC) {
  725. /* bad ICV, the packet is destroyed since the
  726. * decryption is inplace, drop it */
  727. IWL_DEBUG_RX("Packet destroyed\n");
  728. return -1;
  729. }
  730. case RX_RES_STATUS_SEC_TYPE_CCMP:
  731. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  732. RX_RES_STATUS_DECRYPT_OK) {
  733. IWL_DEBUG_RX("hw decrypt successfully!!!\n");
  734. stats->flag |= RX_FLAG_DECRYPTED;
  735. }
  736. break;
  737. default:
  738. break;
  739. }
  740. return 0;
  741. }
  742. static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  743. {
  744. u32 decrypt_out = 0;
  745. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  746. RX_RES_STATUS_STATION_FOUND)
  747. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  748. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  749. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  750. /* packet was not encrypted */
  751. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  752. RX_RES_STATUS_SEC_TYPE_NONE)
  753. return decrypt_out;
  754. /* packet was encrypted with unknown alg */
  755. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  756. RX_RES_STATUS_SEC_TYPE_ERR)
  757. return decrypt_out;
  758. /* decryption was not done in HW */
  759. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  760. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  761. return decrypt_out;
  762. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  763. case RX_RES_STATUS_SEC_TYPE_CCMP:
  764. /* alg is CCM: check MIC only */
  765. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  766. /* Bad MIC */
  767. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  768. else
  769. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  770. break;
  771. case RX_RES_STATUS_SEC_TYPE_TKIP:
  772. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  773. /* Bad TTAK */
  774. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  775. break;
  776. }
  777. /* fall through if TTAK OK */
  778. default:
  779. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  780. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  781. else
  782. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  783. break;
  784. };
  785. IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
  786. decrypt_in, decrypt_out);
  787. return decrypt_out;
  788. }
  789. static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
  790. int include_phy,
  791. struct iwl_rx_mem_buffer *rxb,
  792. struct ieee80211_rx_status *stats)
  793. {
  794. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  795. struct iwl_rx_phy_res *rx_start = (include_phy) ?
  796. (struct iwl_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
  797. struct ieee80211_hdr *hdr;
  798. u16 len;
  799. __le32 *rx_end;
  800. unsigned int skblen;
  801. u32 ampdu_status;
  802. u32 ampdu_status_legacy;
  803. if (!include_phy && priv->last_phy_res[0])
  804. rx_start = (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  805. if (!rx_start) {
  806. IWL_ERROR("MPDU frame without a PHY data\n");
  807. return;
  808. }
  809. if (include_phy) {
  810. hdr = (struct ieee80211_hdr *)((u8 *) &rx_start[1] +
  811. rx_start->cfg_phy_cnt);
  812. len = le16_to_cpu(rx_start->byte_count);
  813. rx_end = (__le32 *)((u8 *) &pkt->u.raw[0] +
  814. sizeof(struct iwl_rx_phy_res) +
  815. rx_start->cfg_phy_cnt + len);
  816. } else {
  817. struct iwl4965_rx_mpdu_res_start *amsdu =
  818. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  819. hdr = (struct ieee80211_hdr *)(pkt->u.raw +
  820. sizeof(struct iwl4965_rx_mpdu_res_start));
  821. len = le16_to_cpu(amsdu->byte_count);
  822. rx_start->byte_count = amsdu->byte_count;
  823. rx_end = (__le32 *) (((u8 *) hdr) + len);
  824. }
  825. ampdu_status = le32_to_cpu(*rx_end);
  826. skblen = ((u8 *) rx_end - (u8 *) &pkt->u.raw[0]) + sizeof(u32);
  827. if (!include_phy) {
  828. /* New status scheme, need to translate */
  829. ampdu_status_legacy = ampdu_status;
  830. ampdu_status = iwl_translate_rx_status(priv, ampdu_status);
  831. }
  832. /* start from MAC */
  833. skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
  834. skb_put(rxb->skb, len); /* end where data ends */
  835. /* We only process data packets if the interface is open */
  836. if (unlikely(!priv->is_open)) {
  837. IWL_DEBUG_DROP_LIMIT
  838. ("Dropping packet while interface is not open.\n");
  839. return;
  840. }
  841. hdr = (struct ieee80211_hdr *)rxb->skb->data;
  842. /* in case of HW accelerated crypto and bad decryption, drop */
  843. if (!priv->hw_params.sw_crypto &&
  844. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  845. return;
  846. iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
  847. ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
  848. priv->alloc_rxb_skb--;
  849. rxb->skb = NULL;
  850. }
  851. /* Calc max signal level (dBm) among 3 possible receivers */
  852. static inline int iwl_calc_rssi(struct iwl_priv *priv,
  853. struct iwl_rx_phy_res *rx_resp)
  854. {
  855. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  856. }
  857. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  858. {
  859. unsigned long flags;
  860. spin_lock_irqsave(&priv->sta_lock, flags);
  861. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  862. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  863. priv->stations[sta_id].sta.sta.modify_mask = 0;
  864. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  865. spin_unlock_irqrestore(&priv->sta_lock, flags);
  866. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  867. }
  868. static void iwl_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
  869. {
  870. /* FIXME: need locking over ps_status ??? */
  871. u8 sta_id = iwl_find_station(priv, addr);
  872. if (sta_id != IWL_INVALID_STATION) {
  873. u8 sta_awake = priv->stations[sta_id].
  874. ps_status == STA_PS_STATUS_WAKE;
  875. if (sta_awake && ps_bit)
  876. priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
  877. else if (!sta_awake && !ps_bit) {
  878. iwl_sta_modify_ps_wake(priv, sta_id);
  879. priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
  880. }
  881. }
  882. }
  883. /* This is necessary only for a number of statistics, see the caller. */
  884. static int iwl_is_network_packet(struct iwl_priv *priv,
  885. struct ieee80211_hdr *header)
  886. {
  887. /* Filter incoming packets to determine if they are targeted toward
  888. * this network, discarding packets coming from ourselves */
  889. switch (priv->iw_mode) {
  890. case NL80211_IFTYPE_ADHOC: /* Header: Dest. | Source | BSSID */
  891. /* packets to our IBSS update information */
  892. return !compare_ether_addr(header->addr3, priv->bssid);
  893. case NL80211_IFTYPE_STATION: /* Header: Dest. | AP{BSSID} | Source */
  894. /* packets to our IBSS update information */
  895. return !compare_ether_addr(header->addr2, priv->bssid);
  896. default:
  897. return 1;
  898. }
  899. }
  900. /* Called for REPLY_RX (legacy ABG frames), or
  901. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  902. void iwl_rx_reply_rx(struct iwl_priv *priv,
  903. struct iwl_rx_mem_buffer *rxb)
  904. {
  905. struct ieee80211_hdr *header;
  906. struct ieee80211_rx_status rx_status;
  907. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  908. /* Use phy data (Rx signal strength, etc.) contained within
  909. * this rx packet for legacy frames,
  910. * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
  911. int include_phy = (pkt->hdr.cmd == REPLY_RX);
  912. struct iwl_rx_phy_res *rx_start = (include_phy) ?
  913. (struct iwl_rx_phy_res *)&(pkt->u.raw[0]) :
  914. (struct iwl_rx_phy_res *)&priv->last_phy_res[1];
  915. __le32 *rx_end;
  916. unsigned int len = 0;
  917. u16 fc;
  918. u8 network_packet;
  919. rx_status.mactime = le64_to_cpu(rx_start->timestamp);
  920. rx_status.freq =
  921. ieee80211_channel_to_frequency(le16_to_cpu(rx_start->channel));
  922. rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  923. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  924. rx_status.rate_idx =
  925. iwl_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
  926. if (rx_status.band == IEEE80211_BAND_5GHZ)
  927. rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
  928. rx_status.flag = 0;
  929. /* TSF isn't reliable. In order to allow smooth user experience,
  930. * this W/A doesn't propagate it to the mac80211 */
  931. /*rx_status.flag |= RX_FLAG_TSFT;*/
  932. if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
  933. IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
  934. rx_start->cfg_phy_cnt);
  935. return;
  936. }
  937. if (!include_phy) {
  938. if (priv->last_phy_res[0])
  939. rx_start = (struct iwl_rx_phy_res *)
  940. &priv->last_phy_res[1];
  941. else
  942. rx_start = NULL;
  943. }
  944. if (!rx_start) {
  945. IWL_ERROR("MPDU frame without a PHY data\n");
  946. return;
  947. }
  948. if (include_phy) {
  949. header = (struct ieee80211_hdr *)((u8 *) &rx_start[1]
  950. + rx_start->cfg_phy_cnt);
  951. len = le16_to_cpu(rx_start->byte_count);
  952. rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
  953. sizeof(struct iwl_rx_phy_res) + len);
  954. } else {
  955. struct iwl4965_rx_mpdu_res_start *amsdu =
  956. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  957. header = (void *)(pkt->u.raw +
  958. sizeof(struct iwl4965_rx_mpdu_res_start));
  959. len = le16_to_cpu(amsdu->byte_count);
  960. rx_end = (__le32 *) (pkt->u.raw +
  961. sizeof(struct iwl4965_rx_mpdu_res_start) + len);
  962. }
  963. if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
  964. !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  965. IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
  966. le32_to_cpu(*rx_end));
  967. return;
  968. }
  969. priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
  970. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  971. rx_status.signal = iwl_calc_rssi(priv, rx_start);
  972. /* Meaningful noise values are available only from beacon statistics,
  973. * which are gathered only when associated, and indicate noise
  974. * only for the associated network channel ...
  975. * Ignore these noise values while scanning (other channels) */
  976. if (iwl_is_associated(priv) &&
  977. !test_bit(STATUS_SCANNING, &priv->status)) {
  978. rx_status.noise = priv->last_rx_noise;
  979. rx_status.qual = iwl_calc_sig_qual(rx_status.signal,
  980. rx_status.noise);
  981. } else {
  982. rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  983. rx_status.qual = iwl_calc_sig_qual(rx_status.signal, 0);
  984. }
  985. /* Reset beacon noise level if not associated. */
  986. if (!iwl_is_associated(priv))
  987. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  988. /* Set "1" to report good data frames in groups of 100 */
  989. /* FIXME: need to optimze the call: */
  990. iwl_dbg_report_frame(priv, pkt, header, 1);
  991. IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
  992. rx_status.signal, rx_status.noise, rx_status.signal,
  993. (unsigned long long)rx_status.mactime);
  994. /*
  995. * "antenna number"
  996. *
  997. * It seems that the antenna field in the phy flags value
  998. * is actually a bitfield. This is undefined by radiotap,
  999. * it wants an actual antenna number but I always get "7"
  1000. * for most legacy frames I receive indicating that the
  1001. * same frame was received on all three RX chains.
  1002. *
  1003. * I think this field should be removed in favour of a
  1004. * new 802.11n radiotap field "RX chains" that is defined
  1005. * as a bitmask.
  1006. */
  1007. rx_status.antenna = le16_to_cpu(rx_start->phy_flags &
  1008. RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
  1009. /* set the preamble flag if appropriate */
  1010. if (rx_start->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  1011. rx_status.flag |= RX_FLAG_SHORTPRE;
  1012. /* Take shortcut when only in monitor mode */
  1013. if (priv->iw_mode == NL80211_IFTYPE_MONITOR) {
  1014. iwl_pass_packet_to_mac80211(priv, include_phy,
  1015. rxb, &rx_status);
  1016. return;
  1017. }
  1018. network_packet = iwl_is_network_packet(priv, header);
  1019. if (network_packet) {
  1020. priv->last_rx_rssi = rx_status.signal;
  1021. priv->last_beacon_time = priv->ucode_beacon_time;
  1022. priv->last_tsf = le64_to_cpu(rx_start->timestamp);
  1023. }
  1024. fc = le16_to_cpu(header->frame_control);
  1025. switch (fc & IEEE80211_FCTL_FTYPE) {
  1026. case IEEE80211_FTYPE_MGMT:
  1027. case IEEE80211_FTYPE_DATA:
  1028. if (priv->iw_mode == NL80211_IFTYPE_AP)
  1029. iwl_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  1030. header->addr2);
  1031. /* fall through */
  1032. default:
  1033. iwl_pass_packet_to_mac80211(priv, include_phy, rxb,
  1034. &rx_status);
  1035. break;
  1036. }
  1037. }
  1038. EXPORT_SYMBOL(iwl_rx_reply_rx);
  1039. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  1040. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  1041. void iwl_rx_reply_rx_phy(struct iwl_priv *priv,
  1042. struct iwl_rx_mem_buffer *rxb)
  1043. {
  1044. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1045. priv->last_phy_res[0] = 1;
  1046. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  1047. sizeof(struct iwl_rx_phy_res));
  1048. }
  1049. EXPORT_SYMBOL(iwl_rx_reply_rx_phy);