init.c 25 KB

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