iwl-agn-lib.c 42 KB

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