iwl-core.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 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. * Tomas Winkler <tomas.winkler@intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *****************************************************************************/
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <net/mac80211.h>
  31. struct iwl_priv; /* FIXME: remove */
  32. #include "iwl-debug.h"
  33. #include "iwl-eeprom.h"
  34. #include "iwl-dev.h" /* FIXME: remove */
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-rfkill.h"
  38. #include "iwl-power.h"
  39. MODULE_DESCRIPTION("iwl core");
  40. MODULE_VERSION(IWLWIFI_VERSION);
  41. MODULE_AUTHOR(DRV_COPYRIGHT);
  42. MODULE_LICENSE("GPL");
  43. #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
  44. [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
  45. IWL_RATE_SISO_##s##M_PLCP, \
  46. IWL_RATE_MIMO2_##s##M_PLCP,\
  47. IWL_RATE_MIMO3_##s##M_PLCP,\
  48. IWL_RATE_##r##M_IEEE, \
  49. IWL_RATE_##ip##M_INDEX, \
  50. IWL_RATE_##in##M_INDEX, \
  51. IWL_RATE_##rp##M_INDEX, \
  52. IWL_RATE_##rn##M_INDEX, \
  53. IWL_RATE_##pp##M_INDEX, \
  54. IWL_RATE_##np##M_INDEX }
  55. /*
  56. * Parameter order:
  57. * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
  58. *
  59. * If there isn't a valid next or previous rate then INV is used which
  60. * maps to IWL_RATE_INVALID
  61. *
  62. */
  63. const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
  64. IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
  65. IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
  66. IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
  67. IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
  68. IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
  69. IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
  70. IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
  71. IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
  72. IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
  73. IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
  74. IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
  75. IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
  76. IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
  77. /* FIXME:RS: ^^ should be INV (legacy) */
  78. };
  79. EXPORT_SYMBOL(iwl_rates);
  80. /**
  81. * translate ucode response to mac80211 tx status control values
  82. */
  83. void iwl_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
  84. struct ieee80211_tx_info *info)
  85. {
  86. int rate_index;
  87. struct ieee80211_tx_rate *r = &info->control.rates[0];
  88. info->antenna_sel_tx =
  89. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  90. if (rate_n_flags & RATE_MCS_HT_MSK)
  91. r->flags |= IEEE80211_TX_RC_MCS;
  92. if (rate_n_flags & RATE_MCS_GF_MSK)
  93. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  94. if (rate_n_flags & RATE_MCS_FAT_MSK)
  95. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  96. if (rate_n_flags & RATE_MCS_DUP_MSK)
  97. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  98. if (rate_n_flags & RATE_MCS_SGI_MSK)
  99. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  100. rate_index = iwl_hwrate_to_plcp_idx(rate_n_flags);
  101. if (info->band == IEEE80211_BAND_5GHZ)
  102. rate_index -= IWL_FIRST_OFDM_RATE;
  103. r->idx = rate_index;
  104. }
  105. EXPORT_SYMBOL(iwl_hwrate_to_tx_control);
  106. int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
  107. {
  108. int idx = 0;
  109. /* HT rate format */
  110. if (rate_n_flags & RATE_MCS_HT_MSK) {
  111. idx = (rate_n_flags & 0xff);
  112. if (idx >= IWL_RATE_MIMO2_6M_PLCP)
  113. idx = idx - IWL_RATE_MIMO2_6M_PLCP;
  114. idx += IWL_FIRST_OFDM_RATE;
  115. /* skip 9M not supported in ht*/
  116. if (idx >= IWL_RATE_9M_INDEX)
  117. idx += 1;
  118. if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
  119. return idx;
  120. /* legacy rate format, search for match in table */
  121. } else {
  122. for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
  123. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  124. return idx;
  125. }
  126. return -1;
  127. }
  128. EXPORT_SYMBOL(iwl_hwrate_to_plcp_idx);
  129. u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant)
  130. {
  131. int i;
  132. u8 ind = ant;
  133. for (i = 0; i < RATE_ANT_NUM - 1; i++) {
  134. ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
  135. if (priv->hw_params.valid_tx_ant & BIT(ind))
  136. return ind;
  137. }
  138. return ant;
  139. }
  140. const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  141. EXPORT_SYMBOL(iwl_bcast_addr);
  142. /* This function both allocates and initializes hw and priv. */
  143. struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg,
  144. struct ieee80211_ops *hw_ops)
  145. {
  146. struct iwl_priv *priv;
  147. /* mac80211 allocates memory for this device instance, including
  148. * space for this driver's private structure */
  149. struct ieee80211_hw *hw =
  150. ieee80211_alloc_hw(sizeof(struct iwl_priv), hw_ops);
  151. if (hw == NULL) {
  152. IWL_ERROR("Can not allocate network device\n");
  153. goto out;
  154. }
  155. priv = hw->priv;
  156. priv->hw = hw;
  157. out:
  158. return hw;
  159. }
  160. EXPORT_SYMBOL(iwl_alloc_all);
  161. void iwl_hw_detect(struct iwl_priv *priv)
  162. {
  163. priv->hw_rev = _iwl_read32(priv, CSR_HW_REV);
  164. priv->hw_wa_rev = _iwl_read32(priv, CSR_HW_REV_WA_REG);
  165. pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &priv->rev_id);
  166. }
  167. EXPORT_SYMBOL(iwl_hw_detect);
  168. /* Tell nic where to find the "keep warm" buffer */
  169. int iwl_kw_init(struct iwl_priv *priv)
  170. {
  171. unsigned long flags;
  172. int ret;
  173. spin_lock_irqsave(&priv->lock, flags);
  174. ret = iwl_grab_nic_access(priv);
  175. if (ret)
  176. goto out;
  177. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG,
  178. priv->kw.dma_addr >> 4);
  179. iwl_release_nic_access(priv);
  180. out:
  181. spin_unlock_irqrestore(&priv->lock, flags);
  182. return ret;
  183. }
  184. int iwl_kw_alloc(struct iwl_priv *priv)
  185. {
  186. struct pci_dev *dev = priv->pci_dev;
  187. struct iwl_kw *kw = &priv->kw;
  188. kw->size = IWL_KW_SIZE;
  189. kw->v_addr = pci_alloc_consistent(dev, kw->size, &kw->dma_addr);
  190. if (!kw->v_addr)
  191. return -ENOMEM;
  192. return 0;
  193. }
  194. /**
  195. * iwl_kw_free - Free the "keep warm" buffer
  196. */
  197. void iwl_kw_free(struct iwl_priv *priv)
  198. {
  199. struct pci_dev *dev = priv->pci_dev;
  200. struct iwl_kw *kw = &priv->kw;
  201. if (kw->v_addr) {
  202. pci_free_consistent(dev, kw->size, kw->v_addr, kw->dma_addr);
  203. memset(kw, 0, sizeof(*kw));
  204. }
  205. }
  206. int iwl_hw_nic_init(struct iwl_priv *priv)
  207. {
  208. unsigned long flags;
  209. struct iwl_rx_queue *rxq = &priv->rxq;
  210. int ret;
  211. /* nic_init */
  212. spin_lock_irqsave(&priv->lock, flags);
  213. priv->cfg->ops->lib->apm_ops.init(priv);
  214. iwl_write32(priv, CSR_INT_COALESCING, 512 / 32);
  215. spin_unlock_irqrestore(&priv->lock, flags);
  216. ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
  217. priv->cfg->ops->lib->apm_ops.config(priv);
  218. /* Allocate the RX queue, or reset if it is already allocated */
  219. if (!rxq->bd) {
  220. ret = iwl_rx_queue_alloc(priv);
  221. if (ret) {
  222. IWL_ERROR("Unable to initialize Rx queue\n");
  223. return -ENOMEM;
  224. }
  225. } else
  226. iwl_rx_queue_reset(priv, rxq);
  227. iwl_rx_replenish(priv);
  228. iwl_rx_init(priv, rxq);
  229. spin_lock_irqsave(&priv->lock, flags);
  230. rxq->need_update = 1;
  231. iwl_rx_queue_update_write_ptr(priv, rxq);
  232. spin_unlock_irqrestore(&priv->lock, flags);
  233. /* Allocate and init all Tx and Command queues */
  234. ret = iwl_txq_ctx_reset(priv);
  235. if (ret)
  236. return ret;
  237. set_bit(STATUS_INIT, &priv->status);
  238. return 0;
  239. }
  240. EXPORT_SYMBOL(iwl_hw_nic_init);
  241. /**
  242. * iwl_clear_stations_table - Clear the driver's station table
  243. *
  244. * NOTE: This does not clear or otherwise alter the device's station table.
  245. */
  246. void iwl_clear_stations_table(struct iwl_priv *priv)
  247. {
  248. unsigned long flags;
  249. spin_lock_irqsave(&priv->sta_lock, flags);
  250. if (iwl_is_alive(priv) &&
  251. !test_bit(STATUS_EXIT_PENDING, &priv->status) &&
  252. iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL))
  253. IWL_ERROR("Couldn't clear the station table\n");
  254. priv->num_stations = 0;
  255. memset(priv->stations, 0, sizeof(priv->stations));
  256. spin_unlock_irqrestore(&priv->sta_lock, flags);
  257. }
  258. EXPORT_SYMBOL(iwl_clear_stations_table);
  259. void iwl_reset_qos(struct iwl_priv *priv)
  260. {
  261. u16 cw_min = 15;
  262. u16 cw_max = 1023;
  263. u8 aifs = 2;
  264. u8 is_legacy = 0;
  265. unsigned long flags;
  266. int i;
  267. spin_lock_irqsave(&priv->lock, flags);
  268. priv->qos_data.qos_active = 0;
  269. if (priv->iw_mode == NL80211_IFTYPE_ADHOC) {
  270. if (priv->qos_data.qos_enable)
  271. priv->qos_data.qos_active = 1;
  272. if (!(priv->active_rate & 0xfff0)) {
  273. cw_min = 31;
  274. is_legacy = 1;
  275. }
  276. } else if (priv->iw_mode == NL80211_IFTYPE_AP) {
  277. if (priv->qos_data.qos_enable)
  278. priv->qos_data.qos_active = 1;
  279. } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
  280. cw_min = 31;
  281. is_legacy = 1;
  282. }
  283. if (priv->qos_data.qos_active)
  284. aifs = 3;
  285. priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
  286. priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
  287. priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
  288. priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
  289. priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
  290. if (priv->qos_data.qos_active) {
  291. i = 1;
  292. priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
  293. priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
  294. priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
  295. priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
  296. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  297. i = 2;
  298. priv->qos_data.def_qos_parm.ac[i].cw_min =
  299. cpu_to_le16((cw_min + 1) / 2 - 1);
  300. priv->qos_data.def_qos_parm.ac[i].cw_max =
  301. cpu_to_le16(cw_max);
  302. priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
  303. if (is_legacy)
  304. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  305. cpu_to_le16(6016);
  306. else
  307. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  308. cpu_to_le16(3008);
  309. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  310. i = 3;
  311. priv->qos_data.def_qos_parm.ac[i].cw_min =
  312. cpu_to_le16((cw_min + 1) / 4 - 1);
  313. priv->qos_data.def_qos_parm.ac[i].cw_max =
  314. cpu_to_le16((cw_max + 1) / 2 - 1);
  315. priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
  316. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  317. if (is_legacy)
  318. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  319. cpu_to_le16(3264);
  320. else
  321. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  322. cpu_to_le16(1504);
  323. } else {
  324. for (i = 1; i < 4; i++) {
  325. priv->qos_data.def_qos_parm.ac[i].cw_min =
  326. cpu_to_le16(cw_min);
  327. priv->qos_data.def_qos_parm.ac[i].cw_max =
  328. cpu_to_le16(cw_max);
  329. priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
  330. priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
  331. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  332. }
  333. }
  334. IWL_DEBUG_QOS("set QoS to default \n");
  335. spin_unlock_irqrestore(&priv->lock, flags);
  336. }
  337. EXPORT_SYMBOL(iwl_reset_qos);
  338. #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
  339. #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
  340. static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
  341. struct ieee80211_sta_ht_cap *ht_info,
  342. enum ieee80211_band band)
  343. {
  344. u16 max_bit_rate = 0;
  345. u8 rx_chains_num = priv->hw_params.rx_chains_num;
  346. u8 tx_chains_num = priv->hw_params.tx_chains_num;
  347. ht_info->cap = 0;
  348. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  349. ht_info->ht_supported = true;
  350. ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
  351. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  352. ht_info->cap |= (IEEE80211_HT_CAP_SM_PS &
  353. (WLAN_HT_CAP_SM_PS_DISABLED << 2));
  354. max_bit_rate = MAX_BIT_RATE_20_MHZ;
  355. if (priv->hw_params.fat_channel & BIT(band)) {
  356. ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  357. ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
  358. ht_info->mcs.rx_mask[4] = 0x01;
  359. max_bit_rate = MAX_BIT_RATE_40_MHZ;
  360. }
  361. if (priv->cfg->mod_params->amsdu_size_8K)
  362. ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  363. ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
  364. ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
  365. ht_info->mcs.rx_mask[0] = 0xFF;
  366. if (rx_chains_num >= 2)
  367. ht_info->mcs.rx_mask[1] = 0xFF;
  368. if (rx_chains_num >= 3)
  369. ht_info->mcs.rx_mask[2] = 0xFF;
  370. /* Highest supported Rx data rate */
  371. max_bit_rate *= rx_chains_num;
  372. WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
  373. ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
  374. /* Tx MCS capabilities */
  375. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  376. if (tx_chains_num != rx_chains_num) {
  377. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  378. ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
  379. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  380. }
  381. }
  382. static void iwlcore_init_hw_rates(struct iwl_priv *priv,
  383. struct ieee80211_rate *rates)
  384. {
  385. int i;
  386. for (i = 0; i < IWL_RATE_COUNT; i++) {
  387. rates[i].bitrate = iwl_rates[i].ieee * 5;
  388. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  389. rates[i].hw_value_short = i;
  390. rates[i].flags = 0;
  391. if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  392. /*
  393. * If CCK != 1M then set short preamble rate flag.
  394. */
  395. rates[i].flags |=
  396. (iwl_rates[i].plcp == IWL_RATE_1M_PLCP) ?
  397. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  398. }
  399. }
  400. }
  401. /**
  402. * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
  403. */
  404. static int iwlcore_init_geos(struct iwl_priv *priv)
  405. {
  406. struct iwl_channel_info *ch;
  407. struct ieee80211_supported_band *sband;
  408. struct ieee80211_channel *channels;
  409. struct ieee80211_channel *geo_ch;
  410. struct ieee80211_rate *rates;
  411. int i = 0;
  412. if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  413. priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  414. IWL_DEBUG_INFO("Geography modes already initialized.\n");
  415. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  416. return 0;
  417. }
  418. channels = kzalloc(sizeof(struct ieee80211_channel) *
  419. priv->channel_count, GFP_KERNEL);
  420. if (!channels)
  421. return -ENOMEM;
  422. rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
  423. GFP_KERNEL);
  424. if (!rates) {
  425. kfree(channels);
  426. return -ENOMEM;
  427. }
  428. /* 5.2GHz channels start after the 2.4GHz channels */
  429. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  430. sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
  431. /* just OFDM */
  432. sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
  433. sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
  434. if (priv->cfg->sku & IWL_SKU_N)
  435. iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
  436. IEEE80211_BAND_5GHZ);
  437. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  438. sband->channels = channels;
  439. /* OFDM & CCK */
  440. sband->bitrates = rates;
  441. sband->n_bitrates = IWL_RATE_COUNT;
  442. if (priv->cfg->sku & IWL_SKU_N)
  443. iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
  444. IEEE80211_BAND_2GHZ);
  445. priv->ieee_channels = channels;
  446. priv->ieee_rates = rates;
  447. iwlcore_init_hw_rates(priv, rates);
  448. for (i = 0; i < priv->channel_count; i++) {
  449. ch = &priv->channel_info[i];
  450. /* FIXME: might be removed if scan is OK */
  451. if (!is_channel_valid(ch))
  452. continue;
  453. if (is_channel_a_band(ch))
  454. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  455. else
  456. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  457. geo_ch = &sband->channels[sband->n_channels++];
  458. geo_ch->center_freq =
  459. ieee80211_channel_to_frequency(ch->channel);
  460. geo_ch->max_power = ch->max_power_avg;
  461. geo_ch->max_antenna_gain = 0xff;
  462. geo_ch->hw_value = ch->channel;
  463. if (is_channel_valid(ch)) {
  464. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  465. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  466. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  467. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  468. if (ch->flags & EEPROM_CHANNEL_RADAR)
  469. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  470. geo_ch->flags |= ch->fat_extension_channel;
  471. if (ch->max_power_avg > priv->tx_power_channel_lmt)
  472. priv->tx_power_channel_lmt = ch->max_power_avg;
  473. } else {
  474. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  475. }
  476. /* Save flags for reg domain usage */
  477. geo_ch->orig_flags = geo_ch->flags;
  478. IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
  479. ch->channel, geo_ch->center_freq,
  480. is_channel_a_band(ch) ? "5.2" : "2.4",
  481. geo_ch->flags & IEEE80211_CHAN_DISABLED ?
  482. "restricted" : "valid",
  483. geo_ch->flags);
  484. }
  485. if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
  486. priv->cfg->sku & IWL_SKU_A) {
  487. printk(KERN_INFO DRV_NAME
  488. ": Incorrectly detected BG card as ABG. Please send "
  489. "your PCI ID 0x%04X:0x%04X to maintainer.\n",
  490. priv->pci_dev->device, priv->pci_dev->subsystem_device);
  491. priv->cfg->sku &= ~IWL_SKU_A;
  492. }
  493. printk(KERN_INFO DRV_NAME
  494. ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  495. priv->bands[IEEE80211_BAND_2GHZ].n_channels,
  496. priv->bands[IEEE80211_BAND_5GHZ].n_channels);
  497. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  498. return 0;
  499. }
  500. /*
  501. * iwlcore_free_geos - undo allocations in iwlcore_init_geos
  502. */
  503. static void iwlcore_free_geos(struct iwl_priv *priv)
  504. {
  505. kfree(priv->ieee_channels);
  506. kfree(priv->ieee_rates);
  507. clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
  508. }
  509. static bool is_single_rx_stream(struct iwl_priv *priv)
  510. {
  511. return !priv->current_ht_config.is_ht ||
  512. ((priv->current_ht_config.mcs.rx_mask[1] == 0) &&
  513. (priv->current_ht_config.mcs.rx_mask[2] == 0));
  514. }
  515. static u8 iwl_is_channel_extension(struct iwl_priv *priv,
  516. enum ieee80211_band band,
  517. u16 channel, u8 extension_chan_offset)
  518. {
  519. const struct iwl_channel_info *ch_info;
  520. ch_info = iwl_get_channel_info(priv, band, channel);
  521. if (!is_channel_valid(ch_info))
  522. return 0;
  523. if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
  524. return !(ch_info->fat_extension_channel &
  525. IEEE80211_CHAN_NO_FAT_ABOVE);
  526. else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
  527. return !(ch_info->fat_extension_channel &
  528. IEEE80211_CHAN_NO_FAT_BELOW);
  529. return 0;
  530. }
  531. u8 iwl_is_fat_tx_allowed(struct iwl_priv *priv,
  532. struct ieee80211_sta_ht_cap *sta_ht_inf)
  533. {
  534. struct iwl_ht_info *iwl_ht_conf = &priv->current_ht_config;
  535. if ((!iwl_ht_conf->is_ht) ||
  536. (iwl_ht_conf->supported_chan_width != IWL_CHANNEL_WIDTH_40MHZ) ||
  537. (iwl_ht_conf->extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_NONE))
  538. return 0;
  539. if (sta_ht_inf) {
  540. if ((!sta_ht_inf->ht_supported) ||
  541. (!(sta_ht_inf->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)))
  542. return 0;
  543. }
  544. return iwl_is_channel_extension(priv, priv->band,
  545. le16_to_cpu(priv->staging_rxon.channel),
  546. iwl_ht_conf->extension_chan_offset);
  547. }
  548. EXPORT_SYMBOL(iwl_is_fat_tx_allowed);
  549. void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_info *ht_info)
  550. {
  551. struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
  552. u32 val;
  553. if (!ht_info->is_ht) {
  554. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
  555. RXON_FLG_CHANNEL_MODE_PURE_40_MSK |
  556. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  557. RXON_FLG_FAT_PROT_MSK |
  558. RXON_FLG_HT_PROT_MSK);
  559. return;
  560. }
  561. /* Set up channel bandwidth: 20 MHz only, or 20/40 mixed if fat ok */
  562. if (iwl_is_fat_tx_allowed(priv, NULL))
  563. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  564. else
  565. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
  566. RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
  567. /* Note: control channel is opposite of extension channel */
  568. switch (ht_info->extension_chan_offset) {
  569. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  570. rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  571. break;
  572. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  573. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  574. break;
  575. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  576. default:
  577. rxon->flags &= ~RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  578. break;
  579. }
  580. val = ht_info->ht_protection;
  581. rxon->flags |= cpu_to_le32(val << RXON_FLG_HT_OPERATING_MODE_POS);
  582. iwl_set_rxon_chain(priv);
  583. IWL_DEBUG_ASSOC("supported HT rate 0x%X 0x%X 0x%X "
  584. "rxon flags 0x%X operation mode :0x%X "
  585. "extension channel offset 0x%x\n",
  586. ht_info->mcs.rx_mask[0],
  587. ht_info->mcs.rx_mask[1],
  588. ht_info->mcs.rx_mask[2],
  589. le32_to_cpu(rxon->flags), ht_info->ht_protection,
  590. ht_info->extension_chan_offset);
  591. return;
  592. }
  593. EXPORT_SYMBOL(iwl_set_rxon_ht);
  594. #define IWL_NUM_RX_CHAINS_MULTIPLE 3
  595. #define IWL_NUM_RX_CHAINS_SINGLE 2
  596. #define IWL_NUM_IDLE_CHAINS_DUAL 2
  597. #define IWL_NUM_IDLE_CHAINS_SINGLE 1
  598. /* Determine how many receiver/antenna chains to use.
  599. * More provides better reception via diversity. Fewer saves power.
  600. * MIMO (dual stream) requires at least 2, but works better with 3.
  601. * This does not determine *which* chains to use, just how many.
  602. */
  603. static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
  604. {
  605. bool is_single = is_single_rx_stream(priv);
  606. bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
  607. /* # of Rx chains to use when expecting MIMO. */
  608. if (is_single || (!is_cam && (priv->current_ht_config.sm_ps ==
  609. WLAN_HT_CAP_SM_PS_STATIC)))
  610. return IWL_NUM_RX_CHAINS_SINGLE;
  611. else
  612. return IWL_NUM_RX_CHAINS_MULTIPLE;
  613. }
  614. static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
  615. {
  616. int idle_cnt;
  617. bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
  618. /* # Rx chains when idling and maybe trying to save power */
  619. switch (priv->current_ht_config.sm_ps) {
  620. case WLAN_HT_CAP_SM_PS_STATIC:
  621. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  622. idle_cnt = (is_cam) ? IWL_NUM_IDLE_CHAINS_DUAL :
  623. IWL_NUM_IDLE_CHAINS_SINGLE;
  624. break;
  625. case WLAN_HT_CAP_SM_PS_DISABLED:
  626. idle_cnt = (is_cam) ? active_cnt : IWL_NUM_IDLE_CHAINS_SINGLE;
  627. break;
  628. case WLAN_HT_CAP_SM_PS_INVALID:
  629. default:
  630. IWL_ERROR("invalide mimo ps mode %d\n",
  631. priv->current_ht_config.sm_ps);
  632. WARN_ON(1);
  633. idle_cnt = -1;
  634. break;
  635. }
  636. return idle_cnt;
  637. }
  638. /* up to 4 chains */
  639. static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
  640. {
  641. u8 res;
  642. res = (chain_bitmap & BIT(0)) >> 0;
  643. res += (chain_bitmap & BIT(1)) >> 1;
  644. res += (chain_bitmap & BIT(2)) >> 2;
  645. res += (chain_bitmap & BIT(4)) >> 4;
  646. return res;
  647. }
  648. /**
  649. * iwl_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  650. *
  651. * Selects how many and which Rx receivers/antennas/chains to use.
  652. * This should not be used for scan command ... it puts data in wrong place.
  653. */
  654. void iwl_set_rxon_chain(struct iwl_priv *priv)
  655. {
  656. bool is_single = is_single_rx_stream(priv);
  657. bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
  658. u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
  659. u32 active_chains;
  660. u16 rx_chain;
  661. /* Tell uCode which antennas are actually connected.
  662. * Before first association, we assume all antennas are connected.
  663. * Just after first association, iwl_chain_noise_calibration()
  664. * checks which antennas actually *are* connected. */
  665. if (priv->chain_noise_data.active_chains)
  666. active_chains = priv->chain_noise_data.active_chains;
  667. else
  668. active_chains = priv->hw_params.valid_rx_ant;
  669. rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
  670. /* How many receivers should we use? */
  671. active_rx_cnt = iwl_get_active_rx_chain_count(priv);
  672. idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
  673. /* correct rx chain count according hw settings
  674. * and chain noise calibration
  675. */
  676. valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
  677. if (valid_rx_cnt < active_rx_cnt)
  678. active_rx_cnt = valid_rx_cnt;
  679. if (valid_rx_cnt < idle_rx_cnt)
  680. idle_rx_cnt = valid_rx_cnt;
  681. rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
  682. rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
  683. priv->staging_rxon.rx_chain = cpu_to_le16(rx_chain);
  684. if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
  685. priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  686. else
  687. priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  688. IWL_DEBUG_ASSOC("rx_chain=0x%X active=%d idle=%d\n",
  689. priv->staging_rxon.rx_chain,
  690. active_rx_cnt, idle_rx_cnt);
  691. WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
  692. active_rx_cnt < idle_rx_cnt);
  693. }
  694. EXPORT_SYMBOL(iwl_set_rxon_chain);
  695. /**
  696. * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
  697. * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
  698. * @channel: Any channel valid for the requested phymode
  699. * In addition to setting the staging RXON, priv->phymode is also set.
  700. *
  701. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  702. * in the staging RXON flag structure based on the phymode
  703. */
  704. int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch)
  705. {
  706. enum ieee80211_band band = ch->band;
  707. u16 channel = ieee80211_frequency_to_channel(ch->center_freq);
  708. if (!iwl_get_channel_info(priv, band, channel)) {
  709. IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
  710. channel, band);
  711. return -EINVAL;
  712. }
  713. if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
  714. (priv->band == band))
  715. return 0;
  716. priv->staging_rxon.channel = cpu_to_le16(channel);
  717. if (band == IEEE80211_BAND_5GHZ)
  718. priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
  719. else
  720. priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
  721. priv->band = band;
  722. IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
  723. return 0;
  724. }
  725. EXPORT_SYMBOL(iwl_set_rxon_channel);
  726. int iwl_setup_mac(struct iwl_priv *priv)
  727. {
  728. int ret;
  729. struct ieee80211_hw *hw = priv->hw;
  730. hw->rate_control_algorithm = "iwl-agn-rs";
  731. /* Tell mac80211 our characteristics */
  732. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  733. IEEE80211_HW_NOISE_DBM;
  734. hw->wiphy->interface_modes =
  735. BIT(NL80211_IFTYPE_AP) |
  736. BIT(NL80211_IFTYPE_STATION) |
  737. BIT(NL80211_IFTYPE_ADHOC);
  738. /* Default value; 4 EDCA QOS priorities */
  739. hw->queues = 4;
  740. /* queues to support 11n aggregation */
  741. if (priv->cfg->sku & IWL_SKU_N)
  742. hw->ampdu_queues = priv->cfg->mod_params->num_of_ampdu_queues;
  743. hw->conf.beacon_int = 100;
  744. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  745. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  746. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  747. &priv->bands[IEEE80211_BAND_2GHZ];
  748. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  749. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  750. &priv->bands[IEEE80211_BAND_5GHZ];
  751. ret = ieee80211_register_hw(priv->hw);
  752. if (ret) {
  753. IWL_ERROR("Failed to register hw (error %d)\n", ret);
  754. return ret;
  755. }
  756. priv->mac80211_registered = 1;
  757. return 0;
  758. }
  759. EXPORT_SYMBOL(iwl_setup_mac);
  760. int iwl_set_hw_params(struct iwl_priv *priv)
  761. {
  762. priv->hw_params.sw_crypto = priv->cfg->mod_params->sw_crypto;
  763. priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
  764. priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
  765. if (priv->cfg->mod_params->amsdu_size_8K)
  766. priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
  767. else
  768. priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
  769. priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
  770. if (priv->cfg->mod_params->disable_11n)
  771. priv->cfg->sku &= ~IWL_SKU_N;
  772. /* Device-specific setup */
  773. return priv->cfg->ops->lib->set_hw_params(priv);
  774. }
  775. EXPORT_SYMBOL(iwl_set_hw_params);
  776. int iwl_init_drv(struct iwl_priv *priv)
  777. {
  778. int ret;
  779. priv->retry_rate = 1;
  780. priv->ibss_beacon = NULL;
  781. spin_lock_init(&priv->lock);
  782. spin_lock_init(&priv->power_data.lock);
  783. spin_lock_init(&priv->sta_lock);
  784. spin_lock_init(&priv->hcmd_lock);
  785. INIT_LIST_HEAD(&priv->free_frames);
  786. mutex_init(&priv->mutex);
  787. /* Clear the driver's (not device's) station table */
  788. iwl_clear_stations_table(priv);
  789. priv->data_retry_limit = -1;
  790. priv->ieee_channels = NULL;
  791. priv->ieee_rates = NULL;
  792. priv->band = IEEE80211_BAND_2GHZ;
  793. priv->iw_mode = NL80211_IFTYPE_STATION;
  794. priv->current_ht_config.sm_ps = WLAN_HT_CAP_SM_PS_DISABLED;
  795. /* Choose which receivers/antennas to use */
  796. iwl_set_rxon_chain(priv);
  797. iwl_init_scan_params(priv);
  798. if (priv->cfg->mod_params->enable_qos)
  799. priv->qos_data.qos_enable = 1;
  800. iwl_reset_qos(priv);
  801. priv->qos_data.qos_active = 0;
  802. priv->qos_data.qos_cap.val = 0;
  803. priv->rates_mask = IWL_RATES_MASK;
  804. /* If power management is turned on, default to AC mode */
  805. priv->power_mode = IWL_POWER_AC;
  806. priv->tx_power_user_lmt = IWL_TX_POWER_TARGET_POWER_MAX;
  807. ret = iwl_init_channel_map(priv);
  808. if (ret) {
  809. IWL_ERROR("initializing regulatory failed: %d\n", ret);
  810. goto err;
  811. }
  812. ret = iwlcore_init_geos(priv);
  813. if (ret) {
  814. IWL_ERROR("initializing geos failed: %d\n", ret);
  815. goto err_free_channel_map;
  816. }
  817. return 0;
  818. err_free_channel_map:
  819. iwl_free_channel_map(priv);
  820. err:
  821. return ret;
  822. }
  823. EXPORT_SYMBOL(iwl_init_drv);
  824. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  825. {
  826. int ret = 0;
  827. if (tx_power < IWL_TX_POWER_TARGET_POWER_MIN) {
  828. IWL_WARNING("Requested user TXPOWER %d below limit.\n",
  829. priv->tx_power_user_lmt);
  830. return -EINVAL;
  831. }
  832. if (tx_power > IWL_TX_POWER_TARGET_POWER_MAX) {
  833. IWL_WARNING("Requested user TXPOWER %d above limit.\n",
  834. priv->tx_power_user_lmt);
  835. return -EINVAL;
  836. }
  837. if (priv->tx_power_user_lmt != tx_power)
  838. force = true;
  839. priv->tx_power_user_lmt = tx_power;
  840. if (force && priv->cfg->ops->lib->send_tx_power)
  841. ret = priv->cfg->ops->lib->send_tx_power(priv);
  842. return ret;
  843. }
  844. EXPORT_SYMBOL(iwl_set_tx_power);
  845. void iwl_uninit_drv(struct iwl_priv *priv)
  846. {
  847. iwl_calib_free_results(priv);
  848. iwlcore_free_geos(priv);
  849. iwl_free_channel_map(priv);
  850. kfree(priv->scan);
  851. }
  852. EXPORT_SYMBOL(iwl_uninit_drv);
  853. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags)
  854. {
  855. u32 stat_flags = 0;
  856. struct iwl_host_cmd cmd = {
  857. .id = REPLY_STATISTICS_CMD,
  858. .meta.flags = flags,
  859. .len = sizeof(stat_flags),
  860. .data = (u8 *) &stat_flags,
  861. };
  862. return iwl_send_cmd(priv, &cmd);
  863. }
  864. EXPORT_SYMBOL(iwl_send_statistics_request);
  865. /**
  866. * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
  867. * using sample data 100 bytes apart. If these sample points are good,
  868. * it's a pretty good bet that everything between them is good, too.
  869. */
  870. static int iwlcore_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  871. {
  872. u32 val;
  873. int ret = 0;
  874. u32 errcnt = 0;
  875. u32 i;
  876. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  877. ret = iwl_grab_nic_access(priv);
  878. if (ret)
  879. return ret;
  880. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  881. /* read data comes through single port, auto-incr addr */
  882. /* NOTE: Use the debugless read so we don't flood kernel log
  883. * if IWL_DL_IO is set */
  884. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  885. i + RTC_INST_LOWER_BOUND);
  886. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  887. if (val != le32_to_cpu(*image)) {
  888. ret = -EIO;
  889. errcnt++;
  890. if (errcnt >= 3)
  891. break;
  892. }
  893. }
  894. iwl_release_nic_access(priv);
  895. return ret;
  896. }
  897. /**
  898. * iwlcore_verify_inst_full - verify runtime uCode image in card vs. host,
  899. * looking at all data.
  900. */
  901. static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 *image,
  902. u32 len)
  903. {
  904. u32 val;
  905. u32 save_len = len;
  906. int ret = 0;
  907. u32 errcnt;
  908. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  909. ret = iwl_grab_nic_access(priv);
  910. if (ret)
  911. return ret;
  912. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
  913. errcnt = 0;
  914. for (; len > 0; len -= sizeof(u32), image++) {
  915. /* read data comes through single port, auto-incr addr */
  916. /* NOTE: Use the debugless read so we don't flood kernel log
  917. * if IWL_DL_IO is set */
  918. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  919. if (val != le32_to_cpu(*image)) {
  920. IWL_ERROR("uCode INST section is invalid at "
  921. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  922. save_len - len, val, le32_to_cpu(*image));
  923. ret = -EIO;
  924. errcnt++;
  925. if (errcnt >= 20)
  926. break;
  927. }
  928. }
  929. iwl_release_nic_access(priv);
  930. if (!errcnt)
  931. IWL_DEBUG_INFO
  932. ("ucode image in INSTRUCTION memory is good\n");
  933. return ret;
  934. }
  935. /**
  936. * iwl_verify_ucode - determine which instruction image is in SRAM,
  937. * and verify its contents
  938. */
  939. int iwl_verify_ucode(struct iwl_priv *priv)
  940. {
  941. __le32 *image;
  942. u32 len;
  943. int ret;
  944. /* Try bootstrap */
  945. image = (__le32 *)priv->ucode_boot.v_addr;
  946. len = priv->ucode_boot.len;
  947. ret = iwlcore_verify_inst_sparse(priv, image, len);
  948. if (!ret) {
  949. IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
  950. return 0;
  951. }
  952. /* Try initialize */
  953. image = (__le32 *)priv->ucode_init.v_addr;
  954. len = priv->ucode_init.len;
  955. ret = iwlcore_verify_inst_sparse(priv, image, len);
  956. if (!ret) {
  957. IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
  958. return 0;
  959. }
  960. /* Try runtime/protocol */
  961. image = (__le32 *)priv->ucode_code.v_addr;
  962. len = priv->ucode_code.len;
  963. ret = iwlcore_verify_inst_sparse(priv, image, len);
  964. if (!ret) {
  965. IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
  966. return 0;
  967. }
  968. IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  969. /* Since nothing seems to match, show first several data entries in
  970. * instruction SRAM, so maybe visual inspection will give a clue.
  971. * Selection of bootstrap image (vs. other images) is arbitrary. */
  972. image = (__le32 *)priv->ucode_boot.v_addr;
  973. len = priv->ucode_boot.len;
  974. ret = iwl_verify_inst_full(priv, image, len);
  975. return ret;
  976. }
  977. EXPORT_SYMBOL(iwl_verify_ucode);
  978. static const char *desc_lookup_text[] = {
  979. "OK",
  980. "FAIL",
  981. "BAD_PARAM",
  982. "BAD_CHECKSUM",
  983. "NMI_INTERRUPT_WDG",
  984. "SYSASSERT",
  985. "FATAL_ERROR",
  986. "BAD_COMMAND",
  987. "HW_ERROR_TUNE_LOCK",
  988. "HW_ERROR_TEMPERATURE",
  989. "ILLEGAL_CHAN_FREQ",
  990. "VCC_NOT_STABLE",
  991. "FH_ERROR",
  992. "NMI_INTERRUPT_HOST",
  993. "NMI_INTERRUPT_ACTION_PT",
  994. "NMI_INTERRUPT_UNKNOWN",
  995. "UCODE_VERSION_MISMATCH",
  996. "HW_ERROR_ABS_LOCK",
  997. "HW_ERROR_CAL_LOCK_FAIL",
  998. "NMI_INTERRUPT_INST_ACTION_PT",
  999. "NMI_INTERRUPT_DATA_ACTION_PT",
  1000. "NMI_TRM_HW_ER",
  1001. "NMI_INTERRUPT_TRM",
  1002. "NMI_INTERRUPT_BREAK_POINT"
  1003. "DEBUG_0",
  1004. "DEBUG_1",
  1005. "DEBUG_2",
  1006. "DEBUG_3",
  1007. "UNKNOWN"
  1008. };
  1009. static const char *desc_lookup(int i)
  1010. {
  1011. int max = ARRAY_SIZE(desc_lookup_text) - 1;
  1012. if (i < 0 || i > max)
  1013. i = max;
  1014. return desc_lookup_text[i];
  1015. }
  1016. #define ERROR_START_OFFSET (1 * sizeof(u32))
  1017. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  1018. void iwl_dump_nic_error_log(struct iwl_priv *priv)
  1019. {
  1020. u32 data2, line;
  1021. u32 desc, time, count, base, data1;
  1022. u32 blink1, blink2, ilink1, ilink2;
  1023. int ret;
  1024. if (priv->ucode_type == UCODE_INIT)
  1025. base = le32_to_cpu(priv->card_alive_init.error_event_table_ptr);
  1026. else
  1027. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  1028. if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
  1029. IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
  1030. return;
  1031. }
  1032. ret = iwl_grab_nic_access(priv);
  1033. if (ret) {
  1034. IWL_WARNING("Can not read from adapter at this time.\n");
  1035. return;
  1036. }
  1037. count = iwl_read_targ_mem(priv, base);
  1038. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  1039. IWL_ERROR("Start IWL Error Log Dump:\n");
  1040. IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
  1041. }
  1042. desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
  1043. blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
  1044. blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
  1045. ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
  1046. ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
  1047. data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
  1048. data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
  1049. line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
  1050. time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
  1051. IWL_ERROR("Desc Time "
  1052. "data1 data2 line\n");
  1053. IWL_ERROR("%-28s (#%02d) %010u 0x%08X 0x%08X %u\n",
  1054. desc_lookup(desc), desc, time, data1, data2, line);
  1055. IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
  1056. IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
  1057. ilink1, ilink2);
  1058. iwl_release_nic_access(priv);
  1059. }
  1060. EXPORT_SYMBOL(iwl_dump_nic_error_log);
  1061. #define EVENT_START_OFFSET (4 * sizeof(u32))
  1062. /**
  1063. * iwl_print_event_log - Dump error event log to syslog
  1064. *
  1065. * NOTE: Must be called with iwl_grab_nic_access() already obtained!
  1066. */
  1067. static void iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
  1068. u32 num_events, u32 mode)
  1069. {
  1070. u32 i;
  1071. u32 base; /* SRAM byte address of event log header */
  1072. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  1073. u32 ptr; /* SRAM byte address of log data */
  1074. u32 ev, time, data; /* event log data */
  1075. if (num_events == 0)
  1076. return;
  1077. if (priv->ucode_type == UCODE_INIT)
  1078. base = le32_to_cpu(priv->card_alive_init.log_event_table_ptr);
  1079. else
  1080. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1081. if (mode == 0)
  1082. event_size = 2 * sizeof(u32);
  1083. else
  1084. event_size = 3 * sizeof(u32);
  1085. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  1086. /* "time" is actually "data" for mode 0 (no timestamp).
  1087. * place event id # at far right for easier visual parsing. */
  1088. for (i = 0; i < num_events; i++) {
  1089. ev = iwl_read_targ_mem(priv, ptr);
  1090. ptr += sizeof(u32);
  1091. time = iwl_read_targ_mem(priv, ptr);
  1092. ptr += sizeof(u32);
  1093. if (mode == 0) {
  1094. /* data, ev */
  1095. IWL_ERROR("EVT_LOG:0x%08x:%04u\n", time, ev);
  1096. } else {
  1097. data = iwl_read_targ_mem(priv, ptr);
  1098. ptr += sizeof(u32);
  1099. IWL_ERROR("EVT_LOGT:%010u:0x%08x:%04u\n",
  1100. time, data, ev);
  1101. }
  1102. }
  1103. }
  1104. void iwl_dump_nic_event_log(struct iwl_priv *priv)
  1105. {
  1106. int ret;
  1107. u32 base; /* SRAM byte address of event log header */
  1108. u32 capacity; /* event log capacity in # entries */
  1109. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  1110. u32 num_wraps; /* # times uCode wrapped to top of log */
  1111. u32 next_entry; /* index of next entry to be written by uCode */
  1112. u32 size; /* # entries that we'll print */
  1113. if (priv->ucode_type == UCODE_INIT)
  1114. base = le32_to_cpu(priv->card_alive_init.log_event_table_ptr);
  1115. else
  1116. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1117. if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
  1118. IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
  1119. return;
  1120. }
  1121. ret = iwl_grab_nic_access(priv);
  1122. if (ret) {
  1123. IWL_WARNING("Can not read from adapter at this time.\n");
  1124. return;
  1125. }
  1126. /* event log header */
  1127. capacity = iwl_read_targ_mem(priv, base);
  1128. mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
  1129. num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
  1130. next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
  1131. size = num_wraps ? capacity : next_entry;
  1132. /* bail out if nothing in log */
  1133. if (size == 0) {
  1134. IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
  1135. iwl_release_nic_access(priv);
  1136. return;
  1137. }
  1138. IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
  1139. size, num_wraps);
  1140. /* if uCode has wrapped back to top of log, start at the oldest entry,
  1141. * i.e the next one that uCode would fill. */
  1142. if (num_wraps)
  1143. iwl_print_event_log(priv, next_entry,
  1144. capacity - next_entry, mode);
  1145. /* (then/else) start at top of log */
  1146. iwl_print_event_log(priv, 0, next_entry, mode);
  1147. iwl_release_nic_access(priv);
  1148. }
  1149. EXPORT_SYMBOL(iwl_dump_nic_event_log);
  1150. void iwl_rf_kill_ct_config(struct iwl_priv *priv)
  1151. {
  1152. struct iwl_ct_kill_config cmd;
  1153. unsigned long flags;
  1154. int ret = 0;
  1155. spin_lock_irqsave(&priv->lock, flags);
  1156. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  1157. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  1158. spin_unlock_irqrestore(&priv->lock, flags);
  1159. cmd.critical_temperature_R =
  1160. cpu_to_le32(priv->hw_params.ct_kill_threshold);
  1161. ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
  1162. sizeof(cmd), &cmd);
  1163. if (ret)
  1164. IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
  1165. else
  1166. IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded, "
  1167. "critical temperature is %d\n",
  1168. cmd.critical_temperature_R);
  1169. }
  1170. EXPORT_SYMBOL(iwl_rf_kill_ct_config);
  1171. /*
  1172. * CARD_STATE_CMD
  1173. *
  1174. * Use: Sets the device's internal card state to enable, disable, or halt
  1175. *
  1176. * When in the 'enable' state the card operates as normal.
  1177. * When in the 'disable' state, the card enters into a low power mode.
  1178. * When in the 'halt' state, the card is shut down and must be fully
  1179. * restarted to come back on.
  1180. */
  1181. static int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
  1182. {
  1183. struct iwl_host_cmd cmd = {
  1184. .id = REPLY_CARD_STATE_CMD,
  1185. .len = sizeof(u32),
  1186. .data = &flags,
  1187. .meta.flags = meta_flag,
  1188. };
  1189. return iwl_send_cmd(priv, &cmd);
  1190. }
  1191. void iwl_radio_kill_sw_disable_radio(struct iwl_priv *priv)
  1192. {
  1193. unsigned long flags;
  1194. if (test_bit(STATUS_RF_KILL_SW, &priv->status))
  1195. return;
  1196. IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO OFF\n");
  1197. iwl_scan_cancel(priv);
  1198. /* FIXME: This is a workaround for AP */
  1199. if (priv->iw_mode != NL80211_IFTYPE_AP) {
  1200. spin_lock_irqsave(&priv->lock, flags);
  1201. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  1202. CSR_UCODE_SW_BIT_RFKILL);
  1203. spin_unlock_irqrestore(&priv->lock, flags);
  1204. /* call the host command only if no hw rf-kill set */
  1205. if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
  1206. iwl_is_ready(priv))
  1207. iwl_send_card_state(priv,
  1208. CARD_STATE_CMD_DISABLE, 0);
  1209. set_bit(STATUS_RF_KILL_SW, &priv->status);
  1210. /* make sure mac80211 stop sending Tx frame */
  1211. if (priv->mac80211_registered)
  1212. ieee80211_stop_queues(priv->hw);
  1213. }
  1214. }
  1215. EXPORT_SYMBOL(iwl_radio_kill_sw_disable_radio);
  1216. int iwl_radio_kill_sw_enable_radio(struct iwl_priv *priv)
  1217. {
  1218. unsigned long flags;
  1219. if (!test_bit(STATUS_RF_KILL_SW, &priv->status))
  1220. return 0;
  1221. IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO ON\n");
  1222. spin_lock_irqsave(&priv->lock, flags);
  1223. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1224. /* If the driver is up it will receive CARD_STATE_NOTIFICATION
  1225. * notification where it will clear SW rfkill status.
  1226. * Setting it here would break the handler. Only if the
  1227. * interface is down we can set here since we don't
  1228. * receive any further notification.
  1229. */
  1230. if (!priv->is_open)
  1231. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  1232. spin_unlock_irqrestore(&priv->lock, flags);
  1233. /* wake up ucode */
  1234. msleep(10);
  1235. spin_lock_irqsave(&priv->lock, flags);
  1236. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  1237. if (!iwl_grab_nic_access(priv))
  1238. iwl_release_nic_access(priv);
  1239. spin_unlock_irqrestore(&priv->lock, flags);
  1240. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  1241. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  1242. "disabled by HW switch\n");
  1243. return 0;
  1244. }
  1245. /* If the driver is already loaded, it will receive
  1246. * CARD_STATE_NOTIFICATION notifications and the handler will
  1247. * call restart to reload the driver.
  1248. */
  1249. return 1;
  1250. }
  1251. EXPORT_SYMBOL(iwl_radio_kill_sw_enable_radio);