iwl-agn-lib.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  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. /*
  222. * If the BT kill count is non-zero, we'll get this
  223. * notification again.
  224. */
  225. if (tx_resp->bt_kill_count && tx_resp->frame_count == 1 &&
  226. priv->cfg->advanced_bt_coexist) {
  227. IWL_WARN(priv, "receive reply tx with bt_kill\n");
  228. }
  229. iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  230. /* check if BAR is needed */
  231. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
  232. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  233. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  234. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  235. IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
  236. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  237. scd_ssn , index, txq_id, txq->swq_id);
  238. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  239. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  240. if (priv->mac80211_registered &&
  241. (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  242. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
  243. if (agg->state == IWL_AGG_OFF)
  244. iwl_wake_queue(priv, txq_id);
  245. else
  246. iwl_wake_queue(priv, txq->swq_id);
  247. }
  248. }
  249. } else {
  250. BUG_ON(txq_id != txq->swq_id);
  251. info->status.rates[0].count = tx_resp->failure_frame + 1;
  252. info->flags |= iwl_tx_status_to_mac80211(status);
  253. iwlagn_hwrate_to_tx_control(priv,
  254. le32_to_cpu(tx_resp->rate_n_flags),
  255. info);
  256. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
  257. "0x%x retries %d\n",
  258. txq_id,
  259. iwl_get_tx_fail_reason(status), status,
  260. le32_to_cpu(tx_resp->rate_n_flags),
  261. tx_resp->failure_frame);
  262. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  263. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  264. if (priv->mac80211_registered &&
  265. (iwl_queue_space(&txq->q) > txq->q.low_mark))
  266. iwl_wake_queue(priv, txq_id);
  267. }
  268. iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
  269. iwl_check_abort_status(priv, tx_resp->frame_count, status);
  270. spin_unlock_irqrestore(&priv->sta_lock, flags);
  271. }
  272. void iwlagn_rx_handler_setup(struct iwl_priv *priv)
  273. {
  274. /* init calibration handlers */
  275. priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
  276. iwlagn_rx_calib_result;
  277. priv->rx_handlers[CALIBRATION_COMPLETE_NOTIFICATION] =
  278. iwlagn_rx_calib_complete;
  279. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  280. }
  281. void iwlagn_setup_deferred_work(struct iwl_priv *priv)
  282. {
  283. /* in agn, the tx power calibration is done in uCode */
  284. priv->disable_tx_power_cal = 1;
  285. }
  286. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  287. {
  288. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  289. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  290. }
  291. int iwlagn_send_tx_power(struct iwl_priv *priv)
  292. {
  293. struct iwl5000_tx_power_dbm_cmd tx_power_cmd;
  294. u8 tx_ant_cfg_cmd;
  295. /* half dBm need to multiply */
  296. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  297. if (priv->tx_power_lmt_in_half_dbm &&
  298. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  299. /*
  300. * For the newer devices which using enhanced/extend tx power
  301. * table in EEPROM, the format is in half dBm. driver need to
  302. * convert to dBm format before report to mac80211.
  303. * By doing so, there is a possibility of 1/2 dBm resolution
  304. * lost. driver will perform "round-up" operation before
  305. * reporting, but it will cause 1/2 dBm tx power over the
  306. * regulatory limit. Perform the checking here, if the
  307. * "tx_power_user_lmt" is higher than EEPROM value (in
  308. * half-dBm format), lower the tx power based on EEPROM
  309. */
  310. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  311. }
  312. tx_power_cmd.flags = IWL50_TX_POWER_NO_CLOSED;
  313. tx_power_cmd.srv_chan_lmt = IWL50_TX_POWER_AUTO;
  314. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  315. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  316. else
  317. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  318. return iwl_send_cmd_pdu_async(priv, tx_ant_cfg_cmd,
  319. sizeof(tx_power_cmd), &tx_power_cmd,
  320. NULL);
  321. }
  322. void iwlagn_temperature(struct iwl_priv *priv)
  323. {
  324. /* store temperature from statistics (in Celsius) */
  325. priv->temperature =
  326. le32_to_cpu(priv->_agn.statistics.general.common.temperature);
  327. iwl_tt_handler(priv);
  328. }
  329. u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
  330. {
  331. struct iwl_eeprom_calib_hdr {
  332. u8 version;
  333. u8 pa_type;
  334. u16 voltage;
  335. } *hdr;
  336. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  337. EEPROM_CALIB_ALL);
  338. return hdr->version;
  339. }
  340. /*
  341. * EEPROM
  342. */
  343. static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
  344. {
  345. u16 offset = 0;
  346. if ((address & INDIRECT_ADDRESS) == 0)
  347. return address;
  348. switch (address & INDIRECT_TYPE_MSK) {
  349. case INDIRECT_HOST:
  350. offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
  351. break;
  352. case INDIRECT_GENERAL:
  353. offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
  354. break;
  355. case INDIRECT_REGULATORY:
  356. offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
  357. break;
  358. case INDIRECT_CALIBRATION:
  359. offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
  360. break;
  361. case INDIRECT_PROCESS_ADJST:
  362. offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
  363. break;
  364. case INDIRECT_OTHERS:
  365. offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
  366. break;
  367. default:
  368. IWL_ERR(priv, "illegal indirect type: 0x%X\n",
  369. address & INDIRECT_TYPE_MSK);
  370. break;
  371. }
  372. /* translate the offset from words to byte */
  373. return (address & ADDRESS_MSK) + (offset << 1);
  374. }
  375. const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
  376. size_t offset)
  377. {
  378. u32 address = eeprom_indirect_address(priv, offset);
  379. BUG_ON(address >= priv->cfg->eeprom_size);
  380. return &priv->eeprom[address];
  381. }
  382. struct iwl_mod_params iwlagn_mod_params = {
  383. .amsdu_size_8K = 1,
  384. .restart_fw = 1,
  385. /* the rest are 0 by default */
  386. };
  387. void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  388. {
  389. unsigned long flags;
  390. int i;
  391. spin_lock_irqsave(&rxq->lock, flags);
  392. INIT_LIST_HEAD(&rxq->rx_free);
  393. INIT_LIST_HEAD(&rxq->rx_used);
  394. /* Fill the rx_used queue with _all_ of the Rx buffers */
  395. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  396. /* In the reset function, these buffers may have been allocated
  397. * to an SKB, so we need to unmap and free potential storage */
  398. if (rxq->pool[i].page != NULL) {
  399. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  400. PAGE_SIZE << priv->hw_params.rx_page_order,
  401. PCI_DMA_FROMDEVICE);
  402. __iwl_free_pages(priv, rxq->pool[i].page);
  403. rxq->pool[i].page = NULL;
  404. }
  405. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  406. }
  407. for (i = 0; i < RX_QUEUE_SIZE; i++)
  408. rxq->queue[i] = NULL;
  409. /* Set us so that we have processed and used all buffers, but have
  410. * not restocked the Rx queue with fresh buffers */
  411. rxq->read = rxq->write = 0;
  412. rxq->write_actual = 0;
  413. rxq->free_count = 0;
  414. spin_unlock_irqrestore(&rxq->lock, flags);
  415. }
  416. int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  417. {
  418. u32 rb_size;
  419. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  420. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  421. if (!priv->cfg->use_isr_legacy)
  422. rb_timeout = RX_RB_TIMEOUT;
  423. if (priv->cfg->mod_params->amsdu_size_8K)
  424. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  425. else
  426. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  427. /* Stop Rx DMA */
  428. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  429. /* Reset driver's Rx queue write index */
  430. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  431. /* Tell device where to find RBD circular buffer in DRAM */
  432. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  433. (u32)(rxq->bd_dma >> 8));
  434. /* Tell device where in DRAM to update its Rx status */
  435. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  436. rxq->rb_stts_dma >> 4);
  437. /* Enable Rx DMA
  438. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  439. * the credit mechanism in 5000 HW RX FIFO
  440. * Direct rx interrupts to hosts
  441. * Rx buffer size 4 or 8k
  442. * RB timeout 0x10
  443. * 256 RBDs
  444. */
  445. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  446. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  447. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  448. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  449. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  450. rb_size|
  451. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  452. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  453. /* Set interrupt coalescing timer to default (2048 usecs) */
  454. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  455. return 0;
  456. }
  457. int iwlagn_hw_nic_init(struct iwl_priv *priv)
  458. {
  459. unsigned long flags;
  460. struct iwl_rx_queue *rxq = &priv->rxq;
  461. int ret;
  462. /* nic_init */
  463. spin_lock_irqsave(&priv->lock, flags);
  464. priv->cfg->ops->lib->apm_ops.init(priv);
  465. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  466. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  467. spin_unlock_irqrestore(&priv->lock, flags);
  468. ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
  469. priv->cfg->ops->lib->apm_ops.config(priv);
  470. /* Allocate the RX queue, or reset if it is already allocated */
  471. if (!rxq->bd) {
  472. ret = iwl_rx_queue_alloc(priv);
  473. if (ret) {
  474. IWL_ERR(priv, "Unable to initialize Rx queue\n");
  475. return -ENOMEM;
  476. }
  477. } else
  478. iwlagn_rx_queue_reset(priv, rxq);
  479. iwlagn_rx_replenish(priv);
  480. iwlagn_rx_init(priv, rxq);
  481. spin_lock_irqsave(&priv->lock, flags);
  482. rxq->need_update = 1;
  483. iwl_rx_queue_update_write_ptr(priv, rxq);
  484. spin_unlock_irqrestore(&priv->lock, flags);
  485. /* Allocate or reset and init all Tx and Command queues */
  486. if (!priv->txq) {
  487. ret = iwlagn_txq_ctx_alloc(priv);
  488. if (ret)
  489. return ret;
  490. } else
  491. iwlagn_txq_ctx_reset(priv);
  492. set_bit(STATUS_INIT, &priv->status);
  493. return 0;
  494. }
  495. /**
  496. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  497. */
  498. static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
  499. dma_addr_t dma_addr)
  500. {
  501. return cpu_to_le32((u32)(dma_addr >> 8));
  502. }
  503. /**
  504. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  505. *
  506. * If there are slots in the RX queue that need to be restocked,
  507. * and we have free pre-allocated buffers, fill the ranks as much
  508. * as we can, pulling from rx_free.
  509. *
  510. * This moves the 'write' index forward to catch up with 'processed', and
  511. * also updates the memory address in the firmware to reference the new
  512. * target buffer.
  513. */
  514. void iwlagn_rx_queue_restock(struct iwl_priv *priv)
  515. {
  516. struct iwl_rx_queue *rxq = &priv->rxq;
  517. struct list_head *element;
  518. struct iwl_rx_mem_buffer *rxb;
  519. unsigned long flags;
  520. spin_lock_irqsave(&rxq->lock, flags);
  521. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  522. /* The overwritten rxb must be a used one */
  523. rxb = rxq->queue[rxq->write];
  524. BUG_ON(rxb && rxb->page);
  525. /* Get next free Rx buffer, remove from free list */
  526. element = rxq->rx_free.next;
  527. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  528. list_del(element);
  529. /* Point to Rx buffer via next RBD in circular buffer */
  530. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
  531. rxb->page_dma);
  532. rxq->queue[rxq->write] = rxb;
  533. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  534. rxq->free_count--;
  535. }
  536. spin_unlock_irqrestore(&rxq->lock, flags);
  537. /* If the pre-allocated buffer pool is dropping low, schedule to
  538. * refill it */
  539. if (rxq->free_count <= RX_LOW_WATERMARK)
  540. queue_work(priv->workqueue, &priv->rx_replenish);
  541. /* If we've added more space for the firmware to place data, tell it.
  542. * Increment device's write pointer in multiples of 8. */
  543. if (rxq->write_actual != (rxq->write & ~0x7)) {
  544. spin_lock_irqsave(&rxq->lock, flags);
  545. rxq->need_update = 1;
  546. spin_unlock_irqrestore(&rxq->lock, flags);
  547. iwl_rx_queue_update_write_ptr(priv, rxq);
  548. }
  549. }
  550. /**
  551. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  552. *
  553. * When moving to rx_free an SKB is allocated for the slot.
  554. *
  555. * Also restock the Rx queue via iwl_rx_queue_restock.
  556. * This is called as a scheduled work item (except for during initialization)
  557. */
  558. void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  559. {
  560. struct iwl_rx_queue *rxq = &priv->rxq;
  561. struct list_head *element;
  562. struct iwl_rx_mem_buffer *rxb;
  563. struct page *page;
  564. unsigned long flags;
  565. gfp_t gfp_mask = priority;
  566. while (1) {
  567. spin_lock_irqsave(&rxq->lock, flags);
  568. if (list_empty(&rxq->rx_used)) {
  569. spin_unlock_irqrestore(&rxq->lock, flags);
  570. return;
  571. }
  572. spin_unlock_irqrestore(&rxq->lock, flags);
  573. if (rxq->free_count > RX_LOW_WATERMARK)
  574. gfp_mask |= __GFP_NOWARN;
  575. if (priv->hw_params.rx_page_order > 0)
  576. gfp_mask |= __GFP_COMP;
  577. /* Alloc a new receive buffer */
  578. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  579. if (!page) {
  580. if (net_ratelimit())
  581. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  582. "order: %d\n",
  583. priv->hw_params.rx_page_order);
  584. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  585. net_ratelimit())
  586. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  587. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  588. rxq->free_count);
  589. /* We don't reschedule replenish work here -- we will
  590. * call the restock method and if it still needs
  591. * more buffers it will schedule replenish */
  592. return;
  593. }
  594. spin_lock_irqsave(&rxq->lock, flags);
  595. if (list_empty(&rxq->rx_used)) {
  596. spin_unlock_irqrestore(&rxq->lock, flags);
  597. __free_pages(page, priv->hw_params.rx_page_order);
  598. return;
  599. }
  600. element = rxq->rx_used.next;
  601. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  602. list_del(element);
  603. spin_unlock_irqrestore(&rxq->lock, flags);
  604. BUG_ON(rxb->page);
  605. rxb->page = page;
  606. /* Get physical address of the RB */
  607. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  608. PAGE_SIZE << priv->hw_params.rx_page_order,
  609. PCI_DMA_FROMDEVICE);
  610. /* dma address must be no more than 36 bits */
  611. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  612. /* and also 256 byte aligned! */
  613. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  614. spin_lock_irqsave(&rxq->lock, flags);
  615. list_add_tail(&rxb->list, &rxq->rx_free);
  616. rxq->free_count++;
  617. priv->alloc_rxb_page++;
  618. spin_unlock_irqrestore(&rxq->lock, flags);
  619. }
  620. }
  621. void iwlagn_rx_replenish(struct iwl_priv *priv)
  622. {
  623. unsigned long flags;
  624. iwlagn_rx_allocate(priv, GFP_KERNEL);
  625. spin_lock_irqsave(&priv->lock, flags);
  626. iwlagn_rx_queue_restock(priv);
  627. spin_unlock_irqrestore(&priv->lock, flags);
  628. }
  629. void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  630. {
  631. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  632. iwlagn_rx_queue_restock(priv);
  633. }
  634. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  635. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  636. * This free routine walks the list of POOL entries and if SKB is set to
  637. * non NULL it is unmapped and freed
  638. */
  639. void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  640. {
  641. int i;
  642. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  643. if (rxq->pool[i].page != NULL) {
  644. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  645. PAGE_SIZE << priv->hw_params.rx_page_order,
  646. PCI_DMA_FROMDEVICE);
  647. __iwl_free_pages(priv, rxq->pool[i].page);
  648. rxq->pool[i].page = NULL;
  649. }
  650. }
  651. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  652. rxq->bd_dma);
  653. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  654. rxq->rb_stts, rxq->rb_stts_dma);
  655. rxq->bd = NULL;
  656. rxq->rb_stts = NULL;
  657. }
  658. int iwlagn_rxq_stop(struct iwl_priv *priv)
  659. {
  660. /* stop Rx DMA */
  661. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  662. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  663. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  664. return 0;
  665. }
  666. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  667. {
  668. int idx = 0;
  669. int band_offset = 0;
  670. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  671. if (rate_n_flags & RATE_MCS_HT_MSK) {
  672. idx = (rate_n_flags & 0xff);
  673. return idx;
  674. /* Legacy rate format, search for match in table */
  675. } else {
  676. if (band == IEEE80211_BAND_5GHZ)
  677. band_offset = IWL_FIRST_OFDM_RATE;
  678. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  679. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  680. return idx - band_offset;
  681. }
  682. return -1;
  683. }
  684. /* Calc max signal level (dBm) among 3 possible receivers */
  685. static inline int iwlagn_calc_rssi(struct iwl_priv *priv,
  686. struct iwl_rx_phy_res *rx_resp)
  687. {
  688. return priv->cfg->ops->utils->calc_rssi(priv, rx_resp);
  689. }
  690. static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  691. {
  692. u32 decrypt_out = 0;
  693. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  694. RX_RES_STATUS_STATION_FOUND)
  695. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  696. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  697. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  698. /* packet was not encrypted */
  699. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  700. RX_RES_STATUS_SEC_TYPE_NONE)
  701. return decrypt_out;
  702. /* packet was encrypted with unknown alg */
  703. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  704. RX_RES_STATUS_SEC_TYPE_ERR)
  705. return decrypt_out;
  706. /* decryption was not done in HW */
  707. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  708. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  709. return decrypt_out;
  710. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  711. case RX_RES_STATUS_SEC_TYPE_CCMP:
  712. /* alg is CCM: check MIC only */
  713. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  714. /* Bad MIC */
  715. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  716. else
  717. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  718. break;
  719. case RX_RES_STATUS_SEC_TYPE_TKIP:
  720. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  721. /* Bad TTAK */
  722. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  723. break;
  724. }
  725. /* fall through if TTAK OK */
  726. default:
  727. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  728. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  729. else
  730. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  731. break;
  732. }
  733. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  734. decrypt_in, decrypt_out);
  735. return decrypt_out;
  736. }
  737. static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
  738. struct ieee80211_hdr *hdr,
  739. u16 len,
  740. u32 ampdu_status,
  741. struct iwl_rx_mem_buffer *rxb,
  742. struct ieee80211_rx_status *stats)
  743. {
  744. struct sk_buff *skb;
  745. __le16 fc = hdr->frame_control;
  746. /* We only process data packets if the interface is open */
  747. if (unlikely(!priv->is_open)) {
  748. IWL_DEBUG_DROP_LIMIT(priv,
  749. "Dropping packet while interface is not open.\n");
  750. return;
  751. }
  752. /* In case of HW accelerated crypto and bad decryption, drop */
  753. if (!priv->cfg->mod_params->sw_crypto &&
  754. iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  755. return;
  756. skb = dev_alloc_skb(128);
  757. if (!skb) {
  758. IWL_ERR(priv, "dev_alloc_skb failed\n");
  759. return;
  760. }
  761. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
  762. iwl_update_stats(priv, false, fc, len);
  763. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  764. ieee80211_rx(priv->hw, skb);
  765. priv->alloc_rxb_page--;
  766. rxb->page = NULL;
  767. }
  768. /* Called for REPLY_RX (legacy ABG frames), or
  769. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  770. void iwlagn_rx_reply_rx(struct iwl_priv *priv,
  771. struct iwl_rx_mem_buffer *rxb)
  772. {
  773. struct ieee80211_hdr *header;
  774. struct ieee80211_rx_status rx_status;
  775. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  776. struct iwl_rx_phy_res *phy_res;
  777. __le32 rx_pkt_status;
  778. struct iwl_rx_mpdu_res_start *amsdu;
  779. u32 len;
  780. u32 ampdu_status;
  781. u32 rate_n_flags;
  782. /**
  783. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  784. * REPLY_RX: physical layer info is in this buffer
  785. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  786. * command and cached in priv->last_phy_res
  787. *
  788. * Here we set up local variables depending on which command is
  789. * received.
  790. */
  791. if (pkt->hdr.cmd == REPLY_RX) {
  792. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  793. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  794. + phy_res->cfg_phy_cnt);
  795. len = le16_to_cpu(phy_res->byte_count);
  796. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  797. phy_res->cfg_phy_cnt + len);
  798. ampdu_status = le32_to_cpu(rx_pkt_status);
  799. } else {
  800. if (!priv->_agn.last_phy_res_valid) {
  801. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  802. return;
  803. }
  804. phy_res = &priv->_agn.last_phy_res;
  805. amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
  806. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  807. len = le16_to_cpu(amsdu->byte_count);
  808. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  809. ampdu_status = iwlagn_translate_rx_status(priv,
  810. le32_to_cpu(rx_pkt_status));
  811. }
  812. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  813. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  814. phy_res->cfg_phy_cnt);
  815. return;
  816. }
  817. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  818. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  819. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  820. le32_to_cpu(rx_pkt_status));
  821. return;
  822. }
  823. /* This will be used in several places later */
  824. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  825. /* rx_status carries information about the packet to mac80211 */
  826. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  827. rx_status.freq =
  828. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel));
  829. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  830. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  831. rx_status.rate_idx =
  832. iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  833. rx_status.flag = 0;
  834. /* TSF isn't reliable. In order to allow smooth user experience,
  835. * this W/A doesn't propagate it to the mac80211 */
  836. /*rx_status.flag |= RX_FLAG_TSFT;*/
  837. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  838. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  839. rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
  840. iwl_dbg_log_rx_data_frame(priv, len, header);
  841. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
  842. rx_status.signal, (unsigned long long)rx_status.mactime);
  843. /*
  844. * "antenna number"
  845. *
  846. * It seems that the antenna field in the phy flags value
  847. * is actually a bit field. This is undefined by radiotap,
  848. * it wants an actual antenna number but I always get "7"
  849. * for most legacy frames I receive indicating that the
  850. * same frame was received on all three RX chains.
  851. *
  852. * I think this field should be removed in favor of a
  853. * new 802.11n radiotap field "RX chains" that is defined
  854. * as a bitmask.
  855. */
  856. rx_status.antenna =
  857. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  858. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  859. /* set the preamble flag if appropriate */
  860. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  861. rx_status.flag |= RX_FLAG_SHORTPRE;
  862. /* Set up the HT phy flags */
  863. if (rate_n_flags & RATE_MCS_HT_MSK)
  864. rx_status.flag |= RX_FLAG_HT;
  865. if (rate_n_flags & RATE_MCS_HT40_MSK)
  866. rx_status.flag |= RX_FLAG_40MHZ;
  867. if (rate_n_flags & RATE_MCS_SGI_MSK)
  868. rx_status.flag |= RX_FLAG_SHORT_GI;
  869. iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  870. rxb, &rx_status);
  871. }
  872. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  873. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  874. void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
  875. struct iwl_rx_mem_buffer *rxb)
  876. {
  877. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  878. priv->_agn.last_phy_res_valid = true;
  879. memcpy(&priv->_agn.last_phy_res, pkt->u.raw,
  880. sizeof(struct iwl_rx_phy_res));
  881. }
  882. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  883. struct ieee80211_vif *vif,
  884. enum ieee80211_band band,
  885. struct iwl_scan_channel *scan_ch)
  886. {
  887. const struct ieee80211_supported_band *sband;
  888. u16 passive_dwell = 0;
  889. u16 active_dwell = 0;
  890. int added = 0;
  891. u16 channel = 0;
  892. sband = iwl_get_hw_mode(priv, band);
  893. if (!sband) {
  894. IWL_ERR(priv, "invalid band\n");
  895. return added;
  896. }
  897. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  898. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  899. if (passive_dwell <= active_dwell)
  900. passive_dwell = active_dwell + 1;
  901. channel = iwl_get_single_channel_number(priv, band);
  902. if (channel) {
  903. scan_ch->channel = cpu_to_le16(channel);
  904. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  905. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  906. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  907. /* Set txpower levels to defaults */
  908. scan_ch->dsp_atten = 110;
  909. if (band == IEEE80211_BAND_5GHZ)
  910. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  911. else
  912. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  913. added++;
  914. } else
  915. IWL_ERR(priv, "no valid channel found\n");
  916. return added;
  917. }
  918. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  919. struct ieee80211_vif *vif,
  920. enum ieee80211_band band,
  921. u8 is_active, u8 n_probes,
  922. struct iwl_scan_channel *scan_ch)
  923. {
  924. struct ieee80211_channel *chan;
  925. const struct ieee80211_supported_band *sband;
  926. const struct iwl_channel_info *ch_info;
  927. u16 passive_dwell = 0;
  928. u16 active_dwell = 0;
  929. int added, i;
  930. u16 channel;
  931. sband = iwl_get_hw_mode(priv, band);
  932. if (!sband)
  933. return 0;
  934. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  935. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  936. if (passive_dwell <= active_dwell)
  937. passive_dwell = active_dwell + 1;
  938. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  939. chan = priv->scan_request->channels[i];
  940. if (chan->band != band)
  941. continue;
  942. channel = chan->hw_value;
  943. scan_ch->channel = cpu_to_le16(channel);
  944. ch_info = iwl_get_channel_info(priv, band, channel);
  945. if (!is_channel_valid(ch_info)) {
  946. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  947. channel);
  948. continue;
  949. }
  950. if (!is_active || is_channel_passive(ch_info) ||
  951. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  952. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  953. else
  954. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  955. if (n_probes)
  956. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  957. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  958. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  959. /* Set txpower levels to defaults */
  960. scan_ch->dsp_atten = 110;
  961. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  962. * power level:
  963. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  964. */
  965. if (band == IEEE80211_BAND_5GHZ)
  966. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  967. else
  968. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  969. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  970. channel, le32_to_cpu(scan_ch->type),
  971. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  972. "ACTIVE" : "PASSIVE",
  973. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  974. active_dwell : passive_dwell);
  975. scan_ch++;
  976. added++;
  977. }
  978. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  979. return added;
  980. }
  981. void iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  982. {
  983. struct iwl_host_cmd cmd = {
  984. .id = REPLY_SCAN_CMD,
  985. .len = sizeof(struct iwl_scan_cmd),
  986. .flags = CMD_SIZE_HUGE,
  987. };
  988. struct iwl_scan_cmd *scan;
  989. struct ieee80211_conf *conf = NULL;
  990. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  991. u32 rate_flags = 0;
  992. u16 cmd_len;
  993. u16 rx_chain = 0;
  994. enum ieee80211_band band;
  995. u8 n_probes = 0;
  996. u8 rx_ant = priv->hw_params.valid_rx_ant;
  997. u8 rate;
  998. bool is_active = false;
  999. int chan_mod;
  1000. u8 active_chains;
  1001. u8 scan_tx_antennas = priv->hw_params.valid_tx_ant;
  1002. if (vif)
  1003. ctx = iwl_rxon_ctx_from_vif(vif);
  1004. conf = ieee80211_get_hw_conf(priv->hw);
  1005. cancel_delayed_work(&priv->scan_check);
  1006. if (!iwl_is_ready(priv)) {
  1007. IWL_WARN(priv, "request scan called when driver not ready.\n");
  1008. goto done;
  1009. }
  1010. /* Make sure the scan wasn't canceled before this queued work
  1011. * was given the chance to run... */
  1012. if (!test_bit(STATUS_SCANNING, &priv->status))
  1013. goto done;
  1014. /* This should never be called or scheduled if there is currently
  1015. * a scan active in the hardware. */
  1016. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  1017. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
  1018. "Ignoring second request.\n");
  1019. goto done;
  1020. }
  1021. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  1022. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  1023. goto done;
  1024. }
  1025. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1026. IWL_DEBUG_HC(priv, "Scan request while abort pending. Queuing.\n");
  1027. goto done;
  1028. }
  1029. if (iwl_is_rfkill(priv)) {
  1030. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  1031. goto done;
  1032. }
  1033. if (!test_bit(STATUS_READY, &priv->status)) {
  1034. IWL_DEBUG_HC(priv, "Scan request while uninitialized. Queuing.\n");
  1035. goto done;
  1036. }
  1037. if (!priv->scan_cmd) {
  1038. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  1039. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  1040. if (!priv->scan_cmd) {
  1041. IWL_DEBUG_SCAN(priv,
  1042. "fail to allocate memory for scan\n");
  1043. goto done;
  1044. }
  1045. }
  1046. scan = priv->scan_cmd;
  1047. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  1048. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  1049. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  1050. if (iwl_is_any_associated(priv)) {
  1051. u16 interval = 0;
  1052. u32 extra;
  1053. u32 suspend_time = 100;
  1054. u32 scan_suspend_time = 100;
  1055. unsigned long flags;
  1056. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  1057. spin_lock_irqsave(&priv->lock, flags);
  1058. if (priv->is_internal_short_scan)
  1059. interval = 0;
  1060. else
  1061. interval = vif->bss_conf.beacon_int;
  1062. spin_unlock_irqrestore(&priv->lock, flags);
  1063. scan->suspend_time = 0;
  1064. scan->max_out_time = cpu_to_le32(200 * 1024);
  1065. if (!interval)
  1066. interval = suspend_time;
  1067. extra = (suspend_time / interval) << 22;
  1068. scan_suspend_time = (extra |
  1069. ((suspend_time % interval) * 1024));
  1070. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  1071. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  1072. scan_suspend_time, interval);
  1073. }
  1074. if (priv->is_internal_short_scan) {
  1075. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  1076. } else if (priv->scan_request->n_ssids) {
  1077. int i, p = 0;
  1078. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  1079. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  1080. /* always does wildcard anyway */
  1081. if (!priv->scan_request->ssids[i].ssid_len)
  1082. continue;
  1083. scan->direct_scan[p].id = WLAN_EID_SSID;
  1084. scan->direct_scan[p].len =
  1085. priv->scan_request->ssids[i].ssid_len;
  1086. memcpy(scan->direct_scan[p].ssid,
  1087. priv->scan_request->ssids[i].ssid,
  1088. priv->scan_request->ssids[i].ssid_len);
  1089. n_probes++;
  1090. p++;
  1091. }
  1092. is_active = true;
  1093. } else
  1094. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  1095. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  1096. scan->tx_cmd.sta_id = ctx->bcast_sta_id;
  1097. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1098. switch (priv->scan_band) {
  1099. case IEEE80211_BAND_2GHZ:
  1100. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  1101. chan_mod = le32_to_cpu(
  1102. priv->contexts[IWL_RXON_CTX_BSS].active.flags &
  1103. RXON_FLG_CHANNEL_MODE_MSK)
  1104. >> RXON_FLG_CHANNEL_MODE_POS;
  1105. if (chan_mod == CHANNEL_MODE_PURE_40) {
  1106. rate = IWL_RATE_6M_PLCP;
  1107. } else {
  1108. rate = IWL_RATE_1M_PLCP;
  1109. rate_flags = RATE_MCS_CCK_MSK;
  1110. }
  1111. /*
  1112. * Internal scans are passive, so we can indiscriminately set
  1113. * the BT ignore flag on 2.4 GHz since it applies to TX only.
  1114. */
  1115. if (priv->cfg->advanced_bt_coexist)
  1116. scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
  1117. scan->good_CRC_th = IWL_GOOD_CRC_TH_DISABLED;
  1118. break;
  1119. case IEEE80211_BAND_5GHZ:
  1120. rate = IWL_RATE_6M_PLCP;
  1121. /*
  1122. * If active scanning is requested but a certain channel is
  1123. * marked passive, we can do active scanning if we detect
  1124. * transmissions.
  1125. *
  1126. * There is an issue with some firmware versions that triggers
  1127. * a sysassert on a "good CRC threshold" of zero (== disabled),
  1128. * on a radar channel even though this means that we should NOT
  1129. * send probes.
  1130. *
  1131. * The "good CRC threshold" is the number of frames that we
  1132. * need to receive during our dwell time on a channel before
  1133. * sending out probes -- setting this to a huge value will
  1134. * mean we never reach it, but at the same time work around
  1135. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  1136. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  1137. */
  1138. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  1139. IWL_GOOD_CRC_TH_NEVER;
  1140. break;
  1141. default:
  1142. IWL_WARN(priv, "Invalid scan band count\n");
  1143. goto done;
  1144. }
  1145. band = priv->scan_band;
  1146. if (priv->cfg->scan_rx_antennas[band])
  1147. rx_ant = priv->cfg->scan_rx_antennas[band];
  1148. if (priv->cfg->scan_tx_antennas[band])
  1149. scan_tx_antennas = priv->cfg->scan_tx_antennas[band];
  1150. if (priv->cfg->advanced_bt_coexist && priv->bt_full_concurrent) {
  1151. /* operated as 1x1 in full concurrency mode */
  1152. scan_tx_antennas =
  1153. first_antenna(priv->cfg->scan_tx_antennas[band]);
  1154. }
  1155. priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
  1156. scan_tx_antennas);
  1157. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  1158. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  1159. /* In power save mode use one chain, otherwise use all chains */
  1160. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  1161. /* rx_ant has been set to all valid chains previously */
  1162. active_chains = rx_ant &
  1163. ((u8)(priv->chain_noise_data.active_chains));
  1164. if (!active_chains)
  1165. active_chains = rx_ant;
  1166. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  1167. priv->chain_noise_data.active_chains);
  1168. rx_ant = first_antenna(active_chains);
  1169. }
  1170. if (priv->cfg->advanced_bt_coexist && priv->bt_full_concurrent) {
  1171. /* operated as 1x1 in full concurrency mode */
  1172. rx_ant = first_antenna(rx_ant);
  1173. }
  1174. /* MIMO is not used here, but value is required */
  1175. rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  1176. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  1177. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  1178. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  1179. scan->rx_chain = cpu_to_le16(rx_chain);
  1180. if (!priv->is_internal_short_scan) {
  1181. cmd_len = iwl_fill_probe_req(priv,
  1182. (struct ieee80211_mgmt *)scan->data,
  1183. vif->addr,
  1184. priv->scan_request->ie,
  1185. priv->scan_request->ie_len,
  1186. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1187. } else {
  1188. /* use bcast addr, will not be transmitted but must be valid */
  1189. cmd_len = iwl_fill_probe_req(priv,
  1190. (struct ieee80211_mgmt *)scan->data,
  1191. iwl_bcast_addr, NULL, 0,
  1192. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1193. }
  1194. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  1195. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  1196. RXON_FILTER_BCON_AWARE_MSK);
  1197. if (priv->is_internal_short_scan) {
  1198. scan->channel_count =
  1199. iwl_get_single_channel_for_scan(priv, vif, band,
  1200. (void *)&scan->data[le16_to_cpu(
  1201. scan->tx_cmd.len)]);
  1202. } else {
  1203. scan->channel_count =
  1204. iwl_get_channels_for_scan(priv, vif, band,
  1205. is_active, n_probes,
  1206. (void *)&scan->data[le16_to_cpu(
  1207. scan->tx_cmd.len)]);
  1208. }
  1209. if (scan->channel_count == 0) {
  1210. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  1211. goto done;
  1212. }
  1213. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  1214. scan->channel_count * sizeof(struct iwl_scan_channel);
  1215. cmd.data = scan;
  1216. scan->len = cpu_to_le16(cmd.len);
  1217. set_bit(STATUS_SCAN_HW, &priv->status);
  1218. if (iwl_send_cmd_sync(priv, &cmd))
  1219. goto done;
  1220. queue_delayed_work(priv->workqueue, &priv->scan_check,
  1221. IWL_SCAN_CHECK_WATCHDOG);
  1222. return;
  1223. done:
  1224. /* Cannot perform scan. Make sure we clear scanning
  1225. * bits from status so next scan request can be performed.
  1226. * If we don't clear scanning status bit here all next scan
  1227. * will fail
  1228. */
  1229. clear_bit(STATUS_SCAN_HW, &priv->status);
  1230. clear_bit(STATUS_SCANNING, &priv->status);
  1231. /* inform mac80211 scan aborted */
  1232. queue_work(priv->workqueue, &priv->scan_completed);
  1233. }
  1234. int iwlagn_manage_ibss_station(struct iwl_priv *priv,
  1235. struct ieee80211_vif *vif, bool add)
  1236. {
  1237. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1238. if (add)
  1239. return iwl_add_bssid_station(priv, vif_priv->ctx,
  1240. vif->bss_conf.bssid, true,
  1241. &vif_priv->ibss_bssid_sta_id);
  1242. return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
  1243. vif->bss_conf.bssid);
  1244. }
  1245. void iwl_free_tfds_in_queue(struct iwl_priv *priv,
  1246. int sta_id, int tid, int freed)
  1247. {
  1248. lockdep_assert_held(&priv->sta_lock);
  1249. if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  1250. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  1251. else {
  1252. IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
  1253. priv->stations[sta_id].tid[tid].tfds_in_queue,
  1254. freed);
  1255. priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
  1256. }
  1257. }
  1258. #define IWL_FLUSH_WAIT_MS 2000
  1259. int iwlagn_wait_tx_queue_empty(struct iwl_priv *priv)
  1260. {
  1261. struct iwl_tx_queue *txq;
  1262. struct iwl_queue *q;
  1263. int cnt;
  1264. unsigned long now = jiffies;
  1265. int ret = 0;
  1266. /* waiting for all the tx frames complete might take a while */
  1267. for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
  1268. if (cnt == priv->cmd_queue)
  1269. continue;
  1270. txq = &priv->txq[cnt];
  1271. q = &txq->q;
  1272. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1273. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1274. msleep(1);
  1275. if (q->read_ptr != q->write_ptr) {
  1276. IWL_ERR(priv, "fail to flush all tx fifo queues\n");
  1277. ret = -ETIMEDOUT;
  1278. break;
  1279. }
  1280. }
  1281. return ret;
  1282. }
  1283. #define IWL_TX_QUEUE_MSK 0xfffff
  1284. /**
  1285. * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
  1286. *
  1287. * pre-requirements:
  1288. * 1. acquire mutex before calling
  1289. * 2. make sure rf is on and not in exit state
  1290. */
  1291. int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  1292. {
  1293. struct iwl_txfifo_flush_cmd flush_cmd;
  1294. struct iwl_host_cmd cmd = {
  1295. .id = REPLY_TXFIFO_FLUSH,
  1296. .len = sizeof(struct iwl_txfifo_flush_cmd),
  1297. .flags = CMD_SYNC,
  1298. .data = &flush_cmd,
  1299. };
  1300. might_sleep();
  1301. memset(&flush_cmd, 0, sizeof(flush_cmd));
  1302. flush_cmd.fifo_control = IWL_TX_FIFO_VO_MSK | IWL_TX_FIFO_VI_MSK |
  1303. IWL_TX_FIFO_BE_MSK | IWL_TX_FIFO_BK_MSK;
  1304. if (priv->cfg->sku & IWL_SKU_N)
  1305. flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
  1306. IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
  1307. flush_cmd.fifo_control);
  1308. flush_cmd.flush_control = cpu_to_le16(flush_control);
  1309. return iwl_send_cmd(priv, &cmd);
  1310. }
  1311. void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  1312. {
  1313. mutex_lock(&priv->mutex);
  1314. ieee80211_stop_queues(priv->hw);
  1315. if (priv->cfg->ops->lib->txfifo_flush(priv, IWL_DROP_ALL)) {
  1316. IWL_ERR(priv, "flush request fail\n");
  1317. goto done;
  1318. }
  1319. IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
  1320. iwlagn_wait_tx_queue_empty(priv);
  1321. done:
  1322. ieee80211_wake_queues(priv->hw);
  1323. mutex_unlock(&priv->mutex);
  1324. }
  1325. /*
  1326. * BT coex
  1327. */
  1328. /*
  1329. * Macros to access the lookup table.
  1330. *
  1331. * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
  1332. * wifi_prio, wifi_txrx and wifi_sh_ant_req.
  1333. *
  1334. * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
  1335. *
  1336. * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
  1337. * one after another in 32-bit registers, and "registers" 0 through 7 contain
  1338. * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
  1339. *
  1340. * These macros encode that format.
  1341. */
  1342. #define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
  1343. wifi_txrx, wifi_sh_ant_req) \
  1344. (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
  1345. (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
  1346. #define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
  1347. lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
  1348. #define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1349. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1350. (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
  1351. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1352. wifi_sh_ant_req))))
  1353. #define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1354. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1355. LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
  1356. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1357. wifi_sh_ant_req))
  1358. #define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
  1359. wifi_req, wifi_prio, wifi_txrx, \
  1360. wifi_sh_ant_req) \
  1361. LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
  1362. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1363. wifi_sh_ant_req))
  1364. #define LUT_WLAN_KILL_OP(lut, op, val) \
  1365. lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
  1366. #define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1367. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1368. (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1369. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
  1370. #define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1371. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1372. LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1373. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1374. #define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1375. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1376. LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1377. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1378. #define LUT_ANT_SWITCH_OP(lut, op, val) \
  1379. lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
  1380. #define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1381. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1382. (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1383. wifi_req, wifi_prio, wifi_txrx, \
  1384. wifi_sh_ant_req))))
  1385. #define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1386. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1387. LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1388. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1389. #define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1390. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1391. LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1392. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1393. static const __le32 iwlagn_def_3w_lookup[12] = {
  1394. cpu_to_le32(0xaaaaaaaa),
  1395. cpu_to_le32(0xaaaaaaaa),
  1396. cpu_to_le32(0xaeaaaaaa),
  1397. cpu_to_le32(0xaaaaaaaa),
  1398. cpu_to_le32(0xcc00ff28),
  1399. cpu_to_le32(0x0000aaaa),
  1400. cpu_to_le32(0xcc00aaaa),
  1401. cpu_to_le32(0x0000aaaa),
  1402. cpu_to_le32(0xc0004000),
  1403. cpu_to_le32(0x00004000),
  1404. cpu_to_le32(0xf0005000),
  1405. cpu_to_le32(0xf0004000),
  1406. };
  1407. static const __le32 iwlagn_concurrent_lookup[12] = {
  1408. cpu_to_le32(0xaaaaaaaa),
  1409. cpu_to_le32(0xaaaaaaaa),
  1410. cpu_to_le32(0xaaaaaaaa),
  1411. cpu_to_le32(0xaaaaaaaa),
  1412. cpu_to_le32(0xaaaaaaaa),
  1413. cpu_to_le32(0xaaaaaaaa),
  1414. cpu_to_le32(0xaaaaaaaa),
  1415. cpu_to_le32(0xaaaaaaaa),
  1416. cpu_to_le32(0x00000000),
  1417. cpu_to_le32(0x00000000),
  1418. cpu_to_le32(0x00000000),
  1419. cpu_to_le32(0x00000000),
  1420. };
  1421. void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
  1422. {
  1423. struct iwlagn_bt_cmd bt_cmd = {
  1424. .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
  1425. .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
  1426. .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
  1427. .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
  1428. };
  1429. BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
  1430. sizeof(bt_cmd.bt3_lookup_table));
  1431. bt_cmd.prio_boost = priv->cfg->bt_prio_boost;
  1432. bt_cmd.kill_ack_mask = priv->kill_ack_mask;
  1433. bt_cmd.kill_cts_mask = priv->kill_cts_mask;
  1434. bt_cmd.valid = priv->bt_valid;
  1435. /*
  1436. * Configure BT coex mode to "no coexistence" when the
  1437. * user disabled BT coexistence, we have no interface
  1438. * (might be in monitor mode), or the interface is in
  1439. * IBSS mode (no proper uCode support for coex then).
  1440. */
  1441. if (!bt_coex_active || priv->iw_mode == NL80211_IFTYPE_ADHOC) {
  1442. bt_cmd.flags = 0;
  1443. } else {
  1444. bt_cmd.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
  1445. IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
  1446. if (priv->bt_ch_announce)
  1447. bt_cmd.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
  1448. IWL_DEBUG_INFO(priv, "BT coex flag: 0X%x\n", bt_cmd.flags);
  1449. }
  1450. if (priv->bt_full_concurrent)
  1451. memcpy(bt_cmd.bt3_lookup_table, iwlagn_concurrent_lookup,
  1452. sizeof(iwlagn_concurrent_lookup));
  1453. else
  1454. memcpy(bt_cmd.bt3_lookup_table, iwlagn_def_3w_lookup,
  1455. sizeof(iwlagn_def_3w_lookup));
  1456. IWL_DEBUG_INFO(priv, "BT coex %s in %s mode\n",
  1457. bt_cmd.flags ? "active" : "disabled",
  1458. priv->bt_full_concurrent ?
  1459. "full concurrency" : "3-wire");
  1460. if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG, sizeof(bt_cmd), &bt_cmd))
  1461. IWL_ERR(priv, "failed to send BT Coex Config\n");
  1462. /*
  1463. * When we are doing a restart, need to also reconfigure BT
  1464. * SCO to the device. If not doing a restart, bt_sco_active
  1465. * will always be false, so there's no need to have an extra
  1466. * variable to check for it.
  1467. */
  1468. if (priv->bt_sco_active) {
  1469. struct iwlagn_bt_sco_cmd sco_cmd = { .flags = 0 };
  1470. if (priv->bt_sco_active)
  1471. sco_cmd.flags |= IWLAGN_BT_SCO_ACTIVE;
  1472. if (iwl_send_cmd_pdu(priv, REPLY_BT_COEX_SCO,
  1473. sizeof(sco_cmd), &sco_cmd))
  1474. IWL_ERR(priv, "failed to send BT SCO command\n");
  1475. }
  1476. }
  1477. static void iwlagn_bt_traffic_change_work(struct work_struct *work)
  1478. {
  1479. struct iwl_priv *priv =
  1480. container_of(work, struct iwl_priv, bt_traffic_change_work);
  1481. struct iwl_rxon_context *ctx;
  1482. int smps_request = -1;
  1483. IWL_DEBUG_INFO(priv, "BT traffic load changes: %d\n",
  1484. priv->bt_traffic_load);
  1485. switch (priv->bt_traffic_load) {
  1486. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1487. smps_request = IEEE80211_SMPS_AUTOMATIC;
  1488. break;
  1489. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1490. smps_request = IEEE80211_SMPS_DYNAMIC;
  1491. break;
  1492. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1493. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1494. smps_request = IEEE80211_SMPS_STATIC;
  1495. break;
  1496. default:
  1497. IWL_ERR(priv, "Invalid BT traffic load: %d\n",
  1498. priv->bt_traffic_load);
  1499. break;
  1500. }
  1501. mutex_lock(&priv->mutex);
  1502. if (priv->cfg->ops->lib->update_chain_flags)
  1503. priv->cfg->ops->lib->update_chain_flags(priv);
  1504. if (smps_request != -1) {
  1505. for_each_context(priv, ctx) {
  1506. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
  1507. ieee80211_request_smps(ctx->vif, smps_request);
  1508. }
  1509. }
  1510. mutex_unlock(&priv->mutex);
  1511. }
  1512. static void iwlagn_print_uartmsg(struct iwl_priv *priv,
  1513. struct iwl_bt_uart_msg *uart_msg)
  1514. {
  1515. IWL_DEBUG_NOTIF(priv, "Message Type = 0x%X, SSN = 0x%X, "
  1516. "Update Req = 0x%X",
  1517. (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
  1518. BT_UART_MSG_FRAME1MSGTYPE_POS,
  1519. (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
  1520. BT_UART_MSG_FRAME1SSN_POS,
  1521. (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
  1522. BT_UART_MSG_FRAME1UPDATEREQ_POS);
  1523. IWL_DEBUG_NOTIF(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
  1524. "Chl_SeqN = 0x%X, In band = 0x%X",
  1525. (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
  1526. BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
  1527. (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
  1528. BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
  1529. (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
  1530. BT_UART_MSG_FRAME2CHLSEQN_POS,
  1531. (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
  1532. BT_UART_MSG_FRAME2INBAND_POS);
  1533. IWL_DEBUG_NOTIF(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
  1534. "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
  1535. (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
  1536. BT_UART_MSG_FRAME3SCOESCO_POS,
  1537. (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
  1538. BT_UART_MSG_FRAME3SNIFF_POS,
  1539. (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
  1540. BT_UART_MSG_FRAME3A2DP_POS,
  1541. (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
  1542. BT_UART_MSG_FRAME3ACL_POS,
  1543. (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
  1544. BT_UART_MSG_FRAME3MASTER_POS,
  1545. (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
  1546. BT_UART_MSG_FRAME3OBEX_POS);
  1547. IWL_DEBUG_NOTIF(priv, "Idle duration = 0x%X",
  1548. (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
  1549. BT_UART_MSG_FRAME4IDLEDURATION_POS);
  1550. IWL_DEBUG_NOTIF(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
  1551. "eSCO Retransmissions = 0x%X",
  1552. (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
  1553. BT_UART_MSG_FRAME5TXACTIVITY_POS,
  1554. (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
  1555. BT_UART_MSG_FRAME5RXACTIVITY_POS,
  1556. (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
  1557. BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
  1558. IWL_DEBUG_NOTIF(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
  1559. (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
  1560. BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
  1561. (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
  1562. BT_UART_MSG_FRAME6DISCOVERABLE_POS);
  1563. IWL_DEBUG_NOTIF(priv, "Sniff Activity = 0x%X, Inquiry/Page SR Mode = "
  1564. "0x%X, Connectable = 0x%X",
  1565. (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
  1566. BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
  1567. (BT_UART_MSG_FRAME7INQUIRYPAGESRMODE_MSK & uart_msg->frame7) >>
  1568. BT_UART_MSG_FRAME7INQUIRYPAGESRMODE_POS,
  1569. (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
  1570. BT_UART_MSG_FRAME7CONNECTABLE_POS);
  1571. }
  1572. static void iwlagn_set_kill_ack_msk(struct iwl_priv *priv,
  1573. struct iwl_bt_uart_msg *uart_msg)
  1574. {
  1575. u8 kill_ack_msk;
  1576. __le32 bt_kill_ack_msg[2] = {
  1577. cpu_to_le32(0xFFFFFFF), cpu_to_le32(0xFFFFFC00) };
  1578. kill_ack_msk = (((BT_UART_MSG_FRAME3A2DP_MSK |
  1579. BT_UART_MSG_FRAME3SNIFF_MSK |
  1580. BT_UART_MSG_FRAME3SCOESCO_MSK) &
  1581. uart_msg->frame3) == 0) ? 1 : 0;
  1582. if (priv->kill_ack_mask != bt_kill_ack_msg[kill_ack_msk]) {
  1583. priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
  1584. priv->kill_ack_mask = bt_kill_ack_msg[kill_ack_msk];
  1585. /* schedule to send runtime bt_config */
  1586. queue_work(priv->workqueue, &priv->bt_runtime_config);
  1587. }
  1588. }
  1589. void iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
  1590. struct iwl_rx_mem_buffer *rxb)
  1591. {
  1592. unsigned long flags;
  1593. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1594. struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
  1595. struct iwlagn_bt_sco_cmd sco_cmd = { .flags = 0 };
  1596. struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
  1597. u8 last_traffic_load;
  1598. IWL_DEBUG_NOTIF(priv, "BT Coex notification:\n");
  1599. IWL_DEBUG_NOTIF(priv, " status: %d\n", coex->bt_status);
  1600. IWL_DEBUG_NOTIF(priv, " traffic load: %d\n", coex->bt_traffic_load);
  1601. IWL_DEBUG_NOTIF(priv, " CI compliance: %d\n",
  1602. coex->bt_ci_compliance);
  1603. iwlagn_print_uartmsg(priv, uart_msg);
  1604. last_traffic_load = priv->notif_bt_traffic_load;
  1605. priv->notif_bt_traffic_load = coex->bt_traffic_load;
  1606. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  1607. if (priv->bt_status != coex->bt_status ||
  1608. last_traffic_load != coex->bt_traffic_load) {
  1609. if (coex->bt_status) {
  1610. /* BT on */
  1611. if (!priv->bt_ch_announce)
  1612. priv->bt_traffic_load =
  1613. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1614. else
  1615. priv->bt_traffic_load =
  1616. coex->bt_traffic_load;
  1617. } else {
  1618. /* BT off */
  1619. priv->bt_traffic_load =
  1620. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  1621. }
  1622. priv->bt_status = coex->bt_status;
  1623. queue_work(priv->workqueue,
  1624. &priv->bt_traffic_change_work);
  1625. }
  1626. if (priv->bt_sco_active !=
  1627. (uart_msg->frame3 & BT_UART_MSG_FRAME3SCOESCO_MSK)) {
  1628. priv->bt_sco_active = uart_msg->frame3 &
  1629. BT_UART_MSG_FRAME3SCOESCO_MSK;
  1630. if (priv->bt_sco_active)
  1631. sco_cmd.flags |= IWLAGN_BT_SCO_ACTIVE;
  1632. iwl_send_cmd_pdu_async(priv, REPLY_BT_COEX_SCO,
  1633. sizeof(sco_cmd), &sco_cmd, NULL);
  1634. }
  1635. }
  1636. iwlagn_set_kill_ack_msk(priv, uart_msg);
  1637. /* FIXME: based on notification, adjust the prio_boost */
  1638. spin_lock_irqsave(&priv->lock, flags);
  1639. priv->bt_ci_compliance = coex->bt_ci_compliance;
  1640. spin_unlock_irqrestore(&priv->lock, flags);
  1641. }
  1642. void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
  1643. {
  1644. iwlagn_rx_handler_setup(priv);
  1645. priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
  1646. iwlagn_bt_coex_profile_notif;
  1647. }
  1648. void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
  1649. {
  1650. iwlagn_setup_deferred_work(priv);
  1651. INIT_WORK(&priv->bt_traffic_change_work,
  1652. iwlagn_bt_traffic_change_work);
  1653. }
  1654. void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
  1655. {
  1656. cancel_work_sync(&priv->bt_traffic_change_work);
  1657. }