iwl-core.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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 <linux/version.h>
  31. #include <net/mac80211.h>
  32. struct iwl_priv; /* FIXME: remove */
  33. #include "iwl-debug.h"
  34. #include "iwl-eeprom.h"
  35. #include "iwl-dev.h" /* FIXME: remove */
  36. #include "iwl-core.h"
  37. #include "iwl-io.h"
  38. #include "iwl-rfkill.h"
  39. #include "iwl-power.h"
  40. MODULE_DESCRIPTION("iwl core");
  41. MODULE_VERSION(IWLWIFI_VERSION);
  42. MODULE_AUTHOR(DRV_COPYRIGHT);
  43. MODULE_LICENSE("GPL");
  44. #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
  45. [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
  46. IWL_RATE_SISO_##s##M_PLCP, \
  47. IWL_RATE_MIMO2_##s##M_PLCP,\
  48. IWL_RATE_MIMO3_##s##M_PLCP,\
  49. IWL_RATE_##r##M_IEEE, \
  50. IWL_RATE_##ip##M_INDEX, \
  51. IWL_RATE_##in##M_INDEX, \
  52. IWL_RATE_##rp##M_INDEX, \
  53. IWL_RATE_##rn##M_INDEX, \
  54. IWL_RATE_##pp##M_INDEX, \
  55. IWL_RATE_##np##M_INDEX }
  56. /*
  57. * Parameter order:
  58. * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
  59. *
  60. * If there isn't a valid next or previous rate then INV is used which
  61. * maps to IWL_RATE_INVALID
  62. *
  63. */
  64. const struct iwl4965_rate_info iwl4965_rates[IWL_RATE_COUNT] = {
  65. IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
  66. IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
  67. IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
  68. IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
  69. IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
  70. IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
  71. IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
  72. IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
  73. IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
  74. IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
  75. IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
  76. IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
  77. IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
  78. /* FIXME:RS: ^^ should be INV (legacy) */
  79. };
  80. EXPORT_SYMBOL(iwl4965_rates);
  81. /* This function both allocates and initializes hw and priv. */
  82. struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg,
  83. struct ieee80211_ops *hw_ops)
  84. {
  85. struct iwl_priv *priv;
  86. /* mac80211 allocates memory for this device instance, including
  87. * space for this driver's private structure */
  88. struct ieee80211_hw *hw =
  89. ieee80211_alloc_hw(sizeof(struct iwl_priv), hw_ops);
  90. if (hw == NULL) {
  91. IWL_ERROR("Can not allocate network device\n");
  92. goto out;
  93. }
  94. priv = hw->priv;
  95. priv->hw = hw;
  96. out:
  97. return hw;
  98. }
  99. EXPORT_SYMBOL(iwl_alloc_all);
  100. void iwl_hw_detect(struct iwl_priv *priv)
  101. {
  102. priv->hw_rev = _iwl_read32(priv, CSR_HW_REV);
  103. priv->hw_wa_rev = _iwl_read32(priv, CSR_HW_REV_WA_REG);
  104. pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &priv->rev_id);
  105. }
  106. EXPORT_SYMBOL(iwl_hw_detect);
  107. /* Tell nic where to find the "keep warm" buffer */
  108. int iwl_kw_init(struct iwl_priv *priv)
  109. {
  110. unsigned long flags;
  111. int ret;
  112. spin_lock_irqsave(&priv->lock, flags);
  113. ret = iwl_grab_nic_access(priv);
  114. if (ret)
  115. goto out;
  116. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG,
  117. priv->kw.dma_addr >> 4);
  118. iwl_release_nic_access(priv);
  119. out:
  120. spin_unlock_irqrestore(&priv->lock, flags);
  121. return ret;
  122. }
  123. int iwl_kw_alloc(struct iwl_priv *priv)
  124. {
  125. struct pci_dev *dev = priv->pci_dev;
  126. struct iwl_kw *kw = &priv->kw;
  127. kw->size = IWL_KW_SIZE;
  128. kw->v_addr = pci_alloc_consistent(dev, kw->size, &kw->dma_addr);
  129. if (!kw->v_addr)
  130. return -ENOMEM;
  131. return 0;
  132. }
  133. /**
  134. * iwl_kw_free - Free the "keep warm" buffer
  135. */
  136. void iwl_kw_free(struct iwl_priv *priv)
  137. {
  138. struct pci_dev *dev = priv->pci_dev;
  139. struct iwl_kw *kw = &priv->kw;
  140. if (kw->v_addr) {
  141. pci_free_consistent(dev, kw->size, kw->v_addr, kw->dma_addr);
  142. memset(kw, 0, sizeof(*kw));
  143. }
  144. }
  145. int iwl_hw_nic_init(struct iwl_priv *priv)
  146. {
  147. unsigned long flags;
  148. struct iwl_rx_queue *rxq = &priv->rxq;
  149. int ret;
  150. /* nic_init */
  151. spin_lock_irqsave(&priv->lock, flags);
  152. priv->cfg->ops->lib->apm_ops.init(priv);
  153. iwl_write32(priv, CSR_INT_COALESCING, 512 / 32);
  154. spin_unlock_irqrestore(&priv->lock, flags);
  155. ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
  156. priv->cfg->ops->lib->apm_ops.config(priv);
  157. /* Allocate the RX queue, or reset if it is already allocated */
  158. if (!rxq->bd) {
  159. ret = iwl_rx_queue_alloc(priv);
  160. if (ret) {
  161. IWL_ERROR("Unable to initialize Rx queue\n");
  162. return -ENOMEM;
  163. }
  164. } else
  165. iwl_rx_queue_reset(priv, rxq);
  166. iwl_rx_replenish(priv);
  167. iwl_rx_init(priv, rxq);
  168. spin_lock_irqsave(&priv->lock, flags);
  169. rxq->need_update = 1;
  170. iwl_rx_queue_update_write_ptr(priv, rxq);
  171. spin_unlock_irqrestore(&priv->lock, flags);
  172. /* Allocate and init all Tx and Command queues */
  173. ret = iwl_txq_ctx_reset(priv);
  174. if (ret)
  175. return ret;
  176. set_bit(STATUS_INIT, &priv->status);
  177. return 0;
  178. }
  179. EXPORT_SYMBOL(iwl_hw_nic_init);
  180. /**
  181. * iwlcore_clear_stations_table - Clear the driver's station table
  182. *
  183. * NOTE: This does not clear or otherwise alter the device's station table.
  184. */
  185. void iwlcore_clear_stations_table(struct iwl_priv *priv)
  186. {
  187. unsigned long flags;
  188. spin_lock_irqsave(&priv->sta_lock, flags);
  189. priv->num_stations = 0;
  190. memset(priv->stations, 0, sizeof(priv->stations));
  191. spin_unlock_irqrestore(&priv->sta_lock, flags);
  192. }
  193. EXPORT_SYMBOL(iwlcore_clear_stations_table);
  194. void iwl_reset_qos(struct iwl_priv *priv)
  195. {
  196. u16 cw_min = 15;
  197. u16 cw_max = 1023;
  198. u8 aifs = 2;
  199. u8 is_legacy = 0;
  200. unsigned long flags;
  201. int i;
  202. spin_lock_irqsave(&priv->lock, flags);
  203. priv->qos_data.qos_active = 0;
  204. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
  205. if (priv->qos_data.qos_enable)
  206. priv->qos_data.qos_active = 1;
  207. if (!(priv->active_rate & 0xfff0)) {
  208. cw_min = 31;
  209. is_legacy = 1;
  210. }
  211. } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  212. if (priv->qos_data.qos_enable)
  213. priv->qos_data.qos_active = 1;
  214. } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
  215. cw_min = 31;
  216. is_legacy = 1;
  217. }
  218. if (priv->qos_data.qos_active)
  219. aifs = 3;
  220. priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
  221. priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
  222. priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
  223. priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
  224. priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
  225. if (priv->qos_data.qos_active) {
  226. i = 1;
  227. priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
  228. priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
  229. priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
  230. priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
  231. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  232. i = 2;
  233. priv->qos_data.def_qos_parm.ac[i].cw_min =
  234. cpu_to_le16((cw_min + 1) / 2 - 1);
  235. priv->qos_data.def_qos_parm.ac[i].cw_max =
  236. cpu_to_le16(cw_max);
  237. priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
  238. if (is_legacy)
  239. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  240. cpu_to_le16(6016);
  241. else
  242. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  243. cpu_to_le16(3008);
  244. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  245. i = 3;
  246. priv->qos_data.def_qos_parm.ac[i].cw_min =
  247. cpu_to_le16((cw_min + 1) / 4 - 1);
  248. priv->qos_data.def_qos_parm.ac[i].cw_max =
  249. cpu_to_le16((cw_max + 1) / 2 - 1);
  250. priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
  251. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  252. if (is_legacy)
  253. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  254. cpu_to_le16(3264);
  255. else
  256. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  257. cpu_to_le16(1504);
  258. } else {
  259. for (i = 1; i < 4; i++) {
  260. priv->qos_data.def_qos_parm.ac[i].cw_min =
  261. cpu_to_le16(cw_min);
  262. priv->qos_data.def_qos_parm.ac[i].cw_max =
  263. cpu_to_le16(cw_max);
  264. priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
  265. priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
  266. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  267. }
  268. }
  269. IWL_DEBUG_QOS("set QoS to default \n");
  270. spin_unlock_irqrestore(&priv->lock, flags);
  271. }
  272. EXPORT_SYMBOL(iwl_reset_qos);
  273. #ifdef CONFIG_IWL4965_HT
  274. #define MAX_BIT_RATE_40_MHZ 0x96; /* 150 Mbps */
  275. #define MAX_BIT_RATE_20_MHZ 0x48; /* 72 Mbps */
  276. static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
  277. struct ieee80211_ht_info *ht_info,
  278. enum ieee80211_band band)
  279. {
  280. u16 max_bit_rate = 0;
  281. u8 rx_chains_num = priv->hw_params.rx_chains_num;
  282. u8 tx_chains_num = priv->hw_params.tx_chains_num;
  283. ht_info->cap = 0;
  284. memset(ht_info->supp_mcs_set, 0, 16);
  285. ht_info->ht_supported = 1;
  286. ht_info->cap |= (u16)IEEE80211_HT_CAP_GRN_FLD;
  287. ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_20;
  288. ht_info->cap |= (u16)(IEEE80211_HT_CAP_MIMO_PS &
  289. (IWL_MIMO_PS_NONE << 2));
  290. max_bit_rate = MAX_BIT_RATE_20_MHZ;
  291. if (priv->hw_params.fat_channel & BIT(band)) {
  292. ht_info->cap |= (u16)IEEE80211_HT_CAP_SUP_WIDTH;
  293. ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_40;
  294. ht_info->supp_mcs_set[4] = 0x01;
  295. max_bit_rate = MAX_BIT_RATE_40_MHZ;
  296. }
  297. if (priv->cfg->mod_params->amsdu_size_8K)
  298. ht_info->cap |= (u16)IEEE80211_HT_CAP_MAX_AMSDU;
  299. ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
  300. ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
  301. ht_info->supp_mcs_set[0] = 0xFF;
  302. if (rx_chains_num >= 2)
  303. ht_info->supp_mcs_set[1] = 0xFF;
  304. if (rx_chains_num >= 3)
  305. ht_info->supp_mcs_set[2] = 0xFF;
  306. /* Highest supported Rx data rate */
  307. max_bit_rate *= rx_chains_num;
  308. ht_info->supp_mcs_set[10] = (u8)(max_bit_rate & 0x00FF);
  309. ht_info->supp_mcs_set[11] = (u8)((max_bit_rate & 0xFF00) >> 8);
  310. /* Tx MCS capabilities */
  311. ht_info->supp_mcs_set[12] = IEEE80211_HT_CAP_MCS_TX_DEFINED;
  312. if (tx_chains_num != rx_chains_num) {
  313. ht_info->supp_mcs_set[12] |= IEEE80211_HT_CAP_MCS_TX_RX_DIFF;
  314. ht_info->supp_mcs_set[12] |= ((tx_chains_num - 1) << 2);
  315. }
  316. }
  317. #else
  318. static inline void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
  319. struct ieee80211_ht_info *ht_info,
  320. enum ieee80211_band band)
  321. {
  322. }
  323. #endif /* CONFIG_IWL4965_HT */
  324. static void iwlcore_init_hw_rates(struct iwl_priv *priv,
  325. struct ieee80211_rate *rates)
  326. {
  327. int i;
  328. for (i = 0; i < IWL_RATE_COUNT; i++) {
  329. rates[i].bitrate = iwl4965_rates[i].ieee * 5;
  330. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  331. rates[i].hw_value_short = i;
  332. rates[i].flags = 0;
  333. if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  334. /*
  335. * If CCK != 1M then set short preamble rate flag.
  336. */
  337. rates[i].flags |=
  338. (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
  339. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  340. }
  341. }
  342. }
  343. /**
  344. * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
  345. */
  346. static int iwlcore_init_geos(struct iwl_priv *priv)
  347. {
  348. struct iwl_channel_info *ch;
  349. struct ieee80211_supported_band *sband;
  350. struct ieee80211_channel *channels;
  351. struct ieee80211_channel *geo_ch;
  352. struct ieee80211_rate *rates;
  353. int i = 0;
  354. if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  355. priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  356. IWL_DEBUG_INFO("Geography modes already initialized.\n");
  357. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  358. return 0;
  359. }
  360. channels = kzalloc(sizeof(struct ieee80211_channel) *
  361. priv->channel_count, GFP_KERNEL);
  362. if (!channels)
  363. return -ENOMEM;
  364. rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
  365. GFP_KERNEL);
  366. if (!rates) {
  367. kfree(channels);
  368. return -ENOMEM;
  369. }
  370. /* 5.2GHz channels start after the 2.4GHz channels */
  371. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  372. sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
  373. /* just OFDM */
  374. sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
  375. sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
  376. iwlcore_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
  377. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  378. sband->channels = channels;
  379. /* OFDM & CCK */
  380. sband->bitrates = rates;
  381. sband->n_bitrates = IWL_RATE_COUNT;
  382. iwlcore_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
  383. priv->ieee_channels = channels;
  384. priv->ieee_rates = rates;
  385. iwlcore_init_hw_rates(priv, rates);
  386. for (i = 0; i < priv->channel_count; i++) {
  387. ch = &priv->channel_info[i];
  388. /* FIXME: might be removed if scan is OK */
  389. if (!is_channel_valid(ch))
  390. continue;
  391. if (is_channel_a_band(ch))
  392. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  393. else
  394. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  395. geo_ch = &sband->channels[sband->n_channels++];
  396. geo_ch->center_freq =
  397. ieee80211_channel_to_frequency(ch->channel);
  398. geo_ch->max_power = ch->max_power_avg;
  399. geo_ch->max_antenna_gain = 0xff;
  400. geo_ch->hw_value = ch->channel;
  401. if (is_channel_valid(ch)) {
  402. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  403. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  404. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  405. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  406. if (ch->flags & EEPROM_CHANNEL_RADAR)
  407. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  408. if (ch->max_power_avg > priv->max_channel_txpower_limit)
  409. priv->max_channel_txpower_limit =
  410. ch->max_power_avg;
  411. } else {
  412. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  413. }
  414. /* Save flags for reg domain usage */
  415. geo_ch->orig_flags = geo_ch->flags;
  416. IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
  417. ch->channel, geo_ch->center_freq,
  418. is_channel_a_band(ch) ? "5.2" : "2.4",
  419. geo_ch->flags & IEEE80211_CHAN_DISABLED ?
  420. "restricted" : "valid",
  421. geo_ch->flags);
  422. }
  423. if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
  424. priv->cfg->sku & IWL_SKU_A) {
  425. printk(KERN_INFO DRV_NAME
  426. ": Incorrectly detected BG card as ABG. Please send "
  427. "your PCI ID 0x%04X:0x%04X to maintainer.\n",
  428. priv->pci_dev->device, priv->pci_dev->subsystem_device);
  429. priv->cfg->sku &= ~IWL_SKU_A;
  430. }
  431. printk(KERN_INFO DRV_NAME
  432. ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  433. priv->bands[IEEE80211_BAND_2GHZ].n_channels,
  434. priv->bands[IEEE80211_BAND_5GHZ].n_channels);
  435. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  436. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  437. &priv->bands[IEEE80211_BAND_2GHZ];
  438. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  439. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  440. &priv->bands[IEEE80211_BAND_5GHZ];
  441. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  442. return 0;
  443. }
  444. /*
  445. * iwlcore_free_geos - undo allocations in iwlcore_init_geos
  446. */
  447. void iwlcore_free_geos(struct iwl_priv *priv)
  448. {
  449. kfree(priv->ieee_channels);
  450. kfree(priv->ieee_rates);
  451. clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
  452. }
  453. EXPORT_SYMBOL(iwlcore_free_geos);
  454. #ifdef CONFIG_IWL4965_HT
  455. static u8 is_single_rx_stream(struct iwl_priv *priv)
  456. {
  457. return !priv->current_ht_config.is_ht ||
  458. ((priv->current_ht_config.supp_mcs_set[1] == 0) &&
  459. (priv->current_ht_config.supp_mcs_set[2] == 0)) ||
  460. priv->ps_mode == IWL_MIMO_PS_STATIC;
  461. }
  462. static u8 iwl_is_channel_extension(struct iwl_priv *priv,
  463. enum ieee80211_band band,
  464. u16 channel, u8 extension_chan_offset)
  465. {
  466. const struct iwl_channel_info *ch_info;
  467. ch_info = iwl_get_channel_info(priv, band, channel);
  468. if (!is_channel_valid(ch_info))
  469. return 0;
  470. if (extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_NONE)
  471. return 0;
  472. if ((ch_info->fat_extension_channel == extension_chan_offset) ||
  473. (ch_info->fat_extension_channel == HT_IE_EXT_CHANNEL_MAX))
  474. return 1;
  475. return 0;
  476. }
  477. u8 iwl_is_fat_tx_allowed(struct iwl_priv *priv,
  478. struct ieee80211_ht_info *sta_ht_inf)
  479. {
  480. struct iwl_ht_info *iwl_ht_conf = &priv->current_ht_config;
  481. if ((!iwl_ht_conf->is_ht) ||
  482. (iwl_ht_conf->supported_chan_width != IWL_CHANNEL_WIDTH_40MHZ) ||
  483. (iwl_ht_conf->extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_NONE))
  484. return 0;
  485. if (sta_ht_inf) {
  486. if ((!sta_ht_inf->ht_supported) ||
  487. (!(sta_ht_inf->cap & IEEE80211_HT_CAP_SUP_WIDTH)))
  488. return 0;
  489. }
  490. return iwl_is_channel_extension(priv, priv->band,
  491. iwl_ht_conf->control_channel,
  492. iwl_ht_conf->extension_chan_offset);
  493. }
  494. EXPORT_SYMBOL(iwl_is_fat_tx_allowed);
  495. void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_info *ht_info)
  496. {
  497. struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
  498. u32 val;
  499. if (!ht_info->is_ht)
  500. return;
  501. /* Set up channel bandwidth: 20 MHz only, or 20/40 mixed if fat ok */
  502. if (iwl_is_fat_tx_allowed(priv, NULL))
  503. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  504. else
  505. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
  506. RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
  507. if (le16_to_cpu(rxon->channel) != ht_info->control_channel) {
  508. IWL_DEBUG_ASSOC("control diff than current %d %d\n",
  509. le16_to_cpu(rxon->channel),
  510. ht_info->control_channel);
  511. rxon->channel = cpu_to_le16(ht_info->control_channel);
  512. return;
  513. }
  514. /* Note: control channel is opposite of extension channel */
  515. switch (ht_info->extension_chan_offset) {
  516. case IWL_EXT_CHANNEL_OFFSET_ABOVE:
  517. rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  518. break;
  519. case IWL_EXT_CHANNEL_OFFSET_BELOW:
  520. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  521. break;
  522. case IWL_EXT_CHANNEL_OFFSET_NONE:
  523. default:
  524. rxon->flags &= ~RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  525. break;
  526. }
  527. val = ht_info->ht_protection;
  528. rxon->flags |= cpu_to_le32(val << RXON_FLG_HT_OPERATING_MODE_POS);
  529. iwl_set_rxon_chain(priv);
  530. IWL_DEBUG_ASSOC("supported HT rate 0x%X 0x%X 0x%X "
  531. "rxon flags 0x%X operation mode :0x%X "
  532. "extension channel offset 0x%x "
  533. "control chan %d\n",
  534. ht_info->supp_mcs_set[0],
  535. ht_info->supp_mcs_set[1],
  536. ht_info->supp_mcs_set[2],
  537. le32_to_cpu(rxon->flags), ht_info->ht_protection,
  538. ht_info->extension_chan_offset,
  539. ht_info->control_channel);
  540. return;
  541. }
  542. EXPORT_SYMBOL(iwl_set_rxon_ht);
  543. #else
  544. static inline u8 is_single_rx_stream(struct iwl_priv *priv)
  545. {
  546. return 1;
  547. }
  548. #endif /*CONFIG_IWL4965_HT */
  549. /*
  550. * Determine how many receiver/antenna chains to use.
  551. * More provides better reception via diversity. Fewer saves power.
  552. * MIMO (dual stream) requires at least 2, but works better with 3.
  553. * This does not determine *which* chains to use, just how many.
  554. */
  555. static int iwlcore_get_rx_chain_counter(struct iwl_priv *priv,
  556. u8 *idle_state, u8 *rx_state)
  557. {
  558. u8 is_single = is_single_rx_stream(priv);
  559. u8 is_cam = test_bit(STATUS_POWER_PMI, &priv->status) ? 0 : 1;
  560. /* # of Rx chains to use when expecting MIMO. */
  561. if (is_single || (!is_cam && (priv->ps_mode == IWL_MIMO_PS_STATIC)))
  562. *rx_state = 2;
  563. else
  564. *rx_state = 3;
  565. /* # Rx chains when idling and maybe trying to save power */
  566. switch (priv->ps_mode) {
  567. case IWL_MIMO_PS_STATIC:
  568. case IWL_MIMO_PS_DYNAMIC:
  569. *idle_state = (is_cam) ? 2 : 1;
  570. break;
  571. case IWL_MIMO_PS_NONE:
  572. *idle_state = (is_cam) ? *rx_state : 1;
  573. break;
  574. default:
  575. *idle_state = 1;
  576. break;
  577. }
  578. return 0;
  579. }
  580. /**
  581. * iwl_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  582. *
  583. * Selects how many and which Rx receivers/antennas/chains to use.
  584. * This should not be used for scan command ... it puts data in wrong place.
  585. */
  586. void iwl_set_rxon_chain(struct iwl_priv *priv)
  587. {
  588. u8 is_single = is_single_rx_stream(priv);
  589. u8 idle_state, rx_state;
  590. priv->staging_rxon.rx_chain = 0;
  591. rx_state = idle_state = 3;
  592. /* Tell uCode which antennas are actually connected.
  593. * Before first association, we assume all antennas are connected.
  594. * Just after first association, iwl_chain_noise_calibration()
  595. * checks which antennas actually *are* connected. */
  596. priv->staging_rxon.rx_chain |=
  597. cpu_to_le16(priv->hw_params.valid_rx_ant <<
  598. RXON_RX_CHAIN_VALID_POS);
  599. /* How many receivers should we use? */
  600. iwlcore_get_rx_chain_counter(priv, &idle_state, &rx_state);
  601. priv->staging_rxon.rx_chain |=
  602. cpu_to_le16(rx_state << RXON_RX_CHAIN_MIMO_CNT_POS);
  603. priv->staging_rxon.rx_chain |=
  604. cpu_to_le16(idle_state << RXON_RX_CHAIN_CNT_POS);
  605. if (!is_single && (rx_state >= 2) &&
  606. !test_bit(STATUS_POWER_PMI, &priv->status))
  607. priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  608. else
  609. priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  610. IWL_DEBUG_ASSOC("rx chain %X\n", priv->staging_rxon.rx_chain);
  611. }
  612. EXPORT_SYMBOL(iwl_set_rxon_chain);
  613. /**
  614. * iwlcore_set_rxon_channel - Set the phymode and channel values in staging RXON
  615. * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
  616. * @channel: Any channel valid for the requested phymode
  617. * In addition to setting the staging RXON, priv->phymode is also set.
  618. *
  619. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  620. * in the staging RXON flag structure based on the phymode
  621. */
  622. int iwl_set_rxon_channel(struct iwl_priv *priv,
  623. enum ieee80211_band band,
  624. u16 channel)
  625. {
  626. if (!iwl_get_channel_info(priv, band, channel)) {
  627. IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
  628. channel, band);
  629. return -EINVAL;
  630. }
  631. if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
  632. (priv->band == band))
  633. return 0;
  634. priv->staging_rxon.channel = cpu_to_le16(channel);
  635. if (band == IEEE80211_BAND_5GHZ)
  636. priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
  637. else
  638. priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
  639. priv->band = band;
  640. IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
  641. return 0;
  642. }
  643. EXPORT_SYMBOL(iwl_set_rxon_channel);
  644. static void iwlcore_init_hw(struct iwl_priv *priv)
  645. {
  646. struct ieee80211_hw *hw = priv->hw;
  647. hw->rate_control_algorithm = "iwl-4965-rs";
  648. /* Tell mac80211 our characteristics */
  649. hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
  650. IEEE80211_HW_SIGNAL_DBM |
  651. IEEE80211_HW_NOISE_DBM;
  652. /* Default value; 4 EDCA QOS priorities */
  653. hw->queues = 4;
  654. #ifdef CONFIG_IWL4965_HT
  655. /* Enhanced value; more queues, to support 11n aggregation */
  656. hw->ampdu_queues = 12;
  657. #endif /* CONFIG_IWL4965_HT */
  658. }
  659. static int iwlcore_init_drv(struct iwl_priv *priv)
  660. {
  661. int ret;
  662. int i;
  663. priv->retry_rate = 1;
  664. priv->ibss_beacon = NULL;
  665. spin_lock_init(&priv->lock);
  666. spin_lock_init(&priv->power_data.lock);
  667. spin_lock_init(&priv->sta_lock);
  668. spin_lock_init(&priv->hcmd_lock);
  669. spin_lock_init(&priv->lq_mngr.lock);
  670. for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
  671. INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
  672. INIT_LIST_HEAD(&priv->free_frames);
  673. mutex_init(&priv->mutex);
  674. /* Clear the driver's (not device's) station table */
  675. iwlcore_clear_stations_table(priv);
  676. priv->data_retry_limit = -1;
  677. priv->ieee_channels = NULL;
  678. priv->ieee_rates = NULL;
  679. priv->band = IEEE80211_BAND_2GHZ;
  680. priv->iw_mode = IEEE80211_IF_TYPE_STA;
  681. priv->use_ant_b_for_management_frame = 1; /* start with ant B */
  682. priv->ps_mode = IWL_MIMO_PS_NONE;
  683. /* Choose which receivers/antennas to use */
  684. iwl_set_rxon_chain(priv);
  685. iwl_reset_qos(priv);
  686. priv->qos_data.qos_active = 0;
  687. priv->qos_data.qos_cap.val = 0;
  688. iwl_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
  689. priv->rates_mask = IWL_RATES_MASK;
  690. /* If power management is turned on, default to AC mode */
  691. priv->power_mode = IWL_POWER_AC;
  692. priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
  693. ret = iwl_init_channel_map(priv);
  694. if (ret) {
  695. IWL_ERROR("initializing regulatory failed: %d\n", ret);
  696. goto err;
  697. }
  698. ret = iwlcore_init_geos(priv);
  699. if (ret) {
  700. IWL_ERROR("initializing geos failed: %d\n", ret);
  701. goto err_free_channel_map;
  702. }
  703. ret = ieee80211_register_hw(priv->hw);
  704. if (ret) {
  705. IWL_ERROR("Failed to register network device (error %d)\n",
  706. ret);
  707. goto err_free_geos;
  708. }
  709. priv->hw->conf.beacon_int = 100;
  710. priv->mac80211_registered = 1;
  711. return 0;
  712. err_free_geos:
  713. iwlcore_free_geos(priv);
  714. err_free_channel_map:
  715. iwl_free_channel_map(priv);
  716. err:
  717. return ret;
  718. }
  719. int iwl_setup(struct iwl_priv *priv)
  720. {
  721. int ret = 0;
  722. iwlcore_init_hw(priv);
  723. ret = iwlcore_init_drv(priv);
  724. return ret;
  725. }
  726. EXPORT_SYMBOL(iwl_setup);
  727. /* Low level driver call this function to update iwlcore with
  728. * driver status.
  729. */
  730. int iwlcore_low_level_notify(struct iwl_priv *priv,
  731. enum iwlcore_card_notify notify)
  732. {
  733. int ret;
  734. switch (notify) {
  735. case IWLCORE_INIT_EVT:
  736. ret = iwl_rfkill_init(priv);
  737. if (ret)
  738. IWL_ERROR("Unable to initialize RFKILL system. "
  739. "Ignoring error: %d\n", ret);
  740. iwl_power_initialize(priv);
  741. break;
  742. case IWLCORE_START_EVT:
  743. iwl_power_update_mode(priv, 1);
  744. break;
  745. case IWLCORE_STOP_EVT:
  746. break;
  747. case IWLCORE_REMOVE_EVT:
  748. iwl_rfkill_unregister(priv);
  749. break;
  750. }
  751. return 0;
  752. }
  753. EXPORT_SYMBOL(iwlcore_low_level_notify);
  754. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags)
  755. {
  756. u32 stat_flags = 0;
  757. struct iwl_host_cmd cmd = {
  758. .id = REPLY_STATISTICS_CMD,
  759. .meta.flags = flags,
  760. .len = sizeof(stat_flags),
  761. .data = (u8 *) &stat_flags,
  762. };
  763. return iwl_send_cmd(priv, &cmd);
  764. }
  765. EXPORT_SYMBOL(iwl_send_statistics_request);
  766. /**
  767. * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
  768. * using sample data 100 bytes apart. If these sample points are good,
  769. * it's a pretty good bet that everything between them is good, too.
  770. */
  771. static int iwlcore_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  772. {
  773. u32 val;
  774. int ret = 0;
  775. u32 errcnt = 0;
  776. u32 i;
  777. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  778. ret = iwl_grab_nic_access(priv);
  779. if (ret)
  780. return ret;
  781. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  782. /* read data comes through single port, auto-incr addr */
  783. /* NOTE: Use the debugless read so we don't flood kernel log
  784. * if IWL_DL_IO is set */
  785. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  786. i + RTC_INST_LOWER_BOUND);
  787. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  788. if (val != le32_to_cpu(*image)) {
  789. ret = -EIO;
  790. errcnt++;
  791. if (errcnt >= 3)
  792. break;
  793. }
  794. }
  795. iwl_release_nic_access(priv);
  796. return ret;
  797. }
  798. /**
  799. * iwlcore_verify_inst_full - verify runtime uCode image in card vs. host,
  800. * looking at all data.
  801. */
  802. static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 *image,
  803. u32 len)
  804. {
  805. u32 val;
  806. u32 save_len = len;
  807. int ret = 0;
  808. u32 errcnt;
  809. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  810. ret = iwl_grab_nic_access(priv);
  811. if (ret)
  812. return ret;
  813. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
  814. errcnt = 0;
  815. for (; len > 0; len -= sizeof(u32), image++) {
  816. /* read data comes through single port, auto-incr addr */
  817. /* NOTE: Use the debugless read so we don't flood kernel log
  818. * if IWL_DL_IO is set */
  819. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  820. if (val != le32_to_cpu(*image)) {
  821. IWL_ERROR("uCode INST section is invalid at "
  822. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  823. save_len - len, val, le32_to_cpu(*image));
  824. ret = -EIO;
  825. errcnt++;
  826. if (errcnt >= 20)
  827. break;
  828. }
  829. }
  830. iwl_release_nic_access(priv);
  831. if (!errcnt)
  832. IWL_DEBUG_INFO
  833. ("ucode image in INSTRUCTION memory is good\n");
  834. return ret;
  835. }
  836. /**
  837. * iwl_verify_ucode - determine which instruction image is in SRAM,
  838. * and verify its contents
  839. */
  840. int iwl_verify_ucode(struct iwl_priv *priv)
  841. {
  842. __le32 *image;
  843. u32 len;
  844. int ret;
  845. /* Try bootstrap */
  846. image = (__le32 *)priv->ucode_boot.v_addr;
  847. len = priv->ucode_boot.len;
  848. ret = iwlcore_verify_inst_sparse(priv, image, len);
  849. if (!ret) {
  850. IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
  851. return 0;
  852. }
  853. /* Try initialize */
  854. image = (__le32 *)priv->ucode_init.v_addr;
  855. len = priv->ucode_init.len;
  856. ret = iwlcore_verify_inst_sparse(priv, image, len);
  857. if (!ret) {
  858. IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
  859. return 0;
  860. }
  861. /* Try runtime/protocol */
  862. image = (__le32 *)priv->ucode_code.v_addr;
  863. len = priv->ucode_code.len;
  864. ret = iwlcore_verify_inst_sparse(priv, image, len);
  865. if (!ret) {
  866. IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
  867. return 0;
  868. }
  869. IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  870. /* Since nothing seems to match, show first several data entries in
  871. * instruction SRAM, so maybe visual inspection will give a clue.
  872. * Selection of bootstrap image (vs. other images) is arbitrary. */
  873. image = (__le32 *)priv->ucode_boot.v_addr;
  874. len = priv->ucode_boot.len;
  875. ret = iwl_verify_inst_full(priv, image, len);
  876. return ret;
  877. }
  878. EXPORT_SYMBOL(iwl_verify_ucode);