init.c 29 KB

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