iwl-agn-lib.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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[0]);
  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[0]);
  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. #ifdef CONFIG_IWLWIFI_DEBUG
  643. /**
  644. * iwlagn_dbg_report_frame - dump frame to syslog during debug sessions
  645. *
  646. * You may hack this function to show different aspects of received frames,
  647. * including selective frame dumps.
  648. * group100 parameter selects whether to show 1 out of 100 good data frames.
  649. * All beacon and probe response frames are printed.
  650. */
  651. static void iwlagn_dbg_report_frame(struct iwl_priv *priv,
  652. struct iwl_rx_phy_res *phy_res, u16 length,
  653. struct ieee80211_hdr *header, int group100)
  654. {
  655. u32 to_us;
  656. u32 print_summary = 0;
  657. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  658. u32 hundred = 0;
  659. u32 dataframe = 0;
  660. __le16 fc;
  661. u16 seq_ctl;
  662. u16 channel;
  663. u16 phy_flags;
  664. u32 rate_n_flags;
  665. u32 tsf_low;
  666. int rssi;
  667. if (likely(!(iwl_get_debug_level(priv) & IWL_DL_RX)))
  668. return;
  669. /* MAC header */
  670. fc = header->frame_control;
  671. seq_ctl = le16_to_cpu(header->seq_ctrl);
  672. /* metadata */
  673. channel = le16_to_cpu(phy_res->channel);
  674. phy_flags = le16_to_cpu(phy_res->phy_flags);
  675. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  676. /* signal statistics */
  677. rssi = iwlagn_calc_rssi(priv, phy_res);
  678. tsf_low = le64_to_cpu(phy_res->timestamp) & 0x0ffffffff;
  679. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  680. /* if data frame is to us and all is good,
  681. * (optionally) print summary for only 1 out of every 100 */
  682. if (to_us && (fc & ~cpu_to_le16(IEEE80211_FCTL_PROTECTED)) ==
  683. cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  684. dataframe = 1;
  685. if (!group100)
  686. print_summary = 1; /* print each frame */
  687. else if (priv->framecnt_to_us < 100) {
  688. priv->framecnt_to_us++;
  689. print_summary = 0;
  690. } else {
  691. priv->framecnt_to_us = 0;
  692. print_summary = 1;
  693. hundred = 1;
  694. }
  695. } else {
  696. /* print summary for all other frames */
  697. print_summary = 1;
  698. }
  699. if (print_summary) {
  700. char *title;
  701. int rate_idx;
  702. u32 bitrate;
  703. if (hundred)
  704. title = "100Frames";
  705. else if (ieee80211_has_retry(fc))
  706. title = "Retry";
  707. else if (ieee80211_is_assoc_resp(fc))
  708. title = "AscRsp";
  709. else if (ieee80211_is_reassoc_resp(fc))
  710. title = "RasRsp";
  711. else if (ieee80211_is_probe_resp(fc)) {
  712. title = "PrbRsp";
  713. print_dump = 1; /* dump frame contents */
  714. } else if (ieee80211_is_beacon(fc)) {
  715. title = "Beacon";
  716. print_dump = 1; /* dump frame contents */
  717. } else if (ieee80211_is_atim(fc))
  718. title = "ATIM";
  719. else if (ieee80211_is_auth(fc))
  720. title = "Auth";
  721. else if (ieee80211_is_deauth(fc))
  722. title = "DeAuth";
  723. else if (ieee80211_is_disassoc(fc))
  724. title = "DisAssoc";
  725. else
  726. title = "Frame";
  727. rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
  728. if (unlikely((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT))) {
  729. bitrate = 0;
  730. WARN_ON_ONCE(1);
  731. } else {
  732. bitrate = iwl_rates[rate_idx].ieee / 2;
  733. }
  734. /* print frame summary.
  735. * MAC addresses show just the last byte (for brevity),
  736. * but you can hack it to show more, if you'd like to. */
  737. if (dataframe)
  738. IWL_DEBUG_RX(priv, "%s: mhd=0x%04x, dst=0x%02x, "
  739. "len=%u, rssi=%d, chnl=%d, rate=%u,\n",
  740. title, le16_to_cpu(fc), header->addr1[5],
  741. length, rssi, channel, bitrate);
  742. else {
  743. /* src/dst addresses assume managed mode */
  744. IWL_DEBUG_RX(priv, "%s: 0x%04x, dst=0x%02x, src=0x%02x, "
  745. "len=%u, rssi=%d, tim=%lu usec, "
  746. "phy=0x%02x, chnl=%d\n",
  747. title, le16_to_cpu(fc), header->addr1[5],
  748. header->addr3[5], length, rssi,
  749. tsf_low - priv->scan_start_tsf,
  750. phy_flags, channel);
  751. }
  752. }
  753. if (print_dump)
  754. iwl_print_hex_dump(priv, IWL_DL_RX, header, length);
  755. }
  756. #endif
  757. static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  758. {
  759. u32 decrypt_out = 0;
  760. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  761. RX_RES_STATUS_STATION_FOUND)
  762. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  763. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  764. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  765. /* packet was not encrypted */
  766. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  767. RX_RES_STATUS_SEC_TYPE_NONE)
  768. return decrypt_out;
  769. /* packet was encrypted with unknown alg */
  770. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  771. RX_RES_STATUS_SEC_TYPE_ERR)
  772. return decrypt_out;
  773. /* decryption was not done in HW */
  774. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  775. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  776. return decrypt_out;
  777. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  778. case RX_RES_STATUS_SEC_TYPE_CCMP:
  779. /* alg is CCM: check MIC only */
  780. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  781. /* Bad MIC */
  782. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  783. else
  784. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  785. break;
  786. case RX_RES_STATUS_SEC_TYPE_TKIP:
  787. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  788. /* Bad TTAK */
  789. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  790. break;
  791. }
  792. /* fall through if TTAK OK */
  793. default:
  794. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  795. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  796. else
  797. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  798. break;
  799. }
  800. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  801. decrypt_in, decrypt_out);
  802. return decrypt_out;
  803. }
  804. static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
  805. struct ieee80211_hdr *hdr,
  806. u16 len,
  807. u32 ampdu_status,
  808. struct iwl_rx_mem_buffer *rxb,
  809. struct ieee80211_rx_status *stats)
  810. {
  811. struct sk_buff *skb;
  812. __le16 fc = hdr->frame_control;
  813. /* We only process data packets if the interface is open */
  814. if (unlikely(!priv->is_open)) {
  815. IWL_DEBUG_DROP_LIMIT(priv,
  816. "Dropping packet while interface is not open.\n");
  817. return;
  818. }
  819. /* In case of HW accelerated crypto and bad decryption, drop */
  820. if (!priv->cfg->mod_params->sw_crypto &&
  821. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  822. return;
  823. skb = dev_alloc_skb(128);
  824. if (!skb) {
  825. IWL_ERR(priv, "dev_alloc_skb failed\n");
  826. return;
  827. }
  828. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
  829. iwl_update_stats(priv, false, fc, len);
  830. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  831. ieee80211_rx(priv->hw, skb);
  832. priv->alloc_rxb_page--;
  833. rxb->page = NULL;
  834. }
  835. /* Called for REPLY_RX (legacy ABG frames), or
  836. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  837. void iwlagn_rx_reply_rx(struct iwl_priv *priv,
  838. struct iwl_rx_mem_buffer *rxb)
  839. {
  840. struct ieee80211_hdr *header;
  841. struct ieee80211_rx_status rx_status;
  842. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  843. struct iwl_rx_phy_res *phy_res;
  844. __le32 rx_pkt_status;
  845. struct iwl4965_rx_mpdu_res_start *amsdu;
  846. u32 len;
  847. u32 ampdu_status;
  848. u32 rate_n_flags;
  849. /**
  850. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  851. * REPLY_RX: physical layer info is in this buffer
  852. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  853. * command and cached in priv->last_phy_res
  854. *
  855. * Here we set up local variables depending on which command is
  856. * received.
  857. */
  858. if (pkt->hdr.cmd == REPLY_RX) {
  859. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  860. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  861. + phy_res->cfg_phy_cnt);
  862. len = le16_to_cpu(phy_res->byte_count);
  863. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  864. phy_res->cfg_phy_cnt + len);
  865. ampdu_status = le32_to_cpu(rx_pkt_status);
  866. } else {
  867. if (!priv->_agn.last_phy_res_valid) {
  868. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  869. return;
  870. }
  871. phy_res = &priv->_agn.last_phy_res;
  872. amsdu = (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  873. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  874. len = le16_to_cpu(amsdu->byte_count);
  875. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  876. ampdu_status = iwlagn_translate_rx_status(priv,
  877. le32_to_cpu(rx_pkt_status));
  878. }
  879. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  880. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  881. phy_res->cfg_phy_cnt);
  882. return;
  883. }
  884. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  885. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  886. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  887. le32_to_cpu(rx_pkt_status));
  888. return;
  889. }
  890. /* This will be used in several places later */
  891. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  892. /* rx_status carries information about the packet to mac80211 */
  893. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  894. rx_status.freq =
  895. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
  896. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  897. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  898. rx_status.rate_idx =
  899. iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  900. rx_status.flag = 0;
  901. /* TSF isn't reliable. In order to allow smooth user experience,
  902. * this W/A doesn't propagate it to the mac80211 */
  903. /*rx_status.flag |= RX_FLAG_TSFT;*/
  904. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  905. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  906. rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
  907. #ifdef CONFIG_IWLWIFI_DEBUG
  908. /* Set "1" to report good data frames in groups of 100 */
  909. if (unlikely(iwl_get_debug_level(priv) & IWL_DL_RX))
  910. iwlagn_dbg_report_frame(priv, phy_res, len, header, 1);
  911. #endif
  912. iwl_dbg_log_rx_data_frame(priv, len, header);
  913. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
  914. rx_status.signal, (unsigned long long)rx_status.mactime);
  915. /*
  916. * "antenna number"
  917. *
  918. * It seems that the antenna field in the phy flags value
  919. * is actually a bit field. This is undefined by radiotap,
  920. * it wants an actual antenna number but I always get "7"
  921. * for most legacy frames I receive indicating that the
  922. * same frame was received on all three RX chains.
  923. *
  924. * I think this field should be removed in favor of a
  925. * new 802.11n radiotap field "RX chains" that is defined
  926. * as a bitmask.
  927. */
  928. rx_status.antenna =
  929. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  930. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  931. /* set the preamble flag if appropriate */
  932. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  933. rx_status.flag |= RX_FLAG_SHORTPRE;
  934. /* Set up the HT phy flags */
  935. if (rate_n_flags & RATE_MCS_HT_MSK)
  936. rx_status.flag |= RX_FLAG_HT;
  937. if (rate_n_flags & RATE_MCS_HT40_MSK)
  938. rx_status.flag |= RX_FLAG_40MHZ;
  939. if (rate_n_flags & RATE_MCS_SGI_MSK)
  940. rx_status.flag |= RX_FLAG_SHORT_GI;
  941. iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  942. rxb, &rx_status);
  943. }
  944. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  945. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  946. void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
  947. struct iwl_rx_mem_buffer *rxb)
  948. {
  949. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  950. priv->_agn.last_phy_res_valid = true;
  951. memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
  952. sizeof(struct iwl_rx_phy_res));
  953. }
  954. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  955. struct ieee80211_vif *vif,
  956. enum ieee80211_band band,
  957. struct iwl_scan_channel *scan_ch)
  958. {
  959. const struct ieee80211_supported_band *sband;
  960. const struct iwl_channel_info *ch_info;
  961. u16 passive_dwell = 0;
  962. u16 active_dwell = 0;
  963. int i, added = 0;
  964. u16 channel = 0;
  965. sband = iwl_get_hw_mode(priv, band);
  966. if (!sband) {
  967. IWL_ERR(priv, "invalid band\n");
  968. return added;
  969. }
  970. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  971. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  972. if (passive_dwell <= active_dwell)
  973. passive_dwell = active_dwell + 1;
  974. /* only scan single channel, good enough to reset the RF */
  975. /* pick the first valid not in-use channel */
  976. if (band == IEEE80211_BAND_5GHZ) {
  977. for (i = 14; i < priv->channel_count; i++) {
  978. if (priv->channel_info[i].channel !=
  979. le16_to_cpu(priv->staging_rxon.channel)) {
  980. channel = priv->channel_info[i].channel;
  981. ch_info = iwl_get_channel_info(priv,
  982. band, channel);
  983. if (is_channel_valid(ch_info))
  984. break;
  985. }
  986. }
  987. } else {
  988. for (i = 0; i < 14; i++) {
  989. if (priv->channel_info[i].channel !=
  990. le16_to_cpu(priv->staging_rxon.channel)) {
  991. channel =
  992. priv->channel_info[i].channel;
  993. ch_info = iwl_get_channel_info(priv,
  994. band, channel);
  995. if (is_channel_valid(ch_info))
  996. break;
  997. }
  998. }
  999. }
  1000. if (channel) {
  1001. scan_ch->channel = cpu_to_le16(channel);
  1002. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  1003. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  1004. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1005. /* Set txpower levels to defaults */
  1006. scan_ch->dsp_atten = 110;
  1007. if (band == IEEE80211_BAND_5GHZ)
  1008. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1009. else
  1010. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  1011. added++;
  1012. } else
  1013. IWL_ERR(priv, "no valid channel found\n");
  1014. return added;
  1015. }
  1016. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  1017. struct ieee80211_vif *vif,
  1018. enum ieee80211_band band,
  1019. u8 is_active, u8 n_probes,
  1020. struct iwl_scan_channel *scan_ch)
  1021. {
  1022. struct ieee80211_channel *chan;
  1023. const struct ieee80211_supported_band *sband;
  1024. const struct iwl_channel_info *ch_info;
  1025. u16 passive_dwell = 0;
  1026. u16 active_dwell = 0;
  1027. int added, i;
  1028. u16 channel;
  1029. sband = iwl_get_hw_mode(priv, band);
  1030. if (!sband)
  1031. return 0;
  1032. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  1033. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  1034. if (passive_dwell <= active_dwell)
  1035. passive_dwell = active_dwell + 1;
  1036. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  1037. chan = priv->scan_request->channels[i];
  1038. if (chan->band != band)
  1039. continue;
  1040. channel = ieee80211_frequency_to_channel(chan->center_freq);
  1041. scan_ch->channel = cpu_to_le16(channel);
  1042. ch_info = iwl_get_channel_info(priv, band, channel);
  1043. if (!is_channel_valid(ch_info)) {
  1044. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  1045. channel);
  1046. continue;
  1047. }
  1048. if (!is_active || is_channel_passive(ch_info) ||
  1049. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  1050. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  1051. else
  1052. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  1053. if (n_probes)
  1054. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  1055. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  1056. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1057. /* Set txpower levels to defaults */
  1058. scan_ch->dsp_atten = 110;
  1059. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  1060. * power level:
  1061. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  1062. */
  1063. if (band == IEEE80211_BAND_5GHZ)
  1064. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1065. else
  1066. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  1067. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  1068. channel, le32_to_cpu(scan_ch->type),
  1069. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  1070. "ACTIVE" : "PASSIVE",
  1071. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  1072. active_dwell : passive_dwell);
  1073. scan_ch++;
  1074. added++;
  1075. }
  1076. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  1077. return added;
  1078. }
  1079. void iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  1080. {
  1081. struct iwl_host_cmd cmd = {
  1082. .id = REPLY_SCAN_CMD,
  1083. .len = sizeof(struct iwl_scan_cmd),
  1084. .flags = CMD_SIZE_HUGE,
  1085. };
  1086. struct iwl_scan_cmd *scan;
  1087. struct ieee80211_conf *conf = NULL;
  1088. u32 rate_flags = 0;
  1089. u16 cmd_len;
  1090. u16 rx_chain = 0;
  1091. enum ieee80211_band band;
  1092. u8 n_probes = 0;
  1093. u8 rx_ant = priv->hw_params.valid_rx_ant;
  1094. u8 rate;
  1095. bool is_active = false;
  1096. int chan_mod;
  1097. u8 active_chains;
  1098. conf = ieee80211_get_hw_conf(priv->hw);
  1099. cancel_delayed_work(&priv->scan_check);
  1100. if (!iwl_is_ready(priv)) {
  1101. IWL_WARN(priv, "request scan called when driver not ready.\n");
  1102. goto done;
  1103. }
  1104. /* Make sure the scan wasn't canceled before this queued work
  1105. * was given the chance to run... */
  1106. if (!test_bit(STATUS_SCANNING, &priv->status))
  1107. goto done;
  1108. /* This should never be called or scheduled if there is currently
  1109. * a scan active in the hardware. */
  1110. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  1111. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
  1112. "Ignoring second request.\n");
  1113. goto done;
  1114. }
  1115. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  1116. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  1117. goto done;
  1118. }
  1119. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1120. IWL_DEBUG_HC(priv, "Scan request while abort pending. Queuing.\n");
  1121. goto done;
  1122. }
  1123. if (iwl_is_rfkill(priv)) {
  1124. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  1125. goto done;
  1126. }
  1127. if (!test_bit(STATUS_READY, &priv->status)) {
  1128. IWL_DEBUG_HC(priv, "Scan request while uninitialized. Queuing.\n");
  1129. goto done;
  1130. }
  1131. if (!priv->scan_cmd) {
  1132. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  1133. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  1134. if (!priv->scan_cmd) {
  1135. IWL_DEBUG_SCAN(priv,
  1136. "fail to allocate memory for scan\n");
  1137. goto done;
  1138. }
  1139. }
  1140. scan = priv->scan_cmd;
  1141. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  1142. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  1143. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  1144. if (iwl_is_associated(priv)) {
  1145. u16 interval = 0;
  1146. u32 extra;
  1147. u32 suspend_time = 100;
  1148. u32 scan_suspend_time = 100;
  1149. unsigned long flags;
  1150. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  1151. spin_lock_irqsave(&priv->lock, flags);
  1152. interval = vif ? vif->bss_conf.beacon_int : 0;
  1153. spin_unlock_irqrestore(&priv->lock, flags);
  1154. scan->suspend_time = 0;
  1155. scan->max_out_time = cpu_to_le32(200 * 1024);
  1156. if (!interval)
  1157. interval = suspend_time;
  1158. extra = (suspend_time / interval) << 22;
  1159. scan_suspend_time = (extra |
  1160. ((suspend_time % interval) * 1024));
  1161. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  1162. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  1163. scan_suspend_time, interval);
  1164. }
  1165. if (priv->is_internal_short_scan) {
  1166. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  1167. } else if (priv->scan_request->n_ssids) {
  1168. int i, p = 0;
  1169. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  1170. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  1171. /* always does wildcard anyway */
  1172. if (!priv->scan_request->ssids[i].ssid_len)
  1173. continue;
  1174. scan->direct_scan[p].id = WLAN_EID_SSID;
  1175. scan->direct_scan[p].len =
  1176. priv->scan_request->ssids[i].ssid_len;
  1177. memcpy(scan->direct_scan[p].ssid,
  1178. priv->scan_request->ssids[i].ssid,
  1179. priv->scan_request->ssids[i].ssid_len);
  1180. n_probes++;
  1181. p++;
  1182. }
  1183. is_active = true;
  1184. } else
  1185. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  1186. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  1187. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  1188. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1189. switch (priv->scan_band) {
  1190. case IEEE80211_BAND_2GHZ:
  1191. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  1192. chan_mod = le32_to_cpu(priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_MSK)
  1193. >> RXON_FLG_CHANNEL_MODE_POS;
  1194. if (chan_mod == CHANNEL_MODE_PURE_40) {
  1195. rate = IWL_RATE_6M_PLCP;
  1196. } else {
  1197. rate = IWL_RATE_1M_PLCP;
  1198. rate_flags = RATE_MCS_CCK_MSK;
  1199. }
  1200. scan->good_CRC_th = IWL_GOOD_CRC_TH_DISABLED;
  1201. break;
  1202. case IEEE80211_BAND_5GHZ:
  1203. rate = IWL_RATE_6M_PLCP;
  1204. /*
  1205. * If active scanning is requested but a certain channel is
  1206. * marked passive, we can do active scanning if we detect
  1207. * transmissions.
  1208. *
  1209. * There is an issue with some firmware versions that triggers
  1210. * a sysassert on a "good CRC threshold" of zero (== disabled),
  1211. * on a radar channel even though this means that we should NOT
  1212. * send probes.
  1213. *
  1214. * The "good CRC threshold" is the number of frames that we
  1215. * need to receive during our dwell time on a channel before
  1216. * sending out probes -- setting this to a huge value will
  1217. * mean we never reach it, but at the same time work around
  1218. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  1219. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  1220. */
  1221. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  1222. IWL_GOOD_CRC_TH_NEVER;
  1223. break;
  1224. default:
  1225. IWL_WARN(priv, "Invalid scan band count\n");
  1226. goto done;
  1227. }
  1228. band = priv->scan_band;
  1229. if (priv->cfg->scan_antennas[band])
  1230. rx_ant = priv->cfg->scan_antennas[band];
  1231. priv->scan_tx_ant[band] =
  1232. iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]);
  1233. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  1234. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  1235. /* In power save mode use one chain, otherwise use all chains */
  1236. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  1237. /* rx_ant has been set to all valid chains previously */
  1238. active_chains = rx_ant &
  1239. ((u8)(priv->chain_noise_data.active_chains));
  1240. if (!active_chains)
  1241. active_chains = rx_ant;
  1242. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  1243. priv->chain_noise_data.active_chains);
  1244. rx_ant = first_antenna(active_chains);
  1245. }
  1246. /* MIMO is not used here, but value is required */
  1247. rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  1248. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  1249. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  1250. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  1251. scan->rx_chain = cpu_to_le16(rx_chain);
  1252. if (!priv->is_internal_short_scan) {
  1253. cmd_len = iwl_fill_probe_req(priv,
  1254. (struct ieee80211_mgmt *)scan->data,
  1255. priv->scan_request->ie,
  1256. priv->scan_request->ie_len,
  1257. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1258. } else {
  1259. cmd_len = iwl_fill_probe_req(priv,
  1260. (struct ieee80211_mgmt *)scan->data,
  1261. NULL, 0,
  1262. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1263. }
  1264. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  1265. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  1266. RXON_FILTER_BCON_AWARE_MSK);
  1267. if (priv->is_internal_short_scan) {
  1268. scan->channel_count =
  1269. iwl_get_single_channel_for_scan(priv, vif, band,
  1270. (void *)&scan->data[le16_to_cpu(
  1271. scan->tx_cmd.len)]);
  1272. } else {
  1273. scan->channel_count =
  1274. iwl_get_channels_for_scan(priv, vif, band,
  1275. is_active, n_probes,
  1276. (void *)&scan->data[le16_to_cpu(
  1277. scan->tx_cmd.len)]);
  1278. }
  1279. if (scan->channel_count == 0) {
  1280. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  1281. goto done;
  1282. }
  1283. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  1284. scan->channel_count * sizeof(struct iwl_scan_channel);
  1285. cmd.data = scan;
  1286. scan->len = cpu_to_le16(cmd.len);
  1287. set_bit(STATUS_SCAN_HW, &priv->status);
  1288. if (iwl_send_cmd_sync(priv, &cmd))
  1289. goto done;
  1290. queue_delayed_work(priv->workqueue, &priv->scan_check,
  1291. IWL_SCAN_CHECK_WATCHDOG);
  1292. return;
  1293. done:
  1294. /* Cannot perform scan. Make sure we clear scanning
  1295. * bits from status so next scan request can be performed.
  1296. * If we don't clear scanning status bit here all next scan
  1297. * will fail
  1298. */
  1299. clear_bit(STATUS_SCAN_HW, &priv->status);
  1300. clear_bit(STATUS_SCANNING, &priv->status);
  1301. /* inform mac80211 scan aborted */
  1302. queue_work(priv->workqueue, &priv->scan_completed);
  1303. }
  1304. int iwlagn_manage_ibss_station(struct iwl_priv *priv,
  1305. struct ieee80211_vif *vif, bool add)
  1306. {
  1307. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1308. if (add)
  1309. return iwl_add_bssid_station(priv, vif->bss_conf.bssid, true,
  1310. &vif_priv->ibss_bssid_sta_id);
  1311. return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
  1312. vif->bss_conf.bssid);
  1313. }
  1314. void iwl_free_tfds_in_queue(struct iwl_priv *priv,
  1315. int sta_id, int tid, int freed)
  1316. {
  1317. WARN_ON(!spin_is_locked(&priv->sta_lock));
  1318. if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  1319. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  1320. else {
  1321. IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
  1322. priv->stations[sta_id].tid[tid].tfds_in_queue,
  1323. freed);
  1324. priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
  1325. }
  1326. }