init.c 26 KB

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