init.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/dma-mapping.h>
  18. #include <linux/slab.h>
  19. #include <linux/ath9k_platform.h>
  20. #include <linux/module.h>
  21. #include <linux/relay.h>
  22. #include <net/ieee80211_radiotap.h>
  23. #include "ath9k.h"
  24. struct ath9k_eeprom_ctx {
  25. struct completion complete;
  26. struct ath_hw *ah;
  27. };
  28. static char *dev_info = "ath9k";
  29. MODULE_AUTHOR("Atheros Communications");
  30. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  31. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  32. MODULE_LICENSE("Dual BSD/GPL");
  33. static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
  34. module_param_named(debug, ath9k_debug, uint, 0);
  35. MODULE_PARM_DESC(debug, "Debugging mask");
  36. int ath9k_modparam_nohwcrypt;
  37. module_param_named(nohwcrypt, ath9k_modparam_nohwcrypt, int, 0444);
  38. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");
  39. int led_blink;
  40. module_param_named(blink, led_blink, int, 0444);
  41. MODULE_PARM_DESC(blink, "Enable LED blink on activity");
  42. static int ath9k_btcoex_enable;
  43. module_param_named(btcoex_enable, ath9k_btcoex_enable, int, 0444);
  44. MODULE_PARM_DESC(btcoex_enable, "Enable wifi-BT coexistence");
  45. static int ath9k_enable_diversity;
  46. module_param_named(enable_diversity, ath9k_enable_diversity, int, 0444);
  47. MODULE_PARM_DESC(enable_diversity, "Enable Antenna diversity for AR9565");
  48. bool is_ath9k_unloaded;
  49. /* We use the hw_value as an index into our private channel structure */
  50. #define CHAN2G(_freq, _idx) { \
  51. .band = IEEE80211_BAND_2GHZ, \
  52. .center_freq = (_freq), \
  53. .hw_value = (_idx), \
  54. .max_power = 20, \
  55. }
  56. #define CHAN5G(_freq, _idx) { \
  57. .band = IEEE80211_BAND_5GHZ, \
  58. .center_freq = (_freq), \
  59. .hw_value = (_idx), \
  60. .max_power = 20, \
  61. }
  62. /* Some 2 GHz radios are actually tunable on 2312-2732
  63. * on 5 MHz steps, we support the channels which we know
  64. * we have calibration data for all cards though to make
  65. * this static */
  66. static const struct ieee80211_channel ath9k_2ghz_chantable[] = {
  67. CHAN2G(2412, 0), /* Channel 1 */
  68. CHAN2G(2417, 1), /* Channel 2 */
  69. CHAN2G(2422, 2), /* Channel 3 */
  70. CHAN2G(2427, 3), /* Channel 4 */
  71. CHAN2G(2432, 4), /* Channel 5 */
  72. CHAN2G(2437, 5), /* Channel 6 */
  73. CHAN2G(2442, 6), /* Channel 7 */
  74. CHAN2G(2447, 7), /* Channel 8 */
  75. CHAN2G(2452, 8), /* Channel 9 */
  76. CHAN2G(2457, 9), /* Channel 10 */
  77. CHAN2G(2462, 10), /* Channel 11 */
  78. CHAN2G(2467, 11), /* Channel 12 */
  79. CHAN2G(2472, 12), /* Channel 13 */
  80. CHAN2G(2484, 13), /* Channel 14 */
  81. };
  82. /* Some 5 GHz radios are actually tunable on XXXX-YYYY
  83. * on 5 MHz steps, we support the channels which we know
  84. * we have calibration data for all cards though to make
  85. * this static */
  86. static const struct ieee80211_channel ath9k_5ghz_chantable[] = {
  87. /* _We_ call this UNII 1 */
  88. CHAN5G(5180, 14), /* Channel 36 */
  89. CHAN5G(5200, 15), /* Channel 40 */
  90. CHAN5G(5220, 16), /* Channel 44 */
  91. CHAN5G(5240, 17), /* Channel 48 */
  92. /* _We_ call this UNII 2 */
  93. CHAN5G(5260, 18), /* Channel 52 */
  94. CHAN5G(5280, 19), /* Channel 56 */
  95. CHAN5G(5300, 20), /* Channel 60 */
  96. CHAN5G(5320, 21), /* Channel 64 */
  97. /* _We_ call this "Middle band" */
  98. CHAN5G(5500, 22), /* Channel 100 */
  99. CHAN5G(5520, 23), /* Channel 104 */
  100. CHAN5G(5540, 24), /* Channel 108 */
  101. CHAN5G(5560, 25), /* Channel 112 */
  102. CHAN5G(5580, 26), /* Channel 116 */
  103. CHAN5G(5600, 27), /* Channel 120 */
  104. CHAN5G(5620, 28), /* Channel 124 */
  105. CHAN5G(5640, 29), /* Channel 128 */
  106. CHAN5G(5660, 30), /* Channel 132 */
  107. CHAN5G(5680, 31), /* Channel 136 */
  108. CHAN5G(5700, 32), /* Channel 140 */
  109. /* _We_ call this UNII 3 */
  110. CHAN5G(5745, 33), /* Channel 149 */
  111. CHAN5G(5765, 34), /* Channel 153 */
  112. CHAN5G(5785, 35), /* Channel 157 */
  113. CHAN5G(5805, 36), /* Channel 161 */
  114. CHAN5G(5825, 37), /* Channel 165 */
  115. };
  116. /* Atheros hardware rate code addition for short premble */
  117. #define SHPCHECK(__hw_rate, __flags) \
  118. ((__flags & IEEE80211_RATE_SHORT_PREAMBLE) ? (__hw_rate | 0x04 ) : 0)
  119. #define RATE(_bitrate, _hw_rate, _flags) { \
  120. .bitrate = (_bitrate), \
  121. .flags = (_flags), \
  122. .hw_value = (_hw_rate), \
  123. .hw_value_short = (SHPCHECK(_hw_rate, _flags)) \
  124. }
  125. static struct ieee80211_rate ath9k_legacy_rates[] = {
  126. RATE(10, 0x1b, 0),
  127. RATE(20, 0x1a, IEEE80211_RATE_SHORT_PREAMBLE),
  128. RATE(55, 0x19, IEEE80211_RATE_SHORT_PREAMBLE),
  129. RATE(110, 0x18, IEEE80211_RATE_SHORT_PREAMBLE),
  130. RATE(60, 0x0b, 0),
  131. RATE(90, 0x0f, 0),
  132. RATE(120, 0x0a, 0),
  133. RATE(180, 0x0e, 0),
  134. RATE(240, 0x09, 0),
  135. RATE(360, 0x0d, 0),
  136. RATE(480, 0x08, 0),
  137. RATE(540, 0x0c, 0),
  138. };
  139. #ifdef CONFIG_MAC80211_LEDS
  140. static const struct ieee80211_tpt_blink ath9k_tpt_blink[] = {
  141. { .throughput = 0 * 1024, .blink_time = 334 },
  142. { .throughput = 1 * 1024, .blink_time = 260 },
  143. { .throughput = 5 * 1024, .blink_time = 220 },
  144. { .throughput = 10 * 1024, .blink_time = 190 },
  145. { .throughput = 20 * 1024, .blink_time = 170 },
  146. { .throughput = 50 * 1024, .blink_time = 150 },
  147. { .throughput = 70 * 1024, .blink_time = 130 },
  148. { .throughput = 100 * 1024, .blink_time = 110 },
  149. { .throughput = 200 * 1024, .blink_time = 80 },
  150. { .throughput = 300 * 1024, .blink_time = 50 },
  151. };
  152. #endif
  153. static void ath9k_deinit_softc(struct ath_softc *sc);
  154. /*
  155. * Read and write, they both share the same lock. We do this to serialize
  156. * reads and writes on Atheros 802.11n PCI devices only. This is required
  157. * as the FIFO on these devices can only accept sanely 2 requests.
  158. */
  159. static void ath9k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
  160. {
  161. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  162. struct ath_common *common = ath9k_hw_common(ah);
  163. struct ath_softc *sc = (struct ath_softc *) common->priv;
  164. if (NR_CPUS > 1 && ah->config.serialize_regmode == SER_REG_MODE_ON) {
  165. unsigned long flags;
  166. spin_lock_irqsave(&sc->sc_serial_rw, flags);
  167. iowrite32(val, sc->mem + reg_offset);
  168. spin_unlock_irqrestore(&sc->sc_serial_rw, flags);
  169. } else
  170. iowrite32(val, sc->mem + reg_offset);
  171. }
  172. static unsigned int ath9k_ioread32(void *hw_priv, u32 reg_offset)
  173. {
  174. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  175. struct ath_common *common = ath9k_hw_common(ah);
  176. struct ath_softc *sc = (struct ath_softc *) common->priv;
  177. u32 val;
  178. if (NR_CPUS > 1 && ah->config.serialize_regmode == SER_REG_MODE_ON) {
  179. unsigned long flags;
  180. spin_lock_irqsave(&sc->sc_serial_rw, flags);
  181. val = ioread32(sc->mem + reg_offset);
  182. spin_unlock_irqrestore(&sc->sc_serial_rw, flags);
  183. } else
  184. val = ioread32(sc->mem + reg_offset);
  185. return val;
  186. }
  187. static unsigned int __ath9k_reg_rmw(struct ath_softc *sc, u32 reg_offset,
  188. u32 set, u32 clr)
  189. {
  190. u32 val;
  191. val = ioread32(sc->mem + reg_offset);
  192. val &= ~clr;
  193. val |= set;
  194. iowrite32(val, sc->mem + reg_offset);
  195. return val;
  196. }
  197. static unsigned int ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
  198. {
  199. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  200. struct ath_common *common = ath9k_hw_common(ah);
  201. struct ath_softc *sc = (struct ath_softc *) common->priv;
  202. unsigned long uninitialized_var(flags);
  203. u32 val;
  204. if (NR_CPUS > 1 && ah->config.serialize_regmode == SER_REG_MODE_ON) {
  205. spin_lock_irqsave(&sc->sc_serial_rw, flags);
  206. val = __ath9k_reg_rmw(sc, reg_offset, set, clr);
  207. spin_unlock_irqrestore(&sc->sc_serial_rw, flags);
  208. } else
  209. val = __ath9k_reg_rmw(sc, reg_offset, set, clr);
  210. return val;
  211. }
  212. /**************************/
  213. /* Initialization */
  214. /**************************/
  215. static void setup_ht_cap(struct ath_softc *sc,
  216. struct ieee80211_sta_ht_cap *ht_info)
  217. {
  218. struct ath_hw *ah = sc->sc_ah;
  219. struct ath_common *common = ath9k_hw_common(ah);
  220. u8 tx_streams, rx_streams;
  221. int i, max_streams;
  222. ht_info->ht_supported = true;
  223. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  224. IEEE80211_HT_CAP_SM_PS |
  225. IEEE80211_HT_CAP_SGI_40 |
  226. IEEE80211_HT_CAP_DSSSCCK40;
  227. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_LDPC)
  228. ht_info->cap |= IEEE80211_HT_CAP_LDPC_CODING;
  229. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_SGI_20)
  230. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  231. ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  232. ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  233. if (AR_SREV_9330(ah) || AR_SREV_9485(ah) || AR_SREV_9565(ah))
  234. max_streams = 1;
  235. else if (AR_SREV_9462(ah))
  236. max_streams = 2;
  237. else if (AR_SREV_9300_20_OR_LATER(ah))
  238. max_streams = 3;
  239. else
  240. max_streams = 2;
  241. if (AR_SREV_9280_20_OR_LATER(ah)) {
  242. if (max_streams >= 2)
  243. ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
  244. ht_info->cap |= (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
  245. }
  246. /* set up supported mcs set */
  247. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  248. tx_streams = ath9k_cmn_count_streams(ah->txchainmask, max_streams);
  249. rx_streams = ath9k_cmn_count_streams(ah->rxchainmask, max_streams);
  250. ath_dbg(common, CONFIG, "TX streams %d, RX streams: %d\n",
  251. tx_streams, rx_streams);
  252. if (tx_streams != rx_streams) {
  253. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  254. ht_info->mcs.tx_params |= ((tx_streams - 1) <<
  255. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  256. }
  257. for (i = 0; i < rx_streams; i++)
  258. ht_info->mcs.rx_mask[i] = 0xff;
  259. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  260. }
  261. static void ath9k_reg_notifier(struct wiphy *wiphy,
  262. struct regulatory_request *request)
  263. {
  264. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  265. struct ath_softc *sc = hw->priv;
  266. struct ath_hw *ah = sc->sc_ah;
  267. struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
  268. ath_reg_notifier_apply(wiphy, request, reg);
  269. /* Set tx power */
  270. if (ah->curchan) {
  271. sc->config.txpowlimit = 2 * ah->curchan->chan->max_power;
  272. ath9k_ps_wakeup(sc);
  273. ath9k_hw_set_txpowerlimit(ah, sc->config.txpowlimit, false);
  274. sc->curtxpow = ath9k_hw_regulatory(ah)->power_limit;
  275. /* synchronize DFS detector if regulatory domain changed */
  276. if (sc->dfs_detector != NULL)
  277. sc->dfs_detector->set_dfs_domain(sc->dfs_detector,
  278. request->dfs_region);
  279. ath9k_ps_restore(sc);
  280. }
  281. }
  282. /*
  283. * This function will allocate both the DMA descriptor structure, and the
  284. * buffers it contains. These are used to contain the descriptors used
  285. * by the system.
  286. */
  287. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  288. struct list_head *head, const char *name,
  289. int nbuf, int ndesc, bool is_tx)
  290. {
  291. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  292. u8 *ds;
  293. struct ath_buf *bf;
  294. int i, bsize, desc_len;
  295. ath_dbg(common, CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  296. name, nbuf, ndesc);
  297. INIT_LIST_HEAD(head);
  298. if (is_tx)
  299. desc_len = sc->sc_ah->caps.tx_desc_len;
  300. else
  301. desc_len = sizeof(struct ath_desc);
  302. /* ath_desc must be a multiple of DWORDs */
  303. if ((desc_len % 4) != 0) {
  304. ath_err(common, "ath_desc not DWORD aligned\n");
  305. BUG_ON((desc_len % 4) != 0);
  306. return -ENOMEM;
  307. }
  308. dd->dd_desc_len = desc_len * nbuf * ndesc;
  309. /*
  310. * Need additional DMA memory because we can't use
  311. * descriptors that cross the 4K page boundary. Assume
  312. * one skipped descriptor per 4K page.
  313. */
  314. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  315. u32 ndesc_skipped =
  316. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  317. u32 dma_len;
  318. while (ndesc_skipped) {
  319. dma_len = ndesc_skipped * desc_len;
  320. dd->dd_desc_len += dma_len;
  321. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  322. }
  323. }
  324. /* allocate descriptors */
  325. dd->dd_desc = dmam_alloc_coherent(sc->dev, dd->dd_desc_len,
  326. &dd->dd_desc_paddr, GFP_KERNEL);
  327. if (!dd->dd_desc)
  328. return -ENOMEM;
  329. ds = (u8 *) dd->dd_desc;
  330. ath_dbg(common, CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  331. name, ds, (u32) dd->dd_desc_len,
  332. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  333. /* allocate buffers */
  334. bsize = sizeof(struct ath_buf) * nbuf;
  335. bf = devm_kzalloc(sc->dev, bsize, GFP_KERNEL);
  336. if (!bf)
  337. return -ENOMEM;
  338. for (i = 0; i < nbuf; i++, bf++, ds += (desc_len * ndesc)) {
  339. bf->bf_desc = ds;
  340. bf->bf_daddr = DS2PHYS(dd, ds);
  341. if (!(sc->sc_ah->caps.hw_caps &
  342. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  343. /*
  344. * Skip descriptor addresses which can cause 4KB
  345. * boundary crossing (addr + length) with a 32 dword
  346. * descriptor fetch.
  347. */
  348. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  349. BUG_ON((caddr_t) bf->bf_desc >=
  350. ((caddr_t) dd->dd_desc +
  351. dd->dd_desc_len));
  352. ds += (desc_len * ndesc);
  353. bf->bf_desc = ds;
  354. bf->bf_daddr = DS2PHYS(dd, ds);
  355. }
  356. }
  357. list_add_tail(&bf->list, head);
  358. }
  359. return 0;
  360. }
  361. static int ath9k_init_queues(struct ath_softc *sc)
  362. {
  363. int i = 0;
  364. sc->beacon.beaconq = ath9k_hw_beaconq_setup(sc->sc_ah);
  365. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  366. sc->config.cabqReadytime = ATH_CABQ_READY_TIME;
  367. ath_cabq_update(sc);
  368. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  369. sc->tx.txq_map[i] = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, i);
  370. sc->tx.txq_map[i]->mac80211_qnum = i;
  371. sc->tx.txq_max_pending[i] = ATH_MAX_QDEPTH;
  372. }
  373. return 0;
  374. }
  375. static int ath9k_init_channels_rates(struct ath_softc *sc)
  376. {
  377. void *channels;
  378. BUILD_BUG_ON(ARRAY_SIZE(ath9k_2ghz_chantable) +
  379. ARRAY_SIZE(ath9k_5ghz_chantable) !=
  380. ATH9K_NUM_CHANNELS);
  381. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ) {
  382. channels = devm_kzalloc(sc->dev,
  383. sizeof(ath9k_2ghz_chantable), GFP_KERNEL);
  384. if (!channels)
  385. return -ENOMEM;
  386. memcpy(channels, ath9k_2ghz_chantable,
  387. sizeof(ath9k_2ghz_chantable));
  388. sc->sbands[IEEE80211_BAND_2GHZ].channels = channels;
  389. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  390. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  391. ARRAY_SIZE(ath9k_2ghz_chantable);
  392. sc->sbands[IEEE80211_BAND_2GHZ].bitrates = ath9k_legacy_rates;
  393. sc->sbands[IEEE80211_BAND_2GHZ].n_bitrates =
  394. ARRAY_SIZE(ath9k_legacy_rates);
  395. }
  396. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ) {
  397. channels = devm_kzalloc(sc->dev,
  398. sizeof(ath9k_5ghz_chantable), GFP_KERNEL);
  399. if (!channels)
  400. return -ENOMEM;
  401. memcpy(channels, ath9k_5ghz_chantable,
  402. sizeof(ath9k_5ghz_chantable));
  403. sc->sbands[IEEE80211_BAND_5GHZ].channels = channels;
  404. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  405. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  406. ARRAY_SIZE(ath9k_5ghz_chantable);
  407. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  408. ath9k_legacy_rates + 4;
  409. sc->sbands[IEEE80211_BAND_5GHZ].n_bitrates =
  410. ARRAY_SIZE(ath9k_legacy_rates) - 4;
  411. }
  412. return 0;
  413. }
  414. static void ath9k_init_misc(struct ath_softc *sc)
  415. {
  416. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  417. int i = 0;
  418. setup_timer(&common->ani.timer, ath_ani_calibrate, (unsigned long)sc);
  419. sc->last_rssi = ATH_RSSI_DUMMY_MARKER;
  420. sc->config.txpowlimit = ATH_TXPOWER_MAX;
  421. memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
  422. sc->beacon.slottime = ATH9K_SLOT_TIME_9;
  423. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++)
  424. sc->beacon.bslot[i] = NULL;
  425. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
  426. sc->ant_comb.count = ATH_ANT_DIV_COMB_INIT_COUNT;
  427. sc->spec_config.enabled = 0;
  428. sc->spec_config.short_repeat = true;
  429. sc->spec_config.count = 8;
  430. sc->spec_config.endless = false;
  431. sc->spec_config.period = 0xFF;
  432. sc->spec_config.fft_period = 0xF;
  433. }
  434. static void ath9k_eeprom_request_cb(const struct firmware *eeprom_blob,
  435. void *ctx)
  436. {
  437. struct ath9k_eeprom_ctx *ec = ctx;
  438. if (eeprom_blob)
  439. ec->ah->eeprom_blob = eeprom_blob;
  440. complete(&ec->complete);
  441. }
  442. static int ath9k_eeprom_request(struct ath_softc *sc, const char *name)
  443. {
  444. struct ath9k_eeprom_ctx ec;
  445. struct ath_hw *ah = ah = sc->sc_ah;
  446. int err;
  447. /* try to load the EEPROM content asynchronously */
  448. init_completion(&ec.complete);
  449. ec.ah = sc->sc_ah;
  450. err = request_firmware_nowait(THIS_MODULE, 1, name, sc->dev, GFP_KERNEL,
  451. &ec, ath9k_eeprom_request_cb);
  452. if (err < 0) {
  453. ath_err(ath9k_hw_common(ah),
  454. "EEPROM request failed\n");
  455. return err;
  456. }
  457. wait_for_completion(&ec.complete);
  458. if (!ah->eeprom_blob) {
  459. ath_err(ath9k_hw_common(ah),
  460. "Unable to load EEPROM file %s\n", name);
  461. return -EINVAL;
  462. }
  463. return 0;
  464. }
  465. static void ath9k_eeprom_release(struct ath_softc *sc)
  466. {
  467. release_firmware(sc->sc_ah->eeprom_blob);
  468. }
  469. static int ath9k_init_softc(u16 devid, struct ath_softc *sc,
  470. const struct ath_bus_ops *bus_ops)
  471. {
  472. struct ath9k_platform_data *pdata = sc->dev->platform_data;
  473. struct ath_hw *ah = NULL;
  474. struct ath_common *common;
  475. int ret = 0, i;
  476. int csz = 0;
  477. ah = devm_kzalloc(sc->dev, sizeof(struct ath_hw), GFP_KERNEL);
  478. if (!ah)
  479. return -ENOMEM;
  480. ah->dev = sc->dev;
  481. ah->hw = sc->hw;
  482. ah->hw_version.devid = devid;
  483. ah->reg_ops.read = ath9k_ioread32;
  484. ah->reg_ops.write = ath9k_iowrite32;
  485. ah->reg_ops.rmw = ath9k_reg_rmw;
  486. atomic_set(&ah->intr_ref_cnt, -1);
  487. sc->sc_ah = ah;
  488. sc->dfs_detector = dfs_pattern_detector_init(ah, NL80211_DFS_UNSET);
  489. if (!pdata) {
  490. ah->ah_flags |= AH_USE_EEPROM;
  491. sc->sc_ah->led_pin = -1;
  492. } else {
  493. sc->sc_ah->gpio_mask = pdata->gpio_mask;
  494. sc->sc_ah->gpio_val = pdata->gpio_val;
  495. sc->sc_ah->led_pin = pdata->led_pin;
  496. ah->is_clk_25mhz = pdata->is_clk_25mhz;
  497. ah->get_mac_revision = pdata->get_mac_revision;
  498. ah->external_reset = pdata->external_reset;
  499. }
  500. common = ath9k_hw_common(ah);
  501. common->ops = &ah->reg_ops;
  502. common->bus_ops = bus_ops;
  503. common->ah = ah;
  504. common->hw = sc->hw;
  505. common->priv = sc;
  506. common->debug_mask = ath9k_debug;
  507. common->btcoex_enabled = ath9k_btcoex_enable == 1;
  508. common->disable_ani = false;
  509. /*
  510. * Enable Antenna diversity only when BTCOEX is disabled
  511. * and the user manually requests the feature.
  512. */
  513. if (!common->btcoex_enabled && ath9k_enable_diversity)
  514. common->antenna_diversity = 1;
  515. spin_lock_init(&common->cc_lock);
  516. spin_lock_init(&sc->sc_serial_rw);
  517. spin_lock_init(&sc->sc_pm_lock);
  518. mutex_init(&sc->mutex);
  519. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  520. tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
  521. (unsigned long)sc);
  522. INIT_WORK(&sc->hw_reset_work, ath_reset_work);
  523. INIT_WORK(&sc->hw_check_work, ath_hw_check);
  524. INIT_WORK(&sc->paprd_work, ath_paprd_calibrate);
  525. INIT_DELAYED_WORK(&sc->hw_pll_work, ath_hw_pll_work);
  526. setup_timer(&sc->rx_poll_timer, ath_rx_poll, (unsigned long)sc);
  527. /*
  528. * Cache line size is used to size and align various
  529. * structures used to communicate with the hardware.
  530. */
  531. ath_read_cachesize(common, &csz);
  532. common->cachelsz = csz << 2; /* convert to bytes */
  533. if (pdata && pdata->eeprom_name) {
  534. ret = ath9k_eeprom_request(sc, pdata->eeprom_name);
  535. if (ret)
  536. return ret;
  537. }
  538. /* Initializes the hardware for all supported chipsets */
  539. ret = ath9k_hw_init(ah);
  540. if (ret)
  541. goto err_hw;
  542. if (pdata && pdata->macaddr)
  543. memcpy(common->macaddr, pdata->macaddr, ETH_ALEN);
  544. ret = ath9k_init_queues(sc);
  545. if (ret)
  546. goto err_queues;
  547. ret = ath9k_init_btcoex(sc);
  548. if (ret)
  549. goto err_btcoex;
  550. ret = ath9k_init_channels_rates(sc);
  551. if (ret)
  552. goto err_btcoex;
  553. ath9k_cmn_init_crypto(sc->sc_ah);
  554. ath9k_init_misc(sc);
  555. ath_fill_led_pin(sc);
  556. if (common->bus_ops->aspm_init)
  557. common->bus_ops->aspm_init(common);
  558. return 0;
  559. err_btcoex:
  560. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  561. if (ATH_TXQ_SETUP(sc, i))
  562. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  563. err_queues:
  564. ath9k_hw_deinit(ah);
  565. err_hw:
  566. ath9k_eeprom_release(sc);
  567. return ret;
  568. }
  569. static void ath9k_init_band_txpower(struct ath_softc *sc, int band)
  570. {
  571. struct ieee80211_supported_band *sband;
  572. struct ieee80211_channel *chan;
  573. struct ath_hw *ah = sc->sc_ah;
  574. int i;
  575. sband = &sc->sbands[band];
  576. for (i = 0; i < sband->n_channels; i++) {
  577. chan = &sband->channels[i];
  578. ah->curchan = &ah->channels[chan->hw_value];
  579. ath9k_cmn_update_ichannel(ah->curchan, chan, NL80211_CHAN_HT20);
  580. ath9k_hw_set_txpowerlimit(ah, MAX_RATE_POWER, true);
  581. }
  582. }
  583. static void ath9k_init_txpower_limits(struct ath_softc *sc)
  584. {
  585. struct ath_hw *ah = sc->sc_ah;
  586. struct ath9k_channel *curchan = ah->curchan;
  587. if (ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  588. ath9k_init_band_txpower(sc, IEEE80211_BAND_2GHZ);
  589. if (ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  590. ath9k_init_band_txpower(sc, IEEE80211_BAND_5GHZ);
  591. ah->curchan = curchan;
  592. }
  593. void ath9k_reload_chainmask_settings(struct ath_softc *sc)
  594. {
  595. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT))
  596. return;
  597. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  598. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  599. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  600. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  601. }
  602. static const struct ieee80211_iface_limit if_limits[] = {
  603. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  604. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  605. BIT(NL80211_IFTYPE_WDS) },
  606. { .max = 8, .types =
  607. #ifdef CONFIG_MAC80211_MESH
  608. BIT(NL80211_IFTYPE_MESH_POINT) |
  609. #endif
  610. BIT(NL80211_IFTYPE_AP) |
  611. BIT(NL80211_IFTYPE_P2P_GO) },
  612. };
  613. static const struct ieee80211_iface_limit if_dfs_limits[] = {
  614. { .max = 1, .types = BIT(NL80211_IFTYPE_AP) },
  615. };
  616. static const struct ieee80211_iface_combination if_comb[] = {
  617. {
  618. .limits = if_limits,
  619. .n_limits = ARRAY_SIZE(if_limits),
  620. .max_interfaces = 2048,
  621. .num_different_channels = 1,
  622. .beacon_int_infra_match = true,
  623. },
  624. {
  625. .limits = if_dfs_limits,
  626. .n_limits = ARRAY_SIZE(if_dfs_limits),
  627. .max_interfaces = 1,
  628. .num_different_channels = 1,
  629. .beacon_int_infra_match = true,
  630. .radar_detect_widths = BIT(NL80211_CHAN_NO_HT) |
  631. BIT(NL80211_CHAN_HT20),
  632. }
  633. };
  634. void ath9k_set_hw_capab(struct ath_softc *sc, struct ieee80211_hw *hw)
  635. {
  636. struct ath_hw *ah = sc->sc_ah;
  637. struct ath_common *common = ath9k_hw_common(ah);
  638. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  639. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  640. IEEE80211_HW_SIGNAL_DBM |
  641. IEEE80211_HW_SUPPORTS_PS |
  642. IEEE80211_HW_PS_NULLFUNC_STACK |
  643. IEEE80211_HW_SPECTRUM_MGMT |
  644. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  645. IEEE80211_HW_SUPPORTS_RC_TABLE;
  646. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  647. hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  648. if (AR_SREV_9280_20_OR_LATER(ah))
  649. hw->radiotap_mcs_details |=
  650. IEEE80211_RADIOTAP_MCS_HAVE_STBC;
  651. }
  652. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah) || ath9k_modparam_nohwcrypt)
  653. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  654. hw->wiphy->interface_modes =
  655. BIT(NL80211_IFTYPE_P2P_GO) |
  656. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  657. BIT(NL80211_IFTYPE_AP) |
  658. BIT(NL80211_IFTYPE_WDS) |
  659. BIT(NL80211_IFTYPE_STATION) |
  660. BIT(NL80211_IFTYPE_ADHOC) |
  661. BIT(NL80211_IFTYPE_MESH_POINT);
  662. hw->wiphy->iface_combinations = if_comb;
  663. hw->wiphy->n_iface_combinations = ARRAY_SIZE(if_comb);
  664. if (AR_SREV_5416(sc->sc_ah))
  665. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  666. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  667. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
  668. hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  669. #ifdef CONFIG_PM_SLEEP
  670. if ((ah->caps.hw_caps & ATH9K_HW_WOW_DEVICE_CAPABLE) &&
  671. device_can_wakeup(sc->dev)) {
  672. hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
  673. WIPHY_WOWLAN_DISCONNECT;
  674. hw->wiphy->wowlan.n_patterns = MAX_NUM_USER_PATTERN;
  675. hw->wiphy->wowlan.pattern_min_len = 1;
  676. hw->wiphy->wowlan.pattern_max_len = MAX_PATTERN_SIZE;
  677. }
  678. atomic_set(&sc->wow_sleep_proc_intr, -1);
  679. atomic_set(&sc->wow_got_bmiss_intr, -1);
  680. #endif
  681. hw->queues = 4;
  682. hw->max_rates = 4;
  683. hw->channel_change_time = 5000;
  684. hw->max_listen_interval = 1;
  685. hw->max_rate_tries = 10;
  686. hw->sta_data_size = sizeof(struct ath_node);
  687. hw->vif_data_size = sizeof(struct ath_vif);
  688. hw->wiphy->available_antennas_rx = BIT(ah->caps.max_rxchains) - 1;
  689. hw->wiphy->available_antennas_tx = BIT(ah->caps.max_txchains) - 1;
  690. /* single chain devices with rx diversity */
  691. if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
  692. hw->wiphy->available_antennas_rx = BIT(0) | BIT(1);
  693. sc->ant_rx = hw->wiphy->available_antennas_rx;
  694. sc->ant_tx = hw->wiphy->available_antennas_tx;
  695. #ifdef CONFIG_ATH9K_RATE_CONTROL
  696. hw->rate_control_algorithm = "ath9k_rate_control";
  697. #endif
  698. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  699. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  700. &sc->sbands[IEEE80211_BAND_2GHZ];
  701. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  702. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  703. &sc->sbands[IEEE80211_BAND_5GHZ];
  704. ath9k_reload_chainmask_settings(sc);
  705. SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
  706. }
  707. int ath9k_init_device(u16 devid, struct ath_softc *sc,
  708. const struct ath_bus_ops *bus_ops)
  709. {
  710. struct ieee80211_hw *hw = sc->hw;
  711. struct ath_common *common;
  712. struct ath_hw *ah;
  713. int error = 0;
  714. struct ath_regulatory *reg;
  715. /* Bring up device */
  716. error = ath9k_init_softc(devid, sc, bus_ops);
  717. if (error)
  718. return error;
  719. ah = sc->sc_ah;
  720. common = ath9k_hw_common(ah);
  721. ath9k_set_hw_capab(sc, hw);
  722. /* Initialize regulatory */
  723. error = ath_regd_init(&common->regulatory, sc->hw->wiphy,
  724. ath9k_reg_notifier);
  725. if (error)
  726. goto deinit;
  727. reg = &common->regulatory;
  728. /* Setup TX DMA */
  729. error = ath_tx_init(sc, ATH_TXBUF);
  730. if (error != 0)
  731. goto deinit;
  732. /* Setup RX DMA */
  733. error = ath_rx_init(sc, ATH_RXBUF);
  734. if (error != 0)
  735. goto deinit;
  736. ath9k_init_txpower_limits(sc);
  737. #ifdef CONFIG_MAC80211_LEDS
  738. /* must be initialized before ieee80211_register_hw */
  739. sc->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(sc->hw,
  740. IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_tpt_blink,
  741. ARRAY_SIZE(ath9k_tpt_blink));
  742. #endif
  743. /* Register with mac80211 */
  744. error = ieee80211_register_hw(hw);
  745. if (error)
  746. goto rx_cleanup;
  747. error = ath9k_init_debug(ah);
  748. if (error) {
  749. ath_err(common, "Unable to create debugfs files\n");
  750. goto unregister;
  751. }
  752. /* Handle world regulatory */
  753. if (!ath_is_world_regd(reg)) {
  754. error = regulatory_hint(hw->wiphy, reg->alpha2);
  755. if (error)
  756. goto unregister;
  757. }
  758. ath_init_leds(sc);
  759. ath_start_rfkill_poll(sc);
  760. return 0;
  761. unregister:
  762. ieee80211_unregister_hw(hw);
  763. rx_cleanup:
  764. ath_rx_cleanup(sc);
  765. deinit:
  766. ath9k_deinit_softc(sc);
  767. return error;
  768. }
  769. /*****************************/
  770. /* De-Initialization */
  771. /*****************************/
  772. static void ath9k_deinit_softc(struct ath_softc *sc)
  773. {
  774. int i = 0;
  775. ath9k_deinit_btcoex(sc);
  776. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  777. if (ATH_TXQ_SETUP(sc, i))
  778. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  779. ath9k_hw_deinit(sc->sc_ah);
  780. if (sc->dfs_detector != NULL)
  781. sc->dfs_detector->exit(sc->dfs_detector);
  782. ath9k_eeprom_release(sc);
  783. if (config_enabled(CONFIG_ATH9K_DEBUGFS) && sc->rfs_chan_spec_scan) {
  784. relay_close(sc->rfs_chan_spec_scan);
  785. sc->rfs_chan_spec_scan = NULL;
  786. }
  787. }
  788. void ath9k_deinit_device(struct ath_softc *sc)
  789. {
  790. struct ieee80211_hw *hw = sc->hw;
  791. ath9k_ps_wakeup(sc);
  792. wiphy_rfkill_stop_polling(sc->hw->wiphy);
  793. ath_deinit_leds(sc);
  794. ath9k_ps_restore(sc);
  795. ieee80211_unregister_hw(hw);
  796. ath_rx_cleanup(sc);
  797. ath9k_deinit_softc(sc);
  798. }
  799. /************************/
  800. /* Module Hooks */
  801. /************************/
  802. static int __init ath9k_init(void)
  803. {
  804. int error;
  805. /* Register rate control algorithm */
  806. error = ath_rate_control_register();
  807. if (error != 0) {
  808. pr_err("Unable to register rate control algorithm: %d\n",
  809. error);
  810. goto err_out;
  811. }
  812. error = ath_pci_init();
  813. if (error < 0) {
  814. pr_err("No PCI devices found, driver not installed\n");
  815. error = -ENODEV;
  816. goto err_rate_unregister;
  817. }
  818. error = ath_ahb_init();
  819. if (error < 0) {
  820. error = -ENODEV;
  821. goto err_pci_exit;
  822. }
  823. return 0;
  824. err_pci_exit:
  825. ath_pci_exit();
  826. err_rate_unregister:
  827. ath_rate_control_unregister();
  828. err_out:
  829. return error;
  830. }
  831. module_init(ath9k_init);
  832. static void __exit ath9k_exit(void)
  833. {
  834. is_ath9k_unloaded = true;
  835. ath_ahb_exit();
  836. ath_pci_exit();
  837. ath_rate_control_unregister();
  838. pr_info("%s: Driver unloaded\n", dev_info);
  839. }
  840. module_exit(ath9k_exit);