init.c 27 KB

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