iwl-core.c 41 KB

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