iwl-agn-lib.c 44 KB

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