init.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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. sc->tx.uapsdq = ath_txq_setup(sc, ATH9K_TX_QUEUE_UAPSD, 0);
  369. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  370. sc->tx.txq_map[i] = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, i);
  371. sc->tx.txq_map[i]->mac80211_qnum = i;
  372. sc->tx.txq_max_pending[i] = ATH_MAX_QDEPTH;
  373. }
  374. return 0;
  375. }
  376. static int ath9k_init_channels_rates(struct ath_softc *sc)
  377. {
  378. void *channels;
  379. BUILD_BUG_ON(ARRAY_SIZE(ath9k_2ghz_chantable) +
  380. ARRAY_SIZE(ath9k_5ghz_chantable) !=
  381. ATH9K_NUM_CHANNELS);
  382. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ) {
  383. channels = devm_kzalloc(sc->dev,
  384. sizeof(ath9k_2ghz_chantable), GFP_KERNEL);
  385. if (!channels)
  386. return -ENOMEM;
  387. memcpy(channels, ath9k_2ghz_chantable,
  388. sizeof(ath9k_2ghz_chantable));
  389. sc->sbands[IEEE80211_BAND_2GHZ].channels = channels;
  390. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  391. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  392. ARRAY_SIZE(ath9k_2ghz_chantable);
  393. sc->sbands[IEEE80211_BAND_2GHZ].bitrates = ath9k_legacy_rates;
  394. sc->sbands[IEEE80211_BAND_2GHZ].n_bitrates =
  395. ARRAY_SIZE(ath9k_legacy_rates);
  396. }
  397. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ) {
  398. channels = devm_kzalloc(sc->dev,
  399. sizeof(ath9k_5ghz_chantable), GFP_KERNEL);
  400. if (!channels)
  401. return -ENOMEM;
  402. memcpy(channels, ath9k_5ghz_chantable,
  403. sizeof(ath9k_5ghz_chantable));
  404. sc->sbands[IEEE80211_BAND_5GHZ].channels = channels;
  405. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  406. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  407. ARRAY_SIZE(ath9k_5ghz_chantable);
  408. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  409. ath9k_legacy_rates + 4;
  410. sc->sbands[IEEE80211_BAND_5GHZ].n_bitrates =
  411. ARRAY_SIZE(ath9k_legacy_rates) - 4;
  412. }
  413. return 0;
  414. }
  415. static void ath9k_init_misc(struct ath_softc *sc)
  416. {
  417. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  418. int i = 0;
  419. setup_timer(&common->ani.timer, ath_ani_calibrate, (unsigned long)sc);
  420. sc->last_rssi = ATH_RSSI_DUMMY_MARKER;
  421. sc->config.txpowlimit = ATH_TXPOWER_MAX;
  422. memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
  423. sc->beacon.slottime = ATH9K_SLOT_TIME_9;
  424. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++)
  425. sc->beacon.bslot[i] = NULL;
  426. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
  427. sc->ant_comb.count = ATH_ANT_DIV_COMB_INIT_COUNT;
  428. sc->spec_config.enabled = 0;
  429. sc->spec_config.short_repeat = true;
  430. sc->spec_config.count = 8;
  431. sc->spec_config.endless = false;
  432. sc->spec_config.period = 0xFF;
  433. sc->spec_config.fft_period = 0xF;
  434. }
  435. static void ath9k_init_platform(struct ath_softc *sc)
  436. {
  437. struct ath_hw *ah = sc->sc_ah;
  438. struct ath_common *common = ath9k_hw_common(ah);
  439. if (common->bus_ops->ath_bus_type != ATH_PCI)
  440. return;
  441. if (sc->driver_data & (ATH9K_PCI_CUS198 |
  442. ATH9K_PCI_CUS230)) {
  443. ah->config.xlna_gpio = 9;
  444. ah->config.xatten_margin_cfg = true;
  445. ath_info(common, "Set parameters for %s\n",
  446. (sc->driver_data & ATH9K_PCI_CUS198) ?
  447. "CUS198" : "CUS230");
  448. } else if (sc->driver_data & ATH9K_PCI_CUS217) {
  449. ath_info(common, "CUS217 card detected\n");
  450. }
  451. }
  452. static void ath9k_eeprom_request_cb(const struct firmware *eeprom_blob,
  453. void *ctx)
  454. {
  455. struct ath9k_eeprom_ctx *ec = ctx;
  456. if (eeprom_blob)
  457. ec->ah->eeprom_blob = eeprom_blob;
  458. complete(&ec->complete);
  459. }
  460. static int ath9k_eeprom_request(struct ath_softc *sc, const char *name)
  461. {
  462. struct ath9k_eeprom_ctx ec;
  463. struct ath_hw *ah = ah = sc->sc_ah;
  464. int err;
  465. /* try to load the EEPROM content asynchronously */
  466. init_completion(&ec.complete);
  467. ec.ah = sc->sc_ah;
  468. err = request_firmware_nowait(THIS_MODULE, 1, name, sc->dev, GFP_KERNEL,
  469. &ec, ath9k_eeprom_request_cb);
  470. if (err < 0) {
  471. ath_err(ath9k_hw_common(ah),
  472. "EEPROM request failed\n");
  473. return err;
  474. }
  475. wait_for_completion(&ec.complete);
  476. if (!ah->eeprom_blob) {
  477. ath_err(ath9k_hw_common(ah),
  478. "Unable to load EEPROM file %s\n", name);
  479. return -EINVAL;
  480. }
  481. return 0;
  482. }
  483. static void ath9k_eeprom_release(struct ath_softc *sc)
  484. {
  485. release_firmware(sc->sc_ah->eeprom_blob);
  486. }
  487. static int ath9k_init_softc(u16 devid, struct ath_softc *sc,
  488. const struct ath_bus_ops *bus_ops)
  489. {
  490. struct ath9k_platform_data *pdata = sc->dev->platform_data;
  491. struct ath_hw *ah = NULL;
  492. struct ath_common *common;
  493. int ret = 0, i;
  494. int csz = 0;
  495. ah = devm_kzalloc(sc->dev, sizeof(struct ath_hw), GFP_KERNEL);
  496. if (!ah)
  497. return -ENOMEM;
  498. ah->dev = sc->dev;
  499. ah->hw = sc->hw;
  500. ah->hw_version.devid = devid;
  501. ah->reg_ops.read = ath9k_ioread32;
  502. ah->reg_ops.write = ath9k_iowrite32;
  503. ah->reg_ops.rmw = ath9k_reg_rmw;
  504. atomic_set(&ah->intr_ref_cnt, -1);
  505. sc->sc_ah = ah;
  506. sc->dfs_detector = dfs_pattern_detector_init(ah, NL80211_DFS_UNSET);
  507. if (!pdata) {
  508. ah->ah_flags |= AH_USE_EEPROM;
  509. sc->sc_ah->led_pin = -1;
  510. } else {
  511. sc->sc_ah->gpio_mask = pdata->gpio_mask;
  512. sc->sc_ah->gpio_val = pdata->gpio_val;
  513. sc->sc_ah->led_pin = pdata->led_pin;
  514. ah->is_clk_25mhz = pdata->is_clk_25mhz;
  515. ah->get_mac_revision = pdata->get_mac_revision;
  516. ah->external_reset = pdata->external_reset;
  517. }
  518. common = ath9k_hw_common(ah);
  519. common->ops = &ah->reg_ops;
  520. common->bus_ops = bus_ops;
  521. common->ah = ah;
  522. common->hw = sc->hw;
  523. common->priv = sc;
  524. common->debug_mask = ath9k_debug;
  525. common->btcoex_enabled = ath9k_btcoex_enable == 1;
  526. common->disable_ani = false;
  527. /*
  528. * Platform quirks.
  529. */
  530. ath9k_init_platform(sc);
  531. /*
  532. * Enable Antenna diversity only when BTCOEX is disabled
  533. * and the user manually requests the feature.
  534. */
  535. if (!common->btcoex_enabled && ath9k_enable_diversity)
  536. common->antenna_diversity = 1;
  537. spin_lock_init(&common->cc_lock);
  538. spin_lock_init(&sc->sc_serial_rw);
  539. spin_lock_init(&sc->sc_pm_lock);
  540. mutex_init(&sc->mutex);
  541. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  542. tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
  543. (unsigned long)sc);
  544. INIT_WORK(&sc->hw_reset_work, ath_reset_work);
  545. INIT_WORK(&sc->hw_check_work, ath_hw_check);
  546. INIT_WORK(&sc->paprd_work, ath_paprd_calibrate);
  547. INIT_DELAYED_WORK(&sc->hw_pll_work, ath_hw_pll_work);
  548. setup_timer(&sc->rx_poll_timer, ath_rx_poll, (unsigned long)sc);
  549. /*
  550. * Cache line size is used to size and align various
  551. * structures used to communicate with the hardware.
  552. */
  553. ath_read_cachesize(common, &csz);
  554. common->cachelsz = csz << 2; /* convert to bytes */
  555. if (pdata && pdata->eeprom_name) {
  556. ret = ath9k_eeprom_request(sc, pdata->eeprom_name);
  557. if (ret)
  558. return ret;
  559. }
  560. /* Initializes the hardware for all supported chipsets */
  561. ret = ath9k_hw_init(ah);
  562. if (ret)
  563. goto err_hw;
  564. if (pdata && pdata->macaddr)
  565. memcpy(common->macaddr, pdata->macaddr, ETH_ALEN);
  566. ret = ath9k_init_queues(sc);
  567. if (ret)
  568. goto err_queues;
  569. ret = ath9k_init_btcoex(sc);
  570. if (ret)
  571. goto err_btcoex;
  572. ret = ath9k_init_channels_rates(sc);
  573. if (ret)
  574. goto err_btcoex;
  575. ath9k_cmn_init_crypto(sc->sc_ah);
  576. ath9k_init_misc(sc);
  577. ath_fill_led_pin(sc);
  578. if (common->bus_ops->aspm_init)
  579. common->bus_ops->aspm_init(common);
  580. return 0;
  581. err_btcoex:
  582. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  583. if (ATH_TXQ_SETUP(sc, i))
  584. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  585. err_queues:
  586. ath9k_hw_deinit(ah);
  587. err_hw:
  588. ath9k_eeprom_release(sc);
  589. return ret;
  590. }
  591. static void ath9k_init_band_txpower(struct ath_softc *sc, int band)
  592. {
  593. struct ieee80211_supported_band *sband;
  594. struct ieee80211_channel *chan;
  595. struct ath_hw *ah = sc->sc_ah;
  596. int i;
  597. sband = &sc->sbands[band];
  598. for (i = 0; i < sband->n_channels; i++) {
  599. chan = &sband->channels[i];
  600. ah->curchan = &ah->channels[chan->hw_value];
  601. ath9k_cmn_update_ichannel(ah->curchan, chan, NL80211_CHAN_HT20);
  602. ath9k_hw_set_txpowerlimit(ah, MAX_RATE_POWER, true);
  603. }
  604. }
  605. static void ath9k_init_txpower_limits(struct ath_softc *sc)
  606. {
  607. struct ath_hw *ah = sc->sc_ah;
  608. struct ath9k_channel *curchan = ah->curchan;
  609. if (ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  610. ath9k_init_band_txpower(sc, IEEE80211_BAND_2GHZ);
  611. if (ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  612. ath9k_init_band_txpower(sc, IEEE80211_BAND_5GHZ);
  613. ah->curchan = curchan;
  614. }
  615. void ath9k_reload_chainmask_settings(struct ath_softc *sc)
  616. {
  617. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT))
  618. return;
  619. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  620. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  621. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  622. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  623. }
  624. static const struct ieee80211_iface_limit if_limits[] = {
  625. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  626. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  627. BIT(NL80211_IFTYPE_WDS) },
  628. { .max = 8, .types =
  629. #ifdef CONFIG_MAC80211_MESH
  630. BIT(NL80211_IFTYPE_MESH_POINT) |
  631. #endif
  632. BIT(NL80211_IFTYPE_AP) |
  633. BIT(NL80211_IFTYPE_P2P_GO) },
  634. };
  635. static const struct ieee80211_iface_limit if_dfs_limits[] = {
  636. { .max = 1, .types = BIT(NL80211_IFTYPE_AP) },
  637. };
  638. static const struct ieee80211_iface_combination if_comb[] = {
  639. {
  640. .limits = if_limits,
  641. .n_limits = ARRAY_SIZE(if_limits),
  642. .max_interfaces = 2048,
  643. .num_different_channels = 1,
  644. .beacon_int_infra_match = true,
  645. },
  646. {
  647. .limits = if_dfs_limits,
  648. .n_limits = ARRAY_SIZE(if_dfs_limits),
  649. .max_interfaces = 1,
  650. .num_different_channels = 1,
  651. .beacon_int_infra_match = true,
  652. .radar_detect_widths = BIT(NL80211_CHAN_NO_HT) |
  653. BIT(NL80211_CHAN_HT20),
  654. }
  655. };
  656. #ifdef CONFIG_PM
  657. static const struct wiphy_wowlan_support ath9k_wowlan_support = {
  658. .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT,
  659. .n_patterns = MAX_NUM_USER_PATTERN,
  660. .pattern_min_len = 1,
  661. .pattern_max_len = MAX_PATTERN_SIZE,
  662. };
  663. #endif
  664. void ath9k_set_hw_capab(struct ath_softc *sc, struct ieee80211_hw *hw)
  665. {
  666. struct ath_hw *ah = sc->sc_ah;
  667. struct ath_common *common = ath9k_hw_common(ah);
  668. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  669. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  670. IEEE80211_HW_SIGNAL_DBM |
  671. IEEE80211_HW_SUPPORTS_PS |
  672. IEEE80211_HW_PS_NULLFUNC_STACK |
  673. IEEE80211_HW_SPECTRUM_MGMT |
  674. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  675. IEEE80211_HW_SUPPORTS_RC_TABLE;
  676. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  677. hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  678. if (AR_SREV_9280_20_OR_LATER(ah))
  679. hw->radiotap_mcs_details |=
  680. IEEE80211_RADIOTAP_MCS_HAVE_STBC;
  681. }
  682. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah) || ath9k_modparam_nohwcrypt)
  683. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  684. hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
  685. hw->wiphy->interface_modes =
  686. BIT(NL80211_IFTYPE_P2P_GO) |
  687. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  688. BIT(NL80211_IFTYPE_AP) |
  689. BIT(NL80211_IFTYPE_WDS) |
  690. BIT(NL80211_IFTYPE_STATION) |
  691. BIT(NL80211_IFTYPE_ADHOC) |
  692. BIT(NL80211_IFTYPE_MESH_POINT);
  693. hw->wiphy->iface_combinations = if_comb;
  694. hw->wiphy->n_iface_combinations = ARRAY_SIZE(if_comb);
  695. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  696. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  697. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
  698. hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  699. #ifdef CONFIG_PM_SLEEP
  700. if ((ah->caps.hw_caps & ATH9K_HW_WOW_DEVICE_CAPABLE) &&
  701. (sc->driver_data & ATH9K_PCI_WOW) &&
  702. device_can_wakeup(sc->dev))
  703. hw->wiphy->wowlan = &ath9k_wowlan_support;
  704. atomic_set(&sc->wow_sleep_proc_intr, -1);
  705. atomic_set(&sc->wow_got_bmiss_intr, -1);
  706. #endif
  707. hw->queues = 4;
  708. hw->max_rates = 4;
  709. hw->channel_change_time = 5000;
  710. hw->max_listen_interval = 1;
  711. hw->max_rate_tries = 10;
  712. hw->sta_data_size = sizeof(struct ath_node);
  713. hw->vif_data_size = sizeof(struct ath_vif);
  714. hw->wiphy->available_antennas_rx = BIT(ah->caps.max_rxchains) - 1;
  715. hw->wiphy->available_antennas_tx = BIT(ah->caps.max_txchains) - 1;
  716. /* single chain devices with rx diversity */
  717. if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
  718. hw->wiphy->available_antennas_rx = BIT(0) | BIT(1);
  719. sc->ant_rx = hw->wiphy->available_antennas_rx;
  720. sc->ant_tx = hw->wiphy->available_antennas_tx;
  721. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  722. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  723. &sc->sbands[IEEE80211_BAND_2GHZ];
  724. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  725. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  726. &sc->sbands[IEEE80211_BAND_5GHZ];
  727. ath9k_reload_chainmask_settings(sc);
  728. SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
  729. }
  730. int ath9k_init_device(u16 devid, struct ath_softc *sc,
  731. const struct ath_bus_ops *bus_ops)
  732. {
  733. struct ieee80211_hw *hw = sc->hw;
  734. struct ath_common *common;
  735. struct ath_hw *ah;
  736. int error = 0;
  737. struct ath_regulatory *reg;
  738. /* Bring up device */
  739. error = ath9k_init_softc(devid, sc, bus_ops);
  740. if (error)
  741. return error;
  742. ah = sc->sc_ah;
  743. common = ath9k_hw_common(ah);
  744. ath9k_set_hw_capab(sc, hw);
  745. /* Initialize regulatory */
  746. error = ath_regd_init(&common->regulatory, sc->hw->wiphy,
  747. ath9k_reg_notifier);
  748. if (error)
  749. goto deinit;
  750. reg = &common->regulatory;
  751. /* Setup TX DMA */
  752. error = ath_tx_init(sc, ATH_TXBUF);
  753. if (error != 0)
  754. goto deinit;
  755. /* Setup RX DMA */
  756. error = ath_rx_init(sc, ATH_RXBUF);
  757. if (error != 0)
  758. goto deinit;
  759. ath9k_init_txpower_limits(sc);
  760. #ifdef CONFIG_MAC80211_LEDS
  761. /* must be initialized before ieee80211_register_hw */
  762. sc->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(sc->hw,
  763. IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_tpt_blink,
  764. ARRAY_SIZE(ath9k_tpt_blink));
  765. #endif
  766. /* Register with mac80211 */
  767. error = ieee80211_register_hw(hw);
  768. if (error)
  769. goto rx_cleanup;
  770. error = ath9k_init_debug(ah);
  771. if (error) {
  772. ath_err(common, "Unable to create debugfs files\n");
  773. goto unregister;
  774. }
  775. /* Handle world regulatory */
  776. if (!ath_is_world_regd(reg)) {
  777. error = regulatory_hint(hw->wiphy, reg->alpha2);
  778. if (error)
  779. goto debug_cleanup;
  780. }
  781. ath_init_leds(sc);
  782. ath_start_rfkill_poll(sc);
  783. return 0;
  784. debug_cleanup:
  785. ath9k_deinit_debug(sc);
  786. unregister:
  787. ieee80211_unregister_hw(hw);
  788. rx_cleanup:
  789. ath_rx_cleanup(sc);
  790. deinit:
  791. ath9k_deinit_softc(sc);
  792. return error;
  793. }
  794. /*****************************/
  795. /* De-Initialization */
  796. /*****************************/
  797. static void ath9k_deinit_softc(struct ath_softc *sc)
  798. {
  799. int i = 0;
  800. ath9k_deinit_btcoex(sc);
  801. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  802. if (ATH_TXQ_SETUP(sc, i))
  803. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  804. ath9k_hw_deinit(sc->sc_ah);
  805. if (sc->dfs_detector != NULL)
  806. sc->dfs_detector->exit(sc->dfs_detector);
  807. ath9k_eeprom_release(sc);
  808. }
  809. void ath9k_deinit_device(struct ath_softc *sc)
  810. {
  811. struct ieee80211_hw *hw = sc->hw;
  812. ath9k_ps_wakeup(sc);
  813. wiphy_rfkill_stop_polling(sc->hw->wiphy);
  814. ath_deinit_leds(sc);
  815. ath9k_ps_restore(sc);
  816. ath9k_deinit_debug(sc);
  817. ieee80211_unregister_hw(hw);
  818. ath_rx_cleanup(sc);
  819. ath9k_deinit_softc(sc);
  820. }
  821. /************************/
  822. /* Module Hooks */
  823. /************************/
  824. static int __init ath9k_init(void)
  825. {
  826. int error;
  827. /* Register rate control algorithm */
  828. error = ath_rate_control_register();
  829. if (error != 0) {
  830. pr_err("Unable to register rate control algorithm: %d\n",
  831. error);
  832. goto err_out;
  833. }
  834. error = ath_pci_init();
  835. if (error < 0) {
  836. pr_err("No PCI devices found, driver not installed\n");
  837. error = -ENODEV;
  838. goto err_rate_unregister;
  839. }
  840. error = ath_ahb_init();
  841. if (error < 0) {
  842. error = -ENODEV;
  843. goto err_pci_exit;
  844. }
  845. return 0;
  846. err_pci_exit:
  847. ath_pci_exit();
  848. err_rate_unregister:
  849. ath_rate_control_unregister();
  850. err_out:
  851. return error;
  852. }
  853. module_init(ath9k_init);
  854. static void __exit ath9k_exit(void)
  855. {
  856. is_ath9k_unloaded = true;
  857. ath_ahb_exit();
  858. ath_pci_exit();
  859. ath_rate_control_unregister();
  860. pr_info("%s: Driver unloaded\n", dev_info);
  861. }
  862. module_exit(ath9k_exit);