iwl-agn-lib.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/etherdevice.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/sched.h>
  34. #include "iwl-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-helpers.h"
  38. #include "iwl-agn-hw.h"
  39. #include "iwl-agn.h"
  40. #include "iwl-sta.h"
  41. static inline u32 iwlagn_get_scd_ssn(struct iwl5000_tx_resp *tx_resp)
  42. {
  43. return le32_to_cpup((__le32 *)&tx_resp->status +
  44. tx_resp->frame_count) & MAX_SN;
  45. }
  46. static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
  47. struct iwl_ht_agg *agg,
  48. struct iwl5000_tx_resp *tx_resp,
  49. int txq_id, u16 start_idx)
  50. {
  51. u16 status;
  52. struct agg_tx_status *frame_status = &tx_resp->status;
  53. struct ieee80211_tx_info *info = NULL;
  54. struct ieee80211_hdr *hdr = NULL;
  55. u32 rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  56. int i, sh, idx;
  57. u16 seq;
  58. if (agg->wait_for_ba)
  59. IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
  60. agg->frame_count = tx_resp->frame_count;
  61. agg->start_idx = start_idx;
  62. agg->rate_n_flags = rate_n_flags;
  63. agg->bitmap = 0;
  64. /* # frames attempted by Tx command */
  65. if (agg->frame_count == 1) {
  66. /* Only one frame was attempted; no block-ack will arrive */
  67. status = le16_to_cpu(frame_status[0].status);
  68. idx = start_idx;
  69. /* FIXME: code repetition */
  70. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
  71. agg->frame_count, agg->start_idx, idx);
  72. info = IEEE80211_SKB_CB(priv->txq[txq_id].txb[idx].skb);
  73. info->status.rates[0].count = tx_resp->failure_frame + 1;
  74. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  75. info->flags |= iwl_tx_status_to_mac80211(status);
  76. iwlagn_hwrate_to_tx_control(priv, rate_n_flags, info);
  77. /* FIXME: code repetition end */
  78. IWL_DEBUG_TX_REPLY(priv, "1 Frame 0x%x failure :%d\n",
  79. status & 0xff, tx_resp->failure_frame);
  80. IWL_DEBUG_TX_REPLY(priv, "Rate Info rate_n_flags=%x\n", rate_n_flags);
  81. agg->wait_for_ba = 0;
  82. } else {
  83. /* Two or more frames were attempted; expect block-ack */
  84. u64 bitmap = 0;
  85. int start = agg->start_idx;
  86. /* Construct bit-map of pending frames within Tx window */
  87. for (i = 0; i < agg->frame_count; i++) {
  88. u16 sc;
  89. status = le16_to_cpu(frame_status[i].status);
  90. seq = le16_to_cpu(frame_status[i].sequence);
  91. idx = SEQ_TO_INDEX(seq);
  92. txq_id = SEQ_TO_QUEUE(seq);
  93. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  94. AGG_TX_STATE_ABORT_MSK))
  95. continue;
  96. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
  97. agg->frame_count, txq_id, idx);
  98. hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
  99. if (!hdr) {
  100. IWL_ERR(priv,
  101. "BUG_ON idx doesn't point to valid skb"
  102. " idx=%d, txq_id=%d\n", idx, txq_id);
  103. return -1;
  104. }
  105. sc = le16_to_cpu(hdr->seq_ctrl);
  106. if (idx != (SEQ_TO_SN(sc) & 0xff)) {
  107. IWL_ERR(priv,
  108. "BUG_ON idx doesn't match seq control"
  109. " idx=%d, seq_idx=%d, seq=%d\n",
  110. idx, SEQ_TO_SN(sc),
  111. hdr->seq_ctrl);
  112. return -1;
  113. }
  114. IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
  115. i, idx, SEQ_TO_SN(sc));
  116. sh = idx - start;
  117. if (sh > 64) {
  118. sh = (start - idx) + 0xff;
  119. bitmap = bitmap << sh;
  120. sh = 0;
  121. start = idx;
  122. } else if (sh < -64)
  123. sh = 0xff - (start - idx);
  124. else if (sh < 0) {
  125. sh = start - idx;
  126. start = idx;
  127. bitmap = bitmap << sh;
  128. sh = 0;
  129. }
  130. bitmap |= 1ULL << sh;
  131. IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
  132. start, (unsigned long long)bitmap);
  133. }
  134. agg->bitmap = bitmap;
  135. agg->start_idx = start;
  136. IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
  137. agg->frame_count, agg->start_idx,
  138. (unsigned long long)agg->bitmap);
  139. if (bitmap)
  140. agg->wait_for_ba = 1;
  141. }
  142. return 0;
  143. }
  144. void iwl_check_abort_status(struct iwl_priv *priv,
  145. u8 frame_count, u32 status)
  146. {
  147. if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
  148. IWL_ERR(priv, "TODO: Implement Tx flush command!!!\n");
  149. }
  150. }
  151. static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
  152. struct iwl_rx_mem_buffer *rxb)
  153. {
  154. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  155. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  156. int txq_id = SEQ_TO_QUEUE(sequence);
  157. int index = SEQ_TO_INDEX(sequence);
  158. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  159. struct ieee80211_tx_info *info;
  160. struct iwl5000_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  161. u32 status = le16_to_cpu(tx_resp->status.status);
  162. int tid;
  163. int sta_id;
  164. int freed;
  165. unsigned long flags;
  166. if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
  167. IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
  168. "is out of range [0-%d] %d %d\n", txq_id,
  169. index, txq->q.n_bd, txq->q.write_ptr,
  170. txq->q.read_ptr);
  171. return;
  172. }
  173. info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb);
  174. memset(&info->status, 0, sizeof(info->status));
  175. tid = (tx_resp->ra_tid & IWL50_TX_RES_TID_MSK) >> IWL50_TX_RES_TID_POS;
  176. sta_id = (tx_resp->ra_tid & IWL50_TX_RES_RA_MSK) >> IWL50_TX_RES_RA_POS;
  177. spin_lock_irqsave(&priv->sta_lock, flags);
  178. if (txq->sched_retry) {
  179. const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
  180. struct iwl_ht_agg *agg;
  181. agg = &priv->stations[sta_id].tid[tid].agg;
  182. iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  183. /* check if BAR is needed */
  184. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
  185. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  186. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  187. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  188. IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
  189. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  190. scd_ssn , index, txq_id, txq->swq_id);
  191. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  192. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  193. if (priv->mac80211_registered &&
  194. (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  195. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
  196. if (agg->state == IWL_AGG_OFF)
  197. iwl_wake_queue(priv, txq_id);
  198. else
  199. iwl_wake_queue(priv, txq->swq_id);
  200. }
  201. }
  202. } else {
  203. BUG_ON(txq_id != txq->swq_id);
  204. info->status.rates[0].count = tx_resp->failure_frame + 1;
  205. info->flags |= iwl_tx_status_to_mac80211(status);
  206. iwlagn_hwrate_to_tx_control(priv,
  207. le32_to_cpu(tx_resp->rate_n_flags),
  208. info);
  209. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
  210. "0x%x retries %d\n",
  211. txq_id,
  212. iwl_get_tx_fail_reason(status), status,
  213. le32_to_cpu(tx_resp->rate_n_flags),
  214. tx_resp->failure_frame);
  215. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  216. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  217. if (priv->mac80211_registered &&
  218. (iwl_queue_space(&txq->q) > txq->q.low_mark))
  219. iwl_wake_queue(priv, txq_id);
  220. }
  221. iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
  222. iwl_check_abort_status(priv, tx_resp->frame_count, status);
  223. spin_unlock_irqrestore(&priv->sta_lock, flags);
  224. }
  225. void iwlagn_rx_handler_setup(struct iwl_priv *priv)
  226. {
  227. /* init calibration handlers */
  228. priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
  229. iwlagn_rx_calib_result;
  230. priv->rx_handlers[CALIBRATION_COMPLETE_NOTIFICATION] =
  231. iwlagn_rx_calib_complete;
  232. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  233. }
  234. void iwlagn_setup_deferred_work(struct iwl_priv *priv)
  235. {
  236. /* in agn, the tx power calibration is done in uCode */
  237. priv->disable_tx_power_cal = 1;
  238. }
  239. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  240. {
  241. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  242. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  243. }
  244. int iwlagn_send_tx_power(struct iwl_priv *priv)
  245. {
  246. struct iwl5000_tx_power_dbm_cmd tx_power_cmd;
  247. u8 tx_ant_cfg_cmd;
  248. /* half dBm need to multiply */
  249. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  250. if (priv->tx_power_lmt_in_half_dbm &&
  251. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  252. /*
  253. * For the newer devices which using enhanced/extend tx power
  254. * table in EEPROM, the format is in half dBm. driver need to
  255. * convert to dBm format before report to mac80211.
  256. * By doing so, there is a possibility of 1/2 dBm resolution
  257. * lost. driver will perform "round-up" operation before
  258. * reporting, but it will cause 1/2 dBm tx power over the
  259. * regulatory limit. Perform the checking here, if the
  260. * "tx_power_user_lmt" is higher than EEPROM value (in
  261. * half-dBm format), lower the tx power based on EEPROM
  262. */
  263. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  264. }
  265. tx_power_cmd.flags = IWL50_TX_POWER_NO_CLOSED;
  266. tx_power_cmd.srv_chan_lmt = IWL50_TX_POWER_AUTO;
  267. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  268. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  269. else
  270. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  271. return iwl_send_cmd_pdu_async(priv, tx_ant_cfg_cmd,
  272. sizeof(tx_power_cmd), &tx_power_cmd,
  273. NULL);
  274. }
  275. void iwlagn_temperature(struct iwl_priv *priv)
  276. {
  277. /* store temperature from statistics (in Celsius) */
  278. priv->temperature = le32_to_cpu(priv->statistics.general.temperature);
  279. iwl_tt_handler(priv);
  280. }
  281. u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
  282. {
  283. struct iwl_eeprom_calib_hdr {
  284. u8 version;
  285. u8 pa_type;
  286. u16 voltage;
  287. } *hdr;
  288. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  289. EEPROM_CALIB_ALL);
  290. return hdr->version;
  291. }
  292. /*
  293. * EEPROM
  294. */
  295. static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
  296. {
  297. u16 offset = 0;
  298. if ((address & INDIRECT_ADDRESS) == 0)
  299. return address;
  300. switch (address & INDIRECT_TYPE_MSK) {
  301. case INDIRECT_HOST:
  302. offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
  303. break;
  304. case INDIRECT_GENERAL:
  305. offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
  306. break;
  307. case INDIRECT_REGULATORY:
  308. offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
  309. break;
  310. case INDIRECT_CALIBRATION:
  311. offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
  312. break;
  313. case INDIRECT_PROCESS_ADJST:
  314. offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
  315. break;
  316. case INDIRECT_OTHERS:
  317. offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
  318. break;
  319. default:
  320. IWL_ERR(priv, "illegal indirect type: 0x%X\n",
  321. address & INDIRECT_TYPE_MSK);
  322. break;
  323. }
  324. /* translate the offset from words to byte */
  325. return (address & ADDRESS_MSK) + (offset << 1);
  326. }
  327. const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
  328. size_t offset)
  329. {
  330. u32 address = eeprom_indirect_address(priv, offset);
  331. BUG_ON(address >= priv->cfg->eeprom_size);
  332. return &priv->eeprom[address];
  333. }
  334. struct iwl_mod_params iwlagn_mod_params = {
  335. .amsdu_size_8K = 1,
  336. .restart_fw = 1,
  337. /* the rest are 0 by default */
  338. };
  339. void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  340. {
  341. unsigned long flags;
  342. int i;
  343. spin_lock_irqsave(&rxq->lock, flags);
  344. INIT_LIST_HEAD(&rxq->rx_free);
  345. INIT_LIST_HEAD(&rxq->rx_used);
  346. /* Fill the rx_used queue with _all_ of the Rx buffers */
  347. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  348. /* In the reset function, these buffers may have been allocated
  349. * to an SKB, so we need to unmap and free potential storage */
  350. if (rxq->pool[i].page != NULL) {
  351. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  352. PAGE_SIZE << priv->hw_params.rx_page_order,
  353. PCI_DMA_FROMDEVICE);
  354. __iwl_free_pages(priv, rxq->pool[i].page);
  355. rxq->pool[i].page = NULL;
  356. }
  357. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  358. }
  359. for (i = 0; i < RX_QUEUE_SIZE; i++)
  360. rxq->queue[i] = NULL;
  361. /* Set us so that we have processed and used all buffers, but have
  362. * not restocked the Rx queue with fresh buffers */
  363. rxq->read = rxq->write = 0;
  364. rxq->write_actual = 0;
  365. rxq->free_count = 0;
  366. spin_unlock_irqrestore(&rxq->lock, flags);
  367. }
  368. int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  369. {
  370. u32 rb_size;
  371. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  372. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  373. if (!priv->cfg->use_isr_legacy)
  374. rb_timeout = RX_RB_TIMEOUT;
  375. if (priv->cfg->mod_params->amsdu_size_8K)
  376. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  377. else
  378. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  379. /* Stop Rx DMA */
  380. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  381. /* Reset driver's Rx queue write index */
  382. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  383. /* Tell device where to find RBD circular buffer in DRAM */
  384. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  385. (u32)(rxq->dma_addr >> 8));
  386. /* Tell device where in DRAM to update its Rx status */
  387. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  388. rxq->rb_stts_dma >> 4);
  389. /* Enable Rx DMA
  390. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  391. * the credit mechanism in 5000 HW RX FIFO
  392. * Direct rx interrupts to hosts
  393. * Rx buffer size 4 or 8k
  394. * RB timeout 0x10
  395. * 256 RBDs
  396. */
  397. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  398. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  399. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  400. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  401. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  402. rb_size|
  403. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  404. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  405. /* Set interrupt coalescing timer to default (2048 usecs) */
  406. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  407. return 0;
  408. }
  409. int iwlagn_hw_nic_init(struct iwl_priv *priv)
  410. {
  411. unsigned long flags;
  412. struct iwl_rx_queue *rxq = &priv->rxq;
  413. int ret;
  414. /* nic_init */
  415. spin_lock_irqsave(&priv->lock, flags);
  416. priv->cfg->ops->lib->apm_ops.init(priv);
  417. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  418. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  419. spin_unlock_irqrestore(&priv->lock, flags);
  420. ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
  421. priv->cfg->ops->lib->apm_ops.config(priv);
  422. /* Allocate the RX queue, or reset if it is already allocated */
  423. if (!rxq->bd) {
  424. ret = iwl_rx_queue_alloc(priv);
  425. if (ret) {
  426. IWL_ERR(priv, "Unable to initialize Rx queue\n");
  427. return -ENOMEM;
  428. }
  429. } else
  430. iwlagn_rx_queue_reset(priv, rxq);
  431. iwlagn_rx_replenish(priv);
  432. iwlagn_rx_init(priv, rxq);
  433. spin_lock_irqsave(&priv->lock, flags);
  434. rxq->need_update = 1;
  435. iwl_rx_queue_update_write_ptr(priv, rxq);
  436. spin_unlock_irqrestore(&priv->lock, flags);
  437. /* Allocate or reset and init all Tx and Command queues */
  438. if (!priv->txq) {
  439. ret = iwlagn_txq_ctx_alloc(priv);
  440. if (ret)
  441. return ret;
  442. } else
  443. iwlagn_txq_ctx_reset(priv);
  444. set_bit(STATUS_INIT, &priv->status);
  445. return 0;
  446. }
  447. /**
  448. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  449. */
  450. static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
  451. dma_addr_t dma_addr)
  452. {
  453. return cpu_to_le32((u32)(dma_addr >> 8));
  454. }
  455. /**
  456. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  457. *
  458. * If there are slots in the RX queue that need to be restocked,
  459. * and we have free pre-allocated buffers, fill the ranks as much
  460. * as we can, pulling from rx_free.
  461. *
  462. * This moves the 'write' index forward to catch up with 'processed', and
  463. * also updates the memory address in the firmware to reference the new
  464. * target buffer.
  465. */
  466. void iwlagn_rx_queue_restock(struct iwl_priv *priv)
  467. {
  468. struct iwl_rx_queue *rxq = &priv->rxq;
  469. struct list_head *element;
  470. struct iwl_rx_mem_buffer *rxb;
  471. unsigned long flags;
  472. spin_lock_irqsave(&rxq->lock, flags);
  473. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  474. /* The overwritten rxb must be a used one */
  475. rxb = rxq->queue[rxq->write];
  476. BUG_ON(rxb && rxb->page);
  477. /* Get next free Rx buffer, remove from free list */
  478. element = rxq->rx_free.next;
  479. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  480. list_del(element);
  481. /* Point to Rx buffer via next RBD in circular buffer */
  482. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
  483. rxb->page_dma);
  484. rxq->queue[rxq->write] = rxb;
  485. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  486. rxq->free_count--;
  487. }
  488. spin_unlock_irqrestore(&rxq->lock, flags);
  489. /* If the pre-allocated buffer pool is dropping low, schedule to
  490. * refill it */
  491. if (rxq->free_count <= RX_LOW_WATERMARK)
  492. queue_work(priv->workqueue, &priv->rx_replenish);
  493. /* If we've added more space for the firmware to place data, tell it.
  494. * Increment device's write pointer in multiples of 8. */
  495. if (rxq->write_actual != (rxq->write & ~0x7)) {
  496. spin_lock_irqsave(&rxq->lock, flags);
  497. rxq->need_update = 1;
  498. spin_unlock_irqrestore(&rxq->lock, flags);
  499. iwl_rx_queue_update_write_ptr(priv, rxq);
  500. }
  501. }
  502. /**
  503. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  504. *
  505. * When moving to rx_free an SKB is allocated for the slot.
  506. *
  507. * Also restock the Rx queue via iwl_rx_queue_restock.
  508. * This is called as a scheduled work item (except for during initialization)
  509. */
  510. void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  511. {
  512. struct iwl_rx_queue *rxq = &priv->rxq;
  513. struct list_head *element;
  514. struct iwl_rx_mem_buffer *rxb;
  515. struct page *page;
  516. unsigned long flags;
  517. gfp_t gfp_mask = priority;
  518. while (1) {
  519. spin_lock_irqsave(&rxq->lock, flags);
  520. if (list_empty(&rxq->rx_used)) {
  521. spin_unlock_irqrestore(&rxq->lock, flags);
  522. return;
  523. }
  524. spin_unlock_irqrestore(&rxq->lock, flags);
  525. if (rxq->free_count > RX_LOW_WATERMARK)
  526. gfp_mask |= __GFP_NOWARN;
  527. if (priv->hw_params.rx_page_order > 0)
  528. gfp_mask |= __GFP_COMP;
  529. /* Alloc a new receive buffer */
  530. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  531. if (!page) {
  532. if (net_ratelimit())
  533. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  534. "order: %d\n",
  535. priv->hw_params.rx_page_order);
  536. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  537. net_ratelimit())
  538. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  539. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  540. rxq->free_count);
  541. /* We don't reschedule replenish work here -- we will
  542. * call the restock method and if it still needs
  543. * more buffers it will schedule replenish */
  544. return;
  545. }
  546. spin_lock_irqsave(&rxq->lock, flags);
  547. if (list_empty(&rxq->rx_used)) {
  548. spin_unlock_irqrestore(&rxq->lock, flags);
  549. __free_pages(page, priv->hw_params.rx_page_order);
  550. return;
  551. }
  552. element = rxq->rx_used.next;
  553. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  554. list_del(element);
  555. spin_unlock_irqrestore(&rxq->lock, flags);
  556. BUG_ON(rxb->page);
  557. rxb->page = page;
  558. /* Get physical address of the RB */
  559. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  560. PAGE_SIZE << priv->hw_params.rx_page_order,
  561. PCI_DMA_FROMDEVICE);
  562. /* dma address must be no more than 36 bits */
  563. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  564. /* and also 256 byte aligned! */
  565. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  566. spin_lock_irqsave(&rxq->lock, flags);
  567. list_add_tail(&rxb->list, &rxq->rx_free);
  568. rxq->free_count++;
  569. priv->alloc_rxb_page++;
  570. spin_unlock_irqrestore(&rxq->lock, flags);
  571. }
  572. }
  573. void iwlagn_rx_replenish(struct iwl_priv *priv)
  574. {
  575. unsigned long flags;
  576. iwlagn_rx_allocate(priv, GFP_KERNEL);
  577. spin_lock_irqsave(&priv->lock, flags);
  578. iwlagn_rx_queue_restock(priv);
  579. spin_unlock_irqrestore(&priv->lock, flags);
  580. }
  581. void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  582. {
  583. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  584. iwlagn_rx_queue_restock(priv);
  585. }
  586. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  587. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  588. * This free routine walks the list of POOL entries and if SKB is set to
  589. * non NULL it is unmapped and freed
  590. */
  591. void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  592. {
  593. int i;
  594. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  595. if (rxq->pool[i].page != NULL) {
  596. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  597. PAGE_SIZE << priv->hw_params.rx_page_order,
  598. PCI_DMA_FROMDEVICE);
  599. __iwl_free_pages(priv, rxq->pool[i].page);
  600. rxq->pool[i].page = NULL;
  601. }
  602. }
  603. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  604. rxq->dma_addr);
  605. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  606. rxq->rb_stts, rxq->rb_stts_dma);
  607. rxq->bd = NULL;
  608. rxq->rb_stts = NULL;
  609. }
  610. int iwlagn_rxq_stop(struct iwl_priv *priv)
  611. {
  612. /* stop Rx DMA */
  613. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  614. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  615. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  616. return 0;
  617. }
  618. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  619. {
  620. int idx = 0;
  621. int band_offset = 0;
  622. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  623. if (rate_n_flags & RATE_MCS_HT_MSK) {
  624. idx = (rate_n_flags & 0xff);
  625. return idx;
  626. /* Legacy rate format, search for match in table */
  627. } else {
  628. if (band == IEEE80211_BAND_5GHZ)
  629. band_offset = IWL_FIRST_OFDM_RATE;
  630. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  631. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  632. return idx - band_offset;
  633. }
  634. return -1;
  635. }
  636. /* Calc max signal level (dBm) among 3 possible receivers */
  637. static inline int iwlagn_calc_rssi(struct iwl_priv *priv,
  638. struct iwl_rx_phy_res *rx_resp)
  639. {
  640. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  641. }
  642. static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  643. {
  644. u32 decrypt_out = 0;
  645. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  646. RX_RES_STATUS_STATION_FOUND)
  647. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  648. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  649. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  650. /* packet was not encrypted */
  651. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  652. RX_RES_STATUS_SEC_TYPE_NONE)
  653. return decrypt_out;
  654. /* packet was encrypted with unknown alg */
  655. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  656. RX_RES_STATUS_SEC_TYPE_ERR)
  657. return decrypt_out;
  658. /* decryption was not done in HW */
  659. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  660. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  661. return decrypt_out;
  662. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  663. case RX_RES_STATUS_SEC_TYPE_CCMP:
  664. /* alg is CCM: check MIC only */
  665. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  666. /* Bad MIC */
  667. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  668. else
  669. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  670. break;
  671. case RX_RES_STATUS_SEC_TYPE_TKIP:
  672. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  673. /* Bad TTAK */
  674. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  675. break;
  676. }
  677. /* fall through if TTAK OK */
  678. default:
  679. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  680. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  681. else
  682. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  683. break;
  684. }
  685. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  686. decrypt_in, decrypt_out);
  687. return decrypt_out;
  688. }
  689. static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
  690. struct ieee80211_hdr *hdr,
  691. u16 len,
  692. u32 ampdu_status,
  693. struct iwl_rx_mem_buffer *rxb,
  694. struct ieee80211_rx_status *stats)
  695. {
  696. struct sk_buff *skb;
  697. __le16 fc = hdr->frame_control;
  698. /* We only process data packets if the interface is open */
  699. if (unlikely(!priv->is_open)) {
  700. IWL_DEBUG_DROP_LIMIT(priv,
  701. "Dropping packet while interface is not open.\n");
  702. return;
  703. }
  704. /* In case of HW accelerated crypto and bad decryption, drop */
  705. if (!priv->cfg->mod_params->sw_crypto &&
  706. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  707. return;
  708. skb = dev_alloc_skb(128);
  709. if (!skb) {
  710. IWL_ERR(priv, "dev_alloc_skb failed\n");
  711. return;
  712. }
  713. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
  714. iwl_update_stats(priv, false, fc, len);
  715. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  716. ieee80211_rx(priv->hw, skb);
  717. priv->alloc_rxb_page--;
  718. rxb->page = NULL;
  719. }
  720. /* Called for REPLY_RX (legacy ABG frames), or
  721. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  722. void iwlagn_rx_reply_rx(struct iwl_priv *priv,
  723. struct iwl_rx_mem_buffer *rxb)
  724. {
  725. struct ieee80211_hdr *header;
  726. struct ieee80211_rx_status rx_status;
  727. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  728. struct iwl_rx_phy_res *phy_res;
  729. __le32 rx_pkt_status;
  730. struct iwl4965_rx_mpdu_res_start *amsdu;
  731. u32 len;
  732. u32 ampdu_status;
  733. u32 rate_n_flags;
  734. /**
  735. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  736. * REPLY_RX: physical layer info is in this buffer
  737. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  738. * command and cached in priv->last_phy_res
  739. *
  740. * Here we set up local variables depending on which command is
  741. * received.
  742. */
  743. if (pkt->hdr.cmd == REPLY_RX) {
  744. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  745. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  746. + phy_res->cfg_phy_cnt);
  747. len = le16_to_cpu(phy_res->byte_count);
  748. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  749. phy_res->cfg_phy_cnt + len);
  750. ampdu_status = le32_to_cpu(rx_pkt_status);
  751. } else {
  752. if (!priv->_agn.last_phy_res_valid) {
  753. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  754. return;
  755. }
  756. phy_res = &priv->_agn.last_phy_res;
  757. amsdu = (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  758. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  759. len = le16_to_cpu(amsdu->byte_count);
  760. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  761. ampdu_status = iwlagn_translate_rx_status(priv,
  762. le32_to_cpu(rx_pkt_status));
  763. }
  764. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  765. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  766. phy_res->cfg_phy_cnt);
  767. return;
  768. }
  769. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  770. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  771. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  772. le32_to_cpu(rx_pkt_status));
  773. return;
  774. }
  775. /* This will be used in several places later */
  776. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  777. /* rx_status carries information about the packet to mac80211 */
  778. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  779. rx_status.freq =
  780. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
  781. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  782. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  783. rx_status.rate_idx =
  784. iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  785. rx_status.flag = 0;
  786. /* TSF isn't reliable. In order to allow smooth user experience,
  787. * this W/A doesn't propagate it to the mac80211 */
  788. /*rx_status.flag |= RX_FLAG_TSFT;*/
  789. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  790. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  791. rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
  792. iwl_dbg_log_rx_data_frame(priv, len, header);
  793. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
  794. rx_status.signal, (unsigned long long)rx_status.mactime);
  795. /*
  796. * "antenna number"
  797. *
  798. * It seems that the antenna field in the phy flags value
  799. * is actually a bit field. This is undefined by radiotap,
  800. * it wants an actual antenna number but I always get "7"
  801. * for most legacy frames I receive indicating that the
  802. * same frame was received on all three RX chains.
  803. *
  804. * I think this field should be removed in favor of a
  805. * new 802.11n radiotap field "RX chains" that is defined
  806. * as a bitmask.
  807. */
  808. rx_status.antenna =
  809. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  810. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  811. /* set the preamble flag if appropriate */
  812. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  813. rx_status.flag |= RX_FLAG_SHORTPRE;
  814. /* Set up the HT phy flags */
  815. if (rate_n_flags & RATE_MCS_HT_MSK)
  816. rx_status.flag |= RX_FLAG_HT;
  817. if (rate_n_flags & RATE_MCS_HT40_MSK)
  818. rx_status.flag |= RX_FLAG_40MHZ;
  819. if (rate_n_flags & RATE_MCS_SGI_MSK)
  820. rx_status.flag |= RX_FLAG_SHORT_GI;
  821. iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  822. rxb, &rx_status);
  823. }
  824. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  825. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  826. void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
  827. struct iwl_rx_mem_buffer *rxb)
  828. {
  829. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  830. priv->_agn.last_phy_res_valid = true;
  831. memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
  832. sizeof(struct iwl_rx_phy_res));
  833. }
  834. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  835. struct ieee80211_vif *vif,
  836. enum ieee80211_band band,
  837. struct iwl_scan_channel *scan_ch)
  838. {
  839. const struct ieee80211_supported_band *sband;
  840. const struct iwl_channel_info *ch_info;
  841. u16 passive_dwell = 0;
  842. u16 active_dwell = 0;
  843. int i, added = 0;
  844. u16 channel = 0;
  845. sband = iwl_get_hw_mode(priv, band);
  846. if (!sband) {
  847. IWL_ERR(priv, "invalid band\n");
  848. return added;
  849. }
  850. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  851. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  852. if (passive_dwell <= active_dwell)
  853. passive_dwell = active_dwell + 1;
  854. /* only scan single channel, good enough to reset the RF */
  855. /* pick the first valid not in-use channel */
  856. if (band == IEEE80211_BAND_5GHZ) {
  857. for (i = 14; i < priv->channel_count; i++) {
  858. if (priv->channel_info[i].channel !=
  859. le16_to_cpu(priv->staging_rxon.channel)) {
  860. channel = priv->channel_info[i].channel;
  861. ch_info = iwl_get_channel_info(priv,
  862. band, channel);
  863. if (is_channel_valid(ch_info))
  864. break;
  865. }
  866. }
  867. } else {
  868. for (i = 0; i < 14; i++) {
  869. if (priv->channel_info[i].channel !=
  870. le16_to_cpu(priv->staging_rxon.channel)) {
  871. channel =
  872. priv->channel_info[i].channel;
  873. ch_info = iwl_get_channel_info(priv,
  874. band, channel);
  875. if (is_channel_valid(ch_info))
  876. break;
  877. }
  878. }
  879. }
  880. if (channel) {
  881. scan_ch->channel = cpu_to_le16(channel);
  882. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  883. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  884. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  885. /* Set txpower levels to defaults */
  886. scan_ch->dsp_atten = 110;
  887. if (band == IEEE80211_BAND_5GHZ)
  888. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  889. else
  890. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  891. added++;
  892. } else
  893. IWL_ERR(priv, "no valid channel found\n");
  894. return added;
  895. }
  896. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  897. struct ieee80211_vif *vif,
  898. enum ieee80211_band band,
  899. u8 is_active, u8 n_probes,
  900. struct iwl_scan_channel *scan_ch)
  901. {
  902. struct ieee80211_channel *chan;
  903. const struct ieee80211_supported_band *sband;
  904. const struct iwl_channel_info *ch_info;
  905. u16 passive_dwell = 0;
  906. u16 active_dwell = 0;
  907. int added, i;
  908. u16 channel;
  909. sband = iwl_get_hw_mode(priv, band);
  910. if (!sband)
  911. return 0;
  912. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  913. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  914. if (passive_dwell <= active_dwell)
  915. passive_dwell = active_dwell + 1;
  916. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  917. chan = priv->scan_request->channels[i];
  918. if (chan->band != band)
  919. continue;
  920. channel = ieee80211_frequency_to_channel(chan->center_freq);
  921. scan_ch->channel = cpu_to_le16(channel);
  922. ch_info = iwl_get_channel_info(priv, band, channel);
  923. if (!is_channel_valid(ch_info)) {
  924. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  925. channel);
  926. continue;
  927. }
  928. if (!is_active || is_channel_passive(ch_info) ||
  929. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  930. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  931. else
  932. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  933. if (n_probes)
  934. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  935. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  936. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  937. /* Set txpower levels to defaults */
  938. scan_ch->dsp_atten = 110;
  939. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  940. * power level:
  941. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  942. */
  943. if (band == IEEE80211_BAND_5GHZ)
  944. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  945. else
  946. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  947. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  948. channel, le32_to_cpu(scan_ch->type),
  949. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  950. "ACTIVE" : "PASSIVE",
  951. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  952. active_dwell : passive_dwell);
  953. scan_ch++;
  954. added++;
  955. }
  956. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  957. return added;
  958. }
  959. void iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  960. {
  961. struct iwl_host_cmd cmd = {
  962. .id = REPLY_SCAN_CMD,
  963. .len = sizeof(struct iwl_scan_cmd),
  964. .flags = CMD_SIZE_HUGE,
  965. };
  966. struct iwl_scan_cmd *scan;
  967. struct ieee80211_conf *conf = NULL;
  968. u32 rate_flags = 0;
  969. u16 cmd_len;
  970. u16 rx_chain = 0;
  971. enum ieee80211_band band;
  972. u8 n_probes = 0;
  973. u8 rx_ant = priv->hw_params.valid_rx_ant;
  974. u8 rate;
  975. bool is_active = false;
  976. int chan_mod;
  977. u8 active_chains;
  978. u8 scan_tx_antennas = priv->hw_params.valid_tx_ant;
  979. conf = ieee80211_get_hw_conf(priv->hw);
  980. cancel_delayed_work(&priv->scan_check);
  981. if (!iwl_is_ready(priv)) {
  982. IWL_WARN(priv, "request scan called when driver not ready.\n");
  983. goto done;
  984. }
  985. /* Make sure the scan wasn't canceled before this queued work
  986. * was given the chance to run... */
  987. if (!test_bit(STATUS_SCANNING, &priv->status))
  988. goto done;
  989. /* This should never be called or scheduled if there is currently
  990. * a scan active in the hardware. */
  991. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  992. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
  993. "Ignoring second request.\n");
  994. goto done;
  995. }
  996. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  997. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  998. goto done;
  999. }
  1000. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1001. IWL_DEBUG_HC(priv, "Scan request while abort pending. Queuing.\n");
  1002. goto done;
  1003. }
  1004. if (iwl_is_rfkill(priv)) {
  1005. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  1006. goto done;
  1007. }
  1008. if (!test_bit(STATUS_READY, &priv->status)) {
  1009. IWL_DEBUG_HC(priv, "Scan request while uninitialized. Queuing.\n");
  1010. goto done;
  1011. }
  1012. if (!priv->scan_cmd) {
  1013. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  1014. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  1015. if (!priv->scan_cmd) {
  1016. IWL_DEBUG_SCAN(priv,
  1017. "fail to allocate memory for scan\n");
  1018. goto done;
  1019. }
  1020. }
  1021. scan = priv->scan_cmd;
  1022. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  1023. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  1024. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  1025. if (iwl_is_associated(priv)) {
  1026. u16 interval = 0;
  1027. u32 extra;
  1028. u32 suspend_time = 100;
  1029. u32 scan_suspend_time = 100;
  1030. unsigned long flags;
  1031. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  1032. spin_lock_irqsave(&priv->lock, flags);
  1033. interval = vif ? vif->bss_conf.beacon_int : 0;
  1034. spin_unlock_irqrestore(&priv->lock, flags);
  1035. scan->suspend_time = 0;
  1036. scan->max_out_time = cpu_to_le32(200 * 1024);
  1037. if (!interval)
  1038. interval = suspend_time;
  1039. extra = (suspend_time / interval) << 22;
  1040. scan_suspend_time = (extra |
  1041. ((suspend_time % interval) * 1024));
  1042. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  1043. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  1044. scan_suspend_time, interval);
  1045. }
  1046. if (priv->is_internal_short_scan) {
  1047. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  1048. } else if (priv->scan_request->n_ssids) {
  1049. int i, p = 0;
  1050. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  1051. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  1052. /* always does wildcard anyway */
  1053. if (!priv->scan_request->ssids[i].ssid_len)
  1054. continue;
  1055. scan->direct_scan[p].id = WLAN_EID_SSID;
  1056. scan->direct_scan[p].len =
  1057. priv->scan_request->ssids[i].ssid_len;
  1058. memcpy(scan->direct_scan[p].ssid,
  1059. priv->scan_request->ssids[i].ssid,
  1060. priv->scan_request->ssids[i].ssid_len);
  1061. n_probes++;
  1062. p++;
  1063. }
  1064. is_active = true;
  1065. } else
  1066. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  1067. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  1068. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  1069. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1070. switch (priv->scan_band) {
  1071. case IEEE80211_BAND_2GHZ:
  1072. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  1073. chan_mod = le32_to_cpu(priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_MSK)
  1074. >> RXON_FLG_CHANNEL_MODE_POS;
  1075. if (chan_mod == CHANNEL_MODE_PURE_40) {
  1076. rate = IWL_RATE_6M_PLCP;
  1077. } else {
  1078. rate = IWL_RATE_1M_PLCP;
  1079. rate_flags = RATE_MCS_CCK_MSK;
  1080. }
  1081. scan->good_CRC_th = IWL_GOOD_CRC_TH_DISABLED;
  1082. break;
  1083. case IEEE80211_BAND_5GHZ:
  1084. rate = IWL_RATE_6M_PLCP;
  1085. /*
  1086. * If active scanning is requested but a certain channel is
  1087. * marked passive, we can do active scanning if we detect
  1088. * transmissions.
  1089. *
  1090. * There is an issue with some firmware versions that triggers
  1091. * a sysassert on a "good CRC threshold" of zero (== disabled),
  1092. * on a radar channel even though this means that we should NOT
  1093. * send probes.
  1094. *
  1095. * The "good CRC threshold" is the number of frames that we
  1096. * need to receive during our dwell time on a channel before
  1097. * sending out probes -- setting this to a huge value will
  1098. * mean we never reach it, but at the same time work around
  1099. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  1100. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  1101. */
  1102. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  1103. IWL_GOOD_CRC_TH_NEVER;
  1104. break;
  1105. default:
  1106. IWL_WARN(priv, "Invalid scan band count\n");
  1107. goto done;
  1108. }
  1109. band = priv->scan_band;
  1110. if (priv->cfg->scan_rx_antennas[band])
  1111. rx_ant = priv->cfg->scan_rx_antennas[band];
  1112. if (priv->cfg->scan_tx_antennas[band])
  1113. scan_tx_antennas = priv->cfg->scan_tx_antennas[band];
  1114. priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
  1115. scan_tx_antennas);
  1116. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  1117. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  1118. /* In power save mode use one chain, otherwise use all chains */
  1119. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  1120. /* rx_ant has been set to all valid chains previously */
  1121. active_chains = rx_ant &
  1122. ((u8)(priv->chain_noise_data.active_chains));
  1123. if (!active_chains)
  1124. active_chains = rx_ant;
  1125. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  1126. priv->chain_noise_data.active_chains);
  1127. rx_ant = first_antenna(active_chains);
  1128. }
  1129. /* MIMO is not used here, but value is required */
  1130. rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  1131. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  1132. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  1133. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  1134. scan->rx_chain = cpu_to_le16(rx_chain);
  1135. if (!priv->is_internal_short_scan) {
  1136. cmd_len = iwl_fill_probe_req(priv,
  1137. (struct ieee80211_mgmt *)scan->data,
  1138. vif->addr,
  1139. priv->scan_request->ie,
  1140. priv->scan_request->ie_len,
  1141. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1142. } else {
  1143. /* use bcast addr, will not be transmitted but must be valid */
  1144. cmd_len = iwl_fill_probe_req(priv,
  1145. (struct ieee80211_mgmt *)scan->data,
  1146. iwl_bcast_addr, NULL, 0,
  1147. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1148. }
  1149. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  1150. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  1151. RXON_FILTER_BCON_AWARE_MSK);
  1152. if (priv->is_internal_short_scan) {
  1153. scan->channel_count =
  1154. iwl_get_single_channel_for_scan(priv, vif, band,
  1155. (void *)&scan->data[le16_to_cpu(
  1156. scan->tx_cmd.len)]);
  1157. } else {
  1158. scan->channel_count =
  1159. iwl_get_channels_for_scan(priv, vif, band,
  1160. is_active, n_probes,
  1161. (void *)&scan->data[le16_to_cpu(
  1162. scan->tx_cmd.len)]);
  1163. }
  1164. if (scan->channel_count == 0) {
  1165. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  1166. goto done;
  1167. }
  1168. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  1169. scan->channel_count * sizeof(struct iwl_scan_channel);
  1170. cmd.data = scan;
  1171. scan->len = cpu_to_le16(cmd.len);
  1172. set_bit(STATUS_SCAN_HW, &priv->status);
  1173. if (iwl_send_cmd_sync(priv, &cmd))
  1174. goto done;
  1175. queue_delayed_work(priv->workqueue, &priv->scan_check,
  1176. IWL_SCAN_CHECK_WATCHDOG);
  1177. return;
  1178. done:
  1179. /* Cannot perform scan. Make sure we clear scanning
  1180. * bits from status so next scan request can be performed.
  1181. * If we don't clear scanning status bit here all next scan
  1182. * will fail
  1183. */
  1184. clear_bit(STATUS_SCAN_HW, &priv->status);
  1185. clear_bit(STATUS_SCANNING, &priv->status);
  1186. /* inform mac80211 scan aborted */
  1187. queue_work(priv->workqueue, &priv->scan_completed);
  1188. }
  1189. int iwlagn_manage_ibss_station(struct iwl_priv *priv,
  1190. struct ieee80211_vif *vif, bool add)
  1191. {
  1192. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1193. if (add)
  1194. return iwl_add_bssid_station(priv, vif->bss_conf.bssid, true,
  1195. &vif_priv->ibss_bssid_sta_id);
  1196. return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
  1197. vif->bss_conf.bssid);
  1198. }
  1199. void iwl_free_tfds_in_queue(struct iwl_priv *priv,
  1200. int sta_id, int tid, int freed)
  1201. {
  1202. WARN_ON(!spin_is_locked(&priv->sta_lock));
  1203. if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  1204. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  1205. else {
  1206. IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
  1207. priv->stations[sta_id].tid[tid].tfds_in_queue,
  1208. freed);
  1209. priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
  1210. }
  1211. }