iwl-agn-lib.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2012 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-wifi.h"
  35. #include "iwl-dev.h"
  36. #include "iwl-core.h"
  37. #include "iwl-io.h"
  38. #include "iwl-agn-hw.h"
  39. #include "iwl-agn.h"
  40. #include "iwl-trans.h"
  41. #include "iwl-shared.h"
  42. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  43. {
  44. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  45. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  46. }
  47. int iwlagn_send_tx_power(struct iwl_priv *priv)
  48. {
  49. struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
  50. u8 tx_ant_cfg_cmd;
  51. if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->shrd->status),
  52. "TX Power requested while scanning!\n"))
  53. return -EAGAIN;
  54. /* half dBm need to multiply */
  55. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  56. if (priv->tx_power_lmt_in_half_dbm &&
  57. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  58. /*
  59. * For the newer devices which using enhanced/extend tx power
  60. * table in EEPROM, the format is in half dBm. driver need to
  61. * convert to dBm format before report to mac80211.
  62. * By doing so, there is a possibility of 1/2 dBm resolution
  63. * lost. driver will perform "round-up" operation before
  64. * reporting, but it will cause 1/2 dBm tx power over the
  65. * regulatory limit. Perform the checking here, if the
  66. * "tx_power_user_lmt" is higher than EEPROM value (in
  67. * half-dBm format), lower the tx power based on EEPROM
  68. */
  69. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  70. }
  71. tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
  72. tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
  73. if (IWL_UCODE_API(nic(priv)->fw.ucode_ver) == 1)
  74. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  75. else
  76. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  77. return iwl_trans_send_cmd_pdu(trans(priv), tx_ant_cfg_cmd, CMD_SYNC,
  78. sizeof(tx_power_cmd), &tx_power_cmd);
  79. }
  80. void iwlagn_temperature(struct iwl_priv *priv)
  81. {
  82. lockdep_assert_held(&priv->statistics.lock);
  83. /* store temperature from correct statistics (in Celsius) */
  84. priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
  85. iwl_tt_handler(priv);
  86. }
  87. u16 iwl_eeprom_calib_version(struct iwl_shared *shrd)
  88. {
  89. struct iwl_eeprom_calib_hdr *hdr;
  90. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(shrd,
  91. EEPROM_CALIB_ALL);
  92. return hdr->version;
  93. }
  94. /*
  95. * EEPROM
  96. */
  97. static u32 eeprom_indirect_address(const struct iwl_shared *shrd, u32 address)
  98. {
  99. u16 offset = 0;
  100. if ((address & INDIRECT_ADDRESS) == 0)
  101. return address;
  102. switch (address & INDIRECT_TYPE_MSK) {
  103. case INDIRECT_HOST:
  104. offset = iwl_eeprom_query16(shrd, EEPROM_LINK_HOST);
  105. break;
  106. case INDIRECT_GENERAL:
  107. offset = iwl_eeprom_query16(shrd, EEPROM_LINK_GENERAL);
  108. break;
  109. case INDIRECT_REGULATORY:
  110. offset = iwl_eeprom_query16(shrd, EEPROM_LINK_REGULATORY);
  111. break;
  112. case INDIRECT_TXP_LIMIT:
  113. offset = iwl_eeprom_query16(shrd, EEPROM_LINK_TXP_LIMIT);
  114. break;
  115. case INDIRECT_TXP_LIMIT_SIZE:
  116. offset = iwl_eeprom_query16(shrd, EEPROM_LINK_TXP_LIMIT_SIZE);
  117. break;
  118. case INDIRECT_CALIBRATION:
  119. offset = iwl_eeprom_query16(shrd, EEPROM_LINK_CALIBRATION);
  120. break;
  121. case INDIRECT_PROCESS_ADJST:
  122. offset = iwl_eeprom_query16(shrd, EEPROM_LINK_PROCESS_ADJST);
  123. break;
  124. case INDIRECT_OTHERS:
  125. offset = iwl_eeprom_query16(shrd, EEPROM_LINK_OTHERS);
  126. break;
  127. default:
  128. IWL_ERR(shrd->trans, "illegal indirect type: 0x%X\n",
  129. address & INDIRECT_TYPE_MSK);
  130. break;
  131. }
  132. /* translate the offset from words to byte */
  133. return (address & ADDRESS_MSK) + (offset << 1);
  134. }
  135. const u8 *iwl_eeprom_query_addr(const struct iwl_shared *shrd, size_t offset)
  136. {
  137. u32 address = eeprom_indirect_address(shrd, offset);
  138. BUG_ON(address >= shrd->cfg->base_params->eeprom_size);
  139. return &shrd->eeprom[address];
  140. }
  141. struct iwl_mod_params iwlagn_mod_params = {
  142. .amsdu_size_8K = 1,
  143. .restart_fw = 1,
  144. .plcp_check = true,
  145. .bt_coex_active = true,
  146. .no_sleep_autoadjust = true,
  147. .power_level = IWL_POWER_INDEX_1,
  148. .bt_ch_announce = true,
  149. .wanted_ucode_alternative = 1,
  150. .auto_agg = true,
  151. /* the rest are 0 by default */
  152. };
  153. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  154. {
  155. int idx = 0;
  156. int band_offset = 0;
  157. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  158. if (rate_n_flags & RATE_MCS_HT_MSK) {
  159. idx = (rate_n_flags & 0xff);
  160. return idx;
  161. /* Legacy rate format, search for match in table */
  162. } else {
  163. if (band == IEEE80211_BAND_5GHZ)
  164. band_offset = IWL_FIRST_OFDM_RATE;
  165. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  166. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  167. return idx - band_offset;
  168. }
  169. return -1;
  170. }
  171. int iwlagn_manage_ibss_station(struct iwl_priv *priv,
  172. struct ieee80211_vif *vif, bool add)
  173. {
  174. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  175. if (add)
  176. return iwlagn_add_bssid_station(priv, vif_priv->ctx,
  177. vif->bss_conf.bssid,
  178. &vif_priv->ibss_bssid_sta_id);
  179. return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
  180. vif->bss_conf.bssid);
  181. }
  182. /**
  183. * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
  184. *
  185. * pre-requirements:
  186. * 1. acquire mutex before calling
  187. * 2. make sure rf is on and not in exit state
  188. */
  189. int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  190. {
  191. struct iwl_txfifo_flush_cmd flush_cmd;
  192. struct iwl_host_cmd cmd = {
  193. .id = REPLY_TXFIFO_FLUSH,
  194. .len = { sizeof(struct iwl_txfifo_flush_cmd), },
  195. .flags = CMD_SYNC,
  196. .data = { &flush_cmd, },
  197. };
  198. might_sleep();
  199. memset(&flush_cmd, 0, sizeof(flush_cmd));
  200. if (flush_control & BIT(IWL_RXON_CTX_BSS))
  201. flush_cmd.fifo_control = IWL_SCD_VO_MSK | IWL_SCD_VI_MSK |
  202. IWL_SCD_BE_MSK | IWL_SCD_BK_MSK |
  203. IWL_SCD_MGMT_MSK;
  204. if ((flush_control & BIT(IWL_RXON_CTX_PAN)) &&
  205. (priv->shrd->valid_contexts != BIT(IWL_RXON_CTX_BSS)))
  206. flush_cmd.fifo_control |= IWL_PAN_SCD_VO_MSK |
  207. IWL_PAN_SCD_VI_MSK | IWL_PAN_SCD_BE_MSK |
  208. IWL_PAN_SCD_BK_MSK | IWL_PAN_SCD_MGMT_MSK |
  209. IWL_PAN_SCD_MULTICAST_MSK;
  210. if (hw_params(priv).sku & EEPROM_SKU_CAP_11N_ENABLE)
  211. flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
  212. IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
  213. flush_cmd.fifo_control);
  214. flush_cmd.flush_control = cpu_to_le16(flush_control);
  215. return iwl_trans_send_cmd(trans(priv), &cmd);
  216. }
  217. void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  218. {
  219. mutex_lock(&priv->shrd->mutex);
  220. ieee80211_stop_queues(priv->hw);
  221. if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
  222. IWL_ERR(priv, "flush request fail\n");
  223. goto done;
  224. }
  225. IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
  226. iwl_trans_wait_tx_queue_empty(trans(priv));
  227. done:
  228. ieee80211_wake_queues(priv->hw);
  229. mutex_unlock(&priv->shrd->mutex);
  230. }
  231. /*
  232. * BT coex
  233. */
  234. /*
  235. * Macros to access the lookup table.
  236. *
  237. * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
  238. * wifi_prio, wifi_txrx and wifi_sh_ant_req.
  239. *
  240. * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
  241. *
  242. * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
  243. * one after another in 32-bit registers, and "registers" 0 through 7 contain
  244. * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
  245. *
  246. * These macros encode that format.
  247. */
  248. #define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
  249. wifi_txrx, wifi_sh_ant_req) \
  250. (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
  251. (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
  252. #define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
  253. lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
  254. #define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  255. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  256. (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
  257. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  258. wifi_sh_ant_req))))
  259. #define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  260. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  261. LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
  262. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  263. wifi_sh_ant_req))
  264. #define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
  265. wifi_req, wifi_prio, wifi_txrx, \
  266. wifi_sh_ant_req) \
  267. LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
  268. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  269. wifi_sh_ant_req))
  270. #define LUT_WLAN_KILL_OP(lut, op, val) \
  271. lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
  272. #define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  273. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  274. (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  275. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
  276. #define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  277. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  278. LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  279. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  280. #define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  281. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  282. LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  283. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  284. #define LUT_ANT_SWITCH_OP(lut, op, val) \
  285. lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
  286. #define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  287. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  288. (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  289. wifi_req, wifi_prio, wifi_txrx, \
  290. wifi_sh_ant_req))))
  291. #define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  292. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  293. LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  294. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  295. #define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  296. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  297. LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  298. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  299. static const __le32 iwlagn_def_3w_lookup[12] = {
  300. cpu_to_le32(0xaaaaaaaa),
  301. cpu_to_le32(0xaaaaaaaa),
  302. cpu_to_le32(0xaeaaaaaa),
  303. cpu_to_le32(0xaaaaaaaa),
  304. cpu_to_le32(0xcc00ff28),
  305. cpu_to_le32(0x0000aaaa),
  306. cpu_to_le32(0xcc00aaaa),
  307. cpu_to_le32(0x0000aaaa),
  308. cpu_to_le32(0xc0004000),
  309. cpu_to_le32(0x00004000),
  310. cpu_to_le32(0xf0005000),
  311. cpu_to_le32(0xf0005000),
  312. };
  313. static const __le32 iwlagn_concurrent_lookup[12] = {
  314. cpu_to_le32(0xaaaaaaaa),
  315. cpu_to_le32(0xaaaaaaaa),
  316. cpu_to_le32(0xaaaaaaaa),
  317. cpu_to_le32(0xaaaaaaaa),
  318. cpu_to_le32(0xaaaaaaaa),
  319. cpu_to_le32(0xaaaaaaaa),
  320. cpu_to_le32(0xaaaaaaaa),
  321. cpu_to_le32(0xaaaaaaaa),
  322. cpu_to_le32(0x00000000),
  323. cpu_to_le32(0x00000000),
  324. cpu_to_le32(0x00000000),
  325. cpu_to_le32(0x00000000),
  326. };
  327. void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
  328. {
  329. struct iwl_basic_bt_cmd basic = {
  330. .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
  331. .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
  332. .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
  333. .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
  334. };
  335. struct iwl6000_bt_cmd bt_cmd_6000;
  336. struct iwl2000_bt_cmd bt_cmd_2000;
  337. int ret;
  338. BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
  339. sizeof(basic.bt3_lookup_table));
  340. if (cfg(priv)->bt_params) {
  341. if (cfg(priv)->bt_params->bt_session_2) {
  342. bt_cmd_2000.prio_boost = cpu_to_le32(
  343. cfg(priv)->bt_params->bt_prio_boost);
  344. bt_cmd_2000.tx_prio_boost = 0;
  345. bt_cmd_2000.rx_prio_boost = 0;
  346. } else {
  347. bt_cmd_6000.prio_boost =
  348. cfg(priv)->bt_params->bt_prio_boost;
  349. bt_cmd_6000.tx_prio_boost = 0;
  350. bt_cmd_6000.rx_prio_boost = 0;
  351. }
  352. } else {
  353. IWL_ERR(priv, "failed to construct BT Coex Config\n");
  354. return;
  355. }
  356. basic.kill_ack_mask = priv->kill_ack_mask;
  357. basic.kill_cts_mask = priv->kill_cts_mask;
  358. basic.valid = priv->bt_valid;
  359. /*
  360. * Configure BT coex mode to "no coexistence" when the
  361. * user disabled BT coexistence, we have no interface
  362. * (might be in monitor mode), or the interface is in
  363. * IBSS mode (no proper uCode support for coex then).
  364. */
  365. if (!iwlagn_mod_params.bt_coex_active ||
  366. priv->iw_mode == NL80211_IFTYPE_ADHOC) {
  367. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
  368. } else {
  369. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
  370. IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
  371. if (!priv->bt_enable_pspoll)
  372. basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
  373. else
  374. basic.flags &= ~IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
  375. if (priv->bt_ch_announce)
  376. basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
  377. IWL_DEBUG_COEX(priv, "BT coex flag: 0X%x\n", basic.flags);
  378. }
  379. priv->bt_enable_flag = basic.flags;
  380. if (priv->bt_full_concurrent)
  381. memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
  382. sizeof(iwlagn_concurrent_lookup));
  383. else
  384. memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
  385. sizeof(iwlagn_def_3w_lookup));
  386. IWL_DEBUG_COEX(priv, "BT coex %s in %s mode\n",
  387. basic.flags ? "active" : "disabled",
  388. priv->bt_full_concurrent ?
  389. "full concurrency" : "3-wire");
  390. if (cfg(priv)->bt_params->bt_session_2) {
  391. memcpy(&bt_cmd_2000.basic, &basic,
  392. sizeof(basic));
  393. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
  394. CMD_SYNC, sizeof(bt_cmd_2000), &bt_cmd_2000);
  395. } else {
  396. memcpy(&bt_cmd_6000.basic, &basic,
  397. sizeof(basic));
  398. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
  399. CMD_SYNC, sizeof(bt_cmd_6000), &bt_cmd_6000);
  400. }
  401. if (ret)
  402. IWL_ERR(priv, "failed to send BT Coex Config\n");
  403. }
  404. void iwlagn_bt_adjust_rssi_monitor(struct iwl_priv *priv, bool rssi_ena)
  405. {
  406. struct iwl_rxon_context *ctx, *found_ctx = NULL;
  407. bool found_ap = false;
  408. lockdep_assert_held(&priv->shrd->mutex);
  409. /* Check whether AP or GO mode is active. */
  410. if (rssi_ena) {
  411. for_each_context(priv, ctx) {
  412. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_AP &&
  413. iwl_is_associated_ctx(ctx)) {
  414. found_ap = true;
  415. break;
  416. }
  417. }
  418. }
  419. /*
  420. * If disable was received or If GO/AP mode, disable RSSI
  421. * measurements.
  422. */
  423. if (!rssi_ena || found_ap) {
  424. if (priv->cur_rssi_ctx) {
  425. ctx = priv->cur_rssi_ctx;
  426. ieee80211_disable_rssi_reports(ctx->vif);
  427. priv->cur_rssi_ctx = NULL;
  428. }
  429. return;
  430. }
  431. /*
  432. * If rssi measurements need to be enabled, consider all cases now.
  433. * Figure out how many contexts are active.
  434. */
  435. for_each_context(priv, ctx) {
  436. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
  437. iwl_is_associated_ctx(ctx)) {
  438. found_ctx = ctx;
  439. break;
  440. }
  441. }
  442. /*
  443. * rssi monitor already enabled for the correct interface...nothing
  444. * to do.
  445. */
  446. if (found_ctx == priv->cur_rssi_ctx)
  447. return;
  448. /*
  449. * Figure out if rssi monitor is currently enabled, and needs
  450. * to be changed. If rssi monitor is already enabled, disable
  451. * it first else just enable rssi measurements on the
  452. * interface found above.
  453. */
  454. if (priv->cur_rssi_ctx) {
  455. ctx = priv->cur_rssi_ctx;
  456. if (ctx->vif)
  457. ieee80211_disable_rssi_reports(ctx->vif);
  458. }
  459. priv->cur_rssi_ctx = found_ctx;
  460. if (!found_ctx)
  461. return;
  462. ieee80211_enable_rssi_reports(found_ctx->vif,
  463. IWLAGN_BT_PSP_MIN_RSSI_THRESHOLD,
  464. IWLAGN_BT_PSP_MAX_RSSI_THRESHOLD);
  465. }
  466. static bool iwlagn_bt_traffic_is_sco(struct iwl_bt_uart_msg *uart_msg)
  467. {
  468. return BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3 >>
  469. BT_UART_MSG_FRAME3SCOESCO_POS;
  470. }
  471. static void iwlagn_bt_traffic_change_work(struct work_struct *work)
  472. {
  473. struct iwl_priv *priv =
  474. container_of(work, struct iwl_priv, bt_traffic_change_work);
  475. struct iwl_rxon_context *ctx;
  476. int smps_request = -1;
  477. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  478. /* bt coex disabled */
  479. return;
  480. }
  481. /*
  482. * Note: bt_traffic_load can be overridden by scan complete and
  483. * coex profile notifications. Ignore that since only bad consequence
  484. * can be not matching debug print with actual state.
  485. */
  486. IWL_DEBUG_COEX(priv, "BT traffic load changes: %d\n",
  487. priv->bt_traffic_load);
  488. switch (priv->bt_traffic_load) {
  489. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  490. if (priv->bt_status)
  491. smps_request = IEEE80211_SMPS_DYNAMIC;
  492. else
  493. smps_request = IEEE80211_SMPS_AUTOMATIC;
  494. break;
  495. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  496. smps_request = IEEE80211_SMPS_DYNAMIC;
  497. break;
  498. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  499. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  500. smps_request = IEEE80211_SMPS_STATIC;
  501. break;
  502. default:
  503. IWL_ERR(priv, "Invalid BT traffic load: %d\n",
  504. priv->bt_traffic_load);
  505. break;
  506. }
  507. mutex_lock(&priv->shrd->mutex);
  508. /*
  509. * We can not send command to firmware while scanning. When the scan
  510. * complete we will schedule this work again. We do check with mutex
  511. * locked to prevent new scan request to arrive. We do not check
  512. * STATUS_SCANNING to avoid race when queue_work two times from
  513. * different notifications, but quit and not perform any work at all.
  514. */
  515. if (test_bit(STATUS_SCAN_HW, &priv->shrd->status))
  516. goto out;
  517. iwl_update_chain_flags(priv);
  518. if (smps_request != -1) {
  519. priv->current_ht_config.smps = smps_request;
  520. for_each_context(priv, ctx) {
  521. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
  522. ieee80211_request_smps(ctx->vif, smps_request);
  523. }
  524. }
  525. /*
  526. * Dynamic PS poll related functionality. Adjust RSSI measurements if
  527. * necessary.
  528. */
  529. iwlagn_bt_coex_rssi_monitor(priv);
  530. out:
  531. mutex_unlock(&priv->shrd->mutex);
  532. }
  533. /*
  534. * If BT sco traffic, and RSSI monitor is enabled, move measurements to the
  535. * correct interface or disable it if this is the last interface to be
  536. * removed.
  537. */
  538. void iwlagn_bt_coex_rssi_monitor(struct iwl_priv *priv)
  539. {
  540. if (priv->bt_is_sco &&
  541. priv->bt_traffic_load == IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS)
  542. iwlagn_bt_adjust_rssi_monitor(priv, true);
  543. else
  544. iwlagn_bt_adjust_rssi_monitor(priv, false);
  545. }
  546. static void iwlagn_print_uartmsg(struct iwl_priv *priv,
  547. struct iwl_bt_uart_msg *uart_msg)
  548. {
  549. IWL_DEBUG_COEX(priv, "Message Type = 0x%X, SSN = 0x%X, "
  550. "Update Req = 0x%X",
  551. (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
  552. BT_UART_MSG_FRAME1MSGTYPE_POS,
  553. (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
  554. BT_UART_MSG_FRAME1SSN_POS,
  555. (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
  556. BT_UART_MSG_FRAME1UPDATEREQ_POS);
  557. IWL_DEBUG_COEX(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
  558. "Chl_SeqN = 0x%X, In band = 0x%X",
  559. (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
  560. BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
  561. (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
  562. BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
  563. (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
  564. BT_UART_MSG_FRAME2CHLSEQN_POS,
  565. (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
  566. BT_UART_MSG_FRAME2INBAND_POS);
  567. IWL_DEBUG_COEX(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
  568. "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
  569. (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
  570. BT_UART_MSG_FRAME3SCOESCO_POS,
  571. (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
  572. BT_UART_MSG_FRAME3SNIFF_POS,
  573. (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
  574. BT_UART_MSG_FRAME3A2DP_POS,
  575. (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
  576. BT_UART_MSG_FRAME3ACL_POS,
  577. (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
  578. BT_UART_MSG_FRAME3MASTER_POS,
  579. (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
  580. BT_UART_MSG_FRAME3OBEX_POS);
  581. IWL_DEBUG_COEX(priv, "Idle duration = 0x%X",
  582. (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
  583. BT_UART_MSG_FRAME4IDLEDURATION_POS);
  584. IWL_DEBUG_COEX(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
  585. "eSCO Retransmissions = 0x%X",
  586. (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
  587. BT_UART_MSG_FRAME5TXACTIVITY_POS,
  588. (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
  589. BT_UART_MSG_FRAME5RXACTIVITY_POS,
  590. (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
  591. BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
  592. IWL_DEBUG_COEX(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
  593. (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
  594. BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
  595. (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
  596. BT_UART_MSG_FRAME6DISCOVERABLE_POS);
  597. IWL_DEBUG_COEX(priv, "Sniff Activity = 0x%X, Page = "
  598. "0x%X, Inquiry = 0x%X, Connectable = 0x%X",
  599. (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
  600. BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
  601. (BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
  602. BT_UART_MSG_FRAME7PAGE_POS,
  603. (BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
  604. BT_UART_MSG_FRAME7INQUIRY_POS,
  605. (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
  606. BT_UART_MSG_FRAME7CONNECTABLE_POS);
  607. }
  608. static void iwlagn_set_kill_msk(struct iwl_priv *priv,
  609. struct iwl_bt_uart_msg *uart_msg)
  610. {
  611. u8 kill_msk;
  612. static const __le32 bt_kill_ack_msg[2] = {
  613. IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
  614. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  615. static const __le32 bt_kill_cts_msg[2] = {
  616. IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
  617. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  618. kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
  619. ? 1 : 0;
  620. if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
  621. priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
  622. priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
  623. priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
  624. priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
  625. priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
  626. /* schedule to send runtime bt_config */
  627. queue_work(priv->workqueue, &priv->bt_runtime_config);
  628. }
  629. }
  630. int iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
  631. struct iwl_rx_mem_buffer *rxb,
  632. struct iwl_device_cmd *cmd)
  633. {
  634. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  635. struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
  636. struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
  637. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  638. /* bt coex disabled */
  639. return 0;
  640. }
  641. IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
  642. IWL_DEBUG_COEX(priv, " status: %d\n", coex->bt_status);
  643. IWL_DEBUG_COEX(priv, " traffic load: %d\n", coex->bt_traffic_load);
  644. IWL_DEBUG_COEX(priv, " CI compliance: %d\n",
  645. coex->bt_ci_compliance);
  646. iwlagn_print_uartmsg(priv, uart_msg);
  647. priv->last_bt_traffic_load = priv->bt_traffic_load;
  648. priv->bt_is_sco = iwlagn_bt_traffic_is_sco(uart_msg);
  649. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  650. if (priv->bt_status != coex->bt_status ||
  651. priv->last_bt_traffic_load != coex->bt_traffic_load) {
  652. if (coex->bt_status) {
  653. /* BT on */
  654. if (!priv->bt_ch_announce)
  655. priv->bt_traffic_load =
  656. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  657. else
  658. priv->bt_traffic_load =
  659. coex->bt_traffic_load;
  660. } else {
  661. /* BT off */
  662. priv->bt_traffic_load =
  663. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  664. }
  665. priv->bt_status = coex->bt_status;
  666. queue_work(priv->workqueue,
  667. &priv->bt_traffic_change_work);
  668. }
  669. }
  670. iwlagn_set_kill_msk(priv, uart_msg);
  671. /* FIXME: based on notification, adjust the prio_boost */
  672. priv->bt_ci_compliance = coex->bt_ci_compliance;
  673. return 0;
  674. }
  675. void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
  676. {
  677. priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
  678. iwlagn_bt_coex_profile_notif;
  679. }
  680. void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
  681. {
  682. INIT_WORK(&priv->bt_traffic_change_work,
  683. iwlagn_bt_traffic_change_work);
  684. }
  685. void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
  686. {
  687. cancel_work_sync(&priv->bt_traffic_change_work);
  688. }
  689. static bool is_single_rx_stream(struct iwl_priv *priv)
  690. {
  691. return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
  692. priv->current_ht_config.single_chain_sufficient;
  693. }
  694. #define IWL_NUM_RX_CHAINS_MULTIPLE 3
  695. #define IWL_NUM_RX_CHAINS_SINGLE 2
  696. #define IWL_NUM_IDLE_CHAINS_DUAL 2
  697. #define IWL_NUM_IDLE_CHAINS_SINGLE 1
  698. /*
  699. * Determine how many receiver/antenna chains to use.
  700. *
  701. * More provides better reception via diversity. Fewer saves power
  702. * at the expense of throughput, but only when not in powersave to
  703. * start with.
  704. *
  705. * MIMO (dual stream) requires at least 2, but works better with 3.
  706. * This does not determine *which* chains to use, just how many.
  707. */
  708. static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
  709. {
  710. if (cfg(priv)->bt_params &&
  711. cfg(priv)->bt_params->advanced_bt_coexist &&
  712. (priv->bt_full_concurrent ||
  713. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  714. /*
  715. * only use chain 'A' in bt high traffic load or
  716. * full concurrency mode
  717. */
  718. return IWL_NUM_RX_CHAINS_SINGLE;
  719. }
  720. /* # of Rx chains to use when expecting MIMO. */
  721. if (is_single_rx_stream(priv))
  722. return IWL_NUM_RX_CHAINS_SINGLE;
  723. else
  724. return IWL_NUM_RX_CHAINS_MULTIPLE;
  725. }
  726. /*
  727. * When we are in power saving mode, unless device support spatial
  728. * multiplexing power save, use the active count for rx chain count.
  729. */
  730. static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
  731. {
  732. /* # Rx chains when idling, depending on SMPS mode */
  733. switch (priv->current_ht_config.smps) {
  734. case IEEE80211_SMPS_STATIC:
  735. case IEEE80211_SMPS_DYNAMIC:
  736. return IWL_NUM_IDLE_CHAINS_SINGLE;
  737. case IEEE80211_SMPS_AUTOMATIC:
  738. case IEEE80211_SMPS_OFF:
  739. return active_cnt;
  740. default:
  741. WARN(1, "invalid SMPS mode %d",
  742. priv->current_ht_config.smps);
  743. return active_cnt;
  744. }
  745. }
  746. /* up to 4 chains */
  747. static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
  748. {
  749. u8 res;
  750. res = (chain_bitmap & BIT(0)) >> 0;
  751. res += (chain_bitmap & BIT(1)) >> 1;
  752. res += (chain_bitmap & BIT(2)) >> 2;
  753. res += (chain_bitmap & BIT(3)) >> 3;
  754. return res;
  755. }
  756. /**
  757. * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  758. *
  759. * Selects how many and which Rx receivers/antennas/chains to use.
  760. * This should not be used for scan command ... it puts data in wrong place.
  761. */
  762. void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  763. {
  764. bool is_single = is_single_rx_stream(priv);
  765. bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->shrd->status);
  766. u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
  767. u32 active_chains;
  768. u16 rx_chain;
  769. /* Tell uCode which antennas are actually connected.
  770. * Before first association, we assume all antennas are connected.
  771. * Just after first association, iwl_chain_noise_calibration()
  772. * checks which antennas actually *are* connected. */
  773. if (priv->chain_noise_data.active_chains)
  774. active_chains = priv->chain_noise_data.active_chains;
  775. else
  776. active_chains = hw_params(priv).valid_rx_ant;
  777. if (cfg(priv)->bt_params &&
  778. cfg(priv)->bt_params->advanced_bt_coexist &&
  779. (priv->bt_full_concurrent ||
  780. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  781. /*
  782. * only use chain 'A' in bt high traffic load or
  783. * full concurrency mode
  784. */
  785. active_chains = first_antenna(active_chains);
  786. }
  787. rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
  788. /* How many receivers should we use? */
  789. active_rx_cnt = iwl_get_active_rx_chain_count(priv);
  790. idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
  791. /* correct rx chain count according hw settings
  792. * and chain noise calibration
  793. */
  794. valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
  795. if (valid_rx_cnt < active_rx_cnt)
  796. active_rx_cnt = valid_rx_cnt;
  797. if (valid_rx_cnt < idle_rx_cnt)
  798. idle_rx_cnt = valid_rx_cnt;
  799. rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
  800. rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
  801. ctx->staging.rx_chain = cpu_to_le16(rx_chain);
  802. if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
  803. ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  804. else
  805. ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  806. IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
  807. ctx->staging.rx_chain,
  808. active_rx_cnt, idle_rx_cnt);
  809. WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
  810. active_rx_cnt < idle_rx_cnt);
  811. }
  812. u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
  813. {
  814. int i;
  815. u8 ind = ant;
  816. if (priv->band == IEEE80211_BAND_2GHZ &&
  817. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
  818. return 0;
  819. for (i = 0; i < RATE_ANT_NUM - 1; i++) {
  820. ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
  821. if (valid & BIT(ind))
  822. return ind;
  823. }
  824. return ant;
  825. }
  826. #ifdef CONFIG_PM_SLEEP
  827. static void iwlagn_convert_p1k(u16 *p1k, __le16 *out)
  828. {
  829. int i;
  830. for (i = 0; i < IWLAGN_P1K_SIZE; i++)
  831. out[i] = cpu_to_le16(p1k[i]);
  832. }
  833. struct wowlan_key_data {
  834. struct iwl_rxon_context *ctx;
  835. struct iwlagn_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  836. struct iwlagn_wowlan_tkip_params_cmd *tkip;
  837. const u8 *bssid;
  838. bool error, use_rsc_tsc, use_tkip;
  839. };
  840. static void iwlagn_wowlan_program_keys(struct ieee80211_hw *hw,
  841. struct ieee80211_vif *vif,
  842. struct ieee80211_sta *sta,
  843. struct ieee80211_key_conf *key,
  844. void *_data)
  845. {
  846. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  847. struct wowlan_key_data *data = _data;
  848. struct iwl_rxon_context *ctx = data->ctx;
  849. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  850. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  851. struct iwlagn_p1k_cache *rx_p1ks;
  852. u8 *rx_mic_key;
  853. struct ieee80211_key_seq seq;
  854. u32 cur_rx_iv32 = 0;
  855. u16 p1k[IWLAGN_P1K_SIZE];
  856. int ret, i;
  857. mutex_lock(&priv->shrd->mutex);
  858. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  859. key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
  860. !sta && !ctx->key_mapping_keys)
  861. ret = iwl_set_default_wep_key(priv, ctx, key);
  862. else
  863. ret = iwl_set_dynamic_key(priv, ctx, key, sta);
  864. if (ret) {
  865. IWL_ERR(priv, "Error setting key during suspend!\n");
  866. data->error = true;
  867. }
  868. switch (key->cipher) {
  869. case WLAN_CIPHER_SUITE_TKIP:
  870. if (sta) {
  871. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  872. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  873. rx_p1ks = data->tkip->rx_uni;
  874. ieee80211_get_key_tx_seq(key, &seq);
  875. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  876. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  877. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  878. iwlagn_convert_p1k(p1k, data->tkip->tx.p1k);
  879. memcpy(data->tkip->mic_keys.tx,
  880. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  881. IWLAGN_MIC_KEY_SIZE);
  882. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  883. } else {
  884. tkip_sc =
  885. data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  886. rx_p1ks = data->tkip->rx_multi;
  887. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  888. }
  889. /*
  890. * For non-QoS this relies on the fact that both the uCode and
  891. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  892. * for checking the IV in the frames.
  893. */
  894. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  895. ieee80211_get_key_rx_seq(key, i, &seq);
  896. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  897. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  898. /* wrapping isn't allowed, AP must rekey */
  899. if (seq.tkip.iv32 > cur_rx_iv32)
  900. cur_rx_iv32 = seq.tkip.iv32;
  901. }
  902. ieee80211_get_tkip_rx_p1k(key, data->bssid, cur_rx_iv32, p1k);
  903. iwlagn_convert_p1k(p1k, rx_p1ks[0].p1k);
  904. ieee80211_get_tkip_rx_p1k(key, data->bssid,
  905. cur_rx_iv32 + 1, p1k);
  906. iwlagn_convert_p1k(p1k, rx_p1ks[1].p1k);
  907. memcpy(rx_mic_key,
  908. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  909. IWLAGN_MIC_KEY_SIZE);
  910. data->use_tkip = true;
  911. data->use_rsc_tsc = true;
  912. break;
  913. case WLAN_CIPHER_SUITE_CCMP:
  914. if (sta) {
  915. u8 *pn = seq.ccmp.pn;
  916. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  917. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  918. ieee80211_get_key_tx_seq(key, &seq);
  919. aes_tx_sc->pn = cpu_to_le64(
  920. (u64)pn[5] |
  921. ((u64)pn[4] << 8) |
  922. ((u64)pn[3] << 16) |
  923. ((u64)pn[2] << 24) |
  924. ((u64)pn[1] << 32) |
  925. ((u64)pn[0] << 40));
  926. } else
  927. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  928. /*
  929. * For non-QoS this relies on the fact that both the uCode and
  930. * mac80211 use TID 0 for checking the IV in the frames.
  931. */
  932. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  933. u8 *pn = seq.ccmp.pn;
  934. ieee80211_get_key_rx_seq(key, i, &seq);
  935. aes_sc->pn = cpu_to_le64(
  936. (u64)pn[5] |
  937. ((u64)pn[4] << 8) |
  938. ((u64)pn[3] << 16) |
  939. ((u64)pn[2] << 24) |
  940. ((u64)pn[1] << 32) |
  941. ((u64)pn[0] << 40));
  942. }
  943. data->use_rsc_tsc = true;
  944. break;
  945. }
  946. mutex_unlock(&priv->shrd->mutex);
  947. }
  948. int iwlagn_send_patterns(struct iwl_priv *priv,
  949. struct cfg80211_wowlan *wowlan)
  950. {
  951. struct iwlagn_wowlan_patterns_cmd *pattern_cmd;
  952. struct iwl_host_cmd cmd = {
  953. .id = REPLY_WOWLAN_PATTERNS,
  954. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  955. .flags = CMD_SYNC,
  956. };
  957. int i, err;
  958. if (!wowlan->n_patterns)
  959. return 0;
  960. cmd.len[0] = sizeof(*pattern_cmd) +
  961. wowlan->n_patterns * sizeof(struct iwlagn_wowlan_pattern);
  962. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  963. if (!pattern_cmd)
  964. return -ENOMEM;
  965. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  966. for (i = 0; i < wowlan->n_patterns; i++) {
  967. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  968. memcpy(&pattern_cmd->patterns[i].mask,
  969. wowlan->patterns[i].mask, mask_len);
  970. memcpy(&pattern_cmd->patterns[i].pattern,
  971. wowlan->patterns[i].pattern,
  972. wowlan->patterns[i].pattern_len);
  973. pattern_cmd->patterns[i].mask_size = mask_len;
  974. pattern_cmd->patterns[i].pattern_size =
  975. wowlan->patterns[i].pattern_len;
  976. }
  977. cmd.data[0] = pattern_cmd;
  978. err = iwl_trans_send_cmd(trans(priv), &cmd);
  979. kfree(pattern_cmd);
  980. return err;
  981. }
  982. int iwlagn_suspend(struct iwl_priv *priv,
  983. struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  984. {
  985. struct iwlagn_wowlan_wakeup_filter_cmd wakeup_filter_cmd;
  986. struct iwl_rxon_cmd rxon;
  987. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  988. struct iwlagn_wowlan_kek_kck_material_cmd kek_kck_cmd;
  989. struct iwlagn_wowlan_tkip_params_cmd tkip_cmd = {};
  990. struct iwlagn_d3_config_cmd d3_cfg_cmd = {};
  991. struct wowlan_key_data key_data = {
  992. .ctx = ctx,
  993. .bssid = ctx->active.bssid_addr,
  994. .use_rsc_tsc = false,
  995. .tkip = &tkip_cmd,
  996. .use_tkip = false,
  997. };
  998. int ret, i;
  999. u16 seq;
  1000. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  1001. if (!key_data.rsc_tsc)
  1002. return -ENOMEM;
  1003. memset(&wakeup_filter_cmd, 0, sizeof(wakeup_filter_cmd));
  1004. /*
  1005. * We know the last used seqno, and the uCode expects to know that
  1006. * one, it will increment before TX.
  1007. */
  1008. seq = le16_to_cpu(priv->last_seq_ctl) & IEEE80211_SCTL_SEQ;
  1009. wakeup_filter_cmd.non_qos_seq = cpu_to_le16(seq);
  1010. /*
  1011. * For QoS counters, we store the one to use next, so subtract 0x10
  1012. * since the uCode will add 0x10 before using the value.
  1013. */
  1014. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1015. seq = priv->tid_data[IWL_AP_ID][i].seq_number;
  1016. seq -= 0x10;
  1017. wakeup_filter_cmd.qos_seq[i] = cpu_to_le16(seq);
  1018. }
  1019. if (wowlan->disconnect)
  1020. wakeup_filter_cmd.enabled |=
  1021. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
  1022. IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE);
  1023. if (wowlan->magic_pkt)
  1024. wakeup_filter_cmd.enabled |=
  1025. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET);
  1026. if (wowlan->gtk_rekey_failure)
  1027. wakeup_filter_cmd.enabled |=
  1028. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  1029. if (wowlan->eap_identity_req)
  1030. wakeup_filter_cmd.enabled |=
  1031. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  1032. if (wowlan->four_way_handshake)
  1033. wakeup_filter_cmd.enabled |=
  1034. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  1035. if (wowlan->n_patterns)
  1036. wakeup_filter_cmd.enabled |=
  1037. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH);
  1038. if (wowlan->rfkill_release)
  1039. d3_cfg_cmd.wakeup_flags |=
  1040. cpu_to_le32(IWLAGN_D3_WAKEUP_RFKILL);
  1041. iwl_scan_cancel_timeout(priv, 200);
  1042. memcpy(&rxon, &ctx->active, sizeof(rxon));
  1043. iwl_trans_stop_device(trans(priv));
  1044. priv->wowlan = true;
  1045. ret = iwl_load_ucode_wait_alive(trans(priv), IWL_UCODE_WOWLAN);
  1046. if (ret)
  1047. goto out;
  1048. /* now configure WoWLAN ucode */
  1049. ret = iwl_alive_start(priv);
  1050. if (ret)
  1051. goto out;
  1052. memcpy(&ctx->staging, &rxon, sizeof(rxon));
  1053. ret = iwlagn_commit_rxon(priv, ctx);
  1054. if (ret)
  1055. goto out;
  1056. ret = iwl_power_update_mode(priv, true);
  1057. if (ret)
  1058. goto out;
  1059. if (!iwlagn_mod_params.sw_crypto) {
  1060. /* mark all keys clear */
  1061. priv->ucode_key_table = 0;
  1062. ctx->key_mapping_keys = 0;
  1063. /*
  1064. * This needs to be unlocked due to lock ordering
  1065. * constraints. Since we're in the suspend path
  1066. * that isn't really a problem though.
  1067. */
  1068. mutex_unlock(&priv->shrd->mutex);
  1069. ieee80211_iter_keys(priv->hw, ctx->vif,
  1070. iwlagn_wowlan_program_keys,
  1071. &key_data);
  1072. mutex_lock(&priv->shrd->mutex);
  1073. if (key_data.error) {
  1074. ret = -EIO;
  1075. goto out;
  1076. }
  1077. if (key_data.use_rsc_tsc) {
  1078. struct iwl_host_cmd rsc_tsc_cmd = {
  1079. .id = REPLY_WOWLAN_TSC_RSC_PARAMS,
  1080. .flags = CMD_SYNC,
  1081. .data[0] = key_data.rsc_tsc,
  1082. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  1083. .len[0] = sizeof(key_data.rsc_tsc),
  1084. };
  1085. ret = iwl_trans_send_cmd(trans(priv), &rsc_tsc_cmd);
  1086. if (ret)
  1087. goto out;
  1088. }
  1089. if (key_data.use_tkip) {
  1090. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1091. REPLY_WOWLAN_TKIP_PARAMS,
  1092. CMD_SYNC, sizeof(tkip_cmd),
  1093. &tkip_cmd);
  1094. if (ret)
  1095. goto out;
  1096. }
  1097. if (priv->have_rekey_data) {
  1098. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  1099. memcpy(kek_kck_cmd.kck, priv->kck, NL80211_KCK_LEN);
  1100. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  1101. memcpy(kek_kck_cmd.kek, priv->kek, NL80211_KEK_LEN);
  1102. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  1103. kek_kck_cmd.replay_ctr = priv->replay_ctr;
  1104. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1105. REPLY_WOWLAN_KEK_KCK_MATERIAL,
  1106. CMD_SYNC, sizeof(kek_kck_cmd),
  1107. &kek_kck_cmd);
  1108. if (ret)
  1109. goto out;
  1110. }
  1111. }
  1112. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_D3_CONFIG, CMD_SYNC,
  1113. sizeof(d3_cfg_cmd), &d3_cfg_cmd);
  1114. if (ret)
  1115. goto out;
  1116. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_WOWLAN_WAKEUP_FILTER,
  1117. CMD_SYNC, sizeof(wakeup_filter_cmd),
  1118. &wakeup_filter_cmd);
  1119. if (ret)
  1120. goto out;
  1121. ret = iwlagn_send_patterns(priv, wowlan);
  1122. out:
  1123. kfree(key_data.rsc_tsc);
  1124. return ret;
  1125. }
  1126. #endif