init.c 25 KB

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