init.c 27 KB

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