mac80211.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/of.h>
  7. #include "mt76.h"
  8. #define CHAN2G(_idx, _freq) { \
  9. .band = NL80211_BAND_2GHZ, \
  10. .center_freq = (_freq), \
  11. .hw_value = (_idx), \
  12. .max_power = 30, \
  13. }
  14. #define CHAN5G(_idx, _freq) { \
  15. .band = NL80211_BAND_5GHZ, \
  16. .center_freq = (_freq), \
  17. .hw_value = (_idx), \
  18. .max_power = 30, \
  19. }
  20. static const struct ieee80211_channel mt76_channels_2ghz[] = {
  21. CHAN2G(1, 2412),
  22. CHAN2G(2, 2417),
  23. CHAN2G(3, 2422),
  24. CHAN2G(4, 2427),
  25. CHAN2G(5, 2432),
  26. CHAN2G(6, 2437),
  27. CHAN2G(7, 2442),
  28. CHAN2G(8, 2447),
  29. CHAN2G(9, 2452),
  30. CHAN2G(10, 2457),
  31. CHAN2G(11, 2462),
  32. CHAN2G(12, 2467),
  33. CHAN2G(13, 2472),
  34. CHAN2G(14, 2484),
  35. };
  36. static const struct ieee80211_channel mt76_channels_5ghz[] = {
  37. CHAN5G(36, 5180),
  38. CHAN5G(40, 5200),
  39. CHAN5G(44, 5220),
  40. CHAN5G(48, 5240),
  41. CHAN5G(52, 5260),
  42. CHAN5G(56, 5280),
  43. CHAN5G(60, 5300),
  44. CHAN5G(64, 5320),
  45. CHAN5G(100, 5500),
  46. CHAN5G(104, 5520),
  47. CHAN5G(108, 5540),
  48. CHAN5G(112, 5560),
  49. CHAN5G(116, 5580),
  50. CHAN5G(120, 5600),
  51. CHAN5G(124, 5620),
  52. CHAN5G(128, 5640),
  53. CHAN5G(132, 5660),
  54. CHAN5G(136, 5680),
  55. CHAN5G(140, 5700),
  56. CHAN5G(144, 5720),
  57. CHAN5G(149, 5745),
  58. CHAN5G(153, 5765),
  59. CHAN5G(157, 5785),
  60. CHAN5G(161, 5805),
  61. CHAN5G(165, 5825),
  62. CHAN5G(169, 5845),
  63. CHAN5G(173, 5865),
  64. };
  65. static const struct ieee80211_tpt_blink mt76_tpt_blink[] = {
  66. { .throughput = 0 * 1024, .blink_time = 334 },
  67. { .throughput = 1 * 1024, .blink_time = 260 },
  68. { .throughput = 5 * 1024, .blink_time = 220 },
  69. { .throughput = 10 * 1024, .blink_time = 190 },
  70. { .throughput = 20 * 1024, .blink_time = 170 },
  71. { .throughput = 50 * 1024, .blink_time = 150 },
  72. { .throughput = 70 * 1024, .blink_time = 130 },
  73. { .throughput = 100 * 1024, .blink_time = 110 },
  74. { .throughput = 200 * 1024, .blink_time = 80 },
  75. { .throughput = 300 * 1024, .blink_time = 50 },
  76. };
  77. struct ieee80211_rate mt76_rates[] = {
  78. CCK_RATE(0, 10),
  79. CCK_RATE(1, 20),
  80. CCK_RATE(2, 55),
  81. CCK_RATE(3, 110),
  82. OFDM_RATE(11, 60),
  83. OFDM_RATE(15, 90),
  84. OFDM_RATE(10, 120),
  85. OFDM_RATE(14, 180),
  86. OFDM_RATE(9, 240),
  87. OFDM_RATE(13, 360),
  88. OFDM_RATE(8, 480),
  89. OFDM_RATE(12, 540),
  90. };
  91. EXPORT_SYMBOL_GPL(mt76_rates);
  92. static const struct cfg80211_sar_freq_ranges mt76_sar_freq_ranges[] = {
  93. { .start_freq = 2402, .end_freq = 2494, },
  94. { .start_freq = 5150, .end_freq = 5350, },
  95. { .start_freq = 5350, .end_freq = 5470, },
  96. { .start_freq = 5470, .end_freq = 5725, },
  97. { .start_freq = 5725, .end_freq = 5950, },
  98. };
  99. const struct cfg80211_sar_capa mt76_sar_capa = {
  100. .type = NL80211_SAR_TYPE_POWER,
  101. .num_freq_ranges = ARRAY_SIZE(mt76_sar_freq_ranges),
  102. .freq_ranges = &mt76_sar_freq_ranges[0],
  103. };
  104. EXPORT_SYMBOL_GPL(mt76_sar_capa);
  105. static int mt76_led_init(struct mt76_dev *dev)
  106. {
  107. struct device_node *np = dev->dev->of_node;
  108. struct ieee80211_hw *hw = dev->hw;
  109. int led_pin;
  110. if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
  111. return 0;
  112. snprintf(dev->led_name, sizeof(dev->led_name),
  113. "mt76-%s", wiphy_name(hw->wiphy));
  114. dev->led_cdev.name = dev->led_name;
  115. dev->led_cdev.default_trigger =
  116. ieee80211_create_tpt_led_trigger(hw,
  117. IEEE80211_TPT_LEDTRIG_FL_RADIO,
  118. mt76_tpt_blink,
  119. ARRAY_SIZE(mt76_tpt_blink));
  120. np = of_get_child_by_name(np, "led");
  121. if (np) {
  122. if (!of_property_read_u32(np, "led-sources", &led_pin))
  123. dev->led_pin = led_pin;
  124. dev->led_al = of_property_read_bool(np, "led-active-low");
  125. }
  126. return led_classdev_register(dev->dev, &dev->led_cdev);
  127. }
  128. static void mt76_led_cleanup(struct mt76_dev *dev)
  129. {
  130. if (!dev->led_cdev.brightness_set && !dev->led_cdev.blink_set)
  131. return;
  132. led_classdev_unregister(&dev->led_cdev);
  133. }
  134. static void mt76_init_stream_cap(struct mt76_phy *phy,
  135. struct ieee80211_supported_band *sband,
  136. bool vht)
  137. {
  138. struct ieee80211_sta_ht_cap *ht_cap = &sband->ht_cap;
  139. int i, nstream = hweight8(phy->antenna_mask);
  140. struct ieee80211_sta_vht_cap *vht_cap;
  141. u16 mcs_map = 0;
  142. if (nstream > 1)
  143. ht_cap->cap |= IEEE80211_HT_CAP_TX_STBC;
  144. else
  145. ht_cap->cap &= ~IEEE80211_HT_CAP_TX_STBC;
  146. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
  147. ht_cap->mcs.rx_mask[i] = i < nstream ? 0xff : 0;
  148. if (!vht)
  149. return;
  150. vht_cap = &sband->vht_cap;
  151. if (nstream > 1)
  152. vht_cap->cap |= IEEE80211_VHT_CAP_TXSTBC;
  153. else
  154. vht_cap->cap &= ~IEEE80211_VHT_CAP_TXSTBC;
  155. for (i = 0; i < 8; i++) {
  156. if (i < nstream)
  157. mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2));
  158. else
  159. mcs_map |=
  160. (IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
  161. }
  162. vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
  163. vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
  164. }
  165. void mt76_set_stream_caps(struct mt76_phy *phy, bool vht)
  166. {
  167. if (phy->cap.has_2ghz)
  168. mt76_init_stream_cap(phy, &phy->sband_2g.sband, false);
  169. if (phy->cap.has_5ghz)
  170. mt76_init_stream_cap(phy, &phy->sband_5g.sband, vht);
  171. }
  172. EXPORT_SYMBOL_GPL(mt76_set_stream_caps);
  173. static int
  174. mt76_init_sband(struct mt76_phy *phy, struct mt76_sband *msband,
  175. const struct ieee80211_channel *chan, int n_chan,
  176. struct ieee80211_rate *rates, int n_rates, bool vht)
  177. {
  178. struct ieee80211_supported_band *sband = &msband->sband;
  179. struct ieee80211_sta_vht_cap *vht_cap;
  180. struct ieee80211_sta_ht_cap *ht_cap;
  181. struct mt76_dev *dev = phy->dev;
  182. void *chanlist;
  183. int size;
  184. size = n_chan * sizeof(*chan);
  185. chanlist = devm_kmemdup(dev->dev, chan, size, GFP_KERNEL);
  186. if (!chanlist)
  187. return -ENOMEM;
  188. msband->chan = devm_kcalloc(dev->dev, n_chan, sizeof(*msband->chan),
  189. GFP_KERNEL);
  190. if (!msband->chan)
  191. return -ENOMEM;
  192. sband->channels = chanlist;
  193. sband->n_channels = n_chan;
  194. sband->bitrates = rates;
  195. sband->n_bitrates = n_rates;
  196. ht_cap = &sband->ht_cap;
  197. ht_cap->ht_supported = true;
  198. ht_cap->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  199. IEEE80211_HT_CAP_GRN_FLD |
  200. IEEE80211_HT_CAP_SGI_20 |
  201. IEEE80211_HT_CAP_SGI_40 |
  202. (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
  203. ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  204. ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  205. mt76_init_stream_cap(phy, sband, vht);
  206. if (!vht)
  207. return 0;
  208. vht_cap = &sband->vht_cap;
  209. vht_cap->vht_supported = true;
  210. vht_cap->cap |= IEEE80211_VHT_CAP_RXLDPC |
  211. IEEE80211_VHT_CAP_RXSTBC_1 |
  212. IEEE80211_VHT_CAP_SHORT_GI_80 |
  213. IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
  214. IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
  215. (3 << IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT);
  216. return 0;
  217. }
  218. static int
  219. mt76_init_sband_2g(struct mt76_phy *phy, struct ieee80211_rate *rates,
  220. int n_rates)
  221. {
  222. phy->hw->wiphy->bands[NL80211_BAND_2GHZ] = &phy->sband_2g.sband;
  223. return mt76_init_sband(phy, &phy->sband_2g, mt76_channels_2ghz,
  224. ARRAY_SIZE(mt76_channels_2ghz), rates,
  225. n_rates, false);
  226. }
  227. static int
  228. mt76_init_sband_5g(struct mt76_phy *phy, struct ieee80211_rate *rates,
  229. int n_rates, bool vht)
  230. {
  231. phy->hw->wiphy->bands[NL80211_BAND_5GHZ] = &phy->sband_5g.sband;
  232. return mt76_init_sband(phy, &phy->sband_5g, mt76_channels_5ghz,
  233. ARRAY_SIZE(mt76_channels_5ghz), rates,
  234. n_rates, vht);
  235. }
  236. static void
  237. mt76_check_sband(struct mt76_phy *phy, struct mt76_sband *msband,
  238. enum nl80211_band band)
  239. {
  240. struct ieee80211_supported_band *sband = &msband->sband;
  241. bool found = false;
  242. int i;
  243. if (!sband)
  244. return;
  245. for (i = 0; i < sband->n_channels; i++) {
  246. if (sband->channels[i].flags & IEEE80211_CHAN_DISABLED)
  247. continue;
  248. found = true;
  249. break;
  250. }
  251. if (found) {
  252. phy->chandef.chan = &sband->channels[0];
  253. phy->chan_state = &msband->chan[0];
  254. return;
  255. }
  256. sband->n_channels = 0;
  257. phy->hw->wiphy->bands[band] = NULL;
  258. }
  259. static void
  260. mt76_phy_init(struct mt76_phy *phy, struct ieee80211_hw *hw)
  261. {
  262. struct mt76_dev *dev = phy->dev;
  263. struct wiphy *wiphy = hw->wiphy;
  264. SET_IEEE80211_DEV(hw, dev->dev);
  265. SET_IEEE80211_PERM_ADDR(hw, phy->macaddr);
  266. wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
  267. wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH |
  268. WIPHY_FLAG_SUPPORTS_TDLS |
  269. WIPHY_FLAG_AP_UAPSD;
  270. wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  271. wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
  272. wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AQL);
  273. wiphy->available_antennas_tx = dev->phy.antenna_mask;
  274. wiphy->available_antennas_rx = dev->phy.antenna_mask;
  275. hw->txq_data_size = sizeof(struct mt76_txq);
  276. hw->uapsd_max_sp_len = IEEE80211_WMM_IE_STA_QOSINFO_SP_ALL;
  277. if (!hw->max_tx_fragments)
  278. hw->max_tx_fragments = 16;
  279. ieee80211_hw_set(hw, SIGNAL_DBM);
  280. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  281. ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
  282. ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
  283. ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
  284. ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
  285. ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
  286. if (!(dev->drv->drv_flags & MT_DRV_AMSDU_OFFLOAD)) {
  287. ieee80211_hw_set(hw, TX_AMSDU);
  288. ieee80211_hw_set(hw, TX_FRAG_LIST);
  289. }
  290. ieee80211_hw_set(hw, MFP_CAPABLE);
  291. ieee80211_hw_set(hw, AP_LINK_PS);
  292. ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
  293. }
  294. struct mt76_phy *
  295. mt76_alloc_phy(struct mt76_dev *dev, unsigned int size,
  296. const struct ieee80211_ops *ops)
  297. {
  298. struct ieee80211_hw *hw;
  299. unsigned int phy_size;
  300. struct mt76_phy *phy;
  301. phy_size = ALIGN(sizeof(*phy), 8);
  302. hw = ieee80211_alloc_hw(size + phy_size, ops);
  303. if (!hw)
  304. return NULL;
  305. phy = hw->priv;
  306. phy->dev = dev;
  307. phy->hw = hw;
  308. phy->priv = hw->priv + phy_size;
  309. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  310. hw->wiphy->interface_modes =
  311. BIT(NL80211_IFTYPE_STATION) |
  312. BIT(NL80211_IFTYPE_AP) |
  313. #ifdef CONFIG_MAC80211_MESH
  314. BIT(NL80211_IFTYPE_MESH_POINT) |
  315. #endif
  316. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  317. BIT(NL80211_IFTYPE_P2P_GO) |
  318. BIT(NL80211_IFTYPE_ADHOC);
  319. return phy;
  320. }
  321. EXPORT_SYMBOL_GPL(mt76_alloc_phy);
  322. int mt76_register_phy(struct mt76_phy *phy, bool vht,
  323. struct ieee80211_rate *rates, int n_rates)
  324. {
  325. int ret;
  326. mt76_phy_init(phy, phy->hw);
  327. if (phy->cap.has_2ghz) {
  328. ret = mt76_init_sband_2g(phy, rates, n_rates);
  329. if (ret)
  330. return ret;
  331. }
  332. if (phy->cap.has_5ghz) {
  333. ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
  334. if (ret)
  335. return ret;
  336. }
  337. wiphy_read_of_freq_limits(phy->hw->wiphy);
  338. mt76_check_sband(phy, &phy->sband_2g, NL80211_BAND_2GHZ);
  339. mt76_check_sband(phy, &phy->sband_5g, NL80211_BAND_5GHZ);
  340. ret = ieee80211_register_hw(phy->hw);
  341. if (ret)
  342. return ret;
  343. phy->dev->phy2 = phy;
  344. return 0;
  345. }
  346. EXPORT_SYMBOL_GPL(mt76_register_phy);
  347. void mt76_unregister_phy(struct mt76_phy *phy)
  348. {
  349. struct mt76_dev *dev = phy->dev;
  350. mt76_tx_status_check(dev, NULL, true);
  351. ieee80211_unregister_hw(phy->hw);
  352. dev->phy2 = NULL;
  353. }
  354. EXPORT_SYMBOL_GPL(mt76_unregister_phy);
  355. struct mt76_dev *
  356. mt76_alloc_device(struct device *pdev, unsigned int size,
  357. const struct ieee80211_ops *ops,
  358. const struct mt76_driver_ops *drv_ops)
  359. {
  360. struct ieee80211_hw *hw;
  361. struct mt76_phy *phy;
  362. struct mt76_dev *dev;
  363. int i;
  364. hw = ieee80211_alloc_hw(size, ops);
  365. if (!hw)
  366. return NULL;
  367. dev = hw->priv;
  368. dev->hw = hw;
  369. dev->dev = pdev;
  370. dev->drv = drv_ops;
  371. phy = &dev->phy;
  372. phy->dev = dev;
  373. phy->hw = hw;
  374. spin_lock_init(&dev->rx_lock);
  375. spin_lock_init(&dev->lock);
  376. spin_lock_init(&dev->cc_lock);
  377. mutex_init(&dev->mutex);
  378. init_waitqueue_head(&dev->tx_wait);
  379. skb_queue_head_init(&dev->status_list);
  380. skb_queue_head_init(&dev->mcu.res_q);
  381. init_waitqueue_head(&dev->mcu.wait);
  382. mutex_init(&dev->mcu.mutex);
  383. dev->tx_worker.fn = mt76_tx_worker;
  384. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  385. hw->wiphy->interface_modes =
  386. BIT(NL80211_IFTYPE_STATION) |
  387. BIT(NL80211_IFTYPE_AP) |
  388. #ifdef CONFIG_MAC80211_MESH
  389. BIT(NL80211_IFTYPE_MESH_POINT) |
  390. #endif
  391. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  392. BIT(NL80211_IFTYPE_P2P_GO) |
  393. BIT(NL80211_IFTYPE_ADHOC);
  394. spin_lock_init(&dev->token_lock);
  395. idr_init(&dev->token);
  396. INIT_LIST_HEAD(&dev->txwi_cache);
  397. for (i = 0; i < ARRAY_SIZE(dev->q_rx); i++)
  398. skb_queue_head_init(&dev->rx_skb[i]);
  399. dev->wq = alloc_ordered_workqueue("mt76", 0);
  400. if (!dev->wq) {
  401. ieee80211_free_hw(hw);
  402. return NULL;
  403. }
  404. return dev;
  405. }
  406. EXPORT_SYMBOL_GPL(mt76_alloc_device);
  407. int mt76_register_device(struct mt76_dev *dev, bool vht,
  408. struct ieee80211_rate *rates, int n_rates)
  409. {
  410. struct ieee80211_hw *hw = dev->hw;
  411. struct mt76_phy *phy = &dev->phy;
  412. int ret;
  413. dev_set_drvdata(dev->dev, dev);
  414. mt76_phy_init(phy, hw);
  415. if (phy->cap.has_2ghz) {
  416. ret = mt76_init_sband_2g(phy, rates, n_rates);
  417. if (ret)
  418. return ret;
  419. }
  420. if (phy->cap.has_5ghz) {
  421. ret = mt76_init_sband_5g(phy, rates + 4, n_rates - 4, vht);
  422. if (ret)
  423. return ret;
  424. }
  425. wiphy_read_of_freq_limits(hw->wiphy);
  426. mt76_check_sband(&dev->phy, &phy->sband_2g, NL80211_BAND_2GHZ);
  427. mt76_check_sband(&dev->phy, &phy->sband_5g, NL80211_BAND_5GHZ);
  428. if (IS_ENABLED(CONFIG_MT76_LEDS)) {
  429. ret = mt76_led_init(dev);
  430. if (ret)
  431. return ret;
  432. }
  433. ret = ieee80211_register_hw(hw);
  434. if (ret)
  435. return ret;
  436. WARN_ON(mt76_worker_setup(hw, &dev->tx_worker, NULL, "tx"));
  437. sched_set_fifo_low(dev->tx_worker.task);
  438. return 0;
  439. }
  440. EXPORT_SYMBOL_GPL(mt76_register_device);
  441. void mt76_unregister_device(struct mt76_dev *dev)
  442. {
  443. struct ieee80211_hw *hw = dev->hw;
  444. if (IS_ENABLED(CONFIG_MT76_LEDS))
  445. mt76_led_cleanup(dev);
  446. mt76_tx_status_check(dev, NULL, true);
  447. ieee80211_unregister_hw(hw);
  448. }
  449. EXPORT_SYMBOL_GPL(mt76_unregister_device);
  450. void mt76_free_device(struct mt76_dev *dev)
  451. {
  452. mt76_worker_teardown(&dev->tx_worker);
  453. if (dev->wq) {
  454. destroy_workqueue(dev->wq);
  455. dev->wq = NULL;
  456. }
  457. ieee80211_free_hw(dev->hw);
  458. }
  459. EXPORT_SYMBOL_GPL(mt76_free_device);
  460. static void mt76_rx_release_amsdu(struct mt76_phy *phy, enum mt76_rxq_id q)
  461. {
  462. struct sk_buff *skb = phy->rx_amsdu[q].head;
  463. struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
  464. struct mt76_dev *dev = phy->dev;
  465. phy->rx_amsdu[q].head = NULL;
  466. phy->rx_amsdu[q].tail = NULL;
  467. /*
  468. * Validate if the amsdu has a proper first subframe.
  469. * A single MSDU can be parsed as A-MSDU when the unauthenticated A-MSDU
  470. * flag of the QoS header gets flipped. In such cases, the first
  471. * subframe has a LLC/SNAP header in the location of the destination
  472. * address.
  473. */
  474. if (skb_shinfo(skb)->frag_list) {
  475. int offset = 0;
  476. if (!(status->flag & RX_FLAG_8023)) {
  477. offset = ieee80211_get_hdrlen_from_skb(skb);
  478. if ((status->flag &
  479. (RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED)) ==
  480. RX_FLAG_DECRYPTED)
  481. offset += 8;
  482. }
  483. if (ether_addr_equal(skb->data + offset, rfc1042_header)) {
  484. dev_kfree_skb(skb);
  485. return;
  486. }
  487. }
  488. __skb_queue_tail(&dev->rx_skb[q], skb);
  489. }
  490. static void mt76_rx_release_burst(struct mt76_phy *phy, enum mt76_rxq_id q,
  491. struct sk_buff *skb)
  492. {
  493. struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
  494. if (phy->rx_amsdu[q].head &&
  495. (!status->amsdu || status->first_amsdu ||
  496. status->seqno != phy->rx_amsdu[q].seqno))
  497. mt76_rx_release_amsdu(phy, q);
  498. if (!phy->rx_amsdu[q].head) {
  499. phy->rx_amsdu[q].tail = &skb_shinfo(skb)->frag_list;
  500. phy->rx_amsdu[q].seqno = status->seqno;
  501. phy->rx_amsdu[q].head = skb;
  502. } else {
  503. *phy->rx_amsdu[q].tail = skb;
  504. phy->rx_amsdu[q].tail = &skb->next;
  505. }
  506. if (!status->amsdu || status->last_amsdu)
  507. mt76_rx_release_amsdu(phy, q);
  508. }
  509. void mt76_rx(struct mt76_dev *dev, enum mt76_rxq_id q, struct sk_buff *skb)
  510. {
  511. struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
  512. struct mt76_phy *phy = mt76_dev_phy(dev, status->ext_phy);
  513. if (!test_bit(MT76_STATE_RUNNING, &phy->state)) {
  514. dev_kfree_skb(skb);
  515. return;
  516. }
  517. #ifdef CONFIG_NL80211_TESTMODE
  518. if (phy->test.state == MT76_TM_STATE_RX_FRAMES) {
  519. phy->test.rx_stats.packets[q]++;
  520. if (status->flag & RX_FLAG_FAILED_FCS_CRC)
  521. phy->test.rx_stats.fcs_error[q]++;
  522. }
  523. #endif
  524. mt76_rx_release_burst(phy, q, skb);
  525. }
  526. EXPORT_SYMBOL_GPL(mt76_rx);
  527. bool mt76_has_tx_pending(struct mt76_phy *phy)
  528. {
  529. struct mt76_queue *q;
  530. int i;
  531. for (i = 0; i < __MT_TXQ_MAX; i++) {
  532. q = phy->q_tx[i];
  533. if (q && q->queued)
  534. return true;
  535. }
  536. return false;
  537. }
  538. EXPORT_SYMBOL_GPL(mt76_has_tx_pending);
  539. static struct mt76_channel_state *
  540. mt76_channel_state(struct mt76_phy *phy, struct ieee80211_channel *c)
  541. {
  542. struct mt76_sband *msband;
  543. int idx;
  544. if (c->band == NL80211_BAND_2GHZ)
  545. msband = &phy->sband_2g;
  546. else
  547. msband = &phy->sband_5g;
  548. idx = c - &msband->sband.channels[0];
  549. return &msband->chan[idx];
  550. }
  551. void mt76_update_survey_active_time(struct mt76_phy *phy, ktime_t time)
  552. {
  553. struct mt76_channel_state *state = phy->chan_state;
  554. state->cc_active += ktime_to_us(ktime_sub(time,
  555. phy->survey_time));
  556. phy->survey_time = time;
  557. }
  558. EXPORT_SYMBOL_GPL(mt76_update_survey_active_time);
  559. void mt76_update_survey(struct mt76_phy *phy)
  560. {
  561. struct mt76_dev *dev = phy->dev;
  562. ktime_t cur_time;
  563. if (dev->drv->update_survey)
  564. dev->drv->update_survey(phy);
  565. cur_time = ktime_get_boottime();
  566. mt76_update_survey_active_time(phy, cur_time);
  567. if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME) {
  568. struct mt76_channel_state *state = phy->chan_state;
  569. spin_lock_bh(&dev->cc_lock);
  570. state->cc_bss_rx += dev->cur_cc_bss_rx;
  571. dev->cur_cc_bss_rx = 0;
  572. spin_unlock_bh(&dev->cc_lock);
  573. }
  574. }
  575. EXPORT_SYMBOL_GPL(mt76_update_survey);
  576. void mt76_set_channel(struct mt76_phy *phy)
  577. {
  578. struct mt76_dev *dev = phy->dev;
  579. struct ieee80211_hw *hw = phy->hw;
  580. struct cfg80211_chan_def *chandef = &hw->conf.chandef;
  581. bool offchannel = hw->conf.flags & IEEE80211_CONF_OFFCHANNEL;
  582. int timeout = HZ / 5;
  583. wait_event_timeout(dev->tx_wait, !mt76_has_tx_pending(phy), timeout);
  584. mt76_update_survey(phy);
  585. phy->chandef = *chandef;
  586. phy->chan_state = mt76_channel_state(phy, chandef->chan);
  587. if (!offchannel)
  588. phy->main_chan = chandef->chan;
  589. if (chandef->chan != phy->main_chan)
  590. memset(phy->chan_state, 0, sizeof(*phy->chan_state));
  591. }
  592. EXPORT_SYMBOL_GPL(mt76_set_channel);
  593. int mt76_get_survey(struct ieee80211_hw *hw, int idx,
  594. struct survey_info *survey)
  595. {
  596. struct mt76_phy *phy = hw->priv;
  597. struct mt76_dev *dev = phy->dev;
  598. struct mt76_sband *sband;
  599. struct ieee80211_channel *chan;
  600. struct mt76_channel_state *state;
  601. int ret = 0;
  602. mutex_lock(&dev->mutex);
  603. if (idx == 0 && dev->drv->update_survey)
  604. mt76_update_survey(phy);
  605. sband = &phy->sband_2g;
  606. if (idx >= sband->sband.n_channels) {
  607. idx -= sband->sband.n_channels;
  608. sband = &phy->sband_5g;
  609. }
  610. if (idx >= sband->sband.n_channels) {
  611. ret = -ENOENT;
  612. goto out;
  613. }
  614. chan = &sband->sband.channels[idx];
  615. state = mt76_channel_state(phy, chan);
  616. memset(survey, 0, sizeof(*survey));
  617. survey->channel = chan;
  618. survey->filled = SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY;
  619. survey->filled |= dev->drv->survey_flags;
  620. if (state->noise)
  621. survey->filled |= SURVEY_INFO_NOISE_DBM;
  622. if (chan == phy->main_chan) {
  623. survey->filled |= SURVEY_INFO_IN_USE;
  624. if (dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME)
  625. survey->filled |= SURVEY_INFO_TIME_BSS_RX;
  626. }
  627. survey->time_busy = div_u64(state->cc_busy, 1000);
  628. survey->time_rx = div_u64(state->cc_rx, 1000);
  629. survey->time = div_u64(state->cc_active, 1000);
  630. survey->noise = state->noise;
  631. spin_lock_bh(&dev->cc_lock);
  632. survey->time_bss_rx = div_u64(state->cc_bss_rx, 1000);
  633. survey->time_tx = div_u64(state->cc_tx, 1000);
  634. spin_unlock_bh(&dev->cc_lock);
  635. out:
  636. mutex_unlock(&dev->mutex);
  637. return ret;
  638. }
  639. EXPORT_SYMBOL_GPL(mt76_get_survey);
  640. void mt76_wcid_key_setup(struct mt76_dev *dev, struct mt76_wcid *wcid,
  641. struct ieee80211_key_conf *key)
  642. {
  643. struct ieee80211_key_seq seq;
  644. int i;
  645. wcid->rx_check_pn = false;
  646. if (!key)
  647. return;
  648. if (key->cipher != WLAN_CIPHER_SUITE_CCMP)
  649. return;
  650. wcid->rx_check_pn = true;
  651. for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
  652. ieee80211_get_key_rx_seq(key, i, &seq);
  653. memcpy(wcid->rx_key_pn[i], seq.ccmp.pn, sizeof(seq.ccmp.pn));
  654. }
  655. }
  656. EXPORT_SYMBOL(mt76_wcid_key_setup);
  657. static void
  658. mt76_rx_convert(struct mt76_dev *dev, struct sk_buff *skb,
  659. struct ieee80211_hw **hw,
  660. struct ieee80211_sta **sta)
  661. {
  662. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  663. struct mt76_rx_status mstat;
  664. mstat = *((struct mt76_rx_status *)skb->cb);
  665. memset(status, 0, sizeof(*status));
  666. status->flag = mstat.flag;
  667. status->freq = mstat.freq;
  668. status->enc_flags = mstat.enc_flags;
  669. status->encoding = mstat.encoding;
  670. status->bw = mstat.bw;
  671. status->he_ru = mstat.he_ru;
  672. status->he_gi = mstat.he_gi;
  673. status->he_dcm = mstat.he_dcm;
  674. status->rate_idx = mstat.rate_idx;
  675. status->nss = mstat.nss;
  676. status->band = mstat.band;
  677. status->signal = mstat.signal;
  678. status->chains = mstat.chains;
  679. status->ampdu_reference = mstat.ampdu_ref;
  680. status->device_timestamp = mstat.timestamp;
  681. status->mactime = mstat.timestamp;
  682. BUILD_BUG_ON(sizeof(mstat) > sizeof(skb->cb));
  683. BUILD_BUG_ON(sizeof(status->chain_signal) !=
  684. sizeof(mstat.chain_signal));
  685. memcpy(status->chain_signal, mstat.chain_signal,
  686. sizeof(mstat.chain_signal));
  687. *sta = wcid_to_sta(mstat.wcid);
  688. *hw = mt76_phy_hw(dev, mstat.ext_phy);
  689. }
  690. static int
  691. mt76_check_ccmp_pn(struct sk_buff *skb)
  692. {
  693. struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
  694. struct mt76_wcid *wcid = status->wcid;
  695. struct ieee80211_hdr *hdr;
  696. u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
  697. int ret;
  698. if (!(status->flag & RX_FLAG_DECRYPTED))
  699. return 0;
  700. if (!wcid || !wcid->rx_check_pn)
  701. return 0;
  702. if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
  703. /*
  704. * Validate the first fragment both here and in mac80211
  705. * All further fragments will be validated by mac80211 only.
  706. */
  707. hdr = mt76_skb_get_hdr(skb);
  708. if (ieee80211_is_frag(hdr) &&
  709. !ieee80211_is_first_frag(hdr->frame_control))
  710. return 0;
  711. }
  712. BUILD_BUG_ON(sizeof(status->iv) != sizeof(wcid->rx_key_pn[0]));
  713. ret = memcmp(status->iv, wcid->rx_key_pn[tidno],
  714. sizeof(status->iv));
  715. if (ret <= 0)
  716. return -EINVAL; /* replay */
  717. memcpy(wcid->rx_key_pn[tidno], status->iv, sizeof(status->iv));
  718. if (status->flag & RX_FLAG_IV_STRIPPED)
  719. status->flag |= RX_FLAG_PN_VALIDATED;
  720. return 0;
  721. }
  722. static void
  723. mt76_airtime_report(struct mt76_dev *dev, struct mt76_rx_status *status,
  724. int len)
  725. {
  726. struct mt76_wcid *wcid = status->wcid;
  727. struct ieee80211_rx_status info = {
  728. .enc_flags = status->enc_flags,
  729. .rate_idx = status->rate_idx,
  730. .encoding = status->encoding,
  731. .band = status->band,
  732. .nss = status->nss,
  733. .bw = status->bw,
  734. };
  735. struct ieee80211_sta *sta;
  736. u32 airtime;
  737. u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
  738. airtime = ieee80211_calc_rx_airtime(dev->hw, &info, len);
  739. spin_lock(&dev->cc_lock);
  740. dev->cur_cc_bss_rx += airtime;
  741. spin_unlock(&dev->cc_lock);
  742. if (!wcid || !wcid->sta)
  743. return;
  744. sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
  745. ieee80211_sta_register_airtime(sta, tidno, 0, airtime);
  746. }
  747. static void
  748. mt76_airtime_flush_ampdu(struct mt76_dev *dev)
  749. {
  750. struct mt76_wcid *wcid;
  751. int wcid_idx;
  752. if (!dev->rx_ampdu_len)
  753. return;
  754. wcid_idx = dev->rx_ampdu_status.wcid_idx;
  755. if (wcid_idx < ARRAY_SIZE(dev->wcid))
  756. wcid = rcu_dereference(dev->wcid[wcid_idx]);
  757. else
  758. wcid = NULL;
  759. dev->rx_ampdu_status.wcid = wcid;
  760. mt76_airtime_report(dev, &dev->rx_ampdu_status, dev->rx_ampdu_len);
  761. dev->rx_ampdu_len = 0;
  762. dev->rx_ampdu_ref = 0;
  763. }
  764. static void
  765. mt76_airtime_check(struct mt76_dev *dev, struct sk_buff *skb)
  766. {
  767. struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
  768. struct mt76_wcid *wcid = status->wcid;
  769. if (!(dev->drv->drv_flags & MT_DRV_SW_RX_AIRTIME))
  770. return;
  771. if (!wcid || !wcid->sta) {
  772. struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
  773. if (status->flag & RX_FLAG_8023)
  774. return;
  775. if (!ether_addr_equal(hdr->addr1, dev->phy.macaddr))
  776. return;
  777. wcid = NULL;
  778. }
  779. if (!(status->flag & RX_FLAG_AMPDU_DETAILS) ||
  780. status->ampdu_ref != dev->rx_ampdu_ref)
  781. mt76_airtime_flush_ampdu(dev);
  782. if (status->flag & RX_FLAG_AMPDU_DETAILS) {
  783. if (!dev->rx_ampdu_len ||
  784. status->ampdu_ref != dev->rx_ampdu_ref) {
  785. dev->rx_ampdu_status = *status;
  786. dev->rx_ampdu_status.wcid_idx = wcid ? wcid->idx : 0xff;
  787. dev->rx_ampdu_ref = status->ampdu_ref;
  788. }
  789. dev->rx_ampdu_len += skb->len;
  790. return;
  791. }
  792. mt76_airtime_report(dev, status, skb->len);
  793. }
  794. static void
  795. mt76_check_sta(struct mt76_dev *dev, struct sk_buff *skb)
  796. {
  797. struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
  798. struct ieee80211_hdr *hdr = mt76_skb_get_hdr(skb);
  799. struct ieee80211_sta *sta;
  800. struct ieee80211_hw *hw;
  801. struct mt76_wcid *wcid = status->wcid;
  802. u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
  803. bool ps;
  804. hw = mt76_phy_hw(dev, status->ext_phy);
  805. if (ieee80211_is_pspoll(hdr->frame_control) && !wcid &&
  806. !(status->flag & RX_FLAG_8023)) {
  807. sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr2, NULL);
  808. if (sta)
  809. wcid = status->wcid = (struct mt76_wcid *)sta->drv_priv;
  810. }
  811. mt76_airtime_check(dev, skb);
  812. if (!wcid || !wcid->sta)
  813. return;
  814. sta = container_of((void *)wcid, struct ieee80211_sta, drv_priv);
  815. if (status->signal <= 0)
  816. ewma_signal_add(&wcid->rssi, -status->signal);
  817. wcid->inactive_count = 0;
  818. if (status->flag & RX_FLAG_8023)
  819. return;
  820. if (!test_bit(MT_WCID_FLAG_CHECK_PS, &wcid->flags))
  821. return;
  822. if (ieee80211_is_pspoll(hdr->frame_control)) {
  823. ieee80211_sta_pspoll(sta);
  824. return;
  825. }
  826. if (ieee80211_has_morefrags(hdr->frame_control) ||
  827. !(ieee80211_is_mgmt(hdr->frame_control) ||
  828. ieee80211_is_data(hdr->frame_control)))
  829. return;
  830. ps = ieee80211_has_pm(hdr->frame_control);
  831. if (ps && (ieee80211_is_data_qos(hdr->frame_control) ||
  832. ieee80211_is_qos_nullfunc(hdr->frame_control)))
  833. ieee80211_sta_uapsd_trigger(sta, tidno);
  834. if (!!test_bit(MT_WCID_FLAG_PS, &wcid->flags) == ps)
  835. return;
  836. if (ps)
  837. set_bit(MT_WCID_FLAG_PS, &wcid->flags);
  838. else
  839. clear_bit(MT_WCID_FLAG_PS, &wcid->flags);
  840. dev->drv->sta_ps(dev, sta, ps);
  841. ieee80211_sta_ps_transition(sta, ps);
  842. }
  843. void mt76_rx_complete(struct mt76_dev *dev, struct sk_buff_head *frames,
  844. struct napi_struct *napi)
  845. {
  846. struct ieee80211_sta *sta;
  847. struct ieee80211_hw *hw;
  848. struct sk_buff *skb, *tmp;
  849. LIST_HEAD(list);
  850. spin_lock(&dev->rx_lock);
  851. while ((skb = __skb_dequeue(frames)) != NULL) {
  852. struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
  853. if (mt76_check_ccmp_pn(skb)) {
  854. dev_kfree_skb(skb);
  855. continue;
  856. }
  857. skb_shinfo(skb)->frag_list = NULL;
  858. mt76_rx_convert(dev, skb, &hw, &sta);
  859. ieee80211_rx_list(hw, sta, skb, &list);
  860. /* subsequent amsdu frames */
  861. while (nskb) {
  862. skb = nskb;
  863. nskb = nskb->next;
  864. skb->next = NULL;
  865. mt76_rx_convert(dev, skb, &hw, &sta);
  866. ieee80211_rx_list(hw, sta, skb, &list);
  867. }
  868. }
  869. spin_unlock(&dev->rx_lock);
  870. if (!napi) {
  871. netif_receive_skb_list(&list);
  872. return;
  873. }
  874. list_for_each_entry_safe(skb, tmp, &list, list) {
  875. skb_list_del_init(skb);
  876. napi_gro_receive(napi, skb);
  877. }
  878. }
  879. void mt76_rx_poll_complete(struct mt76_dev *dev, enum mt76_rxq_id q,
  880. struct napi_struct *napi)
  881. {
  882. struct sk_buff_head frames;
  883. struct sk_buff *skb;
  884. __skb_queue_head_init(&frames);
  885. while ((skb = __skb_dequeue(&dev->rx_skb[q])) != NULL) {
  886. mt76_check_sta(dev, skb);
  887. mt76_rx_aggr_reorder(skb, &frames);
  888. }
  889. mt76_rx_complete(dev, &frames, napi);
  890. }
  891. EXPORT_SYMBOL_GPL(mt76_rx_poll_complete);
  892. static int
  893. mt76_sta_add(struct mt76_dev *dev, struct ieee80211_vif *vif,
  894. struct ieee80211_sta *sta, bool ext_phy)
  895. {
  896. struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
  897. int ret;
  898. int i;
  899. mutex_lock(&dev->mutex);
  900. ret = dev->drv->sta_add(dev, vif, sta);
  901. if (ret)
  902. goto out;
  903. for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
  904. struct mt76_txq *mtxq;
  905. if (!sta->txq[i])
  906. continue;
  907. mtxq = (struct mt76_txq *)sta->txq[i]->drv_priv;
  908. mtxq->wcid = wcid;
  909. }
  910. ewma_signal_init(&wcid->rssi);
  911. if (ext_phy)
  912. mt76_wcid_mask_set(dev->wcid_phy_mask, wcid->idx);
  913. wcid->ext_phy = ext_phy;
  914. rcu_assign_pointer(dev->wcid[wcid->idx], wcid);
  915. out:
  916. mutex_unlock(&dev->mutex);
  917. return ret;
  918. }
  919. void __mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
  920. struct ieee80211_sta *sta)
  921. {
  922. struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
  923. int i, idx = wcid->idx;
  924. for (i = 0; i < ARRAY_SIZE(wcid->aggr); i++)
  925. mt76_rx_aggr_stop(dev, wcid, i);
  926. if (dev->drv->sta_remove)
  927. dev->drv->sta_remove(dev, vif, sta);
  928. mt76_tx_status_check(dev, wcid, true);
  929. mt76_wcid_mask_clear(dev->wcid_mask, idx);
  930. mt76_wcid_mask_clear(dev->wcid_phy_mask, idx);
  931. }
  932. EXPORT_SYMBOL_GPL(__mt76_sta_remove);
  933. static void
  934. mt76_sta_remove(struct mt76_dev *dev, struct ieee80211_vif *vif,
  935. struct ieee80211_sta *sta)
  936. {
  937. mutex_lock(&dev->mutex);
  938. __mt76_sta_remove(dev, vif, sta);
  939. mutex_unlock(&dev->mutex);
  940. }
  941. int mt76_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  942. struct ieee80211_sta *sta,
  943. enum ieee80211_sta_state old_state,
  944. enum ieee80211_sta_state new_state)
  945. {
  946. struct mt76_phy *phy = hw->priv;
  947. struct mt76_dev *dev = phy->dev;
  948. bool ext_phy = phy != &dev->phy;
  949. if (old_state == IEEE80211_STA_NOTEXIST &&
  950. new_state == IEEE80211_STA_NONE)
  951. return mt76_sta_add(dev, vif, sta, ext_phy);
  952. if (old_state == IEEE80211_STA_AUTH &&
  953. new_state == IEEE80211_STA_ASSOC &&
  954. dev->drv->sta_assoc)
  955. dev->drv->sta_assoc(dev, vif, sta);
  956. if (old_state == IEEE80211_STA_NONE &&
  957. new_state == IEEE80211_STA_NOTEXIST)
  958. mt76_sta_remove(dev, vif, sta);
  959. return 0;
  960. }
  961. EXPORT_SYMBOL_GPL(mt76_sta_state);
  962. void mt76_sta_pre_rcu_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  963. struct ieee80211_sta *sta)
  964. {
  965. struct mt76_phy *phy = hw->priv;
  966. struct mt76_dev *dev = phy->dev;
  967. struct mt76_wcid *wcid = (struct mt76_wcid *)sta->drv_priv;
  968. mutex_lock(&dev->mutex);
  969. rcu_assign_pointer(dev->wcid[wcid->idx], NULL);
  970. mutex_unlock(&dev->mutex);
  971. }
  972. EXPORT_SYMBOL_GPL(mt76_sta_pre_rcu_remove);
  973. int mt76_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  974. int *dbm)
  975. {
  976. struct mt76_phy *phy = hw->priv;
  977. int n_chains = hweight8(phy->antenna_mask);
  978. int delta = mt76_tx_power_nss_delta(n_chains);
  979. *dbm = DIV_ROUND_UP(phy->txpower_cur + delta, 2);
  980. return 0;
  981. }
  982. EXPORT_SYMBOL_GPL(mt76_get_txpower);
  983. static void
  984. __mt76_csa_finish(void *priv, u8 *mac, struct ieee80211_vif *vif)
  985. {
  986. if (vif->csa_active && ieee80211_beacon_cntdwn_is_complete(vif))
  987. ieee80211_csa_finish(vif);
  988. }
  989. void mt76_csa_finish(struct mt76_dev *dev)
  990. {
  991. if (!dev->csa_complete)
  992. return;
  993. ieee80211_iterate_active_interfaces_atomic(dev->hw,
  994. IEEE80211_IFACE_ITER_RESUME_ALL,
  995. __mt76_csa_finish, dev);
  996. dev->csa_complete = 0;
  997. }
  998. EXPORT_SYMBOL_GPL(mt76_csa_finish);
  999. static void
  1000. __mt76_csa_check(void *priv, u8 *mac, struct ieee80211_vif *vif)
  1001. {
  1002. struct mt76_dev *dev = priv;
  1003. if (!vif->csa_active)
  1004. return;
  1005. dev->csa_complete |= ieee80211_beacon_cntdwn_is_complete(vif);
  1006. }
  1007. void mt76_csa_check(struct mt76_dev *dev)
  1008. {
  1009. ieee80211_iterate_active_interfaces_atomic(dev->hw,
  1010. IEEE80211_IFACE_ITER_RESUME_ALL,
  1011. __mt76_csa_check, dev);
  1012. }
  1013. EXPORT_SYMBOL_GPL(mt76_csa_check);
  1014. int
  1015. mt76_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, bool set)
  1016. {
  1017. return 0;
  1018. }
  1019. EXPORT_SYMBOL_GPL(mt76_set_tim);
  1020. void mt76_insert_ccmp_hdr(struct sk_buff *skb, u8 key_id)
  1021. {
  1022. struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
  1023. int hdr_len = ieee80211_get_hdrlen_from_skb(skb);
  1024. u8 *hdr, *pn = status->iv;
  1025. __skb_push(skb, 8);
  1026. memmove(skb->data, skb->data + 8, hdr_len);
  1027. hdr = skb->data + hdr_len;
  1028. hdr[0] = pn[5];
  1029. hdr[1] = pn[4];
  1030. hdr[2] = 0;
  1031. hdr[3] = 0x20 | (key_id << 6);
  1032. hdr[4] = pn[3];
  1033. hdr[5] = pn[2];
  1034. hdr[6] = pn[1];
  1035. hdr[7] = pn[0];
  1036. status->flag &= ~RX_FLAG_IV_STRIPPED;
  1037. }
  1038. EXPORT_SYMBOL_GPL(mt76_insert_ccmp_hdr);
  1039. int mt76_get_rate(struct mt76_dev *dev,
  1040. struct ieee80211_supported_band *sband,
  1041. int idx, bool cck)
  1042. {
  1043. int i, offset = 0, len = sband->n_bitrates;
  1044. if (cck) {
  1045. if (sband == &dev->phy.sband_5g.sband)
  1046. return 0;
  1047. idx &= ~BIT(2); /* short preamble */
  1048. } else if (sband == &dev->phy.sband_2g.sband) {
  1049. offset = 4;
  1050. }
  1051. for (i = offset; i < len; i++) {
  1052. if ((sband->bitrates[i].hw_value & GENMASK(7, 0)) == idx)
  1053. return i;
  1054. }
  1055. return 0;
  1056. }
  1057. EXPORT_SYMBOL_GPL(mt76_get_rate);
  1058. void mt76_sw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1059. const u8 *mac)
  1060. {
  1061. struct mt76_phy *phy = hw->priv;
  1062. set_bit(MT76_SCANNING, &phy->state);
  1063. }
  1064. EXPORT_SYMBOL_GPL(mt76_sw_scan);
  1065. void mt76_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1066. {
  1067. struct mt76_phy *phy = hw->priv;
  1068. clear_bit(MT76_SCANNING, &phy->state);
  1069. }
  1070. EXPORT_SYMBOL_GPL(mt76_sw_scan_complete);
  1071. int mt76_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  1072. {
  1073. struct mt76_phy *phy = hw->priv;
  1074. struct mt76_dev *dev = phy->dev;
  1075. mutex_lock(&dev->mutex);
  1076. *tx_ant = phy->antenna_mask;
  1077. *rx_ant = phy->antenna_mask;
  1078. mutex_unlock(&dev->mutex);
  1079. return 0;
  1080. }
  1081. EXPORT_SYMBOL_GPL(mt76_get_antenna);
  1082. struct mt76_queue *
  1083. mt76_init_queue(struct mt76_dev *dev, int qid, int idx, int n_desc,
  1084. int ring_base)
  1085. {
  1086. struct mt76_queue *hwq;
  1087. int err;
  1088. hwq = devm_kzalloc(dev->dev, sizeof(*hwq), GFP_KERNEL);
  1089. if (!hwq)
  1090. return ERR_PTR(-ENOMEM);
  1091. err = dev->queue_ops->alloc(dev, hwq, idx, n_desc, 0, ring_base);
  1092. if (err < 0)
  1093. return ERR_PTR(err);
  1094. return hwq;
  1095. }
  1096. EXPORT_SYMBOL_GPL(mt76_init_queue);
  1097. u16 mt76_default_basic_rate(struct mt76_phy *phy, struct ieee80211_vif *vif)
  1098. {
  1099. int i = ffs(vif->bss_conf.basic_rates) - 1, offset = 0;
  1100. struct ieee80211_rate *rate;
  1101. if (phy->chandef.chan->band == NL80211_BAND_5GHZ)
  1102. offset = 4;
  1103. rate = &mt76_rates[offset + i];
  1104. return rate->hw_value;
  1105. }
  1106. EXPORT_SYMBOL_GPL(mt76_default_basic_rate);